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