1fa9e4066Sahrens /*
2fa9e4066Sahrens * CDDL HEADER START
3fa9e4066Sahrens *
4fa9e4066Sahrens * The contents of this file are subject to the terms of the
599653d4eSeschrock * Common Development and Distribution License (the "License").
699653d4eSeschrock * You may not use this file except in compliance with the License.
7fa9e4066Sahrens *
8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens * See the License for the specific language governing permissions
11fa9e4066Sahrens * and limitations under the License.
12fa9e4066Sahrens *
13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens *
19fa9e4066Sahrens * CDDL HEADER END
20fa9e4066Sahrens */
2143d68d68SYuri Pankov
22fa9e4066Sahrens /*
23a8b6ddafSMark J Musante * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2443d68d68SYuri Pankov * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25*78f17100SMatthew Ahrens * Copyright (c) 2013 by Delphix. All rights reserved.
26fa9e4066Sahrens */
27fa9e4066Sahrens
28fa9e4066Sahrens #include <libintl.h>
29fa9e4066Sahrens #include <libuutil.h>
30fa9e4066Sahrens #include <stddef.h>
31fa9e4066Sahrens #include <stdio.h>
32fa9e4066Sahrens #include <stdlib.h>
33fa9e4066Sahrens #include <strings.h>
34fa9e4066Sahrens
35fa9e4066Sahrens #include <libzfs.h>
36fa9e4066Sahrens
37fa9e4066Sahrens #include "zfs_util.h"
38b6825278Ssjelinek #include "zfs_iter.h"
39fa9e4066Sahrens
40fa9e4066Sahrens /*
41fa9e4066Sahrens * This is a private interface used to gather up all the datasets specified on
42fa9e4066Sahrens * the command line so that we can iterate over them in order.
43fa9e4066Sahrens *
44fa9e4066Sahrens * First, we iterate over all filesystems, gathering them together into an
45b6825278Ssjelinek * AVL tree. We report errors for any explicitly specified datasets
46fa9e4066Sahrens * that we couldn't open.
47fa9e4066Sahrens *
48fa9e4066Sahrens * When finished, we have an AVL tree of ZFS handles. We go through and execute
49fa9e4066Sahrens * the provided callback for each one, passing whatever data the user supplied.
50fa9e4066Sahrens */
51fa9e4066Sahrens
52fa9e4066Sahrens typedef struct zfs_node {
53fa9e4066Sahrens zfs_handle_t *zn_handle;
54fa9e4066Sahrens uu_avl_node_t zn_avlnode;
55fa9e4066Sahrens } zfs_node_t;
56fa9e4066Sahrens
57fa9e4066Sahrens typedef struct callback_data {
58fa9e4066Sahrens uu_avl_t *cb_avl;
59d5b5bb25SRich Morris int cb_flags;
60fa9e4066Sahrens zfs_type_t cb_types;
61b6825278Ssjelinek zfs_sort_column_t *cb_sortcol;
62990b4856Slling zprop_list_t **cb_proplist;
63ae1726b6SChris Gerhard int cb_depth_limit;
64ae1726b6SChris Gerhard int cb_depth;
652e5e9e19SSanjeev Bagewadi uint8_t cb_props_table[ZFS_NUM_PROPS];
66fa9e4066Sahrens } callback_data_t;
67fa9e4066Sahrens
68fa9e4066Sahrens uu_avl_pool_t *avl_pool;
69fa9e4066Sahrens
70fa9e4066Sahrens /*
71d5b5bb25SRich Morris * Include snaps if they were requested or if this a zfs list where types
72d5b5bb25SRich Morris * were not specified and the "listsnapshots" property is set on this pool.
73d5b5bb25SRich Morris */
74*78f17100SMatthew Ahrens static boolean_t
zfs_include_snapshots(zfs_handle_t * zhp,callback_data_t * cb)75d5b5bb25SRich Morris zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb)
76d5b5bb25SRich Morris {
77d5b5bb25SRich Morris zpool_handle_t *zph;
78d5b5bb25SRich Morris
79d5b5bb25SRich Morris if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0)
80d5b5bb25SRich Morris return (cb->cb_types & ZFS_TYPE_SNAPSHOT);
81d5b5bb25SRich Morris
82d5b5bb25SRich Morris zph = zfs_get_pool_handle(zhp);
83d5b5bb25SRich Morris return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL));
84d5b5bb25SRich Morris }
85d5b5bb25SRich Morris
86d5b5bb25SRich Morris /*
87d5b5bb25SRich Morris * Called for each dataset. If the object is of an appropriate type,
88fa9e4066Sahrens * add it to the avl tree and recurse over any children as necessary.
89fa9e4066Sahrens */
903cb34c60Sahrens static int
zfs_callback(zfs_handle_t * zhp,void * data)91fa9e4066Sahrens zfs_callback(zfs_handle_t *zhp, void *data)
92fa9e4066Sahrens {
93fa9e4066Sahrens callback_data_t *cb = data;
94*78f17100SMatthew Ahrens boolean_t dontclose = B_FALSE;
95*78f17100SMatthew Ahrens boolean_t include_snaps = zfs_include_snapshots(zhp, cb);
96*78f17100SMatthew Ahrens boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK);
97fa9e4066Sahrens
98d5b5bb25SRich Morris if ((zfs_get_type(zhp) & cb->cb_types) ||
99d5b5bb25SRich Morris ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
100fa9e4066Sahrens uu_avl_index_t idx;
101fa9e4066Sahrens zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
102fa9e4066Sahrens
103fa9e4066Sahrens node->zn_handle = zhp;
104fa9e4066Sahrens uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
105b6825278Ssjelinek if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
106b6825278Ssjelinek &idx) == NULL) {
1072e5e9e19SSanjeev Bagewadi if (cb->cb_proplist) {
1082e5e9e19SSanjeev Bagewadi if ((*cb->cb_proplist) &&
1092e5e9e19SSanjeev Bagewadi !(*cb->cb_proplist)->pl_all)
1102e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zhp,
1112e5e9e19SSanjeev Bagewadi cb->cb_props_table);
1122e5e9e19SSanjeev Bagewadi
11392241e0bSTom Erickson if (zfs_expand_proplist(zhp, cb->cb_proplist,
11443d68d68SYuri Pankov (cb->cb_flags & ZFS_ITER_RECVD_PROPS),
11543d68d68SYuri Pankov (cb->cb_flags & ZFS_ITER_LITERAL_PROPS))
1162e5e9e19SSanjeev Bagewadi != 0) {
117e9dbad6fSeschrock free(node);
118e9dbad6fSeschrock return (-1);
119e9dbad6fSeschrock }
1202e5e9e19SSanjeev Bagewadi }
121fa9e4066Sahrens uu_avl_insert(cb->cb_avl, node, idx);
122*78f17100SMatthew Ahrens dontclose = B_TRUE;
123fa9e4066Sahrens } else {
124fa9e4066Sahrens free(node);
125fa9e4066Sahrens }
126fa9e4066Sahrens }
127fa9e4066Sahrens
128fa9e4066Sahrens /*
1297f7322feSeschrock * Recurse if necessary.
130fa9e4066Sahrens */
131ae1726b6SChris Gerhard if (cb->cb_flags & ZFS_ITER_RECURSE &&
132ae1726b6SChris Gerhard ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
133ae1726b6SChris Gerhard cb->cb_depth < cb->cb_depth_limit)) {
134ae1726b6SChris Gerhard cb->cb_depth++;
1353cb34c60Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
1363cb34c60Sahrens (void) zfs_iter_filesystems(zhp, zfs_callback, data);
137*78f17100SMatthew Ahrens if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
138*78f17100SMatthew Ahrens ZFS_TYPE_BOOKMARK)) == 0) && include_snaps)
1393cb34c60Sahrens (void) zfs_iter_snapshots(zhp, zfs_callback, data);
140*78f17100SMatthew Ahrens if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
141*78f17100SMatthew Ahrens ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks)
142*78f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, zfs_callback, data);
143ae1726b6SChris Gerhard cb->cb_depth--;
1443cb34c60Sahrens }
145fa9e4066Sahrens
146fa9e4066Sahrens if (!dontclose)
147fa9e4066Sahrens zfs_close(zhp);
148fa9e4066Sahrens
149fa9e4066Sahrens return (0);
150fa9e4066Sahrens }
151fa9e4066Sahrens
152e9dbad6fSeschrock int
zfs_add_sort_column(zfs_sort_column_t ** sc,const char * name,boolean_t reverse)153e9dbad6fSeschrock zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
154b6825278Ssjelinek boolean_t reverse)
155b6825278Ssjelinek {
156b6825278Ssjelinek zfs_sort_column_t *col;
157e9dbad6fSeschrock zfs_prop_t prop;
158e9dbad6fSeschrock
159990b4856Slling if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL &&
160e9dbad6fSeschrock !zfs_prop_user(name))
161e9dbad6fSeschrock return (-1);
162b6825278Ssjelinek
163b6825278Ssjelinek col = safe_malloc(sizeof (zfs_sort_column_t));
164b6825278Ssjelinek
165b6825278Ssjelinek col->sc_prop = prop;
166b6825278Ssjelinek col->sc_reverse = reverse;
167990b4856Slling if (prop == ZPROP_INVAL) {
168e9dbad6fSeschrock col->sc_user_prop = safe_malloc(strlen(name) + 1);
169e9dbad6fSeschrock (void) strcpy(col->sc_user_prop, name);
170e9dbad6fSeschrock }
171b6825278Ssjelinek
172b6825278Ssjelinek if (*sc == NULL) {
173b6825278Ssjelinek col->sc_last = col;
174b6825278Ssjelinek *sc = col;
175b6825278Ssjelinek } else {
176b6825278Ssjelinek (*sc)->sc_last->sc_next = col;
177b6825278Ssjelinek (*sc)->sc_last = col;
178b6825278Ssjelinek }
179e9dbad6fSeschrock
180e9dbad6fSeschrock return (0);
181b6825278Ssjelinek }
182b6825278Ssjelinek
183b6825278Ssjelinek void
zfs_free_sort_columns(zfs_sort_column_t * sc)184b6825278Ssjelinek zfs_free_sort_columns(zfs_sort_column_t *sc)
185b6825278Ssjelinek {
186b6825278Ssjelinek zfs_sort_column_t *col;
187b6825278Ssjelinek
188b6825278Ssjelinek while (sc != NULL) {
189b6825278Ssjelinek col = sc->sc_next;
190e9dbad6fSeschrock free(sc->sc_user_prop);
191b6825278Ssjelinek free(sc);
192b6825278Ssjelinek sc = col;
193b6825278Ssjelinek }
194b6825278Ssjelinek }
195b6825278Ssjelinek
196fa9e4066Sahrens /* ARGSUSED */
197fa9e4066Sahrens static int
zfs_compare(const void * larg,const void * rarg,void * unused)198fa9e4066Sahrens zfs_compare(const void *larg, const void *rarg, void *unused)
199fa9e4066Sahrens {
200fa9e4066Sahrens zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
201fa9e4066Sahrens zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
202fa9e4066Sahrens const char *lname = zfs_get_name(l);
203fa9e4066Sahrens const char *rname = zfs_get_name(r);
204fa9e4066Sahrens char *lat, *rat;
205fa9e4066Sahrens uint64_t lcreate, rcreate;
206fa9e4066Sahrens int ret;
207fa9e4066Sahrens
208fa9e4066Sahrens lat = (char *)strchr(lname, '@');
209fa9e4066Sahrens rat = (char *)strchr(rname, '@');
210fa9e4066Sahrens
211fa9e4066Sahrens if (lat != NULL)
212fa9e4066Sahrens *lat = '\0';
213fa9e4066Sahrens if (rat != NULL)
214fa9e4066Sahrens *rat = '\0';
215fa9e4066Sahrens
216fa9e4066Sahrens ret = strcmp(lname, rname);
217fa9e4066Sahrens if (ret == 0) {
218fa9e4066Sahrens /*
219fa9e4066Sahrens * If we're comparing a dataset to one of its snapshots, we
220fa9e4066Sahrens * always make the full dataset first.
221fa9e4066Sahrens */
222fa9e4066Sahrens if (lat == NULL) {
223fa9e4066Sahrens ret = -1;
224fa9e4066Sahrens } else if (rat == NULL) {
225fa9e4066Sahrens ret = 1;
226fa9e4066Sahrens } else {
227fa9e4066Sahrens /*
228fa9e4066Sahrens * If we have two snapshots from the same dataset, then
229fa9e4066Sahrens * we want to sort them according to creation time. We
230fa9e4066Sahrens * use the hidden CREATETXG property to get an absolute
231fa9e4066Sahrens * ordering of snapshots.
232fa9e4066Sahrens */
233fa9e4066Sahrens lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
234fa9e4066Sahrens rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
235fa9e4066Sahrens
236fa9e4066Sahrens if (lcreate < rcreate)
237fa9e4066Sahrens ret = -1;
238fa9e4066Sahrens else if (lcreate > rcreate)
239fa9e4066Sahrens ret = 1;
240fa9e4066Sahrens }
241fa9e4066Sahrens }
242fa9e4066Sahrens
243fa9e4066Sahrens if (lat != NULL)
244fa9e4066Sahrens *lat = '@';
245fa9e4066Sahrens if (rat != NULL)
246fa9e4066Sahrens *rat = '@';
247fa9e4066Sahrens
248fa9e4066Sahrens return (ret);
249fa9e4066Sahrens }
250fa9e4066Sahrens
251b6825278Ssjelinek /*
252b6825278Ssjelinek * Sort datasets by specified columns.
253b6825278Ssjelinek *
254b6825278Ssjelinek * o Numeric types sort in ascending order.
255b6825278Ssjelinek * o String types sort in alphabetical order.
256b6825278Ssjelinek * o Types inappropriate for a row sort that row to the literal
257b6825278Ssjelinek * bottom, regardless of the specified ordering.
258b6825278Ssjelinek *
259b6825278Ssjelinek * If no sort columns are specified, or two datasets compare equally
260b6825278Ssjelinek * across all specified columns, they are sorted alphabetically by name
261b6825278Ssjelinek * with snapshots grouped under their parents.
262b6825278Ssjelinek */
263b6825278Ssjelinek static int
zfs_sort(const void * larg,const void * rarg,void * data)264b6825278Ssjelinek zfs_sort(const void *larg, const void *rarg, void *data)
265b6825278Ssjelinek {
266b6825278Ssjelinek zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
267b6825278Ssjelinek zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
268b6825278Ssjelinek zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
269b6825278Ssjelinek zfs_sort_column_t *psc;
270b6825278Ssjelinek
271b6825278Ssjelinek for (psc = sc; psc != NULL; psc = psc->sc_next) {
272e9dbad6fSeschrock char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
273e9dbad6fSeschrock char *lstr, *rstr;
274b6825278Ssjelinek uint64_t lnum, rnum;
275e9dbad6fSeschrock boolean_t lvalid, rvalid;
276b6825278Ssjelinek int ret = 0;
277b6825278Ssjelinek
278e9dbad6fSeschrock /*
279e9dbad6fSeschrock * We group the checks below the generic code. If 'lstr' and
280e9dbad6fSeschrock * 'rstr' are non-NULL, then we do a string based comparison.
281e9dbad6fSeschrock * Otherwise, we compare 'lnum' and 'rnum'.
282e9dbad6fSeschrock */
283e9dbad6fSeschrock lstr = rstr = NULL;
284990b4856Slling if (psc->sc_prop == ZPROP_INVAL) {
285e9dbad6fSeschrock nvlist_t *luser, *ruser;
286e9dbad6fSeschrock nvlist_t *lval, *rval;
287b6825278Ssjelinek
288e9dbad6fSeschrock luser = zfs_get_user_props(l);
289e9dbad6fSeschrock ruser = zfs_get_user_props(r);
290b6825278Ssjelinek
291e9dbad6fSeschrock lvalid = (nvlist_lookup_nvlist(luser,
292e9dbad6fSeschrock psc->sc_user_prop, &lval) == 0);
293e9dbad6fSeschrock rvalid = (nvlist_lookup_nvlist(ruser,
294e9dbad6fSeschrock psc->sc_user_prop, &rval) == 0);
295e9dbad6fSeschrock
296e9dbad6fSeschrock if (lvalid)
297e9dbad6fSeschrock verify(nvlist_lookup_string(lval,
298990b4856Slling ZPROP_VALUE, &lstr) == 0);
299e9dbad6fSeschrock if (rvalid)
300e9dbad6fSeschrock verify(nvlist_lookup_string(rval,
301990b4856Slling ZPROP_VALUE, &rstr) == 0);
302e9dbad6fSeschrock
303e9dbad6fSeschrock } else if (zfs_prop_is_string(psc->sc_prop)) {
304e9dbad6fSeschrock lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
305e9dbad6fSeschrock sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
306e9dbad6fSeschrock rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
307e9dbad6fSeschrock sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
308e9dbad6fSeschrock
309e9dbad6fSeschrock lstr = lbuf;
310e9dbad6fSeschrock rstr = rbuf;
311b6825278Ssjelinek } else {
312b6825278Ssjelinek lvalid = zfs_prop_valid_for_type(psc->sc_prop,
313b6825278Ssjelinek zfs_get_type(l));
314b6825278Ssjelinek rvalid = zfs_prop_valid_for_type(psc->sc_prop,
315b6825278Ssjelinek zfs_get_type(r));
316b6825278Ssjelinek
317e9dbad6fSeschrock if (lvalid)
318e9dbad6fSeschrock (void) zfs_prop_get_numeric(l, psc->sc_prop,
319e9dbad6fSeschrock &lnum, NULL, NULL, 0);
320e9dbad6fSeschrock if (rvalid)
321e9dbad6fSeschrock (void) zfs_prop_get_numeric(r, psc->sc_prop,
322e9dbad6fSeschrock &rnum, NULL, NULL, 0);
323e9dbad6fSeschrock }
324e9dbad6fSeschrock
325b6825278Ssjelinek if (!lvalid && !rvalid)
326b6825278Ssjelinek continue;
327b6825278Ssjelinek else if (!lvalid)
328b6825278Ssjelinek return (1);
329b6825278Ssjelinek else if (!rvalid)
330b6825278Ssjelinek return (-1);
331b6825278Ssjelinek
332e9dbad6fSeschrock if (lstr)
333e9dbad6fSeschrock ret = strcmp(lstr, rstr);
334b8a9e29cSrm160521 else if (lnum < rnum)
335b6825278Ssjelinek ret = -1;
336b6825278Ssjelinek else if (lnum > rnum)
337b6825278Ssjelinek ret = 1;
338b6825278Ssjelinek
339b6825278Ssjelinek if (ret != 0) {
340b6825278Ssjelinek if (psc->sc_reverse == B_TRUE)
341b6825278Ssjelinek ret = (ret < 0) ? 1 : -1;
342b6825278Ssjelinek return (ret);
343b6825278Ssjelinek }
344b6825278Ssjelinek }
345b6825278Ssjelinek
346b6825278Ssjelinek return (zfs_compare(larg, rarg, NULL));
347b6825278Ssjelinek }
348b6825278Ssjelinek
349fa9e4066Sahrens int
zfs_for_each(int argc,char ** argv,int flags,zfs_type_t types,zfs_sort_column_t * sortcol,zprop_list_t ** proplist,int limit,zfs_iter_f callback,void * data)350d5b5bb25SRich Morris zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
351ae1726b6SChris Gerhard zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
352d5b5bb25SRich Morris zfs_iter_f callback, void *data)
353fa9e4066Sahrens {
3542e5e9e19SSanjeev Bagewadi callback_data_t cb = {0};
355fa9e4066Sahrens int ret = 0;
356fa9e4066Sahrens zfs_node_t *node;
357fa9e4066Sahrens uu_avl_walk_t *walk;
358fa9e4066Sahrens
359fa9e4066Sahrens avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
360b6825278Ssjelinek offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
361fa9e4066Sahrens
362a8b6ddafSMark J Musante if (avl_pool == NULL)
363a8b6ddafSMark J Musante nomem();
364fa9e4066Sahrens
365b6825278Ssjelinek cb.cb_sortcol = sortcol;
366d5b5bb25SRich Morris cb.cb_flags = flags;
367e9dbad6fSeschrock cb.cb_proplist = proplist;
368fa9e4066Sahrens cb.cb_types = types;
369ae1726b6SChris Gerhard cb.cb_depth_limit = limit;
3702e5e9e19SSanjeev Bagewadi /*
3712e5e9e19SSanjeev Bagewadi * If cb_proplist is provided then in the zfs_handles created we
3722e5e9e19SSanjeev Bagewadi * retain only those properties listed in cb_proplist and sortcol.
3732e5e9e19SSanjeev Bagewadi * The rest are pruned. So, the caller should make sure that no other
3742e5e9e19SSanjeev Bagewadi * properties other than those listed in cb_proplist/sortcol are
3752e5e9e19SSanjeev Bagewadi * accessed.
3762e5e9e19SSanjeev Bagewadi *
37714843421SMatthew Ahrens * If cb_proplist is NULL then we retain all the properties. We
37814843421SMatthew Ahrens * always retain the zoned property, which some other properties
37914843421SMatthew Ahrens * need (userquota & friends), and the createtxg property, which
38014843421SMatthew Ahrens * we need to sort snapshots.
3812e5e9e19SSanjeev Bagewadi */
3822e5e9e19SSanjeev Bagewadi if (cb.cb_proplist && *cb.cb_proplist) {
3832e5e9e19SSanjeev Bagewadi zprop_list_t *p = *cb.cb_proplist;
3842e5e9e19SSanjeev Bagewadi
3852e5e9e19SSanjeev Bagewadi while (p) {
3862e5e9e19SSanjeev Bagewadi if (p->pl_prop >= ZFS_PROP_TYPE &&
3872e5e9e19SSanjeev Bagewadi p->pl_prop < ZFS_NUM_PROPS) {
3882e5e9e19SSanjeev Bagewadi cb.cb_props_table[p->pl_prop] = B_TRUE;
3892e5e9e19SSanjeev Bagewadi }
3902e5e9e19SSanjeev Bagewadi p = p->pl_next;
3912e5e9e19SSanjeev Bagewadi }
3922e5e9e19SSanjeev Bagewadi
3932e5e9e19SSanjeev Bagewadi while (sortcol) {
3942e5e9e19SSanjeev Bagewadi if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
3952e5e9e19SSanjeev Bagewadi sortcol->sc_prop < ZFS_NUM_PROPS) {
3962e5e9e19SSanjeev Bagewadi cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
3972e5e9e19SSanjeev Bagewadi }
3982e5e9e19SSanjeev Bagewadi sortcol = sortcol->sc_next;
3992e5e9e19SSanjeev Bagewadi }
40014843421SMatthew Ahrens
40114843421SMatthew Ahrens cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
40214843421SMatthew Ahrens cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
4032e5e9e19SSanjeev Bagewadi } else {
4042e5e9e19SSanjeev Bagewadi (void) memset(cb.cb_props_table, B_TRUE,
4052e5e9e19SSanjeev Bagewadi sizeof (cb.cb_props_table));
4062e5e9e19SSanjeev Bagewadi }
4072e5e9e19SSanjeev Bagewadi
408a8b6ddafSMark J Musante if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
409a8b6ddafSMark J Musante nomem();
410fa9e4066Sahrens
411fa9e4066Sahrens if (argc == 0) {
412fa9e4066Sahrens /*
413fa9e4066Sahrens * If given no arguments, iterate over all datasets.
414fa9e4066Sahrens */
415d5b5bb25SRich Morris cb.cb_flags |= ZFS_ITER_RECURSE;
41699653d4eSeschrock ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
417fa9e4066Sahrens } else {
418fa9e4066Sahrens int i;
419fa9e4066Sahrens zfs_handle_t *zhp;
420fa9e4066Sahrens zfs_type_t argtype;
421fa9e4066Sahrens
422fa9e4066Sahrens /*
423fa9e4066Sahrens * If we're recursive, then we always allow filesystems as
424fa9e4066Sahrens * arguments. If we also are interested in snapshots, then we
425fa9e4066Sahrens * can take volumes as well.
426fa9e4066Sahrens */
427fa9e4066Sahrens argtype = types;
428d5b5bb25SRich Morris if (flags & ZFS_ITER_RECURSE) {
429fa9e4066Sahrens argtype |= ZFS_TYPE_FILESYSTEM;
430fa9e4066Sahrens if (types & ZFS_TYPE_SNAPSHOT)
431fa9e4066Sahrens argtype |= ZFS_TYPE_VOLUME;
432fa9e4066Sahrens }
433fa9e4066Sahrens
434fa9e4066Sahrens for (i = 0; i < argc; i++) {
435d5b5bb25SRich Morris if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
4365aba80dbSck153898 zhp = zfs_path_to_zhandle(g_zfs, argv[i],
4375aba80dbSck153898 argtype);
4385aba80dbSck153898 } else {
4395aba80dbSck153898 zhp = zfs_open(g_zfs, argv[i], argtype);
4405aba80dbSck153898 }
4415aba80dbSck153898 if (zhp != NULL)
44299653d4eSeschrock ret |= zfs_callback(zhp, &cb);
443fa9e4066Sahrens else
444fa9e4066Sahrens ret = 1;
445fa9e4066Sahrens }
446fa9e4066Sahrens }
447fa9e4066Sahrens
448fa9e4066Sahrens /*
449fa9e4066Sahrens * At this point we've got our AVL tree full of zfs handles, so iterate
450fa9e4066Sahrens * over each one and execute the real user callback.
451fa9e4066Sahrens */
452fa9e4066Sahrens for (node = uu_avl_first(cb.cb_avl); node != NULL;
453fa9e4066Sahrens node = uu_avl_next(cb.cb_avl, node))
454fa9e4066Sahrens ret |= callback(node->zn_handle, data);
455fa9e4066Sahrens
456fa9e4066Sahrens /*
457fa9e4066Sahrens * Finally, clean up the AVL tree.
458fa9e4066Sahrens */
459a8b6ddafSMark J Musante if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
460a8b6ddafSMark J Musante nomem();
461fa9e4066Sahrens
462fa9e4066Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) {
463fa9e4066Sahrens uu_avl_remove(cb.cb_avl, node);
464fa9e4066Sahrens zfs_close(node->zn_handle);
465fa9e4066Sahrens free(node);
466fa9e4066Sahrens }
467fa9e4066Sahrens
468fa9e4066Sahrens uu_avl_walk_end(walk);
469fa9e4066Sahrens uu_avl_destroy(cb.cb_avl);
470fa9e4066Sahrens uu_avl_pool_destroy(avl_pool);
471fa9e4066Sahrens
472fa9e4066Sahrens return (ret);
473fa9e4066Sahrens }
474