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