From 66b86c690245714d9c59bf4487d081115f2d820b Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Wed, 29 Nov 2006 22:33:47 +0000 Subject: mkfs.minix: more cleanups, -~300 bytes of code. Added debugging support. --- util-linux/mkfs_minix.c | 406 ++++++++++++++++++++++++------------------------ 1 file changed, 204 insertions(+), 202 deletions(-) (limited to 'util-linux') diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c index e9ba50046..687cf675f 100644 --- a/util-linux/mkfs_minix.c +++ b/util-linux/mkfs_minix.c @@ -33,8 +33,8 @@ * 03.01.94 - Added support for file system valid flag. * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) * - * 30.10.94 - added support for v2 filesystem - * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) + * 30.10.94 - added support for v2 filesystem + * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) * * 09.11.94 - Added test to prevent overwrite of mounted fs adapted * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs @@ -65,32 +65,24 @@ #include "busybox.h" #include -#define MINIX_ROOT_INO 1 -#define MINIX_LINK_MAX 250 -#define MINIX2_LINK_MAX 65530 +#define DEBUG 0 -#define MINIX_I_MAP_SLOTS 8 -#define MINIX_Z_MAP_SLOTS 64 -#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ -#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ -#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */ -#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */ -#define MINIX_VALID_FS 0x0001 /* Clean fs. */ -#define MINIX_ERROR_FS 0x0002 /* fs has errors. */ - -#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) -#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode))) - -#define MINIX_V1 0x0001 /* original minix fs */ -#define MINIX_V2 0x0002 /* minix V2 fs */ - -#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version +/* If debugging, store the very same times/uids/gids for image consistency */ +#if DEBUG +# define CUR_TIME 0 +# define GETUID 0 +# define GETGID 0 +#else +# define CUR_TIME time(NULL) +# define GETUID getuid() +# define GETGID getgid() +#endif /* * This is the original minix inode layout on disk. * Note the 8-bit gid and atime and ctime. */ -struct minix_inode { +struct minix1_inode { uint16_t i_mode; uint16_t i_uid; uint32_t i_size; @@ -139,107 +131,106 @@ struct minix_dir_entry { char name[0]; }; -#define NAME_MAX 255 /* # chars in a file name */ - -#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode))) - -#define MINIX_VALID_FS 0x0001 /* Clean fs. */ -#define MINIX_ERROR_FS 0x0002 /* fs has errors. */ - -#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */ -#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */ - -#ifndef BLKGETSIZE -#define BLKGETSIZE _IO(0x12,96) /* return device size */ -#endif - - -#ifndef __linux__ -#define volatile -#endif - -#define MINIX_ROOT_INO 1 -#define MINIX_BAD_INO 2 - -#define TEST_BUFFER_BLOCKS 16 -#define MAX_GOOD_BLOCKS 512 - -#define UPPER(size,n) (((size)+((n)-1))/(n)) -#define INODE_SIZE (sizeof(struct minix_inode)) +/* Believe it or not, but mount.h has this one */ +#undef BLOCK_SIZE +enum { + BLOCK_SIZE = 1024, + BITS_PER_BLOCK = BLOCK_SIZE << 3, + + MINIX_ROOT_INO = 1, + MINIX_BAD_INO = 2, + MAX_GOOD_BLOCKS = 512, + + MINIX1_SUPER_MAGIC = 0x137F, /* original minix fs */ + MINIX1_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */ + MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */ + MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */ + MINIX_VALID_FS = 0x0001, /* clean fs */ + MINIX_ERROR_FS = 0x0002, /* fs has errors */ + + INODE_SIZE1 = sizeof(struct minix1_inode), + INODE_SIZE2 = sizeof(struct minix2_inode), + MINIX1_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix1_inode), + MINIX2_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix2_inode), + + TEST_BUFFER_BLOCKS = 16, +}; #if ENABLE_FEATURE_MINIX2 -# define INODE_SIZE2 (sizeof(struct minix2_inode)) -# define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ - : MINIX_INODES_PER_BLOCK)) +static int version2; #else -# define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) +enum { version2 = 0 }; #endif -#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) - -#define BITS_PER_BLOCK (BLOCK_SIZE<<3) - static char *device_name; -static int DEV = -1; -static uint32_t BLOCKS; +static int dev_fd = -1; +static uint32_t total_blocks; static int badblocks; /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */ static int namelen = 30; static int dirsize = 32; -static int magic = MINIX_SUPER_MAGIC2; -#if ENABLE_FEATURE_MINIX2 -static int version2; -#else -enum { version2 = 0 }; -#endif +static int magic = MINIX1_SUPER_MAGIC2; static char root_block[BLOCK_SIZE]; - -static char *inode_buffer; - -#define Inode (((struct minix_inode *) inode_buffer)-1) -#if ENABLE_FEATURE_MINIX2 -#define Inode2 (((struct minix2_inode *) inode_buffer)-1) -#endif static char super_block_buffer[BLOCK_SIZE]; static char boot_block_buffer[512]; - -#define Super (*(struct minix_super_block *)super_block_buffer) - -#define INODES (Super.s_ninodes) -#if ENABLE_FEATURE_MINIX2 -# define ZONES (version2 ? Super.s_zones : Super.s_nzones) -#else -# define ZONES (Super.s_nzones) -#endif - -#define IMAPS (Super.s_imap_blocks) -#define ZMAPS (Super.s_zmap_blocks) -#define FIRSTZONE (Super.s_firstdatazone) -#define ZONESIZE (Super.s_log_zone_size) -#define MAXSIZE (Super.s_max_size) -#define MAGIC (Super.s_magic) -#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) +static char *inode_buffer; static char *inode_map; static char *zone_map; -static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; static int used_good_blocks; +static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; static unsigned long req_nr_inodes; -static int bit(char* a, unsigned i) +extern inline unsigned div_roundup(unsigned size, unsigned n) +{ + return (size + n-1) / n; +} + +#define INODE_BUF1 (((struct minix1_inode*)inode_buffer) - 1) +#define INODE_BUF2 (((struct minix2_inode*)inode_buffer) - 1) + +#define SB (*(struct minix_super_block*)super_block_buffer) + +#define SB_INODES (SB.s_ninodes) +#define SB_IMAPS (SB.s_imap_blocks) +#define SB_ZMAPS (SB.s_zmap_blocks) +#define SB_FIRSTZONE (SB.s_firstdatazone) +#define SB_ZONE_SIZE (SB.s_log_zone_size) +#define SB_MAXSIZE (SB.s_max_size) +#define SB_MAGIC (SB.s_magic) + +#if !ENABLE_FEATURE_MINIX2 +# define SB_ZONES (SB.s_nzones) +# define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK) +#else +# define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones) +# define INODE_BLOCKS div_roundup(SB_INODES, \ + version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK) +#endif + +#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) +#define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS) + +static int bit(const char* a, unsigned i) { - return (a[i >> 3] & (1<<(i & 7))) != 0; + return a[i >> 3] & (1<<(i & 7)); } -#define inode_in_use(x) (bit(inode_map,(x))) -#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1)) -#define mark_inode(x) (setbit(inode_map,(x))) -#define unmark_inode(x) (clrbit(inode_map,(x))) +/* Note: do not assume 0/1, it is 0/nonzero */ +#define inode_in_use(x) bit(inode_map,(x)) +#define zone_in_use(x) bit(zone_map,(x)-SB_FIRSTZONE+1) + +#define mark_inode(x) setbit(inode_map,(x)) +#define unmark_inode(x) clrbit(inode_map,(x)) +#define mark_zone(x) setbit(zone_map,(x)-SB_FIRSTZONE+1) +#define unmark_zone(x) clrbit(zone_map,(x)-SB_FIRSTZONE+1) + +#ifndef BLKGETSIZE +# define BLKGETSIZE _IO(0x12,96) /* return device size */ +#endif -#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1)) -#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1)) static long valid_offset(int fd, int offset) { @@ -291,37 +282,37 @@ static int get_size(const char *file) static void write_tables(void) { /* Mark the super block valid. */ - Super.s_state |= MINIX_VALID_FS; - Super.s_state &= ~MINIX_ERROR_FS; + SB.s_state |= MINIX_VALID_FS; + SB.s_state &= ~MINIX_ERROR_FS; msg_eol = "seek to 0 failed"; - xlseek(DEV, 0, SEEK_SET); + xlseek(dev_fd, 0, SEEK_SET); msg_eol = "cannot clear boot sector"; - xwrite(DEV, boot_block_buffer, 512); + xwrite(dev_fd, boot_block_buffer, 512); msg_eol = "seek to BLOCK_SIZE failed"; - xlseek(DEV, BLOCK_SIZE, SEEK_SET); + xlseek(dev_fd, BLOCK_SIZE, SEEK_SET); msg_eol = "cannot write superblock"; - xwrite(DEV, super_block_buffer, BLOCK_SIZE); + xwrite(dev_fd, super_block_buffer, BLOCK_SIZE); msg_eol = "cannot write inode map"; - xwrite(DEV, inode_map, IMAPS * BLOCK_SIZE); + xwrite(dev_fd, inode_map, SB_IMAPS * BLOCK_SIZE); msg_eol = "cannot write zone map"; - xwrite(DEV, zone_map, ZMAPS * BLOCK_SIZE); + xwrite(dev_fd, zone_map, SB_ZMAPS * BLOCK_SIZE); msg_eol = "cannot write inodes"; - xwrite(DEV, inode_buffer, INODE_BUFFER_SIZE); + xwrite(dev_fd, inode_buffer, INODE_BUFFER_SIZE); msg_eol = "\n"; } static void write_block(int blk, char *buffer) { - xlseek(DEV, blk * BLOCK_SIZE, SEEK_SET); - xwrite(DEV, buffer, BLOCK_SIZE); + xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET); + xwrite(dev_fd, buffer, BLOCK_SIZE); } static int get_free_block(void) @@ -333,10 +324,10 @@ static int get_free_block(void) if (used_good_blocks) blk = good_blocks_table[used_good_blocks - 1] + 1; else - blk = FIRSTZONE; - while (blk < ZONES && zone_in_use(blk)) + blk = SB_FIRSTZONE; + while (blk < SB_ZONES && zone_in_use(blk)) blk++; - if (blk >= ZONES) + if (blk >= SB_ZONES) bb_error_msg_and_die("not enough good blocks"); good_blocks_table[used_good_blocks] = blk; used_good_blocks++; @@ -354,8 +345,8 @@ static void mark_good_blocks(void) static int next(int zone) { if (!zone) - zone = FIRSTZONE - 1; - while (++zone < ZONES) + zone = SB_FIRSTZONE - 1; + while (++zone < SB_ZONES) if (zone_in_use(zone)) return zone; return 0; @@ -363,7 +354,7 @@ static int next(int zone) static void make_bad_inode(void) { - struct minix_inode *inode = &Inode[MINIX_BAD_INO]; + struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO]; int i, j, zone; int ind = 0, dind = 0; unsigned short ind_block[BLOCK_SIZE >> 1]; @@ -377,7 +368,7 @@ static void make_bad_inode(void) inode->i_nlinks = 1; /* BTW, setting this makes all images different */ /* it's harder to check for bugs then - diff isn't helpful :(... */ - inode->i_time = time(NULL); + inode->i_time = CUR_TIME; inode->i_mode = S_IFREG + 0000; inode->i_size = badblocks * BLOCK_SIZE; zone = next(0); @@ -406,7 +397,7 @@ static void make_bad_inode(void) } } bb_error_msg_and_die("too many bad blocks"); - end_bad: + end_bad: if (ind) write_block(ind, (char *) ind_block); if (dind) @@ -416,7 +407,7 @@ static void make_bad_inode(void) #if ENABLE_FEATURE_MINIX2 static void make_bad_inode2(void) { - struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; + struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO]; int i, j, zone; int ind = 0, dind = 0; unsigned long ind_block[BLOCK_SIZE >> 2]; @@ -426,7 +417,7 @@ static void make_bad_inode2(void) return; mark_inode(MINIX_BAD_INO); inode->i_nlinks = 1; - inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); + inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; inode->i_mode = S_IFREG + 0000; inode->i_size = badblocks * BLOCK_SIZE; zone = next(0); @@ -456,7 +447,7 @@ static void make_bad_inode2(void) } /* Could make triple indirect block here */ bb_error_msg_and_die("too many bad blocks"); - end_bad: + end_bad: if (ind) write_block(ind, (char *) ind_block); if (dind) @@ -466,12 +457,12 @@ static void make_bad_inode2(void) static void make_root_inode(void) { - struct minix_inode *inode = &Inode[MINIX_ROOT_INO]; + struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO]; mark_inode(MINIX_ROOT_INO); inode->i_zone[0] = get_free_block(); inode->i_nlinks = 2; - inode->i_time = time(NULL); + inode->i_time = CUR_TIME; if (badblocks) inode->i_size = 3 * dirsize; else { @@ -480,21 +471,21 @@ static void make_root_inode(void) inode->i_size = 2 * dirsize; } inode->i_mode = S_IFDIR + 0755; - inode->i_uid = getuid(); + inode->i_uid = GETUID; if (inode->i_uid) - inode->i_gid = getgid(); + inode->i_gid = GETGID; write_block(inode->i_zone[0], root_block); } #if ENABLE_FEATURE_MINIX2 static void make_root_inode2(void) { - struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; + struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO]; mark_inode(MINIX_ROOT_INO); inode->i_zone[0] = get_free_block(); inode->i_nlinks = 2; - inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); + inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME; if (badblocks) inode->i_size = 3 * dirsize; else { @@ -503,9 +494,9 @@ static void make_root_inode2(void) inode->i_size = 2 * dirsize; } inode->i_mode = S_IFDIR + 0755; - inode->i_uid = getuid(); + inode->i_uid = GETUID; if (inode->i_uid) - inode->i_gid = getgid(); + inode->i_gid = GETGID; write_block(inode->i_zone[0], root_block); } #endif @@ -517,65 +508,64 @@ static void setup_tables(void) memset(super_block_buffer, 0, BLOCK_SIZE); memset(boot_block_buffer, 0, 512); - MAGIC = magic; - ZONESIZE = 0; - MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; - if (version2) { - Super.s_zones = BLOCKS; - } else - Super.s_nzones = BLOCKS; + SB_MAGIC = magic; + SB_ZONE_SIZE = 0; + SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; + if (version2) + SB.s_zones = total_blocks; + else + SB.s_nzones = total_blocks; -/* some magic nrs: 1 inode / 3 blocks */ + /* some magic nrs: 1 inode / 3 blocks */ if (req_nr_inodes == 0) - inodes = BLOCKS / 3; + inodes = total_blocks / 3; else inodes = req_nr_inodes; /* Round up inode count to fill block size */ if (version2) - inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & - ~(MINIX2_INODES_PER_BLOCK - 1)); + inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) & + ~(MINIX2_INODES_PER_BLOCK - 1); else - inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & - ~(MINIX_INODES_PER_BLOCK - 1)); + inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) & + ~(MINIX1_INODES_PER_BLOCK - 1); if (inodes > 65535) inodes = 65535; - INODES = inodes; - IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK); - ZMAPS = 0; - i = 0; - while (ZMAPS != - UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, - BITS_PER_BLOCK) && i < 1000) { - ZMAPS = - UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, - BITS_PER_BLOCK); - i++; - } + SB_INODES = inodes; + SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK); + /* Real bad hack but overwise mkfs.minix can be thrown * in infinite loop... * try: * dd if=/dev/zero of=test.fs count=10 bs=1024 - * /sbin/mkfs.minix -i 200 test.fs - * */ - if (i >= 999) { - bb_error_msg_and_die("cannot allocate buffers for maps"); - } - FIRSTZONE = NORM_FIRSTZONE; - inode_map = xmalloc(IMAPS * BLOCK_SIZE); - zone_map = xmalloc(ZMAPS * BLOCK_SIZE); - memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); - memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); - for (i = FIRSTZONE; i < ZONES; i++) + * mkfs.minix -i 200 test.fs + */ + /* This code is not insane: NORM_FIRSTZONE is not a constant, */ + /* it uses previous value of SB_ZMAPS inside */ + i = 999; + SB_ZMAPS = 0; + do { + uint16_t t = div_roundup(total_blocks - NORM_FIRSTZONE + 1, BITS_PER_BLOCK); + if (SB_ZMAPS == t) goto got_it; + SB_ZMAPS = t; + } while (--i); + bb_error_msg_and_die("incompatible size/inode count, try different -i N"); + got_it: + + SB_FIRSTZONE = NORM_FIRSTZONE; + inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE); + zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE); + memset(inode_map, 0xff, SB_IMAPS * BLOCK_SIZE); + memset(zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE); + for (i = SB_FIRSTZONE; i < SB_ZONES; i++) unmark_zone(i); - for (i = MINIX_ROOT_INO; i <= INODES; i++) + for (i = MINIX_ROOT_INO; i <= SB_INODES; i++) unmark_inode(i); - inode_buffer = xmalloc(INODE_BUFFER_SIZE); - memset(inode_buffer, 0, INODE_BUFFER_SIZE); - printf("%ld inodes\n", (long)INODES); - printf("%ld blocks\n", (long)ZONES); - printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE); - printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); - printf("Maxsize=%ld\n\n", (long)MAXSIZE); + inode_buffer = xzalloc(INODE_BUFFER_SIZE); + printf("%ld inodes\n", (long)SB_INODES); + printf("%ld blocks\n", (long)SB_ZONES); + printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)NORM_FIRSTZONE); + printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE); + printf("Maxsize=%ld\n", (long)SB_MAXSIZE); } /* @@ -588,11 +578,11 @@ static long do_check(char *buffer, int try, unsigned current_block) /* Seek to the correct loc. */ msg_eol = "seek failed during testing of blocks"; - xlseek(DEV, current_block * BLOCK_SIZE, SEEK_SET); + xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET); msg_eol = "\n"; /* Try the read */ - got = read(DEV, buffer, try * BLOCK_SIZE); + got = read(dev_fd, buffer, try * BLOCK_SIZE); if (got < 0) got = 0; if (got & (BLOCK_SIZE - 1)) { @@ -606,7 +596,7 @@ static unsigned currently_testing; static void alarm_intr(int alnum) { - if (currently_testing >= ZONES) + if (currently_testing >= SB_ZONES) return; signal(SIGALRM, alarm_intr); alarm(5); @@ -624,18 +614,18 @@ static void check_blocks(void) currently_testing = 0; signal(SIGALRM, alarm_intr); alarm(5); - while (currently_testing < ZONES) { + while (currently_testing < SB_ZONES) { msg_eol = "seek failed in check_blocks"; - xlseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET); + xlseek(dev_fd, currently_testing * BLOCK_SIZE, SEEK_SET); msg_eol = "\n"; try = TEST_BUFFER_BLOCKS; - if (currently_testing + try > ZONES) - try = ZONES - currently_testing; + if (currently_testing + try > SB_ZONES) + try = SB_ZONES - currently_testing; got = do_check(buffer, try, currently_testing); currently_testing += got; if (got == try) continue; - if (currently_testing < FIRSTZONE) + if (currently_testing < SB_FIRSTZONE) bb_error_msg_and_die("bad blocks before data-area: cannot make fs"); mark_zone(currently_testing); badblocks++; @@ -660,13 +650,14 @@ static void get_list_blocks(char *filename) int mkfs_minix_main(int argc, char **argv) { + struct mntent *mp; unsigned opt; char *tmp; struct stat statbuf; char *str_i, *str_n; char *listfile = NULL; - if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) + if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) bb_error_msg_and_die("bad inode size"); #if ENABLE_FEATURE_MINIX2 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) @@ -680,8 +671,8 @@ int mkfs_minix_main(int argc, char **argv) //if (opt & 4) -l if (opt & 8) { // -n namelen = xatoi_u(str_n); - if (namelen == 14) magic = MINIX_SUPER_MAGIC; - else if (namelen == 30) magic = MINIX_SUPER_MAGIC2; + if (namelen == 14) magic = MINIX1_SUPER_MAGIC; + else if (namelen == 30) magic = MINIX1_SUPER_MAGIC2; else bb_show_usage(); dirsize = namelen + 2; } @@ -697,23 +688,42 @@ int mkfs_minix_main(int argc, char **argv) device_name = *argv++; if (!device_name) bb_show_usage(); - if (*argv) { - BLOCKS = xatou32(*argv); - if (BLOCKS < 10) - bb_show_usage(); - } else - BLOCKS = get_size(device_name) / 1024; + if (*argv) + total_blocks = xatou32(*argv); + else + total_blocks = get_size(device_name) / 1024; + + if (total_blocks < 10) + bb_error_msg_and_die("must have at least 10 blocks"); if (version2) { magic = MINIX2_SUPER_MAGIC2; if (namelen == 14) magic = MINIX2_SUPER_MAGIC; - } else if (BLOCKS > 65535) - BLOCKS = 65535; + } else if (total_blocks > 65535) + total_blocks = 65535; + + /* Check if it is mounted */ + mp = find_mount_point(device_name, NULL); + if (mp && strcmp(device_name, mp->mnt_fsname) == 0) + bb_error_msg_and_die("%s is mounted on %s; " + "refusing to make a filesystem", + device_name, mp->mnt_dir); + + dev_fd = xopen(device_name, O_RDWR); + if (fstat(dev_fd, &statbuf) < 0) + bb_error_msg_and_die("cannot stat %s", device_name); + if (!S_ISBLK(statbuf.st_mode)) + opt &= ~1; // clear -c (check) - if (find_mount_point(device_name, NULL)) - bb_error_msg_and_die("%s is mounted; refusing to make " - "a filesystem", device_name); +/* I don't know why someone has special code to prevent mkfs.minix + * on IDE devices. Why IDE but not SCSI, etc?... */ +#if 0 + else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) + /* what is this? */ + bb_error_msg_and_die("will not try " + "to make filesystem on '%s'", device_name); +#endif tmp = root_block; *(short *) tmp = 1; @@ -725,14 +735,6 @@ int mkfs_minix_main(int argc, char **argv) *(short *) tmp = 2; strcpy(tmp + 2, ".badblocks"); - DEV = xopen(device_name, O_RDWR); - if (fstat(DEV, &statbuf) < 0) - bb_error_msg_and_die("cannot stat %s", device_name); - if (!S_ISBLK(statbuf.st_mode)) - opt &= ~1; // clear -c (check) - else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) - bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name); - setup_tables(); if (opt & 1) // -c ? -- cgit v1.2.3