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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)vdev.c 1.33 07/11/27 SMI"
29 #include <sys/zfs_context.h>
30 #include <sys/fm/fs/zfs.h>
32 #include <sys/spa_impl.h>
34 #include <sys/dmu_tx.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/uberblock_impl.h>
37 #include <sys/metaslab.h>
38 #include <sys/metaslab_impl.h>
39 #include <sys/space_map.h>
42 #include <sys/fs/zfs.h>
45 * Virtual device management.
48 static vdev_ops_t *vdev_ops_table[] = {
60 /* maximum scrub/resilver I/O queue */
61 int zfs_scrub_limit = 70;
64 * Given a vdev type, return the appropriate ops vector.
67 vdev_getops(const char *type)
69 vdev_ops_t *ops, **opspp;
71 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
72 if (strcmp(ops->vdev_op_type, type) == 0)
79 * Default asize function: return the MAX of psize with the asize of
80 * all children. This is what's used by anything other than RAID-Z.
83 vdev_default_asize(vdev_t *vd, uint64_t psize)
85 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
89 for (c = 0; c < vd->vdev_children; c++) {
90 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
91 asize = MAX(asize, csize);
98 * Get the replaceable or attachable device size.
99 * If the parent is a mirror or raidz, the replaceable size is the minimum
100 * psize of all its children. For the rest, just return our own psize.
111 vdev_get_rsize(vdev_t *vd)
116 pvd = vd->vdev_parent;
119 * If our parent is NULL or the root, just return our own psize.
121 if (pvd == NULL || pvd->vdev_parent == NULL)
122 return (vd->vdev_psize);
126 for (c = 0; c < pvd->vdev_children; c++) {
127 cvd = pvd->vdev_child[c];
128 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
135 vdev_lookup_top(spa_t *spa, uint64_t vdev)
137 vdev_t *rvd = spa->spa_root_vdev;
139 ASSERT(spa_config_held(spa, RW_READER) ||
140 curthread == spa->spa_scrub_thread);
142 if (vdev < rvd->vdev_children)
143 return (rvd->vdev_child[vdev]);
149 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
154 if (vd->vdev_guid == guid)
157 for (c = 0; c < vd->vdev_children; c++)
158 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
166 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
168 size_t oldsize, newsize;
169 uint64_t id = cvd->vdev_id;
172 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
173 ASSERT(cvd->vdev_parent == NULL);
175 cvd->vdev_parent = pvd;
180 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
182 oldsize = pvd->vdev_children * sizeof (vdev_t *);
183 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
184 newsize = pvd->vdev_children * sizeof (vdev_t *);
186 newchild = kmem_zalloc(newsize, KM_SLEEP);
187 if (pvd->vdev_child != NULL) {
188 bcopy(pvd->vdev_child, newchild, oldsize);
189 kmem_free(pvd->vdev_child, oldsize);
192 pvd->vdev_child = newchild;
193 pvd->vdev_child[id] = cvd;
195 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
196 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
199 * Walk up all ancestors to update guid sum.
201 for (; pvd != NULL; pvd = pvd->vdev_parent)
202 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
204 if (cvd->vdev_ops->vdev_op_leaf)
205 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
209 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
212 uint_t id = cvd->vdev_id;
214 ASSERT(cvd->vdev_parent == pvd);
219 ASSERT(id < pvd->vdev_children);
220 ASSERT(pvd->vdev_child[id] == cvd);
222 pvd->vdev_child[id] = NULL;
223 cvd->vdev_parent = NULL;
225 for (c = 0; c < pvd->vdev_children; c++)
226 if (pvd->vdev_child[c])
229 if (c == pvd->vdev_children) {
230 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
231 pvd->vdev_child = NULL;
232 pvd->vdev_children = 0;
236 * Walk up all ancestors to update guid sum.
238 for (; pvd != NULL; pvd = pvd->vdev_parent)
239 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
241 if (cvd->vdev_ops->vdev_op_leaf)
242 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
246 * Remove any holes in the child array.
249 vdev_compact_children(vdev_t *pvd)
251 vdev_t **newchild, *cvd;
252 int oldc = pvd->vdev_children;
255 ASSERT(spa_config_held(pvd->vdev_spa, RW_WRITER));
257 for (c = newc = 0; c < oldc; c++)
258 if (pvd->vdev_child[c])
261 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
263 for (c = newc = 0; c < oldc; c++) {
264 if ((cvd = pvd->vdev_child[c]) != NULL) {
265 newchild[newc] = cvd;
266 cvd->vdev_id = newc++;
270 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
271 pvd->vdev_child = newchild;
272 pvd->vdev_children = newc;
276 * Allocate and minimally initialize a vdev_t.
279 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
283 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
285 if (spa->spa_root_vdev == NULL) {
286 ASSERT(ops == &vdev_root_ops);
287 spa->spa_root_vdev = vd;
291 if (spa->spa_root_vdev == vd) {
293 * The root vdev's guid will also be the pool guid,
294 * which must be unique among all pools.
296 while (guid == 0 || spa_guid_exists(guid, 0))
297 guid = spa_get_random(-1ULL);
300 * Any other vdev's guid must be unique within the pool.
303 spa_guid_exists(spa_guid(spa), guid))
304 guid = spa_get_random(-1ULL);
306 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
311 vd->vdev_guid = guid;
312 vd->vdev_guid_sum = guid;
314 vd->vdev_state = VDEV_STATE_CLOSED;
316 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
317 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
318 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
319 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
320 txg_list_create(&vd->vdev_ms_list,
321 offsetof(struct metaslab, ms_txg_node));
322 txg_list_create(&vd->vdev_dtl_list,
323 offsetof(struct vdev, vdev_dtl_node));
324 vd->vdev_stat.vs_timestamp = gethrtime();
332 * Allocate a new vdev. The 'alloctype' is used to control whether we are
333 * creating a new vdev or loading an existing one - the behavior is slightly
334 * different for each case.
337 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
342 uint64_t guid = 0, islog, nparity;
345 ASSERT(spa_config_held(spa, RW_WRITER));
347 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
350 if ((ops = vdev_getops(type)) == NULL)
354 * If this is a load, get the vdev guid from the nvlist.
355 * Otherwise, vdev_alloc_common() will generate one for us.
357 if (alloctype == VDEV_ALLOC_LOAD) {
360 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
364 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
366 } else if (alloctype == VDEV_ALLOC_SPARE) {
367 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
369 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
370 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
375 * The first allocated vdev must be of type 'root'.
377 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
381 * Determine whether we're a log vdev.
384 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
385 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
389 * Set the nparity property for RAID-Z vdevs.
392 if (ops == &vdev_raidz_ops) {
393 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
396 * Currently, we can only support 2 parity devices.
398 if (nparity == 0 || nparity > 2)
401 * Older versions can only support 1 parity device.
404 spa_version(spa) < SPA_VERSION_RAID6)
408 * We require the parity to be specified for SPAs that
409 * support multiple parity levels.
411 if (spa_version(spa) >= SPA_VERSION_RAID6)
414 * Otherwise, we default to 1 parity device for RAID-Z.
421 ASSERT(nparity != -1ULL);
423 vd = vdev_alloc_common(spa, id, guid, ops);
425 vd->vdev_islog = islog;
426 vd->vdev_nparity = nparity;
428 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
429 vd->vdev_path = spa_strdup(vd->vdev_path);
430 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
431 vd->vdev_devid = spa_strdup(vd->vdev_devid);
432 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
433 &vd->vdev_physpath) == 0)
434 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
437 * Set the whole_disk property. If it's not specified, leave the value
440 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
441 &vd->vdev_wholedisk) != 0)
442 vd->vdev_wholedisk = -1ULL;
445 * Look for the 'not present' flag. This will only be set if the device
446 * was not present at the time of import.
448 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
449 &vd->vdev_not_present);
452 * Get the alignment requirement.
454 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
457 * If we're a top-level vdev, try to load the allocation parameters.
459 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
460 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
462 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
464 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
469 * If we're a leaf vdev, try to load the DTL object and other state.
471 if (vd->vdev_ops->vdev_op_leaf && alloctype == VDEV_ALLOC_LOAD) {
472 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
473 &vd->vdev_dtl.smo_object);
474 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
476 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
479 * When importing a pool, we want to ignore the persistent fault
480 * state, as the diagnosis made on another system may not be
481 * valid in the current context.
483 if (spa->spa_load_state == SPA_LOAD_OPEN) {
484 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
486 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
488 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
494 * Add ourselves to the parent's list of children.
496 vdev_add_child(parent, vd);
504 vdev_free(vdev_t *vd)
507 spa_t *spa = vd->vdev_spa;
510 * vdev_free() implies closing the vdev first. This is simpler than
511 * trying to ensure complicated semantics for all callers.
516 ASSERT(!list_link_active(&vd->vdev_dirty_node));
521 for (c = 0; c < vd->vdev_children; c++)
522 vdev_free(vd->vdev_child[c]);
524 ASSERT(vd->vdev_child == NULL);
525 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
528 * Discard allocation state.
530 if (vd == vd->vdev_top)
531 vdev_metaslab_fini(vd);
533 ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
534 ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
535 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
538 * Remove this vdev from its parent's child list.
540 vdev_remove_child(vd->vdev_parent, vd);
542 ASSERT(vd->vdev_parent == NULL);
545 * Clean up vdev structure.
551 spa_strfree(vd->vdev_path);
553 spa_strfree(vd->vdev_devid);
554 if (vd->vdev_physpath)
555 spa_strfree(vd->vdev_physpath);
557 if (vd->vdev_isspare)
558 spa_spare_remove(vd);
559 if (vd->vdev_isl2cache)
560 spa_l2cache_remove(vd);
562 txg_list_destroy(&vd->vdev_ms_list);
563 txg_list_destroy(&vd->vdev_dtl_list);
564 mutex_enter(&vd->vdev_dtl_lock);
565 space_map_unload(&vd->vdev_dtl_map);
566 space_map_destroy(&vd->vdev_dtl_map);
567 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
568 space_map_destroy(&vd->vdev_dtl_scrub);
569 mutex_exit(&vd->vdev_dtl_lock);
570 mutex_destroy(&vd->vdev_dtl_lock);
571 mutex_destroy(&vd->vdev_stat_lock);
573 if (vd == spa->spa_root_vdev)
574 spa->spa_root_vdev = NULL;
576 kmem_free(vd, sizeof (vdev_t));
580 * Transfer top-level vdev state from svd to tvd.
583 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
585 spa_t *spa = svd->vdev_spa;
590 ASSERT(tvd == tvd->vdev_top);
592 tvd->vdev_ms_array = svd->vdev_ms_array;
593 tvd->vdev_ms_shift = svd->vdev_ms_shift;
594 tvd->vdev_ms_count = svd->vdev_ms_count;
596 svd->vdev_ms_array = 0;
597 svd->vdev_ms_shift = 0;
598 svd->vdev_ms_count = 0;
600 tvd->vdev_mg = svd->vdev_mg;
601 tvd->vdev_ms = svd->vdev_ms;
606 if (tvd->vdev_mg != NULL)
607 tvd->vdev_mg->mg_vd = tvd;
609 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
610 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
611 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
613 svd->vdev_stat.vs_alloc = 0;
614 svd->vdev_stat.vs_space = 0;
615 svd->vdev_stat.vs_dspace = 0;
617 for (t = 0; t < TXG_SIZE; t++) {
618 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
619 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
620 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
621 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
622 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
623 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
626 if (list_link_active(&svd->vdev_dirty_node)) {
627 vdev_config_clean(svd);
628 vdev_config_dirty(tvd);
631 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
632 svd->vdev_deflate_ratio = 0;
634 tvd->vdev_islog = svd->vdev_islog;
639 vdev_top_update(vdev_t *tvd, vdev_t *vd)
648 for (c = 0; c < vd->vdev_children; c++)
649 vdev_top_update(tvd, vd->vdev_child[c]);
653 * Add a mirror/replacing vdev above an existing vdev.
656 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
658 spa_t *spa = cvd->vdev_spa;
659 vdev_t *pvd = cvd->vdev_parent;
662 ASSERT(spa_config_held(spa, RW_WRITER));
664 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
666 mvd->vdev_asize = cvd->vdev_asize;
667 mvd->vdev_ashift = cvd->vdev_ashift;
668 mvd->vdev_state = cvd->vdev_state;
670 vdev_remove_child(pvd, cvd);
671 vdev_add_child(pvd, mvd);
672 cvd->vdev_id = mvd->vdev_children;
673 vdev_add_child(mvd, cvd);
674 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
676 if (mvd == mvd->vdev_top)
677 vdev_top_transfer(cvd, mvd);
683 * Remove a 1-way mirror/replacing vdev from the tree.
686 vdev_remove_parent(vdev_t *cvd)
688 vdev_t *mvd = cvd->vdev_parent;
689 vdev_t *pvd = mvd->vdev_parent;
691 ASSERT(spa_config_held(cvd->vdev_spa, RW_WRITER));
693 ASSERT(mvd->vdev_children == 1);
694 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
695 mvd->vdev_ops == &vdev_replacing_ops ||
696 mvd->vdev_ops == &vdev_spare_ops);
697 cvd->vdev_ashift = mvd->vdev_ashift;
699 vdev_remove_child(mvd, cvd);
700 vdev_remove_child(pvd, mvd);
701 cvd->vdev_id = mvd->vdev_id;
702 vdev_add_child(pvd, cvd);
704 * If we created a new toplevel vdev, then we need to change the child's
705 * vdev GUID to match the old toplevel vdev. Otherwise, we could have
706 * detached an offline device, and when we go to import the pool we'll
707 * think we have two toplevel vdevs, instead of a different version of
708 * the same toplevel vdev.
710 if (cvd->vdev_top == cvd) {
711 pvd->vdev_guid_sum -= cvd->vdev_guid;
712 cvd->vdev_guid_sum -= cvd->vdev_guid;
713 cvd->vdev_guid = mvd->vdev_guid;
714 cvd->vdev_guid_sum += mvd->vdev_guid;
715 pvd->vdev_guid_sum += cvd->vdev_guid;
717 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
719 if (cvd == cvd->vdev_top)
720 vdev_top_transfer(mvd, cvd);
722 ASSERT(mvd->vdev_children == 0);
727 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
729 spa_t *spa = vd->vdev_spa;
730 objset_t *mos = spa->spa_meta_objset;
731 metaslab_class_t *mc;
733 uint64_t oldc = vd->vdev_ms_count;
734 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
738 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */
741 dprintf("%s oldc %llu newc %llu\n", vdev_description(vd), oldc, newc);
743 ASSERT(oldc <= newc);
746 mc = spa->spa_log_class;
748 mc = spa->spa_normal_class;
750 if (vd->vdev_mg == NULL)
751 vd->vdev_mg = metaslab_group_create(mc, vd);
753 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
756 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
757 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
761 vd->vdev_ms_count = newc;
763 for (m = oldc; m < newc; m++) {
764 space_map_obj_t smo = { 0, 0, 0 };
767 error = dmu_read(mos, vd->vdev_ms_array,
768 m * sizeof (uint64_t), sizeof (uint64_t), &object);
773 error = dmu_bonus_hold(mos, object, FTAG, &db);
776 ASSERT3U(db->db_size, >=, sizeof (smo));
777 bcopy(db->db_data, &smo, sizeof (smo));
778 ASSERT3U(smo.smo_object, ==, object);
779 dmu_buf_rele(db, FTAG);
782 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
783 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
790 vdev_metaslab_fini(vdev_t *vd)
793 uint64_t count = vd->vdev_ms_count;
795 if (vd->vdev_ms != NULL) {
796 for (m = 0; m < count; m++)
797 if (vd->vdev_ms[m] != NULL)
798 metaslab_fini(vd->vdev_ms[m]);
799 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
805 vdev_probe(vdev_t *vd)
811 * Right now we only support status checks on the leaf vdevs.
813 if (vd->vdev_ops->vdev_op_leaf)
814 return (vd->vdev_ops->vdev_op_probe(vd));
820 * Prepare a virtual device for access.
823 vdev_open(vdev_t *vd)
828 uint64_t asize, psize;
831 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
832 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
833 vd->vdev_state == VDEV_STATE_OFFLINE);
835 if (vd->vdev_fault_mode == VDEV_FAULT_COUNT)
836 vd->vdev_fault_arg >>= 1;
838 vd->vdev_fault_mode = VDEV_FAULT_NONE;
840 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
842 if (!vd->vdev_removed && vd->vdev_faulted) {
843 ASSERT(vd->vdev_children == 0);
844 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
845 VDEV_AUX_ERR_EXCEEDED);
847 } else if (vd->vdev_offline) {
848 ASSERT(vd->vdev_children == 0);
849 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
853 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
855 if (zio_injection_enabled && error == 0)
856 error = zio_handle_device_injection(vd, ENXIO);
859 if (vd->vdev_removed &&
860 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
861 vd->vdev_removed = B_FALSE;
863 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
864 vd->vdev_stat.vs_aux);
868 vd->vdev_removed = B_FALSE;
870 if (vd->vdev_degraded) {
871 ASSERT(vd->vdev_children == 0);
872 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
873 VDEV_AUX_ERR_EXCEEDED);
875 vd->vdev_state = VDEV_STATE_HEALTHY;
878 for (c = 0; c < vd->vdev_children; c++)
879 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
880 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
885 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
887 if (vd->vdev_children == 0) {
888 if (osize < SPA_MINDEVSIZE) {
889 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
894 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
896 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
897 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
898 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
906 vd->vdev_psize = psize;
908 if (vd->vdev_asize == 0) {
910 * This is the first-ever open, so use the computed values.
911 * For testing purposes, a higher ashift can be requested.
913 vd->vdev_asize = asize;
914 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
917 * Make sure the alignment requirement hasn't increased.
919 if (ashift > vd->vdev_top->vdev_ashift) {
920 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
926 * Make sure the device hasn't shrunk.
928 if (asize < vd->vdev_asize) {
929 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
935 * If all children are healthy and the asize has increased,
936 * then we've experienced dynamic LUN growth.
938 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
939 asize > vd->vdev_asize) {
940 vd->vdev_asize = asize;
945 * Ensure we can issue some IO before declaring the
946 * vdev open for business.
948 error = vdev_probe(vd);
950 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
951 VDEV_AUX_OPEN_FAILED);
956 * If this is a top-level vdev, compute the raidz-deflation
957 * ratio. Note, we hard-code in 128k (1<<17) because it is the
958 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE
959 * changes, this algorithm must never change, or we will
960 * inconsistently account for existing bp's.
962 if (vd->vdev_top == vd) {
963 vd->vdev_deflate_ratio = (1<<17) /
964 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
968 * This allows the ZFS DE to close cases appropriately. If a device
969 * goes away and later returns, we want to close the associated case.
970 * But it's not enough to simply post this only when a device goes from
971 * CANT_OPEN -> HEALTHY. If we reboot the system and the device is
972 * back, we also need to close the case (otherwise we will try to replay
973 * it). So we have to post this notifier every time. Since this only
974 * occurs during pool open or error recovery, this should not be an
977 zfs_post_ok(vd->vdev_spa, vd);
983 * Called once the vdevs are all opened, this routine validates the label
984 * contents. This needs to be done before vdev_load() so that we don't
985 * inadvertently do repair I/Os to the wrong device.
987 * This function will only return failure if one of the vdevs indicates that it
988 * has since been destroyed or exported. This is only possible if
989 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
990 * will be updated but the function will return 0.
993 vdev_validate(vdev_t *vd)
995 spa_t *spa = vd->vdev_spa;
1001 for (c = 0; c < vd->vdev_children; c++)
1002 if (vdev_validate(vd->vdev_child[c]) != 0)
1006 * If the device has already failed, or was marked offline, don't do
1007 * any further validation. Otherwise, label I/O will fail and we will
1008 * overwrite the previous state.
1010 if (vd->vdev_ops->vdev_op_leaf && !vdev_is_dead(vd)) {
1012 if ((label = vdev_label_read_config(vd)) == NULL) {
1013 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1014 VDEV_AUX_BAD_LABEL);
1018 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1019 &guid) != 0 || guid != spa_guid(spa)) {
1020 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1021 VDEV_AUX_CORRUPT_DATA);
1026 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1027 &guid) != 0 || guid != vd->vdev_guid) {
1028 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1029 VDEV_AUX_CORRUPT_DATA);
1034 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1036 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1037 VDEV_AUX_CORRUPT_DATA);
1044 if (spa->spa_load_state == SPA_LOAD_OPEN &&
1045 state != POOL_STATE_ACTIVE)
1050 * If we were able to open and validate a vdev that was previously
1051 * marked permanently unavailable, clear that state now.
1053 if (vd->vdev_not_present)
1054 vd->vdev_not_present = 0;
1060 * Close a virtual device.
1063 vdev_close(vdev_t *vd)
1065 vd->vdev_ops->vdev_op_close(vd);
1067 vdev_cache_purge(vd);
1070 * We record the previous state before we close it, so that if we are
1071 * doing a reopen(), we don't generate FMA ereports if we notice that
1072 * it's still faulted.
1074 vd->vdev_prevstate = vd->vdev_state;
1076 if (vd->vdev_offline)
1077 vd->vdev_state = VDEV_STATE_OFFLINE;
1079 vd->vdev_state = VDEV_STATE_CLOSED;
1080 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1084 vdev_reopen(vdev_t *vd)
1086 spa_t *spa = vd->vdev_spa;
1088 ASSERT(spa_config_held(spa, RW_WRITER));
1091 (void) vdev_open(vd);
1094 * Call vdev_validate() here to make sure we have the same device.
1095 * Otherwise, a device with an invalid label could be successfully
1096 * opened in response to vdev_reopen().
1098 (void) vdev_validate(vd);
1101 * Reassess parent vdev's health.
1103 vdev_propagate_state(vd);
1107 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1112 * Normally, partial opens (e.g. of a mirror) are allowed.
1113 * For a create, however, we want to fail the request if
1114 * there are any components we can't open.
1116 error = vdev_open(vd);
1118 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1120 return (error ? error : ENXIO);
1124 * Recursively initialize all labels.
1126 if ((error = vdev_label_init(vd, txg, isreplacing ?
1127 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1136 * The is the latter half of vdev_create(). It is distinct because it
1137 * involves initiating transactions in order to do metaslab creation.
1138 * For creation, we want to try to create all vdevs at once and then undo it
1139 * if anything fails; this is much harder if we have pending transactions.
1142 vdev_init(vdev_t *vd, uint64_t txg)
1145 * Aim for roughly 200 metaslabs per vdev.
1147 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1148 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1151 * Initialize the vdev's metaslabs. This can't fail because
1152 * there's nothing to read when creating all new metaslabs.
1154 VERIFY(vdev_metaslab_init(vd, txg) == 0);
1158 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1160 ASSERT(vd == vd->vdev_top);
1161 ASSERT(ISP2(flags));
1163 if (flags & VDD_METASLAB)
1164 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1166 if (flags & VDD_DTL)
1167 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1169 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1173 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1175 mutex_enter(sm->sm_lock);
1176 if (!space_map_contains(sm, txg, size))
1177 space_map_add(sm, txg, size);
1178 mutex_exit(sm->sm_lock);
1182 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1187 * Quick test without the lock -- covers the common case that
1188 * there are no dirty time segments.
1190 if (sm->sm_space == 0)
1193 mutex_enter(sm->sm_lock);
1194 dirty = space_map_contains(sm, txg, size);
1195 mutex_exit(sm->sm_lock);
1201 * Reassess DTLs after a config change or scrub completion.
1204 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1206 spa_t *spa = vd->vdev_spa;
1209 ASSERT(spa_config_held(spa, RW_WRITER));
1211 if (vd->vdev_children == 0) {
1212 mutex_enter(&vd->vdev_dtl_lock);
1214 * We're successfully scrubbed everything up to scrub_txg.
1215 * Therefore, excise all old DTLs up to that point, then
1216 * fold in the DTLs for everything we couldn't scrub.
1218 if (scrub_txg != 0) {
1219 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1220 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1223 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1224 mutex_exit(&vd->vdev_dtl_lock);
1226 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1231 * Make sure the DTLs are always correct under the scrub lock.
1233 if (vd == spa->spa_root_vdev)
1234 mutex_enter(&spa->spa_scrub_lock);
1236 mutex_enter(&vd->vdev_dtl_lock);
1237 space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1238 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1239 mutex_exit(&vd->vdev_dtl_lock);
1241 for (c = 0; c < vd->vdev_children; c++) {
1242 vdev_t *cvd = vd->vdev_child[c];
1243 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1244 mutex_enter(&vd->vdev_dtl_lock);
1245 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1246 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1247 mutex_exit(&vd->vdev_dtl_lock);
1250 if (vd == spa->spa_root_vdev)
1251 mutex_exit(&spa->spa_scrub_lock);
1255 vdev_dtl_load(vdev_t *vd)
1257 spa_t *spa = vd->vdev_spa;
1258 space_map_obj_t *smo = &vd->vdev_dtl;
1259 objset_t *mos = spa->spa_meta_objset;
1263 ASSERT(vd->vdev_children == 0);
1265 if (smo->smo_object == 0)
1268 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1271 ASSERT3U(db->db_size, >=, sizeof (*smo));
1272 bcopy(db->db_data, smo, sizeof (*smo));
1273 dmu_buf_rele(db, FTAG);
1275 mutex_enter(&vd->vdev_dtl_lock);
1276 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1277 mutex_exit(&vd->vdev_dtl_lock);
1283 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1285 spa_t *spa = vd->vdev_spa;
1286 space_map_obj_t *smo = &vd->vdev_dtl;
1287 space_map_t *sm = &vd->vdev_dtl_map;
1288 objset_t *mos = spa->spa_meta_objset;
1294 dprintf("%s in txg %llu pass %d\n",
1295 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1297 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1299 if (vd->vdev_detached) {
1300 if (smo->smo_object != 0) {
1301 int err = dmu_object_free(mos, smo->smo_object, tx);
1302 ASSERT3U(err, ==, 0);
1303 smo->smo_object = 0;
1306 dprintf("detach %s committed in txg %llu\n",
1307 vdev_description(vd), txg);
1311 if (smo->smo_object == 0) {
1312 ASSERT(smo->smo_objsize == 0);
1313 ASSERT(smo->smo_alloc == 0);
1314 smo->smo_object = dmu_object_alloc(mos,
1315 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1316 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1317 ASSERT(smo->smo_object != 0);
1318 vdev_config_dirty(vd->vdev_top);
1321 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1323 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1326 mutex_enter(&smlock);
1328 mutex_enter(&vd->vdev_dtl_lock);
1329 space_map_walk(sm, space_map_add, &smsync);
1330 mutex_exit(&vd->vdev_dtl_lock);
1332 space_map_truncate(smo, mos, tx);
1333 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1335 space_map_destroy(&smsync);
1337 mutex_exit(&smlock);
1338 mutex_destroy(&smlock);
1340 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1341 dmu_buf_will_dirty(db, tx);
1342 ASSERT3U(db->db_size, >=, sizeof (*smo));
1343 bcopy(smo, db->db_data, sizeof (*smo));
1344 dmu_buf_rele(db, FTAG);
1350 vdev_load(vdev_t *vd)
1355 * Recursively load all children.
1357 for (c = 0; c < vd->vdev_children; c++)
1358 vdev_load(vd->vdev_child[c]);
1361 * If this is a top-level vdev, initialize its metaslabs.
1363 if (vd == vd->vdev_top &&
1364 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1365 vdev_metaslab_init(vd, 0) != 0))
1366 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1367 VDEV_AUX_CORRUPT_DATA);
1370 * If this is a leaf vdev, load its DTL.
1372 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1373 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1374 VDEV_AUX_CORRUPT_DATA);
1378 * The special vdev case is used for hot spares and l2cache devices. Its
1379 * sole purpose it to set the vdev state for the associated vdev. To do this,
1380 * we make sure that we can open the underlying device, then try to read the
1381 * label, and make sure that the label is sane and that it hasn't been
1382 * repurposed to another pool.
1385 vdev_validate_aux(vdev_t *vd)
1388 uint64_t guid, version;
1391 if ((label = vdev_label_read_config(vd)) == NULL) {
1392 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1393 VDEV_AUX_CORRUPT_DATA);
1397 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1398 version > SPA_VERSION ||
1399 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1400 guid != vd->vdev_guid ||
1401 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1402 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1403 VDEV_AUX_CORRUPT_DATA);
1409 * We don't actually check the pool state here. If it's in fact in
1410 * use by another pool, we update this fact on the fly when requested.
1417 vdev_sync_done(vdev_t *vd, uint64_t txg)
1421 dprintf("%s txg %llu\n", vdev_description(vd), txg);
1423 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1424 metaslab_sync_done(msp, txg);
1428 vdev_sync(vdev_t *vd, uint64_t txg)
1430 spa_t *spa = vd->vdev_spa;
1435 dprintf("%s txg %llu pass %d\n",
1436 vdev_description(vd), (u_longlong_t)txg, spa_sync_pass(spa));
1438 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1439 ASSERT(vd == vd->vdev_top);
1440 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1441 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1442 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1443 ASSERT(vd->vdev_ms_array != 0);
1444 vdev_config_dirty(vd);
1448 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1449 metaslab_sync(msp, txg);
1450 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1453 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1454 vdev_dtl_sync(lvd, txg);
1456 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1460 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1462 return (vd->vdev_ops->vdev_op_asize(vd, psize));
1466 vdev_description(vdev_t *vd)
1468 if (vd == NULL || vd->vdev_ops == NULL)
1469 return ("<unknown>");
1471 if (vd->vdev_path != NULL)
1472 return (vd->vdev_path);
1474 if (vd->vdev_parent == NULL)
1475 return (spa_name(vd->vdev_spa));
1477 return (vd->vdev_ops->vdev_op_type);
1481 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
1482 * not be opened, and no I/O is attempted.
1485 vdev_fault(spa_t *spa, uint64_t guid)
1491 * Disregard a vdev fault request if the pool has
1492 * experienced a complete failure.
1494 * XXX - We do this here so that we don't hold the
1495 * spa_namespace_lock in the event that we can't get
1496 * the RW_WRITER spa_config_lock.
1498 if (spa_state(spa) == POOL_STATE_IO_FAILURE)
1501 txg = spa_vdev_enter(spa);
1503 rvd = spa->spa_root_vdev;
1505 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1506 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1507 if (!vd->vdev_ops->vdev_op_leaf)
1508 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1511 * Faulted state takes precedence over degraded.
1513 vd->vdev_faulted = 1ULL;
1514 vd->vdev_degraded = 0ULL;
1515 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED,
1516 VDEV_AUX_ERR_EXCEEDED);
1519 * If marking the vdev as faulted cause the toplevel vdev to become
1520 * unavailable, then back off and simply mark the vdev as degraded
1523 if (vdev_is_dead(vd->vdev_top)) {
1524 vd->vdev_degraded = 1ULL;
1525 vd->vdev_faulted = 0ULL;
1528 * If we reopen the device and it's not dead, only then do we
1533 if (vdev_readable(vd)) {
1534 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1535 VDEV_AUX_ERR_EXCEEDED);
1539 vdev_config_dirty(vd->vdev_top);
1541 (void) spa_vdev_exit(spa, NULL, txg, 0);
1547 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
1548 * user that something is wrong. The vdev continues to operate as normal as far
1549 * as I/O is concerned.
1552 vdev_degrade(spa_t *spa, uint64_t guid)
1558 * Disregard a vdev fault request if the pool has
1559 * experienced a complete failure.
1561 * XXX - We do this here so that we don't hold the
1562 * spa_namespace_lock in the event that we can't get
1563 * the RW_WRITER spa_config_lock.
1565 if (spa_state(spa) == POOL_STATE_IO_FAILURE)
1568 txg = spa_vdev_enter(spa);
1570 rvd = spa->spa_root_vdev;
1572 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1573 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1574 if (!vd->vdev_ops->vdev_op_leaf)
1575 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1578 * If the vdev is already faulted, then don't do anything.
1580 if (vd->vdev_faulted || vd->vdev_degraded) {
1581 (void) spa_vdev_exit(spa, NULL, txg, 0);
1585 vd->vdev_degraded = 1ULL;
1586 if (!vdev_is_dead(vd))
1587 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1588 VDEV_AUX_ERR_EXCEEDED);
1589 vdev_config_dirty(vd->vdev_top);
1591 (void) spa_vdev_exit(spa, NULL, txg, 0);
1597 * Online the given vdev. If 'unspare' is set, it implies two things. First,
1598 * any attached spare device should be detached when the device finishes
1599 * resilvering. Second, the online should be treated like a 'test' online case,
1600 * so no FMA events are generated if the device fails to open.
1603 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags,
1604 vdev_state_t *newstate)
1610 * Disregard a vdev fault request if the pool has
1611 * experienced a complete failure.
1613 * XXX - We do this here so that we don't hold the
1614 * spa_namespace_lock in the event that we can't get
1615 * the RW_WRITER spa_config_lock.
1617 if (spa_state(spa) == POOL_STATE_IO_FAILURE)
1620 txg = spa_vdev_enter(spa);
1622 rvd = spa->spa_root_vdev;
1624 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1625 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1627 if (!vd->vdev_ops->vdev_op_leaf)
1628 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1630 vd->vdev_offline = B_FALSE;
1631 vd->vdev_tmpoffline = B_FALSE;
1632 vd->vdev_checkremove = (flags & ZFS_ONLINE_CHECKREMOVE) ?
1634 vd->vdev_forcefault = (flags & ZFS_ONLINE_FORCEFAULT) ?
1636 vdev_reopen(vd->vdev_top);
1637 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1640 *newstate = vd->vdev_state;
1641 if ((flags & ZFS_ONLINE_UNSPARE) &&
1642 !vdev_is_dead(vd) && vd->vdev_parent &&
1643 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
1644 vd->vdev_parent->vdev_child[0] == vd)
1645 vd->vdev_unspare = B_TRUE;
1647 vdev_config_dirty(vd->vdev_top);
1649 (void) spa_vdev_exit(spa, NULL, txg, 0);
1652 * Must hold spa_namespace_lock in order to post resilver sysevent
1655 mutex_enter(&spa_namespace_lock);
1656 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER, B_TRUE) == 0);
1657 mutex_exit(&spa_namespace_lock);
1663 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1669 * Disregard a vdev fault request if the pool has
1670 * experienced a complete failure.
1672 * XXX - We do this here so that we don't hold the
1673 * spa_namespace_lock in the event that we can't get
1674 * the RW_WRITER spa_config_lock.
1676 if (spa_state(spa) == POOL_STATE_IO_FAILURE)
1679 txg = spa_vdev_enter(spa);
1681 rvd = spa->spa_root_vdev;
1683 if ((vd = vdev_lookup_by_guid(rvd, guid)) == NULL)
1684 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
1686 if (!vd->vdev_ops->vdev_op_leaf)
1687 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
1690 * If the device isn't already offline, try to offline it.
1692 if (!vd->vdev_offline) {
1694 * If this device's top-level vdev has a non-empty DTL,
1695 * don't allow the device to be offlined.
1697 * XXX -- make this more precise by allowing the offline
1698 * as long as the remaining devices don't have any DTL holes.
1700 if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1701 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1704 * Offline this device and reopen its top-level vdev.
1705 * If this action results in the top-level vdev becoming
1706 * unusable, undo it and fail the request.
1708 vd->vdev_offline = B_TRUE;
1709 vdev_reopen(vd->vdev_top);
1710 if (vdev_is_dead(vd->vdev_top)) {
1711 vd->vdev_offline = B_FALSE;
1712 vdev_reopen(vd->vdev_top);
1713 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
1717 vd->vdev_tmpoffline = (flags & ZFS_OFFLINE_TEMPORARY) ?
1720 vdev_config_dirty(vd->vdev_top);
1722 return (spa_vdev_exit(spa, NULL, txg, 0));
1726 * Clear the error counts associated with this vdev. Unlike vdev_online() and
1727 * vdev_offline(), we assume the spa config is locked. We also clear all
1728 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
1729 * If reopen is specified then attempt to reopen the vdev if the vdev is
1730 * faulted or degraded.
1733 vdev_clear(spa_t *spa, vdev_t *vd, boolean_t reopen_wanted)
1738 vd = spa->spa_root_vdev;
1740 vd->vdev_stat.vs_read_errors = 0;
1741 vd->vdev_stat.vs_write_errors = 0;
1742 vd->vdev_stat.vs_checksum_errors = 0;
1743 vd->vdev_is_failing = B_FALSE;
1745 for (c = 0; c < vd->vdev_children; c++)
1746 vdev_clear(spa, vd->vdev_child[c], reopen_wanted);
1749 * If we're in the FAULTED state, then clear the persistent state and
1750 * attempt to reopen the device. We also mark the vdev config dirty, so
1751 * that the new faulted state is written out to disk.
1753 if (reopen_wanted && (vd->vdev_faulted || vd->vdev_degraded)) {
1754 vd->vdev_faulted = vd->vdev_degraded = 0;
1756 vdev_config_dirty(vd->vdev_top);
1758 if (vd->vdev_faulted)
1759 spa_async_request(spa, SPA_ASYNC_RESILVER);
1761 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
1766 vdev_readable(vdev_t *vd)
1769 return (!vdev_is_dead(vd));
1773 vdev_writeable(vdev_t *vd)
1775 return (!vdev_is_dead(vd) && !vd->vdev_is_failing);
1779 vdev_is_dead(vdev_t *vd)
1781 return (vd->vdev_state < VDEV_STATE_DEGRADED);
1785 vdev_error_inject(vdev_t *vd, zio_t *zio)
1789 if (vd->vdev_fault_mode == VDEV_FAULT_NONE)
1792 if (((1ULL << zio->io_type) & vd->vdev_fault_mask) == 0)
1795 switch (vd->vdev_fault_mode) {
1796 case VDEV_FAULT_RANDOM:
1797 if (spa_get_random(vd->vdev_fault_arg) == 0)
1801 case VDEV_FAULT_COUNT:
1802 if ((int64_t)--vd->vdev_fault_arg <= 0)
1803 vd->vdev_fault_mode = VDEV_FAULT_NONE;
1812 * Get statistics for the given vdev.
1815 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1817 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1820 mutex_enter(&vd->vdev_stat_lock);
1821 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1822 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1823 vs->vs_state = vd->vdev_state;
1824 vs->vs_rsize = vdev_get_rsize(vd);
1825 mutex_exit(&vd->vdev_stat_lock);
1828 * If we're getting stats on the root vdev, aggregate the I/O counts
1829 * over all top-level vdevs (i.e. the direct children of the root).
1832 for (c = 0; c < rvd->vdev_children; c++) {
1833 vdev_t *cvd = rvd->vdev_child[c];
1834 vdev_stat_t *cvs = &cvd->vdev_stat;
1836 mutex_enter(&vd->vdev_stat_lock);
1837 for (t = 0; t < ZIO_TYPES; t++) {
1838 vs->vs_ops[t] += cvs->vs_ops[t];
1839 vs->vs_bytes[t] += cvs->vs_bytes[t];
1841 vs->vs_read_errors += cvs->vs_read_errors;
1842 vs->vs_write_errors += cvs->vs_write_errors;
1843 vs->vs_checksum_errors += cvs->vs_checksum_errors;
1844 vs->vs_scrub_examined += cvs->vs_scrub_examined;
1845 vs->vs_scrub_errors += cvs->vs_scrub_errors;
1846 mutex_exit(&vd->vdev_stat_lock);
1852 vdev_clear_stats(vdev_t *vd)
1854 mutex_enter(&vd->vdev_stat_lock);
1855 vd->vdev_stat.vs_space = 0;
1856 vd->vdev_stat.vs_dspace = 0;
1857 vd->vdev_stat.vs_alloc = 0;
1858 mutex_exit(&vd->vdev_stat_lock);
1862 vdev_stat_update(zio_t *zio)
1864 vdev_t *vd = zio->io_vd;
1866 uint64_t txg = zio->io_txg;
1867 vdev_stat_t *vs = &vd->vdev_stat;
1868 zio_type_t type = zio->io_type;
1869 int flags = zio->io_flags;
1871 if (zio->io_error == 0) {
1872 if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1873 mutex_enter(&vd->vdev_stat_lock);
1875 vs->vs_bytes[type] += zio->io_size;
1876 mutex_exit(&vd->vdev_stat_lock);
1878 if ((flags & ZIO_FLAG_IO_REPAIR) &&
1879 zio->io_delegate_list == NULL) {
1880 mutex_enter(&vd->vdev_stat_lock);
1881 if (flags & ZIO_FLAG_SCRUB_THREAD)
1882 vs->vs_scrub_repaired += zio->io_size;
1884 vs->vs_self_healed += zio->io_size;
1885 mutex_exit(&vd->vdev_stat_lock);
1890 if (flags & ZIO_FLAG_SPECULATIVE)
1893 if (vdev_readable(vd)) {
1894 mutex_enter(&vd->vdev_stat_lock);
1895 if (type == ZIO_TYPE_READ) {
1896 if (zio->io_error == ECKSUM)
1897 vs->vs_checksum_errors++;
1899 vs->vs_read_errors++;
1901 if (type == ZIO_TYPE_WRITE)
1902 vs->vs_write_errors++;
1903 mutex_exit(&vd->vdev_stat_lock);
1906 if (type == ZIO_TYPE_WRITE) {
1907 if (txg == 0 || vd->vdev_children != 0)
1909 if (flags & ZIO_FLAG_SCRUB_THREAD) {
1910 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
1911 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1912 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
1914 if (!(flags & ZIO_FLAG_IO_REPAIR)) {
1915 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
1917 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1918 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
1919 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
1925 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
1928 vdev_stat_t *vs = &vd->vdev_stat;
1930 for (c = 0; c < vd->vdev_children; c++)
1931 vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
1933 mutex_enter(&vd->vdev_stat_lock);
1935 if (type == POOL_SCRUB_NONE) {
1937 * Update completion and end time. Leave everything else alone
1938 * so we can report what happened during the previous scrub.
1940 vs->vs_scrub_complete = complete;
1941 vs->vs_scrub_end = gethrestime_sec();
1943 vs->vs_scrub_type = type;
1944 vs->vs_scrub_complete = 0;
1945 vs->vs_scrub_examined = 0;
1946 vs->vs_scrub_repaired = 0;
1947 vs->vs_scrub_errors = 0;
1948 vs->vs_scrub_start = gethrestime_sec();
1949 vs->vs_scrub_end = 0;
1952 mutex_exit(&vd->vdev_stat_lock);
1956 * Update the in-core space usage stats for this vdev and the root vdev.
1959 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
1960 boolean_t update_root)
1962 int64_t dspace_delta = space_delta;
1963 spa_t *spa = vd->vdev_spa;
1964 vdev_t *rvd = spa->spa_root_vdev;
1966 ASSERT(vd == vd->vdev_top);
1969 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
1970 * factor. We must calculate this here and not at the root vdev
1971 * because the root vdev's psize-to-asize is simply the max of its
1972 * childrens', thus not accurate enough for us.
1974 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
1975 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
1976 vd->vdev_deflate_ratio;
1978 mutex_enter(&vd->vdev_stat_lock);
1979 vd->vdev_stat.vs_space += space_delta;
1980 vd->vdev_stat.vs_alloc += alloc_delta;
1981 vd->vdev_stat.vs_dspace += dspace_delta;
1982 mutex_exit(&vd->vdev_stat_lock);
1985 ASSERT(rvd == vd->vdev_parent);
1986 ASSERT(vd->vdev_ms_count != 0);
1989 * Don't count non-normal (e.g. intent log) space as part of
1990 * the pool's capacity.
1992 if (vd->vdev_mg->mg_class != spa->spa_normal_class)
1995 mutex_enter(&rvd->vdev_stat_lock);
1996 rvd->vdev_stat.vs_space += space_delta;
1997 rvd->vdev_stat.vs_alloc += alloc_delta;
1998 rvd->vdev_stat.vs_dspace += dspace_delta;
1999 mutex_exit(&rvd->vdev_stat_lock);
2004 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2005 * so that it will be written out next time the vdev configuration is synced.
2006 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2009 vdev_config_dirty(vdev_t *vd)
2011 spa_t *spa = vd->vdev_spa;
2012 vdev_t *rvd = spa->spa_root_vdev;
2016 * The dirty list is protected by the config lock. The caller must
2017 * either hold the config lock as writer, or must be the sync thread
2018 * (which holds the lock as reader). There's only one sync thread,
2019 * so this is sufficient to ensure mutual exclusion.
2021 ASSERT(spa_config_held(spa, RW_WRITER) ||
2022 dsl_pool_sync_context(spa_get_dsl(spa)));
2025 for (c = 0; c < rvd->vdev_children; c++)
2026 vdev_config_dirty(rvd->vdev_child[c]);
2028 ASSERT(vd == vd->vdev_top);
2030 if (!list_link_active(&vd->vdev_dirty_node))
2031 list_insert_head(&spa->spa_dirty_list, vd);
2036 vdev_config_clean(vdev_t *vd)
2038 spa_t *spa = vd->vdev_spa;
2040 ASSERT(spa_config_held(spa, RW_WRITER) ||
2041 dsl_pool_sync_context(spa_get_dsl(spa)));
2043 ASSERT(list_link_active(&vd->vdev_dirty_node));
2044 list_remove(&spa->spa_dirty_list, vd);
2048 vdev_propagate_state(vdev_t *vd)
2050 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2051 int degraded = 0, faulted = 0;
2056 if (vd->vdev_children > 0) {
2057 for (c = 0; c < vd->vdev_children; c++) {
2058 child = vd->vdev_child[c];
2059 if (vdev_is_dead(child) && !vdev_readable(child))
2061 else if (child->vdev_state <= VDEV_STATE_DEGRADED)
2064 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
2068 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2071 * Root special: if there is a toplevel vdev that cannot be
2072 * opened due to corrupted metadata, then propagate the root
2073 * vdev's aux state as 'corrupt' rather than 'insufficient
2076 if (corrupted && vd == rvd &&
2077 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
2078 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
2079 VDEV_AUX_CORRUPT_DATA);
2082 if (vd->vdev_parent && !vd->vdev_islog)
2083 vdev_propagate_state(vd->vdev_parent);
2087 * Set a vdev's state. If this is during an open, we don't update the parent
2088 * state, because we're in the process of opening children depth-first.
2089 * Otherwise, we propagate the change to the parent.
2091 * If this routine places a device in a faulted state, an appropriate ereport is
2095 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2097 uint64_t save_state;
2099 if (state == vd->vdev_state) {
2100 vd->vdev_stat.vs_aux = aux;
2104 save_state = vd->vdev_state;
2106 vd->vdev_state = state;
2107 vd->vdev_stat.vs_aux = aux;
2110 * If we are setting the vdev state to anything but an open state, then
2111 * always close the underlying device. Otherwise, we keep accessible
2112 * but invalid devices open forever. We don't call vdev_close() itself,
2113 * because that implies some extra checks (offline, etc) that we don't
2114 * want here. This is limited to leaf devices, because otherwise
2115 * closing the device will affect other children.
2117 if (!vdev_readable(vd) && vd->vdev_ops->vdev_op_leaf)
2118 vd->vdev_ops->vdev_op_close(vd);
2120 if (vd->vdev_removed &&
2121 state == VDEV_STATE_CANT_OPEN &&
2122 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
2124 * If the previous state is set to VDEV_STATE_REMOVED, then this
2125 * device was previously marked removed and someone attempted to
2126 * reopen it. If this failed due to a nonexistent device, then
2127 * keep the device in the REMOVED state. We also let this be if
2128 * it is one of our special test online cases, which is only
2129 * attempting to online the device and shouldn't generate an FMA
2132 vd->vdev_state = VDEV_STATE_REMOVED;
2133 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2134 } else if (state == VDEV_STATE_REMOVED) {
2136 * Indicate to the ZFS DE that this device has been removed, and
2137 * any recent errors should be ignored.
2139 zfs_post_remove(vd->vdev_spa, vd);
2140 vd->vdev_removed = B_TRUE;
2141 } else if (state == VDEV_STATE_CANT_OPEN) {
2143 * If we fail to open a vdev during an import, we mark it as
2144 * "not available", which signifies that it was never there to
2145 * begin with. Failure to open such a device is not considered
2148 if (vd->vdev_spa->spa_load_state == SPA_LOAD_IMPORT &&
2149 vd->vdev_ops->vdev_op_leaf)
2150 vd->vdev_not_present = 1;
2153 * Post the appropriate ereport. If the 'prevstate' field is
2154 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2155 * that this is part of a vdev_reopen(). In this case, we don't
2156 * want to post the ereport if the device was already in the
2157 * CANT_OPEN state beforehand.
2159 * If the 'checkremove' flag is set, then this is an attempt to
2160 * online the device in response to an insertion event. If we
2161 * hit this case, then we have detected an insertion event for a
2162 * faulted or offline device that wasn't in the removed state.
2163 * In this scenario, we don't post an ereport because we are
2164 * about to replace the device, or attempt an online with
2165 * vdev_forcefault, which will generate the fault for us.
2167 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
2168 !vd->vdev_not_present && !vd->vdev_checkremove &&
2169 vd != vd->vdev_spa->spa_root_vdev) {
2173 case VDEV_AUX_OPEN_FAILED:
2174 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2176 case VDEV_AUX_CORRUPT_DATA:
2177 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2179 case VDEV_AUX_NO_REPLICAS:
2180 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2182 case VDEV_AUX_BAD_GUID_SUM:
2183 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2185 case VDEV_AUX_TOO_SMALL:
2186 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2188 case VDEV_AUX_BAD_LABEL:
2189 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2192 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2195 zfs_ereport_post(class, vd->vdev_spa,
2196 vd, NULL, save_state, 0);
2199 /* Erase any notion of persistent removed state */
2200 vd->vdev_removed = B_FALSE;
2202 vd->vdev_removed = B_FALSE;
2206 vdev_propagate_state(vd);