dcavalca / rpms / util-linux

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