xref: /freebsd/sys/contrib/openzfs/cmd/zfs/zfs_iter.c (revision 1f1e2261e341e6ca6862f82261066ef1705f0a7a)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
25eda14cbcSMatt Macy  * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2013 by Delphix. All rights reserved.
27eda14cbcSMatt Macy  */
28eda14cbcSMatt Macy 
29eda14cbcSMatt Macy #include <libintl.h>
30eda14cbcSMatt Macy #include <libuutil.h>
31eda14cbcSMatt Macy #include <stddef.h>
32eda14cbcSMatt Macy #include <stdio.h>
33eda14cbcSMatt Macy #include <stdlib.h>
34eda14cbcSMatt Macy #include <string.h>
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <libzfs.h>
37eda14cbcSMatt Macy 
38eda14cbcSMatt Macy #include "zfs_util.h"
39eda14cbcSMatt Macy #include "zfs_iter.h"
40eda14cbcSMatt Macy 
41eda14cbcSMatt Macy /*
42eda14cbcSMatt Macy  * This is a private interface used to gather up all the datasets specified on
43eda14cbcSMatt Macy  * the command line so that we can iterate over them in order.
44eda14cbcSMatt Macy  *
45eda14cbcSMatt Macy  * First, we iterate over all filesystems, gathering them together into an
46eda14cbcSMatt Macy  * AVL tree.  We report errors for any explicitly specified datasets
47eda14cbcSMatt Macy  * that we couldn't open.
48eda14cbcSMatt Macy  *
49eda14cbcSMatt Macy  * When finished, we have an AVL tree of ZFS handles.  We go through and execute
50eda14cbcSMatt Macy  * the provided callback for each one, passing whatever data the user supplied.
51eda14cbcSMatt Macy  */
52eda14cbcSMatt Macy 
53eda14cbcSMatt Macy typedef struct zfs_node {
54eda14cbcSMatt Macy 	zfs_handle_t	*zn_handle;
55eda14cbcSMatt Macy 	uu_avl_node_t	zn_avlnode;
56eda14cbcSMatt Macy } zfs_node_t;
57eda14cbcSMatt Macy 
58eda14cbcSMatt Macy typedef struct callback_data {
59eda14cbcSMatt Macy 	uu_avl_t		*cb_avl;
60eda14cbcSMatt Macy 	int			cb_flags;
61eda14cbcSMatt Macy 	zfs_type_t		cb_types;
62eda14cbcSMatt Macy 	zfs_sort_column_t	*cb_sortcol;
63eda14cbcSMatt Macy 	zprop_list_t		**cb_proplist;
64eda14cbcSMatt Macy 	int			cb_depth_limit;
65eda14cbcSMatt Macy 	int			cb_depth;
66eda14cbcSMatt Macy 	uint8_t			cb_props_table[ZFS_NUM_PROPS];
67eda14cbcSMatt Macy } callback_data_t;
68eda14cbcSMatt Macy 
69eda14cbcSMatt Macy uu_avl_pool_t *avl_pool;
70eda14cbcSMatt Macy 
71eda14cbcSMatt Macy /*
72eda14cbcSMatt Macy  * Include snaps if they were requested or if this a zfs list where types
73eda14cbcSMatt Macy  * were not specified and the "listsnapshots" property is set on this pool.
74eda14cbcSMatt Macy  */
75eda14cbcSMatt Macy static boolean_t
76eda14cbcSMatt Macy zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb)
77eda14cbcSMatt Macy {
78eda14cbcSMatt Macy 	zpool_handle_t *zph;
79eda14cbcSMatt Macy 
80eda14cbcSMatt Macy 	if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0)
81eda14cbcSMatt Macy 		return (cb->cb_types & ZFS_TYPE_SNAPSHOT);
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	zph = zfs_get_pool_handle(zhp);
84eda14cbcSMatt Macy 	return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL));
85eda14cbcSMatt Macy }
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy /*
88eda14cbcSMatt Macy  * Called for each dataset.  If the object is of an appropriate type,
89eda14cbcSMatt Macy  * add it to the avl tree and recurse over any children as necessary.
90eda14cbcSMatt Macy  */
91eda14cbcSMatt Macy static int
92eda14cbcSMatt Macy zfs_callback(zfs_handle_t *zhp, void *data)
93eda14cbcSMatt Macy {
94eda14cbcSMatt Macy 	callback_data_t *cb = data;
95eda14cbcSMatt Macy 	boolean_t should_close = B_TRUE;
96eda14cbcSMatt Macy 	boolean_t include_snaps = zfs_include_snapshots(zhp, cb);
97eda14cbcSMatt Macy 	boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK);
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	if ((zfs_get_type(zhp) & cb->cb_types) ||
100eda14cbcSMatt Macy 	    ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
101eda14cbcSMatt Macy 		uu_avl_index_t idx;
102eda14cbcSMatt Macy 		zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
103eda14cbcSMatt Macy 
104eda14cbcSMatt Macy 		node->zn_handle = zhp;
105eda14cbcSMatt Macy 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
106eda14cbcSMatt Macy 		if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
107eda14cbcSMatt Macy 		    &idx) == NULL) {
108eda14cbcSMatt Macy 			if (cb->cb_proplist) {
109eda14cbcSMatt Macy 				if ((*cb->cb_proplist) &&
110eda14cbcSMatt Macy 				    !(*cb->cb_proplist)->pl_all)
111eda14cbcSMatt Macy 					zfs_prune_proplist(zhp,
112eda14cbcSMatt Macy 					    cb->cb_props_table);
113eda14cbcSMatt Macy 
114eda14cbcSMatt Macy 				if (zfs_expand_proplist(zhp, cb->cb_proplist,
115eda14cbcSMatt Macy 				    (cb->cb_flags & ZFS_ITER_RECVD_PROPS),
116eda14cbcSMatt Macy 				    (cb->cb_flags & ZFS_ITER_LITERAL_PROPS))
117eda14cbcSMatt Macy 				    != 0) {
118eda14cbcSMatt Macy 					free(node);
119eda14cbcSMatt Macy 					return (-1);
120eda14cbcSMatt Macy 				}
121eda14cbcSMatt Macy 			}
122eda14cbcSMatt Macy 			uu_avl_insert(cb->cb_avl, node, idx);
123eda14cbcSMatt Macy 			should_close = B_FALSE;
124eda14cbcSMatt Macy 		} else {
125eda14cbcSMatt Macy 			free(node);
126eda14cbcSMatt Macy 		}
127eda14cbcSMatt Macy 	}
128eda14cbcSMatt Macy 
129eda14cbcSMatt Macy 	/*
130eda14cbcSMatt Macy 	 * Recurse if necessary.
131eda14cbcSMatt Macy 	 */
132eda14cbcSMatt Macy 	if (cb->cb_flags & ZFS_ITER_RECURSE &&
133eda14cbcSMatt Macy 	    ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
134eda14cbcSMatt Macy 	    cb->cb_depth < cb->cb_depth_limit)) {
135eda14cbcSMatt Macy 		cb->cb_depth++;
136eda14cbcSMatt Macy 
137eda14cbcSMatt Macy 		/*
138eda14cbcSMatt Macy 		 * If we are not looking for filesystems, we don't need to
139eda14cbcSMatt Macy 		 * recurse into filesystems when we are at our depth limit.
140eda14cbcSMatt Macy 		 */
141eda14cbcSMatt Macy 		if ((cb->cb_depth < cb->cb_depth_limit ||
142eda14cbcSMatt Macy 		    (cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
143eda14cbcSMatt Macy 		    (cb->cb_types &
144eda14cbcSMatt Macy 		    (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) &&
145eda14cbcSMatt Macy 		    zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
146eda14cbcSMatt Macy 			(void) zfs_iter_filesystems(zhp, zfs_callback, data);
147eda14cbcSMatt Macy 		}
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy 		if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
150eda14cbcSMatt Macy 		    ZFS_TYPE_BOOKMARK)) == 0) && include_snaps) {
151eda14cbcSMatt Macy 			(void) zfs_iter_snapshots(zhp,
152eda14cbcSMatt Macy 			    (cb->cb_flags & ZFS_ITER_SIMPLE) != 0,
153eda14cbcSMatt Macy 			    zfs_callback, data, 0, 0);
154eda14cbcSMatt Macy 		}
155eda14cbcSMatt Macy 
156eda14cbcSMatt Macy 		if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
157eda14cbcSMatt Macy 		    ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks) {
158eda14cbcSMatt Macy 			(void) zfs_iter_bookmarks(zhp, zfs_callback, data);
159eda14cbcSMatt Macy 		}
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 		cb->cb_depth--;
162eda14cbcSMatt Macy 	}
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy 	if (should_close)
165eda14cbcSMatt Macy 		zfs_close(zhp);
166eda14cbcSMatt Macy 
167eda14cbcSMatt Macy 	return (0);
168eda14cbcSMatt Macy }
169eda14cbcSMatt Macy 
170eda14cbcSMatt Macy int
171eda14cbcSMatt Macy zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
172eda14cbcSMatt Macy     boolean_t reverse)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy 	zfs_sort_column_t *col;
175eda14cbcSMatt Macy 	zfs_prop_t prop;
176eda14cbcSMatt Macy 
177*1f1e2261SMartin Matuska 	if ((prop = zfs_name_to_prop(name)) == ZPROP_USERPROP &&
178eda14cbcSMatt Macy 	    !zfs_prop_user(name))
179eda14cbcSMatt Macy 		return (-1);
180eda14cbcSMatt Macy 
181eda14cbcSMatt Macy 	col = safe_malloc(sizeof (zfs_sort_column_t));
182eda14cbcSMatt Macy 
183eda14cbcSMatt Macy 	col->sc_prop = prop;
184eda14cbcSMatt Macy 	col->sc_reverse = reverse;
185*1f1e2261SMartin Matuska 	if (prop == ZPROP_USERPROP) {
186eda14cbcSMatt Macy 		col->sc_user_prop = safe_malloc(strlen(name) + 1);
187eda14cbcSMatt Macy 		(void) strcpy(col->sc_user_prop, name);
188eda14cbcSMatt Macy 	}
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	if (*sc == NULL) {
191eda14cbcSMatt Macy 		col->sc_last = col;
192eda14cbcSMatt Macy 		*sc = col;
193eda14cbcSMatt Macy 	} else {
194eda14cbcSMatt Macy 		(*sc)->sc_last->sc_next = col;
195eda14cbcSMatt Macy 		(*sc)->sc_last = col;
196eda14cbcSMatt Macy 	}
197eda14cbcSMatt Macy 
198eda14cbcSMatt Macy 	return (0);
199eda14cbcSMatt Macy }
200eda14cbcSMatt Macy 
201eda14cbcSMatt Macy void
202eda14cbcSMatt Macy zfs_free_sort_columns(zfs_sort_column_t *sc)
203eda14cbcSMatt Macy {
204eda14cbcSMatt Macy 	zfs_sort_column_t *col;
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy 	while (sc != NULL) {
207eda14cbcSMatt Macy 		col = sc->sc_next;
208eda14cbcSMatt Macy 		free(sc->sc_user_prop);
209eda14cbcSMatt Macy 		free(sc);
210eda14cbcSMatt Macy 		sc = col;
211eda14cbcSMatt Macy 	}
212eda14cbcSMatt Macy }
213eda14cbcSMatt Macy 
214eda14cbcSMatt Macy int
215eda14cbcSMatt Macy zfs_sort_only_by_name(const zfs_sort_column_t *sc)
216eda14cbcSMatt Macy {
217eda14cbcSMatt Macy 	return (sc != NULL && sc->sc_next == NULL &&
218eda14cbcSMatt Macy 	    sc->sc_prop == ZFS_PROP_NAME);
219eda14cbcSMatt Macy }
220eda14cbcSMatt Macy 
221eda14cbcSMatt Macy static int
222e92ffd9bSMartin Matuska zfs_compare(const void *larg, const void *rarg)
223eda14cbcSMatt Macy {
224eda14cbcSMatt Macy 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
225eda14cbcSMatt Macy 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
226eda14cbcSMatt Macy 	const char *lname = zfs_get_name(l);
227eda14cbcSMatt Macy 	const char *rname = zfs_get_name(r);
228eda14cbcSMatt Macy 	char *lat, *rat;
229eda14cbcSMatt Macy 	uint64_t lcreate, rcreate;
230eda14cbcSMatt Macy 	int ret;
231eda14cbcSMatt Macy 
232eda14cbcSMatt Macy 	lat = (char *)strchr(lname, '@');
233eda14cbcSMatt Macy 	rat = (char *)strchr(rname, '@');
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	if (lat != NULL)
236eda14cbcSMatt Macy 		*lat = '\0';
237eda14cbcSMatt Macy 	if (rat != NULL)
238eda14cbcSMatt Macy 		*rat = '\0';
239eda14cbcSMatt Macy 
240eda14cbcSMatt Macy 	ret = strcmp(lname, rname);
241eda14cbcSMatt Macy 	if (ret == 0 && (lat != NULL || rat != NULL)) {
242eda14cbcSMatt Macy 		/*
243eda14cbcSMatt Macy 		 * If we're comparing a dataset to one of its snapshots, we
244eda14cbcSMatt Macy 		 * always make the full dataset first.
245eda14cbcSMatt Macy 		 */
246eda14cbcSMatt Macy 		if (lat == NULL) {
247eda14cbcSMatt Macy 			ret = -1;
248eda14cbcSMatt Macy 		} else if (rat == NULL) {
249eda14cbcSMatt Macy 			ret = 1;
250eda14cbcSMatt Macy 		} else {
251eda14cbcSMatt Macy 			/*
252eda14cbcSMatt Macy 			 * If we have two snapshots from the same dataset, then
253eda14cbcSMatt Macy 			 * we want to sort them according to creation time.  We
254eda14cbcSMatt Macy 			 * use the hidden CREATETXG property to get an absolute
255eda14cbcSMatt Macy 			 * ordering of snapshots.
256eda14cbcSMatt Macy 			 */
257eda14cbcSMatt Macy 			lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
258eda14cbcSMatt Macy 			rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
259eda14cbcSMatt Macy 
260eda14cbcSMatt Macy 			/*
261eda14cbcSMatt Macy 			 * Both lcreate and rcreate being 0 means we don't have
262eda14cbcSMatt Macy 			 * properties and we should compare full name.
263eda14cbcSMatt Macy 			 */
264eda14cbcSMatt Macy 			if (lcreate == 0 && rcreate == 0)
265eda14cbcSMatt Macy 				ret = strcmp(lat + 1, rat + 1);
266eda14cbcSMatt Macy 			else if (lcreate < rcreate)
267eda14cbcSMatt Macy 				ret = -1;
268eda14cbcSMatt Macy 			else if (lcreate > rcreate)
269eda14cbcSMatt Macy 				ret = 1;
270eda14cbcSMatt Macy 		}
271eda14cbcSMatt Macy 	}
272eda14cbcSMatt Macy 
273eda14cbcSMatt Macy 	if (lat != NULL)
274eda14cbcSMatt Macy 		*lat = '@';
275eda14cbcSMatt Macy 	if (rat != NULL)
276eda14cbcSMatt Macy 		*rat = '@';
277eda14cbcSMatt Macy 
278eda14cbcSMatt Macy 	return (ret);
279eda14cbcSMatt Macy }
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy /*
282eda14cbcSMatt Macy  * Sort datasets by specified columns.
283eda14cbcSMatt Macy  *
284eda14cbcSMatt Macy  * o  Numeric types sort in ascending order.
285eda14cbcSMatt Macy  * o  String types sort in alphabetical order.
286eda14cbcSMatt Macy  * o  Types inappropriate for a row sort that row to the literal
287eda14cbcSMatt Macy  *    bottom, regardless of the specified ordering.
288eda14cbcSMatt Macy  *
289eda14cbcSMatt Macy  * If no sort columns are specified, or two datasets compare equally
290eda14cbcSMatt Macy  * across all specified columns, they are sorted alphabetically by name
291eda14cbcSMatt Macy  * with snapshots grouped under their parents.
292eda14cbcSMatt Macy  */
293eda14cbcSMatt Macy static int
294eda14cbcSMatt Macy zfs_sort(const void *larg, const void *rarg, void *data)
295eda14cbcSMatt Macy {
296eda14cbcSMatt Macy 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
297eda14cbcSMatt Macy 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
298eda14cbcSMatt Macy 	zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
299eda14cbcSMatt Macy 	zfs_sort_column_t *psc;
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	for (psc = sc; psc != NULL; psc = psc->sc_next) {
302eda14cbcSMatt Macy 		char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
303eda14cbcSMatt Macy 		char *lstr, *rstr;
304eda14cbcSMatt Macy 		uint64_t lnum, rnum;
305eda14cbcSMatt Macy 		boolean_t lvalid, rvalid;
306eda14cbcSMatt Macy 		int ret = 0;
307eda14cbcSMatt Macy 
308eda14cbcSMatt Macy 		/*
309eda14cbcSMatt Macy 		 * We group the checks below the generic code.  If 'lstr' and
310eda14cbcSMatt Macy 		 * 'rstr' are non-NULL, then we do a string based comparison.
311eda14cbcSMatt Macy 		 * Otherwise, we compare 'lnum' and 'rnum'.
312eda14cbcSMatt Macy 		 */
313eda14cbcSMatt Macy 		lstr = rstr = NULL;
314*1f1e2261SMartin Matuska 		if (psc->sc_prop == ZPROP_USERPROP) {
315eda14cbcSMatt Macy 			nvlist_t *luser, *ruser;
316eda14cbcSMatt Macy 			nvlist_t *lval, *rval;
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 			luser = zfs_get_user_props(l);
319eda14cbcSMatt Macy 			ruser = zfs_get_user_props(r);
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 			lvalid = (nvlist_lookup_nvlist(luser,
322eda14cbcSMatt Macy 			    psc->sc_user_prop, &lval) == 0);
323eda14cbcSMatt Macy 			rvalid = (nvlist_lookup_nvlist(ruser,
324eda14cbcSMatt Macy 			    psc->sc_user_prop, &rval) == 0);
325eda14cbcSMatt Macy 
326eda14cbcSMatt Macy 			if (lvalid)
327eda14cbcSMatt Macy 				verify(nvlist_lookup_string(lval,
328eda14cbcSMatt Macy 				    ZPROP_VALUE, &lstr) == 0);
329eda14cbcSMatt Macy 			if (rvalid)
330eda14cbcSMatt Macy 				verify(nvlist_lookup_string(rval,
331eda14cbcSMatt Macy 				    ZPROP_VALUE, &rstr) == 0);
332eda14cbcSMatt Macy 		} else if (psc->sc_prop == ZFS_PROP_NAME) {
333eda14cbcSMatt Macy 			lvalid = rvalid = B_TRUE;
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 			(void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf));
336eda14cbcSMatt Macy 			(void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf));
337eda14cbcSMatt Macy 
338eda14cbcSMatt Macy 			lstr = lbuf;
339eda14cbcSMatt Macy 			rstr = rbuf;
340eda14cbcSMatt Macy 		} else if (zfs_prop_is_string(psc->sc_prop)) {
341eda14cbcSMatt Macy 			lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
342eda14cbcSMatt Macy 			    sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
343eda14cbcSMatt Macy 			rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
344eda14cbcSMatt Macy 			    sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 			lstr = lbuf;
347eda14cbcSMatt Macy 			rstr = rbuf;
348eda14cbcSMatt Macy 		} else {
349eda14cbcSMatt Macy 			lvalid = zfs_prop_valid_for_type(psc->sc_prop,
350eda14cbcSMatt Macy 			    zfs_get_type(l), B_FALSE);
351eda14cbcSMatt Macy 			rvalid = zfs_prop_valid_for_type(psc->sc_prop,
352eda14cbcSMatt Macy 			    zfs_get_type(r), B_FALSE);
353eda14cbcSMatt Macy 
354eda14cbcSMatt Macy 			if (lvalid)
355eda14cbcSMatt Macy 				(void) zfs_prop_get_numeric(l, psc->sc_prop,
356eda14cbcSMatt Macy 				    &lnum, NULL, NULL, 0);
357eda14cbcSMatt Macy 			if (rvalid)
358eda14cbcSMatt Macy 				(void) zfs_prop_get_numeric(r, psc->sc_prop,
359eda14cbcSMatt Macy 				    &rnum, NULL, NULL, 0);
360eda14cbcSMatt Macy 		}
361eda14cbcSMatt Macy 
362eda14cbcSMatt Macy 		if (!lvalid && !rvalid)
363eda14cbcSMatt Macy 			continue;
364eda14cbcSMatt Macy 		else if (!lvalid)
365eda14cbcSMatt Macy 			return (1);
366eda14cbcSMatt Macy 		else if (!rvalid)
367eda14cbcSMatt Macy 			return (-1);
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 		if (lstr)
370eda14cbcSMatt Macy 			ret = strcmp(lstr, rstr);
371eda14cbcSMatt Macy 		else if (lnum < rnum)
372eda14cbcSMatt Macy 			ret = -1;
373eda14cbcSMatt Macy 		else if (lnum > rnum)
374eda14cbcSMatt Macy 			ret = 1;
375eda14cbcSMatt Macy 
376eda14cbcSMatt Macy 		if (ret != 0) {
377eda14cbcSMatt Macy 			if (psc->sc_reverse == B_TRUE)
378eda14cbcSMatt Macy 				ret = (ret < 0) ? 1 : -1;
379eda14cbcSMatt Macy 			return (ret);
380eda14cbcSMatt Macy 		}
381eda14cbcSMatt Macy 	}
382eda14cbcSMatt Macy 
383e92ffd9bSMartin Matuska 	return (zfs_compare(larg, rarg));
384eda14cbcSMatt Macy }
385eda14cbcSMatt Macy 
386eda14cbcSMatt Macy int
387eda14cbcSMatt Macy zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
388eda14cbcSMatt Macy     zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
389eda14cbcSMatt Macy     zfs_iter_f callback, void *data)
390eda14cbcSMatt Macy {
391eda14cbcSMatt Macy 	callback_data_t cb = {0};
392eda14cbcSMatt Macy 	int ret = 0;
393eda14cbcSMatt Macy 	zfs_node_t *node;
394eda14cbcSMatt Macy 	uu_avl_walk_t *walk;
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
397eda14cbcSMatt Macy 	    offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy 	if (avl_pool == NULL)
400eda14cbcSMatt Macy 		nomem();
401eda14cbcSMatt Macy 
402eda14cbcSMatt Macy 	cb.cb_sortcol = sortcol;
403eda14cbcSMatt Macy 	cb.cb_flags = flags;
404eda14cbcSMatt Macy 	cb.cb_proplist = proplist;
405eda14cbcSMatt Macy 	cb.cb_types = types;
406eda14cbcSMatt Macy 	cb.cb_depth_limit = limit;
407eda14cbcSMatt Macy 	/*
408eda14cbcSMatt Macy 	 * If cb_proplist is provided then in the zfs_handles created we
409eda14cbcSMatt Macy 	 * retain only those properties listed in cb_proplist and sortcol.
410eda14cbcSMatt Macy 	 * The rest are pruned. So, the caller should make sure that no other
411eda14cbcSMatt Macy 	 * properties other than those listed in cb_proplist/sortcol are
412eda14cbcSMatt Macy 	 * accessed.
413eda14cbcSMatt Macy 	 *
414eda14cbcSMatt Macy 	 * If cb_proplist is NULL then we retain all the properties.  We
415eda14cbcSMatt Macy 	 * always retain the zoned property, which some other properties
416eda14cbcSMatt Macy 	 * need (userquota & friends), and the createtxg property, which
417eda14cbcSMatt Macy 	 * we need to sort snapshots.
418eda14cbcSMatt Macy 	 */
419eda14cbcSMatt Macy 	if (cb.cb_proplist && *cb.cb_proplist) {
420eda14cbcSMatt Macy 		zprop_list_t *p = *cb.cb_proplist;
421eda14cbcSMatt Macy 
422eda14cbcSMatt Macy 		while (p) {
423eda14cbcSMatt Macy 			if (p->pl_prop >= ZFS_PROP_TYPE &&
424eda14cbcSMatt Macy 			    p->pl_prop < ZFS_NUM_PROPS) {
425eda14cbcSMatt Macy 				cb.cb_props_table[p->pl_prop] = B_TRUE;
426eda14cbcSMatt Macy 			}
427eda14cbcSMatt Macy 			p = p->pl_next;
428eda14cbcSMatt Macy 		}
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy 		while (sortcol) {
431eda14cbcSMatt Macy 			if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
432eda14cbcSMatt Macy 			    sortcol->sc_prop < ZFS_NUM_PROPS) {
433eda14cbcSMatt Macy 				cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
434eda14cbcSMatt Macy 			}
435eda14cbcSMatt Macy 			sortcol = sortcol->sc_next;
436eda14cbcSMatt Macy 		}
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy 		cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
439eda14cbcSMatt Macy 		cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
440eda14cbcSMatt Macy 	} else {
441eda14cbcSMatt Macy 		(void) memset(cb.cb_props_table, B_TRUE,
442eda14cbcSMatt Macy 		    sizeof (cb.cb_props_table));
443eda14cbcSMatt Macy 	}
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
446eda14cbcSMatt Macy 		nomem();
447eda14cbcSMatt Macy 
448eda14cbcSMatt Macy 	if (argc == 0) {
449eda14cbcSMatt Macy 		/*
450eda14cbcSMatt Macy 		 * If given no arguments, iterate over all datasets.
451eda14cbcSMatt Macy 		 */
452eda14cbcSMatt Macy 		cb.cb_flags |= ZFS_ITER_RECURSE;
453eda14cbcSMatt Macy 		ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
454eda14cbcSMatt Macy 	} else {
455c03c5b1cSMartin Matuska 		zfs_handle_t *zhp = NULL;
456c03c5b1cSMartin Matuska 		zfs_type_t argtype = types;
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy 		/*
459eda14cbcSMatt Macy 		 * If we're recursive, then we always allow filesystems as
460eda14cbcSMatt Macy 		 * arguments.  If we also are interested in snapshots or
461eda14cbcSMatt Macy 		 * bookmarks, then we can take volumes as well.
462eda14cbcSMatt Macy 		 */
463eda14cbcSMatt Macy 		if (flags & ZFS_ITER_RECURSE) {
464eda14cbcSMatt Macy 			argtype |= ZFS_TYPE_FILESYSTEM;
465eda14cbcSMatt Macy 			if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK))
466eda14cbcSMatt Macy 				argtype |= ZFS_TYPE_VOLUME;
467eda14cbcSMatt Macy 		}
468eda14cbcSMatt Macy 
469c03c5b1cSMartin Matuska 		for (int i = 0; i < argc; i++) {
470eda14cbcSMatt Macy 			if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
471eda14cbcSMatt Macy 				zhp = zfs_path_to_zhandle(g_zfs, argv[i],
472eda14cbcSMatt Macy 				    argtype);
473eda14cbcSMatt Macy 			} else {
474eda14cbcSMatt Macy 				zhp = zfs_open(g_zfs, argv[i], argtype);
475eda14cbcSMatt Macy 			}
476eda14cbcSMatt Macy 			if (zhp != NULL)
477eda14cbcSMatt Macy 				ret |= zfs_callback(zhp, &cb);
478eda14cbcSMatt Macy 			else
479eda14cbcSMatt Macy 				ret = 1;
480eda14cbcSMatt Macy 		}
481eda14cbcSMatt Macy 	}
482eda14cbcSMatt Macy 
483eda14cbcSMatt Macy 	/*
484eda14cbcSMatt Macy 	 * At this point we've got our AVL tree full of zfs handles, so iterate
485eda14cbcSMatt Macy 	 * over each one and execute the real user callback.
486eda14cbcSMatt Macy 	 */
487eda14cbcSMatt Macy 	for (node = uu_avl_first(cb.cb_avl); node != NULL;
488eda14cbcSMatt Macy 	    node = uu_avl_next(cb.cb_avl, node))
489eda14cbcSMatt Macy 		ret |= callback(node->zn_handle, data);
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy 	/*
492eda14cbcSMatt Macy 	 * Finally, clean up the AVL tree.
493eda14cbcSMatt Macy 	 */
494eda14cbcSMatt Macy 	if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
495eda14cbcSMatt Macy 		nomem();
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	while ((node = uu_avl_walk_next(walk)) != NULL) {
498eda14cbcSMatt Macy 		uu_avl_remove(cb.cb_avl, node);
499eda14cbcSMatt Macy 		zfs_close(node->zn_handle);
500eda14cbcSMatt Macy 		free(node);
501eda14cbcSMatt Macy 	}
502eda14cbcSMatt Macy 
503eda14cbcSMatt Macy 	uu_avl_walk_end(walk);
504eda14cbcSMatt Macy 	uu_avl_destroy(cb.cb_avl);
505eda14cbcSMatt Macy 	uu_avl_pool_destroy(avl_pool);
506eda14cbcSMatt Macy 
507eda14cbcSMatt Macy 	return (ret);
508eda14cbcSMatt Macy }
509