Skip to content

Commit bc00965

Browse files
Matthew Wilcox (Oracle)kdave
Matthew Wilcox (Oracle)
authored andcommitted
btrfs: count super block write errors in device instead of tracking folio error state
Currently the error status of super block write is tracked in page/folio status bit Error. For that we need to keep the reference for the whole duration of write and wait. Count the number of superblock writeback errors in the btrfs_device. That means we don't need the folio to stay around until it's waited for, and can avoid the extra call to folio_get/put. Also remove a mention of PageError in a comment as it's the last mention of the page Error state. Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 617fb10 commit bc00965

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

fs/btrfs/disk-io.c

+19-27
Original file line numberDiff line numberDiff line change
@@ -3634,11 +3634,15 @@ static void btrfs_end_super_write(struct bio *bio)
36343634
"lost super block write due to IO error on %s (%d)",
36353635
btrfs_dev_name(device),
36363636
blk_status_to_errno(bio->bi_status));
3637-
folio_set_error(fi.folio);
36383637
btrfs_dev_stat_inc_and_print(device,
36393638
BTRFS_DEV_STAT_WRITE_ERRS);
3639+
/* Ensure failure if the primary sb fails. */
3640+
if (bio->bi_opf & REQ_FUA)
3641+
atomic_add(BTRFS_SUPER_PRIMARY_WRITE_ERROR,
3642+
&device->sb_write_errors);
3643+
else
3644+
atomic_inc(&device->sb_write_errors);
36403645
}
3641-
36423646
folio_unlock(fi.folio);
36433647
folio_put(fi.folio);
36443648
}
@@ -3742,10 +3746,11 @@ static int write_dev_supers(struct btrfs_device *device,
37423746
struct address_space *mapping = device->bdev->bd_inode->i_mapping;
37433747
SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
37443748
int i;
3745-
int errors = 0;
37463749
int ret;
37473750
u64 bytenr, bytenr_orig;
37483751

3752+
atomic_set(&device->sb_write_errors, 0);
3753+
37493754
if (max_mirrors == 0)
37503755
max_mirrors = BTRFS_SUPER_MIRROR_MAX;
37513756

@@ -3765,7 +3770,7 @@ static int write_dev_supers(struct btrfs_device *device,
37653770
btrfs_err(device->fs_info,
37663771
"couldn't get super block location for mirror %d",
37673772
i);
3768-
errors++;
3773+
atomic_inc(&device->sb_write_errors);
37693774
continue;
37703775
}
37713776
if (bytenr + BTRFS_SUPER_INFO_SIZE >=
@@ -3785,14 +3790,11 @@ static int write_dev_supers(struct btrfs_device *device,
37853790
btrfs_err(device->fs_info,
37863791
"couldn't get super block page for bytenr %llu",
37873792
bytenr);
3788-
errors++;
3793+
atomic_inc(&device->sb_write_errors);
37893794
continue;
37903795
}
37913796
ASSERT(folio_order(folio) == 0);
37923797

3793-
/* Bump the refcount for wait_dev_supers() */
3794-
folio_get(folio);
3795-
37963798
offset = offset_in_folio(folio, bytenr);
37973799
disk_super = folio_address(folio) + offset;
37983800
memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
@@ -3820,16 +3822,17 @@ static int write_dev_supers(struct btrfs_device *device,
38203822
submit_bio(bio);
38213823

38223824
if (btrfs_advance_sb_log(device, i))
3823-
errors++;
3825+
atomic_inc(&device->sb_write_errors);
38243826
}
3825-
return errors < i ? 0 : -1;
3827+
return atomic_read(&device->sb_write_errors) < i ? 0 : -1;
38263828
}
38273829

38283830
/*
38293831
* Wait for write completion of superblocks done by write_dev_supers,
38303832
* @max_mirrors same for write and wait phases.
38313833
*
3832-
* Return number of errors when folio is not found or not marked up to date.
3834+
* Return -1 if primary super block write failed or when there were no super block
3835+
* copies written. Otherwise 0.
38333836
*/
38343837
static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
38353838
{
@@ -3860,30 +3863,19 @@ static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
38603863

38613864
folio = filemap_get_folio(device->bdev->bd_inode->i_mapping,
38623865
bytenr >> PAGE_SHIFT);
3863-
if (IS_ERR(folio)) {
3864-
errors++;
3865-
if (i == 0)
3866-
primary_failed = true;
3866+
/* If the folio has been removed, then we know it completed. */
3867+
if (IS_ERR(folio))
38673868
continue;
3868-
}
38693869
ASSERT(folio_order(folio) == 0);
38703870

38713871
/* Folio will be unlocked once the write completes. */
38723872
folio_wait_locked(folio);
3873-
if (folio_test_error(folio)) {
3874-
errors++;
3875-
if (i == 0)
3876-
primary_failed = true;
3877-
}
3878-
3879-
/* Drop our reference */
3880-
folio_put(folio);
3881-
3882-
/* Drop the reference from the writing run */
38833873
folio_put(folio);
38843874
}
38853875

3886-
/* log error, force error return */
3876+
errors += atomic_read(&device->sb_write_errors);
3877+
if (errors >= BTRFS_SUPER_PRIMARY_WRITE_ERROR)
3878+
primary_failed = true;
38873879
if (primary_failed) {
38883880
btrfs_err(device->fs_info, "error writing primary super block to device %llu",
38893881
device->devid);

fs/btrfs/extent_io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ static void set_btree_ioerr(struct extent_buffer *eb)
16021602
* can be no longer dirty nor marked anymore for writeback (if a
16031603
* subsequent modification to the extent buffer didn't happen before the
16041604
* transaction commit), which makes filemap_fdata[write|wait]_range not
1605-
* able to find the pages tagged with SetPageError at transaction
1605+
* able to find the pages which contain errors at transaction
16061606
* commit time. So if this happens we must abort the transaction,
16071607
* otherwise we commit a super block with btree roots that point to
16081608
* btree nodes/leafs whose content on disk is invalid - either garbage

fs/btrfs/volumes.h

+9
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ enum btrfs_raid_types {
9292
#define BTRFS_DEV_STATE_FLUSH_SENT (4)
9393
#define BTRFS_DEV_STATE_NO_READA (5)
9494

95+
/* Special value encoding failure to write primary super block. */
96+
#define BTRFS_SUPER_PRIMARY_WRITE_ERROR (INT_MAX / 2)
97+
9598
struct btrfs_fs_devices;
9699

97100
struct btrfs_device {
@@ -142,6 +145,12 @@ struct btrfs_device {
142145
/* type and info about this device */
143146
u64 type;
144147

148+
/*
149+
* Counter of super block write errors, values larger than
150+
* BTRFS_SUPER_PRIMARY_WRITE_ERROR encode primary super block write failure.
151+
*/
152+
atomic_t sb_write_errors;
153+
145154
/* minimal io size for this device */
146155
u32 sector_size;
147156

0 commit comments

Comments
 (0)