libzfs_iter.c revision b7070b7dbcc2758a7f87cefb69ad42887a287152
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A * Copyright (c) 2013 by Delphix. All rights reserved.
2N/A * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <strings.h>
2N/A#include <unistd.h>
2N/A#include <stddef.h>
2N/A#include <libintl.h>
2N/A#include <libzfs.h>
2N/A
2N/A#include "libzfs_impl.h"
2N/A
2N/Aint
2N/Azfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2N/A{
2N/A nvlist_t *nvl = zfs_get_clones_nvl(zhp);
2N/A nvpair_t *pair;
2N/A
2N/A if (nvl == NULL)
2N/A return (0);
2N/A
2N/A for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
2N/A pair = nvlist_next_nvpair(nvl, pair)) {
2N/A zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
2N/A ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2N/A if (clone != NULL) {
2N/A int err = func(clone, data);
2N/A if (err != 0)
2N/A return (err);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Azfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2N/A{
2N/A int rc;
2N/A uint64_t orig_cookie;
2N/A
2N/A orig_cookie = zc->zc_cookie;
2N/Atop:
2N/A (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2N/A rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2N/A
2N/A if (rc == -1) {
2N/A switch (errno) {
2N/A case ENOMEM:
2N/A /* expand nvlist memory and try again */
2N/A if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2N/A zcmd_free_nvlists(zc);
2N/A return (-1);
2N/A }
2N/A zc->zc_cookie = orig_cookie;
2N/A goto top;
2N/A /*
2N/A * An errno value of ESRCH indicates normal completion.
2N/A * If ENOENT is returned, then the underlying dataset
2N/A * has been removed since we obtained the handle.
2N/A */
2N/A case ESRCH:
2N/A case ENOENT:
2N/A rc = 1;
2N/A break;
2N/A default:
2N/A rc = zfs_standard_error(zhp->zfs_hdl, errno,
2N/A dgettext(TEXT_DOMAIN,
2N/A "cannot iterate filesystems"));
2N/A break;
2N/A }
2N/A }
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Iterate over all child filesystems
2N/A */
2N/Aint
2N/Azfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2N/A{
2N/A zfs_cmd_t zc = { 0 };
2N/A zfs_handle_t *nzhp;
2N/A int ret;
2N/A
2N/A if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2N/A return (0);
2N/A
2N/A if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2N/A return (-1);
2N/A
2N/A while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2N/A &zc)) == 0) {
2N/A /*
2N/A * Silently ignore errors, as the only plausible explanation is
2N/A * that the pool has since been removed.
2N/A */
2N/A if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2N/A &zc)) == NULL) {
2N/A continue;
2N/A }
2N/A
2N/A if ((ret = func(nzhp, data)) != 0) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (ret);
2N/A }
2N/A }
2N/A zcmd_free_nvlists(&zc);
2N/A return ((ret < 0) ? ret : 0);
2N/A}
2N/A
2N/A/*
2N/A * Iterate over all snapshots
2N/A */
2N/Aint
2N/Azfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2N/A{
2N/A zfs_cmd_t zc = { 0 };
2N/A zfs_handle_t *nzhp;
2N/A int ret;
2N/A
2N/A if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
2N/A zhp->zfs_type == ZFS_TYPE_BOOKMARK)
2N/A return (0);
2N/A
2N/A if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2N/A return (-1);
2N/A while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2N/A &zc)) == 0) {
2N/A
2N/A if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2N/A &zc)) == NULL) {
2N/A continue;
2N/A }
2N/A
2N/A if ((ret = func(nzhp, data)) != 0) {
2N/A zcmd_free_nvlists(&zc);
2N/A return (ret);
2N/A }
2N/A }
2N/A zcmd_free_nvlists(&zc);
2N/A return ((ret < 0) ? ret : 0);
2N/A}
2N/A
2N/A/*
2N/A * Iterate over all bookmarks
2N/A */
2N/Aint
2N/Azfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2N/A{
2N/A zfs_handle_t *nzhp;
2N/A nvlist_t *props = NULL;
2N/A nvlist_t *bmarks = NULL;
2N/A int err;
2N/A
2N/A if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
2N/A return (0);
2N/A
2N/A /* Setup the requested properties nvlist. */
2N/A props = fnvlist_alloc();
2N/A fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
2N/A fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
2N/A fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
2N/A
2N/A if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
2N/A goto out;
2N/A
2N/A for (nvpair_t *pair = nvlist_next_nvpair(bmarks, NULL);
2N/A pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
2N/A char name[ZFS_MAXNAMELEN];
2N/A char *bmark_name;
2N/A nvlist_t *bmark_props;
2N/A
2N/A bmark_name = nvpair_name(pair);
2N/A bmark_props = fnvpair_value_nvlist(pair);
2N/A
2N/A (void) snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
2N/A bmark_name);
2N/A
2N/A nzhp = make_bookmark_handle(zhp, name, bmark_props);
2N/A if (nzhp == NULL)
2N/A continue;
2N/A
2N/A if ((err = func(nzhp, data)) != 0)
2N/A goto out;
2N/A }
2N/A
2N/Aout:
2N/A fnvlist_free(props);
2N/A fnvlist_free(bmarks);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/*
2N/A * Routines for dealing with the sorted snapshot functionality
2N/A */
2N/Atypedef struct zfs_node {
2N/A zfs_handle_t *zn_handle;
2N/A avl_node_t zn_avlnode;
2N/A} zfs_node_t;
2N/A
2N/Astatic int
2N/Azfs_sort_snaps(zfs_handle_t *zhp, void *data)
2N/A{
2N/A avl_tree_t *avl = data;
2N/A zfs_node_t *node;
2N/A zfs_node_t search;
2N/A
2N/A search.zn_handle = zhp;
2N/A node = avl_find(avl, &search, NULL);
2N/A if (node) {
2N/A /*
2N/A * If this snapshot was renamed while we were creating the
2N/A * AVL tree, it's possible that we already inserted it under
2N/A * its old name. Remove the old handle before adding the new
2N/A * one.
2N/A */
2N/A zfs_close(node->zn_handle);
2N/A avl_remove(avl, node);
2N/A free(node);
2N/A }
2N/A
2N/A node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
2N/A node->zn_handle = zhp;
2N/A avl_add(avl, node);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic int
2N/Azfs_snapshot_compare(const void *larg, const void *rarg)
2N/A{
2N/A zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
2N/A zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
2N/A uint64_t lcreate, rcreate;
2N/A
2N/A /*
2N/A * Sort them according to creation time. We use the hidden
2N/A * CREATETXG property to get an absolute ordering of snapshots.
2N/A */
2N/A lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
2N/A rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
2N/A
2N/A if (lcreate < rcreate)
2N/A return (-1);
2N/A else if (lcreate > rcreate)
2N/A return (+1);
2N/A else
2N/A return (0);
2N/A}
2N/A
2N/Aint
2N/Azfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
2N/A{
2N/A int ret = 0;
2N/A zfs_node_t *node;
2N/A avl_tree_t avl;
2N/A void *cookie = NULL;
2N/A
2N/A avl_create(&avl, zfs_snapshot_compare,
2N/A sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
2N/A
2N/A ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
2N/A
2N/A for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
2N/A ret |= callback(node->zn_handle, data);
2N/A
2N/A while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
2N/A free(node);
2N/A
2N/A avl_destroy(&avl);
2N/A
2N/A return (ret);
2N/A}
2N/A
2N/Atypedef struct {
2N/A char *ssa_first;
2N/A char *ssa_last;
2N/A boolean_t ssa_seenfirst;
2N/A boolean_t ssa_seenlast;
2N/A zfs_iter_f ssa_func;
2N/A void *ssa_arg;
2N/A} snapspec_arg_t;
2N/A
2N/Astatic int
2N/Asnapspec_cb(zfs_handle_t *zhp, void *arg) {
2N/A snapspec_arg_t *ssa = arg;
2N/A char *shortsnapname;
2N/A int err = 0;
2N/A
2N/A if (ssa->ssa_seenlast)
2N/A return (0);
2N/A shortsnapname = zfs_strdup(zhp->zfs_hdl,
2N/A strchr(zfs_get_name(zhp), '@') + 1);
2N/A
2N/A if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
2N/A ssa->ssa_seenfirst = B_TRUE;
2N/A
2N/A if (ssa->ssa_seenfirst) {
2N/A err = ssa->ssa_func(zhp, ssa->ssa_arg);
2N/A } else {
2N/A zfs_close(zhp);
2N/A }
2N/A
2N/A if (strcmp(shortsnapname, ssa->ssa_last) == 0)
2N/A ssa->ssa_seenlast = B_TRUE;
2N/A free(shortsnapname);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/A/*
2N/A * spec is a string like "A,B%C,D"
2N/A *
2N/A * <snaps>, where <snaps> can be:
2N/A * <snap> (single snapshot)
2N/A * <snap>%<snap> (range of snapshots, inclusive)
2N/A * %<snap> (range of snapshots, starting with earliest)
2N/A * <snap>% (range of snapshots, ending with last)
2N/A * % (all snapshots)
2N/A * <snaps>[,...] (comma separated list of the above)
2N/A *
2N/A * If a snapshot can not be opened, continue trying to open the others, but
2N/A * return ENOENT at the end.
2N/A */
2N/Aint
2N/Azfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
2N/A zfs_iter_f func, void *arg)
2N/A{
2N/A char *buf, *comma_separated, *cp;
2N/A int err = 0;
2N/A int ret = 0;
2N/A
2N/A buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
2N/A cp = buf;
2N/A
2N/A while ((comma_separated = strsep(&cp, ",")) != NULL) {
2N/A char *pct = strchr(comma_separated, '%');
2N/A if (pct != NULL) {
2N/A snapspec_arg_t ssa = { 0 };
2N/A ssa.ssa_func = func;
2N/A ssa.ssa_arg = arg;
2N/A
2N/A if (pct == comma_separated)
2N/A ssa.ssa_seenfirst = B_TRUE;
2N/A else
2N/A ssa.ssa_first = comma_separated;
2N/A *pct = '\0';
2N/A ssa.ssa_last = pct + 1;
2N/A
2N/A /*
2N/A * If there is a lastname specified, make sure it
2N/A * exists.
2N/A */
2N/A if (ssa.ssa_last[0] != '\0') {
2N/A char snapname[ZFS_MAXNAMELEN];
2N/A (void) snprintf(snapname, sizeof (snapname),
2N/A "%s@%s", zfs_get_name(fs_zhp),
2N/A ssa.ssa_last);
2N/A if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
2N/A snapname, ZFS_TYPE_SNAPSHOT)) {
2N/A ret = ENOENT;
2N/A continue;
2N/A }
2N/A }
2N/A
2N/A err = zfs_iter_snapshots_sorted(fs_zhp,
2N/A snapspec_cb, &ssa);
2N/A if (ret == 0)
2N/A ret = err;
2N/A if (ret == 0 && (!ssa.ssa_seenfirst ||
2N/A (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
2N/A ret = ENOENT;
2N/A }
2N/A } else {
2N/A char snapname[ZFS_MAXNAMELEN];
2N/A zfs_handle_t *snap_zhp;
2N/A (void) snprintf(snapname, sizeof (snapname), "%s@%s",
2N/A zfs_get_name(fs_zhp), comma_separated);
2N/A snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
2N/A snapname);
2N/A if (snap_zhp == NULL) {
2N/A ret = ENOENT;
2N/A continue;
2N/A }
2N/A err = func(snap_zhp, arg);
2N/A if (ret == 0)
2N/A ret = err;
2N/A }
2N/A }
2N/A
2N/A free(buf);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Iterate over all children, snapshots and filesystems
2N/A */
2N/Aint
2N/Azfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2N/A{
2N/A int ret;
2N/A
2N/A if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2N/A return (ret);
2N/A
2N/A return (zfs_iter_snapshots(zhp, func, data));
2N/A}
2N/A
2N/A
2N/Atypedef struct iter_stack_frame {
2N/A struct iter_stack_frame *next;
2N/A zfs_handle_t *zhp;
2N/A} iter_stack_frame_t;
2N/A
2N/Atypedef struct iter_dependents_arg {
2N/A boolean_t first;
2N/A boolean_t allowrecursion;
2N/A iter_stack_frame_t *stack;
2N/A zfs_iter_f func;
2N/A void *data;
2N/A} iter_dependents_arg_t;
2N/A
2N/Astatic int
2N/Aiter_dependents_cb(zfs_handle_t *zhp, void *arg)
2N/A{
2N/A iter_dependents_arg_t *ida = arg;
2N/A int err = 0;
2N/A boolean_t first = ida->first;
2N/A ida->first = B_FALSE;
2N/A
2N/A if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2N/A err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
2N/A } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
2N/A iter_stack_frame_t isf;
2N/A iter_stack_frame_t *f;
2N/A
2N/A /*
2N/A * check if there is a cycle by seeing if this fs is already
2N/A * on the stack.
2N/A */
2N/A for (f = ida->stack; f != NULL; f = f->next) {
2N/A if (f->zhp->zfs_dmustats.dds_guid ==
2N/A zhp->zfs_dmustats.dds_guid) {
2N/A if (ida->allowrecursion) {
2N/A zfs_close(zhp);
2N/A return (0);
2N/A } else {
2N/A zfs_error_aux(zhp->zfs_hdl,
2N/A dgettext(TEXT_DOMAIN,
2N/A "recursive dependency at '%s'"),
2N/A zfs_get_name(zhp));
2N/A err = zfs_error(zhp->zfs_hdl,
2N/A EZFS_RECURSIVE,
2N/A dgettext(TEXT_DOMAIN,
2N/A "cannot determine dependent "
2N/A "datasets"));
2N/A zfs_close(zhp);
2N/A return (err);
2N/A }
2N/A }
2N/A }
2N/A
2N/A isf.zhp = zhp;
2N/A isf.next = ida->stack;
2N/A ida->stack = &isf;
2N/A err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
2N/A if (err == 0)
2N/A err = zfs_iter_snapshots(zhp, iter_dependents_cb, ida);
2N/A ida->stack = isf.next;
2N/A }
2N/A
2N/A if (!first && err == 0)
2N/A err = ida->func(zhp, ida->data);
2N/A else
2N/A zfs_close(zhp);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/Aint
2N/Azfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
2N/A zfs_iter_f func, void *data)
2N/A{
2N/A iter_dependents_arg_t ida;
2N/A ida.allowrecursion = allowrecursion;
2N/A ida.stack = NULL;
2N/A ida.func = func;
2N/A ida.data = data;
2N/A ida.first = B_TRUE;
2N/A return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
2N/A}
2N/A