summaryrefslogtreecommitdiff
path: root/fs/bcachefs/bkey_buf.h
blob: 0d7c67a959af14baf8efa98be0ea04150f9137ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_BKEY_BUF_H
#define _BCACHEFS_BKEY_BUF_H

#include "bcachefs.h"

struct bkey_buf {
	struct bkey_i	*k;
	u64		onstack[12];
};

static inline void bch2_bkey_buf_realloc(struct bkey_buf *s,
					 struct bch_fs *c, unsigned u64s)
{
	if (s->k == (void *) s->onstack &&
	    u64s > ARRAY_SIZE(s->onstack)) {
		s->k = mempool_alloc(&c->large_bkey_pool, GFP_NOFS);
		memcpy(s->k, s->onstack, sizeof(s->onstack));
	}
}

static inline void bch2_bkey_buf_reassemble(struct bkey_buf *s,
					    struct bch_fs *c,
					    struct bkey_s_c k)
{
	bch2_bkey_buf_realloc(s, c, k.k->u64s);
	bkey_reassemble(s->k, k);
}

static inline void bch2_bkey_buf_copy(struct bkey_buf *s,
				      struct bch_fs *c,
				      struct bkey_i *src)
{
	bch2_bkey_buf_realloc(s, c, src->k.u64s);
	bkey_copy(s->k, src);
}

static inline void bch2_bkey_buf_unpack(struct bkey_buf *s,
					struct bch_fs *c,
					struct btree *b,
					struct bkey_packed *src)
{
	bch2_bkey_buf_realloc(s, c, BKEY_U64s +
			      bkeyp_val_u64s(&b->format, src));
	bch2_bkey_unpack(b, s->k, src);
}

static inline void bch2_bkey_buf_init(struct bkey_buf *s)
{
	s->k = (void *) s->onstack;
}

static inline void bch2_bkey_buf_exit(struct bkey_buf *s, struct bch_fs *c)
{
	if (s->k != (void *) s->onstack)
		mempool_free(s->k, &c->large_bkey_pool);
	s->k = NULL;
}

#endif /* _BCACHEFS_BKEY_BUF_H */