spa.c revision ecc2d604e885a75cc75e647b5641af99d5a6f4a6
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains all the routines used when modifying on-disk SPA state.
* This includes opening, importing, destroying, exporting a pool, and syncing a
* pool.
*/
#include <sys/zfs_context.h>
#include <sys/spa_impl.h>
#include <sys/zio_checksum.h>
#include <sys/zio_compress.h>
#include <sys/vdev_impl.h>
#include <sys/metaslab.h>
#include <sys/uberblock_impl.h>
#include <sys/dmu_traverse.h>
#include <sys/dsl_pool.h>
#include <sys/dsl_prop.h>
/*
* ==========================================================================
* ==========================================================================
*/
static int
spa_error_entry_compare(const void *a, const void *b)
{
int ret;
sizeof (zbookmark_t));
if (ret < 0)
return (-1);
else if (ret > 0)
return (1);
else
return (0);
}
/*
* Utility function which retrieves copies of the current logs and
* re-initializes them in the process.
*/
void
{
spa_error_entry_compare, sizeof (spa_error_entry_t),
spa_error_entry_compare, sizeof (spa_error_entry_t),
}
/*
* Activate an uninitialized pool.
*/
static void
{
int t;
for (t = 0; t < ZIO_TYPES; t++) {
}
spa_error_entry_compare, sizeof (spa_error_entry_t),
spa_error_entry_compare, sizeof (spa_error_entry_t),
}
/*
* Opposite of spa_activate().
*/
static void
{
int t;
for (t = 0; t < ZIO_TYPES; t++) {
}
/*
* If this was part of an import or the open otherwise failed, we may
* still have errors left in the queues. Empty them just in case.
*/
}
/*
* Verify a pool configuration, and construct the vdev tree appropriately. This
* will create all the necessary vdevs in the appropriate layout, with each vdev
* All vdev validation is done by the vdev_alloc() routine.
*/
static vdev_t *
{
return (NULL);
return (vd);
return (NULL);
}
for (c = 0; c < children; c++) {
return (NULL);
}
}
return (vd);
}
/*
* Opposite of spa_load().
*/
static void
{
/*
* Stop async tasks.
*/
/*
* Stop syncing.
*/
if (spa->spa_sync_on) {
}
/*
* Wait for any outstanding prefetch I/O to complete.
*/
/*
* Close the dsl pool.
*/
if (spa->spa_dsl_pool) {
}
/*
* Close all vdevs.
*/
if (spa->spa_root_vdev)
spa->spa_async_suspended = 0;
}
/*
* Load an existing storage pool, using the pool's builtin spa_config as a
* source of configuration information.
*/
static int
{
int error = 0;
goto out;
}
spa_guid_exists(pool_guid, 0)) {
goto out;
}
/*
* Parse the configuration into a vdev tree.
*/
goto out;
}
/*
* Try to open all vdevs, loading each label in the process.
*/
goto out;
}
/*
* Find the best uberblock.
*/
/*
* If we weren't able to find a single valid uberblock, return failure.
*/
goto out;
}
/*
* If the pool is newer than the code, we can't open it.
*/
goto out;
}
/*
* If the vdev guid sum doesn't match the uberblock, we have an
* incomplete configuration.
*/
goto out;
}
/*
* Initialize internal SPA structures.
*/
if (error) {
goto out;
}
goto out;
}
if (!mosconfig) {
if (error == 0)
if (error) {
goto out;
}
}
goto out;
}
/*
* Load the persistent error log. If we have an older pool, this will
* not be present.
*/
goto out;
}
goto out;
}
/*
* Load the vdev state for all top level vdevs. We need to grab the
* config lock because all label I/O is done with the
* ZIO_FLAG_CONFIG_HELD flag.
*/
if (error)
goto out;
/*
* Propagate the leaf DTLs we just loaded all the way up the tree.
*/
/*
* Check the state of the root vdev. If it can't be opened, it
* indicates one or more toplevel vdevs are faulted.
*/
goto out;
}
int need_update = B_FALSE;
int c;
/*
* Claim log blocks that haven't been committed yet.
* This must all happen in a single txg.
*/
spa_first_txg(spa));
/*
* Wait for all claims to sync.
*/
/*
* If the config cache is stale, or we have uninitialized
* metaslabs (see spa_vdev_add()), then update the config.
*/
state == SPA_LOAD_IMPORT)
for (c = 0; c < rvd->vdev_children; c++)
/*
* Update the config cache asychronously in case we're the
* root pool, in which case the config cache isn't writable yet.
*/
if (need_update)
}
error = 0;
out:
if (error)
return (error);
}
/*
*
* The import case is identical to an open except that the configuration is sent
* down from userland, instead of grabbed from the configuration cache. For the
* case of an open, the pool configuration will exist in the
* POOL_STATE_UNITIALIZED state.
*
* the same time open the pool, without having to keep around the spa_t in some
* ambiguous state.
*/
static int
{
int error;
/*
* As disgusting as this is, we need to support recursive calls to this
* function because dsl_dir_open() is called during spa_load(), and ends
* up calling spa_open() again. The real fix is to figure out how to
* avoid dsl_dir_open() calling this in the first place.
*/
}
if (locked)
return (ENOENT);
}
/*
* If vdev_load() returns EBADF, it indicates that one
* of the vdevs indicates that the pool has been
* exported or destroyed. If this is the case, the
* config cache is out of sync and we should remove the
* pool from the namespace.
*/
if (locked)
return (ENOENT);
}
if (error) {
/*
* We can't open the pool, but we still have useful
* information: the state of each vdev after the
* attempted vdev_open(). Return this to the user.
*/
B_TRUE);
}
if (locked)
return (error);
} else {
}
}
if (locked)
}
/*
* If we just loaded the pool, resilver anything that's out of date.
*/
return (0);
}
int
{
}
/*
* Lookup the given spa_t, incrementing the inject count in the process,
* preventing it from being exported or destroyed.
*/
spa_t *
spa_inject_addref(char *name)
{
return (NULL);
}
spa->spa_inject_ref++;
return (spa);
}
void
{
spa->spa_inject_ref--;
}
int
{
int error;
spa_get_errlog_size(spa)) == 0);
/*
* We want to get the alternate root even for faulted pools, so we cheat
* and call spa_lookup() directly.
*/
if (altroot) {
if (spa)
else
altroot[0] = '\0';
} else {
}
}
return (error);
}
/*
* Pool Creation
*/
int
{
dsl_pool_t *dp;
int c, error;
/*
* If this pool already exists, return failure.
*/
return (EEXIST);
}
/*
* Allocate a new spa_t structure.
*/
/*
* Create the root vdev.
*/
} else {
for (c = 0; c < rvd->vdev_children; c++)
}
}
if (error) {
return (error);
}
/*
* Create the pool config object.
*/
}
/*
* Create the deferred-free bplist object. Turn off compression
* because sync-to-convergence takes longer if the blocksize
* keeps changing.
*/
}
/*
* We explicitly wait for the first transaction to complete so that our
* bean counters are appropriately updated.
*/
return (0);
}
/*
* Import the given pool into the system. We set up the necessary spa_t and
* then call spa_load() to do the dirty work.
*/
int
{
int error;
return (EROFS);
/*
* If a pool with this name exists, return failure.
*/
return (EEXIST);
}
/*
* Create and initialize the spa structure.
*/
/*
* Pass off the heavy lifting to spa_load().
* Pass TRUE for mosconfig because the user-supplied config
* is actually the one to trust when doing an import.
*/
if (error) {
return (error);
}
/*
* Update the config cache to include the newly-imported pool.
*/
/*
* Resilver anything that's out of date.
*/
return (0);
}
/*
* This (illegal) pool name is used when temporarily importing a spa_t in order
* to get the vdev stats associated with the imported devices.
*/
#define TRYIMPORT_NAME "$import"
nvlist_t *
{
char *poolname;
return (NULL);
return (NULL);
/*
* Create and initialize the spa structure.
*/
/*
* Pass off the heavy lifting to spa_load().
* Pass TRUE for mosconfig because the user-supplied config
* is actually the one to trust when doing an import.
*/
/*
* If 'tryconfig' was at least parsable, return the current config.
*/
poolname) == 0);
state) == 0);
}
return (config);
}
/*
*
* The act of destroying or exporting a pool is very simple. We make sure there
* is no more pending I/O and any references to the pool are gone. Then, we
* update the pool state and sync all the labels to disk, removing the
* configuration from the cache afterwards.
*/
static int
{
return (EROFS);
return (ENOENT);
}
/*
* Put a hold on the pool, drop the namespace lock, stop async tasks,
* reacquire the namespace lock, and see if we can export.
*/
/*
* The pool will be in core if it's openable,
* in which case we can modify its state.
*/
/*
* Objsets may be open only because they're dirty, so we
* have to force it to sync before checking spa_refcnt.
*/
/*
* A pool cannot be exported or destroyed if there are active
* references. If we are resetting a pool, allow references by
* fault injection handlers.
*/
if (!spa_refcount_zero(spa) ||
(spa->spa_inject_ref != 0 &&
return (EBUSY);
}
/*
* We want this to be reflected on every label,
* so mark them all dirty. spa_unload() will do the
* final sync that pushes these changes out.
*/
if (new_state != POOL_STATE_UNINITIALIZED) {
}
}
}
if (new_state != POOL_STATE_UNINITIALIZED) {
}
return (0);
}
/*
* Destroy a storage pool.
*/
int
spa_destroy(char *pool)
{
}
/*
* Export a storage pool.
*/
int
spa_export(char *pool)
{
}
/*
* Similar to spa_export(), this unloads the spa_t without actually removing it
* from the namespace in any way.
*/
int
{
}
/*
* ==========================================================================
* Device manipulation
* ==========================================================================
*/
/*
* Add capacity to a storage pool.
*/
int
{
int c, error;
/*
* Transfer each new top-level vdev from vd to rvd.
*/
for (c = 0; c < vd->vdev_children; c++) {
}
/*
* We have to be careful when adding new vdevs to an existing pool.
* If other threads start allocating from these vdevs before we
* sync the config cache, and we lose power, then upon reboot we may
* fail to open the pool because there are DVAs that the config cache
* can't translate. Therefore, we first add the vdevs without
* initializing metaslabs; sync the config cache (via spa_vdev_exit());
* and then let spa_config_update() initialize the new metaslabs.
*
* spa_load() checks for added-but-not-initialized vdevs, so that
* if we lose power at any point in this sequence, the remaining
* steps will be completed the next time we load the pool.
*/
return (0);
}
/*
* Attach a device to a mirror. The arguments are the path to any device
* in the mirror, and the nvroot for the new device. If the path specifies
* a device that is not mirrored, we automatically insert the mirror vdev.
*
* If 'replacing' is specified, the new device is intended to replace the
* existing device; in this case the two devices are made into their own
* mirror using the 'replacing' vdev, which is functionally idendical to
* the mirror vdev (it actually reuses all the same ops) but has a few
* extra rules: you can't attach to it after it's been created, and upon
* completion of resilvering, the first disk (the one being replaced)
* is automatically detached.
*/
int
{
int error;
/*
* The parent must be a mirror or the root, unless we're replacing;
* in that case, the parent can be anything but another replacing vdev.
*/
/*
* Compare the new device size with the replaceable/attachable
* device size.
*/
/*
* The new device cannot have a higher alignment requirement
* than the top-level vdev.
*/
/*
* If this is an in-place replacement, update oldvd's path and devid
* to make it distinguishable from newvd, and unopenable from now on.
*/
KM_SLEEP);
}
}
/*
* If the parent is not a mirror, or if we're replacing,
*/
/*
* Extract the new device from its root and add it to pvd.
*/
/*
* If newvd is smaller than oldvd, but larger than its rsize,
* the addition of newvd may have decreased our parent's asize.
*/
/*
* Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate
* upward when spa_vdev_exit() calls vdev_dtl_reassess().
*/
/*
* Mark newvd's DTL dirty in this txg.
*/
/*
* Kick off a resilver to update newvd.
*/
return (0);
}
/*
* Detach a device from a mirror or replacing vdev.
* If 'replace_done' is specified, only detach if the parent
* is a replacing vdev.
*/
int
{
int c, t, error;
/*
* If replace_done is specified, only remove this device if it's
* the first child of a replacing vdev.
*/
if (replace_done &&
/*
* Only mirror and replacing vdevs support detach.
*/
/*
* If there's only one replica, you can't detach it.
*/
/*
* If all siblings have non-empty DTLs, this device may have the only
* valid copy of the data, which means we cannot safely detach it.
*
* XXX -- as in the vdev_offline() case, we really want a more
* precise DTL check.
*/
for (c = 0; c < pvd->vdev_children; c++) {
continue;
if (vdev_is_dead(cvd))
continue;
if (!dirty)
break;
}
if (c == pvd->vdev_children)
/*
* Erase the disk labels so the disk can be used for other things.
* This must be done after all other error cases are handled,
* but before we disembowel vd (so we can still do I/O to it).
* But if we can't do it, don't treat the error as fatal --
* it may be that the unwritability of the disk is the reason
* it's being detached!
*/
if (error)
/*
* Remove vd from its parent and compact the parent's children.
*/
/*
* Remember one of the remaining children so we can get tvd below.
*/
/*
* the parent is no longer needed. Remove it from the tree.
*/
/*
* We don't set tvd until now because the parent we just removed
* may have been the previous top-level vdev.
*/
/*
* Reopen this top-level vdev to reassess health after detach.
*/
/*
* If the device we just detached was smaller than the others,
* it may be possible to add metaslabs (i.e. grow the pool).
* vdev_metaslab_init() can't fail because the existing metaslabs
* are already in core, so there's nothing to read from disk.
*/
/*
* Mark vd's DTL as dirty in this txg.
* vdev_dtl_sync() will see that vd->vdev_detached is set
* and free vd's DTL object in syncing context.
* But first make sure we're not on any *other* txg's DTL list,
* to prevent vd from being accessed after it's freed.
*/
for (t = 0; t < TXG_SIZE; t++)
}
/*
* Find any device that's done replacing, so we can detach it.
*/
static vdev_t *
{
int c;
for (c = 0; c < vd->vdev_children; c++) {
return (oldvd);
}
return (oldvd);
}
}
return (NULL);
}
static void
{
return;
}
}
/*
* Update the stored path for this vdev. Dirty the vdev configuration, relying
* on spa_vdev_enter/exit() to synchronize the labels and cache.
*/
int
{
}
/*
* ==========================================================================
* SPA Scrubbing
* ==========================================================================
*/
void
{
if (spa->spa_scrub_throttled == 0)
}
static void
{
spa->spa_scrub_errors++;
}
if (--spa->spa_scrub_inflight == 0) {
}
}
static void
{
}
/* ARGSUSED */
static int
{
/*
* We can't scrub this block, but we can continue to scrub
* the rest of the pool. Note the error and move along.
*/
spa->spa_scrub_errors++;
}
return (ERESTART);
}
/*
* Keep track of how much data we've examined so that
* zpool(1M) status can make useful progress reports.
*/
/*
* Gang members may be spread across multiple vdevs,
* so the best we can do is look at the pool-wide DTL.
* XXX -- it would be better to change our allocation
* policy to ensure that this can't happen.
*/
}
}
} else {
}
return (0);
}
static void
{
int error = 0;
/*
* wait for that to complete.
*/
dprintf("start %s mintxg=%llu maxtxg=%llu\n",
spa->spa_scrub_errors = 0;
while (!spa->spa_scrub_stop) {
while (spa->spa_scrub_suspended) {
spa->spa_scrub_active = 0;
}
if (spa->spa_scrub_restart_txg != 0)
break;
break;
while (spa->spa_scrub_throttled > 0)
}
while (spa->spa_scrub_inflight)
spa->spa_scrub_active = 0;
/*
* Note: we check spa_scrub_restart_txg under both spa_scrub_lock
* AND the spa config lock to synchronize with any config changes
* that revise the DTLs under spa_vdev_enter() / spa_vdev_exit().
*/
if (spa->spa_scrub_restart_txg != 0)
if (spa->spa_scrub_stop)
/*
* Even if there were uncorrectable errors, we consider the scrub
* completed. The downside is that if there is a transient error during
* a resilver, we won't resilver the data properly to the target. But
* if the damage is permanent (more likely) we will resilver forever,
* which isn't really acceptable. Since there is enough information for
* the user to know what has failed and why, this seems like a more
* tractable approach.
*/
dprintf("end %s to maxtxg=%llu %s, traverse=%d, %llu errors, stop=%u\n",
/*
* Whether it succeeded or not, vacate all temporary scrub DTLs.
*/
/*
* We may have finished replacing a device.
* Let the async thread assess this and handle the detach.
*/
/*
* If we were told to restart, our final act is to start a new scrub.
*/
spa->spa_scrub_active = 0;
thread_exit();
}
void
{
while (spa->spa_scrub_active) {
}
while (spa->spa_scrub_inflight)
}
void
{
if (--spa->spa_scrub_suspended == 0)
}
void
{
/*
* we must restart any in-progress scrubs. The itinerary will
* fix this properly.
*/
}
int
{
return (ENOTSUP);
/*
* If there's a scrub or resilver already in progress, stop it.
*/
/*
* Don't stop a resilver unless forced.
*/
return (EBUSY);
}
}
/*
* Terminate the previous traverse.
*/
}
return (0);
}
/*
* The pool-wide DTL is empty.
* If this is a resilver, there's nothing to do except
* check whether any in-progress replacements have completed.
*/
if (type == POOL_SCRUB_RESILVER) {
}
} else {
/*
* The pool-wide DTL is non-empty.
* If this is a normal scrub, upgrade to a resilver instead.
*/
if (type == POOL_SCRUB_EVERYTHING)
}
if (type == POOL_SCRUB_RESILVER) {
/*
* Determine the resilvering boundaries.
*
* Note: (mintxg, maxtxg) is an open interval,
* i.e. mintxg and maxtxg themselves are not included.
*
* Note: for maxtxg, we MIN with spa_last_synced_txg(spa) + 1
* so we don't claim to resilver a txg that's still changing.
*/
}
spa->spa_scrub_stop = 0;
spa->spa_scrub_restart_txg = 0;
if (type != POOL_SCRUB_NONE) {
}
return (0);
}
/*
* ==========================================================================
* SPA async task processing
* ==========================================================================
*/
static void
{
int c;
for (c = 0; c < rvd->vdev_children; c++) {
if (tvd->vdev_reopen_wanted) {
tvd->vdev_reopen_wanted = 0;
}
}
}
static void
{
int tasks;
spa->spa_async_tasks = 0;
/*
* See if the config needs to be updated.
*/
if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
}
/*
* See if any devices need to be reopened.
*/
if (tasks & SPA_ASYNC_REOPEN)
/*
* If any devices are done replacing, detach them.
*/
if (tasks & SPA_ASYNC_REPLACE_DONE)
/*
* Kick off a scrub.
*/
if (tasks & SPA_ASYNC_SCRUB)
/*
* Kick off a resilver.
*/
if (tasks & SPA_ASYNC_RESILVER)
/*
* Let the world know that we're done.
*/
thread_exit();
}
void
{
}
void
{
}
static void
{
}
void
{
}
/*
* ==========================================================================
* SPA syncing routines
* ==========================================================================
*/
static void
{
int error;
uint8_t c = 1;
/*
* Pre-dirty the first block so we sync to convergence faster.
* (Usually only the first block is needed.)
*/
}
static void
{
return;
if (spa->spa_config_syncing)
KM_SLEEP) == 0);
}
/*
* Sync the specified transaction group. New blocks may be dirtied as
* part of the process, so we iterate until it converges.
*/
void
{
int dirty_vdevs;
/*
* Lock out configuration changes.
*/
spa->spa_sync_pass = 0;
/*
* If anything has changed in this txg, push the deferred frees
* from the previous txg. If not, leave them alone so that we
* don't generate work on an otherwise idle system.
*/
/*
* Iterate to convergence.
*/
do {
spa->spa_sync_pass++;
dirty_vdevs = 0;
dirty_vdevs++;
}
} while (dirty_vdevs);
/*
* Rewrite the vdev configuration (which includes the uberblock)
* to commit the transaction group.
*
* If there are any dirty vdevs, sync the uberblock to all vdevs.
* Otherwise, pick a random top-level vdev that's known to be
* visible in the config cache (see spa_vdev_add() for details).
* If the write fails, try the next vdev until we're tried them all.
*/
} else {
int c;
for (c = 0; c < children; c++) {
if (vd->vdev_ms_array == 0)
continue;
break;
}
if (c == children)
}
/*
* Clear the dirty config list.
*/
/*
* Now that the new config has synced transactionally,
* let it become visible to the config cache.
*/
}
/*
* Make a stable copy of the fully synced uberblock.
* We use this as the root for pool traversals.
*/
spa->spa_traverse_wanted = 0;
/*
* Clean up the ZIL records for the synced txg.
*/
/*
* Update usable space statistics.
*/
/*
* It had better be the case that we didn't dirty anything
* since spa_sync_labels().
*/
/*
* If any async tasks have been requested, kick them off.
*/
}
/*
* Sync all pools. We don't want to hold the namespace lock across these
* operations, so we take a reference on the spa_t and drop the lock during the
* sync.
*/
void
spa_sync_allpools(void)
{
continue;
}
}
/*
* ==========================================================================
* Miscellaneous routines
* ==========================================================================
*/
/*
* Remove all pools in the system.
*/
void
spa_evict_all(void)
{
/*
* Remove all cached state. All pools should be closed now,
* so every spa in the AVL tree should be unreferenced.
*/
/*
* Stop async tasks. The async thread may need to detach
* a device that's been replaced, which requires grabbing
* spa_namespace_lock, so we must drop it here.
*/
}
}
}
vdev_t *
{
}