blob: 7b986d334b33436c0d99cac0a04bac1cd003ca77 [file] [log] [blame]
Christoph Hellwig57926642011-07-13 13:43:48 +02001/*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Christoph Hellwig57926642011-07-13 13:43:48 +02004 * 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/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110040 * 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 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +110069#define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110070
71/*
Christoph Hellwig57926642011-07-13 13:43:48 +020072 * Byte offset in data block and shortform entry.
73 */
74typedef __uint16_t xfs_dir2_data_off_t;
75#define NULLDATAOFF 0xffffU
76typedef 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 */
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
96/*
97 * Directory block number (logical dirblk in file)
98 */
99typedef __uint32_t xfs_dir2_db_t;
100
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
153static inline xfs_dir2_data_aoff_t
154xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
155{
156 return get_unaligned_be16(&sfep->offset.i);
157}
158
159static inline void
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
165static inline int
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
175static inline struct xfs_dir2_sf_entry *
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
182static inline struct xfs_dir2_sf_entry *
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
230/*
231 * Offsets of . and .. in data space (always block 0)
232 */
233#define XFS_DIR2_DATA_DOT_OFFSET \
234 ((xfs_dir2_data_aoff_t)sizeof(struct xfs_dir2_data_hdr))
235#define XFS_DIR2_DATA_DOTDOT_OFFSET \
236 (XFS_DIR2_DATA_DOT_OFFSET + xfs_dir2_data_entsize(1))
237#define XFS_DIR2_DATA_FIRST_OFFSET \
238 (XFS_DIR2_DATA_DOTDOT_OFFSET + xfs_dir2_data_entsize(2))
239
240/*
241 * Describe a free area in the data block.
242 *
243 * The freespace will be formatted as a xfs_dir2_data_unused_t.
244 */
245typedef struct xfs_dir2_data_free {
246 __be16 offset; /* start of freespace */
247 __be16 length; /* length of freespace */
248} xfs_dir2_data_free_t;
249
250/*
251 * Header for the data blocks.
252 *
253 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
254 */
255typedef struct xfs_dir2_data_hdr {
256 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
257 /* XFS_DIR2_BLOCK_MAGIC */
258 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
259} xfs_dir2_data_hdr_t;
260
261/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100262 * define a structure for all the verification fields we are adding to the
263 * directory block structures. This will be used in several structures.
264 * The magic number must be the first entry to align with all the dir2
265 * structures so we determine how to decode them just by the magic number.
266 */
267struct xfs_dir3_blk_hdr {
268 __be32 magic; /* magic number */
269 __be32 crc; /* CRC of block */
270 __be64 blkno; /* first block of the buffer */
271 __be64 lsn; /* sequence number of last write */
272 uuid_t uuid; /* filesystem we belong to */
273 __be64 owner; /* inode that owns the block */
274};
275
276struct xfs_dir3_data_hdr {
277 struct xfs_dir3_blk_hdr hdr;
278 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
279};
280
281#define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
282
283static inline struct xfs_dir2_data_free *
284xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
285{
Dave Chinner33363fe2013-04-03 16:11:22 +1100286 if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
287 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100288 struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
289 return hdr3->best_free;
290 }
291 return hdr->bestfree;
292}
293
294/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200295 * Active entry in a data block.
296 *
297 * Aligned to 8 bytes. After the variable length name field there is a
298 * 2 byte tag field, which can be accessed using xfs_dir2_data_entry_tag_p.
299 */
300typedef struct xfs_dir2_data_entry {
301 __be64 inumber; /* inode number */
302 __u8 namelen; /* name length */
303 __u8 name[]; /* name bytes, no null */
304 /* __be16 tag; */ /* starting offset of us */
305} xfs_dir2_data_entry_t;
306
307/*
308 * Unused entry in a data block.
309 *
310 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
311 * using xfs_dir2_data_unused_tag_p.
312 */
313typedef struct xfs_dir2_data_unused {
314 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
315 __be16 length; /* total free length */
316 /* variable offset */
317 __be16 tag; /* starting offset of us */
318} xfs_dir2_data_unused_t;
319
320/*
321 * Size of a data entry.
322 */
323static inline int xfs_dir2_data_entsize(int n)
324{
325 return (int)roundup(offsetof(struct xfs_dir2_data_entry, name[0]) + n +
326 (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
327}
328
329/*
330 * Pointer to an entry's tag word.
331 */
332static inline __be16 *
333xfs_dir2_data_entry_tag_p(struct xfs_dir2_data_entry *dep)
334{
335 return (__be16 *)((char *)dep +
336 xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
337}
338
339/*
340 * Pointer to a freespace's tag word.
341 */
342static inline __be16 *
343xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
344{
345 return (__be16 *)((char *)dup +
346 be16_to_cpu(dup->length) - sizeof(__be16));
347}
348
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100349static inline size_t
350xfs_dir3_data_hdr_size(bool dir3)
351{
352 if (dir3)
353 return sizeof(struct xfs_dir3_data_hdr);
354 return sizeof(struct xfs_dir2_data_hdr);
355}
356
357static inline size_t
358xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
359{
360 bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
361 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
362 return xfs_dir3_data_hdr_size(dir3);
363}
364
365static inline struct xfs_dir2_data_entry *
366xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
367{
368 return (struct xfs_dir2_data_entry *)
369 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
370}
371
Dave Chinner33363fe2013-04-03 16:11:22 +1100372static inline struct xfs_dir2_data_unused *
373xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
374{
375 return (struct xfs_dir2_data_unused *)
376 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
377}
378
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100379/*
380 * Offsets of . and .. in data space (always block 0)
381 */
382static inline xfs_dir2_data_aoff_t
383xfs_dir3_data_dot_offset(struct xfs_dir2_data_hdr *hdr)
384{
385 return xfs_dir3_data_entry_offset(hdr);
386}
387
388static inline xfs_dir2_data_aoff_t
389xfs_dir3_data_dotdot_offset(struct xfs_dir2_data_hdr *hdr)
390{
391 return xfs_dir3_data_dot_offset(hdr) + xfs_dir2_data_entsize(1);
392}
393
394static inline xfs_dir2_data_aoff_t
395xfs_dir3_data_first_offset(struct xfs_dir2_data_hdr *hdr)
396{
397 return xfs_dir3_data_dotdot_offset(hdr) + xfs_dir2_data_entsize(2);
398}
399
400/*
401 * location of . and .. in data space (always block 0)
402 */
403static inline struct xfs_dir2_data_entry *
404xfs_dir3_data_dot_entry_p(struct xfs_dir2_data_hdr *hdr)
405{
406 return (struct xfs_dir2_data_entry *)
407 ((char *)hdr + xfs_dir3_data_dot_offset(hdr));
408}
409
410static inline struct xfs_dir2_data_entry *
411xfs_dir3_data_dotdot_entry_p(struct xfs_dir2_data_hdr *hdr)
412{
413 return (struct xfs_dir2_data_entry *)
414 ((char *)hdr + xfs_dir3_data_dotdot_offset(hdr));
415}
416
417static inline struct xfs_dir2_data_entry *
418xfs_dir3_data_first_entry_p(struct xfs_dir2_data_hdr *hdr)
419{
420 return (struct xfs_dir2_data_entry *)
421 ((char *)hdr + xfs_dir3_data_first_offset(hdr));
422}
423
Christoph Hellwig57926642011-07-13 13:43:48 +0200424/*
425 * Leaf block structures.
426 *
427 * A pure leaf block looks like the following drawing on disk:
428 *
429 * +---------------------------+
430 * | xfs_dir2_leaf_hdr_t |
431 * +---------------------------+
432 * | xfs_dir2_leaf_entry_t |
433 * | xfs_dir2_leaf_entry_t |
434 * | xfs_dir2_leaf_entry_t |
435 * | xfs_dir2_leaf_entry_t |
436 * | ... |
437 * +---------------------------+
438 * | xfs_dir2_data_off_t |
439 * | xfs_dir2_data_off_t |
440 * | xfs_dir2_data_off_t |
441 * | ... |
442 * +---------------------------+
443 * | xfs_dir2_leaf_tail_t |
444 * +---------------------------+
445 *
446 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
447 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
448 * for directories with separate leaf nodes and free space blocks
449 * (magic = XFS_DIR2_LEAFN_MAGIC).
450 *
451 * As all the entries are variable size structures the accessors below should
452 * be used to iterate over them.
453 */
454
455/*
456 * Offset of the leaf/node space. First block in this space
457 * is the btree root.
458 */
459#define XFS_DIR2_LEAF_SPACE 1
460#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
461#define XFS_DIR2_LEAF_FIRSTDB(mp) \
462 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
463
464/*
465 * Leaf block header.
466 */
467typedef struct xfs_dir2_leaf_hdr {
468 xfs_da_blkinfo_t info; /* header for da routines */
469 __be16 count; /* count of entries */
470 __be16 stale; /* count of stale entries */
471} xfs_dir2_leaf_hdr_t;
472
Dave Chinner24df33b2013-04-12 07:30:21 +1000473struct xfs_dir3_leaf_hdr {
474 struct xfs_da3_blkinfo info; /* header for da routines */
475 __be16 count; /* count of entries */
476 __be16 stale; /* count of stale entries */
477 __be32 pad;
478};
479
480struct xfs_dir3_icleaf_hdr {
481 __uint32_t forw;
482 __uint32_t back;
483 __uint16_t magic;
484 __uint16_t count;
485 __uint16_t stale;
486};
487
Christoph Hellwig57926642011-07-13 13:43:48 +0200488/*
489 * Leaf block entry.
490 */
491typedef struct xfs_dir2_leaf_entry {
492 __be32 hashval; /* hash value of name */
493 __be32 address; /* address of data entry */
494} xfs_dir2_leaf_entry_t;
495
496/*
497 * Leaf block tail.
498 */
499typedef struct xfs_dir2_leaf_tail {
500 __be32 bestcount;
501} xfs_dir2_leaf_tail_t;
502
503/*
504 * Leaf block.
505 */
506typedef struct xfs_dir2_leaf {
Dave Chinner24df33b2013-04-12 07:30:21 +1000507 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
508 xfs_dir2_leaf_entry_t __ents[]; /* entries */
Christoph Hellwig57926642011-07-13 13:43:48 +0200509} xfs_dir2_leaf_t;
510
Dave Chinner24df33b2013-04-12 07:30:21 +1000511struct xfs_dir3_leaf {
512 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
513 struct xfs_dir2_leaf_entry __ents[]; /* entries */
514};
Christoph Hellwig57926642011-07-13 13:43:48 +0200515
Dave Chinner24df33b2013-04-12 07:30:21 +1000516#define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
517
518static inline int
519xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
Christoph Hellwig57926642011-07-13 13:43:48 +0200520{
Dave Chinner24df33b2013-04-12 07:30:21 +1000521 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
522 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
523 return sizeof(struct xfs_dir3_leaf_hdr);
524 return sizeof(struct xfs_dir2_leaf_hdr);
525}
526
527static inline int
528xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
529{
530 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
Christoph Hellwig57926642011-07-13 13:43:48 +0200531 (uint)sizeof(struct xfs_dir2_leaf_entry);
532}
533
534/*
535 * Get address of the bestcount field in the single-leaf block.
536 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000537static inline struct xfs_dir2_leaf_entry *
538xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
539{
540 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
541 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
542 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
543 return lp3->__ents;
544 }
545 return lp->__ents;
546}
547
548/*
549 * Get address of the bestcount field in the single-leaf block.
550 */
Christoph Hellwig57926642011-07-13 13:43:48 +0200551static inline struct xfs_dir2_leaf_tail *
552xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
553{
554 return (struct xfs_dir2_leaf_tail *)
555 ((char *)lp + mp->m_dirblksize -
556 sizeof(struct xfs_dir2_leaf_tail));
557}
558
559/*
560 * Get address of the bests array in the single-leaf block.
561 */
562static inline __be16 *
563xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
564{
565 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
566}
567
568/*
Dave Chinner24df33b2013-04-12 07:30:21 +1000569 * DB blocks here are logical directory block numbers, not filesystem blocks.
570 */
571
572/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200573 * Convert dataptr to byte in file space
574 */
575static inline xfs_dir2_off_t
576xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
577{
578 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
579}
580
581/*
582 * Convert byte in file space to dataptr. It had better be aligned.
583 */
584static inline xfs_dir2_dataptr_t
585xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
586{
587 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
588}
589
590/*
591 * Convert byte in space to (DB) block
592 */
593static inline xfs_dir2_db_t
594xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
595{
596 return (xfs_dir2_db_t)
597 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
598}
599
600/*
601 * Convert dataptr to a block number
602 */
603static inline xfs_dir2_db_t
604xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
605{
606 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
607}
608
609/*
610 * Convert byte in space to offset in a block
611 */
612static inline xfs_dir2_data_aoff_t
613xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
614{
615 return (xfs_dir2_data_aoff_t)(by &
616 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
617}
618
619/*
620 * Convert dataptr to a byte offset in a block
621 */
622static inline xfs_dir2_data_aoff_t
623xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
624{
625 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
626}
627
628/*
629 * Convert block and offset to byte in space
630 */
631static inline xfs_dir2_off_t
632xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
633 xfs_dir2_data_aoff_t o)
634{
635 return ((xfs_dir2_off_t)db <<
636 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
637}
638
639/*
640 * Convert block (DB) to block (dablk)
641 */
642static inline xfs_dablk_t
643xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
644{
645 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
646}
647
648/*
649 * Convert byte in space to (DA) block
650 */
651static inline xfs_dablk_t
652xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
653{
654 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
655}
656
657/*
658 * Convert block and offset to dataptr
659 */
660static inline xfs_dir2_dataptr_t
661xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
662 xfs_dir2_data_aoff_t o)
663{
664 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
665}
666
667/*
668 * Convert block (dablk) to block (DB)
669 */
670static inline xfs_dir2_db_t
671xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
672{
673 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
674}
675
676/*
677 * Convert block (dablk) to byte offset in space
678 */
679static inline xfs_dir2_off_t
680xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
681{
682 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
683}
684
685/*
686 * Free space block defintions for the node format.
687 */
688
689/*
690 * Offset of the freespace index.
691 */
692#define XFS_DIR2_FREE_SPACE 2
693#define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
694#define XFS_DIR2_FREE_FIRSTDB(mp) \
695 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
696
697typedef struct xfs_dir2_free_hdr {
698 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
699 __be32 firstdb; /* db of first entry */
700 __be32 nvalid; /* count of valid entries */
701 __be32 nused; /* count of used entries */
702} xfs_dir2_free_hdr_t;
703
704typedef struct xfs_dir2_free {
705 xfs_dir2_free_hdr_t hdr; /* block header */
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200706 __be16 bests[]; /* best free counts */
Christoph Hellwig57926642011-07-13 13:43:48 +0200707 /* unused entries are -1 */
708} xfs_dir2_free_t;
709
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100710struct xfs_dir3_free_hdr {
711 struct xfs_dir3_blk_hdr hdr;
712 __be32 firstdb; /* db of first entry */
713 __be32 nvalid; /* count of valid entries */
714 __be32 nused; /* count of used entries */
715};
716
717struct xfs_dir3_free {
718 struct xfs_dir3_free_hdr hdr;
719 __be16 bests[]; /* best free counts */
720 /* unused entries are -1 */
721};
722
723#define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
724
725/*
726 * In core version of the free block header, abstracted away from on-disk format
727 * differences. Use this in the code, and convert to/from the disk version using
728 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
729 */
730struct xfs_dir3_icfree_hdr {
731 __uint32_t magic;
732 __uint32_t firstdb;
733 __uint32_t nvalid;
734 __uint32_t nused;
735
736};
737
738void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
739 struct xfs_dir2_free *from);
740
741static inline int
742xfs_dir3_free_hdr_size(struct xfs_mount *mp)
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200743{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100744 if (xfs_sb_version_hascrc(&mp->m_sb))
745 return sizeof(struct xfs_dir3_free_hdr);
746 return sizeof(struct xfs_dir2_free_hdr);
747}
748
749static inline int
750xfs_dir3_free_max_bests(struct xfs_mount *mp)
751{
752 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200753 sizeof(xfs_dir2_data_off_t);
754}
Christoph Hellwig57926642011-07-13 13:43:48 +0200755
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100756static inline __be16 *
757xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
758{
759 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
760}
761
Christoph Hellwig57926642011-07-13 13:43:48 +0200762/*
763 * Convert data space db to the corresponding free db.
764 */
765static inline xfs_dir2_db_t
766xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
767{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100768 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
Christoph Hellwig57926642011-07-13 13:43:48 +0200769}
770
771/*
772 * Convert data space db to the corresponding index in a free db.
773 */
774static inline int
775xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
776{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100777 return db % xfs_dir3_free_max_bests(mp);
Christoph Hellwig57926642011-07-13 13:43:48 +0200778}
779
780/*
781 * Single block format.
782 *
783 * The single block format looks like the following drawing on disk:
784 *
785 * +-------------------------------------------------+
786 * | xfs_dir2_data_hdr_t |
787 * +-------------------------------------------------+
788 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
789 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
790 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
791 * | ... |
792 * +-------------------------------------------------+
793 * | unused space |
794 * +-------------------------------------------------+
795 * | ... |
796 * | xfs_dir2_leaf_entry_t |
797 * | xfs_dir2_leaf_entry_t |
798 * +-------------------------------------------------+
799 * | xfs_dir2_block_tail_t |
800 * +-------------------------------------------------+
801 *
802 * As all the entries are variable size structures the accessors below should
803 * be used to iterate over them.
804 */
805
806typedef struct xfs_dir2_block_tail {
807 __be32 count; /* count of leaf entries */
808 __be32 stale; /* count of stale lf entries */
809} xfs_dir2_block_tail_t;
810
811/*
812 * Pointer to the leaf header embedded in a data block (1-block format)
813 */
814static inline struct xfs_dir2_block_tail *
815xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
816{
817 return ((struct xfs_dir2_block_tail *)
818 ((char *)hdr + mp->m_dirblksize)) - 1;
819}
820
821/*
822 * Pointer to the leaf entries embedded in a data block (1-block format)
823 */
824static inline struct xfs_dir2_leaf_entry *
825xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
826{
827 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
828}
829
830#endif /* __XFS_DIR2_FORMAT_H__ */