blob: a5f4d6ee2bb3f01aa51201d687d732f2a46793ae [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
Dave Chinner57062782013-10-15 09:17:51 +110022/*
23 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
24 *
25 * It is used to manage a doubly linked list of all blocks at the same
26 * level in the Btree, and to identify which type of block this is.
27 */
28#define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
29#define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
30#define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
31#define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
32
33typedef struct xfs_da_blkinfo {
34 __be32 forw; /* previous block in list */
35 __be32 back; /* following block in list */
36 __be16 magic; /* validity check on block */
37 __be16 pad; /* unused */
38} xfs_da_blkinfo_t;
39
40/*
41 * CRC enabled directory structure types
42 *
43 * The headers change size for the additional verification information, but
44 * otherwise the tree layouts and contents are unchanged. Hence the da btree
45 * code can use the struct xfs_da_blkinfo for manipulating the tree links and
46 * magic numbers without modification for both v2 and v3 nodes.
47 */
48#define XFS_DA3_NODE_MAGIC 0x3ebe /* magic number: non-leaf blocks */
49#define XFS_ATTR3_LEAF_MAGIC 0x3bee /* magic number: attribute leaf blks */
50#define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
51#define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
52
53struct xfs_da3_blkinfo {
54 /*
55 * the node link manipulation code relies on the fact that the first
56 * element of this structure is the struct xfs_da_blkinfo so it can
57 * ignore the differences in the rest of the structures.
58 */
59 struct xfs_da_blkinfo hdr;
60 __be32 crc; /* CRC of block */
61 __be64 blkno; /* first block of the buffer */
62 __be64 lsn; /* sequence number of last write */
63 uuid_t uuid; /* filesystem we belong to */
64 __be64 owner; /* inode that owns the block */
65};
66
67/*
68 * This is the structure of the root and intermediate nodes in the Btree.
69 * The leaf nodes are defined above.
70 *
71 * Entries are not packed.
72 *
73 * Since we have duplicate keys, use a binary search but always follow
74 * all match in the block, not just the first match found.
75 */
76#define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
77
78typedef struct xfs_da_node_hdr {
79 struct xfs_da_blkinfo info; /* block type, links, etc. */
80 __be16 __count; /* count of active entries */
81 __be16 __level; /* level above leaves (leaf == 0) */
82} xfs_da_node_hdr_t;
83
84struct xfs_da3_node_hdr {
85 struct xfs_da3_blkinfo info; /* block type, links, etc. */
86 __be16 __count; /* count of active entries */
87 __be16 __level; /* level above leaves (leaf == 0) */
88 __be32 __pad32;
89};
90
91#define XFS_DA3_NODE_CRC_OFF (offsetof(struct xfs_da3_node_hdr, info.crc))
92
93typedef struct xfs_da_node_entry {
94 __be32 hashval; /* hash value for this descendant */
95 __be32 before; /* Btree block before this key */
96} xfs_da_node_entry_t;
97
98typedef struct xfs_da_intnode {
99 struct xfs_da_node_hdr hdr;
100 struct xfs_da_node_entry __btree[];
101} xfs_da_intnode_t;
102
103struct xfs_da3_intnode {
104 struct xfs_da3_node_hdr hdr;
105 struct xfs_da_node_entry __btree[];
106};
107
108/*
109 * In-core version of the node header to abstract the differences in the v2 and
110 * v3 disk format of the headers. Callers need to convert to/from disk format as
111 * appropriate.
112 */
113struct xfs_da3_icnode_hdr {
114 __uint32_t forw;
115 __uint32_t back;
116 __uint16_t magic;
117 __uint16_t count;
118 __uint16_t level;
119};
120
Christoph Hellwig57926642011-07-13 13:43:48 +0200121/*
122 * Directory version 2.
123 *
124 * There are 4 possible formats:
125 * - shortform - embedded into the inode
126 * - single block - data with embedded leaf at the end
127 * - multiple data blocks, single leaf+freeindex block
128 * - data blocks, node and leaf blocks (btree), freeindex blocks
129 *
130 * Note: many node blocks structures and constants are shared with the attr
131 * code and defined in xfs_da_btree.h.
132 */
133
134#define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
135#define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
136#define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
137
138/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100139 * Directory Version 3 With CRCs.
140 *
141 * The tree formats are the same as for version 2 directories. The difference
142 * is in the block header and dirent formats. In many cases the v3 structures
143 * use v2 definitions as they are no different and this makes code sharing much
144 * easier.
145 *
146 * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
147 * format is v2 then they switch to the existing v2 code, or the format is v3
148 * they implement the v3 functionality. This means the existing dir2 is a mix of
149 * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
150 * where there is a difference in the formats, otherwise the code is unchanged.
151 *
152 * Where it is possible, the code decides what to do based on the magic numbers
153 * in the blocks rather than feature bits in the superblock. This means the code
154 * is as independent of the external XFS code as possible as doesn't require
155 * passing struct xfs_mount pointers into places where it isn't really
156 * necessary.
157 *
158 * Version 3 includes:
159 *
160 * - a larger block header for CRC and identification purposes and so the
161 * offsets of all the structures inside the blocks are different.
162 *
163 * - new magic numbers to be able to detect the v2/v3 types on the fly.
164 */
165
166#define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
167#define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100168#define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100169
170/*
Dave Chinner0cb97762013-08-12 20:50:09 +1000171 * Dirents in version 3 directories have a file type field. Additions to this
172 * list are an on-disk format change, requiring feature bits. Valid values
173 * are as follows:
174 */
175#define XFS_DIR3_FT_UNKNOWN 0
176#define XFS_DIR3_FT_REG_FILE 1
177#define XFS_DIR3_FT_DIR 2
178#define XFS_DIR3_FT_CHRDEV 3
179#define XFS_DIR3_FT_BLKDEV 4
180#define XFS_DIR3_FT_FIFO 5
181#define XFS_DIR3_FT_SOCK 6
182#define XFS_DIR3_FT_SYMLINK 7
183#define XFS_DIR3_FT_WHT 8
184
185#define XFS_DIR3_FT_MAX 9
186
187/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200188 * Byte offset in data block and shortform entry.
189 */
190typedef __uint16_t xfs_dir2_data_off_t;
191#define NULLDATAOFF 0xffffU
192typedef uint xfs_dir2_data_aoff_t; /* argument form */
193
194/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200195 * Offset in data space of a data entry.
196 */
197typedef __uint32_t xfs_dir2_dataptr_t;
198#define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
199#define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
200
201/*
202 * Byte offset in a directory.
203 */
204typedef xfs_off_t xfs_dir2_off_t;
205
206/*
207 * Directory block number (logical dirblk in file)
208 */
209typedef __uint32_t xfs_dir2_db_t;
210
211/*
212 * Inode number stored as 8 8-bit values.
213 */
214typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
215
216/*
217 * Inode number stored as 4 8-bit values.
218 * Works a lot of the time, when all the inode numbers in a directory
219 * fit in 32 bits.
220 */
221typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
222
223typedef union {
224 xfs_dir2_ino8_t i8;
225 xfs_dir2_ino4_t i4;
226} xfs_dir2_inou_t;
227#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
228
229/*
230 * Directory layout when stored internal to an inode.
231 *
232 * Small directories are packed as tightly as possible so as to fit into the
233 * literal area of the inode. These "shortform" directories consist of a
234 * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
235 * structures. Due the different inode number storage size and the variable
236 * length name field in the xfs_dir2_sf_entry all these structure are
237 * variable length, and the accessors in this file should be used to iterate
238 * over them.
239 */
240typedef struct xfs_dir2_sf_hdr {
241 __uint8_t count; /* count of entries */
242 __uint8_t i8count; /* count of 8-byte inode #s */
243 xfs_dir2_inou_t parent; /* parent dir inode number */
244} __arch_pack xfs_dir2_sf_hdr_t;
245
246typedef struct xfs_dir2_sf_entry {
247 __u8 namelen; /* actual name length */
Christoph Hellwig8353a642016-07-20 11:47:21 +1000248 __u8 offset[2]; /* saved offset */
Christoph Hellwig57926642011-07-13 13:43:48 +0200249 __u8 name[]; /* name, variable size */
250 /*
Dave Chinner0cb97762013-08-12 20:50:09 +1000251 * A single byte containing the file type field follows the inode
252 * number for version 3 directory entries.
253 *
Christoph Hellwig57926642011-07-13 13:43:48 +0200254 * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
255 * variable offset after the name.
256 */
Christoph Hellwig8353a642016-07-20 11:47:21 +1000257} xfs_dir2_sf_entry_t;
Christoph Hellwig57926642011-07-13 13:43:48 +0200258
259static inline int xfs_dir2_sf_hdr_size(int i8count)
260{
261 return sizeof(struct xfs_dir2_sf_hdr) -
262 (i8count == 0) *
263 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
264}
265
266static inline xfs_dir2_data_aoff_t
267xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
268{
Christoph Hellwig8353a642016-07-20 11:47:21 +1000269 return get_unaligned_be16(sfep->offset);
Christoph Hellwig57926642011-07-13 13:43:48 +0200270}
271
272static inline void
273xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
274{
Christoph Hellwig8353a642016-07-20 11:47:21 +1000275 put_unaligned_be16(off, sfep->offset);
Christoph Hellwig57926642011-07-13 13:43:48 +0200276}
277
Christoph Hellwig57926642011-07-13 13:43:48 +0200278static inline struct xfs_dir2_sf_entry *
279xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
280{
281 return (struct xfs_dir2_sf_entry *)
282 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
283}
284
Dave Chinner0cb97762013-08-12 20:50:09 +1000285/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200286 * Data block structures.
287 *
288 * A pure data block looks like the following drawing on disk:
289 *
290 * +-------------------------------------------------+
291 * | xfs_dir2_data_hdr_t |
292 * +-------------------------------------------------+
293 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
294 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
295 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
296 * | ... |
297 * +-------------------------------------------------+
298 * | unused space |
299 * +-------------------------------------------------+
300 *
301 * As all the entries are variable size structures the accessors below should
302 * be used to iterate over them.
303 *
304 * In addition to the pure data blocks for the data and node formats,
305 * most structures are also used for the combined data/freespace "block"
306 * format below.
307 */
308
309#define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
310#define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
311#define XFS_DIR2_DATA_FREE_TAG 0xffff
312#define XFS_DIR2_DATA_FD_COUNT 3
313
314/*
315 * Directory address space divided into sections,
316 * spaces separated by 32GB.
317 */
318#define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
319#define XFS_DIR2_DATA_SPACE 0
320#define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
Christoph Hellwig57926642011-07-13 13:43:48 +0200321
322/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200323 * Describe a free area in the data block.
324 *
325 * The freespace will be formatted as a xfs_dir2_data_unused_t.
326 */
327typedef struct xfs_dir2_data_free {
328 __be16 offset; /* start of freespace */
329 __be16 length; /* length of freespace */
330} xfs_dir2_data_free_t;
331
332/*
333 * Header for the data blocks.
334 *
335 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
336 */
337typedef struct xfs_dir2_data_hdr {
338 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
339 /* XFS_DIR2_BLOCK_MAGIC */
340 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
341} xfs_dir2_data_hdr_t;
342
343/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100344 * define a structure for all the verification fields we are adding to the
345 * directory block structures. This will be used in several structures.
346 * The magic number must be the first entry to align with all the dir2
347 * structures so we determine how to decode them just by the magic number.
348 */
349struct xfs_dir3_blk_hdr {
350 __be32 magic; /* magic number */
351 __be32 crc; /* CRC of block */
352 __be64 blkno; /* first block of the buffer */
353 __be64 lsn; /* sequence number of last write */
354 uuid_t uuid; /* filesystem we belong to */
355 __be64 owner; /* inode that owns the block */
356};
357
358struct xfs_dir3_data_hdr {
359 struct xfs_dir3_blk_hdr hdr;
360 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
Dave Chinner51707112013-06-12 12:19:07 +1000361 __be32 pad; /* 64 bit alignment */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100362};
363
364#define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
365
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100366/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200367 * Active entry in a data block.
368 *
369 * Aligned to 8 bytes. After the variable length name field there is a
Dave Chinner0cb97762013-08-12 20:50:09 +1000370 * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
371 *
372 * For dir3 structures, there is file type field between the name and the tag.
373 * This can only be manipulated by helper functions. It is packed hard against
374 * the end of the name so any padding for rounding is between the file type and
375 * the tag.
Christoph Hellwig57926642011-07-13 13:43:48 +0200376 */
377typedef struct xfs_dir2_data_entry {
378 __be64 inumber; /* inode number */
379 __u8 namelen; /* name length */
380 __u8 name[]; /* name bytes, no null */
Dave Chinner0cb97762013-08-12 20:50:09 +1000381 /* __u8 filetype; */ /* type of inode we point to */
Christoph Hellwig57926642011-07-13 13:43:48 +0200382 /* __be16 tag; */ /* starting offset of us */
383} xfs_dir2_data_entry_t;
384
385/*
386 * Unused entry in a data block.
387 *
388 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
389 * using xfs_dir2_data_unused_tag_p.
390 */
391typedef struct xfs_dir2_data_unused {
392 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
393 __be16 length; /* total free length */
394 /* variable offset */
395 __be16 tag; /* starting offset of us */
396} xfs_dir2_data_unused_t;
397
398/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200399 * Pointer to a freespace's tag word.
400 */
401static inline __be16 *
402xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
403{
404 return (__be16 *)((char *)dup +
405 be16_to_cpu(dup->length) - sizeof(__be16));
406}
407
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100408/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200409 * Leaf block structures.
410 *
411 * A pure leaf block looks like the following drawing on disk:
412 *
413 * +---------------------------+
414 * | xfs_dir2_leaf_hdr_t |
415 * +---------------------------+
416 * | xfs_dir2_leaf_entry_t |
417 * | xfs_dir2_leaf_entry_t |
418 * | xfs_dir2_leaf_entry_t |
419 * | xfs_dir2_leaf_entry_t |
420 * | ... |
421 * +---------------------------+
422 * | xfs_dir2_data_off_t |
423 * | xfs_dir2_data_off_t |
424 * | xfs_dir2_data_off_t |
425 * | ... |
426 * +---------------------------+
427 * | xfs_dir2_leaf_tail_t |
428 * +---------------------------+
429 *
430 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
431 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
432 * for directories with separate leaf nodes and free space blocks
433 * (magic = XFS_DIR2_LEAFN_MAGIC).
434 *
435 * As all the entries are variable size structures the accessors below should
436 * be used to iterate over them.
437 */
438
439/*
440 * Offset of the leaf/node space. First block in this space
441 * is the btree root.
442 */
443#define XFS_DIR2_LEAF_SPACE 1
444#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
Christoph Hellwig57926642011-07-13 13:43:48 +0200445
446/*
447 * Leaf block header.
448 */
449typedef struct xfs_dir2_leaf_hdr {
450 xfs_da_blkinfo_t info; /* header for da routines */
451 __be16 count; /* count of entries */
452 __be16 stale; /* count of stale entries */
453} xfs_dir2_leaf_hdr_t;
454
Dave Chinner24df33b2013-04-12 07:30:21 +1000455struct xfs_dir3_leaf_hdr {
456 struct xfs_da3_blkinfo info; /* header for da routines */
457 __be16 count; /* count of entries */
458 __be16 stale; /* count of stale entries */
Dave Chinner51707112013-06-12 12:19:07 +1000459 __be32 pad; /* 64 bit alignment */
Dave Chinner24df33b2013-04-12 07:30:21 +1000460};
461
462struct xfs_dir3_icleaf_hdr {
463 __uint32_t forw;
464 __uint32_t back;
465 __uint16_t magic;
466 __uint16_t count;
467 __uint16_t stale;
468};
469
Christoph Hellwig57926642011-07-13 13:43:48 +0200470/*
471 * Leaf block entry.
472 */
473typedef struct xfs_dir2_leaf_entry {
474 __be32 hashval; /* hash value of name */
475 __be32 address; /* address of data entry */
476} xfs_dir2_leaf_entry_t;
477
478/*
479 * Leaf block tail.
480 */
481typedef struct xfs_dir2_leaf_tail {
482 __be32 bestcount;
483} xfs_dir2_leaf_tail_t;
484
485/*
486 * Leaf block.
487 */
488typedef struct xfs_dir2_leaf {
Dave Chinner24df33b2013-04-12 07:30:21 +1000489 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
490 xfs_dir2_leaf_entry_t __ents[]; /* entries */
Christoph Hellwig57926642011-07-13 13:43:48 +0200491} xfs_dir2_leaf_t;
492
Dave Chinner24df33b2013-04-12 07:30:21 +1000493struct xfs_dir3_leaf {
494 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
495 struct xfs_dir2_leaf_entry __ents[]; /* entries */
496};
Christoph Hellwig57926642011-07-13 13:43:48 +0200497
Dave Chinner24df33b2013-04-12 07:30:21 +1000498#define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
499
Dave Chinner24df33b2013-04-12 07:30:21 +1000500/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200501 * Get address of the bests array in the single-leaf block.
502 */
503static inline __be16 *
504xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
505{
506 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
507}
508
509/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200510 * Free space block defintions for the node format.
511 */
512
513/*
514 * Offset of the freespace index.
515 */
516#define XFS_DIR2_FREE_SPACE 2
517#define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
Christoph Hellwig57926642011-07-13 13:43:48 +0200518
519typedef struct xfs_dir2_free_hdr {
520 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
521 __be32 firstdb; /* db of first entry */
522 __be32 nvalid; /* count of valid entries */
523 __be32 nused; /* count of used entries */
524} xfs_dir2_free_hdr_t;
525
526typedef struct xfs_dir2_free {
527 xfs_dir2_free_hdr_t hdr; /* block header */
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200528 __be16 bests[]; /* best free counts */
Christoph Hellwig57926642011-07-13 13:43:48 +0200529 /* unused entries are -1 */
530} xfs_dir2_free_t;
531
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100532struct xfs_dir3_free_hdr {
533 struct xfs_dir3_blk_hdr hdr;
534 __be32 firstdb; /* db of first entry */
535 __be32 nvalid; /* count of valid entries */
536 __be32 nused; /* count of used entries */
Dave Chinner51707112013-06-12 12:19:07 +1000537 __be32 pad; /* 64 bit alignment */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100538};
539
540struct xfs_dir3_free {
541 struct xfs_dir3_free_hdr hdr;
542 __be16 bests[]; /* best free counts */
543 /* unused entries are -1 */
544};
545
546#define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
547
548/*
549 * In core version of the free block header, abstracted away from on-disk format
550 * differences. Use this in the code, and convert to/from the disk version using
551 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
552 */
553struct xfs_dir3_icfree_hdr {
554 __uint32_t magic;
555 __uint32_t firstdb;
556 __uint32_t nvalid;
557 __uint32_t nused;
558
559};
560
Christoph Hellwig57926642011-07-13 13:43:48 +0200561/*
562 * Single block format.
563 *
564 * The single block format looks like the following drawing on disk:
565 *
566 * +-------------------------------------------------+
567 * | xfs_dir2_data_hdr_t |
568 * +-------------------------------------------------+
569 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
570 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
571 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
572 * | ... |
573 * +-------------------------------------------------+
574 * | unused space |
575 * +-------------------------------------------------+
576 * | ... |
577 * | xfs_dir2_leaf_entry_t |
578 * | xfs_dir2_leaf_entry_t |
579 * +-------------------------------------------------+
580 * | xfs_dir2_block_tail_t |
581 * +-------------------------------------------------+
582 *
583 * As all the entries are variable size structures the accessors below should
584 * be used to iterate over them.
585 */
586
587typedef struct xfs_dir2_block_tail {
588 __be32 count; /* count of leaf entries */
589 __be32 stale; /* count of stale lf entries */
590} xfs_dir2_block_tail_t;
591
592/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200593 * Pointer to the leaf entries embedded in a data block (1-block format)
594 */
595static inline struct xfs_dir2_leaf_entry *
596xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
597{
598 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
599}
600
Dave Chinner57062782013-10-15 09:17:51 +1100601
602/*
603 * Attribute storage layout
604 *
605 * Attribute lists are structured around Btrees where all the data
606 * elements are in the leaf nodes. Attribute names are hashed into an int,
607 * then that int is used as the index into the Btree. Since the hashval
608 * of an attribute name may not be unique, we may have duplicate keys. The
609 * internal links in the Btree are logical block offsets into the file.
610 *
Dave Chinner57062782013-10-15 09:17:51 +1100611 * Struct leaf_entry's are packed from the top. Name/values grow from the
612 * bottom but are not packed. The freemap contains run-length-encoded entries
613 * for the free bytes after the leaf_entry's, but only the N largest such,
614 * smaller runs are dropped. When the freemap doesn't show enough space
615 * for an allocation, we compact the name/value area and try again. If we
616 * still don't have enough space, then we have to split the block. The
617 * name/value structs (both local and remote versions) must be 32bit aligned.
618 *
619 * Since we have duplicate hash keys, for each key that matches, compare
620 * the actual name string. The root and intermediate node search always
621 * takes the first-in-the-block key match found, so we should only have
622 * to work "forw"ard. If none matches, continue with the "forw"ard leaf
623 * nodes until the hash key changes or the attribute name is found.
624 *
625 * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
626 * the leaf_entry. The namespaces are independent only because we also look
627 * at the namespace bit when we are looking for a matching attribute name.
628 *
629 * We also store an "incomplete" bit in the leaf_entry. It shows that an
630 * attribute is in the middle of being created and should not be shown to
631 * the user if we crash during the time that the bit is set. We clear the
632 * bit when we have finished setting up the attribute. We do this because
633 * we cannot create some large attributes inside a single transaction, and we
634 * need some indication that we weren't finished if we crash in the middle.
635 */
636#define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
637
Darrick J. Wong244efea2016-02-08 15:00:01 +1100638/*
639 * Entries are packed toward the top as tight as possible.
640 */
641typedef struct xfs_attr_shortform {
642 struct xfs_attr_sf_hdr { /* constant-structure header block */
643 __be16 totsize; /* total bytes in shortform list */
644 __u8 count; /* count of active entries */
645 } hdr;
646 struct xfs_attr_sf_entry {
647 __uint8_t namelen; /* actual length of name (no NULL) */
648 __uint8_t valuelen; /* actual length of value (no NULL) */
649 __uint8_t flags; /* flags bits (see xfs_attr_leaf.h) */
650 __uint8_t nameval[1]; /* name & value bytes concatenated */
651 } list[1]; /* variable sized array */
652} xfs_attr_shortform_t;
653
Dave Chinner57062782013-10-15 09:17:51 +1100654typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
655 __be16 base; /* base of free region */
656 __be16 size; /* length of free region */
657} xfs_attr_leaf_map_t;
658
659typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
660 xfs_da_blkinfo_t info; /* block type, links, etc. */
661 __be16 count; /* count of active leaf_entry's */
662 __be16 usedbytes; /* num bytes of names/values stored */
663 __be16 firstused; /* first used byte in name area */
664 __u8 holes; /* != 0 if blk needs compaction */
665 __u8 pad1;
666 xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
667 /* N largest free regions */
668} xfs_attr_leaf_hdr_t;
669
670typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
671 __be32 hashval; /* hash value of name */
672 __be16 nameidx; /* index into buffer of name/value */
673 __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
674 __u8 pad2; /* unused pad byte */
675} xfs_attr_leaf_entry_t;
676
677typedef struct xfs_attr_leaf_name_local {
678 __be16 valuelen; /* number of bytes in value */
679 __u8 namelen; /* length of name bytes */
680 __u8 nameval[1]; /* name/value bytes */
681} xfs_attr_leaf_name_local_t;
682
683typedef struct xfs_attr_leaf_name_remote {
684 __be32 valueblk; /* block number of value bytes */
685 __be32 valuelen; /* number of bytes in value */
686 __u8 namelen; /* length of name bytes */
687 __u8 name[1]; /* name bytes */
688} xfs_attr_leaf_name_remote_t;
689
690typedef struct xfs_attr_leafblock {
691 xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
692 xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
Jan Karaffeecc52015-08-19 10:34:32 +1000693 /*
694 * The rest of the block contains the following structures after the
695 * leaf entries, growing from the bottom up. The variables are never
696 * referenced and definining them can actually make gcc optimize away
697 * accesses to the 'entries' array above index 0 so don't do that.
698 *
699 * xfs_attr_leaf_name_local_t namelist;
700 * xfs_attr_leaf_name_remote_t valuelist;
701 */
Dave Chinner57062782013-10-15 09:17:51 +1100702} xfs_attr_leafblock_t;
703
704/*
705 * CRC enabled leaf structures. Called "version 3" structures to match the
706 * version number of the directory and dablk structures for this feature, and
707 * attr2 is already taken by the variable inode attribute fork size feature.
708 */
709struct xfs_attr3_leaf_hdr {
710 struct xfs_da3_blkinfo info;
711 __be16 count;
712 __be16 usedbytes;
713 __be16 firstused;
714 __u8 holes;
715 __u8 pad1;
716 struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
717 __be32 pad2; /* 64 bit alignment */
718};
719
720#define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
721
722struct xfs_attr3_leafblock {
723 struct xfs_attr3_leaf_hdr hdr;
724 struct xfs_attr_leaf_entry entries[1];
725
726 /*
727 * The rest of the block contains the following structures after the
728 * leaf entries, growing from the bottom up. The variables are never
729 * referenced, the locations accessed purely from helper functions.
730 *
731 * struct xfs_attr_leaf_name_local
732 * struct xfs_attr_leaf_name_remote
733 */
734};
735
736/*
737 * incore, neutral version of the attribute leaf header
738 */
739struct xfs_attr3_icleaf_hdr {
740 __uint32_t forw;
741 __uint32_t back;
742 __uint16_t magic;
743 __uint16_t count;
744 __uint16_t usedbytes;
Brian Fostere87021a2015-04-13 11:27:10 +1000745 /*
746 * firstused is 32-bit here instead of 16-bit like the on-disk variant
747 * to support maximum fsb size of 64k without overflow issues throughout
748 * the attr code. Instead, the overflow condition is handled on
749 * conversion to/from disk.
750 */
751 __uint32_t firstused;
Dave Chinner57062782013-10-15 09:17:51 +1100752 __u8 holes;
753 struct {
754 __uint16_t base;
755 __uint16_t size;
756 } freemap[XFS_ATTR_LEAF_MAPSIZE];
757};
758
759/*
Brian Fostere87021a2015-04-13 11:27:10 +1000760 * Special value to represent fs block size in the leaf header firstused field.
761 * Only used when block size overflows the 2-bytes available on disk.
762 */
763#define XFS_ATTR3_LEAF_NULLOFF 0
764
765/*
Dave Chinner57062782013-10-15 09:17:51 +1100766 * Flags used in the leaf_entry[i].flags field.
767 * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
768 * on the system call, they are "or"ed together for various operations.
769 */
770#define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
771#define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
772#define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
773#define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
774#define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
775#define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
776#define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
777#define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
778
779/*
780 * Conversion macros for converting namespace bits from argument flags
781 * to ondisk flags.
782 */
783#define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
784#define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
785#define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
786#define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
787#define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
788 ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
789#define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
790 ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
791
792/*
793 * Alignment for namelist and valuelist entries (since they are mixed
794 * there can be only one alignment value)
795 */
796#define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
797
798static inline int
799xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
800{
801 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
802 return sizeof(struct xfs_attr3_leaf_hdr);
803 return sizeof(struct xfs_attr_leaf_hdr);
804}
805
806static inline struct xfs_attr_leaf_entry *
807xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
808{
809 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
810 return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
811 return &leafp->entries[0];
812}
813
814/*
815 * Cast typed pointers for "local" and "remote" name/value structs.
816 */
817static inline char *
818xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
819{
820 struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
821
822 return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
823}
824
825static inline xfs_attr_leaf_name_remote_t *
826xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
827{
828 return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
829}
830
831static inline xfs_attr_leaf_name_local_t *
832xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
833{
834 return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
835}
836
837/*
838 * Calculate total bytes used (including trailing pad for alignment) for
839 * a "local" name/value structure, a "remote" name/value structure, and
840 * a pointer which might be either.
841 */
842static inline int xfs_attr_leaf_entsize_remote(int nlen)
843{
844 return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
845 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
846}
847
848static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
849{
850 return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
851 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
852}
853
854static inline int xfs_attr_leaf_entsize_local_max(int bsize)
855{
856 return (((bsize) >> 1) + ((bsize) >> 2));
857}
858
859
860
861/*
862 * Remote attribute block format definition
863 *
864 * There is one of these headers per filesystem block in a remote attribute.
865 * This is done to ensure there is a 1:1 mapping between the attribute value
866 * length and the number of blocks needed to store the attribute. This makes the
867 * verification of a buffer a little more complex, but greatly simplifies the
868 * allocation, reading and writing of these attributes as we don't have to guess
869 * the number of blocks needed to store the attribute data.
870 */
871#define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
872
873struct xfs_attr3_rmt_hdr {
874 __be32 rm_magic;
875 __be32 rm_offset;
876 __be32 rm_bytes;
877 __be32 rm_crc;
878 uuid_t rm_uuid;
879 __be64 rm_owner;
880 __be64 rm_blkno;
881 __be64 rm_lsn;
882};
883
884#define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
885
886#define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
887 ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
888 sizeof(struct xfs_attr3_rmt_hdr) : 0))
889
890#endif /* __XFS_DA_FORMAT_H__ */