[PATCH] swapon: add pagesize detection

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



As swap format depends on the pagesize being used, it may happen
that the pagesize of the swap space and the current pagesize differ,
resulting in swapon to fail when trying to enable such a swap space.
In such a case swapon should rather reinitialize the swap space.

Signed-off-by: Matthias Koenig <mkoenig@xxxxxxx>
---

 0 files changed, 0 insertions(+), 0 deletions(-)

diff --git a/mount/swapon.c b/mount/swapon.c
index eb0eb4a..dc4ca81 100644
--- a/mount/swapon.c
+++ b/mount/swapon.c
@@ -238,11 +238,87 @@ swap_reinitialize(const char *device) {
 	return -1; /* error */
 }
 
+enum {
+	SWAP_V0 = 1,
+	SWAP_V1,
+	SWAP_SUSPEND,
+	SWAP_ULSUSPEND,
+	SWAP_OOTSUSPEND
+};
+
+static int
+swap_detect_signature(const char *buf)
+{
+	if (memcmp(buf, "SWAP-SPACE", 10) == 0)
+		return SWAP_V0;
+	else if (memcmp(buf, "SWAPSPACE2", 10) == 0)
+		return SWAP_V1;
+	else if	(memcmp(buf, "S1SUSPEND", 9) == 0)
+		return SWAP_SUSPEND;
+	else if	(memcmp(buf, "ULSUSPEND", 9) == 0)
+		return SWAP_ULSUSPEND;
+	else if (memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0)
+		return SWAP_OOTSUSPEND;
+
+	return 0;
+}
+
+#define MAX_PAGESIZE	(16 * 1024)
+
+/* return the pagesize the swap format has been built with
+ * as swap metadata depends on the pagesize, we have to
+ * reinitialize if it does not match with the current pagesize
+ * returns 0 if not a valid swap format
+ */
+static unsigned int
+swap_get_pagesize(const char *dev)
+{
+	unsigned int page;
+	int fd;
+	char *buf;
+	ssize_t n;
+	unsigned int ret = 0;
+
+	fd = open(dev, O_RDONLY);
+	if (fd == -1)
+		return 0;
+
+	buf = malloc(MAX_PAGESIZE);
+	if (!buf) {
+		perror("malloc");
+		goto err;
+	}
+
+	if (read(fd, buf, MAX_PAGESIZE) == (ssize_t)-1) {
+		perror("read");
+		goto err1;
+	}
+
+	for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) {
+		char *off = buf + page - 10;
+		if (swap_detect_signature(off)) {
+			if (verbose > 1)
+				printf("detected swap pagesize = %u\n",
+				       page);
+			ret = page;
+			break;
+		}
+	}
+
+err1:
+	free(buf);
+err:
+	close(fd);
+	return ret;
+}
+
 static int
 do_swapon(const char *orig_special, int prio, int canonic) {
 	int status;
+	int reinitialize = 0;
 	struct stat st;
 	const char *special = orig_special;
+	unsigned int pagesize = sysconf(_SC_PAGESIZE);
 
 	if (verbose)
 		printf(_("%s on %s\n"), progname, orig_special);
@@ -260,6 +336,14 @@ do_swapon(const char *orig_special, int prio, int canonic) {
 		return -1;
 	}
 
+	if (pagesize != swap_get_pagesize(special)) {
+		if (verbose)
+			printf(_("%s: %s: swap format pagesize does not match."
+				 " Reinitializing the swap.\n"),
+				 progname, special);
+		reinitialize = 1;
+	}
+
 	/* We have to reinitialize swap with old (=useless) software suspend
 	 * data. The problem is that if we don't do it, then we get data
 	 * corruption the next time an attempt at unsuspending is made.
@@ -268,6 +352,10 @@ do_swapon(const char *orig_special, int prio, int canonic) {
 		fprintf(stdout, _("%s: %s: software suspend data detected. "
 					"Reinitializing the swap.\n"),
 			progname, special);
+		reinitialize = 1;
+	}
+
+	if (reinitialize) {
 		if (swap_reinitialize(special) < 0)
 			return -1;
 	}

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux