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>
77 #include <sys/param.h>
81 #include <linux/minix_fs.h>
83 #ifdef MINIX2_SUPER_MAGIC2
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
118 suggestion, Sun Nov 21 08:05:07 1993) */
119 static int dirsize = 32;
120 static int magic = MINIX_SUPER_MAGIC2;
121 static int version2 = 0;
123 static char root_block[BLOCK_SIZE] = "\0";
125 static char *inode_buffer = NULL;
127 #define Inode (((struct minix_inode *) inode_buffer)-1)
129 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
131 static char super_block_buffer[BLOCK_SIZE];
132 static char boot_block_buffer[512];
134 #define Super (*(struct minix_super_block *)super_block_buffer)
135 #define INODES ((unsigned long)Super.s_ninodes)
137 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
139 #define ZONES ((unsigned long)(Super.s_nzones))
141 #define IMAPS ((unsigned long)Super.s_imap_blocks)
142 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
143 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
144 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
145 #define MAXSIZE ((unsigned long)Super.s_max_size)
146 #define MAGIC (Super.s_magic)
147 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
149 static char *inode_map;
150 static char *zone_map;
152 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
153 static int used_good_blocks = 0;
154 static unsigned long req_nr_inodes = 0;
156 #define inode_in_use(x) (isset(inode_map,(x)))
157 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
159 #define mark_inode(x) (setbit(inode_map,(x)))
160 #define unmark_inode(x) (clrbit(inode_map,(x)))
162 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
163 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
166 * Volatile to let gcc know that this doesn't return. When trying
167 * to compile this under minix, volatile gives a warning, as
168 * exit() isn't defined as volatile under minix.
170 static volatile void die(char *str)
172 fprintf(stderr, "%s: %s\n", program_name, str);
176 static volatile void show_usage() __attribute__ ((noreturn));
177 static volatile void show_usage()
179 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
182 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
184 #ifndef BB_FEATURE_TRIVIAL_HELP
185 fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
186 fprintf(stderr, "OPTIONS:\n");
187 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
189 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
191 "\t-i INODES\tSpecify the number of inodes for the filesystem\n");
193 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
194 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
200 * Check to make certain that our new filesystem won't be created on
201 * an already mounted partition. Code adapted from mke2fs, Copyright
202 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
204 static void check_mount(void)
209 if ((f = setmntent(MOUNTED, "r")) == NULL)
211 while ((mnt = getmntent(f)) != NULL)
212 if (strcmp(device_name, mnt->mnt_fsname) == 0)
218 die("%s is mounted; will not make a filesystem here!");
221 static long valid_offset(int fd, int offset)
225 if (lseek(fd, offset, 0) < 0)
227 if (read(fd, &ch, 1) < 1)
232 static int count_blocks(int fd)
237 for (high = 1; valid_offset(fd, high); high *= 2)
239 while (low < high - 1) {
240 const int mid = (low + high) / 2;
242 if (valid_offset(fd, mid))
251 static int get_size(const char *file)
256 fd = open(file, O_RDWR);
261 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
266 size = count_blocks(fd);
271 static void write_tables(void)
273 /* Mark the super block valid. */
274 Super.s_state |= MINIX_VALID_FS;
275 Super.s_state &= ~MINIX_ERROR_FS;
277 if (lseek(DEV, 0, SEEK_SET))
278 die("seek to boot block failed in write_tables");
279 if (512 != write(DEV, boot_block_buffer, 512))
280 die("unable to clear boot sector");
281 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
282 die("seek failed in write_tables");
283 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
284 die("unable to write super-block");
285 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
286 die("unable to write inode map");
287 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
288 die("unable to write zone map");
289 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
290 die("unable to write inodes");
294 static void write_block(int blk, char *buffer)
296 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
297 die("seek failed in write_block");
298 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
299 die("write failed in write_block");
302 static int get_free_block(void)
306 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
307 die("too many bad blocks");
308 if (used_good_blocks)
309 blk = good_blocks_table[used_good_blocks - 1] + 1;
312 while (blk < ZONES && zone_in_use(blk))
315 die("not enough good blocks");
316 good_blocks_table[used_good_blocks] = blk;
321 static void mark_good_blocks(void)
325 for (blk = 0; blk < used_good_blocks; blk++)
326 mark_zone(good_blocks_table[blk]);
329 inline int next(int zone)
332 zone = FIRSTZONE - 1;
333 while (++zone < ZONES)
334 if (zone_in_use(zone))
339 static void make_bad_inode(void)
341 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
343 int ind = 0, dind = 0;
344 unsigned short ind_block[BLOCK_SIZE >> 1];
345 unsigned short dind_block[BLOCK_SIZE >> 1];
347 #define NEXT_BAD (zone = next(zone))
351 mark_inode(MINIX_BAD_INO);
353 inode->i_time = time(NULL);
354 inode->i_mode = S_IFREG + 0000;
355 inode->i_size = badblocks * BLOCK_SIZE;
357 for (i = 0; i < 7; i++) {
358 inode->i_zone[i] = zone;
362 inode->i_zone[7] = ind = get_free_block();
363 memset(ind_block, 0, BLOCK_SIZE);
364 for (i = 0; i < 512; i++) {
369 inode->i_zone[8] = dind = get_free_block();
370 memset(dind_block, 0, BLOCK_SIZE);
371 for (i = 0; i < 512; i++) {
372 write_block(ind, (char *) ind_block);
373 dind_block[i] = ind = get_free_block();
374 memset(ind_block, 0, BLOCK_SIZE);
375 for (j = 0; j < 512; j++) {
381 die("too many bad blocks");
384 write_block(ind, (char *) ind_block);
386 write_block(dind, (char *) dind_block);
390 static void make_bad_inode2(void)
392 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
394 int ind = 0, dind = 0;
395 unsigned long ind_block[BLOCK_SIZE >> 2];
396 unsigned long dind_block[BLOCK_SIZE >> 2];
400 mark_inode(MINIX_BAD_INO);
402 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
403 inode->i_mode = S_IFREG + 0000;
404 inode->i_size = badblocks * BLOCK_SIZE;
406 for (i = 0; i < 7; i++) {
407 inode->i_zone[i] = zone;
411 inode->i_zone[7] = ind = get_free_block();
412 memset(ind_block, 0, BLOCK_SIZE);
413 for (i = 0; i < 256; i++) {
418 inode->i_zone[8] = dind = get_free_block();
419 memset(dind_block, 0, BLOCK_SIZE);
420 for (i = 0; i < 256; i++) {
421 write_block(ind, (char *) ind_block);
422 dind_block[i] = ind = get_free_block();
423 memset(ind_block, 0, BLOCK_SIZE);
424 for (j = 0; j < 256; j++) {
430 /* Could make triple indirect block here */
431 die("too many bad blocks");
434 write_block(ind, (char *) ind_block);
436 write_block(dind, (char *) dind_block);
440 static void make_root_inode(void)
442 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
444 mark_inode(MINIX_ROOT_INO);
445 inode->i_zone[0] = get_free_block();
447 inode->i_time = time(NULL);
449 inode->i_size = 3 * dirsize;
451 root_block[2 * dirsize] = '\0';
452 root_block[2 * dirsize + 1] = '\0';
453 inode->i_size = 2 * dirsize;
455 inode->i_mode = S_IFDIR + 0755;
456 inode->i_uid = getuid();
458 inode->i_gid = getgid();
459 write_block(inode->i_zone[0], root_block);
463 static void make_root_inode2(void)
465 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
467 mark_inode(MINIX_ROOT_INO);
468 inode->i_zone[0] = get_free_block();
470 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
472 inode->i_size = 3 * dirsize;
474 root_block[2 * dirsize] = '\0';
475 root_block[2 * dirsize + 1] = '\0';
476 inode->i_size = 2 * dirsize;
478 inode->i_mode = S_IFDIR + 0755;
479 inode->i_uid = getuid();
481 inode->i_gid = getgid();
482 write_block(inode->i_zone[0], root_block);
486 static void setup_tables(void)
489 unsigned long inodes;
491 memset(super_block_buffer, 0, BLOCK_SIZE);
492 memset(boot_block_buffer, 0, 512);
495 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
497 /* some magic nrs: 1 inode / 3 blocks */
498 if (req_nr_inodes == 0)
501 inodes = req_nr_inodes;
502 /* Round up inode count to fill block size */
505 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
506 ~(MINIX2_INODES_PER_BLOCK - 1));
509 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
510 ~(MINIX_INODES_PER_BLOCK - 1));
514 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
518 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
519 BITS_PER_BLOCK) && i < 1000) {
521 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
525 /* Real bad hack but overwise mkfs.minix can be thrown
526 * in infinite loop...
528 * dd if=/dev/zero of=test.fs count=10 bs=1024
529 * /sbin/mkfs.minix -i 200 test.fs
532 die("unable to allocate buffers for maps");
534 FIRSTZONE = NORM_FIRSTZONE;
535 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
536 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
537 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
538 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
539 for (i = FIRSTZONE; i < ZONES; i++)
541 for (i = MINIX_ROOT_INO; i <= INODES; i++)
543 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
544 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
545 printf("%ld inodes\n", INODES);
546 printf("%ld blocks\n", ZONES);
547 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
548 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
549 printf("Maxsize=%ld\n\n", MAXSIZE);
553 * Perform a test of a block; return the number of
554 * blocks readable/writeable.
556 long do_check(char *buffer, int try, unsigned int current_block)
560 /* Seek to the correct loc. */
561 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
562 current_block * BLOCK_SIZE) {
563 die("seek failed during testing of blocks");
568 got = read(DEV, buffer, try * BLOCK_SIZE);
571 if (got & (BLOCK_SIZE - 1)) {
572 printf("Weird values in do_check: probably bugs\n");
578 static unsigned int currently_testing = 0;
580 static void alarm_intr(int alnum)
582 if (currently_testing >= ZONES)
584 signal(SIGALRM, alarm_intr);
586 if (!currently_testing)
588 printf("%d ...", currently_testing);
592 static void check_blocks(void)
595 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
597 currently_testing = 0;
598 signal(SIGALRM, alarm_intr);
600 while (currently_testing < ZONES) {
601 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
602 currently_testing * BLOCK_SIZE)
603 die("seek failed in check_blocks");
604 try = TEST_BUFFER_BLOCKS;
605 if (currently_testing + try > ZONES)
606 try = ZONES - currently_testing;
607 got = do_check(buffer, try, currently_testing);
608 currently_testing += got;
611 if (currently_testing < FIRSTZONE)
612 die("bad blocks before data-area: cannot make fs");
613 mark_zone(currently_testing);
618 printf("%d bad blocks\n", badblocks);
619 else if (badblocks == 1)
620 printf("one bad block\n");
623 static void get_list_blocks(filename)
628 unsigned long blockno;
630 listfile = fopen(filename, "r");
631 if (listfile == (FILE *) NULL) {
632 die("can't open file of bad blocks");
634 while (!feof(listfile)) {
635 fscanf(listfile, "%ld\n", &blockno);
640 printf("%d bad blocks\n", badblocks);
641 else if (badblocks == 1)
642 printf("one bad block\n");
645 extern int mkfs_minix_main(int argc, char **argv)
650 char *listfile = NULL;
654 program_name = *argv;
655 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
656 die("bad inode size");
658 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
659 die("bad inode size");
663 //printf("argc='%d' argv='%s'\n", argc, *argv);
665 while (--argc >= 0 && *argv && **argv) {
668 while (i > 0 && *++(*argv) && stopIt==FALSE) {
669 //printf("argc='%d' argv='%s'\n", argc, *argv);
678 req_nr_inodes = (unsigned long) atol(*(++argv));
684 listfile = *(++argv);
693 if (*(*argv+1) != 0) {
698 i = strtoul(cp, &tmp, 0);
699 //printf("cp='%s' i='%d'\n", cp, i);
703 magic = MINIX_SUPER_MAGIC;
705 magic = MINIX_SUPER_MAGIC2;
717 fprintf(stderr, "%s: not compiled with minix v2 support\n",
718 program_name, device_name);
730 //printf("else: argc='%d' argv='%s'\n", argc, *argv);
731 if (device_name == NULL)
733 else if (BLOCKS == 0)
734 BLOCKS = strtol(*argv, &tmp, 0);
742 if (device_name && !BLOCKS)
743 BLOCKS = get_size(device_name) / 1024;
744 if (!device_name || BLOCKS < 10) {
750 magic = MINIX2_SUPER_MAGIC;
752 magic = MINIX2_SUPER_MAGIC2;
757 check_mount(); /* is it already mounted? */
760 strcpy(tmp + 2, ".");
763 strcpy(tmp + 2, "..");
766 strcpy(tmp + 2, ".badblocks");
767 DEV = open(device_name, O_RDWR);
769 die("unable to open %s");
770 if (fstat(DEV, &statbuf) < 0)
771 die("unable to stat %s");
772 if (!S_ISBLK(statbuf.st_mode))
774 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
775 die("will not try to make filesystem on '%s'");
780 get_list_blocks(listfile);