2 * mkfs.c - make a linux (minix) file-system.
4 * (C) 1991 Linus Torvalds. This file may be redistributed as per
11 * 24.11.91 - Time began. Used the fsck sources to get started.
13 * 25.11.91 - Corrected some bugs. Added support for ".badblocks"
14 * The algorithm for ".badblocks" is a bit weird, but
15 * it should work. Oh, well.
17 * 25.01.92 - Added the -l option for getting the list of bad blocks
18 * out of a named file. (Dave Rivers, rivers@ponds.uucp)
20 * 28.02.92 - Added %-information when using -c.
22 * 28.02.93 - Added support for other namelengths than the original
23 * 14 characters so that I can test the new kernel routines..
25 * 09.10.93 - Make exit status conform to that required by fsutil
26 * (Rik Faith, faith@cs.unc.edu)
28 * 31.10.93 - Added inode request feature, for backup floppies: use
29 * 32 inodes, for a news partition use more.
30 * (Scott Heavner, sdh@po.cwru.edu)
32 * 03.01.94 - Added support for file system valid flag.
33 * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35 * 30.10.94 - added support for v2 filesystem
36 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
38 * 09.11.94 - Added test to prevent overwrite of mounted fs adapted
39 * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
40 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
42 * 03.20.95 - Clear first 512 bytes of filesystem to make certain that
43 * the filesystem is not misidentified as a MS-DOS FAT filesystem.
44 * (Daniel Quinlan, quinlan@yggdrasil.com)
46 * 02.07.96 - Added small patch from Russell King to make the program a
47 * good deal more portable (janl@math.uio.no)
49 * Usage: mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51 * -c for readablility checking (SLOW!)
52 * -l for getting a list of bad blocks from a file.
53 * -n for namelength (currently the kernel only uses 14 or 30)
54 * -i for number of inodes
55 * -v for v2 filesystem
57 * The device may be a block device or a image of one, but this isn't
58 * enforced (but it's not much fun on a character device :-).
72 #include <sys/ioctl.h>
77 #include <linux/minix_fs.h>
79 #ifdef MINIX2_SUPER_MAGIC2
84 #error "needs gcc for the bitop-__asm__'s"
91 #define MINIX_ROOT_INO 1
92 #define MINIX_BAD_INO 2
94 #define TEST_BUFFER_BLOCKS 16
95 #define MAX_GOOD_BLOCKS 512
97 #define UPPER(size,n) (((size)+((n)-1))/(n))
98 #define INODE_SIZE (sizeof(struct minix_inode))
100 #define INODE_SIZE2 (sizeof(struct minix2_inode))
101 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
102 : MINIX_INODES_PER_BLOCK))
104 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
106 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
108 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
110 static char * program_name = "mkfs";
111 static char * device_name = NULL;
113 static long BLOCKS = 0;
114 static int check = 0;
115 static int badblocks = 0;
116 static int namelen = 30; /* default (changed to 30, per Linus's
117 suggestion, Sun Nov 21 08:05:07 1993) */
118 static int dirsize = 32;
119 static int magic = MINIX_SUPER_MAGIC2;
120 static int version2 = 0;
122 static char root_block[BLOCK_SIZE] = "\0";
124 static char * inode_buffer = NULL;
125 #define Inode (((struct minix_inode *) inode_buffer)-1)
127 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
129 static char super_block_buffer[BLOCK_SIZE];
130 static char boot_block_buffer[512];
131 #define Super (*(struct minix_super_block *)super_block_buffer)
132 #define INODES ((unsigned long)Super.s_ninodes)
134 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
136 #define ZONES ((unsigned long)(Super.s_nzones))
138 #define IMAPS ((unsigned long)Super.s_imap_blocks)
139 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
140 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
141 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
142 #define MAXSIZE ((unsigned long)Super.s_max_size)
143 #define MAGIC (Super.s_magic)
144 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
146 static char *inode_map;
147 static char *zone_map;
149 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
150 static int used_good_blocks = 0;
151 static unsigned long req_nr_inodes = 0;
153 #define inode_in_use(x) (bit(inode_map,(x)))
154 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
156 #define mark_inode(x) (setbit(inode_map,(x)))
157 #define unmark_inode(x) (clrbit(inode_map,(x)))
159 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
160 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
163 * Volatile to let gcc know that this doesn't return. When trying
164 * to compile this under minix, volatile gives a warning, as
165 * exit() isn't defined as volatile under minix.
167 static volatile void die(char *str) {
168 fprintf(stderr, "%s: %s\n", program_name, str);
172 static volatile void show_usage()
174 fprintf(stderr, "%s\n", program_name);
176 "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
182 * Check to make certain that our new filesystem won't be created on
183 * an already mounted partition. Code adapted from mke2fs, Copyright
184 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
186 static void check_mount(void)
191 if ((f = setmntent (MOUNTED, "r")) == NULL)
193 while ((mnt = getmntent (f)) != NULL)
194 if (strcmp (device_name, mnt->mnt_fsname) == 0)
200 die("%s is mounted; will not make a filesystem here!");
203 static long valid_offset (int fd, int offset)
207 if (lseek (fd, offset, 0) < 0)
209 if (read (fd, &ch, 1) < 1)
214 static int count_blocks (int fd)
219 for (high = 1; valid_offset (fd, high); high *= 2)
221 while (low < high - 1)
223 const int mid = (low + high) / 2;
225 if (valid_offset (fd, mid))
230 valid_offset (fd, 0);
234 static int get_size(const char *file)
239 fd = open(file, O_RDWR);
244 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
249 size = count_blocks(fd);
254 static void write_tables(void)
256 /* Mark the super block valid. */
257 Super.s_state |= MINIX_VALID_FS;
258 Super.s_state &= ~MINIX_ERROR_FS;
260 if (lseek(DEV, 0, SEEK_SET))
261 die("seek to boot block failed in write_tables");
262 if (512 != write(DEV, boot_block_buffer, 512))
263 die("unable to clear boot sector");
264 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
265 die("seek failed in write_tables");
266 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
267 die("unable to write super-block");
268 if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
269 die("unable to write inode map");
270 if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
271 die("unable to write zone map");
272 if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
273 die("unable to write inodes");
277 static void write_block(int blk, char * buffer)
279 if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
280 die("seek failed in write_block");
281 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
282 die("write failed in write_block");
285 static int get_free_block(void)
289 if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
290 die("too many bad blocks");
291 if (used_good_blocks)
292 blk = good_blocks_table[used_good_blocks-1]+1;
295 while (blk < ZONES && zone_in_use(blk))
298 die("not enough good blocks");
299 good_blocks_table[used_good_blocks] = blk;
304 static void mark_good_blocks(void)
308 for (blk=0 ; blk < used_good_blocks ; blk++)
309 mark_zone(good_blocks_table[blk]);
312 inline int next(int zone)
316 while (++zone < ZONES)
317 if (zone_in_use(zone))
322 static void make_bad_inode(void)
324 struct minix_inode * inode = &Inode[MINIX_BAD_INO];
327 unsigned short ind_block[BLOCK_SIZE>>1];
328 unsigned short dind_block[BLOCK_SIZE>>1];
330 #define NEXT_BAD (zone = next(zone))
334 mark_inode(MINIX_BAD_INO);
336 inode->i_time = time(NULL);
337 inode->i_mode = S_IFREG + 0000;
338 inode->i_size = badblocks*BLOCK_SIZE;
340 for (i=0 ; i<7 ; i++) {
341 inode->i_zone[i] = zone;
345 inode->i_zone[7] = ind = get_free_block();
346 memset(ind_block,0,BLOCK_SIZE);
347 for (i=0 ; i<512 ; i++) {
352 inode->i_zone[8] = dind = get_free_block();
353 memset(dind_block,0,BLOCK_SIZE);
354 for (i=0 ; i<512 ; i++) {
355 write_block(ind,(char *) ind_block);
356 dind_block[i] = ind = get_free_block();
357 memset(ind_block,0,BLOCK_SIZE);
358 for (j=0 ; j<512 ; j++) {
364 die("too many bad blocks");
367 write_block(ind, (char *) ind_block);
369 write_block(dind, (char *) dind_block);
374 make_bad_inode2 (void)
376 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
378 int ind = 0, dind = 0;
379 unsigned long ind_block[BLOCK_SIZE >> 2];
380 unsigned long dind_block[BLOCK_SIZE >> 2];
384 mark_inode (MINIX_BAD_INO);
386 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
387 inode->i_mode = S_IFREG + 0000;
388 inode->i_size = badblocks * BLOCK_SIZE;
390 for (i = 0; i < 7; i++) {
391 inode->i_zone[i] = zone;
395 inode->i_zone[7] = ind = get_free_block ();
396 memset (ind_block, 0, BLOCK_SIZE);
397 for (i = 0; i < 256; i++) {
402 inode->i_zone[8] = dind = get_free_block ();
403 memset (dind_block, 0, BLOCK_SIZE);
404 for (i = 0; i < 256; i++) {
405 write_block (ind, (char *) ind_block);
406 dind_block[i] = ind = get_free_block ();
407 memset (ind_block, 0, BLOCK_SIZE);
408 for (j = 0; j < 256; j++) {
414 /* Could make triple indirect block here */
415 die ("too many bad blocks");
418 write_block (ind, (char *) ind_block);
420 write_block (dind, (char *) dind_block);
424 static void make_root_inode(void)
426 struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
428 mark_inode(MINIX_ROOT_INO);
429 inode->i_zone[0] = get_free_block();
431 inode->i_time = time(NULL);
433 inode->i_size = 3*dirsize;
435 root_block[2*dirsize] = '\0';
436 root_block[2*dirsize+1] = '\0';
437 inode->i_size = 2*dirsize;
439 inode->i_mode = S_IFDIR + 0755;
440 inode->i_uid = getuid();
442 inode->i_gid = getgid();
443 write_block(inode->i_zone[0],root_block);
448 make_root_inode2 (void)
450 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
452 mark_inode (MINIX_ROOT_INO);
453 inode->i_zone[0] = get_free_block ();
455 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
457 inode->i_size = 3 * dirsize;
459 root_block[2 * dirsize] = '\0';
460 root_block[2 * dirsize + 1] = '\0';
461 inode->i_size = 2 * dirsize;
463 inode->i_mode = S_IFDIR + 0755;
464 inode->i_uid = getuid();
466 inode->i_gid = getgid();
467 write_block (inode->i_zone[0], root_block);
471 static void setup_tables(void)
474 unsigned long inodes;
476 memset(super_block_buffer,0,BLOCK_SIZE);
477 memset(boot_block_buffer,0,512);
480 MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024;
482 /* some magic nrs: 1 inode / 3 blocks */
483 if ( req_nr_inodes == 0 )
486 inodes = req_nr_inodes;
487 /* Round up inode count to fill block size */
490 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
491 ~(MINIX2_INODES_PER_BLOCK - 1));
494 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
495 ~(MINIX_INODES_PER_BLOCK - 1));
499 IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK);
502 while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) {
503 ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK);
506 /* Real bad hack but overwise mkfs.minix can be thrown
507 * in infinite loop...
509 * dd if=/dev/zero of=test.fs count=10 bs=1024
510 * /sbin/mkfs.minix -i 200 test.fs
513 die ("unable to allocate buffers for maps");
515 FIRSTZONE = NORM_FIRSTZONE;
516 inode_map = malloc(IMAPS * BLOCK_SIZE);
517 zone_map = malloc(ZMAPS * BLOCK_SIZE);
518 if (!inode_map || !zone_map)
519 die("unable to allocate buffers for maps");
520 memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
521 memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
522 for (i = FIRSTZONE ; i<ZONES ; i++)
524 for (i = MINIX_ROOT_INO ; i<=INODES ; i++)
526 inode_buffer = malloc(INODE_BUFFER_SIZE);
528 die("unable to allocate buffer for inodes");
529 memset(inode_buffer,0,INODE_BUFFER_SIZE);
530 printf("%ld inodes\n",INODES);
531 printf("%ld blocks\n",ZONES);
532 printf("Firstdatazone=%ld (%ld)\n",FIRSTZONE,NORM_FIRSTZONE);
533 printf("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE);
534 printf("Maxsize=%ld\n\n",MAXSIZE);
538 * Perform a test of a block; return the number of
539 * blocks readable/writeable.
541 long do_check(char * buffer, int try, unsigned int current_block)
545 /* Seek to the correct loc. */
546 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
547 current_block * BLOCK_SIZE ) {
548 die("seek failed during testing of blocks");
553 got = read(DEV, buffer, try * BLOCK_SIZE);
554 if (got < 0) got = 0;
555 if (got & (BLOCK_SIZE - 1 )) {
556 printf("Weird values in do_check: probably bugs\n");
562 static unsigned int currently_testing = 0;
564 static void alarm_intr(int alnum)
566 if (currently_testing >= ZONES)
568 signal(SIGALRM,alarm_intr);
570 if (!currently_testing)
572 printf("%d ...", currently_testing);
576 static void check_blocks(void)
579 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
582 signal(SIGALRM,alarm_intr);
584 while (currently_testing < ZONES) {
585 if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
586 currently_testing*BLOCK_SIZE)
587 die("seek failed in check_blocks");
588 try = TEST_BUFFER_BLOCKS;
589 if (currently_testing + try > ZONES)
590 try = ZONES-currently_testing;
591 got = do_check(buffer, try, currently_testing);
592 currently_testing += got;
595 if (currently_testing < FIRSTZONE)
596 die("bad blocks before data-area: cannot make fs");
597 mark_zone(currently_testing);
602 printf("%d bad blocks\n", badblocks);
603 else if (badblocks == 1)
604 printf("one bad block\n");
607 static void get_list_blocks(filename)
612 unsigned long blockno;
614 listfile=fopen(filename,"r");
615 if(listfile == (FILE *)NULL) {
616 die("can't open file of bad blocks");
618 while(!feof(listfile)) {
619 fscanf(listfile,"%ld\n", &blockno);
624 printf("%d bad blocks\n", badblocks);
625 else if (badblocks == 1)
626 printf("one bad block\n");
630 mkfs_minix_main(int argc, char ** argv)
635 char * listfile = NULL;
638 program_name = *argv;
639 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
640 die("bad inode size");
642 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
643 die("bad inode size");
646 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
651 req_nr_inodes = (unsigned long) atol(optarg);
654 listfile = optarg; break;
656 i = strtoul(optarg,&tmp,0);
660 magic = MINIX_SUPER_MAGIC;
662 magic = MINIX_SUPER_MAGIC2;
672 fprintf(stderr,"%s: not compiled with minix v2 support\n",program_name,device_name);
681 if (argc > 0 && !device_name) {
682 device_name = argv[0];
687 BLOCKS = strtol(argv[0],&tmp,0);
689 printf("strtol error: number of blocks not specified");
694 if (device_name && !BLOCKS)
695 BLOCKS = get_size (device_name) / 1024;
696 if (!device_name || BLOCKS<10) {
702 magic = MINIX2_SUPER_MAGIC;
704 magic = MINIX2_SUPER_MAGIC2;
709 check_mount(); /* is it already mounted? */
718 strcpy(tmp+2,".badblocks");
719 DEV = open(device_name,O_RDWR );
721 die("unable to open %s");
722 if (fstat(DEV,&statbuf)<0)
723 die("unable to stat %s");
724 if (!S_ISBLK(statbuf.st_mode))
726 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
727 die("will not try to make filesystem on '%s'");
732 get_list_blocks(listfile);