]> Git Repo - linux.git/blame - fs/xfs/scrub/bmap.c
Merge tag 'xfs-5.15-merge-6' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux.git] / fs / xfs / scrub / bmap.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0+
99d9d8d0
DW
2/*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
99d9d8d0 4 * Author: Darrick J. Wong <[email protected]>
99d9d8d0
DW
5 */
6#include "xfs.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"
99d9d8d0
DW
12#include "xfs_btree.h"
13#include "xfs_bit.h"
14#include "xfs_log_format.h"
15#include "xfs_trans.h"
99d9d8d0 16#include "xfs_inode.h"
99d9d8d0 17#include "xfs_alloc.h"
99d9d8d0 18#include "xfs_bmap.h"
99d9d8d0
DW
19#include "xfs_bmap_btree.h"
20#include "xfs_rmap.h"
5e777b62 21#include "xfs_rmap_btree.h"
99d9d8d0
DW
22#include "scrub/scrub.h"
23#include "scrub/common.h"
24#include "scrub/btree.h"
934933c3 25#include "xfs_ag.h"
99d9d8d0
DW
26
27/* Set us up with an inode's bmap. */
28int
c517b3aa 29xchk_setup_inode_bmap(
026f57eb 30 struct xfs_scrub *sc)
99d9d8d0 31{
032d91f9 32 int error;
99d9d8d0 33
026f57eb 34 error = xchk_get_inode(sc);
99d9d8d0
DW
35 if (error)
36 goto out;
37
38 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
39 xfs_ilock(sc->ip, sc->ilock_flags);
40
41 /*
42 * We don't want any ephemeral data fork updates sitting around
43 * while we inspect block mappings, so wait for directio to finish
44 * and flush dirty data if we have delalloc reservations.
45 */
46 if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
47 sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
eb0efe50
DW
48 struct address_space *mapping = VFS_I(sc->ip)->i_mapping;
49
99d9d8d0 50 inode_dio_wait(VFS_I(sc->ip));
eb0efe50
DW
51
52 /*
53 * Try to flush all incore state to disk before we examine the
54 * space mappings for the data fork. Leave accumulated errors
55 * in the mapping for the writer threads to consume.
56 *
57 * On ENOSPC or EIO writeback errors, we continue into the
58 * extent mapping checks because write failures do not
59 * necessarily imply anything about the correctness of the file
60 * metadata. The metadata and the file data could be on
61 * completely separate devices; a media failure might only
62 * affect a subset of the disk, etc. We can handle delalloc
63 * extents in the scrubber, so leaving them in memory is fine.
64 */
65 error = filemap_fdatawrite(mapping);
66 if (!error)
67 error = filemap_fdatawait_keep_errors(mapping);
68 if (error && (error != -ENOSPC && error != -EIO))
99d9d8d0
DW
69 goto out;
70 }
71
72 /* Got the inode, lock it and we're ready to go. */
c517b3aa 73 error = xchk_trans_alloc(sc, 0);
99d9d8d0
DW
74 if (error)
75 goto out;
76 sc->ilock_flags |= XFS_ILOCK_EXCL;
77 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
78
79out:
80 /* scrub teardown will unlock and release the inode */
81 return error;
82}
83
84/*
85 * Inode fork block mapping (BMBT) scrubber.
86 * More complex than the others because we have to scrub
87 * all the extents regardless of whether or not the fork
88 * is in btree format.
89 */
90
c517b3aa 91struct xchk_bmap_info {
1d8a748a 92 struct xfs_scrub *sc;
032d91f9
DW
93 xfs_fileoff_t lastoff;
94 bool is_rt;
95 bool is_shared;
519e5869 96 bool was_loaded;
032d91f9 97 int whichfork;
99d9d8d0
DW
98};
99
d852657c
DW
100/* Look for a corresponding rmap for this irec. */
101static inline bool
c517b3aa 102xchk_bmap_get_rmap(
032d91f9
DW
103 struct xchk_bmap_info *info,
104 struct xfs_bmbt_irec *irec,
105 xfs_agblock_t agbno,
106 uint64_t owner,
107 struct xfs_rmap_irec *rmap)
d852657c 108{
032d91f9
DW
109 xfs_fileoff_t offset;
110 unsigned int rflags = 0;
111 int has_rmap;
112 int error;
d852657c
DW
113
114 if (info->whichfork == XFS_ATTR_FORK)
115 rflags |= XFS_RMAP_ATTR_FORK;
5dda3897
DW
116 if (irec->br_state == XFS_EXT_UNWRITTEN)
117 rflags |= XFS_RMAP_UNWRITTEN;
d852657c
DW
118
119 /*
120 * CoW staging extents are owned (on disk) by the refcountbt, so
121 * their rmaps do not have offsets.
122 */
123 if (info->whichfork == XFS_COW_FORK)
124 offset = 0;
125 else
126 offset = irec->br_startoff;
127
128 /*
129 * If the caller thinks this could be a shared bmbt extent (IOWs,
130 * any data fork extent of a reflink inode) then we have to use the
131 * range rmap lookup to make sure we get the correct owner/offset.
132 */
133 if (info->is_shared) {
134 error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
135 owner, offset, rflags, rmap, &has_rmap);
c517b3aa 136 if (!xchk_should_check_xref(info->sc, &error,
d852657c
DW
137 &info->sc->sa.rmap_cur))
138 return false;
139 goto out;
140 }
141
142 /*
143 * Otherwise, use the (faster) regular lookup.
144 */
145 error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno, 0, owner,
146 offset, rflags, &has_rmap);
c517b3aa 147 if (!xchk_should_check_xref(info->sc, &error,
d852657c
DW
148 &info->sc->sa.rmap_cur))
149 return false;
150 if (!has_rmap)
151 goto out;
152
153 error = xfs_rmap_get_rec(info->sc->sa.rmap_cur, rmap, &has_rmap);
c517b3aa 154 if (!xchk_should_check_xref(info->sc, &error,
d852657c
DW
155 &info->sc->sa.rmap_cur))
156 return false;
157
158out:
159 if (!has_rmap)
c517b3aa 160 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
161 irec->br_startoff);
162 return has_rmap;
163}
164
165/* Make sure that we have rmapbt records for this extent. */
166STATIC void
c517b3aa 167xchk_bmap_xref_rmap(
032d91f9
DW
168 struct xchk_bmap_info *info,
169 struct xfs_bmbt_irec *irec,
170 xfs_agblock_t agbno)
d852657c 171{
032d91f9
DW
172 struct xfs_rmap_irec rmap;
173 unsigned long long rmap_end;
174 uint64_t owner;
d852657c 175
c517b3aa 176 if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
d852657c
DW
177 return;
178
179 if (info->whichfork == XFS_COW_FORK)
180 owner = XFS_RMAP_OWN_COW;
181 else
182 owner = info->sc->ip->i_ino;
183
184 /* Find the rmap record for this irec. */
c517b3aa 185 if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
d852657c
DW
186 return;
187
188 /* Check the rmap. */
189 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
190 if (rmap.rm_startblock > agbno ||
191 agbno + irec->br_blockcount > rmap_end)
c517b3aa 192 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
193 irec->br_startoff);
194
195 /*
196 * Check the logical offsets if applicable. CoW staging extents
197 * don't track logical offsets since the mappings only exist in
198 * memory.
199 */
200 if (info->whichfork != XFS_COW_FORK) {
201 rmap_end = (unsigned long long)rmap.rm_offset +
202 rmap.rm_blockcount;
203 if (rmap.rm_offset > irec->br_startoff ||
204 irec->br_startoff + irec->br_blockcount > rmap_end)
c517b3aa 205 xchk_fblock_xref_set_corrupt(info->sc,
d852657c
DW
206 info->whichfork, irec->br_startoff);
207 }
208
209 if (rmap.rm_owner != owner)
c517b3aa 210 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
211 irec->br_startoff);
212
213 /*
214 * Check for discrepancies between the unwritten flag in the irec and
215 * the rmap. Note that the (in-memory) CoW fork distinguishes between
216 * unwritten and written extents, but we don't track that in the rmap
217 * records because the blocks are owned (on-disk) by the refcountbt,
218 * which doesn't track unwritten state.
219 */
220 if (owner != XFS_RMAP_OWN_COW &&
498fe261
DW
221 !!(irec->br_state == XFS_EXT_UNWRITTEN) !=
222 !!(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
c517b3aa 223 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
224 irec->br_startoff);
225
498fe261
DW
226 if (!!(info->whichfork == XFS_ATTR_FORK) !=
227 !!(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
c517b3aa 228 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
229 irec->br_startoff);
230 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
c517b3aa 231 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
d852657c
DW
232 irec->br_startoff);
233}
234
166d7641
DW
235/* Cross-reference a single rtdev extent record. */
236STATIC void
519e5869 237xchk_bmap_rt_iextent_xref(
032d91f9 238 struct xfs_inode *ip,
519e5869 239 struct xchk_bmap_info *info,
032d91f9 240 struct xfs_bmbt_irec *irec)
166d7641 241{
c517b3aa 242 xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
46d9bfb5 243 irec->br_blockcount);
166d7641
DW
244}
245
246/* Cross-reference a single datadev extent record. */
247STATIC void
519e5869 248xchk_bmap_iextent_xref(
032d91f9 249 struct xfs_inode *ip,
519e5869 250 struct xchk_bmap_info *info,
032d91f9 251 struct xfs_bmbt_irec *irec)
166d7641 252{
032d91f9
DW
253 struct xfs_mount *mp = info->sc->mp;
254 xfs_agnumber_t agno;
255 xfs_agblock_t agbno;
256 xfs_extlen_t len;
257 int error;
52dc4b44 258
52dc4b44
DW
259 agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
260 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
261 len = irec->br_blockcount;
262
48c6615c 263 error = xchk_ag_init_existing(info->sc, agno, &info->sc->sa);
c517b3aa 264 if (!xchk_fblock_process_error(info->sc, info->whichfork,
52dc4b44 265 irec->br_startoff, &error))
61e0d0cc 266 goto out_free;
52dc4b44 267
c517b3aa
DW
268 xchk_xref_is_used_space(info->sc, agbno, len);
269 xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
270 xchk_bmap_xref_rmap(info, irec, agbno);
f6d5fc21
DW
271 switch (info->whichfork) {
272 case XFS_DATA_FORK:
273 if (xfs_is_reflink_inode(info->sc->ip))
274 break;
53004ee7 275 fallthrough;
f6d5fc21 276 case XFS_ATTR_FORK:
c517b3aa 277 xchk_xref_is_not_shared(info->sc, agbno,
f6d5fc21
DW
278 irec->br_blockcount);
279 break;
280 case XFS_COW_FORK:
c517b3aa 281 xchk_xref_is_cow_staging(info->sc, agbno,
f6d5fc21
DW
282 irec->br_blockcount);
283 break;
284 }
52dc4b44 285
61e0d0cc 286out_free:
c517b3aa 287 xchk_ag_free(info->sc, &info->sc->sa);
166d7641
DW
288}
289
f8c1d702
DW
290/*
291 * Directories and attr forks should never have blocks that can't be addressed
292 * by a xfs_dablk_t.
293 */
294STATIC void
295xchk_bmap_dirattr_extent(
296 struct xfs_inode *ip,
297 struct xchk_bmap_info *info,
298 struct xfs_bmbt_irec *irec)
299{
300 struct xfs_mount *mp = ip->i_mount;
301 xfs_fileoff_t off;
302
303 if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK)
304 return;
305
306 if (!xfs_verify_dablk(mp, irec->br_startoff))
307 xchk_fblock_set_corrupt(info->sc, info->whichfork,
308 irec->br_startoff);
309
310 off = irec->br_startoff + irec->br_blockcount - 1;
311 if (!xfs_verify_dablk(mp, off))
312 xchk_fblock_set_corrupt(info->sc, info->whichfork, off);
313}
314
99d9d8d0
DW
315/* Scrub a single extent record. */
316STATIC int
519e5869 317xchk_bmap_iextent(
032d91f9 318 struct xfs_inode *ip,
032d91f9
DW
319 struct xchk_bmap_info *info,
320 struct xfs_bmbt_irec *irec)
99d9d8d0 321{
032d91f9 322 struct xfs_mount *mp = info->sc->mp;
032d91f9 323 int error = 0;
99d9d8d0 324
99d9d8d0
DW
325 /*
326 * Check for out-of-order extents. This record could have come
327 * from the incore list, for which there is no ordering check.
328 */
329 if (irec->br_startoff < info->lastoff)
c517b3aa 330 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
331 irec->br_startoff);
332
33005fd0
DW
333 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
334 xchk_fblock_set_corrupt(info->sc, info->whichfork,
335 irec->br_startoff);
336
f8c1d702
DW
337 xchk_bmap_dirattr_extent(ip, info, irec);
338
99d9d8d0
DW
339 /* There should never be a "hole" extent in either extent list. */
340 if (irec->br_startblock == HOLESTARTBLOCK)
c517b3aa 341 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
342 irec->br_startoff);
343
344 /*
345 * Check for delalloc extents. We never iterate the ones in the
346 * in-core extent scan, and we should never see these in the bmbt.
347 */
348 if (isnullstartblock(irec->br_startblock))
c517b3aa 349 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
350 irec->br_startoff);
351
352 /* Make sure the extent points to a valid place. */
a5f460b1 353 if (irec->br_blockcount > MAXEXTLEN)
c517b3aa 354 xchk_fblock_set_corrupt(info->sc, info->whichfork,
a5f460b1 355 irec->br_startoff);
99d9d8d0 356 if (info->is_rt &&
18695ad4 357 !xfs_verify_rtext(mp, irec->br_startblock, irec->br_blockcount))
c517b3aa 358 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
359 irec->br_startoff);
360 if (!info->is_rt &&
67457eb0 361 !xfs_verify_fsbext(mp, irec->br_startblock, irec->br_blockcount))
c517b3aa 362 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
363 irec->br_startoff);
364
365 /* We don't allow unwritten extents on attr forks. */
366 if (irec->br_state == XFS_EXT_UNWRITTEN &&
367 info->whichfork == XFS_ATTR_FORK)
c517b3aa 368 xchk_fblock_set_corrupt(info->sc, info->whichfork,
99d9d8d0
DW
369 irec->br_startoff);
370
519e5869
DW
371 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
372 return 0;
373
166d7641 374 if (info->is_rt)
519e5869 375 xchk_bmap_rt_iextent_xref(ip, info, irec);
166d7641 376 else
519e5869 377 xchk_bmap_iextent_xref(ip, info, irec);
166d7641 378
99d9d8d0
DW
379 info->lastoff = irec->br_startoff + irec->br_blockcount;
380 return error;
381}
382
383/* Scrub a bmbt record. */
384STATIC int
c517b3aa 385xchk_bmapbt_rec(
032d91f9 386 struct xchk_btree *bs,
22ece4e8 387 const union xfs_btree_rec *rec)
99d9d8d0 388{
032d91f9 389 struct xfs_bmbt_irec irec;
519e5869
DW
390 struct xfs_bmbt_irec iext_irec;
391 struct xfs_iext_cursor icur;
032d91f9 392 struct xchk_bmap_info *info = bs->private;
92219c29 393 struct xfs_inode *ip = bs->cur->bc_ino.ip;
032d91f9
DW
394 struct xfs_buf *bp = NULL;
395 struct xfs_btree_block *block;
519e5869 396 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, info->whichfork);
032d91f9
DW
397 uint64_t owner;
398 int i;
99d9d8d0
DW
399
400 /*
401 * Check the owners of the btree blocks up to the level below
402 * the root since the verifiers don't do that.
403 */
38c26bfd 404 if (xfs_has_crc(bs->cur->bc_mp) &&
99d9d8d0
DW
405 bs->cur->bc_ptrs[0] == 1) {
406 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
407 block = xfs_btree_get_block(bs->cur, i, &bp);
408 owner = be64_to_cpu(block->bb_u.l.bb_owner);
409 if (owner != ip->i_ino)
c517b3aa 410 xchk_fblock_set_corrupt(bs->sc,
99d9d8d0
DW
411 info->whichfork, 0);
412 }
413 }
414
519e5869
DW
415 /*
416 * Check that the incore extent tree contains an extent that matches
417 * this one exactly. We validate those cached bmaps later, so we don't
418 * need to check them here. If the incore extent tree was just loaded
419 * from disk by the scrubber, we assume that its contents match what's
420 * on disk (we still hold the ILOCK) and skip the equivalence check.
421 */
422 if (!info->was_loaded)
423 return 0;
424
6bdcf26a 425 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
519e5869
DW
426 if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur,
427 &iext_irec) ||
428 irec.br_startoff != iext_irec.br_startoff ||
429 irec.br_startblock != iext_irec.br_startblock ||
430 irec.br_blockcount != iext_irec.br_blockcount ||
431 irec.br_state != iext_irec.br_state)
432 xchk_fblock_set_corrupt(bs->sc, info->whichfork,
433 irec.br_startoff);
434 return 0;
99d9d8d0
DW
435}
436
437/* Scan the btree records. */
438STATIC int
c517b3aa 439xchk_bmap_btree(
1d8a748a 440 struct xfs_scrub *sc,
032d91f9
DW
441 int whichfork,
442 struct xchk_bmap_info *info)
99d9d8d0 443{
032d91f9 444 struct xfs_owner_info oinfo;
519e5869 445 struct xfs_ifork *ifp = XFS_IFORK_PTR(sc->ip, whichfork);
032d91f9
DW
446 struct xfs_mount *mp = sc->mp;
447 struct xfs_inode *ip = sc->ip;
448 struct xfs_btree_cur *cur;
449 int error;
99d9d8d0 450
519e5869 451 /* Load the incore bmap cache if it's not loaded. */
b2197a36 452 info->was_loaded = !xfs_need_iread_extents(ifp);
862a804a
CH
453
454 error = xfs_iread_extents(sc->tp, ip, whichfork);
455 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
456 goto out;
519e5869
DW
457
458 /* Check the btree structure. */
99d9d8d0
DW
459 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
460 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
c517b3aa 461 error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
0b04b6b8 462 xfs_btree_del_cursor(cur, error);
519e5869 463out:
99d9d8d0
DW
464 return error;
465}
466
c517b3aa 467struct xchk_bmap_check_rmap_info {
1d8a748a 468 struct xfs_scrub *sc;
032d91f9
DW
469 int whichfork;
470 struct xfs_iext_cursor icur;
5e777b62
DW
471};
472
473/* Can we find bmaps that fit this rmap? */
474STATIC int
c517b3aa 475xchk_bmap_check_rmap(
5e777b62 476 struct xfs_btree_cur *cur,
159eb69d 477 const struct xfs_rmap_irec *rec,
5e777b62
DW
478 void *priv)
479{
480 struct xfs_bmbt_irec irec;
159eb69d 481 struct xfs_rmap_irec check_rec;
c517b3aa 482 struct xchk_bmap_check_rmap_info *sbcri = priv;
5e777b62 483 struct xfs_ifork *ifp;
032d91f9 484 struct xfs_scrub *sc = sbcri->sc;
5e777b62
DW
485 bool have_map;
486
487 /* Is this even the right fork? */
488 if (rec->rm_owner != sc->ip->i_ino)
489 return 0;
490 if ((sbcri->whichfork == XFS_ATTR_FORK) ^
491 !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
492 return 0;
493 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
494 return 0;
495
496 /* Now look up the bmbt record. */
497 ifp = XFS_IFORK_PTR(sc->ip, sbcri->whichfork);
498 if (!ifp) {
c517b3aa 499 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
5e777b62
DW
500 rec->rm_offset);
501 goto out;
502 }
503 have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
504 &sbcri->icur, &irec);
505 if (!have_map)
c517b3aa 506 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
5e777b62
DW
507 rec->rm_offset);
508 /*
509 * bmap extent record lengths are constrained to 2^21 blocks in length
510 * because of space constraints in the on-disk metadata structure.
511 * However, rmap extent record lengths are constrained only by AG
512 * length, so we have to loop through the bmbt to make sure that the
513 * entire rmap is covered by bmbt records.
514 */
159eb69d 515 check_rec = *rec;
5e777b62 516 while (have_map) {
159eb69d 517 if (irec.br_startoff != check_rec.rm_offset)
c517b3aa 518 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
159eb69d 519 check_rec.rm_offset);
5e777b62 520 if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
159eb69d
DW
521 cur->bc_ag.pag->pag_agno,
522 check_rec.rm_startblock))
c517b3aa 523 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
159eb69d
DW
524 check_rec.rm_offset);
525 if (irec.br_blockcount > check_rec.rm_blockcount)
c517b3aa 526 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
159eb69d 527 check_rec.rm_offset);
5e777b62
DW
528 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
529 break;
159eb69d
DW
530 check_rec.rm_startblock += irec.br_blockcount;
531 check_rec.rm_offset += irec.br_blockcount;
532 check_rec.rm_blockcount -= irec.br_blockcount;
533 if (check_rec.rm_blockcount == 0)
5e777b62
DW
534 break;
535 have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
536 if (!have_map)
c517b3aa 537 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
159eb69d 538 check_rec.rm_offset);
5e777b62
DW
539 }
540
541out:
542 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
e7ee96df 543 return -ECANCELED;
5e777b62
DW
544 return 0;
545}
546
547/* Make sure each rmap has a corresponding bmbt entry. */
548STATIC int
c517b3aa 549xchk_bmap_check_ag_rmaps(
032d91f9 550 struct xfs_scrub *sc,
5e777b62 551 int whichfork,
a81a0621 552 struct xfs_perag *pag)
5e777b62 553{
c517b3aa 554 struct xchk_bmap_check_rmap_info sbcri;
5e777b62
DW
555 struct xfs_btree_cur *cur;
556 struct xfs_buf *agf;
557 int error;
558
a81a0621 559 error = xfs_alloc_read_agf(sc->mp, sc->tp, pag->pag_agno, 0, &agf);
5e777b62
DW
560 if (error)
561 return error;
562
a81a0621 563 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, pag);
5e777b62
DW
564
565 sbcri.sc = sc;
566 sbcri.whichfork = whichfork;
c517b3aa 567 error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
e7ee96df 568 if (error == -ECANCELED)
5e777b62
DW
569 error = 0;
570
0b04b6b8 571 xfs_btree_del_cursor(cur, error);
5e777b62
DW
572 xfs_trans_brelse(sc->tp, agf);
573 return error;
574}
575
576/* Make sure each rmap has a corresponding bmbt entry. */
577STATIC int
c517b3aa 578xchk_bmap_check_rmaps(
1d8a748a 579 struct xfs_scrub *sc,
032d91f9 580 int whichfork)
5e777b62 581{
daf83964 582 struct xfs_ifork *ifp = XFS_IFORK_PTR(sc->ip, whichfork);
934933c3 583 struct xfs_perag *pag;
032d91f9 584 xfs_agnumber_t agno;
5fd68bdb 585 bool zero_size;
032d91f9 586 int error;
5e777b62 587
38c26bfd 588 if (!xfs_has_rmapbt(sc->mp) ||
5e777b62
DW
589 whichfork == XFS_COW_FORK ||
590 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
591 return 0;
592
593 /* Don't support realtime rmap checks yet. */
594 if (XFS_IS_REALTIME_INODE(sc->ip) && whichfork == XFS_DATA_FORK)
595 return 0;
596
5fd68bdb
DW
597 ASSERT(XFS_IFORK_PTR(sc->ip, whichfork) != NULL);
598
5e777b62
DW
599 /*
600 * Only do this for complex maps that are in btree format, or for
601 * situations where we would seem to have a size but zero extents.
602 * The inode repair code can zap broken iforks, which means we have
603 * to flag this bmap as corrupt if there are rmaps that need to be
604 * reattached.
605 */
f7e67b20 606
5fd68bdb
DW
607 if (whichfork == XFS_DATA_FORK)
608 zero_size = i_size_read(VFS_I(sc->ip)) == 0;
609 else
610 zero_size = false;
611
f7e67b20 612 if (ifp->if_format != XFS_DINODE_FMT_BTREE &&
daf83964 613 (zero_size || ifp->if_nextents > 0))
5e777b62
DW
614 return 0;
615
934933c3 616 for_each_perag(sc->mp, agno, pag) {
a81a0621 617 error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag);
5e777b62 618 if (error)
934933c3 619 break;
5e777b62
DW
620 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
621 break;
622 }
934933c3
DC
623 if (pag)
624 xfs_perag_put(pag);
625 return error;
5e777b62
DW
626}
627
99d9d8d0
DW
628/*
629 * Scrub an inode fork's block mappings.
630 *
631 * First we scan every record in every btree block, if applicable.
632 * Then we unconditionally scan the incore extent cache.
633 */
634STATIC int
c517b3aa 635xchk_bmap(
1d8a748a 636 struct xfs_scrub *sc,
032d91f9 637 int whichfork)
99d9d8d0 638{
032d91f9
DW
639 struct xfs_bmbt_irec irec;
640 struct xchk_bmap_info info = { NULL };
641 struct xfs_mount *mp = sc->mp;
642 struct xfs_inode *ip = sc->ip;
5fd68bdb 643 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
032d91f9
DW
644 xfs_fileoff_t endoff;
645 struct xfs_iext_cursor icur;
646 int error = 0;
99d9d8d0 647
5fd68bdb
DW
648 /* Non-existent forks can be ignored. */
649 if (!ifp)
650 goto out;
99d9d8d0
DW
651
652 info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
653 info.whichfork = whichfork;
654 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
655 info.sc = sc;
656
657 switch (whichfork) {
658 case XFS_COW_FORK:
99d9d8d0
DW
659 /* No CoW forks on non-reflink inodes/filesystems. */
660 if (!xfs_is_reflink_inode(ip)) {
c517b3aa 661 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
99d9d8d0
DW
662 goto out;
663 }
664 break;
665 case XFS_ATTR_FORK:
ebd9027d 666 if (!xfs_has_attr(mp) && !xfs_has_attr2(mp))
c517b3aa 667 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
99d9d8d0
DW
668 break;
669 default:
670 ASSERT(whichfork == XFS_DATA_FORK);
671 break;
672 }
673
674 /* Check the fork values */
f7e67b20 675 switch (ifp->if_format) {
99d9d8d0
DW
676 case XFS_DINODE_FMT_UUID:
677 case XFS_DINODE_FMT_DEV:
678 case XFS_DINODE_FMT_LOCAL:
679 /* No mappings to check. */
680 goto out;
681 case XFS_DINODE_FMT_EXTENTS:
99d9d8d0
DW
682 break;
683 case XFS_DINODE_FMT_BTREE:
684 if (whichfork == XFS_COW_FORK) {
c517b3aa 685 xchk_fblock_set_corrupt(sc, whichfork, 0);
99d9d8d0
DW
686 goto out;
687 }
688
c517b3aa 689 error = xchk_bmap_btree(sc, whichfork, &info);
99d9d8d0
DW
690 if (error)
691 goto out;
692 break;
693 default:
c517b3aa 694 xchk_fblock_set_corrupt(sc, whichfork, 0);
99d9d8d0
DW
695 goto out;
696 }
697
698 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
699 goto out;
700
99d9d8d0
DW
701 /* Find the offset of the last extent in the mapping. */
702 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
c517b3aa 703 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
99d9d8d0
DW
704 goto out;
705
706 /* Scrub extent records. */
707 info.lastoff = 0;
708 ifp = XFS_IFORK_PTR(ip, whichfork);
2b9e9b57 709 for_each_xfs_iext(ifp, &icur, &irec) {
c517b3aa 710 if (xchk_should_terminate(sc, &error) ||
8bc763c2 711 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
519e5869 712 goto out;
99d9d8d0
DW
713 if (isnullstartblock(irec.br_startblock))
714 continue;
715 if (irec.br_startoff >= endoff) {
c517b3aa 716 xchk_fblock_set_corrupt(sc, whichfork,
99d9d8d0
DW
717 irec.br_startoff);
718 goto out;
719 }
519e5869 720 error = xchk_bmap_iextent(ip, &info, &irec);
99d9d8d0
DW
721 if (error)
722 goto out;
723 }
724
c517b3aa
DW
725 error = xchk_bmap_check_rmaps(sc, whichfork);
726 if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
5e777b62 727 goto out;
99d9d8d0
DW
728out:
729 return error;
730}
731
732/* Scrub an inode's data fork. */
733int
c517b3aa 734xchk_bmap_data(
1d8a748a 735 struct xfs_scrub *sc)
99d9d8d0 736{
c517b3aa 737 return xchk_bmap(sc, XFS_DATA_FORK);
99d9d8d0
DW
738}
739
740/* Scrub an inode's attr fork. */
741int
c517b3aa 742xchk_bmap_attr(
1d8a748a 743 struct xfs_scrub *sc)
99d9d8d0 744{
c517b3aa 745 return xchk_bmap(sc, XFS_ATTR_FORK);
99d9d8d0
DW
746}
747
748/* Scrub an inode's CoW fork. */
749int
c517b3aa 750xchk_bmap_cow(
1d8a748a 751 struct xfs_scrub *sc)
99d9d8d0
DW
752{
753 if (!xfs_is_reflink_inode(sc->ip))
754 return -ENOENT;
755
c517b3aa 756 return xchk_bmap(sc, XFS_COW_FORK);
99d9d8d0 757}
This page took 0.403328 seconds and 4 git commands to generate.