diff options
author | Matthew Ahrens <mahrens@delphix.com> | 2013-08-14 11:42:31 -0800 |
---|---|---|
committer | Christopher Siden <chris.siden@delphix.com> | 2013-08-14 12:42:37 -0700 |
commit | a7027df17fad220a20367b9d1eb251bc6300d203 (patch) | |
tree | 618275195926e67c76f74aa0d53c5b11a3f56794 | |
parent | 559372b3a1f0f2d0da204a93e42942065acd4530 (diff) | |
download | illumos-joyent-a7027df17fad220a20367b9d1eb251bc6300d203.tar.gz |
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>
-rw-r--r-- | usr/src/lib/libzfs/common/libzfs_dataset.c | 12 | ||||
-rw-r--r-- | usr/src/lib/libzfs_core/common/libzfs_core.c | 26 | ||||
-rw-r--r-- | usr/src/lib/libzfs_core/common/libzfs_core.h | 3 | ||||
-rw-r--r-- | usr/src/lib/libzfs_core/common/mapfile-vers | 3 | ||||
-rw-r--r-- | usr/src/uts/common/fs/zfs/dsl_dataset.c | 15 | ||||
-rw-r--r-- | usr/src/uts/common/fs/zfs/sys/dsl_dataset.h | 2 | ||||
-rw-r--r-- | usr/src/uts/common/fs/zfs/zfs_ioctl.c | 25 |
7 files changed, 59 insertions, 27 deletions
diff --git a/usr/src/lib/libzfs/common/libzfs_dataset.c b/usr/src/lib/libzfs/common/libzfs_dataset.c index 7c331f9990..e9359bc2c6 100644 --- a/usr/src/lib/libzfs/common/libzfs_dataset.c +++ b/usr/src/lib/libzfs/common/libzfs_dataset.c @@ -3518,7 +3518,6 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) { rollback_data_t cb = { 0 }; int err; - zfs_cmd_t zc = { 0 }; boolean_t restore_resv = 0; uint64_t old_volsize, new_volsize; zfs_prop_t resv_prop; @@ -3550,22 +3549,15 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) (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 * newer snapshots for the given dataset. Therefore, we can * simply pass the name on to the ioctl() call. There is still * an unlikely race condition where the user has taken a * 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, dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), zhp->zfs_name); diff --git a/usr/src/lib/libzfs_core/common/libzfs_core.c b/usr/src/lib/libzfs_core/common/libzfs_core.c index a4f51233f4..20d7329677 100644 --- a/usr/src/lib/libzfs_core/common/libzfs_core.c +++ b/usr/src/lib/libzfs_core/common/libzfs_core.c @@ -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. */ @@ -581,3 +581,27 @@ out: free((void*)(uintptr_t)zc.zc_nvlist_dst); 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); +} diff --git a/usr/src/lib/libzfs_core/common/libzfs_core.h b/usr/src/lib/libzfs_core/common/libzfs_core.h index f5fd6cda9f..3642dc7afd 100644 --- a/usr/src/lib/libzfs_core/common/libzfs_core.h +++ b/usr/src/lib/libzfs_core/common/libzfs_core.h @@ -20,7 +20,7 @@ */ /* - * Copyright (c) 2012 by Delphix. All rights reserved. + * Copyright (c) 2013 by Delphix. All rights reserved. */ #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); +int lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen); #ifdef __cplusplus } diff --git a/usr/src/lib/libzfs_core/common/mapfile-vers b/usr/src/lib/libzfs_core/common/mapfile-vers index 1c9930a9cd..a4a110f907 100644 --- a/usr/src/lib/libzfs_core/common/mapfile-vers +++ b/usr/src/lib/libzfs_core/common/mapfile-vers @@ -19,7 +19,7 @@ # CDDL HEADER END # # Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. -# Copyright (c) 2012 by Delphix. All rights reserved. +# Copyright (c) 2013 by Delphix. All rights reserved. # # MAPFILE HEADER START # @@ -49,6 +49,7 @@ SYMBOL_VERSION ILLUMOS_0.1 { lzc_hold; lzc_receive; lzc_release; + lzc_rollback; lzc_send; lzc_send_space; lzc_snaprange_space; diff --git a/usr/src/uts/common/fs/zfs/dsl_dataset.c b/usr/src/uts/common/fs/zfs/dsl_dataset.c index be117151ad..912c15a943 100644 --- a/usr/src/uts/common/fs/zfs/dsl_dataset.c +++ b/usr/src/uts/common/fs/zfs/dsl_dataset.c @@ -1722,6 +1722,7 @@ dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx) typedef struct dsl_dataset_rollback_arg { const char *ddra_fsname; void *ddra_owner; + nvlist_t *ddra_result; } dsl_dataset_rollback_arg_t; static int @@ -1793,9 +1794,13 @@ dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) dsl_pool_t *dp = dmu_tx_pool(tx); dsl_dataset_t *ds, *clone; uint64_t cloneobj; + char namebuf[ZFS_MAXNAMELEN]; 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", ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx); @@ -1811,8 +1816,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 * - Upon return, dataset will still be held by the same owner, whether we * succeed or not. @@ -1821,15 +1829,16 @@ dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx) * notes above zfs_suspend_fs() for further details. */ 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; ddra.ddra_fsname = fsname; ddra.ddra_owner = owner; + ddra.ddra_result = result; return (dsl_sync_task(fsname, dsl_dataset_rollback_check, - dsl_dataset_rollback_sync, (void *)&ddra, 1)); + dsl_dataset_rollback_sync, &ddra, 1)); } struct promotenode { diff --git a/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h b/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h index fa7cad7f76..5605e9298c 100644 --- a/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h +++ b/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h @@ -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); void dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds, 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 #define dprintf_ds(ds, fmt, ...) do { \ diff --git a/usr/src/uts/common/fs/zfs/zfs_ioctl.c b/usr/src/uts/common/fs/zfs/zfs_ioctl.c index e8fd90459e..c843d532e4 100644 --- a/usr/src/uts/common/fs/zfs/zfs_ioctl.c +++ b/usr/src/uts/common/fs/zfs/zfs_ioctl.c @@ -3493,29 +3493,32 @@ zfs_ioc_destroy(zfs_cmd_t *zc) } /* - * inputs: - * zc_name name of dataset to rollback (to most recent snapshot) + * fsname is 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 -zfs_ioc_rollback(zfs_cmd_t *zc) +zfs_ioc_rollback(const char *fsname, nvlist_t *args, nvlist_t *outnvl) { zfsvfs_t *zfsvfs; int error; - if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { + if (getzfsvfs(fsname, &zfsvfs) == 0) { error = zfs_suspend_fs(zfsvfs); if (error == 0) { int resume_err; - error = dsl_dataset_rollback(zc->zc_name, zfsvfs); - resume_err = zfs_resume_fs(zfsvfs, zc->zc_name); + error = dsl_dataset_rollback(fsname, zfsvfs, outnvl); + resume_err = zfs_resume_fs(zfsvfs, fsname); error = error ? error : resume_err; } VFS_RELE(zfsvfs->z_vfs); } else { - error = dsl_dataset_rollback(zc->zc_name, NULL); + error = dsl_dataset_rollback(fsname, NULL, outnvl); } return (error); } @@ -5327,6 +5330,10 @@ zfs_ioctl_init(void) zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, 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 */ zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, @@ -5438,8 +5445,6 @@ zfs_ioctl_init(void) zfs_secpolicy_none); zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_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_secpolicy_rename); zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, |