blob: 1a16f7867e315259f694ad0a4140f9277f7422ec [file] [log] [blame]
Darrick J. Wong3daa6642017-10-17 21:37:40 -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_alloc.h"
34#include "xfs_ialloc.h"
35#include "xfs_ialloc_btree.h"
36#include "xfs_icache.h"
37#include "xfs_rmap.h"
38#include "xfs_log.h"
39#include "xfs_trans_priv.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/*
47 * Set us up to scrub inode btrees.
48 * If we detect a discrepancy between the inobt and the inode,
49 * try again after forcing logged inode cores out to disk.
50 */
51int
52xfs_scrub_setup_ag_iallocbt(
53 struct xfs_scrub_context *sc,
54 struct xfs_inode *ip)
55{
56 return xfs_scrub_setup_ag_btree(sc, ip, sc->try_harder);
57}
58
59/* Inode btree scrubber. */
60
Darrick J. Wong2e6f2752018-01-16 18:53:07 -080061/*
62 * If we're checking the finobt, cross-reference with the inobt.
63 * Otherwise we're checking the inobt; if there is an finobt, make sure
64 * we have a record or not depending on freecount.
65 */
66static inline void
67xfs_scrub_iallocbt_chunk_xref_other(
68 struct xfs_scrub_context *sc,
69 struct xfs_inobt_rec_incore *irec,
70 xfs_agino_t agino)
71{
72 struct xfs_btree_cur **pcur;
73 bool has_irec;
74 int error;
75
76 if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
77 pcur = &sc->sa.ino_cur;
78 else
79 pcur = &sc->sa.fino_cur;
80 if (!(*pcur))
81 return;
82 error = xfs_ialloc_has_inode_record(*pcur, agino, agino, &has_irec);
83 if (!xfs_scrub_should_check_xref(sc, &error, pcur))
84 return;
85 if (((irec->ir_freecount > 0 && !has_irec) ||
86 (irec->ir_freecount == 0 && has_irec)))
87 xfs_scrub_btree_xref_set_corrupt(sc, *pcur, 0);
88}
89
Darrick J. Wong166d7642018-01-16 18:53:05 -080090/* Cross-reference with the other btrees. */
91STATIC void
92xfs_scrub_iallocbt_chunk_xref(
93 struct xfs_scrub_context *sc,
94 struct xfs_inobt_rec_incore *irec,
95 xfs_agino_t agino,
96 xfs_agblock_t agbno,
97 xfs_extlen_t len)
98{
Darrick J. Wongd8526572018-01-16 18:53:08 -080099 struct xfs_owner_info oinfo;
100
Darrick J. Wong166d7642018-01-16 18:53:05 -0800101 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
102 return;
Darrick J. Wong52dc4b42018-01-16 18:53:06 -0800103
104 xfs_scrub_xref_is_used_space(sc, agbno, len);
Darrick J. Wong2e6f2752018-01-16 18:53:07 -0800105 xfs_scrub_iallocbt_chunk_xref_other(sc, irec, agino);
Darrick J. Wongd8526572018-01-16 18:53:08 -0800106 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
107 xfs_scrub_xref_is_owned_by(sc, agbno, len, &oinfo);
Darrick J. Wong166d7642018-01-16 18:53:05 -0800108}
109
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700110/* Is this chunk worth checking? */
111STATIC bool
112xfs_scrub_iallocbt_chunk(
113 struct xfs_scrub_btree *bs,
114 struct xfs_inobt_rec_incore *irec,
115 xfs_agino_t agino,
116 xfs_extlen_t len)
117{
118 struct xfs_mount *mp = bs->cur->bc_mp;
119 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
120 xfs_agblock_t bno;
121
122 bno = XFS_AGINO_TO_AGBNO(mp, agino);
123 if (bno + len <= bno ||
124 !xfs_verify_agbno(mp, agno, bno) ||
125 !xfs_verify_agbno(mp, agno, bno + len - 1))
126 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
127
Darrick J. Wong166d7642018-01-16 18:53:05 -0800128 xfs_scrub_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len);
129
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700130 return true;
131}
132
133/* Count the number of free inodes. */
134static unsigned int
135xfs_scrub_iallocbt_freecount(
136 xfs_inofree_t freemask)
137{
138 BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64));
139 return hweight64(freemask);
140}
141
142/* Check a particular inode with ir_free. */
143STATIC int
144xfs_scrub_iallocbt_check_cluster_freemask(
145 struct xfs_scrub_btree *bs,
146 xfs_ino_t fsino,
147 xfs_agino_t chunkino,
148 xfs_agino_t clusterino,
149 struct xfs_inobt_rec_incore *irec,
150 struct xfs_buf *bp)
151{
152 struct xfs_dinode *dip;
153 struct xfs_mount *mp = bs->cur->bc_mp;
154 bool inode_is_free = false;
155 bool freemask_ok;
156 bool inuse;
157 int error = 0;
158
159 if (xfs_scrub_should_terminate(bs->sc, &error))
160 return error;
161
162 dip = xfs_buf_offset(bp, clusterino * mp->m_sb.sb_inodesize);
163 if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
164 (dip->di_version >= 3 &&
165 be64_to_cpu(dip->di_ino) != fsino + clusterino)) {
166 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
167 goto out;
168 }
169
170 if (irec->ir_free & XFS_INOBT_MASK(chunkino + clusterino))
171 inode_is_free = true;
172 error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp,
173 fsino + clusterino, &inuse);
174 if (error == -ENODATA) {
175 /* Not cached, just read the disk buffer */
176 freemask_ok = inode_is_free ^ !!(dip->di_mode);
177 if (!bs->sc->try_harder && !freemask_ok)
178 return -EDEADLOCK;
179 } else if (error < 0) {
180 /*
181 * Inode is only half assembled, or there was an IO error,
182 * or the verifier failed, so don't bother trying to check.
183 * The inode scrubber can deal with this.
184 */
185 goto out;
186 } else {
187 /* Inode is all there. */
188 freemask_ok = inode_is_free ^ inuse;
189 }
190 if (!freemask_ok)
191 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
192out:
193 return 0;
194}
195
196/* Make sure the free mask is consistent with what the inodes think. */
197STATIC int
198xfs_scrub_iallocbt_check_freemask(
199 struct xfs_scrub_btree *bs,
200 struct xfs_inobt_rec_incore *irec)
201{
202 struct xfs_owner_info oinfo;
203 struct xfs_imap imap;
204 struct xfs_mount *mp = bs->cur->bc_mp;
205 struct xfs_dinode *dip;
206 struct xfs_buf *bp;
207 xfs_ino_t fsino;
208 xfs_agino_t nr_inodes;
209 xfs_agino_t agino;
210 xfs_agino_t chunkino;
211 xfs_agino_t clusterino;
212 xfs_agblock_t agbno;
213 int blks_per_cluster;
214 uint16_t holemask;
215 uint16_t ir_holemask;
216 int error = 0;
217
218 /* Make sure the freemask matches the inode records. */
219 blks_per_cluster = xfs_icluster_size_fsb(mp);
220 nr_inodes = XFS_OFFBNO_TO_AGINO(mp, blks_per_cluster, 0);
221 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
222
223 for (agino = irec->ir_startino;
224 agino < irec->ir_startino + XFS_INODES_PER_CHUNK;
225 agino += blks_per_cluster * mp->m_sb.sb_inopblock) {
226 fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_private.a.agno, agino);
227 chunkino = agino - irec->ir_startino;
228 agbno = XFS_AGINO_TO_AGBNO(mp, agino);
229
230 /* Compute the holemask mask for this cluster. */
231 for (clusterino = 0, holemask = 0; clusterino < nr_inodes;
232 clusterino += XFS_INODES_PER_HOLEMASK_BIT)
233 holemask |= XFS_INOBT_MASK((chunkino + clusterino) /
234 XFS_INODES_PER_HOLEMASK_BIT);
235
236 /* The whole cluster must be a hole or not a hole. */
237 ir_holemask = (irec->ir_holemask & holemask);
238 if (ir_holemask != holemask && ir_holemask != 0) {
239 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
240 continue;
241 }
242
243 /* If any part of this is a hole, skip it. */
Darrick J. Wongd8526572018-01-16 18:53:08 -0800244 if (ir_holemask) {
245 xfs_scrub_xref_is_not_owned_by(bs->sc, agbno,
246 blks_per_cluster, &oinfo);
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700247 continue;
Darrick J. Wongd8526572018-01-16 18:53:08 -0800248 }
249
250 xfs_scrub_xref_is_owned_by(bs->sc, agbno, blks_per_cluster,
251 &oinfo);
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700252
253 /* Grab the inode cluster buffer. */
254 imap.im_blkno = XFS_AGB_TO_DADDR(mp, bs->cur->bc_private.a.agno,
255 agbno);
256 imap.im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
257 imap.im_boffset = 0;
258
259 error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap,
260 &dip, &bp, 0, 0);
261 if (!xfs_scrub_btree_process_error(bs->sc, bs->cur, 0, &error))
262 continue;
263
264 /* Which inodes are free? */
265 for (clusterino = 0; clusterino < nr_inodes; clusterino++) {
266 error = xfs_scrub_iallocbt_check_cluster_freemask(bs,
267 fsino, chunkino, clusterino, irec, bp);
268 if (error) {
269 xfs_trans_brelse(bs->cur->bc_tp, bp);
270 return error;
271 }
272 }
273
274 xfs_trans_brelse(bs->cur->bc_tp, bp);
275 }
276
277 return error;
278}
279
280/* Scrub an inobt/finobt record. */
281STATIC int
282xfs_scrub_iallocbt_rec(
283 struct xfs_scrub_btree *bs,
284 union xfs_btree_rec *rec)
285{
286 struct xfs_mount *mp = bs->cur->bc_mp;
Darrick J. Wongd8526572018-01-16 18:53:08 -0800287 xfs_filblks_t *inode_blocks = bs->private;
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700288 struct xfs_inobt_rec_incore irec;
289 uint64_t holes;
290 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
291 xfs_agino_t agino;
292 xfs_agblock_t agbno;
293 xfs_extlen_t len;
294 int holecount;
295 int i;
296 int error = 0;
297 unsigned int real_freecount;
298 uint16_t holemask;
299
300 xfs_inobt_btrec_to_irec(mp, rec, &irec);
301
302 if (irec.ir_count > XFS_INODES_PER_CHUNK ||
303 irec.ir_freecount > XFS_INODES_PER_CHUNK)
304 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
305
306 real_freecount = irec.ir_freecount +
307 (XFS_INODES_PER_CHUNK - irec.ir_count);
308 if (real_freecount != xfs_scrub_iallocbt_freecount(irec.ir_free))
309 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
310
311 agino = irec.ir_startino;
312 /* Record has to be properly aligned within the AG. */
313 if (!xfs_verify_agino(mp, agno, agino) ||
314 !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) {
315 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
316 goto out;
317 }
318
319 /* Make sure this record is aligned to cluster and inoalignmnt size. */
320 agbno = XFS_AGINO_TO_AGBNO(mp, irec.ir_startino);
321 if ((agbno & (xfs_ialloc_cluster_alignment(mp) - 1)) ||
322 (agbno & (xfs_icluster_size_fsb(mp) - 1)))
323 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
324
Darrick J. Wongd8526572018-01-16 18:53:08 -0800325 *inode_blocks += XFS_B_TO_FSB(mp,
326 irec.ir_count * mp->m_sb.sb_inodesize);
327
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700328 /* Handle non-sparse inodes */
329 if (!xfs_inobt_issparse(irec.ir_holemask)) {
330 len = XFS_B_TO_FSB(mp,
331 XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize);
332 if (irec.ir_count != XFS_INODES_PER_CHUNK)
333 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
334
335 if (!xfs_scrub_iallocbt_chunk(bs, &irec, agino, len))
336 goto out;
337 goto check_freemask;
338 }
339
340 /* Check each chunk of a sparse inode cluster. */
341 holemask = irec.ir_holemask;
342 holecount = 0;
343 len = XFS_B_TO_FSB(mp,
344 XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize);
345 holes = ~xfs_inobt_irec_to_allocmask(&irec);
346 if ((holes & irec.ir_free) != holes ||
347 irec.ir_freecount > irec.ir_count)
348 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
349
350 for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
351 if (holemask & 1)
352 holecount += XFS_INODES_PER_HOLEMASK_BIT;
353 else if (!xfs_scrub_iallocbt_chunk(bs, &irec, agino, len))
354 break;
355 holemask >>= 1;
356 agino += XFS_INODES_PER_HOLEMASK_BIT;
357 }
358
359 if (holecount > XFS_INODES_PER_CHUNK ||
360 holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
361 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
362
363check_freemask:
364 error = xfs_scrub_iallocbt_check_freemask(bs, &irec);
365 if (error)
366 goto out;
367
368out:
369 return error;
370}
371
Darrick J. Wongd8526572018-01-16 18:53:08 -0800372/*
373 * Make sure the inode btrees are as large as the rmap thinks they are.
374 * Don't bother if we're missing btree cursors, as we're already corrupt.
375 */
376STATIC void
377xfs_scrub_iallocbt_xref_rmap_btreeblks(
378 struct xfs_scrub_context *sc,
379 int which)
380{
381 struct xfs_owner_info oinfo;
382 xfs_filblks_t blocks;
383 xfs_extlen_t inobt_blocks = 0;
384 xfs_extlen_t finobt_blocks = 0;
385 int error;
386
387 if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
388 (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->sa.fino_cur))
389 return;
390
391 /* Check that we saw as many inobt blocks as the rmap says. */
392 error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
393 if (!xfs_scrub_should_check_xref(sc, &error, &sc->sa.ino_cur))
394 return;
395
396 if (sc->sa.fino_cur) {
397 error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
398 if (!xfs_scrub_should_check_xref(sc, &error, &sc->sa.fino_cur))
399 return;
400 }
401
402 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
403 error = xfs_scrub_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
404 &blocks);
405 if (!xfs_scrub_should_check_xref(sc, &error, &sc->sa.rmap_cur))
406 return;
407 if (blocks != inobt_blocks + finobt_blocks)
408 xfs_scrub_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
409}
410
411/*
412 * Make sure that the inobt records point to the same number of blocks as
413 * the rmap says are owned by inodes.
414 */
415STATIC void
416xfs_scrub_iallocbt_xref_rmap_inodes(
417 struct xfs_scrub_context *sc,
418 int which,
419 xfs_filblks_t inode_blocks)
420{
421 struct xfs_owner_info oinfo;
422 xfs_filblks_t blocks;
423 int error;
424
425 if (!sc->sa.rmap_cur)
426 return;
427
428 /* Check that we saw as many inode blocks as the rmap knows about. */
429 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
430 error = xfs_scrub_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
431 &blocks);
432 if (!xfs_scrub_should_check_xref(sc, &error, &sc->sa.rmap_cur))
433 return;
434 if (blocks != inode_blocks)
435 xfs_scrub_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
436}
437
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700438/* Scrub the inode btrees for some AG. */
439STATIC int
440xfs_scrub_iallocbt(
441 struct xfs_scrub_context *sc,
442 xfs_btnum_t which)
443{
444 struct xfs_btree_cur *cur;
445 struct xfs_owner_info oinfo;
Darrick J. Wongd8526572018-01-16 18:53:08 -0800446 xfs_filblks_t inode_blocks = 0;
447 int error;
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700448
449 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
450 cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur;
Darrick J. Wongd8526572018-01-16 18:53:08 -0800451 error = xfs_scrub_btree(sc, cur, xfs_scrub_iallocbt_rec, &oinfo,
452 &inode_blocks);
453 if (error)
454 return error;
455
456 xfs_scrub_iallocbt_xref_rmap_btreeblks(sc, which);
457
458 /*
459 * If we're scrubbing the inode btree, inode_blocks is the number of
460 * blocks pointed to by all the inode chunk records. Therefore, we
461 * should compare to the number of inode chunk blocks that the rmap
462 * knows about. We can't do this for the finobt since it only points
463 * to inode chunks with free inodes.
464 */
465 if (which == XFS_BTNUM_INO)
466 xfs_scrub_iallocbt_xref_rmap_inodes(sc, which, inode_blocks);
467
468 return error;
Darrick J. Wong3daa6642017-10-17 21:37:40 -0700469}
470
471int
472xfs_scrub_inobt(
473 struct xfs_scrub_context *sc)
474{
475 return xfs_scrub_iallocbt(sc, XFS_BTNUM_INO);
476}
477
478int
479xfs_scrub_finobt(
480 struct xfs_scrub_context *sc)
481{
482 return xfs_scrub_iallocbt(sc, XFS_BTNUM_FINO);
483}
Darrick J. Wong2e6f2752018-01-16 18:53:07 -0800484
485/* See if an inode btree has (or doesn't have) an inode chunk record. */
486static inline void
487xfs_scrub_xref_inode_check(
488 struct xfs_scrub_context *sc,
489 xfs_agblock_t agbno,
490 xfs_extlen_t len,
491 struct xfs_btree_cur **icur,
492 bool should_have_inodes)
493{
494 bool has_inodes;
495 int error;
496
497 if (!(*icur))
498 return;
499
500 error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes);
501 if (!xfs_scrub_should_check_xref(sc, &error, icur))
502 return;
503 if (has_inodes != should_have_inodes)
504 xfs_scrub_btree_xref_set_corrupt(sc, *icur, 0);
505}
506
507/* xref check that the extent is not covered by inodes */
508void
509xfs_scrub_xref_is_not_inode_chunk(
510 struct xfs_scrub_context *sc,
511 xfs_agblock_t agbno,
512 xfs_extlen_t len)
513{
514 xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false);
515 xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false);
516}
517
518/* xref check that the extent is covered by inodes */
519void
520xfs_scrub_xref_is_inode_chunk(
521 struct xfs_scrub_context *sc,
522 xfs_agblock_t agbno,
523 xfs_extlen_t len)
524{
525 xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true);
526}