3
0

fsck_minix.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 source tree.
  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. * superblock 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 Mickiewicz <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 superblock 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. //usage:#define fsck_minix_trivial_usage
  89. //usage: "[-larvsmf] BLOCKDEV"
  90. //usage:#define fsck_minix_full_usage "\n\n"
  91. //usage: "Check MINIX filesystem\n"
  92. //usage: "\n -l List all filenames"
  93. //usage: "\n -r Perform interactive repairs"
  94. //usage: "\n -a Perform automatic repairs"
  95. //usage: "\n -v Verbose"
  96. //usage: "\n -s Output superblock information"
  97. //usage: "\n -m Show \"mode not cleared\" warnings"
  98. //usage: "\n -f Force file system check"
  99. #include <mntent.h>
  100. #include "libbb.h"
  101. #include "minix.h"
  102. #ifndef BLKGETSIZE
  103. #define BLKGETSIZE _IO(0x12,96) /* return device size */
  104. #endif
  105. struct BUG_bad_inode_size {
  106. char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
  107. #if ENABLE_FEATURE_MINIX2
  108. char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
  109. #endif
  110. };
  111. enum {
  112. #ifdef UNUSED
  113. MINIX1_LINK_MAX = 250,
  114. MINIX2_LINK_MAX = 65530,
  115. MINIX_I_MAP_SLOTS = 8,
  116. MINIX_Z_MAP_SLOTS = 64,
  117. MINIX_V1 = 0x0001, /* original minix fs */
  118. MINIX_V2 = 0x0002, /* minix V2 fs */
  119. #endif
  120. MINIX_NAME_MAX = 255, /* # chars in a file name */
  121. };
  122. #if !ENABLE_FEATURE_MINIX2
  123. enum { version2 = 0 };
  124. #endif
  125. enum { MAX_DEPTH = 32 };
  126. enum { dev_fd = 3 };
  127. struct globals {
  128. #if ENABLE_FEATURE_MINIX2
  129. smallint version2;
  130. #endif
  131. smallint changed; /* is filesystem modified? */
  132. smallint errors_uncorrected; /* flag if some error was not corrected */
  133. smallint termios_set;
  134. smallint dirsize;
  135. smallint namelen;
  136. const char *device_name;
  137. int directory, regular, blockdev, chardev, links, symlinks, total;
  138. char *inode_buffer;
  139. char *inode_map;
  140. char *zone_map;
  141. unsigned char *inode_count;
  142. unsigned char *zone_count;
  143. /* File-name data */
  144. int name_depth;
  145. char *name_component[MAX_DEPTH+1];
  146. /* Bigger stuff */
  147. struct termios sv_termios;
  148. char superblock_buffer[BLOCK_SIZE];
  149. char add_zone_ind_blk[BLOCK_SIZE];
  150. char add_zone_dind_blk[BLOCK_SIZE];
  151. IF_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
  152. char check_file_blk[BLOCK_SIZE];
  153. /* File-name data */
  154. char current_name[MAX_DEPTH * MINIX_NAME_MAX];
  155. };
  156. #define G (*ptr_to_globals)
  157. #if ENABLE_FEATURE_MINIX2
  158. #define version2 (G.version2 )
  159. #endif
  160. #define changed (G.changed )
  161. #define errors_uncorrected (G.errors_uncorrected )
  162. #define termios_set (G.termios_set )
  163. #define dirsize (G.dirsize )
  164. #define namelen (G.namelen )
  165. #define device_name (G.device_name )
  166. #define directory (G.directory )
  167. #define regular (G.regular )
  168. #define blockdev (G.blockdev )
  169. #define chardev (G.chardev )
  170. #define links (G.links )
  171. #define symlinks (G.symlinks )
  172. #define total (G.total )
  173. #define inode_buffer (G.inode_buffer )
  174. #define inode_map (G.inode_map )
  175. #define zone_map (G.zone_map )
  176. #define inode_count (G.inode_count )
  177. #define zone_count (G.zone_count )
  178. #define name_depth (G.name_depth )
  179. #define name_component (G.name_component )
  180. #define sv_termios (G.sv_termios )
  181. #define superblock_buffer (G.superblock_buffer )
  182. #define add_zone_ind_blk (G.add_zone_ind_blk )
  183. #define add_zone_dind_blk (G.add_zone_dind_blk )
  184. #define add_zone_tind_blk (G.add_zone_tind_blk )
  185. #define check_file_blk (G.check_file_blk )
  186. #define current_name (G.current_name )
  187. #define INIT_G() do { \
  188. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  189. dirsize = 16; \
  190. namelen = 14; \
  191. current_name[0] = '/'; \
  192. /*current_name[1] = '\0';*/ \
  193. name_component[0] = &current_name[0]; \
  194. } while (0)
  195. #define OPTION_STR "larvsmf"
  196. enum {
  197. OPT_l = (1 << 0),
  198. OPT_a = (1 << 1),
  199. OPT_r = (1 << 2),
  200. OPT_v = (1 << 3),
  201. OPT_s = (1 << 4),
  202. OPT_w = (1 << 5),
  203. OPT_f = (1 << 6),
  204. };
  205. #define OPT_list (option_mask32 & OPT_l)
  206. #define OPT_automatic (option_mask32 & OPT_a)
  207. #define OPT_repair (option_mask32 & OPT_r)
  208. #define OPT_verbose (option_mask32 & OPT_v)
  209. #define OPT_show (option_mask32 & OPT_s)
  210. #define OPT_warn_mode (option_mask32 & OPT_w)
  211. #define OPT_force (option_mask32 & OPT_f)
  212. /* non-automatic repairs requested? */
  213. #define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
  214. #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
  215. #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
  216. #define Super (*(struct minix_superblock *)(superblock_buffer))
  217. #if ENABLE_FEATURE_MINIX2
  218. # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
  219. #else
  220. # define ZONES ((unsigned)(Super.s_nzones))
  221. #endif
  222. #define INODES ((unsigned)Super.s_ninodes)
  223. #define IMAPS ((unsigned)Super.s_imap_blocks)
  224. #define ZMAPS ((unsigned)Super.s_zmap_blocks)
  225. #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
  226. #define ZONESIZE ((unsigned)Super.s_log_zone_size)
  227. #define MAXSIZE ((unsigned)Super.s_max_size)
  228. #define MAGIC (Super.s_magic)
  229. /* gcc likes this more (code is smaller) than macro variant */
  230. static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
  231. {
  232. return (size + n-1) / n;
  233. }
  234. #if !ENABLE_FEATURE_MINIX2
  235. #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
  236. #else
  237. #define INODE_BLOCKS div_roundup(INODES, \
  238. (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
  239. #endif
  240. #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
  241. #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
  242. /* Before you ask "where they come from?": */
  243. /* setbit/clrbit are supplied by sys/param.h */
  244. static int minix_bit(const char *a, unsigned i)
  245. {
  246. return (a[i >> 3] & (1<<(i & 7)));
  247. }
  248. static void minix_setbit(char *a, unsigned i)
  249. {
  250. setbit(a, i);
  251. changed = 1;
  252. }
  253. static void minix_clrbit(char *a, unsigned i)
  254. {
  255. clrbit(a, i);
  256. changed = 1;
  257. }
  258. /* Note: do not assume 0/1, it is 0/nonzero */
  259. #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
  260. #define inode_in_use(x) (minix_bit(inode_map,(x)))
  261. #define mark_inode(x) (minix_setbit(inode_map,(x)))
  262. #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
  263. #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
  264. #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
  265. static void recursive_check(unsigned ino);
  266. #if ENABLE_FEATURE_MINIX2
  267. static void recursive_check2(unsigned ino);
  268. #endif
  269. static void die(const char *str) NORETURN;
  270. static void die(const char *str)
  271. {
  272. if (termios_set)
  273. tcsetattr_stdin_TCSANOW(&sv_termios);
  274. bb_error_msg_and_die("%s", str);
  275. }
  276. static void push_filename(const char *name)
  277. {
  278. // /dir/dir/dir/file
  279. // ^ ^ ^
  280. // [0] [1] [2] <-name_component[i]
  281. if (name_depth < MAX_DEPTH) {
  282. int len;
  283. char *p = name_component[name_depth];
  284. *p++ = '/';
  285. len = sprintf(p, "%.*s", namelen, name);
  286. name_component[name_depth + 1] = p + len;
  287. }
  288. name_depth++;
  289. }
  290. static void pop_filename(void)
  291. {
  292. name_depth--;
  293. if (name_depth < MAX_DEPTH) {
  294. *name_component[name_depth] = '\0';
  295. if (!name_depth) {
  296. current_name[0] = '/';
  297. current_name[1] = '\0';
  298. }
  299. }
  300. }
  301. static int ask(const char *string, int def)
  302. {
  303. int c;
  304. if (!OPT_repair) {
  305. bb_putchar('\n');
  306. errors_uncorrected = 1;
  307. return 0;
  308. }
  309. if (OPT_automatic) {
  310. bb_putchar('\n');
  311. if (!def)
  312. errors_uncorrected = 1;
  313. return def;
  314. }
  315. printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
  316. for (;;) {
  317. fflush_all();
  318. c = getchar();
  319. if (c == EOF) {
  320. if (!def)
  321. errors_uncorrected = 1;
  322. return def;
  323. }
  324. if (c == '\n')
  325. break;
  326. c |= 0x20; /* tolower */
  327. if (c == 'y') {
  328. def = 1;
  329. break;
  330. }
  331. if (c == 'n') {
  332. def = 0;
  333. break;
  334. }
  335. }
  336. if (def)
  337. printf("y\n");
  338. else {
  339. printf("n\n");
  340. errors_uncorrected = 1;
  341. }
  342. return def;
  343. }
  344. /*
  345. * Make certain that we aren't checking a filesystem that is on a
  346. * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
  347. * 1994 Theodore Ts'o. Also licensed under GPL.
  348. */
  349. static void check_mount(void)
  350. {
  351. if (find_mount_point(device_name, 0)) {
  352. int cont;
  353. #if ENABLE_FEATURE_MTAB_SUPPORT
  354. /*
  355. * If the root is mounted read-only, then /etc/mtab is
  356. * probably not correct; so we won't issue a warning based on
  357. * it.
  358. */
  359. int fd = open(bb_path_mtab_file, O_RDWR);
  360. if (fd < 0 && errno == EROFS)
  361. return;
  362. close(fd);
  363. #endif
  364. printf("%s is mounted. ", device_name);
  365. cont = 0;
  366. if (isatty(0) && isatty(1))
  367. cont = ask("Do you really want to continue", 0);
  368. if (!cont) {
  369. printf("Check aborted\n");
  370. exit(EXIT_SUCCESS);
  371. }
  372. }
  373. }
  374. /*
  375. * check_zone_nr checks to see that *nr is a valid zone nr. If it
  376. * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
  377. * if an error was corrected, and returns the zone (0 for no zone
  378. * or a bad zone-number).
  379. */
  380. static int check_zone_nr2(uint32_t *nr, smallint *corrected)
  381. {
  382. const char *msg;
  383. if (!*nr)
  384. return 0;
  385. if (*nr < FIRSTZONE)
  386. msg = "< FIRSTZONE";
  387. else if (*nr >= ZONES)
  388. msg = ">= ZONES";
  389. else
  390. return *nr;
  391. printf("Zone nr %s in file '%s'. ", msg, current_name);
  392. if (ask("Remove block", 1)) {
  393. *nr = 0;
  394. *corrected = 1;
  395. }
  396. return 0;
  397. }
  398. static int check_zone_nr(uint16_t *nr, smallint *corrected)
  399. {
  400. uint32_t nr32 = *nr;
  401. int r = check_zone_nr2(&nr32, corrected);
  402. *nr = (uint16_t)nr32;
  403. return r;
  404. }
  405. /*
  406. * read-block reads block nr into the buffer at addr.
  407. */
  408. static void read_block(unsigned nr, void *addr)
  409. {
  410. if (!nr) {
  411. memset(addr, 0, BLOCK_SIZE);
  412. return;
  413. }
  414. xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
  415. if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
  416. printf("%s: bad block %u in file '%s'\n",
  417. bb_msg_read_error, nr, current_name);
  418. errors_uncorrected = 1;
  419. memset(addr, 0, BLOCK_SIZE);
  420. }
  421. }
  422. /*
  423. * write_block writes block nr to disk.
  424. */
  425. static void write_block(unsigned nr, void *addr)
  426. {
  427. if (!nr)
  428. return;
  429. if (nr < FIRSTZONE || nr >= ZONES) {
  430. printf("Internal error: trying to write bad block\n"
  431. "Write request ignored\n");
  432. errors_uncorrected = 1;
  433. return;
  434. }
  435. xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
  436. if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
  437. printf("%s: bad block %u in file '%s'\n",
  438. bb_msg_write_error, nr, current_name);
  439. errors_uncorrected = 1;
  440. }
  441. }
  442. /*
  443. * map_block calculates the absolute block nr of a block in a file.
  444. * It sets 'changed' if the inode has needed changing, and re-writes
  445. * any indirect blocks with errors.
  446. */
  447. static int map_block(struct minix1_inode *inode, unsigned blknr)
  448. {
  449. uint16_t ind[BLOCK_SIZE >> 1];
  450. int block, result;
  451. smallint blk_chg;
  452. if (blknr < 7)
  453. return check_zone_nr(inode->i_zone + blknr, &changed);
  454. blknr -= 7;
  455. if (blknr < 512) {
  456. block = check_zone_nr(inode->i_zone + 7, &changed);
  457. goto common;
  458. }
  459. blknr -= 512;
  460. block = check_zone_nr(inode->i_zone + 8, &changed);
  461. read_block(block, ind); /* double indirect */
  462. blk_chg = 0;
  463. result = check_zone_nr(&ind[blknr / 512], &blk_chg);
  464. if (blk_chg)
  465. write_block(block, ind);
  466. block = result;
  467. common:
  468. read_block(block, ind);
  469. blk_chg = 0;
  470. result = check_zone_nr(&ind[blknr % 512], &blk_chg);
  471. if (blk_chg)
  472. write_block(block, ind);
  473. return result;
  474. }
  475. #if ENABLE_FEATURE_MINIX2
  476. static int map_block2(struct minix2_inode *inode, unsigned blknr)
  477. {
  478. uint32_t ind[BLOCK_SIZE >> 2];
  479. int block, result;
  480. smallint blk_chg;
  481. if (blknr < 7)
  482. return check_zone_nr2(inode->i_zone + blknr, &changed);
  483. blknr -= 7;
  484. if (blknr < 256) {
  485. block = check_zone_nr2(inode->i_zone + 7, &changed);
  486. goto common2;
  487. }
  488. blknr -= 256;
  489. if (blknr < 256 * 256) {
  490. block = check_zone_nr2(inode->i_zone + 8, &changed);
  491. goto common1;
  492. }
  493. blknr -= 256 * 256;
  494. block = check_zone_nr2(inode->i_zone + 9, &changed);
  495. read_block(block, ind); /* triple indirect */
  496. blk_chg = 0;
  497. result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg);
  498. if (blk_chg)
  499. write_block(block, ind);
  500. block = result;
  501. common1:
  502. read_block(block, ind); /* double indirect */
  503. blk_chg = 0;
  504. result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
  505. if (blk_chg)
  506. write_block(block, ind);
  507. block = result;
  508. common2:
  509. read_block(block, ind);
  510. blk_chg = 0;
  511. result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
  512. if (blk_chg)
  513. write_block(block, ind);
  514. return result;
  515. }
  516. #endif
  517. static void write_superblock(void)
  518. {
  519. /*
  520. * Set the state of the filesystem based on whether or not there
  521. * are uncorrected errors. The filesystem valid flag is
  522. * unconditionally set if we get this far.
  523. */
  524. Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
  525. if (!errors_uncorrected)
  526. Super.s_state &= ~MINIX_ERROR_FS;
  527. xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
  528. if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
  529. die("can't write superblock");
  530. }
  531. static void write_tables(void)
  532. {
  533. write_superblock();
  534. if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
  535. die("can't write inode map");
  536. if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
  537. die("can't write zone map");
  538. if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
  539. die("can't write inodes");
  540. }
  541. static void get_dirsize(void)
  542. {
  543. int block;
  544. char blk[BLOCK_SIZE];
  545. int size;
  546. #if ENABLE_FEATURE_MINIX2
  547. if (version2)
  548. block = Inode2[MINIX_ROOT_INO].i_zone[0];
  549. else
  550. #endif
  551. block = Inode1[MINIX_ROOT_INO].i_zone[0];
  552. read_block(block, blk);
  553. for (size = 16; size < BLOCK_SIZE; size <<= 1) {
  554. if (strcmp(blk + size + 2, "..") == 0) {
  555. dirsize = size;
  556. namelen = size - 2;
  557. return;
  558. }
  559. }
  560. /* use defaults */
  561. }
  562. static void read_superblock(void)
  563. {
  564. xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
  565. if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
  566. die("can't read superblock");
  567. /* already initialized to:
  568. namelen = 14;
  569. dirsize = 16;
  570. version2 = 0;
  571. */
  572. if (MAGIC == MINIX1_SUPER_MAGIC) {
  573. } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
  574. namelen = 30;
  575. dirsize = 32;
  576. #if ENABLE_FEATURE_MINIX2
  577. } else if (MAGIC == MINIX2_SUPER_MAGIC) {
  578. version2 = 1;
  579. } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
  580. namelen = 30;
  581. dirsize = 32;
  582. version2 = 1;
  583. #endif
  584. } else
  585. die("bad magic number in superblock");
  586. if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
  587. die("only 1k blocks/zones supported");
  588. if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
  589. die("bad s_imap_blocks field in superblock");
  590. if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
  591. die("bad s_zmap_blocks field in superblock");
  592. }
  593. static void read_tables(void)
  594. {
  595. inode_map = xzalloc(IMAPS * BLOCK_SIZE);
  596. zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
  597. inode_buffer = xmalloc(INODE_BUFFER_SIZE);
  598. inode_count = xmalloc(INODES + 1);
  599. zone_count = xmalloc(ZONES);
  600. if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
  601. die("can't read inode map");
  602. if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
  603. die("can't read zone map");
  604. if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
  605. die("can't read inodes");
  606. if (NORM_FIRSTZONE != FIRSTZONE) {
  607. printf("warning: firstzone!=norm_firstzone\n");
  608. errors_uncorrected = 1;
  609. }
  610. get_dirsize();
  611. if (OPT_show) {
  612. printf("%u inodes\n"
  613. "%u blocks\n"
  614. "Firstdatazone=%u (%u)\n"
  615. "Zonesize=%u\n"
  616. "Maxsize=%u\n"
  617. "Filesystem state=%u\n"
  618. "namelen=%u\n\n",
  619. INODES,
  620. ZONES,
  621. FIRSTZONE, NORM_FIRSTZONE,
  622. BLOCK_SIZE << ZONESIZE,
  623. MAXSIZE,
  624. Super.s_state,
  625. namelen);
  626. }
  627. }
  628. static void get_inode_common(unsigned nr, uint16_t i_mode)
  629. {
  630. total++;
  631. if (!inode_count[nr]) {
  632. if (!inode_in_use(nr)) {
  633. printf("Inode %d is marked as 'unused', but it is used "
  634. "for file '%s'\n", nr, current_name);
  635. if (OPT_repair) {
  636. if (ask("Mark as 'in use'", 1))
  637. mark_inode(nr);
  638. else
  639. errors_uncorrected = 1;
  640. }
  641. }
  642. if (S_ISDIR(i_mode))
  643. directory++;
  644. else if (S_ISREG(i_mode))
  645. regular++;
  646. else if (S_ISCHR(i_mode))
  647. chardev++;
  648. else if (S_ISBLK(i_mode))
  649. blockdev++;
  650. else if (S_ISLNK(i_mode))
  651. symlinks++;
  652. else if (S_ISSOCK(i_mode));
  653. else if (S_ISFIFO(i_mode));
  654. else {
  655. printf("%s has mode %05o\n", current_name, i_mode);
  656. }
  657. } else
  658. links++;
  659. if (!++inode_count[nr]) {
  660. printf("Warning: inode count too big\n");
  661. inode_count[nr]--;
  662. errors_uncorrected = 1;
  663. }
  664. }
  665. static struct minix1_inode *get_inode(unsigned nr)
  666. {
  667. struct minix1_inode *inode;
  668. if (!nr || nr > INODES)
  669. return NULL;
  670. inode = Inode1 + nr;
  671. get_inode_common(nr, inode->i_mode);
  672. return inode;
  673. }
  674. #if ENABLE_FEATURE_MINIX2
  675. static struct minix2_inode *get_inode2(unsigned nr)
  676. {
  677. struct minix2_inode *inode;
  678. if (!nr || nr > INODES)
  679. return NULL;
  680. inode = Inode2 + nr;
  681. get_inode_common(nr, inode->i_mode);
  682. return inode;
  683. }
  684. #endif
  685. static void check_root(void)
  686. {
  687. struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
  688. if (!inode || !S_ISDIR(inode->i_mode))
  689. die("root inode isn't a directory");
  690. }
  691. #if ENABLE_FEATURE_MINIX2
  692. static void check_root2(void)
  693. {
  694. struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
  695. if (!inode || !S_ISDIR(inode->i_mode))
  696. die("root inode isn't a directory");
  697. }
  698. #else
  699. void check_root2(void);
  700. #endif
  701. static int add_zone_common(int block, smallint *corrected)
  702. {
  703. if (!block)
  704. return 0;
  705. if (zone_count[block]) {
  706. printf("Already used block is reused in file '%s'. ",
  707. current_name);
  708. if (ask("Clear", 1)) {
  709. block = 0;
  710. *corrected = 1;
  711. return -1; /* "please zero out *znr" */
  712. }
  713. }
  714. if (!zone_in_use(block)) {
  715. printf("Block %d in file '%s' is marked as 'unused'. ",
  716. block, current_name);
  717. if (ask("Correct", 1))
  718. mark_zone(block);
  719. }
  720. if (!++zone_count[block])
  721. zone_count[block]--;
  722. return block;
  723. }
  724. static int add_zone(uint16_t *znr, smallint *corrected)
  725. {
  726. int block;
  727. block = check_zone_nr(znr, corrected);
  728. block = add_zone_common(block, corrected);
  729. if (block == -1) {
  730. *znr = 0;
  731. block = 0;
  732. }
  733. return block;
  734. }
  735. #if ENABLE_FEATURE_MINIX2
  736. static int add_zone2(uint32_t *znr, smallint *corrected)
  737. {
  738. int block;
  739. block = check_zone_nr2(znr, corrected);
  740. block = add_zone_common(block, corrected);
  741. if (block == -1) {
  742. *znr = 0;
  743. block = 0;
  744. }
  745. return block;
  746. }
  747. #endif
  748. static void add_zone_ind(uint16_t *znr, smallint *corrected)
  749. {
  750. int i;
  751. int block;
  752. smallint chg_blk = 0;
  753. block = add_zone(znr, corrected);
  754. if (!block)
  755. return;
  756. read_block(block, add_zone_ind_blk);
  757. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  758. add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
  759. if (chg_blk)
  760. write_block(block, add_zone_ind_blk);
  761. }
  762. #if ENABLE_FEATURE_MINIX2
  763. static void add_zone_ind2(uint32_t *znr, smallint *corrected)
  764. {
  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, add_zone_ind_blk);
  772. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  773. add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
  774. if (chg_blk)
  775. write_block(block, add_zone_ind_blk);
  776. }
  777. #endif
  778. static void add_zone_dind(uint16_t *znr, smallint *corrected)
  779. {
  780. int i;
  781. int block;
  782. smallint chg_blk = 0;
  783. block = add_zone(znr, corrected);
  784. if (!block)
  785. return;
  786. read_block(block, add_zone_dind_blk);
  787. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  788. add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
  789. if (chg_blk)
  790. write_block(block, add_zone_dind_blk);
  791. }
  792. #if ENABLE_FEATURE_MINIX2
  793. static void add_zone_dind2(uint32_t *znr, smallint *corrected)
  794. {
  795. int i;
  796. int block;
  797. smallint chg_blk = 0;
  798. block = add_zone2(znr, corrected);
  799. if (!block)
  800. return;
  801. read_block(block, add_zone_dind_blk);
  802. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  803. add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
  804. if (chg_blk)
  805. write_block(block, add_zone_dind_blk);
  806. }
  807. static void add_zone_tind2(uint32_t *znr, smallint *corrected)
  808. {
  809. int i;
  810. int block;
  811. smallint chg_blk = 0;
  812. block = add_zone2(znr, corrected);
  813. if (!block)
  814. return;
  815. read_block(block, add_zone_tind_blk);
  816. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  817. add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
  818. if (chg_blk)
  819. write_block(block, add_zone_tind_blk);
  820. }
  821. #endif
  822. static void check_zones(unsigned i)
  823. {
  824. struct minix1_inode *inode;
  825. if (!i || i > INODES)
  826. return;
  827. if (inode_count[i] > 1) /* have we counted this file already? */
  828. return;
  829. inode = Inode1 + i;
  830. if (!S_ISDIR(inode->i_mode)
  831. && !S_ISREG(inode->i_mode)
  832. && !S_ISLNK(inode->i_mode)
  833. ) {
  834. return;
  835. }
  836. for (i = 0; i < 7; i++)
  837. add_zone(i + inode->i_zone, &changed);
  838. add_zone_ind(7 + inode->i_zone, &changed);
  839. add_zone_dind(8 + inode->i_zone, &changed);
  840. }
  841. #if ENABLE_FEATURE_MINIX2
  842. static void check_zones2(unsigned i)
  843. {
  844. struct minix2_inode *inode;
  845. if (!i || i > INODES)
  846. return;
  847. if (inode_count[i] > 1) /* have we counted this file already? */
  848. return;
  849. inode = Inode2 + i;
  850. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
  851. && !S_ISLNK(inode->i_mode))
  852. return;
  853. for (i = 0; i < 7; i++)
  854. add_zone2(i + inode->i_zone, &changed);
  855. add_zone_ind2(7 + inode->i_zone, &changed);
  856. add_zone_dind2(8 + inode->i_zone, &changed);
  857. add_zone_tind2(9 + inode->i_zone, &changed);
  858. }
  859. #endif
  860. static void check_file(struct minix1_inode *dir, unsigned offset)
  861. {
  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, check_file_blk);
  868. name = check_file_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, check_file_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 (OPT_list) {
  898. if (OPT_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. struct minix2_inode *inode;
  911. int ino;
  912. char *name;
  913. int block;
  914. block = map_block2(dir, offset / BLOCK_SIZE);
  915. read_block(block, check_file_blk);
  916. name = check_file_blk + (offset % BLOCK_SIZE) + 2;
  917. ino = *(uint16_t *) (name - 2);
  918. if (ino > INODES) {
  919. printf("%s contains a bad inode number for file '%.*s'. ",
  920. current_name, namelen, name);
  921. if (ask("Remove", 1)) {
  922. *(uint16_t *) (name - 2) = 0;
  923. write_block(block, check_file_blk);
  924. }
  925. ino = 0;
  926. }
  927. push_filename(name);
  928. inode = get_inode2(ino);
  929. pop_filename();
  930. if (!offset) {
  931. if (inode && LONE_CHAR(name, '.'))
  932. return;
  933. printf("%s: bad directory: '.' isn't first\n", current_name);
  934. errors_uncorrected = 1;
  935. }
  936. if (offset == dirsize) {
  937. if (inode && strcmp("..", name) == 0)
  938. return;
  939. printf("%s: bad directory: '..' isn't second\n", current_name);
  940. errors_uncorrected = 1;
  941. }
  942. if (!inode)
  943. return;
  944. push_filename(name);
  945. if (OPT_list) {
  946. if (OPT_verbose)
  947. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  948. printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
  949. }
  950. check_zones2(ino);
  951. if (inode && S_ISDIR(inode->i_mode))
  952. recursive_check2(ino);
  953. pop_filename();
  954. }
  955. #endif
  956. static void recursive_check(unsigned ino)
  957. {
  958. struct minix1_inode *dir;
  959. unsigned offset;
  960. dir = Inode1 + ino;
  961. if (!S_ISDIR(dir->i_mode))
  962. die("internal error");
  963. if (dir->i_size < 2 * dirsize) {
  964. printf("%s: bad directory: size<32", current_name);
  965. errors_uncorrected = 1;
  966. }
  967. for (offset = 0; offset < dir->i_size; offset += dirsize)
  968. check_file(dir, offset);
  969. }
  970. #if ENABLE_FEATURE_MINIX2
  971. static void recursive_check2(unsigned ino)
  972. {
  973. struct minix2_inode *dir;
  974. unsigned offset;
  975. dir = Inode2 + ino;
  976. if (!S_ISDIR(dir->i_mode))
  977. die("internal error");
  978. if (dir->i_size < 2 * dirsize) {
  979. printf("%s: bad directory: size<32", current_name);
  980. errors_uncorrected = 1;
  981. }
  982. for (offset = 0; offset < dir->i_size; offset += dirsize)
  983. check_file2(dir, offset);
  984. }
  985. #endif
  986. static int bad_zone(int i)
  987. {
  988. char buffer[BLOCK_SIZE];
  989. xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
  990. return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
  991. }
  992. static void check_counts(void)
  993. {
  994. int i;
  995. for (i = 1; i <= INODES; i++) {
  996. if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
  997. printf("Inode %d has non-zero mode. ", i);
  998. if (ask("Clear", 1)) {
  999. Inode1[i].i_mode = 0;
  1000. changed = 1;
  1001. }
  1002. }
  1003. if (!inode_count[i]) {
  1004. if (!inode_in_use(i))
  1005. continue;
  1006. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  1007. if (ask("Clear", 1))
  1008. unmark_inode(i);
  1009. continue;
  1010. }
  1011. if (!inode_in_use(i)) {
  1012. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1013. if (ask("Set", 1))
  1014. mark_inode(i);
  1015. }
  1016. if (Inode1[i].i_nlinks != inode_count[i]) {
  1017. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1018. i, Inode1[i].i_mode, Inode1[i].i_nlinks,
  1019. inode_count[i]);
  1020. if (ask("Set i_nlinks to count", 1)) {
  1021. Inode1[i].i_nlinks = inode_count[i];
  1022. changed = 1;
  1023. }
  1024. }
  1025. }
  1026. for (i = FIRSTZONE; i < ZONES; i++) {
  1027. if ((zone_in_use(i) != 0) == zone_count[i])
  1028. continue;
  1029. if (!zone_count[i]) {
  1030. if (bad_zone(i))
  1031. continue;
  1032. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1033. if (ask("Unmark", 1))
  1034. unmark_zone(i);
  1035. continue;
  1036. }
  1037. printf("Zone %d: %sin use, counted=%d\n",
  1038. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1039. }
  1040. }
  1041. #if ENABLE_FEATURE_MINIX2
  1042. static void check_counts2(void)
  1043. {
  1044. int i;
  1045. for (i = 1; i <= INODES; i++) {
  1046. if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
  1047. printf("Inode %d has non-zero mode. ", i);
  1048. if (ask("Clear", 1)) {
  1049. Inode2[i].i_mode = 0;
  1050. changed = 1;
  1051. }
  1052. }
  1053. if (!inode_count[i]) {
  1054. if (!inode_in_use(i))
  1055. continue;
  1056. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  1057. if (ask("Clear", 1))
  1058. unmark_inode(i);
  1059. continue;
  1060. }
  1061. if (!inode_in_use(i)) {
  1062. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1063. if (ask("Set", 1))
  1064. mark_inode(i);
  1065. }
  1066. if (Inode2[i].i_nlinks != inode_count[i]) {
  1067. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1068. i, Inode2[i].i_mode, Inode2[i].i_nlinks,
  1069. inode_count[i]);
  1070. if (ask("Set i_nlinks to count", 1)) {
  1071. Inode2[i].i_nlinks = inode_count[i];
  1072. changed = 1;
  1073. }
  1074. }
  1075. }
  1076. for (i = FIRSTZONE; i < ZONES; i++) {
  1077. if ((zone_in_use(i) != 0) == zone_count[i])
  1078. continue;
  1079. if (!zone_count[i]) {
  1080. if (bad_zone(i))
  1081. continue;
  1082. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1083. if (ask("Unmark", 1))
  1084. unmark_zone(i);
  1085. continue;
  1086. }
  1087. printf("Zone %d: %sin use, counted=%d\n",
  1088. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1089. }
  1090. }
  1091. #endif
  1092. static void check(void)
  1093. {
  1094. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1095. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1096. check_zones(MINIX_ROOT_INO);
  1097. recursive_check(MINIX_ROOT_INO);
  1098. check_counts();
  1099. }
  1100. #if ENABLE_FEATURE_MINIX2
  1101. static void check2(void)
  1102. {
  1103. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1104. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1105. check_zones2(MINIX_ROOT_INO);
  1106. recursive_check2(MINIX_ROOT_INO);
  1107. check_counts2();
  1108. }
  1109. #else
  1110. void check2(void);
  1111. #endif
  1112. int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1113. int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
  1114. {
  1115. struct termios tmp;
  1116. int retcode = 0;
  1117. xfunc_error_retval = 8;
  1118. INIT_G();
  1119. opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
  1120. getopt32(argv, OPTION_STR);
  1121. argv += optind;
  1122. device_name = argv[0];
  1123. check_mount(); /* trying to check a mounted filesystem? */
  1124. if (OPT_manual) {
  1125. if (!isatty(0) || !isatty(1))
  1126. die("need terminal for interactive repairs");
  1127. }
  1128. xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
  1129. /*sync(); paranoia? */
  1130. read_superblock();
  1131. /*
  1132. * Determine whether or not we should continue with the checking.
  1133. * This is based on the status of the filesystem valid and error
  1134. * flags and whether or not the -f switch was specified on the
  1135. * command line.
  1136. */
  1137. printf("%s: %s\n", applet_name, bb_banner);
  1138. if (!(Super.s_state & MINIX_ERROR_FS)
  1139. && (Super.s_state & MINIX_VALID_FS) && !OPT_force
  1140. ) {
  1141. if (OPT_repair)
  1142. printf("%s is clean, check is skipped\n", device_name);
  1143. return 0;
  1144. } else if (OPT_force)
  1145. printf("Forcing filesystem check on %s\n", device_name);
  1146. else if (OPT_repair)
  1147. printf("Filesystem on %s is dirty, needs checking\n",
  1148. device_name);
  1149. read_tables();
  1150. if (OPT_manual) {
  1151. tcgetattr(0, &sv_termios);
  1152. tmp = sv_termios;
  1153. tmp.c_lflag &= ~(ICANON | ECHO);
  1154. tcsetattr_stdin_TCSANOW(&tmp);
  1155. termios_set = 1;
  1156. }
  1157. if (version2) {
  1158. check_root2();
  1159. check2();
  1160. } else {
  1161. check_root();
  1162. check();
  1163. }
  1164. if (OPT_verbose) {
  1165. int i, free_cnt;
  1166. for (i = 1, free_cnt = 0; i <= INODES; i++)
  1167. if (!inode_in_use(i))
  1168. free_cnt++;
  1169. printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
  1170. 100 * (INODES - free_cnt) / INODES);
  1171. for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
  1172. if (!zone_in_use(i))
  1173. free_cnt++;
  1174. printf("%6u zones used (%u%%)\n\n"
  1175. "%6u regular files\n"
  1176. "%6u directories\n"
  1177. "%6u character device files\n"
  1178. "%6u block device files\n"
  1179. "%6u links\n"
  1180. "%6u symbolic links\n"
  1181. "------\n"
  1182. "%6u files\n",
  1183. (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
  1184. regular, directory, chardev, blockdev,
  1185. links - 2 * directory + 1, symlinks,
  1186. total - 2 * directory + 1);
  1187. }
  1188. if (changed) {
  1189. write_tables();
  1190. printf("FILE SYSTEM HAS BEEN CHANGED\n");
  1191. sync();
  1192. } else if (OPT_repair)
  1193. write_superblock();
  1194. if (OPT_manual)
  1195. tcsetattr_stdin_TCSANOW(&sv_termios);
  1196. if (changed)
  1197. retcode += 3;
  1198. if (errors_uncorrected)
  1199. retcode += 4;
  1200. return retcode;
  1201. }