mkfs_minix.c 19 KB

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