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