xref: /linux/fs/xfs/scrub/rmap.c (revision fd639726bf15fca8ee1a00dce8e0096d0ad9bd18)
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_ialloc.h"
34 #include "xfs_rmap.h"
35 #include "scrub/xfs_scrub.h"
36 #include "scrub/scrub.h"
37 #include "scrub/common.h"
38 #include "scrub/btree.h"
39 #include "scrub/trace.h"
40 
41 /*
42  * Set us up to scrub reverse mapping btrees.
43  */
44 int
45 xfs_scrub_setup_ag_rmapbt(
46 	struct xfs_scrub_context	*sc,
47 	struct xfs_inode		*ip)
48 {
49 	return xfs_scrub_setup_ag_btree(sc, ip, false);
50 }
51 
52 /* Reverse-mapping scrubber. */
53 
54 /* Scrub an rmapbt record. */
55 STATIC int
56 xfs_scrub_rmapbt_rec(
57 	struct xfs_scrub_btree		*bs,
58 	union xfs_btree_rec		*rec)
59 {
60 	struct xfs_mount		*mp = bs->cur->bc_mp;
61 	struct xfs_rmap_irec		irec;
62 	xfs_agnumber_t			agno = bs->cur->bc_private.a.agno;
63 	bool				non_inode;
64 	bool				is_unwritten;
65 	bool				is_bmbt;
66 	bool				is_attr;
67 	int				error;
68 
69 	error = xfs_rmap_btrec_to_irec(rec, &irec);
70 	if (!xfs_scrub_btree_process_error(bs->sc, bs->cur, 0, &error))
71 		goto out;
72 
73 	/* Check extent. */
74 	if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
75 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
76 
77 	if (irec.rm_owner == XFS_RMAP_OWN_FS) {
78 		/*
79 		 * xfs_verify_agbno returns false for static fs metadata.
80 		 * Since that only exists at the start of the AG, validate
81 		 * that by hand.
82 		 */
83 		if (irec.rm_startblock != 0 ||
84 		    irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
85 			xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
86 	} else {
87 		/*
88 		 * Otherwise we must point somewhere past the static metadata
89 		 * but before the end of the FS.  Run the regular check.
90 		 */
91 		if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
92 		    !xfs_verify_agbno(mp, agno, irec.rm_startblock +
93 				irec.rm_blockcount - 1))
94 			xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
95 	}
96 
97 	/* Check flags. */
98 	non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
99 	is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
100 	is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
101 	is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
102 
103 	if (is_bmbt && irec.rm_offset != 0)
104 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
105 
106 	if (non_inode && irec.rm_offset != 0)
107 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
108 
109 	if (is_unwritten && (is_bmbt || non_inode || is_attr))
110 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
111 
112 	if (non_inode && (is_bmbt || is_unwritten || is_attr))
113 		xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
114 
115 	if (!non_inode) {
116 		if (!xfs_verify_ino(mp, irec.rm_owner))
117 			xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
118 	} else {
119 		/* Non-inode owner within the magic values? */
120 		if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
121 		    irec.rm_owner > XFS_RMAP_OWN_FS)
122 			xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
123 	}
124 out:
125 	return error;
126 }
127 
128 /* Scrub the rmap btree for some AG. */
129 int
130 xfs_scrub_rmapbt(
131 	struct xfs_scrub_context	*sc)
132 {
133 	struct xfs_owner_info		oinfo;
134 
135 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
136 	return xfs_scrub_btree(sc, sc->sa.rmap_cur, xfs_scrub_rmapbt_rec,
137 			&oinfo, NULL);
138 }
139