Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2000-2005 Silicon Graphics, Inc.
0004  * All Rights Reserved.
0005  */
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_bit.h"
0013 #include "xfs_sb.h"
0014 #include "xfs_mount.h"
0015 #include "xfs_ialloc.h"
0016 #include "xfs_alloc.h"
0017 #include "xfs_error.h"
0018 #include "xfs_trans.h"
0019 #include "xfs_buf_item.h"
0020 #include "xfs_bmap_btree.h"
0021 #include "xfs_alloc_btree.h"
0022 #include "xfs_log.h"
0023 #include "xfs_rmap_btree.h"
0024 #include "xfs_refcount_btree.h"
0025 #include "xfs_da_format.h"
0026 #include "xfs_health.h"
0027 #include "xfs_ag.h"
0028 
0029 /*
0030  * Physical superblock buffer manipulations. Shared with libxfs in userspace.
0031  */
0032 
0033 /*
0034  * Check that all the V4 feature bits that the V5 filesystem format requires are
0035  * correctly set.
0036  */
0037 static bool
0038 xfs_sb_validate_v5_features(
0039     struct xfs_sb   *sbp)
0040 {
0041     /* We must not have any unknown V4 feature bits set */
0042     if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
0043         return false;
0044 
0045     /*
0046      * The CRC bit is considered an invalid V4 flag, so we have to add it
0047      * manually to the OKBITS mask.
0048      */
0049     if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
0050                   XFS_SB_VERSION2_CRCBIT))
0051         return false;
0052 
0053     /* Now check all the required V4 feature flags are set. */
0054 
0055 #define V5_VERS_FLAGS   (XFS_SB_VERSION_NLINKBIT    | \
0056             XFS_SB_VERSION_ALIGNBIT     | \
0057             XFS_SB_VERSION_LOGV2BIT     | \
0058             XFS_SB_VERSION_EXTFLGBIT    | \
0059             XFS_SB_VERSION_DIRV2BIT     | \
0060             XFS_SB_VERSION_MOREBITSBIT)
0061 
0062 #define V5_FEAT_FLAGS   (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
0063             XFS_SB_VERSION2_ATTR2BIT    | \
0064             XFS_SB_VERSION2_PROJID32BIT | \
0065             XFS_SB_VERSION2_CRCBIT)
0066 
0067     if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
0068         return false;
0069     if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
0070         return false;
0071     return true;
0072 }
0073 
0074 /*
0075  * We support all XFS versions newer than a v4 superblock with V2 directories.
0076  */
0077 bool
0078 xfs_sb_good_version(
0079     struct xfs_sb   *sbp)
0080 {
0081     /*
0082      * All v5 filesystems are supported, but we must check that all the
0083      * required v4 feature flags are enabled correctly as the code checks
0084      * those flags and not for v5 support.
0085      */
0086     if (xfs_sb_is_v5(sbp))
0087         return xfs_sb_validate_v5_features(sbp);
0088 
0089     /* We must not have any unknown v4 feature bits set */
0090     if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
0091         ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
0092          (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
0093         return false;
0094 
0095     /* versions prior to v4 are not supported */
0096     if (XFS_SB_VERSION_NUM(sbp) < XFS_SB_VERSION_4)
0097         return false;
0098 
0099     /* V4 filesystems need v2 directories and unwritten extents */
0100     if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
0101         return false;
0102     if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
0103         return false;
0104 
0105     /* It's a supported v4 filesystem */
0106     return true;
0107 }
0108 
0109 uint64_t
0110 xfs_sb_version_to_features(
0111     struct xfs_sb   *sbp)
0112 {
0113     uint64_t    features = 0;
0114 
0115     /* optional V4 features */
0116     if (sbp->sb_rblocks > 0)
0117         features |= XFS_FEAT_REALTIME;
0118     if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
0119         features |= XFS_FEAT_NLINK;
0120     if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
0121         features |= XFS_FEAT_ATTR;
0122     if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
0123         features |= XFS_FEAT_QUOTA;
0124     if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
0125         features |= XFS_FEAT_ALIGN;
0126     if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
0127         features |= XFS_FEAT_LOGV2;
0128     if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
0129         features |= XFS_FEAT_DALIGN;
0130     if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
0131         features |= XFS_FEAT_EXTFLG;
0132     if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
0133         features |= XFS_FEAT_SECTOR;
0134     if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
0135         features |= XFS_FEAT_ASCIICI;
0136     if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
0137         if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
0138             features |= XFS_FEAT_LAZYSBCOUNT;
0139         if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
0140             features |= XFS_FEAT_ATTR2;
0141         if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
0142             features |= XFS_FEAT_PROJID32;
0143         if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
0144             features |= XFS_FEAT_FTYPE;
0145     }
0146 
0147     if (!xfs_sb_is_v5(sbp))
0148         return features;
0149 
0150     /* Always on V5 features */
0151     features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
0152             XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
0153             XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
0154 
0155     /* Optional V5 features */
0156     if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
0157         features |= XFS_FEAT_FINOBT;
0158     if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
0159         features |= XFS_FEAT_RMAPBT;
0160     if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
0161         features |= XFS_FEAT_REFLINK;
0162     if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
0163         features |= XFS_FEAT_INOBTCNT;
0164     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
0165         features |= XFS_FEAT_FTYPE;
0166     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
0167         features |= XFS_FEAT_SPINODES;
0168     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
0169         features |= XFS_FEAT_META_UUID;
0170     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
0171         features |= XFS_FEAT_BIGTIME;
0172     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
0173         features |= XFS_FEAT_NEEDSREPAIR;
0174     if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
0175         features |= XFS_FEAT_NREXT64;
0176 
0177     return features;
0178 }
0179 
0180 /* Check all the superblock fields we care about when reading one in. */
0181 STATIC int
0182 xfs_validate_sb_read(
0183     struct xfs_mount    *mp,
0184     struct xfs_sb       *sbp)
0185 {
0186     if (!xfs_sb_is_v5(sbp))
0187         return 0;
0188 
0189     /*
0190      * Version 5 superblock feature mask validation. Reject combinations
0191      * the kernel cannot support up front before checking anything else.
0192      */
0193     if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
0194         xfs_warn(mp,
0195 "Superblock has unknown compatible features (0x%x) enabled.",
0196             (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
0197         xfs_warn(mp,
0198 "Using a more recent kernel is recommended.");
0199     }
0200 
0201     if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
0202         xfs_alert(mp,
0203 "Superblock has unknown read-only compatible features (0x%x) enabled.",
0204             (sbp->sb_features_ro_compat &
0205                     XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
0206         if (!xfs_is_readonly(mp)) {
0207             xfs_warn(mp,
0208 "Attempted to mount read-only compatible filesystem read-write.");
0209             xfs_warn(mp,
0210 "Filesystem can only be safely mounted read only.");
0211 
0212             return -EINVAL;
0213         }
0214     }
0215     if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
0216         xfs_warn(mp,
0217 "Superblock has unknown incompatible features (0x%x) enabled.",
0218             (sbp->sb_features_incompat &
0219                     XFS_SB_FEAT_INCOMPAT_UNKNOWN));
0220         xfs_warn(mp,
0221 "Filesystem cannot be safely mounted by this kernel.");
0222         return -EINVAL;
0223     }
0224 
0225     return 0;
0226 }
0227 
0228 /* Check all the superblock fields we care about when writing one out. */
0229 STATIC int
0230 xfs_validate_sb_write(
0231     struct xfs_mount    *mp,
0232     struct xfs_buf      *bp,
0233     struct xfs_sb       *sbp)
0234 {
0235     /*
0236      * Carry out additional sb summary counter sanity checks when we write
0237      * the superblock.  We skip this in the read validator because there
0238      * could be newer superblocks in the log and if the values are garbage
0239      * even after replay we'll recalculate them at the end of log mount.
0240      *
0241      * mkfs has traditionally written zeroed counters to inprogress and
0242      * secondary superblocks, so allow this usage to continue because
0243      * we never read counters from such superblocks.
0244      */
0245     if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
0246         (sbp->sb_fdblocks > sbp->sb_dblocks ||
0247          !xfs_verify_icount(mp, sbp->sb_icount) ||
0248          sbp->sb_ifree > sbp->sb_icount)) {
0249         xfs_warn(mp, "SB summary counter sanity check failed");
0250         return -EFSCORRUPTED;
0251     }
0252 
0253     if (!xfs_sb_is_v5(sbp))
0254         return 0;
0255 
0256     /*
0257      * Version 5 superblock feature mask validation. Reject combinations
0258      * the kernel cannot support since we checked for unsupported bits in
0259      * the read verifier, which means that memory is corrupt.
0260      */
0261     if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
0262         xfs_warn(mp,
0263 "Corruption detected in superblock compatible features (0x%x)!",
0264             (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
0265         return -EFSCORRUPTED;
0266     }
0267 
0268     if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
0269         xfs_alert(mp,
0270 "Corruption detected in superblock read-only compatible features (0x%x)!",
0271             (sbp->sb_features_ro_compat &
0272                     XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
0273         return -EFSCORRUPTED;
0274     }
0275     if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
0276         xfs_warn(mp,
0277 "Corruption detected in superblock incompatible features (0x%x)!",
0278             (sbp->sb_features_incompat &
0279                     XFS_SB_FEAT_INCOMPAT_UNKNOWN));
0280         return -EFSCORRUPTED;
0281     }
0282     if (xfs_sb_has_incompat_log_feature(sbp,
0283             XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
0284         xfs_warn(mp,
0285 "Corruption detected in superblock incompatible log features (0x%x)!",
0286             (sbp->sb_features_log_incompat &
0287                     XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
0288         return -EFSCORRUPTED;
0289     }
0290 
0291     /*
0292      * We can't read verify the sb LSN because the read verifier is called
0293      * before the log is allocated and processed. We know the log is set up
0294      * before write verifier calls, so check it here.
0295      */
0296     if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
0297         return -EFSCORRUPTED;
0298 
0299     return 0;
0300 }
0301 
0302 /* Check the validity of the SB. */
0303 STATIC int
0304 xfs_validate_sb_common(
0305     struct xfs_mount    *mp,
0306     struct xfs_buf      *bp,
0307     struct xfs_sb       *sbp)
0308 {
0309     struct xfs_dsb      *dsb = bp->b_addr;
0310     uint32_t        agcount = 0;
0311     uint32_t        rem;
0312     bool            has_dalign;
0313 
0314     if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
0315         xfs_warn(mp,
0316 "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
0317             be32_to_cpu(dsb->sb_magicnum));
0318         return -EWRONGFS;
0319     }
0320 
0321     if (!xfs_sb_good_version(sbp)) {
0322         xfs_warn(mp,
0323 "Superblock has unknown features enabled or corrupted feature masks.");
0324         return -EWRONGFS;
0325     }
0326 
0327     /*
0328      * Validate feature flags and state
0329      */
0330     if (xfs_sb_is_v5(sbp)) {
0331         if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
0332             xfs_notice(mp,
0333 "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
0334                 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
0335             return -EFSCORRUPTED;
0336         }
0337 
0338         /* V5 has a separate project quota inode */
0339         if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
0340             xfs_notice(mp,
0341                "Version 5 of Super block has XFS_OQUOTA bits.");
0342             return -EFSCORRUPTED;
0343         }
0344 
0345         /*
0346          * Full inode chunks must be aligned to inode chunk size when
0347          * sparse inodes are enabled to support the sparse chunk
0348          * allocation algorithm and prevent overlapping inode records.
0349          */
0350         if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
0351             uint32_t    align;
0352 
0353             align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
0354                     >> sbp->sb_blocklog;
0355             if (sbp->sb_inoalignmt != align) {
0356                 xfs_warn(mp,
0357 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
0358                      sbp->sb_inoalignmt, align);
0359                 return -EINVAL;
0360             }
0361         }
0362     } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
0363                 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
0364             xfs_notice(mp,
0365 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
0366             return -EFSCORRUPTED;
0367     }
0368 
0369     if (unlikely(
0370         sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
0371         xfs_warn(mp,
0372         "filesystem is marked as having an external log; "
0373         "specify logdev on the mount command line.");
0374         return -EINVAL;
0375     }
0376 
0377     if (unlikely(
0378         sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
0379         xfs_warn(mp,
0380         "filesystem is marked as having an internal log; "
0381         "do not specify logdev on the mount command line.");
0382         return -EINVAL;
0383     }
0384 
0385     /* Compute agcount for this number of dblocks and agblocks */
0386     if (sbp->sb_agblocks) {
0387         agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
0388         if (rem)
0389             agcount++;
0390     }
0391 
0392     /*
0393      * More sanity checking.  Most of these were stolen directly from
0394      * xfs_repair.
0395      */
0396     if (unlikely(
0397         sbp->sb_agcount <= 0                    ||
0398         sbp->sb_sectsize < XFS_MIN_SECTORSIZE           ||
0399         sbp->sb_sectsize > XFS_MAX_SECTORSIZE           ||
0400         sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG            ||
0401         sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG            ||
0402         sbp->sb_sectsize != (1 << sbp->sb_sectlog)          ||
0403         sbp->sb_blocksize < XFS_MIN_BLOCKSIZE           ||
0404         sbp->sb_blocksize > XFS_MAX_BLOCKSIZE           ||
0405         sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG            ||
0406         sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG            ||
0407         sbp->sb_blocksize != (1 << sbp->sb_blocklog)        ||
0408         sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
0409         sbp->sb_inodesize < XFS_DINODE_MIN_SIZE         ||
0410         sbp->sb_inodesize > XFS_DINODE_MAX_SIZE         ||
0411         sbp->sb_inodelog < XFS_DINODE_MIN_LOG           ||
0412         sbp->sb_inodelog > XFS_DINODE_MAX_LOG           ||
0413         sbp->sb_inodesize != (1 << sbp->sb_inodelog)        ||
0414         sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE            ||
0415         sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
0416         XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES   ||
0417         XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES   ||
0418         sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
0419         agcount == 0 || agcount != sbp->sb_agcount          ||
0420         (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)   ||
0421         (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)  ||
0422         (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)  ||
0423         (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)    ||
0424         sbp->sb_dblocks == 0                    ||
0425         sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)          ||
0426         sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp)          ||
0427         sbp->sb_shared_vn != 0)) {
0428         xfs_notice(mp, "SB sanity check failed");
0429         return -EFSCORRUPTED;
0430     }
0431 
0432     /* Validate the realtime geometry; stolen from xfs_repair */
0433     if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
0434         sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
0435         xfs_notice(mp,
0436             "realtime extent sanity check failed");
0437         return -EFSCORRUPTED;
0438     }
0439 
0440     if (sbp->sb_rblocks == 0) {
0441         if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
0442             sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
0443             xfs_notice(mp,
0444                 "realtime zeroed geometry check failed");
0445             return -EFSCORRUPTED;
0446         }
0447     } else {
0448         uint64_t    rexts;
0449         uint64_t    rbmblocks;
0450 
0451         rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
0452         rbmblocks = howmany_64(sbp->sb_rextents,
0453                        NBBY * sbp->sb_blocksize);
0454 
0455         if (sbp->sb_rextents != rexts ||
0456             sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
0457             sbp->sb_rbmblocks != rbmblocks) {
0458             xfs_notice(mp,
0459                 "realtime geometry sanity check failed");
0460             return -EFSCORRUPTED;
0461         }
0462     }
0463 
0464     /*
0465      * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
0466      * would imply the image is corrupted.
0467      */
0468     has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
0469     if (!!sbp->sb_unit ^ has_dalign) {
0470         xfs_notice(mp, "SB stripe alignment sanity check failed");
0471         return -EFSCORRUPTED;
0472     }
0473 
0474     if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
0475             XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
0476         return -EFSCORRUPTED;
0477 
0478     /*
0479      * Currently only very few inode sizes are supported.
0480      */
0481     switch (sbp->sb_inodesize) {
0482     case 256:
0483     case 512:
0484     case 1024:
0485     case 2048:
0486         break;
0487     default:
0488         xfs_warn(mp, "inode size of %d bytes not supported",
0489                 sbp->sb_inodesize);
0490         return -ENOSYS;
0491     }
0492 
0493     return 0;
0494 }
0495 
0496 void
0497 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
0498 {
0499     /*
0500      * older mkfs doesn't initialize quota inodes to NULLFSINO. This
0501      * leads to in-core values having two different values for a quota
0502      * inode to be invalid: 0 and NULLFSINO. Change it to a single value
0503      * NULLFSINO.
0504      *
0505      * Note that this change affect only the in-core values. These
0506      * values are not written back to disk unless any quota information
0507      * is written to the disk. Even in that case, sb_pquotino field is
0508      * not written to disk unless the superblock supports pquotino.
0509      */
0510     if (sbp->sb_uquotino == 0)
0511         sbp->sb_uquotino = NULLFSINO;
0512     if (sbp->sb_gquotino == 0)
0513         sbp->sb_gquotino = NULLFSINO;
0514     if (sbp->sb_pquotino == 0)
0515         sbp->sb_pquotino = NULLFSINO;
0516 
0517     /*
0518      * We need to do these manipilations only if we are working
0519      * with an older version of on-disk superblock.
0520      */
0521     if (xfs_sb_is_v5(sbp))
0522         return;
0523 
0524     if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
0525         sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
0526                     XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
0527     if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
0528         sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
0529                     XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
0530     sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
0531 
0532     if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
0533         sbp->sb_gquotino != NULLFSINO)  {
0534         /*
0535          * In older version of superblock, on-disk superblock only
0536          * has sb_gquotino, and in-core superblock has both sb_gquotino
0537          * and sb_pquotino. But, only one of them is supported at any
0538          * point of time. So, if PQUOTA is set in disk superblock,
0539          * copy over sb_gquotino to sb_pquotino.  The NULLFSINO test
0540          * above is to make sure we don't do this twice and wipe them
0541          * both out!
0542          */
0543         sbp->sb_pquotino = sbp->sb_gquotino;
0544         sbp->sb_gquotino = NULLFSINO;
0545     }
0546 }
0547 
0548 static void
0549 __xfs_sb_from_disk(
0550     struct xfs_sb   *to,
0551     struct xfs_dsb  *from,
0552     bool        convert_xquota)
0553 {
0554     to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
0555     to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
0556     to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
0557     to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
0558     to->sb_rextents = be64_to_cpu(from->sb_rextents);
0559     memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
0560     to->sb_logstart = be64_to_cpu(from->sb_logstart);
0561     to->sb_rootino = be64_to_cpu(from->sb_rootino);
0562     to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
0563     to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
0564     to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
0565     to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
0566     to->sb_agcount = be32_to_cpu(from->sb_agcount);
0567     to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
0568     to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
0569     to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
0570     to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
0571     to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
0572     to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
0573     memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
0574     to->sb_blocklog = from->sb_blocklog;
0575     to->sb_sectlog = from->sb_sectlog;
0576     to->sb_inodelog = from->sb_inodelog;
0577     to->sb_inopblog = from->sb_inopblog;
0578     to->sb_agblklog = from->sb_agblklog;
0579     to->sb_rextslog = from->sb_rextslog;
0580     to->sb_inprogress = from->sb_inprogress;
0581     to->sb_imax_pct = from->sb_imax_pct;
0582     to->sb_icount = be64_to_cpu(from->sb_icount);
0583     to->sb_ifree = be64_to_cpu(from->sb_ifree);
0584     to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
0585     to->sb_frextents = be64_to_cpu(from->sb_frextents);
0586     to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
0587     to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
0588     to->sb_qflags = be16_to_cpu(from->sb_qflags);
0589     to->sb_flags = from->sb_flags;
0590     to->sb_shared_vn = from->sb_shared_vn;
0591     to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
0592     to->sb_unit = be32_to_cpu(from->sb_unit);
0593     to->sb_width = be32_to_cpu(from->sb_width);
0594     to->sb_dirblklog = from->sb_dirblklog;
0595     to->sb_logsectlog = from->sb_logsectlog;
0596     to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
0597     to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
0598     to->sb_features2 = be32_to_cpu(from->sb_features2);
0599     to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
0600     to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
0601     to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
0602     to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
0603     to->sb_features_log_incompat =
0604                 be32_to_cpu(from->sb_features_log_incompat);
0605     /* crc is only used on disk, not in memory; just init to 0 here. */
0606     to->sb_crc = 0;
0607     to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
0608     to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
0609     to->sb_lsn = be64_to_cpu(from->sb_lsn);
0610     /*
0611      * sb_meta_uuid is only on disk if it differs from sb_uuid and the
0612      * feature flag is set; if not set we keep it only in memory.
0613      */
0614     if (xfs_sb_is_v5(to) &&
0615         (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
0616         uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
0617     else
0618         uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
0619     /* Convert on-disk flags to in-memory flags? */
0620     if (convert_xquota)
0621         xfs_sb_quota_from_disk(to);
0622 }
0623 
0624 void
0625 xfs_sb_from_disk(
0626     struct xfs_sb   *to,
0627     struct xfs_dsb  *from)
0628 {
0629     __xfs_sb_from_disk(to, from, true);
0630 }
0631 
0632 static void
0633 xfs_sb_quota_to_disk(
0634     struct xfs_dsb  *to,
0635     struct xfs_sb   *from)
0636 {
0637     uint16_t    qflags = from->sb_qflags;
0638 
0639     to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
0640 
0641     /*
0642      * The in-memory superblock quota state matches the v5 on-disk format so
0643      * just write them out and return
0644      */
0645     if (xfs_sb_is_v5(from)) {
0646         to->sb_qflags = cpu_to_be16(from->sb_qflags);
0647         to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
0648         to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
0649         return;
0650     }
0651 
0652     /*
0653      * For older superblocks (v4), the in-core version of sb_qflags do not
0654      * have XFS_OQUOTA_* flags, whereas the on-disk version does.  So,
0655      * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
0656      */
0657     qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
0658             XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
0659 
0660     if (from->sb_qflags &
0661             (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
0662         qflags |= XFS_OQUOTA_ENFD;
0663     if (from->sb_qflags &
0664             (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
0665         qflags |= XFS_OQUOTA_CHKD;
0666     to->sb_qflags = cpu_to_be16(qflags);
0667 
0668     /*
0669      * GQUOTINO and PQUOTINO cannot be used together in versions
0670      * of superblock that do not have pquotino. from->sb_flags
0671      * tells us which quota is active and should be copied to
0672      * disk. If neither are active, we should NULL the inode.
0673      *
0674      * In all cases, the separate pquotino must remain 0 because it
0675      * is beyond the "end" of the valid non-pquotino superblock.
0676      */
0677     if (from->sb_qflags & XFS_GQUOTA_ACCT)
0678         to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
0679     else if (from->sb_qflags & XFS_PQUOTA_ACCT)
0680         to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
0681     else {
0682         /*
0683          * We can't rely on just the fields being logged to tell us
0684          * that it is safe to write NULLFSINO - we should only do that
0685          * if quotas are not actually enabled. Hence only write
0686          * NULLFSINO if both in-core quota inodes are NULL.
0687          */
0688         if (from->sb_gquotino == NULLFSINO &&
0689             from->sb_pquotino == NULLFSINO)
0690             to->sb_gquotino = cpu_to_be64(NULLFSINO);
0691     }
0692 
0693     to->sb_pquotino = 0;
0694 }
0695 
0696 void
0697 xfs_sb_to_disk(
0698     struct xfs_dsb  *to,
0699     struct xfs_sb   *from)
0700 {
0701     xfs_sb_quota_to_disk(to, from);
0702 
0703     to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
0704     to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
0705     to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
0706     to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
0707     to->sb_rextents = cpu_to_be64(from->sb_rextents);
0708     memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
0709     to->sb_logstart = cpu_to_be64(from->sb_logstart);
0710     to->sb_rootino = cpu_to_be64(from->sb_rootino);
0711     to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
0712     to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
0713     to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
0714     to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
0715     to->sb_agcount = cpu_to_be32(from->sb_agcount);
0716     to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
0717     to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
0718     to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
0719     to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
0720     to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
0721     to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
0722     memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
0723     to->sb_blocklog = from->sb_blocklog;
0724     to->sb_sectlog = from->sb_sectlog;
0725     to->sb_inodelog = from->sb_inodelog;
0726     to->sb_inopblog = from->sb_inopblog;
0727     to->sb_agblklog = from->sb_agblklog;
0728     to->sb_rextslog = from->sb_rextslog;
0729     to->sb_inprogress = from->sb_inprogress;
0730     to->sb_imax_pct = from->sb_imax_pct;
0731     to->sb_icount = cpu_to_be64(from->sb_icount);
0732     to->sb_ifree = cpu_to_be64(from->sb_ifree);
0733     to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
0734     to->sb_frextents = cpu_to_be64(from->sb_frextents);
0735 
0736     to->sb_flags = from->sb_flags;
0737     to->sb_shared_vn = from->sb_shared_vn;
0738     to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
0739     to->sb_unit = cpu_to_be32(from->sb_unit);
0740     to->sb_width = cpu_to_be32(from->sb_width);
0741     to->sb_dirblklog = from->sb_dirblklog;
0742     to->sb_logsectlog = from->sb_logsectlog;
0743     to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
0744     to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
0745 
0746     /*
0747      * We need to ensure that bad_features2 always matches features2.
0748      * Hence we enforce that here rather than having to remember to do it
0749      * everywhere else that updates features2.
0750      */
0751     from->sb_bad_features2 = from->sb_features2;
0752     to->sb_features2 = cpu_to_be32(from->sb_features2);
0753     to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
0754 
0755     if (!xfs_sb_is_v5(from))
0756         return;
0757 
0758     to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
0759     to->sb_features_ro_compat =
0760             cpu_to_be32(from->sb_features_ro_compat);
0761     to->sb_features_incompat =
0762             cpu_to_be32(from->sb_features_incompat);
0763     to->sb_features_log_incompat =
0764             cpu_to_be32(from->sb_features_log_incompat);
0765     to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
0766     to->sb_lsn = cpu_to_be64(from->sb_lsn);
0767     if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
0768         uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
0769 }
0770 
0771 /*
0772  * If the superblock has the CRC feature bit set or the CRC field is non-null,
0773  * check that the CRC is valid.  We check the CRC field is non-null because a
0774  * single bit error could clear the feature bit and unused parts of the
0775  * superblock are supposed to be zero. Hence a non-null crc field indicates that
0776  * we've potentially lost a feature bit and we should check it anyway.
0777  *
0778  * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
0779  * last field in V4 secondary superblocks.  So for secondary superblocks,
0780  * we are more forgiving, and ignore CRC failures if the primary doesn't
0781  * indicate that the fs version is V5.
0782  */
0783 static void
0784 xfs_sb_read_verify(
0785     struct xfs_buf      *bp)
0786 {
0787     struct xfs_sb       sb;
0788     struct xfs_mount    *mp = bp->b_mount;
0789     struct xfs_dsb      *dsb = bp->b_addr;
0790     int         error;
0791 
0792     /*
0793      * open code the version check to avoid needing to convert the entire
0794      * superblock from disk order just to check the version number
0795      */
0796     if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
0797         (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
0798                         XFS_SB_VERSION_5) ||
0799          dsb->sb_crc != 0)) {
0800 
0801         if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
0802             /* Only fail bad secondaries on a known V5 filesystem */
0803             if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
0804                 xfs_has_crc(mp)) {
0805                 error = -EFSBADCRC;
0806                 goto out_error;
0807             }
0808         }
0809     }
0810 
0811     /*
0812      * Check all the superblock fields.  Don't byteswap the xquota flags
0813      * because _verify_common checks the on-disk values.
0814      */
0815     __xfs_sb_from_disk(&sb, dsb, false);
0816     error = xfs_validate_sb_common(mp, bp, &sb);
0817     if (error)
0818         goto out_error;
0819     error = xfs_validate_sb_read(mp, &sb);
0820 
0821 out_error:
0822     if (error == -EFSCORRUPTED || error == -EFSBADCRC)
0823         xfs_verifier_error(bp, error, __this_address);
0824     else if (error)
0825         xfs_buf_ioerror(bp, error);
0826 }
0827 
0828 /*
0829  * We may be probed for a filesystem match, so we may not want to emit
0830  * messages when the superblock buffer is not actually an XFS superblock.
0831  * If we find an XFS superblock, then run a normal, noisy mount because we are
0832  * really going to mount it and want to know about errors.
0833  */
0834 static void
0835 xfs_sb_quiet_read_verify(
0836     struct xfs_buf  *bp)
0837 {
0838     struct xfs_dsb  *dsb = bp->b_addr;
0839 
0840     if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
0841         /* XFS filesystem, verify noisily! */
0842         xfs_sb_read_verify(bp);
0843         return;
0844     }
0845     /* quietly fail */
0846     xfs_buf_ioerror(bp, -EWRONGFS);
0847 }
0848 
0849 static void
0850 xfs_sb_write_verify(
0851     struct xfs_buf      *bp)
0852 {
0853     struct xfs_sb       sb;
0854     struct xfs_mount    *mp = bp->b_mount;
0855     struct xfs_buf_log_item *bip = bp->b_log_item;
0856     struct xfs_dsb      *dsb = bp->b_addr;
0857     int         error;
0858 
0859     /*
0860      * Check all the superblock fields.  Don't byteswap the xquota flags
0861      * because _verify_common checks the on-disk values.
0862      */
0863     __xfs_sb_from_disk(&sb, dsb, false);
0864     error = xfs_validate_sb_common(mp, bp, &sb);
0865     if (error)
0866         goto out_error;
0867     error = xfs_validate_sb_write(mp, bp, &sb);
0868     if (error)
0869         goto out_error;
0870 
0871     if (!xfs_sb_is_v5(&sb))
0872         return;
0873 
0874     if (bip)
0875         dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
0876 
0877     xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
0878     return;
0879 
0880 out_error:
0881     xfs_verifier_error(bp, error, __this_address);
0882 }
0883 
0884 const struct xfs_buf_ops xfs_sb_buf_ops = {
0885     .name = "xfs_sb",
0886     .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
0887     .verify_read = xfs_sb_read_verify,
0888     .verify_write = xfs_sb_write_verify,
0889 };
0890 
0891 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
0892     .name = "xfs_sb_quiet",
0893     .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
0894     .verify_read = xfs_sb_quiet_read_verify,
0895     .verify_write = xfs_sb_write_verify,
0896 };
0897 
0898 /*
0899  * xfs_mount_common
0900  *
0901  * Mount initialization code establishing various mount
0902  * fields from the superblock associated with the given
0903  * mount structure.
0904  *
0905  * Inode geometry are calculated in xfs_ialloc_setup_geometry.
0906  */
0907 void
0908 xfs_sb_mount_common(
0909     struct xfs_mount    *mp,
0910     struct xfs_sb       *sbp)
0911 {
0912     mp->m_agfrotor = mp->m_agirotor = 0;
0913     mp->m_maxagi = mp->m_sb.sb_agcount;
0914     mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
0915     mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
0916     mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
0917     mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
0918     mp->m_blockmask = sbp->sb_blocksize - 1;
0919     mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
0920     mp->m_blockwmask = mp->m_blockwsize - 1;
0921 
0922     mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
0923     mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
0924     mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
0925     mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
0926 
0927     mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
0928     mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
0929     mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
0930     mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
0931 
0932     mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
0933     mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
0934     mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
0935     mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
0936 
0937     mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
0938     mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
0939     mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
0940     mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
0941 
0942     mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
0943     mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
0944     mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
0945 }
0946 
0947 /*
0948  * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
0949  * into the superblock buffer to be logged.  It does not provide the higher
0950  * level of locking that is needed to protect the in-core superblock from
0951  * concurrent access.
0952  */
0953 void
0954 xfs_log_sb(
0955     struct xfs_trans    *tp)
0956 {
0957     struct xfs_mount    *mp = tp->t_mountp;
0958     struct xfs_buf      *bp = xfs_trans_getsb(tp);
0959 
0960     /*
0961      * Lazy sb counters don't update the in-core superblock so do that now.
0962      * If this is at unmount, the counters will be exactly correct, but at
0963      * any other time they will only be ballpark correct because of
0964      * reservations that have been taken out percpu counters. If we have an
0965      * unclean shutdown, this will be corrected by log recovery rebuilding
0966      * the counters from the AGF block counts.
0967      *
0968      * Do not update sb_frextents here because it is not part of the lazy
0969      * sb counters, despite having a percpu counter. It is always kept
0970      * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas()
0971      * and hence we don't need have to update it here.
0972      */
0973     if (xfs_has_lazysbcount(mp)) {
0974         mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
0975         mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
0976         mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
0977     }
0978 
0979     xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
0980     xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
0981     xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
0982 }
0983 
0984 /*
0985  * xfs_sync_sb
0986  *
0987  * Sync the superblock to disk.
0988  *
0989  * Note that the caller is responsible for checking the frozen state of the
0990  * filesystem. This procedure uses the non-blocking transaction allocator and
0991  * thus will allow modifications to a frozen fs. This is required because this
0992  * code can be called during the process of freezing where use of the high-level
0993  * allocator would deadlock.
0994  */
0995 int
0996 xfs_sync_sb(
0997     struct xfs_mount    *mp,
0998     bool            wait)
0999 {
1000     struct xfs_trans    *tp;
1001     int         error;
1002 
1003     error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
1004             XFS_TRANS_NO_WRITECOUNT, &tp);
1005     if (error)
1006         return error;
1007 
1008     xfs_log_sb(tp);
1009     if (wait)
1010         xfs_trans_set_sync(tp);
1011     return xfs_trans_commit(tp);
1012 }
1013 
1014 /*
1015  * Update all the secondary superblocks to match the new state of the primary.
1016  * Because we are completely overwriting all the existing fields in the
1017  * secondary superblock buffers, there is no need to read them in from disk.
1018  * Just get a new buffer, stamp it and write it.
1019  *
1020  * The sb buffers need to be cached here so that we serialise against other
1021  * operations that access the secondary superblocks, but we don't want to keep
1022  * them in memory once it is written so we mark it as a one-shot buffer.
1023  */
1024 int
1025 xfs_update_secondary_sbs(
1026     struct xfs_mount    *mp)
1027 {
1028     struct xfs_perag    *pag;
1029     xfs_agnumber_t      agno = 1;
1030     int         saved_error = 0;
1031     int         error = 0;
1032     LIST_HEAD       (buffer_list);
1033 
1034     /* update secondary superblocks. */
1035     for_each_perag_from(mp, agno, pag) {
1036         struct xfs_buf      *bp;
1037 
1038         error = xfs_buf_get(mp->m_ddev_targp,
1039                  XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
1040                  XFS_FSS_TO_BB(mp, 1), &bp);
1041         /*
1042          * If we get an error reading or writing alternate superblocks,
1043          * continue.  xfs_repair chooses the "best" superblock based
1044          * on most matches; if we break early, we'll leave more
1045          * superblocks un-updated than updated, and xfs_repair may
1046          * pick them over the properly-updated primary.
1047          */
1048         if (error) {
1049             xfs_warn(mp,
1050         "error allocating secondary superblock for ag %d",
1051                 pag->pag_agno);
1052             if (!saved_error)
1053                 saved_error = error;
1054             continue;
1055         }
1056 
1057         bp->b_ops = &xfs_sb_buf_ops;
1058         xfs_buf_oneshot(bp);
1059         xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1060         xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1061         xfs_buf_delwri_queue(bp, &buffer_list);
1062         xfs_buf_relse(bp);
1063 
1064         /* don't hold too many buffers at once */
1065         if (agno % 16)
1066             continue;
1067 
1068         error = xfs_buf_delwri_submit(&buffer_list);
1069         if (error) {
1070             xfs_warn(mp,
1071         "write error %d updating a secondary superblock near ag %d",
1072                 error, pag->pag_agno);
1073             if (!saved_error)
1074                 saved_error = error;
1075             continue;
1076         }
1077     }
1078     error = xfs_buf_delwri_submit(&buffer_list);
1079     if (error) {
1080         xfs_warn(mp,
1081         "write error %d updating a secondary superblock near ag %d",
1082             error, agno);
1083     }
1084 
1085     return saved_error ? saved_error : error;
1086 }
1087 
1088 /*
1089  * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1090  * also writes the superblock buffer to disk sector 0 immediately.
1091  */
1092 int
1093 xfs_sync_sb_buf(
1094     struct xfs_mount    *mp)
1095 {
1096     struct xfs_trans    *tp;
1097     struct xfs_buf      *bp;
1098     int         error;
1099 
1100     error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1101     if (error)
1102         return error;
1103 
1104     bp = xfs_trans_getsb(tp);
1105     xfs_log_sb(tp);
1106     xfs_trans_bhold(tp, bp);
1107     xfs_trans_set_sync(tp);
1108     error = xfs_trans_commit(tp);
1109     if (error)
1110         goto out;
1111     /*
1112      * write out the sb buffer to get the changes to disk
1113      */
1114     error = xfs_bwrite(bp);
1115 out:
1116     xfs_buf_relse(bp);
1117     return error;
1118 }
1119 
1120 void
1121 xfs_fs_geometry(
1122     struct xfs_mount    *mp,
1123     struct xfs_fsop_geom    *geo,
1124     int         struct_version)
1125 {
1126     struct xfs_sb       *sbp = &mp->m_sb;
1127 
1128     memset(geo, 0, sizeof(struct xfs_fsop_geom));
1129 
1130     geo->blocksize = sbp->sb_blocksize;
1131     geo->rtextsize = sbp->sb_rextsize;
1132     geo->agblocks = sbp->sb_agblocks;
1133     geo->agcount = sbp->sb_agcount;
1134     geo->logblocks = sbp->sb_logblocks;
1135     geo->sectsize = sbp->sb_sectsize;
1136     geo->inodesize = sbp->sb_inodesize;
1137     geo->imaxpct = sbp->sb_imax_pct;
1138     geo->datablocks = sbp->sb_dblocks;
1139     geo->rtblocks = sbp->sb_rblocks;
1140     geo->rtextents = sbp->sb_rextents;
1141     geo->logstart = sbp->sb_logstart;
1142     BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1143     memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1144 
1145     if (struct_version < 2)
1146         return;
1147 
1148     geo->sunit = sbp->sb_unit;
1149     geo->swidth = sbp->sb_width;
1150 
1151     if (struct_version < 3)
1152         return;
1153 
1154     geo->version = XFS_FSOP_GEOM_VERSION;
1155     geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1156              XFS_FSOP_GEOM_FLAGS_DIRV2 |
1157              XFS_FSOP_GEOM_FLAGS_EXTFLG;
1158     if (xfs_has_attr(mp))
1159         geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1160     if (xfs_has_quota(mp))
1161         geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1162     if (xfs_has_align(mp))
1163         geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1164     if (xfs_has_dalign(mp))
1165         geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1166     if (xfs_has_asciici(mp))
1167         geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1168     if (xfs_has_lazysbcount(mp))
1169         geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1170     if (xfs_has_attr2(mp))
1171         geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1172     if (xfs_has_projid32(mp))
1173         geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1174     if (xfs_has_crc(mp))
1175         geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1176     if (xfs_has_ftype(mp))
1177         geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1178     if (xfs_has_finobt(mp))
1179         geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1180     if (xfs_has_sparseinodes(mp))
1181         geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1182     if (xfs_has_rmapbt(mp))
1183         geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1184     if (xfs_has_reflink(mp))
1185         geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1186     if (xfs_has_bigtime(mp))
1187         geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
1188     if (xfs_has_inobtcounts(mp))
1189         geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
1190     if (xfs_has_sector(mp)) {
1191         geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1192         geo->logsectsize = sbp->sb_logsectsize;
1193     } else {
1194         geo->logsectsize = BBSIZE;
1195     }
1196     if (xfs_has_large_extent_counts(mp))
1197         geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
1198     geo->rtsectsize = sbp->sb_blocksize;
1199     geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1200 
1201     if (struct_version < 4)
1202         return;
1203 
1204     if (xfs_has_logv2(mp))
1205         geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1206 
1207     geo->logsunit = sbp->sb_logsunit;
1208 
1209     if (struct_version < 5)
1210         return;
1211 
1212     geo->version = XFS_FSOP_GEOM_VERSION_V5;
1213 }
1214 
1215 /* Read a secondary superblock. */
1216 int
1217 xfs_sb_read_secondary(
1218     struct xfs_mount    *mp,
1219     struct xfs_trans    *tp,
1220     xfs_agnumber_t      agno,
1221     struct xfs_buf      **bpp)
1222 {
1223     struct xfs_buf      *bp;
1224     int         error;
1225 
1226     ASSERT(agno != 0 && agno != NULLAGNUMBER);
1227     error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1228             XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1229             XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1230     if (error)
1231         return error;
1232     xfs_buf_set_ref(bp, XFS_SSB_REF);
1233     *bpp = bp;
1234     return 0;
1235 }
1236 
1237 /* Get an uninitialised secondary superblock buffer. */
1238 int
1239 xfs_sb_get_secondary(
1240     struct xfs_mount    *mp,
1241     struct xfs_trans    *tp,
1242     xfs_agnumber_t      agno,
1243     struct xfs_buf      **bpp)
1244 {
1245     struct xfs_buf      *bp;
1246     int         error;
1247 
1248     ASSERT(agno != 0 && agno != NULLAGNUMBER);
1249     error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1250             XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1251             XFS_FSS_TO_BB(mp, 1), 0, &bp);
1252     if (error)
1253         return error;
1254     bp->b_ops = &xfs_sb_buf_ops;
1255     xfs_buf_oneshot(bp);
1256     *bpp = bp;
1257     return 0;
1258 }
1259 
1260 /*
1261  * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
1262  * so users won't be confused by values in error messages.
1263  */
1264 bool
1265 xfs_validate_stripe_geometry(
1266     struct xfs_mount    *mp,
1267     __s64           sunit,
1268     __s64           swidth,
1269     int         sectorsize,
1270     bool            silent)
1271 {
1272     if (swidth > INT_MAX) {
1273         if (!silent)
1274             xfs_notice(mp,
1275 "stripe width (%lld) is too large", swidth);
1276         return false;
1277     }
1278 
1279     if (sunit > swidth) {
1280         if (!silent)
1281             xfs_notice(mp,
1282 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1283         return false;
1284     }
1285 
1286     if (sectorsize && (int)sunit % sectorsize) {
1287         if (!silent)
1288             xfs_notice(mp,
1289 "stripe unit (%lld) must be a multiple of the sector size (%d)",
1290                    sunit, sectorsize);
1291         return false;
1292     }
1293 
1294     if (sunit && !swidth) {
1295         if (!silent)
1296             xfs_notice(mp,
1297 "invalid stripe unit (%lld) and stripe width of 0", sunit);
1298         return false;
1299     }
1300 
1301     if (!sunit && swidth) {
1302         if (!silent)
1303             xfs_notice(mp,
1304 "invalid stripe width (%lld) and stripe unit of 0", swidth);
1305         return false;
1306     }
1307 
1308     if (sunit && (int)swidth % (int)sunit) {
1309         if (!silent)
1310             xfs_notice(mp,
1311 "stripe width (%lld) must be a multiple of the stripe unit (%lld)",
1312                    swidth, sunit);
1313         return false;
1314     }
1315     return true;
1316 }