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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
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 uint8_t cb_props_table
[ZFS_NUM_PROPS
];
66 uu_avl_pool_t
*avl_pool
;
69 * Include snaps if they were requested or if this a zfs list where types
70 * were not specified and the "listsnapshots" property is set on this pool.
73 zfs_include_snapshots(zfs_handle_t
*zhp
, callback_data_t
*cb
)
77 if ((cb
->cb_flags
& ZFS_ITER_PROP_LISTSNAPS
) == 0)
78 return (cb
->cb_types
& ZFS_TYPE_SNAPSHOT
);
80 zph
= zfs_get_pool_handle(zhp
);
81 return (zpool_get_prop_int(zph
, ZPOOL_PROP_LISTSNAPS
, NULL
));
85 * Called for each dataset. If the object is of an appropriate type,
86 * add it to the avl tree and recurse over any children as necessary.
89 zfs_callback(zfs_handle_t
*zhp
, void *data
)
91 callback_data_t
*cb
= data
;
93 int include_snaps
= zfs_include_snapshots(zhp
, cb
);
95 if ((zfs_get_type(zhp
) & cb
->cb_types
) ||
96 ((zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
) && include_snaps
)) {
98 zfs_node_t
*node
= safe_malloc(sizeof (zfs_node_t
));
100 node
->zn_handle
= zhp
;
101 uu_avl_node_init(node
, &node
->zn_avlnode
, avl_pool
);
102 if (uu_avl_find(cb
->cb_avl
, node
, cb
->cb_sortcol
,
104 if (cb
->cb_proplist
) {
105 if ((*cb
->cb_proplist
) &&
106 !(*cb
->cb_proplist
)->pl_all
)
107 zfs_prune_proplist(zhp
,
110 if (zfs_expand_proplist(zhp
, cb
->cb_proplist
,
111 (cb
->cb_flags
& ZFS_ITER_RECVD_PROPS
))
117 uu_avl_insert(cb
->cb_avl
, node
, idx
);
125 * Recurse if necessary.
127 if (cb
->cb_flags
& ZFS_ITER_RECURSE
&&
128 ((cb
->cb_flags
& ZFS_ITER_DEPTH_LIMIT
) == 0 ||
129 cb
->cb_depth
< cb
->cb_depth_limit
)) {
131 if (zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
)
132 (void) zfs_iter_filesystems(zhp
, zfs_callback
, data
);
133 if ((zfs_get_type(zhp
) != ZFS_TYPE_SNAPSHOT
) && include_snaps
) {
134 (void) zfs_iter_snapshots(zhp
,
135 (cb
->cb_flags
& ZFS_ITER_SIMPLE
) != 0, zfs_callback
,
148 zfs_add_sort_column(zfs_sort_column_t
**sc
, const char *name
,
151 zfs_sort_column_t
*col
;
154 if ((prop
= zfs_name_to_prop(name
)) == ZPROP_INVAL
&&
155 !zfs_prop_user(name
))
158 col
= safe_malloc(sizeof (zfs_sort_column_t
));
161 col
->sc_reverse
= reverse
;
162 if (prop
== ZPROP_INVAL
) {
163 col
->sc_user_prop
= safe_malloc(strlen(name
) + 1);
164 (void) strcpy(col
->sc_user_prop
, name
);
171 (*sc
)->sc_last
->sc_next
= col
;
172 (*sc
)->sc_last
= col
;
179 zfs_free_sort_columns(zfs_sort_column_t
*sc
)
181 zfs_sort_column_t
*col
;
185 free(sc
->sc_user_prop
);
192 zfs_sort_only_by_name(const zfs_sort_column_t
*sc
)
194 return (sc
!= NULL
&& sc
->sc_next
== NULL
&&
195 sc
->sc_prop
== ZFS_PROP_NAME
);
200 zfs_compare(const void *larg
, const void *rarg
, void *unused
)
202 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
203 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
204 const char *lname
= zfs_get_name(l
);
205 const char *rname
= zfs_get_name(r
);
207 uint64_t lcreate
, rcreate
;
210 lat
= (char *)strchr(lname
, '@');
211 rat
= (char *)strchr(rname
, '@');
218 ret
= strcmp(lname
, rname
);
221 * If we're comparing a dataset to one of its snapshots, we
222 * always make the full dataset first.
226 } else if (rat
== NULL
) {
230 * If we have two snapshots from the same dataset, then
231 * we want to sort them according to creation time. We
232 * use the hidden CREATETXG property to get an absolute
233 * ordering of snapshots.
235 lcreate
= zfs_prop_get_int(l
, ZFS_PROP_CREATETXG
);
236 rcreate
= zfs_prop_get_int(r
, ZFS_PROP_CREATETXG
);
239 * Both lcreate and rcreate being 0 means we don't have
240 * properties and we should compare full name.
242 if (lcreate
== 0 && rcreate
== 0)
243 ret
= strcmp(lat
+ 1, rat
+ 1);
244 else if (lcreate
< rcreate
)
246 else if (lcreate
> rcreate
)
260 * Sort datasets by specified columns.
262 * o Numeric types sort in ascending order.
263 * o String types sort in alphabetical order.
264 * o Types inappropriate for a row sort that row to the literal
265 * bottom, regardless of the specified ordering.
267 * If no sort columns are specified, or two datasets compare equally
268 * across all specified columns, they are sorted alphabetically by name
269 * with snapshots grouped under their parents.
272 zfs_sort(const void *larg
, const void *rarg
, void *data
)
274 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
275 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
276 zfs_sort_column_t
*sc
= (zfs_sort_column_t
*)data
;
277 zfs_sort_column_t
*psc
;
279 for (psc
= sc
; psc
!= NULL
; psc
= psc
->sc_next
) {
280 char lbuf
[ZFS_MAXPROPLEN
], rbuf
[ZFS_MAXPROPLEN
];
283 boolean_t lvalid
, rvalid
;
287 * We group the checks below the generic code. If 'lstr' and
288 * 'rstr' are non-NULL, then we do a string based comparison.
289 * Otherwise, we compare 'lnum' and 'rnum'.
292 if (psc
->sc_prop
== ZPROP_INVAL
) {
293 nvlist_t
*luser
, *ruser
;
294 nvlist_t
*lval
, *rval
;
296 luser
= zfs_get_user_props(l
);
297 ruser
= zfs_get_user_props(r
);
299 lvalid
= (nvlist_lookup_nvlist(luser
,
300 psc
->sc_user_prop
, &lval
) == 0);
301 rvalid
= (nvlist_lookup_nvlist(ruser
,
302 psc
->sc_user_prop
, &rval
) == 0);
305 verify(nvlist_lookup_string(lval
,
306 ZPROP_VALUE
, &lstr
) == 0);
308 verify(nvlist_lookup_string(rval
,
309 ZPROP_VALUE
, &rstr
) == 0);
310 } else if (psc
->sc_prop
== ZFS_PROP_NAME
) {
311 lvalid
= rvalid
= B_TRUE
;
313 (void) strlcpy(lbuf
, zfs_get_name(l
), sizeof(lbuf
));
314 (void) strlcpy(rbuf
, zfs_get_name(r
), sizeof(rbuf
));
318 } else if (zfs_prop_is_string(psc
->sc_prop
)) {
319 lvalid
= (zfs_prop_get(l
, psc
->sc_prop
, lbuf
,
320 sizeof (lbuf
), NULL
, NULL
, 0, B_TRUE
) == 0);
321 rvalid
= (zfs_prop_get(r
, psc
->sc_prop
, rbuf
,
322 sizeof (rbuf
), NULL
, NULL
, 0, B_TRUE
) == 0);
327 lvalid
= zfs_prop_valid_for_type(psc
->sc_prop
,
329 rvalid
= zfs_prop_valid_for_type(psc
->sc_prop
,
333 (void) zfs_prop_get_numeric(l
, psc
->sc_prop
,
334 &lnum
, NULL
, NULL
, 0);
336 (void) zfs_prop_get_numeric(r
, psc
->sc_prop
,
337 &rnum
, NULL
, NULL
, 0);
340 if (!lvalid
&& !rvalid
)
348 ret
= strcmp(lstr
, rstr
);
349 else if (lnum
< rnum
)
351 else if (lnum
> rnum
)
355 if (psc
->sc_reverse
== B_TRUE
)
356 ret
= (ret
< 0) ? 1 : -1;
361 return (zfs_compare(larg
, rarg
, NULL
));
365 zfs_for_each(int argc
, char **argv
, int flags
, zfs_type_t types
,
366 zfs_sort_column_t
*sortcol
, zprop_list_t
**proplist
, int limit
,
367 zfs_iter_f callback
, void *data
)
369 callback_data_t cb
= {0};
374 avl_pool
= uu_avl_pool_create("zfs_pool", sizeof (zfs_node_t
),
375 offsetof(zfs_node_t
, zn_avlnode
), zfs_sort
, UU_DEFAULT
);
377 if (avl_pool
== NULL
)
380 cb
.cb_sortcol
= sortcol
;
382 cb
.cb_proplist
= proplist
;
384 cb
.cb_depth_limit
= limit
;
386 * If cb_proplist is provided then in the zfs_handles created we
387 * retain only those properties listed in cb_proplist and sortcol.
388 * The rest are pruned. So, the caller should make sure that no other
389 * properties other than those listed in cb_proplist/sortcol are
392 * If cb_proplist is NULL then we retain all the properties. We
393 * always retain the zoned property, which some other properties
394 * need (userquota & friends), and the createtxg property, which
395 * we need to sort snapshots.
397 if (cb
.cb_proplist
&& *cb
.cb_proplist
) {
398 zprop_list_t
*p
= *cb
.cb_proplist
;
401 if (p
->pl_prop
>= ZFS_PROP_TYPE
&&
402 p
->pl_prop
< ZFS_NUM_PROPS
) {
403 cb
.cb_props_table
[p
->pl_prop
] = B_TRUE
;
409 if (sortcol
->sc_prop
>= ZFS_PROP_TYPE
&&
410 sortcol
->sc_prop
< ZFS_NUM_PROPS
) {
411 cb
.cb_props_table
[sortcol
->sc_prop
] = B_TRUE
;
413 sortcol
= sortcol
->sc_next
;
416 cb
.cb_props_table
[ZFS_PROP_ZONED
] = B_TRUE
;
417 cb
.cb_props_table
[ZFS_PROP_CREATETXG
] = B_TRUE
;
419 (void) memset(cb
.cb_props_table
, B_TRUE
,
420 sizeof (cb
.cb_props_table
));
423 if ((cb
.cb_avl
= uu_avl_create(avl_pool
, NULL
, UU_DEFAULT
)) == NULL
)
428 * If given no arguments, iterate over all datasets.
430 cb
.cb_flags
|= ZFS_ITER_RECURSE
;
431 ret
= zfs_iter_root(g_zfs
, zfs_callback
, &cb
);
438 * If we're recursive, then we always allow filesystems as
439 * arguments. If we also are interested in snapshots, then we
440 * can take volumes as well.
443 if (flags
& ZFS_ITER_RECURSE
) {
444 argtype
|= ZFS_TYPE_FILESYSTEM
;
445 if (types
& ZFS_TYPE_SNAPSHOT
)
446 argtype
|= ZFS_TYPE_VOLUME
;
449 for (i
= 0; i
< argc
; i
++) {
450 if (flags
& ZFS_ITER_ARGS_CAN_BE_PATHS
) {
451 zhp
= zfs_path_to_zhandle(g_zfs
, argv
[i
],
454 zhp
= zfs_open(g_zfs
, argv
[i
], argtype
);
457 ret
|= zfs_callback(zhp
, &cb
);
464 * At this point we've got our AVL tree full of zfs handles, so iterate
465 * over each one and execute the real user callback.
467 for (node
= uu_avl_first(cb
.cb_avl
); node
!= NULL
;
468 node
= uu_avl_next(cb
.cb_avl
, node
))
469 ret
|= callback(node
->zn_handle
, data
);
472 * Finally, clean up the AVL tree.
474 if ((walk
= uu_avl_walk_start(cb
.cb_avl
, UU_WALK_ROBUST
)) == NULL
)
477 while ((node
= uu_avl_walk_next(walk
)) != NULL
) {
478 uu_avl_remove(cb
.cb_avl
, node
);
479 zfs_close(node
->zn_handle
);
483 uu_avl_walk_end(walk
);
484 uu_avl_destroy(cb
.cb_avl
);
485 uu_avl_pool_destroy(avl_pool
);