public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
From: Nikita Ofitserov via B4 Relay <devnull+himikof.gmail.com@kernel.org>
To: Kent Overstreet <kent.overstreet@linux.dev>
Cc: linux-bcachefs@vger.kernel.org, Nikita Ofitserov <himikof@gmail.com>
Subject: [PATCH v2 08/15] bcachefs: Optimize/fix bch2_dev_usage_remove
Date: Thu, 28 Aug 2025 17:16:10 +0300	[thread overview]
Message-ID: <20250828-better-progress-v2-8-c088a2003940@gmail.com> (raw)
In-Reply-To: <20250828-better-progress-v2-0-c088a2003940@gmail.com>

From: Nikita Ofitserov <himikof@gmail.com>

Make bch2_dev_usage_remove iterate only through relevant keys instead
of the whole accounting tree. Use the proper bch2_disk_accounting_mod
API to ensure in-memory counters will be zeroed and cleaned up later.

Signed-off-by: Nikita Ofitserov <himikof@gmail.com>
---
 fs/bcachefs/alloc_background.c |  2 +-
 fs/bcachefs/disk_accounting.c  | 47 +++++++++++++++++++++++++++++++-----------
 fs/bcachefs/disk_accounting.h  |  2 +-
 3 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/fs/bcachefs/alloc_background.c b/fs/bcachefs/alloc_background.c
index 3fc728efbf5cc0425b966a0d83d97b33a666c531..0a2b630ab8420b404990eb5a4359c9295b926050 100644
--- a/fs/bcachefs/alloc_background.c
+++ b/fs/bcachefs/alloc_background.c
@@ -2397,7 +2397,7 @@ int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
 					BTREE_TRIGGER_norun, NULL) ?:
 		bch2_btree_delete_range(c, BTREE_ID_alloc, start, end,
 					BTREE_TRIGGER_norun, NULL) ?:
-		bch2_dev_usage_remove(c, ca->dev_idx);
+		bch2_dev_usage_remove(c, ca);
 	bch_err_msg(ca, ret, "removing dev alloc info");
 	return ret;
 }
diff --git a/fs/bcachefs/disk_accounting.c b/fs/bcachefs/disk_accounting.c
index d6c91abcdc4140ef98b407fa580b7cd0cf3b8f56..82ce610a7e40c86d84296a47ef7ea7b5eb82f816 100644
--- a/fs/bcachefs/disk_accounting.c
+++ b/fs/bcachefs/disk_accounting.c
@@ -974,21 +974,44 @@ int bch2_accounting_read(struct bch_fs *c)
 	return ret;
 }
 
-int bch2_dev_usage_remove(struct bch_fs *c, unsigned dev)
+int bch2_dev_usage_remove(struct bch_fs *c, struct bch_dev *ca)
 {
 	CLASS(btree_trans, trans)(c);
+
+	struct disk_accounting_pos start;
+	disk_accounting_key_init(start, dev_data_type, .dev = ca->dev_idx);
+
+	struct disk_accounting_pos end;
+	disk_accounting_key_init(end, dev_data_type, .dev = ca->dev_idx, .data_type = U8_MAX);
+
 	return bch2_btree_write_buffer_flush_sync(trans) ?:
-		for_each_btree_key_commit(trans, iter, BTREE_ID_accounting, POS_MIN,
-				BTREE_ITER_all_snapshots, k, NULL, NULL, 0, ({
-			struct disk_accounting_pos acc;
-			bpos_to_disk_accounting_pos(&acc, k.k->p);
-
-			acc.type == BCH_DISK_ACCOUNTING_dev_data_type &&
-			acc.dev_data_type.dev == dev
-				? bch2_btree_bit_mod_buffered(trans, BTREE_ID_accounting, k.k->p, 0)
-				: 0;
-		})) ?:
-		bch2_btree_write_buffer_flush_sync(trans);
+		commit_do(trans, NULL, NULL, 0, ({
+			struct bkey_s_c k;
+			int ret = 0;
+
+			for_each_btree_key_max_norestart(trans, iter, BTREE_ID_accounting,
+							 disk_accounting_pos_to_bpos(&start),
+							 disk_accounting_pos_to_bpos(&end),
+							 BTREE_ITER_all_snapshots, k, ret) {
+				if (k.k->type != KEY_TYPE_accounting)
+					continue;
+
+				struct disk_accounting_pos acc;
+				bpos_to_disk_accounting_pos(&acc, k.k->p);
+
+				const unsigned nr = bch2_accounting_counters(k.k);
+				u64 v[BCH_ACCOUNTING_MAX_COUNTERS];
+				memcpy_u64s_small(v, bkey_s_c_to_accounting(k).v->d, nr);
+
+				bch2_u64s_neg(v, nr);
+
+				ret = bch2_disk_accounting_mod(trans, &acc, v, nr, false);
+				if (ret)
+					break;
+			}
+
+			ret;
+	})) ?: bch2_btree_write_buffer_flush_sync(trans);
 }
 
 int bch2_dev_usage_init(struct bch_dev *ca, bool gc)
diff --git a/fs/bcachefs/disk_accounting.h b/fs/bcachefs/disk_accounting.h
index cc73cce98a447d96ad40683b88bfbc12689d8bf2..438628b63a8065e22ec76aebcd69e285f84f41ef 100644
--- a/fs/bcachefs/disk_accounting.h
+++ b/fs/bcachefs/disk_accounting.h
@@ -297,7 +297,7 @@ int bch2_gc_accounting_done(struct bch_fs *);
 
 int bch2_accounting_read(struct bch_fs *);
 
-int bch2_dev_usage_remove(struct bch_fs *, unsigned);
+int bch2_dev_usage_remove(struct bch_fs *, struct bch_dev *);
 int bch2_dev_usage_init(struct bch_dev *, bool);
 
 void bch2_verify_accounting_clean(struct bch_fs *c);

-- 
2.50.1



  parent reply	other threads:[~2025-08-28 14:16 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28 14:16 [PATCH v2 00/15] Accounting for accurate progress reporting Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 01/15] bcachefs: Introduce btree_leaf_has_triggers_mask Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 02/15] bcachefs: Refactor bch2_gc_btree/bch2_gc_btrees Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 03/15] bcachefs: Improve check_allocations pass speed when not in fsck Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 04/15] bcachefs: Refactor/rename btree_type_has_ptrs Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 05/15] bcachefs: Fix progress reporting for unknown btrees Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 06/15] bcachefs: Partially fix old device removal with " Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 07/15] bcachefs: Fix missing c->usage updates from early recovery Nikita Ofitserov via B4 Relay
2025-09-01 23:55   ` Kent Overstreet
2025-08-28 14:16 ` Nikita Ofitserov via B4 Relay [this message]
2025-08-28 14:16 ` [PATCH v2 09/15] bcachefs: Fix online hidden (sb+journal) data accounting Nikita Ofitserov via B4 Relay
2025-09-02  0:14   ` Kent Overstreet
2025-08-28 14:16 ` [PATCH v2 10/15] bcachefs: Fix outdated documentation comment Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 11/15] bcachefs: Relax restrictions on the number of accounting counters Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 12/15] bcachefs: bcachefs_metadata_version_btree_node_accounting Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 13/15] bcachefs: Use explicit node counts in progress reporting Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 14/15] bcachefs: Better progress reporting for btree iteration without leaves Nikita Ofitserov via B4 Relay
2025-08-28 14:16 ` [PATCH v2 15/15] bcachefs: More accurate progress reporting for inner node iteration Nikita Ofitserov via B4 Relay
2025-09-01 22:33 ` [PATCH v2 00/15] Accounting for accurate progress reporting Kent Overstreet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250828-better-progress-v2-8-c088a2003940@gmail.com \
    --to=devnull+himikof.gmail.com@kernel.org \
    --cc=himikof@gmail.com \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcachefs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox