More little stuff.
[oweals/busybox.git] / mkfs_minix.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mkfs.c - make a linux (minix) file-system.
4  *
5  * (C) 1991 Linus Torvalds. This file may be redistributed as per
6  * the Linux copyright.
7  */
8
9 /*
10  * DD.MM.YY
11  *
12  * 24.11.91  -  Time began. Used the fsck sources to get started.
13  *
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.
17  *
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)
20  *
21  * 28.02.92  -  Added %-information when using -c.
22  *
23  * 28.02.93  -  Added support for other namelengths than the original
24  *              14 characters so that I can test the new kernel routines..
25  *
26  * 09.10.93  -  Make exit status conform to that required by fsutil
27  *              (Rik Faith, faith@cs.unc.edu)
28  *
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)
32  *
33  * 03.01.94  -  Added support for file system valid flag.
34  *              (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35  *
36  * 30.10.94 - added support for v2 filesystem
37  *            (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
38  * 
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)
42  *
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)
46  *
47  * 02.07.96  -  Added small patch from Russell King to make the program a
48  *              good deal more portable (janl@math.uio.no)
49  *
50  * Usage:  mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51  *
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
57  *
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 :-). 
60  */
61
62 #include "internal.h"
63 #include <stdio.h>
64 #include <time.h>
65 #include <unistd.h>
66 #include <string.h>
67 #include <signal.h>
68 #include <fcntl.h>
69 #include <ctype.h>
70 #include <stdlib.h>
71 #include <termios.h>
72 #include <sys/stat.h>
73 #include <sys/ioctl.h>
74 #include <mntent.h>
75 #include <getopt.h>
76
77 #include <linux/fs.h>
78 #include <linux/minix_fs.h>
79
80 #ifdef MINIX2_SUPER_MAGIC2
81 #define HAVE_MINIX2 1
82 #endif
83
84 #ifndef __GNUC__
85 #error "needs gcc for the bitop-__asm__'s"
86 #endif
87
88 #ifndef __linux__
89 #define volatile
90 #endif
91
92 #define MINIX_ROOT_INO 1
93 #define MINIX_BAD_INO 2
94
95 #define TEST_BUFFER_BLOCKS 16
96 #define MAX_GOOD_BLOCKS 512
97
98 #define UPPER(size,n) (((size)+((n)-1))/(n))
99 #define INODE_SIZE (sizeof(struct minix_inode))
100 #ifdef HAVE_MINIX2
101 #define INODE_SIZE2 (sizeof(struct minix2_inode))
102 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
103                                     : MINIX_INODES_PER_BLOCK))
104 #else
105 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
106 #endif
107 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
108
109 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
110
111 static char *program_name = "mkfs";
112 static char *device_name = NULL;
113 static int DEV = -1;
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
118
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;
123
124 static char root_block[BLOCK_SIZE] = "\0";
125
126 static char *inode_buffer = NULL;
127
128 #define Inode (((struct minix_inode *) inode_buffer)-1)
129 #ifdef HAVE_MINIX2
130 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
131 #endif
132 static char super_block_buffer[BLOCK_SIZE];
133 static char boot_block_buffer[512];
134
135 #define Super (*(struct minix_super_block *)super_block_buffer)
136 #define INODES ((unsigned long)Super.s_ninodes)
137 #ifdef HAVE_MINIX2
138 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
139 #else
140 #define ZONES ((unsigned long)(Super.s_nzones))
141 #endif
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)
149
150 static char *inode_map;
151 static char *zone_map;
152
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;
156
157 #define inode_in_use(x) (bit(inode_map,(x)))
158 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
159
160 #define mark_inode(x) (setbit(inode_map,(x)))
161 #define unmark_inode(x) (clrbit(inode_map,(x)))
162
163 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
164 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
165
166 /*
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.
170  */
171 static volatile void die(char *str)
172 {
173         fprintf(stderr, "%s: %s\n", program_name, str);
174         exit(8);
175 }
176
177 static volatile void show_usage()
178 {
179         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
180                         BB_VER, BB_BT);
181         fprintf(stderr,
182                         "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n",
183                         program_name);
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");
187         fprintf(stderr,
188                         "\t-n [14|30]\tSpecify the maximum length of filenames\n");
189         fprintf(stderr,
190                         "\t-i\t\tSpecify the number of inodes for the filesystem\n");
191         fprintf(stderr,
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");
194         exit(16);
195 }
196
197 /*
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.
201  */
202 static void check_mount(void)
203 {
204         FILE *f;
205         struct mntent *mnt;
206
207         if ((f = setmntent(MOUNTED, "r")) == NULL)
208                 return;
209         while ((mnt = getmntent(f)) != NULL)
210                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
211                         break;
212         endmntent(f);
213         if (!mnt)
214                 return;
215
216         die("%s is mounted; will not make a filesystem here!");
217 }
218
219 static long valid_offset(int fd, int offset)
220 {
221         char ch;
222
223         if (lseek(fd, offset, 0) < 0)
224                 return 0;
225         if (read(fd, &ch, 1) < 1)
226                 return 0;
227         return 1;
228 }
229
230 static int count_blocks(int fd)
231 {
232         int high, low;
233
234         low = 0;
235         for (high = 1; valid_offset(fd, high); high *= 2)
236                 low = high;
237         while (low < high - 1) {
238                 const int mid = (low + high) / 2;
239
240                 if (valid_offset(fd, mid))
241                         low = mid;
242                 else
243                         high = mid;
244         }
245         valid_offset(fd, 0);
246         return (low + 1);
247 }
248
249 static int get_size(const char *file)
250 {
251         int fd;
252         long size;
253
254         fd = open(file, O_RDWR);
255         if (fd < 0) {
256                 perror(file);
257                 exit(1);
258         }
259         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
260                 close(fd);
261                 return (size * 512);
262         }
263
264         size = count_blocks(fd);
265         close(fd);
266         return size;
267 }
268
269 static void write_tables(void)
270 {
271         /* Mark the super block valid. */
272         Super.s_state |= MINIX_VALID_FS;
273         Super.s_state &= ~MINIX_ERROR_FS;
274
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");
289
290 }
291
292 static void write_block(int blk, char *buffer)
293 {
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");
298 }
299
300 static int get_free_block(void)
301 {
302         int blk;
303
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;
308         else
309                 blk = FIRSTZONE;
310         while (blk < ZONES && zone_in_use(blk))
311                 blk++;
312         if (blk >= ZONES)
313                 die("not enough good blocks");
314         good_blocks_table[used_good_blocks] = blk;
315         used_good_blocks++;
316         return blk;
317 }
318
319 static void mark_good_blocks(void)
320 {
321         int blk;
322
323         for (blk = 0; blk < used_good_blocks; blk++)
324                 mark_zone(good_blocks_table[blk]);
325 }
326
327 inline int next(int zone)
328 {
329         if (!zone)
330                 zone = FIRSTZONE - 1;
331         while (++zone < ZONES)
332                 if (zone_in_use(zone))
333                         return zone;
334         return 0;
335 }
336
337 static void make_bad_inode(void)
338 {
339         struct minix_inode *inode = &Inode[MINIX_BAD_INO];
340         int i, j, zone;
341         int ind = 0, dind = 0;
342         unsigned short ind_block[BLOCK_SIZE >> 1];
343         unsigned short dind_block[BLOCK_SIZE >> 1];
344
345 #define NEXT_BAD (zone = next(zone))
346
347         if (!badblocks)
348                 return;
349         mark_inode(MINIX_BAD_INO);
350         inode->i_nlinks = 1;
351         inode->i_time = time(NULL);
352         inode->i_mode = S_IFREG + 0000;
353         inode->i_size = badblocks * BLOCK_SIZE;
354         zone = next(0);
355         for (i = 0; i < 7; i++) {
356                 inode->i_zone[i] = zone;
357                 if (!NEXT_BAD)
358                         goto end_bad;
359         }
360         inode->i_zone[7] = ind = get_free_block();
361         memset(ind_block, 0, BLOCK_SIZE);
362         for (i = 0; i < 512; i++) {
363                 ind_block[i] = zone;
364                 if (!NEXT_BAD)
365                         goto end_bad;
366         }
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++) {
374                         ind_block[j] = zone;
375                         if (!NEXT_BAD)
376                                 goto end_bad;
377                 }
378         }
379         die("too many bad blocks");
380   end_bad:
381         if (ind)
382                 write_block(ind, (char *) ind_block);
383         if (dind)
384                 write_block(dind, (char *) dind_block);
385 }
386
387 #ifdef HAVE_MINIX2
388 static void make_bad_inode2(void)
389 {
390         struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
391         int i, j, zone;
392         int ind = 0, dind = 0;
393         unsigned long ind_block[BLOCK_SIZE >> 2];
394         unsigned long dind_block[BLOCK_SIZE >> 2];
395
396         if (!badblocks)
397                 return;
398         mark_inode(MINIX_BAD_INO);
399         inode->i_nlinks = 1;
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;
403         zone = next(0);
404         for (i = 0; i < 7; i++) {
405                 inode->i_zone[i] = zone;
406                 if (!NEXT_BAD)
407                         goto end_bad;
408         }
409         inode->i_zone[7] = ind = get_free_block();
410         memset(ind_block, 0, BLOCK_SIZE);
411         for (i = 0; i < 256; i++) {
412                 ind_block[i] = zone;
413                 if (!NEXT_BAD)
414                         goto end_bad;
415         }
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++) {
423                         ind_block[j] = zone;
424                         if (!NEXT_BAD)
425                                 goto end_bad;
426                 }
427         }
428         /* Could make triple indirect block here */
429         die("too many bad blocks");
430   end_bad:
431         if (ind)
432                 write_block(ind, (char *) ind_block);
433         if (dind)
434                 write_block(dind, (char *) dind_block);
435 }
436 #endif
437
438 static void make_root_inode(void)
439 {
440         struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
441
442         mark_inode(MINIX_ROOT_INO);
443         inode->i_zone[0] = get_free_block();
444         inode->i_nlinks = 2;
445         inode->i_time = time(NULL);
446         if (badblocks)
447                 inode->i_size = 3 * dirsize;
448         else {
449                 root_block[2 * dirsize] = '\0';
450                 root_block[2 * dirsize + 1] = '\0';
451                 inode->i_size = 2 * dirsize;
452         }
453         inode->i_mode = S_IFDIR + 0755;
454         inode->i_uid = getuid();
455         if (inode->i_uid)
456                 inode->i_gid = getgid();
457         write_block(inode->i_zone[0], root_block);
458 }
459
460 #ifdef HAVE_MINIX2
461 static void make_root_inode2(void)
462 {
463         struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
464
465         mark_inode(MINIX_ROOT_INO);
466         inode->i_zone[0] = get_free_block();
467         inode->i_nlinks = 2;
468         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
469         if (badblocks)
470                 inode->i_size = 3 * dirsize;
471         else {
472                 root_block[2 * dirsize] = '\0';
473                 root_block[2 * dirsize + 1] = '\0';
474                 inode->i_size = 2 * dirsize;
475         }
476         inode->i_mode = S_IFDIR + 0755;
477         inode->i_uid = getuid();
478         if (inode->i_uid)
479                 inode->i_gid = getgid();
480         write_block(inode->i_zone[0], root_block);
481 }
482 #endif
483
484 static void setup_tables(void)
485 {
486         int i;
487         unsigned long inodes;
488
489         memset(super_block_buffer, 0, BLOCK_SIZE);
490         memset(boot_block_buffer, 0, 512);
491         MAGIC = magic;
492         ZONESIZE = 0;
493         MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
494         ZONES = BLOCKS;
495 /* some magic nrs: 1 inode / 3 blocks */
496         if (req_nr_inodes == 0)
497                 inodes = BLOCKS / 3;
498         else
499                 inodes = req_nr_inodes;
500         /* Round up inode count to fill block size */
501 #ifdef HAVE_MINIX2
502         if (version2)
503                 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
504                                   ~(MINIX2_INODES_PER_BLOCK - 1));
505         else
506 #endif
507                 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
508                                   ~(MINIX_INODES_PER_BLOCK - 1));
509         if (inodes > 65535)
510                 inodes = 65535;
511         INODES = inodes;
512         IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
513         ZMAPS = 0;
514         i = 0;
515         while (ZMAPS !=
516                    UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
517                                  BITS_PER_BLOCK) && i < 1000) {
518                 ZMAPS =
519                         UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
520                                   BITS_PER_BLOCK);
521                 i++;
522         }
523         /* Real bad hack but overwise mkfs.minix can be thrown
524          * in infinite loop...
525          * try:
526          * dd if=/dev/zero of=test.fs count=10 bs=1024
527          * /sbin/mkfs.minix -i 200 test.fs
528          * */
529         if (i >= 999) {
530                 die("unable to allocate buffers for maps");
531         }
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++)
538                 unmark_zone(i);
539         for (i = MINIX_ROOT_INO; i <= INODES; i++)
540                 unmark_inode(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);
548 }
549
550 /*
551  * Perform a test of a block; return the number of
552  * blocks readable/writeable.
553  */
554 long do_check(char *buffer, int try, unsigned int current_block)
555 {
556         long got;
557
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");
562         }
563
564
565         /* Try the read */
566         got = read(DEV, buffer, try * BLOCK_SIZE);
567         if (got < 0)
568                 got = 0;
569         if (got & (BLOCK_SIZE - 1)) {
570                 printf("Weird values in do_check: probably bugs\n");
571         }
572         got /= BLOCK_SIZE;
573         return got;
574 }
575
576 static unsigned int currently_testing = 0;
577
578 static void alarm_intr(int alnum)
579 {
580         if (currently_testing >= ZONES)
581                 return;
582         signal(SIGALRM, alarm_intr);
583         alarm(5);
584         if (!currently_testing)
585                 return;
586         printf("%d ...", currently_testing);
587         fflush(stdout);
588 }
589
590 static void check_blocks(void)
591 {
592         int try, got;
593         static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
594
595         currently_testing = 0;
596         signal(SIGALRM, alarm_intr);
597         alarm(5);
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;
607                 if (got == try)
608                         continue;
609                 if (currently_testing < FIRSTZONE)
610                         die("bad blocks before data-area: cannot make fs");
611                 mark_zone(currently_testing);
612                 badblocks++;
613                 currently_testing++;
614         }
615         if (badblocks > 1)
616                 printf("%d bad blocks\n", badblocks);
617         else if (badblocks == 1)
618                 printf("one bad block\n");
619 }
620
621 static void get_list_blocks(filename)
622 char *filename;
623
624 {
625         FILE *listfile;
626         unsigned long blockno;
627
628         listfile = fopen(filename, "r");
629         if (listfile == (FILE *) NULL) {
630                 die("can't open file of bad blocks");
631         }
632         while (!feof(listfile)) {
633                 fscanf(listfile, "%ld\n", &blockno);
634                 mark_zone(blockno);
635                 badblocks++;
636         }
637         if (badblocks > 1)
638                 printf("%d bad blocks\n", badblocks);
639         else if (badblocks == 1)
640                 printf("one bad block\n");
641 }
642
643 extern int mkfs_minix_main(int argc, char **argv)
644 {
645         int i;
646         char *tmp;
647         struct stat statbuf;
648         char *listfile = NULL;
649
650         if (argc && *argv)
651                 program_name = *argv;
652         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
653                 die("bad inode size");
654 #ifdef HAVE_MINIX2
655         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
656                 die("bad inode size");
657 #endif
658         opterr = 0;
659         while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
660                 switch (i) {
661                 case 'c':
662                         check = 1;
663                         break;
664                 case 'i':
665                         req_nr_inodes = (unsigned long) atol(optarg);
666                         break;
667                 case 'l':
668                         listfile = optarg;
669                         break;
670                 case 'n':
671                         i = strtoul(optarg, &tmp, 0);
672                         if (*tmp)
673                                 show_usage();
674                         if (i == 14)
675                                 magic = MINIX_SUPER_MAGIC;
676                         else if (i == 30)
677                                 magic = MINIX_SUPER_MAGIC2;
678                         else
679                                 show_usage();
680                         namelen = i;
681                         dirsize = i + 2;
682                         break;
683                 case 'v':
684 #ifdef HAVE_MINIX2
685                         version2 = 1;
686 #else
687                         fprintf(stderr, "%s: not compiled with minix v2 support\n",
688                                         program_name, device_name);
689                         exit(-1);
690 #endif
691                         break;
692                 default:
693                         show_usage();
694                 }
695         argc -= optind;
696         argv += optind;
697         if (argc > 0 && !device_name) {
698                 device_name = argv[0];
699                 argc--;
700                 argv++;
701         }
702         if (argc > 0) {
703                 BLOCKS = strtol(argv[0], &tmp, 0);
704                 if (*tmp) {
705                         printf("strtol error: number of blocks not specified");
706                         show_usage();
707                 }
708         }
709
710         if (device_name && !BLOCKS)
711                 BLOCKS = get_size(device_name) / 1024;
712         if (!device_name || BLOCKS < 10) {
713                 show_usage();
714         }
715 #ifdef HAVE_MINIX2
716         if (version2) {
717                 if (namelen == 14)
718                         magic = MINIX2_SUPER_MAGIC;
719                 else
720                         magic = MINIX2_SUPER_MAGIC2;
721         } else
722 #endif
723         if (BLOCKS > 65535)
724                 BLOCKS = 65535;
725         check_mount();                          /* is it already mounted? */
726         tmp = root_block;
727         *(short *) tmp = 1;
728         strcpy(tmp + 2, ".");
729         tmp += dirsize;
730         *(short *) tmp = 1;
731         strcpy(tmp + 2, "..");
732         tmp += dirsize;
733         *(short *) tmp = 2;
734         strcpy(tmp + 2, ".badblocks");
735         DEV = open(device_name, O_RDWR);
736         if (DEV < 0)
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))
741                 check = 0;
742         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
743                 die("will not try to make filesystem on '%s'");
744         setup_tables();
745         if (check)
746                 check_blocks();
747         else if (listfile)
748                 get_list_blocks(listfile);
749 #ifdef HAVE_MINIX2
750         if (version2) {
751                 make_root_inode2();
752                 make_bad_inode2();
753         } else
754 #endif
755         {
756                 make_root_inode();
757                 make_bad_inode();
758         }
759         mark_good_blocks();
760         write_tables();
761         return 0;
762 }