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