btrfs: Introduce btrfs_io_geometry
PATCH 1/2 btrfs: Introduce btrfs_io_geometry
--- fs/btrfs/volumes.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 2 + 2 files changed, 100 insertions(+)
fs/btrfs/volumes.c +98 -0
@@ -5907,6 +5907,104 @@ static bool need_full_stripe(enum btrfs_map_op op)return (op ==BTRFS_MAP_WRITE || op ==BTRFS_MAP_GET_READ_MIRRORS ); } +/* +* btrfs_io_geometry - calculates the geomery of a particular (address, len) +* tuple. This information is used to calculate how big a +* particular bio can get before it straddles a stripe. +* +* @fs_info - The omnipresent btrfs structure +* @logical - Address that we want to figure out the geometry of +* @len - The length of IO we are going to perform, starting at @logical +* @op - Type of operation - Write or Read +* @io_geom - Pointer used to return values +* +* Returns < 0 in case a chunk for the given logical address cannot be found, +* usually shouldn't happen unless @logical is corrupted, 0 otherwise. +*/ +int btrfs_io_geometry (struct btrfs_fs_info *fs_info,enum btrfs_map_op op, +u64 logical,u64 len,struct btrfs_io_geometry *io_geom) +{ +struct extent_map *em; +struct map_lookup *map; +u64 offset; +u64 stripe_offset; +u64 stripe_nr; +u64 stripe_len; +u64 raid56_full_stripe_start = (u64)-1 ; +int data_stripes; + +ASSERT (op !=BTRFS_MAP_DISCARD ); + + em =btrfs_get_chunk_map (fs_info, logical, len); +if (IS_ERR (em)) +return PTR_ERR (em); + + map = em->map_lookup; +/* Offset of this logical address in the chunk */ + offset = logical - em->start; +/* Len of a stripe in a chunk */ + stripe_len = map->stripe_len; +/* Stripe wher this block falls in */ + stripe_nr =div64_u64 (offset, stripe_len); +/* Offset of stripe in the chunk */ + stripe_offset = stripe_nr * stripe_len; +if (offset < stripe_offset) { +btrfs_crit (fs_info, +"stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu" , + stripe_offset, offset, em->start, logical, + stripe_len); +free_extent_map (em); +return -EINVAL ; + } + +/* stripe_offset is the offset of this block in its stripe*/ + stripe_offset = offset - stripe_offset; + data_stripes =nr_data_stripes (map); + + +if (map->type &BTRFS_BLOCK_GROUP_PROFILE_MASK ) { +u64 max_len = stripe_len - stripe_offset; + +/* +* In case of raid56, we need to know the stripe aligned start +*/ +if (map->type &BTRFS_BLOCK_GROUP_RAID56_MASK ) { +unsigned long full_stripe_len = stripe_len * data_stripes; + raid56_full_stripe_start = offset; + +/* +* Allow a write of a full stripe, but make sure we +* don't allow straddling of stripes +*/ + raid56_full_stripe_start =div64_u64 (raid56_full_stripe_start, + full_stripe_len); + raid56_full_stripe_start *= full_stripe_len; + +/* +* For writes to RAID[56], allow a full stripeset across +* all disks. For other RAID types and for RAID[56] +* reads, just allow a single stripe (on a single disk). +*/ +if (op ==BTRFS_MAP_WRITE ) { + max_len = stripe_len * data_stripes - + (offset - raid56_full_stripe_start); + } + } + len =min_t (u64, em->len - offset, max_len); + }else { + len = em->len - offset; + } + + io_geom->len = len; + io_geom->offset = offset; + io_geom->stripe_len = stripe_len; + io_geom->stripe_nr = stripe_nr; + io_geom->stripe_offset = stripe_offset; + io_geom->raid56_stripe_offset = raid56_full_stripe_start; + +return 0 ; +} + static int __btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op, u64 logical, u64 *length,
fs/btrfs/volumes.h +2 -0
@@ -421,6 +421,8 @@ int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,int btrfs_map_sblock (struct btrfs_fs_info *fs_info,enum btrfs_map_op op,u64 logical,u64 *length,struct btrfs_bio **bbio_ret); +int btrfs_io_geometry (struct btrfs_fs_info *fs_info,enum btrfs_map_op op, +u64 logical,u64 len,struct btrfs_io_geometry *io_geom);int btrfs_rmap_block (struct btrfs_fs_info *fs_info,u64 chunk_start,u64 physical,u64 **logical,int *naddrs,int *stripe_len);int btrfs_read_sys_array (struct btrfs_fs_info *fs_info);
Signature · 1 line
2.17.1
PATCH 2/2 btrfs: Use btrfs_io_geometry appropriately
Presently btrfs_map_block is used not only to do everything necessary to map a bio to the underlying allocation profile but it's also used to identify how much data could be written based on btrfs' stripe logic without actually submitting anything. This is achieved by passing NULL for 'bbio_ret' parameter.
This patch refactors all callers that require just the mapping length by switching them to using btrfs_io_geometry instead of calling btrfs_map_block with a special NULL value for 'bbio_ret'. No functional change.
--- fs/btrfs/inode.c | 25 +++++++-------- fs/btrfs/volumes.c | 77 +++++++++------------------------------------- 2 files changed, 27 insertions(+), 75 deletions(-)
fs/btrfs/inode.c +13 -12
@@ -1932,17 +1932,19 @@ int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,u64 length =0 ;u64 map_length;int ret; +struct btrfs_io_geometry geom;if (bio_flags &EXTENT_BIO_COMPRESSED )return 0 ; length = bio->bi_iter.bi_size; map_length = length; - ret =btrfs_map_block (fs_info,btrfs_op (bio), logical, &map_length, -NULL ,0 ); + ret =btrfs_io_geometry (fs_info,btrfs_op (bio), logical, map_length, + &geom);if (ret <0 )return ret; -if (map_length < length + size) + +if (geom.len < length + size)return 1 ;return 0 ; }
@@ -8331,15 +8333,15 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)int clone_len;int ret;blk_status_t status; +struct btrfs_io_geometry geom; - map_length = orig_bio->bi_iter.bi_size; - submit_len = map_length; - ret =btrfs_map_block (fs_info,btrfs_op (orig_bio), start_sector <<9 , - &map_length,NULL ,0 ); + submit_len = orig_bio->bi_iter.bi_size; + ret =btrfs_io_geometry (fs_info,btrfs_op (orig_bio), start_sector <<9 , + submit_len, &geom);if (ret)return -EIO ; - map_length =min_t (u64, map_length,SZ_1M ); + map_length =min_t (u64, geom.len,SZ_1M );if (map_length >= submit_len) { bio = orig_bio; dip->flags |=BTRFS_DIO_ORIG_BIO_SUBMITTED ;
@@ -8393,13 +8395,12 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip) start_sector += clone_len >>9 ; file_offset += clone_len; - map_length = submit_len; - ret =btrfs_map_block (fs_info,btrfs_op (orig_bio), - start_sector <<9 , &map_length,NULL ,0 ); + ret =btrfs_io_geometry (fs_info,btrfs_op (orig_bio), + start_sector <<9 , submit_len, &geom);if (ret) goto out_err; - map_length =min_t (u64, map_length,SZ_1M ); + map_length =min_t (u64, geom.len,SZ_1M ); }while (submit_len >0 ); submit:
fs/btrfs/volumes.c +14 -63
99 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 19 lines · Nikolay Borisov
--- fs/btrfs/volumes.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 2 + 2 files changed, 100 insertions(+)fs/btrfs/volumes.c +6
-0@@ -5907,6 +5907,104 @@ static bool need_full_stripe(enum btrfs_map_op op)return (op ==BTRFS_MAP_WRITE || op ==BTRFS_MAP_GET_READ_MIRRORS ); } +/* +* btrfs_io_geometry - calculates the geomery of a particular (address, len) +* tuple. This information is used to calculate how big a +* particular bio can get before it straddles a stripe. +* +* @fs_info - The omnipresent btrfs structure
fs/btrfs/volumes.c static bool need_full_stripe(enum btrfs_map_op op) · line 5925
Quoted 10 lines
fs/btrfs/volumes.c +10
-0+* @logical - Address that we want to figure out the geometry of +* @len - The length of IO we are going to perform, starting at @logical +* @op - Type of operation - Write or Read +* @io_geom - Pointer used to return values +* +* Returns < 0 in case a chunk for the given logical address cannot be found, +* usually shouldn't happen unless @logical is corrupted, 0 otherwise. +*/ +int btrfs_io_geometry (struct btrfs_fs_info *fs_info,enum btrfs_map_op op, +u64 logical,u64 len,struct btrfs_io_geometry *io_geom)
Where is struct btrfs_io_geometry defined?
And changelog and signed-off-by are missing.
Quoted 33 lines · David Sterba
Quoted 19 lines · Nikolay Borisov
--- fs/btrfs/volumes.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/volumes.h | 2 + 2 files changed, 100 insertions(+)fs/btrfs/volumes.c +6
-0@@ -5907,6 +5907,104 @@ static bool need_full_stripe(enum btrfs_map_op op)return (op ==BTRFS_MAP_WRITE || op ==BTRFS_MAP_GET_READ_MIRRORS ); } +/* +* btrfs_io_geometry - calculates the geomery of a particular (address, len) +* tuple. This information is used to calculate how big a +* particular bio can get before it straddles a stripe. +* +* @fs_info - The omnipresent btrfs structure fs/btrfs/volumes.c static bool need_full_stripe(enum btrfs_map_op op) · line 5925
Quoted 10 lines
fs/btrfs/volumes.c +10
-0+* @logical - Address that we want to figure out the geometry of +* @len - The length of IO we are going to perform, starting at @logical +* @op - Type of operation - Write or Read +* @io_geom - Pointer used to return values +* +* Returns < 0 in case a chunk for the given logical address cannot be found, +* usually shouldn't happen unless @logical is corrupted, 0 otherwise. +*/ +int btrfs_io_geometry (struct btrfs_fs_info *fs_info,enum btrfs_map_op op, +u64 logical,u64 len,struct btrfs_io_geometry *io_geom)Where is struct
btrfs_io_geometrydefined?
Quoted 1 line
And changelog and signed-off-by are missing.
Yep, royally fucked up the submission :D