Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/cmd/zfs/zfs_iter.c
108610 views
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 <[email protected]>.
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 <stddef.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.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
typedef struct callback_data callback_data_t;
53
54
typedef struct zfs_node {
55
zfs_handle_t *zn_handle;
56
callback_data_t *zn_callback;
57
avl_node_t zn_avlnode;
58
} zfs_node_t;
59
60
struct callback_data {
61
avl_tree_t cb_avl;
62
int cb_flags;
63
zfs_type_t cb_types;
64
zfs_sort_column_t *cb_sortcol;
65
zprop_list_t **cb_proplist;
66
int cb_depth_limit;
67
int cb_depth;
68
uint8_t cb_props_table[ZFS_NUM_PROPS];
69
};
70
71
/*
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
*/
75
static boolean_t
76
zfs_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,
89
* add it to the avl tree and recurse over any children as necessary.
90
*/
91
static int
92
zfs_callback(zfs_handle_t *zhp, void *data)
93
{
94
callback_data_t *cb = data;
95
boolean_t should_close = B_TRUE;
96
boolean_t include_snaps = zfs_include_snapshots(zhp, cb);
97
boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK);
98
99
if ((zfs_get_type(zhp) & cb->cb_types) ||
100
((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) {
101
avl_index_t idx;
102
zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
103
104
node->zn_handle = zhp;
105
node->zn_callback = cb;
106
if (avl_find(&cb->cb_avl, node, &idx) == NULL) {
107
if (cb->cb_proplist) {
108
if ((*cb->cb_proplist) &&
109
!(*cb->cb_proplist)->pl_all)
110
zfs_prune_proplist(zhp,
111
cb->cb_props_table);
112
113
if (zfs_expand_proplist(zhp, cb->cb_proplist,
114
(cb->cb_flags & ZFS_ITER_RECVD_PROPS),
115
(cb->cb_flags & ZFS_ITER_LITERAL_PROPS))
116
!= 0) {
117
free(node);
118
return (-1);
119
}
120
}
121
avl_insert(&cb->cb_avl, node, idx);
122
should_close = B_FALSE;
123
} else {
124
free(node);
125
}
126
}
127
128
/*
129
* Recurse if necessary.
130
*/
131
if (cb->cb_flags & ZFS_ITER_RECURSE &&
132
((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
133
cb->cb_depth < cb->cb_depth_limit)) {
134
cb->cb_depth++;
135
136
/*
137
* If we are not looking for filesystems, we don't need to
138
* recurse into filesystems when we are at our depth limit.
139
*/
140
if ((cb->cb_depth < cb->cb_depth_limit ||
141
(cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 ||
142
(cb->cb_types &
143
(ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) &&
144
zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) {
145
(void) zfs_iter_filesystems_v2(zhp, cb->cb_flags,
146
zfs_callback, data);
147
}
148
149
if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
150
ZFS_TYPE_BOOKMARK)) == 0) && include_snaps) {
151
(void) zfs_iter_snapshots_v2(zhp, cb->cb_flags,
152
zfs_callback, data, 0, 0);
153
}
154
155
if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT |
156
ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks) {
157
(void) zfs_iter_bookmarks_v2(zhp, cb->cb_flags,
158
zfs_callback, data);
159
}
160
161
cb->cb_depth--;
162
}
163
164
if (should_close)
165
zfs_close(zhp);
166
167
return (0);
168
}
169
170
int
171
zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
172
boolean_t reverse)
173
{
174
zfs_sort_column_t *col;
175
zfs_prop_t prop;
176
177
if ((prop = zfs_name_to_prop(name)) == ZPROP_USERPROP &&
178
!zfs_prop_user(name))
179
return (-1);
180
181
col = safe_malloc(sizeof (zfs_sort_column_t));
182
183
col->sc_prop = prop;
184
col->sc_reverse = reverse;
185
if (prop == ZPROP_USERPROP) {
186
col->sc_user_prop = safe_malloc(strlen(name) + 1);
187
(void) strcpy(col->sc_user_prop, name);
188
}
189
190
if (*sc == NULL) {
191
col->sc_last = col;
192
*sc = col;
193
} else {
194
(*sc)->sc_last->sc_next = col;
195
(*sc)->sc_last = col;
196
}
197
198
return (0);
199
}
200
201
void
202
zfs_free_sort_columns(zfs_sort_column_t *sc)
203
{
204
zfs_sort_column_t *col;
205
206
while (sc != NULL) {
207
col = sc->sc_next;
208
free(sc->sc_user_prop);
209
free(sc);
210
sc = col;
211
}
212
}
213
214
/*
215
* Return true if all of the properties to be sorted are populated by
216
* dsl_dataset_fast_stat(). Note that sc == NULL (no sort) means we
217
* don't need any extra properties, so returns true.
218
*/
219
boolean_t
220
zfs_sort_only_by_fast(const zfs_sort_column_t *sc)
221
{
222
while (sc != NULL) {
223
switch (sc->sc_prop) {
224
case ZFS_PROP_NAME:
225
case ZFS_PROP_GUID:
226
case ZFS_PROP_CREATETXG:
227
case ZFS_PROP_NUMCLONES:
228
case ZFS_PROP_INCONSISTENT:
229
case ZFS_PROP_REDACTED:
230
case ZFS_PROP_ORIGIN:
231
break;
232
default:
233
return (B_FALSE);
234
}
235
sc = sc->sc_next;
236
}
237
238
return (B_TRUE);
239
}
240
241
boolean_t
242
zfs_list_only_by_fast(const zprop_list_t *p)
243
{
244
if (p == NULL) {
245
/* NULL means 'all' so we can't use simple mode */
246
return (B_FALSE);
247
}
248
249
while (p != NULL) {
250
switch (p->pl_prop) {
251
case ZFS_PROP_NAME:
252
case ZFS_PROP_GUID:
253
case ZFS_PROP_CREATETXG:
254
case ZFS_PROP_NUMCLONES:
255
case ZFS_PROP_INCONSISTENT:
256
case ZFS_PROP_REDACTED:
257
case ZFS_PROP_ORIGIN:
258
break;
259
default:
260
return (B_FALSE);
261
}
262
p = p->pl_next;
263
}
264
265
return (B_TRUE);
266
}
267
268
static int
269
zfs_compare(const void *larg, const void *rarg)
270
{
271
zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
272
zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
273
const char *lname = zfs_get_name(l);
274
const char *rname = zfs_get_name(r);
275
char *lat, *rat;
276
uint64_t lcreate, rcreate;
277
int ret;
278
279
lat = (char *)strchr(lname, '@');
280
rat = (char *)strchr(rname, '@');
281
282
if (lat != NULL)
283
*lat = '\0';
284
if (rat != NULL)
285
*rat = '\0';
286
287
ret = TREE_ISIGN(strcmp(lname, rname));
288
if (ret == 0 && (lat != NULL || rat != NULL)) {
289
/*
290
* If we're comparing a dataset to one of its snapshots, we
291
* always make the full dataset first.
292
*/
293
if (lat == NULL) {
294
ret = -1;
295
} else if (rat == NULL) {
296
ret = 1;
297
} else {
298
/*
299
* If we have two snapshots from the same dataset, then
300
* we want to sort them according to creation time. We
301
* use the hidden CREATETXG property to get an absolute
302
* ordering of snapshots.
303
*/
304
lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
305
rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
306
307
/*
308
* Both lcreate and rcreate being 0 means we don't have
309
* properties and we should compare full name.
310
*/
311
if (lcreate == 0 && rcreate == 0)
312
ret = strcmp(lat + 1, rat + 1);
313
else if (lcreate < rcreate)
314
ret = -1;
315
else if (lcreate > rcreate)
316
ret = 1;
317
}
318
}
319
320
if (lat != NULL)
321
*lat = '@';
322
if (rat != NULL)
323
*rat = '@';
324
325
return (ret);
326
}
327
328
/*
329
* Sort datasets by specified columns.
330
*
331
* o Numeric types sort in ascending order.
332
* o String types sort in alphabetical order.
333
* o Types inappropriate for a row sort that row to the literal
334
* bottom, regardless of the specified ordering.
335
*
336
* If no sort columns are specified, or two datasets compare equally
337
* across all specified columns, they are sorted alphabetically by name
338
* with snapshots grouped under their parents.
339
*/
340
static int
341
zfs_sort(const void *larg, const void *rarg)
342
{
343
zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
344
zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
345
zfs_sort_column_t *sc = ((zfs_node_t *)larg)->zn_callback->cb_sortcol;
346
zfs_sort_column_t *psc;
347
348
for (psc = sc; psc != NULL; psc = psc->sc_next) {
349
char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
350
const char *lstr, *rstr;
351
uint64_t lnum = 0, rnum = 0;
352
boolean_t lvalid, rvalid;
353
int ret = 0;
354
355
/*
356
* We group the checks below the generic code. If 'lstr' and
357
* 'rstr' are non-NULL, then we do a string based comparison.
358
* Otherwise, we compare 'lnum' and 'rnum'.
359
*/
360
lstr = rstr = NULL;
361
if (psc->sc_prop == ZPROP_USERPROP) {
362
nvlist_t *luser, *ruser;
363
nvlist_t *lval, *rval;
364
365
luser = zfs_get_user_props(l);
366
ruser = zfs_get_user_props(r);
367
368
lvalid = (nvlist_lookup_nvlist(luser,
369
psc->sc_user_prop, &lval) == 0);
370
rvalid = (nvlist_lookup_nvlist(ruser,
371
psc->sc_user_prop, &rval) == 0);
372
373
if (lvalid)
374
verify(nvlist_lookup_string(lval,
375
ZPROP_VALUE, &lstr) == 0);
376
if (rvalid)
377
verify(nvlist_lookup_string(rval,
378
ZPROP_VALUE, &rstr) == 0);
379
} else if (psc->sc_prop == ZFS_PROP_NAME) {
380
lvalid = rvalid = B_TRUE;
381
382
(void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf));
383
(void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf));
384
385
lstr = lbuf;
386
rstr = rbuf;
387
} else if (zfs_prop_is_string(psc->sc_prop)) {
388
lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
389
sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
390
rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
391
sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
392
393
lstr = lbuf;
394
rstr = rbuf;
395
} else {
396
lvalid = zfs_prop_valid_for_type(psc->sc_prop,
397
zfs_get_type(l), B_FALSE);
398
rvalid = zfs_prop_valid_for_type(psc->sc_prop,
399
zfs_get_type(r), B_FALSE);
400
401
if (lvalid)
402
lnum = zfs_prop_get_int(l, psc->sc_prop);
403
if (rvalid)
404
rnum = zfs_prop_get_int(r, psc->sc_prop);
405
}
406
407
if (!lvalid && !rvalid)
408
continue;
409
else if (!lvalid)
410
return (1);
411
else if (!rvalid)
412
return (-1);
413
414
if (lstr)
415
ret = TREE_ISIGN(strcmp(lstr, rstr));
416
else if (lnum < rnum)
417
ret = -1;
418
else if (lnum > rnum)
419
ret = 1;
420
421
if (ret != 0) {
422
if (psc->sc_reverse == B_TRUE)
423
ret = (ret < 0) ? 1 : -1;
424
return (ret);
425
}
426
}
427
428
return (zfs_compare(larg, rarg));
429
}
430
431
int
432
zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
433
zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit,
434
zfs_iter_f callback, void *data)
435
{
436
callback_data_t cb = {0};
437
int ret = 0;
438
zfs_node_t *node;
439
440
cb.cb_sortcol = sortcol;
441
cb.cb_flags = flags;
442
cb.cb_proplist = proplist;
443
cb.cb_types = types;
444
cb.cb_depth_limit = limit;
445
/*
446
* If cb_proplist is provided then in the zfs_handles created we
447
* retain only those properties listed in cb_proplist and sortcol.
448
* The rest are pruned. So, the caller should make sure that no other
449
* properties other than those listed in cb_proplist/sortcol are
450
* accessed.
451
*
452
* If cb_proplist is NULL then we retain all the properties. We
453
* always retain the zoned property, which some other properties
454
* need (userquota & friends), and the createtxg property, which
455
* we need to sort snapshots.
456
*/
457
if (cb.cb_proplist && *cb.cb_proplist) {
458
zprop_list_t *p = *cb.cb_proplist;
459
460
while (p) {
461
if (p->pl_prop >= ZFS_PROP_TYPE &&
462
p->pl_prop < ZFS_NUM_PROPS) {
463
cb.cb_props_table[p->pl_prop] = B_TRUE;
464
}
465
p = p->pl_next;
466
}
467
468
while (sortcol) {
469
if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
470
sortcol->sc_prop < ZFS_NUM_PROPS) {
471
cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
472
}
473
sortcol = sortcol->sc_next;
474
}
475
476
cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE;
477
cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE;
478
} else {
479
(void) memset(cb.cb_props_table, B_TRUE,
480
sizeof (cb.cb_props_table));
481
}
482
483
avl_create(&cb.cb_avl, zfs_sort,
484
sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
485
486
if (argc == 0) {
487
/*
488
* If given no arguments, iterate over all datasets.
489
*/
490
cb.cb_flags |= ZFS_ITER_RECURSE;
491
ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
492
} else {
493
zfs_handle_t *zhp = NULL;
494
zfs_type_t argtype = types;
495
496
/*
497
* If we're recursive, then we always allow filesystems as
498
* arguments. If we also are interested in snapshots or
499
* bookmarks, then we can take volumes as well.
500
*/
501
if (flags & ZFS_ITER_RECURSE) {
502
argtype |= ZFS_TYPE_FILESYSTEM;
503
if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK))
504
argtype |= ZFS_TYPE_VOLUME;
505
}
506
507
for (int i = 0; i < argc; i++) {
508
if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) {
509
zhp = zfs_path_to_zhandle(g_zfs, argv[i],
510
argtype);
511
} else {
512
zhp = zfs_open(g_zfs, argv[i], argtype);
513
}
514
if (zhp != NULL)
515
ret |= zfs_callback(zhp, &cb);
516
else
517
ret = 1;
518
}
519
}
520
521
/*
522
* At this point we've got our AVL tree full of zfs handles, so iterate
523
* over each one and execute the real user callback.
524
*/
525
for (node = avl_first(&cb.cb_avl); node != NULL;
526
node = AVL_NEXT(&cb.cb_avl, node))
527
ret |= callback(node->zn_handle, data);
528
529
/*
530
* Finally, clean up the AVL tree.
531
*/
532
void *cookie = NULL;
533
while ((node = avl_destroy_nodes(&cb.cb_avl, &cookie)) != NULL) {
534
zfs_close(node->zn_handle);
535
free(node);
536
}
537
538
avl_destroy(&cb.cb_avl);
539
540
return (ret);
541
}
542
543