3
0

fsck_minix.c 33 KB

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