Skip to contentSkip to conversation

block: Cleanup set_capacity()/bdev_set_nr_sectors()

PATCH Applied Damien Le Moal 3 participants 3 messages Apr 24, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 block: Cleanup set_capacity()/bdev_set_nr_sectors()

The code for setting a block device capacity (bd_nr_sectors field of struct block_device) is duplicated in set_capacity() and bdev_set_nr_sectors(). Clean this up by making bdev_set_nr_sectors() a block layer internal function defined in block/bdev.c instead of having this function statically defined in block/partitions/core.c. With this change, set_capacity() implementation can be simplified to only calling bdev_set_nr_sectors().

---
Changes from v1:
- Addressed Christoph's comments: keep set_capacity as a regular exported function

 block/bdev.c            | 8 ++++++++
 block/blk.h             | 2 ++
 block/genhd.c           | 7 +------
 block/partitions/core.c | 8 --------
 4 files changed, 11 insertions(+), 14 deletions(-)
block/bdev.c +8 -0
index 850852fe4b78..717089a5726f 100644
@@ -428,6 +428,14 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
 	return bdev;
 }
 
+void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
+{
+	spin_lock(&bdev->bd_size_lock);
+	i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
+	bdev->bd_nr_sectors = sectors;
+	spin_unlock(&bdev->bd_size_lock);
+}
+
 void bdev_add(struct block_device *bdev, dev_t dev)
 {
 	bdev->bd_dev = dev;
block/blk.h +2 -0
index 2da831103471..564119a76bc5 100644
@@ -419,6 +419,8 @@ int bdev_resize_partition(struct gendisk *disk, int partno, sector_t start,
 		sector_t length);
 void blk_drop_partitions(struct gendisk *disk);
 
+void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors);
+
 struct gendisk *__alloc_disk_node(struct request_queue *q, int node_id,
 		struct lock_class_key *lkclass);
 
block/genhd.c +1 -6
index 2d58ac54043a..08f701cb8553 100644
@@ -57,12 +57,7 @@ static DEFINE_IDA(ext_devt_ida);
 
 void set_capacity(struct gendisk *disk, sector_t sectors)
 {
-	struct block_device *bdev = disk->part0;
-
-	spin_lock(&bdev->bd_size_lock);
-	i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
-	bdev->bd_nr_sectors = sectors;
-	spin_unlock(&bdev->bd_size_lock);
+	bdev_set_nr_sectors(disk->part0, sectors);
 }
 EXPORT_SYMBOL(set_capacity);
 
block/partitions/core.c +0 -8
index 7b8ef6296abd..49e0496ff23c 100644
@@ -85,14 +85,6 @@ static int (*check_part[])(struct parsed_partitions *) = {
 	NULL
 };
 
-static void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors)
-{
-	spin_lock(&bdev->bd_size_lock);
-	i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
-	bdev->bd_nr_sectors = sectors;
-	spin_unlock(&bdev->bd_size_lock);
-}
-
 static struct parsed_partitions *allocate_partitions(struct gendisk *hd)
 {
 	struct parsed_partitions *state;
Signature · 1 line
2.40.0
maintainer Applied
#2 lore raw
In reply to Damien Le Moal
Quoted 9 lines · Damien Le Moal

The code for setting a block device capacity (bd_nr_sectors field of struct block_device) is duplicated in set_capacity() and bdev_set_nr_sectors(). Clean this up by making bdev_set_nr_sectors() a block layer internal function defined in block/bdev.c instead of having this function statically defined in block/partitions/core.c. With this change, set_capacity() implementation can be simplified to only calling bdev_set_nr_sectors().

[...]

Applied, thanks!

[1/1] block: Cleanup set_capacity()/bdev_set_nr_sectors()

      commit: 83794367dcc6749662b17a1e4b8ec085023fc53b

Best regards,

Signature · 1 line
Jens Axboe