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