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.
75 #include <sys/ioctl.h>
76 #include <sys/param.h>
80 typedef unsigned char u8;
81 typedef unsigned short u16;
82 typedef unsigned int u32;
85 #define MINIX_ROOT_INO 1
86 #define MINIX_LINK_MAX 250
87 #define MINIX2_LINK_MAX 65530
89 #define MINIX_I_MAP_SLOTS 8
90 #define MINIX_Z_MAP_SLOTS 64
91 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
92 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
93 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
94 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
95 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
96 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
98 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
99 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
101 #define MINIX_V1 0x0001 /* original minix fs */
102 #define MINIX_V2 0x0002 /* minix V2 fs */
104 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
107 * This is the original minix inode layout on disk.
108 * Note the 8-bit gid and atime and ctime.
121 * The new minix inode has all the time entries, as well as
122 * long block numbers and a third indirect block (7+1+1+1
123 * instead of 7+1+1). Also, some previously 8-bit values are
124 * now 16-bit. The inode is now 64 bytes instead of 32.
126 struct minix2_inode {
139 * minix super-block data on disk
141 struct minix_super_block {
154 struct minix_dir_entry {
159 #define BLOCK_SIZE_BITS 10
160 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
162 #define NAME_MAX 255 /* # chars in a file name */
164 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
166 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
167 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
169 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
170 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
173 #define BLKGETSIZE _IO(0x12,96) /* return device size */
181 #define MINIX_ROOT_INO 1
182 #define MINIX_BAD_INO 2
184 #define TEST_BUFFER_BLOCKS 16
185 #define MAX_GOOD_BLOCKS 512
187 #define UPPER(size,n) (((size)+((n)-1))/(n))
188 #define INODE_SIZE (sizeof(struct minix_inode))
189 #ifdef BB_FEATURE_MINIX2
190 #define INODE_SIZE2 (sizeof(struct minix2_inode))
191 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
192 : MINIX_INODES_PER_BLOCK))
194 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
196 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
198 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
200 static char *device_name = NULL;
202 static long BLOCKS = 0;
203 static int check = 0;
204 static int badblocks = 0;
205 static int namelen = 30; /* default (changed to 30, per Linus's
207 suggestion, Sun Nov 21 08:05:07 1993) */
208 static int dirsize = 32;
209 static int magic = MINIX_SUPER_MAGIC2;
210 static int version2 = 0;
212 static char root_block[BLOCK_SIZE] = "\0";
214 static char *inode_buffer = NULL;
216 #define Inode (((struct minix_inode *) inode_buffer)-1)
217 #ifdef BB_FEATURE_MINIX2
218 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
220 static char super_block_buffer[BLOCK_SIZE];
221 static char boot_block_buffer[512];
223 #define Super (*(struct minix_super_block *)super_block_buffer)
224 #define INODES ((unsigned long)Super.s_ninodes)
225 #ifdef BB_FEATURE_MINIX2
226 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
228 #define ZONES ((unsigned long)(Super.s_nzones))
230 #define IMAPS ((unsigned long)Super.s_imap_blocks)
231 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
232 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
233 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
234 #define MAXSIZE ((unsigned long)Super.s_max_size)
235 #define MAGIC (Super.s_magic)
236 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
238 static char *inode_map;
239 static char *zone_map;
241 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
242 static int used_good_blocks = 0;
243 static unsigned long req_nr_inodes = 0;
245 static inline int bit(char * a,unsigned int i)
247 return (a[i >> 3] & (1<<(i & 7))) != 0;
249 #define inode_in_use(x) (bit(inode_map,(x)))
250 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
252 #define mark_inode(x) (setbit(inode_map,(x)))
253 #define unmark_inode(x) (clrbit(inode_map,(x)))
255 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
256 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
259 * Volatile to let gcc know that this doesn't return. When trying
260 * to compile this under minix, volatile gives a warning, as
261 * exit() isn't defined as volatile under minix.
263 static volatile void die(char *str)
265 error_msg("%s\n", str);
269 static volatile void show_usage() __attribute__ ((noreturn));
270 static volatile void show_usage()
272 usage(mkfs_minix_usage);
276 * Check to make certain that our new filesystem won't be created on
277 * an already mounted partition. Code adapted from mke2fs, Copyright
278 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
280 static void check_mount(void)
285 if ((f = setmntent(MOUNTED, "r")) == NULL)
287 while ((mnt = getmntent(f)) != NULL)
288 if (strcmp(device_name, mnt->mnt_fsname) == 0)
294 die("%s is mounted; will not make a filesystem here!");
297 static long valid_offset(int fd, int offset)
301 if (lseek(fd, offset, 0) < 0)
303 if (read(fd, &ch, 1) < 1)
308 static int count_blocks(int fd)
313 for (high = 1; valid_offset(fd, high); high *= 2)
315 while (low < high - 1) {
316 const int mid = (low + high) / 2;
318 if (valid_offset(fd, mid))
327 static int get_size(const char *file)
332 fd = open(file, O_RDWR);
337 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
342 size = count_blocks(fd);
347 static void write_tables(void)
349 /* Mark the super block valid. */
350 Super.s_state |= MINIX_VALID_FS;
351 Super.s_state &= ~MINIX_ERROR_FS;
353 if (lseek(DEV, 0, SEEK_SET))
354 die("seek to boot block failed in write_tables");
355 if (512 != write(DEV, boot_block_buffer, 512))
356 die("unable to clear boot sector");
357 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
358 die("seek failed in write_tables");
359 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
360 die("unable to write super-block");
361 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
362 die("unable to write inode map");
363 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
364 die("unable to write zone map");
365 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
366 die("unable to write inodes");
370 static void write_block(int blk, char *buffer)
372 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
373 die("seek failed in write_block");
374 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
375 die("write failed in write_block");
378 static int get_free_block(void)
382 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
383 die("too many bad blocks");
384 if (used_good_blocks)
385 blk = good_blocks_table[used_good_blocks - 1] + 1;
388 while (blk < ZONES && zone_in_use(blk))
391 die("not enough good blocks");
392 good_blocks_table[used_good_blocks] = blk;
397 static void mark_good_blocks(void)
401 for (blk = 0; blk < used_good_blocks; blk++)
402 mark_zone(good_blocks_table[blk]);
405 inline int next(int zone)
408 zone = FIRSTZONE - 1;
409 while (++zone < ZONES)
410 if (zone_in_use(zone))
415 static void make_bad_inode(void)
417 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
419 int ind = 0, dind = 0;
420 unsigned short ind_block[BLOCK_SIZE >> 1];
421 unsigned short dind_block[BLOCK_SIZE >> 1];
423 #define NEXT_BAD (zone = next(zone))
427 mark_inode(MINIX_BAD_INO);
429 inode->i_time = time(NULL);
430 inode->i_mode = S_IFREG + 0000;
431 inode->i_size = badblocks * BLOCK_SIZE;
433 for (i = 0; i < 7; i++) {
434 inode->i_zone[i] = zone;
438 inode->i_zone[7] = ind = get_free_block();
439 memset(ind_block, 0, BLOCK_SIZE);
440 for (i = 0; i < 512; i++) {
445 inode->i_zone[8] = dind = get_free_block();
446 memset(dind_block, 0, BLOCK_SIZE);
447 for (i = 0; i < 512; i++) {
448 write_block(ind, (char *) ind_block);
449 dind_block[i] = ind = get_free_block();
450 memset(ind_block, 0, BLOCK_SIZE);
451 for (j = 0; j < 512; j++) {
457 die("too many bad blocks");
460 write_block(ind, (char *) ind_block);
462 write_block(dind, (char *) dind_block);
465 #ifdef BB_FEATURE_MINIX2
466 static void make_bad_inode2(void)
468 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
470 int ind = 0, dind = 0;
471 unsigned long ind_block[BLOCK_SIZE >> 2];
472 unsigned long dind_block[BLOCK_SIZE >> 2];
476 mark_inode(MINIX_BAD_INO);
478 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
479 inode->i_mode = S_IFREG + 0000;
480 inode->i_size = badblocks * BLOCK_SIZE;
482 for (i = 0; i < 7; i++) {
483 inode->i_zone[i] = zone;
487 inode->i_zone[7] = ind = get_free_block();
488 memset(ind_block, 0, BLOCK_SIZE);
489 for (i = 0; i < 256; i++) {
494 inode->i_zone[8] = dind = get_free_block();
495 memset(dind_block, 0, BLOCK_SIZE);
496 for (i = 0; i < 256; i++) {
497 write_block(ind, (char *) ind_block);
498 dind_block[i] = ind = get_free_block();
499 memset(ind_block, 0, BLOCK_SIZE);
500 for (j = 0; j < 256; j++) {
506 /* Could make triple indirect block here */
507 die("too many bad blocks");
510 write_block(ind, (char *) ind_block);
512 write_block(dind, (char *) dind_block);
516 static void make_root_inode(void)
518 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
520 mark_inode(MINIX_ROOT_INO);
521 inode->i_zone[0] = get_free_block();
523 inode->i_time = time(NULL);
525 inode->i_size = 3 * dirsize;
527 root_block[2 * dirsize] = '\0';
528 root_block[2 * dirsize + 1] = '\0';
529 inode->i_size = 2 * dirsize;
531 inode->i_mode = S_IFDIR + 0755;
532 inode->i_uid = getuid();
534 inode->i_gid = getgid();
535 write_block(inode->i_zone[0], root_block);
538 #ifdef BB_FEATURE_MINIX2
539 static void make_root_inode2(void)
541 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
543 mark_inode(MINIX_ROOT_INO);
544 inode->i_zone[0] = get_free_block();
546 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
548 inode->i_size = 3 * dirsize;
550 root_block[2 * dirsize] = '\0';
551 root_block[2 * dirsize + 1] = '\0';
552 inode->i_size = 2 * dirsize;
554 inode->i_mode = S_IFDIR + 0755;
555 inode->i_uid = getuid();
557 inode->i_gid = getgid();
558 write_block(inode->i_zone[0], root_block);
562 static void setup_tables(void)
565 unsigned long inodes;
567 memset(super_block_buffer, 0, BLOCK_SIZE);
568 memset(boot_block_buffer, 0, 512);
571 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
573 /* some magic nrs: 1 inode / 3 blocks */
574 if (req_nr_inodes == 0)
577 inodes = req_nr_inodes;
578 /* Round up inode count to fill block size */
579 #ifdef BB_FEATURE_MINIX2
581 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
582 ~(MINIX2_INODES_PER_BLOCK - 1));
585 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
586 ~(MINIX_INODES_PER_BLOCK - 1));
590 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
594 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
595 BITS_PER_BLOCK) && i < 1000) {
597 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
601 /* Real bad hack but overwise mkfs.minix can be thrown
602 * in infinite loop...
604 * dd if=/dev/zero of=test.fs count=10 bs=1024
605 * /sbin/mkfs.minix -i 200 test.fs
608 die("unable to allocate buffers for maps");
610 FIRSTZONE = NORM_FIRSTZONE;
611 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
612 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
613 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
614 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
615 for (i = FIRSTZONE; i < ZONES; i++)
617 for (i = MINIX_ROOT_INO; i <= INODES; i++)
619 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
620 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
621 printf("%ld inodes\n", INODES);
622 printf("%ld blocks\n", ZONES);
623 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
624 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
625 printf("Maxsize=%ld\n\n", MAXSIZE);
629 * Perform a test of a block; return the number of
630 * blocks readable/writeable.
632 long do_check(char *buffer, int try, unsigned int current_block)
636 /* Seek to the correct loc. */
637 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
638 current_block * BLOCK_SIZE) {
639 die("seek failed during testing of blocks");
644 got = read(DEV, buffer, try * BLOCK_SIZE);
647 if (got & (BLOCK_SIZE - 1)) {
648 printf("Weird values in do_check: probably bugs\n");
654 static unsigned int currently_testing = 0;
656 static void alarm_intr(int alnum)
658 if (currently_testing >= ZONES)
660 signal(SIGALRM, alarm_intr);
662 if (!currently_testing)
664 printf("%d ...", currently_testing);
668 static void check_blocks(void)
671 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
673 currently_testing = 0;
674 signal(SIGALRM, alarm_intr);
676 while (currently_testing < ZONES) {
677 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
678 currently_testing * BLOCK_SIZE)
679 die("seek failed in check_blocks");
680 try = TEST_BUFFER_BLOCKS;
681 if (currently_testing + try > ZONES)
682 try = ZONES - currently_testing;
683 got = do_check(buffer, try, currently_testing);
684 currently_testing += got;
687 if (currently_testing < FIRSTZONE)
688 die("bad blocks before data-area: cannot make fs");
689 mark_zone(currently_testing);
694 printf("%d bad blocks\n", badblocks);
695 else if (badblocks == 1)
696 printf("one bad block\n");
699 static void get_list_blocks(filename)
704 unsigned long blockno;
706 listfile = fopen(filename, "r");
707 if (listfile == (FILE *) NULL) {
708 die("can't open file of bad blocks");
710 while (!feof(listfile)) {
711 fscanf(listfile, "%ld\n", &blockno);
716 printf("%d bad blocks\n", badblocks);
717 else if (badblocks == 1)
718 printf("one bad block\n");
721 extern int mkfs_minix_main(int argc, char **argv)
726 char *listfile = NULL;
729 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
730 die("bad inode size");
731 #ifdef BB_FEATURE_MINIX2
732 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
733 die("bad inode size");
738 while (--argc >= 0 && *argv && **argv) {
741 while (i > 0 && *++(*argv) && stopIt==FALSE) {
749 if (*(*argv+1) != 0) {
757 req_nr_inodes = strtoul(cp, &tmp, 0);
767 listfile = *(++argv);
773 if (*(*argv+1) != 0) {
781 i = strtoul(cp, &tmp, 0);
785 magic = MINIX_SUPER_MAGIC;
787 magic = MINIX_SUPER_MAGIC2;
796 #ifdef BB_FEATURE_MINIX2
799 error_msg("%s: not compiled with minix v2 support\n",
812 if (device_name == NULL)
814 else if (BLOCKS == 0)
815 BLOCKS = strtol(*argv, &tmp, 0);
823 if (device_name && !BLOCKS)
824 BLOCKS = get_size(device_name) / 1024;
825 if (!device_name || BLOCKS < 10) {
828 #ifdef BB_FEATURE_MINIX2
831 magic = MINIX2_SUPER_MAGIC;
833 magic = MINIX2_SUPER_MAGIC2;
838 check_mount(); /* is it already mounted? */
841 strcpy(tmp + 2, ".");
844 strcpy(tmp + 2, "..");
847 strcpy(tmp + 2, ".badblocks");
848 DEV = open(device_name, O_RDWR);
850 die("unable to open %s");
851 if (fstat(DEV, &statbuf) < 0)
852 die("unable to stat %s");
853 if (!S_ISBLK(statbuf.st_mode))
855 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
856 die("will not try to make filesystem on '%s'");
861 get_list_blocks(listfile);
862 #ifdef BB_FEATURE_MINIX2