xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_iter.c (revision b6831eafa2ec2cb283f783b06ee86bf79d5a17c3)
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 2014 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 	if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
190 		goto out;
191 
192 	for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
193 	    pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
194 		char name[ZFS_MAXNAMELEN];
195 		char *bmark_name;
196 		nvlist_t *bmark_props;
197 
198 		bmark_name = nvpair_name(pair);
199 		bmark_props = fnvpair_value_nvlist(pair);
200 
201 		(void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
202 		    bmark_name);
203 
204 		nzhp = make_bookmark_handle(zhp, name, bmark_props);
205 		if (nzhp == NULL)
206 			continue;
207 
208 		if ((err = func(nzhp, data)) != 0)
209 			goto out;
210 	}
211 
212 out:
213 	fnvlist_free(props);
214 	fnvlist_free(bmarks);
215 
216 	return (err);
217 }
218 
219 /*
220  * Routines for dealing with the sorted snapshot functionality
221  */
222 typedef struct zfs_node {
223 	zfs_handle_t	*zn_handle;
224 	avl_node_t	zn_avlnode;
225 } zfs_node_t;
226 
227 static int
228 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
229 {
230 	avl_tree_t *avl = data;
231 	zfs_node_t *node;
232 	zfs_node_t search;
233 
234 	search.zn_handle = zhp;
235 	node = avl_find(avl, &search, NULL);
236 	if (node) {
237 		/*
238 		 * If this snapshot was renamed while we were creating the
239 		 * AVL tree, it's possible that we already inserted it under
240 		 * its old name. Remove the old handle before adding the new
241 		 * one.
242 		 */
243 		zfs_close(node->zn_handle);
244 		avl_remove(avl, node);
245 		free(node);
246 	}
247 
248 	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
249 	node->zn_handle = zhp;
250 	avl_add(avl, node);
251 
252 	return (0);
253 }
254 
255 static int
256 zfs_snapshot_compare(const void *larg, const void *rarg)
257 {
258 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
259 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
260 	uint64_t lcreate, rcreate;
261 
262 	/*
263 	 * Sort them according to creation time.  We use the hidden
264 	 * CREATETXG property to get an absolute ordering of snapshots.
265 	 */
266 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
267 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
268 
269 	if (lcreate < rcreate)
270 		return (-1);
271 	else if (lcreate > rcreate)
272 		return (+1);
273 	else
274 		return (0);
275 }
276 
277 int
278 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
279 {
280 	int ret = 0;
281 	zfs_node_t *node;
282 	avl_tree_t avl;
283 	void *cookie = NULL;
284 
285 	avl_create(&avl, zfs_snapshot_compare,
286 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
287 
288 	ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
289 
290 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
291 		ret |= callback(node->zn_handle, data);
292 
293 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
294 		free(node);
295 
296 	avl_destroy(&avl);
297 
298 	return (ret);
299 }
300 
301 typedef struct {
302 	char *ssa_first;
303 	char *ssa_last;
304 	boolean_t ssa_seenfirst;
305 	boolean_t ssa_seenlast;
306 	zfs_iter_f ssa_func;
307 	void *ssa_arg;
308 } snapspec_arg_t;
309 
310 static int
311 snapspec_cb(zfs_handle_t *zhp, void *arg) {
312 	snapspec_arg_t *ssa = arg;
313 	char *shortsnapname;
314 	int err = 0;
315 
316 	if (ssa->ssa_seenlast)
317 		return (0);
318 	shortsnapname = zfs_strdup(zhp->zfs_hdl,
319 	    strchr(zfs_get_name(zhp), '@') + 1);
320 
321 	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
322 		ssa->ssa_seenfirst = B_TRUE;
323 
324 	if (ssa->ssa_seenfirst) {
325 		err = ssa->ssa_func(zhp, ssa->ssa_arg);
326 	} else {
327 		zfs_close(zhp);
328 	}
329 
330 	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
331 		ssa->ssa_seenlast = B_TRUE;
332 	free(shortsnapname);
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_MAXNAMELEN];
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_MAXNAMELEN];
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  */
424 int
425 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
426 {
427 	int ret;
428 
429 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
430 		return (ret);
431 
432 	return (zfs_iter_snapshots(zhp, func, data));
433 }
434 
435 
436 typedef struct iter_stack_frame {
437 	struct iter_stack_frame *next;
438 	zfs_handle_t *zhp;
439 } iter_stack_frame_t;
440 
441 typedef struct iter_dependents_arg {
442 	boolean_t first;
443 	boolean_t allowrecursion;
444 	iter_stack_frame_t *stack;
445 	zfs_iter_f func;
446 	void *data;
447 } iter_dependents_arg_t;
448 
449 static int
450 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
451 {
452 	iter_dependents_arg_t *ida = arg;
453 	int err = 0;
454 	boolean_t first = ida->first;
455 	ida->first = B_FALSE;
456 
457 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
458 		err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
459 	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
460 		iter_stack_frame_t isf;
461 		iter_stack_frame_t *f;
462 
463 		/*
464 		 * check if there is a cycle by seeing if this fs is already
465 		 * on the stack.
466 		 */
467 		for (f = ida->stack; f != NULL; f = f->next) {
468 			if (f->zhp->zfs_dmustats.dds_guid ==
469 			    zhp->zfs_dmustats.dds_guid) {
470 				if (ida->allowrecursion) {
471 					zfs_close(zhp);
472 					return (0);
473 				} else {
474 					zfs_error_aux(zhp->zfs_hdl,
475 					    dgettext(TEXT_DOMAIN,
476 					    "recursive dependency at '%s'"),
477 					    zfs_get_name(zhp));
478 					err = zfs_error(zhp->zfs_hdl,
479 					    EZFS_RECURSIVE,
480 					    dgettext(TEXT_DOMAIN,
481 					    "cannot determine dependent "
482 					    "datasets"));
483 					zfs_close(zhp);
484 					return (err);
485 				}
486 			}
487 		}
488 
489 		isf.zhp = zhp;
490 		isf.next = ida->stack;
491 		ida->stack = &isf;
492 		err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
493 		if (err == 0)
494 			err = zfs_iter_snapshots(zhp, iter_dependents_cb, ida);
495 		ida->stack = isf.next;
496 	}
497 
498 	if (!first && err == 0)
499 		err = ida->func(zhp, ida->data);
500 	else
501 		zfs_close(zhp);
502 
503 	return (err);
504 }
505 
506 int
507 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
508     zfs_iter_f func, void *data)
509 {
510 	iter_dependents_arg_t ida;
511 	ida.allowrecursion = allowrecursion;
512 	ida.stack = NULL;
513 	ida.func = func;
514 	ida.data = data;
515 	ida.first = B_TRUE;
516 	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
517 }
518