]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: make block_device_remove_all_partitions() take sd_device object
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Sep 2022 19:46:04 +0000 (04:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 Sep 2022 11:45:20 +0000 (20:45 +0900)
Then, it is not necessary to recreate sd_device object when we already
have.

src/shared/blockdev-util.c
src/shared/blockdev-util.h
src/shared/loop-util.c

index 0b2554d4b23bb40f7731c923f01fa2dc3e44a722..1a7419c549fbf3f91ffe2c925de4099971a079d8 100644 (file)
@@ -509,19 +509,27 @@ int block_device_resize_partition(
         return RET_NERRNO(ioctl(fd, BLKPG, &ba));
 }
 
-int block_device_remove_all_partitions(int fd) {
-        struct stat stat;
-        _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
+int block_device_remove_all_partitions(sd_device *dev, int fd) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+        _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
+        _cleanup_close_ int fd_close = -1;
         sd_device *part;
         int r, k = 0;
 
-        if (fstat(fd, &stat) < 0)
-                return -errno;
+        assert(dev || fd >= 0);
 
-        r = sd_device_new_from_devnum(&dev, 'b', stat.st_rdev);
-        if (r < 0)
-                return r;
+        if (!dev) {
+                struct stat st;
+
+                if (fstat(fd, &st) < 0)
+                        return -errno;
+
+                r = sd_device_new_from_stat_rdev(&dev_unref, &st);
+                if (r < 0)
+                        return r;
+
+                dev = dev_unref;
+        }
 
         r = sd_device_enumerator_new(&e);
         if (r < 0)
@@ -539,6 +547,14 @@ int block_device_remove_all_partitions(int fd) {
         if (r < 0)
                 return r;
 
+        if (fd < 0) {
+                fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
+                if (fd_close < 0)
+                        return fd_close;
+
+                fd = fd_close;
+        }
+
         FOREACH_DEVICE(e, part) {
                 const char *v, *devname;
                 int nr;
index 2f1f347d091586e74118b152a214b031ef159016..e63c447331ccbc4fbdbf4178ececcfea96995128 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <sys/types.h>
 
+#include "sd-device.h"
+
 #include "macro.h"
 #include "stdio-util.h"
 #include "string-util.h"
@@ -34,4 +36,4 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
 int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size);
 int block_device_remove_partition(int fd, const char *name, int nr);
 int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
-int block_device_remove_all_partitions(int fd);
+int block_device_remove_all_partitions(sd_device *dev, int fd);
index ac873b015492ceaa29688159e21fbe4c1b8c5284..56574ee7f20dc936ff9602ef13385ffd6ea62a84 100644 (file)
@@ -181,7 +181,7 @@ static int loop_configure(
                 /* Unbound but has children? Remove all partitions, and report this to the caller, to try
                  * again, and count this as an attempt. */
 
-                r = block_device_remove_all_partitions(fd);
+                r = block_device_remove_all_partitions(dev, fd);
                 if (r < 0)
                         return r;
 
@@ -695,7 +695,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
                         if (flock(d->fd, LOCK_EX) < 0)
                                 log_debug_errno(errno, "Failed to lock loop block device, ignoring: %m");
 
-                        r = block_device_remove_all_partitions(d->fd);
+                        r = block_device_remove_all_partitions(d->dev, d->fd);
                         if (r < 0)
                                 log_debug_errno(r, "Failed to remove partitions of loopback block device, ignoring: %m");