]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_rmap_btree.c
xfs: create a separate cow extent size hint for the allocator
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_rmap_btree.c
CommitLineData
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"
36
936ca687
DW
37/*
38 * Reverse map btree.
39 *
40 * This is a per-ag tree used to track the owner(s) of a given extent. With
41 * reflink it is possible for there to be multiple owners, which is a departure
42 * from classic XFS. Owner records for data extents are inserted when the
43 * extent is mapped and removed when an extent is unmapped. Owner records for
44 * all other block types (i.e. metadata) are inserted when an extent is
45 * allocated and removed when an extent is freed. There can only be one owner
46 * of a metadata extent, usually an inode or some other metadata structure like
47 * an AG btree.
48 *
49 * The rmap btree is part of the free space management, so blocks for the tree
50 * are sourced from the agfl. Hence we need transaction reservation support for
51 * this tree so that the freelist is always large enough. This also impacts on
52 * the minimum space we need to leave free in the AG.
53 *
54 * The tree is ordered by [ag block, owner, offset]. This is a large key size,
55 * but it is the only way to enforce unique keys when a block can be owned by
56 * multiple files at any offset. There's no need to order/search by extent
57 * size for online updating/management of the tree. It is intended that most
58 * reverse lookups will be to find the owner(s) of a particular block, or to
59 * try to recover tree and file data from corrupt primary metadata.
60 */
61
b3a96b46
DW
62static struct xfs_btree_cur *
63xfs_rmapbt_dup_cursor(
64 struct xfs_btree_cur *cur)
65{
66 return xfs_rmapbt_init_cursor(cur->bc_mp, cur->bc_tp,
67 cur->bc_private.a.agbp, cur->bc_private.a.agno);
68}
69
936ca687
DW
70STATIC void
71xfs_rmapbt_set_root(
72 struct xfs_btree_cur *cur,
73 union xfs_btree_ptr *ptr,
74 int inc)
75{
76 struct xfs_buf *agbp = cur->bc_private.a.agbp;
77 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
78 xfs_agnumber_t seqno = be32_to_cpu(agf->agf_seqno);
79 int btnum = cur->bc_btnum;
80 struct xfs_perag *pag = xfs_perag_get(cur->bc_mp, seqno);
81
82 ASSERT(ptr->s != 0);
83
84 agf->agf_roots[btnum] = ptr->s;
85 be32_add_cpu(&agf->agf_levels[btnum], inc);
86 pag->pagf_levels[btnum] += inc;
87 xfs_perag_put(pag);
88
89 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_ROOTS | XFS_AGF_LEVELS);
90}
91
92STATIC int
93xfs_rmapbt_alloc_block(
94 struct xfs_btree_cur *cur,
95 union xfs_btree_ptr *start,
96 union xfs_btree_ptr *new,
97 int *stat)
98{
8511b71a
DW
99 struct xfs_buf *agbp = cur->bc_private.a.agbp;
100 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
936ca687
DW
101 int error;
102 xfs_agblock_t bno;
103
104 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
105
106 /* Allocate the new block from the freelist. If we can't, give up. */
107 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
108 &bno, 1);
109 if (error) {
110 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
111 return error;
112 }
113
114 trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
115 bno, 1);
116 if (bno == NULLAGBLOCK) {
117 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
118 *stat = 0;
119 return 0;
120 }
121
122 xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
123 false);
124
125 xfs_trans_agbtree_delta(cur->bc_tp, 1);
126 new->s = cpu_to_be32(bno);
8511b71a
DW
127 be32_add_cpu(&agf->agf_rmap_blocks, 1);
128 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
936ca687
DW
129
130 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
131 *stat = 1;
132 return 0;
133}
134
135STATIC int
136xfs_rmapbt_free_block(
137 struct xfs_btree_cur *cur,
138 struct xfs_buf *bp)
139{
140 struct xfs_buf *agbp = cur->bc_private.a.agbp;
141 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
142 xfs_agblock_t bno;
143 int error;
144
145 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
146 trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
147 bno, 1);
8511b71a
DW
148 be32_add_cpu(&agf->agf_rmap_blocks, -1);
149 xfs_alloc_log_agf(cur->bc_tp, agbp, XFS_AGF_RMAP_BLOCKS);
936ca687
DW
150 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
151 if (error)
152 return error;
153
154 xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
155 XFS_EXTENT_BUSY_SKIP_DISCARD);
156 xfs_trans_agbtree_delta(cur->bc_tp, -1);
157
158 return 0;
159}
160
161STATIC int
162xfs_rmapbt_get_minrecs(
163 struct xfs_btree_cur *cur,
164 int level)
165{
166 return cur->bc_mp->m_rmap_mnr[level != 0];
167}
168
169STATIC int
170xfs_rmapbt_get_maxrecs(
171 struct xfs_btree_cur *cur,
172 int level)
173{
174 return cur->bc_mp->m_rmap_mxr[level != 0];
175}
176
177STATIC void
178xfs_rmapbt_init_key_from_rec(
179 union xfs_btree_key *key,
180 union xfs_btree_rec *rec)
181{
182 key->rmap.rm_startblock = rec->rmap.rm_startblock;
183 key->rmap.rm_owner = rec->rmap.rm_owner;
184 key->rmap.rm_offset = rec->rmap.rm_offset;
185}
186
634b234e
DW
187/*
188 * The high key for a reverse mapping record can be computed by shifting
189 * the startblock and offset to the highest value that would still map
190 * to that record. In practice this means that we add blockcount-1 to
191 * the startblock for all records, and if the record is for a data/attr
192 * fork mapping, we add blockcount-1 to the offset too.
193 */
194STATIC void
195xfs_rmapbt_init_high_key_from_rec(
196 union xfs_btree_key *key,
197 union xfs_btree_rec *rec)
198{
199 __uint64_t off;
200 int adj;
201
202 adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
203
204 key->rmap.rm_startblock = rec->rmap.rm_startblock;
205 be32_add_cpu(&key->rmap.rm_startblock, adj);
206 key->rmap.rm_owner = rec->rmap.rm_owner;
207 key->rmap.rm_offset = rec->rmap.rm_offset;
208 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
209 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
210 return;
211 off = be64_to_cpu(key->rmap.rm_offset);
212 off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
213 key->rmap.rm_offset = cpu_to_be64(off);
214}
215
936ca687
DW
216STATIC void
217xfs_rmapbt_init_rec_from_cur(
218 struct xfs_btree_cur *cur,
219 union xfs_btree_rec *rec)
220{
221 rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
222 rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
223 rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
224 rec->rmap.rm_offset = cpu_to_be64(
225 xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
226}
227
228STATIC void
229xfs_rmapbt_init_ptr_from_cur(
230 struct xfs_btree_cur *cur,
231 union xfs_btree_ptr *ptr)
232{
233 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
234
235 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
236 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
237
238 ptr->s = agf->agf_roots[cur->bc_btnum];
239}
240
241STATIC __int64_t
242xfs_rmapbt_key_diff(
243 struct xfs_btree_cur *cur,
244 union xfs_btree_key *key)
245{
246 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
247 struct xfs_rmap_key *kp = &key->rmap;
248 __u64 x, y;
249 __int64_t d;
250
251 d = (__int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
252 if (d)
253 return d;
254
255 x = be64_to_cpu(kp->rm_owner);
256 y = rec->rm_owner;
257 if (x > y)
258 return 1;
259 else if (y > x)
260 return -1;
261
262 x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
263 y = rec->rm_offset;
264 if (x > y)
265 return 1;
266 else if (y > x)
267 return -1;
268 return 0;
269}
270
634b234e
DW
271STATIC __int64_t
272xfs_rmapbt_diff_two_keys(
273 struct xfs_btree_cur *cur,
274 union xfs_btree_key *k1,
275 union xfs_btree_key *k2)
276{
277 struct xfs_rmap_key *kp1 = &k1->rmap;
278 struct xfs_rmap_key *kp2 = &k2->rmap;
279 __int64_t d;
280 __u64 x, y;
281
282 d = (__int64_t)be32_to_cpu(kp1->rm_startblock) -
283 be32_to_cpu(kp2->rm_startblock);
284 if (d)
285 return d;
286
287 x = be64_to_cpu(kp1->rm_owner);
288 y = be64_to_cpu(kp2->rm_owner);
289 if (x > y)
290 return 1;
291 else if (y > x)
292 return -1;
293
294 x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
295 y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
296 if (x > y)
297 return 1;
298 else if (y > x)
299 return -1;
300 return 0;
301}
302
b3a96b46
DW
303static bool
304xfs_rmapbt_verify(
305 struct xfs_buf *bp)
306{
307 struct xfs_mount *mp = bp->b_target->bt_mount;
308 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
309 struct xfs_perag *pag = bp->b_pag;
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))
325 return false;
326
327 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
328 return false;
329 if (!xfs_btree_sblock_v5hdr_verify(bp))
330 return false;
331
332 level = be16_to_cpu(block->bb_level);
333 if (pag && pag->pagf_init) {
334 if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
335 return false;
336 } else if (level >= mp->m_rmap_maxlevels)
337 return false;
338
339 return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
340}
341
342static void
343xfs_rmapbt_read_verify(
344 struct xfs_buf *bp)
345{
346 if (!xfs_btree_sblock_verify_crc(bp))
347 xfs_buf_ioerror(bp, -EFSBADCRC);
348 else if (!xfs_rmapbt_verify(bp))
349 xfs_buf_ioerror(bp, -EFSCORRUPTED);
350
351 if (bp->b_error) {
352 trace_xfs_btree_corrupt(bp, _RET_IP_);
353 xfs_verifier_error(bp);
354 }
355}
356
357static void
358xfs_rmapbt_write_verify(
359 struct xfs_buf *bp)
360{
361 if (!xfs_rmapbt_verify(bp)) {
362 trace_xfs_btree_corrupt(bp, _RET_IP_);
363 xfs_buf_ioerror(bp, -EFSCORRUPTED);
364 xfs_verifier_error(bp);
365 return;
366 }
367 xfs_btree_sblock_calc_crc(bp);
368
369}
370
371const 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#if defined(DEBUG) || defined(XFS_WARN)
378STATIC int
379xfs_rmapbt_keys_inorder(
380 struct xfs_btree_cur *cur,
381 union xfs_btree_key *k1,
382 union xfs_btree_key *k2)
383{
384 __uint32_t x;
385 __uint32_t y;
386 __uint64_t a;
387 __uint64_t b;
388
389 x = be32_to_cpu(k1->rmap.rm_startblock);
390 y = be32_to_cpu(k2->rmap.rm_startblock);
391 if (x < y)
392 return 1;
393 else if (x > y)
394 return 0;
395 a = be64_to_cpu(k1->rmap.rm_owner);
396 b = be64_to_cpu(k2->rmap.rm_owner);
397 if (a < b)
398 return 1;
399 else if (a > b)
400 return 0;
401 a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
402 b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
403 if (a <= b)
404 return 1;
405 return 0;
406}
407
408STATIC int
409xfs_rmapbt_recs_inorder(
410 struct xfs_btree_cur *cur,
411 union xfs_btree_rec *r1,
412 union xfs_btree_rec *r2)
413{
414 __uint32_t x;
415 __uint32_t y;
416 __uint64_t a;
417 __uint64_t b;
418
419 x = be32_to_cpu(r1->rmap.rm_startblock);
420 y = be32_to_cpu(r2->rmap.rm_startblock);
421 if (x < y)
422 return 1;
423 else if (x > y)
424 return 0;
425 a = be64_to_cpu(r1->rmap.rm_owner);
426 b = be64_to_cpu(r2->rmap.rm_owner);
427 if (a < b)
428 return 1;
429 else if (a > b)
430 return 0;
431 a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
432 b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
433 if (a <= b)
434 return 1;
435 return 0;
436}
437#endif /* DEBUG */
438
b3a96b46
DW
439static const struct xfs_btree_ops xfs_rmapbt_ops = {
440 .rec_len = sizeof(struct xfs_rmap_rec),
441 .key_len = 2 * sizeof(struct xfs_rmap_key),
442
443 .dup_cursor = xfs_rmapbt_dup_cursor,
936ca687
DW
444 .set_root = xfs_rmapbt_set_root,
445 .alloc_block = xfs_rmapbt_alloc_block,
446 .free_block = xfs_rmapbt_free_block,
447 .get_minrecs = xfs_rmapbt_get_minrecs,
448 .get_maxrecs = xfs_rmapbt_get_maxrecs,
449 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
634b234e 450 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
936ca687
DW
451 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
452 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
453 .key_diff = xfs_rmapbt_key_diff,
b3a96b46 454 .buf_ops = &xfs_rmapbt_buf_ops,
634b234e 455 .diff_two_keys = xfs_rmapbt_diff_two_keys,
936ca687
DW
456#if defined(DEBUG) || defined(XFS_WARN)
457 .keys_inorder = xfs_rmapbt_keys_inorder,
458 .recs_inorder = xfs_rmapbt_recs_inorder,
459#endif
b3a96b46
DW
460};
461
462/*
463 * Allocate a new allocation btree cursor.
464 */
465struct xfs_btree_cur *
466xfs_rmapbt_init_cursor(
467 struct xfs_mount *mp,
468 struct xfs_trans *tp,
469 struct xfs_buf *agbp,
470 xfs_agnumber_t agno)
471{
472 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
473 struct xfs_btree_cur *cur;
474
475 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
476 cur->bc_tp = tp;
477 cur->bc_mp = mp;
634b234e 478 /* Overlapping btree; 2 keys per pointer. */
b3a96b46 479 cur->bc_btnum = XFS_BTNUM_RMAP;
634b234e 480 cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
b3a96b46
DW
481 cur->bc_blocklog = mp->m_sb.sb_blocklog;
482 cur->bc_ops = &xfs_rmapbt_ops;
483 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
484
485 cur->bc_private.a.agbp = agbp;
486 cur->bc_private.a.agno = agno;
487
488 return cur;
489}
490
491/*
492 * Calculate number of records in an rmap btree block.
493 */
494int
495xfs_rmapbt_maxrecs(
496 struct xfs_mount *mp,
497 int blocklen,
498 int leaf)
499{
500 blocklen -= XFS_RMAP_BLOCK_LEN;
501
502 if (leaf)
503 return blocklen / sizeof(struct xfs_rmap_rec);
504 return blocklen /
634b234e 505 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
b3a96b46
DW
506}
507
508/* Compute the maximum height of an rmap btree. */
509void
510xfs_rmapbt_compute_maxlevels(
511 struct xfs_mount *mp)
512{
88ce0792
DW
513 /*
514 * On a non-reflink filesystem, the maximum number of rmap
515 * records is the number of blocks in the AG, hence the max
516 * rmapbt height is log_$maxrecs($agblocks). However, with
517 * reflink each AG block can have up to 2^32 (per the refcount
518 * record format) owners, which means that theoretically we
519 * could face up to 2^64 rmap records.
520 *
521 * That effectively means that the max rmapbt height must be
522 * XFS_BTREE_MAXLEVELS. "Fortunately" we'll run out of AG
523 * blocks to feed the rmapbt long before the rmapbt reaches
524 * maximum height. The reflink code uses ag_resv_critical to
525 * disallow reflinking when less than 10% of the per-AG metadata
526 * block reservation since the fallback is a regular file copy.
527 */
528 if (xfs_sb_version_hasreflink(&mp->m_sb))
529 mp->m_rmap_maxlevels = XFS_BTREE_MAXLEVELS;
530 else
531 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
532 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
b3a96b46 533}