Linux 6.13-rc4
[linux.git] / fs / bcachefs / bkey_buf.h
bloba30c4ae8eb369db29c3a5d1333b418b5972efbac
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BKEY_BUF_H
3 #define _BCACHEFS_BKEY_BUF_H
5 #include "bcachefs.h"
6 #include "bkey.h"
8 struct bkey_buf {
9 struct bkey_i *k;
10 u64 onstack[12];
13 static inline void bch2_bkey_buf_realloc(struct bkey_buf *s,
14 struct bch_fs *c, unsigned u64s)
16 if (s->k == (void *) s->onstack &&
17 u64s > ARRAY_SIZE(s->onstack)) {
18 s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS);
19 memcpy(s->k, s->onstack, sizeof(s->onstack));
23 static inline void bch2_bkey_buf_reassemble(struct bkey_buf *s,
24 struct bch_fs *c,
25 struct bkey_s_c k)
27 bch2_bkey_buf_realloc(s, c, k.k->u64s);
28 bkey_reassemble(s->k, k);
31 static inline void bch2_bkey_buf_copy(struct bkey_buf *s,
32 struct bch_fs *c,
33 struct bkey_i *src)
35 bch2_bkey_buf_realloc(s, c, src->k.u64s);
36 bkey_copy(s->k, src);
39 static inline void bch2_bkey_buf_unpack(struct bkey_buf *s,
40 struct bch_fs *c,
41 struct btree *b,
42 struct bkey_packed *src)
44 bch2_bkey_buf_realloc(s, c, BKEY_U64s +
45 bkeyp_val_u64s(&b->format, src));
46 bch2_bkey_unpack(b, s->k, src);
49 static inline void bch2_bkey_buf_init(struct bkey_buf *s)
51 s->k = (void *) s->onstack;
54 static inline void bch2_bkey_buf_exit(struct bkey_buf *s, struct bch_fs *c)
56 if (s->k != (void *) s->onstack)
57 mempool_free(s->k, &c->large_bkey_pool);
58 s->k = NULL;
61 #endif /* _BCACHEFS_BKEY_BUF_H */