bcachefs: bch2_trans_account_disk_usage_change()
authorKent Overstreet <kent.overstreet@linux.dev>
Sat, 11 Nov 2023 20:08:36 +0000 (15:08 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 21 Jan 2024 11:01:45 +0000 (06:01 -0500)
The disk space accounting rewrite is splitting out accounting for each
replicas set - those are moving to btree keys, instead of percpu
counters.

This breaks bch2_trans_fs_usage_apply() up, splitting out the part we
will still need.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/btree_trans_commit.c
fs/bcachefs/buckets.c
fs/bcachefs/buckets.h

index e3a82c33912b956f90224a7a525d96f93f26ecf9..ab00d202361eb167592792422cf047741f420dcf 100644 (file)
@@ -675,6 +675,9 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
            bch2_trans_fs_usage_apply(trans, trans->fs_usage_deltas))
                return -BCH_ERR_btree_insert_need_mark_replicas;
 
+       /* XXX: we only want to run this if deltas are nonzero */
+       bch2_trans_account_disk_usage_change(trans);
+
        h = trans->hooks;
        while (h) {
                ret = h->fn(trans, h);
@@ -989,6 +992,8 @@ int __bch2_trans_commit(struct btree_trans *trans, unsigned flags)
            !trans->journal_entries_u64s)
                goto out_reset;
 
+       memset(&trans->fs_usage_delta, 0, sizeof(trans->fs_usage_delta));
+
        ret = bch2_trans_commit_run_triggers(trans);
        if (ret)
                goto out_reset;
index f8b9be9a845793312a28be3ab68b28de4ed0afcb..54f7826ac49874d46b08330678ea0b2565ecc491 100644 (file)
@@ -694,48 +694,25 @@ void bch2_trans_fs_usage_revert(struct btree_trans *trans,
        percpu_up_read(&c->mark_lock);
 }
 
-int bch2_trans_fs_usage_apply(struct btree_trans *trans,
-                             struct replicas_delta_list *deltas)
+void bch2_trans_account_disk_usage_change(struct btree_trans *trans)
 {
        struct bch_fs *c = trans->c;
+       u64 disk_res_sectors = trans->disk_res ? trans->disk_res->sectors : 0;
        static int warned_disk_usage = 0;
        bool warn = false;
-       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;
-       unsigned i;
 
        percpu_down_read(&c->mark_lock);
        preempt_disable();
-       dst = fs_usage_ptr(c, trans->journal_res.seq, false);
-
-       for (d = deltas->d; d != top; d = replicas_delta_next(d)) {
-               switch (d->r.data_type) {
-               case BCH_DATA_btree:
-               case BCH_DATA_user:
-               case BCH_DATA_parity:
-                       added += d->delta;
-               }
-
-               if (__update_replicas(c, dst, &d->r, d->delta))
-                       goto need_mark;
-       }
+       struct bch_fs_usage_base *dst = &fs_usage_ptr(c, trans->journal_res.seq, false)->b;
+       struct bch_fs_usage_base *src = &trans->fs_usage_delta;
 
-       dst->b.nr_inodes += deltas->nr_inodes;
-
-       for (i = 0; i < BCH_REPLICAS_MAX; i++) {
-               added                           += deltas->persistent_reserved[i];
-               dst->b.reserved                 += deltas->persistent_reserved[i];
-               dst->persistent_reserved[i]     += deltas->persistent_reserved[i];
-       }
+       s64 added = src->btree + src->data + src->reserved;
 
        /*
         * Not allowed to reduce sectors_available except by getting a
         * reservation:
         */
-       should_not_have_added = added - (s64) disk_res_sectors;
+       s64 should_not_have_added = added - (s64) disk_res_sectors;
        if (unlikely(should_not_have_added > 0)) {
                u64 old, new, v = atomic64_read(&c->sectors_available);
 
@@ -754,6 +731,13 @@ int bch2_trans_fs_usage_apply(struct btree_trans *trans,
                this_cpu_sub(*c->online_reserved, added);
        }
 
+       dst->hidden     += src->hidden;
+       dst->btree      += src->btree;
+       dst->data       += src->data;
+       dst->cached     += src->cached;
+       dst->reserved   += src->reserved;
+       dst->nr_inodes  += src->nr_inodes;
+
        preempt_enable();
        percpu_up_read(&c->mark_lock);
 
@@ -761,6 +745,34 @@ int bch2_trans_fs_usage_apply(struct btree_trans *trans,
                bch2_trans_inconsistent(trans,
                                        "disk usage increased %lli more than %llu sectors reserved)",
                                        should_not_have_added, disk_res_sectors);
+}
+
+int bch2_trans_fs_usage_apply(struct btree_trans *trans,
+                             struct replicas_delta_list *deltas)
+{
+       struct bch_fs *c = trans->c;
+       struct replicas_delta *d, *d2;
+       struct replicas_delta *top = (void *) deltas->d + deltas->used;
+       struct bch_fs_usage *dst;
+       unsigned i;
+
+       percpu_down_read(&c->mark_lock);
+       preempt_disable();
+       dst = fs_usage_ptr(c, trans->journal_res.seq, false);
+
+       for (d = deltas->d; d != top; d = replicas_delta_next(d))
+               if (__update_replicas(c, dst, &d->r, d->delta))
+                       goto need_mark;
+
+       dst->b.nr_inodes += deltas->nr_inodes;
+
+       for (i = 0; i < BCH_REPLICAS_MAX; i++) {
+               dst->b.reserved                 += deltas->persistent_reserved[i];
+               dst->persistent_reserved[i]     += deltas->persistent_reserved[i];
+       }
+
+       preempt_enable();
+       percpu_up_read(&c->mark_lock);
        return 0;
 need_mark:
        /* revert changes: */
index 2b1e907f2acada63ba31d3c100ac6b5f59d778ea..6387e039f7897534e27c207dd3818dc4b6afb3b7 100644 (file)
@@ -356,6 +356,8 @@ int bch2_trigger_reservation(struct btree_trans *, enum btree_id, unsigned,
        ret;                                                                                    \
 })
 
+void bch2_trans_account_disk_usage_change(struct btree_trans *);
+
 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 *);