blob: ca02cc71e234511b10f6844f582c2a41a37351d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18#ifndef __XFS_DIR2_DATA_H__
19#define __XFS_DIR2_DATA_H__
20
21/*
22 * Directory format 2, data block structures.
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +020023 *
24 * A pure data block looks like the following drawing on disk:
25 *
26 * +-------------------------------------------------+
27 * | xfs_dir2_data_hdr_t |
28 * +-------------------------------------------------+
29 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
30 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
31 * | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
32 * | ... |
33 * +-------------------------------------------------+
34 * | unused space |
35 * +-------------------------------------------------+
36 *
37 * As all the entries are variable size structures the accessors in this
38 * file should be used to iterate over them.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41struct xfs_dabuf;
42struct xfs_da_args;
43struct xfs_inode;
44struct xfs_trans;
45
46/*
47 * Constants.
48 */
49#define XFS_DIR2_DATA_MAGIC 0x58443244 /* XD2D: for multiblock dirs */
50#define XFS_DIR2_DATA_ALIGN_LOG 3 /* i.e., 8 bytes */
51#define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
52#define XFS_DIR2_DATA_FREE_TAG 0xffff
53#define XFS_DIR2_DATA_FD_COUNT 3
54
55/*
56 * Directory address space divided into sections,
Malcolm Parsons9da096f2009-03-29 09:55:42 +020057 * spaces separated by 32GB.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59#define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
60#define XFS_DIR2_DATA_SPACE 0
61#define XFS_DIR2_DATA_OFFSET (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
62#define XFS_DIR2_DATA_FIRSTDB(mp) \
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100063 xfs_dir2_byte_to_db(mp, XFS_DIR2_DATA_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/*
66 * Offsets of . and .. in data space (always block 0)
67 */
68#define XFS_DIR2_DATA_DOT_OFFSET \
69 ((xfs_dir2_data_aoff_t)sizeof(xfs_dir2_data_hdr_t))
70#define XFS_DIR2_DATA_DOTDOT_OFFSET \
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100071 (XFS_DIR2_DATA_DOT_OFFSET + xfs_dir2_data_entsize(1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define XFS_DIR2_DATA_FIRST_OFFSET \
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100073 (XFS_DIR2_DATA_DOTDOT_OFFSET + xfs_dir2_data_entsize(2))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/*
76 * Structures.
77 */
78
79/*
80 * Describe a free area in the data block.
81 * The freespace will be formatted as a xfs_dir2_data_unused_t.
82 */
83typedef struct xfs_dir2_data_free {
Nathan Scott70e73f52006-03-17 17:26:52 +110084 __be16 offset; /* start of freespace */
85 __be16 length; /* length of freespace */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086} xfs_dir2_data_free_t;
87
88/*
89 * Header for the data blocks.
90 * Always at the beginning of a directory-sized block.
91 * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
92 */
93typedef struct xfs_dir2_data_hdr {
Nathan Scott70e73f52006-03-17 17:26:52 +110094 __be32 magic; /* XFS_DIR2_DATA_MAGIC */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* or XFS_DIR2_BLOCK_MAGIC */
96 xfs_dir2_data_free_t bestfree[XFS_DIR2_DATA_FD_COUNT];
97} xfs_dir2_data_hdr_t;
98
99/*
100 * Active entry in a data block. Aligned to 8 bytes.
101 * Tag appears as the last 2 bytes.
102 */
103typedef struct xfs_dir2_data_entry {
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000104 __be64 inumber; /* inode number */
105 __u8 namelen; /* name length */
106 __u8 name[1]; /* name bytes, no null */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* variable offset */
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000108 __be16 tag; /* starting offset of us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109} xfs_dir2_data_entry_t;
110
111/*
112 * Unused entry in a data block. Aligned to 8 bytes.
113 * Tag appears as the last 2 bytes.
114 */
115typedef struct xfs_dir2_data_unused {
Nathan Scottad354eb2006-03-17 17:27:37 +1100116 __be16 freetag; /* XFS_DIR2_DATA_FREE_TAG */
117 __be16 length; /* total free length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* variable offset */
Nathan Scottad354eb2006-03-17 17:27:37 +1100119 __be16 tag; /* starting offset of us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120} xfs_dir2_data_unused_t;
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
123 * Size of a data entry.
124 */
Nathan Scotta844f452005-11-02 14:38:42 +1100125static inline int xfs_dir2_data_entsize(int n)
126{
127 return (int)roundup(offsetof(xfs_dir2_data_entry_t, name[0]) + (n) + \
128 (uint)sizeof(xfs_dir2_data_off_t), XFS_DIR2_DATA_ALIGN);
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*
132 * Pointer to an entry's tag word.
133 */
Nathan Scott3d693c62006-03-17 17:28:27 +1100134static inline __be16 *
Nathan Scotta844f452005-11-02 14:38:42 +1100135xfs_dir2_data_entry_tag_p(xfs_dir2_data_entry_t *dep)
136{
Nathan Scott3d693c62006-03-17 17:28:27 +1100137 return (__be16 *)((char *)dep +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000138 xfs_dir2_data_entsize(dep->namelen) - sizeof(__be16));
Nathan Scotta844f452005-11-02 14:38:42 +1100139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/*
142 * Pointer to a freespace's tag word.
143 */
Nathan Scott1fba9f72006-03-17 17:27:47 +1100144static inline __be16 *
Nathan Scotta844f452005-11-02 14:38:42 +1100145xfs_dir2_data_unused_tag_p(xfs_dir2_data_unused_t *dup)
146{
Nathan Scott1fba9f72006-03-17 17:27:47 +1100147 return (__be16 *)((char *)dup +
148 be16_to_cpu(dup->length) - sizeof(__be16));
Nathan Scotta844f452005-11-02 14:38:42 +1100149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * Function declarations.
153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#ifdef DEBUG
Nathan Scotta844f452005-11-02 14:38:42 +1100155extern void xfs_dir2_data_check(struct xfs_inode *dp, struct xfs_dabuf *bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#else
157#define xfs_dir2_data_check(dp,bp)
158#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200159extern xfs_dir2_data_free_t *xfs_dir2_data_freeinsert(xfs_dir2_data_hdr_t *hdr,
Nathan Scotta844f452005-11-02 14:38:42 +1100160 xfs_dir2_data_unused_t *dup, int *loghead);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200161extern void xfs_dir2_data_freescan(struct xfs_mount *mp,
162 xfs_dir2_data_hdr_t *hdr, int *loghead);
Nathan Scotta844f452005-11-02 14:38:42 +1100163extern int xfs_dir2_data_init(struct xfs_da_args *args, xfs_dir2_db_t blkno,
164 struct xfs_dabuf **bpp);
165extern void xfs_dir2_data_log_entry(struct xfs_trans *tp, struct xfs_dabuf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 xfs_dir2_data_entry_t *dep);
Nathan Scotta844f452005-11-02 14:38:42 +1100167extern void xfs_dir2_data_log_header(struct xfs_trans *tp,
168 struct xfs_dabuf *bp);
169extern void xfs_dir2_data_log_unused(struct xfs_trans *tp, struct xfs_dabuf *bp,
170 xfs_dir2_data_unused_t *dup);
171extern void xfs_dir2_data_make_free(struct xfs_trans *tp, struct xfs_dabuf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_dir2_data_aoff_t offset,
173 xfs_dir2_data_aoff_t len, int *needlogp,
174 int *needscanp);
Nathan Scotta844f452005-11-02 14:38:42 +1100175extern void xfs_dir2_data_use_free(struct xfs_trans *tp, struct xfs_dabuf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 xfs_dir2_data_unused_t *dup,
177 xfs_dir2_data_aoff_t offset,
178 xfs_dir2_data_aoff_t len, int *needlogp,
179 int *needscanp);
180
181#endif /* __XFS_DIR2_DATA_H__ */