bcachefs: btree_id_cached()
authorKent Overstreet <kent.overstreet@gmail.com>
Wed, 12 Jan 2022 07:13:21 +0000 (02:13 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:23 +0000 (17:09 -0400)
Add a new helper that returns true if the given btree ID uses the btree
key cache. This enables some new cleanups, since the helper can check
the options for whether caching is enabled on a given btree.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/bcachefs.h
fs/bcachefs/btree_iter.c
fs/bcachefs/fs.c
fs/bcachefs/inode.c
fs/bcachefs/inode.h
fs/bcachefs/super.c

index 59c0963..55db3c0 100644 (file)
@@ -716,6 +716,7 @@ struct bch_fs {
        bool                    btree_trans_barrier_initialized;
 
        struct btree_key_cache  btree_key_cache;
+       unsigned                btree_key_cache_btrees;
 
        struct workqueue_struct *btree_update_wq;
        struct workqueue_struct *btree_io_complete_wq;
@@ -952,6 +953,11 @@ static inline size_t btree_sectors(const struct bch_fs *c)
        return c->opts.btree_node_size >> 9;
 }
 
+static inline bool btree_id_cached(const struct bch_fs *c, enum btree_id btree)
+{
+       return c->btree_key_cache_btrees & (1U << btree);
+}
+
 static inline struct timespec64 bch2_time_to_timespec(const struct bch_fs *c, s64 time)
 {
        struct timespec64 t;
index 100305c..986ee09 100644 (file)
@@ -2860,6 +2860,9 @@ static void __bch2_trans_iter_init(struct btree_trans *trans,
        if (trans->journal_replay_not_finished)
                flags |= BTREE_ITER_WITH_JOURNAL;
 
+       if (!btree_id_cached(trans->c, btree_id))
+               flags &= ~BTREE_ITER_CACHED;
+
        iter->trans     = trans;
        iter->path      = NULL;
        iter->update_path = NULL;
index 10a7379..9e8b085 100644 (file)
@@ -105,7 +105,7 @@ void bch2_inode_update_after_write(struct btree_trans *trans,
 
        bch2_assert_pos_locked(trans, BTREE_ID_inodes,
                               POS(0, bi->bi_inum),
-                              0 && c->opts.inodes_use_key_cache);
+                              c->opts.inodes_use_key_cache);
 
        set_nlink(&inode->v, bch2_inode_nlink_get(bi));
        i_uid_write(&inode->v, bi->bi_uid);
@@ -1473,7 +1473,7 @@ static void bch2_evict_inode(struct inode *vinode)
                                KEY_TYPE_QUOTA_WARN);
                bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
                                KEY_TYPE_QUOTA_WARN);
-               bch2_inode_rm(c, inode_inum(inode), true);
+               bch2_inode_rm(c, inode_inum(inode));
        }
 }
 
index 79ee9ca..9214f68 100644 (file)
@@ -252,15 +252,13 @@ int bch2_inode_peek(struct btree_trans *trans,
        u32 snapshot;
        int ret;
 
-       if (trans->c->opts.inodes_use_key_cache)
-               flags |= BTREE_ITER_CACHED;
-
        ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
        if (ret)
                return ret;
 
        bch2_trans_iter_init(trans, iter, BTREE_ID_inodes,
-                            SPOS(0, inum.inum, snapshot), flags);
+                            SPOS(0, inum.inum, snapshot),
+                            flags|BTREE_ITER_CACHED);
        k = bch2_btree_iter_peek_slot(iter);
        ret = bkey_err(k);
        if (ret)
@@ -631,20 +629,16 @@ err:
        return ret;
 }
 
-int bch2_inode_rm(struct bch_fs *c, subvol_inum inum, bool cached)
+int bch2_inode_rm(struct bch_fs *c, subvol_inum inum)
 {
        struct btree_trans trans;
        struct btree_iter iter = { NULL };
        struct bkey_i_inode_generation delete;
        struct bch_inode_unpacked inode_u;
        struct bkey_s_c k;
-       unsigned iter_flags = BTREE_ITER_INTENT;
        u32 snapshot;
        int ret;
 
-       if (cached && c->opts.inodes_use_key_cache)
-               iter_flags |= BTREE_ITER_CACHED;
-
        bch2_trans_init(&trans, c, 0, 1024);
 
        /*
@@ -668,7 +662,8 @@ retry:
                goto err;
 
        bch2_trans_iter_init(&trans, &iter, BTREE_ID_inodes,
-                            SPOS(0, inum.inum, snapshot), iter_flags);
+                            SPOS(0, inum.inum, snapshot),
+                            BTREE_ITER_INTENT|BTREE_ITER_CACHED);
        k = bch2_btree_iter_peek_slot(&iter);
 
        ret = bkey_err(k);
index 723186d..77957cc 100644 (file)
@@ -87,7 +87,7 @@ void bch2_inode_init(struct bch_fs *, struct bch_inode_unpacked *,
 int bch2_inode_create(struct btree_trans *, struct btree_iter *,
                      struct bch_inode_unpacked *, u32, u64);
 
-int bch2_inode_rm(struct bch_fs *, subvol_inum, bool);
+int bch2_inode_rm(struct bch_fs *, subvol_inum);
 
 int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
                                  struct bch_inode_unpacked *);
index 3094eb1..a90fa0a 100644 (file)
@@ -770,6 +770,10 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
 
        bch2_opts_apply(&c->opts, opts);
 
+       c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc;
+       if (c->opts.inodes_use_key_cache)
+               c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes;
+
        c->block_bits           = ilog2(block_sectors(c));
        c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);