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 typedef unsigned char u8;
82 typedef unsigned short u16;
83 typedef unsigned int u32;
86 #define MINIX_ROOT_INO 1
87 #define MINIX_LINK_MAX 250
88 #define MINIX2_LINK_MAX 65530
90 #define MINIX_I_MAP_SLOTS 8
91 #define MINIX_Z_MAP_SLOTS 64
92 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
93 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
94 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
95 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
96 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
97 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
99 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
100 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
102 #define MINIX_V1 0x0001 /* original minix fs */
103 #define MINIX_V2 0x0002 /* minix V2 fs */
105 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
108 * This is the original minix inode layout on disk.
109 * Note the 8-bit gid and atime and ctime.
122 * The new minix inode has all the time entries, as well as
123 * long block numbers and a third indirect block (7+1+1+1
124 * instead of 7+1+1). Also, some previously 8-bit values are
125 * now 16-bit. The inode is now 64 bytes instead of 32.
127 struct minix2_inode {
140 * minix super-block data on disk
142 struct minix_super_block {
155 struct minix_dir_entry {
160 #define BLOCK_SIZE_BITS 10
161 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
163 #define NAME_MAX 255 /* # chars in a file name */
165 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
167 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
168 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
170 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
171 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
174 #define BLKGETSIZE _IO(0x12,96) /* return device size */
178 #ifdef MINIX2_SUPER_MAGIC2
179 #define HAVE_MINIX2 1
186 #define MINIX_ROOT_INO 1
187 #define MINIX_BAD_INO 2
189 #define TEST_BUFFER_BLOCKS 16
190 #define MAX_GOOD_BLOCKS 512
192 #define UPPER(size,n) (((size)+((n)-1))/(n))
193 #define INODE_SIZE (sizeof(struct minix_inode))
195 #define INODE_SIZE2 (sizeof(struct minix2_inode))
196 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
197 : MINIX_INODES_PER_BLOCK))
199 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
201 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
203 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
205 static char *device_name = NULL;
207 static long BLOCKS = 0;
208 static int check = 0;
209 static int badblocks = 0;
210 static int namelen = 30; /* default (changed to 30, per Linus's
212 suggestion, Sun Nov 21 08:05:07 1993) */
213 static int dirsize = 32;
214 static int magic = MINIX_SUPER_MAGIC2;
215 static int version2 = 0;
217 static char root_block[BLOCK_SIZE] = "\0";
219 static char *inode_buffer = NULL;
221 #define Inode (((struct minix_inode *) inode_buffer)-1)
223 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
225 static char super_block_buffer[BLOCK_SIZE];
226 static char boot_block_buffer[512];
228 #define Super (*(struct minix_super_block *)super_block_buffer)
229 #define INODES ((unsigned long)Super.s_ninodes)
231 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
233 #define ZONES ((unsigned long)(Super.s_nzones))
235 #define IMAPS ((unsigned long)Super.s_imap_blocks)
236 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
237 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
238 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
239 #define MAXSIZE ((unsigned long)Super.s_max_size)
240 #define MAGIC (Super.s_magic)
241 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
243 static char *inode_map;
244 static char *zone_map;
246 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
247 static int used_good_blocks = 0;
248 static unsigned long req_nr_inodes = 0;
250 #define inode_in_use(x) (isset(inode_map,(x)))
251 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
253 #define mark_inode(x) (setbit(inode_map,(x)))
254 #define unmark_inode(x) (clrbit(inode_map,(x)))
256 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
257 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
260 * Volatile to let gcc know that this doesn't return. When trying
261 * to compile this under minix, volatile gives a warning, as
262 * exit() isn't defined as volatile under minix.
264 static volatile void die(char *str)
266 errorMsg("%s\n", str);
270 static volatile void show_usage() __attribute__ ((noreturn));
271 static volatile void show_usage()
273 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
276 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
278 #ifndef BB_FEATURE_TRIVIAL_HELP
279 fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
280 fprintf(stderr, "Options:\n");
281 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
283 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
285 "\t-i INODES\tSpecify the number of inodes for the filesystem\n");
287 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
288 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
294 * Check to make certain that our new filesystem won't be created on
295 * an already mounted partition. Code adapted from mke2fs, Copyright
296 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
298 static void check_mount(void)
303 if ((f = setmntent(MOUNTED, "r")) == NULL)
305 while ((mnt = getmntent(f)) != NULL)
306 if (strcmp(device_name, mnt->mnt_fsname) == 0)
312 die("%s is mounted; will not make a filesystem here!");
315 static long valid_offset(int fd, int offset)
319 if (lseek(fd, offset, 0) < 0)
321 if (read(fd, &ch, 1) < 1)
326 static int count_blocks(int fd)
331 for (high = 1; valid_offset(fd, high); high *= 2)
333 while (low < high - 1) {
334 const int mid = (low + high) / 2;
336 if (valid_offset(fd, mid))
345 static int get_size(const char *file)
350 fd = open(file, O_RDWR);
355 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
360 size = count_blocks(fd);
365 static void write_tables(void)
367 /* Mark the super block valid. */
368 Super.s_state |= MINIX_VALID_FS;
369 Super.s_state &= ~MINIX_ERROR_FS;
371 if (lseek(DEV, 0, SEEK_SET))
372 die("seek to boot block failed in write_tables");
373 if (512 != write(DEV, boot_block_buffer, 512))
374 die("unable to clear boot sector");
375 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
376 die("seek failed in write_tables");
377 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
378 die("unable to write super-block");
379 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
380 die("unable to write inode map");
381 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
382 die("unable to write zone map");
383 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
384 die("unable to write inodes");
388 static void write_block(int blk, char *buffer)
390 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
391 die("seek failed in write_block");
392 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
393 die("write failed in write_block");
396 static int get_free_block(void)
400 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
401 die("too many bad blocks");
402 if (used_good_blocks)
403 blk = good_blocks_table[used_good_blocks - 1] + 1;
406 while (blk < ZONES && zone_in_use(blk))
409 die("not enough good blocks");
410 good_blocks_table[used_good_blocks] = blk;
415 static void mark_good_blocks(void)
419 for (blk = 0; blk < used_good_blocks; blk++)
420 mark_zone(good_blocks_table[blk]);
423 inline int next(int zone)
426 zone = FIRSTZONE - 1;
427 while (++zone < ZONES)
428 if (zone_in_use(zone))
433 static void make_bad_inode(void)
435 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
437 int ind = 0, dind = 0;
438 unsigned short ind_block[BLOCK_SIZE >> 1];
439 unsigned short dind_block[BLOCK_SIZE >> 1];
441 #define NEXT_BAD (zone = next(zone))
445 mark_inode(MINIX_BAD_INO);
447 inode->i_time = time(NULL);
448 inode->i_mode = S_IFREG + 0000;
449 inode->i_size = badblocks * BLOCK_SIZE;
451 for (i = 0; i < 7; i++) {
452 inode->i_zone[i] = zone;
456 inode->i_zone[7] = ind = get_free_block();
457 memset(ind_block, 0, BLOCK_SIZE);
458 for (i = 0; i < 512; i++) {
463 inode->i_zone[8] = dind = get_free_block();
464 memset(dind_block, 0, BLOCK_SIZE);
465 for (i = 0; i < 512; i++) {
466 write_block(ind, (char *) ind_block);
467 dind_block[i] = ind = get_free_block();
468 memset(ind_block, 0, BLOCK_SIZE);
469 for (j = 0; j < 512; j++) {
475 die("too many bad blocks");
478 write_block(ind, (char *) ind_block);
480 write_block(dind, (char *) dind_block);
484 static void make_bad_inode2(void)
486 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
488 int ind = 0, dind = 0;
489 unsigned long ind_block[BLOCK_SIZE >> 2];
490 unsigned long dind_block[BLOCK_SIZE >> 2];
494 mark_inode(MINIX_BAD_INO);
496 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
497 inode->i_mode = S_IFREG + 0000;
498 inode->i_size = badblocks * BLOCK_SIZE;
500 for (i = 0; i < 7; i++) {
501 inode->i_zone[i] = zone;
505 inode->i_zone[7] = ind = get_free_block();
506 memset(ind_block, 0, BLOCK_SIZE);
507 for (i = 0; i < 256; i++) {
512 inode->i_zone[8] = dind = get_free_block();
513 memset(dind_block, 0, BLOCK_SIZE);
514 for (i = 0; i < 256; i++) {
515 write_block(ind, (char *) ind_block);
516 dind_block[i] = ind = get_free_block();
517 memset(ind_block, 0, BLOCK_SIZE);
518 for (j = 0; j < 256; j++) {
524 /* Could make triple indirect block here */
525 die("too many bad blocks");
528 write_block(ind, (char *) ind_block);
530 write_block(dind, (char *) dind_block);
534 static void make_root_inode(void)
536 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
538 mark_inode(MINIX_ROOT_INO);
539 inode->i_zone[0] = get_free_block();
541 inode->i_time = time(NULL);
543 inode->i_size = 3 * dirsize;
545 root_block[2 * dirsize] = '\0';
546 root_block[2 * dirsize + 1] = '\0';
547 inode->i_size = 2 * dirsize;
549 inode->i_mode = S_IFDIR + 0755;
550 inode->i_uid = getuid();
552 inode->i_gid = getgid();
553 write_block(inode->i_zone[0], root_block);
557 static void make_root_inode2(void)
559 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
561 mark_inode(MINIX_ROOT_INO);
562 inode->i_zone[0] = get_free_block();
564 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
566 inode->i_size = 3 * dirsize;
568 root_block[2 * dirsize] = '\0';
569 root_block[2 * dirsize + 1] = '\0';
570 inode->i_size = 2 * dirsize;
572 inode->i_mode = S_IFDIR + 0755;
573 inode->i_uid = getuid();
575 inode->i_gid = getgid();
576 write_block(inode->i_zone[0], root_block);
580 static void setup_tables(void)
583 unsigned long inodes;
585 memset(super_block_buffer, 0, BLOCK_SIZE);
586 memset(boot_block_buffer, 0, 512);
589 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
591 /* some magic nrs: 1 inode / 3 blocks */
592 if (req_nr_inodes == 0)
595 inodes = req_nr_inodes;
596 /* Round up inode count to fill block size */
599 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
600 ~(MINIX2_INODES_PER_BLOCK - 1));
603 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
604 ~(MINIX_INODES_PER_BLOCK - 1));
608 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
612 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
613 BITS_PER_BLOCK) && i < 1000) {
615 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
619 /* Real bad hack but overwise mkfs.minix can be thrown
620 * in infinite loop...
622 * dd if=/dev/zero of=test.fs count=10 bs=1024
623 * /sbin/mkfs.minix -i 200 test.fs
626 die("unable to allocate buffers for maps");
628 FIRSTZONE = NORM_FIRSTZONE;
629 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
630 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
631 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
632 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
633 for (i = FIRSTZONE; i < ZONES; i++)
635 for (i = MINIX_ROOT_INO; i <= INODES; i++)
637 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
638 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
639 printf("%ld inodes\n", INODES);
640 printf("%ld blocks\n", ZONES);
641 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
642 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
643 printf("Maxsize=%ld\n\n", MAXSIZE);
647 * Perform a test of a block; return the number of
648 * blocks readable/writeable.
650 long do_check(char *buffer, int try, unsigned int current_block)
654 /* Seek to the correct loc. */
655 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
656 current_block * BLOCK_SIZE) {
657 die("seek failed during testing of blocks");
662 got = read(DEV, buffer, try * BLOCK_SIZE);
665 if (got & (BLOCK_SIZE - 1)) {
666 printf("Weird values in do_check: probably bugs\n");
672 static unsigned int currently_testing = 0;
674 static void alarm_intr(int alnum)
676 if (currently_testing >= ZONES)
678 signal(SIGALRM, alarm_intr);
680 if (!currently_testing)
682 printf("%d ...", currently_testing);
686 static void check_blocks(void)
689 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
691 currently_testing = 0;
692 signal(SIGALRM, alarm_intr);
694 while (currently_testing < ZONES) {
695 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
696 currently_testing * BLOCK_SIZE)
697 die("seek failed in check_blocks");
698 try = TEST_BUFFER_BLOCKS;
699 if (currently_testing + try > ZONES)
700 try = ZONES - currently_testing;
701 got = do_check(buffer, try, currently_testing);
702 currently_testing += got;
705 if (currently_testing < FIRSTZONE)
706 die("bad blocks before data-area: cannot make fs");
707 mark_zone(currently_testing);
712 printf("%d bad blocks\n", badblocks);
713 else if (badblocks == 1)
714 printf("one bad block\n");
717 static void get_list_blocks(filename)
722 unsigned long blockno;
724 listfile = fopen(filename, "r");
725 if (listfile == (FILE *) NULL) {
726 die("can't open file of bad blocks");
728 while (!feof(listfile)) {
729 fscanf(listfile, "%ld\n", &blockno);
734 printf("%d bad blocks\n", badblocks);
735 else if (badblocks == 1)
736 printf("one bad block\n");
739 extern int mkfs_minix_main(int argc, char **argv)
744 char *listfile = NULL;
747 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
748 die("bad inode size");
750 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
751 die("bad inode size");
756 while (--argc >= 0 && *argv && **argv) {
759 while (i > 0 && *++(*argv) && stopIt==FALSE) {
767 if (*(*argv+1) != 0) {
775 req_nr_inodes = strtoul(cp, &tmp, 0);
785 listfile = *(++argv);
791 if (*(*argv+1) != 0) {
799 i = strtoul(cp, &tmp, 0);
803 magic = MINIX_SUPER_MAGIC;
805 magic = MINIX_SUPER_MAGIC2;
817 errorMsg("%s: not compiled with minix v2 support\n",
830 if (device_name == NULL)
832 else if (BLOCKS == 0)
833 BLOCKS = strtol(*argv, &tmp, 0);
841 if (device_name && !BLOCKS)
842 BLOCKS = get_size(device_name) / 1024;
843 if (!device_name || BLOCKS < 10) {
849 magic = MINIX2_SUPER_MAGIC;
851 magic = MINIX2_SUPER_MAGIC2;
856 check_mount(); /* is it already mounted? */
859 strcpy(tmp + 2, ".");
862 strcpy(tmp + 2, "..");
865 strcpy(tmp + 2, ".badblocks");
866 DEV = open(device_name, O_RDWR);
868 die("unable to open %s");
869 if (fstat(DEV, &statbuf) < 0)
870 die("unable to stat %s");
871 if (!S_ISBLK(statbuf.st_mode))
873 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
874 die("will not try to make filesystem on '%s'");
879 get_list_blocks(listfile);