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 :-).
71 #include <sys/ioctl.h>
76 #include <linux/minix_fs.h>
79 #include "../version.h"
81 #ifdef MINIX2_SUPER_MAGIC2
86 #error "needs gcc for the bitop-__asm__'s"
93 #define MINIX_ROOT_INO 1
94 #define MINIX_BAD_INO 2
96 #define TEST_BUFFER_BLOCKS 16
97 #define MAX_GOOD_BLOCKS 512
99 #define UPPER(size,n) (((size)+((n)-1))/(n))
100 #define INODE_SIZE (sizeof(struct minix_inode))
102 #define INODE_SIZE2 (sizeof(struct minix2_inode))
103 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
104 : MINIX_INODES_PER_BLOCK))
106 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
108 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
110 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
112 static char * program_name = "mkfs";
113 static char * device_name = NULL;
115 static long BLOCKS = 0;
116 static int check = 0;
117 static int badblocks = 0;
118 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;
127 #define Inode (((struct minix_inode *) inode_buffer)-1)
129 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
131 static char super_block_buffer[BLOCK_SIZE];
132 static char boot_block_buffer[512];
133 #define Super (*(struct minix_super_block *)super_block_buffer)
134 #define INODES ((unsigned long)Super.s_ninodes)
136 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
138 #define ZONES ((unsigned long)(Super.s_nzones))
140 #define IMAPS ((unsigned long)Super.s_imap_blocks)
141 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
142 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
143 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
144 #define MAXSIZE ((unsigned long)Super.s_max_size)
145 #define MAGIC (Super.s_magic)
146 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
148 static char *inode_map;
149 static char *zone_map;
151 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
152 static int used_good_blocks = 0;
153 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 volatile void fatal_error(const char * fmt_string,int status)
173 fprintf(stderr,fmt_string,program_name,device_name);
177 volatile void die(char *str) {
178 fprintf(stderr, "%s: %s\n", program_name, str);
182 volatile void usage()
184 fprintf(stderr, "%s (%s)\n", program_name, util_linux_version);
186 _("Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"),
192 * Check to make certain that our new filesystem won't be created on
193 * an already mounted partition. Code adapted from mke2fs, Copyright
194 * (C) 1994 Theodore Ts'o. Also licensed under GPL.
196 static void check_mount(void)
201 if ((f = setmntent (MOUNTED, "r")) == NULL)
203 while ((mnt = getmntent (f)) != NULL)
204 if (strcmp (device_name, mnt->mnt_fsname) == 0)
210 die(_("%s is mounted; will not make a filesystem here!"));
213 static long valid_offset (int fd, int offset)
217 if (lseek (fd, offset, 0) < 0)
219 if (read (fd, &ch, 1) < 1)
224 static int count_blocks (int fd)
229 for (high = 1; valid_offset (fd, high); high *= 2)
231 while (low < high - 1)
233 const int mid = (low + high) / 2;
235 if (valid_offset (fd, mid))
240 valid_offset (fd, 0);
244 static int get_size(const char *file)
249 fd = open(file, O_RDWR);
254 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
259 size = count_blocks(fd);
264 void write_tables(void)
266 /* Mark the super block valid. */
267 Super.s_state |= MINIX_VALID_FS;
268 Super.s_state &= ~MINIX_ERROR_FS;
270 if (lseek(DEV, 0, SEEK_SET))
271 die(_("seek to boot block failed in write_tables"));
272 if (512 != write(DEV, boot_block_buffer, 512))
273 die(_("unable to clear boot sector"));
274 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
275 die(_("seek failed in write_tables"));
276 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
277 die(_("unable to write super-block"));
278 if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE))
279 die(_("unable to write inode map"));
280 if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE))
281 die(_("unable to write zone map"));
282 if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE))
283 die(_("unable to write inodes"));
287 void write_block(int blk, char * buffer)
289 if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET))
290 die(_("seek failed in write_block"));
291 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
292 die(_("write failed in write_block"));
295 int get_free_block(void)
299 if (used_good_blocks+1 >= MAX_GOOD_BLOCKS)
300 die(_("too many bad blocks"));
301 if (used_good_blocks)
302 blk = good_blocks_table[used_good_blocks-1]+1;
305 while (blk < ZONES && zone_in_use(blk))
308 die(_("not enough good blocks"));
309 good_blocks_table[used_good_blocks] = blk;
314 void mark_good_blocks(void)
318 for (blk=0 ; blk < used_good_blocks ; blk++)
319 mark_zone(good_blocks_table[blk]);
322 inline int next(int zone)
326 while (++zone < ZONES)
327 if (zone_in_use(zone))
332 void make_bad_inode(void)
334 struct minix_inode * inode = &Inode[MINIX_BAD_INO];
337 unsigned short ind_block[BLOCK_SIZE>>1];
338 unsigned short dind_block[BLOCK_SIZE>>1];
340 #define NEXT_BAD (zone = next(zone))
344 mark_inode(MINIX_BAD_INO);
346 inode->i_time = time(NULL);
347 inode->i_mode = S_IFREG + 0000;
348 inode->i_size = badblocks*BLOCK_SIZE;
350 for (i=0 ; i<7 ; i++) {
351 inode->i_zone[i] = zone;
355 inode->i_zone[7] = ind = get_free_block();
356 memset(ind_block,0,BLOCK_SIZE);
357 for (i=0 ; i<512 ; i++) {
362 inode->i_zone[8] = dind = get_free_block();
363 memset(dind_block,0,BLOCK_SIZE);
364 for (i=0 ; i<512 ; i++) {
365 write_block(ind,(char *) ind_block);
366 dind_block[i] = ind = get_free_block();
367 memset(ind_block,0,BLOCK_SIZE);
368 for (j=0 ; j<512 ; j++) {
374 die(_("too many bad blocks"));
377 write_block(ind, (char *) ind_block);
379 write_block(dind, (char *) dind_block);
384 make_bad_inode2 (void)
386 struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
388 int ind = 0, dind = 0;
389 unsigned long ind_block[BLOCK_SIZE >> 2];
390 unsigned long dind_block[BLOCK_SIZE >> 2];
394 mark_inode (MINIX_BAD_INO);
396 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
397 inode->i_mode = S_IFREG + 0000;
398 inode->i_size = badblocks * BLOCK_SIZE;
400 for (i = 0; i < 7; i++) {
401 inode->i_zone[i] = zone;
405 inode->i_zone[7] = ind = get_free_block ();
406 memset (ind_block, 0, BLOCK_SIZE);
407 for (i = 0; i < 256; i++) {
412 inode->i_zone[8] = dind = get_free_block ();
413 memset (dind_block, 0, BLOCK_SIZE);
414 for (i = 0; i < 256; i++) {
415 write_block (ind, (char *) ind_block);
416 dind_block[i] = ind = get_free_block ();
417 memset (ind_block, 0, BLOCK_SIZE);
418 for (j = 0; j < 256; j++) {
424 /* Could make triple indirect block here */
425 die (_("too many bad blocks"));
428 write_block (ind, (char *) ind_block);
430 write_block (dind, (char *) dind_block);
434 void make_root_inode(void)
436 struct minix_inode * inode = &Inode[MINIX_ROOT_INO];
438 mark_inode(MINIX_ROOT_INO);
439 inode->i_zone[0] = get_free_block();
441 inode->i_time = time(NULL);
443 inode->i_size = 3*dirsize;
445 root_block[2*dirsize] = '\0';
446 root_block[2*dirsize+1] = '\0';
447 inode->i_size = 2*dirsize;
449 inode->i_mode = S_IFDIR + 0755;
450 inode->i_uid = getuid();
452 inode->i_gid = getgid();
453 write_block(inode->i_zone[0],root_block);
458 make_root_inode2 (void)
460 struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
462 mark_inode (MINIX_ROOT_INO);
463 inode->i_zone[0] = get_free_block ();
465 inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL);
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);
481 void setup_tables(void)
484 unsigned long inodes;
486 memset(super_block_buffer,0,BLOCK_SIZE);
487 memset(boot_block_buffer,0,512);
490 MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024;
492 /* some magic nrs: 1 inode / 3 blocks */
493 if ( req_nr_inodes == 0 )
496 inodes = req_nr_inodes;
497 /* Round up inode count to fill block size */
500 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
501 ~(MINIX2_INODES_PER_BLOCK - 1));
504 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
505 ~(MINIX_INODES_PER_BLOCK - 1));
509 IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK);
512 while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) {
513 ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK);
516 /* Real bad hack but overwise mkfs.minix can be thrown
517 * in infinite loop...
519 * dd if=/dev/zero of=test.fs count=10 bs=1024
520 * /sbin/mkfs.minix -i 200 test.fs
523 die ("unable to allocate buffers for maps");
525 FIRSTZONE = NORM_FIRSTZONE;
526 inode_map = malloc(IMAPS * BLOCK_SIZE);
527 zone_map = malloc(ZMAPS * BLOCK_SIZE);
528 if (!inode_map || !zone_map)
529 die(_("unable to allocate buffers for maps"));
530 memset(inode_map,0xff,IMAPS * BLOCK_SIZE);
531 memset(zone_map,0xff,ZMAPS * BLOCK_SIZE);
532 for (i = FIRSTZONE ; i<ZONES ; i++)
534 for (i = MINIX_ROOT_INO ; i<=INODES ; i++)
536 inode_buffer = malloc(INODE_BUFFER_SIZE);
538 die(_("unable to allocate buffer for inodes"));
539 memset(inode_buffer,0,INODE_BUFFER_SIZE);
540 printf(_("%ld inodes\n"),INODES);
541 printf(_("%ld blocks\n"),ZONES);
542 printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
543 printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
544 printf(_("Maxsize=%ld\n\n"),MAXSIZE);
548 * Perform a test of a block; return the number of
549 * blocks readable/writeable.
551 long do_check(char * buffer, int try, unsigned int current_block)
555 /* Seek to the correct loc. */
556 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
557 current_block * BLOCK_SIZE ) {
558 die(_("seek failed during testing of blocks"));
563 got = read(DEV, buffer, try * BLOCK_SIZE);
564 if (got < 0) got = 0;
565 if (got & (BLOCK_SIZE - 1 )) {
566 printf(_("Weird values in do_check: probably bugs\n"));
572 static unsigned int currently_testing = 0;
574 void alarm_intr(int alnum)
576 if (currently_testing >= ZONES)
578 signal(SIGALRM,alarm_intr);
580 if (!currently_testing)
582 printf("%d ...", currently_testing);
586 void check_blocks(void)
589 static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
592 signal(SIGALRM,alarm_intr);
594 while (currently_testing < ZONES) {
595 if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) !=
596 currently_testing*BLOCK_SIZE)
597 die(_("seek failed in check_blocks"));
598 try = TEST_BUFFER_BLOCKS;
599 if (currently_testing + try > ZONES)
600 try = ZONES-currently_testing;
601 got = do_check(buffer, try, currently_testing);
602 currently_testing += got;
605 if (currently_testing < FIRSTZONE)
606 die(_("bad blocks before data-area: cannot make fs"));
607 mark_zone(currently_testing);
612 printf(_("%d bad blocks\n"), badblocks);
613 else if (badblocks == 1)
614 printf(_("one bad block\n"));
617 void get_list_blocks(filename)
622 unsigned long blockno;
624 listfile=fopen(filename,"r");
625 if(listfile == (FILE *)NULL) {
626 die(_("can't open file of bad blocks"));
628 while(!feof(listfile)) {
629 fscanf(listfile,"%ld\n", &blockno);
634 printf(_("%d bad blocks\n"), badblocks);
635 else if (badblocks == 1)
636 printf(_("one bad block\n"));
640 mkfs_minix_main(int argc, char ** argv)
645 char * listfile = NULL;
648 program_name = *argv;
649 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
650 die(_("bad inode size"));
652 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
653 die(_("bad inode size"));
656 while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
661 req_nr_inodes = (unsigned long) atol(optarg);
664 listfile = optarg; break;
666 i = strtoul(optarg,&tmp,0);
670 magic = MINIX_SUPER_MAGIC;
672 magic = MINIX_SUPER_MAGIC2;
682 fatal_error(_("%s: not compiled with minix v2 support\n"),-1);
690 if (argc > 0 && !device_name) {
691 device_name = argv[0];
696 BLOCKS = strtol(argv[0],&tmp,0);
698 printf(_("strtol error: number of blocks not specified"));
703 if (device_name && !BLOCKS)
704 BLOCKS = get_size (device_name) / 1024;
705 if (!device_name || BLOCKS<10) {
711 magic = MINIX2_SUPER_MAGIC;
713 magic = MINIX2_SUPER_MAGIC2;
718 check_mount(); /* is it already mounted? */
727 strcpy(tmp+2,".badblocks");
728 DEV = open(device_name,O_RDWR );
730 die(_("unable to open %s"));
731 if (fstat(DEV,&statbuf)<0)
732 die(_("unable to stat %s"));
733 if (!S_ISBLK(statbuf.st_mode))
735 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
736 die(_("will not try to make filesystem on '%s'"));
741 get_list_blocks(listfile);