]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: use block_device_is_whole_disk()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 20 Sep 2022 00:04:54 +0000 (09:04 +0900)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 20 Sep 2022 07:44:19 +0000 (09:44 +0200)
No functional changes, just refactoring.

Note, this also makes synthesize_change() propagate the error from
synthesize_change_one(). However, the caller of synthesize_change()
ignores the failure anyway, hence the change does not take any effect.

src/shared/blockdev-util.c
src/shared/blockdev-util.h
src/udev/udevd.c

index ab3b5ce263385b8a153cc474f85829d1c1e206bd..12d4b54952693f73971d02e8c71bef9905a76043 100644 (file)
@@ -20,7 +20,7 @@
 #include "missing_magic.h"
 #include "parse-util.h"
 
-static int block_device_is_whole_disk(sd_device *dev) {
+int block_device_is_whole_disk(sd_device *dev) {
         const char *s;
         int r;
 
index 802d79394ffbbf1cf622a2012e55c1b146c3acab..1f612157fc55dfd5527de07fb8236cc3e219b4f2 100644 (file)
@@ -14,6 +14,7 @@
 #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_is_whole_disk(sd_device *dev);
 int block_device_get_whole_disk(sd_device *dev, sd_device **ret);
 
 int block_get_whole_disk(dev_t d, dev_t *ret);
index 5ea197c1c48cf1f9485acc105b115d7542dbfdd9..3e0c02893e48a12f90c41e6a8579ed8743fb8838 100644 (file)
@@ -1362,52 +1362,44 @@ static int synthesize_change_one(sd_device *dev, sd_device *target) {
 }
 
 static int synthesize_change(sd_device *dev) {
-        const char *subsystem, *sysname, *devtype;
-        int r;
+        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+        bool part_table_read;
+        const char *sysname;
+        sd_device *d;
+        int r, k;
 
-        r = sd_device_get_subsystem(dev, &subsystem);
+        r = sd_device_get_sysname(dev, &sysname);
         if (r < 0)
                 return r;
 
-        r = sd_device_get_devtype(dev, &devtype);
+        if (startswith(sysname, "dm-") || block_device_is_whole_disk(dev) <= 0)
+                return synthesize_change_one(dev, dev);
+
+        r = blockdev_reread_partition_table(dev);
         if (r < 0)
-                return r;
+                log_device_debug_errno(dev, r, "Failed to re-read partition table, ignoring: %m");
+        part_table_read = r >= 0;
 
-        r = sd_device_get_sysname(dev, &sysname);
+        /* search for partitions */
+        r = partition_enumerator_new(dev, &e);
         if (r < 0)
                 return r;
 
-        if (streq_ptr(subsystem, "block") &&
-            streq_ptr(devtype, "disk") &&
-            !startswith(sysname, "dm-")) {
-                _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
-                bool part_table_read;
-                sd_device *d;
-
-                r = blockdev_reread_partition_table(dev);
-                if (r < 0)
-                        log_device_debug_errno(dev, r, "Failed to re-read partition table, ignoring: %m");
-                part_table_read = r >= 0;
-
-                /* search for partitions */
-                r = partition_enumerator_new(dev, &e);
-                if (r < 0)
-                        return r;
-
-                /* We have partitions and re-read the table, the kernel already sent out a "change"
-                 * event for the disk, and "remove/add" for all partitions. */
-                if (part_table_read && sd_device_enumerator_get_device_first(e))
-                        return 0;
+        /* We have partitions and re-read the table, the kernel already sent out a "change"
+         * event for the disk, and "remove/add" for all partitions. */
+        if (part_table_read && sd_device_enumerator_get_device_first(e))
+                return 0;
 
-                /* We have partitions but re-reading the partition table did not work, synthesize
-                 * "change" for the disk and all partitions. */
-                (void) synthesize_change_one(dev, dev);
-                FOREACH_DEVICE(e, d)
-                        (void) synthesize_change_one(dev, d);
-        } else
-                (void) synthesize_change_one(dev, dev);
+        /* We have partitions but re-reading the partition table did not work, synthesize
+         * "change" for the disk and all partitions. */
+        r = synthesize_change_one(dev, dev);
+        FOREACH_DEVICE(e, d) {
+                k = synthesize_change_one(dev, d);
+                if (k < 0 && r >= 0)
+                        r = k;
+        }
 
-        return 0;
+        return r;
 }
 
 static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {