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.
75 #include <sys/ioctl.h>
76 #include <sys/param.h>
80 #define MINIX_ROOT_INO 1
81 #define MINIX_LINK_MAX 250
82 #define MINIX2_LINK_MAX 65530
84 #define MINIX_I_MAP_SLOTS 8
85 #define MINIX_Z_MAP_SLOTS 64
86 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
87 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
88 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
89 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
90 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
91 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
93 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
94 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
96 #define MINIX_V1 0x0001 /* original minix fs */
97 #define MINIX_V2 0x0002 /* minix V2 fs */
99 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
102 * This is the original minix inode layout on disk.
103 * Note the 8-bit gid and atime and ctime.
116 * The new minix inode has all the time entries, as well as
117 * long block numbers and a third indirect block (7+1+1+1
118 * instead of 7+1+1). Also, some previously 8-bit values are
119 * now 16-bit. The inode is now 64 bytes instead of 32.
121 struct minix2_inode {
134 * minix super-block data on disk
136 struct minix_super_block {
139 uint16_t s_imap_blocks;
140 uint16_t s_zmap_blocks;
141 uint16_t s_firstdatazone;
142 uint16_t s_log_zone_size;
149 struct minix_dir_entry {
154 #define BLOCK_SIZE_BITS 10
155 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
157 #define NAME_MAX 255 /* # chars in a file name */
159 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
161 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
162 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
164 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
165 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
168 #define BLKGETSIZE _IO(0x12,96) /* return device size */
176 #define MINIX_ROOT_INO 1
177 #define MINIX_BAD_INO 2
179 #define TEST_BUFFER_BLOCKS 16
180 #define MAX_GOOD_BLOCKS 512
182 #define UPPER(size,n) (((size)+((n)-1))/(n))
183 #define INODE_SIZE (sizeof(struct minix_inode))
184 #ifdef CONFIG_FEATURE_MINIX2
185 #define INODE_SIZE2 (sizeof(struct minix2_inode))
186 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
187 : MINIX_INODES_PER_BLOCK))
189 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
191 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
193 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
195 static char *device_name;
197 static uint32_t BLOCKS;
199 static int badblocks;
200 static int namelen = 30; /* default (changed to 30, per Linus's
202 suggestion, Sun Nov 21 08:05:07 1993) */
203 static int dirsize = 32;
204 static int magic = MINIX_SUPER_MAGIC2;
207 static char root_block[BLOCK_SIZE];
209 static char *inode_buffer;
211 #define Inode (((struct minix_inode *) inode_buffer)-1)
212 #ifdef CONFIG_FEATURE_MINIX2
213 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
215 static char super_block_buffer[BLOCK_SIZE];
216 static char boot_block_buffer[512];
218 #define Super (*(struct minix_super_block *)super_block_buffer)
219 #define INODES (Super.s_ninodes)
220 #ifdef CONFIG_FEATURE_MINIX2
221 #define ZONES (version2 ? Super.s_zones : Super.s_nzones)
223 #define ZONES (Super.s_nzones)
225 #define IMAPS (Super.s_imap_blocks)
226 #define ZMAPS (Super.s_zmap_blocks)
227 #define FIRSTZONE (Super.s_firstdatazone)
228 #define ZONESIZE (Super.s_log_zone_size)
229 #define MAXSIZE (Super.s_max_size)
230 #define MAGIC (Super.s_magic)
231 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
233 static char *inode_map;
234 static char *zone_map;
236 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
237 static int used_good_blocks;
238 static unsigned long req_nr_inodes;
240 static inline int bit(char * a,unsigned int i)
242 return (a[i >> 3] & (1<<(i & 7))) != 0;
244 #define inode_in_use(x) (bit(inode_map,(x)))
245 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
247 #define mark_inode(x) (setbit(inode_map,(x)))
248 #define unmark_inode(x) (clrbit(inode_map,(x)))
250 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
251 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
254 * Check to make certain that our new filesystem won't be created on
255 * an already mounted partition. Code adapted from mke2fs, Copyright
256 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
258 static inline void check_mount(void)
263 if ((f = setmntent(MOUNTED, "r")) == NULL)
265 while ((mnt = getmntent(f)) != NULL)
266 if (strcmp(device_name, mnt->mnt_fsname) == 0)
272 bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
275 static long valid_offset(int fd, int offset)
279 if (lseek(fd, offset, 0) < 0)
281 if (read(fd, &ch, 1) < 1)
286 static inline int count_blocks(int fd)
291 for (high = 1; valid_offset(fd, high); high *= 2)
293 while (low < high - 1) {
294 const int mid = (low + high) / 2;
296 if (valid_offset(fd, mid))
305 static inline int get_size(const char *file)
310 fd = bb_xopen(file, O_RDWR);
311 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
316 size = count_blocks(fd);
321 static inline void write_tables(void)
323 /* Mark the super block valid. */
324 Super.s_state |= MINIX_VALID_FS;
325 Super.s_state &= ~MINIX_ERROR_FS;
327 if (lseek(DEV, 0, SEEK_SET))
328 bb_error_msg_and_die("seek to boot block failed in write_tables");
329 if (512 != write(DEV, boot_block_buffer, 512))
330 bb_error_msg_and_die("unable to clear boot sector");
331 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
332 bb_error_msg_and_die("seek failed in write_tables");
333 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
334 bb_error_msg_and_die("unable to write super-block");
335 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
336 bb_error_msg_and_die("unable to write inode map");
337 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
338 bb_error_msg_and_die("unable to write zone map");
339 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
340 bb_error_msg_and_die("unable to write inodes");
344 static void write_block(int blk, char *buffer)
346 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
347 bb_error_msg_and_die("seek failed in write_block");
348 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
349 bb_error_msg_and_die("write failed in write_block");
352 static int get_free_block(void)
356 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
357 bb_error_msg_and_die("too many bad blocks");
358 if (used_good_blocks)
359 blk = good_blocks_table[used_good_blocks - 1] + 1;
362 while (blk < ZONES && zone_in_use(blk))
365 bb_error_msg_and_die("not enough good blocks");
366 good_blocks_table[used_good_blocks] = blk;
371 static inline void mark_good_blocks(void)
375 for (blk = 0; blk < used_good_blocks; blk++)
376 mark_zone(good_blocks_table[blk]);
379 static int next(int zone)
382 zone = FIRSTZONE - 1;
383 while (++zone < ZONES)
384 if (zone_in_use(zone))
389 static inline void make_bad_inode(void)
391 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
393 int ind = 0, dind = 0;
394 unsigned short ind_block[BLOCK_SIZE >> 1];
395 unsigned short dind_block[BLOCK_SIZE >> 1];
397 #define NEXT_BAD (zone = next(zone))
401 mark_inode(MINIX_BAD_INO);
403 inode->i_time = time(NULL);
404 inode->i_mode = S_IFREG + 0000;
405 inode->i_size = badblocks * BLOCK_SIZE;
407 for (i = 0; i < 7; i++) {
408 inode->i_zone[i] = zone;
412 inode->i_zone[7] = ind = get_free_block();
413 memset(ind_block, 0, BLOCK_SIZE);
414 for (i = 0; i < 512; i++) {
419 inode->i_zone[8] = dind = get_free_block();
420 memset(dind_block, 0, BLOCK_SIZE);
421 for (i = 0; i < 512; i++) {
422 write_block(ind, (char *) ind_block);
423 dind_block[i] = ind = get_free_block();
424 memset(ind_block, 0, BLOCK_SIZE);
425 for (j = 0; j < 512; j++) {
431 bb_error_msg_and_die("too many bad blocks");
434 write_block(ind, (char *) ind_block);
436 write_block(dind, (char *) dind_block);
439 #ifdef CONFIG_FEATURE_MINIX2
440 static inline void make_bad_inode2(void)
442 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
444 int ind = 0, dind = 0;
445 unsigned long ind_block[BLOCK_SIZE >> 2];
446 unsigned long dind_block[BLOCK_SIZE >> 2];
450 mark_inode(MINIX_BAD_INO);
452 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
453 inode->i_mode = S_IFREG + 0000;
454 inode->i_size = badblocks * BLOCK_SIZE;
456 for (i = 0; i < 7; i++) {
457 inode->i_zone[i] = zone;
461 inode->i_zone[7] = ind = get_free_block();
462 memset(ind_block, 0, BLOCK_SIZE);
463 for (i = 0; i < 256; i++) {
468 inode->i_zone[8] = dind = get_free_block();
469 memset(dind_block, 0, BLOCK_SIZE);
470 for (i = 0; i < 256; i++) {
471 write_block(ind, (char *) ind_block);
472 dind_block[i] = ind = get_free_block();
473 memset(ind_block, 0, BLOCK_SIZE);
474 for (j = 0; j < 256; j++) {
480 /* Could make triple indirect block here */
481 bb_error_msg_and_die("too many bad blocks");
484 write_block(ind, (char *) ind_block);
486 write_block(dind, (char *) dind_block);
490 static inline void make_root_inode(void)
492 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
494 mark_inode(MINIX_ROOT_INO);
495 inode->i_zone[0] = get_free_block();
497 inode->i_time = time(NULL);
499 inode->i_size = 3 * dirsize;
501 root_block[2 * dirsize] = '\0';
502 root_block[2 * dirsize + 1] = '\0';
503 inode->i_size = 2 * dirsize;
505 inode->i_mode = S_IFDIR + 0755;
506 inode->i_uid = getuid();
508 inode->i_gid = getgid();
509 write_block(inode->i_zone[0], root_block);
512 #ifdef CONFIG_FEATURE_MINIX2
513 static inline void make_root_inode2(void)
515 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
517 mark_inode(MINIX_ROOT_INO);
518 inode->i_zone[0] = get_free_block();
520 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
522 inode->i_size = 3 * dirsize;
524 root_block[2 * dirsize] = '\0';
525 root_block[2 * dirsize + 1] = '\0';
526 inode->i_size = 2 * dirsize;
528 inode->i_mode = S_IFDIR + 0755;
529 inode->i_uid = getuid();
531 inode->i_gid = getgid();
532 write_block(inode->i_zone[0], root_block);
536 static inline void setup_tables(void)
539 unsigned long inodes;
541 memset(super_block_buffer, 0, BLOCK_SIZE);
542 memset(boot_block_buffer, 0, 512);
545 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
546 #ifdef CONFIG_FEATURE_MINIX2
548 Super.s_zones = BLOCKS;
551 Super.s_nzones = BLOCKS;
553 /* some magic nrs: 1 inode / 3 blocks */
554 if (req_nr_inodes == 0)
557 inodes = req_nr_inodes;
558 /* Round up inode count to fill block size */
559 #ifdef CONFIG_FEATURE_MINIX2
561 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
562 ~(MINIX2_INODES_PER_BLOCK - 1));
565 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
566 ~(MINIX_INODES_PER_BLOCK - 1));
570 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
574 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
575 BITS_PER_BLOCK) && i < 1000) {
577 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
581 /* Real bad hack but overwise mkfs.minix can be thrown
582 * in infinite loop...
584 * dd if=/dev/zero of=test.fs count=10 bs=1024
585 * /sbin/mkfs.minix -i 200 test.fs
588 bb_error_msg_and_die("unable to allocate buffers for maps");
590 FIRSTZONE = NORM_FIRSTZONE;
591 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
592 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
593 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
594 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
595 for (i = FIRSTZONE; i < ZONES; i++)
597 for (i = MINIX_ROOT_INO; i <= INODES; i++)
599 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
600 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
601 printf("%ld inodes\n", (long)INODES);
602 printf("%ld blocks\n", (long)ZONES);
603 printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE);
604 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
605 printf("Maxsize=%ld\n\n", (long)MAXSIZE);
609 * Perform a test of a block; return the number of
610 * blocks readable/writable.
612 static inline long do_check(char *buffer, int try, unsigned int current_block)
616 /* Seek to the correct loc. */
617 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
618 current_block * BLOCK_SIZE) {
619 bb_error_msg_and_die("seek failed during testing of blocks");
624 got = read(DEV, buffer, try * BLOCK_SIZE);
627 if (got & (BLOCK_SIZE - 1)) {
628 printf("Weird values in do_check: probably bugs\n");
634 static unsigned int currently_testing;
636 static void alarm_intr(int alnum)
638 if (currently_testing >= ZONES)
640 signal(SIGALRM, alarm_intr);
642 if (!currently_testing)
644 printf("%d ...", currently_testing);
648 static void check_blocks(void)
651 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
653 currently_testing = 0;
654 signal(SIGALRM, alarm_intr);
656 while (currently_testing < ZONES) {
657 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
658 currently_testing * BLOCK_SIZE)
659 bb_error_msg_and_die("seek failed in check_blocks");
660 try = TEST_BUFFER_BLOCKS;
661 if (currently_testing + try > ZONES)
662 try = ZONES - currently_testing;
663 got = do_check(buffer, try, currently_testing);
664 currently_testing += got;
667 if (currently_testing < FIRSTZONE)
668 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
669 mark_zone(currently_testing);
674 printf("%d bad blocks\n", badblocks);
675 else if (badblocks == 1)
676 printf("one bad block\n");
679 static void get_list_blocks(char *filename)
682 unsigned long blockno;
684 listfile = bb_xfopen(filename, "r");
685 while (!feof(listfile)) {
686 fscanf(listfile, "%ld\n", &blockno);
691 printf("%d bad blocks\n", badblocks);
692 else if (badblocks == 1)
693 printf("one bad block\n");
696 int mkfs_minix_main(int argc, char **argv)
701 char *listfile = NULL;
704 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
705 bb_error_msg_and_die("bad inode size");
706 #ifdef CONFIG_FEATURE_MINIX2
707 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
708 bb_error_msg_and_die("bad inode size");
713 while (--argc >= 0 && *argv && **argv) {
716 while (i > 0 && *++(*argv) && stopIt==FALSE) {
724 if (*(*argv+1) != 0) {
732 req_nr_inodes = strtoul(cp, &tmp, 0);
742 listfile = *(++argv);
748 if (*(*argv+1) != 0) {
756 i = strtoul(cp, &tmp, 0);
760 magic = MINIX_SUPER_MAGIC;
762 magic = MINIX_SUPER_MAGIC2;
771 #ifdef CONFIG_FEATURE_MINIX2
774 bb_error_msg("%s: not compiled with minix v2 support",
787 if (device_name == NULL)
789 else if (BLOCKS == 0)
790 BLOCKS = strtol(*argv, &tmp, 0);
798 if (device_name && !BLOCKS)
799 BLOCKS = get_size(device_name) / 1024;
800 if (!device_name || BLOCKS < 10) {
803 #ifdef CONFIG_FEATURE_MINIX2
806 magic = MINIX2_SUPER_MAGIC;
808 magic = MINIX2_SUPER_MAGIC2;
813 check_mount(); /* is it already mounted? */
816 strcpy(tmp + 2, ".");
819 strcpy(tmp + 2, "..");
822 strcpy(tmp + 2, ".badblocks");
823 DEV = bb_xopen(device_name, O_RDWR);
824 if (fstat(DEV, &statbuf) < 0)
825 bb_error_msg_and_die("unable to stat %s", device_name);
826 if (!S_ISBLK(statbuf.st_mode))
828 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
829 bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name);
834 get_list_blocks(listfile);
835 #ifdef CONFIG_FEATURE_MINIX2