diff options
author | Karel Zak | 2017-01-13 11:23:23 +0100 |
---|---|---|
committer | Karel Zak | 2017-01-13 11:23:23 +0100 |
commit | 7526c305b459f1387dad72d1543308246fc12877 (patch) | |
tree | 2947ea35cf9f0f5b688d991bb3ec1d05a2b5c9d4 /libfdisk/src/alignment.c | |
parent | If mtab support is disabled, disable ro/rw mtab checks (diff) | |
download | kernel-qcow2-util-linux-7526c305b459f1387dad72d1543308246fc12877.tar.gz kernel-qcow2-util-linux-7526c305b459f1387dad72d1543308246fc12877.tar.xz kernel-qcow2-util-linux-7526c305b459f1387dad72d1543308246fc12877.zip |
libfdisk: don't use blkdev ioctls for regular files
$ fdisk <diskimage>
open("sdc.img", O_RDONLY|O_CLOEXEC) = 3
ioctl(3, BLKSSZGET, 0x7ffcf51357c4) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(3, BLKGETSIZE64, 0x7ffcf51357b0) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(3, BLKGETSIZE, 0x7ffcf51356c8) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(3, FDGETPRM, 0x7ffcf51356d0) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(3, HDIO_GETGEO, 0x7ffcf5135790) = -1 ENOTTY (Inappropriate ioctl for device)
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/alignment.c')
-rw-r--r-- | libfdisk/src/alignment.c | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/libfdisk/src/alignment.c b/libfdisk/src/alignment.c index 58acefb89..b874a48f8 100644 --- a/libfdisk/src/alignment.c +++ b/libfdisk/src/alignment.c @@ -183,12 +183,14 @@ int fdisk_lba_is_phy_aligned(struct fdisk_context *cxt, fdisk_sector_t lba) return lba_is_phy_aligned(cxt, lba); } -static unsigned long get_sector_size(int fd) +static unsigned long get_sector_size(struct fdisk_context *cxt) { int sect_sz; - if (!blkdev_get_sector_size(fd, §_sz)) + if (!fdisk_is_regfile(cxt) && + !blkdev_get_sector_size(cxt->dev_fd, §_sz)) return (unsigned long) sect_sz; + return DEFAULT_SECTOR_SIZE; } @@ -423,22 +425,31 @@ int fdisk_reset_device_properties(struct fdisk_context *cxt) int fdisk_discover_geometry(struct fdisk_context *cxt) { fdisk_sector_t nsects; + unsigned int h = 0, s = 0; assert(cxt); assert(cxt->geom.heads == 0); DBG(CXT, ul_debugobj(cxt, "%s: discovering geometry...", cxt->dev_path)); - /* get number of 512-byte sectors, and convert it the real sectors */ - if (!blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &nsects)) - cxt->total_sectors = (nsects / (cxt->sector_size >> 9)); + if (fdisk_is_regfile(cxt)) + cxt->total_sectors = cxt->dev_st.st_size / cxt->sector_size; + else { + /* get number of 512-byte sectors, and convert it the real sectors */ + if (!blkdev_get_sectors(cxt->dev_fd, (unsigned long long *) &nsects)) + cxt->total_sectors = (nsects / (cxt->sector_size >> 9)); + + /* what the kernel/bios thinks the geometry is */ + blkdev_get_geometry(cxt->dev_fd, &h, &s); + } DBG(CXT, ul_debugobj(cxt, "total sectors: %ju (ioctl=%ju)", (uintmax_t) cxt->total_sectors, (uintmax_t) nsects)); - /* what the kernel/bios thinks the geometry is */ - blkdev_get_geometry(cxt->dev_fd, &cxt->geom.heads, (unsigned int *) &cxt->geom.sectors); + cxt->geom.cylinders = 0; + cxt->geom.heads = h; + cxt->geom.sectors = s; /* obtained heads and sectors */ recount_geometry(cxt); @@ -491,7 +502,7 @@ int fdisk_discover_topology(struct fdisk_context *cxt) blkid_free_probe(pr); #endif - cxt->sector_size = get_sector_size(cxt->dev_fd); + cxt->sector_size = get_sector_size(cxt); if (!cxt->phy_sector_size) /* could not discover physical size */ cxt->phy_sector_size = cxt->sector_size; |