3
0

mkfs_minix.c 19 KB

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