xref: /openbmc/linux/fs/xfs/scrub/refcount.c (revision bf070bb0)
1 /*
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_alloc.h"
33 #include "xfs_rmap.h"
34 #include "scrub/xfs_scrub.h"
35 #include "scrub/scrub.h"
36 #include "scrub/common.h"
37 #include "scrub/btree.h"
38 #include "scrub/trace.h"
39 
40 /*
41  * Set us up to scrub reference count btrees.
42  */
43 int
44 xfs_scrub_setup_ag_refcountbt(
45 	struct xfs_scrub_context	*sc,
46 	struct xfs_inode		*ip)
47 {
48 	return xfs_scrub_setup_ag_btree(sc, ip, false);
49 }
50 
51 /* Reference count btree scrubber. */
52 
53 /* Scrub a refcountbt record. */
54 STATIC int
55 xfs_scrub_refcountbt_rec(
56 	struct xfs_scrub_btree		*bs,
57 	union xfs_btree_rec		*rec)
58 {
59 	struct xfs_mount		*mp = bs->cur->bc_mp;
60 	xfs_agnumber_t			agno = bs->cur->bc_private.a.agno;
61 	xfs_agblock_t			bno;
62 	xfs_extlen_t			len;
63 	xfs_nlink_t			refcount;
64 	bool				has_cowflag;
65 	int				error = 0;
66 
67 	bno = be32_to_cpu(rec->refc.rc_startblock);
68 	len = be32_to_cpu(rec->refc.rc_blockcount);
69 	refcount = be32_to_cpu(rec->refc.rc_refcount);
70 
71 	/* Only CoW records can have refcount == 1. */
72 	has_cowflag = (bno & XFS_REFC_COW_START);
73 	if ((refcount == 1 && !has_cowflag) || (refcount != 1 && has_cowflag))
74 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
75 
76 	/* Check the extent. */
77 	bno &= ~XFS_REFC_COW_START;
78 	if (bno + len <= bno ||
79 	    !xfs_verify_agbno(mp, agno, bno) ||
80 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
81 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
82 
83 	if (refcount == 0)
84 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
85 
86 	return error;
87 }
88 
89 /* Scrub the refcount btree for some AG. */
90 int
91 xfs_scrub_refcountbt(
92 	struct xfs_scrub_context	*sc)
93 {
94 	struct xfs_owner_info		oinfo;
95 
96 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_REFC);
97 	return xfs_scrub_btree(sc, sc->sa.refc_cur, xfs_scrub_refcountbt_rec,
98 			&oinfo, NULL);
99 }
100