bcachefs: Disk space accounting fix
authorKent Overstreet <kent.overstreet@gmail.com>
Tue, 17 Aug 2021 19:29:21 +0000 (15:29 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:10 +0000 (17:09 -0400)
DIV_ROUND_UP() wasn't doing what we wanted when passing it negative
numbers - fix it by just not passing it negative numbers anymore.

Also, no need to do the scaling by compression ratio for incompressible
data.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
fs/bcachefs/buckets.c

index 71900e7e921f5ec681c3be637d80824ed7acc929..e00c02dcb63ea9e92eaf55021f26dc69572e49db 100644 (file)
@@ -666,7 +666,10 @@ void bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
 
 static s64 ptr_disk_sectors(s64 sectors, struct extent_ptr_decoded p)
 {
-       return p.crc.compression_type
+       EBUG_ON(sectors < 0);
+
+       return p.crc.compression_type &&
+               p.crc.compression_type != BCH_COMPRESSION_TYPE_incompressible
                ? DIV_ROUND_UP(sectors * p.crc.compressed_size,
                               p.crc.uncompressed_size)
                : sectors;
@@ -929,9 +932,6 @@ static int bch2_mark_extent(struct bch_fs *c,
        BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
               (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
 
-       if (flags & BTREE_TRIGGER_OVERWRITE)
-               sectors = -sectors;
-
        r.e.data_type   = data_type;
        r.e.nr_devs     = 0;
        r.e.nr_required = 1;
@@ -939,6 +939,9 @@ static int bch2_mark_extent(struct bch_fs *c,
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
                s64 disk_sectors = ptr_disk_sectors(sectors, p);
 
+               if (flags & BTREE_TRIGGER_OVERWRITE)
+                       disk_sectors = -disk_sectors;
+
                ret = bch2_mark_pointer(c, k, p, disk_sectors, data_type,
                                        journal_seq, flags);
                if (ret < 0)
@@ -1549,9 +1552,6 @@ static int bch2_trans_mark_extent(struct btree_trans *trans,
        BUG_ON((flags & (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE)) ==
               (BTREE_TRIGGER_INSERT|BTREE_TRIGGER_OVERWRITE));
 
-       if (flags & BTREE_TRIGGER_OVERWRITE)
-               sectors = -sectors;
-
        r.e.data_type   = data_type;
        r.e.nr_devs     = 0;
        r.e.nr_required = 1;
@@ -1559,6 +1559,9 @@ static int bch2_trans_mark_extent(struct btree_trans *trans,
        bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
                s64 disk_sectors = ptr_disk_sectors(sectors, p);
 
+               if (flags & BTREE_TRIGGER_OVERWRITE)
+                       disk_sectors = -disk_sectors;
+
                ret = bch2_trans_mark_pointer(trans, k, p,
                                        disk_sectors, data_type);
                if (ret < 0)