]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
xfs_scrub: retry scrub (and repair) of items that are ok except for XFAIL
[thirdparty/xfsprogs-dev.git] / scrub / phase1.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 <unistd.h>
8 #include <sys/types.h>
9 #include <sys/time.h>
10 #include <sys/resource.h>
11 #include <sys/statvfs.h>
12 #include <fcntl.h>
13 #include <dirent.h>
14 #include <stdint.h>
15 #include <pthread.h>
16 #include "libfrog/util.h"
17 #include "libfrog/workqueue.h"
18 #include "input.h"
19 #include "libfrog/paths.h"
20 #include "handle.h"
21 #include "bitops.h"
22 #include "libfrog/avl64.h"
23 #include "list.h"
24 #include "xfs_scrub.h"
25 #include "common.h"
26 #include "disk.h"
27 #include "scrub.h"
28 #include "repair.h"
29 #include "libfrog/fsgeom.h"
30
31 /* Phase 1: Find filesystem geometry (and clean up after) */
32
33 /* Shut down the filesystem. */
34 void
35 xfs_shutdown_fs(
36 struct scrub_ctx *ctx)
37 {
38 int flag;
39
40 flag = XFS_FSOP_GOING_FLAGS_LOGFLUSH;
41 str_info(ctx, ctx->mntpoint, _("Shutting down filesystem!"));
42 if (ioctl(ctx->mnt.fd, XFS_IOC_GOINGDOWN, &flag))
43 str_errno(ctx, ctx->mntpoint);
44 }
45
46 /* Clean up the XFS-specific state data. */
47 int
48 scrub_cleanup(
49 struct scrub_ctx *ctx)
50 {
51 int error;
52
53 action_lists_free(&ctx->action_lists);
54 if (ctx->fshandle)
55 free_handle(ctx->fshandle, ctx->fshandle_len);
56 if (ctx->rtdev)
57 disk_close(ctx->rtdev);
58 if (ctx->logdev)
59 disk_close(ctx->logdev);
60 if (ctx->datadev)
61 disk_close(ctx->datadev);
62 fshandle_destroy();
63 error = -xfd_close(&ctx->mnt);
64 if (error)
65 str_liberror(ctx, error, _("closing mountpoint fd"));
66 fs_table_destroy();
67
68 return error;
69 }
70
71 /*
72 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
73 * Anything we've already built will be cleaned up by scrub_cleanup.
74 */
75 int
76 phase1_func(
77 struct scrub_ctx *ctx)
78 {
79 int error;
80
81 /*
82 * Open the directory with O_NOATIME. For mountpoints owned
83 * by root, this should be sufficient to ensure that we have
84 * CAP_SYS_ADMIN, which we probably need to do anything fancy
85 * with the (XFS driver) kernel.
86 */
87 error = -xfd_open(&ctx->mnt, ctx->mntpoint,
88 O_RDONLY | O_NOATIME | O_DIRECTORY);
89 if (error) {
90 if (error == EPERM)
91 str_error(ctx, ctx->mntpoint,
92 _("Must be root to run scrub."));
93 else if (error == ENOTTY)
94 str_error(ctx, ctx->mntpoint,
95 _("Not an XFS filesystem."));
96 else
97 str_liberror(ctx, error, ctx->mntpoint);
98 return error;
99 }
100
101 error = fstat(ctx->mnt.fd, &ctx->mnt_sb);
102 if (error) {
103 str_errno(ctx, ctx->mntpoint);
104 return error;
105 }
106 error = fstatvfs(ctx->mnt.fd, &ctx->mnt_sv);
107 if (error) {
108 str_errno(ctx, ctx->mntpoint);
109 return error;
110 }
111 error = fstatfs(ctx->mnt.fd, &ctx->mnt_sf);
112 if (error) {
113 str_errno(ctx, ctx->mntpoint);
114 return error;
115 }
116
117 /*
118 * Flush everything out to disk before we start checking.
119 * This seems to reduce the incidence of stale file handle
120 * errors when we open things by handle.
121 */
122 error = syncfs(ctx->mnt.fd);
123 if (error) {
124 str_errno(ctx, ctx->mntpoint);
125 return error;
126 }
127
128 error = action_lists_alloc(ctx->mnt.fsgeom.agcount,
129 &ctx->action_lists);
130 if (error) {
131 str_liberror(ctx, error, _("allocating action lists"));
132 return error;
133 }
134
135 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
136 &ctx->fshandle_len);
137 if (error) {
138 str_errno(ctx, _("getting fshandle"));
139 return error;
140 }
141
142 /* Do we have kernel-assisted metadata scrubbing? */
143 if (!can_scrub_fs_metadata(ctx) || !can_scrub_inode(ctx) ||
144 !can_scrub_bmap(ctx) || !can_scrub_dir(ctx) ||
145 !can_scrub_attr(ctx) || !can_scrub_symlink(ctx) ||
146 !can_scrub_parent(ctx)) {
147 str_error(ctx, ctx->mntpoint,
148 _("Kernel metadata scrubbing facility is not available."));
149 return ECANCELED;
150 }
151
152 /* Do we need kernel-assisted metadata repair? */
153 if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
154 str_error(ctx, ctx->mntpoint,
155 _("Kernel metadata repair facility is not available. Use -n to scrub."));
156 return ECANCELED;
157 }
158
159 /* Did we find the log and rt devices, if they're present? */
160 if (ctx->mnt.fsgeom.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
161 str_error(ctx, ctx->mntpoint,
162 _("Unable to find log device path."));
163 return ECANCELED;
164 }
165 if (ctx->mnt.fsgeom.rtblocks && ctx->fsinfo.fs_rt == NULL) {
166 str_error(ctx, ctx->mntpoint,
167 _("Unable to find realtime device path."));
168 return ECANCELED;
169 }
170
171 /* Open the raw devices. */
172 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
173 if (!ctx->datadev) {
174 str_error(ctx, ctx->mntpoint, _("Unable to open data device."));
175 return ECANCELED;
176 }
177
178 ctx->nr_io_threads = disk_heads(ctx->datadev);
179 if (verbose) {
180 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
181 ctx->mntpoint, scrub_nproc(ctx));
182 fflush(stdout);
183 }
184
185 if (ctx->fsinfo.fs_log) {
186 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
187 if (!ctx->logdev) {
188 str_error(ctx, ctx->mntpoint,
189 _("Unable to open external log device."));
190 return ECANCELED;
191 }
192 }
193 if (ctx->fsinfo.fs_rt) {
194 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
195 if (!ctx->rtdev) {
196 str_error(ctx, ctx->mntpoint,
197 _("Unable to open realtime device."));
198 return ECANCELED;
199 }
200 }
201
202 /*
203 * Everything's set up, which means any failures recorded after
204 * this point are most probably corruption errors (as opposed to
205 * purely setup errors).
206 */
207 log_info(ctx, _("Invoking online scrub."), ctx);
208 ctx->scrub_setup_succeeded = true;
209 return 0;
210 }