blob: 933e0b8be34fd2efedd14a6358816e7ef98356e2 [file] [log] [blame]
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -07001/*
2 * Copyright (C) 2017 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_trans_resv.h"
25#include "xfs_mount.h"
26#include "xfs_defer.h"
27#include "xfs_btree.h"
28#include "xfs_bit.h"
29#include "xfs_log_format.h"
30#include "xfs_trans.h"
31#include "xfs_sb.h"
32#include "xfs_inode.h"
33#include "xfs_inode_fork.h"
34#include "xfs_alloc.h"
35#include "xfs_rtalloc.h"
36#include "xfs_bmap.h"
37#include "xfs_bmap_util.h"
38#include "xfs_bmap_btree.h"
39#include "xfs_rmap.h"
40#include "scrub/xfs_scrub.h"
41#include "scrub/scrub.h"
42#include "scrub/common.h"
43#include "scrub/btree.h"
44#include "scrub/trace.h"
45
46/* Set us up with an inode's bmap. */
47int
48xfs_scrub_setup_inode_bmap(
49 struct xfs_scrub_context *sc,
50 struct xfs_inode *ip)
51{
52 struct xfs_mount *mp = sc->mp;
53 int error;
54
55 error = xfs_scrub_get_inode(sc, ip);
56 if (error)
57 goto out;
58
59 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
60 xfs_ilock(sc->ip, sc->ilock_flags);
61
62 /*
63 * We don't want any ephemeral data fork updates sitting around
64 * while we inspect block mappings, so wait for directio to finish
65 * and flush dirty data if we have delalloc reservations.
66 */
67 if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
68 sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
69 inode_dio_wait(VFS_I(sc->ip));
70 error = filemap_write_and_wait(VFS_I(sc->ip)->i_mapping);
71 if (error)
72 goto out;
73 }
74
75 /* Got the inode, lock it and we're ready to go. */
76 error = xfs_scrub_trans_alloc(sc->sm, mp, &sc->tp);
77 if (error)
78 goto out;
79 sc->ilock_flags |= XFS_ILOCK_EXCL;
80 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
81
82out:
83 /* scrub teardown will unlock and release the inode */
84 return error;
85}
86
87/*
88 * Inode fork block mapping (BMBT) scrubber.
89 * More complex than the others because we have to scrub
90 * all the extents regardless of whether or not the fork
91 * is in btree format.
92 */
93
94struct xfs_scrub_bmap_info {
95 struct xfs_scrub_context *sc;
96 xfs_fileoff_t lastoff;
97 bool is_rt;
98 bool is_shared;
99 int whichfork;
100};
101
Darrick J. Wongd8526572018-01-16 18:53:08 -0800102/* Look for a corresponding rmap for this irec. */
103static inline bool
104xfs_scrub_bmap_get_rmap(
105 struct xfs_scrub_bmap_info *info,
106 struct xfs_bmbt_irec *irec,
107 xfs_agblock_t agbno,
108 uint64_t owner,
109 struct xfs_rmap_irec *rmap)
110{
111 xfs_fileoff_t offset;
112 unsigned int rflags = 0;
113 int has_rmap;
114 int error;
115
116 if (info->whichfork == XFS_ATTR_FORK)
117 rflags |= XFS_RMAP_ATTR_FORK;
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);
136 if (!xfs_scrub_should_check_xref(info->sc, &error,
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);
147 if (!xfs_scrub_should_check_xref(info->sc, &error,
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);
154 if (!xfs_scrub_should_check_xref(info->sc, &error,
155 &info->sc->sa.rmap_cur))
156 return false;
157
158out:
159 if (!has_rmap)
160 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
161 irec->br_startoff);
162 return has_rmap;
163}
164
165/* Make sure that we have rmapbt records for this extent. */
166STATIC void
167xfs_scrub_bmap_xref_rmap(
168 struct xfs_scrub_bmap_info *info,
169 struct xfs_bmbt_irec *irec,
170 xfs_agblock_t agbno)
171{
172 struct xfs_rmap_irec rmap;
173 unsigned long long rmap_end;
174 uint64_t owner;
175
176 if (!info->sc->sa.rmap_cur)
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. */
185 if (!xfs_scrub_bmap_get_rmap(info, irec, agbno, owner, &rmap))
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)
192 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
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)
205 xfs_scrub_fblock_xref_set_corrupt(info->sc,
206 info->whichfork, irec->br_startoff);
207 }
208
209 if (rmap.rm_owner != owner)
210 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
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 &&
221 irec->br_state == XFS_EXT_UNWRITTEN &&
222 !(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
223 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
224 irec->br_startoff);
225
226 if (info->whichfork == XFS_ATTR_FORK &&
227 !(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
228 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
229 irec->br_startoff);
230 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
231 xfs_scrub_fblock_xref_set_corrupt(info->sc, info->whichfork,
232 irec->br_startoff);
233}
234
Darrick J. Wong166d7642018-01-16 18:53:05 -0800235/* Cross-reference a single rtdev extent record. */
236STATIC void
237xfs_scrub_bmap_rt_extent_xref(
238 struct xfs_scrub_bmap_info *info,
239 struct xfs_inode *ip,
240 struct xfs_btree_cur *cur,
241 struct xfs_bmbt_irec *irec)
242{
243 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
244 return;
245}
246
247/* Cross-reference a single datadev extent record. */
248STATIC void
249xfs_scrub_bmap_extent_xref(
250 struct xfs_scrub_bmap_info *info,
251 struct xfs_inode *ip,
252 struct xfs_btree_cur *cur,
253 struct xfs_bmbt_irec *irec)
254{
Darrick J. Wong52dc4b42018-01-16 18:53:06 -0800255 struct xfs_mount *mp = info->sc->mp;
256 xfs_agnumber_t agno;
257 xfs_agblock_t agbno;
258 xfs_extlen_t len;
259 int error;
260
Darrick J. Wong166d7642018-01-16 18:53:05 -0800261 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
262 return;
Darrick J. Wong52dc4b42018-01-16 18:53:06 -0800263
264 agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
265 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
266 len = irec->br_blockcount;
267
268 error = xfs_scrub_ag_init(info->sc, agno, &info->sc->sa);
269 if (!xfs_scrub_fblock_process_error(info->sc, info->whichfork,
270 irec->br_startoff, &error))
271 return;
272
273 xfs_scrub_xref_is_used_space(info->sc, agbno, len);
Darrick J. Wong2e6f2752018-01-16 18:53:07 -0800274 xfs_scrub_xref_is_not_inode_chunk(info->sc, agbno, len);
Darrick J. Wongd8526572018-01-16 18:53:08 -0800275 xfs_scrub_bmap_xref_rmap(info, irec, agbno);
Darrick J. Wong52dc4b42018-01-16 18:53:06 -0800276
277 xfs_scrub_ag_free(info->sc, &info->sc->sa);
Darrick J. Wong166d7642018-01-16 18:53:05 -0800278}
279
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700280/* Scrub a single extent record. */
281STATIC int
282xfs_scrub_bmap_extent(
283 struct xfs_inode *ip,
284 struct xfs_btree_cur *cur,
285 struct xfs_scrub_bmap_info *info,
286 struct xfs_bmbt_irec *irec)
287{
288 struct xfs_mount *mp = info->sc->mp;
289 struct xfs_buf *bp = NULL;
290 int error = 0;
291
292 if (cur)
293 xfs_btree_get_block(cur, 0, &bp);
294
295 /*
296 * Check for out-of-order extents. This record could have come
297 * from the incore list, for which there is no ordering check.
298 */
299 if (irec->br_startoff < info->lastoff)
300 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
301 irec->br_startoff);
302
303 /* There should never be a "hole" extent in either extent list. */
304 if (irec->br_startblock == HOLESTARTBLOCK)
305 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
306 irec->br_startoff);
307
308 /*
309 * Check for delalloc extents. We never iterate the ones in the
310 * in-core extent scan, and we should never see these in the bmbt.
311 */
312 if (isnullstartblock(irec->br_startblock))
313 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
314 irec->br_startoff);
315
316 /* Make sure the extent points to a valid place. */
317 if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
318 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
319 irec->br_startoff);
320 if (info->is_rt &&
321 (!xfs_verify_rtbno(mp, irec->br_startblock) ||
322 !xfs_verify_rtbno(mp, irec->br_startblock +
323 irec->br_blockcount - 1)))
324 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
325 irec->br_startoff);
326 if (!info->is_rt &&
327 (!xfs_verify_fsbno(mp, irec->br_startblock) ||
328 !xfs_verify_fsbno(mp, irec->br_startblock +
329 irec->br_blockcount - 1)))
330 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
331 irec->br_startoff);
332
333 /* We don't allow unwritten extents on attr forks. */
334 if (irec->br_state == XFS_EXT_UNWRITTEN &&
335 info->whichfork == XFS_ATTR_FORK)
336 xfs_scrub_fblock_set_corrupt(info->sc, info->whichfork,
337 irec->br_startoff);
338
Darrick J. Wong166d7642018-01-16 18:53:05 -0800339 if (info->is_rt)
340 xfs_scrub_bmap_rt_extent_xref(info, ip, cur, irec);
341 else
342 xfs_scrub_bmap_extent_xref(info, ip, cur, irec);
343
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700344 info->lastoff = irec->br_startoff + irec->br_blockcount;
345 return error;
346}
347
348/* Scrub a bmbt record. */
349STATIC int
350xfs_scrub_bmapbt_rec(
351 struct xfs_scrub_btree *bs,
352 union xfs_btree_rec *rec)
353{
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700354 struct xfs_bmbt_irec irec;
355 struct xfs_scrub_bmap_info *info = bs->private;
356 struct xfs_inode *ip = bs->cur->bc_private.b.ip;
357 struct xfs_buf *bp = NULL;
358 struct xfs_btree_block *block;
359 uint64_t owner;
360 int i;
361
362 /*
363 * Check the owners of the btree blocks up to the level below
364 * the root since the verifiers don't do that.
365 */
366 if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
367 bs->cur->bc_ptrs[0] == 1) {
368 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
369 block = xfs_btree_get_block(bs->cur, i, &bp);
370 owner = be64_to_cpu(block->bb_u.l.bb_owner);
371 if (owner != ip->i_ino)
372 xfs_scrub_fblock_set_corrupt(bs->sc,
373 info->whichfork, 0);
374 }
375 }
376
377 /* Set up the in-core record and scrub it. */
Christoph Hellwig6bdcf262017-11-03 10:34:46 -0700378 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700379 return xfs_scrub_bmap_extent(ip, bs->cur, info, &irec);
380}
381
382/* Scan the btree records. */
383STATIC int
384xfs_scrub_bmap_btree(
385 struct xfs_scrub_context *sc,
386 int whichfork,
387 struct xfs_scrub_bmap_info *info)
388{
389 struct xfs_owner_info oinfo;
390 struct xfs_mount *mp = sc->mp;
391 struct xfs_inode *ip = sc->ip;
392 struct xfs_btree_cur *cur;
393 int error;
394
395 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
396 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
397 error = xfs_scrub_btree(sc, cur, xfs_scrub_bmapbt_rec, &oinfo, info);
398 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR :
399 XFS_BTREE_NOERROR);
400 return error;
401}
402
403/*
404 * Scrub an inode fork's block mappings.
405 *
406 * First we scan every record in every btree block, if applicable.
407 * Then we unconditionally scan the incore extent cache.
408 */
409STATIC int
410xfs_scrub_bmap(
411 struct xfs_scrub_context *sc,
412 int whichfork)
413{
414 struct xfs_bmbt_irec irec;
Christoph Hellwig88aa5de2017-11-06 11:53:58 -0800415 struct xfs_scrub_bmap_info info = { NULL };
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700416 struct xfs_mount *mp = sc->mp;
417 struct xfs_inode *ip = sc->ip;
418 struct xfs_ifork *ifp;
419 xfs_fileoff_t endoff;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700420 struct xfs_iext_cursor icur;
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700421 int error = 0;
422
423 ifp = XFS_IFORK_PTR(ip, whichfork);
424
425 info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
426 info.whichfork = whichfork;
427 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
428 info.sc = sc;
429
430 switch (whichfork) {
431 case XFS_COW_FORK:
432 /* Non-existent CoW forks are ignorable. */
433 if (!ifp)
434 goto out;
435 /* No CoW forks on non-reflink inodes/filesystems. */
436 if (!xfs_is_reflink_inode(ip)) {
437 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
438 goto out;
439 }
440 break;
441 case XFS_ATTR_FORK:
442 if (!ifp)
443 goto out;
444 if (!xfs_sb_version_hasattr(&mp->m_sb) &&
445 !xfs_sb_version_hasattr2(&mp->m_sb))
446 xfs_scrub_ino_set_corrupt(sc, sc->ip->i_ino, NULL);
447 break;
448 default:
449 ASSERT(whichfork == XFS_DATA_FORK);
450 break;
451 }
452
453 /* Check the fork values */
454 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
455 case XFS_DINODE_FMT_UUID:
456 case XFS_DINODE_FMT_DEV:
457 case XFS_DINODE_FMT_LOCAL:
458 /* No mappings to check. */
459 goto out;
460 case XFS_DINODE_FMT_EXTENTS:
461 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
462 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
463 goto out;
464 }
465 break;
466 case XFS_DINODE_FMT_BTREE:
467 if (whichfork == XFS_COW_FORK) {
468 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
469 goto out;
470 }
471
472 error = xfs_scrub_bmap_btree(sc, whichfork, &info);
473 if (error)
474 goto out;
475 break;
476 default:
477 xfs_scrub_fblock_set_corrupt(sc, whichfork, 0);
478 goto out;
479 }
480
481 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
482 goto out;
483
484 /* Now try to scrub the in-memory extent list. */
485 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
486 error = xfs_iread_extents(sc->tp, ip, whichfork);
487 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
488 goto out;
489 }
490
491 /* Find the offset of the last extent in the mapping. */
492 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
493 if (!xfs_scrub_fblock_process_error(sc, whichfork, 0, &error))
494 goto out;
495
496 /* Scrub extent records. */
497 info.lastoff = 0;
498 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong2b9e9b52018-01-08 10:49:03 -0800499 for_each_xfs_iext(ifp, &icur, &irec) {
Darrick J. Wong99d9d8d2017-10-17 21:37:43 -0700500 if (xfs_scrub_should_terminate(sc, &error))
501 break;
502 if (isnullstartblock(irec.br_startblock))
503 continue;
504 if (irec.br_startoff >= endoff) {
505 xfs_scrub_fblock_set_corrupt(sc, whichfork,
506 irec.br_startoff);
507 goto out;
508 }
509 error = xfs_scrub_bmap_extent(ip, NULL, &info, &irec);
510 if (error)
511 goto out;
512 }
513
514out:
515 return error;
516}
517
518/* Scrub an inode's data fork. */
519int
520xfs_scrub_bmap_data(
521 struct xfs_scrub_context *sc)
522{
523 return xfs_scrub_bmap(sc, XFS_DATA_FORK);
524}
525
526/* Scrub an inode's attr fork. */
527int
528xfs_scrub_bmap_attr(
529 struct xfs_scrub_context *sc)
530{
531 return xfs_scrub_bmap(sc, XFS_ATTR_FORK);
532}
533
534/* Scrub an inode's CoW fork. */
535int
536xfs_scrub_bmap_cow(
537 struct xfs_scrub_context *sc)
538{
539 if (!xfs_is_reflink_inode(sc->ip))
540 return -ENOENT;
541
542 return xfs_scrub_bmap(sc, XFS_COW_FORK);
543}