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