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