]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_cache.c
Update bcachefs sources to 63924135a1 bcachefs: Have fsck check for stripe pointers...
[bcachefs-tools-debian] / libbcachefs / btree_cache.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey_buf.h"
5 #include "btree_cache.h"
6 #include "btree_io.h"
7 #include "btree_iter.h"
8 #include "btree_locking.h"
9 #include "debug.h"
10 #include "error.h"
11
12 #include <linux/prefetch.h>
13 #include <linux/sched/mm.h>
14 #include <trace/events/bcachefs.h>
15
16 void bch2_recalc_btree_reserve(struct bch_fs *c)
17 {
18         unsigned i, reserve = 16;
19
20         if (!c->btree_roots[0].b)
21                 reserve += 8;
22
23         for (i = 0; i < BTREE_ID_NR; i++)
24                 if (c->btree_roots[i].b)
25                         reserve += min_t(unsigned, 1,
26                                          c->btree_roots[i].b->c.level) * 8;
27
28         c->btree_cache.reserve = reserve;
29 }
30
31 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
32 {
33         return max_t(int, 0, bc->used - bc->reserve);
34 }
35
36 static void __btree_node_data_free(struct bch_fs *c, struct btree *b)
37 {
38         EBUG_ON(btree_node_write_in_flight(b));
39
40         kvpfree(b->data, btree_bytes(c));
41         b->data = NULL;
42         vfree(b->aux_data);
43         b->aux_data = NULL;
44 }
45
46 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
47 {
48         struct btree_cache *bc = &c->btree_cache;
49
50         __btree_node_data_free(c, b);
51         bc->used--;
52         list_move(&b->list, &bc->freed);
53 }
54
55 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
56                                    const void *obj)
57 {
58         const struct btree *b = obj;
59         const u64 *v = arg->key;
60
61         return b->hash_val == *v ? 0 : 1;
62 }
63
64 static const struct rhashtable_params bch_btree_cache_params = {
65         .head_offset    = offsetof(struct btree, hash),
66         .key_offset     = offsetof(struct btree, hash_val),
67         .key_len        = sizeof(u64),
68         .obj_cmpfn      = bch2_btree_cache_cmp_fn,
69 };
70
71 static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
72 {
73         BUG_ON(b->data || b->aux_data);
74
75         b->data = kvpmalloc(btree_bytes(c), gfp);
76         if (!b->data)
77                 return -ENOMEM;
78
79         b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
80         if (!b->aux_data) {
81                 kvpfree(b->data, btree_bytes(c));
82                 b->data = NULL;
83                 return -ENOMEM;
84         }
85
86         return 0;
87 }
88
89 static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
90 {
91         struct btree *b = kzalloc(sizeof(struct btree), GFP_KERNEL);
92         if (!b)
93                 return NULL;
94
95         bkey_btree_ptr_init(&b->key);
96         six_lock_init(&b->c.lock);
97         INIT_LIST_HEAD(&b->list);
98         INIT_LIST_HEAD(&b->write_blocked);
99         b->byte_order = ilog2(btree_bytes(c));
100         return b;
101 }
102
103 static struct btree *btree_node_mem_alloc(struct bch_fs *c)
104 {
105         struct btree_cache *bc = &c->btree_cache;
106         struct btree *b = __btree_node_mem_alloc(c);
107         if (!b)
108                 return NULL;
109
110         if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
111                 kfree(b);
112                 return NULL;
113         }
114
115         bc->used++;
116         list_add(&b->list, &bc->freeable);
117         return b;
118 }
119
120 /* Btree in memory cache - hash table */
121
122 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
123 {
124         rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
125
126         /* Cause future lookups for this node to fail: */
127         b->hash_val = 0;
128
129         six_lock_wakeup_all(&b->c.lock);
130 }
131
132 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
133 {
134         BUG_ON(b->hash_val);
135         b->hash_val = btree_ptr_hash_val(&b->key);
136
137         return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
138                                              bch_btree_cache_params);
139 }
140
141 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
142                                 unsigned level, enum btree_id id)
143 {
144         int ret;
145
146         b->c.level      = level;
147         b->c.btree_id   = id;
148
149         mutex_lock(&bc->lock);
150         ret = __bch2_btree_node_hash_insert(bc, b);
151         if (!ret)
152                 list_add(&b->list, &bc->live);
153         mutex_unlock(&bc->lock);
154
155         return ret;
156 }
157
158 __flatten
159 static inline struct btree *btree_cache_find(struct btree_cache *bc,
160                                      const struct bkey_i *k)
161 {
162         u64 v = btree_ptr_hash_val(k);
163
164         return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
165 }
166
167 /*
168  * this version is for btree nodes that have already been freed (we're not
169  * reaping a real btree node)
170  */
171 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
172 {
173         struct btree_cache *bc = &c->btree_cache;
174         int ret = 0;
175
176         lockdep_assert_held(&bc->lock);
177
178         if (!six_trylock_intent(&b->c.lock))
179                 return -ENOMEM;
180
181         if (!six_trylock_write(&b->c.lock))
182                 goto out_unlock_intent;
183
184         if (btree_node_noevict(b))
185                 goto out_unlock;
186
187         if (!btree_node_may_write(b))
188                 goto out_unlock;
189
190         if (btree_node_dirty(b) &&
191             test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
192                 goto out_unlock;
193
194         if (btree_node_dirty(b) ||
195             btree_node_write_in_flight(b) ||
196             btree_node_read_in_flight(b)) {
197                 if (!flush)
198                         goto out_unlock;
199
200                 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
201                                TASK_UNINTERRUPTIBLE);
202
203                 /*
204                  * Using the underscore version because we don't want to compact
205                  * bsets after the write, since this node is about to be evicted
206                  * - unless btree verify mode is enabled, since it runs out of
207                  * the post write cleanup:
208                  */
209                 if (bch2_verify_btree_ondisk)
210                         bch2_btree_node_write(c, b, SIX_LOCK_intent);
211                 else
212                         __bch2_btree_node_write(c, b, SIX_LOCK_read);
213
214                 /* wait for any in flight btree write */
215                 btree_node_wait_on_io(b);
216         }
217 out:
218         if (b->hash_val && !ret)
219                 trace_btree_node_reap(c, b);
220         return ret;
221 out_unlock:
222         six_unlock_write(&b->c.lock);
223 out_unlock_intent:
224         six_unlock_intent(&b->c.lock);
225         ret = -ENOMEM;
226         goto out;
227 }
228
229 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
230 {
231         return __btree_node_reclaim(c, b, false);
232 }
233
234 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
235 {
236         return __btree_node_reclaim(c, b, true);
237 }
238
239 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
240                                            struct shrink_control *sc)
241 {
242         struct bch_fs *c = container_of(shrink, struct bch_fs,
243                                         btree_cache.shrink);
244         struct btree_cache *bc = &c->btree_cache;
245         struct btree *b, *t;
246         unsigned long nr = sc->nr_to_scan;
247         unsigned long can_free;
248         unsigned long touched = 0;
249         unsigned long freed = 0;
250         unsigned i, flags;
251
252         if (bch2_btree_shrinker_disabled)
253                 return SHRINK_STOP;
254
255         /* Return -1 if we can't do anything right now */
256         if (sc->gfp_mask & __GFP_FS)
257                 mutex_lock(&bc->lock);
258         else if (!mutex_trylock(&bc->lock))
259                 return -1;
260
261         flags = memalloc_nofs_save();
262
263         /*
264          * It's _really_ critical that we don't free too many btree nodes - we
265          * have to always leave ourselves a reserve. The reserve is how we
266          * guarantee that allocating memory for a new btree node can always
267          * succeed, so that inserting keys into the btree can always succeed and
268          * IO can always make forward progress:
269          */
270         nr /= btree_pages(c);
271         can_free = btree_cache_can_free(bc);
272         nr = min_t(unsigned long, nr, can_free);
273
274         i = 0;
275         list_for_each_entry_safe(b, t, &bc->freeable, list) {
276                 touched++;
277
278                 if (freed >= nr)
279                         break;
280
281                 if (++i > 3 &&
282                     !btree_node_reclaim(c, b)) {
283                         btree_node_data_free(c, b);
284                         six_unlock_write(&b->c.lock);
285                         six_unlock_intent(&b->c.lock);
286                         freed++;
287                 }
288         }
289 restart:
290         list_for_each_entry_safe(b, t, &bc->live, list) {
291                 touched++;
292
293                 if (freed >= nr) {
294                         /* Save position */
295                         if (&t->list != &bc->live)
296                                 list_move_tail(&bc->live, &t->list);
297                         break;
298                 }
299
300                 if (!btree_node_accessed(b) &&
301                     !btree_node_reclaim(c, b)) {
302                         /* can't call bch2_btree_node_hash_remove under lock  */
303                         freed++;
304                         if (&t->list != &bc->live)
305                                 list_move_tail(&bc->live, &t->list);
306
307                         btree_node_data_free(c, b);
308                         mutex_unlock(&bc->lock);
309
310                         bch2_btree_node_hash_remove(bc, b);
311                         six_unlock_write(&b->c.lock);
312                         six_unlock_intent(&b->c.lock);
313
314                         if (freed >= nr)
315                                 goto out;
316
317                         if (sc->gfp_mask & __GFP_FS)
318                                 mutex_lock(&bc->lock);
319                         else if (!mutex_trylock(&bc->lock))
320                                 goto out;
321                         goto restart;
322                 } else
323                         clear_btree_node_accessed(b);
324         }
325
326         mutex_unlock(&bc->lock);
327 out:
328         memalloc_nofs_restore(flags);
329         return (unsigned long) freed * btree_pages(c);
330 }
331
332 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
333                                             struct shrink_control *sc)
334 {
335         struct bch_fs *c = container_of(shrink, struct bch_fs,
336                                         btree_cache.shrink);
337         struct btree_cache *bc = &c->btree_cache;
338
339         if (bch2_btree_shrinker_disabled)
340                 return 0;
341
342         return btree_cache_can_free(bc) * btree_pages(c);
343 }
344
345 void bch2_fs_btree_cache_exit(struct bch_fs *c)
346 {
347         struct btree_cache *bc = &c->btree_cache;
348         struct btree *b;
349         unsigned i, flags;
350
351         if (bc->shrink.list.next)
352                 unregister_shrinker(&bc->shrink);
353
354         /* vfree() can allocate memory: */
355         flags = memalloc_nofs_save();
356         mutex_lock(&bc->lock);
357
358 #ifdef CONFIG_BCACHEFS_DEBUG
359         if (c->verify_data)
360                 list_move(&c->verify_data->list, &bc->live);
361
362         kvpfree(c->verify_ondisk, btree_bytes(c));
363 #endif
364
365         for (i = 0; i < BTREE_ID_NR; i++)
366                 if (c->btree_roots[i].b)
367                         list_add(&c->btree_roots[i].b->list, &bc->live);
368
369         list_splice(&bc->freeable, &bc->live);
370
371         while (!list_empty(&bc->live)) {
372                 b = list_first_entry(&bc->live, struct btree, list);
373
374                 BUG_ON(btree_node_read_in_flight(b) ||
375                        btree_node_write_in_flight(b));
376
377                 if (btree_node_dirty(b))
378                         bch2_btree_complete_write(c, b, btree_current_write(b));
379                 clear_btree_node_dirty(c, b);
380
381                 btree_node_data_free(c, b);
382         }
383
384         BUG_ON(atomic_read(&c->btree_cache.dirty));
385
386         while (!list_empty(&bc->freed)) {
387                 b = list_first_entry(&bc->freed, struct btree, list);
388                 list_del(&b->list);
389                 kfree(b);
390         }
391
392         mutex_unlock(&bc->lock);
393         memalloc_nofs_restore(flags);
394
395         if (bc->table_init_done)
396                 rhashtable_destroy(&bc->table);
397 }
398
399 int bch2_fs_btree_cache_init(struct bch_fs *c)
400 {
401         struct btree_cache *bc = &c->btree_cache;
402         unsigned i;
403         int ret = 0;
404
405         pr_verbose_init(c->opts, "");
406
407         ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
408         if (ret)
409                 goto out;
410
411         bc->table_init_done = true;
412
413         bch2_recalc_btree_reserve(c);
414
415         for (i = 0; i < bc->reserve; i++)
416                 if (!btree_node_mem_alloc(c)) {
417                         ret = -ENOMEM;
418                         goto out;
419                 }
420
421         list_splice_init(&bc->live, &bc->freeable);
422
423 #ifdef CONFIG_BCACHEFS_DEBUG
424         mutex_init(&c->verify_lock);
425
426         c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
427         if (!c->verify_ondisk) {
428                 ret = -ENOMEM;
429                 goto out;
430         }
431
432         c->verify_data = btree_node_mem_alloc(c);
433         if (!c->verify_data) {
434                 ret = -ENOMEM;
435                 goto out;
436         }
437
438         list_del_init(&c->verify_data->list);
439 #endif
440
441         bc->shrink.count_objects        = bch2_btree_cache_count;
442         bc->shrink.scan_objects         = bch2_btree_cache_scan;
443         bc->shrink.seeks                = 4;
444         bc->shrink.batch                = btree_pages(c) * 2;
445         ret = register_shrinker(&bc->shrink);
446 out:
447         pr_verbose_init(c->opts, "ret %i", ret);
448         return ret;
449 }
450
451 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
452 {
453         mutex_init(&bc->lock);
454         INIT_LIST_HEAD(&bc->live);
455         INIT_LIST_HEAD(&bc->freeable);
456         INIT_LIST_HEAD(&bc->freed);
457 }
458
459 /*
460  * We can only have one thread cannibalizing other cached btree nodes at a time,
461  * or we'll deadlock. We use an open coded mutex to ensure that, which a
462  * cannibalize_bucket() will take. This means every time we unlock the root of
463  * the btree, we need to release this lock if we have it held.
464  */
465 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
466 {
467         struct btree_cache *bc = &c->btree_cache;
468
469         if (bc->alloc_lock == current) {
470                 trace_btree_node_cannibalize_unlock(c);
471                 bc->alloc_lock = NULL;
472                 closure_wake_up(&bc->alloc_wait);
473         }
474 }
475
476 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
477 {
478         struct btree_cache *bc = &c->btree_cache;
479         struct task_struct *old;
480
481         old = cmpxchg(&bc->alloc_lock, NULL, current);
482         if (old == NULL || old == current)
483                 goto success;
484
485         if (!cl) {
486                 trace_btree_node_cannibalize_lock_fail(c);
487                 return -ENOMEM;
488         }
489
490         closure_wait(&bc->alloc_wait, cl);
491
492         /* Try again, after adding ourselves to waitlist */
493         old = cmpxchg(&bc->alloc_lock, NULL, current);
494         if (old == NULL || old == current) {
495                 /* We raced */
496                 closure_wake_up(&bc->alloc_wait);
497                 goto success;
498         }
499
500         trace_btree_node_cannibalize_lock_fail(c);
501         return -EAGAIN;
502
503 success:
504         trace_btree_node_cannibalize_lock(c);
505         return 0;
506 }
507
508 static struct btree *btree_node_cannibalize(struct bch_fs *c)
509 {
510         struct btree_cache *bc = &c->btree_cache;
511         struct btree *b;
512
513         list_for_each_entry_reverse(b, &bc->live, list)
514                 if (!btree_node_reclaim(c, b))
515                         return b;
516
517         while (1) {
518                 list_for_each_entry_reverse(b, &bc->live, list)
519                         if (!btree_node_write_and_reclaim(c, b))
520                                 return b;
521
522                 /*
523                  * Rare case: all nodes were intent-locked.
524                  * Just busy-wait.
525                  */
526                 WARN_ONCE(1, "btree cache cannibalize failed\n");
527                 cond_resched();
528         }
529 }
530
531 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
532 {
533         struct btree_cache *bc = &c->btree_cache;
534         struct btree *b;
535         u64 start_time = local_clock();
536         unsigned flags;
537
538         flags = memalloc_nofs_save();
539         mutex_lock(&bc->lock);
540
541         /*
542          * btree_free() doesn't free memory; it sticks the node on the end of
543          * the list. Check if there's any freed nodes there:
544          */
545         list_for_each_entry(b, &bc->freeable, list)
546                 if (!btree_node_reclaim(c, b))
547                         goto got_node;
548
549         /*
550          * We never free struct btree itself, just the memory that holds the on
551          * disk node. Check the freed list before allocating a new one:
552          */
553         list_for_each_entry(b, &bc->freed, list)
554                 if (!btree_node_reclaim(c, b))
555                         goto got_node;
556
557         b = NULL;
558 got_node:
559         if (b)
560                 list_del_init(&b->list);
561         mutex_unlock(&bc->lock);
562
563         if (!b) {
564                 b = __btree_node_mem_alloc(c);
565                 if (!b)
566                         goto err;
567
568                 BUG_ON(!six_trylock_intent(&b->c.lock));
569                 BUG_ON(!six_trylock_write(&b->c.lock));
570         }
571
572         if (!b->data) {
573                 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
574                         goto err;
575
576                 mutex_lock(&bc->lock);
577                 bc->used++;
578                 mutex_unlock(&bc->lock);
579         }
580
581         BUG_ON(btree_node_hashed(b));
582         BUG_ON(btree_node_write_in_flight(b));
583 out:
584         b->flags                = 0;
585         b->written              = 0;
586         b->nsets                = 0;
587         b->sib_u64s[0]          = 0;
588         b->sib_u64s[1]          = 0;
589         b->whiteout_u64s        = 0;
590         bch2_btree_keys_init(b);
591
592         bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
593                                start_time);
594
595         memalloc_nofs_restore(flags);
596         return b;
597 err:
598         mutex_lock(&bc->lock);
599
600         if (b) {
601                 list_add(&b->list, &bc->freed);
602                 six_unlock_write(&b->c.lock);
603                 six_unlock_intent(&b->c.lock);
604         }
605
606         /* Try to cannibalize another cached btree node: */
607         if (bc->alloc_lock == current) {
608                 b = btree_node_cannibalize(c);
609                 list_del_init(&b->list);
610                 mutex_unlock(&bc->lock);
611
612                 bch2_btree_node_hash_remove(bc, b);
613
614                 trace_btree_node_cannibalize(c);
615                 goto out;
616         }
617
618         mutex_unlock(&bc->lock);
619         memalloc_nofs_restore(flags);
620         return ERR_PTR(-ENOMEM);
621 }
622
623 /* Slowpath, don't want it inlined into btree_iter_traverse() */
624 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
625                                 struct btree_iter *iter,
626                                 const struct bkey_i *k,
627                                 enum btree_id btree_id,
628                                 unsigned level,
629                                 enum six_lock_type lock_type,
630                                 bool sync)
631 {
632         struct btree_cache *bc = &c->btree_cache;
633         struct btree *b;
634
635         BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
636         /*
637          * Parent node must be locked, else we could read in a btree node that's
638          * been freed:
639          */
640         if (iter && !bch2_btree_node_relock(iter, level + 1))
641                 return ERR_PTR(-EINTR);
642
643         b = bch2_btree_node_mem_alloc(c);
644         if (IS_ERR(b))
645                 return b;
646
647         bkey_copy(&b->key, k);
648         if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
649                 /* raced with another fill: */
650
651                 /* mark as unhashed... */
652                 b->hash_val = 0;
653
654                 mutex_lock(&bc->lock);
655                 list_add(&b->list, &bc->freeable);
656                 mutex_unlock(&bc->lock);
657
658                 six_unlock_write(&b->c.lock);
659                 six_unlock_intent(&b->c.lock);
660                 return NULL;
661         }
662
663         /*
664          * Unlock before doing IO:
665          *
666          * XXX: ideally should be dropping all btree node locks here
667          */
668         if (iter && btree_node_read_locked(iter, level + 1))
669                 btree_node_unlock(iter, level + 1);
670
671         bch2_btree_node_read(c, b, sync);
672
673         six_unlock_write(&b->c.lock);
674
675         if (!sync) {
676                 six_unlock_intent(&b->c.lock);
677                 return NULL;
678         }
679
680         if (lock_type == SIX_LOCK_read)
681                 six_lock_downgrade(&b->c.lock);
682
683         return b;
684 }
685
686 static int lock_node_check_fn(struct six_lock *lock, void *p)
687 {
688         struct btree *b = container_of(lock, struct btree, c.lock);
689         const struct bkey_i *k = p;
690
691         return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
692 }
693
694 /**
695  * bch_btree_node_get - find a btree node in the cache and lock it, reading it
696  * in from disk if necessary.
697  *
698  * If IO is necessary and running under generic_make_request, returns -EAGAIN.
699  *
700  * The btree node will have either a read or a write lock held, depending on
701  * the @write parameter.
702  */
703 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
704                                   const struct bkey_i *k, unsigned level,
705                                   enum six_lock_type lock_type,
706                                   unsigned long trace_ip)
707 {
708         struct btree_cache *bc = &c->btree_cache;
709         struct btree *b;
710         struct bset_tree *t;
711
712         EBUG_ON(level >= BTREE_MAX_DEPTH);
713
714         b = btree_node_mem_ptr(k);
715         if (b)
716                 goto lock_node;
717 retry:
718         b = btree_cache_find(bc, k);
719         if (unlikely(!b)) {
720                 /*
721                  * We must have the parent locked to call bch2_btree_node_fill(),
722                  * else we could read in a btree node from disk that's been
723                  * freed:
724                  */
725                 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
726                                          level, lock_type, true);
727
728                 /* We raced and found the btree node in the cache */
729                 if (!b)
730                         goto retry;
731
732                 if (IS_ERR(b))
733                         return b;
734         } else {
735 lock_node:
736                 /*
737                  * There's a potential deadlock with splits and insertions into
738                  * interior nodes we have to avoid:
739                  *
740                  * The other thread might be holding an intent lock on the node
741                  * we want, and they want to update its parent node so they're
742                  * going to upgrade their intent lock on the parent node to a
743                  * write lock.
744                  *
745                  * But if we're holding a read lock on the parent, and we're
746                  * trying to get the intent lock they're holding, we deadlock.
747                  *
748                  * So to avoid this we drop the read locks on parent nodes when
749                  * we're starting to take intent locks - and handle the race.
750                  *
751                  * The race is that they might be about to free the node we
752                  * want, and dropping our read lock on the parent node lets them
753                  * update the parent marking the node we want as freed, and then
754                  * free it:
755                  *
756                  * To guard against this, btree nodes are evicted from the cache
757                  * when they're freed - and b->hash_val is zeroed out, which we
758                  * check for after we lock the node.
759                  *
760                  * Then, bch2_btree_node_relock() on the parent will fail - because
761                  * the parent was modified, when the pointer to the node we want
762                  * was removed - and we'll bail out:
763                  */
764                 if (btree_node_read_locked(iter, level + 1))
765                         btree_node_unlock(iter, level + 1);
766
767                 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
768                                      lock_node_check_fn, (void *) k, trace_ip)) {
769                         if (b->hash_val != btree_ptr_hash_val(k))
770                                 goto retry;
771                         return ERR_PTR(-EINTR);
772                 }
773
774                 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
775                              b->c.level != level ||
776                              race_fault())) {
777                         six_unlock_type(&b->c.lock, lock_type);
778                         if (bch2_btree_node_relock(iter, level + 1))
779                                 goto retry;
780
781                         trace_trans_restart_btree_node_reused(iter->trans->ip);
782                         return ERR_PTR(-EINTR);
783                 }
784         }
785
786         /* XXX: waiting on IO with btree locks held: */
787         wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
788                        TASK_UNINTERRUPTIBLE);
789
790         prefetch(b->aux_data);
791
792         for_each_bset(b, t) {
793                 void *p = (u64 *) b->aux_data + t->aux_data_offset;
794
795                 prefetch(p + L1_CACHE_BYTES * 0);
796                 prefetch(p + L1_CACHE_BYTES * 1);
797                 prefetch(p + L1_CACHE_BYTES * 2);
798         }
799
800         /* avoid atomic set bit if it's not needed: */
801         if (!btree_node_accessed(b))
802                 set_btree_node_accessed(b);
803
804         if (unlikely(btree_node_read_error(b))) {
805                 six_unlock_type(&b->c.lock, lock_type);
806                 return ERR_PTR(-EIO);
807         }
808
809         EBUG_ON(b->c.btree_id != iter->btree_id);
810         EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
811         EBUG_ON(bkey_cmp(b->data->max_key, k->k.p));
812         EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
813                 bkey_cmp(b->data->min_key,
814                          bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
815
816         return b;
817 }
818
819 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
820                                          const struct bkey_i *k,
821                                          enum btree_id btree_id,
822                                          unsigned level,
823                                          bool nofill)
824 {
825         struct btree_cache *bc = &c->btree_cache;
826         struct btree *b;
827         struct bset_tree *t;
828         int ret;
829
830         EBUG_ON(level >= BTREE_MAX_DEPTH);
831
832         b = btree_node_mem_ptr(k);
833         if (b)
834                 goto lock_node;
835 retry:
836         b = btree_cache_find(bc, k);
837         if (unlikely(!b)) {
838                 if (nofill)
839                         goto out;
840
841                 b = bch2_btree_node_fill(c, NULL, k, btree_id,
842                                          level, SIX_LOCK_read, true);
843
844                 /* We raced and found the btree node in the cache */
845                 if (!b)
846                         goto retry;
847
848                 if (IS_ERR(b) &&
849                     !bch2_btree_cache_cannibalize_lock(c, NULL))
850                         goto retry;
851
852                 if (IS_ERR(b))
853                         goto out;
854         } else {
855 lock_node:
856                 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
857                 if (ret)
858                         goto retry;
859
860                 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
861                              b->c.btree_id != btree_id ||
862                              b->c.level != level)) {
863                         six_unlock_read(&b->c.lock);
864                         goto retry;
865                 }
866         }
867
868         /* XXX: waiting on IO with btree locks held: */
869         wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
870                        TASK_UNINTERRUPTIBLE);
871
872         prefetch(b->aux_data);
873
874         for_each_bset(b, t) {
875                 void *p = (u64 *) b->aux_data + t->aux_data_offset;
876
877                 prefetch(p + L1_CACHE_BYTES * 0);
878                 prefetch(p + L1_CACHE_BYTES * 1);
879                 prefetch(p + L1_CACHE_BYTES * 2);
880         }
881
882         /* avoid atomic set bit if it's not needed: */
883         if (!btree_node_accessed(b))
884                 set_btree_node_accessed(b);
885
886         if (unlikely(btree_node_read_error(b))) {
887                 six_unlock_read(&b->c.lock);
888                 b = ERR_PTR(-EIO);
889                 goto out;
890         }
891
892         EBUG_ON(b->c.btree_id != btree_id);
893         EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
894         EBUG_ON(bkey_cmp(b->data->max_key, k->k.p));
895         EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
896                 bkey_cmp(b->data->min_key,
897                          bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
898 out:
899         bch2_btree_cache_cannibalize_unlock(c);
900         return b;
901 }
902
903 struct btree *bch2_btree_node_get_sibling(struct bch_fs *c,
904                                           struct btree_iter *iter,
905                                           struct btree *b,
906                                           enum btree_node_sibling sib)
907 {
908         struct btree_trans *trans = iter->trans;
909         struct btree *parent;
910         struct btree_node_iter node_iter;
911         struct bkey_packed *k;
912         struct bkey_buf tmp;
913         struct btree *ret = NULL;
914         unsigned level = b->c.level;
915
916         bch2_bkey_buf_init(&tmp);
917
918         parent = btree_iter_node(iter, level + 1);
919         if (!parent)
920                 return NULL;
921
922         /*
923          * There's a corner case where a btree_iter might have a node locked
924          * that is just outside its current pos - when
925          * bch2_btree_iter_set_pos_same_leaf() gets to the end of the node.
926          *
927          * But the lock ordering checks in __bch2_btree_node_lock() go off of
928          * iter->pos, not the node's key: so if the iterator is marked as
929          * needing to be traversed, we risk deadlock if we don't bail out here:
930          */
931         if (iter->uptodate >= BTREE_ITER_NEED_TRAVERSE)
932                 return ERR_PTR(-EINTR);
933
934         if (!bch2_btree_node_relock(iter, level + 1)) {
935                 ret = ERR_PTR(-EINTR);
936                 goto out;
937         }
938
939         node_iter = iter->l[parent->c.level].iter;
940
941         k = bch2_btree_node_iter_peek_all(&node_iter, parent);
942         BUG_ON(bkey_cmp_left_packed(parent, k, &b->key.k.p));
943
944         k = sib == btree_prev_sib
945                 ? bch2_btree_node_iter_prev(&node_iter, parent)
946                 : (bch2_btree_node_iter_advance(&node_iter, parent),
947                    bch2_btree_node_iter_peek(&node_iter, parent));
948         if (!k)
949                 goto out;
950
951         bch2_bkey_buf_unpack(&tmp, c, parent, k);
952
953         ret = bch2_btree_node_get(c, iter, tmp.k, level,
954                                   SIX_LOCK_intent, _THIS_IP_);
955
956         if (PTR_ERR_OR_ZERO(ret) == -EINTR && !trans->nounlock) {
957                 struct btree_iter *linked;
958
959                 if (!bch2_btree_node_relock(iter, level + 1))
960                         goto out;
961
962                 /*
963                  * We might have got -EINTR because trylock failed, and we're
964                  * holding other locks that would cause us to deadlock:
965                  */
966                 trans_for_each_iter(trans, linked)
967                         if (btree_iter_lock_cmp(iter, linked) < 0)
968                                 __bch2_btree_iter_unlock(linked);
969
970                 if (sib == btree_prev_sib)
971                         btree_node_unlock(iter, level);
972
973                 ret = bch2_btree_node_get(c, iter, tmp.k, level,
974                                           SIX_LOCK_intent, _THIS_IP_);
975
976                 /*
977                  * before btree_iter_relock() calls btree_iter_verify_locks():
978                  */
979                 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
980                         btree_node_unlock(iter, level + 1);
981
982                 if (!bch2_btree_node_relock(iter, level)) {
983                         btree_iter_set_dirty(iter, BTREE_ITER_NEED_RELOCK);
984
985                         if (!IS_ERR(ret)) {
986                                 six_unlock_intent(&ret->c.lock);
987                                 ret = ERR_PTR(-EINTR);
988                         }
989                 }
990
991                 bch2_trans_relock(trans);
992         }
993 out:
994         if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
995                 btree_node_unlock(iter, level + 1);
996
997         if (PTR_ERR_OR_ZERO(ret) == -EINTR)
998                 bch2_btree_iter_upgrade(iter, level + 2);
999
1000         BUG_ON(!IS_ERR(ret) && !btree_node_locked(iter, level));
1001
1002         if (!IS_ERR_OR_NULL(ret)) {
1003                 struct btree *n1 = ret, *n2 = b;
1004
1005                 if (sib != btree_prev_sib)
1006                         swap(n1, n2);
1007
1008                 if (bkey_cmp(bkey_successor(n1->key.k.p),
1009                              n2->data->min_key)) {
1010                         char buf1[200], buf2[200];
1011
1012                         bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&n1->key));
1013                         bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(&n2->key));
1014
1015                         bch2_fs_inconsistent(c, "btree topology error at btree %s level %u:\n"
1016                                              "prev: %s\n"
1017                                              "next: %s\n",
1018                                              bch2_btree_ids[iter->btree_id], level,
1019                                              buf1, buf2);
1020
1021                         six_unlock_intent(&ret->c.lock);
1022                         ret = NULL;
1023                 }
1024         }
1025
1026         bch2_btree_trans_verify_locks(trans);
1027
1028         bch2_bkey_buf_exit(&tmp, c);
1029
1030         return ret;
1031 }
1032
1033 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
1034                               const struct bkey_i *k,
1035                               enum btree_id btree_id, unsigned level)
1036 {
1037         struct btree_cache *bc = &c->btree_cache;
1038         struct btree *b;
1039
1040         BUG_ON(iter && !btree_node_locked(iter, level + 1));
1041         BUG_ON(level >= BTREE_MAX_DEPTH);
1042
1043         b = btree_cache_find(bc, k);
1044         if (b)
1045                 return;
1046
1047         bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
1048 }
1049
1050 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1051                              struct btree *b)
1052 {
1053         const struct bkey_format *f = &b->format;
1054         struct bset_stats stats;
1055
1056         memset(&stats, 0, sizeof(stats));
1057
1058         bch2_btree_keys_stats(b, &stats);
1059
1060         pr_buf(out, "l %u ", b->c.level);
1061         bch2_bpos_to_text(out, b->data->min_key);
1062         pr_buf(out, " - ");
1063         bch2_bpos_to_text(out, b->data->max_key);
1064         pr_buf(out, ":\n"
1065                "    ptrs: ");
1066         bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1067
1068         pr_buf(out, "\n"
1069                "    format: u64s %u fields %u %u %u %u %u\n"
1070                "    unpack fn len: %u\n"
1071                "    bytes used %zu/%zu (%zu%% full)\n"
1072                "    sib u64s: %u, %u (merge threshold %zu)\n"
1073                "    nr packed keys %u\n"
1074                "    nr unpacked keys %u\n"
1075                "    floats %zu\n"
1076                "    failed unpacked %zu\n",
1077                f->key_u64s,
1078                f->bits_per_field[0],
1079                f->bits_per_field[1],
1080                f->bits_per_field[2],
1081                f->bits_per_field[3],
1082                f->bits_per_field[4],
1083                b->unpack_fn_len,
1084                b->nr.live_u64s * sizeof(u64),
1085                btree_bytes(c) - sizeof(struct btree_node),
1086                b->nr.live_u64s * 100 / btree_max_u64s(c),
1087                b->sib_u64s[0],
1088                b->sib_u64s[1],
1089                BTREE_FOREGROUND_MERGE_THRESHOLD(c),
1090                b->nr.packed_keys,
1091                b->nr.unpacked_keys,
1092                stats.floats,
1093                stats.failed);
1094 }
1095
1096 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1097 {
1098         pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1099         pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1100         pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);
1101 }