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