Skip to contentSkip to conversation

fix buffered write path on -ENOSPC

PATCH In review Kent Overstreet 2 participants 5 messages Oct 18, 2024

lore
Share & tools
lore
#0 lore raw

cover fix buffered write path on -ENOSPC

xfstests generic/299 turned up a data corruption when we do a short write because of -ENOSPC - when we truncate the write we might no longer be doing a full write to a !uptodate folio, which then needs to be read in.

Amusingly, fixing this turned up a bug in fio where verify mode behaves badly in the presence of short writes, which took longer to figure out than the original bug...

Kent Overstreet (3):
bcachefs: fix disk reservation accounting in bch2_folio_reservation_get()
bcachefs: bch2_folio_reservation_get_partial() is now better behaved
bcachefs: Fix data corruption on -ENOSPC in buffered write path

 fs/bcachefs/buckets.c         |  7 +++-
 fs/bcachefs/buckets.h         | 12 +++---
 fs/bcachefs/fs-io-buffered.c  |  6 +++
 fs/bcachefs/fs-io-pagecache.c | 70 ++++++++++++++++++++++-------------
 4 files changed, 63 insertions(+), 32 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/3 bcachefs: fix disk reservation accounting in bch2_folio_reservation_get()

bch2_disk_reservation_put() zeroes out the reservation - oops.

This fixes a disk reservation leak when getting a quota reservation returned an error.

---
 fs/bcachefs/fs-io-pagecache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/fs-io-pagecache.c +1 -1
index af3a24546aa3..cde3a0445ee9 100644
@@ -431,9 +431,9 @@ int bch2_folio_reservation_get(struct bch_fs *c,
 		ret = bch2_quota_reservation_add(c, inode, &res->quota, quota_sectors, true);
 		if (unlikely(ret)) {
 			struct disk_reservation tmp = { .sectors = disk_sectors };
+			res->disk.sectors -= disk_sectors;
 
 			bch2_disk_reservation_put(c, &tmp);
-			res->disk.sectors -= disk_sectors;
 			return ret;
 		}
 	}
Signature · 1 line
2.45.2
#2 lore raw

Re: [PATCH 1/3] bcachefs: fix disk reservation accounting in bch2_folio_reservation_get()

Quoted 15 lines · Kent Overstreet

bch2_disk_reservation_put() zeroes out the reservation - oops.

This fixes a disk reservation leak when getting a quota reservation returned an error.

---
  fs/bcachefs/fs-io-pagecache.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/fs-io-pagecache.c +0 -0
index af3a24546aa3..cde3a0445ee9 100644

fs/bcachefs/fs-io-pagecache.c int bch2_folio_reservation_get(struct bch_fs *c, · line 436

Quoted 7 lines
fs/bcachefs/fs-io-pagecache.c +1 -1
@@ -431,9 +431,9 @@ int bch2_folio_reservation_get(struct bch_fs *c,
  		ret = bch2_quota_reservation_add(c, inode, &res->quota, quota_sectors, true);
  		if (unlikely(ret)) {
  			struct disk_reservation tmp = { .sectors = disk_sectors };
+			res->disk.sectors -= disk_sectors;
 
  			bch2_disk_reservation_put(c, &tmp);
-			res->disk.sectors -= disk_sectors;

I am new to bcachefs, but seems bch2_disk_reservation_put zeros out tmp->sectors, no affect to 'res' ?

Quoted 3 lines
fs/bcachefs/fs-io-pagecache.c +0 -0
  			return ret;
  		}
  	}
Signature · 1 line
Regards
#3 lore raw

PATCH 2/3 bcachefs: bch2_folio_reservation_get_partial() is now better behaved

In reply to Kent Overstreet

bch2_folio_reservation_get_partial(), on partial success, will now return a reservation that's aligned to the filesystem blocksize.

This is a partial fix for fstests generic/299 - fio verify is badly behaved in the presence of short writes that aren't aligned to its blocksize.

---
 fs/bcachefs/buckets.c         |  7 +++-
 fs/bcachefs/buckets.h         | 12 +++---
 fs/bcachefs/fs-io-pagecache.c | 70 ++++++++++++++++++++++-------------
 3 files changed, 57 insertions(+), 32 deletions(-)
fs/bcachefs/buckets.c +5 -2
index 546cd01a72e3..ec7d9a59bea9 100644
@@ -1160,11 +1160,11 @@ int bch2_trans_mark_dev_sbs(struct bch_fs *c)
 #define SECTORS_CACHE	1024
 
 int __bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
-			      u64 sectors, int flags)
+				u64 sectors, enum bch_reservation_flags flags)
 {
 	struct bch_fs_pcpu *pcpu;
 	u64 old, get;
-	s64 sectors_available;
+	u64 sectors_available;
 	int ret;
 
 	percpu_down_read(&c->mark_lock);
@@ -1202,6 +1202,9 @@ int __bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
 	percpu_u64_set(&c->pcpu->sectors_available, 0);
 	sectors_available = avail_factor(__bch2_fs_usage_read_short(c).free);
 
+	if (sectors_available && (flags & BCH_DISK_RESERVATION_PARTIAL))
+		sectors = min(sectors, sectors_available);
+
 	if (sectors <= sectors_available ||
 	    (flags & BCH_DISK_RESERVATION_NOFAIL)) {
 		atomic64_set(&c->sectors_available,
fs/bcachefs/buckets.h +7 -5
index e2cb7b24b220..fd5e6ccad45e 100644
@@ -344,14 +344,16 @@ static inline void bch2_disk_reservation_put(struct bch_fs *c,
 	}
 }
 
-#define BCH_DISK_RESERVATION_NOFAIL		(1 << 0)
+enum bch_reservation_flags {
+	BCH_DISK_RESERVATION_NOFAIL	= 1 << 0,
+	BCH_DISK_RESERVATION_PARTIAL	= 1 << 1,
+};
 
-int __bch2_disk_reservation_add(struct bch_fs *,
-				struct disk_reservation *,
-				u64, int);
+int __bch2_disk_reservation_add(struct bch_fs *, struct disk_reservation *,
+				u64, enum bch_reservation_flags);
 
 static inline int bch2_disk_reservation_add(struct bch_fs *c, struct disk_reservation *res,
-					    u64 sectors, int flags)
+					    u64 sectors, enum bch_reservation_flags flags)
 {
 #ifdef __KERNEL__
 	u64 old, new;
fs/bcachefs/fs-io-pagecache.c +45 -25

107 lines not on this page · raw message

Signature · 1 line
2.45.2
#4 lore raw

PATCH 3/3 bcachefs: Fix data corruption on -ENOSPC in buffered write path

In reply to Kent Overstreet

Found by generic/299: When we have to truncate a write due to -ENOSPC, we may have to read in the folio we're writing to if we're now no longer doing a complete write to a !uptodate folio.

---
 fs/bcachefs/fs-io-buffered.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/bcachefs/fs-io-buffered.c +6 -0
index 48a1ab9a649b..95972809e76d 100644
@@ -856,6 +856,12 @@ static int __bch2_buffered_write(struct bch_inode_info *inode,
 				folios_trunc(&fs, fi);
 				end = min(end, folio_end_pos(darray_last(fs)));
 			} else {
+				if (!folio_test_uptodate(f)) {
+					ret = bch2_read_single_folio(f, mapping);
+					if (ret)
+						goto out;
+				}
+
 				folios_trunc(&fs, fi + 1);
 				end = f_pos + f_reserved;
 			}
Signature · 1 line
2.45.2