blob: a19d3f8f639cf3eb3e95d23bc52b58d5eb9b6591 [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 */
Dave Chinner57062782013-10-15 09:17:51 +110019#ifndef __XFS_DA_FORMAT_H__
20#define __XFS_DA_FORMAT_H__
21
22/*========================================================================
23 * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
24 *========================================================================*/
25
26/*
27 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
28 *
29 * It is used to manage a doubly linked list of all blocks at the same
30 * level in the Btree, and to identify which type of block this is.
31 */
32#define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
33#define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
34#define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
35#define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
36
37typedef struct xfs_da_blkinfo {
38 __be32 forw; /* previous block in list */
39 __be32 back; /* following block in list */
40 __be16 magic; /* validity check on block */
41 __be16 pad; /* unused */
42} xfs_da_blkinfo_t;
43
44/*
45 * CRC enabled directory structure types
46 *
47 * The headers change size for the additional verification information, but
48 * otherwise the tree layouts and contents are unchanged. Hence the da btree
49 * code can use the struct xfs_da_blkinfo for manipulating the tree links and
50 * magic numbers without modification for both v2 and v3 nodes.
51 */
52#define XFS_DA3_NODE_MAGIC 0x3ebe /* magic number: non-leaf blocks */
53#define XFS_ATTR3_LEAF_MAGIC 0x3bee /* magic number: attribute leaf blks */
54#define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
55#define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
56
57struct xfs_da3_blkinfo {
58 /*
59 * the node link manipulation code relies on the fact that the first
60 * element of this structure is the struct xfs_da_blkinfo so it can
61 * ignore the differences in the rest of the structures.
62 */
63 struct xfs_da_blkinfo hdr;
64 __be32 crc; /* CRC of block */
65 __be64 blkno; /* first block of the buffer */
66 __be64 lsn; /* sequence number of last write */
67 uuid_t uuid; /* filesystem we belong to */
68 __be64 owner; /* inode that owns the block */
69};
70
71/*
72 * This is the structure of the root and intermediate nodes in the Btree.
73 * The leaf nodes are defined above.
74 *
75 * Entries are not packed.
76 *
77 * Since we have duplicate keys, use a binary search but always follow
78 * all match in the block, not just the first match found.
79 */
80#define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
81
82typedef struct xfs_da_node_hdr {
83 struct xfs_da_blkinfo info; /* block type, links, etc. */
84 __be16 __count; /* count of active entries */
85 __be16 __level; /* level above leaves (leaf == 0) */
86} xfs_da_node_hdr_t;
87
88struct xfs_da3_node_hdr {
89 struct xfs_da3_blkinfo info; /* block type, links, etc. */
90 __be16 __count; /* count of active entries */
91 __be16 __level; /* level above leaves (leaf == 0) */
92 __be32 __pad32;
93};
94
95#define XFS_DA3_NODE_CRC_OFF (offsetof(struct xfs_da3_node_hdr, info.crc))
96
97typedef struct xfs_da_node_entry {
98 __be32 hashval; /* hash value for this descendant */
99 __be32 before; /* Btree block before this key */
100} xfs_da_node_entry_t;
101
102typedef struct xfs_da_intnode {
103 struct xfs_da_node_hdr hdr;
104 struct xfs_da_node_entry __btree[];
105} xfs_da_intnode_t;
106
107struct xfs_da3_intnode {
108 struct xfs_da3_node_hdr hdr;
109 struct xfs_da_node_entry __btree[];
110};
111
112/*
113 * In-core version of the node header to abstract the differences in the v2 and
114 * v3 disk format of the headers. Callers need to convert to/from disk format as
115 * appropriate.
116 */
117struct xfs_da3_icnode_hdr {
118 __uint32_t forw;
119 __uint32_t back;
120 __uint16_t magic;
121 __uint16_t count;
122 __uint16_t level;
123};
124
Dave Chinner57062782013-10-15 09:17:51 +1100125#define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
Christoph Hellwig57926642011-07-13 13:43:48 +0200126
127/*
128 * Directory version 2.
129 *
130 * There are 4 possible formats:
131 * - shortform - embedded into the inode
132 * - single block - data with embedded leaf at the end
133 * - multiple data blocks, single leaf+freeindex block
134 * - data blocks, node and leaf blocks (btree), freeindex blocks
135 *
136 * Note: many node blocks structures and constants are shared with the attr
137 * code and defined in xfs_da_btree.h.
138 */
139
140#define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
141#define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
142#define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
143
144/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100145 * Directory Version 3 With CRCs.
146 *
147 * The tree formats are the same as for version 2 directories. The difference
148 * is in the block header and dirent formats. In many cases the v3 structures
149 * use v2 definitions as they are no different and this makes code sharing much
150 * easier.
151 *
152 * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
153 * format is v2 then they switch to the existing v2 code, or the format is v3
154 * they implement the v3 functionality. This means the existing dir2 is a mix of
155 * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
156 * where there is a difference in the formats, otherwise the code is unchanged.
157 *
158 * Where it is possible, the code decides what to do based on the magic numbers
159 * in the blocks rather than feature bits in the superblock. This means the code
160 * is as independent of the external XFS code as possible as doesn't require
161 * passing struct xfs_mount pointers into places where it isn't really
162 * necessary.
163 *
164 * Version 3 includes:
165 *
166 * - a larger block header for CRC and identification purposes and so the
167 * offsets of all the structures inside the blocks are different.
168 *
169 * - new magic numbers to be able to detect the v2/v3 types on the fly.
170 */
171
172#define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
173#define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100174#define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100175
176/*
Dave Chinner0cb97762013-08-12 20:50:09 +1000177 * Dirents in version 3 directories have a file type field. Additions to this
178 * list are an on-disk format change, requiring feature bits. Valid values
179 * are as follows:
180 */
181#define XFS_DIR3_FT_UNKNOWN 0
182#define XFS_DIR3_FT_REG_FILE 1
183#define XFS_DIR3_FT_DIR 2
184#define XFS_DIR3_FT_CHRDEV 3
185#define XFS_DIR3_FT_BLKDEV 4
186#define XFS_DIR3_FT_FIFO 5
187#define XFS_DIR3_FT_SOCK 6
188#define XFS_DIR3_FT_SYMLINK 7
189#define XFS_DIR3_FT_WHT 8
190
191#define XFS_DIR3_FT_MAX 9
192
193/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200194 * Byte offset in data block and shortform entry.
195 */
196typedef __uint16_t xfs_dir2_data_off_t;
197#define NULLDATAOFF 0xffffU
198typedef uint xfs_dir2_data_aoff_t; /* argument form */
199
200/*
201 * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
202 * Only need 16 bits, this is the byte offset into the single block form.
203 */
204typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
205
206/*
207 * Offset in data space of a data entry.
208 */
209typedef __uint32_t xfs_dir2_dataptr_t;
210#define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
211#define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
212
213/*
214 * Byte offset in a directory.
215 */
216typedef xfs_off_t xfs_dir2_off_t;
217
218/*
219 * Directory block number (logical dirblk in file)
220 */
221typedef __uint32_t xfs_dir2_db_t;
222
223/*
224 * Inode number stored as 8 8-bit values.
225 */
226typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
227
228/*
229 * Inode number stored as 4 8-bit values.
230 * Works a lot of the time, when all the inode numbers in a directory
231 * fit in 32 bits.
232 */
233typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
234
235typedef union {
236 xfs_dir2_ino8_t i8;
237 xfs_dir2_ino4_t i4;
238} xfs_dir2_inou_t;
239#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
240
241/*
242 * Directory layout when stored internal to an inode.
243 *
244 * Small directories are packed as tightly as possible so as to fit into the
245 * literal area of the inode. These "shortform" directories consist of a
246 * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
247 * structures. Due the different inode number storage size and the variable
248 * length name field in the xfs_dir2_sf_entry all these structure are
249 * variable length, and the accessors in this file should be used to iterate
250 * over them.
251 */
252typedef struct xfs_dir2_sf_hdr {
253 __uint8_t count; /* count of entries */
254 __uint8_t i8count; /* count of 8-byte inode #s */
255 xfs_dir2_inou_t parent; /* parent dir inode number */
256} __arch_pack xfs_dir2_sf_hdr_t;
257
258typedef struct xfs_dir2_sf_entry {
259 __u8 namelen; /* actual name length */
260 xfs_dir2_sf_off_t offset; /* saved offset */
261 __u8 name[]; /* name, variable size */
262 /*
Dave Chinner0cb97762013-08-12 20:50:09 +1000263 * A single byte containing the file type field follows the inode
264 * number for version 3 directory entries.
265 *
Christoph Hellwig57926642011-07-13 13:43:48 +0200266 * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
267 * variable offset after the name.
268 */
269} __arch_pack xfs_dir2_sf_entry_t;
270
271static inline int xfs_dir2_sf_hdr_size(int i8count)
272{
273 return sizeof(struct xfs_dir2_sf_hdr) -
274 (i8count == 0) *
275 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
276}
277
278static inline xfs_dir2_data_aoff_t
279xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
280{
281 return get_unaligned_be16(&sfep->offset.i);
282}
283
284static inline void
285xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
286{
287 put_unaligned_be16(off, &sfep->offset.i);
288}
289
Christoph Hellwig57926642011-07-13 13:43:48 +0200290static inline struct xfs_dir2_sf_entry *
291xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
292{
293 return (struct xfs_dir2_sf_entry *)
294 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
295}
296
Dave Chinner0cb97762013-08-12 20:50:09 +1000297/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200298 * Data block structures.
299 *
300 * A pure data block looks like the following drawing on disk:
301 *
302 * +-------------------------------------------------+
303 * | xfs_dir2_data_hdr_t |
304 * +-------------------------------------------------+
305 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
306 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
307 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
308 * | ... |
309 * +-------------------------------------------------+
310 * | unused space |
311 * +-------------------------------------------------+
312 *
313 * As all the entries are variable size structures the accessors below should
314 * be used to iterate over them.
315 *
316 * In addition to the pure data blocks for the data and node formats,
317 * most structures are also used for the combined data/freespace "block"
318 * format below.
319 */
320
321#define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
322#define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
323#define XFS_DIR2_DATA_FREE_TAG 0xffff
324#define XFS_DIR2_DATA_FD_COUNT 3
325
326/*
327 * Directory address space divided into sections,
328 * spaces separated by 32GB.
329 */
330#define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
331#define XFS_DIR2_DATA_SPACE 0
332#define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
333#define XFS_DIR2_DATA_FIRSTDB(mp) \
334 xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
335
336/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200337 * Describe a free area in the data block.
338 *
339 * The freespace will be formatted as a xfs_dir2_data_unused_t.
340 */
341typedef struct xfs_dir2_data_free {
342 __be16 offset; /* start of freespace */
343 __be16 length; /* length of freespace */
344} xfs_dir2_data_free_t;
345
346/*
347 * Header for the data blocks.
348 *
349 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
350 */
351typedef struct xfs_dir2_data_hdr {
352 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
353 /* XFS_DIR2_BLOCK_MAGIC */
354 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
355} xfs_dir2_data_hdr_t;
356
357/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100358 * define a structure for all the verification fields we are adding to the
359 * directory block structures. This will be used in several structures.
360 * The magic number must be the first entry to align with all the dir2
361 * structures so we determine how to decode them just by the magic number.
362 */
363struct xfs_dir3_blk_hdr {
364 __be32 magic; /* magic number */
365 __be32 crc; /* CRC of block */
366 __be64 blkno; /* first block of the buffer */
367 __be64 lsn; /* sequence number of last write */
368 uuid_t uuid; /* filesystem we belong to */
369 __be64 owner; /* inode that owns the block */
370};
371
372struct xfs_dir3_data_hdr {
373 struct xfs_dir3_blk_hdr hdr;
374 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
Dave Chinner51707112013-06-12 12:19:07 +1000375 __be32 pad; /* 64 bit alignment */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100376};
377
378#define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
379
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100380/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200381 * Active entry in a data block.
382 *
383 * Aligned to 8 bytes. After the variable length name field there is a
Dave Chinner0cb97762013-08-12 20:50:09 +1000384 * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
385 *
386 * For dir3 structures, there is file type field between the name and the tag.
387 * This can only be manipulated by helper functions. It is packed hard against
388 * the end of the name so any padding for rounding is between the file type and
389 * the tag.
Christoph Hellwig57926642011-07-13 13:43:48 +0200390 */
391typedef struct xfs_dir2_data_entry {
392 __be64 inumber; /* inode number */
393 __u8 namelen; /* name length */
394 __u8 name[]; /* name bytes, no null */
Dave Chinner0cb97762013-08-12 20:50:09 +1000395 /* __u8 filetype; */ /* type of inode we point to */
Christoph Hellwig57926642011-07-13 13:43:48 +0200396 /* __be16 tag; */ /* starting offset of us */
397} xfs_dir2_data_entry_t;
398
399/*
400 * Unused entry in a data block.
401 *
402 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
403 * using xfs_dir2_data_unused_tag_p.
404 */
405typedef struct xfs_dir2_data_unused {
406 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
407 __be16 length; /* total free length */
408 /* variable offset */
409 __be16 tag; /* starting offset of us */
410} xfs_dir2_data_unused_t;
411
412/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200413 * Pointer to a freespace's tag word.
414 */
415static inline __be16 *
416xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
417{
418 return (__be16 *)((char *)dup +
419 be16_to_cpu(dup->length) - sizeof(__be16));
420}
421
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100422/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200423 * Leaf block structures.
424 *
425 * A pure leaf block looks like the following drawing on disk:
426 *
427 * +---------------------------+
428 * | xfs_dir2_leaf_hdr_t |
429 * +---------------------------+
430 * | xfs_dir2_leaf_entry_t |
431 * | xfs_dir2_leaf_entry_t |
432 * | xfs_dir2_leaf_entry_t |
433 * | xfs_dir2_leaf_entry_t |
434 * | ... |
435 * +---------------------------+
436 * | xfs_dir2_data_off_t |
437 * | xfs_dir2_data_off_t |
438 * | xfs_dir2_data_off_t |
439 * | ... |
440 * +---------------------------+
441 * | xfs_dir2_leaf_tail_t |
442 * +---------------------------+
443 *
444 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
445 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
446 * for directories with separate leaf nodes and free space blocks
447 * (magic = XFS_DIR2_LEAFN_MAGIC).
448 *
449 * As all the entries are variable size structures the accessors below should
450 * be used to iterate over them.
451 */
452
453/*
454 * Offset of the leaf/node space. First block in this space
455 * is the btree root.
456 */
457#define XFS_DIR2_LEAF_SPACE 1
458#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
459#define XFS_DIR2_LEAF_FIRSTDB(mp) \
460 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
461
462/*
463 * Leaf block header.
464 */
465typedef struct xfs_dir2_leaf_hdr {
466 xfs_da_blkinfo_t info; /* header for da routines */
467 __be16 count; /* count of entries */
468 __be16 stale; /* count of stale entries */
469} xfs_dir2_leaf_hdr_t;
470
Dave Chinner24df33b2013-04-12 07:30:21 +1000471struct xfs_dir3_leaf_hdr {
472 struct xfs_da3_blkinfo info; /* header for da routines */
473 __be16 count; /* count of entries */
474 __be16 stale; /* count of stale entries */
Dave Chinner51707112013-06-12 12:19:07 +1000475 __be32 pad; /* 64 bit alignment */
Dave Chinner24df33b2013-04-12 07:30:21 +1000476};
477
478struct xfs_dir3_icleaf_hdr {
479 __uint32_t forw;
480 __uint32_t back;
481 __uint16_t magic;
482 __uint16_t count;
483 __uint16_t stale;
484};
485
Christoph Hellwig57926642011-07-13 13:43:48 +0200486/*
487 * Leaf block entry.
488 */
489typedef struct xfs_dir2_leaf_entry {
490 __be32 hashval; /* hash value of name */
491 __be32 address; /* address of data entry */
492} xfs_dir2_leaf_entry_t;
493
494/*
495 * Leaf block tail.
496 */
497typedef struct xfs_dir2_leaf_tail {
498 __be32 bestcount;
499} xfs_dir2_leaf_tail_t;
500
501/*
502 * Leaf block.
503 */
504typedef struct xfs_dir2_leaf {
Dave Chinner24df33b2013-04-12 07:30:21 +1000505 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
506 xfs_dir2_leaf_entry_t __ents[]; /* entries */
Christoph Hellwig57926642011-07-13 13:43:48 +0200507} xfs_dir2_leaf_t;
508
Dave Chinner24df33b2013-04-12 07:30:21 +1000509struct xfs_dir3_leaf {
510 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
511 struct xfs_dir2_leaf_entry __ents[]; /* entries */
512};
Christoph Hellwig57926642011-07-13 13:43:48 +0200513
Dave Chinner24df33b2013-04-12 07:30:21 +1000514#define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
515
Dave Chinner24df33b2013-04-12 07:30:21 +1000516/*
517 * Get address of the bestcount field in the single-leaf block.
518 */
Christoph Hellwig57926642011-07-13 13:43:48 +0200519static inline struct xfs_dir2_leaf_tail *
520xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
521{
522 return (struct xfs_dir2_leaf_tail *)
523 ((char *)lp + mp->m_dirblksize -
524 sizeof(struct xfs_dir2_leaf_tail));
525}
526
527/*
528 * Get address of the bests array in the single-leaf block.
529 */
530static inline __be16 *
531xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
532{
533 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
534}
535
536/*
Dave Chinner24df33b2013-04-12 07:30:21 +1000537 * DB blocks here are logical directory block numbers, not filesystem blocks.
538 */
539
540/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200541 * Convert dataptr to byte in file space
542 */
543static inline xfs_dir2_off_t
544xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
545{
546 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
547}
548
549/*
550 * Convert byte in file space to dataptr. It had better be aligned.
551 */
552static inline xfs_dir2_dataptr_t
553xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
554{
555 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
556}
557
558/*
559 * Convert byte in space to (DB) block
560 */
561static inline xfs_dir2_db_t
562xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
563{
564 return (xfs_dir2_db_t)
565 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
566}
567
568/*
569 * Convert dataptr to a block number
570 */
571static inline xfs_dir2_db_t
572xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
573{
574 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
575}
576
577/*
578 * Convert byte in space to offset in a block
579 */
580static inline xfs_dir2_data_aoff_t
581xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
582{
583 return (xfs_dir2_data_aoff_t)(by &
584 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
585}
586
587/*
588 * Convert dataptr to a byte offset in a block
589 */
590static inline xfs_dir2_data_aoff_t
591xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
592{
593 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
594}
595
596/*
597 * Convert block and offset to byte in space
598 */
599static inline xfs_dir2_off_t
600xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
601 xfs_dir2_data_aoff_t o)
602{
603 return ((xfs_dir2_off_t)db <<
604 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
605}
606
607/*
608 * Convert block (DB) to block (dablk)
609 */
610static inline xfs_dablk_t
611xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
612{
613 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
614}
615
616/*
617 * Convert byte in space to (DA) block
618 */
619static inline xfs_dablk_t
620xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
621{
622 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
623}
624
625/*
626 * Convert block and offset to dataptr
627 */
628static inline xfs_dir2_dataptr_t
629xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
630 xfs_dir2_data_aoff_t o)
631{
632 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
633}
634
635/*
636 * Convert block (dablk) to block (DB)
637 */
638static inline xfs_dir2_db_t
639xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
640{
641 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
642}
643
644/*
645 * Convert block (dablk) to byte offset in space
646 */
647static inline xfs_dir2_off_t
648xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
649{
650 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
651}
652
653/*
654 * Free space block defintions for the node format.
655 */
656
657/*
658 * Offset of the freespace index.
659 */
660#define XFS_DIR2_FREE_SPACE 2
661#define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
662#define XFS_DIR2_FREE_FIRSTDB(mp) \
663 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
664
665typedef struct xfs_dir2_free_hdr {
666 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
667 __be32 firstdb; /* db of first entry */
668 __be32 nvalid; /* count of valid entries */
669 __be32 nused; /* count of used entries */
670} xfs_dir2_free_hdr_t;
671
672typedef struct xfs_dir2_free {
673 xfs_dir2_free_hdr_t hdr; /* block header */
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200674 __be16 bests[]; /* best free counts */
Christoph Hellwig57926642011-07-13 13:43:48 +0200675 /* unused entries are -1 */
676} xfs_dir2_free_t;
677
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100678struct xfs_dir3_free_hdr {
679 struct xfs_dir3_blk_hdr hdr;
680 __be32 firstdb; /* db of first entry */
681 __be32 nvalid; /* count of valid entries */
682 __be32 nused; /* count of used entries */
Dave Chinner51707112013-06-12 12:19:07 +1000683 __be32 pad; /* 64 bit alignment */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100684};
685
686struct xfs_dir3_free {
687 struct xfs_dir3_free_hdr hdr;
688 __be16 bests[]; /* best free counts */
689 /* unused entries are -1 */
690};
691
692#define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
693
694/*
695 * In core version of the free block header, abstracted away from on-disk format
696 * differences. Use this in the code, and convert to/from the disk version using
697 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
698 */
699struct xfs_dir3_icfree_hdr {
700 __uint32_t magic;
701 __uint32_t firstdb;
702 __uint32_t nvalid;
703 __uint32_t nused;
704
705};
706
Christoph Hellwig57926642011-07-13 13:43:48 +0200707/*
708 * Single block format.
709 *
710 * The single block format looks like the following drawing on disk:
711 *
712 * +-------------------------------------------------+
713 * | xfs_dir2_data_hdr_t |
714 * +-------------------------------------------------+
715 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
716 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
717 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
718 * | ... |
719 * +-------------------------------------------------+
720 * | unused space |
721 * +-------------------------------------------------+
722 * | ... |
723 * | xfs_dir2_leaf_entry_t |
724 * | xfs_dir2_leaf_entry_t |
725 * +-------------------------------------------------+
726 * | xfs_dir2_block_tail_t |
727 * +-------------------------------------------------+
728 *
729 * As all the entries are variable size structures the accessors below should
730 * be used to iterate over them.
731 */
732
733typedef struct xfs_dir2_block_tail {
734 __be32 count; /* count of leaf entries */
735 __be32 stale; /* count of stale lf entries */
736} xfs_dir2_block_tail_t;
737
738/*
739 * Pointer to the leaf header embedded in a data block (1-block format)
740 */
741static inline struct xfs_dir2_block_tail *
742xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
743{
744 return ((struct xfs_dir2_block_tail *)
745 ((char *)hdr + mp->m_dirblksize)) - 1;
746}
747
748/*
749 * Pointer to the leaf entries embedded in a data block (1-block format)
750 */
751static inline struct xfs_dir2_leaf_entry *
752xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
753{
754 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
755}
756
Dave Chinner57062782013-10-15 09:17:51 +1100757
758/*
759 * Attribute storage layout
760 *
761 * Attribute lists are structured around Btrees where all the data
762 * elements are in the leaf nodes. Attribute names are hashed into an int,
763 * then that int is used as the index into the Btree. Since the hashval
764 * of an attribute name may not be unique, we may have duplicate keys. The
765 * internal links in the Btree are logical block offsets into the file.
766 *
767 *========================================================================
768 * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
769 *========================================================================
770 *
771 * Struct leaf_entry's are packed from the top. Name/values grow from the
772 * bottom but are not packed. The freemap contains run-length-encoded entries
773 * for the free bytes after the leaf_entry's, but only the N largest such,
774 * smaller runs are dropped. When the freemap doesn't show enough space
775 * for an allocation, we compact the name/value area and try again. If we
776 * still don't have enough space, then we have to split the block. The
777 * name/value structs (both local and remote versions) must be 32bit aligned.
778 *
779 * Since we have duplicate hash keys, for each key that matches, compare
780 * the actual name string. The root and intermediate node search always
781 * takes the first-in-the-block key match found, so we should only have
782 * to work "forw"ard. If none matches, continue with the "forw"ard leaf
783 * nodes until the hash key changes or the attribute name is found.
784 *
785 * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
786 * the leaf_entry. The namespaces are independent only because we also look
787 * at the namespace bit when we are looking for a matching attribute name.
788 *
789 * We also store an "incomplete" bit in the leaf_entry. It shows that an
790 * attribute is in the middle of being created and should not be shown to
791 * the user if we crash during the time that the bit is set. We clear the
792 * bit when we have finished setting up the attribute. We do this because
793 * we cannot create some large attributes inside a single transaction, and we
794 * need some indication that we weren't finished if we crash in the middle.
795 */
796#define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
797
798typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
799 __be16 base; /* base of free region */
800 __be16 size; /* length of free region */
801} xfs_attr_leaf_map_t;
802
803typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
804 xfs_da_blkinfo_t info; /* block type, links, etc. */
805 __be16 count; /* count of active leaf_entry's */
806 __be16 usedbytes; /* num bytes of names/values stored */
807 __be16 firstused; /* first used byte in name area */
808 __u8 holes; /* != 0 if blk needs compaction */
809 __u8 pad1;
810 xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
811 /* N largest free regions */
812} xfs_attr_leaf_hdr_t;
813
814typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
815 __be32 hashval; /* hash value of name */
816 __be16 nameidx; /* index into buffer of name/value */
817 __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
818 __u8 pad2; /* unused pad byte */
819} xfs_attr_leaf_entry_t;
820
821typedef struct xfs_attr_leaf_name_local {
822 __be16 valuelen; /* number of bytes in value */
823 __u8 namelen; /* length of name bytes */
824 __u8 nameval[1]; /* name/value bytes */
825} xfs_attr_leaf_name_local_t;
826
827typedef struct xfs_attr_leaf_name_remote {
828 __be32 valueblk; /* block number of value bytes */
829 __be32 valuelen; /* number of bytes in value */
830 __u8 namelen; /* length of name bytes */
831 __u8 name[1]; /* name bytes */
832} xfs_attr_leaf_name_remote_t;
833
834typedef struct xfs_attr_leafblock {
835 xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
836 xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
837 xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
838 xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
839} xfs_attr_leafblock_t;
840
841/*
842 * CRC enabled leaf structures. Called "version 3" structures to match the
843 * version number of the directory and dablk structures for this feature, and
844 * attr2 is already taken by the variable inode attribute fork size feature.
845 */
846struct xfs_attr3_leaf_hdr {
847 struct xfs_da3_blkinfo info;
848 __be16 count;
849 __be16 usedbytes;
850 __be16 firstused;
851 __u8 holes;
852 __u8 pad1;
853 struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
854 __be32 pad2; /* 64 bit alignment */
855};
856
857#define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
858
859struct xfs_attr3_leafblock {
860 struct xfs_attr3_leaf_hdr hdr;
861 struct xfs_attr_leaf_entry entries[1];
862
863 /*
864 * The rest of the block contains the following structures after the
865 * leaf entries, growing from the bottom up. The variables are never
866 * referenced, the locations accessed purely from helper functions.
867 *
868 * struct xfs_attr_leaf_name_local
869 * struct xfs_attr_leaf_name_remote
870 */
871};
872
873/*
874 * incore, neutral version of the attribute leaf header
875 */
876struct xfs_attr3_icleaf_hdr {
877 __uint32_t forw;
878 __uint32_t back;
879 __uint16_t magic;
880 __uint16_t count;
881 __uint16_t usedbytes;
882 __uint16_t firstused;
883 __u8 holes;
884 struct {
885 __uint16_t base;
886 __uint16_t size;
887 } freemap[XFS_ATTR_LEAF_MAPSIZE];
888};
889
890/*
891 * Flags used in the leaf_entry[i].flags field.
892 * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
893 * on the system call, they are "or"ed together for various operations.
894 */
895#define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
896#define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
897#define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
898#define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
899#define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
900#define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
901#define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
902#define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
903
904/*
905 * Conversion macros for converting namespace bits from argument flags
906 * to ondisk flags.
907 */
908#define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
909#define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
910#define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
911#define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
912#define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
913 ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
914#define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
915 ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
916
917/*
918 * Alignment for namelist and valuelist entries (since they are mixed
919 * there can be only one alignment value)
920 */
921#define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
922
923static inline int
924xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
925{
926 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
927 return sizeof(struct xfs_attr3_leaf_hdr);
928 return sizeof(struct xfs_attr_leaf_hdr);
929}
930
931static inline struct xfs_attr_leaf_entry *
932xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
933{
934 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
935 return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
936 return &leafp->entries[0];
937}
938
939/*
940 * Cast typed pointers for "local" and "remote" name/value structs.
941 */
942static inline char *
943xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
944{
945 struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
946
947 return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
948}
949
950static inline xfs_attr_leaf_name_remote_t *
951xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
952{
953 return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
954}
955
956static inline xfs_attr_leaf_name_local_t *
957xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
958{
959 return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
960}
961
962/*
963 * Calculate total bytes used (including trailing pad for alignment) for
964 * a "local" name/value structure, a "remote" name/value structure, and
965 * a pointer which might be either.
966 */
967static inline int xfs_attr_leaf_entsize_remote(int nlen)
968{
969 return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
970 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
971}
972
973static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
974{
975 return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
976 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
977}
978
979static inline int xfs_attr_leaf_entsize_local_max(int bsize)
980{
981 return (((bsize) >> 1) + ((bsize) >> 2));
982}
983
984
985
986/*
987 * Remote attribute block format definition
988 *
989 * There is one of these headers per filesystem block in a remote attribute.
990 * This is done to ensure there is a 1:1 mapping between the attribute value
991 * length and the number of blocks needed to store the attribute. This makes the
992 * verification of a buffer a little more complex, but greatly simplifies the
993 * allocation, reading and writing of these attributes as we don't have to guess
994 * the number of blocks needed to store the attribute data.
995 */
996#define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
997
998struct xfs_attr3_rmt_hdr {
999 __be32 rm_magic;
1000 __be32 rm_offset;
1001 __be32 rm_bytes;
1002 __be32 rm_crc;
1003 uuid_t rm_uuid;
1004 __be64 rm_owner;
1005 __be64 rm_blkno;
1006 __be64 rm_lsn;
1007};
1008
1009#define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
1010
1011#define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
1012 ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
1013 sizeof(struct xfs_attr3_rmt_hdr) : 0))
1014
1015#endif /* __XFS_DA_FORMAT_H__ */