1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_KEYLIST_H
3 #define _BCACHEFS_KEYLIST_H
4
5 #include "keylist_types.h"
6
7 int bch2_keylist_realloc(struct keylist *, u64 *, size_t, size_t);
8 void bch2_keylist_pop_front(struct keylist *);
9
bch2_keylist_init(struct keylist * l,u64 * inline_keys)10 static inline void bch2_keylist_init(struct keylist *l, u64 *inline_keys)
11 {
12 l->top_p = l->keys_p = inline_keys;
13 }
14
bch2_keylist_free(struct keylist * l,u64 * inline_keys)15 static inline void bch2_keylist_free(struct keylist *l, u64 *inline_keys)
16 {
17 if (l->keys_p != inline_keys)
18 kfree(l->keys_p);
19 }
20
bch2_keylist_push(struct keylist * l)21 static inline void bch2_keylist_push(struct keylist *l)
22 {
23 l->top = bkey_next(l->top);
24 }
25
bch2_keylist_add(struct keylist * l,const struct bkey_i * k)26 static inline void bch2_keylist_add(struct keylist *l, const struct bkey_i *k)
27 {
28 bkey_copy(l->top, k);
29 bch2_keylist_push(l);
30 }
31
bch2_keylist_empty(struct keylist * l)32 static inline bool bch2_keylist_empty(struct keylist *l)
33 {
34 return l->top == l->keys;
35 }
36
bch2_keylist_u64s(struct keylist * l)37 static inline size_t bch2_keylist_u64s(struct keylist *l)
38 {
39 return l->top_p - l->keys_p;
40 }
41
bch2_keylist_bytes(struct keylist * l)42 static inline size_t bch2_keylist_bytes(struct keylist *l)
43 {
44 return bch2_keylist_u64s(l) * sizeof(u64);
45 }
46
bch2_keylist_front(struct keylist * l)47 static inline struct bkey_i *bch2_keylist_front(struct keylist *l)
48 {
49 return l->keys;
50 }
51
52 #define for_each_keylist_key(_keylist, _k) \
53 for (struct bkey_i *_k = (_keylist)->keys; \
54 _k != (_keylist)->top; \
55 _k = bkey_next(_k))
56
keylist_sectors(struct keylist * keys)57 static inline u64 keylist_sectors(struct keylist *keys)
58 {
59 u64 ret = 0;
60
61 for_each_keylist_key(keys, k)
62 ret += k->k.size;
63 return ret;
64 }
65
66 #ifdef CONFIG_BCACHEFS_DEBUG
67 void bch2_verify_keylist_sorted(struct keylist *);
68 #else
bch2_verify_keylist_sorted(struct keylist * l)69 static inline void bch2_verify_keylist_sorted(struct keylist *l) {}
70 #endif
71
72 #endif /* _BCACHEFS_KEYLIST_H */
73