mkfs_minix.c 19 KB

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