9ae4b569cec82f12096a7e82c21421dd2e520022
[oweals/busybox.git] / util-linux / mkfs_minix.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mkfs.c - make a linux (minix) file-system.
4  *
5  * (C) 1991 Linus Torvalds. This file may be redistributed as per
6  * the Linux copyright.
7  */
8
9 /*
10  * DD.MM.YY
11  *
12  * 24.11.91  -  Time began. Used the fsck sources to get started.
13  *
14  * 25.11.91  -  Corrected some bugs. Added support for ".badblocks"
15  *              The algorithm for ".badblocks" is a bit weird, but
16  *              it should work. Oh, well.
17  *
18  * 25.01.92  -  Added the -l option for getting the list of bad blocks
19  *              out of a named file. (Dave Rivers, rivers@ponds.uucp)
20  *
21  * 28.02.92  -  Added %-information when using -c.
22  *
23  * 28.02.93  -  Added support for other namelengths than the original
24  *              14 characters so that I can test the new kernel routines..
25  *
26  * 09.10.93  -  Make exit status conform to that required by fsutil
27  *              (Rik Faith, faith@cs.unc.edu)
28  *
29  * 31.10.93  -  Added inode request feature, for backup floppies: use
30  *              32 inodes, for a news partition use more.
31  *              (Scott Heavner, sdh@po.cwru.edu)
32  *
33  * 03.01.94  -  Added support for file system valid flag.
34  *              (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
35  *
36  * 30.10.94 - added support for v2 filesystem
37  *            (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
38  * 
39  * 09.11.94  -  Added test to prevent overwrite of mounted fs adapted
40  *              from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
41  *              program.  (Daniel Quinlan, quinlan@yggdrasil.com)
42  *
43  * 03.20.95  -  Clear first 512 bytes of filesystem to make certain that
44  *              the filesystem is not misidentified as a MS-DOS FAT filesystem.
45  *              (Daniel Quinlan, quinlan@yggdrasil.com)
46  *
47  * 02.07.96  -  Added small patch from Russell King to make the program a
48  *              good deal more portable (janl@math.uio.no)
49  *
50  * Usage:  mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
51  *
52  *      -c for readablility checking (SLOW!)
53  *      -l for getting a list of bad blocks from a file.
54  *      -n for namelength (currently the kernel only uses 14 or 30)
55  *      -i for number of inodes
56  *      -v for v2 filesystem
57  *
58  * The device may be a block device or a image of one, but this isn't
59  * enforced (but it's not much fun on a character device :-). 
60  *
61  * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
62  *      removed getopt based parser and added a hand rolled one.
63  */
64
65 #include "internal.h"
66 #include <stdio.h>
67 #include <time.h>
68 #include <unistd.h>
69 #include <string.h>
70 #include <signal.h>
71 #include <fcntl.h>
72 #include <ctype.h>
73 #include <stdlib.h>
74 #include <termios.h>
75 #include <sys/stat.h>
76 #include <sys/ioctl.h>
77 #include <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 const char mkfs_minix_usage[] =
271         "mkfs.minix [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n"
272 #ifndef BB_FEATURE_TRIVIAL_HELP
273         "\nMake a MINIX filesystem.\n\n"
274         "Options:\n"
275         "\t-c\t\tCheck the device for bad blocks\n"
276         "\t-n [14|30]\tSpecify the maximum length of filenames\n"
277         "\t-i INODES\tSpecify the number of inodes for the filesystem\n"
278         "\t-l FILENAME\tRead the bad blocks list from FILENAME\n"
279         "\t-v\t\tMake a Minix version 2 filesystem\n\n"
280 #endif
281         ;
282
283 static volatile void show_usage() __attribute__ ((noreturn));
284 static volatile void show_usage()
285 {
286         usage(mkfs_minix_usage);
287 }
288
289 /*
290  * Check to make certain that our new filesystem won't be created on
291  * an already mounted partition.  Code adapted from mke2fs, Copyright
292  * (C) 1994 Theodore Ts'o.  Also licensed under GPL.
293  */
294 static void check_mount(void)
295 {
296         FILE *f;
297         struct mntent *mnt;
298
299         if ((f = setmntent(MOUNTED, "r")) == NULL)
300                 return;
301         while ((mnt = getmntent(f)) != NULL)
302                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
303                         break;
304         endmntent(f);
305         if (!mnt)
306                 return;
307
308         die("%s is mounted; will not make a filesystem here!");
309 }
310
311 static long valid_offset(int fd, int offset)
312 {
313         char ch;
314
315         if (lseek(fd, offset, 0) < 0)
316                 return 0;
317         if (read(fd, &ch, 1) < 1)
318                 return 0;
319         return 1;
320 }
321
322 static int count_blocks(int fd)
323 {
324         int high, low;
325
326         low = 0;
327         for (high = 1; valid_offset(fd, high); high *= 2)
328                 low = high;
329         while (low < high - 1) {
330                 const int mid = (low + high) / 2;
331
332                 if (valid_offset(fd, mid))
333                         low = mid;
334                 else
335                         high = mid;
336         }
337         valid_offset(fd, 0);
338         return (low + 1);
339 }
340
341 static int get_size(const char *file)
342 {
343         int fd;
344         long size;
345
346         fd = open(file, O_RDWR);
347         if (fd < 0) {
348                 perror(file);
349                 exit(1);
350         }
351         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
352                 close(fd);
353                 return (size * 512);
354         }
355
356         size = count_blocks(fd);
357         close(fd);
358         return size;
359 }
360
361 static void write_tables(void)
362 {
363         /* Mark the super block valid. */
364         Super.s_state |= MINIX_VALID_FS;
365         Super.s_state &= ~MINIX_ERROR_FS;
366
367         if (lseek(DEV, 0, SEEK_SET))
368                 die("seek to boot block failed in write_tables");
369         if (512 != write(DEV, boot_block_buffer, 512))
370                 die("unable to clear boot sector");
371         if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
372                 die("seek failed in write_tables");
373         if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
374                 die("unable to write super-block");
375         if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
376                 die("unable to write inode map");
377         if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
378                 die("unable to write zone map");
379         if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
380                 die("unable to write inodes");
381
382 }
383
384 static void write_block(int blk, char *buffer)
385 {
386         if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
387                 die("seek failed in write_block");
388         if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
389                 die("write failed in write_block");
390 }
391
392 static int get_free_block(void)
393 {
394         int blk;
395
396         if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
397                 die("too many bad blocks");
398         if (used_good_blocks)
399                 blk = good_blocks_table[used_good_blocks - 1] + 1;
400         else
401                 blk = FIRSTZONE;
402         while (blk < ZONES && zone_in_use(blk))
403                 blk++;
404         if (blk >= ZONES)
405                 die("not enough good blocks");
406         good_blocks_table[used_good_blocks] = blk;
407         used_good_blocks++;
408         return blk;
409 }
410
411 static void mark_good_blocks(void)
412 {
413         int blk;
414
415         for (blk = 0; blk < used_good_blocks; blk++)
416                 mark_zone(good_blocks_table[blk]);
417 }
418
419 inline int next(int zone)
420 {
421         if (!zone)
422                 zone = FIRSTZONE - 1;
423         while (++zone < ZONES)
424                 if (zone_in_use(zone))
425                         return zone;
426         return 0;
427 }
428
429 static void make_bad_inode(void)
430 {
431         struct minix_inode *inode = &Inode[MINIX_BAD_INO];
432         int i, j, zone;
433         int ind = 0, dind = 0;
434         unsigned short ind_block[BLOCK_SIZE >> 1];
435         unsigned short dind_block[BLOCK_SIZE >> 1];
436
437 #define NEXT_BAD (zone = next(zone))
438
439         if (!badblocks)
440                 return;
441         mark_inode(MINIX_BAD_INO);
442         inode->i_nlinks = 1;
443         inode->i_time = time(NULL);
444         inode->i_mode = S_IFREG + 0000;
445         inode->i_size = badblocks * BLOCK_SIZE;
446         zone = next(0);
447         for (i = 0; i < 7; i++) {
448                 inode->i_zone[i] = zone;
449                 if (!NEXT_BAD)
450                         goto end_bad;
451         }
452         inode->i_zone[7] = ind = get_free_block();
453         memset(ind_block, 0, BLOCK_SIZE);
454         for (i = 0; i < 512; i++) {
455                 ind_block[i] = zone;
456                 if (!NEXT_BAD)
457                         goto end_bad;
458         }
459         inode->i_zone[8] = dind = get_free_block();
460         memset(dind_block, 0, BLOCK_SIZE);
461         for (i = 0; i < 512; i++) {
462                 write_block(ind, (char *) ind_block);
463                 dind_block[i] = ind = get_free_block();
464                 memset(ind_block, 0, BLOCK_SIZE);
465                 for (j = 0; j < 512; j++) {
466                         ind_block[j] = zone;
467                         if (!NEXT_BAD)
468                                 goto end_bad;
469                 }
470         }
471         die("too many bad blocks");
472   end_bad:
473         if (ind)
474                 write_block(ind, (char *) ind_block);
475         if (dind)
476                 write_block(dind, (char *) dind_block);
477 }
478
479 #ifdef HAVE_MINIX2
480 static void make_bad_inode2(void)
481 {
482         struct minix2_inode *inode = &Inode2[MINIX_BAD_INO];
483         int i, j, zone;
484         int ind = 0, dind = 0;
485         unsigned long ind_block[BLOCK_SIZE >> 2];
486         unsigned long dind_block[BLOCK_SIZE >> 2];
487
488         if (!badblocks)
489                 return;
490         mark_inode(MINIX_BAD_INO);
491         inode->i_nlinks = 1;
492         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
493         inode->i_mode = S_IFREG + 0000;
494         inode->i_size = badblocks * BLOCK_SIZE;
495         zone = next(0);
496         for (i = 0; i < 7; i++) {
497                 inode->i_zone[i] = zone;
498                 if (!NEXT_BAD)
499                         goto end_bad;
500         }
501         inode->i_zone[7] = ind = get_free_block();
502         memset(ind_block, 0, BLOCK_SIZE);
503         for (i = 0; i < 256; i++) {
504                 ind_block[i] = zone;
505                 if (!NEXT_BAD)
506                         goto end_bad;
507         }
508         inode->i_zone[8] = dind = get_free_block();
509         memset(dind_block, 0, BLOCK_SIZE);
510         for (i = 0; i < 256; i++) {
511                 write_block(ind, (char *) ind_block);
512                 dind_block[i] = ind = get_free_block();
513                 memset(ind_block, 0, BLOCK_SIZE);
514                 for (j = 0; j < 256; j++) {
515                         ind_block[j] = zone;
516                         if (!NEXT_BAD)
517                                 goto end_bad;
518                 }
519         }
520         /* Could make triple indirect block here */
521         die("too many bad blocks");
522   end_bad:
523         if (ind)
524                 write_block(ind, (char *) ind_block);
525         if (dind)
526                 write_block(dind, (char *) dind_block);
527 }
528 #endif
529
530 static void make_root_inode(void)
531 {
532         struct minix_inode *inode = &Inode[MINIX_ROOT_INO];
533
534         mark_inode(MINIX_ROOT_INO);
535         inode->i_zone[0] = get_free_block();
536         inode->i_nlinks = 2;
537         inode->i_time = time(NULL);
538         if (badblocks)
539                 inode->i_size = 3 * dirsize;
540         else {
541                 root_block[2 * dirsize] = '\0';
542                 root_block[2 * dirsize + 1] = '\0';
543                 inode->i_size = 2 * dirsize;
544         }
545         inode->i_mode = S_IFDIR + 0755;
546         inode->i_uid = getuid();
547         if (inode->i_uid)
548                 inode->i_gid = getgid();
549         write_block(inode->i_zone[0], root_block);
550 }
551
552 #ifdef HAVE_MINIX2
553 static void make_root_inode2(void)
554 {
555         struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO];
556
557         mark_inode(MINIX_ROOT_INO);
558         inode->i_zone[0] = get_free_block();
559         inode->i_nlinks = 2;
560         inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL);
561         if (badblocks)
562                 inode->i_size = 3 * dirsize;
563         else {
564                 root_block[2 * dirsize] = '\0';
565                 root_block[2 * dirsize + 1] = '\0';
566                 inode->i_size = 2 * dirsize;
567         }
568         inode->i_mode = S_IFDIR + 0755;
569         inode->i_uid = getuid();
570         if (inode->i_uid)
571                 inode->i_gid = getgid();
572         write_block(inode->i_zone[0], root_block);
573 }
574 #endif
575
576 static void setup_tables(void)
577 {
578         int i;
579         unsigned long inodes;
580
581         memset(super_block_buffer, 0, BLOCK_SIZE);
582         memset(boot_block_buffer, 0, 512);
583         MAGIC = magic;
584         ZONESIZE = 0;
585         MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
586         ZONES = BLOCKS;
587 /* some magic nrs: 1 inode / 3 blocks */
588         if (req_nr_inodes == 0)
589                 inodes = BLOCKS / 3;
590         else
591                 inodes = req_nr_inodes;
592         /* Round up inode count to fill block size */
593 #ifdef HAVE_MINIX2
594         if (version2)
595                 inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) &
596                                   ~(MINIX2_INODES_PER_BLOCK - 1));
597         else
598 #endif
599                 inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) &
600                                   ~(MINIX_INODES_PER_BLOCK - 1));
601         if (inodes > 65535)
602                 inodes = 65535;
603         INODES = inodes;
604         IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK);
605         ZMAPS = 0;
606         i = 0;
607         while (ZMAPS !=
608                    UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
609                                  BITS_PER_BLOCK) && i < 1000) {
610                 ZMAPS =
611                         UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1,
612                                   BITS_PER_BLOCK);
613                 i++;
614         }
615         /* Real bad hack but overwise mkfs.minix can be thrown
616          * in infinite loop...
617          * try:
618          * dd if=/dev/zero of=test.fs count=10 bs=1024
619          * /sbin/mkfs.minix -i 200 test.fs
620          * */
621         if (i >= 999) {
622                 die("unable to allocate buffers for maps");
623         }
624         FIRSTZONE = NORM_FIRSTZONE;
625         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
626         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
627         memset(inode_map, 0xff, IMAPS * BLOCK_SIZE);
628         memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE);
629         for (i = FIRSTZONE; i < ZONES; i++)
630                 unmark_zone(i);
631         for (i = MINIX_ROOT_INO; i <= INODES; i++)
632                 unmark_inode(i);
633         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
634         memset(inode_buffer, 0, INODE_BUFFER_SIZE);
635         printf("%ld inodes\n", INODES);
636         printf("%ld blocks\n", ZONES);
637         printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
638         printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
639         printf("Maxsize=%ld\n\n", MAXSIZE);
640 }
641
642 /*
643  * Perform a test of a block; return the number of
644  * blocks readable/writeable.
645  */
646 long do_check(char *buffer, int try, unsigned int current_block)
647 {
648         long got;
649
650         /* Seek to the correct loc. */
651         if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
652                 current_block * BLOCK_SIZE) {
653                 die("seek failed during testing of blocks");
654         }
655
656
657         /* Try the read */
658         got = read(DEV, buffer, try * BLOCK_SIZE);
659         if (got < 0)
660                 got = 0;
661         if (got & (BLOCK_SIZE - 1)) {
662                 printf("Weird values in do_check: probably bugs\n");
663         }
664         got /= BLOCK_SIZE;
665         return got;
666 }
667
668 static unsigned int currently_testing = 0;
669
670 static void alarm_intr(int alnum)
671 {
672         if (currently_testing >= ZONES)
673                 return;
674         signal(SIGALRM, alarm_intr);
675         alarm(5);
676         if (!currently_testing)
677                 return;
678         printf("%d ...", currently_testing);
679         fflush(stdout);
680 }
681
682 static void check_blocks(void)
683 {
684         int try, got;
685         static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
686
687         currently_testing = 0;
688         signal(SIGALRM, alarm_intr);
689         alarm(5);
690         while (currently_testing < ZONES) {
691                 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
692                         currently_testing * BLOCK_SIZE)
693                         die("seek failed in check_blocks");
694                 try = TEST_BUFFER_BLOCKS;
695                 if (currently_testing + try > ZONES)
696                         try = ZONES - currently_testing;
697                 got = do_check(buffer, try, currently_testing);
698                 currently_testing += got;
699                 if (got == try)
700                         continue;
701                 if (currently_testing < FIRSTZONE)
702                         die("bad blocks before data-area: cannot make fs");
703                 mark_zone(currently_testing);
704                 badblocks++;
705                 currently_testing++;
706         }
707         if (badblocks > 1)
708                 printf("%d bad blocks\n", badblocks);
709         else if (badblocks == 1)
710                 printf("one bad block\n");
711 }
712
713 static void get_list_blocks(filename)
714 char *filename;
715
716 {
717         FILE *listfile;
718         unsigned long blockno;
719
720         listfile = fopen(filename, "r");
721         if (listfile == (FILE *) NULL) {
722                 die("can't open file of bad blocks");
723         }
724         while (!feof(listfile)) {
725                 fscanf(listfile, "%ld\n", &blockno);
726                 mark_zone(blockno);
727                 badblocks++;
728         }
729         if (badblocks > 1)
730                 printf("%d bad blocks\n", badblocks);
731         else if (badblocks == 1)
732                 printf("one bad block\n");
733 }
734
735 extern int mkfs_minix_main(int argc, char **argv)
736 {
737         int i=1;
738         char *tmp;
739         struct stat statbuf;
740         char *listfile = NULL;
741         int stopIt=FALSE;
742
743         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
744                 die("bad inode size");
745 #ifdef HAVE_MINIX2
746         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
747                 die("bad inode size");
748 #endif
749         
750         /* Parse options */
751         argv++;
752         while (--argc >= 0 && *argv && **argv) {
753                 if (**argv == '-') {
754                         stopIt=FALSE;
755                         while (i > 0 && *++(*argv) && stopIt==FALSE) {
756                                 switch (**argv) {
757                                         case 'c':
758                                                 check = 1;
759                                                 break;
760                                         case 'i':
761                                                 {
762                                                         char *cp=NULL;
763                                                         if (*(*argv+1) != 0) {
764                                                                 cp = ++(*argv);
765                                                         } else {
766                                                                 if (--argc == 0) {
767                                                                         goto goodbye;
768                                                                 }
769                                                                 cp = *(++argv);
770                                                         }
771                                                         req_nr_inodes = strtoul(cp, &tmp, 0);
772                                                         if (*tmp)
773                                                                 show_usage();
774                                                         stopIt=TRUE;
775                                                         break;
776                                                 }
777                                         case 'l':
778                                                 if (--argc == 0) {
779                                                         goto goodbye;
780                                                 }
781                                                 listfile = *(++argv);
782                                                 break;
783                                         case 'n':
784                                                 {
785                                                         char *cp=NULL;
786
787                                                         if (*(*argv+1) != 0) {
788                                                                 cp = ++(*argv);
789                                                         } else {
790                                                                 if (--argc == 0) {
791                                                                         goto goodbye;
792                                                                 }
793                                                                 cp = *(++argv);
794                                                         }
795                                                         i = strtoul(cp, &tmp, 0);
796                                                         if (*tmp)
797                                                                 show_usage();
798                                                         if (i == 14)
799                                                                 magic = MINIX_SUPER_MAGIC;
800                                                         else if (i == 30)
801                                                                 magic = MINIX_SUPER_MAGIC2;
802                                                         else 
803                                                                 show_usage();
804                                                         namelen = i;
805                                                         dirsize = i + 2;
806                                                         stopIt=TRUE;
807                                                         break;
808                                                 }
809                                         case 'v':
810 #ifdef HAVE_MINIX2
811                                                 version2 = 1;
812 #else
813                                                 errorMsg("%s: not compiled with minix v2 support\n",
814                                                                 device_name);
815                                                 exit(-1);
816 #endif
817                                                 break;
818                                         case '-':
819                                         case 'h':
820                                         default:
821 goodbye:
822                                                 show_usage();
823                                 }
824                         }
825                 } else {
826                         if (device_name == NULL)
827                                 device_name = *argv;
828                         else if (BLOCKS == 0)
829                                 BLOCKS = strtol(*argv, &tmp, 0);
830                         else {
831                                 goto goodbye;
832                         }
833                 }
834                 argv++;
835         }
836
837         if (device_name && !BLOCKS)
838                 BLOCKS = get_size(device_name) / 1024;
839         if (!device_name || BLOCKS < 10) {
840                 show_usage();
841         }
842 #ifdef HAVE_MINIX2
843         if (version2) {
844                 if (namelen == 14)
845                         magic = MINIX2_SUPER_MAGIC;
846                 else
847                         magic = MINIX2_SUPER_MAGIC2;
848         } else
849 #endif
850         if (BLOCKS > 65535)
851                 BLOCKS = 65535;
852         check_mount();                          /* is it already mounted? */
853         tmp = root_block;
854         *(short *) tmp = 1;
855         strcpy(tmp + 2, ".");
856         tmp += dirsize;
857         *(short *) tmp = 1;
858         strcpy(tmp + 2, "..");
859         tmp += dirsize;
860         *(short *) tmp = 2;
861         strcpy(tmp + 2, ".badblocks");
862         DEV = open(device_name, O_RDWR);
863         if (DEV < 0)
864                 die("unable to open %s");
865         if (fstat(DEV, &statbuf) < 0)
866                 die("unable to stat %s");
867         if (!S_ISBLK(statbuf.st_mode))
868                 check = 0;
869         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
870                 die("will not try to make filesystem on '%s'");
871         setup_tables();
872         if (check)
873                 check_blocks();
874         else if (listfile)
875                 get_list_blocks(listfile);
876 #ifdef HAVE_MINIX2
877         if (version2) {
878                 make_root_inode2();
879                 make_bad_inode2();
880         } else
881 #endif
882         {
883                 make_root_inode();
884                 make_bad_inode();
885         }
886         mark_good_blocks();
887         write_tables();
888         return( 0);
889
890 }