1 /*
2  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * 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.
13  *
14  * 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
17  */
18 #ifndef __XFS_BTREE_H__
19 #define	__XFS_BTREE_H__
20 
21 struct xfs_buf;
22 struct xfs_bmap_free;
23 struct xfs_inode;
24 struct xfs_mount;
25 struct xfs_trans;
26 
27 extern kmem_zone_t	*xfs_btree_cur_zone;
28 
29 /*
30  * This nonsense is to make -wlint happy.
31  */
32 #define	XFS_LOOKUP_EQ	((xfs_lookup_t)XFS_LOOKUP_EQi)
33 #define	XFS_LOOKUP_LE	((xfs_lookup_t)XFS_LOOKUP_LEi)
34 #define	XFS_LOOKUP_GE	((xfs_lookup_t)XFS_LOOKUP_GEi)
35 
36 #define	XFS_BTNUM_BNO	((xfs_btnum_t)XFS_BTNUM_BNOi)
37 #define	XFS_BTNUM_CNT	((xfs_btnum_t)XFS_BTNUM_CNTi)
38 #define	XFS_BTNUM_BMAP	((xfs_btnum_t)XFS_BTNUM_BMAPi)
39 #define	XFS_BTNUM_INO	((xfs_btnum_t)XFS_BTNUM_INOi)
40 
41 /*
42  * Generic btree header.
43  *
44  * This is a combination of the actual format used on disk for short and long
45  * format btrees.  The first three fields are shared by both format, but
46  * the pointers are different and should be used with care.
47  *
48  * To get the size of the actual short or long form headers please use
49  * the size macros below.  Never use sizeof(xfs_btree_block).
50  */
51 struct xfs_btree_block {
52 	__be32		bb_magic;	/* magic number for block type */
53 	__be16		bb_level;	/* 0 is a leaf */
54 	__be16		bb_numrecs;	/* current # of data records */
55 	union {
56 		struct {
57 			__be32		bb_leftsib;
58 			__be32		bb_rightsib;
59 		} s;			/* short form pointers */
60 		struct	{
61 			__be64		bb_leftsib;
62 			__be64		bb_rightsib;
63 		} l;			/* long form pointers */
64 	} bb_u;				/* rest */
65 };
66 
67 #define XFS_BTREE_SBLOCK_LEN	16	/* size of a short form block */
68 #define XFS_BTREE_LBLOCK_LEN	24	/* size of a long form block */
69 
70 
71 /*
72  * Generic key, ptr and record wrapper structures.
73  *
74  * These are disk format structures, and are converted where necessary
75  * by the btree specific code that needs to interpret them.
76  */
77 union xfs_btree_ptr {
78 	__be32			s;	/* short form ptr */
79 	__be64			l;	/* long form ptr */
80 };
81 
82 union xfs_btree_key {
83 	xfs_bmbt_key_t		bmbt;
84 	xfs_bmdr_key_t		bmbr;	/* bmbt root block */
85 	xfs_alloc_key_t		alloc;
86 	xfs_inobt_key_t		inobt;
87 };
88 
89 union xfs_btree_rec {
90 	xfs_bmbt_rec_t		bmbt;
91 	xfs_bmdr_rec_t		bmbr;	/* bmbt root block */
92 	xfs_alloc_rec_t		alloc;
93 	xfs_inobt_rec_t		inobt;
94 };
95 
96 /*
97  * For logging record fields.
98  */
99 #define	XFS_BB_MAGIC		0x01
100 #define	XFS_BB_LEVEL		0x02
101 #define	XFS_BB_NUMRECS		0x04
102 #define	XFS_BB_LEFTSIB		0x08
103 #define	XFS_BB_RIGHTSIB		0x10
104 #define	XFS_BB_NUM_BITS		5
105 #define	XFS_BB_ALL_BITS		((1 << XFS_BB_NUM_BITS) - 1)
106 
107 /*
108  * Magic numbers for btree blocks.
109  */
110 extern const __uint32_t	xfs_magics[];
111 
112 /*
113  * Generic stats interface
114  */
115 #define __XFS_BTREE_STATS_INC(type, stat) \
116 	XFS_STATS_INC(xs_ ## type ## _2_ ## stat)
117 #define XFS_BTREE_STATS_INC(cur, stat)  \
118 do {    \
119 	switch (cur->bc_btnum) {  \
120 	case XFS_BTNUM_BNO: __XFS_BTREE_STATS_INC(abtb, stat); break;	\
121 	case XFS_BTNUM_CNT: __XFS_BTREE_STATS_INC(abtc, stat); break;	\
122 	case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_INC(bmbt, stat); break;	\
123 	case XFS_BTNUM_INO: __XFS_BTREE_STATS_INC(ibt, stat); break;	\
124 	case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break;	\
125 	}       \
126 } while (0)
127 
128 #define __XFS_BTREE_STATS_ADD(type, stat, val) \
129 	XFS_STATS_ADD(xs_ ## type ## _2_ ## stat, val)
130 #define XFS_BTREE_STATS_ADD(cur, stat, val)  \
131 do {    \
132 	switch (cur->bc_btnum) {  \
133 	case XFS_BTNUM_BNO: __XFS_BTREE_STATS_ADD(abtb, stat, val); break; \
134 	case XFS_BTNUM_CNT: __XFS_BTREE_STATS_ADD(abtc, stat, val); break; \
135 	case XFS_BTNUM_BMAP: __XFS_BTREE_STATS_ADD(bmbt, stat, val); break; \
136 	case XFS_BTNUM_INO: __XFS_BTREE_STATS_ADD(ibt, stat, val); break; \
137 	case XFS_BTNUM_MAX: ASSERT(0); /* fucking gcc */ ; break;	\
138 	}       \
139 } while (0)
140 
141 #define	XFS_BTREE_MAXLEVELS	8	/* max of all btrees */
142 
143 struct xfs_btree_ops {
144 	/* size of the key and record structures */
145 	size_t	key_len;
146 	size_t	rec_len;
147 
148 	/* cursor operations */
149 	struct xfs_btree_cur *(*dup_cursor)(struct xfs_btree_cur *);
150 	void	(*update_cursor)(struct xfs_btree_cur *src,
151 				 struct xfs_btree_cur *dst);
152 
153 	/* update btree root pointer */
154 	void	(*set_root)(struct xfs_btree_cur *cur,
155 			    union xfs_btree_ptr *nptr, int level_change);
156 
157 	/* block allocation / freeing */
158 	int	(*alloc_block)(struct xfs_btree_cur *cur,
159 			       union xfs_btree_ptr *start_bno,
160 			       union xfs_btree_ptr *new_bno,
161 			       int length, int *stat);
162 	int	(*free_block)(struct xfs_btree_cur *cur, struct xfs_buf *bp);
163 
164 	/* update last record information */
165 	void	(*update_lastrec)(struct xfs_btree_cur *cur,
166 				  struct xfs_btree_block *block,
167 				  union xfs_btree_rec *rec,
168 				  int ptr, int reason);
169 
170 	/* records in block/level */
171 	int	(*get_minrecs)(struct xfs_btree_cur *cur, int level);
172 	int	(*get_maxrecs)(struct xfs_btree_cur *cur, int level);
173 
174 	/* records on disk.  Matter for the root in inode case. */
175 	int	(*get_dmaxrecs)(struct xfs_btree_cur *cur, int level);
176 
177 	/* init values of btree structures */
178 	void	(*init_key_from_rec)(union xfs_btree_key *key,
179 				     union xfs_btree_rec *rec);
180 	void	(*init_rec_from_key)(union xfs_btree_key *key,
181 				     union xfs_btree_rec *rec);
182 	void	(*init_rec_from_cur)(struct xfs_btree_cur *cur,
183 				     union xfs_btree_rec *rec);
184 	void	(*init_ptr_from_cur)(struct xfs_btree_cur *cur,
185 				     union xfs_btree_ptr *ptr);
186 
187 	/* difference between key value and cursor value */
188 	__int64_t (*key_diff)(struct xfs_btree_cur *cur,
189 			      union xfs_btree_key *key);
190 
191 #ifdef DEBUG
192 	/* check that k1 is lower than k2 */
193 	int	(*keys_inorder)(struct xfs_btree_cur *cur,
194 				union xfs_btree_key *k1,
195 				union xfs_btree_key *k2);
196 
197 	/* check that r1 is lower than r2 */
198 	int	(*recs_inorder)(struct xfs_btree_cur *cur,
199 				union xfs_btree_rec *r1,
200 				union xfs_btree_rec *r2);
201 #endif
202 
203 	/* btree tracing */
204 #ifdef XFS_BTREE_TRACE
205 	void		(*trace_enter)(struct xfs_btree_cur *, const char *,
206 				       char *, int, int, __psunsigned_t,
207 				       __psunsigned_t, __psunsigned_t,
208 				       __psunsigned_t, __psunsigned_t,
209 				       __psunsigned_t, __psunsigned_t,
210 				       __psunsigned_t, __psunsigned_t,
211 				       __psunsigned_t, __psunsigned_t);
212 	void		(*trace_cursor)(struct xfs_btree_cur *, __uint32_t *,
213 					__uint64_t *, __uint64_t *);
214 	void		(*trace_key)(struct xfs_btree_cur *,
215 				     union xfs_btree_key *, __uint64_t *,
216 				     __uint64_t *);
217 	void		(*trace_record)(struct xfs_btree_cur *,
218 					union xfs_btree_rec *, __uint64_t *,
219 					__uint64_t *, __uint64_t *);
220 #endif
221 };
222 
223 /*
224  * Reasons for the update_lastrec method to be called.
225  */
226 #define LASTREC_UPDATE	0
227 #define LASTREC_INSREC	1
228 #define LASTREC_DELREC	2
229 
230 
231 /*
232  * Btree cursor structure.
233  * This collects all information needed by the btree code in one place.
234  */
235 typedef struct xfs_btree_cur
236 {
237 	struct xfs_trans	*bc_tp;	/* transaction we're in, if any */
238 	struct xfs_mount	*bc_mp;	/* file system mount struct */
239 	const struct xfs_btree_ops *bc_ops;
240 	uint			bc_flags; /* btree features - below */
241 	union {
242 		xfs_alloc_rec_incore_t	a;
243 		xfs_bmbt_irec_t		b;
244 		xfs_inobt_rec_incore_t	i;
245 	}		bc_rec;		/* current insert/search record value */
246 	struct xfs_buf	*bc_bufs[XFS_BTREE_MAXLEVELS];	/* buf ptr per level */
247 	int		bc_ptrs[XFS_BTREE_MAXLEVELS];	/* key/record # */
248 	__uint8_t	bc_ra[XFS_BTREE_MAXLEVELS];	/* readahead bits */
249 #define	XFS_BTCUR_LEFTRA	1	/* left sibling has been read-ahead */
250 #define	XFS_BTCUR_RIGHTRA	2	/* right sibling has been read-ahead */
251 	__uint8_t	bc_nlevels;	/* number of levels in the tree */
252 	__uint8_t	bc_blocklog;	/* log2(blocksize) of btree blocks */
253 	xfs_btnum_t	bc_btnum;	/* identifies which btree type */
254 	union {
255 		struct {			/* needed for BNO, CNT, INO */
256 			struct xfs_buf	*agbp;	/* agf/agi buffer pointer */
257 			xfs_agnumber_t	agno;	/* ag number */
258 		} a;
259 		struct {			/* needed for BMAP */
260 			struct xfs_inode *ip;	/* pointer to our inode */
261 			struct xfs_bmap_free *flist;	/* list to free after */
262 			xfs_fsblock_t	firstblock;	/* 1st blk allocated */
263 			int		allocated;	/* count of alloced */
264 			short		forksize;	/* fork's inode space */
265 			char		whichfork;	/* data or attr fork */
266 			char		flags;		/* flags */
267 #define	XFS_BTCUR_BPRV_WASDEL	1			/* was delayed */
268 		} b;
269 	}		bc_private;	/* per-btree type data */
270 } xfs_btree_cur_t;
271 
272 /* cursor flags */
273 #define XFS_BTREE_LONG_PTRS		(1<<0)	/* pointers are 64bits long */
274 #define XFS_BTREE_ROOT_IN_INODE		(1<<1)	/* root may be variable size */
275 #define XFS_BTREE_LASTREC_UPDATE	(1<<2)	/* track last rec externally */
276 
277 
278 #define	XFS_BTREE_NOERROR	0
279 #define	XFS_BTREE_ERROR		1
280 
281 /*
282  * Convert from buffer to btree block header.
283  */
284 #define	XFS_BUF_TO_BLOCK(bp)	((struct xfs_btree_block *)XFS_BUF_PTR(bp))
285 
286 
287 /*
288  * Check that block header is ok.
289  */
290 int
291 xfs_btree_check_block(
292 	struct xfs_btree_cur	*cur,	/* btree cursor */
293 	struct xfs_btree_block	*block,	/* generic btree block pointer */
294 	int			level,	/* level of the btree block */
295 	struct xfs_buf		*bp);	/* buffer containing block, if any */
296 
297 /*
298  * Check that (long) pointer is ok.
299  */
300 int					/* error (0 or EFSCORRUPTED) */
301 xfs_btree_check_lptr(
302 	struct xfs_btree_cur	*cur,	/* btree cursor */
303 	xfs_dfsbno_t		ptr,	/* btree block disk address */
304 	int			level);	/* btree block level */
305 
306 /*
307  * Delete the btree cursor.
308  */
309 void
310 xfs_btree_del_cursor(
311 	xfs_btree_cur_t		*cur,	/* btree cursor */
312 	int			error);	/* del because of error */
313 
314 /*
315  * Duplicate the btree cursor.
316  * Allocate a new one, copy the record, re-get the buffers.
317  */
318 int					/* error */
319 xfs_btree_dup_cursor(
320 	xfs_btree_cur_t		*cur,	/* input cursor */
321 	xfs_btree_cur_t		**ncur);/* output cursor */
322 
323 /*
324  * Get a buffer for the block, return it with no data read.
325  * Long-form addressing.
326  */
327 struct xfs_buf *				/* buffer for fsbno */
328 xfs_btree_get_bufl(
329 	struct xfs_mount	*mp,	/* file system mount point */
330 	struct xfs_trans	*tp,	/* transaction pointer */
331 	xfs_fsblock_t		fsbno,	/* file system block number */
332 	uint			lock);	/* lock flags for get_buf */
333 
334 /*
335  * Get a buffer for the block, return it with no data read.
336  * Short-form addressing.
337  */
338 struct xfs_buf *				/* buffer for agno/agbno */
339 xfs_btree_get_bufs(
340 	struct xfs_mount	*mp,	/* file system mount point */
341 	struct xfs_trans	*tp,	/* transaction pointer */
342 	xfs_agnumber_t		agno,	/* allocation group number */
343 	xfs_agblock_t		agbno,	/* allocation group block number */
344 	uint			lock);	/* lock flags for get_buf */
345 
346 /*
347  * Check for the cursor referring to the last block at the given level.
348  */
349 int					/* 1=is last block, 0=not last block */
350 xfs_btree_islastblock(
351 	xfs_btree_cur_t		*cur,	/* btree cursor */
352 	int			level);	/* level to check */
353 
354 /*
355  * Compute first and last byte offsets for the fields given.
356  * Interprets the offsets table, which contains struct field offsets.
357  */
358 void
359 xfs_btree_offsets(
360 	__int64_t		fields,	/* bitmask of fields */
361 	const short		*offsets,/* table of field offsets */
362 	int			nbits,	/* number of bits to inspect */
363 	int			*first,	/* output: first byte offset */
364 	int			*last);	/* output: last byte offset */
365 
366 /*
367  * Get a buffer for the block, return it read in.
368  * Long-form addressing.
369  */
370 int					/* error */
371 xfs_btree_read_bufl(
372 	struct xfs_mount	*mp,	/* file system mount point */
373 	struct xfs_trans	*tp,	/* transaction pointer */
374 	xfs_fsblock_t		fsbno,	/* file system block number */
375 	uint			lock,	/* lock flags for read_buf */
376 	struct xfs_buf		**bpp,	/* buffer for fsbno */
377 	int			refval);/* ref count value for buffer */
378 
379 /*
380  * Read-ahead the block, don't wait for it, don't return a buffer.
381  * Long-form addressing.
382  */
383 void					/* error */
384 xfs_btree_reada_bufl(
385 	struct xfs_mount	*mp,	/* file system mount point */
386 	xfs_fsblock_t		fsbno,	/* file system block number */
387 	xfs_extlen_t		count);	/* count of filesystem blocks */
388 
389 /*
390  * Read-ahead the block, don't wait for it, don't return a buffer.
391  * Short-form addressing.
392  */
393 void					/* error */
394 xfs_btree_reada_bufs(
395 	struct xfs_mount	*mp,	/* file system mount point */
396 	xfs_agnumber_t		agno,	/* allocation group number */
397 	xfs_agblock_t		agbno,	/* allocation group block number */
398 	xfs_extlen_t		count);	/* count of filesystem blocks */
399 
400 
401 /*
402  * Common btree core entry points.
403  */
404 int xfs_btree_increment(struct xfs_btree_cur *, int, int *);
405 int xfs_btree_decrement(struct xfs_btree_cur *, int, int *);
406 int xfs_btree_lookup(struct xfs_btree_cur *, xfs_lookup_t, int *);
407 int xfs_btree_update(struct xfs_btree_cur *, union xfs_btree_rec *);
408 int xfs_btree_new_iroot(struct xfs_btree_cur *, int *, int *);
409 int xfs_btree_insert(struct xfs_btree_cur *, int *);
410 int xfs_btree_delete(struct xfs_btree_cur *, int *);
411 int xfs_btree_get_rec(struct xfs_btree_cur *, union xfs_btree_rec **, int *);
412 
413 /*
414  * Internal btree helpers also used by xfs_bmap.c.
415  */
416 void xfs_btree_log_block(struct xfs_btree_cur *, struct xfs_buf *, int);
417 void xfs_btree_log_recs(struct xfs_btree_cur *, struct xfs_buf *, int, int);
418 
419 /*
420  * Helpers.
421  */
xfs_btree_get_numrecs(struct xfs_btree_block * block)422 static inline int xfs_btree_get_numrecs(struct xfs_btree_block *block)
423 {
424 	return be16_to_cpu(block->bb_numrecs);
425 }
426 
xfs_btree_set_numrecs(struct xfs_btree_block * block,__uint16_t numrecs)427 static inline void xfs_btree_set_numrecs(struct xfs_btree_block *block,
428 		__uint16_t numrecs)
429 {
430 	block->bb_numrecs = cpu_to_be16(numrecs);
431 }
432 
xfs_btree_get_level(struct xfs_btree_block * block)433 static inline int xfs_btree_get_level(struct xfs_btree_block *block)
434 {
435 	return be16_to_cpu(block->bb_level);
436 }
437 
438 
439 /*
440  * Min and max functions for extlen, agblock, fileoff, and filblks types.
441  */
442 #define	XFS_EXTLEN_MIN(a,b)	min_t(xfs_extlen_t, (a), (b))
443 #define	XFS_EXTLEN_MAX(a,b)	max_t(xfs_extlen_t, (a), (b))
444 #define	XFS_AGBLOCK_MIN(a,b)	min_t(xfs_agblock_t, (a), (b))
445 #define	XFS_AGBLOCK_MAX(a,b)	max_t(xfs_agblock_t, (a), (b))
446 #define	XFS_FILEOFF_MIN(a,b)	min_t(xfs_fileoff_t, (a), (b))
447 #define	XFS_FILEOFF_MAX(a,b)	max_t(xfs_fileoff_t, (a), (b))
448 #define	XFS_FILBLKS_MIN(a,b)	min_t(xfs_filblks_t, (a), (b))
449 #define	XFS_FILBLKS_MAX(a,b)	max_t(xfs_filblks_t, (a), (b))
450 
451 #define	XFS_FSB_SANITY_CHECK(mp,fsb)	\
452 	(XFS_FSB_TO_AGNO(mp, fsb) < mp->m_sb.sb_agcount && \
453 		XFS_FSB_TO_AGBNO(mp, fsb) < mp->m_sb.sb_agblocks)
454 
455 #endif	/* __XFS_BTREE_H__ */
456