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