diff --git a/lib/libbe/be.c b/lib/libbe/be.c index 48bb36c2beea..4e889abee2e4 100644 --- a/lib/libbe/be.c +++ b/lib/libbe/be.c @@ -171,6 +171,15 @@ libbe_close(libbe_handle_t *lbh) free(lbh); } +/* + * Proxy through to libzfs for the moment. + */ +void +be_nicenum(uint64_t num, char *buf, size_t buflen) +{ + + zfs_nicenum(num, buf, buflen); +} /* * Destroy the boot environment or snapshot specified by the name diff --git a/lib/libbe/be.h b/lib/libbe/be.h index 988baaf16b08..e220626e6af2 100644 --- a/lib/libbe/be.h +++ b/lib/libbe/be.h @@ -67,7 +67,7 @@ const char *be_nextboot_path(libbe_handle_t *); const char *be_root_path(libbe_handle_t *); int be_get_bootenv_props(libbe_handle_t *, nvlist_t *); -int be_get_snapshot_props(libbe_handle_t *, const char *, nvlist_t *); +int be_get_dataset_props(libbe_handle_t *, const char *, nvlist_t *); int be_prop_list_alloc(nvlist_t **be_list); void be_prop_list_free(nvlist_t *be_list); @@ -116,5 +116,6 @@ int be_export(libbe_handle_t *, char *, int fd); int be_import(libbe_handle_t *, char *, int fd); int be_add_child(libbe_handle_t *, char *, bool); +void be_nicenum(uint64_t num, char *buf, size_t buflen); #endif /* _LIBBE_H */ diff --git a/lib/libbe/be_info.c b/lib/libbe/be_info.c index c4059ea5d598..49f64151d034 100644 --- a/lib/libbe/be_info.c +++ b/lib/libbe/be_info.c @@ -99,7 +99,7 @@ be_get_bootenv_props(libbe_handle_t *lbh, nvlist_t *dsnvl) } int -be_get_snapshot_props(libbe_handle_t *lbh, const char *name, nvlist_t *props) +be_get_dataset_props(libbe_handle_t *lbh, const char *name, nvlist_t *props) { zfs_handle_t *snap_hdl; prop_data_t data; @@ -109,7 +109,7 @@ be_get_snapshot_props(libbe_handle_t *lbh, const char *name, nvlist_t *props) data.list = props; data.single_object = true; if ((snap_hdl = zfs_open(lbh->lzh, name, - ZFS_TYPE_SNAPSHOT)) == NULL) + ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) return (BE_ERR_ZFSOPEN); ret = prop_list_builder_cb(snap_hdl, &data); diff --git a/lib/libbe/libbe.3 b/lib/libbe/libbe.3 index 365a1e85dc66..cdd4ab7ae653 100644 --- a/lib/libbe/libbe.3 +++ b/lib/libbe/libbe.3 @@ -88,6 +88,9 @@ of state to be retained, such as errors from previous operations. .Ft int .Fn be_destroy "libbe_handle_t *, char *, int" ; .Pp +.Ft void +.Fn be_nicenum "uint64_t, char *, size_t" ; +.Pp .\" TODO: Write up of mount options .\" typedef enum { .\" BE_MNT_FORCE = 1 << 0, @@ -139,7 +142,7 @@ of state to be retained, such as errors from previous operations. .Fn be_get_bootenv_props "libbe_handle_t *, nvlist_t *" ; .Pp .Ft int -.Fn be_get_snapshot_props "libbe_handle_t *, const char *, nvlist_t *" ; +.Fn be_get_dataset_props "libbe_handle_t *, const char *, nvlist_t *" ; .Pp .Ft void .Fn be_prop_list_free "nvlist_t *" ; diff --git a/sbin/bectl/bectl.c b/sbin/bectl/bectl.c index 44bc8a0bcf0f..f726f74596df 100644 --- a/sbin/bectl/bectl.c +++ b/sbin/bectl/bectl.c @@ -445,7 +445,7 @@ get_origin_props(nvlist_t *dsprops, nvlist_t **originprops) "bectl list: failed to allocate origin prop nvlist\n"); return (NULL); } - if (be_get_snapshot_props(be, propstr, *originprops) != 0) { + if (be_get_dataset_props(be, propstr, *originprops) != 0) { /* XXX TODO: Real errors */ fprintf(stderr, "bectl list: failed to fetch origin properties\n"); @@ -471,6 +471,40 @@ print_padding(const char *fval, int colsz, struct printc *pc) printf("%*s ", colsz, ""); } +static unsigned long long +dataset_space(const char *oname) +{ + unsigned long long space; + char *dsname, *propstr, *sep; + nvlist_t *dsprops; + + space = 0; + dsname = strdup(oname); + if (dsname == NULL) + return (0); + + if ((sep = strchr(dsname, '@')) != NULL) + *sep = '\0'; + + if (be_prop_list_alloc(&dsprops) != 0) { + free(dsname); + return (0); + } + + if (be_get_dataset_props(be, dsname, dsprops) != 0) { + nvlist_free(dsprops); + free(dsname); + return (0); + } + + if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) + space = strtoull(propstr, NULL, 10); + + nvlist_free(dsprops); + free(dsname); + return (space); +} + static void print_info(const char *name, nvlist_t *dsprops, struct printc *pc) { @@ -529,7 +563,7 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc) print_padding("-", pc->mount_colsz, pc); } - get_origin_props(dsprops, &originprops); + oname = get_origin_props(dsprops, &originprops); if (nvlist_lookup_string(dsprops, "used", &propstr) == 0) { space = strtoull(propstr, NULL, 10); @@ -538,9 +572,12 @@ print_info(const char *name, nvlist_t *dsprops, struct printc *pc) nvlist_lookup_string(originprops, "used", &propstr) == 0) space += strtoull(propstr, NULL, 10); + if (!pc->show_all_datasets && pc->show_space && oname != NULL) + /* Get the used space of the origin's dataset, too. */ + space += dataset_space(oname); + /* Alas, there's more to it,. */ - humanize_number(buf, 6, space, "", HN_AUTOSCALE, - HN_DECIMAL | HN_NOSPACE | HN_B); + be_nicenum(space, buf, 6); printf("%s", buf); print_padding(buf, pc->space_colsz, pc); } else {