]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/buckets.c
Update bcachefs sources to c3e4d892b77b mean and variance: Promote to lib/math
[bcachefs-tools-debian] / libbcachefs / buckets.c
index 797ef5eceb3f840e64d2f763f6aa2e6a44e19af9..312bd0c86623402d6be837df1fe8298a71dfd5c6 100644 (file)
@@ -61,7 +61,7 @@ void bch2_fs_usage_initialize(struct bch_fs *c)
                usage->reserved += usage->persistent_reserved[i];
 
        for (i = 0; i < c->replicas.nr; i++) {
-               struct bch_replicas_entry *e =
+               struct bch_replicas_entry_v1 *e =
                        cpu_replicas_entry(&c->replicas, i);
 
                fs_usage_data_type_to_base(usage, e->data_type, usage->replicas[i]);
@@ -102,18 +102,6 @@ void bch2_dev_usage_read_fast(struct bch_dev *ca, struct bch_dev_usage *usage)
        } while (read_seqcount_retry(&c->usage_lock, seq));
 }
 
-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]);
-}
-
 u64 bch2_fs_usage_read_one(struct bch_fs *c, u64 *v)
 {
        ssize_t offset = v - (u64 *) c->usage_base;
@@ -226,7 +214,7 @@ void bch2_fs_usage_to_text(struct printbuf *out,
        }
 
        for (i = 0; i < c->replicas.nr; i++) {
-               struct bch_replicas_entry *e =
+               struct bch_replicas_entry_v1 *e =
                        cpu_replicas_entry(&c->replicas, i);
 
                prt_printf(out, "\t");
@@ -289,12 +277,28 @@ void bch2_dev_usage_init(struct bch_dev *ca)
        ca->usage_base->d[BCH_DATA_free].buckets = ca->mi.nbuckets - ca->mi.first_bucket;
 }
 
-static inline int bucket_sectors_fragmented(struct bch_dev *ca,
-                                           struct bch_alloc_v4 a)
+void bch2_dev_usage_to_text(struct printbuf *out, struct bch_dev_usage *usage)
 {
-       return a.dirty_sectors
-               ? max(0, (int) ca->mi.bucket_size - (int) a.dirty_sectors)
-               : 0;
+       prt_tab(out);
+       prt_str(out, "buckets");
+       prt_tab_rjust(out);
+       prt_str(out, "sectors");
+       prt_tab_rjust(out);
+       prt_str(out, "fragmented");
+       prt_tab_rjust(out);
+       prt_newline(out);
+
+       for (unsigned i = 0; i < BCH_DATA_NR; i++) {
+               prt_str(out, bch2_data_types[i]);
+               prt_tab(out);
+               prt_u64(out, usage->d[i].buckets);
+               prt_tab_rjust(out);
+               prt_u64(out, usage->d[i].sectors);
+               prt_tab_rjust(out);
+               prt_u64(out, usage->d[i].fragmented);
+               prt_tab_rjust(out);
+               prt_newline(out);
+       }
 }
 
 static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
@@ -318,46 +322,41 @@ static void bch2_dev_usage_update(struct bch_fs *c, struct bch_dev *ca,
        u->d[old.data_type].buckets--;
        u->d[new.data_type].buckets++;
 
-       u->buckets_ec -= (int) !!old.stripe;
-       u->buckets_ec += (int) !!new.stripe;
-
-       u->d[old.data_type].sectors -= old.dirty_sectors;
-       u->d[new.data_type].sectors += new.dirty_sectors;
+       u->d[old.data_type].sectors -= bch2_bucket_sectors_dirty(old);
+       u->d[new.data_type].sectors += bch2_bucket_sectors_dirty(new);
 
        u->d[BCH_DATA_cached].sectors += new.cached_sectors;
        u->d[BCH_DATA_cached].sectors -= old.cached_sectors;
 
-       u->d[old.data_type].fragmented -= bucket_sectors_fragmented(ca, old);
-       u->d[new.data_type].fragmented += bucket_sectors_fragmented(ca, new);
+       u->d[old.data_type].fragmented -= bch2_bucket_sectors_fragmented(ca, old);
+       u->d[new.data_type].fragmented += bch2_bucket_sectors_fragmented(ca, new);
 
        preempt_enable();
 }
 
-static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
-                                   struct bucket old, struct bucket new,
-                                   u64 journal_seq, bool gc)
+static inline struct bch_alloc_v4 bucket_m_to_alloc(struct bucket b)
 {
-       struct bch_alloc_v4 old_a = {
-               .gen            = old.gen,
-               .data_type      = old.data_type,
-               .dirty_sectors  = old.dirty_sectors,
-               .cached_sectors = old.cached_sectors,
-               .stripe         = old.stripe,
-       };
-       struct bch_alloc_v4 new_a = {
-               .gen            = new.gen,
-               .data_type      = new.data_type,
-               .dirty_sectors  = new.dirty_sectors,
-               .cached_sectors = new.cached_sectors,
-               .stripe         = new.stripe,
+       return (struct bch_alloc_v4) {
+               .gen            = b.gen,
+               .data_type      = b.data_type,
+               .dirty_sectors  = b.dirty_sectors,
+               .cached_sectors = b.cached_sectors,
+               .stripe         = b.stripe,
        };
+}
 
-       bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, gc);
+static void bch2_dev_usage_update_m(struct bch_fs *c, struct bch_dev *ca,
+                                   struct bucket old, struct bucket new)
+{
+       bch2_dev_usage_update(c, ca,
+                             bucket_m_to_alloc(old),
+                             bucket_m_to_alloc(new),
+                             0, true);
 }
 
 static inline int __update_replicas(struct bch_fs *c,
                                    struct bch_fs_usage *fs_usage,
-                                   struct bch_replicas_entry *r,
+                                   struct bch_replicas_entry_v1 *r,
                                    s64 sectors)
 {
        int idx = bch2_replicas_entry_idx(c, r);
@@ -371,7 +370,7 @@ static inline int __update_replicas(struct bch_fs *c,
 }
 
 static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
-                       struct bch_replicas_entry *r, s64 sectors,
+                       struct bch_replicas_entry_v1 *r, s64 sectors,
                        unsigned journal_seq, bool gc)
 {
        struct bch_fs_usage *fs_usage;
@@ -379,12 +378,11 @@ static inline int update_replicas(struct bch_fs *c, struct bkey_s_c k,
        struct printbuf buf = PRINTBUF;
 
        percpu_down_read(&c->mark_lock);
-       buf.atomic++;
 
        idx = bch2_replicas_entry_idx(c, r);
        if (idx < 0 &&
-           fsck_err(c, "no replicas entry\n"
-                    "  while marking %s",
+           fsck_err(c, ptr_to_missing_replicas_entry,
+                    "no replicas entry\n  while marking %s",
                     (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
                percpu_up_read(&c->mark_lock);
                ret = bch2_mark_replicas(c, r);
@@ -460,15 +458,15 @@ static int __replicas_deltas_realloc(struct btree_trans *trans, unsigned more,
        return 0;
 }
 
-static int replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
+int bch2_replicas_deltas_realloc(struct btree_trans *trans, unsigned more)
 {
        return allocate_dropping_locks_errcode(trans,
                                __replicas_deltas_realloc(trans, more, _gfp));
 }
 
-static inline int update_replicas_list(struct btree_trans *trans,
-                                       struct bch_replicas_entry *r,
-                                       s64 sectors)
+int bch2_update_replicas_list(struct btree_trans *trans,
+                        struct bch_replicas_entry_v1 *r,
+                        s64 sectors)
 {
        struct replicas_delta_list *d;
        struct replicas_delta *n;
@@ -479,28 +477,28 @@ static inline int update_replicas_list(struct btree_trans *trans,
                return 0;
 
        b = replicas_entry_bytes(r) + 8;
-       ret = replicas_deltas_realloc(trans, b);
+       ret = bch2_replicas_deltas_realloc(trans, b);
        if (ret)
                return ret;
 
        d = trans->fs_usage_deltas;
        n = (void *) d->d + d->used;
        n->delta = sectors;
-       memcpy((void *) n + offsetof(struct replicas_delta, r),
-              r, replicas_entry_bytes(r));
+       unsafe_memcpy((void *) n + offsetof(struct replicas_delta, r),
+                     r, replicas_entry_bytes(r),
+                     "flexible array member embedded in strcuct with padding");
        bch2_replicas_entry_sort(&n->r);
        d->used += b;
        return 0;
 }
 
-static inline int update_cached_sectors_list(struct btree_trans *trans,
-                                             unsigned dev, s64 sectors)
+int bch2_update_cached_sectors_list(struct btree_trans *trans, unsigned dev, s64 sectors)
 {
        struct bch_replicas_padded r;
 
        bch2_replicas_entry_cached(&r.e, dev);
 
-       return update_replicas_list(trans, &r.e, sectors);
+       return bch2_update_replicas_list(trans, &r.e, sectors);
 }
 
 int bch2_mark_alloc(struct btree_trans *trans,
@@ -592,23 +590,6 @@ int bch2_mark_alloc(struct btree_trans *trans,
        }
        percpu_up_read(&c->mark_lock);
 
-       /*
-        * need to know if we're getting called from the invalidate path or
-        * not:
-        */
-
-       if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
-           old_a->cached_sectors) {
-               ret = update_cached_sectors(c, new, ca->dev_idx,
-                                           -((s64) old_a->cached_sectors),
-                                           journal_seq, gc);
-               if (ret) {
-                       bch2_fs_fatal_error(c, "%s(): no replicas entry while updating cached sectors",
-                                           __func__);
-                       return ret;
-               }
-       }
-
        if (new_a->data_type == BCH_DATA_free &&
            (!new_a->journal_seq || new_a->journal_seq < c->journal.flushed_seq_ondisk))
                closure_wake_up(&c->freelist_wait);
@@ -670,14 +651,13 @@ int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
                goto err;
        }
 
-
        g->data_type = data_type;
        g->dirty_sectors += sectors;
        new = *g;
 err:
        bucket_unlock(g);
        if (!ret)
-               bch2_dev_usage_update_m(c, ca, old, new, 0, true);
+               bch2_dev_usage_update_m(c, ca, old, new);
        percpu_up_read(&c->mark_lock);
        return ret;
 }
@@ -687,14 +667,11 @@ static int check_bucket_ref(struct btree_trans *trans,
                            const struct bch_extent_ptr *ptr,
                            s64 sectors, enum bch_data_type ptr_data_type,
                            u8 b_gen, u8 bucket_data_type,
-                           u32 dirty_sectors, u32 cached_sectors)
+                           u32 bucket_sectors)
 {
        struct bch_fs *c = trans->c;
        struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
        size_t bucket_nr = PTR_BUCKET_NR(ca, ptr);
-       u16 bucket_sectors = !ptr->cached
-               ? dirty_sectors
-               : cached_sectors;
        struct printbuf buf = PRINTBUF;
        int ret = 0;
 
@@ -707,6 +684,7 @@ static int check_bucket_ref(struct btree_trans *trans,
 
        if (gen_after(ptr->gen, b_gen)) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
+                             BCH_FSCK_ERR_ptr_gen_newer_than_bucket_gen,
                        "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
@@ -719,6 +697,7 @@ static int check_bucket_ref(struct btree_trans *trans,
 
        if (gen_cmp(b_gen, ptr->gen) > BUCKET_GC_GEN_MAX) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
+                             BCH_FSCK_ERR_ptr_too_stale,
                        "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
@@ -732,6 +711,7 @@ static int check_bucket_ref(struct btree_trans *trans,
 
        if (b_gen != ptr->gen && !ptr->cached) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
+                             BCH_FSCK_ERR_stale_dirty_ptr,
                        "bucket %u:%zu gen %u (mem gen %u) data type %s: stale dirty ptr (gen %u)\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
@@ -753,6 +733,7 @@ static int check_bucket_ref(struct btree_trans *trans,
            ptr_data_type &&
            bucket_data_type != ptr_data_type) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
+                             BCH_FSCK_ERR_ptr_bucket_data_type_mismatch,
                        "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
@@ -764,9 +745,10 @@ static int check_bucket_ref(struct btree_trans *trans,
                goto err;
        }
 
-       if ((unsigned) (bucket_sectors + sectors) > U32_MAX) {
+       if ((u64) bucket_sectors + sectors > U32_MAX) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
-                       "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U16_MAX\n"
+                             BCH_FSCK_ERR_bucket_sector_count_overflow,
+                       "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U32_MAX\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
                        bch2_data_types[bucket_data_type ?: ptr_data_type],
@@ -790,7 +772,6 @@ static int mark_stripe_bucket(struct btree_trans *trans,
                              unsigned flags)
 {
        struct bch_fs *c = trans->c;
-       u64 journal_seq = trans->journal_res.seq;
        const struct bch_stripe *s = bkey_s_c_to_stripe(k).v;
        unsigned nr_data = s->nr_blocks - s->nr_redundant;
        bool parity = ptr_idx >= nr_data;
@@ -807,7 +788,6 @@ static int mark_stripe_bucket(struct btree_trans *trans,
        /* * XXX doesn't handle deletion */
 
        percpu_down_read(&c->mark_lock);
-       buf.atomic++;
        g = PTR_GC_BUCKET(ca, ptr);
 
        if (g->dirty_sectors ||
@@ -825,7 +805,7 @@ static int mark_stripe_bucket(struct btree_trans *trans,
 
        ret = check_bucket_ref(trans, k, ptr, sectors, data_type,
                               g->gen, g->data_type,
-                              g->dirty_sectors, g->cached_sectors);
+                              g->dirty_sectors);
        if (ret)
                goto err;
 
@@ -838,7 +818,7 @@ static int mark_stripe_bucket(struct btree_trans *trans,
 err:
        bucket_unlock(g);
        if (!ret)
-               bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
+               bch2_dev_usage_update_m(c, ca, old, new);
        percpu_up_read(&c->mark_lock);
        printbuf_exit(&buf);
        return ret;
@@ -855,15 +835,18 @@ static int __mark_pointer(struct btree_trans *trans,
                ? dirty_sectors
                : cached_sectors;
        int ret = check_bucket_ref(trans, k, ptr, sectors, ptr_data_type,
-                                  bucket_gen, *bucket_data_type,
-                                  *dirty_sectors, *cached_sectors);
+                                  bucket_gen, *bucket_data_type, *dst_sectors);
 
        if (ret)
                return ret;
 
        *dst_sectors += sectors;
-       *bucket_data_type = *dirty_sectors || *cached_sectors
-               ? ptr_data_type : 0;
+
+       if (!*dirty_sectors && !*cached_sectors)
+               *bucket_data_type = 0;
+       else if (*bucket_data_type != BCH_DATA_stripe)
+               *bucket_data_type = ptr_data_type;
+
        return 0;
 }
 
@@ -874,7 +857,6 @@ static int bch2_mark_pointer(struct btree_trans *trans,
                             s64 sectors,
                             unsigned flags)
 {
-       u64 journal_seq = trans->journal_res.seq;
        struct bch_fs *c = trans->c;
        struct bch_dev *ca = bch_dev_bkey_exists(c, p.ptr.dev);
        struct bucket old, new, *g;
@@ -901,7 +883,7 @@ static int bch2_mark_pointer(struct btree_trans *trans,
        new = *g;
        bucket_unlock(g);
        if (!ret)
-               bch2_dev_usage_update_m(c, ca, old, new, journal_seq, true);
+               bch2_dev_usage_update_m(c, ca, old, new);
        percpu_up_read(&c->mark_lock);
 
        return ret;
@@ -948,14 +930,12 @@ static int bch2_mark_stripe_ptr(struct btree_trans *trans,
        return 0;
 }
 
-int bch2_mark_extent(struct btree_trans *trans,
-                    enum btree_id btree_id, unsigned level,
-                    struct bkey_s_c old, struct bkey_s_c new,
-                    unsigned flags)
+static int __mark_extent(struct btree_trans *trans,
+                        enum btree_id btree_id, unsigned level,
+                        struct bkey_s_c k, unsigned flags)
 {
        u64 journal_seq = trans->journal_res.seq;
        struct bch_fs *c = trans->c;
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
        struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
        const union bch_extent_entry *entry;
        struct extent_ptr_decoded p;
@@ -1031,6 +1011,14 @@ int bch2_mark_extent(struct btree_trans *trans,
        return 0;
 }
 
+int bch2_mark_extent(struct btree_trans *trans,
+                    enum btree_id btree_id, unsigned level,
+                    struct bkey_s_c old, struct bkey_s_c new,
+                    unsigned flags)
+{
+       return mem_trigger_run_overwrite_then_insert(__mark_extent, trans, btree_id, level, old, new, flags);
+}
+
 int bch2_mark_stripe(struct btree_trans *trans,
                     enum btree_id btree_id, unsigned level,
                     struct bkey_s_c old, struct bkey_s_c new,
@@ -1137,45 +1125,11 @@ int bch2_mark_stripe(struct btree_trans *trans,
        return 0;
 }
 
-int bch2_mark_inode(struct btree_trans *trans,
-                   enum btree_id btree_id, unsigned level,
-                   struct bkey_s_c old, struct bkey_s_c new,
-                   unsigned flags)
-{
-       struct bch_fs *c = trans->c;
-       struct bch_fs_usage *fs_usage;
-       u64 journal_seq = trans->journal_res.seq;
-
-       if (flags & BTREE_TRIGGER_INSERT) {
-               struct bch_inode_v3 *v = (struct bch_inode_v3 *) new.v;
-
-               BUG_ON(!journal_seq);
-               BUG_ON(new.k->type != KEY_TYPE_inode_v3);
-
-               v->bi_journal_seq = cpu_to_le64(journal_seq);
-       }
-
-       if (flags & BTREE_TRIGGER_GC) {
-               percpu_down_read(&c->mark_lock);
-               preempt_disable();
-
-               fs_usage = fs_usage_ptr(c, journal_seq, flags & BTREE_TRIGGER_GC);
-               fs_usage->nr_inodes += bkey_is_inode(new.k);
-               fs_usage->nr_inodes -= bkey_is_inode(old.k);
-
-               preempt_enable();
-               percpu_up_read(&c->mark_lock);
-       }
-       return 0;
-}
-
-int bch2_mark_reservation(struct btree_trans *trans,
-                         enum btree_id btree_id, unsigned level,
-                         struct bkey_s_c old, struct bkey_s_c new,
-                         unsigned flags)
+static int __mark_reservation(struct btree_trans *trans,
+                             enum btree_id btree_id, unsigned level,
+                             struct bkey_s_c k, unsigned flags)
 {
        struct bch_fs *c = trans->c;
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
        struct bch_fs_usage *fs_usage;
        unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
        s64 sectors = (s64) k.k->size;
@@ -1202,6 +1156,14 @@ int bch2_mark_reservation(struct btree_trans *trans,
        return 0;
 }
 
+int bch2_mark_reservation(struct btree_trans *trans,
+                         enum btree_id btree_id, unsigned level,
+                         struct bkey_s_c old, struct bkey_s_c new,
+                         unsigned flags)
+{
+       return mem_trigger_run_overwrite_then_insert(__mark_reservation, trans, btree_id, level, old, new, flags);
+}
+
 static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
                                 struct bkey_s_c_reflink_p p,
                                 u64 start, u64 end,
@@ -1228,7 +1190,8 @@ static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
        *idx = r->offset;
        return 0;
 not_found:
-       if (fsck_err(c, "pointer to missing indirect extent\n"
+       if (fsck_err(c, reflink_p_to_missing_reflink_v,
+                    "pointer to missing indirect extent\n"
                     "  %s\n"
                     "  missing range %llu-%llu",
                     (bch2_bkey_val_to_text(&buf, c, p.s_c), buf.buf),
@@ -1245,7 +1208,7 @@ not_found:
                new->k.p                = bkey_start_pos(p.k);
                new->k.p.offset += *idx - start;
                bch2_key_resize(&new->k, next_idx - *idx);
-               ret = __bch2_btree_insert(trans, BTREE_ID_extents, &new->k_i,
+               ret = bch2_btree_insert_trans(trans, BTREE_ID_extents, &new->k_i,
                                          BTREE_TRIGGER_NORUN);
        }
 
@@ -1256,13 +1219,11 @@ fsck_err:
        return ret;
 }
 
-int bch2_mark_reflink_p(struct btree_trans *trans,
-                       enum btree_id btree_id, unsigned level,
-                       struct bkey_s_c old, struct bkey_s_c new,
-                       unsigned flags)
+static int __mark_reflink_p(struct btree_trans *trans,
+                           enum btree_id btree_id, unsigned level,
+                           struct bkey_s_c k, unsigned flags)
 {
        struct bch_fs *c = trans->c;
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old : new;
        struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
        struct reflink_gc *ref;
        size_t l, r, m;
@@ -1272,7 +1233,7 @@ int bch2_mark_reflink_p(struct btree_trans *trans,
 
        BUG_ON(!(flags & BTREE_TRIGGER_GC));
 
-       if (c->sb.version >= bcachefs_metadata_version_reflink_p_fix) {
+       if (c->sb.version_upgrade_complete >= bcachefs_metadata_version_reflink_p_fix) {
                idx -= le32_to_cpu(p.v->front_pad);
                end += le32_to_cpu(p.v->back_pad);
        }
@@ -1296,6 +1257,14 @@ int bch2_mark_reflink_p(struct btree_trans *trans,
        return ret;
 }
 
+int bch2_mark_reflink_p(struct btree_trans *trans,
+                       enum btree_id btree_id, unsigned level,
+                       struct bkey_s_c old, struct bkey_s_c new,
+                       unsigned flags)
+{
+       return mem_trigger_run_overwrite_then_insert(__mark_reflink_p, trans, btree_id, level, old, new, flags);
+}
+
 void bch2_trans_fs_usage_revert(struct btree_trans *trans,
                                struct replicas_delta_list *deltas)
 {
@@ -1343,8 +1312,8 @@ int bch2_trans_fs_usage_apply(struct btree_trans *trans,
        struct bch_fs *c = trans->c;
        static int warned_disk_usage = 0;
        bool warn = false;
-       unsigned disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
-       struct replicas_delta *d = deltas->d, *d2;
+       u64 disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
+       struct replicas_delta *d, *d2;
        struct replicas_delta *top = (void *) deltas->d + deltas->used;
        struct bch_fs_usage *dst;
        s64 added = 0, should_not_have_added;
@@ -1402,7 +1371,7 @@ int bch2_trans_fs_usage_apply(struct btree_trans *trans,
 
        if (unlikely(warn) && !xchg(&warned_disk_usage, 1))
                bch2_trans_inconsistent(trans,
-                                       "disk usage increased %lli more than %u sectors reserved)",
+                                       "disk usage increased %lli more than %llu sectors reserved)",
                                        should_not_have_added, disk_res_sectors);
        return 0;
 need_mark:
@@ -1491,21 +1460,17 @@ static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
 
        bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(&s->k_i));
        r.e.data_type = data_type;
-       ret = update_replicas_list(trans, &r.e, sectors);
+       ret = bch2_update_replicas_list(trans, &r.e, sectors);
 err:
        bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
 
-int bch2_trans_mark_extent(struct btree_trans *trans,
-                          enum btree_id btree_id, unsigned level,
-                          struct bkey_s_c old, struct bkey_i *new,
-                          unsigned flags)
+static int __trans_mark_extent(struct btree_trans *trans,
+                              enum btree_id btree_id, unsigned level,
+                              struct bkey_s_c k, unsigned flags)
 {
        struct bch_fs *c = trans->c;
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
-               ? old
-               : bkey_i_to_s_c(new);
        struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
        const union bch_extent_entry *entry;
        struct extent_ptr_decoded p;
@@ -1538,8 +1503,8 @@ int bch2_trans_mark_extent(struct btree_trans *trans,
 
                if (p.ptr.cached) {
                        if (!stale) {
-                               ret = update_cached_sectors_list(trans, p.ptr.dev,
-                                                                disk_sectors);
+                               ret = bch2_update_cached_sectors_list(trans, p.ptr.dev,
+                                                                     disk_sectors);
                                if (ret)
                                        return ret;
                        }
@@ -1557,11 +1522,29 @@ int bch2_trans_mark_extent(struct btree_trans *trans,
        }
 
        if (r.e.nr_devs)
-               ret = update_replicas_list(trans, &r.e, dirty_sectors);
+               ret = bch2_update_replicas_list(trans, &r.e, dirty_sectors);
 
        return ret;
 }
 
+int bch2_trans_mark_extent(struct btree_trans *trans,
+                          enum btree_id btree_id, unsigned level,
+                          struct bkey_s_c old, struct bkey_i *new,
+                          unsigned flags)
+{
+       struct bch_fs *c = trans->c;
+       int mod = (int) bch2_bkey_needs_rebalance(c, bkey_i_to_s_c(new)) -
+                 (int) bch2_bkey_needs_rebalance(c, old);
+
+       if (mod) {
+               int ret = bch2_btree_bit_mod(trans, BTREE_ID_rebalance_work, new->k.p, mod > 0);
+               if (ret)
+                       return ret;
+       }
+
+       return trigger_run_overwrite_then_insert(__trans_mark_extent, trans, btree_id, level, old, new, flags);
+}
+
 static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
                                         struct bkey_s_c_stripe s,
                                         unsigned idx, bool deleting)
@@ -1584,7 +1567,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
 
        ret = check_bucket_ref(trans, s.s_c, ptr, sectors, data_type,
                               a->v.gen, a->v.data_type,
-                              a->v.dirty_sectors, a->v.cached_sectors);
+                              a->v.dirty_sectors);
        if (ret)
                goto err;
 
@@ -1676,7 +1659,7 @@ int bch2_trans_mark_stripe(struct btree_trans *trans,
                s64 sectors = le16_to_cpu(new_s->sectors);
 
                bch2_bkey_to_replicas(&r.e, bkey_i_to_s_c(new));
-               ret = update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
+               ret = bch2_update_replicas_list(trans, &r.e, sectors * new_s->nr_redundant);
                if (ret)
                        return ret;
        }
@@ -1685,7 +1668,7 @@ int bch2_trans_mark_stripe(struct btree_trans *trans,
                s64 sectors = -((s64) le16_to_cpu(old_s->sectors));
 
                bch2_bkey_to_replicas(&r.e, old);
-               ret = update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
+               ret = bch2_update_replicas_list(trans, &r.e, sectors * old_s->nr_redundant);
                if (ret)
                        return ret;
        }
@@ -1715,36 +1698,10 @@ int bch2_trans_mark_stripe(struct btree_trans *trans,
        return ret;
 }
 
-int bch2_trans_mark_inode(struct btree_trans *trans,
-                         enum btree_id btree_id, unsigned level,
-                         struct bkey_s_c old,
-                         struct bkey_i *new,
-                         unsigned flags)
-{
-       int nr = bkey_is_inode(&new->k) - bkey_is_inode(old.k);
-
-       if (nr) {
-               int ret = replicas_deltas_realloc(trans, 0);
-               struct replicas_delta_list *d = trans->fs_usage_deltas;
-
-               if (ret)
-                       return ret;
-
-               d->nr_inodes += nr;
-       }
-
-       return 0;
-}
-
-int bch2_trans_mark_reservation(struct btree_trans *trans,
-                               enum btree_id btree_id, unsigned level,
-                               struct bkey_s_c old,
-                               struct bkey_i *new,
-                               unsigned flags)
+static int __trans_mark_reservation(struct btree_trans *trans,
+                                   enum btree_id btree_id, unsigned level,
+                                   struct bkey_s_c k, unsigned flags)
 {
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
-               ? old
-               : bkey_i_to_s_c(new);
        unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
        s64 sectors = (s64) k.k->size;
        struct replicas_delta_list *d;
@@ -1754,7 +1711,7 @@ int bch2_trans_mark_reservation(struct btree_trans *trans,
                sectors = -sectors;
        sectors *= replicas;
 
-       ret = replicas_deltas_realloc(trans, 0);
+       ret = bch2_replicas_deltas_realloc(trans, 0);
        if (ret)
                return ret;
 
@@ -1766,7 +1723,16 @@ int bch2_trans_mark_reservation(struct btree_trans *trans,
        return 0;
 }
 
-static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
+int bch2_trans_mark_reservation(struct btree_trans *trans,
+                               enum btree_id btree_id, unsigned level,
+                               struct bkey_s_c old,
+                               struct bkey_i *new,
+                               unsigned flags)
+{
+       return trigger_run_overwrite_then_insert(__trans_mark_reservation, trans, btree_id, level, old, new, flags);
+}
+
+static int trans_mark_reflink_p_segment(struct btree_trans *trans,
                        struct bkey_s_c_reflink_p p,
                        u64 *idx, unsigned flags)
 {
@@ -1833,35 +1799,38 @@ err:
        return ret;
 }
 
-int bch2_trans_mark_reflink_p(struct btree_trans *trans,
-                             enum btree_id btree_id, unsigned level,
-                             struct bkey_s_c old,
-                             struct bkey_i *new,
-                             unsigned flags)
+static int __trans_mark_reflink_p(struct btree_trans *trans,
+                               enum btree_id btree_id, unsigned level,
+                               struct bkey_s_c k, unsigned flags)
 {
-       struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
-               ? old
-               : bkey_i_to_s_c(new);
        struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
        u64 idx, end_idx;
        int ret = 0;
 
-       if (flags & BTREE_TRIGGER_INSERT) {
-               struct bch_reflink_p *v = (struct bch_reflink_p *) p.v;
-
-               v->front_pad = v->back_pad = 0;
-       }
-
        idx     = le64_to_cpu(p.v->idx) - le32_to_cpu(p.v->front_pad);
        end_idx = le64_to_cpu(p.v->idx) + p.k->size +
                le32_to_cpu(p.v->back_pad);
 
        while (idx < end_idx && !ret)
-               ret = __bch2_trans_mark_reflink_p(trans, p, &idx, flags);
-
+               ret = trans_mark_reflink_p_segment(trans, p, &idx, flags);
        return ret;
 }
 
+int bch2_trans_mark_reflink_p(struct btree_trans *trans,
+                             enum btree_id btree_id, unsigned level,
+                             struct bkey_s_c old,
+                             struct bkey_i *new,
+                             unsigned flags)
+{
+       if (flags & BTREE_TRIGGER_INSERT) {
+               struct bch_reflink_p *v = &bkey_i_to_reflink_p(new)->v;
+
+               v->front_pad = v->back_pad = 0;
+       }
+
+       return trigger_run_overwrite_then_insert(__trans_mark_reflink_p, trans, btree_id, level, old, new, flags);
+}
+
 static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
                                    struct bch_dev *ca, size_t b,
                                    enum bch_data_type type,
@@ -1884,6 +1853,7 @@ static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
 
        if (a->v.data_type && type && a->v.data_type != type) {
                bch2_fsck_err(c, FSCK_CAN_IGNORE|FSCK_NEED_FSCK,
+                             BCH_FSCK_ERR_bucket_metadata_type_mismatch,
                        "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
                        "while marking %s",
                        iter.pos.inode, iter.pos.offset, a->v.gen,
@@ -1891,16 +1861,16 @@ static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
                        bch2_data_types[type],
                        bch2_data_types[type]);
                ret = -EIO;
-               goto out;
+               goto err;
        }
 
-       a->v.data_type          = type;
-       a->v.dirty_sectors      = sectors;
-
-       ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
-       if (ret)
-               goto out;
-out:
+       if (a->v.data_type      != type ||
+           a->v.dirty_sectors  != sectors) {
+               a->v.data_type          = type;
+               a->v.dirty_sectors      = sectors;
+               ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
+       }
+err:
        bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
@@ -1988,12 +1958,29 @@ static int __bch2_trans_mark_dev_sb(struct btree_trans *trans,
 
 int bch2_trans_mark_dev_sb(struct bch_fs *c, struct bch_dev *ca)
 {
-       int ret = bch2_trans_run(c, __bch2_trans_mark_dev_sb(&trans, ca));
+       int ret = bch2_trans_run(c, __bch2_trans_mark_dev_sb(trans, ca));
+
        if (ret)
                bch_err_fn(c, ret);
        return ret;
 }
 
+int bch2_trans_mark_dev_sbs(struct bch_fs *c)
+{
+       struct bch_dev *ca;
+       unsigned i;
+
+       for_each_online_member(ca, c, i) {
+               int ret = bch2_trans_mark_dev_sb(c, ca);
+               if (ret) {
+                       percpu_ref_put(&ca->ref);
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
 /* Disk reservations: */
 
 #define SECTORS_CACHE  1024
@@ -2094,8 +2081,6 @@ int bch2_dev_buckets_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
        bucket_gens->first_bucket = ca->mi.first_bucket;
        bucket_gens->nbuckets   = nbuckets;
 
-       bch2_copygc_stop(c);
-
        if (resize) {
                down_write(&c->gc_lock);
                down_write(&ca->bucket_lock);