119b94df9SMatthew Ahrens /*
219b94df9SMatthew Ahrens  * CDDL HEADER START
319b94df9SMatthew Ahrens  *
419b94df9SMatthew Ahrens  * The contents of this file are subject to the terms of the
519b94df9SMatthew Ahrens  * Common Development and Distribution License (the "License").
619b94df9SMatthew Ahrens  * You may not use this file except in compliance with the License.
719b94df9SMatthew Ahrens  *
819b94df9SMatthew Ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
919b94df9SMatthew Ahrens  * or http://www.opensolaris.org/os/licensing.
1019b94df9SMatthew Ahrens  * See the License for the specific language governing permissions
1119b94df9SMatthew Ahrens  * and limitations under the License.
1219b94df9SMatthew Ahrens  *
1319b94df9SMatthew Ahrens  * When distributing Covered Code, include this CDDL HEADER in each
1419b94df9SMatthew Ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1519b94df9SMatthew Ahrens  * If applicable, add the following below this CDDL HEADER, with the
1619b94df9SMatthew Ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
1719b94df9SMatthew Ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
1819b94df9SMatthew Ahrens  *
1919b94df9SMatthew Ahrens  * CDDL HEADER END
2019b94df9SMatthew Ahrens  */
2119b94df9SMatthew Ahrens 
2219b94df9SMatthew Ahrens /*
2319b94df9SMatthew Ahrens  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
249a686fbcSPaul Dagnelie  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
250d8fa8f8SMartin Matuska  * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
26b7070b7dSJan Kryl  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
2719b94df9SMatthew Ahrens  */
2819b94df9SMatthew Ahrens 
2919b94df9SMatthew Ahrens #include <stdio.h>
3019b94df9SMatthew Ahrens #include <stdlib.h>
3119b94df9SMatthew Ahrens #include <strings.h>
3219b94df9SMatthew Ahrens #include <unistd.h>
3319b94df9SMatthew Ahrens #include <stddef.h>
3419b94df9SMatthew Ahrens #include <libintl.h>
3519b94df9SMatthew Ahrens #include <libzfs.h>
36*d8ab6e12SDon Brady #include <libzutil.h>
3719b94df9SMatthew Ahrens 
3819b94df9SMatthew Ahrens #include "libzfs_impl.h"
3919b94df9SMatthew Ahrens 
4019b94df9SMatthew Ahrens int
zfs_iter_clones(zfs_handle_t * zhp,zfs_iter_f func,void * data)4119b94df9SMatthew Ahrens zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
4219b94df9SMatthew Ahrens {
4319b94df9SMatthew Ahrens 	nvlist_t *nvl = zfs_get_clones_nvl(zhp);
4419b94df9SMatthew Ahrens 	nvpair_t *pair;
4519b94df9SMatthew Ahrens 
4619b94df9SMatthew Ahrens 	if (nvl == NULL)
4719b94df9SMatthew Ahrens 		return (0);
4819b94df9SMatthew Ahrens 
4919b94df9SMatthew Ahrens 	for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
5019b94df9SMatthew Ahrens 	    pair = nvlist_next_nvpair(nvl, pair)) {
5119b94df9SMatthew Ahrens 		zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
5219b94df9SMatthew Ahrens 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5319b94df9SMatthew Ahrens 		if (clone != NULL) {
5419b94df9SMatthew Ahrens 			int err = func(clone, data);
5519b94df9SMatthew Ahrens 			if (err != 0)
5619b94df9SMatthew Ahrens 				return (err);
5719b94df9SMatthew Ahrens 		}
5819b94df9SMatthew Ahrens 	}
5919b94df9SMatthew Ahrens 	return (0);
6019b94df9SMatthew Ahrens }
6119b94df9SMatthew Ahrens 
6219b94df9SMatthew Ahrens static int
zfs_do_list_ioctl(zfs_handle_t * zhp,int arg,zfs_cmd_t * zc)6319b94df9SMatthew Ahrens zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
6419b94df9SMatthew Ahrens {
6519b94df9SMatthew Ahrens 	int rc;
6619b94df9SMatthew Ahrens 	uint64_t	orig_cookie;
6719b94df9SMatthew Ahrens 
6819b94df9SMatthew Ahrens 	orig_cookie = zc->zc_cookie;
6919b94df9SMatthew Ahrens top:
7019b94df9SMatthew Ahrens 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
7119b94df9SMatthew Ahrens 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
7219b94df9SMatthew Ahrens 
7319b94df9SMatthew Ahrens 	if (rc == -1) {
7419b94df9SMatthew Ahrens 		switch (errno) {
7519b94df9SMatthew Ahrens 		case ENOMEM:
7619b94df9SMatthew Ahrens 			/* expand nvlist memory and try again */
7719b94df9SMatthew Ahrens 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
7819b94df9SMatthew Ahrens 				zcmd_free_nvlists(zc);
7919b94df9SMatthew Ahrens 				return (-1);
8019b94df9SMatthew Ahrens 			}
8119b94df9SMatthew Ahrens 			zc->zc_cookie = orig_cookie;
8219b94df9SMatthew Ahrens 			goto top;
8319b94df9SMatthew Ahrens 		/*
8419b94df9SMatthew Ahrens 		 * An errno value of ESRCH indicates normal completion.
8519b94df9SMatthew Ahrens 		 * If ENOENT is returned, then the underlying dataset
8619b94df9SMatthew Ahrens 		 * has been removed since we obtained the handle.
8719b94df9SMatthew Ahrens 		 */
8819b94df9SMatthew Ahrens 		case ESRCH:
8919b94df9SMatthew Ahrens 		case ENOENT:
9019b94df9SMatthew Ahrens 			rc = 1;
9119b94df9SMatthew Ahrens 			break;
9219b94df9SMatthew Ahrens 		default:
9319b94df9SMatthew Ahrens 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
9419b94df9SMatthew Ahrens 			    dgettext(TEXT_DOMAIN,
9519b94df9SMatthew Ahrens 			    "cannot iterate filesystems"));
9619b94df9SMatthew Ahrens 			break;
9719b94df9SMatthew Ahrens 		}
9819b94df9SMatthew Ahrens 	}
9919b94df9SMatthew Ahrens 	return (rc);
10019b94df9SMatthew Ahrens }
10119b94df9SMatthew Ahrens 
10219b94df9SMatthew Ahrens /*
10319b94df9SMatthew Ahrens  * Iterate over all child filesystems
10419b94df9SMatthew Ahrens  */
10519b94df9SMatthew Ahrens int
zfs_iter_filesystems(zfs_handle_t * zhp,zfs_iter_f func,void * data)10619b94df9SMatthew Ahrens zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
10719b94df9SMatthew Ahrens {
10819b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
10919b94df9SMatthew Ahrens 	zfs_handle_t *nzhp;
11019b94df9SMatthew Ahrens 	int ret;
11119b94df9SMatthew Ahrens 
11219b94df9SMatthew Ahrens 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
11319b94df9SMatthew Ahrens 		return (0);
11419b94df9SMatthew Ahrens 
11519b94df9SMatthew Ahrens 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
11619b94df9SMatthew Ahrens 		return (-1);
11719b94df9SMatthew Ahrens 
11819b94df9SMatthew Ahrens 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
11919b94df9SMatthew Ahrens 	    &zc)) == 0) {
12019b94df9SMatthew Ahrens 		/*
12119b94df9SMatthew Ahrens 		 * Silently ignore errors, as the only plausible explanation is
12219b94df9SMatthew Ahrens 		 * that the pool has since been removed.
12319b94df9SMatthew Ahrens 		 */
12419b94df9SMatthew Ahrens 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
12519b94df9SMatthew Ahrens 		    &zc)) == NULL) {
12619b94df9SMatthew Ahrens 			continue;
12719b94df9SMatthew Ahrens 		}
12819b94df9SMatthew Ahrens 
12919b94df9SMatthew Ahrens 		if ((ret = func(nzhp, data)) != 0) {
13019b94df9SMatthew Ahrens 			zcmd_free_nvlists(&zc);
13119b94df9SMatthew Ahrens 			return (ret);
13219b94df9SMatthew Ahrens 		}
13319b94df9SMatthew Ahrens 	}
13419b94df9SMatthew Ahrens 	zcmd_free_nvlists(&zc);
13519b94df9SMatthew Ahrens 	return ((ret < 0) ? ret : 0);
13619b94df9SMatthew Ahrens }
13719b94df9SMatthew Ahrens 
13819b94df9SMatthew Ahrens /*
13919b94df9SMatthew Ahrens  * Iterate over all snapshots
14019b94df9SMatthew Ahrens  */
14119b94df9SMatthew Ahrens int
zfs_iter_snapshots(zfs_handle_t * zhp,boolean_t simple,zfs_iter_f func,void * data)1420d8fa8f8SMartin Matuska zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
1430d8fa8f8SMartin Matuska     void *data)
14419b94df9SMatthew Ahrens {
14519b94df9SMatthew Ahrens 	zfs_cmd_t zc = { 0 };
14619b94df9SMatthew Ahrens 	zfs_handle_t *nzhp;
14719b94df9SMatthew Ahrens 	int ret;
14819b94df9SMatthew Ahrens 
14978f17100SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
15078f17100SMatthew Ahrens 	    zhp->zfs_type == ZFS_TYPE_BOOKMARK)
15119b94df9SMatthew Ahrens 		return (0);
15219b94df9SMatthew Ahrens 
1530d8fa8f8SMartin Matuska 	zc.zc_simple = simple;
1540d8fa8f8SMartin Matuska 
15519b94df9SMatthew Ahrens 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
15619b94df9SMatthew Ahrens 		return (-1);
15719b94df9SMatthew Ahrens 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
15819b94df9SMatthew Ahrens 	    &zc)) == 0) {
15919b94df9SMatthew Ahrens 
1600d8fa8f8SMartin Matuska 		if (simple)
1610d8fa8f8SMartin Matuska 			nzhp = make_dataset_simple_handle_zc(zhp, &zc);
1620d8fa8f8SMartin Matuska 		else
1630d8fa8f8SMartin Matuska 			nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
1640d8fa8f8SMartin Matuska 		if (nzhp == NULL)
16519b94df9SMatthew Ahrens 			continue;
16619b94df9SMatthew Ahrens 
16719b94df9SMatthew Ahrens 		if ((ret = func(nzhp, data)) != 0) {
16819b94df9SMatthew Ahrens 			zcmd_free_nvlists(&zc);
16919b94df9SMatthew Ahrens 			return (ret);
17019b94df9SMatthew Ahrens 		}
17119b94df9SMatthew Ahrens 	}
17219b94df9SMatthew Ahrens 	zcmd_free_nvlists(&zc);
17319b94df9SMatthew Ahrens 	return ((ret < 0) ? ret : 0);
17419b94df9SMatthew Ahrens }
17519b94df9SMatthew Ahrens 
17678f17100SMatthew Ahrens /*
17778f17100SMatthew Ahrens  * Iterate over all bookmarks
17878f17100SMatthew Ahrens  */
17978f17100SMatthew Ahrens int
zfs_iter_bookmarks(zfs_handle_t * zhp,zfs_iter_f func,void * data)18078f17100SMatthew Ahrens zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
18178f17100SMatthew Ahrens {
18278f17100SMatthew Ahrens 	zfs_handle_t *nzhp;
18378f17100SMatthew Ahrens 	nvlist_t *props = NULL;
18478f17100SMatthew Ahrens 	nvlist_t *bmarks = NULL;
18578f17100SMatthew Ahrens 	int err;
18678f17100SMatthew Ahrens 
18778f17100SMatthew Ahrens 	if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
18878f17100SMatthew Ahrens 		return (0);
18978f17100SMatthew Ahrens 
19078f17100SMatthew Ahrens 	/* Setup the requested properties nvlist. */
19178f17100SMatthew Ahrens 	props = fnvlist_alloc();
19278f17100SMatthew Ahrens 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
19378f17100SMatthew Ahrens 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
19478f17100SMatthew Ahrens 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
195eb633035STom Caputi 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_IVSET_GUID));
19678f17100SMatthew Ahrens 
19778f17100SMatthew Ahrens 	if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
19878f17100SMatthew Ahrens 		goto out;
19978f17100SMatthew Ahrens 
20078f17100SMatthew Ahrens 	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
20178f17100SMatthew Ahrens 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
2029adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
20378f17100SMatthew Ahrens 		char *bmark_name;
20478f17100SMatthew Ahrens 		nvlist_t *bmark_props;
20578f17100SMatthew Ahrens 
20678f17100SMatthew Ahrens 		bmark_name = nvpair_name(pair);
20778f17100SMatthew Ahrens 		bmark_props = fnvpair_value_nvlist(pair);
20878f17100SMatthew Ahrens 
20978f17100SMatthew Ahrens 		(void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
21078f17100SMatthew Ahrens 		    bmark_name);
21178f17100SMatthew Ahrens 
21278f17100SMatthew Ahrens 		nzhp = make_bookmark_handle(zhp, name, bmark_props);
21378f17100SMatthew Ahrens 		if (nzhp == NULL)
21478f17100SMatthew Ahrens 			continue;
21578f17100SMatthew Ahrens 
21678f17100SMatthew Ahrens 		if ((err = func(nzhp, data)) != 0)
21778f17100SMatthew Ahrens 			goto out;
21878f17100SMatthew Ahrens 	}
21978f17100SMatthew Ahrens 
22078f17100SMatthew Ahrens out:
22178f17100SMatthew Ahrens 	fnvlist_free(props);
22278f17100SMatthew Ahrens 	fnvlist_free(bmarks);
22378f17100SMatthew Ahrens 
22478f17100SMatthew Ahrens 	return (err);
22578f17100SMatthew Ahrens }
22678f17100SMatthew Ahrens 
22719b94df9SMatthew Ahrens /*
22819b94df9SMatthew Ahrens  * Routines for dealing with the sorted snapshot functionality
22919b94df9SMatthew Ahrens  */
23019b94df9SMatthew Ahrens typedef struct zfs_node {
23119b94df9SMatthew Ahrens 	zfs_handle_t	*zn_handle;
23219b94df9SMatthew Ahrens 	avl_node_t	zn_avlnode;
23319b94df9SMatthew Ahrens } zfs_node_t;
23419b94df9SMatthew Ahrens 
23519b94df9SMatthew Ahrens static int
zfs_sort_snaps(zfs_handle_t * zhp,void * data)23619b94df9SMatthew Ahrens zfs_sort_snaps(zfs_handle_t *zhp, void *data)
23719b94df9SMatthew Ahrens {
23819b94df9SMatthew Ahrens 	avl_tree_t *avl = data;
23919b94df9SMatthew Ahrens 	zfs_node_t *node;
24019b94df9SMatthew Ahrens 	zfs_node_t search;
24119b94df9SMatthew Ahrens 
24219b94df9SMatthew Ahrens 	search.zn_handle = zhp;
24319b94df9SMatthew Ahrens 	node = avl_find(avl, &search, NULL);
24419b94df9SMatthew Ahrens 	if (node) {
24519b94df9SMatthew Ahrens 		/*
24619b94df9SMatthew Ahrens 		 * If this snapshot was renamed while we were creating the
24719b94df9SMatthew Ahrens 		 * AVL tree, it's possible that we already inserted it under
24819b94df9SMatthew Ahrens 		 * its old name. Remove the old handle before adding the new
24919b94df9SMatthew Ahrens 		 * one.
25019b94df9SMatthew Ahrens 		 */
25119b94df9SMatthew Ahrens 		zfs_close(node->zn_handle);
25219b94df9SMatthew Ahrens 		avl_remove(avl, node);
25319b94df9SMatthew Ahrens 		free(node);
25419b94df9SMatthew Ahrens 	}
25519b94df9SMatthew Ahrens 
25619b94df9SMatthew Ahrens 	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
25719b94df9SMatthew Ahrens 	node->zn_handle = zhp;
25819b94df9SMatthew Ahrens 	avl_add(avl, node);
25919b94df9SMatthew Ahrens 
26019b94df9SMatthew Ahrens 	return (0);
26119b94df9SMatthew Ahrens }
26219b94df9SMatthew Ahrens 
26319b94df9SMatthew Ahrens static int
zfs_snapshot_compare(const void * larg,const void * rarg)26419b94df9SMatthew Ahrens zfs_snapshot_compare(const void *larg, const void *rarg)
26519b94df9SMatthew Ahrens {
26619b94df9SMatthew Ahrens 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
26719b94df9SMatthew Ahrens 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
26819b94df9SMatthew Ahrens 	uint64_t lcreate, rcreate;
26919b94df9SMatthew Ahrens 
27019b94df9SMatthew Ahrens 	/*
27119b94df9SMatthew Ahrens 	 * Sort them according to creation time.  We use the hidden
27219b94df9SMatthew Ahrens 	 * CREATETXG property to get an absolute ordering of snapshots.
27319b94df9SMatthew Ahrens 	 */
27419b94df9SMatthew Ahrens 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
27519b94df9SMatthew Ahrens 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
27619b94df9SMatthew Ahrens 
2774d7988d6SPaul Dagnelie 	if (lcreate < rcreate)
2784d7988d6SPaul Dagnelie 		return (-1);
2794d7988d6SPaul Dagnelie 	if (lcreate > rcreate)
2804d7988d6SPaul Dagnelie 		return (+1);
2814d7988d6SPaul Dagnelie 	return (0);
28219b94df9SMatthew Ahrens }
28319b94df9SMatthew Ahrens 
28419b94df9SMatthew Ahrens int
zfs_iter_snapshots_sorted(zfs_handle_t * zhp,zfs_iter_f callback,void * data)28519b94df9SMatthew Ahrens zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
28619b94df9SMatthew Ahrens {
28719b94df9SMatthew Ahrens 	int ret = 0;
28819b94df9SMatthew Ahrens 	zfs_node_t *node;
28919b94df9SMatthew Ahrens 	avl_tree_t avl;
29019b94df9SMatthew Ahrens 	void *cookie = NULL;
29119b94df9SMatthew Ahrens 
29219b94df9SMatthew Ahrens 	avl_create(&avl, zfs_snapshot_compare,
29319b94df9SMatthew Ahrens 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
29419b94df9SMatthew Ahrens 
2950d8fa8f8SMartin Matuska 	ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
29619b94df9SMatthew Ahrens 
29719b94df9SMatthew Ahrens 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
29819b94df9SMatthew Ahrens 		ret |= callback(node->zn_handle, data);
29919b94df9SMatthew Ahrens 
30019b94df9SMatthew Ahrens 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
30119b94df9SMatthew Ahrens 		free(node);
30219b94df9SMatthew Ahrens 
30319b94df9SMatthew Ahrens 	avl_destroy(&avl);
30419b94df9SMatthew Ahrens 
30519b94df9SMatthew Ahrens 	return (ret);
30619b94df9SMatthew Ahrens }
30719b94df9SMatthew Ahrens 
30819b94df9SMatthew Ahrens typedef struct {
30919b94df9SMatthew Ahrens 	char *ssa_first;
31019b94df9SMatthew Ahrens 	char *ssa_last;
31119b94df9SMatthew Ahrens 	boolean_t ssa_seenfirst;
31219b94df9SMatthew Ahrens 	boolean_t ssa_seenlast;
31319b94df9SMatthew Ahrens 	zfs_iter_f ssa_func;
31419b94df9SMatthew Ahrens 	void *ssa_arg;
31519b94df9SMatthew Ahrens } snapspec_arg_t;
31619b94df9SMatthew Ahrens 
31719b94df9SMatthew Ahrens static int
snapspec_cb(zfs_handle_t * zhp,void * arg)3189a686fbcSPaul Dagnelie snapspec_cb(zfs_handle_t *zhp, void *arg)
3199a686fbcSPaul Dagnelie {
32019b94df9SMatthew Ahrens 	snapspec_arg_t *ssa = arg;
321d8584ba6SMarcel Telka 	const char *shortsnapname;
32219b94df9SMatthew Ahrens 	int err = 0;
32319b94df9SMatthew Ahrens 
32419b94df9SMatthew Ahrens 	if (ssa->ssa_seenlast)
32519b94df9SMatthew Ahrens 		return (0);
32619b94df9SMatthew Ahrens 
327d8584ba6SMarcel Telka 	shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
32819b94df9SMatthew Ahrens 	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
32919b94df9SMatthew Ahrens 		ssa->ssa_seenfirst = B_TRUE;
330d8584ba6SMarcel Telka 	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
331d8584ba6SMarcel Telka 		ssa->ssa_seenlast = B_TRUE;
33219b94df9SMatthew Ahrens 
33319b94df9SMatthew Ahrens 	if (ssa->ssa_seenfirst) {
33419b94df9SMatthew Ahrens 		err = ssa->ssa_func(zhp, ssa->ssa_arg);
33519b94df9SMatthew Ahrens 	} else {
33619b94df9SMatthew Ahrens 		zfs_close(zhp);
33719b94df9SMatthew Ahrens 	}
33819b94df9SMatthew Ahrens 
33919b94df9SMatthew Ahrens 	return (err);
34019b94df9SMatthew Ahrens }
34119b94df9SMatthew Ahrens 
34219b94df9SMatthew Ahrens /*
34319b94df9SMatthew Ahrens  * spec is a string like "A,B%C,D"
34419b94df9SMatthew Ahrens  *
34519b94df9SMatthew Ahrens  * <snaps>, where <snaps> can be:
34619b94df9SMatthew Ahrens  *      <snap>          (single snapshot)
34719b94df9SMatthew Ahrens  *      <snap>%<snap>   (range of snapshots, inclusive)
34819b94df9SMatthew Ahrens  *      %<snap>         (range of snapshots, starting with earliest)
34919b94df9SMatthew Ahrens  *      <snap>%         (range of snapshots, ending with last)
35019b94df9SMatthew Ahrens  *      %               (all snapshots)
35119b94df9SMatthew Ahrens  *      <snaps>[,...]   (comma separated list of the above)
35219b94df9SMatthew Ahrens  *
35319b94df9SMatthew Ahrens  * If a snapshot can not be opened, continue trying to open the others, but
35419b94df9SMatthew Ahrens  * return ENOENT at the end.
35519b94df9SMatthew Ahrens  */
35619b94df9SMatthew Ahrens int
zfs_iter_snapspec(zfs_handle_t * fs_zhp,const char * spec_orig,zfs_iter_f func,void * arg)35719b94df9SMatthew Ahrens zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
35819b94df9SMatthew Ahrens     zfs_iter_f func, void *arg)
35919b94df9SMatthew Ahrens {
3604445fffbSMatthew Ahrens 	char *buf, *comma_separated, *cp;
36119b94df9SMatthew Ahrens 	int err = 0;
36219b94df9SMatthew Ahrens 	int ret = 0;
36319b94df9SMatthew Ahrens 
3644445fffbSMatthew Ahrens 	buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
36519b94df9SMatthew Ahrens 	cp = buf;
36619b94df9SMatthew Ahrens 
36719b94df9SMatthew Ahrens 	while ((comma_separated = strsep(&cp, ",")) != NULL) {
36819b94df9SMatthew Ahrens 		char *pct = strchr(comma_separated, '%');
36919b94df9SMatthew Ahrens 		if (pct != NULL) {
37019b94df9SMatthew Ahrens 			snapspec_arg_t ssa = { 0 };
37119b94df9SMatthew Ahrens 			ssa.ssa_func = func;
37219b94df9SMatthew Ahrens 			ssa.ssa_arg = arg;
37319b94df9SMatthew Ahrens 
37419b94df9SMatthew Ahrens 			if (pct == comma_separated)
37519b94df9SMatthew Ahrens 				ssa.ssa_seenfirst = B_TRUE;
37619b94df9SMatthew Ahrens 			else
37719b94df9SMatthew Ahrens 				ssa.ssa_first = comma_separated;
37819b94df9SMatthew Ahrens 			*pct = '\0';
37919b94df9SMatthew Ahrens 			ssa.ssa_last = pct + 1;
38019b94df9SMatthew Ahrens 
38119b94df9SMatthew Ahrens 			/*
38219b94df9SMatthew Ahrens 			 * If there is a lastname specified, make sure it
38319b94df9SMatthew Ahrens 			 * exists.
38419b94df9SMatthew Ahrens 			 */
38519b94df9SMatthew Ahrens 			if (ssa.ssa_last[0] != '\0') {
3869adfa60dSMatthew Ahrens 				char snapname[ZFS_MAX_DATASET_NAME_LEN];
38719b94df9SMatthew Ahrens 				(void) snprintf(snapname, sizeof (snapname),
38819b94df9SMatthew Ahrens 				    "%s@%s", zfs_get_name(fs_zhp),
38919b94df9SMatthew Ahrens 				    ssa.ssa_last);
39019b94df9SMatthew Ahrens 				if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
39119b94df9SMatthew Ahrens 				    snapname, ZFS_TYPE_SNAPSHOT)) {
39219b94df9SMatthew Ahrens 					ret = ENOENT;
39319b94df9SMatthew Ahrens 					continue;
39419b94df9SMatthew Ahrens 				}
39519b94df9SMatthew Ahrens 			}
39619b94df9SMatthew Ahrens 
39719b94df9SMatthew Ahrens 			err = zfs_iter_snapshots_sorted(fs_zhp,
39819b94df9SMatthew Ahrens 			    snapspec_cb, &ssa);
39919b94df9SMatthew Ahrens 			if (ret == 0)
40019b94df9SMatthew Ahrens 				ret = err;
40119b94df9SMatthew Ahrens 			if (ret == 0 && (!ssa.ssa_seenfirst ||
40219b94df9SMatthew Ahrens 			    (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
40319b94df9SMatthew Ahrens 				ret = ENOENT;
40419b94df9SMatthew Ahrens 			}
40519b94df9SMatthew Ahrens 		} else {
4069adfa60dSMatthew Ahrens 			char snapname[ZFS_MAX_DATASET_NAME_LEN];
40719b94df9SMatthew Ahrens 			zfs_handle_t *snap_zhp;
40819b94df9SMatthew Ahrens 			(void) snprintf(snapname, sizeof (snapname), "%s@%s",
40919b94df9SMatthew Ahrens 			    zfs_get_name(fs_zhp), comma_separated);
41019b94df9SMatthew Ahrens 			snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
41119b94df9SMatthew Ahrens 			    snapname);
41219b94df9SMatthew Ahrens 			if (snap_zhp == NULL) {
41319b94df9SMatthew Ahrens 				ret = ENOENT;
41419b94df9SMatthew Ahrens 				continue;
41519b94df9SMatthew Ahrens 			}
41619b94df9SMatthew Ahrens 			err = func(snap_zhp, arg);
41719b94df9SMatthew Ahrens 			if (ret == 0)
41819b94df9SMatthew Ahrens 				ret = err;
41919b94df9SMatthew Ahrens 		}
42019b94df9SMatthew Ahrens 	}
42119b94df9SMatthew Ahrens 
4224445fffbSMatthew Ahrens 	free(buf);
42319b94df9SMatthew Ahrens 	return (ret);
42419b94df9SMatthew Ahrens }
42519b94df9SMatthew Ahrens 
42619b94df9SMatthew Ahrens /*
42719b94df9SMatthew Ahrens  * Iterate over all children, snapshots and filesystems
428544132fcSloli  * Process snapshots before filesystems because they are nearer the input
429544132fcSloli  * handle: this is extremely important when used with zfs_iter_f functions
430544132fcSloli  * looking for data, following the logic that we would like to find it as soon
431544132fcSloli  * and as close as possible.
43219b94df9SMatthew Ahrens  */
43319b94df9SMatthew Ahrens int
zfs_iter_children(zfs_handle_t * zhp,zfs_iter_f func,void * data)43419b94df9SMatthew Ahrens zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
43519b94df9SMatthew Ahrens {
43619b94df9SMatthew Ahrens 	int ret;
43719b94df9SMatthew Ahrens 
438544132fcSloli 	if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
43919b94df9SMatthew Ahrens 		return (ret);
44019b94df9SMatthew Ahrens 
441544132fcSloli 	return (zfs_iter_filesystems(zhp, func, data));
44219b94df9SMatthew Ahrens }
44319b94df9SMatthew Ahrens 
44419b94df9SMatthew Ahrens 
44519b94df9SMatthew Ahrens typedef struct iter_stack_frame {
44619b94df9SMatthew Ahrens 	struct iter_stack_frame *next;
44719b94df9SMatthew Ahrens 	zfs_handle_t *zhp;
44819b94df9SMatthew Ahrens } iter_stack_frame_t;
44919b94df9SMatthew Ahrens 
45019b94df9SMatthew Ahrens typedef struct iter_dependents_arg {
45119b94df9SMatthew Ahrens 	boolean_t first;
45219b94df9SMatthew Ahrens 	boolean_t allowrecursion;
45319b94df9SMatthew Ahrens 	iter_stack_frame_t *stack;
45419b94df9SMatthew Ahrens 	zfs_iter_f func;
45519b94df9SMatthew Ahrens 	void *data;
45619b94df9SMatthew Ahrens } iter_dependents_arg_t;
45719b94df9SMatthew Ahrens 
45819b94df9SMatthew Ahrens static int
iter_dependents_cb(zfs_handle_t * zhp,void * arg)45919b94df9SMatthew Ahrens iter_dependents_cb(zfs_handle_t *zhp, void *arg)
46019b94df9SMatthew Ahrens {
46119b94df9SMatthew Ahrens 	iter_dependents_arg_t *ida = arg;
46278f17100SMatthew Ahrens 	int err = 0;
46319b94df9SMatthew Ahrens 	boolean_t first = ida->first;
46419b94df9SMatthew Ahrens 	ida->first = B_FALSE;
46519b94df9SMatthew Ahrens 
46619b94df9SMatthew Ahrens 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
46719b94df9SMatthew Ahrens 		err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
46878f17100SMatthew Ahrens 	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
46919b94df9SMatthew Ahrens 		iter_stack_frame_t isf;
47019b94df9SMatthew Ahrens 		iter_stack_frame_t *f;
47119b94df9SMatthew Ahrens 
47219b94df9SMatthew Ahrens 		/*
47319b94df9SMatthew Ahrens 		 * check if there is a cycle by seeing if this fs is already
47419b94df9SMatthew Ahrens 		 * on the stack.
47519b94df9SMatthew Ahrens 		 */
47619b94df9SMatthew Ahrens 		for (f = ida->stack; f != NULL; f = f->next) {
47719b94df9SMatthew Ahrens 			if (f->zhp->zfs_dmustats.dds_guid ==
47819b94df9SMatthew Ahrens 			    zhp->zfs_dmustats.dds_guid) {
47919b94df9SMatthew Ahrens 				if (ida->allowrecursion) {
48019b94df9SMatthew Ahrens 					zfs_close(zhp);
48119b94df9SMatthew Ahrens 					return (0);
48219b94df9SMatthew Ahrens 				} else {
48319b94df9SMatthew Ahrens 					zfs_error_aux(zhp->zfs_hdl,
48419b94df9SMatthew Ahrens 					    dgettext(TEXT_DOMAIN,
48519b94df9SMatthew Ahrens 					    "recursive dependency at '%s'"),
48619b94df9SMatthew Ahrens 					    zfs_get_name(zhp));
48719b94df9SMatthew Ahrens 					err = zfs_error(zhp->zfs_hdl,
48819b94df9SMatthew Ahrens 					    EZFS_RECURSIVE,
48919b94df9SMatthew Ahrens 					    dgettext(TEXT_DOMAIN,
49019b94df9SMatthew Ahrens 					    "cannot determine dependent "
49119b94df9SMatthew Ahrens 					    "datasets"));
49219b94df9SMatthew Ahrens 					zfs_close(zhp);
49319b94df9SMatthew Ahrens 					return (err);
49419b94df9SMatthew Ahrens 				}
49519b94df9SMatthew Ahrens 			}
49619b94df9SMatthew Ahrens 		}
49719b94df9SMatthew Ahrens 
49819b94df9SMatthew Ahrens 		isf.zhp = zhp;
49919b94df9SMatthew Ahrens 		isf.next = ida->stack;
50019b94df9SMatthew Ahrens 		ida->stack = &isf;
50119b94df9SMatthew Ahrens 		err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
5020d8fa8f8SMartin Matuska 		if (err == 0) {
5030d8fa8f8SMartin Matuska 			err = zfs_iter_snapshots(zhp, B_FALSE,
5040d8fa8f8SMartin Matuska 			    iter_dependents_cb, ida);
5050d8fa8f8SMartin Matuska 		}
50619b94df9SMatthew Ahrens 		ida->stack = isf.next;
50719b94df9SMatthew Ahrens 	}
5082fbdf8dbSMarcel Telka 
50919b94df9SMatthew Ahrens 	if (!first && err == 0)
51019b94df9SMatthew Ahrens 		err = ida->func(zhp, ida->data);
5112fbdf8dbSMarcel Telka 	else
5122fbdf8dbSMarcel Telka 		zfs_close(zhp);
5132fbdf8dbSMarcel Telka 
51419b94df9SMatthew Ahrens 	return (err);
51519b94df9SMatthew Ahrens }
51619b94df9SMatthew Ahrens 
51719b94df9SMatthew Ahrens int
zfs_iter_dependents(zfs_handle_t * zhp,boolean_t allowrecursion,zfs_iter_f func,void * data)51819b94df9SMatthew Ahrens zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
51919b94df9SMatthew Ahrens     zfs_iter_f func, void *data)
52019b94df9SMatthew Ahrens {
52119b94df9SMatthew Ahrens 	iter_dependents_arg_t ida;
52219b94df9SMatthew Ahrens 	ida.allowrecursion = allowrecursion;
52319b94df9SMatthew Ahrens 	ida.stack = NULL;
52419b94df9SMatthew Ahrens 	ida.func = func;
52519b94df9SMatthew Ahrens 	ida.data = data;
52619b94df9SMatthew Ahrens 	ida.first = B_TRUE;
52719b94df9SMatthew Ahrens 	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
52819b94df9SMatthew Ahrens }
529