custom message type for VM_INFO
[minix3.git] / lib / libminlib / fsversion.c
blob49418995bb06c484a300c96e13f504b5022dc1b9
1 /* This procedure examines a file system and figures out whether it is
2 * version 1 or version 2. It returns the result as an int. If the
3 * file system is neither, it returns -1. A typical call is:
5 * n = fsversion("/dev/hd1", "df");
7 * The first argument is the special file for the file system.
8 * The second is the program name, which is used in error messages.
9 */
11 #include <sys/types.h>
12 #include <minix/config.h>
13 #include <minix/const.h>
14 #include <minix/minlib.h>
15 #include <minix/type.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <string.h>
21 #include "mfs/const.h"
23 static char super[SUPER_BLOCK_BYTES];
25 #define MAGIC_OFFSET_MFS 0x18
26 #define MAGIC_OFFSET_EXT 0x38
27 #define MAGIC_VALUE_EXT2 0xef53
29 static int check_super(off_t offset, unsigned short magic)
31 return (memcmp(super + offset, &magic, sizeof(magic)) == 0) ? 1 : 0;
34 int fsversion(dev, prog)
35 char *dev, *prog;
37 int fd;
39 if ((fd = open(dev, O_RDONLY)) < 0) {
40 std_err(prog);
41 std_err(" cannot open ");
42 perror(dev);
43 return(-1);
46 lseek(fd, (off_t) SUPER_BLOCK_BYTES, SEEK_SET); /* skip boot block */
47 if (read(fd, (char *) &super, sizeof(super)) != sizeof(super)) {
48 std_err(prog);
49 std_err(" cannot read super block on ");
50 perror(dev);
51 close(fd);
52 return(-1);
54 close(fd);
56 /* first check MFS, a valid MFS may look like EXT but not vice versa */
57 if (check_super(MAGIC_OFFSET_MFS, SUPER_MAGIC)) return FSVERSION_MFS1;
58 if (check_super(MAGIC_OFFSET_MFS, SUPER_V2)) return FSVERSION_MFS2;
59 if (check_super(MAGIC_OFFSET_MFS, SUPER_V3)) return FSVERSION_MFS3;
60 if (check_super(MAGIC_OFFSET_EXT, MAGIC_VALUE_EXT2)) return FSVERSION_EXT2;
62 return(-1);