xref: /freebsd/sys/contrib/openzfs/lib/libzfs/libzfs_iter.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 
13 /*
14  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15  * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
16  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
17  * Copyright (c) 2019 Datto Inc.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <stddef.h>
25 #include <libintl.h>
26 #include <libzfs.h>
27 #include <libzutil.h>
28 #include <sys/mntent.h>
29 
30 #include "libzfs_impl.h"
31 
32 static int
zfs_iter_clones(zfs_handle_t * zhp,int flags __maybe_unused,zfs_iter_f func,void * data)33 zfs_iter_clones(zfs_handle_t *zhp, int flags __maybe_unused, zfs_iter_f func,
34     void *data)
35 {
36 	nvlist_t *nvl = zfs_get_clones_nvl(zhp);
37 	nvpair_t *pair;
38 
39 	if (nvl == NULL)
40 		return (0);
41 
42 	for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
43 	    pair = nvlist_next_nvpair(nvl, pair)) {
44 		zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
45 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
46 		if (clone != NULL) {
47 			int err = func(clone, data);
48 			if (err != 0)
49 				return (err);
50 		}
51 	}
52 	return (0);
53 }
54 
55 static int
zfs_do_list_ioctl(zfs_handle_t * zhp,int arg,zfs_cmd_t * zc)56 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
57 {
58 	int rc;
59 	uint64_t	orig_cookie;
60 
61 	orig_cookie = zc->zc_cookie;
62 top:
63 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
64 	zc->zc_objset_stats.dds_creation_txg = 0;
65 	rc = zfs_ioctl(zhp->zfs_hdl, arg, zc);
66 
67 	if (rc == -1) {
68 		switch (errno) {
69 		case ENOMEM:
70 			/* expand nvlist memory and try again */
71 			zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc);
72 			zc->zc_cookie = orig_cookie;
73 			goto top;
74 		/*
75 		 * An errno value of ESRCH indicates normal completion.
76 		 * If ENOENT is returned, then the underlying dataset
77 		 * has been removed since we obtained the handle.
78 		 */
79 		case ESRCH:
80 		case ENOENT:
81 			rc = 1;
82 			break;
83 		default:
84 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
85 			    dgettext(TEXT_DOMAIN,
86 			    "cannot iterate filesystems"));
87 			break;
88 		}
89 	}
90 	return (rc);
91 }
92 
93 /*
94  * Iterate over all child filesystems
95  */
96 int
zfs_iter_filesystems(zfs_handle_t * zhp,zfs_iter_f func,void * data)97 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
98 {
99 	return (zfs_iter_filesystems_v2(zhp, 0, func, data));
100 }
101 
102 int
zfs_iter_filesystems_v2(zfs_handle_t * zhp,int flags,zfs_iter_f func,void * data)103 zfs_iter_filesystems_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func,
104     void *data)
105 {
106 	zfs_cmd_t zc = {"\0"};
107 	zfs_handle_t *nzhp;
108 	int ret;
109 
110 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
111 		return (0);
112 
113 	zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
114 
115 	if ((flags & ZFS_ITER_SIMPLE) == ZFS_ITER_SIMPLE)
116 		zc.zc_simple = B_TRUE;
117 
118 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
119 	    &zc)) == 0) {
120 		if (zc.zc_simple)
121 			nzhp = make_dataset_simple_handle_zc(zhp, &zc);
122 		else
123 			nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
124 		/*
125 		 * Silently ignore errors, as the only plausible explanation is
126 		 * that the pool has since been removed.
127 		 */
128 		if (nzhp == NULL)
129 			continue;
130 
131 		if ((ret = func(nzhp, data)) != 0) {
132 			zcmd_free_nvlists(&zc);
133 			return (ret);
134 		}
135 	}
136 	zcmd_free_nvlists(&zc);
137 	return ((ret < 0) ? ret : 0);
138 }
139 
140 /*
141  * Iterate over all snapshots
142  */
143 int
zfs_iter_snapshots(zfs_handle_t * zhp,boolean_t simple,zfs_iter_f func,void * data,uint64_t min_txg,uint64_t max_txg)144 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
145     void *data, uint64_t min_txg, uint64_t max_txg)
146 {
147 	return (zfs_iter_snapshots_v2(zhp, simple ? ZFS_ITER_SIMPLE : 0, func,
148 	    data, min_txg, max_txg));
149 }
150 
151 int
zfs_iter_snapshots_v2(zfs_handle_t * zhp,int flags,zfs_iter_f func,void * data,uint64_t min_txg,uint64_t max_txg)152 zfs_iter_snapshots_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func,
153     void *data, uint64_t min_txg, uint64_t max_txg)
154 {
155 	zfs_cmd_t zc = {"\0"};
156 	zfs_handle_t *nzhp;
157 	int ret;
158 	nvlist_t *range_nvl = NULL;
159 
160 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
161 	    zhp->zfs_type == ZFS_TYPE_BOOKMARK)
162 		return (0);
163 
164 	zc.zc_simple = (flags & ZFS_ITER_SIMPLE) != 0;
165 
166 	zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
167 
168 	if (min_txg != 0) {
169 		range_nvl = fnvlist_alloc();
170 		fnvlist_add_uint64(range_nvl, SNAP_ITER_MIN_TXG, min_txg);
171 	}
172 	if (max_txg != 0) {
173 		if (range_nvl == NULL)
174 			range_nvl = fnvlist_alloc();
175 		fnvlist_add_uint64(range_nvl, SNAP_ITER_MAX_TXG, max_txg);
176 	}
177 
178 	if (range_nvl != NULL)
179 		zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl);
180 
181 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
182 	    &zc)) == 0) {
183 
184 		if (zc.zc_simple)
185 			nzhp = make_dataset_simple_handle_zc(zhp, &zc);
186 		else
187 			nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
188 		if (nzhp == NULL)
189 			continue;
190 
191 		if ((ret = func(nzhp, data)) != 0) {
192 			zcmd_free_nvlists(&zc);
193 			fnvlist_free(range_nvl);
194 			return (ret);
195 		}
196 	}
197 	zcmd_free_nvlists(&zc);
198 	fnvlist_free(range_nvl);
199 	return ((ret < 0) ? ret : 0);
200 }
201 
202 /*
203  * Iterate over all bookmarks
204  */
205 int
zfs_iter_bookmarks(zfs_handle_t * zhp,zfs_iter_f func,void * data)206 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
207 {
208 	return (zfs_iter_bookmarks_v2(zhp, 0, func, data));
209 }
210 
211 int
zfs_iter_bookmarks_v2(zfs_handle_t * zhp,int flags __maybe_unused,zfs_iter_f func,void * data)212 zfs_iter_bookmarks_v2(zfs_handle_t *zhp, int flags __maybe_unused,
213     zfs_iter_f func, void *data)
214 {
215 	zfs_handle_t *nzhp;
216 	nvlist_t *props = NULL;
217 	nvlist_t *bmarks = NULL;
218 	int err;
219 	nvpair_t *pair;
220 
221 	if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
222 		return (0);
223 
224 	/* Setup the requested properties nvlist. */
225 	props = fnvlist_alloc();
226 	for (zfs_prop_t p = 0; p < ZFS_NUM_PROPS; p++) {
227 		if (zfs_prop_valid_for_type(p, ZFS_TYPE_BOOKMARK, B_FALSE)) {
228 			fnvlist_add_boolean(props, zfs_prop_to_name(p));
229 		}
230 	}
231 	fnvlist_add_boolean(props, "redact_complete");
232 
233 	if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
234 		goto out;
235 
236 	for (pair = nvlist_next_nvpair(bmarks, NULL);
237 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
238 		char name[ZFS_MAX_DATASET_NAME_LEN];
239 		const char *bmark_name;
240 		nvlist_t *bmark_props;
241 
242 		bmark_name = nvpair_name(pair);
243 		bmark_props = fnvpair_value_nvlist(pair);
244 
245 		if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
246 		    bmark_name) >= sizeof (name)) {
247 			err = EINVAL;
248 			goto out;
249 		}
250 
251 		nzhp = make_bookmark_handle(zhp, name, bmark_props);
252 		if (nzhp == NULL)
253 			continue;
254 
255 		if ((err = func(nzhp, data)) != 0)
256 			goto out;
257 	}
258 
259 out:
260 	fnvlist_free(props);
261 	fnvlist_free(bmarks);
262 
263 	return (err);
264 }
265 
266 /*
267  * Routines for dealing with the sorted snapshot functionality
268  */
269 typedef struct zfs_node {
270 	zfs_handle_t	*zn_handle;
271 	avl_node_t	zn_avlnode;
272 } zfs_node_t;
273 
274 static int
zfs_sort_snaps(zfs_handle_t * zhp,void * data)275 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
276 {
277 	avl_tree_t *avl = data;
278 	zfs_node_t *node;
279 	zfs_node_t search;
280 
281 	search.zn_handle = zhp;
282 	node = avl_find(avl, &search, NULL);
283 	if (node) {
284 		/*
285 		 * If this snapshot was renamed while we were creating the
286 		 * AVL tree, it's possible that we already inserted it under
287 		 * its old name. Remove the old handle before adding the new
288 		 * one.
289 		 */
290 		zfs_close(node->zn_handle);
291 		avl_remove(avl, node);
292 		free(node);
293 	}
294 
295 	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
296 	node->zn_handle = zhp;
297 	avl_add(avl, node);
298 
299 	return (0);
300 }
301 
302 static int
zfs_snapshot_compare(const void * larg,const void * rarg)303 zfs_snapshot_compare(const void *larg, const void *rarg)
304 {
305 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
306 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
307 	uint64_t lcreate, rcreate;
308 
309 	/*
310 	 * Sort them according to creation time.  We use the hidden
311 	 * CREATETXG property to get an absolute ordering of snapshots.
312 	 */
313 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
314 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
315 
316 	return (TREE_CMP(lcreate, rcreate));
317 }
318 
319 int
zfs_iter_snapshots_sorted(zfs_handle_t * zhp,zfs_iter_f callback,void * data,uint64_t min_txg,uint64_t max_txg)320 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback,
321     void *data, uint64_t min_txg, uint64_t max_txg)
322 {
323 	return (zfs_iter_snapshots_sorted_v2(zhp, 0, callback, data,
324 	    min_txg, max_txg));
325 }
326 
327 int
zfs_iter_snapshots_sorted_v2(zfs_handle_t * zhp,int flags,zfs_iter_f callback,void * data,uint64_t min_txg,uint64_t max_txg)328 zfs_iter_snapshots_sorted_v2(zfs_handle_t *zhp, int flags, zfs_iter_f callback,
329     void *data, uint64_t min_txg, uint64_t max_txg)
330 {
331 	int ret = 0;
332 	zfs_node_t *node;
333 	avl_tree_t avl;
334 	void *cookie = NULL;
335 
336 	avl_create(&avl, zfs_snapshot_compare,
337 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
338 
339 	ret = zfs_iter_snapshots_v2(zhp, flags, zfs_sort_snaps, &avl, min_txg,
340 	    max_txg);
341 
342 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
343 		ret |= callback(node->zn_handle, data);
344 
345 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
346 		free(node);
347 
348 	avl_destroy(&avl);
349 
350 	return (ret);
351 }
352 
353 typedef struct {
354 	char *ssa_first;
355 	char *ssa_last;
356 	boolean_t ssa_seenfirst;
357 	boolean_t ssa_seenlast;
358 	zfs_iter_f ssa_func;
359 	void *ssa_arg;
360 } snapspec_arg_t;
361 
362 static int
snapspec_cb(zfs_handle_t * zhp,void * arg)363 snapspec_cb(zfs_handle_t *zhp, void *arg)
364 {
365 	snapspec_arg_t *ssa = arg;
366 	const char *shortsnapname;
367 	int err = 0;
368 
369 	if (ssa->ssa_seenlast)
370 		return (0);
371 
372 	shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
373 	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
374 		ssa->ssa_seenfirst = B_TRUE;
375 	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
376 		ssa->ssa_seenlast = B_TRUE;
377 
378 	if (ssa->ssa_seenfirst) {
379 		err = ssa->ssa_func(zhp, ssa->ssa_arg);
380 	} else {
381 		zfs_close(zhp);
382 	}
383 
384 	return (err);
385 }
386 
387 /*
388  * spec is a string like "A,B%C,D"
389  *
390  * <snaps>, where <snaps> can be:
391  *      <snap>          (single snapshot)
392  *      <snap>%<snap>   (range of snapshots, inclusive)
393  *      %<snap>         (range of snapshots, starting with earliest)
394  *      <snap>%         (range of snapshots, ending with last)
395  *      %               (all snapshots)
396  *      <snaps>[,...]   (comma separated list of the above)
397  *
398  * If a snapshot can not be opened, continue trying to open the others, but
399  * return ENOENT at the end.
400  */
401 int
zfs_iter_snapspec(zfs_handle_t * fs_zhp,const char * spec_orig,zfs_iter_f func,void * arg)402 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
403     zfs_iter_f func, void *arg)
404 {
405 	return (zfs_iter_snapspec_v2(fs_zhp, 0, spec_orig, func, arg));
406 }
407 
408 int
zfs_iter_snapspec_v2(zfs_handle_t * fs_zhp,int flags,const char * spec_orig,zfs_iter_f func,void * arg)409 zfs_iter_snapspec_v2(zfs_handle_t *fs_zhp, int flags, const char *spec_orig,
410     zfs_iter_f func, void *arg)
411 {
412 	char *buf, *comma_separated, *cp;
413 	int err = 0;
414 	int ret = 0;
415 
416 	buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
417 	cp = buf;
418 
419 	while ((comma_separated = strsep(&cp, ",")) != NULL) {
420 		char *pct = strchr(comma_separated, '%');
421 		if (pct != NULL) {
422 			snapspec_arg_t ssa = { 0 };
423 			ssa.ssa_func = func;
424 			ssa.ssa_arg = arg;
425 
426 			if (pct == comma_separated)
427 				ssa.ssa_seenfirst = B_TRUE;
428 			else
429 				ssa.ssa_first = comma_separated;
430 			*pct = '\0';
431 			ssa.ssa_last = pct + 1;
432 
433 			/*
434 			 * If there is a lastname specified, make sure it
435 			 * exists.
436 			 */
437 			if (ssa.ssa_last[0] != '\0') {
438 				char snapname[ZFS_MAX_DATASET_NAME_LEN];
439 				(void) snprintf(snapname, sizeof (snapname),
440 				    "%s@%s", zfs_get_name(fs_zhp),
441 				    ssa.ssa_last);
442 				if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
443 				    snapname, ZFS_TYPE_SNAPSHOT)) {
444 					ret = ENOENT;
445 					continue;
446 				}
447 			}
448 
449 			err = zfs_iter_snapshots_sorted_v2(fs_zhp, flags,
450 			    snapspec_cb, &ssa, 0, 0);
451 			if (ret == 0)
452 				ret = err;
453 			if (ret == 0 && (!ssa.ssa_seenfirst ||
454 			    (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
455 				ret = ENOENT;
456 			}
457 		} else {
458 			char snapname[ZFS_MAX_DATASET_NAME_LEN];
459 			zfs_handle_t *snap_zhp;
460 			(void) snprintf(snapname, sizeof (snapname), "%s@%s",
461 			    zfs_get_name(fs_zhp), comma_separated);
462 			snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
463 			    snapname);
464 			if (snap_zhp == NULL) {
465 				ret = ENOENT;
466 				continue;
467 			}
468 			err = func(snap_zhp, arg);
469 			if (ret == 0)
470 				ret = err;
471 		}
472 	}
473 
474 	free(buf);
475 	return (ret);
476 }
477 
478 /*
479  * Iterate over all children, snapshots and filesystems
480  * Process snapshots before filesystems because they are nearer the input
481  * handle: this is extremely important when used with zfs_iter_f functions
482  * looking for data, following the logic that we would like to find it as soon
483  * and as close as possible.
484  */
485 int
zfs_iter_children(zfs_handle_t * zhp,zfs_iter_f func,void * data)486 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
487 {
488 	return (zfs_iter_children_v2(zhp, 0, func, data));
489 }
490 
491 int
zfs_iter_children_v2(zfs_handle_t * zhp,int flags,zfs_iter_f func,void * data)492 zfs_iter_children_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func, void *data)
493 {
494 	int ret;
495 
496 	if ((ret = zfs_iter_snapshots_v2(zhp, flags, func, data, 0, 0)) != 0)
497 		return (ret);
498 
499 	return (zfs_iter_filesystems_v2(zhp, flags, func, data));
500 }
501 
502 
503 typedef struct iter_stack_frame {
504 	struct iter_stack_frame *next;
505 	zfs_handle_t *zhp;
506 } iter_stack_frame_t;
507 
508 typedef struct iter_dependents_arg {
509 	boolean_t first;
510 	int flags;
511 	boolean_t allowrecursion;
512 	iter_stack_frame_t *stack;
513 	zfs_iter_f func;
514 	void *data;
515 } iter_dependents_arg_t;
516 
517 static int
iter_dependents_cb(zfs_handle_t * zhp,void * arg)518 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
519 {
520 	iter_dependents_arg_t *ida = arg;
521 	int err = 0;
522 	boolean_t first = ida->first;
523 	ida->first = B_FALSE;
524 
525 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
526 		err = zfs_iter_clones(zhp, ida->flags, iter_dependents_cb, ida);
527 	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
528 		iter_stack_frame_t isf;
529 		iter_stack_frame_t *f;
530 
531 		/*
532 		 * check if there is a cycle by seeing if this fs is already
533 		 * on the stack.
534 		 */
535 		for (f = ida->stack; f != NULL; f = f->next) {
536 			if (f->zhp->zfs_dmustats.dds_guid ==
537 			    zhp->zfs_dmustats.dds_guid) {
538 				if (ida->allowrecursion) {
539 					zfs_close(zhp);
540 					return (0);
541 				} else {
542 					zfs_error_aux(zhp->zfs_hdl,
543 					    dgettext(TEXT_DOMAIN,
544 					    "recursive dependency at '%s'"),
545 					    zfs_get_name(zhp));
546 					err = zfs_error(zhp->zfs_hdl,
547 					    EZFS_RECURSIVE,
548 					    dgettext(TEXT_DOMAIN,
549 					    "cannot determine dependent "
550 					    "datasets"));
551 					zfs_close(zhp);
552 					return (err);
553 				}
554 			}
555 		}
556 
557 		isf.zhp = zhp;
558 		isf.next = ida->stack;
559 		ida->stack = &isf;
560 		err = zfs_iter_filesystems_v2(zhp, ida->flags,
561 		    iter_dependents_cb, ida);
562 		if (err == 0)
563 			err = zfs_iter_snapshots_sorted_v2(zhp, ida->flags,
564 			    iter_dependents_cb, ida, 0, 0);
565 		ida->stack = isf.next;
566 	}
567 
568 	if (!first && err == 0)
569 		err = ida->func(zhp, ida->data);
570 	else
571 		zfs_close(zhp);
572 
573 	return (err);
574 }
575 
576 int
zfs_iter_dependents(zfs_handle_t * zhp,boolean_t allowrecursion,zfs_iter_f func,void * data)577 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
578     zfs_iter_f func, void *data)
579 {
580 	return (zfs_iter_dependents_v2(zhp, 0, allowrecursion, func, data));
581 }
582 
583 int
zfs_iter_dependents_v2(zfs_handle_t * zhp,int flags,boolean_t allowrecursion,zfs_iter_f func,void * data)584 zfs_iter_dependents_v2(zfs_handle_t *zhp, int flags, boolean_t allowrecursion,
585     zfs_iter_f func, void *data)
586 {
587 	iter_dependents_arg_t ida;
588 	ida.flags = flags;
589 	ida.allowrecursion = allowrecursion;
590 	ida.stack = NULL;
591 	ida.func = func;
592 	ida.data = data;
593 	ida.first = B_TRUE;
594 	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
595 }
596 
597 /*
598  * Iterate over mounted children of the specified dataset
599  */
600 int
zfs_iter_mounted(zfs_handle_t * zhp,zfs_iter_f func,void * data)601 zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
602 {
603 	char mnt_prop[ZFS_MAXPROPLEN];
604 	struct mnttab entry;
605 	zfs_handle_t *mtab_zhp;
606 	size_t namelen = strlen(zhp->zfs_name);
607 	FILE *mnttab;
608 	int err = 0;
609 
610 	if ((mnttab = fopen(MNTTAB, "re")) == NULL)
611 		return (ENOENT);
612 
613 	while (err == 0 && getmntent(mnttab, &entry) == 0) {
614 		/* Ignore non-ZFS entries */
615 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
616 			continue;
617 
618 		/* Ignore datasets not within the provided dataset */
619 		if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 ||
620 		    entry.mnt_special[namelen] != '/')
621 			continue;
622 
623 		/* Skip snapshot of any child dataset */
624 		if (strchr(entry.mnt_special, '@') != NULL)
625 			continue;
626 
627 		if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special,
628 		    ZFS_TYPE_FILESYSTEM)) == NULL)
629 			continue;
630 
631 		/* Ignore legacy mounts as they are user managed */
632 		verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop,
633 		    sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
634 		if (strcmp(mnt_prop, "legacy") == 0) {
635 			zfs_close(mtab_zhp);
636 			continue;
637 		}
638 
639 		err = func(mtab_zhp, data);
640 	}
641 
642 	fclose(mnttab);
643 
644 	return (err);
645 }
646