diff --git a/fs/bcachefs/btree_update.h b/fs/bcachefs/btree_update.h index 256da97f721c..f5700c286b9e 100644 --- a/fs/bcachefs/btree_update.h +++ b/fs/bcachefs/btree_update.h @@ -72,6 +72,8 @@ int bch2_btree_delete_range_trans(struct btree_trans *, enum btree_id, int bch2_btree_delete_range(struct bch_fs *, enum btree_id, struct bpos, struct bpos, unsigned, u64 *); +int bch2_btree_bit_mod(struct btree_trans *, enum btree_id, struct bpos, bool); + int bch2_btree_node_rewrite(struct btree_trans *, struct btree_iter *, struct btree *, unsigned); void bch2_btree_node_rewrite_async(struct bch_fs *, struct btree *); diff --git a/fs/bcachefs/btree_update_leaf.c b/fs/bcachefs/btree_update_leaf.c index 609780f0ce8e..336a91e4fb20 100644 --- a/fs/bcachefs/btree_update_leaf.c +++ b/fs/bcachefs/btree_update_leaf.c @@ -1996,6 +1996,24 @@ int bch2_btree_delete_range(struct bch_fs *c, enum btree_id id, return ret; } +int bch2_btree_bit_mod(struct btree_trans *trans, enum btree_id btree, + struct bpos pos, bool set) +{ + struct bkey_i *k; + int ret = 0; + + k = bch2_trans_kmalloc_nomemzero(trans, sizeof(*k)); + ret = PTR_ERR_OR_ZERO(k); + if (unlikely(ret)) + return ret; + + bkey_init(&k->k); + k->k.type = set ? KEY_TYPE_set : KEY_TYPE_deleted; + k->k.p = pos; + + return bch2_trans_update_buffered(trans, btree, k); +} + static int __bch2_trans_log_msg(darray_u64 *entries, const char *fmt, va_list args) { struct printbuf buf = PRINTBUF; diff --git a/fs/bcachefs/lru.c b/fs/bcachefs/lru.c index 07d1929535b8..3e8b8f2f38a3 100644 --- a/fs/bcachefs/lru.c +++ b/fs/bcachefs/lru.c @@ -41,28 +41,12 @@ void bch2_lru_pos_to_text(struct printbuf *out, struct bpos lru) } static int __bch2_lru_set(struct btree_trans *trans, u16 lru_id, - u64 dev_bucket, u64 time, unsigned key_type) + u64 dev_bucket, u64 time, bool set) { - struct bkey_i *k; - int ret = 0; - - if (!time) - return 0; - - k = bch2_trans_kmalloc_nomemzero(trans, sizeof(*k)); - ret = PTR_ERR_OR_ZERO(k); - if (unlikely(ret)) - return ret; - - bkey_init(&k->k); - k->k.type = key_type; - k->k.p = lru_pos(lru_id, dev_bucket, time); - - EBUG_ON(lru_pos_id(k->k.p) != lru_id); - EBUG_ON(lru_pos_time(k->k.p) != time); - EBUG_ON(k->k.p.offset != dev_bucket); - - return bch2_trans_update_buffered(trans, BTREE_ID_lru, k); + return time + ? bch2_btree_bit_mod(trans, BTREE_ID_lru, + lru_pos(lru_id, dev_bucket, time), set) + : 0; } int bch2_lru_del(struct btree_trans *trans, u16 lru_id, u64 dev_bucket, u64 time) diff --git a/fs/bcachefs/lru.h b/fs/bcachefs/lru.h index 7a3be20a8523..be66bf9ad809 100644 --- a/fs/bcachefs/lru.h +++ b/fs/bcachefs/lru.h @@ -5,13 +5,6 @@ #define LRU_TIME_BITS 48 #define LRU_TIME_MAX ((1ULL << LRU_TIME_BITS) - 1) -static inline struct bpos lru_pos(u16 lru_id, u64 dev_bucket, u64 time) -{ - EBUG_ON(time > LRU_TIME_MAX); - - return POS(((u64) lru_id << LRU_TIME_BITS)|time, dev_bucket); -} - static inline u64 lru_pos_id(struct bpos pos) { return pos.inode >> LRU_TIME_BITS; @@ -22,6 +15,18 @@ static inline u64 lru_pos_time(struct bpos pos) return pos.inode & ~(~0ULL << LRU_TIME_BITS); } +static inline struct bpos lru_pos(u16 lru_id, u64 dev_bucket, u64 time) +{ + struct bpos pos = POS(((u64) lru_id << LRU_TIME_BITS)|time, dev_bucket); + + EBUG_ON(time > LRU_TIME_MAX); + EBUG_ON(lru_pos_id(pos) != lru_id); + EBUG_ON(lru_pos_time(pos) != time); + EBUG_ON(pos.offset != dev_bucket); + + return pos; +} + #define BCH_LRU_TYPES() \ x(read) \ x(fragmentation)