fsck_minix.c 33 KB

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