2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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.
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.
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
19 #ifndef __XFS_DIR2_FORMAT_H__
20 #define __XFS_DIR2_FORMAT_H__
23 * Directory version 2.
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
31 * Note: many node blocks structures and constants are shared with the attr
32 * code and defined in xfs_da_btree.h.
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 */
40 * Directory Version 3 With CRCs.
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
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.
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
61 * - a larger block header for CRC and identification purposes and so the
62 * offsets of all the structures inside the blocks are different.
64 * - new magic numbers to be able to detect the v2/v3 types on the fly.
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 */
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
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
86 #define XFS_DIR3_FT_MAX 9
89 * Byte offset in data block and shortform entry.
91 typedef __uint16_t xfs_dir2_data_off_t;
92 #define NULLDATAOFF 0xffffU
93 typedef uint xfs_dir2_data_aoff_t; /* argument form */
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.
99 typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
102 * Offset in data space of a data entry.
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)
109 * Byte offset in a directory.
111 typedef xfs_off_t xfs_dir2_off_t;
114 * Directory block number (logical dirblk in file)
116 typedef __uint32_t xfs_dir2_db_t;
119 * Inode number stored as 8 8-bit values.
121 typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
124 * Inode number stored as 4 8-bit values.
125 * Works a lot of the time, when all the inode numbers in a directory
128 typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
134 #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
137 * Directory layout when stored internal to an inode.
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
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;
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 */
158 * A single byte containing the file type field follows the inode
159 * number for version 3 directory entries.
161 * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
162 * variable offset after the name.
164 } __arch_pack xfs_dir2_sf_entry_t;
166 static inline int xfs_dir2_sf_hdr_size(int i8count)
168 return sizeof(struct xfs_dir2_sf_hdr) -
170 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
173 static inline xfs_dir2_data_aoff_t
174 xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
176 return get_unaligned_be16(&sfep->offset.i);
180 xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
182 put_unaligned_be16(off, &sfep->offset.i);
185 static inline struct xfs_dir2_sf_entry *
186 xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
188 return (struct xfs_dir2_sf_entry *)
189 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
194 struct xfs_mount *mp,
195 struct xfs_dir2_sf_hdr *hdr,
198 int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
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 */
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)
214 return (struct xfs_dir2_sf_entry *)
215 ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen));
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.
223 static inline __uint8_t *
225 struct xfs_dir2_sf_hdr *hdr,
226 struct xfs_dir2_sf_entry *sfep)
228 return (__uint8_t *)&sfep->name[sfep->namelen];
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)
239 if (!xfs_sb_version_hasftype(&mp->m_sb))
240 return XFS_DIR3_FT_UNKNOWN;
242 ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
243 if (*ftp >= XFS_DIR3_FT_MAX)
244 return XFS_DIR3_FT_UNKNOWN;
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,
257 ASSERT(ftype < XFS_DIR3_FT_MAX);
259 if (!xfs_sb_version_hasftype(&mp->m_sb))
261 ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
266 * Data block structures.
268 * A pure data block looks like the following drawing on disk:
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 |
277 * +-------------------------------------------------+
279 * +-------------------------------------------------+
281 * As all the entries are variable size structures the accessors below should
282 * be used to iterate over them.
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"
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
295 * Directory address space divided into sections,
296 * spaces separated by 32GB.
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)
305 * Describe a free area in the data block.
307 * The freespace will be formatted as a xfs_dir2_data_unused_t.
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;
315 * Header for the data blocks.
317 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
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;
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.
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 */
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 */
346 #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
348 static inline struct xfs_dir2_data_free *
349 xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
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;
356 return hdr->bestfree;
360 * Active entry in a data block.
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.
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
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;
379 * Unused entry in a data block.
381 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
382 * using xfs_dir2_data_unused_tag_p.
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;
392 * Size of a data entry.
395 __xfs_dir3_data_entsize(
399 int size = offsetof(struct xfs_dir2_data_entry, name[0]);
402 size += sizeof(xfs_dir2_data_off_t);
404 size += sizeof(__uint8_t);
405 return roundup(size, XFS_DIR2_DATA_ALIGN);
408 xfs_dir3_data_entsize(
409 struct xfs_mount *mp,
412 bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false;
413 return __xfs_dir3_data_entsize(ftype, n);
416 static inline __uint8_t
417 xfs_dir3_dirent_get_ftype(
418 struct xfs_mount *mp,
419 struct xfs_dir2_data_entry *dep)
421 if (xfs_sb_version_hasftype(&mp->m_sb)) {
422 __uint8_t type = dep->name[dep->namelen];
424 ASSERT(type < XFS_DIR3_FT_MAX);
425 if (type < XFS_DIR3_FT_MAX)
429 return XFS_DIR3_FT_UNKNOWN;
433 xfs_dir3_dirent_put_ftype(
434 struct xfs_mount *mp,
435 struct xfs_dir2_data_entry *dep,
438 ASSERT(type < XFS_DIR3_FT_MAX);
439 ASSERT(dep->namelen != 0);
441 if (xfs_sb_version_hasftype(&mp->m_sb))
442 dep->name[dep->namelen] = type;
446 * Pointer to an entry's tag word.
448 static inline __be16 *
449 xfs_dir3_data_entry_tag_p(
450 struct xfs_mount *mp,
451 struct xfs_dir2_data_entry *dep)
453 return (__be16 *)((char *)dep +
454 xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16));
458 * Pointer to a freespace's tag word.
460 static inline __be16 *
461 xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
463 return (__be16 *)((char *)dup +
464 be16_to_cpu(dup->length) - sizeof(__be16));
468 xfs_dir3_data_hdr_size(bool dir3)
471 return sizeof(struct xfs_dir3_data_hdr);
472 return sizeof(struct xfs_dir2_data_hdr);
476 xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
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);
483 static inline struct xfs_dir2_data_entry *
484 xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
486 return (struct xfs_dir2_data_entry *)
487 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
490 static inline struct xfs_dir2_data_unused *
491 xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
493 return (struct xfs_dir2_data_unused *)
494 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
498 * Offsets of . and .. in data space (always block 0)
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.
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.
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))
517 static inline xfs_dir2_data_aoff_t
518 xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
520 return xfs_dir3_data_entry_offset(hdr);
523 static inline xfs_dir2_data_aoff_t
524 xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
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);
532 static inline xfs_dir2_data_aoff_t
533 xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
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);
542 * location of . and .. in data space (always block 0)
544 static inline struct xfs_dir2_data_entry *
545 xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
547 return (struct xfs_dir2_data_entry *)
548 ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
551 static inline struct xfs_dir2_data_entry *
552 xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
554 return (struct xfs_dir2_data_entry *)
555 ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
558 static inline struct xfs_dir2_data_entry *
559 xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
561 return (struct xfs_dir2_data_entry *)
562 ((char *)hdr + xfs_dir3_data_first_offset(hdr));
566 * Leaf block structures.
568 * A pure leaf block looks like the following drawing on disk:
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 |
578 * +---------------------------+
579 * | xfs_dir2_data_off_t |
580 * | xfs_dir2_data_off_t |
581 * | xfs_dir2_data_off_t |
583 * +---------------------------+
584 * | xfs_dir2_leaf_tail_t |
585 * +---------------------------+
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).
592 * As all the entries are variable size structures the accessors below should
593 * be used to iterate over them.
597 * Offset of the leaf/node space. First block in this space
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)
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;
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 */
621 struct xfs_dir3_icleaf_hdr {
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;
640 typedef struct xfs_dir2_leaf_tail {
642 } xfs_dir2_leaf_tail_t;
647 typedef struct xfs_dir2_leaf {
648 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
649 xfs_dir2_leaf_entry_t __ents[]; /* entries */
652 struct xfs_dir3_leaf {
653 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
654 struct xfs_dir2_leaf_entry __ents[]; /* entries */
657 #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
659 extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
660 struct xfs_dir2_leaf *from);
663 xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
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);
672 xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
674 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
675 (uint)sizeof(struct xfs_dir2_leaf_entry);
679 * Get address of the bestcount field in the single-leaf block.
681 static inline struct xfs_dir2_leaf_entry *
682 xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
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;
693 * Get address of the bestcount field in the single-leaf block.
695 static inline struct xfs_dir2_leaf_tail *
696 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
698 return (struct xfs_dir2_leaf_tail *)
699 ((char *)lp + mp->m_dirblksize -
700 sizeof(struct xfs_dir2_leaf_tail));
704 * Get address of the bests array in the single-leaf block.
706 static inline __be16 *
707 xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
709 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
713 * DB blocks here are logical directory block numbers, not filesystem blocks.
717 * Convert dataptr to byte in file space
719 static inline xfs_dir2_off_t
720 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
722 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
726 * Convert byte in file space to dataptr. It had better be aligned.
728 static inline xfs_dir2_dataptr_t
729 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
731 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
735 * Convert byte in space to (DB) block
737 static inline xfs_dir2_db_t
738 xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
740 return (xfs_dir2_db_t)
741 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
745 * Convert dataptr to a block number
747 static inline xfs_dir2_db_t
748 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
750 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
754 * Convert byte in space to offset in a block
756 static inline xfs_dir2_data_aoff_t
757 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
759 return (xfs_dir2_data_aoff_t)(by &
760 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
764 * Convert dataptr to a byte offset in a block
766 static inline xfs_dir2_data_aoff_t
767 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
769 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
773 * Convert block and offset to byte in space
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)
779 return ((xfs_dir2_off_t)db <<
780 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
784 * Convert block (DB) to block (dablk)
786 static inline xfs_dablk_t
787 xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
789 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
793 * Convert byte in space to (DA) block
795 static inline xfs_dablk_t
796 xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
798 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
802 * Convert block and offset to dataptr
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)
808 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
812 * Convert block (dablk) to block (DB)
814 static inline xfs_dir2_db_t
815 xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
817 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
821 * Convert block (dablk) to byte offset in space
823 static inline xfs_dir2_off_t
824 xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
826 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
830 * Free space block defintions for the node format.
834 * Offset of the freespace index.
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)
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;
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 */
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 */
862 struct xfs_dir3_free {
863 struct xfs_dir3_free_hdr hdr;
864 __be16 bests[]; /* best free counts */
865 /* unused entries are -1 */
868 #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
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.
875 struct xfs_dir3_icfree_hdr {
883 void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
884 struct xfs_dir2_free *from);
887 xfs_dir3_free_hdr_size(struct xfs_mount *mp)
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);
895 xfs_dir3_free_max_bests(struct xfs_mount *mp)
897 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
898 sizeof(xfs_dir2_data_off_t);
901 static inline __be16 *
902 xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
904 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
908 * Convert data space db to the corresponding free db.
910 static inline xfs_dir2_db_t
911 xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
913 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
917 * Convert data space db to the corresponding index in a free db.
920 xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
922 return db % xfs_dir3_free_max_bests(mp);
926 * Single block format.
928 * The single block format looks like the following drawing on disk:
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 :
937 * +-------------------------------------------------+
939 * +-------------------------------------------------+
941 * | xfs_dir2_leaf_entry_t |
942 * | xfs_dir2_leaf_entry_t |
943 * +-------------------------------------------------+
944 * | xfs_dir2_block_tail_t |
945 * +-------------------------------------------------+
947 * As all the entries are variable size structures the accessors below should
948 * be used to iterate over them.
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;
957 * Pointer to the leaf header embedded in a data block (1-block format)
959 static inline struct xfs_dir2_block_tail *
960 xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
962 return ((struct xfs_dir2_block_tail *)
963 ((char *)hdr + mp->m_dirblksize)) - 1;
967 * Pointer to the leaf entries embedded in a data block (1-block format)
969 static inline struct xfs_dir2_leaf_entry *
970 xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
972 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
975 #endif /* __XFS_DIR2_FORMAT_H__ */