bf4bda9fd5ef7c85adce44fd5a305bfaa255593e
[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
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) (isset(inode_map,(x)))
158 #define zone_in_use(x) (isset(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",
183                         program_name);
184 #ifndef BB_FEATURE_TRIVIAL_HELP
185         fprintf(stderr, "\nMake a MINIX filesystem.\n\n");
186         fprintf(stderr, "OPTIONS:\n");
187         fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n");
188         fprintf(stderr,
189                         "\t-n [14|30]\tSpecify the maximum length of filenames\n");
190         fprintf(stderr,
191                         "\t-i\t\tSpecify the number of inodes for the filesystem\n");
192         fprintf(stderr,
193                         "\t-l FILENAME\tRead the bad blocks list from FILENAME\n");
194         fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n");
195 #endif
196         exit(16);
197 }
198
199 /*
200  * Check to make certain that our new filesystem won't be created on
201  * an already mounted partition.  Code adapted from mke2fs, Copyright
202  * (C) 1994 Theodore Ts'o.  Also licensed under GPL.
203  */
204 static void check_mount(void)
205 {
206         FILE *f;
207         struct mntent *mnt;
208
209         if ((f = setmntent(MOUNTED, "r")) == NULL)
210                 return;
211         while ((mnt = getmntent(f)) != NULL)
212                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
213                         break;
214         endmntent(f);
215         if (!mnt)
216                 return;
217
218         die("%s is mounted; will not make a filesystem here!");
219 }
220
221 static long valid_offset(int fd, int offset)
222 {
223         char ch;
224
225         if (lseek(fd, offset, 0) < 0)
226                 return 0;
227         if (read(fd, &ch, 1) < 1)
228                 return 0;
229         return 1;
230 }
231
232 static int count_blocks(int fd)
233 {
234         int high, low;
235
236         low = 0;
237         for (high = 1; valid_offset(fd, high); high *= 2)
238                 low = high;
239         while (low < high - 1) {
240                 const int mid = (low + high) / 2;
241
242                 if (valid_offset(fd, mid))
243                         low = mid;
244                 else
245                         high = mid;
246         }
247         valid_offset(fd, 0);
248         return (low + 1);
249 }
250
251 static int get_size(const char *file)
252 {
253         int fd;
254         long size;
255
256         fd = open(file, O_RDWR);
257         if (fd < 0) {
258                 perror(file);
259                 exit(1);
260         }
261         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
262                 close(fd);
263                 return (size * 512);
264         }
265
266         size = count_blocks(fd);
267         close(fd);
268         return size;
269 }
270
271 static void write_tables(void)
272 {
273         /* Mark the super block valid. */
274         Super.s_state |= MINIX_VALID_FS;
275         Super.s_state &= ~MINIX_ERROR_FS;
276
277         if (lseek(DEV, 0, SEEK_SET))
278                 die("seek to boot block failed in write_tables");
279         if (512 != write(DEV, boot_block_buffer, 512))
280                 die("unable to clear boot sector");
281         if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
282                 die("seek failed in write_tables");
283         if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
284                 die("unable to write super-block");
285         if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
286                 die("unable to write inode map");
287         if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
288                 die("unable to write zone map");
289         if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
290                 die("unable to write inodes");
291
292 }
293
294 static void write_block(int blk, char *buffer)
295 {
296         if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
297                 die("seek failed in write_block");
298         if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
299                 die("write failed in write_block");
300 }
301
302 static int get_free_block(void)
303 {
304         int blk;
305
306         if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
307                 die("too many bad blocks");
308         if (used_good_blocks)
309                 blk = good_blocks_table[used_good_blocks - 1] + 1;
310         else
311                 blk = FIRSTZONE;
312         while (blk < ZONES && zone_in_use(blk))
313                 blk++;
314         if (blk >= ZONES)
315                 die("not enough good blocks");
316         good_blocks_table[used_good_blocks] = blk;
317         used_good_blocks++;
318         return blk;
319 }
320
321 static void mark_good_blocks(void)
322 {
323         int blk;
324
325         for (blk = 0; blk < used_good_blocks; blk++)
326                 mark_zone(good_blocks_table[blk]);
327 }
328
329 inline int next(int zone)
330 {
331         if (!zone)
332                 zone = FIRSTZONE - 1;
333         while (++zone < ZONES)
334                 if (zone_in_use(zone))
335                         return zone;
336         return 0;
337 }
338
339 static void make_bad_inode(void)
340 {
341         struct minix_inode *inode = &Inode[MINIX_BAD_INO];
342         int i, j, zone;
343         int ind = 0, dind = 0;
344         unsigned short ind_block[BLOCK_SIZE >> 1];
345         unsigned short dind_block[BLOCK_SIZE >> 1];
346
347 #define NEXT_BAD (zone = next(zone))
348
349         if (!badblocks)
350                 return;
351         mark_inode(MINIX_BAD_INO);
352         inode->i_nlinks = 1;
353         inode->i_time = time(NULL);
354         inode->i_mode = S_IFREG + 0000;
355         inode->i_size = badblocks * BLOCK_SIZE;
356         zone = next(0);
357         for (i = 0; i < 7; i++) {
358                 inode->i_zone[i] = zone;
359                 if (!NEXT_BAD)
360                         goto end_bad;
361         }
362         inode->i_zone[7] = ind = get_free_block();
363         memset(ind_block, 0, BLOCK_SIZE);
364         for (i = 0; i < 512; i++) {
365                 ind_block[i] = zone;
366                 if (!NEXT_BAD)
367                         goto end_bad;
368         }
369         inode->i_zone[8] = dind = get_free_block();
370         memset(dind_block, 0, BLOCK_SIZE);
371         for (i = 0; i < 512; i++) {
372                 write_block(ind, (char *) ind_block);
373                 dind_block[i] = ind = get_free_block();
374                 memset(ind_block, 0, BLOCK_SIZE);
375                 for (j = 0; j < 512; j++) {
376                         ind_block[j] = zone;
377                         if (!NEXT_BAD)
378                                 goto end_bad;
379                 }
380         }
381         die("too many bad blocks");
382   end_bad:
383         if (ind)
384                 write_block(ind, (char *) ind_block);
385         if (dind)
386                 write_block(dind, (char *) dind_block);
387 }
388
389 #ifdef HAVE_MINIX2
390 static void make_bad_inode2(void)
391 {
392         struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
393         int i, j, zone;
394         int ind = 0, dind = 0;
395         unsigned long ind_block[BLOCK_SIZE >> 2];
396         unsigned long dind_block[BLOCK_SIZE >> 2];
397
398         if (!badblocks)
399                 return;
400         mark_inode(MINIX_BAD_INO);
401         inode->i_nlinks = 1;
402         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
403         inode->i_mode = S_IFREG + 0000;
404         inode->i_size = badblocks * BLOCK_SIZE;
405         zone = next(0);
406         for (i = 0; i < 7; i++) {
407                 inode->i_zone[i] = zone;
408                 if (!NEXT_BAD)
409                         goto end_bad;
410         }
411         inode->i_zone[7] = ind = get_free_block();
412         memset(ind_block, 0, BLOCK_SIZE);
413         for (i = 0; i < 256; i++) {
414                 ind_block[i] = zone;
415                 if (!NEXT_BAD)
416                         goto end_bad;
417         }
418         inode->i_zone[8] = dind = get_free_block();
419         memset(dind_block, 0, BLOCK_SIZE);
420         for (i = 0; i < 256; i++) {
421                 write_block(ind, (char *) ind_block);
422                 dind_block[i] = ind = get_free_block();
423                 memset(ind_block, 0, BLOCK_SIZE);
424                 for (j = 0; j < 256; j++) {
425                         ind_block[j] = zone;
426                         if (!NEXT_BAD)
427                                 goto end_bad;
428                 }
429         }
430         /* Could make triple indirect block here */
431         die("too many bad blocks");
432   end_bad:
433         if (ind)
434                 write_block(ind, (char *) ind_block);
435         if (dind)
436                 write_block(dind, (char *) dind_block);
437 }
438 #endif
439
440 static void make_root_inode(void)
441 {
442         struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
443
444         mark_inode(MINIX_ROOT_INO);
445         inode->i_zone[0] = get_free_block();
446         inode->i_nlinks = 2;
447         inode->i_time = time(NULL);
448         if (badblocks)
449                 inode->i_size = 3 * dirsize;
450         else {
451                 root_block[2 * dirsize] = '\0';
452                 root_block[2 * dirsize + 1] = '\0';
453                 inode->i_size = 2 * dirsize;
454         }
455         inode->i_mode = S_IFDIR + 0755;
456         inode->i_uid = getuid();
457         if (inode->i_uid)
458                 inode->i_gid = getgid();
459         write_block(inode->i_zone[0], root_block);
460 }
461
462 #ifdef HAVE_MINIX2
463 static void make_root_inode2(void)
464 {
465         struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
466
467         mark_inode(MINIX_ROOT_INO);
468         inode->i_zone[0] = get_free_block();
469         inode->i_nlinks = 2;
470         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
471         if (badblocks)
472                 inode->i_size = 3 * dirsize;
473         else {
474                 root_block[2 * dirsize] = '\0';
475                 root_block[2 * dirsize + 1] = '\0';
476                 inode->i_size = 2 * dirsize;
477         }
478         inode->i_mode = S_IFDIR + 0755;
479         inode->i_uid = getuid();
480         if (inode->i_uid)
481                 inode->i_gid = getgid();
482         write_block(inode->i_zone[0], root_block);
483 }
484 #endif
485
486 static void setup_tables(void)
487 {
488         int i;
489         unsigned long inodes;
490
491         memset(super_block_buffer, 0, BLOCK_SIZE);
492         memset(boot_block_buffer, 0, 512);
493         MAGIC = magic;
494         ZONESIZE = 0;
495         MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
496         ZONES = BLOCKS;
497 /* some magic nrs: 1 inode / 3 blocks */
498         if (req_nr_inodes == 0)
499                 inodes = BLOCKS / 3;
500         else
501                 inodes = req_nr_inodes;
502         /* Round up inode count to fill block size */
503 #ifdef HAVE_MINIX2
504         if (version2)
505                 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
506                                   ~(MINIX2_INODES_PER_BLOCK - 1));
507         else
508 #endif
509                 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
510                                   ~(MINIX_INODES_PER_BLOCK - 1));
511         if (inodes > 65535)
512                 inodes = 65535;
513         INODES = inodes;
514         IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
515         ZMAPS = 0;
516         i = 0;
517         while (ZMAPS !=
518                    UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
519                                  BITS_PER_BLOCK) && i < 1000) {
520                 ZMAPS =
521                         UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
522                                   BITS_PER_BLOCK);
523                 i++;
524         }
525         /* Real bad hack but overwise mkfs.minix can be thrown
526          * in infinite loop...
527          * try:
528          * dd if=/dev/zero of=test.fs count=10 bs=1024
529          * /sbin/mkfs.minix -i 200 test.fs
530          * */
531         if (i >= 999) {
532                 die("unable to allocate buffers for maps");
533         }
534         FIRSTZONE = NORM_FIRSTZONE;
535         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
536         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
537         memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
538         memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
539         for (i = FIRSTZONE; i < ZONES; i++)
540                 unmark_zone(i);
541         for (i = MINIX_ROOT_INO; i <= INODES; i++)
542                 unmark_inode(i);
543         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
544         memset(inode_buffer, 0, INODE_BUFFER_SIZE);
545         printf("%ld inodes\n", INODES);
546         printf("%ld blocks\n", ZONES);
547         printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
548         printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
549         printf("Maxsize=%ld\n\n", MAXSIZE);
550 }
551
552 /*
553  * Perform a test of a block; return the number of
554  * blocks readable/writeable.
555  */
556 long do_check(char *buffer, int try, unsigned int current_block)
557 {
558         long got;
559
560         /* Seek to the correct loc. */
561         if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
562                 current_block * BLOCK_SIZE) {
563                 die("seek failed during testing of blocks");
564         }
565
566
567         /* Try the read */
568         got = read(DEV, buffer, try * BLOCK_SIZE);
569         if (got < 0)
570                 got = 0;
571         if (got & (BLOCK_SIZE - 1)) {
572                 printf("Weird values in do_check: probably bugs\n");
573         }
574         got /= BLOCK_SIZE;
575         return got;
576 }
577
578 static unsigned int currently_testing = 0;
579
580 static void alarm_intr(int alnum)
581 {
582         if (currently_testing >= ZONES)
583                 return;
584         signal(SIGALRM, alarm_intr);
585         alarm(5);
586         if (!currently_testing)
587                 return;
588         printf("%d ...", currently_testing);
589         fflush(stdout);
590 }
591
592 static void check_blocks(void)
593 {
594         int try, got;
595         static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
596
597         currently_testing = 0;
598         signal(SIGALRM, alarm_intr);
599         alarm(5);
600         while (currently_testing < ZONES) {
601                 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
602                         currently_testing * BLOCK_SIZE)
603                         die("seek failed in check_blocks");
604                 try = TEST_BUFFER_BLOCKS;
605                 if (currently_testing + try > ZONES)
606                         try = ZONES - currently_testing;
607                 got = do_check(buffer, try, currently_testing);
608                 currently_testing += got;
609                 if (got == try)
610                         continue;
611                 if (currently_testing < FIRSTZONE)
612                         die("bad blocks before data-area: cannot make fs");
613                 mark_zone(currently_testing);
614                 badblocks++;
615                 currently_testing++;
616         }
617         if (badblocks > 1)
618                 printf("%d bad blocks\n", badblocks);
619         else if (badblocks == 1)
620                 printf("one bad block\n");
621 }
622
623 static void get_list_blocks(filename)
624 char *filename;
625
626 {
627         FILE *listfile;
628         unsigned long blockno;
629
630         listfile = fopen(filename, "r");
631         if (listfile == (FILE *) NULL) {
632                 die("can't open file of bad blocks");
633         }
634         while (!feof(listfile)) {
635                 fscanf(listfile, "%ld\n", &blockno);
636                 mark_zone(blockno);
637                 badblocks++;
638         }
639         if (badblocks > 1)
640                 printf("%d bad blocks\n", badblocks);
641         else if (badblocks == 1)
642                 printf("one bad block\n");
643 }
644
645 extern int mkfs_minix_main(int argc, char **argv)
646 {
647         int i;
648         char *tmp;
649         struct stat statbuf;
650         char *listfile = NULL;
651
652         if (argc && *argv)
653                 program_name = *argv;
654         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
655                 die("bad inode size");
656 #ifdef HAVE_MINIX2
657         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
658                 die("bad inode size");
659 #endif
660         opterr = 0;
661         while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF)
662                 switch (i) {
663                 case 'c':
664                         check = 1;
665                         break;
666                 case 'i':
667                         req_nr_inodes = (unsigned long) atol(optarg);
668                         break;
669                 case 'l':
670                         listfile = optarg;
671                         break;
672                 case 'n':
673                         i = strtoul(optarg, &tmp, 0);
674                         if (*tmp)
675                                 show_usage();
676                         if (i == 14)
677                                 magic = MINIX_SUPER_MAGIC;
678                         else if (i == 30)
679                                 magic = MINIX_SUPER_MAGIC2;
680                         else
681                                 show_usage();
682                         namelen = i;
683                         dirsize = i + 2;
684                         break;
685                 case 'v':
686 #ifdef HAVE_MINIX2
687                         version2 = 1;
688 #else
689                         fprintf(stderr, "%s: not compiled with minix v2 support\n",
690                                         program_name, device_name);
691                         exit(-1);
692 #endif
693                         break;
694                 default:
695                         show_usage();
696                 }
697         argc -= optind;
698         argv += optind;
699         if (argc > 0 && !device_name) {
700                 device_name = argv[0];
701                 argc--;
702                 argv++;
703         }
704         if (argc > 0) {
705                 BLOCKS = strtol(argv[0], &tmp, 0);
706                 if (*tmp) {
707                         printf("strtol error: number of blocks not specified");
708                         show_usage();
709                 }
710         }
711
712         if (device_name && !BLOCKS)
713                 BLOCKS = get_size(device_name) / 1024;
714         if (!device_name || BLOCKS < 10) {
715                 show_usage();
716         }
717 #ifdef HAVE_MINIX2
718         if (version2) {
719                 if (namelen == 14)
720                         magic = MINIX2_SUPER_MAGIC;
721                 else
722                         magic = MINIX2_SUPER_MAGIC2;
723         } else
724 #endif
725         if (BLOCKS > 65535)
726                 BLOCKS = 65535;
727         check_mount();                          /* is it already mounted? */
728         tmp = root_block;
729         *(short *) tmp = 1;
730         strcpy(tmp + 2, ".");
731         tmp += dirsize;
732         *(short *) tmp = 1;
733         strcpy(tmp + 2, "..");
734         tmp += dirsize;
735         *(short *) tmp = 2;
736         strcpy(tmp + 2, ".badblocks");
737         DEV = open(device_name, O_RDWR);
738         if (DEV < 0)
739                 die("unable to open %s");
740         if (fstat(DEV, &statbuf) < 0)
741                 die("unable to stat %s");
742         if (!S_ISBLK(statbuf.st_mode))
743                 check = 0;
744         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
745                 die("will not try to make filesystem on '%s'");
746         setup_tables();
747         if (check)
748                 check_blocks();
749         else if (listfile)
750                 get_list_blocks(listfile);
751 #ifdef HAVE_MINIX2
752         if (version2) {
753                 make_root_inode2();
754                 make_bad_inode2();
755         } else
756 #endif
757         {
758                 make_root_inode();
759                 make_bad_inode();
760         }
761         mark_good_blocks();
762         write_tables();
763         return 0;
764 }