diff options
author | Andriy Gapon <[email protected]> | 2017-03-11 20:26:47 +0200 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2017-07-04 15:29:52 -0700 |
commit | 8ca78ab00278332a877d7d95e057c0b4aca5f9ad (patch) | |
tree | 60f894137651ee83fba59b74625fdd8798c66c8c /lib | |
parent | 018503911c3a4d01768270c69e6ec87b3034e86f (diff) |
OpenZFS 7600 - zfs rollback should pass target snapshot to kernel
Authored by: Andriy Gapon <[email protected]>
Reviewed by: Matthew Ahrens <[email protected]>
Reviewed by: Pavel Zakharov <[email protected]>
Approved by: Robert Mustacchi <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Ported-by: Giuseppe Di Natale <[email protected]>
The existing kernel-side code only provides a method to rollback to a
latest snapshot, whatever it happens to be at the time when the rollback
is actually done. That could be unsafe or confusing in environments
where concurrent DSL changes are possible as the resulting state could
correspond to a newer or older snapshot than the originally requested
one.
This change allows to amend that method such that the rollback is
performed only when the latest snapshot has a specific name. That is,
if a new snapshot is concurrently created or the target snapshot is
destroyed, then no rollback is done and EXDEV error is returned.
New libzfs_core function lzc_rollback_to() is provided for the new
functionality. libzfs is changed to use lzc_rollback_to() to implement
zfs rollback command.
Perhaps we should return different errors to distinguish the case where
the desired snapshot exists but it's not the latest snapshot and the
case where the desired snapshot does not exist.
OpenZFS-issue: https://www.illumos.org/issues/7600
OpenZFS-commit: https://github.com/openzfs/openzfs/commit/3d645eb
Closes #6292
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libzfs/libzfs_dataset.c | 19 | ||||
-rw-r--r-- | lib/libzfs_core/libzfs_core.c | 24 |
2 files changed, 36 insertions, 7 deletions
diff --git a/lib/libzfs/libzfs_dataset.c b/lib/libzfs/libzfs_dataset.c index a4f65b397..d6e85024d 100644 --- a/lib/libzfs/libzfs_dataset.c +++ b/lib/libzfs/libzfs_dataset.c @@ -4018,14 +4018,19 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) } /* - * 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. + * Pass both the filesystem and the wanted snapshot names, + * we would get an error back if the snapshot is destroyed or + * a new snapshot is created before this request is processed. */ - err = lzc_rollback(zhp->zfs_name, NULL, 0); - if (err != 0) { + err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); + if (err == EXDEV) { + zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, + "'%s' is not the latest snapshot"), snap->zfs_name); + (void) zfs_error_fmt(zhp->zfs_hdl, EZFS_BUSY, + dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), + zhp->zfs_name); + return (err); + } else if (err != 0) { (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), zhp->zfs_name); diff --git a/lib/libzfs_core/libzfs_core.c b/lib/libzfs_core/libzfs_core.c index 8142f2c63..347d825e2 100644 --- a/lib/libzfs_core/libzfs_core.c +++ b/lib/libzfs_core/libzfs_core.c @@ -888,6 +888,9 @@ int lzc_receive_with_cmdprops(const char *snapname, nvlist_t *props, * 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. + * Note that the latest snapshot may change if a new one is concurrently + * created or the current one is destroyed. lzc_rollback_to can be used + * to roll back to a specific latest snapshot. * * Return 0 on success or an errno on failure. */ @@ -911,6 +914,27 @@ lzc_rollback(const char *fsname, char *snapnamebuf, int snapnamelen) } /* + * Roll back this filesystem or volume to the specified snapshot, + * if possible. + * + * Return 0 on success or an errno on failure. + */ +int +lzc_rollback_to(const char *fsname, const char *snapname) +{ + nvlist_t *args; + nvlist_t *result; + int err; + + args = fnvlist_alloc(); + fnvlist_add_string(args, "target", snapname); + err = lzc_ioctl(ZFS_IOC_ROLLBACK, fsname, args, &result); + nvlist_free(args); + nvlist_free(result); + return (err); +} + +/* * Creates bookmarks. * * The bookmarks nvlist maps from name of the bookmark (e.g. "pool/fs#bmark") to |