]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
xfs_scrub: actually check for errors coming from close()
[thirdparty/xfsprogs-dev.git] / scrub / phase1.c
1 /*
2 * Copyright (C) 2018 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 #include "xfs.h"
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <sys/resource.h>
25 #include <sys/statvfs.h>
26 #include <fcntl.h>
27 #include <dirent.h>
28 #include <stdint.h>
29 #include <pthread.h>
30 #include "libfrog.h"
31 #include "workqueue.h"
32 #include "input.h"
33 #include "path.h"
34 #include "handle.h"
35 #include "bitops.h"
36 #include "avl64.h"
37 #include "list.h"
38 #include "xfs_scrub.h"
39 #include "common.h"
40 #include "disk.h"
41 #include "scrub.h"
42
43 /* Phase 1: Find filesystem geometry (and clean up after) */
44
45 /* Shut down the filesystem. */
46 void
47 xfs_shutdown_fs(
48 struct scrub_ctx *ctx)
49 {
50 int flag;
51
52 flag = XFS_FSOP_GOING_FLAGS_LOGFLUSH;
53 str_info(ctx, ctx->mntpoint, _("Shutting down filesystem!"));
54 if (ioctl(ctx->mnt_fd, XFS_IOC_GOINGDOWN, &flag))
55 str_errno(ctx, ctx->mntpoint);
56 }
57
58 /* Clean up the XFS-specific state data. */
59 bool
60 xfs_cleanup_fs(
61 struct scrub_ctx *ctx)
62 {
63 int error;
64
65 if (ctx->fshandle)
66 free_handle(ctx->fshandle, ctx->fshandle_len);
67 if (ctx->rtdev)
68 disk_close(ctx->rtdev);
69 if (ctx->logdev)
70 disk_close(ctx->logdev);
71 if (ctx->datadev)
72 disk_close(ctx->datadev);
73 fshandle_destroy();
74 error = close(ctx->mnt_fd);
75 if (error)
76 str_errno(ctx, _("closing mountpoint fd"));
77 fs_table_destroy();
78
79 return true;
80 }
81
82 /*
83 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
84 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
85 */
86 bool
87 xfs_setup_fs(
88 struct scrub_ctx *ctx)
89 {
90 int error;
91
92 /*
93 * Open the directory with O_NOATIME. For mountpoints owned
94 * by root, this should be sufficient to ensure that we have
95 * CAP_SYS_ADMIN, which we probably need to do anything fancy
96 * with the (XFS driver) kernel.
97 */
98 ctx->mnt_fd = open(ctx->mntpoint, O_RDONLY | O_NOATIME | O_DIRECTORY);
99 if (ctx->mnt_fd < 0) {
100 if (errno == EPERM)
101 str_info(ctx, ctx->mntpoint,
102 _("Must be root to run scrub."));
103 else
104 str_errno(ctx, ctx->mntpoint);
105 return false;
106 }
107
108 error = fstat(ctx->mnt_fd, &ctx->mnt_sb);
109 if (error) {
110 str_errno(ctx, ctx->mntpoint);
111 return false;
112 }
113 error = fstatvfs(ctx->mnt_fd, &ctx->mnt_sv);
114 if (error) {
115 str_errno(ctx, ctx->mntpoint);
116 return false;
117 }
118 error = fstatfs(ctx->mnt_fd, &ctx->mnt_sf);
119 if (error) {
120 str_errno(ctx, ctx->mntpoint);
121 return false;
122 }
123
124 ctx->nr_io_threads = nproc;
125 if (verbose) {
126 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
127 ctx->mntpoint, scrub_nproc(ctx));
128 fflush(stdout);
129 }
130
131 if (!platform_test_xfs_fd(ctx->mnt_fd)) {
132 str_info(ctx, ctx->mntpoint,
133 _("Does not appear to be an XFS filesystem!"));
134 return false;
135 }
136
137 /*
138 * Flush everything out to disk before we start checking.
139 * This seems to reduce the incidence of stale file handle
140 * errors when we open things by handle.
141 */
142 error = syncfs(ctx->mnt_fd);
143 if (error) {
144 str_errno(ctx, ctx->mntpoint);
145 return false;
146 }
147
148 /* Retrieve XFS geometry. */
149 error = ioctl(ctx->mnt_fd, XFS_IOC_FSGEOMETRY, &ctx->geo);
150 if (error) {
151 str_errno(ctx, ctx->mntpoint);
152 return false;
153 }
154
155 ctx->agblklog = log2_roundup(ctx->geo.agblocks);
156 ctx->blocklog = highbit32(ctx->geo.blocksize);
157 ctx->inodelog = highbit32(ctx->geo.inodesize);
158 ctx->inopblog = ctx->blocklog - ctx->inodelog;
159
160 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
161 &ctx->fshandle_len);
162 if (error) {
163 str_errno(ctx, _("getting fshandle"));
164 return false;
165 }
166
167 /* Do we have kernel-assisted metadata scrubbing? */
168 if (!xfs_can_scrub_fs_metadata(ctx) || !xfs_can_scrub_inode(ctx) ||
169 !xfs_can_scrub_bmap(ctx) || !xfs_can_scrub_dir(ctx) ||
170 !xfs_can_scrub_attr(ctx) || !xfs_can_scrub_symlink(ctx) ||
171 !xfs_can_scrub_parent(ctx)) {
172 str_info(ctx, ctx->mntpoint,
173 _("Kernel metadata scrubbing facility is not available."));
174 return false;
175 }
176
177 /* Do we need kernel-assisted metadata repair? */
178 if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
179 str_info(ctx, ctx->mntpoint,
180 _("Kernel metadata repair facility is not available. Use -n to scrub."));
181 return false;
182 }
183
184 /* Did we find the log and rt devices, if they're present? */
185 if (ctx->geo.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
186 str_info(ctx, ctx->mntpoint,
187 _("Unable to find log device path."));
188 return false;
189 }
190 if (ctx->geo.rtblocks && ctx->fsinfo.fs_rt == NULL) {
191 str_info(ctx, ctx->mntpoint,
192 _("Unable to find realtime device path."));
193 return false;
194 }
195
196 /* Open the raw devices. */
197 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
198 if (error) {
199 str_errno(ctx, ctx->fsinfo.fs_name);
200 return false;
201 }
202
203 if (ctx->fsinfo.fs_log) {
204 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
205 if (error) {
206 str_errno(ctx, ctx->fsinfo.fs_name);
207 return false;
208 }
209 }
210 if (ctx->fsinfo.fs_rt) {
211 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
212 if (error) {
213 str_errno(ctx, ctx->fsinfo.fs_name);
214 return false;
215 }
216 }
217
218 /*
219 * Everything's set up, which means any failures recorded after
220 * this point are most probably corruption errors (as opposed to
221 * purely setup errors).
222 */
223 log_info(ctx, _("Invoking online scrub."), ctx);
224 ctx->scrub_setup_succeeded = true;
225 return true;
226 }