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