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_defer.h"
16 #include "xfs_inode.h"
17 #include "xfs_ialloc.h"
18 #include "xfs_alloc.h"
19 #include "xfs_error.h"
20 #include "xfs_trace.h"
21 #include "xfs_cksum.h"
22 #include "xfs_trans.h"
23 #include "xfs_buf_item.h"
24 #include "xfs_bmap_btree.h"
25 #include "xfs_alloc_btree.h"
26 #include "xfs_ialloc_btree.h"
28 #include "xfs_rmap_btree.h"
30 #include "xfs_refcount_btree.h"
31 #include "xfs_da_format.h"
32 #include "xfs_da_btree.h"
35 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
39 * Reference counting access wrappers to the perag structures.
40 * Because we never free per-ag structures, the only thing we
41 * have to protect against changes is the tree structure itself.
48 struct xfs_perag *pag;
52 pag = radix_tree_lookup(&mp->m_perag_tree, agno);
54 ASSERT(atomic_read(&pag->pag_ref) >= 0);
55 ref = atomic_inc_return(&pag->pag_ref);
58 trace_xfs_perag_get(mp, agno, ref, _RET_IP_);
63 * search from @first to find the next perag with the given tag set.
71 struct xfs_perag *pag;
76 found = radix_tree_gang_lookup_tag(&mp->m_perag_tree,
77 (void **)&pag, first, 1, tag);
82 ref = atomic_inc_return(&pag->pag_ref);
84 trace_xfs_perag_get_tag(mp, pag->pag_agno, ref, _RET_IP_);
90 struct xfs_perag *pag)
94 ASSERT(atomic_read(&pag->pag_ref) > 0);
95 ref = atomic_dec_return(&pag->pag_ref);
96 trace_xfs_perag_put(pag->pag_mount, pag->pag_agno, ref, _RET_IP_);
100 * Check the validity of the SB found.
103 xfs_mount_validate_sb(
106 bool check_inprogress,
109 uint32_t agcount = 0;
112 if (sbp->sb_magicnum != XFS_SB_MAGIC) {
113 xfs_warn(mp, "bad magic number");
118 if (!xfs_sb_good_version(sbp)) {
119 xfs_warn(mp, "bad version");
124 * Version 5 superblock feature mask validation. Reject combinations the
125 * kernel cannot support up front before checking anything else. For
126 * write validation, we don't need to check feature masks.
128 if (check_version && XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) {
129 if (xfs_sb_has_compat_feature(sbp,
130 XFS_SB_FEAT_COMPAT_UNKNOWN)) {
132 "Superblock has unknown compatible features (0x%x) enabled.",
133 (sbp->sb_features_compat &
134 XFS_SB_FEAT_COMPAT_UNKNOWN));
136 "Using a more recent kernel is recommended.");
139 if (xfs_sb_has_ro_compat_feature(sbp,
140 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
142 "Superblock has unknown read-only compatible features (0x%x) enabled.",
143 (sbp->sb_features_ro_compat &
144 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
145 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
147 "Attempted to mount read-only compatible filesystem read-write.");
149 "Filesystem can only be safely mounted read only.");
154 if (xfs_sb_has_incompat_feature(sbp,
155 XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
157 "Superblock has unknown incompatible features (0x%x) enabled.",
158 (sbp->sb_features_incompat &
159 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
161 "Filesystem can not be safely mounted by this kernel.");
164 } else if (xfs_sb_version_hascrc(sbp)) {
166 * We can't read verify the sb LSN because the read verifier is
167 * called before the log is allocated and processed. We know the
168 * log is set up before write verifier (!check_version) calls,
169 * so just check it here.
171 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
172 return -EFSCORRUPTED;
175 if (xfs_sb_version_has_pquotino(sbp)) {
176 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
178 "Version 5 of Super block has XFS_OQUOTA bits.");
179 return -EFSCORRUPTED;
181 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
182 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
184 "Superblock earlier than Version 5 has XFS_[PQ]UOTA_{ENFD|CHKD} bits.");
185 return -EFSCORRUPTED;
189 * Full inode chunks must be aligned to inode chunk size when
190 * sparse inodes are enabled to support the sparse chunk
191 * allocation algorithm and prevent overlapping inode records.
193 if (xfs_sb_version_hassparseinodes(sbp)) {
196 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
198 if (sbp->sb_inoalignmt != align) {
200 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
201 sbp->sb_inoalignmt, align);
207 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
209 "filesystem is marked as having an external log; "
210 "specify logdev on the mount command line.");
215 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
217 "filesystem is marked as having an internal log; "
218 "do not specify logdev on the mount command line.");
222 /* Compute agcount for this number of dblocks and agblocks */
223 if (sbp->sb_agblocks) {
224 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
230 * More sanity checking. Most of these were stolen directly from
234 sbp->sb_agcount <= 0 ||
235 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
236 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
237 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
238 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
239 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
240 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
241 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
242 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
243 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
244 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
245 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
246 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
247 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
248 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
249 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
250 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
251 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
252 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
253 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
254 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
255 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
256 agcount == 0 || agcount != sbp->sb_agcount ||
257 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
258 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
259 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
260 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
261 sbp->sb_dblocks == 0 ||
262 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
263 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
264 sbp->sb_shared_vn != 0)) {
265 xfs_notice(mp, "SB sanity check failed");
266 return -EFSCORRUPTED;
270 if (!xfs_sb_version_hasdalign(sbp) ||
271 sbp->sb_unit > sbp->sb_width ||
272 (sbp->sb_width % sbp->sb_unit) != 0) {
273 xfs_notice(mp, "SB stripe unit sanity check failed");
274 return -EFSCORRUPTED;
276 } else if (xfs_sb_version_hasdalign(sbp)) {
277 xfs_notice(mp, "SB stripe alignment sanity check failed");
278 return -EFSCORRUPTED;
279 } else if (sbp->sb_width) {
280 xfs_notice(mp, "SB stripe width sanity check failed");
281 return -EFSCORRUPTED;
285 if (xfs_sb_version_hascrc(&mp->m_sb) &&
286 sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
287 xfs_notice(mp, "v5 SB sanity check failed");
288 return -EFSCORRUPTED;
292 * Until this is fixed only page-sized or smaller data blocks work.
294 if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
296 "File system with blocksize %d bytes. "
297 "Only pagesize (%ld) or less will currently work.",
298 sbp->sb_blocksize, PAGE_SIZE);
303 * Currently only very few inode sizes are supported.
305 switch (sbp->sb_inodesize) {
312 xfs_warn(mp, "inode size of %d bytes not supported",
317 if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
318 xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
320 "file system too large to be mounted on this system.");
324 if (check_inprogress && sbp->sb_inprogress) {
325 xfs_warn(mp, "Offline file system operation in progress!");
326 return -EFSCORRUPTED;
332 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
335 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
336 * leads to in-core values having two different values for a quota
337 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
340 * Note that this change affect only the in-core values. These
341 * values are not written back to disk unless any quota information
342 * is written to the disk. Even in that case, sb_pquotino field is
343 * not written to disk unless the superblock supports pquotino.
345 if (sbp->sb_uquotino == 0)
346 sbp->sb_uquotino = NULLFSINO;
347 if (sbp->sb_gquotino == 0)
348 sbp->sb_gquotino = NULLFSINO;
349 if (sbp->sb_pquotino == 0)
350 sbp->sb_pquotino = NULLFSINO;
353 * We need to do these manipilations only if we are working
354 * with an older version of on-disk superblock.
356 if (xfs_sb_version_has_pquotino(sbp))
359 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
360 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
361 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
362 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
363 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
364 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
365 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
367 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
368 sbp->sb_gquotino != NULLFSINO) {
370 * In older version of superblock, on-disk superblock only
371 * has sb_gquotino, and in-core superblock has both sb_gquotino
372 * and sb_pquotino. But, only one of them is supported at any
373 * point of time. So, if PQUOTA is set in disk superblock,
374 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
375 * above is to make sure we don't do this twice and wipe them
378 sbp->sb_pquotino = sbp->sb_gquotino;
379 sbp->sb_gquotino = NULLFSINO;
389 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
390 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
391 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
392 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
393 to->sb_rextents = be64_to_cpu(from->sb_rextents);
394 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
395 to->sb_logstart = be64_to_cpu(from->sb_logstart);
396 to->sb_rootino = be64_to_cpu(from->sb_rootino);
397 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
398 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
399 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
400 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
401 to->sb_agcount = be32_to_cpu(from->sb_agcount);
402 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
403 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
404 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
405 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
406 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
407 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
408 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
409 to->sb_blocklog = from->sb_blocklog;
410 to->sb_sectlog = from->sb_sectlog;
411 to->sb_inodelog = from->sb_inodelog;
412 to->sb_inopblog = from->sb_inopblog;
413 to->sb_agblklog = from->sb_agblklog;
414 to->sb_rextslog = from->sb_rextslog;
415 to->sb_inprogress = from->sb_inprogress;
416 to->sb_imax_pct = from->sb_imax_pct;
417 to->sb_icount = be64_to_cpu(from->sb_icount);
418 to->sb_ifree = be64_to_cpu(from->sb_ifree);
419 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
420 to->sb_frextents = be64_to_cpu(from->sb_frextents);
421 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
422 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
423 to->sb_qflags = be16_to_cpu(from->sb_qflags);
424 to->sb_flags = from->sb_flags;
425 to->sb_shared_vn = from->sb_shared_vn;
426 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
427 to->sb_unit = be32_to_cpu(from->sb_unit);
428 to->sb_width = be32_to_cpu(from->sb_width);
429 to->sb_dirblklog = from->sb_dirblklog;
430 to->sb_logsectlog = from->sb_logsectlog;
431 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
432 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
433 to->sb_features2 = be32_to_cpu(from->sb_features2);
434 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
435 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
436 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
437 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
438 to->sb_features_log_incompat =
439 be32_to_cpu(from->sb_features_log_incompat);
440 /* crc is only used on disk, not in memory; just init to 0 here. */
442 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
443 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
444 to->sb_lsn = be64_to_cpu(from->sb_lsn);
446 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
447 * feature flag is set; if not set we keep it only in memory.
449 if (xfs_sb_version_hasmetauuid(to))
450 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
452 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
453 /* Convert on-disk flags to in-memory flags? */
455 xfs_sb_quota_from_disk(to);
463 __xfs_sb_from_disk(to, from, true);
467 xfs_sb_quota_to_disk(
471 uint16_t qflags = from->sb_qflags;
473 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
474 if (xfs_sb_version_has_pquotino(from)) {
475 to->sb_qflags = cpu_to_be16(from->sb_qflags);
476 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
477 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
482 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
483 * flags, whereas the on-disk version does. So, convert incore
484 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
486 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
487 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
489 if (from->sb_qflags &
490 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
491 qflags |= XFS_OQUOTA_ENFD;
492 if (from->sb_qflags &
493 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
494 qflags |= XFS_OQUOTA_CHKD;
495 to->sb_qflags = cpu_to_be16(qflags);
498 * GQUOTINO and PQUOTINO cannot be used together in versions
499 * of superblock that do not have pquotino. from->sb_flags
500 * tells us which quota is active and should be copied to
501 * disk. If neither are active, we should NULL the inode.
503 * In all cases, the separate pquotino must remain 0 because it
504 * it beyond the "end" of the valid non-pquotino superblock.
506 if (from->sb_qflags & XFS_GQUOTA_ACCT)
507 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
508 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
509 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
512 * We can't rely on just the fields being logged to tell us
513 * that it is safe to write NULLFSINO - we should only do that
514 * if quotas are not actually enabled. Hence only write
515 * NULLFSINO if both in-core quota inodes are NULL.
517 if (from->sb_gquotino == NULLFSINO &&
518 from->sb_pquotino == NULLFSINO)
519 to->sb_gquotino = cpu_to_be64(NULLFSINO);
530 xfs_sb_quota_to_disk(to, from);
532 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
533 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
534 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
535 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
536 to->sb_rextents = cpu_to_be64(from->sb_rextents);
537 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
538 to->sb_logstart = cpu_to_be64(from->sb_logstart);
539 to->sb_rootino = cpu_to_be64(from->sb_rootino);
540 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
541 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
542 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
543 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
544 to->sb_agcount = cpu_to_be32(from->sb_agcount);
545 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
546 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
547 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
548 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
549 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
550 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
551 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
552 to->sb_blocklog = from->sb_blocklog;
553 to->sb_sectlog = from->sb_sectlog;
554 to->sb_inodelog = from->sb_inodelog;
555 to->sb_inopblog = from->sb_inopblog;
556 to->sb_agblklog = from->sb_agblklog;
557 to->sb_rextslog = from->sb_rextslog;
558 to->sb_inprogress = from->sb_inprogress;
559 to->sb_imax_pct = from->sb_imax_pct;
560 to->sb_icount = cpu_to_be64(from->sb_icount);
561 to->sb_ifree = cpu_to_be64(from->sb_ifree);
562 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
563 to->sb_frextents = cpu_to_be64(from->sb_frextents);
565 to->sb_flags = from->sb_flags;
566 to->sb_shared_vn = from->sb_shared_vn;
567 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
568 to->sb_unit = cpu_to_be32(from->sb_unit);
569 to->sb_width = cpu_to_be32(from->sb_width);
570 to->sb_dirblklog = from->sb_dirblklog;
571 to->sb_logsectlog = from->sb_logsectlog;
572 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
573 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
576 * We need to ensure that bad_features2 always matches features2.
577 * Hence we enforce that here rather than having to remember to do it
578 * everywhere else that updates features2.
580 from->sb_bad_features2 = from->sb_features2;
581 to->sb_features2 = cpu_to_be32(from->sb_features2);
582 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
584 if (xfs_sb_version_hascrc(from)) {
585 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
586 to->sb_features_ro_compat =
587 cpu_to_be32(from->sb_features_ro_compat);
588 to->sb_features_incompat =
589 cpu_to_be32(from->sb_features_incompat);
590 to->sb_features_log_incompat =
591 cpu_to_be32(from->sb_features_log_incompat);
592 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
593 to->sb_lsn = cpu_to_be64(from->sb_lsn);
594 if (xfs_sb_version_hasmetauuid(from))
595 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
604 struct xfs_mount *mp = bp->b_target->bt_mount;
608 * Use call variant which doesn't convert quota flags from disk
609 * format, because xfs_mount_validate_sb checks the on-disk flags.
611 __xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
614 * Only check the in progress field for the primary superblock as
615 * mkfs.xfs doesn't clear it from secondary superblocks.
617 return xfs_mount_validate_sb(mp, &sb,
618 bp->b_maps[0].bm_bn == XFS_SB_DADDR,
623 * If the superblock has the CRC feature bit set or the CRC field is non-null,
624 * check that the CRC is valid. We check the CRC field is non-null because a
625 * single bit error could clear the feature bit and unused parts of the
626 * superblock are supposed to be zero. Hence a non-null crc field indicates that
627 * we've potentially lost a feature bit and we should check it anyway.
629 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
630 * last field in V4 secondary superblocks. So for secondary superblocks,
631 * we are more forgiving, and ignore CRC failures if the primary doesn't
632 * indicate that the fs version is V5.
638 struct xfs_mount *mp = bp->b_target->bt_mount;
639 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
643 * open code the version check to avoid needing to convert the entire
644 * superblock from disk order just to check the version number
646 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
647 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
651 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
652 /* Only fail bad secondaries on a known V5 filesystem */
653 if (bp->b_bn == XFS_SB_DADDR ||
654 xfs_sb_version_hascrc(&mp->m_sb)) {
660 error = xfs_sb_verify(bp, true);
663 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
664 xfs_verifier_error(bp, error, __this_address);
666 xfs_buf_ioerror(bp, error);
670 * We may be probed for a filesystem match, so we may not want to emit
671 * messages when the superblock buffer is not actually an XFS superblock.
672 * If we find an XFS superblock, then run a normal, noisy mount because we are
673 * really going to mount it and want to know about errors.
676 xfs_sb_quiet_read_verify(
679 struct xfs_dsb *dsb = XFS_BUF_TO_SBP(bp);
681 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
682 /* XFS filesystem, verify noisily! */
683 xfs_sb_read_verify(bp);
687 xfs_buf_ioerror(bp, -EWRONGFS);
694 struct xfs_mount *mp = bp->b_target->bt_mount;
695 struct xfs_buf_log_item *bip = bp->b_log_item;
698 error = xfs_sb_verify(bp, false);
700 xfs_verifier_error(bp, error, __this_address);
704 if (!xfs_sb_version_hascrc(&mp->m_sb))
708 XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
710 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
713 const struct xfs_buf_ops xfs_sb_buf_ops = {
715 .verify_read = xfs_sb_read_verify,
716 .verify_write = xfs_sb_write_verify,
719 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
720 .name = "xfs_sb_quiet",
721 .verify_read = xfs_sb_quiet_read_verify,
722 .verify_write = xfs_sb_write_verify,
728 * Mount initialization code establishing various mount
729 * fields from the superblock associated with the given
734 struct xfs_mount *mp,
737 mp->m_agfrotor = mp->m_agirotor = 0;
738 mp->m_maxagi = mp->m_sb.sb_agcount;
739 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
740 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
741 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
742 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
743 mp->m_agino_log = sbp->sb_inopblog + sbp->sb_agblklog;
744 mp->m_blockmask = sbp->sb_blocksize - 1;
745 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
746 mp->m_blockwmask = mp->m_blockwsize - 1;
748 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
749 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
750 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
751 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
753 mp->m_inobt_mxr[0] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 1);
754 mp->m_inobt_mxr[1] = xfs_inobt_maxrecs(mp, sbp->sb_blocksize, 0);
755 mp->m_inobt_mnr[0] = mp->m_inobt_mxr[0] / 2;
756 mp->m_inobt_mnr[1] = mp->m_inobt_mxr[1] / 2;
758 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
759 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
760 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
761 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
763 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
764 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
765 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
766 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
768 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
769 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
770 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
771 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
773 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
774 mp->m_ialloc_inos = (int)MAX((uint16_t)XFS_INODES_PER_CHUNK,
776 mp->m_ialloc_blks = mp->m_ialloc_inos >> sbp->sb_inopblog;
778 if (sbp->sb_spino_align)
779 mp->m_ialloc_min_blks = sbp->sb_spino_align;
781 mp->m_ialloc_min_blks = mp->m_ialloc_blks;
782 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
783 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
787 * xfs_initialize_perag_data
789 * Read in each per-ag structure so we can count up the number of
790 * allocated inodes, free inodes and used filesystem blocks as this
791 * information is no longer persistent in the superblock. Once we have
792 * this information, write it into the in-core superblock structure.
795 xfs_initialize_perag_data(
796 struct xfs_mount *mp,
797 xfs_agnumber_t agcount)
799 xfs_agnumber_t index;
801 xfs_sb_t *sbp = &mp->m_sb;
805 uint64_t bfreelst = 0;
809 for (index = 0; index < agcount; index++) {
811 * read the agf, then the agi. This gets us
812 * all the information we need and populates the
813 * per-ag structures for us.
815 error = xfs_alloc_pagf_init(mp, NULL, index, 0);
819 error = xfs_ialloc_pagi_init(mp, NULL, index);
822 pag = xfs_perag_get(mp, index);
823 ifree += pag->pagi_freecount;
824 ialloc += pag->pagi_count;
825 bfree += pag->pagf_freeblks;
826 bfreelst += pag->pagf_flcount;
827 btree += pag->pagf_btreeblks;
831 /* Overwrite incore superblock counters with just-read data */
832 spin_lock(&mp->m_sb_lock);
833 sbp->sb_ifree = ifree;
834 sbp->sb_icount = ialloc;
835 sbp->sb_fdblocks = bfree + bfreelst + btree;
836 spin_unlock(&mp->m_sb_lock);
838 xfs_reinit_percpu_counters(mp);
844 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
845 * into the superblock buffer to be logged. It does not provide the higher
846 * level of locking that is needed to protect the in-core superblock from
851 struct xfs_trans *tp)
853 struct xfs_mount *mp = tp->t_mountp;
854 struct xfs_buf *bp = xfs_trans_getsb(tp, mp, 0);
856 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
857 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
858 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
860 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
861 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
862 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb));
868 * Sync the superblock to disk.
870 * Note that the caller is responsible for checking the frozen state of the
871 * filesystem. This procedure uses the non-blocking transaction allocator and
872 * thus will allow modifications to a frozen fs. This is required because this
873 * code can be called during the process of freezing where use of the high-level
874 * allocator would deadlock.
878 struct xfs_mount *mp,
881 struct xfs_trans *tp;
884 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
885 XFS_TRANS_NO_WRITECOUNT, &tp);
891 xfs_trans_set_sync(tp);
892 return xfs_trans_commit(tp);
896 * Update all the secondary superblocks to match the new state of the primary.
897 * Because we are completely overwriting all the existing fields in the
898 * secondary superblock buffers, there is no need to read them in from disk.
899 * Just get a new buffer, stamp it and write it.
901 * The sb buffers need to be cached here so that we serialise against other
902 * operations that access the secondary superblocks, but we don't want to keep
903 * them in memory once it is written so we mark it as a one-shot buffer.
906 xfs_update_secondary_sbs(
907 struct xfs_mount *mp)
912 LIST_HEAD (buffer_list);
914 /* update secondary superblocks. */
915 for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
918 bp = xfs_buf_get(mp->m_ddev_targp,
919 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
920 XFS_FSS_TO_BB(mp, 1), 0);
922 * If we get an error reading or writing alternate superblocks,
923 * continue. xfs_repair chooses the "best" superblock based
924 * on most matches; if we break early, we'll leave more
925 * superblocks un-updated than updated, and xfs_repair may
926 * pick them over the properly-updated primary.
930 "error allocating secondary superblock for ag %d",
933 saved_error = -ENOMEM;
937 bp->b_ops = &xfs_sb_buf_ops;
939 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
940 xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
941 xfs_buf_delwri_queue(bp, &buffer_list);
944 /* don't hold too many buffers at once */
948 error = xfs_buf_delwri_submit(&buffer_list);
951 "write error %d updating a secondary superblock near ag %d",
958 error = xfs_buf_delwri_submit(&buffer_list);
961 "write error %d updating a secondary superblock near ag %d",
965 return saved_error ? saved_error : error;
969 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
970 * also writes the superblock buffer to disk sector 0 immediately.
974 struct xfs_mount *mp)
976 struct xfs_trans *tp;
980 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
984 bp = xfs_trans_getsb(tp, mp, 0);
986 xfs_trans_bhold(tp, bp);
987 xfs_trans_set_sync(tp);
988 error = xfs_trans_commit(tp);
992 * write out the sb buffer to get the changes to disk
994 error = xfs_bwrite(bp);
1003 struct xfs_fsop_geom *geo,
1006 memset(geo, 0, sizeof(struct xfs_fsop_geom));
1008 geo->blocksize = sbp->sb_blocksize;
1009 geo->rtextsize = sbp->sb_rextsize;
1010 geo->agblocks = sbp->sb_agblocks;
1011 geo->agcount = sbp->sb_agcount;
1012 geo->logblocks = sbp->sb_logblocks;
1013 geo->sectsize = sbp->sb_sectsize;
1014 geo->inodesize = sbp->sb_inodesize;
1015 geo->imaxpct = sbp->sb_imax_pct;
1016 geo->datablocks = sbp->sb_dblocks;
1017 geo->rtblocks = sbp->sb_rblocks;
1018 geo->rtextents = sbp->sb_rextents;
1019 geo->logstart = sbp->sb_logstart;
1020 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1021 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1023 if (struct_version < 2)
1026 geo->sunit = sbp->sb_unit;
1027 geo->swidth = sbp->sb_width;
1029 if (struct_version < 3)
1032 geo->version = XFS_FSOP_GEOM_VERSION;
1033 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1034 XFS_FSOP_GEOM_FLAGS_DIRV2;
1035 if (xfs_sb_version_hasattr(sbp))
1036 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1037 if (xfs_sb_version_hasquota(sbp))
1038 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1039 if (xfs_sb_version_hasalign(sbp))
1040 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1041 if (xfs_sb_version_hasdalign(sbp))
1042 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1043 if (xfs_sb_version_hasextflgbit(sbp))
1044 geo->flags |= XFS_FSOP_GEOM_FLAGS_EXTFLG;
1045 if (xfs_sb_version_hassector(sbp))
1046 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1047 if (xfs_sb_version_hasasciici(sbp))
1048 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1049 if (xfs_sb_version_haslazysbcount(sbp))
1050 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1051 if (xfs_sb_version_hasattr2(sbp))
1052 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1053 if (xfs_sb_version_hasprojid32bit(sbp))
1054 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1055 if (xfs_sb_version_hascrc(sbp))
1056 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1057 if (xfs_sb_version_hasftype(sbp))
1058 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1059 if (xfs_sb_version_hasfinobt(sbp))
1060 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1061 if (xfs_sb_version_hassparseinodes(sbp))
1062 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1063 if (xfs_sb_version_hasrmapbt(sbp))
1064 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1065 if (xfs_sb_version_hasreflink(sbp))
1066 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1067 if (xfs_sb_version_hassector(sbp))
1068 geo->logsectsize = sbp->sb_logsectsize;
1070 geo->logsectsize = BBSIZE;
1071 geo->rtsectsize = sbp->sb_blocksize;
1072 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1074 if (struct_version < 4)
1077 if (xfs_sb_version_haslogv2(sbp))
1078 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1080 geo->logsunit = sbp->sb_logsunit;
1085 /* Read a secondary superblock. */
1087 xfs_sb_read_secondary(
1088 struct xfs_mount *mp,
1089 struct xfs_trans *tp,
1090 xfs_agnumber_t agno,
1091 struct xfs_buf **bpp)
1096 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1097 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1098 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1099 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1102 xfs_buf_set_ref(bp, XFS_SSB_REF);
1107 /* Get an uninitialised secondary superblock buffer. */
1109 xfs_sb_get_secondary(
1110 struct xfs_mount *mp,
1111 struct xfs_trans *tp,
1112 xfs_agnumber_t agno,
1113 struct xfs_buf **bpp)
1117 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1118 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1119 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1120 XFS_FSS_TO_BB(mp, 1), 0);
1123 bp->b_ops = &xfs_sb_buf_ops;
1124 xfs_buf_oneshot(bp);