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