]> git.ipfire.org Git - thirdparty/mdadm.git/blob - swap_super.c
Remove lots of unnecessary white space.
[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 for (i=0; i < 4096 ; i+=4) {
51 char t = super[i];
52 super[i] = super[i+3];
53 super[i+3] = t;
54 t=super[i+1];
55 super[i+1]=super[i+2];
56 super[i+2]=t;
57 }
58 /* swap the u64 events counters */
59 for (i=0; i<4; i++) {
60 /* events_hi and events_lo */
61 char t=super[32*4+7*4 +i];
62 super[32*4+7*4 +i] = super[32*4+8*4 +i];
63 super[32*4+8*4 +i] = t;
64
65 /* cp_events_hi and cp_events_lo */
66 t=super[32*4+9*4 +i];
67 super[32*4+9*4 +i] = super[32*4+10*4 +i];
68 super[32*4+10*4 +i] = t;
69 }
70
71 if (lseek64(fd, offset, 0) < 0LL) {
72 perror("lseek64");
73 exit(1);
74 }
75 if (write(fd, super, 4096) != 4096) {
76 perror("write");
77 exit(1);
78 }
79 exit(0);
80
81 }