summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2021-06-10 23:33:27 -0400
committerKent Overstreet <kent.overstreet@gmail.com>2021-07-06 13:03:50 -0400
commitd73739dea7541ccf4eca16b45cafac845dba7eab (patch)
tree641dcaaffea9a6cd2d6db1c5fa24bbe654ec6830
parent9454e4c9eb31af32ad458da365bb515e06b01871 (diff)
bcachefs: Don't underflow c->sectors_available
This rarely used error path should've been checking for underflow - oops. Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
-rw-r--r--fs/bcachefs/buckets.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/fs/bcachefs/buckets.c b/fs/bcachefs/buckets.c
index 6d5fe398007a..84f280b8525c 100644
--- a/fs/bcachefs/buckets.c
+++ b/fs/bcachefs/buckets.c
@@ -1433,7 +1433,14 @@ void bch2_trans_fs_usage_apply(struct btree_trans *trans,
*/
should_not_have_added = added - (s64) disk_res_sectors;
if (unlikely(should_not_have_added > 0)) {
- atomic64_sub(should_not_have_added, &c->sectors_available);
+ u64 old, new, v = atomic64_read(&c->sectors_available);
+
+ do {
+ old = v;
+ new = max_t(s64, 0, old - should_not_have_added);
+ } while ((v = atomic64_cmpxchg(&c->sectors_available,
+ old, new)) != old);
+
added -= should_not_have_added;
warn = true;
}