MIPS: Tell R4k SC and MC variations apart
[linux-2.6/btrfs-unstable.git] / fs / xfs / xfs_dir2_format.h
blob9cf67381adf6769d0b6fc984b6fe5ecaf8cef5b4
1 /*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
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
45 * easier.
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
57 * necessary.
59 * Version 3 includes:
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
74 * are as follows:
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
126 * fit in 32 bits.
128 typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
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)
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
145 * over them.
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) -
169 (i8count == 0) *
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);
179 static inline void
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));
192 static inline int
193 xfs_dir3_sf_entsize(
194 struct xfs_mount *mp,
195 struct xfs_dir2_sf_hdr *hdr,
196 int len)
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 */
205 return count;
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 *
224 xfs_dir3_sfe_ftypep(
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)
237 __uint8_t *ftp;
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;
245 return *ftp;
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)
255 __uint8_t *ftp;
257 ASSERT(ftype < XFS_DIR3_FT_MAX);
259 if (!xfs_sb_version_hasftype(&mp->m_sb))
260 return;
261 ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
262 *ftp = ftype;
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 |
276 * | ... |
277 * +-------------------------------------------------+
278 * | unused space |
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"
286 * format below.
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
368 * the tag.
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.
394 static inline int
395 __xfs_dir3_data_entsize(
396 bool ftype,
397 int n)
399 int size = offsetof(struct xfs_dir2_data_entry, name[0]);
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);
407 static inline int
408 xfs_dir3_data_entsize(
409 struct xfs_mount *mp,
410 int n)
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)
426 return type;
429 return XFS_DIR3_FT_UNKNOWN;
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)
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));
467 static inline size_t
468 xfs_dir3_data_hdr_size(bool dir3)
470 if (dir3)
471 return sizeof(struct xfs_dir3_data_hdr);
472 return sizeof(struct xfs_dir2_data_hdr);
475 static inline size_t
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 * XXX: there is scope for significant optimisation of the logic here. Right
501 * now we are checking for "dir3 format" over and over again. Ideally we should
502 * only do it once for each operation.
504 static inline xfs_dir2_data_aoff_t
505 xfs_dir3_data_dot_offset(struct xfs_mount *mp)
507 return xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
510 static inline xfs_dir2_data_aoff_t
511 xfs_dir3_data_dotdot_offset(struct xfs_mount *mp)
513 return xfs_dir3_data_dot_offset(mp) +
514 xfs_dir3_data_entsize(mp, 1);
517 static inline xfs_dir2_data_aoff_t
518 xfs_dir3_data_first_offset(struct xfs_mount *mp)
520 return xfs_dir3_data_dotdot_offset(mp) +
521 xfs_dir3_data_entsize(mp, 2);
525 * location of . and .. in data space (always block 0)
527 static inline struct xfs_dir2_data_entry *
528 xfs_dir3_data_dot_entry_p(
529 struct xfs_mount *mp,
530 struct xfs_dir2_data_hdr *hdr)
532 return (struct xfs_dir2_data_entry *)
533 ((char *)hdr + xfs_dir3_data_dot_offset(mp));
536 static inline struct xfs_dir2_data_entry *
537 xfs_dir3_data_dotdot_entry_p(
538 struct xfs_mount *mp,
539 struct xfs_dir2_data_hdr *hdr)
541 return (struct xfs_dir2_data_entry *)
542 ((char *)hdr + xfs_dir3_data_dotdot_offset(mp));
545 static inline struct xfs_dir2_data_entry *
546 xfs_dir3_data_first_entry_p(
547 struct xfs_mount *mp,
548 struct xfs_dir2_data_hdr *hdr)
550 return (struct xfs_dir2_data_entry *)
551 ((char *)hdr + xfs_dir3_data_first_offset(mp));
555 * Leaf block structures.
557 * A pure leaf block looks like the following drawing on disk:
559 * +---------------------------+
560 * | xfs_dir2_leaf_hdr_t |
561 * +---------------------------+
562 * | xfs_dir2_leaf_entry_t |
563 * | xfs_dir2_leaf_entry_t |
564 * | xfs_dir2_leaf_entry_t |
565 * | xfs_dir2_leaf_entry_t |
566 * | ... |
567 * +---------------------------+
568 * | xfs_dir2_data_off_t |
569 * | xfs_dir2_data_off_t |
570 * | xfs_dir2_data_off_t |
571 * | ... |
572 * +---------------------------+
573 * | xfs_dir2_leaf_tail_t |
574 * +---------------------------+
576 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
577 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
578 * for directories with separate leaf nodes and free space blocks
579 * (magic = XFS_DIR2_LEAFN_MAGIC).
581 * As all the entries are variable size structures the accessors below should
582 * be used to iterate over them.
586 * Offset of the leaf/node space. First block in this space
587 * is the btree root.
589 #define XFS_DIR2_LEAF_SPACE 1
590 #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
591 #define XFS_DIR2_LEAF_FIRSTDB(mp) \
592 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
595 * Leaf block header.
597 typedef struct xfs_dir2_leaf_hdr {
598 xfs_da_blkinfo_t info; /* header for da routines */
599 __be16 count; /* count of entries */
600 __be16 stale; /* count of stale entries */
601 } xfs_dir2_leaf_hdr_t;
603 struct xfs_dir3_leaf_hdr {
604 struct xfs_da3_blkinfo info; /* header for da routines */
605 __be16 count; /* count of entries */
606 __be16 stale; /* count of stale entries */
607 __be32 pad; /* 64 bit alignment */
610 struct xfs_dir3_icleaf_hdr {
611 __uint32_t forw;
612 __uint32_t back;
613 __uint16_t magic;
614 __uint16_t count;
615 __uint16_t stale;
619 * Leaf block entry.
621 typedef struct xfs_dir2_leaf_entry {
622 __be32 hashval; /* hash value of name */
623 __be32 address; /* address of data entry */
624 } xfs_dir2_leaf_entry_t;
627 * Leaf block tail.
629 typedef struct xfs_dir2_leaf_tail {
630 __be32 bestcount;
631 } xfs_dir2_leaf_tail_t;
634 * Leaf block.
636 typedef struct xfs_dir2_leaf {
637 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
638 xfs_dir2_leaf_entry_t __ents[]; /* entries */
639 } xfs_dir2_leaf_t;
641 struct xfs_dir3_leaf {
642 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
643 struct xfs_dir2_leaf_entry __ents[]; /* entries */
646 #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
648 extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
649 struct xfs_dir2_leaf *from);
651 static inline int
652 xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
654 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
655 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
656 return sizeof(struct xfs_dir3_leaf_hdr);
657 return sizeof(struct xfs_dir2_leaf_hdr);
660 static inline int
661 xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
663 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
664 (uint)sizeof(struct xfs_dir2_leaf_entry);
668 * Get address of the bestcount field in the single-leaf block.
670 static inline struct xfs_dir2_leaf_entry *
671 xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
673 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
674 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
675 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
676 return lp3->__ents;
678 return lp->__ents;
682 * Get address of the bestcount field in the single-leaf block.
684 static inline struct xfs_dir2_leaf_tail *
685 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
687 return (struct xfs_dir2_leaf_tail *)
688 ((char *)lp + mp->m_dirblksize -
689 sizeof(struct xfs_dir2_leaf_tail));
693 * Get address of the bests array in the single-leaf block.
695 static inline __be16 *
696 xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
698 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
702 * DB blocks here are logical directory block numbers, not filesystem blocks.
706 * Convert dataptr to byte in file space
708 static inline xfs_dir2_off_t
709 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
711 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
715 * Convert byte in file space to dataptr. It had better be aligned.
717 static inline xfs_dir2_dataptr_t
718 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
720 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
724 * Convert byte in space to (DB) block
726 static inline xfs_dir2_db_t
727 xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
729 return (xfs_dir2_db_t)
730 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
734 * Convert dataptr to a block number
736 static inline xfs_dir2_db_t
737 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
739 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
743 * Convert byte in space to offset in a block
745 static inline xfs_dir2_data_aoff_t
746 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
748 return (xfs_dir2_data_aoff_t)(by &
749 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
753 * Convert dataptr to a byte offset in a block
755 static inline xfs_dir2_data_aoff_t
756 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
758 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
762 * Convert block and offset to byte in space
764 static inline xfs_dir2_off_t
765 xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
766 xfs_dir2_data_aoff_t o)
768 return ((xfs_dir2_off_t)db <<
769 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
773 * Convert block (DB) to block (dablk)
775 static inline xfs_dablk_t
776 xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
778 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
782 * Convert byte in space to (DA) block
784 static inline xfs_dablk_t
785 xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
787 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
791 * Convert block and offset to dataptr
793 static inline xfs_dir2_dataptr_t
794 xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
795 xfs_dir2_data_aoff_t o)
797 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
801 * Convert block (dablk) to block (DB)
803 static inline xfs_dir2_db_t
804 xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
806 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
810 * Convert block (dablk) to byte offset in space
812 static inline xfs_dir2_off_t
813 xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
815 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
819 * Free space block defintions for the node format.
823 * Offset of the freespace index.
825 #define XFS_DIR2_FREE_SPACE 2
826 #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
827 #define XFS_DIR2_FREE_FIRSTDB(mp) \
828 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
830 typedef struct xfs_dir2_free_hdr {
831 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
832 __be32 firstdb; /* db of first entry */
833 __be32 nvalid; /* count of valid entries */
834 __be32 nused; /* count of used entries */
835 } xfs_dir2_free_hdr_t;
837 typedef struct xfs_dir2_free {
838 xfs_dir2_free_hdr_t hdr; /* block header */
839 __be16 bests[]; /* best free counts */
840 /* unused entries are -1 */
841 } xfs_dir2_free_t;
843 struct xfs_dir3_free_hdr {
844 struct xfs_dir3_blk_hdr hdr;
845 __be32 firstdb; /* db of first entry */
846 __be32 nvalid; /* count of valid entries */
847 __be32 nused; /* count of used entries */
848 __be32 pad; /* 64 bit alignment */
851 struct xfs_dir3_free {
852 struct xfs_dir3_free_hdr hdr;
853 __be16 bests[]; /* best free counts */
854 /* unused entries are -1 */
857 #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
860 * In core version of the free block header, abstracted away from on-disk format
861 * differences. Use this in the code, and convert to/from the disk version using
862 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
864 struct xfs_dir3_icfree_hdr {
865 __uint32_t magic;
866 __uint32_t firstdb;
867 __uint32_t nvalid;
868 __uint32_t nused;
872 void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
873 struct xfs_dir2_free *from);
875 static inline int
876 xfs_dir3_free_hdr_size(struct xfs_mount *mp)
878 if (xfs_sb_version_hascrc(&mp->m_sb))
879 return sizeof(struct xfs_dir3_free_hdr);
880 return sizeof(struct xfs_dir2_free_hdr);
883 static inline int
884 xfs_dir3_free_max_bests(struct xfs_mount *mp)
886 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
887 sizeof(xfs_dir2_data_off_t);
890 static inline __be16 *
891 xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
893 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
897 * Convert data space db to the corresponding free db.
899 static inline xfs_dir2_db_t
900 xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
902 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
906 * Convert data space db to the corresponding index in a free db.
908 static inline int
909 xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
911 return db % xfs_dir3_free_max_bests(mp);
915 * Single block format.
917 * The single block format looks like the following drawing on disk:
919 * +-------------------------------------------------+
920 * | xfs_dir2_data_hdr_t |
921 * +-------------------------------------------------+
922 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
923 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
924 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
925 * | ... |
926 * +-------------------------------------------------+
927 * | unused space |
928 * +-------------------------------------------------+
929 * | ... |
930 * | xfs_dir2_leaf_entry_t |
931 * | xfs_dir2_leaf_entry_t |
932 * +-------------------------------------------------+
933 * | xfs_dir2_block_tail_t |
934 * +-------------------------------------------------+
936 * As all the entries are variable size structures the accessors below should
937 * be used to iterate over them.
940 typedef struct xfs_dir2_block_tail {
941 __be32 count; /* count of leaf entries */
942 __be32 stale; /* count of stale lf entries */
943 } xfs_dir2_block_tail_t;
946 * Pointer to the leaf header embedded in a data block (1-block format)
948 static inline struct xfs_dir2_block_tail *
949 xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
951 return ((struct xfs_dir2_block_tail *)
952 ((char *)hdr + mp->m_dirblksize)) - 1;
956 * Pointer to the leaf entries embedded in a data block (1-block format)
958 static inline struct xfs_dir2_leaf_entry *
959 xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
961 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
964 #endif /* __XFS_DIR2_FORMAT_H__ */