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