1*eda14cbcSMatt Macy /* 2*eda14cbcSMatt Macy * CDDL HEADER START 3*eda14cbcSMatt Macy * 4*eda14cbcSMatt Macy * The contents of this file are subject to the terms of the 5*eda14cbcSMatt Macy * Common Development and Distribution License (the "License"). 6*eda14cbcSMatt Macy * You may not use this file except in compliance with the License. 7*eda14cbcSMatt Macy * 8*eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*eda14cbcSMatt Macy * or http://www.opensolaris.org/os/licensing. 10*eda14cbcSMatt Macy * See the License for the specific language governing permissions 11*eda14cbcSMatt Macy * and limitations under the License. 12*eda14cbcSMatt Macy * 13*eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each 14*eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the 16*eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying 17*eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner] 18*eda14cbcSMatt Macy * 19*eda14cbcSMatt Macy * CDDL HEADER END 20*eda14cbcSMatt Macy */ 21*eda14cbcSMatt Macy 22*eda14cbcSMatt Macy /* 23*eda14cbcSMatt Macy * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24*eda14cbcSMatt Macy * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. 25*eda14cbcSMatt Macy * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 26*eda14cbcSMatt Macy * Copyright (c) 2013 by Delphix. All rights reserved. 27*eda14cbcSMatt Macy */ 28*eda14cbcSMatt Macy 29*eda14cbcSMatt Macy #include <libintl.h> 30*eda14cbcSMatt Macy #include <libuutil.h> 31*eda14cbcSMatt Macy #include <stddef.h> 32*eda14cbcSMatt Macy #include <stdio.h> 33*eda14cbcSMatt Macy #include <stdlib.h> 34*eda14cbcSMatt Macy #include <string.h> 35*eda14cbcSMatt Macy #include <strings.h> 36*eda14cbcSMatt Macy 37*eda14cbcSMatt Macy #include <libzfs.h> 38*eda14cbcSMatt Macy 39*eda14cbcSMatt Macy #include "zfs_util.h" 40*eda14cbcSMatt Macy #include "zfs_iter.h" 41*eda14cbcSMatt Macy 42*eda14cbcSMatt Macy /* 43*eda14cbcSMatt Macy * This is a private interface used to gather up all the datasets specified on 44*eda14cbcSMatt Macy * the command line so that we can iterate over them in order. 45*eda14cbcSMatt Macy * 46*eda14cbcSMatt Macy * First, we iterate over all filesystems, gathering them together into an 47*eda14cbcSMatt Macy * AVL tree. We report errors for any explicitly specified datasets 48*eda14cbcSMatt Macy * that we couldn't open. 49*eda14cbcSMatt Macy * 50*eda14cbcSMatt Macy * When finished, we have an AVL tree of ZFS handles. We go through and execute 51*eda14cbcSMatt Macy * the provided callback for each one, passing whatever data the user supplied. 52*eda14cbcSMatt Macy */ 53*eda14cbcSMatt Macy 54*eda14cbcSMatt Macy typedef struct zfs_node { 55*eda14cbcSMatt Macy zfs_handle_t *zn_handle; 56*eda14cbcSMatt Macy uu_avl_node_t zn_avlnode; 57*eda14cbcSMatt Macy } zfs_node_t; 58*eda14cbcSMatt Macy 59*eda14cbcSMatt Macy typedef struct callback_data { 60*eda14cbcSMatt Macy uu_avl_t *cb_avl; 61*eda14cbcSMatt Macy int cb_flags; 62*eda14cbcSMatt Macy zfs_type_t cb_types; 63*eda14cbcSMatt Macy zfs_sort_column_t *cb_sortcol; 64*eda14cbcSMatt Macy zprop_list_t **cb_proplist; 65*eda14cbcSMatt Macy int cb_depth_limit; 66*eda14cbcSMatt Macy int cb_depth; 67*eda14cbcSMatt Macy uint8_t cb_props_table[ZFS_NUM_PROPS]; 68*eda14cbcSMatt Macy } callback_data_t; 69*eda14cbcSMatt Macy 70*eda14cbcSMatt Macy uu_avl_pool_t *avl_pool; 71*eda14cbcSMatt Macy 72*eda14cbcSMatt Macy /* 73*eda14cbcSMatt Macy * Include snaps if they were requested or if this a zfs list where types 74*eda14cbcSMatt Macy * were not specified and the "listsnapshots" property is set on this pool. 75*eda14cbcSMatt Macy */ 76*eda14cbcSMatt Macy static boolean_t 77*eda14cbcSMatt Macy zfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb) 78*eda14cbcSMatt Macy { 79*eda14cbcSMatt Macy zpool_handle_t *zph; 80*eda14cbcSMatt Macy 81*eda14cbcSMatt Macy if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0) 82*eda14cbcSMatt Macy return (cb->cb_types & ZFS_TYPE_SNAPSHOT); 83*eda14cbcSMatt Macy 84*eda14cbcSMatt Macy zph = zfs_get_pool_handle(zhp); 85*eda14cbcSMatt Macy return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL)); 86*eda14cbcSMatt Macy } 87*eda14cbcSMatt Macy 88*eda14cbcSMatt Macy /* 89*eda14cbcSMatt Macy * Called for each dataset. If the object is of an appropriate type, 90*eda14cbcSMatt Macy * add it to the avl tree and recurse over any children as necessary. 91*eda14cbcSMatt Macy */ 92*eda14cbcSMatt Macy static int 93*eda14cbcSMatt Macy zfs_callback(zfs_handle_t *zhp, void *data) 94*eda14cbcSMatt Macy { 95*eda14cbcSMatt Macy callback_data_t *cb = data; 96*eda14cbcSMatt Macy boolean_t should_close = B_TRUE; 97*eda14cbcSMatt Macy boolean_t include_snaps = zfs_include_snapshots(zhp, cb); 98*eda14cbcSMatt Macy boolean_t include_bmarks = (cb->cb_types & ZFS_TYPE_BOOKMARK); 99*eda14cbcSMatt Macy 100*eda14cbcSMatt Macy if ((zfs_get_type(zhp) & cb->cb_types) || 101*eda14cbcSMatt Macy ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) { 102*eda14cbcSMatt Macy uu_avl_index_t idx; 103*eda14cbcSMatt Macy zfs_node_t *node = safe_malloc(sizeof (zfs_node_t)); 104*eda14cbcSMatt Macy 105*eda14cbcSMatt Macy node->zn_handle = zhp; 106*eda14cbcSMatt Macy uu_avl_node_init(node, &node->zn_avlnode, avl_pool); 107*eda14cbcSMatt Macy if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol, 108*eda14cbcSMatt Macy &idx) == NULL) { 109*eda14cbcSMatt Macy if (cb->cb_proplist) { 110*eda14cbcSMatt Macy if ((*cb->cb_proplist) && 111*eda14cbcSMatt Macy !(*cb->cb_proplist)->pl_all) 112*eda14cbcSMatt Macy zfs_prune_proplist(zhp, 113*eda14cbcSMatt Macy cb->cb_props_table); 114*eda14cbcSMatt Macy 115*eda14cbcSMatt Macy if (zfs_expand_proplist(zhp, cb->cb_proplist, 116*eda14cbcSMatt Macy (cb->cb_flags & ZFS_ITER_RECVD_PROPS), 117*eda14cbcSMatt Macy (cb->cb_flags & ZFS_ITER_LITERAL_PROPS)) 118*eda14cbcSMatt Macy != 0) { 119*eda14cbcSMatt Macy free(node); 120*eda14cbcSMatt Macy return (-1); 121*eda14cbcSMatt Macy } 122*eda14cbcSMatt Macy } 123*eda14cbcSMatt Macy uu_avl_insert(cb->cb_avl, node, idx); 124*eda14cbcSMatt Macy should_close = B_FALSE; 125*eda14cbcSMatt Macy } else { 126*eda14cbcSMatt Macy free(node); 127*eda14cbcSMatt Macy } 128*eda14cbcSMatt Macy } 129*eda14cbcSMatt Macy 130*eda14cbcSMatt Macy /* 131*eda14cbcSMatt Macy * Recurse if necessary. 132*eda14cbcSMatt Macy */ 133*eda14cbcSMatt Macy if (cb->cb_flags & ZFS_ITER_RECURSE && 134*eda14cbcSMatt Macy ((cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 || 135*eda14cbcSMatt Macy cb->cb_depth < cb->cb_depth_limit)) { 136*eda14cbcSMatt Macy cb->cb_depth++; 137*eda14cbcSMatt Macy 138*eda14cbcSMatt Macy /* 139*eda14cbcSMatt Macy * If we are not looking for filesystems, we don't need to 140*eda14cbcSMatt Macy * recurse into filesystems when we are at our depth limit. 141*eda14cbcSMatt Macy */ 142*eda14cbcSMatt Macy if ((cb->cb_depth < cb->cb_depth_limit || 143*eda14cbcSMatt Macy (cb->cb_flags & ZFS_ITER_DEPTH_LIMIT) == 0 || 144*eda14cbcSMatt Macy (cb->cb_types & 145*eda14cbcSMatt Macy (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) && 146*eda14cbcSMatt Macy zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) { 147*eda14cbcSMatt Macy (void) zfs_iter_filesystems(zhp, zfs_callback, data); 148*eda14cbcSMatt Macy } 149*eda14cbcSMatt Macy 150*eda14cbcSMatt Macy if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | 151*eda14cbcSMatt Macy ZFS_TYPE_BOOKMARK)) == 0) && include_snaps) { 152*eda14cbcSMatt Macy (void) zfs_iter_snapshots(zhp, 153*eda14cbcSMatt Macy (cb->cb_flags & ZFS_ITER_SIMPLE) != 0, 154*eda14cbcSMatt Macy zfs_callback, data, 0, 0); 155*eda14cbcSMatt Macy } 156*eda14cbcSMatt Macy 157*eda14cbcSMatt Macy if (((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | 158*eda14cbcSMatt Macy ZFS_TYPE_BOOKMARK)) == 0) && include_bmarks) { 159*eda14cbcSMatt Macy (void) zfs_iter_bookmarks(zhp, zfs_callback, data); 160*eda14cbcSMatt Macy } 161*eda14cbcSMatt Macy 162*eda14cbcSMatt Macy cb->cb_depth--; 163*eda14cbcSMatt Macy } 164*eda14cbcSMatt Macy 165*eda14cbcSMatt Macy if (should_close) 166*eda14cbcSMatt Macy zfs_close(zhp); 167*eda14cbcSMatt Macy 168*eda14cbcSMatt Macy return (0); 169*eda14cbcSMatt Macy } 170*eda14cbcSMatt Macy 171*eda14cbcSMatt Macy int 172*eda14cbcSMatt Macy zfs_add_sort_column(zfs_sort_column_t **sc, const char *name, 173*eda14cbcSMatt Macy boolean_t reverse) 174*eda14cbcSMatt Macy { 175*eda14cbcSMatt Macy zfs_sort_column_t *col; 176*eda14cbcSMatt Macy zfs_prop_t prop; 177*eda14cbcSMatt Macy 178*eda14cbcSMatt Macy if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL && 179*eda14cbcSMatt Macy !zfs_prop_user(name)) 180*eda14cbcSMatt Macy return (-1); 181*eda14cbcSMatt Macy 182*eda14cbcSMatt Macy col = safe_malloc(sizeof (zfs_sort_column_t)); 183*eda14cbcSMatt Macy 184*eda14cbcSMatt Macy col->sc_prop = prop; 185*eda14cbcSMatt Macy col->sc_reverse = reverse; 186*eda14cbcSMatt Macy if (prop == ZPROP_INVAL) { 187*eda14cbcSMatt Macy col->sc_user_prop = safe_malloc(strlen(name) + 1); 188*eda14cbcSMatt Macy (void) strcpy(col->sc_user_prop, name); 189*eda14cbcSMatt Macy } 190*eda14cbcSMatt Macy 191*eda14cbcSMatt Macy if (*sc == NULL) { 192*eda14cbcSMatt Macy col->sc_last = col; 193*eda14cbcSMatt Macy *sc = col; 194*eda14cbcSMatt Macy } else { 195*eda14cbcSMatt Macy (*sc)->sc_last->sc_next = col; 196*eda14cbcSMatt Macy (*sc)->sc_last = col; 197*eda14cbcSMatt Macy } 198*eda14cbcSMatt Macy 199*eda14cbcSMatt Macy return (0); 200*eda14cbcSMatt Macy } 201*eda14cbcSMatt Macy 202*eda14cbcSMatt Macy void 203*eda14cbcSMatt Macy zfs_free_sort_columns(zfs_sort_column_t *sc) 204*eda14cbcSMatt Macy { 205*eda14cbcSMatt Macy zfs_sort_column_t *col; 206*eda14cbcSMatt Macy 207*eda14cbcSMatt Macy while (sc != NULL) { 208*eda14cbcSMatt Macy col = sc->sc_next; 209*eda14cbcSMatt Macy free(sc->sc_user_prop); 210*eda14cbcSMatt Macy free(sc); 211*eda14cbcSMatt Macy sc = col; 212*eda14cbcSMatt Macy } 213*eda14cbcSMatt Macy } 214*eda14cbcSMatt Macy 215*eda14cbcSMatt Macy int 216*eda14cbcSMatt Macy zfs_sort_only_by_name(const zfs_sort_column_t *sc) 217*eda14cbcSMatt Macy { 218*eda14cbcSMatt Macy return (sc != NULL && sc->sc_next == NULL && 219*eda14cbcSMatt Macy sc->sc_prop == ZFS_PROP_NAME); 220*eda14cbcSMatt Macy } 221*eda14cbcSMatt Macy 222*eda14cbcSMatt Macy /* ARGSUSED */ 223*eda14cbcSMatt Macy static int 224*eda14cbcSMatt Macy zfs_compare(const void *larg, const void *rarg, void *unused) 225*eda14cbcSMatt Macy { 226*eda14cbcSMatt Macy zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 227*eda14cbcSMatt Macy zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 228*eda14cbcSMatt Macy const char *lname = zfs_get_name(l); 229*eda14cbcSMatt Macy const char *rname = zfs_get_name(r); 230*eda14cbcSMatt Macy char *lat, *rat; 231*eda14cbcSMatt Macy uint64_t lcreate, rcreate; 232*eda14cbcSMatt Macy int ret; 233*eda14cbcSMatt Macy 234*eda14cbcSMatt Macy lat = (char *)strchr(lname, '@'); 235*eda14cbcSMatt Macy rat = (char *)strchr(rname, '@'); 236*eda14cbcSMatt Macy 237*eda14cbcSMatt Macy if (lat != NULL) 238*eda14cbcSMatt Macy *lat = '\0'; 239*eda14cbcSMatt Macy if (rat != NULL) 240*eda14cbcSMatt Macy *rat = '\0'; 241*eda14cbcSMatt Macy 242*eda14cbcSMatt Macy ret = strcmp(lname, rname); 243*eda14cbcSMatt Macy if (ret == 0 && (lat != NULL || rat != NULL)) { 244*eda14cbcSMatt Macy /* 245*eda14cbcSMatt Macy * If we're comparing a dataset to one of its snapshots, we 246*eda14cbcSMatt Macy * always make the full dataset first. 247*eda14cbcSMatt Macy */ 248*eda14cbcSMatt Macy if (lat == NULL) { 249*eda14cbcSMatt Macy ret = -1; 250*eda14cbcSMatt Macy } else if (rat == NULL) { 251*eda14cbcSMatt Macy ret = 1; 252*eda14cbcSMatt Macy } else { 253*eda14cbcSMatt Macy /* 254*eda14cbcSMatt Macy * If we have two snapshots from the same dataset, then 255*eda14cbcSMatt Macy * we want to sort them according to creation time. We 256*eda14cbcSMatt Macy * use the hidden CREATETXG property to get an absolute 257*eda14cbcSMatt Macy * ordering of snapshots. 258*eda14cbcSMatt Macy */ 259*eda14cbcSMatt Macy lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 260*eda14cbcSMatt Macy rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 261*eda14cbcSMatt Macy 262*eda14cbcSMatt Macy /* 263*eda14cbcSMatt Macy * Both lcreate and rcreate being 0 means we don't have 264*eda14cbcSMatt Macy * properties and we should compare full name. 265*eda14cbcSMatt Macy */ 266*eda14cbcSMatt Macy if (lcreate == 0 && rcreate == 0) 267*eda14cbcSMatt Macy ret = strcmp(lat + 1, rat + 1); 268*eda14cbcSMatt Macy else if (lcreate < rcreate) 269*eda14cbcSMatt Macy ret = -1; 270*eda14cbcSMatt Macy else if (lcreate > rcreate) 271*eda14cbcSMatt Macy ret = 1; 272*eda14cbcSMatt Macy } 273*eda14cbcSMatt Macy } 274*eda14cbcSMatt Macy 275*eda14cbcSMatt Macy if (lat != NULL) 276*eda14cbcSMatt Macy *lat = '@'; 277*eda14cbcSMatt Macy if (rat != NULL) 278*eda14cbcSMatt Macy *rat = '@'; 279*eda14cbcSMatt Macy 280*eda14cbcSMatt Macy return (ret); 281*eda14cbcSMatt Macy } 282*eda14cbcSMatt Macy 283*eda14cbcSMatt Macy /* 284*eda14cbcSMatt Macy * Sort datasets by specified columns. 285*eda14cbcSMatt Macy * 286*eda14cbcSMatt Macy * o Numeric types sort in ascending order. 287*eda14cbcSMatt Macy * o String types sort in alphabetical order. 288*eda14cbcSMatt Macy * o Types inappropriate for a row sort that row to the literal 289*eda14cbcSMatt Macy * bottom, regardless of the specified ordering. 290*eda14cbcSMatt Macy * 291*eda14cbcSMatt Macy * If no sort columns are specified, or two datasets compare equally 292*eda14cbcSMatt Macy * across all specified columns, they are sorted alphabetically by name 293*eda14cbcSMatt Macy * with snapshots grouped under their parents. 294*eda14cbcSMatt Macy */ 295*eda14cbcSMatt Macy static int 296*eda14cbcSMatt Macy zfs_sort(const void *larg, const void *rarg, void *data) 297*eda14cbcSMatt Macy { 298*eda14cbcSMatt Macy zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 299*eda14cbcSMatt Macy zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 300*eda14cbcSMatt Macy zfs_sort_column_t *sc = (zfs_sort_column_t *)data; 301*eda14cbcSMatt Macy zfs_sort_column_t *psc; 302*eda14cbcSMatt Macy 303*eda14cbcSMatt Macy for (psc = sc; psc != NULL; psc = psc->sc_next) { 304*eda14cbcSMatt Macy char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN]; 305*eda14cbcSMatt Macy char *lstr, *rstr; 306*eda14cbcSMatt Macy uint64_t lnum, rnum; 307*eda14cbcSMatt Macy boolean_t lvalid, rvalid; 308*eda14cbcSMatt Macy int ret = 0; 309*eda14cbcSMatt Macy 310*eda14cbcSMatt Macy /* 311*eda14cbcSMatt Macy * We group the checks below the generic code. If 'lstr' and 312*eda14cbcSMatt Macy * 'rstr' are non-NULL, then we do a string based comparison. 313*eda14cbcSMatt Macy * Otherwise, we compare 'lnum' and 'rnum'. 314*eda14cbcSMatt Macy */ 315*eda14cbcSMatt Macy lstr = rstr = NULL; 316*eda14cbcSMatt Macy if (psc->sc_prop == ZPROP_INVAL) { 317*eda14cbcSMatt Macy nvlist_t *luser, *ruser; 318*eda14cbcSMatt Macy nvlist_t *lval, *rval; 319*eda14cbcSMatt Macy 320*eda14cbcSMatt Macy luser = zfs_get_user_props(l); 321*eda14cbcSMatt Macy ruser = zfs_get_user_props(r); 322*eda14cbcSMatt Macy 323*eda14cbcSMatt Macy lvalid = (nvlist_lookup_nvlist(luser, 324*eda14cbcSMatt Macy psc->sc_user_prop, &lval) == 0); 325*eda14cbcSMatt Macy rvalid = (nvlist_lookup_nvlist(ruser, 326*eda14cbcSMatt Macy psc->sc_user_prop, &rval) == 0); 327*eda14cbcSMatt Macy 328*eda14cbcSMatt Macy if (lvalid) 329*eda14cbcSMatt Macy verify(nvlist_lookup_string(lval, 330*eda14cbcSMatt Macy ZPROP_VALUE, &lstr) == 0); 331*eda14cbcSMatt Macy if (rvalid) 332*eda14cbcSMatt Macy verify(nvlist_lookup_string(rval, 333*eda14cbcSMatt Macy ZPROP_VALUE, &rstr) == 0); 334*eda14cbcSMatt Macy } else if (psc->sc_prop == ZFS_PROP_NAME) { 335*eda14cbcSMatt Macy lvalid = rvalid = B_TRUE; 336*eda14cbcSMatt Macy 337*eda14cbcSMatt Macy (void) strlcpy(lbuf, zfs_get_name(l), sizeof (lbuf)); 338*eda14cbcSMatt Macy (void) strlcpy(rbuf, zfs_get_name(r), sizeof (rbuf)); 339*eda14cbcSMatt Macy 340*eda14cbcSMatt Macy lstr = lbuf; 341*eda14cbcSMatt Macy rstr = rbuf; 342*eda14cbcSMatt Macy } else if (zfs_prop_is_string(psc->sc_prop)) { 343*eda14cbcSMatt Macy lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf, 344*eda14cbcSMatt Macy sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0); 345*eda14cbcSMatt Macy rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf, 346*eda14cbcSMatt Macy sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0); 347*eda14cbcSMatt Macy 348*eda14cbcSMatt Macy lstr = lbuf; 349*eda14cbcSMatt Macy rstr = rbuf; 350*eda14cbcSMatt Macy } else { 351*eda14cbcSMatt Macy lvalid = zfs_prop_valid_for_type(psc->sc_prop, 352*eda14cbcSMatt Macy zfs_get_type(l), B_FALSE); 353*eda14cbcSMatt Macy rvalid = zfs_prop_valid_for_type(psc->sc_prop, 354*eda14cbcSMatt Macy zfs_get_type(r), B_FALSE); 355*eda14cbcSMatt Macy 356*eda14cbcSMatt Macy if (lvalid) 357*eda14cbcSMatt Macy (void) zfs_prop_get_numeric(l, psc->sc_prop, 358*eda14cbcSMatt Macy &lnum, NULL, NULL, 0); 359*eda14cbcSMatt Macy if (rvalid) 360*eda14cbcSMatt Macy (void) zfs_prop_get_numeric(r, psc->sc_prop, 361*eda14cbcSMatt Macy &rnum, NULL, NULL, 0); 362*eda14cbcSMatt Macy } 363*eda14cbcSMatt Macy 364*eda14cbcSMatt Macy if (!lvalid && !rvalid) 365*eda14cbcSMatt Macy continue; 366*eda14cbcSMatt Macy else if (!lvalid) 367*eda14cbcSMatt Macy return (1); 368*eda14cbcSMatt Macy else if (!rvalid) 369*eda14cbcSMatt Macy return (-1); 370*eda14cbcSMatt Macy 371*eda14cbcSMatt Macy if (lstr) 372*eda14cbcSMatt Macy ret = strcmp(lstr, rstr); 373*eda14cbcSMatt Macy else if (lnum < rnum) 374*eda14cbcSMatt Macy ret = -1; 375*eda14cbcSMatt Macy else if (lnum > rnum) 376*eda14cbcSMatt Macy ret = 1; 377*eda14cbcSMatt Macy 378*eda14cbcSMatt Macy if (ret != 0) { 379*eda14cbcSMatt Macy if (psc->sc_reverse == B_TRUE) 380*eda14cbcSMatt Macy ret = (ret < 0) ? 1 : -1; 381*eda14cbcSMatt Macy return (ret); 382*eda14cbcSMatt Macy } 383*eda14cbcSMatt Macy } 384*eda14cbcSMatt Macy 385*eda14cbcSMatt Macy return (zfs_compare(larg, rarg, NULL)); 386*eda14cbcSMatt Macy } 387*eda14cbcSMatt Macy 388*eda14cbcSMatt Macy int 389*eda14cbcSMatt Macy zfs_for_each(int argc, char **argv, int flags, zfs_type_t types, 390*eda14cbcSMatt Macy zfs_sort_column_t *sortcol, zprop_list_t **proplist, int limit, 391*eda14cbcSMatt Macy zfs_iter_f callback, void *data) 392*eda14cbcSMatt Macy { 393*eda14cbcSMatt Macy callback_data_t cb = {0}; 394*eda14cbcSMatt Macy int ret = 0; 395*eda14cbcSMatt Macy zfs_node_t *node; 396*eda14cbcSMatt Macy uu_avl_walk_t *walk; 397*eda14cbcSMatt Macy 398*eda14cbcSMatt Macy avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t), 399*eda14cbcSMatt Macy offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT); 400*eda14cbcSMatt Macy 401*eda14cbcSMatt Macy if (avl_pool == NULL) 402*eda14cbcSMatt Macy nomem(); 403*eda14cbcSMatt Macy 404*eda14cbcSMatt Macy cb.cb_sortcol = sortcol; 405*eda14cbcSMatt Macy cb.cb_flags = flags; 406*eda14cbcSMatt Macy cb.cb_proplist = proplist; 407*eda14cbcSMatt Macy cb.cb_types = types; 408*eda14cbcSMatt Macy cb.cb_depth_limit = limit; 409*eda14cbcSMatt Macy /* 410*eda14cbcSMatt Macy * If cb_proplist is provided then in the zfs_handles created we 411*eda14cbcSMatt Macy * retain only those properties listed in cb_proplist and sortcol. 412*eda14cbcSMatt Macy * The rest are pruned. So, the caller should make sure that no other 413*eda14cbcSMatt Macy * properties other than those listed in cb_proplist/sortcol are 414*eda14cbcSMatt Macy * accessed. 415*eda14cbcSMatt Macy * 416*eda14cbcSMatt Macy * If cb_proplist is NULL then we retain all the properties. We 417*eda14cbcSMatt Macy * always retain the zoned property, which some other properties 418*eda14cbcSMatt Macy * need (userquota & friends), and the createtxg property, which 419*eda14cbcSMatt Macy * we need to sort snapshots. 420*eda14cbcSMatt Macy */ 421*eda14cbcSMatt Macy if (cb.cb_proplist && *cb.cb_proplist) { 422*eda14cbcSMatt Macy zprop_list_t *p = *cb.cb_proplist; 423*eda14cbcSMatt Macy 424*eda14cbcSMatt Macy while (p) { 425*eda14cbcSMatt Macy if (p->pl_prop >= ZFS_PROP_TYPE && 426*eda14cbcSMatt Macy p->pl_prop < ZFS_NUM_PROPS) { 427*eda14cbcSMatt Macy cb.cb_props_table[p->pl_prop] = B_TRUE; 428*eda14cbcSMatt Macy } 429*eda14cbcSMatt Macy p = p->pl_next; 430*eda14cbcSMatt Macy } 431*eda14cbcSMatt Macy 432*eda14cbcSMatt Macy while (sortcol) { 433*eda14cbcSMatt Macy if (sortcol->sc_prop >= ZFS_PROP_TYPE && 434*eda14cbcSMatt Macy sortcol->sc_prop < ZFS_NUM_PROPS) { 435*eda14cbcSMatt Macy cb.cb_props_table[sortcol->sc_prop] = B_TRUE; 436*eda14cbcSMatt Macy } 437*eda14cbcSMatt Macy sortcol = sortcol->sc_next; 438*eda14cbcSMatt Macy } 439*eda14cbcSMatt Macy 440*eda14cbcSMatt Macy cb.cb_props_table[ZFS_PROP_ZONED] = B_TRUE; 441*eda14cbcSMatt Macy cb.cb_props_table[ZFS_PROP_CREATETXG] = B_TRUE; 442*eda14cbcSMatt Macy } else { 443*eda14cbcSMatt Macy (void) memset(cb.cb_props_table, B_TRUE, 444*eda14cbcSMatt Macy sizeof (cb.cb_props_table)); 445*eda14cbcSMatt Macy } 446*eda14cbcSMatt Macy 447*eda14cbcSMatt Macy if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) 448*eda14cbcSMatt Macy nomem(); 449*eda14cbcSMatt Macy 450*eda14cbcSMatt Macy if (argc == 0) { 451*eda14cbcSMatt Macy /* 452*eda14cbcSMatt Macy * If given no arguments, iterate over all datasets. 453*eda14cbcSMatt Macy */ 454*eda14cbcSMatt Macy cb.cb_flags |= ZFS_ITER_RECURSE; 455*eda14cbcSMatt Macy ret = zfs_iter_root(g_zfs, zfs_callback, &cb); 456*eda14cbcSMatt Macy } else { 457*eda14cbcSMatt Macy int i; 458*eda14cbcSMatt Macy zfs_handle_t *zhp; 459*eda14cbcSMatt Macy zfs_type_t argtype; 460*eda14cbcSMatt Macy 461*eda14cbcSMatt Macy /* 462*eda14cbcSMatt Macy * If we're recursive, then we always allow filesystems as 463*eda14cbcSMatt Macy * arguments. If we also are interested in snapshots or 464*eda14cbcSMatt Macy * bookmarks, then we can take volumes as well. 465*eda14cbcSMatt Macy */ 466*eda14cbcSMatt Macy argtype = types; 467*eda14cbcSMatt Macy if (flags & ZFS_ITER_RECURSE) { 468*eda14cbcSMatt Macy argtype |= ZFS_TYPE_FILESYSTEM; 469*eda14cbcSMatt Macy if (types & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) 470*eda14cbcSMatt Macy argtype |= ZFS_TYPE_VOLUME; 471*eda14cbcSMatt Macy } 472*eda14cbcSMatt Macy 473*eda14cbcSMatt Macy for (i = 0; i < argc; i++) { 474*eda14cbcSMatt Macy if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) { 475*eda14cbcSMatt Macy zhp = zfs_path_to_zhandle(g_zfs, argv[i], 476*eda14cbcSMatt Macy argtype); 477*eda14cbcSMatt Macy } else { 478*eda14cbcSMatt Macy zhp = zfs_open(g_zfs, argv[i], argtype); 479*eda14cbcSMatt Macy } 480*eda14cbcSMatt Macy if (zhp != NULL) 481*eda14cbcSMatt Macy ret |= zfs_callback(zhp, &cb); 482*eda14cbcSMatt Macy else 483*eda14cbcSMatt Macy ret = 1; 484*eda14cbcSMatt Macy } 485*eda14cbcSMatt Macy } 486*eda14cbcSMatt Macy 487*eda14cbcSMatt Macy /* 488*eda14cbcSMatt Macy * At this point we've got our AVL tree full of zfs handles, so iterate 489*eda14cbcSMatt Macy * over each one and execute the real user callback. 490*eda14cbcSMatt Macy */ 491*eda14cbcSMatt Macy for (node = uu_avl_first(cb.cb_avl); node != NULL; 492*eda14cbcSMatt Macy node = uu_avl_next(cb.cb_avl, node)) 493*eda14cbcSMatt Macy ret |= callback(node->zn_handle, data); 494*eda14cbcSMatt Macy 495*eda14cbcSMatt Macy /* 496*eda14cbcSMatt Macy * Finally, clean up the AVL tree. 497*eda14cbcSMatt Macy */ 498*eda14cbcSMatt Macy if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) 499*eda14cbcSMatt Macy nomem(); 500*eda14cbcSMatt Macy 501*eda14cbcSMatt Macy while ((node = uu_avl_walk_next(walk)) != NULL) { 502*eda14cbcSMatt Macy uu_avl_remove(cb.cb_avl, node); 503*eda14cbcSMatt Macy zfs_close(node->zn_handle); 504*eda14cbcSMatt Macy free(node); 505*eda14cbcSMatt Macy } 506*eda14cbcSMatt Macy 507*eda14cbcSMatt Macy uu_avl_walk_end(walk); 508*eda14cbcSMatt Macy uu_avl_destroy(cb.cb_avl); 509*eda14cbcSMatt Macy uu_avl_pool_destroy(avl_pool); 510*eda14cbcSMatt Macy 511*eda14cbcSMatt Macy return (ret); 512*eda14cbcSMatt Macy } 513