mkfs_minix.c 19 KB

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