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