From 406d6d5a0733f3eacbcd4c33905bf63c5b2f4c07 Mon Sep 17 00:00:00 2001 From: Kent Overstreet <kent.overstreet@gmail.com> Date: Fri, 25 Oct 2019 18:54:58 -0400 Subject: [PATCH] bcachefs: Fix an error path race On IO error, bch2_writepages_io_done() will set the page state to indicate nothing's already reserved (since the write didn't happen, we don't know what's already reserved). This can race with the buffered IO path, in between getting a disk reservation and calling bch2_set_page_dirty(). Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> --- fs/bcachefs/fs-io.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/bcachefs/fs-io.c b/fs/bcachefs/fs-io.c index c539ed3aa48dd..93f6cdbbf7c72 100644 --- a/fs/bcachefs/fs-io.c +++ b/fs/bcachefs/fs-io.c @@ -491,7 +491,12 @@ static void bch2_set_page_dirty(struct bch_fs *c, unsigned sectors = sectors_to_reserve(&s->s[i], res->disk.nr_replicas); - BUG_ON(sectors > res->disk.sectors); + /* + * This can happen if we race with the error path in + * bch2_writepage_io_done(): + */ + sectors = min_t(unsigned, sectors, res->disk.sectors); + s->s[i].replicas_reserved += sectors; res->disk.sectors -= sectors; -- 2.30.2