usr/src/cmd/zfs/zfs_iter.c
changeset 8802 010b31dd4c53
parent 7538 18c2451107fd
child 9365 7838a22eccd6
equal deleted inserted replaced
8801:7a4719d96e14 8802:010b31dd4c53
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
    18  *
    18  *
    19  * CDDL HEADER END
    19  * CDDL HEADER END
    20  */
    20  */
    21 /*
    21 /*
    22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
    22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
    23  * Use is subject to license terms.
    23  * Use is subject to license terms.
    24  */
    24  */
    25 
    25 
    26 #include <libintl.h>
    26 #include <libintl.h>
    27 #include <libuutil.h>
    27 #include <libuutil.h>
    56 	uu_avl_t	*cb_avl;
    56 	uu_avl_t	*cb_avl;
    57 	int		cb_flags;
    57 	int		cb_flags;
    58 	zfs_type_t	cb_types;
    58 	zfs_type_t	cb_types;
    59 	zfs_sort_column_t *cb_sortcol;
    59 	zfs_sort_column_t *cb_sortcol;
    60 	zprop_list_t	**cb_proplist;
    60 	zprop_list_t	**cb_proplist;
       
    61 	uint8_t		cb_props_table[ZFS_NUM_PROPS];
    61 } callback_data_t;
    62 } callback_data_t;
    62 
    63 
    63 uu_avl_pool_t *avl_pool;
    64 uu_avl_pool_t *avl_pool;
    64 
    65 
    65 /*
    66 /*
    96 
    97 
    97 		node->zn_handle = zhp;
    98 		node->zn_handle = zhp;
    98 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
    99 		uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
    99 		if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
   100 		if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
   100 		    &idx) == NULL) {
   101 		    &idx) == NULL) {
   101 			if (cb->cb_proplist &&
   102 
   102 			    zfs_expand_proplist(zhp, cb->cb_proplist) != 0) {
   103 			if (cb->cb_proplist) {
   103 				free(node);
   104 				if ((*cb->cb_proplist) &&
   104 				return (-1);
   105 				    !(*cb->cb_proplist)->pl_all)
       
   106 					zfs_prune_proplist(zhp,
       
   107 					    cb->cb_props_table);
       
   108 
       
   109 				if (zfs_expand_proplist(zhp, cb->cb_proplist)
       
   110 				    != 0) {
       
   111 					free(node);
       
   112 					return (-1);
       
   113 				}
   105 			}
   114 			}
       
   115 
   106 			uu_avl_insert(cb->cb_avl, node, idx);
   116 			uu_avl_insert(cb->cb_avl, node, idx);
   107 			dontclose = 1;
   117 			dontclose = 1;
   108 		} else {
   118 		} else {
   109 			free(node);
   119 			free(node);
   110 		}
   120 		}
   326 int
   336 int
   327 zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
   337 zfs_for_each(int argc, char **argv, int flags, zfs_type_t types,
   328     zfs_sort_column_t *sortcol, zprop_list_t **proplist,
   338     zfs_sort_column_t *sortcol, zprop_list_t **proplist,
   329     zfs_iter_f callback, void *data)
   339     zfs_iter_f callback, void *data)
   330 {
   340 {
   331 	callback_data_t cb;
   341 	callback_data_t cb = {0};
   332 	int ret = 0;
   342 	int ret = 0;
   333 	zfs_node_t *node;
   343 	zfs_node_t *node;
   334 	uu_avl_walk_t *walk;
   344 	uu_avl_walk_t *walk;
   335 
   345 
   336 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
   346 	avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
   344 
   354 
   345 	cb.cb_sortcol = sortcol;
   355 	cb.cb_sortcol = sortcol;
   346 	cb.cb_flags = flags;
   356 	cb.cb_flags = flags;
   347 	cb.cb_proplist = proplist;
   357 	cb.cb_proplist = proplist;
   348 	cb.cb_types = types;
   358 	cb.cb_types = types;
       
   359 
       
   360 	/*
       
   361 	 * If cb_proplist is provided then in the zfs_handles created  we
       
   362 	 * retain only those properties listed in cb_proplist and sortcol.
       
   363 	 * The rest are pruned. So, the caller should make sure that no other
       
   364 	 * properties other than those listed in cb_proplist/sortcol are
       
   365 	 * accessed.
       
   366 	 *
       
   367 	 * If cb_proplist is NULL then we retain all the properties.
       
   368 	 */
       
   369 	if (cb.cb_proplist && *cb.cb_proplist) {
       
   370 		zprop_list_t *p = *cb.cb_proplist;
       
   371 
       
   372 		while (p) {
       
   373 			if (p->pl_prop >= ZFS_PROP_TYPE &&
       
   374 			    p->pl_prop < ZFS_NUM_PROPS) {
       
   375 				cb.cb_props_table[p->pl_prop] = B_TRUE;
       
   376 			}
       
   377 			p = p->pl_next;
       
   378 		}
       
   379 
       
   380 		while (sortcol) {
       
   381 			if (sortcol->sc_prop >= ZFS_PROP_TYPE &&
       
   382 			    sortcol->sc_prop < ZFS_NUM_PROPS) {
       
   383 				cb.cb_props_table[sortcol->sc_prop] = B_TRUE;
       
   384 			}
       
   385 			sortcol = sortcol->sc_next;
       
   386 		}
       
   387 	} else {
       
   388 		(void) memset(cb.cb_props_table, B_TRUE,
       
   389 		    sizeof (cb.cb_props_table));
       
   390 	}
       
   391 
   349 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) {
   392 	if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) {
   350 		(void) fprintf(stderr,
   393 		(void) fprintf(stderr,
   351 		    gettext("internal error: out of memory\n"));
   394 		    gettext("internal error: out of memory\n"));
   352 		exit(1);
   395 		exit(1);
   353 	}
   396 	}