]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_key_cache.c
0ee4f78ce67a1b5ea4acd767973ef68b2db4e098
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
1
2 #include "bcachefs.h"
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"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_reclaim.h"
11
12 #include <trace/events/bcachefs.h>
13
14 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
15                                        const void *obj)
16 {
17         const struct bkey_cached *ck = obj;
18         const struct bkey_cached_key *key = arg->key;
19
20         return cmp_int(ck->key.btree_id, key->btree_id) ?:
21                 bkey_cmp(ck->key.pos, key->pos);
22 }
23
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,
29 };
30
31 __flatten
32 inline struct bkey_cached *
33 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
34 {
35         struct bkey_cached_key key = {
36                 .btree_id       = btree_id,
37                 .pos            = pos,
38         };
39
40         return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
41                                       bch2_btree_key_cache_params);
42 }
43
44 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
45 {
46         if (!six_trylock_intent(&ck->c.lock))
47                 return false;
48
49         if (!six_trylock_write(&ck->c.lock)) {
50                 six_unlock_intent(&ck->c.lock);
51                 return false;
52         }
53
54         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
55                 six_unlock_write(&ck->c.lock);
56                 six_unlock_intent(&ck->c.lock);
57                 return false;
58         }
59
60         return true;
61 }
62
63 static void bkey_cached_evict(struct btree_key_cache *c,
64                               struct bkey_cached *ck)
65 {
66         BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
67                                       bch2_btree_key_cache_params));
68         memset(&ck->key, ~0, sizeof(ck->key));
69 }
70
71 static void bkey_cached_free(struct btree_key_cache *c,
72                              struct bkey_cached *ck)
73 {
74         list_move(&ck->list, &c->freed);
75
76         kfree(ck->k);
77         ck->k           = NULL;
78         ck->u64s        = 0;
79
80         six_unlock_write(&ck->c.lock);
81         six_unlock_intent(&ck->c.lock);
82 }
83
84 static struct bkey_cached *
85 bkey_cached_alloc(struct btree_key_cache *c)
86 {
87         struct bkey_cached *ck;
88
89         list_for_each_entry(ck, &c->freed, list)
90                 if (bkey_cached_lock_for_evict(ck))
91                         return ck;
92
93         list_for_each_entry(ck, &c->clean, list)
94                 if (bkey_cached_lock_for_evict(ck)) {
95                         bkey_cached_evict(c, ck);
96                         return ck;
97                 }
98
99         ck = kzalloc(sizeof(*ck), GFP_NOFS);
100         if (!ck)
101                 return NULL;
102
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));
107
108         return ck;
109 }
110
111 static struct bkey_cached *
112 btree_key_cache_create(struct btree_key_cache *c,
113                        enum btree_id btree_id,
114                        struct bpos pos)
115 {
116         struct bkey_cached *ck;
117
118         ck = bkey_cached_alloc(c);
119         if (!ck)
120                 return ERR_PTR(-ENOMEM);
121
122         ck->c.level             = 0;
123         ck->c.btree_id          = btree_id;
124         ck->key.btree_id        = btree_id;
125         ck->key.pos             = pos;
126         ck->valid               = false;
127
128         BUG_ON(ck->flags);
129
130         if (rhashtable_lookup_insert_fast(&c->table,
131                                           &ck->hash,
132                                           bch2_btree_key_cache_params)) {
133                 /* We raced with another fill: */
134                 bkey_cached_free(c, ck);
135                 return NULL;
136         }
137
138         list_move(&ck->list, &c->clean);
139         six_unlock_write(&ck->c.lock);
140
141         return ck;
142 }
143
144 static int btree_key_cache_fill(struct btree_trans *trans,
145                                 struct btree_iter *ck_iter,
146                                 struct bkey_cached *ck)
147 {
148         struct btree_iter *iter;
149         struct bkey_s_c k;
150         unsigned new_u64s = 0;
151         struct bkey_i *new_k = NULL;
152         int ret;
153
154         iter = bch2_trans_get_iter(trans, ck->key.btree_id,
155                                    ck->key.pos, BTREE_ITER_SLOTS);
156         if (IS_ERR(iter))
157                 return PTR_ERR(iter);
158
159         k = bch2_btree_iter_peek_slot(iter);
160         ret = bkey_err(k);
161         if (ret) {
162                 bch2_trans_iter_put(trans, iter);
163                 return ret;
164         }
165
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_);
169                 return -EINTR;
170         }
171
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);
175                 if (!new_k) {
176                         bch2_trans_iter_put(trans, iter);
177                         return -ENOMEM;
178                 }
179         }
180
181         bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
182         if (new_k) {
183                 kfree(ck->k);
184                 ck->u64s = new_u64s;
185                 ck->k = new_k;
186         }
187
188         bkey_reassemble(ck->k, k);
189         ck->valid = true;
190         bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
191
192         /* We're not likely to need this iterator again: */
193         bch2_trans_iter_free(trans, iter);
194
195         return 0;
196 }
197
198 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
199 {
200         struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
201         const struct btree_iter *iter = p;
202
203         return ck->key.btree_id == iter->btree_id &&
204                 !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
205 }
206
207 __flatten
208 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
209 {
210         struct btree_trans *trans = iter->trans;
211         struct bch_fs *c = trans->c;
212         struct bkey_cached *ck;
213         int ret = 0;
214
215         BUG_ON(iter->level);
216
217         if (btree_node_locked(iter, 0)) {
218                 ck = (void *) iter->l[0].b;
219                 goto fill;
220         }
221 retry:
222         ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
223         if (!ck) {
224                 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
225                         iter->l[0].b = NULL;
226                         return 0;
227                 }
228
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);
233
234                 ret = PTR_ERR_OR_ZERO(ck);
235                 if (ret)
236                         goto err;
237                 if (!ck)
238                         goto retry;
239
240                 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
241                 iter->locks_want = 1;
242         } else {
243                 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
244
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)) {
249                                 goto retry;
250                         }
251
252                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
253                         ret = -EINTR;
254                         goto err;
255                 }
256
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);
260                         goto retry;
261                 }
262
263                 mark_btree_node_locked(iter, 0, lock_want);
264         }
265
266         iter->l[0].lock_seq     = ck->c.lock.state.seq;
267         iter->l[0].b            = (void *) ck;
268 fill:
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_);
274                         ret = -EINTR;
275                         goto err;
276                 }
277
278                 ret = btree_key_cache_fill(trans, iter, ck);
279                 if (ret)
280                         goto err;
281         }
282
283         iter->uptodate = BTREE_ITER_NEED_PEEK;
284         bch2_btree_iter_downgrade(iter);
285         return ret;
286 err:
287         if (ret != -EINTR) {
288                 btree_node_unlock(iter, 0);
289                 iter->flags |= BTREE_ITER_ERROR;
290                 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
291         }
292         return ret;
293 }
294
295 static int btree_key_cache_flush_pos(struct btree_trans *trans,
296                                      struct bkey_cached_key key,
297                                      u64 journal_seq,
298                                      bool evict)
299 {
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;
304         int ret;
305
306         b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
307                                      BTREE_ITER_SLOTS|
308                                      BTREE_ITER_INTENT);
309         ret = PTR_ERR_OR_ZERO(b_iter);
310         if (ret)
311                 goto out;
312
313         c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
314                                      BTREE_ITER_CACHED|
315                                      BTREE_ITER_CACHED_NOFILL|
316                                      BTREE_ITER_CACHED_NOCREATE|
317                                      BTREE_ITER_INTENT);
318         ret = PTR_ERR_OR_ZERO(c_iter);
319         if (ret)
320                 goto out;
321 retry:
322         ret = bch2_btree_iter_traverse(c_iter);
323         if (ret)
324                 goto err;
325
326         ck = (void *) c_iter->l[0].b;
327         if (!ck ||
328             (journal_seq && ck->journal.seq != journal_seq))
329                 goto out;
330
331         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
332                 if (!evict)
333                         goto out;
334                 goto evict;
335         }
336
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|
342                                   BTREE_INSERT_NOFAIL|
343                                   BTREE_INSERT_USE_RESERVE|
344                                   BTREE_INSERT_USE_ALLOC_RESERVE|
345                                   BTREE_INSERT_JOURNAL_RESERVED|
346                                   BTREE_INSERT_JOURNAL_RECLAIM);
347 err:
348         if (ret == -EINTR)
349                 goto retry;
350
351         BUG_ON(ret && !bch2_journal_error(j));
352
353         if (ret)
354                 goto out;
355
356         bch2_journal_pin_drop(j, &ck->journal);
357         bch2_journal_preres_put(j, &ck->res);
358         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
359
360         if (!evict) {
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);
364         } else {
365 evict:
366                 BUG_ON(!btree_node_intent_locked(c_iter, 0));
367
368                 mark_btree_node_unlocked(c_iter, 0);
369                 c_iter->l[0].b = NULL;
370
371                 six_lock_write(&ck->c.lock, NULL, NULL);
372
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);
377         }
378 out:
379         bch2_trans_iter_put(trans, b_iter);
380         bch2_trans_iter_put(trans, c_iter);
381         return ret;
382 }
383
384 static void btree_key_cache_journal_flush(struct journal *j,
385                                           struct journal_entry_pin *pin,
386                                           u64 seq)
387 {
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;
393
394         six_lock_read(&ck->c.lock, NULL, NULL);
395         key = ck->key;
396
397         if (ck->journal.seq != seq ||
398             !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
399                 six_unlock_read(&ck->c.lock);
400                 return;
401         }
402         six_unlock_read(&ck->c.lock);
403
404         bch2_trans_init(&trans, c, 0, 0);
405         btree_key_cache_flush_pos(&trans, key, seq, false);
406         bch2_trans_exit(&trans);
407 }
408
409 /*
410  * Flush and evict a key from the key cache:
411  */
412 int bch2_btree_key_cache_flush(struct btree_trans *trans,
413                                enum btree_id id, struct bpos pos)
414 {
415         struct bch_fs *c = trans->c;
416         struct bkey_cached_key key = { id, pos };
417
418         /* Fastpath - assume it won't be found: */
419         if (!bch2_btree_key_cache_find(c, id, pos))
420                 return 0;
421
422         return btree_key_cache_flush_pos(trans, key, 0, true);
423 }
424
425 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
426                                   struct btree_iter *iter,
427                                   struct bkey_i *insert)
428 {
429         struct bch_fs *c = trans->c;
430         struct bkey_cached *ck = (void *) iter->l[0].b;
431
432         BUG_ON(insert->u64s > ck->u64s);
433
434         if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
435                 int difference;
436
437                 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
438
439                 difference = jset_u64s(insert->u64s) - ck->res.u64s;
440                 if (difference > 0) {
441                         trans->journal_preres.u64s      -= difference;
442                         ck->res.u64s                    += difference;
443                 }
444         }
445
446         bkey_copy(ck->k, insert);
447         ck->valid = true;
448
449         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
450                 mutex_lock(&c->btree_key_cache.lock);
451                 list_del_init(&ck->list);
452
453                 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
454                 mutex_unlock(&c->btree_key_cache.lock);
455         }
456
457         bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
458                                 &ck->journal, btree_key_cache_journal_flush);
459         return true;
460 }
461
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)
465 {
466         BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
467 }
468 #endif
469
470 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *c)
471 {
472         struct bkey_cached *ck, *n;
473
474         mutex_lock(&c->lock);
475         list_for_each_entry_safe(ck, n, &c->clean, list) {
476                 kfree(ck->k);
477                 kfree(ck);
478         }
479         list_for_each_entry_safe(ck, n, &c->freed, list)
480                 kfree(ck);
481         mutex_unlock(&c->lock);
482
483         rhashtable_destroy(&c->table);
484 }
485
486 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
487 {
488         mutex_init(&c->lock);
489         INIT_LIST_HEAD(&c->freed);
490         INIT_LIST_HEAD(&c->clean);
491 }
492
493 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
494 {
495         return rhashtable_init(&c->table, &bch2_btree_key_cache_params);
496 }
497
498 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
499 {
500         struct bucket_table *tbl;
501         struct bkey_cached *ck;
502         struct rhash_head *pos;
503         size_t i;
504
505         mutex_lock(&c->lock);
506         tbl = rht_dereference_rcu(c->table.tbl, &c->table);
507
508         for (i = 0; i < tbl->size; i++) {
509                 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
510                         pr_buf(out, "%s:",
511                                bch2_btree_ids[ck->key.btree_id]);
512                         bch2_bpos_to_text(out, ck->key.pos);
513
514                         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
515                                 pr_buf(out, " journal seq %llu", ck->journal.seq);
516                         pr_buf(out, "\n");
517                 }
518         }
519         mutex_unlock(&c->lock);
520 }