]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
a38718a6 | 24 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. |
1bd201e7 | 25 | * Copyright (c) 2012 by Delphix. All rights reserved. |
a38718a6 | 26 | */ |
34dc7c2f | 27 | |
34dc7c2f BB |
28 | /* |
29 | * This file contains all the routines used when modifying on-disk SPA state. | |
30 | * This includes opening, importing, destroying, exporting a pool, and syncing a | |
31 | * pool. | |
32 | */ | |
33 | ||
34 | #include <sys/zfs_context.h> | |
35 | #include <sys/fm/fs/zfs.h> | |
36 | #include <sys/spa_impl.h> | |
37 | #include <sys/zio.h> | |
38 | #include <sys/zio_checksum.h> | |
34dc7c2f BB |
39 | #include <sys/dmu.h> |
40 | #include <sys/dmu_tx.h> | |
41 | #include <sys/zap.h> | |
42 | #include <sys/zil.h> | |
428870ff | 43 | #include <sys/ddt.h> |
34dc7c2f | 44 | #include <sys/vdev_impl.h> |
c28b2279 | 45 | #include <sys/vdev_disk.h> |
34dc7c2f | 46 | #include <sys/metaslab.h> |
428870ff | 47 | #include <sys/metaslab_impl.h> |
34dc7c2f BB |
48 | #include <sys/uberblock_impl.h> |
49 | #include <sys/txg.h> | |
50 | #include <sys/avl.h> | |
51 | #include <sys/dmu_traverse.h> | |
52 | #include <sys/dmu_objset.h> | |
53 | #include <sys/unique.h> | |
54 | #include <sys/dsl_pool.h> | |
55 | #include <sys/dsl_dataset.h> | |
56 | #include <sys/dsl_dir.h> | |
57 | #include <sys/dsl_prop.h> | |
58 | #include <sys/dsl_synctask.h> | |
59 | #include <sys/fs/zfs.h> | |
60 | #include <sys/arc.h> | |
61 | #include <sys/callb.h> | |
62 | #include <sys/systeminfo.h> | |
34dc7c2f | 63 | #include <sys/spa_boot.h> |
9babb374 | 64 | #include <sys/zfs_ioctl.h> |
428870ff | 65 | #include <sys/dsl_scan.h> |
9ae529ec | 66 | #include <sys/zfeature.h> |
34dc7c2f | 67 | |
d164b209 | 68 | #ifdef _KERNEL |
428870ff BB |
69 | #include <sys/bootprops.h> |
70 | #include <sys/callb.h> | |
71 | #include <sys/cpupart.h> | |
72 | #include <sys/pool.h> | |
73 | #include <sys/sysdc.h> | |
d164b209 BB |
74 | #include <sys/zone.h> |
75 | #endif /* _KERNEL */ | |
76 | ||
34dc7c2f BB |
77 | #include "zfs_prop.h" |
78 | #include "zfs_comutil.h" | |
79 | ||
428870ff | 80 | typedef enum zti_modes { |
9babb374 BB |
81 | zti_mode_fixed, /* value is # of threads (min 1) */ |
82 | zti_mode_online_percent, /* value is % of online CPUs */ | |
428870ff BB |
83 | zti_mode_batch, /* cpu-intensive; value is ignored */ |
84 | zti_mode_null, /* don't create a taskq */ | |
9babb374 | 85 | zti_nmodes |
428870ff | 86 | } zti_modes_t; |
34dc7c2f | 87 | |
428870ff BB |
88 | #define ZTI_FIX(n) { zti_mode_fixed, (n) } |
89 | #define ZTI_PCT(n) { zti_mode_online_percent, (n) } | |
90 | #define ZTI_BATCH { zti_mode_batch, 0 } | |
91 | #define ZTI_NULL { zti_mode_null, 0 } | |
9babb374 | 92 | |
428870ff | 93 | #define ZTI_ONE ZTI_FIX(1) |
9babb374 BB |
94 | |
95 | typedef struct zio_taskq_info { | |
428870ff BB |
96 | enum zti_modes zti_mode; |
97 | uint_t zti_value; | |
9babb374 BB |
98 | } zio_taskq_info_t; |
99 | ||
100 | static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = { | |
451041db | 101 | "iss", "iss_h", "int", "int_h" |
9babb374 BB |
102 | }; |
103 | ||
428870ff BB |
104 | /* |
105 | * Define the taskq threads for the following I/O types: | |
106 | * NULL, READ, WRITE, FREE, CLAIM, and IOCTL | |
107 | */ | |
108 | const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = { | |
109 | /* ISSUE ISSUE_HIGH INTR INTR_HIGH */ | |
110 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, | |
111 | { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, | |
7e55f4e0 | 112 | { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(16), ZTI_FIX(5) }, |
5853fe79 | 113 | { ZTI_FIX(8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, |
428870ff BB |
114 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, |
115 | { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, | |
9babb374 BB |
116 | }; |
117 | ||
9ae529ec | 118 | static dsl_syncfunc_t spa_sync_version; |
428870ff | 119 | static dsl_syncfunc_t spa_sync_props; |
3bc7e0fb GW |
120 | static dsl_checkfunc_t spa_change_guid_check; |
121 | static dsl_syncfunc_t spa_change_guid_sync; | |
b128c09f | 122 | static boolean_t spa_has_active_shared_spare(spa_t *spa); |
bf701a83 | 123 | static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config, |
428870ff BB |
124 | spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, |
125 | char **ereport); | |
572e2857 | 126 | static void spa_vdev_resilver_done(spa_t *spa); |
428870ff BB |
127 | |
128 | uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */ | |
129 | id_t zio_taskq_psrset_bind = PS_NONE; | |
130 | boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */ | |
131 | uint_t zio_taskq_basedc = 80; /* base duty cycle */ | |
132 | ||
133 | boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */ | |
134 | ||
135 | /* | |
136 | * This (illegal) pool name is used when temporarily importing a spa_t in order | |
137 | * to get the vdev stats associated with the imported devices. | |
138 | */ | |
139 | #define TRYIMPORT_NAME "$import" | |
34dc7c2f BB |
140 | |
141 | /* | |
142 | * ========================================================================== | |
143 | * SPA properties routines | |
144 | * ========================================================================== | |
145 | */ | |
146 | ||
147 | /* | |
148 | * Add a (source=src, propname=propval) list to an nvlist. | |
149 | */ | |
150 | static void | |
151 | spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval, | |
152 | uint64_t intval, zprop_source_t src) | |
153 | { | |
154 | const char *propname = zpool_prop_to_name(prop); | |
155 | nvlist_t *propval; | |
156 | ||
b8d06fca | 157 | VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
34dc7c2f BB |
158 | VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0); |
159 | ||
160 | if (strval != NULL) | |
161 | VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0); | |
162 | else | |
163 | VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0); | |
164 | ||
165 | VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0); | |
166 | nvlist_free(propval); | |
167 | } | |
168 | ||
169 | /* | |
170 | * Get property values from the spa configuration. | |
171 | */ | |
172 | static void | |
173 | spa_prop_get_config(spa_t *spa, nvlist_t **nvp) | |
174 | { | |
1bd201e7 | 175 | vdev_t *rvd = spa->spa_root_vdev; |
9ae529ec | 176 | dsl_pool_t *pool = spa->spa_dsl_pool; |
d164b209 | 177 | uint64_t size; |
428870ff | 178 | uint64_t alloc; |
1bd201e7 | 179 | uint64_t space; |
34dc7c2f BB |
180 | uint64_t cap, version; |
181 | zprop_source_t src = ZPROP_SRC_NONE; | |
b128c09f | 182 | spa_config_dirent_t *dp; |
1bd201e7 | 183 | int c; |
b128c09f BB |
184 | |
185 | ASSERT(MUTEX_HELD(&spa->spa_props_lock)); | |
34dc7c2f | 186 | |
1bd201e7 | 187 | if (rvd != NULL) { |
428870ff BB |
188 | alloc = metaslab_class_get_alloc(spa_normal_class(spa)); |
189 | size = metaslab_class_get_space(spa_normal_class(spa)); | |
d164b209 BB |
190 | spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src); |
191 | spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src); | |
428870ff BB |
192 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src); |
193 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL, | |
194 | size - alloc, src); | |
1bd201e7 CS |
195 | |
196 | space = 0; | |
197 | for (c = 0; c < rvd->vdev_children; c++) { | |
198 | vdev_t *tvd = rvd->vdev_child[c]; | |
199 | space += tvd->vdev_max_asize - tvd->vdev_asize; | |
200 | } | |
201 | spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space, | |
202 | src); | |
203 | ||
572e2857 BB |
204 | spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL, |
205 | (spa_mode(spa) == FREAD), src); | |
d164b209 | 206 | |
428870ff | 207 | cap = (size == 0) ? 0 : (alloc * 100 / size); |
d164b209 BB |
208 | spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src); |
209 | ||
428870ff BB |
210 | spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL, |
211 | ddt_get_pool_dedup_ratio(spa), src); | |
212 | ||
d164b209 | 213 | spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL, |
1bd201e7 | 214 | rvd->vdev_state, src); |
d164b209 BB |
215 | |
216 | version = spa_version(spa); | |
217 | if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION)) | |
218 | src = ZPROP_SRC_DEFAULT; | |
219 | else | |
220 | src = ZPROP_SRC_LOCAL; | |
221 | spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src); | |
222 | } | |
34dc7c2f | 223 | |
9ae529ec CS |
224 | if (pool != NULL) { |
225 | dsl_dir_t *freedir = pool->dp_free_dir; | |
226 | ||
227 | /* | |
228 | * The $FREE directory was introduced in SPA_VERSION_DEADLISTS, | |
229 | * when opening pools before this version freedir will be NULL. | |
230 | */ | |
231 | if (freedir != NULL) { | |
232 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL, | |
233 | freedir->dd_phys->dd_used_bytes, src); | |
234 | } else { | |
235 | spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, | |
236 | NULL, 0, src); | |
237 | } | |
238 | } | |
239 | ||
34dc7c2f | 240 | spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src); |
34dc7c2f | 241 | |
d96eb2b1 DM |
242 | if (spa->spa_comment != NULL) { |
243 | spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment, | |
244 | 0, ZPROP_SRC_LOCAL); | |
245 | } | |
246 | ||
34dc7c2f BB |
247 | if (spa->spa_root != NULL) |
248 | spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root, | |
249 | 0, ZPROP_SRC_LOCAL); | |
250 | ||
b128c09f BB |
251 | if ((dp = list_head(&spa->spa_config_list)) != NULL) { |
252 | if (dp->scd_path == NULL) { | |
34dc7c2f | 253 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f BB |
254 | "none", 0, ZPROP_SRC_LOCAL); |
255 | } else if (strcmp(dp->scd_path, spa_config_path) != 0) { | |
34dc7c2f | 256 | spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE, |
b128c09f | 257 | dp->scd_path, 0, ZPROP_SRC_LOCAL); |
34dc7c2f BB |
258 | } |
259 | } | |
260 | } | |
261 | ||
262 | /* | |
263 | * Get zpool property values. | |
264 | */ | |
265 | int | |
266 | spa_prop_get(spa_t *spa, nvlist_t **nvp) | |
267 | { | |
428870ff | 268 | objset_t *mos = spa->spa_meta_objset; |
34dc7c2f BB |
269 | zap_cursor_t zc; |
270 | zap_attribute_t za; | |
34dc7c2f BB |
271 | int err; |
272 | ||
b8d06fca | 273 | err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_PUSHPAGE); |
c28b2279 BB |
274 | if (err) |
275 | return err; | |
34dc7c2f | 276 | |
b128c09f BB |
277 | mutex_enter(&spa->spa_props_lock); |
278 | ||
34dc7c2f BB |
279 | /* |
280 | * Get properties from the spa config. | |
281 | */ | |
282 | spa_prop_get_config(spa, nvp); | |
283 | ||
34dc7c2f | 284 | /* If no pool property object, no more prop to get. */ |
428870ff | 285 | if (mos == NULL || spa->spa_pool_props_object == 0) { |
34dc7c2f | 286 | mutex_exit(&spa->spa_props_lock); |
c28b2279 | 287 | goto out; |
34dc7c2f BB |
288 | } |
289 | ||
290 | /* | |
291 | * Get properties from the MOS pool property object. | |
292 | */ | |
293 | for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object); | |
294 | (err = zap_cursor_retrieve(&zc, &za)) == 0; | |
295 | zap_cursor_advance(&zc)) { | |
296 | uint64_t intval = 0; | |
297 | char *strval = NULL; | |
298 | zprop_source_t src = ZPROP_SRC_DEFAULT; | |
299 | zpool_prop_t prop; | |
300 | ||
301 | if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL) | |
302 | continue; | |
303 | ||
304 | switch (za.za_integer_length) { | |
305 | case 8: | |
306 | /* integer property */ | |
307 | if (za.za_first_integer != | |
308 | zpool_prop_default_numeric(prop)) | |
309 | src = ZPROP_SRC_LOCAL; | |
310 | ||
311 | if (prop == ZPOOL_PROP_BOOTFS) { | |
312 | dsl_pool_t *dp; | |
313 | dsl_dataset_t *ds = NULL; | |
314 | ||
315 | dp = spa_get_dsl(spa); | |
316 | rw_enter(&dp->dp_config_rwlock, RW_READER); | |
c65aa5b2 BB |
317 | if ((err = dsl_dataset_hold_obj(dp, |
318 | za.za_first_integer, FTAG, &ds))) { | |
34dc7c2f BB |
319 | rw_exit(&dp->dp_config_rwlock); |
320 | break; | |
321 | } | |
322 | ||
323 | strval = kmem_alloc( | |
324 | MAXNAMELEN + strlen(MOS_DIR_NAME) + 1, | |
b8d06fca | 325 | KM_PUSHPAGE); |
34dc7c2f | 326 | dsl_dataset_name(ds, strval); |
b128c09f | 327 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
328 | rw_exit(&dp->dp_config_rwlock); |
329 | } else { | |
330 | strval = NULL; | |
331 | intval = za.za_first_integer; | |
332 | } | |
333 | ||
334 | spa_prop_add_list(*nvp, prop, strval, intval, src); | |
335 | ||
336 | if (strval != NULL) | |
337 | kmem_free(strval, | |
338 | MAXNAMELEN + strlen(MOS_DIR_NAME) + 1); | |
339 | ||
340 | break; | |
341 | ||
342 | case 1: | |
343 | /* string property */ | |
b8d06fca | 344 | strval = kmem_alloc(za.za_num_integers, KM_PUSHPAGE); |
34dc7c2f BB |
345 | err = zap_lookup(mos, spa->spa_pool_props_object, |
346 | za.za_name, 1, za.za_num_integers, strval); | |
347 | if (err) { | |
348 | kmem_free(strval, za.za_num_integers); | |
349 | break; | |
350 | } | |
351 | spa_prop_add_list(*nvp, prop, strval, 0, src); | |
352 | kmem_free(strval, za.za_num_integers); | |
353 | break; | |
354 | ||
355 | default: | |
356 | break; | |
357 | } | |
358 | } | |
359 | zap_cursor_fini(&zc); | |
360 | mutex_exit(&spa->spa_props_lock); | |
361 | out: | |
362 | if (err && err != ENOENT) { | |
363 | nvlist_free(*nvp); | |
364 | *nvp = NULL; | |
365 | return (err); | |
366 | } | |
367 | ||
368 | return (0); | |
369 | } | |
370 | ||
371 | /* | |
372 | * Validate the given pool properties nvlist and modify the list | |
373 | * for the property values to be set. | |
374 | */ | |
375 | static int | |
376 | spa_prop_validate(spa_t *spa, nvlist_t *props) | |
377 | { | |
378 | nvpair_t *elem; | |
379 | int error = 0, reset_bootfs = 0; | |
d4ed6673 | 380 | uint64_t objnum = 0; |
9ae529ec | 381 | boolean_t has_feature = B_FALSE; |
34dc7c2f BB |
382 | |
383 | elem = NULL; | |
384 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
34dc7c2f | 385 | uint64_t intval; |
9ae529ec CS |
386 | char *strval, *slash, *check, *fname; |
387 | const char *propname = nvpair_name(elem); | |
388 | zpool_prop_t prop = zpool_name_to_prop(propname); | |
389 | ||
390 | switch ((int)prop) { | |
391 | case ZPROP_INVAL: | |
392 | if (!zpool_prop_feature(propname)) { | |
393 | error = EINVAL; | |
394 | break; | |
395 | } | |
396 | ||
397 | /* | |
398 | * Sanitize the input. | |
399 | */ | |
400 | if (nvpair_type(elem) != DATA_TYPE_UINT64) { | |
401 | error = EINVAL; | |
402 | break; | |
403 | } | |
404 | ||
405 | if (nvpair_value_uint64(elem, &intval) != 0) { | |
406 | error = EINVAL; | |
407 | break; | |
408 | } | |
34dc7c2f | 409 | |
9ae529ec CS |
410 | if (intval != 0) { |
411 | error = EINVAL; | |
412 | break; | |
413 | } | |
34dc7c2f | 414 | |
9ae529ec CS |
415 | fname = strchr(propname, '@') + 1; |
416 | if (zfeature_lookup_name(fname, NULL) != 0) { | |
417 | error = EINVAL; | |
418 | break; | |
419 | } | |
420 | ||
421 | has_feature = B_TRUE; | |
422 | break; | |
34dc7c2f | 423 | |
34dc7c2f BB |
424 | case ZPOOL_PROP_VERSION: |
425 | error = nvpair_value_uint64(elem, &intval); | |
426 | if (!error && | |
9ae529ec CS |
427 | (intval < spa_version(spa) || |
428 | intval > SPA_VERSION_BEFORE_FEATURES || | |
429 | has_feature)) | |
34dc7c2f BB |
430 | error = EINVAL; |
431 | break; | |
432 | ||
433 | case ZPOOL_PROP_DELEGATION: | |
434 | case ZPOOL_PROP_AUTOREPLACE: | |
b128c09f | 435 | case ZPOOL_PROP_LISTSNAPS: |
9babb374 | 436 | case ZPOOL_PROP_AUTOEXPAND: |
34dc7c2f BB |
437 | error = nvpair_value_uint64(elem, &intval); |
438 | if (!error && intval > 1) | |
439 | error = EINVAL; | |
440 | break; | |
441 | ||
442 | case ZPOOL_PROP_BOOTFS: | |
9babb374 BB |
443 | /* |
444 | * If the pool version is less than SPA_VERSION_BOOTFS, | |
445 | * or the pool is still being created (version == 0), | |
446 | * the bootfs property cannot be set. | |
447 | */ | |
34dc7c2f BB |
448 | if (spa_version(spa) < SPA_VERSION_BOOTFS) { |
449 | error = ENOTSUP; | |
450 | break; | |
451 | } | |
452 | ||
453 | /* | |
b128c09f | 454 | * Make sure the vdev config is bootable |
34dc7c2f | 455 | */ |
b128c09f | 456 | if (!vdev_is_bootable(spa->spa_root_vdev)) { |
34dc7c2f BB |
457 | error = ENOTSUP; |
458 | break; | |
459 | } | |
460 | ||
461 | reset_bootfs = 1; | |
462 | ||
463 | error = nvpair_value_string(elem, &strval); | |
464 | ||
465 | if (!error) { | |
9ae529ec | 466 | objset_t *os; |
b128c09f BB |
467 | uint64_t compress; |
468 | ||
34dc7c2f BB |
469 | if (strval == NULL || strval[0] == '\0') { |
470 | objnum = zpool_prop_default_numeric( | |
471 | ZPOOL_PROP_BOOTFS); | |
472 | break; | |
473 | } | |
474 | ||
c65aa5b2 | 475 | if ((error = dmu_objset_hold(strval,FTAG,&os))) |
34dc7c2f | 476 | break; |
b128c09f | 477 | |
428870ff BB |
478 | /* Must be ZPL and not gzip compressed. */ |
479 | ||
480 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
481 | error = ENOTSUP; | |
482 | } else if ((error = dsl_prop_get_integer(strval, | |
b128c09f BB |
483 | zfs_prop_to_name(ZFS_PROP_COMPRESSION), |
484 | &compress, NULL)) == 0 && | |
485 | !BOOTFS_COMPRESS_VALID(compress)) { | |
486 | error = ENOTSUP; | |
487 | } else { | |
488 | objnum = dmu_objset_id(os); | |
489 | } | |
428870ff | 490 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
491 | } |
492 | break; | |
b128c09f | 493 | |
34dc7c2f BB |
494 | case ZPOOL_PROP_FAILUREMODE: |
495 | error = nvpair_value_uint64(elem, &intval); | |
496 | if (!error && (intval < ZIO_FAILURE_MODE_WAIT || | |
497 | intval > ZIO_FAILURE_MODE_PANIC)) | |
498 | error = EINVAL; | |
499 | ||
500 | /* | |
501 | * This is a special case which only occurs when | |
502 | * the pool has completely failed. This allows | |
503 | * the user to change the in-core failmode property | |
504 | * without syncing it out to disk (I/Os might | |
505 | * currently be blocked). We do this by returning | |
506 | * EIO to the caller (spa_prop_set) to trick it | |
507 | * into thinking we encountered a property validation | |
508 | * error. | |
509 | */ | |
b128c09f | 510 | if (!error && spa_suspended(spa)) { |
34dc7c2f BB |
511 | spa->spa_failmode = intval; |
512 | error = EIO; | |
513 | } | |
514 | break; | |
515 | ||
516 | case ZPOOL_PROP_CACHEFILE: | |
517 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
518 | break; | |
519 | ||
520 | if (strval[0] == '\0') | |
521 | break; | |
522 | ||
523 | if (strcmp(strval, "none") == 0) | |
524 | break; | |
525 | ||
526 | if (strval[0] != '/') { | |
527 | error = EINVAL; | |
528 | break; | |
529 | } | |
530 | ||
531 | slash = strrchr(strval, '/'); | |
532 | ASSERT(slash != NULL); | |
533 | ||
534 | if (slash[1] == '\0' || strcmp(slash, "/.") == 0 || | |
535 | strcmp(slash, "/..") == 0) | |
536 | error = EINVAL; | |
537 | break; | |
428870ff | 538 | |
d96eb2b1 DM |
539 | case ZPOOL_PROP_COMMENT: |
540 | if ((error = nvpair_value_string(elem, &strval)) != 0) | |
541 | break; | |
542 | for (check = strval; *check != '\0'; check++) { | |
543 | if (!isprint(*check)) { | |
544 | error = EINVAL; | |
545 | break; | |
546 | } | |
547 | check++; | |
548 | } | |
549 | if (strlen(strval) > ZPROP_MAX_COMMENT) | |
550 | error = E2BIG; | |
551 | break; | |
552 | ||
428870ff BB |
553 | case ZPOOL_PROP_DEDUPDITTO: |
554 | if (spa_version(spa) < SPA_VERSION_DEDUP) | |
555 | error = ENOTSUP; | |
556 | else | |
557 | error = nvpair_value_uint64(elem, &intval); | |
558 | if (error == 0 && | |
559 | intval != 0 && intval < ZIO_DEDUPDITTO_MIN) | |
560 | error = EINVAL; | |
561 | break; | |
e75c13c3 BB |
562 | |
563 | default: | |
564 | break; | |
34dc7c2f BB |
565 | } |
566 | ||
567 | if (error) | |
568 | break; | |
569 | } | |
570 | ||
571 | if (!error && reset_bootfs) { | |
572 | error = nvlist_remove(props, | |
573 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING); | |
574 | ||
575 | if (!error) { | |
576 | error = nvlist_add_uint64(props, | |
577 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum); | |
578 | } | |
579 | } | |
580 | ||
581 | return (error); | |
582 | } | |
583 | ||
d164b209 BB |
584 | void |
585 | spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync) | |
586 | { | |
587 | char *cachefile; | |
588 | spa_config_dirent_t *dp; | |
589 | ||
590 | if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), | |
591 | &cachefile) != 0) | |
592 | return; | |
593 | ||
594 | dp = kmem_alloc(sizeof (spa_config_dirent_t), | |
b8d06fca | 595 | KM_PUSHPAGE); |
d164b209 BB |
596 | |
597 | if (cachefile[0] == '\0') | |
598 | dp->scd_path = spa_strdup(spa_config_path); | |
599 | else if (strcmp(cachefile, "none") == 0) | |
600 | dp->scd_path = NULL; | |
601 | else | |
602 | dp->scd_path = spa_strdup(cachefile); | |
603 | ||
604 | list_insert_head(&spa->spa_config_list, dp); | |
605 | if (need_sync) | |
606 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
607 | } | |
608 | ||
34dc7c2f BB |
609 | int |
610 | spa_prop_set(spa_t *spa, nvlist_t *nvp) | |
611 | { | |
612 | int error; | |
9ae529ec | 613 | nvpair_t *elem = NULL; |
d164b209 | 614 | boolean_t need_sync = B_FALSE; |
34dc7c2f BB |
615 | |
616 | if ((error = spa_prop_validate(spa, nvp)) != 0) | |
617 | return (error); | |
618 | ||
d164b209 | 619 | while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) { |
9ae529ec | 620 | zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem)); |
d164b209 | 621 | |
572e2857 BB |
622 | if (prop == ZPOOL_PROP_CACHEFILE || |
623 | prop == ZPOOL_PROP_ALTROOT || | |
624 | prop == ZPOOL_PROP_READONLY) | |
d164b209 BB |
625 | continue; |
626 | ||
9ae529ec CS |
627 | if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) { |
628 | uint64_t ver; | |
629 | ||
630 | if (prop == ZPOOL_PROP_VERSION) { | |
631 | VERIFY(nvpair_value_uint64(elem, &ver) == 0); | |
632 | } else { | |
633 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
634 | ver = SPA_VERSION_FEATURES; | |
635 | need_sync = B_TRUE; | |
636 | } | |
637 | ||
638 | /* Save time if the version is already set. */ | |
639 | if (ver == spa_version(spa)) | |
640 | continue; | |
641 | ||
642 | /* | |
643 | * In addition to the pool directory object, we might | |
644 | * create the pool properties object, the features for | |
645 | * read object, the features for write object, or the | |
646 | * feature descriptions object. | |
647 | */ | |
648 | error = dsl_sync_task_do(spa_get_dsl(spa), NULL, | |
649 | spa_sync_version, spa, &ver, 6); | |
650 | if (error) | |
651 | return (error); | |
652 | continue; | |
653 | } | |
654 | ||
d164b209 BB |
655 | need_sync = B_TRUE; |
656 | break; | |
657 | } | |
658 | ||
9ae529ec | 659 | if (need_sync) { |
d164b209 | 660 | return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props, |
9ae529ec CS |
661 | spa, nvp, 6)); |
662 | } | |
663 | ||
664 | return (0); | |
34dc7c2f BB |
665 | } |
666 | ||
667 | /* | |
668 | * If the bootfs property value is dsobj, clear it. | |
669 | */ | |
670 | void | |
671 | spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx) | |
672 | { | |
673 | if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) { | |
674 | VERIFY(zap_remove(spa->spa_meta_objset, | |
675 | spa->spa_pool_props_object, | |
676 | zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0); | |
677 | spa->spa_bootfs = 0; | |
678 | } | |
679 | } | |
680 | ||
3bc7e0fb GW |
681 | /*ARGSUSED*/ |
682 | static int | |
683 | spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx) | |
684 | { | |
685 | spa_t *spa = arg1; | |
686 | vdev_t *rvd = spa->spa_root_vdev; | |
687 | uint64_t vdev_state; | |
688 | ASSERTV(uint64_t *newguid = arg2); | |
689 | ||
690 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
691 | vdev_state = rvd->vdev_state; | |
692 | spa_config_exit(spa, SCL_STATE, FTAG); | |
693 | ||
694 | if (vdev_state != VDEV_STATE_HEALTHY) | |
695 | return (ENXIO); | |
696 | ||
697 | ASSERT3U(spa_guid(spa), !=, *newguid); | |
698 | ||
699 | return (0); | |
700 | } | |
701 | ||
702 | static void | |
703 | spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx) | |
704 | { | |
705 | spa_t *spa = arg1; | |
706 | uint64_t *newguid = arg2; | |
707 | uint64_t oldguid; | |
708 | vdev_t *rvd = spa->spa_root_vdev; | |
709 | ||
710 | oldguid = spa_guid(spa); | |
711 | ||
712 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
713 | rvd->vdev_guid = *newguid; | |
714 | rvd->vdev_guid_sum += (*newguid - oldguid); | |
715 | vdev_config_dirty(rvd); | |
716 | spa_config_exit(spa, SCL_STATE, FTAG); | |
717 | ||
718 | spa_history_log_internal(LOG_POOL_GUID_CHANGE, spa, tx, | |
719 | "old=%lld new=%lld", oldguid, *newguid); | |
720 | } | |
721 | ||
3541dc6d GA |
722 | /* |
723 | * Change the GUID for the pool. This is done so that we can later | |
724 | * re-import a pool built from a clone of our own vdevs. We will modify | |
725 | * the root vdev's guid, our own pool guid, and then mark all of our | |
726 | * vdevs dirty. Note that we must make sure that all our vdevs are | |
727 | * online when we do this, or else any vdevs that weren't present | |
728 | * would be orphaned from our pool. We are also going to issue a | |
729 | * sysevent to update any watchers. | |
730 | */ | |
731 | int | |
732 | spa_change_guid(spa_t *spa) | |
733 | { | |
3bc7e0fb GW |
734 | int error; |
735 | uint64_t guid; | |
3541dc6d | 736 | |
3bc7e0fb GW |
737 | mutex_enter(&spa_namespace_lock); |
738 | guid = spa_generate_guid(NULL); | |
3541dc6d | 739 | |
3bc7e0fb GW |
740 | error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check, |
741 | spa_change_guid_sync, spa, &guid, 5); | |
3541dc6d | 742 | |
3bc7e0fb GW |
743 | if (error == 0) { |
744 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
745 | spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID); | |
746 | } | |
3541dc6d | 747 | |
3bc7e0fb | 748 | mutex_exit(&spa_namespace_lock); |
3541dc6d | 749 | |
3bc7e0fb | 750 | return (error); |
3541dc6d GA |
751 | } |
752 | ||
34dc7c2f BB |
753 | /* |
754 | * ========================================================================== | |
755 | * SPA state manipulation (open/create/destroy/import/export) | |
756 | * ========================================================================== | |
757 | */ | |
758 | ||
759 | static int | |
760 | spa_error_entry_compare(const void *a, const void *b) | |
761 | { | |
762 | spa_error_entry_t *sa = (spa_error_entry_t *)a; | |
763 | spa_error_entry_t *sb = (spa_error_entry_t *)b; | |
764 | int ret; | |
765 | ||
766 | ret = bcmp(&sa->se_bookmark, &sb->se_bookmark, | |
767 | sizeof (zbookmark_t)); | |
768 | ||
769 | if (ret < 0) | |
770 | return (-1); | |
771 | else if (ret > 0) | |
772 | return (1); | |
773 | else | |
774 | return (0); | |
775 | } | |
776 | ||
777 | /* | |
778 | * Utility function which retrieves copies of the current logs and | |
779 | * re-initializes them in the process. | |
780 | */ | |
781 | void | |
782 | spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub) | |
783 | { | |
784 | ASSERT(MUTEX_HELD(&spa->spa_errlist_lock)); | |
785 | ||
786 | bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t)); | |
787 | bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t)); | |
788 | ||
789 | avl_create(&spa->spa_errlist_scrub, | |
790 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
791 | offsetof(spa_error_entry_t, se_avl)); | |
792 | avl_create(&spa->spa_errlist_last, | |
793 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
794 | offsetof(spa_error_entry_t, se_avl)); | |
795 | } | |
796 | ||
428870ff BB |
797 | static taskq_t * |
798 | spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode, | |
44f21da4 | 799 | uint_t value) |
34dc7c2f | 800 | { |
44f21da4 | 801 | uint_t flags = TASKQ_PREPOPULATE; |
428870ff | 802 | boolean_t batch = B_FALSE; |
34dc7c2f | 803 | |
428870ff BB |
804 | switch (mode) { |
805 | case zti_mode_null: | |
806 | return (NULL); /* no taskq needed */ | |
807 | ||
808 | case zti_mode_fixed: | |
809 | ASSERT3U(value, >=, 1); | |
810 | value = MAX(value, 1); | |
811 | break; | |
812 | ||
813 | case zti_mode_batch: | |
814 | batch = B_TRUE; | |
815 | flags |= TASKQ_THREADS_CPU_PCT; | |
816 | value = zio_taskq_batch_pct; | |
817 | break; | |
818 | ||
819 | case zti_mode_online_percent: | |
820 | flags |= TASKQ_THREADS_CPU_PCT; | |
821 | break; | |
822 | ||
823 | default: | |
824 | panic("unrecognized mode for %s taskq (%u:%u) in " | |
825 | "spa_activate()", | |
826 | name, mode, value); | |
827 | break; | |
828 | } | |
34dc7c2f | 829 | |
428870ff BB |
830 | if (zio_taskq_sysdc && spa->spa_proc != &p0) { |
831 | if (batch) | |
832 | flags |= TASKQ_DC_BATCH; | |
34dc7c2f | 833 | |
428870ff BB |
834 | return (taskq_create_sysdc(name, value, 50, INT_MAX, |
835 | spa->spa_proc, zio_taskq_basedc, flags)); | |
836 | } | |
837 | return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX, | |
838 | spa->spa_proc, flags)); | |
839 | } | |
840 | ||
841 | static void | |
842 | spa_create_zio_taskqs(spa_t *spa) | |
843 | { | |
d6320ddb BB |
844 | int t, q; |
845 | ||
846 | for (t = 0; t < ZIO_TYPES; t++) { | |
847 | for (q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
428870ff BB |
848 | const zio_taskq_info_t *ztip = &zio_taskqs[t][q]; |
849 | enum zti_modes mode = ztip->zti_mode; | |
850 | uint_t value = ztip->zti_value; | |
9babb374 BB |
851 | char name[32]; |
852 | ||
853 | (void) snprintf(name, sizeof (name), | |
428870ff | 854 | "%s_%s", zio_type_name[t], zio_taskq_types[q]); |
9babb374 | 855 | |
428870ff | 856 | spa->spa_zio_taskq[t][q] = |
44f21da4 | 857 | spa_taskq_create(spa, name, mode, value); |
428870ff BB |
858 | } |
859 | } | |
860 | } | |
9babb374 | 861 | |
7b89a549 | 862 | #if defined(_KERNEL) && defined(HAVE_SPA_THREAD) |
428870ff BB |
863 | static void |
864 | spa_thread(void *arg) | |
865 | { | |
866 | callb_cpr_t cprinfo; | |
9babb374 | 867 | |
428870ff BB |
868 | spa_t *spa = arg; |
869 | user_t *pu = PTOU(curproc); | |
9babb374 | 870 | |
428870ff BB |
871 | CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr, |
872 | spa->spa_name); | |
9babb374 | 873 | |
428870ff BB |
874 | ASSERT(curproc != &p0); |
875 | (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), | |
876 | "zpool-%s", spa->spa_name); | |
877 | (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm)); | |
878 | ||
879 | /* bind this thread to the requested psrset */ | |
880 | if (zio_taskq_psrset_bind != PS_NONE) { | |
881 | pool_lock(); | |
882 | mutex_enter(&cpu_lock); | |
883 | mutex_enter(&pidlock); | |
884 | mutex_enter(&curproc->p_lock); | |
885 | ||
886 | if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind, | |
887 | 0, NULL, NULL) == 0) { | |
888 | curthread->t_bind_pset = zio_taskq_psrset_bind; | |
889 | } else { | |
890 | cmn_err(CE_WARN, | |
891 | "Couldn't bind process for zfs pool \"%s\" to " | |
892 | "pset %d\n", spa->spa_name, zio_taskq_psrset_bind); | |
893 | } | |
894 | ||
895 | mutex_exit(&curproc->p_lock); | |
896 | mutex_exit(&pidlock); | |
897 | mutex_exit(&cpu_lock); | |
898 | pool_unlock(); | |
899 | } | |
900 | ||
901 | if (zio_taskq_sysdc) { | |
902 | sysdc_thread_enter(curthread, 100, 0); | |
903 | } | |
904 | ||
905 | spa->spa_proc = curproc; | |
906 | spa->spa_did = curthread->t_did; | |
907 | ||
908 | spa_create_zio_taskqs(spa); | |
909 | ||
910 | mutex_enter(&spa->spa_proc_lock); | |
911 | ASSERT(spa->spa_proc_state == SPA_PROC_CREATED); | |
912 | ||
913 | spa->spa_proc_state = SPA_PROC_ACTIVE; | |
914 | cv_broadcast(&spa->spa_proc_cv); | |
915 | ||
916 | CALLB_CPR_SAFE_BEGIN(&cprinfo); | |
917 | while (spa->spa_proc_state == SPA_PROC_ACTIVE) | |
918 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
919 | CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock); | |
920 | ||
921 | ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE); | |
922 | spa->spa_proc_state = SPA_PROC_GONE; | |
923 | spa->spa_proc = &p0; | |
924 | cv_broadcast(&spa->spa_proc_cv); | |
925 | CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */ | |
926 | ||
927 | mutex_enter(&curproc->p_lock); | |
928 | lwp_exit(); | |
929 | } | |
930 | #endif | |
931 | ||
932 | /* | |
933 | * Activate an uninitialized pool. | |
934 | */ | |
935 | static void | |
936 | spa_activate(spa_t *spa, int mode) | |
937 | { | |
938 | ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED); | |
939 | ||
940 | spa->spa_state = POOL_STATE_ACTIVE; | |
941 | spa->spa_mode = mode; | |
942 | ||
943 | spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
944 | spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops); | |
945 | ||
946 | /* Try to create a covering process */ | |
947 | mutex_enter(&spa->spa_proc_lock); | |
948 | ASSERT(spa->spa_proc_state == SPA_PROC_NONE); | |
949 | ASSERT(spa->spa_proc == &p0); | |
950 | spa->spa_did = 0; | |
951 | ||
7b89a549 | 952 | #ifdef HAVE_SPA_THREAD |
428870ff BB |
953 | /* Only create a process if we're going to be around a while. */ |
954 | if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) { | |
955 | if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri, | |
956 | NULL, 0) == 0) { | |
957 | spa->spa_proc_state = SPA_PROC_CREATED; | |
958 | while (spa->spa_proc_state == SPA_PROC_CREATED) { | |
959 | cv_wait(&spa->spa_proc_cv, | |
960 | &spa->spa_proc_lock); | |
9babb374 | 961 | } |
428870ff BB |
962 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); |
963 | ASSERT(spa->spa_proc != &p0); | |
964 | ASSERT(spa->spa_did != 0); | |
965 | } else { | |
966 | #ifdef _KERNEL | |
967 | cmn_err(CE_WARN, | |
968 | "Couldn't create process for zfs pool \"%s\"\n", | |
969 | spa->spa_name); | |
970 | #endif | |
b128c09f | 971 | } |
34dc7c2f | 972 | } |
7b89a549 | 973 | #endif /* HAVE_SPA_THREAD */ |
428870ff BB |
974 | mutex_exit(&spa->spa_proc_lock); |
975 | ||
976 | /* If we didn't create a process, we need to create our taskqs. */ | |
977 | if (spa->spa_proc == &p0) { | |
978 | spa_create_zio_taskqs(spa); | |
979 | } | |
34dc7c2f | 980 | |
b128c09f BB |
981 | list_create(&spa->spa_config_dirty_list, sizeof (vdev_t), |
982 | offsetof(vdev_t, vdev_config_dirty_node)); | |
983 | list_create(&spa->spa_state_dirty_list, sizeof (vdev_t), | |
984 | offsetof(vdev_t, vdev_state_dirty_node)); | |
34dc7c2f BB |
985 | |
986 | txg_list_create(&spa->spa_vdev_txg_list, | |
987 | offsetof(struct vdev, vdev_txg_node)); | |
988 | ||
989 | avl_create(&spa->spa_errlist_scrub, | |
990 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
991 | offsetof(spa_error_entry_t, se_avl)); | |
992 | avl_create(&spa->spa_errlist_last, | |
993 | spa_error_entry_compare, sizeof (spa_error_entry_t), | |
994 | offsetof(spa_error_entry_t, se_avl)); | |
995 | } | |
996 | ||
997 | /* | |
998 | * Opposite of spa_activate(). | |
999 | */ | |
1000 | static void | |
1001 | spa_deactivate(spa_t *spa) | |
1002 | { | |
d6320ddb BB |
1003 | int t, q; |
1004 | ||
34dc7c2f BB |
1005 | ASSERT(spa->spa_sync_on == B_FALSE); |
1006 | ASSERT(spa->spa_dsl_pool == NULL); | |
1007 | ASSERT(spa->spa_root_vdev == NULL); | |
9babb374 | 1008 | ASSERT(spa->spa_async_zio_root == NULL); |
34dc7c2f BB |
1009 | ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED); |
1010 | ||
1011 | txg_list_destroy(&spa->spa_vdev_txg_list); | |
1012 | ||
b128c09f BB |
1013 | list_destroy(&spa->spa_config_dirty_list); |
1014 | list_destroy(&spa->spa_state_dirty_list); | |
34dc7c2f | 1015 | |
cc92e9d0 GW |
1016 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); |
1017 | ||
d6320ddb BB |
1018 | for (t = 0; t < ZIO_TYPES; t++) { |
1019 | for (q = 0; q < ZIO_TASKQ_TYPES; q++) { | |
428870ff BB |
1020 | if (spa->spa_zio_taskq[t][q] != NULL) |
1021 | taskq_destroy(spa->spa_zio_taskq[t][q]); | |
b128c09f BB |
1022 | spa->spa_zio_taskq[t][q] = NULL; |
1023 | } | |
34dc7c2f BB |
1024 | } |
1025 | ||
1026 | metaslab_class_destroy(spa->spa_normal_class); | |
1027 | spa->spa_normal_class = NULL; | |
1028 | ||
1029 | metaslab_class_destroy(spa->spa_log_class); | |
1030 | spa->spa_log_class = NULL; | |
1031 | ||
1032 | /* | |
1033 | * If this was part of an import or the open otherwise failed, we may | |
1034 | * still have errors left in the queues. Empty them just in case. | |
1035 | */ | |
1036 | spa_errlog_drain(spa); | |
1037 | ||
1038 | avl_destroy(&spa->spa_errlist_scrub); | |
1039 | avl_destroy(&spa->spa_errlist_last); | |
1040 | ||
1041 | spa->spa_state = POOL_STATE_UNINITIALIZED; | |
428870ff BB |
1042 | |
1043 | mutex_enter(&spa->spa_proc_lock); | |
1044 | if (spa->spa_proc_state != SPA_PROC_NONE) { | |
1045 | ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE); | |
1046 | spa->spa_proc_state = SPA_PROC_DEACTIVATE; | |
1047 | cv_broadcast(&spa->spa_proc_cv); | |
1048 | while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) { | |
1049 | ASSERT(spa->spa_proc != &p0); | |
1050 | cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock); | |
1051 | } | |
1052 | ASSERT(spa->spa_proc_state == SPA_PROC_GONE); | |
1053 | spa->spa_proc_state = SPA_PROC_NONE; | |
1054 | } | |
1055 | ASSERT(spa->spa_proc == &p0); | |
1056 | mutex_exit(&spa->spa_proc_lock); | |
1057 | ||
1058 | /* | |
1059 | * We want to make sure spa_thread() has actually exited the ZFS | |
1060 | * module, so that the module can't be unloaded out from underneath | |
1061 | * it. | |
1062 | */ | |
1063 | if (spa->spa_did != 0) { | |
1064 | thread_join(spa->spa_did); | |
1065 | spa->spa_did = 0; | |
1066 | } | |
34dc7c2f BB |
1067 | } |
1068 | ||
1069 | /* | |
1070 | * Verify a pool configuration, and construct the vdev tree appropriately. This | |
1071 | * will create all the necessary vdevs in the appropriate layout, with each vdev | |
1072 | * in the CLOSED state. This will prep the pool before open/creation/import. | |
1073 | * All vdev validation is done by the vdev_alloc() routine. | |
1074 | */ | |
1075 | static int | |
1076 | spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, | |
1077 | uint_t id, int atype) | |
1078 | { | |
1079 | nvlist_t **child; | |
9babb374 | 1080 | uint_t children; |
34dc7c2f | 1081 | int error; |
d6320ddb | 1082 | int c; |
34dc7c2f BB |
1083 | |
1084 | if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0) | |
1085 | return (error); | |
1086 | ||
1087 | if ((*vdp)->vdev_ops->vdev_op_leaf) | |
1088 | return (0); | |
1089 | ||
b128c09f BB |
1090 | error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, |
1091 | &child, &children); | |
1092 | ||
1093 | if (error == ENOENT) | |
1094 | return (0); | |
1095 | ||
1096 | if (error) { | |
34dc7c2f BB |
1097 | vdev_free(*vdp); |
1098 | *vdp = NULL; | |
1099 | return (EINVAL); | |
1100 | } | |
1101 | ||
d6320ddb | 1102 | for (c = 0; c < children; c++) { |
34dc7c2f BB |
1103 | vdev_t *vd; |
1104 | if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c, | |
1105 | atype)) != 0) { | |
1106 | vdev_free(*vdp); | |
1107 | *vdp = NULL; | |
1108 | return (error); | |
1109 | } | |
1110 | } | |
1111 | ||
1112 | ASSERT(*vdp != NULL); | |
1113 | ||
1114 | return (0); | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * Opposite of spa_load(). | |
1119 | */ | |
1120 | static void | |
1121 | spa_unload(spa_t *spa) | |
1122 | { | |
1123 | int i; | |
1124 | ||
b128c09f BB |
1125 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); |
1126 | ||
34dc7c2f BB |
1127 | /* |
1128 | * Stop async tasks. | |
1129 | */ | |
1130 | spa_async_suspend(spa); | |
1131 | ||
1132 | /* | |
1133 | * Stop syncing. | |
1134 | */ | |
1135 | if (spa->spa_sync_on) { | |
1136 | txg_sync_stop(spa->spa_dsl_pool); | |
1137 | spa->spa_sync_on = B_FALSE; | |
1138 | } | |
1139 | ||
1140 | /* | |
b128c09f | 1141 | * Wait for any outstanding async I/O to complete. |
34dc7c2f | 1142 | */ |
9babb374 BB |
1143 | if (spa->spa_async_zio_root != NULL) { |
1144 | (void) zio_wait(spa->spa_async_zio_root); | |
1145 | spa->spa_async_zio_root = NULL; | |
1146 | } | |
34dc7c2f | 1147 | |
428870ff BB |
1148 | bpobj_close(&spa->spa_deferred_bpobj); |
1149 | ||
34dc7c2f BB |
1150 | /* |
1151 | * Close the dsl pool. | |
1152 | */ | |
1153 | if (spa->spa_dsl_pool) { | |
1154 | dsl_pool_close(spa->spa_dsl_pool); | |
1155 | spa->spa_dsl_pool = NULL; | |
428870ff | 1156 | spa->spa_meta_objset = NULL; |
34dc7c2f BB |
1157 | } |
1158 | ||
428870ff BB |
1159 | ddt_unload(spa); |
1160 | ||
fb5f0bc8 BB |
1161 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
1162 | ||
1163 | /* | |
1164 | * Drop and purge level 2 cache | |
1165 | */ | |
1166 | spa_l2cache_drop(spa); | |
1167 | ||
34dc7c2f BB |
1168 | /* |
1169 | * Close all vdevs. | |
1170 | */ | |
1171 | if (spa->spa_root_vdev) | |
1172 | vdev_free(spa->spa_root_vdev); | |
1173 | ASSERT(spa->spa_root_vdev == NULL); | |
1174 | ||
1175 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1176 | vdev_free(spa->spa_spares.sav_vdevs[i]); | |
1177 | if (spa->spa_spares.sav_vdevs) { | |
1178 | kmem_free(spa->spa_spares.sav_vdevs, | |
1179 | spa->spa_spares.sav_count * sizeof (void *)); | |
1180 | spa->spa_spares.sav_vdevs = NULL; | |
1181 | } | |
1182 | if (spa->spa_spares.sav_config) { | |
1183 | nvlist_free(spa->spa_spares.sav_config); | |
1184 | spa->spa_spares.sav_config = NULL; | |
1185 | } | |
b128c09f | 1186 | spa->spa_spares.sav_count = 0; |
34dc7c2f | 1187 | |
5ffb9d1d GW |
1188 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
1189 | vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]); | |
34dc7c2f | 1190 | vdev_free(spa->spa_l2cache.sav_vdevs[i]); |
5ffb9d1d | 1191 | } |
34dc7c2f BB |
1192 | if (spa->spa_l2cache.sav_vdevs) { |
1193 | kmem_free(spa->spa_l2cache.sav_vdevs, | |
1194 | spa->spa_l2cache.sav_count * sizeof (void *)); | |
1195 | spa->spa_l2cache.sav_vdevs = NULL; | |
1196 | } | |
1197 | if (spa->spa_l2cache.sav_config) { | |
1198 | nvlist_free(spa->spa_l2cache.sav_config); | |
1199 | spa->spa_l2cache.sav_config = NULL; | |
1200 | } | |
b128c09f | 1201 | spa->spa_l2cache.sav_count = 0; |
34dc7c2f BB |
1202 | |
1203 | spa->spa_async_suspended = 0; | |
fb5f0bc8 | 1204 | |
d96eb2b1 DM |
1205 | if (spa->spa_comment != NULL) { |
1206 | spa_strfree(spa->spa_comment); | |
1207 | spa->spa_comment = NULL; | |
1208 | } | |
1209 | ||
fb5f0bc8 | 1210 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
1211 | } |
1212 | ||
1213 | /* | |
1214 | * Load (or re-load) the current list of vdevs describing the active spares for | |
1215 | * this pool. When this is called, we have some form of basic information in | |
1216 | * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and | |
1217 | * then re-generate a more complete list including status information. | |
1218 | */ | |
1219 | static void | |
1220 | spa_load_spares(spa_t *spa) | |
1221 | { | |
1222 | nvlist_t **spares; | |
1223 | uint_t nspares; | |
1224 | int i; | |
1225 | vdev_t *vd, *tvd; | |
1226 | ||
b128c09f BB |
1227 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1228 | ||
34dc7c2f BB |
1229 | /* |
1230 | * First, close and free any existing spare vdevs. | |
1231 | */ | |
1232 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
1233 | vd = spa->spa_spares.sav_vdevs[i]; | |
1234 | ||
1235 | /* Undo the call to spa_activate() below */ | |
b128c09f BB |
1236 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1237 | B_FALSE)) != NULL && tvd->vdev_isspare) | |
34dc7c2f BB |
1238 | spa_spare_remove(tvd); |
1239 | vdev_close(vd); | |
1240 | vdev_free(vd); | |
1241 | } | |
1242 | ||
1243 | if (spa->spa_spares.sav_vdevs) | |
1244 | kmem_free(spa->spa_spares.sav_vdevs, | |
1245 | spa->spa_spares.sav_count * sizeof (void *)); | |
1246 | ||
1247 | if (spa->spa_spares.sav_config == NULL) | |
1248 | nspares = 0; | |
1249 | else | |
1250 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
1251 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
1252 | ||
1253 | spa->spa_spares.sav_count = (int)nspares; | |
1254 | spa->spa_spares.sav_vdevs = NULL; | |
1255 | ||
1256 | if (nspares == 0) | |
1257 | return; | |
1258 | ||
1259 | /* | |
1260 | * Construct the array of vdevs, opening them to get status in the | |
1261 | * process. For each spare, there is potentially two different vdev_t | |
1262 | * structures associated with it: one in the list of spares (used only | |
1263 | * for basic validation purposes) and one in the active vdev | |
1264 | * configuration (if it's spared in). During this phase we open and | |
1265 | * validate each vdev on the spare list. If the vdev also exists in the | |
1266 | * active configuration, then we also mark this vdev as an active spare. | |
1267 | */ | |
1268 | spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *), | |
b8d06fca | 1269 | KM_PUSHPAGE); |
34dc7c2f BB |
1270 | for (i = 0; i < spa->spa_spares.sav_count; i++) { |
1271 | VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0, | |
1272 | VDEV_ALLOC_SPARE) == 0); | |
1273 | ASSERT(vd != NULL); | |
1274 | ||
1275 | spa->spa_spares.sav_vdevs[i] = vd; | |
1276 | ||
b128c09f BB |
1277 | if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid, |
1278 | B_FALSE)) != NULL) { | |
34dc7c2f BB |
1279 | if (!tvd->vdev_isspare) |
1280 | spa_spare_add(tvd); | |
1281 | ||
1282 | /* | |
1283 | * We only mark the spare active if we were successfully | |
1284 | * able to load the vdev. Otherwise, importing a pool | |
1285 | * with a bad active spare would result in strange | |
1286 | * behavior, because multiple pool would think the spare | |
1287 | * is actively in use. | |
1288 | * | |
1289 | * There is a vulnerability here to an equally bizarre | |
1290 | * circumstance, where a dead active spare is later | |
1291 | * brought back to life (onlined or otherwise). Given | |
1292 | * the rarity of this scenario, and the extra complexity | |
1293 | * it adds, we ignore the possibility. | |
1294 | */ | |
1295 | if (!vdev_is_dead(tvd)) | |
1296 | spa_spare_activate(tvd); | |
1297 | } | |
1298 | ||
b128c09f | 1299 | vd->vdev_top = vd; |
9babb374 | 1300 | vd->vdev_aux = &spa->spa_spares; |
b128c09f | 1301 | |
34dc7c2f BB |
1302 | if (vdev_open(vd) != 0) |
1303 | continue; | |
1304 | ||
34dc7c2f BB |
1305 | if (vdev_validate_aux(vd) == 0) |
1306 | spa_spare_add(vd); | |
1307 | } | |
1308 | ||
1309 | /* | |
1310 | * Recompute the stashed list of spares, with status information | |
1311 | * this time. | |
1312 | */ | |
1313 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES, | |
1314 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1315 | ||
1316 | spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *), | |
b8d06fca | 1317 | KM_PUSHPAGE); |
34dc7c2f BB |
1318 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
1319 | spares[i] = vdev_config_generate(spa, | |
428870ff | 1320 | spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE); |
34dc7c2f BB |
1321 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
1322 | ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0); | |
1323 | for (i = 0; i < spa->spa_spares.sav_count; i++) | |
1324 | nvlist_free(spares[i]); | |
1325 | kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *)); | |
1326 | } | |
1327 | ||
1328 | /* | |
1329 | * Load (or re-load) the current list of vdevs describing the active l2cache for | |
1330 | * this pool. When this is called, we have some form of basic information in | |
1331 | * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and | |
1332 | * then re-generate a more complete list including status information. | |
1333 | * Devices which are already active have their details maintained, and are | |
1334 | * not re-opened. | |
1335 | */ | |
1336 | static void | |
1337 | spa_load_l2cache(spa_t *spa) | |
1338 | { | |
1339 | nvlist_t **l2cache; | |
1340 | uint_t nl2cache; | |
1341 | int i, j, oldnvdevs; | |
9babb374 | 1342 | uint64_t guid; |
d4ed6673 | 1343 | vdev_t *vd, **oldvdevs, **newvdevs = NULL; |
34dc7c2f BB |
1344 | spa_aux_vdev_t *sav = &spa->spa_l2cache; |
1345 | ||
b128c09f BB |
1346 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
1347 | ||
34dc7c2f BB |
1348 | if (sav->sav_config != NULL) { |
1349 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, | |
1350 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
b8d06fca | 1351 | newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_PUSHPAGE); |
34dc7c2f BB |
1352 | } else { |
1353 | nl2cache = 0; | |
1354 | } | |
1355 | ||
1356 | oldvdevs = sav->sav_vdevs; | |
1357 | oldnvdevs = sav->sav_count; | |
1358 | sav->sav_vdevs = NULL; | |
1359 | sav->sav_count = 0; | |
1360 | ||
1361 | /* | |
1362 | * Process new nvlist of vdevs. | |
1363 | */ | |
1364 | for (i = 0; i < nl2cache; i++) { | |
1365 | VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID, | |
1366 | &guid) == 0); | |
1367 | ||
1368 | newvdevs[i] = NULL; | |
1369 | for (j = 0; j < oldnvdevs; j++) { | |
1370 | vd = oldvdevs[j]; | |
1371 | if (vd != NULL && guid == vd->vdev_guid) { | |
1372 | /* | |
1373 | * Retain previous vdev for add/remove ops. | |
1374 | */ | |
1375 | newvdevs[i] = vd; | |
1376 | oldvdevs[j] = NULL; | |
1377 | break; | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | if (newvdevs[i] == NULL) { | |
1382 | /* | |
1383 | * Create new vdev | |
1384 | */ | |
1385 | VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0, | |
1386 | VDEV_ALLOC_L2CACHE) == 0); | |
1387 | ASSERT(vd != NULL); | |
1388 | newvdevs[i] = vd; | |
1389 | ||
1390 | /* | |
1391 | * Commit this vdev as an l2cache device, | |
1392 | * even if it fails to open. | |
1393 | */ | |
1394 | spa_l2cache_add(vd); | |
1395 | ||
b128c09f BB |
1396 | vd->vdev_top = vd; |
1397 | vd->vdev_aux = sav; | |
1398 | ||
1399 | spa_l2cache_activate(vd); | |
1400 | ||
34dc7c2f BB |
1401 | if (vdev_open(vd) != 0) |
1402 | continue; | |
1403 | ||
34dc7c2f BB |
1404 | (void) vdev_validate_aux(vd); |
1405 | ||
9babb374 BB |
1406 | if (!vdev_is_dead(vd)) |
1407 | l2arc_add_vdev(spa, vd); | |
34dc7c2f BB |
1408 | } |
1409 | } | |
1410 | ||
1411 | /* | |
1412 | * Purge vdevs that were dropped | |
1413 | */ | |
1414 | for (i = 0; i < oldnvdevs; i++) { | |
1415 | uint64_t pool; | |
1416 | ||
1417 | vd = oldvdevs[i]; | |
1418 | if (vd != NULL) { | |
5ffb9d1d GW |
1419 | ASSERT(vd->vdev_isl2cache); |
1420 | ||
fb5f0bc8 BB |
1421 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
1422 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 1423 | l2arc_remove_vdev(vd); |
5ffb9d1d GW |
1424 | vdev_clear_stats(vd); |
1425 | vdev_free(vd); | |
34dc7c2f BB |
1426 | } |
1427 | } | |
1428 | ||
1429 | if (oldvdevs) | |
1430 | kmem_free(oldvdevs, oldnvdevs * sizeof (void *)); | |
1431 | ||
1432 | if (sav->sav_config == NULL) | |
1433 | goto out; | |
1434 | ||
1435 | sav->sav_vdevs = newvdevs; | |
1436 | sav->sav_count = (int)nl2cache; | |
1437 | ||
1438 | /* | |
1439 | * Recompute the stashed list of l2cache devices, with status | |
1440 | * information this time. | |
1441 | */ | |
1442 | VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE, | |
1443 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
1444 | ||
b8d06fca | 1445 | l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE); |
34dc7c2f BB |
1446 | for (i = 0; i < sav->sav_count; i++) |
1447 | l2cache[i] = vdev_config_generate(spa, | |
428870ff | 1448 | sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
1449 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, |
1450 | ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0); | |
1451 | out: | |
1452 | for (i = 0; i < sav->sav_count; i++) | |
1453 | nvlist_free(l2cache[i]); | |
1454 | if (sav->sav_count) | |
1455 | kmem_free(l2cache, sav->sav_count * sizeof (void *)); | |
1456 | } | |
1457 | ||
1458 | static int | |
1459 | load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value) | |
1460 | { | |
1461 | dmu_buf_t *db; | |
1462 | char *packed = NULL; | |
1463 | size_t nvsize = 0; | |
1464 | int error; | |
1465 | *value = NULL; | |
1466 | ||
c3275b56 BB |
1467 | error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db); |
1468 | if (error) | |
1469 | return (error); | |
1470 | ||
34dc7c2f BB |
1471 | nvsize = *(uint64_t *)db->db_data; |
1472 | dmu_buf_rele(db, FTAG); | |
1473 | ||
b8d06fca | 1474 | packed = kmem_alloc(nvsize, KM_PUSHPAGE | KM_NODEBUG); |
9babb374 BB |
1475 | error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed, |
1476 | DMU_READ_PREFETCH); | |
34dc7c2f BB |
1477 | if (error == 0) |
1478 | error = nvlist_unpack(packed, nvsize, value, 0); | |
1479 | kmem_free(packed, nvsize); | |
1480 | ||
1481 | return (error); | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * Checks to see if the given vdev could not be opened, in which case we post a | |
1486 | * sysevent to notify the autoreplace code that the device has been removed. | |
1487 | */ | |
1488 | static void | |
1489 | spa_check_removed(vdev_t *vd) | |
1490 | { | |
d6320ddb BB |
1491 | int c; |
1492 | ||
1493 | for (c = 0; c < vd->vdev_children; c++) | |
34dc7c2f BB |
1494 | spa_check_removed(vd->vdev_child[c]); |
1495 | ||
1496 | if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) { | |
26685276 BB |
1497 | zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE, |
1498 | vd->vdev_spa, vd, NULL, 0, 0); | |
1499 | spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK); | |
34dc7c2f BB |
1500 | } |
1501 | } | |
1502 | ||
9babb374 | 1503 | /* |
572e2857 | 1504 | * Validate the current config against the MOS config |
9babb374 | 1505 | */ |
572e2857 BB |
1506 | static boolean_t |
1507 | spa_config_valid(spa_t *spa, nvlist_t *config) | |
9babb374 | 1508 | { |
572e2857 BB |
1509 | vdev_t *mrvd, *rvd = spa->spa_root_vdev; |
1510 | nvlist_t *nv; | |
d6320ddb | 1511 | int c, i; |
572e2857 BB |
1512 | |
1513 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0); | |
1514 | ||
1515 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
1516 | VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0); | |
1517 | ||
1518 | ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children); | |
9babb374 | 1519 | |
428870ff | 1520 | /* |
572e2857 BB |
1521 | * If we're doing a normal import, then build up any additional |
1522 | * diagnostic information about missing devices in this config. | |
1523 | * We'll pass this up to the user for further processing. | |
428870ff | 1524 | */ |
572e2857 BB |
1525 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) { |
1526 | nvlist_t **child, *nv; | |
1527 | uint64_t idx = 0; | |
1528 | ||
1529 | child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **), | |
b8d06fca RY |
1530 | KM_PUSHPAGE); |
1531 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); | |
572e2857 | 1532 | |
d6320ddb | 1533 | for (c = 0; c < rvd->vdev_children; c++) { |
572e2857 BB |
1534 | vdev_t *tvd = rvd->vdev_child[c]; |
1535 | vdev_t *mtvd = mrvd->vdev_child[c]; | |
1536 | ||
1537 | if (tvd->vdev_ops == &vdev_missing_ops && | |
1538 | mtvd->vdev_ops != &vdev_missing_ops && | |
1539 | mtvd->vdev_islog) | |
1540 | child[idx++] = vdev_config_generate(spa, mtvd, | |
1541 | B_FALSE, 0); | |
1542 | } | |
9babb374 | 1543 | |
572e2857 BB |
1544 | if (idx) { |
1545 | VERIFY(nvlist_add_nvlist_array(nv, | |
1546 | ZPOOL_CONFIG_CHILDREN, child, idx) == 0); | |
1547 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
1548 | ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0); | |
1549 | ||
d6320ddb | 1550 | for (i = 0; i < idx; i++) |
572e2857 BB |
1551 | nvlist_free(child[i]); |
1552 | } | |
1553 | nvlist_free(nv); | |
1554 | kmem_free(child, rvd->vdev_children * sizeof (char **)); | |
1555 | } | |
1556 | ||
1557 | /* | |
1558 | * Compare the root vdev tree with the information we have | |
1559 | * from the MOS config (mrvd). Check each top-level vdev | |
1560 | * with the corresponding MOS config top-level (mtvd). | |
1561 | */ | |
d6320ddb | 1562 | for (c = 0; c < rvd->vdev_children; c++) { |
572e2857 BB |
1563 | vdev_t *tvd = rvd->vdev_child[c]; |
1564 | vdev_t *mtvd = mrvd->vdev_child[c]; | |
1565 | ||
1566 | /* | |
1567 | * Resolve any "missing" vdevs in the current configuration. | |
1568 | * If we find that the MOS config has more accurate information | |
1569 | * about the top-level vdev then use that vdev instead. | |
1570 | */ | |
1571 | if (tvd->vdev_ops == &vdev_missing_ops && | |
1572 | mtvd->vdev_ops != &vdev_missing_ops) { | |
1573 | ||
1574 | if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) | |
1575 | continue; | |
1576 | ||
1577 | /* | |
1578 | * Device specific actions. | |
1579 | */ | |
1580 | if (mtvd->vdev_islog) { | |
1581 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
1582 | } else { | |
1583 | /* | |
1584 | * XXX - once we have 'readonly' pool | |
1585 | * support we should be able to handle | |
1586 | * missing data devices by transitioning | |
1587 | * the pool to readonly. | |
1588 | */ | |
1589 | continue; | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Swap the missing vdev with the data we were | |
1594 | * able to obtain from the MOS config. | |
1595 | */ | |
1596 | vdev_remove_child(rvd, tvd); | |
1597 | vdev_remove_child(mrvd, mtvd); | |
1598 | ||
1599 | vdev_add_child(rvd, mtvd); | |
1600 | vdev_add_child(mrvd, tvd); | |
1601 | ||
1602 | spa_config_exit(spa, SCL_ALL, FTAG); | |
1603 | vdev_load(mtvd); | |
1604 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
1605 | ||
1606 | vdev_reopen(rvd); | |
1607 | } else if (mtvd->vdev_islog) { | |
1608 | /* | |
1609 | * Load the slog device's state from the MOS config | |
1610 | * since it's possible that the label does not | |
1611 | * contain the most up-to-date information. | |
1612 | */ | |
1613 | vdev_load_log_state(tvd, mtvd); | |
1614 | vdev_reopen(tvd); | |
1615 | } | |
9babb374 | 1616 | } |
572e2857 | 1617 | vdev_free(mrvd); |
428870ff | 1618 | spa_config_exit(spa, SCL_ALL, FTAG); |
572e2857 BB |
1619 | |
1620 | /* | |
1621 | * Ensure we were able to validate the config. | |
1622 | */ | |
1623 | return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum); | |
9babb374 BB |
1624 | } |
1625 | ||
b128c09f BB |
1626 | /* |
1627 | * Check for missing log devices | |
1628 | */ | |
572e2857 | 1629 | static int |
b128c09f BB |
1630 | spa_check_logs(spa_t *spa) |
1631 | { | |
1632 | switch (spa->spa_log_state) { | |
e75c13c3 BB |
1633 | default: |
1634 | break; | |
b128c09f BB |
1635 | case SPA_LOG_MISSING: |
1636 | /* need to recheck in case slog has been restored */ | |
1637 | case SPA_LOG_UNKNOWN: | |
1638 | if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL, | |
1639 | DS_FIND_CHILDREN)) { | |
428870ff | 1640 | spa_set_log_state(spa, SPA_LOG_MISSING); |
b128c09f BB |
1641 | return (1); |
1642 | } | |
1643 | break; | |
b128c09f | 1644 | } |
b128c09f BB |
1645 | return (0); |
1646 | } | |
1647 | ||
428870ff BB |
1648 | static boolean_t |
1649 | spa_passivate_log(spa_t *spa) | |
34dc7c2f | 1650 | { |
428870ff BB |
1651 | vdev_t *rvd = spa->spa_root_vdev; |
1652 | boolean_t slog_found = B_FALSE; | |
d6320ddb | 1653 | int c; |
b128c09f | 1654 | |
428870ff | 1655 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
fb5f0bc8 | 1656 | |
428870ff BB |
1657 | if (!spa_has_slogs(spa)) |
1658 | return (B_FALSE); | |
34dc7c2f | 1659 | |
d6320ddb | 1660 | for (c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
1661 | vdev_t *tvd = rvd->vdev_child[c]; |
1662 | metaslab_group_t *mg = tvd->vdev_mg; | |
34dc7c2f | 1663 | |
428870ff BB |
1664 | if (tvd->vdev_islog) { |
1665 | metaslab_group_passivate(mg); | |
1666 | slog_found = B_TRUE; | |
1667 | } | |
34dc7c2f BB |
1668 | } |
1669 | ||
428870ff BB |
1670 | return (slog_found); |
1671 | } | |
34dc7c2f | 1672 | |
428870ff BB |
1673 | static void |
1674 | spa_activate_log(spa_t *spa) | |
1675 | { | |
1676 | vdev_t *rvd = spa->spa_root_vdev; | |
d6320ddb | 1677 | int c; |
34dc7c2f | 1678 | |
428870ff BB |
1679 | ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER)); |
1680 | ||
d6320ddb | 1681 | for (c = 0; c < rvd->vdev_children; c++) { |
428870ff BB |
1682 | vdev_t *tvd = rvd->vdev_child[c]; |
1683 | metaslab_group_t *mg = tvd->vdev_mg; | |
1684 | ||
1685 | if (tvd->vdev_islog) | |
1686 | metaslab_group_activate(mg); | |
34dc7c2f | 1687 | } |
428870ff | 1688 | } |
34dc7c2f | 1689 | |
428870ff BB |
1690 | int |
1691 | spa_offline_log(spa_t *spa) | |
1692 | { | |
1693 | int error = 0; | |
34dc7c2f | 1694 | |
428870ff BB |
1695 | if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline, |
1696 | NULL, DS_FIND_CHILDREN)) == 0) { | |
9babb374 | 1697 | |
428870ff BB |
1698 | /* |
1699 | * We successfully offlined the log device, sync out the | |
1700 | * current txg so that the "stubby" block can be removed | |
1701 | * by zil_sync(). | |
1702 | */ | |
1703 | txg_wait_synced(spa->spa_dsl_pool, 0); | |
1704 | } | |
1705 | return (error); | |
1706 | } | |
34dc7c2f | 1707 | |
428870ff BB |
1708 | static void |
1709 | spa_aux_check_removed(spa_aux_vdev_t *sav) | |
1710 | { | |
d6320ddb BB |
1711 | int i; |
1712 | ||
1713 | for (i = 0; i < sav->sav_count; i++) | |
428870ff BB |
1714 | spa_check_removed(sav->sav_vdevs[i]); |
1715 | } | |
34dc7c2f | 1716 | |
428870ff BB |
1717 | void |
1718 | spa_claim_notify(zio_t *zio) | |
1719 | { | |
1720 | spa_t *spa = zio->io_spa; | |
34dc7c2f | 1721 | |
428870ff BB |
1722 | if (zio->io_error) |
1723 | return; | |
34dc7c2f | 1724 | |
428870ff BB |
1725 | mutex_enter(&spa->spa_props_lock); /* any mutex will do */ |
1726 | if (spa->spa_claim_max_txg < zio->io_bp->blk_birth) | |
1727 | spa->spa_claim_max_txg = zio->io_bp->blk_birth; | |
1728 | mutex_exit(&spa->spa_props_lock); | |
1729 | } | |
34dc7c2f | 1730 | |
428870ff BB |
1731 | typedef struct spa_load_error { |
1732 | uint64_t sle_meta_count; | |
1733 | uint64_t sle_data_count; | |
1734 | } spa_load_error_t; | |
34dc7c2f | 1735 | |
428870ff BB |
1736 | static void |
1737 | spa_load_verify_done(zio_t *zio) | |
1738 | { | |
1739 | blkptr_t *bp = zio->io_bp; | |
1740 | spa_load_error_t *sle = zio->io_private; | |
1741 | dmu_object_type_t type = BP_GET_TYPE(bp); | |
1742 | int error = zio->io_error; | |
34dc7c2f | 1743 | |
428870ff | 1744 | if (error) { |
9ae529ec | 1745 | if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) && |
428870ff BB |
1746 | type != DMU_OT_INTENT_LOG) |
1747 | atomic_add_64(&sle->sle_meta_count, 1); | |
1748 | else | |
1749 | atomic_add_64(&sle->sle_data_count, 1); | |
34dc7c2f | 1750 | } |
428870ff BB |
1751 | zio_data_buf_free(zio->io_data, zio->io_size); |
1752 | } | |
34dc7c2f | 1753 | |
428870ff BB |
1754 | /*ARGSUSED*/ |
1755 | static int | |
1756 | spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, | |
1757 | arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg) | |
1758 | { | |
1759 | if (bp != NULL) { | |
1760 | zio_t *rio = arg; | |
1761 | size_t size = BP_GET_PSIZE(bp); | |
1762 | void *data = zio_data_buf_alloc(size); | |
34dc7c2f | 1763 | |
428870ff BB |
1764 | zio_nowait(zio_read(rio, spa, bp, data, size, |
1765 | spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB, | |
1766 | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL | | |
1767 | ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb)); | |
34dc7c2f | 1768 | } |
428870ff BB |
1769 | return (0); |
1770 | } | |
34dc7c2f | 1771 | |
428870ff BB |
1772 | static int |
1773 | spa_load_verify(spa_t *spa) | |
1774 | { | |
1775 | zio_t *rio; | |
1776 | spa_load_error_t sle = { 0 }; | |
1777 | zpool_rewind_policy_t policy; | |
1778 | boolean_t verify_ok = B_FALSE; | |
1779 | int error; | |
34dc7c2f | 1780 | |
428870ff | 1781 | zpool_get_rewind_policy(spa->spa_config, &policy); |
34dc7c2f | 1782 | |
428870ff BB |
1783 | if (policy.zrp_request & ZPOOL_NEVER_REWIND) |
1784 | return (0); | |
34dc7c2f | 1785 | |
428870ff BB |
1786 | rio = zio_root(spa, NULL, &sle, |
1787 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE); | |
34dc7c2f | 1788 | |
428870ff BB |
1789 | error = traverse_pool(spa, spa->spa_verify_min_txg, |
1790 | TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio); | |
1791 | ||
1792 | (void) zio_wait(rio); | |
1793 | ||
1794 | spa->spa_load_meta_errors = sle.sle_meta_count; | |
1795 | spa->spa_load_data_errors = sle.sle_data_count; | |
1796 | ||
1797 | if (!error && sle.sle_meta_count <= policy.zrp_maxmeta && | |
1798 | sle.sle_data_count <= policy.zrp_maxdata) { | |
572e2857 BB |
1799 | int64_t loss = 0; |
1800 | ||
428870ff BB |
1801 | verify_ok = B_TRUE; |
1802 | spa->spa_load_txg = spa->spa_uberblock.ub_txg; | |
1803 | spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp; | |
572e2857 BB |
1804 | |
1805 | loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts; | |
1806 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
1807 | ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0); | |
1808 | VERIFY(nvlist_add_int64(spa->spa_load_info, | |
1809 | ZPOOL_CONFIG_REWIND_TIME, loss) == 0); | |
1810 | VERIFY(nvlist_add_uint64(spa->spa_load_info, | |
1811 | ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0); | |
428870ff BB |
1812 | } else { |
1813 | spa->spa_load_max_txg = spa->spa_uberblock.ub_txg; | |
1814 | } | |
1815 | ||
1816 | if (error) { | |
1817 | if (error != ENXIO && error != EIO) | |
1818 | error = EIO; | |
1819 | return (error); | |
1820 | } | |
1821 | ||
1822 | return (verify_ok ? 0 : EIO); | |
1823 | } | |
1824 | ||
1825 | /* | |
1826 | * Find a value in the pool props object. | |
1827 | */ | |
1828 | static void | |
1829 | spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val) | |
1830 | { | |
1831 | (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object, | |
1832 | zpool_prop_to_name(prop), sizeof (uint64_t), 1, val); | |
1833 | } | |
1834 | ||
1835 | /* | |
1836 | * Find a value in the pool directory object. | |
1837 | */ | |
1838 | static int | |
1839 | spa_dir_prop(spa_t *spa, const char *name, uint64_t *val) | |
1840 | { | |
1841 | return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT, | |
1842 | name, sizeof (uint64_t), 1, val)); | |
1843 | } | |
1844 | ||
1845 | static int | |
1846 | spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err) | |
1847 | { | |
1848 | vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux); | |
1849 | return (err); | |
1850 | } | |
1851 | ||
1852 | /* | |
1853 | * Fix up config after a partly-completed split. This is done with the | |
1854 | * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off | |
1855 | * pool have that entry in their config, but only the splitting one contains | |
1856 | * a list of all the guids of the vdevs that are being split off. | |
1857 | * | |
1858 | * This function determines what to do with that list: either rejoin | |
1859 | * all the disks to the pool, or complete the splitting process. To attempt | |
1860 | * the rejoin, each disk that is offlined is marked online again, and | |
1861 | * we do a reopen() call. If the vdev label for every disk that was | |
1862 | * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL) | |
1863 | * then we call vdev_split() on each disk, and complete the split. | |
1864 | * | |
1865 | * Otherwise we leave the config alone, with all the vdevs in place in | |
1866 | * the original pool. | |
1867 | */ | |
1868 | static void | |
1869 | spa_try_repair(spa_t *spa, nvlist_t *config) | |
1870 | { | |
1871 | uint_t extracted; | |
1872 | uint64_t *glist; | |
1873 | uint_t i, gcount; | |
1874 | nvlist_t *nvl; | |
1875 | vdev_t **vd; | |
1876 | boolean_t attempt_reopen; | |
1877 | ||
1878 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0) | |
1879 | return; | |
1880 | ||
1881 | /* check that the config is complete */ | |
1882 | if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, | |
1883 | &glist, &gcount) != 0) | |
1884 | return; | |
1885 | ||
b8d06fca | 1886 | vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE); |
428870ff BB |
1887 | |
1888 | /* attempt to online all the vdevs & validate */ | |
1889 | attempt_reopen = B_TRUE; | |
1890 | for (i = 0; i < gcount; i++) { | |
1891 | if (glist[i] == 0) /* vdev is hole */ | |
1892 | continue; | |
1893 | ||
1894 | vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE); | |
1895 | if (vd[i] == NULL) { | |
1896 | /* | |
1897 | * Don't bother attempting to reopen the disks; | |
1898 | * just do the split. | |
1899 | */ | |
1900 | attempt_reopen = B_FALSE; | |
1901 | } else { | |
1902 | /* attempt to re-online it */ | |
1903 | vd[i]->vdev_offline = B_FALSE; | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | if (attempt_reopen) { | |
1908 | vdev_reopen(spa->spa_root_vdev); | |
1909 | ||
1910 | /* check each device to see what state it's in */ | |
1911 | for (extracted = 0, i = 0; i < gcount; i++) { | |
1912 | if (vd[i] != NULL && | |
1913 | vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL) | |
1914 | break; | |
1915 | ++extracted; | |
1916 | } | |
1917 | } | |
1918 | ||
1919 | /* | |
1920 | * If every disk has been moved to the new pool, or if we never | |
1921 | * even attempted to look at them, then we split them off for | |
1922 | * good. | |
1923 | */ | |
1924 | if (!attempt_reopen || gcount == extracted) { | |
1925 | for (i = 0; i < gcount; i++) | |
1926 | if (vd[i] != NULL) | |
1927 | vdev_split(vd[i]); | |
1928 | vdev_reopen(spa->spa_root_vdev); | |
1929 | } | |
1930 | ||
1931 | kmem_free(vd, gcount * sizeof (vdev_t *)); | |
1932 | } | |
1933 | ||
1934 | static int | |
1935 | spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type, | |
1936 | boolean_t mosconfig) | |
1937 | { | |
1938 | nvlist_t *config = spa->spa_config; | |
1939 | char *ereport = FM_EREPORT_ZFS_POOL; | |
d96eb2b1 | 1940 | char *comment; |
428870ff BB |
1941 | int error; |
1942 | uint64_t pool_guid; | |
1943 | nvlist_t *nvl; | |
1944 | ||
1945 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) | |
1946 | return (EINVAL); | |
1947 | ||
d96eb2b1 DM |
1948 | ASSERT(spa->spa_comment == NULL); |
1949 | if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0) | |
1950 | spa->spa_comment = spa_strdup(comment); | |
1951 | ||
428870ff BB |
1952 | /* |
1953 | * Versioning wasn't explicitly added to the label until later, so if | |
1954 | * it's not present treat it as the initial version. | |
1955 | */ | |
1956 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, | |
1957 | &spa->spa_ubsync.ub_version) != 0) | |
1958 | spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL; | |
1959 | ||
1960 | (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
1961 | &spa->spa_config_txg); | |
1962 | ||
1963 | if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) && | |
1964 | spa_guid_exists(pool_guid, 0)) { | |
1965 | error = EEXIST; | |
1966 | } else { | |
3541dc6d | 1967 | spa->spa_config_guid = pool_guid; |
428870ff BB |
1968 | |
1969 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, | |
1970 | &nvl) == 0) { | |
1971 | VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting, | |
b8d06fca | 1972 | KM_PUSHPAGE) == 0); |
428870ff BB |
1973 | } |
1974 | ||
9ae529ec CS |
1975 | nvlist_free(spa->spa_load_info); |
1976 | spa->spa_load_info = fnvlist_alloc(); | |
1977 | ||
572e2857 | 1978 | gethrestime(&spa->spa_loaded_ts); |
428870ff BB |
1979 | error = spa_load_impl(spa, pool_guid, config, state, type, |
1980 | mosconfig, &ereport); | |
1981 | } | |
1982 | ||
1983 | spa->spa_minref = refcount_count(&spa->spa_refcount); | |
572e2857 BB |
1984 | if (error) { |
1985 | if (error != EEXIST) { | |
1986 | spa->spa_loaded_ts.tv_sec = 0; | |
1987 | spa->spa_loaded_ts.tv_nsec = 0; | |
1988 | } | |
1989 | if (error != EBADF) { | |
1990 | zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0); | |
1991 | } | |
1992 | } | |
428870ff BB |
1993 | spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE; |
1994 | spa->spa_ena = 0; | |
1995 | ||
1996 | return (error); | |
1997 | } | |
1998 | ||
1999 | /* | |
2000 | * Load an existing storage pool, using the pool's builtin spa_config as a | |
2001 | * source of configuration information. | |
2002 | */ | |
bf701a83 BB |
2003 | __attribute__((always_inline)) |
2004 | static inline int | |
428870ff BB |
2005 | spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config, |
2006 | spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig, | |
2007 | char **ereport) | |
2008 | { | |
2009 | int error = 0; | |
2010 | nvlist_t *nvroot = NULL; | |
9ae529ec | 2011 | nvlist_t *label; |
428870ff BB |
2012 | vdev_t *rvd; |
2013 | uberblock_t *ub = &spa->spa_uberblock; | |
572e2857 | 2014 | uint64_t children, config_cache_txg = spa->spa_config_txg; |
428870ff BB |
2015 | int orig_mode = spa->spa_mode; |
2016 | int parse; | |
2017 | uint64_t obj; | |
9ae529ec | 2018 | boolean_t missing_feat_write = B_FALSE; |
428870ff BB |
2019 | |
2020 | /* | |
2021 | * If this is an untrusted config, access the pool in read-only mode. | |
2022 | * This prevents things like resilvering recently removed devices. | |
2023 | */ | |
2024 | if (!mosconfig) | |
2025 | spa->spa_mode = FREAD; | |
2026 | ||
2027 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
2028 | ||
2029 | spa->spa_load_state = state; | |
2030 | ||
2031 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot)) | |
2032 | return (EINVAL); | |
2033 | ||
2034 | parse = (type == SPA_IMPORT_EXISTING ? | |
2035 | VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT); | |
2036 | ||
2037 | /* | |
2038 | * Create "The Godfather" zio to hold all async IOs | |
2039 | */ | |
2040 | spa->spa_async_zio_root = zio_root(spa, NULL, NULL, | |
2041 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); | |
2042 | ||
2043 | /* | |
2044 | * Parse the configuration into a vdev tree. We explicitly set the | |
2045 | * value that will be returned by spa_version() since parsing the | |
2046 | * configuration requires knowing the version number. | |
2047 | */ | |
2048 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2049 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse); | |
2050 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2051 | ||
2052 | if (error != 0) | |
2053 | return (error); | |
2054 | ||
2055 | ASSERT(spa->spa_root_vdev == rvd); | |
2056 | ||
2057 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2058 | ASSERT(spa_guid(spa) == pool_guid); | |
2059 | } | |
2060 | ||
2061 | /* | |
2062 | * Try to open all vdevs, loading each label in the process. | |
2063 | */ | |
2064 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2065 | error = vdev_open(rvd); | |
2066 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2067 | if (error != 0) | |
2068 | return (error); | |
2069 | ||
2070 | /* | |
2071 | * We need to validate the vdev labels against the configuration that | |
2072 | * we have in hand, which is dependent on the setting of mosconfig. If | |
2073 | * mosconfig is true then we're validating the vdev labels based on | |
2074 | * that config. Otherwise, we're validating against the cached config | |
2075 | * (zpool.cache) that was read when we loaded the zfs module, and then | |
2076 | * later we will recursively call spa_load() and validate against | |
2077 | * the vdev config. | |
2078 | * | |
2079 | * If we're assembling a new pool that's been split off from an | |
2080 | * existing pool, the labels haven't yet been updated so we skip | |
2081 | * validation for now. | |
2082 | */ | |
2083 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2084 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
c7f2d69d | 2085 | error = vdev_validate(rvd, mosconfig); |
428870ff BB |
2086 | spa_config_exit(spa, SCL_ALL, FTAG); |
2087 | ||
2088 | if (error != 0) | |
2089 | return (error); | |
2090 | ||
2091 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) | |
2092 | return (ENXIO); | |
2093 | } | |
2094 | ||
2095 | /* | |
2096 | * Find the best uberblock. | |
2097 | */ | |
9ae529ec | 2098 | vdev_uberblock_load(rvd, ub, &label); |
428870ff BB |
2099 | |
2100 | /* | |
2101 | * If we weren't able to find a single valid uberblock, return failure. | |
2102 | */ | |
9ae529ec CS |
2103 | if (ub->ub_txg == 0) { |
2104 | nvlist_free(label); | |
428870ff | 2105 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO)); |
9ae529ec | 2106 | } |
428870ff BB |
2107 | |
2108 | /* | |
9ae529ec | 2109 | * If the pool has an unsupported version we can't open it. |
428870ff | 2110 | */ |
9ae529ec CS |
2111 | if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) { |
2112 | nvlist_free(label); | |
428870ff | 2113 | return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP)); |
9ae529ec CS |
2114 | } |
2115 | ||
2116 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2117 | nvlist_t *features; | |
2118 | ||
2119 | /* | |
2120 | * If we weren't able to find what's necessary for reading the | |
2121 | * MOS in the label, return failure. | |
2122 | */ | |
2123 | if (label == NULL || nvlist_lookup_nvlist(label, | |
2124 | ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) { | |
2125 | nvlist_free(label); | |
2126 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, | |
2127 | ENXIO)); | |
2128 | } | |
2129 | ||
2130 | /* | |
2131 | * Update our in-core representation with the definitive values | |
2132 | * from the label. | |
2133 | */ | |
2134 | nvlist_free(spa->spa_label_features); | |
2135 | VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0); | |
2136 | } | |
2137 | ||
2138 | nvlist_free(label); | |
2139 | ||
2140 | /* | |
2141 | * Look through entries in the label nvlist's features_for_read. If | |
2142 | * there is a feature listed there which we don't understand then we | |
2143 | * cannot open a pool. | |
2144 | */ | |
2145 | if (ub->ub_version >= SPA_VERSION_FEATURES) { | |
2146 | nvlist_t *unsup_feat; | |
2147 | nvpair_t *nvp; | |
2148 | ||
2149 | VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) == | |
2150 | 0); | |
2151 | ||
2152 | for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL); | |
2153 | nvp != NULL; | |
2154 | nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) { | |
2155 | if (!zfeature_is_supported(nvpair_name(nvp))) { | |
2156 | VERIFY(nvlist_add_string(unsup_feat, | |
2157 | nvpair_name(nvp), "") == 0); | |
2158 | } | |
2159 | } | |
2160 | ||
2161 | if (!nvlist_empty(unsup_feat)) { | |
2162 | VERIFY(nvlist_add_nvlist(spa->spa_load_info, | |
2163 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0); | |
2164 | nvlist_free(unsup_feat); | |
2165 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, | |
2166 | ENOTSUP)); | |
2167 | } | |
2168 | ||
2169 | nvlist_free(unsup_feat); | |
2170 | } | |
428870ff BB |
2171 | |
2172 | /* | |
2173 | * If the vdev guid sum doesn't match the uberblock, we have an | |
572e2857 BB |
2174 | * incomplete configuration. We first check to see if the pool |
2175 | * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN). | |
2176 | * If it is, defer the vdev_guid_sum check till later so we | |
2177 | * can handle missing vdevs. | |
428870ff | 2178 | */ |
572e2857 BB |
2179 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, |
2180 | &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE && | |
428870ff BB |
2181 | rvd->vdev_guid_sum != ub->ub_guid_sum) |
2182 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO)); | |
2183 | ||
2184 | if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) { | |
2185 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
2186 | spa_try_repair(spa, config); | |
2187 | spa_config_exit(spa, SCL_ALL, FTAG); | |
2188 | nvlist_free(spa->spa_config_splitting); | |
2189 | spa->spa_config_splitting = NULL; | |
2190 | } | |
2191 | ||
2192 | /* | |
2193 | * Initialize internal SPA structures. | |
2194 | */ | |
2195 | spa->spa_state = POOL_STATE_ACTIVE; | |
2196 | spa->spa_ubsync = spa->spa_uberblock; | |
2197 | spa->spa_verify_min_txg = spa->spa_extreme_rewind ? | |
2198 | TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1; | |
2199 | spa->spa_first_txg = spa->spa_last_ubsync_txg ? | |
2200 | spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1; | |
2201 | spa->spa_claim_max_txg = spa->spa_first_txg; | |
2202 | spa->spa_prev_software_version = ub->ub_software_version; | |
2203 | ||
9ae529ec | 2204 | error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool); |
428870ff BB |
2205 | if (error) |
2206 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2207 | spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset; | |
2208 | ||
2209 | if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0) | |
2210 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2211 | ||
9ae529ec CS |
2212 | if (spa_version(spa) >= SPA_VERSION_FEATURES) { |
2213 | boolean_t missing_feat_read = B_FALSE; | |
b9b24bb4 | 2214 | nvlist_t *unsup_feat, *enabled_feat; |
9ae529ec CS |
2215 | |
2216 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ, | |
2217 | &spa->spa_feat_for_read_obj) != 0) { | |
2218 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2219 | } | |
2220 | ||
2221 | if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE, | |
2222 | &spa->spa_feat_for_write_obj) != 0) { | |
2223 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2224 | } | |
2225 | ||
2226 | if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS, | |
2227 | &spa->spa_feat_desc_obj) != 0) { | |
2228 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2229 | } | |
2230 | ||
b9b24bb4 CS |
2231 | enabled_feat = fnvlist_alloc(); |
2232 | unsup_feat = fnvlist_alloc(); | |
9ae529ec CS |
2233 | |
2234 | if (!feature_is_supported(spa->spa_meta_objset, | |
2235 | spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj, | |
b9b24bb4 | 2236 | unsup_feat, enabled_feat)) |
9ae529ec CS |
2237 | missing_feat_read = B_TRUE; |
2238 | ||
2239 | if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) { | |
2240 | if (!feature_is_supported(spa->spa_meta_objset, | |
2241 | spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj, | |
b9b24bb4 | 2242 | unsup_feat, enabled_feat)) { |
9ae529ec | 2243 | missing_feat_write = B_TRUE; |
b9b24bb4 | 2244 | } |
9ae529ec CS |
2245 | } |
2246 | ||
b9b24bb4 CS |
2247 | fnvlist_add_nvlist(spa->spa_load_info, |
2248 | ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat); | |
2249 | ||
9ae529ec | 2250 | if (!nvlist_empty(unsup_feat)) { |
b9b24bb4 CS |
2251 | fnvlist_add_nvlist(spa->spa_load_info, |
2252 | ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat); | |
9ae529ec CS |
2253 | } |
2254 | ||
b9b24bb4 CS |
2255 | fnvlist_free(enabled_feat); |
2256 | fnvlist_free(unsup_feat); | |
9ae529ec CS |
2257 | |
2258 | if (!missing_feat_read) { | |
2259 | fnvlist_add_boolean(spa->spa_load_info, | |
2260 | ZPOOL_CONFIG_CAN_RDONLY); | |
2261 | } | |
2262 | ||
2263 | /* | |
2264 | * If the state is SPA_LOAD_TRYIMPORT, our objective is | |
2265 | * twofold: to determine whether the pool is available for | |
2266 | * import in read-write mode and (if it is not) whether the | |
2267 | * pool is available for import in read-only mode. If the pool | |
2268 | * is available for import in read-write mode, it is displayed | |
2269 | * as available in userland; if it is not available for import | |
2270 | * in read-only mode, it is displayed as unavailable in | |
2271 | * userland. If the pool is available for import in read-only | |
2272 | * mode but not read-write mode, it is displayed as unavailable | |
2273 | * in userland with a special note that the pool is actually | |
2274 | * available for open in read-only mode. | |
2275 | * | |
2276 | * As a result, if the state is SPA_LOAD_TRYIMPORT and we are | |
2277 | * missing a feature for write, we must first determine whether | |
2278 | * the pool can be opened read-only before returning to | |
2279 | * userland in order to know whether to display the | |
2280 | * abovementioned note. | |
2281 | */ | |
2282 | if (missing_feat_read || (missing_feat_write && | |
2283 | spa_writeable(spa))) { | |
2284 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, | |
2285 | ENOTSUP)); | |
2286 | } | |
2287 | } | |
2288 | ||
2289 | spa->spa_is_initializing = B_TRUE; | |
2290 | error = dsl_pool_open(spa->spa_dsl_pool); | |
2291 | spa->spa_is_initializing = B_FALSE; | |
2292 | if (error != 0) | |
2293 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2294 | ||
428870ff BB |
2295 | if (!mosconfig) { |
2296 | uint64_t hostid; | |
2297 | nvlist_t *policy = NULL, *nvconfig; | |
2298 | ||
2299 | if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) | |
2300 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2301 | ||
2302 | if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig, | |
b128c09f | 2303 | ZPOOL_CONFIG_HOSTID, &hostid) == 0) { |
34dc7c2f BB |
2304 | char *hostname; |
2305 | unsigned long myhostid = 0; | |
2306 | ||
428870ff | 2307 | VERIFY(nvlist_lookup_string(nvconfig, |
34dc7c2f BB |
2308 | ZPOOL_CONFIG_HOSTNAME, &hostname) == 0); |
2309 | ||
d164b209 BB |
2310 | #ifdef _KERNEL |
2311 | myhostid = zone_get_hostid(NULL); | |
2312 | #else /* _KERNEL */ | |
2313 | /* | |
2314 | * We're emulating the system's hostid in userland, so | |
2315 | * we can't use zone_get_hostid(). | |
2316 | */ | |
34dc7c2f | 2317 | (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid); |
d164b209 | 2318 | #endif /* _KERNEL */ |
34dc7c2f | 2319 | if (hostid != 0 && myhostid != 0 && |
d164b209 | 2320 | hostid != myhostid) { |
428870ff | 2321 | nvlist_free(nvconfig); |
34dc7c2f BB |
2322 | cmn_err(CE_WARN, "pool '%s' could not be " |
2323 | "loaded as it was last accessed by " | |
b128c09f | 2324 | "another system (host: %s hostid: 0x%lx). " |
3cee2262 | 2325 | "See: http://zfsonlinux.org/msg/ZFS-8000-EY", |
b128c09f | 2326 | spa_name(spa), hostname, |
34dc7c2f | 2327 | (unsigned long)hostid); |
428870ff | 2328 | return (EBADF); |
34dc7c2f BB |
2329 | } |
2330 | } | |
428870ff BB |
2331 | if (nvlist_lookup_nvlist(spa->spa_config, |
2332 | ZPOOL_REWIND_POLICY, &policy) == 0) | |
2333 | VERIFY(nvlist_add_nvlist(nvconfig, | |
2334 | ZPOOL_REWIND_POLICY, policy) == 0); | |
34dc7c2f | 2335 | |
428870ff | 2336 | spa_config_set(spa, nvconfig); |
34dc7c2f BB |
2337 | spa_unload(spa); |
2338 | spa_deactivate(spa); | |
fb5f0bc8 | 2339 | spa_activate(spa, orig_mode); |
34dc7c2f | 2340 | |
428870ff | 2341 | return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE)); |
34dc7c2f BB |
2342 | } |
2343 | ||
428870ff BB |
2344 | if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0) |
2345 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2346 | error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj); | |
2347 | if (error != 0) | |
2348 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
2349 | |
2350 | /* | |
2351 | * Load the bit that tells us to use the new accounting function | |
2352 | * (raid-z deflation). If we have an older pool, this will not | |
2353 | * be present. | |
2354 | */ | |
428870ff BB |
2355 | error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate); |
2356 | if (error != 0 && error != ENOENT) | |
2357 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2358 | ||
2359 | error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION, | |
2360 | &spa->spa_creation_version); | |
2361 | if (error != 0 && error != ENOENT) | |
2362 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
2363 | |
2364 | /* | |
2365 | * Load the persistent error log. If we have an older pool, this will | |
2366 | * not be present. | |
2367 | */ | |
428870ff BB |
2368 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last); |
2369 | if (error != 0 && error != ENOENT) | |
2370 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 2371 | |
428870ff BB |
2372 | error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB, |
2373 | &spa->spa_errlog_scrub); | |
2374 | if (error != 0 && error != ENOENT) | |
2375 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
2376 | |
2377 | /* | |
2378 | * Load the history object. If we have an older pool, this | |
2379 | * will not be present. | |
2380 | */ | |
428870ff BB |
2381 | error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history); |
2382 | if (error != 0 && error != ENOENT) | |
2383 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2384 | ||
2385 | /* | |
2386 | * If we're assembling the pool from the split-off vdevs of | |
2387 | * an existing pool, we don't want to attach the spares & cache | |
2388 | * devices. | |
2389 | */ | |
34dc7c2f BB |
2390 | |
2391 | /* | |
2392 | * Load any hot spares for this pool. | |
2393 | */ | |
428870ff BB |
2394 | error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object); |
2395 | if (error != 0 && error != ENOENT) | |
2396 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2397 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
2398 | ASSERT(spa_version(spa) >= SPA_VERSION_SPARES); |
2399 | if (load_nvlist(spa, spa->spa_spares.sav_object, | |
428870ff BB |
2400 | &spa->spa_spares.sav_config) != 0) |
2401 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 2402 | |
b128c09f | 2403 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 2404 | spa_load_spares(spa); |
b128c09f | 2405 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
2406 | } else if (error == 0) { |
2407 | spa->spa_spares.sav_sync = B_TRUE; | |
34dc7c2f BB |
2408 | } |
2409 | ||
2410 | /* | |
2411 | * Load any level 2 ARC devices for this pool. | |
2412 | */ | |
428870ff | 2413 | error = spa_dir_prop(spa, DMU_POOL_L2CACHE, |
34dc7c2f | 2414 | &spa->spa_l2cache.sav_object); |
428870ff BB |
2415 | if (error != 0 && error != ENOENT) |
2416 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2417 | if (error == 0 && type != SPA_IMPORT_ASSEMBLE) { | |
34dc7c2f BB |
2418 | ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE); |
2419 | if (load_nvlist(spa, spa->spa_l2cache.sav_object, | |
428870ff BB |
2420 | &spa->spa_l2cache.sav_config) != 0) |
2421 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f | 2422 | |
b128c09f | 2423 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 2424 | spa_load_l2cache(spa); |
b128c09f | 2425 | spa_config_exit(spa, SCL_ALL, FTAG); |
428870ff BB |
2426 | } else if (error == 0) { |
2427 | spa->spa_l2cache.sav_sync = B_TRUE; | |
b128c09f BB |
2428 | } |
2429 | ||
34dc7c2f BB |
2430 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); |
2431 | ||
428870ff BB |
2432 | error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object); |
2433 | if (error && error != ENOENT) | |
2434 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
34dc7c2f BB |
2435 | |
2436 | if (error == 0) { | |
428870ff BB |
2437 | uint64_t autoreplace; |
2438 | ||
2439 | spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs); | |
2440 | spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace); | |
2441 | spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation); | |
2442 | spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode); | |
2443 | spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand); | |
2444 | spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO, | |
2445 | &spa->spa_dedup_ditto); | |
2446 | ||
2447 | spa->spa_autoreplace = (autoreplace != 0); | |
34dc7c2f BB |
2448 | } |
2449 | ||
2450 | /* | |
2451 | * If the 'autoreplace' property is set, then post a resource notifying | |
2452 | * the ZFS DE that it should not issue any faults for unopenable | |
2453 | * devices. We also iterate over the vdevs, and post a sysevent for any | |
2454 | * unopenable vdevs so that the normal autoreplace handler can take | |
2455 | * over. | |
2456 | */ | |
428870ff | 2457 | if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) { |
34dc7c2f | 2458 | spa_check_removed(spa->spa_root_vdev); |
428870ff BB |
2459 | /* |
2460 | * For the import case, this is done in spa_import(), because | |
2461 | * at this point we're using the spare definitions from | |
2462 | * the MOS config, not necessarily from the userland config. | |
2463 | */ | |
2464 | if (state != SPA_LOAD_IMPORT) { | |
2465 | spa_aux_check_removed(&spa->spa_spares); | |
2466 | spa_aux_check_removed(&spa->spa_l2cache); | |
2467 | } | |
2468 | } | |
34dc7c2f BB |
2469 | |
2470 | /* | |
2471 | * Load the vdev state for all toplevel vdevs. | |
2472 | */ | |
2473 | vdev_load(rvd); | |
2474 | ||
2475 | /* | |
2476 | * Propagate the leaf DTLs we just loaded all the way up the tree. | |
2477 | */ | |
b128c09f | 2478 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 2479 | vdev_dtl_reassess(rvd, 0, 0, B_FALSE); |
b128c09f | 2480 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 2481 | |
428870ff BB |
2482 | /* |
2483 | * Load the DDTs (dedup tables). | |
2484 | */ | |
2485 | error = ddt_load(spa); | |
2486 | if (error != 0) | |
2487 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2488 | ||
2489 | spa_update_dspace(spa); | |
2490 | ||
428870ff | 2491 | /* |
572e2857 BB |
2492 | * Validate the config, using the MOS config to fill in any |
2493 | * information which might be missing. If we fail to validate | |
2494 | * the config then declare the pool unfit for use. If we're | |
2495 | * assembling a pool from a split, the log is not transferred | |
2496 | * over. | |
428870ff BB |
2497 | */ |
2498 | if (type != SPA_IMPORT_ASSEMBLE) { | |
2499 | nvlist_t *nvconfig; | |
2500 | ||
2501 | if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0) | |
2502 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO)); | |
2503 | ||
572e2857 BB |
2504 | if (!spa_config_valid(spa, nvconfig)) { |
2505 | nvlist_free(nvconfig); | |
2506 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, | |
2507 | ENXIO)); | |
2508 | } | |
428870ff BB |
2509 | nvlist_free(nvconfig); |
2510 | ||
572e2857 | 2511 | /* |
9ae529ec | 2512 | * Now that we've validated the config, check the state of the |
572e2857 BB |
2513 | * root vdev. If it can't be opened, it indicates one or |
2514 | * more toplevel vdevs are faulted. | |
2515 | */ | |
2516 | if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) | |
2517 | return (ENXIO); | |
2518 | ||
428870ff BB |
2519 | if (spa_check_logs(spa)) { |
2520 | *ereport = FM_EREPORT_ZFS_LOG_REPLAY; | |
2521 | return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO)); | |
2522 | } | |
2523 | } | |
2524 | ||
9ae529ec CS |
2525 | if (missing_feat_write) { |
2526 | ASSERT(state == SPA_LOAD_TRYIMPORT); | |
2527 | ||
2528 | /* | |
2529 | * At this point, we know that we can open the pool in | |
2530 | * read-only mode but not read-write mode. We now have enough | |
2531 | * information and can return to userland. | |
2532 | */ | |
2533 | return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP)); | |
2534 | } | |
2535 | ||
572e2857 BB |
2536 | /* |
2537 | * We've successfully opened the pool, verify that we're ready | |
2538 | * to start pushing transactions. | |
2539 | */ | |
2540 | if (state != SPA_LOAD_TRYIMPORT) { | |
c65aa5b2 | 2541 | if ((error = spa_load_verify(spa))) |
572e2857 BB |
2542 | return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, |
2543 | error)); | |
2544 | } | |
2545 | ||
428870ff BB |
2546 | if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER || |
2547 | spa->spa_load_max_txg == UINT64_MAX)) { | |
34dc7c2f BB |
2548 | dmu_tx_t *tx; |
2549 | int need_update = B_FALSE; | |
d6320ddb | 2550 | int c; |
fb5f0bc8 BB |
2551 | |
2552 | ASSERT(state != SPA_LOAD_TRYIMPORT); | |
34dc7c2f BB |
2553 | |
2554 | /* | |
2555 | * Claim log blocks that haven't been committed yet. | |
2556 | * This must all happen in a single txg. | |
428870ff BB |
2557 | * Note: spa_claim_max_txg is updated by spa_claim_notify(), |
2558 | * invoked from zil_claim_log_block()'s i/o done callback. | |
2559 | * Price of rollback is that we abandon the log. | |
34dc7c2f | 2560 | */ |
428870ff BB |
2561 | spa->spa_claiming = B_TRUE; |
2562 | ||
34dc7c2f BB |
2563 | tx = dmu_tx_create_assigned(spa_get_dsl(spa), |
2564 | spa_first_txg(spa)); | |
b128c09f | 2565 | (void) dmu_objset_find(spa_name(spa), |
34dc7c2f BB |
2566 | zil_claim, tx, DS_FIND_CHILDREN); |
2567 | dmu_tx_commit(tx); | |
2568 | ||
428870ff BB |
2569 | spa->spa_claiming = B_FALSE; |
2570 | ||
2571 | spa_set_log_state(spa, SPA_LOG_GOOD); | |
34dc7c2f BB |
2572 | spa->spa_sync_on = B_TRUE; |
2573 | txg_sync_start(spa->spa_dsl_pool); | |
2574 | ||
2575 | /* | |
428870ff BB |
2576 | * Wait for all claims to sync. We sync up to the highest |
2577 | * claimed log block birth time so that claimed log blocks | |
2578 | * don't appear to be from the future. spa_claim_max_txg | |
2579 | * will have been set for us by either zil_check_log_chain() | |
2580 | * (invoked from spa_check_logs()) or zil_claim() above. | |
34dc7c2f | 2581 | */ |
428870ff | 2582 | txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg); |
34dc7c2f BB |
2583 | |
2584 | /* | |
2585 | * If the config cache is stale, or we have uninitialized | |
2586 | * metaslabs (see spa_vdev_add()), then update the config. | |
45d1cae3 | 2587 | * |
572e2857 | 2588 | * If this is a verbatim import, trust the current |
45d1cae3 | 2589 | * in-core spa_config and update the disk labels. |
34dc7c2f BB |
2590 | */ |
2591 | if (config_cache_txg != spa->spa_config_txg || | |
572e2857 BB |
2592 | state == SPA_LOAD_IMPORT || |
2593 | state == SPA_LOAD_RECOVER || | |
2594 | (spa->spa_import_flags & ZFS_IMPORT_VERBATIM)) | |
34dc7c2f BB |
2595 | need_update = B_TRUE; |
2596 | ||
d6320ddb | 2597 | for (c = 0; c < rvd->vdev_children; c++) |
34dc7c2f BB |
2598 | if (rvd->vdev_child[c]->vdev_ms_array == 0) |
2599 | need_update = B_TRUE; | |
2600 | ||
2601 | /* | |
2602 | * Update the config cache asychronously in case we're the | |
2603 | * root pool, in which case the config cache isn't writable yet. | |
2604 | */ | |
2605 | if (need_update) | |
2606 | spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE); | |
fb5f0bc8 BB |
2607 | |
2608 | /* | |
2609 | * Check all DTLs to see if anything needs resilvering. | |
2610 | */ | |
428870ff BB |
2611 | if (!dsl_scan_resilvering(spa->spa_dsl_pool) && |
2612 | vdev_resilver_needed(rvd, NULL, NULL)) | |
fb5f0bc8 | 2613 | spa_async_request(spa, SPA_ASYNC_RESILVER); |
428870ff BB |
2614 | |
2615 | /* | |
2616 | * Delete any inconsistent datasets. | |
2617 | */ | |
2618 | (void) dmu_objset_find(spa_name(spa), | |
2619 | dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN); | |
2620 | ||
2621 | /* | |
2622 | * Clean up any stale temporary dataset userrefs. | |
2623 | */ | |
2624 | dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool); | |
34dc7c2f BB |
2625 | } |
2626 | ||
428870ff BB |
2627 | return (0); |
2628 | } | |
34dc7c2f | 2629 | |
428870ff BB |
2630 | static int |
2631 | spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig) | |
2632 | { | |
572e2857 BB |
2633 | int mode = spa->spa_mode; |
2634 | ||
428870ff BB |
2635 | spa_unload(spa); |
2636 | spa_deactivate(spa); | |
2637 | ||
2638 | spa->spa_load_max_txg--; | |
2639 | ||
572e2857 | 2640 | spa_activate(spa, mode); |
428870ff BB |
2641 | spa_async_suspend(spa); |
2642 | ||
2643 | return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig)); | |
2644 | } | |
2645 | ||
9ae529ec CS |
2646 | /* |
2647 | * If spa_load() fails this function will try loading prior txg's. If | |
2648 | * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool | |
2649 | * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this | |
2650 | * function will not rewind the pool and will return the same error as | |
2651 | * spa_load(). | |
2652 | */ | |
428870ff BB |
2653 | static int |
2654 | spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig, | |
2655 | uint64_t max_request, int rewind_flags) | |
2656 | { | |
9ae529ec | 2657 | nvlist_t *loadinfo = NULL; |
428870ff BB |
2658 | nvlist_t *config = NULL; |
2659 | int load_error, rewind_error; | |
2660 | uint64_t safe_rewind_txg; | |
2661 | uint64_t min_txg; | |
2662 | ||
2663 | if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) { | |
2664 | spa->spa_load_max_txg = spa->spa_load_txg; | |
2665 | spa_set_log_state(spa, SPA_LOG_CLEAR); | |
2666 | } else { | |
2667 | spa->spa_load_max_txg = max_request; | |
2668 | } | |
2669 | ||
2670 | load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING, | |
2671 | mosconfig); | |
2672 | if (load_error == 0) | |
2673 | return (0); | |
2674 | ||
2675 | if (spa->spa_root_vdev != NULL) | |
2676 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); | |
2677 | ||
2678 | spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg; | |
2679 | spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp; | |
2680 | ||
2681 | if (rewind_flags & ZPOOL_NEVER_REWIND) { | |
2682 | nvlist_free(config); | |
2683 | return (load_error); | |
2684 | } | |
2685 | ||
9ae529ec CS |
2686 | if (state == SPA_LOAD_RECOVER) { |
2687 | /* Price of rolling back is discarding txgs, including log */ | |
428870ff | 2688 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
9ae529ec CS |
2689 | } else { |
2690 | /* | |
2691 | * If we aren't rolling back save the load info from our first | |
2692 | * import attempt so that we can restore it after attempting | |
2693 | * to rewind. | |
2694 | */ | |
2695 | loadinfo = spa->spa_load_info; | |
2696 | spa->spa_load_info = fnvlist_alloc(); | |
2697 | } | |
428870ff BB |
2698 | |
2699 | spa->spa_load_max_txg = spa->spa_last_ubsync_txg; | |
2700 | safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE; | |
2701 | min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ? | |
2702 | TXG_INITIAL : safe_rewind_txg; | |
2703 | ||
2704 | /* | |
2705 | * Continue as long as we're finding errors, we're still within | |
2706 | * the acceptable rewind range, and we're still finding uberblocks | |
2707 | */ | |
2708 | while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg && | |
2709 | spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) { | |
2710 | if (spa->spa_load_max_txg < safe_rewind_txg) | |
2711 | spa->spa_extreme_rewind = B_TRUE; | |
2712 | rewind_error = spa_load_retry(spa, state, mosconfig); | |
2713 | } | |
2714 | ||
428870ff BB |
2715 | spa->spa_extreme_rewind = B_FALSE; |
2716 | spa->spa_load_max_txg = UINT64_MAX; | |
2717 | ||
2718 | if (config && (rewind_error || state != SPA_LOAD_RECOVER)) | |
2719 | spa_config_set(spa, config); | |
2720 | ||
9ae529ec CS |
2721 | if (state == SPA_LOAD_RECOVER) { |
2722 | ASSERT3P(loadinfo, ==, NULL); | |
2723 | return (rewind_error); | |
2724 | } else { | |
2725 | /* Store the rewind info as part of the initial load info */ | |
2726 | fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO, | |
2727 | spa->spa_load_info); | |
2728 | ||
2729 | /* Restore the initial load info */ | |
2730 | fnvlist_free(spa->spa_load_info); | |
2731 | spa->spa_load_info = loadinfo; | |
2732 | ||
2733 | return (load_error); | |
2734 | } | |
34dc7c2f BB |
2735 | } |
2736 | ||
2737 | /* | |
2738 | * Pool Open/Import | |
2739 | * | |
2740 | * The import case is identical to an open except that the configuration is sent | |
2741 | * down from userland, instead of grabbed from the configuration cache. For the | |
2742 | * case of an open, the pool configuration will exist in the | |
2743 | * POOL_STATE_UNINITIALIZED state. | |
2744 | * | |
2745 | * The stats information (gen/count/ustats) is used to gather vdev statistics at | |
2746 | * the same time open the pool, without having to keep around the spa_t in some | |
2747 | * ambiguous state. | |
2748 | */ | |
2749 | static int | |
428870ff BB |
2750 | spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy, |
2751 | nvlist_t **config) | |
34dc7c2f BB |
2752 | { |
2753 | spa_t *spa; | |
572e2857 | 2754 | spa_load_state_t state = SPA_LOAD_OPEN; |
34dc7c2f | 2755 | int error; |
34dc7c2f BB |
2756 | int locked = B_FALSE; |
2757 | ||
2758 | *spapp = NULL; | |
2759 | ||
2760 | /* | |
2761 | * As disgusting as this is, we need to support recursive calls to this | |
2762 | * function because dsl_dir_open() is called during spa_load(), and ends | |
2763 | * up calling spa_open() again. The real fix is to figure out how to | |
2764 | * avoid dsl_dir_open() calling this in the first place. | |
2765 | */ | |
2766 | if (mutex_owner(&spa_namespace_lock) != curthread) { | |
2767 | mutex_enter(&spa_namespace_lock); | |
2768 | locked = B_TRUE; | |
2769 | } | |
2770 | ||
2771 | if ((spa = spa_lookup(pool)) == NULL) { | |
2772 | if (locked) | |
2773 | mutex_exit(&spa_namespace_lock); | |
2774 | return (ENOENT); | |
2775 | } | |
428870ff | 2776 | |
34dc7c2f | 2777 | if (spa->spa_state == POOL_STATE_UNINITIALIZED) { |
428870ff BB |
2778 | zpool_rewind_policy_t policy; |
2779 | ||
2780 | zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config, | |
2781 | &policy); | |
2782 | if (policy.zrp_request & ZPOOL_DO_REWIND) | |
2783 | state = SPA_LOAD_RECOVER; | |
34dc7c2f | 2784 | |
fb5f0bc8 | 2785 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 2786 | |
428870ff BB |
2787 | if (state != SPA_LOAD_RECOVER) |
2788 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
2789 | ||
2790 | error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg, | |
2791 | policy.zrp_request); | |
34dc7c2f BB |
2792 | |
2793 | if (error == EBADF) { | |
2794 | /* | |
2795 | * If vdev_validate() returns failure (indicated by | |
2796 | * EBADF), it indicates that one of the vdevs indicates | |
2797 | * that the pool has been exported or destroyed. If | |
2798 | * this is the case, the config cache is out of sync and | |
2799 | * we should remove the pool from the namespace. | |
2800 | */ | |
34dc7c2f BB |
2801 | spa_unload(spa); |
2802 | spa_deactivate(spa); | |
b128c09f | 2803 | spa_config_sync(spa, B_TRUE, B_TRUE); |
34dc7c2f | 2804 | spa_remove(spa); |
34dc7c2f BB |
2805 | if (locked) |
2806 | mutex_exit(&spa_namespace_lock); | |
2807 | return (ENOENT); | |
2808 | } | |
2809 | ||
2810 | if (error) { | |
2811 | /* | |
2812 | * We can't open the pool, but we still have useful | |
2813 | * information: the state of each vdev after the | |
2814 | * attempted vdev_open(). Return this to the user. | |
2815 | */ | |
572e2857 | 2816 | if (config != NULL && spa->spa_config) { |
428870ff | 2817 | VERIFY(nvlist_dup(spa->spa_config, config, |
b8d06fca | 2818 | KM_PUSHPAGE) == 0); |
572e2857 BB |
2819 | VERIFY(nvlist_add_nvlist(*config, |
2820 | ZPOOL_CONFIG_LOAD_INFO, | |
2821 | spa->spa_load_info) == 0); | |
2822 | } | |
34dc7c2f BB |
2823 | spa_unload(spa); |
2824 | spa_deactivate(spa); | |
428870ff | 2825 | spa->spa_last_open_failed = error; |
34dc7c2f BB |
2826 | if (locked) |
2827 | mutex_exit(&spa_namespace_lock); | |
2828 | *spapp = NULL; | |
2829 | return (error); | |
34dc7c2f | 2830 | } |
34dc7c2f BB |
2831 | } |
2832 | ||
2833 | spa_open_ref(spa, tag); | |
2834 | ||
b128c09f | 2835 | if (config != NULL) |
34dc7c2f | 2836 | *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f | 2837 | |
572e2857 BB |
2838 | /* |
2839 | * If we've recovered the pool, pass back any information we | |
2840 | * gathered while doing the load. | |
2841 | */ | |
2842 | if (state == SPA_LOAD_RECOVER) { | |
2843 | VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO, | |
2844 | spa->spa_load_info) == 0); | |
2845 | } | |
2846 | ||
428870ff BB |
2847 | if (locked) { |
2848 | spa->spa_last_open_failed = 0; | |
2849 | spa->spa_last_ubsync_txg = 0; | |
2850 | spa->spa_load_txg = 0; | |
2851 | mutex_exit(&spa_namespace_lock); | |
2852 | } | |
2853 | ||
2854 | *spapp = spa; | |
2855 | ||
34dc7c2f BB |
2856 | return (0); |
2857 | } | |
2858 | ||
428870ff BB |
2859 | int |
2860 | spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy, | |
2861 | nvlist_t **config) | |
2862 | { | |
2863 | return (spa_open_common(name, spapp, tag, policy, config)); | |
2864 | } | |
2865 | ||
34dc7c2f BB |
2866 | int |
2867 | spa_open(const char *name, spa_t **spapp, void *tag) | |
2868 | { | |
428870ff | 2869 | return (spa_open_common(name, spapp, tag, NULL, NULL)); |
34dc7c2f BB |
2870 | } |
2871 | ||
2872 | /* | |
2873 | * Lookup the given spa_t, incrementing the inject count in the process, | |
2874 | * preventing it from being exported or destroyed. | |
2875 | */ | |
2876 | spa_t * | |
2877 | spa_inject_addref(char *name) | |
2878 | { | |
2879 | spa_t *spa; | |
2880 | ||
2881 | mutex_enter(&spa_namespace_lock); | |
2882 | if ((spa = spa_lookup(name)) == NULL) { | |
2883 | mutex_exit(&spa_namespace_lock); | |
2884 | return (NULL); | |
2885 | } | |
2886 | spa->spa_inject_ref++; | |
2887 | mutex_exit(&spa_namespace_lock); | |
2888 | ||
2889 | return (spa); | |
2890 | } | |
2891 | ||
2892 | void | |
2893 | spa_inject_delref(spa_t *spa) | |
2894 | { | |
2895 | mutex_enter(&spa_namespace_lock); | |
2896 | spa->spa_inject_ref--; | |
2897 | mutex_exit(&spa_namespace_lock); | |
2898 | } | |
2899 | ||
2900 | /* | |
2901 | * Add spares device information to the nvlist. | |
2902 | */ | |
2903 | static void | |
2904 | spa_add_spares(spa_t *spa, nvlist_t *config) | |
2905 | { | |
2906 | nvlist_t **spares; | |
2907 | uint_t i, nspares; | |
2908 | nvlist_t *nvroot; | |
2909 | uint64_t guid; | |
2910 | vdev_stat_t *vs; | |
2911 | uint_t vsc; | |
2912 | uint64_t pool; | |
2913 | ||
9babb374 BB |
2914 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
2915 | ||
34dc7c2f BB |
2916 | if (spa->spa_spares.sav_count == 0) |
2917 | return; | |
2918 | ||
2919 | VERIFY(nvlist_lookup_nvlist(config, | |
2920 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
2921 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, | |
2922 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
2923 | if (nspares != 0) { | |
2924 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
2925 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
2926 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
2927 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0); | |
2928 | ||
2929 | /* | |
2930 | * Go through and find any spares which have since been | |
2931 | * repurposed as an active spare. If this is the case, update | |
2932 | * their status appropriately. | |
2933 | */ | |
2934 | for (i = 0; i < nspares; i++) { | |
2935 | VERIFY(nvlist_lookup_uint64(spares[i], | |
2936 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
b128c09f BB |
2937 | if (spa_spare_exists(guid, &pool, NULL) && |
2938 | pool != 0ULL) { | |
34dc7c2f | 2939 | VERIFY(nvlist_lookup_uint64_array( |
428870ff | 2940 | spares[i], ZPOOL_CONFIG_VDEV_STATS, |
34dc7c2f BB |
2941 | (uint64_t **)&vs, &vsc) == 0); |
2942 | vs->vs_state = VDEV_STATE_CANT_OPEN; | |
2943 | vs->vs_aux = VDEV_AUX_SPARED; | |
2944 | } | |
2945 | } | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | /* | |
2950 | * Add l2cache device information to the nvlist, including vdev stats. | |
2951 | */ | |
2952 | static void | |
2953 | spa_add_l2cache(spa_t *spa, nvlist_t *config) | |
2954 | { | |
2955 | nvlist_t **l2cache; | |
2956 | uint_t i, j, nl2cache; | |
2957 | nvlist_t *nvroot; | |
2958 | uint64_t guid; | |
2959 | vdev_t *vd; | |
2960 | vdev_stat_t *vs; | |
2961 | uint_t vsc; | |
2962 | ||
9babb374 BB |
2963 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); |
2964 | ||
34dc7c2f BB |
2965 | if (spa->spa_l2cache.sav_count == 0) |
2966 | return; | |
2967 | ||
34dc7c2f BB |
2968 | VERIFY(nvlist_lookup_nvlist(config, |
2969 | ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0); | |
2970 | VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, | |
2971 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
2972 | if (nl2cache != 0) { | |
2973 | VERIFY(nvlist_add_nvlist_array(nvroot, | |
2974 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
2975 | VERIFY(nvlist_lookup_nvlist_array(nvroot, | |
2976 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0); | |
2977 | ||
2978 | /* | |
2979 | * Update level 2 cache device stats. | |
2980 | */ | |
2981 | ||
2982 | for (i = 0; i < nl2cache; i++) { | |
2983 | VERIFY(nvlist_lookup_uint64(l2cache[i], | |
2984 | ZPOOL_CONFIG_GUID, &guid) == 0); | |
2985 | ||
2986 | vd = NULL; | |
2987 | for (j = 0; j < spa->spa_l2cache.sav_count; j++) { | |
2988 | if (guid == | |
2989 | spa->spa_l2cache.sav_vdevs[j]->vdev_guid) { | |
2990 | vd = spa->spa_l2cache.sav_vdevs[j]; | |
2991 | break; | |
2992 | } | |
2993 | } | |
2994 | ASSERT(vd != NULL); | |
2995 | ||
2996 | VERIFY(nvlist_lookup_uint64_array(l2cache[i], | |
428870ff BB |
2997 | ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc) |
2998 | == 0); | |
34dc7c2f BB |
2999 | vdev_get_stats(vd, vs); |
3000 | } | |
3001 | } | |
34dc7c2f BB |
3002 | } |
3003 | ||
9ae529ec CS |
3004 | static void |
3005 | spa_add_feature_stats(spa_t *spa, nvlist_t *config) | |
3006 | { | |
3007 | nvlist_t *features; | |
3008 | zap_cursor_t zc; | |
3009 | zap_attribute_t za; | |
3010 | ||
3011 | ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER)); | |
3012 | VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
3013 | ||
3014 | if (spa->spa_feat_for_read_obj != 0) { | |
3015 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
3016 | spa->spa_feat_for_read_obj); | |
3017 | zap_cursor_retrieve(&zc, &za) == 0; | |
3018 | zap_cursor_advance(&zc)) { | |
3019 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
3020 | za.za_num_integers == 1); | |
3021 | VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, | |
3022 | za.za_first_integer)); | |
3023 | } | |
3024 | zap_cursor_fini(&zc); | |
3025 | } | |
3026 | ||
3027 | if (spa->spa_feat_for_write_obj != 0) { | |
3028 | for (zap_cursor_init(&zc, spa->spa_meta_objset, | |
3029 | spa->spa_feat_for_write_obj); | |
3030 | zap_cursor_retrieve(&zc, &za) == 0; | |
3031 | zap_cursor_advance(&zc)) { | |
3032 | ASSERT(za.za_integer_length == sizeof (uint64_t) && | |
3033 | za.za_num_integers == 1); | |
3034 | VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name, | |
3035 | za.za_first_integer)); | |
3036 | } | |
3037 | zap_cursor_fini(&zc); | |
3038 | } | |
3039 | ||
3040 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS, | |
3041 | features) == 0); | |
3042 | nvlist_free(features); | |
3043 | } | |
3044 | ||
34dc7c2f | 3045 | int |
9ae529ec CS |
3046 | spa_get_stats(const char *name, nvlist_t **config, |
3047 | char *altroot, size_t buflen) | |
34dc7c2f BB |
3048 | { |
3049 | int error; | |
3050 | spa_t *spa; | |
3051 | ||
3052 | *config = NULL; | |
428870ff | 3053 | error = spa_open_common(name, &spa, FTAG, NULL, config); |
34dc7c2f | 3054 | |
9babb374 BB |
3055 | if (spa != NULL) { |
3056 | /* | |
3057 | * This still leaves a window of inconsistency where the spares | |
3058 | * or l2cache devices could change and the config would be | |
3059 | * self-inconsistent. | |
3060 | */ | |
3061 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
34dc7c2f | 3062 | |
9babb374 | 3063 | if (*config != NULL) { |
572e2857 BB |
3064 | uint64_t loadtimes[2]; |
3065 | ||
3066 | loadtimes[0] = spa->spa_loaded_ts.tv_sec; | |
3067 | loadtimes[1] = spa->spa_loaded_ts.tv_nsec; | |
3068 | VERIFY(nvlist_add_uint64_array(*config, | |
3069 | ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0); | |
3070 | ||
b128c09f | 3071 | VERIFY(nvlist_add_uint64(*config, |
9babb374 BB |
3072 | ZPOOL_CONFIG_ERRCOUNT, |
3073 | spa_get_errlog_size(spa)) == 0); | |
3074 | ||
3075 | if (spa_suspended(spa)) | |
3076 | VERIFY(nvlist_add_uint64(*config, | |
3077 | ZPOOL_CONFIG_SUSPENDED, | |
3078 | spa->spa_failmode) == 0); | |
b128c09f | 3079 | |
9babb374 BB |
3080 | spa_add_spares(spa, *config); |
3081 | spa_add_l2cache(spa, *config); | |
9ae529ec | 3082 | spa_add_feature_stats(spa, *config); |
9babb374 | 3083 | } |
34dc7c2f BB |
3084 | } |
3085 | ||
3086 | /* | |
3087 | * We want to get the alternate root even for faulted pools, so we cheat | |
3088 | * and call spa_lookup() directly. | |
3089 | */ | |
3090 | if (altroot) { | |
3091 | if (spa == NULL) { | |
3092 | mutex_enter(&spa_namespace_lock); | |
3093 | spa = spa_lookup(name); | |
3094 | if (spa) | |
3095 | spa_altroot(spa, altroot, buflen); | |
3096 | else | |
3097 | altroot[0] = '\0'; | |
3098 | spa = NULL; | |
3099 | mutex_exit(&spa_namespace_lock); | |
3100 | } else { | |
3101 | spa_altroot(spa, altroot, buflen); | |
3102 | } | |
3103 | } | |
3104 | ||
9babb374 BB |
3105 | if (spa != NULL) { |
3106 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
34dc7c2f | 3107 | spa_close(spa, FTAG); |
9babb374 | 3108 | } |
34dc7c2f BB |
3109 | |
3110 | return (error); | |
3111 | } | |
3112 | ||
3113 | /* | |
3114 | * Validate that the auxiliary device array is well formed. We must have an | |
3115 | * array of nvlists, each which describes a valid leaf vdev. If this is an | |
3116 | * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be | |
3117 | * specified, as long as they are well-formed. | |
3118 | */ | |
3119 | static int | |
3120 | spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode, | |
3121 | spa_aux_vdev_t *sav, const char *config, uint64_t version, | |
3122 | vdev_labeltype_t label) | |
3123 | { | |
3124 | nvlist_t **dev; | |
3125 | uint_t i, ndev; | |
3126 | vdev_t *vd; | |
3127 | int error; | |
3128 | ||
b128c09f BB |
3129 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
3130 | ||
34dc7c2f BB |
3131 | /* |
3132 | * It's acceptable to have no devs specified. | |
3133 | */ | |
3134 | if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0) | |
3135 | return (0); | |
3136 | ||
3137 | if (ndev == 0) | |
3138 | return (EINVAL); | |
3139 | ||
3140 | /* | |
3141 | * Make sure the pool is formatted with a version that supports this | |
3142 | * device type. | |
3143 | */ | |
3144 | if (spa_version(spa) < version) | |
3145 | return (ENOTSUP); | |
3146 | ||
3147 | /* | |
3148 | * Set the pending device list so we correctly handle device in-use | |
3149 | * checking. | |
3150 | */ | |
3151 | sav->sav_pending = dev; | |
3152 | sav->sav_npending = ndev; | |
3153 | ||
3154 | for (i = 0; i < ndev; i++) { | |
3155 | if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0, | |
3156 | mode)) != 0) | |
3157 | goto out; | |
3158 | ||
3159 | if (!vd->vdev_ops->vdev_op_leaf) { | |
3160 | vdev_free(vd); | |
3161 | error = EINVAL; | |
3162 | goto out; | |
3163 | } | |
3164 | ||
3165 | /* | |
b128c09f BB |
3166 | * The L2ARC currently only supports disk devices in |
3167 | * kernel context. For user-level testing, we allow it. | |
34dc7c2f | 3168 | */ |
b128c09f | 3169 | #ifdef _KERNEL |
34dc7c2f BB |
3170 | if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) && |
3171 | strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) { | |
3172 | error = ENOTBLK; | |
5ffb9d1d | 3173 | vdev_free(vd); |
34dc7c2f BB |
3174 | goto out; |
3175 | } | |
b128c09f | 3176 | #endif |
34dc7c2f BB |
3177 | vd->vdev_top = vd; |
3178 | ||
3179 | if ((error = vdev_open(vd)) == 0 && | |
3180 | (error = vdev_label_init(vd, crtxg, label)) == 0) { | |
3181 | VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID, | |
3182 | vd->vdev_guid) == 0); | |
3183 | } | |
3184 | ||
3185 | vdev_free(vd); | |
3186 | ||
3187 | if (error && | |
3188 | (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE)) | |
3189 | goto out; | |
3190 | else | |
3191 | error = 0; | |
3192 | } | |
3193 | ||
3194 | out: | |
3195 | sav->sav_pending = NULL; | |
3196 | sav->sav_npending = 0; | |
3197 | return (error); | |
3198 | } | |
3199 | ||
3200 | static int | |
3201 | spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode) | |
3202 | { | |
3203 | int error; | |
3204 | ||
b128c09f BB |
3205 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); |
3206 | ||
34dc7c2f BB |
3207 | if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode, |
3208 | &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES, | |
3209 | VDEV_LABEL_SPARE)) != 0) { | |
3210 | return (error); | |
3211 | } | |
3212 | ||
3213 | return (spa_validate_aux_devs(spa, nvroot, crtxg, mode, | |
3214 | &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE, | |
3215 | VDEV_LABEL_L2CACHE)); | |
3216 | } | |
3217 | ||
3218 | static void | |
3219 | spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs, | |
3220 | const char *config) | |
3221 | { | |
3222 | int i; | |
3223 | ||
3224 | if (sav->sav_config != NULL) { | |
3225 | nvlist_t **olddevs; | |
3226 | uint_t oldndevs; | |
3227 | nvlist_t **newdevs; | |
3228 | ||
3229 | /* | |
3230 | * Generate new dev list by concatentating with the | |
3231 | * current dev list. | |
3232 | */ | |
3233 | VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config, | |
3234 | &olddevs, &oldndevs) == 0); | |
3235 | ||
3236 | newdevs = kmem_alloc(sizeof (void *) * | |
b8d06fca | 3237 | (ndevs + oldndevs), KM_PUSHPAGE); |
34dc7c2f BB |
3238 | for (i = 0; i < oldndevs; i++) |
3239 | VERIFY(nvlist_dup(olddevs[i], &newdevs[i], | |
b8d06fca | 3240 | KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3241 | for (i = 0; i < ndevs; i++) |
3242 | VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs], | |
b8d06fca | 3243 | KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3244 | |
3245 | VERIFY(nvlist_remove(sav->sav_config, config, | |
3246 | DATA_TYPE_NVLIST_ARRAY) == 0); | |
3247 | ||
3248 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, | |
3249 | config, newdevs, ndevs + oldndevs) == 0); | |
3250 | for (i = 0; i < oldndevs + ndevs; i++) | |
3251 | nvlist_free(newdevs[i]); | |
3252 | kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *)); | |
3253 | } else { | |
3254 | /* | |
3255 | * Generate a new dev list. | |
3256 | */ | |
3257 | VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME, | |
b8d06fca | 3258 | KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3259 | VERIFY(nvlist_add_nvlist_array(sav->sav_config, config, |
3260 | devs, ndevs) == 0); | |
3261 | } | |
3262 | } | |
3263 | ||
3264 | /* | |
3265 | * Stop and drop level 2 ARC devices | |
3266 | */ | |
3267 | void | |
3268 | spa_l2cache_drop(spa_t *spa) | |
3269 | { | |
3270 | vdev_t *vd; | |
3271 | int i; | |
3272 | spa_aux_vdev_t *sav = &spa->spa_l2cache; | |
3273 | ||
3274 | for (i = 0; i < sav->sav_count; i++) { | |
3275 | uint64_t pool; | |
3276 | ||
3277 | vd = sav->sav_vdevs[i]; | |
3278 | ASSERT(vd != NULL); | |
3279 | ||
fb5f0bc8 BB |
3280 | if (spa_l2cache_exists(vd->vdev_guid, &pool) && |
3281 | pool != 0ULL && l2arc_vdev_present(vd)) | |
34dc7c2f | 3282 | l2arc_remove_vdev(vd); |
34dc7c2f BB |
3283 | } |
3284 | } | |
3285 | ||
3286 | /* | |
3287 | * Pool Creation | |
3288 | */ | |
3289 | int | |
3290 | spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props, | |
b128c09f | 3291 | const char *history_str, nvlist_t *zplprops) |
34dc7c2f BB |
3292 | { |
3293 | spa_t *spa; | |
3294 | char *altroot = NULL; | |
3295 | vdev_t *rvd; | |
3296 | dsl_pool_t *dp; | |
3297 | dmu_tx_t *tx; | |
9babb374 | 3298 | int error = 0; |
34dc7c2f BB |
3299 | uint64_t txg = TXG_INITIAL; |
3300 | nvlist_t **spares, **l2cache; | |
3301 | uint_t nspares, nl2cache; | |
428870ff | 3302 | uint64_t version, obj; |
9ae529ec CS |
3303 | boolean_t has_features; |
3304 | nvpair_t *elem; | |
d6320ddb | 3305 | int c; |
34dc7c2f BB |
3306 | |
3307 | /* | |
3308 | * If this pool already exists, return failure. | |
3309 | */ | |
3310 | mutex_enter(&spa_namespace_lock); | |
3311 | if (spa_lookup(pool) != NULL) { | |
3312 | mutex_exit(&spa_namespace_lock); | |
3313 | return (EEXIST); | |
3314 | } | |
3315 | ||
3316 | /* | |
3317 | * Allocate a new spa_t structure. | |
3318 | */ | |
3319 | (void) nvlist_lookup_string(props, | |
3320 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
428870ff | 3321 | spa = spa_add(pool, NULL, altroot); |
fb5f0bc8 | 3322 | spa_activate(spa, spa_mode_global); |
34dc7c2f | 3323 | |
34dc7c2f | 3324 | if (props && (error = spa_prop_validate(spa, props))) { |
34dc7c2f BB |
3325 | spa_deactivate(spa); |
3326 | spa_remove(spa); | |
b128c09f | 3327 | mutex_exit(&spa_namespace_lock); |
34dc7c2f BB |
3328 | return (error); |
3329 | } | |
3330 | ||
9ae529ec CS |
3331 | has_features = B_FALSE; |
3332 | for (elem = nvlist_next_nvpair(props, NULL); | |
3333 | elem != NULL; elem = nvlist_next_nvpair(props, elem)) { | |
3334 | if (zpool_prop_feature(nvpair_name(elem))) | |
3335 | has_features = B_TRUE; | |
3336 | } | |
3337 | ||
3338 | if (has_features || nvlist_lookup_uint64(props, | |
3339 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) { | |
34dc7c2f | 3340 | version = SPA_VERSION; |
9ae529ec CS |
3341 | } |
3342 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); | |
428870ff BB |
3343 | |
3344 | spa->spa_first_txg = txg; | |
3345 | spa->spa_uberblock.ub_txg = txg - 1; | |
34dc7c2f BB |
3346 | spa->spa_uberblock.ub_version = version; |
3347 | spa->spa_ubsync = spa->spa_uberblock; | |
3348 | ||
9babb374 BB |
3349 | /* |
3350 | * Create "The Godfather" zio to hold all async IOs | |
3351 | */ | |
3352 | spa->spa_async_zio_root = zio_root(spa, NULL, NULL, | |
3353 | ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER); | |
3354 | ||
34dc7c2f BB |
3355 | /* |
3356 | * Create the root vdev. | |
3357 | */ | |
b128c09f | 3358 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
3359 | |
3360 | error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD); | |
3361 | ||
3362 | ASSERT(error != 0 || rvd != NULL); | |
3363 | ASSERT(error != 0 || spa->spa_root_vdev == rvd); | |
3364 | ||
3365 | if (error == 0 && !zfs_allocatable_devs(nvroot)) | |
3366 | error = EINVAL; | |
3367 | ||
3368 | if (error == 0 && | |
3369 | (error = vdev_create(rvd, txg, B_FALSE)) == 0 && | |
3370 | (error = spa_validate_aux(spa, nvroot, txg, | |
3371 | VDEV_ALLOC_ADD)) == 0) { | |
d6320ddb | 3372 | for (c = 0; c < rvd->vdev_children; c++) { |
9babb374 BB |
3373 | vdev_metaslab_set_size(rvd->vdev_child[c]); |
3374 | vdev_expand(rvd->vdev_child[c], txg); | |
3375 | } | |
34dc7c2f BB |
3376 | } |
3377 | ||
b128c09f | 3378 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
3379 | |
3380 | if (error != 0) { | |
3381 | spa_unload(spa); | |
3382 | spa_deactivate(spa); | |
3383 | spa_remove(spa); | |
3384 | mutex_exit(&spa_namespace_lock); | |
3385 | return (error); | |
3386 | } | |
3387 | ||
3388 | /* | |
3389 | * Get the list of spares, if specified. | |
3390 | */ | |
3391 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
3392 | &spares, &nspares) == 0) { | |
3393 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME, | |
b8d06fca | 3394 | KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3395 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
3396 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 3397 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3398 | spa_load_spares(spa); |
b128c09f | 3399 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
3400 | spa->spa_spares.sav_sync = B_TRUE; |
3401 | } | |
3402 | ||
3403 | /* | |
3404 | * Get the list of level 2 cache devices, if specified. | |
3405 | */ | |
3406 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
3407 | &l2cache, &nl2cache) == 0) { | |
3408 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
b8d06fca | 3409 | NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3410 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
3411 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 3412 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3413 | spa_load_l2cache(spa); |
b128c09f | 3414 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
3415 | spa->spa_l2cache.sav_sync = B_TRUE; |
3416 | } | |
3417 | ||
9ae529ec | 3418 | spa->spa_is_initializing = B_TRUE; |
b128c09f | 3419 | spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg); |
34dc7c2f | 3420 | spa->spa_meta_objset = dp->dp_meta_objset; |
9ae529ec | 3421 | spa->spa_is_initializing = B_FALSE; |
34dc7c2f | 3422 | |
428870ff BB |
3423 | /* |
3424 | * Create DDTs (dedup tables). | |
3425 | */ | |
3426 | ddt_create(spa); | |
3427 | ||
3428 | spa_update_dspace(spa); | |
3429 | ||
34dc7c2f BB |
3430 | tx = dmu_tx_create_assigned(dp, txg); |
3431 | ||
3432 | /* | |
3433 | * Create the pool config object. | |
3434 | */ | |
3435 | spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset, | |
b128c09f | 3436 | DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE, |
34dc7c2f BB |
3437 | DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx); |
3438 | ||
3439 | if (zap_add(spa->spa_meta_objset, | |
3440 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG, | |
3441 | sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) { | |
3442 | cmn_err(CE_PANIC, "failed to add pool config"); | |
3443 | } | |
3444 | ||
9ae529ec CS |
3445 | if (spa_version(spa) >= SPA_VERSION_FEATURES) |
3446 | spa_feature_create_zap_objects(spa, tx); | |
3447 | ||
428870ff BB |
3448 | if (zap_add(spa->spa_meta_objset, |
3449 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION, | |
3450 | sizeof (uint64_t), 1, &version, tx) != 0) { | |
3451 | cmn_err(CE_PANIC, "failed to add pool version"); | |
3452 | } | |
3453 | ||
34dc7c2f BB |
3454 | /* Newly created pools with the right version are always deflated. */ |
3455 | if (version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
3456 | spa->spa_deflate = TRUE; | |
3457 | if (zap_add(spa->spa_meta_objset, | |
3458 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
3459 | sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) { | |
3460 | cmn_err(CE_PANIC, "failed to add deflate"); | |
3461 | } | |
3462 | } | |
3463 | ||
3464 | /* | |
428870ff | 3465 | * Create the deferred-free bpobj. Turn off compression |
34dc7c2f BB |
3466 | * because sync-to-convergence takes longer if the blocksize |
3467 | * keeps changing. | |
3468 | */ | |
428870ff BB |
3469 | obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx); |
3470 | dmu_object_set_compress(spa->spa_meta_objset, obj, | |
34dc7c2f | 3471 | ZIO_COMPRESS_OFF, tx); |
34dc7c2f | 3472 | if (zap_add(spa->spa_meta_objset, |
428870ff BB |
3473 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ, |
3474 | sizeof (uint64_t), 1, &obj, tx) != 0) { | |
3475 | cmn_err(CE_PANIC, "failed to add bpobj"); | |
34dc7c2f | 3476 | } |
428870ff BB |
3477 | VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj, |
3478 | spa->spa_meta_objset, obj)); | |
34dc7c2f BB |
3479 | |
3480 | /* | |
3481 | * Create the pool's history object. | |
3482 | */ | |
3483 | if (version >= SPA_VERSION_ZPOOL_HISTORY) | |
3484 | spa_history_create_obj(spa, tx); | |
3485 | ||
3486 | /* | |
3487 | * Set pool properties. | |
3488 | */ | |
3489 | spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS); | |
3490 | spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION); | |
3491 | spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE); | |
9babb374 | 3492 | spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND); |
428870ff | 3493 | |
d164b209 BB |
3494 | if (props != NULL) { |
3495 | spa_configfile_set(spa, props, B_FALSE); | |
428870ff | 3496 | spa_sync_props(spa, props, tx); |
d164b209 | 3497 | } |
34dc7c2f BB |
3498 | |
3499 | dmu_tx_commit(tx); | |
3500 | ||
3501 | spa->spa_sync_on = B_TRUE; | |
3502 | txg_sync_start(spa->spa_dsl_pool); | |
3503 | ||
3504 | /* | |
3505 | * We explicitly wait for the first transaction to complete so that our | |
3506 | * bean counters are appropriately updated. | |
3507 | */ | |
3508 | txg_wait_synced(spa->spa_dsl_pool, txg); | |
3509 | ||
b128c09f | 3510 | spa_config_sync(spa, B_FALSE, B_TRUE); |
34dc7c2f BB |
3511 | |
3512 | if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL) | |
3513 | (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE); | |
45d1cae3 | 3514 | spa_history_log_version(spa, LOG_POOL_CREATE); |
34dc7c2f | 3515 | |
b128c09f BB |
3516 | spa->spa_minref = refcount_count(&spa->spa_refcount); |
3517 | ||
d164b209 BB |
3518 | mutex_exit(&spa_namespace_lock); |
3519 | ||
34dc7c2f BB |
3520 | return (0); |
3521 | } | |
3522 | ||
9babb374 | 3523 | #ifdef _KERNEL |
34dc7c2f | 3524 | /* |
9babb374 BB |
3525 | * Get the root pool information from the root disk, then import the root pool |
3526 | * during the system boot up time. | |
34dc7c2f | 3527 | */ |
9babb374 BB |
3528 | extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **); |
3529 | ||
3530 | static nvlist_t * | |
3531 | spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid) | |
3532 | { | |
3533 | nvlist_t *config; | |
3534 | nvlist_t *nvtop, *nvroot; | |
3535 | uint64_t pgid; | |
3536 | ||
3537 | if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0) | |
3538 | return (NULL); | |
3539 | ||
3540 | /* | |
3541 | * Add this top-level vdev to the child array. | |
3542 | */ | |
3543 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
3544 | &nvtop) == 0); | |
3545 | VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
3546 | &pgid) == 0); | |
3547 | VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0); | |
3548 | ||
3549 | /* | |
3550 | * Put this pool's top-level vdevs into a root vdev. | |
3551 | */ | |
b8d06fca | 3552 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
9babb374 BB |
3553 | VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, |
3554 | VDEV_TYPE_ROOT) == 0); | |
3555 | VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0); | |
3556 | VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0); | |
3557 | VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, | |
3558 | &nvtop, 1) == 0); | |
3559 | ||
3560 | /* | |
3561 | * Replace the existing vdev_tree with the new root vdev in | |
3562 | * this pool's configuration (remove the old, add the new). | |
3563 | */ | |
3564 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0); | |
3565 | nvlist_free(nvroot); | |
3566 | return (config); | |
3567 | } | |
3568 | ||
3569 | /* | |
3570 | * Walk the vdev tree and see if we can find a device with "better" | |
3571 | * configuration. A configuration is "better" if the label on that | |
3572 | * device has a more recent txg. | |
3573 | */ | |
3574 | static void | |
3575 | spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg) | |
3576 | { | |
d6320ddb BB |
3577 | int c; |
3578 | ||
3579 | for (c = 0; c < vd->vdev_children; c++) | |
9babb374 BB |
3580 | spa_alt_rootvdev(vd->vdev_child[c], avd, txg); |
3581 | ||
3582 | if (vd->vdev_ops->vdev_op_leaf) { | |
3583 | nvlist_t *label; | |
3584 | uint64_t label_txg; | |
3585 | ||
3586 | if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid, | |
3587 | &label) != 0) | |
3588 | return; | |
3589 | ||
3590 | VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG, | |
3591 | &label_txg) == 0); | |
3592 | ||
3593 | /* | |
3594 | * Do we have a better boot device? | |
3595 | */ | |
3596 | if (label_txg > *txg) { | |
3597 | *txg = label_txg; | |
3598 | *avd = vd; | |
3599 | } | |
3600 | nvlist_free(label); | |
3601 | } | |
3602 | } | |
3603 | ||
3604 | /* | |
3605 | * Import a root pool. | |
3606 | * | |
3607 | * For x86. devpath_list will consist of devid and/or physpath name of | |
3608 | * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a"). | |
3609 | * The GRUB "findroot" command will return the vdev we should boot. | |
3610 | * | |
3611 | * For Sparc, devpath_list consists the physpath name of the booting device | |
3612 | * no matter the rootpool is a single device pool or a mirrored pool. | |
3613 | * e.g. | |
3614 | * "/pci@1f,0/ide@d/disk@0,0:a" | |
3615 | */ | |
3616 | int | |
3617 | spa_import_rootpool(char *devpath, char *devid) | |
3618 | { | |
3619 | spa_t *spa; | |
3620 | vdev_t *rvd, *bvd, *avd = NULL; | |
3621 | nvlist_t *config, *nvtop; | |
3622 | uint64_t guid, txg; | |
3623 | char *pname; | |
3624 | int error; | |
3625 | ||
3626 | /* | |
3627 | * Read the label from the boot device and generate a configuration. | |
3628 | */ | |
428870ff BB |
3629 | config = spa_generate_rootconf(devpath, devid, &guid); |
3630 | #if defined(_OBP) && defined(_KERNEL) | |
3631 | if (config == NULL) { | |
3632 | if (strstr(devpath, "/iscsi/ssd") != NULL) { | |
3633 | /* iscsi boot */ | |
3634 | get_iscsi_bootpath_phy(devpath); | |
3635 | config = spa_generate_rootconf(devpath, devid, &guid); | |
3636 | } | |
3637 | } | |
3638 | #endif | |
3639 | if (config == NULL) { | |
9ae529ec | 3640 | cmn_err(CE_NOTE, "Cannot read the pool label from '%s'", |
9babb374 BB |
3641 | devpath); |
3642 | return (EIO); | |
3643 | } | |
3644 | ||
3645 | VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, | |
3646 | &pname) == 0); | |
3647 | VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0); | |
3648 | ||
3649 | mutex_enter(&spa_namespace_lock); | |
3650 | if ((spa = spa_lookup(pname)) != NULL) { | |
3651 | /* | |
3652 | * Remove the existing root pool from the namespace so that we | |
3653 | * can replace it with the correct config we just read in. | |
3654 | */ | |
3655 | spa_remove(spa); | |
3656 | } | |
3657 | ||
428870ff | 3658 | spa = spa_add(pname, config, NULL); |
9babb374 | 3659 | spa->spa_is_root = B_TRUE; |
572e2857 | 3660 | spa->spa_import_flags = ZFS_IMPORT_VERBATIM; |
9babb374 BB |
3661 | |
3662 | /* | |
3663 | * Build up a vdev tree based on the boot device's label config. | |
3664 | */ | |
3665 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
3666 | &nvtop) == 0); | |
3667 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3668 | error = spa_config_parse(spa, &rvd, nvtop, NULL, 0, | |
3669 | VDEV_ALLOC_ROOTPOOL); | |
3670 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3671 | if (error) { | |
3672 | mutex_exit(&spa_namespace_lock); | |
3673 | nvlist_free(config); | |
3674 | cmn_err(CE_NOTE, "Can not parse the config for pool '%s'", | |
3675 | pname); | |
3676 | return (error); | |
3677 | } | |
3678 | ||
3679 | /* | |
3680 | * Get the boot vdev. | |
3681 | */ | |
3682 | if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) { | |
3683 | cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu", | |
3684 | (u_longlong_t)guid); | |
3685 | error = ENOENT; | |
3686 | goto out; | |
3687 | } | |
3688 | ||
3689 | /* | |
3690 | * Determine if there is a better boot device. | |
3691 | */ | |
3692 | avd = bvd; | |
3693 | spa_alt_rootvdev(rvd, &avd, &txg); | |
3694 | if (avd != bvd) { | |
3695 | cmn_err(CE_NOTE, "The boot device is 'degraded'. Please " | |
3696 | "try booting from '%s'", avd->vdev_path); | |
3697 | error = EINVAL; | |
3698 | goto out; | |
3699 | } | |
3700 | ||
3701 | /* | |
3702 | * If the boot device is part of a spare vdev then ensure that | |
3703 | * we're booting off the active spare. | |
3704 | */ | |
3705 | if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops && | |
3706 | !bvd->vdev_isspare) { | |
3707 | cmn_err(CE_NOTE, "The boot device is currently spared. Please " | |
3708 | "try booting from '%s'", | |
572e2857 BB |
3709 | bvd->vdev_parent-> |
3710 | vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path); | |
9babb374 BB |
3711 | error = EINVAL; |
3712 | goto out; | |
3713 | } | |
3714 | ||
9babb374 | 3715 | error = 0; |
45d1cae3 | 3716 | spa_history_log_version(spa, LOG_POOL_IMPORT); |
9babb374 BB |
3717 | out: |
3718 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); | |
3719 | vdev_free(rvd); | |
3720 | spa_config_exit(spa, SCL_ALL, FTAG); | |
3721 | mutex_exit(&spa_namespace_lock); | |
3722 | ||
3723 | nvlist_free(config); | |
3724 | return (error); | |
3725 | } | |
3726 | ||
3727 | #endif | |
3728 | ||
9babb374 BB |
3729 | /* |
3730 | * Import a non-root pool into the system. | |
3731 | */ | |
3732 | int | |
572e2857 | 3733 | spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags) |
34dc7c2f BB |
3734 | { |
3735 | spa_t *spa; | |
3736 | char *altroot = NULL; | |
428870ff BB |
3737 | spa_load_state_t state = SPA_LOAD_IMPORT; |
3738 | zpool_rewind_policy_t policy; | |
572e2857 BB |
3739 | uint64_t mode = spa_mode_global; |
3740 | uint64_t readonly = B_FALSE; | |
9babb374 | 3741 | int error; |
34dc7c2f BB |
3742 | nvlist_t *nvroot; |
3743 | nvlist_t **spares, **l2cache; | |
3744 | uint_t nspares, nl2cache; | |
34dc7c2f BB |
3745 | |
3746 | /* | |
3747 | * If a pool with this name exists, return failure. | |
3748 | */ | |
3749 | mutex_enter(&spa_namespace_lock); | |
428870ff | 3750 | if (spa_lookup(pool) != NULL) { |
9babb374 BB |
3751 | mutex_exit(&spa_namespace_lock); |
3752 | return (EEXIST); | |
34dc7c2f BB |
3753 | } |
3754 | ||
3755 | /* | |
3756 | * Create and initialize the spa structure. | |
3757 | */ | |
3758 | (void) nvlist_lookup_string(props, | |
3759 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
572e2857 BB |
3760 | (void) nvlist_lookup_uint64(props, |
3761 | zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly); | |
3762 | if (readonly) | |
3763 | mode = FREAD; | |
428870ff | 3764 | spa = spa_add(pool, config, altroot); |
572e2857 BB |
3765 | spa->spa_import_flags = flags; |
3766 | ||
3767 | /* | |
3768 | * Verbatim import - Take a pool and insert it into the namespace | |
3769 | * as if it had been loaded at boot. | |
3770 | */ | |
3771 | if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) { | |
3772 | if (props != NULL) | |
3773 | spa_configfile_set(spa, props, B_FALSE); | |
3774 | ||
3775 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
3776 | ||
3777 | mutex_exit(&spa_namespace_lock); | |
3778 | spa_history_log_version(spa, LOG_POOL_IMPORT); | |
3779 | ||
3780 | return (0); | |
3781 | } | |
3782 | ||
3783 | spa_activate(spa, mode); | |
34dc7c2f | 3784 | |
9babb374 BB |
3785 | /* |
3786 | * Don't start async tasks until we know everything is healthy. | |
3787 | */ | |
3788 | spa_async_suspend(spa); | |
b128c09f | 3789 | |
572e2857 BB |
3790 | zpool_get_rewind_policy(config, &policy); |
3791 | if (policy.zrp_request & ZPOOL_DO_REWIND) | |
3792 | state = SPA_LOAD_RECOVER; | |
3793 | ||
34dc7c2f | 3794 | /* |
9babb374 BB |
3795 | * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig |
3796 | * because the user-supplied config is actually the one to trust when | |
b128c09f | 3797 | * doing an import. |
34dc7c2f | 3798 | */ |
428870ff BB |
3799 | if (state != SPA_LOAD_RECOVER) |
3800 | spa->spa_last_ubsync_txg = spa->spa_load_txg = 0; | |
572e2857 | 3801 | |
428870ff BB |
3802 | error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg, |
3803 | policy.zrp_request); | |
3804 | ||
3805 | /* | |
572e2857 BB |
3806 | * Propagate anything learned while loading the pool and pass it |
3807 | * back to caller (i.e. rewind info, missing devices, etc). | |
428870ff | 3808 | */ |
572e2857 BB |
3809 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
3810 | spa->spa_load_info) == 0); | |
34dc7c2f | 3811 | |
b128c09f | 3812 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3813 | /* |
9babb374 BB |
3814 | * Toss any existing sparelist, as it doesn't have any validity |
3815 | * anymore, and conflicts with spa_has_spare(). | |
34dc7c2f | 3816 | */ |
9babb374 | 3817 | if (spa->spa_spares.sav_config) { |
34dc7c2f BB |
3818 | nvlist_free(spa->spa_spares.sav_config); |
3819 | spa->spa_spares.sav_config = NULL; | |
3820 | spa_load_spares(spa); | |
3821 | } | |
9babb374 | 3822 | if (spa->spa_l2cache.sav_config) { |
34dc7c2f BB |
3823 | nvlist_free(spa->spa_l2cache.sav_config); |
3824 | spa->spa_l2cache.sav_config = NULL; | |
3825 | spa_load_l2cache(spa); | |
3826 | } | |
3827 | ||
3828 | VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, | |
3829 | &nvroot) == 0); | |
3830 | if (error == 0) | |
9babb374 BB |
3831 | error = spa_validate_aux(spa, nvroot, -1ULL, |
3832 | VDEV_ALLOC_SPARE); | |
34dc7c2f BB |
3833 | if (error == 0) |
3834 | error = spa_validate_aux(spa, nvroot, -1ULL, | |
3835 | VDEV_ALLOC_L2CACHE); | |
b128c09f | 3836 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f | 3837 | |
d164b209 BB |
3838 | if (props != NULL) |
3839 | spa_configfile_set(spa, props, B_FALSE); | |
3840 | ||
fb5f0bc8 BB |
3841 | if (error != 0 || (props && spa_writeable(spa) && |
3842 | (error = spa_prop_set(spa, props)))) { | |
9babb374 BB |
3843 | spa_unload(spa); |
3844 | spa_deactivate(spa); | |
3845 | spa_remove(spa); | |
34dc7c2f BB |
3846 | mutex_exit(&spa_namespace_lock); |
3847 | return (error); | |
3848 | } | |
3849 | ||
572e2857 BB |
3850 | spa_async_resume(spa); |
3851 | ||
34dc7c2f BB |
3852 | /* |
3853 | * Override any spares and level 2 cache devices as specified by | |
3854 | * the user, as these may have correct device names/devids, etc. | |
3855 | */ | |
3856 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, | |
3857 | &spares, &nspares) == 0) { | |
3858 | if (spa->spa_spares.sav_config) | |
3859 | VERIFY(nvlist_remove(spa->spa_spares.sav_config, | |
3860 | ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0); | |
3861 | else | |
3862 | VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, | |
b8d06fca | 3863 | NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3864 | VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config, |
3865 | ZPOOL_CONFIG_SPARES, spares, nspares) == 0); | |
b128c09f | 3866 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3867 | spa_load_spares(spa); |
b128c09f | 3868 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
3869 | spa->spa_spares.sav_sync = B_TRUE; |
3870 | } | |
3871 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, | |
3872 | &l2cache, &nl2cache) == 0) { | |
3873 | if (spa->spa_l2cache.sav_config) | |
3874 | VERIFY(nvlist_remove(spa->spa_l2cache.sav_config, | |
3875 | ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0); | |
3876 | else | |
3877 | VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config, | |
b8d06fca | 3878 | NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
34dc7c2f BB |
3879 | VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config, |
3880 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0); | |
b128c09f | 3881 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 3882 | spa_load_l2cache(spa); |
b128c09f | 3883 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
3884 | spa->spa_l2cache.sav_sync = B_TRUE; |
3885 | } | |
3886 | ||
428870ff BB |
3887 | /* |
3888 | * Check for any removed devices. | |
3889 | */ | |
3890 | if (spa->spa_autoreplace) { | |
3891 | spa_aux_check_removed(&spa->spa_spares); | |
3892 | spa_aux_check_removed(&spa->spa_l2cache); | |
3893 | } | |
3894 | ||
fb5f0bc8 | 3895 | if (spa_writeable(spa)) { |
b128c09f BB |
3896 | /* |
3897 | * Update the config cache to include the newly-imported pool. | |
3898 | */ | |
45d1cae3 | 3899 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
b128c09f | 3900 | } |
34dc7c2f | 3901 | |
34dc7c2f | 3902 | /* |
9babb374 BB |
3903 | * It's possible that the pool was expanded while it was exported. |
3904 | * We kick off an async task to handle this for us. | |
34dc7c2f | 3905 | */ |
9babb374 | 3906 | spa_async_request(spa, SPA_ASYNC_AUTOEXPAND); |
b128c09f | 3907 | |
9babb374 | 3908 | mutex_exit(&spa_namespace_lock); |
45d1cae3 | 3909 | spa_history_log_version(spa, LOG_POOL_IMPORT); |
b128c09f | 3910 | |
b128c09f BB |
3911 | return (0); |
3912 | } | |
3913 | ||
34dc7c2f BB |
3914 | nvlist_t * |
3915 | spa_tryimport(nvlist_t *tryconfig) | |
3916 | { | |
3917 | nvlist_t *config = NULL; | |
3918 | char *poolname; | |
3919 | spa_t *spa; | |
3920 | uint64_t state; | |
d164b209 | 3921 | int error; |
34dc7c2f BB |
3922 | |
3923 | if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname)) | |
3924 | return (NULL); | |
3925 | ||
3926 | if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state)) | |
3927 | return (NULL); | |
3928 | ||
3929 | /* | |
3930 | * Create and initialize the spa structure. | |
3931 | */ | |
3932 | mutex_enter(&spa_namespace_lock); | |
428870ff | 3933 | spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL); |
fb5f0bc8 | 3934 | spa_activate(spa, FREAD); |
34dc7c2f BB |
3935 | |
3936 | /* | |
3937 | * Pass off the heavy lifting to spa_load(). | |
3938 | * Pass TRUE for mosconfig because the user-supplied config | |
3939 | * is actually the one to trust when doing an import. | |
3940 | */ | |
428870ff | 3941 | error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE); |
34dc7c2f BB |
3942 | |
3943 | /* | |
3944 | * If 'tryconfig' was at least parsable, return the current config. | |
3945 | */ | |
3946 | if (spa->spa_root_vdev != NULL) { | |
34dc7c2f | 3947 | config = spa_config_generate(spa, NULL, -1ULL, B_TRUE); |
34dc7c2f BB |
3948 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, |
3949 | poolname) == 0); | |
3950 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
3951 | state) == 0); | |
3952 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP, | |
3953 | spa->spa_uberblock.ub_timestamp) == 0); | |
9ae529ec CS |
3954 | VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, |
3955 | spa->spa_load_info) == 0); | |
34dc7c2f BB |
3956 | |
3957 | /* | |
3958 | * If the bootfs property exists on this pool then we | |
3959 | * copy it out so that external consumers can tell which | |
3960 | * pools are bootable. | |
3961 | */ | |
d164b209 | 3962 | if ((!error || error == EEXIST) && spa->spa_bootfs) { |
b8d06fca | 3963 | char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE); |
34dc7c2f BB |
3964 | |
3965 | /* | |
3966 | * We have to play games with the name since the | |
3967 | * pool was opened as TRYIMPORT_NAME. | |
3968 | */ | |
b128c09f | 3969 | if (dsl_dsobj_to_dsname(spa_name(spa), |
34dc7c2f BB |
3970 | spa->spa_bootfs, tmpname) == 0) { |
3971 | char *cp; | |
b8d06fca | 3972 | char *dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE); |
34dc7c2f BB |
3973 | |
3974 | cp = strchr(tmpname, '/'); | |
3975 | if (cp == NULL) { | |
3976 | (void) strlcpy(dsname, tmpname, | |
3977 | MAXPATHLEN); | |
3978 | } else { | |
3979 | (void) snprintf(dsname, MAXPATHLEN, | |
3980 | "%s/%s", poolname, ++cp); | |
3981 | } | |
3982 | VERIFY(nvlist_add_string(config, | |
3983 | ZPOOL_CONFIG_BOOTFS, dsname) == 0); | |
3984 | kmem_free(dsname, MAXPATHLEN); | |
3985 | } | |
3986 | kmem_free(tmpname, MAXPATHLEN); | |
3987 | } | |
3988 | ||
3989 | /* | |
3990 | * Add the list of hot spares and level 2 cache devices. | |
3991 | */ | |
9babb374 | 3992 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
3993 | spa_add_spares(spa, config); |
3994 | spa_add_l2cache(spa, config); | |
9babb374 | 3995 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f BB |
3996 | } |
3997 | ||
3998 | spa_unload(spa); | |
3999 | spa_deactivate(spa); | |
4000 | spa_remove(spa); | |
4001 | mutex_exit(&spa_namespace_lock); | |
4002 | ||
4003 | return (config); | |
4004 | } | |
4005 | ||
4006 | /* | |
4007 | * Pool export/destroy | |
4008 | * | |
4009 | * The act of destroying or exporting a pool is very simple. We make sure there | |
4010 | * is no more pending I/O and any references to the pool are gone. Then, we | |
4011 | * update the pool state and sync all the labels to disk, removing the | |
fb5f0bc8 BB |
4012 | * configuration from the cache afterwards. If the 'hardforce' flag is set, then |
4013 | * we don't sync the labels or remove the configuration cache. | |
34dc7c2f BB |
4014 | */ |
4015 | static int | |
b128c09f | 4016 | spa_export_common(char *pool, int new_state, nvlist_t **oldconfig, |
fb5f0bc8 | 4017 | boolean_t force, boolean_t hardforce) |
34dc7c2f BB |
4018 | { |
4019 | spa_t *spa; | |
4020 | ||
4021 | if (oldconfig) | |
4022 | *oldconfig = NULL; | |
4023 | ||
fb5f0bc8 | 4024 | if (!(spa_mode_global & FWRITE)) |
34dc7c2f BB |
4025 | return (EROFS); |
4026 | ||
4027 | mutex_enter(&spa_namespace_lock); | |
4028 | if ((spa = spa_lookup(pool)) == NULL) { | |
4029 | mutex_exit(&spa_namespace_lock); | |
4030 | return (ENOENT); | |
4031 | } | |
4032 | ||
4033 | /* | |
4034 | * Put a hold on the pool, drop the namespace lock, stop async tasks, | |
4035 | * reacquire the namespace lock, and see if we can export. | |
4036 | */ | |
4037 | spa_open_ref(spa, FTAG); | |
4038 | mutex_exit(&spa_namespace_lock); | |
4039 | spa_async_suspend(spa); | |
4040 | mutex_enter(&spa_namespace_lock); | |
4041 | spa_close(spa, FTAG); | |
4042 | ||
4043 | /* | |
4044 | * The pool will be in core if it's openable, | |
4045 | * in which case we can modify its state. | |
4046 | */ | |
4047 | if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) { | |
4048 | /* | |
4049 | * Objsets may be open only because they're dirty, so we | |
4050 | * have to force it to sync before checking spa_refcnt. | |
4051 | */ | |
34dc7c2f BB |
4052 | txg_wait_synced(spa->spa_dsl_pool, 0); |
4053 | ||
4054 | /* | |
4055 | * A pool cannot be exported or destroyed if there are active | |
4056 | * references. If we are resetting a pool, allow references by | |
4057 | * fault injection handlers. | |
4058 | */ | |
4059 | if (!spa_refcount_zero(spa) || | |
4060 | (spa->spa_inject_ref != 0 && | |
4061 | new_state != POOL_STATE_UNINITIALIZED)) { | |
34dc7c2f BB |
4062 | spa_async_resume(spa); |
4063 | mutex_exit(&spa_namespace_lock); | |
4064 | return (EBUSY); | |
4065 | } | |
4066 | ||
b128c09f BB |
4067 | /* |
4068 | * A pool cannot be exported if it has an active shared spare. | |
4069 | * This is to prevent other pools stealing the active spare | |
4070 | * from an exported pool. At user's own will, such pool can | |
4071 | * be forcedly exported. | |
4072 | */ | |
4073 | if (!force && new_state == POOL_STATE_EXPORTED && | |
4074 | spa_has_active_shared_spare(spa)) { | |
4075 | spa_async_resume(spa); | |
4076 | mutex_exit(&spa_namespace_lock); | |
4077 | return (EXDEV); | |
4078 | } | |
34dc7c2f BB |
4079 | |
4080 | /* | |
4081 | * We want this to be reflected on every label, | |
4082 | * so mark them all dirty. spa_unload() will do the | |
4083 | * final sync that pushes these changes out. | |
4084 | */ | |
fb5f0bc8 | 4085 | if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) { |
b128c09f | 4086 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f | 4087 | spa->spa_state = new_state; |
428870ff BB |
4088 | spa->spa_final_txg = spa_last_synced_txg(spa) + |
4089 | TXG_DEFER_SIZE + 1; | |
34dc7c2f | 4090 | vdev_config_dirty(spa->spa_root_vdev); |
b128c09f | 4091 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
4092 | } |
4093 | } | |
4094 | ||
26685276 | 4095 | spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY); |
34dc7c2f BB |
4096 | |
4097 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
4098 | spa_unload(spa); | |
4099 | spa_deactivate(spa); | |
4100 | } | |
4101 | ||
4102 | if (oldconfig && spa->spa_config) | |
4103 | VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0); | |
4104 | ||
4105 | if (new_state != POOL_STATE_UNINITIALIZED) { | |
fb5f0bc8 BB |
4106 | if (!hardforce) |
4107 | spa_config_sync(spa, B_TRUE, B_TRUE); | |
34dc7c2f | 4108 | spa_remove(spa); |
34dc7c2f BB |
4109 | } |
4110 | mutex_exit(&spa_namespace_lock); | |
4111 | ||
4112 | return (0); | |
4113 | } | |
4114 | ||
4115 | /* | |
4116 | * Destroy a storage pool. | |
4117 | */ | |
4118 | int | |
4119 | spa_destroy(char *pool) | |
4120 | { | |
fb5f0bc8 BB |
4121 | return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL, |
4122 | B_FALSE, B_FALSE)); | |
34dc7c2f BB |
4123 | } |
4124 | ||
4125 | /* | |
4126 | * Export a storage pool. | |
4127 | */ | |
4128 | int | |
fb5f0bc8 BB |
4129 | spa_export(char *pool, nvlist_t **oldconfig, boolean_t force, |
4130 | boolean_t hardforce) | |
34dc7c2f | 4131 | { |
fb5f0bc8 BB |
4132 | return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig, |
4133 | force, hardforce)); | |
34dc7c2f BB |
4134 | } |
4135 | ||
4136 | /* | |
4137 | * Similar to spa_export(), this unloads the spa_t without actually removing it | |
4138 | * from the namespace in any way. | |
4139 | */ | |
4140 | int | |
4141 | spa_reset(char *pool) | |
4142 | { | |
b128c09f | 4143 | return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL, |
fb5f0bc8 | 4144 | B_FALSE, B_FALSE)); |
34dc7c2f BB |
4145 | } |
4146 | ||
34dc7c2f BB |
4147 | /* |
4148 | * ========================================================================== | |
4149 | * Device manipulation | |
4150 | * ========================================================================== | |
4151 | */ | |
4152 | ||
4153 | /* | |
4154 | * Add a device to a storage pool. | |
4155 | */ | |
4156 | int | |
4157 | spa_vdev_add(spa_t *spa, nvlist_t *nvroot) | |
4158 | { | |
428870ff | 4159 | uint64_t txg, id; |
fb5f0bc8 | 4160 | int error; |
34dc7c2f BB |
4161 | vdev_t *rvd = spa->spa_root_vdev; |
4162 | vdev_t *vd, *tvd; | |
4163 | nvlist_t **spares, **l2cache; | |
4164 | uint_t nspares, nl2cache; | |
d6320ddb | 4165 | int c; |
34dc7c2f | 4166 | |
572e2857 BB |
4167 | ASSERT(spa_writeable(spa)); |
4168 | ||
34dc7c2f BB |
4169 | txg = spa_vdev_enter(spa); |
4170 | ||
4171 | if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0, | |
4172 | VDEV_ALLOC_ADD)) != 0) | |
4173 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
4174 | ||
b128c09f | 4175 | spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */ |
34dc7c2f BB |
4176 | |
4177 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares, | |
4178 | &nspares) != 0) | |
4179 | nspares = 0; | |
4180 | ||
4181 | if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache, | |
4182 | &nl2cache) != 0) | |
4183 | nl2cache = 0; | |
4184 | ||
b128c09f | 4185 | if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0) |
34dc7c2f | 4186 | return (spa_vdev_exit(spa, vd, txg, EINVAL)); |
34dc7c2f | 4187 | |
b128c09f BB |
4188 | if (vd->vdev_children != 0 && |
4189 | (error = vdev_create(vd, txg, B_FALSE)) != 0) | |
4190 | return (spa_vdev_exit(spa, vd, txg, error)); | |
34dc7c2f BB |
4191 | |
4192 | /* | |
4193 | * We must validate the spares and l2cache devices after checking the | |
4194 | * children. Otherwise, vdev_inuse() will blindly overwrite the spare. | |
4195 | */ | |
b128c09f | 4196 | if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0) |
34dc7c2f | 4197 | return (spa_vdev_exit(spa, vd, txg, error)); |
34dc7c2f BB |
4198 | |
4199 | /* | |
4200 | * Transfer each new top-level vdev from vd to rvd. | |
4201 | */ | |
d6320ddb | 4202 | for (c = 0; c < vd->vdev_children; c++) { |
428870ff BB |
4203 | |
4204 | /* | |
4205 | * Set the vdev id to the first hole, if one exists. | |
4206 | */ | |
4207 | for (id = 0; id < rvd->vdev_children; id++) { | |
4208 | if (rvd->vdev_child[id]->vdev_ishole) { | |
4209 | vdev_free(rvd->vdev_child[id]); | |
4210 | break; | |
4211 | } | |
4212 | } | |
34dc7c2f BB |
4213 | tvd = vd->vdev_child[c]; |
4214 | vdev_remove_child(vd, tvd); | |
428870ff | 4215 | tvd->vdev_id = id; |
34dc7c2f BB |
4216 | vdev_add_child(rvd, tvd); |
4217 | vdev_config_dirty(tvd); | |
4218 | } | |
4219 | ||
4220 | if (nspares != 0) { | |
4221 | spa_set_aux_vdevs(&spa->spa_spares, spares, nspares, | |
4222 | ZPOOL_CONFIG_SPARES); | |
4223 | spa_load_spares(spa); | |
4224 | spa->spa_spares.sav_sync = B_TRUE; | |
4225 | } | |
4226 | ||
4227 | if (nl2cache != 0) { | |
4228 | spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache, | |
4229 | ZPOOL_CONFIG_L2CACHE); | |
4230 | spa_load_l2cache(spa); | |
4231 | spa->spa_l2cache.sav_sync = B_TRUE; | |
4232 | } | |
4233 | ||
4234 | /* | |
4235 | * We have to be careful when adding new vdevs to an existing pool. | |
4236 | * If other threads start allocating from these vdevs before we | |
4237 | * sync the config cache, and we lose power, then upon reboot we may | |
4238 | * fail to open the pool because there are DVAs that the config cache | |
4239 | * can't translate. Therefore, we first add the vdevs without | |
4240 | * initializing metaslabs; sync the config cache (via spa_vdev_exit()); | |
4241 | * and then let spa_config_update() initialize the new metaslabs. | |
4242 | * | |
4243 | * spa_load() checks for added-but-not-initialized vdevs, so that | |
4244 | * if we lose power at any point in this sequence, the remaining | |
4245 | * steps will be completed the next time we load the pool. | |
4246 | */ | |
4247 | (void) spa_vdev_exit(spa, vd, txg, 0); | |
4248 | ||
4249 | mutex_enter(&spa_namespace_lock); | |
4250 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); | |
4251 | mutex_exit(&spa_namespace_lock); | |
4252 | ||
4253 | return (0); | |
4254 | } | |
4255 | ||
4256 | /* | |
4257 | * Attach a device to a mirror. The arguments are the path to any device | |
4258 | * in the mirror, and the nvroot for the new device. If the path specifies | |
4259 | * a device that is not mirrored, we automatically insert the mirror vdev. | |
4260 | * | |
4261 | * If 'replacing' is specified, the new device is intended to replace the | |
4262 | * existing device; in this case the two devices are made into their own | |
4263 | * mirror using the 'replacing' vdev, which is functionally identical to | |
4264 | * the mirror vdev (it actually reuses all the same ops) but has a few | |
4265 | * extra rules: you can't attach to it after it's been created, and upon | |
4266 | * completion of resilvering, the first disk (the one being replaced) | |
4267 | * is automatically detached. | |
4268 | */ | |
4269 | int | |
4270 | spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing) | |
4271 | { | |
428870ff | 4272 | uint64_t txg, dtl_max_txg; |
1fde1e37 | 4273 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev;) |
34dc7c2f BB |
4274 | vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd; |
4275 | vdev_ops_t *pvops; | |
b128c09f BB |
4276 | char *oldvdpath, *newvdpath; |
4277 | int newvd_isspare; | |
4278 | int error; | |
34dc7c2f | 4279 | |
572e2857 BB |
4280 | ASSERT(spa_writeable(spa)); |
4281 | ||
34dc7c2f BB |
4282 | txg = spa_vdev_enter(spa); |
4283 | ||
b128c09f | 4284 | oldvd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f BB |
4285 | |
4286 | if (oldvd == NULL) | |
4287 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
4288 | ||
4289 | if (!oldvd->vdev_ops->vdev_op_leaf) | |
4290 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4291 | ||
4292 | pvd = oldvd->vdev_parent; | |
4293 | ||
4294 | if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0, | |
5ffb9d1d | 4295 | VDEV_ALLOC_ATTACH)) != 0) |
34dc7c2f BB |
4296 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); |
4297 | ||
4298 | if (newrootvd->vdev_children != 1) | |
4299 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
4300 | ||
4301 | newvd = newrootvd->vdev_child[0]; | |
4302 | ||
4303 | if (!newvd->vdev_ops->vdev_op_leaf) | |
4304 | return (spa_vdev_exit(spa, newrootvd, txg, EINVAL)); | |
4305 | ||
4306 | if ((error = vdev_create(newrootvd, txg, replacing)) != 0) | |
4307 | return (spa_vdev_exit(spa, newrootvd, txg, error)); | |
4308 | ||
4309 | /* | |
4310 | * Spares can't replace logs | |
4311 | */ | |
b128c09f | 4312 | if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare) |
34dc7c2f BB |
4313 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
4314 | ||
4315 | if (!replacing) { | |
4316 | /* | |
4317 | * For attach, the only allowable parent is a mirror or the root | |
4318 | * vdev. | |
4319 | */ | |
4320 | if (pvd->vdev_ops != &vdev_mirror_ops && | |
4321 | pvd->vdev_ops != &vdev_root_ops) | |
4322 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4323 | ||
4324 | pvops = &vdev_mirror_ops; | |
4325 | } else { | |
4326 | /* | |
4327 | * Active hot spares can only be replaced by inactive hot | |
4328 | * spares. | |
4329 | */ | |
4330 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 | 4331 | oldvd->vdev_isspare && |
34dc7c2f BB |
4332 | !spa_has_spare(spa, newvd->vdev_guid)) |
4333 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); | |
4334 | ||
4335 | /* | |
4336 | * If the source is a hot spare, and the parent isn't already a | |
4337 | * spare, then we want to create a new hot spare. Otherwise, we | |
4338 | * want to create a replacing vdev. The user is not allowed to | |
4339 | * attach to a spared vdev child unless the 'isspare' state is | |
4340 | * the same (spare replaces spare, non-spare replaces | |
4341 | * non-spare). | |
4342 | */ | |
572e2857 BB |
4343 | if (pvd->vdev_ops == &vdev_replacing_ops && |
4344 | spa_version(spa) < SPA_VERSION_MULTI_REPLACE) { | |
34dc7c2f | 4345 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
4346 | } else if (pvd->vdev_ops == &vdev_spare_ops && |
4347 | newvd->vdev_isspare != oldvd->vdev_isspare) { | |
34dc7c2f | 4348 | return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP)); |
572e2857 BB |
4349 | } |
4350 | ||
4351 | if (newvd->vdev_isspare) | |
34dc7c2f BB |
4352 | pvops = &vdev_spare_ops; |
4353 | else | |
4354 | pvops = &vdev_replacing_ops; | |
4355 | } | |
4356 | ||
4357 | /* | |
9babb374 | 4358 | * Make sure the new device is big enough. |
34dc7c2f | 4359 | */ |
9babb374 | 4360 | if (newvd->vdev_asize < vdev_get_min_asize(oldvd)) |
34dc7c2f BB |
4361 | return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW)); |
4362 | ||
4363 | /* | |
4364 | * The new device cannot have a higher alignment requirement | |
4365 | * than the top-level vdev. | |
4366 | */ | |
4367 | if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift) | |
4368 | return (spa_vdev_exit(spa, newrootvd, txg, EDOM)); | |
4369 | ||
4370 | /* | |
4371 | * If this is an in-place replacement, update oldvd's path and devid | |
4372 | * to make it distinguishable from newvd, and unopenable from now on. | |
4373 | */ | |
4374 | if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) { | |
4375 | spa_strfree(oldvd->vdev_path); | |
4376 | oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5, | |
b8d06fca | 4377 | KM_PUSHPAGE); |
34dc7c2f BB |
4378 | (void) sprintf(oldvd->vdev_path, "%s/%s", |
4379 | newvd->vdev_path, "old"); | |
4380 | if (oldvd->vdev_devid != NULL) { | |
4381 | spa_strfree(oldvd->vdev_devid); | |
4382 | oldvd->vdev_devid = NULL; | |
4383 | } | |
4384 | } | |
4385 | ||
572e2857 BB |
4386 | /* mark the device being resilvered */ |
4387 | newvd->vdev_resilvering = B_TRUE; | |
4388 | ||
34dc7c2f BB |
4389 | /* |
4390 | * If the parent is not a mirror, or if we're replacing, insert the new | |
4391 | * mirror/replacing/spare vdev above oldvd. | |
4392 | */ | |
4393 | if (pvd->vdev_ops != pvops) | |
4394 | pvd = vdev_add_parent(oldvd, pvops); | |
4395 | ||
4396 | ASSERT(pvd->vdev_top->vdev_parent == rvd); | |
4397 | ASSERT(pvd->vdev_ops == pvops); | |
4398 | ASSERT(oldvd->vdev_parent == pvd); | |
4399 | ||
4400 | /* | |
4401 | * Extract the new device from its root and add it to pvd. | |
4402 | */ | |
4403 | vdev_remove_child(newrootvd, newvd); | |
4404 | newvd->vdev_id = pvd->vdev_children; | |
428870ff | 4405 | newvd->vdev_crtxg = oldvd->vdev_crtxg; |
34dc7c2f BB |
4406 | vdev_add_child(pvd, newvd); |
4407 | ||
34dc7c2f BB |
4408 | tvd = newvd->vdev_top; |
4409 | ASSERT(pvd->vdev_top == tvd); | |
4410 | ASSERT(tvd->vdev_parent == rvd); | |
4411 | ||
4412 | vdev_config_dirty(tvd); | |
4413 | ||
4414 | /* | |
428870ff BB |
4415 | * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account |
4416 | * for any dmu_sync-ed blocks. It will propagate upward when | |
4417 | * spa_vdev_exit() calls vdev_dtl_reassess(). | |
34dc7c2f | 4418 | */ |
428870ff | 4419 | dtl_max_txg = txg + TXG_CONCURRENT_STATES; |
34dc7c2f | 4420 | |
428870ff BB |
4421 | vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL, |
4422 | dtl_max_txg - TXG_INITIAL); | |
34dc7c2f | 4423 | |
9babb374 | 4424 | if (newvd->vdev_isspare) { |
34dc7c2f | 4425 | spa_spare_activate(newvd); |
26685276 | 4426 | spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE); |
9babb374 BB |
4427 | } |
4428 | ||
b128c09f BB |
4429 | oldvdpath = spa_strdup(oldvd->vdev_path); |
4430 | newvdpath = spa_strdup(newvd->vdev_path); | |
4431 | newvd_isspare = newvd->vdev_isspare; | |
34dc7c2f BB |
4432 | |
4433 | /* | |
4434 | * Mark newvd's DTL dirty in this txg. | |
4435 | */ | |
4436 | vdev_dirty(tvd, VDD_DTL, newvd, txg); | |
4437 | ||
428870ff BB |
4438 | /* |
4439 | * Restart the resilver | |
4440 | */ | |
4441 | dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg); | |
4442 | ||
4443 | /* | |
4444 | * Commit the config | |
4445 | */ | |
4446 | (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0); | |
34dc7c2f | 4447 | |
428870ff BB |
4448 | spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL, |
4449 | "%s vdev=%s %s vdev=%s", | |
45d1cae3 BB |
4450 | replacing && newvd_isspare ? "spare in" : |
4451 | replacing ? "replace" : "attach", newvdpath, | |
4452 | replacing ? "for" : "to", oldvdpath); | |
b128c09f BB |
4453 | |
4454 | spa_strfree(oldvdpath); | |
4455 | spa_strfree(newvdpath); | |
4456 | ||
572e2857 | 4457 | if (spa->spa_bootfs) |
26685276 | 4458 | spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH); |
572e2857 | 4459 | |
34dc7c2f BB |
4460 | return (0); |
4461 | } | |
4462 | ||
4463 | /* | |
4464 | * Detach a device from a mirror or replacing vdev. | |
4465 | * If 'replace_done' is specified, only detach if the parent | |
4466 | * is a replacing vdev. | |
4467 | */ | |
4468 | int | |
fb5f0bc8 | 4469 | spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done) |
34dc7c2f BB |
4470 | { |
4471 | uint64_t txg; | |
fb5f0bc8 | 4472 | int error; |
1fde1e37 | 4473 | ASSERTV(vdev_t *rvd = spa->spa_root_vdev;) |
34dc7c2f BB |
4474 | vdev_t *vd, *pvd, *cvd, *tvd; |
4475 | boolean_t unspare = B_FALSE; | |
d4ed6673 | 4476 | uint64_t unspare_guid = 0; |
428870ff | 4477 | char *vdpath; |
d6320ddb | 4478 | int c, t; |
34dc7c2f | 4479 | |
572e2857 BB |
4480 | ASSERT(spa_writeable(spa)); |
4481 | ||
34dc7c2f BB |
4482 | txg = spa_vdev_enter(spa); |
4483 | ||
b128c09f | 4484 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f BB |
4485 | |
4486 | if (vd == NULL) | |
4487 | return (spa_vdev_exit(spa, NULL, txg, ENODEV)); | |
4488 | ||
4489 | if (!vd->vdev_ops->vdev_op_leaf) | |
4490 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4491 | ||
4492 | pvd = vd->vdev_parent; | |
4493 | ||
fb5f0bc8 BB |
4494 | /* |
4495 | * If the parent/child relationship is not as expected, don't do it. | |
4496 | * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing | |
4497 | * vdev that's replacing B with C. The user's intent in replacing | |
4498 | * is to go from M(A,B) to M(A,C). If the user decides to cancel | |
4499 | * the replace by detaching C, the expected behavior is to end up | |
4500 | * M(A,B). But suppose that right after deciding to detach C, | |
4501 | * the replacement of B completes. We would have M(A,C), and then | |
4502 | * ask to detach C, which would leave us with just A -- not what | |
4503 | * the user wanted. To prevent this, we make sure that the | |
4504 | * parent/child relationship hasn't changed -- in this example, | |
4505 | * that C's parent is still the replacing vdev R. | |
4506 | */ | |
4507 | if (pvd->vdev_guid != pguid && pguid != 0) | |
4508 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); | |
4509 | ||
34dc7c2f | 4510 | /* |
572e2857 | 4511 | * Only 'replacing' or 'spare' vdevs can be replaced. |
34dc7c2f | 4512 | */ |
572e2857 BB |
4513 | if (replace_done && pvd->vdev_ops != &vdev_replacing_ops && |
4514 | pvd->vdev_ops != &vdev_spare_ops) | |
4515 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
34dc7c2f BB |
4516 | |
4517 | ASSERT(pvd->vdev_ops != &vdev_spare_ops || | |
4518 | spa_version(spa) >= SPA_VERSION_SPARES); | |
4519 | ||
4520 | /* | |
4521 | * Only mirror, replacing, and spare vdevs support detach. | |
4522 | */ | |
4523 | if (pvd->vdev_ops != &vdev_replacing_ops && | |
4524 | pvd->vdev_ops != &vdev_mirror_ops && | |
4525 | pvd->vdev_ops != &vdev_spare_ops) | |
4526 | return (spa_vdev_exit(spa, NULL, txg, ENOTSUP)); | |
4527 | ||
4528 | /* | |
fb5f0bc8 BB |
4529 | * If this device has the only valid copy of some data, |
4530 | * we cannot safely detach it. | |
34dc7c2f | 4531 | */ |
fb5f0bc8 | 4532 | if (vdev_dtl_required(vd)) |
34dc7c2f BB |
4533 | return (spa_vdev_exit(spa, NULL, txg, EBUSY)); |
4534 | ||
fb5f0bc8 | 4535 | ASSERT(pvd->vdev_children >= 2); |
34dc7c2f | 4536 | |
b128c09f BB |
4537 | /* |
4538 | * If we are detaching the second disk from a replacing vdev, then | |
4539 | * check to see if we changed the original vdev's path to have "/old" | |
4540 | * at the end in spa_vdev_attach(). If so, undo that change now. | |
4541 | */ | |
572e2857 BB |
4542 | if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 && |
4543 | vd->vdev_path != NULL) { | |
4544 | size_t len = strlen(vd->vdev_path); | |
4545 | ||
d6320ddb | 4546 | for (c = 0; c < pvd->vdev_children; c++) { |
572e2857 BB |
4547 | cvd = pvd->vdev_child[c]; |
4548 | ||
4549 | if (cvd == vd || cvd->vdev_path == NULL) | |
4550 | continue; | |
4551 | ||
4552 | if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 && | |
4553 | strcmp(cvd->vdev_path + len, "/old") == 0) { | |
4554 | spa_strfree(cvd->vdev_path); | |
4555 | cvd->vdev_path = spa_strdup(vd->vdev_path); | |
4556 | break; | |
4557 | } | |
b128c09f BB |
4558 | } |
4559 | } | |
4560 | ||
34dc7c2f BB |
4561 | /* |
4562 | * If we are detaching the original disk from a spare, then it implies | |
4563 | * that the spare should become a real disk, and be removed from the | |
4564 | * active spare list for the pool. | |
4565 | */ | |
4566 | if (pvd->vdev_ops == &vdev_spare_ops && | |
572e2857 BB |
4567 | vd->vdev_id == 0 && |
4568 | pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare) | |
34dc7c2f BB |
4569 | unspare = B_TRUE; |
4570 | ||
4571 | /* | |
4572 | * Erase the disk labels so the disk can be used for other things. | |
4573 | * This must be done after all other error cases are handled, | |
4574 | * but before we disembowel vd (so we can still do I/O to it). | |
4575 | * But if we can't do it, don't treat the error as fatal -- | |
4576 | * it may be that the unwritability of the disk is the reason | |
4577 | * it's being detached! | |
4578 | */ | |
4579 | error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
4580 | ||
4581 | /* | |
4582 | * Remove vd from its parent and compact the parent's children. | |
4583 | */ | |
4584 | vdev_remove_child(pvd, vd); | |
4585 | vdev_compact_children(pvd); | |
4586 | ||
4587 | /* | |
4588 | * Remember one of the remaining children so we can get tvd below. | |
4589 | */ | |
572e2857 | 4590 | cvd = pvd->vdev_child[pvd->vdev_children - 1]; |
34dc7c2f BB |
4591 | |
4592 | /* | |
4593 | * If we need to remove the remaining child from the list of hot spares, | |
fb5f0bc8 BB |
4594 | * do it now, marking the vdev as no longer a spare in the process. |
4595 | * We must do this before vdev_remove_parent(), because that can | |
4596 | * change the GUID if it creates a new toplevel GUID. For a similar | |
4597 | * reason, we must remove the spare now, in the same txg as the detach; | |
4598 | * otherwise someone could attach a new sibling, change the GUID, and | |
4599 | * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail. | |
34dc7c2f BB |
4600 | */ |
4601 | if (unspare) { | |
4602 | ASSERT(cvd->vdev_isspare); | |
4603 | spa_spare_remove(cvd); | |
4604 | unspare_guid = cvd->vdev_guid; | |
fb5f0bc8 | 4605 | (void) spa_vdev_remove(spa, unspare_guid, B_TRUE); |
572e2857 | 4606 | cvd->vdev_unspare = B_TRUE; |
34dc7c2f BB |
4607 | } |
4608 | ||
428870ff BB |
4609 | /* |
4610 | * If the parent mirror/replacing vdev only has one child, | |
4611 | * the parent is no longer needed. Remove it from the tree. | |
4612 | */ | |
572e2857 BB |
4613 | if (pvd->vdev_children == 1) { |
4614 | if (pvd->vdev_ops == &vdev_spare_ops) | |
4615 | cvd->vdev_unspare = B_FALSE; | |
428870ff | 4616 | vdev_remove_parent(cvd); |
572e2857 BB |
4617 | cvd->vdev_resilvering = B_FALSE; |
4618 | } | |
4619 | ||
428870ff BB |
4620 | |
4621 | /* | |
4622 | * We don't set tvd until now because the parent we just removed | |
4623 | * may have been the previous top-level vdev. | |
4624 | */ | |
4625 | tvd = cvd->vdev_top; | |
4626 | ASSERT(tvd->vdev_parent == rvd); | |
4627 | ||
4628 | /* | |
4629 | * Reevaluate the parent vdev state. | |
4630 | */ | |
4631 | vdev_propagate_state(cvd); | |
4632 | ||
4633 | /* | |
4634 | * If the 'autoexpand' property is set on the pool then automatically | |
4635 | * try to expand the size of the pool. For example if the device we | |
4636 | * just detached was smaller than the others, it may be possible to | |
4637 | * add metaslabs (i.e. grow the pool). We need to reopen the vdev | |
4638 | * first so that we can obtain the updated sizes of the leaf vdevs. | |
4639 | */ | |
4640 | if (spa->spa_autoexpand) { | |
4641 | vdev_reopen(tvd); | |
4642 | vdev_expand(tvd, txg); | |
4643 | } | |
4644 | ||
4645 | vdev_config_dirty(tvd); | |
4646 | ||
4647 | /* | |
4648 | * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that | |
4649 | * vd->vdev_detached is set and free vd's DTL object in syncing context. | |
4650 | * But first make sure we're not on any *other* txg's DTL list, to | |
4651 | * prevent vd from being accessed after it's freed. | |
4652 | */ | |
4653 | vdpath = spa_strdup(vd->vdev_path); | |
d6320ddb | 4654 | for (t = 0; t < TXG_SIZE; t++) |
428870ff BB |
4655 | (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t); |
4656 | vd->vdev_detached = B_TRUE; | |
4657 | vdev_dirty(tvd, VDD_DTL, vd, txg); | |
4658 | ||
26685276 | 4659 | spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE); |
428870ff | 4660 | |
572e2857 BB |
4661 | /* hang on to the spa before we release the lock */ |
4662 | spa_open_ref(spa, FTAG); | |
4663 | ||
428870ff BB |
4664 | error = spa_vdev_exit(spa, vd, txg, 0); |
4665 | ||
4666 | spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL, | |
4667 | "vdev=%s", vdpath); | |
4668 | spa_strfree(vdpath); | |
4669 | ||
4670 | /* | |
4671 | * If this was the removal of the original device in a hot spare vdev, | |
4672 | * then we want to go through and remove the device from the hot spare | |
4673 | * list of every other pool. | |
4674 | */ | |
4675 | if (unspare) { | |
572e2857 BB |
4676 | spa_t *altspa = NULL; |
4677 | ||
428870ff | 4678 | mutex_enter(&spa_namespace_lock); |
572e2857 BB |
4679 | while ((altspa = spa_next(altspa)) != NULL) { |
4680 | if (altspa->spa_state != POOL_STATE_ACTIVE || | |
4681 | altspa == spa) | |
428870ff | 4682 | continue; |
572e2857 BB |
4683 | |
4684 | spa_open_ref(altspa, FTAG); | |
428870ff | 4685 | mutex_exit(&spa_namespace_lock); |
572e2857 | 4686 | (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE); |
428870ff | 4687 | mutex_enter(&spa_namespace_lock); |
572e2857 | 4688 | spa_close(altspa, FTAG); |
428870ff BB |
4689 | } |
4690 | mutex_exit(&spa_namespace_lock); | |
572e2857 BB |
4691 | |
4692 | /* search the rest of the vdevs for spares to remove */ | |
4693 | spa_vdev_resilver_done(spa); | |
428870ff BB |
4694 | } |
4695 | ||
572e2857 BB |
4696 | /* all done with the spa; OK to release */ |
4697 | mutex_enter(&spa_namespace_lock); | |
4698 | spa_close(spa, FTAG); | |
4699 | mutex_exit(&spa_namespace_lock); | |
4700 | ||
428870ff BB |
4701 | return (error); |
4702 | } | |
4703 | ||
4704 | /* | |
4705 | * Split a set of devices from their mirrors, and create a new pool from them. | |
4706 | */ | |
4707 | int | |
4708 | spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config, | |
4709 | nvlist_t *props, boolean_t exp) | |
4710 | { | |
4711 | int error = 0; | |
4712 | uint64_t txg, *glist; | |
4713 | spa_t *newspa; | |
4714 | uint_t c, children, lastlog; | |
4715 | nvlist_t **child, *nvl, *tmp; | |
4716 | dmu_tx_t *tx; | |
4717 | char *altroot = NULL; | |
4718 | vdev_t *rvd, **vml = NULL; /* vdev modify list */ | |
4719 | boolean_t activate_slog; | |
4720 | ||
572e2857 | 4721 | ASSERT(spa_writeable(spa)); |
428870ff BB |
4722 | |
4723 | txg = spa_vdev_enter(spa); | |
4724 | ||
4725 | /* clear the log and flush everything up to now */ | |
4726 | activate_slog = spa_passivate_log(spa); | |
4727 | (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
4728 | error = spa_offline_log(spa); | |
4729 | txg = spa_vdev_config_enter(spa); | |
4730 | ||
4731 | if (activate_slog) | |
4732 | spa_activate_log(spa); | |
4733 | ||
4734 | if (error != 0) | |
4735 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
4736 | ||
4737 | /* check new spa name before going any further */ | |
4738 | if (spa_lookup(newname) != NULL) | |
4739 | return (spa_vdev_exit(spa, NULL, txg, EEXIST)); | |
4740 | ||
4741 | /* | |
4742 | * scan through all the children to ensure they're all mirrors | |
4743 | */ | |
4744 | if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 || | |
4745 | nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child, | |
4746 | &children) != 0) | |
4747 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
4748 | ||
4749 | /* first, check to ensure we've got the right child count */ | |
4750 | rvd = spa->spa_root_vdev; | |
4751 | lastlog = 0; | |
4752 | for (c = 0; c < rvd->vdev_children; c++) { | |
4753 | vdev_t *vd = rvd->vdev_child[c]; | |
4754 | ||
4755 | /* don't count the holes & logs as children */ | |
4756 | if (vd->vdev_islog || vd->vdev_ishole) { | |
4757 | if (lastlog == 0) | |
4758 | lastlog = c; | |
4759 | continue; | |
4760 | } | |
4761 | ||
4762 | lastlog = 0; | |
4763 | } | |
4764 | if (children != (lastlog != 0 ? lastlog : rvd->vdev_children)) | |
4765 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
4766 | ||
4767 | /* next, ensure no spare or cache devices are part of the split */ | |
4768 | if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 || | |
4769 | nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0) | |
4770 | return (spa_vdev_exit(spa, NULL, txg, EINVAL)); | |
4771 | ||
b8d06fca RY |
4772 | vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE); |
4773 | glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE); | |
428870ff BB |
4774 | |
4775 | /* then, loop over each vdev and validate it */ | |
4776 | for (c = 0; c < children; c++) { | |
4777 | uint64_t is_hole = 0; | |
4778 | ||
4779 | (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE, | |
4780 | &is_hole); | |
4781 | ||
4782 | if (is_hole != 0) { | |
4783 | if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole || | |
4784 | spa->spa_root_vdev->vdev_child[c]->vdev_islog) { | |
4785 | continue; | |
4786 | } else { | |
4787 | error = EINVAL; | |
4788 | break; | |
4789 | } | |
4790 | } | |
4791 | ||
4792 | /* which disk is going to be split? */ | |
4793 | if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID, | |
4794 | &glist[c]) != 0) { | |
4795 | error = EINVAL; | |
4796 | break; | |
4797 | } | |
4798 | ||
4799 | /* look it up in the spa */ | |
4800 | vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE); | |
4801 | if (vml[c] == NULL) { | |
4802 | error = ENODEV; | |
4803 | break; | |
4804 | } | |
4805 | ||
4806 | /* make sure there's nothing stopping the split */ | |
4807 | if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops || | |
4808 | vml[c]->vdev_islog || | |
4809 | vml[c]->vdev_ishole || | |
4810 | vml[c]->vdev_isspare || | |
4811 | vml[c]->vdev_isl2cache || | |
4812 | !vdev_writeable(vml[c]) || | |
4813 | vml[c]->vdev_children != 0 || | |
4814 | vml[c]->vdev_state != VDEV_STATE_HEALTHY || | |
4815 | c != spa->spa_root_vdev->vdev_child[c]->vdev_id) { | |
4816 | error = EINVAL; | |
4817 | break; | |
4818 | } | |
4819 | ||
4820 | if (vdev_dtl_required(vml[c])) { | |
4821 | error = EBUSY; | |
4822 | break; | |
4823 | } | |
4824 | ||
4825 | /* we need certain info from the top level */ | |
4826 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY, | |
4827 | vml[c]->vdev_top->vdev_ms_array) == 0); | |
4828 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT, | |
4829 | vml[c]->vdev_top->vdev_ms_shift) == 0); | |
4830 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE, | |
4831 | vml[c]->vdev_top->vdev_asize) == 0); | |
4832 | VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT, | |
4833 | vml[c]->vdev_top->vdev_ashift) == 0); | |
4834 | } | |
4835 | ||
4836 | if (error != 0) { | |
4837 | kmem_free(vml, children * sizeof (vdev_t *)); | |
4838 | kmem_free(glist, children * sizeof (uint64_t)); | |
4839 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
4840 | } | |
4841 | ||
4842 | /* stop writers from using the disks */ | |
4843 | for (c = 0; c < children; c++) { | |
4844 | if (vml[c] != NULL) | |
4845 | vml[c]->vdev_offline = B_TRUE; | |
4846 | } | |
4847 | vdev_reopen(spa->spa_root_vdev); | |
34dc7c2f BB |
4848 | |
4849 | /* | |
428870ff BB |
4850 | * Temporarily record the splitting vdevs in the spa config. This |
4851 | * will disappear once the config is regenerated. | |
34dc7c2f | 4852 | */ |
b8d06fca | 4853 | VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
428870ff BB |
4854 | VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST, |
4855 | glist, children) == 0); | |
4856 | kmem_free(glist, children * sizeof (uint64_t)); | |
34dc7c2f | 4857 | |
428870ff BB |
4858 | mutex_enter(&spa->spa_props_lock); |
4859 | VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT, | |
4860 | nvl) == 0); | |
4861 | mutex_exit(&spa->spa_props_lock); | |
4862 | spa->spa_config_splitting = nvl; | |
4863 | vdev_config_dirty(spa->spa_root_vdev); | |
4864 | ||
4865 | /* configure and create the new pool */ | |
4866 | VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0); | |
4867 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE, | |
4868 | exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0); | |
4869 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
4870 | spa_version(spa)) == 0); | |
4871 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG, | |
4872 | spa->spa_config_txg) == 0); | |
4873 | VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID, | |
4874 | spa_generate_guid(NULL)) == 0); | |
4875 | (void) nvlist_lookup_string(props, | |
4876 | zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot); | |
34dc7c2f | 4877 | |
428870ff BB |
4878 | /* add the new pool to the namespace */ |
4879 | newspa = spa_add(newname, config, altroot); | |
4880 | newspa->spa_config_txg = spa->spa_config_txg; | |
4881 | spa_set_log_state(newspa, SPA_LOG_CLEAR); | |
4882 | ||
4883 | /* release the spa config lock, retaining the namespace lock */ | |
4884 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
4885 | ||
4886 | if (zio_injection_enabled) | |
4887 | zio_handle_panic_injection(spa, FTAG, 1); | |
4888 | ||
4889 | spa_activate(newspa, spa_mode_global); | |
4890 | spa_async_suspend(newspa); | |
4891 | ||
4892 | /* create the new pool from the disks of the original pool */ | |
4893 | error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE); | |
4894 | if (error) | |
4895 | goto out; | |
4896 | ||
4897 | /* if that worked, generate a real config for the new pool */ | |
4898 | if (newspa->spa_root_vdev != NULL) { | |
4899 | VERIFY(nvlist_alloc(&newspa->spa_config_splitting, | |
b8d06fca | 4900 | NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
428870ff BB |
4901 | VERIFY(nvlist_add_uint64(newspa->spa_config_splitting, |
4902 | ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0); | |
4903 | spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL, | |
4904 | B_TRUE)); | |
9babb374 | 4905 | } |
34dc7c2f | 4906 | |
428870ff BB |
4907 | /* set the props */ |
4908 | if (props != NULL) { | |
4909 | spa_configfile_set(newspa, props, B_FALSE); | |
4910 | error = spa_prop_set(newspa, props); | |
4911 | if (error) | |
4912 | goto out; | |
4913 | } | |
34dc7c2f | 4914 | |
428870ff BB |
4915 | /* flush everything */ |
4916 | txg = spa_vdev_config_enter(newspa); | |
4917 | vdev_config_dirty(newspa->spa_root_vdev); | |
4918 | (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG); | |
34dc7c2f | 4919 | |
428870ff BB |
4920 | if (zio_injection_enabled) |
4921 | zio_handle_panic_injection(spa, FTAG, 2); | |
34dc7c2f | 4922 | |
428870ff | 4923 | spa_async_resume(newspa); |
34dc7c2f | 4924 | |
428870ff BB |
4925 | /* finally, update the original pool's config */ |
4926 | txg = spa_vdev_config_enter(spa); | |
4927 | tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir); | |
4928 | error = dmu_tx_assign(tx, TXG_WAIT); | |
4929 | if (error != 0) | |
4930 | dmu_tx_abort(tx); | |
4931 | for (c = 0; c < children; c++) { | |
4932 | if (vml[c] != NULL) { | |
4933 | vdev_split(vml[c]); | |
4934 | if (error == 0) | |
4935 | spa_history_log_internal(LOG_POOL_VDEV_DETACH, | |
4936 | spa, tx, "vdev=%s", | |
4937 | vml[c]->vdev_path); | |
4938 | vdev_free(vml[c]); | |
34dc7c2f | 4939 | } |
34dc7c2f | 4940 | } |
428870ff BB |
4941 | vdev_config_dirty(spa->spa_root_vdev); |
4942 | spa->spa_config_splitting = NULL; | |
4943 | nvlist_free(nvl); | |
4944 | if (error == 0) | |
4945 | dmu_tx_commit(tx); | |
4946 | (void) spa_vdev_exit(spa, NULL, txg, 0); | |
4947 | ||
4948 | if (zio_injection_enabled) | |
4949 | zio_handle_panic_injection(spa, FTAG, 3); | |
4950 | ||
4951 | /* split is complete; log a history record */ | |
4952 | spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL, | |
4953 | "split new pool %s from pool %s", newname, spa_name(spa)); | |
4954 | ||
4955 | kmem_free(vml, children * sizeof (vdev_t *)); | |
4956 | ||
4957 | /* if we're not going to mount the filesystems in userland, export */ | |
4958 | if (exp) | |
4959 | error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL, | |
4960 | B_FALSE, B_FALSE); | |
4961 | ||
4962 | return (error); | |
4963 | ||
4964 | out: | |
4965 | spa_unload(newspa); | |
4966 | spa_deactivate(newspa); | |
4967 | spa_remove(newspa); | |
4968 | ||
4969 | txg = spa_vdev_config_enter(spa); | |
4970 | ||
4971 | /* re-online all offlined disks */ | |
4972 | for (c = 0; c < children; c++) { | |
4973 | if (vml[c] != NULL) | |
4974 | vml[c]->vdev_offline = B_FALSE; | |
4975 | } | |
4976 | vdev_reopen(spa->spa_root_vdev); | |
4977 | ||
4978 | nvlist_free(spa->spa_config_splitting); | |
4979 | spa->spa_config_splitting = NULL; | |
4980 | (void) spa_vdev_exit(spa, NULL, txg, error); | |
34dc7c2f | 4981 | |
428870ff | 4982 | kmem_free(vml, children * sizeof (vdev_t *)); |
34dc7c2f BB |
4983 | return (error); |
4984 | } | |
4985 | ||
b128c09f BB |
4986 | static nvlist_t * |
4987 | spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid) | |
34dc7c2f | 4988 | { |
d6320ddb BB |
4989 | int i; |
4990 | ||
4991 | for (i = 0; i < count; i++) { | |
b128c09f | 4992 | uint64_t guid; |
34dc7c2f | 4993 | |
b128c09f BB |
4994 | VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID, |
4995 | &guid) == 0); | |
34dc7c2f | 4996 | |
b128c09f BB |
4997 | if (guid == target_guid) |
4998 | return (nvpp[i]); | |
34dc7c2f BB |
4999 | } |
5000 | ||
b128c09f | 5001 | return (NULL); |
34dc7c2f BB |
5002 | } |
5003 | ||
b128c09f BB |
5004 | static void |
5005 | spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count, | |
5006 | nvlist_t *dev_to_remove) | |
34dc7c2f | 5007 | { |
b128c09f | 5008 | nvlist_t **newdev = NULL; |
d6320ddb | 5009 | int i, j; |
34dc7c2f | 5010 | |
b128c09f | 5011 | if (count > 1) |
b8d06fca | 5012 | newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE); |
34dc7c2f | 5013 | |
d6320ddb | 5014 | for (i = 0, j = 0; i < count; i++) { |
b128c09f BB |
5015 | if (dev[i] == dev_to_remove) |
5016 | continue; | |
b8d06fca | 5017 | VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0); |
34dc7c2f BB |
5018 | } |
5019 | ||
b128c09f BB |
5020 | VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0); |
5021 | VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0); | |
34dc7c2f | 5022 | |
d6320ddb | 5023 | for (i = 0; i < count - 1; i++) |
b128c09f | 5024 | nvlist_free(newdev[i]); |
34dc7c2f | 5025 | |
b128c09f BB |
5026 | if (count > 1) |
5027 | kmem_free(newdev, (count - 1) * sizeof (void *)); | |
34dc7c2f BB |
5028 | } |
5029 | ||
428870ff BB |
5030 | /* |
5031 | * Evacuate the device. | |
5032 | */ | |
5033 | static int | |
5034 | spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd) | |
5035 | { | |
5036 | uint64_t txg; | |
5037 | int error = 0; | |
5038 | ||
5039 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
5040 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
5041 | ASSERT(vd == vd->vdev_top); | |
5042 | ||
5043 | /* | |
5044 | * Evacuate the device. We don't hold the config lock as writer | |
5045 | * since we need to do I/O but we do keep the | |
5046 | * spa_namespace_lock held. Once this completes the device | |
5047 | * should no longer have any blocks allocated on it. | |
5048 | */ | |
5049 | if (vd->vdev_islog) { | |
5050 | if (vd->vdev_stat.vs_alloc != 0) | |
5051 | error = spa_offline_log(spa); | |
5052 | } else { | |
5053 | error = ENOTSUP; | |
5054 | } | |
5055 | ||
5056 | if (error) | |
5057 | return (error); | |
5058 | ||
5059 | /* | |
5060 | * The evacuation succeeded. Remove any remaining MOS metadata | |
5061 | * associated with this vdev, and wait for these changes to sync. | |
5062 | */ | |
5063 | ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0); | |
5064 | txg = spa_vdev_config_enter(spa); | |
5065 | vd->vdev_removing = B_TRUE; | |
5066 | vdev_dirty(vd, 0, NULL, txg); | |
5067 | vdev_config_dirty(vd); | |
5068 | spa_vdev_config_exit(spa, NULL, txg, 0, FTAG); | |
5069 | ||
5070 | return (0); | |
5071 | } | |
5072 | ||
5073 | /* | |
5074 | * Complete the removal by cleaning up the namespace. | |
5075 | */ | |
5076 | static void | |
5077 | spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd) | |
5078 | { | |
5079 | vdev_t *rvd = spa->spa_root_vdev; | |
5080 | uint64_t id = vd->vdev_id; | |
5081 | boolean_t last_vdev = (id == (rvd->vdev_children - 1)); | |
5082 | ||
5083 | ASSERT(MUTEX_HELD(&spa_namespace_lock)); | |
5084 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL); | |
5085 | ASSERT(vd == vd->vdev_top); | |
5086 | ||
5087 | /* | |
5088 | * Only remove any devices which are empty. | |
5089 | */ | |
5090 | if (vd->vdev_stat.vs_alloc != 0) | |
5091 | return; | |
5092 | ||
5093 | (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE); | |
5094 | ||
5095 | if (list_link_active(&vd->vdev_state_dirty_node)) | |
5096 | vdev_state_clean(vd); | |
5097 | if (list_link_active(&vd->vdev_config_dirty_node)) | |
5098 | vdev_config_clean(vd); | |
5099 | ||
5100 | vdev_free(vd); | |
5101 | ||
5102 | if (last_vdev) { | |
5103 | vdev_compact_children(rvd); | |
5104 | } else { | |
5105 | vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops); | |
5106 | vdev_add_child(rvd, vd); | |
5107 | } | |
5108 | vdev_config_dirty(rvd); | |
5109 | ||
5110 | /* | |
5111 | * Reassess the health of our root vdev. | |
5112 | */ | |
5113 | vdev_reopen(rvd); | |
5114 | } | |
5115 | ||
5116 | /* | |
5117 | * Remove a device from the pool - | |
5118 | * | |
5119 | * Removing a device from the vdev namespace requires several steps | |
5120 | * and can take a significant amount of time. As a result we use | |
5121 | * the spa_vdev_config_[enter/exit] functions which allow us to | |
5122 | * grab and release the spa_config_lock while still holding the namespace | |
5123 | * lock. During each step the configuration is synced out. | |
5124 | */ | |
5125 | ||
34dc7c2f BB |
5126 | /* |
5127 | * Remove a device from the pool. Currently, this supports removing only hot | |
428870ff | 5128 | * spares, slogs, and level 2 ARC devices. |
34dc7c2f BB |
5129 | */ |
5130 | int | |
5131 | spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare) | |
5132 | { | |
5133 | vdev_t *vd; | |
428870ff | 5134 | metaslab_group_t *mg; |
b128c09f | 5135 | nvlist_t **spares, **l2cache, *nv; |
fb5f0bc8 | 5136 | uint64_t txg = 0; |
428870ff | 5137 | uint_t nspares, nl2cache; |
34dc7c2f | 5138 | int error = 0; |
fb5f0bc8 | 5139 | boolean_t locked = MUTEX_HELD(&spa_namespace_lock); |
34dc7c2f | 5140 | |
572e2857 BB |
5141 | ASSERT(spa_writeable(spa)); |
5142 | ||
fb5f0bc8 BB |
5143 | if (!locked) |
5144 | txg = spa_vdev_enter(spa); | |
34dc7c2f | 5145 | |
b128c09f | 5146 | vd = spa_lookup_by_guid(spa, guid, B_FALSE); |
34dc7c2f BB |
5147 | |
5148 | if (spa->spa_spares.sav_vdevs != NULL && | |
34dc7c2f | 5149 | nvlist_lookup_nvlist_array(spa->spa_spares.sav_config, |
b128c09f BB |
5150 | ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 && |
5151 | (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) { | |
5152 | /* | |
5153 | * Only remove the hot spare if it's not currently in use | |
5154 | * in this pool. | |
5155 | */ | |
5156 | if (vd == NULL || unspare) { | |
5157 | spa_vdev_remove_aux(spa->spa_spares.sav_config, | |
5158 | ZPOOL_CONFIG_SPARES, spares, nspares, nv); | |
5159 | spa_load_spares(spa); | |
5160 | spa->spa_spares.sav_sync = B_TRUE; | |
5161 | } else { | |
5162 | error = EBUSY; | |
5163 | } | |
5164 | } else if (spa->spa_l2cache.sav_vdevs != NULL && | |
34dc7c2f | 5165 | nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config, |
b128c09f BB |
5166 | ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 && |
5167 | (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) { | |
5168 | /* | |
5169 | * Cache devices can always be removed. | |
5170 | */ | |
5171 | spa_vdev_remove_aux(spa->spa_l2cache.sav_config, | |
5172 | ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv); | |
34dc7c2f BB |
5173 | spa_load_l2cache(spa); |
5174 | spa->spa_l2cache.sav_sync = B_TRUE; | |
428870ff BB |
5175 | } else if (vd != NULL && vd->vdev_islog) { |
5176 | ASSERT(!locked); | |
5177 | ASSERT(vd == vd->vdev_top); | |
5178 | ||
5179 | /* | |
5180 | * XXX - Once we have bp-rewrite this should | |
5181 | * become the common case. | |
5182 | */ | |
5183 | ||
5184 | mg = vd->vdev_mg; | |
5185 | ||
5186 | /* | |
5187 | * Stop allocating from this vdev. | |
5188 | */ | |
5189 | metaslab_group_passivate(mg); | |
5190 | ||
5191 | /* | |
5192 | * Wait for the youngest allocations and frees to sync, | |
5193 | * and then wait for the deferral of those frees to finish. | |
5194 | */ | |
5195 | spa_vdev_config_exit(spa, NULL, | |
5196 | txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG); | |
5197 | ||
5198 | /* | |
5199 | * Attempt to evacuate the vdev. | |
5200 | */ | |
5201 | error = spa_vdev_remove_evacuate(spa, vd); | |
5202 | ||
5203 | txg = spa_vdev_config_enter(spa); | |
5204 | ||
5205 | /* | |
5206 | * If we couldn't evacuate the vdev, unwind. | |
5207 | */ | |
5208 | if (error) { | |
5209 | metaslab_group_activate(mg); | |
5210 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5211 | } | |
5212 | ||
5213 | /* | |
5214 | * Clean up the vdev namespace. | |
5215 | */ | |
5216 | spa_vdev_remove_from_namespace(spa, vd); | |
5217 | ||
b128c09f BB |
5218 | } else if (vd != NULL) { |
5219 | /* | |
5220 | * Normal vdevs cannot be removed (yet). | |
5221 | */ | |
5222 | error = ENOTSUP; | |
5223 | } else { | |
5224 | /* | |
5225 | * There is no vdev of any kind with the specified guid. | |
5226 | */ | |
5227 | error = ENOENT; | |
34dc7c2f BB |
5228 | } |
5229 | ||
fb5f0bc8 BB |
5230 | if (!locked) |
5231 | return (spa_vdev_exit(spa, NULL, txg, error)); | |
5232 | ||
5233 | return (error); | |
34dc7c2f BB |
5234 | } |
5235 | ||
5236 | /* | |
5237 | * Find any device that's done replacing, or a vdev marked 'unspare' that's | |
5238 | * current spared, so we can detach it. | |
5239 | */ | |
5240 | static vdev_t * | |
5241 | spa_vdev_resilver_done_hunt(vdev_t *vd) | |
5242 | { | |
5243 | vdev_t *newvd, *oldvd; | |
d6320ddb | 5244 | int c; |
34dc7c2f | 5245 | |
d6320ddb | 5246 | for (c = 0; c < vd->vdev_children; c++) { |
34dc7c2f BB |
5247 | oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]); |
5248 | if (oldvd != NULL) | |
5249 | return (oldvd); | |
5250 | } | |
5251 | ||
5252 | /* | |
572e2857 BB |
5253 | * Check for a completed replacement. We always consider the first |
5254 | * vdev in the list to be the oldest vdev, and the last one to be | |
5255 | * the newest (see spa_vdev_attach() for how that works). In | |
5256 | * the case where the newest vdev is faulted, we will not automatically | |
5257 | * remove it after a resilver completes. This is OK as it will require | |
5258 | * user intervention to determine which disk the admin wishes to keep. | |
34dc7c2f | 5259 | */ |
572e2857 BB |
5260 | if (vd->vdev_ops == &vdev_replacing_ops) { |
5261 | ASSERT(vd->vdev_children > 1); | |
5262 | ||
5263 | newvd = vd->vdev_child[vd->vdev_children - 1]; | |
34dc7c2f | 5264 | oldvd = vd->vdev_child[0]; |
34dc7c2f | 5265 | |
fb5f0bc8 | 5266 | if (vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 5267 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
fb5f0bc8 | 5268 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 5269 | return (oldvd); |
34dc7c2f BB |
5270 | } |
5271 | ||
5272 | /* | |
5273 | * Check for a completed resilver with the 'unspare' flag set. | |
5274 | */ | |
572e2857 BB |
5275 | if (vd->vdev_ops == &vdev_spare_ops) { |
5276 | vdev_t *first = vd->vdev_child[0]; | |
5277 | vdev_t *last = vd->vdev_child[vd->vdev_children - 1]; | |
5278 | ||
5279 | if (last->vdev_unspare) { | |
5280 | oldvd = first; | |
5281 | newvd = last; | |
5282 | } else if (first->vdev_unspare) { | |
5283 | oldvd = last; | |
5284 | newvd = first; | |
5285 | } else { | |
5286 | oldvd = NULL; | |
5287 | } | |
34dc7c2f | 5288 | |
572e2857 | 5289 | if (oldvd != NULL && |
fb5f0bc8 | 5290 | vdev_dtl_empty(newvd, DTL_MISSING) && |
428870ff | 5291 | vdev_dtl_empty(newvd, DTL_OUTAGE) && |
572e2857 | 5292 | !vdev_dtl_required(oldvd)) |
34dc7c2f | 5293 | return (oldvd); |
572e2857 BB |
5294 | |
5295 | /* | |
5296 | * If there are more than two spares attached to a disk, | |
5297 | * and those spares are not required, then we want to | |
5298 | * attempt to free them up now so that they can be used | |
5299 | * by other pools. Once we're back down to a single | |
5300 | * disk+spare, we stop removing them. | |
5301 | */ | |
5302 | if (vd->vdev_children > 2) { | |
5303 | newvd = vd->vdev_child[1]; | |
5304 | ||
5305 | if (newvd->vdev_isspare && last->vdev_isspare && | |
5306 | vdev_dtl_empty(last, DTL_MISSING) && | |
5307 | vdev_dtl_empty(last, DTL_OUTAGE) && | |
5308 | !vdev_dtl_required(newvd)) | |
5309 | return (newvd); | |
34dc7c2f | 5310 | } |
34dc7c2f BB |
5311 | } |
5312 | ||
5313 | return (NULL); | |
5314 | } | |
5315 | ||
5316 | static void | |
5317 | spa_vdev_resilver_done(spa_t *spa) | |
5318 | { | |
fb5f0bc8 BB |
5319 | vdev_t *vd, *pvd, *ppvd; |
5320 | uint64_t guid, sguid, pguid, ppguid; | |
34dc7c2f | 5321 | |
fb5f0bc8 | 5322 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5323 | |
5324 | while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) { | |
fb5f0bc8 BB |
5325 | pvd = vd->vdev_parent; |
5326 | ppvd = pvd->vdev_parent; | |
34dc7c2f | 5327 | guid = vd->vdev_guid; |
fb5f0bc8 BB |
5328 | pguid = pvd->vdev_guid; |
5329 | ppguid = ppvd->vdev_guid; | |
5330 | sguid = 0; | |
34dc7c2f BB |
5331 | /* |
5332 | * If we have just finished replacing a hot spared device, then | |
5333 | * we need to detach the parent's first child (the original hot | |
5334 | * spare) as well. | |
5335 | */ | |
572e2857 BB |
5336 | if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 && |
5337 | ppvd->vdev_children == 2) { | |
34dc7c2f | 5338 | ASSERT(pvd->vdev_ops == &vdev_replacing_ops); |
fb5f0bc8 | 5339 | sguid = ppvd->vdev_child[1]->vdev_guid; |
34dc7c2f | 5340 | } |
fb5f0bc8 BB |
5341 | spa_config_exit(spa, SCL_ALL, FTAG); |
5342 | if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0) | |
34dc7c2f | 5343 | return; |
fb5f0bc8 | 5344 | if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0) |
34dc7c2f | 5345 | return; |
fb5f0bc8 | 5346 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
5347 | } |
5348 | ||
fb5f0bc8 | 5349 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
5350 | } |
5351 | ||
5352 | /* | |
428870ff | 5353 | * Update the stored path or FRU for this vdev. |
34dc7c2f BB |
5354 | */ |
5355 | int | |
9babb374 BB |
5356 | spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value, |
5357 | boolean_t ispath) | |
34dc7c2f | 5358 | { |
b128c09f | 5359 | vdev_t *vd; |
428870ff | 5360 | boolean_t sync = B_FALSE; |
34dc7c2f | 5361 | |
572e2857 BB |
5362 | ASSERT(spa_writeable(spa)); |
5363 | ||
428870ff | 5364 | spa_vdev_state_enter(spa, SCL_ALL); |
34dc7c2f | 5365 | |
9babb374 | 5366 | if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) |
428870ff | 5367 | return (spa_vdev_state_exit(spa, NULL, ENOENT)); |
34dc7c2f BB |
5368 | |
5369 | if (!vd->vdev_ops->vdev_op_leaf) | |
428870ff | 5370 | return (spa_vdev_state_exit(spa, NULL, ENOTSUP)); |
34dc7c2f | 5371 | |
9babb374 | 5372 | if (ispath) { |
428870ff BB |
5373 | if (strcmp(value, vd->vdev_path) != 0) { |
5374 | spa_strfree(vd->vdev_path); | |
5375 | vd->vdev_path = spa_strdup(value); | |
5376 | sync = B_TRUE; | |
5377 | } | |
9babb374 | 5378 | } else { |
428870ff BB |
5379 | if (vd->vdev_fru == NULL) { |
5380 | vd->vdev_fru = spa_strdup(value); | |
5381 | sync = B_TRUE; | |
5382 | } else if (strcmp(value, vd->vdev_fru) != 0) { | |
9babb374 | 5383 | spa_strfree(vd->vdev_fru); |
428870ff BB |
5384 | vd->vdev_fru = spa_strdup(value); |
5385 | sync = B_TRUE; | |
5386 | } | |
9babb374 | 5387 | } |
34dc7c2f | 5388 | |
428870ff | 5389 | return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0)); |
34dc7c2f BB |
5390 | } |
5391 | ||
9babb374 BB |
5392 | int |
5393 | spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath) | |
5394 | { | |
5395 | return (spa_vdev_set_common(spa, guid, newpath, B_TRUE)); | |
5396 | } | |
5397 | ||
5398 | int | |
5399 | spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru) | |
5400 | { | |
5401 | return (spa_vdev_set_common(spa, guid, newfru, B_FALSE)); | |
5402 | } | |
5403 | ||
34dc7c2f BB |
5404 | /* |
5405 | * ========================================================================== | |
428870ff | 5406 | * SPA Scanning |
34dc7c2f BB |
5407 | * ========================================================================== |
5408 | */ | |
5409 | ||
34dc7c2f | 5410 | int |
428870ff BB |
5411 | spa_scan_stop(spa_t *spa) |
5412 | { | |
5413 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); | |
5414 | if (dsl_scan_resilvering(spa->spa_dsl_pool)) | |
5415 | return (EBUSY); | |
5416 | return (dsl_scan_cancel(spa->spa_dsl_pool)); | |
5417 | } | |
5418 | ||
5419 | int | |
5420 | spa_scan(spa_t *spa, pool_scan_func_t func) | |
34dc7c2f | 5421 | { |
b128c09f | 5422 | ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0); |
34dc7c2f | 5423 | |
428870ff | 5424 | if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE) |
34dc7c2f BB |
5425 | return (ENOTSUP); |
5426 | ||
34dc7c2f | 5427 | /* |
b128c09f BB |
5428 | * If a resilver was requested, but there is no DTL on a |
5429 | * writeable leaf device, we have nothing to do. | |
34dc7c2f | 5430 | */ |
428870ff | 5431 | if (func == POOL_SCAN_RESILVER && |
b128c09f BB |
5432 | !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) { |
5433 | spa_async_request(spa, SPA_ASYNC_RESILVER_DONE); | |
34dc7c2f BB |
5434 | return (0); |
5435 | } | |
5436 | ||
428870ff | 5437 | return (dsl_scan(spa->spa_dsl_pool, func)); |
34dc7c2f BB |
5438 | } |
5439 | ||
5440 | /* | |
5441 | * ========================================================================== | |
5442 | * SPA async task processing | |
5443 | * ========================================================================== | |
5444 | */ | |
5445 | ||
5446 | static void | |
5447 | spa_async_remove(spa_t *spa, vdev_t *vd) | |
5448 | { | |
d6320ddb BB |
5449 | int c; |
5450 | ||
b128c09f | 5451 | if (vd->vdev_remove_wanted) { |
428870ff BB |
5452 | vd->vdev_remove_wanted = B_FALSE; |
5453 | vd->vdev_delayed_close = B_FALSE; | |
b128c09f | 5454 | vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE); |
428870ff BB |
5455 | |
5456 | /* | |
5457 | * We want to clear the stats, but we don't want to do a full | |
5458 | * vdev_clear() as that will cause us to throw away | |
5459 | * degraded/faulted state as well as attempt to reopen the | |
5460 | * device, all of which is a waste. | |
5461 | */ | |
5462 | vd->vdev_stat.vs_read_errors = 0; | |
5463 | vd->vdev_stat.vs_write_errors = 0; | |
5464 | vd->vdev_stat.vs_checksum_errors = 0; | |
5465 | ||
b128c09f BB |
5466 | vdev_state_dirty(vd->vdev_top); |
5467 | } | |
34dc7c2f | 5468 | |
d6320ddb | 5469 | for (c = 0; c < vd->vdev_children; c++) |
b128c09f BB |
5470 | spa_async_remove(spa, vd->vdev_child[c]); |
5471 | } | |
5472 | ||
5473 | static void | |
5474 | spa_async_probe(spa_t *spa, vdev_t *vd) | |
5475 | { | |
d6320ddb BB |
5476 | int c; |
5477 | ||
b128c09f | 5478 | if (vd->vdev_probe_wanted) { |
428870ff | 5479 | vd->vdev_probe_wanted = B_FALSE; |
b128c09f | 5480 | vdev_reopen(vd); /* vdev_open() does the actual probe */ |
34dc7c2f | 5481 | } |
b128c09f | 5482 | |
d6320ddb | 5483 | for (c = 0; c < vd->vdev_children; c++) |
b128c09f | 5484 | spa_async_probe(spa, vd->vdev_child[c]); |
34dc7c2f BB |
5485 | } |
5486 | ||
9babb374 BB |
5487 | static void |
5488 | spa_async_autoexpand(spa_t *spa, vdev_t *vd) | |
5489 | { | |
d6320ddb | 5490 | int c; |
9babb374 BB |
5491 | |
5492 | if (!spa->spa_autoexpand) | |
5493 | return; | |
5494 | ||
d6320ddb | 5495 | for (c = 0; c < vd->vdev_children; c++) { |
9babb374 BB |
5496 | vdev_t *cvd = vd->vdev_child[c]; |
5497 | spa_async_autoexpand(spa, cvd); | |
5498 | } | |
5499 | ||
5500 | if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL) | |
5501 | return; | |
5502 | ||
26685276 | 5503 | spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND); |
9babb374 BB |
5504 | } |
5505 | ||
34dc7c2f BB |
5506 | static void |
5507 | spa_async_thread(spa_t *spa) | |
5508 | { | |
d6320ddb | 5509 | int tasks, i; |
34dc7c2f BB |
5510 | |
5511 | ASSERT(spa->spa_sync_on); | |
5512 | ||
5513 | mutex_enter(&spa->spa_async_lock); | |
5514 | tasks = spa->spa_async_tasks; | |
5515 | spa->spa_async_tasks = 0; | |
5516 | mutex_exit(&spa->spa_async_lock); | |
5517 | ||
5518 | /* | |
5519 | * See if the config needs to be updated. | |
5520 | */ | |
5521 | if (tasks & SPA_ASYNC_CONFIG_UPDATE) { | |
428870ff | 5522 | uint64_t old_space, new_space; |
9babb374 | 5523 | |
34dc7c2f | 5524 | mutex_enter(&spa_namespace_lock); |
428870ff | 5525 | old_space = metaslab_class_get_space(spa_normal_class(spa)); |
34dc7c2f | 5526 | spa_config_update(spa, SPA_CONFIG_UPDATE_POOL); |
428870ff | 5527 | new_space = metaslab_class_get_space(spa_normal_class(spa)); |
34dc7c2f | 5528 | mutex_exit(&spa_namespace_lock); |
9babb374 BB |
5529 | |
5530 | /* | |
5531 | * If the pool grew as a result of the config update, | |
5532 | * then log an internal history event. | |
5533 | */ | |
428870ff BB |
5534 | if (new_space != old_space) { |
5535 | spa_history_log_internal(LOG_POOL_VDEV_ONLINE, | |
5536 | spa, NULL, | |
45d1cae3 | 5537 | "pool '%s' size: %llu(+%llu)", |
428870ff | 5538 | spa_name(spa), new_space, new_space - old_space); |
9babb374 | 5539 | } |
34dc7c2f BB |
5540 | } |
5541 | ||
5542 | /* | |
5543 | * See if any devices need to be marked REMOVED. | |
34dc7c2f | 5544 | */ |
b128c09f | 5545 | if (tasks & SPA_ASYNC_REMOVE) { |
428870ff | 5546 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f | 5547 | spa_async_remove(spa, spa->spa_root_vdev); |
d6320ddb | 5548 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) |
b128c09f | 5549 | spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]); |
d6320ddb | 5550 | for (i = 0; i < spa->spa_spares.sav_count; i++) |
b128c09f BB |
5551 | spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]); |
5552 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
34dc7c2f BB |
5553 | } |
5554 | ||
9babb374 BB |
5555 | if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) { |
5556 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); | |
5557 | spa_async_autoexpand(spa, spa->spa_root_vdev); | |
5558 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
5559 | } | |
5560 | ||
34dc7c2f | 5561 | /* |
b128c09f | 5562 | * See if any devices need to be probed. |
34dc7c2f | 5563 | */ |
b128c09f | 5564 | if (tasks & SPA_ASYNC_PROBE) { |
428870ff | 5565 | spa_vdev_state_enter(spa, SCL_NONE); |
b128c09f BB |
5566 | spa_async_probe(spa, spa->spa_root_vdev); |
5567 | (void) spa_vdev_state_exit(spa, NULL, 0); | |
5568 | } | |
34dc7c2f BB |
5569 | |
5570 | /* | |
b128c09f | 5571 | * If any devices are done replacing, detach them. |
34dc7c2f | 5572 | */ |
b128c09f BB |
5573 | if (tasks & SPA_ASYNC_RESILVER_DONE) |
5574 | spa_vdev_resilver_done(spa); | |
34dc7c2f BB |
5575 | |
5576 | /* | |
5577 | * Kick off a resilver. | |
5578 | */ | |
b128c09f | 5579 | if (tasks & SPA_ASYNC_RESILVER) |
428870ff | 5580 | dsl_resilver_restart(spa->spa_dsl_pool, 0); |
34dc7c2f BB |
5581 | |
5582 | /* | |
5583 | * Let the world know that we're done. | |
5584 | */ | |
5585 | mutex_enter(&spa->spa_async_lock); | |
5586 | spa->spa_async_thread = NULL; | |
5587 | cv_broadcast(&spa->spa_async_cv); | |
5588 | mutex_exit(&spa->spa_async_lock); | |
5589 | thread_exit(); | |
5590 | } | |
5591 | ||
5592 | void | |
5593 | spa_async_suspend(spa_t *spa) | |
5594 | { | |
5595 | mutex_enter(&spa->spa_async_lock); | |
5596 | spa->spa_async_suspended++; | |
5597 | while (spa->spa_async_thread != NULL) | |
5598 | cv_wait(&spa->spa_async_cv, &spa->spa_async_lock); | |
5599 | mutex_exit(&spa->spa_async_lock); | |
5600 | } | |
5601 | ||
5602 | void | |
5603 | spa_async_resume(spa_t *spa) | |
5604 | { | |
5605 | mutex_enter(&spa->spa_async_lock); | |
5606 | ASSERT(spa->spa_async_suspended != 0); | |
5607 | spa->spa_async_suspended--; | |
5608 | mutex_exit(&spa->spa_async_lock); | |
5609 | } | |
5610 | ||
5611 | static void | |
5612 | spa_async_dispatch(spa_t *spa) | |
5613 | { | |
5614 | mutex_enter(&spa->spa_async_lock); | |
5615 | if (spa->spa_async_tasks && !spa->spa_async_suspended && | |
5616 | spa->spa_async_thread == NULL && | |
5617 | rootdir != NULL && !vn_is_readonly(rootdir)) | |
5618 | spa->spa_async_thread = thread_create(NULL, 0, | |
5619 | spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri); | |
5620 | mutex_exit(&spa->spa_async_lock); | |
5621 | } | |
5622 | ||
5623 | void | |
5624 | spa_async_request(spa_t *spa, int task) | |
5625 | { | |
428870ff | 5626 | zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task); |
34dc7c2f BB |
5627 | mutex_enter(&spa->spa_async_lock); |
5628 | spa->spa_async_tasks |= task; | |
5629 | mutex_exit(&spa->spa_async_lock); | |
5630 | } | |
5631 | ||
5632 | /* | |
5633 | * ========================================================================== | |
5634 | * SPA syncing routines | |
5635 | * ========================================================================== | |
5636 | */ | |
5637 | ||
428870ff BB |
5638 | static int |
5639 | bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
34dc7c2f | 5640 | { |
428870ff BB |
5641 | bpobj_t *bpo = arg; |
5642 | bpobj_enqueue(bpo, bp, tx); | |
5643 | return (0); | |
5644 | } | |
34dc7c2f | 5645 | |
428870ff BB |
5646 | static int |
5647 | spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx) | |
5648 | { | |
5649 | zio_t *zio = arg; | |
34dc7c2f | 5650 | |
428870ff BB |
5651 | zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp, |
5652 | zio->io_flags)); | |
5653 | return (0); | |
34dc7c2f BB |
5654 | } |
5655 | ||
5656 | static void | |
5657 | spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx) | |
5658 | { | |
5659 | char *packed = NULL; | |
b128c09f | 5660 | size_t bufsize; |
34dc7c2f BB |
5661 | size_t nvsize = 0; |
5662 | dmu_buf_t *db; | |
5663 | ||
5664 | VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0); | |
5665 | ||
b128c09f BB |
5666 | /* |
5667 | * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration | |
5668 | * information. This avoids the dbuf_will_dirty() path and | |
5669 | * saves us a pre-read to get data we don't actually care about. | |
5670 | */ | |
9ae529ec | 5671 | bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE); |
b8d06fca | 5672 | packed = vmem_alloc(bufsize, KM_PUSHPAGE); |
34dc7c2f BB |
5673 | |
5674 | VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR, | |
b8d06fca | 5675 | KM_PUSHPAGE) == 0); |
b128c09f | 5676 | bzero(packed + nvsize, bufsize - nvsize); |
34dc7c2f | 5677 | |
b128c09f | 5678 | dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx); |
34dc7c2f | 5679 | |
00b46022 | 5680 | vmem_free(packed, bufsize); |
34dc7c2f BB |
5681 | |
5682 | VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db)); | |
5683 | dmu_buf_will_dirty(db, tx); | |
5684 | *(uint64_t *)db->db_data = nvsize; | |
5685 | dmu_buf_rele(db, FTAG); | |
5686 | } | |
5687 | ||
5688 | static void | |
5689 | spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx, | |
5690 | const char *config, const char *entry) | |
5691 | { | |
5692 | nvlist_t *nvroot; | |
5693 | nvlist_t **list; | |
5694 | int i; | |
5695 | ||
5696 | if (!sav->sav_sync) | |
5697 | return; | |
5698 | ||
5699 | /* | |
5700 | * Update the MOS nvlist describing the list of available devices. | |
5701 | * spa_validate_aux() will have already made sure this nvlist is | |
5702 | * valid and the vdevs are labeled appropriately. | |
5703 | */ | |
5704 | if (sav->sav_object == 0) { | |
5705 | sav->sav_object = dmu_object_alloc(spa->spa_meta_objset, | |
5706 | DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE, | |
5707 | sizeof (uint64_t), tx); | |
5708 | VERIFY(zap_update(spa->spa_meta_objset, | |
5709 | DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1, | |
5710 | &sav->sav_object, tx) == 0); | |
5711 | } | |
5712 | ||
b8d06fca | 5713 | VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0); |
34dc7c2f BB |
5714 | if (sav->sav_count == 0) { |
5715 | VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0); | |
5716 | } else { | |
b8d06fca | 5717 | list = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE); |
34dc7c2f BB |
5718 | for (i = 0; i < sav->sav_count; i++) |
5719 | list[i] = vdev_config_generate(spa, sav->sav_vdevs[i], | |
428870ff | 5720 | B_FALSE, VDEV_CONFIG_L2CACHE); |
34dc7c2f BB |
5721 | VERIFY(nvlist_add_nvlist_array(nvroot, config, list, |
5722 | sav->sav_count) == 0); | |
5723 | for (i = 0; i < sav->sav_count; i++) | |
5724 | nvlist_free(list[i]); | |
5725 | kmem_free(list, sav->sav_count * sizeof (void *)); | |
5726 | } | |
5727 | ||
5728 | spa_sync_nvlist(spa, sav->sav_object, nvroot, tx); | |
5729 | nvlist_free(nvroot); | |
5730 | ||
5731 | sav->sav_sync = B_FALSE; | |
5732 | } | |
5733 | ||
5734 | static void | |
5735 | spa_sync_config_object(spa_t *spa, dmu_tx_t *tx) | |
5736 | { | |
5737 | nvlist_t *config; | |
5738 | ||
b128c09f | 5739 | if (list_is_empty(&spa->spa_config_dirty_list)) |
34dc7c2f BB |
5740 | return; |
5741 | ||
b128c09f BB |
5742 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); |
5743 | ||
5744 | config = spa_config_generate(spa, spa->spa_root_vdev, | |
5745 | dmu_tx_get_txg(tx), B_FALSE); | |
5746 | ||
ea0b2538 GW |
5747 | /* |
5748 | * If we're upgrading the spa version then make sure that | |
5749 | * the config object gets updated with the correct version. | |
5750 | */ | |
5751 | if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version) | |
5752 | fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION, | |
5753 | spa->spa_uberblock.ub_version); | |
5754 | ||
b128c09f | 5755 | spa_config_exit(spa, SCL_STATE, FTAG); |
34dc7c2f BB |
5756 | |
5757 | if (spa->spa_config_syncing) | |
5758 | nvlist_free(spa->spa_config_syncing); | |
5759 | spa->spa_config_syncing = config; | |
5760 | ||
5761 | spa_sync_nvlist(spa, spa->spa_config_object, config, tx); | |
5762 | } | |
5763 | ||
9ae529ec CS |
5764 | static void |
5765 | spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx) | |
5766 | { | |
5767 | spa_t *spa = arg1; | |
5768 | uint64_t version = *(uint64_t *)arg2; | |
5769 | ||
5770 | /* | |
5771 | * Setting the version is special cased when first creating the pool. | |
5772 | */ | |
5773 | ASSERT(tx->tx_txg != TXG_INITIAL); | |
5774 | ||
8dca0a9a | 5775 | ASSERT(SPA_VERSION_IS_SUPPORTED(version)); |
9ae529ec CS |
5776 | ASSERT(version >= spa_version(spa)); |
5777 | ||
5778 | spa->spa_uberblock.ub_version = version; | |
5779 | vdev_config_dirty(spa->spa_root_vdev); | |
5780 | } | |
5781 | ||
34dc7c2f BB |
5782 | /* |
5783 | * Set zpool properties. | |
5784 | */ | |
5785 | static void | |
428870ff | 5786 | spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx) |
34dc7c2f BB |
5787 | { |
5788 | spa_t *spa = arg1; | |
5789 | objset_t *mos = spa->spa_meta_objset; | |
5790 | nvlist_t *nvp = arg2; | |
9ae529ec | 5791 | nvpair_t *elem = NULL; |
b128c09f BB |
5792 | |
5793 | mutex_enter(&spa->spa_props_lock); | |
34dc7c2f | 5794 | |
34dc7c2f | 5795 | while ((elem = nvlist_next_nvpair(nvp, elem))) { |
9ae529ec CS |
5796 | uint64_t intval; |
5797 | char *strval, *fname; | |
5798 | zpool_prop_t prop; | |
5799 | const char *propname; | |
5800 | zprop_type_t proptype; | |
5801 | zfeature_info_t *feature; | |
5802 | ||
5803 | prop = zpool_name_to_prop(nvpair_name(elem)); | |
5804 | switch ((int)prop) { | |
5805 | case ZPROP_INVAL: | |
5806 | /* | |
5807 | * We checked this earlier in spa_prop_validate(). | |
5808 | */ | |
5809 | ASSERT(zpool_prop_feature(nvpair_name(elem))); | |
5810 | ||
5811 | fname = strchr(nvpair_name(elem), '@') + 1; | |
5812 | VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature)); | |
5813 | ||
5814 | spa_feature_enable(spa, feature, tx); | |
5815 | break; | |
5816 | ||
34dc7c2f | 5817 | case ZPOOL_PROP_VERSION: |
9ae529ec | 5818 | VERIFY(nvpair_value_uint64(elem, &intval) == 0); |
34dc7c2f | 5819 | /* |
9ae529ec CS |
5820 | * The version is synced seperatly before other |
5821 | * properties and should be correct by now. | |
34dc7c2f | 5822 | */ |
9ae529ec | 5823 | ASSERT3U(spa_version(spa), >=, intval); |
34dc7c2f BB |
5824 | break; |
5825 | ||
5826 | case ZPOOL_PROP_ALTROOT: | |
5827 | /* | |
5828 | * 'altroot' is a non-persistent property. It should | |
5829 | * have been set temporarily at creation or import time. | |
5830 | */ | |
5831 | ASSERT(spa->spa_root != NULL); | |
5832 | break; | |
5833 | ||
572e2857 | 5834 | case ZPOOL_PROP_READONLY: |
34dc7c2f BB |
5835 | case ZPOOL_PROP_CACHEFILE: |
5836 | /* | |
572e2857 BB |
5837 | * 'readonly' and 'cachefile' are also non-persisitent |
5838 | * properties. | |
34dc7c2f | 5839 | */ |
34dc7c2f | 5840 | break; |
d96eb2b1 DM |
5841 | case ZPOOL_PROP_COMMENT: |
5842 | VERIFY(nvpair_value_string(elem, &strval) == 0); | |
5843 | if (spa->spa_comment != NULL) | |
5844 | spa_strfree(spa->spa_comment); | |
5845 | spa->spa_comment = spa_strdup(strval); | |
5846 | /* | |
5847 | * We need to dirty the configuration on all the vdevs | |
5848 | * so that their labels get updated. It's unnecessary | |
5849 | * to do this for pool creation since the vdev's | |
5850 | * configuratoin has already been dirtied. | |
5851 | */ | |
5852 | if (tx->tx_txg != TXG_INITIAL) | |
5853 | vdev_config_dirty(spa->spa_root_vdev); | |
5854 | break; | |
34dc7c2f BB |
5855 | default: |
5856 | /* | |
5857 | * Set pool property values in the poolprops mos object. | |
5858 | */ | |
34dc7c2f | 5859 | if (spa->spa_pool_props_object == 0) { |
9ae529ec CS |
5860 | spa->spa_pool_props_object = |
5861 | zap_create_link(mos, DMU_OT_POOL_PROPS, | |
34dc7c2f | 5862 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS, |
9ae529ec | 5863 | tx); |
34dc7c2f | 5864 | } |
34dc7c2f BB |
5865 | |
5866 | /* normalize the property name */ | |
5867 | propname = zpool_prop_to_name(prop); | |
5868 | proptype = zpool_prop_get_type(prop); | |
5869 | ||
5870 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
5871 | ASSERT(proptype == PROP_TYPE_STRING); | |
5872 | VERIFY(nvpair_value_string(elem, &strval) == 0); | |
5873 | VERIFY(zap_update(mos, | |
5874 | spa->spa_pool_props_object, propname, | |
5875 | 1, strlen(strval) + 1, strval, tx) == 0); | |
5876 | ||
5877 | } else if (nvpair_type(elem) == DATA_TYPE_UINT64) { | |
5878 | VERIFY(nvpair_value_uint64(elem, &intval) == 0); | |
5879 | ||
5880 | if (proptype == PROP_TYPE_INDEX) { | |
5881 | const char *unused; | |
5882 | VERIFY(zpool_prop_index_to_string( | |
5883 | prop, intval, &unused) == 0); | |
5884 | } | |
5885 | VERIFY(zap_update(mos, | |
5886 | spa->spa_pool_props_object, propname, | |
5887 | 8, 1, &intval, tx) == 0); | |
5888 | } else { | |
5889 | ASSERT(0); /* not allowed */ | |
5890 | } | |
5891 | ||
5892 | switch (prop) { | |
5893 | case ZPOOL_PROP_DELEGATION: | |
5894 | spa->spa_delegation = intval; | |
5895 | break; | |
5896 | case ZPOOL_PROP_BOOTFS: | |
5897 | spa->spa_bootfs = intval; | |
5898 | break; | |
5899 | case ZPOOL_PROP_FAILUREMODE: | |
5900 | spa->spa_failmode = intval; | |
5901 | break; | |
9babb374 BB |
5902 | case ZPOOL_PROP_AUTOEXPAND: |
5903 | spa->spa_autoexpand = intval; | |
428870ff BB |
5904 | if (tx->tx_txg != TXG_INITIAL) |
5905 | spa_async_request(spa, | |
5906 | SPA_ASYNC_AUTOEXPAND); | |
5907 | break; | |
5908 | case ZPOOL_PROP_DEDUPDITTO: | |
5909 | spa->spa_dedup_ditto = intval; | |
9babb374 | 5910 | break; |
34dc7c2f BB |
5911 | default: |
5912 | break; | |
5913 | } | |
5914 | } | |
5915 | ||
5916 | /* log internal history if this is not a zpool create */ | |
5917 | if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY && | |
5918 | tx->tx_txg != TXG_INITIAL) { | |
428870ff BB |
5919 | spa_history_log_internal(LOG_POOL_PROPSET, |
5920 | spa, tx, "%s %lld %s", | |
b128c09f | 5921 | nvpair_name(elem), intval, spa_name(spa)); |
34dc7c2f BB |
5922 | } |
5923 | } | |
b128c09f BB |
5924 | |
5925 | mutex_exit(&spa->spa_props_lock); | |
34dc7c2f BB |
5926 | } |
5927 | ||
428870ff BB |
5928 | /* |
5929 | * Perform one-time upgrade on-disk changes. spa_version() does not | |
5930 | * reflect the new version this txg, so there must be no changes this | |
5931 | * txg to anything that the upgrade code depends on after it executes. | |
5932 | * Therefore this must be called after dsl_pool_sync() does the sync | |
5933 | * tasks. | |
5934 | */ | |
5935 | static void | |
5936 | spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx) | |
5937 | { | |
5938 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
5939 | ||
5940 | ASSERT(spa->spa_sync_pass == 1); | |
5941 | ||
5942 | if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN && | |
5943 | spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) { | |
5944 | dsl_pool_create_origin(dp, tx); | |
5945 | ||
5946 | /* Keeping the origin open increases spa_minref */ | |
5947 | spa->spa_minref += 3; | |
5948 | } | |
5949 | ||
5950 | if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES && | |
5951 | spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) { | |
5952 | dsl_pool_upgrade_clones(dp, tx); | |
5953 | } | |
5954 | ||
5955 | if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES && | |
5956 | spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) { | |
5957 | dsl_pool_upgrade_dir_clones(dp, tx); | |
5958 | ||
5959 | /* Keeping the freedir open increases spa_minref */ | |
5960 | spa->spa_minref += 3; | |
5961 | } | |
9ae529ec CS |
5962 | |
5963 | if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES && | |
5964 | spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) { | |
5965 | spa_feature_create_zap_objects(spa, tx); | |
5966 | } | |
428870ff BB |
5967 | } |
5968 | ||
34dc7c2f BB |
5969 | /* |
5970 | * Sync the specified transaction group. New blocks may be dirtied as | |
5971 | * part of the process, so we iterate until it converges. | |
5972 | */ | |
5973 | void | |
5974 | spa_sync(spa_t *spa, uint64_t txg) | |
5975 | { | |
5976 | dsl_pool_t *dp = spa->spa_dsl_pool; | |
5977 | objset_t *mos = spa->spa_meta_objset; | |
428870ff BB |
5978 | bpobj_t *defer_bpo = &spa->spa_deferred_bpobj; |
5979 | bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK]; | |
34dc7c2f BB |
5980 | vdev_t *rvd = spa->spa_root_vdev; |
5981 | vdev_t *vd; | |
34dc7c2f | 5982 | dmu_tx_t *tx; |
b128c09f | 5983 | int error; |
d6320ddb | 5984 | int c; |
34dc7c2f | 5985 | |
572e2857 BB |
5986 | VERIFY(spa_writeable(spa)); |
5987 | ||
34dc7c2f BB |
5988 | /* |
5989 | * Lock out configuration changes. | |
5990 | */ | |
b128c09f | 5991 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER); |
34dc7c2f BB |
5992 | |
5993 | spa->spa_syncing_txg = txg; | |
5994 | spa->spa_sync_pass = 0; | |
5995 | ||
b128c09f BB |
5996 | /* |
5997 | * If there are any pending vdev state changes, convert them | |
5998 | * into config changes that go out with this transaction group. | |
5999 | */ | |
6000 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
fb5f0bc8 BB |
6001 | while (list_head(&spa->spa_state_dirty_list) != NULL) { |
6002 | /* | |
6003 | * We need the write lock here because, for aux vdevs, | |
6004 | * calling vdev_config_dirty() modifies sav_config. | |
6005 | * This is ugly and will become unnecessary when we | |
6006 | * eliminate the aux vdev wart by integrating all vdevs | |
6007 | * into the root vdev tree. | |
6008 | */ | |
6009 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6010 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER); | |
6011 | while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) { | |
6012 | vdev_state_clean(vd); | |
6013 | vdev_config_dirty(vd); | |
6014 | } | |
6015 | spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG); | |
6016 | spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER); | |
b128c09f BB |
6017 | } |
6018 | spa_config_exit(spa, SCL_STATE, FTAG); | |
6019 | ||
34dc7c2f BB |
6020 | tx = dmu_tx_create_assigned(dp, txg); |
6021 | ||
cc92e9d0 GW |
6022 | spa->spa_sync_starttime = gethrtime(); |
6023 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); | |
6024 | spa->spa_deadman_tqid = taskq_dispatch_delay(system_taskq, | |
6025 | spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() + | |
6026 | NSEC_TO_TICK(spa->spa_deadman_synctime)); | |
6027 | ||
34dc7c2f BB |
6028 | /* |
6029 | * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg, | |
6030 | * set spa_deflate if we have no raid-z vdevs. | |
6031 | */ | |
6032 | if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE && | |
6033 | spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) { | |
6034 | int i; | |
6035 | ||
6036 | for (i = 0; i < rvd->vdev_children; i++) { | |
6037 | vd = rvd->vdev_child[i]; | |
6038 | if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE) | |
6039 | break; | |
6040 | } | |
6041 | if (i == rvd->vdev_children) { | |
6042 | spa->spa_deflate = TRUE; | |
6043 | VERIFY(0 == zap_add(spa->spa_meta_objset, | |
6044 | DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE, | |
6045 | sizeof (uint64_t), 1, &spa->spa_deflate, tx)); | |
6046 | } | |
6047 | } | |
6048 | ||
6049 | /* | |
428870ff BB |
6050 | * If anything has changed in this txg, or if someone is waiting |
6051 | * for this txg to sync (eg, spa_vdev_remove()), push the | |
6052 | * deferred frees from the previous txg. If not, leave them | |
6053 | * alone so that we don't generate work on an otherwise idle | |
6054 | * system. | |
34dc7c2f BB |
6055 | */ |
6056 | if (!txg_list_empty(&dp->dp_dirty_datasets, txg) || | |
6057 | !txg_list_empty(&dp->dp_dirty_dirs, txg) || | |
428870ff BB |
6058 | !txg_list_empty(&dp->dp_sync_tasks, txg) || |
6059 | ((dsl_scan_active(dp->dp_scan) || | |
6060 | txg_sync_waiting(dp)) && !spa_shutting_down(spa))) { | |
6061 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
6062 | VERIFY3U(bpobj_iterate(defer_bpo, | |
6063 | spa_free_sync_cb, zio, tx), ==, 0); | |
6064 | VERIFY3U(zio_wait(zio), ==, 0); | |
6065 | } | |
34dc7c2f BB |
6066 | |
6067 | /* | |
6068 | * Iterate to convergence. | |
6069 | */ | |
6070 | do { | |
428870ff | 6071 | int pass = ++spa->spa_sync_pass; |
34dc7c2f BB |
6072 | |
6073 | spa_sync_config_object(spa, tx); | |
6074 | spa_sync_aux_dev(spa, &spa->spa_spares, tx, | |
6075 | ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES); | |
6076 | spa_sync_aux_dev(spa, &spa->spa_l2cache, tx, | |
6077 | ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE); | |
6078 | spa_errlog_sync(spa, txg); | |
6079 | dsl_pool_sync(dp, txg); | |
6080 | ||
428870ff BB |
6081 | if (pass <= SYNC_PASS_DEFERRED_FREE) { |
6082 | zio_t *zio = zio_root(spa, NULL, NULL, 0); | |
6083 | bplist_iterate(free_bpl, spa_free_sync_cb, | |
6084 | zio, tx); | |
6085 | VERIFY(zio_wait(zio) == 0); | |
6086 | } else { | |
6087 | bplist_iterate(free_bpl, bpobj_enqueue_cb, | |
6088 | defer_bpo, tx); | |
34dc7c2f BB |
6089 | } |
6090 | ||
428870ff BB |
6091 | ddt_sync(spa, txg); |
6092 | dsl_scan_sync(dp, tx); | |
34dc7c2f | 6093 | |
c65aa5b2 | 6094 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))) |
428870ff BB |
6095 | vdev_sync(vd, txg); |
6096 | ||
6097 | if (pass == 1) | |
6098 | spa_sync_upgrades(spa, tx); | |
34dc7c2f | 6099 | |
428870ff | 6100 | } while (dmu_objset_is_dirty(mos, txg)); |
34dc7c2f BB |
6101 | |
6102 | /* | |
6103 | * Rewrite the vdev configuration (which includes the uberblock) | |
6104 | * to commit the transaction group. | |
6105 | * | |
6106 | * If there are no dirty vdevs, we sync the uberblock to a few | |
6107 | * random top-level vdevs that are known to be visible in the | |
b128c09f BB |
6108 | * config cache (see spa_vdev_add() for a complete description). |
6109 | * If there *are* dirty vdevs, sync the uberblock to all vdevs. | |
34dc7c2f | 6110 | */ |
b128c09f BB |
6111 | for (;;) { |
6112 | /* | |
6113 | * We hold SCL_STATE to prevent vdev open/close/etc. | |
6114 | * while we're attempting to write the vdev labels. | |
6115 | */ | |
6116 | spa_config_enter(spa, SCL_STATE, FTAG, RW_READER); | |
6117 | ||
6118 | if (list_is_empty(&spa->spa_config_dirty_list)) { | |
6119 | vdev_t *svd[SPA_DVAS_PER_BP]; | |
6120 | int svdcount = 0; | |
6121 | int children = rvd->vdev_children; | |
6122 | int c0 = spa_get_random(children); | |
b128c09f | 6123 | |
d6320ddb | 6124 | for (c = 0; c < children; c++) { |
b128c09f BB |
6125 | vd = rvd->vdev_child[(c0 + c) % children]; |
6126 | if (vd->vdev_ms_array == 0 || vd->vdev_islog) | |
6127 | continue; | |
6128 | svd[svdcount++] = vd; | |
6129 | if (svdcount == SPA_DVAS_PER_BP) | |
6130 | break; | |
6131 | } | |
9babb374 BB |
6132 | error = vdev_config_sync(svd, svdcount, txg, B_FALSE); |
6133 | if (error != 0) | |
6134 | error = vdev_config_sync(svd, svdcount, txg, | |
6135 | B_TRUE); | |
b128c09f BB |
6136 | } else { |
6137 | error = vdev_config_sync(rvd->vdev_child, | |
9babb374 BB |
6138 | rvd->vdev_children, txg, B_FALSE); |
6139 | if (error != 0) | |
6140 | error = vdev_config_sync(rvd->vdev_child, | |
6141 | rvd->vdev_children, txg, B_TRUE); | |
34dc7c2f | 6142 | } |
34dc7c2f | 6143 | |
3bc7e0fb GW |
6144 | if (error == 0) |
6145 | spa->spa_last_synced_guid = rvd->vdev_guid; | |
6146 | ||
b128c09f BB |
6147 | spa_config_exit(spa, SCL_STATE, FTAG); |
6148 | ||
6149 | if (error == 0) | |
6150 | break; | |
6151 | zio_suspend(spa, NULL); | |
6152 | zio_resume_wait(spa); | |
6153 | } | |
34dc7c2f BB |
6154 | dmu_tx_commit(tx); |
6155 | ||
cc92e9d0 GW |
6156 | taskq_cancel_id(system_taskq, spa->spa_deadman_tqid); |
6157 | spa->spa_deadman_tqid = 0; | |
6158 | ||
34dc7c2f BB |
6159 | /* |
6160 | * Clear the dirty config list. | |
6161 | */ | |
b128c09f | 6162 | while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL) |
34dc7c2f BB |
6163 | vdev_config_clean(vd); |
6164 | ||
6165 | /* | |
6166 | * Now that the new config has synced transactionally, | |
6167 | * let it become visible to the config cache. | |
6168 | */ | |
6169 | if (spa->spa_config_syncing != NULL) { | |
6170 | spa_config_set(spa, spa->spa_config_syncing); | |
6171 | spa->spa_config_txg = txg; | |
6172 | spa->spa_config_syncing = NULL; | |
6173 | } | |
6174 | ||
34dc7c2f | 6175 | spa->spa_ubsync = spa->spa_uberblock; |
34dc7c2f | 6176 | |
428870ff | 6177 | dsl_pool_sync_done(dp, txg); |
34dc7c2f BB |
6178 | |
6179 | /* | |
6180 | * Update usable space statistics. | |
6181 | */ | |
c65aa5b2 | 6182 | while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))) |
34dc7c2f BB |
6183 | vdev_sync_done(vd, txg); |
6184 | ||
428870ff BB |
6185 | spa_update_dspace(spa); |
6186 | ||
34dc7c2f BB |
6187 | /* |
6188 | * It had better be the case that we didn't dirty anything | |
6189 | * since vdev_config_sync(). | |
6190 | */ | |
6191 | ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg)); | |
6192 | ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg)); | |
6193 | ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg)); | |
428870ff BB |
6194 | |
6195 | spa->spa_sync_pass = 0; | |
34dc7c2f | 6196 | |
b128c09f | 6197 | spa_config_exit(spa, SCL_CONFIG, FTAG); |
34dc7c2f | 6198 | |
428870ff BB |
6199 | spa_handle_ignored_writes(spa); |
6200 | ||
34dc7c2f BB |
6201 | /* |
6202 | * If any async tasks have been requested, kick them off. | |
6203 | */ | |
6204 | spa_async_dispatch(spa); | |
6205 | } | |
6206 | ||
6207 | /* | |
6208 | * Sync all pools. We don't want to hold the namespace lock across these | |
6209 | * operations, so we take a reference on the spa_t and drop the lock during the | |
6210 | * sync. | |
6211 | */ | |
6212 | void | |
6213 | spa_sync_allpools(void) | |
6214 | { | |
6215 | spa_t *spa = NULL; | |
6216 | mutex_enter(&spa_namespace_lock); | |
6217 | while ((spa = spa_next(spa)) != NULL) { | |
572e2857 BB |
6218 | if (spa_state(spa) != POOL_STATE_ACTIVE || |
6219 | !spa_writeable(spa) || spa_suspended(spa)) | |
34dc7c2f BB |
6220 | continue; |
6221 | spa_open_ref(spa, FTAG); | |
6222 | mutex_exit(&spa_namespace_lock); | |
6223 | txg_wait_synced(spa_get_dsl(spa), 0); | |
6224 | mutex_enter(&spa_namespace_lock); | |
6225 | spa_close(spa, FTAG); | |
6226 | } | |
6227 | mutex_exit(&spa_namespace_lock); | |
6228 | } | |
6229 | ||
6230 | /* | |
6231 | * ========================================================================== | |
6232 | * Miscellaneous routines | |
6233 | * ========================================================================== | |
6234 | */ | |
6235 | ||
6236 | /* | |
6237 | * Remove all pools in the system. | |
6238 | */ | |
6239 | void | |
6240 | spa_evict_all(void) | |
6241 | { | |
6242 | spa_t *spa; | |
6243 | ||
6244 | /* | |
6245 | * Remove all cached state. All pools should be closed now, | |
6246 | * so every spa in the AVL tree should be unreferenced. | |
6247 | */ | |
6248 | mutex_enter(&spa_namespace_lock); | |
6249 | while ((spa = spa_next(NULL)) != NULL) { | |
6250 | /* | |
6251 | * Stop async tasks. The async thread may need to detach | |
6252 | * a device that's been replaced, which requires grabbing | |
6253 | * spa_namespace_lock, so we must drop it here. | |
6254 | */ | |
6255 | spa_open_ref(spa, FTAG); | |
6256 | mutex_exit(&spa_namespace_lock); | |
6257 | spa_async_suspend(spa); | |
6258 | mutex_enter(&spa_namespace_lock); | |
34dc7c2f BB |
6259 | spa_close(spa, FTAG); |
6260 | ||
6261 | if (spa->spa_state != POOL_STATE_UNINITIALIZED) { | |
6262 | spa_unload(spa); | |
6263 | spa_deactivate(spa); | |
6264 | } | |
6265 | spa_remove(spa); | |
6266 | } | |
6267 | mutex_exit(&spa_namespace_lock); | |
6268 | } | |
6269 | ||
6270 | vdev_t * | |
9babb374 | 6271 | spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux) |
34dc7c2f | 6272 | { |
b128c09f BB |
6273 | vdev_t *vd; |
6274 | int i; | |
6275 | ||
6276 | if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL) | |
6277 | return (vd); | |
6278 | ||
9babb374 | 6279 | if (aux) { |
b128c09f BB |
6280 | for (i = 0; i < spa->spa_l2cache.sav_count; i++) { |
6281 | vd = spa->spa_l2cache.sav_vdevs[i]; | |
9babb374 BB |
6282 | if (vd->vdev_guid == guid) |
6283 | return (vd); | |
6284 | } | |
6285 | ||
6286 | for (i = 0; i < spa->spa_spares.sav_count; i++) { | |
6287 | vd = spa->spa_spares.sav_vdevs[i]; | |
b128c09f BB |
6288 | if (vd->vdev_guid == guid) |
6289 | return (vd); | |
6290 | } | |
6291 | } | |
6292 | ||
6293 | return (NULL); | |
34dc7c2f BB |
6294 | } |
6295 | ||
6296 | void | |
6297 | spa_upgrade(spa_t *spa, uint64_t version) | |
6298 | { | |
572e2857 BB |
6299 | ASSERT(spa_writeable(spa)); |
6300 | ||
b128c09f | 6301 | spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); |
34dc7c2f BB |
6302 | |
6303 | /* | |
6304 | * This should only be called for a non-faulted pool, and since a | |
6305 | * future version would result in an unopenable pool, this shouldn't be | |
6306 | * possible. | |
6307 | */ | |
8dca0a9a | 6308 | ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version)); |
34dc7c2f BB |
6309 | ASSERT(version >= spa->spa_uberblock.ub_version); |
6310 | ||
6311 | spa->spa_uberblock.ub_version = version; | |
6312 | vdev_config_dirty(spa->spa_root_vdev); | |
6313 | ||
b128c09f | 6314 | spa_config_exit(spa, SCL_ALL, FTAG); |
34dc7c2f BB |
6315 | |
6316 | txg_wait_synced(spa_get_dsl(spa), 0); | |
6317 | } | |
6318 | ||
6319 | boolean_t | |
6320 | spa_has_spare(spa_t *spa, uint64_t guid) | |
6321 | { | |
6322 | int i; | |
6323 | uint64_t spareguid; | |
6324 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
6325 | ||
6326 | for (i = 0; i < sav->sav_count; i++) | |
6327 | if (sav->sav_vdevs[i]->vdev_guid == guid) | |
6328 | return (B_TRUE); | |
6329 | ||
6330 | for (i = 0; i < sav->sav_npending; i++) { | |
6331 | if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID, | |
6332 | &spareguid) == 0 && spareguid == guid) | |
6333 | return (B_TRUE); | |
6334 | } | |
6335 | ||
6336 | return (B_FALSE); | |
6337 | } | |
6338 | ||
b128c09f BB |
6339 | /* |
6340 | * Check if a pool has an active shared spare device. | |
6341 | * Note: reference count of an active spare is 2, as a spare and as a replace | |
6342 | */ | |
6343 | static boolean_t | |
6344 | spa_has_active_shared_spare(spa_t *spa) | |
6345 | { | |
6346 | int i, refcnt; | |
6347 | uint64_t pool; | |
6348 | spa_aux_vdev_t *sav = &spa->spa_spares; | |
6349 | ||
6350 | for (i = 0; i < sav->sav_count; i++) { | |
6351 | if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool, | |
6352 | &refcnt) && pool != 0ULL && pool == spa_guid(spa) && | |
6353 | refcnt > 2) | |
6354 | return (B_TRUE); | |
6355 | } | |
6356 | ||
6357 | return (B_FALSE); | |
6358 | } | |
6359 | ||
34dc7c2f | 6360 | /* |
26685276 | 6361 | * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be |
34dc7c2f BB |
6362 | * filled in from the spa and (optionally) the vdev. This doesn't do anything |
6363 | * in the userland libzpool, as we don't want consumers to misinterpret ztest | |
6364 | * or zdb as real changes. | |
6365 | */ | |
6366 | void | |
6367 | spa_event_notify(spa_t *spa, vdev_t *vd, const char *name) | |
6368 | { | |
6369 | #ifdef _KERNEL | |
26685276 | 6370 | zfs_ereport_post(name, spa, vd, NULL, 0, 0); |
34dc7c2f BB |
6371 | #endif |
6372 | } | |
c28b2279 BB |
6373 | |
6374 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
6375 | /* state manipulation functions */ | |
6376 | EXPORT_SYMBOL(spa_open); | |
6377 | EXPORT_SYMBOL(spa_open_rewind); | |
6378 | EXPORT_SYMBOL(spa_get_stats); | |
6379 | EXPORT_SYMBOL(spa_create); | |
6380 | EXPORT_SYMBOL(spa_import_rootpool); | |
6381 | EXPORT_SYMBOL(spa_import); | |
6382 | EXPORT_SYMBOL(spa_tryimport); | |
6383 | EXPORT_SYMBOL(spa_destroy); | |
6384 | EXPORT_SYMBOL(spa_export); | |
6385 | EXPORT_SYMBOL(spa_reset); | |
6386 | EXPORT_SYMBOL(spa_async_request); | |
6387 | EXPORT_SYMBOL(spa_async_suspend); | |
6388 | EXPORT_SYMBOL(spa_async_resume); | |
6389 | EXPORT_SYMBOL(spa_inject_addref); | |
6390 | EXPORT_SYMBOL(spa_inject_delref); | |
6391 | EXPORT_SYMBOL(spa_scan_stat_init); | |
6392 | EXPORT_SYMBOL(spa_scan_get_stats); | |
6393 | ||
6394 | /* device maniion */ | |
6395 | EXPORT_SYMBOL(spa_vdev_add); | |
6396 | EXPORT_SYMBOL(spa_vdev_attach); | |
6397 | EXPORT_SYMBOL(spa_vdev_detach); | |
6398 | EXPORT_SYMBOL(spa_vdev_remove); | |
6399 | EXPORT_SYMBOL(spa_vdev_setpath); | |
6400 | EXPORT_SYMBOL(spa_vdev_setfru); | |
6401 | EXPORT_SYMBOL(spa_vdev_split_mirror); | |
6402 | ||
6403 | /* spare statech is global across all pools) */ | |
6404 | EXPORT_SYMBOL(spa_spare_add); | |
6405 | EXPORT_SYMBOL(spa_spare_remove); | |
6406 | EXPORT_SYMBOL(spa_spare_exists); | |
6407 | EXPORT_SYMBOL(spa_spare_activate); | |
6408 | ||
6409 | /* L2ARC statech is global across all pools) */ | |
6410 | EXPORT_SYMBOL(spa_l2cache_add); | |
6411 | EXPORT_SYMBOL(spa_l2cache_remove); | |
6412 | EXPORT_SYMBOL(spa_l2cache_exists); | |
6413 | EXPORT_SYMBOL(spa_l2cache_activate); | |
6414 | EXPORT_SYMBOL(spa_l2cache_drop); | |
6415 | ||
6416 | /* scanning */ | |
6417 | EXPORT_SYMBOL(spa_scan); | |
6418 | EXPORT_SYMBOL(spa_scan_stop); | |
6419 | ||
6420 | /* spa syncing */ | |
6421 | EXPORT_SYMBOL(spa_sync); /* only for DMU use */ | |
6422 | EXPORT_SYMBOL(spa_sync_allpools); | |
6423 | ||
6424 | /* properties */ | |
6425 | EXPORT_SYMBOL(spa_prop_set); | |
6426 | EXPORT_SYMBOL(spa_prop_get); | |
6427 | EXPORT_SYMBOL(spa_prop_clear_bootfs); | |
6428 | ||
6429 | /* asynchronous event notification */ | |
6430 | EXPORT_SYMBOL(spa_event_notify); | |
6431 | #endif |