05ad79
From 28b08b639aeaadbfcc3fb66558e6b392b2b5d44c Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Tue, 28 Jun 2016 11:30:21 +0200
05ad79
Subject: [PATCH 82/86] fdisk: backport DOS logical partitions chain reorder
05ad79
05ad79
... from the current upstream.
05ad79
05ad79
Addresses: http://bugzilla.redhat.com/show_bug.cgi?id=1304246
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 fdisks/fdiskdoslabel.c | 170 +++++++++++++++++++++++++++++++++----------------
05ad79
 1 file changed, 116 insertions(+), 54 deletions(-)
05ad79
05ad79
diff --git a/fdisks/fdiskdoslabel.c b/fdisks/fdiskdoslabel.c
05ad79
index fe04ac7..b7eb35a 100644
05ad79
--- a/fdisks/fdiskdoslabel.c
05ad79
+++ b/fdisks/fdiskdoslabel.c
05ad79
@@ -55,6 +55,22 @@ static int MBRbuffer_changed;
05ad79
 #define cround(c, n)	(fdisk_context_use_cylinders(c) ? \
05ad79
 				((n) / fdisk_context_get_units_per_sector(c)) + 1 : (n))
05ad79
 
05ad79
+
05ad79
+static unsigned long long
05ad79
+get_abs_partition_start(struct pte *pe)
05ad79
+{
05ad79
+	return pe->offset + get_start_sect(pe->part_table);
05ad79
+}
05ad79
+
05ad79
+static unsigned long long
05ad79
+get_abs_partition_end(struct pte *pe)
05ad79
+{
05ad79
+	unsigned long long size;
05ad79
+
05ad79
+	size = get_nr_sects(pe->part_table);
05ad79
+	return get_abs_partition_start(pe) + size - (size ? 1 : 0);
05ad79
+}
05ad79
+
05ad79
 static void warn_alignment(struct fdisk_context *cxt)
05ad79
 {
05ad79
 	if (nowarn)
05ad79
@@ -1254,67 +1270,113 @@ void dos_list_table_expert(struct fdisk_context *cxt, int extend)
05ad79
 	}
05ad79
 }
05ad79
 
05ad79
-/*
05ad79
- * Fix the chain of logicals.
05ad79
- * extended_offset is unchanged, the set of sectors used is unchanged
05ad79
- * The chain is sorted so that sectors increase, and so that
05ad79
- * starting sectors increase.
05ad79
- *
05ad79
- * After this it may still be that cfdisk doesn't like the table.
05ad79
- * (This is because cfdisk considers expanded parts, from link to
05ad79
- * end of partition, and these may still overlap.)
05ad79
- * Now
05ad79
- *   sfdisk /dev/hda > ohda; sfdisk /dev/hda < ohda
05ad79
- * may help.
05ad79
- */
05ad79
+
05ad79
+static void print_chain_of_logicals(struct fdisk_context *cxt)
05ad79
+{
05ad79
+	size_t i;
05ad79
+
05ad79
+	fputc('\n', stdout);
05ad79
+
05ad79
+	for (i = 4; i < cxt->label->nparts_max; i++) {
05ad79
+		struct pte *pe = &ptes[i];
05ad79
+
05ad79
+		fprintf(stderr, "#%02zu EBR [%10ju], "
05ad79
+			"data[start=%10ju (%10ju), size=%10ju], "
05ad79
+			"link[start=%10ju (%10ju), size=%10ju]\n",
05ad79
+			i, (uintmax_t) pe->offset,
05ad79
+			/* data */
05ad79
+			(uintmax_t) get_start_sect(pe->part_table),
05ad79
+			(uintmax_t) get_abs_partition_start(pe),
05ad79
+			(uintmax_t) get_nr_sects(pe->part_table),
05ad79
+			/* link */
05ad79
+			(uintmax_t) get_start_sect(pe->ext_pointer),
05ad79
+			(uintmax_t) (extended_offset + get_start_sect(pe->ext_pointer)),
05ad79
+			(uintmax_t) get_nr_sects(pe->ext_pointer));
05ad79
+	}
05ad79
+}
05ad79
+
05ad79
+static int cmp_ebr_offsets(const void *a, const void *b)
05ad79
+{
05ad79
+	struct pte *ae = (struct pte *) a,
05ad79
+		   *be = (struct pte *) b;
05ad79
+
05ad79
+	if (ae->offset == 0 && be->offset == 0)
05ad79
+		return 0;
05ad79
+	if (ae->offset == 0)
05ad79
+		return 1;
05ad79
+	if (be->offset == 0)
05ad79
+		return -1;
05ad79
+
05ad79
+	return cmp_numbers(ae->offset, be->offset);
05ad79
+}
05ad79
+
05ad79
 static void fix_chain_of_logicals(struct fdisk_context *cxt)
05ad79
 {
05ad79
-	size_t j, oj, ojj, sj, sjj;
05ad79
-	struct partition *pj,*pjj,tmp;
05ad79
-
05ad79
-	/* Stage 1: sort sectors but leave sector of part 4 */
05ad79
-	/* (Its sector is the global extended_offset.) */
05ad79
- stage1:
05ad79
-	for (j = 5; j < cxt->label->nparts_max - 1; j++) {
05ad79
-		oj = ptes[j].offset;
05ad79
-		ojj = ptes[j+1].offset;
05ad79
-		if (oj > ojj) {
05ad79
-			ptes[j].offset = ojj;
05ad79
-			ptes[j+1].offset = oj;
05ad79
-			pj = ptes[j].part_table;
05ad79
-			set_start_sect(pj, get_start_sect(pj)+oj-ojj);
05ad79
-			pjj = ptes[j+1].part_table;
05ad79
-			set_start_sect(pjj, get_start_sect(pjj)+ojj-oj);
05ad79
-			set_start_sect(ptes[j-1].ext_pointer,
05ad79
-				       ojj-extended_offset);
05ad79
-			set_start_sect(ptes[j].ext_pointer,
05ad79
-				       oj-extended_offset);
05ad79
-			goto stage1;
05ad79
+	struct pte *last;
05ad79
+	size_t i;
05ad79
+
05ad79
+	DBG(CONTEXT, print_chain_of_logicals(cxt));
05ad79
+
05ad79
+	/* Sort chain by EBR offsets */
05ad79
+	qsort(&ptes[4], cxt->label->nparts_max - 4, sizeof(struct pte),
05ad79
+			cmp_ebr_offsets);
05ad79
+
05ad79
+again:
05ad79
+	/* Sort data partitions by start */
05ad79
+	for (i = 4; i < cxt->label->nparts_max - 1; i++) {
05ad79
+		struct pte *cur = &ptes[i],
05ad79
+			   *nxt = &ptes[i + 1];
05ad79
+
05ad79
+		if (get_abs_partition_start(cur) >
05ad79
+		    get_abs_partition_start(nxt)) {
05ad79
+
05ad79
+			struct partition tmp = *cur->part_table;
05ad79
+			sector_t cur_start = get_abs_partition_start(cur),
05ad79
+				 nxt_start = get_abs_partition_start(nxt);
05ad79
+
05ad79
+			/* swap data partitions */
05ad79
+			*cur->part_table = *nxt->part_table;
05ad79
+			*nxt->part_table = tmp;
05ad79
+
05ad79
+			/* Recount starts according to EBR offsets, the absolute
05ad79
+			 * address still has to be the same! */
05ad79
+			set_start_sect(cur->part_table, nxt_start - cur->offset);
05ad79
+			set_start_sect(nxt->part_table, cur_start - nxt->offset);
05ad79
+
05ad79
+			cur->changed = 1;
05ad79
+			nxt->changed = 1;
05ad79
+			goto again;
05ad79
 		}
05ad79
 	}
05ad79
 
05ad79
-	/* Stage 2: sort starting sectors */
05ad79
- stage2:
05ad79
-	for (j = 4; j < cxt->label->nparts_max - 1; j++) {
05ad79
-		pj = ptes[j].part_table;
05ad79
-		pjj = ptes[j+1].part_table;
05ad79
-		sj = get_start_sect(pj);
05ad79
-		sjj = get_start_sect(pjj);
05ad79
-		oj = ptes[j].offset;
05ad79
-		ojj = ptes[j+1].offset;
05ad79
-		if (oj+sj > ojj+sjj) {
05ad79
-			tmp = *pj;
05ad79
-			*pj = *pjj;
05ad79
-			*pjj = tmp;
05ad79
-			set_start_sect(pj, ojj+sjj-oj);
05ad79
-			set_start_sect(pjj, oj+sj-ojj);
05ad79
-			goto stage2;
05ad79
-		}
05ad79
+	/* Update EBR links */
05ad79
+	for (i = 4; i < cxt->label->nparts_max - 1; i++) {
05ad79
+		struct pte *cur = &ptes[i],
05ad79
+			   *nxt = &ptes[i + 1];
05ad79
+
05ad79
+		sector_t noff = nxt->offset - extended_offset,
05ad79
+		         ooff = get_start_sect(cur->ext_pointer);
05ad79
+
05ad79
+		if (noff == ooff)
05ad79
+			continue;
05ad79
+
05ad79
+		DBG(CONTEXT, dbgprint("DOS: fix EBR [%10ju] link %ju -> %ju",
05ad79
+			(uintmax_t) cur->offset,
05ad79
+			(uintmax_t) ooff, (uintmax_t) noff));
05ad79
+
05ad79
+		set_partition(cxt, i, 1, nxt->offset,
05ad79
+				get_abs_partition_end(nxt),
05ad79
+				EXTENDED);
05ad79
+	}
05ad79
+
05ad79
+	/* always terminate the chain ! */
05ad79
+	last = &ptes[cxt->label->nparts_max - 1];
05ad79
+	if (last) {
05ad79
+		clear_partition(last->ext_pointer);
05ad79
+		last->changed = 1;
05ad79
 	}
05ad79
 
05ad79
-	/* Probably something was changed */
05ad79
-	for (j = 4; j < cxt->label->nparts_max; j++)
05ad79
-		ptes[j].changed = 1;
05ad79
+	DBG(CONTEXT, print_chain_of_logicals(cxt));
05ad79
 }
05ad79
 
05ad79
 void dos_fix_partition_table_order(struct fdisk_context *cxt)
05ad79
-- 
05ad79
2.7.4
05ad79