]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase3.c
xfs_scrub: remove moveon from main program
[thirdparty/xfsprogs-dev.git] / scrub / phase3.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6 #include "xfs.h"
7 #include <stdint.h>
8 #include <sys/types.h>
9 #include <sys/statvfs.h>
10 #include "list.h"
11 #include "libfrog/paths.h"
12 #include "libfrog/workqueue.h"
13 #include "xfs_scrub.h"
14 #include "common.h"
15 #include "counter.h"
16 #include "inodes.h"
17 #include "progress.h"
18 #include "scrub.h"
19 #include "repair.h"
20
21 /* Phase 3: Scan all inodes. */
22
23 /*
24 * Run a per-file metadata scanner. We use the ino/gen interface to
25 * ensure that the inode we're checking matches what the inode scan
26 * told us to look at.
27 */
28 static int
29 scrub_fd(
30 struct scrub_ctx *ctx,
31 int (*fn)(struct scrub_ctx *ctx, uint64_t ino,
32 uint32_t gen, struct action_list *a),
33 struct xfs_bulkstat *bs,
34 struct action_list *alist)
35 {
36 return fn(ctx, bs->bs_ino, bs->bs_gen, alist);
37 }
38
39 struct scrub_inode_ctx {
40 struct ptcounter *icount;
41 bool aborted;
42 };
43
44 /* Report a filesystem error that the vfs fed us on close. */
45 static void
46 report_close_error(
47 struct scrub_ctx *ctx,
48 struct xfs_bulkstat *bstat)
49 {
50 char descr[DESCR_BUFSZ];
51 int old_errno = errno;
52
53 scrub_render_ino_descr(ctx, descr, DESCR_BUFSZ, bstat->bs_ino,
54 bstat->bs_gen, NULL);
55 errno = old_errno;
56 str_errno(ctx, descr);
57 }
58
59 /* Verify the contents, xattrs, and extent maps of an inode. */
60 static int
61 scrub_inode(
62 struct scrub_ctx *ctx,
63 struct xfs_handle *handle,
64 struct xfs_bulkstat *bstat,
65 void *arg)
66 {
67 struct action_list alist;
68 struct scrub_inode_ctx *ictx = arg;
69 struct ptcounter *icount = ictx->icount;
70 xfs_agnumber_t agno;
71 int fd = -1;
72 int error;
73
74 action_list_init(&alist);
75 agno = cvt_ino_to_agno(&ctx->mnt, bstat->bs_ino);
76 background_sleep();
77
78 /* Try to open the inode to pin it. */
79 if (S_ISREG(bstat->bs_mode)) {
80 fd = scrub_open_handle(handle);
81 /* Stale inode means we scan the whole cluster again. */
82 if (fd < 0 && errno == ESTALE)
83 return ESTALE;
84 }
85
86 /* Scrub the inode. */
87 error = scrub_fd(ctx, xfs_scrub_inode_fields, bstat, &alist);
88 if (error)
89 goto out;
90
91 error = action_list_process_or_defer(ctx, agno, &alist);
92 if (error)
93 goto out;
94
95 /* Scrub all block mappings. */
96 error = scrub_fd(ctx, xfs_scrub_data_fork, bstat, &alist);
97 if (error)
98 goto out;
99 error = scrub_fd(ctx, xfs_scrub_attr_fork, bstat, &alist);
100 if (error)
101 goto out;
102 error = scrub_fd(ctx, xfs_scrub_cow_fork, bstat, &alist);
103 if (error)
104 goto out;
105
106 error = action_list_process_or_defer(ctx, agno, &alist);
107 if (error)
108 goto out;
109
110 if (S_ISLNK(bstat->bs_mode)) {
111 /* Check symlink contents. */
112 error = xfs_scrub_symlink(ctx, bstat->bs_ino, bstat->bs_gen,
113 &alist);
114 } else if (S_ISDIR(bstat->bs_mode)) {
115 /* Check the directory entries. */
116 error = scrub_fd(ctx, xfs_scrub_dir, bstat, &alist);
117 }
118 if (error)
119 goto out;
120
121 /* Check all the extended attributes. */
122 error = scrub_fd(ctx, xfs_scrub_attr, bstat, &alist);
123 if (error)
124 goto out;
125
126 /* Check parent pointers. */
127 error = scrub_fd(ctx, xfs_scrub_parent, bstat, &alist);
128 if (error)
129 goto out;
130
131 /* Try to repair the file while it's open. */
132 error = action_list_process_or_defer(ctx, agno, &alist);
133 if (error)
134 goto out;
135
136 out:
137 if (error)
138 ictx->aborted = true;
139
140 error = ptcounter_add(icount, 1);
141 if (error) {
142 str_liberror(ctx, error,
143 _("incrementing scanned inode counter"));
144 ictx->aborted = true;
145 }
146 progress_add(1);
147 action_list_defer(ctx, agno, &alist);
148 if (fd >= 0) {
149 int err2;
150
151 err2 = close(fd);
152 if (err2) {
153 report_close_error(ctx, bstat);
154 ictx->aborted = true;
155 }
156 }
157
158 if (!error && ictx->aborted)
159 error = ECANCELED;
160 return error;
161 }
162
163 /* Verify all the inodes in a filesystem. */
164 int
165 phase3_func(
166 struct scrub_ctx *ctx)
167 {
168 struct scrub_inode_ctx ictx = { NULL };
169 uint64_t val;
170 int err;
171
172 err = ptcounter_alloc(scrub_nproc(ctx), &ictx.icount);
173 if (err) {
174 str_liberror(ctx, err, _("creating scanned inode counter"));
175 return err;
176 }
177
178 err = scrub_scan_all_inodes(ctx, scrub_inode, &ictx);
179 if (!err && ictx.aborted)
180 err = ECANCELED;
181 if (err)
182 goto free;
183
184 xfs_scrub_report_preen_triggers(ctx);
185 err = ptcounter_value(ictx.icount, &val);
186 if (err) {
187 str_liberror(ctx, err, _("summing scanned inode counter"));
188 return err;
189 }
190
191 ctx->inodes_checked = val;
192 free:
193 ptcounter_free(ictx.icount);
194 return err;
195 }
196
197 /* Estimate how much work we're going to do. */
198 int
199 phase3_estimate(
200 struct scrub_ctx *ctx,
201 uint64_t *items,
202 unsigned int *nr_threads,
203 int *rshift)
204 {
205 *items = ctx->mnt_sv.f_files - ctx->mnt_sv.f_ffree;
206 *nr_threads = scrub_nproc(ctx);
207 *rshift = 0;
208 return 0;
209 }