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 inline struct bkey_cached *
33 bch2_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;
208 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
210 struct btree_trans *trans = iter->trans;
211 struct bch_fs *c = trans->c;
212 struct bkey_cached *ck;
217 if (btree_node_locked(iter, 0)) {
218 ck = (void *) iter->l[0].b;
222 ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
224 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
229 mutex_lock(&c->btree_key_cache.lock);
230 ck = btree_key_cache_create(&c->btree_key_cache,
231 iter->btree_id, iter->pos);
232 mutex_unlock(&c->btree_key_cache.lock);
234 ret = PTR_ERR_OR_ZERO(ck);
240 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
241 iter->locks_want = 1;
243 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
245 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
246 bkey_cached_check_fn, iter, _THIS_IP_)) {
247 if (ck->key.btree_id != iter->btree_id ||
248 bkey_cmp(ck->key.pos, iter->pos)) {
252 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
257 if (ck->key.btree_id != iter->btree_id ||
258 bkey_cmp(ck->key.pos, iter->pos)) {
259 six_unlock_type(&ck->c.lock, lock_want);
263 mark_btree_node_locked(iter, 0, lock_want);
266 iter->l[0].lock_seq = ck->c.lock.state.seq;
267 iter->l[0].b = (void *) ck;
269 if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
270 if (!btree_node_intent_locked(iter, 0))
271 bch2_btree_iter_upgrade(iter, 1);
272 if (!btree_node_intent_locked(iter, 0)) {
273 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
278 ret = btree_key_cache_fill(trans, iter, ck);
283 iter->uptodate = BTREE_ITER_NEED_PEEK;
284 bch2_btree_iter_downgrade(iter);
288 btree_node_unlock(iter, 0);
289 iter->flags |= BTREE_ITER_ERROR;
290 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
295 static int btree_key_cache_flush_pos(struct btree_trans *trans,
296 struct bkey_cached_key key,
300 struct bch_fs *c = trans->c;
301 struct journal *j = &c->journal;
302 struct btree_iter *c_iter = NULL, *b_iter = NULL;
303 struct bkey_cached *ck;
306 b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
309 ret = PTR_ERR_OR_ZERO(b_iter);
313 c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
315 BTREE_ITER_CACHED_NOFILL|
316 BTREE_ITER_CACHED_NOCREATE|
318 ret = PTR_ERR_OR_ZERO(c_iter);
322 ret = bch2_btree_iter_traverse(c_iter);
326 ck = (void *) c_iter->l[0].b;
328 (journal_seq && ck->journal.seq != journal_seq))
331 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
337 ret = bch2_btree_iter_traverse(b_iter) ?:
338 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
339 bch2_trans_commit(trans, NULL, NULL,
340 BTREE_INSERT_NOUNLOCK|
341 BTREE_INSERT_NOCHECK_RW|
343 BTREE_INSERT_USE_RESERVE|
344 BTREE_INSERT_USE_ALLOC_RESERVE|
345 BTREE_INSERT_JOURNAL_RESERVED|
346 BTREE_INSERT_JOURNAL_RECLAIM);
351 BUG_ON(ret && !bch2_journal_error(j));
356 bch2_journal_pin_drop(j, &ck->journal);
357 bch2_journal_preres_put(j, &ck->res);
358 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
361 mutex_lock(&c->btree_key_cache.lock);
362 list_move_tail(&ck->list, &c->btree_key_cache.clean);
363 mutex_unlock(&c->btree_key_cache.lock);
366 BUG_ON(!btree_node_intent_locked(c_iter, 0));
368 mark_btree_node_unlocked(c_iter, 0);
369 c_iter->l[0].b = NULL;
371 six_lock_write(&ck->c.lock, NULL, NULL);
373 mutex_lock(&c->btree_key_cache.lock);
374 bkey_cached_evict(&c->btree_key_cache, ck);
375 bkey_cached_free(&c->btree_key_cache, ck);
376 mutex_unlock(&c->btree_key_cache.lock);
379 bch2_trans_iter_put(trans, b_iter);
380 bch2_trans_iter_put(trans, c_iter);
384 static void btree_key_cache_journal_flush(struct journal *j,
385 struct journal_entry_pin *pin,
388 struct bch_fs *c = container_of(j, struct bch_fs, journal);
389 struct bkey_cached *ck =
390 container_of(pin, struct bkey_cached, journal);
391 struct bkey_cached_key key;
392 struct btree_trans trans;
394 six_lock_read(&ck->c.lock, NULL, NULL);
397 if (ck->journal.seq != seq ||
398 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
399 six_unlock_read(&ck->c.lock);
402 six_unlock_read(&ck->c.lock);
404 bch2_trans_init(&trans, c, 0, 0);
405 btree_key_cache_flush_pos(&trans, key, seq, false);
406 bch2_trans_exit(&trans);
410 * Flush and evict a key from the key cache:
412 int bch2_btree_key_cache_flush(struct btree_trans *trans,
413 enum btree_id id, struct bpos pos)
415 struct bch_fs *c = trans->c;
416 struct bkey_cached_key key = { id, pos };
418 /* Fastpath - assume it won't be found: */
419 if (!bch2_btree_key_cache_find(c, id, pos))
422 return btree_key_cache_flush_pos(trans, key, 0, true);
425 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
426 struct btree_iter *iter,
427 struct bkey_i *insert)
429 struct bch_fs *c = trans->c;
430 struct bkey_cached *ck = (void *) iter->l[0].b;
432 BUG_ON(insert->u64s > ck->u64s);
434 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
437 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
439 difference = jset_u64s(insert->u64s) - ck->res.u64s;
440 if (difference > 0) {
441 trans->journal_preres.u64s -= difference;
442 ck->res.u64s += difference;
446 bkey_copy(ck->k, insert);
449 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
450 mutex_lock(&c->btree_key_cache.lock);
451 list_del_init(&ck->list);
453 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
454 mutex_unlock(&c->btree_key_cache.lock);
457 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
458 &ck->journal, btree_key_cache_journal_flush);
462 #ifdef CONFIG_BCACHEFS_DEBUG
463 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
464 enum btree_id id, struct bpos pos)
466 BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
470 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *c)
472 struct bkey_cached *ck, *n;
474 mutex_lock(&c->lock);
475 list_for_each_entry_safe(ck, n, &c->clean, list) {
479 list_for_each_entry_safe(ck, n, &c->freed, list)
481 mutex_unlock(&c->lock);
483 rhashtable_destroy(&c->table);
486 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
488 mutex_init(&c->lock);
489 INIT_LIST_HEAD(&c->freed);
490 INIT_LIST_HEAD(&c->clean);
493 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
495 return rhashtable_init(&c->table, &bch2_btree_key_cache_params);
498 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
500 struct bucket_table *tbl;
501 struct bkey_cached *ck;
502 struct rhash_head *pos;
505 mutex_lock(&c->lock);
506 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
508 for (i = 0; i < tbl->size; i++) {
509 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
511 bch2_btree_ids[ck->key.btree_id]);
512 bch2_bpos_to_text(out, ck->key.pos);
514 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
515 pr_buf(out, " journal seq %llu", ck->journal.seq);
519 mutex_unlock(&c->lock);