From: Karel Zak Date: Wed, 7 Nov 2007 18:05:31 +0000 (+0100) Subject: fdisk: cleanup BLK* ioctls usage X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=810f986b42ff7ecc805d134ef32013c3343df31e;p=util-linux fdisk: cleanup BLK* ioctls usage Signed-off-by: Karel Zak --- diff --git a/fdisk/Makefile.am b/fdisk/Makefile.am index c149263b..7e44569e 100644 --- a/fdisk/Makefile.am +++ b/fdisk/Makefile.am @@ -2,7 +2,8 @@ include $(top_srcdir)/config/include-Makefile.am EXTRA_DIST = README.fdisk README.cfdisk sfdisk.examples partitiontype.c -fdisk_common = disksize.c i386_sys_types.c common.h gpt.c gpt.h +fdisk_common = i386_sys_types.c common.h gpt.c gpt.h \ + ../lib/blkdev.c ../lib/linux_version.c if !ARCH_M68K diff --git a/fdisk/cfdisk.c b/fdisk/cfdisk.c index 05c8cd6a..7ed11ea1 100644 --- a/fdisk/cfdisk.c +++ b/fdisk/cfdisk.c @@ -81,6 +81,7 @@ #include #include "nls.h" +#include "blkdev.h" #include "xstrncpy.h" #include "common.h" #include "gpt.h" @@ -1687,7 +1688,7 @@ fill_p_info(void) { ioctl(fd, BLKFLSBUF); /* ignore errors */ /* e.g. Permission Denied */ - if (disksize(fd, &llsectors)) + if (blkdev_get_sectors(fd, &llsectors) == -1) fatal(_("Cannot get disk size"), 3); actual_size = llsectors; diff --git a/fdisk/common.h b/fdisk/common.h index cc5a04cd..380bbc39 100644 --- a/fdisk/common.h +++ b/fdisk/common.h @@ -7,25 +7,6 @@ #define PATH_DEV_BYID "/dev/disk/by-id" #define PATH_DEV_BYPATH "/dev/disk/by-path" -/* including fails */ -#include -#include -#define BLKRRPART _IO(0x12,95) /* re-read partition table */ -#define BLKGETSIZE _IO(0x12,96) /* return device size */ -#define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */ -#define BLKSSZGET _IO(0x12,104) /* get block device sector size */ -#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* size in bytes */ - -/* including also fails */ -struct hd_geometry { - unsigned char heads; - unsigned char sectors; - unsigned short cylinders; - unsigned long start; -}; - -#define HDIO_GETGEO 0x0301 /* get device geometry */ - struct systypes { unsigned char type; char *name; @@ -36,6 +17,4 @@ extern struct systypes i386_sys_types[]; extern char *partname(char *dev, int pno, int lth); extern int is_probably_full_disk(char *name); -int disksize(int fd, unsigned long long *sectors); - #endif /* FDISK_COMMON_H */ diff --git a/fdisk/disksize.c b/fdisk/disksize.c deleted file mode 100644 index cc00e933..00000000 --- a/fdisk/disksize.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - -int disksize(int fd, unsigned long long *sectors) { - int err; - long sz; - long long b; - - err = ioctl(fd, BLKGETSIZE, &sz); - if (err) { - sz = 0; - if (errno != EFBIG) - return err; - } - err = ioctl(fd, BLKGETSIZE64, &b); - if (err || b == 0 || b == sz) - *sectors = sz; - else - *sectors = (b >> 9); - return 0; -} - -int -is_probably_full_disk(char *name) { -#ifdef HDIO_GETGEO - struct hd_geometry geometry; - int fd, i = 0; - - fd = open(name, O_RDONLY); - if (fd >= 0) { - i = ioctl(fd, HDIO_GETGEO, &geometry); - close(fd); - } - if (i==0) - return (fd >= 0 && geometry.start == 0); -#endif - /* - * The "silly heuristic" is still sexy for us, because - * for example Xen doesn't implement HDIO_GETGEO for virtual - * block devices (/dev/xvda). - * - * -- kzak@redhat.com (23-Feb-2006) - */ - while (*name) - name++; - return !isdigit(name[-1]); -} - diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c index aa6004e6..c5e3f06c 100644 --- a/fdisk/fdisk.c +++ b/fdisk/fdisk.c @@ -21,6 +21,7 @@ #include #include "nls.h" +#include "blkdev.h" #include "common.h" #include "fdisk.h" @@ -826,40 +827,18 @@ create_doslabel(void) { write_part_table_flag(MBRbuffer); } -#include -#define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r)) - -static int -linux_version_code(void) { - static int kernel_version = 0; - struct utsname my_utsname; - int p, q, r; - - if (!kernel_version && uname(&my_utsname) == 0) { - p = atoi(strtok(my_utsname.release, ".")); - q = atoi(strtok(NULL, ".")); - r = atoi(strtok(NULL, ".")); - kernel_version = MAKE_VERSION(p,q,r); - } - return kernel_version; -} - static void get_sectorsize(int fd) { -#if defined(BLKSSZGET) - if (!user_set_sector_size && - linux_version_code() >= MAKE_VERSION(2,3,3)) { - int arg; - if (ioctl(fd, BLKSSZGET, &arg) == 0) - sector_size = arg; - if (sector_size != DEFAULT_SECTOR_SIZE) - printf(_("Note: sector size is %d (not %d)\n"), - sector_size, DEFAULT_SECTOR_SIZE); - } -#else - /* maybe the user specified it; and otherwise we still - have the DEFAULT_SECTOR_SIZE default */ -#endif + int arg; + + if (user_set_sector_size) + return; + + if (blkdev_get_sector_size(fd, &arg) == 0) + sector_size = arg; + if (sector_size != DEFAULT_SECTOR_SIZE) + printf(_("Note: sector size is %d (not %d)\n"), + sector_size, DEFAULT_SECTOR_SIZE); } static void @@ -929,7 +908,7 @@ get_geometry(int fd, struct geom *g) { pt_sectors ? pt_sectors : kern_sectors ? kern_sectors : 63; - if (disksize(fd, &llsectors)) + if (blkdev_get_sectors(fd, &llsectors) == -1) llsectors = 0; total_number_of_sectors = llsectors; @@ -2660,7 +2639,7 @@ main(int argc, char **argv) { gpt_warning(disk_device); if ((fd = open(disk_device, type_open)) < 0) fatal(unable_to_open); - if (disksize(fd, &size)) + if (blkdev_get_sectors(fd, &size) == -1) fatal(ioctl_error); close(fd); if (opts == 1) diff --git a/fdisk/fdisksgilabel.c b/fdisk/fdisksgilabel.c index 7e189fb3..5adf64e3 100644 --- a/fdisk/fdisksgilabel.c +++ b/fdisk/fdisksgilabel.c @@ -24,6 +24,8 @@ #include "nls.h" #include /* FLOPPY_MAJOR */ +#include "blkdev.h" + #include "common.h" #include "fdisk.h" #include "fdisksgilabel.h" @@ -710,7 +712,7 @@ create_sgilabel(void) other_endian = (BYTE_ORDER == LITTLE_ENDIAN); - res = disksize(fd, &llsectors); + res = blkdev_get_sectors(fd, &llsectors); if (!ioctl(fd, HDIO_GETGEO, &geometry)) { heads = geometry.heads; diff --git a/fdisk/fdisksunlabel.c b/fdisk/fdisksunlabel.c index 60ade06d..ff9a182d 100644 --- a/fdisk/fdisksunlabel.c +++ b/fdisk/fdisksunlabel.c @@ -19,6 +19,7 @@ #include /* major */ #include "nls.h" +#include "blkdev.h" #include #ifdef HAVE_SCSI_SCSI_H @@ -203,7 +204,7 @@ void create_sunlabel(void) sunlabel->version = SSWAP32(SUN_LABEL_VERSION); sunlabel->num_partitions = SSWAP16(SUN_NUM_PARTITIONS); - res = disksize(fd, &llsectors); + res = blkdev_get_sectors(fd, &llsectors); sec_fac = sector_size / 512; if (!ioctl(fd, HDIO_GETGEO, &geometry)) { diff --git a/fdisk/gpt.c b/fdisk/gpt.c index b8f3d33d..be7c0175 100644 --- a/fdisk/gpt.c +++ b/fdisk/gpt.c @@ -38,9 +38,7 @@ #include #include "gpt.h" - -#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) -#define SECTOR_SIZE 512 /* default */ +#include "blkdev.h" #define _GET_BYTE(x, n) ( ((x) >> (8 * (n))) & 0xff ) @@ -61,11 +59,6 @@ # define CPU_TO_LE64(x) PED_SWAP64(x) #endif -#define BLKSSZGET _IO(0x12,104) /* get block device sector size */ -#define BLKGETLASTSECT _IO(0x12,108) /* get last sector of block device */ -#define BLKGETSIZE _IO(0x12,96) /* return device size */ -#define BLKGETSIZE64 _IOR(0x12,114,size_t) /* return device size in bytes (u64 *arg) */ - #define GPT_HEADER_SIGNATURE 0x5452415020494645LL #define GPT_PRIMARY_PARTITION_TABLE_LBA 1 @@ -108,52 +101,23 @@ struct blkdev_ioctl_param { }; static int -_get_linux_version (void) -{ - static int kver = -1; - struct utsname uts; - int major; - int minor; - int teeny; - - if (kver != -1) - return kver; - if (uname (&uts)) - return kver = 0; - if (sscanf (uts.release, "%u.%u.%u", &major, &minor, &teeny) != 3) - return kver = 0; - return kver = KERNEL_VERSION (major, minor, teeny); -} - -static unsigned int _get_sector_size (int fd) { - unsigned int sector_size; + int sector_size; - if (_get_linux_version() < KERNEL_VERSION (2,3,0)) - return SECTOR_SIZE; - if (ioctl (fd, BLKSSZGET, §or_size)) - return SECTOR_SIZE; + if (blkdev_get_sector_size(fd, §or_size) == -1) + return DEFAULT_SECTOR_SIZE; return sector_size; } static uint64_t _get_num_sectors(int fd) { - int version = _get_linux_version(); - unsigned long size; - uint64_t bytes=0; + unsigned long long bytes=0; - if (version >= KERNEL_VERSION(2,5,4) || - (version < KERNEL_VERSION(2,5,0) && - version >= KERNEL_VERSION (2,4,18))) - { - if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) - return bytes / _get_sector_size(fd); - } - if (ioctl (fd, BLKGETSIZE, &size)) + if (blkdev_get_size(fd, &bytes) == -1) return 0; - return size; + return bytes / _get_sector_size(fd); } static uint64_t diff --git a/fdisk/partname.c b/fdisk/partname.c index 8f60b718..7c71c0dd 100644 --- a/fdisk/partname.c +++ b/fdisk/partname.c @@ -43,3 +43,28 @@ partname(char *dev, int pno, int lth) { return bufp; } +int +is_probably_full_disk(char *name) { +#ifdef HDIO_GETGEO + struct hd_geometry geometry; + int fd, i = 0; + + fd = open(name, O_RDONLY); + if (fd >= 0) { + i = ioctl(fd, HDIO_GETGEO, &geometry); + close(fd); + } + if (i==0) + return (fd >= 0 && geometry.start == 0); +#endif + /* + * The "silly heuristic" is still sexy for us, because + * for example Xen doesn't implement HDIO_GETGEO for virtual + * block devices (/dev/xvda). + * + * -- kzak@redhat.com (23-Feb-2006) + */ + while (*name) + name++; + return !isdigit(name[-1]); +} diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c index cb4229f0..94a7c958 100644 --- a/fdisk/sfdisk.c +++ b/fdisk/sfdisk.c @@ -48,6 +48,7 @@ #include #include /* _syscall */ #include "nls.h" +#include "blkdev.h" #include "common.h" #include "gpt.h" @@ -458,7 +459,7 @@ get_geometry(char *dev, int fd, int silent) { R.cylinders = 0; R.total_size = 0; - if (disksize(fd, §ors)) { + if (blkdev_get_sectors(fd, §ors) == -1) { /* maybe an ordinary file */ struct stat s; @@ -2810,7 +2811,7 @@ do_size (char *dev, int silent) { if (fd < 0) return; - if (disksize(fd, &size)) { + if (blkdev_get_sectors(fd, &size) == -1) { if (!silent) { perror(dev); fatal(_("Cannot get size of %s\n"), dev);