diff options
author | Karel Zak | 2006-12-07 00:26:18 +0100 |
---|---|---|
committer | Karel Zak | 2006-12-07 00:26:18 +0100 |
commit | 2cccd0ff2b26c782dbd05d589b7695cab5e17458 (patch) | |
tree | 45ebc5937d2e083c12b58bdeffc1b1a2046c9598 /fdisk | |
parent | Imported from util-linux-2.12b tarball. (diff) | |
download | kernel-qcow2-util-linux-2cccd0ff2b26c782dbd05d589b7695cab5e17458.tar.gz kernel-qcow2-util-linux-2cccd0ff2b26c782dbd05d589b7695cab5e17458.tar.xz kernel-qcow2-util-linux-2cccd0ff2b26c782dbd05d589b7695cab5e17458.zip |
Imported from util-linux-2.12d tarball.
Diffstat (limited to 'fdisk')
-rw-r--r-- | fdisk/Makefile | 8 | ||||
-rw-r--r-- | fdisk/cfdisk.c | 16 | ||||
-rw-r--r-- | fdisk/common.h | 3 | ||||
-rw-r--r-- | fdisk/disksize.c | 17 | ||||
-rw-r--r-- | fdisk/fdisk.c | 32 | ||||
-rw-r--r-- | fdisk/fdisksgilabel.c | 13 | ||||
-rw-r--r-- | fdisk/sfdisk.c | 58 |
7 files changed, 88 insertions, 59 deletions
diff --git a/fdisk/Makefile b/fdisk/Makefile index c883b5f0d..e0d3f36c7 100644 --- a/fdisk/Makefile +++ b/fdisk/Makefile @@ -39,7 +39,7 @@ else endif endif -cfdisk: cfdisk.o llseek.o i386_sys_types.o $(LIB)/xstrncpy.o +cfdisk: cfdisk.o llseek.o disksize.o i386_sys_types.o $(LIB)/xstrncpy.o ifeq "$(HAVE_SLANG)" "yes" $(CC) $(LDFLAGS) $^ -o $@ $(LIBSLANG) else @@ -55,15 +55,15 @@ activate: sfdisk rm -f activate ln -s sfdisk activate -fdisk: fdisk.o llseek.o fdiskbsdlabel.o fdisksgilabel.o fdisksunlabel.o \ - fdiskaixlabel.o i386_sys_types.o partname.o +fdisk: fdisk.o llseek.o disksize.o fdiskbsdlabel.o fdisksgilabel.o \ + fdisksunlabel.o fdiskaixlabel.o i386_sys_types.o partname.o fdisk.o: fdisk.c fdisk.h fdiskbsdlabel.o: fdiskbsdlabel.c fdisk.h fdiskbsdlabel.h fdisksunlabel.o: fdisksunlabel.c fdisksunlabel.h fdisk.h fdiskaixlabel.o: fdiskaixlabel.c fdiskaixlabel.h fdisk.h fdisk.o cfdisk.o sfdisk.o fdiskbsdlabel.o fdisksunlabel.o \ fdisksgilabel.o fdiskaixlabel.o i386_sys_types.o partname.o: common.h -sfdisk: sfdisk.o i386_sys_types.o partname.o +sfdisk: sfdisk.o disksize.o i386_sys_types.o partname.o install: all $(INSTALLDIR) $(SBINDIR) diff --git a/fdisk/cfdisk.c b/fdisk/cfdisk.c index 9be1c7073..e09b4f5f3 100644 --- a/fdisk/cfdisk.c +++ b/fdisk/cfdisk.c @@ -170,7 +170,7 @@ int sectors = 0; long long cylinders = 0; int cylinder_size = 0; /* heads * sectors */ long long total_size = 0; /* actual_size rounded down */ -long long actual_size = 0; /* set using ioctl */ +long long actual_size = 0; /* (in 512-byte sectors) - set using ioctl */ /* explicitly given user values */ int user_heads = 0, user_sectors = 0; long long user_cylinders = 0; @@ -1579,7 +1579,7 @@ static void fill_p_info(void) { int pn, i; long long bs, bsz; - unsigned long long bytes; + unsigned long long llsectors; struct partition *p; partition_table buffer; partition_info tmp_ext = { 0, 0, 0, 0, FREE_SPACE, PRIMARY }; @@ -1608,15 +1608,9 @@ fill_p_info(void) { ioctl(fd, BLKFLSBUF); /* ignore errors */ /* e.g. Permission Denied */ - if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) - actual_size = (bytes >> 9); - else { - unsigned long sz = 0; - - if (ioctl(fd, BLKGETSIZE, &sz)) - fatal(_("Cannot get disk size"), 3); - actual_size = sz; - } + if (disksize(fd, &llsectors)) + fatal(_("Cannot get disk size"), 3); + actual_size = llsectors; read_sector(buffer.c.b, 0); diff --git a/fdisk/common.h b/fdisk/common.h index a2b9f1cdf..7c1c3cd38 100644 --- a/fdisk/common.h +++ b/fdisk/common.h @@ -1,6 +1,7 @@ /* common stuff for fdisk, cfdisk, sfdisk */ /* 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 */ @@ -27,3 +28,5 @@ struct systypes { extern struct systypes i386_sys_types[]; extern char *partname(char *dev, int pno, int lth); + +int disksize(int fd, unsigned long long *sectors); diff --git a/fdisk/disksize.c b/fdisk/disksize.c new file mode 100644 index 000000000..a0a210e98 --- /dev/null +++ b/fdisk/disksize.c @@ -0,0 +1,17 @@ +#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) + return err; + err = ioctl(fd, BLKGETSIZE64, &b); + if (err || b == 0 || b == sz) + *sectors = sz; + else + *sectors = (b >> 9); + return 0; +} diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c index bf2cdded4..40a4967ef 100644 --- a/fdisk/fdisk.c +++ b/fdisk/fdisk.c @@ -821,8 +821,7 @@ get_partition_table_geometry(void) { void get_geometry(int fd, struct geom *g) { int sec_fac; - unsigned long longsectors; - unsigned long long bytes; /* really u64 */ + unsigned long long llsectors, llcyls; get_sectorsize(fd); sec_fac = sector_size / 512; @@ -841,26 +840,19 @@ get_geometry(int fd, struct geom *g) { pt_sectors ? pt_sectors : kern_sectors ? kern_sectors : 63; - if (ioctl(fd, BLKGETSIZE, &longsectors)) - longsectors = 0; - if (ioctl(fd, BLKGETSIZE64, &bytes)) - bytes = 0; + if (disksize(fd, &llsectors)) + llsectors = 0; - /* - * If BLKGETSIZE64 was unknown or broken, use longsectors. - * (Kernels 2.4.15-2.4.17 had a broken BLKGETSIZE64 - * that returns sectors instead of bytes.) - */ - if (bytes == 0 || bytes == longsectors) - bytes = ((unsigned long long) longsectors) << 9; - - total_number_of_sectors = (bytes >> 9); + total_number_of_sectors = llsectors; sector_offset = 1; if (dos_compatible_flag) sector_offset = sectors; - cylinders = total_number_of_sectors / (heads * sectors * sec_fac); + llcyls = total_number_of_sectors / (heads * sectors * sec_fac); + cylinders = llcyls; + if (cylinders != llcyls) /* truncated? */ + cylinders = ~0; if (!cylinders) cylinders = user_cylinders; @@ -2531,7 +2523,7 @@ main(int argc, char **argv) { } if (opts) { - unsigned long size; + unsigned long long size; nowarn = 1; type_open = O_RDONLY; @@ -2544,13 +2536,13 @@ main(int argc, char **argv) { disk_device = argv[j]; if ((fd = open(disk_device, type_open)) < 0) fatal(unable_to_open); - if (ioctl(fd, BLKGETSIZE, &size)) + if (disksize(fd, &size)) fatal(ioctl_error); close(fd); if (opts == 1) - printf("%lu\n", size/2); + printf("%llu\n", size/2); else - printf("%s: %lu\n", argv[j], size/2); + printf("%s: %llu\n", argv[j], size/2); } exit(0); } diff --git a/fdisk/fdisksgilabel.c b/fdisk/fdisksgilabel.c index ce05418e5..c9ccd3e39 100644 --- a/fdisk/fdisksgilabel.c +++ b/fdisk/fdisksgilabel.c @@ -696,8 +696,8 @@ create_sgilabel(void) int sysid; } old[4]; int i=0; - long longsectors; /* the number of sectors on the device */ - int res; /* the result from the ioctl */ + unsigned long long llsectors; + int res; /* the result from the ioctl */ int sec_fac; /* the sector factor */ sec_fac = sector_size / 512; /* determine the sector factor */ @@ -709,15 +709,18 @@ create_sgilabel(void) other_endian = (BYTE_ORDER == LITTLE_ENDIAN); - res = ioctl(fd, BLKGETSIZE, &longsectors); + res = disksize(fd, &llsectors); if (!ioctl(fd, HDIO_GETGEO, &geometry)) { heads = geometry.heads; sectors = geometry.sectors; if (res == 0) { /* the get device size ioctl was successful */ - cylinders = longsectors / (heads * sectors); - cylinders /= sec_fac; + unsigned long long llcyls; + llcyls = llsectors / (heads * sectors * sec_fac); + cylinders = llcyls; + if (cylinders != llcyls) /* truncated? */ + cylinders = ~0; } else { /* otherwise print error and use truncated version */ cylinders = geometry.cylinders; diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c index f3abe98d0..9dbe709cc 100644 --- a/fdisk/sfdisk.c +++ b/fdisk/sfdisk.c @@ -409,24 +409,32 @@ struct geometry { static struct geometry get_geometry(char *dev, int fd, int silent) { struct hd_geometry g; - unsigned long size; + unsigned long cyls; + unsigned long long sectors; struct geometry R; - if (ioctl(fd, BLKGETSIZE, &size)) { - size = 0; - if (!silent) - do_warn(_("Disk %s: cannot get size\n"), dev); - } if (ioctl(fd, HDIO_GETGEO, &g)) { g.heads = g.sectors = g.cylinders = g.start = 0; if (!silent) do_warn(_("Disk %s: cannot get geometry\n"), dev); } + + R.start = g.start; R.heads = g.heads; R.sectors = g.sectors; R.cylindersize = R.heads * R.sectors; - R.cylinders = (R.cylindersize ? size / R.cylindersize : 0); - R.start = g.start; + R.cylinders = 0; + + if (disksize(fd, §ors)) { + if (!silent) + do_warn(_("Disk %s: cannot get size\n"), dev); + } else if (R.cylindersize) { + sectors /= R.cylindersize; + cyls = sectors; + if (cyls != sectors) + cyls = ~0; + R.cylinders = cyls; + } return R; } @@ -2368,6 +2376,19 @@ is_ide_cdrom_or_tape(char *device) { return is_ide; } +static int +is_probably_full_disk(char *name) { + struct hd_geometry geometry; + int fd, i = 0; + + fd = open(name, O_RDONLY); + if (fd >= 0) { + i = ioctl(fd, HDIO_GETGEO, &geometry); + close(fd); + } + return (fd >= 0 && i == 0 && geometry.start == 0); +} + #define PROC_PARTITIONS "/proc/partitions" static FILE *procf = NULL; @@ -2381,7 +2402,7 @@ openproc(void) { static char * nextproc(void) { static char devname[120]; - char line[100], ptname[100], *s; + char line[100], ptname[100]; int ma, mi, sz; if (procf == NULL) @@ -2390,10 +2411,9 @@ nextproc(void) { if (sscanf (line, " %d %d %d %[^\n ]", &ma, &mi, &sz, ptname) != 4) continue; - for(s = ptname; *s; s++); - if (isdigit(s[-1])) - continue; snprintf(devname, sizeof(devname), "/dev/%s", ptname); + if (!is_probably_full_disk(devname)) + continue; return devname; } @@ -2411,7 +2431,7 @@ static void do_change_id(char *dev, char *part, char *id); static void do_unhide(char **av, int ac, char *arg); static void do_activate(char **av, int ac, char *arg); -unsigned long total_size; +unsigned long long total_size; int main(int argc, char **argv) { @@ -2551,7 +2571,7 @@ main(int argc, char **argv) { } if (opt_size) - printf(_("total: %lu blocks\n"), total_size); + printf(_("total: %llu blocks\n"), total_size); exit(exit_status); } @@ -2677,16 +2697,16 @@ do_geom (char *dev, int silent) { static void do_size (char *dev, int silent) { int fd; - unsigned long size; + unsigned long long size; fd = my_open(dev, 0, silent); if (fd < 0) return; - if (ioctl(fd, BLKGETSIZE, &size)) { + if (disksize(fd, &size)) { if (!silent) { perror(dev); - fatal(_("BLKGETSIZE ioctl failed for %s\n"), dev); + fatal(_("Cannot get size of %s\n"), dev); } return; } @@ -2698,9 +2718,9 @@ do_size (char *dev, int silent) { return; if (silent) - printf("%s: %9lu\n", dev, size); + printf("%s: %9llu\n", dev, size); else - printf("%lu\n", size); + printf("%llu\n", size); total_size += size; } |