Commit 5b08968c authored by Martin K. Petersen's avatar Martin K. Petersen Committed by Ben Hutchings

sd: Fix excessive capacity printing on devices with blocks bigger than 512 bytes

commit f08bb1e0 upstream.

During revalidate we check whether device capacity has changed before we
decide whether to output disk information or not.

The check for old capacity failed to take into account that we scaled
sdkp->capacity based on the reported logical block size. And therefore
the capacity test would always fail for devices with sectors bigger than
512 bytes and we would print several copies of the same discovery
information.

Avoid scaling sdkp->capacity and instead adjust the value on the fly
when setting the block device capacity and generating fake C/H/S
geometry.
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Reported-by: default avatarHannes Reinecke <hare@suse.de>
Reviewed-by: default avatarHannes Reinicke <hare@suse.de>
Reviewed-by: default avatarEwan Milne <emilne@redhat.com>
Signed-off-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
[bwh: Backported to 3.16:
 - logical_to_sectors() is a new function
 - Adjust context]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent 68833f49
...@@ -1230,18 +1230,19 @@ static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo) ...@@ -1230,18 +1230,19 @@ static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk); struct scsi_disk *sdkp = scsi_disk(bdev->bd_disk);
struct scsi_device *sdp = sdkp->device; struct scsi_device *sdp = sdkp->device;
struct Scsi_Host *host = sdp->host; struct Scsi_Host *host = sdp->host;
sector_t capacity = logical_to_sectors(sdp, sdkp->capacity);
int diskinfo[4]; int diskinfo[4];
/* default to most commonly used values */ /* default to most commonly used values */
diskinfo[0] = 0x40; /* 1 << 6 */ diskinfo[0] = 0x40; /* 1 << 6 */
diskinfo[1] = 0x20; /* 1 << 5 */ diskinfo[1] = 0x20; /* 1 << 5 */
diskinfo[2] = sdkp->capacity >> 11; diskinfo[2] = capacity >> 11;
/* override with calculated, extended default, or driver values */ /* override with calculated, extended default, or driver values */
if (host->hostt->bios_param) if (host->hostt->bios_param)
host->hostt->bios_param(sdp, bdev, sdkp->capacity, diskinfo); host->hostt->bios_param(sdp, bdev, capacity, diskinfo);
else else
scsicam_bios_param(bdev, sdkp->capacity, diskinfo); scsicam_bios_param(bdev, capacity, diskinfo);
geo->heads = diskinfo[0]; geo->heads = diskinfo[0];
geo->sectors = diskinfo[1]; geo->sectors = diskinfo[1];
...@@ -2220,14 +2221,6 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer) ...@@ -2220,14 +2221,6 @@ sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)
sdp->use_16_for_rw = (sdkp->capacity > 0xffffffff); sdp->use_16_for_rw = (sdkp->capacity > 0xffffffff);
/* Rescale capacity to 512-byte units */
if (sector_size == 4096)
sdkp->capacity <<= 3;
else if (sector_size == 2048)
sdkp->capacity <<= 2;
else if (sector_size == 1024)
sdkp->capacity <<= 1;
blk_queue_physical_block_size(sdp->request_queue, blk_queue_physical_block_size(sdp->request_queue,
sdkp->physical_block_size); sdkp->physical_block_size);
sdkp->device->sector_size = sector_size; sdkp->device->sector_size = sector_size;
...@@ -2751,7 +2744,7 @@ static int sd_revalidate_disk(struct gendisk *disk) ...@@ -2751,7 +2744,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
blk_queue_flush(sdkp->disk->queue, flush); blk_queue_flush(sdkp->disk->queue, flush);
set_capacity(disk, sdkp->capacity); set_capacity(disk, logical_to_sectors(sdp, sdkp->capacity));
sd_config_write_same(sdkp); sd_config_write_same(sdkp);
kfree(buffer); kfree(buffer);
......
...@@ -63,7 +63,7 @@ struct scsi_disk { ...@@ -63,7 +63,7 @@ struct scsi_disk {
struct device dev; struct device dev;
struct gendisk *disk; struct gendisk *disk;
atomic_t openers; atomic_t openers;
sector_t capacity; /* size in 512-byte sectors */ sector_t capacity; /* size in logical blocks */
u32 max_ws_blocks; u32 max_ws_blocks;
u32 max_unmap_blocks; u32 max_unmap_blocks;
u32 unmap_granularity; u32 unmap_granularity;
...@@ -142,6 +142,11 @@ static inline int scsi_medium_access_command(struct scsi_cmnd *scmd) ...@@ -142,6 +142,11 @@ static inline int scsi_medium_access_command(struct scsi_cmnd *scmd)
return 0; return 0;
} }
static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blocks)
{
return blocks << (ilog2(sdev->sector_size) - 9);
}
/* /*
* A DIF-capable target device can be formatted with different * A DIF-capable target device can be formatted with different
* protection schemes. Currently 0 through 3 are defined: * protection schemes. Currently 0 through 3 are defined:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment