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 readablility 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 :-).
73 #include <sys/ioctl.h>
78 #include <linux/minix_fs.h>
80 #ifdef MINIX2_SUPER_MAGIC2
85 #error "needs gcc for the bitop-__asm__'s"
92 #define MINIX_ROOT_INO 1
93 #define MINIX_BAD_INO 2
95 #define TEST_BUFFER_BLOCKS 16
96 #define MAX_GOOD_BLOCKS 512
98 #define UPPER(size,n) (((size)+((n)-1))/(n))
99 #define INODE_SIZE (sizeof(struct minix_inode))
101 #define INODE_SIZE2 (sizeof(struct minix2_inode))
102 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
103 : MINIX_INODES_PER_BLOCK))
105 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
107 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
109 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
111 static char *program_name = "mkfs";
112 static char *device_name = NULL;
114 static long BLOCKS = 0;
115 static int check = 0;
116 static int badblocks = 0;
117 static int namelen = 30; /* default (changed to 30, per Linus's
119 suggestion, Sun Nov 21 08:05:07 1993) */
120 static int dirsize = 32;
121 static int magic = MINIX_SUPER_MAGIC2;
122 static int version2 = 0;
124 static char root_block[BLOCK_SIZE] = "\0";
126 static char *inode_buffer = NULL;
128 #define Inode (((struct minix_inode *) inode_buffer)-1)
130 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
132 static char super_block_buffer[BLOCK_SIZE];
133 static char boot_block_buffer[512];
135 #define Super (*(struct minix_super_block *)super_block_buffer)
136 #define INODES ((unsigned long)Super.s_ninodes)
138 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
140 #define ZONES ((unsigned long)(Super.s_nzones))
142 #define IMAPS ((unsigned long)Super.s_imap_blocks)
143 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
144 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
145 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
146 #define MAXSIZE ((unsigned long)Super.s_max_size)
147 #define MAGIC (Super.s_magic)
148 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
150 static char *inode_map;
151 static char *zone_map;
153 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
154 static int used_good_blocks = 0;
155 static unsigned long req_nr_inodes = 0;
157 #define inode_in_use(x) (bit(inode_map,(x)))
158 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
160 #define mark_inode(x) (setbit(inode_map,(x)))
161 #define unmark_inode(x) (clrbit(inode_map,(x)))
163 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
164 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
167 * Volatile to let gcc know that this doesn't return. When trying
168 * to compile this under minix, volatile gives a warning, as
169 * exit() isn't defined as volatile under minix.
171 static volatile void die(char *str)
173 fprintf(stderr, "%s: %s\n", program_name, str);
177 static volatile void show_usage()
179 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
182 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n",
184 fprintf(stderr, "Make a MINIX filesystem.\n\n");
185 fprintf(stderr, "OPTIONS:\n");
186 fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
188 "\t-n [14|30]\tSpecify the maximum length of filenames\n");
190 "\t-i\t\tSpecify the number of inodes for the filesystem\n");
192 "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
193 fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
198 * Check to make certain that our new filesystem won't be created on
199 * an already mounted partition. Code adapted from mke2fs, Copyright
200 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
202 static void check_mount(void)
207 if ((f = setmntent(MOUNTED, "r")) == NULL)
209 while ((mnt = getmntent(f)) != NULL)
210 if (strcmp(device_name, mnt->mnt_fsname) == 0)
216 die("%s is mounted; will not make a filesystem here!");
219 static long valid_offset(int fd, int offset)
223 if (lseek(fd, offset, 0) < 0)
225 if (read(fd, &ch, 1) < 1)
230 static int count_blocks(int fd)
235 for (high = 1; valid_offset(fd, high); high *= 2)
237 while (low < high - 1) {
238 const int mid = (low + high) / 2;
240 if (valid_offset(fd, mid))
249 static int get_size(const char *file)
254 fd = open(file, O_RDWR);
259 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
264 size = count_blocks(fd);
269 static void write_tables(void)
271 /* Mark the super block valid. */
272 Super.s_state |= MINIX_VALID_FS;
273 Super.s_state &= ~MINIX_ERROR_FS;
275 if (lseek(DEV, 0, SEEK_SET))
276 die("seek to boot block failed in write_tables");
277 if (512 != write(DEV, boot_block_buffer, 512))
278 die("unable to clear boot sector");
279 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
280 die("seek failed in write_tables");
281 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
282 die("unable to write super-block");
283 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
284 die("unable to write inode map");
285 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
286 die("unable to write zone map");
287 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
288 die("unable to write inodes");
292 static void write_block(int blk, char *buffer)
294 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
295 die("seek failed in write_block");
296 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
297 die("write failed in write_block");
300 static int get_free_block(void)
304 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
305 die("too many bad blocks");
306 if (used_good_blocks)
307 blk = good_blocks_table[used_good_blocks - 1] + 1;
310 while (blk < ZONES && zone_in_use(blk))
313 die("not enough good blocks");
314 good_blocks_table[used_good_blocks] = blk;
319 static void mark_good_blocks(void)
323 for (blk = 0; blk < used_good_blocks; blk++)
324 mark_zone(good_blocks_table[blk]);
327 inline int next(int zone)
330 zone = FIRSTZONE - 1;
331 while (++zone < ZONES)
332 if (zone_in_use(zone))
337 static void make_bad_inode(void)
339 struct minix_inode *inode = &Inode[MINIX_BAD_INO];
341 int ind = 0, dind = 0;
342 unsigned short ind_block[BLOCK_SIZE >> 1];
343 unsigned short dind_block[BLOCK_SIZE >> 1];
345 #define NEXT_BAD (zone = next(zone))
349 mark_inode(MINIX_BAD_INO);
351 inode->i_time = time(NULL);
352 inode->i_mode = S_IFREG + 0000;
353 inode->i_size = badblocks * BLOCK_SIZE;
355 for (i = 0; i < 7; i++) {
356 inode->i_zone[i] = zone;
360 inode->i_zone[7] = ind = get_free_block();
361 memset(ind_block, 0, BLOCK_SIZE);
362 for (i = 0; i < 512; i++) {
367 inode->i_zone[8] = dind = get_free_block();
368 memset(dind_block, 0, BLOCK_SIZE);
369 for (i = 0; i < 512; i++) {
370 write_block(ind, (char *) ind_block);
371 dind_block[i] = ind = get_free_block();
372 memset(ind_block, 0, BLOCK_SIZE);
373 for (j = 0; j < 512; j++) {
379 die("too many bad blocks");
382 write_block(ind, (char *) ind_block);
384 write_block(dind, (char *) dind_block);
388 static void make_bad_inode2(void)
390 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
392 int ind = 0, dind = 0;
393 unsigned long ind_block[BLOCK_SIZE >> 2];
394 unsigned long dind_block[BLOCK_SIZE >> 2];
398 mark_inode(MINIX_BAD_INO);
400 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
401 inode->i_mode = S_IFREG + 0000;
402 inode->i_size = badblocks * BLOCK_SIZE;
404 for (i = 0; i < 7; i++) {
405 inode->i_zone[i] = zone;
409 inode->i_zone[7] = ind = get_free_block();
410 memset(ind_block, 0, BLOCK_SIZE);
411 for (i = 0; i < 256; i++) {
416 inode->i_zone[8] = dind = get_free_block();
417 memset(dind_block, 0, BLOCK_SIZE);
418 for (i = 0; i < 256; i++) {
419 write_block(ind, (char *) ind_block);
420 dind_block[i] = ind = get_free_block();
421 memset(ind_block, 0, BLOCK_SIZE);
422 for (j = 0; j < 256; j++) {
428 /* Could make triple indirect block here */
429 die("too many bad blocks");
432 write_block(ind, (char *) ind_block);
434 write_block(dind, (char *) dind_block);
438 static void make_root_inode(void)
440 struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
442 mark_inode(MINIX_ROOT_INO);
443 inode->i_zone[0] = get_free_block();
445 inode->i_time = time(NULL);
447 inode->i_size = 3 * dirsize;
449 root_block[2 * dirsize] = '\0';
450 root_block[2 * dirsize + 1] = '\0';
451 inode->i_size = 2 * dirsize;
453 inode->i_mode = S_IFDIR + 0755;
454 inode->i_uid = getuid();
456 inode->i_gid = getgid();
457 write_block(inode->i_zone[0], root_block);
461 static void make_root_inode2(void)
463 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
465 mark_inode(MINIX_ROOT_INO);
466 inode->i_zone[0] = get_free_block();
468 inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
470 inode->i_size = 3 * dirsize;
472 root_block[2 * dirsize] = '\0';
473 root_block[2 * dirsize + 1] = '\0';
474 inode->i_size = 2 * dirsize;
476 inode->i_mode = S_IFDIR + 0755;
477 inode->i_uid = getuid();
479 inode->i_gid = getgid();
480 write_block(inode->i_zone[0], root_block);
484 static void setup_tables(void)
487 unsigned long inodes;
489 memset(super_block_buffer, 0, BLOCK_SIZE);
490 memset(boot_block_buffer, 0, 512);
493 MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
495 /* some magic nrs: 1 inode / 3 blocks */
496 if (req_nr_inodes == 0)
499 inodes = req_nr_inodes;
500 /* Round up inode count to fill block size */
503 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
504 ~(MINIX2_INODES_PER_BLOCK - 1));
507 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
508 ~(MINIX_INODES_PER_BLOCK - 1));
512 IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
516 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
517 BITS_PER_BLOCK) && i < 1000) {
519 UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
523 /* Real bad hack but overwise mkfs.minix can be thrown
524 * in infinite loop...
526 * dd if=/dev/zero of=test.fs count=10 bs=1024
527 * /sbin/mkfs.minix -i 200 test.fs
530 die("unable to allocate buffers for maps");
532 FIRSTZONE = NORM_FIRSTZONE;
533 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
534 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
535 memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
536 memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
537 for (i = FIRSTZONE; i < ZONES; i++)
539 for (i = MINIX_ROOT_INO; i <= INODES; i++)
541 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
542 memset(inode_buffer, 0, INODE_BUFFER_SIZE);
543 printf("%ld inodes\n", INODES);
544 printf("%ld blocks\n", ZONES);
545 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
546 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
547 printf("Maxsize=%ld\n\n", MAXSIZE);
551 * Perform a test of a block; return the number of
552 * blocks readable/writeable.
554 long do_check(char *buffer, int try, unsigned int current_block)
558 /* Seek to the correct loc. */
559 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
560 current_block * BLOCK_SIZE) {
561 die("seek failed during testing of blocks");
566 got = read(DEV, buffer, try * BLOCK_SIZE);
569 if (got & (BLOCK_SIZE - 1)) {
570 printf("Weird values in do_check: probably bugs\n");
576 static unsigned int currently_testing = 0;
578 static void alarm_intr(int alnum)
580 if (currently_testing >= ZONES)
582 signal(SIGALRM, alarm_intr);
584 if (!currently_testing)
586 printf("%d ...", currently_testing);
590 static void check_blocks(void)
593 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
595 currently_testing = 0;
596 signal(SIGALRM, alarm_intr);
598 while (currently_testing < ZONES) {
599 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
600 currently_testing * BLOCK_SIZE)
601 die("seek failed in check_blocks");
602 try = TEST_BUFFER_BLOCKS;
603 if (currently_testing + try > ZONES)
604 try = ZONES - currently_testing;
605 got = do_check(buffer, try, currently_testing);
606 currently_testing += got;
609 if (currently_testing < FIRSTZONE)
610 die("bad blocks before data-area: cannot make fs");
611 mark_zone(currently_testing);
616 printf("%d bad blocks\n", badblocks);
617 else if (badblocks == 1)
618 printf("one bad block\n");
621 static void get_list_blocks(filename)
626 unsigned long blockno;
628 listfile = fopen(filename, "r");
629 if (listfile == (FILE *) NULL) {
630 die("can't open file of bad blocks");
632 while (!feof(listfile)) {
633 fscanf(listfile, "%ld\n", &blockno);
638 printf("%d bad blocks\n", badblocks);
639 else if (badblocks == 1)
640 printf("one bad block\n");
643 extern int mkfs_minix_main(int argc, char **argv)
648 char *listfile = NULL;
651 program_name = *argv;
652 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
653 die("bad inode size");
655 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
656 die("bad inode size");
659 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
665 req_nr_inodes = (unsigned long) atol(optarg);
671 i = strtoul(optarg, &tmp, 0);
675 magic = MINIX_SUPER_MAGIC;
677 magic = MINIX_SUPER_MAGIC2;
687 fprintf(stderr, "%s: not compiled with minix v2 support\n",
688 program_name, device_name);
697 if (argc > 0 && !device_name) {
698 device_name = argv[0];
703 BLOCKS = strtol(argv[0], &tmp, 0);
705 printf("strtol error: number of blocks not specified");
710 if (device_name && !BLOCKS)
711 BLOCKS = get_size(device_name) / 1024;
712 if (!device_name || BLOCKS < 10) {
718 magic = MINIX2_SUPER_MAGIC;
720 magic = MINIX2_SUPER_MAGIC2;
725 check_mount(); /* is it already mounted? */
728 strcpy(tmp + 2, ".");
731 strcpy(tmp + 2, "..");
734 strcpy(tmp + 2, ".badblocks");
735 DEV = open(device_name, O_RDWR);
737 die("unable to open %s");
738 if (fstat(DEV, &statbuf) < 0)
739 die("unable to stat %s");
740 if (!S_ISBLK(statbuf.st_mode))
742 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
743 die("will not try to make filesystem on '%s'");
748 get_list_blocks(listfile);