From patchwork Fri Jun 18 18:38:02 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Harper X-Patchwork-Id: 56211 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id CA6731007D4 for ; Sat, 19 Jun 2010 04:40:14 +1000 (EST) Received: from localhost ([127.0.0.1]:55606 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPgU7-00008s-Ff for incoming@patchwork.ozlabs.org; Fri, 18 Jun 2010 14:40:11 -0400 Received: from [140.186.70.92] (port=40465 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OPgSV-0007d2-J5 for qemu-devel@nongnu.org; Fri, 18 Jun 2010 14:38:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OPgSO-0006Vi-Fy for qemu-devel@nongnu.org; Fri, 18 Jun 2010 14:38:26 -0400 Received: from e32.co.us.ibm.com ([32.97.110.150]:58741) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OPgSO-0006VV-AU for qemu-devel@nongnu.org; Fri, 18 Jun 2010 14:38:24 -0400 Received: from d03relay03.boulder.ibm.com (d03relay03.boulder.ibm.com [9.17.195.228]) by e32.co.us.ibm.com (8.14.4/8.13.1) with ESMTP id o5IIUmJ7025346 for ; Fri, 18 Jun 2010 12:30:48 -0600 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay03.boulder.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o5IIc6a5024766 for ; Fri, 18 Jun 2010 12:38:16 -0600 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id o5IIc508023923 for ; Fri, 18 Jun 2010 12:38:06 -0600 Received: from localhost.localdomain (symmetry-009053041164.austin.ibm.com [9.53.41.164]) by d03av03.boulder.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id o5IIc4rC023890; Fri, 18 Jun 2010 12:38:05 -0600 From: Ryan Harper To: virtualization@lists.linux-foundation.org Date: Fri, 18 Jun 2010 13:38:02 -0500 Message-Id: <1276886283-1571-1-git-send-email-ryanh@us.ibm.com> X-Mailer: git-send-email 1.6.3.3 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: john cooper , Rusty Russell , qemu-devel@nongnu.org, kvm@vger.kernel.org, Ryan Harper Subject: [Qemu-devel] [PATCH 1/2] Add 'serial' attribute to virtio-blk devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 NULL-terminated. The buffer is also zero-padded meaning that if the serial is 19 chars or less that we get a NULL terminated string. When copying this value into a string buffer, we must be careful to copy up to the NULL (if it present) and only 20 if it is longer and not to attempt to NULL terminate; this isn't needed. Signed-off-by: Ryan Harper Signed-off-by: john cooper --- drivers/block/virtio_blk.c | 32 ++++++++++++++++++++++++++++++++ 1 files changed, 32 insertions(+), 0 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index 258bc2a..f1ef26f 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -281,6 +281,31 @@ static int index_to_minor(int index) return index << PART_BITS; } +/* Copy serial number from *s to *d. Copy operation terminates on either + * encountering a nul in *s or after n bytes have been copied, whichever + * occurs first. *d is not forcibly nul terminated. Return # of bytes copied. + */ +static inline int serial_sysfs(char *d, char *s, int n) +{ + char *di = d; + + while (*s && n--) + *d++ = *s++; + return d - di; +} + +static ssize_t virtblk_serial_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct gendisk *disk = dev_to_disk(dev); + char id_str[VIRTIO_BLK_ID_BYTES]; + + if (IS_ERR(virtblk_get_id(disk, id_str))) + return 0; + return serial_sysfs(buf, id_str, min(VIRTIO_BLK_ID_BYTES, PAGE_SIZE)); +} +DEVICE_ATTR(serial, S_IRUGO, virtblk_serial_show, NULL); + static int __devinit virtblk_probe(struct virtio_device *vdev) { struct virtio_blk *vblk; @@ -445,8 +470,15 @@ static int __devinit virtblk_probe(struct virtio_device *vdev) add_disk(vblk->disk); + err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial); + if (err) + goto out_del_disk; + return 0; +out_del_disk: + del_gendisk(vblk->disk); + blk_cleanup_queue(vblk->disk->queue); out_put_disk: put_disk(vblk->disk); out_mempool: