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