Skip to contentSkip to conversation

btrfs: btrfs_bio and btrfs_io_bio rename

PATCH In review Qu Wenruo 4 participants 22 messages Sep 15, 2021 – Sep 23, 2021 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v3 cover btrfs: btrfs_bio and btrfs_io_bio rename

The branch can be fetched from github, and is the preferred way to grab the code, as this patchset changed quite a lot of code.
https://github.com/adam900710/linux/tree/chunk_refactor

There are two structure, btrfs_io_bio and btrfs_bio, which have very similar names but completely different meanings.

Btrfs_io_bio mostly works at logical bytenr layer (its bio->bi_iter.bi_sector points to btrfs logical bytenr), and just contains extra info like csum and mirror_num.

And btrfs_io_bio is in fact the most utilized bio, as all data/metadata IO is using btrfs_io_bio.

While btrfs_bio is completely a helper structure for mirrored IO submission (utilized by SINGLE/DUP/RAID1/RAID10), and contains RAID56 maps for RAID56 (it doesn't utilize this structure for IO submission tracking).

Such naming is completely anti-human.

So this patchset will do the following renaming:

- btrfs_bio -> btrfs_io_context
Since it's not really used by all bios (only mirrored profiles utilize it), and it contains extra info for RAID56, it's not proper to name it with _bio suffix.

Later we can integrate btrfs_io_context pointer into the new btrfs_bio.

- btrfs_io_bio -> btrfs_logical_bio
It is intentional not to reuse "btrfs_bio", which could cause confusion for later backport.

Changelog:
v2:
- Rename btrfs_bio to btrfs_io_context (bioc)
- Rename btrfs_io_bio to btrfs_bio
Both suggested by Nikolay

v3:
- Fixes whiespace problems
Caused by "dwi" vim commands

- Update several modified comments

- Rename btrfs_io_bio to btrfs_logical_bio
To avoid backport confusion.

Qu Wenruo (3):
btrfs: rename btrfs_bio to btrfs_io_context
btrfs: remove btrfs_bio_alloc() helper
btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

 fs/btrfs/check-integrity.c |   4 +-
 fs/btrfs/compression.c     |  20 +--
 fs/btrfs/ctree.h           |   6 +-
 fs/btrfs/disk-io.c         |   2 +-
 fs/btrfs/disk-io.h         |   2 +-
 fs/btrfs/extent-tree.c     |  19 ++-
 fs/btrfs/extent_io.c       | 116 ++++++++--------
 fs/btrfs/extent_io.h       |   8 +-
 fs/btrfs/extent_map.c      |   4 +-
 fs/btrfs/file-item.c       |  12 +-
 fs/btrfs/inode.c           |  50 +++----
 fs/btrfs/raid56.c          | 135 +++++++++---------
 fs/btrfs/raid56.h          |   8 +-
 fs/btrfs/reada.c           |  26 ++--
 fs/btrfs/scrub.c           | 130 +++++++++---------
 fs/btrfs/volumes.c         | 272 ++++++++++++++++++-------------------
 fs/btrfs/volumes.h         |  63 +++++----
 fs/btrfs/zoned.c           |  16 +--
 18 files changed, 455 insertions(+), 438 deletions(-)
Signature · 1 line
2.33.0
author
#1 lore raw

PATCH v3 1/3 btrfs: rename btrfs_bio to btrfs_io_context

The structure btrfs_bio is used by two different sites:

- bio->bi_private for mirror based profiles
For those profiles (SINGLE/DUP/RAID1*/RAID10), this structures records how many mirrors are still pending, and save the original endio function of the bio.

- RAID56 code
In that case, RAID56 only utilize the stripes info, and no long uses that to trace the pending mirrors.

So btrfs_bio is not always bind to a bio, and contains more info for IO context, thus renaming it will make the naming less confusing.

---
 fs/btrfs/check-integrity.c |   2 +-
 fs/btrfs/extent-tree.c     |  19 ++-
 fs/btrfs/extent_io.c       |  18 +--
 fs/btrfs/extent_map.c      |   4 +-
 fs/btrfs/raid56.c          | 127 +++++++++---------
 fs/btrfs/raid56.h          |   8 +-
 fs/btrfs/reada.c           |  26 ++--
 fs/btrfs/scrub.c           | 116 ++++++++--------
 fs/btrfs/volumes.c         | 266 ++++++++++++++++++-------------------
 fs/btrfs/volumes.h         |  38 ++++--
 fs/btrfs/zoned.c           |  16 +--
 11 files changed, 326 insertions(+), 314 deletions(-)
fs/btrfs/check-integrity.c +1 -1
index 86816088927f..81b11124b67a 100644
@@ -1455,7 +1455,7 @@ static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
 	struct btrfs_fs_info *fs_info = state->fs_info;
 	int ret;
 	u64 length;
-	struct btrfs_bio *multi = NULL;
+	struct btrfs_io_context *multi = NULL;
 	struct btrfs_device *device;
 
 	length = len;
fs/btrfs/extent-tree.c +9 -10
index 7d03ffa04bce..704ee786acb5 100644
@@ -1266,7 +1266,7 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
 	return ret;
 }
 
-static int do_discard_extent(struct btrfs_bio_stripe *stripe, u64 *bytes)
+static int do_discard_extent(struct btrfs_io_stripe *stripe, u64 *bytes)
 {
 	struct btrfs_device *dev = stripe->dev;
 	struct btrfs_fs_info *fs_info = dev->fs_info;
@@ -1313,22 +1313,21 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 	u64 discarded_bytes = 0;
 	u64 end = bytenr + num_bytes;
 	u64 cur = bytenr;
-	struct btrfs_bio *bbio = NULL;
-
+	struct btrfs_io_context *bioc = NULL;
 
 	/*
-	 * Avoid races with device replace and make sure our bbio has devices
+	 * Avoid races with device replace and make sure our bioc has devices
 	 * associated to its stripes that don't go away while we are discarding.
 	 */
 	btrfs_bio_counter_inc_blocked(fs_info);
 	while (cur < end) {
-		struct btrfs_bio_stripe *stripe;
+		struct btrfs_io_stripe *stripe;
 		int i;
 
 		num_bytes = end - cur;
 		/* Tell the block device(s) that the sectors can be discarded */
 		ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
-				      &num_bytes, &bbio, 0);
+				      &num_bytes, &bioc, 0);
 		/*
 		 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
 		 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
@@ -1337,8 +1336,8 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 		if (ret < 0)
 			goto out;
 
-		stripe = bbio->stripes;
-		for (i = 0; i < bbio->num_stripes; i++, stripe++) {
+		stripe = bioc->stripes;
+		for (i = 0; i < bioc->num_stripes; i++, stripe++) {
 			u64 bytes;
 			struct btrfs_device *device = stripe->dev;
 
@@ -1361,7 +1360,7 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 				 * And since there are two loops, explicitly
 				 * go to out to avoid confusion.
 				 */
-				btrfs_put_bbio(bbio);
+				btrfs_put_bioc(bioc);
 				goto out;
 			}
 
@@ -1372,7 +1371,7 @@ int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
 			 */
 			ret = 0;
 		}
-		btrfs_put_bbio(bbio);
+		btrfs_put_bioc(bioc);
 		cur += num_bytes;
 	}
 out:
fs/btrfs/extent_io.c +9 -9
index 8959ac580f46..1aed03ef5f49 100644
@@ -2290,7 +2290,7 @@ static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
 	struct btrfs_device *dev;
 	u64 map_length = 0;
 	u64 sector;
-	struct btrfs_bio *bbio = NULL;
+	struct btrfs_io_context *bioc = NULL;
 	int ret;
 
 	ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
@@ -2304,7 +2304,7 @@ static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
 	map_length = length;
 
 	/*
-	 * Avoid races with device replace and make sure our bbio has devices
+	 * Avoid races with device replace and make sure our bioc has devices
 	 * associated to its stripes that don't go away while we are doing the
 	 * read repair operation.
 	 */
@@ -2317,28 +2317,28 @@ static int repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
 		 * stripe's dev and sector.
 		 */
 		ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
-				      &map_length, &bbio, 0);
+				      &map_length, &bioc, 0);
 		if (ret) {
 			btrfs_bio_counter_dec(fs_info);
 			bio_put(bio);
 			return -EIO;
 		}
-		ASSERT(bbio->mirror_num == 1);
+		ASSERT(bioc->mirror_num == 1);
 	} else {
 		ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
-				      &map_length, &bbio, mirror_num);
+				      &map_length, &bioc, mirror_num);
 		if (ret) {
 			btrfs_bio_counter_dec(fs_info);
 			bio_put(bio);
 			return -EIO;
 		}
-		BUG_ON(mirror_num != bbio->mirror_num);
+		BUG_ON(mirror_num != bioc->mirror_num);
 	}
 
-	sector = bbio->stripes[bbio->mirror_num - 1].physical >> 9;
+	sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9;
 	bio->bi_iter.bi_sector = sector;
-	dev = bbio->stripes[bbio->mirror_num - 1].dev;
-	btrfs_put_bbio(bbio);
+	dev = bioc->stripes[bioc->mirror_num - 1].dev;
+	btrfs_put_bioc(bioc);
 	if (!dev || !dev->bdev ||
 	    !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
 		btrfs_bio_counter_dec(fs_info);
fs/btrfs/extent_map.c +2 -2
index 4a8e02f7b6c7..5a36add21305 100644
@@ -360,7 +360,7 @@ static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
 	int i;
 
 	for (i = 0; i < map->num_stripes; i++) {
-		struct btrfs_bio_stripe *stripe = &map->stripes[i];
+		struct btrfs_io_stripe *stripe = &map->stripes[i];
 		struct btrfs_device *device = stripe->dev;
 
 		set_extent_bits_nowait(&device->alloc_state, stripe->physical,
@@ -375,7 +375,7 @@ static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
 	int i;
 
 	for (i = 0; i < map->num_stripes; i++) {
-		struct btrfs_bio_stripe *stripe = &map->stripes[i];
+		struct btrfs_io_stripe *stripe = &map->stripes[i];
 		struct btrfs_device *device = stripe->dev;
 
 		__clear_extent_bit(&device->alloc_state, stripe->physical,
fs/btrfs/raid56.c +63 -64

420 lines not on this page · raw message

fs/btrfs/raid56.h +4 -4
index 2503485db859..838d3a5e07ef 100644
@@ -31,24 +31,24 @@ struct btrfs_raid_bio;
 struct btrfs_device;
 
 int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
-			  struct btrfs_bio *bbio, u64 stripe_len,
+			  struct btrfs_io_context *bioc, u64 stripe_len,
 			  int mirror_num, int generic_io);
 int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
-			       struct btrfs_bio *bbio, u64 stripe_len);
+			struct btrfs_io_context *bioc, u64 stripe_len);
 
 void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
 			    u64 logical);
 
 struct btrfs_raid_bio *
 raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
-			       struct btrfs_bio *bbio, u64 stripe_len,
+			       struct btrfs_io_context *bioc, u64 stripe_len,
 			       struct btrfs_device *scrub_dev,
 			       unsigned long *dbitmap, int stripe_nsectors);
 void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
 
 struct btrfs_raid_bio *
 raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
-			  struct btrfs_bio *bbio, u64 length);
+			  struct btrfs_io_context *bioc, u64 length);
 void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio);
 
 int btrfs_alloc_stripe_hash_table(struct btrfs_fs_info *info);
fs/btrfs/reada.c +13 -13
index 06713a8fe26b..eb96c45f205c 100644
@@ -227,7 +227,7 @@ int btree_readahead_hook(struct extent_buffer *eb, int err)
 }
 
 static struct reada_zone *reada_find_zone(struct btrfs_device *dev, u64 logical,
-					  struct btrfs_bio *bbio)
+					  struct btrfs_io_context *bioc)
 {
 	struct btrfs_fs_info *fs_info = dev->fs_info;
 	int ret;
@@ -275,11 +275,11 @@ static struct reada_zone *reada_find_zone(struct btrfs_device *dev, u64 logical,
 	kref_init(&zone->refcnt);
 	zone->elems = 0;
 	zone->device = dev; /* our device always sits at index 0 */
-	for (i = 0; i < bbio->num_stripes; ++i) {
+	for (i = 0; i < bioc->num_stripes; ++i) {
 		/* bounds have already been checked */
-		zone->devs[i] = bbio->stripes[i].dev;
+		zone->devs[i] = bioc->stripes[i].dev;
 	}
-	zone->ndevs = bbio->num_stripes;
+	zone->ndevs = bioc->num_stripes;
 
 	spin_lock(&fs_info->reada_lock);
 	ret = radix_tree_insert(&dev->reada_zones,
@@ -309,7 +309,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
 	int ret;
 	struct reada_extent *re = NULL;
 	struct reada_extent *re_exist = NULL;
-	struct btrfs_bio *bbio = NULL;
+	struct btrfs_io_context *bioc = NULL;
 	struct btrfs_device *dev;
 	struct btrfs_device *prev_dev;
 	u64 length;
@@ -345,28 +345,28 @@ static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
 	 */
 	length = fs_info->nodesize;
 	ret = btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
-			&length, &bbio, 0);
-	if (ret || !bbio || length < fs_info->nodesize)
+			&length, &bioc, 0);
+	if (ret || !bioc || length < fs_info->nodesize)
 		goto error;
 
-	if (bbio->num_stripes > BTRFS_MAX_MIRRORS) {
+	if (bioc->num_stripes > BTRFS_MAX_MIRRORS) {
 		btrfs_err(fs_info,
 			   "readahead: more than %d copies not supported",
 			   BTRFS_MAX_MIRRORS);
 		goto error;
 	}
 
-	real_stripes = bbio->num_stripes - bbio->num_tgtdevs;
+	real_stripes = bioc->num_stripes - bioc->num_tgtdevs;
 	for (nzones = 0; nzones < real_stripes; ++nzones) {
 		struct reada_zone *zone;
 
-		dev = bbio->stripes[nzones].dev;
+		dev = bioc->stripes[nzones].dev;
 
 		/* cannot read ahead on missing device. */
 		if (!dev->bdev)
 			continue;
 
-		zone = reada_find_zone(dev, logical, bbio);
+		zone = reada_find_zone(dev, logical, bioc);
 		if (!zone)
 			continue;
 
@@ -464,7 +464,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
 	if (!have_zone)
 		goto error;
 
-	btrfs_put_bbio(bbio);
+	btrfs_put_bioc(bioc);
 	return re;
 
 error:
@@ -488,7 +488,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
 		kref_put(&zone->refcnt, reada_zone_release);
 		spin_unlock(&fs_info->reada_lock);
 	}
-	btrfs_put_bbio(bbio);
+	btrfs_put_bioc(bioc);
 	kfree(re);
 	return re_exist;
 }
fs/btrfs/scrub.c +58 -58

289 lines not on this page · raw message

fs/btrfs/volumes.c +132 -134

620 lines not on this page · raw message

fs/btrfs/volumes.h +27 -11
index 7e8f205978d9..b69755fc0e0d 100644
@@ -306,11 +306,11 @@ struct btrfs_fs_devices {
 /*
  * we need the mirror number and stripe index to be passed around
  * the call chain while we are processing end_io (especially errors).
- * Really, what we need is a btrfs_bio structure that has this info
+ * Really, what we need is a btrfs_io_context structure that has this info
  * and is properly sized with its stripe array, but we're not there
  * quite yet.  We have our own btrfs bioset, and all of the bios
  * we allocate are actually btrfs_io_bios.  We'll cram as much of
- * struct btrfs_bio as we can into this over time.
+ * struct btrfs_io_context as we can into this over time.
  */
 struct btrfs_io_bio {
 	unsigned int mirror_num;
@@ -339,13 +339,29 @@ static inline void btrfs_io_bio_free_csum(struct btrfs_io_bio *io_bio)
 	}
 }
 
-struct btrfs_bio_stripe {
+struct btrfs_io_stripe {
 	struct btrfs_device *dev;
 	u64 physical;
 	u64 length; /* only used for discard mappings */
 };
 
-struct btrfs_bio {
+/*
+ * Context for IO subsmission for device stripe.
+ *
+ * - Track the unfinished mirrors for mirror based profiles
+ *   Mirror based profiles are SINGLE/DUP/RAID1/RAID10.
+ *
+ * - Contain the logical -> physical mapping info
+ *   Used by submit_stripe_bio() for mapping logical bio
+ *   into physical device address.
+ *
+ * - Contain device replace info
+ *   Used by handle_ops_on_dev_replace() to copy logical bios
+ *   into the new device.
+ *
+ * - Contain RAID56 full stripe logical bytenrs
+ */
+struct btrfs_io_context {
 	refcount_t refs;
 	atomic_t stripes_pending;
 	struct btrfs_fs_info *fs_info;
@@ -365,7 +381,7 @@ struct btrfs_bio {
 	 * so raid_map[0] is the start of our full stripe
 	 */
 	u64 *raid_map;
-	struct btrfs_bio_stripe stripes[];
+	struct btrfs_io_stripe stripes[];
 };
 
 struct btrfs_device_info {
@@ -400,11 +416,11 @@ struct map_lookup {
 	int num_stripes;
 	int sub_stripes;
 	int verified_stripes; /* For mount time dev extent verification */
-	struct btrfs_bio_stripe stripes[];
+	struct btrfs_io_stripe stripes[];
 };
 
 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
-			    (sizeof(struct btrfs_bio_stripe) * (n)))
+			    (sizeof(struct btrfs_io_stripe) * (n)))
 
 struct btrfs_balance_args;
 struct btrfs_balance_progress;
@@ -441,14 +457,14 @@ static inline enum btrfs_map_op btrfs_op(struct bio *bio)
 	}
 }
 
-void btrfs_get_bbio(struct btrfs_bio *bbio);
-void btrfs_put_bbio(struct btrfs_bio *bbio);
+void btrfs_get_bioc(struct btrfs_io_context *bioc);
+void btrfs_put_bioc(struct btrfs_io_context *bioc);
 int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
 		    u64 logical, u64 *length,
-		    struct btrfs_bio **bbio_ret, int mirror_num);
+		    struct btrfs_io_context **bioc_ret, int mirror_num);
 int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
 		     u64 logical, u64 *length,
-		     struct btrfs_bio **bbio_ret);
+		     struct btrfs_io_context **bioc_ret);
 int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *map,
 			  enum btrfs_map_op op, u64 logical,
 			  struct btrfs_io_geometry *io_geom);
fs/btrfs/zoned.c +8 -8
index 28a06c2d80ad..8f9ccfa4157a 100644
@@ -1626,27 +1626,27 @@ int btrfs_zoned_issue_zeroout(struct btrfs_device *device, u64 physical, u64 len
 static int read_zone_info(struct btrfs_fs_info *fs_info, u64 logical,
 			  struct blk_zone *zone)
 {
-	struct btrfs_bio *bbio = NULL;
+	struct btrfs_io_context *bioc = NULL;
 	u64 mapped_length = PAGE_SIZE;
 	unsigned int nofs_flag;
 	int nmirrors;
 	int i, ret;
 
 	ret = btrfs_map_sblock(fs_info, BTRFS_MAP_GET_READ_MIRRORS, logical,
-			       &mapped_length, &bbio);
-	if (ret || !bbio || mapped_length < PAGE_SIZE) {
-		btrfs_put_bbio(bbio);
+			       &mapped_length, &bioc);
+	if (ret || !bioc || mapped_length < PAGE_SIZE) {
+		btrfs_put_bioc(bioc);
 		return -EIO;
 	}
 
-	if (bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK)
+	if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK)
 		return -EINVAL;
 
 	nofs_flag = memalloc_nofs_save();
-	nmirrors = (int)bbio->num_stripes;
+	nmirrors = (int)bioc->num_stripes;
 	for (i = 0; i < nmirrors; i++) {
-		u64 physical = bbio->stripes[i].physical;
-		struct btrfs_device *dev = bbio->stripes[i].dev;
+		u64 physical = bioc->stripes[i].physical;
+		struct btrfs_device *dev = bioc->stripes[i].dev;
 
 		/* Missing device */
 		if (!dev->bdev)
Signature · 1 line
2.33.0
#2 lore raw

Re: [PATCH v3 1/3] btrfs: rename btrfs_bio to btrfs_io_context

Quoted 4 lines · Qu Wenruo

The structure btrfs_bio is used by two different sites:

- bio->bi_private for mirror based profiles
For those profiles (SINGLE/DUP/RAID1*/RAID10), this structures records

Why is SINGLE here?

#3 lore raw

Re: [PATCH v3 1/3] btrfs: rename btrfs_bio to btrfs_io_context

Quoted 8 lines · David Sterba
Quoted 4 lines · Qu Wenruo

The structure btrfs_bio is used by two different sites:

- bio->bi_private for mirror based profiles
For those profiles (SINGLE/DUP/RAID1*/RAID10), this structures records

Why is SINGLE here?

For single we use the same routine as RAID1/DUP/etc, it's submit_stripe_bio() doing the remapping.

Thus there is really only two types, non-RAID56 and RAID56.

Thanks,
Qu

author
#4 lore raw

Re: [PATCH v3 1/3] btrfs: rename btrfs_bio to btrfs_io_context

Quoted 9 lines · Qu Wenruo
Quoted 6 lines · David Sterba
Quoted 4 lines · Qu Wenruo

The structure btrfs_bio is used by two different sites:

- bio->bi_private for mirror based profiles
   For those profiles (SINGLE/DUP/RAID1*/RAID10), this structures records

Quoted 6 lines
Quoted 2 lines

Why is SINGLE here?

For single we use the same routine as RAID1/DUP/etc, it's submit_stripe_bio() doing the remapping.

Thus there is really only two types, non-RAID56 and RAID56.

And there is no really "SINGLE" profile in btrfs.

As even for SINGLE profile, we may need to submit two bios to two different devices (one is the current device, the other is the dev-replace target).

Thanks,
Qu

Quoted 4 lines

Thanks,
Qu

#5 lore raw

Re: [PATCH v3 1/3] btrfs: rename btrfs_bio to btrfs_io_context

Quoted 18 lines · Qu Wenruo
Quoted 15 lines · Qu Wenruo
Quoted 9 lines · David Sterba
Quoted 5 lines · Qu Wenruo

The structure btrfs_bio is used by two different sites:

- bio->bi_private for mirror based profiles
   For those profiles (SINGLE/DUP/RAID1*/RAID10), this structures records

Why is SINGLE here?

For single we use the same routine as RAID1/DUP/etc, it's submit_stripe_bio() doing the remapping.

Quoted 7 lines
Quoted 1 line

Thus there is really only two types, non-RAID56 and RAID56.

And there is no really "SINGLE" profile in btrfs.

As even for SINGLE profile, we may need to submit two bios to two different devices (one is the current device, the other is the dev-replace target).

Ok, thanks. It's a bit confusing to mention 'single' with mirrored profiles but here it's on the implementation level where it could be written on more than one device. We don't have a terminology for that so be it.

author
#6 lore raw

PATCH v3 2/3 btrfs: remove btrfs_bio_alloc() helper

In reply to Qu Wenruo

The helper btrfs_bio_alloc() is almost the same as btrfs_io_bio_alloc(), except it's allocating using BIO_MAX_VECS as @nr_iovecs, and initialize bio->bi_iter.bi_sector.

However the naming itself is not using "btrfs_io_bio" to indicate its parameter is "strcut btrfs_io_bio" and can be easily confused with "struct btrfs_bio".

Considering assigned bio->bi_iter.bi_sector is such a simple work and there are already tons of call sites doing that manually, there is no need to do that in a helper.

Remove btrfs_bio_alloc() helper, and enhance btrfs_io_bio_alloc() function to provide a fail-safe value for its @nr_iovecs.

And then replace all btrfs_bio_alloc() callers with btrfs_io_bio_alloc().

---
 fs/btrfs/compression.c | 12 ++++++++----
 fs/btrfs/extent_io.c   | 33 +++++++++++++++------------------
 fs/btrfs/extent_io.h   |  1 -
 3 files changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +8 -4
index 7869ad12bc6e..2475dc0b1c22 100644
@@ -418,7 +418,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 	cb->orig_bio = NULL;
 	cb->nr_pages = nr_pages;
 
-	bio = btrfs_bio_alloc(first_byte);
+	bio = btrfs_io_bio_alloc(0);
+	bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 	bio->bi_opf = bio_op | write_flags;
 	bio->bi_private = cb;
 	bio->bi_end_io = end_compressed_bio_write;
@@ -490,7 +491,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 				bio_endio(bio);
 			}
 
-			bio = btrfs_bio_alloc(first_byte);
+			bio = btrfs_io_bio_alloc(0);
+			bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 			bio->bi_opf = bio_op | write_flags;
 			bio->bi_private = cb;
 			bio->bi_end_io = end_compressed_bio_write;
@@ -748,7 +750,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	/* include any pages we added in add_ra-bio_pages */
 	cb->len = bio->bi_iter.bi_size;
 
-	comp_bio = btrfs_bio_alloc(cur_disk_byte);
+	comp_bio = btrfs_io_bio_alloc(0);
+	comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 	comp_bio->bi_opf = REQ_OP_READ;
 	comp_bio->bi_private = cb;
 	comp_bio->bi_end_io = end_compressed_bio_read;
@@ -806,7 +809,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 				bio_endio(comp_bio);
 			}
 
-			comp_bio = btrfs_bio_alloc(cur_disk_byte);
+			comp_bio = btrfs_io_bio_alloc(0);
+			comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 			comp_bio->bi_opf = REQ_OP_READ;
 			comp_bio->bi_private = cb;
 			comp_bio->bi_end_io = end_compressed_bio_read;
fs/btrfs/extent_io.c +15 -18
index 1aed03ef5f49..d3fcf7e8dc48 100644
@@ -3121,16 +3121,22 @@ static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
 }
 
 /*
- * The following helpers allocate a bio. As it's backed by a bioset, it'll
- * never fail.  We're returning a bio right now but you can call btrfs_io_bio
- * for the appropriate container_of magic
+ * Allocate a btrfs_io_bio, with @nr_iovecs as maximum iovecs.
+ *
+ * If @nr_iovecs is 0, it will use BIO_MAX_VECS as @nr_iovces instead.
+ * This behavior is to provide a fail-safe default value.
+ *
+ * This helper uses bioset to allocate the bio, thus it's backed by mempool,
+ * and should not fail from process contexts.
  */
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
 {
 	struct bio *bio;
 
-	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset);
-	bio->bi_iter.bi_sector = first_byte >> 9;
+	ASSERT(nr_iovecs <= BIO_MAX_VECS);
+	if (nr_iovecs == 0)
+		nr_iovecs = BIO_MAX_VECS;
+	bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
 	btrfs_io_bio_init(btrfs_io_bio(bio));
 	return bio;
 }
@@ -3148,16 +3154,6 @@ struct bio *btrfs_bio_clone(struct bio *bio)
 	return new;
 }
 
-struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
-{
-	struct bio *bio;
-
-	/* Bio allocation backed by a bioset does not fail */
-	bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
-	btrfs_io_bio_init(btrfs_io_bio(bio));
-	return bio;
-}
-
 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size)
 {
 	struct bio *bio;
@@ -3307,14 +3303,15 @@ static int alloc_new_bio(struct btrfs_inode *inode,
 	struct bio *bio;
 	int ret;
 
+	bio = btrfs_io_bio_alloc(0);
 	/*
 	 * For compressed page range, its disk_bytenr is always @disk_bytenr
 	 * passed in, no matter if we have added any range into previous bio.
 	 */
 	if (bio_flags & EXTENT_BIO_COMPRESSED)
-		bio = btrfs_bio_alloc(disk_bytenr);
+		bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
 	else
-		bio = btrfs_bio_alloc(disk_bytenr + offset);
+		bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT;
 	bio_ctrl->bio = bio;
 	bio_ctrl->bio_flags = bio_flags;
 	bio->bi_end_io = end_io_func;
fs/btrfs/extent_io.h +0 -1
index ba471f2063a7..81fa68eaa699 100644
@@ -278,7 +278,6 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
 				  struct page *locked_page,
 				  u32 bits_to_clear, unsigned long page_ops);
-struct bio *btrfs_bio_alloc(u64 first_byte);
 struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs);
 struct bio *btrfs_bio_clone(struct bio *bio);
 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size);
Signature · 1 line
2.33.0
#7 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init

Quoted 74 lines · Qu Wenruo

The helper btrfs_bio_alloc() is almost the same as btrfs_io_bio_alloc(), except it's allocating using BIO_MAX_VECS as @nr_iovecs, and initialize bio->bi_iter.bi_sector.

However the naming itself is not using "btrfs_io_bio" to indicate its parameter is "strcut btrfs_io_bio" and can be easily confused with "struct btrfs_bio".

Considering assigned bio->bi_iter.bi_sector is such a simple work and there are already tons of call sites doing that manually, there is no need to do that in a helper.

Remove btrfs_bio_alloc() helper, and enhance btrfs_io_bio_alloc() function to provide a fail-safe value for its @nr_iovecs.

And then replace all btrfs_bio_alloc() callers with btrfs_io_bio_alloc().

---
 fs/btrfs/compression.c | 12 ++++++++----
 fs/btrfs/extent_io.c   | 33 +++++++++++++++------------------
 fs/btrfs/extent_io.h   |  1 -
 3 files changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +8 -4
index 7869ad12bc6e..2475dc0b1c22 100644
@@ -418,7 +418,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 	cb->orig_bio = NULL;
 	cb->nr_pages = nr_pages;
 
-	bio = btrfs_bio_alloc(first_byte);
+	bio = btrfs_io_bio_alloc(0);
+	bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 	bio->bi_opf = bio_op | write_flags;
 	bio->bi_private = cb;
 	bio->bi_end_io = end_compressed_bio_write;
@@ -490,7 +491,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 				bio_endio(bio);
 			}
 
-			bio = btrfs_bio_alloc(first_byte);
+			bio = btrfs_io_bio_alloc(0);
+			bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 			bio->bi_opf = bio_op | write_flags;
 			bio->bi_private = cb;
 			bio->bi_end_io = end_compressed_bio_write;
@@ -748,7 +750,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	/* include any pages we added in add_ra-bio_pages */
 	cb->len = bio->bi_iter.bi_size;
 
-	comp_bio = btrfs_bio_alloc(cur_disk_byte);
+	comp_bio = btrfs_io_bio_alloc(0);
+	comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 	comp_bio->bi_opf = REQ_OP_READ;
 	comp_bio->bi_private = cb;
 	comp_bio->bi_end_io = end_compressed_bio_read;
@@ -806,7 +809,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 				bio_endio(comp_bio);
 			}
 
-			comp_bio = btrfs_bio_alloc(cur_disk_byte);
+			comp_bio = btrfs_io_bio_alloc(0);
+			comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 			comp_bio->bi_opf = REQ_OP_READ;
 			comp_bio->bi_private = cb;
 			comp_bio->bi_end_io = end_compressed_bio_read;
fs/btrfs/extent_io.c +0 -0
index 1aed03ef5f49..d3fcf7e8dc48 100644
@@ -3121,16 +3121,22 @@ static inline void btrfs_io_bio_init

After reading through the whole patch I agree with the naming, though yeah it's a bit long, but we've been using this wordy naming. For identifiers it's fine to use lbio and it's now clear from the context that it's about the btrfs-specific features.(struct btrfs_io_bio *btrfs_bio)

Quoted 14 lines
fs/btrfs/extent_io.c +7 -3
 }
 
 /*
- * The following helpers allocate a bio. As it's backed by a bioset, it'll
- * never fail.  We're returning a bio right now but you can call btrfs_io_bio
- * for the appropriate container_of magic
+ * Allocate a btrfs_io_bio, with @nr_iovecs as maximum iovecs.
+ *
+ * If @nr_iovecs is 0, it will use BIO_MAX_VECS as @nr_iovces instead.
+ * This behavior is to provide a fail-safe default value.
+ *
+ * This helper uses bioset to allocate the bio, thus it's backed by mempool,
+ * and should not fail from process contexts.
  */

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) · line 3138

Quoted 10 lines
fs/btrfs/extent_io.c +4 -3
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
 {
 	struct bio *bio;
 
-	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset);
-	bio->bi_iter.bi_sector = first_byte >> 9;
+	ASSERT(nr_iovecs <= BIO_MAX_VECS);
+	if (nr_iovecs == 0)
+		nr_iovecs = BIO_MAX_VECS;

hell no! How come passing 0 actually means BIO_MAX_VEC. Instead of having 0 everywhere and have the function translate this to BIO_MAX_VECS, simply pass BIO_MAX_VECS in every call site where it's needed.

David, please either fix the patch in the tree or retract it. Let's try and refrain from adding such "gems" to the code base.

<snip>

#8 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 100 lines · Nikolay Borisov

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init

Quoted 74 lines · Qu Wenruo

The helper btrfs_bio_alloc() is almost the same as btrfs_io_bio_alloc(), except it's allocating using BIO_MAX_VECS as @nr_iovecs, and initialize bio->bi_iter.bi_sector.

However the naming itself is not using "btrfs_io_bio" to indicate its parameter is "strcut btrfs_io_bio" and can be easily confused with "struct btrfs_bio".

Considering assigned bio->bi_iter.bi_sector is such a simple work and there are already tons of call sites doing that manually, there is no need to do that in a helper.

Remove btrfs_bio_alloc() helper, and enhance btrfs_io_bio_alloc() function to provide a fail-safe value for its @nr_iovecs.

And then replace all btrfs_bio_alloc() callers with btrfs_io_bio_alloc().

---
  fs/btrfs/compression.c | 12 ++++++++----
  fs/btrfs/extent_io.c   | 33 +++++++++++++++------------------
  fs/btrfs/extent_io.h   |  1 -
  3 files changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +8 -4
index 7869ad12bc6e..2475dc0b1c22 100644
@@ -418,7 +418,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
  	cb->orig_bio = NULL;
  	cb->nr_pages = nr_pages;
 
-	bio = btrfs_bio_alloc(first_byte);
+	bio = btrfs_io_bio_alloc(0);
+	bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
  	bio->bi_opf = bio_op | write_flags;
  	bio->bi_private = cb;
  	bio->bi_end_io = end_compressed_bio_write;
@@ -490,7 +491,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
  				bio_endio(bio);
  			}
 
-			bio = btrfs_bio_alloc(first_byte);
+			bio = btrfs_io_bio_alloc(0);
+			bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
  			bio->bi_opf = bio_op | write_flags;
  			bio->bi_private = cb;
  			bio->bi_end_io = end_compressed_bio_write;
@@ -748,7 +750,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  	/* include any pages we added in add_ra-bio_pages */
  	cb->len = bio->bi_iter.bi_size;
 
-	comp_bio = btrfs_bio_alloc(cur_disk_byte);
+	comp_bio = btrfs_io_bio_alloc(0);
+	comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
  	comp_bio->bi_opf = REQ_OP_READ;
  	comp_bio->bi_private = cb;
  	comp_bio->bi_end_io = end_compressed_bio_read;
@@ -806,7 +809,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  				bio_endio(comp_bio);
  			}
 
-			comp_bio = btrfs_bio_alloc(cur_disk_byte);
+			comp_bio = btrfs_io_bio_alloc(0);
+			comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
  			comp_bio->bi_opf = REQ_OP_READ;
  			comp_bio->bi_private = cb;
  			comp_bio->bi_end_io = end_compressed_bio_read;
fs/btrfs/extent_io.c +0 -0
index 1aed03ef5f49..d3fcf7e8dc48 100644
@@ -3121,16 +3121,22 @@ static inline void btrfs_io_bio_init

After reading through the whole patch I agree with the naming, though yeah it's a bit long, but we've been using this wordy naming. For identifiers it's fine to use lbio and it's now clear from the context that it's about the btrfs-specific features.(struct btrfs_io_bio *btrfs_bio)

Quoted 19 lines
fs/btrfs/extent_io.c +8 -4
  }
 
  /*
- * The following helpers allocate a bio. As it's backed by a bioset, it'll
- * never fail.  We're returning a bio right now but you can call btrfs_io_bio
- * for the appropriate container_of magic
+ * Allocate a btrfs_io_bio, with @nr_iovecs as maximum iovecs.
+ *
+ * If @nr_iovecs is 0, it will use BIO_MAX_VECS as @nr_iovces instead.
+ * This behavior is to provide a fail-safe default value.
+ *
+ * This helper uses bioset to allocate the bio, thus it's backed by mempool,
+ * and should not fail from process contexts.
   */
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
  {
  	struct bio *bio;
 
Quoted 10 lines

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) · line 3138

Quoted 5 lines
fs/btrfs/extent_io.c +3 -2
-	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset);
-	bio->bi_iter.bi_sector = first_byte >> 9;
+	ASSERT(nr_iovecs <= BIO_MAX_VECS);
+	if (nr_iovecs == 0)
+		nr_iovecs = BIO_MAX_VECS;

hell no! How come passing 0 actually means BIO_MAX_VEC. Instead of having 0 everywhere and have the function translate this to BIO_MAX_VECS, simply pass BIO_MAX_VECS in every call site where it's needed.

That's part of the feedback I want.

I'm not yet determined on which should be the proper way.

Yes, we can pass BIO_MAX_VEC for call sites which doesn't care about the vector size.

But I also think letting callers to bother less is a good idea. (one of the few moments I think function overriding can be very useful here)

If you have objection, I'm pretty happy to change the behavior, and just do an ASSERT() to catch any values larger than BIO_MAX_VECS.

Thanks,
Qu

Quoted 6 lines

David, please either fix the patch in the tree or retract it. Let's try and refrain from adding such "gems" to the code base.

<snip>

#9 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 130 lines · Qu Wenruo
Quoted 109 lines · Nikolay Borisov

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init

Quoted 79 lines · Qu Wenruo

The helper btrfs_bio_alloc() is almost the same as btrfs_io_bio_alloc(), except it's allocating using BIO_MAX_VECS as @nr_iovecs, and initialize bio->bi_iter.bi_sector.

However the naming itself is not using "btrfs_io_bio" to indicate its parameter is "strcut btrfs_io_bio" and can be easily confused with "struct btrfs_bio".

Considering assigned bio->bi_iter.bi_sector is such a simple work and there are already tons of call sites doing that manually, there is no need to do that in a helper.

Remove btrfs_bio_alloc() helper, and enhance btrfs_io_bio_alloc() function to provide a fail-safe value for its @nr_iovecs.

And then replace all btrfs_bio_alloc() callers with btrfs_io_bio_alloc().

---
  fs/btrfs/compression.c | 12 ++++++++---- fs/btrfs/extent_io.c   | 33 +++++++++++++++------------------ fs/btrfs/extent_io.h   |  1 -

  3 files changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +2 -1
index 7869ad12bc6e..2475dc0b1c22 100644
@@ -418,7 +418,8 @@ blk_status_t btrfs_submit_compressed_write(struct
 btrfs_inode *inode, u64 start,
       cb->orig_bio = NULL;
       cb->nr_pages = nr_pages;
 
-    bio = btrfs_bio_alloc(first_byte);
+    bio = btrfs_io_bio_alloc(0);
+    bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
       bio->bi_opf = bio_op | write_flags;
       bio->bi_private = cb;

      bio->bi_end_io = end_compressed_bio_write;

fs/btrfs/compression.c +2 -1
@@ -490,7 +491,8 @@ blk_status_t btrfs_submit_compressed_write(struct
 btrfs_inode *inode, u64 start,
                   bio_endio(bio);
               }
 
-            bio = btrfs_bio_alloc(first_byte);
+            bio = btrfs_io_bio_alloc(0);
+            bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
               bio->bi_opf = bio_op | write_flags;
               bio->bi_private = cb;

              bio->bi_end_io = end_compressed_bio_write;

fs/btrfs/compression.c +2 -1
@@ -748,7 +750,8 @@ blk_status_t btrfs_submit_compressed_read(struct
 inode *inode, struct bio *bio,
       /* include any pages we added in add_ra-bio_pages */
       cb->len = bio->bi_iter.bi_size;
 
-    comp_bio = btrfs_bio_alloc(cur_disk_byte);
+    comp_bio = btrfs_io_bio_alloc(0);
+    comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
       comp_bio->bi_opf = REQ_OP_READ;
       comp_bio->bi_private = cb;

      comp_bio->bi_end_io = end_compressed_bio_read;

fs/btrfs/compression.c +2 -1
@@ -806,7 +809,8 @@ blk_status_t btrfs_submit_compressed_read(struct
 inode *inode, struct bio *bio,
                   bio_endio(comp_bio);
               }
 
-            comp_bio = btrfs_bio_alloc(cur_disk_byte);
+            comp_bio = btrfs_io_bio_alloc(0);
+            comp_bio->bi_iter.bi_sector = cur_disk_byte >>
 SECTOR_SHIFT;
               comp_bio->bi_opf = REQ_OP_READ;

              comp_bio->bi_private = cb; comp_bio->bi_end_io = end_compressed_bio_read;

fs/btrfs/extent_io.c +0 -0
index 1aed03ef5f49..d3fcf7e8dc48 100644
@@ -3121,16 +3121,22 @@ static inline void btrfs_io_bio_init

After reading through the whole patch I agree with the naming, though yeah it's a bit long, but we've been using this wordy naming. For identifiers it's fine to use lbio and it's now clear from the context that it's about the btrfs-specific features.(struct btrfs_io_bio *btrfs_bio)

Quoted 22 lines
fs/btrfs/extent_io.c +0 -1
   }
 
   /*
- * The following helpers allocate a bio. As it's backed by a bioset,

it'll
- * never fail.  We're returning a bio right now but you can call btrfs_io_bio
- * for the appropriate container_of magic
+ * Allocate a btrfs_io_bio, with @nr_iovecs as maximum iovecs.
+ *
+ * If @nr_iovecs is 0, it will use BIO_MAX_VECS as @nr_iovces instead.
+ * This behavior is to provide a fail-safe default value.
+ *
+ * This helper uses bioset to allocate the bio, thus it's backed by mempool,
+ * and should not fail from process contexts.
   */

fs/btrfs/extent_io.c +1 -1
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)

  {
      struct bio *bio;

Quoted 10 lines
Quoted 5 lines

-    bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset); -    bio->bi_iter.bi_sector = first_byte >> 9;
+    ASSERT(nr_iovecs <= BIO_MAX_VECS);
+    if (nr_iovecs == 0)
+        nr_iovecs = BIO_MAX_VECS;

hell no! How come passing 0 actually means BIO_MAX_VEC. Instead of having 0 everywhere and have the function translate this to BIO_MAX_VECS, simply pass BIO_MAX_VECS in every call site where it's needed.

That's part of the feedback I want.

I'm not yet determined on which should be the proper way.

Yes, we can pass BIO_MAX_VEC for call sites which doesn't care about the vector size.

Quoted 6 lines

But I also think letting callers to bother less is a good idea. (one of the few moments I think function overriding can be very useful here)

If you have objection, I'm pretty happy to change the behavior, and just do an ASSERT() to catch any values larger than BIO_MAX_VECS.

This sounds much better, in the worst case it should be -1 which treated as the "max" and definitely not 0. I don't think 0 has been used as a special value to mean "max" anywhere.

Quoted 10 lines

Thanks,
Qu

Quoted 6 lines

David, please either fix the patch in the tree or retract it. Let's try and refrain from adding such "gems" to the code base.

<snip>

#10 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

In reply to Nikolay Borisov
Quoted 5 lines · Nikolay Borisov
Quoted 5 lines
fs/btrfs/extent_io.c +1 -1
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
 {
 	struct bio *bio;
Quoted 10 lines

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) · line 3138

Quoted 5 lines
fs/btrfs/extent_io.c +3 -2
-	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset);
-	bio->bi_iter.bi_sector = first_byte >> 9;
+	ASSERT(nr_iovecs <= BIO_MAX_VECS);
+	if (nr_iovecs == 0)
+		nr_iovecs = BIO_MAX_VECS;

hell no! How come passing 0 actually means BIO_MAX_VEC. Instead of having 0 everywhere and have the function translate this to BIO_MAX_VECS, simply pass BIO_MAX_VECS in every call site where it's needed.

I had thought about that before and cocluded that passing BIO_MAX_VECS everywhere would be the wrong way as it's a detail about how many bio vecs are allocated. So 0 is a default is ok as any other number means that it's the exact count.

Quoted 2 lines

David, please either fix the patch in the tree or retract it. Let's try and refrain from adding such "gems" to the code base.

So should we add another helper that takes the exact number and drop the parameter everwhere is 0 so it's just btrfs_io_bio_alloc() with the fallback?

#11 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 3 lines · David Sterba

So should we add another helper that takes the exact number and drop the parameter everwhere is 0 so it's just btrfs_io_bio_alloc() with the fallback?

But by adding another helper we just introduce more indirection.

Actually I'd argue that if 0 is a sane default then BIO_MAX_VECS cannot be any worse because:

a) It's a number which is as good as 0
b) It's even named. So this is technically better than a plain 0

#12 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 7 lines · Nikolay Borisov
Quoted 3 lines · David Sterba

So should we add another helper that takes the exact number and drop the parameter everwhere is 0 so it's just btrfs_io_bio_alloc() with the fallback?

Quoted 8 lines

But by adding another helper we just introduce more indirection.

Actually I'd argue that if 0 is a sane default then BIO_MAX_VECS cannot be any worse because:

a) It's a number which is as good as 0
b) It's even named. So this is technically better than a plain 0

Any final call on this?

I hope this could be an example for future optional parameters.

We have some existing codes using two different inline functions, and both of them call a internal but exported function with "__" prefix.

We also have call sites passing all needed parameters just like Nikolay suggested.

Despite that, I have some further optimization for the btrfs_logical_bio structure, thus I hope this helper situation can be solved soon.

Thanks,
Qu

#13 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 21 lines · Qu Wenruo
Quoted 7 lines · Nikolay Borisov
Quoted 3 lines · David Sterba

So should we add another helper that takes the exact number and drop the parameter everwhere is 0 so it's just btrfs_io_bio_alloc() with the fallback?

Quoted 8 lines

But by adding another helper we just introduce more indirection.

Actually I'd argue that if 0 is a sane default then BIO_MAX_VECS cannot be any worse because:

a) It's a number which is as good as 0
b) It's even named. So this is technically better than a plain 0

Any final call on this?

Quoted 7 lines

I hope this could be an example for future optional parameters.

We have some existing codes using two different inline functions, and both of them call a internal but exported function with "__" prefix.

We also have call sites passing all needed parameters just like Nikolay suggested.

I'm fine with explicitly using BIO_MAX_VECS instead of 0. I'll update it in the patch, no need to resend.

author
#14 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

Quoted 24 lines · David Sterba
Quoted 23 lines · Qu Wenruo
Quoted 7 lines · Nikolay Borisov
Quoted 3 lines · David Sterba

So should we add another helper that takes the exact number and drop the parameter everwhere is 0 so it's just btrfs_io_bio_alloc() with the fallback?

Quoted 8 lines

But by adding another helper we just introduce more indirection.

Actually I'd argue that if 0 is a sane default then BIO_MAX_VECS cannot be any worse because:

a) It's a number which is as good as 0
b) It's even named. So this is technically better than a plain 0

Any final call on this?

I hope this could be an example for future optional parameters.

Quoted 9 lines
Quoted 5 lines

We have some existing codes using two different inline functions, and both of them call a internal but exported function with "__" prefix.

We also have call sites passing all needed parameters just like Nikolay suggested.

I'm fine with explicitly using BIO_MAX_VECS instead of 0. I'll update it in the patch, no need to resend.

Thank you very much.

I'll no longer use this tricky way any more.

Thanks,
Qu

author
#15 lore raw

Re: [PATCH v3 2/3] btrfs: remove btrfs_bio_alloc() helper

In reply to Qu Wenruo
Quoted 85 lines · Qu Wenruo

The helper btrfs_bio_alloc() is almost the same as btrfs_io_bio_alloc(), except it's allocating using BIO_MAX_VECS as @nr_iovecs, and initialize bio->bi_iter.bi_sector.

However the naming itself is not using "btrfs_io_bio" to indicate its parameter is "strcut btrfs_io_bio" and can be easily confused with "struct btrfs_bio".

Considering assigned bio->bi_iter.bi_sector is such a simple work and there are already tons of call sites doing that manually, there is no need to do that in a helper.

Remove btrfs_bio_alloc() helper, and enhance btrfs_io_bio_alloc() function to provide a fail-safe value for its @nr_iovecs.

And then replace all btrfs_bio_alloc() callers with btrfs_io_bio_alloc().

---
  fs/btrfs/compression.c | 12 ++++++++----
  fs/btrfs/extent_io.c   | 33 +++++++++++++++------------------
  fs/btrfs/extent_io.h   |  1 -
  3 files changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/compression.c +8 -4
index 7869ad12bc6e..2475dc0b1c22 100644
@@ -418,7 +418,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
  	cb->orig_bio = NULL;
  	cb->nr_pages = nr_pages;
 
-	bio = btrfs_bio_alloc(first_byte);
+	bio = btrfs_io_bio_alloc(0);
+	bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
  	bio->bi_opf = bio_op | write_flags;
  	bio->bi_private = cb;
  	bio->bi_end_io = end_compressed_bio_write;
@@ -490,7 +491,8 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
  				bio_endio(bio);
  			}
 
-			bio = btrfs_bio_alloc(first_byte);
+			bio = btrfs_io_bio_alloc(0);
+			bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
  			bio->bi_opf = bio_op | write_flags;
  			bio->bi_private = cb;
  			bio->bi_end_io = end_compressed_bio_write;
@@ -748,7 +750,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  	/* include any pages we added in add_ra-bio_pages */
  	cb->len = bio->bi_iter.bi_size;
 
-	comp_bio = btrfs_bio_alloc(cur_disk_byte);
+	comp_bio = btrfs_io_bio_alloc(0);
+	comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
  	comp_bio->bi_opf = REQ_OP_READ;
  	comp_bio->bi_private = cb;
  	comp_bio->bi_end_io = end_compressed_bio_read;
@@ -806,7 +809,8 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  				bio_endio(comp_bio);
  			}
 
-			comp_bio = btrfs_bio_alloc(cur_disk_byte);
+			comp_bio = btrfs_io_bio_alloc(0);
+			comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
  			comp_bio->bi_opf = REQ_OP_READ;
  			comp_bio->bi_private = cb;
  			comp_bio->bi_end_io = end_compressed_bio_read;
fs/btrfs/extent_io.c +5 -3
index 1aed03ef5f49..d3fcf7e8dc48 100644
@@ -3121,16 +3121,22 @@ static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio)
  }
 
  /*
- * The following helpers allocate a bio. As it's backed by a bioset, it'll
- * never fail.  We're returning a bio right now but you can call btrfs_io_bio
- * for the appropriate container_of magic
+ * Allocate a btrfs_io_bio, with @nr_iovecs as maximum iovecs.
+ *
+ * If @nr_iovecs is 0, it will use BIO_MAX_VECS as @nr_iovces instead.
+ * This behavior is to provide a fail-safe default value.
+ *

fs/btrfs/extent_io.c static inline void btrfs_io_bio_init(struct btrfs_io_bio *btrfs_bio) · line 3133

Quoted 10 lines
fs/btrfs/extent_io.c +3 -3
+ * This helper uses bioset to allocate the bio, thus it's backed by mempool,
+ * and should not fail from process contexts.
   */
-struct bio *btrfs_bio_alloc(u64 first_byte)
+struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
  {
  	struct bio *bio;
 
-	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &btrfs_bioset);
-	bio->bi_iter.bi_sector = first_byte >> 9;

I'm very surprised that, bbio->logical is not initialized here.

This means, except two call sites which manually initialize bbio->logical, all other sites don't have bbio->logical set from the very beginning.

I guess I need another patch to set the logical bytenr for all btrfs_bio allocator.

Thankfully it doesn't cause any new regression, but any unitizlied member can always cause unexpected behavior when new use cases are added.

In fact this uninitialized @logical is already cause my RFC patchset ("btrfs: refactor how we handle btrfs_io_context and slightly reduce memory usage for both btrfs_bio and btrfs_io_context") to crash.

As in that patchset, we require bbio->logical to lookup the mirror device.

I'll merge the proper initializer into the next version of that patchset.

Thanks,
Qu

Quoted 55 lines
fs/btrfs/extent_io.c +7 -12
+	ASSERT(nr_iovecs <= BIO_MAX_VECS);
+	if (nr_iovecs == 0)
+		nr_iovecs = BIO_MAX_VECS;
+	bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
  	btrfs_io_bio_init(btrfs_io_bio(bio));
  	return bio;
  }
@@ -3148,16 +3154,6 @@ struct bio *btrfs_bio_clone(struct bio *bio)
  	return new;
  }
 
-struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs)
-{
-	struct bio *bio;
-
-	/* Bio allocation backed by a bioset does not fail */
-	bio = bio_alloc_bioset(GFP_NOFS, nr_iovecs, &btrfs_bioset);
-	btrfs_io_bio_init(btrfs_io_bio(bio));
-	return bio;
-}
-
  struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size)
  {
  	struct bio *bio;
@@ -3307,14 +3303,15 @@ static int alloc_new_bio(struct btrfs_inode *inode,
  	struct bio *bio;
  	int ret;
 
+	bio = btrfs_io_bio_alloc(0);
  	/*
  	 * For compressed page range, its disk_bytenr is always @disk_bytenr
  	 * passed in, no matter if we have added any range into previous bio.
  	 */
  	if (bio_flags & EXTENT_BIO_COMPRESSED)
-		bio = btrfs_bio_alloc(disk_bytenr);
+		bio->bi_iter.bi_sector = disk_bytenr >> SECTOR_SHIFT;
  	else
-		bio = btrfs_bio_alloc(disk_bytenr + offset);
+		bio->bi_iter.bi_sector = (disk_bytenr + offset) >> SECTOR_SHIFT;
  	bio_ctrl->bio = bio;
  	bio_ctrl->bio_flags = bio_flags;
  	bio->bi_end_io = end_io_func;
fs/btrfs/extent_io.h +0 -1
index ba471f2063a7..81fa68eaa699 100644
@@ -278,7 +278,6 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
  void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
  				  struct page *locked_page,
  				  u32 bits_to_clear, unsigned long page_ops);
-struct bio *btrfs_bio_alloc(u64 first_byte);
  struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs);
  struct bio *btrfs_bio_clone(struct bio *bio);
  struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size);
author
#16 lore raw

PATCH v3 3/3 btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

In reply to Qu Wenruo

Previously we have "struct btrfs_bio", which records IO context for mirrored IO and RAID56, and "strcut btrfs_io_bio", which records extra btrfs specific info for logical bytenr bio.

With "strcut btrfs_bio" renamed to "struct btrfs_io_context", we are safe to rename "strcut btrfs_io_bio" to "strcut btrfs_logical_bio" which is a more suitable name now.

Although the name, "btrfs_logical_bio", is a little long and name "btrfs_bio" can be much shorter, "btrfs_bio" conflicts with previous "btrfs_bio" structure and can cause a lot of problems for backports.

Thus here we choose the name "btrfs_logical_bio", which also emphasis those bios all work at logical bytenr.

---
 fs/btrfs/check-integrity.c |  2 +-
 fs/btrfs/compression.c     | 16 ++++-----
 fs/btrfs/ctree.h           |  6 ++--
 fs/btrfs/disk-io.c         |  2 +-
 fs/btrfs/disk-io.h         |  2 +-
 fs/btrfs/extent_io.c       | 69 +++++++++++++++++++-------------------
 fs/btrfs/extent_io.h       |  7 ++--
 fs/btrfs/file-item.c       | 12 +++----
 fs/btrfs/inode.c           | 50 ++++++++++++++-------------
 fs/btrfs/raid56.c          |  8 ++---
 fs/btrfs/scrub.c           | 14 ++++----
 fs/btrfs/volumes.c         | 10 +++---
 fs/btrfs/volumes.h         | 29 ++++++++--------
 13 files changed, 116 insertions(+), 111 deletions(-)
fs/btrfs/check-integrity.c +1 -1
index 81b11124b67a..94fefaa6438c 100644
@@ -1561,7 +1561,7 @@ static int btrfsic_read_block(struct btrfsic_state *state,
 		struct bio *bio;
 		unsigned int j;
 
-		bio = btrfs_io_bio_alloc(num_pages - i);
+		bio = btrfs_logical_bio_alloc(num_pages - i);
 		bio_set_dev(bio, block_ctx->dev->bdev);
 		bio->bi_iter.bi_sector = dev_bytenr >> 9;
 		bio->bi_opf = REQ_OP_READ;
fs/btrfs/compression.c +8 -8
index 2475dc0b1c22..331ef88b87d1 100644
@@ -179,9 +179,9 @@ static int check_compressed_csum(struct btrfs_inode *inode, struct bio *bio,
 			if (memcmp(&csum, cb_sum, csum_size) != 0) {
 				btrfs_print_data_csum_error(inode, disk_start,
 						csum, cb_sum, cb->mirror_num);
-				if (btrfs_io_bio(bio)->device)
+				if (btrfs_logical_bio(bio)->device)
 					btrfs_dev_stat_inc_and_print(
-						btrfs_io_bio(bio)->device,
+						btrfs_logical_bio(bio)->device,
 						BTRFS_DEV_STAT_CORRUPTION_ERRS);
 				return -EIO;
 			}
@@ -208,7 +208,7 @@ static void end_compressed_bio_read(struct bio *bio)
 	struct inode *inode;
 	struct page *page;
 	unsigned int index;
-	unsigned int mirror = btrfs_io_bio(bio)->mirror_num;
+	unsigned int mirror = btrfs_logical_bio(bio)->mirror_num;
 	int ret = 0;
 
 	if (bio->bi_status)
@@ -224,7 +224,7 @@ static void end_compressed_bio_read(struct bio *bio)
 	 * Record the correct mirror_num in cb->orig_bio so that
 	 * read-repair can work properly.
 	 */
-	btrfs_io_bio(cb->orig_bio)->mirror_num = mirror;
+	btrfs_logical_bio(cb->orig_bio)->mirror_num = mirror;
 	cb->mirror_num = mirror;
 
 	/*
@@ -418,7 +418,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 	cb->orig_bio = NULL;
 	cb->nr_pages = nr_pages;
 
-	bio = btrfs_io_bio_alloc(0);
+	bio = btrfs_logical_bio_alloc(0);
 	bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 	bio->bi_opf = bio_op | write_flags;
 	bio->bi_private = cb;
@@ -491,7 +491,7 @@ blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
 				bio_endio(bio);
 			}
 
-			bio = btrfs_io_bio_alloc(0);
+			bio = btrfs_logical_bio_alloc(0);
 			bio->bi_iter.bi_sector = first_byte >> SECTOR_SHIFT;
 			bio->bi_opf = bio_op | write_flags;
 			bio->bi_private = cb;
@@ -750,7 +750,7 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 	/* include any pages we added in add_ra-bio_pages */
 	cb->len = bio->bi_iter.bi_size;
 
-	comp_bio = btrfs_io_bio_alloc(0);
+	comp_bio = btrfs_logical_bio_alloc(0);
 	comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 	comp_bio->bi_opf = REQ_OP_READ;
 	comp_bio->bi_private = cb;
@@ -809,7 +809,7 @@ blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
 				bio_endio(comp_bio);
 			}
 
-			comp_bio = btrfs_io_bio_alloc(0);
+			comp_bio = btrfs_logical_bio_alloc(0);
 			comp_bio->bi_iter.bi_sector = cur_disk_byte >> SECTOR_SHIFT;
 			comp_bio->bi_opf = REQ_OP_READ;
 			comp_bio->bi_private = cb;
fs/btrfs/ctree.h +4 -2
index 38870ae46cbb..41bc28f6c6c4 100644
@@ -48,6 +48,7 @@ extern struct kmem_cache *btrfs_free_space_cachep;
 extern struct kmem_cache *btrfs_free_space_bitmap_cachep;
 struct btrfs_ordered_sum;
 struct btrfs_ref;
+struct btrfs_logical_bio;
 
 #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
 
@@ -3133,8 +3134,9 @@ u64 btrfs_file_extent_end(const struct btrfs_path *path);
 /* inode.c */
 blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
 				   int mirror_num, unsigned long bio_flags);
-unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
-				    struct page *page, u64 start, u64 end);
+unsigned int btrfs_verify_data_csum(struct btrfs_logical_bio *lbio,
+				    u32 bio_offset, struct page *page,
+				    u64 start, u64 end);
 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
 					   u64 start, u64 len);
 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
fs/btrfs/disk-io.c +1 -1
index 7d80e5b22d32..4909f6e5c6f8 100644
@@ -683,7 +683,7 @@ static int validate_subpage_buffer(struct page *page, u64 start, u64 end,
 	return ret;
 }
 
-int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio,
+int btrfs_validate_metadata_buffer(struct btrfs_logical_bio *lbio,
 				   struct page *page, u64 start, u64 end,
 				   int mirror)
 {
fs/btrfs/disk-io.h +1 -1
index 0e7e9526b6a8..aad13d02b9ad 100644
@@ -81,7 +81,7 @@ void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info);
 void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info);
 void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
 				 struct btrfs_root *root);
-int btrfs_validate_metadata_buffer(struct btrfs_io_bio *io_bio,
+int btrfs_validate_metadata_buffer(struct btrfs_logical_bio *lbio,
 				   struct page *page, u64 start, u64 end,
 				   int mirror);
 blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio,
fs/btrfs/extent_io.c +35 -34

186 lines not on this page · raw message

fs/btrfs/extent_io.h +4 -3
index 81fa68eaa699..7eac8536b162 100644
@@ -278,9 +278,10 @@ void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end);
 void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
 				  struct page *locked_page,
 				  u32 bits_to_clear, unsigned long page_ops);
-struct bio *btrfs_io_bio_alloc(unsigned int nr_iovecs);
-struct bio *btrfs_bio_clone(struct bio *bio);
-struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size);
+struct bio *btrfs_logical_bio_alloc(unsigned int nr_iovecs);
+struct bio *btrfs_logical_bio_clone(struct bio *bio);
+struct bio *btrfs_logical_bio_clone_partial(struct bio *orig, u64 offset,
+					    u64 size);
 
 void end_extent_writepage(struct page *page, int err, u64 start, u64 end);
 int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num);
fs/btrfs/file-item.c +6 -6
index 2673c6ba7a4e..6e788eb3c689 100644
@@ -358,7 +358,7 @@ static int search_file_offset_in_bio(struct bio *bio, struct inode *inode,
  * @dst: Buffer of size nblocks * btrfs_super_csum_size() used to return
  *       checksum (nblocks = bio->bi_iter.bi_size / fs_info->sectorsize). If
  *       NULL, the checksum buffer is allocated and returned in
- *       btrfs_io_bio(bio)->csum instead.
+ *       btrfs_bio(bio)->csum instead.
  *
  * Return: BLK_STS_RESOURCE if allocating memory fails, BLK_STS_OK otherwise.
  */
@@ -397,19 +397,19 @@ blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u8 *dst
 		return BLK_STS_RESOURCE;
 
 	if (!dst) {
-		struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
+		struct btrfs_logical_bio *logical_bio = btrfs_logical_bio(bio);
 
 		if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
-			btrfs_bio->csum = kmalloc_array(nblocks, csum_size,
+			logical_bio->csum = kmalloc_array(nblocks, csum_size,
 							GFP_NOFS);
-			if (!btrfs_bio->csum) {
+			if (!logical_bio->csum) {
 				btrfs_free_path(path);
 				return BLK_STS_RESOURCE;
 			}
 		} else {
-			btrfs_bio->csum = btrfs_bio->csum_inline;
+			logical_bio->csum = logical_bio->csum_inline;
 		}
-		csum = btrfs_bio->csum;
+		csum = logical_bio->csum;
 	} else {
 		csum = dst;
 	}
fs/btrfs/inode.c +26 -24

140 lines not on this page · raw message

fs/btrfs/raid56.c +4 -4
index 96c149416f99..705e2f243459 100644
@@ -1104,8 +1104,8 @@ static int rbio_add_io_page(struct btrfs_raid_bio *rbio,
 	}
 
 	/* put a new bio on the list */
-	bio = btrfs_io_bio_alloc(bio_max_len >> PAGE_SHIFT ?: 1);
-	btrfs_io_bio(bio)->device = stripe->dev;
+	bio = btrfs_logical_bio_alloc(bio_max_len >> PAGE_SHIFT ?: 1);
+	btrfs_logical_bio(bio)->device = stripe->dev;
 	bio->bi_iter.bi_size = 0;
 	bio_set_dev(bio, stripe->dev->bdev);
 	bio->bi_iter.bi_sector = disk_start >> 9;
@@ -1158,7 +1158,7 @@ static void index_rbio_pages(struct btrfs_raid_bio *rbio)
 		page_index = stripe_offset >> PAGE_SHIFT;
 
 		if (bio_flagged(bio, BIO_CLONED))
-			bio->bi_iter = btrfs_io_bio(bio)->iter;
+			bio->bi_iter = btrfs_logical_bio(bio)->iter;
 
 		bio_for_each_segment(bvec, bio, iter) {
 			rbio->bio_pages[page_index + i] = bvec.bv_page;
@@ -2124,7 +2124,7 @@ int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
 
 	if (generic_io) {
 		ASSERT(bioc->mirror_num == mirror_num);
-		btrfs_io_bio(bio)->mirror_num = mirror_num;
+		btrfs_logical_bio(bio)->mirror_num = mirror_num;
 	}
 
 	rbio = alloc_rbio(fs_info, bioc, stripe_len);
fs/btrfs/scrub.c +7 -7
index bd38e32ef5dc..c1dfe7f3ace6 100644
@@ -1423,7 +1423,7 @@ static void scrub_recheck_block_on_raid56(struct btrfs_fs_info *fs_info,
 	if (!first_page->dev->bdev)
 		goto out;
 
-	bio = btrfs_io_bio_alloc(BIO_MAX_VECS);
+	bio = btrfs_logical_bio_alloc(0);
 	bio_set_dev(bio, first_page->dev->bdev);
 
 	for (page_num = 0; page_num < sblock->page_count; page_num++) {
@@ -1480,7 +1480,7 @@ static void scrub_recheck_block(struct btrfs_fs_info *fs_info,
 		}
 
 		WARN_ON(!spage->page);
-		bio = btrfs_io_bio_alloc(1);
+		bio = btrfs_logical_bio_alloc(1);
 		bio_set_dev(bio, spage->dev->bdev);
 
 		bio_add_page(bio, spage->page, fs_info->sectorsize, 0);
@@ -1562,7 +1562,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
 			return -EIO;
 		}
 
-		bio = btrfs_io_bio_alloc(1);
+		bio = btrfs_logical_bio_alloc(1);
 		bio_set_dev(bio, spage_bad->dev->bdev);
 		bio->bi_iter.bi_sector = spage_bad->physical >> 9;
 		bio->bi_opf = REQ_OP_WRITE;
@@ -1676,7 +1676,7 @@ static int scrub_add_page_to_wr_bio(struct scrub_ctx *sctx,
 		sbio->dev = sctx->wr_tgtdev;
 		bio = sbio->bio;
 		if (!bio) {
-			bio = btrfs_io_bio_alloc(sctx->pages_per_wr_bio);
+			bio = btrfs_logical_bio_alloc(sctx->pages_per_wr_bio);
 			sbio->bio = bio;
 		}
 
@@ -2102,7 +2102,7 @@ static int scrub_add_page_to_rd_bio(struct scrub_ctx *sctx,
 		sbio->dev = spage->dev;
 		bio = sbio->bio;
 		if (!bio) {
-			bio = btrfs_io_bio_alloc(sctx->pages_per_rd_bio);
+			bio = btrfs_logical_bio_alloc(sctx->pages_per_rd_bio);
 			sbio->bio = bio;
 		}
 
@@ -2226,7 +2226,7 @@ static void scrub_missing_raid56_pages(struct scrub_block *sblock)
 		goto bioc_out;
 	}
 
-	bio = btrfs_io_bio_alloc(0);
+	bio = btrfs_logical_bio_alloc(0);
 	bio->bi_iter.bi_sector = logical >> 9;
 	bio->bi_private = sblock;
 	bio->bi_end_io = scrub_missing_raid56_end_io;
@@ -2842,7 +2842,7 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
 	if (ret || !bioc || !bioc->raid_map)
 		goto bioc_out;
 
-	bio = btrfs_io_bio_alloc(0);
+	bio = btrfs_logical_bio_alloc(0);
 	bio->bi_iter.bi_sector = sparity->logic_start >> 9;
 	bio->bi_private = sparity;
 	bio->bi_end_io = scrub_parity_bio_endio;
fs/btrfs/volumes.c +5 -5
index 592d19f95065..9c609ed2606f 100644
@@ -6561,7 +6561,7 @@ static void btrfs_end_bio(struct bio *bio)
 		atomic_inc(&bioc->error);
 		if (bio->bi_status == BLK_STS_IOERR ||
 		    bio->bi_status == BLK_STS_TARGET) {
-			struct btrfs_device *dev = btrfs_io_bio(bio)->device;
+			struct btrfs_device *dev = btrfs_logical_bio(bio)->device;
 
 			ASSERT(dev->bdev);
 			if (btrfs_op(bio) == BTRFS_MAP_WRITE)
@@ -6587,7 +6587,7 @@ static void btrfs_end_bio(struct bio *bio)
 			bio = bioc->orig_bio;
 		}
 
-		btrfs_io_bio(bio)->mirror_num = bioc->mirror_num;
+		btrfs_logical_bio(bio)->mirror_num = bioc->mirror_num;
 		/* only send an error to the higher layers if it is
 		 * beyond the tolerance of the btrfs bio
 		 */
@@ -6613,7 +6613,7 @@ static void submit_stripe_bio(struct btrfs_io_context *bioc, struct bio *bio,
 	struct btrfs_fs_info *fs_info = bioc->fs_info;
 
 	bio->bi_private = bioc;
-	btrfs_io_bio(bio)->device = dev;
+	btrfs_logical_bio(bio)->device = dev;
 	bio->bi_end_io = btrfs_end_bio;
 	bio->bi_iter.bi_sector = physical >> 9;
 	/*
@@ -6649,7 +6649,7 @@ static void bioc_error(struct btrfs_io_context *bioc, struct bio *bio, u64 logic
 		/* Should be the original bio. */
 		WARN_ON(bio != bioc->orig_bio);
 
-		btrfs_io_bio(bio)->mirror_num = bioc->mirror_num;
+		btrfs_logical_bio(bio)->mirror_num = bioc->mirror_num;
 		bio->bi_iter.bi_sector = logical >> 9;
 		if (atomic_read(&bioc->error) > bioc->max_errors)
 			bio->bi_status = BLK_STS_IOERR;
@@ -6724,7 +6724,7 @@ blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
 		}
 
 		if (dev_nr < total_devs - 1)
-			bio = btrfs_bio_clone(first_bio);
+			bio = btrfs_logical_bio_clone(first_bio);
 		else
 			bio = first_bio;
 
fs/btrfs/volumes.h +14 -15
index b69755fc0e0d..6b0edca6ed2a 100644
@@ -304,38 +304,37 @@ struct btrfs_fs_devices {
 				/ sizeof(struct btrfs_stripe) + 1)
 
 /*
- * we need the mirror number and stripe index to be passed around
- * the call chain while we are processing end_io (especially errors).
- * Really, what we need is a btrfs_io_context structure that has this info
- * and is properly sized with its stripe array, but we're not there
- * quite yet.  We have our own btrfs bioset, and all of the bios
- * we allocate are actually btrfs_io_bios.  We'll cram as much of
- * struct btrfs_io_context as we can into this over time.
+ * Extra info to pass along bio.
+ *
+ * Mostly for btrfs specific feature like csum and mirror_num.
  */
-struct btrfs_io_bio {
+struct btrfs_logical_bio {
 	unsigned int mirror_num;
+
+	/* @device is for stripe IO submission. */
 	struct btrfs_device *device;
 	u64 logical;
 	u8 *csum;
 	u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
 	struct bvec_iter iter;
+
 	/*
 	 * This member must come last, bio_alloc_bioset will allocate enough
-	 * bytes for entire btrfs_io_bio but relies on bio being last.
+	 * bytes for entire btrfs_bio but relies on bio being last.
 	 */
 	struct bio bio;
 };
 
-static inline struct btrfs_io_bio *btrfs_io_bio(struct bio *bio)
+static inline struct btrfs_logical_bio *btrfs_logical_bio(struct bio *bio)
 {
-	return container_of(bio, struct btrfs_io_bio, bio);
+	return container_of(bio, struct btrfs_logical_bio, bio);
 }
 
-static inline void btrfs_io_bio_free_csum(struct btrfs_io_bio *io_bio)
+static inline void btrfs_logical_bio_free_csum(struct btrfs_logical_bio *lbio)
 {
-	if (io_bio->csum != io_bio->csum_inline) {
-		kfree(io_bio->csum);
-		io_bio->csum = NULL;
+	if (lbio->csum != lbio->csum_inline) {
+		kfree(lbio->csum);
+		lbio->csum = NULL;
 	}
 }
 
Signature · 1 line
2.33.0
#17 lore raw

Re: [PATCH v3 3/3] btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

Quoted 8 lines · Qu Wenruo

Previously we have "struct btrfs_bio", which records IO context for mirrored IO and RAID56, and "strcut btrfs_io_bio", which records extra btrfs specific info for logical bytenr bio.

With "strcut btrfs_bio" renamed to "struct btrfs_io_context", we are safe to rename "strcut btrfs_io_bio" to "strcut btrfs_logical_bio" which is a more suitable name now.

Quoted 6 lines

Although the name, "btrfs_logical_bio", is a little long and name "btrfs_bio" can be much shorter, "btrfs_bio" conflicts with previous "btrfs_bio" structure and can cause a lot of problems for backports.

Thus here we choose the name "btrfs_logical_bio", which also emphasis those bios all work at logical bytenr.

After reading through the whole patch I agree with the naming, though yeah it's a bit long, but we've been using this wordy naming. For identifiers it's fine to use lbio and it's now clear from the context that it's about the btrfs-specific features.

#18 lore raw

Re: [PATCH v3 3/3] btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

In reply to Qu Wenruo
Quoted 8 lines · Qu Wenruo

Previously we have "struct btrfs_bio", which records IO context for mirrored IO and RAID56, and "strcut btrfs_io_bio", which records extra btrfs specific info for logical bytenr bio.

With "strcut btrfs_bio" renamed to "struct btrfs_io_context", we are safe to rename "strcut btrfs_io_bio" to "strcut btrfs_logical_bio" which is a more suitable name now.

Quoted 8 lines

Although the name, "btrfs_logical_bio", is a little long and name "btrfs_bio" can be much shorter, "btrfs_bio" conflicts with previous "btrfs_bio" structure and can cause a lot of problems for backports.

Thus here we choose the name "btrfs_logical_bio", which also emphasis those bios all work at logical bytenr.

So thinking a bit more about the renaming we are trading "awkwardness" for future generations so that we make backporting easier or rather more fool proof.

What if we backport a patch that does BUILD_BUG_ON predicated on the size of the btrfs_io_bio. That way if a patch backports cleanly and automatically but in fact git got confused by btrfs_bio vs btrfs_io_bio then a build failure would ensue due to mismatched sizes and that would be a clear indication something has gone wrong so whoever is doing the backport can go and correct the backport? David what do you think about this?

#19 lore raw

Re: [PATCH v3 3/3] btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

Quoted 24 lines · Nikolay Borisov
Quoted 8 lines · Qu Wenruo

Previously we have "struct btrfs_bio", which records IO context for mirrored IO and RAID56, and "strcut btrfs_io_bio", which records extra btrfs specific info for logical bytenr bio.

With "strcut btrfs_bio" renamed to "struct btrfs_io_context", we are safe to rename "strcut btrfs_io_bio" to "strcut btrfs_logical_bio" which is a more suitable name now.

Quoted 8 lines

Although the name, "btrfs_logical_bio", is a little long and name "btrfs_bio" can be much shorter, "btrfs_bio" conflicts with previous "btrfs_bio" structure and can cause a lot of problems for backports.

Thus here we choose the name "btrfs_logical_bio", which also emphasis those bios all work at logical bytenr.

So thinking a bit more about the renaming we are trading "awkwardness" for future generations so that we make backporting easier or rather more fool proof.

Quoted 7 lines

What if we backport a patch that does BUILD_BUG_ON predicated on the size of the btrfs_io_bio. That way if a patch backports cleanly and automatically but in fact git got confused by btrfs_bio vs btrfs_io_bio then a build failure would ensue due to mismatched sizes and that would be a clear indication something has gone wrong so whoever is doing the backport can go and correct the backport? David what do you think about this?

So you want to call the structure btrfs_bio and add build protections? I'm not sure how exactly you want to do the sizeof check, one way would be to add a stub structure and compare sizeof against that, because a hardcoded value won't work due to padding, or we'd have to have a 32bit assertion version.

I'd like to see the code, but otherwise I think it's reasonable, the shorter name would be better. I don't expect many backports regarding the bio related code, it could be referenced in the diff context but that we can handle fine. I'm a bit cautious because I've seen patches to other subystems that did changes like swapping parameters or repurposing structures like here we do and Linus did not like that at all. It's trade off if we'll suffer a naming we don't like or would cause a bug because we'd forget about the change.

#20 lore raw

Re: [PATCH v3 3/3] btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

Quoted 36 lines · David Sterba
Quoted 22 lines · Nikolay Borisov
Quoted 8 lines · Qu Wenruo

Previously we have "struct btrfs_bio", which records IO context for mirrored IO and RAID56, and "strcut btrfs_io_bio", which records extra btrfs specific info for logical bytenr bio.

With "strcut btrfs_bio" renamed to "struct btrfs_io_context", we are safe to rename "strcut btrfs_io_bio" to "strcut btrfs_logical_bio" which is a more suitable name now.

Quoted 8 lines

Although the name, "btrfs_logical_bio", is a little long and name "btrfs_bio" can be much shorter, "btrfs_bio" conflicts with previous "btrfs_bio" structure and can cause a lot of problems for backports.

Thus here we choose the name "btrfs_logical_bio", which also emphasis those bios all work at logical bytenr.

So thinking a bit more about the renaming we are trading "awkwardness" for future generations so that we make backporting easier or rather more fool proof.

Quoted 7 lines

What if we backport a patch that does BUILD_BUG_ON predicated on the size of the btrfs_io_bio. That way if a patch backports cleanly and automatically but in fact git got confused by btrfs_bio vs btrfs_io_bio then a build failure would ensue due to mismatched sizes and that would be a clear indication something has gone wrong so whoever is doing the backport can go and correct the backport? David what do you think about this?

So you want to call the structure btrfs_bio and add build protections? I'm not sure how exactly you want to do the sizeof check, one way would be to add a stub structure and compare sizeof against that, because a hardcoded value won't work due to padding, or we'd have to have a 32bit assertion version.

Quoted 8 lines

I'd like to see the code, but otherwise I think it's reasonable, the shorter name would be better. I don't expect many backports regarding the bio related code, it could be referenced in the diff context but that we can handle fine. I'm a bit cautious because I've seen patches to other subystems that did changes like swapping parameters or repurposing structures like here we do and Linus did not like that at all. It's trade off if we'll suffer a naming we don't like or would cause a bug because we'd forget about the change.

For the record, we had a chat about that and found that explicit build checks won't be necessary as the old/new structure has no overlap of the members so the build would fail anyway.

I did the rename from btrfs_logical_bio* to btrfs_bio again in misc-next, please have a look, it's basically what Qu sent as v2.

#21 lore raw
In reply to Qu Wenruo
Quoted 47 lines · Qu Wenruo

The branch can be fetched from github, and is the preferred way to grab the code, as this patchset changed quite a lot of code.
https://github.com/adam900710/linux/tree/chunk_refactor

There are two structure, btrfs_io_bio and btrfs_bio, which have very similar names but completely different meanings.

Btrfs_io_bio mostly works at logical bytenr layer (its bio->bi_iter.bi_sector points to btrfs logical bytenr), and just contains extra info like csum and mirror_num.

And btrfs_io_bio is in fact the most utilized bio, as all data/metadata IO is using btrfs_io_bio.

While btrfs_bio is completely a helper structure for mirrored IO submission (utilized by SINGLE/DUP/RAID1/RAID10), and contains RAID56 maps for RAID56 (it doesn't utilize this structure for IO submission tracking).

Such naming is completely anti-human.

So this patchset will do the following renaming:

- btrfs_bio -> btrfs_io_context
Since it's not really used by all bios (only mirrored profiles utilize it), and it contains extra info for RAID56, it's not proper to name it with _bio suffix.

Later we can integrate btrfs_io_context pointer into the new btrfs_bio.

- btrfs_io_bio -> btrfs_logical_bio
It is intentional not to reuse "btrfs_bio", which could cause confusion for later backport.

Changelog:
v2:
- Rename btrfs_bio to btrfs_io_context (bioc)
- Rename btrfs_io_bio to btrfs_bio
Both suggested by Nikolay

v3:
- Fixes whiespace problems
Caused by "dwi" vim commands

- Update several modified comments

Quoted 7 lines

- Rename btrfs_io_bio to btrfs_logical_bio
To avoid backport confusion.

Qu Wenruo (3):
btrfs: rename btrfs_bio to btrfs_io_context
btrfs: remove btrfs_bio_alloc() helper
btrfs: rename struct btrfs_io_bio to btrfs_logical_bio

Added to misc-next, thanks.