xref: /onnv-gate/usr/src/cmd/zfs/zfs_iter.c (revision 789)
1*789Sahrens /*
2*789Sahrens  * CDDL HEADER START
3*789Sahrens  *
4*789Sahrens  * The contents of this file are subject to the terms of the
5*789Sahrens  * Common Development and Distribution License, Version 1.0 only
6*789Sahrens  * (the "License").  You may not use this file except in compliance
7*789Sahrens  * with the License.
8*789Sahrens  *
9*789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*789Sahrens  * or http://www.opensolaris.org/os/licensing.
11*789Sahrens  * See the License for the specific language governing permissions
12*789Sahrens  * and limitations under the License.
13*789Sahrens  *
14*789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15*789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17*789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18*789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19*789Sahrens  *
20*789Sahrens  * CDDL HEADER END
21*789Sahrens  */
22*789Sahrens /*
23*789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*789Sahrens  * Use is subject to license terms.
25*789Sahrens  */
26*789Sahrens 
27*789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*789Sahrens 
29*789Sahrens #include <libintl.h>
30*789Sahrens #include <libuutil.h>
31*789Sahrens #include <stddef.h>
32*789Sahrens #include <stdio.h>
33*789Sahrens #include <stdlib.h>
34*789Sahrens #include <strings.h>
35*789Sahrens 
36*789Sahrens #include <libzfs.h>
37*789Sahrens 
38*789Sahrens #include "zfs_util.h"
39*789Sahrens 
40*789Sahrens /*
41*789Sahrens  * This is a private interface used to gather up all the datasets specified on
42*789Sahrens  * the command line so that we can iterate over them in order.
43*789Sahrens  *
44*789Sahrens  * First, we iterate over all filesystems, gathering them together into an
45*789Sahrens  * AVL tree sorted by name.  For snapshots, we order them according to
46*789Sahrens  * creation time.  We report errors for any explicitly specified datasets
47*789Sahrens  * that we couldn't open.
48*789Sahrens  *
49*789Sahrens  * When finished, we have an AVL tree of ZFS handles.  We go through and execute
50*789Sahrens  * the provided callback for each one, passing whatever data the user supplied.
51*789Sahrens  */
52*789Sahrens 
53*789Sahrens typedef struct zfs_node {
54*789Sahrens 	zfs_handle_t	*zn_handle;
55*789Sahrens 	uu_avl_node_t	zn_avlnode;
56*789Sahrens } zfs_node_t;
57*789Sahrens 
58*789Sahrens typedef struct callback_data {
59*789Sahrens 	uu_avl_t	*cb_avl;
60*789Sahrens 	int		cb_recurse;
61*789Sahrens 	zfs_type_t	cb_types;
62*789Sahrens } callback_data_t;
63*789Sahrens 
64*789Sahrens uu_avl_pool_t *avl_pool;
65*789Sahrens 
66*789Sahrens /*
67*789Sahrens  * Called for each dataset.  If the object the object is of an appropriate type,
68*789Sahrens  * add it to the avl tree and recurse over any children as necessary.
69*789Sahrens  */
70*789Sahrens int
71*789Sahrens zfs_callback(zfs_handle_t *zhp, void *data)
72*789Sahrens {
73*789Sahrens 	callback_data_t *cb = data;
74*789Sahrens 	int dontclose = 0;
75*789Sahrens 
76*789Sahrens 	/*
77*789Sahrens 	 * If this object is of the appropriate type, add it to the AVL tree.
78*789Sahrens 	 */
79*789Sahrens 	if (zfs_get_type(zhp) & cb->cb_types) {
80*789Sahrens 		uu_avl_index_t idx;
81*789Sahrens 		zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
82*789Sahrens 
83*789Sahrens 		node->zn_handle = zhp;
84*789Sahrens 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
85*789Sahrens 		if (uu_avl_find(cb->cb_avl, node, NULL, &idx) == NULL) {
86*789Sahrens 			uu_avl_insert(cb->cb_avl, node, idx);
87*789Sahrens 			dontclose = 1;
88*789Sahrens 		} else {
89*789Sahrens 			free(node);
90*789Sahrens 		}
91*789Sahrens 	}
92*789Sahrens 
93*789Sahrens 	/*
94*789Sahrens 	 * If 'recurse' is set, and the datasets can have datasets of the
95*789Sahrens 	 * appropriate type, then recurse over its children.
96*789Sahrens 	 */
97*789Sahrens 	if (cb->cb_recurse && (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM ||
98*789Sahrens 	    (cb->cb_types & ZFS_TYPE_SNAPSHOT)))
99*789Sahrens 		(void) zfs_iter_children(zhp, zfs_callback, data);
100*789Sahrens 
101*789Sahrens 	if (!dontclose)
102*789Sahrens 		zfs_close(zhp);
103*789Sahrens 
104*789Sahrens 	return (0);
105*789Sahrens }
106*789Sahrens 
107*789Sahrens /* ARGSUSED */
108*789Sahrens static int
109*789Sahrens zfs_compare(const void *larg, const void *rarg, void *unused)
110*789Sahrens {
111*789Sahrens 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
112*789Sahrens 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
113*789Sahrens 	const char *lname = zfs_get_name(l);
114*789Sahrens 	const char *rname = zfs_get_name(r);
115*789Sahrens 	char *lat, *rat;
116*789Sahrens 	uint64_t lcreate, rcreate;
117*789Sahrens 	int ret;
118*789Sahrens 
119*789Sahrens 	lat = (char *)strchr(lname, '@');
120*789Sahrens 	rat = (char *)strchr(rname, '@');
121*789Sahrens 
122*789Sahrens 	if (lat != NULL)
123*789Sahrens 		*lat = '\0';
124*789Sahrens 	if (rat != NULL)
125*789Sahrens 		*rat = '\0';
126*789Sahrens 
127*789Sahrens 	ret = strcmp(lname, rname);
128*789Sahrens 	if (ret == 0) {
129*789Sahrens 		/*
130*789Sahrens 		 * If we're comparing a dataset to one of its snapshots, we
131*789Sahrens 		 * always make the full dataset first.
132*789Sahrens 		 */
133*789Sahrens 		if (lat == NULL) {
134*789Sahrens 			ret = -1;
135*789Sahrens 		} else if (rat == NULL) {
136*789Sahrens 			ret = 1;
137*789Sahrens 		} else {
138*789Sahrens 			/*
139*789Sahrens 			 * If we have two snapshots from the same dataset, then
140*789Sahrens 			 * we want to sort them according to creation time.  We
141*789Sahrens 			 * use the hidden CREATETXG property to get an absolute
142*789Sahrens 			 * ordering of snapshots.
143*789Sahrens 			 */
144*789Sahrens 			lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
145*789Sahrens 			rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
146*789Sahrens 
147*789Sahrens 			if (lcreate < rcreate)
148*789Sahrens 				ret = -1;
149*789Sahrens 			else if (lcreate > rcreate)
150*789Sahrens 				ret = 1;
151*789Sahrens 		}
152*789Sahrens 	}
153*789Sahrens 
154*789Sahrens 	if (lat != NULL)
155*789Sahrens 		*lat = '@';
156*789Sahrens 	if (rat != NULL)
157*789Sahrens 		*rat = '@';
158*789Sahrens 
159*789Sahrens 	return (ret);
160*789Sahrens }
161*789Sahrens 
162*789Sahrens int
163*789Sahrens zfs_for_each(int argc, char **argv, int recurse, zfs_type_t types,
164*789Sahrens     zfs_iter_f callback, void *data)
165*789Sahrens {
166*789Sahrens 	callback_data_t cb;
167*789Sahrens 	int ret = 0;
168*789Sahrens 	zfs_node_t *node;
169*789Sahrens 	uu_avl_walk_t *walk;
170*789Sahrens 
171*789Sahrens 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
172*789Sahrens 	    offsetof(zfs_node_t, zn_avlnode), zfs_compare, UU_DEFAULT);
173*789Sahrens 
174*789Sahrens 	if (avl_pool == NULL) {
175*789Sahrens 		(void) fprintf(stderr,
176*789Sahrens 		    gettext("internal error: out of memory\n"));
177*789Sahrens 		exit(1);
178*789Sahrens 	}
179*789Sahrens 
180*789Sahrens 	cb.cb_recurse = recurse;
181*789Sahrens 	cb.cb_types = types;
182*789Sahrens 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) {
183*789Sahrens 		(void) fprintf(stderr,
184*789Sahrens 		    gettext("internal error: out of memory\n"));
185*789Sahrens 		exit(1);
186*789Sahrens 	}
187*789Sahrens 
188*789Sahrens 	if (argc == 0) {
189*789Sahrens 		/*
190*789Sahrens 		 * If given no arguments, iterate over all datasets.
191*789Sahrens 		 */
192*789Sahrens 		cb.cb_recurse = 1;
193*789Sahrens 		ret = zfs_iter_root(zfs_callback, &cb);
194*789Sahrens 	} else {
195*789Sahrens 		int i;
196*789Sahrens 		zfs_handle_t *zhp;
197*789Sahrens 		zfs_type_t argtype;
198*789Sahrens 
199*789Sahrens 		/*
200*789Sahrens 		 * If we're recursive, then we always allow filesystems as
201*789Sahrens 		 * arguments.  If we also are interested in snapshots, then we
202*789Sahrens 		 * can take volumes as well.
203*789Sahrens 		 */
204*789Sahrens 		argtype = types;
205*789Sahrens 		if (recurse) {
206*789Sahrens 			argtype |= ZFS_TYPE_FILESYSTEM;
207*789Sahrens 			if (types & ZFS_TYPE_SNAPSHOT)
208*789Sahrens 				argtype |= ZFS_TYPE_VOLUME;
209*789Sahrens 		}
210*789Sahrens 
211*789Sahrens 		for (i = 0; i < argc; i++) {
212*789Sahrens 			if ((zhp = zfs_open(argv[i], argtype)) != NULL)
213*789Sahrens 				ret = zfs_callback(zhp, &cb);
214*789Sahrens 			else
215*789Sahrens 				ret = 1;
216*789Sahrens 		}
217*789Sahrens 	}
218*789Sahrens 
219*789Sahrens 	/*
220*789Sahrens 	 * At this point we've got our AVL tree full of zfs handles, so iterate
221*789Sahrens 	 * over each one and execute the real user callback.
222*789Sahrens 	 */
223*789Sahrens 	for (node = uu_avl_first(cb.cb_avl); node != NULL;
224*789Sahrens 	    node = uu_avl_next(cb.cb_avl, node))
225*789Sahrens 		ret |= callback(node->zn_handle, data);
226*789Sahrens 
227*789Sahrens 	/*
228*789Sahrens 	 * Finally, clean up the AVL tree.
229*789Sahrens 	 */
230*789Sahrens 	if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) {
231*789Sahrens 		(void) fprintf(stderr,
232*789Sahrens 		    gettext("internal error: out of memory"));
233*789Sahrens 		exit(1);
234*789Sahrens 	}
235*789Sahrens 
236*789Sahrens 	while ((node = uu_avl_walk_next(walk)) != NULL) {
237*789Sahrens 		uu_avl_remove(cb.cb_avl, node);
238*789Sahrens 		zfs_close(node->zn_handle);
239*789Sahrens 		free(node);
240*789Sahrens 	}
241*789Sahrens 
242*789Sahrens 	uu_avl_walk_end(walk);
243*789Sahrens 	uu_avl_destroy(cb.cb_avl);
244*789Sahrens 	uu_avl_pool_destroy(avl_pool);
245*789Sahrens 
246*789Sahrens 	return (ret);
247*789Sahrens }
248