1 /* vi: set sw=4 ts=4: */
3 * mkfs.c - make a linux (minix) file-system.
5 * (C) 1991 Linus Torvalds.
7 * Licensed under GPLv2, see file LICENSE in this source tree.
13 * 24.11.91 - Time began. Used the fsck sources to get started.
15 * 25.11.91 - Corrected some bugs. Added support for ".badblocks"
16 * The algorithm for ".badblocks" is a bit weird, but
17 * it should work. Oh, well.
19 * 25.01.92 - Added the -l option for getting the list of bad blocks
20 * out of a named file. (Dave Rivers, rivers@ponds.uucp)
22 * 28.02.92 - Added %-information when using -c.
24 * 28.02.93 - Added support for other namelengths than the original
25 * 14 characters so that I can test the new kernel routines..
27 * 09.10.93 - Make exit status conform to that required by fsutil
28 * (Rik Faith, faith@cs.unc.edu)
30 * 31.10.93 - Added inode request feature, for backup floppies: use
31 * 32 inodes, for a news partition use more.
32 * (Scott Heavner, sdh@po.cwru.edu)
34 * 03.01.94 - Added support for file system valid flag.
35 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
37 * 30.10.94 - added support for v2 filesystem
38 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
40 * 09.11.94 - Added test to prevent overwrite of mounted fs adapted
41 * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
42 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
44 * 03.20.95 - Clear first 512 bytes of filesystem to make certain that
45 * the filesystem is not misidentified as a MS-DOS FAT filesystem.
46 * (Daniel Quinlan, quinlan@yggdrasil.com)
48 * 02.07.96 - Added small patch from Russell King to make the program a
49 * good deal more portable (janl@math.uio.no)
51 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
53 * -c for readability checking (SLOW!)
54 * -l for getting a list of bad blocks from a file.
55 * -n for namelength (currently the kernel only uses 14 or 30)
56 * -i for number of inodes
57 * -v for v2 filesystem
59 * The device may be a block device or a image of one, but this isn't
60 * enforced (but it's not much fun on a character device :-).
62 * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
63 * removed getopt based parser and added a hand rolled one.
65 //config:config MKFS_MINIX
66 //config: bool "mkfs_minix"
68 //config: select PLATFORM_LINUX
70 //config: The minix filesystem is a nice, small, compact, read-write filesystem
71 //config: with little overhead. If you wish to be able to create minix
72 //config: filesystems this utility will do the job for you.
74 //config:config FEATURE_MINIX2
75 //config: bool "Support Minix fs v2 (fsck_minix/mkfs_minix)"
77 //config: depends on FSCK_MINIX || MKFS_MINIX
79 //config: If you wish to be able to create version 2 minix filesystems, enable
80 //config: this. If you enabled 'mkfs_minix' then you almost certainly want to
81 //config: be using the version 2 filesystem support.
83 // APPLET_ODDNAME:name main location suid_type help
84 //applet:IF_MKFS_MINIX(APPLET_ODDNAME(mkfs.minix, mkfs_minix, BB_DIR_SBIN, BB_SUID_DROP, mkfs_minix))
86 //kbuild:lib-$(CONFIG_MKFS_MINIX) += mkfs_minix.o
88 //usage:#define mkfs_minix_trivial_usage
89 //usage: "[-c | -l FILE] [-nXX] [-iXX] BLOCKDEV [KBYTES]"
90 //usage:#define mkfs_minix_full_usage "\n\n"
91 //usage: "Make a MINIX filesystem\n"
92 //usage: "\n -c Check device for bad blocks"
93 //usage: "\n -n [14|30] Maximum length of filenames"
94 //usage: "\n -i INODES Number of inodes for the filesystem"
95 //usage: "\n -l FILE Read bad blocks list from FILE"
96 //usage: "\n -v Make version 2 filesystem"
103 /* Store the very same times/uids/gids for image consistency */
109 /* Was using this. Is it useful? NB: this will break testsuite */
110 # define CUR_TIME time(NULL)
111 # define GETUID getuid()
112 # define GETGID getgid()
116 MAX_GOOD_BLOCKS = 512,
117 TEST_BUFFER_BLOCKS = 16,
120 #if !ENABLE_FEATURE_MINIX2
121 enum { version2 = 0 };
127 #if ENABLE_FEATURE_MINIX2
129 #define version2 G.version2
132 uint32_t total_blocks;
140 int used_good_blocks;
141 unsigned long req_nr_inodes;
142 unsigned currently_testing;
144 char root_block[BLOCK_SIZE];
146 char superblock_buffer[BLOCK_SIZE];
147 struct minix_superblock SB;
149 char boot_block_buffer[512];
150 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
151 /* check_blocks(): buffer[] was the biggest static in entire bbox */
152 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
154 unsigned short ind_block1[BLOCK_SIZE >> 1];
155 unsigned short dind_block1[BLOCK_SIZE >> 1];
156 unsigned long ind_block2[BLOCK_SIZE >> 2];
157 unsigned long dind_block2[BLOCK_SIZE >> 2];
159 #define G (*ptr_to_globals)
160 #define INIT_G() do { \
161 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
164 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
166 return (size + n-1) / n;
169 #define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
170 #define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
174 #define SB_INODES (SB.s_ninodes)
175 #define SB_IMAPS (SB.s_imap_blocks)
176 #define SB_ZMAPS (SB.s_zmap_blocks)
177 #define SB_FIRSTZONE (SB.s_firstdatazone)
178 #define SB_ZONE_SIZE (SB.s_log_zone_size)
179 #define SB_MAXSIZE (SB.s_max_size)
180 #define SB_MAGIC (SB.s_magic)
182 #if !ENABLE_FEATURE_MINIX2
183 # define SB_ZONES (SB.s_nzones)
184 # define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
186 # define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
187 # define INODE_BLOCKS div_roundup(SB_INODES, \
188 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
191 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
192 #define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
194 /* Before you ask "where they come from?": */
195 /* setbit/clrbit are supplied by sys/param.h */
197 static int minix_bit(const char* a, unsigned i)
199 return a[i >> 3] & (1<<(i & 7));
202 static void minix_setbit(char *a, unsigned i)
206 static void minix_clrbit(char *a, unsigned i)
211 /* Note: do not assume 0/1, it is 0/nonzero */
212 #define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
213 /*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
215 #define mark_inode(x) minix_setbit(G.inode_map,(x))
216 #define unmark_inode(x) minix_clrbit(G.inode_map,(x))
217 #define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
218 #define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
221 # define BLKGETSIZE _IO(0x12,96) /* return device size */
224 static void write_tables(void)
226 /* Mark the superblock valid. */
227 SB.s_state |= MINIX_VALID_FS;
228 SB.s_state &= ~MINIX_ERROR_FS;
230 msg_eol = "seek to 0 failed";
231 xlseek(dev_fd, 0, SEEK_SET);
233 msg_eol = "can't clear boot sector";
234 xwrite(dev_fd, G.boot_block_buffer, 512);
236 msg_eol = "seek to BLOCK_SIZE failed";
237 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
239 msg_eol = "can't write superblock";
240 xwrite(dev_fd, G.u.superblock_buffer, BLOCK_SIZE);
242 msg_eol = "can't write inode map";
243 xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
245 msg_eol = "can't write zone map";
246 xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
248 msg_eol = "can't write inodes";
249 xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
254 static void write_block(int blk, char *buffer)
256 xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
257 xwrite(dev_fd, buffer, BLOCK_SIZE);
260 static int get_free_block(void)
264 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
265 bb_error_msg_and_die("too many bad blocks");
266 if (G.used_good_blocks)
267 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
270 while (blk < SB_ZONES && zone_in_use(blk))
273 bb_error_msg_and_die("not enough good blocks");
274 G.good_blocks_table[G.used_good_blocks] = blk;
275 G.used_good_blocks++;
279 static void mark_good_blocks(void)
283 for (blk = 0; blk < G.used_good_blocks; blk++)
284 mark_zone(G.good_blocks_table[blk]);
287 static int next(int zone)
290 zone = SB_FIRSTZONE - 1;
291 while (++zone < SB_ZONES)
292 if (zone_in_use(zone))
297 static void make_bad_inode(void)
299 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
301 int ind = 0, dind = 0;
302 /* moved to globals to reduce stack usage
303 unsigned short ind_block[BLOCK_SIZE >> 1];
304 unsigned short dind_block[BLOCK_SIZE >> 1];
306 #define ind_block (G.ind_block1)
307 #define dind_block (G.dind_block1)
309 #define NEXT_BAD (zone = next(zone))
313 mark_inode(MINIX_BAD_INO);
315 /* BTW, setting this makes all images different */
316 /* it's harder to check for bugs then - diff isn't helpful :(... */
317 inode->i_time = CUR_TIME;
318 inode->i_mode = S_IFREG + 0000;
319 inode->i_size = G.badblocks * BLOCK_SIZE;
321 for (i = 0; i < 7; i++) {
322 inode->i_zone[i] = zone;
326 inode->i_zone[7] = ind = get_free_block();
327 memset(ind_block, 0, BLOCK_SIZE);
328 for (i = 0; i < 512; i++) {
333 inode->i_zone[8] = dind = get_free_block();
334 memset(dind_block, 0, BLOCK_SIZE);
335 for (i = 0; i < 512; i++) {
336 write_block(ind, (char *) ind_block);
337 dind_block[i] = ind = get_free_block();
338 memset(ind_block, 0, BLOCK_SIZE);
339 for (j = 0; j < 512; j++) {
345 bb_error_msg_and_die("too many bad blocks");
348 write_block(ind, (char *) ind_block);
350 write_block(dind, (char *) dind_block);
355 #if ENABLE_FEATURE_MINIX2
356 static void make_bad_inode2(void)
358 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
360 int ind = 0, dind = 0;
361 /* moved to globals to reduce stack usage
362 unsigned long ind_block[BLOCK_SIZE >> 2];
363 unsigned long dind_block[BLOCK_SIZE >> 2];
365 #define ind_block (G.ind_block2)
366 #define dind_block (G.dind_block2)
370 mark_inode(MINIX_BAD_INO);
372 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
373 inode->i_mode = S_IFREG + 0000;
374 inode->i_size = G.badblocks * BLOCK_SIZE;
376 for (i = 0; i < 7; i++) {
377 inode->i_zone[i] = zone;
381 inode->i_zone[7] = ind = get_free_block();
382 memset(ind_block, 0, BLOCK_SIZE);
383 for (i = 0; i < 256; i++) {
388 inode->i_zone[8] = dind = get_free_block();
389 memset(dind_block, 0, BLOCK_SIZE);
390 for (i = 0; i < 256; i++) {
391 write_block(ind, (char *) ind_block);
392 dind_block[i] = ind = get_free_block();
393 memset(ind_block, 0, BLOCK_SIZE);
394 for (j = 0; j < 256; j++) {
400 /* Could make triple indirect block here */
401 bb_error_msg_and_die("too many bad blocks");
404 write_block(ind, (char *) ind_block);
406 write_block(dind, (char *) dind_block);
411 void make_bad_inode2(void);
414 static void make_root_inode(void)
416 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
418 mark_inode(MINIX_ROOT_INO);
419 inode->i_zone[0] = get_free_block();
421 inode->i_time = CUR_TIME;
423 inode->i_size = 3 * G.dirsize;
425 G.root_block[2 * G.dirsize] = '\0';
426 G.root_block[2 * G.dirsize + 1] = '\0';
427 inode->i_size = 2 * G.dirsize;
429 inode->i_mode = S_IFDIR + 0755;
430 inode->i_uid = GETUID;
432 inode->i_gid = GETGID;
433 write_block(inode->i_zone[0], G.root_block);
436 #if ENABLE_FEATURE_MINIX2
437 static void make_root_inode2(void)
439 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
441 mark_inode(MINIX_ROOT_INO);
442 inode->i_zone[0] = get_free_block();
444 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
446 inode->i_size = 3 * G.dirsize;
448 G.root_block[2 * G.dirsize] = '\0';
449 G.root_block[2 * G.dirsize + 1] = '\0';
450 inode->i_size = 2 * G.dirsize;
452 inode->i_mode = S_IFDIR + 0755;
453 inode->i_uid = GETUID;
455 inode->i_gid = GETGID;
456 write_block(inode->i_zone[0], G.root_block);
459 void make_root_inode2(void);
463 * Perform a test of a block; return the number of
466 static size_t do_check(char *buffer, size_t try, unsigned current_block)
470 /* Seek to the correct loc. */
471 msg_eol = "seek failed during testing of blocks";
472 xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
476 got = read(dev_fd, buffer, try * BLOCK_SIZE);
479 try = ((size_t)got) / BLOCK_SIZE;
481 if (got & (BLOCK_SIZE - 1))
482 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
486 static void alarm_intr(int alnum UNUSED_PARAM)
488 if (G.currently_testing >= SB_ZONES)
490 signal(SIGALRM, alarm_intr);
492 if (!G.currently_testing)
494 printf("%d ...", G.currently_testing);
498 static void check_blocks(void)
502 G.currently_testing = 0;
503 signal(SIGALRM, alarm_intr);
505 while (G.currently_testing < SB_ZONES) {
506 msg_eol = "seek failed in check_blocks";
507 xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
509 try = TEST_BUFFER_BLOCKS;
510 if (G.currently_testing + try > SB_ZONES)
511 try = SB_ZONES - G.currently_testing;
512 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
513 G.currently_testing += got;
516 if (G.currently_testing < SB_FIRSTZONE)
517 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
518 mark_zone(G.currently_testing);
520 G.currently_testing++;
523 printf("%d bad block(s)\n", G.badblocks);
526 static void get_list_blocks(char *filename)
529 unsigned long blockno;
531 listfile = xfopen_for_read(filename);
532 while (!feof(listfile)) {
533 fscanf(listfile, "%lu\n", &blockno);
537 printf("%d bad block(s)\n", G.badblocks);
540 static void setup_tables(void)
542 unsigned long inodes;
543 unsigned norm_firstzone;
547 /* memset(G.u.superblock_buffer, 0, BLOCK_SIZE); */
548 /* memset(G.boot_block_buffer, 0, 512); */
551 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
553 SB.s_zones = G.total_blocks;
555 SB.s_nzones = G.total_blocks;
557 /* some magic nrs: 1 inode / 3 blocks */
558 if (G.req_nr_inodes == 0)
559 inodes = G.total_blocks / 3;
561 inodes = G.req_nr_inodes;
562 /* Round up inode count to fill block size */
564 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
565 ~(MINIX2_INODES_PER_BLOCK - 1);
567 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
568 ~(MINIX1_INODES_PER_BLOCK - 1);
572 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
574 /* Real bad hack but overwise mkfs.minix can be thrown
575 * in infinite loop...
577 * dd if=/dev/zero of=test.fs count=10 bs=1024
578 * mkfs.minix -i 200 test.fs
580 /* This code is not insane: NORM_FIRSTZONE is not a constant,
581 * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
585 norm_firstzone = NORM_FIRSTZONE;
586 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
587 if (SB_ZMAPS == sb_zmaps) goto got_it;
589 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
591 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
594 SB_FIRSTZONE = norm_firstzone;
595 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
596 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
597 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
598 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
599 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
601 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
603 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
604 printf("%lu inodes\n", (unsigned long)SB_INODES);
605 printf("%lu blocks\n", (unsigned long)SB_ZONES);
606 printf("Firstdatazone=%lu (%lu)\n", (unsigned long)SB_FIRSTZONE, (unsigned long)norm_firstzone);
607 printf("Zonesize=%u\n", BLOCK_SIZE << SB_ZONE_SIZE);
608 printf("Maxsize=%lu\n", (unsigned long)SB_MAXSIZE);
611 int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
612 int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
617 char *listfile = NULL;
620 /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
623 G.magic = MINIX1_SUPER_MAGIC2;
625 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
626 bb_error_msg_and_die("bad inode size");
627 #if ENABLE_FEATURE_MINIX2
628 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
629 bb_error_msg_and_die("bad inode size");
632 opt = getopt32(argv, "ci:l:n:+v", &str_i, &listfile, &G.namelen);
635 if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
638 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
639 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
640 else bb_show_usage();
641 G.dirsize = G.namelen + 2;
643 if (opt & 0x10) { // -v
644 #if ENABLE_FEATURE_MINIX2
647 bb_error_msg_and_die("not compiled with minix v2 support");
651 G.device_name = argv[0];
655 /* Check if it is mounted */
656 if (find_mount_point(G.device_name, 0))
657 bb_error_msg_and_die("can't format mounted filesystem");
659 xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
661 G.total_blocks = get_volume_size_in_bytes(dev_fd, argv[1], 1024, /*extend:*/ 1) / 1024;
663 if (G.total_blocks < 10)
664 bb_error_msg_and_die("must have at least 10 blocks");
667 G.magic = MINIX2_SUPER_MAGIC2;
669 G.magic = MINIX2_SUPER_MAGIC;
670 } else if (G.total_blocks > 65535)
671 G.total_blocks = 65535;
674 xfstat(dev_fd, &statbuf, G.device_name);
676 if (!S_ISBLK(statbuf.st_mode))
677 opt &= ~1; // clear -c (check)
679 /* I don't know why someone has special code to prevent mkfs.minix
680 * on IDE devices. Why IDE but not SCSI, etc?... */
681 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
683 bb_error_msg_and_die("will not try "
684 "to make filesystem on '%s'", G.device_name);
689 strcpy(tmp + 2, ".");
692 strcpy(tmp + 2, "..");
695 strcpy(tmp + 2, ".badblocks");
702 get_list_blocks(listfile);