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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2019 Datto Inc.
37 #include <sys/mntent.h>
39 #include "libzfs_impl.h"
42 zfs_iter_clones(zfs_handle_t
*zhp
, int flags __maybe_unused
, zfs_iter_f func
,
45 nvlist_t
*nvl
= zfs_get_clones_nvl(zhp
);
51 for (pair
= nvlist_next_nvpair(nvl
, NULL
); pair
!= NULL
;
52 pair
= nvlist_next_nvpair(nvl
, pair
)) {
53 zfs_handle_t
*clone
= zfs_open(zhp
->zfs_hdl
, nvpair_name(pair
),
54 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
56 int err
= func(clone
, data
);
65 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
70 orig_cookie
= zc
->zc_cookie
;
72 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
73 zc
->zc_objset_stats
.dds_creation_txg
= 0;
74 rc
= zfs_ioctl(zhp
->zfs_hdl
, arg
, zc
);
79 /* expand nvlist memory and try again */
80 zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
);
81 zc
->zc_cookie
= orig_cookie
;
84 * An errno value of ESRCH indicates normal completion.
85 * If ENOENT is returned, then the underlying dataset
86 * has been removed since we obtained the handle.
93 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
95 "cannot iterate filesystems"));
103 * Iterate over all child filesystems
106 zfs_iter_filesystems(zfs_handle_t
*zhp
, int flags
, zfs_iter_f func
, void *data
)
108 zfs_cmd_t zc
= {"\0"};
112 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
115 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0);
117 if ((flags
& ZFS_ITER_SIMPLE
) == ZFS_ITER_SIMPLE
)
118 zc
.zc_simple
= B_TRUE
;
120 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
123 nzhp
= make_dataset_simple_handle_zc(zhp
, &zc
);
125 nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
, &zc
);
127 * Silently ignore errors, as the only plausible explanation is
128 * that the pool has since been removed.
133 if ((ret
= func(nzhp
, data
)) != 0) {
134 zcmd_free_nvlists(&zc
);
138 zcmd_free_nvlists(&zc
);
139 return ((ret
< 0) ? ret
: 0);
143 * Iterate over all snapshots
146 zfs_iter_snapshots(zfs_handle_t
*zhp
, int flags
, zfs_iter_f func
,
147 void *data
, uint64_t min_txg
, uint64_t max_txg
)
149 zfs_cmd_t zc
= {"\0"};
152 nvlist_t
*range_nvl
= NULL
;
154 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
||
155 zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
)
158 zc
.zc_simple
= (flags
& ZFS_ITER_SIMPLE
) != 0;
160 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0);
163 range_nvl
= fnvlist_alloc();
164 fnvlist_add_uint64(range_nvl
, SNAP_ITER_MIN_TXG
, min_txg
);
167 if (range_nvl
== NULL
)
168 range_nvl
= fnvlist_alloc();
169 fnvlist_add_uint64(range_nvl
, SNAP_ITER_MAX_TXG
, max_txg
);
172 if (range_nvl
!= NULL
)
173 zcmd_write_src_nvlist(zhp
->zfs_hdl
, &zc
, range_nvl
);
175 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
179 nzhp
= make_dataset_simple_handle_zc(zhp
, &zc
);
181 nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
, &zc
);
185 if ((ret
= func(nzhp
, data
)) != 0) {
186 zcmd_free_nvlists(&zc
);
187 fnvlist_free(range_nvl
);
191 zcmd_free_nvlists(&zc
);
192 fnvlist_free(range_nvl
);
193 return ((ret
< 0) ? ret
: 0);
197 * Iterate over all bookmarks
200 zfs_iter_bookmarks(zfs_handle_t
*zhp
, int flags __maybe_unused
,
201 zfs_iter_f func
, void *data
)
204 nvlist_t
*props
= NULL
;
205 nvlist_t
*bmarks
= NULL
;
209 if ((zfs_get_type(zhp
) & (ZFS_TYPE_SNAPSHOT
| ZFS_TYPE_BOOKMARK
)) != 0)
212 /* Setup the requested properties nvlist. */
213 props
= fnvlist_alloc();
214 for (zfs_prop_t p
= 0; p
< ZFS_NUM_PROPS
; p
++) {
215 if (zfs_prop_valid_for_type(p
, ZFS_TYPE_BOOKMARK
, B_FALSE
)) {
216 fnvlist_add_boolean(props
, zfs_prop_to_name(p
));
219 fnvlist_add_boolean(props
, "redact_complete");
221 if ((err
= lzc_get_bookmarks(zhp
->zfs_name
, props
, &bmarks
)) != 0)
224 for (pair
= nvlist_next_nvpair(bmarks
, NULL
);
225 pair
!= NULL
; pair
= nvlist_next_nvpair(bmarks
, pair
)) {
226 char name
[ZFS_MAX_DATASET_NAME_LEN
];
228 nvlist_t
*bmark_props
;
230 bmark_name
= nvpair_name(pair
);
231 bmark_props
= fnvpair_value_nvlist(pair
);
233 if (snprintf(name
, sizeof (name
), "%s#%s", zhp
->zfs_name
,
234 bmark_name
) >= sizeof (name
)) {
239 nzhp
= make_bookmark_handle(zhp
, name
, bmark_props
);
243 if ((err
= func(nzhp
, data
)) != 0)
249 fnvlist_free(bmarks
);
255 * Routines for dealing with the sorted snapshot functionality
257 typedef struct zfs_node
{
258 zfs_handle_t
*zn_handle
;
259 avl_node_t zn_avlnode
;
263 zfs_sort_snaps(zfs_handle_t
*zhp
, void *data
)
265 avl_tree_t
*avl
= data
;
269 search
.zn_handle
= zhp
;
270 node
= avl_find(avl
, &search
, NULL
);
273 * If this snapshot was renamed while we were creating the
274 * AVL tree, it's possible that we already inserted it under
275 * its old name. Remove the old handle before adding the new
278 zfs_close(node
->zn_handle
);
279 avl_remove(avl
, node
);
283 node
= zfs_alloc(zhp
->zfs_hdl
, sizeof (zfs_node_t
));
284 node
->zn_handle
= zhp
;
291 zfs_snapshot_compare(const void *larg
, const void *rarg
)
293 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
294 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
295 uint64_t lcreate
, rcreate
;
298 * Sort them according to creation time. We use the hidden
299 * CREATETXG property to get an absolute ordering of snapshots.
301 lcreate
= zfs_prop_get_int(l
, ZFS_PROP_CREATETXG
);
302 rcreate
= zfs_prop_get_int(r
, ZFS_PROP_CREATETXG
);
304 return (TREE_CMP(lcreate
, rcreate
));
308 zfs_iter_snapshots_sorted(zfs_handle_t
*zhp
, int flags
, zfs_iter_f callback
,
309 void *data
, uint64_t min_txg
, uint64_t max_txg
)
316 avl_create(&avl
, zfs_snapshot_compare
,
317 sizeof (zfs_node_t
), offsetof(zfs_node_t
, zn_avlnode
));
319 ret
= zfs_iter_snapshots(zhp
, flags
, zfs_sort_snaps
, &avl
, min_txg
,
322 for (node
= avl_first(&avl
); node
!= NULL
; node
= AVL_NEXT(&avl
, node
))
323 ret
|= callback(node
->zn_handle
, data
);
325 while ((node
= avl_destroy_nodes(&avl
, &cookie
)) != NULL
)
336 boolean_t ssa_seenfirst
;
337 boolean_t ssa_seenlast
;
343 snapspec_cb(zfs_handle_t
*zhp
, void *arg
)
345 snapspec_arg_t
*ssa
= arg
;
346 const char *shortsnapname
;
349 if (ssa
->ssa_seenlast
)
352 shortsnapname
= strchr(zfs_get_name(zhp
), '@') + 1;
353 if (!ssa
->ssa_seenfirst
&& strcmp(shortsnapname
, ssa
->ssa_first
) == 0)
354 ssa
->ssa_seenfirst
= B_TRUE
;
355 if (strcmp(shortsnapname
, ssa
->ssa_last
) == 0)
356 ssa
->ssa_seenlast
= B_TRUE
;
358 if (ssa
->ssa_seenfirst
) {
359 err
= ssa
->ssa_func(zhp
, ssa
->ssa_arg
);
368 * spec is a string like "A,B%C,D"
370 * <snaps>, where <snaps> can be:
371 * <snap> (single snapshot)
372 * <snap>%<snap> (range of snapshots, inclusive)
373 * %<snap> (range of snapshots, starting with earliest)
374 * <snap>% (range of snapshots, ending with last)
376 * <snaps>[,...] (comma separated list of the above)
378 * If a snapshot can not be opened, continue trying to open the others, but
379 * return ENOENT at the end.
382 zfs_iter_snapspec(zfs_handle_t
*fs_zhp
, int flags
, const char *spec_orig
,
383 zfs_iter_f func
, void *arg
)
385 char *buf
, *comma_separated
, *cp
;
389 buf
= zfs_strdup(fs_zhp
->zfs_hdl
, spec_orig
);
392 while ((comma_separated
= strsep(&cp
, ",")) != NULL
) {
393 char *pct
= strchr(comma_separated
, '%');
395 snapspec_arg_t ssa
= { 0 };
399 if (pct
== comma_separated
)
400 ssa
.ssa_seenfirst
= B_TRUE
;
402 ssa
.ssa_first
= comma_separated
;
404 ssa
.ssa_last
= pct
+ 1;
407 * If there is a lastname specified, make sure it
410 if (ssa
.ssa_last
[0] != '\0') {
411 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
412 (void) snprintf(snapname
, sizeof (snapname
),
413 "%s@%s", zfs_get_name(fs_zhp
),
415 if (!zfs_dataset_exists(fs_zhp
->zfs_hdl
,
416 snapname
, ZFS_TYPE_SNAPSHOT
)) {
422 err
= zfs_iter_snapshots_sorted(fs_zhp
, flags
,
423 snapspec_cb
, &ssa
, 0, 0);
426 if (ret
== 0 && (!ssa
.ssa_seenfirst
||
427 (ssa
.ssa_last
[0] != '\0' && !ssa
.ssa_seenlast
))) {
431 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
432 zfs_handle_t
*snap_zhp
;
433 (void) snprintf(snapname
, sizeof (snapname
), "%s@%s",
434 zfs_get_name(fs_zhp
), comma_separated
);
435 snap_zhp
= make_dataset_handle(fs_zhp
->zfs_hdl
,
437 if (snap_zhp
== NULL
) {
441 err
= func(snap_zhp
, arg
);
452 * Iterate over all children, snapshots and filesystems
453 * Process snapshots before filesystems because they are nearer the input
454 * handle: this is extremely important when used with zfs_iter_f functions
455 * looking for data, following the logic that we would like to find it as soon
456 * and as close as possible.
459 zfs_iter_children(zfs_handle_t
*zhp
, int flags
, zfs_iter_f func
, void *data
)
463 if ((ret
= zfs_iter_snapshots(zhp
, flags
, func
, data
, 0, 0)) != 0)
466 return (zfs_iter_filesystems(zhp
, flags
, func
, data
));
470 typedef struct iter_stack_frame
{
471 struct iter_stack_frame
*next
;
473 } iter_stack_frame_t
;
475 typedef struct iter_dependents_arg
{
478 boolean_t allowrecursion
;
479 iter_stack_frame_t
*stack
;
482 } iter_dependents_arg_t
;
485 iter_dependents_cb(zfs_handle_t
*zhp
, void *arg
)
487 iter_dependents_arg_t
*ida
= arg
;
489 boolean_t first
= ida
->first
;
490 ida
->first
= B_FALSE
;
492 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
493 err
= zfs_iter_clones(zhp
, ida
->flags
, iter_dependents_cb
, ida
);
494 } else if (zhp
->zfs_type
!= ZFS_TYPE_BOOKMARK
) {
495 iter_stack_frame_t isf
;
496 iter_stack_frame_t
*f
;
499 * check if there is a cycle by seeing if this fs is already
502 for (f
= ida
->stack
; f
!= NULL
; f
= f
->next
) {
503 if (f
->zhp
->zfs_dmustats
.dds_guid
==
504 zhp
->zfs_dmustats
.dds_guid
) {
505 if (ida
->allowrecursion
) {
509 zfs_error_aux(zhp
->zfs_hdl
,
510 dgettext(TEXT_DOMAIN
,
511 "recursive dependency at '%s'"),
513 err
= zfs_error(zhp
->zfs_hdl
,
515 dgettext(TEXT_DOMAIN
,
516 "cannot determine dependent "
525 isf
.next
= ida
->stack
;
527 err
= zfs_iter_filesystems(zhp
, ida
->flags
,
528 iter_dependents_cb
, ida
);
530 err
= zfs_iter_snapshots(zhp
, ida
->flags
,
531 iter_dependents_cb
, ida
, 0, 0);
532 ida
->stack
= isf
.next
;
535 if (!first
&& err
== 0)
536 err
= ida
->func(zhp
, ida
->data
);
544 zfs_iter_dependents(zfs_handle_t
*zhp
, int flags
, boolean_t allowrecursion
,
545 zfs_iter_f func
, void *data
)
547 iter_dependents_arg_t ida
;
549 ida
.allowrecursion
= allowrecursion
;
554 return (iter_dependents_cb(zfs_handle_dup(zhp
), &ida
));
558 * Iterate over mounted children of the specified dataset
561 zfs_iter_mounted(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
563 char mnt_prop
[ZFS_MAXPROPLEN
];
565 zfs_handle_t
*mtab_zhp
;
566 size_t namelen
= strlen(zhp
->zfs_name
);
570 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
573 while (err
== 0 && getmntent(mnttab
, &entry
) == 0) {
574 /* Ignore non-ZFS entries */
575 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
578 /* Ignore datasets not within the provided dataset */
579 if (strncmp(entry
.mnt_special
, zhp
->zfs_name
, namelen
) != 0 ||
580 entry
.mnt_special
[namelen
] != '/')
583 /* Skip snapshot of any child dataset */
584 if (strchr(entry
.mnt_special
, '@') != NULL
)
587 if ((mtab_zhp
= zfs_open(zhp
->zfs_hdl
, entry
.mnt_special
,
588 ZFS_TYPE_FILESYSTEM
)) == NULL
)
591 /* Ignore legacy mounts as they are user managed */
592 verify(zfs_prop_get(mtab_zhp
, ZFS_PROP_MOUNTPOINT
, mnt_prop
,
593 sizeof (mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
594 if (strcmp(mnt_prop
, "legacy") == 0) {
599 err
= func(mtab_zhp
, data
);