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