blob: 54b0eac22707ce742cc58ebf7ca937aaf5e0b1dc [file] [log] [blame]
Darrick J. Wongc7e693d2017-10-17 21:37:41 -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_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 */
44int
45xfs_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
Darrick J. Wong166d7642018-01-16 18:53:05 -080054/* Cross-reference with the other btrees. */
55STATIC void
56xfs_scrub_rmapbt_xref(
57 struct xfs_scrub_context *sc,
58 struct xfs_rmap_irec *irec)
59{
Darrick J. Wong52dc4b42018-01-16 18:53:06 -080060 xfs_agblock_t agbno = irec->rm_startblock;
61 xfs_extlen_t len = irec->rm_blockcount;
62
Darrick J. Wong166d7642018-01-16 18:53:05 -080063 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
64 return;
Darrick J. Wong52dc4b42018-01-16 18:53:06 -080065
66 xfs_scrub_xref_is_used_space(sc, agbno, len);
Darrick J. Wong166d7642018-01-16 18:53:05 -080067}
68
Darrick J. Wongc7e693d2017-10-17 21:37:41 -070069/* Scrub an rmapbt record. */
70STATIC int
71xfs_scrub_rmapbt_rec(
72 struct xfs_scrub_btree *bs,
73 union xfs_btree_rec *rec)
74{
75 struct xfs_mount *mp = bs->cur->bc_mp;
76 struct xfs_rmap_irec irec;
77 xfs_agnumber_t agno = bs->cur->bc_private.a.agno;
78 bool non_inode;
79 bool is_unwritten;
80 bool is_bmbt;
81 bool is_attr;
82 int error;
83
84 error = xfs_rmap_btrec_to_irec(rec, &irec);
85 if (!xfs_scrub_btree_process_error(bs->sc, bs->cur, 0, &error))
86 goto out;
87
88 /* Check extent. */
89 if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
90 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
91
92 if (irec.rm_owner == XFS_RMAP_OWN_FS) {
93 /*
94 * xfs_verify_agbno returns false for static fs metadata.
95 * Since that only exists at the start of the AG, validate
96 * that by hand.
97 */
98 if (irec.rm_startblock != 0 ||
99 irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
100 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
101 } else {
102 /*
103 * Otherwise we must point somewhere past the static metadata
104 * but before the end of the FS. Run the regular check.
105 */
106 if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
107 !xfs_verify_agbno(mp, agno, irec.rm_startblock +
108 irec.rm_blockcount - 1))
109 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
110 }
111
112 /* Check flags. */
113 non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
114 is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
115 is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
116 is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
117
118 if (is_bmbt && irec.rm_offset != 0)
119 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
120
121 if (non_inode && irec.rm_offset != 0)
122 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
123
124 if (is_unwritten && (is_bmbt || non_inode || is_attr))
125 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
126
127 if (non_inode && (is_bmbt || is_unwritten || is_attr))
128 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
129
130 if (!non_inode) {
131 if (!xfs_verify_ino(mp, irec.rm_owner))
132 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
133 } else {
134 /* Non-inode owner within the magic values? */
135 if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
136 irec.rm_owner > XFS_RMAP_OWN_FS)
137 xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0);
138 }
Darrick J. Wong166d7642018-01-16 18:53:05 -0800139
140 xfs_scrub_rmapbt_xref(bs->sc, &irec);
Darrick J. Wongc7e693d2017-10-17 21:37:41 -0700141out:
142 return error;
143}
144
145/* Scrub the rmap btree for some AG. */
146int
147xfs_scrub_rmapbt(
148 struct xfs_scrub_context *sc)
149{
150 struct xfs_owner_info oinfo;
151
152 xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
153 return xfs_scrub_btree(sc, sc->sa.rmap_cur, xfs_scrub_rmapbt_rec,
154 &oinfo, NULL);
155}