1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 599653d4eSeschrock * Common Development and Distribution License (the "License"). 699653d4eSeschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 222e5e9e19SSanjeev Bagewadi * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <libintl.h> 27fa9e4066Sahrens #include <libuutil.h> 28fa9e4066Sahrens #include <stddef.h> 29fa9e4066Sahrens #include <stdio.h> 30fa9e4066Sahrens #include <stdlib.h> 31fa9e4066Sahrens #include <strings.h> 32fa9e4066Sahrens 33fa9e4066Sahrens #include <libzfs.h> 34fa9e4066Sahrens 35fa9e4066Sahrens #include "zfs_util.h" 36b6825278Ssjelinek #include "zfs_iter.h" 37fa9e4066Sahrens 38fa9e4066Sahrens /* 39fa9e4066Sahrens * This is a private interface used to gather up all the datasets specified on 40fa9e4066Sahrens * the command line so that we can iterate over them in order. 41fa9e4066Sahrens * 42fa9e4066Sahrens * First, we iterate over all filesystems, gathering them together into an 43b6825278Ssjelinek * AVL tree. We report errors for any explicitly specified datasets 44fa9e4066Sahrens * that we couldn't open. 45fa9e4066Sahrens * 46fa9e4066Sahrens * When finished, we have an AVL tree of ZFS handles. We go through and execute 47fa9e4066Sahrens * the provided callback for each one, passing whatever data the user supplied. 48fa9e4066Sahrens */ 49fa9e4066Sahrens 50fa9e4066Sahrens typedef struct zfs_node { 51fa9e4066Sahrens zfs_handle_t *zn_handle; 52fa9e4066Sahrens uu_avl_node_t zn_avlnode; 53fa9e4066Sahrens } zfs_node_t; 54fa9e4066Sahrens 55fa9e4066Sahrens typedef struct callback_data { 56fa9e4066Sahrens uu_avl_t *cb_avl; 57d5b5bb25SRich Morris int cb_flags; 58fa9e4066Sahrens zfs_type_t cb_types; 59b6825278Ssjelinek zfs_sort_column_t *cb_sortcol; 60990b4856Slling zprop_list_t **cb_proplist; 61ae1726b6SChris Gerhard int cb_depth_limit; 62ae1726b6SChris Gerhard int cb_depth; 632e5e9e19SSanjeev Bagewadi uint8_t cb_props_table[ZFS_NUM_PROPS]; 64fa9e4066Sahrens } callback_data_t; 65fa9e4066Sahrens 66fa9e4066Sahrens uu_avl_pool_t *avl_pool; 67fa9e4066Sahrens 68fa9e4066Sahrens /* 69d5b5bb25SRich Morris * Include snaps if they were requested or if this a zfs list where types 70d5b5bb25SRich Morris * were not specified and the "listsnapshots" property is set on this pool. 71d5b5bb25SRich Morris */ 72d5b5bb25SRich Morris static int 73d5b5bb25SRich Morris zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb) 74d5b5bb25SRich Morris { 75d5b5bb25SRich Morris zpool_handle_t *zph; 76d5b5bb25SRich Morris 77d5b5bb25SRich Morris if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0) 78d5b5bb25SRich Morris return (cb->cb_types & ZFS_TYPE_SNAPSHOT); 79d5b5bb25SRich Morris 80d5b5bb25SRich Morris zph = zfs_get_pool_handle(zhp); 81d5b5bb25SRich Morris return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL)); 82d5b5bb25SRich Morris } 83d5b5bb25SRich Morris 84d5b5bb25SRich Morris /* 85d5b5bb25SRich Morris * Called for each dataset. If the object is of an appropriate type, 86fa9e4066Sahrens * add it to the avl tree and recurse over any children as necessary. 87fa9e4066Sahrens */ 883cb34c60Sahrens static int 89fa9e4066Sahrens zfs_callback(zfs_handle_t *zhp, void *data) 90fa9e4066Sahrens { 91fa9e4066Sahrens callback_data_t *cb = data; 92fa9e4066Sahrens int dontclose = 0; 93d5b5bb25SRich Morris int include_snaps = zfs_include_snapshots(zhp, cb); 94fa9e4066Sahrens 95d5b5bb25SRich Morris if ((zfs_get_type(zhp) & cb->cb_types) || 96d5b5bb25SRich Morris ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) { 97fa9e4066Sahrens uu_avl_index_t idx; 98fa9e4066Sahrens zfs_node_t *node = safe_malloc(sizeof (zfs_node_t)); 99fa9e4066Sahrens 100fa9e4066Sahrens node->zn_handle = zhp; 101fa9e4066Sahrens uu_avl_node_init(node, &node->zn_avlnode, avl_pool); 102b6825278Ssjelinek if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol, 103b6825278Ssjelinek &idx) == NULL) { 1042e5e9e19SSanjeev Bagewadi if (cb->cb_proplist) { 1052e5e9e19SSanjeev Bagewadi if ((*cb->cb_proplist) && 1062e5e9e19SSanjeev Bagewadi !(*cb->cb_proplist)->pl_all) 1072e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zhp, 1082e5e9e19SSanjeev Bagewadi cb->cb_props_table); 1092e5e9e19SSanjeev Bagewadi 110*92241e0bSTom Erickson if (zfs_expand_proplist(zhp, cb->cb_proplist, 111*92241e0bSTom Erickson (cb->cb_flags & ZFS_ITER_RECVD_PROPS)) 1122e5e9e19SSanjeev Bagewadi != 0) { 113e9dbad6fSeschrock free(node); 114e9dbad6fSeschrock return (-1); 115e9dbad6fSeschrock } 1162e5e9e19SSanjeev Bagewadi } 117fa9e4066Sahrens uu_avl_insert(cb->cb_avl, node, idx); 118fa9e4066Sahrens dontclose = 1; 119fa9e4066Sahrens } else { 120fa9e4066Sahrens free(node); 121fa9e4066Sahrens } 122fa9e4066Sahrens } 123fa9e4066Sahrens 124fa9e4066Sahrens /* 1257f7322feSeschrock * Recurse if necessary. 126fa9e4066Sahrens */ 127ae1726b6SChris Gerhard if (cb->cb_flags & ZFS_ITER_RECURSE && 128ae1726b6SChris Gerhard ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 || 129ae1726b6SChris Gerhard cb->cb_depth < cb->cb_depth_limit)) { 130ae1726b6SChris Gerhard cb->cb_depth++; 1313cb34c60Sahrens if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) 1323cb34c60Sahrens (void) zfs_iter_filesystems(zhp, zfs_callback, data); 133d5b5bb25SRich Morris if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps) 1343cb34c60Sahrens (void) zfs_iter_snapshots(zhp, zfs_callback, data); 135ae1726b6SChris Gerhard cb->cb_depth--; 1363cb34c60Sahrens } 137fa9e4066Sahrens 138fa9e4066Sahrens if (!dontclose) 139fa9e4066Sahrens zfs_close(zhp); 140fa9e4066Sahrens 141fa9e4066Sahrens return (0); 142fa9e4066Sahrens } 143fa9e4066Sahrens 144e9dbad6fSeschrock int 145e9dbad6fSeschrock zfs_add_sort_column(zfs_sort_column_t **sc, const char *name, 146b6825278Ssjelinek boolean_t reverse) 147b6825278Ssjelinek { 148b6825278Ssjelinek zfs_sort_column_t *col; 149e9dbad6fSeschrock zfs_prop_t prop; 150e9dbad6fSeschrock 151990b4856Slling if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL && 152e9dbad6fSeschrock !zfs_prop_user(name)) 153e9dbad6fSeschrock return (-1); 154b6825278Ssjelinek 155b6825278Ssjelinek col = safe_malloc(sizeof (zfs_sort_column_t)); 156b6825278Ssjelinek 157b6825278Ssjelinek col->sc_prop = prop; 158b6825278Ssjelinek col->sc_reverse = reverse; 159990b4856Slling if (prop == ZPROP_INVAL) { 160e9dbad6fSeschrock col->sc_user_prop = safe_malloc(strlen(name) + 1); 161e9dbad6fSeschrock (void) strcpy(col->sc_user_prop, name); 162e9dbad6fSeschrock } 163b6825278Ssjelinek 164b6825278Ssjelinek if (*sc == NULL) { 165b6825278Ssjelinek col->sc_last = col; 166b6825278Ssjelinek *sc = col; 167b6825278Ssjelinek } else { 168b6825278Ssjelinek (*sc)->sc_last->sc_next = col; 169b6825278Ssjelinek (*sc)->sc_last = col; 170b6825278Ssjelinek } 171e9dbad6fSeschrock 172e9dbad6fSeschrock return (0); 173b6825278Ssjelinek } 174b6825278Ssjelinek 175b6825278Ssjelinek void 176b6825278Ssjelinek zfs_free_sort_columns(zfs_sort_column_t *sc) 177b6825278Ssjelinek { 178b6825278Ssjelinek zfs_sort_column_t *col; 179b6825278Ssjelinek 180b6825278Ssjelinek while (sc != NULL) { 181b6825278Ssjelinek col = sc->sc_next; 182e9dbad6fSeschrock free(sc->sc_user_prop); 183b6825278Ssjelinek free(sc); 184b6825278Ssjelinek sc = col; 185b6825278Ssjelinek } 186b6825278Ssjelinek } 187b6825278Ssjelinek 188fa9e4066Sahrens /* ARGSUSED */ 189fa9e4066Sahrens static int 190fa9e4066Sahrens zfs_compare(const void *larg, const void *rarg, void *unused) 191fa9e4066Sahrens { 192fa9e4066Sahrens zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 193fa9e4066Sahrens zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 194fa9e4066Sahrens const char *lname = zfs_get_name(l); 195fa9e4066Sahrens const char *rname = zfs_get_name(r); 196fa9e4066Sahrens char *lat, *rat; 197fa9e4066Sahrens uint64_t lcreate, rcreate; 198fa9e4066Sahrens int ret; 199fa9e4066Sahrens 200fa9e4066Sahrens lat = (char *)strchr(lname, '@'); 201fa9e4066Sahrens rat = (char *)strchr(rname, '@'); 202fa9e4066Sahrens 203fa9e4066Sahrens if (lat != NULL) 204fa9e4066Sahrens *lat = '\0'; 205fa9e4066Sahrens if (rat != NULL) 206fa9e4066Sahrens *rat = '\0'; 207fa9e4066Sahrens 208fa9e4066Sahrens ret = strcmp(lname, rname); 209fa9e4066Sahrens if (ret == 0) { 210fa9e4066Sahrens /* 211fa9e4066Sahrens * If we're comparing a dataset to one of its snapshots, we 212fa9e4066Sahrens * always make the full dataset first. 213fa9e4066Sahrens */ 214fa9e4066Sahrens if (lat == NULL) { 215fa9e4066Sahrens ret = -1; 216fa9e4066Sahrens } else if (rat == NULL) { 217fa9e4066Sahrens ret = 1; 218fa9e4066Sahrens } else { 219fa9e4066Sahrens /* 220fa9e4066Sahrens * If we have two snapshots from the same dataset, then 221fa9e4066Sahrens * we want to sort them according to creation time. We 222fa9e4066Sahrens * use the hidden CREATETXG property to get an absolute 223fa9e4066Sahrens * ordering of snapshots. 224fa9e4066Sahrens */ 225fa9e4066Sahrens lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 226fa9e4066Sahrens rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 227fa9e4066Sahrens 228fa9e4066Sahrens if (lcreate < rcreate) 229fa9e4066Sahrens ret = -1; 230fa9e4066Sahrens else if (lcreate > rcreate) 231fa9e4066Sahrens ret = 1; 232fa9e4066Sahrens } 233fa9e4066Sahrens } 234fa9e4066Sahrens 235fa9e4066Sahrens if (lat != NULL) 236fa9e4066Sahrens *lat = '@'; 237fa9e4066Sahrens if (rat != NULL) 238fa9e4066Sahrens *rat = '@'; 239fa9e4066Sahrens 240fa9e4066Sahrens return (ret); 241fa9e4066Sahrens } 242fa9e4066Sahrens 243b6825278Ssjelinek /* 244b6825278Ssjelinek * Sort datasets by specified columns. 245b6825278Ssjelinek * 246b6825278Ssjelinek * o Numeric types sort in ascending order. 247b6825278Ssjelinek * o String types sort in alphabetical order. 248b6825278Ssjelinek * o Types inappropriate for a row sort that row to the literal 249b6825278Ssjelinek * bottom, regardless of the specified ordering. 250b6825278Ssjelinek * 251b6825278Ssjelinek * If no sort columns are specified, or two datasets compare equally 252b6825278Ssjelinek * across all specified columns, they are sorted alphabetically by name 253b6825278Ssjelinek * with snapshots grouped under their parents. 254b6825278Ssjelinek */ 255b6825278Ssjelinek static int 256b6825278Ssjelinek zfs_sort(const void *larg, const void *rarg, void *data) 257b6825278Ssjelinek { 258b6825278Ssjelinek zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 259b6825278Ssjelinek zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 260b6825278Ssjelinek zfs_sort_column_t *sc = (zfs_sort_column_t *)data; 261b6825278Ssjelinek zfs_sort_column_t *psc; 262b6825278Ssjelinek 263b6825278Ssjelinek for (psc = sc; psc != NULL; psc = psc->sc_next) { 264e9dbad6fSeschrock char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN]; 265e9dbad6fSeschrock char *lstr, *rstr; 266b6825278Ssjelinek uint64_t lnum, rnum; 267e9dbad6fSeschrock boolean_t lvalid, rvalid; 268b6825278Ssjelinek int ret = 0; 269b6825278Ssjelinek 270e9dbad6fSeschrock /* 271e9dbad6fSeschrock * We group the checks below the generic code. If 'lstr' and 272e9dbad6fSeschrock * 'rstr' are non-NULL, then we do a string based comparison. 273e9dbad6fSeschrock * Otherwise, we compare 'lnum' and 'rnum'. 274e9dbad6fSeschrock */ 275e9dbad6fSeschrock lstr = rstr = NULL; 276990b4856Slling if (psc->sc_prop == ZPROP_INVAL) { 277e9dbad6fSeschrock nvlist_t *luser, *ruser; 278e9dbad6fSeschrock nvlist_t *lval, *rval; 279b6825278Ssjelinek 280e9dbad6fSeschrock luser = zfs_get_user_props(l); 281e9dbad6fSeschrock ruser = zfs_get_user_props(r); 282b6825278Ssjelinek 283e9dbad6fSeschrock lvalid = (nvlist_lookup_nvlist(luser, 284e9dbad6fSeschrock psc->sc_user_prop, &lval) == 0); 285e9dbad6fSeschrock rvalid = (nvlist_lookup_nvlist(ruser, 286e9dbad6fSeschrock psc->sc_user_prop, &rval) == 0); 287e9dbad6fSeschrock 288e9dbad6fSeschrock if (lvalid) 289e9dbad6fSeschrock verify(nvlist_lookup_string(lval, 290990b4856Slling ZPROP_VALUE, &lstr) == 0); 291e9dbad6fSeschrock if (rvalid) 292e9dbad6fSeschrock verify(nvlist_lookup_string(rval, 293990b4856Slling ZPROP_VALUE, &rstr) == 0); 294e9dbad6fSeschrock 295e9dbad6fSeschrock } else if (zfs_prop_is_string(psc->sc_prop)) { 296e9dbad6fSeschrock lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf, 297e9dbad6fSeschrock sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0); 298e9dbad6fSeschrock rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf, 299e9dbad6fSeschrock sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0); 300e9dbad6fSeschrock 301e9dbad6fSeschrock lstr = lbuf; 302e9dbad6fSeschrock rstr = rbuf; 303b6825278Ssjelinek } else { 304b6825278Ssjelinek lvalid = zfs_prop_valid_for_type(psc->sc_prop, 305b6825278Ssjelinek zfs_get_type(l)); 306b6825278Ssjelinek rvalid = zfs_prop_valid_for_type(psc->sc_prop, 307b6825278Ssjelinek zfs_get_type(r)); 308b6825278Ssjelinek 309e9dbad6fSeschrock if (lvalid) 310e9dbad6fSeschrock (void) zfs_prop_get_numeric(l, psc->sc_prop, 311e9dbad6fSeschrock &lnum, NULL, NULL, 0); 312e9dbad6fSeschrock if (rvalid) 313e9dbad6fSeschrock (void) zfs_prop_get_numeric(r, psc->sc_prop, 314e9dbad6fSeschrock &rnum, NULL, NULL, 0); 315e9dbad6fSeschrock } 316e9dbad6fSeschrock 317b6825278Ssjelinek if (!lvalid && !rvalid) 318b6825278Ssjelinek continue; 319b6825278Ssjelinek else if (!lvalid) 320b6825278Ssjelinek return (1); 321b6825278Ssjelinek else if (!rvalid) 322b6825278Ssjelinek return (-1); 323b6825278Ssjelinek 324e9dbad6fSeschrock if (lstr) 325e9dbad6fSeschrock ret = strcmp(lstr, rstr); 326b8a9e29cSrm160521 else if (lnum < rnum) 327b6825278Ssjelinek ret = -1; 328b6825278Ssjelinek else if (lnum > rnum) 329b6825278Ssjelinek ret = 1; 330b6825278Ssjelinek 331b6825278Ssjelinek if (ret != 0) { 332b6825278Ssjelinek if (psc->sc_reverse == B_TRUE) 333b6825278Ssjelinek ret = (ret < 0) ? 1 : -1; 334b6825278Ssjelinek return (ret); 335b6825278Ssjelinek } 336b6825278Ssjelinek } 337b6825278Ssjelinek 338b6825278Ssjelinek return (zfs_compare(larg, rarg, NULL)); 339b6825278Ssjelinek } 340b6825278Ssjelinek 341fa9e4066Sahrens int 342d5b5bb25SRich Morris zfs_for_each(int argc, char **argv, int flags, zfs_type_t types, 343ae1726b6SChris Gerhard zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit, 344d5b5bb25SRich Morris zfs_iter_f callback, void *data) 345fa9e4066Sahrens { 3462e5e9e19SSanjeev Bagewadi callback_data_t cb = {0}; 347fa9e4066Sahrens int ret = 0; 348fa9e4066Sahrens zfs_node_t *node; 349fa9e4066Sahrens uu_avl_walk_t *walk; 350fa9e4066Sahrens 351fa9e4066Sahrens avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t), 352b6825278Ssjelinek offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT); 353fa9e4066Sahrens 354fa9e4066Sahrens if (avl_pool == NULL) { 355fa9e4066Sahrens (void) fprintf(stderr, 356fa9e4066Sahrens gettext("internal error: out of memory\n")); 357fa9e4066Sahrens exit(1); 358fa9e4066Sahrens } 359fa9e4066Sahrens 360b6825278Ssjelinek cb.cb_sortcol = sortcol; 361d5b5bb25SRich Morris cb.cb_flags = flags; 362e9dbad6fSeschrock cb.cb_proplist = proplist; 363fa9e4066Sahrens cb.cb_types = types; 364ae1726b6SChris Gerhard cb.cb_depth_limit = limit; 3652e5e9e19SSanjeev Bagewadi /* 3662e5e9e19SSanjeev Bagewadi * If cb_proplist is provided then in the zfs_handles created we 3672e5e9e19SSanjeev Bagewadi * retain only those properties listed in cb_proplist and sortcol. 3682e5e9e19SSanjeev Bagewadi * The rest are pruned. So, the caller should make sure that no other 3692e5e9e19SSanjeev Bagewadi * properties other than those listed in cb_proplist/sortcol are 3702e5e9e19SSanjeev Bagewadi * accessed. 3712e5e9e19SSanjeev Bagewadi * 37214843421SMatthew Ahrens * If cb_proplist is NULL then we retain all the properties. We 37314843421SMatthew Ahrens * always retain the zoned property, which some other properties 37414843421SMatthew Ahrens * need (userquota & friends), and the createtxg property, which 37514843421SMatthew Ahrens * we need to sort snapshots. 3762e5e9e19SSanjeev Bagewadi */ 3772e5e9e19SSanjeev Bagewadi if (cb.cb_proplist && *cb.cb_proplist) { 3782e5e9e19SSanjeev Bagewadi zprop_list_t *p = *cb.cb_proplist; 3792e5e9e19SSanjeev Bagewadi 3802e5e9e19SSanjeev Bagewadi while (p) { 3812e5e9e19SSanjeev Bagewadi if (p->pl_prop >= ZFS_PROP_TYPE && 3822e5e9e19SSanjeev Bagewadi p->pl_prop < ZFS_NUM_PROPS) { 3832e5e9e19SSanjeev Bagewadi cb.cb_props_table[p->pl_prop] = B_TRUE; 3842e5e9e19SSanjeev Bagewadi } 3852e5e9e19SSanjeev Bagewadi p = p->pl_next; 3862e5e9e19SSanjeev Bagewadi } 3872e5e9e19SSanjeev Bagewadi 3882e5e9e19SSanjeev Bagewadi while (sortcol) { 3892e5e9e19SSanjeev Bagewadi if (sortcol->sc_prop >= ZFS_PROP_TYPE && 3902e5e9e19SSanjeev Bagewadi sortcol->sc_prop < ZFS_NUM_PROPS) { 3912e5e9e19SSanjeev Bagewadi cb.cb_props_table[sortcol->sc_prop] = B_TRUE; 3922e5e9e19SSanjeev Bagewadi } 3932e5e9e19SSanjeev Bagewadi sortcol = sortcol->sc_next; 3942e5e9e19SSanjeev Bagewadi } 39514843421SMatthew Ahrens 39614843421SMatthew Ahrens cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE; 39714843421SMatthew Ahrens cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE; 3982e5e9e19SSanjeev Bagewadi } else { 3992e5e9e19SSanjeev Bagewadi (void) memset(cb.cb_props_table, B_TRUE, 4002e5e9e19SSanjeev Bagewadi sizeof (cb.cb_props_table)); 4012e5e9e19SSanjeev Bagewadi } 4022e5e9e19SSanjeev Bagewadi 403fa9e4066Sahrens if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) { 404fa9e4066Sahrens (void) fprintf(stderr, 405fa9e4066Sahrens gettext("internal error: out of memory\n")); 406fa9e4066Sahrens exit(1); 407fa9e4066Sahrens } 408fa9e4066Sahrens 409fa9e4066Sahrens if (argc == 0) { 410fa9e4066Sahrens /* 411fa9e4066Sahrens * If given no arguments, iterate over all datasets. 412fa9e4066Sahrens */ 413d5b5bb25SRich Morris cb.cb_flags |= ZFS_ITER_RECURSE; 41499653d4eSeschrock ret = zfs_iter_root(g_zfs, zfs_callback, &cb); 415fa9e4066Sahrens } else { 416fa9e4066Sahrens int i; 417fa9e4066Sahrens zfs_handle_t *zhp; 418fa9e4066Sahrens zfs_type_t argtype; 419fa9e4066Sahrens 420fa9e4066Sahrens /* 421fa9e4066Sahrens * If we're recursive, then we always allow filesystems as 422fa9e4066Sahrens * arguments. If we also are interested in snapshots, then we 423fa9e4066Sahrens * can take volumes as well. 424fa9e4066Sahrens */ 425fa9e4066Sahrens argtype = types; 426d5b5bb25SRich Morris if (flags & ZFS_ITER_RECURSE) { 427fa9e4066Sahrens argtype |= ZFS_TYPE_FILESYSTEM; 428fa9e4066Sahrens if (types & ZFS_TYPE_SNAPSHOT) 429fa9e4066Sahrens argtype |= ZFS_TYPE_VOLUME; 430fa9e4066Sahrens } 431fa9e4066Sahrens 432fa9e4066Sahrens for (i = 0; i < argc; i++) { 433d5b5bb25SRich Morris if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) { 4345aba80dbSck153898 zhp = zfs_path_to_zhandle(g_zfs, argv[i], 4355aba80dbSck153898 argtype); 4365aba80dbSck153898 } else { 4375aba80dbSck153898 zhp = zfs_open(g_zfs, argv[i], argtype); 4385aba80dbSck153898 } 4395aba80dbSck153898 if (zhp != NULL) 44099653d4eSeschrock ret |= zfs_callback(zhp, &cb); 441fa9e4066Sahrens else 442fa9e4066Sahrens ret = 1; 443fa9e4066Sahrens } 444fa9e4066Sahrens } 445fa9e4066Sahrens 446fa9e4066Sahrens /* 447fa9e4066Sahrens * At this point we've got our AVL tree full of zfs handles, so iterate 448fa9e4066Sahrens * over each one and execute the real user callback. 449fa9e4066Sahrens */ 450fa9e4066Sahrens for (node = uu_avl_first(cb.cb_avl); node != NULL; 451fa9e4066Sahrens node = uu_avl_next(cb.cb_avl, node)) 452fa9e4066Sahrens ret |= callback(node->zn_handle, data); 453fa9e4066Sahrens 454fa9e4066Sahrens /* 455fa9e4066Sahrens * Finally, clean up the AVL tree. 456fa9e4066Sahrens */ 457fa9e4066Sahrens if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) { 458fa9e4066Sahrens (void) fprintf(stderr, 459fa9e4066Sahrens gettext("internal error: out of memory")); 460fa9e4066Sahrens exit(1); 461fa9e4066Sahrens } 462fa9e4066Sahrens 463fa9e4066Sahrens while ((node = uu_avl_walk_next(walk)) != NULL) { 464fa9e4066Sahrens uu_avl_remove(cb.cb_avl, node); 465fa9e4066Sahrens zfs_close(node->zn_handle); 466fa9e4066Sahrens free(node); 467fa9e4066Sahrens } 468fa9e4066Sahrens 469fa9e4066Sahrens uu_avl_walk_end(walk); 470fa9e4066Sahrens uu_avl_destroy(cb.cb_avl); 471fa9e4066Sahrens uu_avl_pool_destroy(avl_pool); 472fa9e4066Sahrens 473fa9e4066Sahrens return (ret); 474fa9e4066Sahrens } 475