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.
70 /* Store the very same times/uids/gids for image consistency */
76 /* Was using this. Is it useful? NB: this will break testsuite */
77 # define CUR_TIME time(NULL)
78 # define GETUID getuid()
79 # define GETGID getgid()
83 MAX_GOOD_BLOCKS = 512,
84 TEST_BUFFER_BLOCKS = 16,
87 #if !ENABLE_FEATURE_MINIX2
88 enum { version2 = 0 };
94 #if ENABLE_FEATURE_MINIX2
96 #define version2 G.version2
99 uint32_t total_blocks;
107 int used_good_blocks;
108 unsigned long req_nr_inodes;
109 unsigned currently_testing;
111 char root_block[BLOCK_SIZE];
112 char super_block_buffer[BLOCK_SIZE];
113 char boot_block_buffer[512];
114 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
115 /* check_blocks(): buffer[] was the biggest static in entire bbox */
116 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
118 unsigned short ind_block1[BLOCK_SIZE >> 1];
119 unsigned short dind_block1[BLOCK_SIZE >> 1];
120 unsigned long ind_block2[BLOCK_SIZE >> 2];
121 unsigned long dind_block2[BLOCK_SIZE >> 2];
123 #define G (*ptr_to_globals)
124 #define INIT_G() do { \
125 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
128 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
130 return (size + n-1) / n;
133 #define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
134 #define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
136 #define SB (*(struct minix_super_block*)G.super_block_buffer)
138 #define SB_INODES (SB.s_ninodes)
139 #define SB_IMAPS (SB.s_imap_blocks)
140 #define SB_ZMAPS (SB.s_zmap_blocks)
141 #define SB_FIRSTZONE (SB.s_firstdatazone)
142 #define SB_ZONE_SIZE (SB.s_log_zone_size)
143 #define SB_MAXSIZE (SB.s_max_size)
144 #define SB_MAGIC (SB.s_magic)
146 #if !ENABLE_FEATURE_MINIX2
147 # define SB_ZONES (SB.s_nzones)
148 # define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
150 # define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
151 # define INODE_BLOCKS div_roundup(SB_INODES, \
152 version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)
155 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
156 #define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
158 /* Before you ask "where they come from?": */
159 /* setbit/clrbit are supplied by sys/param.h */
161 static int minix_bit(const char* a, unsigned i)
163 return a[i >> 3] & (1<<(i & 7));
166 static void minix_setbit(char *a, unsigned i)
170 static void minix_clrbit(char *a, unsigned i)
175 /* Note: do not assume 0/1, it is 0/nonzero */
176 #define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
177 /*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
179 #define mark_inode(x) minix_setbit(G.inode_map,(x))
180 #define unmark_inode(x) minix_clrbit(G.inode_map,(x))
181 #define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
182 #define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
185 # define BLKGETSIZE _IO(0x12,96) /* return device size */
189 static long valid_offset(int fd, int offset)
193 if (lseek(fd, offset, SEEK_SET) < 0)
195 if (read(fd, &ch, 1) < 1)
200 static int count_blocks(int fd)
205 for (high = 1; valid_offset(fd, high); high *= 2)
208 while (low < high - 1) {
209 const int mid = (low + high) / 2;
211 if (valid_offset(fd, mid))
220 static int get_size(const char *file)
225 fd = xopen(file, O_RDWR);
226 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
231 size = count_blocks(fd);
236 static void write_tables(void)
238 /* Mark the super block valid. */
239 SB.s_state |= MINIX_VALID_FS;
240 SB.s_state &= ~MINIX_ERROR_FS;
242 msg_eol = "seek to 0 failed";
243 xlseek(G.dev_fd, 0, SEEK_SET);
245 msg_eol = "cannot clear boot sector";
246 xwrite(G.dev_fd, G.boot_block_buffer, 512);
248 msg_eol = "seek to BLOCK_SIZE failed";
249 xlseek(G.dev_fd, BLOCK_SIZE, SEEK_SET);
251 msg_eol = "cannot write superblock";
252 xwrite(G.dev_fd, G.super_block_buffer, BLOCK_SIZE);
254 msg_eol = "cannot write inode map";
255 xwrite(G.dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
257 msg_eol = "cannot write zone map";
258 xwrite(G.dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
260 msg_eol = "cannot write inodes";
261 xwrite(G.dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
266 static void write_block(int blk, char *buffer)
268 xlseek(G.dev_fd, blk * BLOCK_SIZE, SEEK_SET);
269 xwrite(G.dev_fd, buffer, BLOCK_SIZE);
272 static int get_free_block(void)
276 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
277 bb_error_msg_and_die("too many bad blocks");
278 if (G.used_good_blocks)
279 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
282 while (blk < SB_ZONES && zone_in_use(blk))
285 bb_error_msg_and_die("not enough good blocks");
286 G.good_blocks_table[G.used_good_blocks] = blk;
287 G.used_good_blocks++;
291 static void mark_good_blocks(void)
295 for (blk = 0; blk < G.used_good_blocks; blk++)
296 mark_zone(G.good_blocks_table[blk]);
299 static int next(int zone)
302 zone = SB_FIRSTZONE - 1;
303 while (++zone < SB_ZONES)
304 if (zone_in_use(zone))
309 static void make_bad_inode(void)
311 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
313 int ind = 0, dind = 0;
314 /* moved to globals to reduce stack usage
315 unsigned short ind_block[BLOCK_SIZE >> 1];
316 unsigned short dind_block[BLOCK_SIZE >> 1];
318 #define ind_block (G.ind_block1)
319 #define dind_block (G.dind_block1)
321 #define NEXT_BAD (zone = next(zone))
325 mark_inode(MINIX_BAD_INO);
327 /* BTW, setting this makes all images different */
328 /* it's harder to check for bugs then - diff isn't helpful :(... */
329 inode->i_time = CUR_TIME;
330 inode->i_mode = S_IFREG + 0000;
331 inode->i_size = G.badblocks * BLOCK_SIZE;
333 for (i = 0; i < 7; i++) {
334 inode->i_zone[i] = zone;
338 inode->i_zone[7] = ind = get_free_block();
339 memset(ind_block, 0, BLOCK_SIZE);
340 for (i = 0; i < 512; i++) {
345 inode->i_zone[8] = dind = get_free_block();
346 memset(dind_block, 0, BLOCK_SIZE);
347 for (i = 0; i < 512; i++) {
348 write_block(ind, (char *) ind_block);
349 dind_block[i] = ind = get_free_block();
350 memset(ind_block, 0, BLOCK_SIZE);
351 for (j = 0; j < 512; j++) {
357 bb_error_msg_and_die("too many bad blocks");
360 write_block(ind, (char *) ind_block);
362 write_block(dind, (char *) dind_block);
367 #if ENABLE_FEATURE_MINIX2
368 static void make_bad_inode2(void)
370 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
372 int ind = 0, dind = 0;
373 /* moved to globals to reduce stack usage
374 unsigned long ind_block[BLOCK_SIZE >> 2];
375 unsigned long dind_block[BLOCK_SIZE >> 2];
377 #define ind_block (G.ind_block2)
378 #define dind_block (G.dind_block2)
382 mark_inode(MINIX_BAD_INO);
384 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
385 inode->i_mode = S_IFREG + 0000;
386 inode->i_size = G.badblocks * BLOCK_SIZE;
388 for (i = 0; i < 7; i++) {
389 inode->i_zone[i] = zone;
393 inode->i_zone[7] = ind = get_free_block();
394 memset(ind_block, 0, BLOCK_SIZE);
395 for (i = 0; i < 256; i++) {
400 inode->i_zone[8] = dind = get_free_block();
401 memset(dind_block, 0, BLOCK_SIZE);
402 for (i = 0; i < 256; i++) {
403 write_block(ind, (char *) ind_block);
404 dind_block[i] = ind = get_free_block();
405 memset(ind_block, 0, BLOCK_SIZE);
406 for (j = 0; j < 256; j++) {
412 /* Could make triple indirect block here */
413 bb_error_msg_and_die("too many bad blocks");
416 write_block(ind, (char *) ind_block);
418 write_block(dind, (char *) dind_block);
423 void make_bad_inode2(void);
426 static void make_root_inode(void)
428 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
430 mark_inode(MINIX_ROOT_INO);
431 inode->i_zone[0] = get_free_block();
433 inode->i_time = CUR_TIME;
435 inode->i_size = 3 * G.dirsize;
437 G.root_block[2 * G.dirsize] = '\0';
438 G.root_block[2 * G.dirsize + 1] = '\0';
439 inode->i_size = 2 * G.dirsize;
441 inode->i_mode = S_IFDIR + 0755;
442 inode->i_uid = GETUID;
444 inode->i_gid = GETGID;
445 write_block(inode->i_zone[0], G.root_block);
448 #if ENABLE_FEATURE_MINIX2
449 static void make_root_inode2(void)
451 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
453 mark_inode(MINIX_ROOT_INO);
454 inode->i_zone[0] = get_free_block();
456 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
458 inode->i_size = 3 * G.dirsize;
460 G.root_block[2 * G.dirsize] = '\0';
461 G.root_block[2 * G.dirsize + 1] = '\0';
462 inode->i_size = 2 * G.dirsize;
464 inode->i_mode = S_IFDIR + 0755;
465 inode->i_uid = GETUID;
467 inode->i_gid = GETGID;
468 write_block(inode->i_zone[0], G.root_block);
471 void make_root_inode2(void);
475 * Perform a test of a block; return the number of
478 static size_t do_check(char *buffer, size_t try, unsigned current_block)
482 /* Seek to the correct loc. */
483 msg_eol = "seek failed during testing of blocks";
484 xlseek(G.dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
488 got = read(G.dev_fd, buffer, try * BLOCK_SIZE);
491 try = ((size_t)got) / BLOCK_SIZE;
493 if (got & (BLOCK_SIZE - 1))
494 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
498 static void alarm_intr(int alnum ATTRIBUTE_UNUSED)
500 if (G.currently_testing >= SB_ZONES)
502 signal(SIGALRM, alarm_intr);
504 if (!G.currently_testing)
506 printf("%d ...", G.currently_testing);
510 static void check_blocks(void)
514 G.currently_testing = 0;
515 signal(SIGALRM, alarm_intr);
517 while (G.currently_testing < SB_ZONES) {
518 msg_eol = "seek failed in check_blocks";
519 xlseek(G.dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
521 try = TEST_BUFFER_BLOCKS;
522 if (G.currently_testing + try > SB_ZONES)
523 try = SB_ZONES - G.currently_testing;
524 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
525 G.currently_testing += got;
528 if (G.currently_testing < SB_FIRSTZONE)
529 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
530 mark_zone(G.currently_testing);
532 G.currently_testing++;
535 printf("%d bad block(s)\n", G.badblocks);
538 static void get_list_blocks(char *filename)
541 unsigned long blockno;
543 listfile = xfopen(filename, "r");
544 while (!feof(listfile)) {
545 fscanf(listfile, "%ld\n", &blockno);
549 printf("%d bad block(s)\n", G.badblocks);
552 static void setup_tables(void)
554 unsigned long inodes;
555 unsigned norm_firstzone;
559 /* memset(G.super_block_buffer, 0, BLOCK_SIZE); */
560 /* memset(G.boot_block_buffer, 0, 512); */
563 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
565 SB.s_zones = G.total_blocks;
567 SB.s_nzones = G.total_blocks;
569 /* some magic nrs: 1 inode / 3 blocks */
570 if (G.req_nr_inodes == 0)
571 inodes = G.total_blocks / 3;
573 inodes = G.req_nr_inodes;
574 /* Round up inode count to fill block size */
576 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
577 ~(MINIX2_INODES_PER_BLOCK - 1);
579 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
580 ~(MINIX1_INODES_PER_BLOCK - 1);
584 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
586 /* Real bad hack but overwise mkfs.minix can be thrown
587 * in infinite loop...
589 * dd if=/dev/zero of=test.fs count=10 bs=1024
590 * mkfs.minix -i 200 test.fs
592 /* This code is not insane: NORM_FIRSTZONE is not a constant,
593 * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
597 norm_firstzone = NORM_FIRSTZONE;
598 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
599 if (SB_ZMAPS == sb_zmaps) goto got_it;
601 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
603 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
606 SB_FIRSTZONE = norm_firstzone;
607 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
608 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
609 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
610 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
611 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
613 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
615 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
616 printf("%ld inodes\n", (long)SB_INODES);
617 printf("%ld blocks\n", (long)SB_ZONES);
618 printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone);
619 printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
620 printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
623 int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
624 int mkfs_minix_main(int argc ATTRIBUTE_UNUSED, char **argv)
631 char *listfile = NULL;
634 /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
637 G.magic = MINIX1_SUPER_MAGIC2;
639 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
640 bb_error_msg_and_die("bad inode size");
641 #if ENABLE_FEATURE_MINIX2
642 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
643 bb_error_msg_and_die("bad inode size");
646 opt_complementary = "n+"; /* -n N */
647 opt = getopt32(argv, "ci:l:n:v", &str_i, &listfile, &G.namelen);
650 if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
653 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
654 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
655 else bb_show_usage();
656 G.dirsize = G.namelen + 2;
658 if (opt & 0x10) { // -v
659 #if ENABLE_FEATURE_MINIX2
662 bb_error_msg_and_die("not compiled with minix v2 support");
666 G.device_name = *argv++;
670 G.total_blocks = xatou32(*argv);
672 G.total_blocks = get_size(G.device_name) / 1024;
674 if (G.total_blocks < 10)
675 bb_error_msg_and_die("must have at least 10 blocks");
678 G.magic = MINIX2_SUPER_MAGIC2;
680 G.magic = MINIX2_SUPER_MAGIC;
681 } else if (G.total_blocks > 65535)
682 G.total_blocks = 65535;
684 /* Check if it is mounted */
685 mp = find_mount_point(G.device_name, NULL);
686 if (mp && strcmp(G.device_name, mp->mnt_fsname) == 0)
687 bb_error_msg_and_die("%s is mounted on %s; "
688 "refusing to make a filesystem",
689 G.device_name, mp->mnt_dir);
691 G.dev_fd = xopen(G.device_name, O_RDWR);
692 if (fstat(G.dev_fd, &statbuf) < 0)
693 bb_error_msg_and_die("cannot stat %s", G.device_name);
694 if (!S_ISBLK(statbuf.st_mode))
695 opt &= ~1; // clear -c (check)
697 /* I don't know why someone has special code to prevent mkfs.minix
698 * on IDE devices. Why IDE but not SCSI, etc?... */
700 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
702 bb_error_msg_and_die("will not try "
703 "to make filesystem on '%s'", G.device_name);
708 strcpy(tmp + 2, ".");
711 strcpy(tmp + 2, "..");
714 strcpy(tmp + 2, ".badblocks");
721 get_list_blocks(listfile);