Fix memory leak in zil_parse().
[zfs.git] / lib / libzfs / libzfs_iter.c
blob452d8fd6ab71c806f0c0d26d4243ef339ac442b0
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
25 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2019 Datto Inc.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stddef.h>
34 #include <libintl.h>
35 #include <libzfs.h>
36 #include <libzutil.h>
37 #include <sys/mntent.h>
39 #include "libzfs_impl.h"
41 static int
42 zfs_iter_clones(zfs_handle_t *zhp, int flags __maybe_unused, zfs_iter_f func,
43 void *data)
45 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
46 nvpair_t *pair;
48 if (nvl == NULL)
49 return (0);
51 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
52 pair = nvlist_next_nvpair(nvl, pair)) {
53 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
54 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
55 if (clone != NULL) {
56 int err = func(clone, data);
57 if (err != 0)
58 return (err);
61 return (0);
64 static int
65 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
67 int rc;
68 uint64_t orig_cookie;
70 orig_cookie = zc->zc_cookie;
71 top:
72 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
73 zc->zc_objset_stats.dds_creation_txg = 0;
74 rc = zfs_ioctl(zhp->zfs_hdl, arg, zc);
76 if (rc == -1) {
77 switch (errno) {
78 case ENOMEM:
79 /* expand nvlist memory and try again */
80 zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc);
81 zc->zc_cookie = orig_cookie;
82 goto top;
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.
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;
99 return (rc);
103 * Iterate over all child filesystems
106 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
108 return (zfs_iter_filesystems_v2(zhp, 0, func, data));
112 zfs_iter_filesystems_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func,
113 void *data)
115 zfs_cmd_t zc = {"\0"};
116 zfs_handle_t *nzhp;
117 int ret;
119 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
120 return (0);
122 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
124 if ((flags & ZFS_ITER_SIMPLE) == ZFS_ITER_SIMPLE)
125 zc.zc_simple = B_TRUE;
127 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
128 &zc)) == 0) {
129 if (zc.zc_simple)
130 nzhp = make_dataset_simple_handle_zc(zhp, &zc);
131 else
132 nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
134 * Silently ignore errors, as the only plausible explanation is
135 * that the pool has since been removed.
137 if (nzhp == NULL)
138 continue;
140 if ((ret = func(nzhp, data)) != 0) {
141 zcmd_free_nvlists(&zc);
142 return (ret);
145 zcmd_free_nvlists(&zc);
146 return ((ret < 0) ? ret : 0);
150 * Iterate over all snapshots
153 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
154 void *data, uint64_t min_txg, uint64_t max_txg)
156 return (zfs_iter_snapshots_v2(zhp, simple ? ZFS_ITER_SIMPLE : 0, func,
157 data, min_txg, max_txg));
161 zfs_iter_snapshots_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func,
162 void *data, uint64_t min_txg, uint64_t max_txg)
164 zfs_cmd_t zc = {"\0"};
165 zfs_handle_t *nzhp;
166 int ret;
167 nvlist_t *range_nvl = NULL;
169 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
170 zhp->zfs_type == ZFS_TYPE_BOOKMARK)
171 return (0);
173 zc.zc_simple = (flags & ZFS_ITER_SIMPLE) != 0;
175 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
177 if (min_txg != 0) {
178 range_nvl = fnvlist_alloc();
179 fnvlist_add_uint64(range_nvl, SNAP_ITER_MIN_TXG, min_txg);
181 if (max_txg != 0) {
182 if (range_nvl == NULL)
183 range_nvl = fnvlist_alloc();
184 fnvlist_add_uint64(range_nvl, SNAP_ITER_MAX_TXG, max_txg);
187 if (range_nvl != NULL)
188 zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl);
190 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
191 &zc)) == 0) {
193 if (zc.zc_simple)
194 nzhp = make_dataset_simple_handle_zc(zhp, &zc);
195 else
196 nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
197 if (nzhp == NULL)
198 continue;
200 if ((ret = func(nzhp, data)) != 0) {
201 zcmd_free_nvlists(&zc);
202 fnvlist_free(range_nvl);
203 return (ret);
206 zcmd_free_nvlists(&zc);
207 fnvlist_free(range_nvl);
208 return ((ret < 0) ? ret : 0);
212 * Iterate over all bookmarks
215 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
217 return (zfs_iter_bookmarks_v2(zhp, 0, func, data));
221 zfs_iter_bookmarks_v2(zfs_handle_t *zhp, int flags __maybe_unused,
222 zfs_iter_f func, void *data)
224 zfs_handle_t *nzhp;
225 nvlist_t *props = NULL;
226 nvlist_t *bmarks = NULL;
227 int err;
228 nvpair_t *pair;
230 if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
231 return (0);
233 /* Setup the requested properties nvlist. */
234 props = fnvlist_alloc();
235 for (zfs_prop_t p = 0; p < ZFS_NUM_PROPS; p++) {
236 if (zfs_prop_valid_for_type(p, ZFS_TYPE_BOOKMARK, B_FALSE)) {
237 fnvlist_add_boolean(props, zfs_prop_to_name(p));
240 fnvlist_add_boolean(props, "redact_complete");
242 if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
243 goto out;
245 for (pair = nvlist_next_nvpair(bmarks, NULL);
246 pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
247 char name[ZFS_MAX_DATASET_NAME_LEN];
248 const char *bmark_name;
249 nvlist_t *bmark_props;
251 bmark_name = nvpair_name(pair);
252 bmark_props = fnvpair_value_nvlist(pair);
254 if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
255 bmark_name) >= sizeof (name)) {
256 err = EINVAL;
257 goto out;
260 nzhp = make_bookmark_handle(zhp, name, bmark_props);
261 if (nzhp == NULL)
262 continue;
264 if ((err = func(nzhp, data)) != 0)
265 goto out;
268 out:
269 fnvlist_free(props);
270 fnvlist_free(bmarks);
272 return (err);
276 * Routines for dealing with the sorted snapshot functionality
278 typedef struct zfs_node {
279 zfs_handle_t *zn_handle;
280 avl_node_t zn_avlnode;
281 } zfs_node_t;
283 static int
284 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
286 avl_tree_t *avl = data;
287 zfs_node_t *node;
288 zfs_node_t search;
290 search.zn_handle = zhp;
291 node = avl_find(avl, &search, NULL);
292 if (node) {
294 * If this snapshot was renamed while we were creating the
295 * AVL tree, it's possible that we already inserted it under
296 * its old name. Remove the old handle before adding the new
297 * one.
299 zfs_close(node->zn_handle);
300 avl_remove(avl, node);
301 free(node);
304 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
305 node->zn_handle = zhp;
306 avl_add(avl, node);
308 return (0);
311 static int
312 zfs_snapshot_compare(const void *larg, const void *rarg)
314 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
315 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
316 uint64_t lcreate, rcreate;
319 * Sort them according to creation time. We use the hidden
320 * CREATETXG property to get an absolute ordering of snapshots.
322 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
323 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
325 return (TREE_CMP(lcreate, rcreate));
329 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback,
330 void *data, uint64_t min_txg, uint64_t max_txg)
332 return (zfs_iter_snapshots_sorted_v2(zhp, 0, callback, data,
333 min_txg, max_txg));
337 zfs_iter_snapshots_sorted_v2(zfs_handle_t *zhp, int flags, zfs_iter_f callback,
338 void *data, uint64_t min_txg, uint64_t max_txg)
340 int ret = 0;
341 zfs_node_t *node;
342 avl_tree_t avl;
343 void *cookie = NULL;
345 avl_create(&avl, zfs_snapshot_compare,
346 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
348 ret = zfs_iter_snapshots_v2(zhp, flags, zfs_sort_snaps, &avl, min_txg,
349 max_txg);
351 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
352 ret |= callback(node->zn_handle, data);
354 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
355 free(node);
357 avl_destroy(&avl);
359 return (ret);
362 typedef struct {
363 char *ssa_first;
364 char *ssa_last;
365 boolean_t ssa_seenfirst;
366 boolean_t ssa_seenlast;
367 zfs_iter_f ssa_func;
368 void *ssa_arg;
369 } snapspec_arg_t;
371 static int
372 snapspec_cb(zfs_handle_t *zhp, void *arg)
374 snapspec_arg_t *ssa = arg;
375 const char *shortsnapname;
376 int err = 0;
378 if (ssa->ssa_seenlast)
379 return (0);
381 shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
382 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
383 ssa->ssa_seenfirst = B_TRUE;
384 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
385 ssa->ssa_seenlast = B_TRUE;
387 if (ssa->ssa_seenfirst) {
388 err = ssa->ssa_func(zhp, ssa->ssa_arg);
389 } else {
390 zfs_close(zhp);
393 return (err);
397 * spec is a string like "A,B%C,D"
399 * <snaps>, where <snaps> can be:
400 * <snap> (single snapshot)
401 * <snap>%<snap> (range of snapshots, inclusive)
402 * %<snap> (range of snapshots, starting with earliest)
403 * <snap>% (range of snapshots, ending with last)
404 * % (all snapshots)
405 * <snaps>[,...] (comma separated list of the above)
407 * If a snapshot can not be opened, continue trying to open the others, but
408 * return ENOENT at the end.
411 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
412 zfs_iter_f func, void *arg)
414 return (zfs_iter_snapspec_v2(fs_zhp, 0, spec_orig, func, arg));
418 zfs_iter_snapspec_v2(zfs_handle_t *fs_zhp, int flags, const char *spec_orig,
419 zfs_iter_f func, void *arg)
421 char *buf, *comma_separated, *cp;
422 int err = 0;
423 int ret = 0;
425 buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
426 cp = buf;
428 while ((comma_separated = strsep(&cp, ",")) != NULL) {
429 char *pct = strchr(comma_separated, '%');
430 if (pct != NULL) {
431 snapspec_arg_t ssa = { 0 };
432 ssa.ssa_func = func;
433 ssa.ssa_arg = arg;
435 if (pct == comma_separated)
436 ssa.ssa_seenfirst = B_TRUE;
437 else
438 ssa.ssa_first = comma_separated;
439 *pct = '\0';
440 ssa.ssa_last = pct + 1;
443 * If there is a lastname specified, make sure it
444 * exists.
446 if (ssa.ssa_last[0] != '\0') {
447 char snapname[ZFS_MAX_DATASET_NAME_LEN];
448 (void) snprintf(snapname, sizeof (snapname),
449 "%s@%s", zfs_get_name(fs_zhp),
450 ssa.ssa_last);
451 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
452 snapname, ZFS_TYPE_SNAPSHOT)) {
453 ret = ENOENT;
454 continue;
458 err = zfs_iter_snapshots_sorted_v2(fs_zhp, flags,
459 snapspec_cb, &ssa, 0, 0);
460 if (ret == 0)
461 ret = err;
462 if (ret == 0 && (!ssa.ssa_seenfirst ||
463 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
464 ret = ENOENT;
466 } else {
467 char snapname[ZFS_MAX_DATASET_NAME_LEN];
468 zfs_handle_t *snap_zhp;
469 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
470 zfs_get_name(fs_zhp), comma_separated);
471 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
472 snapname);
473 if (snap_zhp == NULL) {
474 ret = ENOENT;
475 continue;
477 err = func(snap_zhp, arg);
478 if (ret == 0)
479 ret = err;
483 free(buf);
484 return (ret);
488 * Iterate over all children, snapshots and filesystems
489 * Process snapshots before filesystems because they are nearer the input
490 * handle: this is extremely important when used with zfs_iter_f functions
491 * looking for data, following the logic that we would like to find it as soon
492 * and as close as possible.
495 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
497 return (zfs_iter_children_v2(zhp, 0, func, data));
501 zfs_iter_children_v2(zfs_handle_t *zhp, int flags, zfs_iter_f func, void *data)
503 int ret;
505 if ((ret = zfs_iter_snapshots_v2(zhp, flags, func, data, 0, 0)) != 0)
506 return (ret);
508 return (zfs_iter_filesystems_v2(zhp, flags, func, data));
512 typedef struct iter_stack_frame {
513 struct iter_stack_frame *next;
514 zfs_handle_t *zhp;
515 } iter_stack_frame_t;
517 typedef struct iter_dependents_arg {
518 boolean_t first;
519 int flags;
520 boolean_t allowrecursion;
521 iter_stack_frame_t *stack;
522 zfs_iter_f func;
523 void *data;
524 } iter_dependents_arg_t;
526 static int
527 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
529 iter_dependents_arg_t *ida = arg;
530 int err = 0;
531 boolean_t first = ida->first;
532 ida->first = B_FALSE;
534 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
535 err = zfs_iter_clones(zhp, ida->flags, iter_dependents_cb, ida);
536 } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
537 iter_stack_frame_t isf;
538 iter_stack_frame_t *f;
541 * check if there is a cycle by seeing if this fs is already
542 * on the stack.
544 for (f = ida->stack; f != NULL; f = f->next) {
545 if (f->zhp->zfs_dmustats.dds_guid ==
546 zhp->zfs_dmustats.dds_guid) {
547 if (ida->allowrecursion) {
548 zfs_close(zhp);
549 return (0);
550 } else {
551 zfs_error_aux(zhp->zfs_hdl,
552 dgettext(TEXT_DOMAIN,
553 "recursive dependency at '%s'"),
554 zfs_get_name(zhp));
555 err = zfs_error(zhp->zfs_hdl,
556 EZFS_RECURSIVE,
557 dgettext(TEXT_DOMAIN,
558 "cannot determine dependent "
559 "datasets"));
560 zfs_close(zhp);
561 return (err);
566 isf.zhp = zhp;
567 isf.next = ida->stack;
568 ida->stack = &isf;
569 err = zfs_iter_filesystems_v2(zhp, ida->flags,
570 iter_dependents_cb, ida);
571 if (err == 0)
572 err = zfs_iter_snapshots_v2(zhp, ida->flags,
573 iter_dependents_cb, ida, 0, 0);
574 ida->stack = isf.next;
577 if (!first && err == 0)
578 err = ida->func(zhp, ida->data);
579 else
580 zfs_close(zhp);
582 return (err);
586 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
587 zfs_iter_f func, void *data)
589 return (zfs_iter_dependents_v2(zhp, 0, allowrecursion, func, data));
593 zfs_iter_dependents_v2(zfs_handle_t *zhp, int flags, boolean_t allowrecursion,
594 zfs_iter_f func, void *data)
596 iter_dependents_arg_t ida;
597 ida.flags = flags;
598 ida.allowrecursion = allowrecursion;
599 ida.stack = NULL;
600 ida.func = func;
601 ida.data = data;
602 ida.first = B_TRUE;
603 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
607 * Iterate over mounted children of the specified dataset
610 zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
612 char mnt_prop[ZFS_MAXPROPLEN];
613 struct mnttab entry;
614 zfs_handle_t *mtab_zhp;
615 size_t namelen = strlen(zhp->zfs_name);
616 FILE *mnttab;
617 int err = 0;
619 if ((mnttab = fopen(MNTTAB, "re")) == NULL)
620 return (ENOENT);
622 while (err == 0 && getmntent(mnttab, &entry) == 0) {
623 /* Ignore non-ZFS entries */
624 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
625 continue;
627 /* Ignore datasets not within the provided dataset */
628 if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 ||
629 entry.mnt_special[namelen] != '/')
630 continue;
632 /* Skip snapshot of any child dataset */
633 if (strchr(entry.mnt_special, '@') != NULL)
634 continue;
636 if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special,
637 ZFS_TYPE_FILESYSTEM)) == NULL)
638 continue;
640 /* Ignore legacy mounts as they are user managed */
641 verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop,
642 sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
643 if (strcmp(mnt_prop, "legacy") == 0) {
644 zfs_close(mtab_zhp);
645 continue;
648 err = func(mtab_zhp, data);
651 fclose(mnttab);
653 return (err);