set_ext4_err_bit.c revision 2633ce5ca5024d5565c9068c29ec39a8c3ed10e9
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <fcntl.h>
4#include <stdio.h>
5#include <stdlib.h>
6
7#define SB_OFFSET 1024
8#define SB_SIZE 1024
9#define EXT4_MAGIC_OFFSET 0x38
10#define EXT4_STATE_OFFSET 0x3A
11
12int main(int argc, char *argv[])
13{
14    int fd;
15    char me[] = "set_ext4_err_bit";
16    unsigned char sb[1024];
17
18    if (argc != 2) {
19        fprintf(stderr, "%s: Usage: %s <ext4_block_device>\n", me, me);
20        exit(1);
21    }
22
23    fd = open(argv[1], O_RDWR);
24
25    if (fd < 0) {
26        fprintf(stderr, "%s: Cannot open block device %s\n", me, argv[1]);
27        exit(1);
28    }
29
30    if (lseek(fd, SB_OFFSET, SEEK_SET) == -1) {
31        fprintf(stderr, "%s: Cannot lseek to superblock to read\n", me);
32        exit(1);
33    }
34
35    if (read(fd, sb, SB_SIZE) != SB_SIZE) {
36        fprintf(stderr, "%s: Cannot read superblock\n", me);
37        exit(1);
38    }
39
40    if ((sb[EXT4_MAGIC_OFFSET] != 0x53) || (sb[EXT4_MAGIC_OFFSET+1] != 0xEF)) {
41        fprintf(stderr, "%s: invalid superblock magic\n", me);
42        exit(1);
43    }
44
45    /* Set the errors detected bit */
46    sb[EXT4_STATE_OFFSET] |= 0x2;
47
48    if (lseek(fd, SB_OFFSET, SEEK_SET) == -1) {
49        fprintf(stderr, "%s: Cannot lseek to superblock to write\n", me);
50        exit(1);
51    }
52
53    if (write(fd, sb, SB_SIZE) != SB_SIZE) {
54        fprintf(stderr, "%s: Cannot write superblock\n", me);
55        exit(1);
56    }
57
58    close(fd);
59
60    return 0;
61}
62
63