aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/block/virtio_blk.c
diff options
context:
space:
mode:
authorRyan Harper <ryanh@us.ibm.com>2010-06-23 23:19:57 -0400
committerRusty Russell <rusty@rustcorp.com.au>2010-08-04 23:35:30 -0400
commita5eb9e4ff18a33e43557d44b205f953b0c1efade (patch)
tree5b7a1bf70763c08ded665a341ea3cf2d9def9ef6 /drivers/block/virtio_blk.c
parent10bc310c27af1ed358e62351e7ac1d0110c3da27 (diff)
virtio_blk: Add 'serial' attribute to virtio-blk devices (v2)
Create a new attribute for virtio-blk devices that will fetch the serial number of the block device. This attribute can be used by udev to create disk/by-id symlinks for devices that don't have a UUID (filesystem) associated with them. ATA_IDENTIFY strings are special in that they can be up to 20 chars long and aren't required to be nul-terminated. The buffer is also zero-padded meaning that if the serial is 19 chars or less that we get a nul-terminated string. When copying this value into a string buffer, we must be careful to copy up to the nul (if it present) and only 20 if it is longer and not to attempt to nul terminate; this isn't needed. Changes since v1: - Added BUILD_BUG_ON() for PAGE_SIZE check - Removed min() since BUILD_BUG_ON() handles the check - Replaced serial_sysfs() by copying id directly to buffer Signed-off-by: Ryan Harper <ryanh@us.ibm.com> Signed-off-by: john cooper <john.cooper@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'drivers/block/virtio_blk.c')
-rw-r--r--drivers/block/virtio_blk.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 2d6191aa5948..7a93b3f68849 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -281,6 +281,27 @@ static int index_to_minor(int index)
281 return index << PART_BITS; 281 return index << PART_BITS;
282} 282}
283 283
284static ssize_t virtblk_serial_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
287 struct gendisk *disk = dev_to_disk(dev);
288 int err;
289
290 /* sysfs gives us a PAGE_SIZE buffer */
291 BUILD_BUG_ON(PAGE_SIZE < VIRTIO_BLK_ID_BYTES);
292
293 buf[VIRTIO_BLK_ID_BYTES] = '\0';
294 err = virtblk_get_id(disk, buf);
295 if (!err)
296 return strlen(buf);
297
298 if (err == -EIO) /* Unsupported? Make it empty. */
299 return 0;
300
301 return err;
302}
303DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL);
304
284static int __devinit virtblk_probe(struct virtio_device *vdev) 305static int __devinit virtblk_probe(struct virtio_device *vdev)
285{ 306{
286 struct virtio_blk *vblk; 307 struct virtio_blk *vblk;
@@ -465,8 +486,15 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
465 486
466 487
467 add_disk(vblk->disk); 488 add_disk(vblk->disk);
489 err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial);
490 if (err)
491 goto out_del_disk;
492
468 return 0; 493 return 0;
469 494
495out_del_disk:
496 del_gendisk(vblk->disk);
497 blk_cleanup_queue(vblk->disk->queue);
470out_put_disk: 498out_put_disk:
471 put_disk(vblk->disk); 499 put_disk(vblk->disk);
472out_mempool: 500out_mempool: