From patchwork Thu May 20 13:56:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270361 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66930C433B4 for ; Thu, 20 May 2021 13:59:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 267BF613B0 for ; Thu, 20 May 2021 13:59:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235147AbhETOBL (ORCPT ); Thu, 20 May 2021 10:01:11 -0400 Received: from mail-ed1-f46.google.com ([209.85.208.46]:34639 "EHLO mail-ed1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239454AbhETN6w (ORCPT ); Thu, 20 May 2021 09:58:52 -0400 Received: by mail-ed1-f46.google.com with SMTP id w12so11764318edx.1; Thu, 20 May 2021 06:57:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wQqRHdN+0c7ElbyCTxmJqH+JHoz0aWq7OJLTydC/kzE=; b=ovxUwqYrmAx7cjvm+xf0OHZEj04cuEUyxdxdaKiGbQKTFSEyHJfIVvoR7o4RHFYWtA lO7IAD7eltUuSQCOsqcGgGG02BxR4U2OLj/7OeHDCv7i0u+gdpz0OTAfxGpa80QrX6FB GGhQBAHuKKxqMQPTRBg2c83eY3eD9P+Zlem/5H8mfnsmpOaij3HVHJ+XhCDuilkDI9O7 XGnxto1+Andv2fOiuBLhVGD4SHJpJH/rvTEKUJ3uYNGvhF2Coxxxr4cwBFiQBGLMuhBh rLSYz/fpi0aL+4DAKo2XGyuMmLUh0R7yOrGbM3dOz502M+zlZvCXVOT7ZTnHc8k7SxiY +l1g== X-Gm-Message-State: AOAM531Wr64gdtNSvC8hqcqI3FJXshq4aQpwiw+/kzu3VKLIOMxBSFod n4rvuPsbhfAQh4PZVOddDahiczo4APGDFmhH X-Google-Smtp-Source: ABdhPJzoj6FTTJt9HKupqD4mRwFqIf8csge354GCyewKcizvxvhSZw5Vy+jQjZaUrAz6w4hJu4ICJw== X-Received: by 2002:a50:cdd1:: with SMTP id h17mr5167523edj.178.1621519050008; Thu, 20 May 2021 06:57:30 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.57.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:57:29 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 1/6] block: add disk sequence number Date: Thu, 20 May 2021 15:56:17 +0200 Message-Id: <20210520135622.44625-2-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce Add a monotonically increasing number to disk devices. This number is put in the uevent so userspace can correlate events when a driver reuses a device, like cdrom or loop. $ udevadm info /sys/class/block/* |grep -e DEVNAME -e DISKSEQ E: DEVNAME=/dev/loop0 E: DISKSEQ=1 E: DEVNAME=/dev/loop1 E: DISKSEQ=2 E: DEVNAME=/dev/loop2 E: DISKSEQ=3 E: DEVNAME=/dev/loop3 E: DISKSEQ=4 E: DEVNAME=/dev/loop4 E: DISKSEQ=5 E: DEVNAME=/dev/loop5 E: DISKSEQ=6 E: DEVNAME=/dev/loop6 E: DISKSEQ=7 E: DEVNAME=/dev/loop7 E: DISKSEQ=8 E: DEVNAME=/dev/nvme0n1 E: DISKSEQ=9 E: DEVNAME=/dev/nvme0n1p1 E: DISKSEQ=9 E: DEVNAME=/dev/nvme0n1p2 E: DISKSEQ=9 E: DEVNAME=/dev/nvme0n1p3 E: DISKSEQ=9 E: DEVNAME=/dev/nvme0n1p4 E: DISKSEQ=9 E: DEVNAME=/dev/nvme0n1p5 E: DISKSEQ=9 E: DEVNAME=/dev/sda E: DISKSEQ=10 E: DEVNAME=/dev/sda1 E: DISKSEQ=10 E: DEVNAME=/dev/sda2 E: DISKSEQ=10 Signed-off-by: Matteo Croce --- block/genhd.c | 19 +++++++++++++++++++ include/linux/genhd.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/block/genhd.c b/block/genhd.c index 39ca97b0edc6..2c7e148fa944 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1138,8 +1138,17 @@ static void disk_release(struct device *dev) blk_put_queue(disk->queue); kfree(disk); } + +static int block_uevent(struct device *dev, struct kobj_uevent_env *env) +{ + struct gendisk *disk = dev_to_disk(dev); + + return add_uevent_var(env, "DISKSEQ=%llu", disk->diskseq); +} + struct class block_class = { .name = "block", + .dev_uevent = block_uevent, }; static char *block_devnode(struct device *dev, umode_t *mode, @@ -1313,6 +1322,8 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) disk_to_dev(disk)->class = &block_class; disk_to_dev(disk)->type = &disk_type; device_initialize(disk_to_dev(disk)); + inc_diskseq(disk); + return disk; out_destroy_part_tbl: @@ -1863,3 +1874,11 @@ static void disk_release_events(struct gendisk *disk) WARN_ON_ONCE(disk->ev && disk->ev->block != 1); kfree(disk->ev); } + +void inc_diskseq(struct gendisk *disk) +{ + static atomic64_t diskseq; + + disk->diskseq = atomic64_inc_return(&diskseq); +} +EXPORT_SYMBOL_GPL(inc_diskseq); diff --git a/include/linux/genhd.h b/include/linux/genhd.h index 7e9660ea967d..ec98b95c8279 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h @@ -167,6 +167,7 @@ struct gendisk { int node_id; struct badblocks *bb; struct lockdep_map lockdep_map; + u64 diskseq; }; /* @@ -307,6 +308,7 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev, #endif /* CONFIG_SYSFS */ extern struct rw_semaphore bdev_lookup_sem; +extern void inc_diskseq(struct gendisk *disk); dev_t blk_lookup_devt(const char *name, int partno); void blk_request_module(dev_t devt); From patchwork Thu May 20 13:56:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270363 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7966C43461 for ; Thu, 20 May 2021 13:59:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A438A6121E for ; Thu, 20 May 2021 13:59:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237971AbhETOBM (ORCPT ); Thu, 20 May 2021 10:01:12 -0400 Received: from mail-ed1-f42.google.com ([209.85.208.42]:45899 "EHLO mail-ed1-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243477AbhETN7t (ORCPT ); Thu, 20 May 2021 09:59:49 -0400 Received: by mail-ed1-f42.google.com with SMTP id a25so19487724edr.12; Thu, 20 May 2021 06:58:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aJu1+YCrkeg1VrMRtE/40B8yQYYhV/BJgPpoTp2RURs=; b=GPe9f7qMOcG4F3dWVeOSdEuh7uawc7xSej5EBKHRHdYCVNIYwmT1lpEcGQTCFX9MmN B08+pxGLxPNrIPpB/JDTqSsen9SX49qci9LSOXyEsN3N0zXA9aSQb5S0QFk8vSdQOGl9 zQXS2yur7ubxUKy9YD7NS5p3YMrVDC7clAFb9SSKkHEowXjdMo259zt5A5rus4hQtDbz asUu0z8MkX7HWd3SgpAexcUe5FUnNrWdwINwmHxj1/kfWNFlNJ1Y4X2KgRQY6a53xI5o o9wfuVSevLjVM2Y45a1TRiTendp1i//WLxKIRlF0dsd+cg1P2N4lO2P4+6Bd++xjZ5kV i3fw== X-Gm-Message-State: AOAM532dNHfuna/LNo4Alz3dRM33meRiEIhH3/NbHqpDFTZp1ku1vFod LW6HQ3YL8bajzhIact5EzSeRbGfFED4BIt2Q X-Google-Smtp-Source: ABdhPJxCY1xOrV4Ti2X0wSEBgusWXm8TrgBIxXsyRpVDxsMVDSdbwW2OmgmC+9XY5LhxS0DX4F7fNA== X-Received: by 2002:a05:6402:4414:: with SMTP id y20mr5312263eda.41.1621519101911; Thu, 20 May 2021 06:58:21 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:58:21 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 2/6] block: add ioctl to read the disk sequence number Date: Thu, 20 May 2021 15:56:18 +0200 Message-Id: <20210520135622.44625-3-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce Add a new BLKGETDISKSEQ ioctl which retrieves the disk sequence number from the genhd structure. # ./getdiskseq /dev/loop* /dev/loop0: 13 /dev/loop0p1: 13 /dev/loop0p2: 13 /dev/loop0p3: 13 /dev/loop1: 14 /dev/loop1p1: 14 /dev/loop1p2: 14 /dev/loop2: 5 /dev/loop3: 6 Signed-off-by: Matteo Croce --- block/ioctl.c | 2 ++ include/uapi/linux/fs.h | 1 + 2 files changed, 3 insertions(+) diff --git a/block/ioctl.c b/block/ioctl.c index 8ba1ed8defd0..32b339bbaf95 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -469,6 +469,8 @@ static int blkdev_common_ioctl(struct block_device *bdev, fmode_t mode, BLKDEV_DISCARD_SECURE); case BLKZEROOUT: return blk_ioctl_zeroout(bdev, mode, arg); + case BLKGETDISKSEQ: + return put_u64(argp, bdev->bd_disk->diskseq); case BLKREPORTZONE: return blkdev_report_zones_ioctl(bdev, mode, cmd, arg); case BLKRESETZONE: diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h index 4c32e97dcdf0..bdf7b404b3e7 100644 --- a/include/uapi/linux/fs.h +++ b/include/uapi/linux/fs.h @@ -184,6 +184,7 @@ struct fsxattr { #define BLKSECDISCARD _IO(0x12,125) #define BLKROTATIONAL _IO(0x12,126) #define BLKZEROOUT _IO(0x12,127) +#define BLKGETDISKSEQ _IOR(0x12,128,__u64) /* * A jump here: 130-136 are reserved for zoned block devices * (see uapi/linux/blkzoned.h) From patchwork Thu May 20 13:56:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45981C433ED for ; Thu, 20 May 2021 13:59:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 260BE613B0 for ; Thu, 20 May 2021 13:59:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238241AbhETOBO (ORCPT ); Thu, 20 May 2021 10:01:14 -0400 Received: from mail-ed1-f52.google.com ([209.85.208.52]:39595 "EHLO mail-ed1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243506AbhETN7x (ORCPT ); Thu, 20 May 2021 09:59:53 -0400 Received: by mail-ed1-f52.google.com with SMTP id h16so19511966edr.6; Thu, 20 May 2021 06:58:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lwhc0ld/Y5lpNE82ePg9vTLlPV/oOdevCYAnWYhtGwQ=; b=Hy0V/eZUwh43I65xoHtHhlU6twPrWIeMd77EmANNOqmkhUwbkcosjt0rSACZQQiDZJ LPqFKLHSkGt2NSH6EHfPQBEORJP26p9dhe7N3qhagW6roFGfY7bigGD0kuTj7S29kfWy xZ+hft38x6K8YWYU3Q2+lYTEszpd+CUtUnpKX/JZ+iaTZVNrrMeAF6RChnAmy4kH6nlK BA4AfgVYVXxiMLfhWctGM5RP+N5fQSSauYjTtDscLVxXDNKjmRQWYzC1bLeTvraBKmE/ phmBhnq2GwrXOikKksKavK64bAw4tosb95J17AwARyNOfTknCUv+SFF+VbCLxlj9cqaa BKog== X-Gm-Message-State: AOAM531ftPhH7a2sbo8xP4BLdT9sXHsBqSYgEwwJkUIraeKMyDAZFiSm RNQEcuCbhiEqFTwe+Pg8bQ2A17reW8wYF2yh X-Google-Smtp-Source: ABdhPJwRU5hToocffR1A+90qphzYej+eT6IPLmmzZgUUSwC7IqHZQ/d9AO9knzv2r7BI3mAx80kJZA== X-Received: by 2002:a50:9e63:: with SMTP id z90mr5201754ede.342.1621519109374; Thu, 20 May 2021 06:58:29 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.58.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:58:28 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 3/6] block: refactor sysfs code Date: Thu, 20 May 2021 15:56:19 +0200 Message-Id: <20210520135622.44625-4-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce Move the sysfs register code from a function named disk_add_events() to a new function named disk_add_sysfs(). Also, rename the attribute list with a more generic name than disk_events_attrs. This is a prerequisite patch to export diskseq in sysfs later. Signed-off-by: Matteo Croce --- block/genhd.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/block/genhd.c b/block/genhd.c index 2c7e148fa944..417dd5666be5 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -41,6 +41,7 @@ static void disk_alloc_events(struct gendisk *disk); static void disk_add_events(struct gendisk *disk); static void disk_del_events(struct gendisk *disk); static void disk_release_events(struct gendisk *disk); +static void disk_add_sysfs(struct gendisk *disk); void set_capacity(struct gendisk *disk, sector_t sectors) { @@ -562,6 +563,7 @@ static void __device_add_disk(struct device *parent, struct gendisk *disk, */ WARN_ON_ONCE(!blk_get_queue(disk->queue)); + disk_add_sysfs(disk); disk_add_events(disk); blk_integrity_add(disk); } @@ -1763,7 +1765,7 @@ static const DEVICE_ATTR(events_poll_msecs, 0644, disk_events_poll_msecs_show, disk_events_poll_msecs_store); -static const struct attribute *disk_events_attrs[] = { +static const struct attribute *disk_sysfs_attrs[] = { &dev_attr_events.attr, &dev_attr_events_async.attr, &dev_attr_events_poll_msecs.attr, @@ -1834,13 +1836,16 @@ static void disk_alloc_events(struct gendisk *disk) disk->ev = ev; } -static void disk_add_events(struct gendisk *disk) +static void disk_add_sysfs(struct gendisk *disk) { /* FIXME: error handling */ - if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0) + if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_sysfs_attrs) < 0) pr_warn("%s: failed to create sysfs files for events\n", disk->disk_name); +} +static void disk_add_events(struct gendisk *disk) +{ if (!disk->ev) return; @@ -1865,7 +1870,7 @@ static void disk_del_events(struct gendisk *disk) mutex_unlock(&disk_events_mutex); } - sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_events_attrs); + sysfs_remove_files(&disk_to_dev(disk)->kobj, disk_sysfs_attrs); } static void disk_release_events(struct gendisk *disk) From patchwork Thu May 20 13:56:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270367 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8DEBC43462 for ; Thu, 20 May 2021 13:59:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C0BA6613B4 for ; Thu, 20 May 2021 13:59:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239217AbhETOBO (ORCPT ); Thu, 20 May 2021 10:01:14 -0400 Received: from mail-ej1-f48.google.com ([209.85.218.48]:35681 "EHLO mail-ej1-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243534AbhETOAC (ORCPT ); Thu, 20 May 2021 10:00:02 -0400 Received: by mail-ej1-f48.google.com with SMTP id k14so21992470eji.2; Thu, 20 May 2021 06:58:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=M/6Asmzm9q2pjoZlswx9y5LN/G6/90yiJ/pdetWstA8=; b=V2LagRhlLpm95kxga/7nF3xvHwTZg769xIYp5tClrh4EtXQmCwl4e0/A23RFZrA21b 1eQO1bcgqEkCfFfnImYNE7h4R9DnUANxJ4UF2ExBfGpDfQfFw3DJm3dohdhDb1oVJ9Mf qr45QObuEuYQighxPy9/R1UhtTUqdj4DRBKToeEccPjm3YvXkZLjz3s+MTFpa/qsfUE3 4vhaebF7WPU5SUQobB7cdU/qtSTuVD3cwpkfhXYzA30hQFBYFWpVGj7Eo6/NrHfCiNMe rtE6te6twxAAyjMstL1dJLhd+9NmnKnQusLr4ufI1wxG0WaiQZlePYTOVeCr4+jmp0Uo WBFA== X-Gm-Message-State: AOAM532wBHucOjZgePL6ItykRRi1YF6RuCDaO8cJmoR4y84FymT2Z0qS 66tKncYnN711EAyjP++5HmYuw+vtGaT6ys9R X-Google-Smtp-Source: ABdhPJzx5F7gA6ZA2KHXr4jS8GvRbkdKkiESzKxrBiX0r/JrBnJWKAmUs+Zmu7yYfnL4SOSpoNF8vQ== X-Received: by 2002:a17:906:e096:: with SMTP id gh22mr4860444ejb.101.1621519118504; Thu, 20 May 2021 06:58:38 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.58.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:58:37 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 4/6] block: export diskseq in sysfs Date: Thu, 20 May 2021 15:56:20 +0200 Message-Id: <20210520135622.44625-5-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce Add a new sysfs handle to export the new diskseq value. Place it in /block//diskseq and document it. $ grep . /sys/class/block/*/diskseq /sys/class/block/loop0/diskseq:13 /sys/class/block/loop1/diskseq:14 /sys/class/block/loop2/diskseq:5 /sys/class/block/loop3/diskseq:6 /sys/class/block/ram0/diskseq:1 /sys/class/block/ram1/diskseq:2 /sys/class/block/vda/diskseq:7 Signed-off-by: Matteo Croce --- Documentation/ABI/testing/sysfs-block | 12 ++++++++++++ block/genhd.c | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-block b/Documentation/ABI/testing/sysfs-block index e34cdeeeb9d4..a0ed87386639 100644 --- a/Documentation/ABI/testing/sysfs-block +++ b/Documentation/ABI/testing/sysfs-block @@ -28,6 +28,18 @@ Description: For more details refer Documentation/admin-guide/iostats.rst +What: /sys/block//diskseq +Date: February 2021 +Contact: Matteo Croce +Description: + The /sys/block//diskseq files reports the disk + sequence number, which is a monotonically increasing + number assigned to every drive. + Some devices, like the loop device, refresh such number + every time the backing file is changed. + The value type is 64 bit unsigned. + + What: /sys/block///stat Date: February 2008 Contact: Jerome Marchand diff --git a/block/genhd.c b/block/genhd.c index 417dd5666be5..67519c034f9f 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1689,6 +1689,7 @@ static void disk_check_events(struct disk_events *ev, * events_async : list of events which can be detected w/o polling * (always empty, only for backwards compatibility) * events_poll_msecs : polling interval, 0: disable, -1: system default + * diskseq : disk sequence number, since boot */ static ssize_t __disk_events_show(unsigned int events, char *buf) { @@ -1759,16 +1760,26 @@ static ssize_t disk_events_poll_msecs_store(struct device *dev, return count; } +static ssize_t diskseq_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct gendisk *disk = dev_to_disk(dev); + + return sprintf(buf, "%llu\n", disk->diskseq); +} + static const DEVICE_ATTR(events, 0444, disk_events_show, NULL); static const DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL); static const DEVICE_ATTR(events_poll_msecs, 0644, disk_events_poll_msecs_show, disk_events_poll_msecs_store); +static const DEVICE_ATTR(diskseq, 0444, diskseq_show, NULL); static const struct attribute *disk_sysfs_attrs[] = { &dev_attr_events.attr, &dev_attr_events_async.attr, &dev_attr_events_poll_msecs.attr, + &dev_attr_diskseq.attr, NULL, }; From patchwork Thu May 20 13:56:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270369 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 906E8C43470 for ; Thu, 20 May 2021 13:59:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 555AB613CA for ; Thu, 20 May 2021 13:59:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240011AbhETOBQ (ORCPT ); Thu, 20 May 2021 10:01:16 -0400 Received: from mail-ed1-f51.google.com ([209.85.208.51]:40917 "EHLO mail-ed1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243546AbhETOAF (ORCPT ); Thu, 20 May 2021 10:00:05 -0400 Received: by mail-ed1-f51.google.com with SMTP id t3so19501101edc.7; Thu, 20 May 2021 06:58:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KtlDwB3yEpx8TeFOnWtic2oPG9XbfzOFhlQoKOiiSos=; b=L9+OgeYXZXOUAnxXmDaQ7TiacMKMfHybau048JM+ce6ZDq56WlmVRcAEH4C4UV2Scq ZMclq2iyogJs/0PIn105fN0XEeIj8tRDbPAmEN/7mvSoEgRdKboUtaE4BWix4Dtspb0O EJ45R+GBapfzT/J98XiCh1RVP698YHnjxamvqyesP8qFN8+yajWfxPiXIn8rJ434lVL1 Khr4/OClW7FCt+KU2La1J4PkpDNYXT/i0wEQmobv3yr3ccVokd6bvnkwIXBXTPJ9WSkq q0X/wyWtJFTtQF/dMo9vOggs4wPLTqQaSeafhbSoqPN9bJEh1lA3QKdddrfsVq8bX/ii EATQ== X-Gm-Message-State: AOAM5302Rh82lHi0KIjQGfLXMMBS2QYEvJZWpuc16kmLr53CyD4U6iKm X2UvDBSP6x156QDE9SRSXwyV4va0JVBna+sE X-Google-Smtp-Source: ABdhPJzyZajk10SLdiMnhCpzUy1ZNox8Rmvv7xIIn3LrC0A4+H10JVykOD0mYkXIzmX/MxKAg6XTJw== X-Received: by 2002:a05:6402:3511:: with SMTP id b17mr5084804edd.71.1621519122543; Thu, 20 May 2021 06:58:42 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.58.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:58:42 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 5/6] block: increment sequence number Date: Thu, 20 May 2021 15:56:21 +0200 Message-Id: <20210520135622.44625-6-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce Increment the disk sequence number when the media has changed, i.e. on DISK_EVENT_MEDIA_CHANGE event. $ cat /sys/class/block/sr0/diskseq 12 $ eject $ cat /sys/class/block/sr0/diskseq 22 Signed-off-by: Matteo Croce --- block/genhd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/block/genhd.c b/block/genhd.c index 67519c034f9f..5bc6b6c248c4 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -1666,6 +1666,9 @@ static void disk_check_events(struct disk_events *ev, spin_unlock_irq(&ev->lock); + if (events & DISK_EVENT_MEDIA_CHANGE) + inc_diskseq(disk); + /* * Tell userland about new events. Only the events listed in * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT From patchwork Thu May 20 13:56:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Croce X-Patchwork-Id: 12270371 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-21.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9055EC43460 for ; Thu, 20 May 2021 13:59:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3E8036121E for ; Thu, 20 May 2021 13:59:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233191AbhETOBQ (ORCPT ); Thu, 20 May 2021 10:01:16 -0400 Received: from mail-ej1-f50.google.com ([209.85.218.50]:44920 "EHLO mail-ej1-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243555AbhETOAM (ORCPT ); Thu, 20 May 2021 10:00:12 -0400 Received: by mail-ej1-f50.google.com with SMTP id lz27so25432235ejb.11; Thu, 20 May 2021 06:58:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BNRMBQhQwN37AuO48S7OAEO5Yl+NBlyK6oGX4b7ciAc=; b=b7fEJLE7R0Bd/TMm92ytFzbAdIm4qbz+ezMtV0V9XKOlFx/SJwhGfc9cXAXIFVFb6T 8Xd99a6xhpe+bcqxT8X+uuX60P2hGvhm8seXZOg4lPLBhRerKF1J8JueezubJuY4FOMV bAI9TUi+YpQNsBzx4kKFDTn9ud1seJ6ElmVGgsrhR2dasm2c/r7oEFvcXAhBPmrG3yPP IY73jevTfN/cnOqF142QmlKyjkwnLyBQaHGg+58CkqsR6ueT4XKYAM1vX2XQdLj1FZGn MsrH7Ji5q1PFnheYiaMbUjQvvyKIFxNs+7flgAv/zEMMEVMDoDrEElvvuWvxQBElu51F Ei7Q== X-Gm-Message-State: AOAM530dvHdmmCSdMdDZJA2Sz4d8+siXs5RJzivDpqJRVQ1DGV71nvtb 2Kbx1mQOhra1UwKDl4wu19SOTYLziiwReEWa X-Google-Smtp-Source: ABdhPJyHzut9drb/H7h7imcrAFp+JKTXdyjp0T1z+Zo0T0i9bbSpZ6R1X/0cCZTynG1TOiJvS3kEYg== X-Received: by 2002:a17:906:7842:: with SMTP id p2mr4721513ejm.487.1621519130267; Thu, 20 May 2021 06:58:50 -0700 (PDT) Received: from msft-t490s.teknoraver.net (net-5-94-253-60.cust.vodafonedsl.it. [5.94.253.60]) by smtp.gmail.com with ESMTPSA id 9sm1434492ejv.73.2021.05.20.06.58.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 May 2021 06:58:49 -0700 (PDT) From: Matteo Croce To: linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Lennart Poettering , Luca Boccassi , Jens Axboe , Alexander Viro , Damien Le Moal , Tejun Heo , =?utf-8?q?Javier_Gonz=C3=A1lez?= , Niklas Cassel , Johannes Thumshirn , Hannes Reinecke , Matthew Wilcox , Christoph Hellwig , JeffleXu Subject: [PATCH v2 6/6] loop: increment sequence number Date: Thu, 20 May 2021 15:56:22 +0200 Message-Id: <20210520135622.44625-7-mcroce@linux.microsoft.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520135622.44625-1-mcroce@linux.microsoft.com> References: <20210520135622.44625-1-mcroce@linux.microsoft.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org From: Matteo Croce On a very loaded system, if there are many events queued up from multiple attach/detach cycles, it's impossible to match them up with the LOOP_CONFIGURE or LOOP_SET_FD call, since we don't know where the position of our own association in the queue is[1]. Not even an empty uevent queue is a reliable indication that we already received the uevent we were waiting for, since with multi-partition block devices each partition's event is queued asynchronously and might be delivered later. Increment the disk sequence number when setting or changing the backing file, so the userspace knows which backing file generated the event: # udevadm monitor -kp |grep -e ^DEVNAME -e ^DISKSEQ & [1] 263 # losetup -fP 3part [ 12.309974] loop0: detected capacity change from 0 to 2048 DEVNAME=/dev/loop0 DISKSEQ=8 [ 12.360252] loop0: p1 p2 p3 DEVNAME=/dev/loop0 DISKSEQ=8 DEVNAME=/dev/loop0p1 DISKSEQ=8 DEVNAME=/dev/loop0p2 DISKSEQ=8 DEVNAME=/dev/loop0p3 DISKSEQ=8 # losetup -D DEVNAME=/dev/loop0 DISKSEQ=9 DEVNAME=/dev/loop0p1 DISKSEQ=9 DEVNAME=/dev/loop0p2 DISKSEQ=9 DEVNAME=/dev/loop0p3 DISKSEQ=9 # losetup -fP 2part [ 29.001344] loop0: detected capacity change from 0 to 2048 DEVNAME=/dev/loop0 DISKSEQ=10 [ 29.040226] loop0: p1 p2 DEVNAME=/dev/loop0 DISKSEQ=10 DEVNAME=/dev/loop0p1 DISKSEQ=10 DEVNAME=/dev/loop0p2 DISKSEQ=10 [1] https://github.com/systemd/systemd/issues/17469#issuecomment-762919781 Signed-off-by: Matteo Croce --- drivers/block/loop.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index d58d68f3c7cd..b187fe48d2ef 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -735,6 +735,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev, goto out_err; /* and ... switch */ + inc_diskseq(lo->lo_disk); blk_mq_freeze_queue(lo->lo_queue); mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask); lo->lo_backing_file = file; @@ -1123,6 +1124,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode, if (error) goto out_unlock; + inc_diskseq(lo->lo_disk); + if (!(file->f_mode & FMODE_WRITE) || !(mode & FMODE_WRITE) || !file->f_op->write_iter) lo->lo_flags |= LO_FLAGS_READ_ONLY; @@ -1223,6 +1226,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release) lo->lo_backing_file = NULL; spin_unlock_irq(&lo->lo_lock); + inc_diskseq(lo->lo_disk); + loop_release_xfer(lo); lo->transfer = NULL; lo->ioctl = NULL;