]>
Commit | Line | Data |
---|---|---|
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 | xfs_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 | bool | |
97 | xfs_scan_summary( | |
98 | struct scrub_ctx *ctx) | |
99 | { | |
100 | struct summary_counts totalcount = {0}; | |
101 | struct xfs_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 | unsigned long long f_files; | |
115 | unsigned long long f_free; | |
116 | bool moveon; | |
117 | bool complain; | |
118 | int ip; | |
119 | int error; | |
120 | ||
121 | /* Check and fix the fs summary counters. */ | |
122 | xfs_action_list_init(&alist); | |
123 | moveon = xfs_scrub_fs_summary(ctx, &alist); | |
124 | if (!moveon) | |
125 | return false; | |
126 | moveon = xfs_action_list_process(ctx, ctx->mnt.fd, &alist, | |
127 | ALP_COMPLAIN_IF_UNFIXED | ALP_NOPROGRESS); | |
128 | if (!moveon) | |
129 | return moveon; | |
130 | ||
131 | /* Flush everything out to disk before we start counting. */ | |
132 | error = syncfs(ctx->mnt.fd); | |
133 | if (error) { | |
134 | str_errno(ctx, ctx->mntpoint); | |
135 | return false; | |
136 | } | |
137 | ||
138 | error = ptvar_alloc(scrub_nproc(ctx), sizeof(struct summary_counts), | |
139 | &ptvar); | |
140 | if (error) { | |
141 | str_liberror(ctx, error, _("setting up block counter")); | |
142 | return false; | |
143 | } | |
144 | ||
145 | /* Use fsmap to count blocks. */ | |
146 | error = scrub_scan_all_spacemaps(ctx, count_block_summary, ptvar); | |
147 | if (error) { | |
148 | moveon = false; | |
149 | goto out_free; | |
150 | } | |
151 | error = ptvar_foreach(ptvar, xfs_add_summaries, &totalcount); | |
152 | if (error) { | |
153 | str_liberror(ctx, error, _("counting blocks")); | |
154 | goto out_free; | |
155 | } | |
156 | ptvar_free(ptvar); | |
157 | ||
158 | /* Scan the whole fs. */ | |
159 | error = scrub_count_all_inodes(ctx, &counted_inodes); | |
160 | if (error) { | |
161 | str_liberror(ctx, error, _("counting inodes")); | |
162 | moveon = false; | |
163 | goto out; | |
164 | } | |
165 | ||
166 | error = scrub_scan_estimate_blocks(ctx, &d_blocks, &d_bfree, &r_blocks, | |
167 | &r_bfree, &f_files, &f_free); | |
168 | if (error) { | |
169 | str_liberror(ctx, error, _("estimating verify work")); | |
170 | return false; | |
171 | } | |
172 | ||
173 | /* | |
174 | * If we counted blocks with fsmap, then dblocks includes | |
175 | * blocks for the AGFL and the freespace/rmap btrees. The | |
176 | * filesystem treats them as "free", but since we scanned | |
177 | * them, we'll consider them used. | |
178 | */ | |
179 | d_bfree -= cvt_b_to_off_fsbt(&ctx->mnt, totalcount.agbytes); | |
180 | ||
181 | /* Report on what we found. */ | |
182 | used_data = cvt_off_fsb_to_b(&ctx->mnt, d_blocks - d_bfree); | |
183 | used_rt = cvt_off_fsb_to_b(&ctx->mnt, r_blocks - r_bfree); | |
184 | used_files = f_files - f_free; | |
185 | stat_data = totalcount.dbytes; | |
186 | stat_rt = totalcount.rbytes; | |
187 | ||
188 | /* | |
189 | * Complain if the counts are off by more than 10% unless | |
190 | * the inaccuracy is less than 32MB worth of blocks or 100 inodes. | |
191 | */ | |
192 | absdiff = 1ULL << 25; | |
193 | complain = verbose; | |
194 | complain |= !within_range(ctx, stat_data, used_data, absdiff, 1, 10, | |
195 | _("data blocks")); | |
196 | complain |= !within_range(ctx, stat_rt, used_rt, absdiff, 1, 10, | |
197 | _("realtime blocks")); | |
198 | complain |= !within_range(ctx, counted_inodes, used_files, 100, 1, 10, | |
199 | _("inodes")); | |
200 | ||
201 | if (complain) { | |
202 | double d, r, i; | |
203 | char *du, *ru, *iu; | |
204 | ||
205 | if (used_rt || stat_rt) { | |
206 | d = auto_space_units(used_data, &du); | |
207 | r = auto_space_units(used_rt, &ru); | |
208 | i = auto_units(used_files, &iu, &ip); | |
209 | fprintf(stdout, | |
210 | _("%.1f%s data used; %.1f%s realtime data used; %.*f%s inodes used.\n"), | |
211 | d, du, r, ru, ip, i, iu); | |
212 | d = auto_space_units(stat_data, &du); | |
213 | r = auto_space_units(stat_rt, &ru); | |
214 | i = auto_units(counted_inodes, &iu, &ip); | |
215 | fprintf(stdout, | |
216 | _("%.1f%s data found; %.1f%s realtime data found; %.*f%s inodes found.\n"), | |
217 | d, du, r, ru, ip, i, iu); | |
218 | } else { | |
219 | d = auto_space_units(used_data, &du); | |
220 | i = auto_units(used_files, &iu, &ip); | |
221 | fprintf(stdout, | |
222 | _("%.1f%s data used; %.*f%s inodes used.\n"), | |
223 | d, du, ip, i, iu); | |
224 | d = auto_space_units(stat_data, &du); | |
225 | i = auto_units(counted_inodes, &iu, &ip); | |
226 | fprintf(stdout, | |
227 | _("%.1f%s data found; %.*f%s inodes found.\n"), | |
228 | d, du, ip, i, iu); | |
229 | } | |
230 | fflush(stdout); | |
231 | } | |
232 | ||
233 | /* | |
234 | * Complain if the checked inode counts are off, which | |
235 | * implies an incomplete check. | |
236 | */ | |
237 | if (verbose || | |
238 | !within_range(ctx, counted_inodes, ctx->inodes_checked, 100, 1, 10, | |
239 | _("checked inodes"))) { | |
240 | double i1, i2; | |
241 | char *i1u, *i2u; | |
242 | int i1p, i2p; | |
243 | ||
244 | i1 = auto_units(counted_inodes, &i1u, &i1p); | |
245 | i2 = auto_units(ctx->inodes_checked, &i2u, &i2p); | |
246 | fprintf(stdout, | |
247 | _("%.*f%s inodes counted; %.*f%s inodes checked.\n"), | |
248 | i1p, i1, i1u, i2p, i2, i2u); | |
249 | fflush(stdout); | |
250 | } | |
251 | ||
252 | /* | |
253 | * Complain if the checked block counts are off, which | |
254 | * implies an incomplete check. | |
255 | */ | |
256 | if (ctx->bytes_checked && | |
257 | (verbose || | |
258 | !within_range(ctx, used_data + used_rt, | |
259 | ctx->bytes_checked, absdiff, 1, 10, | |
260 | _("verified blocks")))) { | |
261 | double b1, b2; | |
262 | char *b1u, *b2u; | |
263 | ||
264 | b1 = auto_space_units(used_data + used_rt, &b1u); | |
265 | b2 = auto_space_units(ctx->bytes_checked, &b2u); | |
266 | fprintf(stdout, | |
267 | _("%.1f%s data counted; %.1f%s data verified.\n"), | |
268 | b1, b1u, b2, b2u); | |
269 | fflush(stdout); | |
270 | } | |
271 | ||
272 | moveon = true; | |
273 | ||
274 | out: | |
275 | return moveon; | |
276 | out_free: | |
277 | ptvar_free(ptvar); | |
278 | return moveon; | |
279 | } |