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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/zfs_context.h>
28 #include <sys/fm/fs/zfs.h>
30 #include <sys/spa_impl.h>
32 #include <sys/dmu_tx.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/uberblock_impl.h>
35 #include <sys/metaslab.h>
36 #include <sys/metaslab_impl.h>
37 #include <sys/space_map.h>
40 #include <sys/fs/zfs.h>
43 SYSCTL_DECL(_vfs_zfs);
44 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vdev, CTLFLAG_RW, 0, "ZFS VDEV");
47 * Virtual device management.
50 static vdev_ops_t *vdev_ops_table[] = {
66 /* maximum scrub/resilver I/O queue per leaf vdev */
67 int zfs_scrub_limit = 10;
69 TUNABLE_INT("vfs.zfs.scrub_limit", &zfs_scrub_limit);
70 SYSCTL_INT(_vfs_zfs, OID_AUTO, scrub_limit, CTLFLAG_RDTUN, &zfs_scrub_limit, 0,
71 "Maximum scrub/resilver I/O queue");
74 * Given a vdev type, return the appropriate ops vector.
77 vdev_getops(const char *type)
79 vdev_ops_t *ops, **opspp;
81 for (opspp = vdev_ops_table; (ops = *opspp) != NULL; opspp++)
82 if (strcmp(ops->vdev_op_type, type) == 0)
89 * Default asize function: return the MAX of psize with the asize of
90 * all children. This is what's used by anything other than RAID-Z.
93 vdev_default_asize(vdev_t *vd, uint64_t psize)
95 uint64_t asize = P2ROUNDUP(psize, 1ULL << vd->vdev_top->vdev_ashift);
99 for (c = 0; c < vd->vdev_children; c++) {
100 csize = vdev_psize_to_asize(vd->vdev_child[c], psize);
101 asize = MAX(asize, csize);
108 * Get the replaceable or attachable device size.
109 * If the parent is a mirror or raidz, the replaceable size is the minimum
110 * psize of all its children. For the rest, just return our own psize.
121 vdev_get_rsize(vdev_t *vd)
126 pvd = vd->vdev_parent;
129 * If our parent is NULL or the root, just return our own psize.
131 if (pvd == NULL || pvd->vdev_parent == NULL)
132 return (vd->vdev_psize);
136 for (c = 0; c < pvd->vdev_children; c++) {
137 cvd = pvd->vdev_child[c];
138 rsize = MIN(rsize - 1, cvd->vdev_psize - 1) + 1;
145 vdev_lookup_top(spa_t *spa, uint64_t vdev)
147 vdev_t *rvd = spa->spa_root_vdev;
149 ASSERT(spa_config_held(spa, SCL_ALL, RW_READER) != 0);
151 if (vdev < rvd->vdev_children) {
152 ASSERT(rvd->vdev_child[vdev] != NULL);
153 return (rvd->vdev_child[vdev]);
160 vdev_lookup_by_guid(vdev_t *vd, uint64_t guid)
165 if (vd->vdev_guid == guid)
168 for (c = 0; c < vd->vdev_children; c++)
169 if ((mvd = vdev_lookup_by_guid(vd->vdev_child[c], guid)) !=
177 vdev_add_child(vdev_t *pvd, vdev_t *cvd)
179 size_t oldsize, newsize;
180 uint64_t id = cvd->vdev_id;
183 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
184 ASSERT(cvd->vdev_parent == NULL);
186 cvd->vdev_parent = pvd;
191 ASSERT(id >= pvd->vdev_children || pvd->vdev_child[id] == NULL);
193 oldsize = pvd->vdev_children * sizeof (vdev_t *);
194 pvd->vdev_children = MAX(pvd->vdev_children, id + 1);
195 newsize = pvd->vdev_children * sizeof (vdev_t *);
197 newchild = kmem_zalloc(newsize, KM_SLEEP);
198 if (pvd->vdev_child != NULL) {
199 bcopy(pvd->vdev_child, newchild, oldsize);
200 kmem_free(pvd->vdev_child, oldsize);
203 pvd->vdev_child = newchild;
204 pvd->vdev_child[id] = cvd;
206 cvd->vdev_top = (pvd->vdev_top ? pvd->vdev_top: cvd);
207 ASSERT(cvd->vdev_top->vdev_parent->vdev_parent == NULL);
210 * Walk up all ancestors to update guid sum.
212 for (; pvd != NULL; pvd = pvd->vdev_parent)
213 pvd->vdev_guid_sum += cvd->vdev_guid_sum;
215 if (cvd->vdev_ops->vdev_op_leaf)
216 cvd->vdev_spa->spa_scrub_maxinflight += zfs_scrub_limit;
220 vdev_remove_child(vdev_t *pvd, vdev_t *cvd)
223 uint_t id = cvd->vdev_id;
225 ASSERT(cvd->vdev_parent == pvd);
230 ASSERT(id < pvd->vdev_children);
231 ASSERT(pvd->vdev_child[id] == cvd);
233 pvd->vdev_child[id] = NULL;
234 cvd->vdev_parent = NULL;
236 for (c = 0; c < pvd->vdev_children; c++)
237 if (pvd->vdev_child[c])
240 if (c == pvd->vdev_children) {
241 kmem_free(pvd->vdev_child, c * sizeof (vdev_t *));
242 pvd->vdev_child = NULL;
243 pvd->vdev_children = 0;
247 * Walk up all ancestors to update guid sum.
249 for (; pvd != NULL; pvd = pvd->vdev_parent)
250 pvd->vdev_guid_sum -= cvd->vdev_guid_sum;
252 if (cvd->vdev_ops->vdev_op_leaf)
253 cvd->vdev_spa->spa_scrub_maxinflight -= zfs_scrub_limit;
257 * Remove any holes in the child array.
260 vdev_compact_children(vdev_t *pvd)
262 vdev_t **newchild, *cvd;
263 int oldc = pvd->vdev_children;
266 ASSERT(spa_config_held(pvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
268 for (c = newc = 0; c < oldc; c++)
269 if (pvd->vdev_child[c])
272 newchild = kmem_alloc(newc * sizeof (vdev_t *), KM_SLEEP);
274 for (c = newc = 0; c < oldc; c++) {
275 if ((cvd = pvd->vdev_child[c]) != NULL) {
276 newchild[newc] = cvd;
277 cvd->vdev_id = newc++;
281 kmem_free(pvd->vdev_child, oldc * sizeof (vdev_t *));
282 pvd->vdev_child = newchild;
283 pvd->vdev_children = newc;
287 * Allocate and minimally initialize a vdev_t.
290 vdev_alloc_common(spa_t *spa, uint_t id, uint64_t guid, vdev_ops_t *ops)
294 vd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
296 if (spa->spa_root_vdev == NULL) {
297 ASSERT(ops == &vdev_root_ops);
298 spa->spa_root_vdev = vd;
302 if (spa->spa_root_vdev == vd) {
304 * The root vdev's guid will also be the pool guid,
305 * which must be unique among all pools.
307 while (guid == 0 || spa_guid_exists(guid, 0))
308 guid = spa_get_random(-1ULL);
311 * Any other vdev's guid must be unique within the pool.
314 spa_guid_exists(spa_guid(spa), guid))
315 guid = spa_get_random(-1ULL);
317 ASSERT(!spa_guid_exists(spa_guid(spa), guid));
322 vd->vdev_guid = guid;
323 vd->vdev_guid_sum = guid;
325 vd->vdev_state = VDEV_STATE_CLOSED;
327 mutex_init(&vd->vdev_dtl_lock, NULL, MUTEX_DEFAULT, NULL);
328 mutex_init(&vd->vdev_stat_lock, NULL, MUTEX_DEFAULT, NULL);
329 mutex_init(&vd->vdev_probe_lock, NULL, MUTEX_DEFAULT, NULL);
330 space_map_create(&vd->vdev_dtl_map, 0, -1ULL, 0, &vd->vdev_dtl_lock);
331 space_map_create(&vd->vdev_dtl_scrub, 0, -1ULL, 0, &vd->vdev_dtl_lock);
332 txg_list_create(&vd->vdev_ms_list,
333 offsetof(struct metaslab, ms_txg_node));
334 txg_list_create(&vd->vdev_dtl_list,
335 offsetof(struct vdev, vdev_dtl_node));
336 vd->vdev_stat.vs_timestamp = gethrtime();
344 * Allocate a new vdev. The 'alloctype' is used to control whether we are
345 * creating a new vdev or loading an existing one - the behavior is slightly
346 * different for each case.
349 vdev_alloc(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent, uint_t id,
354 uint64_t guid = 0, islog, nparity;
357 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
359 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
362 if ((ops = vdev_getops(type)) == NULL)
366 * If this is a load, get the vdev guid from the nvlist.
367 * Otherwise, vdev_alloc_common() will generate one for us.
369 if (alloctype == VDEV_ALLOC_LOAD) {
372 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID, &label_id) ||
376 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
378 } else if (alloctype == VDEV_ALLOC_SPARE) {
379 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
381 } else if (alloctype == VDEV_ALLOC_L2CACHE) {
382 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &guid) != 0)
387 * The first allocated vdev must be of type 'root'.
389 if (ops != &vdev_root_ops && spa->spa_root_vdev == NULL)
393 * Determine whether we're a log vdev.
396 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &islog);
397 if (islog && spa_version(spa) < SPA_VERSION_SLOGS)
401 * Set the nparity property for RAID-Z vdevs.
404 if (ops == &vdev_raidz_ops) {
405 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
408 * Currently, we can only support 2 parity devices.
410 if (nparity == 0 || nparity > 2)
413 * Older versions can only support 1 parity device.
416 spa_version(spa) < SPA_VERSION_RAID6)
420 * We require the parity to be specified for SPAs that
421 * support multiple parity levels.
423 if (spa_version(spa) >= SPA_VERSION_RAID6)
426 * Otherwise, we default to 1 parity device for RAID-Z.
433 ASSERT(nparity != -1ULL);
435 vd = vdev_alloc_common(spa, id, guid, ops);
437 vd->vdev_islog = islog;
438 vd->vdev_nparity = nparity;
440 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &vd->vdev_path) == 0)
441 vd->vdev_path = spa_strdup(vd->vdev_path);
442 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &vd->vdev_devid) == 0)
443 vd->vdev_devid = spa_strdup(vd->vdev_devid);
444 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PHYS_PATH,
445 &vd->vdev_physpath) == 0)
446 vd->vdev_physpath = spa_strdup(vd->vdev_physpath);
449 * Set the whole_disk property. If it's not specified, leave the value
452 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
453 &vd->vdev_wholedisk) != 0)
454 vd->vdev_wholedisk = -1ULL;
457 * Look for the 'not present' flag. This will only be set if the device
458 * was not present at the time of import.
460 if (!spa->spa_import_faulted)
461 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
462 &vd->vdev_not_present);
465 * Get the alignment requirement.
467 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASHIFT, &vd->vdev_ashift);
470 * If we're a top-level vdev, try to load the allocation parameters.
472 if (parent && !parent->vdev_parent && alloctype == VDEV_ALLOC_LOAD) {
473 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
475 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
477 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ASIZE,
482 * If we're a leaf vdev, try to load the DTL object and other state.
484 if (vd->vdev_ops->vdev_op_leaf &&
485 (alloctype == VDEV_ALLOC_LOAD || alloctype == VDEV_ALLOC_L2CACHE)) {
486 if (alloctype == VDEV_ALLOC_LOAD) {
487 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DTL,
488 &vd->vdev_dtl.smo_object);
489 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_UNSPARE,
492 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE,
496 * When importing a pool, we want to ignore the persistent fault
497 * state, as the diagnosis made on another system may not be
498 * valid in the current context.
500 if (spa->spa_load_state == SPA_LOAD_OPEN) {
501 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED,
503 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_DEGRADED,
505 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED,
511 * Add ourselves to the parent's list of children.
513 vdev_add_child(parent, vd);
521 vdev_free(vdev_t *vd)
524 spa_t *spa = vd->vdev_spa;
527 * vdev_free() implies closing the vdev first. This is simpler than
528 * trying to ensure complicated semantics for all callers.
532 ASSERT(!list_link_active(&vd->vdev_config_dirty_node));
537 for (c = 0; c < vd->vdev_children; c++)
538 vdev_free(vd->vdev_child[c]);
540 ASSERT(vd->vdev_child == NULL);
541 ASSERT(vd->vdev_guid_sum == vd->vdev_guid);
544 * Discard allocation state.
546 if (vd == vd->vdev_top)
547 vdev_metaslab_fini(vd);
549 ASSERT3U(vd->vdev_stat.vs_space, ==, 0);
550 ASSERT3U(vd->vdev_stat.vs_dspace, ==, 0);
551 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
554 * Remove this vdev from its parent's child list.
556 vdev_remove_child(vd->vdev_parent, vd);
558 ASSERT(vd->vdev_parent == NULL);
561 * Clean up vdev structure.
567 spa_strfree(vd->vdev_path);
569 spa_strfree(vd->vdev_devid);
570 if (vd->vdev_physpath)
571 spa_strfree(vd->vdev_physpath);
573 if (vd->vdev_isspare)
574 spa_spare_remove(vd);
575 if (vd->vdev_isl2cache)
576 spa_l2cache_remove(vd);
578 txg_list_destroy(&vd->vdev_ms_list);
579 txg_list_destroy(&vd->vdev_dtl_list);
580 mutex_enter(&vd->vdev_dtl_lock);
581 space_map_unload(&vd->vdev_dtl_map);
582 space_map_destroy(&vd->vdev_dtl_map);
583 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
584 space_map_destroy(&vd->vdev_dtl_scrub);
585 mutex_exit(&vd->vdev_dtl_lock);
586 mutex_destroy(&vd->vdev_dtl_lock);
587 mutex_destroy(&vd->vdev_stat_lock);
588 mutex_destroy(&vd->vdev_probe_lock);
590 if (vd == spa->spa_root_vdev)
591 spa->spa_root_vdev = NULL;
593 kmem_free(vd, sizeof (vdev_t));
597 * Transfer top-level vdev state from svd to tvd.
600 vdev_top_transfer(vdev_t *svd, vdev_t *tvd)
602 spa_t *spa = svd->vdev_spa;
607 ASSERT(tvd == tvd->vdev_top);
609 tvd->vdev_ms_array = svd->vdev_ms_array;
610 tvd->vdev_ms_shift = svd->vdev_ms_shift;
611 tvd->vdev_ms_count = svd->vdev_ms_count;
613 svd->vdev_ms_array = 0;
614 svd->vdev_ms_shift = 0;
615 svd->vdev_ms_count = 0;
617 tvd->vdev_mg = svd->vdev_mg;
618 tvd->vdev_ms = svd->vdev_ms;
623 if (tvd->vdev_mg != NULL)
624 tvd->vdev_mg->mg_vd = tvd;
626 tvd->vdev_stat.vs_alloc = svd->vdev_stat.vs_alloc;
627 tvd->vdev_stat.vs_space = svd->vdev_stat.vs_space;
628 tvd->vdev_stat.vs_dspace = svd->vdev_stat.vs_dspace;
630 svd->vdev_stat.vs_alloc = 0;
631 svd->vdev_stat.vs_space = 0;
632 svd->vdev_stat.vs_dspace = 0;
634 for (t = 0; t < TXG_SIZE; t++) {
635 while ((msp = txg_list_remove(&svd->vdev_ms_list, t)) != NULL)
636 (void) txg_list_add(&tvd->vdev_ms_list, msp, t);
637 while ((vd = txg_list_remove(&svd->vdev_dtl_list, t)) != NULL)
638 (void) txg_list_add(&tvd->vdev_dtl_list, vd, t);
639 if (txg_list_remove_this(&spa->spa_vdev_txg_list, svd, t))
640 (void) txg_list_add(&spa->spa_vdev_txg_list, tvd, t);
643 if (list_link_active(&svd->vdev_config_dirty_node)) {
644 vdev_config_clean(svd);
645 vdev_config_dirty(tvd);
648 if (list_link_active(&svd->vdev_state_dirty_node)) {
649 vdev_state_clean(svd);
650 vdev_state_dirty(tvd);
653 tvd->vdev_deflate_ratio = svd->vdev_deflate_ratio;
654 svd->vdev_deflate_ratio = 0;
656 tvd->vdev_islog = svd->vdev_islog;
661 vdev_top_update(vdev_t *tvd, vdev_t *vd)
670 for (c = 0; c < vd->vdev_children; c++)
671 vdev_top_update(tvd, vd->vdev_child[c]);
675 * Add a mirror/replacing vdev above an existing vdev.
678 vdev_add_parent(vdev_t *cvd, vdev_ops_t *ops)
680 spa_t *spa = cvd->vdev_spa;
681 vdev_t *pvd = cvd->vdev_parent;
684 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
686 mvd = vdev_alloc_common(spa, cvd->vdev_id, 0, ops);
688 mvd->vdev_asize = cvd->vdev_asize;
689 mvd->vdev_ashift = cvd->vdev_ashift;
690 mvd->vdev_state = cvd->vdev_state;
692 vdev_remove_child(pvd, cvd);
693 vdev_add_child(pvd, mvd);
694 cvd->vdev_id = mvd->vdev_children;
695 vdev_add_child(mvd, cvd);
696 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
698 if (mvd == mvd->vdev_top)
699 vdev_top_transfer(cvd, mvd);
705 * Remove a 1-way mirror/replacing vdev from the tree.
708 vdev_remove_parent(vdev_t *cvd)
710 vdev_t *mvd = cvd->vdev_parent;
711 vdev_t *pvd = mvd->vdev_parent;
713 ASSERT(spa_config_held(cvd->vdev_spa, SCL_ALL, RW_WRITER) == SCL_ALL);
715 ASSERT(mvd->vdev_children == 1);
716 ASSERT(mvd->vdev_ops == &vdev_mirror_ops ||
717 mvd->vdev_ops == &vdev_replacing_ops ||
718 mvd->vdev_ops == &vdev_spare_ops);
719 cvd->vdev_ashift = mvd->vdev_ashift;
721 vdev_remove_child(mvd, cvd);
722 vdev_remove_child(pvd, mvd);
724 * If cvd will replace mvd as a top-level vdev, preserve mvd's guid.
725 * Otherwise, we could have detached an offline device, and when we
726 * go to import the pool we'll think we have two top-level vdevs,
727 * instead of a different version of the same top-level vdev.
729 if (mvd->vdev_top == mvd)
730 cvd->vdev_guid = cvd->vdev_guid_sum = mvd->vdev_guid;
731 cvd->vdev_id = mvd->vdev_id;
732 vdev_add_child(pvd, cvd);
733 vdev_top_update(cvd->vdev_top, cvd->vdev_top);
735 if (cvd == cvd->vdev_top)
736 vdev_top_transfer(mvd, cvd);
738 ASSERT(mvd->vdev_children == 0);
743 vdev_metaslab_init(vdev_t *vd, uint64_t txg)
745 spa_t *spa = vd->vdev_spa;
746 objset_t *mos = spa->spa_meta_objset;
747 metaslab_class_t *mc;
749 uint64_t oldc = vd->vdev_ms_count;
750 uint64_t newc = vd->vdev_asize >> vd->vdev_ms_shift;
754 if (vd->vdev_ms_shift == 0) /* not being allocated from yet */
757 ASSERT(oldc <= newc);
760 mc = spa->spa_log_class;
762 mc = spa->spa_normal_class;
764 if (vd->vdev_mg == NULL)
765 vd->vdev_mg = metaslab_group_create(mc, vd);
767 mspp = kmem_zalloc(newc * sizeof (*mspp), KM_SLEEP);
770 bcopy(vd->vdev_ms, mspp, oldc * sizeof (*mspp));
771 kmem_free(vd->vdev_ms, oldc * sizeof (*mspp));
775 vd->vdev_ms_count = newc;
777 for (m = oldc; m < newc; m++) {
778 space_map_obj_t smo = { 0, 0, 0 };
781 error = dmu_read(mos, vd->vdev_ms_array,
782 m * sizeof (uint64_t), sizeof (uint64_t), &object);
787 error = dmu_bonus_hold(mos, object, FTAG, &db);
790 ASSERT3U(db->db_size, >=, sizeof (smo));
791 bcopy(db->db_data, &smo, sizeof (smo));
792 ASSERT3U(smo.smo_object, ==, object);
793 dmu_buf_rele(db, FTAG);
796 vd->vdev_ms[m] = metaslab_init(vd->vdev_mg, &smo,
797 m << vd->vdev_ms_shift, 1ULL << vd->vdev_ms_shift, txg);
804 vdev_metaslab_fini(vdev_t *vd)
807 uint64_t count = vd->vdev_ms_count;
809 if (vd->vdev_ms != NULL) {
810 for (m = 0; m < count; m++)
811 if (vd->vdev_ms[m] != NULL)
812 metaslab_fini(vd->vdev_ms[m]);
813 kmem_free(vd->vdev_ms, count * sizeof (metaslab_t *));
818 typedef struct vdev_probe_stats {
819 boolean_t vps_readable;
820 boolean_t vps_writeable;
824 } vdev_probe_stats_t;
827 vdev_probe_done(zio_t *zio)
829 vdev_probe_stats_t *vps = zio->io_private;
830 vdev_t *vd = vps->vps_vd;
832 if (zio->io_type == ZIO_TYPE_READ) {
833 ASSERT(zio->io_vd == vd);
834 if (zio->io_error == 0)
835 vps->vps_readable = 1;
836 if (zio->io_error == 0 && (spa_mode & FWRITE)) {
837 zio_nowait(zio_write_phys(vps->vps_root, vd,
838 zio->io_offset, zio->io_size, zio->io_data,
839 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
840 ZIO_PRIORITY_SYNC_WRITE, vps->vps_flags, B_TRUE));
842 zio_buf_free(zio->io_data, zio->io_size);
844 } else if (zio->io_type == ZIO_TYPE_WRITE) {
845 ASSERT(zio->io_vd == vd);
846 if (zio->io_error == 0)
847 vps->vps_writeable = 1;
848 zio_buf_free(zio->io_data, zio->io_size);
849 } else if (zio->io_type == ZIO_TYPE_NULL) {
850 ASSERT(zio->io_vd == NULL);
851 ASSERT(zio == vps->vps_root);
853 vd->vdev_cant_read |= !vps->vps_readable;
854 vd->vdev_cant_write |= !vps->vps_writeable;
856 if (vdev_readable(vd) &&
857 (vdev_writeable(vd) || !(spa_mode & FWRITE))) {
860 ASSERT(zio->io_error != 0);
861 zfs_ereport_post(FM_EREPORT_ZFS_PROBE_FAILURE,
862 zio->io_spa, vd, NULL, 0, 0);
863 zio->io_error = ENXIO;
865 kmem_free(vps, sizeof (*vps));
870 * Determine whether this device is accessible by reading and writing
871 * to several known locations: the pad regions of each vdev label
872 * but the first (which we leave alone in case it contains a VTOC).
875 vdev_probe(vdev_t *vd, zio_t *pio)
877 spa_t *spa = vd->vdev_spa;
878 vdev_probe_stats_t *vps;
881 vps = kmem_zalloc(sizeof (*vps), KM_SLEEP);
883 vps->vps_flags = ZIO_FLAG_CANFAIL | ZIO_FLAG_PROBE |
884 ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_AGGREGATE | ZIO_FLAG_DONT_RETRY;
886 if (spa_config_held(spa, SCL_ZIO, RW_WRITER)) {
888 * vdev_cant_read and vdev_cant_write can only transition
889 * from TRUE to FALSE when we have the SCL_ZIO lock as writer;
890 * otherwise they can only transition from FALSE to TRUE.
891 * This ensures that any zio looking at these values can
892 * assume that failures persist for the life of the I/O.
893 * That's important because when a device has intermittent
894 * connectivity problems, we want to ensure that they're
895 * ascribed to the device (ENXIO) and not the zio (EIO).
897 * Since we hold SCL_ZIO as writer here, clear both values
898 * so the probe can reevaluate from first principles.
900 vps->vps_flags |= ZIO_FLAG_CONFIG_WRITER;
901 vd->vdev_cant_read = B_FALSE;
902 vd->vdev_cant_write = B_FALSE;
905 ASSERT(vd->vdev_ops->vdev_op_leaf);
907 zio = zio_null(pio, spa, vdev_probe_done, vps, vps->vps_flags);
912 for (int l = 1; l < VDEV_LABELS; l++) {
913 zio_nowait(zio_read_phys(zio, vd,
914 vdev_label_offset(vd->vdev_psize, l,
915 offsetof(vdev_label_t, vl_pad)),
916 VDEV_SKIP_SIZE, zio_buf_alloc(VDEV_SKIP_SIZE),
917 ZIO_CHECKSUM_OFF, vdev_probe_done, vps,
918 ZIO_PRIORITY_SYNC_READ, vps->vps_flags, B_TRUE));
925 * Prepare a virtual device for access.
928 vdev_open(vdev_t *vd)
933 uint64_t asize, psize;
936 ASSERT(vd->vdev_state == VDEV_STATE_CLOSED ||
937 vd->vdev_state == VDEV_STATE_CANT_OPEN ||
938 vd->vdev_state == VDEV_STATE_OFFLINE);
940 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
942 if (!vd->vdev_removed && vd->vdev_faulted) {
943 ASSERT(vd->vdev_children == 0);
944 vdev_set_state(vd, B_TRUE, VDEV_STATE_FAULTED,
945 VDEV_AUX_ERR_EXCEEDED);
947 } else if (vd->vdev_offline) {
948 ASSERT(vd->vdev_children == 0);
949 vdev_set_state(vd, B_TRUE, VDEV_STATE_OFFLINE, VDEV_AUX_NONE);
953 error = vd->vdev_ops->vdev_op_open(vd, &osize, &ashift);
955 if (zio_injection_enabled && error == 0)
956 error = zio_handle_device_injection(vd, ENXIO);
959 if (vd->vdev_removed &&
960 vd->vdev_stat.vs_aux != VDEV_AUX_OPEN_FAILED)
961 vd->vdev_removed = B_FALSE;
963 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
964 vd->vdev_stat.vs_aux);
968 vd->vdev_removed = B_FALSE;
970 if (vd->vdev_degraded) {
971 ASSERT(vd->vdev_children == 0);
972 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
973 VDEV_AUX_ERR_EXCEEDED);
975 vd->vdev_state = VDEV_STATE_HEALTHY;
978 for (c = 0; c < vd->vdev_children; c++)
979 if (vd->vdev_child[c]->vdev_state != VDEV_STATE_HEALTHY) {
980 vdev_set_state(vd, B_TRUE, VDEV_STATE_DEGRADED,
985 osize = P2ALIGN(osize, (uint64_t)sizeof (vdev_label_t));
987 if (vd->vdev_children == 0) {
988 if (osize < SPA_MINDEVSIZE) {
989 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
994 asize = osize - (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE);
996 if (vd->vdev_parent != NULL && osize < SPA_MINDEVSIZE -
997 (VDEV_LABEL_START_SIZE + VDEV_LABEL_END_SIZE)) {
998 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1006 vd->vdev_psize = psize;
1008 if (vd->vdev_asize == 0) {
1010 * This is the first-ever open, so use the computed values.
1011 * For testing purposes, a higher ashift can be requested.
1013 vd->vdev_asize = asize;
1014 vd->vdev_ashift = MAX(ashift, vd->vdev_ashift);
1017 * Make sure the alignment requirement hasn't increased.
1019 if (ashift > vd->vdev_top->vdev_ashift) {
1020 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1021 VDEV_AUX_BAD_LABEL);
1026 * Make sure the device hasn't shrunk.
1028 if (asize < vd->vdev_asize) {
1029 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1030 VDEV_AUX_BAD_LABEL);
1035 * If all children are healthy and the asize has increased,
1036 * then we've experienced dynamic LUN growth.
1038 if (vd->vdev_state == VDEV_STATE_HEALTHY &&
1039 asize > vd->vdev_asize) {
1040 vd->vdev_asize = asize;
1045 * Ensure we can issue some IO before declaring the
1046 * vdev open for business.
1048 if (vd->vdev_ops->vdev_op_leaf &&
1049 (error = zio_wait(vdev_probe(vd, NULL))) != 0) {
1050 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1051 VDEV_AUX_IO_FAILURE);
1056 * If this is a top-level vdev, compute the raidz-deflation
1057 * ratio. Note, we hard-code in 128k (1<<17) because it is the
1058 * current "typical" blocksize. Even if SPA_MAXBLOCKSIZE
1059 * changes, this algorithm must never change, or we will
1060 * inconsistently account for existing bp's.
1062 if (vd->vdev_top == vd) {
1063 vd->vdev_deflate_ratio = (1<<17) /
1064 (vdev_psize_to_asize(vd, 1<<17) >> SPA_MINBLOCKSHIFT);
1068 * If a leaf vdev has a DTL, and seems healthy, then kick off a
1069 * resilver. But don't do this if we are doing a reopen for a
1070 * scrub, since this would just restart the scrub we are already
1073 if (vd->vdev_children == 0 && !vd->vdev_spa->spa_scrub_reopen) {
1074 mutex_enter(&vd->vdev_dtl_lock);
1075 if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd))
1076 spa_async_request(vd->vdev_spa, SPA_ASYNC_RESILVER);
1077 mutex_exit(&vd->vdev_dtl_lock);
1084 * Called once the vdevs are all opened, this routine validates the label
1085 * contents. This needs to be done before vdev_load() so that we don't
1086 * inadvertently do repair I/Os to the wrong device.
1088 * This function will only return failure if one of the vdevs indicates that it
1089 * has since been destroyed or exported. This is only possible if
1090 * /etc/zfs/zpool.cache was readonly at the time. Otherwise, the vdev state
1091 * will be updated but the function will return 0.
1094 vdev_validate(vdev_t *vd)
1096 spa_t *spa = vd->vdev_spa;
1099 uint64_t guid, top_guid;
1102 for (c = 0; c < vd->vdev_children; c++)
1103 if (vdev_validate(vd->vdev_child[c]) != 0)
1107 * If the device has already failed, or was marked offline, don't do
1108 * any further validation. Otherwise, label I/O will fail and we will
1109 * overwrite the previous state.
1111 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
1113 if ((label = vdev_label_read_config(vd)) == NULL) {
1114 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1115 VDEV_AUX_BAD_LABEL);
1119 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
1120 &guid) != 0 || guid != spa_guid(spa)) {
1121 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1122 VDEV_AUX_CORRUPT_DATA);
1128 * If this vdev just became a top-level vdev because its
1129 * sibling was detached, it will have adopted the parent's
1130 * vdev guid -- but the label may or may not be on disk yet.
1131 * Fortunately, either version of the label will have the
1132 * same top guid, so if we're a top-level vdev, we can
1133 * safely compare to that instead.
1135 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
1137 nvlist_lookup_uint64(label, ZPOOL_CONFIG_TOP_GUID,
1139 (vd->vdev_guid != guid &&
1140 (vd->vdev_guid != top_guid || vd != vd->vdev_top))) {
1141 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1142 VDEV_AUX_CORRUPT_DATA);
1147 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
1149 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1150 VDEV_AUX_CORRUPT_DATA);
1157 if (spa->spa_load_state == SPA_LOAD_OPEN &&
1158 state != POOL_STATE_ACTIVE)
1162 * If we were able to open and validate a vdev that was
1163 * previously marked permanently unavailable, clear that state
1166 if (vd->vdev_not_present)
1167 vd->vdev_not_present = 0;
1174 * Close a virtual device.
1177 vdev_close(vdev_t *vd)
1179 vd->vdev_ops->vdev_op_close(vd);
1181 vdev_cache_purge(vd);
1184 * We record the previous state before we close it, so that if we are
1185 * doing a reopen(), we don't generate FMA ereports if we notice that
1186 * it's still faulted.
1188 vd->vdev_prevstate = vd->vdev_state;
1190 if (vd->vdev_offline)
1191 vd->vdev_state = VDEV_STATE_OFFLINE;
1193 vd->vdev_state = VDEV_STATE_CLOSED;
1194 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
1198 vdev_reopen(vdev_t *vd)
1200 spa_t *spa = vd->vdev_spa;
1202 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1205 (void) vdev_open(vd);
1208 * Call vdev_validate() here to make sure we have the same device.
1209 * Otherwise, a device with an invalid label could be successfully
1210 * opened in response to vdev_reopen().
1213 (void) vdev_validate_aux(vd);
1214 if (vdev_readable(vd) && vdev_writeable(vd) &&
1215 !l2arc_vdev_present(vd)) {
1216 uint64_t size = vdev_get_rsize(vd);
1217 l2arc_add_vdev(spa, vd,
1218 VDEV_LABEL_START_SIZE,
1219 size - VDEV_LABEL_START_SIZE);
1222 (void) vdev_validate(vd);
1226 * Reassess parent vdev's health.
1228 vdev_propagate_state(vd);
1232 vdev_create(vdev_t *vd, uint64_t txg, boolean_t isreplacing)
1237 * Normally, partial opens (e.g. of a mirror) are allowed.
1238 * For a create, however, we want to fail the request if
1239 * there are any components we can't open.
1241 error = vdev_open(vd);
1243 if (error || vd->vdev_state != VDEV_STATE_HEALTHY) {
1245 return (error ? error : ENXIO);
1249 * Recursively initialize all labels.
1251 if ((error = vdev_label_init(vd, txg, isreplacing ?
1252 VDEV_LABEL_REPLACE : VDEV_LABEL_CREATE)) != 0) {
1261 * The is the latter half of vdev_create(). It is distinct because it
1262 * involves initiating transactions in order to do metaslab creation.
1263 * For creation, we want to try to create all vdevs at once and then undo it
1264 * if anything fails; this is much harder if we have pending transactions.
1267 vdev_init(vdev_t *vd, uint64_t txg)
1270 * Aim for roughly 200 metaslabs per vdev.
1272 vd->vdev_ms_shift = highbit(vd->vdev_asize / 200);
1273 vd->vdev_ms_shift = MAX(vd->vdev_ms_shift, SPA_MAXBLOCKSHIFT);
1276 * Initialize the vdev's metaslabs. This can't fail because
1277 * there's nothing to read when creating all new metaslabs.
1279 VERIFY(vdev_metaslab_init(vd, txg) == 0);
1283 vdev_dirty(vdev_t *vd, int flags, void *arg, uint64_t txg)
1285 ASSERT(vd == vd->vdev_top);
1286 ASSERT(ISP2(flags));
1288 if (flags & VDD_METASLAB)
1289 (void) txg_list_add(&vd->vdev_ms_list, arg, txg);
1291 if (flags & VDD_DTL)
1292 (void) txg_list_add(&vd->vdev_dtl_list, arg, txg);
1294 (void) txg_list_add(&vd->vdev_spa->spa_vdev_txg_list, vd, txg);
1298 vdev_dtl_dirty(space_map_t *sm, uint64_t txg, uint64_t size)
1300 mutex_enter(sm->sm_lock);
1301 if (!space_map_contains(sm, txg, size))
1302 space_map_add(sm, txg, size);
1303 mutex_exit(sm->sm_lock);
1307 vdev_dtl_contains(space_map_t *sm, uint64_t txg, uint64_t size)
1312 * Quick test without the lock -- covers the common case that
1313 * there are no dirty time segments.
1315 if (sm->sm_space == 0)
1318 mutex_enter(sm->sm_lock);
1319 dirty = space_map_contains(sm, txg, size);
1320 mutex_exit(sm->sm_lock);
1326 * Reassess DTLs after a config change or scrub completion.
1329 vdev_dtl_reassess(vdev_t *vd, uint64_t txg, uint64_t scrub_txg, int scrub_done)
1331 spa_t *spa = vd->vdev_spa;
1334 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
1336 if (vd->vdev_children == 0) {
1337 mutex_enter(&vd->vdev_dtl_lock);
1338 if (scrub_txg != 0 &&
1339 (spa->spa_scrub_started || spa->spa_scrub_errors == 0)) {
1340 /* XXX should check scrub_done? */
1342 * We completed a scrub up to scrub_txg. If we
1343 * did it without rebooting, then the scrub dtl
1344 * will be valid, so excise the old region and
1345 * fold in the scrub dtl. Otherwise, leave the
1346 * dtl as-is if there was an error.
1348 space_map_excise(&vd->vdev_dtl_map, 0, scrub_txg);
1349 space_map_union(&vd->vdev_dtl_map, &vd->vdev_dtl_scrub);
1352 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1353 mutex_exit(&vd->vdev_dtl_lock);
1356 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
1361 * Make sure the DTLs are always correct under the scrub lock.
1363 if (vd == spa->spa_root_vdev)
1364 mutex_enter(&spa->spa_scrub_lock);
1366 mutex_enter(&vd->vdev_dtl_lock);
1367 space_map_vacate(&vd->vdev_dtl_map, NULL, NULL);
1368 space_map_vacate(&vd->vdev_dtl_scrub, NULL, NULL);
1369 mutex_exit(&vd->vdev_dtl_lock);
1371 for (c = 0; c < vd->vdev_children; c++) {
1372 vdev_t *cvd = vd->vdev_child[c];
1373 vdev_dtl_reassess(cvd, txg, scrub_txg, scrub_done);
1374 mutex_enter(&vd->vdev_dtl_lock);
1375 space_map_union(&vd->vdev_dtl_map, &cvd->vdev_dtl_map);
1376 space_map_union(&vd->vdev_dtl_scrub, &cvd->vdev_dtl_scrub);
1377 mutex_exit(&vd->vdev_dtl_lock);
1380 if (vd == spa->spa_root_vdev)
1381 mutex_exit(&spa->spa_scrub_lock);
1385 vdev_dtl_load(vdev_t *vd)
1387 spa_t *spa = vd->vdev_spa;
1388 space_map_obj_t *smo = &vd->vdev_dtl;
1389 objset_t *mos = spa->spa_meta_objset;
1393 ASSERT(vd->vdev_children == 0);
1395 if (smo->smo_object == 0)
1398 if ((error = dmu_bonus_hold(mos, smo->smo_object, FTAG, &db)) != 0)
1401 ASSERT3U(db->db_size, >=, sizeof (*smo));
1402 bcopy(db->db_data, smo, sizeof (*smo));
1403 dmu_buf_rele(db, FTAG);
1405 mutex_enter(&vd->vdev_dtl_lock);
1406 error = space_map_load(&vd->vdev_dtl_map, NULL, SM_ALLOC, smo, mos);
1407 mutex_exit(&vd->vdev_dtl_lock);
1413 vdev_dtl_sync(vdev_t *vd, uint64_t txg)
1415 spa_t *spa = vd->vdev_spa;
1416 space_map_obj_t *smo = &vd->vdev_dtl;
1417 space_map_t *sm = &vd->vdev_dtl_map;
1418 objset_t *mos = spa->spa_meta_objset;
1424 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1426 if (vd->vdev_detached) {
1427 if (smo->smo_object != 0) {
1428 int err = dmu_object_free(mos, smo->smo_object, tx);
1429 ASSERT3U(err, ==, 0);
1430 smo->smo_object = 0;
1436 if (smo->smo_object == 0) {
1437 ASSERT(smo->smo_objsize == 0);
1438 ASSERT(smo->smo_alloc == 0);
1439 smo->smo_object = dmu_object_alloc(mos,
1440 DMU_OT_SPACE_MAP, 1 << SPACE_MAP_BLOCKSHIFT,
1441 DMU_OT_SPACE_MAP_HEADER, sizeof (*smo), tx);
1442 ASSERT(smo->smo_object != 0);
1443 vdev_config_dirty(vd->vdev_top);
1446 mutex_init(&smlock, NULL, MUTEX_DEFAULT, NULL);
1448 space_map_create(&smsync, sm->sm_start, sm->sm_size, sm->sm_shift,
1451 mutex_enter(&smlock);
1453 mutex_enter(&vd->vdev_dtl_lock);
1454 space_map_walk(sm, space_map_add, &smsync);
1455 mutex_exit(&vd->vdev_dtl_lock);
1457 space_map_truncate(smo, mos, tx);
1458 space_map_sync(&smsync, SM_ALLOC, smo, mos, tx);
1460 space_map_destroy(&smsync);
1462 mutex_exit(&smlock);
1463 mutex_destroy(&smlock);
1465 VERIFY(0 == dmu_bonus_hold(mos, smo->smo_object, FTAG, &db));
1466 dmu_buf_will_dirty(db, tx);
1467 ASSERT3U(db->db_size, >=, sizeof (*smo));
1468 bcopy(smo, db->db_data, sizeof (*smo));
1469 dmu_buf_rele(db, FTAG);
1475 * Determine if resilver is needed, and if so the txg range.
1478 vdev_resilver_needed(vdev_t *vd, uint64_t *minp, uint64_t *maxp)
1480 boolean_t needed = B_FALSE;
1481 uint64_t thismin = UINT64_MAX;
1482 uint64_t thismax = 0;
1484 if (vd->vdev_children == 0) {
1485 mutex_enter(&vd->vdev_dtl_lock);
1486 if (vd->vdev_dtl_map.sm_space != 0 && vdev_writeable(vd)) {
1489 ss = avl_first(&vd->vdev_dtl_map.sm_root);
1490 thismin = ss->ss_start - 1;
1491 ss = avl_last(&vd->vdev_dtl_map.sm_root);
1492 thismax = ss->ss_end;
1495 mutex_exit(&vd->vdev_dtl_lock);
1498 for (c = 0; c < vd->vdev_children; c++) {
1499 vdev_t *cvd = vd->vdev_child[c];
1500 uint64_t cmin, cmax;
1502 if (vdev_resilver_needed(cvd, &cmin, &cmax)) {
1503 thismin = MIN(thismin, cmin);
1504 thismax = MAX(thismax, cmax);
1510 if (needed && minp) {
1518 vdev_load(vdev_t *vd)
1523 * Recursively load all children.
1525 for (c = 0; c < vd->vdev_children; c++)
1526 vdev_load(vd->vdev_child[c]);
1529 * If this is a top-level vdev, initialize its metaslabs.
1531 if (vd == vd->vdev_top &&
1532 (vd->vdev_ashift == 0 || vd->vdev_asize == 0 ||
1533 vdev_metaslab_init(vd, 0) != 0))
1534 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1535 VDEV_AUX_CORRUPT_DATA);
1538 * If this is a leaf vdev, load its DTL.
1540 if (vd->vdev_ops->vdev_op_leaf && vdev_dtl_load(vd) != 0)
1541 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
1542 VDEV_AUX_CORRUPT_DATA);
1546 * The special vdev case is used for hot spares and l2cache devices. Its
1547 * sole purpose it to set the vdev state for the associated vdev. To do this,
1548 * we make sure that we can open the underlying device, then try to read the
1549 * label, and make sure that the label is sane and that it hasn't been
1550 * repurposed to another pool.
1553 vdev_validate_aux(vdev_t *vd)
1556 uint64_t guid, version;
1559 if (!vdev_readable(vd))
1562 if ((label = vdev_label_read_config(vd)) == NULL) {
1563 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1564 VDEV_AUX_CORRUPT_DATA);
1568 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_VERSION, &version) != 0 ||
1569 version > SPA_VERSION ||
1570 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, &guid) != 0 ||
1571 guid != vd->vdev_guid ||
1572 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, &state) != 0) {
1573 vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
1574 VDEV_AUX_CORRUPT_DATA);
1580 * We don't actually check the pool state here. If it's in fact in
1581 * use by another pool, we update this fact on the fly when requested.
1588 vdev_sync_done(vdev_t *vd, uint64_t txg)
1592 while (msp = txg_list_remove(&vd->vdev_ms_list, TXG_CLEAN(txg)))
1593 metaslab_sync_done(msp, txg);
1597 vdev_sync(vdev_t *vd, uint64_t txg)
1599 spa_t *spa = vd->vdev_spa;
1604 if (vd->vdev_ms_array == 0 && vd->vdev_ms_shift != 0) {
1605 ASSERT(vd == vd->vdev_top);
1606 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
1607 vd->vdev_ms_array = dmu_object_alloc(spa->spa_meta_objset,
1608 DMU_OT_OBJECT_ARRAY, 0, DMU_OT_NONE, 0, tx);
1609 ASSERT(vd->vdev_ms_array != 0);
1610 vdev_config_dirty(vd);
1614 while ((msp = txg_list_remove(&vd->vdev_ms_list, txg)) != NULL) {
1615 metaslab_sync(msp, txg);
1616 (void) txg_list_add(&vd->vdev_ms_list, msp, TXG_CLEAN(txg));
1619 while ((lvd = txg_list_remove(&vd->vdev_dtl_list, txg)) != NULL)
1620 vdev_dtl_sync(lvd, txg);
1622 (void) txg_list_add(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg));
1626 vdev_psize_to_asize(vdev_t *vd, uint64_t psize)
1628 return (vd->vdev_ops->vdev_op_asize(vd, psize));
1632 * Mark the given vdev faulted. A faulted vdev behaves as if the device could
1633 * not be opened, and no I/O is attempted.
1636 vdev_fault(spa_t *spa, uint64_t guid)
1640 spa_vdev_state_enter(spa);
1642 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1643 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1645 if (!vd->vdev_ops->vdev_op_leaf)
1646 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1649 * Faulted state takes precedence over degraded.
1651 vd->vdev_faulted = 1ULL;
1652 vd->vdev_degraded = 0ULL;
1653 vdev_set_state(vd, B_FALSE, VDEV_STATE_FAULTED, VDEV_AUX_ERR_EXCEEDED);
1656 * If marking the vdev as faulted cause the top-level vdev to become
1657 * unavailable, then back off and simply mark the vdev as degraded
1660 if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1661 vd->vdev_degraded = 1ULL;
1662 vd->vdev_faulted = 0ULL;
1665 * If we reopen the device and it's not dead, only then do we
1670 if (vdev_readable(vd)) {
1671 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1672 VDEV_AUX_ERR_EXCEEDED);
1676 return (spa_vdev_state_exit(spa, vd, 0));
1680 * Mark the given vdev degraded. A degraded vdev is purely an indication to the
1681 * user that something is wrong. The vdev continues to operate as normal as far
1682 * as I/O is concerned.
1685 vdev_degrade(spa_t *spa, uint64_t guid)
1689 spa_vdev_state_enter(spa);
1691 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1692 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1694 if (!vd->vdev_ops->vdev_op_leaf)
1695 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1698 * If the vdev is already faulted, then don't do anything.
1700 if (vd->vdev_faulted || vd->vdev_degraded)
1701 return (spa_vdev_state_exit(spa, NULL, 0));
1703 vd->vdev_degraded = 1ULL;
1704 if (!vdev_is_dead(vd))
1705 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED,
1706 VDEV_AUX_ERR_EXCEEDED);
1708 return (spa_vdev_state_exit(spa, vd, 0));
1712 * Online the given vdev. If 'unspare' is set, it implies two things. First,
1713 * any attached spare device should be detached when the device finishes
1714 * resilvering. Second, the online should be treated like a 'test' online case,
1715 * so no FMA events are generated if the device fails to open.
1718 vdev_online(spa_t *spa, uint64_t guid, uint64_t flags, vdev_state_t *newstate)
1722 spa_vdev_state_enter(spa);
1724 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1725 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1727 if (!vd->vdev_ops->vdev_op_leaf)
1728 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1730 vd->vdev_offline = B_FALSE;
1731 vd->vdev_tmpoffline = B_FALSE;
1732 vd->vdev_checkremove = !!(flags & ZFS_ONLINE_CHECKREMOVE);
1733 vd->vdev_forcefault = !!(flags & ZFS_ONLINE_FORCEFAULT);
1734 vdev_reopen(vd->vdev_top);
1735 vd->vdev_checkremove = vd->vdev_forcefault = B_FALSE;
1738 *newstate = vd->vdev_state;
1739 if ((flags & ZFS_ONLINE_UNSPARE) &&
1740 !vdev_is_dead(vd) && vd->vdev_parent &&
1741 vd->vdev_parent->vdev_ops == &vdev_spare_ops &&
1742 vd->vdev_parent->vdev_child[0] == vd)
1743 vd->vdev_unspare = B_TRUE;
1745 (void) spa_vdev_state_exit(spa, vd, 0);
1747 VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
1753 vdev_offline(spa_t *spa, uint64_t guid, uint64_t flags)
1757 spa_vdev_state_enter(spa);
1759 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
1760 return (spa_vdev_state_exit(spa, NULL, ENODEV));
1762 if (!vd->vdev_ops->vdev_op_leaf)
1763 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
1766 * If the device isn't already offline, try to offline it.
1768 if (!vd->vdev_offline) {
1770 * If this device's top-level vdev has a non-empty DTL,
1771 * don't allow the device to be offlined.
1773 * XXX -- make this more precise by allowing the offline
1774 * as long as the remaining devices don't have any DTL holes.
1776 if (vd->vdev_top->vdev_dtl_map.sm_space != 0)
1777 return (spa_vdev_state_exit(spa, NULL, EBUSY));
1780 * Offline this device and reopen its top-level vdev.
1781 * If this action results in the top-level vdev becoming
1782 * unusable, undo it and fail the request.
1784 vd->vdev_offline = B_TRUE;
1785 vdev_reopen(vd->vdev_top);
1786 if (vdev_is_dead(vd->vdev_top) && vd->vdev_aux == NULL) {
1787 vd->vdev_offline = B_FALSE;
1788 vdev_reopen(vd->vdev_top);
1789 return (spa_vdev_state_exit(spa, NULL, EBUSY));
1793 vd->vdev_tmpoffline = !!(flags & ZFS_OFFLINE_TEMPORARY);
1795 return (spa_vdev_state_exit(spa, vd, 0));
1799 * Clear the error counts associated with this vdev. Unlike vdev_online() and
1800 * vdev_offline(), we assume the spa config is locked. We also clear all
1801 * children. If 'vd' is NULL, then the user wants to clear all vdevs.
1804 vdev_clear(spa_t *spa, vdev_t *vd)
1806 vdev_t *rvd = spa->spa_root_vdev;
1808 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
1813 vd->vdev_stat.vs_read_errors = 0;
1814 vd->vdev_stat.vs_write_errors = 0;
1815 vd->vdev_stat.vs_checksum_errors = 0;
1817 for (int c = 0; c < vd->vdev_children; c++)
1818 vdev_clear(spa, vd->vdev_child[c]);
1821 * If we're in the FAULTED state or have experienced failed I/O, then
1822 * clear the persistent state and attempt to reopen the device. We
1823 * also mark the vdev config dirty, so that the new faulted state is
1824 * written out to disk.
1826 if (vd->vdev_faulted || vd->vdev_degraded ||
1827 !vdev_readable(vd) || !vdev_writeable(vd)) {
1829 vd->vdev_faulted = vd->vdev_degraded = 0;
1830 vd->vdev_cant_read = B_FALSE;
1831 vd->vdev_cant_write = B_FALSE;
1836 vdev_state_dirty(vd->vdev_top);
1838 if (vd->vdev_aux == NULL && !vdev_is_dead(vd))
1839 spa_async_request(spa, SPA_ASYNC_RESILVER);
1841 spa_event_notify(spa, vd, ESC_ZFS_VDEV_CLEAR);
1846 vdev_is_dead(vdev_t *vd)
1848 return (vd->vdev_state < VDEV_STATE_DEGRADED);
1852 vdev_readable(vdev_t *vd)
1854 return (!vdev_is_dead(vd) && !vd->vdev_cant_read);
1858 vdev_writeable(vdev_t *vd)
1860 return (!vdev_is_dead(vd) && !vd->vdev_cant_write);
1864 vdev_allocatable(vdev_t *vd)
1867 * We currently allow allocations from vdevs which maybe in the
1868 * process of reopening (i.e. VDEV_STATE_CLOSED). If the device
1869 * fails to reopen then we'll catch it later when we're holding
1872 return (!(vdev_is_dead(vd) && vd->vdev_state != VDEV_STATE_CLOSED) &&
1873 !vd->vdev_cant_write);
1877 vdev_accessible(vdev_t *vd, zio_t *zio)
1879 ASSERT(zio->io_vd == vd);
1881 if (vdev_is_dead(vd) || vd->vdev_remove_wanted)
1884 if (zio->io_type == ZIO_TYPE_READ)
1885 return (!vd->vdev_cant_read);
1887 if (zio->io_type == ZIO_TYPE_WRITE)
1888 return (!vd->vdev_cant_write);
1894 * Get statistics for the given vdev.
1897 vdev_get_stats(vdev_t *vd, vdev_stat_t *vs)
1899 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
1901 mutex_enter(&vd->vdev_stat_lock);
1902 bcopy(&vd->vdev_stat, vs, sizeof (*vs));
1903 vs->vs_scrub_errors = vd->vdev_spa->spa_scrub_errors;
1904 vs->vs_timestamp = gethrtime() - vs->vs_timestamp;
1905 vs->vs_state = vd->vdev_state;
1906 vs->vs_rsize = vdev_get_rsize(vd);
1907 mutex_exit(&vd->vdev_stat_lock);
1910 * If we're getting stats on the root vdev, aggregate the I/O counts
1911 * over all top-level vdevs (i.e. the direct children of the root).
1914 for (int c = 0; c < rvd->vdev_children; c++) {
1915 vdev_t *cvd = rvd->vdev_child[c];
1916 vdev_stat_t *cvs = &cvd->vdev_stat;
1918 mutex_enter(&vd->vdev_stat_lock);
1919 for (int t = 0; t < ZIO_TYPES; t++) {
1920 vs->vs_ops[t] += cvs->vs_ops[t];
1921 vs->vs_bytes[t] += cvs->vs_bytes[t];
1923 vs->vs_scrub_examined += cvs->vs_scrub_examined;
1924 mutex_exit(&vd->vdev_stat_lock);
1930 vdev_clear_stats(vdev_t *vd)
1932 mutex_enter(&vd->vdev_stat_lock);
1933 vd->vdev_stat.vs_space = 0;
1934 vd->vdev_stat.vs_dspace = 0;
1935 vd->vdev_stat.vs_alloc = 0;
1936 mutex_exit(&vd->vdev_stat_lock);
1940 vdev_stat_update(zio_t *zio, uint64_t psize)
1942 vdev_t *rvd = zio->io_spa->spa_root_vdev;
1943 vdev_t *vd = zio->io_vd ? zio->io_vd : rvd;
1945 uint64_t txg = zio->io_txg;
1946 vdev_stat_t *vs = &vd->vdev_stat;
1947 zio_type_t type = zio->io_type;
1948 int flags = zio->io_flags;
1951 * If this i/o is a gang leader, it didn't do any actual work.
1953 if (zio->io_gang_tree)
1956 if (zio->io_error == 0) {
1958 * If this is a root i/o, don't count it -- we've already
1959 * counted the top-level vdevs, and vdev_get_stats() will
1960 * aggregate them when asked. This reduces contention on
1961 * the root vdev_stat_lock and implicitly handles blocks
1962 * that compress away to holes, for which there is no i/o.
1963 * (Holes never create vdev children, so all the counters
1964 * remain zero, which is what we want.)
1966 * Note: this only applies to successful i/o (io_error == 0)
1967 * because unlike i/o counts, errors are not additive.
1968 * When reading a ditto block, for example, failure of
1969 * one top-level vdev does not imply a root-level error.
1974 ASSERT(vd == zio->io_vd);
1975 if (!(flags & ZIO_FLAG_IO_BYPASS)) {
1976 mutex_enter(&vd->vdev_stat_lock);
1978 vs->vs_bytes[type] += psize;
1979 mutex_exit(&vd->vdev_stat_lock);
1981 if (flags & ZIO_FLAG_IO_REPAIR) {
1982 ASSERT(zio->io_delegate_list == NULL);
1983 mutex_enter(&vd->vdev_stat_lock);
1984 if (flags & ZIO_FLAG_SCRUB_THREAD)
1985 vs->vs_scrub_repaired += psize;
1987 vs->vs_self_healed += psize;
1988 mutex_exit(&vd->vdev_stat_lock);
1993 if (flags & ZIO_FLAG_SPECULATIVE)
1996 mutex_enter(&vd->vdev_stat_lock);
1997 if (type == ZIO_TYPE_READ) {
1998 if (zio->io_error == ECKSUM)
1999 vs->vs_checksum_errors++;
2001 vs->vs_read_errors++;
2003 if (type == ZIO_TYPE_WRITE)
2004 vs->vs_write_errors++;
2005 mutex_exit(&vd->vdev_stat_lock);
2007 if (type == ZIO_TYPE_WRITE && txg != 0 && vd->vdev_children == 0) {
2008 if (flags & ZIO_FLAG_SCRUB_THREAD) {
2009 ASSERT(flags & ZIO_FLAG_IO_REPAIR);
2010 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2011 vdev_dtl_dirty(&pvd->vdev_dtl_scrub, txg, 1);
2013 if (!(flags & ZIO_FLAG_IO_REPAIR)) {
2014 if (vdev_dtl_contains(&vd->vdev_dtl_map, txg, 1))
2016 vdev_dirty(vd->vdev_top, VDD_DTL, vd, txg);
2017 for (pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
2018 vdev_dtl_dirty(&pvd->vdev_dtl_map, txg, 1);
2024 vdev_scrub_stat_update(vdev_t *vd, pool_scrub_type_t type, boolean_t complete)
2027 vdev_stat_t *vs = &vd->vdev_stat;
2029 for (c = 0; c < vd->vdev_children; c++)
2030 vdev_scrub_stat_update(vd->vdev_child[c], type, complete);
2032 mutex_enter(&vd->vdev_stat_lock);
2034 if (type == POOL_SCRUB_NONE) {
2036 * Update completion and end time. Leave everything else alone
2037 * so we can report what happened during the previous scrub.
2039 vs->vs_scrub_complete = complete;
2040 vs->vs_scrub_end = gethrestime_sec();
2042 vs->vs_scrub_type = type;
2043 vs->vs_scrub_complete = 0;
2044 vs->vs_scrub_examined = 0;
2045 vs->vs_scrub_repaired = 0;
2046 vs->vs_scrub_start = gethrestime_sec();
2047 vs->vs_scrub_end = 0;
2050 mutex_exit(&vd->vdev_stat_lock);
2054 * Update the in-core space usage stats for this vdev and the root vdev.
2057 vdev_space_update(vdev_t *vd, int64_t space_delta, int64_t alloc_delta,
2058 boolean_t update_root)
2060 int64_t dspace_delta = space_delta;
2061 spa_t *spa = vd->vdev_spa;
2062 vdev_t *rvd = spa->spa_root_vdev;
2064 ASSERT(vd == vd->vdev_top);
2067 * Apply the inverse of the psize-to-asize (ie. RAID-Z) space-expansion
2068 * factor. We must calculate this here and not at the root vdev
2069 * because the root vdev's psize-to-asize is simply the max of its
2070 * childrens', thus not accurate enough for us.
2072 ASSERT((dspace_delta & (SPA_MINBLOCKSIZE-1)) == 0);
2073 dspace_delta = (dspace_delta >> SPA_MINBLOCKSHIFT) *
2074 vd->vdev_deflate_ratio;
2076 mutex_enter(&vd->vdev_stat_lock);
2077 vd->vdev_stat.vs_space += space_delta;
2078 vd->vdev_stat.vs_alloc += alloc_delta;
2079 vd->vdev_stat.vs_dspace += dspace_delta;
2080 mutex_exit(&vd->vdev_stat_lock);
2083 ASSERT(rvd == vd->vdev_parent);
2084 ASSERT(vd->vdev_ms_count != 0);
2087 * Don't count non-normal (e.g. intent log) space as part of
2088 * the pool's capacity.
2090 if (vd->vdev_mg->mg_class != spa->spa_normal_class)
2093 mutex_enter(&rvd->vdev_stat_lock);
2094 rvd->vdev_stat.vs_space += space_delta;
2095 rvd->vdev_stat.vs_alloc += alloc_delta;
2096 rvd->vdev_stat.vs_dspace += dspace_delta;
2097 mutex_exit(&rvd->vdev_stat_lock);
2102 * Mark a top-level vdev's config as dirty, placing it on the dirty list
2103 * so that it will be written out next time the vdev configuration is synced.
2104 * If the root vdev is specified (vdev_top == NULL), dirty all top-level vdevs.
2107 vdev_config_dirty(vdev_t *vd)
2109 spa_t *spa = vd->vdev_spa;
2110 vdev_t *rvd = spa->spa_root_vdev;
2114 * If this is an aux vdev (as with l2cache devices), then we update the
2115 * vdev config manually and set the sync flag.
2117 if (vd->vdev_aux != NULL) {
2118 spa_aux_vdev_t *sav = vd->vdev_aux;
2122 for (c = 0; c < sav->sav_count; c++) {
2123 if (sav->sav_vdevs[c] == vd)
2127 if (c == sav->sav_count) {
2129 * We're being removed. There's nothing more to do.
2131 ASSERT(sav->sav_sync == B_TRUE);
2135 sav->sav_sync = B_TRUE;
2137 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
2138 ZPOOL_CONFIG_L2CACHE, &aux, &naux) == 0);
2143 * Setting the nvlist in the middle if the array is a little
2144 * sketchy, but it will work.
2146 nvlist_free(aux[c]);
2147 aux[c] = vdev_config_generate(spa, vd, B_TRUE, B_FALSE, B_TRUE);
2153 * The dirty list is protected by the SCL_CONFIG lock. The caller
2154 * must either hold SCL_CONFIG as writer, or must be the sync thread
2155 * (which holds SCL_CONFIG as reader). There's only one sync thread,
2156 * so this is sufficient to ensure mutual exclusion.
2158 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2159 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2160 spa_config_held(spa, SCL_CONFIG, RW_READER)));
2163 for (c = 0; c < rvd->vdev_children; c++)
2164 vdev_config_dirty(rvd->vdev_child[c]);
2166 ASSERT(vd == vd->vdev_top);
2168 if (!list_link_active(&vd->vdev_config_dirty_node))
2169 list_insert_head(&spa->spa_config_dirty_list, vd);
2174 vdev_config_clean(vdev_t *vd)
2176 spa_t *spa = vd->vdev_spa;
2178 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_WRITER) ||
2179 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2180 spa_config_held(spa, SCL_CONFIG, RW_READER)));
2182 ASSERT(list_link_active(&vd->vdev_config_dirty_node));
2183 list_remove(&spa->spa_config_dirty_list, vd);
2187 * Mark a top-level vdev's state as dirty, so that the next pass of
2188 * spa_sync() can convert this into vdev_config_dirty(). We distinguish
2189 * the state changes from larger config changes because they require
2190 * much less locking, and are often needed for administrative actions.
2193 vdev_state_dirty(vdev_t *vd)
2195 spa_t *spa = vd->vdev_spa;
2197 ASSERT(vd == vd->vdev_top);
2200 * The state list is protected by the SCL_STATE lock. The caller
2201 * must either hold SCL_STATE as writer, or must be the sync thread
2202 * (which holds SCL_STATE as reader). There's only one sync thread,
2203 * so this is sufficient to ensure mutual exclusion.
2205 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2206 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2207 spa_config_held(spa, SCL_STATE, RW_READER)));
2209 if (!list_link_active(&vd->vdev_state_dirty_node))
2210 list_insert_head(&spa->spa_state_dirty_list, vd);
2214 vdev_state_clean(vdev_t *vd)
2216 spa_t *spa = vd->vdev_spa;
2218 ASSERT(spa_config_held(spa, SCL_STATE, RW_WRITER) ||
2219 (dsl_pool_sync_context(spa_get_dsl(spa)) &&
2220 spa_config_held(spa, SCL_STATE, RW_READER)));
2222 ASSERT(list_link_active(&vd->vdev_state_dirty_node));
2223 list_remove(&spa->spa_state_dirty_list, vd);
2227 * Propagate vdev state up from children to parent.
2230 vdev_propagate_state(vdev_t *vd)
2232 vdev_t *rvd = vd->vdev_spa->spa_root_vdev;
2233 int degraded = 0, faulted = 0;
2238 if (vd->vdev_children > 0) {
2239 for (c = 0; c < vd->vdev_children; c++) {
2240 child = vd->vdev_child[c];
2242 if (!vdev_readable(child) ||
2243 (!vdev_writeable(child) && (spa_mode & FWRITE))) {
2245 * Root special: if there is a top-level log
2246 * device, treat the root vdev as if it were
2249 if (child->vdev_islog && vd == rvd)
2253 } else if (child->vdev_state <= VDEV_STATE_DEGRADED) {
2257 if (child->vdev_stat.vs_aux == VDEV_AUX_CORRUPT_DATA)
2261 vd->vdev_ops->vdev_op_state_change(vd, faulted, degraded);
2264 * Root special: if there is a top-level vdev that cannot be
2265 * opened due to corrupted metadata, then propagate the root
2266 * vdev's aux state as 'corrupt' rather than 'insufficient
2269 if (corrupted && vd == rvd &&
2270 rvd->vdev_state == VDEV_STATE_CANT_OPEN)
2271 vdev_set_state(rvd, B_FALSE, VDEV_STATE_CANT_OPEN,
2272 VDEV_AUX_CORRUPT_DATA);
2275 if (vd->vdev_parent)
2276 vdev_propagate_state(vd->vdev_parent);
2280 * Set a vdev's state. If this is during an open, we don't update the parent
2281 * state, because we're in the process of opening children depth-first.
2282 * Otherwise, we propagate the change to the parent.
2284 * If this routine places a device in a faulted state, an appropriate ereport is
2288 vdev_set_state(vdev_t *vd, boolean_t isopen, vdev_state_t state, vdev_aux_t aux)
2290 uint64_t save_state;
2291 spa_t *spa = vd->vdev_spa;
2293 if (state == vd->vdev_state) {
2294 vd->vdev_stat.vs_aux = aux;
2298 save_state = vd->vdev_state;
2300 vd->vdev_state = state;
2301 vd->vdev_stat.vs_aux = aux;
2304 * If we are setting the vdev state to anything but an open state, then
2305 * always close the underlying device. Otherwise, we keep accessible
2306 * but invalid devices open forever. We don't call vdev_close() itself,
2307 * because that implies some extra checks (offline, etc) that we don't
2308 * want here. This is limited to leaf devices, because otherwise
2309 * closing the device will affect other children.
2311 if (vdev_is_dead(vd) && vd->vdev_ops->vdev_op_leaf)
2312 vd->vdev_ops->vdev_op_close(vd);
2314 if (vd->vdev_removed &&
2315 state == VDEV_STATE_CANT_OPEN &&
2316 (aux == VDEV_AUX_OPEN_FAILED || vd->vdev_checkremove)) {
2318 * If the previous state is set to VDEV_STATE_REMOVED, then this
2319 * device was previously marked removed and someone attempted to
2320 * reopen it. If this failed due to a nonexistent device, then
2321 * keep the device in the REMOVED state. We also let this be if
2322 * it is one of our special test online cases, which is only
2323 * attempting to online the device and shouldn't generate an FMA
2326 vd->vdev_state = VDEV_STATE_REMOVED;
2327 vd->vdev_stat.vs_aux = VDEV_AUX_NONE;
2328 } else if (state == VDEV_STATE_REMOVED) {
2330 * Indicate to the ZFS DE that this device has been removed, and
2331 * any recent errors should be ignored.
2333 zfs_post_remove(spa, vd);
2334 vd->vdev_removed = B_TRUE;
2335 } else if (state == VDEV_STATE_CANT_OPEN) {
2337 * If we fail to open a vdev during an import, we mark it as
2338 * "not available", which signifies that it was never there to
2339 * begin with. Failure to open such a device is not considered
2342 if (spa->spa_load_state == SPA_LOAD_IMPORT &&
2343 !spa->spa_import_faulted &&
2344 vd->vdev_ops->vdev_op_leaf)
2345 vd->vdev_not_present = 1;
2348 * Post the appropriate ereport. If the 'prevstate' field is
2349 * set to something other than VDEV_STATE_UNKNOWN, it indicates
2350 * that this is part of a vdev_reopen(). In this case, we don't
2351 * want to post the ereport if the device was already in the
2352 * CANT_OPEN state beforehand.
2354 * If the 'checkremove' flag is set, then this is an attempt to
2355 * online the device in response to an insertion event. If we
2356 * hit this case, then we have detected an insertion event for a
2357 * faulted or offline device that wasn't in the removed state.
2358 * In this scenario, we don't post an ereport because we are
2359 * about to replace the device, or attempt an online with
2360 * vdev_forcefault, which will generate the fault for us.
2362 if ((vd->vdev_prevstate != state || vd->vdev_forcefault) &&
2363 !vd->vdev_not_present && !vd->vdev_checkremove &&
2364 vd != spa->spa_root_vdev) {
2368 case VDEV_AUX_OPEN_FAILED:
2369 class = FM_EREPORT_ZFS_DEVICE_OPEN_FAILED;
2371 case VDEV_AUX_CORRUPT_DATA:
2372 class = FM_EREPORT_ZFS_DEVICE_CORRUPT_DATA;
2374 case VDEV_AUX_NO_REPLICAS:
2375 class = FM_EREPORT_ZFS_DEVICE_NO_REPLICAS;
2377 case VDEV_AUX_BAD_GUID_SUM:
2378 class = FM_EREPORT_ZFS_DEVICE_BAD_GUID_SUM;
2380 case VDEV_AUX_TOO_SMALL:
2381 class = FM_EREPORT_ZFS_DEVICE_TOO_SMALL;
2383 case VDEV_AUX_BAD_LABEL:
2384 class = FM_EREPORT_ZFS_DEVICE_BAD_LABEL;
2386 case VDEV_AUX_IO_FAILURE:
2387 class = FM_EREPORT_ZFS_IO_FAILURE;
2390 class = FM_EREPORT_ZFS_DEVICE_UNKNOWN;
2393 zfs_ereport_post(class, spa, vd, NULL, save_state, 0);
2396 /* Erase any notion of persistent removed state */
2397 vd->vdev_removed = B_FALSE;
2399 vd->vdev_removed = B_FALSE;
2403 vdev_propagate_state(vd);
2407 * Check the vdev configuration to ensure that it's capable of supporting
2410 * On Solaris, we do not support RAID-Z or partial configuration. In
2411 * addition, only a single top-level vdev is allowed and none of the
2412 * leaves can be wholedisks.
2414 * For FreeBSD, we can boot from any configuration. There is a
2415 * limitation that the boot filesystem must be either uncompressed or
2416 * compresses with lzjb compression but I'm not sure how to enforce
2420 vdev_is_bootable(vdev_t *vd)
2422 #ifdef __FreeBSD_version
2427 if (!vd->vdev_ops->vdev_op_leaf) {
2428 char *vdev_type = vd->vdev_ops->vdev_op_type;
2430 if (strcmp(vdev_type, VDEV_TYPE_ROOT) == 0 &&
2431 vd->vdev_children > 1) {
2433 } else if (strcmp(vdev_type, VDEV_TYPE_RAIDZ) == 0 ||
2434 strcmp(vdev_type, VDEV_TYPE_MISSING) == 0) {
2437 } else if (vd->vdev_wholedisk == 1) {
2441 for (c = 0; c < vd->vdev_children; c++) {
2442 if (!vdev_is_bootable(vd->vdev_child[c]))