Merge branch 'fixes-3.10' of git://git.infradead.org/users/willy/linux-nvme
[GitHub/mt8127/android_kernel_alcatel_ttab.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 * Byte offset in data block and shortform entry.
73 */
74 typedef __uint16_t xfs_dir2_data_off_t;
75 #define NULLDATAOFF 0xffffU
76 typedef uint xfs_dir2_data_aoff_t; /* argument form */
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 */
82 typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
83
84 /*
85 * Offset in data space of a data entry.
86 */
87 typedef __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 */
94 typedef xfs_off_t xfs_dir2_off_t;
95
96 /*
97 * Directory block number (logical dirblk in file)
98 */
99 typedef __uint32_t xfs_dir2_db_t;
100
101 /*
102 * Inode number stored as 8 8-bit values.
103 */
104 typedef 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 */
111 typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
112
113 typedef 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 */
130 typedef 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
136 typedef 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
146 static 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
153 static inline xfs_dir2_data_aoff_t
154 xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
155 {
156 return get_unaligned_be16(&sfep->offset.i);
157 }
158
159 static inline void
160 xfs_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
165 static inline int
166 xfs_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
175 static inline struct xfs_dir2_sf_entry *
176 xfs_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
182 static inline struct xfs_dir2_sf_entry *
183 xfs_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
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 */
235 typedef 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 */
245 typedef struct xfs_dir2_data_hdr {
246 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
247 /* XFS_DIR2_BLOCK_MAGIC */
248 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
249 } xfs_dir2_data_hdr_t;
250
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 */
257 struct 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
266 struct xfs_dir3_data_hdr {
267 struct xfs_dir3_blk_hdr hdr;
268 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
269 };
270
271 #define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
272
273 static inline struct xfs_dir2_data_free *
274 xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
275 {
276 if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
277 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
278 struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
279 return hdr3->best_free;
280 }
281 return hdr->bestfree;
282 }
283
284 /*
285 * Active entry in a data block.
286 *
287 * Aligned to 8 bytes. After the variable length name field there is a
288 * 2 byte tag field, which can be accessed using xfs_dir2_data_entry_tag_p.
289 */
290 typedef struct xfs_dir2_data_entry {
291 __be64 inumber; /* inode number */
292 __u8 namelen; /* name length */
293 __u8 name[]; /* name bytes, no null */
294 /* __be16 tag; */ /* starting offset of us */
295 } xfs_dir2_data_entry_t;
296
297 /*
298 * Unused entry in a data block.
299 *
300 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
301 * using xfs_dir2_data_unused_tag_p.
302 */
303 typedef struct xfs_dir2_data_unused {
304 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
305 __be16 length; /* total free length */
306 /* variable offset */
307 __be16 tag; /* starting offset of us */
308 } xfs_dir2_data_unused_t;
309
310 /*
311 * Size of a data entry.
312 */
313 static inline int xfs_dir2_data_entsize(int n)
314 {
315 return (int)roundup(offsetof(struct xfs_dir2_data_entry, name[0]) + n +
316 (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
317 }
318
319 /*
320 * Pointer to an entry's tag word.
321 */
322 static inline __be16 *
323 xfs_dir2_data_entry_tag_p(struct xfs_dir2_data_entry *dep)
324 {
325 return (__be16 *)((char *)dep +
326 xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
327 }
328
329 /*
330 * Pointer to a freespace's tag word.
331 */
332 static inline __be16 *
333 xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
334 {
335 return (__be16 *)((char *)dup +
336 be16_to_cpu(dup->length) - sizeof(__be16));
337 }
338
339 static inline size_t
340 xfs_dir3_data_hdr_size(bool dir3)
341 {
342 if (dir3)
343 return sizeof(struct xfs_dir3_data_hdr);
344 return sizeof(struct xfs_dir2_data_hdr);
345 }
346
347 static inline size_t
348 xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
349 {
350 bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
351 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
352 return xfs_dir3_data_hdr_size(dir3);
353 }
354
355 static inline struct xfs_dir2_data_entry *
356 xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
357 {
358 return (struct xfs_dir2_data_entry *)
359 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
360 }
361
362 static inline struct xfs_dir2_data_unused *
363 xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
364 {
365 return (struct xfs_dir2_data_unused *)
366 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
367 }
368
369 /*
370 * Offsets of . and .. in data space (always block 0)
371 *
372 * The macros are used for shortform directories as they have no headers to read
373 * the magic number out of. Shortform directories need to know the size of the
374 * data block header because the sfe embeds the block offset of the entry into
375 * it so that it doesn't change when format conversion occurs. Bad Things Happen
376 * if we don't follow this rule.
377 */
378 #define XFS_DIR3_DATA_DOT_OFFSET(mp) \
379 xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&(mp)->m_sb))
380 #define XFS_DIR3_DATA_DOTDOT_OFFSET(mp) \
381 (XFS_DIR3_DATA_DOT_OFFSET(mp) + xfs_dir2_data_entsize(1))
382 #define XFS_DIR3_DATA_FIRST_OFFSET(mp) \
383 (XFS_DIR3_DATA_DOTDOT_OFFSET(mp) + xfs_dir2_data_entsize(2))
384
385 static inline xfs_dir2_data_aoff_t
386 xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
387 {
388 return xfs_dir3_data_entry_offset(hdr);
389 }
390
391 static inline xfs_dir2_data_aoff_t
392 xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
393 {
394 return xfs_dir3_data_dot_offset(hdr) + xfs_dir2_data_entsize(1);
395 }
396
397 static inline xfs_dir2_data_aoff_t
398 xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
399 {
400 return xfs_dir3_data_dotdot_offset(hdr) + xfs_dir2_data_entsize(2);
401 }
402
403 /*
404 * location of . and .. in data space (always block 0)
405 */
406 static inline struct xfs_dir2_data_entry *
407 xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
408 {
409 return (struct xfs_dir2_data_entry *)
410 ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
411 }
412
413 static inline struct xfs_dir2_data_entry *
414 xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
415 {
416 return (struct xfs_dir2_data_entry *)
417 ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
418 }
419
420 static inline struct xfs_dir2_data_entry *
421 xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
422 {
423 return (struct xfs_dir2_data_entry *)
424 ((char *)hdr + xfs_dir3_data_first_offset(hdr));
425 }
426
427 /*
428 * Leaf block structures.
429 *
430 * A pure leaf block looks like the following drawing on disk:
431 *
432 * +---------------------------+
433 * | xfs_dir2_leaf_hdr_t |
434 * +---------------------------+
435 * | xfs_dir2_leaf_entry_t |
436 * | xfs_dir2_leaf_entry_t |
437 * | xfs_dir2_leaf_entry_t |
438 * | xfs_dir2_leaf_entry_t |
439 * | ... |
440 * +---------------------------+
441 * | xfs_dir2_data_off_t |
442 * | xfs_dir2_data_off_t |
443 * | xfs_dir2_data_off_t |
444 * | ... |
445 * +---------------------------+
446 * | xfs_dir2_leaf_tail_t |
447 * +---------------------------+
448 *
449 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
450 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
451 * for directories with separate leaf nodes and free space blocks
452 * (magic = XFS_DIR2_LEAFN_MAGIC).
453 *
454 * As all the entries are variable size structures the accessors below should
455 * be used to iterate over them.
456 */
457
458 /*
459 * Offset of the leaf/node space. First block in this space
460 * is the btree root.
461 */
462 #define XFS_DIR2_LEAF_SPACE 1
463 #define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
464 #define XFS_DIR2_LEAF_FIRSTDB(mp) \
465 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
466
467 /*
468 * Leaf block header.
469 */
470 typedef struct xfs_dir2_leaf_hdr {
471 xfs_da_blkinfo_t info; /* header for da routines */
472 __be16 count; /* count of entries */
473 __be16 stale; /* count of stale entries */
474 } xfs_dir2_leaf_hdr_t;
475
476 struct xfs_dir3_leaf_hdr {
477 struct xfs_da3_blkinfo info; /* header for da routines */
478 __be16 count; /* count of entries */
479 __be16 stale; /* count of stale entries */
480 __be32 pad;
481 };
482
483 struct xfs_dir3_icleaf_hdr {
484 __uint32_t forw;
485 __uint32_t back;
486 __uint16_t magic;
487 __uint16_t count;
488 __uint16_t stale;
489 };
490
491 /*
492 * Leaf block entry.
493 */
494 typedef struct xfs_dir2_leaf_entry {
495 __be32 hashval; /* hash value of name */
496 __be32 address; /* address of data entry */
497 } xfs_dir2_leaf_entry_t;
498
499 /*
500 * Leaf block tail.
501 */
502 typedef struct xfs_dir2_leaf_tail {
503 __be32 bestcount;
504 } xfs_dir2_leaf_tail_t;
505
506 /*
507 * Leaf block.
508 */
509 typedef struct xfs_dir2_leaf {
510 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
511 xfs_dir2_leaf_entry_t __ents[]; /* entries */
512 } xfs_dir2_leaf_t;
513
514 struct xfs_dir3_leaf {
515 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
516 struct xfs_dir2_leaf_entry __ents[]; /* entries */
517 };
518
519 #define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
520
521 static inline int
522 xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
523 {
524 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
525 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
526 return sizeof(struct xfs_dir3_leaf_hdr);
527 return sizeof(struct xfs_dir2_leaf_hdr);
528 }
529
530 static inline int
531 xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
532 {
533 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
534 (uint)sizeof(struct xfs_dir2_leaf_entry);
535 }
536
537 /*
538 * Get address of the bestcount field in the single-leaf block.
539 */
540 static inline struct xfs_dir2_leaf_entry *
541 xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
542 {
543 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
544 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
545 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
546 return lp3->__ents;
547 }
548 return lp->__ents;
549 }
550
551 /*
552 * Get address of the bestcount field in the single-leaf block.
553 */
554 static inline struct xfs_dir2_leaf_tail *
555 xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
556 {
557 return (struct xfs_dir2_leaf_tail *)
558 ((char *)lp + mp->m_dirblksize -
559 sizeof(struct xfs_dir2_leaf_tail));
560 }
561
562 /*
563 * Get address of the bests array in the single-leaf block.
564 */
565 static inline __be16 *
566 xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
567 {
568 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
569 }
570
571 /*
572 * DB blocks here are logical directory block numbers, not filesystem blocks.
573 */
574
575 /*
576 * Convert dataptr to byte in file space
577 */
578 static inline xfs_dir2_off_t
579 xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
580 {
581 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
582 }
583
584 /*
585 * Convert byte in file space to dataptr. It had better be aligned.
586 */
587 static inline xfs_dir2_dataptr_t
588 xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
589 {
590 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
591 }
592
593 /*
594 * Convert byte in space to (DB) block
595 */
596 static inline xfs_dir2_db_t
597 xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
598 {
599 return (xfs_dir2_db_t)
600 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
601 }
602
603 /*
604 * Convert dataptr to a block number
605 */
606 static inline xfs_dir2_db_t
607 xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
608 {
609 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
610 }
611
612 /*
613 * Convert byte in space to offset in a block
614 */
615 static inline xfs_dir2_data_aoff_t
616 xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
617 {
618 return (xfs_dir2_data_aoff_t)(by &
619 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
620 }
621
622 /*
623 * Convert dataptr to a byte offset in a block
624 */
625 static inline xfs_dir2_data_aoff_t
626 xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
627 {
628 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
629 }
630
631 /*
632 * Convert block and offset to byte in space
633 */
634 static inline xfs_dir2_off_t
635 xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
636 xfs_dir2_data_aoff_t o)
637 {
638 return ((xfs_dir2_off_t)db <<
639 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
640 }
641
642 /*
643 * Convert block (DB) to block (dablk)
644 */
645 static inline xfs_dablk_t
646 xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
647 {
648 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
649 }
650
651 /*
652 * Convert byte in space to (DA) block
653 */
654 static inline xfs_dablk_t
655 xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
656 {
657 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
658 }
659
660 /*
661 * Convert block and offset to dataptr
662 */
663 static inline xfs_dir2_dataptr_t
664 xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
665 xfs_dir2_data_aoff_t o)
666 {
667 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
668 }
669
670 /*
671 * Convert block (dablk) to block (DB)
672 */
673 static inline xfs_dir2_db_t
674 xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
675 {
676 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
677 }
678
679 /*
680 * Convert block (dablk) to byte offset in space
681 */
682 static inline xfs_dir2_off_t
683 xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
684 {
685 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
686 }
687
688 /*
689 * Free space block defintions for the node format.
690 */
691
692 /*
693 * Offset of the freespace index.
694 */
695 #define XFS_DIR2_FREE_SPACE 2
696 #define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
697 #define XFS_DIR2_FREE_FIRSTDB(mp) \
698 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
699
700 typedef struct xfs_dir2_free_hdr {
701 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
702 __be32 firstdb; /* db of first entry */
703 __be32 nvalid; /* count of valid entries */
704 __be32 nused; /* count of used entries */
705 } xfs_dir2_free_hdr_t;
706
707 typedef struct xfs_dir2_free {
708 xfs_dir2_free_hdr_t hdr; /* block header */
709 __be16 bests[]; /* best free counts */
710 /* unused entries are -1 */
711 } xfs_dir2_free_t;
712
713 struct xfs_dir3_free_hdr {
714 struct xfs_dir3_blk_hdr hdr;
715 __be32 firstdb; /* db of first entry */
716 __be32 nvalid; /* count of valid entries */
717 __be32 nused; /* count of used entries */
718 __be32 pad; /* 64 bit alignment. */
719 };
720
721 struct xfs_dir3_free {
722 struct xfs_dir3_free_hdr hdr;
723 __be16 bests[]; /* best free counts */
724 /* unused entries are -1 */
725 };
726
727 #define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
728
729 /*
730 * In core version of the free block header, abstracted away from on-disk format
731 * differences. Use this in the code, and convert to/from the disk version using
732 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
733 */
734 struct xfs_dir3_icfree_hdr {
735 __uint32_t magic;
736 __uint32_t firstdb;
737 __uint32_t nvalid;
738 __uint32_t nused;
739
740 };
741
742 void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
743 struct xfs_dir2_free *from);
744
745 static inline int
746 xfs_dir3_free_hdr_size(struct xfs_mount *mp)
747 {
748 if (xfs_sb_version_hascrc(&mp->m_sb))
749 return sizeof(struct xfs_dir3_free_hdr);
750 return sizeof(struct xfs_dir2_free_hdr);
751 }
752
753 static inline int
754 xfs_dir3_free_max_bests(struct xfs_mount *mp)
755 {
756 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
757 sizeof(xfs_dir2_data_off_t);
758 }
759
760 static inline __be16 *
761 xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
762 {
763 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
764 }
765
766 /*
767 * Convert data space db to the corresponding free db.
768 */
769 static inline xfs_dir2_db_t
770 xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
771 {
772 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
773 }
774
775 /*
776 * Convert data space db to the corresponding index in a free db.
777 */
778 static inline int
779 xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
780 {
781 return db % xfs_dir3_free_max_bests(mp);
782 }
783
784 /*
785 * Single block format.
786 *
787 * The single block format looks like the following drawing on disk:
788 *
789 * +-------------------------------------------------+
790 * | xfs_dir2_data_hdr_t |
791 * +-------------------------------------------------+
792 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
793 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
794 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
795 * | ... |
796 * +-------------------------------------------------+
797 * | unused space |
798 * +-------------------------------------------------+
799 * | ... |
800 * | xfs_dir2_leaf_entry_t |
801 * | xfs_dir2_leaf_entry_t |
802 * +-------------------------------------------------+
803 * | xfs_dir2_block_tail_t |
804 * +-------------------------------------------------+
805 *
806 * As all the entries are variable size structures the accessors below should
807 * be used to iterate over them.
808 */
809
810 typedef struct xfs_dir2_block_tail {
811 __be32 count; /* count of leaf entries */
812 __be32 stale; /* count of stale lf entries */
813 } xfs_dir2_block_tail_t;
814
815 /*
816 * Pointer to the leaf header embedded in a data block (1-block format)
817 */
818 static inline struct xfs_dir2_block_tail *
819 xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
820 {
821 return ((struct xfs_dir2_block_tail *)
822 ((char *)hdr + mp->m_dirblksize)) - 1;
823 }
824
825 /*
826 * Pointer to the leaf entries embedded in a data block (1-block format)
827 */
828 static inline struct xfs_dir2_leaf_entry *
829 xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
830 {
831 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
832 }
833
834 #endif /* __XFS_DIR2_FORMAT_H__ */