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 http://www.opensolaris.org/os/licensing.
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, 2015 by Delphix. All rights reserved.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2018 Datto Inc.
37 #include <sys/mntent.h>
39 #include "libzfs_impl.h"
42 zfs_iter_clones(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
44 nvlist_t
*nvl
= zfs_get_clones_nvl(zhp
);
50 for (pair
= nvlist_next_nvpair(nvl
, NULL
); pair
!= NULL
;
51 pair
= nvlist_next_nvpair(nvl
, pair
)) {
52 zfs_handle_t
*clone
= zfs_open(zhp
->zfs_hdl
, nvpair_name(pair
),
53 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
55 int err
= func(clone
, data
);
64 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
69 orig_cookie
= zc
->zc_cookie
;
71 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
72 rc
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, arg
, zc
);
77 /* expand nvlist memory and try again */
78 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
) != 0) {
79 zcmd_free_nvlists(zc
);
82 zc
->zc_cookie
= orig_cookie
;
85 * An errno value of ESRCH indicates normal completion.
86 * If ENOENT is returned, then the underlying dataset
87 * has been removed since we obtained the handle.
94 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
96 "cannot iterate filesystems"));
104 * Iterate over all child filesystems
107 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
109 zfs_cmd_t zc
= {"\0"};
113 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
116 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
119 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
122 * Silently ignore errors, as the only plausible explanation is
123 * that the pool has since been removed.
125 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
130 if ((ret
= func(nzhp
, data
)) != 0) {
131 zcmd_free_nvlists(&zc
);
135 zcmd_free_nvlists(&zc
);
136 return ((ret
< 0) ? ret
: 0);
140 * Iterate over all snapshots
143 zfs_iter_snapshots(zfs_handle_t
*zhp
, boolean_t simple
, zfs_iter_f func
,
146 zfs_cmd_t zc
= {"\0"};
150 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
||
151 zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
)
154 zc
.zc_simple
= simple
;
156 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
158 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
162 nzhp
= make_dataset_simple_handle_zc(zhp
, &zc
);
164 nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
, &zc
);
168 if ((ret
= func(nzhp
, data
)) != 0) {
169 zcmd_free_nvlists(&zc
);
173 zcmd_free_nvlists(&zc
);
174 return ((ret
< 0) ? ret
: 0);
178 * Iterate over all bookmarks
181 zfs_iter_bookmarks(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
184 nvlist_t
*props
= NULL
;
185 nvlist_t
*bmarks
= NULL
;
189 if ((zfs_get_type(zhp
) & (ZFS_TYPE_SNAPSHOT
| ZFS_TYPE_BOOKMARK
)) != 0)
192 /* Setup the requested properties nvlist. */
193 props
= fnvlist_alloc();
194 fnvlist_add_boolean(props
, zfs_prop_to_name(ZFS_PROP_GUID
));
195 fnvlist_add_boolean(props
, zfs_prop_to_name(ZFS_PROP_CREATETXG
));
196 fnvlist_add_boolean(props
, zfs_prop_to_name(ZFS_PROP_CREATION
));
198 if ((err
= lzc_get_bookmarks(zhp
->zfs_name
, props
, &bmarks
)) != 0)
201 for (pair
= nvlist_next_nvpair(bmarks
, NULL
);
202 pair
!= NULL
; pair
= nvlist_next_nvpair(bmarks
, pair
)) {
203 char name
[ZFS_MAX_DATASET_NAME_LEN
];
205 nvlist_t
*bmark_props
;
207 bmark_name
= nvpair_name(pair
);
208 bmark_props
= fnvpair_value_nvlist(pair
);
210 if (snprintf(name
, sizeof (name
), "%s#%s", zhp
->zfs_name
,
211 bmark_name
) >= sizeof (name
)) {
216 nzhp
= make_bookmark_handle(zhp
, name
, bmark_props
);
220 if ((err
= func(nzhp
, data
)) != 0)
226 fnvlist_free(bmarks
);
232 * Routines for dealing with the sorted snapshot functionality
234 typedef struct zfs_node
{
235 zfs_handle_t
*zn_handle
;
236 avl_node_t zn_avlnode
;
240 zfs_sort_snaps(zfs_handle_t
*zhp
, void *data
)
242 avl_tree_t
*avl
= data
;
246 search
.zn_handle
= zhp
;
247 node
= avl_find(avl
, &search
, NULL
);
250 * If this snapshot was renamed while we were creating the
251 * AVL tree, it's possible that we already inserted it under
252 * its old name. Remove the old handle before adding the new
255 zfs_close(node
->zn_handle
);
256 avl_remove(avl
, node
);
260 node
= zfs_alloc(zhp
->zfs_hdl
, sizeof (zfs_node_t
));
261 node
->zn_handle
= zhp
;
268 zfs_snapshot_compare(const void *larg
, const void *rarg
)
270 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
271 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
272 uint64_t lcreate
, rcreate
;
275 * Sort them according to creation time. We use the hidden
276 * CREATETXG property to get an absolute ordering of snapshots.
278 lcreate
= zfs_prop_get_int(l
, ZFS_PROP_CREATETXG
);
279 rcreate
= zfs_prop_get_int(r
, ZFS_PROP_CREATETXG
);
281 return (AVL_CMP(lcreate
, rcreate
));
285 zfs_iter_snapshots_sorted(zfs_handle_t
*zhp
, zfs_iter_f callback
, void *data
)
292 avl_create(&avl
, zfs_snapshot_compare
,
293 sizeof (zfs_node_t
), offsetof(zfs_node_t
, zn_avlnode
));
295 ret
= zfs_iter_snapshots(zhp
, B_FALSE
, zfs_sort_snaps
, &avl
);
297 for (node
= avl_first(&avl
); node
!= NULL
; node
= AVL_NEXT(&avl
, node
))
298 ret
|= callback(node
->zn_handle
, data
);
300 while ((node
= avl_destroy_nodes(&avl
, &cookie
)) != NULL
)
311 boolean_t ssa_seenfirst
;
312 boolean_t ssa_seenlast
;
318 snapspec_cb(zfs_handle_t
*zhp
, void *arg
)
320 snapspec_arg_t
*ssa
= arg
;
321 const char *shortsnapname
;
324 if (ssa
->ssa_seenlast
)
327 shortsnapname
= strchr(zfs_get_name(zhp
), '@') + 1;
328 if (!ssa
->ssa_seenfirst
&& strcmp(shortsnapname
, ssa
->ssa_first
) == 0)
329 ssa
->ssa_seenfirst
= B_TRUE
;
330 if (strcmp(shortsnapname
, ssa
->ssa_last
) == 0)
331 ssa
->ssa_seenlast
= B_TRUE
;
333 if (ssa
->ssa_seenfirst
) {
334 err
= ssa
->ssa_func(zhp
, ssa
->ssa_arg
);
343 * spec is a string like "A,B%C,D"
345 * <snaps>, where <snaps> can be:
346 * <snap> (single snapshot)
347 * <snap>%<snap> (range of snapshots, inclusive)
348 * %<snap> (range of snapshots, starting with earliest)
349 * <snap>% (range of snapshots, ending with last)
351 * <snaps>[,...] (comma separated list of the above)
353 * If a snapshot can not be opened, continue trying to open the others, but
354 * return ENOENT at the end.
357 zfs_iter_snapspec(zfs_handle_t
*fs_zhp
, const char *spec_orig
,
358 zfs_iter_f func
, void *arg
)
360 char *buf
, *comma_separated
, *cp
;
364 buf
= zfs_strdup(fs_zhp
->zfs_hdl
, spec_orig
);
367 while ((comma_separated
= strsep(&cp
, ",")) != NULL
) {
368 char *pct
= strchr(comma_separated
, '%');
370 snapspec_arg_t ssa
= { 0 };
374 if (pct
== comma_separated
)
375 ssa
.ssa_seenfirst
= B_TRUE
;
377 ssa
.ssa_first
= comma_separated
;
379 ssa
.ssa_last
= pct
+ 1;
382 * If there is a lastname specified, make sure it
385 if (ssa
.ssa_last
[0] != '\0') {
386 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
387 (void) snprintf(snapname
, sizeof (snapname
),
388 "%s@%s", zfs_get_name(fs_zhp
),
390 if (!zfs_dataset_exists(fs_zhp
->zfs_hdl
,
391 snapname
, ZFS_TYPE_SNAPSHOT
)) {
397 err
= zfs_iter_snapshots_sorted(fs_zhp
,
401 if (ret
== 0 && (!ssa
.ssa_seenfirst
||
402 (ssa
.ssa_last
[0] != '\0' && !ssa
.ssa_seenlast
))) {
406 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
407 zfs_handle_t
*snap_zhp
;
408 (void) snprintf(snapname
, sizeof (snapname
), "%s@%s",
409 zfs_get_name(fs_zhp
), comma_separated
);
410 snap_zhp
= make_dataset_handle(fs_zhp
->zfs_hdl
,
412 if (snap_zhp
== NULL
) {
416 err
= func(snap_zhp
, arg
);
427 * Iterate over all children, snapshots and filesystems
428 * Process snapshots before filesystems because they are nearer the input
429 * handle: this is extremely important when used with zfs_iter_f functions
430 * looking for data, following the logic that we would like to find it as soon
431 * and as close as possible.
434 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
438 if ((ret
= zfs_iter_snapshots(zhp
, B_FALSE
, func
, data
)) != 0)
441 return (zfs_iter_filesystems(zhp
, func
, data
));
445 typedef struct iter_stack_frame
{
446 struct iter_stack_frame
*next
;
448 } iter_stack_frame_t
;
450 typedef struct iter_dependents_arg
{
452 boolean_t allowrecursion
;
453 iter_stack_frame_t
*stack
;
456 } iter_dependents_arg_t
;
459 iter_dependents_cb(zfs_handle_t
*zhp
, void *arg
)
461 iter_dependents_arg_t
*ida
= arg
;
463 boolean_t first
= ida
->first
;
464 ida
->first
= B_FALSE
;
466 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
467 err
= zfs_iter_clones(zhp
, iter_dependents_cb
, ida
);
468 } else if (zhp
->zfs_type
!= ZFS_TYPE_BOOKMARK
) {
469 iter_stack_frame_t isf
;
470 iter_stack_frame_t
*f
;
473 * check if there is a cycle by seeing if this fs is already
476 for (f
= ida
->stack
; f
!= NULL
; f
= f
->next
) {
477 if (f
->zhp
->zfs_dmustats
.dds_guid
==
478 zhp
->zfs_dmustats
.dds_guid
) {
479 if (ida
->allowrecursion
) {
483 zfs_error_aux(zhp
->zfs_hdl
,
484 dgettext(TEXT_DOMAIN
,
485 "recursive dependency at '%s'"),
487 err
= zfs_error(zhp
->zfs_hdl
,
489 dgettext(TEXT_DOMAIN
,
490 "cannot determine dependent "
499 isf
.next
= ida
->stack
;
501 err
= zfs_iter_filesystems(zhp
, iter_dependents_cb
, ida
);
503 err
= zfs_iter_snapshots(zhp
, B_FALSE
,
504 iter_dependents_cb
, ida
);
505 ida
->stack
= isf
.next
;
508 if (!first
&& err
== 0)
509 err
= ida
->func(zhp
, ida
->data
);
517 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
518 zfs_iter_f func
, void *data
)
520 iter_dependents_arg_t ida
;
521 ida
.allowrecursion
= allowrecursion
;
526 return (iter_dependents_cb(zfs_handle_dup(zhp
), &ida
));
530 * Iterate over mounted children of the specified dataset
533 zfs_iter_mounted(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
535 char mnt_prop
[ZFS_MAXPROPLEN
];
537 zfs_handle_t
*mtab_zhp
;
538 size_t namelen
= strlen(zhp
->zfs_name
);
542 if ((mnttab
= fopen(MNTTAB
, "r")) == NULL
)
545 while (err
== 0 && getmntent(mnttab
, &entry
) == 0) {
546 /* Ignore non-ZFS entries */
547 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
550 /* Ignore datasets not within the provided dataset */
551 if (strncmp(entry
.mnt_special
, zhp
->zfs_name
, namelen
) != 0 ||
552 (entry
.mnt_special
[namelen
] != '/' &&
553 entry
.mnt_special
[namelen
] != '@'))
556 if ((mtab_zhp
= zfs_open(zhp
->zfs_hdl
, entry
.mnt_special
,
557 ZFS_TYPE_FILESYSTEM
)) == NULL
)
560 /* Ignore legacy mounts as they are user managed */
561 verify(zfs_prop_get(mtab_zhp
, ZFS_PROP_MOUNTPOINT
, mnt_prop
,
562 sizeof (mnt_prop
), NULL
, NULL
, 0, B_FALSE
) == 0);
563 if (strcmp(mnt_prop
, "legacy") == 0) {
568 err
= func(mtab_zhp
, data
);