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