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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
39 * This is a private interface used to gather up all the datasets specified on
40 * the command line so that we can iterate over them in order.
42 * First, we iterate over all filesystems, gathering them together into an
43 * AVL tree. We report errors for any explicitly specified datasets
44 * that we couldn't open.
46 * When finished, we have an AVL tree of ZFS handles. We go through and execute
47 * the provided callback for each one, passing whatever data the user supplied.
50 typedef struct zfs_node
{
51 zfs_handle_t
*zn_handle
;
52 uu_avl_node_t zn_avlnode
;
55 typedef struct callback_data
{
59 zfs_sort_column_t
*cb_sortcol
;
60 zprop_list_t
**cb_proplist
;
63 uu_avl_pool_t
*avl_pool
;
66 * Include snaps if they were requested or if this a zfs list where types
67 * were not specified and the "listsnapshots" property is set on this pool.
70 zfs_include_snapshots(zfs_handle_t
*zhp
, callback_data_t
*cb
)
74 if ((cb
->cb_flags
& ZFS_ITER_PROP_LISTSNAPS
) == 0)
75 return (cb
->cb_types
& ZFS_TYPE_SNAPSHOT
);
77 zph
= zfs_get_pool_handle(zhp
);
78 return (zpool_get_prop_int(zph
, ZPOOL_PROP_LISTSNAPS
, NULL
));
82 * Called for each dataset. If the object is of an appropriate type,
83 * add it to the avl tree and recurse over any children as necessary.
86 zfs_callback(zfs_handle_t
*zhp
, void *data
)
88 callback_data_t
*cb
= data
;
90 int include_snaps
= zfs_include_snapshots(zhp
, cb
);
92 if ((zfs_get_type(zhp
) & cb
->cb_types
) ||
93 ((zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) && include_snaps
)) {
95 zfs_node_t
*node
= safe_malloc(sizeof (zfs_node_t
));
97 node
->zn_handle
= zhp
;
98 uu_avl_node_init(node
, &node
->zn_avlnode
, avl_pool
);
99 if (uu_avl_find(cb
->cb_avl
, node
, cb
->cb_sortcol
,
101 if (cb
->cb_proplist
&&
102 zfs_expand_proplist(zhp
, cb
->cb_proplist
) != 0) {
106 uu_avl_insert(cb
->cb_avl
, node
, idx
);
114 * Recurse if necessary.
116 if (cb
->cb_flags
& ZFS_ITER_RECURSE
) {
117 if (zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
)
118 (void) zfs_iter_filesystems(zhp
, zfs_callback
, data
);
119 if ((zfs_get_type(zhp
) != ZFS_TYPE_SNAPSHOT
) && include_snaps
)
120 (void) zfs_iter_snapshots(zhp
, zfs_callback
, data
);
130 zfs_add_sort_column(zfs_sort_column_t
**sc
, const char *name
,
133 zfs_sort_column_t
*col
;
136 if ((prop
= zfs_name_to_prop(name
)) == ZPROP_INVAL
&&
137 !zfs_prop_user(name
))
140 col
= safe_malloc(sizeof (zfs_sort_column_t
));
143 col
->sc_reverse
= reverse
;
144 if (prop
== ZPROP_INVAL
) {
145 col
->sc_user_prop
= safe_malloc(strlen(name
) + 1);
146 (void) strcpy(col
->sc_user_prop
, name
);
153 (*sc
)->sc_last
->sc_next
= col
;
154 (*sc
)->sc_last
= col
;
161 zfs_free_sort_columns(zfs_sort_column_t
*sc
)
163 zfs_sort_column_t
*col
;
167 free(sc
->sc_user_prop
);
175 zfs_compare(const void *larg
, const void *rarg
, void *unused
)
177 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
178 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
179 const char *lname
= zfs_get_name(l
);
180 const char *rname
= zfs_get_name(r
);
182 uint64_t lcreate
, rcreate
;
185 lat
= (char *)strchr(lname
, '@');
186 rat
= (char *)strchr(rname
, '@');
193 ret
= strcmp(lname
, rname
);
196 * If we're comparing a dataset to one of its snapshots, we
197 * always make the full dataset first.
201 } else if (rat
== NULL
) {
205 * If we have two snapshots from the same dataset, then
206 * we want to sort them according to creation time. We
207 * use the hidden CREATETXG property to get an absolute
208 * ordering of snapshots.
210 lcreate
= zfs_prop_get_int(l
, ZFS_PROP_CREATETXG
);
211 rcreate
= zfs_prop_get_int(r
, ZFS_PROP_CREATETXG
);
213 if (lcreate
< rcreate
)
215 else if (lcreate
> rcreate
)
229 * Sort datasets by specified columns.
231 * o Numeric types sort in ascending order.
232 * o String types sort in alphabetical order.
233 * o Types inappropriate for a row sort that row to the literal
234 * bottom, regardless of the specified ordering.
236 * If no sort columns are specified, or two datasets compare equally
237 * across all specified columns, they are sorted alphabetically by name
238 * with snapshots grouped under their parents.
241 zfs_sort(const void *larg
, const void *rarg
, void *data
)
243 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
244 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
245 zfs_sort_column_t
*sc
= (zfs_sort_column_t
*)data
;
246 zfs_sort_column_t
*psc
;
248 for (psc
= sc
; psc
!= NULL
; psc
= psc
->sc_next
) {
249 char lbuf
[ZFS_MAXPROPLEN
], rbuf
[ZFS_MAXPROPLEN
];
252 boolean_t lvalid
, rvalid
;
256 * We group the checks below the generic code. If 'lstr' and
257 * 'rstr' are non-NULL, then we do a string based comparison.
258 * Otherwise, we compare 'lnum' and 'rnum'.
261 if (psc
->sc_prop
== ZPROP_INVAL
) {
262 nvlist_t
*luser
, *ruser
;
263 nvlist_t
*lval
, *rval
;
265 luser
= zfs_get_user_props(l
);
266 ruser
= zfs_get_user_props(r
);
268 lvalid
= (nvlist_lookup_nvlist(luser
,
269 psc
->sc_user_prop
, &lval
) == 0);
270 rvalid
= (nvlist_lookup_nvlist(ruser
,
271 psc
->sc_user_prop
, &rval
) == 0);
274 verify(nvlist_lookup_string(lval
,
275 ZPROP_VALUE
, &lstr
) == 0);
277 verify(nvlist_lookup_string(rval
,
278 ZPROP_VALUE
, &rstr
) == 0);
280 } else if (zfs_prop_is_string(psc
->sc_prop
)) {
281 lvalid
= (zfs_prop_get(l
, psc
->sc_prop
, lbuf
,
282 sizeof (lbuf
), NULL
, NULL
, 0, B_TRUE
) == 0);
283 rvalid
= (zfs_prop_get(r
, psc
->sc_prop
, rbuf
,
284 sizeof (rbuf
), NULL
, NULL
, 0, B_TRUE
) == 0);
289 lvalid
= zfs_prop_valid_for_type(psc
->sc_prop
,
291 rvalid
= zfs_prop_valid_for_type(psc
->sc_prop
,
295 (void) zfs_prop_get_numeric(l
, psc
->sc_prop
,
296 &lnum
, NULL
, NULL
, 0);
298 (void) zfs_prop_get_numeric(r
, psc
->sc_prop
,
299 &rnum
, NULL
, NULL
, 0);
302 if (!lvalid
&& !rvalid
)
310 ret
= strcmp(lstr
, rstr
);
311 else if (lnum
< rnum
)
313 else if (lnum
> rnum
)
317 if (psc
->sc_reverse
== B_TRUE
)
318 ret
= (ret
< 0) ? 1 : -1;
323 return (zfs_compare(larg
, rarg
, NULL
));
327 zfs_for_each(int argc
, char **argv
, int flags
, zfs_type_t types
,
328 zfs_sort_column_t
*sortcol
, zprop_list_t
**proplist
,
329 zfs_iter_f callback
, void *data
)
336 avl_pool
= uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t
),
337 offsetof(zfs_node_t
, zn_avlnode
), zfs_sort
, UU_DEFAULT
);
339 if (avl_pool
== NULL
) {
340 (void) fprintf(stderr
,
341 gettext("internal error: out of memory\n"));
345 cb
.cb_sortcol
= sortcol
;
347 cb
.cb_proplist
= proplist
;
349 if ((cb
.cb_avl
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
) {
350 (void) fprintf(stderr
,
351 gettext("internal error: out of memory\n"));
357 * If given no arguments, iterate over all datasets.
359 cb
.cb_flags
|= ZFS_ITER_RECURSE
;
360 ret
= zfs_iter_root(g_zfs
, zfs_callback
, &cb
);
367 * If we're recursive, then we always allow filesystems as
368 * arguments. If we also are interested in snapshots, then we
369 * can take volumes as well.
372 if (flags
& ZFS_ITER_RECURSE
) {
373 argtype
|= ZFS_TYPE_FILESYSTEM
;
374 if (types
& ZFS_TYPE_SNAPSHOT
)
375 argtype
|= ZFS_TYPE_VOLUME
;
378 for (i
= 0; i
< argc
; i
++) {
379 if (flags
& ZFS_ITER_ARGS_CAN_BE_PATHS
) {
380 zhp
= zfs_path_to_zhandle(g_zfs
, argv
[i
],
383 zhp
= zfs_open(g_zfs
, argv
[i
], argtype
);
386 ret
|= zfs_callback(zhp
, &cb
);
393 * At this point we've got our AVL tree full of zfs handles, so iterate
394 * over each one and execute the real user callback.
396 for (node
= uu_avl_first(cb
.cb_avl
); node
!= NULL
;
397 node
= uu_avl_next(cb
.cb_avl
, node
))
398 ret
|= callback(node
->zn_handle
, data
);
401 * Finally, clean up the AVL tree.
403 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
) {
404 (void) fprintf(stderr
,
405 gettext("internal error: out of memory"));
409 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
410 uu_avl_remove(cb
.cb_avl
, node
);
411 zfs_close(node
->zn_handle
);
415 uu_avl_walk_end(walk
);
416 uu_avl_destroy(cb
.cb_avl
);
417 uu_avl_pool_destroy(avl_pool
);