xref: /illumos-gate/usr/src/cmd/zfs/zfs_iter.c (revision 4c2bdae2)
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.
240d8fa8f8SMartin Matuska  * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
2543d68d68SYuri Pankov  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
2678f17100SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
27fa9e4066Sahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <libintl.h>
30fa9e4066Sahrens #include <libuutil.h>
31fa9e4066Sahrens #include <stddef.h>
32fa9e4066Sahrens #include <stdio.h>
33fa9e4066Sahrens #include <stdlib.h>
34fa9e4066Sahrens #include <strings.h>
35fa9e4066Sahrens 
36fa9e4066Sahrens #include <libzfs.h>
37fa9e4066Sahrens 
38fa9e4066Sahrens #include "zfs_util.h"
39b6825278Ssjelinek #include "zfs_iter.h"
40fa9e4066Sahrens 
41fa9e4066Sahrens /*
42fa9e4066Sahrens  * This is a private interface used to gather up all the datasets specified on
43fa9e4066Sahrens  * the command line so that we can iterate over them in order.
44fa9e4066Sahrens  *
45fa9e4066Sahrens  * First, we iterate over all filesystems, gathering them together into an
46b6825278Ssjelinek  * AVL tree.  We report errors for any explicitly specified datasets
47fa9e4066Sahrens  * that we couldn't open.
48fa9e4066Sahrens  *
49fa9e4066Sahrens  * When finished, we have an AVL tree of ZFS handles.  We go through and execute
50fa9e4066Sahrens  * the provided callback for each one, passing whatever data the user supplied.
51fa9e4066Sahrens  */
52fa9e4066Sahrens 
53fa9e4066Sahrens typedef struct zfs_node {
54fa9e4066Sahrens 	zfs_handle_t	*zn_handle;
55fa9e4066Sahrens 	uu_avl_node_t	zn_avlnode;
56fa9e4066Sahrens } zfs_node_t;
57fa9e4066Sahrens 
58fa9e4066Sahrens typedef struct callback_data {
59ae1726b6SChris Gerhard 	uu_avl_t		*cb_avl;
60ae1726b6SChris Gerhard 	int			cb_flags;
61ae1726b6SChris Gerhard 	zfs_type_t		cb_types;
62ae1726b6SChris Gerhard 	zfs_sort_column_t	*cb_sortcol;
63ae1726b6SChris Gerhard 	zprop_list_t		**cb_proplist;
64ae1726b6SChris Gerhard 	int			cb_depth_limit;
65ae1726b6SChris Gerhard 	int			cb_depth;
66ae1726b6SChris Gerhard 	uint8_t			cb_props_table[ZFS_NUM_PROPS];
67fa9e4066Sahrens } callback_data_t;
68fa9e4066Sahrens 
69fa9e4066Sahrens uu_avl_pool_t *avl_pool;
70fa9e4066Sahrens 
71fa9e4066Sahrens /*
72d5b5bb25SRich Morris  * Include snaps if they were requested or if this a zfs list where types
73d5b5bb25SRich Morris  * were not specified and the "listsnapshots" property is set on this pool.
74d5b5bb25SRich Morris  */
7578f17100SMatthew Ahrens static boolean_t
zfs_include_snapshots(zfs_handle_t * zhp,callback_data_t * cb)76d5b5bb25SRich Morris zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb)
77d5b5bb25SRich Morris {
78d5b5bb25SRich Morris 	zpool_handle_t *zph;
79d5b5bb25SRich Morris 
80d5b5bb25SRich Morris 	if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0)
81d5b5bb25SRich Morris 		return (cb->cb_types & ZFS_TYPE_SNAPSHOT);
82d5b5bb25SRich Morris 
83d5b5bb25SRich Morris 	zph = zfs_get_pool_handle(zhp);
84d5b5bb25SRich Morris 	return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL));
85d5b5bb25SRich Morris }
86d5b5bb25SRich Morris 
87d5b5bb25SRich Morris /*
88d5b5bb25SRich Morris  * Called for each dataset.  If the object is of an appropriate type,
89fa9e4066Sahrens  * add it to the avl tree and recurse over any children as necessary.
90fa9e4066Sahrens  */
913cb34c60Sahrens static int
zfs_callback(zfs_handle_t * zhp,void * data)92fa9e4066Sahrens zfs_callback(zfs_handle_t *zhp, void *data)
93fa9e4066Sahrens {
94fa9e4066Sahrens 	callback_data_t *cb = data;
95d1896202SDavid Schwartz 	boolean_t should_close = B_TRUE;
9678f17100SMatthew Ahrens 	boolean_t include_snaps = zfs_include_snapshots(zhp, cb);
9778f17100SMatthew Ahrens 	boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK);
98fa9e4066Sahrens 
99d5b5bb25SRich Morris 	if ((zfs_get_type(zhp) & cb->cb_types) ||
100d5b5bb25SRich Morris 	    ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
101fa9e4066Sahrens 		uu_avl_index_t idx;
102fa9e4066Sahrens 		zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
103fa9e4066Sahrens 
104fa9e4066Sahrens 		node->zn_handle = zhp;
105fa9e4066Sahrens 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
106b6825278Ssjelinek 		if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
107b6825278Ssjelinek 		    &idx) == NULL) {
1082e5e9e19SSanjeev Bagewadi 			if (cb->cb_proplist) {
1092e5e9e19SSanjeev Bagewadi 				if ((*cb->cb_proplist) &&
1102e5e9e19SSanjeev Bagewadi 				    !(*cb->cb_proplist)->pl_all)
1112e5e9e19SSanjeev Bagewadi 					zfs_prune_proplist(zhp,
1122e5e9e19SSanjeev Bagewadi 					    cb->cb_props_table);
1132e5e9e19SSanjeev Bagewadi 
11492241e0bSTom Erickson 				if (zfs_expand_proplist(zhp, cb->cb_proplist,
11543d68d68SYuri Pankov 				    (cb->cb_flags & ZFS_ITER_RECVD_PROPS),
11643d68d68SYuri Pankov 				    (cb->cb_flags & ZFS_ITER_LITERAL_PROPS))
1172e5e9e19SSanjeev Bagewadi 				    != 0) {
1182e5e9e19SSanjeev Bagewadi 					free(node);
1192e5e9e19SSanjeev Bagewadi 					return (-1);
1202e5e9e19SSanjeev Bagewadi 				}
121e9dbad6fSeschrock 			}
122fa9e4066Sahrens 			uu_avl_insert(cb->cb_avl, node, idx);
123d1896202SDavid Schwartz 			should_close = B_FALSE;
124fa9e4066Sahrens 		} else {
125fa9e4066Sahrens 			free(node);
126fa9e4066Sahrens 		}
127fa9e4066Sahrens 	}
128fa9e4066Sahrens 
129fa9e4066Sahrens 	/*
1307f7322feSeschrock 	 * Recurse if necessary.
131fa9e4066Sahrens 	 */
132ae1726b6SChris Gerhard 	if (cb->cb_flags & ZFS_ITER_RECURSE &&
133ae1726b6SChris Gerhard 	    ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
134ae1726b6SChris Gerhard 	    cb->cb_depth < cb->cb_depth_limit)) {
135ae1726b6SChris Gerhard 		cb->cb_depth++;
1363cb34c60Sahrens 		if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM)
1373cb34c60Sahrens 			(void) zfs_iter_filesystems(zhp, zfs_callback, data);
13878f17100SMatthew Ahrens 		if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
13978f17100SMatthew Ahrens 		    ZFS_TYPE_BOOKMARK)) == 0) && include_snaps)
1400d8fa8f8SMartin Matuska 			(void) zfs_iter_snapshots(zhp,
1410d8fa8f8SMartin Matuska 			    (cb->cb_flags & ZFS_ITER_SIMPLE) != 0, zfs_callback,
1420d8fa8f8SMartin Matuska 			    data);
14378f17100SMatthew Ahrens 		if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
14478f17100SMatthew Ahrens 		    ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks)
14578f17100SMatthew Ahrens 			(void) zfs_iter_bookmarks(zhp, zfs_callback, data);
146ae1726b6SChris Gerhard 		cb->cb_depth--;
1473cb34c60Sahrens 	}
148fa9e4066Sahrens 
149d1896202SDavid Schwartz 	if (should_close)
150fa9e4066Sahrens 		zfs_close(zhp);
151fa9e4066Sahrens 
152fa9e4066Sahrens 	return (0);
153fa9e4066Sahrens }
154fa9e4066Sahrens 
155e9dbad6fSeschrock int
zfs_add_sort_column(zfs_sort_column_t ** sc,const char * name,boolean_t reverse)156e9dbad6fSeschrock zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
157b6825278Ssjelinek     boolean_t reverse)
158b6825278Ssjelinek {
159b6825278Ssjelinek 	zfs_sort_column_t *col;
160e9dbad6fSeschrock 	zfs_prop_t prop;
161e9dbad6fSeschrock 
162990b4856Slling 	if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL &&
163e9dbad6fSeschrock 	    !zfs_prop_user(name))
164e9dbad6fSeschrock 		return (-1);
165b6825278Ssjelinek 
166b6825278Ssjelinek 	col = safe_malloc(sizeof (zfs_sort_column_t));
167b6825278Ssjelinek 
168b6825278Ssjelinek 	col->sc_prop = prop;
169b6825278Ssjelinek 	col->sc_reverse = reverse;
170990b4856Slling 	if (prop == ZPROP_INVAL) {
171e9dbad6fSeschrock 		col->sc_user_prop = safe_malloc(strlen(name) + 1);
172e9dbad6fSeschrock 		(void) strcpy(col->sc_user_prop, name);
173e9dbad6fSeschrock 	}
174b6825278Ssjelinek 
175b6825278Ssjelinek 	if (*sc == NULL) {
176b6825278Ssjelinek 		col->sc_last = col;
177b6825278Ssjelinek 		*sc = col;
178b6825278Ssjelinek 	} else {
179b6825278Ssjelinek 		(*sc)->sc_last->sc_next = col;
180b6825278Ssjelinek 		(*sc)->sc_last = col;
181b6825278Ssjelinek 	}
182e9dbad6fSeschrock 
183e9dbad6fSeschrock 	return (0);
184b6825278Ssjelinek }
185b6825278Ssjelinek 
186b6825278Ssjelinek void
zfs_free_sort_columns(zfs_sort_column_t * sc)187b6825278Ssjelinek zfs_free_sort_columns(zfs_sort_column_t *sc)
188b6825278Ssjelinek {
189b6825278Ssjelinek 	zfs_sort_column_t *col;
190b6825278Ssjelinek 
191b6825278Ssjelinek 	while (sc != NULL) {
192b6825278Ssjelinek 		col = sc->sc_next;
193e9dbad6fSeschrock 		free(sc->sc_user_prop);
194b6825278Ssjelinek 		free(sc);
195b6825278Ssjelinek 		sc = col;
196b6825278Ssjelinek 	}
197b6825278Ssjelinek }
198b6825278Ssjelinek 
1990d8fa8f8SMartin Matuska boolean_t
zfs_sort_only_by_name(const zfs_sort_column_t * sc)2000d8fa8f8SMartin Matuska zfs_sort_only_by_name(const zfs_sort_column_t *sc)
2010d8fa8f8SMartin Matuska {
2020d8fa8f8SMartin Matuska 
2030d8fa8f8SMartin Matuska 	return (sc != NULL && sc->sc_next == NULL &&
2040d8fa8f8SMartin Matuska 	    sc->sc_prop == ZFS_PROP_NAME);
2050d8fa8f8SMartin Matuska }
2060d8fa8f8SMartin Matuska 
207fa9e4066Sahrens /* ARGSUSED */
208fa9e4066Sahrens static int
zfs_compare(const void * larg,const void * rarg,void * unused)209fa9e4066Sahrens zfs_compare(const void *larg, const void *rarg, void *unused)
210fa9e4066Sahrens {
211fa9e4066Sahrens 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
212fa9e4066Sahrens 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
213fa9e4066Sahrens 	const char *lname = zfs_get_name(l);
214fa9e4066Sahrens 	const char *rname = zfs_get_name(r);
215fa9e4066Sahrens 	char *lat, *rat;
216fa9e4066Sahrens 	uint64_t lcreate, rcreate;
217fa9e4066Sahrens 	int ret;
218fa9e4066Sahrens 
219fa9e4066Sahrens 	lat = (char *)strchr(lname, '@');
220fa9e4066Sahrens 	rat = (char *)strchr(rname, '@');
221fa9e4066Sahrens 
222fa9e4066Sahrens 	if (lat != NULL)
223fa9e4066Sahrens 		*lat = '\0';
224fa9e4066Sahrens 	if (rat != NULL)
225fa9e4066Sahrens 		*rat = '\0';
226fa9e4066Sahrens 
227fa9e4066Sahrens 	ret = strcmp(lname, rname);
228fa9e4066Sahrens 	if (ret == 0) {
229fa9e4066Sahrens 		/*
230fa9e4066Sahrens 		 * If we're comparing a dataset to one of its snapshots, we
231fa9e4066Sahrens 		 * always make the full dataset first.
232fa9e4066Sahrens 		 */
233fa9e4066Sahrens 		if (lat == NULL) {
234fa9e4066Sahrens 			ret = -1;
235fa9e4066Sahrens 		} else if (rat == NULL) {
236fa9e4066Sahrens 			ret = 1;
237fa9e4066Sahrens 		} else {
238fa9e4066Sahrens 			/*
239fa9e4066Sahrens 			 * If we have two snapshots from the same dataset, then
240fa9e4066Sahrens 			 * we want to sort them according to creation time.  We
241fa9e4066Sahrens 			 * use the hidden CREATETXG property to get an absolute
242fa9e4066Sahrens 			 * ordering of snapshots.
243fa9e4066Sahrens 			 */
244fa9e4066Sahrens 			lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
245fa9e4066Sahrens 			rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
246fa9e4066Sahrens 
2470d8fa8f8SMartin Matuska 			/*
2480d8fa8f8SMartin Matuska 			 * Both lcreate and rcreate being 0 means we don't have
2490d8fa8f8SMartin Matuska 			 * properties and we should compare full name.
2500d8fa8f8SMartin Matuska 			 */
2510d8fa8f8SMartin Matuska 			if (lcreate == 0 && rcreate == 0)
2520d8fa8f8SMartin Matuska 				ret = strcmp(lat + 1, rat + 1);
2530d8fa8f8SMartin Matuska 			else if (lcreate < rcreate)
254fa9e4066Sahrens 				ret = -1;
255fa9e4066Sahrens 			else if (lcreate > rcreate)
256fa9e4066Sahrens 				ret = 1;
257fa9e4066Sahrens 		}
258fa9e4066Sahrens 	}
259fa9e4066Sahrens 
260fa9e4066Sahrens 	if (lat != NULL)
261fa9e4066Sahrens 		*lat = '@';
262fa9e4066Sahrens 	if (rat != NULL)
263fa9e4066Sahrens 		*rat = '@';
264fa9e4066Sahrens 
265fa9e4066Sahrens 	return (ret);
266fa9e4066Sahrens }
267fa9e4066Sahrens 
268b6825278Ssjelinek /*
269b6825278Ssjelinek  * Sort datasets by specified columns.
270b6825278Ssjelinek  *
271b6825278Ssjelinek  * o  Numeric types sort in ascending order.
272b6825278Ssjelinek  * o  String types sort in alphabetical order.
273b6825278Ssjelinek  * o  Types inappropriate for a row sort that row to the literal
274b6825278Ssjelinek  *    bottom, regardless of the specified ordering.
275b6825278Ssjelinek  *
276b6825278Ssjelinek  * If no sort columns are specified, or two datasets compare equally
277b6825278Ssjelinek  * across all specified columns, they are sorted alphabetically by name
278b6825278Ssjelinek  * with snapshots grouped under their parents.
279b6825278Ssjelinek  */
280b6825278Ssjelinek static int
zfs_sort(const void * larg,const void * rarg,void * data)281b6825278Ssjelinek zfs_sort(const void *larg, const void *rarg, void *data)
282b6825278Ssjelinek {
283b6825278Ssjelinek 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
284b6825278Ssjelinek 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
285b6825278Ssjelinek 	zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
286b6825278Ssjelinek 	zfs_sort_column_t *psc;
287b6825278Ssjelinek 
288b6825278Ssjelinek 	for (psc = sc; psc != NULL; psc = psc->sc_next) {
289e9dbad6fSeschrock 		char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
290e9dbad6fSeschrock 		char *lstr, *rstr;
291b6825278Ssjelinek 		uint64_t lnum, rnum;
292e9dbad6fSeschrock 		boolean_t lvalid, rvalid;
293b6825278Ssjelinek 		int ret = 0;
294b6825278Ssjelinek 
295e9dbad6fSeschrock 		/*
296e9dbad6fSeschrock 		 * We group the checks below the generic code.  If 'lstr' and
297e9dbad6fSeschrock 		 * 'rstr' are non-NULL, then we do a string based comparison.
298e9dbad6fSeschrock 		 * Otherwise, we compare 'lnum' and 'rnum'.
299e9dbad6fSeschrock 		 */
300e9dbad6fSeschrock 		lstr = rstr = NULL;
301990b4856Slling 		if (psc->sc_prop == ZPROP_INVAL) {
302e9dbad6fSeschrock 			nvlist_t *luser, *ruser;
303e9dbad6fSeschrock 			nvlist_t *lval, *rval;
304e9dbad6fSeschrock 
305e9dbad6fSeschrock 			luser = zfs_get_user_props(l);
306e9dbad6fSeschrock 			ruser = zfs_get_user_props(r);
307e9dbad6fSeschrock 
308e9dbad6fSeschrock 			lvalid = (nvlist_lookup_nvlist(luser,
309e9dbad6fSeschrock 			    psc->sc_user_prop, &lval) == 0);
310e9dbad6fSeschrock 			rvalid = (nvlist_lookup_nvlist(ruser,
311e9dbad6fSeschrock 			    psc->sc_user_prop, &rval) == 0);
312e9dbad6fSeschrock 
313e9dbad6fSeschrock 			if (lvalid)
314e9dbad6fSeschrock 				verify(nvlist_lookup_string(lval,
315990b4856Slling 				    ZPROP_VALUE, &lstr) == 0);
316e9dbad6fSeschrock 			if (rvalid)
317e9dbad6fSeschrock 				verify(nvlist_lookup_string(rval,
318990b4856Slling 				    ZPROP_VALUE, &rstr) == 0);
3190d8fa8f8SMartin Matuska 		} else if (psc->sc_prop == ZFS_PROP_NAME) {
3200d8fa8f8SMartin Matuska 			lvalid = rvalid = B_TRUE;
3210d8fa8f8SMartin Matuska 
3220d8fa8f8SMartin Matuska 			(void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf));
3230d8fa8f8SMartin Matuska 			(void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf));
324e9dbad6fSeschrock 
3250d8fa8f8SMartin Matuska 			lstr = lbuf;
3260d8fa8f8SMartin Matuska 			rstr = rbuf;
327e9dbad6fSeschrock 		} else if (zfs_prop_is_string(psc->sc_prop)) {
328e9dbad6fSeschrock 			lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
329e9dbad6fSeschrock 			    sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
330e9dbad6fSeschrock 			rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
331e9dbad6fSeschrock 			    sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
332e9dbad6fSeschrock 
333e9dbad6fSeschrock 			lstr = lbuf;
334e9dbad6fSeschrock 			rstr = rbuf;
335b6825278Ssjelinek 		} else {
336b6825278Ssjelinek 			lvalid = zfs_prop_valid_for_type(psc->sc_prop,
337*4c2bdae2STim Chase 			    zfs_get_type(l), B_FALSE);
338b6825278Ssjelinek 			rvalid = zfs_prop_valid_for_type(psc->sc_prop,
339*4c2bdae2STim Chase 			    zfs_get_type(r), B_FALSE);
340b6825278Ssjelinek 
341e9dbad6fSeschrock 			if (lvalid)
342e9dbad6fSeschrock 				(void) zfs_prop_get_numeric(l, psc->sc_prop,
343e9dbad6fSeschrock 				    &lnum, NULL, NULL, 0);
344e9dbad6fSeschrock 			if (rvalid)
345e9dbad6fSeschrock 				(void) zfs_prop_get_numeric(r, psc->sc_prop,
346e9dbad6fSeschrock 				    &rnum, NULL, NULL, 0);
347e9dbad6fSeschrock 		}
348b6825278Ssjelinek 
349e9dbad6fSeschrock 		if (!lvalid && !rvalid)
350e9dbad6fSeschrock 			continue;
351e9dbad6fSeschrock 		else if (!lvalid)
352e9dbad6fSeschrock 			return (1);
353e9dbad6fSeschrock 		else if (!rvalid)
354e9dbad6fSeschrock 			return (-1);
355b6825278Ssjelinek 
356e9dbad6fSeschrock 		if (lstr)
357e9dbad6fSeschrock 			ret = strcmp(lstr, rstr);
358b8a9e29cSrm 		else if (lnum < rnum)
359e9dbad6fSeschrock 			ret = -1;
360e9dbad6fSeschrock 		else if (lnum > rnum)
361e9dbad6fSeschrock 			ret = 1;
362b6825278Ssjelinek 
363b6825278Ssjelinek 		if (ret != 0) {
364b6825278Ssjelinek 			if (psc->sc_reverse == B_TRUE)
365b6825278Ssjelinek 				ret = (ret < 0) ? 1 : -1;
366b6825278Ssjelinek 			return (ret);
367b6825278Ssjelinek 		}
368b6825278Ssjelinek 	}
369b6825278Ssjelinek 
370b6825278Ssjelinek 	return (zfs_compare(larg, rarg, NULL));
371b6825278Ssjelinek }
372b6825278Ssjelinek 
373fa9e4066Sahrens 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)374d5b5bb25SRich Morris zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
375ae1726b6SChris Gerhard     zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
376d5b5bb25SRich Morris     zfs_iter_f callback, void *data)
377fa9e4066Sahrens {
3782e5e9e19SSanjeev Bagewadi 	callback_data_t cb = {0};
379fa9e4066Sahrens 	int ret = 0;
380fa9e4066Sahrens 	zfs_node_t *node;
381fa9e4066Sahrens 	uu_avl_walk_t *walk;
382fa9e4066Sahrens 
383fa9e4066Sahrens 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
384b6825278Ssjelinek 	    offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
385fa9e4066Sahrens 
386a8b6ddafSMark J Musante 	if (avl_pool == NULL)
387a8b6ddafSMark J Musante 		nomem();
388fa9e4066Sahrens 
389b6825278Ssjelinek 	cb.cb_sortcol = sortcol;
390d5b5bb25SRich Morris 	cb.cb_flags = flags;
391e9dbad6fSeschrock 	cb.cb_proplist = proplist;
392fa9e4066Sahrens 	cb.cb_types = types;
393ae1726b6SChris Gerhard 	cb.cb_depth_limit = limit;
3942e5e9e19SSanjeev Bagewadi 	/*
395842727c2SChris Kirby 	 * If cb_proplist is provided then in the zfs_handles created we
3962e5e9e19SSanjeev Bagewadi 	 * retain only those properties listed in cb_proplist and sortcol.
3972e5e9e19SSanjeev Bagewadi 	 * The rest are pruned. So, the caller should make sure that no other
3982e5e9e19SSanjeev Bagewadi 	 * properties other than those listed in cb_proplist/sortcol are
3992e5e9e19SSanjeev Bagewadi 	 * accessed.
4002e5e9e19SSanjeev Bagewadi 	 *
40114843421SMatthew Ahrens 	 * If cb_proplist is NULL then we retain all the properties.  We
40214843421SMatthew Ahrens 	 * always retain the zoned property, which some other properties
40314843421SMatthew Ahrens 	 * need (userquota & friends), and the createtxg property, which
40414843421SMatthew Ahrens 	 * we need to sort snapshots.
4052e5e9e19SSanjeev Bagewadi 	 */
4062e5e9e19SSanjeev Bagewadi 	if (cb.cb_proplist && *cb.cb_proplist) {
4072e5e9e19SSanjeev Bagewadi 		zprop_list_t *p = *cb.cb_proplist;
4082e5e9e19SSanjeev Bagewadi 
4092e5e9e19SSanjeev Bagewadi 		while (p) {
4102e5e9e19SSanjeev Bagewadi 			if (p->pl_prop >= ZFS_PROP_TYPE &&
4112e5e9e19SSanjeev Bagewadi 			    p->pl_prop < ZFS_NUM_PROPS) {
4122e5e9e19SSanjeev Bagewadi 				cb.cb_props_table[p->pl_prop] = B_TRUE;
4132e5e9e19SSanjeev Bagewadi 			}
4142e5e9e19SSanjeev Bagewadi 			p = p->pl_next;
4152e5e9e19SSanjeev Bagewadi 		}
4162e5e9e19SSanjeev Bagewadi 
4172e5e9e19SSanjeev Bagewadi 		while (sortcol) {
4182e5e9e19SSanjeev Bagewadi 			if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
4192e5e9e19SSanjeev Bagewadi 			    sortcol->sc_prop < ZFS_NUM_PROPS) {
4202e5e9e19SSanjeev Bagewadi 				cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
4212e5e9e19SSanjeev Bagewadi 			}
4222e5e9e19SSanjeev Bagewadi 			sortcol = sortcol->sc_next;
4232e5e9e19SSanjeev Bagewadi 		}
42414843421SMatthew Ahrens 
42514843421SMatthew Ahrens 		cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
42614843421SMatthew Ahrens 		cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
4272e5e9e19SSanjeev Bagewadi 	} else {
4282e5e9e19SSanjeev Bagewadi 		(void) memset(cb.cb_props_table, B_TRUE,
4292e5e9e19SSanjeev Bagewadi 		    sizeof (cb.cb_props_table));
4302e5e9e19SSanjeev Bagewadi 	}
4312e5e9e19SSanjeev Bagewadi 
432a8b6ddafSMark J Musante 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
433a8b6ddafSMark J Musante 		nomem();
434fa9e4066Sahrens 
435fa9e4066Sahrens 	if (argc == 0) {
436fa9e4066Sahrens 		/*
437fa9e4066Sahrens 		 * If given no arguments, iterate over all datasets.
438fa9e4066Sahrens 		 */
439d5b5bb25SRich Morris 		cb.cb_flags |= ZFS_ITER_RECURSE;
44099653d4eSeschrock 		ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
441fa9e4066Sahrens 	} else {
442fa9e4066Sahrens 		int i;
443fa9e4066Sahrens 		zfs_handle_t *zhp;
444fa9e4066Sahrens 		zfs_type_t argtype;
445fa9e4066Sahrens 
446fa9e4066Sahrens 		/*
447fa9e4066Sahrens 		 * If we're recursive, then we always allow filesystems as
4481473b8d6Sloli 		 * arguments.  If we also are interested in snapshots or
4491473b8d6Sloli 		 * bookmarks, then we can take volumes as well.
450fa9e4066Sahrens 		 */
451fa9e4066Sahrens 		argtype = types;
452d5b5bb25SRich Morris 		if (flags & ZFS_ITER_RECURSE) {
453fa9e4066Sahrens 			argtype |= ZFS_TYPE_FILESYSTEM;
4541473b8d6Sloli 			if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK))
455fa9e4066Sahrens 				argtype |= ZFS_TYPE_VOLUME;
456fa9e4066Sahrens 		}
457fa9e4066Sahrens 
458fa9e4066Sahrens 		for (i = 0; i < argc; i++) {
459d5b5bb25SRich Morris 			if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
4605aba80dbSck 				zhp = zfs_path_to_zhandle(g_zfs, argv[i],
4615aba80dbSck 				    argtype);
4625aba80dbSck 			} else {
4635aba80dbSck 				zhp = zfs_open(g_zfs, argv[i], argtype);
4645aba80dbSck 			}
4655aba80dbSck 			if (zhp != NULL)
46699653d4eSeschrock 				ret |= zfs_callback(zhp, &cb);
467fa9e4066Sahrens 			else
468fa9e4066Sahrens 				ret = 1;
469fa9e4066Sahrens 		}
470fa9e4066Sahrens 	}
471fa9e4066Sahrens 
472fa9e4066Sahrens 	/*
473fa9e4066Sahrens 	 * At this point we've got our AVL tree full of zfs handles, so iterate
474fa9e4066Sahrens 	 * over each one and execute the real user callback.
475fa9e4066Sahrens 	 */
476fa9e4066Sahrens 	for (node = uu_avl_first(cb.cb_avl); node != NULL;
477fa9e4066Sahrens 	    node = uu_avl_next(cb.cb_avl, node))
478fa9e4066Sahrens 		ret |= callback(node->zn_handle, data);
479fa9e4066Sahrens 
480fa9e4066Sahrens 	/*
481fa9e4066Sahrens 	 * Finally, clean up the AVL tree.
482fa9e4066Sahrens 	 */
483a8b6ddafSMark J Musante 	if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
484a8b6ddafSMark J Musante 		nomem();
485fa9e4066Sahrens 
486fa9e4066Sahrens 	while ((node = uu_avl_walk_next(walk)) != NULL) {
487fa9e4066Sahrens 		uu_avl_remove(cb.cb_avl, node);
488fa9e4066Sahrens 		zfs_close(node->zn_handle);
489fa9e4066Sahrens 		free(node);
490fa9e4066Sahrens 	}
491fa9e4066Sahrens 
492fa9e4066Sahrens 	uu_avl_walk_end(walk);
493fa9e4066Sahrens 	uu_avl_destroy(cb.cb_avl);
494fa9e4066Sahrens 	uu_avl_pool_destroy(avl_pool);
495fa9e4066Sahrens 
496fa9e4066Sahrens 	return (ret);
497fa9e4066Sahrens }
498