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