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