Skip to contentSkip to conversation

btrfs: protect sb_write_pointer() with invalidate lock

PATCH In review KangNing Liao 2 participants 2 messages May 21 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: protect sb_write_pointer() with invalidate lock

sb_write_pointer() reads the super block from the block device page cache using read_cache_page_gfp(). This has the same race with BLKBSZSET as the one fixed by commit 3f29d661e568 ("btrfs: sync read disk super and set block size").

Take the mapping invalidate lock around read_cache_page_gfp() to serialize the read against block size changes.

---
 fs/btrfs/zoned.c | 2 ++
 1 file changed, 2 insertions(+)
fs/btrfs/zoned.c +2 -0
index 16dd87aa06f2..5f75cf0e14b9 100644
@@ -131,8 +131,10 @@ static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
 			u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
 						BTRFS_SUPER_INFO_SIZE;
 
+			filemap_invalidate_lock(mapping);
 			page[i] = read_cache_page_gfp(mapping,
 					bytenr >> PAGE_SHIFT, GFP_NOFS);
+			filemap_invalidate_unlock(mapping);
 			if (IS_ERR(page[i])) {
 				if (i == 1)
 					btrfs_release_disk_super(super[0]);
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · KangNing Liao

sb_write_pointer() reads the super block from the block device page cache using read_cache_page_gfp(). This has the same race with BLKBSZSET as the one fixed by commit 3f29d661e568 ("btrfs: sync read disk super and set block size").

Take the mapping invalidate lock around read_cache_page_gfp() to serialize the read against block size changes.

Thanks,
Qu

Quoted 19 lines
---
  fs/btrfs/zoned.c | 2 ++
  1 file changed, 2 insertions(+)
fs/btrfs/zoned.c +2 -0
index 16dd87aa06f2..5f75cf0e14b9 100644
@@ -131,8 +131,10 @@ static int sb_write_pointer(struct block_device *bdev, struct blk_zone *zones,
  			u64 bytenr = ALIGN_DOWN(zone_end, BTRFS_SUPER_INFO_SIZE) -
  						BTRFS_SUPER_INFO_SIZE;
 
+			filemap_invalidate_lock(mapping);
  			page[i] = read_cache_page_gfp(mapping,
  					bytenr >> PAGE_SHIFT, GFP_NOFS);
+			filemap_invalidate_unlock(mapping);
  			if (IS_ERR(page[i])) {
  				if (i == 1)
  					btrfs_release_disk_super(super[0]);