fsck_minix.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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 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 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 <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 super_block_buffer[BLOCK_SIZE];
  138. char add_zone_ind_blk[BLOCK_SIZE];
  139. char add_zone_dind_blk[BLOCK_SIZE];
  140. USE_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 super_block_buffer (G.super_block_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_super_block *)(super_block_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) ATTRIBUTE_NORETURN;
  259. static void die(const char *str)
  260. {
  261. if (termios_set)
  262. tcsetattr(0, 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(stdout);
  307. c = getchar();
  308. if (c == EOF) {
  309. if (!def)
  310. errors_uncorrected = 1;
  311. return def;
  312. }
  313. c = toupper(c);
  314. if (c == 'Y') {
  315. def = 1;
  316. break;
  317. } else if (c == 'N') {
  318. def = 0;
  319. break;
  320. } else if (c == ' ' || c == '\n')
  321. break;
  322. }
  323. if (def)
  324. printf("y\n");
  325. else {
  326. printf("n\n");
  327. errors_uncorrected = 1;
  328. }
  329. return def;
  330. }
  331. /*
  332. * Make certain that we aren't checking a filesystem that is on a
  333. * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
  334. * 1994 Theodore Ts'o. Also licensed under GPL.
  335. */
  336. static void check_mount(void)
  337. {
  338. FILE *f;
  339. struct mntent *mnt;
  340. int cont;
  341. int fd;
  342. //XXX:FIXME use find_mount_point()
  343. f = setmntent(MOUNTED, "r");
  344. if (f == NULL)
  345. return;
  346. while ((mnt = getmntent(f)) != NULL)
  347. if (strcmp(device_name, mnt->mnt_fsname) == 0)
  348. break;
  349. endmntent(f);
  350. if (!mnt)
  351. return;
  352. /*
  353. * If the root is mounted read-only, then /etc/mtab is
  354. * probably not correct; so we won't issue a warning based on
  355. * it.
  356. */
  357. fd = open(MOUNTED, O_RDWR);
  358. if (fd < 0 && errno == EROFS)
  359. return;
  360. close(fd);
  361. printf("%s is mounted. ", device_name);
  362. cont = 0;
  363. if (isatty(0) && isatty(1))
  364. cont = ask("Do you really want to continue", 0);
  365. if (!cont) {
  366. printf("Check aborted\n");
  367. exit(EXIT_SUCCESS);
  368. }
  369. }
  370. /*
  371. * check_zone_nr checks to see that *nr is a valid zone nr. If it
  372. * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
  373. * if an error was corrected, and returns the zone (0 for no zone
  374. * or a bad zone-number).
  375. */
  376. static int check_zone_nr2(uint32_t *nr, smallint *corrected)
  377. {
  378. const char *msg;
  379. if (!*nr)
  380. return 0;
  381. if (*nr < FIRSTZONE)
  382. msg = "< FIRSTZONE";
  383. else if (*nr >= ZONES)
  384. msg = ">= ZONES";
  385. else
  386. return *nr;
  387. printf("Zone nr %s in file '%s'. ", msg, current_name);
  388. if (ask("Remove block", 1)) {
  389. *nr = 0;
  390. *corrected = 1;
  391. }
  392. return 0;
  393. }
  394. static int check_zone_nr(uint16_t *nr, smallint *corrected)
  395. {
  396. uint32_t nr32 = *nr;
  397. int r = check_zone_nr2(&nr32, corrected);
  398. *nr = (uint16_t)nr32;
  399. return r;
  400. }
  401. /*
  402. * read-block reads block nr into the buffer at addr.
  403. */
  404. static void read_block(unsigned nr, void *addr)
  405. {
  406. if (!nr) {
  407. memset(addr, 0, BLOCK_SIZE);
  408. return;
  409. }
  410. xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
  411. if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
  412. printf("%s: bad block %u in file '%s'\n",
  413. bb_msg_read_error, nr, current_name);
  414. errors_uncorrected = 1;
  415. memset(addr, 0, BLOCK_SIZE);
  416. }
  417. }
  418. /*
  419. * write_block writes block nr to disk.
  420. */
  421. static void write_block(unsigned nr, void *addr)
  422. {
  423. if (!nr)
  424. return;
  425. if (nr < FIRSTZONE || nr >= ZONES) {
  426. printf("Internal error: trying to write bad block\n"
  427. "Write request ignored\n");
  428. errors_uncorrected = 1;
  429. return;
  430. }
  431. xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
  432. if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
  433. printf("%s: bad block %u in file '%s'\n",
  434. bb_msg_write_error, nr, current_name);
  435. errors_uncorrected = 1;
  436. }
  437. }
  438. /*
  439. * map_block calculates the absolute block nr of a block in a file.
  440. * It sets 'changed' if the inode has needed changing, and re-writes
  441. * any indirect blocks with errors.
  442. */
  443. static int map_block(struct minix1_inode *inode, unsigned blknr)
  444. {
  445. uint16_t ind[BLOCK_SIZE >> 1];
  446. int block, result;
  447. smallint blk_chg;
  448. if (blknr < 7)
  449. return check_zone_nr(inode->i_zone + blknr, &changed);
  450. blknr -= 7;
  451. if (blknr < 512) {
  452. block = check_zone_nr(inode->i_zone + 7, &changed);
  453. goto common;
  454. }
  455. blknr -= 512;
  456. block = check_zone_nr(inode->i_zone + 8, &changed);
  457. read_block(block, ind); /* double indirect */
  458. blk_chg = 0;
  459. result = check_zone_nr(&ind[blknr / 512], &blk_chg);
  460. if (blk_chg)
  461. write_block(block, ind);
  462. block = result;
  463. common:
  464. read_block(block, ind);
  465. blk_chg = 0;
  466. result = check_zone_nr(&ind[blknr % 512], &blk_chg);
  467. if (blk_chg)
  468. write_block(block, ind);
  469. return result;
  470. }
  471. #if ENABLE_FEATURE_MINIX2
  472. static int map_block2(struct minix2_inode *inode, unsigned blknr)
  473. {
  474. uint32_t ind[BLOCK_SIZE >> 2];
  475. int block, result;
  476. smallint blk_chg;
  477. if (blknr < 7)
  478. return check_zone_nr2(inode->i_zone + blknr, &changed);
  479. blknr -= 7;
  480. if (blknr < 256) {
  481. block = check_zone_nr2(inode->i_zone + 7, &changed);
  482. goto common2;
  483. }
  484. blknr -= 256;
  485. if (blknr < 256 * 256) {
  486. block = check_zone_nr2(inode->i_zone + 8, &changed);
  487. goto common1;
  488. }
  489. blknr -= 256 * 256;
  490. block = check_zone_nr2(inode->i_zone + 9, &changed);
  491. read_block(block, ind); /* triple 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. common1:
  498. read_block(block, ind); /* double indirect */
  499. blk_chg = 0;
  500. result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
  501. if (blk_chg)
  502. write_block(block, ind);
  503. block = result;
  504. common2:
  505. read_block(block, ind);
  506. blk_chg = 0;
  507. result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
  508. if (blk_chg)
  509. write_block(block, ind);
  510. return result;
  511. }
  512. #endif
  513. static void write_super_block(void)
  514. {
  515. /*
  516. * Set the state of the filesystem based on whether or not there
  517. * are uncorrected errors. The filesystem valid flag is
  518. * unconditionally set if we get this far.
  519. */
  520. Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
  521. if (!errors_uncorrected)
  522. Super.s_state &= ~MINIX_ERROR_FS;
  523. xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
  524. if (BLOCK_SIZE != full_write(dev_fd, super_block_buffer, BLOCK_SIZE))
  525. die("cannot write super-block");
  526. }
  527. static void write_tables(void)
  528. {
  529. write_super_block();
  530. if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
  531. die("cannot write inode map");
  532. if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
  533. die("cannot write zone map");
  534. if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
  535. die("cannot write inodes");
  536. }
  537. static void get_dirsize(void)
  538. {
  539. int block;
  540. char blk[BLOCK_SIZE];
  541. int size;
  542. #if ENABLE_FEATURE_MINIX2
  543. if (version2)
  544. block = Inode2[MINIX_ROOT_INO].i_zone[0];
  545. else
  546. #endif
  547. block = Inode1[MINIX_ROOT_INO].i_zone[0];
  548. read_block(block, blk);
  549. for (size = 16; size < BLOCK_SIZE; size <<= 1) {
  550. if (strcmp(blk + size + 2, "..") == 0) {
  551. dirsize = size;
  552. namelen = size - 2;
  553. return;
  554. }
  555. }
  556. /* use defaults */
  557. }
  558. static void read_superblock(void)
  559. {
  560. xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
  561. if (BLOCK_SIZE != full_read(dev_fd, super_block_buffer, BLOCK_SIZE))
  562. die("cannot read super block");
  563. /* already initialized to:
  564. namelen = 14;
  565. dirsize = 16;
  566. version2 = 0;
  567. */
  568. if (MAGIC == MINIX1_SUPER_MAGIC) {
  569. } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
  570. namelen = 30;
  571. dirsize = 32;
  572. #if ENABLE_FEATURE_MINIX2
  573. } else if (MAGIC == MINIX2_SUPER_MAGIC) {
  574. version2 = 1;
  575. } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
  576. namelen = 30;
  577. dirsize = 32;
  578. version2 = 1;
  579. #endif
  580. } else
  581. die("bad magic number in super-block");
  582. if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
  583. die("only 1k blocks/zones supported");
  584. if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
  585. die("bad s_imap_blocks field in super-block");
  586. if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
  587. die("bad s_zmap_blocks field in super-block");
  588. }
  589. static void read_tables(void)
  590. {
  591. inode_map = xzalloc(IMAPS * BLOCK_SIZE);
  592. zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
  593. inode_buffer = xmalloc(INODE_BUFFER_SIZE);
  594. inode_count = xmalloc(INODES + 1);
  595. zone_count = xmalloc(ZONES);
  596. if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
  597. die("cannot read inode map");
  598. if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
  599. die("cannot read zone map");
  600. if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
  601. die("cannot read inodes");
  602. if (NORM_FIRSTZONE != FIRSTZONE) {
  603. printf("warning: firstzone!=norm_firstzone\n");
  604. errors_uncorrected = 1;
  605. }
  606. get_dirsize();
  607. if (OPT_show) {
  608. printf("%u inodes\n"
  609. "%u blocks\n"
  610. "Firstdatazone=%u (%u)\n"
  611. "Zonesize=%u\n"
  612. "Maxsize=%u\n"
  613. "Filesystem state=%u\n"
  614. "namelen=%u\n\n",
  615. INODES,
  616. ZONES,
  617. FIRSTZONE, NORM_FIRSTZONE,
  618. BLOCK_SIZE << ZONESIZE,
  619. MAXSIZE,
  620. Super.s_state,
  621. namelen);
  622. }
  623. }
  624. static void get_inode_common(unsigned nr, uint16_t i_mode)
  625. {
  626. total++;
  627. if (!inode_count[nr]) {
  628. if (!inode_in_use(nr)) {
  629. printf("Inode %d is marked as 'unused', but it is used "
  630. "for file '%s'\n", nr, current_name);
  631. if (OPT_repair) {
  632. if (ask("Mark as 'in use'", 1))
  633. mark_inode(nr);
  634. else
  635. errors_uncorrected = 1;
  636. }
  637. }
  638. if (S_ISDIR(i_mode))
  639. directory++;
  640. else if (S_ISREG(i_mode))
  641. regular++;
  642. else if (S_ISCHR(i_mode))
  643. chardev++;
  644. else if (S_ISBLK(i_mode))
  645. blockdev++;
  646. else if (S_ISLNK(i_mode))
  647. symlinks++;
  648. else if (S_ISSOCK(i_mode));
  649. else if (S_ISFIFO(i_mode));
  650. else {
  651. printf("%s has mode %05o\n", current_name, i_mode);
  652. }
  653. } else
  654. links++;
  655. if (!++inode_count[nr]) {
  656. printf("Warning: inode count too big\n");
  657. inode_count[nr]--;
  658. errors_uncorrected = 1;
  659. }
  660. }
  661. static struct minix1_inode *get_inode(unsigned nr)
  662. {
  663. struct minix1_inode *inode;
  664. if (!nr || nr > INODES)
  665. return NULL;
  666. inode = Inode1 + nr;
  667. get_inode_common(nr, inode->i_mode);
  668. return inode;
  669. }
  670. #if ENABLE_FEATURE_MINIX2
  671. static struct minix2_inode *get_inode2(unsigned nr)
  672. {
  673. struct minix2_inode *inode;
  674. if (!nr || nr > INODES)
  675. return NULL;
  676. inode = Inode2 + nr;
  677. get_inode_common(nr, inode->i_mode);
  678. return inode;
  679. }
  680. #endif
  681. static void check_root(void)
  682. {
  683. struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
  684. if (!inode || !S_ISDIR(inode->i_mode))
  685. die("root inode isn't a directory");
  686. }
  687. #if ENABLE_FEATURE_MINIX2
  688. static void check_root2(void)
  689. {
  690. struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
  691. if (!inode || !S_ISDIR(inode->i_mode))
  692. die("root inode isn't a directory");
  693. }
  694. #else
  695. void check_root2(void);
  696. #endif
  697. static int add_zone_common(int block, smallint *corrected)
  698. {
  699. if (!block)
  700. return 0;
  701. if (zone_count[block]) {
  702. printf("Already used block is reused in file '%s'. ",
  703. current_name);
  704. if (ask("Clear", 1)) {
  705. block = 0;
  706. *corrected = 1;
  707. return -1; /* "please zero out *znr" */
  708. }
  709. }
  710. if (!zone_in_use(block)) {
  711. printf("Block %d in file '%s' is marked as 'unused'. ",
  712. block, current_name);
  713. if (ask("Correct", 1))
  714. mark_zone(block);
  715. }
  716. if (!++zone_count[block])
  717. zone_count[block]--;
  718. return block;
  719. }
  720. static int add_zone(uint16_t *znr, smallint *corrected)
  721. {
  722. int block;
  723. block = check_zone_nr(znr, corrected);
  724. block = add_zone_common(block, corrected);
  725. if (block == -1) {
  726. *znr = 0;
  727. block = 0;
  728. }
  729. return block;
  730. }
  731. #if ENABLE_FEATURE_MINIX2
  732. static int add_zone2(uint32_t *znr, smallint *corrected)
  733. {
  734. int block;
  735. block = check_zone_nr2(znr, corrected);
  736. block = add_zone_common(block, corrected);
  737. if (block == -1) {
  738. *znr = 0;
  739. block = 0;
  740. }
  741. return block;
  742. }
  743. #endif
  744. static void add_zone_ind(uint16_t *znr, smallint *corrected)
  745. {
  746. int i;
  747. int block;
  748. smallint chg_blk = 0;
  749. block = add_zone(znr, corrected);
  750. if (!block)
  751. return;
  752. read_block(block, add_zone_ind_blk);
  753. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  754. add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
  755. if (chg_blk)
  756. write_block(block, add_zone_ind_blk);
  757. }
  758. #if ENABLE_FEATURE_MINIX2
  759. static void add_zone_ind2(uint32_t *znr, smallint *corrected)
  760. {
  761. int i;
  762. int block;
  763. smallint chg_blk = 0;
  764. block = add_zone2(znr, corrected);
  765. if (!block)
  766. return;
  767. read_block(block, add_zone_ind_blk);
  768. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  769. add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
  770. if (chg_blk)
  771. write_block(block, add_zone_ind_blk);
  772. }
  773. #endif
  774. static void add_zone_dind(uint16_t *znr, smallint *corrected)
  775. {
  776. int i;
  777. int block;
  778. smallint chg_blk = 0;
  779. block = add_zone(znr, corrected);
  780. if (!block)
  781. return;
  782. read_block(block, add_zone_dind_blk);
  783. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  784. add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
  785. if (chg_blk)
  786. write_block(block, add_zone_dind_blk);
  787. }
  788. #if ENABLE_FEATURE_MINIX2
  789. static void add_zone_dind2(uint32_t *znr, smallint *corrected)
  790. {
  791. int i;
  792. int block;
  793. smallint chg_blk = 0;
  794. block = add_zone2(znr, corrected);
  795. if (!block)
  796. return;
  797. read_block(block, add_zone_dind_blk);
  798. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  799. add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
  800. if (chg_blk)
  801. write_block(block, add_zone_dind_blk);
  802. }
  803. static void add_zone_tind2(uint32_t *znr, smallint *corrected)
  804. {
  805. int i;
  806. int block;
  807. smallint chg_blk = 0;
  808. block = add_zone2(znr, corrected);
  809. if (!block)
  810. return;
  811. read_block(block, add_zone_tind_blk);
  812. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  813. add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
  814. if (chg_blk)
  815. write_block(block, add_zone_tind_blk);
  816. }
  817. #endif
  818. static void check_zones(unsigned i)
  819. {
  820. struct minix1_inode *inode;
  821. if (!i || i > INODES)
  822. return;
  823. if (inode_count[i] > 1) /* have we counted this file already? */
  824. return;
  825. inode = Inode1 + i;
  826. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  827. !S_ISLNK(inode->i_mode)) return;
  828. for (i = 0; i < 7; i++)
  829. add_zone(i + inode->i_zone, &changed);
  830. add_zone_ind(7 + inode->i_zone, &changed);
  831. add_zone_dind(8 + inode->i_zone, &changed);
  832. }
  833. #if ENABLE_FEATURE_MINIX2
  834. static void check_zones2(unsigned i)
  835. {
  836. struct minix2_inode *inode;
  837. if (!i || i > INODES)
  838. return;
  839. if (inode_count[i] > 1) /* have we counted this file already? */
  840. return;
  841. inode = Inode2 + i;
  842. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
  843. && !S_ISLNK(inode->i_mode))
  844. return;
  845. for (i = 0; i < 7; i++)
  846. add_zone2(i + inode->i_zone, &changed);
  847. add_zone_ind2(7 + inode->i_zone, &changed);
  848. add_zone_dind2(8 + inode->i_zone, &changed);
  849. add_zone_tind2(9 + inode->i_zone, &changed);
  850. }
  851. #endif
  852. static void check_file(struct minix1_inode *dir, unsigned offset)
  853. {
  854. struct minix1_inode *inode;
  855. int ino;
  856. char *name;
  857. int block;
  858. block = map_block(dir, offset / BLOCK_SIZE);
  859. read_block(block, check_file_blk);
  860. name = check_file_blk + (offset % BLOCK_SIZE) + 2;
  861. ino = *(uint16_t *) (name - 2);
  862. if (ino > INODES) {
  863. printf("%s contains a bad inode number for file '%.*s'. ",
  864. current_name, namelen, name);
  865. if (ask("Remove", 1)) {
  866. *(uint16_t *) (name - 2) = 0;
  867. write_block(block, check_file_blk);
  868. }
  869. ino = 0;
  870. }
  871. push_filename(name);
  872. inode = get_inode(ino);
  873. pop_filename();
  874. if (!offset) {
  875. if (inode && LONE_CHAR(name, '.'))
  876. return;
  877. printf("%s: bad directory: '.' isn't first\n", current_name);
  878. errors_uncorrected = 1;
  879. }
  880. if (offset == dirsize) {
  881. if (inode && strcmp("..", name) == 0)
  882. return;
  883. printf("%s: bad directory: '..' isn't second\n", current_name);
  884. errors_uncorrected = 1;
  885. }
  886. if (!inode)
  887. return;
  888. push_filename(name);
  889. if (OPT_list) {
  890. if (OPT_verbose)
  891. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  892. printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
  893. }
  894. check_zones(ino);
  895. if (inode && S_ISDIR(inode->i_mode))
  896. recursive_check(ino);
  897. pop_filename();
  898. }
  899. #if ENABLE_FEATURE_MINIX2
  900. static void check_file2(struct minix2_inode *dir, unsigned offset)
  901. {
  902. struct minix2_inode *inode;
  903. int ino;
  904. char *name;
  905. int block;
  906. block = map_block2(dir, offset / BLOCK_SIZE);
  907. read_block(block, check_file_blk);
  908. name = check_file_blk + (offset % BLOCK_SIZE) + 2;
  909. ino = *(uint16_t *) (name - 2);
  910. if (ino > INODES) {
  911. printf("%s contains a bad inode number for file '%.*s'. ",
  912. current_name, namelen, name);
  913. if (ask("Remove", 1)) {
  914. *(uint16_t *) (name - 2) = 0;
  915. write_block(block, check_file_blk);
  916. }
  917. ino = 0;
  918. }
  919. push_filename(name);
  920. inode = get_inode2(ino);
  921. pop_filename();
  922. if (!offset) {
  923. if (inode && LONE_CHAR(name, '.'))
  924. return;
  925. printf("%s: bad directory: '.' isn't first\n", current_name);
  926. errors_uncorrected = 1;
  927. }
  928. if (offset == dirsize) {
  929. if (inode && strcmp("..", name) == 0)
  930. return;
  931. printf("%s: bad directory: '..' isn't second\n", current_name);
  932. errors_uncorrected = 1;
  933. }
  934. if (!inode)
  935. return;
  936. push_filename(name);
  937. if (OPT_list) {
  938. if (OPT_verbose)
  939. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  940. printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
  941. }
  942. check_zones2(ino);
  943. if (inode && S_ISDIR(inode->i_mode))
  944. recursive_check2(ino);
  945. pop_filename();
  946. }
  947. #endif
  948. static void recursive_check(unsigned ino)
  949. {
  950. struct minix1_inode *dir;
  951. unsigned offset;
  952. dir = Inode1 + ino;
  953. if (!S_ISDIR(dir->i_mode))
  954. die("internal error");
  955. if (dir->i_size < 2 * dirsize) {
  956. printf("%s: bad directory: size<32", current_name);
  957. errors_uncorrected = 1;
  958. }
  959. for (offset = 0; offset < dir->i_size; offset += dirsize)
  960. check_file(dir, offset);
  961. }
  962. #if ENABLE_FEATURE_MINIX2
  963. static void recursive_check2(unsigned ino)
  964. {
  965. struct minix2_inode *dir;
  966. unsigned offset;
  967. dir = Inode2 + ino;
  968. if (!S_ISDIR(dir->i_mode))
  969. die("internal error");
  970. if (dir->i_size < 2 * dirsize) {
  971. printf("%s: bad directory: size<32", current_name);
  972. errors_uncorrected = 1;
  973. }
  974. for (offset = 0; offset < dir->i_size; offset += dirsize)
  975. check_file2(dir, offset);
  976. }
  977. #endif
  978. static int bad_zone(int i)
  979. {
  980. char buffer[BLOCK_SIZE];
  981. xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
  982. return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
  983. }
  984. static void check_counts(void)
  985. {
  986. int i;
  987. for (i = 1; i <= INODES; i++) {
  988. if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
  989. printf("Inode %d has non-zero mode. ", i);
  990. if (ask("Clear", 1)) {
  991. Inode1[i].i_mode = 0;
  992. changed = 1;
  993. }
  994. }
  995. if (!inode_count[i]) {
  996. if (!inode_in_use(i))
  997. continue;
  998. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  999. if (ask("Clear", 1))
  1000. unmark_inode(i);
  1001. continue;
  1002. }
  1003. if (!inode_in_use(i)) {
  1004. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1005. if (ask("Set", 1))
  1006. mark_inode(i);
  1007. }
  1008. if (Inode1[i].i_nlinks != inode_count[i]) {
  1009. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1010. i, Inode1[i].i_mode, Inode1[i].i_nlinks,
  1011. inode_count[i]);
  1012. if (ask("Set i_nlinks to count", 1)) {
  1013. Inode1[i].i_nlinks = inode_count[i];
  1014. changed = 1;
  1015. }
  1016. }
  1017. }
  1018. for (i = FIRSTZONE; i < ZONES; i++) {
  1019. if ((zone_in_use(i) != 0) == zone_count[i])
  1020. continue;
  1021. if (!zone_count[i]) {
  1022. if (bad_zone(i))
  1023. continue;
  1024. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1025. if (ask("Unmark", 1))
  1026. unmark_zone(i);
  1027. continue;
  1028. }
  1029. printf("Zone %d: %sin use, counted=%d\n",
  1030. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1031. }
  1032. }
  1033. #if ENABLE_FEATURE_MINIX2
  1034. static void check_counts2(void)
  1035. {
  1036. int i;
  1037. for (i = 1; i <= INODES; i++) {
  1038. if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
  1039. printf("Inode %d has non-zero mode. ", i);
  1040. if (ask("Clear", 1)) {
  1041. Inode2[i].i_mode = 0;
  1042. changed = 1;
  1043. }
  1044. }
  1045. if (!inode_count[i]) {
  1046. if (!inode_in_use(i))
  1047. continue;
  1048. printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
  1049. if (ask("Clear", 1))
  1050. unmark_inode(i);
  1051. continue;
  1052. }
  1053. if (!inode_in_use(i)) {
  1054. printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
  1055. if (ask("Set", 1))
  1056. mark_inode(i);
  1057. }
  1058. if (Inode2[i].i_nlinks != inode_count[i]) {
  1059. printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
  1060. i, Inode2[i].i_mode, Inode2[i].i_nlinks,
  1061. inode_count[i]);
  1062. if (ask("Set i_nlinks to count", 1)) {
  1063. Inode2[i].i_nlinks = inode_count[i];
  1064. changed = 1;
  1065. }
  1066. }
  1067. }
  1068. for (i = FIRSTZONE; i < ZONES; i++) {
  1069. if ((zone_in_use(i) != 0) == zone_count[i])
  1070. continue;
  1071. if (!zone_count[i]) {
  1072. if (bad_zone(i))
  1073. continue;
  1074. printf("Zone %d is marked 'in use', but no file uses it. ", i);
  1075. if (ask("Unmark", 1))
  1076. unmark_zone(i);
  1077. continue;
  1078. }
  1079. printf("Zone %d: %sin use, counted=%d\n",
  1080. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1081. }
  1082. }
  1083. #endif
  1084. static void check(void)
  1085. {
  1086. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1087. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1088. check_zones(MINIX_ROOT_INO);
  1089. recursive_check(MINIX_ROOT_INO);
  1090. check_counts();
  1091. }
  1092. #if ENABLE_FEATURE_MINIX2
  1093. static void check2(void)
  1094. {
  1095. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1096. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1097. check_zones2(MINIX_ROOT_INO);
  1098. recursive_check2(MINIX_ROOT_INO);
  1099. check_counts2();
  1100. }
  1101. #else
  1102. void check2(void);
  1103. #endif
  1104. int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1105. int fsck_minix_main(int argc ATTRIBUTE_UNUSED, char **argv)
  1106. {
  1107. struct termios tmp;
  1108. int retcode = 0;
  1109. xfunc_error_retval = 8;
  1110. INIT_G();
  1111. opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
  1112. getopt32(argv, OPTION_STR);
  1113. argv += optind;
  1114. device_name = argv[0];
  1115. check_mount(); /* trying to check a mounted filesystem? */
  1116. if (OPT_manual) {
  1117. if (!isatty(0) || !isatty(1))
  1118. die("need terminal for interactive repairs");
  1119. }
  1120. xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
  1121. /*sync(); paranoia? */
  1122. read_superblock();
  1123. /*
  1124. * Determine whether or not we should continue with the checking.
  1125. * This is based on the status of the filesystem valid and error
  1126. * flags and whether or not the -f switch was specified on the
  1127. * command line.
  1128. */
  1129. printf("%s: %s\n", applet_name, bb_banner);
  1130. if (!(Super.s_state & MINIX_ERROR_FS)
  1131. && (Super.s_state & MINIX_VALID_FS) && !OPT_force
  1132. ) {
  1133. if (OPT_repair)
  1134. printf("%s is clean, check is skipped\n", device_name);
  1135. return 0;
  1136. } else if (OPT_force)
  1137. printf("Forcing filesystem check on %s\n", device_name);
  1138. else if (OPT_repair)
  1139. printf("Filesystem on %s is dirty, needs checking\n",
  1140. device_name);
  1141. read_tables();
  1142. if (OPT_manual) {
  1143. tcgetattr(0, &sv_termios);
  1144. tmp = sv_termios;
  1145. tmp.c_lflag &= ~(ICANON | ECHO);
  1146. tcsetattr(0, TCSANOW, &tmp);
  1147. termios_set = 1;
  1148. }
  1149. if (version2) {
  1150. check_root2();
  1151. check2();
  1152. } else {
  1153. check_root();
  1154. check();
  1155. }
  1156. if (OPT_verbose) {
  1157. int i, free_cnt;
  1158. for (i = 1, free_cnt = 0; i <= INODES; i++)
  1159. if (!inode_in_use(i))
  1160. free_cnt++;
  1161. printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
  1162. 100 * (INODES - free_cnt) / INODES);
  1163. for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
  1164. if (!zone_in_use(i))
  1165. free_cnt++;
  1166. printf("%6u zones used (%u%%)\n\n"
  1167. "%6u regular files\n"
  1168. "%6u directories\n"
  1169. "%6u character device files\n"
  1170. "%6u block device files\n"
  1171. "%6u links\n"
  1172. "%6u symbolic links\n"
  1173. "------\n"
  1174. "%6u files\n",
  1175. (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
  1176. regular, directory, chardev, blockdev,
  1177. links - 2 * directory + 1, symlinks,
  1178. total - 2 * directory + 1);
  1179. }
  1180. if (changed) {
  1181. write_tables();
  1182. printf("FILE SYSTEM HAS BEEN CHANGED\n");
  1183. sync();
  1184. } else if (OPT_repair)
  1185. write_super_block();
  1186. if (OPT_manual)
  1187. tcsetattr(0, TCSANOW, &sv_termios);
  1188. if (changed)
  1189. retcode += 3;
  1190. if (errors_uncorrected)
  1191. retcode += 4;
  1192. return retcode;
  1193. }