1508578f2SNishad Kamdar/* SPDX-License-Identifier: GPL-2.0 */
26ca1c906SDave Chinner/*
36ca1c906SDave Chinner * Copyright (c) 2000-2005 Silicon Graphics, Inc.
46ca1c906SDave Chinner * All Rights Reserved.
56ca1c906SDave Chinner */
66ca1c906SDave Chinner#ifndef __XFS_FORMAT_H__
76ca1c906SDave Chinner#define __XFS_FORMAT_H__
86ca1c906SDave Chinner
96ca1c906SDave Chinner/*
106ca1c906SDave Chinner * XFS On Disk Format Definitions
116ca1c906SDave Chinner *
12b7df7630SDarrick J. Wong * This header file defines all the on-disk format definitions for
136ca1c906SDave Chinner * general XFS objects. Directory and attribute related objects are defined in
146ca1c906SDave Chinner * xfs_da_format.h, which log and log item formats are defined in
156ca1c906SDave Chinner * xfs_log_format.h. Everything else goes here.
166ca1c906SDave Chinner */
176ca1c906SDave Chinner
181fb7e48dSDave Chinnerstruct xfs_mount;
191fb7e48dSDave Chinnerstruct xfs_trans;
201fb7e48dSDave Chinnerstruct xfs_inode;
211fb7e48dSDave Chinnerstruct xfs_buf;
221fb7e48dSDave Chinnerstruct xfs_ifork;
231fb7e48dSDave Chinner
24bb58e618SChristoph Hellwig/*
25bb58e618SChristoph Hellwig * Super block
26bb58e618SChristoph Hellwig * Fits into a sector-sized buffer at address 0 of each allocation group.
27bb58e618SChristoph Hellwig * Only the first of these is ever updated except during growfs.
28bb58e618SChristoph Hellwig */
29bb58e618SChristoph Hellwig#define	XFS_SB_MAGIC		0x58465342	/* 'XFSB' */
30bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_1	1		/* 5.3, 6.0.1, 6.1 */
31bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_2	2		/* 6.2 - attributes */
32bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_3	3		/* 6.2 - new inode version */
33bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_4	4		/* 6.2+ - bitmask version */
34bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_5	5		/* CRC enabled filesystem */
35bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_NUMBITS		0x000f
36bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_ALLFBITS		0xfff0
37bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_ATTRBIT		0x0010
38bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_NLINKBIT		0x0020
39bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_QUOTABIT		0x0040
40bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_ALIGNBIT		0x0080
41bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_DALIGNBIT	0x0100
42bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_SHAREDBIT	0x0200
43bb58e618SChristoph Hellwig#define XFS_SB_VERSION_LOGV2BIT		0x0400
44bb58e618SChristoph Hellwig#define XFS_SB_VERSION_SECTORBIT	0x0800
45bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_EXTFLGBIT	0x1000
46bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_DIRV2BIT		0x2000
47bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_BORGBIT		0x4000	/* ASCII only case-insens. */
48bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_MOREBITSBIT	0x8000
49bb58e618SChristoph Hellwig
5051fcbfe7SJan Tulak/*
5151fcbfe7SJan Tulak * The size of a single extended attribute on disk is limited by
5251fcbfe7SJan Tulak * the size of index values within the attribute entries themselves.
5351fcbfe7SJan Tulak * These are be16 fields, so we can only support attribute data
5451fcbfe7SJan Tulak * sizes up to 2^16 bytes in length.
5551fcbfe7SJan Tulak */
5651fcbfe7SJan Tulak#define XFS_XATTR_SIZE_MAX (1 << 16)
5751fcbfe7SJan Tulak
58bb58e618SChristoph Hellwig/*
59bb58e618SChristoph Hellwig * Supported feature bit list is just all bits in the versionnum field because
60bb58e618SChristoph Hellwig * we've used them all up and understand them all. Except, of course, for the
61bb58e618SChristoph Hellwig * shared superblock bit, which nobody knows what it does and so is unsupported.
62bb58e618SChristoph Hellwig */
63bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_OKBITS		\
64bb58e618SChristoph Hellwig	((XFS_SB_VERSION_NUMBITS | XFS_SB_VERSION_ALLFBITS) & \
65bb58e618SChristoph Hellwig		~XFS_SB_VERSION_SHAREDBIT)
66bb58e618SChristoph Hellwig
67bb58e618SChristoph Hellwig/*
68bb58e618SChristoph Hellwig * There are two words to hold XFS "feature" bits: the original
69bb58e618SChristoph Hellwig * word, sb_versionnum, and sb_features2.  Whenever a bit is set in
70bb58e618SChristoph Hellwig * sb_features2, the feature bit XFS_SB_VERSION_MOREBITSBIT must be set.
71bb58e618SChristoph Hellwig *
72bb58e618SChristoph Hellwig * These defines represent bits in sb_features2.
73bb58e618SChristoph Hellwig */
74bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_RESERVED1BIT	0x00000001
75bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_LAZYSBCOUNTBIT	0x00000002	/* Superblk counters */
76bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_RESERVED4BIT	0x00000004
77bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_ATTR2BIT	0x00000008	/* Inline attr rework */
78bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_PARENTBIT	0x00000010	/* parent pointers */
79bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_PROJID32BIT	0x00000080	/* 32 bit project id */
80bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_CRCBIT		0x00000100	/* metadata CRCs */
81bb58e618SChristoph Hellwig#define XFS_SB_VERSION2_FTYPE		0x00000200	/* inode type in dir */
82bb58e618SChristoph Hellwig
83bb58e618SChristoph Hellwig#define	XFS_SB_VERSION2_OKBITS		\
84bb58e618SChristoph Hellwig	(XFS_SB_VERSION2_LAZYSBCOUNTBIT	| \
85bb58e618SChristoph Hellwig	 XFS_SB_VERSION2_ATTR2BIT	| \
86bb58e618SChristoph Hellwig	 XFS_SB_VERSION2_PROJID32BIT	| \
87bb58e618SChristoph Hellwig	 XFS_SB_VERSION2_FTYPE)
88bb58e618SChristoph Hellwig
89f7664b31SEric Sandeen/* Maximum size of the xfs filesystem label, no terminating NULL */
90f7664b31SEric Sandeen#define XFSLABEL_MAX			12
91f7664b31SEric Sandeen
92bb58e618SChristoph Hellwig/*
93bb58e618SChristoph Hellwig * Superblock - in core version.  Must match the ondisk version below.
94bb58e618SChristoph Hellwig * Must be padded to 64 bit alignment.
95bb58e618SChristoph Hellwig */
96bb58e618SChristoph Hellwigtypedef struct xfs_sb {
97c8ce540dSDarrick J. Wong	uint32_t	sb_magicnum;	/* magic number == XFS_SB_MAGIC */
98c8ce540dSDarrick J. Wong	uint32_t	sb_blocksize;	/* logical block size, bytes */
99bb58e618SChristoph Hellwig	xfs_rfsblock_t	sb_dblocks;	/* number of data blocks */
100bb58e618SChristoph Hellwig	xfs_rfsblock_t	sb_rblocks;	/* number of realtime blocks */
101bb58e618SChristoph Hellwig	xfs_rtblock_t	sb_rextents;	/* number of realtime extents */
102ce748eaaSEric Sandeen	uuid_t		sb_uuid;	/* user-visible file system unique id */
103bb58e618SChristoph Hellwig	xfs_fsblock_t	sb_logstart;	/* starting block of log if internal */
104bb58e618SChristoph Hellwig	xfs_ino_t	sb_rootino;	/* root inode number */
105bb58e618SChristoph Hellwig	xfs_ino_t	sb_rbmino;	/* bitmap inode for realtime extents */
106bb58e618SChristoph Hellwig	xfs_ino_t	sb_rsumino;	/* summary inode for rt bitmap */
107bb58e618SChristoph Hellwig	xfs_agblock_t	sb_rextsize;	/* realtime extent size, blocks */
108bb58e618SChristoph Hellwig	xfs_agblock_t	sb_agblocks;	/* size of an allocation group */
109bb58e618SChristoph Hellwig	xfs_agnumber_t	sb_agcount;	/* number of allocation groups */
110bb58e618SChristoph Hellwig	xfs_extlen_t	sb_rbmblocks;	/* number of rt bitmap blocks */
111bb58e618SChristoph Hellwig	xfs_extlen_t	sb_logblocks;	/* number of log blocks */
112c8ce540dSDarrick J. Wong	uint16_t	sb_versionnum;	/* header version == XFS_SB_VERSION */
113c8ce540dSDarrick J. Wong	uint16_t	sb_sectsize;	/* volume sector size, bytes */
114c8ce540dSDarrick J. Wong	uint16_t	sb_inodesize;	/* inode size, bytes */
115c8ce540dSDarrick J. Wong	uint16_t	sb_inopblock;	/* inodes per block */
116f7664b31SEric Sandeen	char		sb_fname[XFSLABEL_MAX]; /* file system name */
117c8ce540dSDarrick J. Wong	uint8_t		sb_blocklog;	/* log2 of sb_blocksize */
118c8ce540dSDarrick J. Wong	uint8_t		sb_sectlog;	/* log2 of sb_sectsize */
119c8ce540dSDarrick J. Wong	uint8_t		sb_inodelog;	/* log2 of sb_inodesize */
120c8ce540dSDarrick J. Wong	uint8_t		sb_inopblog;	/* log2 of sb_inopblock */
121c8ce540dSDarrick J. Wong	uint8_t		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
122c8ce540dSDarrick J. Wong	uint8_t		sb_rextslog;	/* log2 of sb_rextents */
123c8ce540dSDarrick J. Wong	uint8_t		sb_inprogress;	/* mkfs is in progress, don't mount */
124c8ce540dSDarrick J. Wong	uint8_t		sb_imax_pct;	/* max % of fs for inode space */
125bb58e618SChristoph Hellwig					/* statistics */
126bb58e618SChristoph Hellwig	/*
127bb58e618SChristoph Hellwig	 * These fields must remain contiguous.  If you really
128bb58e618SChristoph Hellwig	 * want to change their layout, make sure you fix the
129bb58e618SChristoph Hellwig	 * code in xfs_trans_apply_sb_deltas().
130bb58e618SChristoph Hellwig	 */
131c8ce540dSDarrick J. Wong	uint64_t	sb_icount;	/* allocated inodes */
132c8ce540dSDarrick J. Wong	uint64_t	sb_ifree;	/* free inodes */
133c8ce540dSDarrick J. Wong	uint64_t	sb_fdblocks;	/* free data blocks */
134c8ce540dSDarrick J. Wong	uint64_t	sb_frextents;	/* free realtime extents */
135bb58e618SChristoph Hellwig	/*
136bb58e618SChristoph Hellwig	 * End contiguous fields.
137bb58e618SChristoph Hellwig	 */
138bb58e618SChristoph Hellwig	xfs_ino_t	sb_uquotino;	/* user quota inode */
139bb58e618SChristoph Hellwig	xfs_ino_t	sb_gquotino;	/* group quota inode */
140c8ce540dSDarrick J. Wong	uint16_t	sb_qflags;	/* quota flags */
141c8ce540dSDarrick J. Wong	uint8_t		sb_flags;	/* misc. flags */
142c8ce540dSDarrick J. Wong	uint8_t		sb_shared_vn;	/* shared version number */
143bb58e618SChristoph Hellwig	xfs_extlen_t	sb_inoalignmt;	/* inode chunk alignment, fsblocks */
144c8ce540dSDarrick J. Wong	uint32_t	sb_unit;	/* stripe or raid unit */
145c8ce540dSDarrick J. Wong	uint32_t	sb_width;	/* stripe or raid width */
146c8ce540dSDarrick J. Wong	uint8_t		sb_dirblklog;	/* log2 of dir block size (fsbs) */
147c8ce540dSDarrick J. Wong	uint8_t		sb_logsectlog;	/* log2 of the log sector size */
148c8ce540dSDarrick J. Wong	uint16_t	sb_logsectsize;	/* sector size for the log, bytes */
149c8ce540dSDarrick J. Wong	uint32_t	sb_logsunit;	/* stripe unit size for the log */
150c8ce540dSDarrick J. Wong	uint32_t	sb_features2;	/* additional feature bits */
151bb58e618SChristoph Hellwig
152bb58e618SChristoph Hellwig	/*
153074e427bSDave Chinner	 * bad features2 field as a result of failing to pad the sb structure to
154074e427bSDave Chinner	 * 64 bits. Some machines will be using this field for features2 bits.
155074e427bSDave Chinner	 * Easiest just to mark it bad and not use it for anything else.
156074e427bSDave Chinner	 *
157074e427bSDave Chinner	 * This is not kept up to date in memory; it is always overwritten by
158074e427bSDave Chinner	 * the value in sb_features2 when formatting the incore superblock to
159074e427bSDave Chinner	 * the disk buffer.
160bb58e618SChristoph Hellwig	 */
161c8ce540dSDarrick J. Wong	uint32_t	sb_bad_features2;
162bb58e618SChristoph Hellwig
163bb58e618SChristoph Hellwig	/* version 5 superblock fields start here */
164bb58e618SChristoph Hellwig
165bb58e618SChristoph Hellwig	/* feature masks */
166c8ce540dSDarrick J. Wong	uint32_t	sb_features_compat;
167c8ce540dSDarrick J. Wong	uint32_t	sb_features_ro_compat;
168c8ce540dSDarrick J. Wong	uint32_t	sb_features_incompat;
169c8ce540dSDarrick J. Wong	uint32_t	sb_features_log_incompat;
170bb58e618SChristoph Hellwig
171c8ce540dSDarrick J. Wong	uint32_t	sb_crc;		/* superblock crc */
172fb4f2b4eSBrian Foster	xfs_extlen_t	sb_spino_align;	/* sparse inode chunk alignment */
173bb58e618SChristoph Hellwig
174bb58e618SChristoph Hellwig	xfs_ino_t	sb_pquotino;	/* project quota inode */
175bb58e618SChristoph Hellwig	xfs_lsn_t	sb_lsn;		/* last write sequence */
176ce748eaaSEric Sandeen	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
177bb58e618SChristoph Hellwig
178bb58e618SChristoph Hellwig	/* must be padded to 64 bit alignment */
179bb58e618SChristoph Hellwig} xfs_sb_t;
180bb58e618SChristoph Hellwig
181bb58e618SChristoph Hellwig#define XFS_SB_CRC_OFF		offsetof(struct xfs_sb, sb_crc)
182bb58e618SChristoph Hellwig
183bb58e618SChristoph Hellwig/*
184bb58e618SChristoph Hellwig * Superblock - on disk version.  Must match the in core version above.
185bb58e618SChristoph Hellwig * Must be padded to 64 bit alignment.
186bb58e618SChristoph Hellwig */
187ed67ebfdSChristoph Hellwigstruct xfs_dsb {
188bb58e618SChristoph Hellwig	__be32		sb_magicnum;	/* magic number == XFS_SB_MAGIC */
189bb58e618SChristoph Hellwig	__be32		sb_blocksize;	/* logical block size, bytes */
190bb58e618SChristoph Hellwig	__be64		sb_dblocks;	/* number of data blocks */
191bb58e618SChristoph Hellwig	__be64		sb_rblocks;	/* number of realtime blocks */
192bb58e618SChristoph Hellwig	__be64		sb_rextents;	/* number of realtime extents */
193ce748eaaSEric Sandeen	uuid_t		sb_uuid;	/* user-visible file system unique id */
194bb58e618SChristoph Hellwig	__be64		sb_logstart;	/* starting block of log if internal */
195bb58e618SChristoph Hellwig	__be64		sb_rootino;	/* root inode number */
196bb58e618SChristoph Hellwig	__be64		sb_rbmino;	/* bitmap inode for realtime extents */
197bb58e618SChristoph Hellwig	__be64		sb_rsumino;	/* summary inode for rt bitmap */
198bb58e618SChristoph Hellwig	__be32		sb_rextsize;	/* realtime extent size, blocks */
199bb58e618SChristoph Hellwig	__be32		sb_agblocks;	/* size of an allocation group */
200bb58e618SChristoph Hellwig	__be32		sb_agcount;	/* number of allocation groups */
201bb58e618SChristoph Hellwig	__be32		sb_rbmblocks;	/* number of rt bitmap blocks */
202bb58e618SChristoph Hellwig	__be32		sb_logblocks;	/* number of log blocks */
203bb58e618SChristoph Hellwig	__be16		sb_versionnum;	/* header version == XFS_SB_VERSION */
204bb58e618SChristoph Hellwig	__be16		sb_sectsize;	/* volume sector size, bytes */
205bb58e618SChristoph Hellwig	__be16		sb_inodesize;	/* inode size, bytes */
206bb58e618SChristoph Hellwig	__be16		sb_inopblock;	/* inodes per block */
207f7664b31SEric Sandeen	char		sb_fname[XFSLABEL_MAX]; /* file system name */
208bb58e618SChristoph Hellwig	__u8		sb_blocklog;	/* log2 of sb_blocksize */
209bb58e618SChristoph Hellwig	__u8		sb_sectlog;	/* log2 of sb_sectsize */
210bb58e618SChristoph Hellwig	__u8		sb_inodelog;	/* log2 of sb_inodesize */
211bb58e618SChristoph Hellwig	__u8		sb_inopblog;	/* log2 of sb_inopblock */
212bb58e618SChristoph Hellwig	__u8		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
213bb58e618SChristoph Hellwig	__u8		sb_rextslog;	/* log2 of sb_rextents */
214bb58e618SChristoph Hellwig	__u8		sb_inprogress;	/* mkfs is in progress, don't mount */
215bb58e618SChristoph Hellwig	__u8		sb_imax_pct;	/* max % of fs for inode space */
216bb58e618SChristoph Hellwig					/* statistics */
217bb58e618SChristoph Hellwig	/*
218bb58e618SChristoph Hellwig	 * These fields must remain contiguous.  If you really
219bb58e618SChristoph Hellwig	 * want to change their layout, make sure you fix the
220bb58e618SChristoph Hellwig	 * code in xfs_trans_apply_sb_deltas().
221bb58e618SChristoph Hellwig	 */
222bb58e618SChristoph Hellwig	__be64		sb_icount;	/* allocated inodes */
223bb58e618SChristoph Hellwig	__be64		sb_ifree;	/* free inodes */
224bb58e618SChristoph Hellwig	__be64		sb_fdblocks;	/* free data blocks */
225bb58e618SChristoph Hellwig	__be64		sb_frextents;	/* free realtime extents */
226bb58e618SChristoph Hellwig	/*
227bb58e618SChristoph Hellwig	 * End contiguous fields.
228bb58e618SChristoph Hellwig	 */
229bb58e618SChristoph Hellwig	__be64		sb_uquotino;	/* user quota inode */
230bb58e618SChristoph Hellwig	__be64		sb_gquotino;	/* group quota inode */
231bb58e618SChristoph Hellwig	__be16		sb_qflags;	/* quota flags */
232bb58e618SChristoph Hellwig	__u8		sb_flags;	/* misc. flags */
233bb58e618SChristoph Hellwig	__u8		sb_shared_vn;	/* shared version number */
234bb58e618SChristoph Hellwig	__be32		sb_inoalignmt;	/* inode chunk alignment, fsblocks */
235bb58e618SChristoph Hellwig	__be32		sb_unit;	/* stripe or raid unit */
236bb58e618SChristoph Hellwig	__be32		sb_width;	/* stripe or raid width */
237bb58e618SChristoph Hellwig	__u8		sb_dirblklog;	/* log2 of dir block size (fsbs) */
238bb58e618SChristoph Hellwig	__u8		sb_logsectlog;	/* log2 of the log sector size */
239bb58e618SChristoph Hellwig	__be16		sb_logsectsize;	/* sector size for the log, bytes */
240bb58e618SChristoph Hellwig	__be32		sb_logsunit;	/* stripe unit size for the log */
241bb58e618SChristoph Hellwig	__be32		sb_features2;	/* additional feature bits */
242bb58e618SChristoph Hellwig	/*
243bb58e618SChristoph Hellwig	 * bad features2 field as a result of failing to pad the sb
244bb58e618SChristoph Hellwig	 * structure to 64 bits. Some machines will be using this field
245bb58e618SChristoph Hellwig	 * for features2 bits. Easiest just to mark it bad and not use
246bb58e618SChristoph Hellwig	 * it for anything else.
247bb58e618SChristoph Hellwig	 */
248bb58e618SChristoph Hellwig	__be32		sb_bad_features2;
249bb58e618SChristoph Hellwig
250bb58e618SChristoph Hellwig	/* version 5 superblock fields start here */
251bb58e618SChristoph Hellwig
252bb58e618SChristoph Hellwig	/* feature masks */
253bb58e618SChristoph Hellwig	__be32		sb_features_compat;
254bb58e618SChristoph Hellwig	__be32		sb_features_ro_compat;
255bb58e618SChristoph Hellwig	__be32		sb_features_incompat;
256bb58e618SChristoph Hellwig	__be32		sb_features_log_incompat;
257bb58e618SChristoph Hellwig
258bb58e618SChristoph Hellwig	__le32		sb_crc;		/* superblock crc */
259fb4f2b4eSBrian Foster	__be32		sb_spino_align;	/* sparse inode chunk alignment */
260bb58e618SChristoph Hellwig
261bb58e618SChristoph Hellwig	__be64		sb_pquotino;	/* project quota inode */
262bb58e618SChristoph Hellwig	__be64		sb_lsn;		/* last write sequence */
263ce748eaaSEric Sandeen	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
264bb58e618SChristoph Hellwig
265bb58e618SChristoph Hellwig	/* must be padded to 64 bit alignment */
266ed67ebfdSChristoph Hellwig};
267bb58e618SChristoph Hellwig
268bb58e618SChristoph Hellwig/*
269bb58e618SChristoph Hellwig * Misc. Flags - warning - these will be cleared by xfs_repair unless
270bb58e618SChristoph Hellwig * a feature bit is set when the flag is used.
271bb58e618SChristoph Hellwig */
272bb58e618SChristoph Hellwig#define XFS_SBF_NOFLAGS		0x00	/* no flags set */
273bb58e618SChristoph Hellwig#define XFS_SBF_READONLY	0x01	/* only read-only mounts allowed */
274bb58e618SChristoph Hellwig
275bb58e618SChristoph Hellwig/*
276bb58e618SChristoph Hellwig * define max. shared version we can interoperate with
277bb58e618SChristoph Hellwig */
278bb58e618SChristoph Hellwig#define XFS_SB_MAX_SHARED_VN	0
279bb58e618SChristoph Hellwig
280bb58e618SChristoph Hellwig#define	XFS_SB_VERSION_NUM(sbp)	((sbp)->sb_versionnum & XFS_SB_VERSION_NUMBITS)
281bb58e618SChristoph Hellwig
282d6837c1aSDave Chinnerstatic inline bool xfs_sb_is_v5(struct xfs_sb *sbp)
283d6837c1aSDave Chinner{
284d6837c1aSDave Chinner	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5;
285d6837c1aSDave Chinner}
286d6837c1aSDave Chinner
287bb58e618SChristoph Hellwig/*
288bb58e618SChristoph Hellwig * Detect a mismatched features2 field.  Older kernels read/wrote
289bb58e618SChristoph Hellwig * this into the wrong slot, so to be safe we keep them in sync.
290bb58e618SChristoph Hellwig */
291bb58e618SChristoph Hellwigstatic inline bool xfs_sb_has_mismatched_features2(struct xfs_sb *sbp)
292bb58e618SChristoph Hellwig{
293bb58e618SChristoph Hellwig	return sbp->sb_bad_features2 != sbp->sb_features2;
294bb58e618SChristoph Hellwig}
295bb58e618SChristoph Hellwig
2962beb7b50SDave Chinnerstatic inline bool xfs_sb_version_hasmorebits(struct xfs_sb *sbp)
297bb58e618SChristoph Hellwig{
298d6837c1aSDave Chinner	return xfs_sb_is_v5(sbp) ||
2992beb7b50SDave Chinner	       (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT);
300bb58e618SChristoph Hellwig}
301bb58e618SChristoph Hellwig
302bb58e618SChristoph Hellwigstatic inline void xfs_sb_version_addattr(struct xfs_sb *sbp)
303bb58e618SChristoph Hellwig{
304bb58e618SChristoph Hellwig	sbp->sb_versionnum |= XFS_SB_VERSION_ATTRBIT;
305bb58e618SChristoph Hellwig}
306bb58e618SChristoph Hellwig
307bb58e618SChristoph Hellwigstatic inline void xfs_sb_version_addquota(struct xfs_sb *sbp)
308bb58e618SChristoph Hellwig{
309bb58e618SChristoph Hellwig	sbp->sb_versionnum |= XFS_SB_VERSION_QUOTABIT;
310bb58e618SChristoph Hellwig}
311bb58e618SChristoph Hellwig
312bb58e618SChristoph Hellwigstatic inline void xfs_sb_version_addattr2(struct xfs_sb *sbp)
313bb58e618SChristoph Hellwig{
314bb58e618SChristoph Hellwig	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
315bb58e618SChristoph Hellwig	sbp->sb_features2 |= XFS_SB_VERSION2_ATTR2BIT;
316bb58e618SChristoph Hellwig}
317bb58e618SChristoph Hellwig
318a1d86e8dSDave Chinnerstatic inline void xfs_sb_version_addprojid32(struct xfs_sb *sbp)
319bb58e618SChristoph Hellwig{
320bb58e618SChristoph Hellwig	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
321bb58e618SChristoph Hellwig	sbp->sb_features2 |= XFS_SB_VERSION2_PROJID32BIT;
322bb58e618SChristoph Hellwig}
323bb58e618SChristoph Hellwig
324bb58e618SChristoph Hellwig/*
325bb58e618SChristoph Hellwig * Extended v5 superblock feature masks. These are to be used for new v5
326bb58e618SChristoph Hellwig * superblock features only.
327bb58e618SChristoph Hellwig *
328bb58e618SChristoph Hellwig * Compat features are new features that old kernels will not notice or affect
329bb58e618SChristoph Hellwig * and so can mount read-write without issues.
330bb58e618SChristoph Hellwig *
331bb58e618SChristoph Hellwig * RO-Compat (read only) are features that old kernels can read but will break
332bb58e618SChristoph Hellwig * if they write. Hence only read-only mounts of such filesystems are allowed on
333bb58e618SChristoph Hellwig * kernels that don't support the feature bit.
334bb58e618SChristoph Hellwig *
335bb58e618SChristoph Hellwig * InCompat features are features which old kernels will not understand and so
336bb58e618SChristoph Hellwig * must not mount.
337bb58e618SChristoph Hellwig *
338bb58e618SChristoph Hellwig * Log-InCompat features are for changes to log formats or new transactions that
339bb58e618SChristoph Hellwig * can't be replayed on older kernels. The fields are set when the filesystem is
340bb58e618SChristoph Hellwig * mounted, and a clean unmount clears the fields.
341bb58e618SChristoph Hellwig */
342bb58e618SChristoph Hellwig#define XFS_SB_FEAT_COMPAT_ALL 0
343bb58e618SChristoph Hellwig#define XFS_SB_FEAT_COMPAT_UNKNOWN	~XFS_SB_FEAT_COMPAT_ALL
344bb58e618SChristoph Hellwigstatic inline bool
345bb58e618SChristoph Hellwigxfs_sb_has_compat_feature(
346bb58e618SChristoph Hellwig	struct xfs_sb	*sbp,
347c8ce540dSDarrick J. Wong	uint32_t	feature)
348bb58e618SChristoph Hellwig{
349bb58e618SChristoph Hellwig	return (sbp->sb_features_compat & feature) != 0;
350bb58e618SChristoph Hellwig}
351bb58e618SChristoph Hellwig
352bb58e618SChristoph Hellwig#define XFS_SB_FEAT_RO_COMPAT_FINOBT   (1 << 0)		/* free inode btree */
353b8704944SDarrick J. Wong#define XFS_SB_FEAT_RO_COMPAT_RMAPBT   (1 << 1)		/* reverse map btree */
35446eeb521SDarrick J. Wong#define XFS_SB_FEAT_RO_COMPAT_REFLINK  (1 << 2)		/* reflinked files */
3552a39946cSDarrick J. Wong#define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)		/* inobt block counts */
356bb58e618SChristoph Hellwig#define XFS_SB_FEAT_RO_COMPAT_ALL \
3571c0607acSDarrick J. Wong		(XFS_SB_FEAT_RO_COMPAT_FINOBT | \
358e54b5bf9SDarrick J. Wong		 XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
359b896a39fSDarrick J. Wong		 XFS_SB_FEAT_RO_COMPAT_REFLINK| \
360b896a39fSDarrick J. Wong		 XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
362bb58e618SChristoph Hellwigstatic inline bool
363bb58e618SChristoph Hellwigxfs_sb_has_ro_compat_feature(
364bb58e618SChristoph Hellwig	struct xfs_sb	*sbp,
365c8ce540dSDarrick J. Wong	uint32_t	feature)
366bb58e618SChristoph Hellwig{
367bb58e618SChristoph Hellwig	return (sbp->sb_features_ro_compat & feature) != 0;
368bb58e618SChristoph Hellwig}
369bb58e618SChristoph Hellwig
370bb58e618SChristoph Hellwig#define XFS_SB_FEAT_INCOMPAT_FTYPE	(1 << 0)	/* filetype in dirent */
371e5376fc1SBrian Foster#define XFS_SB_FEAT_INCOMPAT_SPINODES	(1 << 1)	/* sparse inode chunks */
372ce748eaaSEric Sandeen#define XFS_SB_FEAT_INCOMPAT_META_UUID	(1 << 2)	/* metadata UUID */
373f93e5436SDarrick J. Wong#define XFS_SB_FEAT_INCOMPAT_BIGTIME	(1 << 3)	/* large timestamps */
37480c720b8SDarrick J. Wong#define XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR (1 << 4)	/* needs xfs_repair */
375bb58e618SChristoph Hellwig#define XFS_SB_FEAT_INCOMPAT_ALL \
37622ce1e14SBrian Foster		(XFS_SB_FEAT_INCOMPAT_FTYPE|	\
377ce748eaaSEric Sandeen		 XFS_SB_FEAT_INCOMPAT_SPINODES|	\
37829887a22SDarrick J. Wong		 XFS_SB_FEAT_INCOMPAT_META_UUID| \
37996f65badSDarrick J. Wong		 XFS_SB_FEAT_INCOMPAT_BIGTIME| \
381bb58e618SChristoph Hellwig
383bb58e618SChristoph Hellwigstatic inline bool
384bb58e618SChristoph Hellwigxfs_sb_has_incompat_feature(
385bb58e618SChristoph Hellwig	struct xfs_sb	*sbp,
386c8ce540dSDarrick J. Wong	uint32_t	feature)
387bb58e618SChristoph Hellwig{
388bb58e618SChristoph Hellwig	return (sbp->sb_features_incompat & feature) != 0;
389bb58e618SChristoph Hellwig}
390bb58e618SChristoph Hellwig
391bb58e618SChristoph Hellwig#define XFS_SB_FEAT_INCOMPAT_LOG_ALL 0
393bb58e618SChristoph Hellwigstatic inline bool
394bb58e618SChristoph Hellwigxfs_sb_has_incompat_log_feature(
395bb58e618SChristoph Hellwig	struct xfs_sb	*sbp,
396c8ce540dSDarrick J. Wong	uint32_t	feature)
397bb58e618SChristoph Hellwig{
398bb58e618SChristoph Hellwig	return (sbp->sb_features_log_incompat & feature) != 0;
399bb58e618SChristoph Hellwig}
400bb58e618SChristoph Hellwig
401908ce71eSDarrick J. Wongstatic inline void
402908ce71eSDarrick J. Wongxfs_sb_remove_incompat_log_features(
403908ce71eSDarrick J. Wong	struct xfs_sb	*sbp)
404908ce71eSDarrick J. Wong{
405908ce71eSDarrick J. Wong	sbp->sb_features_log_incompat &= ~XFS_SB_FEAT_INCOMPAT_LOG_ALL;
406908ce71eSDarrick J. Wong}
407908ce71eSDarrick J. Wong
408908ce71eSDarrick J. Wongstatic inline void
409908ce71eSDarrick J. Wongxfs_sb_add_incompat_log_features(
410908ce71eSDarrick J. Wong	struct xfs_sb	*sbp,
411908ce71eSDarrick J. Wong	unsigned int	features)
412908ce71eSDarrick J. Wong{
413908ce71eSDarrick J. Wong	sbp->sb_features_log_incompat |= features;
414908ce71eSDarrick J. Wong}
415908ce71eSDarrick J. Wong
416bb58e618SChristoph Hellwig
417bb58e618SChristoph Hellwigstatic inline bool
418bb58e618SChristoph Hellwigxfs_is_quota_inode(struct xfs_sb *sbp, xfs_ino_t ino)
419bb58e618SChristoph Hellwig{
420bb58e618SChristoph Hellwig	return (ino == sbp->sb_uquotino ||
421bb58e618SChristoph Hellwig		ino == sbp->sb_gquotino ||
422bb58e618SChristoph Hellwig		ino == sbp->sb_pquotino);
423bb58e618SChristoph Hellwig}
424bb58e618SChristoph Hellwig
425bb58e618SChristoph Hellwig#define XFS_SB_DADDR		((xfs_daddr_t)0) /* daddr in filesystem/ag */
426bb58e618SChristoph Hellwig#define	XFS_SB_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_SB_DADDR)
427bb58e618SChristoph Hellwig
428bb58e618SChristoph Hellwig#define	XFS_HDR_BLOCK(mp,d)	((xfs_agblock_t)XFS_BB_TO_FSBT(mp,d))
429bb58e618SChristoph Hellwig#define	XFS_DADDR_TO_FSB(mp,d)	XFS_AGB_TO_FSB(mp, \
430bb58e618SChristoph Hellwig			xfs_daddr_to_agno(mp,d), xfs_daddr_to_agbno(mp,d))
431bb58e618SChristoph Hellwig#define	XFS_FSB_TO_DADDR(mp,fsbno)	XFS_AGB_TO_DADDR(mp, \
432bb58e618SChristoph Hellwig			XFS_FSB_TO_AGNO(mp,fsbno), XFS_FSB_TO_AGBNO(mp,fsbno))
433bb58e618SChristoph Hellwig
434bb58e618SChristoph Hellwig/*
435bb58e618SChristoph Hellwig * File system sector to basic block conversions.
436bb58e618SChristoph Hellwig */
437bb58e618SChristoph Hellwig#define XFS_FSS_TO_BB(mp,sec)	((sec) << (mp)->m_sectbb_log)
438bb58e618SChristoph Hellwig
439bb58e618SChristoph Hellwig/*
440bb58e618SChristoph Hellwig * File system block to basic block conversions.
441bb58e618SChristoph Hellwig */
442bb58e618SChristoph Hellwig#define	XFS_FSB_TO_BB(mp,fsbno)	((fsbno) << (mp)->m_blkbb_log)
443bb58e618SChristoph Hellwig#define	XFS_BB_TO_FSB(mp,bb)	\
444bb58e618SChristoph Hellwig	(((bb) + (XFS_FSB_TO_BB(mp,1) - 1)) >> (mp)->m_blkbb_log)
445bb58e618SChristoph Hellwig#define	XFS_BB_TO_FSBT(mp,bb)	((bb) >> (mp)->m_blkbb_log)
446bb58e618SChristoph Hellwig
447bb58e618SChristoph Hellwig/*
448bb58e618SChristoph Hellwig * File system block to byte conversions.
449bb58e618SChristoph Hellwig */
450bb58e618SChristoph Hellwig#define XFS_FSB_TO_B(mp,fsbno)	((xfs_fsize_t)(fsbno) << (mp)->m_sb.sb_blocklog)
451bb58e618SChristoph Hellwig#define XFS_B_TO_FSB(mp,b)	\
452c8ce540dSDarrick J. Wong	((((uint64_t)(b)) + (mp)->m_blockmask) >> (mp)->m_sb.sb_blocklog)
453c8ce540dSDarrick J. Wong#define XFS_B_TO_FSBT(mp,b)	(((uint64_t)(b)) >> (mp)->m_sb.sb_blocklog)
454bb58e618SChristoph Hellwig
4554fb6e8adSChristoph Hellwig/*
4564fb6e8adSChristoph Hellwig * Allocation group header
4574fb6e8adSChristoph Hellwig *
4584fb6e8adSChristoph Hellwig * This is divided into three structures, placed in sequential 512-byte
4594fb6e8adSChristoph Hellwig * buffers after a copy of the superblock (also in a 512-byte buffer).
4604fb6e8adSChristoph Hellwig */
4614fb6e8adSChristoph Hellwig#define	XFS_AGF_MAGIC	0x58414746	/* 'XAGF' */
4624fb6e8adSChristoph Hellwig#define	XFS_AGI_MAGIC	0x58414749	/* 'XAGI' */
4634fb6e8adSChristoph Hellwig#define	XFS_AGFL_MAGIC	0x5841464c	/* 'XAFL' */
4644fb6e8adSChristoph Hellwig#define	XFS_AGF_VERSION	1
4654fb6e8adSChristoph Hellwig#define	XFS_AGI_VERSION	1
4664fb6e8adSChristoph Hellwig
4674fb6e8adSChristoph Hellwig#define	XFS_AGF_GOOD_VERSION(v)	((v) == XFS_AGF_VERSION)
4684fb6e8adSChristoph Hellwig#define	XFS_AGI_GOOD_VERSION(v)	((v) == XFS_AGI_VERSION)
4694fb6e8adSChristoph Hellwig
4704fb6e8adSChristoph Hellwig/*
471b8704944SDarrick J. Wong * Btree number 0 is bno, 1 is cnt, 2 is rmap. This value gives the size of the
4724fb6e8adSChristoph Hellwig * arrays below.
4734fb6e8adSChristoph Hellwig */
474b8704944SDarrick J. Wong#define	XFS_BTNUM_AGF	((int)XFS_BTNUM_RMAPi + 1)
4754fb6e8adSChristoph Hellwig
4764fb6e8adSChristoph Hellwig/*
4774fb6e8adSChristoph Hellwig * The second word of agf_levels in the first a.g. overlaps the EFS
4784fb6e8adSChristoph Hellwig * superblock's magic number.  Since the magic numbers valid for EFS
4794fb6e8adSChristoph Hellwig * are > 64k, our value cannot be confused for an EFS superblock's.
4804fb6e8adSChristoph Hellwig */
4814fb6e8adSChristoph Hellwig
4824fb6e8adSChristoph Hellwigtypedef struct xfs_agf {
4834fb6e8adSChristoph Hellwig	/*
4844fb6e8adSChristoph Hellwig	 * Common allocation group header information
4854fb6e8adSChristoph Hellwig	 */
4864fb6e8adSChristoph Hellwig	__be32		agf_magicnum;	/* magic number == XFS_AGF_MAGIC */
4874fb6e8adSChristoph Hellwig	__be32		agf_versionnum;	/* header version == XFS_AGF_VERSION */
4884fb6e8adSChristoph Hellwig	__be32		agf_seqno;	/* sequence # starting from 0 */
4894fb6e8adSChristoph Hellwig	__be32		agf_length;	/* size in blocks of a.g. */
4904fb6e8adSChristoph Hellwig	/*
491b8704944SDarrick J. Wong	 * Freespace and rmap information
4924fb6e8adSChristoph Hellwig	 */
4934fb6e8adSChristoph Hellwig	__be32		agf_roots[XFS_BTNUM_AGF];	/* root blocks */
4944fb6e8adSChristoph Hellwig	__be32		agf_levels[XFS_BTNUM_AGF];	/* btree levels */
4954fb6e8adSChristoph Hellwig
4964fb6e8adSChristoph Hellwig	__be32		agf_flfirst;	/* first freelist block's index */
4974fb6e8adSChristoph Hellwig	__be32		agf_fllast;	/* last freelist block's index */
4984fb6e8adSChristoph Hellwig	__be32		agf_flcount;	/* count of blocks in freelist */
4994fb6e8adSChristoph Hellwig	__be32		agf_freeblks;	/* total free blocks */
5004fb6e8adSChristoph Hellwig
5014fb6e8adSChristoph Hellwig	__be32		agf_longest;	/* longest free space */
5024fb6e8adSChristoph Hellwig	__be32		agf_btreeblks;	/* # of blocks held in AGF btrees */
5034fb6e8adSChristoph Hellwig	uuid_t		agf_uuid;	/* uuid of filesystem */
5044fb6e8adSChristoph Hellwig
505f32866fdSDarrick J. Wong	__be32		agf_rmap_blocks;	/* rmapbt blocks used */
50646eeb521SDarrick J. Wong	__be32		agf_refcount_blocks;	/* refcountbt blocks used */
50746eeb521SDarrick J. Wong
50846eeb521SDarrick J. Wong	__be32		agf_refcount_root;	/* refcount tree root block */
50946eeb521SDarrick J. Wong	__be32		agf_refcount_level;	/* refcount btree levels */
510f32866fdSDarrick J. Wong
5114fb6e8adSChristoph Hellwig	/*
5124fb6e8adSChristoph Hellwig	 * reserve some contiguous space for future logged fields before we add
5134fb6e8adSChristoph Hellwig	 * the unlogged fields. This makes the range logging via flags and
5144fb6e8adSChristoph Hellwig	 * structure offsets much simpler.
5154fb6e8adSChristoph Hellwig	 */
51646eeb521SDarrick J. Wong	__be64		agf_spare64[14];
5174fb6e8adSChristoph Hellwig
5184fb6e8adSChristoph Hellwig	/* unlogged fields, written during buffer writeback. */
5194fb6e8adSChristoph Hellwig	__be64		agf_lsn;	/* last write sequence */
5204fb6e8adSChristoph Hellwig	__be32		agf_crc;	/* crc of agf sector */
5214fb6e8adSChristoph Hellwig	__be32		agf_spare2;
5224fb6e8adSChristoph Hellwig
5234fb6e8adSChristoph Hellwig	/* structure must be padded to 64 bit alignment */
5244fb6e8adSChristoph Hellwig} xfs_agf_t;
5254fb6e8adSChristoph Hellwig
5264fb6e8adSChristoph Hellwig#define XFS_AGF_CRC_OFF		offsetof(struct xfs_agf, agf_crc)
5274fb6e8adSChristoph Hellwig
5284fb6e8adSChristoph Hellwig#define	XFS_AGF_MAGICNUM	0x00000001
5294fb6e8adSChristoph Hellwig#define	XFS_AGF_VERSIONNUM	0x00000002
5304fb6e8adSChristoph Hellwig#define	XFS_AGF_SEQNO		0x00000004
5314fb6e8adSChristoph Hellwig#define	XFS_AGF_LENGTH		0x00000008
5324fb6e8adSChristoph Hellwig#define	XFS_AGF_ROOTS		0x00000010
5334fb6e8adSChristoph Hellwig#define	XFS_AGF_LEVELS		0x00000020
5344fb6e8adSChristoph Hellwig#define	XFS_AGF_FLFIRST		0x00000040
5354fb6e8adSChristoph Hellwig#define	XFS_AGF_FLLAST		0x00000080
5364fb6e8adSChristoph Hellwig#define	XFS_AGF_FLCOUNT		0x00000100
5374fb6e8adSChristoph Hellwig#define	XFS_AGF_FREEBLKS	0x00000200
5384fb6e8adSChristoph Hellwig#define	XFS_AGF_LONGEST		0x00000400
5394fb6e8adSChristoph Hellwig#define	XFS_AGF_BTREEBLKS	0x00000800
5404fb6e8adSChristoph Hellwig#define	XFS_AGF_UUID		0x00001000
541f32866fdSDarrick J. Wong#define	XFS_AGF_RMAP_BLOCKS	0x00002000
542bdf28630SDarrick J. Wong#define	XFS_AGF_REFCOUNT_BLOCKS	0x00004000
543bdf28630SDarrick J. Wong#define	XFS_AGF_REFCOUNT_ROOT	0x00008000
544bdf28630SDarrick J. Wong#define	XFS_AGF_REFCOUNT_LEVEL	0x00010000
545bdf28630SDarrick J. Wong#define	XFS_AGF_SPARE64		0x00020000
546bdf28630SDarrick J. Wong#define	XFS_AGF_NUM_BITS	18
5474fb6e8adSChristoph Hellwig#define	XFS_AGF_ALL_BITS	((1 << XFS_AGF_NUM_BITS) - 1)
5484fb6e8adSChristoph Hellwig
5494fb6e8adSChristoph Hellwig#define XFS_AGF_FLAGS \
5504fb6e8adSChristoph Hellwig	{ XFS_AGF_MAGICNUM,	"MAGICNUM" }, \
5514fb6e8adSChristoph Hellwig	{ XFS_AGF_VERSIONNUM,	"VERSIONNUM" }, \
5524fb6e8adSChristoph Hellwig	{ XFS_AGF_SEQNO,	"SEQNO" }, \
5534fb6e8adSChristoph Hellwig	{ XFS_AGF_LENGTH,	"LENGTH" }, \
5544fb6e8adSChristoph Hellwig	{ XFS_AGF_ROOTS,	"ROOTS" }, \
5554fb6e8adSChristoph Hellwig	{ XFS_AGF_LEVELS,	"LEVELS" }, \
5564fb6e8adSChristoph Hellwig	{ XFS_AGF_FLFIRST,	"FLFIRST" }, \
5574fb6e8adSChristoph Hellwig	{ XFS_AGF_FLLAST,	"FLLAST" }, \
5584fb6e8adSChristoph Hellwig	{ XFS_AGF_FLCOUNT,	"FLCOUNT" }, \
5594fb6e8adSChristoph Hellwig	{ XFS_AGF_FREEBLKS,	"FREEBLKS" }, \
5604fb6e8adSChristoph Hellwig	{ XFS_AGF_LONGEST,	"LONGEST" }, \
5614fb6e8adSChristoph Hellwig	{ XFS_AGF_BTREEBLKS,	"BTREEBLKS" }, \
562f32866fdSDarrick J. Wong	{ XFS_AGF_UUID,		"UUID" }, \
563da1f039dSDarrick J. Wong	{ XFS_AGF_RMAP_BLOCKS,	"RMAP_BLOCKS" }, \
564bdf28630SDarrick J. Wong	{ XFS_AGF_REFCOUNT_BLOCKS,	"REFCOUNT_BLOCKS" }, \
565bdf28630SDarrick J. Wong	{ XFS_AGF_REFCOUNT_ROOT,	"REFCOUNT_ROOT" }, \
566bdf28630SDarrick J. Wong	{ XFS_AGF_REFCOUNT_LEVEL,	"REFCOUNT_LEVEL" }, \
567da1f039dSDarrick J. Wong	{ XFS_AGF_SPARE64,	"SPARE64" }
5684fb6e8adSChristoph Hellwig
5694fb6e8adSChristoph Hellwig/* disk block (xfs_daddr_t) in the AG */
5704fb6e8adSChristoph Hellwig#define XFS_AGF_DADDR(mp)	((xfs_daddr_t)(1 << (mp)->m_sectbb_log))
5714fb6e8adSChristoph Hellwig#define	XFS_AGF_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGF_DADDR(mp))
5724fb6e8adSChristoph Hellwig
5734fb6e8adSChristoph Hellwig/*
5744fb6e8adSChristoph Hellwig * Size of the unlinked inode hash table in the agi.
5754fb6e8adSChristoph Hellwig */
5764fb6e8adSChristoph Hellwig#define	XFS_AGI_UNLINKED_BUCKETS	64
5774fb6e8adSChristoph Hellwig
5784fb6e8adSChristoph Hellwigtypedef struct xfs_agi {
5794fb6e8adSChristoph Hellwig	/*
5804fb6e8adSChristoph Hellwig	 * Common allocation group header information
5814fb6e8adSChristoph Hellwig	 */
5824fb6e8adSChristoph Hellwig	__be32		agi_magicnum;	/* magic number == XFS_AGI_MAGIC */
5834fb6e8adSChristoph Hellwig	__be32		agi_versionnum;	/* header version == XFS_AGI_VERSION */
5844fb6e8adSChristoph Hellwig	__be32		agi_seqno;	/* sequence # starting from 0 */
5854fb6e8adSChristoph Hellwig	__be32		agi_length;	/* size in blocks of a.g. */
5864fb6e8adSChristoph Hellwig	/*
5874fb6e8adSChristoph Hellwig	 * Inode information
5884fb6e8adSChristoph Hellwig	 * Inodes are mapped by interpreting the inode number, so no
5894fb6e8adSChristoph Hellwig	 * mapping data is needed here.
5904fb6e8adSChristoph Hellwig	 */
5914fb6e8adSChristoph Hellwig	__be32		agi_count;	/* count of allocated inodes */
5924fb6e8adSChristoph Hellwig	__be32		agi_root;	/* root of inode btree */
5934fb6e8adSChristoph Hellwig	__be32		agi_level;	/* levels in inode btree */
5944fb6e8adSChristoph Hellwig	__be32		agi_freecount;	/* number of free inodes */
5954fb6e8adSChristoph Hellwig
5964fb6e8adSChristoph Hellwig	__be32		agi_newino;	/* new inode just allocated */
5974fb6e8adSChristoph Hellwig	__be32		agi_dirino;	/* last directory inode chunk */
5984fb6e8adSChristoph Hellwig	/*
5994fb6e8adSChristoph Hellwig	 * Hash table of inodes which have been unlinked but are
6004fb6e8adSChristoph Hellwig	 * still being referenced.
6014fb6e8adSChristoph Hellwig	 */
6024fb6e8adSChristoph Hellwig	__be32		agi_unlinked[XFS_AGI_UNLINKED_BUCKETS];
6034fb6e8adSChristoph Hellwig	/*
6044fb6e8adSChristoph Hellwig	 * This marks the end of logging region 1 and start of logging region 2.
6054fb6e8adSChristoph Hellwig	 */
6064fb6e8adSChristoph Hellwig	uuid_t		agi_uuid;	/* uuid of filesystem */
6074fb6e8adSChristoph Hellwig	__be32		agi_crc;	/* crc of agi sector */
6084fb6e8adSChristoph Hellwig	__be32		agi_pad32;
6094fb6e8adSChristoph Hellwig	__be64		agi_lsn;	/* last write sequence */
6104fb6e8adSChristoph Hellwig
6114fb6e8adSChristoph Hellwig	__be32		agi_free_root; /* root of the free inode btree */
6124fb6e8adSChristoph Hellwig	__be32		agi_free_level;/* levels in free inode btree */
6134fb6e8adSChristoph Hellwig
6142a39946cSDarrick J. Wong	__be32		agi_iblocks;	/* inobt blocks used */
6152a39946cSDarrick J. Wong	__be32		agi_fblocks;	/* finobt blocks used */
6162a39946cSDarrick J. Wong
6174fb6e8adSChristoph Hellwig	/* structure must be padded to 64 bit alignment */
6184fb6e8adSChristoph Hellwig} xfs_agi_t;
6194fb6e8adSChristoph Hellwig
6204fb6e8adSChristoph Hellwig#define XFS_AGI_CRC_OFF		offsetof(struct xfs_agi, agi_crc)
6214fb6e8adSChristoph Hellwig
6224fb6e8adSChristoph Hellwig#define	XFS_AGI_MAGICNUM	(1 << 0)
6234fb6e8adSChristoph Hellwig#define	XFS_AGI_VERSIONNUM	(1 << 1)
6244fb6e8adSChristoph Hellwig#define	XFS_AGI_SEQNO		(1 << 2)
6254fb6e8adSChristoph Hellwig#define	XFS_AGI_LENGTH		(1 << 3)
6264fb6e8adSChristoph Hellwig#define	XFS_AGI_COUNT		(1 << 4)
6274fb6e8adSChristoph Hellwig#define	XFS_AGI_ROOT		(1 << 5)
6284fb6e8adSChristoph Hellwig#define	XFS_AGI_LEVEL		(1 << 6)
6294fb6e8adSChristoph Hellwig#define	XFS_AGI_FREECOUNT	(1 << 7)
6304fb6e8adSChristoph Hellwig#define	XFS_AGI_NEWINO		(1 << 8)
6314fb6e8adSChristoph Hellwig#define	XFS_AGI_DIRINO		(1 << 9)
6324fb6e8adSChristoph Hellwig#define	XFS_AGI_UNLINKED	(1 << 10)
6334fb6e8adSChristoph Hellwig#define	XFS_AGI_NUM_BITS_R1	11	/* end of the 1st agi logging region */
6344fb6e8adSChristoph Hellwig#define	XFS_AGI_ALL_BITS_R1	((1 << XFS_AGI_NUM_BITS_R1) - 1)
6354fb6e8adSChristoph Hellwig#define	XFS_AGI_FREE_ROOT	(1 << 11)
6364fb6e8adSChristoph Hellwig#define	XFS_AGI_FREE_LEVEL	(1 << 12)
6372a39946cSDarrick J. Wong#define	XFS_AGI_IBLOCKS		(1 << 13) /* both inobt/finobt block counters */
6382a39946cSDarrick J. Wong#define	XFS_AGI_NUM_BITS_R2	14
6394fb6e8adSChristoph Hellwig
6404fb6e8adSChristoph Hellwig/* disk block (xfs_daddr_t) in the AG */
6414fb6e8adSChristoph Hellwig#define XFS_AGI_DADDR(mp)	((xfs_daddr_t)(2 << (mp)->m_sectbb_log))
6424fb6e8adSChristoph Hellwig#define	XFS_AGI_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGI_DADDR(mp))
6434fb6e8adSChristoph Hellwig
6444fb6e8adSChristoph Hellwig/*
6454fb6e8adSChristoph Hellwig * The third a.g. block contains the a.g. freelist, an array
6464fb6e8adSChristoph Hellwig * of block pointers to blocks owned by the allocation btree code.
6474fb6e8adSChristoph Hellwig */
6484fb6e8adSChristoph Hellwig#define XFS_AGFL_DADDR(mp)	((xfs_daddr_t)(3 << (mp)->m_sectbb_log))
6494fb6e8adSChristoph Hellwig#define	XFS_AGFL_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGFL_DADDR(mp))
6504b975108SChristoph Hellwig#define	XFS_BUF_TO_AGFL(bp)	((struct xfs_agfl *)((bp)->b_addr))
6514fb6e8adSChristoph Hellwig
6524b975108SChristoph Hellwigstruct xfs_agfl {
6534fb6e8adSChristoph Hellwig	__be32		agfl_magicnum;
6544fb6e8adSChristoph Hellwig	__be32		agfl_seqno;
6554fb6e8adSChristoph Hellwig	uuid_t		agfl_uuid;
6564fb6e8adSChristoph Hellwig	__be64		agfl_lsn;
6574fb6e8adSChristoph Hellwig	__be32		agfl_crc;
6584b975108SChristoph Hellwig} __attribute__((packed));
6594fb6e8adSChristoph Hellwig
6604fb6e8adSChristoph Hellwig#define XFS_AGFL_CRC_OFF	offsetof(struct xfs_agfl, agfl_crc)
6614fb6e8adSChristoph Hellwig
6624fb6e8adSChristoph Hellwig#define XFS_AGB_TO_FSB(mp,agno,agbno)	\
6634fb6e8adSChristoph Hellwig	(((xfs_fsblock_t)(agno) << (mp)->m_sb.sb_agblklog) | (agbno))
6644fb6e8adSChristoph Hellwig#define	XFS_FSB_TO_AGNO(mp,fsbno)	\
6654fb6e8adSChristoph Hellwig	((xfs_agnumber_t)((fsbno) >> (mp)->m_sb.sb_agblklog))
6664fb6e8adSChristoph Hellwig#define	XFS_FSB_TO_AGBNO(mp,fsbno)	\
6674fb6e8adSChristoph Hellwig	((xfs_agblock_t)((fsbno) & xfs_mask32lo((mp)->m_sb.sb_agblklog)))
6684fb6e8adSChristoph Hellwig#define	XFS_AGB_TO_DADDR(mp,agno,agbno)	\
6694fb6e8adSChristoph Hellwig	((xfs_daddr_t)XFS_FSB_TO_BB(mp, \
6704fb6e8adSChristoph Hellwig		(xfs_fsblock_t)(agno) * (mp)->m_sb.sb_agblocks + (agbno)))
6714fb6e8adSChristoph Hellwig#define	XFS_AG_DADDR(mp,agno,d)		(XFS_AGB_TO_DADDR(mp, agno, 0) + (d))
6724fb6e8adSChristoph Hellwig
6734fb6e8adSChristoph Hellwig/*
6744fb6e8adSChristoph Hellwig * For checking for bad ranges of xfs_daddr_t's, covering multiple
6754fb6e8adSChristoph Hellwig * allocation groups or a single xfs_daddr_t that's a superblock copy.
6764fb6e8adSChristoph Hellwig */
6774fb6e8adSChristoph Hellwig#define	XFS_AG_CHECK_DADDR(mp,d,len)	\
6784fb6e8adSChristoph Hellwig	((len) == 1 ? \
6794fb6e8adSChristoph Hellwig	    ASSERT((d) == XFS_SB_DADDR || \
6804fb6e8adSChristoph Hellwig		   xfs_daddr_to_agbno(mp, d) != XFS_SB_DADDR) : \
6814fb6e8adSChristoph Hellwig	    ASSERT(xfs_daddr_to_agno(mp, d) == \
6824fb6e8adSChristoph Hellwig		   xfs_daddr_to_agno(mp, (d) + (len) - 1)))
6834fb6e8adSChristoph Hellwig
684876fdc7cSDarrick J. Wong/*
685876fdc7cSDarrick J. Wong * XFS Timestamps
686876fdc7cSDarrick J. Wong * ==============
687876fdc7cSDarrick J. Wong *
688876fdc7cSDarrick J. Wong * Traditional ondisk inode timestamps consist of signed 32-bit counters for
689876fdc7cSDarrick J. Wong * seconds and nanoseconds; time zero is the Unix epoch, Jan  1 00:00:00 UTC
690876fdc7cSDarrick J. Wong * 1970, which means that the timestamp epoch is the same as the Unix epoch.
691876fdc7cSDarrick J. Wong * Therefore, the ondisk min and max defined here can be used directly to
6925a0bb066SDarrick J. Wong * constrain the incore timestamps on a Unix system.  Note that we actually
6935a0bb066SDarrick J. Wong * encode a __be64 value on disk.
694f93e5436SDarrick J. Wong *
695f93e5436SDarrick J. Wong * When the bigtime feature is enabled, ondisk inode timestamps become an
696f93e5436SDarrick J. Wong * unsigned 64-bit nanoseconds counter.  This means that the bigtime inode
697f93e5436SDarrick J. Wong * timestamp epoch is the start of the classic timestamp range, which is
698f93e5436SDarrick J. Wong * Dec 31 20:45:52 UTC 1901.  Because the epochs are not the same, callers