Home | History | Annotate | Line # | Download | only in common
      1  1.1  chs /*
      2  1.1  chs  * CDDL HEADER START
      3  1.1  chs  *
      4  1.1  chs  * The contents of this file are subject to the terms of the
      5  1.1  chs  * Common Development and Distribution License (the "License").
      6  1.1  chs  * You may not use this file except in compliance with the License.
      7  1.1  chs  *
      8  1.1  chs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  1.1  chs  * or http://www.opensolaris.org/os/licensing.
     10  1.1  chs  * See the License for the specific language governing permissions
     11  1.1  chs  * and limitations under the License.
     12  1.1  chs  *
     13  1.1  chs  * When distributing Covered Code, include this CDDL HEADER in each
     14  1.1  chs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  1.1  chs  * If applicable, add the following below this CDDL HEADER, with the
     16  1.1  chs  * fields enclosed by brackets "[]" replaced with your own identifying
     17  1.1  chs  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  1.1  chs  *
     19  1.1  chs  * CDDL HEADER END
     20  1.1  chs  */
     21  1.1  chs 
     22  1.1  chs /*
     23  1.1  chs  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
     24  1.1  chs  * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
     25  1.1  chs  * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
     26  1.1  chs  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
     27  1.1  chs  */
     28  1.1  chs 
     29  1.1  chs #include <stdio.h>
     30  1.1  chs #include <stdlib.h>
     31  1.1  chs #include <strings.h>
     32  1.1  chs #include <unistd.h>
     33  1.1  chs #include <stddef.h>
     34  1.1  chs #include <libintl.h>
     35  1.1  chs #include <libzfs.h>
     36  1.1  chs 
     37  1.1  chs #include "libzfs_impl.h"
     38  1.1  chs 
     39  1.1  chs int
     40  1.1  chs zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
     41  1.1  chs {
     42  1.1  chs 	nvlist_t *nvl = zfs_get_clones_nvl(zhp);
     43  1.1  chs 	nvpair_t *pair;
     44  1.1  chs 
     45  1.1  chs 	if (nvl == NULL)
     46  1.1  chs 		return (0);
     47  1.1  chs 
     48  1.1  chs 	for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
     49  1.1  chs 	    pair = nvlist_next_nvpair(nvl, pair)) {
     50  1.1  chs 		zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
     51  1.1  chs 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
     52  1.1  chs 		if (clone != NULL) {
     53  1.1  chs 			int err = func(clone, data);
     54  1.1  chs 			if (err != 0)
     55  1.1  chs 				return (err);
     56  1.1  chs 		}
     57  1.1  chs 	}
     58  1.1  chs 	return (0);
     59  1.1  chs }
     60  1.1  chs 
     61  1.1  chs static int
     62  1.1  chs zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
     63  1.1  chs {
     64  1.1  chs 	int rc;
     65  1.1  chs 	uint64_t	orig_cookie;
     66  1.1  chs 
     67  1.1  chs 	orig_cookie = zc->zc_cookie;
     68  1.1  chs top:
     69  1.1  chs 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
     70  1.1  chs 	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
     71  1.1  chs 
     72  1.1  chs 	if (rc == -1) {
     73  1.1  chs 		switch (errno) {
     74  1.1  chs 		case ENOMEM:
     75  1.1  chs 			/* expand nvlist memory and try again */
     76  1.1  chs 			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
     77  1.1  chs 				zcmd_free_nvlists(zc);
     78  1.1  chs 				return (-1);
     79  1.1  chs 			}
     80  1.1  chs 			zc->zc_cookie = orig_cookie;
     81  1.1  chs 			goto top;
     82  1.1  chs 		/*
     83  1.1  chs 		 * An errno value of ESRCH indicates normal completion.
     84  1.1  chs 		 * If ENOENT is returned, then the underlying dataset
     85  1.1  chs 		 * has been removed since we obtained the handle.
     86  1.1  chs 		 */
     87  1.1  chs 		case ESRCH:
     88  1.1  chs 		case ENOENT:
     89  1.1  chs 			rc = 1;
     90  1.1  chs 			break;
     91  1.1  chs 		default:
     92  1.1  chs 			rc = zfs_standard_error(zhp->zfs_hdl, errno,
     93  1.1  chs 			    dgettext(TEXT_DOMAIN,
     94  1.1  chs 			    "cannot iterate filesystems"));
     95  1.1  chs 			break;
     96  1.1  chs 		}
     97  1.1  chs 	}
     98  1.1  chs 	return (rc);
     99  1.1  chs }
    100  1.1  chs 
    101  1.1  chs /*
    102  1.1  chs  * Iterate over all child filesystems
    103  1.1  chs  */
    104  1.1  chs int
    105  1.1  chs zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
    106  1.1  chs {
    107  1.1  chs 	zfs_cmd_t zc = { 0 };
    108  1.1  chs 	zfs_handle_t *nzhp;
    109  1.1  chs 	int ret;
    110  1.1  chs 
    111  1.1  chs 	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
    112  1.1  chs 		return (0);
    113  1.1  chs 
    114  1.1  chs 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
    115  1.1  chs 		return (-1);
    116  1.1  chs 
    117  1.1  chs 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
    118  1.1  chs 	    &zc)) == 0) {
    119  1.1  chs 		/*
    120  1.1  chs 		 * Silently ignore errors, as the only plausible explanation is
    121  1.1  chs 		 * that the pool has since been removed.
    122  1.1  chs 		 */
    123  1.1  chs 		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
    124  1.1  chs 		    &zc)) == NULL) {
    125  1.1  chs 			continue;
    126  1.1  chs 		}
    127  1.1  chs 
    128  1.1  chs 		if ((ret = func(nzhp, data)) != 0) {
    129  1.1  chs 			zcmd_free_nvlists(&zc);
    130  1.1  chs 			return (ret);
    131  1.1  chs 		}
    132  1.1  chs 	}
    133  1.1  chs 	zcmd_free_nvlists(&zc);
    134  1.1  chs 	return ((ret < 0) ? ret : 0);
    135  1.1  chs }
    136  1.1  chs 
    137  1.1  chs /*
    138  1.1  chs  * Iterate over all snapshots
    139  1.1  chs  */
    140  1.1  chs int
    141  1.1  chs zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
    142  1.1  chs     void *data)
    143  1.1  chs {
    144  1.1  chs 	zfs_cmd_t zc = { 0 };
    145  1.1  chs 	zfs_handle_t *nzhp;
    146  1.1  chs 	int ret;
    147  1.1  chs 
    148  1.1  chs 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
    149  1.1  chs 	    zhp->zfs_type == ZFS_TYPE_BOOKMARK)
    150  1.1  chs 		return (0);
    151  1.1  chs 
    152  1.1  chs 	zc.zc_simple = simple;
    153  1.1  chs 
    154  1.1  chs 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
    155  1.1  chs 		return (-1);
    156  1.1  chs 	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
    157  1.1  chs 	    &zc)) == 0) {
    158  1.1  chs 
    159  1.1  chs 		if (simple)
    160  1.1  chs 			nzhp = make_dataset_simple_handle_zc(zhp, &zc);
    161  1.1  chs 		else
    162  1.1  chs 			nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
    163  1.1  chs 		if (nzhp == NULL)
    164  1.1  chs 			continue;
    165  1.1  chs 
    166  1.1  chs 		if ((ret = func(nzhp, data)) != 0) {
    167  1.1  chs 			zcmd_free_nvlists(&zc);
    168  1.1  chs 			return (ret);
    169  1.1  chs 		}
    170  1.1  chs 	}
    171  1.1  chs 	zcmd_free_nvlists(&zc);
    172  1.1  chs 	return ((ret < 0) ? ret : 0);
    173  1.1  chs }
    174  1.1  chs 
    175  1.1  chs /*
    176  1.1  chs  * Iterate over all bookmarks
    177  1.1  chs  */
    178  1.1  chs int
    179  1.1  chs zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
    180  1.1  chs {
    181  1.1  chs 	zfs_handle_t *nzhp;
    182  1.1  chs 	nvlist_t *props = NULL;
    183  1.1  chs 	nvlist_t *bmarks = NULL;
    184  1.1  chs 	int err;
    185  1.1  chs 
    186  1.1  chs 	if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
    187  1.1  chs 		return (0);
    188  1.1  chs 
    189  1.1  chs 	/* Setup the requested properties nvlist. */
    190  1.1  chs 	props = fnvlist_alloc();
    191  1.1  chs 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
    192  1.1  chs 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
    193  1.1  chs 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
    194  1.1  chs 
    195  1.1  chs 	if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
    196  1.1  chs 		goto out;
    197  1.1  chs 
    198  1.1  chs 	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
    199  1.1  chs 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
    200  1.1  chs 		char name[ZFS_MAX_DATASET_NAME_LEN];
    201  1.1  chs 		char *bmark_name;
    202  1.1  chs 		nvlist_t *bmark_props;
    203  1.1  chs 
    204  1.1  chs 		bmark_name = nvpair_name(pair);
    205  1.1  chs 		bmark_props = fnvpair_value_nvlist(pair);
    206  1.1  chs 
    207  1.1  chs 		(void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
    208  1.1  chs 		    bmark_name);
    209  1.1  chs 
    210  1.1  chs 		nzhp = make_bookmark_handle(zhp, name, bmark_props);
    211  1.1  chs 		if (nzhp == NULL)
    212  1.1  chs 			continue;
    213  1.1  chs 
    214  1.1  chs 		if ((err = func(nzhp, data)) != 0)
    215  1.1  chs 			goto out;
    216  1.1  chs 	}
    217  1.1  chs 
    218  1.1  chs out:
    219  1.1  chs 	fnvlist_free(props);
    220  1.1  chs 	fnvlist_free(bmarks);
    221  1.1  chs 
    222  1.1  chs 	return (err);
    223  1.1  chs }
    224  1.1  chs 
    225  1.1  chs /*
    226  1.1  chs  * Routines for dealing with the sorted snapshot functionality
    227  1.1  chs  */
    228  1.1  chs typedef struct zfs_node {
    229  1.1  chs 	zfs_handle_t	*zn_handle;
    230  1.1  chs 	avl_node_t	zn_avlnode;
    231  1.1  chs } zfs_node_t;
    232  1.1  chs 
    233  1.1  chs static int
    234  1.1  chs zfs_sort_snaps(zfs_handle_t *zhp, void *data)
    235  1.1  chs {
    236  1.1  chs 	avl_tree_t *avl = data;
    237  1.1  chs 	zfs_node_t *node;
    238  1.1  chs 	zfs_node_t search;
    239  1.1  chs 
    240  1.1  chs 	search.zn_handle = zhp;
    241  1.1  chs 	node = avl_find(avl, &search, NULL);
    242  1.1  chs 	if (node) {
    243  1.1  chs 		/*
    244  1.1  chs 		 * If this snapshot was renamed while we were creating the
    245  1.1  chs 		 * AVL tree, it's possible that we already inserted it under
    246  1.1  chs 		 * its old name. Remove the old handle before adding the new
    247  1.1  chs 		 * one.
    248  1.1  chs 		 */
    249  1.1  chs 		zfs_close(node->zn_handle);
    250  1.1  chs 		avl_remove(avl, node);
    251  1.1  chs 		free(node);
    252  1.1  chs 	}
    253  1.1  chs 
    254  1.1  chs 	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
    255  1.1  chs 	node->zn_handle = zhp;
    256  1.1  chs 	avl_add(avl, node);
    257  1.1  chs 
    258  1.1  chs 	return (0);
    259  1.1  chs }
    260  1.1  chs 
    261  1.1  chs static int
    262  1.1  chs zfs_snapshot_compare(const void *larg, const void *rarg)
    263  1.1  chs {
    264  1.1  chs 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
    265  1.1  chs 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
    266  1.1  chs 	uint64_t lcreate, rcreate;
    267  1.1  chs 
    268  1.1  chs 	/*
    269  1.1  chs 	 * Sort them according to creation time.  We use the hidden
    270  1.1  chs 	 * CREATETXG property to get an absolute ordering of snapshots.
    271  1.1  chs 	 */
    272  1.1  chs 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
    273  1.1  chs 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
    274  1.1  chs 
    275  1.1  chs 	if (lcreate < rcreate)
    276  1.1  chs 		return (-1);
    277  1.1  chs 	else if (lcreate > rcreate)
    278  1.1  chs 		return (+1);
    279  1.1  chs 	else
    280  1.1  chs 		return (0);
    281  1.1  chs }
    282  1.1  chs 
    283  1.1  chs int
    284  1.1  chs zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
    285  1.1  chs {
    286  1.1  chs 	int ret = 0;
    287  1.1  chs 	zfs_node_t *node;
    288  1.1  chs 	avl_tree_t avl;
    289  1.1  chs 	void *cookie = NULL;
    290  1.1  chs 
    291  1.1  chs 	avl_create(&avl, zfs_snapshot_compare,
    292  1.1  chs 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
    293  1.1  chs 
    294  1.1  chs 	ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
    295  1.1  chs 
    296  1.1  chs 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
    297  1.1  chs 		ret |= callback(node->zn_handle, data);
    298  1.1  chs 
    299  1.1  chs 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
    300  1.1  chs 		free(node);
    301  1.1  chs 
    302  1.1  chs 	avl_destroy(&avl);
    303  1.1  chs 
    304  1.1  chs 	return (ret);
    305  1.1  chs }
    306  1.1  chs 
    307  1.1  chs typedef struct {
    308  1.1  chs 	char *ssa_first;
    309  1.1  chs 	char *ssa_last;
    310  1.1  chs 	boolean_t ssa_seenfirst;
    311  1.1  chs 	boolean_t ssa_seenlast;
    312  1.1  chs 	zfs_iter_f ssa_func;
    313  1.1  chs 	void *ssa_arg;
    314  1.1  chs } snapspec_arg_t;
    315  1.1  chs 
    316  1.1  chs static int
    317  1.1  chs snapspec_cb(zfs_handle_t *zhp, void *arg)
    318  1.1  chs {
    319  1.1  chs 	snapspec_arg_t *ssa = arg;
    320  1.1  chs 	char *shortsnapname;
    321  1.1  chs 	int err = 0;
    322  1.1  chs 
    323  1.1  chs 	if (ssa->ssa_seenlast)
    324  1.1  chs 		return (0);
    325  1.1  chs 	shortsnapname = zfs_strdup(zhp->zfs_hdl,
    326  1.1  chs 	    strchr(zfs_get_name(zhp), '@') + 1);
    327  1.1  chs 
    328  1.1  chs 	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
    329  1.1  chs 		ssa->ssa_seenfirst = B_TRUE;
    330  1.1  chs 
    331  1.1  chs 	if (ssa->ssa_seenfirst) {
    332  1.1  chs 		err = ssa->ssa_func(zhp, ssa->ssa_arg);
    333  1.1  chs 	} else {
    334  1.1  chs 		zfs_close(zhp);
    335  1.1  chs 	}
    336  1.1  chs 
    337  1.1  chs 	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
    338  1.1  chs 		ssa->ssa_seenlast = B_TRUE;
    339  1.1  chs 	free(shortsnapname);
    340  1.1  chs 
    341  1.1  chs 	return (err);
    342  1.1  chs }
    343  1.1  chs 
    344  1.1  chs /*
    345  1.1  chs  * spec is a string like "A,B%C,D"
    346  1.1  chs  *
    347  1.1  chs  * <snaps>, where <snaps> can be:
    348  1.1  chs  *      <snap>          (single snapshot)
    349  1.1  chs  *      <snap>%<snap>   (range of snapshots, inclusive)
    350  1.1  chs  *      %<snap>         (range of snapshots, starting with earliest)
    351  1.1  chs  *      <snap>%         (range of snapshots, ending with last)
    352  1.1  chs  *      %               (all snapshots)
    353  1.1  chs  *      <snaps>[,...]   (comma separated list of the above)
    354  1.1  chs  *
    355  1.1  chs  * If a snapshot can not be opened, continue trying to open the others, but
    356  1.1  chs  * return ENOENT at the end.
    357  1.1  chs  */
    358  1.1  chs int
    359  1.1  chs zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
    360  1.1  chs     zfs_iter_f func, void *arg)
    361  1.1  chs {
    362  1.1  chs 	char *buf, *comma_separated, *cp;
    363  1.1  chs 	int err = 0;
    364  1.1  chs 	int ret = 0;
    365  1.1  chs 
    366  1.1  chs 	buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
    367  1.1  chs 	cp = buf;
    368  1.1  chs 
    369  1.1  chs 	while ((comma_separated = strsep(&cp, ",")) != NULL) {
    370  1.1  chs 		char *pct = strchr(comma_separated, '%');
    371  1.1  chs 		if (pct != NULL) {
    372  1.1  chs 			snapspec_arg_t ssa = { 0 };
    373  1.1  chs 			ssa.ssa_func = func;
    374  1.1  chs 			ssa.ssa_arg = arg;
    375  1.1  chs 
    376  1.1  chs 			if (pct == comma_separated)
    377  1.1  chs 				ssa.ssa_seenfirst = B_TRUE;
    378  1.1  chs 			else
    379  1.1  chs 				ssa.ssa_first = comma_separated;
    380  1.1  chs 			*pct = '\0';
    381  1.1  chs 			ssa.ssa_last = pct + 1;
    382  1.1  chs 
    383  1.1  chs 			/*
    384  1.1  chs 			 * If there is a lastname specified, make sure it
    385  1.1  chs 			 * exists.
    386  1.1  chs 			 */
    387  1.1  chs 			if (ssa.ssa_last[0] != '\0') {
    388  1.1  chs 				char snapname[ZFS_MAX_DATASET_NAME_LEN];
    389  1.1  chs 				(void) snprintf(snapname, sizeof (snapname),
    390  1.1  chs 				    "%s@%s", zfs_get_name(fs_zhp),
    391  1.1  chs 				    ssa.ssa_last);
    392  1.1  chs 				if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
    393  1.1  chs 				    snapname, ZFS_TYPE_SNAPSHOT)) {
    394  1.1  chs 					ret = ENOENT;
    395  1.1  chs 					continue;
    396  1.1  chs 				}
    397  1.1  chs 			}
    398  1.1  chs 
    399  1.1  chs 			err = zfs_iter_snapshots_sorted(fs_zhp,
    400  1.1  chs 			    snapspec_cb, &ssa);
    401  1.1  chs 			if (ret == 0)
    402  1.1  chs 				ret = err;
    403  1.1  chs 			if (ret == 0 && (!ssa.ssa_seenfirst ||
    404  1.1  chs 			    (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
    405  1.1  chs 				ret = ENOENT;
    406  1.1  chs 			}
    407  1.1  chs 		} else {
    408  1.1  chs 			char snapname[ZFS_MAX_DATASET_NAME_LEN];
    409  1.1  chs 			zfs_handle_t *snap_zhp;
    410  1.1  chs 			(void) snprintf(snapname, sizeof (snapname), "%s@%s",
    411  1.1  chs 			    zfs_get_name(fs_zhp), comma_separated);
    412  1.1  chs 			snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
    413  1.1  chs 			    snapname);
    414  1.1  chs 			if (snap_zhp == NULL) {
    415  1.1  chs 				ret = ENOENT;
    416  1.1  chs 				continue;
    417  1.1  chs 			}
    418  1.1  chs 			err = func(snap_zhp, arg);
    419  1.1  chs 			if (ret == 0)
    420  1.1  chs 				ret = err;
    421  1.1  chs 		}
    422  1.1  chs 	}
    423  1.1  chs 
    424  1.1  chs 	free(buf);
    425  1.1  chs 	return (ret);
    426  1.1  chs }
    427  1.1  chs 
    428  1.1  chs /*
    429  1.1  chs  * Iterate over all children, snapshots and filesystems
    430  1.1  chs  */
    431  1.1  chs int
    432  1.1  chs zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
    433  1.1  chs {
    434  1.1  chs 	int ret;
    435  1.1  chs 
    436  1.1  chs 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
    437  1.1  chs 		return (ret);
    438  1.1  chs 
    439  1.1  chs 	return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
    440  1.1  chs }
    441  1.1  chs 
    442  1.1  chs 
    443  1.1  chs typedef struct iter_stack_frame {
    444  1.1  chs 	struct iter_stack_frame *next;
    445  1.1  chs 	zfs_handle_t *zhp;
    446  1.1  chs } iter_stack_frame_t;
    447  1.1  chs 
    448  1.1  chs typedef struct iter_dependents_arg {
    449  1.1  chs 	boolean_t first;
    450  1.1  chs 	boolean_t allowrecursion;
    451  1.1  chs 	iter_stack_frame_t *stack;
    452  1.1  chs 	zfs_iter_f func;
    453  1.1  chs 	void *data;
    454  1.1  chs } iter_dependents_arg_t;
    455  1.1  chs 
    456  1.1  chs static int
    457  1.1  chs iter_dependents_cb(zfs_handle_t *zhp, void *arg)
    458  1.1  chs {
    459  1.1  chs 	iter_dependents_arg_t *ida = arg;
    460  1.1  chs 	int err = 0;
    461  1.1  chs 	boolean_t first = ida->first;
    462  1.1  chs 	ida->first = B_FALSE;
    463  1.1  chs 
    464  1.1  chs 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
    465  1.1  chs 		err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
    466  1.1  chs 	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
    467  1.1  chs 		iter_stack_frame_t isf;
    468  1.1  chs 		iter_stack_frame_t *f;
    469  1.1  chs 
    470  1.1  chs 		/*
    471  1.1  chs 		 * check if there is a cycle by seeing if this fs is already
    472  1.1  chs 		 * on the stack.
    473  1.1  chs 		 */
    474  1.1  chs 		for (f = ida->stack; f != NULL; f = f->next) {
    475  1.1  chs 			if (f->zhp->zfs_dmustats.dds_guid ==
    476  1.1  chs 			    zhp->zfs_dmustats.dds_guid) {
    477  1.1  chs 				if (ida->allowrecursion) {
    478  1.1  chs 					zfs_close(zhp);
    479  1.1  chs 					return (0);
    480  1.1  chs 				} else {
    481  1.1  chs 					zfs_error_aux(zhp->zfs_hdl,
    482  1.1  chs 					    dgettext(TEXT_DOMAIN,
    483  1.1  chs 					    "recursive dependency at '%s'"),
    484  1.1  chs 					    zfs_get_name(zhp));
    485  1.1  chs 					err = zfs_error(zhp->zfs_hdl,
    486  1.1  chs 					    EZFS_RECURSIVE,
    487  1.1  chs 					    dgettext(TEXT_DOMAIN,
    488  1.1  chs 					    "cannot determine dependent "
    489  1.1  chs 					    "datasets"));
    490  1.1  chs 					zfs_close(zhp);
    491  1.1  chs 					return (err);
    492  1.1  chs 				}
    493  1.1  chs 			}
    494  1.1  chs 		}
    495  1.1  chs 
    496  1.1  chs 		isf.zhp = zhp;
    497  1.1  chs 		isf.next = ida->stack;
    498  1.1  chs 		ida->stack = &isf;
    499  1.1  chs 		err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
    500  1.1  chs 		if (err == 0) {
    501  1.1  chs 			err = zfs_iter_snapshots(zhp, B_FALSE,
    502  1.1  chs 			    iter_dependents_cb, ida);
    503  1.1  chs 		}
    504  1.1  chs 		ida->stack = isf.next;
    505  1.1  chs 	}
    506  1.1  chs 
    507  1.1  chs 	if (!first && err == 0)
    508  1.1  chs 		err = ida->func(zhp, ida->data);
    509  1.1  chs 	else
    510  1.1  chs 		zfs_close(zhp);
    511  1.1  chs 
    512  1.1  chs 	return (err);
    513  1.1  chs }
    514  1.1  chs 
    515  1.1  chs int
    516  1.1  chs zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
    517  1.1  chs     zfs_iter_f func, void *data)
    518  1.1  chs {
    519  1.1  chs 	iter_dependents_arg_t ida;
    520  1.1  chs 	ida.allowrecursion = allowrecursion;
    521  1.1  chs 	ida.stack = NULL;
    522  1.1  chs 	ida.func = func;
    523  1.1  chs 	ida.data = data;
    524  1.1  chs 	ida.first = B_TRUE;
    525  1.1  chs 	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
    526  1.1  chs }
    527