2021-05-21 05:53:35

by Matteo Croce

[permalink] [raw]
Subject: [PATCH v2 0/6] block: add a sequence number to disks

From: Matteo Croce <[email protected]>

With this series a monotonically increasing number is added to disks,
precisely in the genhd struct, and it's exported in sysfs and uevent.

This helps the userspace correlate events for devices that reuse the
same device, like loop.

The first patch is the core one, the 2..4 expose the information in
different ways, the 5th increases the seqnum on media change and
the last one increases the sequence number for loop devices upon
attach, detach or reconfigure.

If merged, this feature will immediately used by the userspace:
https://github.com/systemd/systemd/issues/17469#issuecomment-762919781

v1 -> v2:
- increase seqnum on media change
- increase on loop detach

Matteo Croce (6):
block: add disk sequence number
block: add ioctl to read the disk sequence number
block: refactor sysfs code
block: export diskseq in sysfs
block: increment sequence number
loop: increment sequence number

Documentation/ABI/testing/sysfs-block | 12 +++++++
block/genhd.c | 46 ++++++++++++++++++++++++---
block/ioctl.c | 2 ++
drivers/block/loop.c | 5 +++
include/linux/genhd.h | 2 ++
include/uapi/linux/fs.h | 1 +
6 files changed, 64 insertions(+), 4 deletions(-)

--
2.31.1


2021-05-21 05:53:48

by Matteo Croce

[permalink] [raw]
Subject: [PATCH v2 5/6] block: increment sequence number

From: Matteo Croce <[email protected]>

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 <[email protected]>
---
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
--
2.31.1

2021-05-21 05:53:48

by Matteo Croce

[permalink] [raw]
Subject: [PATCH v2 4/6] block: export diskseq in sysfs

From: Matteo Croce <[email protected]>

Add a new sysfs handle to export the new diskseq value.
Place it in <sysfs>/block/<disk>/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 <[email protected]>
---
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/<disk>/diskseq
+Date: February 2021
+Contact: Matteo Croce <[email protected]>
+Description:
+ The /sys/block/<disk>/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/<disk>/<part>/stat
Date: February 2008
Contact: Jerome Marchand <[email protected]>
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,
};

--
2.31.1

2021-06-08 14:35:11

by Matteo Croce

[permalink] [raw]
Subject: Re: [PATCH v2 0/6] block: add a sequence number to disks

On Thu, May 20, 2021 at 3:56 PM Matteo Croce <[email protected]> wrote:
>
> From: Matteo Croce <[email protected]>
>
> With this series a monotonically increasing number is added to disks,
> precisely in the genhd struct, and it's exported in sysfs and uevent.
>
> This helps the userspace correlate events for devices that reuse the
> same device, like loop.
>
> The first patch is the core one, the 2..4 expose the information in
> different ways, the 5th increases the seqnum on media change and
> the last one increases the sequence number for loop devices upon
> attach, detach or reconfigure.
>
> If merged, this feature will immediately used by the userspace:
> https://github.com/systemd/systemd/issues/17469#issuecomment-762919781
>

Hi Christoph,

I just noticed that the series doesn't apply anymore. Before
refreshing it, I wish to know what are your opinion on this one, as
nobody expressed one on latest submission.

--
per aspera ad upstream