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