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