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.
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.
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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
41 * This is a private interface used to gather up all the datasets specified on
42 * the command line so that we can iterate over them in order.
44 * First, we iterate over all filesystems, gathering them together into an
45 * AVL tree. We report errors for any explicitly specified datasets
46 * that we couldn't open.
48 * When finished, we have an AVL tree of ZFS handles. We go through and execute
49 * the provided callback for each one, passing whatever data the user supplied.
52 typedef struct zfs_node {
53 zfs_handle_t *zn_handle;
54 uu_avl_node_t zn_avlnode;
57 typedef struct callback_data {
61 zfs_sort_column_t *cb_sortcol;
62 zfs_proplist_t **cb_proplist;
65 uu_avl_pool_t *avl_pool;
68 * Called for each dataset. If the object the object is of an appropriate type,
69 * add it to the avl tree and recurse over any children as necessary.
72 zfs_callback(zfs_handle_t *zhp, void *data)
74 callback_data_t *cb = data;
78 * If this object is of the appropriate type, add it to the AVL tree.
80 if (zfs_get_type(zhp) & cb->cb_types) {
82 zfs_node_t *node = safe_malloc(sizeof (zfs_node_t));
84 node->zn_handle = zhp;
85 uu_avl_node_init(node, &node->zn_avlnode, avl_pool);
86 if (uu_avl_find(cb->cb_avl, node, cb->cb_sortcol,
88 if (cb->cb_proplist &&
89 zfs_expand_proplist(zhp, cb->cb_proplist) != 0) {
93 uu_avl_insert(cb->cb_avl, node, idx);
101 * Recurse if necessary.
103 if (cb->cb_recurse && (zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM ||
104 (zfs_get_type(zhp) == ZFS_TYPE_VOLUME && (cb->cb_types &
105 ZFS_TYPE_SNAPSHOT))))
106 (void) zfs_iter_children(zhp, zfs_callback, data);
115 zfs_add_sort_column(zfs_sort_column_t **sc, const char *name,
118 zfs_sort_column_t *col;
121 if ((prop = zfs_name_to_prop(name)) == ZFS_PROP_INVAL &&
122 !zfs_prop_user(name))
125 col = safe_malloc(sizeof (zfs_sort_column_t));
128 col->sc_reverse = reverse;
129 if (prop == ZFS_PROP_INVAL) {
130 col->sc_user_prop = safe_malloc(strlen(name) + 1);
131 (void) strcpy(col->sc_user_prop, name);
138 (*sc)->sc_last->sc_next = col;
139 (*sc)->sc_last = col;
146 zfs_free_sort_columns(zfs_sort_column_t *sc)
148 zfs_sort_column_t *col;
152 free(sc->sc_user_prop);
160 zfs_compare(const void *larg, const void *rarg, void *unused)
162 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
163 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
164 const char *lname = zfs_get_name(l);
165 const char *rname = zfs_get_name(r);
167 uint64_t lcreate, rcreate;
170 lat = (char *)strchr(lname, '@');
171 rat = (char *)strchr(rname, '@');
178 ret = strcmp(lname, rname);
181 * If we're comparing a dataset to one of its snapshots, we
182 * always make the full dataset first.
186 } else if (rat == NULL) {
190 * If we have two snapshots from the same dataset, then
191 * we want to sort them according to creation time. We
192 * use the hidden CREATETXG property to get an absolute
193 * ordering of snapshots.
195 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
196 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
198 if (lcreate < rcreate)
200 else if (lcreate > rcreate)
214 * Sort datasets by specified columns.
216 * o Numeric types sort in ascending order.
217 * o String types sort in alphabetical order.
218 * o Types inappropriate for a row sort that row to the literal
219 * bottom, regardless of the specified ordering.
221 * If no sort columns are specified, or two datasets compare equally
222 * across all specified columns, they are sorted alphabetically by name
223 * with snapshots grouped under their parents.
226 zfs_sort(const void *larg, const void *rarg, void *data)
228 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
229 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
230 zfs_sort_column_t *sc = (zfs_sort_column_t *)data;
231 zfs_sort_column_t *psc;
233 for (psc = sc; psc != NULL; psc = psc->sc_next) {
234 char lbuf[ZFS_MAXPROPLEN], rbuf[ZFS_MAXPROPLEN];
237 boolean_t lvalid, rvalid;
241 * We group the checks below the generic code. If 'lstr' and
242 * 'rstr' are non-NULL, then we do a string based comparison.
243 * Otherwise, we compare 'lnum' and 'rnum'.
246 if (psc->sc_prop == ZFS_PROP_INVAL) {
247 nvlist_t *luser, *ruser;
248 nvlist_t *lval, *rval;
250 luser = zfs_get_user_props(l);
251 ruser = zfs_get_user_props(r);
253 lvalid = (nvlist_lookup_nvlist(luser,
254 psc->sc_user_prop, &lval) == 0);
255 rvalid = (nvlist_lookup_nvlist(ruser,
256 psc->sc_user_prop, &rval) == 0);
259 verify(nvlist_lookup_string(lval,
260 ZFS_PROP_VALUE, &lstr) == 0);
262 verify(nvlist_lookup_string(rval,
263 ZFS_PROP_VALUE, &rstr) == 0);
265 } else if (zfs_prop_is_string(psc->sc_prop)) {
266 lvalid = (zfs_prop_get(l, psc->sc_prop, lbuf,
267 sizeof (lbuf), NULL, NULL, 0, B_TRUE) == 0);
268 rvalid = (zfs_prop_get(r, psc->sc_prop, rbuf,
269 sizeof (rbuf), NULL, NULL, 0, B_TRUE) == 0);
274 lvalid = zfs_prop_valid_for_type(psc->sc_prop,
276 rvalid = zfs_prop_valid_for_type(psc->sc_prop,
280 (void) zfs_prop_get_numeric(l, psc->sc_prop,
281 &lnum, NULL, NULL, 0);
283 (void) zfs_prop_get_numeric(r, psc->sc_prop,
284 &rnum, NULL, NULL, 0);
287 if (!lvalid && !rvalid)
295 ret = strcmp(lstr, rstr);
298 else if (lnum > rnum)
302 if (psc->sc_reverse == B_TRUE)
303 ret = (ret < 0) ? 1 : -1;
308 return (zfs_compare(larg, rarg, NULL));
312 zfs_for_each(int argc, char **argv, boolean_t recurse, zfs_type_t types,
313 zfs_sort_column_t *sortcol, zfs_proplist_t **proplist, zfs_iter_f callback,
314 void *data, boolean_t args_can_be_paths)
321 avl_pool = uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t),
322 offsetof(zfs_node_t, zn_avlnode), zfs_sort, UU_DEFAULT);
324 if (avl_pool == NULL) {
325 (void) fprintf(stderr,
326 gettext("internal error: out of memory\n"));
330 cb.cb_sortcol = sortcol;
331 cb.cb_recurse = recurse;
332 cb.cb_proplist = proplist;
334 if ((cb.cb_avl = uu_avl_create(avl_pool, NULL, UU_DEFAULT)) == NULL) {
335 (void) fprintf(stderr,
336 gettext("internal error: out of memory\n"));
342 * If given no arguments, iterate over all datasets.
345 ret = zfs_iter_root(g_zfs, zfs_callback, &cb);
352 * If we're recursive, then we always allow filesystems as
353 * arguments. If we also are interested in snapshots, then we
354 * can take volumes as well.
358 argtype |= ZFS_TYPE_FILESYSTEM;
359 if (types & ZFS_TYPE_SNAPSHOT)
360 argtype |= ZFS_TYPE_VOLUME;
363 for (i = 0; i < argc; i++) {
364 if (args_can_be_paths) {
365 zhp = zfs_path_to_zhandle(g_zfs, argv[i],
368 zhp = zfs_open(g_zfs, argv[i], argtype);
371 ret |= zfs_callback(zhp, &cb);
378 * At this point we've got our AVL tree full of zfs handles, so iterate
379 * over each one and execute the real user callback.
381 for (node = uu_avl_first(cb.cb_avl); node != NULL;
382 node = uu_avl_next(cb.cb_avl, node))
383 ret |= callback(node->zn_handle, data);
386 * Finally, clean up the AVL tree.
388 if ((walk = uu_avl_walk_start(cb.cb_avl, UU_WALK_ROBUST)) == NULL) {
389 (void) fprintf(stderr,
390 gettext("internal error: out of memory"));
394 while ((node = uu_avl_walk_next(walk)) != NULL) {
395 uu_avl_remove(cb.cb_avl, node);
396 zfs_close(node->zn_handle);
400 uu_avl_walk_end(walk);
401 uu_avl_destroy(cb.cb_avl);
402 uu_avl_pool_destroy(avl_pool);