fsck_minix.c 32 KB

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