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