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 readability 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.
68 #define MINIX_ROOT_INO 1
69 #define MINIX_LINK_MAX 250
70 #define MINIX2_LINK_MAX 65530
72 #define MINIX_I_MAP_SLOTS 8
73 #define MINIX_Z_MAP_SLOTS 64
74 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
75 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
76 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
77 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
78 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
79 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
81 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
82 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
84 #define MINIX_V1 0x0001 /* original minix fs */
85 #define MINIX_V2 0x0002 /* minix V2 fs */
87 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
90 * This is the original minix inode layout on disk.
91 * Note the 8-bit gid and atime and ctime.
104 * The new minix inode has all the time entries, as well as
105 * long block numbers and a third indirect block (7+1+1+1
106 * instead of 7+1+1). Also, some previously 8-bit values are
107 * now 16-bit. The inode is now 64 bytes instead of 32.
109 struct minix2_inode {
122 * minix super-block data on disk
124 struct minix_super_block {
127 uint16_t s_imap_blocks;
128 uint16_t s_zmap_blocks;
129 uint16_t s_firstdatazone;
130 uint16_t s_log_zone_size;
137 struct minix_dir_entry {
142 #define NAME_MAX 255 /* # chars in a file name */
144 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
146 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
147 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
149 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
150 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
153 #define BLKGETSIZE _IO(0x12,96) /* return device size */
161 #define MINIX_ROOT_INO 1
162 #define MINIX_BAD_INO 2
164 #define TEST_BUFFER_BLOCKS 16
165 #define MAX_GOOD_BLOCKS 512
167 #define UPPER(size,n) (((size)+((n)-1))/(n))
168 #define INODE_SIZE (sizeof(struct minix_inode))
169 #ifdef CONFIG_FEATURE_MINIX2
170 #define INODE_SIZE2 (sizeof(struct minix2_inode))
171 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
172 : MINIX_INODES_PER_BLOCK))
174 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
176 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
178 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
180 static char *device_name;
182 static uint32_t BLOCKS;
184 static int badblocks;
185 static int namelen = 30; /* default (changed to 30, per Linus's
187 suggestion, Sun Nov 21 08:05:07 1993) */
188 static int dirsize = 32;
189 static int magic = MINIX_SUPER_MAGIC2;
192 static char root_block[BLOCK_SIZE];
194 static char *inode_buffer;
196 #define Inode (((struct minix_inode *) inode_buffer)-1)
197 #ifdef CONFIG_FEATURE_MINIX2
198 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
200 static char super_block_buffer[BLOCK_SIZE];
201 static char boot_block_buffer[512];
203 #define Super (*(struct minix_super_block *)super_block_buffer)
204 #define INODES (Super.s_ninodes)
205 #ifdef CONFIG_FEATURE_MINIX2
206 #define ZONES (version2 ? Super.s_zones : Super.s_nzones)
208 #define ZONES (Super.s_nzones)
210 #define IMAPS (Super.s_imap_blocks)
211 #define ZMAPS (Super.s_zmap_blocks)
212 #define FIRSTZONE (Super.s_firstdatazone)
213 #define ZONESIZE (Super.s_log_zone_size)
214 #define MAXSIZE (Super.s_max_size)
215 #define MAGIC (Super.s_magic)
216 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
218 static char *inode_map;
219 static char *zone_map;
221 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
222 static int used_good_blocks;
223 static unsigned long req_nr_inodes;
225 static int bit(char * a,unsigned int i)
227 return (a[i >> 3] & (1<<(i & 7))) != 0;
229 #define inode_in_use(x) (bit(inode_map,(x)))
230 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
232 #define mark_inode(x) (setbit(inode_map,(x)))
233 #define unmark_inode(x) (clrbit(inode_map,(x)))
235 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
236 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
239 * Check to make certain that our new filesystem won't be created on
240 * an already mounted partition. Code adapted from mke2fs, Copyright
241 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
243 static void check_mount(void)
248 if ((f = setmntent(MOUNTED, "r")) == NULL)
250 while ((mnt = getmntent(f)) != NULL)
251 if (strcmp(device_name, mnt->mnt_fsname) == 0)
257 bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
260 static long valid_offset(int fd, int offset)
264 if (lseek(fd, offset, 0) < 0)
266 if (read(fd, &ch, 1) < 1)
271 static int count_blocks(int fd)
276 for (high = 1; valid_offset(fd, high); high *= 2)
278 while (low < high - 1) {
279 const int mid = (low + high) / 2;
281 if (valid_offset(fd, mid))
290 static int get_size(const char *file)
295 fd = xopen(file, O_RDWR);
296 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
301 size = count_blocks(fd);
306 static void write_tables(void)
308 /* Mark the super block valid. */
309 Super.s_state |= MINIX_VALID_FS;
310 Super.s_state &= ~MINIX_ERROR_FS;
312 if (lseek(DEV, 0, SEEK_SET))
313 bb_error_msg_and_die("seek to boot block failed in write_tables");
314 if (512 != write(DEV, boot_block_buffer, 512))
315 bb_error_msg_and_die("cannot clear boot sector");
316 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
317 bb_error_msg_and_die("seek failed in write_tables");
318 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
319 bb_error_msg_and_die("cannot write super-block");
320 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
321 bb_error_msg_and_die("cannot write inode map");
322 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
323 bb_error_msg_and_die("cannot write zone map");
324 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
325 bb_error_msg_and_die("cannot write inodes");
329 static void write_block(int blk, char *buffer)
331 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
332 bb_error_msg_and_die("seek failed in write_block");
333 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
334 bb_error_msg_and_die("write failed in write_block");
337 static int get_free_block(void)
341 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
342 bb_error_msg_and_die("too many bad blocks");
343 if (used_good_blocks)
344 blk = good_blocks_table[used_good_blocks - 1] + 1;
347 while (blk < ZONES && zone_in_use(blk))
350 bb_error_msg_and_die("not enough good blocks");
351 good_blocks_table[used_good_blocks] = blk;
356 static void mark_good_blocks(void)
360 for (blk = 0; blk < used_good_blocks; blk++)
361 mark_zone(good_blocks_table[blk]);
364 static int next(int zone)
367 zone = FIRSTZONE - 1;
368 while (++zone < ZONES)
369 if (zone_in_use(zone))
374 static void make_bad_inode(void)
376 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
378 int ind = 0, dind = 0;
379 unsigned short ind_block[BLOCK_SIZE >> 1];
380 unsigned short dind_block[BLOCK_SIZE >> 1];
382 #define NEXT_BAD (zone = next(zone))
386 mark_inode(MINIX_BAD_INO);
388 inode->i_time = time(NULL);
389 inode->i_mode = S_IFREG + 0000;
390 inode->i_size = badblocks * BLOCK_SIZE;
392 for (i = 0; i < 7; i++) {
393 inode->i_zone[i] = zone;
397 inode->i_zone[7] = ind = get_free_block();
398 memset(ind_block, 0, BLOCK_SIZE);
399 for (i = 0; i < 512; i++) {
404 inode->i_zone[8] = dind = get_free_block();
405 memset(dind_block, 0, BLOCK_SIZE);
406 for (i = 0; i < 512; i++) {
407 write_block(ind, (char *) ind_block);
408 dind_block[i] = ind = get_free_block();
409 memset(ind_block, 0, BLOCK_SIZE);
410 for (j = 0; j < 512; j++) {
416 bb_error_msg_and_die("too many bad blocks");
419 write_block(ind, (char *) ind_block);
421 write_block(dind, (char *) dind_block);
424 #ifdef CONFIG_FEATURE_MINIX2
425 static void make_bad_inode2(void)
427 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
429 int ind = 0, dind = 0;
430 unsigned long ind_block[BLOCK_SIZE >> 2];
431 unsigned long dind_block[BLOCK_SIZE >> 2];
435 mark_inode(MINIX_BAD_INO);
437 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
438 inode->i_mode = S_IFREG + 0000;
439 inode->i_size = badblocks * BLOCK_SIZE;
441 for (i = 0; i < 7; i++) {
442 inode->i_zone[i] = zone;
446 inode->i_zone[7] = ind = get_free_block();
447 memset(ind_block, 0, BLOCK_SIZE);
448 for (i = 0; i < 256; i++) {
453 inode->i_zone[8] = dind = get_free_block();
454 memset(dind_block, 0, BLOCK_SIZE);
455 for (i = 0; i < 256; i++) {
456 write_block(ind, (char *) ind_block);
457 dind_block[i] = ind = get_free_block();
458 memset(ind_block, 0, BLOCK_SIZE);
459 for (j = 0; j < 256; j++) {
465 /* Could make triple indirect block here */
466 bb_error_msg_and_die("too many bad blocks");
469 write_block(ind, (char *) ind_block);
471 write_block(dind, (char *) dind_block);
475 static void make_root_inode(void)
477 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
479 mark_inode(MINIX_ROOT_INO);
480 inode->i_zone[0] = get_free_block();
482 inode->i_time = time(NULL);
484 inode->i_size = 3 * dirsize;
486 root_block[2 * dirsize] = '\0';
487 root_block[2 * dirsize + 1] = '\0';
488 inode->i_size = 2 * dirsize;
490 inode->i_mode = S_IFDIR + 0755;
491 inode->i_uid = getuid();
493 inode->i_gid = getgid();
494 write_block(inode->i_zone[0], root_block);
497 #ifdef CONFIG_FEATURE_MINIX2
498 static void make_root_inode2(void)
500 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
502 mark_inode(MINIX_ROOT_INO);
503 inode->i_zone[0] = get_free_block();
505 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
507 inode->i_size = 3 * dirsize;
509 root_block[2 * dirsize] = '\0';
510 root_block[2 * dirsize + 1] = '\0';
511 inode->i_size = 2 * dirsize;
513 inode->i_mode = S_IFDIR + 0755;
514 inode->i_uid = getuid();
516 inode->i_gid = getgid();
517 write_block(inode->i_zone[0], root_block);
521 static void setup_tables(void)
524 unsigned long inodes;
526 memset(super_block_buffer, 0, BLOCK_SIZE);
527 memset(boot_block_buffer, 0, 512);
530 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
531 #ifdef CONFIG_FEATURE_MINIX2
533 Super.s_zones = BLOCKS;
536 Super.s_nzones = BLOCKS;
538 /* some magic nrs: 1 inode / 3 blocks */
539 if (req_nr_inodes == 0)
542 inodes = req_nr_inodes;
543 /* Round up inode count to fill block size */
544 #ifdef CONFIG_FEATURE_MINIX2
546 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
547 ~(MINIX2_INODES_PER_BLOCK - 1));
550 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
551 ~(MINIX_INODES_PER_BLOCK - 1));
555 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
559 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
560 BITS_PER_BLOCK) && i < 1000) {
562 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
566 /* Real bad hack but overwise mkfs.minix can be thrown
567 * in infinite loop...
569 * dd if=/dev/zero of=test.fs count=10 bs=1024
570 * /sbin/mkfs.minix -i 200 test.fs
573 bb_error_msg_and_die("cannot allocate buffers for maps");
575 FIRSTZONE = NORM_FIRSTZONE;
576 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
577 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
578 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
579 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
580 for (i = FIRSTZONE; i < ZONES; i++)
582 for (i = MINIX_ROOT_INO; i <= INODES; i++)
584 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
585 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
586 printf("%ld inodes\n", (long)INODES);
587 printf("%ld blocks\n", (long)ZONES);
588 printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE);
589 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
590 printf("Maxsize=%ld\n\n", (long)MAXSIZE);
594 * Perform a test of a block; return the number of
595 * blocks readable/writable.
597 static long do_check(char *buffer, int try, unsigned int current_block)
601 /* Seek to the correct loc. */
602 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
603 current_block * BLOCK_SIZE) {
604 bb_error_msg_and_die("seek failed during testing of blocks");
609 got = read(DEV, buffer, try * BLOCK_SIZE);
612 if (got & (BLOCK_SIZE - 1)) {
613 printf("Weird values in do_check: probably bugs\n");
619 static unsigned int currently_testing;
621 static void alarm_intr(int alnum)
623 if (currently_testing >= ZONES)
625 signal(SIGALRM, alarm_intr);
627 if (!currently_testing)
629 printf("%d ...", currently_testing);
633 static void check_blocks(void)
636 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
638 currently_testing = 0;
639 signal(SIGALRM, alarm_intr);
641 while (currently_testing < ZONES) {
642 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
643 currently_testing * BLOCK_SIZE)
644 bb_error_msg_and_die("seek failed in check_blocks");
645 try = TEST_BUFFER_BLOCKS;
646 if (currently_testing + try > ZONES)
647 try = ZONES - currently_testing;
648 got = do_check(buffer, try, currently_testing);
649 currently_testing += got;
652 if (currently_testing < FIRSTZONE)
653 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
654 mark_zone(currently_testing);
659 printf("%d bad blocks\n", badblocks);
660 else if (badblocks == 1)
661 printf("one bad block\n");
664 static void get_list_blocks(char *filename)
667 unsigned long blockno;
669 listfile = xfopen(filename, "r");
670 while (!feof(listfile)) {
671 fscanf(listfile, "%ld\n", &blockno);
676 printf("%d bad blocks\n", badblocks);
677 else if (badblocks == 1)
678 printf("one bad block\n");
681 int mkfs_minix_main(int argc, char **argv)
686 char *listfile = NULL;
689 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
690 bb_error_msg_and_die("bad inode size");
691 #ifdef CONFIG_FEATURE_MINIX2
692 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
693 bb_error_msg_and_die("bad inode size");
698 while (--argc >= 0 && *argv && **argv) {
701 while (i > 0 && *++(*argv) && stopIt==FALSE) {
709 if (*(*argv+1) != 0) {
717 req_nr_inodes = strtoul(cp, &tmp, 0);
727 listfile = *(++argv);
733 if (*(*argv+1) != 0) {
741 i = strtoul(cp, &tmp, 0);
745 magic = MINIX_SUPER_MAGIC;
747 magic = MINIX_SUPER_MAGIC2;
756 #ifdef CONFIG_FEATURE_MINIX2
759 bb_error_msg("%s: not compiled with minix v2 support",
772 if (device_name == NULL)
774 else if (BLOCKS == 0)
775 BLOCKS = strtol(*argv, &tmp, 0);
783 if (device_name && !BLOCKS)
784 BLOCKS = get_size(device_name) / 1024;
785 if (!device_name || BLOCKS < 10) {
788 #ifdef CONFIG_FEATURE_MINIX2
791 magic = MINIX2_SUPER_MAGIC;
793 magic = MINIX2_SUPER_MAGIC2;
798 check_mount(); /* is it already mounted? */
801 strcpy(tmp + 2, ".");
804 strcpy(tmp + 2, "..");
807 strcpy(tmp + 2, ".badblocks");
808 DEV = xopen(device_name, O_RDWR);
809 if (fstat(DEV, &statbuf) < 0)
810 bb_error_msg_and_die("cannot stat %s", device_name);
811 if (!S_ISBLK(statbuf.st_mode))
813 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
814 bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name);
819 get_list_blocks(listfile);
820 #ifdef CONFIG_FEATURE_MINIX2