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 --- 4 unchanged lines hidden (view full) --- 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 * --- 85 unchanged lines hidden (view full) --- 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 */ --- 291 unchanged lines hidden (view full) --- 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. For vdevs 437 * which don't have a txg value stored on their label (i.e. spares/cache) 438 * or have not been completely initialized (txg = 0) just return 439 * the configuration from the first valid label we find. Otherwise, 440 * find the most up-to-date label that does not exceed the specified 441 * 'txg' value. 442 */ |
443nvlist_t * |
444vdev_label_read_config(vdev_t *vd, uint64_t txg) |
445{ 446 spa_t *spa = vd->vdev_spa; 447 nvlist_t *config = NULL; 448 vdev_phys_t *vp; 449 zio_t *zio; |
450 uint64_t best_txg = 0; 451 int error = 0; |
452 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 453 ZIO_FLAG_SPECULATIVE; 454 455 ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL); 456 457 if (!vdev_readable(vd)) 458 return (NULL); 459 460 vp = zio_buf_alloc(sizeof (vdev_phys_t)); 461 462retry: 463 for (int l = 0; l < VDEV_LABELS; l++) { |
464 nvlist_t *label = NULL; |
465 466 zio = zio_root(spa, NULL, NULL, flags); 467 468 vdev_label_read(zio, vd, l, vp, 469 offsetof(vdev_label_t, vl_vdev_phys), 470 sizeof (vdev_phys_t), NULL, NULL, flags); 471 472 if (zio_wait(zio) == 0 && 473 nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist), |
474 &label, 0) == 0) { 475 uint64_t label_txg = 0; |
476 |
477 /* 478 * Auxiliary vdevs won't have txg values in their 479 * labels and newly added vdevs may not have been 480 * completely initialized so just return the 481 * configuration from the first valid label we 482 * encounter. 483 */ 484 error = nvlist_lookup_uint64(label, 485 ZPOOL_CONFIG_POOL_TXG, &label_txg); 486 if ((error || label_txg == 0) && !config) { 487 config = label; 488 break; 489 } else if (label_txg <= txg && label_txg > best_txg) { 490 best_txg = label_txg; 491 nvlist_free(config); 492 config = fnvlist_dup(label); 493 } |
494 } |
495 496 if (label != NULL) { 497 nvlist_free(label); 498 label = NULL; 499 } |
500 } 501 502 if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) { 503 flags |= ZIO_FLAG_TRYHARD; 504 goto retry; 505 } 506 507 zio_buf_free(vp, sizeof (vdev_phys_t)); --- 17 unchanged lines hidden (view full) --- 525 if (spare_guid) 526 *spare_guid = 0ULL; 527 if (l2cache_guid) 528 *l2cache_guid = 0ULL; 529 530 /* 531 * Read the label, if any, and perform some basic sanity checks. 532 */ |
533 if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) |
534 return (B_FALSE); 535 536 (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG, 537 &vdtxg); 538 539 if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE, 540 &state) != 0 || 541 nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID, --- 320 unchanged lines hidden (view full) --- 862 */ 863 864/* 865 * Consider the following situation: txg is safely synced to disk. We've 866 * written the first uberblock for txg + 1, and then we lose power. When we 867 * come back up, we fail to see the uberblock for txg + 1 because, say, 868 * it was on a mirrored device and the replica to which we wrote txg + 1 869 * is now offline. If we then make some changes and sync txg + 1, and then |
870 * the missing replica comes back, then for a few seconds we'll have two |
871 * conflicting uberblocks on disk with the same txg. The solution is simple: 872 * among uberblocks with equal txg, choose the one with the latest timestamp. 873 */ 874static int 875vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2) 876{ 877 if (ub1->ub_txg < ub2->ub_txg) 878 return (-1); 879 if (ub1->ub_txg > ub2->ub_txg) 880 return (1); 881 882 if (ub1->ub_timestamp < ub2->ub_timestamp) 883 return (-1); 884 if (ub1->ub_timestamp > ub2->ub_timestamp) 885 return (1); 886 887 return (0); 888} 889 |
890struct ubl_cbdata { 891 uberblock_t *ubl_ubbest; /* Best uberblock */ 892 vdev_t *ubl_vd; /* vdev associated with the above */ 893}; 894 |
895static void 896vdev_uberblock_load_done(zio_t *zio) 897{ |
898 vdev_t *vd = zio->io_vd; |
899 spa_t *spa = zio->io_spa; 900 zio_t *rio = zio->io_private; 901 uberblock_t *ub = zio->io_data; |
902 struct ubl_cbdata *cbp = rio->io_private; |
903 |
904 ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd)); |
905 906 if (zio->io_error == 0 && uberblock_verify(ub) == 0) { 907 mutex_enter(&rio->io_lock); 908 if (ub->ub_txg <= spa->spa_load_max_txg && |
909 vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) { 910 /* 911 * Keep track of the vdev in which this uberblock 912 * was found. We will use this information later 913 * to obtain the config nvlist associated with 914 * this uberblock. 915 */ 916 *cbp->ubl_ubbest = *ub; 917 cbp->ubl_vd = vd; 918 } |
919 mutex_exit(&rio->io_lock); 920 } 921 922 zio_buf_free(zio->io_data, zio->io_size); 923} 924 |
925static void 926vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags, 927 struct ubl_cbdata *cbp) |
928{ |
929 for (int c = 0; c < vd->vdev_children; c++) |
930 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp); |
931 932 if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) { 933 for (int l = 0; l < VDEV_LABELS; l++) { 934 for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) { 935 vdev_label_read(zio, vd, l, 936 zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)), 937 VDEV_UBERBLOCK_OFFSET(vd, n), 938 VDEV_UBERBLOCK_SIZE(vd), 939 vdev_uberblock_load_done, zio, flags); 940 } 941 } 942 } |
943} |
944 |
945/* 946 * Reads the 'best' uberblock from disk along with its associated 947 * configuration. First, we read the uberblock array of each label of each 948 * vdev, keeping track of the uberblock with the highest txg in each array. 949 * Then, we read the configuration from the same vdev as the best uberblock. 950 */ 951void 952vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config) 953{ 954 zio_t *zio; 955 spa_t *spa = rvd->vdev_spa; 956 struct ubl_cbdata cb; 957 int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL | 958 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD; 959 960 ASSERT(ub); 961 ASSERT(config); 962 963 bzero(ub, sizeof (uberblock_t)); 964 *config = NULL; 965 966 cb.ubl_ubbest = ub; 967 cb.ubl_vd = NULL; 968 969 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER); 970 zio = zio_root(spa, NULL, &cb, flags); 971 vdev_uberblock_load_impl(zio, rvd, flags, &cb); 972 (void) zio_wait(zio); 973 974 /* 975 * It's possible that the best uberblock was discovered on a label 976 * that has a configuration which was written in a future txg. 977 * Search all labels on this vdev to find the configuration that 978 * matches the txg for our uberblock. 979 */ 980 if (cb.ubl_vd != NULL) 981 *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg); 982 spa_config_exit(spa, SCL_ALL, FTAG); |
983} 984 985/* 986 * On success, increment root zio's count of good writes. 987 * We only get credit for writes to known-visible vdevs; see spa_vdev_add(). 988 */ 989static void 990vdev_uberblock_sync_done(zio_t *zio) --- 296 unchanged lines hidden --- |