Skip to content

Commit

Permalink
3090 vdev_reopen() during reguid causes vdev to be treated as corrupt
Browse files Browse the repository at this point in the history
3102 vdev_uberblock_load() and vdev_validate() may read the wrong label
Reviewed by: Matthew Ahrens <[email protected]>
Reviewed by: Christopher Siden <[email protected]>
Reviewed by: Garrett D'Amore <[email protected]>
Approved by: Eric Schrock <[email protected]>
  • Loading branch information
grwilson committed Aug 22, 2012
1 parent ce636f8 commit dfbb943
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 100 deletions.
23 changes: 21 additions & 2 deletions usr/src/cmd/ztest/ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ ztest_info_t ztest_info[] = {
{ ztest_spa_rename, 1, &zopt_rarely },
{ ztest_scrub, 1, &zopt_rarely },
{ ztest_dsl_dataset_promote_busy, 1, &zopt_rarely },
{ ztest_vdev_attach_detach, 1, &zopt_rarely },
{ ztest_vdev_attach_detach, 1, &zopt_rarely },
{ ztest_vdev_LUN_growth, 1, &zopt_rarely },
{ ztest_vdev_add_remove, 1,
&ztest_opts.zo_vdevtime },
Expand Down Expand Up @@ -413,6 +413,13 @@ static spa_t *ztest_spa = NULL;
static ztest_ds_t *ztest_ds;

static mutex_t ztest_vdev_lock;

/*
* The ztest_name_lock protects the pool and dataset namespace used by
* the individual tests. To modify the namespace, consumers must grab
* this lock as writer. Grabbing the lock as reader will ensure that the
* namespace does not change while the lock is held.
*/
static rwlock_t ztest_name_lock;

static boolean_t ztest_dump_core = B_TRUE;
Expand Down Expand Up @@ -4854,10 +4861,16 @@ ztest_reguid(ztest_ds_t *zd, uint64_t id)
{
spa_t *spa = ztest_spa;
uint64_t orig, load;
int error;

orig = spa_guid(spa);
load = spa_load_guid(spa);
if (spa_change_guid(spa) != 0)

(void) rw_wrlock(&ztest_name_lock);
error = spa_change_guid(spa);
(void) rw_unlock(&ztest_name_lock);

if (error != 0)
return;

if (ztest_opts.zo_verbose >= 3) {
Expand Down Expand Up @@ -5536,6 +5549,12 @@ ztest_freeze(void)
ASSERT(spa_freeze_txg(spa) == UINT64_MAX);
VERIFY3U(0, ==, ztest_dataset_open(0));
ztest_dataset_close(0);

spa->spa_debug = B_TRUE;
ztest_spa = spa;
txg_wait_synced(spa_get_dsl(spa), 0);
ztest_reguid(NULL, 0);

spa_close(spa, FTAG);
kernel_fini();
}
Expand Down
85 changes: 39 additions & 46 deletions usr/src/lib/libzfs/common/libzfs_import.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/*
* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2011 by Delphix. All rights reserved.
* Copyright (c) 2012 by Delphix. All rights reserved.
*/

/*
Expand Down Expand Up @@ -439,8 +439,8 @@ get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
uint_t i, nspares, nl2cache;
boolean_t config_seen;
uint64_t best_txg;
char *name, *hostname, *comment;
uint64_t version, guid;
char *name, *hostname;
uint64_t guid;
uint_t children = 0;
nvlist_t **child = NULL;
uint_t holes;
Expand Down Expand Up @@ -526,61 +526,54 @@ get_configs(libzfs_handle_t *hdl, pool_list_t *pl, boolean_t active_ok)
* configuration:
*
* version
* pool guid
* name
* pool guid
* name
* pool txg (if available)
* comment (if available)
* pool state
* pool state
* hostid (if available)
* hostname (if available)
*/
uint64_t state;
uint64_t state, version, pool_txg;
char *comment = NULL;

version = fnvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_VERSION);
fnvlist_add_uint64(config,
ZPOOL_CONFIG_VERSION, version);
guid = fnvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_POOL_GUID);
fnvlist_add_uint64(config,
ZPOOL_CONFIG_POOL_GUID, guid);
name = fnvlist_lookup_string(tmp,
ZPOOL_CONFIG_POOL_NAME);
fnvlist_add_string(config,
ZPOOL_CONFIG_POOL_NAME, name);

verify(nvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_VERSION, &version) == 0);
if (nvlist_add_uint64(config,
ZPOOL_CONFIG_VERSION, version) != 0)
goto nomem;
verify(nvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_POOL_GUID, &guid) == 0);
if (nvlist_add_uint64(config,
ZPOOL_CONFIG_POOL_GUID, guid) != 0)
goto nomem;
verify(nvlist_lookup_string(tmp,
ZPOOL_CONFIG_POOL_NAME, &name) == 0);
if (nvlist_add_string(config,
ZPOOL_CONFIG_POOL_NAME, name) != 0)
goto nomem;
if (nvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_POOL_TXG, &pool_txg) == 0)
fnvlist_add_uint64(config,
ZPOOL_CONFIG_POOL_TXG, pool_txg);

/*
* COMMENT is optional, don't bail if it's not
* there, instead, set it to NULL.
*/
if (nvlist_lookup_string(tmp,
ZPOOL_CONFIG_COMMENT, &comment) != 0)
comment = NULL;
else if (nvlist_add_string(config,
ZPOOL_CONFIG_COMMENT, comment) != 0)
goto nomem;
ZPOOL_CONFIG_COMMENT, &comment) == 0)
fnvlist_add_string(config,
ZPOOL_CONFIG_COMMENT, comment);

verify(nvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_POOL_STATE, &state) == 0);
if (nvlist_add_uint64(config,
ZPOOL_CONFIG_POOL_STATE, state) != 0)
goto nomem;
state = fnvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_POOL_STATE);
fnvlist_add_uint64(config,
ZPOOL_CONFIG_POOL_STATE, state);

hostid = 0;
if (nvlist_lookup_uint64(tmp,
ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
if (nvlist_add_uint64(config,
ZPOOL_CONFIG_HOSTID, hostid) != 0)
goto nomem;
verify(nvlist_lookup_string(tmp,
ZPOOL_CONFIG_HOSTNAME,
&hostname) == 0);
if (nvlist_add_string(config,
ZPOOL_CONFIG_HOSTNAME,
hostname) != 0)
goto nomem;
fnvlist_add_uint64(config,
ZPOOL_CONFIG_HOSTID, hostid);
hostname = fnvlist_lookup_string(tmp,
ZPOOL_CONFIG_HOSTNAME);
fnvlist_add_string(config,
ZPOOL_CONFIG_HOSTNAME, hostname);
}

config_seen = B_TRUE;
Expand Down
76 changes: 58 additions & 18 deletions usr/src/uts/common/fs/zfs/spa.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {

static dsl_syncfunc_t spa_sync_version;
static dsl_syncfunc_t spa_sync_props;
static dsl_checkfunc_t spa_change_guid_check;
static dsl_syncfunc_t spa_change_guid_sync;
static boolean_t spa_has_active_shared_spare(spa_t *spa);
static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
Expand Down Expand Up @@ -675,6 +677,47 @@ spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
}
}

/*ARGSUSED*/
static int
spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx)
{
spa_t *spa = arg1;
uint64_t *newguid = arg2;
vdev_t *rvd = spa->spa_root_vdev;
uint64_t vdev_state;

spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
vdev_state = rvd->vdev_state;
spa_config_exit(spa, SCL_STATE, FTAG);

if (vdev_state != VDEV_STATE_HEALTHY)
return (ENXIO);

ASSERT3U(spa_guid(spa), !=, *newguid);

return (0);
}

static void
spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx)
{
spa_t *spa = arg1;
uint64_t *newguid = arg2;
uint64_t oldguid;
vdev_t *rvd = spa->spa_root_vdev;

oldguid = spa_guid(spa);

spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
rvd->vdev_guid = *newguid;
rvd->vdev_guid_sum += (*newguid - oldguid);
vdev_config_dirty(rvd);
spa_config_exit(spa, SCL_STATE, FTAG);

spa_history_log_internal(spa, "guid change", tx, "old=%lld new=%lld",
oldguid, *newguid);
}

/*
* Change the GUID for the pool. This is done so that we can later
* re-import a pool built from a clone of our own vdevs. We will modify
Expand All @@ -687,29 +730,23 @@ spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
int
spa_change_guid(spa_t *spa)
{
uint64_t oldguid, newguid;
uint64_t txg;

if (!(spa_mode_global & FWRITE))
return (EROFS);

txg = spa_vdev_enter(spa);

if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
return (spa_vdev_exit(spa, NULL, txg, ENXIO));
int error;
uint64_t guid;

oldguid = spa_guid(spa);
newguid = spa_generate_guid(NULL);
ASSERT3U(oldguid, !=, newguid);
mutex_enter(&spa_namespace_lock);
guid = spa_generate_guid(NULL);

spa->spa_root_vdev->vdev_guid = newguid;
spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check,
spa_change_guid_sync, spa, &guid, 5);

vdev_config_dirty(spa->spa_root_vdev);
if (error == 0) {
spa_config_sync(spa, B_FALSE, B_TRUE);
spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
}

spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
mutex_exit(&spa_namespace_lock);

return (spa_vdev_exit(spa, NULL, txg, 0));
return (error);
}

/*
Expand Down Expand Up @@ -6062,6 +6099,9 @@ spa_sync(spa_t *spa, uint64_t txg)
rvd->vdev_children, txg, B_TRUE);
}

if (error == 0)
spa->spa_last_synced_guid = rvd->vdev_guid;

spa_config_exit(spa, SCL_STATE, FTAG);

if (error == 0)
Expand Down
17 changes: 15 additions & 2 deletions usr/src/uts/common/fs/zfs/spa_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1348,16 +1348,29 @@ spa_name(spa_t *spa)
uint64_t
spa_guid(spa_t *spa)
{
dsl_pool_t *dp = spa_get_dsl(spa);
uint64_t guid;

/*
* If we fail to parse the config during spa_load(), we can go through
* the error path (which posts an ereport) and end up here with no root
* vdev. We stash the original pool guid in 'spa_config_guid' to handle
* this case.
*/
if (spa->spa_root_vdev != NULL)
if (spa->spa_root_vdev == NULL)
return (spa->spa_config_guid);

guid = spa->spa_last_synced_guid != 0 ?
spa->spa_last_synced_guid : spa->spa_root_vdev->vdev_guid;

/*
* Return the most recently synced out guid unless we're
* in syncing context.
*/
if (dp && dsl_pool_sync_context(dp))
return (spa->spa_root_vdev->vdev_guid);
else
return (spa->spa_config_guid);
return (guid);
}

uint64_t
Expand Down
1 change: 1 addition & 0 deletions usr/src/uts/common/fs/zfs/sys/spa_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ struct spa {
vdev_t *spa_root_vdev; /* top-level vdev container */
uint64_t spa_config_guid; /* config pool guid */
uint64_t spa_load_guid; /* spa_load initialized guid */
uint64_t spa_last_synced_guid; /* last synced guid */
list_t spa_config_dirty_list; /* vdevs with dirty config */
list_t spa_state_dirty_list; /* vdevs with dirty state */
spa_aux_vdev_t spa_spares; /* hot spares */
Expand Down
2 changes: 1 addition & 1 deletion usr/src/uts/common/fs/zfs/sys/vdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ extern nvlist_t *vdev_config_generate(spa_t *spa, vdev_t *vd,
struct uberblock;
extern uint64_t vdev_label_offset(uint64_t psize, int l, uint64_t offset);
extern int vdev_label_number(uint64_t psise, uint64_t offset);
extern nvlist_t *vdev_label_read_config(vdev_t *vd, int label);
extern nvlist_t *vdev_label_read_config(vdev_t *vd, uint64_t txg);
extern void vdev_uberblock_load(vdev_t *, struct uberblock *, nvlist_t **);

typedef enum {
Expand Down
8 changes: 4 additions & 4 deletions usr/src/uts/common/fs/zfs/vdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1327,9 +1327,9 @@ vdev_validate(vdev_t *vd, boolean_t strict)
if (vd->vdev_ops->vdev_op_leaf && vdev_readable(vd)) {
uint64_t aux_guid = 0;
nvlist_t *nvl;
uint64_t txg = strict ? spa->spa_config_txg : -1ULL;

if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) ==
NULL) {
if ((label = vdev_label_read_config(vd, txg)) == NULL) {
vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
VDEV_AUX_BAD_LABEL);
return (0);
Expand Down Expand Up @@ -1511,7 +1511,7 @@ vdev_reopen(vdev_t *vd)
!l2arc_vdev_present(vd))
l2arc_add_vdev(spa, vd);
} else {
(void) vdev_validate(vd, B_TRUE);
(void) vdev_validate(vd, spa_last_synced_txg(spa));
}

/*
Expand Down Expand Up @@ -1970,7 +1970,7 @@ vdev_validate_aux(vdev_t *vd)
if (!vdev_readable(vd))
return (0);

if ((label = vdev_label_read_config(vd, VDEV_BEST_LABEL)) == NULL) {
if ((label = vdev_label_read_config(vd, -1ULL)) == NULL) {
vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
VDEV_AUX_CORRUPT_DATA);
return (-1);
Expand Down
Loading

0 comments on commit dfbb943

Please sign in to comment.