1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
26 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2013 by Delphix. All rights reserved.
28 */
29
30 #include <libintl.h>
31 #include <libuutil.h>
32 #include <stddef.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.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
54 typedef struct zfs_node {
55 zfs_handle_t *zn_handle;
56 uu_avl_node_t zn_avlnode;
57 } zfs_node_t;
58
59 typedef struct callback_data {
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];
68 } callback_data_t;
69
70 uu_avl_pool_t *avl_pool;
71
72 /*
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 */
76 static boolean_t
zfs_include_snapshots(zfs_handle_t * zhp,callback_data_t * cb)77 zfs_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,
90 * add it to the avl tree and recurse over any children as necessary.
91 */
92 static int
zfs_callback(zfs_handle_t * zhp,void * data)93 zfs_callback(zfs_handle_t *zhp, void *data)
94 {
95 callback_data_t *cb = data;
96 boolean_t should_close = B_TRUE;
97 boolean_t include_snaps = zfs_include_snapshots(zhp, cb);
98 boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK);
99
100 if ((zfs_get_type(zhp) & cb->cb_types) ||
101 ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
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) {
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
115 if (zfs_expand_proplist(zhp, cb->cb_proplist,
116 (cb->cb_flags & ZFS_ITER_RECVD_PROPS),
117 (cb->cb_flags & ZFS_ITER_LITERAL_PROPS))
118 != 0) {
119 free(node);
120 return (-1);
121 }
122 }
123 uu_avl_insert(cb->cb_avl, node, idx);
124 should_close = B_FALSE;
125 } else {
126 free(node);
127 }
128 }
129
130 /*
131 * Recurse if necessary.
132 */
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++;
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) {
147 (void) zfs_iter_filesystems_v2(zhp, cb->cb_flags,
148 zfs_callback, data);
149 }
150
151 if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
152 ZFS_TYPE_BOOKMARK)) == 0) && include_snaps) {
153 (void) zfs_iter_snapshots_v2(zhp, cb->cb_flags,
154 zfs_callback, data, 0, 0);
155 }
156
157 if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
158 ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks) {
159 (void) zfs_iter_bookmarks_v2(zhp, cb->cb_flags,
160 zfs_callback, data);
161 }
162
163 cb->cb_depth--;
164 }
165
166 if (should_close)
167 zfs_close(zhp);
168
169 return (0);
170 }
171
172 int
zfs_add_sort_column(zfs_sort_column_t ** sc,const char * name,boolean_t reverse)173 zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
174 boolean_t reverse)
175 {
176 zfs_sort_column_t *col;
177 zfs_prop_t prop;
178
179 if ((prop = zfs_name_to_prop(name)) == ZPROP_USERPROP &&
180 !zfs_prop_user(name))
181 return (-1);
182
183 col = safe_malloc(sizeof (zfs_sort_column_t));
184
185 col->sc_prop = prop;
186 col->sc_reverse = reverse;
187 if (prop == ZPROP_USERPROP) {
188 col->sc_user_prop = safe_malloc(strlen(name) + 1);
189 (void) strcpy(col->sc_user_prop, name);
190 }
191
192 if (*sc == NULL) {
193 col->sc_last = col;
194 *sc = col;
195 } else {
196 (*sc)->sc_last->sc_next = col;
197 (*sc)->sc_last = col;
198 }
199
200 return (0);
201 }
202
203 void
zfs_free_sort_columns(zfs_sort_column_t * sc)204 zfs_free_sort_columns(zfs_sort_column_t *sc)
205 {
206 zfs_sort_column_t *col;
207
208 while (sc != NULL) {
209 col = sc->sc_next;
210 free(sc->sc_user_prop);
211 free(sc);
212 sc = col;
213 }
214 }
215
216 /*
217 * Return true if all of the properties to be sorted are populated by
218 * dsl_dataset_fast_stat(). Note that sc == NULL (no sort) means we
219 * don't need any extra properties, so returns true.
220 */
221 boolean_t
zfs_sort_only_by_fast(const zfs_sort_column_t * sc)222 zfs_sort_only_by_fast(const zfs_sort_column_t *sc)
223 {
224 while (sc != NULL) {
225 switch (sc->sc_prop) {
226 case ZFS_PROP_NAME:
227 case ZFS_PROP_GUID:
228 case ZFS_PROP_CREATETXG:
229 case ZFS_PROP_NUMCLONES:
230 case ZFS_PROP_INCONSISTENT:
231 case ZFS_PROP_REDACTED:
232 case ZFS_PROP_ORIGIN:
233 break;
234 default:
235 return (B_FALSE);
236 }
237 sc = sc->sc_next;
238 }
239
240 return (B_TRUE);
241 }
242
243 boolean_t
zfs_list_only_by_fast(const zprop_list_t * p)244 zfs_list_only_by_fast(const zprop_list_t *p)
245 {
246 if (p == NULL) {
247 /* NULL means 'all' so we can't use simple mode */
248 return (B_FALSE);
249 }
250
251 while (p != NULL) {
252 switch (p->pl_prop) {
253 case ZFS_PROP_NAME:
254 case ZFS_PROP_GUID:
255 case ZFS_PROP_CREATETXG:
256 case ZFS_PROP_NUMCLONES:
257 case ZFS_PROP_INCONSISTENT:
258 case ZFS_PROP_REDACTED:
259 case ZFS_PROP_ORIGIN:
260 break;
261 default:
262 return (B_FALSE);
263 }
264 p = p->pl_next;
265 }
266
267 return (B_TRUE);
268 }
269
270 static int
zfs_compare(const void * larg,const void * rarg)271 zfs_compare(const void *larg, const void *rarg)
272 {
273 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
274 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
275 const char *lname = zfs_get_name(l);
276 const char *rname = zfs_get_name(r);
277 char *lat, *rat;
278 uint64_t lcreate, rcreate;
279 int ret;
280
281 lat = (char *)strchr(lname, '@');
282 rat = (char *)strchr(rname, '@');
283
284 if (lat != NULL)
285 *lat = '\0';
286 if (rat != NULL)
287 *rat = '\0';
288
289 ret = strcmp(lname, rname);
290 if (ret == 0 && (lat != NULL || rat != NULL)) {
291 /*
292 * If we're comparing a dataset to one of its snapshots, we
293 * always make the full dataset first.
294 */
295 if (lat == NULL) {
296 ret = -1;
297 } else if (rat == NULL) {
298 ret = 1;
299 } else {
300 /*
301 * If we have two snapshots from the same dataset, then
302 * we want to sort them according to creation time. We
303 * use the hidden CREATETXG property to get an absolute
304 * ordering of snapshots.
305 */
306 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
307 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
308
309 /*
310 * Both lcreate and rcreate being 0 means we don't have
311 * properties and we should compare full name.
312 */
313 if (lcreate == 0 && rcreate == 0)
314 ret = strcmp(lat + 1, rat + 1);
315 else if (lcreate < rcreate)
316 ret = -1;
317 else if (lcreate > rcreate)
318 ret = 1;
319 }
320 }
321
322 if (lat != NULL)
323 *lat = '@';
324 if (rat != NULL)
325 *rat = '@';
326
327 return (ret);
328 }
329
330 /*
331 * Sort datasets by specified columns.
332 *
333 * o Numeric types sort in ascending order.
334 * o String types sort in alphabetical order.
335 * o Types inappropriate for a row sort that row to the literal
336 * bottom, regardless of the specified ordering.
337 *
338 * If no sort columns are specified, or two datasets compare equally
339 * across all specified columns, they are sorted alphabetically by name
340 * with snapshots grouped under their parents.
341 */
342 static int
zfs_sort(const void * larg,const void * rarg,void * data)343 zfs_sort(const void *larg, const void *rarg, void *data)
344 {
345 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
346 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
347 zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
348 zfs_sort_column_t *psc;
349
350 for (psc = sc; psc != NULL; psc = psc->sc_next) {
351 char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
352 const char *lstr, *rstr;
353 uint64_t lnum = 0, rnum = 0;
354 boolean_t lvalid, rvalid;
355 int ret = 0;
356
357 /*
358 * We group the checks below the generic code. If 'lstr' and
359 * 'rstr' are non-NULL, then we do a string based comparison.
360 * Otherwise, we compare 'lnum' and 'rnum'.
361 */
362 lstr = rstr = NULL;
363 if (psc->sc_prop == ZPROP_USERPROP) {
364 nvlist_t *luser, *ruser;
365 nvlist_t *lval, *rval;
366
367 luser = zfs_get_user_props(l);
368 ruser = zfs_get_user_props(r);
369
370 lvalid = (nvlist_lookup_nvlist(luser,
371 psc->sc_user_prop, &lval) == 0);
372 rvalid = (nvlist_lookup_nvlist(ruser,
373 psc->sc_user_prop, &rval) == 0);
374
375 if (lvalid)
376 verify(nvlist_lookup_string(lval,
377 ZPROP_VALUE, &lstr) == 0);
378 if (rvalid)
379 verify(nvlist_lookup_string(rval,
380 ZPROP_VALUE, &rstr) == 0);
381 } else if (psc->sc_prop == ZFS_PROP_NAME) {
382 lvalid = rvalid = B_TRUE;
383
384 (void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf));
385 (void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf));
386
387 lstr = lbuf;
388 rstr = rbuf;
389 } else if (zfs_prop_is_string(psc->sc_prop)) {
390 lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
391 sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
392 rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
393 sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
394
395 lstr = lbuf;
396 rstr = rbuf;
397 } else {
398 lvalid = zfs_prop_valid_for_type(psc->sc_prop,
399 zfs_get_type(l), B_FALSE);
400 rvalid = zfs_prop_valid_for_type(psc->sc_prop,
401 zfs_get_type(r), B_FALSE);
402
403 if (lvalid)
404 lnum = zfs_prop_get_int(l, psc->sc_prop);
405 if (rvalid)
406 rnum = zfs_prop_get_int(r, psc->sc_prop);
407 }
408
409 if (!lvalid && !rvalid)
410 continue;
411 else if (!lvalid)
412 return (1);
413 else if (!rvalid)
414 return (-1);
415
416 if (lstr)
417 ret = strcmp(lstr, rstr);
418 else if (lnum < rnum)
419 ret = -1;
420 else if (lnum > rnum)
421 ret = 1;
422
423 if (ret != 0) {
424 if (psc->sc_reverse == B_TRUE)
425 ret = (ret < 0) ? 1 : -1;
426 return (ret);
427 }
428 }
429
430 return (zfs_compare(larg, rarg));
431 }
432
433 int
zfs_for_each(int argc,char ** argv,int flags,zfs_type_t types,zfs_sort_column_t * sortcol,zprop_list_t ** proplist,int limit,zfs_iter_f callback,void * data)434 zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
435 zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
436 zfs_iter_f callback, void *data)
437 {
438 callback_data_t cb = {0};
439 int ret = 0;
440 zfs_node_t *node;
441 uu_avl_walk_t *walk;
442
443 avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
444 offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
445
446 if (avl_pool == NULL)
447 nomem();
448
449 cb.cb_sortcol = sortcol;
450 cb.cb_flags = flags;
451 cb.cb_proplist = proplist;
452 cb.cb_types = types;
453 cb.cb_depth_limit = limit;
454 /*
455 * If cb_proplist is provided then in the zfs_handles created we
456 * retain only those properties listed in cb_proplist and sortcol.
457 * The rest are pruned. So, the caller should make sure that no other
458 * properties other than those listed in cb_proplist/sortcol are
459 * accessed.
460 *
461 * If cb_proplist is NULL then we retain all the properties. We
462 * always retain the zoned property, which some other properties
463 * need (userquota & friends), and the createtxg property, which
464 * we need to sort snapshots.
465 */
466 if (cb.cb_proplist && *cb.cb_proplist) {
467 zprop_list_t *p = *cb.cb_proplist;
468
469 while (p) {
470 if (p->pl_prop >= ZFS_PROP_TYPE &&
471 p->pl_prop < ZFS_NUM_PROPS) {
472 cb.cb_props_table[p->pl_prop] = B_TRUE;
473 }
474 p = p->pl_next;
475 }
476
477 while (sortcol) {
478 if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
479 sortcol->sc_prop < ZFS_NUM_PROPS) {
480 cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
481 }
482 sortcol = sortcol->sc_next;
483 }
484
485 cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
486 cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
487 } else {
488 (void) memset(cb.cb_props_table, B_TRUE,
489 sizeof (cb.cb_props_table));
490 }
491
492 if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL)
493 nomem();
494
495 if (argc == 0) {
496 /*
497 * If given no arguments, iterate over all datasets.
498 */
499 cb.cb_flags |= ZFS_ITER_RECURSE;
500 ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
501 } else {
502 zfs_handle_t *zhp = NULL;
503 zfs_type_t argtype = types;
504
505 /*
506 * If we're recursive, then we always allow filesystems as
507 * arguments. If we also are interested in snapshots or
508 * bookmarks, then we can take volumes as well.
509 */
510 if (flags & ZFS_ITER_RECURSE) {
511 argtype |= ZFS_TYPE_FILESYSTEM;
512 if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK))
513 argtype |= ZFS_TYPE_VOLUME;
514 }
515
516 for (int i = 0; i < argc; i++) {
517 if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
518 zhp = zfs_path_to_zhandle(g_zfs, argv[i],
519 argtype);
520 } else {
521 zhp = zfs_open(g_zfs, argv[i], argtype);
522 }
523 if (zhp != NULL)
524 ret |= zfs_callback(zhp, &cb);
525 else
526 ret = 1;
527 }
528 }
529
530 /*
531 * At this point we've got our AVL tree full of zfs handles, so iterate
532 * over each one and execute the real user callback.
533 */
534 for (node = uu_avl_first(cb.cb_avl); node != NULL;
535 node = uu_avl_next(cb.cb_avl, node))
536 ret |= callback(node->zn_handle, data);
537
538 /*
539 * Finally, clean up the AVL tree.
540 */
541 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL)
542 nomem();
543
544 while ((node = uu_avl_walk_next(walk)) != NULL) {
545 uu_avl_remove(cb.cb_avl, node);
546 zfs_close(node->zn_handle);
547 free(node);
548 }
549
550 uu_avl_walk_end(walk);
551 uu_avl_destroy(cb.cb_avl);
552 uu_avl_pool_destroy(avl_pool);
553
554 return (ret);
555 }
556