xref: /linux/fs/xfs/libxfs/xfs_rtrmap_btree.h (revision 71b8acb42be60e11810eb43a6f470589fcf7b7dd)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (c) 2018-2024 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #ifndef __XFS_RTRMAP_BTREE_H__
7 #define __XFS_RTRMAP_BTREE_H__
8 
9 struct xfs_buf;
10 struct xfs_btree_cur;
11 struct xfs_mount;
12 struct xbtree_ifakeroot;
13 struct xfs_rtgroup;
14 
15 /* rmaps only exist on crc enabled filesystems */
16 #define XFS_RTRMAP_BLOCK_LEN	XFS_BTREE_LBLOCK_CRC_LEN
17 
18 struct xfs_btree_cur *xfs_rtrmapbt_init_cursor(struct xfs_trans *tp,
19 		struct xfs_rtgroup *rtg);
20 struct xfs_btree_cur *xfs_rtrmapbt_stage_cursor(struct xfs_mount *mp,
21 		struct xfs_rtgroup *rtg, struct xfs_inode *ip,
22 		struct xbtree_ifakeroot *ifake);
23 void xfs_rtrmapbt_commit_staged_btree(struct xfs_btree_cur *cur,
24 		struct xfs_trans *tp);
25 unsigned int xfs_rtrmapbt_maxrecs(struct xfs_mount *mp, unsigned int blocklen,
26 		bool leaf);
27 void xfs_rtrmapbt_compute_maxlevels(struct xfs_mount *mp);
28 unsigned int xfs_rtrmapbt_droot_maxrecs(unsigned int blocklen, bool leaf);
29 
30 /*
31  * Addresses of records, keys, and pointers within an incore rtrmapbt block.
32  *
33  * (note that some of these may appear unused, but they are used in userspace)
34  */
35 static inline struct xfs_rmap_rec *
36 xfs_rtrmap_rec_addr(
37 	struct xfs_btree_block	*block,
38 	unsigned int		index)
39 {
40 	return (struct xfs_rmap_rec *)
41 		((char *)block + XFS_RTRMAP_BLOCK_LEN +
42 		 (index - 1) * sizeof(struct xfs_rmap_rec));
43 }
44 
45 static inline struct xfs_rmap_key *
46 xfs_rtrmap_key_addr(
47 	struct xfs_btree_block	*block,
48 	unsigned int		index)
49 {
50 	return (struct xfs_rmap_key *)
51 		((char *)block + XFS_RTRMAP_BLOCK_LEN +
52 		 (index - 1) * 2 * sizeof(struct xfs_rmap_key));
53 }
54 
55 static inline struct xfs_rmap_key *
56 xfs_rtrmap_high_key_addr(
57 	struct xfs_btree_block	*block,
58 	unsigned int		index)
59 {
60 	return (struct xfs_rmap_key *)
61 		((char *)block + XFS_RTRMAP_BLOCK_LEN +
62 		 sizeof(struct xfs_rmap_key) +
63 		 (index - 1) * 2 * sizeof(struct xfs_rmap_key));
64 }
65 
66 static inline xfs_rtrmap_ptr_t *
67 xfs_rtrmap_ptr_addr(
68 	struct xfs_btree_block	*block,
69 	unsigned int		index,
70 	unsigned int		maxrecs)
71 {
72 	return (xfs_rtrmap_ptr_t *)
73 		((char *)block + XFS_RTRMAP_BLOCK_LEN +
74 		 maxrecs * 2 * sizeof(struct xfs_rmap_key) +
75 		 (index - 1) * sizeof(xfs_rtrmap_ptr_t));
76 }
77 
78 unsigned int xfs_rtrmapbt_maxlevels_ondisk(void);
79 
80 int __init xfs_rtrmapbt_init_cur_cache(void);
81 void xfs_rtrmapbt_destroy_cur_cache(void);
82 
83 xfs_filblks_t xfs_rtrmapbt_calc_reserves(struct xfs_mount *mp);
84 
85 /* Addresses of key, pointers, and records within an ondisk rtrmapbt block. */
86 
87 static inline struct xfs_rmap_rec *
88 xfs_rtrmap_droot_rec_addr(
89 	struct xfs_rtrmap_root	*block,
90 	unsigned int		index)
91 {
92 	return (struct xfs_rmap_rec *)
93 		((char *)(block + 1) +
94 		 (index - 1) * sizeof(struct xfs_rmap_rec));
95 }
96 
97 static inline struct xfs_rmap_key *
98 xfs_rtrmap_droot_key_addr(
99 	struct xfs_rtrmap_root	*block,
100 	unsigned int		index)
101 {
102 	return (struct xfs_rmap_key *)
103 		((char *)(block + 1) +
104 		 (index - 1) * 2 * sizeof(struct xfs_rmap_key));
105 }
106 
107 static inline xfs_rtrmap_ptr_t *
108 xfs_rtrmap_droot_ptr_addr(
109 	struct xfs_rtrmap_root	*block,
110 	unsigned int		index,
111 	unsigned int		maxrecs)
112 {
113 	return (xfs_rtrmap_ptr_t *)
114 		((char *)(block + 1) +
115 		 maxrecs * 2 * sizeof(struct xfs_rmap_key) +
116 		 (index - 1) * sizeof(xfs_rtrmap_ptr_t));
117 }
118 
119 /*
120  * Address of pointers within the incore btree root.
121  *
122  * These are to be used when we know the size of the block and
123  * we don't have a cursor.
124  */
125 static inline xfs_rtrmap_ptr_t *
126 xfs_rtrmap_broot_ptr_addr(
127 	struct xfs_mount	*mp,
128 	struct xfs_btree_block	*bb,
129 	unsigned int		index,
130 	unsigned int		block_size)
131 {
132 	return xfs_rtrmap_ptr_addr(bb, index,
133 			xfs_rtrmapbt_maxrecs(mp, block_size, false));
134 }
135 
136 /*
137  * Compute the space required for the incore btree root containing the given
138  * number of records.
139  */
140 static inline size_t
141 xfs_rtrmap_broot_space_calc(
142 	struct xfs_mount	*mp,
143 	unsigned int		level,
144 	unsigned int		nrecs)
145 {
146 	size_t			sz = XFS_RTRMAP_BLOCK_LEN;
147 
148 	if (level > 0)
149 		return sz + nrecs * (2 * sizeof(struct xfs_rmap_key) +
150 					 sizeof(xfs_rtrmap_ptr_t));
151 	return sz + nrecs * sizeof(struct xfs_rmap_rec);
152 }
153 
154 /*
155  * Compute the space required for the incore btree root given the ondisk
156  * btree root block.
157  */
158 static inline size_t
159 xfs_rtrmap_broot_space(struct xfs_mount *mp, struct xfs_rtrmap_root *bb)
160 {
161 	return xfs_rtrmap_broot_space_calc(mp, be16_to_cpu(bb->bb_level),
162 			be16_to_cpu(bb->bb_numrecs));
163 }
164 
165 /* Compute the space required for the ondisk root block. */
166 static inline size_t
167 xfs_rtrmap_droot_space_calc(
168 	unsigned int		level,
169 	unsigned int		nrecs)
170 {
171 	size_t			sz = sizeof(struct xfs_rtrmap_root);
172 
173 	if (level > 0)
174 		return sz + nrecs * (2 * sizeof(struct xfs_rmap_key) +
175 					 sizeof(xfs_rtrmap_ptr_t));
176 	return sz + nrecs * sizeof(struct xfs_rmap_rec);
177 }
178 
179 /*
180  * Compute the space required for the ondisk root block given an incore root
181  * block.
182  */
183 static inline size_t
184 xfs_rtrmap_droot_space(struct xfs_btree_block *bb)
185 {
186 	return xfs_rtrmap_droot_space_calc(be16_to_cpu(bb->bb_level),
187 			be16_to_cpu(bb->bb_numrecs));
188 }
189 
190 int xfs_iformat_rtrmap(struct xfs_inode *ip, struct xfs_dinode *dip);
191 void xfs_rtrmapbt_to_disk(struct xfs_mount *mp, struct xfs_btree_block *rblock,
192 		unsigned int rblocklen, struct xfs_rtrmap_root *dblock,
193 		unsigned int dblocklen);
194 void xfs_iflush_rtrmap(struct xfs_inode *ip, struct xfs_dinode *dip);
195 
196 int xfs_rtrmapbt_create(struct xfs_rtgroup *rtg, struct xfs_inode *ip,
197 		struct xfs_trans *tp, bool init);
198 int xfs_rtrmapbt_init_rtsb(struct xfs_mount *mp, struct xfs_rtgroup *rtg,
199 		struct xfs_trans *tp);
200 
201 #endif /* __XFS_RTRMAP_BTREE_H__ */
202