xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_format.h (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 #ifndef __XFS_FORMAT_H__
7*4882a593Smuzhiyun #define __XFS_FORMAT_H__
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  * XFS On Disk Format Definitions
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * This header file defines all the on-disk format definitions for
13*4882a593Smuzhiyun  * general XFS objects. Directory and attribute related objects are defined in
14*4882a593Smuzhiyun  * xfs_da_format.h, which log and log item formats are defined in
15*4882a593Smuzhiyun  * xfs_log_format.h. Everything else goes here.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct xfs_mount;
19*4882a593Smuzhiyun struct xfs_trans;
20*4882a593Smuzhiyun struct xfs_inode;
21*4882a593Smuzhiyun struct xfs_buf;
22*4882a593Smuzhiyun struct xfs_ifork;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Super block
26*4882a593Smuzhiyun  * Fits into a sector-sized buffer at address 0 of each allocation group.
27*4882a593Smuzhiyun  * Only the first of these is ever updated except during growfs.
28*4882a593Smuzhiyun  */
29*4882a593Smuzhiyun #define	XFS_SB_MAGIC		0x58465342	/* 'XFSB' */
30*4882a593Smuzhiyun #define	XFS_SB_VERSION_1	1		/* 5.3, 6.0.1, 6.1 */
31*4882a593Smuzhiyun #define	XFS_SB_VERSION_2	2		/* 6.2 - attributes */
32*4882a593Smuzhiyun #define	XFS_SB_VERSION_3	3		/* 6.2 - new inode version */
33*4882a593Smuzhiyun #define	XFS_SB_VERSION_4	4		/* 6.2+ - bitmask version */
34*4882a593Smuzhiyun #define	XFS_SB_VERSION_5	5		/* CRC enabled filesystem */
35*4882a593Smuzhiyun #define	XFS_SB_VERSION_NUMBITS		0x000f
36*4882a593Smuzhiyun #define	XFS_SB_VERSION_ALLFBITS		0xfff0
37*4882a593Smuzhiyun #define	XFS_SB_VERSION_ATTRBIT		0x0010
38*4882a593Smuzhiyun #define	XFS_SB_VERSION_NLINKBIT		0x0020
39*4882a593Smuzhiyun #define	XFS_SB_VERSION_QUOTABIT		0x0040
40*4882a593Smuzhiyun #define	XFS_SB_VERSION_ALIGNBIT		0x0080
41*4882a593Smuzhiyun #define	XFS_SB_VERSION_DALIGNBIT	0x0100
42*4882a593Smuzhiyun #define	XFS_SB_VERSION_SHAREDBIT	0x0200
43*4882a593Smuzhiyun #define XFS_SB_VERSION_LOGV2BIT		0x0400
44*4882a593Smuzhiyun #define XFS_SB_VERSION_SECTORBIT	0x0800
45*4882a593Smuzhiyun #define	XFS_SB_VERSION_EXTFLGBIT	0x1000
46*4882a593Smuzhiyun #define	XFS_SB_VERSION_DIRV2BIT		0x2000
47*4882a593Smuzhiyun #define	XFS_SB_VERSION_BORGBIT		0x4000	/* ASCII only case-insens. */
48*4882a593Smuzhiyun #define	XFS_SB_VERSION_MOREBITSBIT	0x8000
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun  * The size of a single extended attribute on disk is limited by
52*4882a593Smuzhiyun  * the size of index values within the attribute entries themselves.
53*4882a593Smuzhiyun  * These are be16 fields, so we can only support attribute data
54*4882a593Smuzhiyun  * sizes up to 2^16 bytes in length.
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun #define XFS_XATTR_SIZE_MAX (1 << 16)
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun  * Supported feature bit list is just all bits in the versionnum field because
60*4882a593Smuzhiyun  * we've used them all up and understand them all. Except, of course, for the
61*4882a593Smuzhiyun  * shared superblock bit, which nobody knows what it does and so is unsupported.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun #define	XFS_SB_VERSION_OKBITS		\
64*4882a593Smuzhiyun 	((XFS_SB_VERSION_NUMBITS | XFS_SB_VERSION_ALLFBITS) & \
65*4882a593Smuzhiyun 		~XFS_SB_VERSION_SHAREDBIT)
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun  * There are two words to hold XFS "feature" bits: the original
69*4882a593Smuzhiyun  * word, sb_versionnum, and sb_features2.  Whenever a bit is set in
70*4882a593Smuzhiyun  * sb_features2, the feature bit XFS_SB_VERSION_MOREBITSBIT must be set.
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * These defines represent bits in sb_features2.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun #define XFS_SB_VERSION2_RESERVED1BIT	0x00000001
75*4882a593Smuzhiyun #define XFS_SB_VERSION2_LAZYSBCOUNTBIT	0x00000002	/* Superblk counters */
76*4882a593Smuzhiyun #define XFS_SB_VERSION2_RESERVED4BIT	0x00000004
77*4882a593Smuzhiyun #define XFS_SB_VERSION2_ATTR2BIT	0x00000008	/* Inline attr rework */
78*4882a593Smuzhiyun #define XFS_SB_VERSION2_PARENTBIT	0x00000010	/* parent pointers */
79*4882a593Smuzhiyun #define XFS_SB_VERSION2_PROJID32BIT	0x00000080	/* 32 bit project id */
80*4882a593Smuzhiyun #define XFS_SB_VERSION2_CRCBIT		0x00000100	/* metadata CRCs */
81*4882a593Smuzhiyun #define XFS_SB_VERSION2_FTYPE		0x00000200	/* inode type in dir */
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define	XFS_SB_VERSION2_OKBITS		\
84*4882a593Smuzhiyun 	(XFS_SB_VERSION2_LAZYSBCOUNTBIT	| \
85*4882a593Smuzhiyun 	 XFS_SB_VERSION2_ATTR2BIT	| \
86*4882a593Smuzhiyun 	 XFS_SB_VERSION2_PROJID32BIT	| \
87*4882a593Smuzhiyun 	 XFS_SB_VERSION2_FTYPE)
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* Maximum size of the xfs filesystem label, no terminating NULL */
90*4882a593Smuzhiyun #define XFSLABEL_MAX			12
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun  * Superblock - in core version.  Must match the ondisk version below.
94*4882a593Smuzhiyun  * Must be padded to 64 bit alignment.
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun typedef struct xfs_sb {
97*4882a593Smuzhiyun 	uint32_t	sb_magicnum;	/* magic number == XFS_SB_MAGIC */
98*4882a593Smuzhiyun 	uint32_t	sb_blocksize;	/* logical block size, bytes */
99*4882a593Smuzhiyun 	xfs_rfsblock_t	sb_dblocks;	/* number of data blocks */
100*4882a593Smuzhiyun 	xfs_rfsblock_t	sb_rblocks;	/* number of realtime blocks */
101*4882a593Smuzhiyun 	xfs_rtblock_t	sb_rextents;	/* number of realtime extents */
102*4882a593Smuzhiyun 	uuid_t		sb_uuid;	/* user-visible file system unique id */
103*4882a593Smuzhiyun 	xfs_fsblock_t	sb_logstart;	/* starting block of log if internal */
104*4882a593Smuzhiyun 	xfs_ino_t	sb_rootino;	/* root inode number */
105*4882a593Smuzhiyun 	xfs_ino_t	sb_rbmino;	/* bitmap inode for realtime extents */
106*4882a593Smuzhiyun 	xfs_ino_t	sb_rsumino;	/* summary inode for rt bitmap */
107*4882a593Smuzhiyun 	xfs_agblock_t	sb_rextsize;	/* realtime extent size, blocks */
108*4882a593Smuzhiyun 	xfs_agblock_t	sb_agblocks;	/* size of an allocation group */
109*4882a593Smuzhiyun 	xfs_agnumber_t	sb_agcount;	/* number of allocation groups */
110*4882a593Smuzhiyun 	xfs_extlen_t	sb_rbmblocks;	/* number of rt bitmap blocks */
111*4882a593Smuzhiyun 	xfs_extlen_t	sb_logblocks;	/* number of log blocks */
112*4882a593Smuzhiyun 	uint16_t	sb_versionnum;	/* header version == XFS_SB_VERSION */
113*4882a593Smuzhiyun 	uint16_t	sb_sectsize;	/* volume sector size, bytes */
114*4882a593Smuzhiyun 	uint16_t	sb_inodesize;	/* inode size, bytes */
115*4882a593Smuzhiyun 	uint16_t	sb_inopblock;	/* inodes per block */
116*4882a593Smuzhiyun 	char		sb_fname[XFSLABEL_MAX]; /* file system name */
117*4882a593Smuzhiyun 	uint8_t		sb_blocklog;	/* log2 of sb_blocksize */
118*4882a593Smuzhiyun 	uint8_t		sb_sectlog;	/* log2 of sb_sectsize */
119*4882a593Smuzhiyun 	uint8_t		sb_inodelog;	/* log2 of sb_inodesize */
120*4882a593Smuzhiyun 	uint8_t		sb_inopblog;	/* log2 of sb_inopblock */
121*4882a593Smuzhiyun 	uint8_t		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
122*4882a593Smuzhiyun 	uint8_t		sb_rextslog;	/* log2 of sb_rextents */
123*4882a593Smuzhiyun 	uint8_t		sb_inprogress;	/* mkfs is in progress, don't mount */
124*4882a593Smuzhiyun 	uint8_t		sb_imax_pct;	/* max % of fs for inode space */
125*4882a593Smuzhiyun 					/* statistics */
126*4882a593Smuzhiyun 	/*
127*4882a593Smuzhiyun 	 * These fields must remain contiguous.  If you really
128*4882a593Smuzhiyun 	 * want to change their layout, make sure you fix the
129*4882a593Smuzhiyun 	 * code in xfs_trans_apply_sb_deltas().
130*4882a593Smuzhiyun 	 */
131*4882a593Smuzhiyun 	uint64_t	sb_icount;	/* allocated inodes */
132*4882a593Smuzhiyun 	uint64_t	sb_ifree;	/* free inodes */
133*4882a593Smuzhiyun 	uint64_t	sb_fdblocks;	/* free data blocks */
134*4882a593Smuzhiyun 	uint64_t	sb_frextents;	/* free realtime extents */
135*4882a593Smuzhiyun 	/*
136*4882a593Smuzhiyun 	 * End contiguous fields.
137*4882a593Smuzhiyun 	 */
138*4882a593Smuzhiyun 	xfs_ino_t	sb_uquotino;	/* user quota inode */
139*4882a593Smuzhiyun 	xfs_ino_t	sb_gquotino;	/* group quota inode */
140*4882a593Smuzhiyun 	uint16_t	sb_qflags;	/* quota flags */
141*4882a593Smuzhiyun 	uint8_t		sb_flags;	/* misc. flags */
142*4882a593Smuzhiyun 	uint8_t		sb_shared_vn;	/* shared version number */
143*4882a593Smuzhiyun 	xfs_extlen_t	sb_inoalignmt;	/* inode chunk alignment, fsblocks */
144*4882a593Smuzhiyun 	uint32_t	sb_unit;	/* stripe or raid unit */
145*4882a593Smuzhiyun 	uint32_t	sb_width;	/* stripe or raid width */
146*4882a593Smuzhiyun 	uint8_t		sb_dirblklog;	/* log2 of dir block size (fsbs) */
147*4882a593Smuzhiyun 	uint8_t		sb_logsectlog;	/* log2 of the log sector size */
148*4882a593Smuzhiyun 	uint16_t	sb_logsectsize;	/* sector size for the log, bytes */
149*4882a593Smuzhiyun 	uint32_t	sb_logsunit;	/* stripe unit size for the log */
150*4882a593Smuzhiyun 	uint32_t	sb_features2;	/* additional feature bits */
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	/*
153*4882a593Smuzhiyun 	 * bad features2 field as a result of failing to pad the sb structure to
154*4882a593Smuzhiyun 	 * 64 bits. Some machines will be using this field for features2 bits.
155*4882a593Smuzhiyun 	 * Easiest just to mark it bad and not use it for anything else.
156*4882a593Smuzhiyun 	 *
157*4882a593Smuzhiyun 	 * This is not kept up to date in memory; it is always overwritten by
158*4882a593Smuzhiyun 	 * the value in sb_features2 when formatting the incore superblock to
159*4882a593Smuzhiyun 	 * the disk buffer.
160*4882a593Smuzhiyun 	 */
161*4882a593Smuzhiyun 	uint32_t	sb_bad_features2;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* version 5 superblock fields start here */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* feature masks */
166*4882a593Smuzhiyun 	uint32_t	sb_features_compat;
167*4882a593Smuzhiyun 	uint32_t	sb_features_ro_compat;
168*4882a593Smuzhiyun 	uint32_t	sb_features_incompat;
169*4882a593Smuzhiyun 	uint32_t	sb_features_log_incompat;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	uint32_t	sb_crc;		/* superblock crc */
172*4882a593Smuzhiyun 	xfs_extlen_t	sb_spino_align;	/* sparse inode chunk alignment */
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	xfs_ino_t	sb_pquotino;	/* project quota inode */
175*4882a593Smuzhiyun 	xfs_lsn_t	sb_lsn;		/* last write sequence */
176*4882a593Smuzhiyun 	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	/* must be padded to 64 bit alignment */
179*4882a593Smuzhiyun } xfs_sb_t;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun #define XFS_SB_CRC_OFF		offsetof(struct xfs_sb, sb_crc)
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun  * Superblock - on disk version.  Must match the in core version above.
185*4882a593Smuzhiyun  * Must be padded to 64 bit alignment.
186*4882a593Smuzhiyun  */
187*4882a593Smuzhiyun typedef struct xfs_dsb {
188*4882a593Smuzhiyun 	__be32		sb_magicnum;	/* magic number == XFS_SB_MAGIC */
189*4882a593Smuzhiyun 	__be32		sb_blocksize;	/* logical block size, bytes */
190*4882a593Smuzhiyun 	__be64		sb_dblocks;	/* number of data blocks */
191*4882a593Smuzhiyun 	__be64		sb_rblocks;	/* number of realtime blocks */
192*4882a593Smuzhiyun 	__be64		sb_rextents;	/* number of realtime extents */
193*4882a593Smuzhiyun 	uuid_t		sb_uuid;	/* user-visible file system unique id */
194*4882a593Smuzhiyun 	__be64		sb_logstart;	/* starting block of log if internal */
195*4882a593Smuzhiyun 	__be64		sb_rootino;	/* root inode number */
196*4882a593Smuzhiyun 	__be64		sb_rbmino;	/* bitmap inode for realtime extents */
197*4882a593Smuzhiyun 	__be64		sb_rsumino;	/* summary inode for rt bitmap */
198*4882a593Smuzhiyun 	__be32		sb_rextsize;	/* realtime extent size, blocks */
199*4882a593Smuzhiyun 	__be32		sb_agblocks;	/* size of an allocation group */
200*4882a593Smuzhiyun 	__be32		sb_agcount;	/* number of allocation groups */
201*4882a593Smuzhiyun 	__be32		sb_rbmblocks;	/* number of rt bitmap blocks */
202*4882a593Smuzhiyun 	__be32		sb_logblocks;	/* number of log blocks */
203*4882a593Smuzhiyun 	__be16		sb_versionnum;	/* header version == XFS_SB_VERSION */
204*4882a593Smuzhiyun 	__be16		sb_sectsize;	/* volume sector size, bytes */
205*4882a593Smuzhiyun 	__be16		sb_inodesize;	/* inode size, bytes */
206*4882a593Smuzhiyun 	__be16		sb_inopblock;	/* inodes per block */
207*4882a593Smuzhiyun 	char		sb_fname[XFSLABEL_MAX]; /* file system name */
208*4882a593Smuzhiyun 	__u8		sb_blocklog;	/* log2 of sb_blocksize */
209*4882a593Smuzhiyun 	__u8		sb_sectlog;	/* log2 of sb_sectsize */
210*4882a593Smuzhiyun 	__u8		sb_inodelog;	/* log2 of sb_inodesize */
211*4882a593Smuzhiyun 	__u8		sb_inopblog;	/* log2 of sb_inopblock */
212*4882a593Smuzhiyun 	__u8		sb_agblklog;	/* log2 of sb_agblocks (rounded up) */
213*4882a593Smuzhiyun 	__u8		sb_rextslog;	/* log2 of sb_rextents */
214*4882a593Smuzhiyun 	__u8		sb_inprogress;	/* mkfs is in progress, don't mount */
215*4882a593Smuzhiyun 	__u8		sb_imax_pct;	/* max % of fs for inode space */
216*4882a593Smuzhiyun 					/* statistics */
217*4882a593Smuzhiyun 	/*
218*4882a593Smuzhiyun 	 * These fields must remain contiguous.  If you really
219*4882a593Smuzhiyun 	 * want to change their layout, make sure you fix the
220*4882a593Smuzhiyun 	 * code in xfs_trans_apply_sb_deltas().
221*4882a593Smuzhiyun 	 */
222*4882a593Smuzhiyun 	__be64		sb_icount;	/* allocated inodes */
223*4882a593Smuzhiyun 	__be64		sb_ifree;	/* free inodes */
224*4882a593Smuzhiyun 	__be64		sb_fdblocks;	/* free data blocks */
225*4882a593Smuzhiyun 	__be64		sb_frextents;	/* free realtime extents */
226*4882a593Smuzhiyun 	/*
227*4882a593Smuzhiyun 	 * End contiguous fields.
228*4882a593Smuzhiyun 	 */
229*4882a593Smuzhiyun 	__be64		sb_uquotino;	/* user quota inode */
230*4882a593Smuzhiyun 	__be64		sb_gquotino;	/* group quota inode */
231*4882a593Smuzhiyun 	__be16		sb_qflags;	/* quota flags */
232*4882a593Smuzhiyun 	__u8		sb_flags;	/* misc. flags */
233*4882a593Smuzhiyun 	__u8		sb_shared_vn;	/* shared version number */
234*4882a593Smuzhiyun 	__be32		sb_inoalignmt;	/* inode chunk alignment, fsblocks */
235*4882a593Smuzhiyun 	__be32		sb_unit;	/* stripe or raid unit */
236*4882a593Smuzhiyun 	__be32		sb_width;	/* stripe or raid width */
237*4882a593Smuzhiyun 	__u8		sb_dirblklog;	/* log2 of dir block size (fsbs) */
238*4882a593Smuzhiyun 	__u8		sb_logsectlog;	/* log2 of the log sector size */
239*4882a593Smuzhiyun 	__be16		sb_logsectsize;	/* sector size for the log, bytes */
240*4882a593Smuzhiyun 	__be32		sb_logsunit;	/* stripe unit size for the log */
241*4882a593Smuzhiyun 	__be32		sb_features2;	/* additional feature bits */
242*4882a593Smuzhiyun 	/*
243*4882a593Smuzhiyun 	 * bad features2 field as a result of failing to pad the sb
244*4882a593Smuzhiyun 	 * structure to 64 bits. Some machines will be using this field
245*4882a593Smuzhiyun 	 * for features2 bits. Easiest just to mark it bad and not use
246*4882a593Smuzhiyun 	 * it for anything else.
247*4882a593Smuzhiyun 	 */
248*4882a593Smuzhiyun 	__be32		sb_bad_features2;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* version 5 superblock fields start here */
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* feature masks */
253*4882a593Smuzhiyun 	__be32		sb_features_compat;
254*4882a593Smuzhiyun 	__be32		sb_features_ro_compat;
255*4882a593Smuzhiyun 	__be32		sb_features_incompat;
256*4882a593Smuzhiyun 	__be32		sb_features_log_incompat;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	__le32		sb_crc;		/* superblock crc */
259*4882a593Smuzhiyun 	__be32		sb_spino_align;	/* sparse inode chunk alignment */
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	__be64		sb_pquotino;	/* project quota inode */
262*4882a593Smuzhiyun 	__be64		sb_lsn;		/* last write sequence */
263*4882a593Smuzhiyun 	uuid_t		sb_meta_uuid;	/* metadata file system unique id */
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	/* must be padded to 64 bit alignment */
266*4882a593Smuzhiyun } xfs_dsb_t;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun /*
270*4882a593Smuzhiyun  * Misc. Flags - warning - these will be cleared by xfs_repair unless
271*4882a593Smuzhiyun  * a feature bit is set when the flag is used.
272*4882a593Smuzhiyun  */
273*4882a593Smuzhiyun #define XFS_SBF_NOFLAGS		0x00	/* no flags set */
274*4882a593Smuzhiyun #define XFS_SBF_READONLY	0x01	/* only read-only mounts allowed */
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun  * define max. shared version we can interoperate with
278*4882a593Smuzhiyun  */
279*4882a593Smuzhiyun #define XFS_SB_MAX_SHARED_VN	0
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun #define	XFS_SB_VERSION_NUM(sbp)	((sbp)->sb_versionnum & XFS_SB_VERSION_NUMBITS)
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun /*
284*4882a593Smuzhiyun  * The first XFS version we support is a v4 superblock with V2 directories.
285*4882a593Smuzhiyun  */
xfs_sb_good_v4_features(struct xfs_sb * sbp)286*4882a593Smuzhiyun static inline bool xfs_sb_good_v4_features(struct xfs_sb *sbp)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
289*4882a593Smuzhiyun 		return false;
290*4882a593Smuzhiyun 	if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
291*4882a593Smuzhiyun 		return false;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* check for unknown features in the fs */
294*4882a593Smuzhiyun 	if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
295*4882a593Smuzhiyun 	    ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
296*4882a593Smuzhiyun 	     (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
297*4882a593Smuzhiyun 		return false;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	return true;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
xfs_sb_good_version(struct xfs_sb * sbp)302*4882a593Smuzhiyun static inline bool xfs_sb_good_version(struct xfs_sb *sbp)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	if (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5)
305*4882a593Smuzhiyun 		return true;
306*4882a593Smuzhiyun 	if (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_4)
307*4882a593Smuzhiyun 		return xfs_sb_good_v4_features(sbp);
308*4882a593Smuzhiyun 	return false;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
xfs_sb_version_hasrealtime(struct xfs_sb * sbp)311*4882a593Smuzhiyun static inline bool xfs_sb_version_hasrealtime(struct xfs_sb *sbp)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	return sbp->sb_rblocks > 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun /*
317*4882a593Smuzhiyun  * Detect a mismatched features2 field.  Older kernels read/wrote
318*4882a593Smuzhiyun  * this into the wrong slot, so to be safe we keep them in sync.
319*4882a593Smuzhiyun  */
xfs_sb_has_mismatched_features2(struct xfs_sb * sbp)320*4882a593Smuzhiyun static inline bool xfs_sb_has_mismatched_features2(struct xfs_sb *sbp)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	return sbp->sb_bad_features2 != sbp->sb_features2;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
xfs_sb_version_hasattr(struct xfs_sb * sbp)325*4882a593Smuzhiyun static inline bool xfs_sb_version_hasattr(struct xfs_sb *sbp)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	return (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun 
xfs_sb_version_addattr(struct xfs_sb * sbp)330*4882a593Smuzhiyun static inline void xfs_sb_version_addattr(struct xfs_sb *sbp)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun 	sbp->sb_versionnum |= XFS_SB_VERSION_ATTRBIT;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
xfs_sb_version_hasquota(struct xfs_sb * sbp)335*4882a593Smuzhiyun static inline bool xfs_sb_version_hasquota(struct xfs_sb *sbp)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	return (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
xfs_sb_version_addquota(struct xfs_sb * sbp)340*4882a593Smuzhiyun static inline void xfs_sb_version_addquota(struct xfs_sb *sbp)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	sbp->sb_versionnum |= XFS_SB_VERSION_QUOTABIT;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
xfs_sb_version_hasalign(struct xfs_sb * sbp)345*4882a593Smuzhiyun static inline bool xfs_sb_version_hasalign(struct xfs_sb *sbp)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 ||
348*4882a593Smuzhiyun 		(sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT));
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
xfs_sb_version_hasdalign(struct xfs_sb * sbp)351*4882a593Smuzhiyun static inline bool xfs_sb_version_hasdalign(struct xfs_sb *sbp)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	return (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT);
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
xfs_sb_version_haslogv2(struct xfs_sb * sbp)356*4882a593Smuzhiyun static inline bool xfs_sb_version_haslogv2(struct xfs_sb *sbp)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 ||
359*4882a593Smuzhiyun 	       (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
xfs_sb_version_hassector(struct xfs_sb * sbp)362*4882a593Smuzhiyun static inline bool xfs_sb_version_hassector(struct xfs_sb *sbp)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	return (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
xfs_sb_version_hasasciici(struct xfs_sb * sbp)367*4882a593Smuzhiyun static inline bool xfs_sb_version_hasasciici(struct xfs_sb *sbp)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	return (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
xfs_sb_version_hasmorebits(struct xfs_sb * sbp)372*4882a593Smuzhiyun static inline bool xfs_sb_version_hasmorebits(struct xfs_sb *sbp)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 ||
375*4882a593Smuzhiyun 	       (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /*
379*4882a593Smuzhiyun  * sb_features2 bit version macros.
380*4882a593Smuzhiyun  */
xfs_sb_version_haslazysbcount(struct xfs_sb * sbp)381*4882a593Smuzhiyun static inline bool xfs_sb_version_haslazysbcount(struct xfs_sb *sbp)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) ||
384*4882a593Smuzhiyun 	       (xfs_sb_version_hasmorebits(sbp) &&
385*4882a593Smuzhiyun 		(sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT));
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
xfs_sb_version_hasattr2(struct xfs_sb * sbp)388*4882a593Smuzhiyun static inline bool xfs_sb_version_hasattr2(struct xfs_sb *sbp)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) ||
391*4882a593Smuzhiyun 	       (xfs_sb_version_hasmorebits(sbp) &&
392*4882a593Smuzhiyun 		(sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT));
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
xfs_sb_version_addattr2(struct xfs_sb * sbp)395*4882a593Smuzhiyun static inline void xfs_sb_version_addattr2(struct xfs_sb *sbp)
396*4882a593Smuzhiyun {
397*4882a593Smuzhiyun 	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
398*4882a593Smuzhiyun 	sbp->sb_features2 |= XFS_SB_VERSION2_ATTR2BIT;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
xfs_sb_version_removeattr2(struct xfs_sb * sbp)401*4882a593Smuzhiyun static inline void xfs_sb_version_removeattr2(struct xfs_sb *sbp)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	sbp->sb_features2 &= ~XFS_SB_VERSION2_ATTR2BIT;
404*4882a593Smuzhiyun 	if (!sbp->sb_features2)
405*4882a593Smuzhiyun 		sbp->sb_versionnum &= ~XFS_SB_VERSION_MOREBITSBIT;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
xfs_sb_version_hasprojid32bit(struct xfs_sb * sbp)408*4882a593Smuzhiyun static inline bool xfs_sb_version_hasprojid32bit(struct xfs_sb *sbp)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) ||
411*4882a593Smuzhiyun 	       (xfs_sb_version_hasmorebits(sbp) &&
412*4882a593Smuzhiyun 		(sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT));
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
xfs_sb_version_addprojid32bit(struct xfs_sb * sbp)415*4882a593Smuzhiyun static inline void xfs_sb_version_addprojid32bit(struct xfs_sb *sbp)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	sbp->sb_versionnum |= XFS_SB_VERSION_MOREBITSBIT;
418*4882a593Smuzhiyun 	sbp->sb_features2 |= XFS_SB_VERSION2_PROJID32BIT;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun /*
422*4882a593Smuzhiyun  * Extended v5 superblock feature masks. These are to be used for new v5
423*4882a593Smuzhiyun  * superblock features only.
424*4882a593Smuzhiyun  *
425*4882a593Smuzhiyun  * Compat features are new features that old kernels will not notice or affect
426*4882a593Smuzhiyun  * and so can mount read-write without issues.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * RO-Compat (read only) are features that old kernels can read but will break
429*4882a593Smuzhiyun  * if they write. Hence only read-only mounts of such filesystems are allowed on
430*4882a593Smuzhiyun  * kernels that don't support the feature bit.
431*4882a593Smuzhiyun  *
432*4882a593Smuzhiyun  * InCompat features are features which old kernels will not understand and so
433*4882a593Smuzhiyun  * must not mount.
434*4882a593Smuzhiyun  *
435*4882a593Smuzhiyun  * Log-InCompat features are for changes to log formats or new transactions that
436*4882a593Smuzhiyun  * can't be replayed on older kernels. The fields are set when the filesystem is
437*4882a593Smuzhiyun  * mounted, and a clean unmount clears the fields.
438*4882a593Smuzhiyun  */
439*4882a593Smuzhiyun #define XFS_SB_FEAT_COMPAT_ALL 0
440*4882a593Smuzhiyun #define XFS_SB_FEAT_COMPAT_UNKNOWN	~XFS_SB_FEAT_COMPAT_ALL
441*4882a593Smuzhiyun static inline bool
xfs_sb_has_compat_feature(struct xfs_sb * sbp,uint32_t feature)442*4882a593Smuzhiyun xfs_sb_has_compat_feature(
443*4882a593Smuzhiyun 	struct xfs_sb	*sbp,
444*4882a593Smuzhiyun 	uint32_t	feature)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	return (sbp->sb_features_compat & feature) != 0;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_FINOBT   (1 << 0)		/* free inode btree */
450*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_RMAPBT   (1 << 1)		/* reverse map btree */
451*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_REFLINK  (1 << 2)		/* reflinked files */
452*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)		/* inobt block counts */
453*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_ALL \
454*4882a593Smuzhiyun 		(XFS_SB_FEAT_RO_COMPAT_FINOBT | \
455*4882a593Smuzhiyun 		 XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
456*4882a593Smuzhiyun 		 XFS_SB_FEAT_RO_COMPAT_REFLINK| \
457*4882a593Smuzhiyun 		 XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
458*4882a593Smuzhiyun #define XFS_SB_FEAT_RO_COMPAT_UNKNOWN	~XFS_SB_FEAT_RO_COMPAT_ALL
459*4882a593Smuzhiyun static inline bool
xfs_sb_has_ro_compat_feature(struct xfs_sb * sbp,uint32_t feature)460*4882a593Smuzhiyun xfs_sb_has_ro_compat_feature(
461*4882a593Smuzhiyun 	struct xfs_sb	*sbp,
462*4882a593Smuzhiyun 	uint32_t	feature)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	return (sbp->sb_features_ro_compat & feature) != 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_FTYPE	(1 << 0)	/* filetype in dirent */
468*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_SPINODES	(1 << 1)	/* sparse inode chunks */
469*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_META_UUID	(1 << 2)	/* metadata UUID */
470*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_BIGTIME	(1 << 3)	/* large timestamps */
471*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_ALL \
472*4882a593Smuzhiyun 		(XFS_SB_FEAT_INCOMPAT_FTYPE|	\
473*4882a593Smuzhiyun 		 XFS_SB_FEAT_INCOMPAT_SPINODES|	\
474*4882a593Smuzhiyun 		 XFS_SB_FEAT_INCOMPAT_META_UUID| \
475*4882a593Smuzhiyun 		 XFS_SB_FEAT_INCOMPAT_BIGTIME)
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_UNKNOWN	~XFS_SB_FEAT_INCOMPAT_ALL
478*4882a593Smuzhiyun static inline bool
xfs_sb_has_incompat_feature(struct xfs_sb * sbp,uint32_t feature)479*4882a593Smuzhiyun xfs_sb_has_incompat_feature(
480*4882a593Smuzhiyun 	struct xfs_sb	*sbp,
481*4882a593Smuzhiyun 	uint32_t	feature)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	return (sbp->sb_features_incompat & feature) != 0;
484*4882a593Smuzhiyun }
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_LOG_ALL 0
487*4882a593Smuzhiyun #define XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN	~XFS_SB_FEAT_INCOMPAT_LOG_ALL
488*4882a593Smuzhiyun static inline bool
xfs_sb_has_incompat_log_feature(struct xfs_sb * sbp,uint32_t feature)489*4882a593Smuzhiyun xfs_sb_has_incompat_log_feature(
490*4882a593Smuzhiyun 	struct xfs_sb	*sbp,
491*4882a593Smuzhiyun 	uint32_t	feature)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	return (sbp->sb_features_log_incompat & feature) != 0;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun /*
497*4882a593Smuzhiyun  * V5 superblock specific feature checks
498*4882a593Smuzhiyun  */
xfs_sb_version_hascrc(struct xfs_sb * sbp)499*4882a593Smuzhiyun static inline bool xfs_sb_version_hascrc(struct xfs_sb *sbp)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun /*
505*4882a593Smuzhiyun  * v5 file systems support V3 inodes only, earlier file systems support
506*4882a593Smuzhiyun  * v2 and v1 inodes.
507*4882a593Smuzhiyun  */
xfs_sb_version_has_v3inode(struct xfs_sb * sbp)508*4882a593Smuzhiyun static inline bool xfs_sb_version_has_v3inode(struct xfs_sb *sbp)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
xfs_dinode_good_version(struct xfs_sb * sbp,uint8_t version)513*4882a593Smuzhiyun static inline bool xfs_dinode_good_version(struct xfs_sb *sbp,
514*4882a593Smuzhiyun 		uint8_t version)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	if (xfs_sb_version_has_v3inode(sbp))
517*4882a593Smuzhiyun 		return version == 3;
518*4882a593Smuzhiyun 	return version == 1 || version == 2;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
xfs_sb_version_has_pquotino(struct xfs_sb * sbp)521*4882a593Smuzhiyun static inline bool xfs_sb_version_has_pquotino(struct xfs_sb *sbp)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
xfs_sb_version_hasftype(struct xfs_sb * sbp)526*4882a593Smuzhiyun static inline int xfs_sb_version_hasftype(struct xfs_sb *sbp)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
529*4882a593Smuzhiyun 		xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_FTYPE)) ||
530*4882a593Smuzhiyun 	       (xfs_sb_version_hasmorebits(sbp) &&
531*4882a593Smuzhiyun 		 (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE));
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun 
xfs_sb_version_hasfinobt(xfs_sb_t * sbp)534*4882a593Smuzhiyun static inline bool xfs_sb_version_hasfinobt(xfs_sb_t *sbp)
535*4882a593Smuzhiyun {
536*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) &&
537*4882a593Smuzhiyun 		(sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT);
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun 
xfs_sb_version_hassparseinodes(struct xfs_sb * sbp)540*4882a593Smuzhiyun static inline bool xfs_sb_version_hassparseinodes(struct xfs_sb *sbp)
541*4882a593Smuzhiyun {
542*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
543*4882a593Smuzhiyun 		xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_SPINODES);
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun /*
547*4882a593Smuzhiyun  * XFS_SB_FEAT_INCOMPAT_META_UUID indicates that the metadata UUID
548*4882a593Smuzhiyun  * is stored separately from the user-visible UUID; this allows the
549*4882a593Smuzhiyun  * user-visible UUID to be changed on V5 filesystems which have a
550*4882a593Smuzhiyun  * filesystem UUID stamped into every piece of metadata.
551*4882a593Smuzhiyun  */
xfs_sb_version_hasmetauuid(struct xfs_sb * sbp)552*4882a593Smuzhiyun static inline bool xfs_sb_version_hasmetauuid(struct xfs_sb *sbp)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) &&
555*4882a593Smuzhiyun 		(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
xfs_sb_version_hasrmapbt(struct xfs_sb * sbp)558*4882a593Smuzhiyun static inline bool xfs_sb_version_hasrmapbt(struct xfs_sb *sbp)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	return (XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5) &&
561*4882a593Smuzhiyun 		(sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT);
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
xfs_sb_version_hasreflink(struct xfs_sb * sbp)564*4882a593Smuzhiyun static inline bool xfs_sb_version_hasreflink(struct xfs_sb *sbp)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
567*4882a593Smuzhiyun 		(sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun 
xfs_sb_version_hasbigtime(struct xfs_sb * sbp)570*4882a593Smuzhiyun static inline bool xfs_sb_version_hasbigtime(struct xfs_sb *sbp)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
573*4882a593Smuzhiyun 		(sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun /*
577*4882a593Smuzhiyun  * Inode btree block counter.  We record the number of inobt and finobt blocks
578*4882a593Smuzhiyun  * in the AGI header so that we can skip the finobt walk at mount time when
579*4882a593Smuzhiyun  * setting up per-AG reservations.
580*4882a593Smuzhiyun  */
xfs_sb_version_hasinobtcounts(struct xfs_sb * sbp)581*4882a593Smuzhiyun static inline bool xfs_sb_version_hasinobtcounts(struct xfs_sb *sbp)
582*4882a593Smuzhiyun {
583*4882a593Smuzhiyun 	return XFS_SB_VERSION_NUM(sbp) == XFS_SB_VERSION_5 &&
584*4882a593Smuzhiyun 		(sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun  * end of superblock version macros
589*4882a593Smuzhiyun  */
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun static inline bool
xfs_is_quota_inode(struct xfs_sb * sbp,xfs_ino_t ino)592*4882a593Smuzhiyun xfs_is_quota_inode(struct xfs_sb *sbp, xfs_ino_t ino)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	return (ino == sbp->sb_uquotino ||
595*4882a593Smuzhiyun 		ino == sbp->sb_gquotino ||
596*4882a593Smuzhiyun 		ino == sbp->sb_pquotino);
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun #define XFS_SB_DADDR		((xfs_daddr_t)0) /* daddr in filesystem/ag */
600*4882a593Smuzhiyun #define	XFS_SB_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_SB_DADDR)
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun #define	XFS_HDR_BLOCK(mp,d)	((xfs_agblock_t)XFS_BB_TO_FSBT(mp,d))
603*4882a593Smuzhiyun #define	XFS_DADDR_TO_FSB(mp,d)	XFS_AGB_TO_FSB(mp, \
604*4882a593Smuzhiyun 			xfs_daddr_to_agno(mp,d), xfs_daddr_to_agbno(mp,d))
605*4882a593Smuzhiyun #define	XFS_FSB_TO_DADDR(mp,fsbno)	XFS_AGB_TO_DADDR(mp, \
606*4882a593Smuzhiyun 			XFS_FSB_TO_AGNO(mp,fsbno), XFS_FSB_TO_AGBNO(mp,fsbno))
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun /*
609*4882a593Smuzhiyun  * File system sector to basic block conversions.
610*4882a593Smuzhiyun  */
611*4882a593Smuzhiyun #define XFS_FSS_TO_BB(mp,sec)	((sec) << (mp)->m_sectbb_log)
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /*
614*4882a593Smuzhiyun  * File system block to basic block conversions.
615*4882a593Smuzhiyun  */
616*4882a593Smuzhiyun #define	XFS_FSB_TO_BB(mp,fsbno)	((fsbno) << (mp)->m_blkbb_log)
617*4882a593Smuzhiyun #define	XFS_BB_TO_FSB(mp,bb)	\
618*4882a593Smuzhiyun 	(((bb) + (XFS_FSB_TO_BB(mp,1) - 1)) >> (mp)->m_blkbb_log)
619*4882a593Smuzhiyun #define	XFS_BB_TO_FSBT(mp,bb)	((bb) >> (mp)->m_blkbb_log)
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun  * File system block to byte conversions.
623*4882a593Smuzhiyun  */
624*4882a593Smuzhiyun #define XFS_FSB_TO_B(mp,fsbno)	((xfs_fsize_t)(fsbno) << (mp)->m_sb.sb_blocklog)
625*4882a593Smuzhiyun #define XFS_B_TO_FSB(mp,b)	\
626*4882a593Smuzhiyun 	((((uint64_t)(b)) + (mp)->m_blockmask) >> (mp)->m_sb.sb_blocklog)
627*4882a593Smuzhiyun #define XFS_B_TO_FSBT(mp,b)	(((uint64_t)(b)) >> (mp)->m_sb.sb_blocklog)
628*4882a593Smuzhiyun #define XFS_B_FSB_OFFSET(mp,b)	((b) & (mp)->m_blockmask)
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun /*
631*4882a593Smuzhiyun  * Allocation group header
632*4882a593Smuzhiyun  *
633*4882a593Smuzhiyun  * This is divided into three structures, placed in sequential 512-byte
634*4882a593Smuzhiyun  * buffers after a copy of the superblock (also in a 512-byte buffer).
635*4882a593Smuzhiyun  */
636*4882a593Smuzhiyun #define	XFS_AGF_MAGIC	0x58414746	/* 'XAGF' */
637*4882a593Smuzhiyun #define	XFS_AGI_MAGIC	0x58414749	/* 'XAGI' */
638*4882a593Smuzhiyun #define	XFS_AGFL_MAGIC	0x5841464c	/* 'XAFL' */
639*4882a593Smuzhiyun #define	XFS_AGF_VERSION	1
640*4882a593Smuzhiyun #define	XFS_AGI_VERSION	1
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun #define	XFS_AGF_GOOD_VERSION(v)	((v) == XFS_AGF_VERSION)
643*4882a593Smuzhiyun #define	XFS_AGI_GOOD_VERSION(v)	((v) == XFS_AGI_VERSION)
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun /*
646*4882a593Smuzhiyun  * Btree number 0 is bno, 1 is cnt, 2 is rmap. This value gives the size of the
647*4882a593Smuzhiyun  * arrays below.
648*4882a593Smuzhiyun  */
649*4882a593Smuzhiyun #define	XFS_BTNUM_AGF	((int)XFS_BTNUM_RMAPi + 1)
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun /*
652*4882a593Smuzhiyun  * The second word of agf_levels in the first a.g. overlaps the EFS
653*4882a593Smuzhiyun  * superblock's magic number.  Since the magic numbers valid for EFS
654*4882a593Smuzhiyun  * are > 64k, our value cannot be confused for an EFS superblock's.
655*4882a593Smuzhiyun  */
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun typedef struct xfs_agf {
658*4882a593Smuzhiyun 	/*
659*4882a593Smuzhiyun 	 * Common allocation group header information
660*4882a593Smuzhiyun 	 */
661*4882a593Smuzhiyun 	__be32		agf_magicnum;	/* magic number == XFS_AGF_MAGIC */
662*4882a593Smuzhiyun 	__be32		agf_versionnum;	/* header version == XFS_AGF_VERSION */
663*4882a593Smuzhiyun 	__be32		agf_seqno;	/* sequence # starting from 0 */
664*4882a593Smuzhiyun 	__be32		agf_length;	/* size in blocks of a.g. */
665*4882a593Smuzhiyun 	/*
666*4882a593Smuzhiyun 	 * Freespace and rmap information
667*4882a593Smuzhiyun 	 */
668*4882a593Smuzhiyun 	__be32		agf_roots[XFS_BTNUM_AGF];	/* root blocks */
669*4882a593Smuzhiyun 	__be32		agf_levels[XFS_BTNUM_AGF];	/* btree levels */
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	__be32		agf_flfirst;	/* first freelist block's index */
672*4882a593Smuzhiyun 	__be32		agf_fllast;	/* last freelist block's index */
673*4882a593Smuzhiyun 	__be32		agf_flcount;	/* count of blocks in freelist */
674*4882a593Smuzhiyun 	__be32		agf_freeblks;	/* total free blocks */
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	__be32		agf_longest;	/* longest free space */
677*4882a593Smuzhiyun 	__be32		agf_btreeblks;	/* # of blocks held in AGF btrees */
678*4882a593Smuzhiyun 	uuid_t		agf_uuid;	/* uuid of filesystem */
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	__be32		agf_rmap_blocks;	/* rmapbt blocks used */
681*4882a593Smuzhiyun 	__be32		agf_refcount_blocks;	/* refcountbt blocks used */
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	__be32		agf_refcount_root;	/* refcount tree root block */
684*4882a593Smuzhiyun 	__be32		agf_refcount_level;	/* refcount btree levels */
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/*
687*4882a593Smuzhiyun 	 * reserve some contiguous space for future logged fields before we add
688*4882a593Smuzhiyun 	 * the unlogged fields. This makes the range logging via flags and
689*4882a593Smuzhiyun 	 * structure offsets much simpler.
690*4882a593Smuzhiyun 	 */
691*4882a593Smuzhiyun 	__be64		agf_spare64[14];
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	/* unlogged fields, written during buffer writeback. */
694*4882a593Smuzhiyun 	__be64		agf_lsn;	/* last write sequence */
695*4882a593Smuzhiyun 	__be32		agf_crc;	/* crc of agf sector */
696*4882a593Smuzhiyun 	__be32		agf_spare2;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	/* structure must be padded to 64 bit alignment */
699*4882a593Smuzhiyun } xfs_agf_t;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun #define XFS_AGF_CRC_OFF		offsetof(struct xfs_agf, agf_crc)
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun #define	XFS_AGF_MAGICNUM	0x00000001
704*4882a593Smuzhiyun #define	XFS_AGF_VERSIONNUM	0x00000002
705*4882a593Smuzhiyun #define	XFS_AGF_SEQNO		0x00000004
706*4882a593Smuzhiyun #define	XFS_AGF_LENGTH		0x00000008
707*4882a593Smuzhiyun #define	XFS_AGF_ROOTS		0x00000010
708*4882a593Smuzhiyun #define	XFS_AGF_LEVELS		0x00000020
709*4882a593Smuzhiyun #define	XFS_AGF_FLFIRST		0x00000040
710*4882a593Smuzhiyun #define	XFS_AGF_FLLAST		0x00000080
711*4882a593Smuzhiyun #define	XFS_AGF_FLCOUNT		0x00000100
712*4882a593Smuzhiyun #define	XFS_AGF_FREEBLKS	0x00000200
713*4882a593Smuzhiyun #define	XFS_AGF_LONGEST		0x00000400
714*4882a593Smuzhiyun #define	XFS_AGF_BTREEBLKS	0x00000800
715*4882a593Smuzhiyun #define	XFS_AGF_UUID		0x00001000
716*4882a593Smuzhiyun #define	XFS_AGF_RMAP_BLOCKS	0x00002000
717*4882a593Smuzhiyun #define	XFS_AGF_REFCOUNT_BLOCKS	0x00004000
718*4882a593Smuzhiyun #define	XFS_AGF_REFCOUNT_ROOT	0x00008000
719*4882a593Smuzhiyun #define	XFS_AGF_REFCOUNT_LEVEL	0x00010000
720*4882a593Smuzhiyun #define	XFS_AGF_SPARE64		0x00020000
721*4882a593Smuzhiyun #define	XFS_AGF_NUM_BITS	18
722*4882a593Smuzhiyun #define	XFS_AGF_ALL_BITS	((1 << XFS_AGF_NUM_BITS) - 1)
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun #define XFS_AGF_FLAGS \
725*4882a593Smuzhiyun 	{ XFS_AGF_MAGICNUM,	"MAGICNUM" }, \
726*4882a593Smuzhiyun 	{ XFS_AGF_VERSIONNUM,	"VERSIONNUM" }, \
727*4882a593Smuzhiyun 	{ XFS_AGF_SEQNO,	"SEQNO" }, \
728*4882a593Smuzhiyun 	{ XFS_AGF_LENGTH,	"LENGTH" }, \
729*4882a593Smuzhiyun 	{ XFS_AGF_ROOTS,	"ROOTS" }, \
730*4882a593Smuzhiyun 	{ XFS_AGF_LEVELS,	"LEVELS" }, \
731*4882a593Smuzhiyun 	{ XFS_AGF_FLFIRST,	"FLFIRST" }, \
732*4882a593Smuzhiyun 	{ XFS_AGF_FLLAST,	"FLLAST" }, \
733*4882a593Smuzhiyun 	{ XFS_AGF_FLCOUNT,	"FLCOUNT" }, \
734*4882a593Smuzhiyun 	{ XFS_AGF_FREEBLKS,	"FREEBLKS" }, \
735*4882a593Smuzhiyun 	{ XFS_AGF_LONGEST,	"LONGEST" }, \
736*4882a593Smuzhiyun 	{ XFS_AGF_BTREEBLKS,	"BTREEBLKS" }, \
737*4882a593Smuzhiyun 	{ XFS_AGF_UUID,		"UUID" }, \
738*4882a593Smuzhiyun 	{ XFS_AGF_RMAP_BLOCKS,	"RMAP_BLOCKS" }, \
739*4882a593Smuzhiyun 	{ XFS_AGF_REFCOUNT_BLOCKS,	"REFCOUNT_BLOCKS" }, \
740*4882a593Smuzhiyun 	{ XFS_AGF_REFCOUNT_ROOT,	"REFCOUNT_ROOT" }, \
741*4882a593Smuzhiyun 	{ XFS_AGF_REFCOUNT_LEVEL,	"REFCOUNT_LEVEL" }, \
742*4882a593Smuzhiyun 	{ XFS_AGF_SPARE64,	"SPARE64" }
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun /* disk block (xfs_daddr_t) in the AG */
745*4882a593Smuzhiyun #define XFS_AGF_DADDR(mp)	((xfs_daddr_t)(1 << (mp)->m_sectbb_log))
746*4882a593Smuzhiyun #define	XFS_AGF_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGF_DADDR(mp))
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun /*
749*4882a593Smuzhiyun  * Size of the unlinked inode hash table in the agi.
750*4882a593Smuzhiyun  */
751*4882a593Smuzhiyun #define	XFS_AGI_UNLINKED_BUCKETS	64
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun typedef struct xfs_agi {
754*4882a593Smuzhiyun 	/*
755*4882a593Smuzhiyun 	 * Common allocation group header information
756*4882a593Smuzhiyun 	 */
757*4882a593Smuzhiyun 	__be32		agi_magicnum;	/* magic number == XFS_AGI_MAGIC */
758*4882a593Smuzhiyun 	__be32		agi_versionnum;	/* header version == XFS_AGI_VERSION */
759*4882a593Smuzhiyun 	__be32		agi_seqno;	/* sequence # starting from 0 */
760*4882a593Smuzhiyun 	__be32		agi_length;	/* size in blocks of a.g. */
761*4882a593Smuzhiyun 	/*
762*4882a593Smuzhiyun 	 * Inode information
763*4882a593Smuzhiyun 	 * Inodes are mapped by interpreting the inode number, so no
764*4882a593Smuzhiyun 	 * mapping data is needed here.
765*4882a593Smuzhiyun 	 */
766*4882a593Smuzhiyun 	__be32		agi_count;	/* count of allocated inodes */
767*4882a593Smuzhiyun 	__be32		agi_root;	/* root of inode btree */
768*4882a593Smuzhiyun 	__be32		agi_level;	/* levels in inode btree */
769*4882a593Smuzhiyun 	__be32		agi_freecount;	/* number of free inodes */
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	__be32		agi_newino;	/* new inode just allocated */
772*4882a593Smuzhiyun 	__be32		agi_dirino;	/* last directory inode chunk */
773*4882a593Smuzhiyun 	/*
774*4882a593Smuzhiyun 	 * Hash table of inodes which have been unlinked but are
775*4882a593Smuzhiyun 	 * still being referenced.
776*4882a593Smuzhiyun 	 */
777*4882a593Smuzhiyun 	__be32		agi_unlinked[XFS_AGI_UNLINKED_BUCKETS];
778*4882a593Smuzhiyun 	/*
779*4882a593Smuzhiyun 	 * This marks the end of logging region 1 and start of logging region 2.
780*4882a593Smuzhiyun 	 */
781*4882a593Smuzhiyun 	uuid_t		agi_uuid;	/* uuid of filesystem */
782*4882a593Smuzhiyun 	__be32		agi_crc;	/* crc of agi sector */
783*4882a593Smuzhiyun 	__be32		agi_pad32;
784*4882a593Smuzhiyun 	__be64		agi_lsn;	/* last write sequence */
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	__be32		agi_free_root; /* root of the free inode btree */
787*4882a593Smuzhiyun 	__be32		agi_free_level;/* levels in free inode btree */
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	__be32		agi_iblocks;	/* inobt blocks used */
790*4882a593Smuzhiyun 	__be32		agi_fblocks;	/* finobt blocks used */
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* structure must be padded to 64 bit alignment */
793*4882a593Smuzhiyun } xfs_agi_t;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun #define XFS_AGI_CRC_OFF		offsetof(struct xfs_agi, agi_crc)
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun #define	XFS_AGI_MAGICNUM	(1 << 0)
798*4882a593Smuzhiyun #define	XFS_AGI_VERSIONNUM	(1 << 1)
799*4882a593Smuzhiyun #define	XFS_AGI_SEQNO		(1 << 2)
800*4882a593Smuzhiyun #define	XFS_AGI_LENGTH		(1 << 3)
801*4882a593Smuzhiyun #define	XFS_AGI_COUNT		(1 << 4)
802*4882a593Smuzhiyun #define	XFS_AGI_ROOT		(1 << 5)
803*4882a593Smuzhiyun #define	XFS_AGI_LEVEL		(1 << 6)
804*4882a593Smuzhiyun #define	XFS_AGI_FREECOUNT	(1 << 7)
805*4882a593Smuzhiyun #define	XFS_AGI_NEWINO		(1 << 8)
806*4882a593Smuzhiyun #define	XFS_AGI_DIRINO		(1 << 9)
807*4882a593Smuzhiyun #define	XFS_AGI_UNLINKED	(1 << 10)
808*4882a593Smuzhiyun #define	XFS_AGI_NUM_BITS_R1	11	/* end of the 1st agi logging region */
809*4882a593Smuzhiyun #define	XFS_AGI_ALL_BITS_R1	((1 << XFS_AGI_NUM_BITS_R1) - 1)
810*4882a593Smuzhiyun #define	XFS_AGI_FREE_ROOT	(1 << 11)
811*4882a593Smuzhiyun #define	XFS_AGI_FREE_LEVEL	(1 << 12)
812*4882a593Smuzhiyun #define	XFS_AGI_IBLOCKS		(1 << 13) /* both inobt/finobt block counters */
813*4882a593Smuzhiyun #define	XFS_AGI_NUM_BITS_R2	14
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun /* disk block (xfs_daddr_t) in the AG */
816*4882a593Smuzhiyun #define XFS_AGI_DADDR(mp)	((xfs_daddr_t)(2 << (mp)->m_sectbb_log))
817*4882a593Smuzhiyun #define	XFS_AGI_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGI_DADDR(mp))
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun /*
820*4882a593Smuzhiyun  * The third a.g. block contains the a.g. freelist, an array
821*4882a593Smuzhiyun  * of block pointers to blocks owned by the allocation btree code.
822*4882a593Smuzhiyun  */
823*4882a593Smuzhiyun #define XFS_AGFL_DADDR(mp)	((xfs_daddr_t)(3 << (mp)->m_sectbb_log))
824*4882a593Smuzhiyun #define	XFS_AGFL_BLOCK(mp)	XFS_HDR_BLOCK(mp, XFS_AGFL_DADDR(mp))
825*4882a593Smuzhiyun #define	XFS_BUF_TO_AGFL(bp)	((struct xfs_agfl *)((bp)->b_addr))
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun struct xfs_agfl {
828*4882a593Smuzhiyun 	__be32		agfl_magicnum;
829*4882a593Smuzhiyun 	__be32		agfl_seqno;
830*4882a593Smuzhiyun 	uuid_t		agfl_uuid;
831*4882a593Smuzhiyun 	__be64		agfl_lsn;
832*4882a593Smuzhiyun 	__be32		agfl_crc;
833*4882a593Smuzhiyun } __attribute__((packed));
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun #define XFS_AGFL_CRC_OFF	offsetof(struct xfs_agfl, agfl_crc)
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun #define XFS_AGB_TO_FSB(mp,agno,agbno)	\
838*4882a593Smuzhiyun 	(((xfs_fsblock_t)(agno) << (mp)->m_sb.sb_agblklog) | (agbno))
839*4882a593Smuzhiyun #define	XFS_FSB_TO_AGNO(mp,fsbno)	\
840*4882a593Smuzhiyun 	((xfs_agnumber_t)((fsbno) >> (mp)->m_sb.sb_agblklog))
841*4882a593Smuzhiyun #define	XFS_FSB_TO_AGBNO(mp,fsbno)	\
842*4882a593Smuzhiyun 	((xfs_agblock_t)((fsbno) & xfs_mask32lo((mp)->m_sb.sb_agblklog)))
843*4882a593Smuzhiyun #define	XFS_AGB_TO_DADDR(mp,agno,agbno)	\
844*4882a593Smuzhiyun 	((xfs_daddr_t)XFS_FSB_TO_BB(mp, \
845*4882a593Smuzhiyun 		(xfs_fsblock_t)(agno) * (mp)->m_sb.sb_agblocks + (agbno)))
846*4882a593Smuzhiyun #define	XFS_AG_DADDR(mp,agno,d)		(XFS_AGB_TO_DADDR(mp, agno, 0) + (d))
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun /*
849*4882a593Smuzhiyun  * For checking for bad ranges of xfs_daddr_t's, covering multiple
850*4882a593Smuzhiyun  * allocation groups or a single xfs_daddr_t that's a superblock copy.
851*4882a593Smuzhiyun  */
852*4882a593Smuzhiyun #define	XFS_AG_CHECK_DADDR(mp,d,len)	\
853*4882a593Smuzhiyun 	((len) == 1 ? \
854*4882a593Smuzhiyun 	    ASSERT((d) == XFS_SB_DADDR || \
855*4882a593Smuzhiyun 		   xfs_daddr_to_agbno(mp, d) != XFS_SB_DADDR) : \
856*4882a593Smuzhiyun 	    ASSERT(xfs_daddr_to_agno(mp, d) == \
857*4882a593Smuzhiyun 		   xfs_daddr_to_agno(mp, (d) + (len) - 1)))
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun /*
860*4882a593Smuzhiyun  * XFS Timestamps
861*4882a593Smuzhiyun  * ==============
862*4882a593Smuzhiyun  *
863*4882a593Smuzhiyun  * Traditional ondisk inode timestamps consist of signed 32-bit counters for
864*4882a593Smuzhiyun  * seconds and nanoseconds; time zero is the Unix epoch, Jan  1 00:00:00 UTC
865*4882a593Smuzhiyun  * 1970, which means that the timestamp epoch is the same as the Unix epoch.
866*4882a593Smuzhiyun  * Therefore, the ondisk min and max defined here can be used directly to
867*4882a593Smuzhiyun  * constrain the incore timestamps on a Unix system.  Note that we actually
868*4882a593Smuzhiyun  * encode a __be64 value on disk.
869*4882a593Smuzhiyun  *
870*4882a593Smuzhiyun  * When the bigtime feature is enabled, ondisk inode timestamps become an
871*4882a593Smuzhiyun  * unsigned 64-bit nanoseconds counter.  This means that the bigtime inode
872*4882a593Smuzhiyun  * timestamp epoch is the start of the classic timestamp range, which is
873*4882a593Smuzhiyun  * Dec 31 20:45:52 UTC 1901.  Because the epochs are not the same, callers
874*4882a593Smuzhiyun  * /must/ use the bigtime conversion functions when encoding and decoding raw
875*4882a593Smuzhiyun  * timestamps.
876*4882a593Smuzhiyun  */
877*4882a593Smuzhiyun typedef __be64 xfs_timestamp_t;
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun /* Legacy timestamp encoding format. */
880*4882a593Smuzhiyun struct xfs_legacy_timestamp {
881*4882a593Smuzhiyun 	__be32		t_sec;		/* timestamp seconds */
882*4882a593Smuzhiyun 	__be32		t_nsec;		/* timestamp nanoseconds */
883*4882a593Smuzhiyun };
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun /*
886*4882a593Smuzhiyun  * Smallest possible ondisk seconds value with traditional timestamps.  This
887*4882a593Smuzhiyun  * corresponds exactly with the incore timestamp Dec 13 20:45:52 UTC 1901.
888*4882a593Smuzhiyun  */
889*4882a593Smuzhiyun #define XFS_LEGACY_TIME_MIN	((int64_t)S32_MIN)
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun /*
892*4882a593Smuzhiyun  * Largest possible ondisk seconds value with traditional timestamps.  This
893*4882a593Smuzhiyun  * corresponds exactly with the incore timestamp Jan 19 03:14:07 UTC 2038.
894*4882a593Smuzhiyun  */
895*4882a593Smuzhiyun #define XFS_LEGACY_TIME_MAX	((int64_t)S32_MAX)
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun /*
898*4882a593Smuzhiyun  * Smallest possible ondisk seconds value with bigtime timestamps.  This
899*4882a593Smuzhiyun  * corresponds (after conversion to a Unix timestamp) with the traditional
900*4882a593Smuzhiyun  * minimum timestamp of Dec 13 20:45:52 UTC 1901.
901*4882a593Smuzhiyun  */
902*4882a593Smuzhiyun #define XFS_BIGTIME_TIME_MIN	((int64_t)0)
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun /*
905*4882a593Smuzhiyun  * Largest supported ondisk seconds value with bigtime timestamps.  This
906*4882a593Smuzhiyun  * corresponds (after conversion to a Unix timestamp) with an incore timestamp
907*4882a593Smuzhiyun  * of Jul  2 20:20:24 UTC 2486.
908*4882a593Smuzhiyun  *
909*4882a593Smuzhiyun  * We round down the ondisk limit so that the bigtime quota and inode max
910*4882a593Smuzhiyun  * timestamps will be the same.
911*4882a593Smuzhiyun  */
912*4882a593Smuzhiyun #define XFS_BIGTIME_TIME_MAX	((int64_t)((-1ULL / NSEC_PER_SEC) & ~0x3ULL))
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun /*
915*4882a593Smuzhiyun  * Bigtime epoch is set exactly to the minimum time value that a traditional
916*4882a593Smuzhiyun  * 32-bit timestamp can represent when using the Unix epoch as a reference.
917*4882a593Smuzhiyun  * Hence the Unix epoch is at a fixed offset into the supported bigtime
918*4882a593Smuzhiyun  * timestamp range.
919*4882a593Smuzhiyun  *
920*4882a593Smuzhiyun  * The bigtime epoch also matches the minimum value an on-disk 32-bit XFS
921*4882a593Smuzhiyun  * timestamp can represent so we will not lose any fidelity in converting
922*4882a593Smuzhiyun  * to/from unix and bigtime timestamps.
923*4882a593Smuzhiyun  *
924*4882a593Smuzhiyun  * The following conversion factor converts a seconds counter from the Unix
925*4882a593Smuzhiyun  * epoch to the bigtime epoch.
926*4882a593Smuzhiyun  */
927*4882a593Smuzhiyun #define XFS_BIGTIME_EPOCH_OFFSET	(-(int64_t)S32_MIN)
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun /* Convert a timestamp from the Unix epoch to the bigtime epoch. */
xfs_unix_to_bigtime(time64_t unix_seconds)930*4882a593Smuzhiyun static inline uint64_t xfs_unix_to_bigtime(time64_t unix_seconds)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	return (uint64_t)unix_seconds + XFS_BIGTIME_EPOCH_OFFSET;
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun /* Convert a timestamp from the bigtime epoch to the Unix epoch. */
xfs_bigtime_to_unix(uint64_t ondisk_seconds)936*4882a593Smuzhiyun static inline time64_t xfs_bigtime_to_unix(uint64_t ondisk_seconds)
937*4882a593Smuzhiyun {
938*4882a593Smuzhiyun 	return (time64_t)ondisk_seconds - XFS_BIGTIME_EPOCH_OFFSET;
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun /*
942*4882a593Smuzhiyun  * On-disk inode structure.
943*4882a593Smuzhiyun  *
944*4882a593Smuzhiyun  * This is just the header or "dinode core", the inode is expanded to fill a
945*4882a593Smuzhiyun  * variable size the leftover area split into a data and an attribute fork.
946*4882a593Smuzhiyun  * The format of the data and attribute fork depends on the format of the
947*4882a593Smuzhiyun  * inode as indicated by di_format and di_aformat.  To access the data and
948*4882a593Smuzhiyun  * attribute use the XFS_DFORK_DPTR, XFS_DFORK_APTR, and XFS_DFORK_PTR macros
949*4882a593Smuzhiyun  * below.
950*4882a593Smuzhiyun  *
951*4882a593Smuzhiyun  * There is a very similar struct icdinode in xfs_inode which matches the
952*4882a593Smuzhiyun  * layout of the first 96 bytes of this structure, but is kept in native
953*4882a593Smuzhiyun  * format instead of big endian.
954*4882a593Smuzhiyun  *
955*4882a593Smuzhiyun  * Note: di_flushiter is only used by v1/2 inodes - it's effectively a zeroed
956*4882a593Smuzhiyun  * padding field for v3 inodes.
957*4882a593Smuzhiyun  */
958*4882a593Smuzhiyun #define	XFS_DINODE_MAGIC		0x494e	/* 'IN' */
959*4882a593Smuzhiyun typedef struct xfs_dinode {
960*4882a593Smuzhiyun 	__be16		di_magic;	/* inode magic # = XFS_DINODE_MAGIC */
961*4882a593Smuzhiyun 	__be16		di_mode;	/* mode and type of file */
962*4882a593Smuzhiyun 	__u8		di_version;	/* inode version */
963*4882a593Smuzhiyun 	__u8		di_format;	/* format of di_c data */
964*4882a593Smuzhiyun 	__be16		di_onlink;	/* old number of links to file */
965*4882a593Smuzhiyun 	__be32		di_uid;		/* owner's user id */
966*4882a593Smuzhiyun 	__be32		di_gid;		/* owner's group id */
967*4882a593Smuzhiyun 	__be32		di_nlink;	/* number of links to file */
968*4882a593Smuzhiyun 	__be16		di_projid_lo;	/* lower part of owner's project id */
969*4882a593Smuzhiyun 	__be16		di_projid_hi;	/* higher part owner's project id */
970*4882a593Smuzhiyun 	__u8		di_pad[6];	/* unused, zeroed space */
971*4882a593Smuzhiyun 	__be16		di_flushiter;	/* incremented on flush */
972*4882a593Smuzhiyun 	xfs_timestamp_t	di_atime;	/* time last accessed */
973*4882a593Smuzhiyun 	xfs_timestamp_t	di_mtime;	/* time last modified */
974*4882a593Smuzhiyun 	xfs_timestamp_t	di_ctime;	/* time created/inode modified */
975*4882a593Smuzhiyun 	__be64		di_size;	/* number of bytes in file */
976*4882a593Smuzhiyun 	__be64		di_nblocks;	/* # of direct & btree blocks used */
977*4882a593Smuzhiyun 	__be32		di_extsize;	/* basic/minimum extent size for file */
978*4882a593Smuzhiyun 	__be32		di_nextents;	/* number of extents in data fork */
979*4882a593Smuzhiyun 	__be16		di_anextents;	/* number of extents in attribute fork*/
980*4882a593Smuzhiyun 	__u8		di_forkoff;	/* attr fork offs, <<3 for 64b align */
981*4882a593Smuzhiyun 	__s8		di_aformat;	/* format of attr fork's data */
982*4882a593Smuzhiyun 	__be32		di_dmevmask;	/* DMIG event mask */
983*4882a593Smuzhiyun 	__be16		di_dmstate;	/* DMIG state info */
984*4882a593Smuzhiyun 	__be16		di_flags;	/* random flags, XFS_DIFLAG_... */
985*4882a593Smuzhiyun 	__be32		di_gen;		/* generation number */
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	/* di_next_unlinked is the only non-core field in the old dinode */
988*4882a593Smuzhiyun 	__be32		di_next_unlinked;/* agi unlinked list ptr */
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	/* start of the extended dinode, writable fields */
991*4882a593Smuzhiyun 	__le32		di_crc;		/* CRC of the inode */
992*4882a593Smuzhiyun 	__be64		di_changecount;	/* number of attribute changes */
993*4882a593Smuzhiyun 	__be64		di_lsn;		/* flush sequence */
994*4882a593Smuzhiyun 	__be64		di_flags2;	/* more random flags */
995*4882a593Smuzhiyun 	__be32		di_cowextsize;	/* basic cow extent size for file */
996*4882a593Smuzhiyun 	__u8		di_pad2[12];	/* more padding for future expansion */
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	/* fields only written to during inode creation */
999*4882a593Smuzhiyun 	xfs_timestamp_t	di_crtime;	/* time created */
1000*4882a593Smuzhiyun 	__be64		di_ino;		/* inode number */
1001*4882a593Smuzhiyun 	uuid_t		di_uuid;	/* UUID of the filesystem */
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	/* structure must be padded to 64 bit alignment */
1004*4882a593Smuzhiyun } xfs_dinode_t;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun #define XFS_DINODE_CRC_OFF	offsetof(struct xfs_dinode, di_crc)
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun #define DI_MAX_FLUSH 0xffff
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun /*
1011*4882a593Smuzhiyun  * Size of the core inode on disk.  Version 1 and 2 inodes have
1012*4882a593Smuzhiyun  * the same size, but version 3 has grown a few additional fields.
1013*4882a593Smuzhiyun  */
xfs_dinode_size(int version)1014*4882a593Smuzhiyun static inline uint xfs_dinode_size(int version)
1015*4882a593Smuzhiyun {
1016*4882a593Smuzhiyun 	if (version == 3)
1017*4882a593Smuzhiyun 		return sizeof(struct xfs_dinode);
1018*4882a593Smuzhiyun 	return offsetof(struct xfs_dinode, di_crc);
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun /*
1022*4882a593Smuzhiyun  * The 32 bit link count in the inode theoretically maxes out at UINT_MAX.
1023*4882a593Smuzhiyun  * Since the pathconf interface is signed, we use 2^31 - 1 instead.
1024*4882a593Smuzhiyun  */
1025*4882a593Smuzhiyun #define	XFS_MAXLINK		((1U << 31) - 1U)
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun /*
1028*4882a593Smuzhiyun  * Values for di_format
1029*4882a593Smuzhiyun  *
1030*4882a593Smuzhiyun  * This enum is used in string mapping in xfs_trace.h; please keep the
1031*4882a593Smuzhiyun  * TRACE_DEFINE_ENUMs for it up to date.
1032*4882a593Smuzhiyun  */
1033*4882a593Smuzhiyun enum xfs_dinode_fmt {
1034*4882a593Smuzhiyun 	XFS_DINODE_FMT_DEV,		/* xfs_dev_t */
1035*4882a593Smuzhiyun 	XFS_DINODE_FMT_LOCAL,		/* bulk data */
1036*4882a593Smuzhiyun 	XFS_DINODE_FMT_EXTENTS,		/* struct xfs_bmbt_rec */
1037*4882a593Smuzhiyun 	XFS_DINODE_FMT_BTREE,		/* struct xfs_bmdr_block */
1038*4882a593Smuzhiyun 	XFS_DINODE_FMT_UUID		/* added long ago, but never used */
1039*4882a593Smuzhiyun };
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun #define XFS_INODE_FORMAT_STR \
1042*4882a593Smuzhiyun 	{ XFS_DINODE_FMT_DEV,		"dev" }, \
1043*4882a593Smuzhiyun 	{ XFS_DINODE_FMT_LOCAL,		"local" }, \
1044*4882a593Smuzhiyun 	{ XFS_DINODE_FMT_EXTENTS,	"extent" }, \
1045*4882a593Smuzhiyun 	{ XFS_DINODE_FMT_BTREE,		"btree" }, \
1046*4882a593Smuzhiyun 	{ XFS_DINODE_FMT_UUID,		"uuid" }
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun /*
1049*4882a593Smuzhiyun  * Inode minimum and maximum sizes.
1050*4882a593Smuzhiyun  */
1051*4882a593Smuzhiyun #define	XFS_DINODE_MIN_LOG	8
1052*4882a593Smuzhiyun #define	XFS_DINODE_MAX_LOG	11
1053*4882a593Smuzhiyun #define	XFS_DINODE_MIN_SIZE	(1 << XFS_DINODE_MIN_LOG)
1054*4882a593Smuzhiyun #define	XFS_DINODE_MAX_SIZE	(1 << XFS_DINODE_MAX_LOG)
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun /*
1057*4882a593Smuzhiyun  * Inode size for given fs.
1058*4882a593Smuzhiyun  */
1059*4882a593Smuzhiyun #define XFS_DINODE_SIZE(sbp) \
1060*4882a593Smuzhiyun 	(xfs_sb_version_has_v3inode(sbp) ? \
1061*4882a593Smuzhiyun 		sizeof(struct xfs_dinode) : \
1062*4882a593Smuzhiyun 		offsetof(struct xfs_dinode, di_crc))
1063*4882a593Smuzhiyun #define XFS_LITINO(mp) \
1064*4882a593Smuzhiyun 	((mp)->m_sb.sb_inodesize - XFS_DINODE_SIZE(&(mp)->m_sb))
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun /*
1067*4882a593Smuzhiyun  * Inode data & attribute fork sizes, per inode.
1068*4882a593Smuzhiyun  */
1069*4882a593Smuzhiyun #define XFS_DFORK_BOFF(dip)		((int)((dip)->di_forkoff << 3))
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun #define XFS_DFORK_DSIZE(dip,mp) \
1072*4882a593Smuzhiyun 	((dip)->di_forkoff ? XFS_DFORK_BOFF(dip) : XFS_LITINO(mp))
1073*4882a593Smuzhiyun #define XFS_DFORK_ASIZE(dip,mp) \
1074*4882a593Smuzhiyun 	((dip)->di_forkoff ? XFS_LITINO(mp) - XFS_DFORK_BOFF(dip) : 0)
1075*4882a593Smuzhiyun #define XFS_DFORK_SIZE(dip,mp,w) \
1076*4882a593Smuzhiyun 	((w) == XFS_DATA_FORK ? \
1077*4882a593Smuzhiyun 		XFS_DFORK_DSIZE(dip, mp) : \
1078*4882a593Smuzhiyun 		XFS_DFORK_ASIZE(dip, mp))
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun #define XFS_DFORK_MAXEXT(dip, mp, w) \
1081*4882a593Smuzhiyun 	(XFS_DFORK_SIZE(dip, mp, w) / sizeof(struct xfs_bmbt_rec))
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun /*
1084*4882a593Smuzhiyun  * Return pointers to the data or attribute forks.
1085*4882a593Smuzhiyun  */
1086*4882a593Smuzhiyun #define XFS_DFORK_DPTR(dip) \
1087*4882a593Smuzhiyun 	((char *)dip + xfs_dinode_size(dip->di_version))
1088*4882a593Smuzhiyun #define XFS_DFORK_APTR(dip)	\
1089*4882a593Smuzhiyun 	(XFS_DFORK_DPTR(dip) + XFS_DFORK_BOFF(dip))
1090*4882a593Smuzhiyun #define XFS_DFORK_PTR(dip,w)	\
1091*4882a593Smuzhiyun 	((w) == XFS_DATA_FORK ? XFS_DFORK_DPTR(dip) : XFS_DFORK_APTR(dip))
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun #define XFS_DFORK_FORMAT(dip,w) \
1094*4882a593Smuzhiyun 	((w) == XFS_DATA_FORK ? \
1095*4882a593Smuzhiyun 		(dip)->di_format : \
1096*4882a593Smuzhiyun 		(dip)->di_aformat)
1097*4882a593Smuzhiyun #define XFS_DFORK_NEXTENTS(dip,w) \
1098*4882a593Smuzhiyun 	((w) == XFS_DATA_FORK ? \
1099*4882a593Smuzhiyun 		be32_to_cpu((dip)->di_nextents) : \
1100*4882a593Smuzhiyun 		be16_to_cpu((dip)->di_anextents))
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun /*
1103*4882a593Smuzhiyun  * For block and character special files the 32bit dev_t is stored at the
1104*4882a593Smuzhiyun  * beginning of the data fork.
1105*4882a593Smuzhiyun  */
xfs_dinode_get_rdev(struct xfs_dinode * dip)1106*4882a593Smuzhiyun static inline xfs_dev_t xfs_dinode_get_rdev(struct xfs_dinode *dip)
1107*4882a593Smuzhiyun {
1108*4882a593Smuzhiyun 	return be32_to_cpu(*(__be32 *)XFS_DFORK_DPTR(dip));
1109*4882a593Smuzhiyun }
1110*4882a593Smuzhiyun 
xfs_dinode_put_rdev(struct xfs_dinode * dip,xfs_dev_t rdev)1111*4882a593Smuzhiyun static inline void xfs_dinode_put_rdev(struct xfs_dinode *dip, xfs_dev_t rdev)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun 	*(__be32 *)XFS_DFORK_DPTR(dip) = cpu_to_be32(rdev);
1114*4882a593Smuzhiyun }
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun /*
1117*4882a593Smuzhiyun  * Values for di_flags
1118*4882a593Smuzhiyun  */
1119*4882a593Smuzhiyun #define XFS_DIFLAG_REALTIME_BIT  0	/* file's blocks come from rt area */
1120*4882a593Smuzhiyun #define XFS_DIFLAG_PREALLOC_BIT  1	/* file space has been preallocated */
1121*4882a593Smuzhiyun #define XFS_DIFLAG_NEWRTBM_BIT   2	/* for rtbitmap inode, new format */
1122*4882a593Smuzhiyun #define XFS_DIFLAG_IMMUTABLE_BIT 3	/* inode is immutable */
1123*4882a593Smuzhiyun #define XFS_DIFLAG_APPEND_BIT    4	/* inode is append-only */
1124*4882a593Smuzhiyun #define XFS_DIFLAG_SYNC_BIT      5	/* inode is written synchronously */
1125*4882a593Smuzhiyun #define XFS_DIFLAG_NOATIME_BIT   6	/* do not update atime */
1126*4882a593Smuzhiyun #define XFS_DIFLAG_NODUMP_BIT    7	/* do not dump */
1127*4882a593Smuzhiyun #define XFS_DIFLAG_RTINHERIT_BIT 8	/* create with realtime bit set */
1128*4882a593Smuzhiyun #define XFS_DIFLAG_PROJINHERIT_BIT   9	/* create with parents projid */
1129*4882a593Smuzhiyun #define XFS_DIFLAG_NOSYMLINKS_BIT   10	/* disallow symlink creation */
1130*4882a593Smuzhiyun #define XFS_DIFLAG_EXTSIZE_BIT      11	/* inode extent size allocator hint */
1131*4882a593Smuzhiyun #define XFS_DIFLAG_EXTSZINHERIT_BIT 12	/* inherit inode extent size */
1132*4882a593Smuzhiyun #define XFS_DIFLAG_NODEFRAG_BIT     13	/* do not reorganize/defragment */
1133*4882a593Smuzhiyun #define XFS_DIFLAG_FILESTREAM_BIT   14  /* use filestream allocator */
1134*4882a593Smuzhiyun /* Do not use bit 15, di_flags is legacy and unchanging now */
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun #define XFS_DIFLAG_REALTIME      (1 << XFS_DIFLAG_REALTIME_BIT)
1137*4882a593Smuzhiyun #define XFS_DIFLAG_PREALLOC      (1 << XFS_DIFLAG_PREALLOC_BIT)
1138*4882a593Smuzhiyun #define XFS_DIFLAG_NEWRTBM       (1 << XFS_DIFLAG_NEWRTBM_BIT)
1139*4882a593Smuzhiyun #define XFS_DIFLAG_IMMUTABLE     (1 << XFS_DIFLAG_IMMUTABLE_BIT)
1140*4882a593Smuzhiyun #define XFS_DIFLAG_APPEND        (1 << XFS_DIFLAG_APPEND_BIT)
1141*4882a593Smuzhiyun #define XFS_DIFLAG_SYNC          (1 << XFS_DIFLAG_SYNC_BIT)
1142*4882a593Smuzhiyun #define XFS_DIFLAG_NOATIME       (1 << XFS_DIFLAG_NOATIME_BIT)
1143*4882a593Smuzhiyun #define XFS_DIFLAG_NODUMP        (1 << XFS_DIFLAG_NODUMP_BIT)
1144*4882a593Smuzhiyun #define XFS_DIFLAG_RTINHERIT     (1 << XFS_DIFLAG_RTINHERIT_BIT)
1145*4882a593Smuzhiyun #define XFS_DIFLAG_PROJINHERIT   (1 << XFS_DIFLAG_PROJINHERIT_BIT)
1146*4882a593Smuzhiyun #define XFS_DIFLAG_NOSYMLINKS    (1 << XFS_DIFLAG_NOSYMLINKS_BIT)
1147*4882a593Smuzhiyun #define XFS_DIFLAG_EXTSIZE       (1 << XFS_DIFLAG_EXTSIZE_BIT)
1148*4882a593Smuzhiyun #define XFS_DIFLAG_EXTSZINHERIT  (1 << XFS_DIFLAG_EXTSZINHERIT_BIT)
1149*4882a593Smuzhiyun #define XFS_DIFLAG_NODEFRAG      (1 << XFS_DIFLAG_NODEFRAG_BIT)
1150*4882a593Smuzhiyun #define XFS_DIFLAG_FILESTREAM    (1 << XFS_DIFLAG_FILESTREAM_BIT)
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun #define XFS_DIFLAG_ANY \
1153*4882a593Smuzhiyun 	(XFS_DIFLAG_REALTIME | XFS_DIFLAG_PREALLOC | XFS_DIFLAG_NEWRTBM | \
1154*4882a593Smuzhiyun 	 XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND | XFS_DIFLAG_SYNC | \
1155*4882a593Smuzhiyun 	 XFS_DIFLAG_NOATIME | XFS_DIFLAG_NODUMP | XFS_DIFLAG_RTINHERIT | \
1156*4882a593Smuzhiyun 	 XFS_DIFLAG_PROJINHERIT | XFS_DIFLAG_NOSYMLINKS | XFS_DIFLAG_EXTSIZE | \
1157*4882a593Smuzhiyun 	 XFS_DIFLAG_EXTSZINHERIT | XFS_DIFLAG_NODEFRAG | XFS_DIFLAG_FILESTREAM)
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun /*
1160*4882a593Smuzhiyun  * Values for di_flags2 These start by being exposed to userspace in the upper
1161*4882a593Smuzhiyun  * 16 bits of the XFS_XFLAG_s range.
1162*4882a593Smuzhiyun  */
1163*4882a593Smuzhiyun #define XFS_DIFLAG2_DAX_BIT	0	/* use DAX for this inode */
1164*4882a593Smuzhiyun #define XFS_DIFLAG2_REFLINK_BIT	1	/* file's blocks may be shared */
1165*4882a593Smuzhiyun #define XFS_DIFLAG2_COWEXTSIZE_BIT   2  /* copy on write extent size hint */
1166*4882a593Smuzhiyun #define XFS_DIFLAG2_BIGTIME_BIT	3	/* big timestamps */
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun #define XFS_DIFLAG2_DAX		(1 << XFS_DIFLAG2_DAX_BIT)
1169*4882a593Smuzhiyun #define XFS_DIFLAG2_REFLINK     (1 << XFS_DIFLAG2_REFLINK_BIT)
1170*4882a593Smuzhiyun #define XFS_DIFLAG2_COWEXTSIZE  (1 << XFS_DIFLAG2_COWEXTSIZE_BIT)
1171*4882a593Smuzhiyun #define XFS_DIFLAG2_BIGTIME	(1 << XFS_DIFLAG2_BIGTIME_BIT)
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun #define XFS_DIFLAG2_ANY \
1174*4882a593Smuzhiyun 	(XFS_DIFLAG2_DAX | XFS_DIFLAG2_REFLINK | XFS_DIFLAG2_COWEXTSIZE | \
1175*4882a593Smuzhiyun 	 XFS_DIFLAG2_BIGTIME)
1176*4882a593Smuzhiyun 
xfs_dinode_has_bigtime(const struct xfs_dinode * dip)1177*4882a593Smuzhiyun static inline bool xfs_dinode_has_bigtime(const struct xfs_dinode *dip)
1178*4882a593Smuzhiyun {
1179*4882a593Smuzhiyun 	return dip->di_version >= 3 &&
1180*4882a593Smuzhiyun 	       (dip->di_flags2 & cpu_to_be64(XFS_DIFLAG2_BIGTIME));
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun /*
1184*4882a593Smuzhiyun  * Inode number format:
1185*4882a593Smuzhiyun  * low inopblog bits - offset in block
1186*4882a593Smuzhiyun  * next agblklog bits - block number in ag
1187*4882a593Smuzhiyun  * next agno_log bits - ag number
1188*4882a593Smuzhiyun  * high agno_log-agblklog-inopblog bits - 0
1189*4882a593Smuzhiyun  */
1190*4882a593Smuzhiyun #define	XFS_INO_MASK(k)			(uint32_t)((1ULL << (k)) - 1)
1191*4882a593Smuzhiyun #define	XFS_INO_OFFSET_BITS(mp)		(mp)->m_sb.sb_inopblog
1192*4882a593Smuzhiyun #define	XFS_INO_AGBNO_BITS(mp)		(mp)->m_sb.sb_agblklog
1193*4882a593Smuzhiyun #define	XFS_INO_AGINO_BITS(mp)		((mp)->m_ino_geo.agino_log)
1194*4882a593Smuzhiyun #define	XFS_INO_AGNO_BITS(mp)		(mp)->m_agno_log
1195*4882a593Smuzhiyun #define	XFS_INO_BITS(mp)		\
1196*4882a593Smuzhiyun 	XFS_INO_AGNO_BITS(mp) + XFS_INO_AGINO_BITS(mp)
1197*4882a593Smuzhiyun #define	XFS_INO_TO_AGNO(mp,i)		\
1198*4882a593Smuzhiyun 	((xfs_agnumber_t)((i) >> XFS_INO_AGINO_BITS(mp)))
1199*4882a593Smuzhiyun #define	XFS_INO_TO_AGINO(mp,i)		\
1200*4882a593Smuzhiyun 	((xfs_agino_t)(i) & XFS_INO_MASK(XFS_INO_AGINO_BITS(mp)))
1201*4882a593Smuzhiyun #define	XFS_INO_TO_AGBNO(mp,i)		\
1202*4882a593Smuzhiyun 	(((xfs_agblock_t)(i) >> XFS_INO_OFFSET_BITS(mp)) & \
1203*4882a593Smuzhiyun 		XFS_INO_MASK(XFS_INO_AGBNO_BITS(mp)))
1204*4882a593Smuzhiyun #define	XFS_INO_TO_OFFSET(mp,i)		\
1205*4882a593Smuzhiyun 	((int)(i) & XFS_INO_MASK(XFS_INO_OFFSET_BITS(mp)))
1206*4882a593Smuzhiyun #define	XFS_INO_TO_FSB(mp,i)		\
1207*4882a593Smuzhiyun 	XFS_AGB_TO_FSB(mp, XFS_INO_TO_AGNO(mp,i), XFS_INO_TO_AGBNO(mp,i))
1208*4882a593Smuzhiyun #define	XFS_AGINO_TO_INO(mp,a,i)	\
1209*4882a593Smuzhiyun 	(((xfs_ino_t)(a) << XFS_INO_AGINO_BITS(mp)) | (i))
1210*4882a593Smuzhiyun #define	XFS_AGINO_TO_AGBNO(mp,i)	((i) >> XFS_INO_OFFSET_BITS(mp))
1211*4882a593Smuzhiyun #define	XFS_AGINO_TO_OFFSET(mp,i)	\
1212*4882a593Smuzhiyun 	((i) & XFS_INO_MASK(XFS_INO_OFFSET_BITS(mp)))
1213*4882a593Smuzhiyun #define	XFS_OFFBNO_TO_AGINO(mp,b,o)	\
1214*4882a593Smuzhiyun 	((xfs_agino_t)(((b) << XFS_INO_OFFSET_BITS(mp)) | (o)))
1215*4882a593Smuzhiyun #define	XFS_FSB_TO_INO(mp, b)	((xfs_ino_t)((b) << XFS_INO_OFFSET_BITS(mp)))
1216*4882a593Smuzhiyun #define	XFS_AGB_TO_AGINO(mp, b)	((xfs_agino_t)((b) << XFS_INO_OFFSET_BITS(mp)))
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun #define	XFS_MAXINUMBER		((xfs_ino_t)((1ULL << 56) - 1ULL))
1219*4882a593Smuzhiyun #define	XFS_MAXINUMBER_32	((xfs_ino_t)((1ULL << 32) - 1ULL))
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun /*
1222*4882a593Smuzhiyun  * RealTime Device format definitions
1223*4882a593Smuzhiyun  */
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun /* Min and max rt extent sizes, specified in bytes */
1226*4882a593Smuzhiyun #define	XFS_MAX_RTEXTSIZE	(1024 * 1024 * 1024)	/* 1GB */
1227*4882a593Smuzhiyun #define	XFS_DFL_RTEXTSIZE	(64 * 1024)	        /* 64kB */
1228*4882a593Smuzhiyun #define	XFS_MIN_RTEXTSIZE	(4 * 1024)		/* 4kB */
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun #define	XFS_BLOCKSIZE(mp)	((mp)->m_sb.sb_blocksize)
1231*4882a593Smuzhiyun #define	XFS_BLOCKMASK(mp)	((mp)->m_blockmask)
1232*4882a593Smuzhiyun #define	XFS_BLOCKWSIZE(mp)	((mp)->m_blockwsize)
1233*4882a593Smuzhiyun #define	XFS_BLOCKWMASK(mp)	((mp)->m_blockwmask)
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun /*
1236*4882a593Smuzhiyun  * RT Summary and bit manipulation macros.
1237*4882a593Smuzhiyun  */
1238*4882a593Smuzhiyun #define	XFS_SUMOFFS(mp,ls,bb)	((int)((ls) * (mp)->m_sb.sb_rbmblocks + (bb)))
1239*4882a593Smuzhiyun #define	XFS_SUMOFFSTOBLOCK(mp,s)	\
1240*4882a593Smuzhiyun 	(((s) * (uint)sizeof(xfs_suminfo_t)) >> (mp)->m_sb.sb_blocklog)
1241*4882a593Smuzhiyun #define	XFS_SUMPTR(mp,bp,so)	\
1242*4882a593Smuzhiyun 	((xfs_suminfo_t *)((bp)->b_addr + \
1243*4882a593Smuzhiyun 		(((so) * (uint)sizeof(xfs_suminfo_t)) & XFS_BLOCKMASK(mp))))
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun #define	XFS_BITTOBLOCK(mp,bi)	((bi) >> (mp)->m_blkbit_log)
1246*4882a593Smuzhiyun #define	XFS_BLOCKTOBIT(mp,bb)	((bb) << (mp)->m_blkbit_log)
1247*4882a593Smuzhiyun #define	XFS_BITTOWORD(mp,bi)	\
1248*4882a593Smuzhiyun 	((int)(((bi) >> XFS_NBWORDLOG) & XFS_BLOCKWMASK(mp)))
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun #define	XFS_RTMIN(a,b)	((a) < (b) ? (a) : (b))
1251*4882a593Smuzhiyun #define	XFS_RTMAX(a,b)	((a) > (b) ? (a) : (b))
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun #define	XFS_RTLOBIT(w)	xfs_lowbit32(w)
1254*4882a593Smuzhiyun #define	XFS_RTHIBIT(w)	xfs_highbit32(w)
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun #define	XFS_RTBLOCKLOG(b)	xfs_highbit64(b)
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun /*
1259*4882a593Smuzhiyun  * Dquot and dquot block format definitions
1260*4882a593Smuzhiyun  */
1261*4882a593Smuzhiyun #define XFS_DQUOT_MAGIC		0x4451		/* 'DQ' */
1262*4882a593Smuzhiyun #define XFS_DQUOT_VERSION	(uint8_t)0x01	/* latest version number */
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun #define XFS_DQTYPE_USER		0x01		/* user dquot record */
1265*4882a593Smuzhiyun #define XFS_DQTYPE_PROJ		0x02		/* project dquot record */
1266*4882a593Smuzhiyun #define XFS_DQTYPE_GROUP	0x04		/* group dquot record */
1267*4882a593Smuzhiyun #define XFS_DQTYPE_BIGTIME	0x80		/* large expiry timestamps */
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun /* bitmask to determine if this is a user/group/project dquot */
1270*4882a593Smuzhiyun #define XFS_DQTYPE_REC_MASK	(XFS_DQTYPE_USER | \
1271*4882a593Smuzhiyun 				 XFS_DQTYPE_PROJ | \
1272*4882a593Smuzhiyun 				 XFS_DQTYPE_GROUP)
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun #define XFS_DQTYPE_ANY		(XFS_DQTYPE_REC_MASK | \
1275*4882a593Smuzhiyun 				 XFS_DQTYPE_BIGTIME)
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun /*
1278*4882a593Smuzhiyun  * XFS Quota Timers
1279*4882a593Smuzhiyun  * ================
1280*4882a593Smuzhiyun  *
1281*4882a593Smuzhiyun  * Traditional quota grace period expiration timers are an unsigned 32-bit
1282*4882a593Smuzhiyun  * seconds counter; time zero is the Unix epoch, Jan  1 00:00:01 UTC 1970.
1283*4882a593Smuzhiyun  * Note that an expiration value of zero means that the quota limit has not
1284*4882a593Smuzhiyun  * been reached, and therefore no expiration has been set.  Therefore, the
1285*4882a593Smuzhiyun  * ondisk min and max defined here can be used directly to constrain the incore
1286*4882a593Smuzhiyun  * quota expiration timestamps on a Unix system.
1287*4882a593Smuzhiyun  *
1288*4882a593Smuzhiyun  * When bigtime is enabled, we trade two bits of precision to expand the
1289*4882a593Smuzhiyun  * expiration timeout range to match that of big inode timestamps.  The min and
1290*4882a593Smuzhiyun  * max recorded here are the on-disk limits, not a Unix timestamp.
1291*4882a593Smuzhiyun  *
1292*4882a593Smuzhiyun  * The grace period for each quota type is stored in the root dquot (id = 0)
1293*4882a593Smuzhiyun  * and is applied to a non-root dquot when it exceeds the soft or hard limits.
1294*4882a593Smuzhiyun  * The length of quota grace periods are unsigned 32-bit quantities measured in
1295*4882a593Smuzhiyun  * units of seconds.  A value of zero means to use the default period.
1296*4882a593Smuzhiyun  */
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun /*
1299*4882a593Smuzhiyun  * Smallest possible ondisk quota expiration value with traditional timestamps.
1300*4882a593Smuzhiyun  * This corresponds exactly with the incore expiration Jan  1 00:00:01 UTC 1970.
1301*4882a593Smuzhiyun  */
1302*4882a593Smuzhiyun #define XFS_DQ_LEGACY_EXPIRY_MIN	((int64_t)1)
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun /*
1305*4882a593Smuzhiyun  * Largest possible ondisk quota expiration value with traditional timestamps.
1306*4882a593Smuzhiyun  * This corresponds exactly with the incore expiration Feb  7 06:28:15 UTC 2106.
1307*4882a593Smuzhiyun  */
1308*4882a593Smuzhiyun #define XFS_DQ_LEGACY_EXPIRY_MAX	((int64_t)U32_MAX)
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun /*
1311*4882a593Smuzhiyun  * Smallest possible ondisk quota expiration value with bigtime timestamps.
1312*4882a593Smuzhiyun  * This corresponds (after conversion to a Unix timestamp) with the incore
1313*4882a593Smuzhiyun  * expiration of Jan  1 00:00:04 UTC 1970.
1314*4882a593Smuzhiyun  */
1315*4882a593Smuzhiyun #define XFS_DQ_BIGTIME_EXPIRY_MIN	(XFS_DQ_LEGACY_EXPIRY_MIN)
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun /*
1318*4882a593Smuzhiyun  * Largest supported ondisk quota expiration value with bigtime timestamps.
1319*4882a593Smuzhiyun  * This corresponds (after conversion to a Unix timestamp) with an incore
1320*4882a593Smuzhiyun  * expiration of Jul  2 20:20:24 UTC 2486.
1321*4882a593Smuzhiyun  *
1322*4882a593Smuzhiyun  * The ondisk field supports values up to -1U, which corresponds to an incore
1323*4882a593Smuzhiyun  * expiration in 2514.  This is beyond the maximum the bigtime inode timestamp,
1324*4882a593Smuzhiyun  * so we cap the maximum bigtime quota expiration to the max inode timestamp.
1325*4882a593Smuzhiyun  */
1326*4882a593Smuzhiyun #define XFS_DQ_BIGTIME_EXPIRY_MAX	((int64_t)4074815106U)
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun /*
1329*4882a593Smuzhiyun  * The following conversion factors assist in converting a quota expiration
1330*4882a593Smuzhiyun  * timestamp between the incore and ondisk formats.
1331*4882a593Smuzhiyun  */
1332*4882a593Smuzhiyun #define XFS_DQ_BIGTIME_SHIFT	(2)
1333*4882a593Smuzhiyun #define XFS_DQ_BIGTIME_SLACK	((int64_t)(1ULL << XFS_DQ_BIGTIME_SHIFT) - 1)
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun /* Convert an incore quota expiration timestamp to an ondisk bigtime value. */
xfs_dq_unix_to_bigtime(time64_t unix_seconds)1336*4882a593Smuzhiyun static inline uint32_t xfs_dq_unix_to_bigtime(time64_t unix_seconds)
1337*4882a593Smuzhiyun {
1338*4882a593Smuzhiyun 	/*
1339*4882a593Smuzhiyun 	 * Round the expiration timestamp up to the nearest bigtime timestamp
1340*4882a593Smuzhiyun 	 * that we can store, to give users the most time to fix problems.
1341*4882a593Smuzhiyun 	 */
1342*4882a593Smuzhiyun 	return ((uint64_t)unix_seconds + XFS_DQ_BIGTIME_SLACK) >>
1343*4882a593Smuzhiyun 			XFS_DQ_BIGTIME_SHIFT;
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun /* Convert an ondisk bigtime quota expiration value to an incore timestamp. */
xfs_dq_bigtime_to_unix(uint32_t ondisk_seconds)1347*4882a593Smuzhiyun static inline time64_t xfs_dq_bigtime_to_unix(uint32_t ondisk_seconds)
1348*4882a593Smuzhiyun {
1349*4882a593Smuzhiyun 	return (time64_t)ondisk_seconds << XFS_DQ_BIGTIME_SHIFT;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun /*
1353*4882a593Smuzhiyun  * Default quota grace periods, ranging from zero (use the compiled defaults)
1354*4882a593Smuzhiyun  * to ~136 years.  These are applied to a non-root dquot that has exceeded
1355*4882a593Smuzhiyun  * either limit.
1356*4882a593Smuzhiyun  */
1357*4882a593Smuzhiyun #define XFS_DQ_GRACE_MIN		((int64_t)0)
1358*4882a593Smuzhiyun #define XFS_DQ_GRACE_MAX		((int64_t)U32_MAX)
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun /*
1361*4882a593Smuzhiyun  * This is the main portion of the on-disk representation of quota information
1362*4882a593Smuzhiyun  * for a user.  We pad this with some more expansion room to construct the on
1363*4882a593Smuzhiyun  * disk structure.
1364*4882a593Smuzhiyun  */
1365*4882a593Smuzhiyun struct xfs_disk_dquot {
1366*4882a593Smuzhiyun 	__be16		d_magic;	/* dquot magic = XFS_DQUOT_MAGIC */
1367*4882a593Smuzhiyun 	__u8		d_version;	/* dquot version */
1368*4882a593Smuzhiyun 	__u8		d_type;		/* XFS_DQTYPE_USER/PROJ/GROUP */
1369*4882a593Smuzhiyun 	__be32		d_id;		/* user,project,group id */
1370*4882a593Smuzhiyun 	__be64		d_blk_hardlimit;/* absolute limit on disk blks */
1371*4882a593Smuzhiyun 	__be64		d_blk_softlimit;/* preferred limit on disk blks */
1372*4882a593Smuzhiyun 	__be64		d_ino_hardlimit;/* maximum # allocated inodes */
1373*4882a593Smuzhiyun 	__be64		d_ino_softlimit;/* preferred inode limit */
1374*4882a593Smuzhiyun 	__be64		d_bcount;	/* disk blocks owned by the user */
1375*4882a593Smuzhiyun 	__be64		d_icount;	/* inodes owned by the user */
1376*4882a593Smuzhiyun 	__be32		d_itimer;	/* zero if within inode limits if not,
1377*4882a593Smuzhiyun 					   this is when we refuse service */
1378*4882a593Smuzhiyun 	__be32		d_btimer;	/* similar to above; for disk blocks */
1379*4882a593Smuzhiyun 	__be16		d_iwarns;	/* warnings issued wrt num inodes */
1380*4882a593Smuzhiyun 	__be16		d_bwarns;	/* warnings issued wrt disk blocks */
1381*4882a593Smuzhiyun 	__be32		d_pad0;		/* 64 bit align */
1382*4882a593Smuzhiyun 	__be64		d_rtb_hardlimit;/* absolute limit on realtime blks */
1383*4882a593Smuzhiyun 	__be64		d_rtb_softlimit;/* preferred limit on RT disk blks */
1384*4882a593Smuzhiyun 	__be64		d_rtbcount;	/* realtime blocks owned */
1385*4882a593Smuzhiyun 	__be32		d_rtbtimer;	/* similar to above; for RT disk blocks */
1386*4882a593Smuzhiyun 	__be16		d_rtbwarns;	/* warnings issued wrt RT disk blocks */
1387*4882a593Smuzhiyun 	__be16		d_pad;
1388*4882a593Smuzhiyun };
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun /*
1391*4882a593Smuzhiyun  * This is what goes on disk. This is separated from the xfs_disk_dquot because
1392*4882a593Smuzhiyun  * carrying the unnecessary padding would be a waste of memory.
1393*4882a593Smuzhiyun  */
1394*4882a593Smuzhiyun typedef struct xfs_dqblk {
1395*4882a593Smuzhiyun 	struct xfs_disk_dquot	dd_diskdq; /* portion living incore as well */
1396*4882a593Smuzhiyun 	char			dd_fill[4];/* filling for posterity */
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	/*
1399*4882a593Smuzhiyun 	 * These two are only present on filesystems with the CRC bits set.
1400*4882a593Smuzhiyun 	 */
1401*4882a593Smuzhiyun 	__be32		  dd_crc;	/* checksum */
1402*4882a593Smuzhiyun 	__be64		  dd_lsn;	/* last modification in log */
1403*4882a593Smuzhiyun 	uuid_t		  dd_uuid;	/* location information */
1404*4882a593Smuzhiyun } xfs_dqblk_t;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun #define XFS_DQUOT_CRC_OFF	offsetof(struct xfs_dqblk, dd_crc)
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun /*
1409*4882a593Smuzhiyun  * This defines the unit of allocation of dquots.
1410*4882a593Smuzhiyun  *
1411*4882a593Smuzhiyun  * Currently, it is just one file system block, and a 4K blk contains 30
1412*4882a593Smuzhiyun  * (136 * 30 = 4080) dquots. It's probably not worth trying to make
1413*4882a593Smuzhiyun  * this more dynamic.
1414*4882a593Smuzhiyun  *
1415*4882a593Smuzhiyun  * However, if this number is changed, we have to make sure that we don't
1416*4882a593Smuzhiyun  * implicitly assume that we do allocations in chunks of a single filesystem
1417*4882a593Smuzhiyun  * block in the dquot/xqm code.
1418*4882a593Smuzhiyun  *
1419*4882a593Smuzhiyun  * This is part of the ondisk format because the structure size is not a power
1420*4882a593Smuzhiyun  * of two, which leaves slack at the end of the disk block.
1421*4882a593Smuzhiyun  */
1422*4882a593Smuzhiyun #define XFS_DQUOT_CLUSTER_SIZE_FSB	(xfs_filblks_t)1
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun /*
1425*4882a593Smuzhiyun  * Remote symlink format and access functions.
1426*4882a593Smuzhiyun  */
1427*4882a593Smuzhiyun #define XFS_SYMLINK_MAGIC	0x58534c4d	/* XSLM */
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun struct xfs_dsymlink_hdr {
1430*4882a593Smuzhiyun 	__be32	sl_magic;
1431*4882a593Smuzhiyun 	__be32	sl_offset;
1432*4882a593Smuzhiyun 	__be32	sl_bytes;
1433*4882a593Smuzhiyun 	__be32	sl_crc;
1434*4882a593Smuzhiyun 	uuid_t	sl_uuid;
1435*4882a593Smuzhiyun 	__be64	sl_owner;
1436*4882a593Smuzhiyun 	__be64	sl_blkno;
1437*4882a593Smuzhiyun 	__be64	sl_lsn;
1438*4882a593Smuzhiyun };
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun #define XFS_SYMLINK_CRC_OFF	offsetof(struct xfs_dsymlink_hdr, sl_crc)
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun #define XFS_SYMLINK_MAXLEN	1024
1443*4882a593Smuzhiyun /*
1444*4882a593Smuzhiyun  * The maximum pathlen is 1024 bytes. Since the minimum file system
1445*4882a593Smuzhiyun  * blocksize is 512 bytes, we can get a max of 3 extents back from
1446*4882a593Smuzhiyun  * bmapi when crc headers are taken into account.
1447*4882a593Smuzhiyun  */
1448*4882a593Smuzhiyun #define XFS_SYMLINK_MAPS 3
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun #define XFS_SYMLINK_BUF_SPACE(mp, bufsize)	\
1451*4882a593Smuzhiyun 	((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
1452*4882a593Smuzhiyun 			sizeof(struct xfs_dsymlink_hdr) : 0))
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun /*
1456*4882a593Smuzhiyun  * Allocation Btree format definitions
1457*4882a593Smuzhiyun  *
1458*4882a593Smuzhiyun  * There are two on-disk btrees, one sorted by blockno and one sorted
1459*4882a593Smuzhiyun  * by blockcount and blockno.  All blocks look the same to make the code
1460*4882a593Smuzhiyun  * simpler; if we have time later, we'll make the optimizations.
1461*4882a593Smuzhiyun  */
1462*4882a593Smuzhiyun #define	XFS_ABTB_MAGIC		0x41425442	/* 'ABTB' for bno tree */
1463*4882a593Smuzhiyun #define	XFS_ABTB_CRC_MAGIC	0x41423342	/* 'AB3B' */
1464*4882a593Smuzhiyun #define	XFS_ABTC_MAGIC		0x41425443	/* 'ABTC' for cnt tree */
1465*4882a593Smuzhiyun #define	XFS_ABTC_CRC_MAGIC	0x41423343	/* 'AB3C' */
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun /*
1468*4882a593Smuzhiyun  * Data record/key structure
1469*4882a593Smuzhiyun  */
1470*4882a593Smuzhiyun typedef struct xfs_alloc_rec {
1471*4882a593Smuzhiyun 	__be32		ar_startblock;	/* starting block number */
1472*4882a593Smuzhiyun 	__be32		ar_blockcount;	/* count of free blocks */
1473*4882a593Smuzhiyun } xfs_alloc_rec_t, xfs_alloc_key_t;
1474*4882a593Smuzhiyun 
1475*4882a593Smuzhiyun typedef struct xfs_alloc_rec_incore {
1476*4882a593Smuzhiyun 	xfs_agblock_t	ar_startblock;	/* starting block number */
1477*4882a593Smuzhiyun 	xfs_extlen_t	ar_blockcount;	/* count of free blocks */
1478*4882a593Smuzhiyun } xfs_alloc_rec_incore_t;
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun /* btree pointer type */
1481*4882a593Smuzhiyun typedef __be32 xfs_alloc_ptr_t;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun /*
1484*4882a593Smuzhiyun  * Block numbers in the AG:
1485*4882a593Smuzhiyun  * SB is sector 0, AGF is sector 1, AGI is sector 2, AGFL is sector 3.
1486*4882a593Smuzhiyun  */
1487*4882a593Smuzhiyun #define	XFS_BNO_BLOCK(mp)	((xfs_agblock_t)(XFS_AGFL_BLOCK(mp) + 1))
1488*4882a593Smuzhiyun #define	XFS_CNT_BLOCK(mp)	((xfs_agblock_t)(XFS_BNO_BLOCK(mp) + 1))
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun /*
1492*4882a593Smuzhiyun  * Inode Allocation Btree format definitions
1493*4882a593Smuzhiyun  *
1494*4882a593Smuzhiyun  * There is a btree for the inode map per allocation group.
1495*4882a593Smuzhiyun  */
1496*4882a593Smuzhiyun #define	XFS_IBT_MAGIC		0x49414254	/* 'IABT' */
1497*4882a593Smuzhiyun #define	XFS_IBT_CRC_MAGIC	0x49414233	/* 'IAB3' */
1498*4882a593Smuzhiyun #define	XFS_FIBT_MAGIC		0x46494254	/* 'FIBT' */
1499*4882a593Smuzhiyun #define	XFS_FIBT_CRC_MAGIC	0x46494233	/* 'FIB3' */
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun typedef uint64_t	xfs_inofree_t;
1502*4882a593Smuzhiyun #define	XFS_INODES_PER_CHUNK		(NBBY * sizeof(xfs_inofree_t))
1503*4882a593Smuzhiyun #define	XFS_INODES_PER_CHUNK_LOG	(XFS_NBBYLOG + 3)
1504*4882a593Smuzhiyun #define	XFS_INOBT_ALL_FREE		((xfs_inofree_t)-1)
1505*4882a593Smuzhiyun #define	XFS_INOBT_MASK(i)		((xfs_inofree_t)1 << (i))
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun #define XFS_INOBT_HOLEMASK_FULL		0	/* holemask for full chunk */
1508*4882a593Smuzhiyun #define XFS_INOBT_HOLEMASK_BITS		(NBBY * sizeof(uint16_t))
1509*4882a593Smuzhiyun #define XFS_INODES_PER_HOLEMASK_BIT	\
1510*4882a593Smuzhiyun 	(XFS_INODES_PER_CHUNK / (NBBY * sizeof(uint16_t)))
1511*4882a593Smuzhiyun 
xfs_inobt_maskn(int i,int n)1512*4882a593Smuzhiyun static inline xfs_inofree_t xfs_inobt_maskn(int i, int n)
1513*4882a593Smuzhiyun {
1514*4882a593Smuzhiyun 	return ((n >= XFS_INODES_PER_CHUNK ? 0 : XFS_INOBT_MASK(n)) - 1) << i;
1515*4882a593Smuzhiyun }
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun /*
1518*4882a593Smuzhiyun  * The on-disk inode record structure has two formats. The original "full"
1519*4882a593Smuzhiyun  * format uses a 4-byte freecount. The "sparse" format uses a 1-byte freecount
1520*4882a593Smuzhiyun  * and replaces the 3 high-order freecount bytes wth the holemask and inode
1521*4882a593Smuzhiyun  * count.
1522*4882a593Smuzhiyun  *
1523*4882a593Smuzhiyun  * The holemask of the sparse record format allows an inode chunk to have holes
1524*4882a593Smuzhiyun  * that refer to blocks not owned by the inode record. This facilitates inode
1525*4882a593Smuzhiyun  * allocation in the event of severe free space fragmentation.
1526*4882a593Smuzhiyun  */
1527*4882a593Smuzhiyun typedef struct xfs_inobt_rec {
1528*4882a593Smuzhiyun 	__be32		ir_startino;	/* starting inode number */
1529*4882a593Smuzhiyun 	union {
1530*4882a593Smuzhiyun 		struct {
1531*4882a593Smuzhiyun 			__be32	ir_freecount;	/* count of free inodes */
1532*4882a593Smuzhiyun 		} f;
1533*4882a593Smuzhiyun 		struct {
1534*4882a593Smuzhiyun 			__be16	ir_holemask;/* hole mask for sparse chunks */
1535*4882a593Smuzhiyun 			__u8	ir_count;	/* total inode count */
1536*4882a593Smuzhiyun 			__u8	ir_freecount;	/* count of free inodes */
1537*4882a593Smuzhiyun 		} sp;
1538*4882a593Smuzhiyun 	} ir_u;
1539*4882a593Smuzhiyun 	__be64		ir_free;	/* free inode mask */
1540*4882a593Smuzhiyun } xfs_inobt_rec_t;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun typedef struct xfs_inobt_rec_incore {
1543*4882a593Smuzhiyun 	xfs_agino_t	ir_startino;	/* starting inode number */
1544*4882a593Smuzhiyun 	uint16_t	ir_holemask;	/* hole mask for sparse chunks */
1545*4882a593Smuzhiyun 	uint8_t		ir_count;	/* total inode count */
1546*4882a593Smuzhiyun 	uint8_t		ir_freecount;	/* count of free inodes (set bits) */
1547*4882a593Smuzhiyun 	xfs_inofree_t	ir_free;	/* free inode mask */
1548*4882a593Smuzhiyun } xfs_inobt_rec_incore_t;
1549*4882a593Smuzhiyun 
xfs_inobt_issparse(uint16_t holemask)1550*4882a593Smuzhiyun static inline bool xfs_inobt_issparse(uint16_t holemask)
1551*4882a593Smuzhiyun {
1552*4882a593Smuzhiyun 	/* non-zero holemask represents a sparse rec. */
1553*4882a593Smuzhiyun 	return holemask;
1554*4882a593Smuzhiyun }
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun /*
1557*4882a593Smuzhiyun  * Key structure
1558*4882a593Smuzhiyun  */
1559*4882a593Smuzhiyun typedef struct xfs_inobt_key {
1560*4882a593Smuzhiyun 	__be32		ir_startino;	/* starting inode number */
1561*4882a593Smuzhiyun } xfs_inobt_key_t;
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun /* btree pointer type */
1564*4882a593Smuzhiyun typedef __be32 xfs_inobt_ptr_t;
1565*4882a593Smuzhiyun 
1566*4882a593Smuzhiyun /*
1567*4882a593Smuzhiyun  * block numbers in the AG.
1568*4882a593Smuzhiyun  */
1569*4882a593Smuzhiyun #define	XFS_IBT_BLOCK(mp)		((xfs_agblock_t)(XFS_CNT_BLOCK(mp) + 1))
1570*4882a593Smuzhiyun #define	XFS_FIBT_BLOCK(mp)		((xfs_agblock_t)(XFS_IBT_BLOCK(mp) + 1))
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun /*
1573*4882a593Smuzhiyun  * Reverse mapping btree format definitions
1574*4882a593Smuzhiyun  *
1575*4882a593Smuzhiyun  * There is a btree for the reverse map per allocation group
1576*4882a593Smuzhiyun  */
1577*4882a593Smuzhiyun #define	XFS_RMAP_CRC_MAGIC	0x524d4233	/* 'RMB3' */
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun /*
1580*4882a593Smuzhiyun  * Ownership info for an extent.  This is used to create reverse-mapping
1581*4882a593Smuzhiyun  * entries.
1582*4882a593Smuzhiyun  */
1583*4882a593Smuzhiyun #define XFS_OWNER_INFO_ATTR_FORK	(1 << 0)
1584*4882a593Smuzhiyun #define XFS_OWNER_INFO_BMBT_BLOCK	(1 << 1)
1585*4882a593Smuzhiyun struct xfs_owner_info {
1586*4882a593Smuzhiyun 	uint64_t		oi_owner;
1587*4882a593Smuzhiyun 	xfs_fileoff_t		oi_offset;
1588*4882a593Smuzhiyun 	unsigned int		oi_flags;
1589*4882a593Smuzhiyun };
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun /*
1592*4882a593Smuzhiyun  * Special owner types.
1593*4882a593Smuzhiyun  *
1594*4882a593Smuzhiyun  * Seeing as we only support up to 8EB, we have the upper bit of the owner field
1595*4882a593Smuzhiyun  * to tell us we have a special owner value. We use these for static metadata
1596*4882a593Smuzhiyun  * allocated at mkfs/growfs time, as well as for freespace management metadata.
1597*4882a593Smuzhiyun  */
1598*4882a593Smuzhiyun #define XFS_RMAP_OWN_NULL	(-1ULL)	/* No owner, for growfs */
1599*4882a593Smuzhiyun #define XFS_RMAP_OWN_UNKNOWN	(-2ULL)	/* Unknown owner, for EFI recovery */
1600*4882a593Smuzhiyun #define XFS_RMAP_OWN_FS		(-3ULL)	/* static fs metadata */
1601*4882a593Smuzhiyun #define XFS_RMAP_OWN_LOG	(-4ULL)	/* static fs metadata */
1602*4882a593Smuzhiyun #define XFS_RMAP_OWN_AG		(-5ULL)	/* AG freespace btree blocks */
1603*4882a593Smuzhiyun #define XFS_RMAP_OWN_INOBT	(-6ULL)	/* Inode btree blocks */
1604*4882a593Smuzhiyun #define XFS_RMAP_OWN_INODES	(-7ULL)	/* Inode chunk */
1605*4882a593Smuzhiyun #define XFS_RMAP_OWN_REFC	(-8ULL) /* refcount tree */
1606*4882a593Smuzhiyun #define XFS_RMAP_OWN_COW	(-9ULL) /* cow allocations */
1607*4882a593Smuzhiyun #define XFS_RMAP_OWN_MIN	(-10ULL) /* guard */
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun #define XFS_RMAP_NON_INODE_OWNER(owner)	(!!((owner) & (1ULL << 63)))
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun /*
1612*4882a593Smuzhiyun  * Data record structure
1613*4882a593Smuzhiyun  */
1614*4882a593Smuzhiyun struct xfs_rmap_rec {
1615*4882a593Smuzhiyun 	__be32		rm_startblock;	/* extent start block */
1616*4882a593Smuzhiyun 	__be32		rm_blockcount;	/* extent length */
1617*4882a593Smuzhiyun 	__be64		rm_owner;	/* extent owner */
1618*4882a593Smuzhiyun 	__be64		rm_offset;	/* offset within the owner */
1619*4882a593Smuzhiyun };
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun /*
1622*4882a593Smuzhiyun  * rmap btree record
1623*4882a593Smuzhiyun  *  rm_offset:63 is the attribute fork flag
1624*4882a593Smuzhiyun  *  rm_offset:62 is the bmbt block flag
1625*4882a593Smuzhiyun  *  rm_offset:61 is the unwritten extent flag (same as l0:63 in bmbt)
1626*4882a593Smuzhiyun  *  rm_offset:54-60 aren't used and should be zero
1627*4882a593Smuzhiyun  *  rm_offset:0-53 is the block offset within the inode
1628*4882a593Smuzhiyun  */
1629*4882a593Smuzhiyun #define XFS_RMAP_OFF_ATTR_FORK	((uint64_t)1ULL << 63)
1630*4882a593Smuzhiyun #define XFS_RMAP_OFF_BMBT_BLOCK	((uint64_t)1ULL << 62)
1631*4882a593Smuzhiyun #define XFS_RMAP_OFF_UNWRITTEN	((uint64_t)1ULL << 61)
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun #define XFS_RMAP_LEN_MAX	((uint32_t)~0U)
1634*4882a593Smuzhiyun #define XFS_RMAP_OFF_FLAGS	(XFS_RMAP_OFF_ATTR_FORK | \
1635*4882a593Smuzhiyun 				 XFS_RMAP_OFF_BMBT_BLOCK | \
1636*4882a593Smuzhiyun 				 XFS_RMAP_OFF_UNWRITTEN)
1637*4882a593Smuzhiyun #define XFS_RMAP_OFF_MASK	((uint64_t)0x3FFFFFFFFFFFFFULL)
1638*4882a593Smuzhiyun 
1639*4882a593Smuzhiyun #define XFS_RMAP_OFF(off)		((off) & XFS_RMAP_OFF_MASK)
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun #define XFS_RMAP_IS_BMBT_BLOCK(off)	(!!((off) & XFS_RMAP_OFF_BMBT_BLOCK))
1642*4882a593Smuzhiyun #define XFS_RMAP_IS_ATTR_FORK(off)	(!!((off) & XFS_RMAP_OFF_ATTR_FORK))
1643*4882a593Smuzhiyun #define XFS_RMAP_IS_UNWRITTEN(len)	(!!((off) & XFS_RMAP_OFF_UNWRITTEN))
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun #define RMAPBT_STARTBLOCK_BITLEN	32
1646*4882a593Smuzhiyun #define RMAPBT_BLOCKCOUNT_BITLEN	32
1647*4882a593Smuzhiyun #define RMAPBT_OWNER_BITLEN		64
1648*4882a593Smuzhiyun #define RMAPBT_ATTRFLAG_BITLEN		1
1649*4882a593Smuzhiyun #define RMAPBT_BMBTFLAG_BITLEN		1
1650*4882a593Smuzhiyun #define RMAPBT_EXNTFLAG_BITLEN		1
1651*4882a593Smuzhiyun #define RMAPBT_UNUSED_OFFSET_BITLEN	7
1652*4882a593Smuzhiyun #define RMAPBT_OFFSET_BITLEN		54
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun #define XFS_RMAP_ATTR_FORK		(1 << 0)
1655*4882a593Smuzhiyun #define XFS_RMAP_BMBT_BLOCK		(1 << 1)
1656*4882a593Smuzhiyun #define XFS_RMAP_UNWRITTEN		(1 << 2)
1657*4882a593Smuzhiyun #define XFS_RMAP_KEY_FLAGS		(XFS_RMAP_ATTR_FORK | \
1658*4882a593Smuzhiyun 					 XFS_RMAP_BMBT_BLOCK)
1659*4882a593Smuzhiyun #define XFS_RMAP_REC_FLAGS		(XFS_RMAP_UNWRITTEN)
1660*4882a593Smuzhiyun struct xfs_rmap_irec {
1661*4882a593Smuzhiyun 	xfs_agblock_t	rm_startblock;	/* extent start block */
1662*4882a593Smuzhiyun 	xfs_extlen_t	rm_blockcount;	/* extent length */
1663*4882a593Smuzhiyun 	uint64_t	rm_owner;	/* extent owner */
1664*4882a593Smuzhiyun 	uint64_t	rm_offset;	/* offset within the owner */
1665*4882a593Smuzhiyun 	unsigned int	rm_flags;	/* state flags */
1666*4882a593Smuzhiyun };
1667*4882a593Smuzhiyun 
1668*4882a593Smuzhiyun /*
1669*4882a593Smuzhiyun  * Key structure
1670*4882a593Smuzhiyun  *
1671*4882a593Smuzhiyun  * We don't use the length for lookups
1672*4882a593Smuzhiyun  */
1673*4882a593Smuzhiyun struct xfs_rmap_key {
1674*4882a593Smuzhiyun 	__be32		rm_startblock;	/* extent start block */
1675*4882a593Smuzhiyun 	__be64		rm_owner;	/* extent owner */
1676*4882a593Smuzhiyun 	__be64		rm_offset;	/* offset within the owner */
1677*4882a593Smuzhiyun } __attribute__((packed));
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun /* btree pointer type */
1680*4882a593Smuzhiyun typedef __be32 xfs_rmap_ptr_t;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun #define	XFS_RMAP_BLOCK(mp) \
1683*4882a593Smuzhiyun 	(xfs_sb_version_hasfinobt(&((mp)->m_sb)) ? \
1684*4882a593Smuzhiyun 	 XFS_FIBT_BLOCK(mp) + 1 : \
1685*4882a593Smuzhiyun 	 XFS_IBT_BLOCK(mp) + 1)
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun /*
1688*4882a593Smuzhiyun  * Reference Count Btree format definitions
1689*4882a593Smuzhiyun  *
1690*4882a593Smuzhiyun  */
1691*4882a593Smuzhiyun #define	XFS_REFC_CRC_MAGIC	0x52334643	/* 'R3FC' */
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun unsigned int xfs_refc_block(struct xfs_mount *mp);
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun /*
1696*4882a593Smuzhiyun  * Data record/key structure
1697*4882a593Smuzhiyun  *
1698*4882a593Smuzhiyun  * Each record associates a range of physical blocks (starting at
1699*4882a593Smuzhiyun  * rc_startblock and ending rc_blockcount blocks later) with a reference
1700*4882a593Smuzhiyun  * count (rc_refcount).  Extents that are being used to stage a copy on
1701*4882a593Smuzhiyun  * write (CoW) operation are recorded in the refcount btree with a
1702*4882a593Smuzhiyun  * refcount of 1.  All other records must have a refcount > 1 and must
1703*4882a593Smuzhiyun  * track an extent mapped only by file data forks.
1704*4882a593Smuzhiyun  *
1705*4882a593Smuzhiyun  * Extents with a single owner (attributes, metadata, non-shared file
1706*4882a593Smuzhiyun  * data) are not tracked here.  Free space is also not tracked here.
1707*4882a593Smuzhiyun  * This is consistent with pre-reflink XFS.
1708*4882a593Smuzhiyun  */
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun /*
1711*4882a593Smuzhiyun  * Extents that are being used to stage a copy on write are stored
1712*4882a593Smuzhiyun  * in the refcount btree with a refcount of 1 and the upper bit set
1713*4882a593Smuzhiyun  * on the startblock.  This speeds up mount time deletion of stale
1714*4882a593Smuzhiyun  * staging extents because they're all at the right side of the tree.
1715*4882a593Smuzhiyun  */
1716*4882a593Smuzhiyun #define XFS_REFC_COW_START		((xfs_agblock_t)(1U << 31))
1717*4882a593Smuzhiyun #define REFCNTBT_COWFLAG_BITLEN		1
1718*4882a593Smuzhiyun #define REFCNTBT_AGBLOCK_BITLEN		31
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun struct xfs_refcount_rec {
1721*4882a593Smuzhiyun 	__be32		rc_startblock;	/* starting block number */
1722*4882a593Smuzhiyun 	__be32		rc_blockcount;	/* count of blocks */
1723*4882a593Smuzhiyun 	__be32		rc_refcount;	/* number of inodes linked here */
1724*4882a593Smuzhiyun };
1725*4882a593Smuzhiyun 
1726*4882a593Smuzhiyun struct xfs_refcount_key {
1727*4882a593Smuzhiyun 	__be32		rc_startblock;	/* starting block number */
1728*4882a593Smuzhiyun };
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun struct xfs_refcount_irec {
1731*4882a593Smuzhiyun 	xfs_agblock_t	rc_startblock;	/* starting block number */
1732*4882a593Smuzhiyun 	xfs_extlen_t	rc_blockcount;	/* count of free blocks */
1733*4882a593Smuzhiyun 	xfs_nlink_t	rc_refcount;	/* number of inodes linked here */
1734*4882a593Smuzhiyun };
1735*4882a593Smuzhiyun 
1736*4882a593Smuzhiyun #define MAXREFCOUNT	((xfs_nlink_t)~0U)
1737*4882a593Smuzhiyun #define MAXREFCEXTLEN	((xfs_extlen_t)~0U)
1738*4882a593Smuzhiyun 
1739*4882a593Smuzhiyun /* btree pointer type */
1740*4882a593Smuzhiyun typedef __be32 xfs_refcount_ptr_t;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun /*
1744*4882a593Smuzhiyun  * BMAP Btree format definitions
1745*4882a593Smuzhiyun  *
1746*4882a593Smuzhiyun  * This includes both the root block definition that sits inside an inode fork
1747*4882a593Smuzhiyun  * and the record/pointer formats for the leaf/node in the blocks.
1748*4882a593Smuzhiyun  */
1749*4882a593Smuzhiyun #define XFS_BMAP_MAGIC		0x424d4150	/* 'BMAP' */
1750*4882a593Smuzhiyun #define XFS_BMAP_CRC_MAGIC	0x424d4133	/* 'BMA3' */
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun /*
1753*4882a593Smuzhiyun  * Bmap root header, on-disk form only.
1754*4882a593Smuzhiyun  */
1755*4882a593Smuzhiyun typedef struct xfs_bmdr_block {
1756*4882a593Smuzhiyun 	__be16		bb_level;	/* 0 is a leaf */
1757*4882a593Smuzhiyun 	__be16		bb_numrecs;	/* current # of data records */
1758*4882a593Smuzhiyun } xfs_bmdr_block_t;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun /*
1761*4882a593Smuzhiyun  * Bmap btree record and extent descriptor.
1762*4882a593Smuzhiyun  *  l0:63 is an extent flag (value 1 indicates non-normal).
1763*4882a593Smuzhiyun  *  l0:9-62 are startoff.
1764*4882a593Smuzhiyun  *  l0:0-8 and l1:21-63 are startblock.
1765*4882a593Smuzhiyun  *  l1:0-20 are blockcount.
1766*4882a593Smuzhiyun  */
1767*4882a593Smuzhiyun #define BMBT_EXNTFLAG_BITLEN	1
1768*4882a593Smuzhiyun #define BMBT_STARTOFF_BITLEN	54
1769*4882a593Smuzhiyun #define BMBT_STARTBLOCK_BITLEN	52
1770*4882a593Smuzhiyun #define BMBT_BLOCKCOUNT_BITLEN	21
1771*4882a593Smuzhiyun 
1772*4882a593Smuzhiyun #define BMBT_STARTOFF_MASK	((1ULL << BMBT_STARTOFF_BITLEN) - 1)
1773*4882a593Smuzhiyun #define BMBT_BLOCKCOUNT_MASK	((1ULL << BMBT_BLOCKCOUNT_BITLEN) - 1)
1774*4882a593Smuzhiyun 
1775*4882a593Smuzhiyun /*
1776*4882a593Smuzhiyun  * bmbt records have a file offset (block) field that is 54 bits wide, so this
1777*4882a593Smuzhiyun  * is the largest xfs_fileoff_t that we ever expect to see.
1778*4882a593Smuzhiyun  */
1779*4882a593Smuzhiyun #define XFS_MAX_FILEOFF		(BMBT_STARTOFF_MASK + BMBT_BLOCKCOUNT_MASK)
1780*4882a593Smuzhiyun 
1781*4882a593Smuzhiyun typedef struct xfs_bmbt_rec {
1782*4882a593Smuzhiyun 	__be64			l0, l1;
1783*4882a593Smuzhiyun } xfs_bmbt_rec_t;
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun typedef uint64_t	xfs_bmbt_rec_base_t;	/* use this for casts */
1786*4882a593Smuzhiyun typedef xfs_bmbt_rec_t xfs_bmdr_rec_t;
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun /*
1789*4882a593Smuzhiyun  * Values and macros for delayed-allocation startblock fields.
1790*4882a593Smuzhiyun  */
1791*4882a593Smuzhiyun #define STARTBLOCKVALBITS	17
1792*4882a593Smuzhiyun #define STARTBLOCKMASKBITS	(15 + 20)
1793*4882a593Smuzhiyun #define STARTBLOCKMASK		\
1794*4882a593Smuzhiyun 	(((((xfs_fsblock_t)1) << STARTBLOCKMASKBITS) - 1) << STARTBLOCKVALBITS)
1795*4882a593Smuzhiyun 
isnullstartblock(xfs_fsblock_t x)1796*4882a593Smuzhiyun static inline int isnullstartblock(xfs_fsblock_t x)
1797*4882a593Smuzhiyun {
1798*4882a593Smuzhiyun 	return ((x) & STARTBLOCKMASK) == STARTBLOCKMASK;
1799*4882a593Smuzhiyun }
1800*4882a593Smuzhiyun 
nullstartblock(int k)1801*4882a593Smuzhiyun static inline xfs_fsblock_t nullstartblock(int k)
1802*4882a593Smuzhiyun {
1803*4882a593Smuzhiyun 	ASSERT(k < (1 << STARTBLOCKVALBITS));
1804*4882a593Smuzhiyun 	return STARTBLOCKMASK | (k);
1805*4882a593Smuzhiyun }
1806*4882a593Smuzhiyun 
startblockval(xfs_fsblock_t x)1807*4882a593Smuzhiyun static inline xfs_filblks_t startblockval(xfs_fsblock_t x)
1808*4882a593Smuzhiyun {
1809*4882a593Smuzhiyun 	return (xfs_filblks_t)((x) & ~STARTBLOCKMASK);
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun /*
1813*4882a593Smuzhiyun  * Key structure for non-leaf levels of the tree.
1814*4882a593Smuzhiyun  */
1815*4882a593Smuzhiyun typedef struct xfs_bmbt_key {
1816*4882a593Smuzhiyun 	__be64		br_startoff;	/* starting file offset */
1817*4882a593Smuzhiyun } xfs_bmbt_key_t, xfs_bmdr_key_t;
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun /* btree pointer type */
1820*4882a593Smuzhiyun typedef __be64 xfs_bmbt_ptr_t, xfs_bmdr_ptr_t;
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 
1823*4882a593Smuzhiyun /*
1824*4882a593Smuzhiyun  * Generic Btree block format definitions
1825*4882a593Smuzhiyun  *
1826*4882a593Smuzhiyun  * This is a combination of the actual format used on disk for short and long
1827*4882a593Smuzhiyun  * format btrees.  The first three fields are shared by both format, but the
1828*4882a593Smuzhiyun  * pointers are different and should be used with care.
1829*4882a593Smuzhiyun  *
1830*4882a593Smuzhiyun  * To get the size of the actual short or long form headers please use the size
1831*4882a593Smuzhiyun  * macros below.  Never use sizeof(xfs_btree_block).
1832*4882a593Smuzhiyun  *
1833*4882a593Smuzhiyun  * The blkno, crc, lsn, owner and uuid fields are only available in filesystems
1834*4882a593Smuzhiyun  * with the crc feature bit, and all accesses to them must be conditional on
1835*4882a593Smuzhiyun  * that flag.
1836*4882a593Smuzhiyun  */
1837*4882a593Smuzhiyun /* short form block header */
1838*4882a593Smuzhiyun struct xfs_btree_block_shdr {
1839*4882a593Smuzhiyun 	__be32		bb_leftsib;
1840*4882a593Smuzhiyun 	__be32		bb_rightsib;
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 	__be64		bb_blkno;
1843*4882a593Smuzhiyun 	__be64		bb_lsn;
1844*4882a593Smuzhiyun 	uuid_t		bb_uuid;
1845*4882a593Smuzhiyun 	__be32		bb_owner;
1846*4882a593Smuzhiyun 	__le32		bb_crc;
1847*4882a593Smuzhiyun };
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun /* long form block header */
1850*4882a593Smuzhiyun struct xfs_btree_block_lhdr {
1851*4882a593Smuzhiyun 	__be64		bb_leftsib;
1852*4882a593Smuzhiyun 	__be64		bb_rightsib;
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	__be64		bb_blkno;
1855*4882a593Smuzhiyun 	__be64		bb_lsn;
1856*4882a593Smuzhiyun 	uuid_t		bb_uuid;
1857*4882a593Smuzhiyun 	__be64		bb_owner;
1858*4882a593Smuzhiyun 	__le32		bb_crc;
1859*4882a593Smuzhiyun 	__be32		bb_pad; /* padding for alignment */
1860*4882a593Smuzhiyun };
1861*4882a593Smuzhiyun 
1862*4882a593Smuzhiyun struct xfs_btree_block {
1863*4882a593Smuzhiyun 	__be32		bb_magic;	/* magic number for block type */
1864*4882a593Smuzhiyun 	__be16		bb_level;	/* 0 is a leaf */
1865*4882a593Smuzhiyun 	__be16		bb_numrecs;	/* current # of data records */
1866*4882a593Smuzhiyun 	union {
1867*4882a593Smuzhiyun 		struct xfs_btree_block_shdr s;
1868*4882a593Smuzhiyun 		struct xfs_btree_block_lhdr l;
1869*4882a593Smuzhiyun 	} bb_u;				/* rest */
1870*4882a593Smuzhiyun };
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun /* size of a short form block */
1873*4882a593Smuzhiyun #define XFS_BTREE_SBLOCK_LEN \
1874*4882a593Smuzhiyun 	(offsetof(struct xfs_btree_block, bb_u) + \
1875*4882a593Smuzhiyun 	 offsetof(struct xfs_btree_block_shdr, bb_blkno))
1876*4882a593Smuzhiyun /* size of a long form block */
1877*4882a593Smuzhiyun #define XFS_BTREE_LBLOCK_LEN \
1878*4882a593Smuzhiyun 	(offsetof(struct xfs_btree_block, bb_u) + \
1879*4882a593Smuzhiyun 	 offsetof(struct xfs_btree_block_lhdr, bb_blkno))
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun /* sizes of CRC enabled btree blocks */
1882*4882a593Smuzhiyun #define XFS_BTREE_SBLOCK_CRC_LEN \
1883*4882a593Smuzhiyun 	(offsetof(struct xfs_btree_block, bb_u) + \
1884*4882a593Smuzhiyun 	 sizeof(struct xfs_btree_block_shdr))
1885*4882a593Smuzhiyun #define XFS_BTREE_LBLOCK_CRC_LEN \
1886*4882a593Smuzhiyun 	(offsetof(struct xfs_btree_block, bb_u) + \
1887*4882a593Smuzhiyun 	 sizeof(struct xfs_btree_block_lhdr))
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun #define XFS_BTREE_SBLOCK_CRC_OFF \
1890*4882a593Smuzhiyun 	offsetof(struct xfs_btree_block, bb_u.s.bb_crc)
1891*4882a593Smuzhiyun #define XFS_BTREE_LBLOCK_CRC_OFF \
1892*4882a593Smuzhiyun 	offsetof(struct xfs_btree_block, bb_u.l.bb_crc)
1893*4882a593Smuzhiyun 
1894*4882a593Smuzhiyun /*
1895*4882a593Smuzhiyun  * On-disk XFS access control list structure.
1896*4882a593Smuzhiyun  */
1897*4882a593Smuzhiyun struct xfs_acl_entry {
1898*4882a593Smuzhiyun 	__be32	ae_tag;
1899*4882a593Smuzhiyun 	__be32	ae_id;
1900*4882a593Smuzhiyun 	__be16	ae_perm;
1901*4882a593Smuzhiyun 	__be16	ae_pad;		/* fill the implicit hole in the structure */
1902*4882a593Smuzhiyun };
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun struct xfs_acl {
1905*4882a593Smuzhiyun 	__be32			acl_cnt;
1906*4882a593Smuzhiyun 	struct xfs_acl_entry	acl_entry[];
1907*4882a593Smuzhiyun };
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun /*
1910*4882a593Smuzhiyun  * The number of ACL entries allowed is defined by the on-disk format.
1911*4882a593Smuzhiyun  * For v4 superblocks, that is limited to 25 entries. For v5 superblocks, it is
1912*4882a593Smuzhiyun  * limited only by the maximum size of the xattr that stores the information.
1913*4882a593Smuzhiyun  */
1914*4882a593Smuzhiyun #define XFS_ACL_MAX_ENTRIES(mp)	\
1915*4882a593Smuzhiyun 	(xfs_sb_version_hascrc(&mp->m_sb) \
1916*4882a593Smuzhiyun 		?  (XFS_XATTR_SIZE_MAX - sizeof(struct xfs_acl)) / \
1917*4882a593Smuzhiyun 						sizeof(struct xfs_acl_entry) \
1918*4882a593Smuzhiyun 		: 25)
1919*4882a593Smuzhiyun 
1920*4882a593Smuzhiyun #define XFS_ACL_SIZE(cnt) \
1921*4882a593Smuzhiyun 	(sizeof(struct xfs_acl) + \
1922*4882a593Smuzhiyun 		sizeof(struct xfs_acl_entry) * cnt)
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun #define XFS_ACL_MAX_SIZE(mp) \
1925*4882a593Smuzhiyun 	XFS_ACL_SIZE(XFS_ACL_MAX_ENTRIES((mp)))
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun /* On-disk XFS extended attribute names */
1929*4882a593Smuzhiyun #define SGI_ACL_FILE		"SGI_ACL_FILE"
1930*4882a593Smuzhiyun #define SGI_ACL_DEFAULT		"SGI_ACL_DEFAULT"
1931*4882a593Smuzhiyun #define SGI_ACL_FILE_SIZE	(sizeof(SGI_ACL_FILE)-1)
1932*4882a593Smuzhiyun #define SGI_ACL_DEFAULT_SIZE	(sizeof(SGI_ACL_DEFAULT)-1)
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun #endif /* __XFS_FORMAT_H__ */
1935