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) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
26 * All rights reserved.
27 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
38 #include "libzfs_impl.h"
41 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
43 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
49 for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
50 pair = nvlist_next_nvpair(nvl, pair)) {
51 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
52 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
54 int err = func(clone, data);
63 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
68 orig_cookie = zc->zc_cookie;
70 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
71 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
76 /* expand nvlist memory and try again */
77 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
78 zcmd_free_nvlists(zc);
81 zc->zc_cookie = orig_cookie;
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.
93 rc = zfs_standard_error(zhp->zfs_hdl, errno,
95 "cannot iterate filesystems"));
103 * Iterate over all child filesystems
106 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
108 zfs_cmd_t zc = { 0 };
112 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
115 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
118 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
121 * Silently ignore errors, as the only plausible explanation is
122 * that the pool has since been removed.
124 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
129 if ((ret = func(nzhp, data)) != 0) {
130 zcmd_free_nvlists(&zc);
134 zcmd_free_nvlists(&zc);
135 return ((ret < 0) ? ret : 0);
139 * Iterate over all snapshots
142 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
145 zfs_cmd_t zc = { 0 };
149 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
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 * Routines for dealing with the sorted snapshot functionality
178 typedef struct zfs_node {
179 zfs_handle_t *zn_handle;
180 avl_node_t zn_avlnode;
184 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
186 avl_tree_t *avl = data;
190 search.zn_handle = zhp;
191 node = avl_find(avl, &search, NULL);
194 * If this snapshot was renamed while we were creating the
195 * AVL tree, it's possible that we already inserted it under
196 * its old name. Remove the old handle before adding the new
199 zfs_close(node->zn_handle);
200 avl_remove(avl, node);
204 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
205 node->zn_handle = zhp;
212 zfs_snapshot_compare(const void *larg, const void *rarg)
214 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
215 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
216 uint64_t lcreate, rcreate;
219 * Sort them according to creation time. We use the hidden
220 * CREATETXG property to get an absolute ordering of snapshots.
222 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
223 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
225 if (lcreate < rcreate)
227 else if (lcreate > rcreate)
234 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
241 avl_create(&avl, zfs_snapshot_compare,
242 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
244 ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
246 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
247 ret |= callback(node->zn_handle, data);
249 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
260 boolean_t ssa_seenfirst;
261 boolean_t ssa_seenlast;
267 snapspec_cb(zfs_handle_t *zhp, void *arg) {
268 snapspec_arg_t *ssa = arg;
272 if (ssa->ssa_seenlast)
274 shortsnapname = zfs_strdup(zhp->zfs_hdl,
275 strchr(zfs_get_name(zhp), '@') + 1);
277 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
278 ssa->ssa_seenfirst = B_TRUE;
280 if (ssa->ssa_seenfirst) {
281 err = ssa->ssa_func(zhp, ssa->ssa_arg);
286 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
287 ssa->ssa_seenlast = B_TRUE;
294 * spec is a string like "A,B%C,D"
296 * <snaps>, where <snaps> can be:
297 * <snap> (single snapshot)
298 * <snap>%<snap> (range of snapshots, inclusive)
299 * %<snap> (range of snapshots, starting with earliest)
300 * <snap>% (range of snapshots, ending with last)
302 * <snaps>[,...] (comma separated list of the above)
304 * If a snapshot can not be opened, continue trying to open the others, but
305 * return ENOENT at the end.
308 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
309 zfs_iter_f func, void *arg)
311 char *buf, *comma_separated, *cp;
315 buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
318 while ((comma_separated = strsep(&cp, ",")) != NULL) {
319 char *pct = strchr(comma_separated, '%');
321 snapspec_arg_t ssa = { 0 };
325 if (pct == comma_separated)
326 ssa.ssa_seenfirst = B_TRUE;
328 ssa.ssa_first = comma_separated;
330 ssa.ssa_last = pct + 1;
333 * If there is a lastname specified, make sure it
336 if (ssa.ssa_last[0] != '\0') {
337 char snapname[ZFS_MAXNAMELEN];
338 (void) snprintf(snapname, sizeof (snapname),
339 "%s@%s", zfs_get_name(fs_zhp),
341 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
342 snapname, ZFS_TYPE_SNAPSHOT)) {
348 err = zfs_iter_snapshots_sorted(fs_zhp,
352 if (ret == 0 && (!ssa.ssa_seenfirst ||
353 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
357 char snapname[ZFS_MAXNAMELEN];
358 zfs_handle_t *snap_zhp;
359 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
360 zfs_get_name(fs_zhp), comma_separated);
361 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
363 if (snap_zhp == NULL) {
367 err = func(snap_zhp, arg);
378 * Iterate over all children, snapshots and filesystems
381 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
385 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
388 return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
392 typedef struct iter_stack_frame {
393 struct iter_stack_frame *next;
395 } iter_stack_frame_t;
397 typedef struct iter_dependents_arg {
399 boolean_t allowrecursion;
400 iter_stack_frame_t *stack;
403 } iter_dependents_arg_t;
406 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
408 iter_dependents_arg_t *ida = arg;
410 boolean_t first = ida->first;
411 ida->first = B_FALSE;
413 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
414 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
416 iter_stack_frame_t isf;
417 iter_stack_frame_t *f;
420 * check if there is a cycle by seeing if this fs is already
423 for (f = ida->stack; f != NULL; f = f->next) {
424 if (f->zhp->zfs_dmustats.dds_guid ==
425 zhp->zfs_dmustats.dds_guid) {
426 if (ida->allowrecursion) {
430 zfs_error_aux(zhp->zfs_hdl,
431 dgettext(TEXT_DOMAIN,
432 "recursive dependency at '%s'"),
434 err = zfs_error(zhp->zfs_hdl,
436 dgettext(TEXT_DOMAIN,
437 "cannot determine dependent "
446 isf.next = ida->stack;
448 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
450 err = zfs_iter_snapshots(zhp, B_FALSE,
451 iter_dependents_cb, ida);
453 ida->stack = isf.next;
456 if (!first && err == 0)
457 err = ida->func(zhp, ida->data);
465 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
466 zfs_iter_f func, void *data)
468 iter_dependents_arg_t ida;
469 ida.allowrecursion = allowrecursion;
474 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));