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