Skip to content

Commit

Permalink
Add createtxg sort support for simple snapshot iterator
Browse files Browse the repository at this point in the history
- When iterating snapshots with name only, e.g., "-o name -s name",
libzfs uses simple snapshot iterator and results are displayed
in alphabetic order. This PR adds support for faster version of
createtxg sort by avoiding nvlist parsing for properties. Flags
"-o name -s createtxg" will enable createtxg sort while using
simple snapshot iterator.
- Added support to read createtxg property directly from zfs handle
for filesystem, volume and snapshot types instead of parsing nvlist.

Reviewed-by: Ryan Moeller <[email protected]>
Reviewed-by: Alexander Motin <[email protected]>
Signed-off-by: Ameer Hamza <[email protected]>
Closes #13577
  • Loading branch information
ixhamza authored Jul 25, 2022
1 parent 8792dd2 commit 3a1ce49
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
15 changes: 10 additions & 5 deletions cmd/zfs/zfs_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ zfs_sort_only_by_name(const zfs_sort_column_t *sc)
sc->sc_prop == ZFS_PROP_NAME);
}

int
zfs_sort_only_by_createtxg(const zfs_sort_column_t *sc)
{
return (sc != NULL && sc->sc_next == NULL &&
sc->sc_prop == ZFS_PROP_CREATETXG);
}

static int
zfs_compare(const void *larg, const void *rarg)
{
Expand Down Expand Up @@ -301,7 +308,7 @@ zfs_sort(const void *larg, const void *rarg, void *data)
for (psc = sc; psc != NULL; psc = psc->sc_next) {
char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
char *lstr, *rstr;
uint64_t lnum, rnum;
uint64_t lnum = 0, rnum = 0;
boolean_t lvalid, rvalid;
int ret = 0;

Expand Down Expand Up @@ -352,11 +359,9 @@ zfs_sort(const void *larg, const void *rarg, void *data)
zfs_get_type(r), B_FALSE);

if (lvalid)
(void) zfs_prop_get_numeric(l, psc->sc_prop,
&lnum, NULL, NULL, 0);
lnum = zfs_prop_get_int(l, psc->sc_prop);
if (rvalid)
(void) zfs_prop_get_numeric(r, psc->sc_prop,
&rnum, NULL, NULL, 0);
rnum = zfs_prop_get_int(r, psc->sc_prop);
}

if (!lvalid && !rvalid)
Expand Down
1 change: 1 addition & 0 deletions cmd/zfs/zfs_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ int zfs_for_each(int, char **, int options, zfs_type_t,
int zfs_add_sort_column(zfs_sort_column_t **, const char *, boolean_t);
void zfs_free_sort_columns(zfs_sort_column_t *);
int zfs_sort_only_by_name(const zfs_sort_column_t *);
int zfs_sort_only_by_createtxg(const zfs_sort_column_t *);

#ifdef __cplusplus
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/zfs/zfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3654,11 +3654,14 @@ found3:;
argv += optind;

/*
* If we are only going to list snapshot names and sort by name,
* then we can use faster version.
* If we are only going to list snapshot names and sort by name or
* by createtxg, then we can use faster version.
*/
if (strcmp(fields, "name") == 0 && zfs_sort_only_by_name(sortcol))
if (strcmp(fields, "name") == 0 &&
(zfs_sort_only_by_name(sortcol) ||
zfs_sort_only_by_createtxg(sortcol))) {
flags |= ZFS_ITER_SIMPLE;
}

/*
* If "-o space" and no types were specified, don't display snapshots.
Expand Down
14 changes: 14 additions & 0 deletions lib/libzfs/libzfs_dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
zhp->zfs_head_type = pzhp->zfs_type;
zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
zhp->zpool_hdl = zpool_handle(zhp);
zhp->zfs_dmustats = zc->zc_objset_stats;

return (zhp);
}
Expand Down Expand Up @@ -2283,6 +2284,19 @@ get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
*val = zhp->zfs_dmustats.dds_redacted;
break;

case ZFS_PROP_CREATETXG:
/*
* We can directly read createtxg property from zfs
* handle for Filesystem, Snapshot and ZVOL types.
*/
if ((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) ||
(zhp->zfs_type == ZFS_TYPE_SNAPSHOT) ||
(zhp->zfs_type == ZFS_TYPE_VOLUME)) {
*val = zhp->zfs_dmustats.dds_creation_txg;
break;
}
zfs_fallthrough;

default:
switch (zfs_prop_get_type(prop)) {
case PROP_TYPE_NUMBER:
Expand Down
2 changes: 2 additions & 0 deletions module/zfs/zfs_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,8 @@ zfs_ioc_snapshot_list_next(zfs_cmd_t *zc)
}

if (zc->zc_simple) {
zc->zc_objset_stats.dds_creation_txg =
dsl_get_creationtxg(ds);
dsl_dataset_rele(ds, FTAG);
break;
}
Expand Down

0 comments on commit 3a1ce49

Please sign in to comment.