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