blob: f6143ff251a019e90ec01673bcf69d8a835d3c1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000,2002-2003,2005 Silicon Graphics, Inc.
Nathan Scottaa82daa2005-11-02 10:33:33 +11003 * 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_ATTR_LEAF_H__
19#define __XFS_ATTR_LEAF_H__
20
21/*
22 * Attribute storage layout, internal structure, access macros, etc.
23 *
24 * Attribute lists are structured around Btrees where all the data
25 * elements are in the leaf nodes. Attribute names are hashed into an int,
26 * then that int is used as the index into the Btree. Since the hashval
27 * of an attribute name may not be unique, we may have duplicate keys. The
28 * internal links in the Btree are logical block offsets into the file.
29 */
30
31struct attrlist;
32struct attrlist_cursor_kern;
33struct attrnames;
34struct xfs_dabuf;
35struct xfs_da_args;
36struct xfs_da_state;
37struct xfs_da_state_blk;
38struct xfs_inode;
39struct xfs_trans;
40
41/*========================================================================
42 * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
43 *========================================================================*/
44
45/*
46 * This is the structure of the leaf nodes in the Btree.
47 *
48 * Struct leaf_entry's are packed from the top. Name/values grow from the
49 * bottom but are not packed. The freemap contains run-length-encoded entries
50 * for the free bytes after the leaf_entry's, but only the N largest such,
51 * smaller runs are dropped. When the freemap doesn't show enough space
52 * for an allocation, we compact the name/value area and try again. If we
53 * still don't have enough space, then we have to split the block. The
54 * name/value structs (both local and remote versions) must be 32bit aligned.
55 *
56 * Since we have duplicate hash keys, for each key that matches, compare
57 * the actual name string. The root and intermediate node search always
58 * takes the first-in-the-block key match found, so we should only have
59 * to work "forw"ard. If none matches, continue with the "forw"ard leaf
60 * nodes until the hash key changes or the attribute name is found.
61 *
62 * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
63 * the leaf_entry. The namespaces are independent only because we also look
64 * at the namespace bit when we are looking for a matching attribute name.
65 *
66 * We also store a "incomplete" bit in the leaf_entry. It shows that an
67 * attribute is in the middle of being created and should not be shown to
68 * the user if we crash during the time that the bit is set. We clear the
69 * bit when we have finished setting up the attribute. We do this because
70 * we cannot create some large attributes inside a single transaction, and we
71 * need some indication that we weren't finished if we crash in the middle.
72 */
73#define XFS_ATTR_LEAF_MAPSIZE 3 /* how many freespace slots */
74
75typedef struct xfs_attr_leafblock {
76 struct xfs_attr_leaf_hdr { /* constant-structure header block */
77 xfs_da_blkinfo_t info; /* block type, links, etc. */
78 __uint16_t count; /* count of active leaf_entry's */
79 __uint16_t usedbytes; /* num bytes of names/values stored */
80 __uint16_t firstused; /* first used byte in name area */
81 __uint8_t holes; /* != 0 if blk needs compaction */
82 __uint8_t pad1;
83 struct xfs_attr_leaf_map { /* RLE map of free bytes */
84 __uint16_t base; /* base of free region */
85 __uint16_t size; /* length of free region */
86 } freemap[XFS_ATTR_LEAF_MAPSIZE]; /* N largest free regions */
87 } hdr;
88 struct xfs_attr_leaf_entry { /* sorted on key, not name */
89 xfs_dahash_t hashval; /* hash value of name */
90 __uint16_t nameidx; /* index into buffer of name/value */
91 __uint8_t flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
92 __uint8_t pad2; /* unused pad byte */
93 } entries[1]; /* variable sized array */
94 struct xfs_attr_leaf_name_local {
95 __uint16_t valuelen; /* number of bytes in value */
96 __uint8_t namelen; /* length of name bytes */
97 __uint8_t nameval[1]; /* name/value bytes */
98 } namelist; /* grows from bottom of buf */
99 struct xfs_attr_leaf_name_remote {
100 xfs_dablk_t valueblk; /* block number of value bytes */
101 __uint32_t valuelen; /* number of bytes in value */
102 __uint8_t namelen; /* length of name bytes */
103 __uint8_t name[1]; /* name bytes */
104 } valuelist; /* grows from bottom of buf */
105} xfs_attr_leafblock_t;
106typedef struct xfs_attr_leaf_hdr xfs_attr_leaf_hdr_t;
107typedef struct xfs_attr_leaf_map xfs_attr_leaf_map_t;
108typedef struct xfs_attr_leaf_entry xfs_attr_leaf_entry_t;
109typedef struct xfs_attr_leaf_name_local xfs_attr_leaf_name_local_t;
110typedef struct xfs_attr_leaf_name_remote xfs_attr_leaf_name_remote_t;
111
112/*
113 * Flags used in the leaf_entry[i].flags field.
114 * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
115 * on the system call, they are "or"ed together for various operations.
116 */
117#define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
118#define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
119#define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
120#define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
121#define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
122#define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
123#define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
124#define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
125
126/*
127 * Alignment for namelist and valuelist entries (since they are mixed
128 * there can be only one alignment value)
129 */
130#define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
131
132/*
133 * Cast typed pointers for "local" and "remote" name/value structs.
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx) \
136 xfs_attr_leaf_name_remote(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100137static inline xfs_attr_leaf_name_remote_t *
138xfs_attr_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
139{
140 return (xfs_attr_leaf_name_remote_t *) &((char *)
141 (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)];
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx) \
145 xfs_attr_leaf_name_local(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100146static inline xfs_attr_leaf_name_local_t *
147xfs_attr_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
148{
149 return (xfs_attr_leaf_name_local_t *) &((char *)
150 (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)];
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define XFS_ATTR_LEAF_NAME(leafp,idx) xfs_attr_leaf_name(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100154static inline char *xfs_attr_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
155{
156 return (&((char *)
157 (leafp))[INT_GET((leafp)->entries[idx].nameidx, ARCH_CONVERT)]);
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * Calculate total bytes used (including trailing pad for alignment) for
162 * a "local" name/value structure, a "remote" name/value structure, and
163 * a pointer which might be either.
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen) \
166 xfs_attr_leaf_entsize_remote(nlen)
Nathan Scotta844f452005-11-02 14:38:42 +1100167static inline int xfs_attr_leaf_entsize_remote(int nlen)
168{
169 return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
170 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen) \
174 xfs_attr_leaf_entsize_local(nlen,vlen)
Nathan Scotta844f452005-11-02 14:38:42 +1100175static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
176{
177 return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
178 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize) \
182 xfs_attr_leaf_entsize_local_max(bsize)
Nathan Scotta844f452005-11-02 14:38:42 +1100183static inline int xfs_attr_leaf_entsize_local_max(int bsize)
184{
185 return (((bsize) >> 1) + ((bsize) >> 2));
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188
189/*========================================================================
190 * Structure used to pass context around among the routines.
191 *========================================================================*/
192
193typedef struct xfs_attr_list_context {
194 struct xfs_inode *dp; /* inode */
195 struct attrlist_cursor_kern *cursor;/* position in list */
196 struct attrlist *alist; /* output buffer */
197 int count; /* num used entries */
198 int dupcnt; /* count dup hashvals seen */
199 int bufsize;/* total buffer size */
200 int firstu; /* first used byte in buffer */
201 int flags; /* from VOP call */
202 int resynch;/* T/F: resynch with cursor */
203} xfs_attr_list_context_t;
204
205/*
206 * Used to keep a list of "remote value" extents when unlinking an inode.
207 */
208typedef struct xfs_attr_inactive_list {
209 xfs_dablk_t valueblk; /* block number of value bytes */
210 int valuelen; /* number of bytes in value */
211} xfs_attr_inactive_list_t;
212
213
214/*========================================================================
215 * Function prototypes for the kernel.
216 *========================================================================*/
217
218/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100219 * Internal routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100221void xfs_attr_shortform_create(struct xfs_da_args *args);
222void xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223int xfs_attr_shortform_lookup(struct xfs_da_args *args);
224int xfs_attr_shortform_getvalue(struct xfs_da_args *args);
225int xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100226int xfs_attr_shortform_remove(struct xfs_da_args *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227int xfs_attr_shortform_list(struct xfs_attr_list_context *context);
228int xfs_attr_shortform_allfit(struct xfs_dabuf *bp, struct xfs_inode *dp);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100229int xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100233 * Internal routines when attribute fork size == XFS_LBSIZE(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235int xfs_attr_leaf_to_node(struct xfs_da_args *args);
236int xfs_attr_leaf_to_shortform(struct xfs_dabuf *bp,
Nathan Scottd8cc8902005-11-02 10:34:53 +1100237 struct xfs_da_args *args, int forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238int xfs_attr_leaf_clearflag(struct xfs_da_args *args);
239int xfs_attr_leaf_setflag(struct xfs_da_args *args);
240int xfs_attr_leaf_flipflags(xfs_da_args_t *args);
241
242/*
243 * Routines used for growing the Btree.
244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245int xfs_attr_leaf_split(struct xfs_da_state *state,
246 struct xfs_da_state_blk *oldblk,
247 struct xfs_da_state_blk *newblk);
248int xfs_attr_leaf_lookup_int(struct xfs_dabuf *leaf,
249 struct xfs_da_args *args);
250int xfs_attr_leaf_getvalue(struct xfs_dabuf *bp, struct xfs_da_args *args);
251int xfs_attr_leaf_add(struct xfs_dabuf *leaf_buffer,
252 struct xfs_da_args *args);
253int xfs_attr_leaf_remove(struct xfs_dabuf *leaf_buffer,
254 struct xfs_da_args *args);
255int xfs_attr_leaf_list_int(struct xfs_dabuf *bp,
256 struct xfs_attr_list_context *context);
257
258/*
259 * Routines used for shrinking the Btree.
260 */
261int xfs_attr_leaf_toosmall(struct xfs_da_state *state, int *retval);
262void xfs_attr_leaf_unbalance(struct xfs_da_state *state,
263 struct xfs_da_state_blk *drop_blk,
264 struct xfs_da_state_blk *save_blk);
265int xfs_attr_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/*
268 * Utility routines.
269 */
270xfs_dahash_t xfs_attr_leaf_lasthash(struct xfs_dabuf *bp, int *count);
271int xfs_attr_leaf_order(struct xfs_dabuf *leaf1_bp,
272 struct xfs_dabuf *leaf2_bp);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100273int xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 int *local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275int xfs_attr_rolltrans(struct xfs_trans **transp, struct xfs_inode *dp);
276
277#endif /* __XFS_ATTR_LEAF_H__ */