Skip to contentSkip to conversation

block: Cleanup set_capacity()/bdev_set_nr_sectors()

PATCH Superseded Damien Le Moal 3 participants 3 messages Apr 21, 2023 – Apr 24, 2023 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 turning set_capacity() into an inline function calling bdev_set_nr_sectors() and move bdev_set_nr_sectors() code to block/bdev.c instead of having this function in block/partitions/core.c.

---
 block/bdev.c            |  9 +++++++++
 block/genhd.c           | 11 -----------
 block/partitions/core.c |  8 --------
 include/linux/blkdev.h  |  8 +++++++-
 4 files changed, 16 insertions(+), 20 deletions(-)
block/bdev.c +9 -0
index 850852fe4b78..6fc21f30b870 100644
@@ -428,6 +428,15 @@ 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);
+}
+EXPORT_SYMBOL_GPL(bdev_set_nr_sectors);
+
 void bdev_add(struct block_device *bdev, dev_t dev)
 {
 	bdev->bd_dev = dev;
block/genhd.c +0 -11
index cadcb472dfc3..7d44d25f2a54 100644
@@ -55,17 +55,6 @@ static atomic64_t diskseq;
 #define NR_EXT_DEVT		(1 << MINORBITS)
 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);
-}
-EXPORT_SYMBOL(set_capacity);
-
 /*
  * Set disk capacity and notify if the size is not currently zero and will not
  * be set to zero.  Returns true if a uevent was sent, otherwise false.
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;
include/linux/blkdev.h +7 -1
index 6ede578dfbc6..ea8f95e0754c 100644
@@ -770,6 +770,8 @@ static inline sector_t get_start_sect(struct block_device *bdev)
 	return bdev->bd_start_sect;
 }
 
+void bdev_set_nr_sectors(struct block_device *bdev, sector_t sectors);
+
 static inline sector_t bdev_nr_sectors(struct block_device *bdev)
 {
 	return bdev->bd_nr_sectors;
@@ -780,6 +782,11 @@ static inline loff_t bdev_nr_bytes(struct block_device *bdev)
 	return (loff_t)bdev_nr_sectors(bdev) << SECTOR_SHIFT;
 }
 
+static inline void set_capacity(struct gendisk *disk, sector_t sectors)
+{
+	bdev_set_nr_sectors(disk->part0, sectors);
+}
+
 static inline sector_t get_capacity(struct gendisk *disk)
 {
 	return bdev_nr_sectors(disk->part0);
@@ -820,7 +827,6 @@ void unregister_blkdev(unsigned int major, const char *name);
 
 bool bdev_check_media_change(struct block_device *bdev);
 int __invalidate_device(struct block_device *bdev, bool kill_dirty);
-void set_capacity(struct gendisk *disk, sector_t size);
 
 #ifdef CONFIG_BLOCK_HOLDER_DEPRECATED
 int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
Signature · 1 line
2.40.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 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 turning set_capacity() into an inline function calling bdev_set_nr_sectors() and move bdev_set_nr_sectors() code to block/bdev.c instead of having this function in block/partitions/core.c.

---

Looks good.

-ck

#2 lore raw
In reply to Damien Le Moal
Quoted 6 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 turning set_capacity() into an inline function calling bdev_set_nr_sectors() and move bdev_set_nr_sectors() code to block/bdev.c instead of having this function in block/partitions/core.c.

I don't think bdev_set_nr_sectors should be exported. So given that all this is a slow path anyway, please keep set_capacity out of line as well and don't export bdev_set_nr_sectors.