summaryrefslogtreecommitdiff
path: root/swap_super.c
diff options
context:
space:
mode:
authorMichael Tokarev <mjt@tls.msk.ru>2014-12-20 08:48:44 +0000
committerMichael Tokarev <mjt@tls.msk.ru>2014-12-20 08:48:44 +0000
commit489bea7ee8e1dbecfa517b8415568044ab57c73a (patch)
tree44d4878d4c7da3f4908ea9a765ef9b8f9c141756 /swap_super.c
mdadm (3.3.2-5) unstable; urgency=medium
* use-tempnode-not-devnode.patch: change udev rules file to use $tempnode which works both on wheezy and jessie udev, instead of $devnode which only works in jessie. At this stage it is better to make rules file compatible with old version instead of adding versioned dependency. Should be removed for jessie+1. (Closes: #770883) * fix Closes: list in previous entry (Closes: #771852) # imported from the archive
Diffstat (limited to 'swap_super.c')
-rw-r--r--swap_super.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/swap_super.c b/swap_super.c
new file mode 100644
index 00000000..b6db5743
--- /dev/null
+++ b/swap_super.c
@@ -0,0 +1,81 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <sys/mount.h>
+/*
+ * This is a tiny test program to endian-swap
+ * the superblock on a given device.
+ * We simply read 4k from where the superblock should be
+ * do the swap, and write it back
+ * Don't use this on a real array, use mdadm.
+ */
+
+#define MD_RESERVED_BYTES (64 * 1024)
+#define MD_RESERVED_SECTORS (MD_RESERVED_BYTES / 512)
+
+#define MD_NEW_SIZE_SECTORS(x) ((x & ~(MD_RESERVED_SECTORS - 1)) - MD_RESERVED_SECTORS)
+
+extern long long lseek64(int, long long, int);
+
+int main(int argc, char *argv[])
+{
+ int fd, i;
+ unsigned long size;
+ unsigned long long offset;
+ char super[4096];
+ if (argc != 2) {
+ fprintf(stderr, "Usage: swap_super device\n");
+ exit(1);
+ }
+ fd = open(argv[1], O_RDWR);
+ if (fd<0) {
+ perror(argv[1]);
+ exit(1);
+ }
+ if (ioctl(fd, BLKGETSIZE, &size)) {
+ perror("BLKGETSIZE");
+ exit(1);
+ }
+ offset = MD_NEW_SIZE_SECTORS(size) * 512LL;
+ if (lseek64(fd, offset, 0) < 0LL) {
+ perror("lseek64");
+ exit(1);
+ }
+ if (read(fd, super, 4096) != 4096) {
+ perror("read");
+ exit(1);
+ }
+
+ for (i=0; i < 4096 ; i+=4) {
+ char t = super[i];
+ super[i] = super[i+3];
+ super[i+3] = t;
+ t=super[i+1];
+ super[i+1]=super[i+2];
+ super[i+2]=t;
+ }
+ /* swap the u64 events counters */
+ for (i=0; i<4; i++) {
+ /* events_hi and events_lo */
+ char t=super[32*4+7*4 +i];
+ super[32*4+7*4 +i] = super[32*4+8*4 +i];
+ super[32*4+8*4 +i] = t;
+
+ /* cp_events_hi and cp_events_lo */
+ t=super[32*4+9*4 +i];
+ super[32*4+9*4 +i] = super[32*4+10*4 +i];
+ super[32*4+10*4 +i] = t;
+ }
+
+ if (lseek64(fd, offset, 0) < 0LL) {
+ perror("lseek64");
+ exit(1);
+ }
+ if (write(fd, super, 4096) != 4096) {
+ perror("write");
+ exit(1);
+ }
+ exit(0);
+
+}