]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_cache.c
Update bcachefs sources to 787de128a5 bcachefs: Improvements to fsck check_dirents()
[bcachefs-tools-debian] / libbcachefs / btree_cache.c
index 1abc50f134e6fe4e2797f1c7445b6cb7ed85c0f6..1aacd271021b475433ddc5b546dc93744395b1bf 100644 (file)
@@ -13,6 +13,8 @@
 #include <linux/sched/mm.h>
 #include <trace/events/bcachefs.h>
 
+struct lock_class_key bch2_btree_node_lock_key;
+
 void bch2_recalc_btree_reserve(struct bch_fs *c)
 {
        unsigned i, reserve = 16;
@@ -33,21 +35,21 @@ static inline unsigned btree_cache_can_free(struct btree_cache *bc)
        return max_t(int, 0, bc->used - bc->reserve);
 }
 
-static void __btree_node_data_free(struct bch_fs *c, struct btree *b)
+static void btree_node_data_free(struct bch_fs *c, struct btree *b)
 {
+       struct btree_cache *bc = &c->btree_cache;
+
        EBUG_ON(btree_node_write_in_flight(b));
 
        kvpfree(b->data, btree_bytes(c));
        b->data = NULL;
+#ifdef __KERNEL__
        vfree(b->aux_data);
+#else
+       munmap(b->aux_data, btree_aux_data_bytes(b));
+#endif
        b->aux_data = NULL;
-}
 
-static void btree_node_data_free(struct bch_fs *c, struct btree *b)
-{
-       struct btree_cache *bc = &c->btree_cache;
-
-       __btree_node_data_free(c, b);
        bc->used--;
        list_move(&b->list, &bc->freed);
 }
@@ -75,8 +77,13 @@ static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
        b->data = kvpmalloc(btree_bytes(c), gfp);
        if (!b->data)
                return -ENOMEM;
-
+#ifdef __KERNEL__
        b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
+#else
+       b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
+                          PROT_READ|PROT_WRITE|PROT_EXEC,
+                          MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
+#endif
        if (!b->aux_data) {
                kvpfree(b->data, btree_bytes(c));
                b->data = NULL;
@@ -93,14 +100,14 @@ static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
                return NULL;
 
        bkey_btree_ptr_init(&b->key);
-       six_lock_init(&b->c.lock);
+       __six_lock_init(&b->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
        INIT_LIST_HEAD(&b->list);
        INIT_LIST_HEAD(&b->write_blocked);
        b->byte_order = ilog2(btree_bytes(c));
        return b;
 }
 
-static struct btree *btree_node_mem_alloc(struct bch_fs *c)
+struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
 {
        struct btree_cache *bc = &c->btree_cache;
        struct btree *b = __btree_node_mem_alloc(c);
@@ -179,6 +186,17 @@ static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
        int ret = 0;
 
        lockdep_assert_held(&bc->lock);
+wait_on_io:
+       if (b->flags & ((1U << BTREE_NODE_dirty)|
+                       (1U << BTREE_NODE_read_in_flight)|
+                       (1U << BTREE_NODE_write_in_flight))) {
+               if (!flush)
+                       return -ENOMEM;
+
+               /* XXX: waiting on IO with btree cache lock held */
+               bch2_btree_node_wait_on_read(b);
+               bch2_btree_node_wait_on_write(b);
+       }
 
        if (!six_trylock_intent(&b->c.lock))
                return -ENOMEM;
@@ -186,25 +204,26 @@ static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
        if (!six_trylock_write(&b->c.lock))
                goto out_unlock_intent;
 
+       /* recheck under lock */
+       if (b->flags & ((1U << BTREE_NODE_read_in_flight)|
+                       (1U << BTREE_NODE_write_in_flight))) {
+               if (!flush)
+                       goto out_unlock;
+               six_unlock_write(&b->c.lock);
+               six_unlock_intent(&b->c.lock);
+               goto wait_on_io;
+       }
+
        if (btree_node_noevict(b))
                goto out_unlock;
 
        if (!btree_node_may_write(b))
                goto out_unlock;
 
-       if (btree_node_dirty(b) &&
-           test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
-               goto out_unlock;
-
-       if (btree_node_dirty(b) ||
-           btree_node_write_in_flight(b) ||
-           btree_node_read_in_flight(b)) {
-               if (!flush)
+       if (btree_node_dirty(b)) {
+               if (!flush ||
+                   test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
                        goto out_unlock;
-
-               wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
-                              TASK_UNINTERRUPTIBLE);
-
                /*
                 * Using the underscore version because we don't want to compact
                 * bsets after the write, since this node is about to be evicted
@@ -214,10 +233,11 @@ static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
                if (bch2_verify_btree_ondisk)
                        bch2_btree_node_write(c, b, SIX_LOCK_intent);
                else
-                       __bch2_btree_node_write(c, b, SIX_LOCK_read);
+                       __bch2_btree_node_write(c, b, false);
 
-               /* wait for any in flight btree write */
-               btree_node_wait_on_io(b);
+               six_unlock_write(&b->c.lock);
+               six_unlock_intent(&b->c.lock);
+               goto wait_on_io;
        }
 out:
        if (b->hash_val && !ret)
@@ -360,12 +380,10 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
        flags = memalloc_nofs_save();
        mutex_lock(&bc->lock);
 
-#ifdef CONFIG_BCACHEFS_DEBUG
        if (c->verify_data)
                list_move(&c->verify_data->list, &bc->live);
 
        kvpfree(c->verify_ondisk, btree_bytes(c));
-#endif
 
        for (i = 0; i < BTREE_ID_NR; i++)
                if (c->btree_roots[i].b)
@@ -419,31 +437,15 @@ int bch2_fs_btree_cache_init(struct bch_fs *c)
        bch2_recalc_btree_reserve(c);
 
        for (i = 0; i < bc->reserve; i++)
-               if (!btree_node_mem_alloc(c)) {
+               if (!__bch2_btree_node_mem_alloc(c)) {
                        ret = -ENOMEM;
                        goto out;
                }
 
        list_splice_init(&bc->live, &bc->freeable);
 
-#ifdef CONFIG_BCACHEFS_DEBUG
        mutex_init(&c->verify_lock);
 
-       c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
-       if (!c->verify_ondisk) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
-       c->verify_data = btree_node_mem_alloc(c);
-       if (!c->verify_data) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
-       list_del_init(&c->verify_data->list);
-#endif
-
        bc->shrink.count_objects        = bch2_btree_cache_count;
        bc->shrink.scan_objects         = bch2_btree_cache_scan;
        bc->shrink.seeks                = 4;
@@ -585,6 +587,7 @@ got_node:
        }
 
        BUG_ON(btree_node_hashed(b));
+       BUG_ON(btree_node_dirty(b));
        BUG_ON(btree_node_write_in_flight(b));
 out:
        b->flags                = 0;
@@ -594,6 +597,7 @@ out:
        b->sib_u64s[1]          = 0;
        b->whiteout_u64s        = 0;
        bch2_btree_keys_init(b);
+       set_btree_node_accessed(b);
 
        bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
                               start_time);
@@ -637,6 +641,7 @@ static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
 {
        struct btree_cache *bc = &c->btree_cache;
        struct btree *b;
+       u32 seq;
 
        BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
        /*
@@ -666,25 +671,33 @@ static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
                return NULL;
        }
 
-       /*
-        * Unlock before doing IO:
-        *
-        * XXX: ideally should be dropping all btree node locks here
-        */
-       if (iter && btree_node_read_locked(iter, level + 1))
-               btree_node_unlock(iter, level + 1);
-
-       bch2_btree_node_read(c, b, sync);
+       set_btree_node_read_in_flight(b);
 
        six_unlock_write(&b->c.lock);
+       seq = b->c.lock.state.seq;
+       six_unlock_intent(&b->c.lock);
 
-       if (!sync) {
-               six_unlock_intent(&b->c.lock);
+       /* Unlock before doing IO: */
+       if (iter && sync)
+               bch2_trans_unlock(iter->trans);
+
+       bch2_btree_node_read(c, b, sync);
+
+       if (!sync)
                return NULL;
-       }
 
-       if (lock_type == SIX_LOCK_read)
-               six_lock_downgrade(&b->c.lock);
+       /*
+        * XXX: this will probably always fail because btree_iter_relock()
+        * currently fails for iterators that aren't pointed at a valid btree
+        * node
+        */
+       if (iter &&
+           (!bch2_trans_relock(iter->trans) ||
+            !bch2_btree_iter_relock(iter, _THIS_IP_)))
+               return ERR_PTR(-EINTR);
+
+       if (!six_relock_type(&b->c.lock, lock_type, seq))
+               return ERR_PTR(-EINTR);
 
        return b;
 }
@@ -697,6 +710,41 @@ static int lock_node_check_fn(struct six_lock *lock, void *p)
        return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
 }
 
+static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
+{
+       char buf1[100], buf2[100], buf3[100], buf4[100];
+
+       if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
+               return;
+
+       bch2_bpos_to_text(&PBUF(buf1), b->key.k.type == KEY_TYPE_btree_ptr_v2
+               ? bkey_i_to_btree_ptr_v2(&b->key)->v.min_key
+               : POS_MIN);
+       bch2_bpos_to_text(&PBUF(buf2), b->data->min_key);
+
+       bch2_bpos_to_text(&PBUF(buf3), b->key.k.p);
+       bch2_bpos_to_text(&PBUF(buf4), b->data->max_key);
+       bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
+                            "btree: ptr %u header %llu\n"
+                            "level: ptr %u header %llu\n"
+                            "min ptr %s node header %s\n"
+                            "max ptr %s node header %s",
+                            b->c.btree_id,     BTREE_NODE_ID(b->data),
+                            b->c.level,        BTREE_NODE_LEVEL(b->data),
+                            buf1, buf2, buf3, buf4);
+}
+
+static inline void btree_check_header(struct bch_fs *c, struct btree *b)
+{
+       if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
+           b->c.level != BTREE_NODE_LEVEL(b->data) ||
+           bpos_cmp(b->data->max_key, b->key.k.p) ||
+           (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
+            bpos_cmp(b->data->min_key,
+                     bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
+               btree_bad_header(c, b);
+}
+
 /**
  * bch_btree_node_get - find a btree node in the cache and lock it, reading it
  * in from disk if necessary.
@@ -784,14 +832,35 @@ lock_node:
                        if (bch2_btree_node_relock(iter, level + 1))
                                goto retry;
 
-                       trace_trans_restart_btree_node_reused(iter->trans->ip);
+                       trace_trans_restart_btree_node_reused(iter->trans->ip,
+                                                             trace_ip,
+                                                             iter->btree_id,
+                                                             &iter->real_pos);
                        return ERR_PTR(-EINTR);
                }
        }
 
-       /* XXX: waiting on IO with btree locks held: */
-       wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
-                      TASK_UNINTERRUPTIBLE);
+       if (unlikely(btree_node_read_in_flight(b))) {
+               u32 seq = b->c.lock.state.seq;
+
+               six_unlock_type(&b->c.lock, lock_type);
+               bch2_trans_unlock(iter->trans);
+
+               bch2_btree_node_wait_on_read(b);
+
+               /*
+                * XXX: check if this always fails - btree_iter_relock()
+                * currently fails for iterators that aren't pointed at a valid
+                * btree node
+                */
+               if (iter &&
+                   (!bch2_trans_relock(iter->trans) ||
+                    !bch2_btree_iter_relock(iter, _THIS_IP_)))
+                       return ERR_PTR(-EINTR);
+
+               if (!six_relock_type(&b->c.lock, lock_type, seq))
+                       goto retry;
+       }
 
        prefetch(b->aux_data);
 
@@ -814,10 +883,7 @@ lock_node:
 
        EBUG_ON(b->c.btree_id != iter->btree_id);
        EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
-       EBUG_ON(bpos_cmp(b->data->max_key, k->k.p));
-       EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
-               bpos_cmp(b->data->min_key,
-                        bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
+       btree_check_header(c, b);
 
        return b;
 }
@@ -872,8 +938,7 @@ lock_node:
        }
 
        /* XXX: waiting on IO with btree locks held: */
-       wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
-                      TASK_UNINTERRUPTIBLE);
+       __bch2_btree_node_wait_on_read(b);
 
        prefetch(b->aux_data);
 
@@ -897,10 +962,7 @@ lock_node:
 
        EBUG_ON(b->c.btree_id != btree_id);
        EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
-       EBUG_ON(bpos_cmp(b->data->max_key, k->k.p));
-       EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
-               bpos_cmp(b->data->min_key,
-                        bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
+       btree_check_header(c, b);
 out:
        bch2_btree_cache_cannibalize_unlock(c);
        return b;
@@ -923,6 +985,44 @@ void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
        bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
 }
 
+void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
+{
+       struct btree_cache *bc = &c->btree_cache;
+       struct btree *b;
+
+       b = btree_cache_find(bc, k);
+       if (!b)
+               return;
+wait_on_io:
+       /* not allowed to wait on io with btree locks held: */
+
+       /* XXX we're called from btree_gc which will be holding other btree
+        * nodes locked
+        * */
+       __bch2_btree_node_wait_on_read(b);
+       __bch2_btree_node_wait_on_write(b);
+
+       six_lock_intent(&b->c.lock, NULL, NULL);
+       six_lock_write(&b->c.lock, NULL, NULL);
+
+       if (btree_node_dirty(b)) {
+               __bch2_btree_node_write(c, b, false);
+               six_unlock_write(&b->c.lock);
+               six_unlock_intent(&b->c.lock);
+               goto wait_on_io;
+       }
+
+       BUG_ON(btree_node_dirty(b));
+
+       mutex_lock(&bc->lock);
+       btree_node_data_free(c, b);
+       bch2_btree_node_hash_remove(bc, b);
+       mutex_unlock(&bc->lock);
+
+       six_unlock_write(&b->c.lock);
+       six_unlock_intent(&b->c.lock);
+}
+
 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
                             struct btree *b)
 {