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