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