fsck_minix.c 33 KB

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