3996 want a libzfs_core API to rollback to latest snapshot
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Andy Stormont <andyjstormont@gmail.com>
Approved by: Richard Lowe <richlowe@richlowe.net>

References:
  https://www.illumos.org/issues/3996
  illumos/illumos-gate@a7027df17f

Ported-by: Richard Yao <ryao@gentoo.org>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Issue #1775
This commit is contained in:
Matthew Ahrens 2013-08-14 11:42:31 -08:00 committed by Brian Behlendorf
parent 5d1f7fb647
commit 46ba1e59d3
6 changed files with 57 additions and 26 deletions

View File

@ -20,7 +20,7 @@
*/ */
/* /*
* Copyright (c) 2012 by Delphix. All rights reserved. * Copyright (c) 2013 by Delphix. All rights reserved.
*/ */
#ifndef _LIBZFS_CORE_H #ifndef _LIBZFS_CORE_H
@ -58,6 +58,7 @@ int lzc_send_space(const char *snapname, const char *fromsnap,
boolean_t lzc_exists(const char *dataset); boolean_t lzc_exists(const char *dataset);
int lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -265,7 +265,7 @@ int dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name,
int dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx); int dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx);
void dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, void dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
zprop_source_t source, uint64_t value, dmu_tx_t *tx); zprop_source_t source, uint64_t value, dmu_tx_t *tx);
int dsl_dataset_rollback(const char *fsname, void *owner); int dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result);
#ifdef ZFS_DEBUG #ifdef ZFS_DEBUG
#define dprintf_ds(ds, fmt, ...) do { \ #define dprintf_ds(ds, fmt, ...) do { \

View File

@ -3732,7 +3732,6 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
{ {
rollback_data_t cb = { 0 }; rollback_data_t cb = { 0 };
int err; int err;
zfs_cmd_t zc = {"\0"};
boolean_t restore_resv = 0; boolean_t restore_resv = 0;
uint64_t old_volsize = 0, new_volsize; uint64_t old_volsize = 0, new_volsize;
zfs_prop_t resv_prop = { 0 }; zfs_prop_t resv_prop = { 0 };
@ -3766,22 +3765,15 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
(old_volsize == zfs_prop_get_int(zhp, resv_prop)); (old_volsize == zfs_prop_get_int(zhp, resv_prop));
} }
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
if (ZFS_IS_VOLUME(zhp))
zc.zc_objset_type = DMU_OST_ZVOL;
else
zc.zc_objset_type = DMU_OST_ZFS;
/* /*
* We rely on zfs_iter_children() to verify that there are no * We rely on zfs_iter_children() to verify that there are no
* newer snapshots for the given dataset. Therefore, we can * newer snapshots for the given dataset. Therefore, we can
* simply pass the name on to the ioctl() call. There is still * simply pass the name on to the ioctl() call. There is still
* an unlikely race condition where the user has taken a * an unlikely race condition where the user has taken a
* snapshot since we verified that this was the most recent. * snapshot since we verified that this was the most recent.
*
*/ */
if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { err = lzc_rollback(zhp->zfs_name, NULL, 0);
if (err != 0) {
(void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
zhp->zfs_name); zhp->zfs_name);

View File

@ -20,7 +20,7 @@
*/ */
/* /*
* Copyright (c) 2012 by Delphix. All rights reserved. * Copyright (c) 2013 by Delphix. All rights reserved.
* Copyright (c) 2013 Steven Hartland. All rights reserved. * Copyright (c) 2013 Steven Hartland. All rights reserved.
*/ */
@ -581,3 +581,27 @@ out:
free((void*)(uintptr_t)zc.zc_nvlist_dst); free((void*)(uintptr_t)zc.zc_nvlist_dst);
return (error); return (error);
} }
/*
* Roll back this filesystem or volume to its most recent snapshot.
* If snapnamebuf is not NULL, it will be filled in with the name
* of the most recent snapshot.
*
* Return 0 on success or an errno on failure.
*/
int
lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen)
{
nvlist_t *args;
nvlist_t *result;
int err;
args = fnvlist_alloc();
err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result);
nvlist_free(args);
if (err == 0 && snapnamebuf != NULL) {
const char *snapname = fnvlist_lookup_string(result, "target");
(void) strlcpy(snapnamebuf, snapname, snapnamelen);
}
return (err);
}

View File

@ -1703,6 +1703,7 @@ dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
typedef struct dsl_dataset_rollback_arg { typedef struct dsl_dataset_rollback_arg {
const char *ddra_fsname; const char *ddra_fsname;
void *ddra_owner; void *ddra_owner;
nvlist_t *ddra_result;
} dsl_dataset_rollback_arg_t; } dsl_dataset_rollback_arg_t;
static int static int
@ -1774,9 +1775,13 @@ dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
dsl_pool_t *dp = dmu_tx_pool(tx); dsl_pool_t *dp = dmu_tx_pool(tx);
dsl_dataset_t *ds, *clone; dsl_dataset_t *ds, *clone;
uint64_t cloneobj; uint64_t cloneobj;
char namebuf[ZFS_MAXNAMELEN];
VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds)); VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
dsl_dataset_name(ds->ds_prev, namebuf);
fnvlist_add_string(ddra->ddra_result, "target", namebuf);
cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback", cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
@ -1792,8 +1797,11 @@ dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
} }
/* /*
* If owner != NULL: * Rolls back the given filesystem or volume to the most recent snapshot.
* The name of the most recent snapshot will be returned under key "target"
* in the result nvlist.
* *
* If owner != NULL:
* - The existing dataset MUST be owned by the specified owner at entry * - The existing dataset MUST be owned by the specified owner at entry
* - Upon return, dataset will still be held by the same owner, whether we * - Upon return, dataset will still be held by the same owner, whether we
* succeed or not. * succeed or not.
@ -1802,15 +1810,16 @@ dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
* notes above zfs_suspend_fs() for further details. * notes above zfs_suspend_fs() for further details.
*/ */
int int
dsl_dataset_rollback(const char *fsname, void *owner) dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
{ {
dsl_dataset_rollback_arg_t ddra; dsl_dataset_rollback_arg_t ddra;
ddra.ddra_fsname = fsname; ddra.ddra_fsname = fsname;
ddra.ddra_owner = owner; ddra.ddra_owner = owner;
ddra.ddra_result = result;
return (dsl_sync_task(fsname, dsl_dataset_rollback_check, return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
dsl_dataset_rollback_sync, (void *)&ddra, 1)); dsl_dataset_rollback_sync, &ddra, 1));
} }
struct promotenode { struct promotenode {

View File

@ -3488,29 +3488,32 @@ zfs_ioc_destroy(zfs_cmd_t *zc)
} }
/* /*
* inputs: * fsname is name of dataset to rollback (to most recent snapshot)
* zc_name name of dataset to rollback (to most recent snapshot)
* *
* outputs: none * innvl is not used.
*
* outnvl: "target" -> name of most recent snapshot
* }
*/ */
/* ARGSUSED */
static int static int
zfs_ioc_rollback(zfs_cmd_t *zc) zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl)
{ {
zfs_sb_t *zsb; zfs_sb_t *zsb;
int error; int error;
if (get_zfs_sb(zc->zc_name, &zsb) == 0) { if (get_zfs_sb(fsname, &zsb) == 0) {
error = zfs_suspend_fs(zsb); error = zfs_suspend_fs(zsb);
if (error == 0) { if (error == 0) {
int resume_err; int resume_err;
error = dsl_dataset_rollback(zc->zc_name, zsb); error = dsl_dataset_rollback(fsname, zsb, outnvl);
resume_err = zfs_resume_fs(zsb, zc->zc_name); resume_err = zfs_resume_fs(zsb, fsname);
error = error ? error : resume_err; error = error ? error : resume_err;
} }
deactivate_super(zsb->z_sb); deactivate_super(zsb->z_sb);
} else { } else {
error = dsl_dataset_rollback(zc->zc_name, NULL); error = dsl_dataset_rollback(fsname, NULL, outnvl);
} }
return (error); return (error);
} }
@ -5273,6 +5276,10 @@ zfs_ioctl_init(void)
zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
/* IOCTLS that use the legacy function signature */ /* IOCTLS that use the legacy function signature */
zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
@ -5384,8 +5391,6 @@ zfs_ioctl_init(void)
zfs_secpolicy_none); zfs_secpolicy_none);
zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy,
zfs_secpolicy_destroy); zfs_secpolicy_destroy);
zfs_ioctl_register_dataset_modify(ZFS_IOC_ROLLBACK, zfs_ioc_rollback,
zfs_secpolicy_rollback);
zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename,
zfs_secpolicy_rename); zfs_secpolicy_rename);
zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv,