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