Skip to contentSkip to conversation

null_blk: fix wrong capacity when bs is not 512 bytes

PATCH In review Matias Bjørling 2 participants 2 messages Aug 31, 2015 – Sep 2, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 null_blk: fix wrong capacity when bs is not 512 bytes

set_capacity() sets device's capacity using 512 bytes sectors. null_blk calculates the number of sectors by size / bs, which set_capacity is called with. This led to null_blk exposing the wrong number of sectors when bs is not 512 bytes.

---
 drivers/block/null_blk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/block/null_blk.c +1 -2
index 6f9b753..bf8f764 100644
@@ -588,8 +588,7 @@ static int null_add_dev(void)
 	blk_queue_physical_block_size(nullb->q, bs);
 
 	size = gb * 1024 * 1024 * 1024ULL;
-	sector_div(size, bs);
-	set_capacity(disk, size);
+	set_capacity(disk, size >> 9);
 
 	disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
 	disk->major		= null_major;
Signature · 1 line
2.1.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Matias Bjørling

set_capacity() sets device's capacity using 512 bytes sectors. null_blk calculates the number of sectors by size / bs, which set_capacity is called with. This led to null_blk exposing the wrong number of sectors when bs is not 512 bytes.

---
 drivers/block/null_blk.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/block/null_blk.c +0 -0
index 6f9b753..bf8f764 100644

drivers/block/null_blk.c static int null_add_dev(void) · line 594

Quoted 9 lines
drivers/block/null_blk.c +1 -2
@@ -588,8 +588,7 @@ static int null_add_dev(void)
 	blk_queue_physical_block_size(nullb->q, bs);
 
 	size = gb * 1024 * 1024 * 1024ULL;
-	sector_div(size, bs);
-	set_capacity(disk, size);
+	set_capacity(disk, size >> 9);
 
 	disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
 	disk->major		= null_major;

Looks correct.