Skip to content

Commit

Permalink
libblkid: optimize ioctl calls in blkid_probe_set_device()
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
karelzak committed Jan 21, 2022
1 parent 01b63ee commit f5ea9ee
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions libblkid/src/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f5ea9ee

Please sign in to comment.