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