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