xref: /titanic_41/usr/src/cmd/zfs/zfs_iter.c (revision ae1726b6ae69a6882b0d520f4bc3433f501f2065)
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  */
21fa9e4066Sahrens /*
222e5e9e19SSanjeev Bagewadi  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #include <libintl.h>
27fa9e4066Sahrens #include <libuutil.h>
28fa9e4066Sahrens #include <stddef.h>
29fa9e4066Sahrens #include <stdio.h>
30fa9e4066Sahrens #include <stdlib.h>
31fa9e4066Sahrens #include <strings.h>
32fa9e4066Sahrens 
33fa9e4066Sahrens #include <libzfs.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens #include "zfs_util.h"
36b6825278Ssjelinek #include "zfs_iter.h"
37fa9e4066Sahrens 
38fa9e4066Sahrens /*
39fa9e4066Sahrens  * This is a private interface used to gather up all the datasets specified on
40fa9e4066Sahrens  * the command line so that we can iterate over them in order.
41fa9e4066Sahrens  *
42fa9e4066Sahrens  * First, we iterate over all filesystems, gathering them together into an
43b6825278Ssjelinek  * AVL tree.  We report errors for any explicitly specified datasets
44fa9e4066Sahrens  * that we couldn't open.
45fa9e4066Sahrens  *
46fa9e4066Sahrens  * When finished, we have an AVL tree of ZFS handles.  We go through and execute
47fa9e4066Sahrens  * the provided callback for each one, passing whatever data the user supplied.
48fa9e4066Sahrens  */
49fa9e4066Sahrens 
50fa9e4066Sahrens typedef struct zfs_node {
51fa9e4066Sahrens 	zfs_handle_t	*zn_handle;
52fa9e4066Sahrens 	uu_avl_node_t	zn_avlnode;
53fa9e4066Sahrens } zfs_node_t;
54fa9e4066Sahrens 
55fa9e4066Sahrens typedef struct callback_data {
56fa9e4066Sahrens 	uu_avl_t		*cb_avl;
57d5b5bb25SRich Morris 	int			cb_flags;
58fa9e4066Sahrens 	zfs_type_t		cb_types;
59b6825278Ssjelinek 	zfs_sort_column_t	*cb_sortcol;
60990b4856Slling 	zprop_list_t		**cb_proplist;
61*ae1726b6SChris Gerhard 	int			cb_depth_limit;
62*ae1726b6SChris Gerhard 	int			cb_depth;
632e5e9e19SSanjeev Bagewadi 	uint8_t			cb_props_table[ZFS_NUM_PROPS];
64fa9e4066Sahrens } callback_data_t;
65fa9e4066Sahrens 
66fa9e4066Sahrens uu_avl_pool_t *avl_pool;
67fa9e4066Sahrens 
68fa9e4066Sahrens /*
69d5b5bb25SRich Morris  * Include snaps if they were requested or if this a zfs list where types
70d5b5bb25SRich Morris  * were not specified and the "listsnapshots" property is set on this pool.
71d5b5bb25SRich Morris  */
72d5b5bb25SRich Morris static int
73d5b5bb25SRich Morris zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb)
74d5b5bb25SRich Morris {
75d5b5bb25SRich Morris 	zpool_handle_t *zph;
76d5b5bb25SRich Morris 
77d5b5bb25SRich Morris 	if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0)
78d5b5bb25SRich Morris 		return (cb->cb_types & ZFS_TYPE_SNAPSHOT);
79d5b5bb25SRich Morris 
80d5b5bb25SRich Morris 	zph = zfs_get_pool_handle(zhp);
81d5b5bb25SRich Morris 	return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL));
82d5b5bb25SRich Morris }
83d5b5bb25SRich Morris 
84d5b5bb25SRich Morris /*
85d5b5bb25SRich Morris  * Called for each dataset.  If the object is of an appropriate type,
86fa9e4066Sahrens  * add it to the avl tree and recurse over any children as necessary.
87fa9e4066Sahrens  */
883cb34c60Sahrens static int
89fa9e4066Sahrens zfs_callback(zfs_handle_t *zhp, void *data)
90fa9e4066Sahrens {
91fa9e4066Sahrens 	callback_data_t *cb = data;
92fa9e4066Sahrens 	int dontclose = 0;
93d5b5bb25SRich Morris 	int include_snaps = zfs_include_snapshots(zhp, cb);
94fa9e4066Sahrens 
95d5b5bb25SRich Morris 	if ((zfs_get_type(zhp) & cb->cb_types) ||
96d5b5bb25SRich Morris 	    ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
97fa9e4066Sahrens 		uu_avl_index_t idx;
98fa9e4066Sahrens 		zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
99fa9e4066Sahrens 
100fa9e4066Sahrens 		node->zn_handle = zhp;
101fa9e4066Sahrens 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
102b6825278Ssjelinek 		if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
103b6825278Ssjelinek 		    &idx) == NULL) {
1042e5e9e19SSanjeev Bagewadi 			if (cb->cb_proplist) {
1052e5e9e19SSanjeev Bagewadi 				if ((*cb->cb_proplist) &&
1062e5e9e19SSanjeev Bagewadi 				    !(*cb->cb_proplist)->pl_all)
1072e5e9e19SSanjeev Bagewadi 					zfs_prune_proplist(zhp,
1082e5e9e19SSanjeev Bagewadi 					    cb->cb_props_table);
1092e5e9e19SSanjeev Bagewadi 
1102e5e9e19SSanjeev Bagewadi 				if (zfs_expand_proplist(zhp, cb->cb_proplist)
1112e5e9e19SSanjeev Bagewadi 				    != 0) {
112e9dbad6fSeschrock 					free(node);
113e9dbad6fSeschrock 					return (-1);
114e9dbad6fSeschrock 				}
1152e5e9e19SSanjeev Bagewadi 			}
116fa9e4066Sahrens 			uu_avl_insert(cb->cb_avl, node, idx);
117fa9e4066Sahrens 			dontclose = 1;
118fa9e4066Sahrens 		} else {
119fa9e4066Sahrens 			free(node);
120fa9e4066Sahrens 		}
121fa9e4066Sahrens 	}
122fa9e4066Sahrens 
123fa9e4066Sahrens 	/*
1247f7322feSeschrock 	 * Recurse if necessary.
125fa9e4066Sahrens 	 */
126*ae1726b6SChris Gerhard 	if (cb->cb_flags & ZFS_ITER_RECURSE &&
127*ae1726b6SChris Gerhard 	    ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
128*ae1726b6SChris Gerhard 	    cb->cb_depth < cb->cb_depth_limit)) {
129*ae1726b6SChris Gerhard 		cb->cb_depth++;
1303cb34c60Sahrens 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
1313cb34c60Sahrens 			(void) zfs_iter_filesystems(zhp, zfs_callback, data);
132d5b5bb25SRich Morris 		if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps)
1333cb34c60Sahrens 			(void) zfs_iter_snapshots(zhp, zfs_callback, data);
134*ae1726b6SChris Gerhard 		cb->cb_depth--;
1353cb34c60Sahrens 	}
136fa9e4066Sahrens 
137fa9e4066Sahrens 	if (!dontclose)
138fa9e4066Sahrens 		zfs_close(zhp);
139fa9e4066Sahrens 
140fa9e4066Sahrens 	return (0);
141fa9e4066Sahrens }
142fa9e4066Sahrens 
143e9dbad6fSeschrock int
144e9dbad6fSeschrock zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
145b6825278Ssjelinek     boolean_t reverse)
146b6825278Ssjelinek {
147b6825278Ssjelinek 	zfs_sort_column_t *col;
148e9dbad6fSeschrock 	zfs_prop_t prop;
149e9dbad6fSeschrock 
150990b4856Slling 	if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL &&
151e9dbad6fSeschrock 	    !zfs_prop_user(name))
152e9dbad6fSeschrock 		return (-1);
153b6825278Ssjelinek 
154b6825278Ssjelinek 	col = safe_malloc(sizeof (zfs_sort_column_t));
155b6825278Ssjelinek 
156b6825278Ssjelinek 	col->sc_prop = prop;
157b6825278Ssjelinek 	col->sc_reverse = reverse;
158990b4856Slling 	if (prop == ZPROP_INVAL) {
159e9dbad6fSeschrock 		col->sc_user_prop = safe_malloc(strlen(name) + 1);
160e9dbad6fSeschrock 		(void) strcpy(col->sc_user_prop, name);
161e9dbad6fSeschrock 	}
162b6825278Ssjelinek 
163b6825278Ssjelinek 	if (*sc == NULL) {
164b6825278Ssjelinek 		col->sc_last = col;
165b6825278Ssjelinek 		*sc = col;
166b6825278Ssjelinek 	} else {
167b6825278Ssjelinek 		(*sc)->sc_last->sc_next = col;
168b6825278Ssjelinek 		(*sc)->sc_last = col;
169b6825278Ssjelinek 	}
170e9dbad6fSeschrock 
171e9dbad6fSeschrock 	return (0);
172b6825278Ssjelinek }
173b6825278Ssjelinek 
174b6825278Ssjelinek void
175b6825278Ssjelinek zfs_free_sort_columns(zfs_sort_column_t *sc)
176b6825278Ssjelinek {
177b6825278Ssjelinek 	zfs_sort_column_t *col;
178b6825278Ssjelinek 
179b6825278Ssjelinek 	while (sc != NULL) {
180b6825278Ssjelinek 		col = sc->sc_next;
181e9dbad6fSeschrock 		free(sc->sc_user_prop);
182b6825278Ssjelinek 		free(sc);
183b6825278Ssjelinek 		sc = col;
184b6825278Ssjelinek 	}
185b6825278Ssjelinek }
186b6825278Ssjelinek 
187fa9e4066Sahrens /* ARGSUSED */
188fa9e4066Sahrens static int
189fa9e4066Sahrens zfs_compare(const void *larg, const void *rarg, void *unused)
190fa9e4066Sahrens {
191fa9e4066Sahrens 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
192fa9e4066Sahrens 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
193fa9e4066Sahrens 	const char *lname = zfs_get_name(l);
194fa9e4066Sahrens 	const char *rname = zfs_get_name(r);
195fa9e4066Sahrens 	char *lat, *rat;
196fa9e4066Sahrens 	uint64_t lcreate, rcreate;
197fa9e4066Sahrens 	int ret;
198fa9e4066Sahrens 
199fa9e4066Sahrens 	lat = (char *)strchr(lname, '@');
200fa9e4066Sahrens 	rat = (char *)strchr(rname, '@');
201fa9e4066Sahrens 
202fa9e4066Sahrens 	if (lat != NULL)
203fa9e4066Sahrens 		*lat = '\0';
204fa9e4066Sahrens 	if (rat != NULL)
205fa9e4066Sahrens 		*rat = '\0';
206fa9e4066Sahrens 
207fa9e4066Sahrens 	ret = strcmp(lname, rname);
208fa9e4066Sahrens 	if (ret == 0) {
209fa9e4066Sahrens 		/*
210fa9e4066Sahrens 		 * If we're comparing a dataset to one of its snapshots, we
211fa9e4066Sahrens 		 * always make the full dataset first.
212fa9e4066Sahrens 		 */
213fa9e4066Sahrens 		if (lat == NULL) {
214fa9e4066Sahrens 			ret = -1;
215fa9e4066Sahrens 		} else if (rat == NULL) {
216fa9e4066Sahrens 			ret = 1;
217fa9e4066Sahrens 		} else {
218fa9e4066Sahrens 			/*
219fa9e4066Sahrens 			 * If we have two snapshots from the same dataset, then
220fa9e4066Sahrens 			 * we want to sort them according to creation time.  We
221fa9e4066Sahrens 			 * use the hidden CREATETXG property to get an absolute
222fa9e4066Sahrens 			 * ordering of snapshots.
223fa9e4066Sahrens 			 */
224fa9e4066Sahrens 			lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
225fa9e4066Sahrens 			rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
226fa9e4066Sahrens 
227fa9e4066Sahrens 			if (lcreate < rcreate)
228fa9e4066Sahrens 				ret = -1;
229fa9e4066Sahrens 			else if (lcreate > rcreate)
230fa9e4066Sahrens 				ret = 1;
231fa9e4066Sahrens 		}
232fa9e4066Sahrens 	}
233fa9e4066Sahrens 
234fa9e4066Sahrens 	if (lat != NULL)
235fa9e4066Sahrens 		*lat = '@';
236fa9e4066Sahrens 	if (rat != NULL)
237fa9e4066Sahrens 		*rat = '@';
238fa9e4066Sahrens 
239fa9e4066Sahrens 	return (ret);
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242b6825278Ssjelinek /*
243b6825278Ssjelinek  * Sort datasets by specified columns.
244b6825278Ssjelinek  *
245b6825278Ssjelinek  * o  Numeric types sort in ascending order.
246b6825278Ssjelinek  * o  String types sort in alphabetical order.
247b6825278Ssjelinek  * o  Types inappropriate for a row sort that row to the literal
248b6825278Ssjelinek  *    bottom, regardless of the specified ordering.
249b6825278Ssjelinek  *
250b6825278Ssjelinek  * If no sort columns are specified, or two datasets compare equally
251b6825278Ssjelinek  * across all specified columns, they are sorted alphabetically by name
252b6825278Ssjelinek  * with snapshots grouped under their parents.
253b6825278Ssjelinek  */
254b6825278Ssjelinek static int
255b6825278Ssjelinek zfs_sort(const void *larg, const void *rarg, void *data)
256b6825278Ssjelinek {
257b6825278Ssjelinek 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
258b6825278Ssjelinek 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
259b6825278Ssjelinek 	zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
260b6825278Ssjelinek 	zfs_sort_column_t *psc;
261b6825278Ssjelinek 
262b6825278Ssjelinek 	for (psc = sc; psc != NULL; psc = psc->sc_next) {
263e9dbad6fSeschrock 		char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
264e9dbad6fSeschrock 		char *lstr, *rstr;
265b6825278Ssjelinek 		uint64_t lnum, rnum;
266e9dbad6fSeschrock 		boolean_t lvalid, rvalid;
267b6825278Ssjelinek 		int ret = 0;
268b6825278Ssjelinek 
269e9dbad6fSeschrock 		/*
270e9dbad6fSeschrock 		 * We group the checks below the generic code.  If 'lstr' and
271e9dbad6fSeschrock 		 * 'rstr' are non-NULL, then we do a string based comparison.
272e9dbad6fSeschrock 		 * Otherwise, we compare 'lnum' and 'rnum'.
273e9dbad6fSeschrock 		 */
274e9dbad6fSeschrock 		lstr = rstr = NULL;
275990b4856Slling 		if (psc->sc_prop == ZPROP_INVAL) {
276e9dbad6fSeschrock 			nvlist_t *luser, *ruser;
277e9dbad6fSeschrock 			nvlist_t *lval, *rval;
278b6825278Ssjelinek 
279e9dbad6fSeschrock 			luser = zfs_get_user_props(l);
280e9dbad6fSeschrock 			ruser = zfs_get_user_props(r);
281b6825278Ssjelinek 
282e9dbad6fSeschrock 			lvalid = (nvlist_lookup_nvlist(luser,
283e9dbad6fSeschrock 			    psc->sc_user_prop, &lval) == 0);
284e9dbad6fSeschrock 			rvalid = (nvlist_lookup_nvlist(ruser,
285e9dbad6fSeschrock 			    psc->sc_user_prop, &rval) == 0);
286e9dbad6fSeschrock 
287e9dbad6fSeschrock 			if (lvalid)
288e9dbad6fSeschrock 				verify(nvlist_lookup_string(lval,
289990b4856Slling 				    ZPROP_VALUE, &lstr) == 0);
290e9dbad6fSeschrock 			if (rvalid)
291e9dbad6fSeschrock 				verify(nvlist_lookup_string(rval,
292990b4856Slling 				    ZPROP_VALUE, &rstr) == 0);
293e9dbad6fSeschrock 
294e9dbad6fSeschrock 		} else if (zfs_prop_is_string(psc->sc_prop)) {
295e9dbad6fSeschrock 			lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
296e9dbad6fSeschrock 			    sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
297e9dbad6fSeschrock 			rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
298e9dbad6fSeschrock 			    sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
299e9dbad6fSeschrock 
300e9dbad6fSeschrock 			lstr = lbuf;
301e9dbad6fSeschrock 			rstr = rbuf;
302b6825278Ssjelinek 		} else {
303b6825278Ssjelinek 			lvalid = zfs_prop_valid_for_type(psc->sc_prop,
304b6825278Ssjelinek 			    zfs_get_type(l));
305b6825278Ssjelinek 			rvalid = zfs_prop_valid_for_type(psc->sc_prop,
306b6825278Ssjelinek 			    zfs_get_type(r));
307b6825278Ssjelinek 
308e9dbad6fSeschrock 			if (lvalid)
309e9dbad6fSeschrock 				(void) zfs_prop_get_numeric(l, psc->sc_prop,
310e9dbad6fSeschrock 				    &lnum, NULL, NULL, 0);
311e9dbad6fSeschrock 			if (rvalid)
312e9dbad6fSeschrock 				(void) zfs_prop_get_numeric(r, psc->sc_prop,
313e9dbad6fSeschrock 				    &rnum, NULL, NULL, 0);
314e9dbad6fSeschrock 		}
315e9dbad6fSeschrock 
316b6825278Ssjelinek 		if (!lvalid && !rvalid)
317b6825278Ssjelinek 			continue;
318b6825278Ssjelinek 		else if (!lvalid)
319b6825278Ssjelinek 			return (1);
320b6825278Ssjelinek 		else if (!rvalid)
321b6825278Ssjelinek 			return (-1);
322b6825278Ssjelinek 
323e9dbad6fSeschrock 		if (lstr)
324e9dbad6fSeschrock 			ret = strcmp(lstr, rstr);
325b8a9e29cSrm160521 		else if (lnum < rnum)
326b6825278Ssjelinek 			ret = -1;
327b6825278Ssjelinek 		else if (lnum > rnum)
328b6825278Ssjelinek 			ret = 1;
329b6825278Ssjelinek 
330b6825278Ssjelinek 		if (ret != 0) {
331b6825278Ssjelinek 			if (psc->sc_reverse == B_TRUE)
332b6825278Ssjelinek 				ret = (ret < 0) ? 1 : -1;
333b6825278Ssjelinek 			return (ret);
334b6825278Ssjelinek 		}
335b6825278Ssjelinek 	}
336b6825278Ssjelinek 
337b6825278Ssjelinek 	return (zfs_compare(larg, rarg, NULL));
338b6825278Ssjelinek }
339b6825278Ssjelinek 
340fa9e4066Sahrens int
341d5b5bb25SRich Morris zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
342*ae1726b6SChris Gerhard     zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
343d5b5bb25SRich Morris     zfs_iter_f callback, void *data)
344fa9e4066Sahrens {
3452e5e9e19SSanjeev Bagewadi 	callback_data_t cb = {0};
346fa9e4066Sahrens 	int ret = 0;
347fa9e4066Sahrens 	zfs_node_t *node;
348fa9e4066Sahrens 	uu_avl_walk_t *walk;
349fa9e4066Sahrens 
350fa9e4066Sahrens 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
351b6825278Ssjelinek 	    offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
352fa9e4066Sahrens 
353fa9e4066Sahrens 	if (avl_pool == NULL) {
354fa9e4066Sahrens 		(void) fprintf(stderr,
355fa9e4066Sahrens 		    gettext("internal error: out of memory\n"));
356fa9e4066Sahrens 		exit(1);
357fa9e4066Sahrens 	}
358fa9e4066Sahrens 
359b6825278Ssjelinek 	cb.cb_sortcol = sortcol;
360d5b5bb25SRich Morris 	cb.cb_flags = flags;
361e9dbad6fSeschrock 	cb.cb_proplist = proplist;
362fa9e4066Sahrens 	cb.cb_types = types;
363*ae1726b6SChris Gerhard 	cb.cb_depth_limit = limit;
3642e5e9e19SSanjeev Bagewadi 	/*
3652e5e9e19SSanjeev Bagewadi 	 * If cb_proplist is provided then in the zfs_handles created  we
3662e5e9e19SSanjeev Bagewadi 	 * retain only those properties listed in cb_proplist and sortcol.
3672e5e9e19SSanjeev Bagewadi 	 * The rest are pruned. So, the caller should make sure that no other
3682e5e9e19SSanjeev Bagewadi 	 * properties other than those listed in cb_proplist/sortcol are
3692e5e9e19SSanjeev Bagewadi 	 * accessed.
3702e5e9e19SSanjeev Bagewadi 	 *
3712e5e9e19SSanjeev Bagewadi 	 * If cb_proplist is NULL then we retain all the properties.
3722e5e9e19SSanjeev Bagewadi 	 */
3732e5e9e19SSanjeev Bagewadi 	if (cb.cb_proplist && *cb.cb_proplist) {
3742e5e9e19SSanjeev Bagewadi 		zprop_list_t *p = *cb.cb_proplist;
3752e5e9e19SSanjeev Bagewadi 
3762e5e9e19SSanjeev Bagewadi 		while (p) {
3772e5e9e19SSanjeev Bagewadi 			if (p->pl_prop >= ZFS_PROP_TYPE &&
3782e5e9e19SSanjeev Bagewadi 			    p->pl_prop < ZFS_NUM_PROPS) {
3792e5e9e19SSanjeev Bagewadi 				cb.cb_props_table[p->pl_prop] = B_TRUE;
3802e5e9e19SSanjeev Bagewadi 			}
3812e5e9e19SSanjeev Bagewadi 			p = p->pl_next;
3822e5e9e19SSanjeev Bagewadi 		}
3832e5e9e19SSanjeev Bagewadi 
3842e5e9e19SSanjeev Bagewadi 		while (sortcol) {
3852e5e9e19SSanjeev Bagewadi 			if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
3862e5e9e19SSanjeev Bagewadi 			    sortcol->sc_prop < ZFS_NUM_PROPS) {
3872e5e9e19SSanjeev Bagewadi 				cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
3882e5e9e19SSanjeev Bagewadi 			}
3892e5e9e19SSanjeev Bagewadi 			sortcol = sortcol->sc_next;
3902e5e9e19SSanjeev Bagewadi 		}
3912e5e9e19SSanjeev Bagewadi 	} else {
3922e5e9e19SSanjeev Bagewadi 		(void) memset(cb.cb_props_table, B_TRUE,
3932e5e9e19SSanjeev Bagewadi 		    sizeof (cb.cb_props_table));
3942e5e9e19SSanjeev Bagewadi 	}
3952e5e9e19SSanjeev Bagewadi 
396fa9e4066Sahrens 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) {
397fa9e4066Sahrens 		(void) fprintf(stderr,
398fa9e4066Sahrens 		    gettext("internal error: out of memory\n"));
399fa9e4066Sahrens 		exit(1);
400fa9e4066Sahrens 	}
401fa9e4066Sahrens 
402fa9e4066Sahrens 	if (argc == 0) {
403fa9e4066Sahrens 		/*
404fa9e4066Sahrens 		 * If given no arguments, iterate over all datasets.
405fa9e4066Sahrens 		 */
406d5b5bb25SRich Morris 		cb.cb_flags |= ZFS_ITER_RECURSE;
40799653d4eSeschrock 		ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
408fa9e4066Sahrens 	} else {
409fa9e4066Sahrens 		int i;
410fa9e4066Sahrens 		zfs_handle_t *zhp;
411fa9e4066Sahrens 		zfs_type_t argtype;
412fa9e4066Sahrens 
413fa9e4066Sahrens 		/*
414fa9e4066Sahrens 		 * If we're recursive, then we always allow filesystems as
415fa9e4066Sahrens 		 * arguments.  If we also are interested in snapshots, then we
416fa9e4066Sahrens 		 * can take volumes as well.
417fa9e4066Sahrens 		 */
418fa9e4066Sahrens 		argtype = types;
419d5b5bb25SRich Morris 		if (flags & ZFS_ITER_RECURSE) {
420fa9e4066Sahrens 			argtype |= ZFS_TYPE_FILESYSTEM;
421fa9e4066Sahrens 			if (types & ZFS_TYPE_SNAPSHOT)
422fa9e4066Sahrens 				argtype |= ZFS_TYPE_VOLUME;
423fa9e4066Sahrens 		}
424fa9e4066Sahrens 
425fa9e4066Sahrens 		for (i = 0; i < argc; i++) {
426d5b5bb25SRich Morris 			if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
4275aba80dbSck153898 				zhp = zfs_path_to_zhandle(g_zfs, argv[i],
4285aba80dbSck153898 				    argtype);
4295aba80dbSck153898 			} else {
4305aba80dbSck153898 				zhp = zfs_open(g_zfs, argv[i], argtype);
4315aba80dbSck153898 			}
4325aba80dbSck153898 			if (zhp != NULL)
43399653d4eSeschrock 				ret |= zfs_callback(zhp, &cb);
434fa9e4066Sahrens 			else
435fa9e4066Sahrens 				ret = 1;
436fa9e4066Sahrens 		}
437fa9e4066Sahrens 	}
438fa9e4066Sahrens 
439fa9e4066Sahrens 	/*
440fa9e4066Sahrens 	 * At this point we've got our AVL tree full of zfs handles, so iterate
441fa9e4066Sahrens 	 * over each one and execute the real user callback.
442fa9e4066Sahrens 	 */
443fa9e4066Sahrens 	for (node = uu_avl_first(cb.cb_avl); node != NULL;
444fa9e4066Sahrens 	    node = uu_avl_next(cb.cb_avl, node))
445fa9e4066Sahrens 		ret |= callback(node->zn_handle, data);
446fa9e4066Sahrens 
447fa9e4066Sahrens 	/*
448fa9e4066Sahrens 	 * Finally, clean up the AVL tree.
449fa9e4066Sahrens 	 */
450fa9e4066Sahrens 	if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) {
451fa9e4066Sahrens 		(void) fprintf(stderr,
452fa9e4066Sahrens 		    gettext("internal error: out of memory"));
453fa9e4066Sahrens 		exit(1);
454fa9e4066Sahrens 	}
455fa9e4066Sahrens 
456fa9e4066Sahrens 	while ((node = uu_avl_walk_next(walk)) != NULL) {
457fa9e4066Sahrens 		uu_avl_remove(cb.cb_avl, node);
458fa9e4066Sahrens 		zfs_close(node->zn_handle);
459fa9e4066Sahrens 		free(node);
460fa9e4066Sahrens 	}
461fa9e4066Sahrens 
462fa9e4066Sahrens 	uu_avl_walk_end(walk);
463fa9e4066Sahrens 	uu_avl_destroy(cb.cb_avl);
464fa9e4066Sahrens 	uu_avl_pool_destroy(avl_pool);
465fa9e4066Sahrens 
466fa9e4066Sahrens 	return (ret);
467fa9e4066Sahrens }
468