]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
xfs_scrub: don't close mnt_fd when mnt_fd open fails
[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.h"
17 #include "workqueue.h"
18 #include "input.h"
19 #include "path.h"
20 #include "handle.h"
21 #include "bitops.h"
22 #include "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
30 /* Phase 1: Find filesystem geometry (and clean up after) */
31
32 /* Shut down the filesystem. */
33 void
34 xfs_shutdown_fs(
35 struct scrub_ctx *ctx)
36 {
37 int flag;
38
39 flag = XFS_FSOP_GOING_FLAGS_LOGFLUSH;
40 str_info(ctx, ctx->mntpoint, _("Shutting down filesystem!"));
41 if (ioctl(ctx->mnt_fd, XFS_IOC_GOINGDOWN, &flag))
42 str_errno(ctx, ctx->mntpoint);
43 }
44
45 /* Clean up the XFS-specific state data. */
46 bool
47 xfs_cleanup_fs(
48 struct scrub_ctx *ctx)
49 {
50 int error;
51
52 xfs_action_lists_free(&ctx->action_lists);
53 if (ctx->fshandle)
54 free_handle(ctx->fshandle, ctx->fshandle_len);
55 if (ctx->rtdev)
56 disk_close(ctx->rtdev);
57 if (ctx->logdev)
58 disk_close(ctx->logdev);
59 if (ctx->datadev)
60 disk_close(ctx->datadev);
61 fshandle_destroy();
62 if (ctx->mnt_fd >= 0) {
63 error = close(ctx->mnt_fd);
64 if (error)
65 str_errno(ctx, _("closing mountpoint fd"));
66 }
67 fs_table_destroy();
68
69 return true;
70 }
71
72 /*
73 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
74 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
75 */
76 bool
77 xfs_setup_fs(
78 struct scrub_ctx *ctx)
79 {
80 int error;
81
82 /*
83 * Open the directory with O_NOATIME. For mountpoints owned
84 * by root, this should be sufficient to ensure that we have
85 * CAP_SYS_ADMIN, which we probably need to do anything fancy
86 * with the (XFS driver) kernel.
87 */
88 ctx->mnt_fd = open(ctx->mntpoint, O_RDONLY | O_NOATIME | O_DIRECTORY);
89 if (ctx->mnt_fd < 0) {
90 if (errno == EPERM)
91 str_info(ctx, ctx->mntpoint,
92 _("Must be root to run scrub."));
93 else
94 str_errno(ctx, ctx->mntpoint);
95 return false;
96 }
97
98 error = fstat(ctx->mnt_fd, &ctx->mnt_sb);
99 if (error) {
100 str_errno(ctx, ctx->mntpoint);
101 return false;
102 }
103 error = fstatvfs(ctx->mnt_fd, &ctx->mnt_sv);
104 if (error) {
105 str_errno(ctx, ctx->mntpoint);
106 return false;
107 }
108 error = fstatfs(ctx->mnt_fd, &ctx->mnt_sf);
109 if (error) {
110 str_errno(ctx, ctx->mntpoint);
111 return false;
112 }
113
114 if (!platform_test_xfs_fd(ctx->mnt_fd)) {
115 str_info(ctx, ctx->mntpoint,
116 _("Does not appear to be an XFS filesystem!"));
117 return false;
118 }
119
120 /*
121 * Flush everything out to disk before we start checking.
122 * This seems to reduce the incidence of stale file handle
123 * errors when we open things by handle.
124 */
125 error = syncfs(ctx->mnt_fd);
126 if (error) {
127 str_errno(ctx, ctx->mntpoint);
128 return false;
129 }
130
131 /* Retrieve XFS geometry. */
132 error = ioctl(ctx->mnt_fd, XFS_IOC_FSGEOMETRY, &ctx->geo);
133 if (error) {
134 str_errno(ctx, ctx->mntpoint);
135 return false;
136 }
137
138 if (!xfs_action_lists_alloc(ctx->geo.agcount, &ctx->action_lists)) {
139 str_error(ctx, ctx->mntpoint, _("Not enough memory."));
140 return false;
141 }
142
143 ctx->agblklog = log2_roundup(ctx->geo.agblocks);
144 ctx->blocklog = highbit32(ctx->geo.blocksize);
145 ctx->inodelog = highbit32(ctx->geo.inodesize);
146 ctx->inopblog = ctx->blocklog - ctx->inodelog;
147
148 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
149 &ctx->fshandle_len);
150 if (error) {
151 str_errno(ctx, _("getting fshandle"));
152 return false;
153 }
154
155 /* Do we have kernel-assisted metadata scrubbing? */
156 if (!xfs_can_scrub_fs_metadata(ctx) || !xfs_can_scrub_inode(ctx) ||
157 !xfs_can_scrub_bmap(ctx) || !xfs_can_scrub_dir(ctx) ||
158 !xfs_can_scrub_attr(ctx) || !xfs_can_scrub_symlink(ctx) ||
159 !xfs_can_scrub_parent(ctx)) {
160 str_info(ctx, ctx->mntpoint,
161 _("Kernel metadata scrubbing facility is not available."));
162 return false;
163 }
164
165 /* Do we need kernel-assisted metadata repair? */
166 if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
167 str_info(ctx, ctx->mntpoint,
168 _("Kernel metadata repair facility is not available. Use -n to scrub."));
169 return false;
170 }
171
172 /* Did we find the log and rt devices, if they're present? */
173 if (ctx->geo.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
174 str_info(ctx, ctx->mntpoint,
175 _("Unable to find log device path."));
176 return false;
177 }
178 if (ctx->geo.rtblocks && ctx->fsinfo.fs_rt == NULL) {
179 str_info(ctx, ctx->mntpoint,
180 _("Unable to find realtime device path."));
181 return false;
182 }
183
184 /* Open the raw devices. */
185 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
186 if (error) {
187 str_errno(ctx, ctx->fsinfo.fs_name);
188 return false;
189 }
190
191 ctx->nr_io_threads = disk_heads(ctx->datadev);
192 if (verbose) {
193 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
194 ctx->mntpoint, scrub_nproc(ctx));
195 fflush(stdout);
196 }
197
198 if (ctx->fsinfo.fs_log) {
199 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
200 if (error) {
201 str_errno(ctx, ctx->fsinfo.fs_name);
202 return false;
203 }
204 }
205 if (ctx->fsinfo.fs_rt) {
206 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
207 if (error) {
208 str_errno(ctx, ctx->fsinfo.fs_name);
209 return false;
210 }
211 }
212
213 /*
214 * Everything's set up, which means any failures recorded after
215 * this point are most probably corruption errors (as opposed to
216 * purely setup errors).
217 */
218 log_info(ctx, _("Invoking online scrub."), ctx);
219 ctx->scrub_setup_succeeded = true;
220 return true;
221 }