]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - include/xfs_dir2_format.h
xfs: Add xfs_log_rlimit.c
[thirdparty/xfsprogs-dev.git] / include / xfs_dir2_format.h
CommitLineData
a2ceac1f
DC
1/*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
693fc8f6 3 * Copyright (c) 2013 Red Hat, Inc.
a2ceac1f
DC
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
693fc8f6
DC
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 */
3303b09f 69#define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
693fc8f6 70
a2ceac1f
DC
71/*
72 * Byte offset in data block and shortform entry.
73 */
74typedef __uint16_t xfs_dir2_data_off_t;
75#define NULLDATAOFF 0xffffU
79737cd9 76typedef uint xfs_dir2_data_aoff_t; /* argument form */
a2ceac1f
DC
77
78/*
79 * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
80 * Only need 16 bits, this is the byte offset into the single block form.
81 */
82typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
83
84/*
85 * Offset in data space of a data entry.
86 */
87typedef __uint32_t xfs_dir2_dataptr_t;
88#define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
89#define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
90
91/*
92 * Byte offset in a directory.
93 */
94typedef xfs_off_t xfs_dir2_off_t;
95
79737cd9
DC
96/*
97 * Directory block number (logical dirblk in file)
98 */
99typedef __uint32_t xfs_dir2_db_t;
100
a2ceac1f
DC
101/*
102 * Inode number stored as 8 8-bit values.
103 */
104typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
105
106/*
107 * Inode number stored as 4 8-bit values.
108 * Works a lot of the time, when all the inode numbers in a directory
109 * fit in 32 bits.
110 */
111typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
112
113typedef union {
114 xfs_dir2_ino8_t i8;
115 xfs_dir2_ino4_t i4;
116} xfs_dir2_inou_t;
117#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
118
119/*
120 * Directory layout when stored internal to an inode.
121 *
122 * Small directories are packed as tightly as possible so as to fit into the
123 * literal area of the inode. These "shortform" directories consist of a
124 * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
125 * structures. Due the different inode number storage size and the variable
126 * length name field in the xfs_dir2_sf_entry all these structure are
127 * variable length, and the accessors in this file should be used to iterate
128 * over them.
129 */
130typedef struct xfs_dir2_sf_hdr {
131 __uint8_t count; /* count of entries */
132 __uint8_t i8count; /* count of 8-byte inode #s */
133 xfs_dir2_inou_t parent; /* parent dir inode number */
134} __arch_pack xfs_dir2_sf_hdr_t;
135
136typedef struct xfs_dir2_sf_entry {
137 __u8 namelen; /* actual name length */
138 xfs_dir2_sf_off_t offset; /* saved offset */
139 __u8 name[]; /* name, variable size */
140 /*
141 * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
142 * variable offset after the name.
143 */
144} __arch_pack xfs_dir2_sf_entry_t;
145
146static inline int xfs_dir2_sf_hdr_size(int i8count)
147{
148 return sizeof(struct xfs_dir2_sf_hdr) -
149 (i8count == 0) *
150 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
151}
152
79737cd9 153static inline xfs_dir2_data_aoff_t
a2ceac1f
DC
154xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
155{
156 return get_unaligned_be16(&sfep->offset.i);
157}
158
79737cd9 159static inline void
a2ceac1f
DC
160xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
161{
162 put_unaligned_be16(off, &sfep->offset.i);
163}
164
79737cd9 165static inline int
a2ceac1f
DC
166xfs_dir2_sf_entsize(struct xfs_dir2_sf_hdr *hdr, int len)
167{
168 return sizeof(struct xfs_dir2_sf_entry) + /* namelen + offset */
169 len + /* name */
170 (hdr->i8count ? /* ino */
171 sizeof(xfs_dir2_ino8_t) :
172 sizeof(xfs_dir2_ino4_t));
173}
174
79737cd9 175static inline struct xfs_dir2_sf_entry *
a2ceac1f
DC
176xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
177{
178 return (struct xfs_dir2_sf_entry *)
179 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
180}
181
79737cd9 182static inline struct xfs_dir2_sf_entry *
a2ceac1f
DC
183xfs_dir2_sf_nextentry(struct xfs_dir2_sf_hdr *hdr,
184 struct xfs_dir2_sf_entry *sfep)
185{
186 return (struct xfs_dir2_sf_entry *)
187 ((char *)sfep + xfs_dir2_sf_entsize(hdr, sfep->namelen));
188}
189
190
191/*
192 * Data block structures.
193 *
194 * A pure data block looks like the following drawing on disk:
195 *
196 * +-------------------------------------------------+
197 * | xfs_dir2_data_hdr_t |
198 * +-------------------------------------------------+
199 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
200 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
201 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
202 * | ... |
203 * +-------------------------------------------------+
204 * | unused space |
205 * +-------------------------------------------------+
206 *
207 * As all the entries are variable size structures the accessors below should
208 * be used to iterate over them.
209 *
210 * In addition to the pure data blocks for the data and node formats,
211 * most structures are also used for the combined data/freespace "block"
212 * format below.
213 */
214
215#define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
216#define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
217#define XFS_DIR2_DATA_FREE_TAG 0xffff
218#define XFS_DIR2_DATA_FD_COUNT 3
219
220/*
221 * Directory address space divided into sections,
222 * spaces separated by 32GB.
223 */
224#define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
225#define XFS_DIR2_DATA_SPACE 0
226#define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
227#define XFS_DIR2_DATA_FIRSTDB(mp) \
228 xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
229
a2ceac1f
DC
230/*
231 * Describe a free area in the data block.
232 *
233 * The freespace will be formatted as a xfs_dir2_data_unused_t.
234 */
235typedef struct xfs_dir2_data_free {
236 __be16 offset; /* start of freespace */
237 __be16 length; /* length of freespace */
238} xfs_dir2_data_free_t;
239
240/*
241 * Header for the data blocks.
242 *
243 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
244 */
245typedef struct xfs_dir2_data_hdr {
246 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
79737cd9 247 /* XFS_DIR2_BLOCK_MAGIC */
a2ceac1f
DC
248 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
249} xfs_dir2_data_hdr_t;
250
693fc8f6
DC
251/*
252 * define a structure for all the verification fields we are adding to the
253 * directory block structures. This will be used in several structures.
254 * The magic number must be the first entry to align with all the dir2
255 * structures so we determine how to decode them just by the magic number.
256 */
257struct xfs_dir3_blk_hdr {
258 __be32 magic; /* magic number */
259 __be32 crc; /* CRC of block */
260 __be64 blkno; /* first block of the buffer */
261 __be64 lsn; /* sequence number of last write */
262 uuid_t uuid; /* filesystem we belong to */
263 __be64 owner; /* inode that owns the block */
264};
265
266struct xfs_dir3_data_hdr {
267 struct xfs_dir3_blk_hdr hdr;
268 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
84796a14 269 __be32 pad; /* 64 bit alignment */
693fc8f6
DC
270};
271
272#define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
273
79737cd9 274static inline struct xfs_dir2_data_free *
693fc8f6
DC
275xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
276{
90ea28c3
DC
277 if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
278 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
693fc8f6
DC
279 struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
280 return hdr3->best_free;
281 }
282 return hdr->bestfree;
283}
284
a2ceac1f
DC
285/*
286 * Active entry in a data block.
287 *
288 * Aligned to 8 bytes. After the variable length name field there is a
289 * 2 byte tag field, which can be accessed using xfs_dir2_data_entry_tag_p.
290 */
291typedef struct xfs_dir2_data_entry {
292 __be64 inumber; /* inode number */
293 __u8 namelen; /* name length */
294 __u8 name[]; /* name bytes, no null */
295 /* __be16 tag; */ /* starting offset of us */
296} xfs_dir2_data_entry_t;
297
298/*
299 * Unused entry in a data block.
300 *
301 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
302 * using xfs_dir2_data_unused_tag_p.
303 */
304typedef struct xfs_dir2_data_unused {
305 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
306 __be16 length; /* total free length */
307 /* variable offset */
308 __be16 tag; /* starting offset of us */
309} xfs_dir2_data_unused_t;
310
311/*
312 * Size of a data entry.
313 */
314static inline int xfs_dir2_data_entsize(int n)
315{
316 return (int)roundup(offsetof(struct xfs_dir2_data_entry, name[0]) + n +
317 (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
318}
319
320/*
321 * Pointer to an entry's tag word.
322 */
323static inline __be16 *
324xfs_dir2_data_entry_tag_p(struct xfs_dir2_data_entry *dep)
325{
326 return (__be16 *)((char *)dep +
327 xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
328}
329
330/*
331 * Pointer to a freespace's tag word.
332 */
333static inline __be16 *
334xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
335{
336 return (__be16 *)((char *)dup +
337 be16_to_cpu(dup->length) - sizeof(__be16));
338}
339
693fc8f6
DC
340static inline size_t
341xfs_dir3_data_hdr_size(bool dir3)
342{
343 if (dir3)
344 return sizeof(struct xfs_dir3_data_hdr);
345 return sizeof(struct xfs_dir2_data_hdr);
346}
347
348static inline size_t
349xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
350{
351 bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
352 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
353 return xfs_dir3_data_hdr_size(dir3);
354}
355
356static inline struct xfs_dir2_data_entry *
357xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
358{
359 return (struct xfs_dir2_data_entry *)
360 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
361}
362
90ea28c3
DC
363static inline struct xfs_dir2_data_unused *
364xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
365{
366 return (struct xfs_dir2_data_unused *)
367 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
368}
369
693fc8f6
DC
370/*
371 * Offsets of . and .. in data space (always block 0)
584f90b0
DC
372 *
373 * The macros are used for shortform directories as they have no headers to read
374 * the magic number out of. Shortform directories need to know the size of the
375 * data block header because the sfe embeds the block offset of the entry into
376 * it so that it doesn't change when format conversion occurs. Bad Things Happen
377 * if we don't follow this rule.
693fc8f6 378 */
584f90b0
DC
379#define XFS_DIR3_DATA_DOT_OFFSET(mp) \
380 xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&(mp)->m_sb))
381#define XFS_DIR3_DATA_DOTDOT_OFFSET(mp) \
382 (XFS_DIR3_DATA_DOT_OFFSET(mp) + xfs_dir2_data_entsize(1))
383#define XFS_DIR3_DATA_FIRST_OFFSET(mp) \
384 (XFS_DIR3_DATA_DOTDOT_OFFSET(mp) + xfs_dir2_data_entsize(2))
385
693fc8f6
DC
386static inline xfs_dir2_data_aoff_t
387xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
388{
389 return xfs_dir3_data_entry_offset(hdr);
390}
391
392static inline xfs_dir2_data_aoff_t
393xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
394{
395 return xfs_dir3_data_dot_offset(hdr) + xfs_dir2_data_entsize(1);
396}
397
398static inline xfs_dir2_data_aoff_t
399xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
400{
401 return xfs_dir3_data_dotdot_offset(hdr) + xfs_dir2_data_entsize(2);
402}
403
404/*
405 * location of . and .. in data space (always block 0)
406 */
407static inline struct xfs_dir2_data_entry *
408xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
409{
410 return (struct xfs_dir2_data_entry *)
411 ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
412}
413
414static inline struct xfs_dir2_data_entry *
415xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
416{
417 return (struct xfs_dir2_data_entry *)
418 ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
419}
420
421static inline struct xfs_dir2_data_entry *
422xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
423{
424 return (struct xfs_dir2_data_entry *)
425 ((char *)hdr + xfs_dir3_data_first_offset(hdr));
426}
427
a2ceac1f
DC
428/*
429 * Leaf block structures.
430 *
431 * A pure leaf block looks like the following drawing on disk:
432 *
433 * +---------------------------+
434 * | xfs_dir2_leaf_hdr_t |
435 * +---------------------------+
436 * | xfs_dir2_leaf_entry_t |
437 * | xfs_dir2_leaf_entry_t |
438 * | xfs_dir2_leaf_entry_t |
439 * | xfs_dir2_leaf_entry_t |
440 * | ... |
441 * +---------------------------+
442 * | xfs_dir2_data_off_t |
443 * | xfs_dir2_data_off_t |
444 * | xfs_dir2_data_off_t |
445 * | ... |
446 * +---------------------------+
447 * | xfs_dir2_leaf_tail_t |
448 * +---------------------------+
449 *
450 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
451 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
452 * for directories with separate leaf nodes and free space blocks
453 * (magic = XFS_DIR2_LEAFN_MAGIC).
454 *
455 * As all the entries are variable size structures the accessors below should
456 * be used to iterate over them.
457 */
458
459/*
460 * Offset of the leaf/node space. First block in this space
461 * is the btree root.
462 */
463#define XFS_DIR2_LEAF_SPACE 1
464#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
465#define XFS_DIR2_LEAF_FIRSTDB(mp) \
466 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
467
468/*
469 * Leaf block header.
470 */
471typedef struct xfs_dir2_leaf_hdr {
472 xfs_da_blkinfo_t info; /* header for da routines */
473 __be16 count; /* count of entries */
474 __be16 stale; /* count of stale entries */
475} xfs_dir2_leaf_hdr_t;
476
65b80c98
DC
477struct xfs_dir3_leaf_hdr {
478 struct xfs_da3_blkinfo info; /* header for da routines */
479 __be16 count; /* count of entries */
480 __be16 stale; /* count of stale entries */
84796a14 481 __be32 pad; /* 64 bit alignment */
65b80c98
DC
482};
483
484struct xfs_dir3_icleaf_hdr {
485 __uint32_t forw;
486 __uint32_t back;
487 __uint16_t magic;
488 __uint16_t count;
489 __uint16_t stale;
490};
491
a2ceac1f
DC
492/*
493 * Leaf block entry.
494 */
495typedef struct xfs_dir2_leaf_entry {
496 __be32 hashval; /* hash value of name */
497 __be32 address; /* address of data entry */
498} xfs_dir2_leaf_entry_t;
499
500/*
501 * Leaf block tail.
502 */
503typedef struct xfs_dir2_leaf_tail {
504 __be32 bestcount;
505} xfs_dir2_leaf_tail_t;
506
507/*
508 * Leaf block.
509 */
510typedef struct xfs_dir2_leaf {
65b80c98
DC
511 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
512 xfs_dir2_leaf_entry_t __ents[]; /* entries */
a2ceac1f
DC
513} xfs_dir2_leaf_t;
514
65b80c98
DC
515struct xfs_dir3_leaf {
516 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
517 struct xfs_dir2_leaf_entry __ents[]; /* entries */
518};
a2ceac1f 519
65b80c98
DC
520#define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
521
658ac3e3 522extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
79737cd9 523 struct xfs_dir2_leaf *from);
658ac3e3 524
65b80c98
DC
525static inline int
526xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
a2ceac1f 527{
65b80c98
DC
528 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
529 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
530 return sizeof(struct xfs_dir3_leaf_hdr);
531 return sizeof(struct xfs_dir2_leaf_hdr);
532}
533
534static inline int
535xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
536{
537 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
a2ceac1f
DC
538 (uint)sizeof(struct xfs_dir2_leaf_entry);
539}
540
65b80c98
DC
541/*
542 * Get address of the bestcount field in the single-leaf block.
543 */
544static inline struct xfs_dir2_leaf_entry *
545xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
546{
547 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
548 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
549 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
550 return lp3->__ents;
551 }
552 return lp->__ents;
553}
554
a2ceac1f
DC
555/*
556 * Get address of the bestcount field in the single-leaf block.
557 */
558static inline struct xfs_dir2_leaf_tail *
559xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
560{
561 return (struct xfs_dir2_leaf_tail *)
562 ((char *)lp + mp->m_dirblksize -
563 sizeof(struct xfs_dir2_leaf_tail));
564}
565
566/*
567 * Get address of the bests array in the single-leaf block.
568 */
569static inline __be16 *
570xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
571{
572 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
573}
574
65b80c98
DC
575/*
576 * DB blocks here are logical directory block numbers, not filesystem blocks.
577 */
578
a2ceac1f
DC
579/*
580 * Convert dataptr to byte in file space
581 */
582static inline xfs_dir2_off_t
583xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
584{
585 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
586}
587
588/*
589 * Convert byte in file space to dataptr. It had better be aligned.
590 */
591static inline xfs_dir2_dataptr_t
592xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
593{
594 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
595}
596
597/*
598 * Convert byte in space to (DB) block
599 */
600static inline xfs_dir2_db_t
601xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
602{
603 return (xfs_dir2_db_t)
604 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
605}
606
607/*
608 * Convert dataptr to a block number
609 */
610static inline xfs_dir2_db_t
611xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
612{
613 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
614}
615
616/*
617 * Convert byte in space to offset in a block
618 */
619static inline xfs_dir2_data_aoff_t
620xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
621{
622 return (xfs_dir2_data_aoff_t)(by &
623 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
624}
625
626/*
627 * Convert dataptr to a byte offset in a block
628 */
629static inline xfs_dir2_data_aoff_t
630xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
631{
632 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
633}
634
635/*
636 * Convert block and offset to byte in space
637 */
638static inline xfs_dir2_off_t
639xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
640 xfs_dir2_data_aoff_t o)
641{
642 return ((xfs_dir2_off_t)db <<
643 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
644}
645
646/*
647 * Convert block (DB) to block (dablk)
648 */
649static inline xfs_dablk_t
650xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
651{
652 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
653}
654
655/*
656 * Convert byte in space to (DA) block
657 */
658static inline xfs_dablk_t
659xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
660{
661 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
662}
663
664/*
665 * Convert block and offset to dataptr
666 */
667static inline xfs_dir2_dataptr_t
668xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
669 xfs_dir2_data_aoff_t o)
670{
671 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
672}
673
674/*
675 * Convert block (dablk) to block (DB)
676 */
677static inline xfs_dir2_db_t
678xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
679{
680 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
681}
682
683/*
684 * Convert block (dablk) to byte offset in space
685 */
686static inline xfs_dir2_off_t
687xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
688{
689 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
690}
691
692/*
693 * Free space block defintions for the node format.
694 */
695
696/*
697 * Offset of the freespace index.
698 */
699#define XFS_DIR2_FREE_SPACE 2
700#define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
701#define XFS_DIR2_FREE_FIRSTDB(mp) \
702 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
703
704typedef struct xfs_dir2_free_hdr {
705 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
706 __be32 firstdb; /* db of first entry */
707 __be32 nvalid; /* count of valid entries */
708 __be32 nused; /* count of used entries */
709} xfs_dir2_free_hdr_t;
710
711typedef struct xfs_dir2_free {
712 xfs_dir2_free_hdr_t hdr; /* block header */
713 __be16 bests[]; /* best free counts */
714 /* unused entries are -1 */
715} xfs_dir2_free_t;
716
3303b09f
DC
717struct xfs_dir3_free_hdr {
718 struct xfs_dir3_blk_hdr hdr;
719 __be32 firstdb; /* db of first entry */
720 __be32 nvalid; /* count of valid entries */
721 __be32 nused; /* count of used entries */
84796a14 722 __be32 pad; /* 64 bit alignment */
3303b09f
DC
723};
724
725struct xfs_dir3_free {
726 struct xfs_dir3_free_hdr hdr;
727 __be16 bests[]; /* best free counts */
728 /* unused entries are -1 */
729};
730
731#define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
732
733/*
734 * In core version of the free block header, abstracted away from on-disk format
735 * differences. Use this in the code, and convert to/from the disk version using
736 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
737 */
738struct xfs_dir3_icfree_hdr {
739 __uint32_t magic;
740 __uint32_t firstdb;
741 __uint32_t nvalid;
742 __uint32_t nused;
743
744};
745
746void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
747 struct xfs_dir2_free *from);
748
749static inline int
750xfs_dir3_free_hdr_size(struct xfs_mount *mp)
a2ceac1f 751{
3303b09f
DC
752 if (xfs_sb_version_hascrc(&mp->m_sb))
753 return sizeof(struct xfs_dir3_free_hdr);
754 return sizeof(struct xfs_dir2_free_hdr);
755}
756
757static inline int
758xfs_dir3_free_max_bests(struct xfs_mount *mp)
759{
760 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
a2ceac1f
DC
761 sizeof(xfs_dir2_data_off_t);
762}
763
3303b09f
DC
764static inline __be16 *
765xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
766{
767 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
768}
769
a2ceac1f
DC
770/*
771 * Convert data space db to the corresponding free db.
772 */
773static inline xfs_dir2_db_t
774xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
775{
3303b09f 776 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
a2ceac1f
DC
777}
778
779/*
780 * Convert data space db to the corresponding index in a free db.
781 */
782static inline int
783xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
784{
3303b09f 785 return db % xfs_dir3_free_max_bests(mp);
a2ceac1f
DC
786}
787
788/*
789 * Single block format.
790 *
791 * The single block format looks like the following drawing on disk:
792 *
793 * +-------------------------------------------------+
794 * | xfs_dir2_data_hdr_t |
795 * +-------------------------------------------------+
796 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
797 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
798 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
799 * | ... |
800 * +-------------------------------------------------+
801 * | unused space |
802 * +-------------------------------------------------+
803 * | ... |
804 * | xfs_dir2_leaf_entry_t |
805 * | xfs_dir2_leaf_entry_t |
806 * +-------------------------------------------------+
807 * | xfs_dir2_block_tail_t |
808 * +-------------------------------------------------+
809 *
810 * As all the entries are variable size structures the accessors below should
811 * be used to iterate over them.
812 */
813
814typedef struct xfs_dir2_block_tail {
815 __be32 count; /* count of leaf entries */
816 __be32 stale; /* count of stale lf entries */
817} xfs_dir2_block_tail_t;
818
819/*
820 * Pointer to the leaf header embedded in a data block (1-block format)
821 */
822static inline struct xfs_dir2_block_tail *
823xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
824{
825 return ((struct xfs_dir2_block_tail *)
826 ((char *)hdr + mp->m_dirblksize)) - 1;
827}
828
829/*
830 * Pointer to the leaf entries embedded in a data block (1-block format)
831 */
832static inline struct xfs_dir2_leaf_entry *
833xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
834{
835 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
836}
837
838#endif /* __XFS_DIR2_FORMAT_H__ */