xref: /linux/fs/xfs/libxfs/xfs_bmap_btree.h (revision 9e56ff53b4115875667760445b028357848b4748)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2000,2002-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #ifndef __XFS_BMAP_BTREE_H__
7 #define __XFS_BMAP_BTREE_H__
8 
9 struct xfs_btree_cur;
10 struct xfs_btree_block;
11 struct xfs_mount;
12 struct xfs_inode;
13 struct xfs_trans;
14 struct xbtree_ifakeroot;
15 
16 /*
17  * Btree block header size depends on a superblock flag.
18  */
19 #define XFS_BMBT_BLOCK_LEN(mp) \
20 	(xfs_has_crc(((mp))) ? \
21 		XFS_BTREE_LBLOCK_CRC_LEN : XFS_BTREE_LBLOCK_LEN)
22 
23 #define XFS_BMBT_REC_ADDR(mp, block, index) \
24 	((xfs_bmbt_rec_t *) \
25 		((char *)(block) + \
26 		 XFS_BMBT_BLOCK_LEN(mp) + \
27 		 ((index) - 1) * sizeof(xfs_bmbt_rec_t)))
28 
29 #define XFS_BMBT_KEY_ADDR(mp, block, index) \
30 	((xfs_bmbt_key_t *) \
31 		((char *)(block) + \
32 		 XFS_BMBT_BLOCK_LEN(mp) + \
33 		 ((index) - 1) * sizeof(xfs_bmbt_key_t)))
34 
35 #define XFS_BMBT_PTR_ADDR(mp, block, index, maxrecs) \
36 	((xfs_bmbt_ptr_t *) \
37 		((char *)(block) + \
38 		 XFS_BMBT_BLOCK_LEN(mp) + \
39 		 (maxrecs) * sizeof(xfs_bmbt_key_t) + \
40 		 ((index) - 1) * sizeof(xfs_bmbt_ptr_t)))
41 
42 #define XFS_BMDR_REC_ADDR(block, index) \
43 	((xfs_bmdr_rec_t *) \
44 		((char *)(block) + \
45 		 sizeof(struct xfs_bmdr_block) + \
46 	         ((index) - 1) * sizeof(xfs_bmdr_rec_t)))
47 
48 #define XFS_BMDR_KEY_ADDR(block, index) \
49 	((xfs_bmdr_key_t *) \
50 		((char *)(block) + \
51 		 sizeof(struct xfs_bmdr_block) + \
52 		 ((index) - 1) * sizeof(xfs_bmdr_key_t)))
53 
54 #define XFS_BMDR_PTR_ADDR(block, index, maxrecs) \
55 	((xfs_bmdr_ptr_t *) \
56 		((char *)(block) + \
57 		 sizeof(struct xfs_bmdr_block) + \
58 		 (maxrecs) * sizeof(xfs_bmdr_key_t) + \
59 		 ((index) - 1) * sizeof(xfs_bmdr_ptr_t)))
60 
61 /*
62  * These are to be used when we know the size of the block and
63  * we don't have a cursor.
64  */
65 #define XFS_BMAP_BROOT_PTR_ADDR(mp, bb, i, sz) \
66 	XFS_BMBT_PTR_ADDR(mp, bb, i, xfs_bmbt_maxrecs(mp, sz, 0))
67 
68 #define XFS_BMAP_BROOT_SPACE_CALC(mp, nrecs) \
69 	(int)(XFS_BMBT_BLOCK_LEN(mp) + \
70 	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
71 
72 #define XFS_BMAP_BROOT_SPACE(mp, bb) \
73 	(XFS_BMAP_BROOT_SPACE_CALC(mp, be16_to_cpu((bb)->bb_numrecs)))
74 #define XFS_BMDR_SPACE_CALC(nrecs) \
75 	(int)(sizeof(xfs_bmdr_block_t) + \
76 	       ((nrecs) * (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t))))
77 #define XFS_BMAP_BMDR_SPACE(bb) \
78 	(XFS_BMDR_SPACE_CALC(be16_to_cpu((bb)->bb_numrecs)))
79 
80 /*
81  * Maximum number of bmap btree levels.
82  */
83 #define XFS_BM_MAXLEVELS(mp,w)		((mp)->m_bm_maxlevels[(w)])
84 
85 /*
86  * Prototypes for xfs_bmap.c to call.
87  */
88 extern void xfs_bmdr_to_bmbt(struct xfs_inode *, xfs_bmdr_block_t *, int,
89 			struct xfs_btree_block *, int);
90 
91 void xfs_bmbt_disk_set_all(struct xfs_bmbt_rec *r, struct xfs_bmbt_irec *s);
92 extern xfs_filblks_t xfs_bmbt_disk_get_blockcount(const struct xfs_bmbt_rec *r);
93 extern xfs_fileoff_t xfs_bmbt_disk_get_startoff(const struct xfs_bmbt_rec *r);
94 void xfs_bmbt_disk_get_all(const struct xfs_bmbt_rec *r,
95 		struct xfs_bmbt_irec *s);
96 
97 extern void xfs_bmbt_to_bmdr(struct xfs_mount *, struct xfs_btree_block *, int,
98 			xfs_bmdr_block_t *, int);
99 
100 extern int xfs_bmbt_get_maxrecs(struct xfs_btree_cur *, int level);
101 extern int xfs_bmdr_maxrecs(int blocklen, int leaf);
102 extern int xfs_bmbt_maxrecs(struct xfs_mount *, int blocklen, int leaf);
103 
104 extern int xfs_bmbt_change_owner(struct xfs_trans *tp, struct xfs_inode *ip,
105 				 int whichfork, xfs_ino_t new_owner,
106 				 struct list_head *buffer_list);
107 
108 extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
109 		struct xfs_trans *, struct xfs_inode *, int);
110 struct xfs_btree_cur *xfs_bmbt_stage_cursor(struct xfs_mount *mp,
111 		struct xfs_inode *ip, struct xbtree_ifakeroot *ifake);
112 void xfs_bmbt_commit_staged_btree(struct xfs_btree_cur *cur,
113 		struct xfs_trans *tp, int whichfork);
114 
115 extern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
116 		unsigned long long len);
117 
118 unsigned int xfs_bmbt_maxlevels_ondisk(void);
119 
120 int __init xfs_bmbt_init_cur_cache(void);
121 void xfs_bmbt_destroy_cur_cache(void);
122 
123 #endif	/* __XFS_BMAP_BTREE_H__ */
124