xref: /linux/fs/xfs/libxfs/xfs_da_btree.h (revision 5773f7f8)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2000,2002,2005 Silicon Graphics, Inc.
4  * Copyright (c) 2013 Red Hat, Inc.
5  * All Rights Reserved.
6  */
7 #ifndef __XFS_DA_BTREE_H__
8 #define	__XFS_DA_BTREE_H__
9 
10 struct xfs_inode;
11 struct xfs_trans;
12 
13 /*
14  * Directory/attribute geometry information. There will be one of these for each
15  * data fork type, and it will be passed around via the xfs_da_args. Global
16  * structures will be attached to the xfs_mount.
17  */
18 struct xfs_da_geometry {
19 	unsigned int	blksize;	/* da block size in bytes */
20 	unsigned int	fsbcount;	/* da block size in filesystem blocks */
21 	uint8_t		fsblog;		/* log2 of _filesystem_ block size */
22 	uint8_t		blklog;		/* log2 of da block size */
23 	unsigned int	node_hdr_size;	/* danode header size in bytes */
24 	unsigned int	node_ents;	/* # of entries in a danode */
25 	unsigned int	magicpct;	/* 37% of block size in bytes */
26 	xfs_dablk_t	datablk;	/* blockno of dir data v2 */
27 	unsigned int	leaf_hdr_size;	/* dir2 leaf header size */
28 	unsigned int	leaf_max_ents;	/* # of entries in dir2 leaf */
29 	xfs_dablk_t	leafblk;	/* blockno of leaf data v2 */
30 	unsigned int	free_hdr_size;	/* dir2 free header size */
31 	unsigned int	free_max_bests;	/* # of bests entries in dir2 free */
32 	xfs_dablk_t	freeblk;	/* blockno of free data v2 */
33 	xfs_extnum_t	max_extents;	/* Max. extents in corresponding fork */
34 
35 	xfs_dir2_data_aoff_t data_first_offset;
36 	size_t		data_entry_offset;
37 };
38 
39 /*========================================================================
40  * Btree searching and modification structure definitions.
41  *========================================================================*/
42 
43 /*
44  * Search comparison results
45  */
46 enum xfs_dacmp {
47 	XFS_CMP_DIFFERENT,	/* names are completely different */
48 	XFS_CMP_EXACT,		/* names are exactly the same */
49 	XFS_CMP_CASE		/* names are same but differ in case */
50 };
51 
52 /*
53  * Structure to ease passing around component names.
54  */
55 typedef struct xfs_da_args {
56 	struct xfs_da_geometry *geo;	/* da block geometry */
57 	const uint8_t	*name;		/* string (maybe not NULL terminated) */
58 	const uint8_t	*new_name;	/* new attr name */
59 	void		*value;		/* set of bytes (maybe contain NULLs) */
60 	void		*new_value;	/* new xattr value (may contain NULLs) */
61 	struct xfs_inode *dp;		/* directory inode to manipulate */
62 	struct xfs_trans *trans;	/* current trans (changes over time) */
63 
64 	xfs_ino_t	inumber;	/* input/output inode number */
65 	xfs_ino_t	owner;		/* inode that owns the dir/attr data */
66 
67 	int		valuelen;	/* length of value */
68 	int		new_valuelen;	/* length of new_value */
69 	uint8_t		filetype;	/* filetype of inode for directories */
70 	uint8_t		op_flags;	/* operation flags */
71 	uint8_t		attr_filter;	/* XFS_ATTR_{ROOT,SECURE,INCOMPLETE} */
72 	short		namelen;	/* length of string (maybe no NULL) */
73 	short		new_namelen;	/* length of new attr name */
74 	xfs_dahash_t	hashval;	/* hash value of name */
75 	xfs_extlen_t	total;		/* total blocks needed, for 1st bmap */
76 	int		whichfork;	/* data or attribute fork */
77 	xfs_dablk_t	blkno;		/* blkno of attr leaf of interest */
78 	int		index;		/* index of attr of interest in blk */
79 	xfs_dablk_t	rmtblkno;	/* remote attr value starting blkno */
80 	int		rmtblkcnt;	/* remote attr value block count */
81 	int		rmtvaluelen;	/* remote attr value length in bytes */
82 	xfs_dablk_t	blkno2;		/* blkno of 2nd attr leaf of interest */
83 	int		index2;		/* index of 2nd attr in blk */
84 	xfs_dablk_t	rmtblkno2;	/* remote attr value starting blkno */
85 	int		rmtblkcnt2;	/* remote attr value block count */
86 	int		rmtvaluelen2;	/* remote attr value length in bytes */
87 	enum xfs_dacmp	cmpresult;	/* name compare result for lookups */
88 } xfs_da_args_t;
89 
90 /*
91  * Operation flags:
92  */
93 #define XFS_DA_OP_JUSTCHECK	(1u << 0) /* check for ok with no space */
94 #define XFS_DA_OP_REPLACE	(1u << 1) /* this is an atomic replace op */
95 #define XFS_DA_OP_ADDNAME	(1u << 2) /* this is an add operation */
96 #define XFS_DA_OP_OKNOENT	(1u << 3) /* lookup op, ENOENT ok, else die */
97 #define XFS_DA_OP_CILOOKUP	(1u << 4) /* lookup returns CI name if found */
98 #define XFS_DA_OP_RECOVERY	(1u << 5) /* Log recovery operation */
99 #define XFS_DA_OP_LOGGED	(1u << 6) /* Use intent items to track op */
100 
101 #define XFS_DA_OP_FLAGS \
102 	{ XFS_DA_OP_JUSTCHECK,	"JUSTCHECK" }, \
103 	{ XFS_DA_OP_REPLACE,	"REPLACE" }, \
104 	{ XFS_DA_OP_ADDNAME,	"ADDNAME" }, \
105 	{ XFS_DA_OP_OKNOENT,	"OKNOENT" }, \
106 	{ XFS_DA_OP_CILOOKUP,	"CILOOKUP" }, \
107 	{ XFS_DA_OP_RECOVERY,	"RECOVERY" }, \
108 	{ XFS_DA_OP_LOGGED,	"LOGGED" }
109 
110 /*
111  * Storage for holding state during Btree searches and split/join ops.
112  *
113  * Only need space for 5 intermediate nodes.  With a minimum of 62-way
114  * fanout to the Btree, we can support over 900 million directory blocks,
115  * which is slightly more than enough.
116  */
117 typedef struct xfs_da_state_blk {
118 	struct xfs_buf	*bp;		/* buffer containing block */
119 	xfs_dablk_t	blkno;		/* filesystem blkno of buffer */
120 	xfs_daddr_t	disk_blkno;	/* on-disk blkno (in BBs) of buffer */
121 	int		index;		/* relevant index into block */
122 	xfs_dahash_t	hashval;	/* last hash value in block */
123 	int		magic;		/* blk's magic number, ie: blk type */
124 } xfs_da_state_blk_t;
125 
126 typedef struct xfs_da_state_path {
127 	int			active;		/* number of active levels */
128 	xfs_da_state_blk_t	blk[XFS_DA_NODE_MAXDEPTH];
129 } xfs_da_state_path_t;
130 
131 typedef struct xfs_da_state {
132 	xfs_da_args_t		*args;		/* filename arguments */
133 	struct xfs_mount	*mp;		/* filesystem mount point */
134 	xfs_da_state_path_t	path;		/* search/split paths */
135 	xfs_da_state_path_t	altpath;	/* alternate path for join */
136 	unsigned char		inleaf;		/* insert into 1->lf, 0->splf */
137 	unsigned char		extravalid;	/* T/F: extrablk is in use */
138 	unsigned char		extraafter;	/* T/F: extrablk is after new */
139 	xfs_da_state_blk_t	extrablk;	/* for double-splits on leaves */
140 						/* for dirv2 extrablk is data */
141 } xfs_da_state_t;
142 
143 /*
144  * In-core version of the node header to abstract the differences in the v2 and
145  * v3 disk format of the headers. Callers need to convert to/from disk format as
146  * appropriate.
147  */
148 struct xfs_da3_icnode_hdr {
149 	uint32_t		forw;
150 	uint32_t		back;
151 	uint16_t		magic;
152 	uint16_t		count;
153 	uint16_t		level;
154 
155 	/*
156 	 * Pointer to the on-disk format entries, which are behind the
157 	 * variable size (v4 vs v5) header in the on-disk block.
158 	 */
159 	struct xfs_da_node_entry *btree;
160 };
161 
162 /*
163  * Utility macros to aid in logging changed structure fields.
164  */
165 #define XFS_DA_LOGOFF(BASE, ADDR)	((char *)(ADDR) - (char *)(BASE))
166 #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE)	\
167 		(uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
168 		(uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
169 
170 /*========================================================================
171  * Function prototypes.
172  *========================================================================*/
173 
174 /*
175  * Routines used for growing the Btree.
176  */
177 int	xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno,
178 			    int level, struct xfs_buf **bpp, int whichfork);
179 int	xfs_da3_split(xfs_da_state_t *state);
180 
181 /*
182  * Routines used for shrinking the Btree.
183  */
184 int	xfs_da3_join(xfs_da_state_t *state);
185 void	xfs_da3_fixhashpath(struct xfs_da_state *state,
186 			    struct xfs_da_state_path *path_to_to_fix);
187 
188 /*
189  * Routines used for finding things in the Btree.
190  */
191 int	xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result);
192 int	xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
193 					 int forward, int release, int *result);
194 /*
195  * Utility routines.
196  */
197 int	xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
198 				       xfs_da_state_blk_t *new_blk);
199 int	xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
200 			xfs_dablk_t bno, struct xfs_buf **bpp, int whichfork);
201 int	xfs_da3_node_read_mapped(struct xfs_trans *tp, struct xfs_inode *dp,
202 			xfs_daddr_t mappedbno, struct xfs_buf **bpp,
203 			int whichfork);
204 
205 /*
206  * Utility routines.
207  */
208 
209 #define XFS_DABUF_MAP_HOLE_OK	(1u << 0)
210 
211 int	xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
212 int	xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
213 			      int count);
214 int	xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
215 		xfs_dablk_t bno, struct xfs_buf **bp, int whichfork);
216 int	xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
217 		xfs_dablk_t bno, unsigned int flags, struct xfs_buf **bpp,
218 		int whichfork, const struct xfs_buf_ops *ops);
219 int	xfs_da_reada_buf(struct xfs_inode *dp, xfs_dablk_t bno,
220 		unsigned int flags, int whichfork,
221 		const struct xfs_buf_ops *ops);
222 int	xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
223 					  struct xfs_buf *dead_buf);
224 void	xfs_da_buf_copy(struct xfs_buf *dst, struct xfs_buf *src,
225 			size_t size);
226 
227 uint xfs_da_hashname(const uint8_t *name_string, int name_length);
228 enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
229 				const unsigned char *name, int len);
230 
231 
232 struct xfs_da_state *xfs_da_state_alloc(struct xfs_da_args *args);
233 void xfs_da_state_free(xfs_da_state_t *state);
234 void xfs_da_state_reset(struct xfs_da_state *state, struct xfs_da_args *args);
235 
236 void	xfs_da3_node_hdr_from_disk(struct xfs_mount *mp,
237 		struct xfs_da3_icnode_hdr *to, struct xfs_da_intnode *from);
238 void	xfs_da3_node_hdr_to_disk(struct xfs_mount *mp,
239 		struct xfs_da_intnode *to, struct xfs_da3_icnode_hdr *from);
240 xfs_failaddr_t xfs_da3_header_check(struct xfs_buf *bp, xfs_ino_t owner);
241 xfs_failaddr_t xfs_da3_node_header_check(struct xfs_buf *bp, xfs_ino_t owner);
242 
243 extern struct kmem_cache	*xfs_da_state_cache;
244 
245 #endif	/* __XFS_DA_BTREE_H__ */
246