Print "(repairing)" in zpool status again
[zfs.git] / lib / libzfs / libzfs_iter.c
blob73dc2c793f5ea021dc3941c31a687b60eb034fb6
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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) 2018 Datto Inc.
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 <sys/mntent.h>
38 #include "libzfs_impl.h"
40 int
41 zfs_iter_clones(zfs_handle_t *zhp, zfs_iter_f func, void *data)
43 nvlist_t *nvl = zfs_get_clones_nvl(zhp);
44 nvpair_t *pair;
46 if (nvl == NULL)
47 return (0);
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);
59 return (0);
62 static int
63 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
65 int rc;
66 uint64_t orig_cookie;
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);
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);
81 zc->zc_cookie = orig_cookie;
82 goto top;
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.
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;
99 return (rc);
103 * Iterate over all child filesystems
106 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
108 zfs_cmd_t zc = {"\0"};
109 zfs_handle_t *nzhp;
110 int ret;
112 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
113 return (0);
115 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
116 return (-1);
118 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
119 &zc)) == 0) {
121 * Silently ignore errors, as the only plausible explanation is
122 * that the pool has since been removed.
124 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
125 &zc)) == NULL) {
126 continue;
129 if ((ret = func(nzhp, data)) != 0) {
130 zcmd_free_nvlists(&zc);
131 return (ret);
134 zcmd_free_nvlists(&zc);
135 return ((ret < 0) ? ret : 0);
139 * Iterate over all snapshots
142 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
143 void *data)
145 zfs_cmd_t zc = {"\0"};
146 zfs_handle_t *nzhp;
147 int ret;
149 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT ||
150 zhp->zfs_type == ZFS_TYPE_BOOKMARK)
151 return (0);
153 zc.zc_simple = simple;
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) {
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;
167 if ((ret = func(nzhp, data)) != 0) {
168 zcmd_free_nvlists(&zc);
169 return (ret);
172 zcmd_free_nvlists(&zc);
173 return ((ret < 0) ? ret : 0);
177 * Iterate over all bookmarks
180 zfs_iter_bookmarks(zfs_handle_t *zhp, zfs_iter_f func, void *data)
182 zfs_handle_t *nzhp;
183 nvlist_t *props = NULL;
184 nvlist_t *bmarks = NULL;
185 int err;
186 nvpair_t *pair;
188 if ((zfs_get_type(zhp) & (ZFS_TYPE_SNAPSHOT | ZFS_TYPE_BOOKMARK)) != 0)
189 return (0);
191 /* Setup the requested properties nvlist. */
192 props = fnvlist_alloc();
193 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_GUID));
194 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATETXG));
195 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_CREATION));
197 if ((err = lzc_get_bookmarks(zhp->zfs_name, props, &bmarks)) != 0)
198 goto out;
200 for (pair = nvlist_next_nvpair(bmarks, NULL);
201 pair != NULL; pair = nvlist_next_nvpair(bmarks, pair)) {
202 char name[ZFS_MAX_DATASET_NAME_LEN];
203 char *bmark_name;
204 nvlist_t *bmark_props;
206 bmark_name = nvpair_name(pair);
207 bmark_props = fnvpair_value_nvlist(pair);
209 if (snprintf(name, sizeof (name), "%s#%s", zhp->zfs_name,
210 bmark_name) >= sizeof (name)) {
211 err = EINVAL;
212 goto out;
215 nzhp = make_bookmark_handle(zhp, name, bmark_props);
216 if (nzhp == NULL)
217 continue;
219 if ((err = func(nzhp, data)) != 0)
220 goto out;
223 out:
224 fnvlist_free(props);
225 fnvlist_free(bmarks);
227 return (err);
231 * Routines for dealing with the sorted snapshot functionality
233 typedef struct zfs_node {
234 zfs_handle_t *zn_handle;
235 avl_node_t zn_avlnode;
236 } zfs_node_t;
238 static int
239 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
241 avl_tree_t *avl = data;
242 zfs_node_t *node;
243 zfs_node_t search;
245 search.zn_handle = zhp;
246 node = avl_find(avl, &search, NULL);
247 if (node) {
249 * If this snapshot was renamed while we were creating the
250 * AVL tree, it's possible that we already inserted it under
251 * its old name. Remove the old handle before adding the new
252 * one.
254 zfs_close(node->zn_handle);
255 avl_remove(avl, node);
256 free(node);
259 node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
260 node->zn_handle = zhp;
261 avl_add(avl, node);
263 return (0);
266 static int
267 zfs_snapshot_compare(const void *larg, const void *rarg)
269 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
270 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
271 uint64_t lcreate, rcreate;
274 * Sort them according to creation time. We use the hidden
275 * CREATETXG property to get an absolute ordering of snapshots.
277 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
278 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
280 return (AVL_CMP(lcreate, rcreate));
284 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
286 int ret = 0;
287 zfs_node_t *node;
288 avl_tree_t avl;
289 void *cookie = NULL;
291 avl_create(&avl, zfs_snapshot_compare,
292 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
294 ret = zfs_iter_snapshots(zhp, B_FALSE, zfs_sort_snaps, &avl);
296 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
297 ret |= callback(node->zn_handle, data);
299 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
300 free(node);
302 avl_destroy(&avl);
304 return (ret);
307 typedef struct {
308 char *ssa_first;
309 char *ssa_last;
310 boolean_t ssa_seenfirst;
311 boolean_t ssa_seenlast;
312 zfs_iter_f ssa_func;
313 void *ssa_arg;
314 } snapspec_arg_t;
316 static int
317 snapspec_cb(zfs_handle_t *zhp, void *arg)
319 snapspec_arg_t *ssa = arg;
320 const char *shortsnapname;
321 int err = 0;
323 if (ssa->ssa_seenlast)
324 return (0);
326 shortsnapname = strchr(zfs_get_name(zhp), '@') + 1;
327 if (!ssa->ssa_seenfirst && strcmp(shortsnapname, ssa->ssa_first) == 0)
328 ssa->ssa_seenfirst = B_TRUE;
329 if (strcmp(shortsnapname, ssa->ssa_last) == 0)
330 ssa->ssa_seenlast = B_TRUE;
332 if (ssa->ssa_seenfirst) {
333 err = ssa->ssa_func(zhp, ssa->ssa_arg);
334 } else {
335 zfs_close(zhp);
338 return (err);
342 * spec is a string like "A,B%C,D"
344 * <snaps>, where <snaps> can be:
345 * <snap> (single snapshot)
346 * <snap>%<snap> (range of snapshots, inclusive)
347 * %<snap> (range of snapshots, starting with earliest)
348 * <snap>% (range of snapshots, ending with last)
349 * % (all snapshots)
350 * <snaps>[,...] (comma separated list of the above)
352 * If a snapshot can not be opened, continue trying to open the others, but
353 * return ENOENT at the end.
356 zfs_iter_snapspec(zfs_handle_t *fs_zhp, const char *spec_orig,
357 zfs_iter_f func, void *arg)
359 char *buf, *comma_separated, *cp;
360 int err = 0;
361 int ret = 0;
363 buf = zfs_strdup(fs_zhp->zfs_hdl, spec_orig);
364 cp = buf;
366 while ((comma_separated = strsep(&cp, ",")) != NULL) {
367 char *pct = strchr(comma_separated, '%');
368 if (pct != NULL) {
369 snapspec_arg_t ssa = { 0 };
370 ssa.ssa_func = func;
371 ssa.ssa_arg = arg;
373 if (pct == comma_separated)
374 ssa.ssa_seenfirst = B_TRUE;
375 else
376 ssa.ssa_first = comma_separated;
377 *pct = '\0';
378 ssa.ssa_last = pct + 1;
381 * If there is a lastname specified, make sure it
382 * exists.
384 if (ssa.ssa_last[0] != '\0') {
385 char snapname[ZFS_MAX_DATASET_NAME_LEN];
386 (void) snprintf(snapname, sizeof (snapname),
387 "%s@%s", zfs_get_name(fs_zhp),
388 ssa.ssa_last);
389 if (!zfs_dataset_exists(fs_zhp->zfs_hdl,
390 snapname, ZFS_TYPE_SNAPSHOT)) {
391 ret = ENOENT;
392 continue;
396 err = zfs_iter_snapshots_sorted(fs_zhp,
397 snapspec_cb, &ssa);
398 if (ret == 0)
399 ret = err;
400 if (ret == 0 && (!ssa.ssa_seenfirst ||
401 (ssa.ssa_last[0] != '\0' && !ssa.ssa_seenlast))) {
402 ret = ENOENT;
404 } else {
405 char snapname[ZFS_MAX_DATASET_NAME_LEN];
406 zfs_handle_t *snap_zhp;
407 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
408 zfs_get_name(fs_zhp), comma_separated);
409 snap_zhp = make_dataset_handle(fs_zhp->zfs_hdl,
410 snapname);
411 if (snap_zhp == NULL) {
412 ret = ENOENT;
413 continue;
415 err = func(snap_zhp, arg);
416 if (ret == 0)
417 ret = err;
421 free(buf);
422 return (ret);
426 * Iterate over all children, snapshots and filesystems
427 * Process snapshots before filesystems because they are nearer the input
428 * handle: this is extremely important when used with zfs_iter_f functions
429 * looking for data, following the logic that we would like to find it as soon
430 * and as close as possible.
433 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
435 int ret;
437 if ((ret = zfs_iter_snapshots(zhp, B_FALSE, func, data)) != 0)
438 return (ret);
440 return (zfs_iter_filesystems(zhp, func, data));
444 typedef struct iter_stack_frame {
445 struct iter_stack_frame *next;
446 zfs_handle_t *zhp;
447 } iter_stack_frame_t;
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;
457 static int
458 iter_dependents_cb(zfs_handle_t *zhp, void *arg)
460 iter_dependents_arg_t *ida = arg;
461 int err = 0;
462 boolean_t first = ida->first;
463 ida->first = B_FALSE;
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;
472 * check if there is a cycle by seeing if this fs is already
473 * on the stack.
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);
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 ida->stack = isf.next;
507 if (!first && err == 0)
508 err = ida->func(zhp, ida->data);
509 else
510 zfs_close(zhp);
512 return (err);
516 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
517 zfs_iter_f func, void *data)
519 iter_dependents_arg_t ida;
520 ida.allowrecursion = allowrecursion;
521 ida.stack = NULL;
522 ida.func = func;
523 ida.data = data;
524 ida.first = B_TRUE;
525 return (iter_dependents_cb(zfs_handle_dup(zhp), &ida));
529 * Iterate over mounted children of the specified dataset
532 zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
534 char mnt_prop[ZFS_MAXPROPLEN];
535 struct mnttab entry;
536 zfs_handle_t *mtab_zhp;
537 size_t namelen = strlen(zhp->zfs_name);
538 FILE *mnttab;
539 int err = 0;
541 if ((mnttab = fopen(MNTTAB, "r")) == NULL)
542 return (ENOENT);
544 while (err == 0 && getmntent(mnttab, &entry) == 0) {
545 /* Ignore non-ZFS entries */
546 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
547 continue;
549 /* Ignore datasets not within the provided dataset */
550 if (strncmp(entry.mnt_special, zhp->zfs_name, namelen) != 0 ||
551 (entry.mnt_special[namelen] != '/' &&
552 entry.mnt_special[namelen] != '@'))
553 continue;
555 if ((mtab_zhp = zfs_open(zhp->zfs_hdl, entry.mnt_special,
556 ZFS_TYPE_FILESYSTEM)) == NULL)
557 continue;
559 /* Ignore legacy mounts as they are user managed */
560 verify(zfs_prop_get(mtab_zhp, ZFS_PROP_MOUNTPOINT, mnt_prop,
561 sizeof (mnt_prop), NULL, NULL, 0, B_FALSE) == 0);
562 if (strcmp(mnt_prop, "legacy") == 0) {
563 zfs_close(mtab_zhp);
564 continue;
567 err = func(mtab_zhp, data);
570 fclose(mnttab);
572 return (err);