linux/fs/xfs/libxfs/xfs_sb.c
<<
>>
Prefs
   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_defer.h"
  16#include "xfs_inode.h"
  17#include "xfs_ialloc.h"
  18#include "xfs_alloc.h"
  19#include "xfs_error.h"
  20#include "xfs_trace.h"
  21#include "xfs_cksum.h"
  22#include "xfs_trans.h"
  23#include "xfs_buf_item.h"
  24#include "xfs_bmap_btree.h"
  25#include "xfs_alloc_btree.h"
  26#include "xfs_ialloc_btree.h"
  27#include "xfs_log.h"
  28#include "xfs_rmap_btree.h"
  29#include "xfs_bmap.h"
  30#include "xfs_refcount_btree.h"
  31#include "xfs_da_format.h"
  32#include "xfs_da_btree.h"
  33
  34/*
  35 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
  36 */
  37
  38/*
  39 * Reference counting access wrappers to the perag structures.
  40 * Because we never free per-ag structures, the only thing we
  41 * have to protect against changes is the tree structure itself.
  42 */
  43struct xfs_perag *
  44xfs_perag_get(
  45        struct xfs_mount        *mp,
  46        xfs_agnumber_t          agno)
  47{
  48        struct xfs_perag        *pag;
  49        int                     ref = 0;
  50
  51        rcu_read_lock();
  52        pag = radix_tree_lookup(&mp->m_perag_tree, agno);
  53        if (pag) {
  54                ASSERT(atomic_read(&pag->pag_ref) >= 0);
  55                ref = atomic_inc_return(&pag->pag_ref);
  56        }
  57        rcu_read_unlock();
  58        trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
  59        return pag;
  60}
  61
  62/*
  63 * search from @first to find the next perag with the given tag set.
  64 */
  65struct xfs_perag *
  66xfs_perag_get_tag(
  67        struct xfs_mount        *mp,
  68        xfs_agnumber_t          first,
  69        int                     tag)
  70{
  71        struct xfs_perag        *pag;
  72        int                     found;
  73        int                     ref;
  74
  75        rcu_read_lock();
  76        found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
  77                                        (void **)&pag, first, 1, tag);
  78        if (found <= 0) {
  79                rcu_read_unlock();
  80                return NULL;
  81        }
  82        ref = atomic_inc_return(&pag->pag_ref);
  83        rcu_read_unlock();
  84        trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
  85        return pag;
  86}
  87
  88void
  89xfs_perag_put(
  90        struct xfs_perag        *pag)
  91{
  92        int     ref;
  93
  94        ASSERT(atomic_read(&pag->pag_ref) > 0);
  95        ref = atomic_dec_return(&pag->pag_ref);
  96        trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
  97}
  98
  99/* Check all the superblock fields we care about when reading one in. */
 100STATIC int
 101xfs_validate_sb_read(
 102        struct xfs_mount        *mp,
 103        struct xfs_sb           *sbp)
 104{
 105        if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
 106                return 0;
 107
 108        /*
 109         * Version 5 superblock feature mask validation. Reject combinations
 110         * the kernel cannot support up front before checking anything else.
 111         */
 112        if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
 113                xfs_warn(mp,
 114"Superblock has unknown compatible features (0x%x) enabled.",
 115                        (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
 116                xfs_warn(mp,
 117"Using a more recent kernel is recommended.");
 118        }
 119
 120        if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
 121                xfs_alert(mp,
 122"Superblock has unknown read-only compatible features (0x%x) enabled.",
 123                        (sbp->sb_features_ro_compat &
 124                                        XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
 125                if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
 126                        xfs_warn(mp,
 127"Attempted to mount read-only compatible filesystem read-write.");
 128                        xfs_warn(mp,
 129"Filesystem can only be safely mounted read only.");
 130
 131                        return -EINVAL;
 132                }
 133        }
 134        if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
 135                xfs_warn(mp,
 136"Superblock has unknown incompatible features (0x%x) enabled.",
 137                        (sbp->sb_features_incompat &
 138                                        XFS_SB_FEAT_INCOMPAT_UNKNOWN));
 139                xfs_warn(mp,
 140"Filesystem cannot be safely mounted by this kernel.");
 141                return -EINVAL;
 142        }
 143
 144        return 0;
 145}
 146
 147/* Check all the superblock fields we care about when writing one out. */
 148STATIC int
 149xfs_validate_sb_write(
 150        struct xfs_mount        *mp,
 151        struct xfs_buf          *bp,
 152        struct xfs_sb           *sbp)
 153{
 154        /*
 155         * Carry out additional sb summary counter sanity checks when we write
 156         * the superblock.  We skip this in the read validator because there
 157         * could be newer superblocks in the log and if the values are garbage
 158         * even after replay we'll recalculate them at the end of log mount.
 159         *
 160         * mkfs has traditionally written zeroed counters to inprogress and
 161         * secondary superblocks, so allow this usage to continue because
 162         * we never read counters from such superblocks.
 163         */
 164        if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
 165            (sbp->sb_fdblocks > sbp->sb_dblocks ||
 166             !xfs_verify_icount(mp, sbp->sb_icount) ||
 167             sbp->sb_ifree > sbp->sb_icount)) {
 168                xfs_warn(mp, "SB summary counter sanity check failed");
 169                return -EFSCORRUPTED;
 170        }
 171
 172        if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
 173                return 0;
 174
 175        /*
 176         * Version 5 superblock feature mask validation. Reject combinations
 177         * the kernel cannot support since we checked for unsupported bits in
 178         * the read verifier, which means that memory is corrupt.
 179         */
 180        if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
 181                xfs_warn(mp,
 182"Corruption detected in superblock compatible features (0x%x)!",
 183                        (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
 184                return -EFSCORRUPTED;
 185        }
 186
 187        if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
 188                xfs_alert(mp,
 189"Corruption detected in superblock read-only compatible features (0x%x)!",
 190                        (sbp->sb_features_ro_compat &
 191                                        XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
 192                return -EFSCORRUPTED;
 193        }
 194        if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
 195                xfs_warn(mp,
 196"Corruption detected in superblock incompatible features (0x%x)!",
 197                        (sbp->sb_features_incompat &
 198                                        XFS_SB_FEAT_INCOMPAT_UNKNOWN));
 199                return -EFSCORRUPTED;
 200        }
 201        if (xfs_sb_has_incompat_log_feature(sbp,
 202                        XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
 203                xfs_warn(mp,
 204"Corruption detected in superblock incompatible log features (0x%x)!",
 205                        (sbp->sb_features_log_incompat &
 206                                        XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
 207                return -EFSCORRUPTED;
 208        }
 209
 210        /*
 211         * We can't read verify the sb LSN because the read verifier is called
 212         * before the log is allocated and processed. We know the log is set up
 213         * before write verifier calls, so check it here.
 214         */
 215        if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
 216                return -EFSCORRUPTED;
 217
 218        return 0;
 219}
 220
 221/* Check the validity of the SB. */
 222STATIC int
 223xfs_validate_sb_common(
 224        struct xfs_mount        *mp,
 225        struct xfs_buf          *bp,
 226        struct xfs_sb           *sbp)
 227{
 228        struct xfs_dsb          *dsb = XFS_BUF_TO_SBP(bp);
 229        uint32_t                agcount = 0;
 230        uint32_t                rem;
 231
 232        if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
 233                xfs_warn(mp, "bad magic number");
 234                return -EWRONGFS;
 235        }
 236
 237        if (!xfs_sb_good_version(sbp)) {
 238                xfs_warn(mp, "bad version");
 239                return -EWRONGFS;
 240        }
 241
 242        if (xfs_sb_version_has_pquotino(sbp)) {
 243                if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
 244                        xfs_notice(mp,
 245                           "Version 5 of Super block has XFS_OQUOTA bits.");
 246                        return -EFSCORRUPTED;
 247                }
 248        } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
 249                                XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
 250                        xfs_notice(mp,
 251"Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
 252                        return -EFSCORRUPTED;
 253        }
 254
 255        /*
 256         * Full inode chunks must be aligned to inode chunk size when
 257         * sparse inodes are enabled to support the sparse chunk
 258         * allocation algorithm and prevent overlapping inode records.
 259         */
 260        if (xfs_sb_version_hassparseinodes(sbp)) {
 261                uint32_t        align;
 262
 263                align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
 264                                >> sbp->sb_blocklog;
 265                if (sbp->sb_inoalignmt != align) {
 266                        xfs_warn(mp,
 267"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
 268                                 sbp->sb_inoalignmt, align);
 269                        return -EINVAL;
 270                }
 271        }
 272
 273        if (unlikely(
 274            sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
 275                xfs_warn(mp,
 276                "filesystem is marked as having an external log; "
 277                "specify logdev on the mount command line.");
 278                return -EINVAL;
 279        }
 280
 281        if (unlikely(
 282            sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
 283                xfs_warn(mp,
 284                "filesystem is marked as having an internal log; "
 285                "do not specify logdev on the mount command line.");
 286                return -EINVAL;
 287        }
 288
 289        /* Compute agcount for this number of dblocks and agblocks */
 290        if (sbp->sb_agblocks) {
 291                agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
 292                if (rem)
 293                        agcount++;
 294        }
 295
 296        /*
 297         * More sanity checking.  Most of these were stolen directly from
 298         * xfs_repair.
 299         */
 300        if (unlikely(
 301            sbp->sb_agcount <= 0                                        ||
 302            sbp->sb_sectsize < XFS_MIN_SECTORSIZE                       ||
 303            sbp->sb_sectsize > XFS_MAX_SECTORSIZE                       ||
 304            sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG                    ||
 305            sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG                    ||
 306            sbp->sb_sectsize != (1 << sbp->sb_sectlog)                  ||
 307            sbp->sb_blocksize < XFS_MIN_BLOCKSIZE                       ||
 308            sbp->sb_blocksize > XFS_MAX_BLOCKSIZE                       ||
 309            sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG                    ||
 310            sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG                    ||
 311            sbp->sb_blocksize != (1 << sbp->sb_blocklog)                ||
 312            sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
 313            sbp->sb_inodesize < XFS_DINODE_MIN_SIZE                     ||
 314            sbp->sb_inodesize > XFS_DINODE_MAX_SIZE                     ||
 315            sbp->sb_inodelog < XFS_DINODE_MIN_LOG                       ||
 316            sbp->sb_inodelog > XFS_DINODE_MAX_LOG                       ||
 317            sbp->sb_inodesize != (1 << sbp->sb_inodelog)                ||
 318            sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE                    ||
 319            sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
 320            XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES       ||
 321            XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES       ||
 322            sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
 323            agcount == 0 || agcount != sbp->sb_agcount                  ||
 324            (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)   ||
 325            (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)  ||
 326            (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)  ||
 327            (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)    ||
 328            sbp->sb_dblocks == 0                                        ||
 329            sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)                      ||
 330            sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp)                      ||
 331            sbp->sb_shared_vn != 0)) {
 332                xfs_notice(mp, "SB sanity check failed");
 333                return -EFSCORRUPTED;
 334        }
 335
 336        if (sbp->sb_unit) {
 337                if (!xfs_sb_version_hasdalign(sbp) ||
 338                    sbp->sb_unit > sbp->sb_width ||
 339                    (sbp->sb_width % sbp->sb_unit) != 0) {
 340                        xfs_notice(mp, "SB stripe unit sanity check failed");
 341                        return -EFSCORRUPTED;
 342                }
 343        } else if (xfs_sb_version_hasdalign(sbp)) {
 344                xfs_notice(mp, "SB stripe alignment sanity check failed");
 345                return -EFSCORRUPTED;
 346        } else if (sbp->sb_width) {
 347                xfs_notice(mp, "SB stripe width sanity check failed");
 348                return -EFSCORRUPTED;
 349        }
 350
 351
 352        if (xfs_sb_version_hascrc(&mp->m_sb) &&
 353            sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
 354                xfs_notice(mp, "v5 SB sanity check failed");
 355                return -EFSCORRUPTED;
 356        }
 357
 358        /*
 359         * Until this is fixed only page-sized or smaller data blocks work.
 360         */
 361        if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
 362                xfs_warn(mp,
 363                "File system with blocksize %d bytes. "
 364                "Only pagesize (%ld) or less will currently work.",
 365                                sbp->sb_blocksize, PAGE_SIZE);
 366                return -ENOSYS;
 367        }
 368
 369        /*
 370         * Currently only very few inode sizes are supported.
 371         */
 372        switch (sbp->sb_inodesize) {
 373        case 256:
 374        case 512:
 375        case 1024:
 376        case 2048:
 377                break;
 378        default:
 379                xfs_warn(mp, "inode size of %d bytes not supported",
 380                                sbp->sb_inodesize);
 381                return -ENOSYS;
 382        }
 383
 384        if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
 385            xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
 386                xfs_warn(mp,
 387                "file system too large to be mounted on this system.");
 388                return -EFBIG;
 389        }
 390
 391        /*
 392         * Don't touch the filesystem if a user tool thinks it owns the primary
 393         * superblock.  mkfs doesn't clear the flag from secondary supers, so
 394         * we don't check them at all.
 395         */
 396        if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && sbp->sb_inprogress) {
 397                xfs_warn(mp, "Offline file system operation in progress!");
 398                return -EFSCORRUPTED;
 399        }
 400        return 0;
 401}
 402
 403void
 404xfs_sb_quota_from_disk(struct xfs_sb *sbp)
 405{
 406        /*
 407         * older mkfs doesn't initialize quota inodes to NULLFSINO. This
 408         * leads to in-core values having two different values for a quota
 409         * inode to be invalid: 0 and NULLFSINO. Change it to a single value
 410         * NULLFSINO.
 411         *
 412         * Note that this change affect only the in-core values. These
 413         * values are not written back to disk unless any quota information
 414         * is written to the disk. Even in that case, sb_pquotino field is
 415         * not written to disk unless the superblock supports pquotino.
 416         */
 417        if (sbp->sb_uquotino == 0)
 418                sbp->sb_uquotino = NULLFSINO;
 419        if (sbp->sb_gquotino == 0)
 420                sbp->sb_gquotino = NULLFSINO;
 421        if (sbp->sb_pquotino == 0)
 422                sbp->sb_pquotino = NULLFSINO;
 423
 424        /*
 425         * We need to do these manipilations only if we are working
 426         * with an older version of on-disk superblock.
 427         */
 428        if (xfs_sb_version_has_pquotino(sbp))
 429                return;
 430
 431        if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
 432                sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
 433                                        XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
 434        if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
 435                sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
 436                                        XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
 437        sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
 438
 439        if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
 440            sbp->sb_gquotino != NULLFSINO)  {
 441                /*
 442                 * In older version of superblock, on-disk superblock only
 443                 * has sb_gquotino, and in-core superblock has both sb_gquotino
 444                 * and sb_pquotino. But, only one of them is supported at any
 445                 * point of time. So, if PQUOTA is set in disk superblock,
 446                 * copy over sb_gquotino to sb_pquotino.  The NULLFSINO test
 447                 * above is to make sure we don't do this twice and wipe them
 448                 * both out!
 449                 */
 450                sbp->sb_pquotino = sbp->sb_gquotino;
 451                sbp->sb_gquotino = NULLFSINO;
 452        }
 453}
 454
 455static void
 456__xfs_sb_from_disk(
 457        struct xfs_sb   *to,
 458        xfs_dsb_t       *from,
 459        bool            convert_xquota)
 460{
 461        to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
 462        to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
 463        to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
 464        to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
 465        to->sb_rextents = be64_to_cpu(from->sb_rextents);
 466        memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
 467        to->sb_logstart = be64_to_cpu(from->sb_logstart);
 468        to->sb_rootino = be64_to_cpu(from->sb_rootino);
 469        to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
 470        to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
 471        to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
 472        to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
 473        to->sb_agcount = be32_to_cpu(from->sb_agcount);
 474        to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
 475        to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
 476        to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
 477        to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
 478        to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
 479        to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
 480        memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
 481        to->sb_blocklog = from->sb_blocklog;
 482        to->sb_sectlog = from->sb_sectlog;
 483        to->sb_inodelog = from->sb_inodelog;
 484        to->sb_inopblog = from->sb_inopblog;
 485        to->sb_agblklog = from->sb_agblklog;
 486        to->sb_rextslog = from->sb_rextslog;
 487        to->sb_inprogress = from->sb_inprogress;
 488        to->sb_imax_pct = from->sb_imax_pct;
 489        to->sb_icount = be64_to_cpu(from->sb_icount);
 490        to->sb_ifree = be64_to_cpu(from->sb_ifree);
 491        to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
 492        to->sb_frextents = be64_to_cpu(from->sb_frextents);
 493        to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
 494        to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
 495        to->sb_qflags = be16_to_cpu(from->sb_qflags);
 496        to->sb_flags = from->sb_flags;
 497        to->sb_shared_vn = from->sb_shared_vn;
 498        to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
 499        to->sb_unit = be32_to_cpu(from->sb_unit);
 500        to->sb_width = be32_to_cpu(from->sb_width);
 501        to->sb_dirblklog = from->sb_dirblklog;
 502        to->sb_logsectlog = from->sb_logsectlog;
 503        to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
 504        to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
 505        to->sb_features2 = be32_to_cpu(from->sb_features2);
 506        to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
 507        to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
 508        to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
 509        to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
 510        to->sb_features_log_incompat =
 511                                be32_to_cpu(from->sb_features_log_incompat);
 512        /* crc is only used on disk, not in memory; just init to 0 here. */
 513        to->sb_crc = 0;
 514        to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
 515        to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
 516        to->sb_lsn = be64_to_cpu(from->sb_lsn);
 517        /*
 518         * sb_meta_uuid is only on disk if it differs from sb_uuid and the
 519         * feature flag is set; if not set we keep it only in memory.
 520         */
 521        if (xfs_sb_version_hasmetauuid(to))
 522                uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
 523        else
 524                uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
 525        /* Convert on-disk flags to in-memory flags? */
 526        if (convert_xquota)
 527                xfs_sb_quota_from_disk(to);
 528}
 529
 530void
 531xfs_sb_from_disk(
 532        struct xfs_sb   *to,
 533        xfs_dsb_t       *from)
 534{
 535        __xfs_sb_from_disk(to, from, true);
 536}
 537
 538static void
 539xfs_sb_quota_to_disk(
 540        struct xfs_dsb  *to,
 541        struct xfs_sb   *from)
 542{
 543        uint16_t        qflags = from->sb_qflags;
 544
 545        to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
 546        if (xfs_sb_version_has_pquotino(from)) {
 547                to->sb_qflags = cpu_to_be16(from->sb_qflags);
 548                to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
 549                to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
 550                return;
 551        }
 552
 553        /*
 554         * The in-core version of sb_qflags do not have XFS_OQUOTA_*
 555         * flags, whereas the on-disk version does.  So, convert incore
 556         * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
 557         */
 558        qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
 559                        XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
 560
 561        if (from->sb_qflags &
 562                        (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
 563                qflags |= XFS_OQUOTA_ENFD;
 564        if (from->sb_qflags &
 565                        (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
 566                qflags |= XFS_OQUOTA_CHKD;
 567        to->sb_qflags = cpu_to_be16(qflags);
 568
 569        /*
 570         * GQUOTINO and PQUOTINO cannot be used together in versions
 571         * of superblock that do not have pquotino. from->sb_flags
 572         * tells us which quota is active and should be copied to
 573         * disk. If neither are active, we should NULL the inode.
 574         *
 575         * In all cases, the separate pquotino must remain 0 because it
 576         * it beyond the "end" of the valid non-pquotino superblock.
 577         */
 578        if (from->sb_qflags & XFS_GQUOTA_ACCT)
 579                to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
 580        else if (from->sb_qflags & XFS_PQUOTA_ACCT)
 581                to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
 582        else {
 583                /*
 584                 * We can't rely on just the fields being logged to tell us
 585                 * that it is safe to write NULLFSINO - we should only do that
 586                 * if quotas are not actually enabled. Hence only write
 587                 * NULLFSINO if both in-core quota inodes are NULL.
 588                 */
 589                if (from->sb_gquotino == NULLFSINO &&
 590                    from->sb_pquotino == NULLFSINO)
 591                        to->sb_gquotino = cpu_to_be64(NULLFSINO);
 592        }
 593
 594        to->sb_pquotino = 0;
 595}
 596
 597void
 598xfs_sb_to_disk(
 599        struct xfs_dsb  *to,
 600        struct xfs_sb   *from)
 601{
 602        xfs_sb_quota_to_disk(to, from);
 603
 604        to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
 605        to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
 606        to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
 607        to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
 608        to->sb_rextents = cpu_to_be64(from->sb_rextents);
 609        memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
 610        to->sb_logstart = cpu_to_be64(from->sb_logstart);
 611        to->sb_rootino = cpu_to_be64(from->sb_rootino);
 612        to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
 613        to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
 614        to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
 615        to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
 616        to->sb_agcount = cpu_to_be32(from->sb_agcount);
 617        to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
 618        to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
 619        to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
 620        to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
 621        to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
 622        to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
 623        memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
 624        to->sb_blocklog = from->sb_blocklog;
 625        to->sb_sectlog = from->sb_sectlog;
 626        to->sb_inodelog = from->sb_inodelog;
 627        to->sb_inopblog = from->sb_inopblog;
 628        to->sb_agblklog = from->sb_agblklog;
 629        to->sb_rextslog = from->sb_rextslog;
 630        to->sb_inprogress = from->sb_inprogress;
 631        to->sb_imax_pct = from->sb_imax_pct;
 632        to->sb_icount = cpu_to_be64(from->sb_icount);
 633        to->sb_ifree = cpu_to_be64(from->sb_ifree);
 634        to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
 635        to->sb_frextents = cpu_to_be64(from->sb_frextents);
 636
 637        to->sb_flags = from->sb_flags;
 638        to->sb_shared_vn = from->sb_shared_vn;
 639        to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
 640        to->sb_unit = cpu_to_be32(from->sb_unit);
 641        to->sb_width = cpu_to_be32(from->sb_width);
 642        to->sb_dirblklog = from->sb_dirblklog;
 643        to->sb_logsectlog = from->sb_logsectlog;
 644        to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
 645        to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
 646
 647        /*
 648         * We need to ensure that bad_features2 always matches features2.
 649         * Hence we enforce that here rather than having to remember to do it
 650         * everywhere else that updates features2.
 651         */
 652        from->sb_bad_features2 = from->sb_features2;
 653        to->sb_features2 = cpu_to_be32(from->sb_features2);
 654        to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
 655
 656        if (xfs_sb_version_hascrc(from)) {
 657                to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
 658                to->sb_features_ro_compat =
 659                                cpu_to_be32(from->sb_features_ro_compat);
 660                to->sb_features_incompat =
 661                                cpu_to_be32(from->sb_features_incompat);
 662                to->sb_features_log_incompat =
 663                                cpu_to_be32(from->sb_features_log_incompat);
 664                to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
 665                to->sb_lsn = cpu_to_be64(from->sb_lsn);
 666                if (xfs_sb_version_hasmetauuid(from))
 667                        uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
 668        }
 669}
 670
 671/*
 672 * If the superblock has the CRC feature bit set or the CRC field is non-null,
 673 * check that the CRC is valid.  We check the CRC field is non-null because a
 674 * single bit error could clear the feature bit and unused parts of the
 675 * superblock are supposed to be zero. Hence a non-null crc field indicates that
 676 * we've potentially lost a feature bit and we should check it anyway.
 677 *
 678 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
 679 * last field in V4 secondary superblocks.  So for secondary superblocks,
 680 * we are more forgiving, and ignore CRC failures if the primary doesn't
 681 * indicate that the fs version is V5.
 682 */
 683static void
 684xfs_sb_read_verify(
 685        struct xfs_buf          *bp)
 686{
 687        struct xfs_sb           sb;
 688        struct xfs_mount        *mp = bp->b_target->bt_mount;
 689        struct xfs_dsb          *dsb = XFS_BUF_TO_SBP(bp);
 690        int                     error;
 691
 692        /*
 693         * open code the version check to avoid needing to convert the entire
 694         * superblock from disk order just to check the version number
 695         */
 696        if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
 697            (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
 698                                                XFS_SB_VERSION_5) ||
 699             dsb->sb_crc != 0)) {
 700
 701                if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
 702                        /* Only fail bad secondaries on a known V5 filesystem */
 703                        if (bp->b_bn == XFS_SB_DADDR ||
 704                            xfs_sb_version_hascrc(&mp->m_sb)) {
 705                                error = -EFSBADCRC;
 706                                goto out_error;
 707                        }
 708                }
 709        }
 710
 711        /*
 712         * Check all the superblock fields.  Don't byteswap the xquota flags
 713         * because _verify_common checks the on-disk values.
 714         */
 715        __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
 716        error = xfs_validate_sb_common(mp, bp, &sb);
 717        if (error)
 718                goto out_error;
 719        error = xfs_validate_sb_read(mp, &sb);
 720
 721out_error:
 722        if (error == -EFSCORRUPTED || error == -EFSBADCRC)
 723                xfs_verifier_error(bp, error, __this_address);
 724        else if (error)
 725                xfs_buf_ioerror(bp, error);
 726}
 727
 728/*
 729 * We may be probed for a filesystem match, so we may not want to emit
 730 * messages when the superblock buffer is not actually an XFS superblock.
 731 * If we find an XFS superblock, then run a normal, noisy mount because we are
 732 * really going to mount it and want to know about errors.
 733 */
 734static void
 735xfs_sb_quiet_read_verify(
 736        struct xfs_buf  *bp)
 737{
 738        struct xfs_dsb  *dsb = XFS_BUF_TO_SBP(bp);
 739
 740        if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
 741                /* XFS filesystem, verify noisily! */
 742                xfs_sb_read_verify(bp);
 743                return;
 744        }
 745        /* quietly fail */
 746        xfs_buf_ioerror(bp, -EWRONGFS);
 747}
 748
 749static void
 750xfs_sb_write_verify(
 751        struct xfs_buf          *bp)
 752{
 753        struct xfs_sb           sb;
 754        struct xfs_mount        *mp = bp->b_target->bt_mount;
 755        struct xfs_buf_log_item *bip = bp->b_log_item;
 756        int                     error;
 757
 758        /*
 759         * Check all the superblock fields.  Don't byteswap the xquota flags
 760         * because _verify_common checks the on-disk values.
 761         */
 762        __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
 763        error = xfs_validate_sb_common(mp, bp, &sb);
 764        if (error)
 765                goto out_error;
 766        error = xfs_validate_sb_write(mp, bp, &sb);
 767        if (error)
 768                goto out_error;
 769
 770        if (!xfs_sb_version_hascrc(&mp->m_sb))
 771                return;
 772
 773        if (bip)
 774                XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
 775
 776        xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
 777        return;
 778
 779out_error:
 780        xfs_verifier_error(bp, error, __this_address);
 781}
 782
 783const struct xfs_buf_ops xfs_sb_buf_ops = {
 784        .name = "xfs_sb",
 785        .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
 786        .verify_read = xfs_sb_read_verify,
 787        .verify_write = xfs_sb_write_verify,
 788};
 789
 790const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
 791        .name = "xfs_sb_quiet",
 792        .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
 793        .verify_read = xfs_sb_quiet_read_verify,
 794        .verify_write = xfs_sb_write_verify,
 795};
 796
 797/*
 798 * xfs_mount_common
 799 *
 800 * Mount initialization code establishing various mount
 801 * fields from the superblock associated with the given
 802 * mount structure
 803 */
 804void
 805xfs_sb_mount_common(
 806        struct xfs_mount *mp,
 807        struct xfs_sb   *sbp)
 808{
 809        mp->m_agfrotor = mp->m_agirotor = 0;
 810        mp->m_maxagi = mp->m_sb.sb_agcount;
 811        mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
 812        mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
 813        mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
 814        mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
 815        mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
 816        mp->m_blockmask = sbp->sb_blocksize - 1;
 817        mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
 818        mp->m_blockwmask = mp->m_blockwsize - 1;
 819
 820        mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
 821        mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
 822        mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
 823        mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
 824
 825        mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
 826        mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
 827        mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
 828        mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
 829
 830        mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
 831        mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
 832        mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
 833        mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
 834
 835        mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
 836        mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
 837        mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
 838        mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
 839
 840        mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
 841        mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
 842        mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
 843        mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
 844
 845        mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
 846        mp->m_ialloc_inos = max_t(uint16_t, XFS_INODES_PER_CHUNK,
 847                                        sbp->sb_inopblock);
 848        mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
 849
 850        if (sbp->sb_spino_align)
 851                mp->m_ialloc_min_blks = sbp->sb_spino_align;
 852        else
 853                mp->m_ialloc_min_blks = mp->m_ialloc_blks;
 854        mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
 855        mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
 856}
 857
 858/*
 859 * xfs_initialize_perag_data
 860 *
 861 * Read in each per-ag structure so we can count up the number of
 862 * allocated inodes, free inodes and used filesystem blocks as this
 863 * information is no longer persistent in the superblock. Once we have
 864 * this information, write it into the in-core superblock structure.
 865 */
 866int
 867xfs_initialize_perag_data(
 868        struct xfs_mount *mp,
 869        xfs_agnumber_t  agcount)
 870{
 871        xfs_agnumber_t  index;
 872        xfs_perag_t     *pag;
 873        xfs_sb_t        *sbp = &mp->m_sb;
 874        uint64_t        ifree = 0;
 875        uint64_t        ialloc = 0;
 876        uint64_t        bfree = 0;
 877        uint64_t        bfreelst = 0;
 878        uint64_t        btree = 0;
 879        uint64_t        fdblocks;
 880        int             error = 0;
 881
 882        for (index = 0; index < agcount; index++) {
 883                /*
 884                 * read the agf, then the agi. This gets us
 885                 * all the information we need and populates the
 886                 * per-ag structures for us.
 887                 */
 888                error = xfs_alloc_pagf_init(mp, NULL, index, 0);
 889                if (error)
 890                        return error;
 891
 892                error = xfs_ialloc_pagi_init(mp, NULL, index);
 893                if (error)
 894                        return error;
 895                pag = xfs_perag_get(mp, index);
 896                ifree += pag->pagi_freecount;
 897                ialloc += pag->pagi_count;
 898                bfree += pag->pagf_freeblks;
 899                bfreelst += pag->pagf_flcount;
 900                btree += pag->pagf_btreeblks;
 901                xfs_perag_put(pag);
 902        }
 903        fdblocks = bfree + bfreelst + btree;
 904
 905        /*
 906         * If the new summary counts are obviously incorrect, fail the
 907         * mount operation because that implies the AGFs are also corrupt.
 908         * Clear BAD_SUMMARY so that we don't unmount with a dirty log, which
 909         * will prevent xfs_repair from fixing anything.
 910         */
 911        if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
 912                xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
 913                error = -EFSCORRUPTED;
 914                goto out;
 915        }
 916
 917        /* Overwrite incore superblock counters with just-read data */
 918        spin_lock(&mp->m_sb_lock);
 919        sbp->sb_ifree = ifree;
 920        sbp->sb_icount = ialloc;
 921        sbp->sb_fdblocks = fdblocks;
 922        spin_unlock(&mp->m_sb_lock);
 923
 924        xfs_reinit_percpu_counters(mp);
 925out:
 926        mp->m_flags &= ~XFS_MOUNT_BAD_SUMMARY;
 927        return error;
 928}
 929
 930/*
 931 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
 932 * into the superblock buffer to be logged.  It does not provide the higher
 933 * level of locking that is needed to protect the in-core superblock from
 934 * concurrent access.
 935 */
 936void
 937xfs_log_sb(
 938        struct xfs_trans        *tp)
 939{
 940        struct xfs_mount        *mp = tp->t_mountp;
 941        struct xfs_buf          *bp = xfs_trans_getsb(tp, mp, 0);
 942
 943        mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
 944        mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
 945        mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
 946
 947        xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
 948        xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
 949        xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
 950}
 951
 952/*
 953 * xfs_sync_sb
 954 *
 955 * Sync the superblock to disk.
 956 *
 957 * Note that the caller is responsible for checking the frozen state of the
 958 * filesystem. This procedure uses the non-blocking transaction allocator and
 959 * thus will allow modifications to a frozen fs. This is required because this
 960 * code can be called during the process of freezing where use of the high-level
 961 * allocator would deadlock.
 962 */
 963int
 964xfs_sync_sb(
 965        struct xfs_mount        *mp,
 966        bool                    wait)
 967{
 968        struct xfs_trans        *tp;
 969        int                     error;
 970
 971        error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
 972                        XFS_TRANS_NO_WRITECOUNT, &tp);
 973        if (error)
 974                return error;
 975
 976        xfs_log_sb(tp);
 977        if (wait)
 978                xfs_trans_set_sync(tp);
 979        return xfs_trans_commit(tp);
 980}
 981
 982/*
 983 * Update all the secondary superblocks to match the new state of the primary.
 984 * Because we are completely overwriting all the existing fields in the
 985 * secondary superblock buffers, there is no need to read them in from disk.
 986 * Just get a new buffer, stamp it and write it.
 987 *
 988 * The sb buffers need to be cached here so that we serialise against other
 989 * operations that access the secondary superblocks, but we don't want to keep
 990 * them in memory once it is written so we mark it as a one-shot buffer.
 991 */
 992int
 993xfs_update_secondary_sbs(
 994        struct xfs_mount        *mp)
 995{
 996        xfs_agnumber_t          agno;
 997        int                     saved_error = 0;
 998        int                     error = 0;
 999        LIST_HEAD               (buffer_list);
1000
1001        /* update secondary superblocks. */
1002        for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
1003                struct xfs_buf          *bp;
1004
1005                bp = xfs_buf_get(mp->m_ddev_targp,
1006                                 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
1007                                 XFS_FSS_TO_BB(mp, 1), 0);
1008                /*
1009                 * If we get an error reading or writing alternate superblocks,
1010                 * continue.  xfs_repair chooses the "best" superblock based
1011                 * on most matches; if we break early, we'll leave more
1012                 * superblocks un-updated than updated, and xfs_repair may
1013                 * pick them over the properly-updated primary.
1014                 */
1015                if (!bp) {
1016                        xfs_warn(mp,
1017                "error allocating secondary superblock for ag %d",
1018                                agno);
1019                        if (!saved_error)
1020                                saved_error = -ENOMEM;
1021                        continue;
1022                }
1023
1024                bp->b_ops = &xfs_sb_buf_ops;
1025                xfs_buf_oneshot(bp);
1026                xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1027                xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
1028                xfs_buf_delwri_queue(bp, &buffer_list);
1029                xfs_buf_relse(bp);
1030
1031                /* don't hold too many buffers at once */
1032                if (agno % 16)
1033                        continue;
1034
1035                error = xfs_buf_delwri_submit(&buffer_list);
1036                if (error) {
1037                        xfs_warn(mp,
1038                "write error %d updating a secondary superblock near ag %d",
1039                                error, agno);
1040                        if (!saved_error)
1041                                saved_error = error;
1042                        continue;
1043                }
1044        }
1045        error = xfs_buf_delwri_submit(&buffer_list);
1046        if (error) {
1047                xfs_warn(mp,
1048                "write error %d updating a secondary superblock near ag %d",
1049                        error, agno);
1050        }
1051
1052        return saved_error ? saved_error : error;
1053}
1054
1055/*
1056 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1057 * also writes the superblock buffer to disk sector 0 immediately.
1058 */
1059int
1060xfs_sync_sb_buf(
1061        struct xfs_mount        *mp)
1062{
1063        struct xfs_trans        *tp;
1064        struct xfs_buf          *bp;
1065        int                     error;
1066
1067        error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1068        if (error)
1069                return error;
1070
1071        bp = xfs_trans_getsb(tp, mp, 0);
1072        xfs_log_sb(tp);
1073        xfs_trans_bhold(tp, bp);
1074        xfs_trans_set_sync(tp);
1075        error = xfs_trans_commit(tp);
1076        if (error)
1077                goto out;
1078        /*
1079         * write out the sb buffer to get the changes to disk
1080         */
1081        error = xfs_bwrite(bp);
1082out:
1083        xfs_buf_relse(bp);
1084        return error;
1085}
1086
1087int
1088xfs_fs_geometry(
1089        struct xfs_sb           *sbp,
1090        struct xfs_fsop_geom    *geo,
1091        int                     struct_version)
1092{
1093        memset(geo, 0, sizeof(struct xfs_fsop_geom));
1094
1095        geo->blocksize = sbp->sb_blocksize;
1096        geo->rtextsize = sbp->sb_rextsize;
1097        geo->agblocks = sbp->sb_agblocks;
1098        geo->agcount = sbp->sb_agcount;
1099        geo->logblocks = sbp->sb_logblocks;
1100        geo->sectsize = sbp->sb_sectsize;
1101        geo->inodesize = sbp->sb_inodesize;
1102        geo->imaxpct = sbp->sb_imax_pct;
1103        geo->datablocks = sbp->sb_dblocks;
1104        geo->rtblocks = sbp->sb_rblocks;
1105        geo->rtextents = sbp->sb_rextents;
1106        geo->logstart = sbp->sb_logstart;
1107        BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1108        memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1109
1110        if (struct_version < 2)
1111                return 0;
1112
1113        geo->sunit = sbp->sb_unit;
1114        geo->swidth = sbp->sb_width;
1115
1116        if (struct_version < 3)
1117                return 0;
1118
1119        geo->version = XFS_FSOP_GEOM_VERSION;
1120        geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1121                     XFS_FSOP_GEOM_FLAGS_DIRV2 |
1122                     XFS_FSOP_GEOM_FLAGS_EXTFLG;
1123        if (xfs_sb_version_hasattr(sbp))
1124                geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1125        if (xfs_sb_version_hasquota(sbp))
1126                geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1127        if (xfs_sb_version_hasalign(sbp))
1128                geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1129        if (xfs_sb_version_hasdalign(sbp))
1130                geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1131        if (xfs_sb_version_hassector(sbp))
1132                geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1133        if (xfs_sb_version_hasasciici(sbp))
1134                geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1135        if (xfs_sb_version_haslazysbcount(sbp))
1136                geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1137        if (xfs_sb_version_hasattr2(sbp))
1138                geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1139        if (xfs_sb_version_hasprojid32bit(sbp))
1140                geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1141        if (xfs_sb_version_hascrc(sbp))
1142                geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1143        if (xfs_sb_version_hasftype(sbp))
1144                geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1145        if (xfs_sb_version_hasfinobt(sbp))
1146                geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1147        if (xfs_sb_version_hassparseinodes(sbp))
1148                geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1149        if (xfs_sb_version_hasrmapbt(sbp))
1150                geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1151        if (xfs_sb_version_hasreflink(sbp))
1152                geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1153        if (xfs_sb_version_hassector(sbp))
1154                geo->logsectsize = sbp->sb_logsectsize;
1155        else
1156                geo->logsectsize = BBSIZE;
1157        geo->rtsectsize = sbp->sb_blocksize;
1158        geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1159
1160        if (struct_version < 4)
1161                return 0;
1162
1163        if (xfs_sb_version_haslogv2(sbp))
1164                geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1165
1166        geo->logsunit = sbp->sb_logsunit;
1167
1168        return 0;
1169}
1170
1171/* Read a secondary superblock. */
1172int
1173xfs_sb_read_secondary(
1174        struct xfs_mount        *mp,
1175        struct xfs_trans        *tp,
1176        xfs_agnumber_t          agno,
1177        struct xfs_buf          **bpp)
1178{
1179        struct xfs_buf          *bp;
1180        int                     error;
1181
1182        ASSERT(agno != 0 && agno != NULLAGNUMBER);
1183        error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1184                        XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1185                        XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1186        if (error)
1187                return error;
1188        xfs_buf_set_ref(bp, XFS_SSB_REF);
1189        *bpp = bp;
1190        return 0;
1191}
1192
1193/* Get an uninitialised secondary superblock buffer. */
1194int
1195xfs_sb_get_secondary(
1196        struct xfs_mount        *mp,
1197        struct xfs_trans        *tp,
1198        xfs_agnumber_t          agno,
1199        struct xfs_buf          **bpp)
1200{
1201        struct xfs_buf          *bp;
1202
1203        ASSERT(agno != 0 && agno != NULLAGNUMBER);
1204        bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1205                        XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1206                        XFS_FSS_TO_BB(mp, 1), 0);
1207        if (!bp)
1208                return -ENOMEM;
1209        bp->b_ops = &xfs_sb_buf_ops;
1210        xfs_buf_oneshot(bp);
1211        *bpp = bp;
1212        return 0;
1213}
1214