Commit | Line | Data |
---|---|---|
07a1006a KO |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef _BCACHEFS_BKEY_BUF_H | |
3 | #define _BCACHEFS_BKEY_BUF_H | |
4 | ||
5 | #include "bcachefs.h" | |
6 | ||
7 | struct bkey_buf { | |
8 | struct bkey_i *k; | |
9 | u64 onstack[12]; | |
10 | }; | |
11 | ||
12 | static inline void bch2_bkey_buf_realloc(struct bkey_buf *s, | |
13 | struct bch_fs *c, unsigned u64s) | |
14 | { | |
15 | if (s->k == (void *) s->onstack && | |
16 | u64s > ARRAY_SIZE(s->onstack)) { | |
17 | s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS); | |
18 | memcpy(s->k, s->onstack, sizeof(s->onstack)); | |
19 | } | |
20 | } | |
21 | ||
22 | static inline void bch2_bkey_buf_reassemble(struct bkey_buf *s, | |
23 | struct bch_fs *c, | |
24 | struct bkey_s_c k) | |
25 | { | |
26 | bch2_bkey_buf_realloc(s, c, k.k->u64s); | |
27 | bkey_reassemble(s->k, k); | |
28 | } | |
29 | ||
30 | static inline void bch2_bkey_buf_copy(struct bkey_buf *s, | |
31 | struct bch_fs *c, | |
32 | struct bkey_i *src) | |
33 | { | |
34 | bch2_bkey_buf_realloc(s, c, src->k.u64s); | |
35 | bkey_copy(s->k, src); | |
36 | } | |
37 | ||
38 | static inline void bch2_bkey_buf_unpack(struct bkey_buf *s, | |
39 | struct bch_fs *c, | |
40 | struct btree *b, | |
41 | struct bkey_packed *src) | |
42 | { | |
43 | bch2_bkey_buf_realloc(s, c, BKEY_U64s + | |
44 | bkeyp_val_u64s(&b->format, src)); | |
45 | bch2_bkey_unpack(b, s->k, src); | |
46 | } | |
47 | ||
48 | static inline void bch2_bkey_buf_init(struct bkey_buf *s) | |
49 | { | |
50 | s->k = (void *) s->onstack; | |
51 | } | |
52 | ||
53 | static inline void bch2_bkey_buf_exit(struct bkey_buf *s, struct bch_fs *c) | |
54 | { | |
55 | if (s->k != (void *) s->onstack) | |
56 | mempool_free(s->k, &c->large_bkey_pool); | |
57 | s->k = NULL; | |
58 | } | |
59 | ||
60 | #endif /* _BCACHEFS_BKEY_BUF_H */ |