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