]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase7.c
xfs: remove unnecessary parameter from scrub_scan_estimate_blocks
[thirdparty/xfsprogs-dev.git] / scrub / phase7.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 <stdlib.h>
9 #include <sys/statvfs.h>
10 #include "libfrog/paths.h"
11 #include "libfrog/ptvar.h"
12 #include "list.h"
13 #include "xfs_scrub.h"
14 #include "common.h"
15 #include "scrub.h"
16 #include "fscounters.h"
17 #include "spacemap.h"
18 #include "repair.h"
19
20 /* Phase 7: Check summary counters. */
21
22 struct summary_counts {
23 unsigned long long dbytes; /* data dev bytes */
24 unsigned long long rbytes; /* rt dev bytes */
25 unsigned long long next_phys; /* next phys bytes we see? */
26 unsigned long long agbytes; /* freespace bytes */
27 };
28
29 /* Record block usage. */
30 static int
31 count_block_summary(
32 struct scrub_ctx *ctx,
33 struct fsmap *fsmap,
34 void *arg)
35 {
36 struct summary_counts *counts;
37 unsigned long long len;
38 int ret;
39
40 counts = ptvar_get((struct ptvar *)arg, &ret);
41 if (ret) {
42 str_liberror(ctx, -ret, _("retrieving summary counts"));
43 return -ret;
44 }
45 if (fsmap->fmr_device == ctx->fsinfo.fs_logdev)
46 return 0;
47 if ((fsmap->fmr_flags & FMR_OF_SPECIAL_OWNER) &&
48 fsmap->fmr_owner == XFS_FMR_OWN_FREE)
49 return 0;
50
51 len = fsmap->fmr_length;
52
53 /* freesp btrees live in free space, need to adjust counters later. */
54 if ((fsmap->fmr_flags & FMR_OF_SPECIAL_OWNER) &&
55 fsmap->fmr_owner == XFS_FMR_OWN_AG) {
56 counts->agbytes += fsmap->fmr_length;
57 }
58 if (fsmap->fmr_device == ctx->fsinfo.fs_rtdev) {
59 /* Count realtime extents. */
60 counts->rbytes += len;
61 } else {
62 /* Count datadev extents. */
63 if (counts->next_phys >= fsmap->fmr_physical + len)
64 return 0;
65 else if (counts->next_phys > fsmap->fmr_physical)
66 len = counts->next_phys - fsmap->fmr_physical;
67 counts->dbytes += len;
68 counts->next_phys = fsmap->fmr_physical + fsmap->fmr_length;
69 }
70
71 return 0;
72 }
73
74 /* Add all the summaries in the per-thread counter */
75 static int
76 add_summaries(
77 struct ptvar *ptv,
78 void *data,
79 void *arg)
80 {
81 struct summary_counts *total = arg;
82 struct summary_counts *item = data;
83
84 total->dbytes += item->dbytes;
85 total->rbytes += item->rbytes;
86 total->agbytes += item->agbytes;
87 return 0;
88 }
89
90 /*
91 * Count all inodes and blocks in the filesystem as told by GETFSMAP and
92 * BULKSTAT, and compare that to summary counters. Since this is a live
93 * filesystem we'll be content if the summary counts are within 10% of
94 * what we observed.
95 */
96 int
97 phase7_func(
98 struct scrub_ctx *ctx)
99 {
100 struct summary_counts totalcount = {0};
101 struct action_list alist;
102 struct ptvar *ptvar;
103 unsigned long long used_data;
104 unsigned long long used_rt;
105 unsigned long long used_files;
106 unsigned long long stat_data;
107 unsigned long long stat_rt;
108 uint64_t counted_inodes = 0;
109 unsigned long long absdiff;
110 unsigned long long d_blocks;
111 unsigned long long d_bfree;
112 unsigned long long r_blocks;
113 unsigned long long r_bfree;
114 bool complain;
115 int ip;
116 int error;
117
118 /* Check and fix the fs summary counters. */
119 action_list_init(&alist);
120 error = scrub_fs_summary(ctx, &alist);
121 if (error)
122 return error;
123 error = action_list_process(ctx, ctx->mnt.fd, &alist,
124 ALP_COMPLAIN_IF_UNFIXED | ALP_NOPROGRESS);
125 if (error)
126 return error;
127
128 /* Flush everything out to disk before we start counting. */
129 error = syncfs(ctx->mnt.fd);
130 if (error) {
131 str_errno(ctx, ctx->mntpoint);
132 return error;
133 }
134
135 error = -ptvar_alloc(scrub_nproc(ctx), sizeof(struct summary_counts),
136 &ptvar);
137 if (error) {
138 str_liberror(ctx, error, _("setting up block counter"));
139 return error;
140 }
141
142 /* Use fsmap to count blocks. */
143 error = scrub_scan_all_spacemaps(ctx, count_block_summary, ptvar);
144 if (error)
145 goto out_free;
146 error = -ptvar_foreach(ptvar, add_summaries, &totalcount);
147 if (error) {
148 str_liberror(ctx, error, _("counting blocks"));
149 goto out_free;
150 }
151 ptvar_free(ptvar);
152
153 /* Scan the whole fs. */
154 error = scrub_count_all_inodes(ctx, &counted_inodes);
155 if (error) {
156 str_liberror(ctx, error, _("counting inodes"));
157 return error;
158 }
159
160 error = scrub_scan_estimate_blocks(ctx, &d_blocks, &d_bfree, &r_blocks,
161 &r_bfree, &used_files);
162 if (error) {
163 str_liberror(ctx, error, _("estimating verify work"));
164 return error;
165 }
166
167 /*
168 * If we counted blocks with fsmap, then dblocks includes
169 * blocks for the AGFL and the freespace/rmap btrees. The
170 * filesystem treats them as "free", but since we scanned
171 * them, we'll consider them used.
172 */
173 d_bfree -= cvt_b_to_off_fsbt(&ctx->mnt, totalcount.agbytes);
174
175 /* Report on what we found. */
176 used_data = cvt_off_fsb_to_b(&ctx->mnt, d_blocks - d_bfree);
177 used_rt = cvt_off_fsb_to_b(&ctx->mnt, r_blocks - r_bfree);
178 stat_data = totalcount.dbytes;
179 stat_rt = totalcount.rbytes;
180
181 /*
182 * Complain if the counts are off by more than 10% unless
183 * the inaccuracy is less than 32MB worth of blocks or 100 inodes.
184 */
185 absdiff = 1ULL << 25;
186 complain = verbose;
187 complain |= !within_range(ctx, stat_data, used_data, absdiff, 1, 10,
188 _("data blocks"));
189 complain |= !within_range(ctx, stat_rt, used_rt, absdiff, 1, 10,
190 _("realtime blocks"));
191 complain |= !within_range(ctx, counted_inodes, used_files, 100, 1, 10,
192 _("inodes"));
193
194 if (complain) {
195 double d, r, i;
196 char *du, *ru, *iu;
197
198 if (used_rt || stat_rt) {
199 d = auto_space_units(used_data, &du);
200 r = auto_space_units(used_rt, &ru);
201 i = auto_units(used_files, &iu, &ip);
202 fprintf(stdout,
203 _("%.1f%s data used; %.1f%s realtime data used; %.*f%s inodes used.\n"),
204 d, du, r, ru, ip, i, iu);
205 d = auto_space_units(stat_data, &du);
206 r = auto_space_units(stat_rt, &ru);
207 i = auto_units(counted_inodes, &iu, &ip);
208 fprintf(stdout,
209 _("%.1f%s data found; %.1f%s realtime data found; %.*f%s inodes found.\n"),
210 d, du, r, ru, ip, i, iu);
211 } else {
212 d = auto_space_units(used_data, &du);
213 i = auto_units(used_files, &iu, &ip);
214 fprintf(stdout,
215 _("%.1f%s data used; %.*f%s inodes used.\n"),
216 d, du, ip, i, iu);
217 d = auto_space_units(stat_data, &du);
218 i = auto_units(counted_inodes, &iu, &ip);
219 fprintf(stdout,
220 _("%.1f%s data found; %.*f%s inodes found.\n"),
221 d, du, ip, i, iu);
222 }
223 fflush(stdout);
224 }
225
226 /*
227 * Complain if the checked inode counts are off, which
228 * implies an incomplete check.
229 */
230 if (verbose ||
231 !within_range(ctx, counted_inodes, ctx->inodes_checked, 100, 1, 10,
232 _("checked inodes"))) {
233 double i1, i2;
234 char *i1u, *i2u;
235 int i1p, i2p;
236
237 i1 = auto_units(counted_inodes, &i1u, &i1p);
238 i2 = auto_units(ctx->inodes_checked, &i2u, &i2p);
239 fprintf(stdout,
240 _("%.*f%s inodes counted; %.*f%s inodes checked.\n"),
241 i1p, i1, i1u, i2p, i2, i2u);
242 fflush(stdout);
243 }
244
245 /*
246 * Complain if the checked block counts are off, which
247 * implies an incomplete check.
248 */
249 if (ctx->bytes_checked &&
250 (verbose ||
251 !within_range(ctx, used_data + used_rt,
252 ctx->bytes_checked, absdiff, 1, 10,
253 _("verified blocks")))) {
254 double b1, b2;
255 char *b1u, *b2u;
256
257 b1 = auto_space_units(used_data + used_rt, &b1u);
258 b2 = auto_space_units(ctx->bytes_checked, &b2u);
259 fprintf(stdout,
260 _("%.1f%s data counted; %.1f%s data verified.\n"),
261 b1, b1u, b2, b2u);
262 fflush(stdout);
263 }
264
265 return 0;
266 out_free:
267 ptvar_free(ptvar);
268 return error;
269 }