2 * mkfs.minix.c - make a linux (minix) file-system.
4 * (C) 1991 Linus Torvalds. This file may be redistributed as per
11 * 24.11.91 - Time began. Used the fsck sources to get started.
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.
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)
20 * 28.02.92 - Added %-information when using -c.
22 * 28.02.93 - Added support for other namelengths than the original
23 * 14 characters so that I can test the new kernel routines..
25 * 09.10.93 - Make exit status conform to that required by fsutil
26 * (Rik Faith, faith@cs.unc.edu)
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)
32 * 03.01.94 - Added support for file system valid flag.
33 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35 * 30.10.94 - Added support for v2 filesystem
36 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
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)
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)
46 * 02.07.96 - Added small patch from Russell King to make the program a
47 * good deal more portable (janl@math.uio.no)
49 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51 * -c for readablility checking (SLOW!)
52 * -l for getting a list of bad blocks from a file.
53 * -n for namelength (currently the kernel only uses 14 or 30)
54 * -i for number of inodes
55 * -v for v2 filesystem
57 * The device may be a block device or a image of one, but this isn't
58 * enforced (but it's not much fun on a character device :-).
71 #include <sys/ioctl.h>
72 #include <sys/param.h>
79 #include "pathnames.h"
82 #define MINIX_ROOT_INO 1
83 #define MINIX_BAD_INO 2
85 #define TEST_BUFFER_BLOCKS 16
86 #define MAX_GOOD_BLOCKS 512
88 #define UPPER(size,n) ((size+((n)-1))/(n))
89 #define INODE_SIZE (sizeof(struct minix_inode))
91 #define INODE_SIZE2 (sizeof(struct minix2_inode))
92 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
93 : MINIX_INODES_PER_BLOCK))
94 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
96 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
98 static char * program_name = "mkfs";
99 static char * device_name = NULL;
101 static unsigned long long BLOCKS = 0;
102 static int check = 0;
103 static int badblocks = 0;
104 static int namelen = 30; /* default (changed to 30, per Linus's
105 suggestion, Sun Nov 21 08:05:07 1993) */
106 static int dirsize = 32;
107 static int magic = MINIX_SUPER_MAGIC2;
108 static int version2 = 0;
110 static char root_block[BLOCK_SIZE] = "\0";
112 static char * inode_buffer = NULL;
113 #define Inode (((struct minix_inode *) inode_buffer)-1)
114 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
116 static char super_block_buffer[BLOCK_SIZE];
117 static char boot_block_buffer[512];
118 #define Super (*(struct minix_super_block *)super_block_buffer)
119 #define INODES ((unsigned long)Super.s_ninodes)
120 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
121 #define IMAPS ((unsigned long)Super.s_imap_blocks)
122 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
123 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
124 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
125 #define MAXSIZE ((unsigned long)Super.s_max_size)
126 #define MAGIC (Super.s_magic)
127 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
129 static char *inode_map;
130 static char *zone_map;
132 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
133 static int used_good_blocks = 0;
134 static unsigned long req_nr_inodes = 0;
136 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1) != 0)
138 #define mark_inode(x) (setbit(inode_map,(x)))
139 #define unmark_inode(x) (clrbit(inode_map,(x)))
141 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
142 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
146 fprintf(stderr, "%s: ", program_name);
147 fprintf(stderr, str, device_name);
148 fprintf(stderr, "\n");
154 fprintf(stderr, "%s (%s)\n", program_name, PACKAGE_STRING);
156 _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"),
162 * Check to make certain that our new filesystem won't be created on
163 * an already mounted partition. Code adapted from mke2fs, Copyright
164 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
171 if ((f = setmntent (_PATH_MOUNTED, "r")) == NULL)
173 while ((mnt = getmntent (f)) != NULL)
174 if (strcmp (device_name, mnt->mnt_fsname) == 0)
180 die(_("%s is mounted; will not make a filesystem here!"));
185 /* Mark the super block valid. */
186 Super.s_state |= MINIX_VALID_FS;
187 Super.s_state &= ~MINIX_ERROR_FS;
189 if (lseek(DEV, 0, SEEK_SET))
190 die(_("seek to boot block failed in write_tables"));
191 if (512 != write(DEV, boot_block_buffer, 512))
192 die(_("unable to clear boot sector"));
193 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
194 die(_("seek failed in write_tables"));
195 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
196 die(_("unable to write super-block"));
197 if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
198 die(_("unable to write inode map"));
199 if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
200 die(_("unable to write zone map"));
201 if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
202 die(_("unable to write inodes"));
207 write_block(int blk, char * buffer) {
208 if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
209 die(_("seek failed in write_block"));
210 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
211 die(_("write failed in write_block"));
215 get_free_block(void) {
218 if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
219 die(_("too many bad blocks"));
220 if (used_good_blocks)
221 blk = good_blocks_table[used_good_blocks-1]+1;
224 while (blk < ZONES && zone_in_use(blk))
227 die(_("not enough good blocks"));
228 good_blocks_table[used_good_blocks] = blk;
234 mark_good_blocks(void) {
237 for (blk=0 ; blk < used_good_blocks ; blk++)
238 mark_zone(good_blocks_table[blk]);
245 while (++zone < ZONES)
246 if (zone_in_use(zone))
252 make_bad_inode(void) {
253 struct minix_inode * inode = &Inode[MINIX_BAD_INO];
256 unsigned short ind_block[BLOCK_SIZE>>1];
257 unsigned short dind_block[BLOCK_SIZE>>1];
259 #define NEXT_BAD (zone = next(zone))
263 mark_inode(MINIX_BAD_INO);
265 inode->i_time = time(NULL);
266 inode->i_mode = S_IFREG + 0000;
267 inode->i_size = badblocks*BLOCK_SIZE;
269 for (i=0 ; i<7 ; i++) {
270 inode->i_zone[i] = zone;
274 inode->i_zone[7] = ind = get_free_block();
275 memset(ind_block,0,BLOCK_SIZE);
276 for (i=0 ; i<512 ; i++) {
281 inode->i_zone[8] = dind = get_free_block();
282 memset(dind_block,0,BLOCK_SIZE);
283 for (i=0 ; i<512 ; i++) {
284 write_block(ind,(char *) ind_block);
285 dind_block[i] = ind = get_free_block();
286 memset(ind_block,0,BLOCK_SIZE);
287 for (j=0 ; j<512 ; j++) {
293 die(_("too many bad blocks"));
296 write_block(ind, (char *) ind_block);
298 write_block(dind, (char *) dind_block);
302 make_bad_inode2 (void) {
303 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
305 int ind = 0, dind = 0;
306 unsigned long ind_block[BLOCK_SIZE >> 2];
307 unsigned long dind_block[BLOCK_SIZE >> 2];
311 mark_inode (MINIX_BAD_INO);
313 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
314 inode->i_mode = S_IFREG + 0000;
315 inode->i_size = badblocks * BLOCK_SIZE;
317 for (i = 0; i < 7; i++) {
318 inode->i_zone[i] = zone;
322 inode->i_zone[7] = ind = get_free_block ();
323 memset (ind_block, 0, BLOCK_SIZE);
324 for (i = 0; i < 256; i++) {
329 inode->i_zone[8] = dind = get_free_block ();
330 memset (dind_block, 0, BLOCK_SIZE);
331 for (i = 0; i < 256; i++) {
332 write_block (ind, (char *) ind_block);
333 dind_block[i] = ind = get_free_block ();
334 memset (ind_block, 0, BLOCK_SIZE);
335 for (j = 0; j < 256; j++) {
341 /* Could make triple indirect block here */
342 die (_("too many bad blocks"));
345 write_block (ind, (char *) ind_block);
347 write_block (dind, (char *) dind_block);
351 make_root_inode(void) {
352 struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
354 mark_inode(MINIX_ROOT_INO);
355 inode->i_zone[0] = get_free_block();
357 inode->i_time = time(NULL);
359 inode->i_size = 3*dirsize;
361 root_block[2*dirsize] = '\0';
362 root_block[2*dirsize+1] = '\0';
363 inode->i_size = 2*dirsize;
365 inode->i_mode = S_IFDIR + 0755;
366 inode->i_uid = getuid();
368 inode->i_gid = getgid();
369 write_block(inode->i_zone[0],root_block);
373 make_root_inode2 (void) {
374 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
376 mark_inode (MINIX_ROOT_INO);
377 inode->i_zone[0] = get_free_block ();
379 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
381 inode->i_size = 3 * dirsize;
383 root_block[2 * dirsize] = '\0';
384 root_block[2 * dirsize + 1] = '\0';
385 inode->i_size = 2 * dirsize;
387 inode->i_mode = S_IFDIR + 0755;
388 inode->i_uid = getuid();
390 inode->i_gid = getgid();
391 write_block (inode->i_zone[0], root_block);
397 unsigned long inodes;
399 memset(super_block_buffer,0,BLOCK_SIZE);
400 memset(boot_block_buffer,0,512);
401 Super.s_magic = magic;
402 Super.s_log_zone_size = 0;
403 Super.s_max_size = version2 ? 0x7fffffff : (7+512+512*512)*1024;
405 Super.s_zones = BLOCKS;
407 Super.s_nzones = BLOCKS;
409 /* some magic nrs: 1 inode / 3 blocks */
410 if ( req_nr_inodes == 0 )
413 inodes = req_nr_inodes;
414 /* Round up inode count to fill block size */
416 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
417 ~(MINIX2_INODES_PER_BLOCK - 1));
419 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
420 ~(MINIX_INODES_PER_BLOCK - 1));
423 Super.s_ninodes = inodes;
427 * while (ZMAPS != UPPER(BLOCKS - NORM_FIRSTZONE + 1,BITS_PER_BLOCK))
428 * ZMAPS = UPPER(BLOCKS - NORM_FIRSTZONE + 1,BITS_PER_BLOCK);
429 * was no good, since it may loop. - aeb
431 Super.s_imap_blocks = UPPER(INODES + 1, BITS_PER_BLOCK);
432 Super.s_zmap_blocks = UPPER(BLOCKS - (1+IMAPS+INODE_BLOCKS),
434 Super.s_firstdatazone = NORM_FIRSTZONE;
436 inode_map = malloc(IMAPS * BLOCK_SIZE);
437 zone_map = malloc(ZMAPS * BLOCK_SIZE);
438 if (!inode_map || !zone_map)
439 die(_("unable to allocate buffers for maps"));
440 memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
441 memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
442 for (i = FIRSTZONE ; i<ZONES ; i++)
444 for (i = MINIX_ROOT_INO ; i<=INODES ; i++)
446 inode_buffer = malloc(INODE_BUFFER_SIZE);
448 die(_("unable to allocate buffer for inodes"));
449 memset(inode_buffer,0,INODE_BUFFER_SIZE);
450 printf(_("%ld inodes\n"),INODES);
451 printf(_("%ld blocks\n"),ZONES);
452 printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
453 printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
454 printf(_("Maxsize=%ld\n\n"),MAXSIZE);
458 * Perform a test of a block; return the number of
459 * blocks readable/writeable.
462 do_check(char * buffer, int try, unsigned int current_block) {
465 /* Seek to the correct loc. */
466 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
467 current_block * BLOCK_SIZE ) {
468 die(_("seek failed during testing of blocks"));
473 got = read(DEV, buffer, try * BLOCK_SIZE);
474 if (got < 0) got = 0;
475 if (got & (BLOCK_SIZE - 1 )) {
476 printf(_("Weird values in do_check: probably bugs\n"));
482 static unsigned int currently_testing = 0;
485 alarm_intr(int alnum) {
486 if (currently_testing >= ZONES)
488 signal(SIGALRM,alarm_intr);
490 if (!currently_testing)
492 printf("%d ...", currently_testing);
499 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
502 signal(SIGALRM,alarm_intr);
504 while (currently_testing < ZONES) {
505 if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
506 currently_testing*BLOCK_SIZE)
507 die(_("seek failed in check_blocks"));
508 try = TEST_BUFFER_BLOCKS;
509 if (currently_testing + try > ZONES)
510 try = ZONES-currently_testing;
511 got = do_check(buffer, try, currently_testing);
512 currently_testing += got;
515 if (currently_testing < FIRSTZONE)
516 die(_("bad blocks before data-area: cannot make fs"));
517 mark_zone(currently_testing);
522 printf(_("%d bad blocks\n"), badblocks);
523 else if (badblocks == 1)
524 printf(_("one bad block\n"));
528 get_list_blocks(char *filename) {
530 unsigned long blockno;
532 listfile = fopen(filename,"r");
533 if (listfile == NULL)
534 die(_("can't open file of bad blocks"));
536 while (!feof(listfile)) {
537 if (fscanf(listfile,"%ld\n", &blockno) != 1) {
538 printf(_("badblock number input error on line %d\n"), badblocks + 1);
539 die(_("cannot read badblocks file"));
547 printf(_("%d bad blocks\n"), badblocks);
548 else if (badblocks == 1)
549 printf(_("one bad block\n"));
553 main(int argc, char ** argv) {
557 char * listfile = NULL;
561 program_name = *argv;
562 if ((p = strrchr(program_name, '/')) != NULL)
565 setlocale(LC_ALL, "");
566 bindtextdomain(PACKAGE, LOCALEDIR);
570 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
571 printf(_("%s (%s)\n"), program_name, PACKAGE_STRING);
575 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
576 die(_("bad inode size"));
577 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
578 die(_("bad inode size"));
581 while ((i = getopt(argc, argv, "ci:l:n:v")) != -1)
586 req_nr_inodes = (unsigned long) atol(optarg);
589 listfile = optarg; break;
591 i = strtoul(optarg,&tmp,0);
595 magic = MINIX_SUPER_MAGIC;
597 magic = MINIX_SUPER_MAGIC2;
611 if (argc > 0 && !device_name) {
612 device_name = argv[0];
617 BLOCKS = strtol(argv[0],&tmp,0);
619 printf(_("strtol error: number of blocks not specified"));
627 check_mount(); /* is it already mounted? */
636 strcpy(tmp+2,".badblocks");
637 if (stat(device_name, &statbuf) < 0)
638 die(_("unable to stat %s"));
639 if (S_ISBLK(statbuf.st_mode))
640 DEV = open(device_name,O_RDWR | O_EXCL);
642 DEV = open(device_name,O_RDWR);
644 die(_("unable to open %s"));
645 if (S_ISBLK(statbuf.st_mode)) {
648 if (blkdev_get_sector_size(DEV, §orsize) == -1)
649 die(_("cannot determine sector size for %s"));
650 if (BLOCK_SIZE < sectorsize)
651 die(_("block size smaller than physical sector size of %s"));
653 if (blkdev_get_size(DEV, &BLOCKS) == -1)
654 die(_("cannot determine size of %s"));
655 BLOCKS /= BLOCK_SIZE;
657 } else if (!S_ISBLK(statbuf.st_mode)) {
659 BLOCKS = statbuf.st_size / BLOCK_SIZE;
661 } else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
662 die(_("will not try to make filesystem on '%s'"));
664 die(_("number of blocks too small"));
667 magic = MINIX2_SUPER_MAGIC;
669 magic = MINIX2_SUPER_MAGIC2;
677 get_list_blocks(listfile);