zfs_iter.c revision 205198
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License (the "License"). 6168404Spjd * You may not use this file except in compliance with the License. 7168404Spjd * 8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9168404Spjd * or http://www.opensolaris.org/os/licensing. 10168404Spjd * See the License for the specific language governing permissions 11168404Spjd * and limitations under the License. 12168404Spjd * 13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15168404Spjd * If applicable, add the following below this CDDL HEADER, with the 16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 18168404Spjd * 19168404Spjd * CDDL HEADER END 20168404Spjd */ 21168404Spjd/* 22205198Sdelphij * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23168404Spjd * Use is subject to license terms. 24168404Spjd */ 25168404Spjd 26168404Spjd#include <libintl.h> 27168404Spjd#include <libuutil.h> 28168404Spjd#include <stddef.h> 29168404Spjd#include <stdio.h> 30168404Spjd#include <stdlib.h> 31168404Spjd#include <strings.h> 32168404Spjd 33168404Spjd#include <libzfs.h> 34168404Spjd 35168404Spjd#include "zfs_util.h" 36168404Spjd#include "zfs_iter.h" 37168404Spjd 38168404Spjd/* 39168404Spjd * This is a private interface used to gather up all the datasets specified on 40168404Spjd * the command line so that we can iterate over them in order. 41168404Spjd * 42168404Spjd * First, we iterate over all filesystems, gathering them together into an 43168404Spjd * AVL tree. We report errors for any explicitly specified datasets 44168404Spjd * that we couldn't open. 45168404Spjd * 46168404Spjd * When finished, we have an AVL tree of ZFS handles. We go through and execute 47168404Spjd * the provided callback for each one, passing whatever data the user supplied. 48168404Spjd */ 49168404Spjd 50168404Spjdtypedef struct zfs_node { 51168404Spjd zfs_handle_t *zn_handle; 52168404Spjd uu_avl_node_t zn_avlnode; 53168404Spjd} zfs_node_t; 54168404Spjd 55168404Spjdtypedef struct callback_data { 56168404Spjd uu_avl_t *cb_avl; 57185029Spjd int cb_flags; 58168404Spjd zfs_type_t cb_types; 59168404Spjd zfs_sort_column_t *cb_sortcol; 60185029Spjd zprop_list_t **cb_proplist; 61205198Sdelphij uint8_t cb_props_table[ZFS_NUM_PROPS]; 62168404Spjd} callback_data_t; 63168404Spjd 64168404Spjduu_avl_pool_t *avl_pool; 65168404Spjd 66168404Spjd/* 67185029Spjd * Include snaps if they were requested or if this a zfs list where types 68185029Spjd * were not specified and the "listsnapshots" property is set on this pool. 69185029Spjd */ 70185029Spjdstatic int 71185029Spjdzfs_include_snapshots(zfs_handle_t *zhp, callback_data_t *cb) 72185029Spjd{ 73185029Spjd zpool_handle_t *zph; 74185029Spjd 75185029Spjd if ((cb->cb_flags & ZFS_ITER_PROP_LISTSNAPS) == 0) 76185029Spjd return (cb->cb_types & ZFS_TYPE_SNAPSHOT); 77185029Spjd 78185029Spjd zph = zfs_get_pool_handle(zhp); 79185029Spjd return (zpool_get_prop_int(zph, ZPOOL_PROP_LISTSNAPS, NULL)); 80185029Spjd} 81185029Spjd 82185029Spjd/* 83185029Spjd * Called for each dataset. If the object is of an appropriate type, 84168404Spjd * add it to the avl tree and recurse over any children as necessary. 85168404Spjd */ 86185029Spjdstatic int 87168404Spjdzfs_callback(zfs_handle_t *zhp, void *data) 88168404Spjd{ 89168404Spjd callback_data_t *cb = data; 90168404Spjd int dontclose = 0; 91185029Spjd int include_snaps = zfs_include_snapshots(zhp, cb); 92168404Spjd 93185029Spjd if ((zfs_get_type(zhp) & cb->cb_types) || 94185029Spjd ((zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT) && include_snaps)) { 95168404Spjd uu_avl_index_t idx; 96168404Spjd zfs_node_t *node = safe_malloc(sizeof (zfs_node_t)); 97168404Spjd 98168404Spjd node->zn_handle = zhp; 99168404Spjd uu_avl_node_init(node, &node->zn_avlnode, avl_pool); 100168404Spjd if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol, 101168404Spjd &idx) == NULL) { 102205198Sdelphij 103205198Sdelphij if (cb->cb_proplist) { 104205198Sdelphij if ((*cb->cb_proplist) && 105205198Sdelphij !(*cb->cb_proplist)->pl_all) 106205198Sdelphij zfs_prune_proplist(zhp, 107205198Sdelphij cb->cb_props_table); 108205198Sdelphij 109205198Sdelphij if (zfs_expand_proplist(zhp, cb->cb_proplist) 110205198Sdelphij != 0) { 111205198Sdelphij free(node); 112205198Sdelphij return (-1); 113205198Sdelphij } 114168404Spjd } 115205198Sdelphij 116168404Spjd uu_avl_insert(cb->cb_avl, node, idx); 117168404Spjd dontclose = 1; 118168404Spjd } else { 119168404Spjd free(node); 120168404Spjd } 121168404Spjd } 122168404Spjd 123168404Spjd /* 124168404Spjd * Recurse if necessary. 125168404Spjd */ 126185029Spjd if (cb->cb_flags & ZFS_ITER_RECURSE) { 127185029Spjd if (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) 128185029Spjd (void) zfs_iter_filesystems(zhp, zfs_callback, data); 129185029Spjd if ((zfs_get_type(zhp) != ZFS_TYPE_SNAPSHOT) && include_snaps) 130185029Spjd (void) zfs_iter_snapshots(zhp, zfs_callback, data); 131185029Spjd } 132168404Spjd 133168404Spjd if (!dontclose) 134168404Spjd zfs_close(zhp); 135168404Spjd 136168404Spjd return (0); 137168404Spjd} 138168404Spjd 139168404Spjdint 140168404Spjdzfs_add_sort_column(zfs_sort_column_t **sc, const char *name, 141168404Spjd boolean_t reverse) 142168404Spjd{ 143168404Spjd zfs_sort_column_t *col; 144168404Spjd zfs_prop_t prop; 145168404Spjd 146185029Spjd if ((prop = zfs_name_to_prop(name)) == ZPROP_INVAL && 147168404Spjd !zfs_prop_user(name)) 148168404Spjd return (-1); 149168404Spjd 150168404Spjd col = safe_malloc(sizeof (zfs_sort_column_t)); 151168404Spjd 152168404Spjd col->sc_prop = prop; 153168404Spjd col->sc_reverse = reverse; 154185029Spjd if (prop == ZPROP_INVAL) { 155168404Spjd col->sc_user_prop = safe_malloc(strlen(name) + 1); 156168404Spjd (void) strcpy(col->sc_user_prop, name); 157168404Spjd } 158168404Spjd 159168404Spjd if (*sc == NULL) { 160168404Spjd col->sc_last = col; 161168404Spjd *sc = col; 162168404Spjd } else { 163168404Spjd (*sc)->sc_last->sc_next = col; 164168404Spjd (*sc)->sc_last = col; 165168404Spjd } 166168404Spjd 167168404Spjd return (0); 168168404Spjd} 169168404Spjd 170168404Spjdvoid 171168404Spjdzfs_free_sort_columns(zfs_sort_column_t *sc) 172168404Spjd{ 173168404Spjd zfs_sort_column_t *col; 174168404Spjd 175168404Spjd while (sc != NULL) { 176168404Spjd col = sc->sc_next; 177168404Spjd free(sc->sc_user_prop); 178168404Spjd free(sc); 179168404Spjd sc = col; 180168404Spjd } 181168404Spjd} 182168404Spjd 183168404Spjd/* ARGSUSED */ 184168404Spjdstatic int 185168404Spjdzfs_compare(const void *larg, const void *rarg, void *unused) 186168404Spjd{ 187168404Spjd zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 188168404Spjd zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 189168404Spjd const char *lname = zfs_get_name(l); 190168404Spjd const char *rname = zfs_get_name(r); 191168404Spjd char *lat, *rat; 192168404Spjd uint64_t lcreate, rcreate; 193168404Spjd int ret; 194168404Spjd 195168404Spjd lat = (char *)strchr(lname, '@'); 196168404Spjd rat = (char *)strchr(rname, '@'); 197168404Spjd 198168404Spjd if (lat != NULL) 199168404Spjd *lat = '\0'; 200168404Spjd if (rat != NULL) 201168404Spjd *rat = '\0'; 202168404Spjd 203168404Spjd ret = strcmp(lname, rname); 204168404Spjd if (ret == 0) { 205168404Spjd /* 206168404Spjd * If we're comparing a dataset to one of its snapshots, we 207168404Spjd * always make the full dataset first. 208168404Spjd */ 209168404Spjd if (lat == NULL) { 210168404Spjd ret = -1; 211168404Spjd } else if (rat == NULL) { 212168404Spjd ret = 1; 213168404Spjd } else { 214168404Spjd /* 215168404Spjd * If we have two snapshots from the same dataset, then 216168404Spjd * we want to sort them according to creation time. We 217168404Spjd * use the hidden CREATETXG property to get an absolute 218168404Spjd * ordering of snapshots. 219168404Spjd */ 220168404Spjd lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); 221168404Spjd rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); 222168404Spjd 223168404Spjd if (lcreate < rcreate) 224168404Spjd ret = -1; 225168404Spjd else if (lcreate > rcreate) 226168404Spjd ret = 1; 227168404Spjd } 228168404Spjd } 229168404Spjd 230168404Spjd if (lat != NULL) 231168404Spjd *lat = '@'; 232168404Spjd if (rat != NULL) 233168404Spjd *rat = '@'; 234168404Spjd 235168404Spjd return (ret); 236168404Spjd} 237168404Spjd 238168404Spjd/* 239168404Spjd * Sort datasets by specified columns. 240168404Spjd * 241168404Spjd * o Numeric types sort in ascending order. 242168404Spjd * o String types sort in alphabetical order. 243168404Spjd * o Types inappropriate for a row sort that row to the literal 244168404Spjd * bottom, regardless of the specified ordering. 245168404Spjd * 246168404Spjd * If no sort columns are specified, or two datasets compare equally 247168404Spjd * across all specified columns, they are sorted alphabetically by name 248168404Spjd * with snapshots grouped under their parents. 249168404Spjd */ 250168404Spjdstatic int 251168404Spjdzfs_sort(const void *larg, const void *rarg, void *data) 252168404Spjd{ 253168404Spjd zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; 254168404Spjd zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; 255168404Spjd zfs_sort_column_t *sc = (zfs_sort_column_t *)data; 256168404Spjd zfs_sort_column_t *psc; 257168404Spjd 258168404Spjd for (psc = sc; psc != NULL; psc = psc->sc_next) { 259168404Spjd char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN]; 260168404Spjd char *lstr, *rstr; 261168404Spjd uint64_t lnum, rnum; 262168404Spjd boolean_t lvalid, rvalid; 263168404Spjd int ret = 0; 264168404Spjd 265168404Spjd /* 266168404Spjd * We group the checks below the generic code. If 'lstr' and 267168404Spjd * 'rstr' are non-NULL, then we do a string based comparison. 268168404Spjd * Otherwise, we compare 'lnum' and 'rnum'. 269168404Spjd */ 270168404Spjd lstr = rstr = NULL; 271185029Spjd if (psc->sc_prop == ZPROP_INVAL) { 272168404Spjd nvlist_t *luser, *ruser; 273168404Spjd nvlist_t *lval, *rval; 274168404Spjd 275168404Spjd luser = zfs_get_user_props(l); 276168404Spjd ruser = zfs_get_user_props(r); 277168404Spjd 278168404Spjd lvalid = (nvlist_lookup_nvlist(luser, 279168404Spjd psc->sc_user_prop, &lval) == 0); 280168404Spjd rvalid = (nvlist_lookup_nvlist(ruser, 281168404Spjd psc->sc_user_prop, &rval) == 0); 282168404Spjd 283168404Spjd if (lvalid) 284168404Spjd verify(nvlist_lookup_string(lval, 285185029Spjd ZPROP_VALUE, &lstr) == 0); 286168404Spjd if (rvalid) 287168404Spjd verify(nvlist_lookup_string(rval, 288185029Spjd ZPROP_VALUE, &rstr) == 0); 289168404Spjd 290168404Spjd } else if (zfs_prop_is_string(psc->sc_prop)) { 291168404Spjd lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf, 292168404Spjd sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0); 293168404Spjd rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf, 294168404Spjd sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0); 295168404Spjd 296168404Spjd lstr = lbuf; 297168404Spjd rstr = rbuf; 298168404Spjd } else { 299168404Spjd lvalid = zfs_prop_valid_for_type(psc->sc_prop, 300168404Spjd zfs_get_type(l)); 301168404Spjd rvalid = zfs_prop_valid_for_type(psc->sc_prop, 302168404Spjd zfs_get_type(r)); 303168404Spjd 304168404Spjd if (lvalid) 305168404Spjd (void) zfs_prop_get_numeric(l, psc->sc_prop, 306168404Spjd &lnum, NULL, NULL, 0); 307168404Spjd if (rvalid) 308168404Spjd (void) zfs_prop_get_numeric(r, psc->sc_prop, 309168404Spjd &rnum, NULL, NULL, 0); 310168404Spjd } 311168404Spjd 312168404Spjd if (!lvalid && !rvalid) 313168404Spjd continue; 314168404Spjd else if (!lvalid) 315168404Spjd return (1); 316168404Spjd else if (!rvalid) 317168404Spjd return (-1); 318168404Spjd 319168404Spjd if (lstr) 320168404Spjd ret = strcmp(lstr, rstr); 321185029Spjd else if (lnum < rnum) 322168404Spjd ret = -1; 323168404Spjd else if (lnum > rnum) 324168404Spjd ret = 1; 325168404Spjd 326168404Spjd if (ret != 0) { 327168404Spjd if (psc->sc_reverse == B_TRUE) 328168404Spjd ret = (ret < 0) ? 1 : -1; 329168404Spjd return (ret); 330168404Spjd } 331168404Spjd } 332168404Spjd 333168404Spjd return (zfs_compare(larg, rarg, NULL)); 334168404Spjd} 335168404Spjd 336168404Spjdint 337185029Spjdzfs_for_each(int argc, char **argv, int flags, zfs_type_t types, 338185029Spjd zfs_sort_column_t *sortcol, zprop_list_t **proplist, 339185029Spjd zfs_iter_f callback, void *data) 340168404Spjd{ 341205198Sdelphij callback_data_t cb = {0}; 342168404Spjd int ret = 0; 343168404Spjd zfs_node_t *node; 344168404Spjd uu_avl_walk_t *walk; 345168404Spjd 346168404Spjd avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t), 347168404Spjd offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT); 348168404Spjd 349168404Spjd if (avl_pool == NULL) { 350168404Spjd (void) fprintf(stderr, 351168404Spjd gettext("internal error: out of memory\n")); 352168404Spjd exit(1); 353168404Spjd } 354168404Spjd 355168404Spjd cb.cb_sortcol = sortcol; 356185029Spjd cb.cb_flags = flags; 357168404Spjd cb.cb_proplist = proplist; 358168404Spjd cb.cb_types = types; 359205198Sdelphij 360205198Sdelphij /* 361205198Sdelphij * If cb_proplist is provided then in the zfs_handles created we 362205198Sdelphij * retain only those properties listed in cb_proplist and sortcol. 363205198Sdelphij * The rest are pruned. So, the caller should make sure that no other 364205198Sdelphij * properties other than those listed in cb_proplist/sortcol are 365205198Sdelphij * accessed. 366205198Sdelphij * 367205198Sdelphij * If cb_proplist is NULL then we retain all the properties. 368205198Sdelphij */ 369205198Sdelphij if (cb.cb_proplist && *cb.cb_proplist) { 370205198Sdelphij zprop_list_t *p = *cb.cb_proplist; 371205198Sdelphij 372205198Sdelphij while (p) { 373205198Sdelphij if (p->pl_prop >= ZFS_PROP_TYPE && 374205198Sdelphij p->pl_prop < ZFS_NUM_PROPS) { 375205198Sdelphij cb.cb_props_table[p->pl_prop] = B_TRUE; 376205198Sdelphij } 377205198Sdelphij p = p->pl_next; 378205198Sdelphij } 379205198Sdelphij 380205198Sdelphij while (sortcol) { 381205198Sdelphij if (sortcol->sc_prop >= ZFS_PROP_TYPE && 382205198Sdelphij sortcol->sc_prop < ZFS_NUM_PROPS) { 383205198Sdelphij cb.cb_props_table[sortcol->sc_prop] = B_TRUE; 384205198Sdelphij } 385205198Sdelphij sortcol = sortcol->sc_next; 386205198Sdelphij } 387205198Sdelphij } else { 388205198Sdelphij (void) memset(cb.cb_props_table, B_TRUE, 389205198Sdelphij sizeof (cb.cb_props_table)); 390205198Sdelphij } 391205198Sdelphij 392168404Spjd if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) { 393168404Spjd (void) fprintf(stderr, 394168404Spjd gettext("internal error: out of memory\n")); 395168404Spjd exit(1); 396168404Spjd } 397168404Spjd 398168404Spjd if (argc == 0) { 399168404Spjd /* 400168404Spjd * If given no arguments, iterate over all datasets. 401168404Spjd */ 402185029Spjd cb.cb_flags |= ZFS_ITER_RECURSE; 403168404Spjd ret = zfs_iter_root(g_zfs, zfs_callback, &cb); 404168404Spjd } else { 405168404Spjd int i; 406168404Spjd zfs_handle_t *zhp; 407168404Spjd zfs_type_t argtype; 408168404Spjd 409168404Spjd /* 410168404Spjd * If we're recursive, then we always allow filesystems as 411168404Spjd * arguments. If we also are interested in snapshots, then we 412168404Spjd * can take volumes as well. 413168404Spjd */ 414168404Spjd argtype = types; 415185029Spjd if (flags & ZFS_ITER_RECURSE) { 416168404Spjd argtype |= ZFS_TYPE_FILESYSTEM; 417168404Spjd if (types & ZFS_TYPE_SNAPSHOT) 418168404Spjd argtype |= ZFS_TYPE_VOLUME; 419168404Spjd } 420168404Spjd 421168404Spjd for (i = 0; i < argc; i++) { 422185029Spjd if (flags & ZFS_ITER_ARGS_CAN_BE_PATHS) { 423168404Spjd zhp = zfs_path_to_zhandle(g_zfs, argv[i], 424168404Spjd argtype); 425168404Spjd } else { 426168404Spjd zhp = zfs_open(g_zfs, argv[i], argtype); 427168404Spjd } 428168404Spjd if (zhp != NULL) 429168404Spjd ret |= zfs_callback(zhp, &cb); 430168404Spjd else 431168404Spjd ret = 1; 432168404Spjd } 433168404Spjd } 434168404Spjd 435168404Spjd /* 436168404Spjd * At this point we've got our AVL tree full of zfs handles, so iterate 437168404Spjd * over each one and execute the real user callback. 438168404Spjd */ 439168404Spjd for (node = uu_avl_first(cb.cb_avl); node != NULL; 440168404Spjd node = uu_avl_next(cb.cb_avl, node)) 441168404Spjd ret |= callback(node->zn_handle, data); 442168404Spjd 443168404Spjd /* 444168404Spjd * Finally, clean up the AVL tree. 445168404Spjd */ 446168404Spjd if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) { 447168404Spjd (void) fprintf(stderr, 448168404Spjd gettext("internal error: out of memory")); 449168404Spjd exit(1); 450168404Spjd } 451168404Spjd 452168404Spjd while ((node = uu_avl_walk_next(walk)) != NULL) { 453168404Spjd uu_avl_remove(cb.cb_avl, node); 454168404Spjd zfs_close(node->zn_handle); 455168404Spjd free(node); 456168404Spjd } 457168404Spjd 458168404Spjd uu_avl_walk_end(walk); 459168404Spjd uu_avl_destroy(cb.cb_avl); 460168404Spjd uu_avl_pool_destroy(avl_pool); 461168404Spjd 462168404Spjd return (ret); 463168404Spjd} 464