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