[XFS] Ondisk format extension for extended attributes (attr2). Basically,
[safe/jmp/linux-2.6] / fs / xfs / xfs_attr_leaf.h
1 /*
2  * Copyright (c) 2000, 2002-2003, 2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * Further, this software is distributed without any warranty that it is
14  * free of the rightful claim of any third person regarding infringement
15  * or the like.  Any license provided herein, whether implied or
16  * otherwise, applies only to this software file.  Patent licenses, if
17  * any, provided herein do not apply to combinations of this program with
18  * other software, or any other product whatsoever.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write the Free Software Foundation, Inc., 59
22  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23  *
24  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25  * Mountain View, CA  94043, or:
26  *
27  * http://www.sgi.com
28  *
29  * For further information regarding this notice, see:
30  *
31  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
32  */
33 #ifndef __XFS_ATTR_LEAF_H__
34 #define __XFS_ATTR_LEAF_H__
35
36 /*
37  * Attribute storage layout, internal structure, access macros, etc.
38  *
39  * Attribute lists are structured around Btrees where all the data
40  * elements are in the leaf nodes.  Attribute names are hashed into an int,
41  * then that int is used as the index into the Btree.  Since the hashval
42  * of an attribute name may not be unique, we may have duplicate keys.  The
43  * internal links in the Btree are logical block offsets into the file.
44  */
45
46 struct attrlist;
47 struct attrlist_cursor_kern;
48 struct attrnames;
49 struct xfs_dabuf;
50 struct xfs_da_args;
51 struct xfs_da_state;
52 struct xfs_da_state_blk;
53 struct xfs_inode;
54 struct xfs_trans;
55
56 /*========================================================================
57  * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
58  *========================================================================*/
59
60 /*
61  * This is the structure of the leaf nodes in the Btree.
62  *
63  * Struct leaf_entry's are packed from the top.  Name/values grow from the
64  * bottom but are not packed.  The freemap contains run-length-encoded entries
65  * for the free bytes after the leaf_entry's, but only the N largest such,
66  * smaller runs are dropped.  When the freemap doesn't show enough space
67  * for an allocation, we compact the name/value area and try again.  If we
68  * still don't have enough space, then we have to split the block.  The
69  * name/value structs (both local and remote versions) must be 32bit aligned.
70  *
71  * Since we have duplicate hash keys, for each key that matches, compare
72  * the actual name string.  The root and intermediate node search always
73  * takes the first-in-the-block key match found, so we should only have
74  * to work "forw"ard.  If none matches, continue with the "forw"ard leaf
75  * nodes until the hash key changes or the attribute name is found.
76  *
77  * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
78  * the leaf_entry.  The namespaces are independent only because we also look
79  * at the namespace bit when we are looking for a matching attribute name.
80  *
81  * We also store a "incomplete" bit in the leaf_entry.  It shows that an
82  * attribute is in the middle of being created and should not be shown to
83  * the user if we crash during the time that the bit is set.  We clear the
84  * bit when we have finished setting up the attribute.  We do this because
85  * we cannot create some large attributes inside a single transaction, and we
86  * need some indication that we weren't finished if we crash in the middle.
87  */
88 #define XFS_ATTR_LEAF_MAPSIZE   3       /* how many freespace slots */
89
90 typedef struct xfs_attr_leafblock {
91         struct xfs_attr_leaf_hdr {      /* constant-structure header block */
92                 xfs_da_blkinfo_t info;  /* block type, links, etc. */
93                 __uint16_t count;       /* count of active leaf_entry's */
94                 __uint16_t usedbytes;   /* num bytes of names/values stored */
95                 __uint16_t firstused;   /* first used byte in name area */
96                 __uint8_t  holes;       /* != 0 if blk needs compaction */
97                 __uint8_t  pad1;
98                 struct xfs_attr_leaf_map {        /* RLE map of free bytes */
99                         __uint16_t base;          /* base of free region */
100                         __uint16_t size;          /* length of free region */
101                 } freemap[XFS_ATTR_LEAF_MAPSIZE]; /* N largest free regions */
102         } hdr;
103         struct xfs_attr_leaf_entry {    /* sorted on key, not name */
104                 xfs_dahash_t hashval;   /* hash value of name */
105                 __uint16_t nameidx;     /* index into buffer of name/value */
106                 __uint8_t flags;        /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
107                 __uint8_t pad2;         /* unused pad byte */
108         } entries[1];                   /* variable sized array */
109         struct xfs_attr_leaf_name_local {
110                 __uint16_t valuelen;    /* number of bytes in value */
111                 __uint8_t namelen;      /* length of name bytes */
112                 __uint8_t nameval[1];   /* name/value bytes */
113         } namelist;                     /* grows from bottom of buf */
114         struct xfs_attr_leaf_name_remote {
115                 xfs_dablk_t valueblk;   /* block number of value bytes */
116                 __uint32_t valuelen;    /* number of bytes in value */
117                 __uint8_t namelen;      /* length of name bytes */
118                 __uint8_t name[1];      /* name bytes */
119         } valuelist;                    /* grows from bottom of buf */
120 } xfs_attr_leafblock_t;
121 typedef struct xfs_attr_leaf_hdr xfs_attr_leaf_hdr_t;
122 typedef struct xfs_attr_leaf_map xfs_attr_leaf_map_t;
123 typedef struct xfs_attr_leaf_entry xfs_attr_leaf_entry_t;
124 typedef struct xfs_attr_leaf_name_local xfs_attr_leaf_name_local_t;
125 typedef struct xfs_attr_leaf_name_remote xfs_attr_leaf_name_remote_t;
126
127 /*
128  * Flags used in the leaf_entry[i].flags field.
129  * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
130  * on the system call, they are "or"ed together for various operations.
131  */
132 #define XFS_ATTR_LOCAL_BIT      0       /* attr is stored locally */
133 #define XFS_ATTR_ROOT_BIT       1       /* limit access to trusted attrs */
134 #define XFS_ATTR_SECURE_BIT     2       /* limit access to secure attrs */
135 #define XFS_ATTR_INCOMPLETE_BIT 7       /* attr in middle of create/delete */
136 #define XFS_ATTR_LOCAL          (1 << XFS_ATTR_LOCAL_BIT)
137 #define XFS_ATTR_ROOT           (1 << XFS_ATTR_ROOT_BIT)
138 #define XFS_ATTR_SECURE         (1 << XFS_ATTR_SECURE_BIT)
139 #define XFS_ATTR_INCOMPLETE     (1 << XFS_ATTR_INCOMPLETE_BIT)
140
141 /*
142  * Alignment for namelist and valuelist entries (since they are mixed
143  * there can be only one alignment value)
144  */
145 #define XFS_ATTR_LEAF_NAME_ALIGN        ((uint)sizeof(xfs_dablk_t))
146
147 /*
148  * Cast typed pointers for "local" and "remote" name/value structs.
149  */
150 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_NAME_REMOTE)
151 xfs_attr_leaf_name_remote_t *
152 xfs_attr_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx);
153 #define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx)    \
154         xfs_attr_leaf_name_remote(leafp,idx)
155 #else
156 #define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx)    /* remote name struct ptr */ \
157         ((xfs_attr_leaf_name_remote_t *)                \
158          &((char *)(leafp))[ INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT) ])
159 #endif
160 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_NAME_LOCAL)
161 xfs_attr_leaf_name_local_t *
162 xfs_attr_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx);
163 #define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx)     \
164         xfs_attr_leaf_name_local(leafp,idx)
165 #else
166 #define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx)     /* local name struct ptr */ \
167         ((xfs_attr_leaf_name_local_t *)         \
168          &((char *)(leafp))[ INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT) ])
169 #endif
170 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_NAME)
171 char *xfs_attr_leaf_name(xfs_attr_leafblock_t *leafp, int idx);
172 #define XFS_ATTR_LEAF_NAME(leafp,idx)           xfs_attr_leaf_name(leafp,idx)
173 #else
174 #define XFS_ATTR_LEAF_NAME(leafp,idx)           /* generic name struct ptr */ \
175         (&((char *)(leafp))[ INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT) ])
176 #endif
177
178 /*
179  * Calculate total bytes used (including trailing pad for alignment) for
180  * a "local" name/value structure, a "remote" name/value structure, and
181  * a pointer which might be either.
182  */
183 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_ENTSIZE_REMOTE)
184 int xfs_attr_leaf_entsize_remote(int nlen);
185 #define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen)      \
186         xfs_attr_leaf_entsize_remote(nlen)
187 #else
188 #define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen)      /* space for remote struct */ \
189         (((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
190           XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1))
191 #endif
192 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_ENTSIZE_LOCAL)
193 int xfs_attr_leaf_entsize_local(int nlen, int vlen);
194 #define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen)  \
195         xfs_attr_leaf_entsize_local(nlen,vlen)
196 #else
197 #define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen)  /* space for local struct */ \
198         (((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) + \
199           XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1))
200 #endif
201 #if XFS_WANT_FUNCS || (XFS_WANT_SPACE && XFSSO_XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX)
202 int xfs_attr_leaf_entsize_local_max(int bsize);
203 #define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize)  \
204         xfs_attr_leaf_entsize_local_max(bsize)
205 #else
206 #define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize)  /* max local struct size */ \
207         (((bsize) >> 1) + ((bsize) >> 2))
208 #endif
209
210
211 /*========================================================================
212  * Structure used to pass context around among the routines.
213  *========================================================================*/
214
215 typedef struct xfs_attr_list_context {
216         struct xfs_inode                *dp;    /* inode */
217         struct attrlist_cursor_kern     *cursor;/* position in list */
218         struct attrlist                 *alist; /* output buffer */
219         int                             count;  /* num used entries */
220         int                             dupcnt; /* count dup hashvals seen */
221         int                             bufsize;/* total buffer size */
222         int                             firstu; /* first used byte in buffer */
223         int                             flags;  /* from VOP call */
224         int                             resynch;/* T/F: resynch with cursor */
225 } xfs_attr_list_context_t;
226
227 /*
228  * Used to keep a list of "remote value" extents when unlinking an inode.
229  */
230 typedef struct xfs_attr_inactive_list {
231         xfs_dablk_t     valueblk;       /* block number of value bytes */
232         int             valuelen;       /* number of bytes in value */
233 } xfs_attr_inactive_list_t;
234
235
236 /*========================================================================
237  * Function prototypes for the kernel.
238  *========================================================================*/
239
240 /*
241  * Internal routines when attribute fork size < XFS_LITINO(mp).
242  */
243 void    xfs_attr_shortform_create(struct xfs_da_args *args);
244 void    xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
245 int     xfs_attr_shortform_lookup(struct xfs_da_args *args);
246 int     xfs_attr_shortform_getvalue(struct xfs_da_args *args);
247 int     xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
248 int     xfs_attr_shortform_remove(struct xfs_da_args *args);
249 int     xfs_attr_shortform_list(struct xfs_attr_list_context *context);
250 int     xfs_attr_shortform_allfit(struct xfs_dabuf *bp, struct xfs_inode *dp);
251 int     xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
252
253
254 /*
255  * Internal routines when attribute fork size == XFS_LBSIZE(mp).
256  */
257 int     xfs_attr_leaf_to_node(struct xfs_da_args *args);
258 int     xfs_attr_leaf_to_shortform(struct xfs_dabuf *bp,
259                                    struct xfs_da_args *args, int forkoff);
260 int     xfs_attr_leaf_clearflag(struct xfs_da_args *args);
261 int     xfs_attr_leaf_setflag(struct xfs_da_args *args);
262 int     xfs_attr_leaf_flipflags(xfs_da_args_t *args);
263
264 /*
265  * Routines used for growing the Btree.
266  */
267 int     xfs_attr_leaf_split(struct xfs_da_state *state,
268                                    struct xfs_da_state_blk *oldblk,
269                                    struct xfs_da_state_blk *newblk);
270 int     xfs_attr_leaf_lookup_int(struct xfs_dabuf *leaf,
271                                         struct xfs_da_args *args);
272 int     xfs_attr_leaf_getvalue(struct xfs_dabuf *bp, struct xfs_da_args *args);
273 int     xfs_attr_leaf_add(struct xfs_dabuf *leaf_buffer,
274                                  struct xfs_da_args *args);
275 int     xfs_attr_leaf_remove(struct xfs_dabuf *leaf_buffer,
276                                     struct xfs_da_args *args);
277 int     xfs_attr_leaf_list_int(struct xfs_dabuf *bp,
278                                       struct xfs_attr_list_context *context);
279
280 /*
281  * Routines used for shrinking the Btree.
282  */
283 int     xfs_attr_leaf_toosmall(struct xfs_da_state *state, int *retval);
284 void    xfs_attr_leaf_unbalance(struct xfs_da_state *state,
285                                        struct xfs_da_state_blk *drop_blk,
286                                        struct xfs_da_state_blk *save_blk);
287 int     xfs_attr_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
288
289 /*
290  * Utility routines.
291  */
292 xfs_dahash_t    xfs_attr_leaf_lasthash(struct xfs_dabuf *bp, int *count);
293 int     xfs_attr_leaf_order(struct xfs_dabuf *leaf1_bp,
294                                    struct xfs_dabuf *leaf2_bp);
295 int     xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
296                                         int *local);
297 int     xfs_attr_rolltrans(struct xfs_trans **transp, struct xfs_inode *dp);
298
299 #endif  /* __XFS_ATTR_LEAF_H__ */