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