(root)/
util-linux-2.39/
disk-utils/
mkfs.minix.c
       1  /*
       2   * mkfs.minix.c - make a linux (minix) file-system.
       3   *
       4   * (C) 1991 Linus Torvalds. This file may be redistributed as per
       5   * the Linux copyright.
       6   */
       7  
       8  /*
       9   * DD.MM.YY
      10   *
      11   * 24.11.91  -	Time began. Used the fsck sources to get started.
      12   *
      13   * 25.11.91  -	Corrected some bugs. Added support for ".badblocks"
      14   *		The algorithm for ".badblocks" is a bit weird, but
      15   *		it should work. Oh, well.
      16   *
      17   * 25.01.92  -	Added the -l option for getting the list of bad blocks
      18   *		out of a named file. (Dave Rivers, rivers@ponds.uucp)
      19   *
      20   * 28.02.92  -	Added %-information when using -c.
      21   *
      22   * 28.02.93  -	Added support for other namelengths than the original
      23   *		14 characters so that I can test the new kernel routines..
      24   *
      25   * 09.10.93  -	Make exit status conform to that required by fsutil
      26   *		(Rik Faith, faith@cs.unc.edu)
      27   *
      28   * 31.10.93  -	Added inode request feature, for backup floppies: use
      29   *		32 inodes, for a news partition use more.
      30   *		(Scott Heavner, sdh@po.cwru.edu)
      31   *
      32   * 03.01.94  -	Added support for file system valid flag.
      33   *		(Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
      34   *
      35   * 30.10.94  -  Added support for v2 filesystem
      36   *		(Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
      37   *
      38   * 09.11.94  -	Added test to prevent overwrite of mounted fs adapted
      39   *		from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
      40   *		program.  (Daniel Quinlan, quinlan@yggdrasil.com)
      41   *
      42   * 03.20.95  -	Clear first 512 bytes of filesystem to make certain that
      43   *		the filesystem is not misidentified as a MS-DOS FAT filesystem.
      44   *		(Daniel Quinlan, quinlan@yggdrasil.com)
      45   *
      46   * 02.07.96  -  Added small patch from Russell King to make the program a
      47   *		good deal more portable (janl@math.uio.no)
      48   *
      49   * 06.29.11  -  Overall cleanups for util-linux and v3 support
      50   *              Davidlohr Bueso <dave@gnu.org>
      51   *
      52   * 06.20.15  -  Do not infinite loop or crash on large devices
      53   *              Joshua Hudson <joshudson@gmail.com>
      54   *
      55   */
      56  
      57  #include <stdio.h>
      58  #include <time.h>
      59  #include <unistd.h>
      60  #include <string.h>
      61  #include <signal.h>
      62  #include <fcntl.h>
      63  #include <ctype.h>
      64  #include <stdlib.h>
      65  #include <termios.h>
      66  #include <sys/stat.h>
      67  #include <getopt.h>
      68  #include <err.h>
      69  
      70  #include "blkdev.h"
      71  #include "minix_programs.h"
      72  #include "nls.h"
      73  #include "pathnames.h"
      74  #include "bitops.h"
      75  #include "exitcodes.h"
      76  #include "strutils.h"
      77  #include "all-io.h"
      78  #include "closestream.h"
      79  #include "ismounted.h"
      80  
      81  #define XALLOC_EXIT_CODE MKFS_EX_ERROR
      82  #include "xalloc.h"
      83  
      84  #define MINIX_ROOT_INO 1
      85  #define MINIX_BAD_INO 2
      86  
      87  #define TEST_BUFFER_BLOCKS 16
      88  #define MAX_GOOD_BLOCKS 512
      89  
      90  #define MINIX_MAX_INODES 65535
      91  
      92  #define DEFAULT_FS_VERSION 1
      93  
      94  /*
      95   * Global variables used in minix_programs.h inline functions
      96   */
      97  int fs_version = DEFAULT_FS_VERSION;
      98  char *super_block_buffer;
      99  
     100  static char *inode_buffer = NULL;
     101  
     102  #define Inode (((struct minix_inode *) inode_buffer) - 1)
     103  #define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
     104  
     105  struct fs_control {
     106  	char *device_name;		/* device on a Minix file system is created */
     107  	int device_fd;			/* open file descriptor of the device */
     108  	char *lockmode;			/* as specified by --lock */
     109  	unsigned long long fs_blocks;	/* device block count for the file system */
     110  	int fs_used_blocks;		/* used blocks on a device */
     111  	int fs_bad_blocks;		/* number of bad blocks found from device */
     112  	uint16_t fs_namelen;		/* maximum length of filenames */
     113  	size_t fs_dirsize;		/* maximum size of directory */
     114  	unsigned long fs_inodes;	/* number of inodes */
     115  	int fs_magic;			/* file system magic number */
     116  	unsigned int
     117  	 check_blocks:1;		/* check for bad blocks */
     118  };
     119  
     120  static char root_block[MINIX_BLOCK_SIZE];
     121  static char boot_block_buffer[512];
     122  static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
     123  
     124  static char *inode_map;
     125  static char *zone_map;
     126  
     127  #define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0)
     128  
     129  #define mark_inode(x) (setbit(inode_map,(x)))
     130  #define unmark_inode(x) (clrbit(inode_map,(x)))
     131  
     132  #define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1))
     133  #define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1))
     134  
     135  static void __attribute__((__noreturn__)) usage(void)
     136  {
     137  	FILE *out = stdout;
     138  	fputs(USAGE_HEADER, out);
     139  	fprintf(out, _(" %s [options] /dev/name [blocks]\n"), program_invocation_short_name);
     140  	fputs(USAGE_OPTIONS, out);
     141  	fputs(_(" -1                      use Minix version 1\n"), out);
     142  	fputs(_(" -2, -v                  use Minix version 2\n"), out);
     143  	fputs(_(" -3                      use Minix version 3\n"), out);
     144  	fputs(_(" -n, --namelength <num>  maximum length of filenames\n"), out);
     145  	fputs(_(" -i, --inodes <num>      number of inodes for the filesystem\n"), out);
     146  	fputs(_(" -c, --check             check the device for bad blocks\n"), out);
     147  	fputs(_(" -l, --badblocks <file>  list of bad blocks from file\n"), out);
     148  	fprintf(out, _(
     149  		"     --lock[=<mode>]     use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
     150  	fputs(USAGE_SEPARATOR, out);
     151  	printf(USAGE_HELP_OPTIONS(25));
     152  	printf(USAGE_MAN_TAIL("mkfs.minix(8)"));
     153  	exit(MKFS_EX_OK);
     154  }
     155  
     156  #ifdef TEST_SCRIPT
     157  static inline time_t mkfs_minix_time(time_t *t)
     158  {
     159  	const char *str = getenv("MKFS_MINIX_TEST_SECOND_SINCE_EPOCH");
     160  	time_t sec;
     161  
     162  	if (str && sscanf(str, "%ld", &sec) == 1)
     163  		return sec;
     164  	return time(t);
     165  }
     166  #else				/* !TEST_SCRIPT */
     167  # define mkfs_minix_time(x) time(x)
     168  #endif
     169  
     170  static void super_set_state(void)
     171  {
     172  	switch (fs_version) {
     173  	case 1:
     174  	case 2:
     175  		Super.s_state |= MINIX_VALID_FS;
     176  		Super.s_state &= ~MINIX_ERROR_FS;
     177  		break;
     178  	default: /* v3 */
     179  		break;
     180  	}
     181  }
     182  
     183  static void write_tables(const struct fs_control *ctl) {
     184  	unsigned long imaps = get_nimaps();
     185  	unsigned long zmaps = get_nzmaps();
     186  	size_t buffsz = get_inode_buffer_size();
     187  
     188  	/* Mark the super block valid. */
     189  	super_set_state();
     190  
     191  	if (lseek(ctl->device_fd, 0, SEEK_SET))
     192  		err(MKFS_EX_ERROR, _("%s: seek to boot block failed "
     193  				   " in write_tables"), ctl->device_name);
     194  	if (write_all(ctl->device_fd, boot_block_buffer, 512))
     195  		err(MKFS_EX_ERROR, _("%s: unable to clear boot sector"), ctl->device_name);
     196  	if (MINIX_BLOCK_SIZE != lseek(ctl->device_fd, MINIX_BLOCK_SIZE, SEEK_SET))
     197  		err(MKFS_EX_ERROR, _("%s: seek failed in write_tables"), ctl->device_name);
     198  
     199  	if (write_all(ctl->device_fd, super_block_buffer, MINIX_BLOCK_SIZE))
     200  		err(MKFS_EX_ERROR, _("%s: unable to write super-block"), ctl->device_name);
     201  
     202  	if (write_all(ctl->device_fd, inode_map, imaps * MINIX_BLOCK_SIZE))
     203  		err(MKFS_EX_ERROR, _("%s: unable to write inode map"), ctl->device_name);
     204  
     205  	if (write_all(ctl->device_fd, zone_map, zmaps * MINIX_BLOCK_SIZE))
     206  		err(MKFS_EX_ERROR, _("%s: unable to write zone map"), ctl->device_name);
     207  
     208  	if (write_all(ctl->device_fd, inode_buffer, buffsz))
     209  		err(MKFS_EX_ERROR, _("%s: unable to write inodes"), ctl->device_name);
     210  }
     211  
     212  static void write_block(const struct fs_control *ctl, int blk, char * buffer) {
     213  	if (blk * MINIX_BLOCK_SIZE != lseek(ctl->device_fd, blk * MINIX_BLOCK_SIZE, SEEK_SET))
     214  		errx(MKFS_EX_ERROR, _("%s: seek failed in write_block"), ctl->device_name);
     215  
     216  	if (write_all(ctl->device_fd, buffer, MINIX_BLOCK_SIZE))
     217  		errx(MKFS_EX_ERROR, _("%s: write failed in write_block"), ctl->device_name);
     218  }
     219  
     220  static int get_free_block(struct fs_control *ctl) {
     221  	unsigned int blk;
     222  	unsigned int zones = get_nzones();
     223  	unsigned int first_zone = get_first_zone();
     224  
     225  	if (ctl->fs_used_blocks + 1 >= MAX_GOOD_BLOCKS)
     226  		errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name);
     227  	if (ctl->fs_used_blocks)
     228  		blk = good_blocks_table[ctl->fs_used_blocks - 1] + 1;
     229  	else
     230  		blk = first_zone;
     231  	while (blk < zones && zone_in_use(blk))
     232  		blk++;
     233  	if (blk >= zones)
     234  		errx(MKFS_EX_ERROR, _("%s: not enough good blocks"), ctl->device_name);
     235  	good_blocks_table[ctl->fs_used_blocks] = blk;
     236  	ctl->fs_used_blocks++;
     237  	return blk;
     238  }
     239  
     240  static void mark_good_blocks(const struct fs_control *ctl) {
     241  	int blk;
     242  
     243  	for (blk=0 ; blk < ctl->fs_used_blocks ; blk++)
     244  		mark_zone(good_blocks_table[blk]);
     245  }
     246  
     247  static inline int next(unsigned long zone) {
     248  	unsigned long zones = get_nzones();
     249  	unsigned long first_zone = get_first_zone();
     250  
     251  	if (!zone)
     252  		zone = first_zone-1;
     253  	while (++zone < zones)
     254  		if (zone_in_use(zone))
     255  			return zone;
     256  	return 0;
     257  }
     258  
     259  static void make_bad_inode_v1(struct fs_control *ctl)
     260  {
     261  	struct minix_inode * inode = &Inode[MINIX_BAD_INO];
     262  	int i,j,zone;
     263  	int ind=0,dind=0;
     264  	unsigned short ind_block[MINIX_BLOCK_SIZE>>1];
     265  	unsigned short dind_block[MINIX_BLOCK_SIZE>>1];
     266  
     267  #define NEXT_BAD (zone = next(zone))
     268  
     269  	if (!ctl->fs_bad_blocks)
     270  		return;
     271  	mark_inode(MINIX_BAD_INO);
     272  	inode->i_nlinks = 1;
     273  	inode->i_time = mkfs_minix_time(NULL);
     274  	inode->i_mode = S_IFREG + 0000;
     275  	inode->i_size = ctl->fs_bad_blocks * MINIX_BLOCK_SIZE;
     276  	zone = next(0);
     277  	for (i=0 ; i<7 ; i++) {
     278  		inode->i_zone[i] = zone;
     279  		if (!NEXT_BAD)
     280  			goto end_bad;
     281  	}
     282  	inode->i_zone[7] = ind = get_free_block(ctl);
     283  	memset(ind_block,0,MINIX_BLOCK_SIZE);
     284  	for (i=0 ; i<512 ; i++) {
     285  		ind_block[i] = zone;
     286  		if (!NEXT_BAD)
     287  			goto end_bad;
     288  	}
     289  	inode->i_zone[8] = dind = get_free_block(ctl);
     290  	memset(dind_block,0,MINIX_BLOCK_SIZE);
     291  	for (i=0 ; i<512 ; i++) {
     292  		write_block(ctl, ind,(char *) ind_block);
     293  		dind_block[i] = ind = get_free_block(ctl);
     294  		memset(ind_block,0,MINIX_BLOCK_SIZE);
     295  		for (j=0 ; j<512 ; j++) {
     296  			ind_block[j] = zone;
     297  			if (!NEXT_BAD)
     298  				goto end_bad;
     299  		}
     300  	}
     301  	errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name);
     302  end_bad:
     303  	if (ind)
     304  		write_block(ctl, ind, (char *) ind_block);
     305  	if (dind)
     306  		write_block(ctl, dind, (char *) dind_block);
     307  }
     308  
     309  static void make_bad_inode_v2_v3 (struct fs_control *ctl)
     310  {
     311  	struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
     312  	int i, j, zone;
     313  	int ind = 0, dind = 0;
     314  	unsigned long ind_block[MINIX_BLOCK_SIZE >> 2];
     315  	unsigned long dind_block[MINIX_BLOCK_SIZE >> 2];
     316  
     317  	if (!ctl->fs_bad_blocks)
     318  		return;
     319  	mark_inode (MINIX_BAD_INO);
     320  	inode->i_nlinks = 1;
     321  	inode->i_atime = inode->i_mtime = inode->i_ctime = mkfs_minix_time(NULL);
     322  	inode->i_mode = S_IFREG + 0000;
     323  	inode->i_size = ctl->fs_bad_blocks * MINIX_BLOCK_SIZE;
     324  	zone = next (0);
     325  	for (i = 0; i < 7; i++) {
     326  		inode->i_zone[i] = zone;
     327  		if (!NEXT_BAD)
     328  			goto end_bad;
     329  	}
     330  	inode->i_zone[7] = ind = get_free_block (ctl);
     331  	memset (ind_block, 0, MINIX_BLOCK_SIZE);
     332  	for (i = 0; i < 256; i++) {
     333  		ind_block[i] = zone;
     334  		if (!NEXT_BAD)
     335  			goto end_bad;
     336  	}
     337  	inode->i_zone[8] = dind = get_free_block (ctl);
     338  	memset (dind_block, 0, MINIX_BLOCK_SIZE);
     339  	for (i = 0; i < 256; i++) {
     340  		write_block (ctl, ind, (char *) ind_block);
     341  		dind_block[i] = ind = get_free_block (ctl);
     342  		memset (ind_block, 0, MINIX_BLOCK_SIZE);
     343  		for (j = 0; j < 256; j++) {
     344  			ind_block[j] = zone;
     345  			if (!NEXT_BAD)
     346  				goto end_bad;
     347  		}
     348  	}
     349  	/* Could make triple indirect block here */
     350  	errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name);
     351   end_bad:
     352  	if (ind)
     353  		write_block (ctl, ind, (char *) ind_block);
     354  	if (dind)
     355  		write_block (ctl, dind, (char *) dind_block);
     356  }
     357  
     358  static void make_bad_inode(struct fs_control *ctl)
     359  {
     360  	if (fs_version < 2) {
     361  		make_bad_inode_v1(ctl);
     362  		return;
     363  	}
     364  	make_bad_inode_v2_v3(ctl);
     365  }
     366  
     367  static void make_root_inode_v1(struct fs_control *ctl) {
     368  	struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
     369  
     370  	mark_inode(MINIX_ROOT_INO);
     371  	inode->i_zone[0] = get_free_block(ctl);
     372  	inode->i_nlinks = 2;
     373  	inode->i_time = mkfs_minix_time(NULL);
     374  	if (ctl->fs_bad_blocks)
     375  		inode->i_size = 3 * ctl->fs_dirsize;
     376  	else {
     377  		memset(&root_block[2 * ctl->fs_dirsize], 0, ctl->fs_dirsize);
     378  		inode->i_size = 2 * ctl->fs_dirsize;
     379  	}
     380  	inode->i_mode = S_IFDIR + 0755;
     381  	inode->i_uid = getuid();
     382  	if (inode->i_uid)
     383  		inode->i_gid = getgid();
     384  	write_block(ctl, inode->i_zone[0],root_block);
     385  }
     386  
     387  static void make_root_inode_v2_v3 (struct fs_control *ctl) {
     388  	struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
     389  
     390  	mark_inode (MINIX_ROOT_INO);
     391  	inode->i_zone[0] = get_free_block (ctl);
     392  	inode->i_nlinks = 2;
     393  	inode->i_atime = inode->i_mtime = inode->i_ctime = mkfs_minix_time(NULL);
     394  
     395  	if (ctl->fs_bad_blocks)
     396  		inode->i_size = 3 * ctl->fs_dirsize;
     397  	else {
     398  		memset(&root_block[2 * ctl->fs_dirsize], 0, ctl->fs_dirsize);
     399  		inode->i_size = 2 * ctl->fs_dirsize;
     400  	}
     401  
     402  	inode->i_mode = S_IFDIR + 0755;
     403  	inode->i_uid = getuid();
     404  	if (inode->i_uid)
     405  		inode->i_gid = getgid();
     406  	write_block (ctl, inode->i_zone[0], root_block);
     407  }
     408  
     409  static void make_root_inode(struct fs_control *ctl)
     410  {
     411  	char *tmp = root_block;
     412  
     413  	if (fs_version == 3) {
     414  		*(uint32_t *) tmp = 1;
     415  		strcpy(tmp + 4, ".");
     416  		tmp += ctl->fs_dirsize;
     417  		*(uint32_t *) tmp = 1;
     418  		strcpy(tmp + 4, "..");
     419  		tmp += ctl->fs_dirsize;
     420  		*(uint32_t *) tmp = 2;
     421  		strcpy(tmp + 4, ".badblocks");
     422  	} else {
     423  		*(uint16_t *) tmp = 1;
     424  		strcpy(tmp + 2, ".");
     425  		tmp += ctl->fs_dirsize;
     426  		*(uint16_t *) tmp = 1;
     427  		strcpy(tmp + 2, "..");
     428  		tmp += ctl->fs_dirsize;
     429  		*(uint16_t *) tmp = 2;
     430  		strcpy(tmp + 2, ".badblocks");
     431  	}
     432  	if (fs_version < 2) {
     433  		make_root_inode_v1(ctl);
     434  		return;
     435  	}
     436  	make_root_inode_v2_v3(ctl);
     437  }
     438  
     439  static void super_set_nzones(const struct fs_control *ctl)
     440  {
     441  	switch (fs_version) {
     442  	case 3:
     443  		Super3.s_zones = ctl->fs_blocks;
     444  		break;
     445  	case 2:
     446  		Super.s_zones = ctl->fs_blocks;
     447  		break;
     448  	default: /* v1 */
     449  		Super.s_nzones = ctl->fs_blocks;
     450  		break;
     451  	}
     452  }
     453  
     454  static void super_init_maxsize(void)
     455  {
     456  	switch (fs_version) {
     457  	case 3:
     458  		Super3.s_max_size = 2147483647L;
     459  		break;
     460  	case 2:
     461  		Super.s_max_size =  0x7fffffff;
     462  		break;
     463  	default: /* v1 */
     464  		Super.s_max_size = (7+512+512*512)*1024;
     465  		break;
     466  	}
     467  }
     468  
     469  static void super_set_map_blocks(const struct fs_control *ctl, unsigned long inodes)
     470  {
     471  	switch (fs_version) {
     472  	case 3:
     473  		Super3.s_imap_blocks = UPPER(inodes + 1, BITS_PER_BLOCK);
     474  		Super3.s_zmap_blocks = UPPER(ctl->fs_blocks - (1 + get_nimaps() + inode_blocks()),
     475  					     BITS_PER_BLOCK + 1);
     476  		Super3.s_firstdatazone = first_zone_data();
     477  		break;
     478  	default:
     479  		Super.s_imap_blocks = UPPER(inodes + 1, BITS_PER_BLOCK);
     480  		Super.s_zmap_blocks = UPPER(ctl->fs_blocks - (1 + get_nimaps() + inode_blocks()),
     481  					     BITS_PER_BLOCK + 1);
     482  		Super.s_firstdatazone = first_zone_data();
     483  		break;
     484  	}
     485  }
     486  
     487  static void super_set_magic(const struct fs_control *ctl)
     488  {
     489  	switch (fs_version) {
     490  	case 3:
     491  		Super3.s_magic = ctl->fs_magic;
     492  		break;
     493  	default:
     494  		Super.s_magic = ctl->fs_magic;
     495  		break;
     496  	}
     497  }
     498  
     499  static void setup_tables(const struct fs_control *ctl) {
     500  	unsigned long inodes, zmaps, imaps, zones, i;
     501  
     502  	super_block_buffer = xcalloc(1, MINIX_BLOCK_SIZE);
     503  
     504  	memset(boot_block_buffer,0,512);
     505  	super_set_magic(ctl);
     506  
     507  	if (fs_version == 3) {
     508  		Super3.s_log_zone_size = 0;
     509  		Super3.s_blocksize = MINIX_BLOCK_SIZE;
     510  	}
     511  	else {
     512  		Super.s_log_zone_size = 0;
     513  	}
     514  
     515  	super_init_maxsize();
     516  	super_set_nzones(ctl);
     517  	zones = get_nzones();
     518  
     519  	/* some magic nrs: 1 inode / 3 blocks for smaller filesystems,
     520  	 * for one inode / 16 blocks for large ones. mkfs will eventually
     521  	 * crab about too far when getting close to the maximum size. */
     522  	if (ctl->fs_inodes == 0)
     523  		if (2048 * 1024 < ctl->fs_blocks)	/* 2GB */
     524  			inodes = ctl->fs_blocks / 16;
     525  		else if (512 * 1024 < ctl->fs_blocks)	/* 0.5GB */
     526  			inodes = ctl->fs_blocks / 8;
     527  		else
     528  			inodes = ctl->fs_blocks / 3;
     529  	else
     530  		inodes = ctl->fs_inodes;
     531  	/* Round up inode count to fill block size */
     532  	if (fs_version == 2 || fs_version == 3)
     533  		inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
     534  			  ~(MINIX2_INODES_PER_BLOCK - 1));
     535  	else
     536  		inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
     537  			  ~(MINIX_INODES_PER_BLOCK - 1));
     538  
     539  	if (fs_version == 3)
     540  		Super3.s_ninodes = inodes;
     541  	else {
     542  		if (inodes > MINIX_MAX_INODES)
     543  			inodes = MINIX_MAX_INODES;
     544  		Super.s_ninodes = inodes;
     545  	}
     546  	super_set_map_blocks(ctl, inodes);
     547  	if (MINIX_MAX_INODES < first_zone_data())
     548  		errx(MKFS_EX_ERROR,
     549  		     _("First data block at %jd, which is too far (max %d).\n"
     550  		       "Try specifying fewer inodes by passing --inodes <num>"),
     551  		     (intmax_t)first_zone_data(),
     552  		     MINIX_MAX_INODES);
     553  	imaps = get_nimaps();
     554  	zmaps = get_nzmaps();
     555  
     556  	inode_map = xmalloc(imaps * MINIX_BLOCK_SIZE);
     557  	zone_map = xmalloc(zmaps * MINIX_BLOCK_SIZE);
     558  	memset(inode_map,0xff,imaps * MINIX_BLOCK_SIZE);
     559  	memset(zone_map,0xff,zmaps * MINIX_BLOCK_SIZE);
     560  
     561  	for (i = get_first_zone() ; i<zones ; i++)
     562  		unmark_zone(i);
     563  	for (i = MINIX_ROOT_INO ; i<=inodes; i++)
     564  		unmark_inode(i);
     565  
     566  	inode_buffer = xmalloc(get_inode_buffer_size());
     567  	memset(inode_buffer,0, get_inode_buffer_size());
     568  
     569  	printf(P_("%lu inode\n", "%lu inodes\n", inodes), inodes);
     570  	printf(P_("%lu block\n", "%lu blocks\n", zones), zones);
     571  	printf(_("Firstdatazone=%jd (%jd)\n"),
     572  		(intmax_t)get_first_zone(), (intmax_t)first_zone_data());
     573  	printf(_("Zonesize=%zu\n"), (size_t) MINIX_BLOCK_SIZE << get_zone_size());
     574  	printf(_("Maxsize=%zu\n\n"),get_max_size());
     575  }
     576  
     577  /*
     578   * Perform a test of a block; return the number of
     579   * blocks readable/writable.
     580   */
     581  static size_t do_check(const struct fs_control *ctl, char * buffer, int try, unsigned int current_block) {
     582  	ssize_t got;
     583  
     584  	/* Seek to the correct loc. */
     585  	if (lseek(ctl->device_fd, current_block * MINIX_BLOCK_SIZE, SEEK_SET) !=
     586  		       current_block * MINIX_BLOCK_SIZE )
     587  		err(MKFS_EX_ERROR, _("%s: seek failed during testing of blocks"),
     588  				ctl->device_name);
     589  
     590  	/* Try the read */
     591  	got = read(ctl->device_fd, buffer, try * MINIX_BLOCK_SIZE);
     592  	if (got < 0) got = 0;
     593  	if (got & (MINIX_BLOCK_SIZE - 1 )) {
     594  		printf(_("Weird values in do_check: probably bugs\n"));
     595  	}
     596  	got /= MINIX_BLOCK_SIZE;
     597  	return got;
     598  }
     599  
     600  static unsigned int currently_testing = 0;
     601  
     602  static void alarm_intr(int alnum __attribute__ ((__unused__))) {
     603  	unsigned long zones = get_nzones();
     604  
     605  	if (currently_testing >= zones)
     606  		return;
     607  	signal(SIGALRM,alarm_intr);
     608  	alarm(5);
     609  	if (!currently_testing)
     610  		return;
     611  	printf("%d ...", currently_testing);
     612  	fflush(stdout);
     613  }
     614  
     615  static void check_blocks(struct fs_control *ctl) {
     616  	size_t try, got;
     617  	static char buffer[MINIX_BLOCK_SIZE * TEST_BUFFER_BLOCKS];
     618  	unsigned long zones = get_nzones();
     619  	unsigned long first_zone = get_first_zone();
     620  
     621  	currently_testing=0;
     622  	signal(SIGALRM,alarm_intr);
     623  	alarm(5);
     624  	while (currently_testing < zones) {
     625  		if (lseek(ctl->device_fd, currently_testing * MINIX_BLOCK_SIZE,SEEK_SET) !=
     626  		    currently_testing*MINIX_BLOCK_SIZE)
     627  			errx(MKFS_EX_ERROR, _("%s: seek failed in check_blocks"),
     628  					ctl->device_name);
     629  		try = TEST_BUFFER_BLOCKS;
     630  		if (currently_testing + try > zones)
     631  			try = zones-currently_testing;
     632  		got = do_check(ctl, buffer, try, currently_testing);
     633  		currently_testing += got;
     634  		if (got == try)
     635  			continue;
     636  		if (currently_testing < first_zone)
     637  			errx(MKFS_EX_ERROR, _("%s: bad blocks before data-area: "
     638  					"cannot make fs"), ctl->device_name);
     639  		mark_zone(currently_testing);
     640  		ctl->fs_bad_blocks++;
     641  		currently_testing++;
     642  	}
     643  	if (ctl->fs_bad_blocks > 0)
     644  		printf(P_("%d bad block\n", "%d bad blocks\n", ctl->fs_bad_blocks), ctl->fs_bad_blocks);
     645  }
     646  
     647  static void get_list_blocks(struct fs_control *ctl, char *filename) {
     648  	FILE *listfile;
     649  	unsigned long blockno;
     650  
     651  	listfile = fopen(filename,"r");
     652  	if (listfile == NULL)
     653  		err(MKFS_EX_ERROR, _("%s: can't open file of bad blocks"),
     654  				ctl->device_name);
     655  
     656  	while (!feof(listfile)) {
     657  		if (fscanf(listfile,"%lu\n", &blockno) != 1) {
     658  			printf(_("badblock number input error on line %d\n"), ctl->fs_bad_blocks + 1);
     659  			errx(MKFS_EX_ERROR, _("%s: cannot read badblocks file"),
     660  					ctl->device_name);
     661  		}
     662  		mark_zone(blockno);
     663  		ctl->fs_bad_blocks++;
     664  	}
     665  	fclose(listfile);
     666  
     667  	if (ctl->fs_bad_blocks > 0)
     668  		printf(P_("%d bad block\n", "%d bad blocks\n", ctl->fs_bad_blocks), ctl->fs_bad_blocks);
     669  }
     670  
     671  static int find_super_magic(const struct fs_control *ctl)
     672  {
     673  	switch (fs_version) {
     674  	case 1:
     675  		if (ctl->fs_namelen == 14)
     676  			return MINIX_SUPER_MAGIC;
     677  		return MINIX_SUPER_MAGIC2;
     678  	case 2:
     679  		if (ctl->fs_namelen == 14)
     680  			return MINIX2_SUPER_MAGIC;
     681  		return MINIX2_SUPER_MAGIC2;
     682  	case 3:
     683  		return MINIX3_SUPER_MAGIC;
     684  	default:
     685  		abort();
     686  	}
     687  }
     688  
     689  static void determine_device_blocks(struct fs_control *ctl, const struct stat *statbuf)
     690  {
     691  	unsigned long long dev_blocks = 0;
     692  
     693  	if (S_ISBLK(statbuf->st_mode)) {
     694  		int sectorsize;
     695  
     696  		if (blkdev_get_sector_size(ctl->device_fd, &sectorsize) == -1)
     697  			sectorsize = DEFAULT_SECTOR_SIZE;	/* kernel < 2.3.3 */
     698  		if (MINIX_BLOCK_SIZE < sectorsize)
     699  			errx(MKFS_EX_ERROR, _("block size smaller than physical "
     700  					      "sector size of %s"), ctl->device_name);
     701  		if (blkdev_get_size(ctl->device_fd, &dev_blocks) == -1)
     702  			errx(MKFS_EX_ERROR, _("cannot determine size of %s"), ctl->device_name);
     703  		dev_blocks /= MINIX_BLOCK_SIZE;
     704  	} else if (!S_ISBLK(statbuf->st_mode))
     705  		dev_blocks = statbuf->st_size / MINIX_BLOCK_SIZE;
     706  	if (!ctl->fs_blocks)
     707  		ctl->fs_blocks = dev_blocks;
     708  	else if (dev_blocks < ctl->fs_blocks)
     709  		errx(MKFS_EX_ERROR,
     710  		     _("%s: requested blocks (%llu) exceeds available (%llu) blocks\n"),
     711  		     ctl->device_name, ctl->fs_blocks, dev_blocks);
     712  	if (ctl->fs_blocks < 10)
     713  		errx(MKFS_EX_ERROR, _("%s: number of blocks too small"), ctl->device_name);
     714  	if (fs_version == 1 && ctl->fs_blocks > MINIX_MAX_INODES)
     715  		ctl->fs_blocks = MINIX_MAX_INODES;
     716  	if (ctl->fs_blocks > (4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK)))
     717  		ctl->fs_blocks = 4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK);	/* Utter maximum: Clip. */
     718  }
     719  
     720  static void check_user_instructions(struct fs_control *ctl)
     721  {
     722  	switch (fs_version) {
     723  	case 1:
     724  	case 2:
     725  		if (ctl->fs_namelen == 14 || ctl->fs_namelen == 30)
     726  			ctl->fs_dirsize = ctl->fs_namelen + 2;
     727  		else
     728  			errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen);
     729  		break;
     730  	case 3:
     731  		if (ctl->fs_namelen == 60)
     732  			ctl->fs_dirsize = ctl->fs_namelen + 4;
     733  		else
     734  			errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen);
     735  		break;
     736  	default:
     737  		errx(MKFS_EX_ERROR, _("unsupported minix file system version: %d"), fs_version);
     738  	}
     739  	ctl->fs_magic = find_super_magic(ctl);
     740  }
     741  
     742  int main(int argc, char ** argv)
     743  {
     744  	struct fs_control ctl = {
     745  		.fs_namelen = 30,	/* keep in sync with DEFAULT_FS_VERSION */
     746  		0
     747  	};
     748  	int i;
     749  	struct stat statbuf;
     750  	char * listfile = NULL;
     751  	enum {
     752  		OPT_LOCK = CHAR_MAX + 1
     753  	};
     754  	static const struct option longopts[] = {
     755  		{"namelength", required_argument, NULL, 'n'},
     756  		{"inodes", required_argument, NULL, 'i'},
     757  		{"check", no_argument, NULL, 'c'},
     758  		{"badblocks", required_argument, NULL, 'l'},
     759  		{"version", no_argument, NULL, 'V'},
     760  		{"help", no_argument, NULL, 'h'},
     761  		{"lock",optional_argument, NULL, OPT_LOCK},
     762  		{NULL, 0, NULL, 0}
     763  	};
     764  
     765  	setlocale(LC_ALL, "");
     766  	bindtextdomain(PACKAGE, LOCALEDIR);
     767  	textdomain(PACKAGE);
     768  	close_stdout_atexit();
     769  
     770  	strutils_set_exitcode(MKFS_EX_USAGE);
     771  
     772  	while ((i = getopt_long(argc, argv, "1v23n:i:cl:Vh", longopts, NULL)) != -1)
     773  		switch (i) {
     774  		case '1':
     775  			fs_version = 1;
     776  			break;
     777  		case 'v': /* kept for backwards compatibility */
     778  			warnx(_("-v is ambiguous, use '-2' instead"));
     779  			/* fallthrough */
     780  		case '2':
     781  			fs_version = 2;
     782  			break;
     783  		case '3':
     784  			fs_version = 3;
     785  			ctl.fs_namelen = 60;
     786  			break;
     787  		case 'n':
     788  			ctl.fs_namelen = strtou16_or_err(optarg,
     789  					_("failed to parse maximum length of filenames"));
     790  			break;
     791  		case 'i':
     792  			ctl.fs_inodes = strtoul_or_err(optarg,
     793  					_("failed to parse number of inodes"));
     794  			break;
     795  		case 'c':
     796  			ctl.check_blocks = 1;
     797  			break;
     798  		case 'l':
     799  			listfile = optarg;
     800  			break;
     801  		case OPT_LOCK:
     802  			ctl.lockmode = "1";
     803  			if (optarg) {
     804  				if (*optarg == '=')
     805  					optarg++;
     806  				ctl.lockmode = optarg;
     807  			}
     808  			break;
     809  		case 'V':
     810  			print_version(MKFS_EX_OK);
     811  		case 'h':
     812  			usage();
     813  		default:
     814  			errtryhelp(MKFS_EX_USAGE);
     815  		}
     816  	argc -= optind;
     817  	argv += optind;
     818  	if (argc > 0) {
     819  		ctl.device_name = argv[0];
     820  		argc--;
     821  		argv++;
     822  	}
     823  	if (argc > 0)
     824  		ctl.fs_blocks = strtoul_or_err(argv[0], _("failed to parse number of blocks"));
     825  
     826  	if (!ctl.device_name) {
     827  		warnx(_("no device specified"));
     828  		errtryhelp(MKFS_EX_USAGE);
     829  	}
     830  	check_user_instructions(&ctl);
     831  	if (is_mounted(ctl.device_name))
     832  		errx(MKFS_EX_ERROR, _("%s is mounted; will not make a filesystem here!"),
     833  			ctl.device_name);
     834  	if (stat(ctl.device_name, &statbuf) < 0)
     835  		err(MKFS_EX_ERROR, _("stat of %s failed"), ctl.device_name);
     836  	ctl.device_fd = open_blkdev_or_file(&statbuf, ctl.device_name, O_RDWR);
     837  	if (ctl.device_fd < 0)
     838  		err(MKFS_EX_ERROR, _("cannot open %s"), ctl.device_name);
     839  	if (blkdev_lock(ctl.device_fd, ctl.device_name, ctl.lockmode) != 0)
     840  		exit(MKFS_EX_ERROR);
     841  	determine_device_blocks(&ctl, &statbuf);
     842  	setup_tables(&ctl);
     843  	if (ctl.check_blocks)
     844  		check_blocks(&ctl);
     845  	else if (listfile)
     846  		get_list_blocks(&ctl, listfile);
     847  
     848  	make_root_inode(&ctl);
     849  	make_bad_inode(&ctl);
     850  
     851  	mark_good_blocks(&ctl);
     852  	write_tables(&ctl);
     853  	if (close_fd(ctl.device_fd) != 0)
     854  		err(MKFS_EX_ERROR, _("write failed"));
     855  
     856  	return MKFS_EX_OK;
     857  }