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