mkfs_minix.c 19 KB

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