Darrick J. Wong | c7e693d | 2017-10-17 21:37:41 -0700 | [diff] [blame] | 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 | |
Darrick J. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 54 | /* Cross-reference with the other btrees. */ |
| 55 | STATIC void |
| 56 | xfs_scrub_rmapbt_xref( |
| 57 | struct xfs_scrub_context *sc, |
| 58 | struct xfs_rmap_irec *irec) |
| 59 | { |
Darrick J. Wong | 52dc4b4 | 2018-01-16 18:53:06 -0800 | [diff] [blame^] | 60 | xfs_agblock_t agbno = irec->rm_startblock; |
| 61 | xfs_extlen_t len = irec->rm_blockcount; |
| 62 | |
Darrick J. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 63 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 64 | return; |
Darrick J. Wong | 52dc4b4 | 2018-01-16 18:53:06 -0800 | [diff] [blame^] | 65 | |
| 66 | xfs_scrub_xref_is_used_space(sc, agbno, len); |
Darrick J. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Darrick J. Wong | c7e693d | 2017-10-17 21:37:41 -0700 | [diff] [blame] | 69 | /* Scrub an rmapbt record. */ |
| 70 | STATIC int |
| 71 | xfs_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. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 139 | |
| 140 | xfs_scrub_rmapbt_xref(bs->sc, &irec); |
Darrick J. Wong | c7e693d | 2017-10-17 21:37:41 -0700 | [diff] [blame] | 141 | out: |
| 142 | return error; |
| 143 | } |
| 144 | |
| 145 | /* Scrub the rmap btree for some AG. */ |
| 146 | int |
| 147 | xfs_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 | } |