blob: 0854b95b1dc1c967ff201caead9d233581b68b8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2002,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_DA_BTREE_H__
19#define __XFS_DA_BTREE_H__
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021struct xfs_bmap_free;
22struct xfs_inode;
23struct xfs_mount;
24struct xfs_trans;
25struct zone;
26
27/*========================================================================
28 * Directory Structure when greater than XFS_LBSIZE(mp) bytes.
29 *========================================================================*/
30
31/*
32 * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
33 *
Chen Baozi96ee34b2012-07-14 03:38:13 +080034 * It is used to manage a doubly linked list of all blocks at the same
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level in the Btree, and to identify which type of block this is.
36 */
37#define XFS_DA_NODE_MAGIC 0xfebe /* magic number: non-leaf blocks */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define XFS_ATTR_LEAF_MAGIC 0xfbee /* magic number: attribute leaf blks */
39#define XFS_DIR2_LEAF1_MAGIC 0xd2f1 /* magic number: v2 dirlf single blks */
40#define XFS_DIR2_LEAFN_MAGIC 0xd2ff /* magic number: v2 dirlf multi blks */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042typedef struct xfs_da_blkinfo {
Nathan Scott89da0542006-03-17 17:28:40 +110043 __be32 forw; /* previous block in list */
44 __be32 back; /* following block in list */
45 __be16 magic; /* validity check on block */
46 __be16 pad; /* unused */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047} xfs_da_blkinfo_t;
48
49/*
Dave Chinner24df33b2013-04-12 07:30:21 +100050 * CRC enabled directory structure types
51 *
52 * The headers change size for the additional verification information, but
53 * otherwise the tree layouts and contents are unchanged.
54 */
55#define XFS_DIR3_LEAF1_MAGIC 0x3df1 /* magic number: v2 dirlf single blks */
56#define XFS_DIR3_LEAFN_MAGIC 0x3dff /* magic number: v2 dirlf multi blks */
57
58struct xfs_da3_blkinfo {
59 /*
60 * the node link manipulation code relies on the fact that the first
61 * element of this structure is the struct xfs_da_blkinfo so it can
62 * ignore the differences in the rest of the structures.
63 */
64 struct xfs_da_blkinfo hdr;
65 __be32 crc; /* CRC of block */
66 __be64 blkno; /* first block of the buffer */
67 __be64 lsn; /* sequence number of last write */
68 uuid_t uuid; /* filesystem we belong to */
69 __be64 owner; /* inode that owns the block */
70};
71
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * This is the structure of the root and intermediate nodes in the Btree.
74 * The leaf nodes are defined above.
75 *
76 * Entries are not packed.
77 *
78 * Since we have duplicate keys, use a binary search but always follow
79 * all match in the block, not just the first match found.
80 */
81#define XFS_DA_NODE_MAXDEPTH 5 /* max depth of Btree */
82
83typedef struct xfs_da_intnode {
84 struct xfs_da_node_hdr { /* constant-structure header block */
85 xfs_da_blkinfo_t info; /* block type, links, etc. */
Nathan Scottfac80cc2006-03-17 17:29:56 +110086 __be16 count; /* count of active entries */
87 __be16 level; /* level above leaves (leaf == 0) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } hdr;
89 struct xfs_da_node_entry {
Nathan Scott403432d2006-03-17 17:29:46 +110090 __be32 hashval; /* hash value for this descendant */
91 __be32 before; /* Btree block before this key */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 } btree[1]; /* variable sized array of keys */
93} xfs_da_intnode_t;
94typedef struct xfs_da_node_hdr xfs_da_node_hdr_t;
95typedef struct xfs_da_node_entry xfs_da_node_entry_t;
96
Nathan Scotta844f452005-11-02 14:38:42 +110097#define XFS_LBSIZE(mp) (mp)->m_sb.sb_blocksize
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/*========================================================================
100 * Btree searching and modification structure definitions.
101 *========================================================================*/
102
103/*
Barry Naujok5163f952008-05-21 16:41:01 +1000104 * Search comparison results
105 */
106enum xfs_dacmp {
107 XFS_CMP_DIFFERENT, /* names are completely different */
108 XFS_CMP_EXACT, /* names are exactly the same */
109 XFS_CMP_CASE /* names are same but differ in case */
110};
111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Structure to ease passing around component names.
114 */
115typedef struct xfs_da_args {
Christoph Hellwiga5687782009-02-09 08:37:39 +0100116 const __uint8_t *name; /* string (maybe not NULL terminated) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int namelen; /* length of string (maybe no NULL) */
Christoph Hellwiga5687782009-02-09 08:37:39 +0100118 __uint8_t *value; /* set of bytes (maybe contain NULLs) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 int valuelen; /* length of value */
120 int flags; /* argument flags (eg: ATTR_NOCREATE) */
121 xfs_dahash_t hashval; /* hash value of name */
122 xfs_ino_t inumber; /* input/output inode number */
123 struct xfs_inode *dp; /* directory inode to manipulate */
124 xfs_fsblock_t *firstblock; /* ptr to firstblock for bmap calls */
125 struct xfs_bmap_free *flist; /* ptr to freelist for bmap_finish */
126 struct xfs_trans *trans; /* current trans (changes over time) */
127 xfs_extlen_t total; /* total blocks needed, for 1st bmap */
128 int whichfork; /* data or attribute fork */
129 xfs_dablk_t blkno; /* blkno of attr leaf of interest */
130 int index; /* index of attr of interest in blk */
131 xfs_dablk_t rmtblkno; /* remote attr value starting blkno */
132 int rmtblkcnt; /* remote attr value block count */
133 xfs_dablk_t blkno2; /* blkno of 2nd attr leaf of interest */
134 int index2; /* index of 2nd attr in blk */
135 xfs_dablk_t rmtblkno2; /* remote attr value starting blkno */
136 int rmtblkcnt2; /* remote attr value block count */
Barry Naujok6a178102008-05-21 16:42:05 +1000137 int op_flags; /* operation flags */
Barry Naujok5163f952008-05-21 16:41:01 +1000138 enum xfs_dacmp cmpresult; /* name compare result for lookups */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139} xfs_da_args_t;
140
141/*
Barry Naujok6a178102008-05-21 16:42:05 +1000142 * Operation flags:
143 */
144#define XFS_DA_OP_JUSTCHECK 0x0001 /* check for ok with no space */
145#define XFS_DA_OP_RENAME 0x0002 /* this is an atomic rename op */
146#define XFS_DA_OP_ADDNAME 0x0004 /* this is an add operation */
147#define XFS_DA_OP_OKNOENT 0x0008 /* lookup/add op, ENOENT ok, else die */
Barry Naujok384f3ce2008-05-21 16:58:22 +1000148#define XFS_DA_OP_CILOOKUP 0x0010 /* lookup to return CI name if found */
Barry Naujok6a178102008-05-21 16:42:05 +1000149
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000150#define XFS_DA_OP_FLAGS \
151 { XFS_DA_OP_JUSTCHECK, "JUSTCHECK" }, \
152 { XFS_DA_OP_RENAME, "RENAME" }, \
153 { XFS_DA_OP_ADDNAME, "ADDNAME" }, \
154 { XFS_DA_OP_OKNOENT, "OKNOENT" }, \
155 { XFS_DA_OP_CILOOKUP, "CILOOKUP" }
156
Barry Naujok6a178102008-05-21 16:42:05 +1000157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 * Storage for holding state during Btree searches and split/join ops.
159 *
160 * Only need space for 5 intermediate nodes. With a minimum of 62-way
161 * fanout to the Btree, we can support over 900 million directory blocks,
162 * which is slightly more than enough.
163 */
164typedef struct xfs_da_state_blk {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000165 struct xfs_buf *bp; /* buffer containing block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 xfs_dablk_t blkno; /* filesystem blkno of buffer */
167 xfs_daddr_t disk_blkno; /* on-disk blkno (in BBs) of buffer */
168 int index; /* relevant index into block */
169 xfs_dahash_t hashval; /* last hash value in block */
170 int magic; /* blk's magic number, ie: blk type */
171} xfs_da_state_blk_t;
172
173typedef struct xfs_da_state_path {
174 int active; /* number of active levels */
175 xfs_da_state_blk_t blk[XFS_DA_NODE_MAXDEPTH];
176} xfs_da_state_path_t;
177
178typedef struct xfs_da_state {
179 xfs_da_args_t *args; /* filename arguments */
180 struct xfs_mount *mp; /* filesystem mount point */
181 unsigned int blocksize; /* logical block size */
182 unsigned int node_ents; /* how many entries in danode */
183 xfs_da_state_path_t path; /* search/split paths */
184 xfs_da_state_path_t altpath; /* alternate path for join */
185 unsigned char inleaf; /* insert into 1->lf, 0->splf */
186 unsigned char extravalid; /* T/F: extrablk is in use */
187 unsigned char extraafter; /* T/F: extrablk is after new */
Malcolm Parsons9da096f2009-03-29 09:55:42 +0200188 xfs_da_state_blk_t extrablk; /* for double-splits on leaves */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* for dirv2 extrablk is data */
190} xfs_da_state_t;
191
192/*
193 * Utility macros to aid in logging changed structure fields.
194 */
195#define XFS_DA_LOGOFF(BASE, ADDR) ((char *)(ADDR) - (char *)(BASE))
196#define XFS_DA_LOGRANGE(BASE, ADDR, SIZE) \
197 (uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
198 (uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
199
Barry Naujok5163f952008-05-21 16:41:01 +1000200/*
201 * Name ops for directory and/or attr name operations
202 */
203struct xfs_nameops {
204 xfs_dahash_t (*hashname)(struct xfs_name *);
Dave Chinner2bc75422010-01-20 10:47:17 +1100205 enum xfs_dacmp (*compname)(struct xfs_da_args *,
206 const unsigned char *, int);
Barry Naujok5163f952008-05-21 16:41:01 +1000207};
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*========================================================================
Barry Naujok847fff52008-10-30 17:05:38 +1100211 * Function prototypes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *========================================================================*/
213
214/*
215 * Routines used for growing the Btree.
216 */
217int xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000218 struct xfs_buf **bpp, int whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219int xfs_da_split(xfs_da_state_t *state);
220
221/*
222 * Routines used for shrinking the Btree.
223 */
224int xfs_da_join(xfs_da_state_t *state);
225void xfs_da_fixhashpath(xfs_da_state_t *state,
226 xfs_da_state_path_t *path_to_to_fix);
227
228/*
229 * Routines used for finding things in the Btree.
230 */
231int xfs_da_node_lookup_int(xfs_da_state_t *state, int *result);
232int xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
233 int forward, int release, int *result);
234/*
235 * Utility routines.
236 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237int xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
238 xfs_da_state_blk_t *new_blk);
Dave Chinnerd9392a42012-11-12 22:54:17 +1100239int xfs_da_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
240 xfs_dablk_t bno, xfs_daddr_t mappedbno,
241 struct xfs_buf **bpp, int which_fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243/*
244 * Utility routines.
245 */
246int xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
Christoph Hellwig77936d02011-07-13 13:43:49 +0200247int xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
248 int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249int xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
250 xfs_dablk_t bno, xfs_daddr_t mappedbno,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000251 struct xfs_buf **bp, int whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252int xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
253 xfs_dablk_t bno, xfs_daddr_t mappedbno,
Dave Chinner4bb20a82012-11-12 22:54:10 +1100254 struct xfs_buf **bpp, int whichfork,
Dave Chinner1813dd62012-11-14 17:54:40 +1100255 const struct xfs_buf_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256xfs_daddr_t xfs_da_reada_buf(struct xfs_trans *trans, struct xfs_inode *dp,
Dave Chinnerda6958c2012-11-12 22:54:18 +1100257 xfs_dablk_t bno, xfs_daddr_t mapped_bno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100258 int whichfork, const struct xfs_buf_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259int xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000260 struct xfs_buf *dead_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Christoph Hellwiga5687782009-02-09 08:37:39 +0100262uint xfs_da_hashname(const __uint8_t *name_string, int name_length);
Barry Naujok5163f952008-05-21 16:41:01 +1000263enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
Dave Chinner2bc75422010-01-20 10:47:17 +1100264 const unsigned char *name, int len);
Barry Naujok5163f952008-05-21 16:41:01 +1000265
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267xfs_da_state_t *xfs_da_state_alloc(void);
268void xfs_da_state_free(xfs_da_state_t *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270extern struct kmem_zone *xfs_da_state_zone;
Hannes Eder24418492009-03-04 19:33:48 +0100271extern const struct xfs_nameops xfs_default_nameops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273#endif /* __XFS_DA_BTREE_H__ */