]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/spa.c
Illumos 5034 - ARC's buf_hash_table is too small
[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;
34dc7c2f 1858
428870ff 1859 if (error) {
9ae529ec 1860 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
428870ff
BB
1861 type != DMU_OT_INTENT_LOG)
1862 atomic_add_64(&sle->sle_meta_count, 1);
1863 else
1864 atomic_add_64(&sle->sle_data_count, 1);
34dc7c2f 1865 }
428870ff
BB
1866 zio_data_buf_free(zio->io_data, zio->io_size);
1867}
34dc7c2f 1868
428870ff
BB
1869/*ARGSUSED*/
1870static int
1871spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
5dbd68a3 1872 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
428870ff 1873{
9b67f605 1874 if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
428870ff
BB
1875 zio_t *rio = arg;
1876 size_t size = BP_GET_PSIZE(bp);
1877 void *data = zio_data_buf_alloc(size);
34dc7c2f 1878
428870ff
BB
1879 zio_nowait(zio_read(rio, spa, bp, data, size,
1880 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1881 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1882 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
34dc7c2f 1883 }
428870ff
BB
1884 return (0);
1885}
34dc7c2f 1886
428870ff
BB
1887static int
1888spa_load_verify(spa_t *spa)
1889{
1890 zio_t *rio;
1891 spa_load_error_t sle = { 0 };
1892 zpool_rewind_policy_t policy;
1893 boolean_t verify_ok = B_FALSE;
1894 int error;
34dc7c2f 1895
428870ff 1896 zpool_get_rewind_policy(spa->spa_config, &policy);
34dc7c2f 1897
428870ff
BB
1898 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1899 return (0);
34dc7c2f 1900
428870ff
BB
1901 rio = zio_root(spa, NULL, &sle,
1902 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
34dc7c2f 1903
428870ff
BB
1904 error = traverse_pool(spa, spa->spa_verify_min_txg,
1905 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1906
1907 (void) zio_wait(rio);
1908
1909 spa->spa_load_meta_errors = sle.sle_meta_count;
1910 spa->spa_load_data_errors = sle.sle_data_count;
1911
1912 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1913 sle.sle_data_count <= policy.zrp_maxdata) {
572e2857
BB
1914 int64_t loss = 0;
1915
428870ff
BB
1916 verify_ok = B_TRUE;
1917 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1918 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
572e2857
BB
1919
1920 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1921 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1922 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1923 VERIFY(nvlist_add_int64(spa->spa_load_info,
1924 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1925 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1926 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
428870ff
BB
1927 } else {
1928 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1929 }
1930
1931 if (error) {
1932 if (error != ENXIO && error != EIO)
2e528b49 1933 error = SET_ERROR(EIO);
428870ff
BB
1934 return (error);
1935 }
1936
1937 return (verify_ok ? 0 : EIO);
1938}
1939
1940/*
1941 * Find a value in the pool props object.
1942 */
1943static void
1944spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1945{
1946 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1947 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1948}
1949
1950/*
1951 * Find a value in the pool directory object.
1952 */
1953static int
1954spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1955{
1956 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1957 name, sizeof (uint64_t), 1, val));
1958}
1959
1960static int
1961spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1962{
1963 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1964 return (err);
1965}
1966
1967/*
1968 * Fix up config after a partly-completed split. This is done with the
1969 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1970 * pool have that entry in their config, but only the splitting one contains
1971 * a list of all the guids of the vdevs that are being split off.
1972 *
1973 * This function determines what to do with that list: either rejoin
1974 * all the disks to the pool, or complete the splitting process. To attempt
1975 * the rejoin, each disk that is offlined is marked online again, and
1976 * we do a reopen() call. If the vdev label for every disk that was
1977 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1978 * then we call vdev_split() on each disk, and complete the split.
1979 *
1980 * Otherwise we leave the config alone, with all the vdevs in place in
1981 * the original pool.
1982 */
1983static void
1984spa_try_repair(spa_t *spa, nvlist_t *config)
1985{
1986 uint_t extracted;
1987 uint64_t *glist;
1988 uint_t i, gcount;
1989 nvlist_t *nvl;
1990 vdev_t **vd;
1991 boolean_t attempt_reopen;
1992
1993 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1994 return;
1995
1996 /* check that the config is complete */
1997 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1998 &glist, &gcount) != 0)
1999 return;
2000
b8d06fca 2001 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE);
428870ff
BB
2002
2003 /* attempt to online all the vdevs & validate */
2004 attempt_reopen = B_TRUE;
2005 for (i = 0; i < gcount; i++) {
2006 if (glist[i] == 0) /* vdev is hole */
2007 continue;
2008
2009 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2010 if (vd[i] == NULL) {
2011 /*
2012 * Don't bother attempting to reopen the disks;
2013 * just do the split.
2014 */
2015 attempt_reopen = B_FALSE;
2016 } else {
2017 /* attempt to re-online it */
2018 vd[i]->vdev_offline = B_FALSE;
2019 }
2020 }
2021
2022 if (attempt_reopen) {
2023 vdev_reopen(spa->spa_root_vdev);
2024
2025 /* check each device to see what state it's in */
2026 for (extracted = 0, i = 0; i < gcount; i++) {
2027 if (vd[i] != NULL &&
2028 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2029 break;
2030 ++extracted;
2031 }
2032 }
2033
2034 /*
2035 * If every disk has been moved to the new pool, or if we never
2036 * even attempted to look at them, then we split them off for
2037 * good.
2038 */
2039 if (!attempt_reopen || gcount == extracted) {
2040 for (i = 0; i < gcount; i++)
2041 if (vd[i] != NULL)
2042 vdev_split(vd[i]);
2043 vdev_reopen(spa->spa_root_vdev);
2044 }
2045
2046 kmem_free(vd, gcount * sizeof (vdev_t *));
2047}
2048
2049static int
2050spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2051 boolean_t mosconfig)
2052{
2053 nvlist_t *config = spa->spa_config;
2054 char *ereport = FM_EREPORT_ZFS_POOL;
d96eb2b1 2055 char *comment;
428870ff
BB
2056 int error;
2057 uint64_t pool_guid;
2058 nvlist_t *nvl;
2059
2060 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2e528b49 2061 return (SET_ERROR(EINVAL));
428870ff 2062
d96eb2b1
DM
2063 ASSERT(spa->spa_comment == NULL);
2064 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2065 spa->spa_comment = spa_strdup(comment);
2066
428870ff
BB
2067 /*
2068 * Versioning wasn't explicitly added to the label until later, so if
2069 * it's not present treat it as the initial version.
2070 */
2071 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2072 &spa->spa_ubsync.ub_version) != 0)
2073 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2074
2075 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2076 &spa->spa_config_txg);
2077
2078 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2079 spa_guid_exists(pool_guid, 0)) {
2e528b49 2080 error = SET_ERROR(EEXIST);
428870ff 2081 } else {
3541dc6d 2082 spa->spa_config_guid = pool_guid;
428870ff
BB
2083
2084 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2085 &nvl) == 0) {
2086 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
b8d06fca 2087 KM_PUSHPAGE) == 0);
428870ff
BB
2088 }
2089
9ae529ec
CS
2090 nvlist_free(spa->spa_load_info);
2091 spa->spa_load_info = fnvlist_alloc();
2092
572e2857 2093 gethrestime(&spa->spa_loaded_ts);
428870ff
BB
2094 error = spa_load_impl(spa, pool_guid, config, state, type,
2095 mosconfig, &ereport);
2096 }
2097
2098 spa->spa_minref = refcount_count(&spa->spa_refcount);
572e2857
BB
2099 if (error) {
2100 if (error != EEXIST) {
2101 spa->spa_loaded_ts.tv_sec = 0;
2102 spa->spa_loaded_ts.tv_nsec = 0;
2103 }
2104 if (error != EBADF) {
2105 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2106 }
2107 }
428870ff
BB
2108 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2109 spa->spa_ena = 0;
2110
2111 return (error);
2112}
2113
2114/*
2115 * Load an existing storage pool, using the pool's builtin spa_config as a
2116 * source of configuration information.
2117 */
bf701a83
BB
2118__attribute__((always_inline))
2119static inline int
428870ff
BB
2120spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2121 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2122 char **ereport)
2123{
2124 int error = 0;
2125 nvlist_t *nvroot = NULL;
9ae529ec 2126 nvlist_t *label;
428870ff
BB
2127 vdev_t *rvd;
2128 uberblock_t *ub = &spa->spa_uberblock;
572e2857 2129 uint64_t children, config_cache_txg = spa->spa_config_txg;
428870ff
BB
2130 int orig_mode = spa->spa_mode;
2131 int parse;
2132 uint64_t obj;
9ae529ec 2133 boolean_t missing_feat_write = B_FALSE;
428870ff
BB
2134
2135 /*
2136 * If this is an untrusted config, access the pool in read-only mode.
2137 * This prevents things like resilvering recently removed devices.
2138 */
2139 if (!mosconfig)
2140 spa->spa_mode = FREAD;
2141
2142 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2143
2144 spa->spa_load_state = state;
2145
2146 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2e528b49 2147 return (SET_ERROR(EINVAL));
428870ff
BB
2148
2149 parse = (type == SPA_IMPORT_EXISTING ?
2150 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2151
2152 /*
2153 * Create "The Godfather" zio to hold all async IOs
2154 */
2155 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2156 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2157
2158 /*
2159 * Parse the configuration into a vdev tree. We explicitly set the
2160 * value that will be returned by spa_version() since parsing the
2161 * configuration requires knowing the version number.
2162 */
2163 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2164 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2165 spa_config_exit(spa, SCL_ALL, FTAG);
2166
2167 if (error != 0)
2168 return (error);
2169
2170 ASSERT(spa->spa_root_vdev == rvd);
2171
2172 if (type != SPA_IMPORT_ASSEMBLE) {
2173 ASSERT(spa_guid(spa) == pool_guid);
2174 }
2175
2176 /*
2177 * Try to open all vdevs, loading each label in the process.
2178 */
2179 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2180 error = vdev_open(rvd);
2181 spa_config_exit(spa, SCL_ALL, FTAG);
2182 if (error != 0)
2183 return (error);
2184
2185 /*
2186 * We need to validate the vdev labels against the configuration that
2187 * we have in hand, which is dependent on the setting of mosconfig. If
2188 * mosconfig is true then we're validating the vdev labels based on
2189 * that config. Otherwise, we're validating against the cached config
2190 * (zpool.cache) that was read when we loaded the zfs module, and then
2191 * later we will recursively call spa_load() and validate against
2192 * the vdev config.
2193 *
2194 * If we're assembling a new pool that's been split off from an
2195 * existing pool, the labels haven't yet been updated so we skip
2196 * validation for now.
2197 */
2198 if (type != SPA_IMPORT_ASSEMBLE) {
2199 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
c7f2d69d 2200 error = vdev_validate(rvd, mosconfig);
428870ff
BB
2201 spa_config_exit(spa, SCL_ALL, FTAG);
2202
2203 if (error != 0)
2204 return (error);
2205
2206 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2e528b49 2207 return (SET_ERROR(ENXIO));
428870ff
BB
2208 }
2209
2210 /*
2211 * Find the best uberblock.
2212 */
9ae529ec 2213 vdev_uberblock_load(rvd, ub, &label);
428870ff
BB
2214
2215 /*
2216 * If we weren't able to find a single valid uberblock, return failure.
2217 */
9ae529ec
CS
2218 if (ub->ub_txg == 0) {
2219 nvlist_free(label);
428870ff 2220 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
9ae529ec 2221 }
428870ff
BB
2222
2223 /*
9ae529ec 2224 * If the pool has an unsupported version we can't open it.
428870ff 2225 */
9ae529ec
CS
2226 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2227 nvlist_free(label);
428870ff 2228 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
9ae529ec
CS
2229 }
2230
2231 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2232 nvlist_t *features;
2233
2234 /*
2235 * If we weren't able to find what's necessary for reading the
2236 * MOS in the label, return failure.
2237 */
2238 if (label == NULL || nvlist_lookup_nvlist(label,
2239 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2240 nvlist_free(label);
2241 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2242 ENXIO));
2243 }
2244
2245 /*
2246 * Update our in-core representation with the definitive values
2247 * from the label.
2248 */
2249 nvlist_free(spa->spa_label_features);
2250 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2251 }
2252
2253 nvlist_free(label);
2254
2255 /*
2256 * Look through entries in the label nvlist's features_for_read. If
2257 * there is a feature listed there which we don't understand then we
2258 * cannot open a pool.
2259 */
2260 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2261 nvlist_t *unsup_feat;
2262 nvpair_t *nvp;
2263
2264 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2265 0);
2266
2267 for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL);
2268 nvp != NULL;
2269 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2270 if (!zfeature_is_supported(nvpair_name(nvp))) {
2271 VERIFY(nvlist_add_string(unsup_feat,
2272 nvpair_name(nvp), "") == 0);
2273 }
2274 }
2275
2276 if (!nvlist_empty(unsup_feat)) {
2277 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2278 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2279 nvlist_free(unsup_feat);
2280 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2281 ENOTSUP));
2282 }
2283
2284 nvlist_free(unsup_feat);
2285 }
428870ff
BB
2286
2287 /*
2288 * If the vdev guid sum doesn't match the uberblock, we have an
572e2857
BB
2289 * incomplete configuration. We first check to see if the pool
2290 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2291 * If it is, defer the vdev_guid_sum check till later so we
2292 * can handle missing vdevs.
428870ff 2293 */
572e2857
BB
2294 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2295 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
428870ff
BB
2296 rvd->vdev_guid_sum != ub->ub_guid_sum)
2297 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2298
2299 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2300 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2301 spa_try_repair(spa, config);
2302 spa_config_exit(spa, SCL_ALL, FTAG);
2303 nvlist_free(spa->spa_config_splitting);
2304 spa->spa_config_splitting = NULL;
2305 }
2306
2307 /*
2308 * Initialize internal SPA structures.
2309 */
2310 spa->spa_state = POOL_STATE_ACTIVE;
2311 spa->spa_ubsync = spa->spa_uberblock;
2312 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2313 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2314 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2315 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2316 spa->spa_claim_max_txg = spa->spa_first_txg;
2317 spa->spa_prev_software_version = ub->ub_software_version;
2318
9ae529ec 2319 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
428870ff
BB
2320 if (error)
2321 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2322 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2323
2324 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2325 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2326
9ae529ec
CS
2327 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2328 boolean_t missing_feat_read = B_FALSE;
b9b24bb4 2329 nvlist_t *unsup_feat, *enabled_feat;
b0bc7a84 2330 spa_feature_t i;
9ae529ec
CS
2331
2332 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2333 &spa->spa_feat_for_read_obj) != 0) {
2334 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2335 }
2336
2337 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2338 &spa->spa_feat_for_write_obj) != 0) {
2339 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2340 }
2341
2342 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2343 &spa->spa_feat_desc_obj) != 0) {
2344 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2345 }
2346
b9b24bb4
CS
2347 enabled_feat = fnvlist_alloc();
2348 unsup_feat = fnvlist_alloc();
9ae529ec 2349
fa86b5db 2350 if (!spa_features_check(spa, B_FALSE,
b9b24bb4 2351 unsup_feat, enabled_feat))
9ae529ec
CS
2352 missing_feat_read = B_TRUE;
2353
2354 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
fa86b5db 2355 if (!spa_features_check(spa, B_TRUE,
b9b24bb4 2356 unsup_feat, enabled_feat)) {
9ae529ec 2357 missing_feat_write = B_TRUE;
b9b24bb4 2358 }
9ae529ec
CS
2359 }
2360
b9b24bb4
CS
2361 fnvlist_add_nvlist(spa->spa_load_info,
2362 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2363
9ae529ec 2364 if (!nvlist_empty(unsup_feat)) {
b9b24bb4
CS
2365 fnvlist_add_nvlist(spa->spa_load_info,
2366 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
9ae529ec
CS
2367 }
2368
b9b24bb4
CS
2369 fnvlist_free(enabled_feat);
2370 fnvlist_free(unsup_feat);
9ae529ec
CS
2371
2372 if (!missing_feat_read) {
2373 fnvlist_add_boolean(spa->spa_load_info,
2374 ZPOOL_CONFIG_CAN_RDONLY);
2375 }
2376
2377 /*
2378 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2379 * twofold: to determine whether the pool is available for
2380 * import in read-write mode and (if it is not) whether the
2381 * pool is available for import in read-only mode. If the pool
2382 * is available for import in read-write mode, it is displayed
2383 * as available in userland; if it is not available for import
2384 * in read-only mode, it is displayed as unavailable in
2385 * userland. If the pool is available for import in read-only
2386 * mode but not read-write mode, it is displayed as unavailable
2387 * in userland with a special note that the pool is actually
2388 * available for open in read-only mode.
2389 *
2390 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2391 * missing a feature for write, we must first determine whether
2392 * the pool can be opened read-only before returning to
2393 * userland in order to know whether to display the
2394 * abovementioned note.
2395 */
2396 if (missing_feat_read || (missing_feat_write &&
2397 spa_writeable(spa))) {
2398 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2399 ENOTSUP));
2400 }
b0bc7a84
MG
2401
2402 /*
2403 * Load refcounts for ZFS features from disk into an in-memory
2404 * cache during SPA initialization.
2405 */
2406 for (i = 0; i < SPA_FEATURES; i++) {
2407 uint64_t refcount;
2408
2409 error = feature_get_refcount_from_disk(spa,
2410 &spa_feature_table[i], &refcount);
2411 if (error == 0) {
2412 spa->spa_feat_refcount_cache[i] = refcount;
2413 } else if (error == ENOTSUP) {
2414 spa->spa_feat_refcount_cache[i] =
2415 SPA_FEATURE_DISABLED;
2416 } else {
2417 return (spa_vdev_err(rvd,
2418 VDEV_AUX_CORRUPT_DATA, EIO));
2419 }
2420 }
2421 }
2422
2423 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2424 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
9b67f605 2425 &spa->spa_feat_enabled_txg_obj) != 0)
b0bc7a84 2426 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
9ae529ec
CS
2427 }
2428
2429 spa->spa_is_initializing = B_TRUE;
2430 error = dsl_pool_open(spa->spa_dsl_pool);
2431 spa->spa_is_initializing = B_FALSE;
2432 if (error != 0)
2433 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2434
428870ff
BB
2435 if (!mosconfig) {
2436 uint64_t hostid;
2437 nvlist_t *policy = NULL, *nvconfig;
2438
2439 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2440 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2441
2442 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
b128c09f 2443 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
34dc7c2f
BB
2444 char *hostname;
2445 unsigned long myhostid = 0;
2446
428870ff 2447 VERIFY(nvlist_lookup_string(nvconfig,
34dc7c2f
BB
2448 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2449
d164b209
BB
2450#ifdef _KERNEL
2451 myhostid = zone_get_hostid(NULL);
2452#else /* _KERNEL */
2453 /*
2454 * We're emulating the system's hostid in userland, so
2455 * we can't use zone_get_hostid().
2456 */
34dc7c2f 2457 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
d164b209 2458#endif /* _KERNEL */
34dc7c2f 2459 if (hostid != 0 && myhostid != 0 &&
d164b209 2460 hostid != myhostid) {
428870ff 2461 nvlist_free(nvconfig);
34dc7c2f 2462 cmn_err(CE_WARN, "pool '%s' could not be "
d1d7e268
MK
2463 "loaded as it was last accessed by another "
2464 "system (host: %s hostid: 0x%lx). See: "
2465 "http://zfsonlinux.org/msg/ZFS-8000-EY",
b128c09f 2466 spa_name(spa), hostname,
34dc7c2f 2467 (unsigned long)hostid);
2e528b49 2468 return (SET_ERROR(EBADF));
34dc7c2f
BB
2469 }
2470 }
428870ff
BB
2471 if (nvlist_lookup_nvlist(spa->spa_config,
2472 ZPOOL_REWIND_POLICY, &policy) == 0)
2473 VERIFY(nvlist_add_nvlist(nvconfig,
2474 ZPOOL_REWIND_POLICY, policy) == 0);
34dc7c2f 2475
428870ff 2476 spa_config_set(spa, nvconfig);
34dc7c2f
BB
2477 spa_unload(spa);
2478 spa_deactivate(spa);
fb5f0bc8 2479 spa_activate(spa, orig_mode);
34dc7c2f 2480
428870ff 2481 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
34dc7c2f
BB
2482 }
2483
428870ff
BB
2484 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2485 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2486 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2487 if (error != 0)
2488 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
2489
2490 /*
2491 * Load the bit that tells us to use the new accounting function
2492 * (raid-z deflation). If we have an older pool, this will not
2493 * be present.
2494 */
428870ff
BB
2495 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2496 if (error != 0 && error != ENOENT)
2497 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2498
2499 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2500 &spa->spa_creation_version);
2501 if (error != 0 && error != ENOENT)
2502 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
2503
2504 /*
2505 * Load the persistent error log. If we have an older pool, this will
2506 * not be present.
2507 */
428870ff
BB
2508 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2509 if (error != 0 && error != ENOENT)
2510 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 2511
428870ff
BB
2512 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2513 &spa->spa_errlog_scrub);
2514 if (error != 0 && error != ENOENT)
2515 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
2516
2517 /*
2518 * Load the history object. If we have an older pool, this
2519 * will not be present.
2520 */
428870ff
BB
2521 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2522 if (error != 0 && error != ENOENT)
2523 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2524
2525 /*
2526 * If we're assembling the pool from the split-off vdevs of
2527 * an existing pool, we don't want to attach the spares & cache
2528 * devices.
2529 */
34dc7c2f
BB
2530
2531 /*
2532 * Load any hot spares for this pool.
2533 */
428870ff
BB
2534 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2535 if (error != 0 && error != ENOENT)
2536 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2537 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
2538 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2539 if (load_nvlist(spa, spa->spa_spares.sav_object,
428870ff
BB
2540 &spa->spa_spares.sav_config) != 0)
2541 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 2542
b128c09f 2543 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 2544 spa_load_spares(spa);
b128c09f 2545 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
2546 } else if (error == 0) {
2547 spa->spa_spares.sav_sync = B_TRUE;
34dc7c2f
BB
2548 }
2549
2550 /*
2551 * Load any level 2 ARC devices for this pool.
2552 */
428870ff 2553 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
34dc7c2f 2554 &spa->spa_l2cache.sav_object);
428870ff
BB
2555 if (error != 0 && error != ENOENT)
2556 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2557 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
34dc7c2f
BB
2558 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2559 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
428870ff
BB
2560 &spa->spa_l2cache.sav_config) != 0)
2561 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f 2562
b128c09f 2563 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 2564 spa_load_l2cache(spa);
b128c09f 2565 spa_config_exit(spa, SCL_ALL, FTAG);
428870ff
BB
2566 } else if (error == 0) {
2567 spa->spa_l2cache.sav_sync = B_TRUE;
b128c09f
BB
2568 }
2569
34dc7c2f
BB
2570 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2571
428870ff
BB
2572 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2573 if (error && error != ENOENT)
2574 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
34dc7c2f
BB
2575
2576 if (error == 0) {
2dbedf54 2577 uint64_t autoreplace = 0;
428870ff
BB
2578
2579 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2580 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2581 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2582 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2583 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2584 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2585 &spa->spa_dedup_ditto);
2586
2587 spa->spa_autoreplace = (autoreplace != 0);
34dc7c2f
BB
2588 }
2589
2590 /*
2591 * If the 'autoreplace' property is set, then post a resource notifying
2592 * the ZFS DE that it should not issue any faults for unopenable
2593 * devices. We also iterate over the vdevs, and post a sysevent for any
2594 * unopenable vdevs so that the normal autoreplace handler can take
2595 * over.
2596 */
428870ff 2597 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
34dc7c2f 2598 spa_check_removed(spa->spa_root_vdev);
428870ff
BB
2599 /*
2600 * For the import case, this is done in spa_import(), because
2601 * at this point we're using the spare definitions from
2602 * the MOS config, not necessarily from the userland config.
2603 */
2604 if (state != SPA_LOAD_IMPORT) {
2605 spa_aux_check_removed(&spa->spa_spares);
2606 spa_aux_check_removed(&spa->spa_l2cache);
2607 }
2608 }
34dc7c2f
BB
2609
2610 /*
2611 * Load the vdev state for all toplevel vdevs.
2612 */
2613 vdev_load(rvd);
2614
2615 /*
2616 * Propagate the leaf DTLs we just loaded all the way up the tree.
2617 */
b128c09f 2618 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 2619 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
b128c09f 2620 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 2621
428870ff
BB
2622 /*
2623 * Load the DDTs (dedup tables).
2624 */
2625 error = ddt_load(spa);
2626 if (error != 0)
2627 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2628
2629 spa_update_dspace(spa);
2630
428870ff 2631 /*
572e2857
BB
2632 * Validate the config, using the MOS config to fill in any
2633 * information which might be missing. If we fail to validate
2634 * the config then declare the pool unfit for use. If we're
2635 * assembling a pool from a split, the log is not transferred
2636 * over.
428870ff
BB
2637 */
2638 if (type != SPA_IMPORT_ASSEMBLE) {
2639 nvlist_t *nvconfig;
2640
2641 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2642 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2643
572e2857
BB
2644 if (!spa_config_valid(spa, nvconfig)) {
2645 nvlist_free(nvconfig);
2646 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2647 ENXIO));
2648 }
428870ff
BB
2649 nvlist_free(nvconfig);
2650
572e2857 2651 /*
9ae529ec 2652 * Now that we've validated the config, check the state of the
572e2857
BB
2653 * root vdev. If it can't be opened, it indicates one or
2654 * more toplevel vdevs are faulted.
2655 */
2656 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2e528b49 2657 return (SET_ERROR(ENXIO));
572e2857 2658
428870ff
BB
2659 if (spa_check_logs(spa)) {
2660 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2661 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2662 }
2663 }
2664
9ae529ec
CS
2665 if (missing_feat_write) {
2666 ASSERT(state == SPA_LOAD_TRYIMPORT);
2667
2668 /*
2669 * At this point, we know that we can open the pool in
2670 * read-only mode but not read-write mode. We now have enough
2671 * information and can return to userland.
2672 */
2673 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2674 }
2675
572e2857
BB
2676 /*
2677 * We've successfully opened the pool, verify that we're ready
2678 * to start pushing transactions.
2679 */
2680 if (state != SPA_LOAD_TRYIMPORT) {
c65aa5b2 2681 if ((error = spa_load_verify(spa)))
572e2857
BB
2682 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2683 error));
2684 }
2685
428870ff
BB
2686 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2687 spa->spa_load_max_txg == UINT64_MAX)) {
34dc7c2f
BB
2688 dmu_tx_t *tx;
2689 int need_update = B_FALSE;
d6320ddb 2690 int c;
fb5f0bc8
BB
2691
2692 ASSERT(state != SPA_LOAD_TRYIMPORT);
34dc7c2f
BB
2693
2694 /*
2695 * Claim log blocks that haven't been committed yet.
2696 * This must all happen in a single txg.
428870ff
BB
2697 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2698 * invoked from zil_claim_log_block()'s i/o done callback.
2699 * Price of rollback is that we abandon the log.
34dc7c2f 2700 */
428870ff
BB
2701 spa->spa_claiming = B_TRUE;
2702
34dc7c2f
BB
2703 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2704 spa_first_txg(spa));
b128c09f 2705 (void) dmu_objset_find(spa_name(spa),
34dc7c2f
BB
2706 zil_claim, tx, DS_FIND_CHILDREN);
2707 dmu_tx_commit(tx);
2708
428870ff
BB
2709 spa->spa_claiming = B_FALSE;
2710
2711 spa_set_log_state(spa, SPA_LOG_GOOD);
34dc7c2f
BB
2712 spa->spa_sync_on = B_TRUE;
2713 txg_sync_start(spa->spa_dsl_pool);
2714
2715 /*
428870ff
BB
2716 * Wait for all claims to sync. We sync up to the highest
2717 * claimed log block birth time so that claimed log blocks
2718 * don't appear to be from the future. spa_claim_max_txg
2719 * will have been set for us by either zil_check_log_chain()
2720 * (invoked from spa_check_logs()) or zil_claim() above.
34dc7c2f 2721 */
428870ff 2722 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
34dc7c2f
BB
2723
2724 /*
2725 * If the config cache is stale, or we have uninitialized
2726 * metaslabs (see spa_vdev_add()), then update the config.
45d1cae3 2727 *
572e2857 2728 * If this is a verbatim import, trust the current
45d1cae3 2729 * in-core spa_config and update the disk labels.
34dc7c2f
BB
2730 */
2731 if (config_cache_txg != spa->spa_config_txg ||
572e2857
BB
2732 state == SPA_LOAD_IMPORT ||
2733 state == SPA_LOAD_RECOVER ||
2734 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
34dc7c2f
BB
2735 need_update = B_TRUE;
2736
d6320ddb 2737 for (c = 0; c < rvd->vdev_children; c++)
34dc7c2f
BB
2738 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2739 need_update = B_TRUE;
2740
2741 /*
2742 * Update the config cache asychronously in case we're the
2743 * root pool, in which case the config cache isn't writable yet.
2744 */
2745 if (need_update)
2746 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
fb5f0bc8
BB
2747
2748 /*
2749 * Check all DTLs to see if anything needs resilvering.
2750 */
428870ff
BB
2751 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2752 vdev_resilver_needed(rvd, NULL, NULL))
fb5f0bc8 2753 spa_async_request(spa, SPA_ASYNC_RESILVER);
428870ff 2754
6f1ffb06
MA
2755 /*
2756 * Log the fact that we booted up (so that we can detect if
2757 * we rebooted in the middle of an operation).
2758 */
2759 spa_history_log_version(spa, "open");
2760
428870ff
BB
2761 /*
2762 * Delete any inconsistent datasets.
2763 */
2764 (void) dmu_objset_find(spa_name(spa),
2765 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2766
2767 /*
2768 * Clean up any stale temporary dataset userrefs.
2769 */
2770 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
34dc7c2f
BB
2771 }
2772
428870ff
BB
2773 return (0);
2774}
34dc7c2f 2775
428870ff
BB
2776static int
2777spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2778{
572e2857
BB
2779 int mode = spa->spa_mode;
2780
428870ff
BB
2781 spa_unload(spa);
2782 spa_deactivate(spa);
2783
2784 spa->spa_load_max_txg--;
2785
572e2857 2786 spa_activate(spa, mode);
428870ff
BB
2787 spa_async_suspend(spa);
2788
2789 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2790}
2791
9ae529ec
CS
2792/*
2793 * If spa_load() fails this function will try loading prior txg's. If
2794 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2795 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2796 * function will not rewind the pool and will return the same error as
2797 * spa_load().
2798 */
428870ff
BB
2799static int
2800spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2801 uint64_t max_request, int rewind_flags)
2802{
9ae529ec 2803 nvlist_t *loadinfo = NULL;
428870ff
BB
2804 nvlist_t *config = NULL;
2805 int load_error, rewind_error;
2806 uint64_t safe_rewind_txg;
2807 uint64_t min_txg;
2808
2809 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2810 spa->spa_load_max_txg = spa->spa_load_txg;
2811 spa_set_log_state(spa, SPA_LOG_CLEAR);
2812 } else {
2813 spa->spa_load_max_txg = max_request;
2814 }
2815
2816 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2817 mosconfig);
2818 if (load_error == 0)
2819 return (0);
2820
2821 if (spa->spa_root_vdev != NULL)
2822 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2823
2824 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2825 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2826
2827 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2828 nvlist_free(config);
2829 return (load_error);
2830 }
2831
9ae529ec
CS
2832 if (state == SPA_LOAD_RECOVER) {
2833 /* Price of rolling back is discarding txgs, including log */
428870ff 2834 spa_set_log_state(spa, SPA_LOG_CLEAR);
9ae529ec
CS
2835 } else {
2836 /*
2837 * If we aren't rolling back save the load info from our first
2838 * import attempt so that we can restore it after attempting
2839 * to rewind.
2840 */
2841 loadinfo = spa->spa_load_info;
2842 spa->spa_load_info = fnvlist_alloc();
2843 }
428870ff
BB
2844
2845 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2846 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2847 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2848 TXG_INITIAL : safe_rewind_txg;
2849
2850 /*
2851 * Continue as long as we're finding errors, we're still within
2852 * the acceptable rewind range, and we're still finding uberblocks
2853 */
2854 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2855 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2856 if (spa->spa_load_max_txg < safe_rewind_txg)
2857 spa->spa_extreme_rewind = B_TRUE;
2858 rewind_error = spa_load_retry(spa, state, mosconfig);
2859 }
2860
428870ff
BB
2861 spa->spa_extreme_rewind = B_FALSE;
2862 spa->spa_load_max_txg = UINT64_MAX;
2863
2864 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2865 spa_config_set(spa, config);
2866
9ae529ec
CS
2867 if (state == SPA_LOAD_RECOVER) {
2868 ASSERT3P(loadinfo, ==, NULL);
2869 return (rewind_error);
2870 } else {
2871 /* Store the rewind info as part of the initial load info */
2872 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2873 spa->spa_load_info);
2874
2875 /* Restore the initial load info */
2876 fnvlist_free(spa->spa_load_info);
2877 spa->spa_load_info = loadinfo;
2878
2879 return (load_error);
2880 }
34dc7c2f
BB
2881}
2882
2883/*
2884 * Pool Open/Import
2885 *
2886 * The import case is identical to an open except that the configuration is sent
2887 * down from userland, instead of grabbed from the configuration cache. For the
2888 * case of an open, the pool configuration will exist in the
2889 * POOL_STATE_UNINITIALIZED state.
2890 *
2891 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2892 * the same time open the pool, without having to keep around the spa_t in some
2893 * ambiguous state.
2894 */
2895static int
428870ff
BB
2896spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2897 nvlist_t **config)
34dc7c2f
BB
2898{
2899 spa_t *spa;
572e2857 2900 spa_load_state_t state = SPA_LOAD_OPEN;
34dc7c2f 2901 int error;
34dc7c2f 2902 int locked = B_FALSE;
526af785 2903 int firstopen = B_FALSE;
34dc7c2f
BB
2904
2905 *spapp = NULL;
2906
2907 /*
2908 * As disgusting as this is, we need to support recursive calls to this
2909 * function because dsl_dir_open() is called during spa_load(), and ends
2910 * up calling spa_open() again. The real fix is to figure out how to
2911 * avoid dsl_dir_open() calling this in the first place.
2912 */
2913 if (mutex_owner(&spa_namespace_lock) != curthread) {
2914 mutex_enter(&spa_namespace_lock);
2915 locked = B_TRUE;
2916 }
2917
2918 if ((spa = spa_lookup(pool)) == NULL) {
2919 if (locked)
2920 mutex_exit(&spa_namespace_lock);
2e528b49 2921 return (SET_ERROR(ENOENT));
34dc7c2f 2922 }
428870ff 2923
34dc7c2f 2924 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
428870ff
BB
2925 zpool_rewind_policy_t policy;
2926
526af785
PJD
2927 firstopen = B_TRUE;
2928
428870ff
BB
2929 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2930 &policy);
2931 if (policy.zrp_request & ZPOOL_DO_REWIND)
2932 state = SPA_LOAD_RECOVER;
34dc7c2f 2933
fb5f0bc8 2934 spa_activate(spa, spa_mode_global);
34dc7c2f 2935
428870ff
BB
2936 if (state != SPA_LOAD_RECOVER)
2937 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2938
2939 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2940 policy.zrp_request);
34dc7c2f
BB
2941
2942 if (error == EBADF) {
2943 /*
2944 * If vdev_validate() returns failure (indicated by
2945 * EBADF), it indicates that one of the vdevs indicates
2946 * that the pool has been exported or destroyed. If
2947 * this is the case, the config cache is out of sync and
2948 * we should remove the pool from the namespace.
2949 */
34dc7c2f
BB
2950 spa_unload(spa);
2951 spa_deactivate(spa);
b128c09f 2952 spa_config_sync(spa, B_TRUE, B_TRUE);
34dc7c2f 2953 spa_remove(spa);
34dc7c2f
BB
2954 if (locked)
2955 mutex_exit(&spa_namespace_lock);
2e528b49 2956 return (SET_ERROR(ENOENT));
34dc7c2f
BB
2957 }
2958
2959 if (error) {
2960 /*
2961 * We can't open the pool, but we still have useful
2962 * information: the state of each vdev after the
2963 * attempted vdev_open(). Return this to the user.
2964 */
572e2857 2965 if (config != NULL && spa->spa_config) {
428870ff 2966 VERIFY(nvlist_dup(spa->spa_config, config,
b8d06fca 2967 KM_PUSHPAGE) == 0);
572e2857
BB
2968 VERIFY(nvlist_add_nvlist(*config,
2969 ZPOOL_CONFIG_LOAD_INFO,
2970 spa->spa_load_info) == 0);
2971 }
34dc7c2f
BB
2972 spa_unload(spa);
2973 spa_deactivate(spa);
428870ff 2974 spa->spa_last_open_failed = error;
34dc7c2f
BB
2975 if (locked)
2976 mutex_exit(&spa_namespace_lock);
2977 *spapp = NULL;
2978 return (error);
34dc7c2f 2979 }
34dc7c2f
BB
2980 }
2981
2982 spa_open_ref(spa, tag);
2983
b128c09f 2984 if (config != NULL)
34dc7c2f 2985 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
34dc7c2f 2986
572e2857
BB
2987 /*
2988 * If we've recovered the pool, pass back any information we
2989 * gathered while doing the load.
2990 */
2991 if (state == SPA_LOAD_RECOVER) {
2992 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2993 spa->spa_load_info) == 0);
2994 }
2995
428870ff
BB
2996 if (locked) {
2997 spa->spa_last_open_failed = 0;
2998 spa->spa_last_ubsync_txg = 0;
2999 spa->spa_load_txg = 0;
3000 mutex_exit(&spa_namespace_lock);
3001 }
3002
526af785
PJD
3003#ifdef _KERNEL
3004 if (firstopen)
3005 zvol_create_minors(spa->spa_name);
3006#endif
3007
428870ff
BB
3008 *spapp = spa;
3009
34dc7c2f
BB
3010 return (0);
3011}
3012
428870ff
BB
3013int
3014spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3015 nvlist_t **config)
3016{
3017 return (spa_open_common(name, spapp, tag, policy, config));
3018}
3019
34dc7c2f
BB
3020int
3021spa_open(const char *name, spa_t **spapp, void *tag)
3022{
428870ff 3023 return (spa_open_common(name, spapp, tag, NULL, NULL));
34dc7c2f
BB
3024}
3025
3026/*
3027 * Lookup the given spa_t, incrementing the inject count in the process,
3028 * preventing it from being exported or destroyed.
3029 */
3030spa_t *
3031spa_inject_addref(char *name)
3032{
3033 spa_t *spa;
3034
3035 mutex_enter(&spa_namespace_lock);
3036 if ((spa = spa_lookup(name)) == NULL) {
3037 mutex_exit(&spa_namespace_lock);
3038 return (NULL);
3039 }
3040 spa->spa_inject_ref++;
3041 mutex_exit(&spa_namespace_lock);
3042
3043 return (spa);
3044}
3045
3046void
3047spa_inject_delref(spa_t *spa)
3048{
3049 mutex_enter(&spa_namespace_lock);
3050 spa->spa_inject_ref--;
3051 mutex_exit(&spa_namespace_lock);
3052}
3053
3054/*
3055 * Add spares device information to the nvlist.
3056 */
3057static void
3058spa_add_spares(spa_t *spa, nvlist_t *config)
3059{
3060 nvlist_t **spares;
3061 uint_t i, nspares;
3062 nvlist_t *nvroot;
3063 uint64_t guid;
3064 vdev_stat_t *vs;
3065 uint_t vsc;
3066 uint64_t pool;
3067
9babb374
BB
3068 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3069
34dc7c2f
BB
3070 if (spa->spa_spares.sav_count == 0)
3071 return;
3072
3073 VERIFY(nvlist_lookup_nvlist(config,
3074 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3075 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3076 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3077 if (nspares != 0) {
3078 VERIFY(nvlist_add_nvlist_array(nvroot,
3079 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3080 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3081 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3082
3083 /*
3084 * Go through and find any spares which have since been
3085 * repurposed as an active spare. If this is the case, update
3086 * their status appropriately.
3087 */
3088 for (i = 0; i < nspares; i++) {
3089 VERIFY(nvlist_lookup_uint64(spares[i],
3090 ZPOOL_CONFIG_GUID, &guid) == 0);
b128c09f
BB
3091 if (spa_spare_exists(guid, &pool, NULL) &&
3092 pool != 0ULL) {
34dc7c2f 3093 VERIFY(nvlist_lookup_uint64_array(
428870ff 3094 spares[i], ZPOOL_CONFIG_VDEV_STATS,
34dc7c2f
BB
3095 (uint64_t **)&vs, &vsc) == 0);
3096 vs->vs_state = VDEV_STATE_CANT_OPEN;
3097 vs->vs_aux = VDEV_AUX_SPARED;
3098 }
3099 }
3100 }
3101}
3102
3103/*
3104 * Add l2cache device information to the nvlist, including vdev stats.
3105 */
3106static void
3107spa_add_l2cache(spa_t *spa, nvlist_t *config)
3108{
3109 nvlist_t **l2cache;
3110 uint_t i, j, nl2cache;
3111 nvlist_t *nvroot;
3112 uint64_t guid;
3113 vdev_t *vd;
3114 vdev_stat_t *vs;
3115 uint_t vsc;
3116
9babb374
BB
3117 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3118
34dc7c2f
BB
3119 if (spa->spa_l2cache.sav_count == 0)
3120 return;
3121
34dc7c2f
BB
3122 VERIFY(nvlist_lookup_nvlist(config,
3123 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3124 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3125 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3126 if (nl2cache != 0) {
3127 VERIFY(nvlist_add_nvlist_array(nvroot,
3128 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3129 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3130 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3131
3132 /*
3133 * Update level 2 cache device stats.
3134 */
3135
3136 for (i = 0; i < nl2cache; i++) {
3137 VERIFY(nvlist_lookup_uint64(l2cache[i],
3138 ZPOOL_CONFIG_GUID, &guid) == 0);
3139
3140 vd = NULL;
3141 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3142 if (guid ==
3143 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3144 vd = spa->spa_l2cache.sav_vdevs[j];
3145 break;
3146 }
3147 }
3148 ASSERT(vd != NULL);
3149
3150 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
428870ff
BB
3151 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3152 == 0);
34dc7c2f
BB
3153 vdev_get_stats(vd, vs);
3154 }
3155 }
34dc7c2f
BB
3156}
3157
9ae529ec
CS
3158static void
3159spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3160{
3161 nvlist_t *features;
3162 zap_cursor_t zc;
3163 zap_attribute_t za;
3164
3165 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3166 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3167
3168 if (spa->spa_feat_for_read_obj != 0) {
3169 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3170 spa->spa_feat_for_read_obj);
3171 zap_cursor_retrieve(&zc, &za) == 0;
3172 zap_cursor_advance(&zc)) {
3173 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3174 za.za_num_integers == 1);
3175 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3176 za.za_first_integer));
3177 }
3178 zap_cursor_fini(&zc);
3179 }
3180
3181 if (spa->spa_feat_for_write_obj != 0) {
3182 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3183 spa->spa_feat_for_write_obj);
3184 zap_cursor_retrieve(&zc, &za) == 0;
3185 zap_cursor_advance(&zc)) {
3186 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3187 za.za_num_integers == 1);
3188 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3189 za.za_first_integer));
3190 }
3191 zap_cursor_fini(&zc);
3192 }
3193
3194 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3195 features) == 0);
3196 nvlist_free(features);
3197}
3198
34dc7c2f 3199int
9ae529ec
CS
3200spa_get_stats(const char *name, nvlist_t **config,
3201 char *altroot, size_t buflen)
34dc7c2f
BB
3202{
3203 int error;
3204 spa_t *spa;
3205
3206 *config = NULL;
428870ff 3207 error = spa_open_common(name, &spa, FTAG, NULL, config);
34dc7c2f 3208
9babb374
BB
3209 if (spa != NULL) {
3210 /*
3211 * This still leaves a window of inconsistency where the spares
3212 * or l2cache devices could change and the config would be
3213 * self-inconsistent.
3214 */
3215 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f 3216
9babb374 3217 if (*config != NULL) {
572e2857
BB
3218 uint64_t loadtimes[2];
3219
3220 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3221 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3222 VERIFY(nvlist_add_uint64_array(*config,
3223 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3224
b128c09f 3225 VERIFY(nvlist_add_uint64(*config,
9babb374
BB
3226 ZPOOL_CONFIG_ERRCOUNT,
3227 spa_get_errlog_size(spa)) == 0);
3228
3229 if (spa_suspended(spa))
3230 VERIFY(nvlist_add_uint64(*config,
3231 ZPOOL_CONFIG_SUSPENDED,
3232 spa->spa_failmode) == 0);
b128c09f 3233
9babb374
BB
3234 spa_add_spares(spa, *config);
3235 spa_add_l2cache(spa, *config);
9ae529ec 3236 spa_add_feature_stats(spa, *config);
9babb374 3237 }
34dc7c2f
BB
3238 }
3239
3240 /*
3241 * We want to get the alternate root even for faulted pools, so we cheat
3242 * and call spa_lookup() directly.
3243 */
3244 if (altroot) {
3245 if (spa == NULL) {
3246 mutex_enter(&spa_namespace_lock);
3247 spa = spa_lookup(name);
3248 if (spa)
3249 spa_altroot(spa, altroot, buflen);
3250 else
3251 altroot[0] = '\0';
3252 spa = NULL;
3253 mutex_exit(&spa_namespace_lock);
3254 } else {
3255 spa_altroot(spa, altroot, buflen);
3256 }
3257 }
3258
9babb374
BB
3259 if (spa != NULL) {
3260 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 3261 spa_close(spa, FTAG);
9babb374 3262 }
34dc7c2f
BB
3263
3264 return (error);
3265}
3266
3267/*
3268 * Validate that the auxiliary device array is well formed. We must have an
3269 * array of nvlists, each which describes a valid leaf vdev. If this is an
3270 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3271 * specified, as long as they are well-formed.
3272 */
3273static int
3274spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3275 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3276 vdev_labeltype_t label)
3277{
3278 nvlist_t **dev;
3279 uint_t i, ndev;
3280 vdev_t *vd;
3281 int error;
3282
b128c09f
BB
3283 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3284
34dc7c2f
BB
3285 /*
3286 * It's acceptable to have no devs specified.
3287 */
3288 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3289 return (0);
3290
3291 if (ndev == 0)
2e528b49 3292 return (SET_ERROR(EINVAL));
34dc7c2f
BB
3293
3294 /*
3295 * Make sure the pool is formatted with a version that supports this
3296 * device type.
3297 */
3298 if (spa_version(spa) < version)
2e528b49 3299 return (SET_ERROR(ENOTSUP));
34dc7c2f
BB
3300
3301 /*
3302 * Set the pending device list so we correctly handle device in-use
3303 * checking.
3304 */
3305 sav->sav_pending = dev;
3306 sav->sav_npending = ndev;
3307
3308 for (i = 0; i < ndev; i++) {
3309 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3310 mode)) != 0)
3311 goto out;
3312
3313 if (!vd->vdev_ops->vdev_op_leaf) {
3314 vdev_free(vd);
2e528b49 3315 error = SET_ERROR(EINVAL);
34dc7c2f
BB
3316 goto out;
3317 }
3318
3319 /*
b128c09f
BB
3320 * The L2ARC currently only supports disk devices in
3321 * kernel context. For user-level testing, we allow it.
34dc7c2f 3322 */
b128c09f 3323#ifdef _KERNEL
34dc7c2f
BB
3324 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3325 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2e528b49 3326 error = SET_ERROR(ENOTBLK);
5ffb9d1d 3327 vdev_free(vd);
34dc7c2f
BB
3328 goto out;
3329 }
b128c09f 3330#endif
34dc7c2f
BB
3331 vd->vdev_top = vd;
3332
3333 if ((error = vdev_open(vd)) == 0 &&
3334 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3335 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3336 vd->vdev_guid) == 0);
3337 }
3338
3339 vdev_free(vd);
3340
3341 if (error &&
3342 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3343 goto out;
3344 else
3345 error = 0;
3346 }
3347
3348out:
3349 sav->sav_pending = NULL;
3350 sav->sav_npending = 0;
3351 return (error);
3352}
3353
3354static int
3355spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3356{
3357 int error;
3358
b128c09f
BB
3359 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3360
34dc7c2f
BB
3361 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3362 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3363 VDEV_LABEL_SPARE)) != 0) {
3364 return (error);
3365 }
3366
3367 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3368 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3369 VDEV_LABEL_L2CACHE));
3370}
3371
3372static void
3373spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3374 const char *config)
3375{
3376 int i;
3377
3378 if (sav->sav_config != NULL) {
3379 nvlist_t **olddevs;
3380 uint_t oldndevs;
3381 nvlist_t **newdevs;
3382
3383 /*
3384 * Generate new dev list by concatentating with the
3385 * current dev list.
3386 */
3387 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3388 &olddevs, &oldndevs) == 0);
3389
3390 newdevs = kmem_alloc(sizeof (void *) *
b8d06fca 3391 (ndevs + oldndevs), KM_PUSHPAGE);
34dc7c2f
BB
3392 for (i = 0; i < oldndevs; i++)
3393 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
b8d06fca 3394 KM_PUSHPAGE) == 0);
34dc7c2f
BB
3395 for (i = 0; i < ndevs; i++)
3396 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
b8d06fca 3397 KM_PUSHPAGE) == 0);
34dc7c2f
BB
3398
3399 VERIFY(nvlist_remove(sav->sav_config, config,
3400 DATA_TYPE_NVLIST_ARRAY) == 0);
3401
3402 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3403 config, newdevs, ndevs + oldndevs) == 0);
3404 for (i = 0; i < oldndevs + ndevs; i++)
3405 nvlist_free(newdevs[i]);
3406 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3407 } else {
3408 /*
3409 * Generate a new dev list.
3410 */
3411 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
b8d06fca 3412 KM_PUSHPAGE) == 0);
34dc7c2f
BB
3413 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3414 devs, ndevs) == 0);
3415 }
3416}
3417
3418/*
3419 * Stop and drop level 2 ARC devices
3420 */
3421void
3422spa_l2cache_drop(spa_t *spa)
3423{
3424 vdev_t *vd;
3425 int i;
3426 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3427
3428 for (i = 0; i < sav->sav_count; i++) {
3429 uint64_t pool;
3430
3431 vd = sav->sav_vdevs[i];
3432 ASSERT(vd != NULL);
3433
fb5f0bc8
BB
3434 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3435 pool != 0ULL && l2arc_vdev_present(vd))
34dc7c2f 3436 l2arc_remove_vdev(vd);
34dc7c2f
BB
3437 }
3438}
3439
3440/*
3441 * Pool Creation
3442 */
3443int
3444spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
6f1ffb06 3445 nvlist_t *zplprops)
34dc7c2f
BB
3446{
3447 spa_t *spa;
3448 char *altroot = NULL;
3449 vdev_t *rvd;
3450 dsl_pool_t *dp;
3451 dmu_tx_t *tx;
9babb374 3452 int error = 0;
34dc7c2f
BB
3453 uint64_t txg = TXG_INITIAL;
3454 nvlist_t **spares, **l2cache;
3455 uint_t nspares, nl2cache;
428870ff 3456 uint64_t version, obj;
9ae529ec
CS
3457 boolean_t has_features;
3458 nvpair_t *elem;
d6320ddb 3459 int c;
34dc7c2f
BB
3460
3461 /*
3462 * If this pool already exists, return failure.
3463 */
3464 mutex_enter(&spa_namespace_lock);
3465 if (spa_lookup(pool) != NULL) {
3466 mutex_exit(&spa_namespace_lock);
2e528b49 3467 return (SET_ERROR(EEXIST));
34dc7c2f
BB
3468 }
3469
3470 /*
3471 * Allocate a new spa_t structure.
3472 */
3473 (void) nvlist_lookup_string(props,
3474 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
428870ff 3475 spa = spa_add(pool, NULL, altroot);
fb5f0bc8 3476 spa_activate(spa, spa_mode_global);
34dc7c2f 3477
34dc7c2f 3478 if (props && (error = spa_prop_validate(spa, props))) {
34dc7c2f
BB
3479 spa_deactivate(spa);
3480 spa_remove(spa);
b128c09f 3481 mutex_exit(&spa_namespace_lock);
34dc7c2f
BB
3482 return (error);
3483 }
3484
9ae529ec
CS
3485 has_features = B_FALSE;
3486 for (elem = nvlist_next_nvpair(props, NULL);
3487 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3488 if (zpool_prop_feature(nvpair_name(elem)))
3489 has_features = B_TRUE;
3490 }
3491
3492 if (has_features || nvlist_lookup_uint64(props,
3493 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
34dc7c2f 3494 version = SPA_VERSION;
9ae529ec
CS
3495 }
3496 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
428870ff
BB
3497
3498 spa->spa_first_txg = txg;
3499 spa->spa_uberblock.ub_txg = txg - 1;
34dc7c2f
BB
3500 spa->spa_uberblock.ub_version = version;
3501 spa->spa_ubsync = spa->spa_uberblock;
3502
9babb374
BB
3503 /*
3504 * Create "The Godfather" zio to hold all async IOs
3505 */
3506 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3507 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3508
34dc7c2f
BB
3509 /*
3510 * Create the root vdev.
3511 */
b128c09f 3512 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
3513
3514 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3515
3516 ASSERT(error != 0 || rvd != NULL);
3517 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3518
3519 if (error == 0 && !zfs_allocatable_devs(nvroot))
2e528b49 3520 error = SET_ERROR(EINVAL);
34dc7c2f
BB
3521
3522 if (error == 0 &&
3523 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3524 (error = spa_validate_aux(spa, nvroot, txg,
3525 VDEV_ALLOC_ADD)) == 0) {
d6320ddb 3526 for (c = 0; c < rvd->vdev_children; c++) {
9babb374
BB
3527 vdev_metaslab_set_size(rvd->vdev_child[c]);
3528 vdev_expand(rvd->vdev_child[c], txg);
3529 }
34dc7c2f
BB
3530 }
3531
b128c09f 3532 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
3533
3534 if (error != 0) {
3535 spa_unload(spa);
3536 spa_deactivate(spa);
3537 spa_remove(spa);
3538 mutex_exit(&spa_namespace_lock);
3539 return (error);
3540 }
3541
3542 /*
3543 * Get the list of spares, if specified.
3544 */
3545 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3546 &spares, &nspares) == 0) {
3547 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
b8d06fca 3548 KM_PUSHPAGE) == 0);
34dc7c2f
BB
3549 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3550 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
b128c09f 3551 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3552 spa_load_spares(spa);
b128c09f 3553 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
3554 spa->spa_spares.sav_sync = B_TRUE;
3555 }
3556
3557 /*
3558 * Get the list of level 2 cache devices, if specified.
3559 */
3560 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3561 &l2cache, &nl2cache) == 0) {
3562 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
b8d06fca 3563 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
34dc7c2f
BB
3564 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3565 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
b128c09f 3566 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3567 spa_load_l2cache(spa);
b128c09f 3568 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
3569 spa->spa_l2cache.sav_sync = B_TRUE;
3570 }
3571
9ae529ec 3572 spa->spa_is_initializing = B_TRUE;
b128c09f 3573 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
34dc7c2f 3574 spa->spa_meta_objset = dp->dp_meta_objset;
9ae529ec 3575 spa->spa_is_initializing = B_FALSE;
34dc7c2f 3576
428870ff
BB
3577 /*
3578 * Create DDTs (dedup tables).
3579 */
3580 ddt_create(spa);
3581
3582 spa_update_dspace(spa);
3583
34dc7c2f
BB
3584 tx = dmu_tx_create_assigned(dp, txg);
3585
3586 /*
3587 * Create the pool config object.
3588 */
3589 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
b128c09f 3590 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
34dc7c2f
BB
3591 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3592
3593 if (zap_add(spa->spa_meta_objset,
3594 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3595 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3596 cmn_err(CE_PANIC, "failed to add pool config");
3597 }
3598
9ae529ec
CS
3599 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3600 spa_feature_create_zap_objects(spa, tx);
3601
428870ff
BB
3602 if (zap_add(spa->spa_meta_objset,
3603 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3604 sizeof (uint64_t), 1, &version, tx) != 0) {
3605 cmn_err(CE_PANIC, "failed to add pool version");
3606 }
3607
34dc7c2f
BB
3608 /* Newly created pools with the right version are always deflated. */
3609 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3610 spa->spa_deflate = TRUE;
3611 if (zap_add(spa->spa_meta_objset,
3612 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3613 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3614 cmn_err(CE_PANIC, "failed to add deflate");
3615 }
3616 }
3617
3618 /*
428870ff 3619 * Create the deferred-free bpobj. Turn off compression
34dc7c2f
BB
3620 * because sync-to-convergence takes longer if the blocksize
3621 * keeps changing.
3622 */
428870ff
BB
3623 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3624 dmu_object_set_compress(spa->spa_meta_objset, obj,
34dc7c2f 3625 ZIO_COMPRESS_OFF, tx);
34dc7c2f 3626 if (zap_add(spa->spa_meta_objset,
428870ff
BB
3627 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3628 sizeof (uint64_t), 1, &obj, tx) != 0) {
3629 cmn_err(CE_PANIC, "failed to add bpobj");
34dc7c2f 3630 }
428870ff
BB
3631 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3632 spa->spa_meta_objset, obj));
34dc7c2f
BB
3633
3634 /*
3635 * Create the pool's history object.
3636 */
3637 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3638 spa_history_create_obj(spa, tx);
3639
3640 /*
3641 * Set pool properties.
3642 */
3643 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3644 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3645 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
9babb374 3646 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
428870ff 3647
d164b209
BB
3648 if (props != NULL) {
3649 spa_configfile_set(spa, props, B_FALSE);
13fe0198 3650 spa_sync_props(props, tx);
d164b209 3651 }
34dc7c2f
BB
3652
3653 dmu_tx_commit(tx);
3654
3655 spa->spa_sync_on = B_TRUE;
3656 txg_sync_start(spa->spa_dsl_pool);
3657
3658 /*
3659 * We explicitly wait for the first transaction to complete so that our
3660 * bean counters are appropriately updated.
3661 */
3662 txg_wait_synced(spa->spa_dsl_pool, txg);
3663
b128c09f 3664 spa_config_sync(spa, B_FALSE, B_TRUE);
34dc7c2f 3665
6f1ffb06 3666 spa_history_log_version(spa, "create");
34dc7c2f 3667
b128c09f
BB
3668 spa->spa_minref = refcount_count(&spa->spa_refcount);
3669
d164b209
BB
3670 mutex_exit(&spa_namespace_lock);
3671
34dc7c2f
BB
3672 return (0);
3673}
3674
9babb374 3675#ifdef _KERNEL
34dc7c2f 3676/*
9babb374
BB
3677 * Get the root pool information from the root disk, then import the root pool
3678 * during the system boot up time.
34dc7c2f 3679 */
9babb374
BB
3680extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3681
3682static nvlist_t *
3683spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3684{
3685 nvlist_t *config;
3686 nvlist_t *nvtop, *nvroot;
3687 uint64_t pgid;
3688
3689 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3690 return (NULL);
3691
3692 /*
3693 * Add this top-level vdev to the child array.
3694 */
3695 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3696 &nvtop) == 0);
3697 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3698 &pgid) == 0);
3699 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3700
3701 /*
3702 * Put this pool's top-level vdevs into a root vdev.
3703 */
b8d06fca 3704 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
9babb374
BB
3705 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3706 VDEV_TYPE_ROOT) == 0);
3707 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3708 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3709 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3710 &nvtop, 1) == 0);
3711
3712 /*
3713 * Replace the existing vdev_tree with the new root vdev in
3714 * this pool's configuration (remove the old, add the new).
3715 */
3716 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3717 nvlist_free(nvroot);
3718 return (config);
3719}
3720
3721/*
3722 * Walk the vdev tree and see if we can find a device with "better"
3723 * configuration. A configuration is "better" if the label on that
3724 * device has a more recent txg.
3725 */
3726static void
3727spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3728{
d6320ddb
BB
3729 int c;
3730
3731 for (c = 0; c < vd->vdev_children; c++)
9babb374
BB
3732 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3733
3734 if (vd->vdev_ops->vdev_op_leaf) {
3735 nvlist_t *label;
3736 uint64_t label_txg;
3737
3738 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3739 &label) != 0)
3740 return;
3741
3742 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3743 &label_txg) == 0);
3744
3745 /*
3746 * Do we have a better boot device?
3747 */
3748 if (label_txg > *txg) {
3749 *txg = label_txg;
3750 *avd = vd;
3751 }
3752 nvlist_free(label);
3753 }
3754}
3755
3756/*
3757 * Import a root pool.
3758 *
3759 * For x86. devpath_list will consist of devid and/or physpath name of
3760 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3761 * The GRUB "findroot" command will return the vdev we should boot.
3762 *
3763 * For Sparc, devpath_list consists the physpath name of the booting device
3764 * no matter the rootpool is a single device pool or a mirrored pool.
3765 * e.g.
3766 * "/pci@1f,0/ide@d/disk@0,0:a"
3767 */
3768int
3769spa_import_rootpool(char *devpath, char *devid)
3770{
3771 spa_t *spa;
3772 vdev_t *rvd, *bvd, *avd = NULL;
3773 nvlist_t *config, *nvtop;
3774 uint64_t guid, txg;
3775 char *pname;
3776 int error;
3777
3778 /*
3779 * Read the label from the boot device and generate a configuration.
3780 */
428870ff
BB
3781 config = spa_generate_rootconf(devpath, devid, &guid);
3782#if defined(_OBP) && defined(_KERNEL)
3783 if (config == NULL) {
3784 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3785 /* iscsi boot */
3786 get_iscsi_bootpath_phy(devpath);
3787 config = spa_generate_rootconf(devpath, devid, &guid);
3788 }
3789 }
3790#endif
3791 if (config == NULL) {
9ae529ec 3792 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
9babb374 3793 devpath);
2e528b49 3794 return (SET_ERROR(EIO));
9babb374
BB
3795 }
3796
3797 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3798 &pname) == 0);
3799 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3800
3801 mutex_enter(&spa_namespace_lock);
3802 if ((spa = spa_lookup(pname)) != NULL) {
3803 /*
3804 * Remove the existing root pool from the namespace so that we
3805 * can replace it with the correct config we just read in.
3806 */
3807 spa_remove(spa);
3808 }
3809
428870ff 3810 spa = spa_add(pname, config, NULL);
9babb374 3811 spa->spa_is_root = B_TRUE;
572e2857 3812 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
9babb374
BB
3813
3814 /*
3815 * Build up a vdev tree based on the boot device's label config.
3816 */
3817 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3818 &nvtop) == 0);
3819 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3820 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3821 VDEV_ALLOC_ROOTPOOL);
3822 spa_config_exit(spa, SCL_ALL, FTAG);
3823 if (error) {
3824 mutex_exit(&spa_namespace_lock);
3825 nvlist_free(config);
3826 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3827 pname);
3828 return (error);
3829 }
3830
3831 /*
3832 * Get the boot vdev.
3833 */
3834 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3835 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3836 (u_longlong_t)guid);
2e528b49 3837 error = SET_ERROR(ENOENT);
9babb374
BB
3838 goto out;
3839 }
3840
3841 /*
3842 * Determine if there is a better boot device.
3843 */
3844 avd = bvd;
3845 spa_alt_rootvdev(rvd, &avd, &txg);
3846 if (avd != bvd) {
3847 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3848 "try booting from '%s'", avd->vdev_path);
2e528b49 3849 error = SET_ERROR(EINVAL);
9babb374
BB
3850 goto out;
3851 }
3852
3853 /*
3854 * If the boot device is part of a spare vdev then ensure that
3855 * we're booting off the active spare.
3856 */
3857 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3858 !bvd->vdev_isspare) {
3859 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3860 "try booting from '%s'",
572e2857
BB
3861 bvd->vdev_parent->
3862 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
2e528b49 3863 error = SET_ERROR(EINVAL);
9babb374
BB
3864 goto out;
3865 }
3866
9babb374
BB
3867 error = 0;
3868out:
3869 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3870 vdev_free(rvd);
3871 spa_config_exit(spa, SCL_ALL, FTAG);
3872 mutex_exit(&spa_namespace_lock);
3873
3874 nvlist_free(config);
3875 return (error);
3876}
3877
3878#endif
3879
9babb374
BB
3880/*
3881 * Import a non-root pool into the system.
3882 */
3883int
13fe0198 3884spa_import(char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
34dc7c2f
BB
3885{
3886 spa_t *spa;
3887 char *altroot = NULL;
428870ff
BB
3888 spa_load_state_t state = SPA_LOAD_IMPORT;
3889 zpool_rewind_policy_t policy;
572e2857
BB
3890 uint64_t mode = spa_mode_global;
3891 uint64_t readonly = B_FALSE;
9babb374 3892 int error;
34dc7c2f
BB
3893 nvlist_t *nvroot;
3894 nvlist_t **spares, **l2cache;
3895 uint_t nspares, nl2cache;
34dc7c2f
BB
3896
3897 /*
3898 * If a pool with this name exists, return failure.
3899 */
3900 mutex_enter(&spa_namespace_lock);
428870ff 3901 if (spa_lookup(pool) != NULL) {
9babb374 3902 mutex_exit(&spa_namespace_lock);
2e528b49 3903 return (SET_ERROR(EEXIST));
34dc7c2f
BB
3904 }
3905
3906 /*
3907 * Create and initialize the spa structure.
3908 */
3909 (void) nvlist_lookup_string(props,
3910 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
572e2857
BB
3911 (void) nvlist_lookup_uint64(props,
3912 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3913 if (readonly)
3914 mode = FREAD;
428870ff 3915 spa = spa_add(pool, config, altroot);
572e2857
BB
3916 spa->spa_import_flags = flags;
3917
3918 /*
3919 * Verbatim import - Take a pool and insert it into the namespace
3920 * as if it had been loaded at boot.
3921 */
3922 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3923 if (props != NULL)
3924 spa_configfile_set(spa, props, B_FALSE);
3925
3926 spa_config_sync(spa, B_FALSE, B_TRUE);
3927
3928 mutex_exit(&spa_namespace_lock);
572e2857
BB
3929 return (0);
3930 }
3931
3932 spa_activate(spa, mode);
34dc7c2f 3933
9babb374
BB
3934 /*
3935 * Don't start async tasks until we know everything is healthy.
3936 */
3937 spa_async_suspend(spa);
b128c09f 3938
572e2857
BB
3939 zpool_get_rewind_policy(config, &policy);
3940 if (policy.zrp_request & ZPOOL_DO_REWIND)
3941 state = SPA_LOAD_RECOVER;
3942
34dc7c2f 3943 /*
9babb374
BB
3944 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3945 * because the user-supplied config is actually the one to trust when
b128c09f 3946 * doing an import.
34dc7c2f 3947 */
428870ff
BB
3948 if (state != SPA_LOAD_RECOVER)
3949 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
572e2857 3950
428870ff
BB
3951 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3952 policy.zrp_request);
3953
3954 /*
572e2857
BB
3955 * Propagate anything learned while loading the pool and pass it
3956 * back to caller (i.e. rewind info, missing devices, etc).
428870ff 3957 */
572e2857
BB
3958 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3959 spa->spa_load_info) == 0);
34dc7c2f 3960
b128c09f 3961 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 3962 /*
9babb374
BB
3963 * Toss any existing sparelist, as it doesn't have any validity
3964 * anymore, and conflicts with spa_has_spare().
34dc7c2f 3965 */
9babb374 3966 if (spa->spa_spares.sav_config) {
34dc7c2f
BB
3967 nvlist_free(spa->spa_spares.sav_config);
3968 spa->spa_spares.sav_config = NULL;
3969 spa_load_spares(spa);
3970 }
9babb374 3971 if (spa->spa_l2cache.sav_config) {
34dc7c2f
BB
3972 nvlist_free(spa->spa_l2cache.sav_config);
3973 spa->spa_l2cache.sav_config = NULL;
3974 spa_load_l2cache(spa);
3975 }
3976
3977 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3978 &nvroot) == 0);
3979 if (error == 0)
9babb374
BB
3980 error = spa_validate_aux(spa, nvroot, -1ULL,
3981 VDEV_ALLOC_SPARE);
34dc7c2f
BB
3982 if (error == 0)
3983 error = spa_validate_aux(spa, nvroot, -1ULL,
3984 VDEV_ALLOC_L2CACHE);
b128c09f 3985 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f 3986
d164b209
BB
3987 if (props != NULL)
3988 spa_configfile_set(spa, props, B_FALSE);
3989
fb5f0bc8
BB
3990 if (error != 0 || (props && spa_writeable(spa) &&
3991 (error = spa_prop_set(spa, props)))) {
9babb374
BB
3992 spa_unload(spa);
3993 spa_deactivate(spa);
3994 spa_remove(spa);
34dc7c2f
BB
3995 mutex_exit(&spa_namespace_lock);
3996 return (error);
3997 }
3998
572e2857
BB
3999 spa_async_resume(spa);
4000
34dc7c2f
BB
4001 /*
4002 * Override any spares and level 2 cache devices as specified by
4003 * the user, as these may have correct device names/devids, etc.
4004 */
4005 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4006 &spares, &nspares) == 0) {
4007 if (spa->spa_spares.sav_config)
4008 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4009 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4010 else
4011 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
b8d06fca 4012 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
34dc7c2f
BB
4013 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4014 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
b128c09f 4015 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4016 spa_load_spares(spa);
b128c09f 4017 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4018 spa->spa_spares.sav_sync = B_TRUE;
4019 }
4020 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4021 &l2cache, &nl2cache) == 0) {
4022 if (spa->spa_l2cache.sav_config)
4023 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4024 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4025 else
4026 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
b8d06fca 4027 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
34dc7c2f
BB
4028 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4029 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
b128c09f 4030 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4031 spa_load_l2cache(spa);
b128c09f 4032 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4033 spa->spa_l2cache.sav_sync = B_TRUE;
4034 }
4035
428870ff
BB
4036 /*
4037 * Check for any removed devices.
4038 */
4039 if (spa->spa_autoreplace) {
4040 spa_aux_check_removed(&spa->spa_spares);
4041 spa_aux_check_removed(&spa->spa_l2cache);
4042 }
4043
fb5f0bc8 4044 if (spa_writeable(spa)) {
b128c09f
BB
4045 /*
4046 * Update the config cache to include the newly-imported pool.
4047 */
45d1cae3 4048 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
b128c09f 4049 }
34dc7c2f 4050
34dc7c2f 4051 /*
9babb374
BB
4052 * It's possible that the pool was expanded while it was exported.
4053 * We kick off an async task to handle this for us.
34dc7c2f 4054 */
9babb374 4055 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
b128c09f 4056
9babb374 4057 mutex_exit(&spa_namespace_lock);
6f1ffb06 4058 spa_history_log_version(spa, "import");
b128c09f 4059
526af785
PJD
4060#ifdef _KERNEL
4061 zvol_create_minors(pool);
4062#endif
4063
b128c09f
BB
4064 return (0);
4065}
4066
34dc7c2f
BB
4067nvlist_t *
4068spa_tryimport(nvlist_t *tryconfig)
4069{
4070 nvlist_t *config = NULL;
4071 char *poolname;
4072 spa_t *spa;
4073 uint64_t state;
d164b209 4074 int error;
34dc7c2f
BB
4075
4076 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4077 return (NULL);
4078
4079 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4080 return (NULL);
4081
4082 /*
4083 * Create and initialize the spa structure.
4084 */
4085 mutex_enter(&spa_namespace_lock);
428870ff 4086 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
fb5f0bc8 4087 spa_activate(spa, FREAD);
34dc7c2f
BB
4088
4089 /*
4090 * Pass off the heavy lifting to spa_load().
4091 * Pass TRUE for mosconfig because the user-supplied config
4092 * is actually the one to trust when doing an import.
4093 */
428870ff 4094 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
34dc7c2f
BB
4095
4096 /*
4097 * If 'tryconfig' was at least parsable, return the current config.
4098 */
4099 if (spa->spa_root_vdev != NULL) {
34dc7c2f 4100 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
34dc7c2f
BB
4101 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4102 poolname) == 0);
4103 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4104 state) == 0);
4105 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4106 spa->spa_uberblock.ub_timestamp) == 0);
9ae529ec
CS
4107 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4108 spa->spa_load_info) == 0);
ffe9d382
BB
4109 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_ERRATA,
4110 spa->spa_errata) == 0);
34dc7c2f
BB
4111
4112 /*
4113 * If the bootfs property exists on this pool then we
4114 * copy it out so that external consumers can tell which
4115 * pools are bootable.
4116 */
d164b209 4117 if ((!error || error == EEXIST) && spa->spa_bootfs) {
b8d06fca 4118 char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
34dc7c2f
BB
4119
4120 /*
4121 * We have to play games with the name since the
4122 * pool was opened as TRYIMPORT_NAME.
4123 */
b128c09f 4124 if (dsl_dsobj_to_dsname(spa_name(spa),
34dc7c2f
BB
4125 spa->spa_bootfs, tmpname) == 0) {
4126 char *cp;
d1d7e268
MK
4127 char *dsname;
4128
4129 dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
34dc7c2f
BB
4130
4131 cp = strchr(tmpname, '/');
4132 if (cp == NULL) {
4133 (void) strlcpy(dsname, tmpname,
4134 MAXPATHLEN);
4135 } else {
4136 (void) snprintf(dsname, MAXPATHLEN,
4137 "%s/%s", poolname, ++cp);
4138 }
4139 VERIFY(nvlist_add_string(config,
4140 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4141 kmem_free(dsname, MAXPATHLEN);
4142 }
4143 kmem_free(tmpname, MAXPATHLEN);
4144 }
4145
4146 /*
4147 * Add the list of hot spares and level 2 cache devices.
4148 */
9babb374 4149 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f
BB
4150 spa_add_spares(spa, config);
4151 spa_add_l2cache(spa, config);
9babb374 4152 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f
BB
4153 }
4154
4155 spa_unload(spa);
4156 spa_deactivate(spa);
4157 spa_remove(spa);
4158 mutex_exit(&spa_namespace_lock);
4159
4160 return (config);
4161}
4162
4163/*
4164 * Pool export/destroy
4165 *
4166 * The act of destroying or exporting a pool is very simple. We make sure there
4167 * is no more pending I/O and any references to the pool are gone. Then, we
4168 * update the pool state and sync all the labels to disk, removing the
fb5f0bc8
BB
4169 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4170 * we don't sync the labels or remove the configuration cache.
34dc7c2f
BB
4171 */
4172static int
b128c09f 4173spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
fb5f0bc8 4174 boolean_t force, boolean_t hardforce)
34dc7c2f
BB
4175{
4176 spa_t *spa;
4177
4178 if (oldconfig)
4179 *oldconfig = NULL;
4180
fb5f0bc8 4181 if (!(spa_mode_global & FWRITE))
2e528b49 4182 return (SET_ERROR(EROFS));
34dc7c2f
BB
4183
4184 mutex_enter(&spa_namespace_lock);
4185 if ((spa = spa_lookup(pool)) == NULL) {
4186 mutex_exit(&spa_namespace_lock);
2e528b49 4187 return (SET_ERROR(ENOENT));
34dc7c2f
BB
4188 }
4189
4190 /*
4191 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4192 * reacquire the namespace lock, and see if we can export.
4193 */
4194 spa_open_ref(spa, FTAG);
4195 mutex_exit(&spa_namespace_lock);
4196 spa_async_suspend(spa);
4197 mutex_enter(&spa_namespace_lock);
4198 spa_close(spa, FTAG);
4199
4200 /*
4201 * The pool will be in core if it's openable,
4202 * in which case we can modify its state.
4203 */
4204 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4205 /*
4206 * Objsets may be open only because they're dirty, so we
4207 * have to force it to sync before checking spa_refcnt.
4208 */
34dc7c2f
BB
4209 txg_wait_synced(spa->spa_dsl_pool, 0);
4210
4211 /*
4212 * A pool cannot be exported or destroyed if there are active
4213 * references. If we are resetting a pool, allow references by
4214 * fault injection handlers.
4215 */
4216 if (!spa_refcount_zero(spa) ||
4217 (spa->spa_inject_ref != 0 &&
4218 new_state != POOL_STATE_UNINITIALIZED)) {
34dc7c2f
BB
4219 spa_async_resume(spa);
4220 mutex_exit(&spa_namespace_lock);
2e528b49 4221 return (SET_ERROR(EBUSY));
34dc7c2f
BB
4222 }
4223
b128c09f
BB
4224 /*
4225 * A pool cannot be exported if it has an active shared spare.
4226 * This is to prevent other pools stealing the active spare
4227 * from an exported pool. At user's own will, such pool can
4228 * be forcedly exported.
4229 */
4230 if (!force && new_state == POOL_STATE_EXPORTED &&
4231 spa_has_active_shared_spare(spa)) {
4232 spa_async_resume(spa);
4233 mutex_exit(&spa_namespace_lock);
2e528b49 4234 return (SET_ERROR(EXDEV));
b128c09f 4235 }
34dc7c2f
BB
4236
4237 /*
4238 * We want this to be reflected on every label,
4239 * so mark them all dirty. spa_unload() will do the
4240 * final sync that pushes these changes out.
4241 */
fb5f0bc8 4242 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
b128c09f 4243 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f 4244 spa->spa_state = new_state;
428870ff
BB
4245 spa->spa_final_txg = spa_last_synced_txg(spa) +
4246 TXG_DEFER_SIZE + 1;
34dc7c2f 4247 vdev_config_dirty(spa->spa_root_vdev);
b128c09f 4248 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
4249 }
4250 }
4251
26685276 4252 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
34dc7c2f
BB
4253
4254 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4255 spa_unload(spa);
4256 spa_deactivate(spa);
4257 }
4258
4259 if (oldconfig && spa->spa_config)
4260 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4261
4262 if (new_state != POOL_STATE_UNINITIALIZED) {
fb5f0bc8
BB
4263 if (!hardforce)
4264 spa_config_sync(spa, B_TRUE, B_TRUE);
34dc7c2f 4265 spa_remove(spa);
34dc7c2f
BB
4266 }
4267 mutex_exit(&spa_namespace_lock);
4268
4269 return (0);
4270}
4271
4272/*
4273 * Destroy a storage pool.
4274 */
4275int
4276spa_destroy(char *pool)
4277{
fb5f0bc8
BB
4278 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4279 B_FALSE, B_FALSE));
34dc7c2f
BB
4280}
4281
4282/*
4283 * Export a storage pool.
4284 */
4285int
fb5f0bc8
BB
4286spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4287 boolean_t hardforce)
34dc7c2f 4288{
fb5f0bc8
BB
4289 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4290 force, hardforce));
34dc7c2f
BB
4291}
4292
4293/*
4294 * Similar to spa_export(), this unloads the spa_t without actually removing it
4295 * from the namespace in any way.
4296 */
4297int
4298spa_reset(char *pool)
4299{
b128c09f 4300 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
fb5f0bc8 4301 B_FALSE, B_FALSE));
34dc7c2f
BB
4302}
4303
34dc7c2f
BB
4304/*
4305 * ==========================================================================
4306 * Device manipulation
4307 * ==========================================================================
4308 */
4309
4310/*
4311 * Add a device to a storage pool.
4312 */
4313int
4314spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4315{
428870ff 4316 uint64_t txg, id;
fb5f0bc8 4317 int error;
34dc7c2f
BB
4318 vdev_t *rvd = spa->spa_root_vdev;
4319 vdev_t *vd, *tvd;
4320 nvlist_t **spares, **l2cache;
4321 uint_t nspares, nl2cache;
d6320ddb 4322 int c;
34dc7c2f 4323
572e2857
BB
4324 ASSERT(spa_writeable(spa));
4325
34dc7c2f
BB
4326 txg = spa_vdev_enter(spa);
4327
4328 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4329 VDEV_ALLOC_ADD)) != 0)
4330 return (spa_vdev_exit(spa, NULL, txg, error));
4331
b128c09f 4332 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
34dc7c2f
BB
4333
4334 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4335 &nspares) != 0)
4336 nspares = 0;
4337
4338 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4339 &nl2cache) != 0)
4340 nl2cache = 0;
4341
b128c09f 4342 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
34dc7c2f 4343 return (spa_vdev_exit(spa, vd, txg, EINVAL));
34dc7c2f 4344
b128c09f
BB
4345 if (vd->vdev_children != 0 &&
4346 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4347 return (spa_vdev_exit(spa, vd, txg, error));
34dc7c2f
BB
4348
4349 /*
4350 * We must validate the spares and l2cache devices after checking the
4351 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4352 */
b128c09f 4353 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
34dc7c2f 4354 return (spa_vdev_exit(spa, vd, txg, error));
34dc7c2f
BB
4355
4356 /*
4357 * Transfer each new top-level vdev from vd to rvd.
4358 */
d6320ddb 4359 for (c = 0; c < vd->vdev_children; c++) {
428870ff
BB
4360
4361 /*
4362 * Set the vdev id to the first hole, if one exists.
4363 */
4364 for (id = 0; id < rvd->vdev_children; id++) {
4365 if (rvd->vdev_child[id]->vdev_ishole) {
4366 vdev_free(rvd->vdev_child[id]);
4367 break;
4368 }
4369 }
34dc7c2f
BB
4370 tvd = vd->vdev_child[c];
4371 vdev_remove_child(vd, tvd);
428870ff 4372 tvd->vdev_id = id;
34dc7c2f
BB
4373 vdev_add_child(rvd, tvd);
4374 vdev_config_dirty(tvd);
4375 }
4376
4377 if (nspares != 0) {
4378 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4379 ZPOOL_CONFIG_SPARES);
4380 spa_load_spares(spa);
4381 spa->spa_spares.sav_sync = B_TRUE;
4382 }
4383
4384 if (nl2cache != 0) {
4385 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4386 ZPOOL_CONFIG_L2CACHE);
4387 spa_load_l2cache(spa);
4388 spa->spa_l2cache.sav_sync = B_TRUE;
4389 }
4390
4391 /*
4392 * We have to be careful when adding new vdevs to an existing pool.
4393 * If other threads start allocating from these vdevs before we
4394 * sync the config cache, and we lose power, then upon reboot we may
4395 * fail to open the pool because there are DVAs that the config cache
4396 * can't translate. Therefore, we first add the vdevs without
4397 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4398 * and then let spa_config_update() initialize the new metaslabs.
4399 *
4400 * spa_load() checks for added-but-not-initialized vdevs, so that
4401 * if we lose power at any point in this sequence, the remaining
4402 * steps will be completed the next time we load the pool.
4403 */
4404 (void) spa_vdev_exit(spa, vd, txg, 0);
4405
4406 mutex_enter(&spa_namespace_lock);
4407 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4408 mutex_exit(&spa_namespace_lock);
4409
4410 return (0);
4411}
4412
4413/*
4414 * Attach a device to a mirror. The arguments are the path to any device
4415 * in the mirror, and the nvroot for the new device. If the path specifies
4416 * a device that is not mirrored, we automatically insert the mirror vdev.
4417 *
4418 * If 'replacing' is specified, the new device is intended to replace the
4419 * existing device; in this case the two devices are made into their own
4420 * mirror using the 'replacing' vdev, which is functionally identical to
4421 * the mirror vdev (it actually reuses all the same ops) but has a few
4422 * extra rules: you can't attach to it after it's been created, and upon
4423 * completion of resilvering, the first disk (the one being replaced)
4424 * is automatically detached.
4425 */
4426int
4427spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4428{
428870ff 4429 uint64_t txg, dtl_max_txg;
34dc7c2f
BB
4430 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4431 vdev_ops_t *pvops;
b128c09f
BB
4432 char *oldvdpath, *newvdpath;
4433 int newvd_isspare;
4434 int error;
2e528b49 4435 ASSERTV(vdev_t *rvd = spa->spa_root_vdev);
34dc7c2f 4436
572e2857
BB
4437 ASSERT(spa_writeable(spa));
4438
34dc7c2f
BB
4439 txg = spa_vdev_enter(spa);
4440
b128c09f 4441 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f
BB
4442
4443 if (oldvd == NULL)
4444 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4445
4446 if (!oldvd->vdev_ops->vdev_op_leaf)
4447 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4448
4449 pvd = oldvd->vdev_parent;
4450
4451 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
5ffb9d1d 4452 VDEV_ALLOC_ATTACH)) != 0)
34dc7c2f
BB
4453 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4454
4455 if (newrootvd->vdev_children != 1)
4456 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4457
4458 newvd = newrootvd->vdev_child[0];
4459
4460 if (!newvd->vdev_ops->vdev_op_leaf)
4461 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4462
4463 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4464 return (spa_vdev_exit(spa, newrootvd, txg, error));
4465
4466 /*
4467 * Spares can't replace logs
4468 */
b128c09f 4469 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
34dc7c2f
BB
4470 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4471
4472 if (!replacing) {
4473 /*
4474 * For attach, the only allowable parent is a mirror or the root
4475 * vdev.
4476 */
4477 if (pvd->vdev_ops != &vdev_mirror_ops &&
4478 pvd->vdev_ops != &vdev_root_ops)
4479 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4480
4481 pvops = &vdev_mirror_ops;
4482 } else {
4483 /*
4484 * Active hot spares can only be replaced by inactive hot
4485 * spares.
4486 */
4487 if (pvd->vdev_ops == &vdev_spare_ops &&
572e2857 4488 oldvd->vdev_isspare &&
34dc7c2f
BB
4489 !spa_has_spare(spa, newvd->vdev_guid))
4490 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4491
4492 /*
4493 * If the source is a hot spare, and the parent isn't already a
4494 * spare, then we want to create a new hot spare. Otherwise, we
4495 * want to create a replacing vdev. The user is not allowed to
4496 * attach to a spared vdev child unless the 'isspare' state is
4497 * the same (spare replaces spare, non-spare replaces
4498 * non-spare).
4499 */
572e2857
BB
4500 if (pvd->vdev_ops == &vdev_replacing_ops &&
4501 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
34dc7c2f 4502 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
4503 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4504 newvd->vdev_isspare != oldvd->vdev_isspare) {
34dc7c2f 4505 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
572e2857
BB
4506 }
4507
4508 if (newvd->vdev_isspare)
34dc7c2f
BB
4509 pvops = &vdev_spare_ops;
4510 else
4511 pvops = &vdev_replacing_ops;
4512 }
4513
4514 /*
9babb374 4515 * Make sure the new device is big enough.
34dc7c2f 4516 */
9babb374 4517 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
34dc7c2f
BB
4518 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4519
4520 /*
4521 * The new device cannot have a higher alignment requirement
4522 * than the top-level vdev.
4523 */
4524 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4525 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4526
4527 /*
4528 * If this is an in-place replacement, update oldvd's path and devid
4529 * to make it distinguishable from newvd, and unopenable from now on.
4530 */
4531 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4532 spa_strfree(oldvd->vdev_path);
4533 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
b8d06fca 4534 KM_PUSHPAGE);
34dc7c2f
BB
4535 (void) sprintf(oldvd->vdev_path, "%s/%s",
4536 newvd->vdev_path, "old");
4537 if (oldvd->vdev_devid != NULL) {
4538 spa_strfree(oldvd->vdev_devid);
4539 oldvd->vdev_devid = NULL;
4540 }
4541 }
4542
572e2857 4543 /* mark the device being resilvered */
5d1f7fb6 4544 newvd->vdev_resilver_txg = txg;
572e2857 4545
34dc7c2f
BB
4546 /*
4547 * If the parent is not a mirror, or if we're replacing, insert the new
4548 * mirror/replacing/spare vdev above oldvd.
4549 */
4550 if (pvd->vdev_ops != pvops)
4551 pvd = vdev_add_parent(oldvd, pvops);
4552
4553 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4554 ASSERT(pvd->vdev_ops == pvops);
4555 ASSERT(oldvd->vdev_parent == pvd);
4556
4557 /*
4558 * Extract the new device from its root and add it to pvd.
4559 */
4560 vdev_remove_child(newrootvd, newvd);
4561 newvd->vdev_id = pvd->vdev_children;
428870ff 4562 newvd->vdev_crtxg = oldvd->vdev_crtxg;
34dc7c2f
BB
4563 vdev_add_child(pvd, newvd);
4564
34dc7c2f
BB
4565 tvd = newvd->vdev_top;
4566 ASSERT(pvd->vdev_top == tvd);
4567 ASSERT(tvd->vdev_parent == rvd);
4568
4569 vdev_config_dirty(tvd);
4570
4571 /*
428870ff
BB
4572 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4573 * for any dmu_sync-ed blocks. It will propagate upward when
4574 * spa_vdev_exit() calls vdev_dtl_reassess().
34dc7c2f 4575 */
428870ff 4576 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
34dc7c2f 4577
428870ff
BB
4578 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4579 dtl_max_txg - TXG_INITIAL);
34dc7c2f 4580
9babb374 4581 if (newvd->vdev_isspare) {
34dc7c2f 4582 spa_spare_activate(newvd);
26685276 4583 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
9babb374
BB
4584 }
4585
b128c09f
BB
4586 oldvdpath = spa_strdup(oldvd->vdev_path);
4587 newvdpath = spa_strdup(newvd->vdev_path);
4588 newvd_isspare = newvd->vdev_isspare;
34dc7c2f
BB
4589
4590 /*
4591 * Mark newvd's DTL dirty in this txg.
4592 */
4593 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4594
428870ff 4595 /*
93cf2076
GW
4596 * Schedule the resilver to restart in the future. We do this to
4597 * ensure that dmu_sync-ed blocks have been stitched into the
4598 * respective datasets.
428870ff
BB
4599 */
4600 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4601
4602 /*
4603 * Commit the config
4604 */
4605 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
34dc7c2f 4606
6f1ffb06 4607 spa_history_log_internal(spa, "vdev attach", NULL,
428870ff 4608 "%s vdev=%s %s vdev=%s",
45d1cae3
BB
4609 replacing && newvd_isspare ? "spare in" :
4610 replacing ? "replace" : "attach", newvdpath,
4611 replacing ? "for" : "to", oldvdpath);
b128c09f
BB
4612
4613 spa_strfree(oldvdpath);
4614 spa_strfree(newvdpath);
4615
572e2857 4616 if (spa->spa_bootfs)
26685276 4617 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
572e2857 4618
34dc7c2f
BB
4619 return (0);
4620}
4621
4622/*
4623 * Detach a device from a mirror or replacing vdev.
d3cc8b15 4624 *
34dc7c2f
BB
4625 * If 'replace_done' is specified, only detach if the parent
4626 * is a replacing vdev.
4627 */
4628int
fb5f0bc8 4629spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
34dc7c2f
BB
4630{
4631 uint64_t txg;
fb5f0bc8 4632 int error;
34dc7c2f
BB
4633 vdev_t *vd, *pvd, *cvd, *tvd;
4634 boolean_t unspare = B_FALSE;
d4ed6673 4635 uint64_t unspare_guid = 0;
428870ff 4636 char *vdpath;
d6320ddb 4637 int c, t;
2e528b49 4638 ASSERTV(vdev_t *rvd = spa->spa_root_vdev);
572e2857
BB
4639 ASSERT(spa_writeable(spa));
4640
34dc7c2f
BB
4641 txg = spa_vdev_enter(spa);
4642
b128c09f 4643 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f
BB
4644
4645 if (vd == NULL)
4646 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4647
4648 if (!vd->vdev_ops->vdev_op_leaf)
4649 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4650
4651 pvd = vd->vdev_parent;
4652
fb5f0bc8
BB
4653 /*
4654 * If the parent/child relationship is not as expected, don't do it.
4655 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4656 * vdev that's replacing B with C. The user's intent in replacing
4657 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4658 * the replace by detaching C, the expected behavior is to end up
4659 * M(A,B). But suppose that right after deciding to detach C,
4660 * the replacement of B completes. We would have M(A,C), and then
4661 * ask to detach C, which would leave us with just A -- not what
4662 * the user wanted. To prevent this, we make sure that the
4663 * parent/child relationship hasn't changed -- in this example,
4664 * that C's parent is still the replacing vdev R.
4665 */
4666 if (pvd->vdev_guid != pguid && pguid != 0)
4667 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4668
34dc7c2f 4669 /*
572e2857 4670 * Only 'replacing' or 'spare' vdevs can be replaced.
34dc7c2f 4671 */
572e2857
BB
4672 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4673 pvd->vdev_ops != &vdev_spare_ops)
4674 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
34dc7c2f
BB
4675
4676 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4677 spa_version(spa) >= SPA_VERSION_SPARES);
4678
4679 /*
4680 * Only mirror, replacing, and spare vdevs support detach.
4681 */
4682 if (pvd->vdev_ops != &vdev_replacing_ops &&
4683 pvd->vdev_ops != &vdev_mirror_ops &&
4684 pvd->vdev_ops != &vdev_spare_ops)
4685 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4686
4687 /*
fb5f0bc8
BB
4688 * If this device has the only valid copy of some data,
4689 * we cannot safely detach it.
34dc7c2f 4690 */
fb5f0bc8 4691 if (vdev_dtl_required(vd))
34dc7c2f
BB
4692 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4693
fb5f0bc8 4694 ASSERT(pvd->vdev_children >= 2);
34dc7c2f 4695
b128c09f
BB
4696 /*
4697 * If we are detaching the second disk from a replacing vdev, then
4698 * check to see if we changed the original vdev's path to have "/old"
4699 * at the end in spa_vdev_attach(). If so, undo that change now.
4700 */
572e2857
BB
4701 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4702 vd->vdev_path != NULL) {
4703 size_t len = strlen(vd->vdev_path);
4704
d6320ddb 4705 for (c = 0; c < pvd->vdev_children; c++) {
572e2857
BB
4706 cvd = pvd->vdev_child[c];
4707
4708 if (cvd == vd || cvd->vdev_path == NULL)
4709 continue;
4710
4711 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4712 strcmp(cvd->vdev_path + len, "/old") == 0) {
4713 spa_strfree(cvd->vdev_path);
4714 cvd->vdev_path = spa_strdup(vd->vdev_path);
4715 break;
4716 }
b128c09f
BB
4717 }
4718 }
4719
34dc7c2f
BB
4720 /*
4721 * If we are detaching the original disk from a spare, then it implies
4722 * that the spare should become a real disk, and be removed from the
4723 * active spare list for the pool.
4724 */
4725 if (pvd->vdev_ops == &vdev_spare_ops &&
572e2857
BB
4726 vd->vdev_id == 0 &&
4727 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
34dc7c2f
BB
4728 unspare = B_TRUE;
4729
4730 /*
4731 * Erase the disk labels so the disk can be used for other things.
4732 * This must be done after all other error cases are handled,
4733 * but before we disembowel vd (so we can still do I/O to it).
4734 * But if we can't do it, don't treat the error as fatal --
4735 * it may be that the unwritability of the disk is the reason
4736 * it's being detached!
4737 */
4738 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4739
4740 /*
4741 * Remove vd from its parent and compact the parent's children.
4742 */
4743 vdev_remove_child(pvd, vd);
4744 vdev_compact_children(pvd);
4745
4746 /*
4747 * Remember one of the remaining children so we can get tvd below.
4748 */
572e2857 4749 cvd = pvd->vdev_child[pvd->vdev_children - 1];
34dc7c2f
BB
4750
4751 /*
4752 * If we need to remove the remaining child from the list of hot spares,
fb5f0bc8
BB
4753 * do it now, marking the vdev as no longer a spare in the process.
4754 * We must do this before vdev_remove_parent(), because that can
4755 * change the GUID if it creates a new toplevel GUID. For a similar
4756 * reason, we must remove the spare now, in the same txg as the detach;
4757 * otherwise someone could attach a new sibling, change the GUID, and
4758 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
34dc7c2f
BB
4759 */
4760 if (unspare) {
4761 ASSERT(cvd->vdev_isspare);
4762 spa_spare_remove(cvd);
4763 unspare_guid = cvd->vdev_guid;
fb5f0bc8 4764 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
572e2857 4765 cvd->vdev_unspare = B_TRUE;
34dc7c2f
BB
4766 }
4767
428870ff
BB
4768 /*
4769 * If the parent mirror/replacing vdev only has one child,
4770 * the parent is no longer needed. Remove it from the tree.
4771 */
572e2857
BB
4772 if (pvd->vdev_children == 1) {
4773 if (pvd->vdev_ops == &vdev_spare_ops)
4774 cvd->vdev_unspare = B_FALSE;
428870ff 4775 vdev_remove_parent(cvd);
572e2857
BB
4776 }
4777
428870ff
BB
4778
4779 /*
4780 * We don't set tvd until now because the parent we just removed
4781 * may have been the previous top-level vdev.
4782 */
4783 tvd = cvd->vdev_top;
4784 ASSERT(tvd->vdev_parent == rvd);
4785
4786 /*
4787 * Reevaluate the parent vdev state.
4788 */
4789 vdev_propagate_state(cvd);
4790
4791 /*
4792 * If the 'autoexpand' property is set on the pool then automatically
4793 * try to expand the size of the pool. For example if the device we
4794 * just detached was smaller than the others, it may be possible to
4795 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4796 * first so that we can obtain the updated sizes of the leaf vdevs.
4797 */
4798 if (spa->spa_autoexpand) {
4799 vdev_reopen(tvd);
4800 vdev_expand(tvd, txg);
4801 }
4802
4803 vdev_config_dirty(tvd);
4804
4805 /*
4806 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4807 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4808 * But first make sure we're not on any *other* txg's DTL list, to
4809 * prevent vd from being accessed after it's freed.
4810 */
4811 vdpath = spa_strdup(vd->vdev_path);
d6320ddb 4812 for (t = 0; t < TXG_SIZE; t++)
428870ff
BB
4813 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4814 vd->vdev_detached = B_TRUE;
4815 vdev_dirty(tvd, VDD_DTL, vd, txg);
4816
26685276 4817 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
428870ff 4818
572e2857
BB
4819 /* hang on to the spa before we release the lock */
4820 spa_open_ref(spa, FTAG);
4821
428870ff
BB
4822 error = spa_vdev_exit(spa, vd, txg, 0);
4823
6f1ffb06 4824 spa_history_log_internal(spa, "detach", NULL,
428870ff
BB
4825 "vdev=%s", vdpath);
4826 spa_strfree(vdpath);
4827
4828 /*
4829 * If this was the removal of the original device in a hot spare vdev,
4830 * then we want to go through and remove the device from the hot spare
4831 * list of every other pool.
4832 */
4833 if (unspare) {
572e2857
BB
4834 spa_t *altspa = NULL;
4835
428870ff 4836 mutex_enter(&spa_namespace_lock);
572e2857
BB
4837 while ((altspa = spa_next(altspa)) != NULL) {
4838 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4839 altspa == spa)
428870ff 4840 continue;
572e2857
BB
4841
4842 spa_open_ref(altspa, FTAG);
428870ff 4843 mutex_exit(&spa_namespace_lock);
572e2857 4844 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
428870ff 4845 mutex_enter(&spa_namespace_lock);
572e2857 4846 spa_close(altspa, FTAG);
428870ff
BB
4847 }
4848 mutex_exit(&spa_namespace_lock);
572e2857
BB
4849
4850 /* search the rest of the vdevs for spares to remove */
4851 spa_vdev_resilver_done(spa);
428870ff
BB
4852 }
4853
572e2857
BB
4854 /* all done with the spa; OK to release */
4855 mutex_enter(&spa_namespace_lock);
4856 spa_close(spa, FTAG);
4857 mutex_exit(&spa_namespace_lock);
4858
428870ff
BB
4859 return (error);
4860}
4861
4862/*
4863 * Split a set of devices from their mirrors, and create a new pool from them.
4864 */
4865int
4866spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4867 nvlist_t *props, boolean_t exp)
4868{
4869 int error = 0;
4870 uint64_t txg, *glist;
4871 spa_t *newspa;
4872 uint_t c, children, lastlog;
4873 nvlist_t **child, *nvl, *tmp;
4874 dmu_tx_t *tx;
4875 char *altroot = NULL;
4876 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4877 boolean_t activate_slog;
4878
572e2857 4879 ASSERT(spa_writeable(spa));
428870ff
BB
4880
4881 txg = spa_vdev_enter(spa);
4882
4883 /* clear the log and flush everything up to now */
4884 activate_slog = spa_passivate_log(spa);
4885 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4886 error = spa_offline_log(spa);
4887 txg = spa_vdev_config_enter(spa);
4888
4889 if (activate_slog)
4890 spa_activate_log(spa);
4891
4892 if (error != 0)
4893 return (spa_vdev_exit(spa, NULL, txg, error));
4894
4895 /* check new spa name before going any further */
4896 if (spa_lookup(newname) != NULL)
4897 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4898
4899 /*
4900 * scan through all the children to ensure they're all mirrors
4901 */
4902 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4903 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4904 &children) != 0)
4905 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4906
4907 /* first, check to ensure we've got the right child count */
4908 rvd = spa->spa_root_vdev;
4909 lastlog = 0;
4910 for (c = 0; c < rvd->vdev_children; c++) {
4911 vdev_t *vd = rvd->vdev_child[c];
4912
4913 /* don't count the holes & logs as children */
4914 if (vd->vdev_islog || vd->vdev_ishole) {
4915 if (lastlog == 0)
4916 lastlog = c;
4917 continue;
4918 }
4919
4920 lastlog = 0;
4921 }
4922 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4923 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4924
4925 /* next, ensure no spare or cache devices are part of the split */
4926 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4927 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4928 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4929
b8d06fca
RY
4930 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE);
4931 glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE);
428870ff
BB
4932
4933 /* then, loop over each vdev and validate it */
4934 for (c = 0; c < children; c++) {
4935 uint64_t is_hole = 0;
4936
4937 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4938 &is_hole);
4939
4940 if (is_hole != 0) {
4941 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4942 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4943 continue;
4944 } else {
2e528b49 4945 error = SET_ERROR(EINVAL);
428870ff
BB
4946 break;
4947 }
4948 }
4949
4950 /* which disk is going to be split? */
4951 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4952 &glist[c]) != 0) {
2e528b49 4953 error = SET_ERROR(EINVAL);
428870ff
BB
4954 break;
4955 }
4956
4957 /* look it up in the spa */
4958 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4959 if (vml[c] == NULL) {
2e528b49 4960 error = SET_ERROR(ENODEV);
428870ff
BB
4961 break;
4962 }
4963
4964 /* make sure there's nothing stopping the split */
4965 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4966 vml[c]->vdev_islog ||
4967 vml[c]->vdev_ishole ||
4968 vml[c]->vdev_isspare ||
4969 vml[c]->vdev_isl2cache ||
4970 !vdev_writeable(vml[c]) ||
4971 vml[c]->vdev_children != 0 ||
4972 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4973 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
2e528b49 4974 error = SET_ERROR(EINVAL);
428870ff
BB
4975 break;
4976 }
4977
4978 if (vdev_dtl_required(vml[c])) {
2e528b49 4979 error = SET_ERROR(EBUSY);
428870ff
BB
4980 break;
4981 }
4982
4983 /* we need certain info from the top level */
4984 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4985 vml[c]->vdev_top->vdev_ms_array) == 0);
4986 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4987 vml[c]->vdev_top->vdev_ms_shift) == 0);
4988 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4989 vml[c]->vdev_top->vdev_asize) == 0);
4990 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4991 vml[c]->vdev_top->vdev_ashift) == 0);
4992 }
4993
4994 if (error != 0) {
4995 kmem_free(vml, children * sizeof (vdev_t *));
4996 kmem_free(glist, children * sizeof (uint64_t));
4997 return (spa_vdev_exit(spa, NULL, txg, error));
4998 }
4999
5000 /* stop writers from using the disks */
5001 for (c = 0; c < children; c++) {
5002 if (vml[c] != NULL)
5003 vml[c]->vdev_offline = B_TRUE;
5004 }
5005 vdev_reopen(spa->spa_root_vdev);
34dc7c2f
BB
5006
5007 /*
428870ff
BB
5008 * Temporarily record the splitting vdevs in the spa config. This
5009 * will disappear once the config is regenerated.
34dc7c2f 5010 */
b8d06fca 5011 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
428870ff
BB
5012 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5013 glist, children) == 0);
5014 kmem_free(glist, children * sizeof (uint64_t));
34dc7c2f 5015
428870ff
BB
5016 mutex_enter(&spa->spa_props_lock);
5017 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5018 nvl) == 0);
5019 mutex_exit(&spa->spa_props_lock);
5020 spa->spa_config_splitting = nvl;
5021 vdev_config_dirty(spa->spa_root_vdev);
5022
5023 /* configure and create the new pool */
5024 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5025 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5026 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5027 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5028 spa_version(spa)) == 0);
5029 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5030 spa->spa_config_txg) == 0);
5031 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5032 spa_generate_guid(NULL)) == 0);
5033 (void) nvlist_lookup_string(props,
5034 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
34dc7c2f 5035
428870ff
BB
5036 /* add the new pool to the namespace */
5037 newspa = spa_add(newname, config, altroot);
5038 newspa->spa_config_txg = spa->spa_config_txg;
5039 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5040
5041 /* release the spa config lock, retaining the namespace lock */
5042 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5043
5044 if (zio_injection_enabled)
5045 zio_handle_panic_injection(spa, FTAG, 1);
5046
5047 spa_activate(newspa, spa_mode_global);
5048 spa_async_suspend(newspa);
5049
5050 /* create the new pool from the disks of the original pool */
5051 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5052 if (error)
5053 goto out;
5054
5055 /* if that worked, generate a real config for the new pool */
5056 if (newspa->spa_root_vdev != NULL) {
5057 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
b8d06fca 5058 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
428870ff
BB
5059 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5060 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5061 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5062 B_TRUE));
9babb374 5063 }
34dc7c2f 5064
428870ff
BB
5065 /* set the props */
5066 if (props != NULL) {
5067 spa_configfile_set(newspa, props, B_FALSE);
5068 error = spa_prop_set(newspa, props);
5069 if (error)
5070 goto out;
5071 }
34dc7c2f 5072
428870ff
BB
5073 /* flush everything */
5074 txg = spa_vdev_config_enter(newspa);
5075 vdev_config_dirty(newspa->spa_root_vdev);
5076 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
34dc7c2f 5077
428870ff
BB
5078 if (zio_injection_enabled)
5079 zio_handle_panic_injection(spa, FTAG, 2);
34dc7c2f 5080
428870ff 5081 spa_async_resume(newspa);
34dc7c2f 5082
428870ff
BB
5083 /* finally, update the original pool's config */
5084 txg = spa_vdev_config_enter(spa);
5085 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5086 error = dmu_tx_assign(tx, TXG_WAIT);
5087 if (error != 0)
5088 dmu_tx_abort(tx);
5089 for (c = 0; c < children; c++) {
5090 if (vml[c] != NULL) {
5091 vdev_split(vml[c]);
5092 if (error == 0)
6f1ffb06
MA
5093 spa_history_log_internal(spa, "detach", tx,
5094 "vdev=%s", vml[c]->vdev_path);
428870ff 5095 vdev_free(vml[c]);
34dc7c2f 5096 }
34dc7c2f 5097 }
428870ff
BB
5098 vdev_config_dirty(spa->spa_root_vdev);
5099 spa->spa_config_splitting = NULL;
5100 nvlist_free(nvl);
5101 if (error == 0)
5102 dmu_tx_commit(tx);
5103 (void) spa_vdev_exit(spa, NULL, txg, 0);
5104
5105 if (zio_injection_enabled)
5106 zio_handle_panic_injection(spa, FTAG, 3);
5107
5108 /* split is complete; log a history record */
6f1ffb06
MA
5109 spa_history_log_internal(newspa, "split", NULL,
5110 "from pool %s", spa_name(spa));
428870ff
BB
5111
5112 kmem_free(vml, children * sizeof (vdev_t *));
5113
5114 /* if we're not going to mount the filesystems in userland, export */
5115 if (exp)
5116 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5117 B_FALSE, B_FALSE);
5118
5119 return (error);
5120
5121out:
5122 spa_unload(newspa);
5123 spa_deactivate(newspa);
5124 spa_remove(newspa);
5125
5126 txg = spa_vdev_config_enter(spa);
5127
5128 /* re-online all offlined disks */
5129 for (c = 0; c < children; c++) {
5130 if (vml[c] != NULL)
5131 vml[c]->vdev_offline = B_FALSE;
5132 }
5133 vdev_reopen(spa->spa_root_vdev);
5134
5135 nvlist_free(spa->spa_config_splitting);
5136 spa->spa_config_splitting = NULL;
5137 (void) spa_vdev_exit(spa, NULL, txg, error);
34dc7c2f 5138
428870ff 5139 kmem_free(vml, children * sizeof (vdev_t *));
34dc7c2f
BB
5140 return (error);
5141}
5142
b128c09f
BB
5143static nvlist_t *
5144spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
34dc7c2f 5145{
d6320ddb
BB
5146 int i;
5147
5148 for (i = 0; i < count; i++) {
b128c09f 5149 uint64_t guid;
34dc7c2f 5150
b128c09f
BB
5151 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5152 &guid) == 0);
34dc7c2f 5153
b128c09f
BB
5154 if (guid == target_guid)
5155 return (nvpp[i]);
34dc7c2f
BB
5156 }
5157
b128c09f 5158 return (NULL);
34dc7c2f
BB
5159}
5160
b128c09f
BB
5161static void
5162spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5163 nvlist_t *dev_to_remove)
34dc7c2f 5164{
b128c09f 5165 nvlist_t **newdev = NULL;
d6320ddb 5166 int i, j;
34dc7c2f 5167
b128c09f 5168 if (count > 1)
b8d06fca 5169 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE);
34dc7c2f 5170
d6320ddb 5171 for (i = 0, j = 0; i < count; i++) {
b128c09f
BB
5172 if (dev[i] == dev_to_remove)
5173 continue;
b8d06fca 5174 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0);
34dc7c2f
BB
5175 }
5176
b128c09f
BB
5177 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5178 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
34dc7c2f 5179
d6320ddb 5180 for (i = 0; i < count - 1; i++)
b128c09f 5181 nvlist_free(newdev[i]);
34dc7c2f 5182
b128c09f
BB
5183 if (count > 1)
5184 kmem_free(newdev, (count - 1) * sizeof (void *));
34dc7c2f
BB
5185}
5186
428870ff
BB
5187/*
5188 * Evacuate the device.
5189 */
5190static int
5191spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5192{
5193 uint64_t txg;
5194 int error = 0;
5195
5196 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5197 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5198 ASSERT(vd == vd->vdev_top);
5199
5200 /*
5201 * Evacuate the device. We don't hold the config lock as writer
5202 * since we need to do I/O but we do keep the
5203 * spa_namespace_lock held. Once this completes the device
5204 * should no longer have any blocks allocated on it.
5205 */
5206 if (vd->vdev_islog) {
5207 if (vd->vdev_stat.vs_alloc != 0)
5208 error = spa_offline_log(spa);
5209 } else {
2e528b49 5210 error = SET_ERROR(ENOTSUP);
428870ff
BB
5211 }
5212
5213 if (error)
5214 return (error);
5215
5216 /*
5217 * The evacuation succeeded. Remove any remaining MOS metadata
5218 * associated with this vdev, and wait for these changes to sync.
5219 */
c99c9001 5220 ASSERT0(vd->vdev_stat.vs_alloc);
428870ff
BB
5221 txg = spa_vdev_config_enter(spa);
5222 vd->vdev_removing = B_TRUE;
93cf2076 5223 vdev_dirty_leaves(vd, VDD_DTL, txg);
428870ff
BB
5224 vdev_config_dirty(vd);
5225 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5226
5227 return (0);
5228}
5229
5230/*
5231 * Complete the removal by cleaning up the namespace.
5232 */
5233static void
5234spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5235{
5236 vdev_t *rvd = spa->spa_root_vdev;
5237 uint64_t id = vd->vdev_id;
5238 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5239
5240 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5241 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5242 ASSERT(vd == vd->vdev_top);
5243
5244 /*
5245 * Only remove any devices which are empty.
5246 */
5247 if (vd->vdev_stat.vs_alloc != 0)
5248 return;
5249
5250 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5251
5252 if (list_link_active(&vd->vdev_state_dirty_node))
5253 vdev_state_clean(vd);
5254 if (list_link_active(&vd->vdev_config_dirty_node))
5255 vdev_config_clean(vd);
5256
5257 vdev_free(vd);
5258
5259 if (last_vdev) {
5260 vdev_compact_children(rvd);
5261 } else {
5262 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5263 vdev_add_child(rvd, vd);
5264 }
5265 vdev_config_dirty(rvd);
5266
5267 /*
5268 * Reassess the health of our root vdev.
5269 */
5270 vdev_reopen(rvd);
5271}
5272
5273/*
5274 * Remove a device from the pool -
5275 *
5276 * Removing a device from the vdev namespace requires several steps
5277 * and can take a significant amount of time. As a result we use
5278 * the spa_vdev_config_[enter/exit] functions which allow us to
5279 * grab and release the spa_config_lock while still holding the namespace
5280 * lock. During each step the configuration is synced out.
d3cc8b15
WA
5281 *
5282 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5283 * devices.
34dc7c2f
BB
5284 */
5285int
5286spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5287{
5288 vdev_t *vd;
428870ff 5289 metaslab_group_t *mg;
b128c09f 5290 nvlist_t **spares, **l2cache, *nv;
fb5f0bc8 5291 uint64_t txg = 0;
428870ff 5292 uint_t nspares, nl2cache;
34dc7c2f 5293 int error = 0;
fb5f0bc8 5294 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
34dc7c2f 5295
572e2857
BB
5296 ASSERT(spa_writeable(spa));
5297
fb5f0bc8
BB
5298 if (!locked)
5299 txg = spa_vdev_enter(spa);
34dc7c2f 5300
b128c09f 5301 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
34dc7c2f
BB
5302
5303 if (spa->spa_spares.sav_vdevs != NULL &&
34dc7c2f 5304 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
b128c09f
BB
5305 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5306 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5307 /*
5308 * Only remove the hot spare if it's not currently in use
5309 * in this pool.
5310 */
5311 if (vd == NULL || unspare) {
5312 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5313 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5314 spa_load_spares(spa);
5315 spa->spa_spares.sav_sync = B_TRUE;
5316 } else {
2e528b49 5317 error = SET_ERROR(EBUSY);
b128c09f
BB
5318 }
5319 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
34dc7c2f 5320 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
b128c09f
BB
5321 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5322 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5323 /*
5324 * Cache devices can always be removed.
5325 */
5326 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5327 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
34dc7c2f
BB
5328 spa_load_l2cache(spa);
5329 spa->spa_l2cache.sav_sync = B_TRUE;
428870ff
BB
5330 } else if (vd != NULL && vd->vdev_islog) {
5331 ASSERT(!locked);
5332 ASSERT(vd == vd->vdev_top);
5333
428870ff
BB
5334 mg = vd->vdev_mg;
5335
5336 /*
5337 * Stop allocating from this vdev.
5338 */
5339 metaslab_group_passivate(mg);
5340
5341 /*
5342 * Wait for the youngest allocations and frees to sync,
5343 * and then wait for the deferral of those frees to finish.
5344 */
5345 spa_vdev_config_exit(spa, NULL,
5346 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5347
5348 /*
5349 * Attempt to evacuate the vdev.
5350 */
5351 error = spa_vdev_remove_evacuate(spa, vd);
5352
5353 txg = spa_vdev_config_enter(spa);
5354
5355 /*
5356 * If we couldn't evacuate the vdev, unwind.
5357 */
5358 if (error) {
5359 metaslab_group_activate(mg);
5360 return (spa_vdev_exit(spa, NULL, txg, error));
5361 }
5362
5363 /*
5364 * Clean up the vdev namespace.
5365 */
5366 spa_vdev_remove_from_namespace(spa, vd);
5367
b128c09f
BB
5368 } else if (vd != NULL) {
5369 /*
5370 * Normal vdevs cannot be removed (yet).
5371 */
2e528b49 5372 error = SET_ERROR(ENOTSUP);
b128c09f
BB
5373 } else {
5374 /*
5375 * There is no vdev of any kind with the specified guid.
5376 */
2e528b49 5377 error = SET_ERROR(ENOENT);
34dc7c2f
BB
5378 }
5379
fb5f0bc8
BB
5380 if (!locked)
5381 return (spa_vdev_exit(spa, NULL, txg, error));
5382
5383 return (error);
34dc7c2f
BB
5384}
5385
5386/*
5387 * Find any device that's done replacing, or a vdev marked 'unspare' that's
d3cc8b15 5388 * currently spared, so we can detach it.
34dc7c2f
BB
5389 */
5390static vdev_t *
5391spa_vdev_resilver_done_hunt(vdev_t *vd)
5392{
5393 vdev_t *newvd, *oldvd;
d6320ddb 5394 int c;
34dc7c2f 5395
d6320ddb 5396 for (c = 0; c < vd->vdev_children; c++) {
34dc7c2f
BB
5397 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5398 if (oldvd != NULL)
5399 return (oldvd);
5400 }
5401
5402 /*
572e2857
BB
5403 * Check for a completed replacement. We always consider the first
5404 * vdev in the list to be the oldest vdev, and the last one to be
5405 * the newest (see spa_vdev_attach() for how that works). In
5406 * the case where the newest vdev is faulted, we will not automatically
5407 * remove it after a resilver completes. This is OK as it will require
5408 * user intervention to determine which disk the admin wishes to keep.
34dc7c2f 5409 */
572e2857
BB
5410 if (vd->vdev_ops == &vdev_replacing_ops) {
5411 ASSERT(vd->vdev_children > 1);
5412
5413 newvd = vd->vdev_child[vd->vdev_children - 1];
34dc7c2f 5414 oldvd = vd->vdev_child[0];
34dc7c2f 5415
fb5f0bc8 5416 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 5417 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
fb5f0bc8 5418 !vdev_dtl_required(oldvd))
34dc7c2f 5419 return (oldvd);
34dc7c2f
BB
5420 }
5421
5422 /*
5423 * Check for a completed resilver with the 'unspare' flag set.
5424 */
572e2857
BB
5425 if (vd->vdev_ops == &vdev_spare_ops) {
5426 vdev_t *first = vd->vdev_child[0];
5427 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5428
5429 if (last->vdev_unspare) {
5430 oldvd = first;
5431 newvd = last;
5432 } else if (first->vdev_unspare) {
5433 oldvd = last;
5434 newvd = first;
5435 } else {
5436 oldvd = NULL;
5437 }
34dc7c2f 5438
572e2857 5439 if (oldvd != NULL &&
fb5f0bc8 5440 vdev_dtl_empty(newvd, DTL_MISSING) &&
428870ff 5441 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
572e2857 5442 !vdev_dtl_required(oldvd))
34dc7c2f 5443 return (oldvd);
572e2857
BB
5444
5445 /*
5446 * If there are more than two spares attached to a disk,
5447 * and those spares are not required, then we want to
5448 * attempt to free them up now so that they can be used
5449 * by other pools. Once we're back down to a single
5450 * disk+spare, we stop removing them.
5451 */
5452 if (vd->vdev_children > 2) {
5453 newvd = vd->vdev_child[1];
5454
5455 if (newvd->vdev_isspare && last->vdev_isspare &&
5456 vdev_dtl_empty(last, DTL_MISSING) &&
5457 vdev_dtl_empty(last, DTL_OUTAGE) &&
5458 !vdev_dtl_required(newvd))
5459 return (newvd);
34dc7c2f 5460 }
34dc7c2f
BB
5461 }
5462
5463 return (NULL);
5464}
5465
5466static void
5467spa_vdev_resilver_done(spa_t *spa)
5468{
fb5f0bc8
BB
5469 vdev_t *vd, *pvd, *ppvd;
5470 uint64_t guid, sguid, pguid, ppguid;
34dc7c2f 5471
fb5f0bc8 5472 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
5473
5474 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
fb5f0bc8
BB
5475 pvd = vd->vdev_parent;
5476 ppvd = pvd->vdev_parent;
34dc7c2f 5477 guid = vd->vdev_guid;
fb5f0bc8
BB
5478 pguid = pvd->vdev_guid;
5479 ppguid = ppvd->vdev_guid;
5480 sguid = 0;
34dc7c2f
BB
5481 /*
5482 * If we have just finished replacing a hot spared device, then
5483 * we need to detach the parent's first child (the original hot
5484 * spare) as well.
5485 */
572e2857
BB
5486 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5487 ppvd->vdev_children == 2) {
34dc7c2f 5488 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
fb5f0bc8 5489 sguid = ppvd->vdev_child[1]->vdev_guid;
34dc7c2f 5490 }
5d1f7fb6
GW
5491 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5492
fb5f0bc8
BB
5493 spa_config_exit(spa, SCL_ALL, FTAG);
5494 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
34dc7c2f 5495 return;
fb5f0bc8 5496 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
34dc7c2f 5497 return;
fb5f0bc8 5498 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
5499 }
5500
fb5f0bc8 5501 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
5502}
5503
5504/*
428870ff 5505 * Update the stored path or FRU for this vdev.
34dc7c2f
BB
5506 */
5507int
9babb374
BB
5508spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5509 boolean_t ispath)
34dc7c2f 5510{
b128c09f 5511 vdev_t *vd;
428870ff 5512 boolean_t sync = B_FALSE;
34dc7c2f 5513
572e2857
BB
5514 ASSERT(spa_writeable(spa));
5515
428870ff 5516 spa_vdev_state_enter(spa, SCL_ALL);
34dc7c2f 5517
9babb374 5518 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
428870ff 5519 return (spa_vdev_state_exit(spa, NULL, ENOENT));
34dc7c2f
BB
5520
5521 if (!vd->vdev_ops->vdev_op_leaf)
428870ff 5522 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
34dc7c2f 5523
9babb374 5524 if (ispath) {
428870ff
BB
5525 if (strcmp(value, vd->vdev_path) != 0) {
5526 spa_strfree(vd->vdev_path);
5527 vd->vdev_path = spa_strdup(value);
5528 sync = B_TRUE;
5529 }
9babb374 5530 } else {
428870ff
BB
5531 if (vd->vdev_fru == NULL) {
5532 vd->vdev_fru = spa_strdup(value);
5533 sync = B_TRUE;
5534 } else if (strcmp(value, vd->vdev_fru) != 0) {
9babb374 5535 spa_strfree(vd->vdev_fru);
428870ff
BB
5536 vd->vdev_fru = spa_strdup(value);
5537 sync = B_TRUE;
5538 }
9babb374 5539 }
34dc7c2f 5540
428870ff 5541 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
34dc7c2f
BB
5542}
5543
9babb374
BB
5544int
5545spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5546{
5547 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5548}
5549
5550int
5551spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5552{
5553 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5554}
5555
34dc7c2f
BB
5556/*
5557 * ==========================================================================
428870ff 5558 * SPA Scanning
34dc7c2f
BB
5559 * ==========================================================================
5560 */
5561
34dc7c2f 5562int
428870ff
BB
5563spa_scan_stop(spa_t *spa)
5564{
5565 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5566 if (dsl_scan_resilvering(spa->spa_dsl_pool))
2e528b49 5567 return (SET_ERROR(EBUSY));
428870ff
BB
5568 return (dsl_scan_cancel(spa->spa_dsl_pool));
5569}
5570
5571int
5572spa_scan(spa_t *spa, pool_scan_func_t func)
34dc7c2f 5573{
b128c09f 5574 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
34dc7c2f 5575
428870ff 5576 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
2e528b49 5577 return (SET_ERROR(ENOTSUP));
34dc7c2f 5578
34dc7c2f 5579 /*
b128c09f
BB
5580 * If a resilver was requested, but there is no DTL on a
5581 * writeable leaf device, we have nothing to do.
34dc7c2f 5582 */
428870ff 5583 if (func == POOL_SCAN_RESILVER &&
b128c09f
BB
5584 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5585 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
34dc7c2f
BB
5586 return (0);
5587 }
5588
428870ff 5589 return (dsl_scan(spa->spa_dsl_pool, func));
34dc7c2f
BB
5590}
5591
5592/*
5593 * ==========================================================================
5594 * SPA async task processing
5595 * ==========================================================================
5596 */
5597
5598static void
5599spa_async_remove(spa_t *spa, vdev_t *vd)
5600{
d6320ddb
BB
5601 int c;
5602
b128c09f 5603 if (vd->vdev_remove_wanted) {
428870ff
BB
5604 vd->vdev_remove_wanted = B_FALSE;
5605 vd->vdev_delayed_close = B_FALSE;
b128c09f 5606 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
428870ff
BB
5607
5608 /*
5609 * We want to clear the stats, but we don't want to do a full
5610 * vdev_clear() as that will cause us to throw away
5611 * degraded/faulted state as well as attempt to reopen the
5612 * device, all of which is a waste.
5613 */
5614 vd->vdev_stat.vs_read_errors = 0;
5615 vd->vdev_stat.vs_write_errors = 0;
5616 vd->vdev_stat.vs_checksum_errors = 0;
5617
b128c09f
BB
5618 vdev_state_dirty(vd->vdev_top);
5619 }
34dc7c2f 5620
d6320ddb 5621 for (c = 0; c < vd->vdev_children; c++)
b128c09f
BB
5622 spa_async_remove(spa, vd->vdev_child[c]);
5623}
5624
5625static void
5626spa_async_probe(spa_t *spa, vdev_t *vd)
5627{
d6320ddb
BB
5628 int c;
5629
b128c09f 5630 if (vd->vdev_probe_wanted) {
428870ff 5631 vd->vdev_probe_wanted = B_FALSE;
b128c09f 5632 vdev_reopen(vd); /* vdev_open() does the actual probe */
34dc7c2f 5633 }
b128c09f 5634
d6320ddb 5635 for (c = 0; c < vd->vdev_children; c++)
b128c09f 5636 spa_async_probe(spa, vd->vdev_child[c]);
34dc7c2f
BB
5637}
5638
9babb374
BB
5639static void
5640spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5641{
d6320ddb 5642 int c;
9babb374
BB
5643
5644 if (!spa->spa_autoexpand)
5645 return;
5646
d6320ddb 5647 for (c = 0; c < vd->vdev_children; c++) {
9babb374
BB
5648 vdev_t *cvd = vd->vdev_child[c];
5649 spa_async_autoexpand(spa, cvd);
5650 }
5651
5652 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5653 return;
5654
26685276 5655 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
9babb374
BB
5656}
5657
34dc7c2f
BB
5658static void
5659spa_async_thread(spa_t *spa)
5660{
d6320ddb 5661 int tasks, i;
34dc7c2f
BB
5662
5663 ASSERT(spa->spa_sync_on);
5664
5665 mutex_enter(&spa->spa_async_lock);
5666 tasks = spa->spa_async_tasks;
5667 spa->spa_async_tasks = 0;
5668 mutex_exit(&spa->spa_async_lock);
5669
5670 /*
5671 * See if the config needs to be updated.
5672 */
5673 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
428870ff 5674 uint64_t old_space, new_space;
9babb374 5675
34dc7c2f 5676 mutex_enter(&spa_namespace_lock);
428870ff 5677 old_space = metaslab_class_get_space(spa_normal_class(spa));
34dc7c2f 5678 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
428870ff 5679 new_space = metaslab_class_get_space(spa_normal_class(spa));
34dc7c2f 5680 mutex_exit(&spa_namespace_lock);
9babb374
BB
5681
5682 /*
5683 * If the pool grew as a result of the config update,
5684 * then log an internal history event.
5685 */
428870ff 5686 if (new_space != old_space) {
6f1ffb06 5687 spa_history_log_internal(spa, "vdev online", NULL,
45d1cae3 5688 "pool '%s' size: %llu(+%llu)",
428870ff 5689 spa_name(spa), new_space, new_space - old_space);
9babb374 5690 }
34dc7c2f
BB
5691 }
5692
5693 /*
5694 * See if any devices need to be marked REMOVED.
34dc7c2f 5695 */
b128c09f 5696 if (tasks & SPA_ASYNC_REMOVE) {
428870ff 5697 spa_vdev_state_enter(spa, SCL_NONE);
34dc7c2f 5698 spa_async_remove(spa, spa->spa_root_vdev);
d6320ddb 5699 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
b128c09f 5700 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
d6320ddb 5701 for (i = 0; i < spa->spa_spares.sav_count; i++)
b128c09f
BB
5702 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5703 (void) spa_vdev_state_exit(spa, NULL, 0);
34dc7c2f
BB
5704 }
5705
9babb374
BB
5706 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5707 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5708 spa_async_autoexpand(spa, spa->spa_root_vdev);
5709 spa_config_exit(spa, SCL_CONFIG, FTAG);
5710 }
5711
34dc7c2f 5712 /*
b128c09f 5713 * See if any devices need to be probed.
34dc7c2f 5714 */
b128c09f 5715 if (tasks & SPA_ASYNC_PROBE) {
428870ff 5716 spa_vdev_state_enter(spa, SCL_NONE);
b128c09f
BB
5717 spa_async_probe(spa, spa->spa_root_vdev);
5718 (void) spa_vdev_state_exit(spa, NULL, 0);
5719 }
34dc7c2f
BB
5720
5721 /*
b128c09f 5722 * If any devices are done replacing, detach them.
34dc7c2f 5723 */
b128c09f
BB
5724 if (tasks & SPA_ASYNC_RESILVER_DONE)
5725 spa_vdev_resilver_done(spa);
34dc7c2f
BB
5726
5727 /*
5728 * Kick off a resilver.
5729 */
b128c09f 5730 if (tasks & SPA_ASYNC_RESILVER)
428870ff 5731 dsl_resilver_restart(spa->spa_dsl_pool, 0);
34dc7c2f
BB
5732
5733 /*
5734 * Let the world know that we're done.
5735 */
5736 mutex_enter(&spa->spa_async_lock);
5737 spa->spa_async_thread = NULL;
5738 cv_broadcast(&spa->spa_async_cv);
5739 mutex_exit(&spa->spa_async_lock);
5740 thread_exit();
5741}
5742
5743void
5744spa_async_suspend(spa_t *spa)
5745{
5746 mutex_enter(&spa->spa_async_lock);
5747 spa->spa_async_suspended++;
5748 while (spa->spa_async_thread != NULL)
5749 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5750 mutex_exit(&spa->spa_async_lock);
5751}
5752
5753void
5754spa_async_resume(spa_t *spa)
5755{
5756 mutex_enter(&spa->spa_async_lock);
5757 ASSERT(spa->spa_async_suspended != 0);
5758 spa->spa_async_suspended--;
5759 mutex_exit(&spa->spa_async_lock);
5760}
5761
5762static void
5763spa_async_dispatch(spa_t *spa)
5764{
5765 mutex_enter(&spa->spa_async_lock);
5766 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5767 spa->spa_async_thread == NULL &&
5768 rootdir != NULL && !vn_is_readonly(rootdir))
5769 spa->spa_async_thread = thread_create(NULL, 0,
5770 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5771 mutex_exit(&spa->spa_async_lock);
5772}
5773
5774void
5775spa_async_request(spa_t *spa, int task)
5776{
428870ff 5777 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
34dc7c2f
BB
5778 mutex_enter(&spa->spa_async_lock);
5779 spa->spa_async_tasks |= task;
5780 mutex_exit(&spa->spa_async_lock);
5781}
5782
5783/*
5784 * ==========================================================================
5785 * SPA syncing routines
5786 * ==========================================================================
5787 */
5788
428870ff
BB
5789static int
5790bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
34dc7c2f 5791{
428870ff
BB
5792 bpobj_t *bpo = arg;
5793 bpobj_enqueue(bpo, bp, tx);
5794 return (0);
5795}
34dc7c2f 5796
428870ff
BB
5797static int
5798spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5799{
5800 zio_t *zio = arg;
34dc7c2f 5801
428870ff
BB
5802 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5803 zio->io_flags));
5804 return (0);
34dc7c2f
BB
5805}
5806
e8b96c60
MA
5807/*
5808 * Note: this simple function is not inlined to make it easier to dtrace the
5809 * amount of time spent syncing frees.
5810 */
5811static void
5812spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
5813{
5814 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5815 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
5816 VERIFY(zio_wait(zio) == 0);
5817}
5818
5819/*
5820 * Note: this simple function is not inlined to make it easier to dtrace the
5821 * amount of time spent syncing deferred frees.
5822 */
5823static void
5824spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
5825{
5826 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5827 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
5828 spa_free_sync_cb, zio, tx), ==, 0);
5829 VERIFY0(zio_wait(zio));
5830}
5831
34dc7c2f
BB
5832static void
5833spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5834{
5835 char *packed = NULL;
b128c09f 5836 size_t bufsize;
34dc7c2f
BB
5837 size_t nvsize = 0;
5838 dmu_buf_t *db;
5839
5840 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5841
b128c09f
BB
5842 /*
5843 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
b0bc7a84 5844 * information. This avoids the dmu_buf_will_dirty() path and
b128c09f
BB
5845 * saves us a pre-read to get data we don't actually care about.
5846 */
9ae529ec 5847 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
b8d06fca 5848 packed = vmem_alloc(bufsize, KM_PUSHPAGE);
34dc7c2f
BB
5849
5850 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
b8d06fca 5851 KM_PUSHPAGE) == 0);
b128c09f 5852 bzero(packed + nvsize, bufsize - nvsize);
34dc7c2f 5853
b128c09f 5854 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
34dc7c2f 5855
00b46022 5856 vmem_free(packed, bufsize);
34dc7c2f
BB
5857
5858 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5859 dmu_buf_will_dirty(db, tx);
5860 *(uint64_t *)db->db_data = nvsize;
5861 dmu_buf_rele(db, FTAG);
5862}
5863
5864static void
5865spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5866 const char *config, const char *entry)
5867{
5868 nvlist_t *nvroot;
5869 nvlist_t **list;
5870 int i;
5871
5872 if (!sav->sav_sync)
5873 return;
5874
5875 /*
5876 * Update the MOS nvlist describing the list of available devices.
5877 * spa_validate_aux() will have already made sure this nvlist is
5878 * valid and the vdevs are labeled appropriately.
5879 */
5880 if (sav->sav_object == 0) {
5881 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5882 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5883 sizeof (uint64_t), tx);
5884 VERIFY(zap_update(spa->spa_meta_objset,
5885 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5886 &sav->sav_object, tx) == 0);
5887 }
5888
b8d06fca 5889 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
34dc7c2f
BB
5890 if (sav->sav_count == 0) {
5891 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5892 } else {
d1d7e268 5893 list = kmem_alloc(sav->sav_count*sizeof (void *), KM_PUSHPAGE);
34dc7c2f
BB
5894 for (i = 0; i < sav->sav_count; i++)
5895 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
428870ff 5896 B_FALSE, VDEV_CONFIG_L2CACHE);
34dc7c2f
BB
5897 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5898 sav->sav_count) == 0);
5899 for (i = 0; i < sav->sav_count; i++)
5900 nvlist_free(list[i]);
5901 kmem_free(list, sav->sav_count * sizeof (void *));
5902 }
5903
5904 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5905 nvlist_free(nvroot);
5906
5907 sav->sav_sync = B_FALSE;
5908}
5909
5910static void
5911spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5912{
5913 nvlist_t *config;
5914
b128c09f 5915 if (list_is_empty(&spa->spa_config_dirty_list))
34dc7c2f
BB
5916 return;
5917
b128c09f
BB
5918 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5919
5920 config = spa_config_generate(spa, spa->spa_root_vdev,
5921 dmu_tx_get_txg(tx), B_FALSE);
5922
ea0b2538
GW
5923 /*
5924 * If we're upgrading the spa version then make sure that
5925 * the config object gets updated with the correct version.
5926 */
5927 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
5928 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5929 spa->spa_uberblock.ub_version);
5930
b128c09f 5931 spa_config_exit(spa, SCL_STATE, FTAG);
34dc7c2f
BB
5932
5933 if (spa->spa_config_syncing)
5934 nvlist_free(spa->spa_config_syncing);
5935 spa->spa_config_syncing = config;
5936
5937 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5938}
5939
9ae529ec 5940static void
13fe0198 5941spa_sync_version(void *arg, dmu_tx_t *tx)
9ae529ec 5942{
13fe0198
MA
5943 uint64_t *versionp = arg;
5944 uint64_t version = *versionp;
5945 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
9ae529ec
CS
5946
5947 /*
5948 * Setting the version is special cased when first creating the pool.
5949 */
5950 ASSERT(tx->tx_txg != TXG_INITIAL);
5951
8dca0a9a 5952 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
9ae529ec
CS
5953 ASSERT(version >= spa_version(spa));
5954
5955 spa->spa_uberblock.ub_version = version;
5956 vdev_config_dirty(spa->spa_root_vdev);
6f1ffb06 5957 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
9ae529ec
CS
5958}
5959
34dc7c2f
BB
5960/*
5961 * Set zpool properties.
5962 */
5963static void
13fe0198 5964spa_sync_props(void *arg, dmu_tx_t *tx)
34dc7c2f 5965{
13fe0198
MA
5966 nvlist_t *nvp = arg;
5967 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
34dc7c2f 5968 objset_t *mos = spa->spa_meta_objset;
9ae529ec 5969 nvpair_t *elem = NULL;
b128c09f
BB
5970
5971 mutex_enter(&spa->spa_props_lock);
34dc7c2f 5972
34dc7c2f 5973 while ((elem = nvlist_next_nvpair(nvp, elem))) {
9ae529ec
CS
5974 uint64_t intval;
5975 char *strval, *fname;
5976 zpool_prop_t prop;
5977 const char *propname;
5978 zprop_type_t proptype;
fa86b5db 5979 spa_feature_t fid;
9ae529ec
CS
5980
5981 prop = zpool_name_to_prop(nvpair_name(elem));
5982 switch ((int)prop) {
5983 case ZPROP_INVAL:
5984 /*
5985 * We checked this earlier in spa_prop_validate().
5986 */
5987 ASSERT(zpool_prop_feature(nvpair_name(elem)));
5988
5989 fname = strchr(nvpair_name(elem), '@') + 1;
fa86b5db 5990 VERIFY0(zfeature_lookup_name(fname, &fid));
9ae529ec 5991
fa86b5db 5992 spa_feature_enable(spa, fid, tx);
6f1ffb06
MA
5993 spa_history_log_internal(spa, "set", tx,
5994 "%s=enabled", nvpair_name(elem));
9ae529ec
CS
5995 break;
5996
34dc7c2f 5997 case ZPOOL_PROP_VERSION:
93cf2076 5998 intval = fnvpair_value_uint64(elem);
34dc7c2f 5999 /*
9ae529ec
CS
6000 * The version is synced seperatly before other
6001 * properties and should be correct by now.
34dc7c2f 6002 */
9ae529ec 6003 ASSERT3U(spa_version(spa), >=, intval);
34dc7c2f
BB
6004 break;
6005
6006 case ZPOOL_PROP_ALTROOT:
6007 /*
6008 * 'altroot' is a non-persistent property. It should
6009 * have been set temporarily at creation or import time.
6010 */
6011 ASSERT(spa->spa_root != NULL);
6012 break;
6013
572e2857 6014 case ZPOOL_PROP_READONLY:
34dc7c2f
BB
6015 case ZPOOL_PROP_CACHEFILE:
6016 /*
572e2857
BB
6017 * 'readonly' and 'cachefile' are also non-persisitent
6018 * properties.
34dc7c2f 6019 */
34dc7c2f 6020 break;
d96eb2b1 6021 case ZPOOL_PROP_COMMENT:
93cf2076 6022 strval = fnvpair_value_string(elem);
d96eb2b1
DM
6023 if (spa->spa_comment != NULL)
6024 spa_strfree(spa->spa_comment);
6025 spa->spa_comment = spa_strdup(strval);
6026 /*
6027 * We need to dirty the configuration on all the vdevs
6028 * so that their labels get updated. It's unnecessary
6029 * to do this for pool creation since the vdev's
6030 * configuratoin has already been dirtied.
6031 */
6032 if (tx->tx_txg != TXG_INITIAL)
6033 vdev_config_dirty(spa->spa_root_vdev);
6f1ffb06
MA
6034 spa_history_log_internal(spa, "set", tx,
6035 "%s=%s", nvpair_name(elem), strval);
d96eb2b1 6036 break;
34dc7c2f
BB
6037 default:
6038 /*
6039 * Set pool property values in the poolprops mos object.
6040 */
34dc7c2f 6041 if (spa->spa_pool_props_object == 0) {
9ae529ec
CS
6042 spa->spa_pool_props_object =
6043 zap_create_link(mos, DMU_OT_POOL_PROPS,
34dc7c2f 6044 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
9ae529ec 6045 tx);
34dc7c2f 6046 }
34dc7c2f
BB
6047
6048 /* normalize the property name */
6049 propname = zpool_prop_to_name(prop);
6050 proptype = zpool_prop_get_type(prop);
6051
6052 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6053 ASSERT(proptype == PROP_TYPE_STRING);
93cf2076
GW
6054 strval = fnvpair_value_string(elem);
6055 VERIFY0(zap_update(mos,
34dc7c2f 6056 spa->spa_pool_props_object, propname,
93cf2076 6057 1, strlen(strval) + 1, strval, tx));
6f1ffb06
MA
6058 spa_history_log_internal(spa, "set", tx,
6059 "%s=%s", nvpair_name(elem), strval);
34dc7c2f 6060 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
93cf2076 6061 intval = fnvpair_value_uint64(elem);
34dc7c2f
BB
6062
6063 if (proptype == PROP_TYPE_INDEX) {
6064 const char *unused;
93cf2076
GW
6065 VERIFY0(zpool_prop_index_to_string(
6066 prop, intval, &unused));
34dc7c2f 6067 }
93cf2076 6068 VERIFY0(zap_update(mos,
34dc7c2f 6069 spa->spa_pool_props_object, propname,
93cf2076 6070 8, 1, &intval, tx));
6f1ffb06
MA
6071 spa_history_log_internal(spa, "set", tx,
6072 "%s=%lld", nvpair_name(elem), intval);
34dc7c2f
BB
6073 } else {
6074 ASSERT(0); /* not allowed */
6075 }
6076
6077 switch (prop) {
6078 case ZPOOL_PROP_DELEGATION:
6079 spa->spa_delegation = intval;
6080 break;
6081 case ZPOOL_PROP_BOOTFS:
6082 spa->spa_bootfs = intval;
6083 break;
6084 case ZPOOL_PROP_FAILUREMODE:
6085 spa->spa_failmode = intval;
6086 break;
9babb374
BB
6087 case ZPOOL_PROP_AUTOEXPAND:
6088 spa->spa_autoexpand = intval;
428870ff
BB
6089 if (tx->tx_txg != TXG_INITIAL)
6090 spa_async_request(spa,
6091 SPA_ASYNC_AUTOEXPAND);
6092 break;
6093 case ZPOOL_PROP_DEDUPDITTO:
6094 spa->spa_dedup_ditto = intval;
9babb374 6095 break;
34dc7c2f
BB
6096 default:
6097 break;
6098 }
6099 }
6100
34dc7c2f 6101 }
b128c09f
BB
6102
6103 mutex_exit(&spa->spa_props_lock);
34dc7c2f
BB
6104}
6105
428870ff
BB
6106/*
6107 * Perform one-time upgrade on-disk changes. spa_version() does not
6108 * reflect the new version this txg, so there must be no changes this
6109 * txg to anything that the upgrade code depends on after it executes.
6110 * Therefore this must be called after dsl_pool_sync() does the sync
6111 * tasks.
6112 */
6113static void
6114spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6115{
6116 dsl_pool_t *dp = spa->spa_dsl_pool;
6117
6118 ASSERT(spa->spa_sync_pass == 1);
6119
13fe0198
MA
6120 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6121
428870ff
BB
6122 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6123 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6124 dsl_pool_create_origin(dp, tx);
6125
6126 /* Keeping the origin open increases spa_minref */
6127 spa->spa_minref += 3;
6128 }
6129
6130 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6131 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6132 dsl_pool_upgrade_clones(dp, tx);
6133 }
6134
6135 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6136 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6137 dsl_pool_upgrade_dir_clones(dp, tx);
6138
6139 /* Keeping the freedir open increases spa_minref */
6140 spa->spa_minref += 3;
6141 }
9ae529ec
CS
6142
6143 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6144 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6145 spa_feature_create_zap_objects(spa, tx);
6146 }
13fe0198 6147 rrw_exit(&dp->dp_config_rwlock, FTAG);
428870ff
BB
6148}
6149
34dc7c2f
BB
6150/*
6151 * Sync the specified transaction group. New blocks may be dirtied as
6152 * part of the process, so we iterate until it converges.
6153 */
6154void
6155spa_sync(spa_t *spa, uint64_t txg)
6156{
6157 dsl_pool_t *dp = spa->spa_dsl_pool;
6158 objset_t *mos = spa->spa_meta_objset;
428870ff 6159 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
34dc7c2f
BB
6160 vdev_t *rvd = spa->spa_root_vdev;
6161 vdev_t *vd;
34dc7c2f 6162 dmu_tx_t *tx;
b128c09f 6163 int error;
d6320ddb 6164 int c;
34dc7c2f 6165
572e2857
BB
6166 VERIFY(spa_writeable(spa));
6167
34dc7c2f
BB
6168 /*
6169 * Lock out configuration changes.
6170 */
b128c09f 6171 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
34dc7c2f
BB
6172
6173 spa->spa_syncing_txg = txg;
6174 spa->spa_sync_pass = 0;
6175
b128c09f
BB
6176 /*
6177 * If there are any pending vdev state changes, convert them
6178 * into config changes that go out with this transaction group.
6179 */
6180 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
fb5f0bc8
BB
6181 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6182 /*
6183 * We need the write lock here because, for aux vdevs,
6184 * calling vdev_config_dirty() modifies sav_config.
6185 * This is ugly and will become unnecessary when we
6186 * eliminate the aux vdev wart by integrating all vdevs
6187 * into the root vdev tree.
6188 */
6189 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6190 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6191 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6192 vdev_state_clean(vd);
6193 vdev_config_dirty(vd);
6194 }
6195 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6196 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
b128c09f
BB
6197 }
6198 spa_config_exit(spa, SCL_STATE, FTAG);
6199
34dc7c2f
BB
6200 tx = dmu_tx_create_assigned(dp, txg);
6201
cc92e9d0
GW
6202 spa->spa_sync_starttime = gethrtime();
6203 taskq_cancel_id(system_taskq, spa->spa_deadman_tqid);
6204 spa->spa_deadman_tqid = taskq_dispatch_delay(system_taskq,
cbfa294d 6205 spa_deadman, spa, TQ_PUSHPAGE, ddi_get_lbolt() +
cc92e9d0
GW
6206 NSEC_TO_TICK(spa->spa_deadman_synctime));
6207
34dc7c2f
BB
6208 /*
6209 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6210 * set spa_deflate if we have no raid-z vdevs.
6211 */
6212 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6213 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6214 int i;
6215
6216 for (i = 0; i < rvd->vdev_children; i++) {
6217 vd = rvd->vdev_child[i];
6218 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6219 break;
6220 }
6221 if (i == rvd->vdev_children) {
6222 spa->spa_deflate = TRUE;
6223 VERIFY(0 == zap_add(spa->spa_meta_objset,
6224 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6225 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6226 }
6227 }
6228
6229 /*
428870ff
BB
6230 * If anything has changed in this txg, or if someone is waiting
6231 * for this txg to sync (eg, spa_vdev_remove()), push the
6232 * deferred frees from the previous txg. If not, leave them
6233 * alone so that we don't generate work on an otherwise idle
6234 * system.
34dc7c2f
BB
6235 */
6236 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6237 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
428870ff
BB
6238 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6239 ((dsl_scan_active(dp->dp_scan) ||
6240 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
e8b96c60 6241 spa_sync_deferred_frees(spa, tx);
428870ff 6242 }
34dc7c2f
BB
6243
6244 /*
6245 * Iterate to convergence.
6246 */
6247 do {
428870ff 6248 int pass = ++spa->spa_sync_pass;
34dc7c2f
BB
6249
6250 spa_sync_config_object(spa, tx);
6251 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6252 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6253 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6254 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6255 spa_errlog_sync(spa, txg);
6256 dsl_pool_sync(dp, txg);
6257
55d85d5a 6258 if (pass < zfs_sync_pass_deferred_free) {
e8b96c60 6259 spa_sync_frees(spa, free_bpl, tx);
428870ff
BB
6260 } else {
6261 bplist_iterate(free_bpl, bpobj_enqueue_cb,
e8b96c60 6262 &spa->spa_deferred_bpobj, tx);
34dc7c2f
BB
6263 }
6264
428870ff
BB
6265 ddt_sync(spa, txg);
6266 dsl_scan_sync(dp, tx);
34dc7c2f 6267
c65aa5b2 6268 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
428870ff
BB
6269 vdev_sync(vd, txg);
6270
6271 if (pass == 1)
6272 spa_sync_upgrades(spa, tx);
34dc7c2f 6273
428870ff 6274 } while (dmu_objset_is_dirty(mos, txg));
34dc7c2f
BB
6275
6276 /*
6277 * Rewrite the vdev configuration (which includes the uberblock)
6278 * to commit the transaction group.
6279 *
6280 * If there are no dirty vdevs, we sync the uberblock to a few
6281 * random top-level vdevs that are known to be visible in the
b128c09f
BB
6282 * config cache (see spa_vdev_add() for a complete description).
6283 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
34dc7c2f 6284 */
b128c09f
BB
6285 for (;;) {
6286 /*
6287 * We hold SCL_STATE to prevent vdev open/close/etc.
6288 * while we're attempting to write the vdev labels.
6289 */
6290 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6291
6292 if (list_is_empty(&spa->spa_config_dirty_list)) {
6293 vdev_t *svd[SPA_DVAS_PER_BP];
6294 int svdcount = 0;
6295 int children = rvd->vdev_children;
6296 int c0 = spa_get_random(children);
b128c09f 6297
d6320ddb 6298 for (c = 0; c < children; c++) {
b128c09f
BB
6299 vd = rvd->vdev_child[(c0 + c) % children];
6300 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6301 continue;
6302 svd[svdcount++] = vd;
6303 if (svdcount == SPA_DVAS_PER_BP)
6304 break;
6305 }
9babb374
BB
6306 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6307 if (error != 0)
6308 error = vdev_config_sync(svd, svdcount, txg,
6309 B_TRUE);
b128c09f
BB
6310 } else {
6311 error = vdev_config_sync(rvd->vdev_child,
9babb374
BB
6312 rvd->vdev_children, txg, B_FALSE);
6313 if (error != 0)
6314 error = vdev_config_sync(rvd->vdev_child,
6315 rvd->vdev_children, txg, B_TRUE);
34dc7c2f 6316 }
34dc7c2f 6317
3bc7e0fb
GW
6318 if (error == 0)
6319 spa->spa_last_synced_guid = rvd->vdev_guid;
6320
b128c09f
BB
6321 spa_config_exit(spa, SCL_STATE, FTAG);
6322
6323 if (error == 0)
6324 break;
6325 zio_suspend(spa, NULL);
6326 zio_resume_wait(spa);
6327 }
34dc7c2f
BB
6328 dmu_tx_commit(tx);
6329
cc92e9d0
GW
6330 taskq_cancel_id(system_taskq, spa->spa_deadman_tqid);
6331 spa->spa_deadman_tqid = 0;
6332
34dc7c2f
BB
6333 /*
6334 * Clear the dirty config list.
6335 */
b128c09f 6336 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
34dc7c2f
BB
6337 vdev_config_clean(vd);
6338
6339 /*
6340 * Now that the new config has synced transactionally,
6341 * let it become visible to the config cache.
6342 */
6343 if (spa->spa_config_syncing != NULL) {
6344 spa_config_set(spa, spa->spa_config_syncing);
6345 spa->spa_config_txg = txg;
6346 spa->spa_config_syncing = NULL;
6347 }
6348
34dc7c2f 6349 spa->spa_ubsync = spa->spa_uberblock;
34dc7c2f 6350
428870ff 6351 dsl_pool_sync_done(dp, txg);
34dc7c2f
BB
6352
6353 /*
6354 * Update usable space statistics.
6355 */
c65aa5b2 6356 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
34dc7c2f
BB
6357 vdev_sync_done(vd, txg);
6358
428870ff
BB
6359 spa_update_dspace(spa);
6360
34dc7c2f
BB
6361 /*
6362 * It had better be the case that we didn't dirty anything
6363 * since vdev_config_sync().
6364 */
6365 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6366 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6367 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
428870ff
BB
6368
6369 spa->spa_sync_pass = 0;
34dc7c2f 6370
b128c09f 6371 spa_config_exit(spa, SCL_CONFIG, FTAG);
34dc7c2f 6372
428870ff
BB
6373 spa_handle_ignored_writes(spa);
6374
34dc7c2f
BB
6375 /*
6376 * If any async tasks have been requested, kick them off.
6377 */
6378 spa_async_dispatch(spa);
6379}
6380
6381/*
6382 * Sync all pools. We don't want to hold the namespace lock across these
6383 * operations, so we take a reference on the spa_t and drop the lock during the
6384 * sync.
6385 */
6386void
6387spa_sync_allpools(void)
6388{
6389 spa_t *spa = NULL;
6390 mutex_enter(&spa_namespace_lock);
6391 while ((spa = spa_next(spa)) != NULL) {
572e2857
BB
6392 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6393 !spa_writeable(spa) || spa_suspended(spa))
34dc7c2f
BB
6394 continue;
6395 spa_open_ref(spa, FTAG);
6396 mutex_exit(&spa_namespace_lock);
6397 txg_wait_synced(spa_get_dsl(spa), 0);
6398 mutex_enter(&spa_namespace_lock);
6399 spa_close(spa, FTAG);
6400 }
6401 mutex_exit(&spa_namespace_lock);
6402}
6403
6404/*
6405 * ==========================================================================
6406 * Miscellaneous routines
6407 * ==========================================================================
6408 */
6409
6410/*
6411 * Remove all pools in the system.
6412 */
6413void
6414spa_evict_all(void)
6415{
6416 spa_t *spa;
6417
6418 /*
6419 * Remove all cached state. All pools should be closed now,
6420 * so every spa in the AVL tree should be unreferenced.
6421 */
6422 mutex_enter(&spa_namespace_lock);
6423 while ((spa = spa_next(NULL)) != NULL) {
6424 /*
6425 * Stop async tasks. The async thread may need to detach
6426 * a device that's been replaced, which requires grabbing
6427 * spa_namespace_lock, so we must drop it here.
6428 */
6429 spa_open_ref(spa, FTAG);
6430 mutex_exit(&spa_namespace_lock);
6431 spa_async_suspend(spa);
6432 mutex_enter(&spa_namespace_lock);
34dc7c2f
BB
6433 spa_close(spa, FTAG);
6434
6435 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6436 spa_unload(spa);
6437 spa_deactivate(spa);
6438 }
6439 spa_remove(spa);
6440 }
6441 mutex_exit(&spa_namespace_lock);
6442}
6443
6444vdev_t *
9babb374 6445spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
34dc7c2f 6446{
b128c09f
BB
6447 vdev_t *vd;
6448 int i;
6449
6450 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6451 return (vd);
6452
9babb374 6453 if (aux) {
b128c09f
BB
6454 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6455 vd = spa->spa_l2cache.sav_vdevs[i];
9babb374
BB
6456 if (vd->vdev_guid == guid)
6457 return (vd);
6458 }
6459
6460 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6461 vd = spa->spa_spares.sav_vdevs[i];
b128c09f
BB
6462 if (vd->vdev_guid == guid)
6463 return (vd);
6464 }
6465 }
6466
6467 return (NULL);
34dc7c2f
BB
6468}
6469
6470void
6471spa_upgrade(spa_t *spa, uint64_t version)
6472{
572e2857
BB
6473 ASSERT(spa_writeable(spa));
6474
b128c09f 6475 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
34dc7c2f
BB
6476
6477 /*
6478 * This should only be called for a non-faulted pool, and since a
6479 * future version would result in an unopenable pool, this shouldn't be
6480 * possible.
6481 */
8dca0a9a 6482 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
9b67f605 6483 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
34dc7c2f
BB
6484
6485 spa->spa_uberblock.ub_version = version;
6486 vdev_config_dirty(spa->spa_root_vdev);
6487
b128c09f 6488 spa_config_exit(spa, SCL_ALL, FTAG);
34dc7c2f
BB
6489
6490 txg_wait_synced(spa_get_dsl(spa), 0);
6491}
6492
6493boolean_t
6494spa_has_spare(spa_t *spa, uint64_t guid)
6495{
6496 int i;
6497 uint64_t spareguid;
6498 spa_aux_vdev_t *sav = &spa->spa_spares;
6499
6500 for (i = 0; i < sav->sav_count; i++)
6501 if (sav->sav_vdevs[i]->vdev_guid == guid)
6502 return (B_TRUE);
6503
6504 for (i = 0; i < sav->sav_npending; i++) {
6505 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6506 &spareguid) == 0 && spareguid == guid)
6507 return (B_TRUE);
6508 }
6509
6510 return (B_FALSE);
6511}
6512
b128c09f
BB
6513/*
6514 * Check if a pool has an active shared spare device.
6515 * Note: reference count of an active spare is 2, as a spare and as a replace
6516 */
6517static boolean_t
6518spa_has_active_shared_spare(spa_t *spa)
6519{
6520 int i, refcnt;
6521 uint64_t pool;
6522 spa_aux_vdev_t *sav = &spa->spa_spares;
6523
6524 for (i = 0; i < sav->sav_count; i++) {
6525 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6526 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6527 refcnt > 2)
6528 return (B_TRUE);
6529 }
6530
6531 return (B_FALSE);
6532}
6533
34dc7c2f 6534/*
26685276 6535 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
34dc7c2f
BB
6536 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6537 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6538 * or zdb as real changes.
6539 */
6540void
6541spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6542{
6543#ifdef _KERNEL
26685276 6544 zfs_ereport_post(name, spa, vd, NULL, 0, 0);
34dc7c2f
BB
6545#endif
6546}
c28b2279
BB
6547
6548#if defined(_KERNEL) && defined(HAVE_SPL)
6549/* state manipulation functions */
6550EXPORT_SYMBOL(spa_open);
6551EXPORT_SYMBOL(spa_open_rewind);
6552EXPORT_SYMBOL(spa_get_stats);
6553EXPORT_SYMBOL(spa_create);
6554EXPORT_SYMBOL(spa_import_rootpool);
6555EXPORT_SYMBOL(spa_import);
6556EXPORT_SYMBOL(spa_tryimport);
6557EXPORT_SYMBOL(spa_destroy);
6558EXPORT_SYMBOL(spa_export);
6559EXPORT_SYMBOL(spa_reset);
6560EXPORT_SYMBOL(spa_async_request);
6561EXPORT_SYMBOL(spa_async_suspend);
6562EXPORT_SYMBOL(spa_async_resume);
6563EXPORT_SYMBOL(spa_inject_addref);
6564EXPORT_SYMBOL(spa_inject_delref);
6565EXPORT_SYMBOL(spa_scan_stat_init);
6566EXPORT_SYMBOL(spa_scan_get_stats);
6567
6568/* device maniion */
6569EXPORT_SYMBOL(spa_vdev_add);
6570EXPORT_SYMBOL(spa_vdev_attach);
6571EXPORT_SYMBOL(spa_vdev_detach);
6572EXPORT_SYMBOL(spa_vdev_remove);
6573EXPORT_SYMBOL(spa_vdev_setpath);
6574EXPORT_SYMBOL(spa_vdev_setfru);
6575EXPORT_SYMBOL(spa_vdev_split_mirror);
6576
6577/* spare statech is global across all pools) */
6578EXPORT_SYMBOL(spa_spare_add);
6579EXPORT_SYMBOL(spa_spare_remove);
6580EXPORT_SYMBOL(spa_spare_exists);
6581EXPORT_SYMBOL(spa_spare_activate);
6582
6583/* L2ARC statech is global across all pools) */
6584EXPORT_SYMBOL(spa_l2cache_add);
6585EXPORT_SYMBOL(spa_l2cache_remove);
6586EXPORT_SYMBOL(spa_l2cache_exists);
6587EXPORT_SYMBOL(spa_l2cache_activate);
6588EXPORT_SYMBOL(spa_l2cache_drop);
6589
6590/* scanning */
6591EXPORT_SYMBOL(spa_scan);
6592EXPORT_SYMBOL(spa_scan_stop);
6593
6594/* spa syncing */
6595EXPORT_SYMBOL(spa_sync); /* only for DMU use */
6596EXPORT_SYMBOL(spa_sync_allpools);
6597
6598/* properties */
6599EXPORT_SYMBOL(spa_prop_set);
6600EXPORT_SYMBOL(spa_prop_get);
6601EXPORT_SYMBOL(spa_prop_clear_bootfs);
6602
6603/* asynchronous event notification */
6604EXPORT_SYMBOL(spa_event_notify);
6605#endif