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 <linux/sched/mm.h>
13 #include <trace/events/bcachefs.h>
15 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
18 const struct bkey_cached *ck = obj;
19 const struct bkey_cached_key *key = arg->key;
21 return cmp_int(ck->key.btree_id, key->btree_id) ?:
22 bkey_cmp(ck->key.pos, key->pos);
25 static const struct rhashtable_params bch2_btree_key_cache_params = {
26 .head_offset = offsetof(struct bkey_cached, hash),
27 .key_offset = offsetof(struct bkey_cached, key),
28 .key_len = sizeof(struct bkey_cached_key),
29 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
33 inline struct bkey_cached *
34 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
36 struct bkey_cached_key key = {
41 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
42 bch2_btree_key_cache_params);
45 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
47 if (!six_trylock_intent(&ck->c.lock))
50 if (!six_trylock_write(&ck->c.lock)) {
51 six_unlock_intent(&ck->c.lock);
55 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
56 six_unlock_write(&ck->c.lock);
57 six_unlock_intent(&ck->c.lock);
64 static void bkey_cached_evict(struct btree_key_cache *c,
65 struct bkey_cached *ck)
67 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
68 bch2_btree_key_cache_params));
69 memset(&ck->key, ~0, sizeof(ck->key));
74 static void bkey_cached_free(struct btree_key_cache *bc,
75 struct bkey_cached *ck)
77 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
79 ck->btree_trans_barrier_seq =
80 start_poll_synchronize_srcu(&c->btree_trans_barrier);
82 list_move(&ck->list, &bc->freed);
88 six_unlock_write(&ck->c.lock);
89 six_unlock_intent(&ck->c.lock);
92 static struct bkey_cached *
93 bkey_cached_alloc(struct btree_key_cache *c)
95 struct bkey_cached *ck;
97 list_for_each_entry(ck, &c->freed, list)
98 if (bkey_cached_lock_for_evict(ck))
101 list_for_each_entry(ck, &c->clean, list)
102 if (bkey_cached_lock_for_evict(ck)) {
103 bkey_cached_evict(c, ck);
107 ck = kzalloc(sizeof(*ck), GFP_NOFS);
111 INIT_LIST_HEAD(&ck->list);
112 six_lock_init(&ck->c.lock);
113 BUG_ON(!six_trylock_intent(&ck->c.lock));
114 BUG_ON(!six_trylock_write(&ck->c.lock));
119 static struct bkey_cached *
120 btree_key_cache_create(struct btree_key_cache *c,
121 enum btree_id btree_id,
124 struct bkey_cached *ck;
126 ck = bkey_cached_alloc(c);
128 return ERR_PTR(-ENOMEM);
131 ck->c.btree_id = btree_id;
132 ck->key.btree_id = btree_id;
138 if (rhashtable_lookup_insert_fast(&c->table,
140 bch2_btree_key_cache_params)) {
141 /* We raced with another fill: */
142 bkey_cached_free(c, ck);
148 list_move(&ck->list, &c->clean);
149 six_unlock_write(&ck->c.lock);
154 static int btree_key_cache_fill(struct btree_trans *trans,
155 struct btree_iter *ck_iter,
156 struct bkey_cached *ck)
158 struct btree_iter *iter;
160 unsigned new_u64s = 0;
161 struct bkey_i *new_k = NULL;
164 iter = bch2_trans_get_iter(trans, ck->key.btree_id,
165 ck->key.pos, BTREE_ITER_SLOTS);
167 return PTR_ERR(iter);
169 k = bch2_btree_iter_peek_slot(iter);
172 bch2_trans_iter_put(trans, iter);
176 if (!bch2_btree_node_relock(ck_iter, 0)) {
177 bch2_trans_iter_put(trans, iter);
178 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
182 if (k.k->u64s > ck->u64s) {
183 new_u64s = roundup_pow_of_two(k.k->u64s);
184 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
186 bch2_trans_iter_put(trans, iter);
191 bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
198 bkey_reassemble(ck->k, k);
200 bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
202 /* We're not likely to need this iterator again: */
203 bch2_trans_iter_free(trans, iter);
208 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
210 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
211 const struct btree_iter *iter = p;
213 return ck->key.btree_id == iter->btree_id &&
214 !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
218 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
220 struct btree_trans *trans = iter->trans;
221 struct bch_fs *c = trans->c;
222 struct bkey_cached *ck;
227 if (btree_node_locked(iter, 0)) {
228 ck = (void *) iter->l[0].b;
232 ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
234 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
239 mutex_lock(&c->btree_key_cache.lock);
240 ck = btree_key_cache_create(&c->btree_key_cache,
241 iter->btree_id, iter->pos);
242 mutex_unlock(&c->btree_key_cache.lock);
244 ret = PTR_ERR_OR_ZERO(ck);
250 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
251 iter->locks_want = 1;
253 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
255 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
256 bkey_cached_check_fn, iter, _THIS_IP_)) {
257 if (ck->key.btree_id != iter->btree_id ||
258 bkey_cmp(ck->key.pos, iter->pos)) {
262 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
267 if (ck->key.btree_id != iter->btree_id ||
268 bkey_cmp(ck->key.pos, iter->pos)) {
269 six_unlock_type(&ck->c.lock, lock_want);
273 mark_btree_node_locked(iter, 0, lock_want);
276 iter->l[0].lock_seq = ck->c.lock.state.seq;
277 iter->l[0].b = (void *) ck;
279 if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
280 if (!btree_node_intent_locked(iter, 0))
281 bch2_btree_iter_upgrade(iter, 1);
282 if (!btree_node_intent_locked(iter, 0)) {
283 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
288 ret = btree_key_cache_fill(trans, iter, ck);
293 iter->uptodate = BTREE_ITER_NEED_PEEK;
294 bch2_btree_iter_downgrade(iter);
298 btree_node_unlock(iter, 0);
299 iter->flags |= BTREE_ITER_ERROR;
300 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
305 static int btree_key_cache_flush_pos(struct btree_trans *trans,
306 struct bkey_cached_key key,
310 struct bch_fs *c = trans->c;
311 struct journal *j = &c->journal;
312 struct btree_iter *c_iter = NULL, *b_iter = NULL;
313 struct bkey_cached *ck;
316 b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
319 ret = PTR_ERR_OR_ZERO(b_iter);
323 c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
325 BTREE_ITER_CACHED_NOFILL|
326 BTREE_ITER_CACHED_NOCREATE|
328 ret = PTR_ERR_OR_ZERO(c_iter);
332 ret = bch2_btree_iter_traverse(c_iter);
336 ck = (void *) c_iter->l[0].b;
338 (journal_seq && ck->journal.seq != journal_seq))
341 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
347 ret = bch2_btree_iter_traverse(b_iter) ?:
348 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
349 bch2_trans_commit(trans, NULL, NULL,
350 BTREE_INSERT_NOUNLOCK|
351 BTREE_INSERT_NOCHECK_RW|
353 BTREE_INSERT_USE_RESERVE|
354 BTREE_INSERT_USE_ALLOC_RESERVE|
355 BTREE_INSERT_JOURNAL_RESERVED|
356 BTREE_INSERT_JOURNAL_RECLAIM);
361 BUG_ON(ret && !bch2_journal_error(j));
366 bch2_journal_pin_drop(j, &ck->journal);
367 bch2_journal_preres_put(j, &ck->res);
370 mutex_lock(&c->btree_key_cache.lock);
371 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
372 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
373 c->btree_key_cache.nr_dirty--;
376 list_move_tail(&ck->list, &c->btree_key_cache.clean);
377 mutex_unlock(&c->btree_key_cache.lock);
380 BUG_ON(!btree_node_intent_locked(c_iter, 0));
382 mark_btree_node_unlocked(c_iter, 0);
383 c_iter->l[0].b = NULL;
385 six_lock_write(&ck->c.lock, NULL, NULL);
387 mutex_lock(&c->btree_key_cache.lock);
388 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
389 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
390 c->btree_key_cache.nr_dirty--;
393 bkey_cached_evict(&c->btree_key_cache, ck);
394 bkey_cached_free(&c->btree_key_cache, ck);
395 mutex_unlock(&c->btree_key_cache.lock);
398 bch2_trans_iter_put(trans, b_iter);
399 bch2_trans_iter_put(trans, c_iter);
403 static void btree_key_cache_journal_flush(struct journal *j,
404 struct journal_entry_pin *pin,
407 struct bch_fs *c = container_of(j, struct bch_fs, journal);
408 struct bkey_cached *ck =
409 container_of(pin, struct bkey_cached, journal);
410 struct bkey_cached_key key;
411 struct btree_trans trans;
413 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
415 six_lock_read(&ck->c.lock, NULL, NULL);
418 if (ck->journal.seq != seq ||
419 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
420 six_unlock_read(&ck->c.lock);
423 six_unlock_read(&ck->c.lock);
425 bch2_trans_init(&trans, c, 0, 0);
426 btree_key_cache_flush_pos(&trans, key, seq, false);
427 bch2_trans_exit(&trans);
429 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
433 * Flush and evict a key from the key cache:
435 int bch2_btree_key_cache_flush(struct btree_trans *trans,
436 enum btree_id id, struct bpos pos)
438 struct bch_fs *c = trans->c;
439 struct bkey_cached_key key = { id, pos };
441 /* Fastpath - assume it won't be found: */
442 if (!bch2_btree_key_cache_find(c, id, pos))
445 return btree_key_cache_flush_pos(trans, key, 0, true);
448 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
449 struct btree_iter *iter,
450 struct bkey_i *insert)
452 struct bch_fs *c = trans->c;
453 struct bkey_cached *ck = (void *) iter->l[0].b;
455 BUG_ON(insert->u64s > ck->u64s);
457 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
460 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
462 difference = jset_u64s(insert->u64s) - ck->res.u64s;
463 if (difference > 0) {
464 trans->journal_preres.u64s -= difference;
465 ck->res.u64s += difference;
469 bkey_copy(ck->k, insert);
472 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
473 mutex_lock(&c->btree_key_cache.lock);
474 list_move(&ck->list, &c->btree_key_cache.dirty);
476 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
477 c->btree_key_cache.nr_dirty++;
478 mutex_unlock(&c->btree_key_cache.lock);
481 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
482 &ck->journal, btree_key_cache_journal_flush);
486 #ifdef CONFIG_BCACHEFS_DEBUG
487 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
488 enum btree_id id, struct bpos pos)
490 BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
494 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
495 struct shrink_control *sc)
497 struct bch_fs *c = container_of(shrink, struct bch_fs,
498 btree_key_cache.shrink);
499 struct btree_key_cache *bc = &c->btree_key_cache;
500 struct bkey_cached *ck, *t;
501 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
504 /* Return -1 if we can't do anything right now */
505 if (sc->gfp_mask & __GFP_FS)
506 mutex_lock(&bc->lock);
507 else if (!mutex_trylock(&bc->lock))
510 flags = memalloc_nofs_save();
512 list_for_each_entry_safe(ck, t, &bc->freed, list) {
515 if (poll_state_synchronize_srcu(&c->btree_trans_barrier,
516 ck->btree_trans_barrier_seq)) {
526 list_for_each_entry_safe(ck, t, &bc->clean, list) {
529 if (bkey_cached_lock_for_evict(ck)) {
530 bkey_cached_evict(bc, ck);
531 bkey_cached_free(bc, ck);
535 if (&t->list != &bc->clean)
536 list_move_tail(&bc->clean, &t->list);
541 memalloc_nofs_restore(flags);
542 mutex_unlock(&bc->lock);
547 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
548 struct shrink_control *sc)
550 struct bch_fs *c = container_of(shrink, struct bch_fs,
551 btree_key_cache.shrink);
552 struct btree_key_cache *bc = &c->btree_key_cache;
557 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
559 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
560 struct bkey_cached *ck, *n;
562 if (bc->shrink.list.next)
563 unregister_shrinker(&bc->shrink);
565 mutex_lock(&bc->lock);
566 list_splice(&bc->dirty, &bc->clean);
568 list_for_each_entry_safe(ck, n, &bc->clean, list) {
569 bch2_journal_pin_drop(&c->journal, &ck->journal);
570 bch2_journal_preres_put(&c->journal, &ck->res);
577 BUG_ON(bc->nr_dirty && !bch2_journal_error(&c->journal));
580 list_for_each_entry_safe(ck, n, &bc->freed, list)
582 mutex_unlock(&bc->lock);
584 rhashtable_destroy(&bc->table);
587 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
589 mutex_init(&c->lock);
590 INIT_LIST_HEAD(&c->freed);
591 INIT_LIST_HEAD(&c->clean);
592 INIT_LIST_HEAD(&c->dirty);
595 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
597 c->shrink.count_objects = bch2_btree_key_cache_count;
598 c->shrink.scan_objects = bch2_btree_key_cache_scan;
600 return register_shrinker(&c->shrink) ?:
601 rhashtable_init(&c->table, &bch2_btree_key_cache_params);
604 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
606 struct bucket_table *tbl;
607 struct bkey_cached *ck;
608 struct rhash_head *pos;
611 mutex_lock(&c->lock);
612 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
614 for (i = 0; i < tbl->size; i++) {
615 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
617 bch2_btree_ids[ck->key.btree_id]);
618 bch2_bpos_to_text(out, ck->key.pos);
620 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
621 pr_buf(out, " journal seq %llu", ck->journal.seq);
625 mutex_unlock(&c->lock);