1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_ialloc.h"
16 #include "xfs_alloc.h"
17 #include "xfs_error.h"
18 #include "xfs_trans.h"
19 #include "xfs_buf_item.h"
20 #include "xfs_bmap_btree.h"
21 #include "xfs_alloc_btree.h"
23 #include "xfs_rmap_btree.h"
24 #include "xfs_refcount_btree.h"
25 #include "xfs_da_format.h"
26 #include "xfs_health.h"
30 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
33 /* Check all the superblock fields we care about when reading one in. */
39 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
43 * Version 5 superblock feature mask validation. Reject combinations
44 * the kernel cannot support up front before checking anything else.
46 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
48 "Superblock has unknown compatible features (0x%x) enabled.",
49 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
51 "Using a more recent kernel is recommended.");
54 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
56 "Superblock has unknown read-only compatible features (0x%x) enabled.",
57 (sbp->sb_features_ro_compat &
58 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
59 if (!(mp->m_flags & XFS_MOUNT_RDONLY)) {
61 "Attempted to mount read-only compatible filesystem read-write.");
63 "Filesystem can only be safely mounted read only.");
68 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
70 "Superblock has unknown incompatible features (0x%x) enabled.",
71 (sbp->sb_features_incompat &
72 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
74 "Filesystem cannot be safely mounted by this kernel.");
81 /* Check all the superblock fields we care about when writing one out. */
83 xfs_validate_sb_write(
89 * Carry out additional sb summary counter sanity checks when we write
90 * the superblock. We skip this in the read validator because there
91 * could be newer superblocks in the log and if the values are garbage
92 * even after replay we'll recalculate them at the end of log mount.
94 * mkfs has traditionally written zeroed counters to inprogress and
95 * secondary superblocks, so allow this usage to continue because
96 * we never read counters from such superblocks.
98 if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
99 (sbp->sb_fdblocks > sbp->sb_dblocks ||
100 !xfs_verify_icount(mp, sbp->sb_icount) ||
101 sbp->sb_ifree > sbp->sb_icount)) {
102 xfs_warn(mp, "SB summary counter sanity check failed");
103 return -EFSCORRUPTED;
106 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_5)
110 * Version 5 superblock feature mask validation. Reject combinations
111 * the kernel cannot support since we checked for unsupported bits in
112 * the read verifier, which means that memory is corrupt.
114 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
116 "Corruption detected in superblock compatible features (0x%x)!",
117 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
118 return -EFSCORRUPTED;
121 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
123 "Corruption detected in superblock read-only compatible features (0x%x)!",
124 (sbp->sb_features_ro_compat &
125 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
126 return -EFSCORRUPTED;
128 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
130 "Corruption detected in superblock incompatible features (0x%x)!",
131 (sbp->sb_features_incompat &
132 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
133 return -EFSCORRUPTED;
135 if (xfs_sb_has_incompat_log_feature(sbp,
136 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
138 "Corruption detected in superblock incompatible log features (0x%x)!",
139 (sbp->sb_features_log_incompat &
140 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
141 return -EFSCORRUPTED;
145 * We can't read verify the sb LSN because the read verifier is called
146 * before the log is allocated and processed. We know the log is set up
147 * before write verifier calls, so check it here.
149 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
150 return -EFSCORRUPTED;
155 /* Check the validity of the SB. */
157 xfs_validate_sb_common(
158 struct xfs_mount *mp,
162 struct xfs_dsb *dsb = bp->b_addr;
163 uint32_t agcount = 0;
166 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
167 xfs_warn(mp, "bad magic number");
171 if (!xfs_sb_good_version(sbp)) {
172 xfs_warn(mp, "bad version");
176 if (xfs_sb_version_has_pquotino(sbp)) {
177 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
179 "Version 5 of Super block has XFS_OQUOTA bits.");
180 return -EFSCORRUPTED;
182 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
183 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
185 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
186 return -EFSCORRUPTED;
190 * Full inode chunks must be aligned to inode chunk size when
191 * sparse inodes are enabled to support the sparse chunk
192 * allocation algorithm and prevent overlapping inode records.
194 if (xfs_sb_version_hassparseinodes(sbp)) {
197 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
199 if (sbp->sb_inoalignmt != align) {
201 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
202 sbp->sb_inoalignmt, align);
208 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
210 "filesystem is marked as having an external log; "
211 "specify logdev on the mount command line.");
216 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
218 "filesystem is marked as having an internal log; "
219 "do not specify logdev on the mount command line.");
223 /* Compute agcount for this number of dblocks and agblocks */
224 if (sbp->sb_agblocks) {
225 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
231 * More sanity checking. Most of these were stolen directly from
235 sbp->sb_agcount <= 0 ||
236 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
237 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
238 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
239 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
240 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
241 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
242 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
243 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
244 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
245 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
246 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
247 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
248 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
249 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
250 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
251 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
252 sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE ||
253 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
254 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
255 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
256 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
257 agcount == 0 || agcount != sbp->sb_agcount ||
258 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
259 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
260 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
261 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
262 sbp->sb_dblocks == 0 ||
263 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
264 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
265 sbp->sb_shared_vn != 0)) {
266 xfs_notice(mp, "SB sanity check failed");
267 return -EFSCORRUPTED;
270 /* Validate the realtime geometry; stolen from xfs_repair */
271 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
272 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
274 "realtime extent sanity check failed");
275 return -EFSCORRUPTED;
278 if (sbp->sb_rblocks == 0) {
279 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
280 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
282 "realtime zeroed geometry check failed");
283 return -EFSCORRUPTED;
289 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
290 rbmblocks = howmany_64(sbp->sb_rextents,
291 NBBY * sbp->sb_blocksize);
293 if (sbp->sb_rextents != rexts ||
294 sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
295 sbp->sb_rbmblocks != rbmblocks) {
297 "realtime geometry sanity check failed");
298 return -EFSCORRUPTED;
303 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
304 * would imply the image is corrupted.
306 if (!!sbp->sb_unit ^ xfs_sb_version_hasdalign(sbp)) {
307 xfs_notice(mp, "SB stripe alignment sanity check failed");
308 return -EFSCORRUPTED;
311 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
312 XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
313 return -EFSCORRUPTED;
315 if (xfs_sb_version_hascrc(&mp->m_sb) &&
316 sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
317 xfs_notice(mp, "v5 SB sanity check failed");
318 return -EFSCORRUPTED;
322 * Currently only very few inode sizes are supported.
324 switch (sbp->sb_inodesize) {
331 xfs_warn(mp, "inode size of %d bytes not supported",
340 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
343 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
344 * leads to in-core values having two different values for a quota
345 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
348 * Note that this change affect only the in-core values. These
349 * values are not written back to disk unless any quota information
350 * is written to the disk. Even in that case, sb_pquotino field is
351 * not written to disk unless the superblock supports pquotino.
353 if (sbp->sb_uquotino == 0)
354 sbp->sb_uquotino = NULLFSINO;
355 if (sbp->sb_gquotino == 0)
356 sbp->sb_gquotino = NULLFSINO;
357 if (sbp->sb_pquotino == 0)
358 sbp->sb_pquotino = NULLFSINO;
361 * We need to do these manipilations only if we are working
362 * with an older version of on-disk superblock.
364 if (xfs_sb_version_has_pquotino(sbp))
367 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
368 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
369 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
370 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
371 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
372 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
373 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
375 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
376 sbp->sb_gquotino != NULLFSINO) {
378 * In older version of superblock, on-disk superblock only
379 * has sb_gquotino, and in-core superblock has both sb_gquotino
380 * and sb_pquotino. But, only one of them is supported at any
381 * point of time. So, if PQUOTA is set in disk superblock,
382 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
383 * above is to make sure we don't do this twice and wipe them
386 sbp->sb_pquotino = sbp->sb_gquotino;
387 sbp->sb_gquotino = NULLFSINO;
397 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
398 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
399 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
400 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
401 to->sb_rextents = be64_to_cpu(from->sb_rextents);
402 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
403 to->sb_logstart = be64_to_cpu(from->sb_logstart);
404 to->sb_rootino = be64_to_cpu(from->sb_rootino);
405 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
406 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
407 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
408 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
409 to->sb_agcount = be32_to_cpu(from->sb_agcount);
410 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
411 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
412 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
413 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
414 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
415 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
416 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
417 to->sb_blocklog = from->sb_blocklog;
418 to->sb_sectlog = from->sb_sectlog;
419 to->sb_inodelog = from->sb_inodelog;
420 to->sb_inopblog = from->sb_inopblog;
421 to->sb_agblklog = from->sb_agblklog;
422 to->sb_rextslog = from->sb_rextslog;
423 to->sb_inprogress = from->sb_inprogress;
424 to->sb_imax_pct = from->sb_imax_pct;
425 to->sb_icount = be64_to_cpu(from->sb_icount);
426 to->sb_ifree = be64_to_cpu(from->sb_ifree);
427 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
428 to->sb_frextents = be64_to_cpu(from->sb_frextents);
429 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
430 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
431 to->sb_qflags = be16_to_cpu(from->sb_qflags);
432 to->sb_flags = from->sb_flags;
433 to->sb_shared_vn = from->sb_shared_vn;
434 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
435 to->sb_unit = be32_to_cpu(from->sb_unit);
436 to->sb_width = be32_to_cpu(from->sb_width);
437 to->sb_dirblklog = from->sb_dirblklog;
438 to->sb_logsectlog = from->sb_logsectlog;
439 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
440 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
441 to->sb_features2 = be32_to_cpu(from->sb_features2);
442 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
443 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
444 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
445 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
446 to->sb_features_log_incompat =
447 be32_to_cpu(from->sb_features_log_incompat);
448 /* crc is only used on disk, not in memory; just init to 0 here. */
450 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
451 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
452 to->sb_lsn = be64_to_cpu(from->sb_lsn);
454 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
455 * feature flag is set; if not set we keep it only in memory.
457 if (xfs_sb_version_hasmetauuid(to))
458 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
460 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
461 /* Convert on-disk flags to in-memory flags? */
463 xfs_sb_quota_from_disk(to);
471 __xfs_sb_from_disk(to, from, true);
475 xfs_sb_quota_to_disk(
479 uint16_t qflags = from->sb_qflags;
481 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
482 if (xfs_sb_version_has_pquotino(from)) {
483 to->sb_qflags = cpu_to_be16(from->sb_qflags);
484 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
485 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
490 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
491 * flags, whereas the on-disk version does. So, convert incore
492 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
494 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
495 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
497 if (from->sb_qflags &
498 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
499 qflags |= XFS_OQUOTA_ENFD;
500 if (from->sb_qflags &
501 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
502 qflags |= XFS_OQUOTA_CHKD;
503 to->sb_qflags = cpu_to_be16(qflags);
506 * GQUOTINO and PQUOTINO cannot be used together in versions
507 * of superblock that do not have pquotino. from->sb_flags
508 * tells us which quota is active and should be copied to
509 * disk. If neither are active, we should NULL the inode.
511 * In all cases, the separate pquotino must remain 0 because it
512 * is beyond the "end" of the valid non-pquotino superblock.
514 if (from->sb_qflags & XFS_GQUOTA_ACCT)
515 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
516 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
517 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
520 * We can't rely on just the fields being logged to tell us
521 * that it is safe to write NULLFSINO - we should only do that
522 * if quotas are not actually enabled. Hence only write
523 * NULLFSINO if both in-core quota inodes are NULL.
525 if (from->sb_gquotino == NULLFSINO &&
526 from->sb_pquotino == NULLFSINO)
527 to->sb_gquotino = cpu_to_be64(NULLFSINO);
538 xfs_sb_quota_to_disk(to, from);
540 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
541 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
542 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
543 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
544 to->sb_rextents = cpu_to_be64(from->sb_rextents);
545 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
546 to->sb_logstart = cpu_to_be64(from->sb_logstart);
547 to->sb_rootino = cpu_to_be64(from->sb_rootino);
548 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
549 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
550 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
551 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
552 to->sb_agcount = cpu_to_be32(from->sb_agcount);
553 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
554 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
555 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
556 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
557 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
558 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
559 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
560 to->sb_blocklog = from->sb_blocklog;
561 to->sb_sectlog = from->sb_sectlog;
562 to->sb_inodelog = from->sb_inodelog;
563 to->sb_inopblog = from->sb_inopblog;
564 to->sb_agblklog = from->sb_agblklog;
565 to->sb_rextslog = from->sb_rextslog;
566 to->sb_inprogress = from->sb_inprogress;
567 to->sb_imax_pct = from->sb_imax_pct;
568 to->sb_icount = cpu_to_be64(from->sb_icount);
569 to->sb_ifree = cpu_to_be64(from->sb_ifree);
570 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
571 to->sb_frextents = cpu_to_be64(from->sb_frextents);
573 to->sb_flags = from->sb_flags;
574 to->sb_shared_vn = from->sb_shared_vn;
575 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
576 to->sb_unit = cpu_to_be32(from->sb_unit);
577 to->sb_width = cpu_to_be32(from->sb_width);
578 to->sb_dirblklog = from->sb_dirblklog;
579 to->sb_logsectlog = from->sb_logsectlog;
580 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
581 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
584 * We need to ensure that bad_features2 always matches features2.
585 * Hence we enforce that here rather than having to remember to do it
586 * everywhere else that updates features2.
588 from->sb_bad_features2 = from->sb_features2;
589 to->sb_features2 = cpu_to_be32(from->sb_features2);
590 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
592 if (xfs_sb_version_hascrc(from)) {
593 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
594 to->sb_features_ro_compat =
595 cpu_to_be32(from->sb_features_ro_compat);
596 to->sb_features_incompat =
597 cpu_to_be32(from->sb_features_incompat);
598 to->sb_features_log_incompat =
599 cpu_to_be32(from->sb_features_log_incompat);
600 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
601 to->sb_lsn = cpu_to_be64(from->sb_lsn);
602 if (xfs_sb_version_hasmetauuid(from))
603 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
608 * If the superblock has the CRC feature bit set or the CRC field is non-null,
609 * check that the CRC is valid. We check the CRC field is non-null because a
610 * single bit error could clear the feature bit and unused parts of the
611 * superblock are supposed to be zero. Hence a non-null crc field indicates that
612 * we've potentially lost a feature bit and we should check it anyway.
614 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
615 * last field in V4 secondary superblocks. So for secondary superblocks,
616 * we are more forgiving, and ignore CRC failures if the primary doesn't
617 * indicate that the fs version is V5.
624 struct xfs_mount *mp = bp->b_mount;
625 struct xfs_dsb *dsb = bp->b_addr;
629 * open code the version check to avoid needing to convert the entire
630 * superblock from disk order just to check the version number
632 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
633 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
637 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
638 /* Only fail bad secondaries on a known V5 filesystem */
639 if (bp->b_bn == XFS_SB_DADDR ||
640 xfs_sb_version_hascrc(&mp->m_sb)) {
648 * Check all the superblock fields. Don't byteswap the xquota flags
649 * because _verify_common checks the on-disk values.
651 __xfs_sb_from_disk(&sb, dsb, false);
652 error = xfs_validate_sb_common(mp, bp, &sb);
655 error = xfs_validate_sb_read(mp, &sb);
658 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
659 xfs_verifier_error(bp, error, __this_address);
661 xfs_buf_ioerror(bp, error);
665 * We may be probed for a filesystem match, so we may not want to emit
666 * messages when the superblock buffer is not actually an XFS superblock.
667 * If we find an XFS superblock, then run a normal, noisy mount because we are
668 * really going to mount it and want to know about errors.
671 xfs_sb_quiet_read_verify(
674 struct xfs_dsb *dsb = bp->b_addr;
676 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
677 /* XFS filesystem, verify noisily! */
678 xfs_sb_read_verify(bp);
682 xfs_buf_ioerror(bp, -EWRONGFS);
690 struct xfs_mount *mp = bp->b_mount;
691 struct xfs_buf_log_item *bip = bp->b_log_item;
692 struct xfs_dsb *dsb = bp->b_addr;
696 * Check all the superblock fields. Don't byteswap the xquota flags
697 * because _verify_common checks the on-disk values.
699 __xfs_sb_from_disk(&sb, dsb, false);
700 error = xfs_validate_sb_common(mp, bp, &sb);
703 error = xfs_validate_sb_write(mp, bp, &sb);
707 if (!xfs_sb_version_hascrc(&mp->m_sb))
711 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
713 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
717 xfs_verifier_error(bp, error, __this_address);
720 const struct xfs_buf_ops xfs_sb_buf_ops = {
722 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
723 .verify_read = xfs_sb_read_verify,
724 .verify_write = xfs_sb_write_verify,
727 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
728 .name = "xfs_sb_quiet",
729 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
730 .verify_read = xfs_sb_quiet_read_verify,
731 .verify_write = xfs_sb_write_verify,
737 * Mount initialization code establishing various mount
738 * fields from the superblock associated with the given
741 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
745 struct xfs_mount *mp,
748 mp->m_agfrotor = mp->m_agirotor = 0;
749 mp->m_maxagi = mp->m_sb.sb_agcount;
750 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
751 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
752 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
753 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
754 mp->m_blockmask = sbp->sb_blocksize - 1;
755 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
756 mp->m_blockwmask = mp->m_blockwsize - 1;
758 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
759 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
760 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
761 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
763 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
764 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
765 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
766 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
768 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
769 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
770 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
771 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
773 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
774 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
775 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
776 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
778 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
779 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
780 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
784 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
785 * into the superblock buffer to be logged. It does not provide the higher
786 * level of locking that is needed to protect the in-core superblock from
791 struct xfs_trans *tp)
793 struct xfs_mount *mp = tp->t_mountp;
794 struct xfs_buf *bp = xfs_trans_getsb(tp);
797 * Lazy sb counters don't update the in-core superblock so do that now.
798 * If this is at unmount, the counters will be exactly correct, but at
799 * any other time they will only be ballpark correct because of
800 * reservations that have been taken out percpu counters. If we have an
801 * unclean shutdown, this will be corrected by log recovery rebuilding
802 * the counters from the AGF block counts.
804 if (xfs_sb_version_haslazysbcount(&mp->m_sb)) {
805 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
806 mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
807 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
810 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
811 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
812 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
818 * Sync the superblock to disk.
820 * Note that the caller is responsible for checking the frozen state of the
821 * filesystem. This procedure uses the non-blocking transaction allocator and
822 * thus will allow modifications to a frozen fs. This is required because this
823 * code can be called during the process of freezing where use of the high-level
824 * allocator would deadlock.
828 struct xfs_mount *mp,
831 struct xfs_trans *tp;
834 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
835 XFS_TRANS_NO_WRITECOUNT, &tp);
841 xfs_trans_set_sync(tp);
842 return xfs_trans_commit(tp);
846 * Update all the secondary superblocks to match the new state of the primary.
847 * Because we are completely overwriting all the existing fields in the
848 * secondary superblock buffers, there is no need to read them in from disk.
849 * Just get a new buffer, stamp it and write it.
851 * The sb buffers need to be cached here so that we serialise against other
852 * operations that access the secondary superblocks, but we don't want to keep
853 * them in memory once it is written so we mark it as a one-shot buffer.
856 xfs_update_secondary_sbs(
857 struct xfs_mount *mp)
859 struct xfs_perag *pag;
860 xfs_agnumber_t agno = 1;
863 LIST_HEAD (buffer_list);
865 /* update secondary superblocks. */
866 for_each_perag_from(mp, agno, pag) {
869 error = xfs_buf_get(mp->m_ddev_targp,
870 XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
871 XFS_FSS_TO_BB(mp, 1), &bp);
873 * If we get an error reading or writing alternate superblocks,
874 * continue. xfs_repair chooses the "best" superblock based
875 * on most matches; if we break early, we'll leave more
876 * superblocks un-updated than updated, and xfs_repair may
877 * pick them over the properly-updated primary.
881 "error allocating secondary superblock for ag %d",
888 bp->b_ops = &xfs_sb_buf_ops;
890 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
891 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
892 xfs_buf_delwri_queue(bp, &buffer_list);
895 /* don't hold too many buffers at once */
899 error = xfs_buf_delwri_submit(&buffer_list);
902 "write error %d updating a secondary superblock near ag %d",
903 error, pag->pag_agno);
909 error = xfs_buf_delwri_submit(&buffer_list);
912 "write error %d updating a secondary superblock near ag %d",
916 return saved_error ? saved_error : error;
920 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
921 * also writes the superblock buffer to disk sector 0 immediately.
925 struct xfs_mount *mp)
927 struct xfs_trans *tp;
931 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
935 bp = xfs_trans_getsb(tp);
937 xfs_trans_bhold(tp, bp);
938 xfs_trans_set_sync(tp);
939 error = xfs_trans_commit(tp);
943 * write out the sb buffer to get the changes to disk
945 error = xfs_bwrite(bp);
954 struct xfs_fsop_geom *geo,
957 memset(geo, 0, sizeof(struct xfs_fsop_geom));
959 geo->blocksize = sbp->sb_blocksize;
960 geo->rtextsize = sbp->sb_rextsize;
961 geo->agblocks = sbp->sb_agblocks;
962 geo->agcount = sbp->sb_agcount;
963 geo->logblocks = sbp->sb_logblocks;
964 geo->sectsize = sbp->sb_sectsize;
965 geo->inodesize = sbp->sb_inodesize;
966 geo->imaxpct = sbp->sb_imax_pct;
967 geo->datablocks = sbp->sb_dblocks;
968 geo->rtblocks = sbp->sb_rblocks;
969 geo->rtextents = sbp->sb_rextents;
970 geo->logstart = sbp->sb_logstart;
971 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
972 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
974 if (struct_version < 2)
977 geo->sunit = sbp->sb_unit;
978 geo->swidth = sbp->sb_width;
980 if (struct_version < 3)
983 geo->version = XFS_FSOP_GEOM_VERSION;
984 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
985 XFS_FSOP_GEOM_FLAGS_DIRV2 |
986 XFS_FSOP_GEOM_FLAGS_EXTFLG;
987 if (xfs_sb_version_hasattr(sbp))
988 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
989 if (xfs_sb_version_hasquota(sbp))
990 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
991 if (xfs_sb_version_hasalign(sbp))
992 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
993 if (xfs_sb_version_hasdalign(sbp))
994 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
995 if (xfs_sb_version_hassector(sbp))
996 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
997 if (xfs_sb_version_hasasciici(sbp))
998 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
999 if (xfs_sb_version_haslazysbcount(sbp))
1000 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1001 if (xfs_sb_version_hasattr2(sbp))
1002 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1003 if (xfs_sb_version_hasprojid32bit(sbp))
1004 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1005 if (xfs_sb_version_hascrc(sbp))
1006 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1007 if (xfs_sb_version_hasftype(sbp))
1008 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1009 if (xfs_sb_version_hasfinobt(sbp))
1010 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1011 if (xfs_sb_version_hassparseinodes(sbp))
1012 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1013 if (xfs_sb_version_hasrmapbt(sbp))
1014 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1015 if (xfs_sb_version_hasreflink(sbp))
1016 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1017 if (xfs_sb_version_hasbigtime(sbp))
1018 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
1019 if (xfs_sb_version_hasinobtcounts(sbp))
1020 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
1021 if (xfs_sb_version_hassector(sbp))
1022 geo->logsectsize = sbp->sb_logsectsize;
1024 geo->logsectsize = BBSIZE;
1025 geo->rtsectsize = sbp->sb_blocksize;
1026 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1028 if (struct_version < 4)
1031 if (xfs_sb_version_haslogv2(sbp))
1032 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1034 geo->logsunit = sbp->sb_logsunit;
1036 if (struct_version < 5)
1039 geo->version = XFS_FSOP_GEOM_VERSION_V5;
1042 /* Read a secondary superblock. */
1044 xfs_sb_read_secondary(
1045 struct xfs_mount *mp,
1046 struct xfs_trans *tp,
1047 xfs_agnumber_t agno,
1048 struct xfs_buf **bpp)
1053 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1054 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1055 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1056 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1059 xfs_buf_set_ref(bp, XFS_SSB_REF);
1064 /* Get an uninitialised secondary superblock buffer. */
1066 xfs_sb_get_secondary(
1067 struct xfs_mount *mp,
1068 struct xfs_trans *tp,
1069 xfs_agnumber_t agno,
1070 struct xfs_buf **bpp)
1075 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1076 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1077 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1078 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1081 bp->b_ops = &xfs_sb_buf_ops;
1082 xfs_buf_oneshot(bp);
1088 * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
1089 * so users won't be confused by values in error messages.
1092 xfs_validate_stripe_geometry(
1093 struct xfs_mount *mp,
1099 if (swidth > INT_MAX) {
1102 "stripe width (%lld) is too large", swidth);
1106 if (sunit > swidth) {
1109 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1113 if (sectorsize && (int)sunit % sectorsize) {
1116 "stripe unit (%lld) must be a multiple of the sector size (%d)",
1121 if (sunit && !swidth) {
1124 "invalid stripe unit (%lld) and stripe width of 0", sunit);
1128 if (!sunit && swidth) {
1131 "invalid stripe width (%lld) and stripe unit of 0", swidth);
1135 if (sunit && (int)swidth % (int)sunit) {
1138 "stripe width (%lld) must be a multiple of the stripe unit (%lld)",