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