]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libzfs/libzfs_iter.c
Avoid retrieving unused snapshot props
[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) 2019 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, uint64_t min_txg, uint64_t max_txg)
145 {
146         zfs_cmd_t zc = {"\0"};
147         zfs_handle_t *nzhp;
148         int ret;
149         nvlist_t *range_nvl = NULL;
150
151         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
152             zhp->zfs_type == ZFS_TYPE_BOOKMARK)
153                 return (0);
154
155         zc.zc_simple = simple;
156
157         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
158                 return (-1);
159
160         if (min_txg != 0) {
161                 range_nvl = fnvlist_alloc();
162                 fnvlist_add_uint64(range_nvl, SNAP_ITER_MIN_TXG, min_txg);
163         }
164         if (max_txg != 0) {
165                 if (range_nvl == NULL)
166                         range_nvl = fnvlist_alloc();
167                 fnvlist_add_uint64(range_nvl, SNAP_ITER_MAX_TXG, max_txg);
168         }
169
170         if (range_nvl != NULL &&
171             zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, range_nvl) != 0) {
172                 zcmd_free_nvlists(&zc);
173                 fnvlist_free(range_nvl);
174                 return (-1);
175         }
176
177         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
178             &zc)) == 0) {
179
180                 if (simple)
181                         nzhp = make_dataset_simple_handle_zc(zhp, &zc);
182                 else
183                         nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
184                 if (nzhp == NULL)
185                         continue;
186
187                 if ((ret = func(nzhp, data)) != 0) {
188                         zcmd_free_nvlists(&zc);
189                         fnvlist_free(range_nvl);
190                         return (ret);
191                 }
192         }
193         zcmd_free_nvlists(&zc);
194         fnvlist_free(range_nvl);
195         return ((ret < 0) ? ret : 0);
196 }
197
198 /*
199  * Iterate over all bookmarks
200  */
201 int
202 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
203 {
204         zfs_handle_t *nzhp;
205         nvlist_t *props = NULL;
206         nvlist_t *bmarks = NULL;
207         int err;
208         nvpair_t *pair;
209
210         if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
211                 return (0);
212
213         /* Setup the requested properties nvlist. */
214         props = fnvlist_alloc();
215         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
216         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
217         fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
218
219         if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
220                 goto out;
221
222         for (pair = nvlist_next_nvpair(bmarks, NULL);
223             pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
224                 char name[ZFS_MAX_DATASET_NAME_LEN];
225                 char *bmark_name;
226                 nvlist_t *bmark_props;
227
228                 bmark_name = nvpair_name(pair);
229                 bmark_props = fnvpair_value_nvlist(pair);
230
231                 if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
232                     bmark_name) >= sizeof (name)) {
233                         err = EINVAL;
234                         goto out;
235                 }
236
237                 nzhp = make_bookmark_handle(zhp, name, bmark_props);
238                 if (nzhp == NULL)
239                         continue;
240
241                 if ((err = func(nzhp, data)) != 0)
242                         goto out;
243         }
244
245 out:
246         fnvlist_free(props);
247         fnvlist_free(bmarks);
248
249         return (err);
250 }
251
252 /*
253  * Routines for dealing with the sorted snapshot functionality
254  */
255 typedef struct zfs_node {
256         zfs_handle_t    *zn_handle;
257         avl_node_t      zn_avlnode;
258 } zfs_node_t;
259
260 static int
261 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
262 {
263         avl_tree_t *avl = data;
264         zfs_node_t *node;
265         zfs_node_t search;
266
267         search.zn_handle = zhp;
268         node = avl_find(avl, &search, NULL);
269         if (node) {
270                 /*
271                  * If this snapshot was renamed while we were creating the
272                  * AVL tree, it's possible that we already inserted it under
273                  * its old name. Remove the old handle before adding the new
274                  * one.
275                  */
276                 zfs_close(node->zn_handle);
277                 avl_remove(avl, node);
278                 free(node);
279         }
280
281         node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
282         node->zn_handle = zhp;
283         avl_add(avl, node);
284
285         return (0);
286 }
287
288 static int
289 zfs_snapshot_compare(const void *larg, const void *rarg)
290 {
291         zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
292         zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
293         uint64_t lcreate, rcreate;
294
295         /*
296          * Sort them according to creation time.  We use the hidden
297          * CREATETXG property to get an absolute ordering of snapshots.
298          */
299         lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
300         rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
301
302         return (AVL_CMP(lcreate, rcreate));
303 }
304
305 int
306 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data,
307     uint64_t min_txg, uint64_t max_txg)
308 {
309         int ret = 0;
310         zfs_node_t *node;
311         avl_tree_t avl;
312         void *cookie = NULL;
313
314         avl_create(&avl, zfs_snapshot_compare,
315             sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
316
317         ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl, min_txg,
318             max_txg);
319
320         for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
321                 ret |= callback(node->zn_handle, data);
322
323         while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
324                 free(node);
325
326         avl_destroy(&avl);
327
328         return (ret);
329 }
330
331 typedef struct {
332         char *ssa_first;
333         char *ssa_last;
334         boolean_t ssa_seenfirst;
335         boolean_t ssa_seenlast;
336         zfs_iter_f ssa_func;
337         void *ssa_arg;
338 } snapspec_arg_t;
339
340 static int
341 snapspec_cb(zfs_handle_t *zhp, void *arg)
342 {
343         snapspec_arg_t *ssa = arg;
344         const char *shortsnapname;
345         int err = 0;
346
347         if (ssa->ssa_seenlast)
348                 return (0);
349
350         shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
351         if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
352                 ssa->ssa_seenfirst = B_TRUE;
353         if (strcmp(shortsnapname, ssa->ssa_last) == 0)
354                 ssa->ssa_seenlast = B_TRUE;
355
356         if (ssa->ssa_seenfirst) {
357                 err = ssa->ssa_func(zhp, ssa->ssa_arg);
358         } else {
359                 zfs_close(zhp);
360         }
361
362         return (err);
363 }
364
365 /*
366  * spec is a string like "A,B%C,D"
367  *
368  * <snaps>, where <snaps> can be:
369  *      <snap>          (single snapshot)
370  *      <snap>%<snap>   (range of snapshots, inclusive)
371  *      %<snap>         (range of snapshots, starting with earliest)
372  *      <snap>%         (range of snapshots, ending with last)
373  *      %               (all snapshots)
374  *      <snaps>[,...]   (comma separated list of the above)
375  *
376  * If a snapshot can not be opened, continue trying to open the others, but
377  * return ENOENT at the end.
378  */
379 int
380 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
381     zfs_iter_f func, void *arg)
382 {
383         char *buf, *comma_separated, *cp;
384         int err = 0;
385         int ret = 0;
386
387         buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
388         cp = buf;
389
390         while ((comma_separated = strsep(&cp, ",")) != NULL) {
391                 char *pct = strchr(comma_separated, '%');
392                 if (pct != NULL) {
393                         snapspec_arg_t ssa = { 0 };
394                         ssa.ssa_func = func;
395                         ssa.ssa_arg = arg;
396
397                         if (pct == comma_separated)
398                                 ssa.ssa_seenfirst = B_TRUE;
399                         else
400                                 ssa.ssa_first = comma_separated;
401                         *pct = '\0';
402                         ssa.ssa_last = pct + 1;
403
404                         /*
405                          * If there is a lastname specified, make sure it
406                          * exists.
407                          */
408                         if (ssa.ssa_last[0] != '\0') {
409                                 char snapname[ZFS_MAX_DATASET_NAME_LEN];
410                                 (void) snprintf(snapname, sizeof (snapname),
411                                     "%s@%s", zfs_get_name(fs_zhp),
412                                     ssa.ssa_last);
413                                 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
414                                     snapname, ZFS_TYPE_SNAPSHOT)) {
415                                         ret = ENOENT;
416                                         continue;
417                                 }
418                         }
419
420                         err = zfs_iter_snapshots_sorted(fs_zhp,
421                             snapspec_cb, &ssa, 0, 0);
422                         if (ret == 0)
423                                 ret = err;
424                         if (ret == 0 && (!ssa.ssa_seenfirst ||
425                             (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
426                                 ret = ENOENT;
427                         }
428                 } else {
429                         char snapname[ZFS_MAX_DATASET_NAME_LEN];
430                         zfs_handle_t *snap_zhp;
431                         (void) snprintf(snapname, sizeof (snapname), "%s@%s",
432                             zfs_get_name(fs_zhp), comma_separated);
433                         snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
434                             snapname);
435                         if (snap_zhp == NULL) {
436                                 ret = ENOENT;
437                                 continue;
438                         }
439                         err = func(snap_zhp, arg);
440                         if (ret == 0)
441                                 ret = err;
442                 }
443         }
444
445         free(buf);
446         return (ret);
447 }
448
449 /*
450  * Iterate over all children, snapshots and filesystems
451  * Process snapshots before filesystems because they are nearer the input
452  * handle: this is extremely important when used with zfs_iter_f functions
453  * looking for data, following the logic that we would like to find it as soon
454  * and as close as possible.
455  */
456 int
457 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
458 {
459         int ret;
460
461         if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data, 0, 0)) != 0)
462                 return (ret);
463
464         return (zfs_iter_filesystems(zhp, func, data));
465 }
466
467
468 typedef struct iter_stack_frame {
469         struct iter_stack_frame *next;
470         zfs_handle_t *zhp;
471 } iter_stack_frame_t;
472
473 typedef struct iter_dependents_arg {
474         boolean_t first;
475         boolean_t allowrecursion;
476         iter_stack_frame_t *stack;
477         zfs_iter_f func;
478         void *data;
479 } iter_dependents_arg_t;
480
481 static int
482 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
483 {
484         iter_dependents_arg_t *ida = arg;
485         int err = 0;
486         boolean_t first = ida->first;
487         ida->first = B_FALSE;
488
489         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
490                 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
491         } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) {
492                 iter_stack_frame_t isf;
493                 iter_stack_frame_t *f;
494
495                 /*
496                  * check if there is a cycle by seeing if this fs is already
497                  * on the stack.
498                  */
499                 for (f = ida->stack; f != NULL; f = f->next) {
500                         if (f->zhp->zfs_dmustats.dds_guid ==
501                             zhp->zfs_dmustats.dds_guid) {
502                                 if (ida->allowrecursion) {
503                                         zfs_close(zhp);
504                                         return (0);
505                                 } else {
506                                         zfs_error_aux(zhp->zfs_hdl,
507                                             dgettext(TEXT_DOMAIN,
508                                             "recursive dependency at '%s'"),
509                                             zfs_get_name(zhp));
510                                         err = zfs_error(zhp->zfs_hdl,
511                                             EZFS_RECURSIVE,
512                                             dgettext(TEXT_DOMAIN,
513                                             "cannot determine dependent "
514                                             "datasets"));
515                                         zfs_close(zhp);
516                                         return (err);
517                                 }
518                         }
519                 }
520
521                 isf.zhp = zhp;
522                 isf.next = ida->stack;
523                 ida->stack = &isf;
524                 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
525                 if (err == 0)
526                         err = zfs_iter_snapshots(zhp, B_FALSE,
527                             iter_dependents_cb, ida, 0, 0);
528                 ida->stack = isf.next;
529         }
530
531         if (!first && err == 0)
532                 err = ida->func(zhp, ida->data);
533         else
534                 zfs_close(zhp);
535
536         return (err);
537 }
538
539 int
540 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
541     zfs_iter_f func, void *data)
542 {
543         iter_dependents_arg_t ida;
544         ida.allowrecursion = allowrecursion;
545         ida.stack = NULL;
546         ida.func = func;
547         ida.data = data;
548         ida.first = B_TRUE;
549         return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
550 }
551
552 /*
553  * Iterate over mounted children of the specified dataset
554  */
555 int
556 zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
557 {
558         char mnt_prop[ZFS_MAXPROPLEN];
559         struct mnttab entry;
560         zfs_handle_t *mtab_zhp;
561         size_t namelen = strlen(zhp->zfs_name);
562         FILE *mnttab;
563         int err = 0;
564
565         if ((mnttab = fopen(MNTTAB, "r")) == NULL)
566                 return (ENOENT);
567
568         while (err == 0 && getmntent(mnttab, &entry) == 0) {
569                 /* Ignore non-ZFS entries */
570                 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
571                         continue;
572
573                 /* Ignore datasets not within the provided dataset */
574                 if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 ||
575                     (entry.mnt_special[namelen] != '/' &&
576                     entry.mnt_special[namelen] != '@'))
577                         continue;
578
579                 if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special,
580                     ZFS_TYPE_FILESYSTEM)) == NULL)
581                         continue;
582
583                 /* Ignore legacy mounts as they are user managed */
584                 verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop,
585                     sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
586                 if (strcmp(mnt_prop, "legacy") == 0) {
587                         zfs_close(mtab_zhp);
588                         continue;
589                 }
590
591                 err = func(mtab_zhp, data);
592         }
593
594         fclose(mnttab);
595
596         return (err);
597 }