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