xref: /openbmc/linux/fs/xfs/libxfs/xfs_sb.c (revision dc6a81c3)
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 = XFS_BUF_TO_SBP(bp);
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_[PQ]UOTA_{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 	if (sbp->sb_unit) {
332 		if (!xfs_sb_version_hasdalign(sbp) ||
333 		    sbp->sb_unit > sbp->sb_width ||
334 		    (sbp->sb_width % sbp->sb_unit) != 0) {
335 			xfs_notice(mp, "SB stripe unit sanity check failed");
336 			return -EFSCORRUPTED;
337 		}
338 	} else if (xfs_sb_version_hasdalign(sbp)) {
339 		xfs_notice(mp, "SB stripe alignment sanity check failed");
340 		return -EFSCORRUPTED;
341 	} else if (sbp->sb_width) {
342 		xfs_notice(mp, "SB stripe width sanity check failed");
343 		return -EFSCORRUPTED;
344 	}
345 
346 
347 	if (xfs_sb_version_hascrc(&mp->m_sb) &&
348 	    sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
349 		xfs_notice(mp, "v5 SB sanity check failed");
350 		return -EFSCORRUPTED;
351 	}
352 
353 	/*
354 	 * Until this is fixed only page-sized or smaller data blocks work.
355 	 */
356 	if (unlikely(sbp->sb_blocksize > PAGE_SIZE)) {
357 		xfs_warn(mp,
358 		"File system with blocksize %d bytes. "
359 		"Only pagesize (%ld) or less will currently work.",
360 				sbp->sb_blocksize, PAGE_SIZE);
361 		return -ENOSYS;
362 	}
363 
364 	/*
365 	 * Currently only very few inode sizes are supported.
366 	 */
367 	switch (sbp->sb_inodesize) {
368 	case 256:
369 	case 512:
370 	case 1024:
371 	case 2048:
372 		break;
373 	default:
374 		xfs_warn(mp, "inode size of %d bytes not supported",
375 				sbp->sb_inodesize);
376 		return -ENOSYS;
377 	}
378 
379 	if (xfs_sb_validate_fsb_count(sbp, sbp->sb_dblocks) ||
380 	    xfs_sb_validate_fsb_count(sbp, sbp->sb_rblocks)) {
381 		xfs_warn(mp,
382 		"file system too large to be mounted on this system.");
383 		return -EFBIG;
384 	}
385 
386 	/*
387 	 * Don't touch the filesystem if a user tool thinks it owns the primary
388 	 * superblock.  mkfs doesn't clear the flag from secondary supers, so
389 	 * we don't check them at all.
390 	 */
391 	if (XFS_BUF_ADDR(bp) == XFS_SB_DADDR && sbp->sb_inprogress) {
392 		xfs_warn(mp, "Offline file system operation in progress!");
393 		return -EFSCORRUPTED;
394 	}
395 	return 0;
396 }
397 
398 void
399 xfs_sb_quota_from_disk(struct xfs_sb *sbp)
400 {
401 	/*
402 	 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
403 	 * leads to in-core values having two different values for a quota
404 	 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
405 	 * NULLFSINO.
406 	 *
407 	 * Note that this change affect only the in-core values. These
408 	 * values are not written back to disk unless any quota information
409 	 * is written to the disk. Even in that case, sb_pquotino field is
410 	 * not written to disk unless the superblock supports pquotino.
411 	 */
412 	if (sbp->sb_uquotino == 0)
413 		sbp->sb_uquotino = NULLFSINO;
414 	if (sbp->sb_gquotino == 0)
415 		sbp->sb_gquotino = NULLFSINO;
416 	if (sbp->sb_pquotino == 0)
417 		sbp->sb_pquotino = NULLFSINO;
418 
419 	/*
420 	 * We need to do these manipilations only if we are working
421 	 * with an older version of on-disk superblock.
422 	 */
423 	if (xfs_sb_version_has_pquotino(sbp))
424 		return;
425 
426 	if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
427 		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
428 					XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
429 	if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
430 		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
431 					XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
432 	sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
433 
434 	if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
435 	    sbp->sb_gquotino != NULLFSINO)  {
436 		/*
437 		 * In older version of superblock, on-disk superblock only
438 		 * has sb_gquotino, and in-core superblock has both sb_gquotino
439 		 * and sb_pquotino. But, only one of them is supported at any
440 		 * point of time. So, if PQUOTA is set in disk superblock,
441 		 * copy over sb_gquotino to sb_pquotino.  The NULLFSINO test
442 		 * above is to make sure we don't do this twice and wipe them
443 		 * both out!
444 		 */
445 		sbp->sb_pquotino = sbp->sb_gquotino;
446 		sbp->sb_gquotino = NULLFSINO;
447 	}
448 }
449 
450 static void
451 __xfs_sb_from_disk(
452 	struct xfs_sb	*to,
453 	xfs_dsb_t	*from,
454 	bool		convert_xquota)
455 {
456 	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
457 	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
458 	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
459 	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
460 	to->sb_rextents = be64_to_cpu(from->sb_rextents);
461 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
462 	to->sb_logstart = be64_to_cpu(from->sb_logstart);
463 	to->sb_rootino = be64_to_cpu(from->sb_rootino);
464 	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
465 	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
466 	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
467 	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
468 	to->sb_agcount = be32_to_cpu(from->sb_agcount);
469 	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
470 	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
471 	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
472 	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
473 	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
474 	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
475 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
476 	to->sb_blocklog = from->sb_blocklog;
477 	to->sb_sectlog = from->sb_sectlog;
478 	to->sb_inodelog = from->sb_inodelog;
479 	to->sb_inopblog = from->sb_inopblog;
480 	to->sb_agblklog = from->sb_agblklog;
481 	to->sb_rextslog = from->sb_rextslog;
482 	to->sb_inprogress = from->sb_inprogress;
483 	to->sb_imax_pct = from->sb_imax_pct;
484 	to->sb_icount = be64_to_cpu(from->sb_icount);
485 	to->sb_ifree = be64_to_cpu(from->sb_ifree);
486 	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
487 	to->sb_frextents = be64_to_cpu(from->sb_frextents);
488 	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
489 	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
490 	to->sb_qflags = be16_to_cpu(from->sb_qflags);
491 	to->sb_flags = from->sb_flags;
492 	to->sb_shared_vn = from->sb_shared_vn;
493 	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
494 	to->sb_unit = be32_to_cpu(from->sb_unit);
495 	to->sb_width = be32_to_cpu(from->sb_width);
496 	to->sb_dirblklog = from->sb_dirblklog;
497 	to->sb_logsectlog = from->sb_logsectlog;
498 	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
499 	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
500 	to->sb_features2 = be32_to_cpu(from->sb_features2);
501 	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
502 	to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
503 	to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
504 	to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
505 	to->sb_features_log_incompat =
506 				be32_to_cpu(from->sb_features_log_incompat);
507 	/* crc is only used on disk, not in memory; just init to 0 here. */
508 	to->sb_crc = 0;
509 	to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
510 	to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
511 	to->sb_lsn = be64_to_cpu(from->sb_lsn);
512 	/*
513 	 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
514 	 * feature flag is set; if not set we keep it only in memory.
515 	 */
516 	if (xfs_sb_version_hasmetauuid(to))
517 		uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
518 	else
519 		uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
520 	/* Convert on-disk flags to in-memory flags? */
521 	if (convert_xquota)
522 		xfs_sb_quota_from_disk(to);
523 }
524 
525 void
526 xfs_sb_from_disk(
527 	struct xfs_sb	*to,
528 	xfs_dsb_t	*from)
529 {
530 	__xfs_sb_from_disk(to, from, true);
531 }
532 
533 static void
534 xfs_sb_quota_to_disk(
535 	struct xfs_dsb	*to,
536 	struct xfs_sb	*from)
537 {
538 	uint16_t	qflags = from->sb_qflags;
539 
540 	to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
541 	if (xfs_sb_version_has_pquotino(from)) {
542 		to->sb_qflags = cpu_to_be16(from->sb_qflags);
543 		to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
544 		to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
545 		return;
546 	}
547 
548 	/*
549 	 * The in-core version of sb_qflags do not have XFS_OQUOTA_*
550 	 * flags, whereas the on-disk version does.  So, convert incore
551 	 * XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
552 	 */
553 	qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
554 			XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
555 
556 	if (from->sb_qflags &
557 			(XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
558 		qflags |= XFS_OQUOTA_ENFD;
559 	if (from->sb_qflags &
560 			(XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
561 		qflags |= XFS_OQUOTA_CHKD;
562 	to->sb_qflags = cpu_to_be16(qflags);
563 
564 	/*
565 	 * GQUOTINO and PQUOTINO cannot be used together in versions
566 	 * of superblock that do not have pquotino. from->sb_flags
567 	 * tells us which quota is active and should be copied to
568 	 * disk. If neither are active, we should NULL the inode.
569 	 *
570 	 * In all cases, the separate pquotino must remain 0 because it
571 	 * it beyond the "end" of the valid non-pquotino superblock.
572 	 */
573 	if (from->sb_qflags & XFS_GQUOTA_ACCT)
574 		to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
575 	else if (from->sb_qflags & XFS_PQUOTA_ACCT)
576 		to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
577 	else {
578 		/*
579 		 * We can't rely on just the fields being logged to tell us
580 		 * that it is safe to write NULLFSINO - we should only do that
581 		 * if quotas are not actually enabled. Hence only write
582 		 * NULLFSINO if both in-core quota inodes are NULL.
583 		 */
584 		if (from->sb_gquotino == NULLFSINO &&
585 		    from->sb_pquotino == NULLFSINO)
586 			to->sb_gquotino = cpu_to_be64(NULLFSINO);
587 	}
588 
589 	to->sb_pquotino = 0;
590 }
591 
592 void
593 xfs_sb_to_disk(
594 	struct xfs_dsb	*to,
595 	struct xfs_sb	*from)
596 {
597 	xfs_sb_quota_to_disk(to, from);
598 
599 	to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
600 	to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
601 	to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
602 	to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
603 	to->sb_rextents = cpu_to_be64(from->sb_rextents);
604 	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
605 	to->sb_logstart = cpu_to_be64(from->sb_logstart);
606 	to->sb_rootino = cpu_to_be64(from->sb_rootino);
607 	to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
608 	to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
609 	to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
610 	to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
611 	to->sb_agcount = cpu_to_be32(from->sb_agcount);
612 	to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
613 	to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
614 	to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
615 	to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
616 	to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
617 	to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
618 	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
619 	to->sb_blocklog = from->sb_blocklog;
620 	to->sb_sectlog = from->sb_sectlog;
621 	to->sb_inodelog = from->sb_inodelog;
622 	to->sb_inopblog = from->sb_inopblog;
623 	to->sb_agblklog = from->sb_agblklog;
624 	to->sb_rextslog = from->sb_rextslog;
625 	to->sb_inprogress = from->sb_inprogress;
626 	to->sb_imax_pct = from->sb_imax_pct;
627 	to->sb_icount = cpu_to_be64(from->sb_icount);
628 	to->sb_ifree = cpu_to_be64(from->sb_ifree);
629 	to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
630 	to->sb_frextents = cpu_to_be64(from->sb_frextents);
631 
632 	to->sb_flags = from->sb_flags;
633 	to->sb_shared_vn = from->sb_shared_vn;
634 	to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
635 	to->sb_unit = cpu_to_be32(from->sb_unit);
636 	to->sb_width = cpu_to_be32(from->sb_width);
637 	to->sb_dirblklog = from->sb_dirblklog;
638 	to->sb_logsectlog = from->sb_logsectlog;
639 	to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
640 	to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
641 
642 	/*
643 	 * We need to ensure that bad_features2 always matches features2.
644 	 * Hence we enforce that here rather than having to remember to do it
645 	 * everywhere else that updates features2.
646 	 */
647 	from->sb_bad_features2 = from->sb_features2;
648 	to->sb_features2 = cpu_to_be32(from->sb_features2);
649 	to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
650 
651 	if (xfs_sb_version_hascrc(from)) {
652 		to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
653 		to->sb_features_ro_compat =
654 				cpu_to_be32(from->sb_features_ro_compat);
655 		to->sb_features_incompat =
656 				cpu_to_be32(from->sb_features_incompat);
657 		to->sb_features_log_incompat =
658 				cpu_to_be32(from->sb_features_log_incompat);
659 		to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
660 		to->sb_lsn = cpu_to_be64(from->sb_lsn);
661 		if (xfs_sb_version_hasmetauuid(from))
662 			uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
663 	}
664 }
665 
666 /*
667  * If the superblock has the CRC feature bit set or the CRC field is non-null,
668  * check that the CRC is valid.  We check the CRC field is non-null because a
669  * single bit error could clear the feature bit and unused parts of the
670  * superblock are supposed to be zero. Hence a non-null crc field indicates that
671  * we've potentially lost a feature bit and we should check it anyway.
672  *
673  * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
674  * last field in V4 secondary superblocks.  So for secondary superblocks,
675  * we are more forgiving, and ignore CRC failures if the primary doesn't
676  * indicate that the fs version is V5.
677  */
678 static void
679 xfs_sb_read_verify(
680 	struct xfs_buf		*bp)
681 {
682 	struct xfs_sb		sb;
683 	struct xfs_mount	*mp = bp->b_mount;
684 	struct xfs_dsb		*dsb = XFS_BUF_TO_SBP(bp);
685 	int			error;
686 
687 	/*
688 	 * open code the version check to avoid needing to convert the entire
689 	 * superblock from disk order just to check the version number
690 	 */
691 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
692 	    (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
693 						XFS_SB_VERSION_5) ||
694 	     dsb->sb_crc != 0)) {
695 
696 		if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
697 			/* Only fail bad secondaries on a known V5 filesystem */
698 			if (bp->b_bn == XFS_SB_DADDR ||
699 			    xfs_sb_version_hascrc(&mp->m_sb)) {
700 				error = -EFSBADCRC;
701 				goto out_error;
702 			}
703 		}
704 	}
705 
706 	/*
707 	 * Check all the superblock fields.  Don't byteswap the xquota flags
708 	 * because _verify_common checks the on-disk values.
709 	 */
710 	__xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
711 	error = xfs_validate_sb_common(mp, bp, &sb);
712 	if (error)
713 		goto out_error;
714 	error = xfs_validate_sb_read(mp, &sb);
715 
716 out_error:
717 	if (error == -EFSCORRUPTED || error == -EFSBADCRC)
718 		xfs_verifier_error(bp, error, __this_address);
719 	else if (error)
720 		xfs_buf_ioerror(bp, error);
721 }
722 
723 /*
724  * We may be probed for a filesystem match, so we may not want to emit
725  * messages when the superblock buffer is not actually an XFS superblock.
726  * If we find an XFS superblock, then run a normal, noisy mount because we are
727  * really going to mount it and want to know about errors.
728  */
729 static void
730 xfs_sb_quiet_read_verify(
731 	struct xfs_buf	*bp)
732 {
733 	struct xfs_dsb	*dsb = XFS_BUF_TO_SBP(bp);
734 
735 	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
736 		/* XFS filesystem, verify noisily! */
737 		xfs_sb_read_verify(bp);
738 		return;
739 	}
740 	/* quietly fail */
741 	xfs_buf_ioerror(bp, -EWRONGFS);
742 }
743 
744 static void
745 xfs_sb_write_verify(
746 	struct xfs_buf		*bp)
747 {
748 	struct xfs_sb		sb;
749 	struct xfs_mount	*mp = bp->b_mount;
750 	struct xfs_buf_log_item	*bip = bp->b_log_item;
751 	int			error;
752 
753 	/*
754 	 * Check all the superblock fields.  Don't byteswap the xquota flags
755 	 * because _verify_common checks the on-disk values.
756 	 */
757 	__xfs_sb_from_disk(&sb, XFS_BUF_TO_SBP(bp), false);
758 	error = xfs_validate_sb_common(mp, bp, &sb);
759 	if (error)
760 		goto out_error;
761 	error = xfs_validate_sb_write(mp, bp, &sb);
762 	if (error)
763 		goto out_error;
764 
765 	if (!xfs_sb_version_hascrc(&mp->m_sb))
766 		return;
767 
768 	if (bip)
769 		XFS_BUF_TO_SBP(bp)->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
770 
771 	xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
772 	return;
773 
774 out_error:
775 	xfs_verifier_error(bp, error, __this_address);
776 }
777 
778 const struct xfs_buf_ops xfs_sb_buf_ops = {
779 	.name = "xfs_sb",
780 	.magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
781 	.verify_read = xfs_sb_read_verify,
782 	.verify_write = xfs_sb_write_verify,
783 };
784 
785 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
786 	.name = "xfs_sb_quiet",
787 	.magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
788 	.verify_read = xfs_sb_quiet_read_verify,
789 	.verify_write = xfs_sb_write_verify,
790 };
791 
792 /*
793  * xfs_mount_common
794  *
795  * Mount initialization code establishing various mount
796  * fields from the superblock associated with the given
797  * mount structure.
798  *
799  * Inode geometry are calculated in xfs_ialloc_setup_geometry.
800  */
801 void
802 xfs_sb_mount_common(
803 	struct xfs_mount	*mp,
804 	struct xfs_sb		*sbp)
805 {
806 	mp->m_agfrotor = mp->m_agirotor = 0;
807 	mp->m_maxagi = mp->m_sb.sb_agcount;
808 	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
809 	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
810 	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
811 	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
812 	mp->m_blockmask = sbp->sb_blocksize - 1;
813 	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
814 	mp->m_blockwmask = mp->m_blockwsize - 1;
815 
816 	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
817 	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
818 	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
819 	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
820 
821 	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
822 	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
823 	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
824 	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
825 
826 	mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
827 	mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
828 	mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
829 	mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
830 
831 	mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
832 	mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
833 	mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
834 	mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
835 
836 	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
837 	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
838 	mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
839 }
840 
841 /*
842  * xfs_initialize_perag_data
843  *
844  * Read in each per-ag structure so we can count up the number of
845  * allocated inodes, free inodes and used filesystem blocks as this
846  * information is no longer persistent in the superblock. Once we have
847  * this information, write it into the in-core superblock structure.
848  */
849 int
850 xfs_initialize_perag_data(
851 	struct xfs_mount *mp,
852 	xfs_agnumber_t	agcount)
853 {
854 	xfs_agnumber_t	index;
855 	xfs_perag_t	*pag;
856 	xfs_sb_t	*sbp = &mp->m_sb;
857 	uint64_t	ifree = 0;
858 	uint64_t	ialloc = 0;
859 	uint64_t	bfree = 0;
860 	uint64_t	bfreelst = 0;
861 	uint64_t	btree = 0;
862 	uint64_t	fdblocks;
863 	int		error = 0;
864 
865 	for (index = 0; index < agcount; index++) {
866 		/*
867 		 * read the agf, then the agi. This gets us
868 		 * all the information we need and populates the
869 		 * per-ag structures for us.
870 		 */
871 		error = xfs_alloc_pagf_init(mp, NULL, index, 0);
872 		if (error)
873 			return error;
874 
875 		error = xfs_ialloc_pagi_init(mp, NULL, index);
876 		if (error)
877 			return error;
878 		pag = xfs_perag_get(mp, index);
879 		ifree += pag->pagi_freecount;
880 		ialloc += pag->pagi_count;
881 		bfree += pag->pagf_freeblks;
882 		bfreelst += pag->pagf_flcount;
883 		btree += pag->pagf_btreeblks;
884 		xfs_perag_put(pag);
885 	}
886 	fdblocks = bfree + bfreelst + btree;
887 
888 	/*
889 	 * If the new summary counts are obviously incorrect, fail the
890 	 * mount operation because that implies the AGFs are also corrupt.
891 	 * Clear FS_COUNTERS so that we don't unmount with a dirty log, which
892 	 * will prevent xfs_repair from fixing anything.
893 	 */
894 	if (fdblocks > sbp->sb_dblocks || ifree > ialloc) {
895 		xfs_alert(mp, "AGF corruption. Please run xfs_repair.");
896 		error = -EFSCORRUPTED;
897 		goto out;
898 	}
899 
900 	/* Overwrite incore superblock counters with just-read data */
901 	spin_lock(&mp->m_sb_lock);
902 	sbp->sb_ifree = ifree;
903 	sbp->sb_icount = ialloc;
904 	sbp->sb_fdblocks = fdblocks;
905 	spin_unlock(&mp->m_sb_lock);
906 
907 	xfs_reinit_percpu_counters(mp);
908 out:
909 	xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
910 	return error;
911 }
912 
913 /*
914  * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
915  * into the superblock buffer to be logged.  It does not provide the higher
916  * level of locking that is needed to protect the in-core superblock from
917  * concurrent access.
918  */
919 void
920 xfs_log_sb(
921 	struct xfs_trans	*tp)
922 {
923 	struct xfs_mount	*mp = tp->t_mountp;
924 	struct xfs_buf		*bp = xfs_trans_getsb(tp, mp);
925 
926 	mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
927 	mp->m_sb.sb_ifree = percpu_counter_sum(&mp->m_ifree);
928 	mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
929 
930 	xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
931 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
932 	xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
933 }
934 
935 /*
936  * xfs_sync_sb
937  *
938  * Sync the superblock to disk.
939  *
940  * Note that the caller is responsible for checking the frozen state of the
941  * filesystem. This procedure uses the non-blocking transaction allocator and
942  * thus will allow modifications to a frozen fs. This is required because this
943  * code can be called during the process of freezing where use of the high-level
944  * allocator would deadlock.
945  */
946 int
947 xfs_sync_sb(
948 	struct xfs_mount	*mp,
949 	bool			wait)
950 {
951 	struct xfs_trans	*tp;
952 	int			error;
953 
954 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
955 			XFS_TRANS_NO_WRITECOUNT, &tp);
956 	if (error)
957 		return error;
958 
959 	xfs_log_sb(tp);
960 	if (wait)
961 		xfs_trans_set_sync(tp);
962 	return xfs_trans_commit(tp);
963 }
964 
965 /*
966  * Update all the secondary superblocks to match the new state of the primary.
967  * Because we are completely overwriting all the existing fields in the
968  * secondary superblock buffers, there is no need to read them in from disk.
969  * Just get a new buffer, stamp it and write it.
970  *
971  * The sb buffers need to be cached here so that we serialise against other
972  * operations that access the secondary superblocks, but we don't want to keep
973  * them in memory once it is written so we mark it as a one-shot buffer.
974  */
975 int
976 xfs_update_secondary_sbs(
977 	struct xfs_mount	*mp)
978 {
979 	xfs_agnumber_t		agno;
980 	int			saved_error = 0;
981 	int			error = 0;
982 	LIST_HEAD		(buffer_list);
983 
984 	/* update secondary superblocks. */
985 	for (agno = 1; agno < mp->m_sb.sb_agcount; agno++) {
986 		struct xfs_buf		*bp;
987 
988 		error = xfs_buf_get(mp->m_ddev_targp,
989 				 XFS_AG_DADDR(mp, agno, XFS_SB_DADDR),
990 				 XFS_FSS_TO_BB(mp, 1), &bp);
991 		/*
992 		 * If we get an error reading or writing alternate superblocks,
993 		 * continue.  xfs_repair chooses the "best" superblock based
994 		 * on most matches; if we break early, we'll leave more
995 		 * superblocks un-updated than updated, and xfs_repair may
996 		 * pick them over the properly-updated primary.
997 		 */
998 		if (error) {
999 			xfs_warn(mp,
1000 		"error allocating secondary superblock for ag %d",
1001 				agno);
1002 			if (!saved_error)
1003 				saved_error = error;
1004 			continue;
1005 		}
1006 
1007 		bp->b_ops = &xfs_sb_buf_ops;
1008 		xfs_buf_oneshot(bp);
1009 		xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
1010 		xfs_sb_to_disk(XFS_BUF_TO_SBP(bp), &mp->m_sb);
1011 		xfs_buf_delwri_queue(bp, &buffer_list);
1012 		xfs_buf_relse(bp);
1013 
1014 		/* don't hold too many buffers at once */
1015 		if (agno % 16)
1016 			continue;
1017 
1018 		error = xfs_buf_delwri_submit(&buffer_list);
1019 		if (error) {
1020 			xfs_warn(mp,
1021 		"write error %d updating a secondary superblock near ag %d",
1022 				error, agno);
1023 			if (!saved_error)
1024 				saved_error = error;
1025 			continue;
1026 		}
1027 	}
1028 	error = xfs_buf_delwri_submit(&buffer_list);
1029 	if (error) {
1030 		xfs_warn(mp,
1031 		"write error %d updating a secondary superblock near ag %d",
1032 			error, agno);
1033 	}
1034 
1035 	return saved_error ? saved_error : error;
1036 }
1037 
1038 /*
1039  * Same behavior as xfs_sync_sb, except that it is always synchronous and it
1040  * also writes the superblock buffer to disk sector 0 immediately.
1041  */
1042 int
1043 xfs_sync_sb_buf(
1044 	struct xfs_mount	*mp)
1045 {
1046 	struct xfs_trans	*tp;
1047 	struct xfs_buf		*bp;
1048 	int			error;
1049 
1050 	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
1051 	if (error)
1052 		return error;
1053 
1054 	bp = xfs_trans_getsb(tp, mp);
1055 	xfs_log_sb(tp);
1056 	xfs_trans_bhold(tp, bp);
1057 	xfs_trans_set_sync(tp);
1058 	error = xfs_trans_commit(tp);
1059 	if (error)
1060 		goto out;
1061 	/*
1062 	 * write out the sb buffer to get the changes to disk
1063 	 */
1064 	error = xfs_bwrite(bp);
1065 out:
1066 	xfs_buf_relse(bp);
1067 	return error;
1068 }
1069 
1070 void
1071 xfs_fs_geometry(
1072 	struct xfs_sb		*sbp,
1073 	struct xfs_fsop_geom	*geo,
1074 	int			struct_version)
1075 {
1076 	memset(geo, 0, sizeof(struct xfs_fsop_geom));
1077 
1078 	geo->blocksize = sbp->sb_blocksize;
1079 	geo->rtextsize = sbp->sb_rextsize;
1080 	geo->agblocks = sbp->sb_agblocks;
1081 	geo->agcount = sbp->sb_agcount;
1082 	geo->logblocks = sbp->sb_logblocks;
1083 	geo->sectsize = sbp->sb_sectsize;
1084 	geo->inodesize = sbp->sb_inodesize;
1085 	geo->imaxpct = sbp->sb_imax_pct;
1086 	geo->datablocks = sbp->sb_dblocks;
1087 	geo->rtblocks = sbp->sb_rblocks;
1088 	geo->rtextents = sbp->sb_rextents;
1089 	geo->logstart = sbp->sb_logstart;
1090 	BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
1091 	memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
1092 
1093 	if (struct_version < 2)
1094 		return;
1095 
1096 	geo->sunit = sbp->sb_unit;
1097 	geo->swidth = sbp->sb_width;
1098 
1099 	if (struct_version < 3)
1100 		return;
1101 
1102 	geo->version = XFS_FSOP_GEOM_VERSION;
1103 	geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
1104 		     XFS_FSOP_GEOM_FLAGS_DIRV2 |
1105 		     XFS_FSOP_GEOM_FLAGS_EXTFLG;
1106 	if (xfs_sb_version_hasattr(sbp))
1107 		geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
1108 	if (xfs_sb_version_hasquota(sbp))
1109 		geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
1110 	if (xfs_sb_version_hasalign(sbp))
1111 		geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
1112 	if (xfs_sb_version_hasdalign(sbp))
1113 		geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
1114 	if (xfs_sb_version_hassector(sbp))
1115 		geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
1116 	if (xfs_sb_version_hasasciici(sbp))
1117 		geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
1118 	if (xfs_sb_version_haslazysbcount(sbp))
1119 		geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
1120 	if (xfs_sb_version_hasattr2(sbp))
1121 		geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
1122 	if (xfs_sb_version_hasprojid32bit(sbp))
1123 		geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
1124 	if (xfs_sb_version_hascrc(sbp))
1125 		geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
1126 	if (xfs_sb_version_hasftype(sbp))
1127 		geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
1128 	if (xfs_sb_version_hasfinobt(sbp))
1129 		geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
1130 	if (xfs_sb_version_hassparseinodes(sbp))
1131 		geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
1132 	if (xfs_sb_version_hasrmapbt(sbp))
1133 		geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
1134 	if (xfs_sb_version_hasreflink(sbp))
1135 		geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
1136 	if (xfs_sb_version_hassector(sbp))
1137 		geo->logsectsize = sbp->sb_logsectsize;
1138 	else
1139 		geo->logsectsize = BBSIZE;
1140 	geo->rtsectsize = sbp->sb_blocksize;
1141 	geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
1142 
1143 	if (struct_version < 4)
1144 		return;
1145 
1146 	if (xfs_sb_version_haslogv2(sbp))
1147 		geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
1148 
1149 	geo->logsunit = sbp->sb_logsunit;
1150 
1151 	if (struct_version < 5)
1152 		return;
1153 
1154 	geo->version = XFS_FSOP_GEOM_VERSION_V5;
1155 }
1156 
1157 /* Read a secondary superblock. */
1158 int
1159 xfs_sb_read_secondary(
1160 	struct xfs_mount	*mp,
1161 	struct xfs_trans	*tp,
1162 	xfs_agnumber_t		agno,
1163 	struct xfs_buf		**bpp)
1164 {
1165 	struct xfs_buf		*bp;
1166 	int			error;
1167 
1168 	ASSERT(agno != 0 && agno != NULLAGNUMBER);
1169 	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
1170 			XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1171 			XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
1172 	if (error)
1173 		return error;
1174 	xfs_buf_set_ref(bp, XFS_SSB_REF);
1175 	*bpp = bp;
1176 	return 0;
1177 }
1178 
1179 /* Get an uninitialised secondary superblock buffer. */
1180 int
1181 xfs_sb_get_secondary(
1182 	struct xfs_mount	*mp,
1183 	struct xfs_trans	*tp,
1184 	xfs_agnumber_t		agno,
1185 	struct xfs_buf		**bpp)
1186 {
1187 	struct xfs_buf		*bp;
1188 	int			error;
1189 
1190 	ASSERT(agno != 0 && agno != NULLAGNUMBER);
1191 	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1192 			XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
1193 			XFS_FSS_TO_BB(mp, 1), 0, &bp);
1194 	if (error)
1195 		return error;
1196 	bp->b_ops = &xfs_sb_buf_ops;
1197 	xfs_buf_oneshot(bp);
1198 	*bpp = bp;
1199 	return 0;
1200 }
1201