mkfs_minix: stop using lots of bss/data.
[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 #include "minix.h"
69
70 #define DEBUG 0
71
72 /* If debugging, store the very same times/uids/gids for image consistency */
73 #if DEBUG
74 # define CUR_TIME 0
75 # define GETUID 0
76 # define GETGID 0
77 #else
78 # define CUR_TIME time(NULL)
79 # define GETUID getuid()
80 # define GETGID getgid()
81 #endif
82
83 enum {
84         MAX_GOOD_BLOCKS         = 512,
85         TEST_BUFFER_BLOCKS      = 16,
86 };
87
88 #if !ENABLE_FEATURE_MINIX2
89 enum { version2 = 0 };
90 #endif
91
92 struct globals {
93
94         int dev_fd;
95
96 #if ENABLE_FEATURE_MINIX2
97         int version2;
98 #define version2 G.version2
99 #endif
100         char *device_name;
101         uint32_t total_blocks;
102         int badblocks;
103         int namelen;
104         int dirsize;
105         int magic;
106         char *inode_buffer;
107         char *inode_map;
108         char *zone_map;
109         int used_good_blocks;
110         unsigned long req_nr_inodes;
111         unsigned currently_testing;
112
113
114         char root_block[BLOCK_SIZE];
115         char super_block_buffer[BLOCK_SIZE];
116         char boot_block_buffer[512];
117         unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
118 };
119
120 #define G (*ptr_to_globals)
121
122 static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
123 {
124         return (size + n-1) / n;
125 }
126
127 #define INODE_BUF1              (((struct minix1_inode*)G.inode_buffer) - 1)
128 #define INODE_BUF2              (((struct minix2_inode*)G.inode_buffer) - 1)
129
130 #define SB                      (*(struct minix_super_block*)G.super_block_buffer)
131
132 #define SB_INODES               (SB.s_ninodes)
133 #define SB_IMAPS                (SB.s_imap_blocks)
134 #define SB_ZMAPS                (SB.s_zmap_blocks)
135 #define SB_FIRSTZONE            (SB.s_firstdatazone)
136 #define SB_ZONE_SIZE            (SB.s_log_zone_size)
137 #define SB_MAXSIZE              (SB.s_max_size)
138 #define SB_MAGIC                (SB.s_magic)
139
140 #if !ENABLE_FEATURE_MINIX2
141 # define SB_ZONES               (SB.s_nzones)
142 # define INODE_BLOCKS           div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
143 #else
144 # define SB_ZONES               (version2 ? SB.s_zones : SB.s_nzones)
145 # define INODE_BLOCKS           div_roundup(SB_INODES, \
146                                 version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)
147 #endif
148
149 #define INODE_BUFFER_SIZE       (INODE_BLOCKS * BLOCK_SIZE)
150 #define NORM_FIRSTZONE          (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
151
152 /* Before you ask "where they come from?": */
153 /* setbit/clrbit are supplied by sys/param.h */
154
155 static int minix_bit(const char* a, unsigned i)
156 {
157           return a[i >> 3] & (1<<(i & 7));
158 }
159
160 static void minix_setbit(char *a, unsigned i)
161 {
162         setbit(a, i);
163 }
164 static void minix_clrbit(char *a, unsigned i)
165 {
166         clrbit(a, i);
167 }
168
169 /* Note: do not assume 0/1, it is 0/nonzero */
170 #define zone_in_use(x)  minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
171 /*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
172
173 #define mark_inode(x)   minix_setbit(G.inode_map,(x))
174 #define unmark_inode(x) minix_clrbit(G.inode_map,(x))
175 #define mark_zone(x)    minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
176 #define unmark_zone(x)  minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
177
178 #ifndef BLKGETSIZE
179 # define BLKGETSIZE     _IO(0x12,96)    /* return device size */
180 #endif
181
182
183 static long valid_offset(int fd, int offset)
184 {
185         char ch;
186
187         if (lseek(fd, offset, SEEK_SET) < 0)
188                 return 0;
189         if (read(fd, &ch, 1) < 1)
190                 return 0;
191         return 1;
192 }
193
194 static int count_blocks(int fd)
195 {
196         int high, low;
197
198         low = 0;
199         for (high = 1; valid_offset(fd, high); high *= 2)
200                 low = high;
201
202         while (low < high - 1) {
203                 const int mid = (low + high) / 2;
204
205                 if (valid_offset(fd, mid))
206                         low = mid;
207                 else
208                         high = mid;
209         }
210         valid_offset(fd, 0);
211         return (low + 1);
212 }
213
214 static int get_size(const char *file)
215 {
216         int fd;
217         long size;
218
219         fd = xopen(file, O_RDWR);
220         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
221                 close(fd);
222                 return (size * 512);
223         }
224
225         size = count_blocks(fd);
226         close(fd);
227         return size;
228 }
229
230 static void write_tables(void)
231 {
232         /* Mark the super block valid. */
233         SB.s_state |= MINIX_VALID_FS;
234         SB.s_state &= ~MINIX_ERROR_FS;
235
236         msg_eol = "seek to 0 failed";
237         xlseek(G.dev_fd, 0, SEEK_SET);
238
239         msg_eol = "cannot clear boot sector";
240         xwrite(G.dev_fd, G.boot_block_buffer, 512);
241
242         msg_eol = "seek to BLOCK_SIZE failed";
243         xlseek(G.dev_fd, BLOCK_SIZE, SEEK_SET);
244
245         msg_eol = "cannot write superblock";
246         xwrite(G.dev_fd, G.super_block_buffer, BLOCK_SIZE);
247
248         msg_eol = "cannot write inode map";
249         xwrite(G.dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
250
251         msg_eol = "cannot write zone map";
252         xwrite(G.dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
253
254         msg_eol = "cannot write inodes";
255         xwrite(G.dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
256
257         msg_eol = "\n";
258 }
259
260 static void write_block(int blk, char *buffer)
261 {
262         xlseek(G.dev_fd, blk * BLOCK_SIZE, SEEK_SET);
263         xwrite(G.dev_fd, buffer, BLOCK_SIZE);
264 }
265
266 static int get_free_block(void)
267 {
268         int blk;
269
270         if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
271                 bb_error_msg_and_die("too many bad blocks");
272         if (G.used_good_blocks)
273                 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
274         else
275                 blk = SB_FIRSTZONE;
276         while (blk < SB_ZONES && zone_in_use(blk))
277                 blk++;
278         if (blk >= SB_ZONES)
279                 bb_error_msg_and_die("not enough good blocks");
280         G.good_blocks_table[G.used_good_blocks] = blk;
281         G.used_good_blocks++;
282         return blk;
283 }
284
285 static void mark_good_blocks(void)
286 {
287         int blk;
288
289         for (blk = 0; blk < G.used_good_blocks; blk++)
290                 mark_zone(G.good_blocks_table[blk]);
291 }
292
293 static int next(int zone)
294 {
295         if (!zone)
296                 zone = SB_FIRSTZONE - 1;
297         while (++zone < SB_ZONES)
298                 if (zone_in_use(zone))
299                         return zone;
300         return 0;
301 }
302
303 static void make_bad_inode(void)
304 {
305         struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
306         int i, j, zone;
307         int ind = 0, dind = 0;
308         unsigned short ind_block[BLOCK_SIZE >> 1];
309         unsigned short dind_block[BLOCK_SIZE >> 1];
310
311 #define NEXT_BAD (zone = next(zone))
312
313         if (!G.badblocks)
314                 return;
315         mark_inode(MINIX_BAD_INO);
316         inode->i_nlinks = 1;
317         /* BTW, setting this makes all images different */
318         /* it's harder to check for bugs then - diff isn't helpful :(... */
319         inode->i_time = CUR_TIME;
320         inode->i_mode = S_IFREG + 0000;
321         inode->i_size = G.badblocks * BLOCK_SIZE;
322         zone = next(0);
323         for (i = 0; i < 7; i++) {
324                 inode->i_zone[i] = zone;
325                 if (!NEXT_BAD)
326                         goto end_bad;
327         }
328         inode->i_zone[7] = ind = get_free_block();
329         memset(ind_block, 0, BLOCK_SIZE);
330         for (i = 0; i < 512; i++) {
331                 ind_block[i] = zone;
332                 if (!NEXT_BAD)
333                         goto end_bad;
334         }
335         inode->i_zone[8] = dind = get_free_block();
336         memset(dind_block, 0, BLOCK_SIZE);
337         for (i = 0; i < 512; i++) {
338                 write_block(ind, (char *) ind_block);
339                 dind_block[i] = ind = get_free_block();
340                 memset(ind_block, 0, BLOCK_SIZE);
341                 for (j = 0; j < 512; j++) {
342                         ind_block[j] = zone;
343                         if (!NEXT_BAD)
344                                 goto end_bad;
345                 }
346         }
347         bb_error_msg_and_die("too many bad blocks");
348  end_bad:
349         if (ind)
350                 write_block(ind, (char *) ind_block);
351         if (dind)
352                 write_block(dind, (char *) dind_block);
353 }
354
355 #if ENABLE_FEATURE_MINIX2
356 static void make_bad_inode2(void)
357 {
358         struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
359         int i, j, zone;
360         int ind = 0, dind = 0;
361         unsigned long ind_block[BLOCK_SIZE >> 2];
362         unsigned long dind_block[BLOCK_SIZE >> 2];
363
364         if (!G.badblocks)
365                 return;
366         mark_inode(MINIX_BAD_INO);
367         inode->i_nlinks = 1;
368         inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
369         inode->i_mode = S_IFREG + 0000;
370         inode->i_size = G.badblocks * BLOCK_SIZE;
371         zone = next(0);
372         for (i = 0; i < 7; i++) {
373                 inode->i_zone[i] = zone;
374                 if (!NEXT_BAD)
375                         goto end_bad;
376         }
377         inode->i_zone[7] = ind = get_free_block();
378         memset(ind_block, 0, BLOCK_SIZE);
379         for (i = 0; i < 256; i++) {
380                 ind_block[i] = zone;
381                 if (!NEXT_BAD)
382                         goto end_bad;
383         }
384         inode->i_zone[8] = dind = get_free_block();
385         memset(dind_block, 0, BLOCK_SIZE);
386         for (i = 0; i < 256; i++) {
387                 write_block(ind, (char *) ind_block);
388                 dind_block[i] = ind = get_free_block();
389                 memset(ind_block, 0, BLOCK_SIZE);
390                 for (j = 0; j < 256; j++) {
391                         ind_block[j] = zone;
392                         if (!NEXT_BAD)
393                                 goto end_bad;
394                 }
395         }
396         /* Could make triple indirect block here */
397         bb_error_msg_and_die("too many bad blocks");
398  end_bad:
399         if (ind)
400                 write_block(ind, (char *) ind_block);
401         if (dind)
402                 write_block(dind, (char *) dind_block);
403 }
404 #else
405 void make_bad_inode2(void);
406 #endif
407
408 static void make_root_inode(void)
409 {
410         struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
411
412         mark_inode(MINIX_ROOT_INO);
413         inode->i_zone[0] = get_free_block();
414         inode->i_nlinks = 2;
415         inode->i_time = CUR_TIME;
416         if (G.badblocks)
417                 inode->i_size = 3 * G.dirsize;
418         else {
419                 G.root_block[2 * G.dirsize] = '\0';
420                 G.root_block[2 * G.dirsize + 1] = '\0';
421                 inode->i_size = 2 * G.dirsize;
422         }
423         inode->i_mode = S_IFDIR + 0755;
424         inode->i_uid = GETUID;
425         if (inode->i_uid)
426                 inode->i_gid = GETGID;
427         write_block(inode->i_zone[0], G.root_block);
428 }
429
430 #if ENABLE_FEATURE_MINIX2
431 static void make_root_inode2(void)
432 {
433         struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
434
435         mark_inode(MINIX_ROOT_INO);
436         inode->i_zone[0] = get_free_block();
437         inode->i_nlinks = 2;
438         inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
439         if (G.badblocks)
440                 inode->i_size = 3 * G.dirsize;
441         else {
442                 G.root_block[2 * G.dirsize] = '\0';
443                 G.root_block[2 * G.dirsize + 1] = '\0';
444                 inode->i_size = 2 * G.dirsize;
445         }
446         inode->i_mode = S_IFDIR + 0755;
447         inode->i_uid = GETUID;
448         if (inode->i_uid)
449                 inode->i_gid = GETGID;
450         write_block(inode->i_zone[0], G.root_block);
451 }
452 #else
453 void make_root_inode2(void);
454 #endif
455
456 /*
457  * Perform a test of a block; return the number of
458  * blocks readable.
459  */
460 static size_t do_check(char *buffer, size_t try, unsigned current_block)
461 {
462         ssize_t got;
463
464         /* Seek to the correct loc. */
465         msg_eol = "seek failed during testing of blocks";
466         xlseek(G.dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
467         msg_eol = "\n";
468
469         /* Try the read */
470         got = read(G.dev_fd, buffer, try * BLOCK_SIZE);
471         if (got < 0)
472                 got = 0;
473         try = ((size_t)got) / BLOCK_SIZE;
474
475         if (got & (BLOCK_SIZE - 1))
476                 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
477         return try;
478 }
479
480 static void alarm_intr(int alnum)
481 {
482         if (G.currently_testing >= SB_ZONES)
483                 return;
484         signal(SIGALRM, alarm_intr);
485         alarm(5);
486         if (!G.currently_testing)
487                 return;
488         printf("%d ...", G.currently_testing);
489         fflush(stdout);
490 }
491
492 static void check_blocks(void)
493 {
494         size_t try, got;
495         /* buffer[] was the biggest static in entire bbox */
496         char *buffer = xmalloc(BLOCK_SIZE * TEST_BUFFER_BLOCKS);
497
498         G.currently_testing = 0;
499         signal(SIGALRM, alarm_intr);
500         alarm(5);
501         while (G.currently_testing < SB_ZONES) {
502                 msg_eol = "seek failed in check_blocks";
503                 xlseek(G.dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
504                 msg_eol = "\n";
505                 try = TEST_BUFFER_BLOCKS;
506                 if (G.currently_testing + try > SB_ZONES)
507                         try = SB_ZONES - G.currently_testing;
508                 got = do_check(buffer, try, G.currently_testing);
509                 G.currently_testing += got;
510                 if (got == try)
511                         continue;
512                 if (G.currently_testing < SB_FIRSTZONE)
513                         bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
514                 mark_zone(G.currently_testing);
515                 G.badblocks++;
516                 G.currently_testing++;
517         }
518         alarm(0);
519         free(buffer);
520         printf("%d bad block(s)\n", G.badblocks);
521 }
522
523 static void get_list_blocks(char *filename)
524 {
525         FILE *listfile;
526         unsigned long blockno;
527
528         listfile = xfopen(filename, "r");
529         while (!feof(listfile)) {
530                 fscanf(listfile, "%ld\n", &blockno);
531                 mark_zone(blockno);
532                 G.badblocks++;
533         }
534         printf("%d bad block(s)\n", G.badblocks);
535 }
536
537 static void setup_tables(void)
538 {
539         unsigned long inodes;
540         unsigned norm_firstzone;
541         unsigned sb_zmaps;
542         unsigned i;
543
544         /* memset(G.super_block_buffer, 0, BLOCK_SIZE); */
545         /* memset(G.boot_block_buffer, 0, 512); */
546         SB_MAGIC = G.magic;
547         SB_ZONE_SIZE = 0;
548         SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
549         if (version2)
550                 SB.s_zones = G.total_blocks;
551         else
552                 SB.s_nzones = G.total_blocks;
553
554         /* some magic nrs: 1 inode / 3 blocks */
555         if (G.req_nr_inodes == 0)
556                 inodes = G.total_blocks / 3;
557         else
558                 inodes = G.req_nr_inodes;
559         /* Round up inode count to fill block size */
560         if (version2)
561                 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
562                                  ~(MINIX2_INODES_PER_BLOCK - 1);
563         else
564                 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
565                                  ~(MINIX1_INODES_PER_BLOCK - 1);
566         if (inodes > 65535)
567                 inodes = 65535;
568         SB_INODES = inodes;
569         SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
570
571         /* Real bad hack but overwise mkfs.minix can be thrown
572          * in infinite loop...
573          * try:
574          * dd if=/dev/zero of=test.fs count=10 bs=1024
575          * mkfs.minix -i 200 test.fs
576          */
577         /* This code is not insane: NORM_FIRSTZONE is not a constant,
578          * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
579         i = 999;
580         SB_ZMAPS = 0;
581         do {
582                 norm_firstzone = NORM_FIRSTZONE;
583                 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
584                 if (SB_ZMAPS == sb_zmaps) goto got_it;
585                 SB_ZMAPS = sb_zmaps;
586                 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
587         } while (--i);
588         bb_error_msg_and_die("incompatible size/inode count, try different -i N");
589  got_it:
590
591         SB_FIRSTZONE = norm_firstzone;
592         G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
593         G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
594         memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
595         memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
596         for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
597                 unmark_zone(i);
598         for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
599                 unmark_inode(i);
600         G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
601         printf("%ld inodes\n", (long)SB_INODES);
602         printf("%ld blocks\n", (long)SB_ZONES);
603         printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone);
604         printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
605         printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
606 }
607
608 int mkfs_minix_main(int argc, char **argv);
609 int mkfs_minix_main(int argc, char **argv)
610 {
611         struct mntent *mp;
612         unsigned opt;
613         char *tmp;
614         struct stat statbuf;
615         char *str_i, *str_n;
616         char *listfile = NULL;
617
618         PTR_TO_GLOBALS = xzalloc(sizeof(G));
619 /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
620         G.namelen = 30;
621         G.dirsize = 32;
622         G.magic = MINIX1_SUPER_MAGIC2;
623
624         if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
625                 bb_error_msg_and_die("bad inode size");
626 #if ENABLE_FEATURE_MINIX2
627         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
628                 bb_error_msg_and_die("bad inode size");
629 #endif
630
631         opt = getopt32(argc, argv, "ci:l:n:v", &str_i, &listfile, &str_n);
632         argv += optind;
633         //if (opt & 1) -c
634         if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
635         //if (opt & 4) -l
636         if (opt & 8) { // -n
637                 G.namelen = xatoi_u(str_n);
638                 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
639                 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
640                 else bb_show_usage();
641                 G.dirsize = G.namelen + 2;
642         }
643         if (opt & 0x10) { // -v
644 #if ENABLE_FEATURE_MINIX2
645                 version2 = 1;
646 #else
647                 bb_error_msg_and_die("not compiled with minix v2 support");
648 #endif
649         }
650
651         G.device_name = *argv++;
652         if (!G.device_name)
653                 bb_show_usage();
654         if (*argv)
655                 G.total_blocks = xatou32(*argv);
656         else
657                 G.total_blocks = get_size(G.device_name) / 1024;
658
659         if (G.total_blocks < 10)
660                 bb_error_msg_and_die("must have at least 10 blocks");
661
662         if (version2) {
663                 G.magic = MINIX2_SUPER_MAGIC2;
664                 if (G.namelen == 14)
665                         G.magic = MINIX2_SUPER_MAGIC;
666         } else if (G.total_blocks > 65535)
667                 G.total_blocks = 65535;
668
669         /* Check if it is mounted */
670         mp = find_mount_point(G.device_name, NULL);
671         if (mp && strcmp(G.device_name, mp->mnt_fsname) == 0)
672                 bb_error_msg_and_die("%s is mounted on %s; "
673                                 "refusing to make a filesystem",
674                                 G.device_name, mp->mnt_dir);
675
676         G.dev_fd = xopen(G.device_name, O_RDWR);
677         if (fstat(G.dev_fd, &statbuf) < 0)
678                 bb_error_msg_and_die("cannot stat %s", G.device_name);
679         if (!S_ISBLK(statbuf.st_mode))
680                 opt &= ~1; // clear -c (check)
681
682 /* I don't know why someone has special code to prevent mkfs.minix
683  * on IDE devices. Why IDE but not SCSI, etc?... */
684 #if 0
685         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
686                 /* what is this? */
687                 bb_error_msg_and_die("will not try "
688                         "to make filesystem on '%s'", G.device_name);
689 #endif
690
691         tmp = G.root_block;
692         *(short *) tmp = 1;
693         strcpy(tmp + 2, ".");
694         tmp += G.dirsize;
695         *(short *) tmp = 1;
696         strcpy(tmp + 2, "..");
697         tmp += G.dirsize;
698         *(short *) tmp = 2;
699         strcpy(tmp + 2, ".badblocks");
700
701         setup_tables();
702
703         if (opt & 1) // -c ?
704                 check_blocks();
705         else if (listfile)
706                 get_list_blocks(listfile);
707
708         if (version2) {
709                 make_root_inode2();
710                 make_bad_inode2();
711         } else {
712                 make_root_inode();
713                 make_bad_inode();
714         }
715
716         mark_good_blocks();
717         write_tables();
718         return 0;
719 }