blob: 654956e9787c7347eb5d68b05272ed757627d55d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2000, 2002 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
35struct xfs_buf;
36struct xfs_bmap_free;
37struct xfs_inode;
38struct xfs_mount;
39struct xfs_trans;
40struct 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
61typedef 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
79typedef 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;
90typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
91typedef struct xfs_da_node_entry xfs_da_node_entry_t;
92
93#define XFS_DA_MAXHASH ((xfs_dahash_t)-1) /* largest valid hash value */
94
Nathan Scotta844f452005-11-02 14:38:42 +110095#define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
96#define XFS_LBLOG(mp) (mp)->m_sb.sb_blocklog
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define XFS_DA_MAKE_BNOENTRY(mp,bno,entry) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 (((bno) << (mp)->m_dircook_elog) | (entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#define XFS_DA_MAKE_COOKIE(mp,bno,entry,hash) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 (((xfs_off_t)XFS_DA_MAKE_BNOENTRY(mp, bno, entry) << 32) | (hash))
Nathan Scotta844f452005-11-02 14:38:42 +1100102#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 ? \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 (xfs_dablk_t)0 : \
Nathan Scotta844f452005-11-02 14:38:42 +1100106 (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 ? \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 (xfs_dablk_t)0 : \
111 (xfs_dablk_t)(((xfs_off_t)(cookie) >> 32) & \
Nathan Scotta844f452005-11-02 14:38:42 +1100112 ((1 << (mp)->m_dircook_elog) - 1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114
115/*========================================================================
116 * Btree searching and modification structure definitions.
117 *========================================================================*/
118
119/*
120 * Structure to ease passing around component names.
121 */
122typedef struct xfs_da_args {
Nathan Scott80cce772005-11-02 11:43:04 +1100123 const uchar_t *name; /* string (maybe not NULL terminated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 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 */
148} xfs_da_args_t;
149
150/*
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.
157 */
158typedef 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] */
169#endif
170 struct xfs_buf *bps[1]; /* actually nbuf of these */
171} xfs_dabuf_t;
172#define XFS_DA_BUF_SIZE(n) \
173 (sizeof(xfs_dabuf_t) + sizeof(struct xfs_buf *) * ((n) - 1))
174
175#ifdef XFS_DABUF_DEBUG
176extern xfs_dabuf_t *xfs_dabuf_global_list;
177#endif
178
179/*
180 * Storage for holding state during Btree searches and split/join ops.
181 *
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.
185 */
186typedef 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;
194
195typedef 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;
199
200typedef 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 */
212} xfs_da_state_t;
213
214/*
215 * Utility macros to aid in logging changed structure fields.
216 */
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)
221
222
223#ifdef __KERNEL__
224/*========================================================================
225 * Function prototypes for the kernel.
226 *========================================================================*/
227
228/*
229 * Routines used for growing the Btree.
230 */
231int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
232 xfs_dabuf_t **bpp, int whichfork);
233int xfs_da_split(xfs_da_state_t *state);
234
235/*
236 * Routines used for shrinking the Btree.
237 */
238int xfs_da_join(xfs_da_state_t *state);
239void xfs_da_fixhashpath(xfs_da_state_t *state,
240 xfs_da_state_path_t *path_to_to_fix);
241
242/*
243 * Routines used for finding things in the Btree.
244 */
245int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
246int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
247 int forward, int release, int *result);
248/*
249 * Utility routines.
250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
252 xfs_da_state_blk_t *new_blk);
253
254/*
255 * Utility routines.
256 */
257int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
258int 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);
261int 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);
264xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
265 xfs_dablk_t bno, int whichfork);
266int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
267 xfs_dabuf_t *dead_buf);
268
Nathan Scott80cce772005-11-02 11:43:04 +1100269uint xfs_da_hashname(const uchar_t *name_string, int name_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270uint xfs_da_log2_roundup(uint i);
271xfs_da_state_t *xfs_da_state_alloc(void);
272void xfs_da_state_free(xfs_da_state_t *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274void xfs_da_buf_done(xfs_dabuf_t *dabuf);
275void xfs_da_log_buf(struct xfs_trans *tp, xfs_dabuf_t *dabuf, uint first,
276 uint last);
277void xfs_da_brelse(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
278void xfs_da_binval(struct xfs_trans *tp, xfs_dabuf_t *dabuf);
279xfs_daddr_t xfs_da_blkno(xfs_dabuf_t *dabuf);
280
281extern struct kmem_zone *xfs_da_state_zone;
282#endif /* __KERNEL__ */
283
284#endif /* __XFS_DA_BTREE_H__ */