|
| 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 "scrub/xfs_scrub.h" |
| 34 | +#include "scrub/scrub.h" |
| 35 | +#include "scrub/common.h" |
| 36 | +#include "scrub/trace.h" |
| 37 | + |
| 38 | +/* |
| 39 | + * Set up scrub to check all the static metadata in each AG. |
| 40 | + * This means the SB, AGF, AGI, and AGFL headers. |
| 41 | + */ |
| 42 | +int |
| 43 | +xfs_scrub_setup_ag_header( |
| 44 | + struct xfs_scrub_context *sc, |
| 45 | + struct xfs_inode *ip) |
| 46 | +{ |
| 47 | + struct xfs_mount *mp = sc->mp; |
| 48 | + |
| 49 | + if (sc->sm->sm_agno >= mp->m_sb.sb_agcount || |
| 50 | + sc->sm->sm_ino || sc->sm->sm_gen) |
| 51 | + return -EINVAL; |
| 52 | + return xfs_scrub_setup_fs(sc, ip); |
| 53 | +} |
| 54 | + |
| 55 | +/* Superblock */ |
| 56 | + |
| 57 | +/* |
| 58 | + * Scrub the filesystem superblock. |
| 59 | + * |
| 60 | + * Note: We do /not/ attempt to check AG 0's superblock. Mount is |
| 61 | + * responsible for validating all the geometry information in sb 0, so |
| 62 | + * if the filesystem is capable of initiating online scrub, then clearly |
| 63 | + * sb 0 is ok and we can use its information to check everything else. |
| 64 | + */ |
| 65 | +int |
| 66 | +xfs_scrub_superblock( |
| 67 | + struct xfs_scrub_context *sc) |
| 68 | +{ |
| 69 | + struct xfs_mount *mp = sc->mp; |
| 70 | + struct xfs_buf *bp; |
| 71 | + struct xfs_dsb *sb; |
| 72 | + xfs_agnumber_t agno; |
| 73 | + uint32_t v2_ok; |
| 74 | + __be32 features_mask; |
| 75 | + int error; |
| 76 | + __be16 vernum_mask; |
| 77 | + |
| 78 | + agno = sc->sm->sm_agno; |
| 79 | + if (agno == 0) |
| 80 | + return 0; |
| 81 | + |
| 82 | + error = xfs_trans_read_buf(mp, sc->tp, mp->m_ddev_targp, |
| 83 | + XFS_AGB_TO_DADDR(mp, agno, XFS_SB_BLOCK(mp)), |
| 84 | + XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops); |
| 85 | + if (!xfs_scrub_process_error(sc, agno, XFS_SB_BLOCK(mp), &error)) |
| 86 | + return error; |
| 87 | + |
| 88 | + sb = XFS_BUF_TO_SBP(bp); |
| 89 | + |
| 90 | + /* |
| 91 | + * Verify the geometries match. Fields that are permanently |
| 92 | + * set by mkfs are checked; fields that can be updated later |
| 93 | + * (and are not propagated to backup superblocks) are preen |
| 94 | + * checked. |
| 95 | + */ |
| 96 | + if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize)) |
| 97 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 98 | + |
| 99 | + if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks)) |
| 100 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 101 | + |
| 102 | + if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks)) |
| 103 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 104 | + |
| 105 | + if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents)) |
| 106 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 107 | + |
| 108 | + if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid)) |
| 109 | + xfs_scrub_block_set_preen(sc, bp); |
| 110 | + |
| 111 | + if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart)) |
| 112 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 113 | + |
| 114 | + if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino)) |
| 115 | + xfs_scrub_block_set_preen(sc, bp); |
| 116 | + |
| 117 | + if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino)) |
| 118 | + xfs_scrub_block_set_preen(sc, bp); |
| 119 | + |
| 120 | + if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino)) |
| 121 | + xfs_scrub_block_set_preen(sc, bp); |
| 122 | + |
| 123 | + if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize)) |
| 124 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 125 | + |
| 126 | + if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks)) |
| 127 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 128 | + |
| 129 | + if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount)) |
| 130 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 131 | + |
| 132 | + if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks)) |
| 133 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 134 | + |
| 135 | + if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks)) |
| 136 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 137 | + |
| 138 | + /* Check sb_versionnum bits that are set at mkfs time. */ |
| 139 | + vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS | |
| 140 | + XFS_SB_VERSION_NUMBITS | |
| 141 | + XFS_SB_VERSION_ALIGNBIT | |
| 142 | + XFS_SB_VERSION_DALIGNBIT | |
| 143 | + XFS_SB_VERSION_SHAREDBIT | |
| 144 | + XFS_SB_VERSION_LOGV2BIT | |
| 145 | + XFS_SB_VERSION_SECTORBIT | |
| 146 | + XFS_SB_VERSION_EXTFLGBIT | |
| 147 | + XFS_SB_VERSION_DIRV2BIT); |
| 148 | + if ((sb->sb_versionnum & vernum_mask) != |
| 149 | + (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask)) |
| 150 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 151 | + |
| 152 | + /* Check sb_versionnum bits that can be set after mkfs time. */ |
| 153 | + vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT | |
| 154 | + XFS_SB_VERSION_NLINKBIT | |
| 155 | + XFS_SB_VERSION_QUOTABIT); |
| 156 | + if ((sb->sb_versionnum & vernum_mask) != |
| 157 | + (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask)) |
| 158 | + xfs_scrub_block_set_preen(sc, bp); |
| 159 | + |
| 160 | + if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize)) |
| 161 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 162 | + |
| 163 | + if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize)) |
| 164 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 165 | + |
| 166 | + if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock)) |
| 167 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 168 | + |
| 169 | + if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname))) |
| 170 | + xfs_scrub_block_set_preen(sc, bp); |
| 171 | + |
| 172 | + if (sb->sb_blocklog != mp->m_sb.sb_blocklog) |
| 173 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 174 | + |
| 175 | + if (sb->sb_sectlog != mp->m_sb.sb_sectlog) |
| 176 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 177 | + |
| 178 | + if (sb->sb_inodelog != mp->m_sb.sb_inodelog) |
| 179 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 180 | + |
| 181 | + if (sb->sb_inopblog != mp->m_sb.sb_inopblog) |
| 182 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 183 | + |
| 184 | + if (sb->sb_agblklog != mp->m_sb.sb_agblklog) |
| 185 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 186 | + |
| 187 | + if (sb->sb_rextslog != mp->m_sb.sb_rextslog) |
| 188 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 189 | + |
| 190 | + if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct) |
| 191 | + xfs_scrub_block_set_preen(sc, bp); |
| 192 | + |
| 193 | + /* |
| 194 | + * Skip the summary counters since we track them in memory anyway. |
| 195 | + * sb_icount, sb_ifree, sb_fdblocks, sb_frexents |
| 196 | + */ |
| 197 | + |
| 198 | + if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino)) |
| 199 | + xfs_scrub_block_set_preen(sc, bp); |
| 200 | + |
| 201 | + if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino)) |
| 202 | + xfs_scrub_block_set_preen(sc, bp); |
| 203 | + |
| 204 | + /* |
| 205 | + * Skip the quota flags since repair will force quotacheck. |
| 206 | + * sb_qflags |
| 207 | + */ |
| 208 | + |
| 209 | + if (sb->sb_flags != mp->m_sb.sb_flags) |
| 210 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 211 | + |
| 212 | + if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn) |
| 213 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 214 | + |
| 215 | + if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt)) |
| 216 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 217 | + |
| 218 | + if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit)) |
| 219 | + xfs_scrub_block_set_preen(sc, bp); |
| 220 | + |
| 221 | + if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width)) |
| 222 | + xfs_scrub_block_set_preen(sc, bp); |
| 223 | + |
| 224 | + if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog) |
| 225 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 226 | + |
| 227 | + if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog) |
| 228 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 229 | + |
| 230 | + if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize)) |
| 231 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 232 | + |
| 233 | + if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit)) |
| 234 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 235 | + |
| 236 | + /* Do we see any invalid bits in sb_features2? */ |
| 237 | + if (!xfs_sb_version_hasmorebits(&mp->m_sb)) { |
| 238 | + if (sb->sb_features2 != 0) |
| 239 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 240 | + } else { |
| 241 | + v2_ok = XFS_SB_VERSION2_OKBITS; |
| 242 | + if (XFS_SB_VERSION_NUM(&mp->m_sb) >= XFS_SB_VERSION_5) |
| 243 | + v2_ok |= XFS_SB_VERSION2_CRCBIT; |
| 244 | + |
| 245 | + if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok))) |
| 246 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 247 | + |
| 248 | + if (sb->sb_features2 != sb->sb_bad_features2) |
| 249 | + xfs_scrub_block_set_preen(sc, bp); |
| 250 | + } |
| 251 | + |
| 252 | + /* Check sb_features2 flags that are set at mkfs time. */ |
| 253 | + features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT | |
| 254 | + XFS_SB_VERSION2_PROJID32BIT | |
| 255 | + XFS_SB_VERSION2_CRCBIT | |
| 256 | + XFS_SB_VERSION2_FTYPE); |
| 257 | + if ((sb->sb_features2 & features_mask) != |
| 258 | + (cpu_to_be32(mp->m_sb.sb_features2) & features_mask)) |
| 259 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 260 | + |
| 261 | + /* Check sb_features2 flags that can be set after mkfs time. */ |
| 262 | + features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT); |
| 263 | + if ((sb->sb_features2 & features_mask) != |
| 264 | + (cpu_to_be32(mp->m_sb.sb_features2) & features_mask)) |
| 265 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 266 | + |
| 267 | + if (!xfs_sb_version_hascrc(&mp->m_sb)) { |
| 268 | + /* all v5 fields must be zero */ |
| 269 | + if (memchr_inv(&sb->sb_features_compat, 0, |
| 270 | + sizeof(struct xfs_dsb) - |
| 271 | + offsetof(struct xfs_dsb, sb_features_compat))) |
| 272 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 273 | + } else { |
| 274 | + /* Check compat flags; all are set at mkfs time. */ |
| 275 | + features_mask = cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN); |
| 276 | + if ((sb->sb_features_compat & features_mask) != |
| 277 | + (cpu_to_be32(mp->m_sb.sb_features_compat) & features_mask)) |
| 278 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 279 | + |
| 280 | + /* Check ro compat flags; all are set at mkfs time. */ |
| 281 | + features_mask = cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN | |
| 282 | + XFS_SB_FEAT_RO_COMPAT_FINOBT | |
| 283 | + XFS_SB_FEAT_RO_COMPAT_RMAPBT | |
| 284 | + XFS_SB_FEAT_RO_COMPAT_REFLINK); |
| 285 | + if ((sb->sb_features_ro_compat & features_mask) != |
| 286 | + (cpu_to_be32(mp->m_sb.sb_features_ro_compat) & |
| 287 | + features_mask)) |
| 288 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 289 | + |
| 290 | + /* Check incompat flags; all are set at mkfs time. */ |
| 291 | + features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN | |
| 292 | + XFS_SB_FEAT_INCOMPAT_FTYPE | |
| 293 | + XFS_SB_FEAT_INCOMPAT_SPINODES | |
| 294 | + XFS_SB_FEAT_INCOMPAT_META_UUID); |
| 295 | + if ((sb->sb_features_incompat & features_mask) != |
| 296 | + (cpu_to_be32(mp->m_sb.sb_features_incompat) & |
| 297 | + features_mask)) |
| 298 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 299 | + |
| 300 | + /* Check log incompat flags; all are set at mkfs time. */ |
| 301 | + features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN); |
| 302 | + if ((sb->sb_features_log_incompat & features_mask) != |
| 303 | + (cpu_to_be32(mp->m_sb.sb_features_log_incompat) & |
| 304 | + features_mask)) |
| 305 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 306 | + |
| 307 | + /* Don't care about sb_crc */ |
| 308 | + |
| 309 | + if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align)) |
| 310 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 311 | + |
| 312 | + if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino)) |
| 313 | + xfs_scrub_block_set_preen(sc, bp); |
| 314 | + |
| 315 | + /* Don't care about sb_lsn */ |
| 316 | + } |
| 317 | + |
| 318 | + if (xfs_sb_version_hasmetauuid(&mp->m_sb)) { |
| 319 | + /* The metadata UUID must be the same for all supers */ |
| 320 | + if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid)) |
| 321 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 322 | + } |
| 323 | + |
| 324 | + /* Everything else must be zero. */ |
| 325 | + if (memchr_inv(sb + 1, 0, |
| 326 | + BBTOB(bp->b_length) - sizeof(struct xfs_dsb))) |
| 327 | + xfs_scrub_block_set_corrupt(sc, bp); |
| 328 | + |
| 329 | + return error; |
| 330 | +} |
0 commit comments