3 #include "btree_cache.h"
4 #include "btree_iter.h"
5 #include "btree_key_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
10 #include "journal_reclaim.h"
12 #include <trace/events/bcachefs.h>
14 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
17 const struct bkey_cached *ck = obj;
18 const struct bkey_cached_key *key = arg->key;
20 return cmp_int(ck->key.btree_id, key->btree_id) ?:
21 bkey_cmp(ck->key.pos, key->pos);
24 static const struct rhashtable_params bch2_btree_key_cache_params = {
25 .head_offset = offsetof(struct bkey_cached, hash),
26 .key_offset = offsetof(struct bkey_cached, key),
27 .key_len = sizeof(struct bkey_cached_key),
28 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
32 static inline struct bkey_cached *
33 btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
35 struct bkey_cached_key key = {
40 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
41 bch2_btree_key_cache_params);
44 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
46 if (!six_trylock_intent(&ck->c.lock))
49 if (!six_trylock_write(&ck->c.lock)) {
50 six_unlock_intent(&ck->c.lock);
54 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
55 six_unlock_write(&ck->c.lock);
56 six_unlock_intent(&ck->c.lock);
63 static void bkey_cached_evict(struct btree_key_cache *c,
64 struct bkey_cached *ck)
66 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
67 bch2_btree_key_cache_params));
68 memset(&ck->key, ~0, sizeof(ck->key));
71 static void bkey_cached_free(struct btree_key_cache *c,
72 struct bkey_cached *ck)
74 list_move(&ck->list, &c->freed);
80 six_unlock_write(&ck->c.lock);
81 six_unlock_intent(&ck->c.lock);
84 static struct bkey_cached *
85 bkey_cached_alloc(struct btree_key_cache *c)
87 struct bkey_cached *ck;
89 list_for_each_entry(ck, &c->freed, list)
90 if (bkey_cached_lock_for_evict(ck))
93 list_for_each_entry(ck, &c->clean, list)
94 if (bkey_cached_lock_for_evict(ck)) {
95 bkey_cached_evict(c, ck);
99 ck = kzalloc(sizeof(*ck), GFP_NOFS);
103 INIT_LIST_HEAD(&ck->list);
104 six_lock_init(&ck->c.lock);
105 BUG_ON(!six_trylock_intent(&ck->c.lock));
106 BUG_ON(!six_trylock_write(&ck->c.lock));
111 static struct bkey_cached *
112 btree_key_cache_create(struct btree_key_cache *c,
113 enum btree_id btree_id,
116 struct bkey_cached *ck;
118 ck = bkey_cached_alloc(c);
120 return ERR_PTR(-ENOMEM);
123 ck->c.btree_id = btree_id;
124 ck->key.btree_id = btree_id;
130 if (rhashtable_lookup_insert_fast(&c->table,
132 bch2_btree_key_cache_params)) {
133 /* We raced with another fill: */
134 bkey_cached_free(c, ck);
138 list_move(&ck->list, &c->clean);
139 six_unlock_write(&ck->c.lock);
144 static int btree_key_cache_fill(struct btree_trans *trans,
145 struct btree_iter *ck_iter,
146 struct bkey_cached *ck)
148 struct btree_iter *iter;
150 unsigned new_u64s = 0;
151 struct bkey_i *new_k = NULL;
154 iter = bch2_trans_get_iter(trans, ck->key.btree_id,
155 ck->key.pos, BTREE_ITER_SLOTS);
157 return PTR_ERR(iter);
159 k = bch2_btree_iter_peek_slot(iter);
162 bch2_trans_iter_put(trans, iter);
166 if (!bch2_btree_node_relock(ck_iter, 0)) {
167 bch2_trans_iter_put(trans, iter);
168 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
172 if (k.k->u64s > ck->u64s) {
173 new_u64s = roundup_pow_of_two(k.k->u64s);
174 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
176 bch2_trans_iter_put(trans, iter);
181 bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
188 bkey_reassemble(ck->k, k);
190 bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
192 /* We're not likely to need this iterator again: */
193 bch2_trans_iter_free(trans, iter);
198 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
200 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
201 const struct btree_iter *iter = p;
203 return ck->key.btree_id == iter->btree_id &&
204 !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
207 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
209 struct btree_trans *trans = iter->trans;
210 struct bch_fs *c = trans->c;
211 struct bkey_cached *ck;
216 if (btree_node_locked(iter, 0)) {
217 ck = (void *) iter->l[0].b;
221 ck = btree_key_cache_find(c, iter->btree_id, iter->pos);
223 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
228 mutex_lock(&c->btree_key_cache.lock);
229 ck = btree_key_cache_create(&c->btree_key_cache,
230 iter->btree_id, iter->pos);
231 mutex_unlock(&c->btree_key_cache.lock);
233 ret = PTR_ERR_OR_ZERO(ck);
239 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
240 iter->locks_want = 1;
242 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
244 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
245 bkey_cached_check_fn, iter)) {
246 if (ck->key.btree_id != iter->btree_id ||
247 bkey_cmp(ck->key.pos, iter->pos)) {
251 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
256 if (ck->key.btree_id != iter->btree_id ||
257 bkey_cmp(ck->key.pos, iter->pos)) {
258 six_unlock_type(&ck->c.lock, lock_want);
262 mark_btree_node_locked(iter, 0, lock_want);
265 iter->l[0].lock_seq = ck->c.lock.state.seq;
266 iter->l[0].b = (void *) ck;
268 if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
269 if (!btree_node_intent_locked(iter, 0))
270 bch2_btree_iter_upgrade(iter, 1);
271 if (!btree_node_intent_locked(iter, 0)) {
272 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
277 ret = btree_key_cache_fill(trans, iter, ck);
282 iter->uptodate = BTREE_ITER_NEED_PEEK;
283 bch2_btree_iter_downgrade(iter);
287 btree_node_unlock(iter, 0);
288 iter->flags |= BTREE_ITER_ERROR;
289 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
294 static int btree_key_cache_flush_pos(struct btree_trans *trans,
295 struct bkey_cached_key key,
299 struct bch_fs *c = trans->c;
300 struct journal *j = &c->journal;
301 struct btree_iter *c_iter = NULL, *b_iter = NULL;
302 struct bkey_cached *ck;
305 b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
308 ret = PTR_ERR_OR_ZERO(b_iter);
312 c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
314 BTREE_ITER_CACHED_NOFILL|
315 BTREE_ITER_CACHED_NOCREATE|
317 ret = PTR_ERR_OR_ZERO(c_iter);
321 ret = bch2_btree_iter_traverse(c_iter);
325 ck = (void *) c_iter->l[0].b;
327 (journal_seq && ck->journal.seq != journal_seq))
330 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
336 ret = bch2_btree_iter_traverse(b_iter) ?:
337 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
338 bch2_trans_commit(trans, NULL, NULL,
339 BTREE_INSERT_NOUNLOCK|
340 BTREE_INSERT_NOCHECK_RW|
342 BTREE_INSERT_USE_RESERVE|
343 BTREE_INSERT_USE_ALLOC_RESERVE|
344 BTREE_INSERT_JOURNAL_RESERVED|
345 BTREE_INSERT_JOURNAL_RECLAIM);
350 BUG_ON(ret && !bch2_journal_error(j));
355 bch2_journal_pin_drop(j, &ck->journal);
356 bch2_journal_preres_put(j, &ck->res);
357 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
360 mutex_lock(&c->btree_key_cache.lock);
361 list_move_tail(&ck->list, &c->btree_key_cache.clean);
362 mutex_unlock(&c->btree_key_cache.lock);
365 BUG_ON(!btree_node_intent_locked(c_iter, 0));
367 mark_btree_node_unlocked(c_iter, 0);
368 c_iter->l[0].b = NULL;
370 six_lock_write(&ck->c.lock, NULL, NULL);
372 mutex_lock(&c->btree_key_cache.lock);
373 bkey_cached_evict(&c->btree_key_cache, ck);
374 bkey_cached_free(&c->btree_key_cache, ck);
375 mutex_unlock(&c->btree_key_cache.lock);
378 bch2_trans_iter_put(trans, b_iter);
379 bch2_trans_iter_put(trans, c_iter);
383 static void btree_key_cache_journal_flush(struct journal *j,
384 struct journal_entry_pin *pin,
387 struct bch_fs *c = container_of(j, struct bch_fs, journal);
388 struct bkey_cached *ck =
389 container_of(pin, struct bkey_cached, journal);
390 struct bkey_cached_key key;
391 struct btree_trans trans;
393 six_lock_read(&ck->c.lock, NULL, NULL);
396 if (ck->journal.seq != seq ||
397 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
398 six_unlock_read(&ck->c.lock);
401 six_unlock_read(&ck->c.lock);
403 bch2_trans_init(&trans, c, 0, 0);
404 btree_key_cache_flush_pos(&trans, key, seq, false);
405 bch2_trans_exit(&trans);
409 * Flush and evict a key from the key cache:
411 int bch2_btree_key_cache_flush(struct btree_trans *trans,
412 enum btree_id id, struct bpos pos)
414 struct bch_fs *c = trans->c;
415 struct bkey_cached_key key = { id, pos };
417 /* Fastpath - assume it won't be found: */
418 if (!btree_key_cache_find(c, id, pos))
421 return btree_key_cache_flush_pos(trans, key, 0, true);
424 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
425 struct btree_iter *iter,
426 struct bkey_i *insert)
428 struct bch_fs *c = trans->c;
429 struct bkey_cached *ck = (void *) iter->l[0].b;
431 BUG_ON(insert->u64s > ck->u64s);
433 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
436 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
438 difference = jset_u64s(insert->u64s) - ck->res.u64s;
439 if (difference > 0) {
440 trans->journal_preres.u64s -= difference;
441 ck->res.u64s += difference;
445 bkey_copy(ck->k, insert);
448 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
449 mutex_lock(&c->btree_key_cache.lock);
450 list_del_init(&ck->list);
452 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
453 mutex_unlock(&c->btree_key_cache.lock);
456 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
457 &ck->journal, btree_key_cache_journal_flush);
461 #ifdef CONFIG_BCACHEFS_DEBUG
462 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
463 enum btree_id id, struct bpos pos)
465 BUG_ON(btree_key_cache_find(trans->c, id, pos));
469 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *c)
471 struct bkey_cached *ck, *n;
473 mutex_lock(&c->lock);
474 list_for_each_entry_safe(ck, n, &c->clean, list) {
478 list_for_each_entry_safe(ck, n, &c->freed, list)
480 mutex_unlock(&c->lock);
482 rhashtable_destroy(&c->table);
485 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
487 mutex_init(&c->lock);
488 INIT_LIST_HEAD(&c->freed);
489 INIT_LIST_HEAD(&c->clean);
492 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
494 return rhashtable_init(&c->table, &bch2_btree_key_cache_params);
497 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
499 struct bucket_table *tbl;
500 struct bkey_cached *ck;
501 struct rhash_head *pos;
504 mutex_lock(&c->lock);
505 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
507 for (i = 0; i < tbl->size; i++) {
508 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
510 bch2_btree_ids[ck->key.btree_id]);
511 bch2_bpos_to_text(out, ck->key.pos);
513 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
514 pr_buf(out, " journal seq %llu", ck->journal.seq);
518 mutex_unlock(&c->lock);