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