fsck_minix.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fsck.c - a file system consistency checker for Linux.
  4. *
  5. * (C) 1991, 1992 Linus Torvalds.
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. /*
  10. * 09.11.91 - made the first rudimentary functions
  11. *
  12. * 10.11.91 - updated, does checking, no repairs yet.
  13. * Sent out to the mailing-list for testing.
  14. *
  15. * 14.11.91 - Testing seems to have gone well. Added some
  16. * correction-code, and changed some functions.
  17. *
  18. * 15.11.91 - More correction code. Hopefully it notices most
  19. * cases now, and tries to do something about them.
  20. *
  21. * 16.11.91 - More corrections (thanks to Mika Jalava). Most
  22. * things seem to work now. Yeah, sure.
  23. *
  24. *
  25. * 19.04.92 - Had to start over again from this old version, as a
  26. * kernel bug ate my enhanced fsck in february.
  27. *
  28. * 28.02.93 - added support for different directory entry sizes..
  29. *
  30. * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
  31. * super-block information
  32. *
  33. * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
  34. * to that required by fsutil
  35. *
  36. * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
  37. * Added support for file system valid flag. Also
  38. * added program_version variable and output of
  39. * program name and version number when program
  40. * is executed.
  41. *
  42. * 30.10.94 - added support for v2 filesystem
  43. * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
  44. *
  45. * 10.12.94 - added test to prevent checking of mounted fs adapted
  46. * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
  47. * program. (Daniel Quinlan, quinlan@yggdrasil.com)
  48. *
  49. * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
  50. * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
  51. *
  52. * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
  53. * (Russell King). He made them for ARM. It would seem
  54. * that the ARM is powerful enough to do this in C whereas
  55. * i386 and m64k must use assembly to get it fast >:-)
  56. * This should make minix fsck system-independent.
  57. * (janl@math.uio.no, Nicolai Langfeldt)
  58. *
  59. * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
  60. * warnings. Added mc68k bitops from
  61. * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
  62. *
  63. * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
  64. * Andreas Schwab.
  65. *
  66. * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
  67. * - added Native Language Support
  68. *
  69. *
  70. * I've had no time to add comments - hopefully the function names
  71. * are comments enough. As with all file system checkers, this assumes
  72. * the file system is quiescent - don't use it on a mounted device
  73. * unless you can be sure nobody is writing to it (and remember that the
  74. * kernel can write to it when it searches for files).
  75. *
  76. * Usage: fsck [-larvsm] device
  77. * -l for a listing of all the filenames
  78. * -a for automatic repairs (not implemented)
  79. * -r for repairs (interactive) (not implemented)
  80. * -v for verbose (tells how many files)
  81. * -s for super-block info
  82. * -m for minix-like "mode not cleared" warnings
  83. * -f force filesystem check even if filesystem marked as valid
  84. *
  85. * The device may be a block device or a image of one, but this isn't
  86. * enforced (but it's not much fun on a character device :-).
  87. */
  88. #include "busybox.h"
  89. #include <mntent.h>
  90. #include "minix.h"
  91. #ifndef BLKGETSIZE
  92. #define BLKGETSIZE _IO(0x12,96) /* return device size */
  93. #endif
  94. #ifdef UNUSED
  95. enum {
  96. MINIX1_LINK_MAX = 250,
  97. MINIX2_LINK_MAX = 65530,
  98. MINIX_I_MAP_SLOTS = 8,
  99. MINIX_Z_MAP_SLOTS = 64,
  100. MINIX_V1 = 0x0001, /* original minix fs */
  101. MINIX_V2 = 0x0002, /* minix V2 fs */
  102. NAME_MAX = 255, /* # chars in a file name */
  103. };
  104. #endif
  105. #if ENABLE_FEATURE_MINIX2
  106. static smallint version2;
  107. #else
  108. enum { version2 = 0 };
  109. #endif
  110. #define PROGRAM_VERSION "1.2 - 11/11/96"
  111. static smallint repair, automatic, verbose, list, show, warn_mode, force;
  112. static smallint changed; /* is filesystem modified? */
  113. static smallint errors_uncorrected; /* flag if some error was not corrected */
  114. static smallint termios_set;
  115. static struct termios termios;
  116. static char *device_name;
  117. static int IN;
  118. static int directory, regular, blockdev, chardev, links, symlinks, total;
  119. //also smallint?
  120. static int dirsize = 16;
  121. static int namelen = 14;
  122. static char *inode_buffer;
  123. //xzalloc?
  124. static char super_block_buffer[BLOCK_SIZE];
  125. #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
  126. #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
  127. #define Super (*(struct minix_super_block *)super_block_buffer)
  128. #if ENABLE_FEATURE_MINIX2
  129. # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
  130. #else
  131. # define ZONES ((unsigned)(Super.s_nzones))
  132. #endif
  133. #define INODES ((unsigned)Super.s_ninodes)
  134. #define IMAPS ((unsigned)Super.s_imap_blocks)
  135. #define ZMAPS ((unsigned)Super.s_zmap_blocks)
  136. #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
  137. #define ZONESIZE ((unsigned)Super.s_log_zone_size)
  138. #define MAXSIZE ((unsigned)Super.s_max_size)
  139. #define MAGIC (Super.s_magic)
  140. /* gcc likes this more (code is smaller) than macro variant */
  141. static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
  142. {
  143. return (size + n-1) / n;
  144. }
  145. #if ENABLE_FEATURE_MINIX2
  146. #define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
  147. : MINIX1_INODES_PER_BLOCK))
  148. #else
  149. #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
  150. #endif
  151. #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
  152. #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
  153. static char *inode_map;
  154. static char *zone_map;
  155. static unsigned char *inode_count;
  156. static unsigned char *zone_count;
  157. static int bit(char *a, unsigned i)
  158. {
  159. return (a[i >> 3] & (1<<(i & 7))) != 0;
  160. }
  161. #define inode_in_use(x) (bit(inode_map,(x)))
  162. #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
  163. #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
  164. #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
  165. #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  166. #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  167. static void recursive_check(unsigned ino);
  168. #if ENABLE_FEATURE_MINIX2
  169. static void recursive_check2(unsigned ino);
  170. #endif
  171. static void die(const char *str) ATTRIBUTE_NORETURN;
  172. static void die(const char *str)
  173. {
  174. if (termios_set)
  175. tcsetattr(0, TCSANOW, &termios);
  176. bb_error_msg_and_die("%s", str);
  177. }
  178. /* File-name data */
  179. enum { MAX_DEPTH = 32 };
  180. static int name_depth;
  181. static char *current_name;
  182. static char *name_component[MAX_DEPTH+1];
  183. /* Wed Feb 9 15:17:06 MST 2000 */
  184. /* dynamically allocate name_list (instead of making it static) */
  185. static void alloc_current_name(void)
  186. {
  187. current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
  188. current_name[0] = '/';
  189. current_name[1] = '\0';
  190. name_component[0] = &current_name[0];
  191. }
  192. #if ENABLE_FEATURE_CLEAN_UP
  193. /* execute this atexit() to deallocate name_list[] */
  194. /* piptigger was here */
  195. static void free_current_name(void)
  196. {
  197. free(current_name);
  198. }
  199. #endif
  200. static void push_filename(const char *name)
  201. {
  202. // /dir/dir/dir/file
  203. // ^ ^ ^
  204. // [0] [1] [2] <-name_component[i]
  205. if (name_depth < MAX_DEPTH) {
  206. int len;
  207. char *p = name_component[name_depth];
  208. *p++ = '/';
  209. len = sprintf(p, "%.*s", namelen, name);
  210. name_component[name_depth + 1] = p + len;
  211. }
  212. name_depth++;
  213. }
  214. static void pop_filename(void)
  215. {
  216. name_depth--;
  217. if (name_depth < MAX_DEPTH) {
  218. *name_component[name_depth] = '\0';
  219. if (!name_depth) {
  220. current_name[0] = '/';
  221. current_name[1] = '\0';
  222. }
  223. }
  224. }
  225. static int ask(const char *string, int def)
  226. {
  227. int c;
  228. if (!repair) {
  229. puts("");
  230. errors_uncorrected = 1;
  231. return 0;
  232. }
  233. if (automatic) {
  234. puts("");
  235. if (!def)
  236. errors_uncorrected = 1;
  237. return def;
  238. }
  239. printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
  240. for (;;) {
  241. fflush(stdout);
  242. c = getchar();
  243. if (c == EOF) {
  244. if (!def)
  245. errors_uncorrected = 1;
  246. return def;
  247. }
  248. c = toupper(c);
  249. if (c == 'Y') {
  250. def = 1;
  251. break;
  252. } else if (c == 'N') {
  253. def = 0;
  254. break;
  255. } else if (c == ' ' || c == '\n')
  256. break;
  257. }
  258. if (def)
  259. printf("y\n");
  260. else {
  261. printf("n\n");
  262. errors_uncorrected = 1;
  263. }
  264. return def;
  265. }
  266. /*
  267. * Make certain that we aren't checking a filesystem that is on a
  268. * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
  269. * 1994 Theodore Ts'o. Also licensed under GPL.
  270. */
  271. static void check_mount(void)
  272. {
  273. FILE *f;
  274. struct mntent *mnt;
  275. int cont;
  276. int fd;
  277. f = setmntent(MOUNTED, "r");
  278. if (f == NULL)
  279. return;
  280. while ((mnt = getmntent(f)) != NULL)
  281. if (strcmp(device_name, mnt->mnt_fsname) == 0)
  282. break;
  283. endmntent(f);
  284. if (!mnt)
  285. return;
  286. /*
  287. * If the root is mounted read-only, then /etc/mtab is
  288. * probably not correct; so we won't issue a warning based on
  289. * it.
  290. */
  291. fd = open(MOUNTED, O_RDWR);
  292. if (fd < 0 && errno == EROFS)
  293. return;
  294. close(fd);
  295. printf("%s is mounted. ", device_name);
  296. cont = 0;
  297. if (isatty(0) && isatty(1))
  298. cont = ask("Do you really want to continue", 0);
  299. if (!cont) {
  300. printf("Check aborted\n");
  301. exit(0);
  302. }
  303. }
  304. /*
  305. * check_zone_nr checks to see that *nr is a valid zone nr. If it
  306. * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
  307. * if an error was corrected, and returns the zone (0 for no zone
  308. * or a bad zone-number).
  309. */
  310. static int check_zone_nr2(uint32_t *nr, smallint *corrected)
  311. {
  312. const char *msg;
  313. if (!*nr)
  314. return 0;
  315. if (*nr < FIRSTZONE)
  316. msg = "< FIRSTZONE";
  317. else if (*nr >= ZONES)
  318. msg = ">= ZONES";
  319. else
  320. return *nr;
  321. printf("Zone nr %s in file '%s'. ", msg, current_name);
  322. if (ask("Remove block", 1)) {
  323. *nr = 0;
  324. *corrected = 1;
  325. }
  326. return 0;
  327. }
  328. static int check_zone_nr(uint16_t *nr, smallint *corrected)
  329. {
  330. uint32_t nr32 = *nr;
  331. int r = check_zone_nr2(&nr32, corrected);
  332. *nr = (uint16_t)nr32;
  333. return r;
  334. }
  335. /*
  336. * read-block reads block nr into the buffer at addr.
  337. */
  338. static void read_block(unsigned nr, char *addr)
  339. {
  340. if (!nr) {
  341. memset(addr, 0, BLOCK_SIZE);
  342. return;
  343. }
  344. if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
  345. printf("%s: cannot seek to block in file '%s'\n",
  346. bb_msg_read_error, current_name);
  347. errors_uncorrected = 1;
  348. memset(addr, 0, BLOCK_SIZE);
  349. } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
  350. printf("%s: bad block in file '%s'\n",
  351. bb_msg_read_error, current_name);
  352. errors_uncorrected = 1;
  353. memset(addr, 0, BLOCK_SIZE);
  354. }
  355. }
  356. /*
  357. * write_block writes block nr to disk.
  358. */
  359. static void write_block(unsigned nr, char *addr)
  360. {
  361. if (!nr)
  362. return;
  363. if (nr < FIRSTZONE || nr >= ZONES) {
  364. printf("Internal error: trying to write bad block\n"
  365. "Write request ignored\n");
  366. errors_uncorrected = 1;
  367. return;
  368. }
  369. if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
  370. die("seek failed in write_block");
  371. if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
  372. printf("%s: bad block in file '%s'\n",
  373. bb_msg_write_error, current_name);
  374. errors_uncorrected = 1;
  375. }
  376. }
  377. /*
  378. * map_block calculates the absolute block nr of a block in a file.
  379. * It sets 'changed' if the inode has needed changing, and re-writes
  380. * any indirect blocks with errors.
  381. */
  382. static int map_block(struct minix1_inode *inode, unsigned blknr)
  383. {
  384. uint16_t ind[BLOCK_SIZE >> 1];
  385. uint16_t dind[BLOCK_SIZE >> 1];
  386. int block, result;
  387. smallint blk_chg;
  388. if (blknr < 7)
  389. return check_zone_nr(inode->i_zone + blknr, &changed);
  390. blknr -= 7;
  391. if (blknr < 512) {
  392. block = check_zone_nr(inode->i_zone + 7, &changed);
  393. read_block(block, (char *) ind);
  394. blk_chg = 0;
  395. result = check_zone_nr(blknr + ind, &blk_chg);
  396. if (blk_chg)
  397. write_block(block, (char *) ind);
  398. return result;
  399. }
  400. blknr -= 512;
  401. block = check_zone_nr(inode->i_zone + 8, &changed);
  402. read_block(block, (char *) dind);
  403. blk_chg = 0;
  404. result = check_zone_nr(dind + (blknr / 512), &blk_chg);
  405. if (blk_chg)
  406. write_block(block, (char *) dind);
  407. block = result;
  408. read_block(block, (char *) ind);
  409. blk_chg = 0;
  410. result = check_zone_nr(ind + (blknr % 512), &blk_chg);
  411. if (blk_chg)
  412. write_block(block, (char *) ind);
  413. return result;
  414. }
  415. #if ENABLE_FEATURE_MINIX2
  416. static int map_block2(struct minix2_inode *inode, unsigned blknr)
  417. {
  418. uint32_t ind[BLOCK_SIZE >> 2];
  419. uint32_t dind[BLOCK_SIZE >> 2];
  420. uint32_t tind[BLOCK_SIZE >> 2];
  421. int block, result;
  422. smallint blk_chg;
  423. if (blknr < 7)
  424. return check_zone_nr2(inode->i_zone + blknr, &changed);
  425. blknr -= 7;
  426. if (blknr < 256) {
  427. block = check_zone_nr2(inode->i_zone + 7, &changed);
  428. read_block(block, (char *) ind);
  429. blk_chg = 0;
  430. result = check_zone_nr2(blknr + ind, &blk_chg);
  431. if (blk_chg)
  432. write_block(block, (char *) ind);
  433. return result;
  434. }
  435. blknr -= 256;
  436. if (blknr >= 256 * 256) {
  437. block = check_zone_nr2(inode->i_zone + 8, &changed);
  438. read_block(block, (char *) dind);
  439. blk_chg = 0;
  440. result = check_zone_nr2(dind + blknr / 256, &blk_chg);
  441. if (blk_chg)
  442. write_block(block, (char *) dind);
  443. block = result;
  444. read_block(block, (char *) ind);
  445. blk_chg = 0;
  446. result = check_zone_nr2(ind + blknr % 256, &blk_chg);
  447. if (blk_chg)
  448. write_block(block, (char *) ind);
  449. return result;
  450. }
  451. blknr -= 256 * 256;
  452. block = check_zone_nr2(inode->i_zone + 9, &changed);
  453. read_block(block, (char *) tind);
  454. blk_chg = 0;
  455. result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
  456. if (blk_chg)
  457. write_block(block, (char *) tind);
  458. block = result;
  459. read_block(block, (char *) dind);
  460. blk_chg = 0;
  461. result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
  462. if (blk_chg)
  463. write_block(block, (char *) dind);
  464. block = result;
  465. read_block(block, (char *) ind);
  466. blk_chg = 0;
  467. result = check_zone_nr2(ind + blknr % 256, &blk_chg);
  468. if (blk_chg)
  469. write_block(block, (char *) ind);
  470. return result;
  471. }
  472. #endif
  473. static void write_super_block(void)
  474. {
  475. /*
  476. * Set the state of the filesystem based on whether or not there
  477. * are uncorrected errors. The filesystem valid flag is
  478. * unconditionally set if we get this far.
  479. */
  480. Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
  481. if (!errors_uncorrected)
  482. Super.s_state &= ~MINIX_ERROR_FS;
  483. if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
  484. die("seek failed in write_super_block");
  485. if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
  486. die("cannot write super-block");
  487. }
  488. static void write_tables(void)
  489. {
  490. write_super_block();
  491. if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
  492. die("cannot write inode map");
  493. if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
  494. die("cannot write zone map");
  495. if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
  496. die("cannot write inodes");
  497. }
  498. static void get_dirsize(void)
  499. {
  500. int block;
  501. char blk[BLOCK_SIZE];
  502. int size;
  503. #if ENABLE_FEATURE_MINIX2
  504. if (version2)
  505. block = Inode2[MINIX_ROOT_INO].i_zone[0];
  506. else
  507. #endif
  508. block = Inode1[MINIX_ROOT_INO].i_zone[0];
  509. read_block(block, blk);
  510. for (size = 16; size < BLOCK_SIZE; size <<= 1) {
  511. if (strcmp(blk + size + 2, "..") == 0) {
  512. dirsize = size;
  513. namelen = size - 2;
  514. return;
  515. }
  516. }
  517. /* use defaults */
  518. }
  519. static void read_superblock(void)
  520. {
  521. if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
  522. die("seek failed");
  523. if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
  524. die("cannot read super block");
  525. /* already initialized to:
  526. namelen = 14;
  527. dirsize = 16;
  528. version2 = 0;
  529. */
  530. if (MAGIC == MINIX1_SUPER_MAGIC) {
  531. } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
  532. namelen = 30;
  533. dirsize = 32;
  534. #if ENABLE_FEATURE_MINIX2
  535. } else if (MAGIC == MINIX2_SUPER_MAGIC) {
  536. version2 = 1;
  537. } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
  538. namelen = 30;
  539. dirsize = 32;
  540. version2 = 1;
  541. #endif
  542. } else
  543. die("bad magic number in super-block");
  544. if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
  545. die("only 1k blocks/zones supported");
  546. if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
  547. die("bad s_imap_blocks field in super-block");
  548. if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
  549. die("bad s_zmap_blocks field in super-block");
  550. }
  551. static void read_tables(void)
  552. {
  553. inode_map = xzalloc(IMAPS * BLOCK_SIZE);
  554. zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
  555. inode_buffer = xmalloc(INODE_BUFFER_SIZE);
  556. inode_count = xmalloc(INODES + 1);
  557. zone_count = xmalloc(ZONES);
  558. if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
  559. die("cannot read inode map");
  560. if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
  561. die("cannot read zone map");
  562. if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
  563. die("cannot read inodes");
  564. if (NORM_FIRSTZONE != FIRSTZONE) {
  565. printf("warning: firstzone!=norm_firstzone\n");
  566. errors_uncorrected = 1;
  567. }
  568. get_dirsize();
  569. if (show) {
  570. printf("%u inodes\n"
  571. "%u blocks\n"
  572. "Firstdatazone=%u (%u)\n"
  573. "Zonesize=%u\n"
  574. "Maxsize=%u\n"
  575. "Filesystem state=%u\n"
  576. "namelen=%u\n\n",
  577. INODES,
  578. ZONES,
  579. FIRSTZONE, NORM_FIRSTZONE,
  580. BLOCK_SIZE << ZONESIZE,
  581. MAXSIZE,
  582. Super.s_state,
  583. namelen);
  584. }
  585. }
  586. static struct minix1_inode *get_inode(unsigned nr)
  587. {
  588. struct minix1_inode *inode;
  589. if (!nr || nr > INODES)
  590. return NULL;
  591. total++;
  592. inode = Inode1 + nr;
  593. if (!inode_count[nr]) {
  594. if (!inode_in_use(nr)) {
  595. printf("Inode %d is marked as 'unused', but it is used "
  596. "for file '%s'\n", nr, current_name);
  597. if (repair) {
  598. if (ask("Mark as 'in use'", 1))
  599. mark_inode(nr);
  600. } else {
  601. errors_uncorrected = 1;
  602. }
  603. }
  604. if (S_ISDIR(inode->i_mode))
  605. directory++;
  606. else if (S_ISREG(inode->i_mode))
  607. regular++;
  608. else if (S_ISCHR(inode->i_mode))
  609. chardev++;
  610. else if (S_ISBLK(inode->i_mode))
  611. blockdev++;
  612. else if (S_ISLNK(inode->i_mode))
  613. symlinks++;
  614. else if (S_ISSOCK(inode->i_mode));
  615. else if (S_ISFIFO(inode->i_mode));
  616. else {
  617. printf("%s has mode %05o\n", current_name, inode->i_mode);
  618. }
  619. } else
  620. links++;
  621. if (!++inode_count[nr]) {
  622. printf("Warning: inode count too big\n");
  623. inode_count[nr]--;
  624. errors_uncorrected = 1;
  625. }
  626. return inode;
  627. }
  628. #if ENABLE_FEATURE_MINIX2
  629. static struct minix2_inode *get_inode2(unsigned nr)
  630. {
  631. struct minix2_inode *inode;
  632. if (!nr || nr > INODES)
  633. return NULL;
  634. total++;
  635. inode = Inode2 + nr;
  636. if (!inode_count[nr]) {
  637. if (!inode_in_use(nr)) {
  638. printf("Inode %d is marked as 'unused', but it is used "
  639. "for file '%s'\n", nr, current_name);
  640. if (repair) {
  641. if (ask("Mark as 'in use'", 1))
  642. mark_inode(nr);
  643. else
  644. errors_uncorrected = 1;
  645. }
  646. }
  647. if (S_ISDIR(inode->i_mode))
  648. directory++;
  649. else if (S_ISREG(inode->i_mode))
  650. regular++;
  651. else if (S_ISCHR(inode->i_mode))
  652. chardev++;
  653. else if (S_ISBLK(inode->i_mode))
  654. blockdev++;
  655. else if (S_ISLNK(inode->i_mode))
  656. symlinks++;
  657. else if (S_ISSOCK(inode->i_mode));
  658. else if (S_ISFIFO(inode->i_mode));
  659. else {
  660. printf("%s has mode %05o\n", current_name, inode->i_mode);
  661. }
  662. } else
  663. links++;
  664. if (!++inode_count[nr]) {
  665. printf("Warning: inode count too big\n");
  666. inode_count[nr]--;
  667. errors_uncorrected = 1;
  668. }
  669. return inode;
  670. }
  671. #endif
  672. static void check_root(void)
  673. {
  674. struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
  675. if (!inode || !S_ISDIR(inode->i_mode))
  676. die("root inode isn't a directory");
  677. }
  678. #if ENABLE_FEATURE_MINIX2
  679. static void check_root2(void)
  680. {
  681. struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
  682. if (!inode || !S_ISDIR(inode->i_mode))
  683. die("root inode isn't a directory");
  684. }
  685. #else
  686. void check_root2(void);
  687. #endif
  688. static int add_zone(uint16_t *znr, smallint *corrected)
  689. {
  690. int result;
  691. int block;
  692. result = 0;
  693. block = check_zone_nr(znr, corrected);
  694. if (!block)
  695. return 0;
  696. if (zone_count[block]) {
  697. printf("Already used block is reused in file '%s'. ",
  698. current_name);
  699. if (ask("Clear", 1)) {
  700. *znr = 0;
  701. block = 0;
  702. *corrected = 1;
  703. return 0;
  704. }
  705. }
  706. if (!zone_in_use(block)) {
  707. printf("Block %d in file '%s' is marked as 'unused'. ",
  708. block, current_name);
  709. if (ask("Correct", 1))
  710. mark_zone(block);
  711. }
  712. if (!++zone_count[block])
  713. zone_count[block]--;
  714. return block;
  715. }
  716. #if ENABLE_FEATURE_MINIX2
  717. static int add_zone2(uint32_t *znr, smallint *corrected)
  718. {
  719. int result;
  720. int block;
  721. result = 0;
  722. block = check_zone_nr2(znr, corrected);
  723. if (!block)
  724. return 0;
  725. if (zone_count[block]) {
  726. printf("Already used block is reused in file '%s'. ",
  727. current_name);
  728. if (ask("Clear", 1)) {
  729. *znr = 0;
  730. block = 0;
  731. *corrected = 1;
  732. return 0;
  733. }
  734. }
  735. if (!zone_in_use(block)) {
  736. printf("Block %d in file '%s' is marked as 'unused'. ",
  737. block, current_name);
  738. if (ask("Correct", 1))
  739. mark_zone(block);
  740. }
  741. if (!++zone_count[block])
  742. zone_count[block]--;
  743. return block;
  744. }
  745. #endif
  746. static void add_zone_ind(uint16_t *znr, smallint *corrected)
  747. {
  748. static char blk[BLOCK_SIZE];
  749. int i;
  750. int block;
  751. smallint chg_blk = 0;
  752. block = add_zone(znr, corrected);
  753. if (!block)
  754. return;
  755. read_block(block, blk);
  756. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  757. add_zone(i + (uint16_t *) blk, &chg_blk);
  758. if (chg_blk)
  759. write_block(block, blk);
  760. }
  761. #if ENABLE_FEATURE_MINIX2
  762. static void add_zone_ind2(uint32_t *znr, smallint *corrected)
  763. {
  764. static char blk[BLOCK_SIZE];
  765. int i;
  766. int block;
  767. smallint chg_blk = 0;
  768. block = add_zone2(znr, corrected);
  769. if (!block)
  770. return;
  771. read_block(block, blk);
  772. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  773. add_zone2(i + (uint32_t *) blk, &chg_blk);
  774. if (chg_blk)
  775. write_block(block, blk);
  776. }
  777. #endif
  778. static void add_zone_dind(uint16_t *znr, smallint *corrected)
  779. {
  780. static char blk[BLOCK_SIZE];
  781. int i;
  782. int block;
  783. smallint chg_blk = 0;
  784. block = add_zone(znr, corrected);
  785. if (!block)
  786. return;
  787. read_block(block, blk);
  788. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  789. add_zone_ind(i + (uint16_t *) blk, &chg_blk);
  790. if (chg_blk)
  791. write_block(block, blk);
  792. }
  793. #if ENABLE_FEATURE_MINIX2
  794. static void add_zone_dind2(uint32_t *znr, smallint *corrected)
  795. {
  796. static char blk[BLOCK_SIZE];
  797. int i;
  798. int block;
  799. smallint chg_blk = 0;
  800. block = add_zone2(znr, corrected);
  801. if (!block)
  802. return;
  803. read_block(block, blk);
  804. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  805. add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
  806. if (chg_blk)
  807. write_block(block, blk);
  808. }
  809. static void add_zone_tind2(uint32_t *znr, smallint *corrected)
  810. {
  811. static char blk[BLOCK_SIZE];
  812. int i;
  813. int block;
  814. smallint chg_blk = 0;
  815. block = add_zone2(znr, corrected);
  816. if (!block)
  817. return;
  818. read_block(block, blk);
  819. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  820. add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
  821. if (chg_blk)
  822. write_block(block, blk);
  823. }
  824. #endif
  825. static void check_zones(unsigned i)
  826. {
  827. struct minix1_inode *inode;
  828. if (!i || i > INODES)
  829. return;
  830. if (inode_count[i] > 1) /* have we counted this file already? */
  831. return;
  832. inode = Inode1 + i;
  833. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  834. !S_ISLNK(inode->i_mode)) return;
  835. for (i = 0; i < 7; i++)
  836. add_zone(i + inode->i_zone, &changed);
  837. add_zone_ind(7 + inode->i_zone, &changed);
  838. add_zone_dind(8 + inode->i_zone, &changed);
  839. }
  840. #if ENABLE_FEATURE_MINIX2
  841. static void check_zones2(unsigned i)
  842. {
  843. struct minix2_inode *inode;
  844. if (!i || i > INODES)
  845. return;
  846. if (inode_count[i] > 1) /* have we counted this file already? */
  847. return;
  848. inode = Inode2 + i;
  849. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
  850. && !S_ISLNK(inode->i_mode))
  851. return;
  852. for (i = 0; i < 7; i++)
  853. add_zone2(i + inode->i_zone, &changed);
  854. add_zone_ind2(7 + inode->i_zone, &changed);
  855. add_zone_dind2(8 + inode->i_zone, &changed);
  856. add_zone_tind2(9 + inode->i_zone, &changed);
  857. }
  858. #endif
  859. static void check_file(struct minix1_inode *dir, unsigned offset)
  860. {
  861. static char blk[BLOCK_SIZE];
  862. struct minix1_inode *inode;
  863. int ino;
  864. char *name;
  865. int block;
  866. block = map_block(dir, offset / BLOCK_SIZE);
  867. read_block(block, blk);
  868. name = blk + (offset % BLOCK_SIZE) + 2;
  869. ino = *(uint16_t *) (name - 2);
  870. if (ino > INODES) {
  871. printf("%s contains a bad inode number for file '%.*s'. ",
  872. current_name, namelen, name);
  873. if (ask("Remove", 1)) {
  874. *(uint16_t *) (name - 2) = 0;
  875. write_block(block, blk);
  876. }
  877. ino = 0;
  878. }
  879. push_filename(name);
  880. inode = get_inode(ino);
  881. pop_filename();
  882. if (!offset) {
  883. if (inode && LONE_CHAR(name, '.'))
  884. return;
  885. printf("%s: bad directory: '.' isn't first\n", current_name);
  886. errors_uncorrected = 1;
  887. }
  888. if (offset == dirsize) {
  889. if (inode && strcmp("..", name) == 0)
  890. return;
  891. printf("%s: bad directory: '..' isn't second\n", current_name);
  892. errors_uncorrected = 1;
  893. }
  894. if (!inode)
  895. return;
  896. push_filename(name);
  897. if (list) {
  898. if (verbose)
  899. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  900. printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
  901. }
  902. check_zones(ino);
  903. if (inode && S_ISDIR(inode->i_mode))
  904. recursive_check(ino);
  905. pop_filename();
  906. }
  907. #if ENABLE_FEATURE_MINIX2
  908. static void check_file2(struct minix2_inode *dir, unsigned offset)
  909. {
  910. static char blk[BLOCK_SIZE];
  911. struct minix2_inode *inode;
  912. int ino;
  913. char *name;
  914. int block;
  915. block = map_block2(dir, offset / BLOCK_SIZE);
  916. read_block(block, blk);
  917. name = blk + (offset % BLOCK_SIZE) + 2;
  918. ino = *(uint16_t *) (name - 2);
  919. if (ino > INODES) {
  920. printf("%s contains a bad inode number for file '%.*s'. ",
  921. current_name, namelen, name);
  922. if (ask("Remove", 1)) {
  923. *(uint16_t *) (name - 2) = 0;
  924. write_block(block, blk);
  925. }
  926. ino = 0;
  927. }
  928. push_filename(name);
  929. inode = get_inode2(ino);
  930. pop_filename();
  931. if (!offset) {
  932. if (inode && LONE_CHAR(name, '.'))
  933. return;
  934. printf("%s: bad directory: '.' isn't first\n", current_name);
  935. errors_uncorrected = 1;
  936. }
  937. if (offset == dirsize) {
  938. if (inode && strcmp("..", name) == 0)
  939. return;
  940. printf("%s: bad directory: '..' isn't second\n", current_name);
  941. errors_uncorrected = 1;
  942. }
  943. if (!inode)
  944. return;
  945. push_filename(name);
  946. if (list) {
  947. if (verbose)
  948. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  949. printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
  950. }
  951. check_zones2(ino);
  952. if (inode && S_ISDIR(inode->i_mode))
  953. recursive_check2(ino);
  954. pop_filename();
  955. }
  956. #endif
  957. static void recursive_check(unsigned ino)
  958. {
  959. struct minix1_inode *dir;
  960. unsigned offset;
  961. dir = Inode1 + ino;
  962. if (!S_ISDIR(dir->i_mode))
  963. die("internal error");
  964. if (dir->i_size < 2 * dirsize) {
  965. printf("%s: bad directory: size<32", current_name);
  966. errors_uncorrected = 1;
  967. }
  968. for (offset = 0; offset < dir->i_size; offset += dirsize)
  969. check_file(dir, offset);
  970. }
  971. #if ENABLE_FEATURE_MINIX2
  972. static void recursive_check2(unsigned ino)
  973. {
  974. struct minix2_inode *dir;
  975. unsigned offset;
  976. dir = Inode2 + ino;
  977. if (!S_ISDIR(dir->i_mode))
  978. die("internal error");
  979. if (dir->i_size < 2 * dirsize) {
  980. printf("%s: bad directory: size<32", current_name);
  981. errors_uncorrected = 1;
  982. }
  983. for (offset = 0; offset < dir->i_size; offset += dirsize)
  984. check_file2(dir, offset);
  985. }
  986. #endif
  987. static int bad_zone(int i)
  988. {
  989. char buffer[BLOCK_SIZE];
  990. if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
  991. die("seek failed in bad_zone");
  992. return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
  993. }
  994. static void check_counts(void)
  995. {
  996. int i;
  997. for (i = 1; i <= INODES; i++) {
  998. if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
  999. printf("Inode %d has non-zero mode. ", i);
  1000. if (ask("Clear", 1)) {
  1001. Inode1[i].i_mode = 0;
  1002. changed = 1;
  1003. }
  1004. }
  1005. if (!inode_count[i]) {
  1006. if (!inode_in_use(i))
  1007. continue;
  1008. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  1009. if (ask("Clear", 1))
  1010. unmark_inode(i);
  1011. continue;
  1012. }
  1013. if (!inode_in_use(i)) {
  1014. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1015. if (ask("Set", 1))
  1016. mark_inode(i);
  1017. }
  1018. if (Inode1[i].i_nlinks != inode_count[i]) {
  1019. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1020. i, Inode1[i].i_mode, Inode1[i].i_nlinks, inode_count[i]);
  1021. if (ask("Set i_nlinks to count", 1)) {
  1022. Inode1[i].i_nlinks = inode_count[i];
  1023. changed = 1;
  1024. }
  1025. }
  1026. }
  1027. for (i = FIRSTZONE; i < ZONES; i++) {
  1028. if (zone_in_use(i) == zone_count[i])
  1029. continue;
  1030. if (!zone_count[i]) {
  1031. if (bad_zone(i))
  1032. continue;
  1033. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1034. if (ask("Unmark", 1))
  1035. unmark_zone(i);
  1036. continue;
  1037. }
  1038. printf("Zone %d: %sin use, counted=%d\n",
  1039. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1040. }
  1041. }
  1042. #if ENABLE_FEATURE_MINIX2
  1043. static void check_counts2(void)
  1044. {
  1045. int i;
  1046. for (i = 1; i <= INODES; i++) {
  1047. if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
  1048. printf("Inode %d has non-zero mode. ", i);
  1049. if (ask("Clear", 1)) {
  1050. Inode2[i].i_mode = 0;
  1051. changed = 1;
  1052. }
  1053. }
  1054. if (!inode_count[i]) {
  1055. if (!inode_in_use(i))
  1056. continue;
  1057. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  1058. if (ask("Clear", 1))
  1059. unmark_inode(i);
  1060. continue;
  1061. }
  1062. if (!inode_in_use(i)) {
  1063. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1064. if (ask("Set", 1))
  1065. mark_inode(i);
  1066. }
  1067. if (Inode2[i].i_nlinks != inode_count[i]) {
  1068. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1069. i, Inode2[i].i_mode, Inode2[i].i_nlinks,
  1070. inode_count[i]);
  1071. if (ask("Set i_nlinks to count", 1)) {
  1072. Inode2[i].i_nlinks = inode_count[i];
  1073. changed = 1;
  1074. }
  1075. }
  1076. }
  1077. for (i = FIRSTZONE; i < ZONES; i++) {
  1078. if (zone_in_use(i) == zone_count[i])
  1079. continue;
  1080. if (!zone_count[i]) {
  1081. if (bad_zone(i))
  1082. continue;
  1083. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1084. if (ask("Unmark", 1))
  1085. unmark_zone(i);
  1086. continue;
  1087. }
  1088. printf("Zone %d: %sin use, counted=%d\n",
  1089. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1090. }
  1091. }
  1092. #endif
  1093. static void check(void)
  1094. {
  1095. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1096. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1097. check_zones(MINIX_ROOT_INO);
  1098. recursive_check(MINIX_ROOT_INO);
  1099. check_counts();
  1100. }
  1101. #if ENABLE_FEATURE_MINIX2
  1102. static void check2(void)
  1103. {
  1104. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1105. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1106. check_zones2(MINIX_ROOT_INO);
  1107. recursive_check2(MINIX_ROOT_INO);
  1108. check_counts2();
  1109. }
  1110. #else
  1111. void check2(void);
  1112. #endif
  1113. int fsck_minix_main(int argc, char **argv)
  1114. {
  1115. struct termios tmp;
  1116. int retcode = 0;
  1117. xfunc_error_retval = 8;
  1118. alloc_current_name();
  1119. #if ENABLE_FEATURE_CLEAN_UP
  1120. /* Don't bother to free memory. Exit does
  1121. * that automagically, so we can save a few bytes */
  1122. atexit(free_current_name);
  1123. #endif
  1124. if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
  1125. die("bad inode size");
  1126. #if ENABLE_FEATURE_MINIX2
  1127. if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
  1128. die("bad v2 inode size");
  1129. #endif
  1130. while (--argc != 0) {
  1131. argv++;
  1132. if (argv[0][0] != '-') {
  1133. if (device_name)
  1134. bb_show_usage();
  1135. device_name = argv[0];
  1136. } else {
  1137. while (*++argv[0]) {
  1138. switch (argv[0][0]) {
  1139. case 'l':
  1140. list = 1;
  1141. break;
  1142. case 'a':
  1143. automatic = 1;
  1144. repair = 1;
  1145. break;
  1146. case 'r':
  1147. automatic = 0;
  1148. repair = 1;
  1149. break;
  1150. case 'v':
  1151. verbose = 1;
  1152. break;
  1153. case 's':
  1154. show = 1;
  1155. break;
  1156. case 'm':
  1157. warn_mode = 1;
  1158. break;
  1159. case 'f':
  1160. force = 1;
  1161. break;
  1162. default:
  1163. bb_show_usage();
  1164. }
  1165. }
  1166. }
  1167. }
  1168. if (!device_name)
  1169. bb_show_usage();
  1170. check_mount(); /* trying to check a mounted filesystem? */
  1171. if (repair && !automatic) {
  1172. if (!isatty(0) || !isatty(1))
  1173. die("need terminal for interactive repairs");
  1174. }
  1175. IN = xopen(device_name, repair ? O_RDWR : O_RDONLY);
  1176. /*sync(); paranoia? */
  1177. read_superblock();
  1178. /*
  1179. * Determine whether or not we should continue with the checking.
  1180. * This is based on the status of the filesystem valid and error
  1181. * flags and whether or not the -f switch was specified on the
  1182. * command line.
  1183. */
  1184. printf("%s, "PROGRAM_VERSION"\n", applet_name);
  1185. if (!(Super.s_state & MINIX_ERROR_FS)
  1186. && (Super.s_state & MINIX_VALID_FS) && !force
  1187. ) {
  1188. if (repair)
  1189. printf("%s is clean, check is skipped\n", device_name);
  1190. return 0;
  1191. } else if (force)
  1192. printf("Forcing filesystem check on %s\n", device_name);
  1193. else if (repair)
  1194. printf("Filesystem on %s is dirty, needs checking\n",
  1195. device_name);
  1196. read_tables();
  1197. if (repair && !automatic) {
  1198. tcgetattr(0, &termios);
  1199. tmp = termios;
  1200. tmp.c_lflag &= ~(ICANON | ECHO);
  1201. tcsetattr(0, TCSANOW, &tmp);
  1202. termios_set = 1;
  1203. }
  1204. if (version2) {
  1205. check_root2();
  1206. check2();
  1207. } else {
  1208. check_root();
  1209. check();
  1210. }
  1211. if (verbose) {
  1212. int i, free_cnt;
  1213. for (i = 1, free_cnt = 0; i <= INODES; i++)
  1214. if (!inode_in_use(i))
  1215. free_cnt++;
  1216. printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
  1217. 100 * (INODES - free_cnt) / INODES);
  1218. for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
  1219. if (!zone_in_use(i))
  1220. free_cnt++;
  1221. printf("%6u zones used (%u%%)\n\n"
  1222. "%6u regular files\n"
  1223. "%6u directories\n"
  1224. "%6u character device files\n"
  1225. "%6u block device files\n"
  1226. "%6u links\n"
  1227. "%6u symbolic links\n"
  1228. "------\n"
  1229. "%6u files\n",
  1230. (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
  1231. regular, directory, chardev, blockdev,
  1232. links - 2 * directory + 1, symlinks,
  1233. total - 2 * directory + 1);
  1234. }
  1235. if (changed) {
  1236. write_tables();
  1237. printf("FILE SYSTEM HAS BEEN CHANGED\n");
  1238. sync();
  1239. } else if (repair)
  1240. write_super_block();
  1241. if (repair && !automatic)
  1242. tcsetattr(0, TCSANOW, &termios);
  1243. if (changed)
  1244. retcode += 3;
  1245. if (errors_uncorrected)
  1246. retcode += 4;
  1247. return retcode;
  1248. }