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