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