1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
6 #include "libxfs_priv.h"
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_trace.h"
18 #include "xfs_trans.h"
19 #include "xfs_bmap_btree.h"
20 #include "xfs_alloc_btree.h"
21 #include "xfs_rmap_btree.h"
22 #include "xfs_refcount_btree.h"
23 #include "xfs_da_format.h"
24 #include "xfs_health.h"
27 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
31 * Reference counting access wrappers to the perag structures.
32 * Because we never free per-ag structures, the only thing we
33 * have to protect against changes is the tree structure itself.
40 struct xfs_perag
*pag
;
44 pag
= radix_tree_lookup(&mp
->m_perag_tree
, agno
);
46 ASSERT(atomic_read(&pag
->pag_ref
) >= 0);
47 ref
= atomic_inc_return(&pag
->pag_ref
);
50 trace_xfs_perag_get(mp
, agno
, ref
, _RET_IP_
);
55 * search from @first to find the next perag with the given tag set.
63 struct xfs_perag
*pag
;
68 found
= radix_tree_gang_lookup_tag(&mp
->m_perag_tree
,
69 (void **)&pag
, first
, 1, tag
);
74 ref
= atomic_inc_return(&pag
->pag_ref
);
76 trace_xfs_perag_get_tag(mp
, pag
->pag_agno
, ref
, _RET_IP_
);
82 struct xfs_perag
*pag
)
86 ASSERT(atomic_read(&pag
->pag_ref
) > 0);
87 ref
= atomic_dec_return(&pag
->pag_ref
);
88 trace_xfs_perag_put(pag
->pag_mount
, pag
->pag_agno
, ref
, _RET_IP_
);
91 /* Check all the superblock fields we care about when reading one in. */
97 if (XFS_SB_VERSION_NUM(sbp
) != XFS_SB_VERSION_5
)
101 * Version 5 superblock feature mask validation. Reject combinations
102 * the kernel cannot support up front before checking anything else.
104 if (xfs_sb_has_compat_feature(sbp
, XFS_SB_FEAT_COMPAT_UNKNOWN
)) {
106 "Superblock has unknown compatible features (0x%x) enabled.",
107 (sbp
->sb_features_compat
& XFS_SB_FEAT_COMPAT_UNKNOWN
));
109 "Using a more recent kernel is recommended.");
112 if (xfs_sb_has_ro_compat_feature(sbp
, XFS_SB_FEAT_RO_COMPAT_UNKNOWN
)) {
114 "Superblock has unknown read-only compatible features (0x%x) enabled.",
115 (sbp
->sb_features_ro_compat
&
116 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
));
117 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
119 "Attempted to mount read-only compatible filesystem read-write.");
121 "Filesystem can only be safely mounted read only.");
126 if (xfs_sb_has_incompat_feature(sbp
, XFS_SB_FEAT_INCOMPAT_UNKNOWN
)) {
128 "Superblock has unknown incompatible features (0x%x) enabled.",
129 (sbp
->sb_features_incompat
&
130 XFS_SB_FEAT_INCOMPAT_UNKNOWN
));
132 "Filesystem cannot be safely mounted by this kernel.");
139 /* Check all the superblock fields we care about when writing one out. */
141 xfs_validate_sb_write(
142 struct xfs_mount
*mp
,
147 * Carry out additional sb summary counter sanity checks when we write
148 * the superblock. We skip this in the read validator because there
149 * could be newer superblocks in the log and if the values are garbage
150 * even after replay we'll recalculate them at the end of log mount.
152 * mkfs has traditionally written zeroed counters to inprogress and
153 * secondary superblocks, so allow this usage to continue because
154 * we never read counters from such superblocks.
156 if (XFS_BUF_ADDR(bp
) == XFS_SB_DADDR
&& !sbp
->sb_inprogress
&&
157 (sbp
->sb_fdblocks
> sbp
->sb_dblocks
||
158 !xfs_verify_icount(mp
, sbp
->sb_icount
) ||
159 sbp
->sb_ifree
> sbp
->sb_icount
)) {
160 xfs_warn(mp
, "SB summary counter sanity check failed");
161 return -EFSCORRUPTED
;
164 if (XFS_SB_VERSION_NUM(sbp
) != XFS_SB_VERSION_5
)
168 * Version 5 superblock feature mask validation. Reject combinations
169 * the kernel cannot support since we checked for unsupported bits in
170 * the read verifier, which means that memory is corrupt.
172 if (xfs_sb_has_compat_feature(sbp
, XFS_SB_FEAT_COMPAT_UNKNOWN
)) {
174 "Corruption detected in superblock compatible features (0x%x)!",
175 (sbp
->sb_features_compat
& XFS_SB_FEAT_COMPAT_UNKNOWN
));
176 return -EFSCORRUPTED
;
179 if (xfs_sb_has_ro_compat_feature(sbp
, XFS_SB_FEAT_RO_COMPAT_UNKNOWN
)) {
181 "Corruption detected in superblock read-only compatible features (0x%x)!",
182 (sbp
->sb_features_ro_compat
&
183 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
));
184 return -EFSCORRUPTED
;
186 if (xfs_sb_has_incompat_feature(sbp
, XFS_SB_FEAT_INCOMPAT_UNKNOWN
)) {
188 "Corruption detected in superblock incompatible features (0x%x)!",
189 (sbp
->sb_features_incompat
&
190 XFS_SB_FEAT_INCOMPAT_UNKNOWN
));
191 return -EFSCORRUPTED
;
193 if (xfs_sb_has_incompat_log_feature(sbp
,
194 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
)) {
196 "Corruption detected in superblock incompatible log features (0x%x)!",
197 (sbp
->sb_features_log_incompat
&
198 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
));
199 return -EFSCORRUPTED
;
203 * We can't read verify the sb LSN because the read verifier is called
204 * before the log is allocated and processed. We know the log is set up
205 * before write verifier calls, so check it here.
207 if (!xfs_log_check_lsn(mp
, sbp
->sb_lsn
))
208 return -EFSCORRUPTED
;
213 /* Check the validity of the SB. */
215 xfs_validate_sb_common(
216 struct xfs_mount
*mp
,
220 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
221 uint32_t agcount
= 0;
224 if (!xfs_verify_magic(bp
, dsb
->sb_magicnum
)) {
225 xfs_warn(mp
, "bad magic number");
229 if (!xfs_sb_good_version(sbp
)) {
230 xfs_warn(mp
, "bad version");
234 if (xfs_sb_version_has_pquotino(sbp
)) {
235 if (sbp
->sb_qflags
& (XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
)) {
237 "Version 5 of Super block has XFS_OQUOTA bits.");
238 return -EFSCORRUPTED
;
240 } else if (sbp
->sb_qflags
& (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
|
241 XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
)) {
243 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
244 return -EFSCORRUPTED
;
248 * Full inode chunks must be aligned to inode chunk size when
249 * sparse inodes are enabled to support the sparse chunk
250 * allocation algorithm and prevent overlapping inode records.
252 if (xfs_sb_version_hassparseinodes(sbp
)) {
255 align
= XFS_INODES_PER_CHUNK
* sbp
->sb_inodesize
257 if (sbp
->sb_inoalignmt
!= align
) {
259 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
260 sbp
->sb_inoalignmt
, align
);
266 sbp
->sb_logstart
== 0 && mp
->m_logdev_targp
== mp
->m_ddev_targp
)) {
268 "filesystem is marked as having an external log; "
269 "specify logdev on the mount command line.");
274 sbp
->sb_logstart
!= 0 && mp
->m_logdev_targp
!= mp
->m_ddev_targp
)) {
276 "filesystem is marked as having an internal log; "
277 "do not specify logdev on the mount command line.");
281 /* Compute agcount for this number of dblocks and agblocks */
282 if (sbp
->sb_agblocks
) {
283 agcount
= div_u64_rem(sbp
->sb_dblocks
, sbp
->sb_agblocks
, &rem
);
289 * More sanity checking. Most of these were stolen directly from
293 sbp
->sb_agcount
<= 0 ||
294 sbp
->sb_sectsize
< XFS_MIN_SECTORSIZE
||
295 sbp
->sb_sectsize
> XFS_MAX_SECTORSIZE
||
296 sbp
->sb_sectlog
< XFS_MIN_SECTORSIZE_LOG
||
297 sbp
->sb_sectlog
> XFS_MAX_SECTORSIZE_LOG
||
298 sbp
->sb_sectsize
!= (1 << sbp
->sb_sectlog
) ||
299 sbp
->sb_blocksize
< XFS_MIN_BLOCKSIZE
||
300 sbp
->sb_blocksize
> XFS_MAX_BLOCKSIZE
||
301 sbp
->sb_blocklog
< XFS_MIN_BLOCKSIZE_LOG
||
302 sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
303 sbp
->sb_blocksize
!= (1 << sbp
->sb_blocklog
) ||
304 sbp
->sb_dirblklog
+ sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
305 sbp
->sb_inodesize
< XFS_DINODE_MIN_SIZE
||
306 sbp
->sb_inodesize
> XFS_DINODE_MAX_SIZE
||
307 sbp
->sb_inodelog
< XFS_DINODE_MIN_LOG
||
308 sbp
->sb_inodelog
> XFS_DINODE_MAX_LOG
||
309 sbp
->sb_inodesize
!= (1 << sbp
->sb_inodelog
) ||
310 sbp
->sb_logsunit
> XLOG_MAX_RECORD_BSIZE
||
311 sbp
->sb_inopblock
!= howmany(sbp
->sb_blocksize
,sbp
->sb_inodesize
) ||
312 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) < XFS_MIN_AG_BYTES
||
313 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) > XFS_MAX_AG_BYTES
||
314 sbp
->sb_agblklog
!= xfs_highbit32(sbp
->sb_agblocks
- 1) + 1 ||
315 agcount
== 0 || agcount
!= sbp
->sb_agcount
||
316 (sbp
->sb_blocklog
- sbp
->sb_inodelog
!= sbp
->sb_inopblog
) ||
317 (sbp
->sb_rextsize
* sbp
->sb_blocksize
> XFS_MAX_RTEXTSIZE
) ||
318 (sbp
->sb_rextsize
* sbp
->sb_blocksize
< XFS_MIN_RTEXTSIZE
) ||
319 (sbp
->sb_imax_pct
> 100 /* zero sb_imax_pct is valid */) ||
320 sbp
->sb_dblocks
== 0 ||
321 sbp
->sb_dblocks
> XFS_MAX_DBLOCKS(sbp
) ||
322 sbp
->sb_dblocks
< XFS_MIN_DBLOCKS(sbp
) ||
323 sbp
->sb_shared_vn
!= 0)) {
324 xfs_notice(mp
, "SB sanity check failed");
325 return -EFSCORRUPTED
;
329 if (!xfs_sb_version_hasdalign(sbp
) ||
330 sbp
->sb_unit
> sbp
->sb_width
||
331 (sbp
->sb_width
% sbp
->sb_unit
) != 0) {
332 xfs_notice(mp
, "SB stripe unit sanity check failed");
333 return -EFSCORRUPTED
;
335 } else if (xfs_sb_version_hasdalign(sbp
)) {
336 xfs_notice(mp
, "SB stripe alignment sanity check failed");
337 return -EFSCORRUPTED
;
338 } else if (sbp
->sb_width
) {
339 xfs_notice(mp
, "SB stripe width sanity check failed");
340 return -EFSCORRUPTED
;
344 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
345 sbp
->sb_blocksize
< XFS_MIN_CRC_BLOCKSIZE
) {
346 xfs_notice(mp
, "v5 SB sanity check failed");
347 return -EFSCORRUPTED
;
351 * Currently only very few inode sizes are supported.
353 switch (sbp
->sb_inodesize
) {
360 xfs_warn(mp
, "inode size of %d bytes not supported",
365 if (xfs_sb_validate_fsb_count(sbp
, sbp
->sb_dblocks
) ||
366 xfs_sb_validate_fsb_count(sbp
, sbp
->sb_rblocks
)) {
368 "file system too large to be mounted on this system.");
376 xfs_sb_quota_from_disk(struct xfs_sb
*sbp
)
379 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
380 * leads to in-core values having two different values for a quota
381 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
384 * Note that this change affect only the in-core values. These
385 * values are not written back to disk unless any quota information
386 * is written to the disk. Even in that case, sb_pquotino field is
387 * not written to disk unless the superblock supports pquotino.
389 if (sbp
->sb_uquotino
== 0)
390 sbp
->sb_uquotino
= NULLFSINO
;
391 if (sbp
->sb_gquotino
== 0)
392 sbp
->sb_gquotino
= NULLFSINO
;
393 if (sbp
->sb_pquotino
== 0)
394 sbp
->sb_pquotino
= NULLFSINO
;
397 * We need to do these manipilations only if we are working
398 * with an older version of on-disk superblock.
400 if (xfs_sb_version_has_pquotino(sbp
))
403 if (sbp
->sb_qflags
& XFS_OQUOTA_ENFD
)
404 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
405 XFS_PQUOTA_ENFD
: XFS_GQUOTA_ENFD
;
406 if (sbp
->sb_qflags
& XFS_OQUOTA_CHKD
)
407 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
408 XFS_PQUOTA_CHKD
: XFS_GQUOTA_CHKD
;
409 sbp
->sb_qflags
&= ~(XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
);
411 if (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
&&
412 sbp
->sb_gquotino
!= NULLFSINO
) {
414 * In older version of superblock, on-disk superblock only
415 * has sb_gquotino, and in-core superblock has both sb_gquotino
416 * and sb_pquotino. But, only one of them is supported at any
417 * point of time. So, if PQUOTA is set in disk superblock,
418 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
419 * above is to make sure we don't do this twice and wipe them
422 sbp
->sb_pquotino
= sbp
->sb_gquotino
;
423 sbp
->sb_gquotino
= NULLFSINO
;
433 to
->sb_magicnum
= be32_to_cpu(from
->sb_magicnum
);
434 to
->sb_blocksize
= be32_to_cpu(from
->sb_blocksize
);
435 to
->sb_dblocks
= be64_to_cpu(from
->sb_dblocks
);
436 to
->sb_rblocks
= be64_to_cpu(from
->sb_rblocks
);
437 to
->sb_rextents
= be64_to_cpu(from
->sb_rextents
);
438 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
439 to
->sb_logstart
= be64_to_cpu(from
->sb_logstart
);
440 to
->sb_rootino
= be64_to_cpu(from
->sb_rootino
);
441 to
->sb_rbmino
= be64_to_cpu(from
->sb_rbmino
);
442 to
->sb_rsumino
= be64_to_cpu(from
->sb_rsumino
);
443 to
->sb_rextsize
= be32_to_cpu(from
->sb_rextsize
);
444 to
->sb_agblocks
= be32_to_cpu(from
->sb_agblocks
);
445 to
->sb_agcount
= be32_to_cpu(from
->sb_agcount
);
446 to
->sb_rbmblocks
= be32_to_cpu(from
->sb_rbmblocks
);
447 to
->sb_logblocks
= be32_to_cpu(from
->sb_logblocks
);
448 to
->sb_versionnum
= be16_to_cpu(from
->sb_versionnum
);
449 to
->sb_sectsize
= be16_to_cpu(from
->sb_sectsize
);
450 to
->sb_inodesize
= be16_to_cpu(from
->sb_inodesize
);
451 to
->sb_inopblock
= be16_to_cpu(from
->sb_inopblock
);
452 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
453 to
->sb_blocklog
= from
->sb_blocklog
;
454 to
->sb_sectlog
= from
->sb_sectlog
;
455 to
->sb_inodelog
= from
->sb_inodelog
;
456 to
->sb_inopblog
= from
->sb_inopblog
;
457 to
->sb_agblklog
= from
->sb_agblklog
;
458 to
->sb_rextslog
= from
->sb_rextslog
;
459 to
->sb_inprogress
= from
->sb_inprogress
;
460 to
->sb_imax_pct
= from
->sb_imax_pct
;
461 to
->sb_icount
= be64_to_cpu(from
->sb_icount
);
462 to
->sb_ifree
= be64_to_cpu(from
->sb_ifree
);
463 to
->sb_fdblocks
= be64_to_cpu(from
->sb_fdblocks
);
464 to
->sb_frextents
= be64_to_cpu(from
->sb_frextents
);
465 to
->sb_uquotino
= be64_to_cpu(from
->sb_uquotino
);
466 to
->sb_gquotino
= be64_to_cpu(from
->sb_gquotino
);
467 to
->sb_qflags
= be16_to_cpu(from
->sb_qflags
);
468 to
->sb_flags
= from
->sb_flags
;
469 to
->sb_shared_vn
= from
->sb_shared_vn
;
470 to
->sb_inoalignmt
= be32_to_cpu(from
->sb_inoalignmt
);
471 to
->sb_unit
= be32_to_cpu(from
->sb_unit
);
472 to
->sb_width
= be32_to_cpu(from
->sb_width
);
473 to
->sb_dirblklog
= from
->sb_dirblklog
;
474 to
->sb_logsectlog
= from
->sb_logsectlog
;
475 to
->sb_logsectsize
= be16_to_cpu(from
->sb_logsectsize
);
476 to
->sb_logsunit
= be32_to_cpu(from
->sb_logsunit
);
477 to
->sb_features2
= be32_to_cpu(from
->sb_features2
);
478 to
->sb_bad_features2
= be32_to_cpu(from
->sb_bad_features2
);
479 to
->sb_features_compat
= be32_to_cpu(from
->sb_features_compat
);
480 to
->sb_features_ro_compat
= be32_to_cpu(from
->sb_features_ro_compat
);
481 to
->sb_features_incompat
= be32_to_cpu(from
->sb_features_incompat
);
482 to
->sb_features_log_incompat
=
483 be32_to_cpu(from
->sb_features_log_incompat
);
484 /* crc is only used on disk, not in memory; just init to 0 here. */
486 to
->sb_spino_align
= be32_to_cpu(from
->sb_spino_align
);
487 to
->sb_pquotino
= be64_to_cpu(from
->sb_pquotino
);
488 to
->sb_lsn
= be64_to_cpu(from
->sb_lsn
);
490 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
491 * feature flag is set; if not set we keep it only in memory.
493 if (xfs_sb_version_hasmetauuid(to
))
494 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
496 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_uuid
);
497 /* Convert on-disk flags to in-memory flags? */
499 xfs_sb_quota_from_disk(to
);
507 __xfs_sb_from_disk(to
, from
, true);
511 xfs_sb_quota_to_disk(
515 uint16_t qflags
= from
->sb_qflags
;
517 to
->sb_uquotino
= cpu_to_be64(from
->sb_uquotino
);
518 if (xfs_sb_version_has_pquotino(from
)) {
519 to
->sb_qflags
= cpu_to_be16(from
->sb_qflags
);
520 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
521 to
->sb_pquotino
= cpu_to_be64(from
->sb_pquotino
);
526 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
527 * flags, whereas the on-disk version does. So, convert incore
528 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
530 qflags
&= ~(XFS_PQUOTA_ENFD
| XFS_PQUOTA_CHKD
|
531 XFS_GQUOTA_ENFD
| XFS_GQUOTA_CHKD
);
533 if (from
->sb_qflags
&
534 (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
))
535 qflags
|= XFS_OQUOTA_ENFD
;
536 if (from
->sb_qflags
&
537 (XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
))
538 qflags
|= XFS_OQUOTA_CHKD
;
539 to
->sb_qflags
= cpu_to_be16(qflags
);
542 * GQUOTINO and PQUOTINO cannot be used together in versions
543 * of superblock that do not have pquotino. from->sb_flags
544 * tells us which quota is active and should be copied to
545 * disk. If neither are active, we should NULL the inode.
547 * In all cases, the separate pquotino must remain 0 because it
548 * it beyond the "end" of the valid non-pquotino superblock.
550 if (from
->sb_qflags
& XFS_GQUOTA_ACCT
)
551 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
552 else if (from
->sb_qflags
& XFS_PQUOTA_ACCT
)
553 to
->sb_gquotino
= cpu_to_be64(from
->sb_pquotino
);
556 * We can't rely on just the fields being logged to tell us
557 * that it is safe to write NULLFSINO - we should only do that
558 * if quotas are not actually enabled. Hence only write
559 * NULLFSINO if both in-core quota inodes are NULL.
561 if (from
->sb_gquotino
== NULLFSINO
&&
562 from
->sb_pquotino
== NULLFSINO
)
563 to
->sb_gquotino
= cpu_to_be64(NULLFSINO
);
574 xfs_sb_quota_to_disk(to
, from
);
576 to
->sb_magicnum
= cpu_to_be32(from
->sb_magicnum
);
577 to
->sb_blocksize
= cpu_to_be32(from
->sb_blocksize
);
578 to
->sb_dblocks
= cpu_to_be64(from
->sb_dblocks
);
579 to
->sb_rblocks
= cpu_to_be64(from
->sb_rblocks
);
580 to
->sb_rextents
= cpu_to_be64(from
->sb_rextents
);
581 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
582 to
->sb_logstart
= cpu_to_be64(from
->sb_logstart
);
583 to
->sb_rootino
= cpu_to_be64(from
->sb_rootino
);
584 to
->sb_rbmino
= cpu_to_be64(from
->sb_rbmino
);
585 to
->sb_rsumino
= cpu_to_be64(from
->sb_rsumino
);
586 to
->sb_rextsize
= cpu_to_be32(from
->sb_rextsize
);
587 to
->sb_agblocks
= cpu_to_be32(from
->sb_agblocks
);
588 to
->sb_agcount
= cpu_to_be32(from
->sb_agcount
);
589 to
->sb_rbmblocks
= cpu_to_be32(from
->sb_rbmblocks
);
590 to
->sb_logblocks
= cpu_to_be32(from
->sb_logblocks
);
591 to
->sb_versionnum
= cpu_to_be16(from
->sb_versionnum
);
592 to
->sb_sectsize
= cpu_to_be16(from
->sb_sectsize
);
593 to
->sb_inodesize
= cpu_to_be16(from
->sb_inodesize
);
594 to
->sb_inopblock
= cpu_to_be16(from
->sb_inopblock
);
595 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
596 to
->sb_blocklog
= from
->sb_blocklog
;
597 to
->sb_sectlog
= from
->sb_sectlog
;
598 to
->sb_inodelog
= from
->sb_inodelog
;
599 to
->sb_inopblog
= from
->sb_inopblog
;
600 to
->sb_agblklog
= from
->sb_agblklog
;
601 to
->sb_rextslog
= from
->sb_rextslog
;
602 to
->sb_inprogress
= from
->sb_inprogress
;
603 to
->sb_imax_pct
= from
->sb_imax_pct
;
604 to
->sb_icount
= cpu_to_be64(from
->sb_icount
);
605 to
->sb_ifree
= cpu_to_be64(from
->sb_ifree
);
606 to
->sb_fdblocks
= cpu_to_be64(from
->sb_fdblocks
);
607 to
->sb_frextents
= cpu_to_be64(from
->sb_frextents
);
609 to
->sb_flags
= from
->sb_flags
;
610 to
->sb_shared_vn
= from
->sb_shared_vn
;
611 to
->sb_inoalignmt
= cpu_to_be32(from
->sb_inoalignmt
);
612 to
->sb_unit
= cpu_to_be32(from
->sb_unit
);
613 to
->sb_width
= cpu_to_be32(from
->sb_width
);
614 to
->sb_dirblklog
= from
->sb_dirblklog
;
615 to
->sb_logsectlog
= from
->sb_logsectlog
;
616 to
->sb_logsectsize
= cpu_to_be16(from
->sb_logsectsize
);
617 to
->sb_logsunit
= cpu_to_be32(from
->sb_logsunit
);
620 * We need to ensure that bad_features2 always matches features2.
621 * Hence we enforce that here rather than having to remember to do it
622 * everywhere else that updates features2.
624 from
->sb_bad_features2
= from
->sb_features2
;
625 to
->sb_features2
= cpu_to_be32(from
->sb_features2
);
626 to
->sb_bad_features2
= cpu_to_be32(from
->sb_bad_features2
);
628 if (xfs_sb_version_hascrc(from
)) {
629 to
->sb_features_compat
= cpu_to_be32(from
->sb_features_compat
);
630 to
->sb_features_ro_compat
=
631 cpu_to_be32(from
->sb_features_ro_compat
);
632 to
->sb_features_incompat
=
633 cpu_to_be32(from
->sb_features_incompat
);
634 to
->sb_features_log_incompat
=
635 cpu_to_be32(from
->sb_features_log_incompat
);
636 to
->sb_spino_align
= cpu_to_be32(from
->sb_spino_align
);
637 to
->sb_lsn
= cpu_to_be64(from
->sb_lsn
);
638 if (xfs_sb_version_hasmetauuid(from
))
639 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
644 * If the superblock has the CRC feature bit set or the CRC field is non-null,
645 * check that the CRC is valid. We check the CRC field is non-null because a
646 * single bit error could clear the feature bit and unused parts of the
647 * superblock are supposed to be zero. Hence a non-null crc field indicates that
648 * we've potentially lost a feature bit and we should check it anyway.
650 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
651 * last field in V4 secondary superblocks. So for secondary superblocks,
652 * we are more forgiving, and ignore CRC failures if the primary doesn't
653 * indicate that the fs version is V5.
660 struct xfs_mount
*mp
= bp
->b_mount
;
661 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
665 * open code the version check to avoid needing to convert the entire
666 * superblock from disk order just to check the version number
668 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
) &&
669 (((be16_to_cpu(dsb
->sb_versionnum
) & XFS_SB_VERSION_NUMBITS
) ==
673 if (!xfs_buf_verify_cksum(bp
, XFS_SB_CRC_OFF
)) {
674 /* Only fail bad secondaries on a known V5 filesystem */
675 if (bp
->b_bn
== XFS_SB_DADDR
||
676 xfs_sb_version_hascrc(&mp
->m_sb
)) {
684 * Check all the superblock fields. Don't byteswap the xquota flags
685 * because _verify_common checks the on-disk values.
687 __xfs_sb_from_disk(&sb
, XFS_BUF_TO_SBP(bp
), false);
688 error
= xfs_validate_sb_common(mp
, bp
, &sb
);
691 error
= xfs_validate_sb_read(mp
, &sb
);
694 if (error
== -EFSCORRUPTED
|| error
== -EFSBADCRC
)
695 xfs_verifier_error(bp
, error
, __this_address
);
697 xfs_buf_ioerror(bp
, error
);
701 * We may be probed for a filesystem match, so we may not want to emit
702 * messages when the superblock buffer is not actually an XFS superblock.
703 * If we find an XFS superblock, then run a normal, noisy mount because we are
704 * really going to mount it and want to know about errors.
707 xfs_sb_quiet_read_verify(
710 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
712 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
)) {
713 /* XFS filesystem, verify noisily! */
714 xfs_sb_read_verify(bp
);
718 xfs_buf_ioerror(bp
, -EWRONGFS
);
726 struct xfs_mount
*mp
= bp
->b_mount
;
727 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
731 * Check all the superblock fields. Don't byteswap the xquota flags
732 * because _verify_common checks the on-disk values.
734 __xfs_sb_from_disk(&sb
, XFS_BUF_TO_SBP(bp
), false);
735 error
= xfs_validate_sb_common(mp
, bp
, &sb
);
738 error
= xfs_validate_sb_write(mp
, bp
, &sb
);
742 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
746 XFS_BUF_TO_SBP(bp
)->sb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
748 xfs_buf_update_cksum(bp
, XFS_SB_CRC_OFF
);
752 xfs_verifier_error(bp
, error
, __this_address
);
755 const struct xfs_buf_ops xfs_sb_buf_ops
= {
757 .magic
= { cpu_to_be32(XFS_SB_MAGIC
), cpu_to_be32(XFS_SB_MAGIC
) },
758 .verify_read
= xfs_sb_read_verify
,
759 .verify_write
= xfs_sb_write_verify
,
762 const struct xfs_buf_ops xfs_sb_quiet_buf_ops
= {
763 .name
= "xfs_sb_quiet",
764 .magic
= { cpu_to_be32(XFS_SB_MAGIC
), cpu_to_be32(XFS_SB_MAGIC
) },
765 .verify_read
= xfs_sb_quiet_read_verify
,
766 .verify_write
= xfs_sb_write_verify
,
772 * Mount initialization code establishing various mount
773 * fields from the superblock associated with the given
776 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
780 struct xfs_mount
*mp
,
783 mp
->m_agfrotor
= mp
->m_agirotor
= 0;
784 mp
->m_maxagi
= mp
->m_sb
.sb_agcount
;
785 mp
->m_blkbit_log
= sbp
->sb_blocklog
+ XFS_NBBYLOG
;
786 mp
->m_blkbb_log
= sbp
->sb_blocklog
- BBSHIFT
;
787 mp
->m_sectbb_log
= sbp
->sb_sectlog
- BBSHIFT
;
788 mp
->m_agno_log
= xfs_highbit32(sbp
->sb_agcount
- 1) + 1;
789 mp
->m_blockmask
= sbp
->sb_blocksize
- 1;
790 mp
->m_blockwsize
= sbp
->sb_blocksize
>> XFS_WORDLOG
;
791 mp
->m_blockwmask
= mp
->m_blockwsize
- 1;
793 mp
->m_alloc_mxr
[0] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
794 mp
->m_alloc_mxr
[1] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
795 mp
->m_alloc_mnr
[0] = mp
->m_alloc_mxr
[0] / 2;
796 mp
->m_alloc_mnr
[1] = mp
->m_alloc_mxr
[1] / 2;
798 mp
->m_bmap_dmxr
[0] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
799 mp
->m_bmap_dmxr
[1] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
800 mp
->m_bmap_dmnr
[0] = mp
->m_bmap_dmxr
[0] / 2;
801 mp
->m_bmap_dmnr
[1] = mp
->m_bmap_dmxr
[1] / 2;
803 mp
->m_rmap_mxr
[0] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 1);
804 mp
->m_rmap_mxr
[1] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 0);
805 mp
->m_rmap_mnr
[0] = mp
->m_rmap_mxr
[0] / 2;
806 mp
->m_rmap_mnr
[1] = mp
->m_rmap_mxr
[1] / 2;
808 mp
->m_refc_mxr
[0] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, true);
809 mp
->m_refc_mxr
[1] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, false);
810 mp
->m_refc_mnr
[0] = mp
->m_refc_mxr
[0] / 2;
811 mp
->m_refc_mnr
[1] = mp
->m_refc_mxr
[1] / 2;
813 mp
->m_bsize
= XFS_FSB_TO_BB(mp
, 1);
814 mp
->m_alloc_set_aside
= xfs_alloc_set_aside(mp
);
815 mp
->m_ag_max_usable
= xfs_alloc_ag_max_usable(mp
);
819 * xfs_initialize_perag_data
821 * Read in each per-ag structure so we can count up the number of
822 * allocated inodes, free inodes and used filesystem blocks as this
823 * information is no longer persistent in the superblock. Once we have
824 * this information, write it into the in-core superblock structure.
827 xfs_initialize_perag_data(
828 struct xfs_mount
*mp
,
829 xfs_agnumber_t agcount
)
831 xfs_agnumber_t index
;
833 xfs_sb_t
*sbp
= &mp
->m_sb
;
837 uint64_t bfreelst
= 0;
842 for (index
= 0; index
< agcount
; index
++) {
844 * read the agf, then the agi. This gets us
845 * all the information we need and populates the
846 * per-ag structures for us.
848 error
= xfs_alloc_pagf_init(mp
, NULL
, index
, 0);
852 error
= xfs_ialloc_pagi_init(mp
, NULL
, index
);
855 pag
= xfs_perag_get(mp
, index
);
856 ifree
+= pag
->pagi_freecount
;
857 ialloc
+= pag
->pagi_count
;
858 bfree
+= pag
->pagf_freeblks
;
859 bfreelst
+= pag
->pagf_flcount
;
860 btree
+= pag
->pagf_btreeblks
;
863 fdblocks
= bfree
+ bfreelst
+ btree
;
866 * If the new summary counts are obviously incorrect, fail the
867 * mount operation because that implies the AGFs are also corrupt.
868 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
869 * will prevent xfs_repair from fixing anything.
871 if (fdblocks
> sbp
->sb_dblocks
|| ifree
> ialloc
) {
872 xfs_alert(mp
, "AGF corruption. Please run xfs_repair.");
873 error
= -EFSCORRUPTED
;
877 /* Overwrite incore superblock counters with just-read data */
878 spin_lock(&mp
->m_sb_lock
);
879 sbp
->sb_ifree
= ifree
;
880 sbp
->sb_icount
= ialloc
;
881 sbp
->sb_fdblocks
= fdblocks
;
882 spin_unlock(&mp
->m_sb_lock
);
884 xfs_reinit_percpu_counters(mp
);
886 xfs_fs_mark_healthy(mp
, XFS_SICK_FS_COUNTERS
);
891 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
892 * into the superblock buffer to be logged. It does not provide the higher
893 * level of locking that is needed to protect the in-core superblock from
898 struct xfs_trans
*tp
)
900 struct xfs_mount
*mp
= tp
->t_mountp
;
901 struct xfs_buf
*bp
= xfs_trans_getsb(tp
, mp
);
903 mp
->m_sb
.sb_icount
= percpu_counter_sum(&mp
->m_icount
);
904 mp
->m_sb
.sb_ifree
= percpu_counter_sum(&mp
->m_ifree
);
905 mp
->m_sb
.sb_fdblocks
= percpu_counter_sum(&mp
->m_fdblocks
);
907 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
908 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_SB_BUF
);
909 xfs_trans_log_buf(tp
, bp
, 0, sizeof(struct xfs_dsb
) - 1);
915 * Sync the superblock to disk.
917 * Note that the caller is responsible for checking the frozen state of the
918 * filesystem. This procedure uses the non-blocking transaction allocator and
919 * thus will allow modifications to a frozen fs. This is required because this
920 * code can be called during the process of freezing where use of the high-level
921 * allocator would deadlock.
925 struct xfs_mount
*mp
,
928 struct xfs_trans
*tp
;
931 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0,
932 XFS_TRANS_NO_WRITECOUNT
, &tp
);
938 xfs_trans_set_sync(tp
);
939 return xfs_trans_commit(tp
);
943 * Update all the secondary superblocks to match the new state of the primary.
944 * Because we are completely overwriting all the existing fields in the
945 * secondary superblock buffers, there is no need to read them in from disk.
946 * Just get a new buffer, stamp it and write it.
948 * The sb buffers need to be cached here so that we serialise against other
949 * operations that access the secondary superblocks, but we don't want to keep
950 * them in memory once it is written so we mark it as a one-shot buffer.
953 xfs_update_secondary_sbs(
954 struct xfs_mount
*mp
)
959 LIST_HEAD (buffer_list
);
961 /* update secondary superblocks. */
962 for (agno
= 1; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
965 error
= xfs_buf_get(mp
->m_ddev_targp
,
966 XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
967 XFS_FSS_TO_BB(mp
, 1), &bp
);
969 * If we get an error reading or writing alternate superblocks,
970 * continue. xfs_repair chooses the "best" superblock based
971 * on most matches; if we break early, we'll leave more
972 * superblocks un-updated than updated, and xfs_repair may
973 * pick them over the properly-updated primary.
977 "error allocating secondary superblock for ag %d",
984 bp
->b_ops
= &xfs_sb_buf_ops
;
986 xfs_buf_zero(bp
, 0, BBTOB(bp
->b_length
));
987 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
988 xfs_buf_delwri_queue(bp
, &buffer_list
);
991 /* don't hold too many buffers at once */
995 error
= xfs_buf_delwri_submit(&buffer_list
);
998 "write error %d updating a secondary superblock near ag %d",
1001 saved_error
= error
;
1005 error
= xfs_buf_delwri_submit(&buffer_list
);
1008 "write error %d updating a secondary superblock near ag %d",
1012 return saved_error
? saved_error
: error
;
1016 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1017 * also writes the superblock buffer to disk sector 0 immediately.
1021 struct xfs_mount
*mp
)
1023 struct xfs_trans
*tp
;
1027 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0, 0, &tp
);
1031 bp
= xfs_trans_getsb(tp
, mp
);
1033 xfs_trans_bhold(tp
, bp
);
1034 xfs_trans_set_sync(tp
);
1035 error
= xfs_trans_commit(tp
);
1039 * write out the sb buffer to get the changes to disk
1041 error
= xfs_bwrite(bp
);
1050 struct xfs_fsop_geom
*geo
,
1053 memset(geo
, 0, sizeof(struct xfs_fsop_geom
));
1055 geo
->blocksize
= sbp
->sb_blocksize
;
1056 geo
->rtextsize
= sbp
->sb_rextsize
;
1057 geo
->agblocks
= sbp
->sb_agblocks
;
1058 geo
->agcount
= sbp
->sb_agcount
;
1059 geo
->logblocks
= sbp
->sb_logblocks
;
1060 geo
->sectsize
= sbp
->sb_sectsize
;
1061 geo
->inodesize
= sbp
->sb_inodesize
;
1062 geo
->imaxpct
= sbp
->sb_imax_pct
;
1063 geo
->datablocks
= sbp
->sb_dblocks
;
1064 geo
->rtblocks
= sbp
->sb_rblocks
;
1065 geo
->rtextents
= sbp
->sb_rextents
;
1066 geo
->logstart
= sbp
->sb_logstart
;
1067 BUILD_BUG_ON(sizeof(geo
->uuid
) != sizeof(sbp
->sb_uuid
));
1068 memcpy(geo
->uuid
, &sbp
->sb_uuid
, sizeof(sbp
->sb_uuid
));
1070 if (struct_version
< 2)
1073 geo
->sunit
= sbp
->sb_unit
;
1074 geo
->swidth
= sbp
->sb_width
;
1076 if (struct_version
< 3)
1079 geo
->version
= XFS_FSOP_GEOM_VERSION
;
1080 geo
->flags
= XFS_FSOP_GEOM_FLAGS_NLINK
|
1081 XFS_FSOP_GEOM_FLAGS_DIRV2
|
1082 XFS_FSOP_GEOM_FLAGS_EXTFLG
;
1083 if (xfs_sb_version_hasattr(sbp
))
1084 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR
;
1085 if (xfs_sb_version_hasquota(sbp
))
1086 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_QUOTA
;
1087 if (xfs_sb_version_hasalign(sbp
))
1088 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_IALIGN
;
1089 if (xfs_sb_version_hasdalign(sbp
))
1090 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DALIGN
;
1091 if (xfs_sb_version_hassector(sbp
))
1092 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SECTOR
;
1093 if (xfs_sb_version_hasasciici(sbp
))
1094 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DIRV2CI
;
1095 if (xfs_sb_version_haslazysbcount(sbp
))
1096 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LAZYSB
;
1097 if (xfs_sb_version_hasattr2(sbp
))
1098 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR2
;
1099 if (xfs_sb_version_hasprojid32bit(sbp
))
1100 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_PROJID32
;
1101 if (xfs_sb_version_hascrc(sbp
))
1102 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_V5SB
;
1103 if (xfs_sb_version_hasftype(sbp
))
1104 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FTYPE
;
1105 if (xfs_sb_version_hasfinobt(sbp
))
1106 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FINOBT
;
1107 if (xfs_sb_version_hassparseinodes(sbp
))
1108 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SPINODES
;
1109 if (xfs_sb_version_hasrmapbt(sbp
))
1110 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_RMAPBT
;
1111 if (xfs_sb_version_hasreflink(sbp
))
1112 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_REFLINK
;
1113 if (xfs_sb_version_hassector(sbp
))
1114 geo
->logsectsize
= sbp
->sb_logsectsize
;
1116 geo
->logsectsize
= BBSIZE
;
1117 geo
->rtsectsize
= sbp
->sb_blocksize
;
1118 geo
->dirblocksize
= xfs_dir2_dirblock_bytes(sbp
);
1120 if (struct_version
< 4)
1123 if (xfs_sb_version_haslogv2(sbp
))
1124 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LOGV2
;
1126 geo
->logsunit
= sbp
->sb_logsunit
;
1128 if (struct_version
< 5)
1131 geo
->version
= XFS_FSOP_GEOM_VERSION_V5
;
1134 /* Read a secondary superblock. */
1136 xfs_sb_read_secondary(
1137 struct xfs_mount
*mp
,
1138 struct xfs_trans
*tp
,
1139 xfs_agnumber_t agno
,
1140 struct xfs_buf
**bpp
)
1145 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1146 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1147 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1148 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_sb_buf_ops
);
1151 xfs_buf_set_ref(bp
, XFS_SSB_REF
);
1156 /* Get an uninitialised secondary superblock buffer. */
1158 xfs_sb_get_secondary(
1159 struct xfs_mount
*mp
,
1160 struct xfs_trans
*tp
,
1161 xfs_agnumber_t agno
,
1162 struct xfs_buf
**bpp
)
1167 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1168 error
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1169 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1170 XFS_FSS_TO_BB(mp
, 1), 0, &bp
);
1173 bp
->b_ops
= &xfs_sb_buf_ops
;
1174 xfs_buf_oneshot(bp
);