]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: optimize ioctl calls in blkid_probe_set_device()
authorKarel Zak <kzak@redhat.com>
Fri, 21 Jan 2022 13:49:47 +0000 (14:49 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 21 Jan 2022 13:51:30 +0000 (14:51 +0100)
* call FDGETFDCSTAT only for block devices
* don't check floppies for device-mapper, CD-ROM features or zones

Signed-off-by: Karel Zak <kzak@redhat.com>
libblkid/src/probe.c

index 6443f10091798f6f1d99ee09a0eb7149e215b185..2e0e08ac1d1a45be039a381faa2f3009c54c4494 100644 (file)
@@ -880,6 +880,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
        struct stat sb;
        uint64_t devsiz = 0;
        char *dm_uuid = NULL;
+       int is_floppy = 0;
 
        blkid_reset_probe(pr);
        blkid_probe_reset_buffers(pr);
@@ -911,35 +912,6 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
        if (fd < 0)
                return 1;
 
-#ifdef FDGETFDCSTAT
-       {
-               /*
-                * Re-open without O_NONBLOCK for floppy device.
-                *
-                * Since kernel commit c7e9d0020361f4308a70cdfd6d5335e273eb8717
-                * floppy drive works bad when opened with O_NONBLOCK.
-                */
-               struct floppy_fdc_state flst;
-
-               if (ioctl(fd, FDGETFDCSTAT, &flst) >= 0) {
-                       int flags = fcntl(fd, F_GETFL, 0);
-
-                       if (flags < 0)
-                               goto err;
-                       if (flags & O_NONBLOCK) {
-                               flags &= ~O_NONBLOCK;
-
-                               fd = ul_reopen(fd, flags | O_CLOEXEC);
-                               if (fd < 0)
-                                       goto err;
-
-                               pr->flags |= BLKID_FL_PRIVATE_FD;
-                               pr->fd = fd;
-                       }
-               }
-               errno = 0;
-       }
-#endif
 
 #if defined(POSIX_FADV_RANDOM) && defined(HAVE_POSIX_FADVISE)
        /* Disable read-ahead */
@@ -990,7 +962,38 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
        if (pr->size <= 1440 * 1024 && !S_ISCHR(sb.st_mode))
                pr->flags |= BLKID_FL_TINY_DEV;
 
+#ifdef FDGETFDCSTAT
+       if (S_ISBLK(sb.st_mode)) {
+               /*
+                * Re-open without O_NONBLOCK for floppy device.
+                *
+                * Since kernel commit c7e9d0020361f4308a70cdfd6d5335e273eb8717
+                * floppy drive works bad when opened with O_NONBLOCK.
+                */
+               struct floppy_fdc_state flst;
+
+               if (ioctl(fd, FDGETFDCSTAT, &flst) >= 0) {
+                       int flags = fcntl(fd, F_GETFL, 0);
+
+                       if (flags < 0)
+                               goto err;
+                       if (flags & O_NONBLOCK) {
+                               flags &= ~O_NONBLOCK;
+
+                               fd = ul_reopen(fd, flags | O_CLOEXEC);
+                               if (fd < 0)
+                                       goto err;
+
+                               pr->flags |= BLKID_FL_PRIVATE_FD;
+                               pr->fd = fd;
+                       }
+                       is_floppy = 1;
+               }
+               errno = 0;
+       }
+#endif
        if (S_ISBLK(sb.st_mode) &&
+           !is_floppy &&
            sysfs_devno_is_dm_private(sb.st_rdev, &dm_uuid)) {
                DBG(LOWPROBE, ul_debug("ignore private device mapper device"));
                pr->flags |= BLKID_FL_NOSCAN_DEV;
@@ -1000,6 +1003,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
        else if (S_ISBLK(sb.st_mode) &&
            !blkid_probe_is_tiny(pr) &&
            !dm_uuid &&
+           !is_floppy &&
            blkid_probe_is_wholedisk(pr)) {
 
                long last_written = 0;
@@ -1051,7 +1055,7 @@ int blkid_probe_set_device(blkid_probe pr, int fd,
        free(dm_uuid);
 
 # ifdef BLKGETZONESZ
-       if (S_ISBLK(sb.st_mode)) {
+       if (S_ISBLK(sb.st_mode) && !is_floppy) {
                uint32_t zone_size_sector;
 
                if (!ioctl(pr->fd, BLKGETZONESZ, &zone_size_sector))