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