]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: add simple wrapper around BLKSSZGET
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Jan 2023 19:12:30 +0000 (20:12 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Jan 2023 09:10:57 +0000 (10:10 +0100)
Just adds some typesafety and generates an error if the field is not
initialized in the block device yet.

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

index 7a4d21d02e2f2bc3c34626735eb96102361eb52c..ee134146a152695e21407b0556dbac09c71d61d7 100644 (file)
@@ -762,3 +762,18 @@ int blockdev_reread_partition_table(sd_device *dev) {
 
         return 0;
 }
+
+int blockdev_get_sector_size(int fd, uint32_t *ret) {
+        int ssz = 0;
+
+        assert(fd >= 0);
+        assert(ret);
+
+        if (ioctl(fd, BLKSSZGET, &ssz) < 0)
+                return -errno;
+        if (ssz <= 0) /* make sure the field is initialized */
+                return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Block device reported invalid sector size %i.", ssz);
+
+        *ret = ssz;
+        return 0;
+}
index b2c14102ae18f352aa3a2314422b20c2a7e07c0f..5b27d23e8ae77230c2ac7cb8ca5153fa1e8c8c78 100644 (file)
@@ -54,3 +54,5 @@ int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret);
 int block_device_remove_all_partitions(sd_device *dev, int fd);
 int block_device_has_partitions(sd_device *dev);
 int blockdev_reread_partition_table(sd_device *dev);
+
+int blockdev_get_sector_size(int fd, uint32_t *ret);
index 3224286186c8f5c69fdc1a0e165089ecc1b13c67..4b63e0340b9a3a80433fa9d93e66ea2f9f48d4d7 100644 (file)
@@ -125,14 +125,14 @@ static int loop_configure_verify(int fd, const struct loop_config *c) {
         assert(c);
 
         if (c->block_size != 0) {
-                int z;
+                uint32_t ssz;
 
-                if (ioctl(fd, BLKSSZGET, &z) < 0)
-                        return -errno;
+                r = blockdev_get_sector_size(fd, &ssz);
+                if (r < 0)
+                        return r;
 
-                assert(z >= 0);
-                if ((uint32_t) z != c->block_size)
-                        log_debug("LOOP_CONFIGURE didn't honour requested block size %u, got %i instead. Ignoring.", c->block_size, z);
+                if (ssz != c->block_size)
+                        log_debug("LOOP_CONFIGURE didn't honour requested block size %" PRIu32 ", got %" PRIu32 " instead. Ignoring.", c->block_size, ssz);
         }
 
         if (c->info.lo_sizelimit != 0) {