xref: /linux/fs/xfs/libxfs/xfs_da_btree.h (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
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 	int		namelen;	/* length of string (maybe no NULL) */
59 	uint8_t		filetype;	/* filetype of inode for directories */
60 	void		*value;		/* set of bytes (maybe contain NULLs) */
61 	int		valuelen;	/* length of value */
62 	unsigned int	attr_filter;	/* XFS_ATTR_{ROOT,SECURE,INCOMPLETE} */
63 	unsigned int	attr_flags;	/* XATTR_{CREATE,REPLACE} */
64 	xfs_dahash_t	hashval;	/* hash value of name */
65 	xfs_ino_t	inumber;	/* input/output inode number */
66 	struct xfs_inode *dp;		/* directory inode to manipulate */
67 	struct xfs_trans *trans;	/* current trans (changes over time) */
68 	xfs_extlen_t	total;		/* total blocks needed, for 1st bmap */
69 	int		whichfork;	/* data or attribute fork */
70 	xfs_dablk_t	blkno;		/* blkno of attr leaf of interest */
71 	int		index;		/* index of attr of interest in blk */
72 	xfs_dablk_t	rmtblkno;	/* remote attr value starting blkno */
73 	int		rmtblkcnt;	/* remote attr value block count */
74 	int		rmtvaluelen;	/* remote attr value length in bytes */
75 	xfs_dablk_t	blkno2;		/* blkno of 2nd attr leaf of interest */
76 	int		index2;		/* index of 2nd attr in blk */
77 	xfs_dablk_t	rmtblkno2;	/* remote attr value starting blkno */
78 	int		rmtblkcnt2;	/* remote attr value block count */
79 	int		rmtvaluelen2;	/* remote attr value length in bytes */
80 	uint32_t	op_flags;	/* operation flags */
81 	enum xfs_dacmp	cmpresult;	/* name compare result for lookups */
82 } xfs_da_args_t;
83 
84 /*
85  * Operation flags:
86  */
87 #define XFS_DA_OP_JUSTCHECK	(1u << 0) /* check for ok with no space */
88 #define XFS_DA_OP_REPLACE	(1u << 1) /* this is an atomic replace op */
89 #define XFS_DA_OP_ADDNAME	(1u << 2) /* this is an add operation */
90 #define XFS_DA_OP_OKNOENT	(1u << 3) /* lookup op, ENOENT ok, else die */
91 #define XFS_DA_OP_CILOOKUP	(1u << 4) /* lookup returns CI name if found */
92 #define XFS_DA_OP_NOTIME	(1u << 5) /* don't update inode timestamps */
93 #define XFS_DA_OP_REMOVE	(1u << 6) /* this is a remove operation */
94 #define XFS_DA_OP_RECOVERY	(1u << 7) /* Log recovery operation */
95 
96 #define XFS_DA_OP_FLAGS \
97 	{ XFS_DA_OP_JUSTCHECK,	"JUSTCHECK" }, \
98 	{ XFS_DA_OP_REPLACE,	"REPLACE" }, \
99 	{ XFS_DA_OP_ADDNAME,	"ADDNAME" }, \
100 	{ XFS_DA_OP_OKNOENT,	"OKNOENT" }, \
101 	{ XFS_DA_OP_CILOOKUP,	"CILOOKUP" }, \
102 	{ XFS_DA_OP_NOTIME,	"NOTIME" }, \
103 	{ XFS_DA_OP_REMOVE,	"REMOVE" }, \
104 	{ XFS_DA_OP_RECOVERY,	"RECOVERY" }
105 
106 /*
107  * Storage for holding state during Btree searches and split/join ops.
108  *
109  * Only need space for 5 intermediate nodes.  With a minimum of 62-way
110  * fanout to the Btree, we can support over 900 million directory blocks,
111  * which is slightly more than enough.
112  */
113 typedef struct xfs_da_state_blk {
114 	struct xfs_buf	*bp;		/* buffer containing block */
115 	xfs_dablk_t	blkno;		/* filesystem blkno of buffer */
116 	xfs_daddr_t	disk_blkno;	/* on-disk blkno (in BBs) of buffer */
117 	int		index;		/* relevant index into block */
118 	xfs_dahash_t	hashval;	/* last hash value in block */
119 	int		magic;		/* blk's magic number, ie: blk type */
120 } xfs_da_state_blk_t;
121 
122 typedef struct xfs_da_state_path {
123 	int			active;		/* number of active levels */
124 	xfs_da_state_blk_t	blk[XFS_DA_NODE_MAXDEPTH];
125 } xfs_da_state_path_t;
126 
127 typedef struct xfs_da_state {
128 	xfs_da_args_t		*args;		/* filename arguments */
129 	struct xfs_mount	*mp;		/* filesystem mount point */
130 	xfs_da_state_path_t	path;		/* search/split paths */
131 	xfs_da_state_path_t	altpath;	/* alternate path for join */
132 	unsigned char		inleaf;		/* insert into 1->lf, 0->splf */
133 	unsigned char		extravalid;	/* T/F: extrablk is in use */
134 	unsigned char		extraafter;	/* T/F: extrablk is after new */
135 	xfs_da_state_blk_t	extrablk;	/* for double-splits on leaves */
136 						/* for dirv2 extrablk is data */
137 } xfs_da_state_t;
138 
139 /*
140  * In-core version of the node header to abstract the differences in the v2 and
141  * v3 disk format of the headers. Callers need to convert to/from disk format as
142  * appropriate.
143  */
144 struct xfs_da3_icnode_hdr {
145 	uint32_t		forw;
146 	uint32_t		back;
147 	uint16_t		magic;
148 	uint16_t		count;
149 	uint16_t		level;
150 
151 	/*
152 	 * Pointer to the on-disk format entries, which are behind the
153 	 * variable size (v4 vs v5) header in the on-disk block.
154 	 */
155 	struct xfs_da_node_entry *btree;
156 };
157 
158 /*
159  * Utility macros to aid in logging changed structure fields.
160  */
161 #define XFS_DA_LOGOFF(BASE, ADDR)	((char *)(ADDR) - (char *)(BASE))
162 #define XFS_DA_LOGRANGE(BASE, ADDR, SIZE)	\
163 		(uint)(XFS_DA_LOGOFF(BASE, ADDR)), \
164 		(uint)(XFS_DA_LOGOFF(BASE, ADDR)+(SIZE)-1)
165 
166 /*========================================================================
167  * Function prototypes.
168  *========================================================================*/
169 
170 /*
171  * Routines used for growing the Btree.
172  */
173 int	xfs_da3_node_create(struct xfs_da_args *args, xfs_dablk_t blkno,
174 			    int level, struct xfs_buf **bpp, int whichfork);
175 int	xfs_da3_split(xfs_da_state_t *state);
176 
177 /*
178  * Routines used for shrinking the Btree.
179  */
180 int	xfs_da3_join(xfs_da_state_t *state);
181 void	xfs_da3_fixhashpath(struct xfs_da_state *state,
182 			    struct xfs_da_state_path *path_to_to_fix);
183 
184 /*
185  * Routines used for finding things in the Btree.
186  */
187 int	xfs_da3_node_lookup_int(xfs_da_state_t *state, int *result);
188 int	xfs_da3_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
189 					 int forward, int release, int *result);
190 /*
191  * Utility routines.
192  */
193 int	xfs_da3_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
194 				       xfs_da_state_blk_t *new_blk);
195 int	xfs_da3_node_read(struct xfs_trans *tp, struct xfs_inode *dp,
196 			xfs_dablk_t bno, struct xfs_buf **bpp, int whichfork);
197 int	xfs_da3_node_read_mapped(struct xfs_trans *tp, struct xfs_inode *dp,
198 			xfs_daddr_t mappedbno, struct xfs_buf **bpp,
199 			int whichfork);
200 
201 /*
202  * Utility routines.
203  */
204 
205 #define XFS_DABUF_MAP_HOLE_OK	(1u << 0)
206 
207 int	xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno);
208 int	xfs_da_grow_inode_int(struct xfs_da_args *args, xfs_fileoff_t *bno,
209 			      int count);
210 int	xfs_da_get_buf(struct xfs_trans *trans, struct xfs_inode *dp,
211 		xfs_dablk_t bno, struct xfs_buf **bp, int whichfork);
212 int	xfs_da_read_buf(struct xfs_trans *trans, struct xfs_inode *dp,
213 		xfs_dablk_t bno, unsigned int flags, struct xfs_buf **bpp,
214 		int whichfork, const struct xfs_buf_ops *ops);
215 int	xfs_da_reada_buf(struct xfs_inode *dp, xfs_dablk_t bno,
216 		unsigned int flags, int whichfork,
217 		const struct xfs_buf_ops *ops);
218 int	xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
219 					  struct xfs_buf *dead_buf);
220 
221 uint xfs_da_hashname(const uint8_t *name_string, int name_length);
222 enum xfs_dacmp xfs_da_compname(struct xfs_da_args *args,
223 				const unsigned char *name, int len);
224 
225 
226 struct xfs_da_state *xfs_da_state_alloc(struct xfs_da_args *args);
227 void xfs_da_state_free(xfs_da_state_t *state);
228 void xfs_da_state_reset(struct xfs_da_state *state, struct xfs_da_args *args);
229 
230 void	xfs_da3_node_hdr_from_disk(struct xfs_mount *mp,
231 		struct xfs_da3_icnode_hdr *to, struct xfs_da_intnode *from);
232 void	xfs_da3_node_hdr_to_disk(struct xfs_mount *mp,
233 		struct xfs_da_intnode *to, struct xfs_da3_icnode_hdr *from);
234 
235 extern struct kmem_cache	*xfs_da_state_cache;
236 
237 #endif	/* __XFS_DA_BTREE_H__ */
238