]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_cache.c
Update bcachefs sources to b9bd69421f73 bcachefs: x-macro-ify inode flags enum
[bcachefs-tools-debian] / libbcachefs / btree_cache.c
index 73d326880cbb1168986f011d22a64b4fea9c8167..84636ad034fa84cdfaecf654cc9b17eeb79bd04a 100644 (file)
@@ -32,13 +32,15 @@ void bch2_recalc_btree_reserve(struct bch_fs *c)
 {
        unsigned i, reserve = 16;
 
-       if (!c->btree_roots[0].b)
+       if (!c->btree_roots_known[0].b)
                reserve += 8;
 
-       for (i = 0; i < BTREE_ID_NR; i++)
-               if (c->btree_roots[i].b)
-                       reserve += min_t(unsigned, 1,
-                                        c->btree_roots[i].b->c.level) * 8;
+       for (i = 0; i < btree_id_nr_alive(c); i++) {
+               struct btree_root *r = bch2_btree_id_root(c, i);
+
+               if (r->b)
+                       reserve += min_t(unsigned, 1, r->b->c.level) * 8;
+       }
 
        c->btree_cache.reserve = reserve;
 }
@@ -62,10 +64,12 @@ static void btree_node_data_free(struct bch_fs *c, struct btree *b)
 
        EBUG_ON(btree_node_write_in_flight(b));
 
+       clear_btree_node_just_written(b);
+
        kvpfree(b->data, btree_bytes(c));
        b->data = NULL;
 #ifdef __KERNEL__
-       vfree(b->aux_data);
+       kvfree(b->aux_data);
 #else
        munmap(b->aux_data, btree_aux_data_bytes(b));
 #endif
@@ -100,7 +104,7 @@ static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
        if (!b->data)
                return -BCH_ERR_ENOMEM_btree_node_mem_alloc;
 #ifdef __KERNEL__
-       b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
+       b->aux_data = kvmalloc(btree_aux_data_bytes(b), gfp);
 #else
        b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
                           PROT_READ|PROT_WRITE|PROT_EXEC,
@@ -126,10 +130,6 @@ static struct btree *__btree_node_mem_alloc(struct bch_fs *c, gfp_t gfp)
                return NULL;
 
        bkey_btree_ptr_init(&b->key);
-       bch2_btree_lock_init(&b->c);
-#ifdef CONFIG_DEBUG_LOCK_ALLOC
-       lockdep_set_no_check_recursion(&b->c.lock.dep_map);
-#endif
        INIT_LIST_HEAD(&b->list);
        INIT_LIST_HEAD(&b->write_blocked);
        b->byte_order = ilog2(btree_bytes(c));
@@ -150,6 +150,8 @@ struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
                return NULL;
        }
 
+       bch2_btree_lock_init(&b->c, 0);
+
        bc->used++;
        list_add(&b->list, &bc->freeable);
        return b;
@@ -446,8 +448,7 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
        struct btree *b;
        unsigned i, flags;
 
-       if (bc->shrink.list.next)
-               unregister_shrinker(&bc->shrink);
+       unregister_shrinker(&bc->shrink);
 
        /* vfree() can allocate memory: */
        flags = memalloc_nofs_save();
@@ -458,9 +459,12 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
 
        kvpfree(c->verify_ondisk, btree_bytes(c));
 
-       for (i = 0; i < BTREE_ID_NR; i++)
-               if (c->btree_roots[i].b)
-                       list_add(&c->btree_roots[i].b->list, &bc->live);
+       for (i = 0; i < btree_id_nr_alive(c); i++) {
+               struct btree_root *r = bch2_btree_id_root(c, i);
+
+               if (r->b)
+                       list_add(&r->b->list, &bc->live);
+       }
 
        list_splice(&bc->freeable, &bc->live);
 
@@ -484,7 +488,7 @@ void bch2_fs_btree_cache_exit(struct bch_fs *c)
        while (!list_empty(&bc->freed_nonpcpu)) {
                b = list_first_entry(&bc->freed_nonpcpu, struct btree, list);
                list_del(&b->list);
-               six_lock_pcpu_free(&b->c.lock);
+               six_lock_exit(&b->c.lock);
                kfree(b);
        }
 
@@ -501,21 +505,17 @@ int bch2_fs_btree_cache_init(struct bch_fs *c)
        unsigned i;
        int ret = 0;
 
-       pr_verbose_init(c->opts, "");
-
        ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
        if (ret)
-               goto out;
+               goto err;
 
        bc->table_init_done = true;
 
        bch2_recalc_btree_reserve(c);
 
        for (i = 0; i < bc->reserve; i++)
-               if (!__bch2_btree_node_mem_alloc(c)) {
-                       ret = -BCH_ERR_ENOMEM_fs_btree_cache_init;
-                       goto out;
-               }
+               if (!__bch2_btree_node_mem_alloc(c))
+                       goto err;
 
        list_splice_init(&bc->live, &bc->freeable);
 
@@ -525,10 +525,13 @@ int bch2_fs_btree_cache_init(struct bch_fs *c)
        bc->shrink.scan_objects         = bch2_btree_cache_scan;
        bc->shrink.to_text              = bch2_btree_cache_shrinker_to_text;
        bc->shrink.seeks                = 4;
-       ret = register_shrinker(&bc->shrink, "%s/btree_cache", c->name);
-out:
-       pr_verbose_init(c->opts, "ret %i", ret);
-       return ret;
+       ret = register_shrinker(&bc->shrink, "%s-btree_cache", c->name);
+       if (ret)
+               goto err;
+
+       return 0;
+err:
+       return -BCH_ERR_ENOMEM_fs_btree_cache_init;
 }
 
 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
@@ -636,17 +639,17 @@ struct btree *bch2_btree_node_mem_alloc(struct btree_trans *trans, bool pcpu_rea
                        goto got_node;
                }
 
-       b = __btree_node_mem_alloc(c, __GFP_NOWARN);
+       b = __btree_node_mem_alloc(c, GFP_NOWAIT|__GFP_NOWARN);
        if (!b) {
                mutex_unlock(&bc->lock);
+               bch2_trans_unlock(trans);
                b = __btree_node_mem_alloc(c, GFP_KERNEL);
                if (!b)
                        goto err;
                mutex_lock(&bc->lock);
        }
 
-       if (pcpu_read_locks)
-               six_lock_pcpu_alloc(&b->c.lock);
+       bch2_btree_lock_init(&b->c, pcpu_read_locks ? SIX_LOCK_INIT_PCPU : 0);
 
        BUG_ON(!six_trylock_intent(&b->c.lock));
        BUG_ON(!six_trylock_write(&b->c.lock));
@@ -668,8 +671,11 @@ got_node:
 
        mutex_unlock(&bc->lock);
 
-       if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
-               goto err;
+       if (btree_node_data_alloc(c, b, GFP_NOWAIT|__GFP_NOWARN)) {
+               bch2_trans_unlock(trans);
+               if (btree_node_data_alloc(c, b, GFP_KERNEL|__GFP_NOWARN))
+                       goto err;
+       }
 
        mutex_lock(&bc->lock);
        bc->used++;
@@ -700,6 +706,7 @@ err:
        /* Try to cannibalize another cached btree node: */
        if (bc->alloc_lock == current) {
                b2 = btree_node_cannibalize(c);
+               clear_btree_node_just_written(b2);
                bch2_btree_node_hash_remove(bc, b2);
 
                if (b) {
@@ -784,12 +791,12 @@ static noinline struct btree *bch2_btree_node_fill(struct btree_trans *trans,
        set_btree_node_read_in_flight(b);
 
        six_unlock_write(&b->c.lock);
-       seq = b->c.lock.state.seq;
+       seq = six_lock_seq(&b->c.lock);
        six_unlock_intent(&b->c.lock);
 
        /* Unlock before doing IO: */
-       if (trans && sync)
-               bch2_trans_unlock(trans);
+       if (path && sync)
+               bch2_trans_unlock_noassert(trans);
 
        bch2_btree_node_read(c, b, sync);
 
@@ -818,19 +825,19 @@ static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
 {
        struct printbuf buf = PRINTBUF;
 
-       if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
+       if (c->curr_recovery_pass <= BCH_RECOVERY_PASS_check_allocations)
                return;
 
        prt_printf(&buf,
               "btree node header doesn't match ptr\n"
               "btree %s level %u\n"
               "ptr: ",
-              bch2_btree_ids[b->c.btree_id], b->c.level);
+              bch2_btree_id_str(b->c.btree_id), b->c.level);
        bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
 
        prt_printf(&buf, "\nheader: btree %s level %llu\n"
               "min ",
-              bch2_btree_ids[BTREE_NODE_ID(b->data)],
+              bch2_btree_id_str(BTREE_NODE_ID(b->data)),
               BTREE_NODE_LEVEL(b->data));
        bch2_bpos_to_text(&buf, b->data->min_key);
 
@@ -861,6 +868,7 @@ static struct btree *__bch2_btree_node_get(struct btree_trans *trans, struct btr
        struct btree_cache *bc = &c->btree_cache;
        struct btree *b;
        struct bset_tree *t;
+       bool need_relock = false;
        int ret;
 
        EBUG_ON(level >= BTREE_MAX_DEPTH);
@@ -874,6 +882,7 @@ retry:
                 */
                b = bch2_btree_node_fill(trans, path, k, path->btree_id,
                                         level, lock_type, true);
+               need_relock = true;
 
                /* We raced and found the btree node in the cache */
                if (!b)
@@ -908,10 +917,11 @@ retry:
        }
 
        if (unlikely(btree_node_read_in_flight(b))) {
-               u32 seq = b->c.lock.state.seq;
+               u32 seq = six_lock_seq(&b->c.lock);
 
                six_unlock_type(&b->c.lock, lock_type);
                bch2_trans_unlock(trans);
+               need_relock = true;
 
                bch2_btree_node_wait_on_read(b);
 
@@ -919,19 +929,19 @@ retry:
                 * should_be_locked is not set on this path yet, so we need to
                 * relock it specifically:
                 */
-               if (trans) {
-                       int ret = bch2_trans_relock(trans) ?:
-                               bch2_btree_path_relock_intent(trans, path);
-                       if (ret) {
-                               BUG_ON(!trans->restarted);
-                               return ERR_PTR(ret);
-                       }
-               }
-
                if (!six_relock_type(&b->c.lock, lock_type, seq))
                        goto retry;
        }
 
+       if (unlikely(need_relock)) {
+               ret = bch2_trans_relock(trans) ?:
+                       bch2_btree_path_relock_intent(trans, path);
+               if (ret) {
+                       six_unlock_type(&b->c.lock, lock_type);
+                       return ERR_PTR(ret);
+               }
+       }
+
        prefetch(b->aux_data);
 
        for_each_bset(b, t) {
@@ -955,11 +965,20 @@ retry:
 }
 
 /**
- * bch_btree_node_get - find a btree node in the cache and lock it, reading it
+ * bch2_btree_node_get - find a btree node in the cache and lock it, reading it
  * in from disk if necessary.
  *
+ * @trans:     btree transaction object
+ * @path:      btree_path being traversed
+ * @k:         pointer to btree node (generally KEY_TYPE_btree_ptr_v2)
+ * @level:     level of btree node being looked up (0 == leaf node)
+ * @lock_type: SIX_LOCK_read or SIX_LOCK_intent
+ * @trace_ip:  ip of caller of btree iterator code (i.e. caller of bch2_btree_iter_peek())
+ *
  * The btree node will have either a read or a write lock held, depending on
  * the @write parameter.
+ *
+ * Returns: btree node or ERR_PTR()
  */
 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *path,
                                  const struct bkey_i *k, unsigned level,
@@ -1006,28 +1025,8 @@ struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *
        }
 
        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(trans);
-
-               bch2_btree_node_wait_on_read(b);
-
-               /*
-                * should_be_locked is not set on this path yet, so we need to
-                * relock it specifically:
-                */
-               if (trans) {
-                       int ret = bch2_trans_relock(trans) ?:
-                               bch2_btree_path_relock_intent(trans, path);
-                       if (ret) {
-                               BUG_ON(!trans->restarted);
-                               return ERR_PTR(ret);
-                       }
-               }
-
-               if (!six_relock_type(&b->c.lock, lock_type, seq))
-                       return __bch2_btree_node_get(trans, path, k, level, lock_type, trace_ip);
+               return __bch2_btree_node_get(trans, path, k, level, lock_type, trace_ip);
        }
 
        prefetch(b->aux_data);
@@ -1201,10 +1200,22 @@ wait_on_io:
        six_unlock_intent(&b->c.lock);
 }
 
-void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
-                            const struct btree *b)
+const char *bch2_btree_id_str(enum btree_id btree)
+{
+       return btree < BTREE_ID_NR ? __bch2_btree_ids[btree] : "(unknown)";
+}
+
+void bch2_btree_pos_to_text(struct printbuf *out, struct bch_fs *c, const struct btree *b)
+{
+       prt_printf(out, "%s level %u/%u\n  ",
+              bch2_btree_id_str(b->c.btree_id),
+              b->c.level,
+              bch2_btree_id_root(c, b->c.btree_id)->level);
+       bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(&b->key));
+}
+
+void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c, const struct btree *b)
 {
-       const struct bkey_format *f = &b->format;
        struct bset_stats stats;
 
        memset(&stats, 0, sizeof(stats));
@@ -1218,9 +1229,13 @@ void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
        prt_printf(out, ":\n"
               "    ptrs: ");
        bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
+       prt_newline(out);
+
+       prt_printf(out,
+              "    format: ");
+       bch2_bkey_format_to_text(out, &b->format);
 
-       prt_printf(out, "\n"
-              "    format: u64s %u fields %u %u %u %u %u\n"
+       prt_printf(out,
               "    unpack fn len: %u\n"
               "    bytes used %zu/%zu (%zu%% full)\n"
               "    sib u64s: %u, %u (merge threshold %u)\n"
@@ -1228,12 +1243,6 @@ void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
               "    nr unpacked keys %u\n"
               "    floats %zu\n"
               "    failed unpacked %zu\n",
-              f->key_u64s,
-              f->bits_per_field[0],
-              f->bits_per_field[1],
-              f->bits_per_field[2],
-              f->bits_per_field[3],
-              f->bits_per_field[4],
               b->unpack_fn_len,
               b->nr.live_u64s * sizeof(u64),
               btree_bytes(c) - sizeof(struct btree_node),