]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_iter.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / cddl / contrib / opensolaris / lib / libzfs / common / 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) 2012 by Delphix. All rights reserved.
25  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
26  * All rights reserved.
27  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <unistd.h>
34 #include <stddef.h>
35 #include <libintl.h>
36 #include <libzfs.h>
37
38 #include "libzfs_impl.h"
39
40 int
41 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
42 {
43         nvlist_t *nvl = zfs_get_clones_nvl(zhp);
44         nvpair_t *pair;
45
46         if (nvl == NULL)
47                 return (0);
48
49         for (pair = nvlist_next_nvpair(nvl, NULL); pair != NULL;
50             pair = nvlist_next_nvpair(nvl, pair)) {
51                 zfs_handle_t *clone = zfs_open(zhp->zfs_hdl, nvpair_name(pair),
52                     ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
53                 if (clone != NULL) {
54                         int err = func(clone, data);
55                         if (err != 0)
56                                 return (err);
57                 }
58         }
59         return (0);
60 }
61
62 static int
63 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
64 {
65         int rc;
66         uint64_t        orig_cookie;
67
68         orig_cookie = zc->zc_cookie;
69 top:
70         (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
71         rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
72
73         if (rc == -1) {
74                 switch (errno) {
75                 case ENOMEM:
76                         /* expand nvlist memory and try again */
77                         if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
78                                 zcmd_free_nvlists(zc);
79                                 return (-1);
80                         }
81                         zc->zc_cookie = orig_cookie;
82                         goto top;
83                 /*
84                  * An errno value of ESRCH indicates normal completion.
85                  * If ENOENT is returned, then the underlying dataset
86                  * has been removed since we obtained the handle.
87                  */
88                 case ESRCH:
89                 case ENOENT:
90                         rc = 1;
91                         break;
92                 default:
93                         rc = zfs_standard_error(zhp->zfs_hdl, errno,
94                             dgettext(TEXT_DOMAIN,
95                             "cannot iterate filesystems"));
96                         break;
97                 }
98         }
99         return (rc);
100 }
101
102 /*
103  * Iterate over all child filesystems
104  */
105 int
106 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
107 {
108         zfs_cmd_t zc = { 0 };
109         zfs_handle_t *nzhp;
110         int ret;
111
112         if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
113                 return (0);
114
115         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
116                 return (-1);
117
118         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
119             &zc)) == 0) {
120                 /*
121                  * Silently ignore errors, as the only plausible explanation is
122                  * that the pool has since been removed.
123                  */
124                 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
125                     &zc)) == NULL) {
126                         continue;
127                 }
128
129                 if ((ret = func(nzhp, data)) != 0) {
130                         zcmd_free_nvlists(&zc);
131                         return (ret);
132                 }
133         }
134         zcmd_free_nvlists(&zc);
135         return ((ret < 0) ? ret : 0);
136 }
137
138 /*
139  * Iterate over all snapshots
140  */
141 int
142 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
143     void *data)
144 {
145         zfs_cmd_t zc = { 0 };
146         zfs_handle_t *nzhp;
147         int ret;
148
149         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
150                 return (0);
151
152         zc.zc_simple = simple;
153
154         if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
155                 return (-1);
156         while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
157             &zc)) == 0) {
158
159                 if (simple)
160                         nzhp = make_dataset_simple_handle_zc(zhp, &zc);
161                 else
162                         nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
163                 if (nzhp == NULL)
164                         continue;
165
166                 if ((ret = func(nzhp, data)) != 0) {
167                         zcmd_free_nvlists(&zc);
168                         return (ret);
169                 }
170         }
171         zcmd_free_nvlists(&zc);
172         return ((ret < 0) ? ret : 0);
173 }
174
175 /*
176  * Routines for dealing with the sorted snapshot functionality
177  */
178 typedef struct zfs_node {
179         zfs_handle_t    *zn_handle;
180         avl_node_t      zn_avlnode;
181 } zfs_node_t;
182
183 static int
184 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
185 {
186         avl_tree_t *avl = data;
187         zfs_node_t *node;
188         zfs_node_t search;
189
190         search.zn_handle = zhp;
191         node = avl_find(avl, &search, NULL);
192         if (node) {
193                 /*
194                  * If this snapshot was renamed while we were creating the
195                  * AVL tree, it's possible that we already inserted it under
196                  * its old name. Remove the old handle before adding the new
197                  * one.
198                  */
199                 zfs_close(node->zn_handle);
200                 avl_remove(avl, node);
201                 free(node);
202         }
203
204         node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
205         node->zn_handle = zhp;
206         avl_add(avl, node);
207
208         return (0);
209 }
210
211 static int
212 zfs_snapshot_compare(const void *larg, const void *rarg)
213 {
214         zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
215         zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
216         uint64_t lcreate, rcreate;
217
218         /*
219          * Sort them according to creation time.  We use the hidden
220          * CREATETXG property to get an absolute ordering of snapshots.
221          */
222         lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
223         rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
224
225         if (lcreate < rcreate)
226                 return (-1);
227         else if (lcreate > rcreate)
228                 return (+1);
229         else
230                 return (0);
231 }
232
233 int
234 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
235 {
236         int ret = 0;
237         zfs_node_t *node;
238         avl_tree_t avl;
239         void *cookie = NULL;
240
241         avl_create(&avl, zfs_snapshot_compare,
242             sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
243
244         ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
245
246         for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
247                 ret |= callback(node->zn_handle, data);
248
249         while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
250                 free(node);
251
252         avl_destroy(&avl);
253
254         return (ret);
255 }
256
257 typedef struct {
258         char *ssa_first;
259         char *ssa_last;
260         boolean_t ssa_seenfirst;
261         boolean_t ssa_seenlast;
262         zfs_iter_f ssa_func;
263         void *ssa_arg;
264 } snapspec_arg_t;
265
266 static int
267 snapspec_cb(zfs_handle_t *zhp, void *arg) {
268         snapspec_arg_t *ssa = arg;
269         char *shortsnapname;
270         int err = 0;
271
272         if (ssa->ssa_seenlast)
273                 return (0);
274         shortsnapname = zfs_strdup(zhp->zfs_hdl,
275             strchr(zfs_get_name(zhp), '@') + 1);
276
277         if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
278                 ssa->ssa_seenfirst = B_TRUE;
279
280         if (ssa->ssa_seenfirst) {
281                 err = ssa->ssa_func(zhp, ssa->ssa_arg);
282         } else {
283                 zfs_close(zhp);
284         }
285
286         if (strcmp(shortsnapname, ssa->ssa_last) == 0)
287                 ssa->ssa_seenlast = B_TRUE;
288         free(shortsnapname);
289
290         return (err);
291 }
292
293 /*
294  * spec is a string like "A,B%C,D"
295  *
296  * <snaps>, where <snaps> can be:
297  *      <snap>          (single snapshot)
298  *      <snap>%<snap>   (range of snapshots, inclusive)
299  *      %<snap>         (range of snapshots, starting with earliest)
300  *      <snap>%         (range of snapshots, ending with last)
301  *      %               (all snapshots)
302  *      <snaps>[,...]   (comma separated list of the above)
303  *
304  * If a snapshot can not be opened, continue trying to open the others, but
305  * return ENOENT at the end.
306  */
307 int
308 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
309     zfs_iter_f func, void *arg)
310 {
311         char *buf, *comma_separated, *cp;
312         int err = 0;
313         int ret = 0;
314
315         buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
316         cp = buf;
317
318         while ((comma_separated = strsep(&cp, ",")) != NULL) {
319                 char *pct = strchr(comma_separated, '%');
320                 if (pct != NULL) {
321                         snapspec_arg_t ssa = { 0 };
322                         ssa.ssa_func = func;
323                         ssa.ssa_arg = arg;
324
325                         if (pct == comma_separated)
326                                 ssa.ssa_seenfirst = B_TRUE;
327                         else
328                                 ssa.ssa_first = comma_separated;
329                         *pct = '\0';
330                         ssa.ssa_last = pct + 1;
331
332                         /*
333                          * If there is a lastname specified, make sure it
334                          * exists.
335                          */
336                         if (ssa.ssa_last[0] != '\0') {
337                                 char snapname[ZFS_MAXNAMELEN];
338                                 (void) snprintf(snapname, sizeof (snapname),
339                                     "%s@%s", zfs_get_name(fs_zhp),
340                                     ssa.ssa_last);
341                                 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
342                                     snapname, ZFS_TYPE_SNAPSHOT)) {
343                                         ret = ENOENT;
344                                         continue;
345                                 }
346                         }
347
348                         err = zfs_iter_snapshots_sorted(fs_zhp,
349                             snapspec_cb, &ssa);
350                         if (ret == 0)
351                                 ret = err;
352                         if (ret == 0 && (!ssa.ssa_seenfirst ||
353                             (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
354                                 ret = ENOENT;
355                         }
356                 } else {
357                         char snapname[ZFS_MAXNAMELEN];
358                         zfs_handle_t *snap_zhp;
359                         (void) snprintf(snapname, sizeof (snapname), "%s@%s",
360                             zfs_get_name(fs_zhp), comma_separated);
361                         snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
362                             snapname);
363                         if (snap_zhp == NULL) {
364                                 ret = ENOENT;
365                                 continue;
366                         }
367                         err = func(snap_zhp, arg);
368                         if (ret == 0)
369                                 ret = err;
370                 }
371         }
372
373         free(buf);
374         return (ret);
375 }
376
377 /*
378  * Iterate over all children, snapshots and filesystems
379  */
380 int
381 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
382 {
383         int ret;
384
385         if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
386                 return (ret);
387
388         return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
389 }
390
391
392 typedef struct iter_stack_frame {
393         struct iter_stack_frame *next;
394         zfs_handle_t *zhp;
395 } iter_stack_frame_t;
396
397 typedef struct iter_dependents_arg {
398         boolean_t first;
399         boolean_t allowrecursion;
400         iter_stack_frame_t *stack;
401         zfs_iter_f func;
402         void *data;
403 } iter_dependents_arg_t;
404
405 static int
406 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
407 {
408         iter_dependents_arg_t *ida = arg;
409         int err;
410         boolean_t first = ida->first;
411         ida->first = B_FALSE;
412
413         if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
414                 err = zfs_iter_clones(zhp, iter_dependents_cb, ida);
415         } else {
416                 iter_stack_frame_t isf;
417                 iter_stack_frame_t *f;
418
419                 /*
420                  * check if there is a cycle by seeing if this fs is already
421                  * on the stack.
422                  */
423                 for (f = ida->stack; f != NULL; f = f->next) {
424                         if (f->zhp->zfs_dmustats.dds_guid ==
425                             zhp->zfs_dmustats.dds_guid) {
426                                 if (ida->allowrecursion) {
427                                         zfs_close(zhp);
428                                         return (0);
429                                 } else {
430                                         zfs_error_aux(zhp->zfs_hdl,
431                                             dgettext(TEXT_DOMAIN,
432                                             "recursive dependency at '%s'"),
433                                             zfs_get_name(zhp));
434                                         err = zfs_error(zhp->zfs_hdl,
435                                             EZFS_RECURSIVE,
436                                             dgettext(TEXT_DOMAIN,
437                                             "cannot determine dependent "
438                                             "datasets"));
439                                         zfs_close(zhp);
440                                         return (err);
441                                 }
442                         }
443                 }
444
445                 isf.zhp = zhp;
446                 isf.next = ida->stack;
447                 ida->stack = &isf;
448                 err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida);
449                 if (err == 0) {
450                         err = zfs_iter_snapshots(zhp, B_FALSE,
451                             iter_dependents_cb, ida);
452                 }
453                 ida->stack = isf.next;
454         }
455
456         if (!first && err == 0)
457                 err = ida->func(zhp, ida->data);
458         else
459                 zfs_close(zhp);
460
461         return (err);
462 }
463
464 int
465 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
466     zfs_iter_f func, void *data)
467 {
468         iter_dependents_arg_t ida;
469         ida.allowrecursion = allowrecursion;
470         ida.stack = NULL;
471         ida.func = func;
472         ida.data = data;
473         ida.first = B_TRUE;
474         return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
475 }