drm/panel-edp: Add STA 116QHD024002
[drm/drm-misc.git] / fs / xfs / libxfs / xfs_sb.c
bloba809513a290cf4a4d150a7dc8d3a374f79b3ea7e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_sb.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"
22 #include "xfs_log.h"
23 #include "xfs_rmap_btree.h"
24 #include "xfs_refcount_btree.h"
25 #include "xfs_da_format.h"
26 #include "xfs_health.h"
27 #include "xfs_ag.h"
28 #include "xfs_rtbitmap.h"
29 #include "xfs_exchrange.h"
30 #include "xfs_rtgroup.h"
33 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
37 * Check that all the V4 feature bits that the V5 filesystem format requires are
38 * correctly set.
40 static bool
41 xfs_sb_validate_v5_features(
42 struct xfs_sb *sbp)
44 /* We must not have any unknown V4 feature bits set */
45 if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
46 return false;
49 * The CRC bit is considered an invalid V4 flag, so we have to add it
50 * manually to the OKBITS mask.
52 if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
53 XFS_SB_VERSION2_CRCBIT))
54 return false;
56 /* Now check all the required V4 feature flags are set. */
58 #define V5_VERS_FLAGS (XFS_SB_VERSION_NLINKBIT | \
59 XFS_SB_VERSION_ALIGNBIT | \
60 XFS_SB_VERSION_LOGV2BIT | \
61 XFS_SB_VERSION_EXTFLGBIT | \
62 XFS_SB_VERSION_DIRV2BIT | \
63 XFS_SB_VERSION_MOREBITSBIT)
65 #define V5_FEAT_FLAGS (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
66 XFS_SB_VERSION2_ATTR2BIT | \
67 XFS_SB_VERSION2_PROJID32BIT | \
68 XFS_SB_VERSION2_CRCBIT)
70 if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
71 return false;
72 if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
73 return false;
74 return true;
78 * We current support XFS v5 formats with known features and v4 superblocks with
79 * at least V2 directories.
81 bool
82 xfs_sb_good_version(
83 struct xfs_sb *sbp)
86 * All v5 filesystems are supported, but we must check that all the
87 * required v4 feature flags are enabled correctly as the code checks
88 * those flags and not for v5 support.
90 if (xfs_sb_is_v5(sbp))
91 return xfs_sb_validate_v5_features(sbp);
93 /* versions prior to v4 are not supported */
94 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
95 return false;
97 /* We must not have any unknown v4 feature bits set */
98 if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
99 ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
100 (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
101 return false;
103 /* V4 filesystems need v2 directories and unwritten extents */
104 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
105 return false;
106 if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
107 return false;
109 /* It's a supported v4 filesystem */
110 return true;
113 uint64_t
114 xfs_sb_version_to_features(
115 struct xfs_sb *sbp)
117 uint64_t features = 0;
119 /* optional V4 features */
120 if (sbp->sb_rblocks > 0)
121 features |= XFS_FEAT_REALTIME;
122 if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
123 features |= XFS_FEAT_NLINK;
124 if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
125 features |= XFS_FEAT_ATTR;
126 if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
127 features |= XFS_FEAT_QUOTA;
128 if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
129 features |= XFS_FEAT_ALIGN;
130 if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
131 features |= XFS_FEAT_LOGV2;
132 if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
133 features |= XFS_FEAT_DALIGN;
134 if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
135 features |= XFS_FEAT_EXTFLG;
136 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
137 features |= XFS_FEAT_SECTOR;
138 if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
139 features |= XFS_FEAT_ASCIICI;
140 if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
141 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
142 features |= XFS_FEAT_LAZYSBCOUNT;
143 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
144 features |= XFS_FEAT_ATTR2;
145 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
146 features |= XFS_FEAT_PROJID32;
147 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
148 features |= XFS_FEAT_FTYPE;
151 if (!xfs_sb_is_v5(sbp))
152 return features;
154 /* Always on V5 features */
155 features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
156 XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
157 XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
159 /* Optional V5 features */
160 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
161 features |= XFS_FEAT_FINOBT;
162 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
163 features |= XFS_FEAT_RMAPBT;
164 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
165 features |= XFS_FEAT_REFLINK;
166 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
167 features |= XFS_FEAT_INOBTCNT;
168 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
169 features |= XFS_FEAT_FTYPE;
170 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
171 features |= XFS_FEAT_SPINODES;
172 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
173 features |= XFS_FEAT_META_UUID;
174 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
175 features |= XFS_FEAT_BIGTIME;
176 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
177 features |= XFS_FEAT_NEEDSREPAIR;
178 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
179 features |= XFS_FEAT_NREXT64;
180 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE)
181 features |= XFS_FEAT_EXCHANGE_RANGE;
182 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_PARENT)
183 features |= XFS_FEAT_PARENT;
184 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)
185 features |= XFS_FEAT_METADIR;
187 return features;
190 /* Check all the superblock fields we care about when reading one in. */
191 STATIC int
192 xfs_validate_sb_read(
193 struct xfs_mount *mp,
194 struct xfs_sb *sbp)
196 if (!xfs_sb_is_v5(sbp))
197 return 0;
200 * Version 5 superblock feature mask validation. Reject combinations
201 * the kernel cannot support up front before checking anything else.
203 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
204 xfs_warn(mp,
205 "Superblock has unknown compatible features (0x%x) enabled.",
206 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
207 xfs_warn(mp,
208 "Using a more recent kernel is recommended.");
211 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
212 xfs_alert(mp,
213 "Superblock has unknown read-only compatible features (0x%x) enabled.",
214 (sbp->sb_features_ro_compat &
215 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
216 if (!xfs_is_readonly(mp)) {
217 xfs_warn(mp,
218 "Attempted to mount read-only compatible filesystem read-write.");
219 xfs_warn(mp,
220 "Filesystem can only be safely mounted read only.");
222 return -EINVAL;
225 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
226 xfs_warn(mp,
227 "Superblock has unknown incompatible features (0x%x) enabled.",
228 (sbp->sb_features_incompat &
229 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
230 xfs_warn(mp,
231 "Filesystem cannot be safely mounted by this kernel.");
232 return -EINVAL;
235 return 0;
238 /* Return the number of extents covered by a single rt bitmap file */
239 static xfs_rtbxlen_t
240 xfs_extents_per_rbm(
241 struct xfs_sb *sbp)
243 if (xfs_sb_is_v5(sbp) &&
244 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
245 return sbp->sb_rgextents;
246 return sbp->sb_rextents;
250 * Return the payload size of a single rt bitmap block (without the metadata
251 * header if any).
253 static inline unsigned int
254 xfs_rtbmblock_size(
255 struct xfs_sb *sbp)
257 if (xfs_sb_is_v5(sbp) &&
258 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
259 return sbp->sb_blocksize - sizeof(struct xfs_rtbuf_blkinfo);
260 return sbp->sb_blocksize;
263 static uint64_t
264 xfs_expected_rbmblocks(
265 struct xfs_sb *sbp)
267 return howmany_64(xfs_extents_per_rbm(sbp),
268 NBBY * xfs_rtbmblock_size(sbp));
271 /* Validate the realtime geometry */
272 bool
273 xfs_validate_rt_geometry(
274 struct xfs_sb *sbp)
276 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
277 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)
278 return false;
280 if (sbp->sb_rblocks == 0) {
281 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
282 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0)
283 return false;
284 return true;
287 if (sbp->sb_rextents == 0 ||
288 sbp->sb_rextents != div_u64(sbp->sb_rblocks, sbp->sb_rextsize) ||
289 sbp->sb_rextslog != xfs_compute_rextslog(sbp->sb_rextents) ||
290 sbp->sb_rbmblocks != xfs_expected_rbmblocks(sbp))
291 return false;
293 return true;
296 /* Check all the superblock fields we care about when writing one out. */
297 STATIC int
298 xfs_validate_sb_write(
299 struct xfs_mount *mp,
300 struct xfs_buf *bp,
301 struct xfs_sb *sbp)
304 * Carry out additional sb summary counter sanity checks when we write
305 * the superblock. We skip this in the read validator because there
306 * could be newer superblocks in the log and if the values are garbage
307 * even after replay we'll recalculate them at the end of log mount.
309 * mkfs has traditionally written zeroed counters to inprogress and
310 * secondary superblocks, so allow this usage to continue because
311 * we never read counters from such superblocks.
313 if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
314 (sbp->sb_fdblocks > sbp->sb_dblocks ||
315 !xfs_verify_icount(mp, sbp->sb_icount) ||
316 sbp->sb_ifree > sbp->sb_icount)) {
317 xfs_warn(mp, "SB summary counter sanity check failed");
318 return -EFSCORRUPTED;
321 if (!xfs_sb_is_v5(sbp))
322 return 0;
325 * Version 5 superblock feature mask validation. Reject combinations
326 * the kernel cannot support since we checked for unsupported bits in
327 * the read verifier, which means that memory is corrupt.
329 if (!xfs_is_readonly(mp) &&
330 xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
331 xfs_alert(mp,
332 "Corruption detected in superblock read-only compatible features (0x%x)!",
333 (sbp->sb_features_ro_compat &
334 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
335 return -EFSCORRUPTED;
337 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
338 xfs_warn(mp,
339 "Corruption detected in superblock incompatible features (0x%x)!",
340 (sbp->sb_features_incompat &
341 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
342 return -EFSCORRUPTED;
344 if (xfs_sb_has_incompat_log_feature(sbp,
345 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
346 xfs_warn(mp,
347 "Corruption detected in superblock incompatible log features (0x%x)!",
348 (sbp->sb_features_log_incompat &
349 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
350 return -EFSCORRUPTED;
354 * We can't read verify the sb LSN because the read verifier is called
355 * before the log is allocated and processed. We know the log is set up
356 * before write verifier calls, so check it here.
358 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
359 return -EFSCORRUPTED;
361 return 0;
365 xfs_compute_rgblklog(
366 xfs_rtxlen_t rgextents,
367 xfs_rgblock_t rextsize)
369 uint64_t rgblocks = (uint64_t)rgextents * rextsize;
371 return xfs_highbit64(rgblocks - 1) + 1;
374 static int
375 xfs_validate_sb_rtgroups(
376 struct xfs_mount *mp,
377 struct xfs_sb *sbp)
379 uint64_t groups;
380 int rgblklog;
382 if (sbp->sb_rextsize == 0) {
383 xfs_warn(mp,
384 "Realtime extent size must not be zero.");
385 return -EINVAL;
388 if (sbp->sb_rgextents > XFS_MAX_RGBLOCKS / sbp->sb_rextsize) {
389 xfs_warn(mp,
390 "Realtime group size (%u) must be less than %u rt extents.",
391 sbp->sb_rgextents,
392 XFS_MAX_RGBLOCKS / sbp->sb_rextsize);
393 return -EINVAL;
396 if (sbp->sb_rgextents < XFS_MIN_RGEXTENTS) {
397 xfs_warn(mp,
398 "Realtime group size (%u) must be at least %u rt extents.",
399 sbp->sb_rgextents, XFS_MIN_RGEXTENTS);
400 return -EINVAL;
403 if (sbp->sb_rgcount > XFS_MAX_RGNUMBER) {
404 xfs_warn(mp,
405 "Realtime groups (%u) must be less than %u.",
406 sbp->sb_rgcount, XFS_MAX_RGNUMBER);
407 return -EINVAL;
410 groups = howmany_64(sbp->sb_rextents, sbp->sb_rgextents);
411 if (groups != sbp->sb_rgcount) {
412 xfs_warn(mp,
413 "Realtime groups (%u) do not cover the entire rt section; need (%llu) groups.",
414 sbp->sb_rgcount, groups);
415 return -EINVAL;
418 /* Exchange-range is required for fsr to work on realtime files */
419 if (!(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE)) {
420 xfs_warn(mp,
421 "Realtime groups feature requires exchange-range support.");
422 return -EINVAL;
425 rgblklog = xfs_compute_rgblklog(sbp->sb_rgextents, sbp->sb_rextsize);
426 if (sbp->sb_rgblklog != rgblklog) {
427 xfs_warn(mp,
428 "Realtime group log (%d) does not match expected value (%d).",
429 sbp->sb_rgblklog, rgblklog);
430 return -EINVAL;
433 return 0;
436 /* Check the validity of the SB. */
437 STATIC int
438 xfs_validate_sb_common(
439 struct xfs_mount *mp,
440 struct xfs_buf *bp,
441 struct xfs_sb *sbp)
443 struct xfs_dsb *dsb = bp->b_addr;
444 uint32_t agcount = 0;
445 uint32_t rem;
446 bool has_dalign;
447 int error;
449 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
450 xfs_warn(mp,
451 "Superblock has bad magic number 0x%x. Not an XFS filesystem?",
452 be32_to_cpu(dsb->sb_magicnum));
453 return -EWRONGFS;
456 if (!xfs_sb_good_version(sbp)) {
457 xfs_warn(mp,
458 "Superblock has unknown features enabled or corrupted feature masks.");
459 return -EWRONGFS;
463 * Validate feature flags and state
465 if (xfs_sb_is_v5(sbp)) {
466 if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
467 xfs_notice(mp,
468 "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
469 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
470 return -EFSCORRUPTED;
473 /* V5 has a separate project quota inode */
474 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
475 xfs_notice(mp,
476 "Version 5 of Super block has XFS_OQUOTA bits.");
477 return -EFSCORRUPTED;
481 * Full inode chunks must be aligned to inode chunk size when
482 * sparse inodes are enabled to support the sparse chunk
483 * allocation algorithm and prevent overlapping inode records.
485 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
486 uint32_t align;
488 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
489 >> sbp->sb_blocklog;
490 if (sbp->sb_inoalignmt != align) {
491 xfs_warn(mp,
492 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
493 sbp->sb_inoalignmt, align);
494 return -EINVAL;
497 if (!sbp->sb_spino_align ||
498 sbp->sb_spino_align > sbp->sb_inoalignmt ||
499 (sbp->sb_inoalignmt % sbp->sb_spino_align) != 0) {
500 xfs_warn(mp,
501 "Sparse inode alignment (%u) is invalid.",
502 sbp->sb_spino_align);
503 return -EINVAL;
505 } else if (sbp->sb_spino_align) {
506 xfs_warn(mp,
507 "Sparse inode alignment (%u) should be zero.",
508 sbp->sb_spino_align);
509 return -EINVAL;
512 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
513 if (memchr_inv(sbp->sb_pad, 0, sizeof(sbp->sb_pad))) {
514 xfs_warn(mp,
515 "Metadir superblock padding fields must be zero.");
516 return -EINVAL;
519 error = xfs_validate_sb_rtgroups(mp, sbp);
520 if (error)
521 return error;
523 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
524 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
525 xfs_notice(mp,
526 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
527 return -EFSCORRUPTED;
530 if (unlikely(
531 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
532 xfs_warn(mp,
533 "filesystem is marked as having an external log; "
534 "specify logdev on the mount command line.");
535 return -EINVAL;
538 if (unlikely(
539 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
540 xfs_warn(mp,
541 "filesystem is marked as having an internal log; "
542 "do not specify logdev on the mount command line.");
543 return -EINVAL;
546 /* Compute agcount for this number of dblocks and agblocks */
547 if (sbp->sb_agblocks) {
548 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
549 if (rem)
550 agcount++;
554 * More sanity checking. Most of these were stolen directly from
555 * xfs_repair.
557 if (unlikely(
558 sbp->sb_agcount <= 0 ||
559 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
560 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
561 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
562 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
563 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
564 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
565 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
566 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
567 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
568 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
569 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
570 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
571 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
572 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
573 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
574 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
575 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
576 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
577 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
578 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
579 agcount == 0 || agcount != sbp->sb_agcount ||
580 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
581 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
582 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
583 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
584 sbp->sb_dblocks == 0 ||
585 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
586 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
587 sbp->sb_shared_vn != 0)) {
588 xfs_notice(mp, "SB sanity check failed");
589 return -EFSCORRUPTED;
593 * Logs that are too large are not supported at all. Reject them
594 * outright. Logs that are too small are tolerated on v4 filesystems,
595 * but we can only check that when mounting the log. Hence we skip
596 * those checks here.
598 if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) {
599 xfs_notice(mp,
600 "Log size 0x%x blocks too large, maximum size is 0x%llx blocks",
601 sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS);
602 return -EFSCORRUPTED;
605 if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) {
606 xfs_warn(mp,
607 "log size 0x%llx bytes too large, maximum size is 0x%llx bytes",
608 XFS_FSB_TO_B(mp, sbp->sb_logblocks),
609 XFS_MAX_LOG_BYTES);
610 return -EFSCORRUPTED;
614 * Do not allow filesystems with corrupted log sector or stripe units to
615 * be mounted. We cannot safely size the iclogs or write to the log if
616 * the log stripe unit is not valid.
618 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
619 if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) {
620 xfs_notice(mp,
621 "log sector size in bytes/log2 (0x%x/0x%x) must match",
622 sbp->sb_logsectsize, 1U << sbp->sb_logsectlog);
623 return -EFSCORRUPTED;
625 } else if (sbp->sb_logsectsize || sbp->sb_logsectlog) {
626 xfs_notice(mp,
627 "log sector size in bytes/log2 (0x%x/0x%x) are not zero",
628 sbp->sb_logsectsize, sbp->sb_logsectlog);
629 return -EFSCORRUPTED;
632 if (sbp->sb_logsunit > 1) {
633 if (sbp->sb_logsunit % sbp->sb_blocksize) {
634 xfs_notice(mp,
635 "log stripe unit 0x%x bytes must be a multiple of block size",
636 sbp->sb_logsunit);
637 return -EFSCORRUPTED;
639 if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) {
640 xfs_notice(mp,
641 "log stripe unit 0x%x bytes over maximum size (0x%x bytes)",
642 sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE);
643 return -EFSCORRUPTED;
647 if (!xfs_validate_rt_geometry(sbp)) {
648 xfs_notice(mp,
649 "realtime %sgeometry check failed",
650 sbp->sb_rblocks ? "" : "zeroed ");
651 return -EFSCORRUPTED;
655 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
656 * would imply the image is corrupted.
658 has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
659 if (!!sbp->sb_unit ^ has_dalign) {
660 xfs_notice(mp, "SB stripe alignment sanity check failed");
661 return -EFSCORRUPTED;
664 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
665 XFS_FSB_TO_B(mp, sbp->sb_width), 0,
666 xfs_buf_daddr(bp) == XFS_SB_DADDR, false))
667 return -EFSCORRUPTED;
670 * Currently only very few inode sizes are supported.
672 switch (sbp->sb_inodesize) {
673 case 256:
674 case 512:
675 case 1024:
676 case 2048:
677 break;
678 default:
679 xfs_warn(mp, "inode size of %d bytes not supported",
680 sbp->sb_inodesize);
681 return -ENOSYS;
684 return 0;
687 void
688 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
690 if (xfs_sb_is_v5(sbp) &&
691 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
692 sbp->sb_uquotino = NULLFSINO;
693 sbp->sb_gquotino = NULLFSINO;
694 sbp->sb_pquotino = NULLFSINO;
695 return;
699 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
700 * leads to in-core values having two different values for a quota
701 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
702 * NULLFSINO.
704 * Note that this change affect only the in-core values. These
705 * values are not written back to disk unless any quota information
706 * is written to the disk. Even in that case, sb_pquotino field is
707 * not written to disk unless the superblock supports pquotino.
709 if (sbp->sb_uquotino == 0)
710 sbp->sb_uquotino = NULLFSINO;
711 if (sbp->sb_gquotino == 0)
712 sbp->sb_gquotino = NULLFSINO;
713 if (sbp->sb_pquotino == 0)
714 sbp->sb_pquotino = NULLFSINO;
717 * We need to do these manipilations only if we are working
718 * with an older version of on-disk superblock.
720 if (xfs_sb_is_v5(sbp))
721 return;
723 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
724 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
725 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
726 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
727 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
728 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
729 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
731 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
732 sbp->sb_gquotino != NULLFSINO) {
734 * In older version of superblock, on-disk superblock only
735 * has sb_gquotino, and in-core superblock has both sb_gquotino
736 * and sb_pquotino. But, only one of them is supported at any
737 * point of time. So, if PQUOTA is set in disk superblock,
738 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
739 * above is to make sure we don't do this twice and wipe them
740 * both out!
742 sbp->sb_pquotino = sbp->sb_gquotino;
743 sbp->sb_gquotino = NULLFSINO;
747 static void
748 __xfs_sb_from_disk(
749 struct xfs_sb *to,
750 struct xfs_dsb *from,
751 bool convert_xquota)
753 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
754 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
755 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
756 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
757 to->sb_rextents = be64_to_cpu(from->sb_rextents);
758 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
759 to->sb_logstart = be64_to_cpu(from->sb_logstart);
760 to->sb_rootino = be64_to_cpu(from->sb_rootino);
761 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
762 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
763 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
764 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
765 to->sb_agcount = be32_to_cpu(from->sb_agcount);
766 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
767 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
768 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
769 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
770 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
771 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
772 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
773 to->sb_blocklog = from->sb_blocklog;
774 to->sb_sectlog = from->sb_sectlog;
775 to->sb_inodelog = from->sb_inodelog;
776 to->sb_inopblog = from->sb_inopblog;
777 to->sb_agblklog = from->sb_agblklog;
778 to->sb_rextslog = from->sb_rextslog;
779 to->sb_inprogress = from->sb_inprogress;
780 to->sb_imax_pct = from->sb_imax_pct;
781 to->sb_icount = be64_to_cpu(from->sb_icount);
782 to->sb_ifree = be64_to_cpu(from->sb_ifree);
783 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
784 to->sb_frextents = be64_to_cpu(from->sb_frextents);
785 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
786 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
787 to->sb_qflags = be16_to_cpu(from->sb_qflags);
788 to->sb_flags = from->sb_flags;
789 to->sb_shared_vn = from->sb_shared_vn;
790 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
791 to->sb_unit = be32_to_cpu(from->sb_unit);
792 to->sb_width = be32_to_cpu(from->sb_width);
793 to->sb_dirblklog = from->sb_dirblklog;
794 to->sb_logsectlog = from->sb_logsectlog;
795 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
796 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
797 to->sb_features2 = be32_to_cpu(from->sb_features2);
798 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
799 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
800 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
801 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
802 to->sb_features_log_incompat =
803 be32_to_cpu(from->sb_features_log_incompat);
804 /* crc is only used on disk, not in memory; just init to 0 here. */
805 to->sb_crc = 0;
806 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
807 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
808 to->sb_lsn = be64_to_cpu(from->sb_lsn);
810 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
811 * feature flag is set; if not set we keep it only in memory.
813 if (xfs_sb_is_v5(to) &&
814 (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
815 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
816 else
817 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
818 /* Convert on-disk flags to in-memory flags? */
819 if (convert_xquota)
820 xfs_sb_quota_from_disk(to);
822 if (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
823 to->sb_metadirino = be64_to_cpu(from->sb_metadirino);
824 to->sb_rgblklog = from->sb_rgblklog;
825 memcpy(to->sb_pad, from->sb_pad, sizeof(to->sb_pad));
826 to->sb_rgcount = be32_to_cpu(from->sb_rgcount);
827 to->sb_rgextents = be32_to_cpu(from->sb_rgextents);
828 to->sb_rbmino = NULLFSINO;
829 to->sb_rsumino = NULLFSINO;
830 } else {
831 to->sb_metadirino = NULLFSINO;
832 to->sb_rgcount = 1;
833 to->sb_rgextents = 0;
837 void
838 xfs_sb_from_disk(
839 struct xfs_sb *to,
840 struct xfs_dsb *from)
842 __xfs_sb_from_disk(to, from, true);
845 static void
846 xfs_sb_quota_to_disk(
847 struct xfs_dsb *to,
848 struct xfs_sb *from)
850 uint16_t qflags = from->sb_qflags;
852 if (xfs_sb_is_v5(from) &&
853 (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
854 to->sb_qflags = cpu_to_be16(from->sb_qflags);
855 to->sb_uquotino = cpu_to_be64(0);
856 to->sb_gquotino = cpu_to_be64(0);
857 to->sb_pquotino = cpu_to_be64(0);
858 return;
861 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
864 * The in-memory superblock quota state matches the v5 on-disk format so
865 * just write them out and return
867 if (xfs_sb_is_v5(from)) {
868 to->sb_qflags = cpu_to_be16(from->sb_qflags);
869 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
870 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
871 return;
875 * For older superblocks (v4), the in-core version of sb_qflags do not
876 * have XFS_OQUOTA_* flags, whereas the on-disk version does. So,
877 * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
879 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
880 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
882 if (from->sb_qflags &
883 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
884 qflags |= XFS_OQUOTA_ENFD;
885 if (from->sb_qflags &
886 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
887 qflags |= XFS_OQUOTA_CHKD;
888 to->sb_qflags = cpu_to_be16(qflags);
891 * GQUOTINO and PQUOTINO cannot be used together in versions
892 * of superblock that do not have pquotino. from->sb_flags
893 * tells us which quota is active and should be copied to
894 * disk. If neither are active, we should NULL the inode.
896 * In all cases, the separate pquotino must remain 0 because it
897 * is beyond the "end" of the valid non-pquotino superblock.
899 if (from->sb_qflags & XFS_GQUOTA_ACCT)
900 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
901 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
902 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
903 else {
905 * We can't rely on just the fields being logged to tell us
906 * that it is safe to write NULLFSINO - we should only do that
907 * if quotas are not actually enabled. Hence only write
908 * NULLFSINO if both in-core quota inodes are NULL.
910 if (from->sb_gquotino == NULLFSINO &&
911 from->sb_pquotino == NULLFSINO)
912 to->sb_gquotino = cpu_to_be64(NULLFSINO);
915 to->sb_pquotino = 0;
918 void
919 xfs_sb_to_disk(
920 struct xfs_dsb *to,
921 struct xfs_sb *from)
923 xfs_sb_quota_to_disk(to, from);
925 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
926 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
927 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
928 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
929 to->sb_rextents = cpu_to_be64(from->sb_rextents);
930 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
931 to->sb_logstart = cpu_to_be64(from->sb_logstart);
932 to->sb_rootino = cpu_to_be64(from->sb_rootino);
933 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
934 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
935 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
936 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
937 to->sb_agcount = cpu_to_be32(from->sb_agcount);
938 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
939 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
940 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
941 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
942 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
943 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
944 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
945 to->sb_blocklog = from->sb_blocklog;
946 to->sb_sectlog = from->sb_sectlog;
947 to->sb_inodelog = from->sb_inodelog;
948 to->sb_inopblog = from->sb_inopblog;
949 to->sb_agblklog = from->sb_agblklog;
950 to->sb_rextslog = from->sb_rextslog;
951 to->sb_inprogress = from->sb_inprogress;
952 to->sb_imax_pct = from->sb_imax_pct;
953 to->sb_icount = cpu_to_be64(from->sb_icount);
954 to->sb_ifree = cpu_to_be64(from->sb_ifree);
955 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
956 to->sb_frextents = cpu_to_be64(from->sb_frextents);
958 to->sb_flags = from->sb_flags;
959 to->sb_shared_vn = from->sb_shared_vn;
960 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
961 to->sb_unit = cpu_to_be32(from->sb_unit);
962 to->sb_width = cpu_to_be32(from->sb_width);
963 to->sb_dirblklog = from->sb_dirblklog;
964 to->sb_logsectlog = from->sb_logsectlog;
965 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
966 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
969 * We need to ensure that bad_features2 always matches features2.
970 * Hence we enforce that here rather than having to remember to do it
971 * everywhere else that updates features2.
973 from->sb_bad_features2 = from->sb_features2;
974 to->sb_features2 = cpu_to_be32(from->sb_features2);
975 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
977 if (!xfs_sb_is_v5(from))
978 return;
980 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
981 to->sb_features_ro_compat =
982 cpu_to_be32(from->sb_features_ro_compat);
983 to->sb_features_incompat =
984 cpu_to_be32(from->sb_features_incompat);
985 to->sb_features_log_incompat =
986 cpu_to_be32(from->sb_features_log_incompat);
987 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
988 to->sb_lsn = cpu_to_be64(from->sb_lsn);
989 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
990 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
992 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR) {
993 to->sb_metadirino = cpu_to_be64(from->sb_metadirino);
994 to->sb_rgblklog = from->sb_rgblklog;
995 memset(to->sb_pad, 0, sizeof(to->sb_pad));
996 to->sb_rgcount = cpu_to_be32(from->sb_rgcount);
997 to->sb_rgextents = cpu_to_be32(from->sb_rgextents);
998 to->sb_rbmino = cpu_to_be64(0);
999 to->sb_rsumino = cpu_to_be64(0);
1004 * If the superblock has the CRC feature bit set or the CRC field is non-null,
1005 * check that the CRC is valid. We check the CRC field is non-null because a
1006 * single bit error could clear the feature bit and unused parts of the
1007 * superblock are supposed to be zero. Hence a non-null crc field indicates that
1008 * we've potentially lost a feature bit and we should check it anyway.
1010 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
1011 * last field in V4 secondary superblocks. So for secondary superblocks,
1012 * we are more forgiving, and ignore CRC failures if the primary doesn't
1013 * indicate that the fs version is V5.
1015 static void
1016 xfs_sb_read_verify(
1017 struct xfs_buf *bp)
1019 struct xfs_sb sb;
1020 struct xfs_mount *mp = bp->b_mount;
1021 struct xfs_dsb *dsb = bp->b_addr;
1022 int error;
1025 * open code the version check to avoid needing to convert the entire
1026 * superblock from disk order just to check the version number
1028 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
1029 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
1030 XFS_SB_VERSION_5) ||
1031 dsb->sb_crc != 0)) {
1033 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
1034 /* Only fail bad secondaries on a known V5 filesystem */
1035 if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
1036 xfs_has_crc(mp)) {
1037 error = -EFSBADCRC;
1038 goto out_error;
1044 * Check all the superblock fields. Don't byteswap the xquota flags
1045 * because _verify_common checks the on-disk values.
1047 __xfs_sb_from_disk(&sb, dsb, false);
1048 error = xfs_validate_sb_common(mp, bp, &sb);
1049 if (error)
1050 goto out_error;
1051 error = xfs_validate_sb_read(mp, &sb);
1053 out_error:
1054 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
1055 xfs_verifier_error(bp, error, __this_address);
1056 else if (error)
1057 xfs_buf_ioerror(bp, error);
1061 * We may be probed for a filesystem match, so we may not want to emit
1062 * messages when the superblock buffer is not actually an XFS superblock.
1063 * If we find an XFS superblock, then run a normal, noisy mount because we are
1064 * really going to mount it and want to know about errors.
1066 static void
1067 xfs_sb_quiet_read_verify(
1068 struct xfs_buf *bp)
1070 struct xfs_dsb *dsb = bp->b_addr;
1072 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
1073 /* XFS filesystem, verify noisily! */
1074 xfs_sb_read_verify(bp);
1075 return;
1077 /* quietly fail */
1078 xfs_buf_ioerror(bp, -EWRONGFS);
1081 static void
1082 xfs_sb_write_verify(
1083 struct xfs_buf *bp)
1085 struct xfs_sb sb;
1086 struct xfs_mount *mp = bp->b_mount;
1087 struct xfs_buf_log_item *bip = bp->b_log_item;
1088 struct xfs_dsb *dsb = bp->b_addr;
1089 int error;
1092 * Check all the superblock fields. Don't byteswap the xquota flags
1093 * because _verify_common checks the on-disk values.
1095 __xfs_sb_from_disk(&sb, dsb, false);
1096 error = xfs_validate_sb_common(mp, bp, &sb);
1097 if (error)
1098 goto out_error;
1099 error = xfs_validate_sb_write(mp, bp, &sb);
1100 if (error)
1101 goto out_error;
1103 if (!xfs_sb_is_v5(&sb))
1104 return;
1106 if (bip)
1107 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
1109 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
1110 return;
1112 out_error:
1113 xfs_verifier_error(bp, error, __this_address);
1116 const struct xfs_buf_ops xfs_sb_buf_ops = {
1117 .name = "xfs_sb",
1118 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
1119 .verify_read = xfs_sb_read_verify,
1120 .verify_write = xfs_sb_write_verify,
1123 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
1124 .name = "xfs_sb_quiet",
1125 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
1126 .verify_read = xfs_sb_quiet_read_verify,
1127 .verify_write = xfs_sb_write_verify,
1130 /* Compute cached rt geometry from the incore sb. */
1131 void
1132 xfs_sb_mount_rextsize(
1133 struct xfs_mount *mp,
1134 struct xfs_sb *sbp)
1136 struct xfs_groups *rgs = &mp->m_groups[XG_TYPE_RTG];
1138 mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize);
1139 mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize);
1141 if (xfs_sb_is_v5(sbp) &&
1142 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR)) {
1143 rgs->blocks = sbp->sb_rgextents * sbp->sb_rextsize;
1144 rgs->blklog = mp->m_sb.sb_rgblklog;
1145 rgs->blkmask = xfs_mask32lo(mp->m_sb.sb_rgblklog);
1146 } else {
1147 rgs->blocks = 0;
1148 rgs->blklog = 0;
1149 rgs->blkmask = (uint64_t)-1;
1153 /* Update incore sb rt extent size, then recompute the cached rt geometry. */
1154 void
1155 xfs_mount_sb_set_rextsize(
1156 struct xfs_mount *mp,
1157 struct xfs_sb *sbp,
1158 xfs_agblock_t rextsize)
1160 sbp->sb_rextsize = rextsize;
1161 if (xfs_sb_is_v5(sbp) &&
1162 (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_METADIR))
1163 sbp->sb_rgblklog = xfs_compute_rgblklog(sbp->sb_rgextents,
1164 rextsize);
1166 xfs_sb_mount_rextsize(mp, sbp);
1170 * xfs_mount_common
1172 * Mount initialization code establishing various mount
1173 * fields from the superblock associated with the given
1174 * mount structure.
1176 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
1178 void
1179 xfs_sb_mount_common(
1180 struct xfs_mount *mp,
1181 struct xfs_sb *sbp)
1183 struct xfs_groups *ags = &mp->m_groups[XG_TYPE_AG];
1185 mp->m_agfrotor = 0;
1186 atomic_set(&mp->m_agirotor, 0);
1187 mp->m_maxagi = mp->m_sb.sb_agcount;
1188 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
1189 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
1190 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
1191 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
1192 mp->m_blockmask = sbp->sb_blocksize - 1;
1193 mp->m_blockwsize = xfs_rtbmblock_size(sbp) >> XFS_WORDLOG;
1194 mp->m_rtx_per_rbmblock = mp->m_blockwsize << XFS_NBWORDLOG;
1196 ags->blocks = mp->m_sb.sb_agblocks;
1197 ags->blklog = mp->m_sb.sb_agblklog;
1198 ags->blkmask = xfs_mask32lo(mp->m_sb.sb_agblklog);
1200 xfs_sb_mount_rextsize(mp, sbp);
1202 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, true);
1203 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, false);
1204 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
1205 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
1207 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, true);
1208 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, false);
1209 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
1210 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
1212 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, true);
1213 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(mp, sbp->sb_blocksize, false);
1214 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
1215 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
1217 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, true);
1218 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(mp, sbp->sb_blocksize, false);
1219 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
1220 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
1222 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
1223 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
1224 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
1228 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
1229 * into the superblock buffer to be logged. It does not provide the higher
1230 * level of locking that is needed to protect the in-core superblock from
1231 * concurrent access.
1233 void
1234 xfs_log_sb(
1235 struct xfs_trans *tp)
1237 struct xfs_mount *mp = tp->t_mountp;
1238 struct xfs_buf *bp = xfs_trans_getsb(tp);
1241 * Lazy sb counters don't update the in-core superblock so do that now.
1242 * If this is at unmount, the counters will be exactly correct, but at
1243 * any other time they will only be ballpark correct because of
1244 * reservations that have been taken out percpu counters. If we have an
1245 * unclean shutdown, this will be corrected by log recovery rebuilding
1246 * the counters from the AGF block counts.
1248 if (xfs_has_lazysbcount(mp)) {
1249 mp->m_sb.sb_icount = percpu_counter_sum_positive(&mp->m_icount);
1250 mp->m_sb.sb_ifree = min_t(uint64_t,
1251 percpu_counter_sum_positive(&mp->m_ifree),
1252 mp->m_sb.sb_icount);
1253 mp->m_sb.sb_fdblocks =
1254 percpu_counter_sum_positive(&mp->m_fdblocks);
1258 * sb_frextents was added to the lazy sb counters when the rt groups
1259 * feature was introduced. This counter can go negative due to the way
1260 * we handle nearly-lockless reservations, so we must use the _positive
1261 * variant here to avoid writing out nonsense frextents.
1263 if (xfs_has_rtgroups(mp))
1264 mp->m_sb.sb_frextents =
1265 percpu_counter_sum_positive(&mp->m_frextents);
1267 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1268 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
1269 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
1273 * xfs_sync_sb
1275 * Sync the superblock to disk.
1277 * Note that the caller is responsible for checking the frozen state of the
1278 * filesystem. This procedure uses the non-blocking transaction allocator and
1279 * thus will allow modifications to a frozen fs. This is required because this
1280 * code can be called during the process of freezing where use of the high-level
1281 * allocator would deadlock.
1284 xfs_sync_sb(
1285 struct xfs_mount *mp,
1286 bool wait)
1288 struct xfs_trans *tp;
1289 int error;
1291 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
1292 XFS_TRANS_NO_WRITECOUNT, &tp);
1293 if (error)
1294 return error;
1296 xfs_log_sb(tp);
1297 if (wait)
1298 xfs_trans_set_sync(tp);
1299 return xfs_trans_commit(tp);
1303 * Update all the secondary superblocks to match the new state of the primary.
1304 * Because we are completely overwriting all the existing fields in the
1305 * secondary superblock buffers, there is no need to read them in from disk.
1306 * Just get a new buffer, stamp it and write it.
1308 * The sb buffers need to be cached here so that we serialise against other
1309 * operations that access the secondary superblocks, but we don't want to keep
1310 * them in memory once it is written so we mark it as a one-shot buffer.
1313 xfs_update_secondary_sbs(
1314 struct xfs_mount *mp)
1316 struct xfs_perag *pag = NULL;
1317 int saved_error = 0;
1318 int error = 0;
1319 LIST_HEAD (buffer_list);
1321 /* update secondary superblocks. */
1322 while ((pag = xfs_perag_next_from(mp, pag, 1))) {
1323 struct xfs_buf *bp;
1325 error = xfs_buf_get(mp->m_ddev_targp,
1326 XFS_AG_DADDR(mp, pag_agno(pag), XFS_SB_DADDR),
1327 XFS_FSS_TO_BB(mp, 1), &bp);
1329 * If we get an error reading or writing alternate superblocks,
1330 * continue. xfs_repair chooses the "best" superblock based
1331 * on most matches; if we break early, we'll leave more
1332 * superblocks un-updated than updated, and xfs_repair may
1333 * pick them over the properly-updated primary.
1335 if (error) {
1336 xfs_warn(mp,
1337 "error allocating secondary superblock for ag %d",
1338 pag_agno(pag));
1339 if (!saved_error)
1340 saved_error = error;
1341 continue;
1344 bp->b_ops = &xfs_sb_buf_ops;
1345 xfs_buf_oneshot(bp);
1346 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1347 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
1348 xfs_buf_delwri_queue(bp, &buffer_list);
1349 xfs_buf_relse(bp);
1351 /* don't hold too many buffers at once */
1352 if (pag_agno(pag) % 16)
1353 continue;
1355 error = xfs_buf_delwri_submit(&buffer_list);
1356 if (error) {
1357 xfs_warn(mp,
1358 "write error %d updating a secondary superblock near ag %d",
1359 error, pag_agno(pag));
1360 if (!saved_error)
1361 saved_error = error;
1362 continue;
1365 error = xfs_buf_delwri_submit(&buffer_list);
1366 if (error)
1367 xfs_warn(mp, "error %d writing secondary superblocks", error);
1368 return saved_error ? saved_error : error;
1372 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1373 * also writes the superblock buffer to disk sector 0 immediately.
1376 xfs_sync_sb_buf(
1377 struct xfs_mount *mp,
1378 bool update_rtsb)
1380 struct xfs_trans *tp;
1381 struct xfs_buf *bp;
1382 struct xfs_buf *rtsb_bp = NULL;
1383 int error;
1385 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1386 if (error)
1387 return error;
1389 bp = xfs_trans_getsb(tp);
1390 xfs_log_sb(tp);
1391 xfs_trans_bhold(tp, bp);
1392 if (update_rtsb) {
1393 rtsb_bp = xfs_log_rtsb(tp, bp);
1394 if (rtsb_bp)
1395 xfs_trans_bhold(tp, rtsb_bp);
1397 xfs_trans_set_sync(tp);
1398 error = xfs_trans_commit(tp);
1399 if (error)
1400 goto out;
1402 * write out the sb buffer to get the changes to disk
1404 error = xfs_bwrite(bp);
1405 if (!error && rtsb_bp)
1406 error = xfs_bwrite(rtsb_bp);
1407 out:
1408 if (rtsb_bp)
1409 xfs_buf_relse(rtsb_bp);
1410 xfs_buf_relse(bp);
1411 return error;
1414 void
1415 xfs_fs_geometry(
1416 struct xfs_mount *mp,
1417 struct xfs_fsop_geom *geo,
1418 int struct_version)
1420 struct xfs_sb *sbp = &mp->m_sb;
1422 memset(geo, 0, sizeof(struct xfs_fsop_geom));
1424 geo->blocksize = sbp->sb_blocksize;
1425 geo->rtextsize = sbp->sb_rextsize;
1426 geo->agblocks = sbp->sb_agblocks;
1427 geo->agcount = sbp->sb_agcount;
1428 geo->logblocks = sbp->sb_logblocks;
1429 geo->sectsize = sbp->sb_sectsize;
1430 geo->inodesize = sbp->sb_inodesize;
1431 geo->imaxpct = sbp->sb_imax_pct;
1432 geo->datablocks = sbp->sb_dblocks;
1433 geo->rtblocks = sbp->sb_rblocks;
1434 geo->rtextents = sbp->sb_rextents;
1435 geo->logstart = sbp->sb_logstart;
1436 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1437 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1439 if (struct_version < 2)
1440 return;
1442 geo->sunit = sbp->sb_unit;
1443 geo->swidth = sbp->sb_width;
1445 if (struct_version < 3)
1446 return;
1448 geo->version = XFS_FSOP_GEOM_VERSION;
1449 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1450 XFS_FSOP_GEOM_FLAGS_DIRV2 |
1451 XFS_FSOP_GEOM_FLAGS_EXTFLG;
1452 if (xfs_has_attr(mp))
1453 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1454 if (xfs_has_quota(mp))
1455 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1456 if (xfs_has_align(mp))
1457 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1458 if (xfs_has_dalign(mp))
1459 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1460 if (xfs_has_asciici(mp))
1461 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1462 if (xfs_has_lazysbcount(mp))
1463 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1464 if (xfs_has_attr2(mp))
1465 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1466 if (xfs_has_projid32(mp))
1467 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1468 if (xfs_has_crc(mp))
1469 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1470 if (xfs_has_ftype(mp))
1471 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1472 if (xfs_has_finobt(mp))
1473 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1474 if (xfs_has_sparseinodes(mp))
1475 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1476 if (xfs_has_rmapbt(mp))
1477 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1478 if (xfs_has_reflink(mp))
1479 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1480 if (xfs_has_bigtime(mp))
1481 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
1482 if (xfs_has_inobtcounts(mp))
1483 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
1484 if (xfs_has_parent(mp))
1485 geo->flags |= XFS_FSOP_GEOM_FLAGS_PARENT;
1486 if (xfs_has_sector(mp)) {
1487 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1488 geo->logsectsize = sbp->sb_logsectsize;
1489 } else {
1490 geo->logsectsize = BBSIZE;
1492 if (xfs_has_large_extent_counts(mp))
1493 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
1494 if (xfs_has_exchange_range(mp))
1495 geo->flags |= XFS_FSOP_GEOM_FLAGS_EXCHANGE_RANGE;
1496 if (xfs_has_metadir(mp))
1497 geo->flags |= XFS_FSOP_GEOM_FLAGS_METADIR;
1498 geo->rtsectsize = sbp->sb_blocksize;
1499 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1501 if (struct_version < 4)
1502 return;
1504 if (xfs_has_logv2(mp))
1505 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1507 geo->logsunit = sbp->sb_logsunit;
1509 if (struct_version < 5)
1510 return;
1512 geo->version = XFS_FSOP_GEOM_VERSION_V5;
1514 if (xfs_has_rtgroups(mp)) {
1515 geo->rgcount = sbp->sb_rgcount;
1516 geo->rgextents = sbp->sb_rgextents;
1520 /* Read a secondary superblock. */
1522 xfs_sb_read_secondary(
1523 struct xfs_mount *mp,
1524 struct xfs_trans *tp,
1525 xfs_agnumber_t agno,
1526 struct xfs_buf **bpp)
1528 struct xfs_buf *bp;
1529 int error;
1531 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1532 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1533 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1534 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1535 if (xfs_metadata_is_sick(error))
1536 xfs_agno_mark_sick(mp, agno, XFS_SICK_AG_SB);
1537 if (error)
1538 return error;
1539 xfs_buf_set_ref(bp, XFS_SSB_REF);
1540 *bpp = bp;
1541 return 0;
1544 /* Get an uninitialised secondary superblock buffer. */
1546 xfs_sb_get_secondary(
1547 struct xfs_mount *mp,
1548 struct xfs_trans *tp,
1549 xfs_agnumber_t agno,
1550 struct xfs_buf **bpp)
1552 struct xfs_buf *bp;
1553 int error;
1555 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1556 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1557 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1558 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1559 if (error)
1560 return error;
1561 bp->b_ops = &xfs_sb_buf_ops;
1562 xfs_buf_oneshot(bp);
1563 *bpp = bp;
1564 return 0;
1568 * sunit, swidth, sectorsize(optional with 0) should be all in bytes, so users
1569 * won't be confused by values in error messages. This function returns false
1570 * if the stripe geometry is invalid and the caller is unable to repair the
1571 * stripe configuration later in the mount process.
1573 bool
1574 xfs_validate_stripe_geometry(
1575 struct xfs_mount *mp,
1576 __s64 sunit,
1577 __s64 swidth,
1578 int sectorsize,
1579 bool may_repair,
1580 bool silent)
1582 if (swidth > INT_MAX) {
1583 if (!silent)
1584 xfs_notice(mp,
1585 "stripe width (%lld) is too large", swidth);
1586 goto check_override;
1589 if (sunit > swidth) {
1590 if (!silent)
1591 xfs_notice(mp,
1592 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1593 goto check_override;
1596 if (sectorsize && (int)sunit % sectorsize) {
1597 if (!silent)
1598 xfs_notice(mp,
1599 "stripe unit (%lld) must be a multiple of the sector size (%d)",
1600 sunit, sectorsize);
1601 goto check_override;
1604 if (sunit && !swidth) {
1605 if (!silent)
1606 xfs_notice(mp,
1607 "invalid stripe unit (%lld) and stripe width of 0", sunit);
1608 goto check_override;
1611 if (!sunit && swidth) {
1612 if (!silent)
1613 xfs_notice(mp,
1614 "invalid stripe width (%lld) and stripe unit of 0", swidth);
1615 goto check_override;
1618 if (sunit && (int)swidth % (int)sunit) {
1619 if (!silent)
1620 xfs_notice(mp,
1621 "stripe width (%lld) must be a multiple of the stripe unit (%lld)",
1622 swidth, sunit);
1623 goto check_override;
1625 return true;
1627 check_override:
1628 if (!may_repair)
1629 return false;
1631 * During mount, mp->m_dalign will not be set unless the sunit mount
1632 * option was set. If it was set, ignore the bad stripe alignment values
1633 * and allow the validation and overwrite later in the mount process to
1634 * attempt to overwrite the bad stripe alignment values with the values
1635 * supplied by mount options.
1637 if (!mp->m_dalign)
1638 return false;
1639 if (!silent)
1640 xfs_notice(mp,
1641 "Will try to correct with specified mount options sunit (%d) and swidth (%d)",
1642 BBTOB(mp->m_dalign), BBTOB(mp->m_swidth));
1643 return true;
1647 * Compute the maximum level number of the realtime summary file, as defined by
1648 * mkfs. The historic use of highbit32 on a 64-bit quantity prohibited correct
1649 * use of rt volumes with more than 2^32 extents.
1651 uint8_t
1652 xfs_compute_rextslog(
1653 xfs_rtbxlen_t rtextents)
1655 if (!rtextents)
1656 return 0;
1657 return xfs_highbit64(rtextents);