mkfs.minix: more cleanups, -~300 bytes of code.

Added debugging support.
This commit is contained in:
Denis Vlasenko 2006-11-29 22:33:47 +00:00
parent c6ce8733dd
commit 66b86c6902

View File

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