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 http://www.opensolaris.org/os/licensing.
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]
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.
37 #include "libzfs_impl.h"
40 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
42 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
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);
53 int err = func(clone, data);
62 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
67 orig_cookie = zc->zc_cookie;
69 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
70 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
75 /* expand nvlist memory and try again */
76 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
77 zcmd_free_nvlists(zc);
80 zc->zc_cookie = orig_cookie;
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.
92 rc = zfs_standard_error(zhp->zfs_hdl, errno,
94 "cannot iterate filesystems"));
102 * Iterate over all child filesystems
105 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
107 zfs_cmd_t zc = { 0 };
111 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
114 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
117 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
120 * Silently ignore errors, as the only plausible explanation is
121 * that the pool has since been removed.
123 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
128 if ((ret = func(nzhp, data)) != 0) {
129 zcmd_free_nvlists(&zc);
133 zcmd_free_nvlists(&zc);
134 return ((ret < 0) ? ret : 0);
138 * Iterate over all snapshots
141 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
144 zfs_cmd_t zc = { 0 };
148 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
149 zhp->zfs_type == ZFS_TYPE_BOOKMARK)
152 zc.zc_simple = simple;
154 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
156 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
160 nzhp = make_dataset_simple_handle_zc(zhp, &zc);
162 nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
166 if ((ret = func(nzhp, data)) != 0) {
167 zcmd_free_nvlists(&zc);
171 zcmd_free_nvlists(&zc);
172 return ((ret < 0) ? ret : 0);
176 * Iterate over all bookmarks
179 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
182 nvlist_t *props = NULL;
183 nvlist_t *bmarks = NULL;
186 if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
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));
195 if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
198 for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
199 pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
200 char name[ZFS_MAX_DATASET_NAME_LEN];
202 nvlist_t *bmark_props;
204 bmark_name = nvpair_name(pair);
205 bmark_props = fnvpair_value_nvlist(pair);
207 (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
210 nzhp = make_bookmark_handle(zhp, name, bmark_props);
214 if ((err = func(nzhp, data)) != 0)
220 fnvlist_free(bmarks);
226 * Routines for dealing with the sorted snapshot functionality
228 typedef struct zfs_node {
229 zfs_handle_t *zn_handle;
230 avl_node_t zn_avlnode;
234 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
236 avl_tree_t *avl = data;
240 search.zn_handle = zhp;
241 node = avl_find(avl, &search, NULL);
244 * If this snapshot was renamed while we were creating the
245 * AVL tree, it's possible that we already inserted it under
246 * its old name. Remove the old handle before adding the new
249 zfs_close(node->zn_handle);
250 avl_remove(avl, node);
254 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
255 node->zn_handle = zhp;
262 zfs_snapshot_compare(const void *larg, const void *rarg)
264 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
265 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
266 uint64_t lcreate, rcreate;
269 * Sort them according to creation time. We use the hidden
270 * CREATETXG property to get an absolute ordering of snapshots.
272 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
273 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
275 if (lcreate < rcreate)
277 else if (lcreate > rcreate)
284 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
291 avl_create(&avl, zfs_snapshot_compare,
292 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
294 ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
296 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
297 ret |= callback(node->zn_handle, data);
299 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
310 boolean_t ssa_seenfirst;
311 boolean_t ssa_seenlast;
317 snapspec_cb(zfs_handle_t *zhp, void *arg)
319 snapspec_arg_t *ssa = arg;
320 const char *shortsnapname;
323 if (ssa->ssa_seenlast)
326 shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
327 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
328 ssa->ssa_seenfirst = B_TRUE;
329 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
330 ssa->ssa_seenlast = B_TRUE;
332 if (ssa->ssa_seenfirst) {
333 err = ssa->ssa_func(zhp, ssa->ssa_arg);
342 * spec is a string like "A,B%C,D"
344 * <snaps>, where <snaps> can be:
345 * <snap> (single snapshot)
346 * <snap>%<snap> (range of snapshots, inclusive)
347 * %<snap> (range of snapshots, starting with earliest)
348 * <snap>% (range of snapshots, ending with last)
350 * <snaps>[,...] (comma separated list of the above)
352 * If a snapshot can not be opened, continue trying to open the others, but
353 * return ENOENT at the end.
356 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
357 zfs_iter_f func, void *arg)
359 char *buf, *comma_separated, *cp;
363 buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
366 while ((comma_separated = strsep(&cp, ",")) != NULL) {
367 char *pct = strchr(comma_separated, '%');
369 snapspec_arg_t ssa = { 0 };
373 if (pct == comma_separated)
374 ssa.ssa_seenfirst = B_TRUE;
376 ssa.ssa_first = comma_separated;
378 ssa.ssa_last = pct + 1;
381 * If there is a lastname specified, make sure it
384 if (ssa.ssa_last[0] != '\0') {
385 char snapname[ZFS_MAX_DATASET_NAME_LEN];
386 (void) snprintf(snapname, sizeof (snapname),
387 "%s@%s", zfs_get_name(fs_zhp),
389 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
390 snapname, ZFS_TYPE_SNAPSHOT)) {
396 err = zfs_iter_snapshots_sorted(fs_zhp,
400 if (ret == 0 && (!ssa.ssa_seenfirst ||
401 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
405 char snapname[ZFS_MAX_DATASET_NAME_LEN];
406 zfs_handle_t *snap_zhp;
407 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
408 zfs_get_name(fs_zhp), comma_separated);
409 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
411 if (snap_zhp == NULL) {
415 err = func(snap_zhp, arg);
426 * Iterate over all children, snapshots and filesystems
427 * Process snapshots before filesystems because they are nearer the input
428 * handle: this is extremely important when used with zfs_iter_f functions
429 * looking for data, following the logic that we would like to find it as soon
430 * and as close as possible.
433 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
437 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
440 return (zfs_iter_filesystems(zhp, func, data));
444 typedef struct iter_stack_frame {
445 struct iter_stack_frame *next;
447 } iter_stack_frame_t;
449 typedef struct iter_dependents_arg {
451 boolean_t allowrecursion;
452 iter_stack_frame_t *stack;
455 } iter_dependents_arg_t;
458 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
460 iter_dependents_arg_t *ida = arg;
462 boolean_t first = ida->first;
463 ida->first = B_FALSE;
465 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
466 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
467 } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
468 iter_stack_frame_t isf;
469 iter_stack_frame_t *f;
472 * check if there is a cycle by seeing if this fs is already
475 for (f = ida->stack; f != NULL; f = f->next) {
476 if (f->zhp->zfs_dmustats.dds_guid ==
477 zhp->zfs_dmustats.dds_guid) {
478 if (ida->allowrecursion) {
482 zfs_error_aux(zhp->zfs_hdl,
483 dgettext(TEXT_DOMAIN,
484 "recursive dependency at '%s'"),
486 err = zfs_error(zhp->zfs_hdl,
488 dgettext(TEXT_DOMAIN,
489 "cannot determine dependent "
498 isf.next = ida->stack;
500 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
502 err = zfs_iter_snapshots(zhp, B_FALSE,
503 iter_dependents_cb, ida);
505 ida->stack = isf.next;
508 if (!first && err == 0)
509 err = ida->func(zhp, ida->data);
517 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
518 zfs_iter_f func, void *data)
520 iter_dependents_arg_t ida;
521 ida.allowrecursion = allowrecursion;
526 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));