public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Scott Moser <smoser@ubuntu.com>
To: util-linux@vger.kernel.org
Cc: Philip Susi <psusi@ubuntu.com>
Subject: [PATCH] fix partx --update ranges and out of order tables
Date: Mon, 13 Jan 2014 15:32:49 -0500 (EST)	[thread overview]
Message-ID: <alpine.DEB.2.02.1401131526340.5329@brickies> (raw)

partx --update DEVICE NUMBER
was broken in 2 cases:
 * if NUMBER != 1
 * if the partition table was "out of order".
   Ie, where sda2 came after sda3.

fixes http://pad.lv/1244662.
See bug for example recreates.

diff --git a/disk-utils/partx.c b/disk-utils/partx.c
index 880d779..df03e59 100644
--- a/disk-utils/partx.c
+++ b/disk-utils/partx.c
@@ -412,10 +412,41 @@ static void upd_parts_warnx(const char *device, int first, int last)
 				device, first, last);
 }

+/**
+ * get_partition_by_partno:
+ * @ls: partitions list
+ * @n: the partition number (e.g. 'N' from sda'N')
+ *
+ * This does not assume any order of the input blkid_partlist.
+ * And correctly handles "out of order" partition tables.
+ * partition N is located after partition N+1 on the disk.
+ *
+ * Returns: partition object or NULL in case or error.
+ */
+blkid_partition get_partition_by_partno(blkid_partlist ls, int n)
+{
+	int i, nparts;
+	blkid_partition par;
+	if (!ls)
+		return NULL;
+
+	nparts = blkid_partlist_numof_partitions(ls);
+	if (nparts < 0)
+		return NULL;
+
+	for (i = 0; i < nparts; i++) {
+		par = blkid_partlist_get_partition(ls, i);
+		if (n == blkid_partition_get_partno(par)) {
+			return par;
+		}
+	}
+	return NULL;
+}
+
 static int upd_parts(int fd, const char *device, dev_t devno,
 		     blkid_partlist ls, int lower, int upper)
 {
-	int i, n, an, nparts, rc = 0, errfirst = 0, errlast = 0, err;
+	int n, nparts, rc = 0, errfirst = 0, errlast = 0, err;
 	blkid_partition par;
 	uintmax_t start, size;

@@ -441,18 +472,16 @@ static int upd_parts(int fd, const char *device, dev_t devno,
 		return -1;
 	}

-	for (i = 0, n = lower; n <= upper; n++) {
-		par = blkid_partlist_get_partition(ls, i);
-		an = blkid_partition_get_partno(par);
-
-		if (lower && n < lower)
-			continue;
-		if (upper && n > upper)
+	for (n = lower; n <= upper; n++) {
+		par = get_partition_by_partno(ls, n);
+		if (!par) {
+			if (verbose)
+				warn(_("%s: no partition #%d"), device, n);
 			continue;
+		}

 		start = blkid_partition_get_start(par);
 		size =  blkid_partition_get_size(par);
-
 		if (blkid_partition_is_extended(par))
 			/*
 			 * Let's follow the Linux kernel and reduce
@@ -463,25 +492,21 @@ static int upd_parts(int fd, const char *device, dev_t devno,
 		err = partx_del_partition(fd, n);
 		if (err == -1 && errno == ENXIO)
 			err = 0; /* good, it already doesn't exist */
-		if (an == n)
+		if (err == -1 && errno == EBUSY)
 		{
-			if (i < nparts)
-				i++;
-			if (err == -1 && errno == EBUSY)
-			{
-				/* try to resize */
-				err = partx_resize_partition(fd, n, start, size);
-				if (verbose)
-					printf(_("%s: partition #%d resized\n"), device, n);
-				if (err == 0)
-					continue;
-			}
-			if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
-				if (verbose)
-					printf(_("%s: partition #%d added\n"), device, n);
+			/* try to resize */
+			err = partx_resize_partition(fd, n, start, size);
+			if (verbose)
+				printf(_("%s: partition #%d resized\n"), device, n);
+			if (err == 0)
 				continue;
-			}
 		}
+		if (err == 0 && partx_add_partition(fd, n, start, size) == 0) {
+			if (verbose)
+				printf(_("%s: partition #%d added\n"), device, n);
+			continue;
+		}
+
 		if (err == 0)
 			continue;
 		rc = -1;

             reply	other threads:[~2014-01-13 20:32 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-13 20:32 Scott Moser [this message]
2014-01-14  8:54 ` [PATCH] fix partx --update ranges and out of order tables Karel Zak
2014-01-14 14:41   ` Scott Moser
2014-01-14 14:43   ` Phillip Susi
2014-01-14 14:53     ` Scott Moser
2014-01-14 15:21       ` Phillip Susi
2014-01-15  9:55     ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.02.1401131526340.5329@brickies \
    --to=smoser@ubuntu.com \
    --cc=psusi@ubuntu.com \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox