xref: /OK3568_Linux_fs/kernel/fs/xfs/libxfs/xfs_da_format.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * Copyright (c) 2013 Red Hat, Inc.
5*4882a593Smuzhiyun  * All Rights Reserved.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #ifndef __XFS_DA_FORMAT_H__
8*4882a593Smuzhiyun #define __XFS_DA_FORMAT_H__
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun  * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * It is used to manage a doubly linked list of all blocks at the same
14*4882a593Smuzhiyun  * level in the Btree, and to identify which type of block this is.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #define XFS_DA_NODE_MAGIC	0xfebe	/* magic number: non-leaf blocks */
17*4882a593Smuzhiyun #define XFS_ATTR_LEAF_MAGIC	0xfbee	/* magic number: attribute leaf blks */
18*4882a593Smuzhiyun #define XFS_DIR2_LEAF1_MAGIC	0xd2f1	/* magic number: v2 dirlf single blks */
19*4882a593Smuzhiyun #define XFS_DIR2_LEAFN_MAGIC	0xd2ff	/* magic number: v2 dirlf multi blks */
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun typedef struct xfs_da_blkinfo {
22*4882a593Smuzhiyun 	__be32		forw;			/* previous block in list */
23*4882a593Smuzhiyun 	__be32		back;			/* following block in list */
24*4882a593Smuzhiyun 	__be16		magic;			/* validity check on block */
25*4882a593Smuzhiyun 	__be16		pad;			/* unused */
26*4882a593Smuzhiyun } xfs_da_blkinfo_t;
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * CRC enabled directory structure types
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  * The headers change size for the additional verification information, but
32*4882a593Smuzhiyun  * otherwise the tree layouts and contents are unchanged. Hence the da btree
33*4882a593Smuzhiyun  * code can use the struct xfs_da_blkinfo for manipulating the tree links and
34*4882a593Smuzhiyun  * magic numbers without modification for both v2 and v3 nodes.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun #define XFS_DA3_NODE_MAGIC	0x3ebe	/* magic number: non-leaf blocks */
37*4882a593Smuzhiyun #define XFS_ATTR3_LEAF_MAGIC	0x3bee	/* magic number: attribute leaf blks */
38*4882a593Smuzhiyun #define XFS_DIR3_LEAF1_MAGIC	0x3df1	/* magic number: v3 dirlf single blks */
39*4882a593Smuzhiyun #define XFS_DIR3_LEAFN_MAGIC	0x3dff	/* magic number: v3 dirlf multi blks */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun struct xfs_da3_blkinfo {
42*4882a593Smuzhiyun 	/*
43*4882a593Smuzhiyun 	 * the node link manipulation code relies on the fact that the first
44*4882a593Smuzhiyun 	 * element of this structure is the struct xfs_da_blkinfo so it can
45*4882a593Smuzhiyun 	 * ignore the differences in the rest of the structures.
46*4882a593Smuzhiyun 	 */
47*4882a593Smuzhiyun 	struct xfs_da_blkinfo	hdr;
48*4882a593Smuzhiyun 	__be32			crc;	/* CRC of block */
49*4882a593Smuzhiyun 	__be64			blkno;	/* first block of the buffer */
50*4882a593Smuzhiyun 	__be64			lsn;	/* sequence number of last write */
51*4882a593Smuzhiyun 	uuid_t			uuid;	/* filesystem we belong to */
52*4882a593Smuzhiyun 	__be64			owner;	/* inode that owns the block */
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * This is the structure of the root and intermediate nodes in the Btree.
57*4882a593Smuzhiyun  * The leaf nodes are defined above.
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Entries are not packed.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Since we have duplicate keys, use a binary search but always follow
62*4882a593Smuzhiyun  * all match in the block, not just the first match found.
63*4882a593Smuzhiyun  */
64*4882a593Smuzhiyun #define XFS_DA_NODE_MAXDEPTH	5	/* max depth of Btree */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun typedef struct xfs_da_node_hdr {
67*4882a593Smuzhiyun 	struct xfs_da_blkinfo	info;	/* block type, links, etc. */
68*4882a593Smuzhiyun 	__be16			__count; /* count of active entries */
69*4882a593Smuzhiyun 	__be16			__level; /* level above leaves (leaf == 0) */
70*4882a593Smuzhiyun } xfs_da_node_hdr_t;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun struct xfs_da3_node_hdr {
73*4882a593Smuzhiyun 	struct xfs_da3_blkinfo	info;	/* block type, links, etc. */
74*4882a593Smuzhiyun 	__be16			__count; /* count of active entries */
75*4882a593Smuzhiyun 	__be16			__level; /* level above leaves (leaf == 0) */
76*4882a593Smuzhiyun 	__be32			__pad32;
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #define XFS_DA3_NODE_CRC_OFF	(offsetof(struct xfs_da3_node_hdr, info.crc))
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun typedef struct xfs_da_node_entry {
82*4882a593Smuzhiyun 	__be32	hashval;	/* hash value for this descendant */
83*4882a593Smuzhiyun 	__be32	before;		/* Btree block before this key */
84*4882a593Smuzhiyun } xfs_da_node_entry_t;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun typedef struct xfs_da_intnode {
87*4882a593Smuzhiyun 	struct xfs_da_node_hdr	hdr;
88*4882a593Smuzhiyun 	struct xfs_da_node_entry __btree[];
89*4882a593Smuzhiyun } xfs_da_intnode_t;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun struct xfs_da3_intnode {
92*4882a593Smuzhiyun 	struct xfs_da3_node_hdr	hdr;
93*4882a593Smuzhiyun 	struct xfs_da_node_entry __btree[];
94*4882a593Smuzhiyun };
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /*
97*4882a593Smuzhiyun  * Directory version 2.
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * There are 4 possible formats:
100*4882a593Smuzhiyun  *  - shortform - embedded into the inode
101*4882a593Smuzhiyun  *  - single block - data with embedded leaf at the end
102*4882a593Smuzhiyun  *  - multiple data blocks, single leaf+freeindex block
103*4882a593Smuzhiyun  *  - data blocks, node and leaf blocks (btree), freeindex blocks
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * Note: many node blocks structures and constants are shared with the attr
106*4882a593Smuzhiyun  * code and defined in xfs_da_btree.h.
107*4882a593Smuzhiyun  */
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun #define	XFS_DIR2_BLOCK_MAGIC	0x58443242	/* XD2B: single block dirs */
110*4882a593Smuzhiyun #define	XFS_DIR2_DATA_MAGIC	0x58443244	/* XD2D: multiblock dirs */
111*4882a593Smuzhiyun #define	XFS_DIR2_FREE_MAGIC	0x58443246	/* XD2F: free index blocks */
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /*
114*4882a593Smuzhiyun  * Directory Version 3 With CRCs.
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * The tree formats are the same as for version 2 directories.  The difference
117*4882a593Smuzhiyun  * is in the block header and dirent formats. In many cases the v3 structures
118*4882a593Smuzhiyun  * use v2 definitions as they are no different and this makes code sharing much
119*4882a593Smuzhiyun  * easier.
120*4882a593Smuzhiyun  *
121*4882a593Smuzhiyun  * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
122*4882a593Smuzhiyun  * format is v2 then they switch to the existing v2 code, or the format is v3
123*4882a593Smuzhiyun  * they implement the v3 functionality. This means the existing dir2 is a mix of
124*4882a593Smuzhiyun  * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
125*4882a593Smuzhiyun  * where there is a difference in the formats, otherwise the code is unchanged.
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * Where it is possible, the code decides what to do based on the magic numbers
128*4882a593Smuzhiyun  * in the blocks rather than feature bits in the superblock. This means the code
129*4882a593Smuzhiyun  * is as independent of the external XFS code as possible as doesn't require
130*4882a593Smuzhiyun  * passing struct xfs_mount pointers into places where it isn't really
131*4882a593Smuzhiyun  * necessary.
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * Version 3 includes:
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  *	- a larger block header for CRC and identification purposes and so the
136*4882a593Smuzhiyun  *	offsets of all the structures inside the blocks are different.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  *	- new magic numbers to be able to detect the v2/v3 types on the fly.
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun #define	XFS_DIR3_BLOCK_MAGIC	0x58444233	/* XDB3: single block dirs */
142*4882a593Smuzhiyun #define	XFS_DIR3_DATA_MAGIC	0x58444433	/* XDD3: multiblock dirs */
143*4882a593Smuzhiyun #define	XFS_DIR3_FREE_MAGIC	0x58444633	/* XDF3: free index blocks */
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun  * Dirents in version 3 directories have a file type field. Additions to this
147*4882a593Smuzhiyun  * list are an on-disk format change, requiring feature bits. Valid values
148*4882a593Smuzhiyun  * are as follows:
149*4882a593Smuzhiyun  */
150*4882a593Smuzhiyun #define XFS_DIR3_FT_UNKNOWN		0
151*4882a593Smuzhiyun #define XFS_DIR3_FT_REG_FILE		1
152*4882a593Smuzhiyun #define XFS_DIR3_FT_DIR			2
153*4882a593Smuzhiyun #define XFS_DIR3_FT_CHRDEV		3
154*4882a593Smuzhiyun #define XFS_DIR3_FT_BLKDEV		4
155*4882a593Smuzhiyun #define XFS_DIR3_FT_FIFO		5
156*4882a593Smuzhiyun #define XFS_DIR3_FT_SOCK		6
157*4882a593Smuzhiyun #define XFS_DIR3_FT_SYMLINK		7
158*4882a593Smuzhiyun #define XFS_DIR3_FT_WHT			8
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun #define XFS_DIR3_FT_MAX			9
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun  * Byte offset in data block and shortform entry.
164*4882a593Smuzhiyun  */
165*4882a593Smuzhiyun typedef uint16_t	xfs_dir2_data_off_t;
166*4882a593Smuzhiyun #define	NULLDATAOFF	0xffffU
167*4882a593Smuzhiyun typedef uint		xfs_dir2_data_aoff_t;	/* argument form */
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun  * Offset in data space of a data entry.
171*4882a593Smuzhiyun  */
172*4882a593Smuzhiyun typedef uint32_t	xfs_dir2_dataptr_t;
173*4882a593Smuzhiyun #define	XFS_DIR2_MAX_DATAPTR	((xfs_dir2_dataptr_t)0xffffffff)
174*4882a593Smuzhiyun #define	XFS_DIR2_NULL_DATAPTR	((xfs_dir2_dataptr_t)0)
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun  * Byte offset in a directory.
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun typedef	xfs_off_t	xfs_dir2_off_t;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /*
182*4882a593Smuzhiyun  * Directory block number (logical dirblk in file)
183*4882a593Smuzhiyun  */
184*4882a593Smuzhiyun typedef uint32_t	xfs_dir2_db_t;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun #define XFS_INO32_SIZE	4
187*4882a593Smuzhiyun #define XFS_INO64_SIZE	8
188*4882a593Smuzhiyun #define XFS_INO64_DIFF	(XFS_INO64_SIZE - XFS_INO32_SIZE)
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun #define	XFS_DIR2_MAX_SHORT_INUM	((xfs_ino_t)0xffffffffULL)
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun  * Directory layout when stored internal to an inode.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * Small directories are packed as tightly as possible so as to fit into the
196*4882a593Smuzhiyun  * literal area of the inode.  These "shortform" directories consist of a
197*4882a593Smuzhiyun  * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
198*4882a593Smuzhiyun  * structures.  Due the different inode number storage size and the variable
199*4882a593Smuzhiyun  * length name field in the xfs_dir2_sf_entry all these structure are
200*4882a593Smuzhiyun  * variable length, and the accessors in this file should be used to iterate
201*4882a593Smuzhiyun  * over them.
202*4882a593Smuzhiyun  */
203*4882a593Smuzhiyun typedef struct xfs_dir2_sf_hdr {
204*4882a593Smuzhiyun 	uint8_t			count;		/* count of entries */
205*4882a593Smuzhiyun 	uint8_t			i8count;	/* count of 8-byte inode #s */
206*4882a593Smuzhiyun 	uint8_t			parent[8];	/* parent dir inode number */
207*4882a593Smuzhiyun } __packed xfs_dir2_sf_hdr_t;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun typedef struct xfs_dir2_sf_entry {
210*4882a593Smuzhiyun 	__u8			namelen;	/* actual name length */
211*4882a593Smuzhiyun 	__u8			offset[2];	/* saved offset */
212*4882a593Smuzhiyun 	__u8			name[];		/* name, variable size */
213*4882a593Smuzhiyun 	/*
214*4882a593Smuzhiyun 	 * A single byte containing the file type field follows the inode
215*4882a593Smuzhiyun 	 * number for version 3 directory entries.
216*4882a593Smuzhiyun 	 *
217*4882a593Smuzhiyun 	 * A 64-bit or 32-bit inode number follows here, at a variable offset
218*4882a593Smuzhiyun 	 * after the name.
219*4882a593Smuzhiyun 	 */
220*4882a593Smuzhiyun } __packed xfs_dir2_sf_entry_t;
221*4882a593Smuzhiyun 
xfs_dir2_sf_hdr_size(int i8count)222*4882a593Smuzhiyun static inline int xfs_dir2_sf_hdr_size(int i8count)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	return sizeof(struct xfs_dir2_sf_hdr) -
225*4882a593Smuzhiyun 		(i8count == 0) * XFS_INO64_DIFF;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun static inline xfs_dir2_data_aoff_t
xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t * sfep)229*4882a593Smuzhiyun xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	return get_unaligned_be16(sfep->offset);
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun static inline void
xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t * sfep,xfs_dir2_data_aoff_t off)235*4882a593Smuzhiyun xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	put_unaligned_be16(off, sfep->offset);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun static inline struct xfs_dir2_sf_entry *
xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr * hdr)241*4882a593Smuzhiyun xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	return (struct xfs_dir2_sf_entry *)
244*4882a593Smuzhiyun 		((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun  * Data block structures.
249*4882a593Smuzhiyun  *
250*4882a593Smuzhiyun  * A pure data block looks like the following drawing on disk:
251*4882a593Smuzhiyun  *
252*4882a593Smuzhiyun  *    +-------------------------------------------------+
253*4882a593Smuzhiyun  *    | xfs_dir2_data_hdr_t                             |
254*4882a593Smuzhiyun  *    +-------------------------------------------------+
255*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
256*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
257*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
258*4882a593Smuzhiyun  *    | ...                                             |
259*4882a593Smuzhiyun  *    +-------------------------------------------------+
260*4882a593Smuzhiyun  *    | unused space                                    |
261*4882a593Smuzhiyun  *    +-------------------------------------------------+
262*4882a593Smuzhiyun  *
263*4882a593Smuzhiyun  * As all the entries are variable size structures the accessors below should
264*4882a593Smuzhiyun  * be used to iterate over them.
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  * In addition to the pure data blocks for the data and node formats,
267*4882a593Smuzhiyun  * most structures are also used for the combined data/freespace "block"
268*4882a593Smuzhiyun  * format below.
269*4882a593Smuzhiyun  */
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun #define	XFS_DIR2_DATA_ALIGN_LOG	3		/* i.e., 8 bytes */
272*4882a593Smuzhiyun #define	XFS_DIR2_DATA_ALIGN	(1 << XFS_DIR2_DATA_ALIGN_LOG)
273*4882a593Smuzhiyun #define	XFS_DIR2_DATA_FREE_TAG	0xffff
274*4882a593Smuzhiyun #define	XFS_DIR2_DATA_FD_COUNT	3
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun  * Directory address space divided into sections,
278*4882a593Smuzhiyun  * spaces separated by 32GB.
279*4882a593Smuzhiyun  */
280*4882a593Smuzhiyun #define	XFS_DIR2_SPACE_SIZE	(1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
281*4882a593Smuzhiyun #define	XFS_DIR2_DATA_SPACE	0
282*4882a593Smuzhiyun #define	XFS_DIR2_DATA_OFFSET	(XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /*
285*4882a593Smuzhiyun  * Describe a free area in the data block.
286*4882a593Smuzhiyun  *
287*4882a593Smuzhiyun  * The freespace will be formatted as a xfs_dir2_data_unused_t.
288*4882a593Smuzhiyun  */
289*4882a593Smuzhiyun typedef struct xfs_dir2_data_free {
290*4882a593Smuzhiyun 	__be16			offset;		/* start of freespace */
291*4882a593Smuzhiyun 	__be16			length;		/* length of freespace */
292*4882a593Smuzhiyun } xfs_dir2_data_free_t;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun  * Header for the data blocks.
296*4882a593Smuzhiyun  *
297*4882a593Smuzhiyun  * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
298*4882a593Smuzhiyun  */
299*4882a593Smuzhiyun typedef struct xfs_dir2_data_hdr {
300*4882a593Smuzhiyun 	__be32			magic;		/* XFS_DIR2_DATA_MAGIC or */
301*4882a593Smuzhiyun 						/* XFS_DIR2_BLOCK_MAGIC */
302*4882a593Smuzhiyun 	xfs_dir2_data_free_t	bestfree[XFS_DIR2_DATA_FD_COUNT];
303*4882a593Smuzhiyun } xfs_dir2_data_hdr_t;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * define a structure for all the verification fields we are adding to the
307*4882a593Smuzhiyun  * directory block structures. This will be used in several structures.
308*4882a593Smuzhiyun  * The magic number must be the first entry to align with all the dir2
309*4882a593Smuzhiyun  * structures so we determine how to decode them just by the magic number.
310*4882a593Smuzhiyun  */
311*4882a593Smuzhiyun struct xfs_dir3_blk_hdr {
312*4882a593Smuzhiyun 	__be32			magic;	/* magic number */
313*4882a593Smuzhiyun 	__be32			crc;	/* CRC of block */
314*4882a593Smuzhiyun 	__be64			blkno;	/* first block of the buffer */
315*4882a593Smuzhiyun 	__be64			lsn;	/* sequence number of last write */
316*4882a593Smuzhiyun 	uuid_t			uuid;	/* filesystem we belong to */
317*4882a593Smuzhiyun 	__be64			owner;	/* inode that owns the block */
318*4882a593Smuzhiyun };
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun struct xfs_dir3_data_hdr {
321*4882a593Smuzhiyun 	struct xfs_dir3_blk_hdr	hdr;
322*4882a593Smuzhiyun 	xfs_dir2_data_free_t	best_free[XFS_DIR2_DATA_FD_COUNT];
323*4882a593Smuzhiyun 	__be32			pad;	/* 64 bit alignment */
324*4882a593Smuzhiyun };
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun #define XFS_DIR3_DATA_CRC_OFF  offsetof(struct xfs_dir3_data_hdr, hdr.crc)
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun /*
329*4882a593Smuzhiyun  * Active entry in a data block.
330*4882a593Smuzhiyun  *
331*4882a593Smuzhiyun  * Aligned to 8 bytes.  After the variable length name field there is a
332*4882a593Smuzhiyun  * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
333*4882a593Smuzhiyun  *
334*4882a593Smuzhiyun  * For dir3 structures, there is file type field between the name and the tag.
335*4882a593Smuzhiyun  * This can only be manipulated by helper functions. It is packed hard against
336*4882a593Smuzhiyun  * the end of the name so any padding for rounding is between the file type and
337*4882a593Smuzhiyun  * the tag.
338*4882a593Smuzhiyun  */
339*4882a593Smuzhiyun typedef struct xfs_dir2_data_entry {
340*4882a593Smuzhiyun 	__be64			inumber;	/* inode number */
341*4882a593Smuzhiyun 	__u8			namelen;	/* name length */
342*4882a593Smuzhiyun 	__u8			name[];		/* name bytes, no null */
343*4882a593Smuzhiyun      /* __u8			filetype; */	/* type of inode we point to */
344*4882a593Smuzhiyun      /*	__be16                  tag; */		/* starting offset of us */
345*4882a593Smuzhiyun } xfs_dir2_data_entry_t;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun  * Unused entry in a data block.
349*4882a593Smuzhiyun  *
350*4882a593Smuzhiyun  * Aligned to 8 bytes.  Tag appears as the last 2 bytes and must be accessed
351*4882a593Smuzhiyun  * using xfs_dir2_data_unused_tag_p.
352*4882a593Smuzhiyun  */
353*4882a593Smuzhiyun typedef struct xfs_dir2_data_unused {
354*4882a593Smuzhiyun 	__be16			freetag;	/* XFS_DIR2_DATA_FREE_TAG */
355*4882a593Smuzhiyun 	__be16			length;		/* total free length */
356*4882a593Smuzhiyun 						/* variable offset */
357*4882a593Smuzhiyun 	__be16			tag;		/* starting offset of us */
358*4882a593Smuzhiyun } xfs_dir2_data_unused_t;
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun  * Pointer to a freespace's tag word.
362*4882a593Smuzhiyun  */
363*4882a593Smuzhiyun static inline __be16 *
xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused * dup)364*4882a593Smuzhiyun xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	return (__be16 *)((char *)dup +
367*4882a593Smuzhiyun 			be16_to_cpu(dup->length) - sizeof(__be16));
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun  * Leaf block structures.
372*4882a593Smuzhiyun  *
373*4882a593Smuzhiyun  * A pure leaf block looks like the following drawing on disk:
374*4882a593Smuzhiyun  *
375*4882a593Smuzhiyun  *    +---------------------------+
376*4882a593Smuzhiyun  *    | xfs_dir2_leaf_hdr_t       |
377*4882a593Smuzhiyun  *    +---------------------------+
378*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t     |
379*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t     |
380*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t     |
381*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t     |
382*4882a593Smuzhiyun  *    | ...                       |
383*4882a593Smuzhiyun  *    +---------------------------+
384*4882a593Smuzhiyun  *    | xfs_dir2_data_off_t       |
385*4882a593Smuzhiyun  *    | xfs_dir2_data_off_t       |
386*4882a593Smuzhiyun  *    | xfs_dir2_data_off_t       |
387*4882a593Smuzhiyun  *    | ...                       |
388*4882a593Smuzhiyun  *    +---------------------------+
389*4882a593Smuzhiyun  *    | xfs_dir2_leaf_tail_t      |
390*4882a593Smuzhiyun  *    +---------------------------+
391*4882a593Smuzhiyun  *
392*4882a593Smuzhiyun  * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
393*4882a593Smuzhiyun  * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
394*4882a593Smuzhiyun  * for directories with separate leaf nodes and free space blocks
395*4882a593Smuzhiyun  * (magic = XFS_DIR2_LEAFN_MAGIC).
396*4882a593Smuzhiyun  *
397*4882a593Smuzhiyun  * As all the entries are variable size structures the accessors below should
398*4882a593Smuzhiyun  * be used to iterate over them.
399*4882a593Smuzhiyun  */
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun /*
402*4882a593Smuzhiyun  * Offset of the leaf/node space.  First block in this space
403*4882a593Smuzhiyun  * is the btree root.
404*4882a593Smuzhiyun  */
405*4882a593Smuzhiyun #define	XFS_DIR2_LEAF_SPACE	1
406*4882a593Smuzhiyun #define	XFS_DIR2_LEAF_OFFSET	(XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun /*
409*4882a593Smuzhiyun  * Leaf block header.
410*4882a593Smuzhiyun  */
411*4882a593Smuzhiyun typedef struct xfs_dir2_leaf_hdr {
412*4882a593Smuzhiyun 	xfs_da_blkinfo_t	info;		/* header for da routines */
413*4882a593Smuzhiyun 	__be16			count;		/* count of entries */
414*4882a593Smuzhiyun 	__be16			stale;		/* count of stale entries */
415*4882a593Smuzhiyun } xfs_dir2_leaf_hdr_t;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun struct xfs_dir3_leaf_hdr {
418*4882a593Smuzhiyun 	struct xfs_da3_blkinfo	info;		/* header for da routines */
419*4882a593Smuzhiyun 	__be16			count;		/* count of entries */
420*4882a593Smuzhiyun 	__be16			stale;		/* count of stale entries */
421*4882a593Smuzhiyun 	__be32			pad;		/* 64 bit alignment */
422*4882a593Smuzhiyun };
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun /*
425*4882a593Smuzhiyun  * Leaf block entry.
426*4882a593Smuzhiyun  */
427*4882a593Smuzhiyun typedef struct xfs_dir2_leaf_entry {
428*4882a593Smuzhiyun 	__be32			hashval;	/* hash value of name */
429*4882a593Smuzhiyun 	__be32			address;	/* address of data entry */
430*4882a593Smuzhiyun } xfs_dir2_leaf_entry_t;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun /*
433*4882a593Smuzhiyun  * Leaf block tail.
434*4882a593Smuzhiyun  */
435*4882a593Smuzhiyun typedef struct xfs_dir2_leaf_tail {
436*4882a593Smuzhiyun 	__be32			bestcount;
437*4882a593Smuzhiyun } xfs_dir2_leaf_tail_t;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /*
440*4882a593Smuzhiyun  * Leaf block.
441*4882a593Smuzhiyun  */
442*4882a593Smuzhiyun typedef struct xfs_dir2_leaf {
443*4882a593Smuzhiyun 	xfs_dir2_leaf_hdr_t	hdr;			/* leaf header */
444*4882a593Smuzhiyun 	xfs_dir2_leaf_entry_t	__ents[];		/* entries */
445*4882a593Smuzhiyun } xfs_dir2_leaf_t;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun struct xfs_dir3_leaf {
448*4882a593Smuzhiyun 	struct xfs_dir3_leaf_hdr	hdr;		/* leaf header */
449*4882a593Smuzhiyun 	struct xfs_dir2_leaf_entry	__ents[];	/* entries */
450*4882a593Smuzhiyun };
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun #define XFS_DIR3_LEAF_CRC_OFF  offsetof(struct xfs_dir3_leaf_hdr, info.crc)
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun /*
455*4882a593Smuzhiyun  * Get address of the bests array in the single-leaf block.
456*4882a593Smuzhiyun  */
457*4882a593Smuzhiyun static inline __be16 *
xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail * ltp)458*4882a593Smuzhiyun xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun /*
464*4882a593Smuzhiyun  * Free space block definitions for the node format.
465*4882a593Smuzhiyun  */
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun  * Offset of the freespace index.
469*4882a593Smuzhiyun  */
470*4882a593Smuzhiyun #define	XFS_DIR2_FREE_SPACE	2
471*4882a593Smuzhiyun #define	XFS_DIR2_FREE_OFFSET	(XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun typedef	struct xfs_dir2_free_hdr {
474*4882a593Smuzhiyun 	__be32			magic;		/* XFS_DIR2_FREE_MAGIC */
475*4882a593Smuzhiyun 	__be32			firstdb;	/* db of first entry */
476*4882a593Smuzhiyun 	__be32			nvalid;		/* count of valid entries */
477*4882a593Smuzhiyun 	__be32			nused;		/* count of used entries */
478*4882a593Smuzhiyun } xfs_dir2_free_hdr_t;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun typedef struct xfs_dir2_free {
481*4882a593Smuzhiyun 	xfs_dir2_free_hdr_t	hdr;		/* block header */
482*4882a593Smuzhiyun 	__be16			bests[];	/* best free counts */
483*4882a593Smuzhiyun 						/* unused entries are -1 */
484*4882a593Smuzhiyun } xfs_dir2_free_t;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun struct xfs_dir3_free_hdr {
487*4882a593Smuzhiyun 	struct xfs_dir3_blk_hdr	hdr;
488*4882a593Smuzhiyun 	__be32			firstdb;	/* db of first entry */
489*4882a593Smuzhiyun 	__be32			nvalid;		/* count of valid entries */
490*4882a593Smuzhiyun 	__be32			nused;		/* count of used entries */
491*4882a593Smuzhiyun 	__be32			pad;		/* 64 bit alignment */
492*4882a593Smuzhiyun };
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun struct xfs_dir3_free {
495*4882a593Smuzhiyun 	struct xfs_dir3_free_hdr hdr;
496*4882a593Smuzhiyun 	__be16			bests[];	/* best free counts */
497*4882a593Smuzhiyun 						/* unused entries are -1 */
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun #define XFS_DIR3_FREE_CRC_OFF  offsetof(struct xfs_dir3_free, hdr.hdr.crc)
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun /*
503*4882a593Smuzhiyun  * Single block format.
504*4882a593Smuzhiyun  *
505*4882a593Smuzhiyun  * The single block format looks like the following drawing on disk:
506*4882a593Smuzhiyun  *
507*4882a593Smuzhiyun  *    +-------------------------------------------------+
508*4882a593Smuzhiyun  *    | xfs_dir2_data_hdr_t                             |
509*4882a593Smuzhiyun  *    +-------------------------------------------------+
510*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
511*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
512*4882a593Smuzhiyun  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
513*4882a593Smuzhiyun  *    | ...                                             |
514*4882a593Smuzhiyun  *    +-------------------------------------------------+
515*4882a593Smuzhiyun  *    | unused space                                    |
516*4882a593Smuzhiyun  *    +-------------------------------------------------+
517*4882a593Smuzhiyun  *    | ...                                             |
518*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t                           |
519*4882a593Smuzhiyun  *    | xfs_dir2_leaf_entry_t                           |
520*4882a593Smuzhiyun  *    +-------------------------------------------------+
521*4882a593Smuzhiyun  *    | xfs_dir2_block_tail_t                           |
522*4882a593Smuzhiyun  *    +-------------------------------------------------+
523*4882a593Smuzhiyun  *
524*4882a593Smuzhiyun  * As all the entries are variable size structures the accessors below should
525*4882a593Smuzhiyun  * be used to iterate over them.
526*4882a593Smuzhiyun  */
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun typedef struct xfs_dir2_block_tail {
529*4882a593Smuzhiyun 	__be32		count;			/* count of leaf entries */
530*4882a593Smuzhiyun 	__be32		stale;			/* count of stale lf entries */
531*4882a593Smuzhiyun } xfs_dir2_block_tail_t;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun /*
534*4882a593Smuzhiyun  * Pointer to the leaf entries embedded in a data block (1-block format)
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun static inline struct xfs_dir2_leaf_entry *
xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail * btp)537*4882a593Smuzhiyun xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * Attribute storage layout
545*4882a593Smuzhiyun  *
546*4882a593Smuzhiyun  * Attribute lists are structured around Btrees where all the data
547*4882a593Smuzhiyun  * elements are in the leaf nodes.  Attribute names are hashed into an int,
548*4882a593Smuzhiyun  * then that int is used as the index into the Btree.  Since the hashval
549*4882a593Smuzhiyun  * of an attribute name may not be unique, we may have duplicate keys.  The
550*4882a593Smuzhiyun  * internal links in the Btree are logical block offsets into the file.
551*4882a593Smuzhiyun  *
552*4882a593Smuzhiyun  * Struct leaf_entry's are packed from the top.  Name/values grow from the
553*4882a593Smuzhiyun  * bottom but are not packed.  The freemap contains run-length-encoded entries
554*4882a593Smuzhiyun  * for the free bytes after the leaf_entry's, but only the N largest such,
555*4882a593Smuzhiyun  * smaller runs are dropped.  When the freemap doesn't show enough space
556*4882a593Smuzhiyun  * for an allocation, we compact the name/value area and try again.  If we
557*4882a593Smuzhiyun  * still don't have enough space, then we have to split the block.  The
558*4882a593Smuzhiyun  * name/value structs (both local and remote versions) must be 32bit aligned.
559*4882a593Smuzhiyun  *
560*4882a593Smuzhiyun  * Since we have duplicate hash keys, for each key that matches, compare
561*4882a593Smuzhiyun  * the actual name string.  The root and intermediate node search always
562*4882a593Smuzhiyun  * takes the first-in-the-block key match found, so we should only have
563*4882a593Smuzhiyun  * to work "forw"ard.  If none matches, continue with the "forw"ard leaf
564*4882a593Smuzhiyun  * nodes until the hash key changes or the attribute name is found.
565*4882a593Smuzhiyun  *
566*4882a593Smuzhiyun  * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
567*4882a593Smuzhiyun  * the leaf_entry.  The namespaces are independent only because we also look
568*4882a593Smuzhiyun  * at the namespace bit when we are looking for a matching attribute name.
569*4882a593Smuzhiyun  *
570*4882a593Smuzhiyun  * We also store an "incomplete" bit in the leaf_entry.  It shows that an
571*4882a593Smuzhiyun  * attribute is in the middle of being created and should not be shown to
572*4882a593Smuzhiyun  * the user if we crash during the time that the bit is set.  We clear the
573*4882a593Smuzhiyun  * bit when we have finished setting up the attribute.  We do this because
574*4882a593Smuzhiyun  * we cannot create some large attributes inside a single transaction, and we
575*4882a593Smuzhiyun  * need some indication that we weren't finished if we crash in the middle.
576*4882a593Smuzhiyun  */
577*4882a593Smuzhiyun #define XFS_ATTR_LEAF_MAPSIZE	3	/* how many freespace slots */
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun /*
580*4882a593Smuzhiyun  * Entries are packed toward the top as tight as possible.
581*4882a593Smuzhiyun  */
582*4882a593Smuzhiyun struct xfs_attr_shortform {
583*4882a593Smuzhiyun 	struct xfs_attr_sf_hdr {	/* constant-structure header block */
584*4882a593Smuzhiyun 		__be16	totsize;	/* total bytes in shortform list */
585*4882a593Smuzhiyun 		__u8	count;	/* count of active entries */
586*4882a593Smuzhiyun 		__u8	padding;
587*4882a593Smuzhiyun 	} hdr;
588*4882a593Smuzhiyun 	struct xfs_attr_sf_entry {
589*4882a593Smuzhiyun 		uint8_t namelen;	/* actual length of name (no NULL) */
590*4882a593Smuzhiyun 		uint8_t valuelen;	/* actual length of value (no NULL) */
591*4882a593Smuzhiyun 		uint8_t flags;	/* flags bits (see xfs_attr_leaf.h) */
592*4882a593Smuzhiyun 		uint8_t nameval[];	/* name & value bytes concatenated */
593*4882a593Smuzhiyun 	} list[1];			/* variable sized array */
594*4882a593Smuzhiyun };
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun typedef struct xfs_attr_leaf_map {	/* RLE map of free bytes */
597*4882a593Smuzhiyun 	__be16	base;			  /* base of free region */
598*4882a593Smuzhiyun 	__be16	size;			  /* length of free region */
599*4882a593Smuzhiyun } xfs_attr_leaf_map_t;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun typedef struct xfs_attr_leaf_hdr {	/* constant-structure header block */
602*4882a593Smuzhiyun 	xfs_da_blkinfo_t info;		/* block type, links, etc. */
603*4882a593Smuzhiyun 	__be16	count;			/* count of active leaf_entry's */
604*4882a593Smuzhiyun 	__be16	usedbytes;		/* num bytes of names/values stored */
605*4882a593Smuzhiyun 	__be16	firstused;		/* first used byte in name area */
606*4882a593Smuzhiyun 	__u8	holes;			/* != 0 if blk needs compaction */
607*4882a593Smuzhiyun 	__u8	pad1;
608*4882a593Smuzhiyun 	xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
609*4882a593Smuzhiyun 					/* N largest free regions */
610*4882a593Smuzhiyun } xfs_attr_leaf_hdr_t;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun typedef struct xfs_attr_leaf_entry {	/* sorted on key, not name */
613*4882a593Smuzhiyun 	__be32	hashval;		/* hash value of name */
614*4882a593Smuzhiyun 	__be16	nameidx;		/* index into buffer of name/value */
615*4882a593Smuzhiyun 	__u8	flags;			/* LOCAL/ROOT/SECURE/INCOMPLETE flag */
616*4882a593Smuzhiyun 	__u8	pad2;			/* unused pad byte */
617*4882a593Smuzhiyun } xfs_attr_leaf_entry_t;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun typedef struct xfs_attr_leaf_name_local {
620*4882a593Smuzhiyun 	__be16	valuelen;		/* number of bytes in value */
621*4882a593Smuzhiyun 	__u8	namelen;		/* length of name bytes */
622*4882a593Smuzhiyun 	__u8	nameval[1];		/* name/value bytes */
623*4882a593Smuzhiyun } xfs_attr_leaf_name_local_t;
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun typedef struct xfs_attr_leaf_name_remote {
626*4882a593Smuzhiyun 	__be32	valueblk;		/* block number of value bytes */
627*4882a593Smuzhiyun 	__be32	valuelen;		/* number of bytes in value */
628*4882a593Smuzhiyun 	__u8	namelen;		/* length of name bytes */
629*4882a593Smuzhiyun 	__u8	name[1];		/* name bytes */
630*4882a593Smuzhiyun } xfs_attr_leaf_name_remote_t;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun typedef struct xfs_attr_leafblock {
633*4882a593Smuzhiyun 	xfs_attr_leaf_hdr_t	hdr;	/* constant-structure header block */
634*4882a593Smuzhiyun 	xfs_attr_leaf_entry_t	entries[1];	/* sorted on key, not name */
635*4882a593Smuzhiyun 	/*
636*4882a593Smuzhiyun 	 * The rest of the block contains the following structures after the
637*4882a593Smuzhiyun 	 * leaf entries, growing from the bottom up. The variables are never
638*4882a593Smuzhiyun 	 * referenced and definining them can actually make gcc optimize away
639*4882a593Smuzhiyun 	 * accesses to the 'entries' array above index 0 so don't do that.
640*4882a593Smuzhiyun 	 *
641*4882a593Smuzhiyun 	 * xfs_attr_leaf_name_local_t namelist;
642*4882a593Smuzhiyun 	 * xfs_attr_leaf_name_remote_t valuelist;
643*4882a593Smuzhiyun 	 */
644*4882a593Smuzhiyun } xfs_attr_leafblock_t;
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun /*
647*4882a593Smuzhiyun  * CRC enabled leaf structures. Called "version 3" structures to match the
648*4882a593Smuzhiyun  * version number of the directory and dablk structures for this feature, and
649*4882a593Smuzhiyun  * attr2 is already taken by the variable inode attribute fork size feature.
650*4882a593Smuzhiyun  */
651*4882a593Smuzhiyun struct xfs_attr3_leaf_hdr {
652*4882a593Smuzhiyun 	struct xfs_da3_blkinfo	info;
653*4882a593Smuzhiyun 	__be16			count;
654*4882a593Smuzhiyun 	__be16			usedbytes;
655*4882a593Smuzhiyun 	__be16			firstused;
656*4882a593Smuzhiyun 	__u8			holes;
657*4882a593Smuzhiyun 	__u8			pad1;
658*4882a593Smuzhiyun 	struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
659*4882a593Smuzhiyun 	__be32			pad2;		/* 64 bit alignment */
660*4882a593Smuzhiyun };
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun #define XFS_ATTR3_LEAF_CRC_OFF	(offsetof(struct xfs_attr3_leaf_hdr, info.crc))
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun struct xfs_attr3_leafblock {
665*4882a593Smuzhiyun 	struct xfs_attr3_leaf_hdr	hdr;
666*4882a593Smuzhiyun 	struct xfs_attr_leaf_entry	entries[1];
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	/*
669*4882a593Smuzhiyun 	 * The rest of the block contains the following structures after the
670*4882a593Smuzhiyun 	 * leaf entries, growing from the bottom up. The variables are never
671*4882a593Smuzhiyun 	 * referenced, the locations accessed purely from helper functions.
672*4882a593Smuzhiyun 	 *
673*4882a593Smuzhiyun 	 * struct xfs_attr_leaf_name_local
674*4882a593Smuzhiyun 	 * struct xfs_attr_leaf_name_remote
675*4882a593Smuzhiyun 	 */
676*4882a593Smuzhiyun };
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /*
679*4882a593Smuzhiyun  * Special value to represent fs block size in the leaf header firstused field.
680*4882a593Smuzhiyun  * Only used when block size overflows the 2-bytes available on disk.
681*4882a593Smuzhiyun  */
682*4882a593Smuzhiyun #define XFS_ATTR3_LEAF_NULLOFF	0
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun /*
685*4882a593Smuzhiyun  * Flags used in the leaf_entry[i].flags field.
686*4882a593Smuzhiyun  */
687*4882a593Smuzhiyun #define	XFS_ATTR_LOCAL_BIT	0	/* attr is stored locally */
688*4882a593Smuzhiyun #define	XFS_ATTR_ROOT_BIT	1	/* limit access to trusted attrs */
689*4882a593Smuzhiyun #define	XFS_ATTR_SECURE_BIT	2	/* limit access to secure attrs */
690*4882a593Smuzhiyun #define	XFS_ATTR_INCOMPLETE_BIT	7	/* attr in middle of create/delete */
691*4882a593Smuzhiyun #define XFS_ATTR_LOCAL		(1 << XFS_ATTR_LOCAL_BIT)
692*4882a593Smuzhiyun #define XFS_ATTR_ROOT		(1 << XFS_ATTR_ROOT_BIT)
693*4882a593Smuzhiyun #define XFS_ATTR_SECURE		(1 << XFS_ATTR_SECURE_BIT)
694*4882a593Smuzhiyun #define XFS_ATTR_INCOMPLETE	(1 << XFS_ATTR_INCOMPLETE_BIT)
695*4882a593Smuzhiyun #define XFS_ATTR_NSP_ONDISK_MASK	(XFS_ATTR_ROOT | XFS_ATTR_SECURE)
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun /*
698*4882a593Smuzhiyun  * Alignment for namelist and valuelist entries (since they are mixed
699*4882a593Smuzhiyun  * there can be only one alignment value)
700*4882a593Smuzhiyun  */
701*4882a593Smuzhiyun #define	XFS_ATTR_LEAF_NAME_ALIGN	((uint)sizeof(xfs_dablk_t))
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun static inline int
xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock * leafp)704*4882a593Smuzhiyun xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
707*4882a593Smuzhiyun 		return sizeof(struct xfs_attr3_leaf_hdr);
708*4882a593Smuzhiyun 	return sizeof(struct xfs_attr_leaf_hdr);
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun static inline struct xfs_attr_leaf_entry *
xfs_attr3_leaf_entryp(xfs_attr_leafblock_t * leafp)712*4882a593Smuzhiyun xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun 	if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
715*4882a593Smuzhiyun 		return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
716*4882a593Smuzhiyun 	return &leafp->entries[0];
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun /*
720*4882a593Smuzhiyun  * Cast typed pointers for "local" and "remote" name/value structs.
721*4882a593Smuzhiyun  */
722*4882a593Smuzhiyun static inline char *
xfs_attr3_leaf_name(xfs_attr_leafblock_t * leafp,int idx)723*4882a593Smuzhiyun xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun static inline xfs_attr_leaf_name_remote_t *
xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t * leafp,int idx)731*4882a593Smuzhiyun xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
734*4882a593Smuzhiyun }
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun static inline xfs_attr_leaf_name_local_t *
xfs_attr3_leaf_name_local(xfs_attr_leafblock_t * leafp,int idx)737*4882a593Smuzhiyun xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
740*4882a593Smuzhiyun }
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun  * Calculate total bytes used (including trailing pad for alignment) for
744*4882a593Smuzhiyun  * a "local" name/value structure, a "remote" name/value structure, and
745*4882a593Smuzhiyun  * a pointer which might be either.
746*4882a593Smuzhiyun  */
xfs_attr_leaf_entsize_remote(int nlen)747*4882a593Smuzhiyun static inline int xfs_attr_leaf_entsize_remote(int nlen)
748*4882a593Smuzhiyun {
749*4882a593Smuzhiyun 	return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 +
750*4882a593Smuzhiyun 			nlen, XFS_ATTR_LEAF_NAME_ALIGN);
751*4882a593Smuzhiyun }
752*4882a593Smuzhiyun 
xfs_attr_leaf_entsize_local(int nlen,int vlen)753*4882a593Smuzhiyun static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun 	return round_up(sizeof(struct xfs_attr_leaf_name_local) - 1 +
756*4882a593Smuzhiyun 			nlen + vlen, XFS_ATTR_LEAF_NAME_ALIGN);
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun 
xfs_attr_leaf_entsize_local_max(int bsize)759*4882a593Smuzhiyun static inline int xfs_attr_leaf_entsize_local_max(int bsize)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	return (((bsize) >> 1) + ((bsize) >> 2));
762*4882a593Smuzhiyun }
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /*
767*4882a593Smuzhiyun  * Remote attribute block format definition
768*4882a593Smuzhiyun  *
769*4882a593Smuzhiyun  * There is one of these headers per filesystem block in a remote attribute.
770*4882a593Smuzhiyun  * This is done to ensure there is a 1:1 mapping between the attribute value
771*4882a593Smuzhiyun  * length and the number of blocks needed to store the attribute. This makes the
772*4882a593Smuzhiyun  * verification of a buffer a little more complex, but greatly simplifies the
773*4882a593Smuzhiyun  * allocation, reading and writing of these attributes as we don't have to guess
774*4882a593Smuzhiyun  * the number of blocks needed to store the attribute data.
775*4882a593Smuzhiyun  */
776*4882a593Smuzhiyun #define XFS_ATTR3_RMT_MAGIC	0x5841524d	/* XARM */
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun struct xfs_attr3_rmt_hdr {
779*4882a593Smuzhiyun 	__be32	rm_magic;
780*4882a593Smuzhiyun 	__be32	rm_offset;
781*4882a593Smuzhiyun 	__be32	rm_bytes;
782*4882a593Smuzhiyun 	__be32	rm_crc;
783*4882a593Smuzhiyun 	uuid_t	rm_uuid;
784*4882a593Smuzhiyun 	__be64	rm_owner;
785*4882a593Smuzhiyun 	__be64	rm_blkno;
786*4882a593Smuzhiyun 	__be64	rm_lsn;
787*4882a593Smuzhiyun };
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun #define XFS_ATTR3_RMT_CRC_OFF	offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize)	\
792*4882a593Smuzhiyun 	((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
793*4882a593Smuzhiyun 			sizeof(struct xfs_attr3_rmt_hdr) : 0))
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /* Number of bytes in a directory block. */
xfs_dir2_dirblock_bytes(struct xfs_sb * sbp)796*4882a593Smuzhiyun static inline unsigned int xfs_dir2_dirblock_bytes(struct xfs_sb *sbp)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	return 1 << (sbp->sb_blocklog + sbp->sb_dirblklog);
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun xfs_failaddr_t xfs_da3_blkinfo_verify(struct xfs_buf *bp,
802*4882a593Smuzhiyun 				      struct xfs_da3_blkinfo *hdr3);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun #endif /* __XFS_DA_FORMAT_H__ */
805