From f5ea9eee5e45f9349e67506e3f7c81d8087c1cde Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 21 Jan 2022 14:49:47 +0100 Subject: [PATCH] libblkid: optimize ioctl calls in blkid_probe_set_device() * call FDGETFDCSTAT only for block devices * don't check floppies for device-mapper, CD-ROM features or zones Signed-off-by: Karel Zak --- libblkid/src/probe.c | 64 +++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/libblkid/src/probe.c b/libblkid/src/probe.c index 6443f100917..2e0e08ac1d1 100644 --- a/libblkid/src/probe.c +++ b/libblkid/src/probe.c @@ -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))