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.
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>
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
)
39 if ((fd
= open(dev
, O_RDONLY
)) < 0) {
41 std_err(" cannot open ");
46 lseek(fd
, (off_t
) SUPER_BLOCK_BYTES
, SEEK_SET
); /* skip boot block */
47 if (read(fd
, (char *) &super
, sizeof(super
)) != sizeof(super
)) {
49 std_err(" cannot read super block on ");
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
;