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