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