libfdisk: use context geometry in SGI and SUN

* don't call HDIO_GETGEO ioctls in the drivers
 * use context geometry (so user can overwrite it by -C/H/S options)
 * use default geometry is ioctl does not return anything

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2013-09-25 15:34:34 +02:00
parent 15c755975b
commit e5b5a349cd
2 changed files with 30 additions and 47 deletions

View File

@ -952,41 +952,32 @@ static int sgi_create_disklabel(struct fdisk_context *cxt)
{
struct fdisk_sgi_label *sgi;
struct sgi_disklabel *sgilabel;
struct hd_geometry geometry;
sector_t llsectors;
int res; /* the result from the ioctl */
int sec_fac; /* the sector factor */
assert(cxt);
assert(cxt->label);
assert(fdisk_is_disklabel(cxt, SGI));
sec_fac = cxt->sector_size / 512; /* determine the sector factor */
res = blkdev_get_sectors(cxt->dev_fd, &llsectors);
#ifdef HDIO_GETGEO
/* TODO: it seems unnecessary, geometry is already set in the context */
if (ioctl(cxt->dev_fd, HDIO_GETGEO, &geometry) < 0) {
fdisk_warn(cxt, _("HDIO_GETGEO ioctl failed on %s"), cxt->dev_path);
return -1;
}
cxt->geom.heads = geometry.heads;
cxt->geom.sectors = geometry.sectors;
if (res == 0) {
/* the get device size ioctl was successful */
sector_t llcyls;
llcyls = llsectors / (cxt->geom.heads * cxt->geom.sectors * sec_fac);
cxt->geom.cylinders = llcyls;
if (cxt->geom.cylinders != llcyls) /* truncated? */
cxt->geom.cylinders = ~0;
} else {
/* otherwise print error and use truncated version */
cxt->geom.cylinders = geometry.cylinders;
fdisk_warnx(cxt,
_("Warning: BLKGETSIZE ioctl failed on %s. "
"Using geometry cylinder value of %llu."
"This value may be truncated for devices"
" > 33.8 GB."), cxt->dev_path, cxt->geom.cylinders);
if (cxt->geom.heads && cxt->geom.sectors) {
sector_t llsectors;
if (blkdev_get_sectors(cxt->dev_fd, &llsectors) == 0) {
/* the get device size ioctl was successful */
sector_t llcyls;
int sec_fac = cxt->sector_size / 512;
llcyls = llsectors / (cxt->geom.heads * cxt->geom.sectors * sec_fac);
cxt->geom.cylinders = llcyls;
if (cxt->geom.cylinders != llcyls) /* truncated? */
cxt->geom.cylinders = ~0;
} else {
/* otherwise print error and use truncated version */
fdisk_warnx(cxt,
_("Warning: BLKGETSIZE ioctl failed on %s. "
"Using geometry cylinder value of %llu."
"This value may be truncated for devices"
" > 33.8 GB."), cxt->dev_path, cxt->geom.cylinders);
}
}
#endif
fdisk_zeroize_firstsector(cxt);
@ -1007,14 +998,14 @@ static int sgi_create_disklabel(struct fdisk_context *cxt)
sgilabel->devparam.gap1 = (0);
sgilabel->devparam.gap2 = (0);
sgilabel->devparam.sparecyl = (0);
sgilabel->devparam.pcylcount = cpu_to_be16(geometry.cylinders);
sgilabel->devparam.pcylcount = cpu_to_be16(cxt->geom.cylinders);
sgilabel->devparam.head_vol0 = cpu_to_be16(0);
sgilabel->devparam.ntrks = cpu_to_be16(geometry.heads);
sgilabel->devparam.ntrks = cpu_to_be16(cxt->geom.heads);
/* tracks/cylinder (heads) */
sgilabel->devparam.cmd_tag_queue_depth = (0);
sgilabel->devparam.unused0 = (0);
sgilabel->devparam.unused1 = cpu_to_be16(0);
sgilabel->devparam.nsect = cpu_to_be16(geometry.sectors);
sgilabel->devparam.nsect = cpu_to_be16(cxt->geom.sectors);
/* sectors/track */
sgilabel->devparam.bytes = cpu_to_be16(cxt->sector_size);
sgilabel->devparam.ilfact = cpu_to_be16(1);

View File

@ -190,11 +190,7 @@ static void ask_geom(struct fdisk_context *cxt)
static int sun_create_disklabel(struct fdisk_context *cxt)
{
struct hd_geometry geometry;
sector_t llsectors, llcyls;
unsigned int ndiv, sec_fac;
int res;
unsigned int ndiv;
struct fdisk_sun_label *sun; /* libfdisk sun handler */
struct sun_disklabel *sunlabel; /* on disk data */
@ -216,23 +212,19 @@ static int sun_create_disklabel(struct fdisk_context *cxt)
sunlabel->vtoc.sanity = cpu_to_be32(SUN_VTOC_SANITY);
sunlabel->vtoc.nparts = cpu_to_be16(SUN_MAXPARTITIONS);
res = blkdev_get_sectors(cxt->dev_fd, &llsectors);
sec_fac = cxt->sector_size / 512;
#ifdef HDIO_GETGEO
if (ioctl(cxt->dev_fd, HDIO_GETGEO, &geometry) == 0
&& geometry.heads
&& geometry.sectors) {
if (cxt->geom.heads && cxt->geom.sectors) {
sector_t llsectors;
if (blkdev_get_sectors(cxt->dev_fd, &llsectors) == 0) {
int sec_fac = cxt->sector_size / 512;
sector_t llcyls;
cxt->geom.heads = geometry.heads;
cxt->geom.sectors = geometry.sectors;
if (res == 0) {
llcyls = llsectors / (cxt->geom.heads * cxt->geom.sectors * sec_fac);
cxt->geom.cylinders = llcyls;
if (cxt->geom.cylinders != llcyls)
cxt->geom.cylinders = ~0;
} else {
cxt->geom.cylinders = geometry.cylinders;
fdisk_warnx(cxt,
_("BLKGETSIZE ioctl failed on %s. "
"Using geometry cylinder value of %llu. "