More libc portability updates, add in the website (which has not been
[platform/upstream/busybox.git] / util-linux / 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  * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
62  *      removed getopt based parser and added a hand rolled one.
63  */
64
65 #include "internal.h"
66 #include <stdio.h>
67 #include <time.h>
68 #include <unistd.h>
69 #include <string.h>
70 #include <signal.h>
71 #include <fcntl.h>
72 #include <ctype.h>
73 #include <stdlib.h>
74 #include <termios.h>
75 #include <sys/stat.h>
76 #include <sys/ioctl.h>
77 #include <mntent.h>
78
79 #include <linux/fs.h>
80 #include <linux/minix_fs.h>
81
82 #ifdef MINIX2_SUPER_MAGIC2
83 #define HAVE_MINIX2 1
84 #endif
85
86 #ifndef __GNUC__
87 #error "needs gcc for the bitop-__asm__'s"
88 #endif
89
90 #ifndef __linux__
91 #define volatile
92 #endif
93
94 #define MINIX_ROOT_INO 1
95 #define MINIX_BAD_INO 2
96
97 #define TEST_BUFFER_BLOCKS 16
98 #define MAX_GOOD_BLOCKS 512
99
100 #define UPPER(size,n) (((size)+((n)-1))/(n))
101 #define INODE_SIZE (sizeof(struct minix_inode))
102 #ifdef HAVE_MINIX2
103 #define INODE_SIZE2 (sizeof(struct minix2_inode))
104 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
105                                     : MINIX_INODES_PER_BLOCK))
106 #else
107 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
108 #endif
109 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
110
111 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
112
113 static char *program_name = "mkfs";
114 static char *device_name = NULL;
115 static int DEV = -1;
116 static long BLOCKS = 0;
117 static int check = 0;
118 static int badblocks = 0;
119 static int namelen = 30;                /* default (changed to 30, per Linus's
120
121                                                                    suggestion, Sun Nov 21 08:05:07 1993) */
122 static int dirsize = 32;
123 static int magic = MINIX_SUPER_MAGIC2;
124 static int version2 = 0;
125
126 static char root_block[BLOCK_SIZE] = "\0";
127
128 static char *inode_buffer = NULL;
129
130 #define Inode (((struct minix_inode *) inode_buffer)-1)
131 #ifdef HAVE_MINIX2
132 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
133 #endif
134 static char super_block_buffer[BLOCK_SIZE];
135 static char boot_block_buffer[512];
136
137 #define Super (*(struct minix_super_block *)super_block_buffer)
138 #define INODES ((unsigned long)Super.s_ninodes)
139 #ifdef HAVE_MINIX2
140 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
141 #else
142 #define ZONES ((unsigned long)(Super.s_nzones))
143 #endif
144 #define IMAPS ((unsigned long)Super.s_imap_blocks)
145 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
146 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
147 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
148 #define MAXSIZE ((unsigned long)Super.s_max_size)
149 #define MAGIC (Super.s_magic)
150 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
151
152 static char *inode_map;
153 static char *zone_map;
154
155 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
156 static int used_good_blocks = 0;
157 static unsigned long req_nr_inodes = 0;
158
159 #define inode_in_use(x) (isset(inode_map,(x)))
160 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
161
162 #define mark_inode(x) (setbit(inode_map,(x)))
163 #define unmark_inode(x) (clrbit(inode_map,(x)))
164
165 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1))
166 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1))
167
168 /*
169  * Volatile to let gcc know that this doesn't return. When trying
170  * to compile this under minix, volatile gives a warning, as
171  * exit() isn't defined as volatile under minix.
172  */
173 static volatile void die(char *str)
174 {
175         fprintf(stderr, "%s: %s\n", program_name, str);
176         exit(8);
177 }
178
179 static volatile void show_usage() __attribute__ ((noreturn));
180 static volatile void show_usage()
181 {
182         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
183                         BB_VER, BB_BT);
184         fprintf(stderr,
185                         "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n",
186                         program_name);
187 #ifndef BB_FEATURE_TRIVIAL_HELP
188         fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
189         fprintf(stderr, "OPTIONS:\n");
190         fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
191         fprintf(stderr,
192                         "\t-n [14|30]\tSpecify the maximum length of filenames\n");
193         fprintf(stderr,
194                         "\t-i\t\tSpecify the number of inodes for the filesystem\n");
195         fprintf(stderr,
196                         "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
197         fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
198 #endif
199         exit(16);
200 }
201
202 /*
203  * Check to make certain that our new filesystem won't be created on
204  * an already mounted partition.  Code adapted from mke2fs, Copyright
205  * (C) 1994 Theodore Ts'o.  Also licensed under GPL.
206  */
207 static void check_mount(void)
208 {
209         FILE *f;
210         struct mntent *mnt;
211
212         if ((f = setmntent(MOUNTED, "r")) == NULL)
213                 return;
214         while ((mnt = getmntent(f)) != NULL)
215                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
216                         break;
217         endmntent(f);
218         if (!mnt)
219                 return;
220
221         die("%s is mounted; will not make a filesystem here!");
222 }
223
224 static long valid_offset(int fd, int offset)
225 {
226         char ch;
227
228         if (lseek(fd, offset, 0) < 0)
229                 return 0;
230         if (read(fd, &ch, 1) < 1)
231                 return 0;
232         return 1;
233 }
234
235 static int count_blocks(int fd)
236 {
237         int high, low;
238
239         low = 0;
240         for (high = 1; valid_offset(fd, high); high *= 2)
241                 low = high;
242         while (low < high - 1) {
243                 const int mid = (low + high) / 2;
244
245                 if (valid_offset(fd, mid))
246                         low = mid;
247                 else
248                         high = mid;
249         }
250         valid_offset(fd, 0);
251         return (low + 1);
252 }
253
254 static int get_size(const char *file)
255 {
256         int fd;
257         long size;
258
259         fd = open(file, O_RDWR);
260         if (fd < 0) {
261                 perror(file);
262                 exit(1);
263         }
264         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
265                 close(fd);
266                 return (size * 512);
267         }
268
269         size = count_blocks(fd);
270         close(fd);
271         return size;
272 }
273
274 static void write_tables(void)
275 {
276         /* Mark the super block valid. */
277         Super.s_state |= MINIX_VALID_FS;
278         Super.s_state &= ~MINIX_ERROR_FS;
279
280         if (lseek(DEV, 0, SEEK_SET))
281                 die("seek to boot block failed in write_tables");
282         if (512 != write(DEV, boot_block_buffer, 512))
283                 die("unable to clear boot sector");
284         if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
285                 die("seek failed in write_tables");
286         if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
287                 die("unable to write super-block");
288         if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
289                 die("unable to write inode map");
290         if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
291                 die("unable to write zone map");
292         if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
293                 die("unable to write inodes");
294
295 }
296
297 static void write_block(int blk, char *buffer)
298 {
299         if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
300                 die("seek failed in write_block");
301         if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
302                 die("write failed in write_block");
303 }
304
305 static int get_free_block(void)
306 {
307         int blk;
308
309         if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
310                 die("too many bad blocks");
311         if (used_good_blocks)
312                 blk = good_blocks_table[used_good_blocks - 1] + 1;
313         else
314                 blk = FIRSTZONE;
315         while (blk < ZONES && zone_in_use(blk))
316                 blk++;
317         if (blk >= ZONES)
318                 die("not enough good blocks");
319         good_blocks_table[used_good_blocks] = blk;
320         used_good_blocks++;
321         return blk;
322 }
323
324 static void mark_good_blocks(void)
325 {
326         int blk;
327
328         for (blk = 0; blk < used_good_blocks; blk++)
329                 mark_zone(good_blocks_table[blk]);
330 }
331
332 inline int next(int zone)
333 {
334         if (!zone)
335                 zone = FIRSTZONE - 1;
336         while (++zone < ZONES)
337                 if (zone_in_use(zone))
338                         return zone;
339         return 0;
340 }
341
342 static void make_bad_inode(void)
343 {
344         struct minix_inode *inode = &Inode[MINIX_BAD_INO];
345         int i, j, zone;
346         int ind = 0, dind = 0;
347         unsigned short ind_block[BLOCK_SIZE >> 1];
348         unsigned short dind_block[BLOCK_SIZE >> 1];
349
350 #define NEXT_BAD (zone = next(zone))
351
352         if (!badblocks)
353                 return;
354         mark_inode(MINIX_BAD_INO);
355         inode->i_nlinks = 1;
356         inode->i_time = time(NULL);
357         inode->i_mode = S_IFREG + 0000;
358         inode->i_size = badblocks * BLOCK_SIZE;
359         zone = next(0);
360         for (i = 0; i < 7; i++) {
361                 inode->i_zone[i] = zone;
362                 if (!NEXT_BAD)
363                         goto end_bad;
364         }
365         inode->i_zone[7] = ind = get_free_block();
366         memset(ind_block, 0, BLOCK_SIZE);
367         for (i = 0; i < 512; i++) {
368                 ind_block[i] = zone;
369                 if (!NEXT_BAD)
370                         goto end_bad;
371         }
372         inode->i_zone[8] = dind = get_free_block();
373         memset(dind_block, 0, BLOCK_SIZE);
374         for (i = 0; i < 512; i++) {
375                 write_block(ind, (char *) ind_block);
376                 dind_block[i] = ind = get_free_block();
377                 memset(ind_block, 0, BLOCK_SIZE);
378                 for (j = 0; j < 512; j++) {
379                         ind_block[j] = zone;
380                         if (!NEXT_BAD)
381                                 goto end_bad;
382                 }
383         }
384         die("too many bad blocks");
385   end_bad:
386         if (ind)
387                 write_block(ind, (char *) ind_block);
388         if (dind)
389                 write_block(dind, (char *) dind_block);
390 }
391
392 #ifdef HAVE_MINIX2
393 static void make_bad_inode2(void)
394 {
395         struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
396         int i, j, zone;
397         int ind = 0, dind = 0;
398         unsigned long ind_block[BLOCK_SIZE >> 2];
399         unsigned long dind_block[BLOCK_SIZE >> 2];
400
401         if (!badblocks)
402                 return;
403         mark_inode(MINIX_BAD_INO);
404         inode->i_nlinks = 1;
405         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
406         inode->i_mode = S_IFREG + 0000;
407         inode->i_size = badblocks * BLOCK_SIZE;
408         zone = next(0);
409         for (i = 0; i < 7; i++) {
410                 inode->i_zone[i] = zone;
411                 if (!NEXT_BAD)
412                         goto end_bad;
413         }
414         inode->i_zone[7] = ind = get_free_block();
415         memset(ind_block, 0, BLOCK_SIZE);
416         for (i = 0; i < 256; i++) {
417                 ind_block[i] = zone;
418                 if (!NEXT_BAD)
419                         goto end_bad;
420         }
421         inode->i_zone[8] = dind = get_free_block();
422         memset(dind_block, 0, BLOCK_SIZE);
423         for (i = 0; i < 256; i++) {
424                 write_block(ind, (char *) ind_block);
425                 dind_block[i] = ind = get_free_block();
426                 memset(ind_block, 0, BLOCK_SIZE);
427                 for (j = 0; j < 256; j++) {
428                         ind_block[j] = zone;
429                         if (!NEXT_BAD)
430                                 goto end_bad;
431                 }
432         }
433         /* Could make triple indirect block here */
434         die("too many bad blocks");
435   end_bad:
436         if (ind)
437                 write_block(ind, (char *) ind_block);
438         if (dind)
439                 write_block(dind, (char *) dind_block);
440 }
441 #endif
442
443 static void make_root_inode(void)
444 {
445         struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
446
447         mark_inode(MINIX_ROOT_INO);
448         inode->i_zone[0] = get_free_block();
449         inode->i_nlinks = 2;
450         inode->i_time = time(NULL);
451         if (badblocks)
452                 inode->i_size = 3 * dirsize;
453         else {
454                 root_block[2 * dirsize] = '\0';
455                 root_block[2 * dirsize + 1] = '\0';
456                 inode->i_size = 2 * dirsize;
457         }
458         inode->i_mode = S_IFDIR + 0755;
459         inode->i_uid = getuid();
460         if (inode->i_uid)
461                 inode->i_gid = getgid();
462         write_block(inode->i_zone[0], root_block);
463 }
464
465 #ifdef HAVE_MINIX2
466 static void make_root_inode2(void)
467 {
468         struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
469
470         mark_inode(MINIX_ROOT_INO);
471         inode->i_zone[0] = get_free_block();
472         inode->i_nlinks = 2;
473         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
474         if (badblocks)
475                 inode->i_size = 3 * dirsize;
476         else {
477                 root_block[2 * dirsize] = '\0';
478                 root_block[2 * dirsize + 1] = '\0';
479                 inode->i_size = 2 * dirsize;
480         }
481         inode->i_mode = S_IFDIR + 0755;
482         inode->i_uid = getuid();
483         if (inode->i_uid)
484                 inode->i_gid = getgid();
485         write_block(inode->i_zone[0], root_block);
486 }
487 #endif
488
489 static void setup_tables(void)
490 {
491         int i;
492         unsigned long inodes;
493
494         memset(super_block_buffer, 0, BLOCK_SIZE);
495         memset(boot_block_buffer, 0, 512);
496         MAGIC = magic;
497         ZONESIZE = 0;
498         MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
499         ZONES = BLOCKS;
500 /* some magic nrs: 1 inode / 3 blocks */
501         if (req_nr_inodes == 0)
502                 inodes = BLOCKS / 3;
503         else
504                 inodes = req_nr_inodes;
505         /* Round up inode count to fill block size */
506 #ifdef HAVE_MINIX2
507         if (version2)
508                 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
509                                   ~(MINIX2_INODES_PER_BLOCK - 1));
510         else
511 #endif
512                 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
513                                   ~(MINIX_INODES_PER_BLOCK - 1));
514         if (inodes > 65535)
515                 inodes = 65535;
516         INODES = inodes;
517         IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
518         ZMAPS = 0;
519         i = 0;
520         while (ZMAPS !=
521                    UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
522                                  BITS_PER_BLOCK) && i < 1000) {
523                 ZMAPS =
524                         UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
525                                   BITS_PER_BLOCK);
526                 i++;
527         }
528         /* Real bad hack but overwise mkfs.minix can be thrown
529          * in infinite loop...
530          * try:
531          * dd if=/dev/zero of=test.fs count=10 bs=1024
532          * /sbin/mkfs.minix -i 200 test.fs
533          * */
534         if (i >= 999) {
535                 die("unable to allocate buffers for maps");
536         }
537         FIRSTZONE = NORM_FIRSTZONE;
538         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
539         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
540         memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
541         memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
542         for (i = FIRSTZONE; i < ZONES; i++)
543                 unmark_zone(i);
544         for (i = MINIX_ROOT_INO; i <= INODES; i++)
545                 unmark_inode(i);
546         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
547         memset(inode_buffer, 0, INODE_BUFFER_SIZE);
548         printf("%ld inodes\n", INODES);
549         printf("%ld blocks\n", ZONES);
550         printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
551         printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
552         printf("Maxsize=%ld\n\n", MAXSIZE);
553 }
554
555 /*
556  * Perform a test of a block; return the number of
557  * blocks readable/writeable.
558  */
559 long do_check(char *buffer, int try, unsigned int current_block)
560 {
561         long got;
562
563         /* Seek to the correct loc. */
564         if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
565                 current_block * BLOCK_SIZE) {
566                 die("seek failed during testing of blocks");
567         }
568
569
570         /* Try the read */
571         got = read(DEV, buffer, try * BLOCK_SIZE);
572         if (got < 0)
573                 got = 0;
574         if (got & (BLOCK_SIZE - 1)) {
575                 printf("Weird values in do_check: probably bugs\n");
576         }
577         got /= BLOCK_SIZE;
578         return got;
579 }
580
581 static unsigned int currently_testing = 0;
582
583 static void alarm_intr(int alnum)
584 {
585         if (currently_testing >= ZONES)
586                 return;
587         signal(SIGALRM, alarm_intr);
588         alarm(5);
589         if (!currently_testing)
590                 return;
591         printf("%d ...", currently_testing);
592         fflush(stdout);
593 }
594
595 static void check_blocks(void)
596 {
597         int try, got;
598         static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
599
600         currently_testing = 0;
601         signal(SIGALRM, alarm_intr);
602         alarm(5);
603         while (currently_testing < ZONES) {
604                 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
605                         currently_testing * BLOCK_SIZE)
606                         die("seek failed in check_blocks");
607                 try = TEST_BUFFER_BLOCKS;
608                 if (currently_testing + try > ZONES)
609                         try = ZONES - currently_testing;
610                 got = do_check(buffer, try, currently_testing);
611                 currently_testing += got;
612                 if (got == try)
613                         continue;
614                 if (currently_testing < FIRSTZONE)
615                         die("bad blocks before data-area: cannot make fs");
616                 mark_zone(currently_testing);
617                 badblocks++;
618                 currently_testing++;
619         }
620         if (badblocks > 1)
621                 printf("%d bad blocks\n", badblocks);
622         else if (badblocks == 1)
623                 printf("one bad block\n");
624 }
625
626 static void get_list_blocks(filename)
627 char *filename;
628
629 {
630         FILE *listfile;
631         unsigned long blockno;
632
633         listfile = fopen(filename, "r");
634         if (listfile == (FILE *) NULL) {
635                 die("can't open file of bad blocks");
636         }
637         while (!feof(listfile)) {
638                 fscanf(listfile, "%ld\n", &blockno);
639                 mark_zone(blockno);
640                 badblocks++;
641         }
642         if (badblocks > 1)
643                 printf("%d bad blocks\n", badblocks);
644         else if (badblocks == 1)
645                 printf("one bad block\n");
646 }
647
648 extern int mkfs_minix_main(int argc, char **argv)
649 {
650         int i;
651         char *tmp;
652         struct stat statbuf;
653         char *listfile = NULL;
654         int stopIt=FALSE;
655
656         if (argc && *argv)
657                 program_name = *argv;
658         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
659                 die("bad inode size");
660 #ifdef HAVE_MINIX2
661         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
662                 die("bad inode size");
663 #endif
664         
665         /* Parse options */
666         //printf("argc='%d'  argv='%s'\n", argc, *argv);
667         argv++;
668         while (--argc >= 0 && *argv && **argv) {
669                 if (**argv == '-') {
670                         stopIt=FALSE;
671                         while (i > 0 && *++(*argv) && stopIt==FALSE) {
672                                 //printf("argc='%d'  argv='%s'\n", argc, *argv);
673                                 switch (**argv) {
674                                         case 'c':
675                                                 check = 1;
676                                                 break;
677                                         case 'i':
678                                                 if (--argc == 0) {
679                                                         goto goodbye;
680                                                 }
681                                                 req_nr_inodes = (unsigned long) atol(*(++argv));
682                                                 break;
683                                         case 'l':
684                                                 if (--argc == 0) {
685                                                         goto goodbye;
686                                                 }
687                                                 listfile = *(++argv);
688                                                 break;
689                                         case 'n':
690                                                 {
691                                                         char *cp=NULL;
692
693                                                         if (--argc == 0) {
694                                                                 goto goodbye;
695                                                         }
696                                                         if (*(*argv+1) != 0) {
697                                                                 cp = ++(*argv);
698                                                         } else {
699                                                                 cp = *(++argv);
700                                                         }
701                                                         i = strtoul(cp, &tmp, 0);
702                                                         //printf("cp='%s'   i='%d'\n", cp, i);
703                                                         if (*tmp)
704                                                                 show_usage();
705                                                         if (i == 14)
706                                                                 magic = MINIX_SUPER_MAGIC;
707                                                         else if (i == 30)
708                                                                 magic = MINIX_SUPER_MAGIC2;
709                                                         else 
710                                                                 show_usage();
711                                                         namelen = i;
712                                                         dirsize = i + 2;
713                                                         stopIt=TRUE;
714                                                         break;
715                                                 }
716                                         case 'v':
717 #ifdef HAVE_MINIX2
718                                                 version2 = 1;
719 #else
720                                                 fprintf(stderr, "%s: not compiled with minix v2 support\n",
721                                                                 program_name, device_name);
722                                                 exit(-1);
723 #endif
724                                                 break;
725                                         case '-':
726                                         case 'h':
727                                         default:
728 goodbye:
729                                                 show_usage();
730                                 }
731                         }
732                 } else {
733                         //printf("else:  argc='%d'  argv='%s'\n", argc, *argv);
734                         if (device_name == NULL)
735                                 device_name = *argv;
736                         else if (BLOCKS == 0)
737                                 BLOCKS = strtol(*argv, &tmp, 0);
738                         else {
739                                 goto goodbye;
740                         }
741                 }
742                 argv++;
743         }
744
745         if (device_name && !BLOCKS)
746                 BLOCKS = get_size(device_name) / 1024;
747         if (!device_name || BLOCKS < 10) {
748                 show_usage();
749         }
750 #ifdef HAVE_MINIX2
751         if (version2) {
752                 if (namelen == 14)
753                         magic = MINIX2_SUPER_MAGIC;
754                 else
755                         magic = MINIX2_SUPER_MAGIC2;
756         } else
757 #endif
758         if (BLOCKS > 65535)
759                 BLOCKS = 65535;
760         check_mount();                          /* is it already mounted? */
761         tmp = root_block;
762         *(short *) tmp = 1;
763         strcpy(tmp + 2, ".");
764         tmp += dirsize;
765         *(short *) tmp = 1;
766         strcpy(tmp + 2, "..");
767         tmp += dirsize;
768         *(short *) tmp = 2;
769         strcpy(tmp + 2, ".badblocks");
770         DEV = open(device_name, O_RDWR);
771         if (DEV < 0)
772                 die("unable to open %s");
773         if (fstat(DEV, &statbuf) < 0)
774                 die("unable to stat %s");
775         if (!S_ISBLK(statbuf.st_mode))
776                 check = 0;
777         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
778                 die("will not try to make filesystem on '%s'");
779         setup_tables();
780         if (check)
781                 check_blocks();
782         else if (listfile)
783                 get_list_blocks(listfile);
784 #ifdef HAVE_MINIX2
785         if (version2) {
786                 make_root_inode2();
787                 make_bad_inode2();
788         } else
789 #endif
790         {
791                 make_root_inode();
792                 make_bad_inode();
793         }
794         mark_good_blocks();
795         write_tables();
796         exit( 0);
797
798 }