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-rc3-acha @ Mon Jul 31 20:08:06 PDT 2023 Lines: 555 634 87.5 %
Date: 2023-07-31 20:08:07 Functions: 22 23 95.7 %

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

Generated by: LCOV version 1.14