Darrick J. Wong | 3daa664 | 2017-10-17 21:37:40 -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_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 | */ |
| 51 | int |
| 52 | xfs_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. Wong | 2e6f275 | 2018-01-16 18:53:07 -0800 | [diff] [blame^] | 61 | /* |
| 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 | */ |
| 66 | static inline void |
| 67 | xfs_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. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 90 | /* Cross-reference with the other btrees. */ |
| 91 | STATIC void |
| 92 | xfs_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 | { |
| 99 | if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 100 | return; |
Darrick J. Wong | 52dc4b4 | 2018-01-16 18:53:06 -0800 | [diff] [blame] | 101 | |
| 102 | xfs_scrub_xref_is_used_space(sc, agbno, len); |
Darrick J. Wong | 2e6f275 | 2018-01-16 18:53:07 -0800 | [diff] [blame^] | 103 | xfs_scrub_iallocbt_chunk_xref_other(sc, irec, agino); |
Darrick J. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Darrick J. Wong | 3daa664 | 2017-10-17 21:37:40 -0700 | [diff] [blame] | 106 | /* Is this chunk worth checking? */ |
| 107 | STATIC bool |
| 108 | xfs_scrub_iallocbt_chunk( |
| 109 | struct xfs_scrub_btree *bs, |
| 110 | struct xfs_inobt_rec_incore *irec, |
| 111 | xfs_agino_t agino, |
| 112 | xfs_extlen_t len) |
| 113 | { |
| 114 | struct xfs_mount *mp = bs->cur->bc_mp; |
| 115 | xfs_agnumber_t agno = bs->cur->bc_private.a.agno; |
| 116 | xfs_agblock_t bno; |
| 117 | |
| 118 | bno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 119 | if (bno + len <= bno || |
| 120 | !xfs_verify_agbno(mp, agno, bno) || |
| 121 | !xfs_verify_agbno(mp, agno, bno + len - 1)) |
| 122 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 123 | |
Darrick J. Wong | 166d764 | 2018-01-16 18:53:05 -0800 | [diff] [blame] | 124 | xfs_scrub_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len); |
| 125 | |
Darrick J. Wong | 3daa664 | 2017-10-17 21:37:40 -0700 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /* Count the number of free inodes. */ |
| 130 | static unsigned int |
| 131 | xfs_scrub_iallocbt_freecount( |
| 132 | xfs_inofree_t freemask) |
| 133 | { |
| 134 | BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64)); |
| 135 | return hweight64(freemask); |
| 136 | } |
| 137 | |
| 138 | /* Check a particular inode with ir_free. */ |
| 139 | STATIC int |
| 140 | xfs_scrub_iallocbt_check_cluster_freemask( |
| 141 | struct xfs_scrub_btree *bs, |
| 142 | xfs_ino_t fsino, |
| 143 | xfs_agino_t chunkino, |
| 144 | xfs_agino_t clusterino, |
| 145 | struct xfs_inobt_rec_incore *irec, |
| 146 | struct xfs_buf *bp) |
| 147 | { |
| 148 | struct xfs_dinode *dip; |
| 149 | struct xfs_mount *mp = bs->cur->bc_mp; |
| 150 | bool inode_is_free = false; |
| 151 | bool freemask_ok; |
| 152 | bool inuse; |
| 153 | int error = 0; |
| 154 | |
| 155 | if (xfs_scrub_should_terminate(bs->sc, &error)) |
| 156 | return error; |
| 157 | |
| 158 | dip = xfs_buf_offset(bp, clusterino * mp->m_sb.sb_inodesize); |
| 159 | if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC || |
| 160 | (dip->di_version >= 3 && |
| 161 | be64_to_cpu(dip->di_ino) != fsino + clusterino)) { |
| 162 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | if (irec->ir_free & XFS_INOBT_MASK(chunkino + clusterino)) |
| 167 | inode_is_free = true; |
| 168 | error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp, |
| 169 | fsino + clusterino, &inuse); |
| 170 | if (error == -ENODATA) { |
| 171 | /* Not cached, just read the disk buffer */ |
| 172 | freemask_ok = inode_is_free ^ !!(dip->di_mode); |
| 173 | if (!bs->sc->try_harder && !freemask_ok) |
| 174 | return -EDEADLOCK; |
| 175 | } else if (error < 0) { |
| 176 | /* |
| 177 | * Inode is only half assembled, or there was an IO error, |
| 178 | * or the verifier failed, so don't bother trying to check. |
| 179 | * The inode scrubber can deal with this. |
| 180 | */ |
| 181 | goto out; |
| 182 | } else { |
| 183 | /* Inode is all there. */ |
| 184 | freemask_ok = inode_is_free ^ inuse; |
| 185 | } |
| 186 | if (!freemask_ok) |
| 187 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 188 | out: |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /* Make sure the free mask is consistent with what the inodes think. */ |
| 193 | STATIC int |
| 194 | xfs_scrub_iallocbt_check_freemask( |
| 195 | struct xfs_scrub_btree *bs, |
| 196 | struct xfs_inobt_rec_incore *irec) |
| 197 | { |
| 198 | struct xfs_owner_info oinfo; |
| 199 | struct xfs_imap imap; |
| 200 | struct xfs_mount *mp = bs->cur->bc_mp; |
| 201 | struct xfs_dinode *dip; |
| 202 | struct xfs_buf *bp; |
| 203 | xfs_ino_t fsino; |
| 204 | xfs_agino_t nr_inodes; |
| 205 | xfs_agino_t agino; |
| 206 | xfs_agino_t chunkino; |
| 207 | xfs_agino_t clusterino; |
| 208 | xfs_agblock_t agbno; |
| 209 | int blks_per_cluster; |
| 210 | uint16_t holemask; |
| 211 | uint16_t ir_holemask; |
| 212 | int error = 0; |
| 213 | |
| 214 | /* Make sure the freemask matches the inode records. */ |
| 215 | blks_per_cluster = xfs_icluster_size_fsb(mp); |
| 216 | nr_inodes = XFS_OFFBNO_TO_AGINO(mp, blks_per_cluster, 0); |
| 217 | xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES); |
| 218 | |
| 219 | for (agino = irec->ir_startino; |
| 220 | agino < irec->ir_startino + XFS_INODES_PER_CHUNK; |
| 221 | agino += blks_per_cluster * mp->m_sb.sb_inopblock) { |
| 222 | fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_private.a.agno, agino); |
| 223 | chunkino = agino - irec->ir_startino; |
| 224 | agbno = XFS_AGINO_TO_AGBNO(mp, agino); |
| 225 | |
| 226 | /* Compute the holemask mask for this cluster. */ |
| 227 | for (clusterino = 0, holemask = 0; clusterino < nr_inodes; |
| 228 | clusterino += XFS_INODES_PER_HOLEMASK_BIT) |
| 229 | holemask |= XFS_INOBT_MASK((chunkino + clusterino) / |
| 230 | XFS_INODES_PER_HOLEMASK_BIT); |
| 231 | |
| 232 | /* The whole cluster must be a hole or not a hole. */ |
| 233 | ir_holemask = (irec->ir_holemask & holemask); |
| 234 | if (ir_holemask != holemask && ir_holemask != 0) { |
| 235 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | /* If any part of this is a hole, skip it. */ |
| 240 | if (ir_holemask) |
| 241 | continue; |
| 242 | |
| 243 | /* Grab the inode cluster buffer. */ |
| 244 | imap.im_blkno = XFS_AGB_TO_DADDR(mp, bs->cur->bc_private.a.agno, |
| 245 | agbno); |
| 246 | imap.im_len = XFS_FSB_TO_BB(mp, blks_per_cluster); |
| 247 | imap.im_boffset = 0; |
| 248 | |
| 249 | error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap, |
| 250 | &dip, &bp, 0, 0); |
| 251 | if (!xfs_scrub_btree_process_error(bs->sc, bs->cur, 0, &error)) |
| 252 | continue; |
| 253 | |
| 254 | /* Which inodes are free? */ |
| 255 | for (clusterino = 0; clusterino < nr_inodes; clusterino++) { |
| 256 | error = xfs_scrub_iallocbt_check_cluster_freemask(bs, |
| 257 | fsino, chunkino, clusterino, irec, bp); |
| 258 | if (error) { |
| 259 | xfs_trans_brelse(bs->cur->bc_tp, bp); |
| 260 | return error; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | xfs_trans_brelse(bs->cur->bc_tp, bp); |
| 265 | } |
| 266 | |
| 267 | return error; |
| 268 | } |
| 269 | |
| 270 | /* Scrub an inobt/finobt record. */ |
| 271 | STATIC int |
| 272 | xfs_scrub_iallocbt_rec( |
| 273 | struct xfs_scrub_btree *bs, |
| 274 | union xfs_btree_rec *rec) |
| 275 | { |
| 276 | struct xfs_mount *mp = bs->cur->bc_mp; |
| 277 | struct xfs_inobt_rec_incore irec; |
| 278 | uint64_t holes; |
| 279 | xfs_agnumber_t agno = bs->cur->bc_private.a.agno; |
| 280 | xfs_agino_t agino; |
| 281 | xfs_agblock_t agbno; |
| 282 | xfs_extlen_t len; |
| 283 | int holecount; |
| 284 | int i; |
| 285 | int error = 0; |
| 286 | unsigned int real_freecount; |
| 287 | uint16_t holemask; |
| 288 | |
| 289 | xfs_inobt_btrec_to_irec(mp, rec, &irec); |
| 290 | |
| 291 | if (irec.ir_count > XFS_INODES_PER_CHUNK || |
| 292 | irec.ir_freecount > XFS_INODES_PER_CHUNK) |
| 293 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 294 | |
| 295 | real_freecount = irec.ir_freecount + |
| 296 | (XFS_INODES_PER_CHUNK - irec.ir_count); |
| 297 | if (real_freecount != xfs_scrub_iallocbt_freecount(irec.ir_free)) |
| 298 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 299 | |
| 300 | agino = irec.ir_startino; |
| 301 | /* Record has to be properly aligned within the AG. */ |
| 302 | if (!xfs_verify_agino(mp, agno, agino) || |
| 303 | !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) { |
| 304 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 305 | goto out; |
| 306 | } |
| 307 | |
| 308 | /* Make sure this record is aligned to cluster and inoalignmnt size. */ |
| 309 | agbno = XFS_AGINO_TO_AGBNO(mp, irec.ir_startino); |
| 310 | if ((agbno & (xfs_ialloc_cluster_alignment(mp) - 1)) || |
| 311 | (agbno & (xfs_icluster_size_fsb(mp) - 1))) |
| 312 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 313 | |
| 314 | /* Handle non-sparse inodes */ |
| 315 | if (!xfs_inobt_issparse(irec.ir_holemask)) { |
| 316 | len = XFS_B_TO_FSB(mp, |
| 317 | XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize); |
| 318 | if (irec.ir_count != XFS_INODES_PER_CHUNK) |
| 319 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 320 | |
| 321 | if (!xfs_scrub_iallocbt_chunk(bs, &irec, agino, len)) |
| 322 | goto out; |
| 323 | goto check_freemask; |
| 324 | } |
| 325 | |
| 326 | /* Check each chunk of a sparse inode cluster. */ |
| 327 | holemask = irec.ir_holemask; |
| 328 | holecount = 0; |
| 329 | len = XFS_B_TO_FSB(mp, |
| 330 | XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize); |
| 331 | holes = ~xfs_inobt_irec_to_allocmask(&irec); |
| 332 | if ((holes & irec.ir_free) != holes || |
| 333 | irec.ir_freecount > irec.ir_count) |
| 334 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 335 | |
| 336 | for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) { |
| 337 | if (holemask & 1) |
| 338 | holecount += XFS_INODES_PER_HOLEMASK_BIT; |
| 339 | else if (!xfs_scrub_iallocbt_chunk(bs, &irec, agino, len)) |
| 340 | break; |
| 341 | holemask >>= 1; |
| 342 | agino += XFS_INODES_PER_HOLEMASK_BIT; |
| 343 | } |
| 344 | |
| 345 | if (holecount > XFS_INODES_PER_CHUNK || |
| 346 | holecount + irec.ir_count != XFS_INODES_PER_CHUNK) |
| 347 | xfs_scrub_btree_set_corrupt(bs->sc, bs->cur, 0); |
| 348 | |
| 349 | check_freemask: |
| 350 | error = xfs_scrub_iallocbt_check_freemask(bs, &irec); |
| 351 | if (error) |
| 352 | goto out; |
| 353 | |
| 354 | out: |
| 355 | return error; |
| 356 | } |
| 357 | |
| 358 | /* Scrub the inode btrees for some AG. */ |
| 359 | STATIC int |
| 360 | xfs_scrub_iallocbt( |
| 361 | struct xfs_scrub_context *sc, |
| 362 | xfs_btnum_t which) |
| 363 | { |
| 364 | struct xfs_btree_cur *cur; |
| 365 | struct xfs_owner_info oinfo; |
| 366 | |
| 367 | xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT); |
| 368 | cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur; |
| 369 | return xfs_scrub_btree(sc, cur, xfs_scrub_iallocbt_rec, &oinfo, NULL); |
| 370 | } |
| 371 | |
| 372 | int |
| 373 | xfs_scrub_inobt( |
| 374 | struct xfs_scrub_context *sc) |
| 375 | { |
| 376 | return xfs_scrub_iallocbt(sc, XFS_BTNUM_INO); |
| 377 | } |
| 378 | |
| 379 | int |
| 380 | xfs_scrub_finobt( |
| 381 | struct xfs_scrub_context *sc) |
| 382 | { |
| 383 | return xfs_scrub_iallocbt(sc, XFS_BTNUM_FINO); |
| 384 | } |
Darrick J. Wong | 2e6f275 | 2018-01-16 18:53:07 -0800 | [diff] [blame^] | 385 | |
| 386 | /* See if an inode btree has (or doesn't have) an inode chunk record. */ |
| 387 | static inline void |
| 388 | xfs_scrub_xref_inode_check( |
| 389 | struct xfs_scrub_context *sc, |
| 390 | xfs_agblock_t agbno, |
| 391 | xfs_extlen_t len, |
| 392 | struct xfs_btree_cur **icur, |
| 393 | bool should_have_inodes) |
| 394 | { |
| 395 | bool has_inodes; |
| 396 | int error; |
| 397 | |
| 398 | if (!(*icur)) |
| 399 | return; |
| 400 | |
| 401 | error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes); |
| 402 | if (!xfs_scrub_should_check_xref(sc, &error, icur)) |
| 403 | return; |
| 404 | if (has_inodes != should_have_inodes) |
| 405 | xfs_scrub_btree_xref_set_corrupt(sc, *icur, 0); |
| 406 | } |
| 407 | |
| 408 | /* xref check that the extent is not covered by inodes */ |
| 409 | void |
| 410 | xfs_scrub_xref_is_not_inode_chunk( |
| 411 | struct xfs_scrub_context *sc, |
| 412 | xfs_agblock_t agbno, |
| 413 | xfs_extlen_t len) |
| 414 | { |
| 415 | xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false); |
| 416 | xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false); |
| 417 | } |
| 418 | |
| 419 | /* xref check that the extent is covered by inodes */ |
| 420 | void |
| 421 | xfs_scrub_xref_is_inode_chunk( |
| 422 | struct xfs_scrub_context *sc, |
| 423 | xfs_agblock_t agbno, |
| 424 | xfs_extlen_t len) |
| 425 | { |
| 426 | xfs_scrub_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true); |
| 427 | } |