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