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