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