1 /* vi: set sw=4 ts=4: */
3 * mkfs.c - make a linux (minix) file-system.
5 * (C) 1991 Linus Torvalds. This file may be redistributed as per
12 * 24.11.91 - Time began. Used the fsck sources to get started.
14 * 25.11.91 - Corrected some bugs. Added support for ".badblocks"
15 * The algorithm for ".badblocks" is a bit weird, but
16 * it should work. Oh, well.
18 * 25.01.92 - Added the -l option for getting the list of bad blocks
19 * out of a named file. (Dave Rivers, rivers@ponds.uucp)
21 * 28.02.92 - Added %-information when using -c.
23 * 28.02.93 - Added support for other namelengths than the original
24 * 14 characters so that I can test the new kernel routines..
26 * 09.10.93 - Make exit status conform to that required by fsutil
27 * (Rik Faith, faith@cs.unc.edu)
29 * 31.10.93 - Added inode request feature, for backup floppies: use
30 * 32 inodes, for a news partition use more.
31 * (Scott Heavner, sdh@po.cwru.edu)
33 * 03.01.94 - Added support for file system valid flag.
34 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
36 * 30.10.94 - added support for v2 filesystem
37 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
39 * 09.11.94 - Added test to prevent overwrite of mounted fs adapted
40 * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
41 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
43 * 03.20.95 - Clear first 512 bytes of filesystem to make certain that
44 * the filesystem is not misidentified as a MS-DOS FAT filesystem.
45 * (Daniel Quinlan, quinlan@yggdrasil.com)
47 * 02.07.96 - Added small patch from Russell King to make the program a
48 * good deal more portable (janl@math.uio.no)
50 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
52 * -c for readablility checking (SLOW!)
53 * -l for getting a list of bad blocks from a file.
54 * -n for namelength (currently the kernel only uses 14 or 30)
55 * -i for number of inodes
56 * -v for v2 filesystem
58 * The device may be a block device or a image of one, but this isn't
59 * enforced (but it's not much fun on a character device :-).
61 * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
62 * removed getopt based parser and added a hand rolled one.
76 #include <sys/ioctl.h>
80 #include <linux/minix_fs.h>
82 #ifdef MINIX2_SUPER_MAGIC2
87 #error "needs gcc for the bitop-__asm__'s"
94 #define MINIX_ROOT_INO 1
95 #define MINIX_BAD_INO 2
97 #define TEST_BUFFER_BLOCKS 16
98 #define MAX_GOOD_BLOCKS 512
100 #define UPPER(size,n) (((size)+((n)-1))/(n))
101 #define INODE_SIZE (sizeof(struct minix_inode))
103 #define INODE_SIZE2 (sizeof(struct minix2_inode))
104 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
105 : MINIX_INODES_PER_BLOCK))
107 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
109 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
111 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
113 static char *program_name = "mkfs";
114 static char *device_name = NULL;
116 static long BLOCKS = 0;
117 static int check = 0;
118 static int badblocks = 0;
119 static int namelen = 30; /* default (changed to 30, per Linus's
121 suggestion, Sun Nov 21 08:05:07 1993) */
122 static int dirsize = 32;
123 static int magic = MINIX_SUPER_MAGIC2;
124 static int version2 = 0;
126 static char root_block[BLOCK_SIZE] = "\0";
128 static char *inode_buffer = NULL;
130 #define Inode (((struct minix_inode *) inode_buffer)-1)
132 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
134 static char super_block_buffer[BLOCK_SIZE];
135 static char boot_block_buffer[512];
137 #define Super (*(struct minix_super_block *)super_block_buffer)
138 #define INODES ((unsigned long)Super.s_ninodes)
140 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
142 #define ZONES ((unsigned long)(Super.s_nzones))
144 #define IMAPS ((unsigned long)Super.s_imap_blocks)
145 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
146 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
147 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
148 #define MAXSIZE ((unsigned long)Super.s_max_size)
149 #define MAGIC (Super.s_magic)
150 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
152 static char *inode_map;
153 static char *zone_map;
155 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
156 static int used_good_blocks = 0;
157 static unsigned long req_nr_inodes = 0;
159 #define inode_in_use(x) (isset(inode_map,(x)))
160 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
162 #define mark_inode(x) (setbit(inode_map,(x)))
163 #define unmark_inode(x) (clrbit(inode_map,(x)))
165 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
166 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
169 * Volatile to let gcc know that this doesn't return. When trying
170 * to compile this under minix, volatile gives a warning, as
171 * exit() isn't defined as volatile under minix.
173 static volatile void die(char *str)
175 fprintf(stderr, "%s: %s\n", program_name, str);
179 static volatile void show_usage() __attribute__ ((noreturn));
180 static volatile void show_usage()
182 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
185 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
187 #ifndef BB_FEATURE_TRIVIAL_HELP
188 fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
189 fprintf(stderr, "OPTIONS:\n");
190 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
192 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
194 "\t-i\t\tSpecify the number of inodes for the filesystem\n");
196 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
197 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
203 * Check to make certain that our new filesystem won't be created on
204 * an already mounted partition. Code adapted from mke2fs, Copyright
205 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
207 static void check_mount(void)
212 if ((f = setmntent(MOUNTED, "r")) == NULL)
214 while ((mnt = getmntent(f)) != NULL)
215 if (strcmp(device_name, mnt->mnt_fsname) == 0)
221 die("%s is mounted; will not make a filesystem here!");
224 static long valid_offset(int fd, int offset)
228 if (lseek(fd, offset, 0) < 0)
230 if (read(fd, &ch, 1) < 1)
235 static int count_blocks(int fd)
240 for (high = 1; valid_offset(fd, high); high *= 2)
242 while (low < high - 1) {
243 const int mid = (low + high) / 2;
245 if (valid_offset(fd, mid))
254 static int get_size(const char *file)
259 fd = open(file, O_RDWR);
264 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
269 size = count_blocks(fd);
274 static void write_tables(void)
276 /* Mark the super block valid. */
277 Super.s_state |= MINIX_VALID_FS;
278 Super.s_state &= ~MINIX_ERROR_FS;
280 if (lseek(DEV, 0, SEEK_SET))
281 die("seek to boot block failed in write_tables");
282 if (512 != write(DEV, boot_block_buffer, 512))
283 die("unable to clear boot sector");
284 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
285 die("seek failed in write_tables");
286 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
287 die("unable to write super-block");
288 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
289 die("unable to write inode map");
290 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
291 die("unable to write zone map");
292 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
293 die("unable to write inodes");
297 static void write_block(int blk, char *buffer)
299 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
300 die("seek failed in write_block");
301 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
302 die("write failed in write_block");
305 static int get_free_block(void)
309 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
310 die("too many bad blocks");
311 if (used_good_blocks)
312 blk = good_blocks_table[used_good_blocks - 1] + 1;
315 while (blk < ZONES && zone_in_use(blk))
318 die("not enough good blocks");
319 good_blocks_table[used_good_blocks] = blk;
324 static void mark_good_blocks(void)
328 for (blk = 0; blk < used_good_blocks; blk++)
329 mark_zone(good_blocks_table[blk]);
332 inline int next(int zone)
335 zone = FIRSTZONE - 1;
336 while (++zone < ZONES)
337 if (zone_in_use(zone))
342 static void make_bad_inode(void)
344 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
346 int ind = 0, dind = 0;
347 unsigned short ind_block[BLOCK_SIZE >> 1];
348 unsigned short dind_block[BLOCK_SIZE >> 1];
350 #define NEXT_BAD (zone = next(zone))
354 mark_inode(MINIX_BAD_INO);
356 inode->i_time = time(NULL);
357 inode->i_mode = S_IFREG + 0000;
358 inode->i_size = badblocks * BLOCK_SIZE;
360 for (i = 0; i < 7; i++) {
361 inode->i_zone[i] = zone;
365 inode->i_zone[7] = ind = get_free_block();
366 memset(ind_block, 0, BLOCK_SIZE);
367 for (i = 0; i < 512; i++) {
372 inode->i_zone[8] = dind = get_free_block();
373 memset(dind_block, 0, BLOCK_SIZE);
374 for (i = 0; i < 512; i++) {
375 write_block(ind, (char *) ind_block);
376 dind_block[i] = ind = get_free_block();
377 memset(ind_block, 0, BLOCK_SIZE);
378 for (j = 0; j < 512; j++) {
384 die("too many bad blocks");
387 write_block(ind, (char *) ind_block);
389 write_block(dind, (char *) dind_block);
393 static void make_bad_inode2(void)
395 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
397 int ind = 0, dind = 0;
398 unsigned long ind_block[BLOCK_SIZE >> 2];
399 unsigned long dind_block[BLOCK_SIZE >> 2];
403 mark_inode(MINIX_BAD_INO);
405 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
406 inode->i_mode = S_IFREG + 0000;
407 inode->i_size = badblocks * BLOCK_SIZE;
409 for (i = 0; i < 7; i++) {
410 inode->i_zone[i] = zone;
414 inode->i_zone[7] = ind = get_free_block();
415 memset(ind_block, 0, BLOCK_SIZE);
416 for (i = 0; i < 256; i++) {
421 inode->i_zone[8] = dind = get_free_block();
422 memset(dind_block, 0, BLOCK_SIZE);
423 for (i = 0; i < 256; i++) {
424 write_block(ind, (char *) ind_block);
425 dind_block[i] = ind = get_free_block();
426 memset(ind_block, 0, BLOCK_SIZE);
427 for (j = 0; j < 256; j++) {
433 /* Could make triple indirect block here */
434 die("too many bad blocks");
437 write_block(ind, (char *) ind_block);
439 write_block(dind, (char *) dind_block);
443 static void make_root_inode(void)
445 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
447 mark_inode(MINIX_ROOT_INO);
448 inode->i_zone[0] = get_free_block();
450 inode->i_time = time(NULL);
452 inode->i_size = 3 * dirsize;
454 root_block[2 * dirsize] = '\0';
455 root_block[2 * dirsize + 1] = '\0';
456 inode->i_size = 2 * dirsize;
458 inode->i_mode = S_IFDIR + 0755;
459 inode->i_uid = getuid();
461 inode->i_gid = getgid();
462 write_block(inode->i_zone[0], root_block);
466 static void make_root_inode2(void)
468 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
470 mark_inode(MINIX_ROOT_INO);
471 inode->i_zone[0] = get_free_block();
473 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
475 inode->i_size = 3 * dirsize;
477 root_block[2 * dirsize] = '\0';
478 root_block[2 * dirsize + 1] = '\0';
479 inode->i_size = 2 * dirsize;
481 inode->i_mode = S_IFDIR + 0755;
482 inode->i_uid = getuid();
484 inode->i_gid = getgid();
485 write_block(inode->i_zone[0], root_block);
489 static void setup_tables(void)
492 unsigned long inodes;
494 memset(super_block_buffer, 0, BLOCK_SIZE);
495 memset(boot_block_buffer, 0, 512);
498 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
500 /* some magic nrs: 1 inode / 3 blocks */
501 if (req_nr_inodes == 0)
504 inodes = req_nr_inodes;
505 /* Round up inode count to fill block size */
508 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
509 ~(MINIX2_INODES_PER_BLOCK - 1));
512 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
513 ~(MINIX_INODES_PER_BLOCK - 1));
517 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
521 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
522 BITS_PER_BLOCK) && i < 1000) {
524 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
528 /* Real bad hack but overwise mkfs.minix can be thrown
529 * in infinite loop...
531 * dd if=/dev/zero of=test.fs count=10 bs=1024
532 * /sbin/mkfs.minix -i 200 test.fs
535 die("unable to allocate buffers for maps");
537 FIRSTZONE = NORM_FIRSTZONE;
538 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
539 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
540 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
541 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
542 for (i = FIRSTZONE; i < ZONES; i++)
544 for (i = MINIX_ROOT_INO; i <= INODES; i++)
546 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
547 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
548 printf("%ld inodes\n", INODES);
549 printf("%ld blocks\n", ZONES);
550 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
551 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
552 printf("Maxsize=%ld\n\n", MAXSIZE);
556 * Perform a test of a block; return the number of
557 * blocks readable/writeable.
559 long do_check(char *buffer, int try, unsigned int current_block)
563 /* Seek to the correct loc. */
564 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
565 current_block * BLOCK_SIZE) {
566 die("seek failed during testing of blocks");
571 got = read(DEV, buffer, try * BLOCK_SIZE);
574 if (got & (BLOCK_SIZE - 1)) {
575 printf("Weird values in do_check: probably bugs\n");
581 static unsigned int currently_testing = 0;
583 static void alarm_intr(int alnum)
585 if (currently_testing >= ZONES)
587 signal(SIGALRM, alarm_intr);
589 if (!currently_testing)
591 printf("%d ...", currently_testing);
595 static void check_blocks(void)
598 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
600 currently_testing = 0;
601 signal(SIGALRM, alarm_intr);
603 while (currently_testing < ZONES) {
604 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
605 currently_testing * BLOCK_SIZE)
606 die("seek failed in check_blocks");
607 try = TEST_BUFFER_BLOCKS;
608 if (currently_testing + try > ZONES)
609 try = ZONES - currently_testing;
610 got = do_check(buffer, try, currently_testing);
611 currently_testing += got;
614 if (currently_testing < FIRSTZONE)
615 die("bad blocks before data-area: cannot make fs");
616 mark_zone(currently_testing);
621 printf("%d bad blocks\n", badblocks);
622 else if (badblocks == 1)
623 printf("one bad block\n");
626 static void get_list_blocks(filename)
631 unsigned long blockno;
633 listfile = fopen(filename, "r");
634 if (listfile == (FILE *) NULL) {
635 die("can't open file of bad blocks");
637 while (!feof(listfile)) {
638 fscanf(listfile, "%ld\n", &blockno);
643 printf("%d bad blocks\n", badblocks);
644 else if (badblocks == 1)
645 printf("one bad block\n");
648 extern int mkfs_minix_main(int argc, char **argv)
653 char *listfile = NULL;
657 program_name = *argv;
658 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
659 die("bad inode size");
661 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
662 die("bad inode size");
666 //printf("argc='%d' argv='%s'\n", argc, *argv);
668 while (--argc >= 0 && *argv && **argv) {
671 while (i > 0 && *++(*argv) && stopIt==FALSE) {
672 //printf("argc='%d' argv='%s'\n", argc, *argv);
681 req_nr_inodes = (unsigned long) atol(*(++argv));
687 listfile = *(++argv);
696 if (*(*argv+1) != 0) {
701 i = strtoul(cp, &tmp, 0);
702 //printf("cp='%s' i='%d'\n", cp, i);
706 magic = MINIX_SUPER_MAGIC;
708 magic = MINIX_SUPER_MAGIC2;
720 fprintf(stderr, "%s: not compiled with minix v2 support\n",
721 program_name, device_name);
733 //printf("else: argc='%d' argv='%s'\n", argc, *argv);
734 if (device_name == NULL)
736 else if (BLOCKS == 0)
737 BLOCKS = strtol(*argv, &tmp, 0);
745 if (device_name && !BLOCKS)
746 BLOCKS = get_size(device_name) / 1024;
747 if (!device_name || BLOCKS < 10) {
753 magic = MINIX2_SUPER_MAGIC;
755 magic = MINIX2_SUPER_MAGIC2;
760 check_mount(); /* is it already mounted? */
763 strcpy(tmp + 2, ".");
766 strcpy(tmp + 2, "..");
769 strcpy(tmp + 2, ".badblocks");
770 DEV = open(device_name, O_RDWR);
772 die("unable to open %s");
773 if (fstat(DEV, &statbuf) < 0)
774 die("unable to stat %s");
775 if (!S_ISBLK(statbuf.st_mode))
777 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
778 die("will not try to make filesystem on '%s'");
783 get_list_blocks(listfile);