Blame SOURCES/0021-fdasd-geometry-handling-updated-from-upstream-s390-t.patch

0cb0b9
From 4d480d980a9b69b432b8d60df3c4397ba8cdc965 Mon Sep 17 00:00:00 2001
0cb0b9
From: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
0cb0b9
Date: Thu, 17 Sep 2015 15:33:28 +0200
0cb0b9
Subject: [PATCH 21/22] fdasd: geometry handling updated from upstream
0cb0b9
 s390-tools
0cb0b9
0cb0b9
Remove the necessity for DASD-specific ioctls for partition handling.
0cb0b9
This allows to correctly handle DASD-backed virtio block devices.
0cb0b9
0cb0b9
Note that this is necessary because virtio is just the transport.
0cb0b9
A DASD, even if provided via virtio, still has it's unique
0cb0b9
characteristics, which means that only traditional DASD partition
0cb0b9
table formats can be used (no MBR, GPT, or similar).
0cb0b9
0cb0b9
Use bzero for initialization to make sure all struct members are
0cb0b9
properly cleared. Also changed partition list handling code to be
0cb0b9
more similar to upstream s390-tools fdasd.
0cb0b9
0cb0b9
Further, enhanced error handling capabilities by providing a
0cb0b9
return code by fdasd_get_geometry.
0cb0b9
0cb0b9
Code is largely backported from s390-tools project.
0cb0b9
0cb0b9
Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
0cb0b9
Acked-by: Stefan Haberland <stefan.haberland@de.ibm.com>
0cb0b9
Signed-off-by: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
0cb0b9
Signed-off-by: Brian C. Lane <bcl@redhat.com>
0cb0b9
---
0cb0b9
 include/parted/fdasd.in.h |   4 +-
0cb0b9
 libparted/labels/fdasd.c  | 166 ++++++++++++++++++++++++++++++++--------------
0cb0b9
 2 files changed, 119 insertions(+), 51 deletions(-)
0cb0b9
0cb0b9
diff --git a/include/parted/fdasd.in.h b/include/parted/fdasd.in.h
0cb0b9
index 6f6a7e0..4e351c4 100644
0cb0b9
--- a/include/parted/fdasd.in.h
0cb0b9
+++ b/include/parted/fdasd.in.h
0cb0b9
@@ -190,6 +190,8 @@ typedef struct format_data_t {
0cb0b9
 #define BLKRRPART  _IO(0x12,95)
0cb0b9
 /* get block device sector size */
0cb0b9
 #define BLKSSZGET  _IO(0x12,104)
0cb0b9
+/* device size in bytes (u64 *arg)*/
0cb0b9
+#define BLKGETSIZE64 _IOR(0x12,114,size_t)
0cb0b9
 /* get device geometry */
0cb0b9
 #define HDIO_GETGEO		0x0301
0cb0b9
 
0cb0b9
@@ -285,7 +287,7 @@ enum fdasd_failure {
0cb0b9
 
0cb0b9
 void fdasd_cleanup (fdasd_anchor_t *anchor);
0cb0b9
 void fdasd_initialize_anchor (fdasd_anchor_t * anc);
0cb0b9
-void fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int fd);
0cb0b9
+int fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int fd);
0cb0b9
 void fdasd_check_api_version (fdasd_anchor_t *anc, int fd);
0cb0b9
 int fdasd_check_volume (fdasd_anchor_t *anc, int fd);
0cb0b9
 int fdasd_write_labels (fdasd_anchor_t *anc, int fd);
0cb0b9
diff --git a/libparted/labels/fdasd.c b/libparted/labels/fdasd.c
0cb0b9
index 1f87937..7e6a77a 100644
0cb0b9
--- a/libparted/labels/fdasd.c
0cb0b9
+++ b/libparted/labels/fdasd.c
0cb0b9
@@ -210,27 +210,7 @@ fdasd_initialize_anchor (fdasd_anchor_t * anc)
0cb0b9
 	partition_info_t *p = NULL;
0cb0b9
 	partition_info_t *q = NULL;
0cb0b9
 
0cb0b9
-	anc->devno             = 0;
0cb0b9
-	anc->dev_type          = 0;
0cb0b9
-	anc->used_partitions   = 0;
0cb0b9
-
0cb0b9
-	anc->silent            = 0;
0cb0b9
-	anc->verbose           = 0;
0cb0b9
-	anc->big_disk          = 0;
0cb0b9
-	anc->volid_specified   = 0;
0cb0b9
-	anc->config_specified  = 0;
0cb0b9
-	anc->auto_partition    = 0;
0cb0b9
-	anc->devname_specified = 0;
0cb0b9
-	anc->print_table       = 0;
0cb0b9
-
0cb0b9
-	anc->option_reuse      = 0;
0cb0b9
-	anc->option_recreate   = 0;
0cb0b9
-
0cb0b9
-	anc->vlabel_changed    = 0;
0cb0b9
-	anc->vtoc_changed      = 0;
0cb0b9
-	anc->blksize           = 0;
0cb0b9
-	anc->fspace_trk        = 0;
0cb0b9
-	anc->label_pos         = 0;
0cb0b9
+	bzero(anc, sizeof(fdasd_anchor_t));
0cb0b9
 
0cb0b9
 	for (i=0; i
0cb0b9
 		setpos(anc, i, -1);
0cb0b9
@@ -272,24 +252,18 @@ fdasd_initialize_anchor (fdasd_anchor_t * anc)
0cb0b9
 		if (p == NULL)
0cb0b9
 			fdasd_error(anc, malloc_failed,
0cb0b9
 				    _("No room for partition info."));
0cb0b9
-		p->used       = 0x00;
0cb0b9
-		p->len_trk    = 0;
0cb0b9
-		p->start_trk  = 0;
0cb0b9
-		p->fspace_trk = 0;
0cb0b9
-		p->type       = 0;
0cb0b9
+		bzero(p, sizeof(partition_info_t));
0cb0b9
 
0cb0b9
 		/* add p to double pointered list */
0cb0b9
 		if (i == 1) {
0cb0b9
-	        anc->first = p;
0cb0b9
-			p->prev = NULL;
0cb0b9
+		        anc->first = p;
0cb0b9
 		} else if (i == USABLE_PARTITIONS) {
0cb0b9
-	        anc->last = p;
0cb0b9
-	        p->next = NULL;
0cb0b9
+		        anc->last = p;
0cb0b9
 			p->prev = q;
0cb0b9
 			q->next = p;
0cb0b9
 		} else {
0cb0b9
-	        p->prev = q;
0cb0b9
-	        q->next = p;
0cb0b9
+	                p->prev = q;
0cb0b9
+	                q->next = p;
0cb0b9
 		}
0cb0b9
 
0cb0b9
 		p->f1 = malloc(sizeof(format1_label_t));
0cb0b9
@@ -947,15 +921,77 @@ fdasd_check_api_version (fdasd_anchor_t *anc, int f)
0cb0b9
 }
0cb0b9
 
0cb0b9
 /*
0cb0b9
+ * The following two functions match those in the DASD ECKD device driver.
0cb0b9
+ * They are used to compute how many records of a given size can be stored
0cb0b9
+ * in one track.
0cb0b9
+ */
0cb0b9
+static unsigned int ceil_quot(unsigned int d1, unsigned int d2)
0cb0b9
+{
0cb0b9
+	return (d1 + (d2 - 1)) / d2;
0cb0b9
+}
0cb0b9
+
0cb0b9
+/* kl: key length, dl: data length */
0cb0b9
+static unsigned int recs_per_track(unsigned short dev_type, unsigned int kl,
0cb0b9
+				   unsigned int dl)
0cb0b9
+{
0cb0b9
+	unsigned int dn, kn;
0cb0b9
+
0cb0b9
+	switch (dev_type) {
0cb0b9
+	case DASD_3380_TYPE:
0cb0b9
+		if (kl)
0cb0b9
+			return 1499 / (15 + 7 + ceil_quot(kl + 12, 32) +
0cb0b9
+				       ceil_quot(dl + 12, 32));
0cb0b9
+		else
0cb0b9
+			return 1499 / (15 + ceil_quot(dl + 12, 32));
0cb0b9
+	case DASD_3390_TYPE:
0cb0b9
+		dn = ceil_quot(dl + 6, 232) + 1;
0cb0b9
+		if (kl) {
0cb0b9
+			kn = ceil_quot(kl + 6, 232) + 1;
0cb0b9
+			return 1729 / (10 + 9 + ceil_quot(kl + 6 * kn, 34) +
0cb0b9
+				       9 + ceil_quot(dl + 6 * dn, 34));
0cb0b9
+		} else
0cb0b9
+			return 1729 / (10 + 9 + ceil_quot(dl + 6 * dn, 34));
0cb0b9
+	case DASD_9345_TYPE:
0cb0b9
+		dn = ceil_quot(dl + 6, 232) + 1;
0cb0b9
+		if (kl) {
0cb0b9
+			kn = ceil_quot(kl + 6, 232) + 1;
0cb0b9
+			return 1420 / (18 + 7 + ceil_quot(kl + 6 * kn, 34) +
0cb0b9
+				       ceil_quot(dl + 6 * dn, 34));
0cb0b9
+		} else
0cb0b9
+			return 1420 / (18 + 7 + ceil_quot(dl + 6 * dn, 34));
0cb0b9
+	}
0cb0b9
+	return 0;
0cb0b9
+}
0cb0b9
+
0cb0b9
+/*
0cb0b9
+ * Verify that number of tracks (heads) per cylinder and number of
0cb0b9
+ * sectors per track match the expected values for a given device type
0cb0b9
+ * and block size.
0cb0b9
+ * Returns 1 for a valid match and 0 otherwise.
0cb0b9
+ */
0cb0b9
+static int fdasd_verify_geometry(unsigned short dev_type, int blksize,
0cb0b9
+				 struct fdasd_hd_geometry *geometry)
0cb0b9
+{
0cb0b9
+	unsigned int expected_sectors;
0cb0b9
+	if (geometry->heads != 15)
0cb0b9
+		return 0;
0cb0b9
+	expected_sectors = recs_per_track(dev_type, 0, blksize);
0cb0b9
+	if (geometry->sectors == expected_sectors)
0cb0b9
+		return 1;
0cb0b9
+	return 0;
0cb0b9
+}
0cb0b9
+
0cb0b9
+/*
0cb0b9
  * reads dasd geometry data
0cb0b9
  */
0cb0b9
-void
0cb0b9
+int
0cb0b9
 fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int f)
0cb0b9
 {
0cb0b9
 	PDEBUG
0cb0b9
 	int blksize = 0;
0cb0b9
 	dasd_information_t dasd_info;
0cb0b9
 	struct dasd_eckd_characteristics *characteristics;
0cb0b9
+	unsigned long long size_in_bytes;
0cb0b9
 
0cb0b9
 	/* We can't get geometry from a regular file,
0cb0b9
 	   so simulate something usable, for the sake of testing.  */
0cb0b9
@@ -979,6 +1015,12 @@ fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int f)
0cb0b9
 				anc->geo.heads;
0cb0b9
 	    anc->is_file = 1;
0cb0b9
 	} else {
0cb0b9
+	        if (ioctl(f, BLKGETSIZE64, &size_in_bytes) != 0) {
0cb0b9
+		        fdasd_error(anc, unable_to_ioctl,
0cb0b9
+				    _("Could not retrieve disk size."));
0cb0b9
+			goto error;
0cb0b9
+		}
0cb0b9
+
0cb0b9
 		if (ioctl(f, HDIO_GETGEO, &anc->geo) != 0)
0cb0b9
 			fdasd_error(anc, unable_to_ioctl,
0cb0b9
 			    _("Could not retrieve disk geometry information."));
0cb0b9
@@ -988,27 +1030,51 @@ fdasd_get_geometry (const PedDevice *dev, fdasd_anchor_t *anc, int f)
0cb0b9
 			    _("Could not retrieve blocksize information."));
0cb0b9
 
0cb0b9
 		/* get disk type */
0cb0b9
-		if (ioctl(f, BIODASDINFO, &dasd_info) != 0)
0cb0b9
-			fdasd_error(anc, unable_to_ioctl,
0cb0b9
-				    _("Could not retrieve disk information."));
0cb0b9
-
0cb0b9
-		characteristics = (struct dasd_eckd_characteristics *)
0cb0b9
-					&dasd_info.characteristics;
0cb0b9
-		if (characteristics->no_cyl == LV_COMPAT_CYL &&
0cb0b9
-		    characteristics->long_no_cyl)
0cb0b9
-			anc->hw_cylinders = characteristics->long_no_cyl;
0cb0b9
-		else
0cb0b9
-			anc->hw_cylinders = characteristics->no_cyl;
0cb0b9
+		if (ioctl(f, BIODASDINFO, &dasd_info) != 0) {
0cb0b9
+		        /* verify that the geometry matches a 3390 DASD */
0cb0b9
+		        if (!fdasd_verify_geometry(DASD_3390_TYPE, blksize,
0cb0b9
+						   &anc->geo)) {
0cb0b9
+			        fdasd_error(anc, wrong_disk_type,
0cb0b9
+					    _("Disk geometry does not match a " \
0cb0b9
+					      "DASD device of type 3390."));
0cb0b9
+				goto error;
0cb0b9
+			}
0cb0b9
+			anc->dev_type = DASD_3390_TYPE;
0cb0b9
+			anc->hw_cylinders =
0cb0b9
+			        size_in_bytes / (blksize * anc->geo.heads * anc->geo.sectors);
0cb0b9
+			/* The VOL1 label on a CDL formatted ECKD DASD is in block 2
0cb0b9
+			 * It will be verified later, if this position actually holds a
0cb0b9
+			 * valid label record.
0cb0b9
+			 */
0cb0b9
+			anc->label_pos = 2 * blksize;
0cb0b9
+			/* A devno 0 is actually a valid devno, which could exist
0cb0b9
+			 * in the system. Since we use this number only to create
0cb0b9
+			 * a default volume serial, there is no serious conflict.
0cb0b9
+			 */
0cb0b9
+			anc->devno = 0;
0cb0b9
+		} else {
0cb0b9
+		        characteristics = (struct dasd_eckd_characteristics *)
0cb0b9
+			        &dasd_info.characteristics;
0cb0b9
+			if (characteristics->no_cyl == LV_COMPAT_CYL &&
0cb0b9
+			        characteristics->long_no_cyl)
0cb0b9
+		                anc->hw_cylinders = characteristics->long_no_cyl;
0cb0b9
+			else
0cb0b9
+		                anc->hw_cylinders = characteristics->no_cyl;
0cb0b9
+			anc->dev_type = dasd_info.dev_type;
0cb0b9
+			anc->label_pos = dasd_info.label_block * blksize;
0cb0b9
+			anc->devno = dasd_info.devno;
0cb0b9
+			anc->label_block = dasd_info.label_block;
0cb0b9
+			anc->FBA_layout = dasd_info.FBA_layout;
0cb0b9
+		}
0cb0b9
 
0cb0b9
 		anc->is_file = 0;
0cb0b9
 	}
0cb0b9
 
0cb0b9
-	anc->dev_type   = dasd_info.dev_type;
0cb0b9
-	anc->blksize    = blksize;
0cb0b9
-	anc->label_pos  = dasd_info.label_block * blksize;
0cb0b9
-	anc->devno      = dasd_info.devno;
0cb0b9
-	anc->label_block = dasd_info.label_block;
0cb0b9
-	anc->FBA_layout = dasd_info.FBA_layout;
0cb0b9
+	anc->blksize = blksize;
0cb0b9
+	return 1;
0cb0b9
+
0cb0b9
+ error:
0cb0b9
+	return 0;
0cb0b9
 }
0cb0b9
 
0cb0b9
 /*
0cb0b9
-- 
0cb0b9
2.4.3
0cb0b9