xfs_attr_leaf.h revision 180208
150276Speter/*
2176187Srafan * Copyright (c) 2000,2002-2003,2005 Silicon Graphics, Inc.
350276Speter * All Rights Reserved.
450276Speter *
550276Speter * This program is free software; you can redistribute it and/or
650276Speter * modify it under the terms of the GNU General Public License as
750276Speter * published by the Free Software Foundation.
850276Speter *
950276Speter * This program is distributed in the hope that it would be useful,
1050276Speter * but WITHOUT ANY WARRANTY; without even the implied warranty of
1150276Speter * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1250276Speter * GNU General Public License for more details.
1350276Speter *
1450276Speter * You should have received a copy of the GNU General Public License
1550276Speter * along with this program; if not, write the Free Software Foundation,
1650276Speter * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1750276Speter */
1850276Speter#ifndef __XFS_ATTR_LEAF_H__
1950276Speter#define	__XFS_ATTR_LEAF_H__
2050276Speter
2150276Speter/*
2250276Speter * Attribute storage layout, internal structure, access macros, etc.
2350276Speter *
2450276Speter * Attribute lists are structured around Btrees where all the data
2550276Speter * elements are in the leaf nodes.  Attribute names are hashed into an int,
2650276Speter * then that int is used as the index into the Btree.  Since the hashval
2750276Speter * of an attribute name may not be unique, we may have duplicate keys.  The
2850276Speter * internal links in the Btree are logical block offsets into the file.
2950276Speter */
3050276Speter
3150276Speterstruct attrlist;
32166124Srafanstruct attrlist_cursor_kern;
3350276Speterstruct attrnames;
3450276Speterstruct xfs_dabuf;
3550276Speterstruct xfs_da_args;
3650276Speterstruct xfs_da_state;
3750276Speterstruct xfs_da_state_blk;
3850276Speterstruct xfs_inode;
3950276Speterstruct xfs_trans;
4050276Speter
4150276Speter/*========================================================================
4250276Speter * Attribute structure when equal to XFS_LBSIZE(mp) bytes.
4350276Speter *========================================================================*/
44184989Srafan
4550276Speter/*
4650276Speter * This is the structure of the leaf nodes in the Btree.
4750276Speter *
48174993Srafan * Struct leaf_entry's are packed from the top.  Name/values grow from the
49178866Srafan * bottom but are not packed.  The freemap contains run-length-encoded entries
50174993Srafan * for the free bytes after the leaf_entry's, but only the N largest such,
51174993Srafan * smaller runs are dropped.  When the freemap doesn't show enough space
52174993Srafan * for an allocation, we compact the name/value area and try again.  If we
53178866Srafan * still don't have enough space, then we have to split the block.  The
54174993Srafan * name/value structs (both local and remote versions) must be 32bit aligned.
55174993Srafan *
56178866Srafan * Since we have duplicate hash keys, for each key that matches, compare
5776726Speter * the actual name string.  The root and intermediate node search always
5876726Speter * takes the first-in-the-block key match found, so we should only have
59174993Srafan * to work "forw"ard.  If none matches, continue with the "forw"ard leaf
6050276Speter * nodes until the hash key changes or the attribute name is found.
61176187Srafan *
62176187Srafan * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
63176187Srafan * the leaf_entry.  The namespaces are independent only because we also look
64176187Srafan * at the namespace bit when we are looking for a matching attribute name.
65176187Srafan *
66176187Srafan * We also store an "incomplete" bit in the leaf_entry.  It shows that an
67176187Srafan * attribute is in the middle of being created and should not be shown to
68176187Srafan * the user if we crash during the time that the bit is set.  We clear the
69176187Srafan * bit when we have finished setting up the attribute.  We do this because
70176187Srafan * we cannot create some large attributes inside a single transaction, and we
71176187Srafan * need some indication that we weren't finished if we crash in the middle.
72176187Srafan */
73176187Srafan#define XFS_ATTR_LEAF_MAPSIZE	3	/* how many freespace slots */
74176187Srafan
75176187Srafantypedef struct xfs_attr_leaf_map {	/* RLE map of free bytes */
76176187Srafan	__be16	base;			  /* base of free region */
77176187Srafan	__be16	size;			  /* length of free region */
78176187Srafan} xfs_attr_leaf_map_t;
79184989Srafan
80184989Srafantypedef struct xfs_attr_leaf_hdr {	/* constant-structure header block */
81184989Srafan	xfs_da_blkinfo_t info;		/* block type, links, etc. */
82184989Srafan	__be16	count;			/* count of active leaf_entry's */
83184989Srafan	__be16	usedbytes;		/* num bytes of names/values stored */
84184989Srafan	__be16	firstused;		/* first used byte in name area */
85184989Srafan	__u8	holes;			/* != 0 if blk needs compaction */
86166124Srafan	__u8	pad1;
87166124Srafan	xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
88166124Srafan					/* N largest free regions */
89166124Srafan} xfs_attr_leaf_hdr_t;
90166124Srafan
91166124Srafantypedef struct xfs_attr_leaf_entry {	/* sorted on key, not name */
92166124Srafan	__be32	hashval;		/* hash value of name */
93166124Srafan 	__be16	nameidx;		/* index into buffer of name/value */
94166124Srafan	__u8	flags;			/* LOCAL/ROOT/SECURE/INCOMPLETE flag */
95166124Srafan	__u8	pad2;			/* unused pad byte */
96178866Srafan} xfs_attr_leaf_entry_t;
97166124Srafan
98166124Srafantypedef struct xfs_attr_leaf_name_local {
99166124Srafan	__be16	valuelen;		/* number of bytes in value */
100166124Srafan	__u8	namelen;		/* length of name bytes */
101178866Srafan	__u8	nameval[1];		/* name/value bytes */
102178866Srafan} xfs_attr_leaf_name_local_t;
103166124Srafan
104166124Srafantypedef struct xfs_attr_leaf_name_remote {
105166124Srafan	__be32	valueblk;		/* block number of value bytes */
106178866Srafan	__be32	valuelen;		/* number of bytes in value */
107166124Srafan	__u8	namelen;		/* length of name bytes */
108178866Srafan	__u8	name[1];		/* name bytes */
109178866Srafan} xfs_attr_leaf_name_remote_t;
110166124Srafan
111166124Srafantypedef struct xfs_attr_leafblock {
112166124Srafan	xfs_attr_leaf_hdr_t	hdr;	/* constant-structure header block */
113166124Srafan	xfs_attr_leaf_entry_t	entries[1];	/* sorted on key, not name */
114166124Srafan	xfs_attr_leaf_name_local_t namelist;	/* grows from bottom of buf */
115166124Srafan	xfs_attr_leaf_name_remote_t valuelist;	/* grows from bottom of buf */
116166124Srafan} xfs_attr_leafblock_t;
117166124Srafan
118166124Srafan/*
119178866Srafan * Flags used in the leaf_entry[i].flags field.
12050276Speter * NOTE: the INCOMPLETE bit must not collide with the flags bits specified
121178866Srafan * on the system call, they are "or"ed together for various operations.
12266963Speter */
12350276Speter#define	XFS_ATTR_LOCAL_BIT	0	/* attr is stored locally */
12462449Speter#define	XFS_ATTR_ROOT_BIT	1	/* limit access to trusted attrs */
12562449Speter#define	XFS_ATTR_SECURE_BIT	2	/* limit access to secure attrs */
12650276Speter#define	XFS_ATTR_INCOMPLETE_BIT	7	/* attr in middle of create/delete */
12750276Speter#define XFS_ATTR_LOCAL		(1 << XFS_ATTR_LOCAL_BIT)
128166124Srafan#define XFS_ATTR_ROOT		(1 << XFS_ATTR_ROOT_BIT)
129178866Srafan#define XFS_ATTR_SECURE		(1 << XFS_ATTR_SECURE_BIT)
13050276Speter#define XFS_ATTR_INCOMPLETE	(1 << XFS_ATTR_INCOMPLETE_BIT)
13162449Speter
132178866Srafan/*
133184989Srafan * Alignment for namelist and valuelist entries (since they are mixed
13450276Speter * there can be only one alignment value)
13562449Speter */
13662449Speter#define	XFS_ATTR_LEAF_NAME_ALIGN	((uint)sizeof(xfs_dablk_t))
13762449Speter
13862449Speter/*
13962449Speter * Cast typed pointers for "local" and "remote" name/value structs.
14050276Speter */
14150276Speter#define XFS_ATTR_LEAF_NAME_REMOTE(leafp,idx)	\
142174993Srafan	xfs_attr_leaf_name_remote(leafp,idx)
143178866Srafanstatic inline xfs_attr_leaf_name_remote_t *
144174993Srafanxfs_attr_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
145174993Srafan{
14650276Speter	return (xfs_attr_leaf_name_remote_t *)
14762449Speter		&((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
14850276Speter}
14950276Speter
150166124Srafan#define XFS_ATTR_LEAF_NAME_LOCAL(leafp,idx)	\
151178866Srafan	xfs_attr_leaf_name_local(leafp,idx)
15250276Speterstatic inline xfs_attr_leaf_name_local_t *
15362449Speterxfs_attr_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
154166124Srafan{
155166124Srafan	return (xfs_attr_leaf_name_local_t *)
15650276Speter		&((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
157166124Srafan}
15862449Speter
15962449Speter#define XFS_ATTR_LEAF_NAME(leafp,idx)		\
16050276Speter	xfs_attr_leaf_name(leafp,idx)
16150276Speterstatic inline char *xfs_attr_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
16262449Speter{
16362449Speter	return &((char *)leafp)[be16_to_cpu(leafp->entries[idx].nameidx)];
16450276Speter}
16550276Speter
166166124Srafan/*
167166124Srafan * Calculate total bytes used (including trailing pad for alignment) for
168166124Srafan * a "local" name/value structure, a "remote" name/value structure, and
169178866Srafan * a pointer which might be either.
170166124Srafan */
171166124Srafan#define XFS_ATTR_LEAF_ENTSIZE_REMOTE(nlen)	\
172178866Srafan	xfs_attr_leaf_entsize_remote(nlen)
173166124Srafanstatic inline int xfs_attr_leaf_entsize_remote(int nlen)
174166124Srafan{
175166124Srafan	return ((uint)sizeof(xfs_attr_leaf_name_remote_t) - 1 + (nlen) + \
176166124Srafan		XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
177166124Srafan}
178178866Srafan
179166124Srafan#define XFS_ATTR_LEAF_ENTSIZE_LOCAL(nlen,vlen)	\
180166124Srafan	xfs_attr_leaf_entsize_local(nlen,vlen)
181166124Srafanstatic inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
182178866Srafan{
183178866Srafan	return ((uint)sizeof(xfs_attr_leaf_name_local_t) - 1 + (nlen) + (vlen) +
184166124Srafan		XFS_ATTR_LEAF_NAME_ALIGN - 1) & ~(XFS_ATTR_LEAF_NAME_ALIGN - 1);
185166124Srafan}
186166124Srafan
187166124Srafan#define XFS_ATTR_LEAF_ENTSIZE_LOCAL_MAX(bsize)	\
188178866Srafan	xfs_attr_leaf_entsize_local_max(bsize)
189178866Srafanstatic inline int xfs_attr_leaf_entsize_local_max(int bsize)
19062449Speter{
19162449Speter	return (((bsize) >> 1) + ((bsize) >> 2));
19262449Speter}
19350276Speter
194166124Srafan
195178866Srafan/*========================================================================
196178866Srafan * Structure used to pass context around among the routines.
197178866Srafan *========================================================================*/
198166124Srafan
199166124Srafantypedef struct xfs_attr_list_context {
200178866Srafan	struct xfs_inode		*dp;	/* inode */
201166124Srafan	struct attrlist_cursor_kern	*cursor;/* position in list */
202178866Srafan	struct attrlist			*alist;	/* output buffer */
203166124Srafan	int				count;	/* num used entries */
204166124Srafan	int				dupcnt;	/* count dup hashvals seen */
205166124Srafan	int				bufsize;/* total buffer size */
206166124Srafan	int				firstu;	/* first used byte in buffer */
207166124Srafan	int				flags;	/* from VOP call */
20862449Speter	int				resynch;/* T/F: resynch with cursor */
209178866Srafan} xfs_attr_list_context_t;
21097049Speter
21162449Speter/*
21250276Speter * Used to keep a list of "remote value" extents when unlinking an inode.
21350276Speter */
21462449Spetertypedef struct xfs_attr_inactive_list {
21562449Speter	xfs_dablk_t	valueblk;	/* block number of value bytes */
21662449Speter	int		valuelen;	/* number of bytes in value */
21762449Speter} xfs_attr_inactive_list_t;
21862449Speter
21962449Speter
22062449Speter/*========================================================================
22162449Speter * Function prototypes for the kernel.
22262449Speter *========================================================================*/
22362449Speter
22462449Speter/*
22550276Speter * Internal routines when attribute fork size < XFS_LITINO(mp).
22650276Speter */
22762449Spetervoid	xfs_attr_shortform_create(struct xfs_da_args *args);
228178866Srafanvoid	xfs_attr_shortform_add(struct xfs_da_args *args, int forkoff);
22966963Speterint	xfs_attr_shortform_lookup(struct xfs_da_args *args);
23062449Speterint	xfs_attr_shortform_to_leaf(struct xfs_da_args *args);
23166963Speterint	xfs_attr_shortform_remove(struct xfs_da_args *args);
23250276Speterint	xfs_attr_shortform_list(struct xfs_attr_list_context *context);
233178866Srafanint	xfs_attr_shortform_allfit(struct xfs_dabuf *bp, struct xfs_inode *dp);
234178866Srafanint	xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes);
23562449Speter
23662449Speter
23762449Speter/*
238184989Srafan * Internal routines when attribute fork size == XFS_LBSIZE(mp).
23950276Speter */
240174993Srafanint	xfs_attr_leaf_to_node(struct xfs_da_args *args);
241178866Srafanint	xfs_attr_leaf_to_shortform(struct xfs_dabuf *bp,
242174993Srafan				   struct xfs_da_args *args, int forkoff);
243174993Srafanint	xfs_attr_leaf_clearflag(struct xfs_da_args *args);
24450276Speterint	xfs_attr_leaf_setflag(struct xfs_da_args *args);
24562449Speterint	xfs_attr_leaf_flipflags(xfs_da_args_t *args);
24650276Speter
24750276Speter/*
248166124Srafan * Routines used for growing the Btree.
249178866Srafan */
25050276Speterint	xfs_attr_leaf_split(struct xfs_da_state *state,
251178866Srafan				   struct xfs_da_state_blk *oldblk,
25262449Speter				   struct xfs_da_state_blk *newblk);
25362449Speterint	xfs_attr_leaf_lookup_int(struct xfs_dabuf *leaf,
25450276Speter					struct xfs_da_args *args);
25550276Speterint	xfs_attr_leaf_getvalue(struct xfs_dabuf *bp, struct xfs_da_args *args);
256178866Srafanint	xfs_attr_leaf_add(struct xfs_dabuf *leaf_buffer,
25750276Speter				 struct xfs_da_args *args);
258184989Srafanint	xfs_attr_leaf_remove(struct xfs_dabuf *leaf_buffer,
259184989Srafan				    struct xfs_da_args *args);
260184989Srafanint	xfs_attr_leaf_list_int(struct xfs_dabuf *bp,
261184989Srafan				      struct xfs_attr_list_context *context);
262184989Srafan
263184989Srafan/*
264184989Srafan * Routines used for shrinking the Btree.
26550276Speter */
266184989Srafanint	xfs_attr_leaf_toosmall(struct xfs_da_state *state, int *retval);
267184989Srafanvoid	xfs_attr_leaf_unbalance(struct xfs_da_state *state,
268184989Srafan				       struct xfs_da_state_blk *drop_blk,
269184989Srafan				       struct xfs_da_state_blk *save_blk);
270184989Srafanint	xfs_attr_root_inactive(struct xfs_trans **trans, struct xfs_inode *dp);
271184989Srafan
272184989Srafan/*
273184989Srafan * Utility routines.
274184989Srafan */
275184989Srafanxfs_dahash_t	xfs_attr_leaf_lasthash(struct xfs_dabuf *bp, int *count);
276184989Srafanint	xfs_attr_leaf_order(struct xfs_dabuf *leaf1_bp,
277184989Srafan				   struct xfs_dabuf *leaf2_bp);
278184989Srafanint	xfs_attr_leaf_newentsize(int namelen, int valuelen, int blocksize,
279184989Srafan					int *local);
280184989Srafanint	xfs_attr_rolltrans(struct xfs_trans **transp, struct xfs_inode *dp);
281184989Srafan
282184989Srafan#endif	/* __XFS_ATTR_LEAF_H__ */
283184989Srafan