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