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