2 * mkfs.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 :-).
72 #include <sys/ioctl.h>
77 #include <linux/minix_fs.h>
79 #ifdef MINIX2_SUPER_MAGIC2
84 #error "needs gcc for the bitop-__asm__'s"
91 #define MINIX_ROOT_INO 1
92 #define MINIX_BAD_INO 2
94 #define TEST_BUFFER_BLOCKS 16
95 #define MAX_GOOD_BLOCKS 512
97 #define UPPER(size,n) (((size)+((n)-1))/(n))
98 #define INODE_SIZE (sizeof(struct minix_inode))
100 #define INODE_SIZE2 (sizeof(struct minix2_inode))
101 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
102 : MINIX_INODES_PER_BLOCK))
104 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
106 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
108 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
110 static char * program_name = "mkfs";
111 static char * device_name = NULL;
113 static long BLOCKS = 0;
114 static int check = 0;
115 static int badblocks = 0;
116 static int namelen = 30; /* default (changed to 30, per Linus's
117 suggestion, Sun Nov 21 08:05:07 1993) */
118 static int dirsize = 32;
119 static int magic = MINIX_SUPER_MAGIC2;
120 static int version2 = 0;
122 static char root_block[BLOCK_SIZE] = "\0";
124 static char * inode_buffer = NULL;
125 #define Inode (((struct minix_inode *) inode_buffer)-1)
127 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
129 static char super_block_buffer[BLOCK_SIZE];
130 static char boot_block_buffer[512];
131 #define Super (*(struct minix_super_block *)super_block_buffer)
132 #define INODES ((unsigned long)Super.s_ninodes)
134 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
136 #define ZONES ((unsigned long)(Super.s_nzones))
138 #define IMAPS ((unsigned long)Super.s_imap_blocks)
139 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
140 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
141 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
142 #define MAXSIZE ((unsigned long)Super.s_max_size)
143 #define MAGIC (Super.s_magic)
144 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
146 static char *inode_map;
147 static char *zone_map;
149 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
150 static int used_good_blocks = 0;
151 static unsigned long req_nr_inodes = 0;
153 #define inode_in_use(x) (bit(inode_map,(x)))
154 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
156 #define mark_inode(x) (setbit(inode_map,(x)))
157 #define unmark_inode(x) (clrbit(inode_map,(x)))
159 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
160 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
163 * Volatile to let gcc know that this doesn't return. When trying
164 * to compile this under minix, volatile gives a warning, as
165 * exit() isn't defined as volatile under minix.
167 static volatile void die(char *str) {
168 fprintf(stderr, "%s: %s\n", program_name, str);
172 static volatile void show_usage()
174 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
175 fprintf(stderr, "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n", program_name);
176 fprintf(stderr, "Make a MINIX filesystem.\n\n");
177 fprintf(stderr, "OPTIONS:\n");
178 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
179 fprintf(stderr, "\t-n [14|30]\tSpecify the maximum length of filenames\n");
180 fprintf(stderr, "\t-i\t\tSpecify the number of inodes for the filesystem\n");
181 fprintf(stderr, "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
182 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
187 * Check to make certain that our new filesystem won't be created on
188 * an already mounted partition. Code adapted from mke2fs, Copyright
189 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
191 static void check_mount(void)
196 if ((f = setmntent (MOUNTED, "r")) == NULL)
198 while ((mnt = getmntent (f)) != NULL)
199 if (strcmp (device_name, mnt->mnt_fsname) == 0)
205 die("%s is mounted; will not make a filesystem here!");
208 static long valid_offset (int fd, int offset)
212 if (lseek (fd, offset, 0) < 0)
214 if (read (fd, &ch, 1) < 1)
219 static int count_blocks (int fd)
224 for (high = 1; valid_offset (fd, high); high *= 2)
226 while (low < high - 1)
228 const int mid = (low + high) / 2;
230 if (valid_offset (fd, mid))
235 valid_offset (fd, 0);
239 static int get_size(const char *file)
244 fd = open(file, O_RDWR);
249 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
254 size = count_blocks(fd);
259 static void write_tables(void)
261 /* Mark the super block valid. */
262 Super.s_state |= MINIX_VALID_FS;
263 Super.s_state &= ~MINIX_ERROR_FS;
265 if (lseek(DEV, 0, SEEK_SET))
266 die("seek to boot block failed in write_tables");
267 if (512 != write(DEV, boot_block_buffer, 512))
268 die("unable to clear boot sector");
269 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
270 die("seek failed in write_tables");
271 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
272 die("unable to write super-block");
273 if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
274 die("unable to write inode map");
275 if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
276 die("unable to write zone map");
277 if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
278 die("unable to write inodes");
282 static void write_block(int blk, char * buffer)
284 if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
285 die("seek failed in write_block");
286 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
287 die("write failed in write_block");
290 static int get_free_block(void)
294 if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
295 die("too many bad blocks");
296 if (used_good_blocks)
297 blk = good_blocks_table[used_good_blocks-1]+1;
300 while (blk < ZONES && zone_in_use(blk))
303 die("not enough good blocks");
304 good_blocks_table[used_good_blocks] = blk;
309 static void mark_good_blocks(void)
313 for (blk=0 ; blk < used_good_blocks ; blk++)
314 mark_zone(good_blocks_table[blk]);
317 inline int next(int zone)
321 while (++zone < ZONES)
322 if (zone_in_use(zone))
327 static void make_bad_inode(void)
329 struct minix_inode * inode = &Inode[MINIX_BAD_INO];
332 unsigned short ind_block[BLOCK_SIZE>>1];
333 unsigned short dind_block[BLOCK_SIZE>>1];
335 #define NEXT_BAD (zone = next(zone))
339 mark_inode(MINIX_BAD_INO);
341 inode->i_time = time(NULL);
342 inode->i_mode = S_IFREG + 0000;
343 inode->i_size = badblocks*BLOCK_SIZE;
345 for (i=0 ; i<7 ; i++) {
346 inode->i_zone[i] = zone;
350 inode->i_zone[7] = ind = get_free_block();
351 memset(ind_block,0,BLOCK_SIZE);
352 for (i=0 ; i<512 ; i++) {
357 inode->i_zone[8] = dind = get_free_block();
358 memset(dind_block,0,BLOCK_SIZE);
359 for (i=0 ; i<512 ; i++) {
360 write_block(ind,(char *) ind_block);
361 dind_block[i] = ind = get_free_block();
362 memset(ind_block,0,BLOCK_SIZE);
363 for (j=0 ; j<512 ; j++) {
369 die("too many bad blocks");
372 write_block(ind, (char *) ind_block);
374 write_block(dind, (char *) dind_block);
379 make_bad_inode2 (void)
381 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
383 int ind = 0, dind = 0;
384 unsigned long ind_block[BLOCK_SIZE >> 2];
385 unsigned long dind_block[BLOCK_SIZE >> 2];
389 mark_inode (MINIX_BAD_INO);
391 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
392 inode->i_mode = S_IFREG + 0000;
393 inode->i_size = badblocks * BLOCK_SIZE;
395 for (i = 0; i < 7; i++) {
396 inode->i_zone[i] = zone;
400 inode->i_zone[7] = ind = get_free_block ();
401 memset (ind_block, 0, BLOCK_SIZE);
402 for (i = 0; i < 256; i++) {
407 inode->i_zone[8] = dind = get_free_block ();
408 memset (dind_block, 0, BLOCK_SIZE);
409 for (i = 0; i < 256; i++) {
410 write_block (ind, (char *) ind_block);
411 dind_block[i] = ind = get_free_block ();
412 memset (ind_block, 0, BLOCK_SIZE);
413 for (j = 0; j < 256; j++) {
419 /* Could make triple indirect block here */
420 die ("too many bad blocks");
423 write_block (ind, (char *) ind_block);
425 write_block (dind, (char *) dind_block);
429 static void make_root_inode(void)
431 struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
433 mark_inode(MINIX_ROOT_INO);
434 inode->i_zone[0] = get_free_block();
436 inode->i_time = time(NULL);
438 inode->i_size = 3*dirsize;
440 root_block[2*dirsize] = '\0';
441 root_block[2*dirsize+1] = '\0';
442 inode->i_size = 2*dirsize;
444 inode->i_mode = S_IFDIR + 0755;
445 inode->i_uid = getuid();
447 inode->i_gid = getgid();
448 write_block(inode->i_zone[0],root_block);
453 make_root_inode2 (void)
455 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
457 mark_inode (MINIX_ROOT_INO);
458 inode->i_zone[0] = get_free_block ();
460 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
462 inode->i_size = 3 * dirsize;
464 root_block[2 * dirsize] = '\0';
465 root_block[2 * dirsize + 1] = '\0';
466 inode->i_size = 2 * dirsize;
468 inode->i_mode = S_IFDIR + 0755;
469 inode->i_uid = getuid();
471 inode->i_gid = getgid();
472 write_block (inode->i_zone[0], root_block);
476 static void setup_tables(void)
479 unsigned long inodes;
481 memset(super_block_buffer,0,BLOCK_SIZE);
482 memset(boot_block_buffer,0,512);
485 MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024;
487 /* some magic nrs: 1 inode / 3 blocks */
488 if ( req_nr_inodes == 0 )
491 inodes = req_nr_inodes;
492 /* Round up inode count to fill block size */
495 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
496 ~(MINIX2_INODES_PER_BLOCK - 1));
499 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
500 ~(MINIX_INODES_PER_BLOCK - 1));
504 IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK);
507 while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) {
508 ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK);
511 /* Real bad hack but overwise mkfs.minix can be thrown
512 * in infinite loop...
514 * dd if=/dev/zero of=test.fs count=10 bs=1024
515 * /sbin/mkfs.minix -i 200 test.fs
518 die ("unable to allocate buffers for maps");
520 FIRSTZONE = NORM_FIRSTZONE;
521 inode_map = malloc(IMAPS * BLOCK_SIZE);
522 zone_map = malloc(ZMAPS * BLOCK_SIZE);
523 if (!inode_map || !zone_map)
524 die("unable to allocate buffers for maps");
525 memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
526 memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
527 for (i = FIRSTZONE ; i<ZONES ; i++)
529 for (i = MINIX_ROOT_INO ; i<=INODES ; i++)
531 inode_buffer = malloc(INODE_BUFFER_SIZE);
533 die("unable to allocate buffer for inodes");
534 memset(inode_buffer,0,INODE_BUFFER_SIZE);
535 printf("%ld inodes\n",INODES);
536 printf("%ld blocks\n",ZONES);
537 printf("Firstdatazone=%ld (%ld)\n",FIRSTZONE,NORM_FIRSTZONE);
538 printf("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE);
539 printf("Maxsize=%ld\n\n",MAXSIZE);
543 * Perform a test of a block; return the number of
544 * blocks readable/writeable.
546 long do_check(char * buffer, int try, unsigned int current_block)
550 /* Seek to the correct loc. */
551 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
552 current_block * BLOCK_SIZE ) {
553 die("seek failed during testing of blocks");
558 got = read(DEV, buffer, try * BLOCK_SIZE);
559 if (got < 0) got = 0;
560 if (got & (BLOCK_SIZE - 1 )) {
561 printf("Weird values in do_check: probably bugs\n");
567 static unsigned int currently_testing = 0;
569 static void alarm_intr(int alnum)
571 if (currently_testing >= ZONES)
573 signal(SIGALRM,alarm_intr);
575 if (!currently_testing)
577 printf("%d ...", currently_testing);
581 static void check_blocks(void)
584 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
587 signal(SIGALRM,alarm_intr);
589 while (currently_testing < ZONES) {
590 if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
591 currently_testing*BLOCK_SIZE)
592 die("seek failed in check_blocks");
593 try = TEST_BUFFER_BLOCKS;
594 if (currently_testing + try > ZONES)
595 try = ZONES-currently_testing;
596 got = do_check(buffer, try, currently_testing);
597 currently_testing += got;
600 if (currently_testing < FIRSTZONE)
601 die("bad blocks before data-area: cannot make fs");
602 mark_zone(currently_testing);
607 printf("%d bad blocks\n", badblocks);
608 else if (badblocks == 1)
609 printf("one bad block\n");
612 static void get_list_blocks(filename)
617 unsigned long blockno;
619 listfile=fopen(filename,"r");
620 if(listfile == (FILE *)NULL) {
621 die("can't open file of bad blocks");
623 while(!feof(listfile)) {
624 fscanf(listfile,"%ld\n", &blockno);
629 printf("%d bad blocks\n", badblocks);
630 else if (badblocks == 1)
631 printf("one bad block\n");
635 mkfs_minix_main(int argc, char ** argv)
640 char * listfile = NULL;
643 program_name = *argv;
644 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
645 die("bad inode size");
647 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
648 die("bad inode size");
651 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
656 req_nr_inodes = (unsigned long) atol(optarg);
659 listfile = optarg; break;
661 i = strtoul(optarg,&tmp,0);
665 magic = MINIX_SUPER_MAGIC;
667 magic = MINIX_SUPER_MAGIC2;
677 fprintf(stderr,"%s: not compiled with minix v2 support\n",program_name,device_name);
686 if (argc > 0 && !device_name) {
687 device_name = argv[0];
692 BLOCKS = strtol(argv[0],&tmp,0);
694 printf("strtol error: number of blocks not specified");
699 if (device_name && !BLOCKS)
700 BLOCKS = get_size (device_name) / 1024;
701 if (!device_name || BLOCKS<10) {
707 magic = MINIX2_SUPER_MAGIC;
709 magic = MINIX2_SUPER_MAGIC2;
714 check_mount(); /* is it already mounted? */
723 strcpy(tmp+2,".badblocks");
724 DEV = open(device_name,O_RDWR );
726 die("unable to open %s");
727 if (fstat(DEV,&statbuf)<0)
728 die("unable to stat %s");
729 if (!S_ISBLK(statbuf.st_mode))
731 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
732 die("will not try to make filesystem on '%s'");
737 get_list_blocks(listfile);