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"
13 #include "xfs_mount.h"
14 #include "xfs_ialloc.h"
15 #include "xfs_alloc.h"
16 #include "xfs_trace.h"
17 #include "xfs_trans.h"
18 #include "xfs_bmap_btree.h"
19 #include "xfs_alloc_btree.h"
20 #include "xfs_rmap_btree.h"
21 #include "xfs_refcount_btree.h"
22 #include "xfs_da_format.h"
23 #include "xfs_health.h"
26 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
30 * Reference counting access wrappers to the perag structures.
31 * Because we never free per-ag structures, the only thing we
32 * have to protect against changes is the tree structure itself.
39 struct xfs_perag
*pag
;
43 pag
= radix_tree_lookup(&mp
->m_perag_tree
, agno
);
45 ASSERT(atomic_read(&pag
->pag_ref
) >= 0);
46 ref
= atomic_inc_return(&pag
->pag_ref
);
49 trace_xfs_perag_get(mp
, agno
, ref
, _RET_IP_
);
54 * search from @first to find the next perag with the given tag set.
62 struct xfs_perag
*pag
;
67 found
= radix_tree_gang_lookup_tag(&mp
->m_perag_tree
,
68 (void **)&pag
, first
, 1, tag
);
73 ref
= atomic_inc_return(&pag
->pag_ref
);
75 trace_xfs_perag_get_tag(mp
, pag
->pag_agno
, ref
, _RET_IP_
);
81 struct xfs_perag
*pag
)
85 ASSERT(atomic_read(&pag
->pag_ref
) > 0);
86 ref
= atomic_dec_return(&pag
->pag_ref
);
87 trace_xfs_perag_put(pag
->pag_mount
, pag
->pag_agno
, ref
, _RET_IP_
);
90 /* Check all the superblock fields we care about when reading one in. */
96 if (XFS_SB_VERSION_NUM(sbp
) != XFS_SB_VERSION_5
)
100 * Version 5 superblock feature mask validation. Reject combinations
101 * the kernel cannot support up front before checking anything else.
103 if (xfs_sb_has_compat_feature(sbp
, XFS_SB_FEAT_COMPAT_UNKNOWN
)) {
105 "Superblock has unknown compatible features (0x%x) enabled.",
106 (sbp
->sb_features_compat
& XFS_SB_FEAT_COMPAT_UNKNOWN
));
108 "Using a more recent kernel is recommended.");
111 if (xfs_sb_has_ro_compat_feature(sbp
, XFS_SB_FEAT_RO_COMPAT_UNKNOWN
)) {
113 "Superblock has unknown read-only compatible features (0x%x) enabled.",
114 (sbp
->sb_features_ro_compat
&
115 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
));
116 if (!(mp
->m_flags
& XFS_MOUNT_RDONLY
)) {
118 "Attempted to mount read-only compatible filesystem read-write.");
120 "Filesystem can only be safely mounted read only.");
125 if (xfs_sb_has_incompat_feature(sbp
, XFS_SB_FEAT_INCOMPAT_UNKNOWN
)) {
127 "Superblock has unknown incompatible features (0x%x) enabled.",
128 (sbp
->sb_features_incompat
&
129 XFS_SB_FEAT_INCOMPAT_UNKNOWN
));
131 "Filesystem cannot be safely mounted by this kernel.");
138 /* Check all the superblock fields we care about when writing one out. */
140 xfs_validate_sb_write(
141 struct xfs_mount
*mp
,
146 * Carry out additional sb summary counter sanity checks when we write
147 * the superblock. We skip this in the read validator because there
148 * could be newer superblocks in the log and if the values are garbage
149 * even after replay we'll recalculate them at the end of log mount.
151 * mkfs has traditionally written zeroed counters to inprogress and
152 * secondary superblocks, so allow this usage to continue because
153 * we never read counters from such superblocks.
155 if (XFS_BUF_ADDR(bp
) == XFS_SB_DADDR
&& !sbp
->sb_inprogress
&&
156 (sbp
->sb_fdblocks
> sbp
->sb_dblocks
||
157 !xfs_verify_icount(mp
, sbp
->sb_icount
) ||
158 sbp
->sb_ifree
> sbp
->sb_icount
)) {
159 xfs_warn(mp
, "SB summary counter sanity check failed");
160 return -EFSCORRUPTED
;
163 if (XFS_SB_VERSION_NUM(sbp
) != XFS_SB_VERSION_5
)
167 * Version 5 superblock feature mask validation. Reject combinations
168 * the kernel cannot support since we checked for unsupported bits in
169 * the read verifier, which means that memory is corrupt.
171 if (xfs_sb_has_compat_feature(sbp
, XFS_SB_FEAT_COMPAT_UNKNOWN
)) {
173 "Corruption detected in superblock compatible features (0x%x)!",
174 (sbp
->sb_features_compat
& XFS_SB_FEAT_COMPAT_UNKNOWN
));
175 return -EFSCORRUPTED
;
178 if (xfs_sb_has_ro_compat_feature(sbp
, XFS_SB_FEAT_RO_COMPAT_UNKNOWN
)) {
180 "Corruption detected in superblock read-only compatible features (0x%x)!",
181 (sbp
->sb_features_ro_compat
&
182 XFS_SB_FEAT_RO_COMPAT_UNKNOWN
));
183 return -EFSCORRUPTED
;
185 if (xfs_sb_has_incompat_feature(sbp
, XFS_SB_FEAT_INCOMPAT_UNKNOWN
)) {
187 "Corruption detected in superblock incompatible features (0x%x)!",
188 (sbp
->sb_features_incompat
&
189 XFS_SB_FEAT_INCOMPAT_UNKNOWN
));
190 return -EFSCORRUPTED
;
192 if (xfs_sb_has_incompat_log_feature(sbp
,
193 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
)) {
195 "Corruption detected in superblock incompatible log features (0x%x)!",
196 (sbp
->sb_features_log_incompat
&
197 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
));
198 return -EFSCORRUPTED
;
202 * We can't read verify the sb LSN because the read verifier is called
203 * before the log is allocated and processed. We know the log is set up
204 * before write verifier calls, so check it here.
206 if (!xfs_log_check_lsn(mp
, sbp
->sb_lsn
))
207 return -EFSCORRUPTED
;
212 /* Check the validity of the SB. */
214 xfs_validate_sb_common(
215 struct xfs_mount
*mp
,
219 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
220 uint32_t agcount
= 0;
223 if (!xfs_verify_magic(bp
, dsb
->sb_magicnum
)) {
224 xfs_warn(mp
, "bad magic number");
228 if (!xfs_sb_good_version(sbp
)) {
229 xfs_warn(mp
, "bad version");
233 if (xfs_sb_version_has_pquotino(sbp
)) {
234 if (sbp
->sb_qflags
& (XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
)) {
236 "Version 5 of Super block has XFS_OQUOTA bits.");
237 return -EFSCORRUPTED
;
239 } else if (sbp
->sb_qflags
& (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
|
240 XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
)) {
242 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
243 return -EFSCORRUPTED
;
247 * Full inode chunks must be aligned to inode chunk size when
248 * sparse inodes are enabled to support the sparse chunk
249 * allocation algorithm and prevent overlapping inode records.
251 if (xfs_sb_version_hassparseinodes(sbp
)) {
254 align
= XFS_INODES_PER_CHUNK
* sbp
->sb_inodesize
256 if (sbp
->sb_inoalignmt
!= align
) {
258 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
259 sbp
->sb_inoalignmt
, align
);
265 sbp
->sb_logstart
== 0 && mp
->m_logdev_targp
== mp
->m_ddev_targp
)) {
267 "filesystem is marked as having an external log; "
268 "specify logdev on the mount command line.");
273 sbp
->sb_logstart
!= 0 && mp
->m_logdev_targp
!= mp
->m_ddev_targp
)) {
275 "filesystem is marked as having an internal log; "
276 "do not specify logdev on the mount command line.");
280 /* Compute agcount for this number of dblocks and agblocks */
281 if (sbp
->sb_agblocks
) {
282 agcount
= div_u64_rem(sbp
->sb_dblocks
, sbp
->sb_agblocks
, &rem
);
288 * More sanity checking. Most of these were stolen directly from
292 sbp
->sb_agcount
<= 0 ||
293 sbp
->sb_sectsize
< XFS_MIN_SECTORSIZE
||
294 sbp
->sb_sectsize
> XFS_MAX_SECTORSIZE
||
295 sbp
->sb_sectlog
< XFS_MIN_SECTORSIZE_LOG
||
296 sbp
->sb_sectlog
> XFS_MAX_SECTORSIZE_LOG
||
297 sbp
->sb_sectsize
!= (1 << sbp
->sb_sectlog
) ||
298 sbp
->sb_blocksize
< XFS_MIN_BLOCKSIZE
||
299 sbp
->sb_blocksize
> XFS_MAX_BLOCKSIZE
||
300 sbp
->sb_blocklog
< XFS_MIN_BLOCKSIZE_LOG
||
301 sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
302 sbp
->sb_blocksize
!= (1 << sbp
->sb_blocklog
) ||
303 sbp
->sb_dirblklog
+ sbp
->sb_blocklog
> XFS_MAX_BLOCKSIZE_LOG
||
304 sbp
->sb_inodesize
< XFS_DINODE_MIN_SIZE
||
305 sbp
->sb_inodesize
> XFS_DINODE_MAX_SIZE
||
306 sbp
->sb_inodelog
< XFS_DINODE_MIN_LOG
||
307 sbp
->sb_inodelog
> XFS_DINODE_MAX_LOG
||
308 sbp
->sb_inodesize
!= (1 << sbp
->sb_inodelog
) ||
309 sbp
->sb_logsunit
> XLOG_MAX_RECORD_BSIZE
||
310 sbp
->sb_inopblock
!= howmany(sbp
->sb_blocksize
,sbp
->sb_inodesize
) ||
311 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) < XFS_MIN_AG_BYTES
||
312 XFS_FSB_TO_B(mp
, sbp
->sb_agblocks
) > XFS_MAX_AG_BYTES
||
313 sbp
->sb_agblklog
!= xfs_highbit32(sbp
->sb_agblocks
- 1) + 1 ||
314 agcount
== 0 || agcount
!= sbp
->sb_agcount
||
315 (sbp
->sb_blocklog
- sbp
->sb_inodelog
!= sbp
->sb_inopblog
) ||
316 (sbp
->sb_rextsize
* sbp
->sb_blocksize
> XFS_MAX_RTEXTSIZE
) ||
317 (sbp
->sb_rextsize
* sbp
->sb_blocksize
< XFS_MIN_RTEXTSIZE
) ||
318 (sbp
->sb_imax_pct
> 100 /* zero sb_imax_pct is valid */) ||
319 sbp
->sb_dblocks
== 0 ||
320 sbp
->sb_dblocks
> XFS_MAX_DBLOCKS(sbp
) ||
321 sbp
->sb_dblocks
< XFS_MIN_DBLOCKS(sbp
) ||
322 sbp
->sb_shared_vn
!= 0)) {
323 xfs_notice(mp
, "SB sanity check failed");
324 return -EFSCORRUPTED
;
328 if (!xfs_sb_version_hasdalign(sbp
) ||
329 sbp
->sb_unit
> sbp
->sb_width
||
330 (sbp
->sb_width
% sbp
->sb_unit
) != 0) {
331 xfs_notice(mp
, "SB stripe unit sanity check failed");
332 return -EFSCORRUPTED
;
334 } else if (xfs_sb_version_hasdalign(sbp
)) {
335 xfs_notice(mp
, "SB stripe alignment sanity check failed");
336 return -EFSCORRUPTED
;
337 } else if (sbp
->sb_width
) {
338 xfs_notice(mp
, "SB stripe width sanity check failed");
339 return -EFSCORRUPTED
;
343 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
344 sbp
->sb_blocksize
< XFS_MIN_CRC_BLOCKSIZE
) {
345 xfs_notice(mp
, "v5 SB sanity check failed");
346 return -EFSCORRUPTED
;
350 * Currently only very few inode sizes are supported.
352 switch (sbp
->sb_inodesize
) {
359 xfs_warn(mp
, "inode size of %d bytes not supported",
364 if (xfs_sb_validate_fsb_count(sbp
, sbp
->sb_dblocks
) ||
365 xfs_sb_validate_fsb_count(sbp
, sbp
->sb_rblocks
)) {
367 "file system too large to be mounted on this system.");
375 xfs_sb_quota_from_disk(struct xfs_sb
*sbp
)
378 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
379 * leads to in-core values having two different values for a quota
380 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
383 * Note that this change affect only the in-core values. These
384 * values are not written back to disk unless any quota information
385 * is written to the disk. Even in that case, sb_pquotino field is
386 * not written to disk unless the superblock supports pquotino.
388 if (sbp
->sb_uquotino
== 0)
389 sbp
->sb_uquotino
= NULLFSINO
;
390 if (sbp
->sb_gquotino
== 0)
391 sbp
->sb_gquotino
= NULLFSINO
;
392 if (sbp
->sb_pquotino
== 0)
393 sbp
->sb_pquotino
= NULLFSINO
;
396 * We need to do these manipilations only if we are working
397 * with an older version of on-disk superblock.
399 if (xfs_sb_version_has_pquotino(sbp
))
402 if (sbp
->sb_qflags
& XFS_OQUOTA_ENFD
)
403 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
404 XFS_PQUOTA_ENFD
: XFS_GQUOTA_ENFD
;
405 if (sbp
->sb_qflags
& XFS_OQUOTA_CHKD
)
406 sbp
->sb_qflags
|= (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
) ?
407 XFS_PQUOTA_CHKD
: XFS_GQUOTA_CHKD
;
408 sbp
->sb_qflags
&= ~(XFS_OQUOTA_ENFD
| XFS_OQUOTA_CHKD
);
410 if (sbp
->sb_qflags
& XFS_PQUOTA_ACCT
&&
411 sbp
->sb_gquotino
!= NULLFSINO
) {
413 * In older version of superblock, on-disk superblock only
414 * has sb_gquotino, and in-core superblock has both sb_gquotino
415 * and sb_pquotino. But, only one of them is supported at any
416 * point of time. So, if PQUOTA is set in disk superblock,
417 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
418 * above is to make sure we don't do this twice and wipe them
421 sbp
->sb_pquotino
= sbp
->sb_gquotino
;
422 sbp
->sb_gquotino
= NULLFSINO
;
432 to
->sb_magicnum
= be32_to_cpu(from
->sb_magicnum
);
433 to
->sb_blocksize
= be32_to_cpu(from
->sb_blocksize
);
434 to
->sb_dblocks
= be64_to_cpu(from
->sb_dblocks
);
435 to
->sb_rblocks
= be64_to_cpu(from
->sb_rblocks
);
436 to
->sb_rextents
= be64_to_cpu(from
->sb_rextents
);
437 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
438 to
->sb_logstart
= be64_to_cpu(from
->sb_logstart
);
439 to
->sb_rootino
= be64_to_cpu(from
->sb_rootino
);
440 to
->sb_rbmino
= be64_to_cpu(from
->sb_rbmino
);
441 to
->sb_rsumino
= be64_to_cpu(from
->sb_rsumino
);
442 to
->sb_rextsize
= be32_to_cpu(from
->sb_rextsize
);
443 to
->sb_agblocks
= be32_to_cpu(from
->sb_agblocks
);
444 to
->sb_agcount
= be32_to_cpu(from
->sb_agcount
);
445 to
->sb_rbmblocks
= be32_to_cpu(from
->sb_rbmblocks
);
446 to
->sb_logblocks
= be32_to_cpu(from
->sb_logblocks
);
447 to
->sb_versionnum
= be16_to_cpu(from
->sb_versionnum
);
448 to
->sb_sectsize
= be16_to_cpu(from
->sb_sectsize
);
449 to
->sb_inodesize
= be16_to_cpu(from
->sb_inodesize
);
450 to
->sb_inopblock
= be16_to_cpu(from
->sb_inopblock
);
451 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
452 to
->sb_blocklog
= from
->sb_blocklog
;
453 to
->sb_sectlog
= from
->sb_sectlog
;
454 to
->sb_inodelog
= from
->sb_inodelog
;
455 to
->sb_inopblog
= from
->sb_inopblog
;
456 to
->sb_agblklog
= from
->sb_agblklog
;
457 to
->sb_rextslog
= from
->sb_rextslog
;
458 to
->sb_inprogress
= from
->sb_inprogress
;
459 to
->sb_imax_pct
= from
->sb_imax_pct
;
460 to
->sb_icount
= be64_to_cpu(from
->sb_icount
);
461 to
->sb_ifree
= be64_to_cpu(from
->sb_ifree
);
462 to
->sb_fdblocks
= be64_to_cpu(from
->sb_fdblocks
);
463 to
->sb_frextents
= be64_to_cpu(from
->sb_frextents
);
464 to
->sb_uquotino
= be64_to_cpu(from
->sb_uquotino
);
465 to
->sb_gquotino
= be64_to_cpu(from
->sb_gquotino
);
466 to
->sb_qflags
= be16_to_cpu(from
->sb_qflags
);
467 to
->sb_flags
= from
->sb_flags
;
468 to
->sb_shared_vn
= from
->sb_shared_vn
;
469 to
->sb_inoalignmt
= be32_to_cpu(from
->sb_inoalignmt
);
470 to
->sb_unit
= be32_to_cpu(from
->sb_unit
);
471 to
->sb_width
= be32_to_cpu(from
->sb_width
);
472 to
->sb_dirblklog
= from
->sb_dirblklog
;
473 to
->sb_logsectlog
= from
->sb_logsectlog
;
474 to
->sb_logsectsize
= be16_to_cpu(from
->sb_logsectsize
);
475 to
->sb_logsunit
= be32_to_cpu(from
->sb_logsunit
);
476 to
->sb_features2
= be32_to_cpu(from
->sb_features2
);
477 to
->sb_bad_features2
= be32_to_cpu(from
->sb_bad_features2
);
478 to
->sb_features_compat
= be32_to_cpu(from
->sb_features_compat
);
479 to
->sb_features_ro_compat
= be32_to_cpu(from
->sb_features_ro_compat
);
480 to
->sb_features_incompat
= be32_to_cpu(from
->sb_features_incompat
);
481 to
->sb_features_log_incompat
=
482 be32_to_cpu(from
->sb_features_log_incompat
);
483 /* crc is only used on disk, not in memory; just init to 0 here. */
485 to
->sb_spino_align
= be32_to_cpu(from
->sb_spino_align
);
486 to
->sb_pquotino
= be64_to_cpu(from
->sb_pquotino
);
487 to
->sb_lsn
= be64_to_cpu(from
->sb_lsn
);
489 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
490 * feature flag is set; if not set we keep it only in memory.
492 if (xfs_sb_version_hasmetauuid(to
))
493 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
495 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_uuid
);
496 /* Convert on-disk flags to in-memory flags? */
498 xfs_sb_quota_from_disk(to
);
506 __xfs_sb_from_disk(to
, from
, true);
510 xfs_sb_quota_to_disk(
514 uint16_t qflags
= from
->sb_qflags
;
516 to
->sb_uquotino
= cpu_to_be64(from
->sb_uquotino
);
517 if (xfs_sb_version_has_pquotino(from
)) {
518 to
->sb_qflags
= cpu_to_be16(from
->sb_qflags
);
519 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
520 to
->sb_pquotino
= cpu_to_be64(from
->sb_pquotino
);
525 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
526 * flags, whereas the on-disk version does. So, convert incore
527 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
529 qflags
&= ~(XFS_PQUOTA_ENFD
| XFS_PQUOTA_CHKD
|
530 XFS_GQUOTA_ENFD
| XFS_GQUOTA_CHKD
);
532 if (from
->sb_qflags
&
533 (XFS_PQUOTA_ENFD
| XFS_GQUOTA_ENFD
))
534 qflags
|= XFS_OQUOTA_ENFD
;
535 if (from
->sb_qflags
&
536 (XFS_PQUOTA_CHKD
| XFS_GQUOTA_CHKD
))
537 qflags
|= XFS_OQUOTA_CHKD
;
538 to
->sb_qflags
= cpu_to_be16(qflags
);
541 * GQUOTINO and PQUOTINO cannot be used together in versions
542 * of superblock that do not have pquotino. from->sb_flags
543 * tells us which quota is active and should be copied to
544 * disk. If neither are active, we should NULL the inode.
546 * In all cases, the separate pquotino must remain 0 because it
547 * it beyond the "end" of the valid non-pquotino superblock.
549 if (from
->sb_qflags
& XFS_GQUOTA_ACCT
)
550 to
->sb_gquotino
= cpu_to_be64(from
->sb_gquotino
);
551 else if (from
->sb_qflags
& XFS_PQUOTA_ACCT
)
552 to
->sb_gquotino
= cpu_to_be64(from
->sb_pquotino
);
555 * We can't rely on just the fields being logged to tell us
556 * that it is safe to write NULLFSINO - we should only do that
557 * if quotas are not actually enabled. Hence only write
558 * NULLFSINO if both in-core quota inodes are NULL.
560 if (from
->sb_gquotino
== NULLFSINO
&&
561 from
->sb_pquotino
== NULLFSINO
)
562 to
->sb_gquotino
= cpu_to_be64(NULLFSINO
);
573 xfs_sb_quota_to_disk(to
, from
);
575 to
->sb_magicnum
= cpu_to_be32(from
->sb_magicnum
);
576 to
->sb_blocksize
= cpu_to_be32(from
->sb_blocksize
);
577 to
->sb_dblocks
= cpu_to_be64(from
->sb_dblocks
);
578 to
->sb_rblocks
= cpu_to_be64(from
->sb_rblocks
);
579 to
->sb_rextents
= cpu_to_be64(from
->sb_rextents
);
580 memcpy(&to
->sb_uuid
, &from
->sb_uuid
, sizeof(to
->sb_uuid
));
581 to
->sb_logstart
= cpu_to_be64(from
->sb_logstart
);
582 to
->sb_rootino
= cpu_to_be64(from
->sb_rootino
);
583 to
->sb_rbmino
= cpu_to_be64(from
->sb_rbmino
);
584 to
->sb_rsumino
= cpu_to_be64(from
->sb_rsumino
);
585 to
->sb_rextsize
= cpu_to_be32(from
->sb_rextsize
);
586 to
->sb_agblocks
= cpu_to_be32(from
->sb_agblocks
);
587 to
->sb_agcount
= cpu_to_be32(from
->sb_agcount
);
588 to
->sb_rbmblocks
= cpu_to_be32(from
->sb_rbmblocks
);
589 to
->sb_logblocks
= cpu_to_be32(from
->sb_logblocks
);
590 to
->sb_versionnum
= cpu_to_be16(from
->sb_versionnum
);
591 to
->sb_sectsize
= cpu_to_be16(from
->sb_sectsize
);
592 to
->sb_inodesize
= cpu_to_be16(from
->sb_inodesize
);
593 to
->sb_inopblock
= cpu_to_be16(from
->sb_inopblock
);
594 memcpy(&to
->sb_fname
, &from
->sb_fname
, sizeof(to
->sb_fname
));
595 to
->sb_blocklog
= from
->sb_blocklog
;
596 to
->sb_sectlog
= from
->sb_sectlog
;
597 to
->sb_inodelog
= from
->sb_inodelog
;
598 to
->sb_inopblog
= from
->sb_inopblog
;
599 to
->sb_agblklog
= from
->sb_agblklog
;
600 to
->sb_rextslog
= from
->sb_rextslog
;
601 to
->sb_inprogress
= from
->sb_inprogress
;
602 to
->sb_imax_pct
= from
->sb_imax_pct
;
603 to
->sb_icount
= cpu_to_be64(from
->sb_icount
);
604 to
->sb_ifree
= cpu_to_be64(from
->sb_ifree
);
605 to
->sb_fdblocks
= cpu_to_be64(from
->sb_fdblocks
);
606 to
->sb_frextents
= cpu_to_be64(from
->sb_frextents
);
608 to
->sb_flags
= from
->sb_flags
;
609 to
->sb_shared_vn
= from
->sb_shared_vn
;
610 to
->sb_inoalignmt
= cpu_to_be32(from
->sb_inoalignmt
);
611 to
->sb_unit
= cpu_to_be32(from
->sb_unit
);
612 to
->sb_width
= cpu_to_be32(from
->sb_width
);
613 to
->sb_dirblklog
= from
->sb_dirblklog
;
614 to
->sb_logsectlog
= from
->sb_logsectlog
;
615 to
->sb_logsectsize
= cpu_to_be16(from
->sb_logsectsize
);
616 to
->sb_logsunit
= cpu_to_be32(from
->sb_logsunit
);
619 * We need to ensure that bad_features2 always matches features2.
620 * Hence we enforce that here rather than having to remember to do it
621 * everywhere else that updates features2.
623 from
->sb_bad_features2
= from
->sb_features2
;
624 to
->sb_features2
= cpu_to_be32(from
->sb_features2
);
625 to
->sb_bad_features2
= cpu_to_be32(from
->sb_bad_features2
);
627 if (xfs_sb_version_hascrc(from
)) {
628 to
->sb_features_compat
= cpu_to_be32(from
->sb_features_compat
);
629 to
->sb_features_ro_compat
=
630 cpu_to_be32(from
->sb_features_ro_compat
);
631 to
->sb_features_incompat
=
632 cpu_to_be32(from
->sb_features_incompat
);
633 to
->sb_features_log_incompat
=
634 cpu_to_be32(from
->sb_features_log_incompat
);
635 to
->sb_spino_align
= cpu_to_be32(from
->sb_spino_align
);
636 to
->sb_lsn
= cpu_to_be64(from
->sb_lsn
);
637 if (xfs_sb_version_hasmetauuid(from
))
638 uuid_copy(&to
->sb_meta_uuid
, &from
->sb_meta_uuid
);
643 * If the superblock has the CRC feature bit set or the CRC field is non-null,
644 * check that the CRC is valid. We check the CRC field is non-null because a
645 * single bit error could clear the feature bit and unused parts of the
646 * superblock are supposed to be zero. Hence a non-null crc field indicates that
647 * we've potentially lost a feature bit and we should check it anyway.
649 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
650 * last field in V4 secondary superblocks. So for secondary superblocks,
651 * we are more forgiving, and ignore CRC failures if the primary doesn't
652 * indicate that the fs version is V5.
659 struct xfs_mount
*mp
= bp
->b_mount
;
660 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
664 * open code the version check to avoid needing to convert the entire
665 * superblock from disk order just to check the version number
667 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
) &&
668 (((be16_to_cpu(dsb
->sb_versionnum
) & XFS_SB_VERSION_NUMBITS
) ==
672 if (!xfs_buf_verify_cksum(bp
, XFS_SB_CRC_OFF
)) {
673 /* Only fail bad secondaries on a known V5 filesystem */
674 if (bp
->b_bn
== XFS_SB_DADDR
||
675 xfs_sb_version_hascrc(&mp
->m_sb
)) {
683 * Check all the superblock fields. Don't byteswap the xquota flags
684 * because _verify_common checks the on-disk values.
686 __xfs_sb_from_disk(&sb
, XFS_BUF_TO_SBP(bp
), false);
687 error
= xfs_validate_sb_common(mp
, bp
, &sb
);
690 error
= xfs_validate_sb_read(mp
, &sb
);
693 if (error
== -EFSCORRUPTED
|| error
== -EFSBADCRC
)
694 xfs_verifier_error(bp
, error
, __this_address
);
696 xfs_buf_ioerror(bp
, error
);
700 * We may be probed for a filesystem match, so we may not want to emit
701 * messages when the superblock buffer is not actually an XFS superblock.
702 * If we find an XFS superblock, then run a normal, noisy mount because we are
703 * really going to mount it and want to know about errors.
706 xfs_sb_quiet_read_verify(
709 struct xfs_dsb
*dsb
= XFS_BUF_TO_SBP(bp
);
711 if (dsb
->sb_magicnum
== cpu_to_be32(XFS_SB_MAGIC
)) {
712 /* XFS filesystem, verify noisily! */
713 xfs_sb_read_verify(bp
);
717 xfs_buf_ioerror(bp
, -EWRONGFS
);
725 struct xfs_mount
*mp
= bp
->b_mount
;
726 struct xfs_buf_log_item
*bip
= bp
->b_log_item
;
730 * Check all the superblock fields. Don't byteswap the xquota flags
731 * because _verify_common checks the on-disk values.
733 __xfs_sb_from_disk(&sb
, XFS_BUF_TO_SBP(bp
), false);
734 error
= xfs_validate_sb_common(mp
, bp
, &sb
);
737 error
= xfs_validate_sb_write(mp
, bp
, &sb
);
741 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
745 XFS_BUF_TO_SBP(bp
)->sb_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
747 xfs_buf_update_cksum(bp
, XFS_SB_CRC_OFF
);
751 xfs_verifier_error(bp
, error
, __this_address
);
754 const struct xfs_buf_ops xfs_sb_buf_ops
= {
756 .magic
= { cpu_to_be32(XFS_SB_MAGIC
), cpu_to_be32(XFS_SB_MAGIC
) },
757 .verify_read
= xfs_sb_read_verify
,
758 .verify_write
= xfs_sb_write_verify
,
761 const struct xfs_buf_ops xfs_sb_quiet_buf_ops
= {
762 .name
= "xfs_sb_quiet",
763 .magic
= { cpu_to_be32(XFS_SB_MAGIC
), cpu_to_be32(XFS_SB_MAGIC
) },
764 .verify_read
= xfs_sb_quiet_read_verify
,
765 .verify_write
= xfs_sb_write_verify
,
771 * Mount initialization code establishing various mount
772 * fields from the superblock associated with the given
775 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
779 struct xfs_mount
*mp
,
782 mp
->m_agfrotor
= mp
->m_agirotor
= 0;
783 mp
->m_maxagi
= mp
->m_sb
.sb_agcount
;
784 mp
->m_blkbit_log
= sbp
->sb_blocklog
+ XFS_NBBYLOG
;
785 mp
->m_blkbb_log
= sbp
->sb_blocklog
- BBSHIFT
;
786 mp
->m_sectbb_log
= sbp
->sb_sectlog
- BBSHIFT
;
787 mp
->m_agno_log
= xfs_highbit32(sbp
->sb_agcount
- 1) + 1;
788 mp
->m_blockmask
= sbp
->sb_blocksize
- 1;
789 mp
->m_blockwsize
= sbp
->sb_blocksize
>> XFS_WORDLOG
;
790 mp
->m_blockwmask
= mp
->m_blockwsize
- 1;
792 mp
->m_alloc_mxr
[0] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
793 mp
->m_alloc_mxr
[1] = xfs_allocbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
794 mp
->m_alloc_mnr
[0] = mp
->m_alloc_mxr
[0] / 2;
795 mp
->m_alloc_mnr
[1] = mp
->m_alloc_mxr
[1] / 2;
797 mp
->m_bmap_dmxr
[0] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 1);
798 mp
->m_bmap_dmxr
[1] = xfs_bmbt_maxrecs(mp
, sbp
->sb_blocksize
, 0);
799 mp
->m_bmap_dmnr
[0] = mp
->m_bmap_dmxr
[0] / 2;
800 mp
->m_bmap_dmnr
[1] = mp
->m_bmap_dmxr
[1] / 2;
802 mp
->m_rmap_mxr
[0] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 1);
803 mp
->m_rmap_mxr
[1] = xfs_rmapbt_maxrecs(sbp
->sb_blocksize
, 0);
804 mp
->m_rmap_mnr
[0] = mp
->m_rmap_mxr
[0] / 2;
805 mp
->m_rmap_mnr
[1] = mp
->m_rmap_mxr
[1] / 2;
807 mp
->m_refc_mxr
[0] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, true);
808 mp
->m_refc_mxr
[1] = xfs_refcountbt_maxrecs(sbp
->sb_blocksize
, false);
809 mp
->m_refc_mnr
[0] = mp
->m_refc_mxr
[0] / 2;
810 mp
->m_refc_mnr
[1] = mp
->m_refc_mxr
[1] / 2;
812 mp
->m_bsize
= XFS_FSB_TO_BB(mp
, 1);
813 mp
->m_alloc_set_aside
= xfs_alloc_set_aside(mp
);
814 mp
->m_ag_max_usable
= xfs_alloc_ag_max_usable(mp
);
818 * xfs_initialize_perag_data
820 * Read in each per-ag structure so we can count up the number of
821 * allocated inodes, free inodes and used filesystem blocks as this
822 * information is no longer persistent in the superblock. Once we have
823 * this information, write it into the in-core superblock structure.
826 xfs_initialize_perag_data(
827 struct xfs_mount
*mp
,
828 xfs_agnumber_t agcount
)
830 xfs_agnumber_t index
;
832 xfs_sb_t
*sbp
= &mp
->m_sb
;
836 uint64_t bfreelst
= 0;
841 for (index
= 0; index
< agcount
; index
++) {
843 * read the agf, then the agi. This gets us
844 * all the information we need and populates the
845 * per-ag structures for us.
847 error
= xfs_alloc_pagf_init(mp
, NULL
, index
, 0);
851 error
= xfs_ialloc_pagi_init(mp
, NULL
, index
);
854 pag
= xfs_perag_get(mp
, index
);
855 ifree
+= pag
->pagi_freecount
;
856 ialloc
+= pag
->pagi_count
;
857 bfree
+= pag
->pagf_freeblks
;
858 bfreelst
+= pag
->pagf_flcount
;
859 btree
+= pag
->pagf_btreeblks
;
862 fdblocks
= bfree
+ bfreelst
+ btree
;
865 * If the new summary counts are obviously incorrect, fail the
866 * mount operation because that implies the AGFs are also corrupt.
867 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
868 * will prevent xfs_repair from fixing anything.
870 if (fdblocks
> sbp
->sb_dblocks
|| ifree
> ialloc
) {
871 xfs_alert(mp
, "AGF corruption. Please run xfs_repair.");
872 error
= -EFSCORRUPTED
;
876 /* Overwrite incore superblock counters with just-read data */
877 spin_lock(&mp
->m_sb_lock
);
878 sbp
->sb_ifree
= ifree
;
879 sbp
->sb_icount
= ialloc
;
880 sbp
->sb_fdblocks
= fdblocks
;
881 spin_unlock(&mp
->m_sb_lock
);
883 xfs_reinit_percpu_counters(mp
);
885 xfs_fs_mark_healthy(mp
, XFS_SICK_FS_COUNTERS
);
890 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
891 * into the superblock buffer to be logged. It does not provide the higher
892 * level of locking that is needed to protect the in-core superblock from
897 struct xfs_trans
*tp
)
899 struct xfs_mount
*mp
= tp
->t_mountp
;
900 struct xfs_buf
*bp
= xfs_trans_getsb(tp
, mp
);
902 mp
->m_sb
.sb_icount
= percpu_counter_sum(&mp
->m_icount
);
903 mp
->m_sb
.sb_ifree
= percpu_counter_sum(&mp
->m_ifree
);
904 mp
->m_sb
.sb_fdblocks
= percpu_counter_sum(&mp
->m_fdblocks
);
906 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
907 xfs_trans_buf_set_type(tp
, bp
, XFS_BLFT_SB_BUF
);
908 xfs_trans_log_buf(tp
, bp
, 0, sizeof(struct xfs_dsb
));
914 * Sync the superblock to disk.
916 * Note that the caller is responsible for checking the frozen state of the
917 * filesystem. This procedure uses the non-blocking transaction allocator and
918 * thus will allow modifications to a frozen fs. This is required because this
919 * code can be called during the process of freezing where use of the high-level
920 * allocator would deadlock.
924 struct xfs_mount
*mp
,
927 struct xfs_trans
*tp
;
930 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0,
931 XFS_TRANS_NO_WRITECOUNT
, &tp
);
937 xfs_trans_set_sync(tp
);
938 return xfs_trans_commit(tp
);
942 * Update all the secondary superblocks to match the new state of the primary.
943 * Because we are completely overwriting all the existing fields in the
944 * secondary superblock buffers, there is no need to read them in from disk.
945 * Just get a new buffer, stamp it and write it.
947 * The sb buffers need to be cached here so that we serialise against other
948 * operations that access the secondary superblocks, but we don't want to keep
949 * them in memory once it is written so we mark it as a one-shot buffer.
952 xfs_update_secondary_sbs(
953 struct xfs_mount
*mp
)
958 LIST_HEAD (buffer_list
);
960 /* update secondary superblocks. */
961 for (agno
= 1; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
964 bp
= xfs_buf_get(mp
->m_ddev_targp
,
965 XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
966 XFS_FSS_TO_BB(mp
, 1));
968 * If we get an error reading or writing alternate superblocks,
969 * continue. xfs_repair chooses the "best" superblock based
970 * on most matches; if we break early, we'll leave more
971 * superblocks un-updated than updated, and xfs_repair may
972 * pick them over the properly-updated primary.
976 "error allocating secondary superblock for ag %d",
979 saved_error
= -ENOMEM
;
983 bp
->b_ops
= &xfs_sb_buf_ops
;
985 xfs_buf_zero(bp
, 0, BBTOB(bp
->b_length
));
986 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp
), &mp
->m_sb
);
987 xfs_buf_delwri_queue(bp
, &buffer_list
);
990 /* don't hold too many buffers at once */
994 error
= xfs_buf_delwri_submit(&buffer_list
);
997 "write error %d updating a secondary superblock near ag %d",
1000 saved_error
= error
;
1004 error
= xfs_buf_delwri_submit(&buffer_list
);
1007 "write error %d updating a secondary superblock near ag %d",
1011 return saved_error
? saved_error
: error
;
1015 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1016 * also writes the superblock buffer to disk sector 0 immediately.
1020 struct xfs_mount
*mp
)
1022 struct xfs_trans
*tp
;
1026 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_sb
, 0, 0, 0, &tp
);
1030 bp
= xfs_trans_getsb(tp
, mp
);
1032 xfs_trans_bhold(tp
, bp
);
1033 xfs_trans_set_sync(tp
);
1034 error
= xfs_trans_commit(tp
);
1038 * write out the sb buffer to get the changes to disk
1040 error
= xfs_bwrite(bp
);
1049 struct xfs_fsop_geom
*geo
,
1052 memset(geo
, 0, sizeof(struct xfs_fsop_geom
));
1054 geo
->blocksize
= sbp
->sb_blocksize
;
1055 geo
->rtextsize
= sbp
->sb_rextsize
;
1056 geo
->agblocks
= sbp
->sb_agblocks
;
1057 geo
->agcount
= sbp
->sb_agcount
;
1058 geo
->logblocks
= sbp
->sb_logblocks
;
1059 geo
->sectsize
= sbp
->sb_sectsize
;
1060 geo
->inodesize
= sbp
->sb_inodesize
;
1061 geo
->imaxpct
= sbp
->sb_imax_pct
;
1062 geo
->datablocks
= sbp
->sb_dblocks
;
1063 geo
->rtblocks
= sbp
->sb_rblocks
;
1064 geo
->rtextents
= sbp
->sb_rextents
;
1065 geo
->logstart
= sbp
->sb_logstart
;
1066 BUILD_BUG_ON(sizeof(geo
->uuid
) != sizeof(sbp
->sb_uuid
));
1067 memcpy(geo
->uuid
, &sbp
->sb_uuid
, sizeof(sbp
->sb_uuid
));
1069 if (struct_version
< 2)
1072 geo
->sunit
= sbp
->sb_unit
;
1073 geo
->swidth
= sbp
->sb_width
;
1075 if (struct_version
< 3)
1078 geo
->version
= XFS_FSOP_GEOM_VERSION
;
1079 geo
->flags
= XFS_FSOP_GEOM_FLAGS_NLINK
|
1080 XFS_FSOP_GEOM_FLAGS_DIRV2
|
1081 XFS_FSOP_GEOM_FLAGS_EXTFLG
;
1082 if (xfs_sb_version_hasattr(sbp
))
1083 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR
;
1084 if (xfs_sb_version_hasquota(sbp
))
1085 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_QUOTA
;
1086 if (xfs_sb_version_hasalign(sbp
))
1087 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_IALIGN
;
1088 if (xfs_sb_version_hasdalign(sbp
))
1089 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DALIGN
;
1090 if (xfs_sb_version_hassector(sbp
))
1091 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SECTOR
;
1092 if (xfs_sb_version_hasasciici(sbp
))
1093 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_DIRV2CI
;
1094 if (xfs_sb_version_haslazysbcount(sbp
))
1095 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LAZYSB
;
1096 if (xfs_sb_version_hasattr2(sbp
))
1097 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_ATTR2
;
1098 if (xfs_sb_version_hasprojid32bit(sbp
))
1099 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_PROJID32
;
1100 if (xfs_sb_version_hascrc(sbp
))
1101 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_V5SB
;
1102 if (xfs_sb_version_hasftype(sbp
))
1103 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FTYPE
;
1104 if (xfs_sb_version_hasfinobt(sbp
))
1105 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_FINOBT
;
1106 if (xfs_sb_version_hassparseinodes(sbp
))
1107 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_SPINODES
;
1108 if (xfs_sb_version_hasrmapbt(sbp
))
1109 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_RMAPBT
;
1110 if (xfs_sb_version_hasreflink(sbp
))
1111 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_REFLINK
;
1112 if (xfs_sb_version_hassector(sbp
))
1113 geo
->logsectsize
= sbp
->sb_logsectsize
;
1115 geo
->logsectsize
= BBSIZE
;
1116 geo
->rtsectsize
= sbp
->sb_blocksize
;
1117 geo
->dirblocksize
= xfs_dir2_dirblock_bytes(sbp
);
1119 if (struct_version
< 4)
1122 if (xfs_sb_version_haslogv2(sbp
))
1123 geo
->flags
|= XFS_FSOP_GEOM_FLAGS_LOGV2
;
1125 geo
->logsunit
= sbp
->sb_logsunit
;
1127 if (struct_version
< 5)
1130 geo
->version
= XFS_FSOP_GEOM_VERSION_V5
;
1133 /* Read a secondary superblock. */
1135 xfs_sb_read_secondary(
1136 struct xfs_mount
*mp
,
1137 struct xfs_trans
*tp
,
1138 xfs_agnumber_t agno
,
1139 struct xfs_buf
**bpp
)
1144 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1145 error
= xfs_trans_read_buf(mp
, tp
, mp
->m_ddev_targp
,
1146 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1147 XFS_FSS_TO_BB(mp
, 1), 0, &bp
, &xfs_sb_buf_ops
);
1150 xfs_buf_set_ref(bp
, XFS_SSB_REF
);
1155 /* Get an uninitialised secondary superblock buffer. */
1157 xfs_sb_get_secondary(
1158 struct xfs_mount
*mp
,
1159 struct xfs_trans
*tp
,
1160 xfs_agnumber_t agno
,
1161 struct xfs_buf
**bpp
)
1165 ASSERT(agno
!= 0 && agno
!= NULLAGNUMBER
);
1166 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1167 XFS_AG_DADDR(mp
, agno
, XFS_SB_BLOCK(mp
)),
1168 XFS_FSS_TO_BB(mp
, 1), 0);
1171 bp
->b_ops
= &xfs_sb_buf_ops
;
1172 xfs_buf_oneshot(bp
);