2 * Copyright (c) 2000, 2002 Silicon Graphics, Inc. All Rights Reserved.
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.
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.
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.
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.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32 #ifndef __XFS_DA_BTREE_H__
33 #define __XFS_DA_BTREE_H__
42 /*========================================================================
43 * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
44 *========================================================================*/
47 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
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.
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 */
58 #define XFS_DIRX_LEAF_MAGIC(mp) \
59 (XFS_DIR_IS_V1(mp) ? XFS_DIR_LEAF_MAGIC : XFS_DIR2_LEAFN_MAGIC)
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 */
69 * This is the structure of the root and intermediate nodes in the Btree.
70 * The leaf nodes are defined above.
72 * Entries are not packed.
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.
77 #define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
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) */
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 */
90 typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
91 typedef struct xfs_da_node_entry xfs_da_node_entry_t;
93 #define XFS_DA_MAXHASH ((xfs_dahash_t)-1) /* largest valid hash value */
95 #define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
96 #define XFS_LBLOG(mp) (mp)->m_sb.sb_blocklog
98 #define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
99 (((bno) << (mp)->m_dircook_elog) | (entry))
100 #define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
101 (((xfs_off_t)XFS_DA_MAKE_BNOENTRY(mp, bno, entry) << 32) | (hash))
102 #define XFS_DA_COOKIE_HASH(mp,cookie) ((xfs_dahash_t)cookie)
103 #define XFS_DA_COOKIE_BNO(mp,cookie) \
104 ((((xfs_off_t)(cookie) >> 31) == -1LL ? \
106 (xfs_dablk_t)((xfs_off_t)(cookie) >> \
107 ((mp)->m_dircook_elog + 32))))
108 #define XFS_DA_COOKIE_ENTRY(mp,cookie) \
109 ((((xfs_off_t)(cookie) >> 31) == -1LL ? \
111 (xfs_dablk_t)(((xfs_off_t)(cookie) >> 32) & \
112 ((1 << (mp)->m_dircook_elog) - 1))))
115 /*========================================================================
116 * Btree searching and modification structure definitions.
117 *========================================================================*/
120 * Structure to ease passing around component names.
122 typedef struct xfs_da_args {
123 const uchar_t *name; /* string (maybe not NULL terminated) */
124 int namelen; /* length of string (maybe no NULL) */
125 uchar_t *value; /* set of bytes (maybe contain NULLs) */
126 int valuelen; /* length of value */
127 int flags; /* argument flags (eg: ATTR_NOCREATE) */
128 xfs_dahash_t hashval; /* hash value of name */
129 xfs_ino_t inumber; /* input/output inode number */
130 struct xfs_inode *dp; /* directory inode to manipulate */
131 xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
132 struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
133 struct xfs_trans *trans; /* current trans (changes over time) */
134 xfs_extlen_t total; /* total blocks needed, for 1st bmap */
135 int whichfork; /* data or attribute fork */
136 xfs_dablk_t blkno; /* blkno of attr leaf of interest */
137 int index; /* index of attr of interest in blk */
138 xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
139 int rmtblkcnt; /* remote attr value block count */
140 xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
141 int index2; /* index of 2nd attr in blk */
142 xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
143 int rmtblkcnt2; /* remote attr value block count */
144 unsigned char justcheck; /* T/F: check for ok with no space */
145 unsigned char rename; /* T/F: this is an atomic rename op */
146 unsigned char addname; /* T/F: this is an add operation */
147 unsigned char oknoent; /* T/F: ok to return ENOENT, else die */
151 * Structure to describe buffer(s) for a block.
152 * This is needed in the directory version 2 format case, when
153 * multiple non-contiguous fsblocks might be needed to cover one
154 * logical directory block.
155 * If the buffer count is 1 then the data pointer points to the
156 * same place as the b_addr field for the buffer, else to kmem_alloced memory.
158 typedef struct xfs_dabuf {
159 int nbuf; /* number of buffer pointers present */
160 short dirty; /* data needs to be copied back */
161 short bbcount; /* how large is data in bbs */
162 void *data; /* pointer for buffers' data */
163 #ifdef XFS_DABUF_DEBUG
164 inst_t *ra; /* return address of caller to make */
165 struct xfs_dabuf *next; /* next in global chain */
166 struct xfs_dabuf *prev; /* previous in global chain */
167 struct xfs_buftarg *target; /* device for buffer */
168 xfs_daddr_t blkno; /* daddr first in bps[0] */
170 struct xfs_buf *bps[1]; /* actually nbuf of these */
172 #define XFS_DA_BUF_SIZE(n) \
173 (sizeof(xfs_dabuf_t) + sizeof(struct xfs_buf *) * ((n) - 1))
175 #ifdef XFS_DABUF_DEBUG
176 extern xfs_dabuf_t *xfs_dabuf_global_list;
180 * Storage for holding state during Btree searches and split/join ops.
182 * Only need space for 5 intermediate nodes. With a minimum of 62-way
183 * fanout to the Btree, we can support over 900 million directory blocks,
184 * which is slightly more than enough.
186 typedef struct xfs_da_state_blk {
187 xfs_dabuf_t *bp; /* buffer containing block */
188 xfs_dablk_t blkno; /* filesystem blkno of buffer */
189 xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
190 int index; /* relevant index into block */
191 xfs_dahash_t hashval; /* last hash value in block */
192 int magic; /* blk's magic number, ie: blk type */
193 } xfs_da_state_blk_t;
195 typedef struct xfs_da_state_path {
196 int active; /* number of active levels */
197 xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
198 } xfs_da_state_path_t;
200 typedef struct xfs_da_state {
201 xfs_da_args_t *args; /* filename arguments */
202 struct xfs_mount *mp; /* filesystem mount point */
203 unsigned int blocksize; /* logical block size */
204 unsigned int node_ents; /* how many entries in danode */
205 xfs_da_state_path_t path; /* search/split paths */
206 xfs_da_state_path_t altpath; /* alternate path for join */
207 unsigned char inleaf; /* insert into 1->lf, 0->splf */
208 unsigned char extravalid; /* T/F: extrablk is in use */
209 unsigned char extraafter; /* T/F: extrablk is after new */
210 xfs_da_state_blk_t extrablk; /* for double-splits on leafs */
211 /* for dirv2 extrablk is data */
215 * Utility macros to aid in logging changed structure fields.
217 #define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
218 #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
219 (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
220 (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
224 /*========================================================================
225 * Function prototypes for the kernel.
226 *========================================================================*/
229 * Routines used for growing the Btree.
231 int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
232 xfs_dabuf_t **bpp, int whichfork);
233 int xfs_da_split(xfs_da_state_t *state);
236 * Routines used for shrinking the Btree.
238 int xfs_da_join(xfs_da_state_t *state);
239 void xfs_da_fixhashpath(xfs_da_state_t *state,
240 xfs_da_state_path_t *path_to_to_fix);
243 * Routines used for finding things in the Btree.
245 int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
246 int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
247 int forward, int release, int *result);
251 int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
252 xfs_da_state_blk_t *new_blk);
257 int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
258 int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
259 xfs_dablk_t bno, xfs_daddr_t mappedbno,
260 xfs_dabuf_t **bp, int whichfork);
261 int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
262 xfs_dablk_t bno, xfs_daddr_t mappedbno,
263 xfs_dabuf_t **bpp, int whichfork);
264 xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
265 xfs_dablk_t bno, int whichfork);
266 int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
267 xfs_dabuf_t *dead_buf);
269 uint xfs_da_hashname(const uchar_t *name_string, int name_length);
270 uint xfs_da_log2_roundup(uint i);
271 xfs_da_state_t *xfs_da_state_alloc(void);
272 void xfs_da_state_free(xfs_da_state_t *state);
274 void xfs_da_buf_done(xfs_dabuf_t *dabuf);
275 void xfs_da_log_buf(struct xfs_trans *tp, xfs_dabuf_t *dabuf, uint first,
277 void xfs_da_brelse(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
278 void xfs_da_binval(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
279 xfs_daddr_t xfs_da_blkno(xfs_dabuf_t *dabuf);
281 extern struct kmem_zone *xfs_da_state_zone;
282 #endif /* __KERNEL__ */
284 #endif /* __XFS_DA_BTREE_H__ */