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