]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_rmap_btree.c
xfs: remove the get*keys and update_keys btree ops pointers
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_rmap_btree.c
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"
32 #include "xfs_rmap.h"
33 #include "xfs_rmap_btree.h"
34 #include "xfs_trace.h"
35 #include "xfs_cksum.h"
36
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
62 static struct xfs_btree_cur *
63 xfs_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
70 STATIC void
71 xfs_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
92 STATIC int
93 xfs_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 {
99 int error;
100 xfs_agblock_t bno;
101
102 XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
103
104 /* Allocate the new block from the freelist. If we can't, give up. */
105 error = xfs_alloc_get_freelist(cur->bc_tp, cur->bc_private.a.agbp,
106 &bno, 1);
107 if (error) {
108 XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
109 return error;
110 }
111
112 trace_xfs_rmapbt_alloc_block(cur->bc_mp, cur->bc_private.a.agno,
113 bno, 1);
114 if (bno == NULLAGBLOCK) {
115 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
116 *stat = 0;
117 return 0;
118 }
119
120 xfs_extent_busy_reuse(cur->bc_mp, cur->bc_private.a.agno, bno, 1,
121 false);
122
123 xfs_trans_agbtree_delta(cur->bc_tp, 1);
124 new->s = cpu_to_be32(bno);
125
126 XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
127 *stat = 1;
128 return 0;
129 }
130
131 STATIC int
132 xfs_rmapbt_free_block(
133 struct xfs_btree_cur *cur,
134 struct xfs_buf *bp)
135 {
136 struct xfs_buf *agbp = cur->bc_private.a.agbp;
137 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
138 xfs_agblock_t bno;
139 int error;
140
141 bno = xfs_daddr_to_agbno(cur->bc_mp, XFS_BUF_ADDR(bp));
142 trace_xfs_rmapbt_free_block(cur->bc_mp, cur->bc_private.a.agno,
143 bno, 1);
144 error = xfs_alloc_put_freelist(cur->bc_tp, agbp, NULL, bno, 1);
145 if (error)
146 return error;
147
148 xfs_extent_busy_insert(cur->bc_tp, be32_to_cpu(agf->agf_seqno), bno, 1,
149 XFS_EXTENT_BUSY_SKIP_DISCARD);
150 xfs_trans_agbtree_delta(cur->bc_tp, -1);
151
152 return 0;
153 }
154
155 STATIC int
156 xfs_rmapbt_get_minrecs(
157 struct xfs_btree_cur *cur,
158 int level)
159 {
160 return cur->bc_mp->m_rmap_mnr[level != 0];
161 }
162
163 STATIC int
164 xfs_rmapbt_get_maxrecs(
165 struct xfs_btree_cur *cur,
166 int level)
167 {
168 return cur->bc_mp->m_rmap_mxr[level != 0];
169 }
170
171 STATIC void
172 xfs_rmapbt_init_key_from_rec(
173 union xfs_btree_key *key,
174 union xfs_btree_rec *rec)
175 {
176 key->rmap.rm_startblock = rec->rmap.rm_startblock;
177 key->rmap.rm_owner = rec->rmap.rm_owner;
178 key->rmap.rm_offset = rec->rmap.rm_offset;
179 }
180
181 /*
182 * The high key for a reverse mapping record can be computed by shifting
183 * the startblock and offset to the highest value that would still map
184 * to that record. In practice this means that we add blockcount-1 to
185 * the startblock for all records, and if the record is for a data/attr
186 * fork mapping, we add blockcount-1 to the offset too.
187 */
188 STATIC void
189 xfs_rmapbt_init_high_key_from_rec(
190 union xfs_btree_key *key,
191 union xfs_btree_rec *rec)
192 {
193 __uint64_t off;
194 int adj;
195
196 adj = be32_to_cpu(rec->rmap.rm_blockcount) - 1;
197
198 key->rmap.rm_startblock = rec->rmap.rm_startblock;
199 be32_add_cpu(&key->rmap.rm_startblock, adj);
200 key->rmap.rm_owner = rec->rmap.rm_owner;
201 key->rmap.rm_offset = rec->rmap.rm_offset;
202 if (XFS_RMAP_NON_INODE_OWNER(be64_to_cpu(rec->rmap.rm_owner)) ||
203 XFS_RMAP_IS_BMBT_BLOCK(be64_to_cpu(rec->rmap.rm_offset)))
204 return;
205 off = be64_to_cpu(key->rmap.rm_offset);
206 off = (XFS_RMAP_OFF(off) + adj) | (off & ~XFS_RMAP_OFF_MASK);
207 key->rmap.rm_offset = cpu_to_be64(off);
208 }
209
210 STATIC void
211 xfs_rmapbt_init_rec_from_cur(
212 struct xfs_btree_cur *cur,
213 union xfs_btree_rec *rec)
214 {
215 rec->rmap.rm_startblock = cpu_to_be32(cur->bc_rec.r.rm_startblock);
216 rec->rmap.rm_blockcount = cpu_to_be32(cur->bc_rec.r.rm_blockcount);
217 rec->rmap.rm_owner = cpu_to_be64(cur->bc_rec.r.rm_owner);
218 rec->rmap.rm_offset = cpu_to_be64(
219 xfs_rmap_irec_offset_pack(&cur->bc_rec.r));
220 }
221
222 STATIC void
223 xfs_rmapbt_init_ptr_from_cur(
224 struct xfs_btree_cur *cur,
225 union xfs_btree_ptr *ptr)
226 {
227 struct xfs_agf *agf = XFS_BUF_TO_AGF(cur->bc_private.a.agbp);
228
229 ASSERT(cur->bc_private.a.agno == be32_to_cpu(agf->agf_seqno));
230 ASSERT(agf->agf_roots[cur->bc_btnum] != 0);
231
232 ptr->s = agf->agf_roots[cur->bc_btnum];
233 }
234
235 STATIC __int64_t
236 xfs_rmapbt_key_diff(
237 struct xfs_btree_cur *cur,
238 union xfs_btree_key *key)
239 {
240 struct xfs_rmap_irec *rec = &cur->bc_rec.r;
241 struct xfs_rmap_key *kp = &key->rmap;
242 __u64 x, y;
243 __int64_t d;
244
245 d = (__int64_t)be32_to_cpu(kp->rm_startblock) - rec->rm_startblock;
246 if (d)
247 return d;
248
249 x = be64_to_cpu(kp->rm_owner);
250 y = rec->rm_owner;
251 if (x > y)
252 return 1;
253 else if (y > x)
254 return -1;
255
256 x = XFS_RMAP_OFF(be64_to_cpu(kp->rm_offset));
257 y = rec->rm_offset;
258 if (x > y)
259 return 1;
260 else if (y > x)
261 return -1;
262 return 0;
263 }
264
265 STATIC __int64_t
266 xfs_rmapbt_diff_two_keys(
267 struct xfs_btree_cur *cur,
268 union xfs_btree_key *k1,
269 union xfs_btree_key *k2)
270 {
271 struct xfs_rmap_key *kp1 = &k1->rmap;
272 struct xfs_rmap_key *kp2 = &k2->rmap;
273 __int64_t d;
274 __u64 x, y;
275
276 d = (__int64_t)be32_to_cpu(kp1->rm_startblock) -
277 be32_to_cpu(kp2->rm_startblock);
278 if (d)
279 return d;
280
281 x = be64_to_cpu(kp1->rm_owner);
282 y = be64_to_cpu(kp2->rm_owner);
283 if (x > y)
284 return 1;
285 else if (y > x)
286 return -1;
287
288 x = XFS_RMAP_OFF(be64_to_cpu(kp1->rm_offset));
289 y = XFS_RMAP_OFF(be64_to_cpu(kp2->rm_offset));
290 if (x > y)
291 return 1;
292 else if (y > x)
293 return -1;
294 return 0;
295 }
296
297 static bool
298 xfs_rmapbt_verify(
299 struct xfs_buf *bp)
300 {
301 struct xfs_mount *mp = bp->b_target->bt_mount;
302 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
303 struct xfs_perag *pag = bp->b_pag;
304 unsigned int level;
305
306 /*
307 * magic number and level verification
308 *
309 * During growfs operations, we can't verify the exact level or owner as
310 * the perag is not fully initialised and hence not attached to the
311 * buffer. In this case, check against the maximum tree depth.
312 *
313 * Similarly, during log recovery we will have a perag structure
314 * attached, but the agf information will not yet have been initialised
315 * from the on disk AGF. Again, we can only check against maximum limits
316 * in this case.
317 */
318 if (block->bb_magic != cpu_to_be32(XFS_RMAP_CRC_MAGIC))
319 return false;
320
321 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
322 return false;
323 if (!xfs_btree_sblock_v5hdr_verify(bp))
324 return false;
325
326 level = be16_to_cpu(block->bb_level);
327 if (pag && pag->pagf_init) {
328 if (level >= pag->pagf_levels[XFS_BTNUM_RMAPi])
329 return false;
330 } else if (level >= mp->m_rmap_maxlevels)
331 return false;
332
333 return xfs_btree_sblock_verify(bp, mp->m_rmap_mxr[level != 0]);
334 }
335
336 static void
337 xfs_rmapbt_read_verify(
338 struct xfs_buf *bp)
339 {
340 if (!xfs_btree_sblock_verify_crc(bp))
341 xfs_buf_ioerror(bp, -EFSBADCRC);
342 else if (!xfs_rmapbt_verify(bp))
343 xfs_buf_ioerror(bp, -EFSCORRUPTED);
344
345 if (bp->b_error) {
346 trace_xfs_btree_corrupt(bp, _RET_IP_);
347 xfs_verifier_error(bp);
348 }
349 }
350
351 static void
352 xfs_rmapbt_write_verify(
353 struct xfs_buf *bp)
354 {
355 if (!xfs_rmapbt_verify(bp)) {
356 trace_xfs_btree_corrupt(bp, _RET_IP_);
357 xfs_buf_ioerror(bp, -EFSCORRUPTED);
358 xfs_verifier_error(bp);
359 return;
360 }
361 xfs_btree_sblock_calc_crc(bp);
362
363 }
364
365 const struct xfs_buf_ops xfs_rmapbt_buf_ops = {
366 .name = "xfs_rmapbt",
367 .verify_read = xfs_rmapbt_read_verify,
368 .verify_write = xfs_rmapbt_write_verify,
369 };
370
371 #if defined(DEBUG) || defined(XFS_WARN)
372 STATIC int
373 xfs_rmapbt_keys_inorder(
374 struct xfs_btree_cur *cur,
375 union xfs_btree_key *k1,
376 union xfs_btree_key *k2)
377 {
378 __uint32_t x;
379 __uint32_t y;
380 __uint64_t a;
381 __uint64_t b;
382
383 x = be32_to_cpu(k1->rmap.rm_startblock);
384 y = be32_to_cpu(k2->rmap.rm_startblock);
385 if (x < y)
386 return 1;
387 else if (x > y)
388 return 0;
389 a = be64_to_cpu(k1->rmap.rm_owner);
390 b = be64_to_cpu(k2->rmap.rm_owner);
391 if (a < b)
392 return 1;
393 else if (a > b)
394 return 0;
395 a = XFS_RMAP_OFF(be64_to_cpu(k1->rmap.rm_offset));
396 b = XFS_RMAP_OFF(be64_to_cpu(k2->rmap.rm_offset));
397 if (a <= b)
398 return 1;
399 return 0;
400 }
401
402 STATIC int
403 xfs_rmapbt_recs_inorder(
404 struct xfs_btree_cur *cur,
405 union xfs_btree_rec *r1,
406 union xfs_btree_rec *r2)
407 {
408 __uint32_t x;
409 __uint32_t y;
410 __uint64_t a;
411 __uint64_t b;
412
413 x = be32_to_cpu(r1->rmap.rm_startblock);
414 y = be32_to_cpu(r2->rmap.rm_startblock);
415 if (x < y)
416 return 1;
417 else if (x > y)
418 return 0;
419 a = be64_to_cpu(r1->rmap.rm_owner);
420 b = be64_to_cpu(r2->rmap.rm_owner);
421 if (a < b)
422 return 1;
423 else if (a > b)
424 return 0;
425 a = XFS_RMAP_OFF(be64_to_cpu(r1->rmap.rm_offset));
426 b = XFS_RMAP_OFF(be64_to_cpu(r2->rmap.rm_offset));
427 if (a <= b)
428 return 1;
429 return 0;
430 }
431 #endif /* DEBUG */
432
433 static const struct xfs_btree_ops xfs_rmapbt_ops = {
434 .rec_len = sizeof(struct xfs_rmap_rec),
435 .key_len = 2 * sizeof(struct xfs_rmap_key),
436
437 .dup_cursor = xfs_rmapbt_dup_cursor,
438 .set_root = xfs_rmapbt_set_root,
439 .alloc_block = xfs_rmapbt_alloc_block,
440 .free_block = xfs_rmapbt_free_block,
441 .get_minrecs = xfs_rmapbt_get_minrecs,
442 .get_maxrecs = xfs_rmapbt_get_maxrecs,
443 .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
444 .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
445 .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
446 .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur,
447 .key_diff = xfs_rmapbt_key_diff,
448 .buf_ops = &xfs_rmapbt_buf_ops,
449 .diff_two_keys = xfs_rmapbt_diff_two_keys,
450 #if defined(DEBUG) || defined(XFS_WARN)
451 .keys_inorder = xfs_rmapbt_keys_inorder,
452 .recs_inorder = xfs_rmapbt_recs_inorder,
453 #endif
454 };
455
456 /*
457 * Allocate a new allocation btree cursor.
458 */
459 struct xfs_btree_cur *
460 xfs_rmapbt_init_cursor(
461 struct xfs_mount *mp,
462 struct xfs_trans *tp,
463 struct xfs_buf *agbp,
464 xfs_agnumber_t agno)
465 {
466 struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
467 struct xfs_btree_cur *cur;
468
469 cur = kmem_zone_zalloc(xfs_btree_cur_zone, KM_NOFS);
470 cur->bc_tp = tp;
471 cur->bc_mp = mp;
472 /* Overlapping btree; 2 keys per pointer. */
473 cur->bc_btnum = XFS_BTNUM_RMAP;
474 cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING;
475 cur->bc_blocklog = mp->m_sb.sb_blocklog;
476 cur->bc_ops = &xfs_rmapbt_ops;
477 cur->bc_nlevels = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
478
479 cur->bc_private.a.agbp = agbp;
480 cur->bc_private.a.agno = agno;
481
482 return cur;
483 }
484
485 /*
486 * Calculate number of records in an rmap btree block.
487 */
488 int
489 xfs_rmapbt_maxrecs(
490 struct xfs_mount *mp,
491 int blocklen,
492 int leaf)
493 {
494 blocklen -= XFS_RMAP_BLOCK_LEN;
495
496 if (leaf)
497 return blocklen / sizeof(struct xfs_rmap_rec);
498 return blocklen /
499 (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
500 }
501
502 /* Compute the maximum height of an rmap btree. */
503 void
504 xfs_rmapbt_compute_maxlevels(
505 struct xfs_mount *mp)
506 {
507 mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(mp,
508 mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
509 }