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