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