]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - fs/xfs/libxfs/xfs_sb.c
Merge tag 'kvm-x86-docs-6.7' of https://github.com/kvm-x86/linux into HEAD
[thirdparty/kernel/stable.git] / fs / xfs / libxfs / xfs_sb.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
ff55068c
DC
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
ff55068c
DC
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
632b89e8 8#include "xfs_shared.h"
ff55068c 9#include "xfs_format.h"
239880ef
DC
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
ff55068c 12#include "xfs_bit.h"
5f213ddb 13#include "xfs_sb.h"
ff55068c 14#include "xfs_mount.h"
ff55068c
DC
15#include "xfs_ialloc.h"
16#include "xfs_alloc.h"
ff55068c 17#include "xfs_error.h"
239880ef 18#include "xfs_trans.h"
ff55068c 19#include "xfs_buf_item.h"
a4fbe6ab
DC
20#include "xfs_bmap_btree.h"
21#include "xfs_alloc_btree.h"
a45086e2 22#include "xfs_log.h"
035e00ac 23#include "xfs_rmap_btree.h"
1946b91c 24#include "xfs_refcount_btree.h"
c368ebcd 25#include "xfs_da_format.h"
39353ff6 26#include "xfs_health.h"
7f8d3b3c 27#include "xfs_ag.h"
ff55068c
DC
28
29/*
30 * Physical superblock buffer manipulations. Shared with libxfs in userspace.
31 */
32
f0f5f658
DC
33/*
34 * Check that all the V4 feature bits that the V5 filesystem format requires are
35 * correctly set.
36 */
37static bool
38xfs_sb_validate_v5_features(
39 struct xfs_sb *sbp)
40{
41 /* We must not have any unknown V4 feature bits set */
42 if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
43 return false;
44
45 /*
46 * The CRC bit is considered an invalid V4 flag, so we have to add it
47 * manually to the OKBITS mask.
48 */
49 if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
50 XFS_SB_VERSION2_CRCBIT))
51 return false;
52
53 /* Now check all the required V4 feature flags are set. */
54
55#define V5_VERS_FLAGS (XFS_SB_VERSION_NLINKBIT | \
56 XFS_SB_VERSION_ALIGNBIT | \
57 XFS_SB_VERSION_LOGV2BIT | \
58 XFS_SB_VERSION_EXTFLGBIT | \
59 XFS_SB_VERSION_DIRV2BIT | \
60 XFS_SB_VERSION_MOREBITSBIT)
61
62#define V5_FEAT_FLAGS (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \
63 XFS_SB_VERSION2_ATTR2BIT | \
64 XFS_SB_VERSION2_PROJID32BIT | \
65 XFS_SB_VERSION2_CRCBIT)
66
67 if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
68 return false;
69 if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
70 return false;
71 return true;
72}
73
fe08cc50 74/*
aa880198
DC
75 * We current support XFS v5 formats with known features and v4 superblocks with
76 * at least V2 directories.
fe08cc50
DC
77 */
78bool
79xfs_sb_good_version(
80 struct xfs_sb *sbp)
81{
f0f5f658
DC
82 /*
83 * All v5 filesystems are supported, but we must check that all the
84 * required v4 feature flags are enabled correctly as the code checks
85 * those flags and not for v5 support.
86 */
d6837c1a 87 if (xfs_sb_is_v5(sbp))
f0f5f658
DC
88 return xfs_sb_validate_v5_features(sbp);
89
aa880198
DC
90 /* versions prior to v4 are not supported */
91 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
92 return false;
93
f0f5f658
DC
94 /* We must not have any unknown v4 feature bits set */
95 if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
96 ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
97 (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
98 return false;
fe08cc50 99
fe08cc50
DC
100 /* V4 filesystems need v2 directories and unwritten extents */
101 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
102 return false;
103 if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
104 return false;
105
fe08cc50
DC
106 /* It's a supported v4 filesystem */
107 return true;
108}
109
a1d86e8d
DC
110uint64_t
111xfs_sb_version_to_features(
112 struct xfs_sb *sbp)
113{
114 uint64_t features = 0;
115
116 /* optional V4 features */
117 if (sbp->sb_rblocks > 0)
118 features |= XFS_FEAT_REALTIME;
dd0d2f97
DC
119 if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
120 features |= XFS_FEAT_NLINK;
a1d86e8d
DC
121 if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
122 features |= XFS_FEAT_ATTR;
123 if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
124 features |= XFS_FEAT_QUOTA;
125 if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
126 features |= XFS_FEAT_ALIGN;
127 if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
128 features |= XFS_FEAT_LOGV2;
129 if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
130 features |= XFS_FEAT_DALIGN;
131 if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
132 features |= XFS_FEAT_EXTFLG;
133 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
134 features |= XFS_FEAT_SECTOR;
135 if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
136 features |= XFS_FEAT_ASCIICI;
137 if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
138 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
139 features |= XFS_FEAT_LAZYSBCOUNT;
140 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
141 features |= XFS_FEAT_ATTR2;
142 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
143 features |= XFS_FEAT_PROJID32;
144 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
145 features |= XFS_FEAT_FTYPE;
146 }
147
d6837c1a 148 if (!xfs_sb_is_v5(sbp))
a1d86e8d
DC
149 return features;
150
151 /* Always on V5 features */
152 features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
153 XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
154 XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
155
156 /* Optional V5 features */
157 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
158 features |= XFS_FEAT_FINOBT;
159 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
160 features |= XFS_FEAT_RMAPBT;
161 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
162 features |= XFS_FEAT_REFLINK;
163 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
164 features |= XFS_FEAT_INOBTCNT;
165 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
166 features |= XFS_FEAT_FTYPE;
167 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
168 features |= XFS_FEAT_SPINODES;
169 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
170 features |= XFS_FEAT_META_UUID;
171 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
172 features |= XFS_FEAT_BIGTIME;
173 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
174 features |= XFS_FEAT_NEEDSREPAIR;
919819f5
CB
175 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
176 features |= XFS_FEAT_NREXT64;
177
a1d86e8d
DC
178 return features;
179}
180
eca383fc 181/* Check all the superblock fields we care about when reading one in. */
ff55068c 182STATIC int
eca383fc
DW
183xfs_validate_sb_read(
184 struct xfs_mount *mp,
185 struct xfs_sb *sbp)
ff55068c 186{
d6837c1a 187 if (!xfs_sb_is_v5(sbp))
eca383fc 188 return 0;
ff55068c
DC
189
190 /*
eca383fc
DW
191 * Version 5 superblock feature mask validation. Reject combinations
192 * the kernel cannot support up front before checking anything else.
ff55068c 193 */
eca383fc
DW
194 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
195 xfs_warn(mp,
f41febd2 196"Superblock has unknown compatible features (0x%x) enabled.",
eca383fc
DW
197 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
198 xfs_warn(mp,
f41febd2 199"Using a more recent kernel is recommended.");
eca383fc 200 }
ff55068c 201
eca383fc
DW
202 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
203 xfs_alert(mp,
ff55068c 204"Superblock has unknown read-only compatible features (0x%x) enabled.",
eca383fc
DW
205 (sbp->sb_features_ro_compat &
206 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
2e973b2c 207 if (!xfs_is_readonly(mp)) {
eca383fc 208 xfs_warn(mp,
f41febd2 209"Attempted to mount read-only compatible filesystem read-write.");
eca383fc 210 xfs_warn(mp,
ff55068c 211"Filesystem can only be safely mounted read only.");
f41febd2 212
2451337d 213 return -EINVAL;
ff55068c 214 }
eca383fc
DW
215 }
216 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
217 xfs_warn(mp,
218"Superblock has unknown incompatible features (0x%x) enabled.",
219 (sbp->sb_features_incompat &
220 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
221 xfs_warn(mp,
222"Filesystem cannot be safely mounted by this kernel.");
223 return -EINVAL;
224 }
225
226 return 0;
227}
228
229/* Check all the superblock fields we care about when writing one out. */
230STATIC int
231xfs_validate_sb_write(
232 struct xfs_mount *mp,
1f31c98d 233 struct xfs_buf *bp,
eca383fc
DW
234 struct xfs_sb *sbp)
235{
8756a5af
BD
236 /*
237 * Carry out additional sb summary counter sanity checks when we write
238 * the superblock. We skip this in the read validator because there
239 * could be newer superblocks in the log and if the values are garbage
69775fd1 240 * even after replay we'll recalculate them at the end of log mount.
1f31c98d
DW
241 *
242 * mkfs has traditionally written zeroed counters to inprogress and
243 * secondary superblocks, so allow this usage to continue because
244 * we never read counters from such superblocks.
8756a5af 245 */
04fcad80 246 if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
1f31c98d
DW
247 (sbp->sb_fdblocks > sbp->sb_dblocks ||
248 !xfs_verify_icount(mp, sbp->sb_icount) ||
249 sbp->sb_ifree > sbp->sb_icount)) {
8756a5af
BD
250 xfs_warn(mp, "SB summary counter sanity check failed");
251 return -EFSCORRUPTED;
252 }
253
d6837c1a 254 if (!xfs_sb_is_v5(sbp))
eca383fc
DW
255 return 0;
256
9e037cb7
DW
257 /*
258 * Version 5 superblock feature mask validation. Reject combinations
259 * the kernel cannot support since we checked for unsupported bits in
260 * the read verifier, which means that memory is corrupt.
261 */
262 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
263 xfs_warn(mp,
264"Corruption detected in superblock compatible features (0x%x)!",
265 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
266 return -EFSCORRUPTED;
267 }
268
74ad4693
DW
269 if (!xfs_is_readonly(mp) &&
270 xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
9e037cb7
DW
271 xfs_alert(mp,
272"Corruption detected in superblock read-only compatible features (0x%x)!",
273 (sbp->sb_features_ro_compat &
274 XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
275 return -EFSCORRUPTED;
276 }
277 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
278 xfs_warn(mp,
279"Corruption detected in superblock incompatible features (0x%x)!",
280 (sbp->sb_features_incompat &
281 XFS_SB_FEAT_INCOMPAT_UNKNOWN));
282 return -EFSCORRUPTED;
283 }
284 if (xfs_sb_has_incompat_log_feature(sbp,
285 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
286 xfs_warn(mp,
287"Corruption detected in superblock incompatible log features (0x%x)!",
288 (sbp->sb_features_log_incompat &
289 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
290 return -EFSCORRUPTED;
291 }
eca383fc
DW
292
293 /*
294 * We can't read verify the sb LSN because the read verifier is called
295 * before the log is allocated and processed. We know the log is set up
296 * before write verifier calls, so check it here.
297 */
298 if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
299 return -EFSCORRUPTED;
300
301 return 0;
302}
303
304/* Check the validity of the SB. */
305STATIC int
306xfs_validate_sb_common(
307 struct xfs_mount *mp,
308 struct xfs_buf *bp,
309 struct xfs_sb *sbp)
310{
3e6e8afd 311 struct xfs_dsb *dsb = bp->b_addr;
eca383fc
DW
312 uint32_t agcount = 0;
313 uint32_t rem;
fe08cc50 314 bool has_dalign;
eca383fc 315
39708c20 316 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
f0f5f658
DC
317 xfs_warn(mp,
318"Superblock has bad magic number 0x%x. Not an XFS filesystem?",
319 be32_to_cpu(dsb->sb_magicnum));
eca383fc
DW
320 return -EWRONGFS;
321 }
322
323 if (!xfs_sb_good_version(sbp)) {
f0f5f658
DC
324 xfs_warn(mp,
325"Superblock has unknown features enabled or corrupted feature masks.");
eca383fc 326 return -EWRONGFS;
ff55068c
DC
327 }
328
fe08cc50
DC
329 /*
330 * Validate feature flags and state
331 */
d6837c1a 332 if (xfs_sb_is_v5(sbp)) {
fe08cc50
DC
333 if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
334 xfs_notice(mp,
335"Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
336 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
337 return -EFSCORRUPTED;
338 }
339
340 /* V5 has a separate project quota inode */
ff55068c
DC
341 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
342 xfs_notice(mp,
08e96e1a 343 "Version 5 of Super block has XFS_OQUOTA bits.");
2451337d 344 return -EFSCORRUPTED;
ff55068c 345 }
fe08cc50
DC
346
347 /*
348 * Full inode chunks must be aligned to inode chunk size when
349 * sparse inodes are enabled to support the sparse chunk
350 * allocation algorithm and prevent overlapping inode records.
351 */
352 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
353 uint32_t align;
354
355 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
356 >> sbp->sb_blocklog;
357 if (sbp->sb_inoalignmt != align) {
358 xfs_warn(mp,
359"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
360 sbp->sb_inoalignmt, align);
361 return -EINVAL;
362 }
363 }
ff55068c
DC
364 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
365 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
366 xfs_notice(mp,
14506f7a 367"Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
2451337d 368 return -EFSCORRUPTED;
ff55068c
DC
369 }
370
371 if (unlikely(
372 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
373 xfs_warn(mp,
374 "filesystem is marked as having an external log; "
375 "specify logdev on the mount command line.");
2451337d 376 return -EINVAL;
ff55068c
DC
377 }
378
379 if (unlikely(
380 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
381 xfs_warn(mp,
382 "filesystem is marked as having an internal log; "
383 "do not specify logdev on the mount command line.");
2451337d 384 return -EINVAL;
ff55068c
DC
385 }
386
4bb73d01
DW
387 /* Compute agcount for this number of dblocks and agblocks */
388 if (sbp->sb_agblocks) {
389 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
390 if (rem)
391 agcount++;
392 }
393
ff55068c
DC
394 /*
395 * More sanity checking. Most of these were stolen directly from
396 * xfs_repair.
397 */
398 if (unlikely(
399 sbp->sb_agcount <= 0 ||
400 sbp->sb_sectsize < XFS_MIN_SECTORSIZE ||
401 sbp->sb_sectsize > XFS_MAX_SECTORSIZE ||
402 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG ||
403 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG ||
404 sbp->sb_sectsize != (1 << sbp->sb_sectlog) ||
405 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE ||
406 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE ||
407 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG ||
408 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
409 sbp->sb_blocksize != (1 << sbp->sb_blocklog) ||
83d230eb 410 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
ff55068c
DC
411 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE ||
412 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE ||
413 sbp->sb_inodelog < XFS_DINODE_MIN_LOG ||
414 sbp->sb_inodelog > XFS_DINODE_MAX_LOG ||
415 sbp->sb_inodesize != (1 << sbp->sb_inodelog) ||
392c6de9 416 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
4bb73d01
DW
417 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES ||
418 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES ||
419 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 ||
420 agcount == 0 || agcount != sbp->sb_agcount ||
ff55068c
DC
421 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) ||
422 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) ||
423 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) ||
424 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) ||
425 sbp->sb_dblocks == 0 ||
426 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) ||
ab3e57b5
DC
427 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) ||
428 sbp->sb_shared_vn != 0)) {
5ef11eb0 429 xfs_notice(mp, "SB sanity check failed");
2451337d 430 return -EFSCORRUPTED;
bec9d48d
DW
431 }
432
f1e1765a
DC
433 /*
434 * Logs that are too large are not supported at all. Reject them
435 * outright. Logs that are too small are tolerated on v4 filesystems,
436 * but we can only check that when mounting the log. Hence we skip
437 * those checks here.
438 */
439 if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) {
440 xfs_notice(mp,
441 "Log size 0x%x blocks too large, maximum size is 0x%llx blocks",
442 sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS);
443 return -EFSCORRUPTED;
444 }
445
446 if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) {
447 xfs_warn(mp,
448 "log size 0x%llx bytes too large, maximum size is 0x%llx bytes",
449 XFS_FSB_TO_B(mp, sbp->sb_logblocks),
450 XFS_MAX_LOG_BYTES);
451 return -EFSCORRUPTED;
452 }
453
454 /*
455 * Do not allow filesystems with corrupted log sector or stripe units to
456 * be mounted. We cannot safely size the iclogs or write to the log if
457 * the log stripe unit is not valid.
458 */
459 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
460 if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) {
461 xfs_notice(mp,
462 "log sector size in bytes/log2 (0x%x/0x%x) must match",
463 sbp->sb_logsectsize, 1U << sbp->sb_logsectlog);
464 return -EFSCORRUPTED;
465 }
466 } else if (sbp->sb_logsectsize || sbp->sb_logsectlog) {
467 xfs_notice(mp,
468 "log sector size in bytes/log2 (0x%x/0x%x) are not zero",
469 sbp->sb_logsectsize, sbp->sb_logsectlog);
470 return -EFSCORRUPTED;
471 }
472
473 if (sbp->sb_logsunit > 1) {
474 if (sbp->sb_logsunit % sbp->sb_blocksize) {
475 xfs_notice(mp,
476 "log stripe unit 0x%x bytes must be a multiple of block size",
477 sbp->sb_logsunit);
478 return -EFSCORRUPTED;
479 }
480 if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) {
481 xfs_notice(mp,
482 "log stripe unit 0x%x bytes over maximum size (0x%x bytes)",
483 sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE);
484 return -EFSCORRUPTED;
485 }
486 }
487
f8e566c0
DW
488 /* Validate the realtime geometry; stolen from xfs_repair */
489 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
490 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
491 xfs_notice(mp,
492 "realtime extent sanity check failed");
493 return -EFSCORRUPTED;
494 }
495
496 if (sbp->sb_rblocks == 0) {
497 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
498 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
499 xfs_notice(mp,
500 "realtime zeroed geometry check failed");
501 return -EFSCORRUPTED;
502 }
503 } else {
504 uint64_t rexts;
505 uint64_t rbmblocks;
506
507 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
508 rbmblocks = howmany_64(sbp->sb_rextents,
509 NBBY * sbp->sb_blocksize);
510
511 if (sbp->sb_rextents != rexts ||
512 sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
513 sbp->sb_rbmblocks != rbmblocks) {
514 xfs_notice(mp,
515 "realtime geometry sanity check failed");
516 return -EFSCORRUPTED;
517 }
518 }
519
7bc1fea9
GX
520 /*
521 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
522 * would imply the image is corrupted.
523 */
fe08cc50
DC
524 has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
525 if (!!sbp->sb_unit ^ has_dalign) {
fa4ca9c5
DC
526 xfs_notice(mp, "SB stripe alignment sanity check failed");
527 return -EFSCORRUPTED;
fa4ca9c5
DC
528 }
529
7bc1fea9
GX
530 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
531 XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
532 return -EFSCORRUPTED;
fa4ca9c5 533
ff55068c
DC
534 /*
535 * Currently only very few inode sizes are supported.
536 */
537 switch (sbp->sb_inodesize) {
538 case 256:
539 case 512:
540 case 1024:
541 case 2048:
542 break;
543 default:
544 xfs_warn(mp, "inode size of %d bytes not supported",
545 sbp->sb_inodesize);
2451337d 546 return -ENOSYS;
ff55068c
DC
547 }
548
ff55068c
DC
549 return 0;
550}
551
552void
553xfs_sb_quota_from_disk(struct xfs_sb *sbp)
554{
555 /*
556 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
557 * leads to in-core values having two different values for a quota
558 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
559 * NULLFSINO.
560 *
561 * Note that this change affect only the in-core values. These
562 * values are not written back to disk unless any quota information
563 * is written to the disk. Even in that case, sb_pquotino field is
564 * not written to disk unless the superblock supports pquotino.
565 */
566 if (sbp->sb_uquotino == 0)
567 sbp->sb_uquotino = NULLFSINO;
568 if (sbp->sb_gquotino == 0)
569 sbp->sb_gquotino = NULLFSINO;
570 if (sbp->sb_pquotino == 0)
571 sbp->sb_pquotino = NULLFSINO;
572
573 /*
574 * We need to do these manipilations only if we are working
575 * with an older version of on-disk superblock.
576 */
d6837c1a 577 if (xfs_sb_is_v5(sbp))
ff55068c
DC
578 return;
579
580 if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
581 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
582 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
583 if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
584 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
585 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
586 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
587
e6fc6fcf
ES
588 if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
589 sbp->sb_gquotino != NULLFSINO) {
ff55068c
DC
590 /*
591 * In older version of superblock, on-disk superblock only
592 * has sb_gquotino, and in-core superblock has both sb_gquotino
593 * and sb_pquotino. But, only one of them is supported at any
594 * point of time. So, if PQUOTA is set in disk superblock,
e6fc6fcf
ES
595 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test
596 * above is to make sure we don't do this twice and wipe them
597 * both out!
ff55068c
DC
598 */
599 sbp->sb_pquotino = sbp->sb_gquotino;
600 sbp->sb_gquotino = NULLFSINO;
601 }
602}
603
5ef828c4
ES
604static void
605__xfs_sb_from_disk(
ff55068c 606 struct xfs_sb *to,
ed67ebfd 607 struct xfs_dsb *from,
5ef828c4 608 bool convert_xquota)
ff55068c
DC
609{
610 to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
611 to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
612 to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
613 to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
614 to->sb_rextents = be64_to_cpu(from->sb_rextents);
615 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
616 to->sb_logstart = be64_to_cpu(from->sb_logstart);
617 to->sb_rootino = be64_to_cpu(from->sb_rootino);
618 to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
619 to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
620 to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
621 to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
622 to->sb_agcount = be32_to_cpu(from->sb_agcount);
623 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
624 to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
625 to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
626 to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
627 to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
628 to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
629 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
630 to->sb_blocklog = from->sb_blocklog;
631 to->sb_sectlog = from->sb_sectlog;
632 to->sb_inodelog = from->sb_inodelog;
633 to->sb_inopblog = from->sb_inopblog;
634 to->sb_agblklog = from->sb_agblklog;
635 to->sb_rextslog = from->sb_rextslog;
636 to->sb_inprogress = from->sb_inprogress;
637 to->sb_imax_pct = from->sb_imax_pct;
638 to->sb_icount = be64_to_cpu(from->sb_icount);
639 to->sb_ifree = be64_to_cpu(from->sb_ifree);
640 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
641 to->sb_frextents = be64_to_cpu(from->sb_frextents);
642 to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
643 to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
644 to->sb_qflags = be16_to_cpu(from->sb_qflags);
645 to->sb_flags = from->sb_flags;
646 to->sb_shared_vn = from->sb_shared_vn;
647 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
648 to->sb_unit = be32_to_cpu(from->sb_unit);
649 to->sb_width = be32_to_cpu(from->sb_width);
650 to->sb_dirblklog = from->sb_dirblklog;
651 to->sb_logsectlog = from->sb_logsectlog;
652 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
653 to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
654 to->sb_features2 = be32_to_cpu(from->sb_features2);
655 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
656 to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
657 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
658 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
659 to->sb_features_log_incompat =
660 be32_to_cpu(from->sb_features_log_incompat);
04dd1a0d
ES
661 /* crc is only used on disk, not in memory; just init to 0 here. */
662 to->sb_crc = 0;
fb4f2b4e 663 to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
ff55068c
DC
664 to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
665 to->sb_lsn = be64_to_cpu(from->sb_lsn);
ce748eaa
ES
666 /*
667 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
668 * feature flag is set; if not set we keep it only in memory.
669 */
d6837c1a 670 if (xfs_sb_is_v5(to) &&
fe08cc50 671 (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
ce748eaa
ES
672 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
673 else
674 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
5ef828c4
ES
675 /* Convert on-disk flags to in-memory flags? */
676 if (convert_xquota)
677 xfs_sb_quota_from_disk(to);
678}
679
680void
681xfs_sb_from_disk(
682 struct xfs_sb *to,
ed67ebfd 683 struct xfs_dsb *from)
5ef828c4
ES
684{
685 __xfs_sb_from_disk(to, from, true);
ff55068c
DC
686}
687
4d11a402 688static void
ff55068c 689xfs_sb_quota_to_disk(
4d11a402
DC
690 struct xfs_dsb *to,
691 struct xfs_sb *from)
ff55068c 692{
c8ce540d 693 uint16_t qflags = from->sb_qflags;
ff55068c 694
4d11a402 695 to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
fe08cc50
DC
696
697 /*
698 * The in-memory superblock quota state matches the v5 on-disk format so
699 * just write them out and return
700 */
d6837c1a 701 if (xfs_sb_is_v5(from)) {
4d11a402
DC
702 to->sb_qflags = cpu_to_be16(from->sb_qflags);
703 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
704 to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
705 return;
706 }
707
ff55068c 708 /*
fe08cc50
DC
709 * For older superblocks (v4), the in-core version of sb_qflags do not
710 * have XFS_OQUOTA_* flags, whereas the on-disk version does. So,
711 * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
ff55068c 712 */
4d11a402
DC
713 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
714 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
ff55068c 715
4d11a402
DC
716 if (from->sb_qflags &
717 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
718 qflags |= XFS_OQUOTA_ENFD;
719 if (from->sb_qflags &
720 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
721 qflags |= XFS_OQUOTA_CHKD;
722 to->sb_qflags = cpu_to_be16(qflags);
ff55068c
DC
723
724 /*
4d11a402
DC
725 * GQUOTINO and PQUOTINO cannot be used together in versions
726 * of superblock that do not have pquotino. from->sb_flags
727 * tells us which quota is active and should be copied to
728 * disk. If neither are active, we should NULL the inode.
03e01349 729 *
4d11a402 730 * In all cases, the separate pquotino must remain 0 because it
b63da6c8 731 * is beyond the "end" of the valid non-pquotino superblock.
ff55068c 732 */
4d11a402 733 if (from->sb_qflags & XFS_GQUOTA_ACCT)
ff55068c 734 to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
4d11a402 735 else if (from->sb_qflags & XFS_PQUOTA_ACCT)
ff55068c 736 to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
03e01349
DC
737 else {
738 /*
739 * We can't rely on just the fields being logged to tell us
740 * that it is safe to write NULLFSINO - we should only do that
741 * if quotas are not actually enabled. Hence only write
742 * NULLFSINO if both in-core quota inodes are NULL.
743 */
744 if (from->sb_gquotino == NULLFSINO &&
745 from->sb_pquotino == NULLFSINO)
746 to->sb_gquotino = cpu_to_be64(NULLFSINO);
747 }
ff55068c 748
4d11a402 749 to->sb_pquotino = 0;
ff55068c
DC
750}
751
ff55068c
DC
752void
753xfs_sb_to_disk(
4d11a402
DC
754 struct xfs_dsb *to,
755 struct xfs_sb *from)
ff55068c 756{
4d11a402
DC
757 xfs_sb_quota_to_disk(to, from);
758
759 to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
760 to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
761 to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
762 to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
763 to->sb_rextents = cpu_to_be64(from->sb_rextents);
764 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
765 to->sb_logstart = cpu_to_be64(from->sb_logstart);
766 to->sb_rootino = cpu_to_be64(from->sb_rootino);
767 to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
768 to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
769 to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
770 to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
771 to->sb_agcount = cpu_to_be32(from->sb_agcount);
772 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
773 to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
774 to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
775 to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
776 to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
777 to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
778 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
779 to->sb_blocklog = from->sb_blocklog;
780 to->sb_sectlog = from->sb_sectlog;
781 to->sb_inodelog = from->sb_inodelog;
782 to->sb_inopblog = from->sb_inopblog;
783 to->sb_agblklog = from->sb_agblklog;
784 to->sb_rextslog = from->sb_rextslog;
785 to->sb_inprogress = from->sb_inprogress;
786 to->sb_imax_pct = from->sb_imax_pct;
787 to->sb_icount = cpu_to_be64(from->sb_icount);
788 to->sb_ifree = cpu_to_be64(from->sb_ifree);
789 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
790 to->sb_frextents = cpu_to_be64(from->sb_frextents);
ff55068c 791
4d11a402
DC
792 to->sb_flags = from->sb_flags;
793 to->sb_shared_vn = from->sb_shared_vn;
794 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
795 to->sb_unit = cpu_to_be32(from->sb_unit);
796 to->sb_width = cpu_to_be32(from->sb_width);
797 to->sb_dirblklog = from->sb_dirblklog;
798 to->sb_logsectlog = from->sb_logsectlog;
799 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
800 to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
074e427b
DC
801
802 /*
803 * We need to ensure that bad_features2 always matches features2.
804 * Hence we enforce that here rather than having to remember to do it
805 * everywhere else that updates features2.
806 */
807 from->sb_bad_features2 = from->sb_features2;
4d11a402
DC
808 to->sb_features2 = cpu_to_be32(from->sb_features2);
809 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
810
d6837c1a
DC
811 if (!xfs_sb_is_v5(from))
812 return;
813
814 to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
815 to->sb_features_ro_compat =
816 cpu_to_be32(from->sb_features_ro_compat);
817 to->sb_features_incompat =
818 cpu_to_be32(from->sb_features_incompat);
819 to->sb_features_log_incompat =
820 cpu_to_be32(from->sb_features_log_incompat);
821 to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
822 to->sb_lsn = cpu_to_be64(from->sb_lsn);
823 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
824 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
ff55068c
DC
825}
826
ff55068c
DC
827/*
828 * If the superblock has the CRC feature bit set or the CRC field is non-null,
829 * check that the CRC is valid. We check the CRC field is non-null because a
830 * single bit error could clear the feature bit and unused parts of the
831 * superblock are supposed to be zero. Hence a non-null crc field indicates that
832 * we've potentially lost a feature bit and we should check it anyway.
10e6e65d
ES
833 *
834 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
835 * last field in V4 secondary superblocks. So for secondary superblocks,
836 * we are more forgiving, and ignore CRC failures if the primary doesn't
837 * indicate that the fs version is V5.
ff55068c
DC
838 */
839static void
840xfs_sb_read_verify(
eca383fc 841 struct xfs_buf *bp)
ff55068c 842{
eca383fc 843 struct xfs_sb sb;
dbd329f1 844 struct xfs_mount *mp = bp->b_mount;
3e6e8afd 845 struct xfs_dsb *dsb = bp->b_addr;
eca383fc 846 int error;
ff55068c
DC
847
848 /*
849 * open code the version check to avoid needing to convert the entire
850 * superblock from disk order just to check the version number
851 */
852 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
853 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
854 XFS_SB_VERSION_5) ||
855 dsb->sb_crc != 0)) {
856
51582170 857 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
10e6e65d 858 /* Only fail bad secondaries on a known V5 filesystem */
9343ee76 859 if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
fe08cc50 860 xfs_has_crc(mp)) {
2451337d 861 error = -EFSBADCRC;
10e6e65d
ES
862 goto out_error;
863 }
ff55068c
DC
864 }
865 }
eca383fc
DW
866
867 /*
868 * Check all the superblock fields. Don't byteswap the xquota flags
869 * because _verify_common checks the on-disk values.
870 */
3e6e8afd 871 __xfs_sb_from_disk(&sb, dsb, false);
eca383fc
DW
872 error = xfs_validate_sb_common(mp, bp, &sb);
873 if (error)
874 goto out_error;
875 error = xfs_validate_sb_read(mp, &sb);
ff55068c
DC
876
877out_error:
31ca03c9 878 if (error == -EFSCORRUPTED || error == -EFSBADCRC)
bc1a09b8 879 xfs_verifier_error(bp, error, __this_address);
31ca03c9 880 else if (error)
ff55068c 881 xfs_buf_ioerror(bp, error);
ff55068c
DC
882}
883
884/*
885 * We may be probed for a filesystem match, so we may not want to emit
886 * messages when the superblock buffer is not actually an XFS superblock.
2533787a 887 * If we find an XFS superblock, then run a normal, noisy mount because we are
ff55068c
DC
888 * really going to mount it and want to know about errors.
889 */
890static void
891xfs_sb_quiet_read_verify(
892 struct xfs_buf *bp)
893{
3e6e8afd 894 struct xfs_dsb *dsb = bp->b_addr;
ff55068c 895
ff55068c
DC
896 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
897 /* XFS filesystem, verify noisily! */
898 xfs_sb_read_verify(bp);
899 return;
900 }
901 /* quietly fail */
2451337d 902 xfs_buf_ioerror(bp, -EWRONGFS);
ff55068c
DC
903}
904
905static void
906xfs_sb_write_verify(
907 struct xfs_buf *bp)
908{
eca383fc 909 struct xfs_sb sb;
dbd329f1 910 struct xfs_mount *mp = bp->b_mount;
fb1755a6 911 struct xfs_buf_log_item *bip = bp->b_log_item;
3e6e8afd 912 struct xfs_dsb *dsb = bp->b_addr;
ff55068c
DC
913 int error;
914
eca383fc
DW
915 /*
916 * Check all the superblock fields. Don't byteswap the xquota flags
917 * because _verify_common checks the on-disk values.
918 */
3e6e8afd 919 __xfs_sb_from_disk(&sb, dsb, false);
eca383fc
DW
920 error = xfs_validate_sb_common(mp, bp, &sb);
921 if (error)
922 goto out_error;
1f31c98d 923 error = xfs_validate_sb_write(mp, bp, &sb);
eca383fc
DW
924 if (error)
925 goto out_error;
ff55068c 926
d6837c1a 927 if (!xfs_sb_is_v5(&sb))
ff55068c
DC
928 return;
929
930 if (bip)
3e6e8afd 931 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
ff55068c 932
f1dbcd7e 933 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
eca383fc
DW
934 return;
935
936out_error:
937 xfs_verifier_error(bp, error, __this_address);
ff55068c
DC
938}
939
940const struct xfs_buf_ops xfs_sb_buf_ops = {
233135b7 941 .name = "xfs_sb",
39708c20 942 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
ff55068c
DC
943 .verify_read = xfs_sb_read_verify,
944 .verify_write = xfs_sb_write_verify,
945};
946
947const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
233135b7 948 .name = "xfs_sb_quiet",
39708c20 949 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
ff55068c
DC
950 .verify_read = xfs_sb_quiet_read_verify,
951 .verify_write = xfs_sb_write_verify,
952};
953
954/*
955 * xfs_mount_common
956 *
957 * Mount initialization code establishing various mount
958 * fields from the superblock associated with the given
494dba7b
DW
959 * mount structure.
960 *
961 * Inode geometry are calculated in xfs_ialloc_setup_geometry.
ff55068c
DC
962 */
963void
964xfs_sb_mount_common(
ef325959
DW
965 struct xfs_mount *mp,
966 struct xfs_sb *sbp)
ff55068c 967{
20a5eab4
DC
968 mp->m_agfrotor = 0;
969 atomic_set(&mp->m_agirotor, 0);
ff55068c
DC
970 mp->m_maxagi = mp->m_sb.sb_agcount;
971 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
972 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
973 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
974 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
ff55068c
DC
975 mp->m_blockmask = sbp->sb_blocksize - 1;
976 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
977 mp->m_blockwmask = mp->m_blockwsize - 1;
978
979 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
980 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
981 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
982 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
983
ff55068c
DC
984 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
985 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
986 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
987 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
988
a1f69417
ES
989 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
990 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
035e00ac
DW
991 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
992 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
993
a1f69417
ES
994 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
995 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
1946b91c
DW
996 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
997 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
998
ff55068c 999 mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
52548852
DW
1000 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
1001 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
ff55068c
DC
1002}
1003
ff55068c 1004/*
61e63ecb
DC
1005 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
1006 * into the superblock buffer to be logged. It does not provide the higher
1007 * level of locking that is needed to protect the in-core superblock from
1008 * concurrent access.
ff55068c
DC
1009 */
1010void
61e63ecb 1011xfs_log_sb(
4d11a402 1012 struct xfs_trans *tp)
ff55068c 1013{
4d11a402 1014 struct xfs_mount *mp = tp->t_mountp;
cead0b10 1015 struct xfs_buf *bp = xfs_trans_getsb(tp);
ff55068c 1016
6543990a
DC
1017 /*
1018 * Lazy sb counters don't update the in-core superblock so do that now.
1019 * If this is at unmount, the counters will be exactly correct, but at
1020 * any other time they will only be ballpark correct because of
1021 * reservations that have been taken out percpu counters. If we have an
1022 * unclean shutdown, this will be corrected by log recovery rebuilding
1023 * the counters from the AGF block counts.
2229276c
DW
1024 *
1025 * Do not update sb_frextents here because it is not part of the lazy
1026 * sb counters, despite having a percpu counter. It is always kept
1027 * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas()
1028 * and hence we don't need have to update it here.
6543990a 1029 */
ebd9027d 1030 if (xfs_has_lazysbcount(mp)) {
6543990a 1031 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
59f6ab40
LL
1032 mp->m_sb.sb_ifree = min_t(uint64_t,
1033 percpu_counter_sum(&mp->m_ifree),
1034 mp->m_sb.sb_icount);
6543990a
DC
1035 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
1036 }
501ab323 1037
3e6e8afd 1038 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
ff55068c 1039 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
6f4ff81a 1040 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
ff55068c 1041}
61e63ecb
DC
1042
1043/*
1044 * xfs_sync_sb
1045 *
1046 * Sync the superblock to disk.
1047 *
1048 * Note that the caller is responsible for checking the frozen state of the
1049 * filesystem. This procedure uses the non-blocking transaction allocator and
1050 * thus will allow modifications to a frozen fs. This is required because this
1051 * code can be called during the process of freezing where use of the high-level
1052 * allocator would deadlock.
1053 */
1054int
1055xfs_sync_sb(
1056 struct xfs_mount *mp,
1057 bool wait)
1058{
1059 struct xfs_trans *tp;
1060 int error;
1061
253f4911
CH
1062 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
1063 XFS_TRANS_NO_WRITECOUNT, &tp);
1064 if (error)
61e63ecb 1065 return error;
61e63ecb
DC
1066
1067 xfs_log_sb(tp);
1068 if (wait)
1069 xfs_trans_set_sync(tp);
70393313 1070 return xfs_trans_commit(tp);
61e63ecb 1071}
c368ebcd 1072
b16817b6
DC
1073/*
1074 * Update all the secondary superblocks to match the new state of the primary.
1075 * Because we are completely overwriting all the existing fields in the
1076 * secondary superblock buffers, there is no need to read them in from disk.
1077 * Just get a new buffer, stamp it and write it.
1078 *
1079 * The sb buffers need to be cached here so that we serialise against other
1080 * operations that access the secondary superblocks, but we don't want to keep
1081 * them in memory once it is written so we mark it as a one-shot buffer.
1082 */
1083int
1084xfs_update_secondary_sbs(
1085 struct xfs_mount *mp)
1086{
7f8d3b3c
DC
1087 struct xfs_perag *pag;
1088 xfs_agnumber_t agno = 1;
b16817b6
DC
1089 int saved_error = 0;
1090 int error = 0;
1091 LIST_HEAD (buffer_list);
1092
1093 /* update secondary superblocks. */
7f8d3b3c 1094 for_each_perag_from(mp, agno, pag) {
b16817b6
DC
1095 struct xfs_buf *bp;
1096
841263e9 1097 error = xfs_buf_get(mp->m_ddev_targp,
7f8d3b3c 1098 XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
841263e9 1099 XFS_FSS_TO_BB(mp, 1), &bp);
b16817b6
DC
1100 /*
1101 * If we get an error reading or writing alternate superblocks,
1102 * continue. xfs_repair chooses the "best" superblock based
1103 * on most matches; if we break early, we'll leave more
1104 * superblocks un-updated than updated, and xfs_repair may
1105 * pick them over the properly-updated primary.
1106 */
841263e9 1107 if (error) {
b16817b6
DC
1108 xfs_warn(mp,
1109 "error allocating secondary superblock for ag %d",
7f8d3b3c 1110 pag->pag_agno);
b16817b6 1111 if (!saved_error)
841263e9 1112 saved_error = error;
b16817b6
DC
1113 continue;
1114 }
1115
1116 bp->b_ops = &xfs_sb_buf_ops;
1117 xfs_buf_oneshot(bp);
1118 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
3e6e8afd 1119 xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
b16817b6
DC
1120 xfs_buf_delwri_queue(bp, &buffer_list);
1121 xfs_buf_relse(bp);
1122
1123 /* don't hold too many buffers at once */
1124 if (agno % 16)
1125 continue;
1126
1127 error = xfs_buf_delwri_submit(&buffer_list);
1128 if (error) {
1129 xfs_warn(mp,
1130 "write error %d updating a secondary superblock near ag %d",
7f8d3b3c 1131 error, pag->pag_agno);
b16817b6
DC
1132 if (!saved_error)
1133 saved_error = error;
1134 continue;
1135 }
1136 }
1137 error = xfs_buf_delwri_submit(&buffer_list);
1138 if (error) {
1139 xfs_warn(mp,
1140 "write error %d updating a secondary superblock near ag %d",
1141 error, agno);
1142 }
1143
1144 return saved_error ? saved_error : error;
1145}
1146
f7664b31
ES
1147/*
1148 * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1149 * also writes the superblock buffer to disk sector 0 immediately.
1150 */
1151int
1152xfs_sync_sb_buf(
1153 struct xfs_mount *mp)
1154{
1155 struct xfs_trans *tp;
89c2e711 1156 struct xfs_buf *bp;
f7664b31
ES
1157 int error;
1158
1159 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1160 if (error)
1161 return error;
1162
cead0b10 1163 bp = xfs_trans_getsb(tp);
f7664b31 1164 xfs_log_sb(tp);
89c2e711 1165 xfs_trans_bhold(tp, bp);
f7664b31
ES
1166 xfs_trans_set_sync(tp);
1167 error = xfs_trans_commit(tp);
1168 if (error)
1169 goto out;
1170 /*
1171 * write out the sb buffer to get the changes to disk
1172 */
89c2e711 1173 error = xfs_bwrite(bp);
f7664b31 1174out:
89c2e711 1175 xfs_buf_relse(bp);
f7664b31
ES
1176 return error;
1177}
1178
91083269 1179void
c368ebcd 1180xfs_fs_geometry(
03288b19 1181 struct xfs_mount *mp,
ac503a4c
DW
1182 struct xfs_fsop_geom *geo,
1183 int struct_version)
c368ebcd 1184{
03288b19
DC
1185 struct xfs_sb *sbp = &mp->m_sb;
1186
ac503a4c
DW
1187 memset(geo, 0, sizeof(struct xfs_fsop_geom));
1188
1189 geo->blocksize = sbp->sb_blocksize;
1190 geo->rtextsize = sbp->sb_rextsize;
1191 geo->agblocks = sbp->sb_agblocks;
1192 geo->agcount = sbp->sb_agcount;
1193 geo->logblocks = sbp->sb_logblocks;
1194 geo->sectsize = sbp->sb_sectsize;
1195 geo->inodesize = sbp->sb_inodesize;
1196 geo->imaxpct = sbp->sb_imax_pct;
1197 geo->datablocks = sbp->sb_dblocks;
1198 geo->rtblocks = sbp->sb_rblocks;
1199 geo->rtextents = sbp->sb_rextents;
1200 geo->logstart = sbp->sb_logstart;
1201 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1202 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1203
1204 if (struct_version < 2)
91083269 1205 return;
ac503a4c
DW
1206
1207 geo->sunit = sbp->sb_unit;
1208 geo->swidth = sbp->sb_width;
1209
1210 if (struct_version < 3)
91083269 1211 return;
ac503a4c
DW
1212
1213 geo->version = XFS_FSOP_GEOM_VERSION;
1214 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
daa79bae
CH
1215 XFS_FSOP_GEOM_FLAGS_DIRV2 |
1216 XFS_FSOP_GEOM_FLAGS_EXTFLG;
03288b19 1217 if (xfs_has_attr(mp))
ac503a4c 1218 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
03288b19 1219 if (xfs_has_quota(mp))
ac503a4c 1220 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
03288b19 1221 if (xfs_has_align(mp))
ac503a4c 1222 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
03288b19 1223 if (xfs_has_dalign(mp))
ac503a4c 1224 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
03288b19 1225 if (xfs_has_asciici(mp))
ac503a4c 1226 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
03288b19 1227 if (xfs_has_lazysbcount(mp))
ac503a4c 1228 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
03288b19 1229 if (xfs_has_attr2(mp))
ac503a4c 1230 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
03288b19 1231 if (xfs_has_projid32(mp))
ac503a4c 1232 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
03288b19 1233 if (xfs_has_crc(mp))
ac503a4c 1234 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
03288b19 1235 if (xfs_has_ftype(mp))
ac503a4c 1236 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
03288b19 1237 if (xfs_has_finobt(mp))
ac503a4c 1238 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
03288b19 1239 if (xfs_has_sparseinodes(mp))
ac503a4c 1240 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
03288b19 1241 if (xfs_has_rmapbt(mp))
ac503a4c 1242 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
03288b19 1243 if (xfs_has_reflink(mp))
ac503a4c 1244 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
03288b19 1245 if (xfs_has_bigtime(mp))
f93e5436 1246 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
03288b19 1247 if (xfs_has_inobtcounts(mp))
bc41fa53 1248 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
03288b19
DC
1249 if (xfs_has_sector(mp)) {
1250 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
ac503a4c 1251 geo->logsectsize = sbp->sb_logsectsize;
03288b19 1252 } else {
ac503a4c 1253 geo->logsectsize = BBSIZE;
03288b19 1254 }
7c05aa9d
CB
1255 if (xfs_has_large_extent_counts(mp))
1256 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
ac503a4c
DW
1257 geo->rtsectsize = sbp->sb_blocksize;
1258 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1259
a8789a5a 1260 if (struct_version < 4)
91083269 1261 return;
ac503a4c 1262
03288b19 1263 if (xfs_has_logv2(mp))
ac503a4c
DW
1264 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1265
1266 geo->logsunit = sbp->sb_logsunit;
c368ebcd 1267
1b6d968d 1268 if (struct_version < 5)
91083269 1269 return;
1b6d968d
DC
1270
1271 geo->version = XFS_FSOP_GEOM_VERSION_V5;
c368ebcd 1272}
689e11c8
DW
1273
1274/* Read a secondary superblock. */
1275int
1276xfs_sb_read_secondary(
1277 struct xfs_mount *mp,
1278 struct xfs_trans *tp,
1279 xfs_agnumber_t agno,
1280 struct xfs_buf **bpp)
1281{
1282 struct xfs_buf *bp;
1283 int error;
1284
1285 ASSERT(agno != 0 && agno != NULLAGNUMBER);
1286 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1287 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1288 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1289 if (error)
1290 return error;
1291 xfs_buf_set_ref(bp, XFS_SSB_REF);
1292 *bpp = bp;
1293 return 0;
1294}
d25522f1
DW
1295
1296/* Get an uninitialised secondary superblock buffer. */
1297int
1298xfs_sb_get_secondary(
1299 struct xfs_mount *mp,
1300 struct xfs_trans *tp,
1301 xfs_agnumber_t agno,
1302 struct xfs_buf **bpp)
1303{
1304 struct xfs_buf *bp;
ce92464c 1305 int error;
d25522f1
DW
1306
1307 ASSERT(agno != 0 && agno != NULLAGNUMBER);
ce92464c 1308 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
d25522f1 1309 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
ce92464c
DW
1310 XFS_FSS_TO_BB(mp, 1), 0, &bp);
1311 if (error)
1312 return error;
d25522f1
DW
1313 bp->b_ops = &xfs_sb_buf_ops;
1314 xfs_buf_oneshot(bp);
1315 *bpp = bp;
1316 return 0;
1317}
7bc1fea9
GX
1318
1319/*
1320 * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
1321 * so users won't be confused by values in error messages.
1322 */
1323bool
1324xfs_validate_stripe_geometry(
1325 struct xfs_mount *mp,
1326 __s64 sunit,
1327 __s64 swidth,
1328 int sectorsize,
1329 bool silent)
1330{
1331 if (swidth > INT_MAX) {
1332 if (!silent)
1333 xfs_notice(mp,
1334"stripe width (%lld) is too large", swidth);
1335 return false;
1336 }
1337
1338 if (sunit > swidth) {
1339 if (!silent)
1340 xfs_notice(mp,
1341"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
1342 return false;
1343 }
1344
1345 if (sectorsize && (int)sunit % sectorsize) {
1346 if (!silent)
1347 xfs_notice(mp,
1348"stripe unit (%lld) must be a multiple of the sector size (%d)",
1349 sunit, sectorsize);
1350 return false;
1351 }
1352
1353 if (sunit && !swidth) {
1354 if (!silent)
1355 xfs_notice(mp,
1356"invalid stripe unit (%lld) and stripe width of 0", sunit);
1357 return false;
1358 }
1359
1360 if (!sunit && swidth) {
1361 if (!silent)
1362 xfs_notice(mp,
1363"invalid stripe width (%lld) and stripe unit of 0", swidth);
1364 return false;
1365 }
1366
1367 if (sunit && (int)swidth % (int)sunit) {
1368 if (!silent)
1369 xfs_notice(mp,
1370"stripe width (%lld) must be a multiple of the stripe unit (%lld)",
1371 swidth, sunit);
1372 return false;
1373 }
1374 return true;
1375}