1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 5789Sahrens * Common Development and Distribution License, Version 1.0 only 6789Sahrens * (the "License"). You may not use this file except in compliance 7789Sahrens * with the License. 8789Sahrens * 9789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10789Sahrens * or http://www.opensolaris.org/os/licensing. 11789Sahrens * See the License for the specific language governing permissions 12789Sahrens * and limitations under the License. 13789Sahrens * 14789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 15789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16789Sahrens * If applicable, add the following below this CDDL HEADER, with the 17789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 18789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 19789Sahrens * 20789Sahrens * CDDL HEADER END 21789Sahrens */ 22789Sahrens /* 23*1356Seschrock * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24789Sahrens * Use is subject to license terms. 25789Sahrens */ 26789Sahrens 27789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28789Sahrens 29789Sahrens #include <libintl.h> 30789Sahrens #include <libuutil.h> 31789Sahrens #include <stddef.h> 32789Sahrens #include <stdio.h> 33789Sahrens #include <stdlib.h> 34789Sahrens #include <strings.h> 35789Sahrens 36789Sahrens #include <libzfs.h> 37789Sahrens 38789Sahrens #include "zfs_util.h" 39789Sahrens 40789Sahrens /* 41789Sahrens * This is a private interface used to gather up all the datasets specified on 42789Sahrens * the command line so that we can iterate over them in order. 43789Sahrens * 44789Sahrens * First, we iterate over all filesystems, gathering them together into an 45789Sahrens * AVL tree sorted by name. For snapshots, we order them according to 46789Sahrens * creation time. We report errors for any explicitly specified datasets 47789Sahrens * that we couldn't open. 48789Sahrens * 49789Sahrens * When finished, we have an AVL tree of ZFS handles. We go through and execute 50789Sahrens * the provided callback for each one, passing whatever data the user supplied. 51789Sahrens */ 52789Sahrens 53789Sahrens typedef struct zfs_node { 54789Sahrens zfs_handle_t *zn_handle; 55789Sahrens uu_avl_node_t zn_avlnode; 56789Sahrens } zfs_node_t; 57789Sahrens 58789Sahrens typedef struct callback_data { 59789Sahrens uu_avl_t *cb_avl; 60789Sahrens int cb_recurse; 61789Sahrens zfs_type_t cb_types; 62789Sahrens } callback_data_t; 63789Sahrens 64789Sahrens uu_avl_pool_t *avl_pool; 65789Sahrens 66789Sahrens /* 67789Sahrens * Called for each dataset. If the object the object is of an appropriate type, 68789Sahrens * add it to the avl tree and recurse over any children as necessary. 69789Sahrens */ 70789Sahrens int 71789Sahrens zfs_callback(zfs_handle_t *zhp, void *data) 72789Sahrens { 73789Sahrens callback_data_t *cb = data; 74789Sahrens int dontclose = 0; 75789Sahrens 76789Sahrens /* 77789Sahrens * If this object is of the appropriate type, add it to the AVL tree. 78789Sahrens */ 79789Sahrens if (zfs_get_type(zhp) & cb->cb_types) { 80789Sahrens uu_avl_index_t idx; 81789Sahrens zfs_node_t *node = safe_malloc(sizeof (zfs_node_t)); 82789Sahrens 83789Sahrens node->zn_handle = zhp; 84789Sahrens uu_avl_node_init(node, &node->zn_avlnode, avl_pool); 85789Sahrens if (uu_avl_find(cb->cb_avl, node, NULL, &idx) == NULL) { 86789Sahrens uu_avl_insert(cb->cb_avl, node, idx); 87789Sahrens dontclose = 1; 88789Sahrens } else { 89789Sahrens free(node); 90789Sahrens } 91789Sahrens } 92789Sahrens 93789Sahrens /* 94*1356Seschrock * Recurse if necessary. 95789Sahrens */ 96789Sahrens if (cb->cb_recurse && (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM || 97*1356Seschrock (zfs_get_type(zhp) == ZFS_TYPE_VOLUME && (cb->cb_types & 98*1356Seschrock ZFS_TYPE_SNAPSHOT)))) 99789Sahrens (void) zfs_iter_children(zhp, zfs_callback, data); 100789Sahrens 101789Sahrens if (!dontclose) 102789Sahrens zfs_close(zhp); 103789Sahrens 104789Sahrens return (0); 105789Sahrens } 106789Sahrens 107789Sahrens /* ARGSUSED */ 108789Sahrens static int 109789Sahrens zfs_compare(const void *larg, const void *rarg, void *unused) 110789Sahrens { 111789Sahrens zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 112789Sahrens zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 113789Sahrens const char *lname = zfs_get_name(l); 114789Sahrens const char *rname = zfs_get_name(r); 115789Sahrens char *lat, *rat; 116789Sahrens uint64_t lcreate, rcreate; 117789Sahrens int ret; 118789Sahrens 119789Sahrens lat = (char *)strchr(lname, '@'); 120789Sahrens rat = (char *)strchr(rname, '@'); 121789Sahrens 122789Sahrens if (lat != NULL) 123789Sahrens *lat = '\0'; 124789Sahrens if (rat != NULL) 125789Sahrens *rat = '\0'; 126789Sahrens 127789Sahrens ret = strcmp(lname, rname); 128789Sahrens if (ret == 0) { 129789Sahrens /* 130789Sahrens * If we're comparing a dataset to one of its snapshots, we 131789Sahrens * always make the full dataset first. 132789Sahrens */ 133789Sahrens if (lat == NULL) { 134789Sahrens ret = -1; 135789Sahrens } else if (rat == NULL) { 136789Sahrens ret = 1; 137789Sahrens } else { 138789Sahrens /* 139789Sahrens * If we have two snapshots from the same dataset, then 140789Sahrens * we want to sort them according to creation time. We 141789Sahrens * use the hidden CREATETXG property to get an absolute 142789Sahrens * ordering of snapshots. 143789Sahrens */ 144789Sahrens lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 145789Sahrens rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 146789Sahrens 147789Sahrens if (lcreate < rcreate) 148789Sahrens ret = -1; 149789Sahrens else if (lcreate > rcreate) 150789Sahrens ret = 1; 151789Sahrens } 152789Sahrens } 153789Sahrens 154789Sahrens if (lat != NULL) 155789Sahrens *lat = '@'; 156789Sahrens if (rat != NULL) 157789Sahrens *rat = '@'; 158789Sahrens 159789Sahrens return (ret); 160789Sahrens } 161789Sahrens 162789Sahrens int 163789Sahrens zfs_for_each(int argc, char **argv, int recurse, zfs_type_t types, 164789Sahrens zfs_iter_f callback, void *data) 165789Sahrens { 166789Sahrens callback_data_t cb; 167789Sahrens int ret = 0; 168789Sahrens zfs_node_t *node; 169789Sahrens uu_avl_walk_t *walk; 170789Sahrens 171789Sahrens avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t), 172789Sahrens offsetof(zfs_node_t, zn_avlnode), zfs_compare, UU_DEFAULT); 173789Sahrens 174789Sahrens if (avl_pool == NULL) { 175789Sahrens (void) fprintf(stderr, 176789Sahrens gettext("internal error: out of memory\n")); 177789Sahrens exit(1); 178789Sahrens } 179789Sahrens 180789Sahrens cb.cb_recurse = recurse; 181789Sahrens cb.cb_types = types; 182789Sahrens if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) { 183789Sahrens (void) fprintf(stderr, 184789Sahrens gettext("internal error: out of memory\n")); 185789Sahrens exit(1); 186789Sahrens } 187789Sahrens 188789Sahrens if (argc == 0) { 189789Sahrens /* 190789Sahrens * If given no arguments, iterate over all datasets. 191789Sahrens */ 192789Sahrens cb.cb_recurse = 1; 193789Sahrens ret = zfs_iter_root(zfs_callback, &cb); 194789Sahrens } else { 195789Sahrens int i; 196789Sahrens zfs_handle_t *zhp; 197789Sahrens zfs_type_t argtype; 198789Sahrens 199789Sahrens /* 200789Sahrens * If we're recursive, then we always allow filesystems as 201789Sahrens * arguments. If we also are interested in snapshots, then we 202789Sahrens * can take volumes as well. 203789Sahrens */ 204789Sahrens argtype = types; 205789Sahrens if (recurse) { 206789Sahrens argtype |= ZFS_TYPE_FILESYSTEM; 207789Sahrens if (types & ZFS_TYPE_SNAPSHOT) 208789Sahrens argtype |= ZFS_TYPE_VOLUME; 209789Sahrens } 210789Sahrens 211789Sahrens for (i = 0; i < argc; i++) { 212789Sahrens if ((zhp = zfs_open(argv[i], argtype)) != NULL) 213789Sahrens ret = zfs_callback(zhp, &cb); 214789Sahrens else 215789Sahrens ret = 1; 216789Sahrens } 217789Sahrens } 218789Sahrens 219789Sahrens /* 220789Sahrens * At this point we've got our AVL tree full of zfs handles, so iterate 221789Sahrens * over each one and execute the real user callback. 222789Sahrens */ 223789Sahrens for (node = uu_avl_first(cb.cb_avl); node != NULL; 224789Sahrens node = uu_avl_next(cb.cb_avl, node)) 225789Sahrens ret |= callback(node->zn_handle, data); 226789Sahrens 227789Sahrens /* 228789Sahrens * Finally, clean up the AVL tree. 229789Sahrens */ 230789Sahrens if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) { 231789Sahrens (void) fprintf(stderr, 232789Sahrens gettext("internal error: out of memory")); 233789Sahrens exit(1); 234789Sahrens } 235789Sahrens 236789Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 237789Sahrens uu_avl_remove(cb.cb_avl, node); 238789Sahrens zfs_close(node->zn_handle); 239789Sahrens free(node); 240789Sahrens } 241789Sahrens 242789Sahrens uu_avl_walk_end(walk); 243789Sahrens uu_avl_destroy(cb.cb_avl); 244789Sahrens uu_avl_pool_destroy(avl_pool); 245789Sahrens 246789Sahrens return (ret); 247789Sahrens } 248