]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
libfrog: move libfrog.h to libfrog/util.h
[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 bool
48 xfs_cleanup_fs(
49 struct scrub_ctx *ctx)
50 {
51 int error;
52
53 xfs_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 true;
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 xfs_cleanup_fs.
74 */
75 bool
76 xfs_setup_fs(
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_info(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 false;
99 }
100
101 error = fstat(ctx->mnt.fd, &ctx->mnt_sb);
102 if (error) {
103 str_errno(ctx, ctx->mntpoint);
104 return false;
105 }
106 error = fstatvfs(ctx->mnt.fd, &ctx->mnt_sv);
107 if (error) {
108 str_errno(ctx, ctx->mntpoint);
109 return false;
110 }
111 error = fstatfs(ctx->mnt.fd, &ctx->mnt_sf);
112 if (error) {
113 str_errno(ctx, ctx->mntpoint);
114 return false;
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 false;
126 }
127
128 if (!xfs_action_lists_alloc(ctx->mnt.fsgeom.agcount,
129 &ctx->action_lists)) {
130 str_error(ctx, ctx->mntpoint, _("Not enough memory."));
131 return false;
132 }
133
134 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
135 &ctx->fshandle_len);
136 if (error) {
137 str_errno(ctx, _("getting fshandle"));
138 return false;
139 }
140
141 /* Do we have kernel-assisted metadata scrubbing? */
142 if (!xfs_can_scrub_fs_metadata(ctx) || !xfs_can_scrub_inode(ctx) ||
143 !xfs_can_scrub_bmap(ctx) || !xfs_can_scrub_dir(ctx) ||
144 !xfs_can_scrub_attr(ctx) || !xfs_can_scrub_symlink(ctx) ||
145 !xfs_can_scrub_parent(ctx)) {
146 str_info(ctx, ctx->mntpoint,
147 _("Kernel metadata scrubbing facility is not available."));
148 return false;
149 }
150
151 /* Do we need kernel-assisted metadata repair? */
152 if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
153 str_info(ctx, ctx->mntpoint,
154 _("Kernel metadata repair facility is not available. Use -n to scrub."));
155 return false;
156 }
157
158 /* Did we find the log and rt devices, if they're present? */
159 if (ctx->mnt.fsgeom.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
160 str_info(ctx, ctx->mntpoint,
161 _("Unable to find log device path."));
162 return false;
163 }
164 if (ctx->mnt.fsgeom.rtblocks && ctx->fsinfo.fs_rt == NULL) {
165 str_info(ctx, ctx->mntpoint,
166 _("Unable to find realtime device path."));
167 return false;
168 }
169
170 /* Open the raw devices. */
171 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
172 if (error) {
173 str_errno(ctx, ctx->fsinfo.fs_name);
174 return false;
175 }
176
177 ctx->nr_io_threads = disk_heads(ctx->datadev);
178 if (verbose) {
179 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
180 ctx->mntpoint, scrub_nproc(ctx));
181 fflush(stdout);
182 }
183
184 if (ctx->fsinfo.fs_log) {
185 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
186 if (error) {
187 str_errno(ctx, ctx->fsinfo.fs_name);
188 return false;
189 }
190 }
191 if (ctx->fsinfo.fs_rt) {
192 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
193 if (error) {
194 str_errno(ctx, ctx->fsinfo.fs_name);
195 return false;
196 }
197 }
198
199 /*
200 * Everything's set up, which means any failures recorded after
201 * this point are most probably corruption errors (as opposed to
202 * purely setup errors).
203 */
204 log_info(ctx, _("Invoking online scrub."), ctx);
205 ctx->scrub_setup_succeeded = true;
206 return true;
207 }