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