Skip to content

Commit daf2a50

Browse files
stefanhaRHmstsirkin
authored andcommitted
virtio_blk: print capacity at probe time
Print the capacity of the block device when the driver is probed. Many users expect this since SCSI disks (sd) do it. Moreover, kernel dmesg output is the primary source of troubleshooting information so it's helpful to include the disk size there. The capacity is already printed by virtio_blk when a resize event occurs. Extract the code and reuse it from virtblk_probe(). This patch also adds the block device name to the message so it can be correlated with a specific device: virtio_blk virtio0: [vda] 20971520 512-byte logical blocks (10.7 GB/10.0 GiB) Cc: Rodrigo A B Freire <rfreire@redhat.com> Cc: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 7b95fec commit daf2a50

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

drivers/block/virtio_blk.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,12 @@ static ssize_t virtblk_serial_show(struct device *dev,
373373

374374
static DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
375375

376-
static void virtblk_config_changed_work(struct work_struct *work)
376+
/* The queue's logical block size must be set before calling this */
377+
static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize)
377378
{
378-
struct virtio_blk *vblk =
379-
container_of(work, struct virtio_blk, config_work);
380379
struct virtio_device *vdev = vblk->vdev;
381380
struct request_queue *q = vblk->disk->queue;
382381
char cap_str_2[10], cap_str_10[10];
383-
char *envp[] = { "RESIZE=1", NULL };
384382
unsigned long long nblocks;
385383
u64 capacity;
386384

@@ -402,13 +400,24 @@ static void virtblk_config_changed_work(struct work_struct *work)
402400
STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
403401

404402
dev_notice(&vdev->dev,
405-
"new size: %llu %d-byte logical blocks (%s/%s)\n",
403+
"[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
404+
vblk->disk->disk_name,
405+
resize ? "new size: " : "",
406406
nblocks,
407407
queue_logical_block_size(q),
408408
cap_str_10,
409409
cap_str_2);
410410

411411
set_capacity(vblk->disk, capacity);
412+
}
413+
414+
static void virtblk_config_changed_work(struct work_struct *work)
415+
{
416+
struct virtio_blk *vblk =
417+
container_of(work, struct virtio_blk, config_work);
418+
char *envp[] = { "RESIZE=1", NULL };
419+
420+
virtblk_update_capacity(vblk, true);
412421
revalidate_disk(vblk->disk);
413422
kobject_uevent_env(&disk_to_dev(vblk->disk)->kobj, KOBJ_CHANGE, envp);
414423
}
@@ -621,7 +630,6 @@ static int virtblk_probe(struct virtio_device *vdev)
621630
struct request_queue *q;
622631
int err, index;
623632

624-
u64 cap;
625633
u32 v, blk_size, sg_elems, opt_io_size;
626634
u16 min_io_size;
627635
u8 physical_block_exp, alignment_offset;
@@ -719,17 +727,6 @@ static int virtblk_probe(struct virtio_device *vdev)
719727
if (virtio_has_feature(vdev, VIRTIO_BLK_F_RO))
720728
set_disk_ro(vblk->disk, 1);
721729

722-
/* Host must always specify the capacity. */
723-
virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
724-
725-
/* If capacity is too big, truncate with warning. */
726-
if ((sector_t)cap != cap) {
727-
dev_warn(&vdev->dev, "Capacity %llu too large: truncating\n",
728-
(unsigned long long)cap);
729-
cap = (sector_t)-1;
730-
}
731-
set_capacity(vblk->disk, cap);
732-
733730
/* We can handle whatever the host told us to handle. */
734731
blk_queue_max_segments(q, vblk->sg_elems-2);
735732

@@ -780,6 +777,7 @@ static int virtblk_probe(struct virtio_device *vdev)
780777
if (!err && opt_io_size)
781778
blk_queue_io_opt(q, blk_size * opt_io_size);
782779

780+
virtblk_update_capacity(vblk, false);
783781
virtio_device_ready(vdev);
784782

785783
device_add_disk(&vdev->dev, vblk->disk);

0 commit comments

Comments
 (0)