]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/xfs/xfs_dir2_format.h
Merge branch 'dice-driver-playback-only' of git://git.alsa-project.org/alsa-kprivate...
[karo-tx-linux.git] / fs / xfs / xfs_dir2_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_DIR2_FORMAT_H__
20 #define __XFS_DIR2_FORMAT_H__
21
22 /*
23  * Directory version 2.
24  *
25  * There are 4 possible formats:
26  *  - shortform - embedded into the inode
27  *  - single block - data with embedded leaf at the end
28  *  - multiple data blocks, single leaf+freeindex block
29  *  - data blocks, node and leaf blocks (btree), freeindex blocks
30  *
31  * Note: many node blocks structures and constants are shared with the attr
32  * code and defined in xfs_da_btree.h.
33  */
34
35 #define XFS_DIR2_BLOCK_MAGIC    0x58443242      /* XD2B: single block dirs */
36 #define XFS_DIR2_DATA_MAGIC     0x58443244      /* XD2D: multiblock dirs */
37 #define XFS_DIR2_FREE_MAGIC     0x58443246      /* XD2F: free index blocks */
38
39 /*
40  * Directory Version 3 With CRCs.
41  *
42  * The tree formats are the same as for version 2 directories.  The difference
43  * is in the block header and dirent formats. In many cases the v3 structures
44  * use v2 definitions as they are no different and this makes code sharing much
45  * easier.
46  *
47  * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
48  * format is v2 then they switch to the existing v2 code, or the format is v3
49  * they implement the v3 functionality. This means the existing dir2 is a mix of
50  * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
51  * where there is a difference in the formats, otherwise the code is unchanged.
52  *
53  * Where it is possible, the code decides what to do based on the magic numbers
54  * in the blocks rather than feature bits in the superblock. This means the code
55  * is as independent of the external XFS code as possible as doesn't require
56  * passing struct xfs_mount pointers into places where it isn't really
57  * necessary.
58  *
59  * Version 3 includes:
60  *
61  *      - a larger block header for CRC and identification purposes and so the
62  *      offsets of all the structures inside the blocks are different.
63  *
64  *      - new magic numbers to be able to detect the v2/v3 types on the fly.
65  */
66
67 #define XFS_DIR3_BLOCK_MAGIC    0x58444233      /* XDB3: single block dirs */
68 #define XFS_DIR3_DATA_MAGIC     0x58444433      /* XDD3: multiblock dirs */
69 #define XFS_DIR3_FREE_MAGIC     0x58444633      /* XDF3: free index blocks */
70
71 /*
72  * Dirents in version 3 directories have a file type field. Additions to this
73  * list are an on-disk format change, requiring feature bits. Valid values
74  * are as follows:
75  */
76 #define XFS_DIR3_FT_UNKNOWN             0
77 #define XFS_DIR3_FT_REG_FILE            1
78 #define XFS_DIR3_FT_DIR                 2
79 #define XFS_DIR3_FT_CHRDEV              3
80 #define XFS_DIR3_FT_BLKDEV              4
81 #define XFS_DIR3_FT_FIFO                5
82 #define XFS_DIR3_FT_SOCK                6
83 #define XFS_DIR3_FT_SYMLINK             7
84 #define XFS_DIR3_FT_WHT                 8
85
86 #define XFS_DIR3_FT_MAX                 9
87
88 /*
89  * Byte offset in data block and shortform entry.
90  */
91 typedef __uint16_t      xfs_dir2_data_off_t;
92 #define NULLDATAOFF     0xffffU
93 typedef uint            xfs_dir2_data_aoff_t;   /* argument form */
94
95 /*
96  * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
97  * Only need 16 bits, this is the byte offset into the single block form.
98  */
99 typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
100
101 /*
102  * Offset in data space of a data entry.
103  */
104 typedef __uint32_t      xfs_dir2_dataptr_t;
105 #define XFS_DIR2_MAX_DATAPTR    ((xfs_dir2_dataptr_t)0xffffffff)
106 #define XFS_DIR2_NULL_DATAPTR   ((xfs_dir2_dataptr_t)0)
107
108 /*
109  * Byte offset in a directory.
110  */
111 typedef xfs_off_t       xfs_dir2_off_t;
112
113 /*
114  * Directory block number (logical dirblk in file)
115  */
116 typedef __uint32_t      xfs_dir2_db_t;
117
118 /*
119  * Inode number stored as 8 8-bit values.
120  */
121 typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
122
123 /*
124  * Inode number stored as 4 8-bit values.
125  * Works a lot of the time, when all the inode numbers in a directory
126  * fit in 32 bits.
127  */
128 typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
129
130 typedef union {
131         xfs_dir2_ino8_t i8;
132         xfs_dir2_ino4_t i4;
133 } xfs_dir2_inou_t;
134 #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
135
136 /*
137  * Directory layout when stored internal to an inode.
138  *
139  * Small directories are packed as tightly as possible so as to fit into the
140  * literal area of the inode.  These "shortform" directories consist of a
141  * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
142  * structures.  Due the different inode number storage size and the variable
143  * length name field in the xfs_dir2_sf_entry all these structure are
144  * variable length, and the accessors in this file should be used to iterate
145  * over them.
146  */
147 typedef struct xfs_dir2_sf_hdr {
148         __uint8_t               count;          /* count of entries */
149         __uint8_t               i8count;        /* count of 8-byte inode #s */
150         xfs_dir2_inou_t         parent;         /* parent dir inode number */
151 } __arch_pack xfs_dir2_sf_hdr_t;
152
153 typedef struct xfs_dir2_sf_entry {
154         __u8                    namelen;        /* actual name length */
155         xfs_dir2_sf_off_t       offset;         /* saved offset */
156         __u8                    name[];         /* name, variable size */
157         /*
158          * A single byte containing the file type field follows the inode
159          * number for version 3 directory entries.
160          *
161          * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
162          * variable offset after the name.
163          */
164 } __arch_pack xfs_dir2_sf_entry_t;
165
166 static inline int xfs_dir2_sf_hdr_size(int i8count)
167 {
168         return sizeof(struct xfs_dir2_sf_hdr) -
169                 (i8count == 0) *
170                 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
171 }
172
173 static inline xfs_dir2_data_aoff_t
174 xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
175 {
176         return get_unaligned_be16(&sfep->offset.i);
177 }
178
179 static inline void
180 xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
181 {
182         put_unaligned_be16(off, &sfep->offset.i);
183 }
184
185 static inline struct xfs_dir2_sf_entry *
186 xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
187 {
188         return (struct xfs_dir2_sf_entry *)
189                 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
190 }
191
192 static inline int
193 xfs_dir3_sf_entsize(
194         struct xfs_mount        *mp,
195         struct xfs_dir2_sf_hdr  *hdr,
196         int                     len)
197 {
198         int count = sizeof(struct xfs_dir2_sf_entry);   /* namelen + offset */
199
200         count += len;                                   /* name */
201         count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) :
202                                 sizeof(xfs_dir2_ino4_t); /* ino # */
203         if (xfs_sb_version_hasftype(&mp->m_sb))
204                 count += sizeof(__uint8_t);             /* file type */
205         return count;
206 }
207
208 static inline struct xfs_dir2_sf_entry *
209 xfs_dir3_sf_nextentry(
210         struct xfs_mount        *mp,
211         struct xfs_dir2_sf_hdr  *hdr,
212         struct xfs_dir2_sf_entry *sfep)
213 {
214         return (struct xfs_dir2_sf_entry *)
215                 ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen));
216 }
217
218 /*
219  * in dir3 shortform directories, the file type field is stored at a variable
220  * offset after the inode number. Because it's only a single byte, endian
221  * conversion is not necessary.
222  */
223 static inline __uint8_t *
224 xfs_dir3_sfe_ftypep(
225         struct xfs_dir2_sf_hdr  *hdr,
226         struct xfs_dir2_sf_entry *sfep)
227 {
228         return (__uint8_t *)&sfep->name[sfep->namelen];
229 }
230
231 static inline __uint8_t
232 xfs_dir3_sfe_get_ftype(
233         struct xfs_mount        *mp,
234         struct xfs_dir2_sf_hdr  *hdr,
235         struct xfs_dir2_sf_entry *sfep)
236 {
237         __uint8_t       *ftp;
238
239         if (!xfs_sb_version_hasftype(&mp->m_sb))
240                 return XFS_DIR3_FT_UNKNOWN;
241
242         ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
243         if (*ftp >= XFS_DIR3_FT_MAX)
244                 return XFS_DIR3_FT_UNKNOWN;
245         return *ftp;
246 }
247
248 static inline void
249 xfs_dir3_sfe_put_ftype(
250         struct xfs_mount        *mp,
251         struct xfs_dir2_sf_hdr  *hdr,
252         struct xfs_dir2_sf_entry *sfep,
253         __uint8_t               ftype)
254 {
255         __uint8_t       *ftp;
256
257         ASSERT(ftype < XFS_DIR3_FT_MAX);
258
259         if (!xfs_sb_version_hasftype(&mp->m_sb))
260                 return;
261         ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
262         *ftp = ftype;
263 }
264
265 /*
266  * Data block structures.
267  *
268  * A pure data block looks like the following drawing on disk:
269  *
270  *    +-------------------------------------------------+
271  *    | xfs_dir2_data_hdr_t                             |
272  *    +-------------------------------------------------+
273  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
274  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
275  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
276  *    | ...                                             |
277  *    +-------------------------------------------------+
278  *    | unused space                                    |
279  *    +-------------------------------------------------+
280  *
281  * As all the entries are variable size structures the accessors below should
282  * be used to iterate over them.
283  *
284  * In addition to the pure data blocks for the data and node formats,
285  * most structures are also used for the combined data/freespace "block"
286  * format below.
287  */
288
289 #define XFS_DIR2_DATA_ALIGN_LOG 3               /* i.e., 8 bytes */
290 #define XFS_DIR2_DATA_ALIGN     (1 << XFS_DIR2_DATA_ALIGN_LOG)
291 #define XFS_DIR2_DATA_FREE_TAG  0xffff
292 #define XFS_DIR2_DATA_FD_COUNT  3
293
294 /*
295  * Directory address space divided into sections,
296  * spaces separated by 32GB.
297  */
298 #define XFS_DIR2_SPACE_SIZE     (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
299 #define XFS_DIR2_DATA_SPACE     0
300 #define XFS_DIR2_DATA_OFFSET    (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
301 #define XFS_DIR2_DATA_FIRSTDB(mp)       \
302         xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
303
304 /*
305  * Describe a free area in the data block.
306  *
307  * The freespace will be formatted as a xfs_dir2_data_unused_t.
308  */
309 typedef struct xfs_dir2_data_free {
310         __be16                  offset;         /* start of freespace */
311         __be16                  length;         /* length of freespace */
312 } xfs_dir2_data_free_t;
313
314 /*
315  * Header for the data blocks.
316  *
317  * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
318  */
319 typedef struct xfs_dir2_data_hdr {
320         __be32                  magic;          /* XFS_DIR2_DATA_MAGIC or */
321                                                 /* XFS_DIR2_BLOCK_MAGIC */
322         xfs_dir2_data_free_t    bestfree[XFS_DIR2_DATA_FD_COUNT];
323 } xfs_dir2_data_hdr_t;
324
325 /*
326  * define a structure for all the verification fields we are adding to the
327  * directory block structures. This will be used in several structures.
328  * The magic number must be the first entry to align with all the dir2
329  * structures so we determine how to decode them just by the magic number.
330  */
331 struct xfs_dir3_blk_hdr {
332         __be32                  magic;  /* magic number */
333         __be32                  crc;    /* CRC of block */
334         __be64                  blkno;  /* first block of the buffer */
335         __be64                  lsn;    /* sequence number of last write */
336         uuid_t                  uuid;   /* filesystem we belong to */
337         __be64                  owner;  /* inode that owns the block */
338 };
339
340 struct xfs_dir3_data_hdr {
341         struct xfs_dir3_blk_hdr hdr;
342         xfs_dir2_data_free_t    best_free[XFS_DIR2_DATA_FD_COUNT];
343         __be32                  pad;    /* 64 bit alignment */
344 };
345
346 #define XFS_DIR3_DATA_CRC_OFF  offsetof(struct xfs_dir3_data_hdr, hdr.crc)
347
348 static inline struct xfs_dir2_data_free *
349 xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
350 {
351         if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
352             hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
353                 struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
354                 return hdr3->best_free;
355         }
356         return hdr->bestfree;
357 }
358
359 /*
360  * Active entry in a data block.
361  *
362  * Aligned to 8 bytes.  After the variable length name field there is a
363  * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
364  *
365  * For dir3 structures, there is file type field between the name and the tag.
366  * This can only be manipulated by helper functions. It is packed hard against
367  * the end of the name so any padding for rounding is between the file type and
368  * the tag.
369  */
370 typedef struct xfs_dir2_data_entry {
371         __be64                  inumber;        /* inode number */
372         __u8                    namelen;        /* name length */
373         __u8                    name[];         /* name bytes, no null */
374      /* __u8                    filetype; */    /* type of inode we point to */
375      /* __be16                  tag; */         /* starting offset of us */
376 } xfs_dir2_data_entry_t;
377
378 /*
379  * Unused entry in a data block.
380  *
381  * Aligned to 8 bytes.  Tag appears as the last 2 bytes and must be accessed
382  * using xfs_dir2_data_unused_tag_p.
383  */
384 typedef struct xfs_dir2_data_unused {
385         __be16                  freetag;        /* XFS_DIR2_DATA_FREE_TAG */
386         __be16                  length;         /* total free length */
387                                                 /* variable offset */
388         __be16                  tag;            /* starting offset of us */
389 } xfs_dir2_data_unused_t;
390
391 /*
392  * Size of a data entry.
393  */
394 static inline int
395 __xfs_dir3_data_entsize(
396         bool    ftype,
397         int     n)
398 {
399         int     size = offsetof(struct xfs_dir2_data_entry, name[0]);
400
401         size += n;
402         size += sizeof(xfs_dir2_data_off_t);
403         if (ftype)
404                 size += sizeof(__uint8_t);
405         return roundup(size, XFS_DIR2_DATA_ALIGN);
406 }
407 static inline int
408 xfs_dir3_data_entsize(
409         struct xfs_mount        *mp,
410         int                     n)
411 {
412         bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false;
413         return __xfs_dir3_data_entsize(ftype, n);
414 }
415
416 static inline __uint8_t
417 xfs_dir3_dirent_get_ftype(
418         struct xfs_mount        *mp,
419         struct xfs_dir2_data_entry *dep)
420 {
421         if (xfs_sb_version_hasftype(&mp->m_sb)) {
422                 __uint8_t       type = dep->name[dep->namelen];
423
424                 ASSERT(type < XFS_DIR3_FT_MAX);
425                 if (type < XFS_DIR3_FT_MAX)
426                         return type;
427
428         }
429         return XFS_DIR3_FT_UNKNOWN;
430 }
431
432 static inline void
433 xfs_dir3_dirent_put_ftype(
434         struct xfs_mount        *mp,
435         struct xfs_dir2_data_entry *dep,
436         __uint8_t               type)
437 {
438         ASSERT(type < XFS_DIR3_FT_MAX);
439         ASSERT(dep->namelen != 0);
440
441         if (xfs_sb_version_hasftype(&mp->m_sb))
442                 dep->name[dep->namelen] = type;
443 }
444
445 /*
446  * Pointer to an entry's tag word.
447  */
448 static inline __be16 *
449 xfs_dir3_data_entry_tag_p(
450         struct xfs_mount        *mp,
451         struct xfs_dir2_data_entry *dep)
452 {
453         return (__be16 *)((char *)dep +
454                 xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16));
455 }
456
457 /*
458  * Pointer to a freespace's tag word.
459  */
460 static inline __be16 *
461 xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
462 {
463         return (__be16 *)((char *)dup +
464                         be16_to_cpu(dup->length) - sizeof(__be16));
465 }
466
467 static inline size_t
468 xfs_dir3_data_hdr_size(bool dir3)
469 {
470         if (dir3)
471                 return sizeof(struct xfs_dir3_data_hdr);
472         return sizeof(struct xfs_dir2_data_hdr);
473 }
474
475 static inline size_t
476 xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
477 {
478         bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
479                     hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
480         return xfs_dir3_data_hdr_size(dir3);
481 }
482
483 static inline struct xfs_dir2_data_entry *
484 xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
485 {
486         return (struct xfs_dir2_data_entry *)
487                 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
488 }
489
490 static inline struct xfs_dir2_data_unused *
491 xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
492 {
493         return (struct xfs_dir2_data_unused *)
494                 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
495 }
496
497 /*
498  * Offsets of . and .. in data space (always block 0)
499  *
500  * The macros are used for shortform directories as they have no headers to read
501  * the magic number out of. Shortform directories need to know the size of the
502  * data block header because the sfe embeds the block offset of the entry into
503  * it so that it doesn't change when format conversion occurs. Bad Things Happen
504  * if we don't follow this rule.
505  *
506  * XXX: there is scope for significant optimisation of the logic here. Right
507  * now we are checking for "dir3 format" over and over again. Ideally we should
508  * only do it once for each operation.
509  */
510 #define XFS_DIR3_DATA_DOT_OFFSET(mp)    \
511         xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&(mp)->m_sb))
512 #define XFS_DIR3_DATA_DOTDOT_OFFSET(mp) \
513         (XFS_DIR3_DATA_DOT_OFFSET(mp) + xfs_dir3_data_entsize(mp, 1))
514 #define XFS_DIR3_DATA_FIRST_OFFSET(mp)          \
515         (XFS_DIR3_DATA_DOTDOT_OFFSET(mp) + xfs_dir3_data_entsize(mp, 2))
516
517 static inline xfs_dir2_data_aoff_t
518 xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
519 {
520         return xfs_dir3_data_entry_offset(hdr);
521 }
522
523 static inline xfs_dir2_data_aoff_t
524 xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
525 {
526         bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
527                     hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
528         return xfs_dir3_data_dot_offset(hdr) +
529                 __xfs_dir3_data_entsize(dir3, 1);
530 }
531
532 static inline xfs_dir2_data_aoff_t
533 xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
534 {
535         bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
536                     hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
537         return xfs_dir3_data_dotdot_offset(hdr) +
538                 __xfs_dir3_data_entsize(dir3, 2);
539 }
540
541 /*
542  * location of . and .. in data space (always block 0)
543  */
544 static inline struct xfs_dir2_data_entry *
545 xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
546 {
547         return (struct xfs_dir2_data_entry *)
548                 ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
549 }
550
551 static inline struct xfs_dir2_data_entry *
552 xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
553 {
554         return (struct xfs_dir2_data_entry *)
555                 ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
556 }
557
558 static inline struct xfs_dir2_data_entry *
559 xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
560 {
561         return (struct xfs_dir2_data_entry *)
562                 ((char *)hdr + xfs_dir3_data_first_offset(hdr));
563 }
564
565 /*
566  * Leaf block structures.
567  *
568  * A pure leaf block looks like the following drawing on disk:
569  *
570  *    +---------------------------+
571  *    | xfs_dir2_leaf_hdr_t       |
572  *    +---------------------------+
573  *    | xfs_dir2_leaf_entry_t     |
574  *    | xfs_dir2_leaf_entry_t     |
575  *    | xfs_dir2_leaf_entry_t     |
576  *    | xfs_dir2_leaf_entry_t     |
577  *    | ...                       |
578  *    +---------------------------+
579  *    | xfs_dir2_data_off_t       |
580  *    | xfs_dir2_data_off_t       |
581  *    | xfs_dir2_data_off_t       |
582  *    | ...                       |
583  *    +---------------------------+
584  *    | xfs_dir2_leaf_tail_t      |
585  *    +---------------------------+
586  *
587  * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
588  * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
589  * for directories with separate leaf nodes and free space blocks
590  * (magic = XFS_DIR2_LEAFN_MAGIC).
591  *
592  * As all the entries are variable size structures the accessors below should
593  * be used to iterate over them.
594  */
595
596 /*
597  * Offset of the leaf/node space.  First block in this space
598  * is the btree root.
599  */
600 #define XFS_DIR2_LEAF_SPACE     1
601 #define XFS_DIR2_LEAF_OFFSET    (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
602 #define XFS_DIR2_LEAF_FIRSTDB(mp)       \
603         xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
604
605 /*
606  * Leaf block header.
607  */
608 typedef struct xfs_dir2_leaf_hdr {
609         xfs_da_blkinfo_t        info;           /* header for da routines */
610         __be16                  count;          /* count of entries */
611         __be16                  stale;          /* count of stale entries */
612 } xfs_dir2_leaf_hdr_t;
613
614 struct xfs_dir3_leaf_hdr {
615         struct xfs_da3_blkinfo  info;           /* header for da routines */
616         __be16                  count;          /* count of entries */
617         __be16                  stale;          /* count of stale entries */
618         __be32                  pad;            /* 64 bit alignment */
619 };
620
621 struct xfs_dir3_icleaf_hdr {
622         __uint32_t              forw;
623         __uint32_t              back;
624         __uint16_t              magic;
625         __uint16_t              count;
626         __uint16_t              stale;
627 };
628
629 /*
630  * Leaf block entry.
631  */
632 typedef struct xfs_dir2_leaf_entry {
633         __be32                  hashval;        /* hash value of name */
634         __be32                  address;        /* address of data entry */
635 } xfs_dir2_leaf_entry_t;
636
637 /*
638  * Leaf block tail.
639  */
640 typedef struct xfs_dir2_leaf_tail {
641         __be32                  bestcount;
642 } xfs_dir2_leaf_tail_t;
643
644 /*
645  * Leaf block.
646  */
647 typedef struct xfs_dir2_leaf {
648         xfs_dir2_leaf_hdr_t     hdr;                    /* leaf header */
649         xfs_dir2_leaf_entry_t   __ents[];               /* entries */
650 } xfs_dir2_leaf_t;
651
652 struct xfs_dir3_leaf {
653         struct xfs_dir3_leaf_hdr        hdr;            /* leaf header */
654         struct xfs_dir2_leaf_entry      __ents[];       /* entries */
655 };
656
657 #define XFS_DIR3_LEAF_CRC_OFF  offsetof(struct xfs_dir3_leaf_hdr, info.crc)
658
659 extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
660                                         struct xfs_dir2_leaf *from);
661
662 static inline int
663 xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
664 {
665         if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
666             lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
667                 return sizeof(struct xfs_dir3_leaf_hdr);
668         return sizeof(struct xfs_dir2_leaf_hdr);
669 }
670
671 static inline int
672 xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
673 {
674         return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
675                 (uint)sizeof(struct xfs_dir2_leaf_entry);
676 }
677
678 /*
679  * Get address of the bestcount field in the single-leaf block.
680  */
681 static inline struct xfs_dir2_leaf_entry *
682 xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
683 {
684         if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
685             lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
686                 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
687                 return lp3->__ents;
688         }
689         return lp->__ents;
690 }
691
692 /*
693  * Get address of the bestcount field in the single-leaf block.
694  */
695 static inline struct xfs_dir2_leaf_tail *
696 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
697 {
698         return (struct xfs_dir2_leaf_tail *)
699                 ((char *)lp + mp->m_dirblksize -
700                   sizeof(struct xfs_dir2_leaf_tail));
701 }
702
703 /*
704  * Get address of the bests array in the single-leaf block.
705  */
706 static inline __be16 *
707 xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
708 {
709         return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
710 }
711
712 /*
713  * DB blocks here are logical directory block numbers, not filesystem blocks.
714  */
715
716 /*
717  * Convert dataptr to byte in file space
718  */
719 static inline xfs_dir2_off_t
720 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
721 {
722         return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
723 }
724
725 /*
726  * Convert byte in file space to dataptr.  It had better be aligned.
727  */
728 static inline xfs_dir2_dataptr_t
729 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
730 {
731         return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
732 }
733
734 /*
735  * Convert byte in space to (DB) block
736  */
737 static inline xfs_dir2_db_t
738 xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
739 {
740         return (xfs_dir2_db_t)
741                 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
742 }
743
744 /*
745  * Convert dataptr to a block number
746  */
747 static inline xfs_dir2_db_t
748 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
749 {
750         return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
751 }
752
753 /*
754  * Convert byte in space to offset in a block
755  */
756 static inline xfs_dir2_data_aoff_t
757 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
758 {
759         return (xfs_dir2_data_aoff_t)(by &
760                 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
761 }
762
763 /*
764  * Convert dataptr to a byte offset in a block
765  */
766 static inline xfs_dir2_data_aoff_t
767 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
768 {
769         return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
770 }
771
772 /*
773  * Convert block and offset to byte in space
774  */
775 static inline xfs_dir2_off_t
776 xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
777                         xfs_dir2_data_aoff_t o)
778 {
779         return ((xfs_dir2_off_t)db <<
780                 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
781 }
782
783 /*
784  * Convert block (DB) to block (dablk)
785  */
786 static inline xfs_dablk_t
787 xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
788 {
789         return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
790 }
791
792 /*
793  * Convert byte in space to (DA) block
794  */
795 static inline xfs_dablk_t
796 xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
797 {
798         return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
799 }
800
801 /*
802  * Convert block and offset to dataptr
803  */
804 static inline xfs_dir2_dataptr_t
805 xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
806                            xfs_dir2_data_aoff_t o)
807 {
808         return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
809 }
810
811 /*
812  * Convert block (dablk) to block (DB)
813  */
814 static inline xfs_dir2_db_t
815 xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
816 {
817         return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
818 }
819
820 /*
821  * Convert block (dablk) to byte offset in space
822  */
823 static inline xfs_dir2_off_t
824 xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
825 {
826         return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
827 }
828
829 /*
830  * Free space block defintions for the node format.
831  */
832
833 /*
834  * Offset of the freespace index.
835  */
836 #define XFS_DIR2_FREE_SPACE     2
837 #define XFS_DIR2_FREE_OFFSET    (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
838 #define XFS_DIR2_FREE_FIRSTDB(mp)       \
839         xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
840
841 typedef struct xfs_dir2_free_hdr {
842         __be32                  magic;          /* XFS_DIR2_FREE_MAGIC */
843         __be32                  firstdb;        /* db of first entry */
844         __be32                  nvalid;         /* count of valid entries */
845         __be32                  nused;          /* count of used entries */
846 } xfs_dir2_free_hdr_t;
847
848 typedef struct xfs_dir2_free {
849         xfs_dir2_free_hdr_t     hdr;            /* block header */
850         __be16                  bests[];        /* best free counts */
851                                                 /* unused entries are -1 */
852 } xfs_dir2_free_t;
853
854 struct xfs_dir3_free_hdr {
855         struct xfs_dir3_blk_hdr hdr;
856         __be32                  firstdb;        /* db of first entry */
857         __be32                  nvalid;         /* count of valid entries */
858         __be32                  nused;          /* count of used entries */
859         __be32                  pad;            /* 64 bit alignment */
860 };
861
862 struct xfs_dir3_free {
863         struct xfs_dir3_free_hdr hdr;
864         __be16                  bests[];        /* best free counts */
865                                                 /* unused entries are -1 */
866 };
867
868 #define XFS_DIR3_FREE_CRC_OFF  offsetof(struct xfs_dir3_free, hdr.hdr.crc)
869
870 /*
871  * In core version of the free block header, abstracted away from on-disk format
872  * differences. Use this in the code, and convert to/from the disk version using
873  * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
874  */
875 struct xfs_dir3_icfree_hdr {
876         __uint32_t      magic;
877         __uint32_t      firstdb;
878         __uint32_t      nvalid;
879         __uint32_t      nused;
880
881 };
882
883 void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
884                                  struct xfs_dir2_free *from);
885
886 static inline int
887 xfs_dir3_free_hdr_size(struct xfs_mount *mp)
888 {
889         if (xfs_sb_version_hascrc(&mp->m_sb))
890                 return sizeof(struct xfs_dir3_free_hdr);
891         return sizeof(struct xfs_dir2_free_hdr);
892 }
893
894 static inline int
895 xfs_dir3_free_max_bests(struct xfs_mount *mp)
896 {
897         return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
898                 sizeof(xfs_dir2_data_off_t);
899 }
900
901 static inline __be16 *
902 xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
903 {
904         return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
905 }
906
907 /*
908  * Convert data space db to the corresponding free db.
909  */
910 static inline xfs_dir2_db_t
911 xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
912 {
913         return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
914 }
915
916 /*
917  * Convert data space db to the corresponding index in a free db.
918  */
919 static inline int
920 xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
921 {
922         return db % xfs_dir3_free_max_bests(mp);
923 }
924
925 /*
926  * Single block format.
927  *
928  * The single block format looks like the following drawing on disk:
929  *
930  *    +-------------------------------------------------+
931  *    | xfs_dir2_data_hdr_t                             |
932  *    +-------------------------------------------------+
933  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
934  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
935  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
936  *    | ...                                             |
937  *    +-------------------------------------------------+
938  *    | unused space                                    |
939  *    +-------------------------------------------------+
940  *    | ...                                             |
941  *    | xfs_dir2_leaf_entry_t                           |
942  *    | xfs_dir2_leaf_entry_t                           |
943  *    +-------------------------------------------------+
944  *    | xfs_dir2_block_tail_t                           |
945  *    +-------------------------------------------------+
946  *
947  * As all the entries are variable size structures the accessors below should
948  * be used to iterate over them.
949  */
950
951 typedef struct xfs_dir2_block_tail {
952         __be32          count;                  /* count of leaf entries */
953         __be32          stale;                  /* count of stale lf entries */
954 } xfs_dir2_block_tail_t;
955
956 /*
957  * Pointer to the leaf header embedded in a data block (1-block format)
958  */
959 static inline struct xfs_dir2_block_tail *
960 xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
961 {
962         return ((struct xfs_dir2_block_tail *)
963                 ((char *)hdr + mp->m_dirblksize)) - 1;
964 }
965
966 /*
967  * Pointer to the leaf entries embedded in a data block (1-block format)
968  */
969 static inline struct xfs_dir2_leaf_entry *
970 xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
971 {
972         return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
973 }
974
975 #endif /* __XFS_DIR2_FORMAT_H__ */