X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs%2Fbuckets.h;h=bc088673009bfce4f9af14506d9f16382338a4f4;hb=3bd4653767bb5eadd253e382fe8503ceb6bef5d1;hp=9c77304ff5be9346d063fd2c4142f229bc1df0f3;hpb=5ec39af8eaba49aee7bafa44c661da39e2f40dc3;p=bcachefs-tools-debian diff --git a/libbcachefs/buckets.h b/libbcachefs/buckets.h index 9c77304..bc08867 100644 --- a/libbcachefs/buckets.h +++ b/libbcachefs/buckets.h @@ -1,3 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ /* * Code for manipulating bucket marks for garbage collection. * @@ -8,35 +9,110 @@ #define _BUCKETS_H #include "buckets_types.h" -#include "super.h" - -#define for_each_bucket(b, ca) \ - for (b = (ca)->buckets + (ca)->mi.first_bucket; \ - b < (ca)->buckets + (ca)->mi.nbuckets; b++) - -#define bucket_cmpxchg(g, new, expr) \ -({ \ - u64 _v = READ_ONCE((g)->_mark.counter); \ - struct bucket_mark _old; \ - \ - do { \ - (new).counter = _old.counter = _v; \ - expr; \ - } while ((_v = cmpxchg(&(g)->_mark.counter, \ - _old.counter, \ - (new).counter)) != _old.counter);\ - _old; \ -}) +#include "extents.h" +#include "sb-members.h" + +static inline size_t sector_to_bucket(const struct bch_dev *ca, sector_t s) +{ + return div_u64(s, ca->mi.bucket_size); +} + +static inline sector_t bucket_to_sector(const struct bch_dev *ca, size_t b) +{ + return ((sector_t) b) * ca->mi.bucket_size; +} + +static inline sector_t bucket_remainder(const struct bch_dev *ca, sector_t s) +{ + u32 remainder; + + div_u64_rem(s, ca->mi.bucket_size, &remainder); + return remainder; +} + +static inline size_t sector_to_bucket_and_offset(const struct bch_dev *ca, sector_t s, + u32 *offset) +{ + return div_u64_rem(s, ca->mi.bucket_size, offset); +} + +#define for_each_bucket(_b, _buckets) \ + for (_b = (_buckets)->b + (_buckets)->first_bucket; \ + _b < (_buckets)->b + (_buckets)->nbuckets; _b++) /* - * bucket_gc_gen() returns the difference between the bucket's current gen and - * the oldest gen of any pointer into that bucket in the btree. + * Ugly hack alert: + * + * We need to cram a spinlock in a single byte, because that's what we have left + * in struct bucket, and we care about the size of these - during fsck, we need + * in memory state for every single bucket on every device. + * + * We used to do + * while (xchg(&b->lock, 1) cpu_relax(); + * but, it turns out not all architectures support xchg on a single byte. + * + * So now we use bit_spin_lock(), with fun games since we can't burn a whole + * ulong for this - we just need to make sure the lock bit always ends up in the + * first byte. */ -static inline u8 bucket_gc_gen(struct bch_dev *ca, struct bucket *g) +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define BUCKET_LOCK_BITNR 0 +#else +#define BUCKET_LOCK_BITNR (BITS_PER_LONG - 1) +#endif + +union ulong_byte_assert { + ulong ulong; + u8 byte; +}; + +static inline void bucket_unlock(struct bucket *b) +{ + BUILD_BUG_ON(!((union ulong_byte_assert) { .ulong = 1UL << BUCKET_LOCK_BITNR }).byte); + + clear_bit_unlock(BUCKET_LOCK_BITNR, (void *) &b->lock); + wake_up_bit((void *) &b->lock, BUCKET_LOCK_BITNR); +} + +static inline void bucket_lock(struct bucket *b) +{ + wait_on_bit_lock((void *) &b->lock, BUCKET_LOCK_BITNR, + TASK_UNINTERRUPTIBLE); +} + +static inline struct bucket_array *gc_bucket_array(struct bch_dev *ca) { - unsigned long r = g - ca->buckets; - return g->mark.gen - ca->oldest_gens[r]; + return rcu_dereference_check(ca->buckets_gc, + !ca->fs || + percpu_rwsem_is_held(&ca->fs->mark_lock) || + lockdep_is_held(&ca->fs->gc_lock) || + lockdep_is_held(&ca->bucket_lock)); +} + +static inline struct bucket *gc_bucket(struct bch_dev *ca, size_t b) +{ + struct bucket_array *buckets = gc_bucket_array(ca); + + BUG_ON(b < buckets->first_bucket || b >= buckets->nbuckets); + return buckets->b + b; +} + +static inline struct bucket_gens *bucket_gens(struct bch_dev *ca) +{ + return rcu_dereference_check(ca->bucket_gens, + !ca->fs || + percpu_rwsem_is_held(&ca->fs->mark_lock) || + lockdep_is_held(&ca->fs->gc_lock) || + lockdep_is_held(&ca->bucket_lock)); +} + +static inline u8 *bucket_gen(struct bch_dev *ca, size_t b) +{ + struct bucket_gens *gens = bucket_gens(ca); + + BUG_ON(b < gens->first_bucket || b >= gens->nbuckets); + return gens->b + b; } static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca, @@ -45,223 +121,342 @@ static inline size_t PTR_BUCKET_NR(const struct bch_dev *ca, return sector_to_bucket(ca, ptr->offset); } -/* - * Returns 0 if no pointers or device offline - only for tracepoints! - */ -static inline size_t PTR_BUCKET_NR_TRACE(const struct bch_fs *c, - const struct bkey_i *k, - unsigned ptr) +static inline struct bpos PTR_BUCKET_POS(const struct bch_fs *c, + const struct bch_extent_ptr *ptr) { - size_t bucket = 0; -#if 0 - if (bkey_extent_is_data(&k->k)) { - const struct bch_extent_ptr *ptr; - - extent_for_each_ptr(bkey_i_to_s_c_extent(k), ptr) { - const struct bch_dev *ca = c->devs[ptr->dev]; - bucket = PTR_BUCKET_NR(ca, ptr); - break; - } - } -#endif - return bucket; + struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev); + + return POS(ptr->dev, PTR_BUCKET_NR(ca, ptr)); +} + +static inline struct bpos PTR_BUCKET_POS_OFFSET(const struct bch_fs *c, + const struct bch_extent_ptr *ptr, + u32 *bucket_offset) +{ + struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev); + + return POS(ptr->dev, sector_to_bucket_and_offset(ca, ptr->offset, bucket_offset)); } -static inline struct bucket *PTR_BUCKET(const struct bch_dev *ca, - const struct bch_extent_ptr *ptr) +static inline struct bucket *PTR_GC_BUCKET(struct bch_dev *ca, + const struct bch_extent_ptr *ptr) { - return ca->buckets + PTR_BUCKET_NR(ca, ptr); + return gc_bucket(ca, PTR_BUCKET_NR(ca, ptr)); } -static inline u8 __gen_after(u8 a, u8 b) +static inline enum bch_data_type ptr_data_type(const struct bkey *k, + const struct bch_extent_ptr *ptr) { - u8 r = a - b; + if (bkey_is_btree_ptr(k)) + return BCH_DATA_btree; - return r > 128U ? 0 : r; + return ptr->cached ? BCH_DATA_cached : BCH_DATA_user; } -static inline u8 gen_after(u8 a, u8 b) +static inline s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p) { - u8 r = a - b; + EBUG_ON(sectors < 0); + + return crc_is_compressed(p.crc) + ? DIV_ROUND_UP_ULL(sectors * p.crc.compressed_size, + p.crc.uncompressed_size) + : sectors; +} - BUG_ON(r > 128U); +static inline int gen_cmp(u8 a, u8 b) +{ + return (s8) (a - b); +} + +static inline int gen_after(u8 a, u8 b) +{ + int r = gen_cmp(a, b); - return r; + return r > 0 ? r : 0; } /** * ptr_stale() - check if a pointer points into a bucket that has been * invalidated. */ -static inline u8 ptr_stale(const struct bch_dev *ca, +static inline u8 ptr_stale(struct bch_dev *ca, const struct bch_extent_ptr *ptr) { - return gen_after(PTR_BUCKET(ca, ptr)->mark.gen, ptr->gen); -} + u8 ret; -/* bucket heaps */ + rcu_read_lock(); + ret = gen_after(*bucket_gen(ca, PTR_BUCKET_NR(ca, ptr)), ptr->gen); + rcu_read_unlock(); -static inline bool bucket_min_cmp(struct bucket_heap_entry l, - struct bucket_heap_entry r) -{ - return l.val < r.val; + return ret; } -static inline bool bucket_max_cmp(struct bucket_heap_entry l, - struct bucket_heap_entry r) +/* Device usage: */ + +void bch2_dev_usage_read_fast(struct bch_dev *, struct bch_dev_usage *); +static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca) { - return l.val > r.val; + struct bch_dev_usage ret; + + bch2_dev_usage_read_fast(ca, &ret); + return ret; } -static inline void bucket_heap_push(struct bch_dev *ca, struct bucket *g, - unsigned long val) -{ - struct bucket_heap_entry new = { g, val }; +void bch2_dev_usage_init(struct bch_dev *); +void bch2_dev_usage_to_text(struct printbuf *, struct bch_dev_usage *); - if (!heap_full(&ca->heap)) - heap_add(&ca->heap, new, bucket_min_cmp); - else if (bucket_min_cmp(new, heap_peek(&ca->heap))) { - ca->heap.data[0] = new; - heap_sift(&ca->heap, 0, bucket_min_cmp); +static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark) +{ + s64 reserved = 0; + + switch (watermark) { + case BCH_WATERMARK_NR: + BUG(); + case BCH_WATERMARK_stripe: + reserved += ca->mi.nbuckets >> 6; + fallthrough; + case BCH_WATERMARK_normal: + reserved += ca->mi.nbuckets >> 6; + fallthrough; + case BCH_WATERMARK_copygc: + reserved += ca->nr_btree_reserve; + fallthrough; + case BCH_WATERMARK_btree: + reserved += ca->nr_btree_reserve; + fallthrough; + case BCH_WATERMARK_btree_copygc: + case BCH_WATERMARK_reclaim: + break; } -} -/* bucket gc marks */ + return reserved; +} -/* The dirty and cached sector counts saturate. If this occurs, - * reference counting alone will not free the bucket, and a btree - * GC must be performed. */ -#define GC_MAX_SECTORS_USED ((1U << 15) - 1) +static inline u64 dev_buckets_free(struct bch_dev *ca, + struct bch_dev_usage usage, + enum bch_watermark watermark) +{ + return max_t(s64, 0, + usage.d[BCH_DATA_free].buckets - + ca->nr_open_buckets - + bch2_dev_buckets_reserved(ca, watermark)); +} -static inline bool bucket_unused(struct bucket *g) +static inline u64 __dev_buckets_available(struct bch_dev *ca, + struct bch_dev_usage usage, + enum bch_watermark watermark) { - return !g->mark.counter; + return max_t(s64, 0, + usage.d[BCH_DATA_free].buckets + + usage.d[BCH_DATA_cached].buckets + + usage.d[BCH_DATA_need_gc_gens].buckets + + usage.d[BCH_DATA_need_discard].buckets + - ca->nr_open_buckets + - bch2_dev_buckets_reserved(ca, watermark)); } -static inline unsigned bucket_sectors_used(struct bucket *g) +static inline u64 dev_buckets_available(struct bch_dev *ca, + enum bch_watermark watermark) { - return g->mark.dirty_sectors + g->mark.cached_sectors; + return __dev_buckets_available(ca, bch2_dev_usage_read(ca), watermark); } -/* Per device stats: */ +/* Filesystem usage: */ -struct bch_dev_usage __bch2_dev_usage_read(struct bch_dev *); -struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *); +static inline unsigned __fs_usage_u64s(unsigned nr_replicas) +{ + return sizeof(struct bch_fs_usage) / sizeof(u64) + nr_replicas; +} -static inline u64 __dev_buckets_available(struct bch_dev *ca, - struct bch_dev_usage stats) +static inline unsigned fs_usage_u64s(struct bch_fs *c) { - return max_t(s64, 0, - ca->mi.nbuckets - ca->mi.first_bucket - - stats.buckets_dirty - - stats.buckets_alloc - - stats.buckets_meta); + return __fs_usage_u64s(READ_ONCE(c->replicas.nr)); } -/* - * Number of reclaimable buckets - only for use by the allocator thread: - */ -static inline u64 dev_buckets_available(struct bch_dev *ca) +static inline unsigned __fs_usage_online_u64s(unsigned nr_replicas) { - return __dev_buckets_available(ca, bch2_dev_usage_read(ca)); + return sizeof(struct bch_fs_usage_online) / sizeof(u64) + nr_replicas; } -static inline u64 __dev_buckets_free(struct bch_dev *ca, - struct bch_dev_usage stats) +static inline unsigned fs_usage_online_u64s(struct bch_fs *c) { - return __dev_buckets_available(ca, stats) + - fifo_used(&ca->free[RESERVE_NONE]) + - fifo_used(&ca->free_inc); + return __fs_usage_online_u64s(READ_ONCE(c->replicas.nr)); } -static inline u64 dev_buckets_free(struct bch_dev *ca) +static inline unsigned dev_usage_u64s(void) { - return __dev_buckets_free(ca, bch2_dev_usage_read(ca)); + return sizeof(struct bch_dev_usage) / sizeof(u64); } -/* Cache set stats: */ +u64 bch2_fs_usage_read_one(struct bch_fs *, u64 *); -struct bch_fs_usage __bch2_fs_usage_read(struct bch_fs *); -struct bch_fs_usage bch2_fs_usage_read(struct bch_fs *); -void bch2_fs_usage_apply(struct bch_fs *, struct bch_fs_usage *, - struct disk_reservation *, struct gc_pos); +struct bch_fs_usage_online *bch2_fs_usage_read(struct bch_fs *); -static inline u64 __bch2_fs_sectors_used(struct bch_fs *c) -{ - struct bch_fs_usage stats = __bch2_fs_usage_read(c); - u64 reserved = stats.persistent_reserved + - stats.online_reserved; +void bch2_fs_usage_acc_to_base(struct bch_fs *, unsigned); - return stats.s[S_COMPRESSED][S_META] + - stats.s[S_COMPRESSED][S_DIRTY] + - reserved + - (reserved >> 7); -} +void bch2_fs_usage_to_text(struct printbuf *, + struct bch_fs *, struct bch_fs_usage_online *); + +u64 bch2_fs_sectors_used(struct bch_fs *, struct bch_fs_usage_online *); + +struct bch_fs_usage_short +bch2_fs_usage_read_short(struct bch_fs *); + +/* key/bucket marking: */ -static inline u64 bch2_fs_sectors_used(struct bch_fs *c) +static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c, + unsigned journal_seq, + bool gc) { - return min(c->capacity, __bch2_fs_sectors_used(c)); + percpu_rwsem_assert_held(&c->mark_lock); + BUG_ON(!gc && !journal_seq); + + return this_cpu_ptr(gc + ? c->usage_gc + : c->usage[journal_seq & JOURNAL_BUF_MASK]); } -/* XXX: kill? */ -static inline u64 sectors_available(struct bch_fs *c) +int bch2_update_replicas_list(struct btree_trans *, + struct bch_replicas_entry_v1 *, s64); +int bch2_update_cached_sectors_list(struct btree_trans *, unsigned, s64); +int bch2_replicas_deltas_realloc(struct btree_trans *, unsigned); + +void bch2_fs_usage_initialize(struct bch_fs *); + +int bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *, + size_t, enum bch_data_type, unsigned, + struct gc_pos, unsigned); + +int bch2_mark_alloc(struct btree_trans *, enum btree_id, unsigned, + struct bkey_s_c, struct bkey_s_c, unsigned); +int bch2_mark_extent(struct btree_trans *, enum btree_id, unsigned, + struct bkey_s_c, struct bkey_s_c, unsigned); +int bch2_mark_stripe(struct btree_trans *, enum btree_id, unsigned, + struct bkey_s_c, struct bkey_s_c, unsigned); +int bch2_mark_reservation(struct btree_trans *, enum btree_id, unsigned, + struct bkey_s_c, struct bkey_s_c, unsigned); +int bch2_mark_reflink_p(struct btree_trans *, enum btree_id, unsigned, + struct bkey_s_c, struct bkey_s_c, unsigned); + +int bch2_trans_mark_extent(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned); +int bch2_trans_mark_stripe(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned); +int bch2_trans_mark_reservation(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned); +int bch2_trans_mark_reflink_p(struct btree_trans *, enum btree_id, unsigned, struct bkey_s_c, struct bkey_i *, unsigned); + +#define mem_trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, _new, _flags)\ +({ \ + int ret = 0; \ + \ + if (_old.k->type) \ + ret = _fn(_trans, _btree_id, _level, _old, _flags & ~BTREE_TRIGGER_INSERT); \ + if (!ret && _new.k->type) \ + ret = _fn(_trans, _btree_id, _level, _new, _flags & ~BTREE_TRIGGER_OVERWRITE); \ + ret; \ +}) + +#define trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, _new, _flags) \ + mem_trigger_run_overwrite_then_insert(_fn, _trans, _btree_id, _level, _old, bkey_i_to_s_c(_new), _flags) + +void bch2_trans_fs_usage_revert(struct btree_trans *, struct replicas_delta_list *); +int bch2_trans_fs_usage_apply(struct btree_trans *, struct replicas_delta_list *); + +int bch2_trans_mark_metadata_bucket(struct btree_trans *, struct bch_dev *, + size_t, enum bch_data_type, unsigned); +int bch2_trans_mark_dev_sb(struct bch_fs *, struct bch_dev *); +int bch2_trans_mark_dev_sbs(struct bch_fs *); + +static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b) { - struct bch_dev *ca; + struct bch_sb_layout *layout = &ca->disk_sb.sb->layout; + u64 b_offset = bucket_to_sector(ca, b); + u64 b_end = bucket_to_sector(ca, b + 1); unsigned i; - u64 ret = 0; - rcu_read_lock(); - for_each_member_device_rcu(ca, c, i) - ret += dev_buckets_available(ca) << ca->bucket_bits; - rcu_read_unlock(); + if (!b) + return true; - return ret; + for (i = 0; i < layout->nr_superblocks; i++) { + u64 offset = le64_to_cpu(layout->sb_offset[i]); + u64 end = offset + (1 << layout->sb_max_size_bits); + + if (!(offset >= b_end || end <= b_offset)) + return true; + } + + return false; } -static inline bool is_available_bucket(struct bucket_mark mark) +/* disk reservations: */ + +static inline void bch2_disk_reservation_put(struct bch_fs *c, + struct disk_reservation *res) { - return (!mark.owned_by_allocator && - mark.data_type == BUCKET_DATA && - !mark.dirty_sectors && - !mark.nouse); + if (res->sectors) { + this_cpu_sub(*c->online_reserved, res->sectors); + res->sectors = 0; + } } -static inline bool bucket_needs_journal_commit(struct bucket_mark m, - u16 last_seq_ondisk) +#define BCH_DISK_RESERVATION_NOFAIL (1 << 0) + +int __bch2_disk_reservation_add(struct bch_fs *, + struct disk_reservation *, + u64, int); + +static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res, + u64 sectors, int flags) { - return m.journal_seq_valid && - ((s16) m.journal_seq - (s16) last_seq_ondisk > 0); +#ifdef __KERNEL__ + u64 old, new; + + do { + old = this_cpu_read(c->pcpu->sectors_available); + if (sectors > old) + return __bch2_disk_reservation_add(c, res, sectors, flags); + + new = old - sectors; + } while (this_cpu_cmpxchg(c->pcpu->sectors_available, old, new) != old); + + this_cpu_add(*c->online_reserved, sectors); + res->sectors += sectors; + return 0; +#else + return __bch2_disk_reservation_add(c, res, sectors, flags); +#endif } -void bch2_bucket_seq_cleanup(struct bch_fs *); +static inline struct disk_reservation +bch2_disk_reservation_init(struct bch_fs *c, unsigned nr_replicas) +{ + return (struct disk_reservation) { + .sectors = 0, +#if 0 + /* not used yet: */ + .gen = c->capacity_gen, +#endif + .nr_replicas = nr_replicas, + }; +} -void bch2_invalidate_bucket(struct bch_dev *, struct bucket *); -void bch2_mark_free_bucket(struct bch_dev *, struct bucket *); -void bch2_mark_alloc_bucket(struct bch_dev *, struct bucket *, bool); -void bch2_mark_metadata_bucket(struct bch_dev *, struct bucket *, - enum bucket_data_type, bool); +static inline int bch2_disk_reservation_get(struct bch_fs *c, + struct disk_reservation *res, + u64 sectors, unsigned nr_replicas, + int flags) +{ + *res = bch2_disk_reservation_init(c, nr_replicas); -void __bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool, - struct bch_fs_usage *); -void bch2_gc_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool); -void bch2_mark_key(struct bch_fs *, struct bkey_s_c, s64, bool, - struct gc_pos, struct bch_fs_usage *, u64); + return bch2_disk_reservation_add(c, res, sectors * nr_replicas, flags); +} -void bch2_recalc_sectors_available(struct bch_fs *); +#define RESERVE_FACTOR 6 -void bch2_disk_reservation_put(struct bch_fs *, - struct disk_reservation *); +static inline u64 avail_factor(u64 r) +{ + return div_u64(r << RESERVE_FACTOR, (1 << RESERVE_FACTOR) + 1); +} -#define BCH_DISK_RESERVATION_NOFAIL (1 << 0) -#define BCH_DISK_RESERVATION_METADATA (1 << 1) -#define BCH_DISK_RESERVATION_GC_LOCK_HELD (1 << 2) -#define BCH_DISK_RESERVATION_BTREE_LOCKS_HELD (1 << 3) - -int bch2_disk_reservation_add(struct bch_fs *, - struct disk_reservation *, - unsigned, int); -int bch2_disk_reservation_get(struct bch_fs *, - struct disk_reservation *, - unsigned, int); +int bch2_dev_buckets_resize(struct bch_fs *, struct bch_dev *, u64); +void bch2_dev_buckets_free(struct bch_dev *); +int bch2_dev_buckets_alloc(struct bch_fs *, struct bch_dev *); #endif /* _BUCKETS_H */