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