From c07186ec03e888bce8d7929554d4141db6e6824e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Sep 2022 16:18:53 +0900 Subject: blockdev-util: split-out block_device_is_whole_disk() No functional changes, just preparation for later commits. --- src/shared/blockdev-util.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index e5abb524d5..829abba675 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -20,6 +20,26 @@ #include "missing_magic.h" #include "parse-util.h" +static int block_device_is_whole_disk(sd_device *dev) { + const char *s; + int r; + + assert(dev); + + r = sd_device_get_subsystem(dev, &s); + if (r < 0) + return r; + + if (!streq(s, "block")) + return -ENOTBLK; + + r = sd_device_get_devtype(dev, &s); + if (r < 0) + return r; + + return streq(s, "disk"); +} + int block_get_whole_disk(dev_t d, dev_t *ret) { char p[SYS_BLOCK_PATH_MAX("/partition")]; _cleanup_free_ char *s = NULL; @@ -518,18 +538,11 @@ int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) { assert(dev); assert(ret); - r = sd_device_get_subsystem(dev, &s); + /* Refuse invocation on partition block device, insist on "whole" device */ + r = block_device_is_whole_disk(dev); if (r < 0) return r; - - if (!streq(s, "block")) - return -ENOTBLK; - - r = sd_device_get_devtype(dev, &s); - if (r < 0) - return r; - - if (!streq(s, "disk")) /* Refuse invocation on partition block device, insist on "whole" device */ + if (r == 0) return -EINVAL; r = sd_device_enumerator_new(&e); -- cgit v1.2.3 From 55a437f8b22ae5a4c6e3b349e86b40e105d48ee6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Sep 2022 16:25:40 +0900 Subject: blockdev-util: introduce block_device_get_whole_disk() --- src/shared/blockdev-util.c | 29 +++++++++++++++++++++++++++++ src/shared/blockdev-util.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index 829abba675..ff77a8e4ea 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -40,6 +40,35 @@ static int block_device_is_whole_disk(sd_device *dev) { return streq(s, "disk"); } +int block_device_get_whole_disk(sd_device *dev, sd_device **ret) { + int r; + + assert(dev); + assert(ret); + + /* Do not unref returned sd_device object. */ + + r = block_device_is_whole_disk(dev); + if (r < 0) + return r; + if (r == 0) { + r = sd_device_get_parent(dev, &dev); + if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */ + return -ENODEV; + if (r < 0) + return r; + + r = block_device_is_whole_disk(dev); + if (r < 0) + return r; + if (r == 0) + return -ENXIO; + } + + *ret = dev; + return 0; +} + int block_get_whole_disk(dev_t d, dev_t *ret) { char p[SYS_BLOCK_PATH_MAX("/partition")]; _cleanup_free_ char *s = NULL; diff --git a/src/shared/blockdev-util.h b/src/shared/blockdev-util.h index 550b2786d2..802d79394f 100644 --- a/src/shared/blockdev-util.h +++ b/src/shared/blockdev-util.h @@ -14,6 +14,8 @@ #define xsprintf_sys_block_path(buf, suffix, devno) \ xsprintf(buf, "/sys/dev/block/%u:%u%s", major(devno), minor(devno), strempty(suffix)) +int block_device_get_whole_disk(sd_device *dev, sd_device **ret); + int block_get_whole_disk(dev_t d, dev_t *ret); int block_get_originating(dev_t d, dev_t *ret); -- cgit v1.2.3 From 5a27af52c755c7c76f8b47046903a2cdd64aaf7b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Sep 2022 22:48:53 +0900 Subject: blockdev-util: change return value when a partition device is passed to partition_enumerator_new() To make it consistent what returned by block_device_get_whole_disk(). --- src/shared/blockdev-util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index ff77a8e4ea..ab3b5ce263 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -572,7 +572,7 @@ int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) { if (r < 0) return r; if (r == 0) - return -EINVAL; + return -ENXIO; /* return a recognizable error */ r = sd_device_enumerator_new(&e); if (r < 0) -- cgit v1.2.3 From 8f71534e06d40312371fa80d932783e1f937bc59 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Sep 2022 13:24:12 +0900 Subject: udev: use block_device_get_whole_disk() This should not change anything effectively. --- src/udev/udevd.c | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 20a9a8cdbe..9900a9363e 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -473,13 +473,6 @@ static int device_get_whole_disk(sd_device *dev, sd_device **ret_device, const c if (device_for_action(dev, SD_DEVICE_REMOVE)) goto irrelevant; - r = sd_device_get_subsystem(dev, &val); - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get subsystem: %m"); - - if (!streq(val, "block")) - goto irrelevant; - r = sd_device_get_sysname(dev, &val); if (r < 0) return log_device_debug_errno(dev, r, "Failed to get sysname: %m"); @@ -493,16 +486,13 @@ static int device_get_whole_disk(sd_device *dev, sd_device **ret_device, const c if (STARTSWITH_SET(val, "dm-", "md", "drbd")) goto irrelevant; - r = sd_device_get_devtype(dev, &val); - if (r < 0 && r != -ENOENT) - return log_device_debug_errno(dev, r, "Failed to get devtype: %m"); - if (r >= 0 && streq(val, "partition")) { - r = sd_device_get_parent(dev, &dev); - if (r == -ENOENT) /* The device may be already removed. */ - goto irrelevant; - if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get parent device: %m"); - } + r = block_device_get_whole_disk(dev, &dev); + if (IN_SET(r, + -ENOTBLK, /* The device is not a block device. */ + -ENODEV /* The whole disk device was not found, it may already be removed. */)) + goto irrelevant; + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); r = sd_device_get_devname(dev, &val); if (r == -ENOENT) -- cgit v1.2.3 From d50c55bd435a5c338665da1cb83df88b49b96eff Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 18 Sep 2022 23:32:21 +0900 Subject: udev: do not ignore -ENOENT from sd_device_get_devname() for block device We already checked that the sd_device object 'dev' is for a whole block device. So, -ENOENT should not be triggeered here, and if it is, there exists something spurious. Hence we should not ignore the failure. --- src/udev/udevd.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 9900a9363e..5ea197c1c4 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -495,8 +495,6 @@ static int device_get_whole_disk(sd_device *dev, sd_device **ret_device, const c return log_device_debug_errno(dev, r, "Failed to get whole disk device: %m"); r = sd_device_get_devname(dev, &val); - if (r == -ENOENT) - goto irrelevant; if (r < 0) return log_device_debug_errno(dev, r, "Failed to get devname: %m"); -- cgit v1.2.3