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