FreeBSD ZFS
The Zettabyte File System

vdev_label.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 
00022 /*
00023  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
00024  * Copyright (c) 2012 by Delphix. All rights reserved.
00025  */
00026 
00134 #include <sys/zfs_context.h>
00135 #include <sys/spa.h>
00136 #include <sys/spa_impl.h>
00137 #include <sys/dmu.h>
00138 #include <sys/zap.h>
00139 #include <sys/vdev.h>
00140 #include <sys/vdev_impl.h>
00141 #include <sys/uberblock_impl.h>
00142 #include <sys/metaslab.h>
00143 #include <sys/zio.h>
00144 #include <sys/dsl_scan.h>
00145 #include <sys/trim_map.h>
00146 #include <sys/fs/zfs.h>
00147 
00152 uint64_t
00153 vdev_label_offset(uint64_t psize, int l, uint64_t offset)
00154 {
00155         ASSERT(offset < sizeof (vdev_label_t));
00156         ASSERT(P2PHASE_TYPED(psize, sizeof (vdev_label_t), uint64_t) == 0);
00157 
00158         return (offset + l * sizeof (vdev_label_t) + (l < VDEV_LABELS / 2 ?
00159             0 : psize - VDEV_LABELS * sizeof (vdev_label_t)));
00160 }
00161 
00165 int
00166 vdev_label_number(uint64_t psize, uint64_t offset)
00167 {
00168         int l;
00169 
00170         if (offset >= psize - VDEV_LABEL_END_SIZE) {
00171                 offset -= psize - VDEV_LABEL_END_SIZE;
00172                 offset += (VDEV_LABELS / 2) * sizeof (vdev_label_t);
00173         }
00174         l = offset / sizeof (vdev_label_t);
00175         return (l < VDEV_LABELS ? l : -1);
00176 }
00177 
00178 static void
00179 vdev_label_read(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
00180         uint64_t size, zio_done_func_t *done, void *private, int flags)
00181 {
00182         ASSERT(spa_config_held(zio->io_spa, SCL_STATE_ALL, RW_WRITER) ==
00183             SCL_STATE_ALL);
00184         ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
00185 
00186         zio_nowait(zio_read_phys(zio, vd,
00187             vdev_label_offset(vd->vdev_psize, l, offset),
00188             size, buf, ZIO_CHECKSUM_LABEL, done, private,
00189             ZIO_PRIORITY_SYNC_READ, flags, B_TRUE));
00190 }
00191 
00192 static void
00193 vdev_label_write(zio_t *zio, vdev_t *vd, int l, void *buf, uint64_t offset,
00194         uint64_t size, zio_done_func_t *done, void *private, int flags)
00195 {
00196         ASSERT(spa_config_held(zio->io_spa, SCL_ALL, RW_WRITER) == SCL_ALL ||
00197             (spa_config_held(zio->io_spa, SCL_CONFIG | SCL_STATE, RW_READER) ==
00198             (SCL_CONFIG | SCL_STATE) &&
00199             dsl_pool_sync_context(spa_get_dsl(zio->io_spa))));
00200         ASSERT(flags & ZIO_FLAG_CONFIG_WRITER);
00201 
00202         zio_nowait(zio_write_phys(zio, vd,
00203             vdev_label_offset(vd->vdev_psize, l, offset),
00204             size, buf, ZIO_CHECKSUM_LABEL, done, private,
00205             ZIO_PRIORITY_SYNC_WRITE, flags, B_TRUE));
00206 }
00207 
00211 nvlist_t *
00212 vdev_config_generate(spa_t *spa, vdev_t *vd, boolean_t getstats,
00213     vdev_config_flag_t flags)
00214 {
00215         nvlist_t *nv = NULL;
00216 
00217         VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
00218 
00219         VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_TYPE,
00220             vd->vdev_ops->vdev_op_type) == 0);
00221         if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)))
00222                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ID, vd->vdev_id)
00223                     == 0);
00224         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_GUID, vd->vdev_guid) == 0);
00225 
00226         if (vd->vdev_path != NULL)
00227                 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PATH,
00228                     vd->vdev_path) == 0);
00229 
00230         if (vd->vdev_devid != NULL)
00231                 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_DEVID,
00232                     vd->vdev_devid) == 0);
00233 
00234         if (vd->vdev_physpath != NULL)
00235                 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_PHYS_PATH,
00236                     vd->vdev_physpath) == 0);
00237 
00238         if (vd->vdev_fru != NULL)
00239                 VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_FRU,
00240                     vd->vdev_fru) == 0);
00241 
00242         if (vd->vdev_nparity != 0) {
00243                 ASSERT(strcmp(vd->vdev_ops->vdev_op_type,
00244                     VDEV_TYPE_RAIDZ) == 0);
00245 
00246                 /*
00247                  * Make sure someone hasn't managed to sneak a fancy new vdev
00248                  * into a crufty old storage pool.
00249                  */
00250                 ASSERT(vd->vdev_nparity == 1 ||
00251                     (vd->vdev_nparity <= 2 &&
00252                     spa_version(spa) >= SPA_VERSION_RAIDZ2) ||
00253                     (vd->vdev_nparity <= 3 &&
00254                     spa_version(spa) >= SPA_VERSION_RAIDZ3));
00255 
00256                 /*
00257                  * Note that we'll add the nparity tag even on storage pools
00258                  * that only support a single parity device -- older software
00259                  * will just ignore it.
00260                  */
00261                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NPARITY,
00262                     vd->vdev_nparity) == 0);
00263         }
00264 
00265         if (vd->vdev_wholedisk != -1ULL)
00266                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
00267                     vd->vdev_wholedisk) == 0);
00268 
00269         if (vd->vdev_not_present)
00270                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, 1) == 0);
00271 
00272         if (vd->vdev_isspare)
00273                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_SPARE, 1) == 0);
00274 
00275         if (!(flags & (VDEV_CONFIG_SPARE | VDEV_CONFIG_L2CACHE)) &&
00276             vd == vd->vdev_top) {
00277                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_ARRAY,
00278                     vd->vdev_ms_array) == 0);
00279                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_METASLAB_SHIFT,
00280                     vd->vdev_ms_shift) == 0);
00281                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASHIFT,
00282                     vd->vdev_ashift) == 0);
00283                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ASIZE,
00284                     vd->vdev_asize) == 0);
00285                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_LOG,
00286                     vd->vdev_islog) == 0);
00287                 if (vd->vdev_removing)
00288                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVING,
00289                             vd->vdev_removing) == 0);
00290         }
00291 
00292         if (vd->vdev_dtl_smo.smo_object != 0)
00293                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DTL,
00294                     vd->vdev_dtl_smo.smo_object) == 0);
00295 
00296         if (vd->vdev_crtxg)
00297                 VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_CREATE_TXG,
00298                     vd->vdev_crtxg) == 0);
00299 
00300         if (getstats) {
00301                 vdev_stat_t vs;
00302                 pool_scan_stat_t ps;
00303 
00304                 vdev_get_stats(vd, &vs);
00305                 VERIFY(nvlist_add_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
00306                     (uint64_t *)&vs, sizeof (vs) / sizeof (uint64_t)) == 0);
00307 
00308                 /* provide either current or previous scan information */
00309                 if (spa_scan_get_stats(spa, &ps) == 0) {
00310                         VERIFY(nvlist_add_uint64_array(nv,
00311                             ZPOOL_CONFIG_SCAN_STATS, (uint64_t *)&ps,
00312                             sizeof (pool_scan_stat_t) / sizeof (uint64_t))
00313                             == 0);
00314                 }
00315         }
00316 
00317         if (!vd->vdev_ops->vdev_op_leaf) {
00318                 nvlist_t **child;
00319                 int c, idx;
00320 
00321                 ASSERT(!vd->vdev_ishole);
00322 
00323                 child = kmem_alloc(vd->vdev_children * sizeof (nvlist_t *),
00324                     KM_SLEEP);
00325 
00326                 for (c = 0, idx = 0; c < vd->vdev_children; c++) {
00327                         vdev_t *cvd = vd->vdev_child[c];
00328 
00329                         /*
00330                          * If we're generating an nvlist of removing
00331                          * vdevs then skip over any device which is
00332                          * not being removed.
00333                          */
00334                         if ((flags & VDEV_CONFIG_REMOVING) &&
00335                             !cvd->vdev_removing)
00336                                 continue;
00337 
00338                         child[idx++] = vdev_config_generate(spa, cvd,
00339                             getstats, flags);
00340                 }
00341 
00342                 if (idx) {
00343                         VERIFY(nvlist_add_nvlist_array(nv,
00344                             ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
00345                 }
00346 
00347                 for (c = 0; c < idx; c++)
00348                         nvlist_free(child[c]);
00349 
00350                 kmem_free(child, vd->vdev_children * sizeof (nvlist_t *));
00351 
00352         } else {
00353                 const char *aux = NULL;
00354 
00355                 if (vd->vdev_offline && !vd->vdev_tmpoffline)
00356                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_OFFLINE,
00357                             B_TRUE) == 0);
00358                 if (vd->vdev_resilvering)
00359                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_RESILVERING,
00360                             B_TRUE) == 0);
00361                 if (vd->vdev_faulted)
00362                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_FAULTED,
00363                             B_TRUE) == 0);
00364                 if (vd->vdev_degraded)
00365                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_DEGRADED,
00366                             B_TRUE) == 0);
00367                 if (vd->vdev_removed)
00368                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_REMOVED,
00369                             B_TRUE) == 0);
00370                 if (vd->vdev_unspare)
00371                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_UNSPARE,
00372                             B_TRUE) == 0);
00373                 if (vd->vdev_ishole)
00374                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_IS_HOLE,
00375                             B_TRUE) == 0);
00376 
00377                 switch (vd->vdev_stat.vs_aux) {
00378                 case VDEV_AUX_ERR_EXCEEDED:
00379                         aux = "err_exceeded";
00380                         break;
00381 
00382                 case VDEV_AUX_EXTERNAL:
00383                         aux = "external";
00384                         break;
00385                 }
00386 
00387                 if (aux != NULL)
00388                         VERIFY(nvlist_add_string(nv, ZPOOL_CONFIG_AUX_STATE,
00389                             aux) == 0);
00390 
00391                 if (vd->vdev_splitting && vd->vdev_orig_guid != 0LL) {
00392                         VERIFY(nvlist_add_uint64(nv, ZPOOL_CONFIG_ORIG_GUID,
00393                             vd->vdev_orig_guid) == 0);
00394                 }
00395         }
00396 
00397         return (nv);
00398 }
00399 
00406 void
00407 vdev_top_config_generate(spa_t *spa, nvlist_t *config)
00408 {
00409         vdev_t *rvd = spa->spa_root_vdev;
00410         uint64_t *array;
00411         uint_t c, idx;
00412 
00413         array = kmem_alloc(rvd->vdev_children * sizeof (uint64_t), KM_SLEEP);
00414 
00415         for (c = 0, idx = 0; c < rvd->vdev_children; c++) {
00416                 vdev_t *tvd = rvd->vdev_child[c];
00417 
00418                 if (tvd->vdev_ishole)
00419                         array[idx++] = c;
00420         }
00421 
00422         if (idx) {
00423                 VERIFY(nvlist_add_uint64_array(config, ZPOOL_CONFIG_HOLE_ARRAY,
00424                     array, idx) == 0);
00425         }
00426 
00427         VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
00428             rvd->vdev_children) == 0);
00429 
00430         kmem_free(array, rvd->vdev_children * sizeof (uint64_t));
00431 }
00432 
00441 nvlist_t *
00442 vdev_label_read_config(vdev_t *vd, uint64_t txg)
00443 {
00444         spa_t *spa = vd->vdev_spa;
00445         nvlist_t *config = NULL;
00446         vdev_phys_t *vp;
00447         zio_t *zio;
00448         uint64_t best_txg = 0;
00449         int error = 0;
00450         int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
00451             ZIO_FLAG_SPECULATIVE;
00452 
00453         ASSERT(spa_config_held(spa, SCL_STATE_ALL, RW_WRITER) == SCL_STATE_ALL);
00454 
00455         if (!vdev_readable(vd))
00456                 return (NULL);
00457 
00458         vp = zio_buf_alloc(sizeof (vdev_phys_t));
00459 
00460 retry:
00461         for (int l = 0; l < VDEV_LABELS; l++) {
00462                 nvlist_t *label = NULL;
00463 
00464                 zio = zio_root(spa, NULL, NULL, flags);
00465 
00466                 vdev_label_read(zio, vd, l, vp,
00467                     offsetof(vdev_label_t, vl_vdev_phys),
00468                     sizeof (vdev_phys_t), NULL, NULL, flags);
00469 
00470                 if (zio_wait(zio) == 0 &&
00471                     nvlist_unpack(vp->vp_nvlist, sizeof (vp->vp_nvlist),
00472                     &label, 0) == 0) {
00473                         uint64_t label_txg = 0;
00474 
00475                         /*
00476                          * Auxiliary vdevs won't have txg values in their
00477                          * labels and newly added vdevs may not have been
00478                          * completely initialized so just return the
00479                          * configuration from the first valid label we
00480                          * encounter.
00481                          */
00482                         error = nvlist_lookup_uint64(label,
00483                             ZPOOL_CONFIG_POOL_TXG, &label_txg);
00484                         if ((error || label_txg == 0) && !config) {
00485                                 config = label;
00486                                 break;
00487                         } else if (label_txg <= txg && label_txg > best_txg) {
00488                                 best_txg = label_txg;
00489                                 nvlist_free(config);
00490                                 config = fnvlist_dup(label);
00491                         }
00492                 }
00493 
00494                 if (label != NULL) {
00495                         nvlist_free(label);
00496                         label = NULL;
00497                 }
00498         }
00499 
00500         if (config == NULL && !(flags & ZIO_FLAG_TRYHARD)) {
00501                 flags |= ZIO_FLAG_TRYHARD;
00502                 goto retry;
00503         }
00504 
00505         zio_buf_free(vp, sizeof (vdev_phys_t));
00506 
00507         return (config);
00508 }
00509 
00514 static boolean_t
00515 vdev_inuse(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason,
00516     uint64_t *spare_guid, uint64_t *l2cache_guid)
00517 {
00518         spa_t *spa = vd->vdev_spa;
00519         uint64_t state, pool_guid, device_guid, txg, spare_pool;
00520         uint64_t vdtxg = 0;
00521         nvlist_t *label;
00522 
00523         if (spare_guid)
00524                 *spare_guid = 0ULL;
00525         if (l2cache_guid)
00526                 *l2cache_guid = 0ULL;
00527 
00528         /*
00529          * Read the label, if any, and perform some basic sanity checks.
00530          */
00531         if ((label = vdev_label_read_config(vd, -1ULL)) == NULL)
00532                 return (B_FALSE);
00533 
00534         (void) nvlist_lookup_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
00535             &vdtxg);
00536 
00537         if (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_STATE,
00538             &state) != 0 ||
00539             nvlist_lookup_uint64(label, ZPOOL_CONFIG_GUID,
00540             &device_guid) != 0) {
00541                 nvlist_free(label);
00542                 return (B_FALSE);
00543         }
00544 
00545         if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
00546             (nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_GUID,
00547             &pool_guid) != 0 ||
00548             nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
00549             &txg) != 0)) {
00550                 nvlist_free(label);
00551                 return (B_FALSE);
00552         }
00553 
00554         nvlist_free(label);
00555 
00556         /*
00557          * Check to see if this device indeed belongs to the pool it claims to
00558          * be a part of.  The only way this is allowed is if the device is a hot
00559          * spare (which we check for later on).
00560          */
00561         if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
00562             !spa_guid_exists(pool_guid, device_guid) &&
00563             !spa_spare_exists(device_guid, NULL, NULL) &&
00564             !spa_l2cache_exists(device_guid, NULL))
00565                 return (B_FALSE);
00566 
00567         /*
00568          * If the transaction group is zero, then this an initialized (but
00569          * unused) label.  This is only an error if the create transaction
00570          * on-disk is the same as the one we're using now, in which case the
00571          * user has attempted to add the same vdev multiple times in the same
00572          * transaction.
00573          */
00574         if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
00575             txg == 0 && vdtxg == crtxg)
00576                 return (B_TRUE);
00577 
00578         /*
00579          * Check to see if this is a spare device.  We do an explicit check for
00580          * spa_has_spare() here because it may be on our pending list of spares
00581          * to add.  We also check if it is an l2cache device.
00582          */
00583         if (spa_spare_exists(device_guid, &spare_pool, NULL) ||
00584             spa_has_spare(spa, device_guid)) {
00585                 if (spare_guid)
00586                         *spare_guid = device_guid;
00587 
00588                 switch (reason) {
00589                 case VDEV_LABEL_CREATE:
00590                 case VDEV_LABEL_L2CACHE:
00591                         return (B_TRUE);
00592 
00593                 case VDEV_LABEL_REPLACE:
00594                         return (!spa_has_spare(spa, device_guid) ||
00595                             spare_pool != 0ULL);
00596 
00597                 case VDEV_LABEL_SPARE:
00598                         return (spa_has_spare(spa, device_guid));
00599                 }
00600         }
00601 
00602         /*
00603          * Check to see if this is an l2cache device.
00604          */
00605         if (spa_l2cache_exists(device_guid, NULL))
00606                 return (B_TRUE);
00607 
00608         /*
00609          * We can't rely on a pool's state if it's been imported
00610          * read-only.  Instead we look to see if the pools is marked
00611          * read-only in the namespace and set the state to active.
00612          */
00613         if ((spa = spa_by_guid(pool_guid, device_guid)) != NULL &&
00614             spa_mode(spa) == FREAD)
00615                 state = POOL_STATE_ACTIVE;
00616 
00617         /*
00618          * If the device is marked ACTIVE, then this device is in use by another
00619          * pool on the system.
00620          */
00621         return (state == POOL_STATE_ACTIVE);
00622 }
00623 
00632 int
00633 vdev_label_init(vdev_t *vd, uint64_t crtxg, vdev_labeltype_t reason)
00634 {
00635         spa_t *spa = vd->vdev_spa;
00636         nvlist_t *label;
00637         vdev_phys_t *vp;
00638         char *pad2;
00639         uberblock_t *ub;
00640         zio_t *zio;
00641         char *buf;
00642         size_t buflen;
00643         int error;
00644         uint64_t spare_guid, l2cache_guid;
00645         int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
00646 
00647         ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
00648 
00649         for (int c = 0; c < vd->vdev_children; c++)
00650                 if ((error = vdev_label_init(vd->vdev_child[c],
00651                     crtxg, reason)) != 0)
00652                         return (error);
00653 
00654         /* Track the creation time for this vdev */
00655         vd->vdev_crtxg = crtxg;
00656 
00657         if (!vd->vdev_ops->vdev_op_leaf)
00658                 return (0);
00659 
00660         /*
00661          * Dead vdevs cannot be initialized.
00662          */
00663         if (vdev_is_dead(vd))
00664                 return (EIO);
00665 
00666         /*
00667          * Determine if the vdev is in use.
00668          */
00669         if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPLIT &&
00670             vdev_inuse(vd, crtxg, reason, &spare_guid, &l2cache_guid))
00671                 return (EBUSY);
00672 
00673         /*
00674          * If this is a request to add or replace a spare or l2cache device
00675          * that is in use elsewhere on the system, then we must update the
00676          * guid (which was initialized to a random value) to reflect the
00677          * actual GUID (which is shared between multiple pools).
00678          */
00679         if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_L2CACHE &&
00680             spare_guid != 0ULL) {
00681                 uint64_t guid_delta = spare_guid - vd->vdev_guid;
00682 
00683                 vd->vdev_guid += guid_delta;
00684 
00685                 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
00686                         pvd->vdev_guid_sum += guid_delta;
00687 
00688                 /*
00689                  * If this is a replacement, then we want to fallthrough to the
00690                  * rest of the code.  If we're adding a spare, then it's already
00691                  * labeled appropriately and we can just return.
00692                  */
00693                 if (reason == VDEV_LABEL_SPARE)
00694                         return (0);
00695                 ASSERT(reason == VDEV_LABEL_REPLACE ||
00696                     reason == VDEV_LABEL_SPLIT);
00697         }
00698 
00699         if (reason != VDEV_LABEL_REMOVE && reason != VDEV_LABEL_SPARE &&
00700             l2cache_guid != 0ULL) {
00701                 uint64_t guid_delta = l2cache_guid - vd->vdev_guid;
00702 
00703                 vd->vdev_guid += guid_delta;
00704 
00705                 for (vdev_t *pvd = vd; pvd != NULL; pvd = pvd->vdev_parent)
00706                         pvd->vdev_guid_sum += guid_delta;
00707 
00708                 /*
00709                  * If this is a replacement, then we want to fallthrough to the
00710                  * rest of the code.  If we're adding an l2cache, then it's
00711                  * already labeled appropriately and we can just return.
00712                  */
00713                 if (reason == VDEV_LABEL_L2CACHE)
00714                         return (0);
00715                 ASSERT(reason == VDEV_LABEL_REPLACE);
00716         }
00717 
00718         /*
00719          * TRIM the whole thing so that we start with a clean slate.
00720          * It's just an optimization, so we don't care if it fails.
00721          * Don't TRIM if removing so that we don't interfere with zpool
00722          * disaster recovery.
00723          */
00724         if (!zfs_notrim && (reason == VDEV_LABEL_CREATE ||
00725             reason == VDEV_LABEL_SPARE || reason == VDEV_LABEL_L2CACHE))
00726                 zio_wait(zio_trim(NULL, spa, vd, 0, vd->vdev_psize));
00727 
00728         /*
00729          * Initialize its label.
00730          */
00731         vp = zio_buf_alloc(sizeof (vdev_phys_t));
00732         bzero(vp, sizeof (vdev_phys_t));
00733 
00734         /*
00735          * Generate a label describing the pool and our top-level vdev.
00736          * We mark it as being from txg 0 to indicate that it's not
00737          * really part of an active pool just yet.  The labels will
00738          * be written again with a meaningful txg by spa_sync().
00739          */
00740         if (reason == VDEV_LABEL_SPARE ||
00741             (reason == VDEV_LABEL_REMOVE && vd->vdev_isspare)) {
00742                 /*
00743                  * For inactive hot spares, we generate a special label that
00744                  * identifies as a mutually shared hot spare.  We write the
00745                  * label if we are adding a hot spare, or if we are removing an
00746                  * active hot spare (in which case we want to revert the
00747                  * labels).
00748                  */
00749                 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
00750 
00751                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
00752                     spa_version(spa)) == 0);
00753                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
00754                     POOL_STATE_SPARE) == 0);
00755                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
00756                     vd->vdev_guid) == 0);
00757         } else if (reason == VDEV_LABEL_L2CACHE ||
00758             (reason == VDEV_LABEL_REMOVE && vd->vdev_isl2cache)) {
00759                 /*
00760                  * For level 2 ARC devices, add a special label.
00761                  */
00762                 VERIFY(nvlist_alloc(&label, NV_UNIQUE_NAME, KM_SLEEP) == 0);
00763 
00764                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_VERSION,
00765                     spa_version(spa)) == 0);
00766                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_POOL_STATE,
00767                     POOL_STATE_L2CACHE) == 0);
00768                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_GUID,
00769                     vd->vdev_guid) == 0);
00770         } else {
00771                 uint64_t txg = 0ULL;
00772 
00773                 if (reason == VDEV_LABEL_SPLIT)
00774                         txg = spa->spa_uberblock.ub_txg;
00775                 label = spa_config_generate(spa, vd, txg, B_FALSE);
00776 
00777                 /*
00778                  * Add our creation time.  This allows us to detect multiple
00779                  * vdev uses as described above, and automatically expires if we
00780                  * fail.
00781                  */
00782                 VERIFY(nvlist_add_uint64(label, ZPOOL_CONFIG_CREATE_TXG,
00783                     crtxg) == 0);
00784         }
00785 
00786         buf = vp->vp_nvlist;
00787         buflen = sizeof (vp->vp_nvlist);
00788 
00789         error = nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP);
00790         if (error != 0) {
00791                 nvlist_free(label);
00792                 zio_buf_free(vp, sizeof (vdev_phys_t));
00793                 /* EFAULT means nvlist_pack ran out of room */
00794                 return (error == EFAULT ? ENAMETOOLONG : EINVAL);
00795         }
00796 
00797         /*
00798          * Initialize uberblock template.
00799          */
00800         ub = zio_buf_alloc(VDEV_UBERBLOCK_RING);
00801         bzero(ub, VDEV_UBERBLOCK_RING);
00802         *ub = spa->spa_uberblock;
00803         ub->ub_txg = 0;
00804 
00805         /* Initialize the 2nd padding area. */
00806         pad2 = zio_buf_alloc(VDEV_PAD_SIZE);
00807         bzero(pad2, VDEV_PAD_SIZE);
00808 
00809         /*
00810          * Write everything in parallel.
00811          */
00812 retry:
00813         zio = zio_root(spa, NULL, NULL, flags);
00814 
00815         for (int l = 0; l < VDEV_LABELS; l++) {
00816 
00817                 vdev_label_write(zio, vd, l, vp,
00818                     offsetof(vdev_label_t, vl_vdev_phys),
00819                     sizeof (vdev_phys_t), NULL, NULL, flags);
00820 
00821                 /*
00822                  * Skip the 1st padding area.
00823                  * Zero out the 2nd padding area where it might have
00824                  * left over data from previous filesystem format.
00825                  */
00826                 vdev_label_write(zio, vd, l, pad2,
00827                     offsetof(vdev_label_t, vl_pad2),
00828                     VDEV_PAD_SIZE, NULL, NULL, flags);
00829 
00830                 vdev_label_write(zio, vd, l, ub,
00831                     offsetof(vdev_label_t, vl_uberblock),
00832                     VDEV_UBERBLOCK_RING, NULL, NULL, flags);
00833         }
00834 
00835         error = zio_wait(zio);
00836 
00837         if (error != 0 && !(flags & ZIO_FLAG_TRYHARD)) {
00838                 flags |= ZIO_FLAG_TRYHARD;
00839                 goto retry;
00840         }
00841 
00842         nvlist_free(label);
00843         zio_buf_free(pad2, VDEV_PAD_SIZE);
00844         zio_buf_free(ub, VDEV_UBERBLOCK_RING);
00845         zio_buf_free(vp, sizeof (vdev_phys_t));
00846 
00847         /*
00848          * If this vdev hasn't been previously identified as a spare, then we
00849          * mark it as such only if a) we are labeling it as a spare, or b) it
00850          * exists as a spare elsewhere in the system.  Do the same for
00851          * level 2 ARC devices.
00852          */
00853         if (error == 0 && !vd->vdev_isspare &&
00854             (reason == VDEV_LABEL_SPARE ||
00855             spa_spare_exists(vd->vdev_guid, NULL, NULL)))
00856                 spa_spare_add(vd);
00857 
00858         if (error == 0 && !vd->vdev_isl2cache &&
00859             (reason == VDEV_LABEL_L2CACHE ||
00860             spa_l2cache_exists(vd->vdev_guid, NULL)))
00861                 spa_l2cache_add(vd);
00862 
00863         return (error);
00864 }
00865 
00866 /*
00867  * ==========================================================================
00868  * uberblock load/sync
00869  * ==========================================================================
00870  */
00871 
00882 static int
00883 vdev_uberblock_compare(uberblock_t *ub1, uberblock_t *ub2)
00884 {
00885         if (ub1->ub_txg < ub2->ub_txg)
00886                 return (-1);
00887         if (ub1->ub_txg > ub2->ub_txg)
00888                 return (1);
00889 
00890         if (ub1->ub_timestamp < ub2->ub_timestamp)
00891                 return (-1);
00892         if (ub1->ub_timestamp > ub2->ub_timestamp)
00893                 return (1);
00894 
00895         return (0);
00896 }
00897 
00898 struct ubl_cbdata {
00899         uberblock_t     *ubl_ubbest;    /* Best uberblock */
00900         vdev_t          *ubl_vd;        /* vdev associated with the above */
00901 };
00902 
00903 static void
00904 vdev_uberblock_load_done(zio_t *zio)
00905 {
00906         vdev_t *vd = zio->io_vd;
00907         spa_t *spa = zio->io_spa;
00908         zio_t *rio = zio->io_private;
00909         uberblock_t *ub = zio->io_data;
00910         struct ubl_cbdata *cbp = rio->io_private;
00911 
00912         ASSERT3U(zio->io_size, ==, VDEV_UBERBLOCK_SIZE(vd));
00913 
00914         if (zio->io_error == 0 && uberblock_verify(ub) == 0) {
00915                 mutex_enter(&rio->io_lock);
00916                 if (ub->ub_txg <= spa->spa_load_max_txg &&
00917                     vdev_uberblock_compare(ub, cbp->ubl_ubbest) > 0) {
00918                         /*
00919                          * Keep track of the vdev in which this uberblock
00920                          * was found. We will use this information later
00921                          * to obtain the config nvlist associated with
00922                          * this uberblock.
00923                          */
00924                         *cbp->ubl_ubbest = *ub;
00925                         cbp->ubl_vd = vd;
00926                 }
00927                 mutex_exit(&rio->io_lock);
00928         }
00929 
00930         zio_buf_free(zio->io_data, zio->io_size);
00931 }
00932 
00933 static void
00934 vdev_uberblock_load_impl(zio_t *zio, vdev_t *vd, int flags,
00935     struct ubl_cbdata *cbp)
00936 {
00937         for (int c = 0; c < vd->vdev_children; c++)
00938                 vdev_uberblock_load_impl(zio, vd->vdev_child[c], flags, cbp);
00939 
00940         if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
00941                 for (int l = 0; l < VDEV_LABELS; l++) {
00942                         for (int n = 0; n < VDEV_UBERBLOCK_COUNT(vd); n++) {
00943                                 vdev_label_read(zio, vd, l,
00944                                     zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd)),
00945                                     VDEV_UBERBLOCK_OFFSET(vd, n),
00946                                     VDEV_UBERBLOCK_SIZE(vd),
00947                                     vdev_uberblock_load_done, zio, flags);
00948                         }
00949                 }
00950         }
00951 }
00952 
00959 void
00960 vdev_uberblock_load(vdev_t *rvd, uberblock_t *ub, nvlist_t **config)
00961 {
00962         zio_t *zio;
00963         spa_t *spa = rvd->vdev_spa;
00964         struct ubl_cbdata cb;
00965         int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL |
00966             ZIO_FLAG_SPECULATIVE | ZIO_FLAG_TRYHARD;
00967 
00968         ASSERT(ub);
00969         ASSERT(config);
00970 
00971         bzero(ub, sizeof (uberblock_t));
00972         *config = NULL;
00973 
00974         cb.ubl_ubbest = ub;
00975         cb.ubl_vd = NULL;
00976 
00977         spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
00978         zio = zio_root(spa, NULL, &cb, flags);
00979         vdev_uberblock_load_impl(zio, rvd, flags, &cb);
00980         (void) zio_wait(zio);
00981 
00982         /*
00983          * It's possible that the best uberblock was discovered on a label
00984          * that has a configuration which was written in a future txg.
00985          * Search all labels on this vdev to find the configuration that
00986          * matches the txg for our uberblock.
00987          */
00988         if (cb.ubl_vd != NULL)
00989                 *config = vdev_label_read_config(cb.ubl_vd, ub->ub_txg);
00990         spa_config_exit(spa, SCL_ALL, FTAG);
00991 }
00992 
00997 static void
00998 vdev_uberblock_sync_done(zio_t *zio)
00999 {
01000         uint64_t *good_writes = zio->io_private;
01001 
01002         if (zio->io_error == 0 && zio->io_vd->vdev_top->vdev_ms_array != 0)
01003                 atomic_add_64(good_writes, 1);
01004 }
01005 
01009 static void
01010 vdev_uberblock_sync(zio_t *zio, uberblock_t *ub, vdev_t *vd, int flags)
01011 {
01012         uberblock_t *ubbuf;
01013         int n;
01014 
01015         for (int c = 0; c < vd->vdev_children; c++)
01016                 vdev_uberblock_sync(zio, ub, vd->vdev_child[c], flags);
01017 
01018         if (!vd->vdev_ops->vdev_op_leaf)
01019                 return;
01020 
01021         if (!vdev_writeable(vd))
01022                 return;
01023 
01024         n = ub->ub_txg & (VDEV_UBERBLOCK_COUNT(vd) - 1);
01025 
01026         ubbuf = zio_buf_alloc(VDEV_UBERBLOCK_SIZE(vd));
01027         bzero(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
01028         *ubbuf = *ub;
01029 
01030         for (int l = 0; l < VDEV_LABELS; l++)
01031                 vdev_label_write(zio, vd, l, ubbuf,
01032                     VDEV_UBERBLOCK_OFFSET(vd, n), VDEV_UBERBLOCK_SIZE(vd),
01033                     vdev_uberblock_sync_done, zio->io_private,
01034                     flags | ZIO_FLAG_DONT_PROPAGATE);
01035 
01036         zio_buf_free(ubbuf, VDEV_UBERBLOCK_SIZE(vd));
01037 }
01038 
01042 int
01043 vdev_uberblock_sync_list(vdev_t **svd, int svdcount, uberblock_t *ub, int flags)
01044 {
01045         spa_t *spa = svd[0]->vdev_spa;
01046         zio_t *zio;
01047         uint64_t good_writes = 0;
01048 
01049         zio = zio_root(spa, NULL, &good_writes, flags);
01050 
01051         for (int v = 0; v < svdcount; v++)
01052                 vdev_uberblock_sync(zio, ub, svd[v], flags);
01053 
01054         (void) zio_wait(zio);
01055 
01056         /*
01057          * Flush the uberblocks to disk.  This ensures that the odd labels
01058          * are no longer needed (because the new uberblocks and the even
01059          * labels are safely on disk), so it is safe to overwrite them.
01060          */
01061         zio = zio_root(spa, NULL, NULL, flags);
01062 
01063         for (int v = 0; v < svdcount; v++)
01064                 zio_flush(zio, svd[v]);
01065 
01066         (void) zio_wait(zio);
01067 
01068         return (good_writes >= 1 ? 0 : EIO);
01069 }
01070 
01074 static void
01075 vdev_label_sync_done(zio_t *zio)
01076 {
01077         uint64_t *good_writes = zio->io_private;
01078 
01079         if (zio->io_error == 0)
01080                 atomic_add_64(good_writes, 1);
01081 }
01082 
01086 static void
01087 vdev_label_sync_top_done(zio_t *zio)
01088 {
01089         uint64_t *good_writes = zio->io_private;
01090 
01091         if (*good_writes == 0)
01092                 zio->io_error = EIO;
01093 
01094         kmem_free(good_writes, sizeof (uint64_t));
01095 }
01096 
01100 static void
01101 vdev_label_sync_ignore_done(zio_t *zio)
01102 {
01103         kmem_free(zio->io_private, sizeof (uint64_t));
01104 }
01105 
01109 static void
01110 vdev_label_sync(zio_t *zio, vdev_t *vd, int l, uint64_t txg, int flags)
01111 {
01112         nvlist_t *label;
01113         vdev_phys_t *vp;
01114         char *buf;
01115         size_t buflen;
01116 
01117         for (int c = 0; c < vd->vdev_children; c++)
01118                 vdev_label_sync(zio, vd->vdev_child[c], l, txg, flags);
01119 
01120         if (!vd->vdev_ops->vdev_op_leaf)
01121                 return;
01122 
01123         if (!vdev_writeable(vd))
01124                 return;
01125 
01126         /*
01127          * Generate a label describing the top-level config to which we belong.
01128          */
01129         label = spa_config_generate(vd->vdev_spa, vd, txg, B_FALSE);
01130 
01131         vp = zio_buf_alloc(sizeof (vdev_phys_t));
01132         bzero(vp, sizeof (vdev_phys_t));
01133 
01134         buf = vp->vp_nvlist;
01135         buflen = sizeof (vp->vp_nvlist);
01136 
01137         if (nvlist_pack(label, &buf, &buflen, NV_ENCODE_XDR, KM_SLEEP) == 0) {
01138                 for (; l < VDEV_LABELS; l += 2) {
01139                         vdev_label_write(zio, vd, l, vp,
01140                             offsetof(vdev_label_t, vl_vdev_phys),
01141                             sizeof (vdev_phys_t),
01142                             vdev_label_sync_done, zio->io_private,
01143                             flags | ZIO_FLAG_DONT_PROPAGATE);
01144                 }
01145         }
01146 
01147         zio_buf_free(vp, sizeof (vdev_phys_t));
01148         nvlist_free(label);
01149 }
01150 
01151 int
01152 vdev_label_sync_list(spa_t *spa, int l, uint64_t txg, int flags)
01153 {
01154         list_t *dl = &spa->spa_config_dirty_list;
01155         vdev_t *vd;
01156         zio_t *zio;
01157         int error;
01158 
01159         /*
01160          * Write the new labels to disk.
01161          */
01162         zio = zio_root(spa, NULL, NULL, flags);
01163 
01164         for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd)) {
01165                 uint64_t *good_writes = kmem_zalloc(sizeof (uint64_t),
01166                     KM_SLEEP);
01167 
01168                 ASSERT(!vd->vdev_ishole);
01169 
01170                 zio_t *vio = zio_null(zio, spa, NULL,
01171                     (vd->vdev_islog || vd->vdev_aux != NULL) ?
01172                     vdev_label_sync_ignore_done : vdev_label_sync_top_done,
01173                     good_writes, flags);
01174                 vdev_label_sync(vio, vd, l, txg, flags);
01175                 zio_nowait(vio);
01176         }
01177 
01178         error = zio_wait(zio);
01179 
01180         /*
01181          * Flush the new labels to disk.
01182          */
01183         zio = zio_root(spa, NULL, NULL, flags);
01184 
01185         for (vd = list_head(dl); vd != NULL; vd = list_next(dl, vd))
01186                 zio_flush(zio, vd);
01187 
01188         (void) zio_wait(zio);
01189 
01190         return (error);
01191 }
01192 
01204 int
01205 vdev_config_sync(vdev_t **svd, int svdcount, uint64_t txg, boolean_t tryhard)
01206 {
01207         spa_t *spa = svd[0]->vdev_spa;
01208         uberblock_t *ub = &spa->spa_uberblock;
01209         vdev_t *vd;
01210         zio_t *zio;
01211         int error;
01212         int flags = ZIO_FLAG_CONFIG_WRITER | ZIO_FLAG_CANFAIL;
01213 
01214         /*
01215          * Normally, we don't want to try too hard to write every label and
01216          * uberblock.  If there is a flaky disk, we don't want the rest of the
01217          * sync process to block while we retry.  But if we can't write a
01218          * single label out, we should retry with ZIO_FLAG_TRYHARD before
01219          * bailing out and declaring the pool faulted.
01220          */
01221         if (tryhard)
01222                 flags |= ZIO_FLAG_TRYHARD;
01223 
01224         ASSERT(ub->ub_txg <= txg);
01225 
01226         /*
01227          * If this isn't a resync due to I/O errors,
01228          * and nothing changed in this transaction group,
01229          * and the vdev configuration hasn't changed,
01230          * then there's nothing to do.
01231          */
01232         if (ub->ub_txg < txg &&
01233             uberblock_update(ub, spa->spa_root_vdev, txg) == B_FALSE &&
01234             list_is_empty(&spa->spa_config_dirty_list))
01235                 return (0);
01236 
01237         if (txg > spa_freeze_txg(spa))
01238                 return (0);
01239 
01240         ASSERT(txg <= spa->spa_final_txg);
01241 
01242         /*
01243          * Flush the write cache of every disk that's been written to
01244          * in this transaction group.  This ensures that all blocks
01245          * written in this txg will be committed to stable storage
01246          * before any uberblock that references them.
01247          */
01248         zio = zio_root(spa, NULL, NULL, flags);
01249 
01250         for (vd = txg_list_head(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)); vd;
01251             vd = txg_list_next(&spa->spa_vdev_txg_list, vd, TXG_CLEAN(txg)))
01252                 zio_flush(zio, vd);
01253 
01254         (void) zio_wait(zio);
01255 
01256         /*
01257          * Sync out the even labels (L0, L2) for every dirty vdev.  If the
01258          * system dies in the middle of this process, that's OK: all of the
01259          * even labels that made it to disk will be newer than any uberblock,
01260          * and will therefore be considered invalid.  The odd labels (L1, L3),
01261          * which have not yet been touched, will still be valid.  We flush
01262          * the new labels to disk to ensure that all even-label updates
01263          * are committed to stable storage before the uberblock update.
01264          */
01265         if ((error = vdev_label_sync_list(spa, 0, txg, flags)) != 0)
01266                 return (error);
01267 
01268         /*
01269          * Sync the uberblocks to all vdevs in svd[].
01270          * If the system dies in the middle of this step, there are two cases
01271          * to consider, and the on-disk state is consistent either way:
01272          *
01273          * (1)  If none of the new uberblocks made it to disk, then the
01274          *      previous uberblock will be the newest, and the odd labels
01275          *      (which had not yet been touched) will be valid with respect
01276          *      to that uberblock.
01277          *
01278          * (2)  If one or more new uberblocks made it to disk, then they
01279          *      will be the newest, and the even labels (which had all
01280          *      been successfully committed) will be valid with respect
01281          *      to the new uberblocks.
01282          */
01283         if ((error = vdev_uberblock_sync_list(svd, svdcount, ub, flags)) != 0)
01284                 return (error);
01285 
01286         /*
01287          * Sync out odd labels for every dirty vdev.  If the system dies
01288          * in the middle of this process, the even labels and the new
01289          * uberblocks will suffice to open the pool.  The next time
01290          * the pool is opened, the first thing we'll do -- before any
01291          * user data is modified -- is mark every vdev dirty so that
01292          * all labels will be brought up to date.  We flush the new labels
01293          * to disk to ensure that all odd-label updates are committed to
01294          * stable storage before the next transaction group begins.
01295          */
01296         if ((error = vdev_label_sync_list(spa, 1, txg, flags)) != 0)
01297                 return (error);
01298 
01299         trim_thread_wakeup(spa);
01300 
01301         return (0);
01302 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines