blob: 83e9af417ca21280492fcfa37a7fd93494429fbb [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;
Christoph Hellwigad9b4632008-06-23 13:23:48 +100033struct xfs_attr_list_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct 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 *
Nathan Scott3762ec62006-01-12 10:29:53 +110066 * We also store an "incomplete" bit in the leaf_entry. It shows that an
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * 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
Nathan Scott3762ec62006-01-12 10:29:53 +110075typedef struct xfs_attr_leaf_map { /* RLE map of free bytes */
Nathan Scott918ae422006-03-17 17:28:54 +110076 __be16 base; /* base of free region */
77 __be16 size; /* length of free region */
Nathan Scott3762ec62006-01-12 10:29:53 +110078} xfs_attr_leaf_map_t;
79
80typedef struct xfs_attr_leaf_hdr { /* constant-structure header block */
81 xfs_da_blkinfo_t info; /* block type, links, etc. */
Nathan Scott918ae422006-03-17 17:28:54 +110082 __be16 count; /* count of active leaf_entry's */
83 __be16 usedbytes; /* num bytes of names/values stored */
84 __be16 firstused; /* first used byte in name area */
85 __u8 holes; /* != 0 if blk needs compaction */
86 __u8 pad1;
Nathan Scott3762ec62006-01-12 10:29:53 +110087 xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
88 /* N largest free regions */
89} xfs_attr_leaf_hdr_t;
90
91typedef struct xfs_attr_leaf_entry { /* sorted on key, not name */
Nathan Scott6b19f2d2006-03-17 17:29:02 +110092 __be32 hashval; /* hash value of name */
93 __be16 nameidx; /* index into buffer of name/value */
94 __u8 flags; /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
95 __u8 pad2; /* unused pad byte */
Nathan Scott3762ec62006-01-12 10:29:53 +110096} xfs_attr_leaf_entry_t;
97
98typedef struct xfs_attr_leaf_name_local {
Nathan Scott053b5752006-03-17 17:29:09 +110099 __be16 valuelen; /* number of bytes in value */
100 __u8 namelen; /* length of name bytes */
101 __u8 nameval[1]; /* name/value bytes */
Nathan Scott3762ec62006-01-12 10:29:53 +1100102} xfs_attr_leaf_name_local_t;
103
104typedef struct xfs_attr_leaf_name_remote {
Nathan Scottc0f054e2006-03-17 17:29:18 +1100105 __be32 valueblk; /* block number of value bytes */
106 __be32 valuelen; /* number of bytes in value */
107 __u8 namelen; /* length of name bytes */
108 __u8 name[1]; /* name bytes */
Nathan Scott3762ec62006-01-12 10:29:53 +1100109} xfs_attr_leaf_name_remote_t;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111typedef struct xfs_attr_leafblock {
Nathan Scott3762ec62006-01-12 10:29:53 +1100112 xfs_attr_leaf_hdr_t hdr; /* constant-structure header block */
113 xfs_attr_leaf_entry_t entries[1]; /* sorted on key, not name */
114 xfs_attr_leaf_name_local_t namelist; /* grows from bottom of buf */
115 xfs_attr_leaf_name_remote_t valuelist; /* grows from bottom of buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116} xfs_attr_leafblock_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/*
119 * Flags used in the leaf_entry[i].flags field.
120 * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
121 * on the system call, they are "or"ed together for various operations.
122 */
123#define XFS_ATTR_LOCAL_BIT 0 /* attr is stored locally */
124#define XFS_ATTR_ROOT_BIT 1 /* limit access to trusted attrs */
125#define XFS_ATTR_SECURE_BIT 2 /* limit access to secure attrs */
126#define XFS_ATTR_INCOMPLETE_BIT 7 /* attr in middle of create/delete */
127#define XFS_ATTR_LOCAL (1 << XFS_ATTR_LOCAL_BIT)
128#define XFS_ATTR_ROOT (1 << XFS_ATTR_ROOT_BIT)
129#define XFS_ATTR_SECURE (1 << XFS_ATTR_SECURE_BIT)
130#define XFS_ATTR_INCOMPLETE (1 << XFS_ATTR_INCOMPLETE_BIT)
131
132/*
Tim Shimmin726801b2006-09-28 11:01:37 +1000133 * Conversion macros for converting namespace bits from argument flags
134 * to ondisk flags.
135 */
136#define XFS_ATTR_NSP_ARGS_MASK (ATTR_ROOT | ATTR_SECURE)
137#define XFS_ATTR_NSP_ONDISK_MASK (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
138#define XFS_ATTR_NSP_ONDISK(flags) ((flags) & XFS_ATTR_NSP_ONDISK_MASK)
139#define XFS_ATTR_NSP_ARGS(flags) ((flags) & XFS_ATTR_NSP_ARGS_MASK)
140#define XFS_ATTR_NSP_ARGS_TO_ONDISK(x) (((x) & ATTR_ROOT ? XFS_ATTR_ROOT : 0) |\
141 ((x) & ATTR_SECURE ? XFS_ATTR_SECURE : 0))
142#define XFS_ATTR_NSP_ONDISK_TO_ARGS(x) (((x) & XFS_ATTR_ROOT ? ATTR_ROOT : 0) |\
143 ((x) & XFS_ATTR_SECURE ? ATTR_SECURE : 0))
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * Alignment for namelist and valuelist entries (since they are mixed
147 * there can be only one alignment value)
148 */
149#define XFS_ATTR_LEAF_NAME_ALIGN ((uint)sizeof(xfs_dablk_t))
150
151/*
152 * Cast typed pointers for "local" and "remote" name/value structs.
153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx) \
155 xfs_attr_leaf_name_remote(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100156static inline xfs_attr_leaf_name_remote_t *
157xfs_attr_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
158{
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100159 return (xfs_attr_leaf_name_remote_t *)
160 &((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
Nathan Scotta844f452005-11-02 14:38:42 +1100161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx) \
164 xfs_attr_leaf_name_local(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100165static inline xfs_attr_leaf_name_local_t *
166xfs_attr_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
167{
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100168 return (xfs_attr_leaf_name_local_t *)
169 &((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
Nathan Scotta844f452005-11-02 14:38:42 +1100170}
171
Nathan Scott3762ec62006-01-12 10:29:53 +1100172#define XFS_ATTR_LEAF_NAME(leafp,idx) \
173 xfs_attr_leaf_name(leafp,idx)
Nathan Scotta844f452005-11-02 14:38:42 +1100174static inline char *xfs_attr_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
175{
Nathan Scott6b19f2d2006-03-17 17:29:02 +1100176 return &((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
Nathan Scotta844f452005-11-02 14:38:42 +1100177}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/*
180 * Calculate total bytes used (including trailing pad for alignment) for
181 * a "local" name/value structure, a "remote" name/value structure, and
182 * a pointer which might be either.
183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen) \
185 xfs_attr_leaf_entsize_remote(nlen)
Nathan Scotta844f452005-11-02 14:38:42 +1100186static inline int xfs_attr_leaf_entsize_remote(int nlen)
187{
188 return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
189 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
190}
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen) \
193 xfs_attr_leaf_entsize_local(nlen,vlen)
Nathan Scotta844f452005-11-02 14:38:42 +1100194static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
195{
196 return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
197 XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize) \
201 xfs_attr_leaf_entsize_local_max(bsize)
Nathan Scotta844f452005-11-02 14:38:42 +1100202static inline int xfs_attr_leaf_entsize_local_max(int bsize)
203{
204 return (((bsize) >> 1) + ((bsize) >> 2));
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
208 * Used to keep a list of "remote value" extents when unlinking an inode.
209 */
210typedef struct xfs_attr_inactive_list {
211 xfs_dablk_t valueblk; /* block number of value bytes */
212 int valuelen; /* number of bytes in value */
213} xfs_attr_inactive_list_t;
214
215
216/*========================================================================
217 * Function prototypes for the kernel.
218 *========================================================================*/
219
220/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100221 * Internal routines when attribute fork size < XFS_LITINO(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
Nathan Scottd8cc8902005-11-02 10:34:53 +1100223void xfs_attr_shortform_create(struct xfs_da_args *args);
224void xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225int xfs_attr_shortform_lookup(struct xfs_da_args *args);
226int xfs_attr_shortform_getvalue(struct xfs_da_args *args);
227int xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100228int xfs_attr_shortform_remove(struct xfs_da_args *args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229int xfs_attr_shortform_list(struct xfs_attr_list_context *context);
230int xfs_attr_shortform_allfit(struct xfs_dabuf *bp, struct xfs_inode *dp);
Nathan Scottd8cc8902005-11-02 10:34:53 +1100231int xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234/*
Nathan Scottd8cc8902005-11-02 10:34:53 +1100235 * Internal routines when attribute fork size == XFS_LBSIZE(mp).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 */
237int xfs_attr_leaf_to_node(struct xfs_da_args *args);
238int xfs_attr_leaf_to_shortform(struct xfs_dabuf *bp,
Nathan Scottd8cc8902005-11-02 10:34:53 +1100239 struct xfs_da_args *args, int forkoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240int xfs_attr_leaf_clearflag(struct xfs_da_args *args);
241int xfs_attr_leaf_setflag(struct xfs_da_args *args);
242int xfs_attr_leaf_flipflags(xfs_da_args_t *args);
243
244/*
245 * Routines used for growing the Btree.
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247int xfs_attr_leaf_split(struct xfs_da_state *state,
248 struct xfs_da_state_blk *oldblk,
249 struct xfs_da_state_blk *newblk);
250int xfs_attr_leaf_lookup_int(struct xfs_dabuf *leaf,
251 struct xfs_da_args *args);
252int xfs_attr_leaf_getvalue(struct xfs_dabuf *bp, struct xfs_da_args *args);
253int xfs_attr_leaf_add(struct xfs_dabuf *leaf_buffer,
254 struct xfs_da_args *args);
255int xfs_attr_leaf_remove(struct xfs_dabuf *leaf_buffer,
256 struct xfs_da_args *args);
257int xfs_attr_leaf_list_int(struct xfs_dabuf *bp,
258 struct xfs_attr_list_context *context);
259
260/*
261 * Routines used for shrinking the Btree.
262 */
263int xfs_attr_leaf_toosmall(struct xfs_da_state *state, int *retval);
264void xfs_attr_leaf_unbalance(struct xfs_da_state *state,
265 struct xfs_da_state_blk *drop_blk,
266 struct xfs_da_state_blk *save_blk);
267int xfs_attr_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/*
270 * Utility routines.
271 */
272xfs_dahash_t xfs_attr_leaf_lasthash(struct xfs_dabuf *bp, int *count);
273int xfs_attr_leaf_order(struct xfs_dabuf *leaf1_bp,
274 struct xfs_dabuf *leaf2_bp);
Nathan Scottaa82daa2005-11-02 10:33:33 +1100275int xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int *local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#endif /* __XFS_ATTR_LEAF_H__ */