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