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 /* If debugging, store the very same times/uids/gids for image consistency */
76 # define CUR_TIME time(NULL)
77 # define GETUID getuid()
78 # define GETGID getgid()
82 * This is the original minix inode layout on disk.
83 * Note the 8-bit gid and atime and ctime.
96 * The new minix inode has all the time entries, as well as
97 * long block numbers and a third indirect block (7+1+1+1
98 * instead of 7+1+1). Also, some previously 8-bit values are
99 * now 16-bit. The inode is now 64 bytes instead of 32.
101 struct minix2_inode {
114 * minix super-block data on disk
116 struct minix_super_block {
119 uint16_t s_imap_blocks;
120 uint16_t s_zmap_blocks;
121 uint16_t s_firstdatazone;
122 uint16_t s_log_zone_size;
129 struct minix_dir_entry {
134 /* Believe it or not, but mount.h has this one */
138 BITS_PER_BLOCK = BLOCK_SIZE << 3,
142 MAX_GOOD_BLOCKS = 512,
144 MINIX1_SUPER_MAGIC = 0x137F, /* original minix fs */
145 MINIX1_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
146 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
147 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
148 MINIX_VALID_FS = 0x0001, /* clean fs */
149 MINIX_ERROR_FS = 0x0002, /* fs has errors */
151 INODE_SIZE1 = sizeof(struct minix1_inode),
152 INODE_SIZE2 = sizeof(struct minix2_inode),
153 MINIX1_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix1_inode),
154 MINIX2_INODES_PER_BLOCK = BLOCK_SIZE / sizeof(struct minix2_inode),
156 TEST_BUFFER_BLOCKS = 16,
159 #if ENABLE_FEATURE_MINIX2
162 enum { version2 = 0 };
165 static char *device_name;
166 static int dev_fd = -1;
167 static uint32_t total_blocks;
168 static int badblocks;
169 /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
170 static int namelen = 30;
171 static int dirsize = 32;
172 static int magic = MINIX1_SUPER_MAGIC2;
174 static char root_block[BLOCK_SIZE];
175 static char super_block_buffer[BLOCK_SIZE];
176 static char boot_block_buffer[512];
177 static char *inode_buffer;
179 static char *inode_map;
180 static char *zone_map;
182 static int used_good_blocks;
183 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
184 static unsigned long req_nr_inodes;
186 extern inline unsigned div_roundup(unsigned size, unsigned n)
188 return (size + n-1) / n;
191 #define INODE_BUF1 (((struct minix1_inode*)inode_buffer) - 1)
192 #define INODE_BUF2 (((struct minix2_inode*)inode_buffer) - 1)
194 #define SB (*(struct minix_super_block*)super_block_buffer)
196 #define SB_INODES (SB.s_ninodes)
197 #define SB_IMAPS (SB.s_imap_blocks)
198 #define SB_ZMAPS (SB.s_zmap_blocks)
199 #define SB_FIRSTZONE (SB.s_firstdatazone)
200 #define SB_ZONE_SIZE (SB.s_log_zone_size)
201 #define SB_MAXSIZE (SB.s_max_size)
202 #define SB_MAGIC (SB.s_magic)
204 #if !ENABLE_FEATURE_MINIX2
205 # define SB_ZONES (SB.s_nzones)
206 # define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
208 # define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
209 # define INODE_BLOCKS div_roundup(SB_INODES, \
210 version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)
213 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
214 #define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
216 static int bit(const char* a, unsigned i)
218 return a[i >> 3] & (1<<(i & 7));
221 /* Note: do not assume 0/1, it is 0/nonzero */
222 #define inode_in_use(x) bit(inode_map,(x))
223 #define zone_in_use(x) bit(zone_map,(x)-SB_FIRSTZONE+1)
225 #define mark_inode(x) setbit(inode_map,(x))
226 #define unmark_inode(x) clrbit(inode_map,(x))
227 #define mark_zone(x) setbit(zone_map,(x)-SB_FIRSTZONE+1)
228 #define unmark_zone(x) clrbit(zone_map,(x)-SB_FIRSTZONE+1)
231 # define BLKGETSIZE _IO(0x12,96) /* return device size */
235 static long valid_offset(int fd, int offset)
239 if (lseek(fd, offset, SEEK_SET) < 0)
241 if (read(fd, &ch, 1) < 1)
246 static int count_blocks(int fd)
251 for (high = 1; valid_offset(fd, high); high *= 2)
254 while (low < high - 1) {
255 const int mid = (low + high) / 2;
257 if (valid_offset(fd, mid))
266 static int get_size(const char *file)
271 fd = xopen(file, O_RDWR);
272 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
277 size = count_blocks(fd);
282 static void write_tables(void)
284 /* Mark the super block valid. */
285 SB.s_state |= MINIX_VALID_FS;
286 SB.s_state &= ~MINIX_ERROR_FS;
288 msg_eol = "seek to 0 failed";
289 xlseek(dev_fd, 0, SEEK_SET);
291 msg_eol = "cannot clear boot sector";
292 xwrite(dev_fd, boot_block_buffer, 512);
294 msg_eol = "seek to BLOCK_SIZE failed";
295 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
297 msg_eol = "cannot write superblock";
298 xwrite(dev_fd, super_block_buffer, BLOCK_SIZE);
300 msg_eol = "cannot write inode map";
301 xwrite(dev_fd, inode_map, SB_IMAPS * BLOCK_SIZE);
303 msg_eol = "cannot write zone map";
304 xwrite(dev_fd, zone_map, SB_ZMAPS * BLOCK_SIZE);
306 msg_eol = "cannot write inodes";
307 xwrite(dev_fd, inode_buffer, INODE_BUFFER_SIZE);
312 static void write_block(int blk, char *buffer)
314 xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
315 xwrite(dev_fd, buffer, BLOCK_SIZE);
318 static int get_free_block(void)
322 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
323 bb_error_msg_and_die("too many bad blocks");
324 if (used_good_blocks)
325 blk = good_blocks_table[used_good_blocks - 1] + 1;
328 while (blk < SB_ZONES && zone_in_use(blk))
331 bb_error_msg_and_die("not enough good blocks");
332 good_blocks_table[used_good_blocks] = blk;
337 static void mark_good_blocks(void)
341 for (blk = 0; blk < used_good_blocks; blk++)
342 mark_zone(good_blocks_table[blk]);
345 static int next(int zone)
348 zone = SB_FIRSTZONE - 1;
349 while (++zone < SB_ZONES)
350 if (zone_in_use(zone))
355 static void make_bad_inode(void)
357 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
359 int ind = 0, dind = 0;
360 unsigned short ind_block[BLOCK_SIZE >> 1];
361 unsigned short dind_block[BLOCK_SIZE >> 1];
363 #define NEXT_BAD (zone = next(zone))
367 mark_inode(MINIX_BAD_INO);
369 /* BTW, setting this makes all images different */
370 /* it's harder to check for bugs then - diff isn't helpful :(... */
371 inode->i_time = CUR_TIME;
372 inode->i_mode = S_IFREG + 0000;
373 inode->i_size = badblocks * BLOCK_SIZE;
375 for (i = 0; i < 7; i++) {
376 inode->i_zone[i] = zone;
380 inode->i_zone[7] = ind = get_free_block();
381 memset(ind_block, 0, BLOCK_SIZE);
382 for (i = 0; i < 512; i++) {
387 inode->i_zone[8] = dind = get_free_block();
388 memset(dind_block, 0, BLOCK_SIZE);
389 for (i = 0; i < 512; i++) {
390 write_block(ind, (char *) ind_block);
391 dind_block[i] = ind = get_free_block();
392 memset(ind_block, 0, BLOCK_SIZE);
393 for (j = 0; j < 512; j++) {
399 bb_error_msg_and_die("too many bad blocks");
402 write_block(ind, (char *) ind_block);
404 write_block(dind, (char *) dind_block);
407 #if ENABLE_FEATURE_MINIX2
408 static void make_bad_inode2(void)
410 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
412 int ind = 0, dind = 0;
413 unsigned long ind_block[BLOCK_SIZE >> 2];
414 unsigned long dind_block[BLOCK_SIZE >> 2];
418 mark_inode(MINIX_BAD_INO);
420 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
421 inode->i_mode = S_IFREG + 0000;
422 inode->i_size = badblocks * BLOCK_SIZE;
424 for (i = 0; i < 7; i++) {
425 inode->i_zone[i] = zone;
429 inode->i_zone[7] = ind = get_free_block();
430 memset(ind_block, 0, BLOCK_SIZE);
431 for (i = 0; i < 256; i++) {
436 inode->i_zone[8] = dind = get_free_block();
437 memset(dind_block, 0, BLOCK_SIZE);
438 for (i = 0; i < 256; i++) {
439 write_block(ind, (char *) ind_block);
440 dind_block[i] = ind = get_free_block();
441 memset(ind_block, 0, BLOCK_SIZE);
442 for (j = 0; j < 256; j++) {
448 /* Could make triple indirect block here */
449 bb_error_msg_and_die("too many bad blocks");
452 write_block(ind, (char *) ind_block);
454 write_block(dind, (char *) dind_block);
458 static void make_root_inode(void)
460 struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
462 mark_inode(MINIX_ROOT_INO);
463 inode->i_zone[0] = get_free_block();
465 inode->i_time = CUR_TIME;
467 inode->i_size = 3 * dirsize;
469 root_block[2 * dirsize] = '\0';
470 root_block[2 * dirsize + 1] = '\0';
471 inode->i_size = 2 * dirsize;
473 inode->i_mode = S_IFDIR + 0755;
474 inode->i_uid = GETUID;
476 inode->i_gid = GETGID;
477 write_block(inode->i_zone[0], root_block);
480 #if ENABLE_FEATURE_MINIX2
481 static void make_root_inode2(void)
483 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
485 mark_inode(MINIX_ROOT_INO);
486 inode->i_zone[0] = get_free_block();
488 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
490 inode->i_size = 3 * dirsize;
492 root_block[2 * dirsize] = '\0';
493 root_block[2 * dirsize + 1] = '\0';
494 inode->i_size = 2 * dirsize;
496 inode->i_mode = S_IFDIR + 0755;
497 inode->i_uid = GETUID;
499 inode->i_gid = GETGID;
500 write_block(inode->i_zone[0], root_block);
504 static void setup_tables(void)
507 unsigned long inodes;
509 memset(super_block_buffer, 0, BLOCK_SIZE);
510 memset(boot_block_buffer, 0, 512);
513 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
515 SB.s_zones = total_blocks;
517 SB.s_nzones = total_blocks;
519 /* some magic nrs: 1 inode / 3 blocks */
520 if (req_nr_inodes == 0)
521 inodes = total_blocks / 3;
523 inodes = req_nr_inodes;
524 /* Round up inode count to fill block size */
526 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
527 ~(MINIX2_INODES_PER_BLOCK - 1);
529 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
530 ~(MINIX1_INODES_PER_BLOCK - 1);
534 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
536 /* Real bad hack but overwise mkfs.minix can be thrown
537 * in infinite loop...
539 * dd if=/dev/zero of=test.fs count=10 bs=1024
540 * mkfs.minix -i 200 test.fs
542 /* This code is not insane: NORM_FIRSTZONE is not a constant, */
543 /* it uses previous value of SB_ZMAPS inside */
547 uint16_t t = div_roundup(total_blocks - NORM_FIRSTZONE + 1, BITS_PER_BLOCK);
548 if (SB_ZMAPS == t) goto got_it;
551 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
554 SB_FIRSTZONE = NORM_FIRSTZONE;
555 inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
556 zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
557 memset(inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
558 memset(zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
559 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
561 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
563 inode_buffer = xzalloc(INODE_BUFFER_SIZE);
564 printf("%ld inodes\n", (long)SB_INODES);
565 printf("%ld blocks\n", (long)SB_ZONES);
566 printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)NORM_FIRSTZONE);
567 printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
568 printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
572 * Perform a test of a block; return the number of
573 * blocks readable/writable.
575 static long do_check(char *buffer, int try, unsigned current_block)
579 /* Seek to the correct loc. */
580 msg_eol = "seek failed during testing of blocks";
581 xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
585 got = read(dev_fd, buffer, try * BLOCK_SIZE);
588 if (got & (BLOCK_SIZE - 1)) {
589 printf("Weird values in do_check: probably bugs\n");
595 static unsigned currently_testing;
597 static void alarm_intr(int alnum)
599 if (currently_testing >= SB_ZONES)
601 signal(SIGALRM, alarm_intr);
603 if (!currently_testing)
605 printf("%d ...", currently_testing);
609 static void check_blocks(void)
612 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
614 currently_testing = 0;
615 signal(SIGALRM, alarm_intr);
617 while (currently_testing < SB_ZONES) {
618 msg_eol = "seek failed in check_blocks";
619 xlseek(dev_fd, currently_testing * BLOCK_SIZE, SEEK_SET);
621 try = TEST_BUFFER_BLOCKS;
622 if (currently_testing + try > SB_ZONES)
623 try = SB_ZONES - currently_testing;
624 got = do_check(buffer, try, currently_testing);
625 currently_testing += got;
628 if (currently_testing < SB_FIRSTZONE)
629 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
630 mark_zone(currently_testing);
634 printf("%d bad block(s)\n", badblocks);
637 static void get_list_blocks(char *filename)
640 unsigned long blockno;
642 listfile = xfopen(filename, "r");
643 while (!feof(listfile)) {
644 fscanf(listfile, "%ld\n", &blockno);
648 printf("%d bad block(s)\n", badblocks);
651 int mkfs_minix_main(int argc, char **argv)
658 char *listfile = NULL;
660 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
661 bb_error_msg_and_die("bad inode size");
662 #if ENABLE_FEATURE_MINIX2
663 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
664 bb_error_msg_and_die("bad inode size");
667 opt = getopt32(argc, argv, "ci:l:n:v", &str_i, &listfile, &str_n);
670 if (opt & 2) req_nr_inodes = xatoul(str_i); // -i
673 namelen = xatoi_u(str_n);
674 if (namelen == 14) magic = MINIX1_SUPER_MAGIC;
675 else if (namelen == 30) magic = MINIX1_SUPER_MAGIC2;
676 else bb_show_usage();
677 dirsize = namelen + 2;
679 if (opt & 0x10) { // -v
680 #if ENABLE_FEATURE_MINIX2
683 bb_error_msg_and_die("%s: not compiled with minix v2 support",
688 device_name = *argv++;
692 total_blocks = xatou32(*argv);
694 total_blocks = get_size(device_name) / 1024;
696 if (total_blocks < 10)
697 bb_error_msg_and_die("must have at least 10 blocks");
700 magic = MINIX2_SUPER_MAGIC2;
702 magic = MINIX2_SUPER_MAGIC;
703 } else if (total_blocks > 65535)
704 total_blocks = 65535;
706 /* Check if it is mounted */
707 mp = find_mount_point(device_name, NULL);
708 if (mp && strcmp(device_name, mp->mnt_fsname) == 0)
709 bb_error_msg_and_die("%s is mounted on %s; "
710 "refusing to make a filesystem",
711 device_name, mp->mnt_dir);
713 dev_fd = xopen(device_name, O_RDWR);
714 if (fstat(dev_fd, &statbuf) < 0)
715 bb_error_msg_and_die("cannot stat %s", device_name);
716 if (!S_ISBLK(statbuf.st_mode))
717 opt &= ~1; // clear -c (check)
719 /* I don't know why someone has special code to prevent mkfs.minix
720 * on IDE devices. Why IDE but not SCSI, etc?... */
722 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
724 bb_error_msg_and_die("will not try "
725 "to make filesystem on '%s'", device_name);
730 strcpy(tmp + 2, ".");
733 strcpy(tmp + 2, "..");
736 strcpy(tmp + 2, ".badblocks");
743 get_list_blocks(listfile);