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