]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_key_cache.c
61662750dfc046583d2d4c6092854e6a2c948335
[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 static inline struct bkey_cached *
33 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 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
208 {
209         struct btree_trans *trans = iter->trans;
210         struct bch_fs *c = trans->c;
211         struct bkey_cached *ck;
212         int ret = 0;
213
214         BUG_ON(iter->level);
215
216         if (btree_node_locked(iter, 0)) {
217                 ck = (void *) iter->l[0].b;
218                 goto fill;
219         }
220 retry:
221         ck = btree_key_cache_find(c, iter->btree_id, iter->pos);
222         if (!ck) {
223                 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
224                         iter->l[0].b = NULL;
225                         return 0;
226                 }
227
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);
232
233                 ret = PTR_ERR_OR_ZERO(ck);
234                 if (ret)
235                         goto err;
236                 if (!ck)
237                         goto retry;
238
239                 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
240                 iter->locks_want = 1;
241         } else {
242                 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
243
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)) {
248                                 goto retry;
249                         }
250
251                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
252                         ret = -EINTR;
253                         goto err;
254                 }
255
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);
259                         goto retry;
260                 }
261
262                 mark_btree_node_locked(iter, 0, lock_want);
263         }
264
265         iter->l[0].lock_seq     = ck->c.lock.state.seq;
266         iter->l[0].b            = (void *) ck;
267 fill:
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_);
273                         ret = -EINTR;
274                         goto err;
275                 }
276
277                 ret = btree_key_cache_fill(trans, iter, ck);
278                 if (ret)
279                         goto err;
280         }
281
282         iter->uptodate = BTREE_ITER_NEED_PEEK;
283         bch2_btree_iter_downgrade(iter);
284         return ret;
285 err:
286         if (ret != -EINTR) {
287                 btree_node_unlock(iter, 0);
288                 iter->flags |= BTREE_ITER_ERROR;
289                 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
290         }
291         return ret;
292 }
293
294 static int btree_key_cache_flush_pos(struct btree_trans *trans,
295                                      struct bkey_cached_key key,
296                                      u64 journal_seq,
297                                      bool evict)
298 {
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;
303         int ret;
304
305         b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
306                                      BTREE_ITER_SLOTS|
307                                      BTREE_ITER_INTENT);
308         ret = PTR_ERR_OR_ZERO(b_iter);
309         if (ret)
310                 goto out;
311
312         c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
313                                      BTREE_ITER_CACHED|
314                                      BTREE_ITER_CACHED_NOFILL|
315                                      BTREE_ITER_CACHED_NOCREATE|
316                                      BTREE_ITER_INTENT);
317         ret = PTR_ERR_OR_ZERO(c_iter);
318         if (ret)
319                 goto out;
320 retry:
321         ret = bch2_btree_iter_traverse(c_iter);
322         if (ret)
323                 goto err;
324
325         ck = (void *) c_iter->l[0].b;
326         if (!ck ||
327             (journal_seq && ck->journal.seq != journal_seq))
328                 goto out;
329
330         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
331                 if (!evict)
332                         goto out;
333                 goto evict;
334         }
335
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|
341                                   BTREE_INSERT_NOFAIL|
342                                   BTREE_INSERT_USE_RESERVE|
343                                   BTREE_INSERT_USE_ALLOC_RESERVE|
344                                   BTREE_INSERT_JOURNAL_RESERVED|
345                                   BTREE_INSERT_JOURNAL_RECLAIM);
346 err:
347         if (ret == -EINTR)
348                 goto retry;
349
350         BUG_ON(ret && !bch2_journal_error(j));
351
352         if (ret)
353                 goto out;
354
355         bch2_journal_pin_drop(j, &ck->journal);
356         bch2_journal_preres_put(j, &ck->res);
357         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
358
359         if (!evict) {
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);
363         } else {
364 evict:
365                 BUG_ON(!btree_node_intent_locked(c_iter, 0));
366
367                 mark_btree_node_unlocked(c_iter, 0);
368                 c_iter->l[0].b = NULL;
369
370                 six_lock_write(&ck->c.lock, NULL, NULL);
371
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);
376         }
377 out:
378         bch2_trans_iter_put(trans, b_iter);
379         bch2_trans_iter_put(trans, c_iter);
380         return ret;
381 }
382
383 static void btree_key_cache_journal_flush(struct journal *j,
384                                           struct journal_entry_pin *pin,
385                                           u64 seq)
386 {
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;
392
393         six_lock_read(&ck->c.lock, NULL, NULL);
394         key = ck->key;
395
396         if (ck->journal.seq != seq ||
397             !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
398                 six_unlock_read(&ck->c.lock);
399                 return;
400         }
401         six_unlock_read(&ck->c.lock);
402
403         bch2_trans_init(&trans, c, 0, 0);
404         btree_key_cache_flush_pos(&trans, key, seq, false);
405         bch2_trans_exit(&trans);
406 }
407
408 /*
409  * Flush and evict a key from the key cache:
410  */
411 int bch2_btree_key_cache_flush(struct btree_trans *trans,
412                                enum btree_id id, struct bpos pos)
413 {
414         struct bch_fs *c = trans->c;
415         struct bkey_cached_key key = { id, pos };
416
417         /* Fastpath - assume it won't be found: */
418         if (!btree_key_cache_find(c, id, pos))
419                 return 0;
420
421         return btree_key_cache_flush_pos(trans, key, 0, true);
422 }
423
424 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
425                                   struct btree_iter *iter,
426                                   struct bkey_i *insert)
427 {
428         struct bch_fs *c = trans->c;
429         struct bkey_cached *ck = (void *) iter->l[0].b;
430
431         BUG_ON(insert->u64s > ck->u64s);
432
433         if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
434                 int difference;
435
436                 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
437
438                 difference = jset_u64s(insert->u64s) - ck->res.u64s;
439                 if (difference > 0) {
440                         trans->journal_preres.u64s      -= difference;
441                         ck->res.u64s                    += difference;
442                 }
443         }
444
445         bkey_copy(ck->k, insert);
446         ck->valid = true;
447
448         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
449                 mutex_lock(&c->btree_key_cache.lock);
450                 list_del_init(&ck->list);
451
452                 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
453                 mutex_unlock(&c->btree_key_cache.lock);
454         }
455
456         bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
457                                 &ck->journal, btree_key_cache_journal_flush);
458         return true;
459 }
460
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)
464 {
465         BUG_ON(btree_key_cache_find(trans->c, id, pos));
466 }
467 #endif
468
469 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *c)
470 {
471         struct bkey_cached *ck, *n;
472
473         mutex_lock(&c->lock);
474         list_for_each_entry_safe(ck, n, &c->clean, list) {
475                 kfree(ck->k);
476                 kfree(ck);
477         }
478         list_for_each_entry_safe(ck, n, &c->freed, list)
479                 kfree(ck);
480         mutex_unlock(&c->lock);
481
482         rhashtable_destroy(&c->table);
483 }
484
485 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
486 {
487         mutex_init(&c->lock);
488         INIT_LIST_HEAD(&c->freed);
489         INIT_LIST_HEAD(&c->clean);
490 }
491
492 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
493 {
494         return rhashtable_init(&c->table, &bch2_btree_key_cache_params);
495 }
496
497 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
498 {
499         struct bucket_table *tbl;
500         struct bkey_cached *ck;
501         struct rhash_head *pos;
502         size_t i;
503
504         mutex_lock(&c->lock);
505         tbl = rht_dereference_rcu(c->table.tbl, &c->table);
506
507         for (i = 0; i < tbl->size; i++) {
508                 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
509                         pr_buf(out, "%s:",
510                                bch2_btree_ids[ck->key.btree_id]);
511                         bch2_bpos_to_text(out, ck->key.pos);
512
513                         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
514                                 pr_buf(out, " journal seq %llu", ck->journal.seq);
515                         pr_buf(out, "\n");
516                 }
517         }
518         mutex_unlock(&c->lock);
519 }