]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/xfs/scrub/ialloc.c
xfs: hoist inode cluster checks out of loop
[linux.git] / fs / xfs / scrub / ialloc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
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_alloc.h"
20 #include "xfs_ialloc.h"
21 #include "xfs_ialloc_btree.h"
22 #include "xfs_icache.h"
23 #include "xfs_rmap.h"
24 #include "xfs_log.h"
25 #include "xfs_trans_priv.h"
26 #include "scrub/xfs_scrub.h"
27 #include "scrub/scrub.h"
28 #include "scrub/common.h"
29 #include "scrub/btree.h"
30 #include "scrub/trace.h"
31
32 /*
33  * Set us up to scrub inode btrees.
34  * If we detect a discrepancy between the inobt and the inode,
35  * try again after forcing logged inode cores out to disk.
36  */
37 int
38 xchk_setup_ag_iallocbt(
39         struct xfs_scrub        *sc,
40         struct xfs_inode        *ip)
41 {
42         return xchk_setup_ag_btree(sc, ip, sc->try_harder);
43 }
44
45 /* Inode btree scrubber. */
46
47 struct xchk_iallocbt {
48         /* Number of inodes we see while scanning inobt. */
49         unsigned long long      inodes;
50
51         /* Expected next startino, for big block filesystems. */
52         xfs_agino_t             next_startino;
53
54         /* Expected end of the current inode cluster. */
55         xfs_agino_t             next_cluster_ino;
56 };
57
58 /*
59  * If we're checking the finobt, cross-reference with the inobt.
60  * Otherwise we're checking the inobt; if there is an finobt, make sure
61  * we have a record or not depending on freecount.
62  */
63 static inline void
64 xchk_iallocbt_chunk_xref_other(
65         struct xfs_scrub                *sc,
66         struct xfs_inobt_rec_incore     *irec,
67         xfs_agino_t                     agino)
68 {
69         struct xfs_btree_cur            **pcur;
70         bool                            has_irec;
71         int                             error;
72
73         if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
74                 pcur = &sc->sa.ino_cur;
75         else
76                 pcur = &sc->sa.fino_cur;
77         if (!(*pcur))
78                 return;
79         error = xfs_ialloc_has_inode_record(*pcur, agino, agino, &has_irec);
80         if (!xchk_should_check_xref(sc, &error, pcur))
81                 return;
82         if (((irec->ir_freecount > 0 && !has_irec) ||
83              (irec->ir_freecount == 0 && has_irec)))
84                 xchk_btree_xref_set_corrupt(sc, *pcur, 0);
85 }
86
87 /* Cross-reference with the other btrees. */
88 STATIC void
89 xchk_iallocbt_chunk_xref(
90         struct xfs_scrub                *sc,
91         struct xfs_inobt_rec_incore     *irec,
92         xfs_agino_t                     agino,
93         xfs_agblock_t                   agbno,
94         xfs_extlen_t                    len)
95 {
96         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
97                 return;
98
99         xchk_xref_is_used_space(sc, agbno, len);
100         xchk_iallocbt_chunk_xref_other(sc, irec, agino);
101         xchk_xref_is_owned_by(sc, agbno, len, &XFS_RMAP_OINFO_INODES);
102         xchk_xref_is_not_shared(sc, agbno, len);
103 }
104
105 /* Is this chunk worth checking? */
106 STATIC bool
107 xchk_iallocbt_chunk(
108         struct xchk_btree               *bs,
109         struct xfs_inobt_rec_incore     *irec,
110         xfs_agino_t                     agino,
111         xfs_extlen_t                    len)
112 {
113         struct xfs_mount                *mp = bs->cur->bc_mp;
114         xfs_agnumber_t                  agno = bs->cur->bc_private.a.agno;
115         xfs_agblock_t                   bno;
116
117         bno = XFS_AGINO_TO_AGBNO(mp, agino);
118         if (bno + len <= bno ||
119             !xfs_verify_agbno(mp, agno, bno) ||
120             !xfs_verify_agbno(mp, agno, bno + len - 1))
121                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
122
123         xchk_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len);
124
125         return true;
126 }
127
128 /* Count the number of free inodes. */
129 static unsigned int
130 xchk_iallocbt_freecount(
131         xfs_inofree_t                   freemask)
132 {
133         BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64));
134         return hweight64(freemask);
135 }
136
137 /* Check a particular inode with ir_free. */
138 STATIC int
139 xchk_iallocbt_check_cluster_freemask(
140         struct xchk_btree               *bs,
141         xfs_ino_t                       fsino,
142         xfs_agino_t                     chunkino,
143         xfs_agino_t                     clusterino,
144         struct xfs_inobt_rec_incore     *irec,
145         struct xfs_buf                  *bp)
146 {
147         struct xfs_dinode               *dip;
148         struct xfs_mount                *mp = bs->cur->bc_mp;
149         bool                            inode_is_free = false;
150         bool                            freemask_ok;
151         bool                            inuse;
152         int                             error = 0;
153
154         if (xchk_should_terminate(bs->sc, &error))
155                 return error;
156
157         dip = xfs_buf_offset(bp, clusterino * mp->m_sb.sb_inodesize);
158         if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
159             (dip->di_version >= 3 &&
160              be64_to_cpu(dip->di_ino) != fsino + clusterino)) {
161                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
162                 goto out;
163         }
164
165         if (irec->ir_free & XFS_INOBT_MASK(chunkino + clusterino))
166                 inode_is_free = true;
167         error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp,
168                         fsino + clusterino, &inuse);
169         if (error == -ENODATA) {
170                 /* Not cached, just read the disk buffer */
171                 freemask_ok = inode_is_free ^ !!(dip->di_mode);
172                 if (!bs->sc->try_harder && !freemask_ok)
173                         return -EDEADLOCK;
174         } else if (error < 0) {
175                 /*
176                  * Inode is only half assembled, or there was an IO error,
177                  * or the verifier failed, so don't bother trying to check.
178                  * The inode scrubber can deal with this.
179                  */
180                 goto out;
181         } else {
182                 /* Inode is all there. */
183                 freemask_ok = inode_is_free ^ inuse;
184         }
185         if (!freemask_ok)
186                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
187 out:
188         return 0;
189 }
190
191 /* Check an inode cluster. */
192 STATIC int
193 xchk_iallocbt_check_cluster(
194         struct xchk_btree               *bs,
195         struct xfs_inobt_rec_incore     *irec,
196         xfs_agino_t                     agino)
197 {
198         struct xfs_imap                 imap;
199         struct xfs_mount                *mp = bs->cur->bc_mp;
200         struct xfs_dinode               *dip;
201         struct xfs_buf                  *bp;
202         xfs_ino_t                       fsino;
203         unsigned int                    nr_inodes;
204         xfs_agino_t                     chunkino;
205         xfs_agino_t                     clusterino;
206         xfs_agblock_t                   agbno;
207         uint16_t                        holemask;
208         uint16_t                        ir_holemask;
209         int                             error = 0;
210
211         /* Make sure the freemask matches the inode records. */
212         nr_inodes = min_t(unsigned int, XFS_INODES_PER_CHUNK,
213                         mp->m_inodes_per_cluster);
214
215         fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_private.a.agno, agino);
216         chunkino = agino - irec->ir_startino;
217         agbno = XFS_AGINO_TO_AGBNO(mp, agino);
218
219         /* Compute the holemask mask for this cluster. */
220         for (clusterino = 0, holemask = 0; clusterino < nr_inodes;
221              clusterino += XFS_INODES_PER_HOLEMASK_BIT)
222                 holemask |= XFS_INOBT_MASK((chunkino + clusterino) /
223                                 XFS_INODES_PER_HOLEMASK_BIT);
224
225         /* The whole cluster must be a hole or not a hole. */
226         ir_holemask = (irec->ir_holemask & holemask);
227         if (ir_holemask != holemask && ir_holemask != 0) {
228                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
229                 return 0;
230         }
231
232         /* If any part of this is a hole, skip it. */
233         if (ir_holemask) {
234                 xchk_xref_is_not_owned_by(bs->sc, agbno,
235                                 mp->m_blocks_per_cluster,
236                                 &XFS_RMAP_OINFO_INODES);
237                 return 0;
238         }
239
240         xchk_xref_is_owned_by(bs->sc, agbno, mp->m_blocks_per_cluster,
241                         &XFS_RMAP_OINFO_INODES);
242
243         /* Grab the inode cluster buffer. */
244         imap.im_blkno = XFS_AGB_TO_DADDR(mp, bs->cur->bc_private.a.agno, agbno);
245         imap.im_len = XFS_FSB_TO_BB(mp, mp->m_blocks_per_cluster);
246         imap.im_boffset = 0;
247
248         error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap, &dip, &bp, 0, 0);
249         if (!xchk_btree_xref_process_error(bs->sc, bs->cur, 0, &error))
250                 return 0;
251
252         /* Which inodes are free? */
253         for (clusterino = 0; clusterino < nr_inodes; clusterino++) {
254                 error = xchk_iallocbt_check_cluster_freemask(bs, fsino,
255                                 chunkino, clusterino, irec, bp);
256                 if (error)
257                         break;
258         }
259
260         xfs_trans_brelse(bs->cur->bc_tp, bp);
261         return error;
262 }
263
264 /* Make sure the free mask is consistent with what the inodes think. */
265 STATIC int
266 xchk_iallocbt_check_freemask(
267         struct xchk_btree               *bs,
268         struct xfs_inobt_rec_incore     *irec)
269 {
270         struct xfs_mount                *mp = bs->cur->bc_mp;
271         xfs_agino_t                     agino;
272         int                             error = 0;
273
274         for (agino = irec->ir_startino;
275              agino < irec->ir_startino + XFS_INODES_PER_CHUNK;
276              agino += mp->m_inodes_per_cluster) {
277                 error = xchk_iallocbt_check_cluster(bs, irec, agino);
278                 if (error)
279                         break;
280         }
281
282         return error;
283 }
284
285 /*
286  * Make sure this inode btree record is aligned properly.  Because a fs block
287  * contains multiple inodes, we check that the inobt record is aligned to the
288  * correct inode, not just the correct block on disk.  This results in a finer
289  * grained corruption check.
290  */
291 STATIC void
292 xchk_iallocbt_rec_alignment(
293         struct xchk_btree               *bs,
294         struct xfs_inobt_rec_incore     *irec)
295 {
296         struct xfs_mount                *mp = bs->sc->mp;
297         struct xchk_iallocbt            *iabt = bs->private;
298
299         /*
300          * finobt records have different positioning requirements than inobt
301          * records: each finobt record must have a corresponding inobt record.
302          * That is checked in the xref function, so for now we only catch the
303          * obvious case where the record isn't at all aligned properly.
304          *
305          * Note that if a fs block contains more than a single chunk of inodes,
306          * we will have finobt records only for those chunks containing free
307          * inodes, and therefore expect chunk alignment of finobt records.
308          * Otherwise, we expect that the finobt record is aligned to the
309          * cluster alignment as told by the superblock.
310          */
311         if (bs->cur->bc_btnum == XFS_BTNUM_FINO) {
312                 unsigned int    imask;
313
314                 imask = min_t(unsigned int, XFS_INODES_PER_CHUNK,
315                                 mp->m_cluster_align_inodes) - 1;
316                 if (irec->ir_startino & imask)
317                         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
318                 return;
319         }
320
321         if (iabt->next_startino != NULLAGINO) {
322                 /*
323                  * We're midway through a cluster of inodes that is mapped by
324                  * multiple inobt records.  Did we get the record for the next
325                  * irec in the sequence?
326                  */
327                 if (irec->ir_startino != iabt->next_startino) {
328                         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
329                         return;
330                 }
331
332                 iabt->next_startino += XFS_INODES_PER_CHUNK;
333
334                 /* Are we done with the cluster? */
335                 if (iabt->next_startino >= iabt->next_cluster_ino) {
336                         iabt->next_startino = NULLAGINO;
337                         iabt->next_cluster_ino = NULLAGINO;
338                 }
339                 return;
340         }
341
342         /* inobt records must be aligned to cluster and inoalignmnt size. */
343         if (irec->ir_startino & (mp->m_cluster_align_inodes - 1)) {
344                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
345                 return;
346         }
347
348         if (irec->ir_startino & (mp->m_inodes_per_cluster - 1)) {
349                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
350                 return;
351         }
352
353         if (mp->m_inodes_per_cluster <= XFS_INODES_PER_CHUNK)
354                 return;
355
356         /*
357          * If this is the start of an inode cluster that can be mapped by
358          * multiple inobt records, the next inobt record must follow exactly
359          * after this one.
360          */
361         iabt->next_startino = irec->ir_startino + XFS_INODES_PER_CHUNK;
362         iabt->next_cluster_ino = irec->ir_startino + mp->m_inodes_per_cluster;
363 }
364
365 /* Scrub an inobt/finobt record. */
366 STATIC int
367 xchk_iallocbt_rec(
368         struct xchk_btree               *bs,
369         union xfs_btree_rec             *rec)
370 {
371         struct xfs_mount                *mp = bs->cur->bc_mp;
372         struct xchk_iallocbt            *iabt = bs->private;
373         struct xfs_inobt_rec_incore     irec;
374         uint64_t                        holes;
375         xfs_agnumber_t                  agno = bs->cur->bc_private.a.agno;
376         xfs_agino_t                     agino;
377         xfs_extlen_t                    len;
378         int                             holecount;
379         int                             i;
380         int                             error = 0;
381         unsigned int                    real_freecount;
382         uint16_t                        holemask;
383
384         xfs_inobt_btrec_to_irec(mp, rec, &irec);
385
386         if (irec.ir_count > XFS_INODES_PER_CHUNK ||
387             irec.ir_freecount > XFS_INODES_PER_CHUNK)
388                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
389
390         real_freecount = irec.ir_freecount +
391                         (XFS_INODES_PER_CHUNK - irec.ir_count);
392         if (real_freecount != xchk_iallocbt_freecount(irec.ir_free))
393                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
394
395         agino = irec.ir_startino;
396         /* Record has to be properly aligned within the AG. */
397         if (!xfs_verify_agino(mp, agno, agino) ||
398             !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) {
399                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
400                 goto out;
401         }
402
403         xchk_iallocbt_rec_alignment(bs, &irec);
404         if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
405                 goto out;
406
407         iabt->inodes += irec.ir_count;
408
409         /* Handle non-sparse inodes */
410         if (!xfs_inobt_issparse(irec.ir_holemask)) {
411                 len = XFS_B_TO_FSB(mp,
412                                 XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize);
413                 if (irec.ir_count != XFS_INODES_PER_CHUNK)
414                         xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
415
416                 if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
417                         goto out;
418                 goto check_freemask;
419         }
420
421         /* Check each chunk of a sparse inode cluster. */
422         holemask = irec.ir_holemask;
423         holecount = 0;
424         len = XFS_B_TO_FSB(mp,
425                         XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize);
426         holes = ~xfs_inobt_irec_to_allocmask(&irec);
427         if ((holes & irec.ir_free) != holes ||
428             irec.ir_freecount > irec.ir_count)
429                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
430
431         for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
432                 if (holemask & 1)
433                         holecount += XFS_INODES_PER_HOLEMASK_BIT;
434                 else if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
435                         break;
436                 holemask >>= 1;
437                 agino += XFS_INODES_PER_HOLEMASK_BIT;
438         }
439
440         if (holecount > XFS_INODES_PER_CHUNK ||
441             holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
442                 xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
443
444 check_freemask:
445         error = xchk_iallocbt_check_freemask(bs, &irec);
446         if (error)
447                 goto out;
448
449 out:
450         return error;
451 }
452
453 /*
454  * Make sure the inode btrees are as large as the rmap thinks they are.
455  * Don't bother if we're missing btree cursors, as we're already corrupt.
456  */
457 STATIC void
458 xchk_iallocbt_xref_rmap_btreeblks(
459         struct xfs_scrub        *sc,
460         int                     which)
461 {
462         xfs_filblks_t           blocks;
463         xfs_extlen_t            inobt_blocks = 0;
464         xfs_extlen_t            finobt_blocks = 0;
465         int                     error;
466
467         if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
468             (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->sa.fino_cur) ||
469             xchk_skip_xref(sc->sm))
470                 return;
471
472         /* Check that we saw as many inobt blocks as the rmap says. */
473         error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
474         if (!xchk_process_error(sc, 0, 0, &error))
475                 return;
476
477         if (sc->sa.fino_cur) {
478                 error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
479                 if (!xchk_process_error(sc, 0, 0, &error))
480                         return;
481         }
482
483         error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
484                         &XFS_RMAP_OINFO_INOBT, &blocks);
485         if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
486                 return;
487         if (blocks != inobt_blocks + finobt_blocks)
488                 xchk_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
489 }
490
491 /*
492  * Make sure that the inobt records point to the same number of blocks as
493  * the rmap says are owned by inodes.
494  */
495 STATIC void
496 xchk_iallocbt_xref_rmap_inodes(
497         struct xfs_scrub        *sc,
498         int                     which,
499         unsigned long long      inodes)
500 {
501         xfs_filblks_t           blocks;
502         xfs_filblks_t           inode_blocks;
503         int                     error;
504
505         if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
506                 return;
507
508         /* Check that we saw as many inode blocks as the rmap knows about. */
509         error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
510                         &XFS_RMAP_OINFO_INODES, &blocks);
511         if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
512                 return;
513         inode_blocks = XFS_B_TO_FSB(sc->mp, inodes * sc->mp->m_sb.sb_inodesize);
514         if (blocks != inode_blocks)
515                 xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
516 }
517
518 /* Scrub the inode btrees for some AG. */
519 STATIC int
520 xchk_iallocbt(
521         struct xfs_scrub        *sc,
522         xfs_btnum_t             which)
523 {
524         struct xfs_btree_cur    *cur;
525         struct xchk_iallocbt    iabt = {
526                 .inodes         = 0,
527                 .next_startino  = NULLAGINO,
528                 .next_cluster_ino = NULLAGINO,
529         };
530         int                     error;
531
532         cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur;
533         error = xchk_btree(sc, cur, xchk_iallocbt_rec, &XFS_RMAP_OINFO_INOBT,
534                         &iabt);
535         if (error)
536                 return error;
537
538         xchk_iallocbt_xref_rmap_btreeblks(sc, which);
539
540         /*
541          * If we're scrubbing the inode btree, inode_blocks is the number of
542          * blocks pointed to by all the inode chunk records.  Therefore, we
543          * should compare to the number of inode chunk blocks that the rmap
544          * knows about.  We can't do this for the finobt since it only points
545          * to inode chunks with free inodes.
546          */
547         if (which == XFS_BTNUM_INO)
548                 xchk_iallocbt_xref_rmap_inodes(sc, which, iabt.inodes);
549
550         return error;
551 }
552
553 int
554 xchk_inobt(
555         struct xfs_scrub        *sc)
556 {
557         return xchk_iallocbt(sc, XFS_BTNUM_INO);
558 }
559
560 int
561 xchk_finobt(
562         struct xfs_scrub        *sc)
563 {
564         return xchk_iallocbt(sc, XFS_BTNUM_FINO);
565 }
566
567 /* See if an inode btree has (or doesn't have) an inode chunk record. */
568 static inline void
569 xchk_xref_inode_check(
570         struct xfs_scrub        *sc,
571         xfs_agblock_t           agbno,
572         xfs_extlen_t            len,
573         struct xfs_btree_cur    **icur,
574         bool                    should_have_inodes)
575 {
576         bool                    has_inodes;
577         int                     error;
578
579         if (!(*icur) || xchk_skip_xref(sc->sm))
580                 return;
581
582         error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes);
583         if (!xchk_should_check_xref(sc, &error, icur))
584                 return;
585         if (has_inodes != should_have_inodes)
586                 xchk_btree_xref_set_corrupt(sc, *icur, 0);
587 }
588
589 /* xref check that the extent is not covered by inodes */
590 void
591 xchk_xref_is_not_inode_chunk(
592         struct xfs_scrub        *sc,
593         xfs_agblock_t           agbno,
594         xfs_extlen_t            len)
595 {
596         xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false);
597         xchk_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false);
598 }
599
600 /* xref check that the extent is covered by inodes */
601 void
602 xchk_xref_is_inode_chunk(
603         struct xfs_scrub        *sc,
604         xfs_agblock_t           agbno,
605         xfs_extlen_t            len)
606 {
607         xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true);
608 }