Received: by 2002:a4a:311b:0:0:0:0:0 with SMTP id k27-v6csp4092111ooa; Tue, 14 Aug 2018 00:35:29 -0700 (PDT) X-Google-Smtp-Source: AA+uWPwMSwf5VkBzPbMyY9JZMILdX76yv06e4PN1ymsGmumrfuvKneWP5+8nzI4LGjyfcr9CKU/M X-Received: by 2002:a63:2647:: with SMTP id m68-v6mr20034087pgm.60.1534232129875; Tue, 14 Aug 2018 00:35:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534232129; cv=none; d=google.com; s=arc-20160816; b=BAMTWVMG9tTyzCjKY2e2p5E0imVjHOgTDypwzttDtUCOeerEGxWygknmlW0IZX2G7T Yj79TuWqAVZ95aNlf50UbT2aIUV++UsA83sTf5jSzI7yQXTiHOjLTK8Q5lflvXN5Tblp mIVaxWIM6lI0iQNzIfSyXNO2yuCG8CnZAHERQrUzGHYJaNJDdlz6uUjF/Pl+/U9sVjrO qIhI04Xh/qdG245OyvWaPZYW7Uw+77asmt1arBdpZiyVA/E8q8KoB+H9cNjUEd3Knrga lrOSnW1XN1vqOdsZMbD9KHelv2J3NCA/zGp6xdoow53QX/iIQ+kdJUrQtsNinrNl0334 1s3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=cDCxlSStrybDiJ+4uy47iTYPbowx7b5TZGwBUgN8fJ8=; b=xM0ZOmLBblir34Z2hOQX2ML67FuCNJbAWYJiHR5h1Jnoso48UA29VKcIsRFmwq1XiW MJeNdifRiX/xdk0Bc8s7Sz2oac2acPyF8MyX1MSn/DdoC72WY32WkF86NPm8mfIw+Efm fVdDLyxyd95xDbeIaE/fwbJ/DWcdLxBmNBqJc6bWl/gfUvyMWCOrMUk5PDl5mgxOiqAN IsVtIRDBTtDYz60CkiDQdR8Jw5bveFBXKh35BFzgwx5LmyVIzFwi5I9bCxWzjrLYH9sx KkDSwNinQ1HyFR/tj7z6WQmbkmIc+RzvvBoQypBuuErDB+AneQNYyiVK98CpiW9REdXl QHkg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 24-v6si19671867pgx.314.2018.08.14.00.35.15; Tue, 14 Aug 2018 00:35:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732059AbeHNKTK (ORCPT + 99 others); Tue, 14 Aug 2018 06:19:10 -0400 Received: from mx2.suse.de ([195.135.220.15]:54286 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731341AbeHNKTJ (ORCPT ); Tue, 14 Aug 2018 06:19:09 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 29981AF5C; Tue, 14 Aug 2018 07:33:10 +0000 (UTC) From: Hannes Reinecke To: Jens Axboe Cc: Christoph Hellwig , Sagi Grimberg , Keith Busch , Bart van Assche , linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, Linux Kernel Mailinglist , Hannes Reinecke , Hannes Reinecke Subject: [PATCH 5/5] virtio-blk: modernize sysfs attribute creation Date: Tue, 14 Aug 2018 09:33:05 +0200 Message-Id: <20180814073305.87255-6-hare@suse.de> X-Mailer: git-send-email 2.12.3 In-Reply-To: <20180814073305.87255-1-hare@suse.de> References: <20180814073305.87255-1-hare@suse.de> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Use new-style DEVICE_ATTR_RO/DEVICE_ATTR_RW to create the sysfs attributes and register the disk with default sysfs attribute groups. Signed-off-by: Hannes Reinecke Reviewed-by: Christoph Hellwig Acked-by: Michael S. Tsirkin --- drivers/block/virtio_blk.c | 68 ++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index fe80560000a1..086c6bb12baa 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -351,8 +351,8 @@ static int minor_to_index(int minor) return minor >> PART_BITS; } -static ssize_t virtblk_serial_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t serial_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct gendisk *disk = dev_to_disk(dev); int err; @@ -371,7 +371,7 @@ static ssize_t virtblk_serial_show(struct device *dev, return err; } -static DEVICE_ATTR(serial, 0444, virtblk_serial_show, NULL); +static DEVICE_ATTR_RO(serial); /* The queue's logical block size must be set before calling this */ static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) @@ -545,8 +545,8 @@ static const char *const virtblk_cache_types[] = { }; static ssize_t -virtblk_cache_type_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +cache_type_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) { struct gendisk *disk = dev_to_disk(dev); struct virtio_blk *vblk = disk->private_data; @@ -564,8 +564,7 @@ virtblk_cache_type_store(struct device *dev, struct device_attribute *attr, } static ssize_t -virtblk_cache_type_show(struct device *dev, struct device_attribute *attr, - char *buf) +cache_type_show(struct device *dev, struct device_attribute *attr, char *buf) { struct gendisk *disk = dev_to_disk(dev); struct virtio_blk *vblk = disk->private_data; @@ -575,12 +574,38 @@ virtblk_cache_type_show(struct device *dev, struct device_attribute *attr, return snprintf(buf, 40, "%s\n", virtblk_cache_types[writeback]); } -static const struct device_attribute dev_attr_cache_type_ro = - __ATTR(cache_type, 0444, - virtblk_cache_type_show, NULL); -static const struct device_attribute dev_attr_cache_type_rw = - __ATTR(cache_type, 0644, - virtblk_cache_type_show, virtblk_cache_type_store); +static DEVICE_ATTR_RW(cache_type); + +static struct attribute *virtblk_attrs[] = { + &dev_attr_serial.attr, + &dev_attr_cache_type.attr, + NULL, +}; + +static umode_t virtblk_attrs_are_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct device *dev = container_of(kobj, struct device, kobj); + struct gendisk *disk = dev_to_disk(dev); + struct virtio_blk *vblk = disk->private_data; + struct virtio_device *vdev = vblk->vdev; + + if (a == &dev_attr_cache_type.attr && + !virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) + return S_IRUGO; + + return a->mode; +} + +static const struct attribute_group virtblk_attr_group = { + .attrs = virtblk_attrs, + .is_visible = virtblk_attrs_are_visible, +}; + +static const struct attribute_group *virtblk_attr_groups[] = { + &virtblk_attr_group, + NULL, +}; static int virtblk_init_request(struct blk_mq_tag_set *set, struct request *rq, unsigned int hctx_idx, unsigned int numa_node) @@ -780,24 +805,9 @@ static int virtblk_probe(struct virtio_device *vdev) virtblk_update_capacity(vblk, false); virtio_device_ready(vdev); - device_add_disk(&vdev->dev, vblk->disk, NULL); - err = device_create_file(disk_to_dev(vblk->disk), &dev_attr_serial); - if (err) - goto out_del_disk; - - if (virtio_has_feature(vdev, VIRTIO_BLK_F_CONFIG_WCE)) - err = device_create_file(disk_to_dev(vblk->disk), - &dev_attr_cache_type_rw); - else - err = device_create_file(disk_to_dev(vblk->disk), - &dev_attr_cache_type_ro); - if (err) - goto out_del_disk; + device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups); return 0; -out_del_disk: - del_gendisk(vblk->disk); - blk_cleanup_queue(vblk->disk->queue); out_free_tags: blk_mq_free_tag_set(&vblk->tag_set); out_put_disk: -- 2.12.3