]>
Commit | Line | Data |
---|---|---|
b3a96b46 DW |
1 | /* |
2 | * Copyright (c) 2014 Red Hat, 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 | #include "libxfs_priv.h" | |
19 | #include "xfs_fs.h" | |
20 | #include "xfs_shared.h" | |
21 | #include "xfs_format.h" | |
22 | #include "xfs_log_format.h" | |
23 | #include "xfs_trans_resv.h" | |
24 | #include "xfs_bit.h" | |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_mount.h" | |
27 | #include "xfs_defer.h" | |
28 | #include "xfs_inode.h" | |
29 | #include "xfs_trans.h" | |
30 | #include "xfs_alloc.h" | |
31 | #include "xfs_btree.h" | |
936ca687 | 32 | #include "xfs_rmap.h" |
b3a96b46 DW |
33 | #include "xfs_rmap_btree.h" |
34 | #include "xfs_trace.h" | |
35 | #include "xfs_cksum.h" | |
02cc8b2a | 36 | #include "xfs_ag_resv.h" |
b3a96b46 | 37 | |
936ca687 DW |
38 | /* |
39 | * Reverse map btree. | |
40 | * | |
41 | * This is a per-ag tree used to track the owner(s) of a given extent. With | |
42 | * reflink it is possible for there to be multiple owners, which is a departure | |
43 | * from classic XFS. Owner records for data extents are inserted when the | |
44 | * extent is mapped and removed when an extent is unmapped. Owner records for | |
45 | * all other block types (i.e. metadata) are inserted when an extent is | |
46 | * allocated and removed when an extent is freed. There can only be one owner | |
47 | * of a metadata extent, usually an inode or some other metadata structure like | |
48 | * an AG btree. | |
49 | * | |
50 | * The rmap btree is part of the free space management, so blocks for the tree | |
51 | * are sourced from the agfl. Hence we need transaction reservation support for | |
52 | * this tree so that the freelist is always large enough. This also impacts on | |
53 | * the minimum space we need to leave free in the AG. | |
54 | * | |
55 | * The tree is ordered by [ag block, owner, offset]. This is a large key size, | |
56 | * but it is the only way to enforce unique keys when a block can be owned by | |
57 | * multiple files at any offset. There's no need to order/search by extent | |
58 | * size for online updating/management of the tree. It is intended that most | |
59 | * reverse lookups will be to find the owner(s) of a particular block, or to | |
60 | * try to recover tree and file data from corrupt primary metadata. | |
61 | */ | |
62 | ||
b3a96b46 DW |
63 | static struct xfs_btree_cur * |
64 | xfs_rmapbt_dup_cursor( | |
65 | struct xfs_btree_cur *cur) | |
66 | { | |
67 | return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp, | |
68 | cur->bc_private.a.agbp, cur->bc_private.a.agno); | |
69 | } | |
70 | ||
936ca687 DW |
71 | STATIC void |
72 | xfs_rmapbt_set_root( | |
73 | struct xfs_btree_cur *cur, | |
74 | union xfs_btree_ptr *ptr, | |
75 | int inc) | |
76 | { | |
77 | struct xfs_buf *agbp = cur->bc_private.a.agbp; | |
78 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); | |
79 | xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno); | |
80 | int btnum = cur->bc_btnum; | |
81 | struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno); | |
82 | ||
83 | ASSERT(ptr->s != 0); | |
84 | ||
85 | agf->agf_roots[btnum] = ptr->s; | |
86 | be32_add_cpu(&agf->agf_levels[btnum], inc); | |
87 | pag->pagf_levels[btnum] += inc; | |
88 | xfs_perag_put(pag); | |
89 | ||
90 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS); | |
91 | } | |
92 | ||
93 | STATIC int | |
94 | xfs_rmapbt_alloc_block( | |
95 | struct xfs_btree_cur *cur, | |
96 | union xfs_btree_ptr *start, | |
97 | union xfs_btree_ptr *new, | |
98 | int *stat) | |
99 | { | |
8511b71a DW |
100 | struct xfs_buf *agbp = cur->bc_private.a.agbp; |
101 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); | |
936ca687 DW |
102 | int error; |
103 | xfs_agblock_t bno; | |
104 | ||
936ca687 DW |
105 | /* Allocate the new block from the freelist. If we can't, give up. */ |
106 | error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp, | |
107 | &bno, 1); | |
97b3ffd0 | 108 | if (error) |
936ca687 | 109 | return error; |
936ca687 DW |
110 | |
111 | trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno, | |
112 | bno, 1); | |
113 | if (bno == NULLAGBLOCK) { | |
936ca687 DW |
114 | *stat = 0; |
115 | return 0; | |
116 | } | |
117 | ||
118 | xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1, | |
119 | false); | |
120 | ||
121 | xfs_trans_agbtree_delta(cur->bc_tp, 1); | |
122 | new->s = cpu_to_be32(bno); | |
8511b71a DW |
123 | be32_add_cpu(&agf->agf_rmap_blocks, 1); |
124 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS); | |
936ca687 | 125 | |
9760cac2 BF |
126 | xfs_ag_resv_rmapbt_alloc(cur->bc_mp, cur->bc_private.a.agno); |
127 | ||
936ca687 DW |
128 | *stat = 1; |
129 | return 0; | |
130 | } | |
131 | ||
132 | STATIC int | |
133 | xfs_rmapbt_free_block( | |
134 | struct xfs_btree_cur *cur, | |
135 | struct xfs_buf *bp) | |
136 | { | |
137 | struct xfs_buf *agbp = cur->bc_private.a.agbp; | |
138 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); | |
139 | xfs_agblock_t bno; | |
140 | int error; | |
141 | ||
142 | bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp)); | |
143 | trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno, | |
144 | bno, 1); | |
8511b71a DW |
145 | be32_add_cpu(&agf->agf_rmap_blocks, -1); |
146 | xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS); | |
936ca687 DW |
147 | error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1); |
148 | if (error) | |
149 | return error; | |
150 | ||
151 | xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1, | |
152 | XFS_EXTENT_BUSY_SKIP_DISCARD); | |
153 | xfs_trans_agbtree_delta(cur->bc_tp, -1); | |
154 | ||
9760cac2 BF |
155 | xfs_ag_resv_rmapbt_free(cur->bc_mp, cur->bc_private.a.agno); |
156 | ||
936ca687 DW |
157 | return 0; |
158 | } | |
159 | ||
160 | STATIC int | |
161 | xfs_rmapbt_get_minrecs( | |
162 | struct xfs_btree_cur *cur, | |
163 | int level) | |
164 | { | |
165 | return cur->bc_mp->m_rmap_mnr[level != 0]; | |
166 | } | |
167 | ||
168 | STATIC int | |
169 | xfs_rmapbt_get_maxrecs( | |
170 | struct xfs_btree_cur *cur, | |
171 | int level) | |
172 | { | |
173 | return cur->bc_mp->m_rmap_mxr[level != 0]; | |
174 | } | |
175 | ||
176 | STATIC void | |
177 | xfs_rmapbt_init_key_from_rec( | |
178 | union xfs_btree_key *key, | |
179 | union xfs_btree_rec *rec) | |
180 | { | |
181 | key->rmap.rm_startblock = rec->rmap.rm_startblock; | |
182 | key->rmap.rm_owner = rec->rmap.rm_owner; | |
183 | key->rmap.rm_offset = rec->rmap.rm_offset; | |
184 | } | |
185 | ||
634b234e DW |
186 | /* |
187 | * The high key for a reverse mapping record can be computed by shifting | |
188 | * the startblock and offset to the highest value that would still map | |
189 | * to that record. In practice this means that we add blockcount-1 to | |
190 | * the startblock for all records, and if the record is for a data/attr | |
191 | * fork mapping, we add blockcount-1 to the offset too. | |
192 | */ | |
193 | STATIC void | |
194 | xfs_rmapbt_init_high_key_from_rec( | |
195 | union xfs_btree_key *key, | |
196 | union xfs_btree_rec *rec) | |
197 | { | |
4a492e72 | 198 | uint64_t off; |
634b234e DW |
199 | int adj; |
200 | ||
201 | adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1; | |
202 | ||
203 | key->rmap.rm_startblock = rec->rmap.rm_startblock; | |
204 | be32_add_cpu(&key->rmap.rm_startblock, adj); | |
205 | key->rmap.rm_owner = rec->rmap.rm_owner; | |
206 | key->rmap.rm_offset = rec->rmap.rm_offset; | |
207 | if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) || | |
208 | XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset))) | |
209 | return; | |
210 | off = be64_to_cpu(key->rmap.rm_offset); | |
211 | off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK); | |
212 | key->rmap.rm_offset = cpu_to_be64(off); | |
213 | } | |
214 | ||
936ca687 DW |
215 | STATIC void |
216 | xfs_rmapbt_init_rec_from_cur( | |
217 | struct xfs_btree_cur *cur, | |
218 | union xfs_btree_rec *rec) | |
219 | { | |
220 | rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock); | |
221 | rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount); | |
222 | rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner); | |
223 | rec->rmap.rm_offset = cpu_to_be64( | |
224 | xfs_rmap_irec_offset_pack(&cur->bc_rec.r)); | |
225 | } | |
226 | ||
227 | STATIC void | |
228 | xfs_rmapbt_init_ptr_from_cur( | |
229 | struct xfs_btree_cur *cur, | |
230 | union xfs_btree_ptr *ptr) | |
231 | { | |
232 | struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp); | |
233 | ||
234 | ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno)); | |
235 | ASSERT(agf->agf_roots[cur->bc_btnum] != 0); | |
236 | ||
237 | ptr->s = agf->agf_roots[cur->bc_btnum]; | |
238 | } | |
239 | ||
4a492e72 | 240 | STATIC int64_t |
936ca687 DW |
241 | xfs_rmapbt_key_diff( |
242 | struct xfs_btree_cur *cur, | |
243 | union xfs_btree_key *key) | |
244 | { | |
245 | struct xfs_rmap_irec *rec = &cur->bc_rec.r; | |
246 | struct xfs_rmap_key *kp = &key->rmap; | |
247 | __u64 x, y; | |
4a492e72 | 248 | int64_t d; |
936ca687 | 249 | |
4a492e72 | 250 | d = (int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock; |
936ca687 DW |
251 | if (d) |
252 | return d; | |
253 | ||
254 | x = be64_to_cpu(kp->rm_owner); | |
255 | y = rec->rm_owner; | |
256 | if (x > y) | |
257 | return 1; | |
258 | else if (y > x) | |
259 | return -1; | |
260 | ||
261 | x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset)); | |
262 | y = rec->rm_offset; | |
263 | if (x > y) | |
264 | return 1; | |
265 | else if (y > x) | |
266 | return -1; | |
267 | return 0; | |
268 | } | |
269 | ||
4a492e72 | 270 | STATIC int64_t |
634b234e DW |
271 | xfs_rmapbt_diff_two_keys( |
272 | struct xfs_btree_cur *cur, | |
273 | union xfs_btree_key *k1, | |
274 | union xfs_btree_key *k2) | |
275 | { | |
276 | struct xfs_rmap_key *kp1 = &k1->rmap; | |
277 | struct xfs_rmap_key *kp2 = &k2->rmap; | |
4a492e72 | 278 | int64_t d; |
634b234e DW |
279 | __u64 x, y; |
280 | ||
4a492e72 | 281 | d = (int64_t)be32_to_cpu(kp1->rm_startblock) - |
634b234e DW |
282 | be32_to_cpu(kp2->rm_startblock); |
283 | if (d) | |
284 | return d; | |
285 | ||
286 | x = be64_to_cpu(kp1->rm_owner); | |
287 | y = be64_to_cpu(kp2->rm_owner); | |
288 | if (x > y) | |
289 | return 1; | |
290 | else if (y > x) | |
291 | return -1; | |
292 | ||
293 | x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset)); | |
294 | y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset)); | |
295 | if (x > y) | |
296 | return 1; | |
297 | else if (y > x) | |
298 | return -1; | |
299 | return 0; | |
300 | } | |
301 | ||
bc01119d | 302 | static xfs_failaddr_t |
b3a96b46 DW |
303 | xfs_rmapbt_verify( |
304 | struct xfs_buf *bp) | |
305 | { | |
306 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
307 | struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp); | |
308 | struct xfs_perag *pag = bp->b_pag; | |
bc01119d | 309 | xfs_failaddr_t fa; |
b3a96b46 DW |
310 | unsigned int level; |
311 | ||
312 | /* | |
313 | * magic number and level verification | |
314 | * | |
315 | * During growfs operations, we can't verify the exact level or owner as | |
316 | * the perag is not fully initialised and hence not attached to the | |
317 | * buffer. In this case, check against the maximum tree depth. | |
318 | * | |
319 | * Similarly, during log recovery we will have a perag structure | |
320 | * attached, but the agf information will not yet have been initialised | |
321 | * from the on disk AGF. Again, we can only check against maximum limits | |
322 | * in this case. | |
323 | */ | |
324 | if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC)) | |
bc01119d | 325 | return __this_address; |
b3a96b46 DW |
326 | |
327 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) | |
bc01119d DW |
328 | return __this_address; |
329 | fa = xfs_btree_sblock_v5hdr_verify(bp); | |
330 | if (fa) | |
331 | return fa; | |
b3a96b46 DW |
332 | |
333 | level = be16_to_cpu(block->bb_level); | |
334 | if (pag && pag->pagf_init) { | |
335 | if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi]) | |
bc01119d | 336 | return __this_address; |
b3a96b46 | 337 | } else if (level >= mp->m_rmap_maxlevels) |
bc01119d | 338 | return __this_address; |
b3a96b46 DW |
339 | |
340 | return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]); | |
341 | } | |
342 | ||
343 | static void | |
344 | xfs_rmapbt_read_verify( | |
345 | struct xfs_buf *bp) | |
346 | { | |
1e697959 DW |
347 | xfs_failaddr_t fa; |
348 | ||
b3a96b46 | 349 | if (!xfs_btree_sblock_verify_crc(bp)) |
1e697959 DW |
350 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
351 | else { | |
352 | fa = xfs_rmapbt_verify(bp); | |
353 | if (fa) | |
354 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
355 | } | |
b3a96b46 | 356 | |
7e6c95f1 | 357 | if (bp->b_error) |
b3a96b46 | 358 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
b3a96b46 DW |
359 | } |
360 | ||
361 | static void | |
362 | xfs_rmapbt_write_verify( | |
363 | struct xfs_buf *bp) | |
364 | { | |
1e697959 DW |
365 | xfs_failaddr_t fa; |
366 | ||
367 | fa = xfs_rmapbt_verify(bp); | |
368 | if (fa) { | |
b3a96b46 | 369 | trace_xfs_btree_corrupt(bp, _RET_IP_); |
1e697959 | 370 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
b3a96b46 DW |
371 | return; |
372 | } | |
373 | xfs_btree_sblock_calc_crc(bp); | |
374 | ||
375 | } | |
376 | ||
377 | const struct xfs_buf_ops xfs_rmapbt_buf_ops = { | |
378 | .name = "xfs_rmapbt", | |
379 | .verify_read = xfs_rmapbt_read_verify, | |
380 | .verify_write = xfs_rmapbt_write_verify, | |
95d9582b | 381 | .verify_struct = xfs_rmapbt_verify, |
b3a96b46 DW |
382 | }; |
383 | ||
936ca687 DW |
384 | STATIC int |
385 | xfs_rmapbt_keys_inorder( | |
386 | struct xfs_btree_cur *cur, | |
387 | union xfs_btree_key *k1, | |
388 | union xfs_btree_key *k2) | |
389 | { | |
4a492e72 DW |
390 | uint32_t x; |
391 | uint32_t y; | |
392 | uint64_t a; | |
393 | uint64_t b; | |
936ca687 DW |
394 | |
395 | x = be32_to_cpu(k1->rmap.rm_startblock); | |
396 | y = be32_to_cpu(k2->rmap.rm_startblock); | |
397 | if (x < y) | |
398 | return 1; | |
399 | else if (x > y) | |
400 | return 0; | |
401 | a = be64_to_cpu(k1->rmap.rm_owner); | |
402 | b = be64_to_cpu(k2->rmap.rm_owner); | |
403 | if (a < b) | |
404 | return 1; | |
405 | else if (a > b) | |
406 | return 0; | |
407 | a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset)); | |
408 | b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset)); | |
409 | if (a <= b) | |
410 | return 1; | |
411 | return 0; | |
412 | } | |
413 | ||
414 | STATIC int | |
415 | xfs_rmapbt_recs_inorder( | |
416 | struct xfs_btree_cur *cur, | |
417 | union xfs_btree_rec *r1, | |
418 | union xfs_btree_rec *r2) | |
419 | { | |
4a492e72 DW |
420 | uint32_t x; |
421 | uint32_t y; | |
422 | uint64_t a; | |
423 | uint64_t b; | |
936ca687 DW |
424 | |
425 | x = be32_to_cpu(r1->rmap.rm_startblock); | |
426 | y = be32_to_cpu(r2->rmap.rm_startblock); | |
427 | if (x < y) | |
428 | return 1; | |
429 | else if (x > y) | |
430 | return 0; | |
431 | a = be64_to_cpu(r1->rmap.rm_owner); | |
432 | b = be64_to_cpu(r2->rmap.rm_owner); | |
433 | if (a < b) | |
434 | return 1; | |
435 | else if (a > b) | |
436 | return 0; | |
437 | a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset)); | |
438 | b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset)); | |
439 | if (a <= b) | |
440 | return 1; | |
441 | return 0; | |
442 | } | |
936ca687 | 443 | |
b3a96b46 DW |
444 | static const struct xfs_btree_ops xfs_rmapbt_ops = { |
445 | .rec_len = sizeof(struct xfs_rmap_rec), | |
446 | .key_len = 2 * sizeof(struct xfs_rmap_key), | |
447 | ||
448 | .dup_cursor = xfs_rmapbt_dup_cursor, | |
936ca687 DW |
449 | .set_root = xfs_rmapbt_set_root, |
450 | .alloc_block = xfs_rmapbt_alloc_block, | |
451 | .free_block = xfs_rmapbt_free_block, | |
452 | .get_minrecs = xfs_rmapbt_get_minrecs, | |
453 | .get_maxrecs = xfs_rmapbt_get_maxrecs, | |
454 | .init_key_from_rec = xfs_rmapbt_init_key_from_rec, | |
634b234e | 455 | .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec, |
936ca687 DW |
456 | .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur, |
457 | .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur, | |
458 | .key_diff = xfs_rmapbt_key_diff, | |
b3a96b46 | 459 | .buf_ops = &xfs_rmapbt_buf_ops, |
634b234e | 460 | .diff_two_keys = xfs_rmapbt_diff_two_keys, |
936ca687 DW |
461 | .keys_inorder = xfs_rmapbt_keys_inorder, |
462 | .recs_inorder = xfs_rmapbt_recs_inorder, | |
b3a96b46 DW |
463 | }; |
464 | ||
465 | /* | |
466 | * Allocate a new allocation btree cursor. | |
467 | */ | |
468 | struct xfs_btree_cur * | |
469 | xfs_rmapbt_init_cursor( | |
470 | struct xfs_mount *mp, | |
471 | struct xfs_trans *tp, | |
472 | struct xfs_buf *agbp, | |
473 | xfs_agnumber_t agno) | |
474 | { | |
475 | struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp); | |
476 | struct xfs_btree_cur *cur; | |
477 | ||
478 | cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS); | |
479 | cur->bc_tp = tp; | |
480 | cur->bc_mp = mp; | |
634b234e | 481 | /* Overlapping btree; 2 keys per pointer. */ |
b3a96b46 | 482 | cur->bc_btnum = XFS_BTNUM_RMAP; |
634b234e | 483 | cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING; |
b3a96b46 DW |
484 | cur->bc_blocklog = mp->m_sb.sb_blocklog; |
485 | cur->bc_ops = &xfs_rmapbt_ops; | |
486 | cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]); | |
5d8acc46 | 487 | cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2); |
b3a96b46 DW |
488 | |
489 | cur->bc_private.a.agbp = agbp; | |
490 | cur->bc_private.a.agno = agno; | |
491 | ||
492 | return cur; | |
493 | } | |
494 | ||
495 | /* | |
496 | * Calculate number of records in an rmap btree block. | |
497 | */ | |
498 | int | |
499 | xfs_rmapbt_maxrecs( | |
500 | struct xfs_mount *mp, | |
501 | int blocklen, | |
502 | int leaf) | |
503 | { | |
504 | blocklen -= XFS_RMAP_BLOCK_LEN; | |
505 | ||
506 | if (leaf) | |
507 | return blocklen / sizeof(struct xfs_rmap_rec); | |
508 | return blocklen / | |
634b234e | 509 | (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t)); |
b3a96b46 DW |
510 | } |
511 | ||
512 | /* Compute the maximum height of an rmap btree. */ | |
513 | void | |
514 | xfs_rmapbt_compute_maxlevels( | |
515 | struct xfs_mount *mp) | |
516 | { | |
88ce0792 DW |
517 | /* |
518 | * On a non-reflink filesystem, the maximum number of rmap | |
519 | * records is the number of blocks in the AG, hence the max | |
520 | * rmapbt height is log_$maxrecs($agblocks). However, with | |
521 | * reflink each AG block can have up to 2^32 (per the refcount | |
522 | * record format) owners, which means that theoretically we | |
523 | * could face up to 2^64 rmap records. | |
524 | * | |
525 | * That effectively means that the max rmapbt height must be | |
526 | * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG | |
527 | * blocks to feed the rmapbt long before the rmapbt reaches | |
528 | * maximum height. The reflink code uses ag_resv_critical to | |
529 | * disallow reflinking when less than 10% of the per-AG metadata | |
530 | * block reservation since the fallback is a regular file copy. | |
531 | */ | |
532 | if (xfs_sb_version_hasreflink(&mp->m_sb)) | |
533 | mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS; | |
534 | else | |
535 | mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp, | |
536 | mp->m_rmap_mnr, mp->m_sb.sb_agblocks); | |
b3a96b46 | 537 | } |
02cc8b2a DW |
538 | |
539 | /* Calculate the refcount btree size for some records. */ | |
540 | xfs_extlen_t | |
541 | xfs_rmapbt_calc_size( | |
542 | struct xfs_mount *mp, | |
543 | unsigned long long len) | |
544 | { | |
545 | return xfs_btree_calc_size(mp, mp->m_rmap_mnr, len); | |
546 | } | |
547 | ||
548 | /* | |
549 | * Calculate the maximum refcount btree size. | |
550 | */ | |
551 | xfs_extlen_t | |
552 | xfs_rmapbt_max_size( | |
f21c57ed DW |
553 | struct xfs_mount *mp, |
554 | xfs_agblock_t agblocks) | |
02cc8b2a DW |
555 | { |
556 | /* Bail out if we're uninitialized, which can happen in mkfs. */ | |
557 | if (mp->m_rmap_mxr[0] == 0) | |
558 | return 0; | |
559 | ||
f21c57ed | 560 | return xfs_rmapbt_calc_size(mp, agblocks); |
02cc8b2a DW |
561 | } |
562 | ||
563 | /* | |
564 | * Figure out how many blocks to reserve and how many are used by this btree. | |
565 | */ | |
566 | int | |
567 | xfs_rmapbt_calc_reserves( | |
568 | struct xfs_mount *mp, | |
569 | xfs_agnumber_t agno, | |
570 | xfs_extlen_t *ask, | |
571 | xfs_extlen_t *used) | |
572 | { | |
573 | struct xfs_buf *agbp; | |
574 | struct xfs_agf *agf; | |
f21c57ed | 575 | xfs_agblock_t agblocks; |
02cc8b2a DW |
576 | xfs_extlen_t tree_len; |
577 | int error; | |
578 | ||
579 | if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) | |
580 | return 0; | |
581 | ||
02cc8b2a DW |
582 | error = xfs_alloc_read_agf(mp, NULL, agno, 0, &agbp); |
583 | if (error) | |
584 | return error; | |
585 | ||
586 | agf = XFS_BUF_TO_AGF(agbp); | |
f21c57ed | 587 | agblocks = be32_to_cpu(agf->agf_length); |
02cc8b2a DW |
588 | tree_len = be32_to_cpu(agf->agf_rmap_blocks); |
589 | xfs_buf_relse(agbp); | |
590 | ||
f21c57ed DW |
591 | /* Reserve 1% of the AG or enough for 1 block per record. */ |
592 | *ask += max(agblocks / 100, xfs_rmapbt_max_size(mp, agblocks)); | |
02cc8b2a DW |
593 | *used += tree_len; |
594 | ||
595 | return error; | |
596 | } |