fdisk: cleanup BLK* ioctls usage

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2007-11-07 19:05:31 +01:00
parent b7188e932e
commit 810f986b42
10 changed files with 57 additions and 158 deletions

View File

@ -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

View File

@ -81,6 +81,7 @@
#include <linux/types.h>
#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;

View File

@ -7,25 +7,6 @@
#define PATH_DEV_BYID "/dev/disk/by-id"
#define PATH_DEV_BYPATH "/dev/disk/by-path"
/* including <linux/fs.h> fails */
#include <sys/types.h>
#include <sys/ioctl.h>
#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 <linux/hdreg.h> 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 */

View File

@ -1,54 +0,0 @@
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#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]);
}

View File

@ -21,6 +21,7 @@
#include <time.h>
#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 <sys/utsname.h>
#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)

View File

@ -24,6 +24,8 @@
#include "nls.h"
#include <linux/major.h> /* 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;

View File

@ -19,6 +19,7 @@
#include <sys/sysmacros.h> /* major */
#include "nls.h"
#include "blkdev.h"
#include <endian.h>
#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)) {

View File

@ -38,9 +38,7 @@
#include <errno.h>
#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, &sector_size))
return SECTOR_SIZE;
if (blkdev_get_sector_size(fd, &sector_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

View File

@ -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]);
}

View File

@ -48,6 +48,7 @@
#include <sys/utsname.h>
#include <linux/unistd.h> /* _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, &sectors)) {
if (blkdev_get_sectors(fd, &sectors) == -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);