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-xfsx @ Mon Jul 31 20:08:34 PDT 2023 Lines: 630 721 87.4 %
Date: 2023-07-31 20:08:34 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      979701 : xfs_sb_validate_v5_features(
      43             :         struct xfs_sb   *sbp)
      44             : {
      45             :         /* We must not have any unknown V4 feature bits set */
      46      979701 :         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      979701 :         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      979701 :         if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
      72             :                 return false;
      73      979701 :         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     1007683 : 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     1007683 :         if (xfs_sb_is_v5(sbp))
      92      979700 :                 return xfs_sb_validate_v5_features(sbp);
      93             : 
      94             :         /* versions prior to v4 are not supported */
      95       27983 :         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       27983 :         if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
     100       27983 :             ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
     101       27983 :              (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
     102             :                 return false;
     103             : 
     104             :         /* V4 filesystems need v2 directories and unwritten extents */
     105       27983 :         if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
     106             :                 return false;
     107       27983 :         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       80689 : xfs_sb_version_to_features(
     116             :         struct xfs_sb   *sbp)
     117             : {
     118       80689 :         uint64_t        features = 0;
     119             : 
     120             :         /* optional V4 features */
     121       80689 :         if (sbp->sb_rblocks > 0)
     122       24893 :                 features |= XFS_FEAT_REALTIME;
     123       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
     124       80689 :                 features |= XFS_FEAT_NLINK;
     125       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
     126       80689 :                 features |= XFS_FEAT_ATTR;
     127       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
     128       55089 :                 features |= XFS_FEAT_QUOTA;
     129       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
     130       80689 :                 features |= XFS_FEAT_ALIGN;
     131       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
     132       80638 :                 features |= XFS_FEAT_LOGV2;
     133       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
     134        5895 :                 features |= XFS_FEAT_DALIGN;
     135       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
     136       80689 :                 features |= XFS_FEAT_EXTFLG;
     137       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
     138       78224 :                 features |= XFS_FEAT_SECTOR;
     139       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
     140         155 :                 features |= XFS_FEAT_ASCIICI;
     141       80689 :         if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
     142       80689 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
     143       80656 :                         features |= XFS_FEAT_LAZYSBCOUNT;
     144       80689 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
     145       80656 :                         features |= XFS_FEAT_ATTR2;
     146       80689 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
     147       80656 :                         features |= XFS_FEAT_PROJID32;
     148       80689 :                 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
     149         158 :                         features |= XFS_FEAT_FTYPE;
     150             :         }
     151             : 
     152       80689 :         if (!xfs_sb_is_v5(sbp))
     153             :                 return features;
     154             : 
     155             :         /* Always on V5 features */
     156       80410 :         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       80410 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
     162       80377 :                 features |= XFS_FEAT_FINOBT;
     163       80410 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
     164       75544 :                 features |= XFS_FEAT_RMAPBT;
     165       80410 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
     166       75496 :                 features |= XFS_FEAT_REFLINK;
     167       80410 :         if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
     168       80266 :                 features |= XFS_FEAT_INOBTCNT;
     169       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
     170       80410 :                 features |= XFS_FEAT_FTYPE;
     171       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
     172       80410 :                 features |= XFS_FEAT_SPINODES;
     173       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
     174          43 :                 features |= XFS_FEAT_META_UUID;
     175       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
     176       80229 :                 features |= XFS_FEAT_BIGTIME;
     177       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
     178          24 :                 features |= XFS_FEAT_NEEDSREPAIR;
     179       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
     180       80399 :                 features |= XFS_FEAT_NREXT64;
     181       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_PARENT)
     182       73377 :                 features |= XFS_FEAT_PARENT;
     183       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)
     184       73412 :                 features |= XFS_FEAT_METADIR;
     185       80410 :         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS)
     186       73392 :                 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      406535 : xfs_validate_sb_read(
     194             :         struct xfs_mount        *mp,
     195             :         struct xfs_sb           *sbp)
     196             : {
     197      406535 :         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      406245 :         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      406245 :         if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
     213          22 :                 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          44 :                 if (!xfs_is_readonly(mp)) {
     218          11 :                         xfs_warn(mp,
     219             : "Attempted to mount read-only compatible filesystem read-write.");
     220          11 :                         xfs_warn(mp,
     221             : "Filesystem can only be safely mounted read only.");
     222             : 
     223          11 :                         return -EINVAL;
     224             :                 }
     225             :         }
     226      406234 :         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      600521 : 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      600521 :         if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
     257      969727 :             (sbp->sb_fdblocks > sbp->sb_dblocks ||
     258      484858 :              !xfs_verify_icount(mp, sbp->sb_icount) ||
     259      484858 :              sbp->sb_ifree > sbp->sb_icount)) {
     260          11 :                 xfs_warn(mp, "SB summary counter sanity check failed");
     261          11 :                 return -EFSCORRUPTED;
     262             :         }
     263             : 
     264      600510 :         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      572817 :         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      572817 :         if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
     280          11 :                 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          11 :                 return -EFSCORRUPTED;
     285             :         }
     286      572806 :         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      572806 :         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      572806 :         if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
     308           0 :                 return -EFSCORRUPTED;
     309             : 
     310             :         return 0;
     311             : }
     312             : 
     313             : static int
     314      931661 : xfs_validate_sb_rtgroups(
     315             :         struct xfs_mount        *mp,
     316             :         struct xfs_sb           *sbp)
     317             : {
     318      931661 :         uint64_t                groups;
     319             : 
     320      931661 :         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      931661 :         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      931661 :         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      931661 :         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      931661 :         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      931661 :         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      931661 :         groups = howmany_64(sbp->sb_rblocks, sbp->sb_rgblocks);
     361      931662 :         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     1007695 : xfs_validate_sb_common(
     374             :         struct xfs_mount        *mp,
     375             :         struct xfs_buf          *bp,
     376             :         struct xfs_sb           *sbp)
     377             : {
     378     1007695 :         struct xfs_dsb          *dsb = bp->b_addr;
     379     1007695 :         uint32_t                agcount = 0;
     380     1007695 :         uint32_t                rem;
     381     1007695 :         bool                    has_dalign;
     382     1007695 :         int                     error;
     383             : 
     384     1007695 :         if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
     385          11 :                 xfs_warn(mp,
     386             : "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
     387             :                         be32_to_cpu(dsb->sb_magicnum));
     388          11 :                 return -EWRONGFS;
     389             :         }
     390             : 
     391     1007689 :         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     1007689 :         if (xfs_sb_is_v5(sbp)) {
     401      979702 :                 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      979702 :                 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      979702 :                 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
     421      979702 :                         uint32_t        align;
     422             : 
     423     1959404 :                         align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
     424      979702 :                                         >> sbp->sb_blocklog;
     425      979702 :                         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      979702 :                 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     434      931663 :                         error = xfs_validate_sb_rtgroups(mp, sbp);
     435      931662 :                         if (error)
     436             :                                 return error;
     437             :                 }
     438       27987 :         } 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     1007688 :         if (unlikely(
     446             :             sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
     447         510 :                 xfs_warn(mp,
     448             :                 "filesystem is marked as having an external log; "
     449             :                 "specify logdev on the mount command line.");
     450         510 :                 return -EINVAL;
     451             :         }
     452             : 
     453     1007178 :         if (unlikely(
     454             :             sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
     455          10 :                 xfs_warn(mp,
     456             :                 "filesystem is marked as having an internal log; "
     457             :                 "do not specify logdev on the mount command line.");
     458          10 :                 return -EINVAL;
     459             :         }
     460             : 
     461             :         /* Compute agcount for this number of dblocks and agblocks */
     462     1007168 :         if (sbp->sb_agblocks) {
     463     1007165 :                 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
     464     1007163 :                 if (rem)
     465      108018 :                         agcount++;
     466             :         }
     467             : 
     468             :         /*
     469             :          * More sanity checking.  Most of these were stolen directly from
     470             :          * xfs_repair.
     471             :          */
     472     1007166 :         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          99 :                 xfs_notice(mp, "SB sanity check failed");
     504          99 :                 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     1007067 :         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     1007067 :         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     1007067 :         if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
     534      986947 :                 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       20120 :         } 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     1007067 :         if (sbp->sb_logsunit > 1) {
     548      718088 :                 if (sbp->sb_logsunit % sbp->sb_blocksize) {
     549          11 :                         xfs_notice(mp,
     550             :                 "log stripe unit 0x%x bytes must be a multiple of block size",
     551             :                                 sbp->sb_logsunit);
     552          11 :                         return -EFSCORRUPTED;
     553             :                 }
     554      718077 :                 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     1007056 :         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     1007056 :         if (sbp->sb_rblocks == 0) {
     571      573600 :                 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
     572      573600 :                     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      433456 :                 uint64_t        rexts;
     579      433456 :                 uint64_t        rbmblocks;
     580      433456 :                 unsigned int    rbmblock_bytes = sbp->sb_blocksize;
     581             : 
     582      433456 :                 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
     583             : 
     584      433457 :                 if (xfs_sb_is_v5(sbp) &&
     585      422591 :                     (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS))
     586      391478 :                         rbmblock_bytes -= sizeof(struct xfs_rtbuf_blkinfo);
     587             : 
     588      433457 :                 rbmblocks = howmany_64(sbp->sb_rextents, NBBY * rbmblock_bytes);
     589             : 
     590      433456 :                 if (sbp->sb_rextents != rexts ||
     591      433456 :                     sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
     592      433456 :                     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     1007056 :         has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
     604     1007056 :         if (!!sbp->sb_unit ^ has_dalign) {
     605           0 :                 xfs_notice(mp, "SB stripe alignment sanity check failed");
     606           0 :                 return -EFSCORRUPTED;
     607             :         }
     608             : 
     609     1007056 :         if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
     610     1007056 :                         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     1007053 :         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      148372 : 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      148372 :         if (sbp->sb_uquotino == 0)
     646        6036 :                 sbp->sb_uquotino = NULLFSINO;
     647      148372 :         if (sbp->sb_gquotino == 0)
     648        6036 :                 sbp->sb_gquotino = NULLFSINO;
     649      148372 :         if (sbp->sb_pquotino == 0)
     650        6378 :                 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      148372 :         if (xfs_sb_is_v5(sbp))
     657             :                 return;
     658             : 
     659         559 :         if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
     660         174 :                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
     661             :                                         XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
     662         559 :         if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
     663         174 :                 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
     664             :                                         XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
     665         559 :         sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
     666             : 
     667         559 :         if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
     668          86 :             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          86 :                 sbp->sb_pquotino = sbp->sb_gquotino;
     679          86 :                 sbp->sb_gquotino = NULLFSINO;
     680             :         }
     681             : }
     682             : 
     683             : static void
     684     1156068 : __xfs_sb_from_disk(
     685             :         struct xfs_sb   *to,
     686             :         struct xfs_dsb  *from,
     687             :         bool            convert_xquota)
     688             : {
     689     1156068 :         to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
     690     1156068 :         to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
     691     1156068 :         to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
     692     1156068 :         to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
     693     1156068 :         to->sb_rextents = be64_to_cpu(from->sb_rextents);
     694     2312136 :         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
     695     1156068 :         to->sb_logstart = be64_to_cpu(from->sb_logstart);
     696     1156068 :         to->sb_rootino = be64_to_cpu(from->sb_rootino);
     697     1156068 :         to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
     698     1156068 :         to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
     699     1156068 :         to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
     700     1156068 :         to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
     701     1156068 :         to->sb_agcount = be32_to_cpu(from->sb_agcount);
     702     1156068 :         to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
     703     1156068 :         to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
     704     1156068 :         to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
     705     1156068 :         to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
     706     1156068 :         to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
     707     1156068 :         to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
     708     2312136 :         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
     709     1156068 :         to->sb_blocklog = from->sb_blocklog;
     710     1156068 :         to->sb_sectlog = from->sb_sectlog;
     711     1156068 :         to->sb_inodelog = from->sb_inodelog;
     712     1156068 :         to->sb_inopblog = from->sb_inopblog;
     713     1156068 :         to->sb_agblklog = from->sb_agblklog;
     714     1156068 :         to->sb_rextslog = from->sb_rextslog;
     715     1156068 :         to->sb_inprogress = from->sb_inprogress;
     716     1156068 :         to->sb_imax_pct = from->sb_imax_pct;
     717     1156068 :         to->sb_icount = be64_to_cpu(from->sb_icount);
     718     1156068 :         to->sb_ifree = be64_to_cpu(from->sb_ifree);
     719     1156068 :         to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
     720     1156068 :         to->sb_frextents = be64_to_cpu(from->sb_frextents);
     721     1156068 :         to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
     722     1156068 :         to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
     723     1156068 :         to->sb_qflags = be16_to_cpu(from->sb_qflags);
     724     1156068 :         to->sb_flags = from->sb_flags;
     725     1156068 :         to->sb_shared_vn = from->sb_shared_vn;
     726     1156068 :         to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
     727     1156068 :         to->sb_unit = be32_to_cpu(from->sb_unit);
     728     1156068 :         to->sb_width = be32_to_cpu(from->sb_width);
     729     1156068 :         to->sb_dirblklog = from->sb_dirblklog;
     730     1156068 :         to->sb_logsectlog = from->sb_logsectlog;
     731     1156068 :         to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
     732     1156068 :         to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
     733     1156068 :         to->sb_features2 = be32_to_cpu(from->sb_features2);
     734     1156068 :         to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
     735     1156068 :         to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
     736     1156068 :         to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
     737     1156068 :         to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
     738     1156068 :         to->sb_features_log_incompat =
     739     1156068 :                                 be32_to_cpu(from->sb_features_log_incompat);
     740             :         /* crc is only used on disk, not in memory; just init to 0 here. */
     741     1156068 :         to->sb_crc = 0;
     742     1156068 :         to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
     743     1156068 :         to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
     744     1156068 :         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     1156068 :         if (xfs_sb_is_v5(to) &&
     750     1127526 :             (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
     751         223 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
     752             :         else
     753     1155845 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
     754             :         /* Convert on-disk flags to in-memory flags? */
     755     1156068 :         if (convert_xquota)
     756      148372 :                 xfs_sb_quota_from_disk(to);
     757             : 
     758     1156068 :         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     1066306 :                 to->sb_metadirino = be64_to_cpu(from->sb_rbmino);
     765     1066306 :                 to->sb_rbmino = NULLFSINO;
     766     1066306 :                 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     1066306 :                 to->sb_uquotino = NULLFSINO;
     773     1066306 :                 to->sb_gquotino = NULLFSINO;
     774     1066306 :                 to->sb_pquotino = NULLFSINO;
     775             :         }
     776             : 
     777     1156068 :         if (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     778     1066084 :                 to->sb_rgcount = be32_to_cpu(from->sb_rgcount);
     779     1066084 :                 to->sb_rgblocks = be32_to_cpu(from->sb_rgblocks);
     780             :         } else {
     781       89984 :                 to->sb_rgcount = 0;
     782       89984 :                 to->sb_rgblocks = 0;
     783             :         }
     784     1156068 : }
     785             : 
     786             : void
     787      148372 : xfs_sb_from_disk(
     788             :         struct xfs_sb   *to,
     789             :         struct xfs_dsb  *from)
     790             : {
     791      148372 :         __xfs_sb_from_disk(to, from, true);
     792      148372 : }
     793             : 
     794             : static void
     795      994745 : xfs_sb_quota_to_disk(
     796             :         struct xfs_dsb  *to,
     797             :         struct xfs_sb   *from)
     798             : {
     799      994745 :         uint16_t        qflags = from->sb_qflags;
     800             : 
     801      994745 :         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      994745 :         if (xfs_sb_is_v5(from)) {
     808      966868 :                 to->sb_qflags = cpu_to_be16(from->sb_qflags);
     809      966868 :                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
     810      966868 :                 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
     811      966868 :                 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       27877 :         qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
     820             :                         XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
     821             : 
     822       27877 :         if (from->sb_qflags &
     823             :                         (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
     824       23625 :                 qflags |= XFS_OQUOTA_ENFD;
     825       27877 :         if (from->sb_qflags &
     826             :                         (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
     827       23625 :                 qflags |= XFS_OQUOTA_CHKD;
     828       27877 :         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       27877 :         if (from->sb_qflags & XFS_GQUOTA_ACCT)
     840          66 :                 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
     841       27811 :         else if (from->sb_qflags & XFS_PQUOTA_ACCT)
     842       23679 :                 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        4132 :                 if (from->sb_gquotino == NULLFSINO &&
     851        4121 :                     from->sb_pquotino == NULLFSINO)
     852        4121 :                         to->sb_gquotino = cpu_to_be64(NULLFSINO);
     853             :         }
     854             : 
     855       27877 :         to->sb_pquotino = 0;
     856             : }
     857             : 
     858             : void
     859      994867 : xfs_sb_to_disk(
     860             :         struct xfs_dsb  *to,
     861             :         struct xfs_sb   *from)
     862             : {
     863      994867 :         xfs_sb_quota_to_disk(to, from);
     864             : 
     865      994722 :         to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
     866      994722 :         to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
     867      994722 :         to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
     868      994722 :         to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
     869      994722 :         to->sb_rextents = cpu_to_be64(from->sb_rextents);
     870     1989444 :         memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
     871      994722 :         to->sb_logstart = cpu_to_be64(from->sb_logstart);
     872      994722 :         to->sb_rootino = cpu_to_be64(from->sb_rootino);
     873      994722 :         to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
     874      994722 :         to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
     875      994722 :         to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
     876      994722 :         to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
     877      994722 :         to->sb_agcount = cpu_to_be32(from->sb_agcount);
     878      994722 :         to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
     879      994722 :         to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
     880      994722 :         to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
     881      994722 :         to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
     882      994722 :         to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
     883      994722 :         to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
     884     1989444 :         memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
     885      994722 :         to->sb_blocklog = from->sb_blocklog;
     886      994722 :         to->sb_sectlog = from->sb_sectlog;
     887      994722 :         to->sb_inodelog = from->sb_inodelog;
     888      994722 :         to->sb_inopblog = from->sb_inopblog;
     889      994722 :         to->sb_agblklog = from->sb_agblklog;
     890      994722 :         to->sb_rextslog = from->sb_rextslog;
     891      994722 :         to->sb_inprogress = from->sb_inprogress;
     892      994722 :         to->sb_imax_pct = from->sb_imax_pct;
     893      994722 :         to->sb_icount = cpu_to_be64(from->sb_icount);
     894      994722 :         to->sb_ifree = cpu_to_be64(from->sb_ifree);
     895      994722 :         to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
     896      994722 :         to->sb_frextents = cpu_to_be64(from->sb_frextents);
     897             : 
     898      994722 :         to->sb_flags = from->sb_flags;
     899      994722 :         to->sb_shared_vn = from->sb_shared_vn;
     900      994722 :         to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
     901      994722 :         to->sb_unit = cpu_to_be32(from->sb_unit);
     902      994722 :         to->sb_width = cpu_to_be32(from->sb_width);
     903      994722 :         to->sb_dirblklog = from->sb_dirblklog;
     904      994722 :         to->sb_logsectlog = from->sb_logsectlog;
     905      994722 :         to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
     906      994722 :         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      994722 :         from->sb_bad_features2 = from->sb_features2;
     914      994722 :         to->sb_features2 = cpu_to_be32(from->sb_features2);
     915      994722 :         to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
     916             : 
     917      994722 :         if (!xfs_sb_is_v5(from))
     918             :                 return;
     919             : 
     920      966816 :         to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
     921      966816 :         to->sb_features_ro_compat =
     922      966816 :                         cpu_to_be32(from->sb_features_ro_compat);
     923      966816 :         to->sb_features_incompat =
     924      966816 :                         cpu_to_be32(from->sb_features_incompat);
     925      966816 :         to->sb_features_log_incompat =
     926      966816 :                         cpu_to_be32(from->sb_features_log_incompat);
     927      966816 :         to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
     928      966816 :         to->sb_lsn = cpu_to_be64(from->sb_lsn);
     929      966816 :         if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
     930          70 :                 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
     931             : 
     932      966816 :         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      927644 :                 to->sb_rbmino = cpu_to_be64(from->sb_metadirino);
     938      927644 :                 to->sb_rsumino = cpu_to_be64(NULLFSINO);
     939      927644 :                 to->sb_uquotino = cpu_to_be64(NULLFSINO);
     940      927644 :                 to->sb_gquotino = cpu_to_be64(NULLFSINO);
     941      927644 :                 to->sb_pquotino = cpu_to_be64(NULLFSINO);
     942             :         }
     943             : 
     944      966816 :         if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_RTGROUPS) {
     945             :                 /* must come after setting to_rsumino */
     946      927544 :                 to->sb_rgcount = cpu_to_be32(from->sb_rgcount);
     947      927544 :                 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      407197 : xfs_sb_read_verify(
     965             :         struct xfs_buf          *bp)
     966             : {
     967      407197 :         struct xfs_sb           sb;
     968      407197 :         struct xfs_mount        *mp = bp->b_mount;
     969      407197 :         struct xfs_dsb          *dsb = bp->b_addr;
     970      407197 :         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      407197 :         if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
     977      407186 :             (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
     978         290 :                                                 XFS_SB_VERSION_5) ||
     979         290 :              dsb->sb_crc != 0)) {
     980             : 
     981      406896 :                 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
     982             :                         /* Only fail bad secondaries on a known V5 filesystem */
     983          21 :                         if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
     984             :                             xfs_has_crc(mp)) {
     985          21 :                                 error = -EFSBADCRC;
     986          21 :                                 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      407176 :         __xfs_sb_from_disk(&sb, dsb, false);
     996      407176 :         error = xfs_validate_sb_common(mp, bp, &sb);
     997      407176 :         if (error)
     998         641 :                 goto out_error;
     999      406535 :         error = xfs_validate_sb_read(mp, &sb);
    1000             : 
    1001      407197 : out_error:
    1002      407197 :         if (error == -EFSCORRUPTED || error == -EFSBADCRC)
    1003         131 :                 xfs_verifier_error(bp, error, __this_address);
    1004      407066 :         else if (error)
    1005         542 :                 xfs_buf_ioerror(bp, error);
    1006      407197 : }
    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      600521 : xfs_sb_write_verify(
    1031             :         struct xfs_buf          *bp)
    1032             : {
    1033      600521 :         struct xfs_sb           sb;
    1034      600521 :         struct xfs_mount        *mp = bp->b_mount;
    1035      600521 :         struct xfs_buf_log_item *bip = bp->b_log_item;
    1036      600521 :         struct xfs_dsb          *dsb = bp->b_addr;
    1037      600521 :         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      600521 :         __xfs_sb_from_disk(&sb, dsb, false);
    1044      600520 :         error = xfs_validate_sb_common(mp, bp, &sb);
    1045      600517 :         if (error)
    1046           0 :                 goto out_error;
    1047      600517 :         error = xfs_validate_sb_write(mp, bp, &sb);
    1048      600521 :         if (error)
    1049          22 :                 goto out_error;
    1050             : 
    1051      600499 :         if (!xfs_sb_is_v5(&sb))
    1052      600499 :                 return;
    1053             : 
    1054      572806 :         if (bip)
    1055      403496 :                 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
    1056             : 
    1057      572806 :         xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
    1058             :         return;
    1059             : 
    1060          22 : out_error:
    1061          22 :         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       66982 : xfs_sb_mount_common(
    1089             :         struct xfs_mount        *mp,
    1090             :         struct xfs_sb           *sbp)
    1091             : {
    1092       66982 :         mp->m_agfrotor = 0;
    1093       66982 :         atomic_set(&mp->m_agirotor, 0);
    1094       66982 :         mp->m_maxagi = mp->m_sb.sb_agcount;
    1095       66982 :         mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
    1096       66982 :         mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
    1097       66982 :         mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
    1098       66982 :         mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
    1099       66982 :         mp->m_blockmask = sbp->sb_blocksize - 1;
    1100       66982 :         if (xfs_has_rtgroups(mp))
    1101       60441 :                 mp->m_blockwsize = (sbp->sb_blocksize -
    1102       60441 :                                         sizeof(struct xfs_rtbuf_blkinfo)) >>
    1103             :                                         XFS_WORDLOG;
    1104             :         else
    1105        6541 :                 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
    1106       66982 :         mp->m_rtx_per_rbmblock = mp->m_blockwsize << XFS_NBWORDLOG;
    1107       66982 :         mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize);
    1108       66982 :         mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize);
    1109       66982 :         mp->m_rgblklog = log2_if_power2(sbp->sb_rgblocks);
    1110       66982 :         mp->m_rgblkmask = mask64_if_power2(sbp->sb_rgblocks);
    1111             : 
    1112       66982 :         mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, true);
    1113       66982 :         mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, false);
    1114       66982 :         mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
    1115       66982 :         mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
    1116             : 
    1117       66982 :         mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, true);
    1118       66982 :         mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, false);
    1119       66982 :         mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
    1120       66982 :         mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
    1121             : 
    1122       66982 :         mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, true);
    1123       66982 :         mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, false);
    1124       66982 :         mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
    1125       66982 :         mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
    1126             : 
    1127       66982 :         mp->m_rtrmap_mxr[0] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, true);
    1128       66982 :         mp->m_rtrmap_mxr[1] = xfs_rtrmapbt_maxrecs(mp, sbp->sb_blocksize, false);
    1129       66982 :         mp->m_rtrmap_mnr[0] = mp->m_rtrmap_mxr[0] / 2;
    1130       66982 :         mp->m_rtrmap_mnr[1] = mp->m_rtrmap_mxr[1] / 2;
    1131             : 
    1132       66982 :         mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, true);
    1133       66982 :         mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, false);
    1134       66982 :         mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
    1135       66982 :         mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
    1136             : 
    1137       66982 :         mp->m_rtrefc_mxr[0] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
    1138             :                         true);
    1139       66982 :         mp->m_rtrefc_mxr[1] = xfs_rtrefcountbt_maxrecs(mp, sbp->sb_blocksize,
    1140             :                         false);
    1141       66982 :         mp->m_rtrefc_mnr[0] = mp->m_rtrefc_mxr[0] / 2;
    1142       66982 :         mp->m_rtrefc_mnr[1] = mp->m_rtrefc_mxr[1] / 2;
    1143             : 
    1144       66982 :         mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
    1145       66982 :         mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
    1146       66982 :         mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
    1147       66982 : }
    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      492000 : xfs_log_sb(
    1157             :         struct xfs_trans        *tp)
    1158             : {
    1159      492000 :         struct xfs_mount        *mp = tp->t_mountp;
    1160      492000 :         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      492000 :         if (xfs_has_lazysbcount(mp)) {
    1179      491884 :                 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
    1180      491884 :                 mp->m_sb.sb_ifree = min_t(uint64_t,
    1181             :                                 percpu_counter_sum(&mp->m_ifree),
    1182             :                                 mp->m_sb.sb_icount);
    1183      491884 :                 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
    1184             :         }
    1185      492000 :         if (xfs_has_rtgroups(mp))
    1186      469943 :                 mp->m_sb.sb_frextents = percpu_counter_sum(&mp->m_frextents);
    1187             : 
    1188      492000 :         xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
    1189      492000 :         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
    1190      492000 :         xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
    1191             : 
    1192      492000 :         xfs_rtgroup_log_super(tp, bp);
    1193      492000 : }
    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      450302 : xfs_sync_sb(
    1208             :         struct xfs_mount        *mp,
    1209             :         bool                    wait)
    1210             : {
    1211      450302 :         struct xfs_trans        *tp;
    1212      450302 :         int                     error;
    1213             : 
    1214      450302 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
    1215             :                         XFS_TRANS_NO_WRITECOUNT, &tp);
    1216      450302 :         if (error)
    1217             :                 return error;
    1218             : 
    1219      450274 :         xfs_log_sb(tp);
    1220      450274 :         if (wait)
    1221      228752 :                 xfs_trans_set_sync(tp);
    1222      450274 :         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         993 : xfs_update_secondary_sbs(
    1237             :         struct xfs_mount        *mp)
    1238             : {
    1239         993 :         struct xfs_perag        *pag;
    1240         993 :         xfs_agnumber_t          agno = 1;
    1241         993 :         int                     saved_error = 0;
    1242         993 :         int                     error = 0;
    1243         993 :         LIST_HEAD               (buffer_list);
    1244             : 
    1245             :         /* update secondary superblocks. */
    1246       55629 :         for_each_perag_from(mp, agno, pag) {
    1247       54636 :                 struct xfs_buf          *bp;
    1248             : 
    1249      163908 :                 error = xfs_buf_get(mp->m_ddev_targp,
    1250       54636 :                                  XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
    1251       54636 :                                  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       54636 :                 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       51502 :                         continue;
    1266             :                 }
    1267             : 
    1268       54636 :                 bp->b_ops = &xfs_sb_buf_ops;
    1269       54636 :                 xfs_buf_oneshot(bp);
    1270       54636 :                 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
    1271       54636 :                 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
    1272       54636 :                 xfs_buf_delwri_queue(bp, &buffer_list);
    1273       54636 :                 xfs_buf_relse(bp);
    1274             : 
    1275             :                 /* don't hold too many buffers at once */
    1276       54636 :                 if (agno % 16)
    1277       51502 :                         continue;
    1278             : 
    1279        3134 :                 error = xfs_buf_delwri_submit(&buffer_list);
    1280        3134 :                 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         993 :         error = xfs_buf_delwri_submit(&buffer_list);
    1290         993 :         if (error) {
    1291           0 :                 xfs_warn(mp,
    1292             :                 "write error %d updating a secondary superblock near ag %d",
    1293             :                         error, agno);
    1294             :         }
    1295             : 
    1296         993 :         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         286 : xfs_sync_sb_buf(
    1305             :         struct xfs_mount        *mp)
    1306             : {
    1307         286 :         struct xfs_trans        *tp;
    1308         286 :         struct xfs_buf          *bp;
    1309         286 :         struct xfs_buf          *rtsb_bp = NULL;
    1310         286 :         int                     error;
    1311             : 
    1312         286 :         error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
    1313         286 :         if (error)
    1314             :                 return error;
    1315             : 
    1316         286 :         bp = xfs_trans_getsb(tp);
    1317         286 :         xfs_log_sb(tp);
    1318         286 :         xfs_trans_bhold(tp, bp);
    1319         286 :         if (xfs_has_rtgroups(mp)) {
    1320         260 :                 rtsb_bp = xfs_trans_getrtsb(tp);
    1321         260 :                 if (rtsb_bp)
    1322         104 :                         xfs_trans_bhold(tp, rtsb_bp);
    1323             :         }
    1324         286 :         xfs_trans_set_sync(tp);
    1325         286 :         error = xfs_trans_commit(tp);
    1326         286 :         if (error)
    1327           0 :                 goto out;
    1328             :         /*
    1329             :          * write out the sb buffer to get the changes to disk
    1330             :          */
    1331         286 :         error = xfs_bwrite(bp);
    1332         286 :         if (!error && rtsb_bp)
    1333         104 :                 error = xfs_bwrite(rtsb_bp);
    1334         182 : out:
    1335         286 :         if (rtsb_bp)
    1336         104 :                 xfs_buf_relse(rtsb_bp);
    1337         286 :         xfs_buf_relse(bp);
    1338         286 :         return error;
    1339             : }
    1340             : 
    1341             : void
    1342     7224843 : xfs_fs_geometry(
    1343             :         struct xfs_mount        *mp,
    1344             :         struct xfs_fsop_geom    *geo,
    1345             :         int                     struct_version)
    1346             : {
    1347     7224843 :         struct xfs_sb           *sbp = &mp->m_sb;
    1348             : 
    1349     7224843 :         memset(geo, 0, sizeof(struct xfs_fsop_geom));
    1350             : 
    1351     7224843 :         geo->blocksize = sbp->sb_blocksize;
    1352     7224843 :         geo->rtextsize = sbp->sb_rextsize;
    1353     7224843 :         geo->agblocks = sbp->sb_agblocks;
    1354     7224843 :         geo->agcount = sbp->sb_agcount;
    1355     7224843 :         geo->logblocks = sbp->sb_logblocks;
    1356     7224843 :         geo->sectsize = sbp->sb_sectsize;
    1357     7224843 :         geo->inodesize = sbp->sb_inodesize;
    1358     7224843 :         geo->imaxpct = sbp->sb_imax_pct;
    1359     7224843 :         geo->datablocks = sbp->sb_dblocks;
    1360     7224843 :         geo->rtblocks = sbp->sb_rblocks;
    1361     7224843 :         geo->rtextents = sbp->sb_rextents;
    1362     7224843 :         geo->logstart = sbp->sb_logstart;
    1363     7224843 :         BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
    1364    14449686 :         memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
    1365             : 
    1366     7224843 :         if (struct_version < 2)
    1367             :                 return;
    1368             : 
    1369     7224843 :         geo->sunit = sbp->sb_unit;
    1370     7224843 :         geo->swidth = sbp->sb_width;
    1371             : 
    1372     7224843 :         if (struct_version < 3)
    1373             :                 return;
    1374             : 
    1375     7224843 :         geo->version = XFS_FSOP_GEOM_VERSION;
    1376     7224843 :         geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
    1377             :                      XFS_FSOP_GEOM_FLAGS_DIRV2 |
    1378             :                      XFS_FSOP_GEOM_FLAGS_EXTFLG;
    1379     7224843 :         if (xfs_has_attr(mp))
    1380     7212424 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
    1381     7224843 :         if (xfs_has_quota(mp))
    1382     6580583 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
    1383     7224843 :         if (xfs_has_align(mp))
    1384     7208348 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
    1385     7224843 :         if (xfs_has_dalign(mp))
    1386      592577 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
    1387     7224843 :         if (xfs_has_asciici(mp))
    1388         334 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
    1389     7224843 :         if (xfs_has_lazysbcount(mp))
    1390     7214757 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
    1391     7224843 :         if (xfs_has_attr2(mp))
    1392     7217340 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
    1393     7224843 :         if (xfs_has_projid32(mp))
    1394     7218374 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
    1395     7224843 :         if (xfs_has_crc(mp))
    1396     7218480 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
    1397     7224843 :         if (xfs_has_ftype(mp))
    1398     7220922 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
    1399     7224843 :         if (xfs_has_finobt(mp))
    1400     7220701 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
    1401     7224843 :         if (xfs_has_sparseinodes(mp))
    1402     7220532 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
    1403     7224843 :         if (xfs_has_rmapbt(mp))
    1404     6877329 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
    1405     7224843 :         if (xfs_has_reflink(mp))
    1406     6877997 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
    1407     7224843 :         if (xfs_has_bigtime(mp))
    1408     7221254 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
    1409     7224843 :         if (xfs_has_inobtcounts(mp))
    1410     7222534 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
    1411     7224843 :         if (xfs_has_parent(mp))
    1412     6853788 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_PARENT;
    1413     7224843 :         if (xfs_has_sector(mp)) {
    1414     7180408 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
    1415     7180408 :                 geo->logsectsize = sbp->sb_logsectsize;
    1416             :         } else {
    1417       44435 :                 geo->logsectsize = BBSIZE;
    1418             :         }
    1419     7224843 :         if (xfs_has_large_extent_counts(mp))
    1420     7222721 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
    1421     7568225 :         if (xfs_swapext_supported(mp))
    1422     6877150 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATOMIC_SWAP;
    1423     7224843 :         if (xfs_has_metadir(mp))
    1424     6845194 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_METADIR;
    1425     7224843 :         geo->rtsectsize = sbp->sb_blocksize;
    1426     7224843 :         geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
    1427             : 
    1428     7198942 :         if (struct_version < 4)
    1429             :                 return;
    1430             : 
    1431     7198170 :         if (xfs_has_logv2(mp))
    1432     7198585 :                 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
    1433             : 
    1434     7198170 :         geo->logsunit = sbp->sb_logsunit;
    1435             : 
    1436     7198170 :         if (struct_version < 5)
    1437             :                 return;
    1438             : 
    1439     7198170 :         geo->version = XFS_FSOP_GEOM_VERSION_V5;
    1440             : 
    1441     7198170 :         if (xfs_has_rtgroups(mp)) {
    1442     6821690 :                 geo->rgcount = sbp->sb_rgcount;
    1443     6821690 :                 geo->rgblocks = sbp->sb_rgblocks;
    1444             :         }
    1445             : }
    1446             : 
    1447             : /* Read a secondary superblock. */
    1448             : int
    1449      883918 : 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      883918 :         struct xfs_buf          *bp;
    1456      883918 :         int                     error;
    1457             : 
    1458      883918 :         ASSERT(agno != 0 && agno != NULLAGNUMBER);
    1459     3535672 :         error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
    1460      883918 :                         XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
    1461      883918 :                         XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
    1462      884082 :         if (xfs_metadata_is_sick(error))
    1463          88 :                 xfs_agno_mark_sick(mp, agno, XFS_SICK_AG_SB);
    1464      884082 :         if (error)
    1465             :                 return error;
    1466      883899 :         xfs_buf_set_ref(bp, XFS_SSB_REF);
    1467      883874 :         *bpp = bp;
    1468      883874 :         return 0;
    1469             : }
    1470             : 
    1471             : /* Get an uninitialised secondary superblock buffer. */
    1472             : int
    1473      281182 : 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      281182 :         struct xfs_buf          *bp;
    1480      281182 :         int                     error;
    1481             : 
    1482      281182 :         ASSERT(agno != 0 && agno != NULLAGNUMBER);
    1483     1124728 :         error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
    1484      281182 :                         XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
    1485      281182 :                         XFS_FSS_TO_BB(mp, 1), 0, &bp);
    1486      281163 :         if (error)
    1487             :                 return error;
    1488      281163 :         bp->b_ops = &xfs_sb_buf_ops;
    1489      281163 :         xfs_buf_oneshot(bp);
    1490      281163 :         *bpp = bp;
    1491      281163 :         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     1007056 : xfs_validate_stripe_geometry(
    1500             :         struct xfs_mount        *mp,
    1501             :         __s64                   sunit,
    1502             :         __s64                   swidth,
    1503             :         int                     sectorsize,
    1504             :         bool                    silent)
    1505             : {
    1506     1007056 :         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     1007056 :         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     1007056 :         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     1007056 :         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     1007056 :         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     1007056 :         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