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