Skip to content

Commit

Permalink
expose snapshot count via stat(2) of .zfs/snapshot (openzfs#13559)
Browse files Browse the repository at this point in the history
Increase nlinks in stat results of ./zfs/snapshot based on snapshot
count. This provides quick and efficient method for administrators to
get snapshot counts without having to use libzfs or list the snapdir
contents.

Reviewed-by: Ryan Moeller <ryan@iXsystems.com>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Andrew Walker <awalker@ixsystems.com>
Closes openzfs#13559
  • Loading branch information
anodos325 authored and andrewc12 committed Sep 23, 2022
1 parent 83674c7 commit b8a6ab0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions module/os/linux/zfs/zpl_ctldir.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <sys/zfs_vnops.h>
#include <sys/zfs_ctldir.h>
#include <sys/zpl.h>
#include <sys/dmu.h>
#include <sys/dsl_dataset.h>
#include <sys/zap.h>

/*
* Common open routine. Disallow any write access.
Expand Down Expand Up @@ -411,6 +414,20 @@ zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
#endif

stat->nlink = stat->size = 2;

dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
uint64_t snap_count;
int err = zap_count(
dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
if (err != 0) {
ZPL_EXIT(zfsvfs);
return (-err);
}
stat->nlink += snap_count;
}

stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
stat->atime = current_time(ip);
ZPL_EXIT(zfsvfs);
Expand Down

0 comments on commit b8a6ab0

Please sign in to comment.