]> git.ipfire.org Git - thirdparty/mdadm.git/blame - swap_super.c
Add raid10 doco to mdadm.8
[thirdparty/mdadm.git] / swap_super.c
CommitLineData
eb3b43aa
NB
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
19main(int argc, char *argv[])
20{
21 int fd, i;
22 unsigned long size;
23 unsigned long long offset;
24 char super[4096];
25 if (argc != 2) {
26 fprintf(stderr, "Usage: swap_super device\n");
27 exit(1);
28 }
29 fd = open(argv[1], O_RDWR);
30 if (fd<0) {
31 perror(argv[1]);
32 exit(1);
33 }
34 if (ioctl(fd, BLKGETSIZE, &size)) {
35 perror("BLKGETSIZE");
36 exit(1);
37 }
38 offset = MD_NEW_SIZE_SECTORS(size) * 512LL;
39 if (lseek64(fd, offset, 0) < 0LL) {
40 perror("lseek64");
41 exit(1);
42 }
43 if (read(fd, super, 4096) != 4096) {
44 perror("read");
45 exit(1);
46 }
47
48
49 for (i=0; i < 4096 ; i+=4) {
50 char t = super[i];
51 super[i] = super[i+3];
52 super[i+3] = t;
53 t=super[i+1];
54 super[i+1]=super[i+2];
55 super[i+2]=t;
56 }
57 /* swap the u64 events counters */
58 for (i=0; i<4; i++) {
59 /* events_hi and events_lo */
60 char t=super[32*4+7*4 +i];
61 super[32*4+7*4 +i] = super[32*4+8*4 +i];
62 super[32*4+8*4 +i] = t;
63
64 /* cp_events_hi and cp_events_lo */
65 t=super[32*4+9*4 +i];
66 super[32*4+9*4 +i] = super[32*4+10*4 +i];
67 super[32*4+10*4 +i] = t;
68 }
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}
82
83