blob: 89a1a219c8ff57cc35a6e8c10f2309fff8ee183f [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
125extern void xfs_da3_node_hdr_from_disk(struct xfs_da3_icnode_hdr *to,
126 struct xfs_da_intnode *from);
127extern void xfs_da3_node_hdr_to_disk(struct xfs_da_intnode *to,
128 struct xfs_da3_icnode_hdr *from);
129
130static inline int
131__xfs_da3_node_hdr_size(bool v3)
132{
133 if (v3)
134 return sizeof(struct xfs_da3_node_hdr);
135 return sizeof(struct xfs_da_node_hdr);
136}
137static inline int
138xfs_da3_node_hdr_size(struct xfs_da_intnode *dap)
139{
140 bool v3 = dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC);
141
142 return __xfs_da3_node_hdr_size(v3);
143}
144
145static inline struct xfs_da_node_entry *
146xfs_da3_node_tree_p(struct xfs_da_intnode *dap)
147{
148 if (dap->hdr.info.magic == cpu_to_be16(XFS_DA3_NODE_MAGIC)) {
149 struct xfs_da3_intnode *dap3 = (struct xfs_da3_intnode *)dap;
150 return dap3->__btree;
151 }
152 return dap->__btree;
153}
154
155extern void xfs_da3_intnode_from_disk(struct xfs_da3_icnode_hdr *to,
156 struct xfs_da_intnode *from);
157extern void xfs_da3_intnode_to_disk(struct xfs_da_intnode *to,
158 struct xfs_da3_icnode_hdr *from);
159
160#define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
Christoph Hellwig57926642011-07-13 13:43:48 +0200161
162/*
163 * Directory version 2.
164 *
165 * There are 4 possible formats:
166 * - shortform - embedded into the inode
167 * - single block - data with embedded leaf at the end
168 * - multiple data blocks, single leaf+freeindex block
169 * - data blocks, node and leaf blocks (btree), freeindex blocks
170 *
171 * Note: many node blocks structures and constants are shared with the attr
172 * code and defined in xfs_da_btree.h.
173 */
174
175#define XFS_DIR2_BLOCK_MAGIC 0x58443242 /* XD2B: single block dirs */
176#define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: multiblock dirs */
177#define XFS_DIR2_FREE_MAGIC 0x58443246 /* XD2F: free index blocks */
178
179/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100180 * Directory Version 3 With CRCs.
181 *
182 * The tree formats are the same as for version 2 directories. The difference
183 * is in the block header and dirent formats. In many cases the v3 structures
184 * use v2 definitions as they are no different and this makes code sharing much
185 * easier.
186 *
187 * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
188 * format is v2 then they switch to the existing v2 code, or the format is v3
189 * they implement the v3 functionality. This means the existing dir2 is a mix of
190 * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
191 * where there is a difference in the formats, otherwise the code is unchanged.
192 *
193 * Where it is possible, the code decides what to do based on the magic numbers
194 * in the blocks rather than feature bits in the superblock. This means the code
195 * is as independent of the external XFS code as possible as doesn't require
196 * passing struct xfs_mount pointers into places where it isn't really
197 * necessary.
198 *
199 * Version 3 includes:
200 *
201 * - a larger block header for CRC and identification purposes and so the
202 * offsets of all the structures inside the blocks are different.
203 *
204 * - new magic numbers to be able to detect the v2/v3 types on the fly.
205 */
206
207#define XFS_DIR3_BLOCK_MAGIC 0x58444233 /* XDB3: single block dirs */
208#define XFS_DIR3_DATA_MAGIC 0x58444433 /* XDD3: multiblock dirs */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100209#define XFS_DIR3_FREE_MAGIC 0x58444633 /* XDF3: free index blocks */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100210
211/*
Dave Chinner0cb97762013-08-12 20:50:09 +1000212 * Dirents in version 3 directories have a file type field. Additions to this
213 * list are an on-disk format change, requiring feature bits. Valid values
214 * are as follows:
215 */
216#define XFS_DIR3_FT_UNKNOWN 0
217#define XFS_DIR3_FT_REG_FILE 1
218#define XFS_DIR3_FT_DIR 2
219#define XFS_DIR3_FT_CHRDEV 3
220#define XFS_DIR3_FT_BLKDEV 4
221#define XFS_DIR3_FT_FIFO 5
222#define XFS_DIR3_FT_SOCK 6
223#define XFS_DIR3_FT_SYMLINK 7
224#define XFS_DIR3_FT_WHT 8
225
226#define XFS_DIR3_FT_MAX 9
227
228/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200229 * Byte offset in data block and shortform entry.
230 */
231typedef __uint16_t xfs_dir2_data_off_t;
232#define NULLDATAOFF 0xffffU
233typedef uint xfs_dir2_data_aoff_t; /* argument form */
234
235/*
236 * Normalized offset (in a data block) of the entry, really xfs_dir2_data_off_t.
237 * Only need 16 bits, this is the byte offset into the single block form.
238 */
239typedef struct { __uint8_t i[2]; } __arch_pack xfs_dir2_sf_off_t;
240
241/*
242 * Offset in data space of a data entry.
243 */
244typedef __uint32_t xfs_dir2_dataptr_t;
245#define XFS_DIR2_MAX_DATAPTR ((xfs_dir2_dataptr_t)0xffffffff)
246#define XFS_DIR2_NULL_DATAPTR ((xfs_dir2_dataptr_t)0)
247
248/*
249 * Byte offset in a directory.
250 */
251typedef xfs_off_t xfs_dir2_off_t;
252
253/*
254 * Directory block number (logical dirblk in file)
255 */
256typedef __uint32_t xfs_dir2_db_t;
257
258/*
259 * Inode number stored as 8 8-bit values.
260 */
261typedef struct { __uint8_t i[8]; } xfs_dir2_ino8_t;
262
263/*
264 * Inode number stored as 4 8-bit values.
265 * Works a lot of the time, when all the inode numbers in a directory
266 * fit in 32 bits.
267 */
268typedef struct { __uint8_t i[4]; } xfs_dir2_ino4_t;
269
270typedef union {
271 xfs_dir2_ino8_t i8;
272 xfs_dir2_ino4_t i4;
273} xfs_dir2_inou_t;
274#define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
275
276/*
277 * Directory layout when stored internal to an inode.
278 *
279 * Small directories are packed as tightly as possible so as to fit into the
280 * literal area of the inode. These "shortform" directories consist of a
281 * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
282 * structures. Due the different inode number storage size and the variable
283 * length name field in the xfs_dir2_sf_entry all these structure are
284 * variable length, and the accessors in this file should be used to iterate
285 * over them.
286 */
287typedef struct xfs_dir2_sf_hdr {
288 __uint8_t count; /* count of entries */
289 __uint8_t i8count; /* count of 8-byte inode #s */
290 xfs_dir2_inou_t parent; /* parent dir inode number */
291} __arch_pack xfs_dir2_sf_hdr_t;
292
293typedef struct xfs_dir2_sf_entry {
294 __u8 namelen; /* actual name length */
295 xfs_dir2_sf_off_t offset; /* saved offset */
296 __u8 name[]; /* name, variable size */
297 /*
Dave Chinner0cb97762013-08-12 20:50:09 +1000298 * A single byte containing the file type field follows the inode
299 * number for version 3 directory entries.
300 *
Christoph Hellwig57926642011-07-13 13:43:48 +0200301 * A xfs_dir2_ino8_t or xfs_dir2_ino4_t follows here, at a
302 * variable offset after the name.
303 */
304} __arch_pack xfs_dir2_sf_entry_t;
305
306static inline int xfs_dir2_sf_hdr_size(int i8count)
307{
308 return sizeof(struct xfs_dir2_sf_hdr) -
309 (i8count == 0) *
310 (sizeof(xfs_dir2_ino8_t) - sizeof(xfs_dir2_ino4_t));
311}
312
313static inline xfs_dir2_data_aoff_t
314xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
315{
316 return get_unaligned_be16(&sfep->offset.i);
317}
318
319static inline void
320xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
321{
322 put_unaligned_be16(off, &sfep->offset.i);
323}
324
Christoph Hellwig57926642011-07-13 13:43:48 +0200325static inline struct xfs_dir2_sf_entry *
326xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
327{
328 return (struct xfs_dir2_sf_entry *)
329 ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
330}
331
Dave Chinner0cb97762013-08-12 20:50:09 +1000332static inline int
333xfs_dir3_sf_entsize(
334 struct xfs_mount *mp,
335 struct xfs_dir2_sf_hdr *hdr,
336 int len)
Christoph Hellwig57926642011-07-13 13:43:48 +0200337{
Dave Chinner0cb97762013-08-12 20:50:09 +1000338 int count = sizeof(struct xfs_dir2_sf_entry); /* namelen + offset */
339
340 count += len; /* name */
341 count += hdr->i8count ? sizeof(xfs_dir2_ino8_t) :
342 sizeof(xfs_dir2_ino4_t); /* ino # */
343 if (xfs_sb_version_hasftype(&mp->m_sb))
344 count += sizeof(__uint8_t); /* file type */
345 return count;
Christoph Hellwig57926642011-07-13 13:43:48 +0200346}
347
Dave Chinner0cb97762013-08-12 20:50:09 +1000348static inline struct xfs_dir2_sf_entry *
349xfs_dir3_sf_nextentry(
350 struct xfs_mount *mp,
351 struct xfs_dir2_sf_hdr *hdr,
352 struct xfs_dir2_sf_entry *sfep)
353{
354 return (struct xfs_dir2_sf_entry *)
355 ((char *)sfep + xfs_dir3_sf_entsize(mp, hdr, sfep->namelen));
356}
357
358/*
359 * in dir3 shortform directories, the file type field is stored at a variable
360 * offset after the inode number. Because it's only a single byte, endian
361 * conversion is not necessary.
362 */
363static inline __uint8_t *
364xfs_dir3_sfe_ftypep(
365 struct xfs_dir2_sf_hdr *hdr,
366 struct xfs_dir2_sf_entry *sfep)
367{
368 return (__uint8_t *)&sfep->name[sfep->namelen];
369}
370
371static inline __uint8_t
372xfs_dir3_sfe_get_ftype(
373 struct xfs_mount *mp,
374 struct xfs_dir2_sf_hdr *hdr,
375 struct xfs_dir2_sf_entry *sfep)
376{
377 __uint8_t *ftp;
378
379 if (!xfs_sb_version_hasftype(&mp->m_sb))
380 return XFS_DIR3_FT_UNKNOWN;
381
382 ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
383 if (*ftp >= XFS_DIR3_FT_MAX)
384 return XFS_DIR3_FT_UNKNOWN;
385 return *ftp;
386}
387
388static inline void
389xfs_dir3_sfe_put_ftype(
390 struct xfs_mount *mp,
391 struct xfs_dir2_sf_hdr *hdr,
392 struct xfs_dir2_sf_entry *sfep,
393 __uint8_t ftype)
394{
395 __uint8_t *ftp;
396
397 ASSERT(ftype < XFS_DIR3_FT_MAX);
398
399 if (!xfs_sb_version_hasftype(&mp->m_sb))
400 return;
401 ftp = xfs_dir3_sfe_ftypep(hdr, sfep);
402 *ftp = ftype;
403}
Christoph Hellwig57926642011-07-13 13:43:48 +0200404
405/*
406 * Data block structures.
407 *
408 * A pure data block looks like the following drawing on disk:
409 *
410 * +-------------------------------------------------+
411 * | xfs_dir2_data_hdr_t |
412 * +-------------------------------------------------+
413 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
414 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
415 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
416 * | ... |
417 * +-------------------------------------------------+
418 * | unused space |
419 * +-------------------------------------------------+
420 *
421 * As all the entries are variable size structures the accessors below should
422 * be used to iterate over them.
423 *
424 * In addition to the pure data blocks for the data and node formats,
425 * most structures are also used for the combined data/freespace "block"
426 * format below.
427 */
428
429#define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
430#define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
431#define XFS_DIR2_DATA_FREE_TAG 0xffff
432#define XFS_DIR2_DATA_FD_COUNT 3
433
434/*
435 * Directory address space divided into sections,
436 * spaces separated by 32GB.
437 */
438#define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
439#define XFS_DIR2_DATA_SPACE 0
440#define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
441#define XFS_DIR2_DATA_FIRSTDB(mp) \
442 xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
443
444/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200445 * Describe a free area in the data block.
446 *
447 * The freespace will be formatted as a xfs_dir2_data_unused_t.
448 */
449typedef struct xfs_dir2_data_free {
450 __be16 offset; /* start of freespace */
451 __be16 length; /* length of freespace */
452} xfs_dir2_data_free_t;
453
454/*
455 * Header for the data blocks.
456 *
457 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
458 */
459typedef struct xfs_dir2_data_hdr {
460 __be32 magic; /* XFS_DIR2_DATA_MAGIC or */
461 /* XFS_DIR2_BLOCK_MAGIC */
462 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
463} xfs_dir2_data_hdr_t;
464
465/*
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100466 * define a structure for all the verification fields we are adding to the
467 * directory block structures. This will be used in several structures.
468 * The magic number must be the first entry to align with all the dir2
469 * structures so we determine how to decode them just by the magic number.
470 */
471struct xfs_dir3_blk_hdr {
472 __be32 magic; /* magic number */
473 __be32 crc; /* CRC of block */
474 __be64 blkno; /* first block of the buffer */
475 __be64 lsn; /* sequence number of last write */
476 uuid_t uuid; /* filesystem we belong to */
477 __be64 owner; /* inode that owns the block */
478};
479
480struct xfs_dir3_data_hdr {
481 struct xfs_dir3_blk_hdr hdr;
482 xfs_dir2_data_free_t best_free[XFS_DIR2_DATA_FD_COUNT];
Dave Chinner51707112013-06-12 12:19:07 +1000483 __be32 pad; /* 64 bit alignment */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100484};
485
486#define XFS_DIR3_DATA_CRC_OFF offsetof(struct xfs_dir3_data_hdr, hdr.crc)
487
488static inline struct xfs_dir2_data_free *
489xfs_dir3_data_bestfree_p(struct xfs_dir2_data_hdr *hdr)
490{
Dave Chinner33363fe2013-04-03 16:11:22 +1100491 if (hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
492 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100493 struct xfs_dir3_data_hdr *hdr3 = (struct xfs_dir3_data_hdr *)hdr;
494 return hdr3->best_free;
495 }
496 return hdr->bestfree;
497}
498
499/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200500 * Active entry in a data block.
501 *
502 * Aligned to 8 bytes. After the variable length name field there is a
Dave Chinner0cb97762013-08-12 20:50:09 +1000503 * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
504 *
505 * For dir3 structures, there is file type field between the name and the tag.
506 * This can only be manipulated by helper functions. It is packed hard against
507 * the end of the name so any padding for rounding is between the file type and
508 * the tag.
Christoph Hellwig57926642011-07-13 13:43:48 +0200509 */
510typedef struct xfs_dir2_data_entry {
511 __be64 inumber; /* inode number */
512 __u8 namelen; /* name length */
513 __u8 name[]; /* name bytes, no null */
Dave Chinner0cb97762013-08-12 20:50:09 +1000514 /* __u8 filetype; */ /* type of inode we point to */
Christoph Hellwig57926642011-07-13 13:43:48 +0200515 /* __be16 tag; */ /* starting offset of us */
516} xfs_dir2_data_entry_t;
517
518/*
519 * Unused entry in a data block.
520 *
521 * Aligned to 8 bytes. Tag appears as the last 2 bytes and must be accessed
522 * using xfs_dir2_data_unused_tag_p.
523 */
524typedef struct xfs_dir2_data_unused {
525 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
526 __be16 length; /* total free length */
527 /* variable offset */
528 __be16 tag; /* starting offset of us */
529} xfs_dir2_data_unused_t;
530
531/*
532 * Size of a data entry.
533 */
Dave Chinner0cb97762013-08-12 20:50:09 +1000534static inline int
535__xfs_dir3_data_entsize(
536 bool ftype,
537 int n)
Christoph Hellwig57926642011-07-13 13:43:48 +0200538{
Dave Chinner0cb97762013-08-12 20:50:09 +1000539 int size = offsetof(struct xfs_dir2_data_entry, name[0]);
540
541 size += n;
542 size += sizeof(xfs_dir2_data_off_t);
543 if (ftype)
544 size += sizeof(__uint8_t);
545 return roundup(size, XFS_DIR2_DATA_ALIGN);
546}
547static inline int
548xfs_dir3_data_entsize(
549 struct xfs_mount *mp,
550 int n)
551{
552 bool ftype = xfs_sb_version_hasftype(&mp->m_sb) ? true : false;
553 return __xfs_dir3_data_entsize(ftype, n);
554}
555
556static inline __uint8_t
557xfs_dir3_dirent_get_ftype(
558 struct xfs_mount *mp,
559 struct xfs_dir2_data_entry *dep)
560{
561 if (xfs_sb_version_hasftype(&mp->m_sb)) {
562 __uint8_t type = dep->name[dep->namelen];
563
564 ASSERT(type < XFS_DIR3_FT_MAX);
565 if (type < XFS_DIR3_FT_MAX)
566 return type;
567
568 }
569 return XFS_DIR3_FT_UNKNOWN;
570}
571
572static inline void
573xfs_dir3_dirent_put_ftype(
574 struct xfs_mount *mp,
575 struct xfs_dir2_data_entry *dep,
576 __uint8_t type)
577{
578 ASSERT(type < XFS_DIR3_FT_MAX);
579 ASSERT(dep->namelen != 0);
580
581 if (xfs_sb_version_hasftype(&mp->m_sb))
582 dep->name[dep->namelen] = type;
Christoph Hellwig57926642011-07-13 13:43:48 +0200583}
584
585/*
586 * Pointer to an entry's tag word.
587 */
588static inline __be16 *
Dave Chinner0cb97762013-08-12 20:50:09 +1000589xfs_dir3_data_entry_tag_p(
590 struct xfs_mount *mp,
591 struct xfs_dir2_data_entry *dep)
Christoph Hellwig57926642011-07-13 13:43:48 +0200592{
593 return (__be16 *)((char *)dep +
Dave Chinner0cb97762013-08-12 20:50:09 +1000594 xfs_dir3_data_entsize(mp, dep->namelen) - sizeof(__be16));
Christoph Hellwig57926642011-07-13 13:43:48 +0200595}
596
597/*
598 * Pointer to a freespace's tag word.
599 */
600static inline __be16 *
601xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
602{
603 return (__be16 *)((char *)dup +
604 be16_to_cpu(dup->length) - sizeof(__be16));
605}
606
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100607static inline size_t
608xfs_dir3_data_hdr_size(bool dir3)
609{
610 if (dir3)
611 return sizeof(struct xfs_dir3_data_hdr);
612 return sizeof(struct xfs_dir2_data_hdr);
613}
614
615static inline size_t
616xfs_dir3_data_entry_offset(struct xfs_dir2_data_hdr *hdr)
617{
618 bool dir3 = hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
619 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC);
620 return xfs_dir3_data_hdr_size(dir3);
621}
622
623static inline struct xfs_dir2_data_entry *
624xfs_dir3_data_entry_p(struct xfs_dir2_data_hdr *hdr)
625{
626 return (struct xfs_dir2_data_entry *)
627 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
628}
629
Dave Chinner33363fe2013-04-03 16:11:22 +1100630static inline struct xfs_dir2_data_unused *
631xfs_dir3_data_unused_p(struct xfs_dir2_data_hdr *hdr)
632{
633 return (struct xfs_dir2_data_unused *)
634 ((char *)hdr + xfs_dir3_data_entry_offset(hdr));
635}
636
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100637/*
638 * Offsets of . and .. in data space (always block 0)
Dave Chinner6b2647a2013-04-03 16:11:24 +1100639 *
Dave Chinner0cb97762013-08-12 20:50:09 +1000640 * XXX: there is scope for significant optimisation of the logic here. Right
641 * now we are checking for "dir3 format" over and over again. Ideally we should
642 * only do it once for each operation.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100643 */
644static inline xfs_dir2_data_aoff_t
Dave Chinner367993e2013-09-30 09:37:04 +1000645xfs_dir3_data_dot_offset(struct xfs_mount *mp)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100646{
Dave Chinner367993e2013-09-30 09:37:04 +1000647 return xfs_dir3_data_hdr_size(xfs_sb_version_hascrc(&mp->m_sb));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100648}
649
650static inline xfs_dir2_data_aoff_t
Dave Chinner367993e2013-09-30 09:37:04 +1000651xfs_dir3_data_dotdot_offset(struct xfs_mount *mp)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100652{
Dave Chinner367993e2013-09-30 09:37:04 +1000653 return xfs_dir3_data_dot_offset(mp) +
654 xfs_dir3_data_entsize(mp, 1);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100655}
656
657static inline xfs_dir2_data_aoff_t
Dave Chinner367993e2013-09-30 09:37:04 +1000658xfs_dir3_data_first_offset(struct xfs_mount *mp)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100659{
Dave Chinner367993e2013-09-30 09:37:04 +1000660 return xfs_dir3_data_dotdot_offset(mp) +
661 xfs_dir3_data_entsize(mp, 2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100662}
663
664/*
665 * location of . and .. in data space (always block 0)
666 */
667static inline struct xfs_dir2_data_entry *
Dave Chinner367993e2013-09-30 09:37:04 +1000668xfs_dir3_data_dot_entry_p(
669 struct xfs_mount *mp,
670 struct xfs_dir2_data_hdr *hdr)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100671{
672 return (struct xfs_dir2_data_entry *)
Dave Chinner367993e2013-09-30 09:37:04 +1000673 ((char *)hdr + xfs_dir3_data_dot_offset(mp));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100674}
675
676static inline struct xfs_dir2_data_entry *
Dave Chinner367993e2013-09-30 09:37:04 +1000677xfs_dir3_data_dotdot_entry_p(
678 struct xfs_mount *mp,
679 struct xfs_dir2_data_hdr *hdr)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100680{
681 return (struct xfs_dir2_data_entry *)
Dave Chinner367993e2013-09-30 09:37:04 +1000682 ((char *)hdr + xfs_dir3_data_dotdot_offset(mp));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100683}
684
685static inline struct xfs_dir2_data_entry *
Dave Chinner367993e2013-09-30 09:37:04 +1000686xfs_dir3_data_first_entry_p(
687 struct xfs_mount *mp,
688 struct xfs_dir2_data_hdr *hdr)
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100689{
690 return (struct xfs_dir2_data_entry *)
Dave Chinner367993e2013-09-30 09:37:04 +1000691 ((char *)hdr + xfs_dir3_data_first_offset(mp));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100692}
693
Christoph Hellwig57926642011-07-13 13:43:48 +0200694/*
695 * Leaf block structures.
696 *
697 * A pure leaf block looks like the following drawing on disk:
698 *
699 * +---------------------------+
700 * | xfs_dir2_leaf_hdr_t |
701 * +---------------------------+
702 * | xfs_dir2_leaf_entry_t |
703 * | xfs_dir2_leaf_entry_t |
704 * | xfs_dir2_leaf_entry_t |
705 * | xfs_dir2_leaf_entry_t |
706 * | ... |
707 * +---------------------------+
708 * | xfs_dir2_data_off_t |
709 * | xfs_dir2_data_off_t |
710 * | xfs_dir2_data_off_t |
711 * | ... |
712 * +---------------------------+
713 * | xfs_dir2_leaf_tail_t |
714 * +---------------------------+
715 *
716 * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
717 * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
718 * for directories with separate leaf nodes and free space blocks
719 * (magic = XFS_DIR2_LEAFN_MAGIC).
720 *
721 * As all the entries are variable size structures the accessors below should
722 * be used to iterate over them.
723 */
724
725/*
726 * Offset of the leaf/node space. First block in this space
727 * is the btree root.
728 */
729#define XFS_DIR2_LEAF_SPACE 1
730#define XFS_DIR2_LEAF_OFFSET (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
731#define XFS_DIR2_LEAF_FIRSTDB(mp) \
732 xfs_dir2_byte_to_db(mp, XFS_DIR2_LEAF_OFFSET)
733
734/*
735 * Leaf block header.
736 */
737typedef struct xfs_dir2_leaf_hdr {
738 xfs_da_blkinfo_t info; /* header for da routines */
739 __be16 count; /* count of entries */
740 __be16 stale; /* count of stale entries */
741} xfs_dir2_leaf_hdr_t;
742
Dave Chinner24df33b2013-04-12 07:30:21 +1000743struct xfs_dir3_leaf_hdr {
744 struct xfs_da3_blkinfo info; /* header for da routines */
745 __be16 count; /* count of entries */
746 __be16 stale; /* count of stale entries */
Dave Chinner51707112013-06-12 12:19:07 +1000747 __be32 pad; /* 64 bit alignment */
Dave Chinner24df33b2013-04-12 07:30:21 +1000748};
749
750struct xfs_dir3_icleaf_hdr {
751 __uint32_t forw;
752 __uint32_t back;
753 __uint16_t magic;
754 __uint16_t count;
755 __uint16_t stale;
756};
757
Christoph Hellwig57926642011-07-13 13:43:48 +0200758/*
759 * Leaf block entry.
760 */
761typedef struct xfs_dir2_leaf_entry {
762 __be32 hashval; /* hash value of name */
763 __be32 address; /* address of data entry */
764} xfs_dir2_leaf_entry_t;
765
766/*
767 * Leaf block tail.
768 */
769typedef struct xfs_dir2_leaf_tail {
770 __be32 bestcount;
771} xfs_dir2_leaf_tail_t;
772
773/*
774 * Leaf block.
775 */
776typedef struct xfs_dir2_leaf {
Dave Chinner24df33b2013-04-12 07:30:21 +1000777 xfs_dir2_leaf_hdr_t hdr; /* leaf header */
778 xfs_dir2_leaf_entry_t __ents[]; /* entries */
Christoph Hellwig57926642011-07-13 13:43:48 +0200779} xfs_dir2_leaf_t;
780
Dave Chinner24df33b2013-04-12 07:30:21 +1000781struct xfs_dir3_leaf {
782 struct xfs_dir3_leaf_hdr hdr; /* leaf header */
783 struct xfs_dir2_leaf_entry __ents[]; /* entries */
784};
Christoph Hellwig57926642011-07-13 13:43:48 +0200785
Dave Chinner24df33b2013-04-12 07:30:21 +1000786#define XFS_DIR3_LEAF_CRC_OFF offsetof(struct xfs_dir3_leaf_hdr, info.crc)
787
Dave Chinnerd386b322013-08-12 20:49:31 +1000788extern void xfs_dir3_leaf_hdr_from_disk(struct xfs_dir3_icleaf_hdr *to,
789 struct xfs_dir2_leaf *from);
790
Dave Chinner24df33b2013-04-12 07:30:21 +1000791static inline int
792xfs_dir3_leaf_hdr_size(struct xfs_dir2_leaf *lp)
Christoph Hellwig57926642011-07-13 13:43:48 +0200793{
Dave Chinner24df33b2013-04-12 07:30:21 +1000794 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
795 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC))
796 return sizeof(struct xfs_dir3_leaf_hdr);
797 return sizeof(struct xfs_dir2_leaf_hdr);
798}
799
800static inline int
801xfs_dir3_max_leaf_ents(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
802{
803 return (mp->m_dirblksize - xfs_dir3_leaf_hdr_size(lp)) /
Christoph Hellwig57926642011-07-13 13:43:48 +0200804 (uint)sizeof(struct xfs_dir2_leaf_entry);
805}
806
807/*
808 * Get address of the bestcount field in the single-leaf block.
809 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000810static inline struct xfs_dir2_leaf_entry *
811xfs_dir3_leaf_ents_p(struct xfs_dir2_leaf *lp)
812{
813 if (lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
814 lp->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC)) {
815 struct xfs_dir3_leaf *lp3 = (struct xfs_dir3_leaf *)lp;
816 return lp3->__ents;
817 }
818 return lp->__ents;
819}
820
821/*
822 * Get address of the bestcount field in the single-leaf block.
823 */
Christoph Hellwig57926642011-07-13 13:43:48 +0200824static inline struct xfs_dir2_leaf_tail *
825xfs_dir2_leaf_tail_p(struct xfs_mount *mp, struct xfs_dir2_leaf *lp)
826{
827 return (struct xfs_dir2_leaf_tail *)
828 ((char *)lp + mp->m_dirblksize -
829 sizeof(struct xfs_dir2_leaf_tail));
830}
831
832/*
833 * Get address of the bests array in the single-leaf block.
834 */
835static inline __be16 *
836xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
837{
838 return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
839}
840
841/*
Dave Chinner24df33b2013-04-12 07:30:21 +1000842 * DB blocks here are logical directory block numbers, not filesystem blocks.
843 */
844
845/*
Christoph Hellwig57926642011-07-13 13:43:48 +0200846 * Convert dataptr to byte in file space
847 */
848static inline xfs_dir2_off_t
849xfs_dir2_dataptr_to_byte(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
850{
851 return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG;
852}
853
854/*
855 * Convert byte in file space to dataptr. It had better be aligned.
856 */
857static inline xfs_dir2_dataptr_t
858xfs_dir2_byte_to_dataptr(struct xfs_mount *mp, xfs_dir2_off_t by)
859{
860 return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG);
861}
862
863/*
864 * Convert byte in space to (DB) block
865 */
866static inline xfs_dir2_db_t
867xfs_dir2_byte_to_db(struct xfs_mount *mp, xfs_dir2_off_t by)
868{
869 return (xfs_dir2_db_t)
870 (by >> (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog));
871}
872
873/*
874 * Convert dataptr to a block number
875 */
876static inline xfs_dir2_db_t
877xfs_dir2_dataptr_to_db(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
878{
879 return xfs_dir2_byte_to_db(mp, xfs_dir2_dataptr_to_byte(mp, dp));
880}
881
882/*
883 * Convert byte in space to offset in a block
884 */
885static inline xfs_dir2_data_aoff_t
886xfs_dir2_byte_to_off(struct xfs_mount *mp, xfs_dir2_off_t by)
887{
888 return (xfs_dir2_data_aoff_t)(by &
889 ((1 << (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) - 1));
890}
891
892/*
893 * Convert dataptr to a byte offset in a block
894 */
895static inline xfs_dir2_data_aoff_t
896xfs_dir2_dataptr_to_off(struct xfs_mount *mp, xfs_dir2_dataptr_t dp)
897{
898 return xfs_dir2_byte_to_off(mp, xfs_dir2_dataptr_to_byte(mp, dp));
899}
900
901/*
902 * Convert block and offset to byte in space
903 */
904static inline xfs_dir2_off_t
905xfs_dir2_db_off_to_byte(struct xfs_mount *mp, xfs_dir2_db_t db,
906 xfs_dir2_data_aoff_t o)
907{
908 return ((xfs_dir2_off_t)db <<
909 (mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog)) + o;
910}
911
912/*
913 * Convert block (DB) to block (dablk)
914 */
915static inline xfs_dablk_t
916xfs_dir2_db_to_da(struct xfs_mount *mp, xfs_dir2_db_t db)
917{
918 return (xfs_dablk_t)(db << mp->m_sb.sb_dirblklog);
919}
920
921/*
922 * Convert byte in space to (DA) block
923 */
924static inline xfs_dablk_t
925xfs_dir2_byte_to_da(struct xfs_mount *mp, xfs_dir2_off_t by)
926{
927 return xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, by));
928}
929
930/*
931 * Convert block and offset to dataptr
932 */
933static inline xfs_dir2_dataptr_t
934xfs_dir2_db_off_to_dataptr(struct xfs_mount *mp, xfs_dir2_db_t db,
935 xfs_dir2_data_aoff_t o)
936{
937 return xfs_dir2_byte_to_dataptr(mp, xfs_dir2_db_off_to_byte(mp, db, o));
938}
939
940/*
941 * Convert block (dablk) to block (DB)
942 */
943static inline xfs_dir2_db_t
944xfs_dir2_da_to_db(struct xfs_mount *mp, xfs_dablk_t da)
945{
946 return (xfs_dir2_db_t)(da >> mp->m_sb.sb_dirblklog);
947}
948
949/*
950 * Convert block (dablk) to byte offset in space
951 */
952static inline xfs_dir2_off_t
953xfs_dir2_da_to_byte(struct xfs_mount *mp, xfs_dablk_t da)
954{
955 return xfs_dir2_db_off_to_byte(mp, xfs_dir2_da_to_db(mp, da), 0);
956}
957
958/*
959 * Free space block defintions for the node format.
960 */
961
962/*
963 * Offset of the freespace index.
964 */
965#define XFS_DIR2_FREE_SPACE 2
966#define XFS_DIR2_FREE_OFFSET (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
967#define XFS_DIR2_FREE_FIRSTDB(mp) \
968 xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)
969
970typedef struct xfs_dir2_free_hdr {
971 __be32 magic; /* XFS_DIR2_FREE_MAGIC */
972 __be32 firstdb; /* db of first entry */
973 __be32 nvalid; /* count of valid entries */
974 __be32 nused; /* count of used entries */
975} xfs_dir2_free_hdr_t;
976
977typedef struct xfs_dir2_free {
978 xfs_dir2_free_hdr_t hdr; /* block header */
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200979 __be16 bests[]; /* best free counts */
Christoph Hellwig57926642011-07-13 13:43:48 +0200980 /* unused entries are -1 */
981} xfs_dir2_free_t;
982
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100983struct xfs_dir3_free_hdr {
984 struct xfs_dir3_blk_hdr hdr;
985 __be32 firstdb; /* db of first entry */
986 __be32 nvalid; /* count of valid entries */
987 __be32 nused; /* count of used entries */
Dave Chinner51707112013-06-12 12:19:07 +1000988 __be32 pad; /* 64 bit alignment */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100989};
990
991struct xfs_dir3_free {
992 struct xfs_dir3_free_hdr hdr;
993 __be16 bests[]; /* best free counts */
994 /* unused entries are -1 */
995};
996
997#define XFS_DIR3_FREE_CRC_OFF offsetof(struct xfs_dir3_free, hdr.hdr.crc)
998
999/*
1000 * In core version of the free block header, abstracted away from on-disk format
1001 * differences. Use this in the code, and convert to/from the disk version using
1002 * xfs_dir3_free_hdr_from_disk/xfs_dir3_free_hdr_to_disk.
1003 */
1004struct xfs_dir3_icfree_hdr {
1005 __uint32_t magic;
1006 __uint32_t firstdb;
1007 __uint32_t nvalid;
1008 __uint32_t nused;
1009
1010};
1011
1012void xfs_dir3_free_hdr_from_disk(struct xfs_dir3_icfree_hdr *to,
1013 struct xfs_dir2_free *from);
1014
1015static inline int
1016xfs_dir3_free_hdr_size(struct xfs_mount *mp)
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001017{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001018 if (xfs_sb_version_hascrc(&mp->m_sb))
1019 return sizeof(struct xfs_dir3_free_hdr);
1020 return sizeof(struct xfs_dir2_free_hdr);
1021}
1022
1023static inline int
1024xfs_dir3_free_max_bests(struct xfs_mount *mp)
1025{
1026 return (mp->m_dirblksize - xfs_dir3_free_hdr_size(mp)) /
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001027 sizeof(xfs_dir2_data_off_t);
1028}
Christoph Hellwig57926642011-07-13 13:43:48 +02001029
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001030static inline __be16 *
1031xfs_dir3_free_bests_p(struct xfs_mount *mp, struct xfs_dir2_free *free)
1032{
1033 return (__be16 *)((char *)free + xfs_dir3_free_hdr_size(mp));
1034}
1035
Christoph Hellwig57926642011-07-13 13:43:48 +02001036/*
1037 * Convert data space db to the corresponding free db.
1038 */
1039static inline xfs_dir2_db_t
1040xfs_dir2_db_to_fdb(struct xfs_mount *mp, xfs_dir2_db_t db)
1041{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001042 return XFS_DIR2_FREE_FIRSTDB(mp) + db / xfs_dir3_free_max_bests(mp);
Christoph Hellwig57926642011-07-13 13:43:48 +02001043}
1044
1045/*
1046 * Convert data space db to the corresponding index in a free db.
1047 */
1048static inline int
1049xfs_dir2_db_to_fdindex(struct xfs_mount *mp, xfs_dir2_db_t db)
1050{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001051 return db % xfs_dir3_free_max_bests(mp);
Christoph Hellwig57926642011-07-13 13:43:48 +02001052}
1053
1054/*
1055 * Single block format.
1056 *
1057 * The single block format looks like the following drawing on disk:
1058 *
1059 * +-------------------------------------------------+
1060 * | xfs_dir2_data_hdr_t |
1061 * +-------------------------------------------------+
1062 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
1063 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
1064 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
1065 * | ... |
1066 * +-------------------------------------------------+
1067 * | unused space |
1068 * +-------------------------------------------------+
1069 * | ... |
1070 * | xfs_dir2_leaf_entry_t |
1071 * | xfs_dir2_leaf_entry_t |
1072 * +-------------------------------------------------+
1073 * | xfs_dir2_block_tail_t |
1074 * +-------------------------------------------------+
1075 *
1076 * As all the entries are variable size structures the accessors below should
1077 * be used to iterate over them.
1078 */
1079
1080typedef struct xfs_dir2_block_tail {
1081 __be32 count; /* count of leaf entries */
1082 __be32 stale; /* count of stale lf entries */
1083} xfs_dir2_block_tail_t;
1084
1085/*
1086 * Pointer to the leaf header embedded in a data block (1-block format)
1087 */
1088static inline struct xfs_dir2_block_tail *
1089xfs_dir2_block_tail_p(struct xfs_mount *mp, struct xfs_dir2_data_hdr *hdr)
1090{
1091 return ((struct xfs_dir2_block_tail *)
1092 ((char *)hdr + mp->m_dirblksize)) - 1;
1093}
1094
1095/*
1096 * Pointer to the leaf entries embedded in a data block (1-block format)
1097 */
1098static inline struct xfs_dir2_leaf_entry *
1099xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
1100{
1101 return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
1102}
1103
Dave Chinner57062782013-10-15 09:17:51 +11001104
1105/*
1106 * Attribute storage layout
1107 *
1108 * Attribute lists are structured around Btrees where all the data
1109 * elements are in the leaf nodes. Attribute names are hashed into an int,
1110 * then that int is used as the index into the Btree. Since the hashval
1111 * of an attribute name may not be unique, we may have duplicate keys. The
1112 * internal links in the Btree are logical block offsets into the file.
1113 *
1114 *========================================================================
1115 * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
1116 *========================================================================
1117 *
1118 * Struct leaf_entry's are packed from the top. Name/values grow from the
1119 * bottom but are not packed. The freemap contains run-length-encoded entries
1120 * for the free bytes after the leaf_entry's, but only the N largest such,
1121 * smaller runs are dropped. When the freemap doesn't show enough space
1122 * for an allocation, we compact the name/value area and try again. If we
1123 * still don't have enough space, then we have to split the block. The
1124 * name/value structs (both local and remote versions) must be 32bit aligned.
1125 *
1126 * Since we have duplicate hash keys, for each key that matches, compare
1127 * the actual name string. The root and intermediate node search always
1128 * takes the first-in-the-block key match found, so we should only have
1129 * to work "forw"ard. If none matches, continue with the "forw"ard leaf
1130 * nodes until the hash key changes or the attribute name is found.
1131 *
1132 * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
1133 * the leaf_entry. The namespaces are independent only because we also look
1134 * at the namespace bit when we are looking for a matching attribute name.
1135 *
1136 * We also store an "incomplete" bit in the leaf_entry. It shows that an
1137 * attribute is in the middle of being created and should not be shown to
1138 * the user if we crash during the time that the bit is set. We clear the
1139 * bit when we have finished setting up the attribute. We do this because
1140 * we cannot create some large attributes inside a single transaction, and we
1141 * need some indication that we weren't finished if we crash in the middle.
1142 */
1143#define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
1144
1145typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
1146 __be16 base; /* base of free region */
1147 __be16 size; /* length of free region */
1148} xfs_attr_leaf_map_t;
1149
1150typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
1151 xfs_da_blkinfo_t info; /* block type, links, etc. */
1152 __be16 count; /* count of active leaf_entry's */
1153 __be16 usedbytes; /* num bytes of names/values stored */
1154 __be16 firstused; /* first used byte in name area */
1155 __u8 holes; /* != 0 if blk needs compaction */
1156 __u8 pad1;
1157 xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
1158 /* N largest free regions */
1159} xfs_attr_leaf_hdr_t;
1160
1161typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
1162 __be32 hashval; /* hash value of name */
1163 __be16 nameidx; /* index into buffer of name/value */
1164 __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
1165 __u8 pad2; /* unused pad byte */
1166} xfs_attr_leaf_entry_t;
1167
1168typedef struct xfs_attr_leaf_name_local {
1169 __be16 valuelen; /* number of bytes in value */
1170 __u8 namelen; /* length of name bytes */
1171 __u8 nameval[1]; /* name/value bytes */
1172} xfs_attr_leaf_name_local_t;
1173
1174typedef struct xfs_attr_leaf_name_remote {
1175 __be32 valueblk; /* block number of value bytes */
1176 __be32 valuelen; /* number of bytes in value */
1177 __u8 namelen; /* length of name bytes */
1178 __u8 name[1]; /* name bytes */
1179} xfs_attr_leaf_name_remote_t;
1180
1181typedef struct xfs_attr_leafblock {
1182 xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
1183 xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
1184 xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
1185 xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
1186} xfs_attr_leafblock_t;
1187
1188/*
1189 * CRC enabled leaf structures. Called "version 3" structures to match the
1190 * version number of the directory and dablk structures for this feature, and
1191 * attr2 is already taken by the variable inode attribute fork size feature.
1192 */
1193struct xfs_attr3_leaf_hdr {
1194 struct xfs_da3_blkinfo info;
1195 __be16 count;
1196 __be16 usedbytes;
1197 __be16 firstused;
1198 __u8 holes;
1199 __u8 pad1;
1200 struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
1201 __be32 pad2; /* 64 bit alignment */
1202};
1203
1204#define XFS_ATTR3_LEAF_CRC_OFF (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
1205
1206struct xfs_attr3_leafblock {
1207 struct xfs_attr3_leaf_hdr hdr;
1208 struct xfs_attr_leaf_entry entries[1];
1209
1210 /*
1211 * The rest of the block contains the following structures after the
1212 * leaf entries, growing from the bottom up. The variables are never
1213 * referenced, the locations accessed purely from helper functions.
1214 *
1215 * struct xfs_attr_leaf_name_local
1216 * struct xfs_attr_leaf_name_remote
1217 */
1218};
1219
1220/*
1221 * incore, neutral version of the attribute leaf header
1222 */
1223struct xfs_attr3_icleaf_hdr {
1224 __uint32_t forw;
1225 __uint32_t back;
1226 __uint16_t magic;
1227 __uint16_t count;
1228 __uint16_t usedbytes;
1229 __uint16_t firstused;
1230 __u8 holes;
1231 struct {
1232 __uint16_t base;
1233 __uint16_t size;
1234 } freemap[XFS_ATTR_LEAF_MAPSIZE];
1235};
1236
1237/*
1238 * Flags used in the leaf_entry[i].flags field.
1239 * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
1240 * on the system call, they are "or"ed together for various operations.
1241 */
1242#define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
1243#define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
1244#define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
1245#define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
1246#define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
1247#define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
1248#define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
1249#define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
1250
1251/*
1252 * Conversion macros for converting namespace bits from argument flags
1253 * to ondisk flags.
1254 */
1255#define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
1256#define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
1257#define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
1258#define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
1259#define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
1260 ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
1261#define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
1262 ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
1263
1264/*
1265 * Alignment for namelist and valuelist entries (since they are mixed
1266 * there can be only one alignment value)
1267 */
1268#define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
1269
1270static inline int
1271xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
1272{
1273 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
1274 return sizeof(struct xfs_attr3_leaf_hdr);
1275 return sizeof(struct xfs_attr_leaf_hdr);
1276}
1277
1278static inline struct xfs_attr_leaf_entry *
1279xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
1280{
1281 if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
1282 return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
1283 return &leafp->entries[0];
1284}
1285
1286/*
1287 * Cast typed pointers for "local" and "remote" name/value structs.
1288 */
1289static inline char *
1290xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
1291{
1292 struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
1293
1294 return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
1295}
1296
1297static inline xfs_attr_leaf_name_remote_t *
1298xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
1299{
1300 return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
1301}
1302
1303static inline xfs_attr_leaf_name_local_t *
1304xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
1305{
1306 return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
1307}
1308
1309/*
1310 * Calculate total bytes used (including trailing pad for alignment) for
1311 * a "local" name/value structure, a "remote" name/value structure, and
1312 * a pointer which might be either.
1313 */
1314static inline int xfs_attr_leaf_entsize_remote(int nlen)
1315{
1316 return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
1317 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
1318}
1319
1320static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
1321{
1322 return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
1323 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
1324}
1325
1326static inline int xfs_attr_leaf_entsize_local_max(int bsize)
1327{
1328 return (((bsize) >> 1) + ((bsize) >> 2));
1329}
1330
1331
1332
1333/*
1334 * Remote attribute block format definition
1335 *
1336 * There is one of these headers per filesystem block in a remote attribute.
1337 * This is done to ensure there is a 1:1 mapping between the attribute value
1338 * length and the number of blocks needed to store the attribute. This makes the
1339 * verification of a buffer a little more complex, but greatly simplifies the
1340 * allocation, reading and writing of these attributes as we don't have to guess
1341 * the number of blocks needed to store the attribute data.
1342 */
1343#define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
1344
1345struct xfs_attr3_rmt_hdr {
1346 __be32 rm_magic;
1347 __be32 rm_offset;
1348 __be32 rm_bytes;
1349 __be32 rm_crc;
1350 uuid_t rm_uuid;
1351 __be64 rm_owner;
1352 __be64 rm_blkno;
1353 __be64 rm_lsn;
1354};
1355
1356#define XFS_ATTR3_RMT_CRC_OFF offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
1357
1358#define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize) \
1359 ((bufsize) - (xfs_sb_version_hascrc(&(mp)->m_sb) ? \
1360 sizeof(struct xfs_attr3_rmt_hdr) : 0))
1361
1362#endif /* __XFS_DA_FORMAT_H__ */