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: 4 * 5 * n = fsversion("/dev/hd1", "df"); 6 * 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 */ 10 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> 20 21 #include "mfs/const.h" 22 23 static char super[SUPER_BLOCK_BYTES]; 24 25 #define MAGIC_OFFSET_MFS 0x18 26 #define MAGIC_OFFSET_EXT 0x38 27 #define MAGIC_VALUE_EXT2 0xef53 28 29 static int check_super(off_t offset, unsigned short magic) 30 { 31 return (memcmp(super + offset, &magic, sizeof(magic)) == 0) ? 1 : 0; 32 } 33 34 int fsversion(dev, prog) 35 char *dev, *prog; 36 { 37 int fd; 38 39 if ((fd = open(dev, O_RDONLY)) < 0) { 40 std_err(prog); 41 std_err(" cannot open "); 42 perror(dev); 43 return(-1); 44 } 45 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); 53 } 54 close(fd); 55 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; 61 62 return(-1); 63 } 64