]> git.ipfire.org Git - thirdparty/mdadm.git/blob - swap_super.c
tests/06name: adjust for homehost
[thirdparty/mdadm.git] / swap_super.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <sys/mount.h>
6 /*
7 * This is a tiny test program to endian-swap
8 * the superblock on a given device.
9 * We simply read 4k from where the superblock should be
10 * do the swap, and write it back
11 * Don't use this on a real array, use mdadm.
12 */
13
14 #define MD_RESERVED_BYTES (64 * 1024)
15 #define MD_RESERVED_SECTORS (MD_RESERVED_BYTES / 512)
16
17 #define MD_NEW_SIZE_SECTORS(x) ((x & ~(MD_RESERVED_SECTORS - 1)) - MD_RESERVED_SECTORS)
18
19 extern long long lseek64(int, long long, int);
20
21 int main(int argc, char *argv[])
22 {
23 int fd, i;
24 unsigned long size;
25 unsigned long long offset;
26 char super[4096];
27 if (argc != 2) {
28 fprintf(stderr, "Usage: swap_super device\n");
29 exit(1);
30 }
31 fd = open(argv[1], O_RDWR);
32 if (fd<0) {
33 perror(argv[1]);
34 exit(1);
35 }
36 if (ioctl(fd, BLKGETSIZE, &size)) {
37 perror("BLKGETSIZE");
38 exit(1);
39 }
40 offset = MD_NEW_SIZE_SECTORS(size) * 512LL;
41 if (lseek64(fd, offset, 0) < 0LL) {
42 perror("lseek64");
43 exit(1);
44 }
45 if (read(fd, super, 4096) != 4096) {
46 perror("read");
47 exit(1);
48 }
49
50
51 for (i=0; i < 4096 ; i+=4) {
52 char t = super[i];
53 super[i] = super[i+3];
54 super[i+3] = t;
55 t=super[i+1];
56 super[i+1]=super[i+2];
57 super[i+2]=t;
58 }
59 /* swap the u64 events counters */
60 for (i=0; i<4; i++) {
61 /* events_hi and events_lo */
62 char t=super[32*4+7*4 +i];
63 super[32*4+7*4 +i] = super[32*4+8*4 +i];
64 super[32*4+8*4 +i] = t;
65
66 /* cp_events_hi and cp_events_lo */
67 t=super[32*4+9*4 +i];
68 super[32*4+9*4 +i] = super[32*4+10*4 +i];
69 super[32*4+10*4 +i] = t;
70 }
71
72
73 if (lseek64(fd, offset, 0) < 0LL) {
74 perror("lseek64");
75 exit(1);
76 }
77 if (write(fd, super, 4096) != 4096) {
78 perror("write");
79 exit(1);
80 }
81 exit(0);
82
83 }
84
85