]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libzfs/libzfs_iter.c
Add libzutil for libzfs or libzpool consumers
[FreeBSD/FreeBSD.git] / lib / libzfs / libzfs_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) 2013, 2015 by Delphix. All rights reserved.
25  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2018 Datto Inc.
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 #include <unistd.h>
33 #include <stddef.h>
34 #include <libintl.h>
35 #include <libzfs.h>
36 #include <libzutil.h>
37 #include <sys/mntent.h>
38
39 #include "libzfs_impl.h"
40
41 int
42 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
43 {
44         nvlist_t *nvl = zfs_get_clones_nvl(zhp);
45         nvpair_t *pair;
46
47         if (nvl == NULL)
48                 return (0);
49
50         for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
51             pair = nvlist_next_nvpair(nvl, pair)) {
52                 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
53                     ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
54                 if (clone != NULL) {
55                         int err = func(clone, data);
56                         if (err != 0)
57                                 return (err);
58                 }
59         }
60         return (0);
61 }
62
63 static int
64 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
65 {
66         int rc;
67         uint64_t        orig_cookie;
68
69         orig_cookie = zc->zc_cookie;
70 top:
71         (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
72         rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
73
74         if (rc == -1) {
75                 switch (errno) {
76                 case ENOMEM:
77                         /* expand nvlist memory and try again */
78                         if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
79                                 zcmd_free_nvlists(zc);
80                                 return (-1);
81                         }
82                         zc->zc_cookie = orig_cookie;
83                         goto top;
84                 /*
85                  * An errno value of ESRCH indicates normal completion.
86                  * If ENOENT is returned, then the underlying dataset
87                  * has been removed since we obtained the handle.
88                  */
89                 case ESRCH:
90                 case ENOENT:
91                         rc = 1;
92                         break;
93                 default:
94                         rc = zfs_standard_error(zhp->zfs_hdl, errno,
95                             dgettext(TEXT_DOMAIN,
96                             "cannot iterate filesystems"));
97                         break;
98                 }
99         }
100         return (rc);
101 }
102
103 /*
104  * Iterate over all child filesystems
105  */
106 int
107 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
108 {
109         zfs_cmd_t zc = {"\0"};
110         zfs_handle_t *nzhp;
111         int ret;
112
113         if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
114                 return (0);
115
116         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
117                 return (-1);
118
119         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
120             &zc)) == 0) {
121                 /*
122                  * Silently ignore errors, as the only plausible explanation is
123                  * that the pool has since been removed.
124                  */
125                 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
126                     &zc)) == NULL) {
127                         continue;
128                 }
129
130                 if ((ret = func(nzhp, data)) != 0) {
131                         zcmd_free_nvlists(&zc);
132                         return (ret);
133                 }
134         }
135         zcmd_free_nvlists(&zc);
136         return ((ret < 0) ? ret : 0);
137 }
138
139 /*
140  * Iterate over all snapshots
141  */
142 int
143 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
144     void *data)
145 {
146         zfs_cmd_t zc = {"\0"};
147         zfs_handle_t *nzhp;
148         int ret;
149
150         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
151             zhp->zfs_type == ZFS_TYPE_BOOKMARK)
152                 return (0);
153
154         zc.zc_simple = simple;
155
156         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
157                 return (-1);
158         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
159             &zc)) == 0) {
160
161                 if (simple)
162                         nzhp = make_dataset_simple_handle_zc(zhp, &zc);
163                 else
164                         nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
165                 if (nzhp == NULL)
166                         continue;
167
168                 if ((ret = func(nzhp, data)) != 0) {
169                         zcmd_free_nvlists(&zc);
170                         return (ret);
171                 }
172         }
173         zcmd_free_nvlists(&zc);
174         return ((ret < 0) ? ret : 0);
175 }
176
177 /*
178  * Iterate over all bookmarks
179  */
180 int
181 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
182 {
183         zfs_handle_t *nzhp;
184         nvlist_t *props = NULL;
185         nvlist_t *bmarks = NULL;
186         int err;
187         nvpair_t *pair;
188
189         if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
190                 return (0);
191
192         /* Setup the requested properties nvlist. */
193         props = fnvlist_alloc();
194         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
195         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
196         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
197
198         if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
199                 goto out;
200
201         for (pair = nvlist_next_nvpair(bmarks, NULL);
202             pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
203                 char name[ZFS_MAX_DATASET_NAME_LEN];
204                 char *bmark_name;
205                 nvlist_t *bmark_props;
206
207                 bmark_name = nvpair_name(pair);
208                 bmark_props = fnvpair_value_nvlist(pair);
209
210                 if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
211                     bmark_name) >= sizeof (name)) {
212                         err = EINVAL;
213                         goto out;
214                 }
215
216                 nzhp = make_bookmark_handle(zhp, name, bmark_props);
217                 if (nzhp == NULL)
218                         continue;
219
220                 if ((err = func(nzhp, data)) != 0)
221                         goto out;
222         }
223
224 out:
225         fnvlist_free(props);
226         fnvlist_free(bmarks);
227
228         return (err);
229 }
230
231 /*
232  * Routines for dealing with the sorted snapshot functionality
233  */
234 typedef struct zfs_node {
235         zfs_handle_t    *zn_handle;
236         avl_node_t      zn_avlnode;
237 } zfs_node_t;
238
239 static int
240 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
241 {
242         avl_tree_t *avl = data;
243         zfs_node_t *node;
244         zfs_node_t search;
245
246         search.zn_handle = zhp;
247         node = avl_find(avl, &search, NULL);
248         if (node) {
249                 /*
250                  * If this snapshot was renamed while we were creating the
251                  * AVL tree, it's possible that we already inserted it under
252                  * its old name. Remove the old handle before adding the new
253                  * one.
254                  */
255                 zfs_close(node->zn_handle);
256                 avl_remove(avl, node);
257                 free(node);
258         }
259
260         node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
261         node->zn_handle = zhp;
262         avl_add(avl, node);
263
264         return (0);
265 }
266
267 static int
268 zfs_snapshot_compare(const void *larg, const void *rarg)
269 {
270         zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
271         zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
272         uint64_t lcreate, rcreate;
273
274         /*
275          * Sort them according to creation time.  We use the hidden
276          * CREATETXG property to get an absolute ordering of snapshots.
277          */
278         lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
279         rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
280
281         return (AVL_CMP(lcreate, rcreate));
282 }
283
284 int
285 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
286 {
287         int ret = 0;
288         zfs_node_t *node;
289         avl_tree_t avl;
290         void *cookie = NULL;
291
292         avl_create(&avl, zfs_snapshot_compare,
293             sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
294
295         ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
296
297         for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
298                 ret |= callback(node->zn_handle, data);
299
300         while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
301                 free(node);
302
303         avl_destroy(&avl);
304
305         return (ret);
306 }
307
308 typedef struct {
309         char *ssa_first;
310         char *ssa_last;
311         boolean_t ssa_seenfirst;
312         boolean_t ssa_seenlast;
313         zfs_iter_f ssa_func;
314         void *ssa_arg;
315 } snapspec_arg_t;
316
317 static int
318 snapspec_cb(zfs_handle_t *zhp, void *arg)
319 {
320         snapspec_arg_t *ssa = arg;
321         const char *shortsnapname;
322         int err = 0;
323
324         if (ssa->ssa_seenlast)
325                 return (0);
326
327         shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
328         if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
329                 ssa->ssa_seenfirst = B_TRUE;
330         if (strcmp(shortsnapname, ssa->ssa_last) == 0)
331                 ssa->ssa_seenlast = B_TRUE;
332
333         if (ssa->ssa_seenfirst) {
334                 err = ssa->ssa_func(zhp, ssa->ssa_arg);
335         } else {
336                 zfs_close(zhp);
337         }
338
339         return (err);
340 }
341
342 /*
343  * spec is a string like "A,B%C,D"
344  *
345  * <snaps>, where <snaps> can be:
346  *      <snap>          (single snapshot)
347  *      <snap>%<snap>   (range of snapshots, inclusive)
348  *      %<snap>         (range of snapshots, starting with earliest)
349  *      <snap>%         (range of snapshots, ending with last)
350  *      %               (all snapshots)
351  *      <snaps>[,...]   (comma separated list of the above)
352  *
353  * If a snapshot can not be opened, continue trying to open the others, but
354  * return ENOENT at the end.
355  */
356 int
357 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
358     zfs_iter_f func, void *arg)
359 {
360         char *buf, *comma_separated, *cp;
361         int err = 0;
362         int ret = 0;
363
364         buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
365         cp = buf;
366
367         while ((comma_separated = strsep(&cp, ",")) != NULL) {
368                 char *pct = strchr(comma_separated, '%');
369                 if (pct != NULL) {
370                         snapspec_arg_t ssa = { 0 };
371                         ssa.ssa_func = func;
372                         ssa.ssa_arg = arg;
373
374                         if (pct == comma_separated)
375                                 ssa.ssa_seenfirst = B_TRUE;
376                         else
377                                 ssa.ssa_first = comma_separated;
378                         *pct = '\0';
379                         ssa.ssa_last = pct + 1;
380
381                         /*
382                          * If there is a lastname specified, make sure it
383                          * exists.
384                          */
385                         if (ssa.ssa_last[0] != '\0') {
386                                 char snapname[ZFS_MAX_DATASET_NAME_LEN];
387                                 (void) snprintf(snapname, sizeof (snapname),
388                                     "%s@%s", zfs_get_name(fs_zhp),
389                                     ssa.ssa_last);
390                                 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
391                                     snapname, ZFS_TYPE_SNAPSHOT)) {
392                                         ret = ENOENT;
393                                         continue;
394                                 }
395                         }
396
397                         err = zfs_iter_snapshots_sorted(fs_zhp,
398                             snapspec_cb, &ssa);
399                         if (ret == 0)
400                                 ret = err;
401                         if (ret == 0 && (!ssa.ssa_seenfirst ||
402                             (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
403                                 ret = ENOENT;
404                         }
405                 } else {
406                         char snapname[ZFS_MAX_DATASET_NAME_LEN];
407                         zfs_handle_t *snap_zhp;
408                         (void) snprintf(snapname, sizeof (snapname), "%s@%s",
409                             zfs_get_name(fs_zhp), comma_separated);
410                         snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
411                             snapname);
412                         if (snap_zhp == NULL) {
413                                 ret = ENOENT;
414                                 continue;
415                         }
416                         err = func(snap_zhp, arg);
417                         if (ret == 0)
418                                 ret = err;
419                 }
420         }
421
422         free(buf);
423         return (ret);
424 }
425
426 /*
427  * Iterate over all children, snapshots and filesystems
428  * Process snapshots before filesystems because they are nearer the input
429  * handle: this is extremely important when used with zfs_iter_f functions
430  * looking for data, following the logic that we would like to find it as soon
431  * and as close as possible.
432  */
433 int
434 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
435 {
436         int ret;
437
438         if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
439                 return (ret);
440
441         return (zfs_iter_filesystems(zhp, func, data));
442 }
443
444
445 typedef struct iter_stack_frame {
446         struct iter_stack_frame *next;
447         zfs_handle_t *zhp;
448 } iter_stack_frame_t;
449
450 typedef struct iter_dependents_arg {
451         boolean_t first;
452         boolean_t allowrecursion;
453         iter_stack_frame_t *stack;
454         zfs_iter_f func;
455         void *data;
456 } iter_dependents_arg_t;
457
458 static int
459 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
460 {
461         iter_dependents_arg_t *ida = arg;
462         int err = 0;
463         boolean_t first = ida->first;
464         ida->first = B_FALSE;
465
466         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
467                 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
468         } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
469                 iter_stack_frame_t isf;
470                 iter_stack_frame_t *f;
471
472                 /*
473                  * check if there is a cycle by seeing if this fs is already
474                  * on the stack.
475                  */
476                 for (f = ida->stack; f != NULL; f = f->next) {
477                         if (f->zhp->zfs_dmustats.dds_guid ==
478                             zhp->zfs_dmustats.dds_guid) {
479                                 if (ida->allowrecursion) {
480                                         zfs_close(zhp);
481                                         return (0);
482                                 } else {
483                                         zfs_error_aux(zhp->zfs_hdl,
484                                             dgettext(TEXT_DOMAIN,
485                                             "recursive dependency at '%s'"),
486                                             zfs_get_name(zhp));
487                                         err = zfs_error(zhp->zfs_hdl,
488                                             EZFS_RECURSIVE,
489                                             dgettext(TEXT_DOMAIN,
490                                             "cannot determine dependent "
491                                             "datasets"));
492                                         zfs_close(zhp);
493                                         return (err);
494                                 }
495                         }
496                 }
497
498                 isf.zhp = zhp;
499                 isf.next = ida->stack;
500                 ida->stack = &isf;
501                 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
502                 if (err == 0)
503                         err = zfs_iter_snapshots(zhp, B_FALSE,
504                             iter_dependents_cb, ida);
505                 ida->stack = isf.next;
506         }
507
508         if (!first && err == 0)
509                 err = ida->func(zhp, ida->data);
510         else
511                 zfs_close(zhp);
512
513         return (err);
514 }
515
516 int
517 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
518     zfs_iter_f func, void *data)
519 {
520         iter_dependents_arg_t ida;
521         ida.allowrecursion = allowrecursion;
522         ida.stack = NULL;
523         ida.func = func;
524         ida.data = data;
525         ida.first = B_TRUE;
526         return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
527 }
528
529 /*
530  * Iterate over mounted children of the specified dataset
531  */
532 int
533 zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
534 {
535         char mnt_prop[ZFS_MAXPROPLEN];
536         struct mnttab entry;
537         zfs_handle_t *mtab_zhp;
538         size_t namelen = strlen(zhp->zfs_name);
539         FILE *mnttab;
540         int err = 0;
541
542         if ((mnttab = fopen(MNTTAB, "r")) == NULL)
543                 return (ENOENT);
544
545         while (err == 0 && getmntent(mnttab, &entry) == 0) {
546                 /* Ignore non-ZFS entries */
547                 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
548                         continue;
549
550                 /* Ignore datasets not within the provided dataset */
551                 if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 ||
552                     (entry.mnt_special[namelen] != '/' &&
553                     entry.mnt_special[namelen] != '@'))
554                         continue;
555
556                 if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special,
557                     ZFS_TYPE_FILESYSTEM)) == NULL)
558                         continue;
559
560                 /* Ignore legacy mounts as they are user managed */
561                 verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop,
562                     sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
563                 if (strcmp(mnt_prop, "legacy") == 0) {
564                         zfs_close(mtab_zhp);
565                         continue;
566                 }
567
568                 err = func(mtab_zhp, data);
569         }
570
571         fclose(mnttab);
572
573         return (err);
574 }