fix a few zoned append issues
Subsystems (2)
Hi Jens, hi Damien, hi btrfs maintainers,
this fixes a few issues found with zoned xfs testing that affect block layer interfaces used by file systems, and the btrfs code making use of them.
PATCH 1/4 block: fix bio_split_rw_at to take zone_write_granularity into account
Otherwise it can create unaligned writes on zoned devices.
--- block/blk-merge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
block/blk-merge.c +2 -1
@@ -358,7 +358,8 @@ int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim,* split size so that each bio is properly block size aligned, even if * we do not use the full hardware limits. */ - bytes =ALIGN_DOWN (bytes, lim->logical_block_size); + bytes =ALIGN_DOWN (bytes, lim->zone_write_granularity ? + lim->zone_write_granularity : lim->logical_block_size);/* * Bio splitting may cause subtle trouble such as hang when doing sync
Signature · 1 line
2.45.2
Re: [PATCH 1/4] block: fix bio_split_rw_at to take zone_write_granularity into account
Quoted 13 lines · Christoph Hellwig
Otherwise it can create unaligned writes on zoned devices.
Fixes a805a4fa4fa3 ("block: introduce zone_write_granularity limit")Signed-off-by Christoph Hellwig <[email protected]>--- block/blk-merge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)block/blk-merge.c +0
-0
block/blk-merge.c int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim, · line 362
Quoted 6 lines
block/blk-merge.c +2
-1@@ -358,7 +358,8 @@ int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim,* split size so that each bio is properly block size aligned, even if * we do not use the full hardware limits. */ - bytes =ALIGN_DOWN (bytes, lim->logical_block_size); + bytes =ALIGN_DOWN (bytes, lim->zone_write_granularity ? + lim->zone_write_granularity : lim->logical_block_size);
Nit: we could also do:
bytes = ALIGN_DOWN(bytes, max(lim->logical_block_size, lim->zone_write_granularity));
Also, I wonder if we should leave read split as is based on the logical block size only ? Probably does not matter much...
Quoted 3 lines
block/blk-merge.c +0
-0/* * Bio splitting may cause subtle trouble such as hang when doing sync
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 1/4] block: fix bio_split_rw_at to take zone_write_granularity into account
Quoted 8 lines · Damien Le Moal
block/blk-merge.c int bio_split_rw_at(struct bio *bio, const struct queue_limits *lim, · line 362
Quoted 3 lines
block/blk-merge.c +2
-1- bytes =ALIGN_DOWN (bytes, lim->logical_block_size); + bytes =ALIGN_DOWN (bytes, lim->zone_write_granularity ? + lim->zone_write_granularity : lim->logical_block_size);Nit: we could also do:
bytes = ALIGN_DOWN(bytes, max(lim->logical_block_size, lim->zone_write_granularity));
That's what I had first. It is a little odd as zone_write_granularity is defined to be >= logical_block_size, though.
Quoted 2 lines
Also, I wonder if we should leave read split as is based on the logical block size only ? Probably does not matter much...
Good point. Probably doesn't matter much, but randomly forcing it on reads seems odd.
PATCH 2/4 block: lift bio_is_zone_append to bio.h
In reply to Christoph HellwigMake bio_is_zone_append globally available, because file systems need to use to check for a zone append bio in their end_io handlers to deal with the block layer emulation.
--- block/blk.h | 9 --------- include/linux/bio.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-)
block/blk.h +0 -9
@@ -457,11 +457,6 @@ static inline bool bio_zone_write_plugging(struct bio *bio) {return bio_flagged (bio,BIO_ZONE_WRITE_PLUGGING ); } -static inline bool bio_is_zone_append (struct bio *bio) -{ -return bio_op (bio) ==REQ_OP_ZONE_APPEND || -bio_flagged (bio,BIO_EMULATES_ZONE_APPEND ); -}void blk_zone_write_plug_bio_merged (struct bio *bio);void blk_zone_write_plug_init_request (struct request *rq); static inline void blk_zone_update_request_bio(struct request *rq,
@@ -510,10 +505,6 @@ static inline bool bio_zone_write_plugging(struct bio *bio) {return false; } -static inline bool bio_is_zone_append (struct bio *bio) -{ -return false; -}static inline void blk_zone_write_plug_bio_merged (struct bio *bio) { }
include/linux/bio.h +17 -0
@@ -675,6 +675,23 @@ static inline void bio_clear_polled(struct bio *bio) bio->bi_opf &= ~REQ_POLLED ; } +/** +* bio_is_zone_append - is this a zone append bio? +* @bio: bio to check +* +* Check if @bio is a zone append operation. Core block layer code and end_io +* handlers must use this instead of an open coded REQ_OP_ZONE_APPEND check +* because the block layer can rewrite REQ_OP_ZONE_APPEND to REQ_OP_WRITE if +* it is not natively supported. +*/ +static inline bool bio_is_zone_append (struct bio *bio) +{ +if (!IS_ENABLED (CONFIG_BLK_DEV_ZONED )) +return false; +return bio_op (bio) ==REQ_OP_ZONE_APPEND || +bio_flagged (bio,BIO_EMULATES_ZONE_APPEND ); +} +struct bio *blk_next_bio (struct bio *bio,struct block_device *bdev,unsigned int nr_pages,blk_opf_t opf,gfp_t gfp);struct bio *bio_chain_and_submit (struct bio *prev,struct bio *new);
Signature · 1 line
2.45.2
Re: [PATCH 2/4] block: lift bio_is_zone_append to bio.h
Quoted 5 lines · Christoph Hellwig
Make
bio_is_zone_appendglobally available, because file systems need to use to check for a zone append bio in theirend_iohandlers to deal with the block layer emulation.Signed-off-by Christoph Hellwig <[email protected]>
Looks good (one nit below).
Quoted 43 lines
--- block/blk.h | 9 --------- include/linux/bio.h | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-)block/blk.h +0
-9@@ -457,11 +457,6 @@ static inline bool bio_zone_write_plugging(struct bio *bio) {return bio_flagged (bio,BIO_ZONE_WRITE_PLUGGING ); } -static inline bool bio_is_zone_append (struct bio *bio) -{ -return bio_op (bio) ==REQ_OP_ZONE_APPEND || -bio_flagged (bio,BIO_EMULATES_ZONE_APPEND ); -}void blk_zone_write_plug_bio_merged (struct bio *bio);void blk_zone_write_plug_init_request (struct request *rq); static inline void blk_zone_update_request_bio(struct request *rq,@@ -510,10 +505,6 @@ static inline bool bio_zone_write_plugging(struct bio *bio) {return false; } -static inline bool bio_is_zone_append (struct bio *bio) -{ -return false; -}static inline void blk_zone_write_plug_bio_merged (struct bio *bio) { }include/linux/bio.h +3
-0@@ -675,6 +675,23 @@ static inline void bio_clear_polled(struct bio *bio) bio->bi_opf &= ~REQ_POLLED ; } +/** +* bio_is_zone_append - is this a zone append bio? +* @bio: bio to check
include/linux/bio.h static inline void bio_clear_polled(struct bio *bio) · line 690
Quoted 10 lines
include/linux/bio.h +10
-0+* +* Check if @bio is a zone append operation. Core block layer code and end_io +* handlers must use this instead of an open coded REQ_OP_ZONE_APPEND check +* because the block layer can rewrite REQ_OP_ZONE_APPEND to REQ_OP_WRITE if +* it is not natively supported. +*/ +static inline bool bio_is_zone_append (struct bio *bio) +{ +if (!IS_ENABLED (CONFIG_BLK_DEV_ZONED )) +return false;
Nit: this "if" is probably not needed. But it does not hurt either. Since we should never be seeing this function being called for the !IS_ENABLED(CONFIG_BLK_DEV_ZONED) case, should we add a WARN_ON_ONCE() ?
Quoted 7 lines
include/linux/bio.h +4
-0+return bio_op (bio) ==REQ_OP_ZONE_APPEND || +bio_flagged (bio,BIO_EMULATES_ZONE_APPEND ); +} +struct bio *blk_next_bio (struct bio *bio,struct block_device *bdev,unsigned int nr_pages,blk_opf_t opf,gfp_t gfp);struct bio *bio_chain_and_submit (struct bio *prev,struct bio *new);
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 2/4] block: lift bio_is_zone_append to bio.h
Quoted 8 lines · Damien Le Moal
include/linux/bio.h static inline void bio_clear_polled(struct bio *bio) · line 690
Quoted 4 lines
include/linux/bio.h +4
-0+static inline bool bio_is_zone_append (struct bio *bio) +{ +if (!IS_ENABLED (CONFIG_BLK_DEV_ZONED )) +return false;Nit: this "if" is probably not needed. But it does not hurt either. Since we should never be seeing this function being called for the !IS_ENABLED(CONFIG_BLK_DEV_ZONED) case, should we add a
WARN_ON_ONCE()?
The point of the IS_ENALBED is to optimize away the code when it can't be used. The WARN_ON_ONCE would generate worse code than just leaving the check in.
Re: [PATCH 2/4] block: lift bio_is_zone_append to bio.h
In reply to Christoph HellwigLooks good,
PATCH 3/4 btrfs: use bio_is_zone_append in the completion handler
In reply to Christoph HellwigOtherwise it won't catch bios turned into regular writes by the block level zone write plugging.
--- fs/btrfs/bio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/bio.c +3 -3
@@ -366,7 +366,7 @@ static void btrfs_simple_end_io(struct bio *bio)INIT_WORK (&bbio->end_io_work, btrfs_end_bio_work);queue_work (btrfs_end_io_wq (fs_info, bio), &bbio->end_io_work); }else { -if (bio_op (bio) ==REQ_OP_ZONE_APPEND && !bio->bi_status) +if (bio_is_zone_append (bio) && !bio->bi_status)btrfs_record_physical_zoned (bbio);btrfs_bio_end_io (bbio, bbio->bio.bi_status); }
@@ -409,7 +409,7 @@ static void btrfs_orig_write_end_io(struct bio *bio)else bio->bi_status =BLK_STS_OK ; -if (bio_op (bio) ==REQ_OP_ZONE_APPEND && !bio->bi_status) +if (bio_is_zone_append (bio) && !bio->bi_status) stripe->physical = bio->bi_iter.bi_sector <<SECTOR_SHIFT ;btrfs_bio_end_io (bbio, bbio->bio.bi_status);
@@ -423,7 +423,7 @@ static void btrfs_clone_write_end_io(struct bio *bio)if (bio->bi_status) {atomic_inc (&stripe->bioc->error);btrfs_log_dev_io_error (bio, stripe->dev); - }else if (bio_op (bio) ==REQ_OP_ZONE_APPEND ) { + }else if (bio_is_zone_append (bio)) { stripe->physical = bio->bi_iter.bi_sector <<SECTOR_SHIFT ; }
Signature · 1 line
2.45.2
Re: [PATCH 3/4] btrfs: use bio_is_zone_append in the completion handler
Quoted 5 lines · Christoph Hellwig
Otherwise it won't catch bios turned into regular writes by the block level zone write plugging.
Fixes 9b1ce7f0c6f8 ("block: Implement zone append emulation")Signed-off-by Christoph Hellwig <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 3/4] btrfs: use bio_is_zone_append in the completion handler
In reply to Christoph HellwigLooks good,
Re: [PATCH 3/4] btrfs: use bio_is_zone_append in the completion handler
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
Otherwise it won't catch bios turned into regular writes by the block level zone write plugging.
Fixes 9b1ce7f0c6f8 ("block: Implement zone append emulation")Signed-off-by Christoph Hellwig <[email protected]>--- fs/btrfs/bio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
PATCH 4/4 btrfs: split bios to the fs sector size boundary
In reply to Christoph HellwigBtrfs like other file systems can't really deal with I/O not aligned to it's internal block size (which strangely is called sector size in btrfs), but the block layer split helper doesn't even know about that.
Round down the split boundary so that all I/Os are aligned.
--- fs/btrfs/bio.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
fs/btrfs/bio.c +9 -2
@@ -660,8 +660,15 @@ static u64 btrfs_append_map_length(struct btrfs_bio *bbio, u64 map_length) map_length =min (map_length, bbio->fs_info->max_zone_append_size); sector_offset =bio_split_rw_at (&bbio->bio, &bbio->fs_info->limits, &nr_segs, map_length); -if (sector_offset) -return sector_offset <<SECTOR_SHIFT ; +if (sector_offset) { +/* +* bio_split_rw_at could split at a size smaller than the +* file system sector size and thus cause unaligned I/Os. +* Fix that by always rounding down to the nearest boundary. +*/ +return ALIGN_DOWN (sector_offset <<SECTOR_SHIFT , + bbio->fs_info->sectorsize); + }return map_length; }
Signature · 1 line
2.45.2
Re: [PATCH 4/4] btrfs: split bios to the fs sector size boundary
Quoted 8 lines · Christoph Hellwig
Btrfs like other file systems can't really deal with I/O not aligned to it's internal block size (which strangely is called sector size in btrfs), but the block layer split helper doesn't even know about that.
Round down the split boundary so that all I/Os are aligned.
Fixes d5e4377d5051 ("btrfs: split zone append bios in btrfs_submit_bio")Signed-off-by Christoph Hellwig <[email protected]>
Looks OK to me.
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH 4/4] btrfs: split bios to the fs sector size boundary
In reply to Christoph HellwigLooks good,
Re: [PATCH 4/4] btrfs: split bios to the fs sector size boundary
In reply to Christoph HellwigQuoted 11 lines · Christoph Hellwig
Btrfs like other file systems can't really deal with I/O not aligned to it's internal block size (which strangely is called sector size in btrfs), but the block layer split helper doesn't even know about that.
Round down the split boundary so that all I/Os are aligned.
Fixes d5e4377d5051 ("btrfs: split zone append bios in btrfs_submit_bio")Signed-off-by Christoph Hellwig <[email protected]>--- fs/btrfs/bio.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
Looks good to me.