]> git.proxmox.com Git - mirror_zfs.git/blame - cmd/zfs/zfs_iter.c
libzfs: add v2 iterator interfaces
[mirror_zfs.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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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 */
54d5378f 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>.
54d5378f 25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
da536844 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>
93ce2b4c 34#include <string.h>
34dc7c2f
BB
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 */
da536844 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;
5727b00e 95 boolean_t should_close = B_TRUE;
da536844
MA
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,
54d5378f
YP
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);
5727b00e 123 should_close = B_FALSE;
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++;
df583073
TC
136
137 /*
138 * If we are not looking for filesystems, we don't need to
139 * recurse into filesystems when we are at our depth limit.
140 */
141 if ((cb->cb_depth < cb->cb_depth_limit ||
142 (cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
143 (cb->cb_types &
144 (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) &&
145 zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
baca06c2 146 (void) zfs_iter_filesystems_v2(zhp, cb->cb_flags,
dc95911d 147 zfs_callback, data);
df583073
TC
148 }
149
da536844 150 if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
df583073 151 ZFS_TYPE_BOOKMARK)) == 0) && include_snaps) {
baca06c2 152 (void) zfs_iter_snapshots_v2(zhp, cb->cb_flags,
df583073
TC
153 zfs_callback, data, 0, 0);
154 }
155
da536844 156 if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
df583073 157 ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks) {
baca06c2 158 (void) zfs_iter_bookmarks_v2(zhp, cb->cb_flags,
dc95911d 159 zfs_callback, data);
df583073
TC
160 }
161
9babb374 162 cb->cb_depth--;
34dc7c2f
BB
163 }
164
5727b00e 165 if (should_close)
34dc7c2f
BB
166 zfs_close(zhp);
167
168 return (0);
169}
170
171int
172zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
173 boolean_t reverse)
174{
175 zfs_sort_column_t *col;
176 zfs_prop_t prop;
177
4ff7a8fa 178 if ((prop = zfs_name_to_prop(name)) == ZPROP_USERPROP &&
34dc7c2f
BB
179 !zfs_prop_user(name))
180 return (-1);
181
182 col = safe_malloc(sizeof (zfs_sort_column_t));
183
184 col->sc_prop = prop;
185 col->sc_reverse = reverse;
4ff7a8fa 186 if (prop == ZPROP_USERPROP) {
34dc7c2f
BB
187 col->sc_user_prop = safe_malloc(strlen(name) + 1);
188 (void) strcpy(col->sc_user_prop, name);
189 }
190
191 if (*sc == NULL) {
192 col->sc_last = col;
193 *sc = col;
194 } else {
195 (*sc)->sc_last->sc_next = col;
196 (*sc)->sc_last = col;
197 }
198
199 return (0);
200}
201
202void
203zfs_free_sort_columns(zfs_sort_column_t *sc)
204{
205 zfs_sort_column_t *col;
206
207 while (sc != NULL) {
208 col = sc->sc_next;
209 free(sc->sc_user_prop);
210 free(sc);
211 sc = col;
212 }
213}
214
dc95911d
AJ
215/*
216 * Return true if all of the properties to be sorted are populated by
217 * dsl_dataset_fast_stat(). Note that sc == NULL (no sort) means we
218 * don't need any extra properties, so returns true.
219 */
220boolean_t
221zfs_sort_only_by_fast(const zfs_sort_column_t *sc)
f6a0dac8 222{
dc95911d
AJ
223 while (sc != NULL) {
224 switch (sc->sc_prop) {
225 case ZFS_PROP_NAME:
226 case ZFS_PROP_GUID:
227 case ZFS_PROP_CREATETXG:
228 case ZFS_PROP_NUMCLONES:
229 case ZFS_PROP_INCONSISTENT:
230 case ZFS_PROP_REDACTED:
231 case ZFS_PROP_ORIGIN:
232 break;
233 default:
234 return (B_FALSE);
235 }
236 sc = sc->sc_next;
237 }
238
239 return (B_TRUE);
0cee2406
PJD
240}
241
dc95911d
AJ
242boolean_t
243zfs_list_only_by_fast(const zprop_list_t *p)
3a1ce491 244{
dc95911d
AJ
245 if (p == NULL) {
246 /* NULL means 'all' so we can't use simple mode */
247 return (B_FALSE);
248 }
249
250 while (p != NULL) {
251 switch (p->pl_prop) {
252 case ZFS_PROP_NAME:
253 case ZFS_PROP_GUID:
254 case ZFS_PROP_CREATETXG:
255 case ZFS_PROP_NUMCLONES:
256 case ZFS_PROP_INCONSISTENT:
257 case ZFS_PROP_REDACTED:
258 case ZFS_PROP_ORIGIN:
259 break;
260 default:
261 return (B_FALSE);
262 }
263 p = p->pl_next;
264 }
265
266 return (B_TRUE);
3a1ce491
AH
267}
268
34dc7c2f 269static int
1fedd012 270zfs_compare(const void *larg, const void *rarg)
34dc7c2f
BB
271{
272 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
273 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
274 const char *lname = zfs_get_name(l);
275 const char *rname = zfs_get_name(r);
276 char *lat, *rat;
277 uint64_t lcreate, rcreate;
278 int ret;
279
280 lat = (char *)strchr(lname, '@');
281 rat = (char *)strchr(rname, '@');
282
283 if (lat != NULL)
284 *lat = '\0';
285 if (rat != NULL)
286 *rat = '\0';
287
288 ret = strcmp(lname, rname);
4bb40c1c 289 if (ret == 0 && (lat != NULL || rat != NULL)) {
34dc7c2f
BB
290 /*
291 * If we're comparing a dataset to one of its snapshots, we
292 * always make the full dataset first.
293 */
294 if (lat == NULL) {
295 ret = -1;
296 } else if (rat == NULL) {
297 ret = 1;
298 } else {
299 /*
300 * If we have two snapshots from the same dataset, then
301 * we want to sort them according to creation time. We
302 * use the hidden CREATETXG property to get an absolute
303 * ordering of snapshots.
304 */
305 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
306 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
307
0cee2406
PJD
308 /*
309 * Both lcreate and rcreate being 0 means we don't have
310 * properties and we should compare full name.
311 */
312 if (lcreate == 0 && rcreate == 0)
313 ret = strcmp(lat + 1, rat + 1);
314 else if (lcreate < rcreate)
34dc7c2f
BB
315 ret = -1;
316 else if (lcreate > rcreate)
317 ret = 1;
318 }
319 }
320
321 if (lat != NULL)
322 *lat = '@';
323 if (rat != NULL)
324 *rat = '@';
325
326 return (ret);
327}
328
329/*
330 * Sort datasets by specified columns.
331 *
332 * o Numeric types sort in ascending order.
333 * o String types sort in alphabetical order.
334 * o Types inappropriate for a row sort that row to the literal
335 * bottom, regardless of the specified ordering.
336 *
337 * If no sort columns are specified, or two datasets compare equally
338 * across all specified columns, they are sorted alphabetically by name
339 * with snapshots grouped under their parents.
340 */
341static int
342zfs_sort(const void *larg, const void *rarg, void *data)
343{
344 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
345 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
346 zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
347 zfs_sort_column_t *psc;
348
349 for (psc = sc; psc != NULL; psc = psc->sc_next) {
350 char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
d1807f16 351 const char *lstr, *rstr;
3a1ce491 352 uint64_t lnum = 0, rnum = 0;
34dc7c2f
BB
353 boolean_t lvalid, rvalid;
354 int ret = 0;
355
356 /*
357 * We group the checks below the generic code. If 'lstr' and
358 * 'rstr' are non-NULL, then we do a string based comparison.
359 * Otherwise, we compare 'lnum' and 'rnum'.
360 */
361 lstr = rstr = NULL;
4ff7a8fa 362 if (psc->sc_prop == ZPROP_USERPROP) {
34dc7c2f
BB
363 nvlist_t *luser, *ruser;
364 nvlist_t *lval, *rval;
365
366 luser = zfs_get_user_props(l);
367 ruser = zfs_get_user_props(r);
368
369 lvalid = (nvlist_lookup_nvlist(luser,
370 psc->sc_user_prop, &lval) == 0);
371 rvalid = (nvlist_lookup_nvlist(ruser,
372 psc->sc_user_prop, &rval) == 0);
373
374 if (lvalid)
375 verify(nvlist_lookup_string(lval,
376 ZPROP_VALUE, &lstr) == 0);
377 if (rvalid)
378 verify(nvlist_lookup_string(rval,
379 ZPROP_VALUE, &rstr) == 0);
0cee2406
PJD
380 } else if (psc->sc_prop == ZFS_PROP_NAME) {
381 lvalid = rvalid = B_TRUE;
382
d1d7e268
MK
383 (void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf));
384 (void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf));
34dc7c2f 385
0cee2406
PJD
386 lstr = lbuf;
387 rstr = rbuf;
34dc7c2f
BB
388 } else if (zfs_prop_is_string(psc->sc_prop)) {
389 lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
390 sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
391 rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
392 sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
393
394 lstr = lbuf;
395 rstr = rbuf;
396 } else {
397 lvalid = zfs_prop_valid_for_type(psc->sc_prop,
962d5242 398 zfs_get_type(l), B_FALSE);
34dc7c2f 399 rvalid = zfs_prop_valid_for_type(psc->sc_prop,
962d5242 400 zfs_get_type(r), B_FALSE);
34dc7c2f
BB
401
402 if (lvalid)
3a1ce491 403 lnum = zfs_prop_get_int(l, psc->sc_prop);
34dc7c2f 404 if (rvalid)
3a1ce491 405 rnum = zfs_prop_get_int(r, psc->sc_prop);
34dc7c2f
BB
406 }
407
408 if (!lvalid && !rvalid)
409 continue;
410 else if (!lvalid)
411 return (1);
412 else if (!rvalid)
413 return (-1);
414
415 if (lstr)
416 ret = strcmp(lstr, rstr);
b128c09f 417 else if (lnum < rnum)
34dc7c2f
BB
418 ret = -1;
419 else if (lnum > rnum)
420 ret = 1;
421
422 if (ret != 0) {
423 if (psc->sc_reverse == B_TRUE)
424 ret = (ret < 0) ? 1 : -1;
425 return (ret);
426 }
427 }
428
1fedd012 429 return (zfs_compare(larg, rarg));
34dc7c2f
BB
430}
431
432int
b128c09f 433zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
9babb374 434 zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
b128c09f 435 zfs_iter_f callback, void *data)
34dc7c2f 436{
9babb374 437 callback_data_t cb = {0};
34dc7c2f
BB
438 int ret = 0;
439 zfs_node_t *node;
440 uu_avl_walk_t *walk;
441
442 avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
443 offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
444
428870ff
BB
445 if (avl_pool == NULL)
446 nomem();
34dc7c2f
BB
447
448 cb.cb_sortcol = sortcol;
b128c09f 449 cb.cb_flags = flags;
34dc7c2f
BB
450 cb.cb_proplist = proplist;
451 cb.cb_types = types;
9babb374
BB
452 cb.cb_depth_limit = limit;
453 /*
45d1cae3 454 * If cb_proplist is provided then in the zfs_handles created we
9babb374
BB
455 * retain only those properties listed in cb_proplist and sortcol.
456 * The rest are pruned. So, the caller should make sure that no other
457 * properties other than those listed in cb_proplist/sortcol are
458 * accessed.
459 *
460 * If cb_proplist is NULL then we retain all the properties. We
461 * always retain the zoned property, which some other properties
462 * need (userquota & friends), and the createtxg property, which
463 * we need to sort snapshots.
464 */
465 if (cb.cb_proplist && *cb.cb_proplist) {
466 zprop_list_t *p = *cb.cb_proplist;
467
468 while (p) {
469 if (p->pl_prop >= ZFS_PROP_TYPE &&
470 p->pl_prop < ZFS_NUM_PROPS) {
471 cb.cb_props_table[p->pl_prop] = B_TRUE;
472 }
473 p = p->pl_next;
474 }
475
476 while (sortcol) {
477 if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
478 sortcol->sc_prop < ZFS_NUM_PROPS) {
479 cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
480 }
481 sortcol = sortcol->sc_next;
482 }
483
484 cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
485 cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
486 } else {
487 (void) memset(cb.cb_props_table, B_TRUE,
488 sizeof (cb.cb_props_table));
489 }
490
428870ff
BB
491 if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
492 nomem();
34dc7c2f
BB
493
494 if (argc == 0) {
495 /*
496 * If given no arguments, iterate over all datasets.
497 */
b128c09f 498 cb.cb_flags |= ZFS_ITER_RECURSE;
34dc7c2f
BB
499 ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
500 } else {
806739f9
DS
501 zfs_handle_t *zhp = NULL;
502 zfs_type_t argtype = types;
34dc7c2f
BB
503
504 /*
505 * If we're recursive, then we always allow filesystems as
e33da554 506 * arguments. If we also are interested in snapshots or
507 * bookmarks, then we can take volumes as well.
34dc7c2f 508 */
b128c09f 509 if (flags & ZFS_ITER_RECURSE) {
34dc7c2f 510 argtype |= ZFS_TYPE_FILESYSTEM;
e33da554 511 if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK))
34dc7c2f
BB
512 argtype |= ZFS_TYPE_VOLUME;
513 }
514
806739f9 515 for (int i = 0; i < argc; i++) {
b128c09f 516 if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
34dc7c2f
BB
517 zhp = zfs_path_to_zhandle(g_zfs, argv[i],
518 argtype);
519 } else {
520 zhp = zfs_open(g_zfs, argv[i], argtype);
521 }
522 if (zhp != NULL)
523 ret |= zfs_callback(zhp, &cb);
524 else
525 ret = 1;
526 }
527 }
528
529 /*
530 * At this point we've got our AVL tree full of zfs handles, so iterate
531 * over each one and execute the real user callback.
532 */
533 for (node = uu_avl_first(cb.cb_avl); node != NULL;
534 node = uu_avl_next(cb.cb_avl, node))
535 ret |= callback(node->zn_handle, data);
536
537 /*
538 * Finally, clean up the AVL tree.
539 */
428870ff
BB
540 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
541 nomem();
34dc7c2f
BB
542
543 while ((node = uu_avl_walk_next(walk)) != NULL) {
544 uu_avl_remove(cb.cb_avl, node);
545 zfs_close(node->zn_handle);
546 free(node);
547 }
548
549 uu_avl_walk_end(walk);
550 uu_avl_destroy(cb.cb_avl);
551 uu_avl_pool_destroy(avl_pool);
552
553 return (ret);
554}