1#ifndef _BCACHE_BTREE_H
2#define _BCACHE_BTREE_H
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101#include "bset.h"
102#include "debug.h"
103
104struct btree_write {
105 atomic_t *journal;
106
107
108
109
110
111
112
113 int prio_blocked;
114};
115
116struct btree {
117
118 struct hlist_node hash;
119
120
121 BKEY_PADDED(key);
122
123
124 unsigned long accessed;
125 unsigned long seq;
126 struct rw_semaphore lock;
127 struct cache_set *c;
128 struct btree *parent;
129
130 struct mutex write_lock;
131
132 unsigned long flags;
133 uint16_t written;
134 uint8_t level;
135
136 struct btree_keys keys;
137
138
139 struct closure io;
140 struct semaphore io_mutex;
141
142 struct list_head list;
143 struct delayed_work work;
144
145 struct btree_write writes[2];
146 struct bio *bio;
147};
148
149#define BTREE_FLAG(flag) \
150static inline bool btree_node_ ## flag(struct btree *b) \
151{ return test_bit(BTREE_NODE_ ## flag, &b->flags); } \
152 \
153static inline void set_btree_node_ ## flag(struct btree *b) \
154{ set_bit(BTREE_NODE_ ## flag, &b->flags); } \
155
156enum btree_flags {
157 BTREE_NODE_io_error,
158 BTREE_NODE_dirty,
159 BTREE_NODE_write_idx,
160};
161
162BTREE_FLAG(io_error);
163BTREE_FLAG(dirty);
164BTREE_FLAG(write_idx);
165
166static inline struct btree_write *btree_current_write(struct btree *b)
167{
168 return b->writes + btree_node_write_idx(b);
169}
170
171static inline struct btree_write *btree_prev_write(struct btree *b)
172{
173 return b->writes + (btree_node_write_idx(b) ^ 1);
174}
175
176static inline struct bset *btree_bset_first(struct btree *b)
177{
178 return b->keys.set->data;
179}
180
181static inline struct bset *btree_bset_last(struct btree *b)
182{
183 return bset_tree_last(&b->keys)->data;
184}
185
186static inline unsigned bset_block_offset(struct btree *b, struct bset *i)
187{
188 return bset_sector_offset(&b->keys, i) >> b->c->block_bits;
189}
190
191static inline void set_gc_sectors(struct cache_set *c)
192{
193 atomic_set(&c->sectors_to_gc, c->sb.bucket_size * c->nbuckets / 16);
194}
195
196void bkey_put(struct cache_set *c, struct bkey *k);
197
198
199
200#define for_each_cached_btree(b, c, iter) \
201 for (iter = 0; \
202 iter < ARRAY_SIZE((c)->bucket_hash); \
203 iter++) \
204 hlist_for_each_entry_rcu((b), (c)->bucket_hash + iter, hash)
205
206
207
208struct btree_op {
209
210 wait_queue_t wait;
211
212
213 short lock;
214
215 unsigned insert_collision:1;
216};
217
218static inline void bch_btree_op_init(struct btree_op *op, int write_lock_level)
219{
220 memset(op, 0, sizeof(struct btree_op));
221 init_wait(&op->wait);
222 op->lock = write_lock_level;
223}
224
225static inline void rw_lock(bool w, struct btree *b, int level)
226{
227 w ? down_write_nested(&b->lock, level + 1)
228 : down_read_nested(&b->lock, level + 1);
229 if (w)
230 b->seq++;
231}
232
233static inline void rw_unlock(bool w, struct btree *b)
234{
235 if (w)
236 b->seq++;
237 (w ? up_write : up_read)(&b->lock);
238}
239
240void bch_btree_node_read_done(struct btree *);
241void __bch_btree_node_write(struct btree *, struct closure *);
242void bch_btree_node_write(struct btree *, struct closure *);
243
244void bch_btree_set_root(struct btree *);
245struct btree *bch_btree_node_alloc(struct cache_set *, struct btree_op *, int);
246struct btree *bch_btree_node_get(struct cache_set *, struct btree_op *,
247 struct bkey *, int, bool);
248
249int bch_btree_insert_check_key(struct btree *, struct btree_op *,
250 struct bkey *);
251int bch_btree_insert(struct cache_set *, struct keylist *,
252 atomic_t *, struct bkey *);
253
254int bch_gc_thread_start(struct cache_set *);
255void bch_initial_gc_finish(struct cache_set *);
256void bch_moving_gc(struct cache_set *);
257int bch_btree_check(struct cache_set *);
258void bch_initial_mark_key(struct cache_set *, int, struct bkey *);
259
260static inline void wake_up_gc(struct cache_set *c)
261{
262 if (c->gc_thread)
263 wake_up_process(c->gc_thread);
264}
265
266#define MAP_DONE 0
267#define MAP_CONTINUE 1
268
269#define MAP_ALL_NODES 0
270#define MAP_LEAF_NODES 1
271
272#define MAP_END_KEY 1
273
274typedef int (btree_map_nodes_fn)(struct btree_op *, struct btree *);
275int __bch_btree_map_nodes(struct btree_op *, struct cache_set *,
276 struct bkey *, btree_map_nodes_fn *, int);
277
278static inline int bch_btree_map_nodes(struct btree_op *op, struct cache_set *c,
279 struct bkey *from, btree_map_nodes_fn *fn)
280{
281 return __bch_btree_map_nodes(op, c, from, fn, MAP_ALL_NODES);
282}
283
284static inline int bch_btree_map_leaf_nodes(struct btree_op *op,
285 struct cache_set *c,
286 struct bkey *from,
287 btree_map_nodes_fn *fn)
288{
289 return __bch_btree_map_nodes(op, c, from, fn, MAP_LEAF_NODES);
290}
291
292typedef int (btree_map_keys_fn)(struct btree_op *, struct btree *,
293 struct bkey *);
294int bch_btree_map_keys(struct btree_op *, struct cache_set *,
295 struct bkey *, btree_map_keys_fn *, int);
296
297typedef bool (keybuf_pred_fn)(struct keybuf *, struct bkey *);
298
299void bch_keybuf_init(struct keybuf *);
300void bch_refill_keybuf(struct cache_set *, struct keybuf *,
301 struct bkey *, keybuf_pred_fn *);
302bool bch_keybuf_check_overlapping(struct keybuf *, struct bkey *,
303 struct bkey *);
304void bch_keybuf_del(struct keybuf *, struct keybuf_key *);
305struct keybuf_key *bch_keybuf_next(struct keybuf *);
306struct keybuf_key *bch_keybuf_next_rescan(struct cache_set *, struct keybuf *,
307 struct bkey *, keybuf_pred_fn *);
308
309#endif
310