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