1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 /*
26 * Virtual Device Labels
27 * ---------------------
28 *
29 * The vdev label serves several distinct purposes:
30 *
31 * 1. Uniquely identify this device as part of a ZFS pool and confirm its
32 * identity within the pool.
33 *
34 * 2. Verify that all the devices given in a configuration are present
35 * within the pool.
36 *
37 * 3. Determine the uberblock for the pool.
38 *
39 * 4. In case of an import operation, determine the configuration of the
40 * toplevel vdev of which it is a part.
41 *
42 * 5. If an import operation cannot find all the devices in the pool,
104 * properties, per-vdev properties, and configuration information. It is
105 * described in more detail below.
106 *
107 * The latter half of the label consists of a redundant array of uberblocks.
108 * These uberblocks are updated whenever a transaction group is committed,
109 * or when the configuration is updated. When a pool is loaded, we scan each
110 * vdev for the 'best' uberblock.
111 *
112 *
113 * Configuration Information
114 * -------------------------
115 *
116 * The nvlist describing the pool and vdev contains the following elements:
117 *
118 * version ZFS on-disk version
119 * name Pool name
120 * state Pool state
121 * txg Transaction group in which this label was written
122 * pool_guid Unique identifier for this pool
123 * vdev_tree An nvlist describing vdev tree.
124 *
125 * Each leaf device label also contains the following:
126 *
127 * top_guid Unique ID for top-level vdev in which this is contained
128 * guid Unique ID for the leaf vdev
129 *
130 * The 'vs' configuration follows the format described in 'spa_config.c'.
131 */
132
133 #include <sys/zfs_context.h>
134 #include <sys/spa.h>
135 #include <sys/spa_impl.h>
136 #include <sys/dmu.h>
137 #include <sys/zap.h>
138 #include <sys/vdev.h>
139 #include <sys/vdev_impl.h>
140 #include <sys/uberblock_impl.h>
141 #include <sys/metaslab.h>
142 #include <sys/zio.h>
143 #include <sys/dsl_scan.h>
411 array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
412
413 for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
414 vdev_t *tvd = rvd->vdev_child[c];
415
416 if (tvd->vdev_ishole)
417 array[idx++] = c;
418 }
419
420 if (idx) {
421 VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
422 array, idx) == 0);
423 }
424
425 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
426 rvd->vdev_children) == 0);
427
428 kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
429 }
430
431 nvlist_t *
432 vdev_label_read_config(vdev_t *vd)
433 {
434 spa_t *spa = vd->vdev_spa;
435 nvlist_t *config = NULL;
436 vdev_phys_t *vp;
437 zio_t *zio;
438 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
439 ZIO_FLAG_SPECULATIVE;
440
441 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
442
443 if (!vdev_readable(vd))
444 return (NULL);
445
446 vp = zio_buf_alloc(sizeof (vdev_phys_t));
447
448 retry:
449 for (int l = 0; l < VDEV_LABELS; l++) {
450
451 zio = zio_root(spa, NULL, NULL, flags);
452
453 vdev_label_read(zio, vd, l, vp,
454 offsetof(vdev_label_t, vl_vdev_phys),
455 sizeof (vdev_phys_t), NULL, NULL, flags);
456
457 if (zio_wait(zio) == 0 &&
458 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
459 &config, 0) == 0)
460 break;
461
462 if (config != NULL) {
463 nvlist_free(config);
464 config = NULL;
465 }
466 }
467
468 if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
469 flags |= ZIO_FLAG_TRYHARD;
479 * Determine if a device is in use. The 'spare_guid' parameter will be filled
480 * in with the device guid if this spare is active elsewhere on the system.
481 */
482 static boolean_t
483 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
484 uint64_t *spare_guid, uint64_t *l2cache_guid)
485 {
486 spa_t *spa = vd->vdev_spa;
487 uint64_t state, pool_guid, device_guid, txg, spare_pool;
488 uint64_t vdtxg = 0;
489 nvlist_t *label;
490
491 if (spare_guid)
492 *spare_guid = 0ULL;
493 if (l2cache_guid)
494 *l2cache_guid = 0ULL;
495
496 /*
497 * Read the label, if any, and perform some basic sanity checks.
498 */
499 if ((label = vdev_label_read_config(vd)) == NULL)
500 return (B_FALSE);
501
502 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
503 &vdtxg);
504
505 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
506 &state) != 0 ||
507 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
508 &device_guid) != 0) {
509 nvlist_free(label);
510 return (B_FALSE);
511 }
512
513 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
514 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
515 &pool_guid) != 0 ||
516 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
517 &txg) != 0)) {
518 nvlist_free(label);
519 return (B_FALSE);
816 if (error == 0 && !vd->vdev_isl2cache &&
817 (reason == VDEV_LABEL_L2CACHE ||
818 spa_l2cache_exists(vd->vdev_guid, NULL)))
819 spa_l2cache_add(vd);
820
821 return (error);
822 }
823
824 /*
825 * ==========================================================================
826 * uberblock load/sync
827 * ==========================================================================
828 */
829
830 /*
831 * Consider the following situation: txg is safely synced to disk. We've
832 * written the first uberblock for txg + 1, and then we lose power. When we
833 * come back up, we fail to see the uberblock for txg + 1 because, say,
834 * it was on a mirrored device and the replica to which we wrote txg + 1
835 * is now offline. If we then make some changes and sync txg + 1, and then
836 * the missing replica comes back, then for a new seconds we'll have two
837 * conflicting uberblocks on disk with the same txg. The solution is simple:
838 * among uberblocks with equal txg, choose the one with the latest timestamp.
839 */
840 static int
841 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
842 {
843 if (ub1->ub_txg < ub2->ub_txg)
844 return (-1);
845 if (ub1->ub_txg > ub2->ub_txg)
846 return (1);
847
848 if (ub1->ub_timestamp < ub2->ub_timestamp)
849 return (-1);
850 if (ub1->ub_timestamp > ub2->ub_timestamp)
851 return (1);
852
853 return (0);
854 }
855
856 static void
857 vdev_uberblock_load_done(zio_t *zio)
858 {
859 spa_t *spa = zio->io_spa;
860 zio_t *rio = zio->io_private;
861 uberblock_t *ub = zio->io_data;
862 uberblock_t *ubbest = rio->io_private;
863
864 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(zio->io_vd));
865
866 if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
867 mutex_enter(&rio->io_lock);
868 if (ub->ub_txg <= spa->spa_load_max_txg &&
869 vdev_uberblock_compare(ub, ubbest) > 0)
870 *ubbest = *ub;
871 mutex_exit(&rio->io_lock);
872 }
873
874 zio_buf_free(zio->io_data, zio->io_size);
875 }
876
877 void
878 vdev_uberblock_load(zio_t *zio, vdev_t *vd, uberblock_t *ubbest)
879 {
880 spa_t *spa = vd->vdev_spa;
881 vdev_t *rvd = spa->spa_root_vdev;
882 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
883 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
884
885 if (vd == rvd) {
886 ASSERT(zio == NULL);
887 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
888 zio = zio_root(spa, NULL, ubbest, flags);
889 bzero(ubbest, sizeof (uberblock_t));
890 }
891
892 ASSERT(zio != NULL);
893
894 for (int c = 0; c < vd->vdev_children; c++)
895 vdev_uberblock_load(zio, vd->vdev_child[c], ubbest);
896
897 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
898 for (int l = 0; l < VDEV_LABELS; l++) {
899 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
900 vdev_label_read(zio, vd, l,
901 zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
902 VDEV_UBERBLOCK_OFFSET(vd, n),
903 VDEV_UBERBLOCK_SIZE(vd),
904 vdev_uberblock_load_done, zio, flags);
905 }
906 }
907 }
908
909 if (vd == rvd) {
910 (void) zio_wait(zio);
911 spa_config_exit(spa, SCL_ALL, FTAG);
912 }
913 }
914
915 /*
916 * On success, increment root zio's count of good writes.
917 * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
918 */
919 static void
920 vdev_uberblock_sync_done(zio_t *zio)
921 {
922 uint64_t *good_writes = zio->io_private;
923
924 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
925 atomic_add_64(good_writes, 1);
926 }
927
928 /*
929 * Write the uberblock to all labels of all leaves of the specified vdev.
930 */
931 static void
932 vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
|
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
25 */
26
27 /*
28 * Virtual Device Labels
29 * ---------------------
30 *
31 * The vdev label serves several distinct purposes:
32 *
33 * 1. Uniquely identify this device as part of a ZFS pool and confirm its
34 * identity within the pool.
35 *
36 * 2. Verify that all the devices given in a configuration are present
37 * within the pool.
38 *
39 * 3. Determine the uberblock for the pool.
40 *
41 * 4. In case of an import operation, determine the configuration of the
42 * toplevel vdev of which it is a part.
43 *
44 * 5. If an import operation cannot find all the devices in the pool,
106 * properties, per-vdev properties, and configuration information. It is
107 * described in more detail below.
108 *
109 * The latter half of the label consists of a redundant array of uberblocks.
110 * These uberblocks are updated whenever a transaction group is committed,
111 * or when the configuration is updated. When a pool is loaded, we scan each
112 * vdev for the 'best' uberblock.
113 *
114 *
115 * Configuration Information
116 * -------------------------
117 *
118 * The nvlist describing the pool and vdev contains the following elements:
119 *
120 * version ZFS on-disk version
121 * name Pool name
122 * state Pool state
123 * txg Transaction group in which this label was written
124 * pool_guid Unique identifier for this pool
125 * vdev_tree An nvlist describing vdev tree.
126 * features_for_read
127 * An nvlist of the features necessary for reading the MOS.
128 *
129 * Each leaf device label also contains the following:
130 *
131 * top_guid Unique ID for top-level vdev in which this is contained
132 * guid Unique ID for the leaf vdev
133 *
134 * The 'vs' configuration follows the format described in 'spa_config.c'.
135 */
136
137 #include <sys/zfs_context.h>
138 #include <sys/spa.h>
139 #include <sys/spa_impl.h>
140 #include <sys/dmu.h>
141 #include <sys/zap.h>
142 #include <sys/vdev.h>
143 #include <sys/vdev_impl.h>
144 #include <sys/uberblock_impl.h>
145 #include <sys/metaslab.h>
146 #include <sys/zio.h>
147 #include <sys/dsl_scan.h>
415 array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
416
417 for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
418 vdev_t *tvd = rvd->vdev_child[c];
419
420 if (tvd->vdev_ishole)
421 array[idx++] = c;
422 }
423
424 if (idx) {
425 VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
426 array, idx) == 0);
427 }
428
429 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
430 rvd->vdev_children) == 0);
431
432 kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
433 }
434
435 /*
436 * Returns the configuration from the label of the given vdev. If 'label' is
437 * VDEV_BEST_LABEL, each label of the vdev will be read until a valid
438 * configuration is found; otherwise, only the specified label will be read.
439 */
440 nvlist_t *
441 vdev_label_read_config(vdev_t *vd, int label)
442 {
443 spa_t *spa = vd->vdev_spa;
444 nvlist_t *config = NULL;
445 vdev_phys_t *vp;
446 zio_t *zio;
447 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
448 ZIO_FLAG_SPECULATIVE;
449
450 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
451
452 if (!vdev_readable(vd))
453 return (NULL);
454
455 vp = zio_buf_alloc(sizeof (vdev_phys_t));
456
457 retry:
458 for (int l = 0; l < VDEV_LABELS; l++) {
459 if (label >= 0 && label < VDEV_LABELS && label != l)
460 continue;
461
462 zio = zio_root(spa, NULL, NULL, flags);
463
464 vdev_label_read(zio, vd, l, vp,
465 offsetof(vdev_label_t, vl_vdev_phys),
466 sizeof (vdev_phys_t), NULL, NULL, flags);
467
468 if (zio_wait(zio) == 0 &&
469 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
470 &config, 0) == 0)
471 break;
472
473 if (config != NULL) {
474 nvlist_free(config);
475 config = NULL;
476 }
477 }
478
479 if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
480 flags |= ZIO_FLAG_TRYHARD;
490 * Determine if a device is in use. The 'spare_guid' parameter will be filled
491 * in with the device guid if this spare is active elsewhere on the system.
492 */
493 static boolean_t
494 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
495 uint64_t *spare_guid, uint64_t *l2cache_guid)
496 {
497 spa_t *spa = vd->vdev_spa;
498 uint64_t state, pool_guid, device_guid, txg, spare_pool;
499 uint64_t vdtxg = 0;
500 nvlist_t *label;
501
502 if (spare_guid)
503 *spare_guid = 0ULL;
504 if (l2cache_guid)
505 *l2cache_guid = 0ULL;
506
507 /*
508 * Read the label, if any, and perform some basic sanity checks.
509 */
510 if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) == NULL)
511 return (B_FALSE);
512
513 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
514 &vdtxg);
515
516 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
517 &state) != 0 ||
518 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
519 &device_guid) != 0) {
520 nvlist_free(label);
521 return (B_FALSE);
522 }
523
524 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
525 (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
526 &pool_guid) != 0 ||
527 nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
528 &txg) != 0)) {
529 nvlist_free(label);
530 return (B_FALSE);
827 if (error == 0 && !vd->vdev_isl2cache &&
828 (reason == VDEV_LABEL_L2CACHE ||
829 spa_l2cache_exists(vd->vdev_guid, NULL)))
830 spa_l2cache_add(vd);
831
832 return (error);
833 }
834
835 /*
836 * ==========================================================================
837 * uberblock load/sync
838 * ==========================================================================
839 */
840
841 /*
842 * Consider the following situation: txg is safely synced to disk. We've
843 * written the first uberblock for txg + 1, and then we lose power. When we
844 * come back up, we fail to see the uberblock for txg + 1 because, say,
845 * it was on a mirrored device and the replica to which we wrote txg + 1
846 * is now offline. If we then make some changes and sync txg + 1, and then
847 * the missing replica comes back, then for a few seconds we'll have two
848 * conflicting uberblocks on disk with the same txg. The solution is simple:
849 * among uberblocks with equal txg, choose the one with the latest timestamp.
850 */
851 static int
852 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
853 {
854 if (ub1->ub_txg < ub2->ub_txg)
855 return (-1);
856 if (ub1->ub_txg > ub2->ub_txg)
857 return (1);
858
859 if (ub1->ub_timestamp < ub2->ub_timestamp)
860 return (-1);
861 if (ub1->ub_timestamp > ub2->ub_timestamp)
862 return (1);
863
864 return (0);
865 }
866
867 struct ubl_cbdata {
868 uberblock_t *ubl_ubbest; /* Best uberblock */
869 vdev_t *ubl_vd; /* vdev associated with the above */
870 int ubl_label; /* Label associated with the above */
871 };
872
873 static void
874 vdev_uberblock_load_done(zio_t *zio)
875 {
876 vdev_t *vd = zio->io_vd;
877 spa_t *spa = zio->io_spa;
878 zio_t *rio = zio->io_private;
879 uberblock_t *ub = zio->io_data;
880 struct ubl_cbdata *cbp = rio->io_private;
881
882 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
883
884 if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
885 mutex_enter(&rio->io_lock);
886 if (ub->ub_txg <= spa->spa_load_max_txg &&
887 vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
888 /*
889 * Keep track of the vdev and label in which this
890 * uberblock was found. We will use this information
891 * later to obtain the config nvlist associated with
892 * this uberblock.
893 */
894 *cbp->ubl_ubbest = *ub;
895 cbp->ubl_vd = vd;
896 cbp->ubl_label = vdev_label_number(vd->vdev_psize,
897 zio->io_offset);
898 }
899 mutex_exit(&rio->io_lock);
900 }
901
902 zio_buf_free(zio->io_data, zio->io_size);
903 }
904
905 static void
906 vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
907 struct ubl_cbdata *cbp)
908 {
909 for (int c = 0; c < vd->vdev_children; c++)
910 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
911
912 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
913 for (int l = 0; l < VDEV_LABELS; l++) {
914 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
915 vdev_label_read(zio, vd, l,
916 zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
917 VDEV_UBERBLOCK_OFFSET(vd, n),
918 VDEV_UBERBLOCK_SIZE(vd),
919 vdev_uberblock_load_done, zio, flags);
920 }
921 }
922 }
923 }
924
925 /*
926 * Reads the 'best' uberblock from disk along with its associated
927 * configuration. First, we read the uberblock array of each label of each
928 * vdev, keeping track of the uberblock with the highest txg in each array.
929 * Then, we read the configuration from the same label as the best uberblock.
930 */
931 void
932 vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
933 {
934 int i;
935 zio_t *zio;
936 spa_t *spa = rvd->vdev_spa;
937 struct ubl_cbdata cb;
938 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
939 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
940
941 ASSERT(ub);
942 ASSERT(config);
943
944 bzero(ub, sizeof (uberblock_t));
945 *config = NULL;
946
947 cb.ubl_ubbest = ub;
948 cb.ubl_vd = NULL;
949
950 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
951 zio = zio_root(spa, NULL, &cb, flags);
952 vdev_uberblock_load_impl(zio, rvd, flags, &cb);
953 (void) zio_wait(zio);
954 if (cb.ubl_vd != NULL) {
955 for (i = cb.ubl_label % 2; i < VDEV_LABELS; i += 2) {
956 *config = vdev_label_read_config(cb.ubl_vd, i);
957 if (*config != NULL)
958 break;
959 }
960 }
961 spa_config_exit(spa, SCL_ALL, FTAG);
962 }
963
964 /*
965 * On success, increment root zio's count of good writes.
966 * We only get credit for writes to known-visible vdevs; see spa_vdev_add().
967 */
968 static void
969 vdev_uberblock_sync_done(zio_t *zio)
970 {
971 uint64_t *good_writes = zio->io_private;
972
973 if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
974 atomic_add_64(good_writes, 1);
975 }
976
977 /*
978 * Write the uberblock to all labels of all leaves of the specified vdev.
979 */
980 static void
981 vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
|