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