mkfs_minix.c 19 KB

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