-struct hold_range_arg {
- zfs_handle_t *origin;
- const char *fromsnap;
- const char *tosnap;
- char lastsnapheld[ZFS_MAXNAMELEN];
- const char *tag;
- boolean_t temphold;
- boolean_t seento;
- boolean_t seenfrom;
- boolean_t holding;
- boolean_t recursive;
- snapfilter_cb_t *filter_cb;
- void *filter_cb_arg;
-};
-
-static int
-zfs_hold_range_one(zfs_handle_t *zhp, void *arg)
-{
- struct hold_range_arg *hra = arg;
- const char *thissnap;
- int error;
-
- thissnap = strchr(zfs_get_name(zhp), '@') + 1;
-
- if (hra->fromsnap && !hra->seenfrom &&
- strcmp(hra->fromsnap, thissnap) == 0)
- hra->seenfrom = B_TRUE;
-
- /* snap is older or newer than the desired range, ignore it */
- if (hra->seento || !hra->seenfrom) {
- zfs_close(zhp);
- return (0);
- }
-
- if (!hra->seento && strcmp(hra->tosnap, thissnap) == 0)
- hra->seento = B_TRUE;
-
- if (hra->filter_cb != NULL &&
- hra->filter_cb(zhp, hra->filter_cb_arg) == B_FALSE) {
- zfs_close(zhp);
- return (0);
- }
-
- if (hra->holding) {
- /* We could be racing with destroy, so ignore ENOENT. */
- error = zfs_hold(hra->origin, thissnap, hra->tag,
- hra->recursive, hra->temphold, B_TRUE);
- if (error == 0) {
- (void) strlcpy(hra->lastsnapheld, zfs_get_name(zhp),
- sizeof (hra->lastsnapheld));
- }
- } else {
- error = zfs_release(hra->origin, thissnap, hra->tag,
- hra->recursive);
- }
-
- zfs_close(zhp);
- return (error);
-}
-
-/*
- * Add a user hold on the set of snapshots starting with fromsnap up to
- * and including tosnap. If we're unable to to acquire a particular hold,
- * undo any holds up to that point.
- */
-int
-zfs_hold_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
- const char *tag, boolean_t recursive, boolean_t temphold,
- snapfilter_cb_t filter_cb, void *cbarg)
-{
- struct hold_range_arg arg = { 0 };
- int error;
-
- arg.origin = zhp;
- arg.fromsnap = fromsnap;
- arg.tosnap = tosnap;
- arg.tag = tag;
- arg.temphold = temphold;
- arg.holding = B_TRUE;
- arg.recursive = recursive;
- arg.seenfrom = (fromsnap == NULL);
- arg.filter_cb = filter_cb;
- arg.filter_cb_arg = cbarg;
-
- error = zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg);
-
- /*
- * Make sure we either hold the entire range or none.
- */
- if (error && arg.lastsnapheld[0] != '\0') {
- (void) zfs_release_range(zhp, fromsnap,
- (const char *)arg.lastsnapheld, tag, recursive);
- }
- return (error);
-}
-