]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/xfs_da_btree.h
Undoes mod: xfs-cmds:slinx:120772a
[thirdparty/xfsprogs-dev.git] / include / xfs_da_btree.h
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32 #ifndef __XFS_DA_BTREE_H__
33 #define __XFS_DA_BTREE_H__
34
35 struct xfs_buf;
36 struct xfs_bmap_free;
37 struct xfs_inode;
38 struct xfs_mount;
39 struct xfs_trans;
40 struct zone;
41
42 /*========================================================================
43 * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
44 *========================================================================*/
45
46 /*
47 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
48 *
49 * Is is used to manage a doubly linked list of all blocks at the same
50 * level in the Btree, and to identify which type of block this is.
51 */
52 #define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
53 #define XFS_DIR_LEAF_MAGIC 0xfeeb /* magic number: directory leaf blks */
54 #define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
55 #define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
56 #define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
57
58 #define XFS_DIRX_LEAF_MAGIC(mp) \
59 (XFS_DIR_IS_V1(mp) ? XFS_DIR_LEAF_MAGIC : XFS_DIR2_LEAFN_MAGIC)
60
61 typedef struct xfs_da_blkinfo {
62 xfs_dablk_t forw; /* previous block in list */
63 xfs_dablk_t back; /* following block in list */
64 __uint16_t magic; /* validity check on block */
65 __uint16_t pad; /* unused */
66 } xfs_da_blkinfo_t;
67
68 /*
69 * This is the structure of the root and intermediate nodes in the Btree.
70 * The leaf nodes are defined above.
71 *
72 * Entries are not packed.
73 *
74 * Since we have duplicate keys, use a binary search but always follow
75 * all match in the block, not just the first match found.
76 */
77 #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
78
79 typedef struct xfs_da_intnode {
80 struct xfs_da_node_hdr { /* constant-structure header block */
81 xfs_da_blkinfo_t info; /* block type, links, etc. */
82 __uint16_t count; /* count of active entries */
83 __uint16_t level; /* level above leaves (leaf == 0) */
84 } hdr;
85 struct xfs_da_node_entry {
86 xfs_dahash_t hashval; /* hash value for this descendant */
87 xfs_dablk_t before; /* Btree block before this key */
88 } btree[1]; /* variable sized array of keys */
89 } xfs_da_intnode_t;
90 typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
91 typedef struct xfs_da_node_entry xfs_da_node_entry_t;
92
93 #define XFS_DA_NODE_ENTSIZE_BYNAME /* space a name uses */ \
94 (sizeof(xfs_da_node_entry_t))
95 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_NODE_ENTRIES)
96 int xfs_da_node_entries(struct xfs_mount *mp);
97 #define XFS_DA_NODE_ENTRIES(mp) xfs_da_node_entries(mp)
98 #else
99 #define XFS_DA_NODE_ENTRIES(mp) ((mp)->m_da_node_ents)
100 #endif
101
102 #define XFS_DA_MAXHASH ((xfs_dahash_t)-1) /* largest valid hash value */
103
104 /*
105 * Macros used by directory code to interface to the filesystem.
106 */
107 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_LBSIZE)
108 int xfs_lbsize(struct xfs_mount *mp);
109 #define XFS_LBSIZE(mp) xfs_lbsize(mp)
110 #else
111 #define XFS_LBSIZE(mp) ((mp)->m_sb.sb_blocksize)
112 #endif
113 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_LBLOG)
114 int xfs_lblog(struct xfs_mount *mp);
115 #define XFS_LBLOG(mp) xfs_lblog(mp)
116 #else
117 #define XFS_LBLOG(mp) ((mp)->m_sb.sb_blocklog)
118 #endif
119
120 /*
121 * Macros used by directory code to interface to the kernel
122 */
123
124 /*
125 * Macros used to manipulate directory off_t's
126 */
127 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_MAKE_BNOENTRY)
128 __uint32_t xfs_da_make_bnoentry(struct xfs_mount *mp, xfs_dablk_t bno,
129 int entry);
130 #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
131 xfs_da_make_bnoentry(mp,bno,entry)
132 #else
133 #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
134 (((bno) << (mp)->m_dircook_elog) | (entry))
135 #endif
136 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_MAKE_COOKIE)
137 xfs_off_t xfs_da_make_cookie(struct xfs_mount *mp, xfs_dablk_t bno, int entry,
138 xfs_dahash_t hash);
139 #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
140 xfs_da_make_cookie(mp,bno,entry,hash)
141 #else
142 #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
143 (((xfs_off_t)XFS_DA_MAKE_BNOENTRY(mp, bno, entry) << 32) | (hash))
144 #endif
145 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_HASH)
146 xfs_dahash_t xfs_da_cookie_hash(struct xfs_mount *mp, xfs_off_t cookie);
147 #define XFS_DA_COOKIE_HASH(mp,cookie) xfs_da_cookie_hash(mp,cookie)
148 #else
149 #define XFS_DA_COOKIE_HASH(mp,cookie) ((xfs_dahash_t)(cookie))
150 #endif
151 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_BNO)
152 xfs_dablk_t xfs_da_cookie_bno(struct xfs_mount *mp, xfs_off_t cookie);
153 #define XFS_DA_COOKIE_BNO(mp,cookie) xfs_da_cookie_bno(mp,cookie)
154 #else
155 #define XFS_DA_COOKIE_BNO(mp,cookie) \
156 (((xfs_off_t)(cookie) >> 31) == -1LL ? \
157 (xfs_dablk_t)0 : \
158 (xfs_dablk_t)((xfs_off_t)(cookie) >> ((mp)->m_dircook_elog + 32)))
159 #endif
160 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_DA_COOKIE_ENTRY)
161 int xfs_da_cookie_entry(struct xfs_mount *mp, xfs_off_t cookie);
162 #define XFS_DA_COOKIE_ENTRY(mp,cookie) xfs_da_cookie_entry(mp,cookie)
163 #else
164 #define XFS_DA_COOKIE_ENTRY(mp,cookie) \
165 (((xfs_off_t)(cookie) >> 31) == -1LL ? \
166 (xfs_dablk_t)0 : \
167 (xfs_dablk_t)(((xfs_off_t)(cookie) >> 32) & \
168 ((1 << (mp)->m_dircook_elog) - 1)))
169 #endif
170
171
172 /*========================================================================
173 * Btree searching and modification structure definitions.
174 *========================================================================*/
175
176 /*
177 * Structure to ease passing around component names.
178 */
179 typedef struct xfs_da_args {
180 uchar_t *name; /* string (maybe not NULL terminated) */
181 int namelen; /* length of string (maybe no NULL) */
182 uchar_t *value; /* set of bytes (maybe contain NULLs) */
183 int valuelen; /* length of value */
184 int flags; /* argument flags (eg: ATTR_NOCREATE) */
185 xfs_dahash_t hashval; /* hash value of name */
186 xfs_ino_t inumber; /* input/output inode number */
187 struct xfs_inode *dp; /* directory inode to manipulate */
188 xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
189 struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
190 struct xfs_trans *trans; /* current trans (changes over time) */
191 xfs_extlen_t total; /* total blocks needed, for 1st bmap */
192 int whichfork; /* data or attribute fork */
193 xfs_dablk_t blkno; /* blkno of attr leaf of interest */
194 int index; /* index of attr of interest in blk */
195 xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
196 int rmtblkcnt; /* remote attr value block count */
197 int rename; /* T/F: this is an atomic rename op */
198 xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
199 int index2; /* index of 2nd attr in blk */
200 xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
201 int rmtblkcnt2; /* remote attr value block count */
202 int justcheck; /* check for ok with no space */
203 int addname; /* T/F: this is an add operation */
204 int oknoent; /* T/F: ok to return ENOENT, else die */
205 } xfs_da_args_t;
206
207 /*
208 * Structure to describe buffer(s) for a block.
209 * This is needed in the directory version 2 format case, when
210 * multiple non-contiguous fsblocks might be needed to cover one
211 * logical directory block.
212 * If the buffer count is 1 then the data pointer points to the
213 * same place as the b_addr field for the buffer, else to kmem_alloced memory.
214 */
215 typedef struct xfs_dabuf {
216 int nbuf; /* number of buffer pointers present */
217 short dirty; /* data needs to be copied back */
218 short bbcount; /* how large is data in bbs */
219 void *data; /* pointer for buffers' data */
220 #ifdef XFS_DABUF_DEBUG
221 inst_t *ra; /* return address of caller to make */
222 struct xfs_dabuf *next; /* next in global chain */
223 struct xfs_dabuf *prev; /* previous in global chain */
224 kdev_t dev; /* device for buffer */
225 xfs_daddr_t blkno; /* daddr first in bps[0] */
226 #endif
227 struct xfs_buf *bps[1]; /* actually nbuf of these */
228 } xfs_dabuf_t;
229 #define XFS_DA_BUF_SIZE(n) \
230 (sizeof(xfs_dabuf_t) + sizeof(struct xfs_buf *) * ((n) - 1))
231
232 #ifdef XFS_DABUF_DEBUG
233 extern xfs_dabuf_t *xfs_dabuf_global_list;
234 #endif
235
236 /*
237 * Storage for holding state during Btree searches and split/join ops.
238 *
239 * Only need space for 5 intermediate nodes. With a minimum of 62-way
240 * fanout to the Btree, we can support over 900 million directory blocks,
241 * which is slightly more than enough.
242 */
243 typedef struct xfs_da_state_blk {
244 xfs_dabuf_t *bp; /* buffer containing block */
245 xfs_dablk_t blkno; /* filesystem blkno of buffer */
246 xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
247 int index; /* relevant index into block */
248 xfs_dahash_t hashval; /* last hash value in block */
249 int magic; /* blk's magic number, ie: blk type */
250 } xfs_da_state_blk_t;
251
252 typedef struct xfs_da_state_path {
253 int active; /* number of active levels */
254 xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
255 } xfs_da_state_path_t;
256
257 typedef struct xfs_da_state {
258 xfs_da_args_t *args; /* filename arguments */
259 struct xfs_mount *mp; /* filesystem mount point */
260 int blocksize; /* logical block size */
261 int inleaf; /* insert into 1->lf, 0->splf */
262 xfs_da_state_path_t path; /* search/split paths */
263 xfs_da_state_path_t altpath; /* alternate path for join */
264 int extravalid; /* T/F: extrablk is in use */
265 int extraafter; /* T/F: extrablk is after new */
266 xfs_da_state_blk_t extrablk; /* for double-splits on leafs */
267 /* for dirv2 extrablk is data */
268 } xfs_da_state_t;
269
270 /*
271 * Utility macros to aid in logging changed structure fields.
272 */
273 #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
274 #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
275 (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
276 (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
277
278 /*========================================================================
279 * Function prototypes for the kernel.
280 *========================================================================*/
281
282 /*
283 * Routines used for growing the Btree.
284 */
285 int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
286 xfs_dabuf_t **bpp, int whichfork);
287 int xfs_da_split(xfs_da_state_t *state);
288
289 /*
290 * Routines used for shrinking the Btree.
291 */
292 int xfs_da_join(xfs_da_state_t *state);
293 void xfs_da_fixhashpath(xfs_da_state_t *state,
294 xfs_da_state_path_t *path_to_to_fix);
295
296 /*
297 * Routines used for finding things in the Btree.
298 */
299 int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
300 int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
301 int forward, int release, int *result);
302 /*
303 * Utility routines.
304 */
305 int xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
306 xfs_da_state_blk_t *save_blk);
307 int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
308 xfs_da_state_blk_t *new_blk);
309
310 /*
311 * Utility routines.
312 */
313 int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
314 int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
315 xfs_dablk_t bno, xfs_daddr_t mappedbno,
316 xfs_dabuf_t **bp, int whichfork);
317 int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
318 xfs_dablk_t bno, xfs_daddr_t mappedbno,
319 xfs_dabuf_t **bpp, int whichfork);
320 xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
321 xfs_dablk_t bno, int whichfork);
322 int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
323 xfs_dabuf_t *dead_buf);
324
325 uint xfs_da_hashname(uchar_t *name_string, int name_length);
326 uint xfs_da_log2_roundup(uint i);
327 xfs_da_state_t *xfs_da_state_alloc(void);
328 void xfs_da_state_free(xfs_da_state_t *state);
329 void xfs_da_state_kill_altpath(xfs_da_state_t *state);
330
331 void xfs_da_buf_done(xfs_dabuf_t *dabuf);
332 void xfs_da_log_buf(struct xfs_trans *tp, xfs_dabuf_t *dabuf, uint first,
333 uint last);
334 void xfs_da_brelse(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
335 void xfs_da_binval(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
336 xfs_daddr_t xfs_da_blkno(xfs_dabuf_t *dabuf);
337
338 extern struct xfs_zone *xfs_da_state_zone;
339
340 #endif /* __XFS_DA_BTREE_H__ */