| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2017-2023 Oracle. All Rights Reserved. |
| 4 |
* Author: Darrick J. Wong <[email protected]> |
| 5 |
*/ |
| 6 |
#include "xfs_platform.h" |
| 7 |
#include "xfs_fs.h" |
| 8 |
#include "xfs_shared.h" |
| 9 |
#include "xfs_format.h" |
| 10 |
#include "xfs_trans_resv.h" |
| 11 |
#include "xfs_mount.h" |
| 12 |
#include "xfs_btree.h" |
| 13 |
#include "xfs_inode.h" |
| 14 |
#include "xfs_log_format.h" |
| 15 |
#include "xfs_trans.h" |
| 16 |
#include "xfs_rtbitmap.h" |
| 17 |
#include "xfs_bit.h" |
| 18 |
#include "xfs_bmap.h" |
| 19 |
#include "xfs_sb.h" |
| 20 |
#include "xfs_exchmaps.h" |
| 21 |
#include "xfs_rtgroup.h" |
| 22 |
#include "scrub/scrub.h" |
| 23 |
#include "scrub/common.h" |
| 24 |
#include "scrub/trace.h" |
| 25 |
#include "scrub/xfile.h" |
| 26 |
#include "scrub/repair.h" |
| 27 |
#include "scrub/tempexch.h" |
| 28 |
#include "scrub/rtsummary.h" |
| 29 |
|
| 30 |
/* |
| 31 |
* Realtime Summary |
| 32 |
* ================ |
| 33 |
* |
| 34 |
* We check the realtime summary by scanning the realtime bitmap file to create |
| 35 |
* a new summary file incore, and then we compare the computed version against |
| 36 |
* the ondisk version. We use the 'xfile' functionality to store this |
| 37 |
* (potentially large) amount of data in pageable memory. |
| 38 |
*/ |
| 39 |
|
| 40 |
/* Set us up to check the rtsummary file. */ |
| 41 |
int |
| 42 |
xchk_setup_rtsummary( |
| 43 |
struct xfs_scrub *sc) |
| 44 |
{ |
| 45 |
struct xfs_mount *mp = sc->mp; |
| 46 |
struct xchk_rtsummary *rts; |
| 47 |
int error; |
| 48 |
|
| 49 |
if (xchk_need_intent_drain(sc)) |
| 50 |
xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN); |
| 51 |
|
| 52 |
rts = kvzalloc_flex(*rts, words, mp->m_blockwsize, XCHK_GFP_FLAGS); |
| 53 |
if (!rts) |
| 54 |
return -ENOMEM; |
| 55 |
sc->buf = rts; |
| 56 |
|
| 57 |
error = xchk_rtgroup_init(sc, sc->sm->sm_agno, &sc->sr); |
| 58 |
if (error) |
| 59 |
return error; |
| 60 |
|
| 61 |
if (xchk_could_repair(sc)) { |
| 62 |
error = xrep_setup_rtsummary(sc, rts); |
| 63 |
if (error) |
| 64 |
return error; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* Create an xfile to construct a new rtsummary file. The xfile allows |
| 69 |
* us to avoid pinning kernel memory for this purpose. |
| 70 |
*/ |
| 71 |
error = xfile_create("realtime summary file", |
| 72 |
XFS_FSB_TO_B(mp, mp->m_rsumblocks), &sc->xfile); |
| 73 |
if (error) |
| 74 |
return error; |
| 75 |
|
| 76 |
error = xchk_trans_alloc(sc, rts->resblks); |
| 77 |
if (error) |
| 78 |
return error; |
| 79 |
|
| 80 |
error = xchk_install_live_inode(sc, rtg_summary(sc->sr.rtg)); |
| 81 |
if (error) |
| 82 |
return error; |
| 83 |
|
| 84 |
error = xchk_ino_dqattach(sc); |
| 85 |
if (error) |
| 86 |
return error; |
| 87 |
|
| 88 |
error = xchk_rtgroup_lock(sc, &sc->sr, XFS_RTGLOCK_BITMAP); |
| 89 |
if (error) |
| 90 |
return error; |
| 91 |
|
| 92 |
/* |
| 93 |
* Now that we've locked the rtbitmap and rtsummary, we can't race with |
| 94 |
* growfsrt trying to expand the summary or change the size of the rt |
| 95 |
* volume. Hence it is safe to compute and check the geometry values. |
| 96 |
* |
| 97 |
* Note that there is no strict requirement for an exclusive lock on the |
| 98 |
* summary here, but to keep the locking APIs simple we lock both inodes |
| 99 |
* exclusively here. If we ever start caring about running concurrent |
| 100 |
* fsmap with scrub this could be changed. |
| 101 |
*/ |
| 102 |
if (mp->m_sb.sb_rblocks) { |
| 103 |
rts->rextents = xfs_blen_to_rtbxlen(mp, mp->m_sb.sb_rblocks); |
| 104 |
rts->rbmblocks = xfs_rtbitmap_blockcount(mp); |
| 105 |
rts->rsumblocks = |
| 106 |
xfs_rtsummary_blockcount(mp, &rts->rsumlevels); |
| 107 |
} |
| 108 |
|
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
/* Helper functions to record suminfo words in an xfile. */ |
| 113 |
|
| 114 |
static inline int |
| 115 |
xfsum_load( |
| 116 |
struct xfs_scrub *sc, |
| 117 |
xfs_rtsumoff_t sumoff, |
| 118 |
union xfs_suminfo_raw *rawinfo) |
| 119 |
{ |
| 120 |
return xfile_load(sc->xfile, rawinfo, |
| 121 |
sizeof(union xfs_suminfo_raw), |
| 122 |
sumoff << XFS_WORDLOG); |
| 123 |
} |
| 124 |
|
| 125 |
static inline int |
| 126 |
xfsum_store( |
| 127 |
struct xfs_scrub *sc, |
| 128 |
xfs_rtsumoff_t sumoff, |
| 129 |
const union xfs_suminfo_raw rawinfo) |
| 130 |
{ |
| 131 |
return xfile_store(sc->xfile, &rawinfo, |
| 132 |
sizeof(union xfs_suminfo_raw), |
| 133 |
sumoff << XFS_WORDLOG); |
| 134 |
} |
| 135 |
|
| 136 |
inline int |
| 137 |
xfsum_copyout( |
| 138 |
struct xfs_scrub *sc, |
| 139 |
xfs_rtsumoff_t sumoff, |
| 140 |
union xfs_suminfo_raw *rawinfo, |
| 141 |
unsigned int nr_words) |
| 142 |
{ |
| 143 |
return xfile_load(sc->xfile, rawinfo, nr_words << XFS_WORDLOG, |
| 144 |
sumoff << XFS_WORDLOG); |
| 145 |
} |
| 146 |
|
| 147 |
static inline xfs_suminfo_t |
| 148 |
xchk_rtsum_inc( |
| 149 |
struct xfs_mount *mp, |
| 150 |
union xfs_suminfo_raw *v) |
| 151 |
{ |
| 152 |
if (xfs_has_rtgroups(mp)) { |
| 153 |
be32_add_cpu(&v->rtg, 1); |
| 154 |
return be32_to_cpu(v->rtg); |
| 155 |
} |
| 156 |
|
| 157 |
v->old += 1; |
| 158 |
return v->old; |
| 159 |
} |
| 160 |
|
| 161 |
/* Update the summary file to reflect the free extent that we've accumulated. */ |
| 162 |
STATIC int |
| 163 |
xchk_rtsum_record_free( |
| 164 |
struct xfs_rtgroup *rtg, |
| 165 |
struct xfs_trans *tp, |
| 166 |
const struct xfs_rtalloc_rec *rec, |
| 167 |
void *priv) |
| 168 |
{ |
| 169 |
struct xfs_mount *mp = rtg_mount(rtg); |
| 170 |
struct xfs_scrub *sc = priv; |
| 171 |
xfs_fileoff_t rbmoff; |
| 172 |
xfs_rtblock_t rtbno; |
| 173 |
xfs_filblks_t rtlen; |
| 174 |
xfs_rtsumoff_t offs; |
| 175 |
unsigned int lenlog; |
| 176 |
union xfs_suminfo_raw v; |
| 177 |
xfs_suminfo_t value; |
| 178 |
int error = 0; |
| 179 |
|
| 180 |
if (xchk_should_terminate(sc, &error)) |
| 181 |
return error; |
| 182 |
|
| 183 |
/* Compute the relevant location in the rtsum file. */ |
| 184 |
rbmoff = xfs_rtx_to_rbmblock(mp, rec->ar_startext); |
| 185 |
lenlog = xfs_highbit64(rec->ar_extcount); |
| 186 |
offs = xfs_rtsumoffs(mp, lenlog, rbmoff); |
| 187 |
|
| 188 |
rtbno = xfs_rtx_to_rtb(rtg, rec->ar_startext); |
| 189 |
rtlen = xfs_rtxlen_to_extlen(mp, rec->ar_extcount); |
| 190 |
|
| 191 |
if (!xfs_verify_rtbext(mp, rtbno, rtlen)) { |
| 192 |
xchk_ino_xref_set_corrupt(sc, rtg_bitmap(rtg)->i_ino); |
| 193 |
return -EFSCORRUPTED; |
| 194 |
} |
| 195 |
|
| 196 |
/* Bump the summary count. */ |
| 197 |
error = xfsum_load(sc, offs, &v); |
| 198 |
if (error) |
| 199 |
return error; |
| 200 |
|
| 201 |
value = xchk_rtsum_inc(sc->mp, &v); |
| 202 |
trace_xchk_rtsum_record_free(mp, rec->ar_startext, rec->ar_extcount, |
| 203 |
lenlog, offs, value); |
| 204 |
|
| 205 |
return xfsum_store(sc, offs, v); |
| 206 |
} |
| 207 |
|
| 208 |
/* Compute the realtime summary from the realtime bitmap. */ |
| 209 |
STATIC int |
| 210 |
xchk_rtsum_compute( |
| 211 |
struct xfs_scrub *sc) |
| 212 |
{ |
| 213 |
struct xfs_mount *mp = sc->mp; |
| 214 |
struct xfs_rtgroup *rtg = sc->sr.rtg; |
| 215 |
|
| 216 |
/* If the bitmap size doesn't match the computed size, bail. */ |
| 217 |
if (XFS_FSB_TO_B(mp, xfs_rtbitmap_blockcount(mp)) != |
| 218 |
rtg_bitmap(rtg)->i_disk_size) |
| 219 |
return -EFSCORRUPTED; |
| 220 |
|
| 221 |
return xfs_rtalloc_query_all(rtg, sc->tp, xchk_rtsum_record_free, sc); |
| 222 |
} |
| 223 |
|
| 224 |
/* Compare the rtsummary file against the one we computed. */ |
| 225 |
STATIC int |
| 226 |
xchk_rtsum_compare( |
| 227 |
struct xfs_scrub *sc) |
| 228 |
{ |
| 229 |
struct xfs_bmbt_irec map; |
| 230 |
struct xfs_iext_cursor icur; |
| 231 |
|
| 232 |
struct xfs_mount *mp = sc->mp; |
| 233 |
struct xfs_inode *ip = sc->ip; |
| 234 |
struct xchk_rtsummary *rts = sc->buf; |
| 235 |
xfs_fileoff_t off = 0; |
| 236 |
xfs_fileoff_t endoff; |
| 237 |
xfs_rtsumoff_t sumoff = 0; |
| 238 |
int error = 0; |
| 239 |
|
| 240 |
rts->args.mp = mp; |
| 241 |
rts->args.tp = sc->tp; |
| 242 |
rts->args.rtg = sc->sr.rtg; |
| 243 |
|
| 244 |
/* Mappings may not cross or lie beyond EOF. */ |
| 245 |
endoff = XFS_B_TO_FSB(mp, ip->i_disk_size); |
| 246 |
if (xfs_iext_lookup_extent(ip, &ip->i_df, endoff, &icur, &map)) { |
| 247 |
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, endoff); |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
while (off < endoff) { |
| 252 |
int nmap = 1; |
| 253 |
|
| 254 |
if (xchk_should_terminate(sc, &error)) |
| 255 |
return error; |
| 256 |
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 257 |
return 0; |
| 258 |
|
| 259 |
/* Make sure we have a written extent. */ |
| 260 |
error = xfs_bmapi_read(ip, off, endoff - off, &map, &nmap, |
| 261 |
XFS_DATA_FORK); |
| 262 |
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error)) |
| 263 |
return error; |
| 264 |
|
| 265 |
if (nmap != 1 || !xfs_bmap_is_written_extent(&map)) { |
| 266 |
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
off += map.br_blockcount; |
| 271 |
} |
| 272 |
|
| 273 |
for (off = 0; off < endoff; off++) { |
| 274 |
union xfs_suminfo_raw *ondisk_info; |
| 275 |
|
| 276 |
/* Read a block's worth of ondisk rtsummary file. */ |
| 277 |
error = xfs_rtsummary_read_buf(&rts->args, off); |
| 278 |
if (!xchk_fblock_process_error(sc, XFS_DATA_FORK, off, &error)) |
| 279 |
return error; |
| 280 |
|
| 281 |
/* Read a block's worth of computed rtsummary file. */ |
| 282 |
error = xfsum_copyout(sc, sumoff, rts->words, mp->m_blockwsize); |
| 283 |
if (error) { |
| 284 |
xfs_rtbuf_cache_relse(&rts->args); |
| 285 |
return error; |
| 286 |
} |
| 287 |
|
| 288 |
ondisk_info = xfs_rsumblock_infoptr(&rts->args, 0); |
| 289 |
if (memcmp(ondisk_info, rts->words, |
| 290 |
mp->m_blockwsize << XFS_WORDLOG) != 0) { |
| 291 |
xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, off); |
| 292 |
xfs_rtbuf_cache_relse(&rts->args); |
| 293 |
return error; |
| 294 |
} |
| 295 |
|
| 296 |
xfs_rtbuf_cache_relse(&rts->args); |
| 297 |
sumoff += mp->m_blockwsize; |
| 298 |
} |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
/* Scrub the realtime summary. */ |
| 304 |
int |
| 305 |
xchk_rtsummary( |
| 306 |
struct xfs_scrub *sc) |
| 307 |
{ |
| 308 |
struct xfs_mount *mp = sc->mp; |
| 309 |
struct xfs_rtgroup *rtg = sc->sr.rtg; |
| 310 |
struct xfs_inode *rbmip = rtg_bitmap(rtg); |
| 311 |
struct xfs_inode *rsumip = rtg_summary(rtg); |
| 312 |
struct xchk_rtsummary *rts = sc->buf; |
| 313 |
int error; |
| 314 |
|
| 315 |
/* Is sb_rextents correct? */ |
| 316 |
if (mp->m_sb.sb_rextents != rts->rextents) { |
| 317 |
xchk_ino_set_corrupt(sc, rbmip->i_ino); |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
/* Is m_rsumlevels correct? */ |
| 322 |
if (mp->m_rsumlevels != rts->rsumlevels) { |
| 323 |
xchk_ino_set_corrupt(sc, rsumip->i_ino); |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
/* Is m_rsumsize correct? */ |
| 328 |
if (mp->m_rsumblocks != rts->rsumblocks) { |
| 329 |
xchk_ino_set_corrupt(sc, rsumip->i_ino); |
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
/* The summary file length must be aligned to an fsblock. */ |
| 334 |
if (rsumip->i_disk_size & mp->m_blockmask) { |
| 335 |
xchk_ino_set_corrupt(sc, rsumip->i_ino); |
| 336 |
return 0; |
| 337 |
} |
| 338 |
|
| 339 |
/* |
| 340 |
* Is the summary file itself large enough to handle the rt volume? |
| 341 |
* growfsrt expands the summary file before updating sb_rextents, so |
| 342 |
* the file can be larger than rsumsize. |
| 343 |
*/ |
| 344 |
if (rsumip->i_disk_size < XFS_FSB_TO_B(mp, rts->rsumblocks)) { |
| 345 |
xchk_ino_set_corrupt(sc, rsumip->i_ino); |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
/* Invoke the fork scrubber. */ |
| 350 |
error = xchk_metadata_inode_forks(sc); |
| 351 |
if (error || (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) |
| 352 |
return error; |
| 353 |
|
| 354 |
/* Construct the new summary file from the rtbitmap. */ |
| 355 |
error = xchk_rtsum_compute(sc); |
| 356 |
if (error == -EFSCORRUPTED) { |
| 357 |
/* |
| 358 |
* EFSCORRUPTED means the rtbitmap is corrupt, which is an xref |
| 359 |
* error since we're checking the summary file. |
| 360 |
*/ |
| 361 |
xchk_ino_set_corrupt(sc, rbmip->i_ino); |
| 362 |
return 0; |
| 363 |
} |
| 364 |
if (error) |
| 365 |
return error; |
| 366 |
|
| 367 |
/* Does the computed summary file match the actual rtsummary file? */ |
| 368 |
return xchk_rtsum_compare(sc); |
| 369 |
} |