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