xref: /titanic_50/usr/src/lib/libzfs/common/libzfs_iter.c (revision 40a5c998e5462ed47916e2edfcc5016073cf8851)
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 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
zfs_iter_clones(zfs_handle_t * zhp,zfs_iter_f func,void * data)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
zfs_do_list_ioctl(zfs_handle_t * zhp,int arg,zfs_cmd_t * zc)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
zfs_iter_filesystems(zfs_handle_t * zhp,zfs_iter_f func,void * data)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
zfs_iter_snapshots(zfs_handle_t * zhp,zfs_iter_f func,void * data)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
zfs_iter_bookmarks(zfs_handle_t * zhp,zfs_iter_f func,void * data)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_MAX_DATASET_NAME_LEN];
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
zfs_sort_snaps(zfs_handle_t * zhp,void * data)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
zfs_snapshot_compare(const void * larg,const void * rarg)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
zfs_iter_snapshots_sorted(zfs_handle_t * zhp,zfs_iter_f callback,void * data)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
snapspec_cb(zfs_handle_t * zhp,void * arg)311 snapspec_cb(zfs_handle_t *zhp, void *arg)
312 {
313 	snapspec_arg_t *ssa = arg;
314 	char *shortsnapname;
315 	int err = 0;
316 
317 	if (ssa->ssa_seenlast)
318 		return (0);
319 	shortsnapname = zfs_strdup(zhp->zfs_hdl,
320 	    strchr(zfs_get_name(zhp), '@') + 1);
321 
322 	if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
323 		ssa->ssa_seenfirst = B_TRUE;
324 
325 	if (ssa->ssa_seenfirst) {
326 		err = ssa->ssa_func(zhp, ssa->ssa_arg);
327 	} else {
328 		zfs_close(zhp);
329 	}
330 
331 	if (strcmp(shortsnapname, ssa->ssa_last) == 0)
332 		ssa->ssa_seenlast = B_TRUE;
333 	free(shortsnapname);
334 
335 	return (err);
336 }
337 
338 /*
339  * spec is a string like "A,B%C,D"
340  *
341  * <snaps>, where <snaps> can be:
342  *      <snap>          (single snapshot)
343  *      <snap>%<snap>   (range of snapshots, inclusive)
344  *      %<snap>         (range of snapshots, starting with earliest)
345  *      <snap>%         (range of snapshots, ending with last)
346  *      %               (all snapshots)
347  *      <snaps>[,...]   (comma separated list of the above)
348  *
349  * If a snapshot can not be opened, continue trying to open the others, but
350  * return ENOENT at the end.
351  */
352 int
zfs_iter_snapspec(zfs_handle_t * fs_zhp,const char * spec_orig,zfs_iter_f func,void * arg)353 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
354     zfs_iter_f func, void *arg)
355 {
356 	char *buf, *comma_separated, *cp;
357 	int err = 0;
358 	int ret = 0;
359 
360 	buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
361 	cp = buf;
362 
363 	while ((comma_separated = strsep(&cp, ",")) != NULL) {
364 		char *pct = strchr(comma_separated, '%');
365 		if (pct != NULL) {
366 			snapspec_arg_t ssa = { 0 };
367 			ssa.ssa_func = func;
368 			ssa.ssa_arg = arg;
369 
370 			if (pct == comma_separated)
371 				ssa.ssa_seenfirst = B_TRUE;
372 			else
373 				ssa.ssa_first = comma_separated;
374 			*pct = '\0';
375 			ssa.ssa_last = pct + 1;
376 
377 			/*
378 			 * If there is a lastname specified, make sure it
379 			 * exists.
380 			 */
381 			if (ssa.ssa_last[0] != '\0') {
382 				char snapname[ZFS_MAX_DATASET_NAME_LEN];
383 				(void) snprintf(snapname, sizeof (snapname),
384 				    "%s@%s", zfs_get_name(fs_zhp),
385 				    ssa.ssa_last);
386 				if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
387 				    snapname, ZFS_TYPE_SNAPSHOT)) {
388 					ret = ENOENT;
389 					continue;
390 				}
391 			}
392 
393 			err = zfs_iter_snapshots_sorted(fs_zhp,
394 			    snapspec_cb, &ssa);
395 			if (ret == 0)
396 				ret = err;
397 			if (ret == 0 && (!ssa.ssa_seenfirst ||
398 			    (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
399 				ret = ENOENT;
400 			}
401 		} else {
402 			char snapname[ZFS_MAX_DATASET_NAME_LEN];
403 			zfs_handle_t *snap_zhp;
404 			(void) snprintf(snapname, sizeof (snapname), "%s@%s",
405 			    zfs_get_name(fs_zhp), comma_separated);
406 			snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
407 			    snapname);
408 			if (snap_zhp == NULL) {
409 				ret = ENOENT;
410 				continue;
411 			}
412 			err = func(snap_zhp, arg);
413 			if (ret == 0)
414 				ret = err;
415 		}
416 	}
417 
418 	free(buf);
419 	return (ret);
420 }
421 
422 /*
423  * Iterate over all children, snapshots and filesystems
424  */
425 int
zfs_iter_children(zfs_handle_t * zhp,zfs_iter_f func,void * data)426 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
427 {
428 	int ret;
429 
430 	if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
431 		return (ret);
432 
433 	return (zfs_iter_snapshots(zhp, func, data));
434 }
435 
436 
437 typedef struct iter_stack_frame {
438 	struct iter_stack_frame *next;
439 	zfs_handle_t *zhp;
440 } iter_stack_frame_t;
441 
442 typedef struct iter_dependents_arg {
443 	boolean_t first;
444 	boolean_t allowrecursion;
445 	iter_stack_frame_t *stack;
446 	zfs_iter_f func;
447 	void *data;
448 } iter_dependents_arg_t;
449 
450 static int
iter_dependents_cb(zfs_handle_t * zhp,void * arg)451 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
452 {
453 	iter_dependents_arg_t *ida = arg;
454 	int err = 0;
455 	boolean_t first = ida->first;
456 	ida->first = B_FALSE;
457 
458 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
459 		err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
460 	} else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
461 		iter_stack_frame_t isf;
462 		iter_stack_frame_t *f;
463 
464 		/*
465 		 * check if there is a cycle by seeing if this fs is already
466 		 * on the stack.
467 		 */
468 		for (f = ida->stack; f != NULL; f = f->next) {
469 			if (f->zhp->zfs_dmustats.dds_guid ==
470 			    zhp->zfs_dmustats.dds_guid) {
471 				if (ida->allowrecursion) {
472 					zfs_close(zhp);
473 					return (0);
474 				} else {
475 					zfs_error_aux(zhp->zfs_hdl,
476 					    dgettext(TEXT_DOMAIN,
477 					    "recursive dependency at '%s'"),
478 					    zfs_get_name(zhp));
479 					err = zfs_error(zhp->zfs_hdl,
480 					    EZFS_RECURSIVE,
481 					    dgettext(TEXT_DOMAIN,
482 					    "cannot determine dependent "
483 					    "datasets"));
484 					zfs_close(zhp);
485 					return (err);
486 				}
487 			}
488 		}
489 
490 		isf.zhp = zhp;
491 		isf.next = ida->stack;
492 		ida->stack = &isf;
493 		err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
494 		if (err == 0)
495 			err = zfs_iter_snapshots(zhp, iter_dependents_cb, ida);
496 		ida->stack = isf.next;
497 	}
498 
499 	if (!first && err == 0)
500 		err = ida->func(zhp, ida->data);
501 	else
502 		zfs_close(zhp);
503 
504 	return (err);
505 }
506 
507 int
zfs_iter_dependents(zfs_handle_t * zhp,boolean_t allowrecursion,zfs_iter_f func,void * data)508 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
509     zfs_iter_f func, void *data)
510 {
511 	iter_dependents_arg_t ida;
512 	ida.allowrecursion = allowrecursion;
513 	ida.stack = NULL;
514 	ida.func = func;
515 	ida.data = data;
516 	ida.first = B_TRUE;
517 	return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
518 }
519