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
#include <linux/types.h>
#include "nls.h"
+#include "blkdev.h"
#include "xstrncpy.h"
#include "common.h"
#include "gpt.h"
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;
#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;
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 */
+++ /dev/null
-#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]);
-}
-
#include <time.h>
#include "nls.h"
+#include "blkdev.h"
#include "common.h"
#include "fdisk.h"
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
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;
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)
#include "nls.h"
#include <linux/major.h> /* FLOPPY_MAJOR */
+#include "blkdev.h"
+
#include "common.h"
#include "fdisk.h"
#include "fdisksgilabel.h"
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;
#include <sys/sysmacros.h> /* major */
#include "nls.h"
+#include "blkdev.h"
#include <endian.h>
#ifdef HAVE_SCSI_SCSI_H
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)) {
#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 )
# 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
};
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
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]);
+}
#include <sys/utsname.h>
#include <linux/unistd.h> /* _syscall */
#include "nls.h"
+#include "blkdev.h"
#include "common.h"
#include "gpt.h"
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;
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);