1228103Smm/*
2228103Smm * CDDL HEADER START
3228103Smm *
4228103Smm * The contents of this file are subject to the terms of the
5228103Smm * Common Development and Distribution License (the "License").
6228103Smm * You may not use this file except in compliance with the License.
7228103Smm *
8228103Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9228103Smm * or http://www.opensolaris.org/os/licensing.
10228103Smm * See the License for the specific language governing permissions
11228103Smm * and limitations under the License.
12228103Smm *
13228103Smm * When distributing Covered Code, include this CDDL HEADER in each
14228103Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15228103Smm * If applicable, add the following below this CDDL HEADER, with the
16228103Smm * fields enclosed by brackets "[]" replaced with your own identifying
17228103Smm * information: Portions Copyright [yyyy] [name of copyright owner]
18228103Smm *
19228103Smm * CDDL HEADER END
20228103Smm */
21228103Smm
22228103Smm/*
23228103Smm * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24289562Smav * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
25307046Smav * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
26275812Sdelphij * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
27228103Smm */
28228103Smm
29228103Smm#include <stdio.h>
30228103Smm#include <stdlib.h>
31228103Smm#include <strings.h>
32228103Smm#include <unistd.h>
33228103Smm#include <stddef.h>
34228103Smm#include <libintl.h>
35228103Smm#include <libzfs.h>
36228103Smm
37228103Smm#include "libzfs_impl.h"
38228103Smm
39228103Smmint
40228103Smmzfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
41228103Smm{
42228103Smm	nvlist_t *nvl = zfs_get_clones_nvl(zhp);
43228103Smm	nvpair_t *pair;
44228103Smm
45228103Smm	if (nvl == NULL)
46228103Smm		return (0);
47228103Smm
48228103Smm	for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
49228103Smm	    pair = nvlist_next_nvpair(nvl, pair)) {
50228103Smm		zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
51228103Smm		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
52228103Smm		if (clone != NULL) {
53228103Smm			int err = func(clone, data);
54228103Smm			if (err != 0)
55228103Smm				return (err);
56228103Smm		}
57228103Smm	}
58228103Smm	return (0);
59228103Smm}
60228103Smm
61228103Smmstatic int
62228103Smmzfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
63228103Smm{
64228103Smm	int rc;
65228103Smm	uint64_t	orig_cookie;
66228103Smm
67228103Smm	orig_cookie = zc->zc_cookie;
68228103Smmtop:
69228103Smm	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
70228103Smm	rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
71228103Smm
72228103Smm	if (rc == -1) {
73228103Smm		switch (errno) {
74228103Smm		case ENOMEM:
75228103Smm			/* expand nvlist memory and try again */
76228103Smm			if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
77228103Smm				zcmd_free_nvlists(zc);
78228103Smm				return (-1);
79228103Smm			}
80228103Smm			zc->zc_cookie = orig_cookie;
81228103Smm			goto top;
82228103Smm		/*
83228103Smm		 * An errno value of ESRCH indicates normal completion.
84228103Smm		 * If ENOENT is returned, then the underlying dataset
85228103Smm		 * has been removed since we obtained the handle.
86228103Smm		 */
87228103Smm		case ESRCH:
88228103Smm		case ENOENT:
89228103Smm			rc = 1;
90228103Smm			break;
91228103Smm		default:
92228103Smm			rc = zfs_standard_error(zhp->zfs_hdl, errno,
93228103Smm			    dgettext(TEXT_DOMAIN,
94228103Smm			    "cannot iterate filesystems"));
95228103Smm			break;
96228103Smm		}
97228103Smm	}
98228103Smm	return (rc);
99228103Smm}
100228103Smm
101228103Smm/*
102228103Smm * Iterate over all child filesystems
103228103Smm */
104228103Smmint
105228103Smmzfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
106228103Smm{
107228103Smm	zfs_cmd_t zc = { 0 };
108228103Smm	zfs_handle_t *nzhp;
109228103Smm	int ret;
110228103Smm
111228103Smm	if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
112228103Smm		return (0);
113228103Smm
114228103Smm	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
115228103Smm		return (-1);
116228103Smm
117228103Smm	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
118228103Smm	    &zc)) == 0) {
119228103Smm		/*
120228103Smm		 * Silently ignore errors, as the only plausible explanation is
121228103Smm		 * that the pool has since been removed.
122228103Smm		 */
123228103Smm		if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
124228103Smm		    &zc)) == NULL) {
125228103Smm			continue;
126228103Smm		}
127228103Smm
128228103Smm		if ((ret = func(nzhp, data)) != 0) {
129228103Smm			zcmd_free_nvlists(&zc);
130228103Smm			return (ret);
131228103Smm		}
132228103Smm	}
133228103Smm	zcmd_free_nvlists(&zc);
134228103Smm	return ((ret < 0) ? ret : 0);
135228103Smm}
136228103Smm
137228103Smm/*
138228103Smm * Iterate over all snapshots
139228103Smm */
140228103Smmint
141230438Spjdzfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
142230438Spjd    void *data)
143228103Smm{
144228103Smm	zfs_cmd_t zc = { 0 };
145228103Smm	zfs_handle_t *nzhp;
146228103Smm	int ret;
147228103Smm
148260183Sdelphij	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
149260183Sdelphij	    zhp->zfs_type == ZFS_TYPE_BOOKMARK)
150228103Smm		return (0);
151228103Smm
152230438Spjd	zc.zc_simple = simple;
153230438Spjd
154228103Smm	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
155228103Smm		return (-1);
156228103Smm	while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
157228103Smm	    &zc)) == 0) {
158228103Smm
159230438Spjd		if (simple)
160230438Spjd			nzhp = make_dataset_simple_handle_zc(zhp, &zc);
161230438Spjd		else
162230438Spjd			nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
163230438Spjd		if (nzhp == NULL)
164228103Smm			continue;
165228103Smm
166228103Smm		if ((ret = func(nzhp, data)) != 0) {
167228103Smm			zcmd_free_nvlists(&zc);
168228103Smm			return (ret);
169228103Smm		}
170228103Smm	}
171228103Smm	zcmd_free_nvlists(&zc);
172228103Smm	return ((ret < 0) ? ret : 0);
173228103Smm}
174228103Smm
175228103Smm/*
176260183Sdelphij * Iterate over all bookmarks
177260183Sdelphij */
178260183Sdelphijint
179260183Sdelphijzfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
180260183Sdelphij{
181260183Sdelphij	zfs_handle_t *nzhp;
182260183Sdelphij	nvlist_t *props = NULL;
183260183Sdelphij	nvlist_t *bmarks = NULL;
184260183Sdelphij	int err;
185260183Sdelphij
186260183Sdelphij	if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
187260183Sdelphij		return (0);
188260183Sdelphij
189260183Sdelphij	/* Setup the requested properties nvlist. */
190260183Sdelphij	props = fnvlist_alloc();
191260183Sdelphij	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
192260183Sdelphij	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
193260183Sdelphij	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
194260183Sdelphij
195260183Sdelphij	if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
196260183Sdelphij		goto out;
197260183Sdelphij
198260183Sdelphij	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
199260183Sdelphij	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
200307108Smav		char name[ZFS_MAX_DATASET_NAME_LEN];
201260183Sdelphij		char *bmark_name;
202260183Sdelphij		nvlist_t *bmark_props;
203260183Sdelphij
204260183Sdelphij		bmark_name = nvpair_name(pair);
205260183Sdelphij		bmark_props = fnvpair_value_nvlist(pair);
206260183Sdelphij
207260183Sdelphij		(void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
208260183Sdelphij		    bmark_name);
209260183Sdelphij
210260183Sdelphij		nzhp = make_bookmark_handle(zhp, name, bmark_props);
211260183Sdelphij		if (nzhp == NULL)
212260183Sdelphij			continue;
213260183Sdelphij
214260183Sdelphij		if ((err = func(nzhp, data)) != 0)
215260183Sdelphij			goto out;
216260183Sdelphij	}
217260183Sdelphij
218260183Sdelphijout:
219260183Sdelphij	fnvlist_free(props);
220260183Sdelphij	fnvlist_free(bmarks);
221260183Sdelphij
222260183Sdelphij	return (err);
223260183Sdelphij}
224260183Sdelphij
225260183Sdelphij/*
226228103Smm * Routines for dealing with the sorted snapshot functionality
227228103Smm */
228228103Smmtypedef struct zfs_node {
229228103Smm	zfs_handle_t	*zn_handle;
230228103Smm	avl_node_t	zn_avlnode;
231228103Smm} zfs_node_t;
232228103Smm
233228103Smmstatic int
234228103Smmzfs_sort_snaps(zfs_handle_t *zhp, void *data)
235228103Smm{
236228103Smm	avl_tree_t *avl = data;
237228103Smm	zfs_node_t *node;
238228103Smm	zfs_node_t search;
239228103Smm
240228103Smm	search.zn_handle = zhp;
241228103Smm	node = avl_find(avl, &search, NULL);
242228103Smm	if (node) {
243228103Smm		/*
244228103Smm		 * If this snapshot was renamed while we were creating the
245228103Smm		 * AVL tree, it's possible that we already inserted it under
246228103Smm		 * its old name. Remove the old handle before adding the new
247228103Smm		 * one.
248228103Smm		 */
249228103Smm		zfs_close(node->zn_handle);
250228103Smm		avl_remove(avl, node);
251228103Smm		free(node);
252228103Smm	}
253228103Smm
254228103Smm	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
255228103Smm	node->zn_handle = zhp;
256228103Smm	avl_add(avl, node);
257228103Smm
258228103Smm	return (0);
259228103Smm}
260228103Smm
261228103Smmstatic int
262228103Smmzfs_snapshot_compare(const void *larg, const void *rarg)
263228103Smm{
264228103Smm	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
265228103Smm	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
266228103Smm	uint64_t lcreate, rcreate;
267228103Smm
268228103Smm	/*
269228103Smm	 * Sort them according to creation time.  We use the hidden
270228103Smm	 * CREATETXG property to get an absolute ordering of snapshots.
271228103Smm	 */
272228103Smm	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
273228103Smm	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
274228103Smm
275339158Smav	return (AVL_CMP(lcreate, rcreate));
276228103Smm}
277228103Smm
278228103Smmint
279228103Smmzfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
280228103Smm{
281228103Smm	int ret = 0;
282228103Smm	zfs_node_t *node;
283228103Smm	avl_tree_t avl;
284228103Smm	void *cookie = NULL;
285228103Smm
286228103Smm	avl_create(&avl, zfs_snapshot_compare,
287228103Smm	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
288228103Smm
289230438Spjd	ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
290228103Smm
291228103Smm	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
292228103Smm		ret |= callback(node->zn_handle, data);
293228103Smm
294228103Smm	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
295228103Smm		free(node);
296228103Smm
297228103Smm	avl_destroy(&avl);
298228103Smm
299228103Smm	return (ret);
300228103Smm}
301228103Smm
302228103Smmtypedef struct {
303228103Smm	char *ssa_first;
304228103Smm	char *ssa_last;
305228103Smm	boolean_t ssa_seenfirst;
306228103Smm	boolean_t ssa_seenlast;
307228103Smm	zfs_iter_f ssa_func;
308228103Smm	void *ssa_arg;
309228103Smm} snapspec_arg_t;
310228103Smm
311228103Smmstatic int
312289562Smavsnapspec_cb(zfs_handle_t *zhp, void *arg)
313289562Smav{
314228103Smm	snapspec_arg_t *ssa = arg;
315331394Smav	const char *shortsnapname;
316228103Smm	int err = 0;
317228103Smm
318228103Smm	if (ssa->ssa_seenlast)
319228103Smm		return (0);
320228103Smm
321331394Smav	shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
322228103Smm	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
323228103Smm		ssa->ssa_seenfirst = B_TRUE;
324331394Smav	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
325331394Smav		ssa->ssa_seenlast = B_TRUE;
326228103Smm
327228103Smm	if (ssa->ssa_seenfirst) {
328228103Smm		err = ssa->ssa_func(zhp, ssa->ssa_arg);
329228103Smm	} else {
330228103Smm		zfs_close(zhp);
331228103Smm	}
332228103Smm
333228103Smm	return (err);
334228103Smm}
335228103Smm
336228103Smm/*
337228103Smm * spec is a string like "A,B%C,D"
338228103Smm *
339228103Smm * <snaps>, where <snaps> can be:
340228103Smm *      <snap>          (single snapshot)
341228103Smm *      <snap>%<snap>   (range of snapshots, inclusive)
342228103Smm *      %<snap>         (range of snapshots, starting with earliest)
343228103Smm *      <snap>%         (range of snapshots, ending with last)
344228103Smm *      %               (all snapshots)
345228103Smm *      <snaps>[,...]   (comma separated list of the above)
346228103Smm *
347228103Smm * If a snapshot can not be opened, continue trying to open the others, but
348228103Smm * return ENOENT at the end.
349228103Smm */
350228103Smmint
351228103Smmzfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
352228103Smm    zfs_iter_f func, void *arg)
353228103Smm{
354248571Smm	char *buf, *comma_separated, *cp;
355228103Smm	int err = 0;
356228103Smm	int ret = 0;
357228103Smm
358248571Smm	buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
359228103Smm	cp = buf;
360228103Smm
361228103Smm	while ((comma_separated = strsep(&cp, ",")) != NULL) {
362228103Smm		char *pct = strchr(comma_separated, '%');
363228103Smm		if (pct != NULL) {
364228103Smm			snapspec_arg_t ssa = { 0 };
365228103Smm			ssa.ssa_func = func;
366228103Smm			ssa.ssa_arg = arg;
367228103Smm
368228103Smm			if (pct == comma_separated)
369228103Smm				ssa.ssa_seenfirst = B_TRUE;
370228103Smm			else
371228103Smm				ssa.ssa_first = comma_separated;
372228103Smm			*pct = '\0';
373228103Smm			ssa.ssa_last = pct + 1;
374228103Smm
375228103Smm			/*
376228103Smm			 * If there is a lastname specified, make sure it
377228103Smm			 * exists.
378228103Smm			 */
379228103Smm			if (ssa.ssa_last[0] != '\0') {
380307108Smav				char snapname[ZFS_MAX_DATASET_NAME_LEN];
381228103Smm				(void) snprintf(snapname, sizeof (snapname),
382228103Smm				    "%s@%s", zfs_get_name(fs_zhp),
383228103Smm				    ssa.ssa_last);
384228103Smm				if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
385228103Smm				    snapname, ZFS_TYPE_SNAPSHOT)) {
386228103Smm					ret = ENOENT;
387228103Smm					continue;
388228103Smm				}
389228103Smm			}
390228103Smm
391228103Smm			err = zfs_iter_snapshots_sorted(fs_zhp,
392228103Smm			    snapspec_cb, &ssa);
393228103Smm			if (ret == 0)
394228103Smm				ret = err;
395228103Smm			if (ret == 0 && (!ssa.ssa_seenfirst ||
396228103Smm			    (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
397228103Smm				ret = ENOENT;
398228103Smm			}
399228103Smm		} else {
400307108Smav			char snapname[ZFS_MAX_DATASET_NAME_LEN];
401228103Smm			zfs_handle_t *snap_zhp;
402228103Smm			(void) snprintf(snapname, sizeof (snapname), "%s@%s",
403228103Smm			    zfs_get_name(fs_zhp), comma_separated);
404228103Smm			snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
405228103Smm			    snapname);
406228103Smm			if (snap_zhp == NULL) {
407228103Smm				ret = ENOENT;
408228103Smm				continue;
409228103Smm			}
410228103Smm			err = func(snap_zhp, arg);
411228103Smm			if (ret == 0)
412228103Smm				ret = err;
413228103Smm		}
414228103Smm	}
415228103Smm
416248571Smm	free(buf);
417228103Smm	return (ret);
418228103Smm}
419228103Smm
420228103Smm/*
421228103Smm * Iterate over all children, snapshots and filesystems
422332539Smav * Process snapshots before filesystems because they are nearer the input
423332539Smav * handle: this is extremely important when used with zfs_iter_f functions
424332539Smav * looking for data, following the logic that we would like to find it as soon
425332539Smav * and as close as possible.
426228103Smm */
427228103Smmint
428228103Smmzfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
429228103Smm{
430228103Smm	int ret;
431228103Smm
432332539Smav	if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
433228103Smm		return (ret);
434228103Smm
435332539Smav	return (zfs_iter_filesystems(zhp, func, data));
436228103Smm}
437228103Smm
438228103Smm
439228103Smmtypedef struct iter_stack_frame {
440228103Smm	struct iter_stack_frame *next;
441228103Smm	zfs_handle_t *zhp;
442228103Smm} iter_stack_frame_t;
443228103Smm
444228103Smmtypedef struct iter_dependents_arg {
445228103Smm	boolean_t first;
446228103Smm	boolean_t allowrecursion;
447228103Smm	iter_stack_frame_t *stack;
448228103Smm	zfs_iter_f func;
449228103Smm	void *data;
450228103Smm} iter_dependents_arg_t;
451228103Smm
452228103Smmstatic int
453228103Smmiter_dependents_cb(zfs_handle_t *zhp, void *arg)
454228103Smm{
455228103Smm	iter_dependents_arg_t *ida = arg;
456260183Sdelphij	int err = 0;
457228103Smm	boolean_t first = ida->first;
458228103Smm	ida->first = B_FALSE;
459228103Smm
460228103Smm	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
461228103Smm		err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
462260183Sdelphij	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
463228103Smm		iter_stack_frame_t isf;
464228103Smm		iter_stack_frame_t *f;
465228103Smm
466228103Smm		/*
467228103Smm		 * check if there is a cycle by seeing if this fs is already
468228103Smm		 * on the stack.
469228103Smm		 */
470228103Smm		for (f = ida->stack; f != NULL; f = f->next) {
471228103Smm			if (f->zhp->zfs_dmustats.dds_guid ==
472228103Smm			    zhp->zfs_dmustats.dds_guid) {
473228103Smm				if (ida->allowrecursion) {
474228103Smm					zfs_close(zhp);
475228103Smm					return (0);
476228103Smm				} else {
477228103Smm					zfs_error_aux(zhp->zfs_hdl,
478228103Smm					    dgettext(TEXT_DOMAIN,
479228103Smm					    "recursive dependency at '%s'"),
480228103Smm					    zfs_get_name(zhp));
481228103Smm					err = zfs_error(zhp->zfs_hdl,
482228103Smm					    EZFS_RECURSIVE,
483228103Smm					    dgettext(TEXT_DOMAIN,
484228103Smm					    "cannot determine dependent "
485228103Smm					    "datasets"));
486228103Smm					zfs_close(zhp);
487228103Smm					return (err);
488228103Smm				}
489228103Smm			}
490228103Smm		}
491228103Smm
492228103Smm		isf.zhp = zhp;
493228103Smm		isf.next = ida->stack;
494228103Smm		ida->stack = &isf;
495228103Smm		err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
496230438Spjd		if (err == 0) {
497230438Spjd			err = zfs_iter_snapshots(zhp, B_FALSE,
498230438Spjd			    iter_dependents_cb, ida);
499230438Spjd		}
500228103Smm		ida->stack = isf.next;
501228103Smm	}
502254755Sdelphij
503228103Smm	if (!first && err == 0)
504228103Smm		err = ida->func(zhp, ida->data);
505254755Sdelphij	else
506254755Sdelphij		zfs_close(zhp);
507254755Sdelphij
508228103Smm	return (err);
509228103Smm}
510228103Smm
511228103Smmint
512228103Smmzfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
513228103Smm    zfs_iter_f func, void *data)
514228103Smm{
515228103Smm	iter_dependents_arg_t ida;
516228103Smm	ida.allowrecursion = allowrecursion;
517228103Smm	ida.stack = NULL;
518228103Smm	ida.func = func;
519228103Smm	ida.data = data;
520228103Smm	ida.first = B_TRUE;
521228103Smm	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
522228103Smm}
523