]>
Commit | Line | Data |
---|---|---|
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. | |
cae5b340 | 24 | * Copyright (c) 2013, 2015 by Delphix. All rights reserved. |
e10b0808 | 25 | * Copyright 2014 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 | ||
38 | int | |
39 | zfs_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 | ||
60 | static int | |
61 | zfs_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; | |
67 | top: | |
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 | */ | |
103 | int | |
104 | zfs_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 | */ | |
139 | int | |
140 | zfs_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 | ||
ea04106b AX |
147 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT || |
148 | zhp->zfs_type == ZFS_TYPE_BOOKMARK) | |
330d06f9 MA |
149 | return (0); |
150 | ||
151 | zc.zc_simple = simple; | |
152 | ||
153 | if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) | |
154 | return (-1); | |
155 | while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT, | |
156 | &zc)) == 0) { | |
157 | ||
158 | if (simple) | |
159 | nzhp = make_dataset_simple_handle_zc(zhp, &zc); | |
160 | else | |
161 | nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc); | |
162 | if (nzhp == NULL) | |
163 | continue; | |
164 | ||
165 | if ((ret = func(nzhp, data)) != 0) { | |
166 | zcmd_free_nvlists(&zc); | |
167 | return (ret); | |
168 | } | |
169 | } | |
170 | zcmd_free_nvlists(&zc); | |
171 | return ((ret < 0) ? ret : 0); | |
172 | } | |
173 | ||
ea04106b AX |
174 | /* |
175 | * Iterate over all bookmarks | |
176 | */ | |
177 | int | |
178 | zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data) | |
179 | { | |
180 | zfs_handle_t *nzhp; | |
181 | nvlist_t *props = NULL; | |
182 | nvlist_t *bmarks = NULL; | |
183 | int err; | |
184 | nvpair_t *pair; | |
185 | ||
186 | if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0) | |
187 | return (0); | |
188 | ||
189 | /* Setup the requested properties nvlist. */ | |
190 | props = fnvlist_alloc(); | |
191 | fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID)); | |
192 | fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG)); | |
193 | fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION)); | |
194 | ||
ea04106b AX |
195 | if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0) |
196 | goto out; | |
197 | ||
198 | for (pair = nvlist_next_nvpair(bmarks, NULL); | |
199 | pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) { | |
cae5b340 | 200 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
ea04106b AX |
201 | char *bmark_name; |
202 | nvlist_t *bmark_props; | |
203 | ||
204 | bmark_name = nvpair_name(pair); | |
205 | bmark_props = fnvpair_value_nvlist(pair); | |
206 | ||
cae5b340 AX |
207 | if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name, |
208 | bmark_name) >= sizeof (name)) { | |
209 | err = EINVAL; | |
210 | goto out; | |
211 | } | |
ea04106b AX |
212 | |
213 | nzhp = make_bookmark_handle(zhp, name, bmark_props); | |
214 | if (nzhp == NULL) | |
215 | continue; | |
216 | ||
217 | if ((err = func(nzhp, data)) != 0) | |
218 | goto out; | |
219 | } | |
220 | ||
221 | out: | |
222 | fnvlist_free(props); | |
223 | fnvlist_free(bmarks); | |
224 | ||
225 | return (err); | |
226 | } | |
227 | ||
330d06f9 MA |
228 | /* |
229 | * Routines for dealing with the sorted snapshot functionality | |
230 | */ | |
231 | typedef struct zfs_node { | |
232 | zfs_handle_t *zn_handle; | |
233 | avl_node_t zn_avlnode; | |
234 | } zfs_node_t; | |
235 | ||
236 | static int | |
237 | zfs_sort_snaps(zfs_handle_t *zhp, void *data) | |
238 | { | |
239 | avl_tree_t *avl = data; | |
240 | zfs_node_t *node; | |
241 | zfs_node_t search; | |
242 | ||
243 | search.zn_handle = zhp; | |
244 | node = avl_find(avl, &search, NULL); | |
245 | if (node) { | |
246 | /* | |
247 | * If this snapshot was renamed while we were creating the | |
248 | * AVL tree, it's possible that we already inserted it under | |
249 | * its old name. Remove the old handle before adding the new | |
250 | * one. | |
251 | */ | |
252 | zfs_close(node->zn_handle); | |
253 | avl_remove(avl, node); | |
254 | free(node); | |
255 | } | |
256 | ||
257 | node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t)); | |
258 | node->zn_handle = zhp; | |
259 | avl_add(avl, node); | |
260 | ||
261 | return (0); | |
262 | } | |
263 | ||
264 | static int | |
265 | zfs_snapshot_compare(const void *larg, const void *rarg) | |
266 | { | |
267 | zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle; | |
268 | zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle; | |
269 | uint64_t lcreate, rcreate; | |
270 | ||
271 | /* | |
272 | * Sort them according to creation time. We use the hidden | |
273 | * CREATETXG property to get an absolute ordering of snapshots. | |
274 | */ | |
275 | lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG); | |
276 | rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG); | |
277 | ||
cae5b340 | 278 | return (AVL_CMP(lcreate, rcreate)); |
330d06f9 MA |
279 | } |
280 | ||
281 | int | |
282 | zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data) | |
283 | { | |
284 | int ret = 0; | |
285 | zfs_node_t *node; | |
286 | avl_tree_t avl; | |
287 | void *cookie = NULL; | |
288 | ||
289 | avl_create(&avl, zfs_snapshot_compare, | |
290 | sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode)); | |
291 | ||
292 | ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl); | |
293 | ||
294 | for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node)) | |
295 | ret |= callback(node->zn_handle, data); | |
296 | ||
297 | while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL) | |
298 | free(node); | |
299 | ||
300 | avl_destroy(&avl); | |
301 | ||
302 | return (ret); | |
303 | } | |
304 | ||
305 | typedef struct { | |
306 | char *ssa_first; | |
307 | char *ssa_last; | |
308 | boolean_t ssa_seenfirst; | |
309 | boolean_t ssa_seenlast; | |
310 | zfs_iter_f ssa_func; | |
311 | void *ssa_arg; | |
312 | } snapspec_arg_t; | |
313 | ||
314 | static int | |
cae5b340 AX |
315 | snapspec_cb(zfs_handle_t *zhp, void *arg) |
316 | { | |
330d06f9 | 317 | snapspec_arg_t *ssa = arg; |
cae5b340 | 318 | const char *shortsnapname; |
330d06f9 MA |
319 | int err = 0; |
320 | ||
321 | if (ssa->ssa_seenlast) | |
322 | return (0); | |
330d06f9 | 323 | |
cae5b340 | 324 | shortsnapname = strchr(zfs_get_name(zhp), '@') + 1; |
330d06f9 MA |
325 | if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0) |
326 | ssa->ssa_seenfirst = B_TRUE; | |
cae5b340 AX |
327 | if (strcmp(shortsnapname, ssa->ssa_last) == 0) |
328 | ssa->ssa_seenlast = B_TRUE; | |
330d06f9 MA |
329 | |
330 | if (ssa->ssa_seenfirst) { | |
331 | err = ssa->ssa_func(zhp, ssa->ssa_arg); | |
332 | } else { | |
333 | zfs_close(zhp); | |
334 | } | |
335 | ||
330d06f9 MA |
336 | return (err); |
337 | } | |
338 | ||
339 | /* | |
340 | * spec is a string like "A,B%C,D" | |
341 | * | |
342 | * <snaps>, where <snaps> can be: | |
343 | * <snap> (single snapshot) | |
344 | * <snap>%<snap> (range of snapshots, inclusive) | |
345 | * %<snap> (range of snapshots, starting with earliest) | |
346 | * <snap>% (range of snapshots, ending with last) | |
347 | * % (all snapshots) | |
348 | * <snaps>[,...] (comma separated list of the above) | |
349 | * | |
350 | * If a snapshot can not be opened, continue trying to open the others, but | |
351 | * return ENOENT at the end. | |
352 | */ | |
353 | int | |
354 | zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig, | |
355 | zfs_iter_f func, void *arg) | |
356 | { | |
a08ee875 | 357 | char *buf, *comma_separated, *cp; |
330d06f9 MA |
358 | int err = 0; |
359 | int ret = 0; | |
360 | ||
a08ee875 | 361 | buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig); |
330d06f9 MA |
362 | cp = buf; |
363 | ||
364 | while ((comma_separated = strsep(&cp, ",")) != NULL) { | |
365 | char *pct = strchr(comma_separated, '%'); | |
366 | if (pct != NULL) { | |
367 | snapspec_arg_t ssa = { 0 }; | |
368 | ssa.ssa_func = func; | |
369 | ssa.ssa_arg = arg; | |
370 | ||
371 | if (pct == comma_separated) | |
372 | ssa.ssa_seenfirst = B_TRUE; | |
373 | else | |
374 | ssa.ssa_first = comma_separated; | |
375 | *pct = '\0'; | |
376 | ssa.ssa_last = pct + 1; | |
377 | ||
378 | /* | |
379 | * If there is a lastname specified, make sure it | |
380 | * exists. | |
381 | */ | |
382 | if (ssa.ssa_last[0] != '\0') { | |
cae5b340 | 383 | char snapname[ZFS_MAX_DATASET_NAME_LEN]; |
330d06f9 MA |
384 | (void) snprintf(snapname, sizeof (snapname), |
385 | "%s@%s", zfs_get_name(fs_zhp), | |
386 | ssa.ssa_last); | |
387 | if (!zfs_dataset_exists(fs_zhp->zfs_hdl, | |
388 | snapname, ZFS_TYPE_SNAPSHOT)) { | |
389 | ret = ENOENT; | |
390 | continue; | |
391 | } | |
392 | } | |
393 | ||
394 | err = zfs_iter_snapshots_sorted(fs_zhp, | |
395 | snapspec_cb, &ssa); | |
396 | if (ret == 0) | |
397 | ret = err; | |
398 | if (ret == 0 && (!ssa.ssa_seenfirst || | |
399 | (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) { | |
400 | ret = ENOENT; | |
401 | } | |
402 | } else { | |
cae5b340 | 403 | char snapname[ZFS_MAX_DATASET_NAME_LEN]; |
330d06f9 MA |
404 | zfs_handle_t *snap_zhp; |
405 | (void) snprintf(snapname, sizeof (snapname), "%s@%s", | |
406 | zfs_get_name(fs_zhp), comma_separated); | |
407 | snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl, | |
408 | snapname); | |
409 | if (snap_zhp == NULL) { | |
410 | ret = ENOENT; | |
411 | continue; | |
412 | } | |
413 | err = func(snap_zhp, arg); | |
414 | if (ret == 0) | |
415 | ret = err; | |
416 | } | |
417 | } | |
418 | ||
a08ee875 | 419 | free(buf); |
330d06f9 MA |
420 | return (ret); |
421 | } | |
422 | ||
423 | /* | |
424 | * Iterate over all children, snapshots and filesystems | |
425 | */ | |
426 | int | |
427 | zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) | |
428 | { | |
429 | int ret; | |
430 | ||
8ec27e97 | 431 | if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0) |
330d06f9 MA |
432 | return (ret); |
433 | ||
8ec27e97 | 434 | return (zfs_iter_filesystems(zhp, func, data)); |
330d06f9 MA |
435 | } |
436 | ||
437 | ||
438 | typedef struct iter_stack_frame { | |
439 | struct iter_stack_frame *next; | |
440 | zfs_handle_t *zhp; | |
441 | } iter_stack_frame_t; | |
442 | ||
443 | typedef struct iter_dependents_arg { | |
444 | boolean_t first; | |
445 | boolean_t allowrecursion; | |
446 | iter_stack_frame_t *stack; | |
447 | zfs_iter_f func; | |
448 | void *data; | |
449 | } iter_dependents_arg_t; | |
450 | ||
451 | static int | |
452 | iter_dependents_cb(zfs_handle_t *zhp, void *arg) | |
453 | { | |
454 | iter_dependents_arg_t *ida = arg; | |
ea04106b | 455 | int err = 0; |
330d06f9 MA |
456 | boolean_t first = ida->first; |
457 | ida->first = B_FALSE; | |
458 | ||
459 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { | |
460 | err = zfs_iter_clones(zhp, iter_dependents_cb, ida); | |
ea04106b | 461 | } else if (zhp->zfs_type != ZFS_TYPE_BOOKMARK) { |
330d06f9 MA |
462 | iter_stack_frame_t isf; |
463 | iter_stack_frame_t *f; | |
464 | ||
465 | /* | |
466 | * check if there is a cycle by seeing if this fs is already | |
467 | * on the stack. | |
468 | */ | |
469 | for (f = ida->stack; f != NULL; f = f->next) { | |
470 | if (f->zhp->zfs_dmustats.dds_guid == | |
471 | zhp->zfs_dmustats.dds_guid) { | |
472 | if (ida->allowrecursion) { | |
473 | zfs_close(zhp); | |
474 | return (0); | |
475 | } else { | |
476 | zfs_error_aux(zhp->zfs_hdl, | |
477 | dgettext(TEXT_DOMAIN, | |
478 | "recursive dependency at '%s'"), | |
479 | zfs_get_name(zhp)); | |
480 | err = zfs_error(zhp->zfs_hdl, | |
481 | EZFS_RECURSIVE, | |
482 | dgettext(TEXT_DOMAIN, | |
483 | "cannot determine dependent " | |
484 | "datasets")); | |
485 | zfs_close(zhp); | |
486 | return (err); | |
487 | } | |
488 | } | |
489 | } | |
490 | ||
491 | isf.zhp = zhp; | |
492 | isf.next = ida->stack; | |
493 | ida->stack = &isf; | |
494 | err = zfs_iter_filesystems(zhp, iter_dependents_cb, ida); | |
495 | if (err == 0) | |
496 | err = zfs_iter_snapshots(zhp, B_FALSE, | |
497 | iter_dependents_cb, ida); | |
498 | ida->stack = isf.next; | |
499 | } | |
a08ee875 | 500 | |
330d06f9 MA |
501 | if (!first && err == 0) |
502 | err = ida->func(zhp, ida->data); | |
a08ee875 LG |
503 | else |
504 | zfs_close(zhp); | |
505 | ||
330d06f9 MA |
506 | return (err); |
507 | } | |
508 | ||
509 | int | |
510 | zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion, | |
511 | zfs_iter_f func, void *data) | |
512 | { | |
513 | iter_dependents_arg_t ida; | |
514 | ida.allowrecursion = allowrecursion; | |
515 | ida.stack = NULL; | |
516 | ida.func = func; | |
517 | ida.data = data; | |
518 | ida.first = B_TRUE; | |
519 | return (iter_dependents_cb(zfs_handle_dup(zhp), &ida)); | |
520 | } |