mkfs_minix.c 19 KB

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