Merge OpenSolaris revision 9365:7838a22eccd6:

PSARC/2009/171 zfs list -d and zfs get -d
6762432 zfs list --depth

PR:		bin/144720
Submitted by:	mm
Approved by:	pjd
Obtained from:	OpenSolaris
MFC after:	1 month
This commit is contained in:
Xin LI 2010-03-16 00:49:37 +00:00
parent e500694751
commit 10db393f9f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=205199
3 changed files with 64 additions and 29 deletions

View File

@ -53,12 +53,14 @@ typedef struct zfs_node {
} zfs_node_t;
typedef struct callback_data {
uu_avl_t *cb_avl;
int cb_flags;
zfs_type_t cb_types;
zfs_sort_column_t *cb_sortcol;
zprop_list_t **cb_proplist;
uint8_t cb_props_table[ZFS_NUM_PROPS];
uu_avl_t *cb_avl;
int cb_flags;
zfs_type_t cb_types;
zfs_sort_column_t *cb_sortcol;
zprop_list_t **cb_proplist;
int cb_depth_limit;
int cb_depth;
uint8_t cb_props_table[ZFS_NUM_PROPS];
} callback_data_t;
uu_avl_pool_t *avl_pool;
@ -99,7 +101,6 @@ zfs_callback(zfs_handle_t *zhp, void *data)
uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
&idx) == NULL) {
if (cb->cb_proplist) {
if ((*cb->cb_proplist) &&
!(*cb->cb_proplist)->pl_all)
@ -112,7 +113,6 @@ zfs_callback(zfs_handle_t *zhp, void *data)
return (-1);
}
}
uu_avl_insert(cb->cb_avl, node, idx);
dontclose = 1;
} else {
@ -123,11 +123,15 @@ zfs_callback(zfs_handle_t *zhp, void *data)
/*
* Recurse if necessary.
*/
if (cb->cb_flags & ZFS_ITER_RECURSE) {
if (cb->cb_flags & ZFS_ITER_RECURSE &&
((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
cb->cb_depth < cb->cb_depth_limit)) {
cb->cb_depth++;
if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
(void) zfs_iter_filesystems(zhp, zfs_callback, data);
if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps)
(void) zfs_iter_snapshots(zhp, zfs_callback, data);
cb->cb_depth--;
}
if (!dontclose)
@ -335,7 +339,7 @@ zfs_sort(const void *larg, const void *rarg, void *data)
int
zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
zfs_sort_column_t *sortcol, zprop_list_t **proplist,
zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
zfs_iter_f callback, void *data)
{
callback_data_t cb = {0};
@ -356,7 +360,7 @@ zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
cb.cb_flags = flags;
cb.cb_proplist = proplist;
cb.cb_types = types;
cb.cb_depth_limit = limit;
/*
* If cb_proplist is provided then in the zfs_handles created we
* retain only those properties listed in cb_proplist and sortcol.

View File

@ -19,7 +19,7 @@
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
@ -41,9 +41,10 @@ typedef struct zfs_sort_column {
#define ZFS_ITER_RECURSE (1 << 0)
#define ZFS_ITER_ARGS_CAN_BE_PATHS (1 << 1)
#define ZFS_ITER_PROP_LISTSNAPS (1 << 2)
#define ZFS_ITER_DEPTH_LIMIT (1 << 3)
int zfs_for_each(int, char **, int options, zfs_type_t,
zfs_sort_column_t *, zprop_list_t **, zfs_iter_f, void *);
zfs_sort_column_t *, zprop_list_t **, int, zfs_iter_f, void *);
int zfs_add_sort_column(zfs_sort_column_t **, const char *, boolean_t);
void zfs_free_sort_columns(zfs_sort_column_t *);

View File

@ -190,8 +190,8 @@ get_usage(zfs_help_t idx)
return (gettext("\tdestroy [-rRf] "
"<filesystem|volume|snapshot>\n"));
case HELP_GET:
return (gettext("\tget [-rHp] [-o field[,...]] "
"[-s source[,...]]\n"
return (gettext("\tget [-rHp] [-d max] "
"[-o field[,...]] [-s source[,...]]\n"
"\t <\"all\" | property[,...]> "
"[filesystem|volume|snapshot] ...\n"));
case HELP_INHERIT:
@ -205,8 +205,8 @@ get_usage(zfs_help_t idx)
case HELP_UNJAIL:
return (gettext("\tunjail <jailid> <filesystem>\n"));
case HELP_LIST:
return (gettext("\tlist [-rH] [-o property[,...]] "
"[-t type[,...]] [-s property] ...\n"
return (gettext("\tlist [-rH][-d max] "
"[-o property[,...]] [-t type[,...]] [-s property] ...\n"
"\t [-S property] ... "
"[filesystem|volume|snapshot] ...\n"));
case HELP_MOUNT:
@ -432,6 +432,27 @@ parseprop(nvlist_t *props)
}
static int
parse_depth(char *opt, int *flags)
{
char *tmp;
int depth;
depth = (int)strtol(opt, &tmp, 0);
if (*tmp) {
(void) fprintf(stderr,
gettext("%s is not an integer\n"), optarg);
usage(B_FALSE);
}
if (depth < 0) {
(void) fprintf(stderr,
gettext("Depth can not be negative.\n"));
usage(B_FALSE);
}
*flags |= (ZFS_ITER_DEPTH_LIMIT|ZFS_ITER_RECURSE);
return (depth);
}
/*
* zfs clone [-p] [-o prop=value] ... <snap> <fs | vol>
*
@ -1119,6 +1140,7 @@ zfs_do_get(int argc, char **argv)
int i, c, flags = 0;
char *value, *fields;
int ret;
int limit = 0;
zprop_list_t fake_name = { 0 };
/*
@ -1132,11 +1154,14 @@ zfs_do_get(int argc, char **argv)
cb.cb_type = ZFS_TYPE_DATASET;
/* check options */
while ((c = getopt(argc, argv, ":o:s:rHp")) != -1) {
while ((c = getopt(argc, argv, ":d:o:s:rHp")) != -1) {
switch (c) {
case 'p':
cb.cb_literal = B_TRUE;
break;
case 'd':
limit = parse_depth(optarg, &flags);
break;
case 'r':
flags |= ZFS_ITER_RECURSE;
break;
@ -1267,7 +1292,7 @@ zfs_do_get(int argc, char **argv)
/* run for each object */
ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET, NULL,
&cb.cb_proplist, get_callback, &cb);
&cb.cb_proplist, limit, get_callback, &cb);
if (cb.cb_proplist == &fake_name)
zprop_free_list(fake_name.pl_next);
@ -1380,10 +1405,10 @@ zfs_do_inherit(int argc, char **argv)
if (flags & ZFS_ITER_RECURSE) {
ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
NULL, NULL, inherit_recurse_cb, propname);
NULL, NULL, 0, inherit_recurse_cb, propname);
} else {
ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_DATASET,
NULL, NULL, inherit_cb, propname);
NULL, NULL, 0, inherit_cb, propname);
}
return (ret);
@ -1578,7 +1603,7 @@ zfs_do_upgrade(int argc, char **argv)
if (cb.cb_version == 0)
cb.cb_version = ZPL_VERSION;
ret = zfs_for_each(argc, argv, flags, ZFS_TYPE_FILESYSTEM,
NULL, NULL, upgrade_set_callback, &cb);
NULL, NULL, 0, upgrade_set_callback, &cb);
(void) printf(gettext("%llu filesystems upgraded\n"),
cb.cb_numupgraded);
if (cb.cb_numsamegraded) {
@ -1596,14 +1621,14 @@ zfs_do_upgrade(int argc, char **argv)
flags |= ZFS_ITER_RECURSE;
ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
NULL, NULL, upgrade_list_callback, &cb);
NULL, NULL, 0, upgrade_list_callback, &cb);
found = cb.cb_foundone;
cb.cb_foundone = B_FALSE;
cb.cb_newer = B_TRUE;
ret = zfs_for_each(0, NULL, flags, ZFS_TYPE_FILESYSTEM,
NULL, NULL, upgrade_list_callback, &cb);
NULL, NULL, 0, upgrade_list_callback, &cb);
if (!cb.cb_foundone && !found) {
(void) printf(gettext("All filesystems are "
@ -1615,11 +1640,12 @@ zfs_do_upgrade(int argc, char **argv)
}
/*
* list [-rH] [-o property[,property]...] [-t type[,type]...]
* list [-r][-d max] [-H] [-o property[,property]...] [-t type[,type]...]
* [-s property [-s property]...] [-S property [-S property]...]
* <dataset> ...
*
* -r Recurse over all children
* -d Limit recursion by depth.
* -H Scripted mode; elide headers and separate columns by tabs
* -o Control which fields to display.
* -t Control which object types to display.
@ -1769,16 +1795,20 @@ zfs_do_list(int argc, char **argv)
char *fields = NULL;
list_cbdata_t cb = { 0 };
char *value;
int limit = 0;
int ret;
zfs_sort_column_t *sortcol = NULL;
int flags = ZFS_ITER_PROP_LISTSNAPS | ZFS_ITER_ARGS_CAN_BE_PATHS;
/* check options */
while ((c = getopt(argc, argv, ":o:rt:Hs:S:")) != -1) {
while ((c = getopt(argc, argv, ":d:o:rt:Hs:S:")) != -1) {
switch (c) {
case 'o':
fields = optarg;
break;
case 'd':
limit = parse_depth(optarg, &flags);
break;
case 'r':
flags |= ZFS_ITER_RECURSE;
break;
@ -1869,7 +1899,7 @@ zfs_do_list(int argc, char **argv)
cb.cb_first = B_TRUE;
ret = zfs_for_each(argc, argv, flags, types, sortcol, &cb.cb_proplist,
list_callback, &cb);
limit, list_callback, &cb);
zprop_free_list(cb.cb_proplist);
zfs_free_sort_columns(sortcol);
@ -2252,7 +2282,7 @@ zfs_do_set(int argc, char **argv)
}
ret = zfs_for_each(argc - 2, argv + 2, NULL,
ZFS_TYPE_DATASET, NULL, NULL, set_callback, &cb);
ZFS_TYPE_DATASET, NULL, NULL, 0, set_callback, &cb);
return (ret);
}
@ -2886,7 +2916,7 @@ zfs_do_unallow(int argc, char **argv)
flags |= ZFS_ITER_RECURSE;
error = zfs_for_each(argc, argv, flags,
ZFS_TYPE_FILESYSTEM|ZFS_TYPE_VOLUME, NULL,
NULL, unallow_callback, (void *)zperms);
NULL, 0, unallow_callback, (void *)zperms);
if (zperms)
nvlist_free(zperms);