Check-in swap_super.c

and update a test that uses it.

Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au>
This commit is contained in:
Neil Brown 2005-08-18 06:02:33 +00:00
parent c9fae71049
commit eb3b43aa2a
2 changed files with 85 additions and 2 deletions

83
swap_super.c Normal file
View File

@ -0,0 +1,83 @@
#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)
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);
}

View File

@ -5,11 +5,11 @@ mdadm -CR $md0 -l5 -n4 $dev0 $dev1 $dev2 $dev3
sleep 4
mdadm -S $md0
mdadm -E --metadata=0 $dev1 | grep -v Events > $targetdir/d1
mdadm -E --metadata=0 $dev1 > $targetdir/d1
for d in $dev0 $dev1 $dev2 $dev3
do $dir/swap_super $d
done
mdadm -E --metadata=0.swap $dev1 | grep -v Events > $targetdir/d1s
mdadm -E --metadata=0.swap $dev1 > $targetdir/d1s
diff -u $targetdir/d1 $targetdir/d1s
mdadm --assemble --update=byteorder $md0 $dev0 $dev1 $dev2 $dev3