]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/buckets.h
Update bcachefs sources to 717b356d1d bcachefs: Convert journal validation to bkey_in...
[bcachefs-tools-debian] / libbcachefs / buckets.h
index bdf4fff9cb8a8a69e372a03daf9051231ae352bd..f192809f50cf040fe0129f00c7c8dc011356ef7c 100644 (file)
 
 #include "buckets_types.h"
 #include "extents.h"
-#include "super.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;     \
@@ -150,26 +174,27 @@ static inline struct bch_dev_usage bch2_dev_usage_read(struct bch_dev *ca)
 
 void bch2_dev_usage_init(struct bch_dev *);
 
-static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum alloc_reserve reserve)
+static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum bch_watermark watermark)
 {
        s64 reserved = 0;
 
-       switch (reserve) {
-       case RESERVE_NR:
+       switch (watermark) {
+       case BCH_WATERMARK_NR:
                unreachable();
-       case RESERVE_stripe:
+       case BCH_WATERMARK_stripe:
                reserved += ca->mi.nbuckets >> 6;
                fallthrough;
-       case RESERVE_none:
+       case BCH_WATERMARK_normal:
                reserved += ca->mi.nbuckets >> 6;
                fallthrough;
-       case RESERVE_movinggc:
+       case BCH_WATERMARK_copygc:
                reserved += ca->nr_btree_reserve;
                fallthrough;
-       case RESERVE_btree:
+       case BCH_WATERMARK_btree:
                reserved += ca->nr_btree_reserve;
                fallthrough;
-       case RESERVE_btree_movinggc:
+       case BCH_WATERMARK_btree_copygc:
+       case BCH_WATERMARK_reclaim:
                break;
        }
 
@@ -178,17 +203,17 @@ static inline u64 bch2_dev_buckets_reserved(struct bch_dev *ca, enum alloc_reser
 
 static inline u64 dev_buckets_free(struct bch_dev *ca,
                                   struct bch_dev_usage usage,
-                                  enum alloc_reserve reserve)
+                                  enum bch_watermark watermark)
 {
        return max_t(s64, 0,
                     usage.d[BCH_DATA_free].buckets -
                     ca->nr_open_buckets -
-                    bch2_dev_buckets_reserved(ca, reserve));
+                    bch2_dev_buckets_reserved(ca, watermark));
 }
 
 static inline u64 __dev_buckets_available(struct bch_dev *ca,
                                          struct bch_dev_usage usage,
-                                         enum alloc_reserve reserve)
+                                         enum bch_watermark watermark)
 {
        return max_t(s64, 0,
                       usage.d[BCH_DATA_free].buckets
@@ -196,13 +221,13 @@ static inline u64 __dev_buckets_available(struct bch_dev *ca,
                     + usage.d[BCH_DATA_need_gc_gens].buckets
                     + usage.d[BCH_DATA_need_discard].buckets
                     - ca->nr_open_buckets
-                    - bch2_dev_buckets_reserved(ca, reserve));
+                    - bch2_dev_buckets_reserved(ca, watermark));
 }
 
 static inline u64 dev_buckets_available(struct bch_dev *ca,
-                                       enum alloc_reserve reserve)
+                                       enum bch_watermark watermark)
 {
-       return __dev_buckets_available(ca, bch2_dev_usage_read(ca), reserve);
+       return __dev_buckets_available(ca, bch2_dev_usage_read(ca), watermark);
 }
 
 /* Filesystem usage: */
@@ -248,6 +273,20 @@ bch2_fs_usage_read_short(struct bch_fs *);
 
 /* key/bucket marking: */
 
+static inline struct bch_fs_usage *fs_usage_ptr(struct bch_fs *c,
+                                               unsigned journal_seq,
+                                               bool gc)
+{
+       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]);
+}
+
+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 *,
@@ -260,8 +299,6 @@ 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_inode(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,
@@ -269,7 +306,6 @@ int bch2_mark_reflink_p(struct btree_trans *, enum btree_id, 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_inode(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);
 
@@ -280,6 +316,27 @@ 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 *);
 
+static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b)
+{
+       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;
+
+       if (!b)
+               return true;
+
+       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;
+}
+
 /* disk reservations: */
 
 static inline void bch2_disk_reservation_put(struct bch_fs *c,