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