zfs: allow large block/gzip/raidz for boot pools
[unleashed.git] / usr / src / uts / common / fs / zfs / vdev.c
blob4d217fb6e101463f8435c374076417713b215239
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Integros [integros.com]
27 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 #include <sys/zfs_context.h>
31 #include <sys/fm/fs/zfs.h>
32 #include <sys/spa.h>
33 #include <sys/spa_impl.h>
34 #include <sys/dmu.h>
35 #include <sys/dmu_tx.h>
36 #include <sys/vdev_impl.h>
37 #include <sys/uberblock_impl.h>
38 #include <sys/metaslab.h>
39 #include <sys/metaslab_impl.h>
40 #include <sys/space_map.h>
41 #include <sys/space_reftree.h>
42 #include <sys/zio.h>
43 #include <sys/zap.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/arc.h>
46 #include <sys/zil.h>
47 #include <sys/dsl_scan.h>
50 * Virtual device management.
53 static vdev_ops_t *vdev_ops_table[] = {
54 &vdev_root_ops,
55 &vdev_raidz_ops,
56 &vdev_mirror_ops,
57 &vdev_replacing_ops,
58 &vdev_spare_ops,
59 &vdev_disk_ops,
60 &vdev_file_ops,
61 &vdev_missing_ops,
62 &vdev_hole_ops,
63 NULL
66 /* maximum scrub/resilver I/O queue per leaf vdev */
67 int zfs_scrub_limit = 10;
70 * When a vdev is added, it will be divided into approximately (but no
71 * more than) this number of metaslabs.
73 int metaslabs_per_vdev = 200;
76 * Given a vdev type, return the appropriate ops vector.
78 static vdev_ops_t *
79 vdev_getops(const char *type)
81 vdev_ops_t *ops, **opspp;
83 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
84 if (strcmp(ops->vdev_op_type, type) == 0)
85 break;
87 return (ops);
91 * Default asize function: return the MAX of psize with the asize of
92 * all children. This is what's used by anything other than RAID-Z.
94 uint64_t
95 vdev_default_asize(vdev_t *vd, uint64_t psize)
97 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
98 uint64_t csize;
100 for (int c = 0; c < vd->vdev_children; c++) {
101 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
102 asize = MAX(asize, csize);
105 return (asize);
109 * Get the minimum allocatable size. We define the allocatable size as
110 * the vdev's asize rounded to the nearest metaslab. This allows us to
111 * replace or attach devices which don't have the same physical size but
112 * can still satisfy the same number of allocations.
114 uint64_t
115 vdev_get_min_asize(vdev_t *vd)
117 vdev_t *pvd = vd->vdev_parent;
120 * If our parent is NULL (inactive spare or cache) or is the root,
121 * just return our own asize.
123 if (pvd == NULL)
124 return (vd->vdev_asize);
127 * The top-level vdev just returns the allocatable size rounded
128 * to the nearest metaslab.
130 if (vd == vd->vdev_top)
131 return (P2ALIGN(vd->vdev_asize, 1ULL << vd->vdev_ms_shift));
134 * The allocatable space for a raidz vdev is N * sizeof(smallest child),
135 * so each child must provide at least 1/Nth of its asize.
137 if (pvd->vdev_ops == &vdev_raidz_ops)
138 return (pvd->vdev_min_asize / pvd->vdev_children);
140 return (pvd->vdev_min_asize);
143 void
144 vdev_set_min_asize(vdev_t *vd)
146 vd->vdev_min_asize = vdev_get_min_asize(vd);
148 for (int c = 0; c < vd->vdev_children; c++)
149 vdev_set_min_asize(vd->vdev_child[c]);
152 vdev_t *
153 vdev_lookup_top(spa_t *spa, uint64_t vdev)
155 vdev_t *rvd = spa->spa_root_vdev;
157 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
159 if (vdev < rvd->vdev_children) {
160 ASSERT(rvd->vdev_child[vdev] != NULL);
161 return (rvd->vdev_child[vdev]);
164 return (NULL);
167 vdev_t *
168 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
170 vdev_t *mvd;
172 if (vd->vdev_guid == guid)
173 return (vd);
175 for (int c = 0; c < vd->vdev_children; c++)
176 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
177 NULL)
178 return (mvd);
180 return (NULL);
183 static int
184 vdev_count_leaves_impl(vdev_t *vd)
186 int n = 0;
188 if (vd->vdev_ops->vdev_op_leaf)
189 return (1);
191 for (int c = 0; c < vd->vdev_children; c++)
192 n += vdev_count_leaves_impl(vd->vdev_child[c]);
194 return (n);
198 vdev_count_leaves(spa_t *spa)
200 return (vdev_count_leaves_impl(spa->spa_root_vdev));
203 void
204 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
206 size_t oldsize, newsize;
207 uint64_t id = cvd->vdev_id;
208 vdev_t **newchild;
209 spa_t *spa = cvd->vdev_spa;
211 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
212 ASSERT(cvd->vdev_parent == NULL);
214 cvd->vdev_parent = pvd;
216 if (pvd == NULL)
217 return;
219 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
221 oldsize = pvd->vdev_children * sizeof (vdev_t *);
222 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
223 newsize = pvd->vdev_children * sizeof (vdev_t *);
225 newchild = kmem_zalloc(newsize, KM_SLEEP);
226 if (pvd->vdev_child != NULL) {
227 bcopy(pvd->vdev_child, newchild, oldsize);
228 kmem_free(pvd->vdev_child, oldsize);
231 pvd->vdev_child = newchild;
232 pvd->vdev_child[id] = cvd;
234 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
235 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
238 * Walk up all ancestors to update guid sum.
240 for (; pvd != NULL; pvd = pvd->vdev_parent)
241 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
244 void
245 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
247 int c;
248 uint_t id = cvd->vdev_id;
250 ASSERT(cvd->vdev_parent == pvd);
252 if (pvd == NULL)
253 return;
255 ASSERT(id < pvd->vdev_children);
256 ASSERT(pvd->vdev_child[id] == cvd);
258 pvd->vdev_child[id] = NULL;
259 cvd->vdev_parent = NULL;
261 for (c = 0; c < pvd->vdev_children; c++)
262 if (pvd->vdev_child[c])
263 break;
265 if (c == pvd->vdev_children) {
266 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
267 pvd->vdev_child = NULL;
268 pvd->vdev_children = 0;
272 * Walk up all ancestors to update guid sum.
274 for (; pvd != NULL; pvd = pvd->vdev_parent)
275 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
279 * Remove any holes in the child array.
281 void
282 vdev_compact_children(vdev_t *pvd)
284 vdev_t **newchild, *cvd;
285 int oldc = pvd->vdev_children;
286 int newc;
288 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
290 for (int c = newc = 0; c < oldc; c++)
291 if (pvd->vdev_child[c])
292 newc++;
294 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
296 for (int c = newc = 0; c < oldc; c++) {
297 if ((cvd = pvd->vdev_child[c]) != NULL) {
298 newchild[newc] = cvd;
299 cvd->vdev_id = newc++;
303 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
304 pvd->vdev_child = newchild;
305 pvd->vdev_children = newc;
309 * Allocate and minimally initialize a vdev_t.
311 vdev_t *
312 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
314 vdev_t *vd;
316 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
318 if (spa->spa_root_vdev == NULL) {
319 ASSERT(ops == &vdev_root_ops);
320 spa->spa_root_vdev = vd;
321 spa->spa_load_guid = spa_generate_guid(NULL);
324 if (guid == 0 && ops != &vdev_hole_ops) {
325 if (spa->spa_root_vdev == vd) {
327 * The root vdev's guid will also be the pool guid,
328 * which must be unique among all pools.
330 guid = spa_generate_guid(NULL);
331 } else {
333 * Any other vdev's guid must be unique within the pool.
335 guid = spa_generate_guid(spa);
337 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
340 vd->vdev_spa = spa;
341 vd->vdev_id = id;
342 vd->vdev_guid = guid;
343 vd->vdev_guid_sum = guid;
344 vd->vdev_ops = ops;
345 vd->vdev_state = VDEV_STATE_CLOSED;
346 vd->vdev_ishole = (ops == &vdev_hole_ops);
348 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
349 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
350 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
351 for (int t = 0; t < DTL_TYPES; t++) {
352 vd->vdev_dtl[t] = range_tree_create(NULL, NULL,
353 &vd->vdev_dtl_lock);
355 txg_list_create(&vd->vdev_ms_list,
356 offsetof(struct metaslab, ms_txg_node));
357 txg_list_create(&vd->vdev_dtl_list,
358 offsetof(struct vdev, vdev_dtl_node));
359 vd->vdev_stat.vs_timestamp = gethrtime();
360 vdev_queue_init(vd);
361 vdev_cache_init(vd);
363 return (vd);
367 * Allocate a new vdev. The 'alloctype' is used to control whether we are
368 * creating a new vdev or loading an existing one - the behavior is slightly
369 * different for each case.
372 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
373 int alloctype)
375 vdev_ops_t *ops;
376 char *type;
377 uint64_t guid = 0, islog, nparity;
378 vdev_t *vd;
380 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
382 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
383 return (SET_ERROR(EINVAL));
385 if ((ops = vdev_getops(type)) == NULL)
386 return (SET_ERROR(EINVAL));
389 * If this is a load, get the vdev guid from the nvlist.
390 * Otherwise, vdev_alloc_common() will generate one for us.
392 if (alloctype == VDEV_ALLOC_LOAD) {
393 uint64_t label_id;
395 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
396 label_id != id)
397 return (SET_ERROR(EINVAL));
399 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
400 return (SET_ERROR(EINVAL));
401 } else if (alloctype == VDEV_ALLOC_SPARE) {
402 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
403 return (SET_ERROR(EINVAL));
404 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
405 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
406 return (SET_ERROR(EINVAL));
407 } else if (alloctype == VDEV_ALLOC_ROOTPOOL) {
408 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
409 return (SET_ERROR(EINVAL));
413 * The first allocated vdev must be of type 'root'.
415 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
416 return (SET_ERROR(EINVAL));
419 * Determine whether we're a log vdev.
421 islog = 0;
422 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
423 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
424 return (SET_ERROR(ENOTSUP));
426 if (ops == &vdev_hole_ops && spa_version(spa) < SPA_VERSION_HOLES)
427 return (SET_ERROR(ENOTSUP));
430 * Set the nparity property for RAID-Z vdevs.
432 nparity = -1ULL;
433 if (ops == &vdev_raidz_ops) {
434 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
435 &nparity) == 0) {
436 if (nparity == 0 || nparity > VDEV_RAIDZ_MAXPARITY)
437 return (SET_ERROR(EINVAL));
439 * Previous versions could only support 1 or 2 parity
440 * device.
442 if (nparity > 1 &&
443 spa_version(spa) < SPA_VERSION_RAIDZ2)
444 return (SET_ERROR(ENOTSUP));
445 if (nparity > 2 &&
446 spa_version(spa) < SPA_VERSION_RAIDZ3)
447 return (SET_ERROR(ENOTSUP));
448 } else {
450 * We require the parity to be specified for SPAs that
451 * support multiple parity levels.
453 if (spa_version(spa) >= SPA_VERSION_RAIDZ2)
454 return (SET_ERROR(EINVAL));
456 * Otherwise, we default to 1 parity device for RAID-Z.
458 nparity = 1;
460 } else {
461 nparity = 0;
463 ASSERT(nparity != -1ULL);
465 vd = vdev_alloc_common(spa, id, guid, ops);
467 vd->vdev_islog = islog;
468 vd->vdev_nparity = nparity;
470 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
471 vd->vdev_path = spa_strdup(vd->vdev_path);
472 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
473 vd->vdev_devid = spa_strdup(vd->vdev_devid);
474 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
475 &vd->vdev_physpath) == 0)
476 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
477 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_FRU, &vd->vdev_fru) == 0)
478 vd->vdev_fru = spa_strdup(vd->vdev_fru);
481 * Set the whole_disk property. If it's not specified, leave the value
482 * as -1.
484 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
485 &vd->vdev_wholedisk) != 0)
486 vd->vdev_wholedisk = -1ULL;
489 * Look for the 'not present' flag. This will only be set if the device
490 * was not present at the time of import.
492 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
493 &vd->vdev_not_present);
496 * Get the alignment requirement.
498 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
501 * Retrieve the vdev creation time.
503 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
504 &vd->vdev_crtxg);
507 * If we're a top-level vdev, try to load the allocation parameters.
509 if (parent && !parent->vdev_parent &&
510 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
511 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
512 &vd->vdev_ms_array);
513 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
514 &vd->vdev_ms_shift);
515 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
516 &vd->vdev_asize);
517 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVING,
518 &vd->vdev_removing);
519 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_VDEV_TOP_ZAP,
520 &vd->vdev_top_zap);
521 } else {
522 ASSERT0(vd->vdev_top_zap);
525 if (parent && !parent->vdev_parent && alloctype != VDEV_ALLOC_ATTACH) {
526 ASSERT(alloctype == VDEV_ALLOC_LOAD ||
527 alloctype == VDEV_ALLOC_ADD ||
528 alloctype == VDEV_ALLOC_SPLIT ||
529 alloctype == VDEV_ALLOC_ROOTPOOL);
530 vd->vdev_mg = metaslab_group_create(islog ?
531 spa_log_class(spa) : spa_normal_class(spa), vd);
534 if (vd->vdev_ops->vdev_op_leaf &&
535 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_SPLIT)) {
536 (void) nvlist_lookup_uint64(nv,
537 ZPOOL_CONFIG_VDEV_LEAF_ZAP, &vd->vdev_leaf_zap);
538 } else {
539 ASSERT0(vd->vdev_leaf_zap);
543 * If we're a leaf vdev, try to load the DTL object and other state.
546 if (vd->vdev_ops->vdev_op_leaf &&
547 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE ||
548 alloctype == VDEV_ALLOC_ROOTPOOL)) {
549 if (alloctype == VDEV_ALLOC_LOAD) {
550 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
551 &vd->vdev_dtl_object);
552 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
553 &vd->vdev_unspare);
556 if (alloctype == VDEV_ALLOC_ROOTPOOL) {
557 uint64_t spare = 0;
559 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
560 &spare) == 0 && spare)
561 spa_spare_add(vd);
564 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
565 &vd->vdev_offline);
567 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_RESILVER_TXG,
568 &vd->vdev_resilver_txg);
571 * When importing a pool, we want to ignore the persistent fault
572 * state, as the diagnosis made on another system may not be
573 * valid in the current context. Local vdevs will
574 * remain in the faulted state.
576 if (spa_load_state(spa) == SPA_LOAD_OPEN) {
577 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
578 &vd->vdev_faulted);
579 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
580 &vd->vdev_degraded);
581 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
582 &vd->vdev_removed);
584 if (vd->vdev_faulted || vd->vdev_degraded) {
585 char *aux;
587 vd->vdev_label_aux =
588 VDEV_AUX_ERR_EXCEEDED;
589 if (nvlist_lookup_string(nv,
590 ZPOOL_CONFIG_AUX_STATE, &aux) == 0 &&
591 strcmp(aux, "external") == 0)
592 vd->vdev_label_aux = VDEV_AUX_EXTERNAL;
598 * Add ourselves to the parent's list of children.
600 vdev_add_child(parent, vd);
602 *vdp = vd;
604 return (0);
607 void
608 vdev_free(vdev_t *vd)
610 spa_t *spa = vd->vdev_spa;
613 * vdev_free() implies closing the vdev first. This is simpler than
614 * trying to ensure complicated semantics for all callers.
616 vdev_close(vd);
618 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
619 ASSERT(!list_link_active(&vd->vdev_state_dirty_node));
622 * Free all children.
624 for (int c = 0; c < vd->vdev_children; c++)
625 vdev_free(vd->vdev_child[c]);
627 ASSERT(vd->vdev_child == NULL);
628 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
631 * Discard allocation state.
633 if (vd->vdev_mg != NULL) {
634 vdev_metaslab_fini(vd);
635 metaslab_group_destroy(vd->vdev_mg);
638 ASSERT0(vd->vdev_stat.vs_space);
639 ASSERT0(vd->vdev_stat.vs_dspace);
640 ASSERT0(vd->vdev_stat.vs_alloc);
643 * Remove this vdev from its parent's child list.
645 vdev_remove_child(vd->vdev_parent, vd);
647 ASSERT(vd->vdev_parent == NULL);
650 * Clean up vdev structure.
652 vdev_queue_fini(vd);
653 vdev_cache_fini(vd);
655 if (vd->vdev_path)
656 spa_strfree(vd->vdev_path);
657 if (vd->vdev_devid)
658 spa_strfree(vd->vdev_devid);
659 if (vd->vdev_physpath)
660 spa_strfree(vd->vdev_physpath);
661 if (vd->vdev_fru)
662 spa_strfree(vd->vdev_fru);
664 if (vd->vdev_isspare)
665 spa_spare_remove(vd);
666 if (vd->vdev_isl2cache)
667 spa_l2cache_remove(vd);
669 txg_list_destroy(&vd->vdev_ms_list);
670 txg_list_destroy(&vd->vdev_dtl_list);
672 mutex_enter(&vd->vdev_dtl_lock);
673 space_map_close(vd->vdev_dtl_sm);
674 for (int t = 0; t < DTL_TYPES; t++) {
675 range_tree_vacate(vd->vdev_dtl[t], NULL, NULL);
676 range_tree_destroy(vd->vdev_dtl[t]);
678 mutex_exit(&vd->vdev_dtl_lock);
680 mutex_destroy(&vd->vdev_dtl_lock);
681 mutex_destroy(&vd->vdev_stat_lock);
682 mutex_destroy(&vd->vdev_probe_lock);
684 if (vd == spa->spa_root_vdev)
685 spa->spa_root_vdev = NULL;
687 kmem_free(vd, sizeof (vdev_t));
691 * Transfer top-level vdev state from svd to tvd.
693 static void
694 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
696 spa_t *spa = svd->vdev_spa;
697 metaslab_t *msp;
698 vdev_t *vd;
699 int t;
701 ASSERT(tvd == tvd->vdev_top);
703 tvd->vdev_ms_array = svd->vdev_ms_array;
704 tvd->vdev_ms_shift = svd->vdev_ms_shift;
705 tvd->vdev_ms_count = svd->vdev_ms_count;
706 tvd->vdev_top_zap = svd->vdev_top_zap;
708 svd->vdev_ms_array = 0;
709 svd->vdev_ms_shift = 0;
710 svd->vdev_ms_count = 0;
711 svd->vdev_top_zap = 0;
713 if (tvd->vdev_mg)
714 ASSERT3P(tvd->vdev_mg, ==, svd->vdev_mg);
715 tvd->vdev_mg = svd->vdev_mg;
716 tvd->vdev_ms = svd->vdev_ms;
718 svd->vdev_mg = NULL;
719 svd->vdev_ms = NULL;
721 if (tvd->vdev_mg != NULL)
722 tvd->vdev_mg->mg_vd = tvd;
724 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
725 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
726 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
728 svd->vdev_stat.vs_alloc = 0;
729 svd->vdev_stat.vs_space = 0;
730 svd->vdev_stat.vs_dspace = 0;
732 for (t = 0; t < TXG_SIZE; t++) {
733 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
734 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
735 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
736 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
737 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
738 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
741 if (list_link_active(&svd->vdev_config_dirty_node)) {
742 vdev_config_clean(svd);
743 vdev_config_dirty(tvd);
746 if (list_link_active(&svd->vdev_state_dirty_node)) {
747 vdev_state_clean(svd);
748 vdev_state_dirty(tvd);
751 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
752 svd->vdev_deflate_ratio = 0;
754 tvd->vdev_islog = svd->vdev_islog;
755 svd->vdev_islog = 0;
758 static void
759 vdev_top_update(vdev_t *tvd, vdev_t *vd)
761 if (vd == NULL)
762 return;
764 vd->vdev_top = tvd;
766 for (int c = 0; c < vd->vdev_children; c++)
767 vdev_top_update(tvd, vd->vdev_child[c]);
771 * Add a mirror/replacing vdev above an existing vdev.
773 vdev_t *
774 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
776 spa_t *spa = cvd->vdev_spa;
777 vdev_t *pvd = cvd->vdev_parent;
778 vdev_t *mvd;
780 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
782 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
784 mvd->vdev_asize = cvd->vdev_asize;
785 mvd->vdev_min_asize = cvd->vdev_min_asize;
786 mvd->vdev_max_asize = cvd->vdev_max_asize;
787 mvd->vdev_ashift = cvd->vdev_ashift;
788 mvd->vdev_state = cvd->vdev_state;
789 mvd->vdev_crtxg = cvd->vdev_crtxg;
791 vdev_remove_child(pvd, cvd);
792 vdev_add_child(pvd, mvd);
793 cvd->vdev_id = mvd->vdev_children;
794 vdev_add_child(mvd, cvd);
795 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
797 if (mvd == mvd->vdev_top)
798 vdev_top_transfer(cvd, mvd);
800 return (mvd);
804 * Remove a 1-way mirror/replacing vdev from the tree.
806 void
807 vdev_remove_parent(vdev_t *cvd)
809 vdev_t *mvd = cvd->vdev_parent;
810 vdev_t *pvd = mvd->vdev_parent;
812 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
814 ASSERT(mvd->vdev_children == 1);
815 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
816 mvd->vdev_ops == &vdev_replacing_ops ||
817 mvd->vdev_ops == &vdev_spare_ops);
818 cvd->vdev_ashift = mvd->vdev_ashift;
820 vdev_remove_child(mvd, cvd);
821 vdev_remove_child(pvd, mvd);
824 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
825 * Otherwise, we could have detached an offline device, and when we
826 * go to import the pool we'll think we have two top-level vdevs,
827 * instead of a different version of the same top-level vdev.
829 if (mvd->vdev_top == mvd) {
830 uint64_t guid_delta = mvd->vdev_guid - cvd->vdev_guid;
831 cvd->vdev_orig_guid = cvd->vdev_guid;
832 cvd->vdev_guid += guid_delta;
833 cvd->vdev_guid_sum += guid_delta;
835 cvd->vdev_id = mvd->vdev_id;
836 vdev_add_child(pvd, cvd);
837 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
839 if (cvd == cvd->vdev_top)
840 vdev_top_transfer(mvd, cvd);
842 ASSERT(mvd->vdev_children == 0);
843 vdev_free(mvd);
847 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
849 spa_t *spa = vd->vdev_spa;
850 objset_t *mos = spa->spa_meta_objset;
851 uint64_t m;
852 uint64_t oldc = vd->vdev_ms_count;
853 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
854 metaslab_t **mspp;
855 int error;
857 ASSERT(txg == 0 || spa_config_held(spa, SCL_ALLOC, RW_WRITER));
860 * This vdev is not being allocated from yet or is a hole.
862 if (vd->vdev_ms_shift == 0)
863 return (0);
865 ASSERT(!vd->vdev_ishole);
868 * Compute the raidz-deflation ratio. Note, we hard-code
869 * in 128k (1 << 17) because it is the "typical" blocksize.
870 * Even though SPA_MAXBLOCKSIZE changed, this algorithm can not change,
871 * otherwise it would inconsistently account for existing bp's.
873 vd->vdev_deflate_ratio = (1 << 17) /
874 (vdev_psize_to_asize(vd, 1 << 17) >> SPA_MINBLOCKSHIFT);
876 ASSERT(oldc <= newc);
878 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
880 if (oldc != 0) {
881 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
882 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
885 vd->vdev_ms = mspp;
886 vd->vdev_ms_count = newc;
888 for (m = oldc; m < newc; m++) {
889 uint64_t object = 0;
891 if (txg == 0) {
892 error = dmu_read(mos, vd->vdev_ms_array,
893 m * sizeof (uint64_t), sizeof (uint64_t), &object,
894 DMU_READ_PREFETCH);
895 if (error)
896 return (error);
899 error = metaslab_init(vd->vdev_mg, m, object, txg,
900 &(vd->vdev_ms[m]));
901 if (error)
902 return (error);
905 if (txg == 0)
906 spa_config_enter(spa, SCL_ALLOC, FTAG, RW_WRITER);
909 * If the vdev is being removed we don't activate
910 * the metaslabs since we want to ensure that no new
911 * allocations are performed on this device.
913 if (oldc == 0 && !vd->vdev_removing)
914 metaslab_group_activate(vd->vdev_mg);
916 if (txg == 0)
917 spa_config_exit(spa, SCL_ALLOC, FTAG);
919 return (0);
922 void
923 vdev_metaslab_fini(vdev_t *vd)
925 uint64_t m;
926 uint64_t count = vd->vdev_ms_count;
928 if (vd->vdev_ms != NULL) {
929 metaslab_group_passivate(vd->vdev_mg);
930 for (m = 0; m < count; m++) {
931 metaslab_t *msp = vd->vdev_ms[m];
933 if (msp != NULL)
934 metaslab_fini(msp);
936 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
937 vd->vdev_ms = NULL;
941 typedef struct vdev_probe_stats {
942 boolean_t vps_readable;
943 boolean_t vps_writeable;
944 int vps_flags;
945 } vdev_probe_stats_t;
947 static void
948 vdev_probe_done(zio_t *zio)
950 spa_t *spa = zio->io_spa;
951 vdev_t *vd = zio->io_vd;
952 vdev_probe_stats_t *vps = zio->io_private;
954 ASSERT(vd->vdev_probe_zio != NULL);
956 if (zio->io_type == ZIO_TYPE_READ) {
957 if (zio->io_error == 0)
958 vps->vps_readable = 1;
959 if (zio->io_error == 0 && spa_writeable(spa)) {
960 zio_nowait(zio_write_phys(vd->vdev_probe_zio, vd,
961 zio->io_offset, zio->io_size, zio->io_data,
962 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
963 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
964 } else {
965 zio_buf_free(zio->io_data, zio->io_size);
967 } else if (zio->io_type == ZIO_TYPE_WRITE) {
968 if (zio->io_error == 0)
969 vps->vps_writeable = 1;
970 zio_buf_free(zio->io_data, zio->io_size);
971 } else if (zio->io_type == ZIO_TYPE_NULL) {
972 zio_t *pio;
974 vd->vdev_cant_read |= !vps->vps_readable;
975 vd->vdev_cant_write |= !vps->vps_writeable;
977 if (vdev_readable(vd) &&
978 (vdev_writeable(vd) || !spa_writeable(spa))) {
979 zio->io_error = 0;
980 } else {
981 ASSERT(zio->io_error != 0);
982 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
983 spa, vd, NULL, 0, 0);
984 zio->io_error = SET_ERROR(ENXIO);
987 mutex_enter(&vd->vdev_probe_lock);
988 ASSERT(vd->vdev_probe_zio == zio);
989 vd->vdev_probe_zio = NULL;
990 mutex_exit(&vd->vdev_probe_lock);
992 while ((pio = zio_walk_parents(zio)) != NULL)
993 if (!vdev_accessible(vd, pio))
994 pio->io_error = SET_ERROR(ENXIO);
996 kmem_free(vps, sizeof (*vps));
1001 * Determine whether this device is accessible.
1003 * Read and write to several known locations: the pad regions of each
1004 * vdev label but the first, which we leave alone in case it contains
1005 * a VTOC.
1007 zio_t *
1008 vdev_probe(vdev_t *vd, zio_t *zio)
1010 spa_t *spa = vd->vdev_spa;
1011 vdev_probe_stats_t *vps = NULL;
1012 zio_t *pio;
1014 ASSERT(vd->vdev_ops->vdev_op_leaf);
1017 * Don't probe the probe.
1019 if (zio && (zio->io_flags & ZIO_FLAG_PROBE))
1020 return (NULL);
1023 * To prevent 'probe storms' when a device fails, we create
1024 * just one probe i/o at a time. All zios that want to probe
1025 * this vdev will become parents of the probe io.
1027 mutex_enter(&vd->vdev_probe_lock);
1029 if ((pio = vd->vdev_probe_zio) == NULL) {
1030 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
1032 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
1033 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE |
1034 ZIO_FLAG_TRYHARD;
1036 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
1038 * vdev_cant_read and vdev_cant_write can only
1039 * transition from TRUE to FALSE when we have the
1040 * SCL_ZIO lock as writer; otherwise they can only
1041 * transition from FALSE to TRUE. This ensures that
1042 * any zio looking at these values can assume that
1043 * failures persist for the life of the I/O. That's
1044 * important because when a device has intermittent
1045 * connectivity problems, we want to ensure that
1046 * they're ascribed to the device (ENXIO) and not
1047 * the zio (EIO).
1049 * Since we hold SCL_ZIO as writer here, clear both
1050 * values so the probe can reevaluate from first
1051 * principles.
1053 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
1054 vd->vdev_cant_read = B_FALSE;
1055 vd->vdev_cant_write = B_FALSE;
1058 vd->vdev_probe_zio = pio = zio_null(NULL, spa, vd,
1059 vdev_probe_done, vps,
1060 vps->vps_flags | ZIO_FLAG_DONT_PROPAGATE);
1063 * We can't change the vdev state in this context, so we
1064 * kick off an async task to do it on our behalf.
1066 if (zio != NULL) {
1067 vd->vdev_probe_wanted = B_TRUE;
1068 spa_async_request(spa, SPA_ASYNC_PROBE);
1072 if (zio != NULL)
1073 zio_add_child(zio, pio);
1075 mutex_exit(&vd->vdev_probe_lock);
1077 if (vps == NULL) {
1078 ASSERT(zio != NULL);
1079 return (NULL);
1082 for (int l = 1; l < VDEV_LABELS; l++) {
1083 zio_nowait(zio_read_phys(pio, vd,
1084 vdev_label_offset(vd->vdev_psize, l,
1085 offsetof(vdev_label_t, vl_pad2)),
1086 VDEV_PAD_SIZE, zio_buf_alloc(VDEV_PAD_SIZE),
1087 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
1088 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
1091 if (zio == NULL)
1092 return (pio);
1094 zio_nowait(pio);
1095 return (NULL);
1098 static void
1099 vdev_open_child(void *arg)
1101 vdev_t *vd = arg;
1103 vd->vdev_open_thread = curthread;
1104 vd->vdev_open_error = vdev_open(vd);
1105 vd->vdev_open_thread = NULL;
1108 boolean_t
1109 vdev_uses_zvols(vdev_t *vd)
1111 if (vd->vdev_path && strncmp(vd->vdev_path, ZVOL_DIR,
1112 strlen(ZVOL_DIR)) == 0)
1113 return (B_TRUE);
1114 for (int c = 0; c < vd->vdev_children; c++)
1115 if (vdev_uses_zvols(vd->vdev_child[c]))
1116 return (B_TRUE);
1117 return (B_FALSE);
1120 void
1121 vdev_open_children(vdev_t *vd)
1123 taskq_t *tq;
1124 int children = vd->vdev_children;
1127 * in order to handle pools on top of zvols, do the opens
1128 * in a single thread so that the same thread holds the
1129 * spa_namespace_lock
1131 if (vdev_uses_zvols(vd)) {
1132 for (int c = 0; c < children; c++)
1133 vd->vdev_child[c]->vdev_open_error =
1134 vdev_open(vd->vdev_child[c]);
1135 return;
1137 tq = taskq_create("vdev_open", children, minclsyspri,
1138 children, children, TASKQ_PREPOPULATE);
1140 for (int c = 0; c < children; c++)
1141 VERIFY(taskq_dispatch(tq, vdev_open_child, vd->vdev_child[c],
1142 TQ_SLEEP) != NULL);
1144 taskq_destroy(tq);
1148 * Prepare a virtual device for access.
1151 vdev_open(vdev_t *vd)
1153 spa_t *spa = vd->vdev_spa;
1154 int error;
1155 uint64_t osize = 0;
1156 uint64_t max_osize = 0;
1157 uint64_t asize, max_asize, psize;
1158 uint64_t ashift = 0;
1160 ASSERT(vd->vdev_open_thread == curthread ||
1161 spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1162 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
1163 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
1164 vd->vdev_state == VDEV_STATE_OFFLINE);
1166 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1167 vd->vdev_cant_read = B_FALSE;
1168 vd->vdev_cant_write = B_FALSE;
1169 vd->vdev_min_asize = vdev_get_min_asize(vd);
1172 * If this vdev is not removed, check its fault status. If it's
1173 * faulted, bail out of the open.
1175 if (!vd->vdev_removed && vd->vdev_faulted) {
1176 ASSERT(vd->vdev_children == 0);
1177 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1178 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1179 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1180 vd->vdev_label_aux);
1181 return (SET_ERROR(ENXIO));
1182 } else if (vd->vdev_offline) {
1183 ASSERT(vd->vdev_children == 0);
1184 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
1185 return (SET_ERROR(ENXIO));
1188 error = vd->vdev_ops->vdev_op_open(vd, &osize, &max_osize, &ashift);
1191 * Reset the vdev_reopening flag so that we actually close
1192 * the vdev on error.
1194 vd->vdev_reopening = B_FALSE;
1195 if (zio_injection_enabled && error == 0)
1196 error = zio_handle_device_injection(vd, NULL, ENXIO);
1198 if (error) {
1199 if (vd->vdev_removed &&
1200 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
1201 vd->vdev_removed = B_FALSE;
1203 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1204 vd->vdev_stat.vs_aux);
1205 return (error);
1208 vd->vdev_removed = B_FALSE;
1211 * Recheck the faulted flag now that we have confirmed that
1212 * the vdev is accessible. If we're faulted, bail.
1214 if (vd->vdev_faulted) {
1215 ASSERT(vd->vdev_children == 0);
1216 ASSERT(vd->vdev_label_aux == VDEV_AUX_ERR_EXCEEDED ||
1217 vd->vdev_label_aux == VDEV_AUX_EXTERNAL);
1218 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1219 vd->vdev_label_aux);
1220 return (SET_ERROR(ENXIO));
1223 if (vd->vdev_degraded) {
1224 ASSERT(vd->vdev_children == 0);
1225 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1226 VDEV_AUX_ERR_EXCEEDED);
1227 } else {
1228 vdev_set_state(vd, B_TRUE, VDEV_STATE_HEALTHY, 0);
1232 * For hole or missing vdevs we just return success.
1234 if (vd->vdev_ishole || vd->vdev_ops == &vdev_missing_ops)
1235 return (0);
1237 for (int c = 0; c < vd->vdev_children; c++) {
1238 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
1239 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
1240 VDEV_AUX_NONE);
1241 break;
1245 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
1246 max_osize = P2ALIGN(max_osize, (uint64_t)sizeof (vdev_label_t));
1248 if (vd->vdev_children == 0) {
1249 if (osize < SPA_MINDEVSIZE) {
1250 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1251 VDEV_AUX_TOO_SMALL);
1252 return (SET_ERROR(EOVERFLOW));
1254 psize = osize;
1255 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
1256 max_asize = max_osize - (VDEV_LABEL_START_SIZE +
1257 VDEV_LABEL_END_SIZE);
1258 } else {
1259 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
1260 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
1261 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1262 VDEV_AUX_TOO_SMALL);
1263 return (SET_ERROR(EOVERFLOW));
1265 psize = 0;
1266 asize = osize;
1267 max_asize = max_osize;
1270 vd->vdev_psize = psize;
1273 * Make sure the allocatable size hasn't shrunk.
1275 if (asize < vd->vdev_min_asize) {
1276 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1277 VDEV_AUX_BAD_LABEL);
1278 return (SET_ERROR(EINVAL));
1281 if (vd->vdev_asize == 0) {
1283 * This is the first-ever open, so use the computed values.
1284 * For testing purposes, a higher ashift can be requested.
1286 vd->vdev_asize = asize;
1287 vd->vdev_max_asize = max_asize;
1288 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1289 } else {
1291 * Detect if the alignment requirement has increased.
1292 * We don't want to make the pool unavailable, just
1293 * issue a warning instead.
1295 if (ashift > vd->vdev_top->vdev_ashift &&
1296 vd->vdev_ops->vdev_op_leaf) {
1297 cmn_err(CE_WARN,
1298 "Disk, '%s', has a block alignment that is "
1299 "larger than the pool's alignment\n",
1300 vd->vdev_path);
1302 vd->vdev_max_asize = max_asize;
1306 * If all children are healthy and the asize has increased,
1307 * then we've experienced dynamic LUN growth. If automatic
1308 * expansion is enabled then use the additional space.
1310 if (vd->vdev_state == VDEV_STATE_HEALTHY && asize > vd->vdev_asize &&
1311 (vd->vdev_expanding || spa->spa_autoexpand))
1312 vd->vdev_asize = asize;
1314 vdev_set_min_asize(vd);
1317 * Ensure we can issue some IO before declaring the
1318 * vdev open for business.
1320 if (vd->vdev_ops->vdev_op_leaf &&
1321 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1322 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
1323 VDEV_AUX_ERR_EXCEEDED);
1324 return (error);
1328 * Track the min and max ashift values for normal data devices.
1330 if (vd->vdev_top == vd && vd->vdev_ashift != 0 &&
1331 !vd->vdev_islog && vd->vdev_aux == NULL) {
1332 if (vd->vdev_ashift > spa->spa_max_ashift)
1333 spa->spa_max_ashift = vd->vdev_ashift;
1334 if (vd->vdev_ashift < spa->spa_min_ashift)
1335 spa->spa_min_ashift = vd->vdev_ashift;
1339 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1340 * resilver. But don't do this if we are doing a reopen for a scrub,
1341 * since this would just restart the scrub we are already doing.
1343 if (vd->vdev_ops->vdev_op_leaf && !spa->spa_scrub_reopen &&
1344 vdev_resilver_needed(vd, NULL, NULL))
1345 spa_async_request(spa, SPA_ASYNC_RESILVER);
1347 return (0);
1351 * Called once the vdevs are all opened, this routine validates the label
1352 * contents. This needs to be done before vdev_load() so that we don't
1353 * inadvertently do repair I/Os to the wrong device.
1355 * If 'strict' is false ignore the spa guid check. This is necessary because
1356 * if the machine crashed during a re-guid the new guid might have been written
1357 * to all of the vdev labels, but not the cached config. The strict check
1358 * will be performed when the pool is opened again using the mos config.
1360 * This function will only return failure if one of the vdevs indicates that it
1361 * has since been destroyed or exported. This is only possible if
1362 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1363 * will be updated but the function will return 0.
1366 vdev_validate(vdev_t *vd, boolean_t strict)
1368 spa_t *spa = vd->vdev_spa;
1369 nvlist_t *label;
1370 uint64_t guid = 0, top_guid;
1371 uint64_t state;
1373 for (int c = 0; c < vd->vdev_children; c++)
1374 if (vdev_validate(vd->vdev_child[c], strict) != 0)
1375 return (SET_ERROR(EBADF));
1378 * If the device has already failed, or was marked offline, don't do
1379 * any further validation. Otherwise, label I/O will fail and we will
1380 * overwrite the previous state.
1382 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1383 uint64_t aux_guid = 0;
1384 nvlist_t *nvl;
1385 uint64_t txg = spa_last_synced_txg(spa) != 0 ?
1386 spa_last_synced_txg(spa) : -1ULL;
1388 if ((label = vdev_label_read_config(vd, txg)) == NULL) {
1389 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1390 VDEV_AUX_BAD_LABEL);
1391 return (0);
1395 * Determine if this vdev has been split off into another
1396 * pool. If so, then refuse to open it.
1398 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_SPLIT_GUID,
1399 &aux_guid) == 0 && aux_guid == spa_guid(spa)) {
1400 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1401 VDEV_AUX_SPLIT_POOL);
1402 nvlist_free(label);
1403 return (0);
1406 if (strict && (nvlist_lookup_uint64(label,
1407 ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1408 guid != spa_guid(spa))) {
1409 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1410 VDEV_AUX_CORRUPT_DATA);
1411 nvlist_free(label);
1412 return (0);
1415 if (nvlist_lookup_nvlist(label, ZPOOL_CONFIG_VDEV_TREE, &nvl)
1416 != 0 || nvlist_lookup_uint64(nvl, ZPOOL_CONFIG_ORIG_GUID,
1417 &aux_guid) != 0)
1418 aux_guid = 0;
1421 * If this vdev just became a top-level vdev because its
1422 * sibling was detached, it will have adopted the parent's
1423 * vdev guid -- but the label may or may not be on disk yet.
1424 * Fortunately, either version of the label will have the
1425 * same top guid, so if we're a top-level vdev, we can
1426 * safely compare to that instead.
1428 * If we split this vdev off instead, then we also check the
1429 * original pool's guid. We don't want to consider the vdev
1430 * corrupt if it is partway through a split operation.
1432 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1433 &guid) != 0 ||
1434 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1435 &top_guid) != 0 ||
1436 ((vd->vdev_guid != guid && vd->vdev_guid != aux_guid) &&
1437 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1438 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1439 VDEV_AUX_CORRUPT_DATA);
1440 nvlist_free(label);
1441 return (0);
1444 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1445 &state) != 0) {
1446 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1447 VDEV_AUX_CORRUPT_DATA);
1448 nvlist_free(label);
1449 return (0);
1452 nvlist_free(label);
1455 * If this is a verbatim import, no need to check the
1456 * state of the pool.
1458 if (!(spa->spa_import_flags & ZFS_IMPORT_VERBATIM) &&
1459 spa_load_state(spa) == SPA_LOAD_OPEN &&
1460 state != POOL_STATE_ACTIVE)
1461 return (SET_ERROR(EBADF));
1464 * If we were able to open and validate a vdev that was
1465 * previously marked permanently unavailable, clear that state
1466 * now.
1468 if (vd->vdev_not_present)
1469 vd->vdev_not_present = 0;
1472 return (0);
1476 * Close a virtual device.
1478 void
1479 vdev_close(vdev_t *vd)
1481 spa_t *spa = vd->vdev_spa;
1482 vdev_t *pvd = vd->vdev_parent;
1484 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1487 * If our parent is reopening, then we are as well, unless we are
1488 * going offline.
1490 if (pvd != NULL && pvd->vdev_reopening)
1491 vd->vdev_reopening = (pvd->vdev_reopening && !vd->vdev_offline);
1493 vd->vdev_ops->vdev_op_close(vd);
1495 vdev_cache_purge(vd);
1498 * We record the previous state before we close it, so that if we are
1499 * doing a reopen(), we don't generate FMA ereports if we notice that
1500 * it's still faulted.
1502 vd->vdev_prevstate = vd->vdev_state;
1504 if (vd->vdev_offline)
1505 vd->vdev_state = VDEV_STATE_OFFLINE;
1506 else
1507 vd->vdev_state = VDEV_STATE_CLOSED;
1508 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1511 void
1512 vdev_hold(vdev_t *vd)
1514 spa_t *spa = vd->vdev_spa;
1516 ASSERT(spa_is_root(spa));
1517 if (spa->spa_state == POOL_STATE_UNINITIALIZED)
1518 return;
1520 for (int c = 0; c < vd->vdev_children; c++)
1521 vdev_hold(vd->vdev_child[c]);
1523 if (vd->vdev_ops->vdev_op_leaf)
1524 vd->vdev_ops->vdev_op_hold(vd);
1527 void
1528 vdev_rele(vdev_t *vd)
1530 spa_t *spa = vd->vdev_spa;
1532 ASSERT(spa_is_root(spa));
1533 for (int c = 0; c < vd->vdev_children; c++)
1534 vdev_rele(vd->vdev_child[c]);
1536 if (vd->vdev_ops->vdev_op_leaf)
1537 vd->vdev_ops->vdev_op_rele(vd);
1541 * Reopen all interior vdevs and any unopened leaves. We don't actually
1542 * reopen leaf vdevs which had previously been opened as they might deadlock
1543 * on the spa_config_lock. Instead we only obtain the leaf's physical size.
1544 * If the leaf has never been opened then open it, as usual.
1546 void
1547 vdev_reopen(vdev_t *vd)
1549 spa_t *spa = vd->vdev_spa;
1551 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1553 /* set the reopening flag unless we're taking the vdev offline */
1554 vd->vdev_reopening = !vd->vdev_offline;
1555 vdev_close(vd);
1556 (void) vdev_open(vd);
1559 * Call vdev_validate() here to make sure we have the same device.
1560 * Otherwise, a device with an invalid label could be successfully
1561 * opened in response to vdev_reopen().
1563 if (vd->vdev_aux) {
1564 (void) vdev_validate_aux(vd);
1565 if (vdev_readable(vd) && vdev_writeable(vd) &&
1566 vd->vdev_aux == &spa->spa_l2cache &&
1567 !l2arc_vdev_present(vd))
1568 l2arc_add_vdev(spa, vd);
1569 } else {
1570 (void) vdev_validate(vd, B_TRUE);
1574 * Reassess parent vdev's health.
1576 vdev_propagate_state(vd);
1580 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1582 int error;
1585 * Normally, partial opens (e.g. of a mirror) are allowed.
1586 * For a create, however, we want to fail the request if
1587 * there are any components we can't open.
1589 error = vdev_open(vd);
1591 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1592 vdev_close(vd);
1593 return (error ? error : ENXIO);
1597 * Recursively load DTLs and initialize all labels.
1599 if ((error = vdev_dtl_load(vd)) != 0 ||
1600 (error = vdev_label_init(vd, txg, isreplacing ?
1601 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1602 vdev_close(vd);
1603 return (error);
1606 return (0);
1609 void
1610 vdev_metaslab_set_size(vdev_t *vd)
1613 * Aim for roughly metaslabs_per_vdev (default 200) metaslabs per vdev.
1615 vd->vdev_ms_shift = highbit64(vd->vdev_asize / metaslabs_per_vdev);
1616 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1619 void
1620 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1622 ASSERT(vd == vd->vdev_top);
1623 ASSERT(!vd->vdev_ishole);
1624 ASSERT(ISP2(flags));
1625 ASSERT(spa_writeable(vd->vdev_spa));
1627 if (flags & VDD_METASLAB)
1628 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1630 if (flags & VDD_DTL)
1631 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1633 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1636 void
1637 vdev_dirty_leaves(vdev_t *vd, int flags, uint64_t txg)
1639 for (int c = 0; c < vd->vdev_children; c++)
1640 vdev_dirty_leaves(vd->vdev_child[c], flags, txg);
1642 if (vd->vdev_ops->vdev_op_leaf)
1643 vdev_dirty(vd->vdev_top, flags, vd, txg);
1647 * DTLs.
1649 * A vdev's DTL (dirty time log) is the set of transaction groups for which
1650 * the vdev has less than perfect replication. There are four kinds of DTL:
1652 * DTL_MISSING: txgs for which the vdev has no valid copies of the data
1654 * DTL_PARTIAL: txgs for which data is available, but not fully replicated
1656 * DTL_SCRUB: the txgs that could not be repaired by the last scrub; upon
1657 * scrub completion, DTL_SCRUB replaces DTL_MISSING in the range of
1658 * txgs that was scrubbed.
1660 * DTL_OUTAGE: txgs which cannot currently be read, whether due to
1661 * persistent errors or just some device being offline.
1662 * Unlike the other three, the DTL_OUTAGE map is not generally
1663 * maintained; it's only computed when needed, typically to
1664 * determine whether a device can be detached.
1666 * For leaf vdevs, DTL_MISSING and DTL_PARTIAL are identical: the device
1667 * either has the data or it doesn't.
1669 * For interior vdevs such as mirror and RAID-Z the picture is more complex.
1670 * A vdev's DTL_PARTIAL is the union of its children's DTL_PARTIALs, because
1671 * if any child is less than fully replicated, then so is its parent.
1672 * A vdev's DTL_MISSING is a modified union of its children's DTL_MISSINGs,
1673 * comprising only those txgs which appear in 'maxfaults' or more children;
1674 * those are the txgs we don't have enough replication to read. For example,
1675 * double-parity RAID-Z can tolerate up to two missing devices (maxfaults == 2);
1676 * thus, its DTL_MISSING consists of the set of txgs that appear in more than
1677 * two child DTL_MISSING maps.
1679 * It should be clear from the above that to compute the DTLs and outage maps
1680 * for all vdevs, it suffices to know just the leaf vdevs' DTL_MISSING maps.
1681 * Therefore, that is all we keep on disk. When loading the pool, or after
1682 * a configuration change, we generate all other DTLs from first principles.
1684 void
1685 vdev_dtl_dirty(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1687 range_tree_t *rt = vd->vdev_dtl[t];
1689 ASSERT(t < DTL_TYPES);
1690 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1691 ASSERT(spa_writeable(vd->vdev_spa));
1693 mutex_enter(rt->rt_lock);
1694 if (!range_tree_contains(rt, txg, size))
1695 range_tree_add(rt, txg, size);
1696 mutex_exit(rt->rt_lock);
1699 boolean_t
1700 vdev_dtl_contains(vdev_t *vd, vdev_dtl_type_t t, uint64_t txg, uint64_t size)
1702 range_tree_t *rt = vd->vdev_dtl[t];
1703 boolean_t dirty = B_FALSE;
1705 ASSERT(t < DTL_TYPES);
1706 ASSERT(vd != vd->vdev_spa->spa_root_vdev);
1708 mutex_enter(rt->rt_lock);
1709 if (range_tree_space(rt) != 0)
1710 dirty = range_tree_contains(rt, txg, size);
1711 mutex_exit(rt->rt_lock);
1713 return (dirty);
1716 boolean_t
1717 vdev_dtl_empty(vdev_t *vd, vdev_dtl_type_t t)
1719 range_tree_t *rt = vd->vdev_dtl[t];
1720 boolean_t empty;
1722 mutex_enter(rt->rt_lock);
1723 empty = (range_tree_space(rt) == 0);
1724 mutex_exit(rt->rt_lock);
1726 return (empty);
1730 * Returns the lowest txg in the DTL range.
1732 static uint64_t
1733 vdev_dtl_min(vdev_t *vd)
1735 range_seg_t *rs;
1737 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1738 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1739 ASSERT0(vd->vdev_children);
1741 rs = avl_first(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1742 return (rs->rs_start - 1);
1746 * Returns the highest txg in the DTL.
1748 static uint64_t
1749 vdev_dtl_max(vdev_t *vd)
1751 range_seg_t *rs;
1753 ASSERT(MUTEX_HELD(&vd->vdev_dtl_lock));
1754 ASSERT3U(range_tree_space(vd->vdev_dtl[DTL_MISSING]), !=, 0);
1755 ASSERT0(vd->vdev_children);
1757 rs = avl_last(&vd->vdev_dtl[DTL_MISSING]->rt_root);
1758 return (rs->rs_end);
1762 * Determine if a resilvering vdev should remove any DTL entries from
1763 * its range. If the vdev was resilvering for the entire duration of the
1764 * scan then it should excise that range from its DTLs. Otherwise, this
1765 * vdev is considered partially resilvered and should leave its DTL
1766 * entries intact. The comment in vdev_dtl_reassess() describes how we
1767 * excise the DTLs.
1769 static boolean_t
1770 vdev_dtl_should_excise(vdev_t *vd)
1772 spa_t *spa = vd->vdev_spa;
1773 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1775 ASSERT0(scn->scn_phys.scn_errors);
1776 ASSERT0(vd->vdev_children);
1778 if (vd->vdev_resilver_txg == 0 ||
1779 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0)
1780 return (B_TRUE);
1783 * When a resilver is initiated the scan will assign the scn_max_txg
1784 * value to the highest txg value that exists in all DTLs. If this
1785 * device's max DTL is not part of this scan (i.e. it is not in
1786 * the range (scn_min_txg, scn_max_txg] then it is not eligible
1787 * for excision.
1789 if (vdev_dtl_max(vd) <= scn->scn_phys.scn_max_txg) {
1790 ASSERT3U(scn->scn_phys.scn_min_txg, <=, vdev_dtl_min(vd));
1791 ASSERT3U(scn->scn_phys.scn_min_txg, <, vd->vdev_resilver_txg);
1792 ASSERT3U(vd->vdev_resilver_txg, <=, scn->scn_phys.scn_max_txg);
1793 return (B_TRUE);
1795 return (B_FALSE);
1799 * Reassess DTLs after a config change or scrub completion.
1801 void
1802 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1804 spa_t *spa = vd->vdev_spa;
1805 avl_tree_t reftree;
1806 int minref;
1808 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
1810 for (int c = 0; c < vd->vdev_children; c++)
1811 vdev_dtl_reassess(vd->vdev_child[c], txg,
1812 scrub_txg, scrub_done);
1814 if (vd == spa->spa_root_vdev || vd->vdev_ishole || vd->vdev_aux)
1815 return;
1817 if (vd->vdev_ops->vdev_op_leaf) {
1818 dsl_scan_t *scn = spa->spa_dsl_pool->dp_scan;
1820 mutex_enter(&vd->vdev_dtl_lock);
1823 * If we've completed a scan cleanly then determine
1824 * if this vdev should remove any DTLs. We only want to
1825 * excise regions on vdevs that were available during
1826 * the entire duration of this scan.
1828 if (scrub_txg != 0 &&
1829 (spa->spa_scrub_started ||
1830 (scn != NULL && scn->scn_phys.scn_errors == 0)) &&
1831 vdev_dtl_should_excise(vd)) {
1833 * We completed a scrub up to scrub_txg. If we
1834 * did it without rebooting, then the scrub dtl
1835 * will be valid, so excise the old region and
1836 * fold in the scrub dtl. Otherwise, leave the
1837 * dtl as-is if there was an error.
1839 * There's little trick here: to excise the beginning
1840 * of the DTL_MISSING map, we put it into a reference
1841 * tree and then add a segment with refcnt -1 that
1842 * covers the range [0, scrub_txg). This means
1843 * that each txg in that range has refcnt -1 or 0.
1844 * We then add DTL_SCRUB with a refcnt of 2, so that
1845 * entries in the range [0, scrub_txg) will have a
1846 * positive refcnt -- either 1 or 2. We then convert
1847 * the reference tree into the new DTL_MISSING map.
1849 space_reftree_create(&reftree);
1850 space_reftree_add_map(&reftree,
1851 vd->vdev_dtl[DTL_MISSING], 1);
1852 space_reftree_add_seg(&reftree, 0, scrub_txg, -1);
1853 space_reftree_add_map(&reftree,
1854 vd->vdev_dtl[DTL_SCRUB], 2);
1855 space_reftree_generate_map(&reftree,
1856 vd->vdev_dtl[DTL_MISSING], 1);
1857 space_reftree_destroy(&reftree);
1859 range_tree_vacate(vd->vdev_dtl[DTL_PARTIAL], NULL, NULL);
1860 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1861 range_tree_add, vd->vdev_dtl[DTL_PARTIAL]);
1862 if (scrub_done)
1863 range_tree_vacate(vd->vdev_dtl[DTL_SCRUB], NULL, NULL);
1864 range_tree_vacate(vd->vdev_dtl[DTL_OUTAGE], NULL, NULL);
1865 if (!vdev_readable(vd))
1866 range_tree_add(vd->vdev_dtl[DTL_OUTAGE], 0, -1ULL);
1867 else
1868 range_tree_walk(vd->vdev_dtl[DTL_MISSING],
1869 range_tree_add, vd->vdev_dtl[DTL_OUTAGE]);
1872 * If the vdev was resilvering and no longer has any
1873 * DTLs then reset its resilvering flag.
1875 if (vd->vdev_resilver_txg != 0 &&
1876 range_tree_space(vd->vdev_dtl[DTL_MISSING]) == 0 &&
1877 range_tree_space(vd->vdev_dtl[DTL_OUTAGE]) == 0)
1878 vd->vdev_resilver_txg = 0;
1880 mutex_exit(&vd->vdev_dtl_lock);
1882 if (txg != 0)
1883 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1884 return;
1887 mutex_enter(&vd->vdev_dtl_lock);
1888 for (int t = 0; t < DTL_TYPES; t++) {
1889 /* account for child's outage in parent's missing map */
1890 int s = (t == DTL_MISSING) ? DTL_OUTAGE: t;
1891 if (t == DTL_SCRUB)
1892 continue; /* leaf vdevs only */
1893 if (t == DTL_PARTIAL)
1894 minref = 1; /* i.e. non-zero */
1895 else if (vd->vdev_nparity != 0)
1896 minref = vd->vdev_nparity + 1; /* RAID-Z */
1897 else
1898 minref = vd->vdev_children; /* any kind of mirror */
1899 space_reftree_create(&reftree);
1900 for (int c = 0; c < vd->vdev_children; c++) {
1901 vdev_t *cvd = vd->vdev_child[c];
1902 mutex_enter(&cvd->vdev_dtl_lock);
1903 space_reftree_add_map(&reftree, cvd->vdev_dtl[s], 1);
1904 mutex_exit(&cvd->vdev_dtl_lock);
1906 space_reftree_generate_map(&reftree, vd->vdev_dtl[t], minref);
1907 space_reftree_destroy(&reftree);
1909 mutex_exit(&vd->vdev_dtl_lock);
1913 vdev_dtl_load(vdev_t *vd)
1915 spa_t *spa = vd->vdev_spa;
1916 objset_t *mos = spa->spa_meta_objset;
1917 int error = 0;
1919 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_dtl_object != 0) {
1920 ASSERT(!vd->vdev_ishole);
1922 error = space_map_open(&vd->vdev_dtl_sm, mos,
1923 vd->vdev_dtl_object, 0, -1ULL, 0, &vd->vdev_dtl_lock);
1924 if (error)
1925 return (error);
1926 ASSERT(vd->vdev_dtl_sm != NULL);
1928 mutex_enter(&vd->vdev_dtl_lock);
1931 * Now that we've opened the space_map we need to update
1932 * the in-core DTL.
1934 space_map_update(vd->vdev_dtl_sm);
1936 error = space_map_load(vd->vdev_dtl_sm,
1937 vd->vdev_dtl[DTL_MISSING], SM_ALLOC);
1938 mutex_exit(&vd->vdev_dtl_lock);
1940 return (error);
1943 for (int c = 0; c < vd->vdev_children; c++) {
1944 error = vdev_dtl_load(vd->vdev_child[c]);
1945 if (error != 0)
1946 break;
1949 return (error);
1952 void
1953 vdev_destroy_unlink_zap(vdev_t *vd, uint64_t zapobj, dmu_tx_t *tx)
1955 spa_t *spa = vd->vdev_spa;
1957 VERIFY0(zap_destroy(spa->spa_meta_objset, zapobj, tx));
1958 VERIFY0(zap_remove_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1959 zapobj, tx));
1962 uint64_t
1963 vdev_create_link_zap(vdev_t *vd, dmu_tx_t *tx)
1965 spa_t *spa = vd->vdev_spa;
1966 uint64_t zap = zap_create(spa->spa_meta_objset, DMU_OTN_ZAP_METADATA,
1967 DMU_OT_NONE, 0, tx);
1969 ASSERT(zap != 0);
1970 VERIFY0(zap_add_int(spa->spa_meta_objset, spa->spa_all_vdev_zaps,
1971 zap, tx));
1973 return (zap);
1976 void
1977 vdev_construct_zaps(vdev_t *vd, dmu_tx_t *tx)
1979 if (vd->vdev_ops != &vdev_hole_ops &&
1980 vd->vdev_ops != &vdev_missing_ops &&
1981 vd->vdev_ops != &vdev_root_ops &&
1982 !vd->vdev_top->vdev_removing) {
1983 if (vd->vdev_ops->vdev_op_leaf && vd->vdev_leaf_zap == 0) {
1984 vd->vdev_leaf_zap = vdev_create_link_zap(vd, tx);
1986 if (vd == vd->vdev_top && vd->vdev_top_zap == 0) {
1987 vd->vdev_top_zap = vdev_create_link_zap(vd, tx);
1990 for (uint64_t i = 0; i < vd->vdev_children; i++) {
1991 vdev_construct_zaps(vd->vdev_child[i], tx);
1995 void
1996 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1998 spa_t *spa = vd->vdev_spa;
1999 range_tree_t *rt = vd->vdev_dtl[DTL_MISSING];
2000 objset_t *mos = spa->spa_meta_objset;
2001 range_tree_t *rtsync;
2002 kmutex_t rtlock;
2003 dmu_tx_t *tx;
2004 uint64_t object = space_map_object(vd->vdev_dtl_sm);
2006 ASSERT(!vd->vdev_ishole);
2007 ASSERT(vd->vdev_ops->vdev_op_leaf);
2009 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2011 if (vd->vdev_detached || vd->vdev_top->vdev_removing) {
2012 mutex_enter(&vd->vdev_dtl_lock);
2013 space_map_free(vd->vdev_dtl_sm, tx);
2014 space_map_close(vd->vdev_dtl_sm);
2015 vd->vdev_dtl_sm = NULL;
2016 mutex_exit(&vd->vdev_dtl_lock);
2019 * We only destroy the leaf ZAP for detached leaves or for
2020 * removed log devices. Removed data devices handle leaf ZAP
2021 * cleanup later, once cancellation is no longer possible.
2023 if (vd->vdev_leaf_zap != 0 && (vd->vdev_detached ||
2024 vd->vdev_top->vdev_islog)) {
2025 vdev_destroy_unlink_zap(vd, vd->vdev_leaf_zap, tx);
2026 vd->vdev_leaf_zap = 0;
2029 dmu_tx_commit(tx);
2030 return;
2033 if (vd->vdev_dtl_sm == NULL) {
2034 uint64_t new_object;
2036 new_object = space_map_alloc(mos, tx);
2037 VERIFY3U(new_object, !=, 0);
2039 VERIFY0(space_map_open(&vd->vdev_dtl_sm, mos, new_object,
2040 0, -1ULL, 0, &vd->vdev_dtl_lock));
2041 ASSERT(vd->vdev_dtl_sm != NULL);
2044 mutex_init(&rtlock, NULL, MUTEX_DEFAULT, NULL);
2046 rtsync = range_tree_create(NULL, NULL, &rtlock);
2048 mutex_enter(&rtlock);
2050 mutex_enter(&vd->vdev_dtl_lock);
2051 range_tree_walk(rt, range_tree_add, rtsync);
2052 mutex_exit(&vd->vdev_dtl_lock);
2054 space_map_truncate(vd->vdev_dtl_sm, tx);
2055 space_map_write(vd->vdev_dtl_sm, rtsync, SM_ALLOC, tx);
2056 range_tree_vacate(rtsync, NULL, NULL);
2058 range_tree_destroy(rtsync);
2060 mutex_exit(&rtlock);
2061 mutex_destroy(&rtlock);
2064 * If the object for the space map has changed then dirty
2065 * the top level so that we update the config.
2067 if (object != space_map_object(vd->vdev_dtl_sm)) {
2068 zfs_dbgmsg("txg %llu, spa %s, DTL old object %llu, "
2069 "new object %llu", txg, spa_name(spa), object,
2070 space_map_object(vd->vdev_dtl_sm));
2071 vdev_config_dirty(vd->vdev_top);
2074 dmu_tx_commit(tx);
2076 mutex_enter(&vd->vdev_dtl_lock);
2077 space_map_update(vd->vdev_dtl_sm);
2078 mutex_exit(&vd->vdev_dtl_lock);
2082 * Determine whether the specified vdev can be offlined/detached/removed
2083 * without losing data.
2085 boolean_t
2086 vdev_dtl_required(vdev_t *vd)
2088 spa_t *spa = vd->vdev_spa;
2089 vdev_t *tvd = vd->vdev_top;
2090 uint8_t cant_read = vd->vdev_cant_read;
2091 boolean_t required;
2093 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2095 if (vd == spa->spa_root_vdev || vd == tvd)
2096 return (B_TRUE);
2099 * Temporarily mark the device as unreadable, and then determine
2100 * whether this results in any DTL outages in the top-level vdev.
2101 * If not, we can safely offline/detach/remove the device.
2103 vd->vdev_cant_read = B_TRUE;
2104 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2105 required = !vdev_dtl_empty(tvd, DTL_OUTAGE);
2106 vd->vdev_cant_read = cant_read;
2107 vdev_dtl_reassess(tvd, 0, 0, B_FALSE);
2109 if (!required && zio_injection_enabled)
2110 required = !!zio_handle_device_injection(vd, NULL, ECHILD);
2112 return (required);
2116 * Determine if resilver is needed, and if so the txg range.
2118 boolean_t
2119 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
2121 boolean_t needed = B_FALSE;
2122 uint64_t thismin = UINT64_MAX;
2123 uint64_t thismax = 0;
2125 if (vd->vdev_children == 0) {
2126 mutex_enter(&vd->vdev_dtl_lock);
2127 if (range_tree_space(vd->vdev_dtl[DTL_MISSING]) != 0 &&
2128 vdev_writeable(vd)) {
2130 thismin = vdev_dtl_min(vd);
2131 thismax = vdev_dtl_max(vd);
2132 needed = B_TRUE;
2134 mutex_exit(&vd->vdev_dtl_lock);
2135 } else {
2136 for (int c = 0; c < vd->vdev_children; c++) {
2137 vdev_t *cvd = vd->vdev_child[c];
2138 uint64_t cmin, cmax;
2140 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
2141 thismin = MIN(thismin, cmin);
2142 thismax = MAX(thismax, cmax);
2143 needed = B_TRUE;
2148 if (needed && minp) {
2149 *minp = thismin;
2150 *maxp = thismax;
2152 return (needed);
2155 void
2156 vdev_load(vdev_t *vd)
2159 * Recursively load all children.
2161 for (int c = 0; c < vd->vdev_children; c++)
2162 vdev_load(vd->vdev_child[c]);
2165 * If this is a top-level vdev, initialize its metaslabs.
2167 if (vd == vd->vdev_top && !vd->vdev_ishole &&
2168 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
2169 vdev_metaslab_init(vd, 0) != 0))
2170 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2171 VDEV_AUX_CORRUPT_DATA);
2174 * If this is a leaf vdev, load its DTL.
2176 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
2177 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
2178 VDEV_AUX_CORRUPT_DATA);
2182 * The special vdev case is used for hot spares and l2cache devices. Its
2183 * sole purpose it to set the vdev state for the associated vdev. To do this,
2184 * we make sure that we can open the underlying device, then try to read the
2185 * label, and make sure that the label is sane and that it hasn't been
2186 * repurposed to another pool.
2189 vdev_validate_aux(vdev_t *vd)
2191 nvlist_t *label;
2192 uint64_t guid, version;
2193 uint64_t state;
2195 if (!vdev_readable(vd))
2196 return (0);
2198 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
2199 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2200 VDEV_AUX_CORRUPT_DATA);
2201 return (-1);
2204 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
2205 !SPA_VERSION_IS_SUPPORTED(version) ||
2206 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
2207 guid != vd->vdev_guid ||
2208 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
2209 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
2210 VDEV_AUX_CORRUPT_DATA);
2211 nvlist_free(label);
2212 return (-1);
2216 * We don't actually check the pool state here. If it's in fact in
2217 * use by another pool, we update this fact on the fly when requested.
2219 nvlist_free(label);
2220 return (0);
2223 void
2224 vdev_remove(vdev_t *vd, uint64_t txg)
2226 spa_t *spa = vd->vdev_spa;
2227 objset_t *mos = spa->spa_meta_objset;
2228 dmu_tx_t *tx;
2230 tx = dmu_tx_create_assigned(spa_get_dsl(spa), txg);
2231 ASSERT(vd == vd->vdev_top);
2232 ASSERT3U(txg, ==, spa_syncing_txg(spa));
2234 if (vd->vdev_ms != NULL) {
2235 metaslab_group_t *mg = vd->vdev_mg;
2237 metaslab_group_histogram_verify(mg);
2238 metaslab_class_histogram_verify(mg->mg_class);
2240 for (int m = 0; m < vd->vdev_ms_count; m++) {
2241 metaslab_t *msp = vd->vdev_ms[m];
2243 if (msp == NULL || msp->ms_sm == NULL)
2244 continue;
2246 mutex_enter(&msp->ms_lock);
2248 * If the metaslab was not loaded when the vdev
2249 * was removed then the histogram accounting may
2250 * not be accurate. Update the histogram information
2251 * here so that we ensure that the metaslab group
2252 * and metaslab class are up-to-date.
2254 metaslab_group_histogram_remove(mg, msp);
2256 VERIFY0(space_map_allocated(msp->ms_sm));
2257 space_map_free(msp->ms_sm, tx);
2258 space_map_close(msp->ms_sm);
2259 msp->ms_sm = NULL;
2260 mutex_exit(&msp->ms_lock);
2263 metaslab_group_histogram_verify(mg);
2264 metaslab_class_histogram_verify(mg->mg_class);
2265 for (int i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++)
2266 ASSERT0(mg->mg_histogram[i]);
2270 if (vd->vdev_ms_array) {
2271 (void) dmu_object_free(mos, vd->vdev_ms_array, tx);
2272 vd->vdev_ms_array = 0;
2275 if (vd->vdev_islog && vd->vdev_top_zap != 0) {
2276 vdev_destroy_unlink_zap(vd, vd->vdev_top_zap, tx);
2277 vd->vdev_top_zap = 0;
2279 dmu_tx_commit(tx);
2282 void
2283 vdev_sync_done(vdev_t *vd, uint64_t txg)
2285 metaslab_t *msp;
2286 boolean_t reassess = !txg_list_empty(&vd->vdev_ms_list, TXG_CLEAN(txg));
2288 ASSERT(!vd->vdev_ishole);
2290 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
2291 metaslab_sync_done(msp, txg);
2293 if (reassess)
2294 metaslab_sync_reassess(vd->vdev_mg);
2297 void
2298 vdev_sync(vdev_t *vd, uint64_t txg)
2300 spa_t *spa = vd->vdev_spa;
2301 vdev_t *lvd;
2302 metaslab_t *msp;
2303 dmu_tx_t *tx;
2305 ASSERT(!vd->vdev_ishole);
2307 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
2308 ASSERT(vd == vd->vdev_top);
2309 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
2310 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
2311 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
2312 ASSERT(vd->vdev_ms_array != 0);
2313 vdev_config_dirty(vd);
2314 dmu_tx_commit(tx);
2318 * Remove the metadata associated with this vdev once it's empty.
2320 if (vd->vdev_stat.vs_alloc == 0 && vd->vdev_removing)
2321 vdev_remove(vd, txg);
2323 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
2324 metaslab_sync(msp, txg);
2325 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
2328 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
2329 vdev_dtl_sync(lvd, txg);
2331 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
2334 uint64_t
2335 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
2337 return (vd->vdev_ops->vdev_op_asize(vd, psize));
2341 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
2342 * not be opened, and no I/O is attempted.
2345 vdev_fault(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2347 vdev_t *vd, *tvd;
2349 spa_vdev_state_enter(spa, SCL_NONE);
2351 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2352 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2354 if (!vd->vdev_ops->vdev_op_leaf)
2355 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2357 tvd = vd->vdev_top;
2360 * We don't directly use the aux state here, but if we do a
2361 * vdev_reopen(), we need this value to be present to remember why we
2362 * were faulted.
2364 vd->vdev_label_aux = aux;
2367 * Faulted state takes precedence over degraded.
2369 vd->vdev_delayed_close = B_FALSE;
2370 vd->vdev_faulted = 1ULL;
2371 vd->vdev_degraded = 0ULL;
2372 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, aux);
2375 * If this device has the only valid copy of the data, then
2376 * back off and simply mark the vdev as degraded instead.
2378 if (!tvd->vdev_islog && vd->vdev_aux == NULL && vdev_dtl_required(vd)) {
2379 vd->vdev_degraded = 1ULL;
2380 vd->vdev_faulted = 0ULL;
2383 * If we reopen the device and it's not dead, only then do we
2384 * mark it degraded.
2386 vdev_reopen(tvd);
2388 if (vdev_readable(vd))
2389 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, aux);
2392 return (spa_vdev_state_exit(spa, vd, 0));
2396 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
2397 * user that something is wrong. The vdev continues to operate as normal as far
2398 * as I/O is concerned.
2401 vdev_degrade(spa_t *spa, uint64_t guid, vdev_aux_t aux)
2403 vdev_t *vd;
2405 spa_vdev_state_enter(spa, SCL_NONE);
2407 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2408 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2410 if (!vd->vdev_ops->vdev_op_leaf)
2411 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2414 * If the vdev is already faulted, then don't do anything.
2416 if (vd->vdev_faulted || vd->vdev_degraded)
2417 return (spa_vdev_state_exit(spa, NULL, 0));
2419 vd->vdev_degraded = 1ULL;
2420 if (!vdev_is_dead(vd))
2421 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
2422 aux);
2424 return (spa_vdev_state_exit(spa, vd, 0));
2428 * Online the given vdev.
2430 * If 'ZFS_ONLINE_UNSPARE' is set, it implies two things. First, any attached
2431 * spare device should be detached when the device finishes resilvering.
2432 * Second, the online should be treated like a 'test' online case, so no FMA
2433 * events are generated if the device fails to open.
2436 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
2438 vdev_t *vd, *tvd, *pvd, *rvd = spa->spa_root_vdev;
2439 boolean_t postevent = B_FALSE;
2441 spa_vdev_state_enter(spa, SCL_NONE);
2443 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2444 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2446 if (!vd->vdev_ops->vdev_op_leaf)
2447 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2449 postevent =
2450 (vd->vdev_offline == B_TRUE || vd->vdev_tmpoffline == B_TRUE) ?
2451 B_TRUE : B_FALSE;
2453 tvd = vd->vdev_top;
2454 vd->vdev_offline = B_FALSE;
2455 vd->vdev_tmpoffline = B_FALSE;
2456 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
2457 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
2459 /* XXX - L2ARC 1.0 does not support expansion */
2460 if (!vd->vdev_aux) {
2461 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2462 pvd->vdev_expanding = !!(flags & ZFS_ONLINE_EXPAND);
2465 vdev_reopen(tvd);
2466 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
2468 if (!vd->vdev_aux) {
2469 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2470 pvd->vdev_expanding = B_FALSE;
2473 if (newstate)
2474 *newstate = vd->vdev_state;
2475 if ((flags & ZFS_ONLINE_UNSPARE) &&
2476 !vdev_is_dead(vd) && vd->vdev_parent &&
2477 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2478 vd->vdev_parent->vdev_child[0] == vd)
2479 vd->vdev_unspare = B_TRUE;
2481 if ((flags & ZFS_ONLINE_EXPAND) || spa->spa_autoexpand) {
2483 /* XXX - L2ARC 1.0 does not support expansion */
2484 if (vd->vdev_aux)
2485 return (spa_vdev_state_exit(spa, vd, ENOTSUP));
2486 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2489 if (postevent)
2490 spa_event_notify(spa, vd, ESC_ZFS_VDEV_ONLINE);
2492 return (spa_vdev_state_exit(spa, vd, 0));
2495 static int
2496 vdev_offline_locked(spa_t *spa, uint64_t guid, uint64_t flags)
2498 vdev_t *vd, *tvd;
2499 int error = 0;
2500 uint64_t generation;
2501 metaslab_group_t *mg;
2503 top:
2504 spa_vdev_state_enter(spa, SCL_ALLOC);
2506 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
2507 return (spa_vdev_state_exit(spa, NULL, ENODEV));
2509 if (!vd->vdev_ops->vdev_op_leaf)
2510 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
2512 tvd = vd->vdev_top;
2513 mg = tvd->vdev_mg;
2514 generation = spa->spa_config_generation + 1;
2517 * If the device isn't already offline, try to offline it.
2519 if (!vd->vdev_offline) {
2521 * If this device has the only valid copy of some data,
2522 * don't allow it to be offlined. Log devices are always
2523 * expendable.
2525 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2526 vdev_dtl_required(vd))
2527 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2530 * If the top-level is a slog and it has had allocations
2531 * then proceed. We check that the vdev's metaslab group
2532 * is not NULL since it's possible that we may have just
2533 * added this vdev but not yet initialized its metaslabs.
2535 if (tvd->vdev_islog && mg != NULL) {
2537 * Prevent any future allocations.
2539 metaslab_group_passivate(mg);
2540 (void) spa_vdev_state_exit(spa, vd, 0);
2542 error = spa_offline_log(spa);
2544 spa_vdev_state_enter(spa, SCL_ALLOC);
2547 * Check to see if the config has changed.
2549 if (error || generation != spa->spa_config_generation) {
2550 metaslab_group_activate(mg);
2551 if (error)
2552 return (spa_vdev_state_exit(spa,
2553 vd, error));
2554 (void) spa_vdev_state_exit(spa, vd, 0);
2555 goto top;
2557 ASSERT0(tvd->vdev_stat.vs_alloc);
2561 * Offline this device and reopen its top-level vdev.
2562 * If the top-level vdev is a log device then just offline
2563 * it. Otherwise, if this action results in the top-level
2564 * vdev becoming unusable, undo it and fail the request.
2566 vd->vdev_offline = B_TRUE;
2567 vdev_reopen(tvd);
2569 if (!tvd->vdev_islog && vd->vdev_aux == NULL &&
2570 vdev_is_dead(tvd)) {
2571 vd->vdev_offline = B_FALSE;
2572 vdev_reopen(tvd);
2573 return (spa_vdev_state_exit(spa, NULL, EBUSY));
2577 * Add the device back into the metaslab rotor so that
2578 * once we online the device it's open for business.
2580 if (tvd->vdev_islog && mg != NULL)
2581 metaslab_group_activate(mg);
2584 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
2586 return (spa_vdev_state_exit(spa, vd, 0));
2590 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
2592 int error;
2594 mutex_enter(&spa->spa_vdev_top_lock);
2595 error = vdev_offline_locked(spa, guid, flags);
2596 mutex_exit(&spa->spa_vdev_top_lock);
2598 return (error);
2602 * Clear the error counts associated with this vdev. Unlike vdev_online() and
2603 * vdev_offline(), we assume the spa config is locked. We also clear all
2604 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
2606 void
2607 vdev_clear(spa_t *spa, vdev_t *vd)
2609 vdev_t *rvd = spa->spa_root_vdev;
2611 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
2613 if (vd == NULL)
2614 vd = rvd;
2616 vd->vdev_stat.vs_read_errors = 0;
2617 vd->vdev_stat.vs_write_errors = 0;
2618 vd->vdev_stat.vs_checksum_errors = 0;
2620 for (int c = 0; c < vd->vdev_children; c++)
2621 vdev_clear(spa, vd->vdev_child[c]);
2624 * If we're in the FAULTED state or have experienced failed I/O, then
2625 * clear the persistent state and attempt to reopen the device. We
2626 * also mark the vdev config dirty, so that the new faulted state is
2627 * written out to disk.
2629 if (vd->vdev_faulted || vd->vdev_degraded ||
2630 !vdev_readable(vd) || !vdev_writeable(vd)) {
2633 * When reopening in reponse to a clear event, it may be due to
2634 * a fmadm repair request. In this case, if the device is
2635 * still broken, we want to still post the ereport again.
2637 vd->vdev_forcefault = B_TRUE;
2639 vd->vdev_faulted = vd->vdev_degraded = 0ULL;
2640 vd->vdev_cant_read = B_FALSE;
2641 vd->vdev_cant_write = B_FALSE;
2643 vdev_reopen(vd == rvd ? rvd : vd->vdev_top);
2645 vd->vdev_forcefault = B_FALSE;
2647 if (vd != rvd && vdev_writeable(vd->vdev_top))
2648 vdev_state_dirty(vd->vdev_top);
2650 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
2651 spa_async_request(spa, SPA_ASYNC_RESILVER);
2653 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
2657 * When clearing a FMA-diagnosed fault, we always want to
2658 * unspare the device, as we assume that the original spare was
2659 * done in response to the FMA fault.
2661 if (!vdev_is_dead(vd) && vd->vdev_parent != NULL &&
2662 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
2663 vd->vdev_parent->vdev_child[0] == vd)
2664 vd->vdev_unspare = B_TRUE;
2667 boolean_t
2668 vdev_is_dead(vdev_t *vd)
2671 * Holes and missing devices are always considered "dead".
2672 * This simplifies the code since we don't have to check for
2673 * these types of devices in the various code paths.
2674 * Instead we rely on the fact that we skip over dead devices
2675 * before issuing I/O to them.
2677 return (vd->vdev_state < VDEV_STATE_DEGRADED || vd->vdev_ishole ||
2678 vd->vdev_ops == &vdev_missing_ops);
2681 boolean_t
2682 vdev_readable(vdev_t *vd)
2684 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
2687 boolean_t
2688 vdev_writeable(vdev_t *vd)
2690 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
2693 boolean_t
2694 vdev_allocatable(vdev_t *vd)
2696 uint64_t state = vd->vdev_state;
2699 * We currently allow allocations from vdevs which may be in the
2700 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
2701 * fails to reopen then we'll catch it later when we're holding
2702 * the proper locks. Note that we have to get the vdev state
2703 * in a local variable because although it changes atomically,
2704 * we're asking two separate questions about it.
2706 return (!(state < VDEV_STATE_DEGRADED && state != VDEV_STATE_CLOSED) &&
2707 !vd->vdev_cant_write && !vd->vdev_ishole);
2710 boolean_t
2711 vdev_accessible(vdev_t *vd, zio_t *zio)
2713 ASSERT(zio->io_vd == vd);
2715 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
2716 return (B_FALSE);
2718 if (zio->io_type == ZIO_TYPE_READ)
2719 return (!vd->vdev_cant_read);
2721 if (zio->io_type == ZIO_TYPE_WRITE)
2722 return (!vd->vdev_cant_write);
2724 return (B_TRUE);
2728 * Get statistics for the given vdev.
2730 void
2731 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
2733 spa_t *spa = vd->vdev_spa;
2734 vdev_t *rvd = spa->spa_root_vdev;
2736 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
2738 mutex_enter(&vd->vdev_stat_lock);
2739 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
2740 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
2741 vs->vs_state = vd->vdev_state;
2742 vs->vs_rsize = vdev_get_min_asize(vd);
2743 if (vd->vdev_ops->vdev_op_leaf)
2744 vs->vs_rsize += VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE;
2745 vs->vs_esize = vd->vdev_max_asize - vd->vdev_asize;
2746 if (vd->vdev_aux == NULL && vd == vd->vdev_top && !vd->vdev_ishole) {
2747 vs->vs_fragmentation = vd->vdev_mg->mg_fragmentation;
2751 * If we're getting stats on the root vdev, aggregate the I/O counts
2752 * over all top-level vdevs (i.e. the direct children of the root).
2754 if (vd == rvd) {
2755 for (int c = 0; c < rvd->vdev_children; c++) {
2756 vdev_t *cvd = rvd->vdev_child[c];
2757 vdev_stat_t *cvs = &cvd->vdev_stat;
2759 for (int t = 0; t < ZIO_TYPES; t++) {
2760 vs->vs_ops[t] += cvs->vs_ops[t];
2761 vs->vs_bytes[t] += cvs->vs_bytes[t];
2763 cvs->vs_scan_removing = cvd->vdev_removing;
2766 mutex_exit(&vd->vdev_stat_lock);
2769 void
2770 vdev_clear_stats(vdev_t *vd)
2772 mutex_enter(&vd->vdev_stat_lock);
2773 vd->vdev_stat.vs_space = 0;
2774 vd->vdev_stat.vs_dspace = 0;
2775 vd->vdev_stat.vs_alloc = 0;
2776 mutex_exit(&vd->vdev_stat_lock);
2779 void
2780 vdev_scan_stat_init(vdev_t *vd)
2782 vdev_stat_t *vs = &vd->vdev_stat;
2784 for (int c = 0; c < vd->vdev_children; c++)
2785 vdev_scan_stat_init(vd->vdev_child[c]);
2787 mutex_enter(&vd->vdev_stat_lock);
2788 vs->vs_scan_processed = 0;
2789 mutex_exit(&vd->vdev_stat_lock);
2792 void
2793 vdev_stat_update(zio_t *zio, uint64_t psize)
2795 spa_t *spa = zio->io_spa;
2796 vdev_t *rvd = spa->spa_root_vdev;
2797 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
2798 vdev_t *pvd;
2799 uint64_t txg = zio->io_txg;
2800 vdev_stat_t *vs = &vd->vdev_stat;
2801 zio_type_t type = zio->io_type;
2802 int flags = zio->io_flags;
2805 * If this i/o is a gang leader, it didn't do any actual work.
2807 if (zio->io_gang_tree)
2808 return;
2810 if (zio->io_error == 0) {
2812 * If this is a root i/o, don't count it -- we've already
2813 * counted the top-level vdevs, and vdev_get_stats() will
2814 * aggregate them when asked. This reduces contention on
2815 * the root vdev_stat_lock and implicitly handles blocks
2816 * that compress away to holes, for which there is no i/o.
2817 * (Holes never create vdev children, so all the counters
2818 * remain zero, which is what we want.)
2820 * Note: this only applies to successful i/o (io_error == 0)
2821 * because unlike i/o counts, errors are not additive.
2822 * When reading a ditto block, for example, failure of
2823 * one top-level vdev does not imply a root-level error.
2825 if (vd == rvd)
2826 return;
2828 ASSERT(vd == zio->io_vd);
2830 if (flags & ZIO_FLAG_IO_BYPASS)
2831 return;
2833 mutex_enter(&vd->vdev_stat_lock);
2835 if (flags & ZIO_FLAG_IO_REPAIR) {
2836 if (flags & ZIO_FLAG_SCAN_THREAD) {
2837 dsl_scan_phys_t *scn_phys =
2838 &spa->spa_dsl_pool->dp_scan->scn_phys;
2839 uint64_t *processed = &scn_phys->scn_processed;
2841 /* XXX cleanup? */
2842 if (vd->vdev_ops->vdev_op_leaf)
2843 atomic_add_64(processed, psize);
2844 vs->vs_scan_processed += psize;
2847 if (flags & ZIO_FLAG_SELF_HEAL)
2848 vs->vs_self_healed += psize;
2851 vs->vs_ops[type]++;
2852 vs->vs_bytes[type] += psize;
2854 mutex_exit(&vd->vdev_stat_lock);
2855 return;
2858 if (flags & ZIO_FLAG_SPECULATIVE)
2859 return;
2862 * If this is an I/O error that is going to be retried, then ignore the
2863 * error. Otherwise, the user may interpret B_FAILFAST I/O errors as
2864 * hard errors, when in reality they can happen for any number of
2865 * innocuous reasons (bus resets, MPxIO link failure, etc).
2867 if (zio->io_error == EIO &&
2868 !(zio->io_flags & ZIO_FLAG_IO_RETRY))
2869 return;
2872 * Intent logs writes won't propagate their error to the root
2873 * I/O so don't mark these types of failures as pool-level
2874 * errors.
2876 if (zio->io_vd == NULL && (zio->io_flags & ZIO_FLAG_DONT_PROPAGATE))
2877 return;
2879 mutex_enter(&vd->vdev_stat_lock);
2880 if (type == ZIO_TYPE_READ && !vdev_is_dead(vd)) {
2881 if (zio->io_error == ECKSUM)
2882 vs->vs_checksum_errors++;
2883 else
2884 vs->vs_read_errors++;
2886 if (type == ZIO_TYPE_WRITE && !vdev_is_dead(vd))
2887 vs->vs_write_errors++;
2888 mutex_exit(&vd->vdev_stat_lock);
2890 if (type == ZIO_TYPE_WRITE && txg != 0 &&
2891 (!(flags & ZIO_FLAG_IO_REPAIR) ||
2892 (flags & ZIO_FLAG_SCAN_THREAD) ||
2893 spa->spa_claiming)) {
2895 * This is either a normal write (not a repair), or it's
2896 * a repair induced by the scrub thread, or it's a repair
2897 * made by zil_claim() during spa_load() in the first txg.
2898 * In the normal case, we commit the DTL change in the same
2899 * txg as the block was born. In the scrub-induced repair
2900 * case, we know that scrubs run in first-pass syncing context,
2901 * so we commit the DTL change in spa_syncing_txg(spa).
2902 * In the zil_claim() case, we commit in spa_first_txg(spa).
2904 * We currently do not make DTL entries for failed spontaneous
2905 * self-healing writes triggered by normal (non-scrubbing)
2906 * reads, because we have no transactional context in which to
2907 * do so -- and it's not clear that it'd be desirable anyway.
2909 if (vd->vdev_ops->vdev_op_leaf) {
2910 uint64_t commit_txg = txg;
2911 if (flags & ZIO_FLAG_SCAN_THREAD) {
2912 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2913 ASSERT(spa_sync_pass(spa) == 1);
2914 vdev_dtl_dirty(vd, DTL_SCRUB, txg, 1);
2915 commit_txg = spa_syncing_txg(spa);
2916 } else if (spa->spa_claiming) {
2917 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2918 commit_txg = spa_first_txg(spa);
2920 ASSERT(commit_txg >= spa_syncing_txg(spa));
2921 if (vdev_dtl_contains(vd, DTL_MISSING, txg, 1))
2922 return;
2923 for (pvd = vd; pvd != rvd; pvd = pvd->vdev_parent)
2924 vdev_dtl_dirty(pvd, DTL_PARTIAL, txg, 1);
2925 vdev_dirty(vd->vdev_top, VDD_DTL, vd, commit_txg);
2927 if (vd != rvd)
2928 vdev_dtl_dirty(vd, DTL_MISSING, txg, 1);
2933 * Update the in-core space usage stats for this vdev, its metaslab class,
2934 * and the root vdev.
2936 void
2937 vdev_space_update(vdev_t *vd, int64_t alloc_delta, int64_t defer_delta,
2938 int64_t space_delta)
2940 int64_t dspace_delta = space_delta;
2941 spa_t *spa = vd->vdev_spa;
2942 vdev_t *rvd = spa->spa_root_vdev;
2943 metaslab_group_t *mg = vd->vdev_mg;
2944 metaslab_class_t *mc = mg ? mg->mg_class : NULL;
2946 ASSERT(vd == vd->vdev_top);
2949 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2950 * factor. We must calculate this here and not at the root vdev
2951 * because the root vdev's psize-to-asize is simply the max of its
2952 * childrens', thus not accurate enough for us.
2954 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2955 ASSERT(vd->vdev_deflate_ratio != 0 || vd->vdev_isl2cache);
2956 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2957 vd->vdev_deflate_ratio;
2959 mutex_enter(&vd->vdev_stat_lock);
2960 vd->vdev_stat.vs_alloc += alloc_delta;
2961 vd->vdev_stat.vs_space += space_delta;
2962 vd->vdev_stat.vs_dspace += dspace_delta;
2963 mutex_exit(&vd->vdev_stat_lock);
2965 if (mc == spa_normal_class(spa)) {
2966 mutex_enter(&rvd->vdev_stat_lock);
2967 rvd->vdev_stat.vs_alloc += alloc_delta;
2968 rvd->vdev_stat.vs_space += space_delta;
2969 rvd->vdev_stat.vs_dspace += dspace_delta;
2970 mutex_exit(&rvd->vdev_stat_lock);
2973 if (mc != NULL) {
2974 ASSERT(rvd == vd->vdev_parent);
2975 ASSERT(vd->vdev_ms_count != 0);
2977 metaslab_class_space_update(mc,
2978 alloc_delta, defer_delta, space_delta, dspace_delta);
2983 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2984 * so that it will be written out next time the vdev configuration is synced.
2985 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2987 void
2988 vdev_config_dirty(vdev_t *vd)
2990 spa_t *spa = vd->vdev_spa;
2991 vdev_t *rvd = spa->spa_root_vdev;
2992 int c;
2994 ASSERT(spa_writeable(spa));
2997 * If this is an aux vdev (as with l2cache and spare devices), then we
2998 * update the vdev config manually and set the sync flag.
3000 if (vd->vdev_aux != NULL) {
3001 spa_aux_vdev_t *sav = vd->vdev_aux;
3002 nvlist_t **aux;
3003 uint_t naux;
3005 for (c = 0; c < sav->sav_count; c++) {
3006 if (sav->sav_vdevs[c] == vd)
3007 break;
3010 if (c == sav->sav_count) {
3012 * We're being removed. There's nothing more to do.
3014 ASSERT(sav->sav_sync == B_TRUE);
3015 return;
3018 sav->sav_sync = B_TRUE;
3020 if (nvlist_lookup_nvlist_array(sav->sav_config,
3021 ZPOOL_CONFIG_L2CACHE, &aux, &naux) != 0) {
3022 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
3023 ZPOOL_CONFIG_SPARES, &aux, &naux) == 0);
3026 ASSERT(c < naux);
3029 * Setting the nvlist in the middle if the array is a little
3030 * sketchy, but it will work.
3032 nvlist_free(aux[c]);
3033 aux[c] = vdev_config_generate(spa, vd, B_TRUE, 0);
3035 return;
3039 * The dirty list is protected by the SCL_CONFIG lock. The caller
3040 * must either hold SCL_CONFIG as writer, or must be the sync thread
3041 * (which holds SCL_CONFIG as reader). There's only one sync thread,
3042 * so this is sufficient to ensure mutual exclusion.
3044 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3045 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3046 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3048 if (vd == rvd) {
3049 for (c = 0; c < rvd->vdev_children; c++)
3050 vdev_config_dirty(rvd->vdev_child[c]);
3051 } else {
3052 ASSERT(vd == vd->vdev_top);
3054 if (!list_link_active(&vd->vdev_config_dirty_node) &&
3055 !vd->vdev_ishole)
3056 list_insert_head(&spa->spa_config_dirty_list, vd);
3060 void
3061 vdev_config_clean(vdev_t *vd)
3063 spa_t *spa = vd->vdev_spa;
3065 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
3066 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3067 spa_config_held(spa, SCL_CONFIG, RW_READER)));
3069 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
3070 list_remove(&spa->spa_config_dirty_list, vd);
3074 * Mark a top-level vdev's state as dirty, so that the next pass of
3075 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
3076 * the state changes from larger config changes because they require
3077 * much less locking, and are often needed for administrative actions.
3079 void
3080 vdev_state_dirty(vdev_t *vd)
3082 spa_t *spa = vd->vdev_spa;
3084 ASSERT(spa_writeable(spa));
3085 ASSERT(vd == vd->vdev_top);
3088 * The state list is protected by the SCL_STATE lock. The caller
3089 * must either hold SCL_STATE as writer, or must be the sync thread
3090 * (which holds SCL_STATE as reader). There's only one sync thread,
3091 * so this is sufficient to ensure mutual exclusion.
3093 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3094 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3095 spa_config_held(spa, SCL_STATE, RW_READER)));
3097 if (!list_link_active(&vd->vdev_state_dirty_node) && !vd->vdev_ishole)
3098 list_insert_head(&spa->spa_state_dirty_list, vd);
3101 void
3102 vdev_state_clean(vdev_t *vd)
3104 spa_t *spa = vd->vdev_spa;
3106 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
3107 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
3108 spa_config_held(spa, SCL_STATE, RW_READER)));
3110 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
3111 list_remove(&spa->spa_state_dirty_list, vd);
3115 * Propagate vdev state up from children to parent.
3117 void
3118 vdev_propagate_state(vdev_t *vd)
3120 spa_t *spa = vd->vdev_spa;
3121 vdev_t *rvd = spa->spa_root_vdev;
3122 int degraded = 0, faulted = 0;
3123 int corrupted = 0;
3124 vdev_t *child;
3126 if (vd->vdev_children > 0) {
3127 for (int c = 0; c < vd->vdev_children; c++) {
3128 child = vd->vdev_child[c];
3131 * Don't factor holes into the decision.
3133 if (child->vdev_ishole)
3134 continue;
3136 if (!vdev_readable(child) ||
3137 (!vdev_writeable(child) && spa_writeable(spa))) {
3139 * Root special: if there is a top-level log
3140 * device, treat the root vdev as if it were
3141 * degraded.
3143 if (child->vdev_islog && vd == rvd)
3144 degraded++;
3145 else
3146 faulted++;
3147 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
3148 degraded++;
3151 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
3152 corrupted++;
3155 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
3158 * Root special: if there is a top-level vdev that cannot be
3159 * opened due to corrupted metadata, then propagate the root
3160 * vdev's aux state as 'corrupt' rather than 'insufficient
3161 * replicas'.
3163 if (corrupted && vd == rvd &&
3164 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
3165 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
3166 VDEV_AUX_CORRUPT_DATA);
3169 if (vd->vdev_parent)
3170 vdev_propagate_state(vd->vdev_parent);
3174 * Set a vdev's state. If this is during an open, we don't update the parent
3175 * state, because we're in the process of opening children depth-first.
3176 * Otherwise, we propagate the change to the parent.
3178 * If this routine places a device in a faulted state, an appropriate ereport is
3179 * generated.
3181 void
3182 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
3184 uint64_t save_state;
3185 spa_t *spa = vd->vdev_spa;
3187 if (state == vd->vdev_state) {
3188 vd->vdev_stat.vs_aux = aux;
3189 return;
3192 save_state = vd->vdev_state;
3194 vd->vdev_state = state;
3195 vd->vdev_stat.vs_aux = aux;
3198 * If we are setting the vdev state to anything but an open state, then
3199 * always close the underlying device unless the device has requested
3200 * a delayed close (i.e. we're about to remove or fault the device).
3201 * Otherwise, we keep accessible but invalid devices open forever.
3202 * We don't call vdev_close() itself, because that implies some extra
3203 * checks (offline, etc) that we don't want here. This is limited to
3204 * leaf devices, because otherwise closing the device will affect other
3205 * children.
3207 if (!vd->vdev_delayed_close && vdev_is_dead(vd) &&
3208 vd->vdev_ops->vdev_op_leaf)
3209 vd->vdev_ops->vdev_op_close(vd);
3212 * If we have brought this vdev back into service, we need
3213 * to notify fmd so that it can gracefully repair any outstanding
3214 * cases due to a missing device. We do this in all cases, even those
3215 * that probably don't correlate to a repaired fault. This is sure to
3216 * catch all cases, and we let the zfs-retire agent sort it out. If
3217 * this is a transient state it's OK, as the retire agent will
3218 * double-check the state of the vdev before repairing it.
3220 if (state == VDEV_STATE_HEALTHY && vd->vdev_ops->vdev_op_leaf &&
3221 vd->vdev_prevstate != state)
3222 zfs_post_state_change(spa, vd);
3224 if (vd->vdev_removed &&
3225 state == VDEV_STATE_CANT_OPEN &&
3226 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
3228 * If the previous state is set to VDEV_STATE_REMOVED, then this
3229 * device was previously marked removed and someone attempted to
3230 * reopen it. If this failed due to a nonexistent device, then
3231 * keep the device in the REMOVED state. We also let this be if
3232 * it is one of our special test online cases, which is only
3233 * attempting to online the device and shouldn't generate an FMA
3234 * fault.
3236 vd->vdev_state = VDEV_STATE_REMOVED;
3237 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
3238 } else if (state == VDEV_STATE_REMOVED) {
3239 vd->vdev_removed = B_TRUE;
3240 } else if (state == VDEV_STATE_CANT_OPEN) {
3242 * If we fail to open a vdev during an import or recovery, we
3243 * mark it as "not available", which signifies that it was
3244 * never there to begin with. Failure to open such a device
3245 * is not considered an error.
3247 if ((spa_load_state(spa) == SPA_LOAD_IMPORT ||
3248 spa_load_state(spa) == SPA_LOAD_RECOVER) &&
3249 vd->vdev_ops->vdev_op_leaf)
3250 vd->vdev_not_present = 1;
3253 * Post the appropriate ereport. If the 'prevstate' field is
3254 * set to something other than VDEV_STATE_UNKNOWN, it indicates
3255 * that this is part of a vdev_reopen(). In this case, we don't
3256 * want to post the ereport if the device was already in the
3257 * CANT_OPEN state beforehand.
3259 * If the 'checkremove' flag is set, then this is an attempt to
3260 * online the device in response to an insertion event. If we
3261 * hit this case, then we have detected an insertion event for a
3262 * faulted or offline device that wasn't in the removed state.
3263 * In this scenario, we don't post an ereport because we are
3264 * about to replace the device, or attempt an online with
3265 * vdev_forcefault, which will generate the fault for us.
3267 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
3268 !vd->vdev_not_present && !vd->vdev_checkremove &&
3269 vd != spa->spa_root_vdev) {
3270 const char *class;
3272 switch (aux) {
3273 case VDEV_AUX_OPEN_FAILED:
3274 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
3275 break;
3276 case VDEV_AUX_CORRUPT_DATA:
3277 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
3278 break;
3279 case VDEV_AUX_NO_REPLICAS:
3280 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
3281 break;
3282 case VDEV_AUX_BAD_GUID_SUM:
3283 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
3284 break;
3285 case VDEV_AUX_TOO_SMALL:
3286 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
3287 break;
3288 case VDEV_AUX_BAD_LABEL:
3289 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
3290 break;
3291 default:
3292 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
3295 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
3298 /* Erase any notion of persistent removed state */
3299 vd->vdev_removed = B_FALSE;
3300 } else {
3301 vd->vdev_removed = B_FALSE;
3304 if (!isopen && vd->vdev_parent)
3305 vdev_propagate_state(vd->vdev_parent);
3309 * Check the vdev configuration to ensure that it's capable of supporting
3310 * a root pool. We do not support partial configuration.
3311 * In addition, only a single top-level vdev is allowed.
3313 boolean_t
3314 vdev_is_bootable(vdev_t *vd)
3316 if (!vd->vdev_ops->vdev_op_leaf) {
3317 char *vdev_type = vd->vdev_ops->vdev_op_type;
3319 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
3320 vd->vdev_children > 1) {
3321 return (B_FALSE);
3322 } else if (strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
3323 return (B_FALSE);
3327 for (int c = 0; c < vd->vdev_children; c++) {
3328 if (!vdev_is_bootable(vd->vdev_child[c]))
3329 return (B_FALSE);
3331 return (B_TRUE);
3335 * Load the state from the original vdev tree (ovd) which
3336 * we've retrieved from the MOS config object. If the original
3337 * vdev was offline or faulted then we transfer that state to the
3338 * device in the current vdev tree (nvd).
3340 void
3341 vdev_load_log_state(vdev_t *nvd, vdev_t *ovd)
3343 spa_t *spa = nvd->vdev_spa;
3345 ASSERT(nvd->vdev_top->vdev_islog);
3346 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
3347 ASSERT3U(nvd->vdev_guid, ==, ovd->vdev_guid);
3349 for (int c = 0; c < nvd->vdev_children; c++)
3350 vdev_load_log_state(nvd->vdev_child[c], ovd->vdev_child[c]);
3352 if (nvd->vdev_ops->vdev_op_leaf) {
3354 * Restore the persistent vdev state
3356 nvd->vdev_offline = ovd->vdev_offline;
3357 nvd->vdev_faulted = ovd->vdev_faulted;
3358 nvd->vdev_degraded = ovd->vdev_degraded;
3359 nvd->vdev_removed = ovd->vdev_removed;
3364 * Determine if a log device has valid content. If the vdev was
3365 * removed or faulted in the MOS config then we know that
3366 * the content on the log device has already been written to the pool.
3368 boolean_t
3369 vdev_log_state_valid(vdev_t *vd)
3371 if (vd->vdev_ops->vdev_op_leaf && !vd->vdev_faulted &&
3372 !vd->vdev_removed)
3373 return (B_TRUE);
3375 for (int c = 0; c < vd->vdev_children; c++)
3376 if (vdev_log_state_valid(vd->vdev_child[c]))
3377 return (B_TRUE);
3379 return (B_FALSE);
3383 * Expand a vdev if possible.
3385 void
3386 vdev_expand(vdev_t *vd, uint64_t txg)
3388 ASSERT(vd->vdev_top == vd);
3389 ASSERT(spa_config_held(vd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3391 if ((vd->vdev_asize >> vd->vdev_ms_shift) > vd->vdev_ms_count) {
3392 VERIFY(vdev_metaslab_init(vd, txg) == 0);
3393 vdev_config_dirty(vd);
3398 * Split a vdev.
3400 void
3401 vdev_split(vdev_t *vd)
3403 vdev_t *cvd, *pvd = vd->vdev_parent;
3405 vdev_remove_child(pvd, vd);
3406 vdev_compact_children(pvd);
3408 cvd = pvd->vdev_child[0];
3409 if (pvd->vdev_children == 1) {
3410 vdev_remove_parent(cvd);
3411 cvd->vdev_splitting = B_TRUE;
3413 vdev_propagate_state(cvd);
3416 void
3417 vdev_deadman(vdev_t *vd)
3419 for (int c = 0; c < vd->vdev_children; c++) {
3420 vdev_t *cvd = vd->vdev_child[c];
3422 vdev_deadman(cvd);
3425 if (vd->vdev_ops->vdev_op_leaf) {
3426 vdev_queue_t *vq = &vd->vdev_queue;
3428 mutex_enter(&vq->vq_lock);
3429 if (avl_numnodes(&vq->vq_active_tree) > 0) {
3430 spa_t *spa = vd->vdev_spa;
3431 zio_t *fio;
3432 uint64_t delta;
3435 * Look at the head of all the pending queues,
3436 * if any I/O has been outstanding for longer than
3437 * the spa_deadman_synctime we panic the system.
3439 fio = avl_first(&vq->vq_active_tree);
3440 delta = gethrtime() - fio->io_timestamp;
3441 if (delta > spa_deadman_synctime(spa)) {
3442 zfs_dbgmsg("SLOW IO: zio timestamp %lluns, "
3443 "delta %lluns, last io %lluns",
3444 fio->io_timestamp, delta,
3445 vq->vq_io_complete_ts);
3446 fm_panic("I/O to pool '%s' appears to be "
3447 "hung.", spa_name(spa));
3450 mutex_exit(&vq->vq_lock);