1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_ialloc.h"
16 #include "xfs_alloc.h"
17 #include "xfs_error.h"
18 #include "xfs_trace.h"
19 #include "xfs_trans.h"
20 #include "xfs_buf_item.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_alloc_btree.h"
24 #include "xfs_rmap_btree.h"
25 #include "xfs_refcount_btree.h"
26 #include "xfs_da_format.h"
27 #include "xfs_health.h"
30 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
34 * Reference counting access wrappers to the perag structures.
35 * Because we never free per-ag structures, the only thing we
36 * have to protect against changes is the tree structure itself.
43 struct xfs_perag *pag;
47 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
49 ASSERT(atomic_read(&pag->pag_ref) >= 0);
50 ref = atomic_inc_return(&pag->pag_ref);
53 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
58 * search from @first to find the next perag with the given tag set.
66 struct xfs_perag *pag;
71 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
72 (void **)&pag, first, 1, tag);
77 ref = atomic_inc_return(&pag->pag_ref);
79 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
85 struct xfs_perag *pag)
89 ASSERT(atomic_read(&pag->pag_ref) > 0);
90 ref = atomic_dec_return(&pag->pag_ref);
91 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
94 /* Check all the superblock fields we care about when reading one in. */
100 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
104 * Version 5 superblock feature mask validation. Reject combinations
105 * the kernel cannot support up front before checking anything else.
107 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
109 "Superblock has unknown compatible features (0x%x) enabled.",
110 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
112 "Using a more recent kernel is recommended.");
115 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
117 "Superblock has unknown read-only compatible features (0x%x) enabled.",
118 (sbp->sb_features_ro_compat &
119 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
120 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
122 "Attempted to mount read-only compatible filesystem read-write.");
124 "Filesystem can only be safely mounted read only.");
129 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
131 "Superblock has unknown incompatible features (0x%x) enabled.",
132 (sbp->sb_features_incompat &
133 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
135 "Filesystem cannot be safely mounted by this kernel.");
142 /* Check all the superblock fields we care about when writing one out. */
144 xfs_validate_sb_write(
145 struct xfs_mount *mp,
150 * Carry out additional sb summary counter sanity checks when we write
151 * the superblock. We skip this in the read validator because there
152 * could be newer superblocks in the log and if the values are garbage
153 * even after replay we'll recalculate them at the end of log mount.
155 * mkfs has traditionally written zeroed counters to inprogress and
156 * secondary superblocks, so allow this usage to continue because
157 * we never read counters from such superblocks.
159 if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
160 (sbp->sb_fdblocks > sbp->sb_dblocks ||
161 !xfs_verify_icount(mp, sbp->sb_icount) ||
162 sbp->sb_ifree > sbp->sb_icount)) {
163 xfs_warn(mp, "SB summary counter sanity check failed");
164 return -EFSCORRUPTED;
167 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
171 * Version 5 superblock feature mask validation. Reject combinations
172 * the kernel cannot support since we checked for unsupported bits in
173 * the read verifier, which means that memory is corrupt.
175 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
177 "Corruption detected in superblock compatible features (0x%x)!",
178 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
179 return -EFSCORRUPTED;
182 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
184 "Corruption detected in superblock read-only compatible features (0x%x)!",
185 (sbp->sb_features_ro_compat &
186 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
187 return -EFSCORRUPTED;
189 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
191 "Corruption detected in superblock incompatible features (0x%x)!",
192 (sbp->sb_features_incompat &
193 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
194 return -EFSCORRUPTED;
196 if (xfs_sb_has_incompat_log_feature(sbp,
197 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
199 "Corruption detected in superblock incompatible log features (0x%x)!",
200 (sbp->sb_features_log_incompat &
201 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
202 return -EFSCORRUPTED;
206 * We can't read verify the sb LSN because the read verifier is called
207 * before the log is allocated and processed. We know the log is set up
208 * before write verifier calls, so check it here.
210 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
211 return -EFSCORRUPTED;
216 /* Check the validity of the SB. */
218 xfs_validate_sb_common(
219 struct xfs_mount *mp,
223 struct xfs_dsb *dsb = bp->b_addr;
224 uint32_t agcount = 0;
227 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
228 xfs_warn(mp, "bad magic number");
232 if (!xfs_sb_good_version(sbp)) {
233 xfs_warn(mp, "bad version");
237 if (xfs_sb_version_has_pquotino(sbp)) {
238 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
240 "Version 5 of Super block has XFS_OQUOTA bits.");
241 return -EFSCORRUPTED;
243 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
244 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
246 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
247 return -EFSCORRUPTED;
251 * Full inode chunks must be aligned to inode chunk size when
252 * sparse inodes are enabled to support the sparse chunk
253 * allocation algorithm and prevent overlapping inode records.
255 if (xfs_sb_version_hassparseinodes(sbp)) {
258 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
260 if (sbp->sb_inoalignmt != align) {
262 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
263 sbp->sb_inoalignmt, align);
269 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
271 "filesystem is marked as having an external log; "
272 "specify logdev on the mount command line.");
277 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
279 "filesystem is marked as having an internal log; "
280 "do not specify logdev on the mount command line.");
284 /* Compute agcount for this number of dblocks and agblocks */
285 if (sbp->sb_agblocks) {
286 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
292 * More sanity checking. Most of these were stolen directly from
296 sbp->sb_agcount <= 0 ||
297 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
298 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
299 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
300 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
301 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
302 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
303 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
304 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
305 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
306 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
307 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
308 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
309 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
310 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
311 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
312 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
313 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
314 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
315 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
316 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
317 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
318 agcount == 0 || agcount != sbp->sb_agcount ||
319 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
320 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
321 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
322 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
323 sbp->sb_dblocks == 0 ||
324 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
325 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
326 sbp->sb_shared_vn != 0)) {
327 xfs_notice(mp, "SB sanity check failed");
328 return -EFSCORRUPTED;
331 /* Validate the realtime geometry; stolen from xfs_repair */
332 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
333 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
335 "realtime extent sanity check failed");
336 return -EFSCORRUPTED;
339 if (sbp->sb_rblocks == 0) {
340 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
341 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
343 "realtime zeroed geometry check failed");
344 return -EFSCORRUPTED;
350 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
351 rbmblocks = howmany_64(sbp->sb_rextents,
352 NBBY * sbp->sb_blocksize);
354 if (sbp->sb_rextents != rexts ||
355 sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
356 sbp->sb_rbmblocks != rbmblocks) {
358 "realtime geometry sanity check failed");
359 return -EFSCORRUPTED;
364 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
365 * would imply the image is corrupted.
367 if (!!sbp->sb_unit ^ xfs_sb_version_hasdalign(sbp)) {
368 xfs_notice(mp, "SB stripe alignment sanity check failed");
369 return -EFSCORRUPTED;
372 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
373 XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
374 return -EFSCORRUPTED;
376 if (xfs_sb_version_hascrc(&mp->m_sb) &&
377 sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
378 xfs_notice(mp, "v5 SB sanity check failed");
379 return -EFSCORRUPTED;
383 * Currently only very few inode sizes are supported.
385 switch (sbp->sb_inodesize) {
392 xfs_warn(mp, "inode size of %d bytes not supported",
401 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
404 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
405 * leads to in-core values having two different values for a quota
406 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
409 * Note that this change affect only the in-core values. These
410 * values are not written back to disk unless any quota information
411 * is written to the disk. Even in that case, sb_pquotino field is
412 * not written to disk unless the superblock supports pquotino.
414 if (sbp->sb_uquotino == 0)
415 sbp->sb_uquotino = NULLFSINO;
416 if (sbp->sb_gquotino == 0)
417 sbp->sb_gquotino = NULLFSINO;
418 if (sbp->sb_pquotino == 0)
419 sbp->sb_pquotino = NULLFSINO;
422 * We need to do these manipilations only if we are working
423 * with an older version of on-disk superblock.
425 if (xfs_sb_version_has_pquotino(sbp))
428 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
429 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
430 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
431 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
432 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
433 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
434 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
436 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
437 sbp->sb_gquotino != NULLFSINO) {
439 * In older version of superblock, on-disk superblock only
440 * has sb_gquotino, and in-core superblock has both sb_gquotino
441 * and sb_pquotino. But, only one of them is supported at any
442 * point of time. So, if PQUOTA is set in disk superblock,
443 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
444 * above is to make sure we don't do this twice and wipe them
447 sbp->sb_pquotino = sbp->sb_gquotino;
448 sbp->sb_gquotino = NULLFSINO;
458 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
459 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
460 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
461 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
462 to->sb_rextents = be64_to_cpu(from->sb_rextents);
463 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
464 to->sb_logstart = be64_to_cpu(from->sb_logstart);
465 to->sb_rootino = be64_to_cpu(from->sb_rootino);
466 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
467 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
468 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
469 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
470 to->sb_agcount = be32_to_cpu(from->sb_agcount);
471 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
472 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
473 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
474 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
475 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
476 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
477 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
478 to->sb_blocklog = from->sb_blocklog;
479 to->sb_sectlog = from->sb_sectlog;
480 to->sb_inodelog = from->sb_inodelog;
481 to->sb_inopblog = from->sb_inopblog;
482 to->sb_agblklog = from->sb_agblklog;
483 to->sb_rextslog = from->sb_rextslog;
484 to->sb_inprogress = from->sb_inprogress;
485 to->sb_imax_pct = from->sb_imax_pct;
486 to->sb_icount = be64_to_cpu(from->sb_icount);
487 to->sb_ifree = be64_to_cpu(from->sb_ifree);
488 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
489 to->sb_frextents = be64_to_cpu(from->sb_frextents);
490 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
491 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
492 to->sb_qflags = be16_to_cpu(from->sb_qflags);
493 to->sb_flags = from->sb_flags;
494 to->sb_shared_vn = from->sb_shared_vn;
495 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
496 to->sb_unit = be32_to_cpu(from->sb_unit);
497 to->sb_width = be32_to_cpu(from->sb_width);
498 to->sb_dirblklog = from->sb_dirblklog;
499 to->sb_logsectlog = from->sb_logsectlog;
500 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
501 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
502 to->sb_features2 = be32_to_cpu(from->sb_features2);
503 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
504 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
505 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
506 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
507 to->sb_features_log_incompat =
508 be32_to_cpu(from->sb_features_log_incompat);
509 /* crc is only used on disk, not in memory; just init to 0 here. */
511 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
512 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
513 to->sb_lsn = be64_to_cpu(from->sb_lsn);
515 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
516 * feature flag is set; if not set we keep it only in memory.
518 if (xfs_sb_version_hasmetauuid(to))
519 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
521 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
522 /* Convert on-disk flags to in-memory flags? */
524 xfs_sb_quota_from_disk(to);
532 __xfs_sb_from_disk(to, from, true);
536 xfs_sb_quota_to_disk(
540 uint16_t qflags = from->sb_qflags;
542 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
543 if (xfs_sb_version_has_pquotino(from)) {
544 to->sb_qflags = cpu_to_be16(from->sb_qflags);
545 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
546 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
551 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
552 * flags, whereas the on-disk version does. So, convert incore
553 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
555 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
556 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
558 if (from->sb_qflags &
559 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
560 qflags |= XFS_OQUOTA_ENFD;
561 if (from->sb_qflags &
562 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
563 qflags |= XFS_OQUOTA_CHKD;
564 to->sb_qflags = cpu_to_be16(qflags);
567 * GQUOTINO and PQUOTINO cannot be used together in versions
568 * of superblock that do not have pquotino. from->sb_flags
569 * tells us which quota is active and should be copied to
570 * disk. If neither are active, we should NULL the inode.
572 * In all cases, the separate pquotino must remain 0 because it
573 * is beyond the "end" of the valid non-pquotino superblock.
575 if (from->sb_qflags & XFS_GQUOTA_ACCT)
576 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
577 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
578 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
581 * We can't rely on just the fields being logged to tell us
582 * that it is safe to write NULLFSINO - we should only do that
583 * if quotas are not actually enabled. Hence only write
584 * NULLFSINO if both in-core quota inodes are NULL.
586 if (from->sb_gquotino == NULLFSINO &&
587 from->sb_pquotino == NULLFSINO)
588 to->sb_gquotino = cpu_to_be64(NULLFSINO);
599 xfs_sb_quota_to_disk(to, from);
601 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
602 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
603 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
604 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
605 to->sb_rextents = cpu_to_be64(from->sb_rextents);
606 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
607 to->sb_logstart = cpu_to_be64(from->sb_logstart);
608 to->sb_rootino = cpu_to_be64(from->sb_rootino);
609 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
610 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
611 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
612 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
613 to->sb_agcount = cpu_to_be32(from->sb_agcount);
614 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
615 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
616 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
617 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
618 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
619 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
620 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
621 to->sb_blocklog = from->sb_blocklog;
622 to->sb_sectlog = from->sb_sectlog;
623 to->sb_inodelog = from->sb_inodelog;
624 to->sb_inopblog = from->sb_inopblog;
625 to->sb_agblklog = from->sb_agblklog;
626 to->sb_rextslog = from->sb_rextslog;
627 to->sb_inprogress = from->sb_inprogress;
628 to->sb_imax_pct = from->sb_imax_pct;
629 to->sb_icount = cpu_to_be64(from->sb_icount);
630 to->sb_ifree = cpu_to_be64(from->sb_ifree);
631 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
632 to->sb_frextents = cpu_to_be64(from->sb_frextents);
634 to->sb_flags = from->sb_flags;
635 to->sb_shared_vn = from->sb_shared_vn;
636 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
637 to->sb_unit = cpu_to_be32(from->sb_unit);
638 to->sb_width = cpu_to_be32(from->sb_width);
639 to->sb_dirblklog = from->sb_dirblklog;
640 to->sb_logsectlog = from->sb_logsectlog;
641 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
642 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
645 * We need to ensure that bad_features2 always matches features2.
646 * Hence we enforce that here rather than having to remember to do it
647 * everywhere else that updates features2.
649 from->sb_bad_features2 = from->sb_features2;
650 to->sb_features2 = cpu_to_be32(from->sb_features2);
651 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
653 if (xfs_sb_version_hascrc(from)) {
654 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
655 to->sb_features_ro_compat =
656 cpu_to_be32(from->sb_features_ro_compat);
657 to->sb_features_incompat =
658 cpu_to_be32(from->sb_features_incompat);
659 to->sb_features_log_incompat =
660 cpu_to_be32(from->sb_features_log_incompat);
661 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
662 to->sb_lsn = cpu_to_be64(from->sb_lsn);
663 if (xfs_sb_version_hasmetauuid(from))
664 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
669 * If the superblock has the CRC feature bit set or the CRC field is non-null,
670 * check that the CRC is valid. We check the CRC field is non-null because a
671 * single bit error could clear the feature bit and unused parts of the
672 * superblock are supposed to be zero. Hence a non-null crc field indicates that
673 * we've potentially lost a feature bit and we should check it anyway.
675 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
676 * last field in V4 secondary superblocks. So for secondary superblocks,
677 * we are more forgiving, and ignore CRC failures if the primary doesn't
678 * indicate that the fs version is V5.
685 struct xfs_mount *mp = bp->b_mount;
686 struct xfs_dsb *dsb = bp->b_addr;
690 * open code the version check to avoid needing to convert the entire
691 * superblock from disk order just to check the version number
693 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
694 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
698 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
699 /* Only fail bad secondaries on a known V5 filesystem */
700 if (bp->b_bn == XFS_SB_DADDR ||
701 xfs_sb_version_hascrc(&mp->m_sb)) {
709 * Check all the superblock fields. Don't byteswap the xquota flags
710 * because _verify_common checks the on-disk values.
712 __xfs_sb_from_disk(&sb, dsb, false);
713 error = xfs_validate_sb_common(mp, bp, &sb);
716 error = xfs_validate_sb_read(mp, &sb);
719 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
720 xfs_verifier_error(bp, error, __this_address);
722 xfs_buf_ioerror(bp, error);
726 * We may be probed for a filesystem match, so we may not want to emit
727 * messages when the superblock buffer is not actually an XFS superblock.
728 * If we find an XFS superblock, then run a normal, noisy mount because we are
729 * really going to mount it and want to know about errors.
732 xfs_sb_quiet_read_verify(
735 struct xfs_dsb *dsb = bp->b_addr;
737 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
738 /* XFS filesystem, verify noisily! */
739 xfs_sb_read_verify(bp);
743 xfs_buf_ioerror(bp, -EWRONGFS);
751 struct xfs_mount *mp = bp->b_mount;
752 struct xfs_buf_log_item *bip = bp->b_log_item;
753 struct xfs_dsb *dsb = bp->b_addr;
757 * Check all the superblock fields. Don't byteswap the xquota flags
758 * because _verify_common checks the on-disk values.
760 __xfs_sb_from_disk(&sb, dsb, false);
761 error = xfs_validate_sb_common(mp, bp, &sb);
764 error = xfs_validate_sb_write(mp, bp, &sb);
768 if (!xfs_sb_version_hascrc(&mp->m_sb))
772 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
774 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
778 xfs_verifier_error(bp, error, __this_address);
781 const struct xfs_buf_ops xfs_sb_buf_ops = {
783 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
784 .verify_read = xfs_sb_read_verify,
785 .verify_write = xfs_sb_write_verify,
788 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
789 .name = "xfs_sb_quiet",
790 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
791 .verify_read = xfs_sb_quiet_read_verify,
792 .verify_write = xfs_sb_write_verify,
798 * Mount initialization code establishing various mount
799 * fields from the superblock associated with the given
802 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
806 struct xfs_mount *mp,
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_blockmask = sbp->sb_blocksize - 1;
816 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
817 mp->m_blockwmask = mp->m_blockwsize - 1;
819 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
820 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
821 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
822 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
824 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
825 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
826 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
827 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
829 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
830 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
831 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
832 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
834 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
835 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
836 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
837 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
839 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
840 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
841 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
845 * xfs_initialize_perag_data
847 * Read in each per-ag structure so we can count up the number of
848 * allocated inodes, free inodes and used filesystem blocks as this
849 * information is no longer persistent in the superblock. Once we have
850 * this information, write it into the in-core superblock structure.
853 xfs_initialize_perag_data(
854 struct xfs_mount *mp,
855 xfs_agnumber_t agcount)
857 xfs_agnumber_t index;
859 xfs_sb_t *sbp = &mp->m_sb;
863 uint64_t bfreelst = 0;
868 for (index = 0; index < agcount; index++) {
870 * read the agf, then the agi. This gets us
871 * all the information we need and populates the
872 * per-ag structures for us.
874 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
878 error = xfs_ialloc_pagi_init(mp, NULL, index);
881 pag = xfs_perag_get(mp, index);
882 ifree += pag->pagi_freecount;
883 ialloc += pag->pagi_count;
884 bfree += pag->pagf_freeblks;
885 bfreelst += pag->pagf_flcount;
886 btree += pag->pagf_btreeblks;
889 fdblocks = bfree + bfreelst + btree;
892 * If the new summary counts are obviously incorrect, fail the
893 * mount operation because that implies the AGFs are also corrupt.
894 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
895 * will prevent xfs_repair from fixing anything.
897 if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
898 xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
899 error = -EFSCORRUPTED;
903 /* Overwrite incore superblock counters with just-read data */
904 spin_lock(&mp->m_sb_lock);
905 sbp->sb_ifree = ifree;
906 sbp->sb_icount = ialloc;
907 sbp->sb_fdblocks = fdblocks;
908 spin_unlock(&mp->m_sb_lock);
910 xfs_reinit_percpu_counters(mp);
912 xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
917 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
918 * into the superblock buffer to be logged. It does not provide the higher
919 * level of locking that is needed to protect the in-core superblock from
924 struct xfs_trans *tp)
926 struct xfs_mount *mp = tp->t_mountp;
927 struct xfs_buf *bp = xfs_trans_getsb(tp);
929 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
930 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
931 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
933 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
934 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
935 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
941 * Sync the superblock to disk.
943 * Note that the caller is responsible for checking the frozen state of the
944 * filesystem. This procedure uses the non-blocking transaction allocator and
945 * thus will allow modifications to a frozen fs. This is required because this
946 * code can be called during the process of freezing where use of the high-level
947 * allocator would deadlock.
951 struct xfs_mount *mp,
954 struct xfs_trans *tp;
957 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
958 XFS_TRANS_NO_WRITECOUNT, &tp);
964 xfs_trans_set_sync(tp);
965 return xfs_trans_commit(tp);
969 * Update all the secondary superblocks to match the new state of the primary.
970 * Because we are completely overwriting all the existing fields in the
971 * secondary superblock buffers, there is no need to read them in from disk.
972 * Just get a new buffer, stamp it and write it.
974 * The sb buffers need to be cached here so that we serialise against other
975 * operations that access the secondary superblocks, but we don't want to keep
976 * them in memory once it is written so we mark it as a one-shot buffer.
979 xfs_update_secondary_sbs(
980 struct xfs_mount *mp)
985 LIST_HEAD (buffer_list);
987 /* update secondary superblocks. */
988 for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
991 error = xfs_buf_get(mp->m_ddev_targp,
992 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
993 XFS_FSS_TO_BB(mp, 1), &bp);
995 * If we get an error reading or writing alternate superblocks,
996 * continue. xfs_repair chooses the "best" superblock based
997 * on most matches; if we break early, we'll leave more
998 * superblocks un-updated than updated, and xfs_repair may
999 * pick them over the properly-updated primary.
1003 "error allocating secondary superblock for ag %d",
1006 saved_error = error;
1010 bp->b_ops = &xfs_sb_buf_ops;
1011 xfs_buf_oneshot(bp);
1012 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1013 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1014 xfs_buf_delwri_queue(bp, &buffer_list);
1017 /* don't hold too many buffers at once */
1021 error = xfs_buf_delwri_submit(&buffer_list);
1024 "write error %d updating a secondary superblock near ag %d",
1027 saved_error = error;
1031 error = xfs_buf_delwri_submit(&buffer_list);
1034 "write error %d updating a secondary superblock near ag %d",
1038 return saved_error ? saved_error : error;
1042 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1043 * also writes the superblock buffer to disk sector 0 immediately.
1047 struct xfs_mount *mp)
1049 struct xfs_trans *tp;
1053 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1057 bp = xfs_trans_getsb(tp);
1059 xfs_trans_bhold(tp, bp);
1060 xfs_trans_set_sync(tp);
1061 error = xfs_trans_commit(tp);
1065 * write out the sb buffer to get the changes to disk
1067 error = xfs_bwrite(bp);
1076 struct xfs_fsop_geom *geo,
1079 memset(geo, 0, sizeof(struct xfs_fsop_geom));
1081 geo->blocksize = sbp->sb_blocksize;
1082 geo->rtextsize = sbp->sb_rextsize;
1083 geo->agblocks = sbp->sb_agblocks;
1084 geo->agcount = sbp->sb_agcount;
1085 geo->logblocks = sbp->sb_logblocks;
1086 geo->sectsize = sbp->sb_sectsize;
1087 geo->inodesize = sbp->sb_inodesize;
1088 geo->imaxpct = sbp->sb_imax_pct;
1089 geo->datablocks = sbp->sb_dblocks;
1090 geo->rtblocks = sbp->sb_rblocks;
1091 geo->rtextents = sbp->sb_rextents;
1092 geo->logstart = sbp->sb_logstart;
1093 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1094 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1096 if (struct_version < 2)
1099 geo->sunit = sbp->sb_unit;
1100 geo->swidth = sbp->sb_width;
1102 if (struct_version < 3)
1105 geo->version = XFS_FSOP_GEOM_VERSION;
1106 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1107 XFS_FSOP_GEOM_FLAGS_DIRV2 |
1108 XFS_FSOP_GEOM_FLAGS_EXTFLG;
1109 if (xfs_sb_version_hasattr(sbp))
1110 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1111 if (xfs_sb_version_hasquota(sbp))
1112 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1113 if (xfs_sb_version_hasalign(sbp))
1114 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1115 if (xfs_sb_version_hasdalign(sbp))
1116 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1117 if (xfs_sb_version_hassector(sbp))
1118 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1119 if (xfs_sb_version_hasasciici(sbp))
1120 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1121 if (xfs_sb_version_haslazysbcount(sbp))
1122 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1123 if (xfs_sb_version_hasattr2(sbp))
1124 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1125 if (xfs_sb_version_hasprojid32bit(sbp))
1126 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1127 if (xfs_sb_version_hascrc(sbp))
1128 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1129 if (xfs_sb_version_hasftype(sbp))
1130 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1131 if (xfs_sb_version_hasfinobt(sbp))
1132 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1133 if (xfs_sb_version_hassparseinodes(sbp))
1134 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1135 if (xfs_sb_version_hasrmapbt(sbp))
1136 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1137 if (xfs_sb_version_hasreflink(sbp))
1138 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1139 if (xfs_sb_version_hasbigtime(sbp))
1140 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
1141 if (xfs_sb_version_hasinobtcounts(sbp))
1142 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
1143 if (xfs_sb_version_hassector(sbp))
1144 geo->logsectsize = sbp->sb_logsectsize;
1146 geo->logsectsize = BBSIZE;
1147 geo->rtsectsize = sbp->sb_blocksize;
1148 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1150 if (struct_version < 4)
1153 if (xfs_sb_version_haslogv2(sbp))
1154 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1156 geo->logsunit = sbp->sb_logsunit;
1158 if (struct_version < 5)
1161 geo->version = XFS_FSOP_GEOM_VERSION_V5;
1164 /* Read a secondary superblock. */
1166 xfs_sb_read_secondary(
1167 struct xfs_mount *mp,
1168 struct xfs_trans *tp,
1169 xfs_agnumber_t agno,
1170 struct xfs_buf **bpp)
1175 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1176 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1177 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1178 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1181 xfs_buf_set_ref(bp, XFS_SSB_REF);
1186 /* Get an uninitialised secondary superblock buffer. */
1188 xfs_sb_get_secondary(
1189 struct xfs_mount *mp,
1190 struct xfs_trans *tp,
1191 xfs_agnumber_t agno,
1192 struct xfs_buf **bpp)
1197 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1198 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1199 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1200 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1203 bp->b_ops = &xfs_sb_buf_ops;
1204 xfs_buf_oneshot(bp);
1210 * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
1211 * so users won't be confused by values in error messages.
1214 xfs_validate_stripe_geometry(
1215 struct xfs_mount *mp,
1221 if (swidth > INT_MAX) {
1224 "stripe width (%lld) is too large", swidth);
1228 if (sunit > swidth) {
1231 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1235 if (sectorsize && (int)sunit % sectorsize) {
1238 "stripe unit (%lld) must be a multiple of the sector size (%d)",
1243 if (sunit && !swidth) {
1246 "invalid stripe unit (%lld) and stripe width of 0", sunit);
1250 if (!sunit && swidth) {
1253 "invalid stripe width (%lld) and stripe unit of 0", swidth);
1257 if (sunit && (int)swidth % (int)sunit) {
1260 "stripe width (%lld) must be a multiple of the stripe unit (%lld)",