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