1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98#include "libbb.h"
99#include <mntent.h>
100
101#include "minix.h"
102
103
104#if 1
105# define CUR_TIME 0
106# define GETUID 0
107# define GETGID 0
108#else
109
110# define CUR_TIME time(NULL)
111# define GETUID getuid()
112# define GETGID getgid()
113#endif
114
115enum {
116 MAX_GOOD_BLOCKS = 512,
117 TEST_BUFFER_BLOCKS = 16,
118};
119
120#if !ENABLE_FEATURE_MINIX2
121enum { version2 = 0 };
122#endif
123
124enum { dev_fd = 3 };
125
126struct globals {
127#if ENABLE_FEATURE_MINIX2
128 smallint version2;
129#define version2 G.version2
130#endif
131 char *device_name;
132 uint32_t total_blocks;
133 int badblocks;
134 int namelen;
135 int dirsize;
136 int magic;
137 char *inode_buffer;
138 char *inode_map;
139 char *zone_map;
140 int used_good_blocks;
141 unsigned long req_nr_inodes;
142 unsigned currently_testing;
143
144 char root_block[BLOCK_SIZE];
145 union {
146 char superblock_buffer[BLOCK_SIZE];
147 struct minix_superblock SB;
148 } u;
149 char boot_block_buffer[512];
150 unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
151
152 char check_blocks_buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
153
154 unsigned short ind_block1[BLOCK_SIZE >> 1];
155 unsigned short dind_block1[BLOCK_SIZE >> 1];
156 unsigned long ind_block2[BLOCK_SIZE >> 2];
157 unsigned long dind_block2[BLOCK_SIZE >> 2];
158};
159#define G (*ptr_to_globals)
160#define INIT_G() do { \
161 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
162} while (0)
163
164static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
165{
166 return (size + n-1) / n;
167}
168
169#define INODE_BUF1 (((struct minix1_inode*)G.inode_buffer) - 1)
170#define INODE_BUF2 (((struct minix2_inode*)G.inode_buffer) - 1)
171
172#define SB (G.u.SB)
173
174#define SB_INODES (SB.s_ninodes)
175#define SB_IMAPS (SB.s_imap_blocks)
176#define SB_ZMAPS (SB.s_zmap_blocks)
177#define SB_FIRSTZONE (SB.s_firstdatazone)
178#define SB_ZONE_SIZE (SB.s_log_zone_size)
179#define SB_MAXSIZE (SB.s_max_size)
180#define SB_MAGIC (SB.s_magic)
181
182#if !ENABLE_FEATURE_MINIX2
183# define SB_ZONES (SB.s_nzones)
184# define INODE_BLOCKS div_roundup(SB_INODES, MINIX1_INODES_PER_BLOCK)
185#else
186# define SB_ZONES (version2 ? SB.s_zones : SB.s_nzones)
187# define INODE_BLOCKS div_roundup(SB_INODES, \
188 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
189#endif
190
191#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
192#define NORM_FIRSTZONE (2 + SB_IMAPS + SB_ZMAPS + INODE_BLOCKS)
193
194
195
196
197static int minix_bit(const char* a, unsigned i)
198{
199 return a[i >> 3] & (1<<(i & 7));
200}
201
202static void minix_setbit(char *a, unsigned i)
203{
204 setbit(a, i);
205}
206static void minix_clrbit(char *a, unsigned i)
207{
208 clrbit(a, i);
209}
210
211
212#define zone_in_use(x) minix_bit(G.zone_map,(x)-SB_FIRSTZONE+1)
213
214
215#define mark_inode(x) minix_setbit(G.inode_map,(x))
216#define unmark_inode(x) minix_clrbit(G.inode_map,(x))
217#define mark_zone(x) minix_setbit(G.zone_map,(x)-SB_FIRSTZONE+1)
218#define unmark_zone(x) minix_clrbit(G.zone_map,(x)-SB_FIRSTZONE+1)
219
220#ifndef BLKGETSIZE
221# define BLKGETSIZE _IO(0x12,96)
222#endif
223
224static void write_tables(void)
225{
226
227 SB.s_state |= MINIX_VALID_FS;
228 SB.s_state &= ~MINIX_ERROR_FS;
229
230 msg_eol = "seek to 0 failed";
231 xlseek(dev_fd, 0, SEEK_SET);
232
233 msg_eol = "can't clear boot sector";
234 xwrite(dev_fd, G.boot_block_buffer, 512);
235
236 msg_eol = "seek to BLOCK_SIZE failed";
237 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
238
239 msg_eol = "can't write superblock";
240 xwrite(dev_fd, G.u.superblock_buffer, BLOCK_SIZE);
241
242 msg_eol = "can't write inode map";
243 xwrite(dev_fd, G.inode_map, SB_IMAPS * BLOCK_SIZE);
244
245 msg_eol = "can't write zone map";
246 xwrite(dev_fd, G.zone_map, SB_ZMAPS * BLOCK_SIZE);
247
248 msg_eol = "can't write inodes";
249 xwrite(dev_fd, G.inode_buffer, INODE_BUFFER_SIZE);
250
251 msg_eol = "\n";
252}
253
254static void write_block(int blk, char *buffer)
255{
256 xlseek(dev_fd, blk * BLOCK_SIZE, SEEK_SET);
257 xwrite(dev_fd, buffer, BLOCK_SIZE);
258}
259
260static int get_free_block(void)
261{
262 int blk;
263
264 if (G.used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
265 bb_error_msg_and_die("too many bad blocks");
266 if (G.used_good_blocks)
267 blk = G.good_blocks_table[G.used_good_blocks - 1] + 1;
268 else
269 blk = SB_FIRSTZONE;
270 while (blk < SB_ZONES && zone_in_use(blk))
271 blk++;
272 if (blk >= SB_ZONES)
273 bb_error_msg_and_die("not enough good blocks");
274 G.good_blocks_table[G.used_good_blocks] = blk;
275 G.used_good_blocks++;
276 return blk;
277}
278
279static void mark_good_blocks(void)
280{
281 int blk;
282
283 for (blk = 0; blk < G.used_good_blocks; blk++)
284 mark_zone(G.good_blocks_table[blk]);
285}
286
287static int next(int zone)
288{
289 if (!zone)
290 zone = SB_FIRSTZONE - 1;
291 while (++zone < SB_ZONES)
292 if (zone_in_use(zone))
293 return zone;
294 return 0;
295}
296
297static void make_bad_inode(void)
298{
299 struct minix1_inode *inode = &INODE_BUF1[MINIX_BAD_INO];
300 int i, j, zone;
301 int ind = 0, dind = 0;
302
303
304
305
306#define ind_block (G.ind_block1)
307#define dind_block (G.dind_block1)
308
309#define NEXT_BAD (zone = next(zone))
310
311 if (!G.badblocks)
312 return;
313 mark_inode(MINIX_BAD_INO);
314 inode->i_nlinks = 1;
315
316
317 inode->i_time = CUR_TIME;
318 inode->i_mode = S_IFREG + 0000;
319 inode->i_size = G.badblocks * BLOCK_SIZE;
320 zone = next(0);
321 for (i = 0; i < 7; i++) {
322 inode->i_zone[i] = zone;
323 if (!NEXT_BAD)
324 goto end_bad;
325 }
326 inode->i_zone[7] = ind = get_free_block();
327 memset(ind_block, 0, BLOCK_SIZE);
328 for (i = 0; i < 512; i++) {
329 ind_block[i] = zone;
330 if (!NEXT_BAD)
331 goto end_bad;
332 }
333 inode->i_zone[8] = dind = get_free_block();
334 memset(dind_block, 0, BLOCK_SIZE);
335 for (i = 0; i < 512; i++) {
336 write_block(ind, (char *) ind_block);
337 dind_block[i] = ind = get_free_block();
338 memset(ind_block, 0, BLOCK_SIZE);
339 for (j = 0; j < 512; j++) {
340 ind_block[j] = zone;
341 if (!NEXT_BAD)
342 goto end_bad;
343 }
344 }
345 bb_error_msg_and_die("too many bad blocks");
346 end_bad:
347 if (ind)
348 write_block(ind, (char *) ind_block);
349 if (dind)
350 write_block(dind, (char *) dind_block);
351#undef ind_block
352#undef dind_block
353}
354
355#if ENABLE_FEATURE_MINIX2
356static void make_bad_inode2(void)
357{
358 struct minix2_inode *inode = &INODE_BUF2[MINIX_BAD_INO];
359 int i, j, zone;
360 int ind = 0, dind = 0;
361
362
363
364
365#define ind_block (G.ind_block2)
366#define dind_block (G.dind_block2)
367
368 if (!G.badblocks)
369 return;
370 mark_inode(MINIX_BAD_INO);
371 inode->i_nlinks = 1;
372 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
373 inode->i_mode = S_IFREG + 0000;
374 inode->i_size = G.badblocks * BLOCK_SIZE;
375 zone = next(0);
376 for (i = 0; i < 7; i++) {
377 inode->i_zone[i] = zone;
378 if (!NEXT_BAD)
379 goto end_bad;
380 }
381 inode->i_zone[7] = ind = get_free_block();
382 memset(ind_block, 0, BLOCK_SIZE);
383 for (i = 0; i < 256; i++) {
384 ind_block[i] = zone;
385 if (!NEXT_BAD)
386 goto end_bad;
387 }
388 inode->i_zone[8] = dind = get_free_block();
389 memset(dind_block, 0, BLOCK_SIZE);
390 for (i = 0; i < 256; i++) {
391 write_block(ind, (char *) ind_block);
392 dind_block[i] = ind = get_free_block();
393 memset(ind_block, 0, BLOCK_SIZE);
394 for (j = 0; j < 256; j++) {
395 ind_block[j] = zone;
396 if (!NEXT_BAD)
397 goto end_bad;
398 }
399 }
400
401 bb_error_msg_and_die("too many bad blocks");
402 end_bad:
403 if (ind)
404 write_block(ind, (char *) ind_block);
405 if (dind)
406 write_block(dind, (char *) dind_block);
407#undef ind_block
408#undef dind_block
409}
410#else
411void make_bad_inode2(void);
412#endif
413
414static void make_root_inode(void)
415{
416 struct minix1_inode *inode = &INODE_BUF1[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_time = CUR_TIME;
422 if (G.badblocks)
423 inode->i_size = 3 * G.dirsize;
424 else {
425 G.root_block[2 * G.dirsize] = '\0';
426 G.root_block[2 * G.dirsize + 1] = '\0';
427 inode->i_size = 2 * G.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], G.root_block);
434}
435
436#if ENABLE_FEATURE_MINIX2
437static void make_root_inode2(void)
438{
439 struct minix2_inode *inode = &INODE_BUF2[MINIX_ROOT_INO];
440
441 mark_inode(MINIX_ROOT_INO);
442 inode->i_zone[0] = get_free_block();
443 inode->i_nlinks = 2;
444 inode->i_atime = inode->i_mtime = inode->i_ctime = CUR_TIME;
445 if (G.badblocks)
446 inode->i_size = 3 * G.dirsize;
447 else {
448 G.root_block[2 * G.dirsize] = '\0';
449 G.root_block[2 * G.dirsize + 1] = '\0';
450 inode->i_size = 2 * G.dirsize;
451 }
452 inode->i_mode = S_IFDIR + 0755;
453 inode->i_uid = GETUID;
454 if (inode->i_uid)
455 inode->i_gid = GETGID;
456 write_block(inode->i_zone[0], G.root_block);
457}
458#else
459void make_root_inode2(void);
460#endif
461
462
463
464
465
466static size_t do_check(char *buffer, size_t try, unsigned current_block)
467{
468 ssize_t got;
469
470
471 msg_eol = "seek failed during testing of blocks";
472 xlseek(dev_fd, current_block * BLOCK_SIZE, SEEK_SET);
473 msg_eol = "\n";
474
475
476 got = read(dev_fd, buffer, try * BLOCK_SIZE);
477 if (got < 0)
478 got = 0;
479 try = ((size_t)got) / BLOCK_SIZE;
480
481 if (got & (BLOCK_SIZE - 1))
482 fprintf(stderr, "Short read at block %u\n", (unsigned)(current_block + try));
483 return try;
484}
485
486static void alarm_intr(int alnum UNUSED_PARAM)
487{
488 if (G.currently_testing >= SB_ZONES)
489 return;
490 signal(SIGALRM, alarm_intr);
491 alarm(5);
492 if (!G.currently_testing)
493 return;
494 printf("%d ...", G.currently_testing);
495 fflush_all();
496}
497
498static void check_blocks(void)
499{
500 size_t try, got;
501
502 G.currently_testing = 0;
503 signal(SIGALRM, alarm_intr);
504 alarm(5);
505 while (G.currently_testing < SB_ZONES) {
506 msg_eol = "seek failed in check_blocks";
507 xlseek(dev_fd, G.currently_testing * BLOCK_SIZE, SEEK_SET);
508 msg_eol = "\n";
509 try = TEST_BUFFER_BLOCKS;
510 if (G.currently_testing + try > SB_ZONES)
511 try = SB_ZONES - G.currently_testing;
512 got = do_check(G.check_blocks_buffer, try, G.currently_testing);
513 G.currently_testing += got;
514 if (got == try)
515 continue;
516 if (G.currently_testing < SB_FIRSTZONE)
517 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
518 mark_zone(G.currently_testing);
519 G.badblocks++;
520 G.currently_testing++;
521 }
522 alarm(0);
523 printf("%d bad block(s)\n", G.badblocks);
524}
525
526static void get_list_blocks(char *filename)
527{
528 FILE *listfile;
529 unsigned long blockno;
530
531 listfile = xfopen_for_read(filename);
532 while (!feof(listfile)) {
533 fscanf(listfile, "%lu\n", &blockno);
534 mark_zone(blockno);
535 G.badblocks++;
536 }
537 printf("%d bad block(s)\n", G.badblocks);
538}
539
540static void setup_tables(void)
541{
542 unsigned long inodes;
543 unsigned norm_firstzone;
544 unsigned sb_zmaps;
545 unsigned i;
546
547
548
549 SB_MAGIC = G.magic;
550 SB_ZONE_SIZE = 0;
551 SB_MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
552 if (version2)
553 SB.s_zones = G.total_blocks;
554 else
555 SB.s_nzones = G.total_blocks;
556
557
558 if (G.req_nr_inodes == 0)
559 inodes = G.total_blocks / 3;
560 else
561 inodes = G.req_nr_inodes;
562
563 if (version2)
564 inodes = (inodes + MINIX2_INODES_PER_BLOCK - 1) &
565 ~(MINIX2_INODES_PER_BLOCK - 1);
566 else
567 inodes = (inodes + MINIX1_INODES_PER_BLOCK - 1) &
568 ~(MINIX1_INODES_PER_BLOCK - 1);
569 if (inodes > 65535)
570 inodes = 65535;
571 SB_INODES = inodes;
572 SB_IMAPS = div_roundup(SB_INODES + 1, BITS_PER_BLOCK);
573
574
575
576
577
578
579
580
581
582 i = 999;
583 SB_ZMAPS = 0;
584 do {
585 norm_firstzone = NORM_FIRSTZONE;
586 sb_zmaps = div_roundup(G.total_blocks - norm_firstzone + 1, BITS_PER_BLOCK);
587 if (SB_ZMAPS == sb_zmaps) goto got_it;
588 SB_ZMAPS = sb_zmaps;
589
590 } while (--i);
591 bb_error_msg_and_die("incompatible size/inode count, try different -i N");
592 got_it:
593
594 SB_FIRSTZONE = norm_firstzone;
595 G.inode_map = xmalloc(SB_IMAPS * BLOCK_SIZE);
596 G.zone_map = xmalloc(SB_ZMAPS * BLOCK_SIZE);
597 memset(G.inode_map, 0xff, SB_IMAPS * BLOCK_SIZE);
598 memset(G.zone_map, 0xff, SB_ZMAPS * BLOCK_SIZE);
599 for (i = SB_FIRSTZONE; i < SB_ZONES; i++)
600 unmark_zone(i);
601 for (i = MINIX_ROOT_INO; i <= SB_INODES; i++)
602 unmark_inode(i);
603 G.inode_buffer = xzalloc(INODE_BUFFER_SIZE);
604 printf("%lu inodes\n", (unsigned long)SB_INODES);
605 printf("%lu blocks\n", (unsigned long)SB_ZONES);
606 printf("Firstdatazone=%lu (%lu)\n", (unsigned long)SB_FIRSTZONE, (unsigned long)norm_firstzone);
607 printf("Zonesize=%u\n", BLOCK_SIZE << SB_ZONE_SIZE);
608 printf("Maxsize=%lu\n", (unsigned long)SB_MAXSIZE);
609}
610
611int mkfs_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
612int mkfs_minix_main(int argc UNUSED_PARAM, char **argv)
613{
614 unsigned opt;
615 char *tmp;
616 char *str_i;
617 char *listfile = NULL;
618
619 INIT_G();
620
621 G.namelen = 30;
622 G.dirsize = 32;
623 G.magic = MINIX1_SUPER_MAGIC2;
624
625 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
626 bb_error_msg_and_die("bad inode size");
627#if ENABLE_FEATURE_MINIX2
628 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
629 bb_error_msg_and_die("bad inode size");
630#endif
631
632 opt = getopt32(argv, "ci:l:n:+v", &str_i, &listfile, &G.namelen);
633 argv += optind;
634
635 if (opt & 2) G.req_nr_inodes = xatoul(str_i);
636
637 if (opt & 8) {
638 if (G.namelen == 14) G.magic = MINIX1_SUPER_MAGIC;
639 else if (G.namelen == 30) G.magic = MINIX1_SUPER_MAGIC2;
640 else bb_show_usage();
641 G.dirsize = G.namelen + 2;
642 }
643 if (opt & 0x10) {
644#if ENABLE_FEATURE_MINIX2
645 version2 = 1;
646#else
647 bb_error_msg_and_die("not compiled with minix v2 support");
648#endif
649 }
650
651 G.device_name = argv[0];
652 if (!G.device_name)
653 bb_show_usage();
654
655
656 if (find_mount_point(G.device_name, 0))
657 bb_error_msg_and_die("can't format mounted filesystem");
658
659 xmove_fd(xopen(G.device_name, O_RDWR), dev_fd);
660
661 G.total_blocks = get_volume_size_in_bytes(dev_fd, argv[1], 1024, 1) / 1024;
662
663 if (G.total_blocks < 10)
664 bb_error_msg_and_die("must have at least 10 blocks");
665
666 if (version2) {
667 G.magic = MINIX2_SUPER_MAGIC2;
668 if (G.namelen == 14)
669 G.magic = MINIX2_SUPER_MAGIC;
670 } else if (G.total_blocks > 65535)
671 G.total_blocks = 65535;
672#if 0
673 struct stat statbuf;
674 xfstat(dev_fd, &statbuf, G.device_name);
675
676 if (!S_ISBLK(statbuf.st_mode))
677 opt &= ~1;
678#if 0
679
680
681 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
682
683 bb_error_msg_and_die("will not try "
684 "to make filesystem on '%s'", G.device_name);
685#endif
686#endif
687 tmp = G.root_block;
688 *(short *) tmp = 1;
689 strcpy(tmp + 2, ".");
690 tmp += G.dirsize;
691 *(short *) tmp = 1;
692 strcpy(tmp + 2, "..");
693 tmp += G.dirsize;
694 *(short *) tmp = 2;
695 strcpy(tmp + 2, ".badblocks");
696
697 setup_tables();
698
699 if (opt & 1)
700 check_blocks();
701 else if (listfile)
702 get_list_blocks(listfile);
703
704 if (version2) {
705 make_root_inode2();
706 make_bad_inode2();
707 } else {
708 make_root_inode();
709 make_bad_inode();
710 }
711
712 mark_good_blocks();
713 write_tables();
714 return 0;
715}
716