blkid: add type display for btrfs
[platform/upstream/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.
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9
10 /*
11  * DD.MM.YY
12  *
13  * 24.11.91  -  Time began. Used the fsck sources to get started.
14  *
15  * 25.11.91  -  Corrected some bugs. Added support for ".badblocks"
16  *              The algorithm for ".badblocks" is a bit weird, but
17  *              it should work. Oh, well.
18  *
19  * 25.01.92  -  Added the -l option for getting the list of bad blocks
20  *              out of a named file. (Dave Rivers, rivers@ponds.uucp)
21  *
22  * 28.02.92  -  Added %-information when using -c.
23  *
24  * 28.02.93  -  Added support for other namelengths than the original
25  *              14 characters so that I can test the new kernel routines..
26  *
27  * 09.10.93  -  Make exit status conform to that required by fsutil
28  *              (Rik Faith, faith@cs.unc.edu)
29  *
30  * 31.10.93  -  Added inode request feature, for backup floppies: use
31  *              32 inodes, for a news partition use more.
32  *              (Scott Heavner, sdh@po.cwru.edu)
33  *
34  * 03.01.94  -  Added support for file system valid flag.
35  *              (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu)
36  *
37  * 30.10.94  -  added support for v2 filesystem
38  *              (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
39  *
40  * 09.11.94  -  Added test to prevent overwrite of mounted fs adapted
41  *              from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
42  *              program.  (Daniel Quinlan, quinlan@yggdrasil.com)
43  *
44  * 03.20.95  -  Clear first 512 bytes of filesystem to make certain that
45  *              the filesystem is not misidentified as a MS-DOS FAT filesystem.
46  *              (Daniel Quinlan, quinlan@yggdrasil.com)
47  *
48  * 02.07.96  -  Added small patch from Russell King to make the program a
49  *              good deal more portable (janl@math.uio.no)
50  *
51  * Usage:  mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
52  *
53  *      -c for readability checking (SLOW!)
54  *      -l for getting a list of bad blocks from a file.
55  *      -n for namelength (currently the kernel only uses 14 or 30)
56  *      -i for number of inodes
57  *      -v for v2 filesystem
58  *
59  * The device may be a block device or a image of one, but this isn't
60  * enforced (but it's not much fun on a character device :-).
61  *
62  * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
63  *      removed getopt based parser and added a hand rolled one.
64  */
65
66 //usage:#define mkfs_minix_trivial_usage
67 //usage:       "[-c | -l FILE] [-nXX] [-iXX] BLOCKDEV [KBYTES]"
68 //usage:#define mkfs_minix_full_usage "\n\n"
69 //usage:       "Make a MINIX filesystem\n"
70 //usage:     "\n        -c              Check device for bad blocks"
71 //usage:     "\n        -n [14|30]      Maximum length of filenames"
72 //usage:     "\n        -i INODES       Number of inodes for the filesystem"
73 //usage:     "\n        -l FILE         Read bad blocks list from FILE"
74 //usage:     "\n        -v              Make version 2 filesystem"
75
76 #include "libbb.h"
77 #include <mntent.h>
78
79 #include "minix.h"
80
81 /* Store the very same times/uids/gids for image consistency */
82 #if 1
83 # define CUR_TIME 0
84 # define GETUID 0
85 # define GETGID 0
86 #else
87 /* Was using this. Is it useful? NB: this will break testsuite */
88 # define CUR_TIME time(NULL)
89 # define GETUID getuid()
90 # define GETGID getgid()
91 #endif
92
93 enum {
94         MAX_GOOD_BLOCKS         = 512,
95         TEST_BUFFER_BLOCKS      = 16,
96 };
97
98 #if !ENABLE_FEATURE_MINIX2
99 enum { version2 = 0 };
100 #endif
101
102 enum { dev_fd = 3 };
103
104 struct globals {
105 #if ENABLE_FEATURE_MINIX2
106         smallint version2;
107 #define version2 G.version2
108 #endif
109         char *device_name;
110         uint32_t total_blocks;
111         int badblocks;
112         int namelen;
113         int dirsize;
114         int magic;
115         char *inode_buffer;
116         char *inode_map;
117         char *zone_map;
118         int used_good_blocks;
119         unsigned long req_nr_inodes;
120         unsigned currently_testing;
121
122         char root_block[BLOCK_SIZE];
123         char superblock_buffer[BLOCK_SIZE];
124         char boot_block_buffer[512];
125         unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
126         /* check_blocks(): buffer[] was the biggest static in entire bbox */
127         char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
128
129         unsigned short ind_block1[BLOCK_SIZE >> 1];
130         unsigned short dind_block1[BLOCK_SIZE >> 1];
131         unsigned long ind_block2[BLOCK_SIZE >> 2];
132         unsigned long dind_block2[BLOCK_SIZE >> 2];
133 };
134 #define G (*ptr_to_globals)
135 #define INIT_G() do { \
136         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
137 } while (0)
138
139 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
140 {
141         return (size + n-1) / n;
142 }
143
144 #define INODE_BUF1              (((struct minix1_inode*)G.inode_buffer) - 1)
145 #define INODE_BUF2              (((struct minix2_inode*)G.inode_buffer) - 1)
146
147 #define SB                      (*(struct minix_superblock*)G.superblock_buffer)
148
149 #define SB_INODES               (SB.s_ninodes)
150 #define SB_IMAPS                (SB.s_imap_blocks)
151 #define SB_ZMAPS                (SB.s_zmap_blocks)
152 #define SB_FIRSTZONE            (SB.s_firstdatazone)
153 #define SB_ZONE_SIZE            (SB.s_log_zone_size)
154 #define SB_MAXSIZE              (SB.s_max_size)
155 #define SB_MAGIC                (SB.s_magic)
156
157 #if !ENABLE_FEATURE_MINIX2
158 # define SB_ZONES               (SB.s_nzones)
159 # define INODE_BLOCKS           div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
160 #else
161 # define SB_ZONES               (version2 ? SB.s_zones : SB.s_nzones)
162 # define INODE_BLOCKS           div_roundup(SB_INODES, \
163                                 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
164 #endif
165
166 #define INODE_BUFFER_SIZE       (INODE_BLOCKS * BLOCK_SIZE)
167 #define NORM_FIRSTZONE          (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
168
169 /* Before you ask "where they come from?": */
170 /* setbit/clrbit are supplied by sys/param.h */
171
172 static int minix_bit(const char* a, unsigned i)
173 {
174         return a[i >> 3] & (1<<(i & 7));
175 }
176
177 static void minix_setbit(char *a, unsigned i)
178 {
179         setbit(a, i);
180 }
181 static void minix_clrbit(char *a, unsigned i)
182 {
183         clrbit(a, i);
184 }
185
186 /* Note: do not assume 0/1, it is 0/nonzero */
187 #define zone_in_use(x)  minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
188 /*#define inode_in_use(x) minix_bit(G.inode_map,(x))*/
189
190 #define mark_inode(x)   minix_setbit(G.inode_map,(x))
191 #define unmark_inode(x) minix_clrbit(G.inode_map,(x))
192 #define mark_zone(x)    minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
193 #define unmark_zone(x)  minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
194
195 #ifndef BLKGETSIZE
196 # define BLKGETSIZE     _IO(0x12,96)    /* return device size */
197 #endif
198
199
200 static long valid_offset(int fd, int offset)
201 {
202         char ch;
203
204         if (lseek(fd, offset, SEEK_SET) < 0)
205                 return 0;
206         if (read(fd, &ch, 1) < 1)
207                 return 0;
208         return 1;
209 }
210
211 static int count_blocks(int fd)
212 {
213         int high, low;
214
215         low = 0;
216         for (high = 1; valid_offset(fd, high); high *= 2)
217                 low = high;
218
219         while (low < high - 1) {
220                 const int mid = (low + high) / 2;
221
222                 if (valid_offset(fd, mid))
223                         low = mid;
224                 else
225                         high = mid;
226         }
227         valid_offset(fd, 0);
228         return (low + 1);
229 }
230
231 static int get_size(const char *file)
232 {
233         int fd;
234         long size;
235
236         fd = xopen(file, O_RDWR);
237         if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
238                 close(fd);
239                 return (size * 512);
240         }
241
242         size = count_blocks(fd);
243         close(fd);
244         return size;
245 }
246
247 static void write_tables(void)
248 {
249         /* Mark the superblock valid. */
250         SB.s_state |= MINIX_VALID_FS;
251         SB.s_state &= ~MINIX_ERROR_FS;
252
253         msg_eol = "seek to 0 failed";
254         xlseek(dev_fd, 0, SEEK_SET);
255
256         msg_eol = "can't clear boot sector";
257         xwrite(dev_fd, G.boot_block_buffer, 512);
258
259         msg_eol = "seek to BLOCK_SIZE failed";
260         xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
261
262         msg_eol = "can't write superblock";
263         xwrite(dev_fd, G.superblock_buffer, BLOCK_SIZE);
264
265         msg_eol = "can't write inode map";
266         xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
267
268         msg_eol = "can't write zone map";
269         xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
270
271         msg_eol = "can't write inodes";
272         xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
273
274         msg_eol = "\n";
275 }
276
277 static void write_block(int blk, char *buffer)
278 {
279         xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
280         xwrite(dev_fd, buffer, BLOCK_SIZE);
281 }
282
283 static int get_free_block(void)
284 {
285         int blk;
286
287         if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
288                 bb_error_msg_and_die("too many bad blocks");
289         if (G.used_good_blocks)
290                 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
291         else
292                 blk = SB_FIRSTZONE;
293         while (blk < SB_ZONES && zone_in_use(blk))
294                 blk++;
295         if (blk >= SB_ZONES)
296                 bb_error_msg_and_die("not enough good blocks");
297         G.good_blocks_table[G.used_good_blocks] = blk;
298         G.used_good_blocks++;
299         return blk;
300 }
301
302 static void mark_good_blocks(void)
303 {
304         int blk;
305
306         for (blk = 0; blk < G.used_good_blocks; blk++)
307                 mark_zone(G.good_blocks_table[blk]);
308 }
309
310 static int next(int zone)
311 {
312         if (!zone)
313                 zone = SB_FIRSTZONE - 1;
314         while (++zone < SB_ZONES)
315                 if (zone_in_use(zone))
316                         return zone;
317         return 0;
318 }
319
320 static void make_bad_inode(void)
321 {
322         struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
323         int i, j, zone;
324         int ind = 0, dind = 0;
325         /* moved to globals to reduce stack usage
326         unsigned short ind_block[BLOCK_SIZE >> 1];
327         unsigned short dind_block[BLOCK_SIZE >> 1];
328         */
329 #define ind_block (G.ind_block1)
330 #define dind_block (G.dind_block1)
331
332 #define NEXT_BAD (zone = next(zone))
333
334         if (!G.badblocks)
335                 return;
336         mark_inode(MINIX_BAD_INO);
337         inode->i_nlinks = 1;
338         /* BTW, setting this makes all images different */
339         /* it's harder to check for bugs then - diff isn't helpful :(... */
340         inode->i_time = CUR_TIME;
341         inode->i_mode = S_IFREG + 0000;
342         inode->i_size = G.badblocks * BLOCK_SIZE;
343         zone = next(0);
344         for (i = 0; i < 7; i++) {
345                 inode->i_zone[i] = zone;
346                 if (!NEXT_BAD)
347                         goto end_bad;
348         }
349         inode->i_zone[7] = ind = get_free_block();
350         memset(ind_block, 0, BLOCK_SIZE);
351         for (i = 0; i < 512; i++) {
352                 ind_block[i] = zone;
353                 if (!NEXT_BAD)
354                         goto end_bad;
355         }
356         inode->i_zone[8] = dind = get_free_block();
357         memset(dind_block, 0, BLOCK_SIZE);
358         for (i = 0; i < 512; i++) {
359                 write_block(ind, (char *) ind_block);
360                 dind_block[i] = ind = get_free_block();
361                 memset(ind_block, 0, BLOCK_SIZE);
362                 for (j = 0; j < 512; j++) {
363                         ind_block[j] = zone;
364                         if (!NEXT_BAD)
365                                 goto end_bad;
366                 }
367         }
368         bb_error_msg_and_die("too many bad blocks");
369  end_bad:
370         if (ind)
371                 write_block(ind, (char *) ind_block);
372         if (dind)
373                 write_block(dind, (char *) dind_block);
374 #undef ind_block
375 #undef dind_block
376 }
377
378 #if ENABLE_FEATURE_MINIX2
379 static void make_bad_inode2(void)
380 {
381         struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
382         int i, j, zone;
383         int ind = 0, dind = 0;
384         /* moved to globals to reduce stack usage
385         unsigned long ind_block[BLOCK_SIZE >> 2];
386         unsigned long dind_block[BLOCK_SIZE >> 2];
387         */
388 #define ind_block (G.ind_block2)
389 #define dind_block (G.dind_block2)
390
391         if (!G.badblocks)
392                 return;
393         mark_inode(MINIX_BAD_INO);
394         inode->i_nlinks = 1;
395         inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
396         inode->i_mode = S_IFREG + 0000;
397         inode->i_size = G.badblocks * BLOCK_SIZE;
398         zone = next(0);
399         for (i = 0; i < 7; i++) {
400                 inode->i_zone[i] = zone;
401                 if (!NEXT_BAD)
402                         goto end_bad;
403         }
404         inode->i_zone[7] = ind = get_free_block();
405         memset(ind_block, 0, BLOCK_SIZE);
406         for (i = 0; i < 256; i++) {
407                 ind_block[i] = zone;
408                 if (!NEXT_BAD)
409                         goto end_bad;
410         }
411         inode->i_zone[8] = dind = get_free_block();
412         memset(dind_block, 0, BLOCK_SIZE);
413         for (i = 0; i < 256; i++) {
414                 write_block(ind, (char *) ind_block);
415                 dind_block[i] = ind = get_free_block();
416                 memset(ind_block, 0, BLOCK_SIZE);
417                 for (j = 0; j < 256; j++) {
418                         ind_block[j] = zone;
419                         if (!NEXT_BAD)
420                                 goto end_bad;
421                 }
422         }
423         /* Could make triple indirect block here */
424         bb_error_msg_and_die("too many bad blocks");
425  end_bad:
426         if (ind)
427                 write_block(ind, (char *) ind_block);
428         if (dind)
429                 write_block(dind, (char *) dind_block);
430 #undef ind_block
431 #undef dind_block
432 }
433 #else
434 void make_bad_inode2(void);
435 #endif
436
437 static void make_root_inode(void)
438 {
439         struct minix1_inode *inode = &INODE_BUF1[MINIX_ROOT_INO];
440
441         mark_inode(MINIX_ROOT_INO);
442         inode->i_zone[0] = get_free_block();
443         inode->i_nlinks = 2;
444         inode->i_time = CUR_TIME;
445         if (G.badblocks)
446                 inode->i_size = 3 * G.dirsize;
447         else {
448                 G.root_block[2 * G.dirsize] = '\0';
449                 G.root_block[2 * G.dirsize + 1] = '\0';
450                 inode->i_size = 2 * G.dirsize;
451         }
452         inode->i_mode = S_IFDIR + 0755;
453         inode->i_uid = GETUID;
454         if (inode->i_uid)
455                 inode->i_gid = GETGID;
456         write_block(inode->i_zone[0], G.root_block);
457 }
458
459 #if ENABLE_FEATURE_MINIX2
460 static void make_root_inode2(void)
461 {
462         struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
463
464         mark_inode(MINIX_ROOT_INO);
465         inode->i_zone[0] = get_free_block();
466         inode->i_nlinks = 2;
467         inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
468         if (G.badblocks)
469                 inode->i_size = 3 * G.dirsize;
470         else {
471                 G.root_block[2 * G.dirsize] = '\0';
472                 G.root_block[2 * G.dirsize + 1] = '\0';
473                 inode->i_size = 2 * G.dirsize;
474         }
475         inode->i_mode = S_IFDIR + 0755;
476         inode->i_uid = GETUID;
477         if (inode->i_uid)
478                 inode->i_gid = GETGID;
479         write_block(inode->i_zone[0], G.root_block);
480 }
481 #else
482 void make_root_inode2(void);
483 #endif
484
485 /*
486  * Perform a test of a block; return the number of
487  * blocks readable.
488  */
489 static size_t do_check(char *buffer, size_t try, unsigned current_block)
490 {
491         ssize_t got;
492
493         /* Seek to the correct loc. */
494         msg_eol = "seek failed during testing of blocks";
495         xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
496         msg_eol = "\n";
497
498         /* Try the read */
499         got = read(dev_fd, buffer, try * BLOCK_SIZE);
500         if (got < 0)
501                 got = 0;
502         try = ((size_t)got) / BLOCK_SIZE;
503
504         if (got & (BLOCK_SIZE - 1))
505                 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
506         return try;
507 }
508
509 static void alarm_intr(int alnum UNUSED_PARAM)
510 {
511         if (G.currently_testing >= SB_ZONES)
512                 return;
513         signal(SIGALRM, alarm_intr);
514         alarm(5);
515         if (!G.currently_testing)
516                 return;
517         printf("%d ...", G.currently_testing);
518         fflush_all();
519 }
520
521 static void check_blocks(void)
522 {
523         size_t try, got;
524
525         G.currently_testing = 0;
526         signal(SIGALRM, alarm_intr);
527         alarm(5);
528         while (G.currently_testing < SB_ZONES) {
529                 msg_eol = "seek failed in check_blocks";
530                 xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
531                 msg_eol = "\n";
532                 try = TEST_BUFFER_BLOCKS;
533                 if (G.currently_testing + try > SB_ZONES)
534                         try = SB_ZONES - G.currently_testing;
535                 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
536                 G.currently_testing += got;
537                 if (got == try)
538                         continue;
539                 if (G.currently_testing < SB_FIRSTZONE)
540                         bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
541                 mark_zone(G.currently_testing);
542                 G.badblocks++;
543                 G.currently_testing++;
544         }
545         alarm(0);
546         printf("%d bad block(s)\n", G.badblocks);
547 }
548
549 static void get_list_blocks(char *filename)
550 {
551         FILE *listfile;
552         unsigned long blockno;
553
554         listfile = xfopen_for_read(filename);
555         while (!feof(listfile)) {
556                 fscanf(listfile, "%ld\n", &blockno);
557                 mark_zone(blockno);
558                 G.badblocks++;
559         }
560         printf("%d bad block(s)\n", G.badblocks);
561 }
562
563 static void setup_tables(void)
564 {
565         unsigned long inodes;
566         unsigned norm_firstzone;
567         unsigned sb_zmaps;
568         unsigned i;
569
570         /* memset(G.superblock_buffer, 0, BLOCK_SIZE); */
571         /* memset(G.boot_block_buffer, 0, 512); */
572         SB_MAGIC = G.magic;
573         SB_ZONE_SIZE = 0;
574         SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
575         if (version2)
576                 SB.s_zones = G.total_blocks;
577         else
578                 SB.s_nzones = G.total_blocks;
579
580         /* some magic nrs: 1 inode / 3 blocks */
581         if (G.req_nr_inodes == 0)
582                 inodes = G.total_blocks / 3;
583         else
584                 inodes = G.req_nr_inodes;
585         /* Round up inode count to fill block size */
586         if (version2)
587                 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
588                                  ~(MINIX2_INODES_PER_BLOCK - 1);
589         else
590                 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
591                                  ~(MINIX1_INODES_PER_BLOCK - 1);
592         if (inodes > 65535)
593                 inodes = 65535;
594         SB_INODES = inodes;
595         SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
596
597         /* Real bad hack but overwise mkfs.minix can be thrown
598          * in infinite loop...
599          * try:
600          * dd if=/dev/zero of=test.fs count=10 bs=1024
601          * mkfs.minix -i 200 test.fs
602          */
603         /* This code is not insane: NORM_FIRSTZONE is not a constant,
604          * it is calculated from SB_INODES, SB_IMAPS and SB_ZMAPS */
605         i = 999;
606         SB_ZMAPS = 0;
607         do {
608                 norm_firstzone = NORM_FIRSTZONE;
609                 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
610                 if (SB_ZMAPS == sb_zmaps) goto got_it;
611                 SB_ZMAPS = sb_zmaps;
612                 /* new SB_ZMAPS, need to recalc NORM_FIRSTZONE */
613         } while (--i);
614         bb_error_msg_and_die("incompatible size/inode count, try different -i N");
615  got_it:
616
617         SB_FIRSTZONE = norm_firstzone;
618         G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
619         G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
620         memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
621         memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
622         for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
623                 unmark_zone(i);
624         for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
625                 unmark_inode(i);
626         G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
627         printf("%ld inodes\n", (long)SB_INODES);
628         printf("%ld blocks\n", (long)SB_ZONES);
629         printf("Firstdatazone=%ld (%ld)\n", (long)SB_FIRSTZONE, (long)norm_firstzone);
630         printf("Zonesize=%d\n", BLOCK_SIZE << SB_ZONE_SIZE);
631         printf("Maxsize=%ld\n", (long)SB_MAXSIZE);
632 }
633
634 int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
635 int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
636 {
637         unsigned opt;
638         char *tmp;
639         struct stat statbuf;
640         char *str_i;
641         char *listfile = NULL;
642
643         INIT_G();
644 /* default (changed to 30, per Linus's suggestion, Sun Nov 21 08:05:07 1993) */
645         G.namelen = 30;
646         G.dirsize = 32;
647         G.magic = MINIX1_SUPER_MAGIC2;
648
649         if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
650                 bb_error_msg_and_die("bad inode size");
651 #if ENABLE_FEATURE_MINIX2
652         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
653                 bb_error_msg_and_die("bad inode size");
654 #endif
655
656         opt_complementary = "n+"; /* -n N */
657         opt = getopt32(argv, "ci:l:n:v", &str_i, &listfile, &G.namelen);
658         argv += optind;
659         //if (opt & 1) -c
660         if (opt & 2) G.req_nr_inodes = xatoul(str_i); // -i
661         //if (opt & 4) -l
662         if (opt & 8) { // -n
663                 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
664                 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
665                 else bb_show_usage();
666                 G.dirsize = G.namelen + 2;
667         }
668         if (opt & 0x10) { // -v
669 #if ENABLE_FEATURE_MINIX2
670                 version2 = 1;
671 #else
672                 bb_error_msg_and_die("not compiled with minix v2 support");
673 #endif
674         }
675
676         G.device_name = *argv++;
677         if (!G.device_name)
678                 bb_show_usage();
679         if (*argv)
680                 G.total_blocks = xatou32(*argv);
681         else
682                 G.total_blocks = get_size(G.device_name) / 1024;
683
684         if (G.total_blocks < 10)
685                 bb_error_msg_and_die("must have at least 10 blocks");
686
687         if (version2) {
688                 G.magic = MINIX2_SUPER_MAGIC2;
689                 if (G.namelen == 14)
690                         G.magic = MINIX2_SUPER_MAGIC;
691         } else if (G.total_blocks > 65535)
692                 G.total_blocks = 65535;
693
694         /* Check if it is mounted */
695         if (find_mount_point(G.device_name, 0))
696                 bb_error_msg_and_die("can't format mounted filesystem");
697
698         xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
699         xfstat(dev_fd, &statbuf, G.device_name);
700         if (!S_ISBLK(statbuf.st_mode))
701                 opt &= ~1; // clear -c (check)
702
703 /* I don't know why someone has special code to prevent mkfs.minix
704  * on IDE devices. Why IDE but not SCSI, etc?... */
705 #if 0
706         else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
707                 /* what is this? */
708                 bb_error_msg_and_die("will not try "
709                         "to make filesystem on '%s'", G.device_name);
710 #endif
711
712         tmp = G.root_block;
713         *(short *) tmp = 1;
714         strcpy(tmp + 2, ".");
715         tmp += G.dirsize;
716         *(short *) tmp = 1;
717         strcpy(tmp + 2, "..");
718         tmp += G.dirsize;
719         *(short *) tmp = 2;
720         strcpy(tmp + 2, ".badblocks");
721
722         setup_tables();
723
724         if (opt & 1) // -c ?
725                 check_blocks();
726         else if (listfile)
727                 get_list_blocks(listfile);
728
729         if (version2) {
730                 make_root_inode2();
731                 make_bad_inode2();
732         } else {
733                 make_root_inode();
734                 make_bad_inode();
735         }
736
737         mark_good_blocks();
738         write_tables();
739         return 0;
740 }