LCOV - code coverage report
Current view: top level - fs/xfs/libxfs - xfs_sb.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc4-xfsa @ Mon Jul 31 20:08:27 PDT 2023 Lines: 627 721 87.0 %
Date: 2023-07-31 20:08:27 Functions: 23 24 95.8 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : /*
       3             :  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
       4             :  * All Rights Reserved.
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "xfs_log_format.h"
      11             : #include "xfs_trans_resv.h"
      12             : #include "xfs_bit.h"
      13             : #include "xfs_sb.h"
      14             : #include "xfs_mount.h"
      15             : #include "xfs_ialloc.h"
      16             : #include "xfs_alloc.h"
      17             : #include "xfs_error.h"
      18             : #include "xfs_trans.h"
      19             : #include "xfs_buf_item.h"
      20             : #include "xfs_bmap_btree.h"
      21             : #include "xfs_alloc_btree.h"
      22             : #include "xfs_log.h"
      23             : #include "xfs_rmap_btree.h"
      24             : #include "xfs_refcount_btree.h"
      25             : #include "xfs_da_format.h"
      26             : #include "xfs_health.h"
      27             : #include "xfs_ag.h"
      28             : #include "xfs_swapext.h"
      29             : #include "xfs_rtgroup.h"
      30             : #include "xfs_rtrmap_btree.h"
      31             : #include "xfs_rtrefcount_btree.h"
      32             : 
      33             : /*
      34             :  * Physical superblock buffer manipulations. Shared with libxfs in userspace.
      35             :  */
      36             : 
      37             : /*
      38             :  * Check that all the V4 feature bits that the V5 filesystem format requires are
      39             :  * correctly set.
      40             :  */
      41             : static bool
      42      457637 : xfs_sb_validate_v5_features(
      43             :         struct xfs_sb   *sbp)
      44             : {
      45             :         /* We must not have any unknown V4 feature bits set */
      46      457637 :         if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
      47             :                 return false;
      48             : 
      49             :         /*
      50             :          * The CRC bit is considered an invalid V4 flag, so we have to add it
      51             :          * manually to the OKBITS mask.
      52             :          */
      53      457637 :         if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
      54             :                                   XFS_SB_VERSION2_CRCBIT))
      55             :                 return false;
      56             : 
      57             :         /* Now check all the required V4 feature flags are set. */
      58             : 
      59             : #define V5_VERS_FLAGS   (XFS_SB_VERSION_NLINKBIT        | \
      60             :                         XFS_SB_VERSION_ALIGNBIT         | \
      61             :                         XFS_SB_VERSION_LOGV2BIT         | \
      62             :                         XFS_SB_VERSION_EXTFLGBIT        | \
      63             :                         XFS_SB_VERSION_DIRV2BIT         | \
      64             :                         XFS_SB_VERSION_MOREBITSBIT)
      65             : 
      66             : #define V5_FEAT_FLAGS   (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
      67             :                         XFS_SB_VERSION2_ATTR2BIT        | \
      68             :                         XFS_SB_VERSION2_PROJID32BIT     | \
      69             :                         XFS_SB_VERSION2_CRCBIT)
      70             : 
      71      457637 :         if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
      72             :                 return false;
      73      457637 :         if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
      74           0 :                 return false;
      75             :         return true;
      76             : }
      77             : 
      78             : /*
      79             :  * We current support XFS v5 formats with known features and v4 superblocks with
      80             :  * at least V2 directories.
      81             :  */
      82             : bool
      83      462347 : xfs_sb_good_version(
      84             :         struct xfs_sb   *sbp)
      85             : {
      86             :         /*
      87             :          * All v5 filesystems are supported, but we must check that all the
      88             :          * required v4 feature flags are enabled correctly as the code checks
      89             :          * those flags and not for v5 support.
      90             :          */
      91      462347 :         if (xfs_sb_is_v5(sbp))
      92      457637 :                 return xfs_sb_validate_v5_features(sbp);
      93             : 
      94             :         /* versions prior to v4 are not supported */
      95        4710 :         if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
      96             :                 return false;
      97             : 
      98             :         /* We must not have any unknown v4 feature bits set */
      99        4710 :         if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
     100        4710 :             ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
     101        4710 :              (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
     102             :                 return false;
     103             : 
     104             :         /* V4 filesystems need v2 directories and unwritten extents */
     105        4710 :         if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
     106             :                 return false;
     107        4710 :         if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
     108           0 :                 return false;
     109             : 
     110             :         /* It's a supported v4 filesystem */
     111             :         return true;
     112             : }
     113             : 
     114             : uint64_t
     115       35728 : xfs_sb_version_to_features(
     116             :         struct xfs_sb   *sbp)
     117             : {
     118       35728 :         uint64_t        features = 0;
     119             : 
     120             :         /* optional V4 features */
     121       35728 :         if (sbp->sb_rblocks > 0)
     122         241 :                 features |= XFS_FEAT_REALTIME;
     123       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
     124       35728 :                 features |= XFS_FEAT_NLINK;
     125       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
     126       35728 :                 features |= XFS_FEAT_ATTR;
     127       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
     128       29116 :                 features |= XFS_FEAT_QUOTA;
     129       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
     130       35728 :                 features |= XFS_FEAT_ALIGN;
     131       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
     132       35718 :                 features |= XFS_FEAT_LOGV2;
     133       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
     134         114 :                 features |= XFS_FEAT_DALIGN;
     135       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
     136       35728 :                 features |= XFS_FEAT_EXTFLG;
     137       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
     138       35404 :                 features |= XFS_FEAT_SECTOR;
     139       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
     140          26 :                 features |= XFS_FEAT_ASCIICI;
     141       35728 :         if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
     142       35728 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
     143       35722 :                         features |= XFS_FEAT_LAZYSBCOUNT;
     144       35728 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
     145       35722 :                         features |= XFS_FEAT_ATTR2;
     146       35728 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
     147       35722 :                         features |= XFS_FEAT_PROJID32;
     148       35728 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
     149          30 :                         features |= XFS_FEAT_FTYPE;
     150             :         }
     151             : 
     152       35728 :         if (!xfs_sb_is_v5(sbp))
     153             :                 return features;
     154             : 
     155             :         /* Always on V5 features */
     156       35676 :         features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
     157             :                     XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
     158             :                     XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
     159             : 
     160             :         /* Optional V5 features */
     161       35676 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
     162       35670 :                 features |= XFS_FEAT_FINOBT;
     163       35676 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
     164       35646 :                 features |= XFS_FEAT_RMAPBT;
     165       35676 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
     166       35641 :                 features |= XFS_FEAT_REFLINK;
     167       35676 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
     168       35647 :                 features |= XFS_FEAT_INOBTCNT;
     169       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
     170       35676 :                 features |= XFS_FEAT_FTYPE;
     171       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
     172       35676 :                 features |= XFS_FEAT_SPINODES;
     173       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
     174          18 :                 features |= XFS_FEAT_META_UUID;
     175       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
     176       35641 :                 features |= XFS_FEAT_BIGTIME;
     177       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
     178           4 :                 features |= XFS_FEAT_NEEDSREPAIR;
     179       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
     180       35674 :                 features |= XFS_FEAT_NREXT64;
     181       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_PARENT)
     182       35038 :                 features |= XFS_FEAT_PARENT;
     183       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)
     184       35045 :                 features |= XFS_FEAT_METADIR;
     185       35676 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS)
     186       35039 :                 features |= XFS_FEAT_RTGROUPS;
     187             : 
     188             :         return features;
     189             : }
     190             : 
     191             : /* Check all the superblock fields we care about when reading one in. */
     192             : STATIC int
     193      171121 : xfs_validate_sb_read(
     194             :         struct xfs_mount        *mp,
     195             :         struct xfs_sb           *sbp)
     196             : {
     197      171121 :         if (!xfs_sb_is_v5(sbp))
     198             :                 return 0;
     199             : 
     200             :         /*
     201             :          * Version 5 superblock feature mask validation. Reject combinations
     202             :          * the kernel cannot support up front before checking anything else.
     203             :          */
     204      171067 :         if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
     205           0 :                 xfs_warn(mp,
     206             : "Superblock has unknown compatible features (0x%x) enabled.",
     207             :                         (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
     208           0 :                 xfs_warn(mp,
     209             : "Using a more recent kernel is recommended.");
     210             :         }
     211             : 
     212      171067 :         if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
     213           4 :                 xfs_alert(mp,
     214             : "Superblock has unknown read-only compatible features (0x%x) enabled.",
     215             :                         (sbp->sb_features_ro_compat &
     216             :                                         XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
     217           8 :                 if (!xfs_is_readonly(mp)) {
     218           2 :                         xfs_warn(mp,
     219             : "Attempted to mount read-only compatible filesystem read-write.");
     220           2 :                         xfs_warn(mp,
     221             : "Filesystem can only be safely mounted read only.");
     222             : 
     223           2 :                         return -EINVAL;
     224             :                 }
     225             :         }
     226      171065 :         if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
     227           0 :                 xfs_warn(mp,
     228             : "Superblock has unknown incompatible features (0x%x) enabled.",
     229             :                         (sbp->sb_features_incompat &
     230             :                                         XFS_SB_FEAT_INCOMPAT_UNKNOWN));
     231           0 :                 xfs_warn(mp,
     232             : "Filesystem cannot be safely mounted by this kernel.");
     233           0 :                 return -EINVAL;
     234             :         }
     235             : 
     236             :         return 0;
     237             : }
     238             : 
     239             : /* Check all the superblock fields we care about when writing one out. */
     240             : STATIC int
     241      291203 : xfs_validate_sb_write(
     242             :         struct xfs_mount        *mp,
     243             :         struct xfs_buf          *bp,
     244             :         struct xfs_sb           *sbp)
     245             : {
     246             :         /*
     247             :          * Carry out additional sb summary counter sanity checks when we write
     248             :          * the superblock.  We skip this in the read validator because there
     249             :          * could be newer superblocks in the log and if the values are garbage
     250             :          * even after replay we'll recalculate them at the end of log mount.
     251             :          *
     252             :          * mkfs has traditionally written zeroed counters to inprogress and
     253             :          * secondary superblocks, so allow this usage to continue because
     254             :          * we never read counters from such superblocks.
     255             :          */
     256      291203 :         if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
     257      541820 :             (sbp->sb_fdblocks > sbp->sb_dblocks ||
     258      270909 :              !xfs_verify_icount(mp, sbp->sb_icount) ||
     259      270909 :              sbp->sb_ifree > sbp->sb_icount)) {
     260           2 :                 xfs_warn(mp, "SB summary counter sanity check failed");
     261           2 :                 return -EFSCORRUPTED;
     262             :         }
     263             : 
     264      291201 :         if (!xfs_sb_is_v5(sbp))
     265             :                 return 0;
     266             : 
     267             :         /*
     268             :          * Version 5 superblock feature mask validation. Reject combinations
     269             :          * the kernel cannot support since we checked for unsupported bits in
     270             :          * the read verifier, which means that memory is corrupt.
     271             :          */
     272      286545 :         if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
     273           0 :                 xfs_warn(mp,
     274             : "Corruption detected in superblock compatible features (0x%x)!",
     275             :                         (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
     276           0 :                 return -EFSCORRUPTED;
     277             :         }
     278             : 
     279      286545 :         if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
     280           2 :                 xfs_alert(mp,
     281             : "Corruption detected in superblock read-only compatible features (0x%x)!",
     282             :                         (sbp->sb_features_ro_compat &
     283             :                                         XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
     284           2 :                 return -EFSCORRUPTED;
     285             :         }
     286      286543 :         if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
     287           0 :                 xfs_warn(mp,
     288             : "Corruption detected in superblock incompatible features (0x%x)!",
     289             :                         (sbp->sb_features_incompat &
     290             :                                         XFS_SB_FEAT_INCOMPAT_UNKNOWN));
     291           0 :                 return -EFSCORRUPTED;
     292             :         }
     293      286543 :         if (xfs_sb_has_incompat_log_feature(sbp,
     294             :                         XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
     295           0 :                 xfs_warn(mp,
     296             : "Corruption detected in superblock incompatible log features (0x%x)!",
     297             :                         (sbp->sb_features_log_incompat &
     298             :                                         XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
     299           0 :                 return -EFSCORRUPTED;
     300             :         }
     301             : 
     302             :         /*
     303             :          * We can't read verify the sb LSN because the read verifier is called
     304             :          * before the log is allocated and processed. We know the log is set up
     305             :          * before write verifier calls, so check it here.
     306             :          */
     307      286543 :         if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
     308           0 :                 return -EFSCORRUPTED;
     309             : 
     310             :         return 0;
     311             : }
     312             : 
     313             : static int
     314      453761 : xfs_validate_sb_rtgroups(
     315             :         struct xfs_mount        *mp,
     316             :         struct xfs_sb           *sbp)
     317             : {
     318      453761 :         uint64_t                groups;
     319             : 
     320      453761 :         if (!(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
     321           0 :                 xfs_warn(mp,
     322             : "Realtime groups require metadata directory tree.");
     323           0 :                 return -EINVAL;
     324             :         }
     325             : 
     326      453761 :         if (sbp->sb_rgblocks > XFS_MAX_RGBLOCKS) {
     327           0 :                 xfs_warn(mp,
     328             : "Realtime group size (%u) must be less than %u.",
     329             :                          sbp->sb_rgblocks, XFS_MAX_RGBLOCKS);
     330           0 :                 return -EINVAL;
     331             :         }
     332             : 
     333      453761 :         if (sbp->sb_rextsize == 0) {
     334           0 :                 xfs_warn(mp,
     335             : "Realtime extent size must not be zero.");
     336           0 :                 return -EINVAL;
     337             :         }
     338             : 
     339      453761 :         if (sbp->sb_rgblocks % sbp->sb_rextsize != 0) {
     340           0 :                 xfs_warn(mp,
     341             : "Realtime group size (%u) must be an even multiple of extent size (%u).",
     342             :                          sbp->sb_rgblocks, sbp->sb_rextsize);
     343           0 :                 return -EINVAL;
     344             :         }
     345             : 
     346      453761 :         if (sbp->sb_rgblocks < (sbp->sb_rextsize << 1)) {
     347           0 :                 xfs_warn(mp,
     348             : "Realtime group size (%u) must be greater than 1 rt extent.",
     349             :                          sbp->sb_rgblocks);
     350           0 :                 return -EINVAL;
     351             :         }
     352             : 
     353      453761 :         if (sbp->sb_rgcount > XFS_MAX_RGNUMBER) {
     354             :                 xfs_warn(mp,
     355             : "Realtime groups (%u) must be less than %u.",
     356             :                          sbp->sb_rgcount, XFS_MAX_RGNUMBER);
     357             :                 return -EINVAL;
     358             :         }
     359             : 
     360      453761 :         groups = howmany_64(sbp->sb_rblocks, sbp->sb_rgblocks);
     361      453761 :         if (groups != sbp->sb_rgcount) {
     362           0 :                 xfs_warn(mp,
     363             : "Realtime groups (%u) do not cover the entire rt section; need (%llu) groups.",
     364             :                         sbp->sb_rgcount, groups);
     365           0 :                 return -EINVAL;
     366             :         }
     367             : 
     368             :         return 0;
     369             : }
     370             : 
     371             : /* Check the validity of the SB. */
     372             : STATIC int
     373      462349 : xfs_validate_sb_common(
     374             :         struct xfs_mount        *mp,
     375             :         struct xfs_buf          *bp,
     376             :         struct xfs_sb           *sbp)
     377             : {
     378      462349 :         struct xfs_dsb          *dsb = bp->b_addr;
     379      462349 :         uint32_t                agcount = 0;
     380      462349 :         uint32_t                rem;
     381      462349 :         bool                    has_dalign;
     382      462349 :         int                     error;
     383             : 
     384      462349 :         if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
     385           2 :                 xfs_warn(mp,
     386             : "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
     387             :                         be32_to_cpu(dsb->sb_magicnum));
     388           2 :                 return -EWRONGFS;
     389             :         }
     390             : 
     391      462348 :         if (!xfs_sb_good_version(sbp)) {
     392           0 :                 xfs_warn(mp,
     393             : "Superblock has unknown features enabled or corrupted feature masks.");
     394           0 :                 return -EWRONGFS;
     395             :         }
     396             : 
     397             :         /*
     398             :          * Validate feature flags and state
     399             :          */
     400      462348 :         if (xfs_sb_is_v5(sbp)) {
     401      457637 :                 if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
     402           0 :                         xfs_notice(mp,
     403             : "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
     404             :                                 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
     405           0 :                         return -EFSCORRUPTED;
     406             :                 }
     407             : 
     408             :                 /* V5 has a separate project quota inode */
     409      457637 :                 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
     410           0 :                         xfs_notice(mp,
     411             :                            "Version 5 of Super block has XFS_OQUOTA bits.");
     412           0 :                         return -EFSCORRUPTED;
     413             :                 }
     414             : 
     415             :                 /*
     416             :                  * Full inode chunks must be aligned to inode chunk size when
     417             :                  * sparse inodes are enabled to support the sparse chunk
     418             :                  * allocation algorithm and prevent overlapping inode records.
     419             :                  */
     420      457637 :                 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
     421      457638 :                         uint32_t        align;
     422             : 
     423      457638 :                         align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
     424      457638 :                                         >> sbp->sb_blocklog;
     425      457638 :                         if (sbp->sb_inoalignmt != align) {
     426           0 :                                 xfs_warn(mp,
     427             : "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
     428             :                                          sbp->sb_inoalignmt, align);
     429           0 :                                 return -EINVAL;
     430             :                         }
     431             :                 }
     432             : 
     433      457637 :                 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     434      453761 :                         error = xfs_validate_sb_rtgroups(mp, sbp);
     435      453761 :                         if (error)
     436             :                                 return error;
     437             :                 }
     438        4711 :         } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
     439             :                                 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
     440           0 :                         xfs_notice(mp,
     441             : "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
     442           0 :                         return -EFSCORRUPTED;
     443             :         }
     444             : 
     445      462348 :         if (unlikely(
     446             :             sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
     447           2 :                 xfs_warn(mp,
     448             :                 "filesystem is marked as having an external log; "
     449             :                 "specify logdev on the mount command line.");
     450           2 :                 return -EINVAL;
     451             :         }
     452             : 
     453      462346 :         if (unlikely(
     454             :             sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
     455           2 :                 xfs_warn(mp,
     456             :                 "filesystem is marked as having an internal log; "
     457             :                 "do not specify logdev on the mount command line.");
     458           2 :                 return -EINVAL;
     459             :         }
     460             : 
     461             :         /* Compute agcount for this number of dblocks and agblocks */
     462      462344 :         if (sbp->sb_agblocks) {
     463      462343 :                 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
     464      462343 :                 if (rem)
     465       14570 :                         agcount++;
     466             :         }
     467             : 
     468             :         /*
     469             :          * More sanity checking.  Most of these were stolen directly from
     470             :          * xfs_repair.
     471             :          */
     472      924686 :         if (unlikely(
     473             :             sbp->sb_agcount <= 0                                  ||
     474             :             sbp->sb_sectsize < XFS_MIN_SECTORSIZE                 ||
     475             :             sbp->sb_sectsize > XFS_MAX_SECTORSIZE                 ||
     476             :             sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG                      ||
     477             :             sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG                      ||
     478             :             sbp->sb_sectsize != (1 << sbp->sb_sectlog)                      ||
     479             :             sbp->sb_blocksize < XFS_MIN_BLOCKSIZE                 ||
     480             :             sbp->sb_blocksize > XFS_MAX_BLOCKSIZE                 ||
     481             :             sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG                      ||
     482             :             sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG                      ||
     483             :             sbp->sb_blocksize != (1 << sbp->sb_blocklog)            ||
     484             :             sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
     485             :             sbp->sb_inodesize < XFS_DINODE_MIN_SIZE                       ||
     486             :             sbp->sb_inodesize > XFS_DINODE_MAX_SIZE                       ||
     487             :             sbp->sb_inodelog < XFS_DINODE_MIN_LOG                 ||
     488             :             sbp->sb_inodelog > XFS_DINODE_MAX_LOG                 ||
     489             :             sbp->sb_inodesize != (1 << sbp->sb_inodelog)            ||
     490             :             sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
     491             :             XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
     492             :             XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
     493             :             sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1   ||
     494             :             agcount == 0 || agcount != sbp->sb_agcount                       ||
     495             :             (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)  ||
     496             :             (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
     497             :             (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
     498             :             (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)      ||
     499             :             sbp->sb_dblocks == 0                                     ||
     500             :             sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)                        ||
     501             :             sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp)                        ||
     502             :             sbp->sb_shared_vn != 0)) {
     503          18 :                 xfs_notice(mp, "SB sanity check failed");
     504          18 :                 return -EFSCORRUPTED;
     505             :         }
     506             : 
     507             :         /*
     508             :          * Logs that are too large are not supported at all. Reject them
     509             :          * outright. Logs that are too small are tolerated on v4 filesystems,
     510             :          * but we can only check that when mounting the log. Hence we skip
     511             :          * those checks here.
     512             :          */
     513      462326 :         if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) {
     514           0 :                 xfs_notice(mp,
     515             :                 "Log size 0x%x blocks too large, maximum size is 0x%llx blocks",
     516             :                          sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS);
     517           0 :                 return -EFSCORRUPTED;
     518             :         }
     519             : 
     520      462326 :         if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) {
     521           0 :                 xfs_warn(mp,
     522             :                 "log size 0x%llx bytes too large, maximum size is 0x%llx bytes",
     523             :                          XFS_FSB_TO_B(mp, sbp->sb_logblocks),
     524             :                          XFS_MAX_LOG_BYTES);
     525           0 :                 return -EFSCORRUPTED;
     526             :         }
     527             : 
     528             :         /*
     529             :          * Do not allow filesystems with corrupted log sector or stripe units to
     530             :          * be mounted. We cannot safely size the iclogs or write to the log if
     531             :          * the log stripe unit is not valid.
     532             :          */
     533      462326 :         if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
     534      459486 :                 if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) {
     535           0 :                         xfs_notice(mp,
     536             :                         "log sector size in bytes/log2 (0x%x/0x%x) must match",
     537             :                                 sbp->sb_logsectsize, 1U << sbp->sb_logsectlog);
     538           0 :                         return -EFSCORRUPTED;
     539             :                 }
     540        2840 :         } else if (sbp->sb_logsectsize || sbp->sb_logsectlog) {
     541           0 :                 xfs_notice(mp,
     542             :                 "log sector size in bytes/log2 (0x%x/0x%x) are not zero",
     543             :                         sbp->sb_logsectsize, sbp->sb_logsectlog);
     544           0 :                 return -EFSCORRUPTED;
     545             :         }
     546             : 
     547      462326 :         if (sbp->sb_logsunit > 1) {
     548      459733 :                 if (sbp->sb_logsunit % sbp->sb_blocksize) {
     549           2 :                         xfs_notice(mp,
     550             :                 "log stripe unit 0x%x bytes must be a multiple of block size",
     551             :                                 sbp->sb_logsunit);
     552           2 :                         return -EFSCORRUPTED;
     553             :                 }
     554      459731 :                 if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) {
     555           0 :                         xfs_notice(mp,
     556             :                 "log stripe unit 0x%x bytes over maximum size (0x%x bytes)",
     557             :                                 sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE);
     558           0 :                         return -EFSCORRUPTED;
     559             :                 }
     560             :         }
     561             : 
     562             :         /* Validate the realtime geometry; stolen from xfs_repair */
     563      462324 :         if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
     564             :             sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
     565           0 :                 xfs_notice(mp,
     566             :                         "realtime extent sanity check failed");
     567           0 :                 return -EFSCORRUPTED;
     568             :         }
     569             : 
     570      462324 :         if (sbp->sb_rblocks == 0) {
     571      299788 :                 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
     572      299788 :                     sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
     573           0 :                         xfs_notice(mp,
     574             :                                 "realtime zeroed geometry check failed");
     575           0 :                         return -EFSCORRUPTED;
     576             :                 }
     577             :         } else {
     578      162536 :                 uint64_t        rexts;
     579      162536 :                 uint64_t        rbmblocks;
     580      162536 :                 unsigned int    rbmblock_bytes = sbp->sb_blocksize;
     581             : 
     582      162536 :                 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
     583             : 
     584      162536 :                 if (xfs_sb_is_v5(sbp) &&
     585      162536 :                     (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS))
     586      162528 :                         rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo);
     587             : 
     588      162536 :                 rbmblocks = howmany_64(sbp->sb_rextents, NBBY * rbmblock_bytes);
     589             : 
     590      325072 :                 if (sbp->sb_rextents != rexts ||
     591      162536 :                     sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
     592      162536 :                     sbp->sb_rbmblocks != rbmblocks) {
     593           0 :                         xfs_notice(mp,
     594             :                                 "realtime geometry sanity check failed");
     595           0 :                         return -EFSCORRUPTED;
     596             :                 }
     597             :         }
     598             : 
     599             :         /*
     600             :          * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
     601             :          * would imply the image is corrupted.
     602             :          */
     603      462324 :         has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
     604      462324 :         if (!!sbp->sb_unit ^ has_dalign) {
     605           0 :                 xfs_notice(mp, "SB stripe alignment sanity check failed");
     606           0 :                 return -EFSCORRUPTED;
     607             :         }
     608             : 
     609      462322 :         if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
     610      462324 :                         XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
     611             :                 return -EFSCORRUPTED;
     612             : 
     613             :         /*
     614             :          * Currently only very few inode sizes are supported.
     615             :          */
     616      462322 :         switch (sbp->sb_inodesize) {
     617             :         case 256:
     618             :         case 512:
     619             :         case 1024:
     620             :         case 2048:
     621             :                 break;
     622           0 :         default:
     623           0 :                 xfs_warn(mp, "inode size of %d bytes not supported",
     624             :                                 sbp->sb_inodesize);
     625           0 :                 return -ENOSYS;
     626             :         }
     627             : 
     628             :         return 0;
     629             : }
     630             : 
     631             : void
     632       60101 : xfs_sb_quota_from_disk(struct xfs_sb *sbp)
     633             : {
     634             :         /*
     635             :          * older mkfs doesn't initialize quota inodes to NULLFSINO. This
     636             :          * leads to in-core values having two different values for a quota
     637             :          * inode to be invalid: 0 and NULLFSINO. Change it to a single value
     638             :          * NULLFSINO.
     639             :          *
     640             :          * Note that this change affect only the in-core values. These
     641             :          * values are not written back to disk unless any quota information
     642             :          * is written to the disk. Even in that case, sb_pquotino field is
     643             :          * not written to disk unless the superblock supports pquotino.
     644             :          */
     645       60101 :         if (sbp->sb_uquotino == 0)
     646         702 :                 sbp->sb_uquotino = NULLFSINO;
     647       60101 :         if (sbp->sb_gquotino == 0)
     648         702 :                 sbp->sb_gquotino = NULLFSINO;
     649       60101 :         if (sbp->sb_pquotino == 0)
     650         766 :                 sbp->sb_pquotino = NULLFSINO;
     651             : 
     652             :         /*
     653             :          * We need to do these manipilations only if we are working
     654             :          * with an older version of on-disk superblock.
     655             :          */
     656       60101 :         if (xfs_sb_is_v5(sbp))
     657             :                 return;
     658             : 
     659         104 :         if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
     660          32 :                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
     661             :                                         XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
     662         104 :         if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
     663          32 :                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
     664             :                                         XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
     665         104 :         sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
     666             : 
     667         104 :         if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
     668          16 :             sbp->sb_gquotino != NULLFSINO)  {
     669             :                 /*
     670             :                  * In older version of superblock, on-disk superblock only
     671             :                  * has sb_gquotino, and in-core superblock has both sb_gquotino
     672             :                  * and sb_pquotino. But, only one of them is supported at any
     673             :                  * point of time. So, if PQUOTA is set in disk superblock,
     674             :                  * copy over sb_gquotino to sb_pquotino.  The NULLFSINO test
     675             :                  * above is to make sure we don't do this twice and wipe them
     676             :                  * both out!
     677             :                  */
     678          16 :                 sbp->sb_pquotino = sbp->sb_gquotino;
     679          16 :                 sbp->sb_gquotino = NULLFSINO;
     680             :         }
     681             : }
     682             : 
     683             : static void
     684      522451 : __xfs_sb_from_disk(
     685             :         struct xfs_sb   *to,
     686             :         struct xfs_dsb  *from,
     687             :         bool            convert_xquota)
     688             : {
     689      522451 :         to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
     690      522451 :         to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
     691      522451 :         to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
     692      522451 :         to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
     693      522451 :         to->sb_rextents = be64_to_cpu(from->sb_rextents);
     694     1044902 :         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
     695      522451 :         to->sb_logstart = be64_to_cpu(from->sb_logstart);
     696      522451 :         to->sb_rootino = be64_to_cpu(from->sb_rootino);
     697      522451 :         to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
     698      522451 :         to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
     699      522451 :         to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
     700      522451 :         to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
     701      522451 :         to->sb_agcount = be32_to_cpu(from->sb_agcount);
     702      522451 :         to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
     703      522451 :         to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
     704      522451 :         to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
     705      522451 :         to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
     706      522451 :         to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
     707      522451 :         to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
     708     1044902 :         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
     709      522451 :         to->sb_blocklog = from->sb_blocklog;
     710      522451 :         to->sb_sectlog = from->sb_sectlog;
     711      522451 :         to->sb_inodelog = from->sb_inodelog;
     712      522451 :         to->sb_inopblog = from->sb_inopblog;
     713      522451 :         to->sb_agblklog = from->sb_agblklog;
     714      522451 :         to->sb_rextslog = from->sb_rextslog;
     715      522451 :         to->sb_inprogress = from->sb_inprogress;
     716      522451 :         to->sb_imax_pct = from->sb_imax_pct;
     717      522451 :         to->sb_icount = be64_to_cpu(from->sb_icount);
     718      522451 :         to->sb_ifree = be64_to_cpu(from->sb_ifree);
     719      522451 :         to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
     720      522451 :         to->sb_frextents = be64_to_cpu(from->sb_frextents);
     721      522451 :         to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
     722      522451 :         to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
     723      522451 :         to->sb_qflags = be16_to_cpu(from->sb_qflags);
     724      522451 :         to->sb_flags = from->sb_flags;
     725      522451 :         to->sb_shared_vn = from->sb_shared_vn;
     726      522451 :         to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
     727      522451 :         to->sb_unit = be32_to_cpu(from->sb_unit);
     728      522451 :         to->sb_width = be32_to_cpu(from->sb_width);
     729      522451 :         to->sb_dirblklog = from->sb_dirblklog;
     730      522451 :         to->sb_logsectlog = from->sb_logsectlog;
     731      522451 :         to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
     732      522451 :         to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
     733      522451 :         to->sb_features2 = be32_to_cpu(from->sb_features2);
     734      522451 :         to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
     735      522451 :         to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
     736      522451 :         to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
     737      522451 :         to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
     738     1044902 :         to->sb_features_log_incompat =
     739      522451 :                                 be32_to_cpu(from->sb_features_log_incompat);
     740             :         /* crc is only used on disk, not in memory; just init to 0 here. */
     741      522451 :         to->sb_crc = 0;
     742      522451 :         to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
     743      522451 :         to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
     744      522451 :         to->sb_lsn = be64_to_cpu(from->sb_lsn);
     745             :         /*
     746             :          * sb_meta_uuid is only on disk if it differs from sb_uuid and the
     747             :          * feature flag is set; if not set we keep it only in memory.
     748             :          */
     749      522451 :         if (xfs_sb_is_v5(to) &&
     750      517636 :             (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
     751          90 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
     752             :         else
     753      522361 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
     754             :         /* Convert on-disk flags to in-memory flags? */
     755      522450 :         if (convert_xquota)
     756       60101 :                 xfs_sb_quota_from_disk(to);
     757             : 
     758      522450 :         if (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
     759             :                 /*
     760             :                  * Set metadirino here and null out the in-core fields for
     761             :                  * the other inodes because metadir initialization will load
     762             :                  * them later.
     763             :                  */
     764      512597 :                 to->sb_metadirino = be64_to_cpu(from->sb_rbmino);
     765      512597 :                 to->sb_rbmino = NULLFSINO;
     766      512597 :                 to->sb_rsumino = NULLFSINO;
     767             : 
     768             :                 /*
     769             :                  * We don't have to worry about quota inode conversion here
     770             :                  * because metadir requires a v5 filesystem.
     771             :                  */
     772      512597 :                 to->sb_uquotino = NULLFSINO;
     773      512597 :                 to->sb_gquotino = NULLFSINO;
     774      512597 :                 to->sb_pquotino = NULLFSINO;
     775             :         }
     776             : 
     777      522450 :         if (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     778      512545 :                 to->sb_rgcount = be32_to_cpu(from->sb_rgcount);
     779     1025090 :                 to->sb_rgblocks = be32_to_cpu(from->sb_rgblocks);
     780             :         } else {
     781        9905 :                 to->sb_rgcount = 0;
     782        9905 :                 to->sb_rgblocks = 0;
     783             :         }
     784      522450 : }
     785             : 
     786             : void
     787       60101 : xfs_sb_from_disk(
     788             :         struct xfs_sb   *to,
     789             :         struct xfs_dsb  *from)
     790             : {
     791       60101 :         __xfs_sb_from_disk(to, from, true);
     792       60101 : }
     793             : 
     794             : static void
     795      498027 : xfs_sb_quota_to_disk(
     796             :         struct xfs_dsb  *to,
     797             :         struct xfs_sb   *from)
     798             : {
     799      498027 :         uint16_t        qflags = from->sb_qflags;
     800             : 
     801      498027 :         to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
     802             : 
     803             :         /*
     804             :          * The in-memory superblock quota state matches the v5 on-disk format so
     805             :          * just write them out and return
     806             :          */
     807      498027 :         if (xfs_sb_is_v5(from)) {
     808      493335 :                 to->sb_qflags = cpu_to_be16(from->sb_qflags);
     809      493335 :                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
     810      493335 :                 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
     811      493335 :                 return;
     812             :         }
     813             : 
     814             :         /*
     815             :          * For older superblocks (v4), the in-core version of sb_qflags do not
     816             :          * have XFS_OQUOTA_* flags, whereas the on-disk version does.  So,
     817             :          * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
     818             :          */
     819        4692 :         qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
     820             :                         XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
     821             : 
     822        4692 :         if (from->sb_qflags &
     823             :                         (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
     824        4304 :                 qflags |= XFS_OQUOTA_ENFD;
     825        4692 :         if (from->sb_qflags &
     826             :                         (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
     827        4304 :                 qflags |= XFS_OQUOTA_CHKD;
     828        4692 :         to->sb_qflags = cpu_to_be16(qflags);
     829             : 
     830             :         /*
     831             :          * GQUOTINO and PQUOTINO cannot be used together in versions
     832             :          * of superblock that do not have pquotino. from->sb_flags
     833             :          * tells us which quota is active and should be copied to
     834             :          * disk. If neither are active, we should NULL the inode.
     835             :          *
     836             :          * In all cases, the separate pquotino must remain 0 because it
     837             :          * is beyond the "end" of the valid non-pquotino superblock.
     838             :          */
     839        4692 :         if (from->sb_qflags & XFS_GQUOTA_ACCT)
     840          12 :                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
     841        4680 :         else if (from->sb_qflags & XFS_PQUOTA_ACCT)
     842        4316 :                 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
     843             :         else {
     844             :                 /*
     845             :                  * We can't rely on just the fields being logged to tell us
     846             :                  * that it is safe to write NULLFSINO - we should only do that
     847             :                  * if quotas are not actually enabled. Hence only write
     848             :                  * NULLFSINO if both in-core quota inodes are NULL.
     849             :                  */
     850         364 :                 if (from->sb_gquotino == NULLFSINO &&
     851         362 :                     from->sb_pquotino == NULLFSINO)
     852         362 :                         to->sb_gquotino = cpu_to_be64(NULLFSINO);
     853             :         }
     854             : 
     855        4692 :         to->sb_pquotino = 0;
     856             : }
     857             : 
     858             : void
     859      498026 : xfs_sb_to_disk(
     860             :         struct xfs_dsb  *to,
     861             :         struct xfs_sb   *from)
     862             : {
     863      498026 :         xfs_sb_quota_to_disk(to, from);
     864             : 
     865      498028 :         to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
     866      498028 :         to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
     867      498028 :         to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
     868      498028 :         to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
     869      498028 :         to->sb_rextents = cpu_to_be64(from->sb_rextents);
     870      996056 :         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
     871      498028 :         to->sb_logstart = cpu_to_be64(from->sb_logstart);
     872      498028 :         to->sb_rootino = cpu_to_be64(from->sb_rootino);
     873      498028 :         to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
     874      498028 :         to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
     875      498028 :         to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
     876      498028 :         to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
     877      498028 :         to->sb_agcount = cpu_to_be32(from->sb_agcount);
     878      498028 :         to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
     879      498028 :         to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
     880      498028 :         to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
     881      498028 :         to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
     882      498028 :         to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
     883      498028 :         to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
     884      996056 :         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
     885      498028 :         to->sb_blocklog = from->sb_blocklog;
     886      498028 :         to->sb_sectlog = from->sb_sectlog;
     887      498028 :         to->sb_inodelog = from->sb_inodelog;
     888      498028 :         to->sb_inopblog = from->sb_inopblog;
     889      498028 :         to->sb_agblklog = from->sb_agblklog;
     890      498028 :         to->sb_rextslog = from->sb_rextslog;
     891      498028 :         to->sb_inprogress = from->sb_inprogress;
     892      498028 :         to->sb_imax_pct = from->sb_imax_pct;
     893      498028 :         to->sb_icount = cpu_to_be64(from->sb_icount);
     894      498028 :         to->sb_ifree = cpu_to_be64(from->sb_ifree);
     895      498028 :         to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
     896      498028 :         to->sb_frextents = cpu_to_be64(from->sb_frextents);
     897             : 
     898      498028 :         to->sb_flags = from->sb_flags;
     899      498028 :         to->sb_shared_vn = from->sb_shared_vn;
     900      498028 :         to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
     901      498028 :         to->sb_unit = cpu_to_be32(from->sb_unit);
     902      498028 :         to->sb_width = cpu_to_be32(from->sb_width);
     903      498028 :         to->sb_dirblklog = from->sb_dirblklog;
     904      498028 :         to->sb_logsectlog = from->sb_logsectlog;
     905      498028 :         to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
     906      498028 :         to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
     907             : 
     908             :         /*
     909             :          * We need to ensure that bad_features2 always matches features2.
     910             :          * Hence we enforce that here rather than having to remember to do it
     911             :          * everywhere else that updates features2.
     912             :          */
     913      498028 :         from->sb_bad_features2 = from->sb_features2;
     914      498028 :         to->sb_features2 = cpu_to_be32(from->sb_features2);
     915      498028 :         to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
     916             : 
     917      498028 :         if (!xfs_sb_is_v5(from))
     918             :                 return;
     919             : 
     920      493336 :         to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
     921      986672 :         to->sb_features_ro_compat =
     922      493336 :                         cpu_to_be32(from->sb_features_ro_compat);
     923      986672 :         to->sb_features_incompat =
     924      493336 :                         cpu_to_be32(from->sb_features_incompat);
     925      986672 :         to->sb_features_log_incompat =
     926      493336 :                         cpu_to_be32(from->sb_features_log_incompat);
     927      493336 :         to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
     928      493336 :         to->sb_lsn = cpu_to_be64(from->sb_lsn);
     929      493336 :         if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
     930          30 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
     931             : 
     932      493336 :         if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
     933             :                 /*
     934             :                  * Save metadirino here and null out the on-disk fields for
     935             :                  * the other inodes, at least until we reuse the fields.
     936             :                  */
     937      489995 :                 to->sb_rbmino = cpu_to_be64(from->sb_metadirino);
     938      489995 :                 to->sb_rsumino = cpu_to_be64(NULLFSINO);
     939      489995 :                 to->sb_uquotino = cpu_to_be64(NULLFSINO);
     940      489995 :                 to->sb_gquotino = cpu_to_be64(NULLFSINO);
     941      489995 :                 to->sb_pquotino = cpu_to_be64(NULLFSINO);
     942             :         }
     943             : 
     944      493336 :         if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     945             :                 /* must come after setting to_rsumino */
     946      489965 :                 to->sb_rgcount = cpu_to_be32(from->sb_rgcount);
     947      979930 :                 to->sb_rgblocks = cpu_to_be32(from->sb_rgblocks);
     948             :         }
     949             : }
     950             : 
     951             : /*
     952             :  * If the superblock has the CRC feature bit set or the CRC field is non-null,
     953             :  * check that the CRC is valid.  We check the CRC field is non-null because a
     954             :  * single bit error could clear the feature bit and unused parts of the
     955             :  * superblock are supposed to be zero. Hence a non-null crc field indicates that
     956             :  * we've potentially lost a feature bit and we should check it anyway.
     957             :  *
     958             :  * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
     959             :  * last field in V4 secondary superblocks.  So for secondary superblocks,
     960             :  * we are more forgiving, and ignore CRC failures if the primary doesn't
     961             :  * indicate that the fs version is V5.
     962             :  */
     963             : static void
     964      171151 : xfs_sb_read_verify(
     965             :         struct xfs_buf          *bp)
     966             : {
     967      171151 :         struct xfs_sb           sb;
     968      171151 :         struct xfs_mount        *mp = bp->b_mount;
     969      171151 :         struct xfs_dsb          *dsb = bp->b_addr;
     970      171151 :         int                     error;
     971             : 
     972             :         /*
     973             :          * open code the version check to avoid needing to convert the entire
     974             :          * superblock from disk order just to check the version number
     975             :          */
     976      171151 :         if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
     977      171149 :             (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
     978          54 :                                                 XFS_SB_VERSION_5) ||
     979          54 :              dsb->sb_crc != 0)) {
     980             : 
     981      171095 :                 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
     982             :                         /* Only fail bad secondaries on a known V5 filesystem */
     983           4 :                         if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
     984             :                             xfs_has_crc(mp)) {
     985           4 :                                 error = -EFSBADCRC;
     986           4 :                                 goto out_error;
     987             :                         }
     988             :                 }
     989             :         }
     990             : 
     991             :         /*
     992             :          * Check all the superblock fields.  Don't byteswap the xquota flags
     993             :          * because _verify_common checks the on-disk values.
     994             :          */
     995      171147 :         __xfs_sb_from_disk(&sb, dsb, false);
     996      171147 :         error = xfs_validate_sb_common(mp, bp, &sb);
     997      171147 :         if (error)
     998          26 :                 goto out_error;
     999      171121 :         error = xfs_validate_sb_read(mp, &sb);
    1000             : 
    1001      171151 : out_error:
    1002      171151 :         if (error == -EFSCORRUPTED || error == -EFSBADCRC)
    1003          24 :                 xfs_verifier_error(bp, error, __this_address);
    1004      171127 :         else if (error)
    1005           8 :                 xfs_buf_ioerror(bp, error);
    1006      171151 : }
    1007             : 
    1008             : /*
    1009             :  * We may be probed for a filesystem match, so we may not want to emit
    1010             :  * messages when the superblock buffer is not actually an XFS superblock.
    1011             :  * If we find an XFS superblock, then run a normal, noisy mount because we are
    1012             :  * really going to mount it and want to know about errors.
    1013             :  */
    1014             : static void
    1015           0 : xfs_sb_quiet_read_verify(
    1016             :         struct xfs_buf  *bp)
    1017             : {
    1018           0 :         struct xfs_dsb  *dsb = bp->b_addr;
    1019             : 
    1020           0 :         if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
    1021             :                 /* XFS filesystem, verify noisily! */
    1022           0 :                 xfs_sb_read_verify(bp);
    1023           0 :                 return;
    1024             :         }
    1025             :         /* quietly fail */
    1026           0 :         xfs_buf_ioerror(bp, -EWRONGFS);
    1027             : }
    1028             : 
    1029             : static void
    1030      291201 : xfs_sb_write_verify(
    1031             :         struct xfs_buf          *bp)
    1032             : {
    1033      291201 :         struct xfs_sb           sb;
    1034      291201 :         struct xfs_mount        *mp = bp->b_mount;
    1035      291201 :         struct xfs_buf_log_item *bip = bp->b_log_item;
    1036      291201 :         struct xfs_dsb          *dsb = bp->b_addr;
    1037      291201 :         int                     error;
    1038             : 
    1039             :         /*
    1040             :          * Check all the superblock fields.  Don't byteswap the xquota flags
    1041             :          * because _verify_common checks the on-disk values.
    1042             :          */
    1043      291201 :         __xfs_sb_from_disk(&sb, dsb, false);
    1044      291202 :         error = xfs_validate_sb_common(mp, bp, &sb);
    1045      291201 :         if (error)
    1046           0 :                 goto out_error;
    1047      291201 :         error = xfs_validate_sb_write(mp, bp, &sb);
    1048      291203 :         if (error)
    1049           4 :                 goto out_error;
    1050             : 
    1051      291199 :         if (!xfs_sb_is_v5(&sb))
    1052      291199 :                 return;
    1053             : 
    1054      286543 :         if (bip)
    1055      210191 :                 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
    1056             : 
    1057      286543 :         xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
    1058             :         return;
    1059             : 
    1060           4 : out_error:
    1061           4 :         xfs_verifier_error(bp, error, __this_address);
    1062             : }
    1063             : 
    1064             : const struct xfs_buf_ops xfs_sb_buf_ops = {
    1065             :         .name = "xfs_sb",
    1066             :         .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
    1067             :         .verify_read = xfs_sb_read_verify,
    1068             :         .verify_write = xfs_sb_write_verify,
    1069             : };
    1070             : 
    1071             : const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
    1072             :         .name = "xfs_sb_quiet",
    1073             :         .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
    1074             :         .verify_read = xfs_sb_quiet_read_verify,
    1075             :         .verify_write = xfs_sb_write_verify,
    1076             : };
    1077             : 
    1078             : /*
    1079             :  * xfs_mount_common
    1080             :  *
    1081             :  * Mount initialization code establishing various mount
    1082             :  * fields from the superblock associated with the given
    1083             :  * mount structure.
    1084             :  *
    1085             :  * Inode geometry are calculated in xfs_ialloc_setup_geometry.
    1086             :  */
    1087             : void
    1088       24343 : xfs_sb_mount_common(
    1089             :         struct xfs_mount        *mp,
    1090             :         struct xfs_sb           *sbp)
    1091             : {
    1092       24343 :         mp->m_agfrotor = 0;
    1093       24343 :         atomic_set(&mp->m_agirotor, 0);
    1094       24343 :         mp->m_maxagi = mp->m_sb.sb_agcount;
    1095       24343 :         mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
    1096       24343 :         mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
    1097       24343 :         mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
    1098       24343 :         mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
    1099       24343 :         mp->m_blockmask = sbp->sb_blocksize - 1;
    1100       24343 :         if (xfs_has_rtgroups(mp))
    1101       23728 :                 mp->m_blockwsize = (sbp->sb_blocksize -
    1102       23728 :                                         sizeof(struct xfs_rtbuf_blkinfo)) >>
    1103             :                                         XFS_WORDLOG;
    1104             :         else
    1105         615 :                 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
    1106       24343 :         mp->m_rtx_per_rbmblock = mp->m_blockwsize << XFS_NBWORDLOG;
    1107       24343 :         mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize);
    1108       24343 :         mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize);
    1109       24343 :         mp->m_rgblklog = log2_if_power2(sbp->sb_rgblocks);
    1110       24343 :         mp->m_rgblkmask = mask64_if_power2(sbp->sb_rgblocks);
    1111             : 
    1112       24343 :         mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, true);
    1113       24343 :         mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, false);
    1114       24343 :         mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
    1115       24343 :         mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
    1116             : 
    1117       24343 :         mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, true);
    1118       24343 :         mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, false);
    1119       24343 :         mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
    1120       24343 :         mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
    1121             : 
    1122       24343 :         mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, true);
    1123       24343 :         mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, false);
    1124       24343 :         mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
    1125       24343 :         mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
    1126             : 
    1127       24343 :         mp->m_rtrmap_mxr[0] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, true);
    1128       24343 :         mp->m_rtrmap_mxr[1] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, false);
    1129       24343 :         mp->m_rtrmap_mnr[0] = mp->m_rtrmap_mxr[0] / 2;
    1130       24343 :         mp->m_rtrmap_mnr[1] = mp->m_rtrmap_mxr[1] / 2;
    1131             : 
    1132       24343 :         mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, true);
    1133       24343 :         mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, false);
    1134       24343 :         mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
    1135       24343 :         mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
    1136             : 
    1137       24343 :         mp->m_rtrefc_mxr[0] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
    1138             :                         true);
    1139       24343 :         mp->m_rtrefc_mxr[1] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
    1140             :                         false);
    1141       24343 :         mp->m_rtrefc_mnr[0] = mp->m_rtrefc_mxr[0] / 2;
    1142       24343 :         mp->m_rtrefc_mnr[1] = mp->m_rtrefc_mxr[1] / 2;
    1143             : 
    1144       24343 :         mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
    1145       24343 :         mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
    1146       24343 :         mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
    1147       24343 : }
    1148             : 
    1149             : /*
    1150             :  * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
    1151             :  * into the superblock buffer to be logged.  It does not provide the higher
    1152             :  * level of locking that is needed to protect the in-core superblock from
    1153             :  * concurrent access.
    1154             :  */
    1155             : void
    1156      262914 : xfs_log_sb(
    1157             :         struct xfs_trans        *tp)
    1158             : {
    1159      262914 :         struct xfs_mount        *mp = tp->t_mountp;
    1160      262914 :         struct xfs_buf          *bp = xfs_trans_getsb(tp);
    1161             : 
    1162             :         /*
    1163             :          * Lazy sb counters don't update the in-core superblock so do that now.
    1164             :          * If this is at unmount, the counters will be exactly correct, but at
    1165             :          * any other time they will only be ballpark correct because of
    1166             :          * reservations that have been taken out percpu counters. If we have an
    1167             :          * unclean shutdown, this will be corrected by log recovery rebuilding
    1168             :          * the counters from the AGF block counts.
    1169             :          *
    1170             :          * Do not update sb_frextents here because it is not part of the lazy
    1171             :          * sb counters, despite having a percpu counter. It is always kept
    1172             :          * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas()
    1173             :          * and hence we don't need have to update it here.
    1174             :          *
    1175             :          * sb_frextents was added to the lazy sb counters when the rt groups
    1176             :          * feature was introduced.
    1177             :          */
    1178      262914 :         if (xfs_has_lazysbcount(mp)) {
    1179      262892 :                 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
    1180      262892 :                 mp->m_sb.sb_ifree = min_t(uint64_t,
    1181             :                                 percpu_counter_sum(&mp->m_ifree),
    1182             :                                 mp->m_sb.sb_icount);
    1183      262892 :                 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
    1184             :         }
    1185      262914 :         if (xfs_has_rtgroups(mp))
    1186      261214 :                 mp->m_sb.sb_frextents = percpu_counter_sum(&mp->m_frextents);
    1187             : 
    1188      262914 :         xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
    1189      262914 :         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
    1190      262914 :         xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
    1191             : 
    1192      262914 :         xfs_rtgroup_log_super(tp, bp);
    1193      262914 : }
    1194             : 
    1195             : /*
    1196             :  * xfs_sync_sb
    1197             :  *
    1198             :  * Sync the superblock to disk.
    1199             :  *
    1200             :  * Note that the caller is responsible for checking the frozen state of the
    1201             :  * filesystem. This procedure uses the non-blocking transaction allocator and
    1202             :  * thus will allow modifications to a frozen fs. This is required because this
    1203             :  * code can be called during the process of freezing where use of the high-level
    1204             :  * allocator would deadlock.
    1205             :  */
    1206             : int
    1207      253671 : xfs_sync_sb(
    1208             :         struct xfs_mount        *mp,
    1209             :         bool                    wait)
    1210             : {
    1211      253671 :         struct xfs_trans        *tp;
    1212      253671 :         int                     error;
    1213             : 
    1214      253671 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
    1215             :                         XFS_TRANS_NO_WRITECOUNT, &tp);
    1216      253671 :         if (error)
    1217             :                 return error;
    1218             : 
    1219      253662 :         xfs_log_sb(tp);
    1220      253662 :         if (wait)
    1221      120677 :                 xfs_trans_set_sync(tp);
    1222      253662 :         return xfs_trans_commit(tp);
    1223             : }
    1224             : 
    1225             : /*
    1226             :  * Update all the secondary superblocks to match the new state of the primary.
    1227             :  * Because we are completely overwriting all the existing fields in the
    1228             :  * secondary superblock buffers, there is no need to read them in from disk.
    1229             :  * Just get a new buffer, stamp it and write it.
    1230             :  *
    1231             :  * The sb buffers need to be cached here so that we serialise against other
    1232             :  * operations that access the secondary superblocks, but we don't want to keep
    1233             :  * them in memory once it is written so we mark it as a one-shot buffer.
    1234             :  */
    1235             : int
    1236         181 : xfs_update_secondary_sbs(
    1237             :         struct xfs_mount        *mp)
    1238             : {
    1239         181 :         struct xfs_perag        *pag;
    1240         181 :         xfs_agnumber_t          agno = 1;
    1241         181 :         int                     saved_error = 0;
    1242         181 :         int                     error = 0;
    1243         181 :         LIST_HEAD               (buffer_list);
    1244             : 
    1245             :         /* update secondary superblocks. */
    1246        8602 :         for_each_perag_from(mp, agno, pag) {
    1247        8421 :                 struct xfs_buf          *bp;
    1248             : 
    1249        8421 :                 error = xfs_buf_get(mp->m_ddev_targp,
    1250        8421 :                                  XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
    1251        8421 :                                  XFS_FSS_TO_BB(mp, 1), &bp);
    1252             :                 /*
    1253             :                  * If we get an error reading or writing alternate superblocks,
    1254             :                  * continue.  xfs_repair chooses the "best" superblock based
    1255             :                  * on most matches; if we break early, we'll leave more
    1256             :                  * superblocks un-updated than updated, and xfs_repair may
    1257             :                  * pick them over the properly-updated primary.
    1258             :                  */
    1259        8421 :                 if (error) {
    1260           0 :                         xfs_warn(mp,
    1261             :                 "error allocating secondary superblock for ag %d",
    1262             :                                 pag->pag_agno);
    1263           0 :                         if (!saved_error)
    1264           0 :                                 saved_error = error;
    1265        7945 :                         continue;
    1266             :                 }
    1267             : 
    1268        8421 :                 bp->b_ops = &xfs_sb_buf_ops;
    1269        8421 :                 xfs_buf_oneshot(bp);
    1270        8421 :                 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
    1271        8421 :                 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
    1272        8421 :                 xfs_buf_delwri_queue(bp, &buffer_list);
    1273        8421 :                 xfs_buf_relse(bp);
    1274             : 
    1275             :                 /* don't hold too many buffers at once */
    1276        8421 :                 if (agno % 16)
    1277        7945 :                         continue;
    1278             : 
    1279         476 :                 error = xfs_buf_delwri_submit(&buffer_list);
    1280         476 :                 if (error) {
    1281           0 :                         xfs_warn(mp,
    1282             :                 "write error %d updating a secondary superblock near ag %d",
    1283             :                                 error, pag->pag_agno);
    1284           0 :                         if (!saved_error)
    1285           0 :                                 saved_error = error;
    1286           0 :                         continue;
    1287             :                 }
    1288             :         }
    1289         181 :         error = xfs_buf_delwri_submit(&buffer_list);
    1290         181 :         if (error) {
    1291           0 :                 xfs_warn(mp,
    1292             :                 "write error %d updating a secondary superblock near ag %d",
    1293             :                         error, agno);
    1294             :         }
    1295             : 
    1296         181 :         return saved_error ? saved_error : error;
    1297             : }
    1298             : 
    1299             : /*
    1300             :  * Same behavior as xfs_sync_sb, except that it is always synchronous and it
    1301             :  * also writes the superblock buffer to disk sector 0 immediately.
    1302             :  */
    1303             : int
    1304          52 : xfs_sync_sb_buf(
    1305             :         struct xfs_mount        *mp)
    1306             : {
    1307          52 :         struct xfs_trans        *tp;
    1308          52 :         struct xfs_buf          *bp;
    1309          52 :         struct xfs_buf          *rtsb_bp = NULL;
    1310          52 :         int                     error;
    1311             : 
    1312          52 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
    1313          52 :         if (error)
    1314             :                 return error;
    1315             : 
    1316          52 :         bp = xfs_trans_getsb(tp);
    1317          52 :         xfs_log_sb(tp);
    1318          52 :         xfs_trans_bhold(tp, bp);
    1319          52 :         if (xfs_has_rtgroups(mp)) {
    1320          52 :                 rtsb_bp = xfs_trans_getrtsb(tp);
    1321          52 :                 if (rtsb_bp)
    1322           0 :                         xfs_trans_bhold(tp, rtsb_bp);
    1323             :         }
    1324          52 :         xfs_trans_set_sync(tp);
    1325          52 :         error = xfs_trans_commit(tp);
    1326          52 :         if (error)
    1327           0 :                 goto out;
    1328             :         /*
    1329             :          * write out the sb buffer to get the changes to disk
    1330             :          */
    1331          52 :         error = xfs_bwrite(bp);
    1332          52 :         if (!error && rtsb_bp)
    1333           0 :                 error = xfs_bwrite(rtsb_bp);
    1334          52 : out:
    1335          52 :         if (rtsb_bp)
    1336           0 :                 xfs_buf_relse(rtsb_bp);
    1337          52 :         xfs_buf_relse(bp);
    1338          52 :         return error;
    1339             : }
    1340             : 
    1341             : void
    1342     2055194 : xfs_fs_geometry(
    1343             :         struct xfs_mount        *mp,
    1344             :         struct xfs_fsop_geom    *geo,
    1345             :         int                     struct_version)
    1346             : {
    1347     2055194 :         struct xfs_sb           *sbp = &mp->m_sb;
    1348             : 
    1349     2055194 :         memset(geo, 0, sizeof(struct xfs_fsop_geom));
    1350             : 
    1351     2055194 :         geo->blocksize = sbp->sb_blocksize;
    1352     2055194 :         geo->rtextsize = sbp->sb_rextsize;
    1353     2055194 :         geo->agblocks = sbp->sb_agblocks;
    1354     2055194 :         geo->agcount = sbp->sb_agcount;
    1355     2055194 :         geo->logblocks = sbp->sb_logblocks;
    1356     2055194 :         geo->sectsize = sbp->sb_sectsize;
    1357     2055194 :         geo->inodesize = sbp->sb_inodesize;
    1358     2055194 :         geo->imaxpct = sbp->sb_imax_pct;
    1359     2055194 :         geo->datablocks = sbp->sb_dblocks;
    1360     2055194 :         geo->rtblocks = sbp->sb_rblocks;
    1361     2055194 :         geo->rtextents = sbp->sb_rextents;
    1362     2055194 :         geo->logstart = sbp->sb_logstart;
    1363     2055194 :         BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
    1364     4110388 :         memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
    1365             : 
    1366     2055194 :         if (struct_version < 2)
    1367             :                 return;
    1368             : 
    1369     2055194 :         geo->sunit = sbp->sb_unit;
    1370     2055194 :         geo->swidth = sbp->sb_width;
    1371             : 
    1372     2055194 :         if (struct_version < 3)
    1373             :                 return;
    1374             : 
    1375     2055194 :         geo->version = XFS_FSOP_GEOM_VERSION;
    1376     2055194 :         geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
    1377             :                      XFS_FSOP_GEOM_FLAGS_DIRV2 |
    1378             :                      XFS_FSOP_GEOM_FLAGS_EXTFLG;
    1379     2055194 :         if (xfs_has_attr(mp))
    1380     2058913 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
    1381     2055194 :         if (xfs_has_quota(mp))
    1382     2045905 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
    1383     2055194 :         if (xfs_has_align(mp))
    1384     2057807 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
    1385     2055194 :         if (xfs_has_dalign(mp))
    1386         596 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
    1387     2055194 :         if (xfs_has_asciici(mp))
    1388          52 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
    1389     2055194 :         if (xfs_has_lazysbcount(mp))
    1390     2056650 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
    1391     2055194 :         if (xfs_has_attr2(mp))
    1392     2057651 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
    1393     2055194 :         if (xfs_has_projid32(mp))
    1394     2057641 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
    1395     2055194 :         if (xfs_has_crc(mp))
    1396     2057362 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
    1397     2055194 :         if (xfs_has_ftype(mp))
    1398     2057431 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
    1399     2055194 :         if (xfs_has_finobt(mp))
    1400     2054990 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
    1401     2055194 :         if (xfs_has_sparseinodes(mp))
    1402     2057769 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
    1403     2055194 :         if (xfs_has_rmapbt(mp))
    1404     2057464 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
    1405     2055194 :         if (xfs_has_reflink(mp))
    1406     2057946 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
    1407     2055194 :         if (xfs_has_bigtime(mp))
    1408     2057354 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
    1409     2055194 :         if (xfs_has_inobtcounts(mp))
    1410     2057421 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
    1411     2055194 :         if (xfs_has_parent(mp))
    1412     2050696 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_PARENT;
    1413     2055194 :         if (xfs_has_sector(mp)) {
    1414     2046212 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
    1415     2046212 :                 geo->logsectsize = sbp->sb_logsectsize;
    1416             :         } else {
    1417        8982 :                 geo->logsectsize = BBSIZE;
    1418             :         }
    1419     2055194 :         if (xfs_has_large_extent_counts(mp))
    1420     2048056 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
    1421     2055306 :         if (xfs_swapext_supported(mp))
    1422     2048504 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATOMIC_SWAP;
    1423     2055194 :         if (xfs_has_metadir(mp))
    1424     2045889 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_METADIR;
    1425     2055194 :         geo->rtsectsize = sbp->sb_blocksize;
    1426     2055194 :         geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
    1427             : 
    1428     2055194 :         if (struct_version < 4)
    1429             :                 return;
    1430             : 
    1431     2055050 :         if (xfs_has_logv2(mp))
    1432     2050002 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
    1433             : 
    1434     2055050 :         geo->logsunit = sbp->sb_logsunit;
    1435             : 
    1436     2055050 :         if (struct_version < 5)
    1437             :                 return;
    1438             : 
    1439     2055050 :         geo->version = XFS_FSOP_GEOM_VERSION_V5;
    1440             : 
    1441     2055050 :         if (xfs_has_rtgroups(mp)) {
    1442     2045560 :                 geo->rgcount = sbp->sb_rgcount;
    1443     2045560 :                 geo->rgblocks = sbp->sb_rgblocks;
    1444             :         }
    1445             : }
    1446             : 
    1447             : /* Read a secondary superblock. */
    1448             : int
    1449      423849 : xfs_sb_read_secondary(
    1450             :         struct xfs_mount        *mp,
    1451             :         struct xfs_trans        *tp,
    1452             :         xfs_agnumber_t          agno,
    1453             :         struct xfs_buf          **bpp)
    1454             : {
    1455      423849 :         struct xfs_buf          *bp;
    1456      423849 :         int                     error;
    1457             : 
    1458      423849 :         ASSERT(agno != 0 && agno != NULLAGNUMBER);
    1459      423849 :         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
    1460      423849 :                         XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
    1461      423849 :                         XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
    1462      423848 :         if (xfs_metadata_is_sick(error))
    1463          16 :                 xfs_agno_mark_sick(mp, agno, XFS_SICK_AG_SB);
    1464      423848 :         if (error)
    1465             :                 return error;
    1466      423829 :         xfs_buf_set_ref(bp, XFS_SSB_REF);
    1467      423829 :         *bpp = bp;
    1468      423829 :         return 0;
    1469             : }
    1470             : 
    1471             : /* Get an uninitialised secondary superblock buffer. */
    1472             : int
    1473      145371 : xfs_sb_get_secondary(
    1474             :         struct xfs_mount        *mp,
    1475             :         struct xfs_trans        *tp,
    1476             :         xfs_agnumber_t          agno,
    1477             :         struct xfs_buf          **bpp)
    1478             : {
    1479      145371 :         struct xfs_buf          *bp;
    1480      145371 :         int                     error;
    1481             : 
    1482      145371 :         ASSERT(agno != 0 && agno != NULLAGNUMBER);
    1483      145371 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
    1484      145371 :                         XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
    1485      145371 :                         XFS_FSS_TO_BB(mp, 1), 0, &bp);
    1486      145371 :         if (error)
    1487             :                 return error;
    1488      145371 :         bp->b_ops = &xfs_sb_buf_ops;
    1489      145371 :         xfs_buf_oneshot(bp);
    1490      145371 :         *bpp = bp;
    1491      145371 :         return 0;
    1492             : }
    1493             : 
    1494             : /*
    1495             :  * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
    1496             :  * so users won't be confused by values in error messages.
    1497             :  */
    1498             : bool
    1499      462322 : xfs_validate_stripe_geometry(
    1500             :         struct xfs_mount        *mp,
    1501             :         __s64                   sunit,
    1502             :         __s64                   swidth,
    1503             :         int                     sectorsize,
    1504             :         bool                    silent)
    1505             : {
    1506      462322 :         if (swidth > INT_MAX) {
    1507           0 :                 if (!silent)
    1508           0 :                         xfs_notice(mp,
    1509             : "stripe width (%lld) is too large", swidth);
    1510           0 :                 return false;
    1511             :         }
    1512             : 
    1513      462322 :         if (sunit > swidth) {
    1514           0 :                 if (!silent)
    1515           0 :                         xfs_notice(mp,
    1516             : "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
    1517           0 :                 return false;
    1518             :         }
    1519             : 
    1520      462322 :         if (sectorsize && (int)sunit % sectorsize) {
    1521           0 :                 if (!silent)
    1522           0 :                         xfs_notice(mp,
    1523             : "stripe unit (%lld) must be a multiple of the sector size (%d)",
    1524             :                                    sunit, sectorsize);
    1525           0 :                 return false;
    1526             :         }
    1527             : 
    1528      462322 :         if (sunit && !swidth) {
    1529           0 :                 if (!silent)
    1530           0 :                         xfs_notice(mp,
    1531             : "invalid stripe unit (%lld) and stripe width of 0", sunit);
    1532           0 :                 return false;
    1533             :         }
    1534             : 
    1535      462322 :         if (!sunit && swidth) {
    1536           0 :                 if (!silent)
    1537           0 :                         xfs_notice(mp,
    1538             : "invalid stripe width (%lld) and stripe unit of 0", swidth);
    1539           0 :                 return false;
    1540             :         }
    1541             : 
    1542      462322 :         if (sunit && (int)swidth % (int)sunit) {
    1543           0 :                 if (!silent)
    1544           0 :                         xfs_notice(mp,
    1545             : "stripe width (%lld) must be a multiple of the stripe unit (%lld)",
    1546             :                                    swidth, sunit);
    1547           0 :                 return false;
    1548             :         }
    1549             :         return true;
    1550             : }

Generated by: LCOV version 1.14