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