fsck_minix.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. This file may be redistributed
  6. * as per the GNU copyleft.
  7. */
  8. /*
  9. * 09.11.91 - made the first rudimentary functions
  10. *
  11. * 10.11.91 - updated, does checking, no repairs yet.
  12. * Sent out to the mailing-list for testing.
  13. *
  14. * 14.11.91 - Testing seems to have gone well. Added some
  15. * correction-code, and changed some functions.
  16. *
  17. * 15.11.91 - More correction code. Hopefully it notices most
  18. * cases now, and tries to do something about them.
  19. *
  20. * 16.11.91 - More corrections (thanks to Mika Jalava). Most
  21. * things seem to work now. Yeah, sure.
  22. *
  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. * super-block 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 Mi¶kiewicz <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 super-block 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. #include <stdio.h>
  88. #include <errno.h>
  89. #include <unistd.h>
  90. #include <string.h>
  91. #include <fcntl.h>
  92. #include <ctype.h>
  93. #include <stdlib.h>
  94. #include <termios.h>
  95. #include <mntent.h>
  96. #include <sys/param.h>
  97. #include "busybox.h"
  98. static const int MINIX_ROOT_INO = 1;
  99. static const int MINIX_LINK_MAX = 250;
  100. static const int MINIX2_LINK_MAX = 65530;
  101. static const int MINIX_I_MAP_SLOTS = 8;
  102. static const int MINIX_Z_MAP_SLOTS = 64;
  103. static const int MINIX_SUPER_MAGIC = 0x137F; /* original minix fs */
  104. static const int MINIX_SUPER_MAGIC2 = 0x138F; /* minix fs, 30 char names */
  105. static const int MINIX2_SUPER_MAGIC = 0x2468; /* minix V2 fs */
  106. static const int MINIX2_SUPER_MAGIC2 = 0x2478; /* minix V2 fs, 30 char names */
  107. static const int MINIX_VALID_FS = 0x0001; /* Clean fs. */
  108. static const int MINIX_ERROR_FS = 0x0002; /* fs has errors. */
  109. #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
  110. #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
  111. static const int MINIX_V1 = 0x0001; /* original minix fs */
  112. static const int MINIX_V2 = 0x0002; /* minix V2 fs */
  113. #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
  114. /*
  115. * This is the original minix inode layout on disk.
  116. * Note the 8-bit gid and atime and ctime.
  117. */
  118. struct minix_inode {
  119. uint16_t i_mode;
  120. uint16_t i_uid;
  121. uint32_t i_size;
  122. uint32_t i_time;
  123. uint8_t i_gid;
  124. uint8_t i_nlinks;
  125. uint16_t i_zone[9];
  126. };
  127. /*
  128. * The new minix inode has all the time entries, as well as
  129. * long block numbers and a third indirect block (7+1+1+1
  130. * instead of 7+1+1). Also, some previously 8-bit values are
  131. * now 16-bit. The inode is now 64 bytes instead of 32.
  132. */
  133. struct minix2_inode {
  134. uint16_t i_mode;
  135. uint16_t i_nlinks;
  136. uint16_t i_uid;
  137. uint16_t i_gid;
  138. uint32_t i_size;
  139. uint32_t i_atime;
  140. uint32_t i_mtime;
  141. uint32_t i_ctime;
  142. uint32_t i_zone[10];
  143. };
  144. /*
  145. * minix super-block data on disk
  146. */
  147. struct minix_super_block {
  148. uint16_t s_ninodes;
  149. uint16_t s_nzones;
  150. uint16_t s_imap_blocks;
  151. uint16_t s_zmap_blocks;
  152. uint16_t s_firstdatazone;
  153. uint16_t s_log_zone_size;
  154. uint32_t s_max_size;
  155. uint16_t s_magic;
  156. uint16_t s_state;
  157. uint32_t s_zones;
  158. };
  159. struct minix_dir_entry {
  160. uint16_t inode;
  161. char name[0];
  162. };
  163. #define BLOCK_SIZE_BITS 10
  164. #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
  165. #define NAME_MAX 255 /* # chars in a file name */
  166. #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
  167. #ifndef BLKGETSIZE
  168. #define BLKGETSIZE _IO(0x12,96) /* return device size */
  169. #endif
  170. #ifndef __linux__
  171. #define volatile
  172. #endif
  173. static const int ROOT_INO = 1;
  174. #define UPPER(size,n) ((size+((n)-1))/(n))
  175. #define INODE_SIZE (sizeof(struct minix_inode))
  176. #ifdef CONFIG_FEATURE_MINIX2
  177. #define INODE_SIZE2 (sizeof(struct minix2_inode))
  178. #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
  179. : MINIX_INODES_PER_BLOCK))
  180. #else
  181. #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
  182. #endif
  183. #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
  184. #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
  185. static char *program_version = "1.2 - 11/11/96";
  186. static char *device_name = NULL;
  187. static int IN;
  188. static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
  189. 0, warn_mode = 0, force = 0;
  190. static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
  191. 0, symlinks = 0, total = 0;
  192. static int changed = 0; /* flags if the filesystem has been changed */
  193. static int errors_uncorrected = 0; /* flag if some error was not corrected */
  194. static int dirsize = 16;
  195. static int namelen = 14;
  196. static int version2 = 0;
  197. static struct termios termios;
  198. static int termios_set = 0;
  199. /* File-name data */
  200. static const int MAX_DEPTH = 32;
  201. static int name_depth = 0;
  202. // static char name_list[MAX_DEPTH][BUFSIZ + 1];
  203. static char **name_list = NULL;
  204. static char *inode_buffer = NULL;
  205. #define Inode (((struct minix_inode *) inode_buffer)-1)
  206. #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
  207. static char super_block_buffer[BLOCK_SIZE];
  208. #define Super (*(struct minix_super_block *)super_block_buffer)
  209. #define INODES ((unsigned long)Super.s_ninodes)
  210. #ifdef CONFIG_FEATURE_MINIX2
  211. #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
  212. #else
  213. #define ZONES ((unsigned long)(Super.s_nzones))
  214. #endif
  215. #define IMAPS ((unsigned long)Super.s_imap_blocks)
  216. #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
  217. #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
  218. #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
  219. #define MAXSIZE ((unsigned long)Super.s_max_size)
  220. #define MAGIC (Super.s_magic)
  221. #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
  222. static char *inode_map;
  223. static char *zone_map;
  224. static unsigned char *inode_count = NULL;
  225. static unsigned char *zone_count = NULL;
  226. static void recursive_check(unsigned int ino);
  227. #ifdef CONFIG_FEATURE_MINIX2
  228. static void recursive_check2(unsigned int ino);
  229. #endif
  230. static inline int bit(char * a,unsigned int i)
  231. {
  232. return (a[i >> 3] & (1<<(i & 7))) != 0;
  233. }
  234. #define inode_in_use(x) (bit(inode_map,(x)))
  235. #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
  236. #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
  237. #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
  238. #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  239. #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
  240. static void leave(int) __attribute__ ((noreturn));
  241. static void leave(int status)
  242. {
  243. if (termios_set)
  244. tcsetattr(0, TCSANOW, &termios);
  245. exit(status);
  246. }
  247. static void die(const char *str)
  248. {
  249. bb_error_msg("%s", str);
  250. leave(8);
  251. }
  252. /*
  253. * This simply goes through the file-name data and prints out the
  254. * current file.
  255. */
  256. static void print_current_name(void)
  257. {
  258. int i = 0;
  259. while (i < name_depth)
  260. printf("/%.*s", namelen, name_list[i++]);
  261. if (i == 0)
  262. printf("/");
  263. }
  264. static int ask(const char *string, int def)
  265. {
  266. int c;
  267. if (!repair) {
  268. printf("\n");
  269. errors_uncorrected = 1;
  270. return 0;
  271. }
  272. if (automatic) {
  273. printf("\n");
  274. if (!def)
  275. errors_uncorrected = 1;
  276. return def;
  277. }
  278. printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
  279. for (;;) {
  280. fflush(stdout);
  281. if ((c = getchar()) == EOF) {
  282. if (!def)
  283. errors_uncorrected = 1;
  284. return def;
  285. }
  286. c = toupper(c);
  287. if (c == 'Y') {
  288. def = 1;
  289. break;
  290. } else if (c == 'N') {
  291. def = 0;
  292. break;
  293. } else if (c == ' ' || c == '\n')
  294. break;
  295. }
  296. if (def)
  297. printf("y\n");
  298. else {
  299. printf("n\n");
  300. errors_uncorrected = 1;
  301. }
  302. return def;
  303. }
  304. /*
  305. * Make certain that we aren't checking a filesystem that is on a
  306. * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
  307. * 1994 Theodore Ts'o. Also licensed under GPL.
  308. */
  309. static void check_mount(void)
  310. {
  311. FILE *f;
  312. struct mntent *mnt;
  313. int cont;
  314. int fd;
  315. if ((f = setmntent(MOUNTED, "r")) == NULL)
  316. return;
  317. while ((mnt = getmntent(f)) != NULL)
  318. if (strcmp(device_name, mnt->mnt_fsname) == 0)
  319. break;
  320. endmntent(f);
  321. if (!mnt)
  322. return;
  323. /*
  324. * If the root is mounted read-only, then /etc/mtab is
  325. * probably not correct; so we won't issue a warning based on
  326. * it.
  327. */
  328. fd = open(MOUNTED, O_RDWR);
  329. if (fd < 0 && errno == EROFS)
  330. return;
  331. else
  332. close(fd);
  333. printf("%s is mounted. ", device_name);
  334. if (isatty(0) && isatty(1))
  335. cont = ask("Do you really want to continue", 0);
  336. else
  337. cont = 0;
  338. if (!cont) {
  339. printf("check aborted.\n");
  340. exit(0);
  341. }
  342. return;
  343. }
  344. /*
  345. * check_zone_nr checks to see that *nr is a valid zone nr. If it
  346. * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
  347. * if an error was corrected, and returns the zone (0 for no zone
  348. * or a bad zone-number).
  349. */
  350. static int check_zone_nr(unsigned short *nr, int *corrected)
  351. {
  352. if (!*nr)
  353. return 0;
  354. if (*nr < FIRSTZONE)
  355. printf("Zone nr < FIRSTZONE in file `");
  356. else if (*nr >= ZONES)
  357. printf("Zone nr >= ZONES in file `");
  358. else
  359. return *nr;
  360. print_current_name();
  361. printf("'.");
  362. if (ask("Remove block", 1)) {
  363. *nr = 0;
  364. *corrected = 1;
  365. }
  366. return 0;
  367. }
  368. #ifdef CONFIG_FEATURE_MINIX2
  369. static int check_zone_nr2(unsigned int *nr, int *corrected)
  370. {
  371. if (!*nr)
  372. return 0;
  373. if (*nr < FIRSTZONE)
  374. printf("Zone nr < FIRSTZONE in file `");
  375. else if (*nr >= ZONES)
  376. printf("Zone nr >= ZONES in file `");
  377. else
  378. return *nr;
  379. print_current_name();
  380. printf("'.");
  381. if (ask("Remove block", 1)) {
  382. *nr = 0;
  383. *corrected = 1;
  384. }
  385. return 0;
  386. }
  387. #endif
  388. /*
  389. * read-block reads block nr into the buffer at addr.
  390. */
  391. static void read_block(unsigned int nr, char *addr)
  392. {
  393. if (!nr) {
  394. memset(addr, 0, BLOCK_SIZE);
  395. return;
  396. }
  397. if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
  398. printf("Read error: unable to seek to block in file '");
  399. print_current_name();
  400. printf("'\n");
  401. memset(addr, 0, BLOCK_SIZE);
  402. errors_uncorrected = 1;
  403. } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
  404. printf("Read error: bad block in file '");
  405. print_current_name();
  406. printf("'\n");
  407. memset(addr, 0, BLOCK_SIZE);
  408. errors_uncorrected = 1;
  409. }
  410. }
  411. /*
  412. * write_block writes block nr to disk.
  413. */
  414. static void write_block(unsigned int nr, char *addr)
  415. {
  416. if (!nr)
  417. return;
  418. if (nr < FIRSTZONE || nr >= ZONES) {
  419. printf("Internal error: trying to write bad block\n"
  420. "Write request ignored\n");
  421. errors_uncorrected = 1;
  422. return;
  423. }
  424. if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
  425. die("seek failed in write_block");
  426. if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
  427. printf("Write error: bad block in file '");
  428. print_current_name();
  429. printf("'\n");
  430. errors_uncorrected = 1;
  431. }
  432. }
  433. /*
  434. * map-block calculates the absolute block nr of a block in a file.
  435. * It sets 'changed' if the inode has needed changing, and re-writes
  436. * any indirect blocks with errors.
  437. */
  438. static int map_block(struct minix_inode *inode, unsigned int blknr)
  439. {
  440. unsigned short ind[BLOCK_SIZE >> 1];
  441. unsigned short dind[BLOCK_SIZE >> 1];
  442. int blk_chg, block, result;
  443. if (blknr < 7)
  444. return check_zone_nr(inode->i_zone + blknr, &changed);
  445. blknr -= 7;
  446. if (blknr < 512) {
  447. block = check_zone_nr(inode->i_zone + 7, &changed);
  448. read_block(block, (char *) ind);
  449. blk_chg = 0;
  450. result = check_zone_nr(blknr + ind, &blk_chg);
  451. if (blk_chg)
  452. write_block(block, (char *) ind);
  453. return result;
  454. }
  455. blknr -= 512;
  456. block = check_zone_nr(inode->i_zone + 8, &changed);
  457. read_block(block, (char *) dind);
  458. blk_chg = 0;
  459. result = check_zone_nr(dind + (blknr / 512), &blk_chg);
  460. if (blk_chg)
  461. write_block(block, (char *) dind);
  462. block = result;
  463. read_block(block, (char *) ind);
  464. blk_chg = 0;
  465. result = check_zone_nr(ind + (blknr % 512), &blk_chg);
  466. if (blk_chg)
  467. write_block(block, (char *) ind);
  468. return result;
  469. }
  470. #ifdef CONFIG_FEATURE_MINIX2
  471. static int map_block2(struct minix2_inode *inode, unsigned int blknr)
  472. {
  473. unsigned int ind[BLOCK_SIZE >> 2];
  474. unsigned int dind[BLOCK_SIZE >> 2];
  475. unsigned int tind[BLOCK_SIZE >> 2];
  476. int blk_chg, block, result;
  477. if (blknr < 7)
  478. return check_zone_nr2(inode->i_zone + blknr, &changed);
  479. blknr -= 7;
  480. if (blknr < 256) {
  481. block = check_zone_nr2(inode->i_zone + 7, &changed);
  482. read_block(block, (char *) ind);
  483. blk_chg = 0;
  484. result = check_zone_nr2(blknr + ind, &blk_chg);
  485. if (blk_chg)
  486. write_block(block, (char *) ind);
  487. return result;
  488. }
  489. blknr -= 256;
  490. if (blknr >= 256 * 256) {
  491. block = check_zone_nr2(inode->i_zone + 8, &changed);
  492. read_block(block, (char *) dind);
  493. blk_chg = 0;
  494. result = check_zone_nr2(dind + blknr / 256, &blk_chg);
  495. if (blk_chg)
  496. write_block(block, (char *) dind);
  497. block = result;
  498. read_block(block, (char *) ind);
  499. blk_chg = 0;
  500. result = check_zone_nr2(ind + blknr % 256, &blk_chg);
  501. if (blk_chg)
  502. write_block(block, (char *) ind);
  503. return result;
  504. }
  505. blknr -= 256 * 256;
  506. block = check_zone_nr2(inode->i_zone + 9, &changed);
  507. read_block(block, (char *) tind);
  508. blk_chg = 0;
  509. result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
  510. if (blk_chg)
  511. write_block(block, (char *) tind);
  512. block = result;
  513. read_block(block, (char *) dind);
  514. blk_chg = 0;
  515. result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
  516. if (blk_chg)
  517. write_block(block, (char *) dind);
  518. block = result;
  519. read_block(block, (char *) ind);
  520. blk_chg = 0;
  521. result = check_zone_nr2(ind + blknr % 256, &blk_chg);
  522. if (blk_chg)
  523. write_block(block, (char *) ind);
  524. return result;
  525. }
  526. #endif
  527. static void write_super_block(void)
  528. {
  529. /*
  530. * Set the state of the filesystem based on whether or not there
  531. * are uncorrected errors. The filesystem valid flag is
  532. * unconditionally set if we get this far.
  533. */
  534. Super.s_state |= MINIX_VALID_FS;
  535. if (errors_uncorrected)
  536. Super.s_state |= MINIX_ERROR_FS;
  537. else
  538. Super.s_state &= ~MINIX_ERROR_FS;
  539. if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
  540. die("seek failed in write_super_block");
  541. if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
  542. die("unable to write super-block");
  543. return;
  544. }
  545. static void write_tables(void)
  546. {
  547. write_super_block();
  548. if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
  549. die("Unable to write inode map");
  550. if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
  551. die("Unable to write zone map");
  552. if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
  553. die("Unable to write inodes");
  554. }
  555. static void get_dirsize(void)
  556. {
  557. int block;
  558. char blk[BLOCK_SIZE];
  559. int size;
  560. #ifdef CONFIG_FEATURE_MINIX2
  561. if (version2)
  562. block = Inode2[ROOT_INO].i_zone[0];
  563. else
  564. #endif
  565. block = Inode[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. if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
  579. die("seek failed");
  580. if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
  581. die("unable to read super block");
  582. if (MAGIC == MINIX_SUPER_MAGIC) {
  583. namelen = 14;
  584. dirsize = 16;
  585. version2 = 0;
  586. } else if (MAGIC == MINIX_SUPER_MAGIC2) {
  587. namelen = 30;
  588. dirsize = 32;
  589. version2 = 0;
  590. #ifdef CONFIG_FEATURE_MINIX2
  591. } else if (MAGIC == MINIX2_SUPER_MAGIC) {
  592. namelen = 14;
  593. dirsize = 16;
  594. version2 = 1;
  595. } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
  596. namelen = 30;
  597. dirsize = 32;
  598. version2 = 1;
  599. #endif
  600. } else
  601. die("bad magic number in super-block");
  602. if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
  603. die("Only 1k blocks/zones supported");
  604. if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
  605. die("bad s_imap_blocks field in super-block");
  606. if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
  607. die("bad s_zmap_blocks field in super-block");
  608. }
  609. static void read_tables(void)
  610. {
  611. inode_map = xmalloc(IMAPS * BLOCK_SIZE);
  612. zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
  613. memset(inode_map, 0, sizeof(inode_map));
  614. memset(zone_map, 0, sizeof(zone_map));
  615. inode_buffer = xmalloc(INODE_BUFFER_SIZE);
  616. inode_count = xmalloc(INODES + 1);
  617. zone_count = xmalloc(ZONES);
  618. if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
  619. die("Unable to read inode map");
  620. if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
  621. die("Unable to read zone map");
  622. if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
  623. die("Unable to read inodes");
  624. if (NORM_FIRSTZONE != FIRSTZONE) {
  625. printf("Warning: Firstzone != Norm_firstzone\n");
  626. errors_uncorrected = 1;
  627. }
  628. get_dirsize();
  629. if (show) {
  630. printf("%ld inodes\n", INODES);
  631. printf("%ld blocks\n", ZONES);
  632. printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
  633. printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
  634. printf("Maxsize=%ld\n", MAXSIZE);
  635. printf("Filesystem state=%d\n", Super.s_state);
  636. printf("namelen=%d\n\n", namelen);
  637. }
  638. }
  639. static struct minix_inode *get_inode(unsigned int nr)
  640. {
  641. struct minix_inode *inode;
  642. if (!nr || nr > INODES)
  643. return NULL;
  644. total++;
  645. inode = Inode + nr;
  646. if (!inode_count[nr]) {
  647. if (!inode_in_use(nr)) {
  648. printf("Inode %d marked not used, but used for file '", nr);
  649. print_current_name();
  650. printf("'\n");
  651. if (repair) {
  652. if (ask("Mark in use", 1))
  653. mark_inode(nr);
  654. } else {
  655. errors_uncorrected = 1;
  656. }
  657. }
  658. if (S_ISDIR(inode->i_mode))
  659. directory++;
  660. else if (S_ISREG(inode->i_mode))
  661. regular++;
  662. else if (S_ISCHR(inode->i_mode))
  663. chardev++;
  664. else if (S_ISBLK(inode->i_mode))
  665. blockdev++;
  666. else if (S_ISLNK(inode->i_mode))
  667. symlinks++;
  668. else if (S_ISSOCK(inode->i_mode));
  669. else if (S_ISFIFO(inode->i_mode));
  670. else {
  671. print_current_name();
  672. printf(" has mode %05o\n", inode->i_mode);
  673. }
  674. } else
  675. links++;
  676. if (!++inode_count[nr]) {
  677. printf("Warning: inode count too big.\n");
  678. inode_count[nr]--;
  679. errors_uncorrected = 1;
  680. }
  681. return inode;
  682. }
  683. #ifdef CONFIG_FEATURE_MINIX2
  684. static struct minix2_inode *get_inode2(unsigned int nr)
  685. {
  686. struct minix2_inode *inode;
  687. if (!nr || nr > INODES)
  688. return NULL;
  689. total++;
  690. inode = Inode2 + nr;
  691. if (!inode_count[nr]) {
  692. if (!inode_in_use(nr)) {
  693. printf("Inode %d marked not used, but used for file '", nr);
  694. print_current_name();
  695. printf("'\n");
  696. if (repair) {
  697. if (ask("Mark in use", 1))
  698. mark_inode(nr);
  699. else
  700. errors_uncorrected = 1;
  701. }
  702. }
  703. if (S_ISDIR(inode->i_mode))
  704. directory++;
  705. else if (S_ISREG(inode->i_mode))
  706. regular++;
  707. else if (S_ISCHR(inode->i_mode))
  708. chardev++;
  709. else if (S_ISBLK(inode->i_mode))
  710. blockdev++;
  711. else if (S_ISLNK(inode->i_mode))
  712. symlinks++;
  713. else if (S_ISSOCK(inode->i_mode));
  714. else if (S_ISFIFO(inode->i_mode));
  715. else {
  716. print_current_name();
  717. printf(" has mode %05o\n", inode->i_mode);
  718. }
  719. } else
  720. links++;
  721. if (!++inode_count[nr]) {
  722. printf("Warning: inode count too big.\n");
  723. inode_count[nr]--;
  724. errors_uncorrected = 1;
  725. }
  726. return inode;
  727. }
  728. #endif
  729. static void check_root(void)
  730. {
  731. struct minix_inode *inode = Inode + ROOT_INO;
  732. if (!inode || !S_ISDIR(inode->i_mode))
  733. die("root inode isn't a directory");
  734. }
  735. #ifdef CONFIG_FEATURE_MINIX2
  736. static void check_root2(void)
  737. {
  738. struct minix2_inode *inode = Inode2 + ROOT_INO;
  739. if (!inode || !S_ISDIR(inode->i_mode))
  740. die("root inode isn't a directory");
  741. }
  742. #endif
  743. static int add_zone(unsigned short *znr, int *corrected)
  744. {
  745. int result;
  746. int block;
  747. result = 0;
  748. block = check_zone_nr(znr, corrected);
  749. if (!block)
  750. return 0;
  751. if (zone_count[block]) {
  752. printf("Block has been used before. Now in file `");
  753. print_current_name();
  754. printf("'.");
  755. if (ask("Clear", 1)) {
  756. *znr = 0;
  757. block = 0;
  758. *corrected = 1;
  759. }
  760. }
  761. if (!block)
  762. return 0;
  763. if (!zone_in_use(block)) {
  764. printf("Block %d in file `", block);
  765. print_current_name();
  766. printf("' is marked not in use.");
  767. if (ask("Correct", 1))
  768. mark_zone(block);
  769. }
  770. if (!++zone_count[block])
  771. zone_count[block]--;
  772. return block;
  773. }
  774. #ifdef CONFIG_FEATURE_MINIX2
  775. static int add_zone2(unsigned int *znr, int *corrected)
  776. {
  777. int result;
  778. int block;
  779. result = 0;
  780. block = check_zone_nr2(znr, corrected);
  781. if (!block)
  782. return 0;
  783. if (zone_count[block]) {
  784. printf("Block has been used before. Now in file `");
  785. print_current_name();
  786. printf("'.");
  787. if (ask("Clear", 1)) {
  788. *znr = 0;
  789. block = 0;
  790. *corrected = 1;
  791. }
  792. }
  793. if (!block)
  794. return 0;
  795. if (!zone_in_use(block)) {
  796. printf("Block %d in file `", block);
  797. print_current_name();
  798. printf("' is marked not in use.");
  799. if (ask("Correct", 1))
  800. mark_zone(block);
  801. }
  802. if (!++zone_count[block])
  803. zone_count[block]--;
  804. return block;
  805. }
  806. #endif
  807. static void add_zone_ind(unsigned short *znr, int *corrected)
  808. {
  809. static char blk[BLOCK_SIZE];
  810. int i, chg_blk = 0;
  811. int block;
  812. block = add_zone(znr, corrected);
  813. if (!block)
  814. return;
  815. read_block(block, blk);
  816. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  817. add_zone(i + (unsigned short *) blk, &chg_blk);
  818. if (chg_blk)
  819. write_block(block, blk);
  820. }
  821. #ifdef CONFIG_FEATURE_MINIX2
  822. static void add_zone_ind2(unsigned int *znr, int *corrected)
  823. {
  824. static char blk[BLOCK_SIZE];
  825. int i, chg_blk = 0;
  826. int block;
  827. block = add_zone2(znr, corrected);
  828. if (!block)
  829. return;
  830. read_block(block, blk);
  831. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  832. add_zone2(i + (unsigned int *) blk, &chg_blk);
  833. if (chg_blk)
  834. write_block(block, blk);
  835. }
  836. #endif
  837. static void add_zone_dind(unsigned short *znr, int *corrected)
  838. {
  839. static char blk[BLOCK_SIZE];
  840. int i, blk_chg = 0;
  841. int block;
  842. block = add_zone(znr, corrected);
  843. if (!block)
  844. return;
  845. read_block(block, blk);
  846. for (i = 0; i < (BLOCK_SIZE >> 1); i++)
  847. add_zone_ind(i + (unsigned short *) blk, &blk_chg);
  848. if (blk_chg)
  849. write_block(block, blk);
  850. }
  851. #ifdef CONFIG_FEATURE_MINIX2
  852. static void add_zone_dind2(unsigned int *znr, int *corrected)
  853. {
  854. static char blk[BLOCK_SIZE];
  855. int i, blk_chg = 0;
  856. int block;
  857. block = add_zone2(znr, corrected);
  858. if (!block)
  859. return;
  860. read_block(block, blk);
  861. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  862. add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
  863. if (blk_chg)
  864. write_block(block, blk);
  865. }
  866. static void add_zone_tind2(unsigned int *znr, int *corrected)
  867. {
  868. static char blk[BLOCK_SIZE];
  869. int i, blk_chg = 0;
  870. int block;
  871. block = add_zone2(znr, corrected);
  872. if (!block)
  873. return;
  874. read_block(block, blk);
  875. for (i = 0; i < BLOCK_SIZE >> 2; i++)
  876. add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
  877. if (blk_chg)
  878. write_block(block, blk);
  879. }
  880. #endif
  881. static void check_zones(unsigned int i)
  882. {
  883. struct minix_inode *inode;
  884. if (!i || i > INODES)
  885. return;
  886. if (inode_count[i] > 1) /* have we counted this file already? */
  887. return;
  888. inode = Inode + i;
  889. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  890. !S_ISLNK(inode->i_mode)) return;
  891. for (i = 0; i < 7; i++)
  892. add_zone(i + inode->i_zone, &changed);
  893. add_zone_ind(7 + inode->i_zone, &changed);
  894. add_zone_dind(8 + inode->i_zone, &changed);
  895. }
  896. #ifdef CONFIG_FEATURE_MINIX2
  897. static void check_zones2(unsigned int i)
  898. {
  899. struct minix2_inode *inode;
  900. if (!i || i > INODES)
  901. return;
  902. if (inode_count[i] > 1) /* have we counted this file already? */
  903. return;
  904. inode = Inode2 + i;
  905. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
  906. && !S_ISLNK(inode->i_mode))
  907. return;
  908. for (i = 0; i < 7; i++)
  909. add_zone2(i + inode->i_zone, &changed);
  910. add_zone_ind2(7 + inode->i_zone, &changed);
  911. add_zone_dind2(8 + inode->i_zone, &changed);
  912. add_zone_tind2(9 + inode->i_zone, &changed);
  913. }
  914. #endif
  915. static void check_file(struct minix_inode *dir, unsigned int offset)
  916. {
  917. static char blk[BLOCK_SIZE];
  918. struct minix_inode *inode;
  919. int ino;
  920. char *name;
  921. int block;
  922. block = map_block(dir, offset / BLOCK_SIZE);
  923. read_block(block, blk);
  924. name = blk + (offset % BLOCK_SIZE) + 2;
  925. ino = *(unsigned short *) (name - 2);
  926. if (ino > INODES) {
  927. print_current_name();
  928. printf(" contains a bad inode number for file '");
  929. printf("%.*s'.", namelen, name);
  930. if (ask(" Remove", 1)) {
  931. *(unsigned short *) (name - 2) = 0;
  932. write_block(block, blk);
  933. }
  934. ino = 0;
  935. }
  936. if (name_depth < MAX_DEPTH)
  937. strncpy(name_list[name_depth], name, namelen);
  938. name_depth++;
  939. inode = get_inode(ino);
  940. name_depth--;
  941. if (!offset) {
  942. if (!inode || strcmp(".", name)) {
  943. print_current_name();
  944. printf(": bad directory: '.' isn't first\n");
  945. errors_uncorrected = 1;
  946. } else
  947. return;
  948. }
  949. if (offset == dirsize) {
  950. if (!inode || strcmp("..", name)) {
  951. print_current_name();
  952. printf(": bad directory: '..' isn't second\n");
  953. errors_uncorrected = 1;
  954. } else
  955. return;
  956. }
  957. if (!inode)
  958. return;
  959. if (name_depth < MAX_DEPTH)
  960. strncpy(name_list[name_depth], name, namelen);
  961. name_depth++;
  962. if (list) {
  963. if (verbose)
  964. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  965. print_current_name();
  966. if (S_ISDIR(inode->i_mode))
  967. printf(":\n");
  968. else
  969. printf("\n");
  970. }
  971. check_zones(ino);
  972. if (inode && S_ISDIR(inode->i_mode))
  973. recursive_check(ino);
  974. name_depth--;
  975. return;
  976. }
  977. #ifdef CONFIG_FEATURE_MINIX2
  978. static void check_file2(struct minix2_inode *dir, unsigned int offset)
  979. {
  980. static char blk[BLOCK_SIZE];
  981. struct minix2_inode *inode;
  982. int ino;
  983. char *name;
  984. int block;
  985. block = map_block2(dir, offset / BLOCK_SIZE);
  986. read_block(block, blk);
  987. name = blk + (offset % BLOCK_SIZE) + 2;
  988. ino = *(unsigned short *) (name - 2);
  989. if (ino > INODES) {
  990. print_current_name();
  991. printf(" contains a bad inode number for file '");
  992. printf("%.*s'.", namelen, name);
  993. if (ask(" Remove", 1)) {
  994. *(unsigned short *) (name - 2) = 0;
  995. write_block(block, blk);
  996. }
  997. ino = 0;
  998. }
  999. if (name_depth < MAX_DEPTH)
  1000. strncpy(name_list[name_depth], name, namelen);
  1001. name_depth++;
  1002. inode = get_inode2(ino);
  1003. name_depth--;
  1004. if (!offset) {
  1005. if (!inode || strcmp(".", name)) {
  1006. print_current_name();
  1007. printf(": bad directory: '.' isn't first\n");
  1008. errors_uncorrected = 1;
  1009. } else
  1010. return;
  1011. }
  1012. if (offset == dirsize) {
  1013. if (!inode || strcmp("..", name)) {
  1014. print_current_name();
  1015. printf(": bad directory: '..' isn't second\n");
  1016. errors_uncorrected = 1;
  1017. } else
  1018. return;
  1019. }
  1020. if (!inode)
  1021. return;
  1022. name_depth++;
  1023. if (list) {
  1024. if (verbose)
  1025. printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
  1026. print_current_name();
  1027. if (S_ISDIR(inode->i_mode))
  1028. printf(":\n");
  1029. else
  1030. printf("\n");
  1031. }
  1032. check_zones2(ino);
  1033. if (inode && S_ISDIR(inode->i_mode))
  1034. recursive_check2(ino);
  1035. name_depth--;
  1036. return;
  1037. }
  1038. #endif
  1039. static void recursive_check(unsigned int ino)
  1040. {
  1041. struct minix_inode *dir;
  1042. unsigned int offset;
  1043. dir = Inode + ino;
  1044. if (!S_ISDIR(dir->i_mode))
  1045. die("internal error");
  1046. if (dir->i_size < 2 * dirsize) {
  1047. print_current_name();
  1048. printf(": bad directory: size<32");
  1049. errors_uncorrected = 1;
  1050. }
  1051. for (offset = 0; offset < dir->i_size; offset += dirsize)
  1052. check_file(dir, offset);
  1053. }
  1054. #ifdef CONFIG_FEATURE_MINIX2
  1055. static void recursive_check2(unsigned int ino)
  1056. {
  1057. struct minix2_inode *dir;
  1058. unsigned int offset;
  1059. dir = Inode2 + ino;
  1060. if (!S_ISDIR(dir->i_mode))
  1061. die("internal error");
  1062. if (dir->i_size < 2 * dirsize) {
  1063. print_current_name();
  1064. printf(": bad directory: size < 32");
  1065. errors_uncorrected = 1;
  1066. }
  1067. for (offset = 0; offset < dir->i_size; offset += dirsize)
  1068. check_file2(dir, offset);
  1069. }
  1070. #endif
  1071. static int bad_zone(int i)
  1072. {
  1073. char buffer[1024];
  1074. if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
  1075. die("seek failed in bad_zone");
  1076. return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
  1077. }
  1078. static void check_counts(void)
  1079. {
  1080. int i;
  1081. for (i = 1; i <= INODES; i++) {
  1082. if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
  1083. printf("Inode %d mode not cleared.", i);
  1084. if (ask("Clear", 1)) {
  1085. Inode[i].i_mode = 0;
  1086. changed = 1;
  1087. }
  1088. }
  1089. if (!inode_count[i]) {
  1090. if (!inode_in_use(i))
  1091. continue;
  1092. printf("Inode %d not used, marked used in the bitmap.", i);
  1093. if (ask("Clear", 1))
  1094. unmark_inode(i);
  1095. continue;
  1096. }
  1097. if (!inode_in_use(i)) {
  1098. printf("Inode %d used, marked unused in the bitmap.", i);
  1099. if (ask("Set", 1))
  1100. mark_inode(i);
  1101. }
  1102. if (Inode[i].i_nlinks != inode_count[i]) {
  1103. printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
  1104. i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
  1105. if (ask("Set i_nlinks to count", 1)) {
  1106. Inode[i].i_nlinks = inode_count[i];
  1107. changed = 1;
  1108. }
  1109. }
  1110. }
  1111. for (i = FIRSTZONE; i < ZONES; i++) {
  1112. if (zone_in_use(i) == zone_count[i])
  1113. continue;
  1114. if (!zone_count[i]) {
  1115. if (bad_zone(i))
  1116. continue;
  1117. printf("Zone %d: marked in use, no file uses it.", i);
  1118. if (ask("Unmark", 1))
  1119. unmark_zone(i);
  1120. continue;
  1121. }
  1122. printf("Zone %d: %sin use, counted=%d\n",
  1123. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1124. }
  1125. }
  1126. #ifdef CONFIG_FEATURE_MINIX2
  1127. static void check_counts2(void)
  1128. {
  1129. int i;
  1130. for (i = 1; i <= INODES; i++) {
  1131. if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
  1132. printf("Inode %d mode not cleared.", i);
  1133. if (ask("Clear", 1)) {
  1134. Inode2[i].i_mode = 0;
  1135. changed = 1;
  1136. }
  1137. }
  1138. if (!inode_count[i]) {
  1139. if (!inode_in_use(i))
  1140. continue;
  1141. printf("Inode %d not used, marked used in the bitmap.", i);
  1142. if (ask("Clear", 1))
  1143. unmark_inode(i);
  1144. continue;
  1145. }
  1146. if (!inode_in_use(i)) {
  1147. printf("Inode %d used, marked unused in the bitmap.", i);
  1148. if (ask("Set", 1))
  1149. mark_inode(i);
  1150. }
  1151. if (Inode2[i].i_nlinks != inode_count[i]) {
  1152. printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
  1153. i, Inode2[i].i_mode, Inode2[i].i_nlinks,
  1154. inode_count[i]);
  1155. if (ask("Set i_nlinks to count", 1)) {
  1156. Inode2[i].i_nlinks = inode_count[i];
  1157. changed = 1;
  1158. }
  1159. }
  1160. }
  1161. for (i = FIRSTZONE; i < ZONES; i++) {
  1162. if (zone_in_use(i) == zone_count[i])
  1163. continue;
  1164. if (!zone_count[i]) {
  1165. if (bad_zone(i))
  1166. continue;
  1167. printf("Zone %d: marked in use, no file uses it.", i);
  1168. if (ask("Unmark", 1))
  1169. unmark_zone(i);
  1170. continue;
  1171. }
  1172. printf("Zone %d: %sin use, counted=%d\n",
  1173. i, zone_in_use(i) ? "" : "not ", zone_count[i]);
  1174. }
  1175. }
  1176. #endif
  1177. static void check(void)
  1178. {
  1179. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1180. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1181. check_zones(ROOT_INO);
  1182. recursive_check(ROOT_INO);
  1183. check_counts();
  1184. }
  1185. #ifdef CONFIG_FEATURE_MINIX2
  1186. static void check2(void)
  1187. {
  1188. memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
  1189. memset(zone_count, 0, ZONES * sizeof(*zone_count));
  1190. check_zones2(ROOT_INO);
  1191. recursive_check2(ROOT_INO);
  1192. check_counts2();
  1193. }
  1194. #endif
  1195. /* Wed Feb 9 15:17:06 MST 2000 */
  1196. /* dynamically allocate name_list (instead of making it static) */
  1197. static void alloc_name_list(void)
  1198. {
  1199. int i;
  1200. name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
  1201. for (i = 0; i < MAX_DEPTH; i++)
  1202. name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
  1203. }
  1204. #ifdef CONFIG_FEATURE_CLEAN_UP
  1205. /* execute this atexit() to deallocate name_list[] */
  1206. /* piptigger was here */
  1207. static void free_name_list(void)
  1208. {
  1209. int i;
  1210. if (name_list) {
  1211. for (i = 0; i < MAX_DEPTH; i++) {
  1212. free(name_list[i]);
  1213. }
  1214. free(name_list);
  1215. }
  1216. }
  1217. #endif
  1218. extern int fsck_minix_main(int argc, char **argv)
  1219. {
  1220. struct termios tmp;
  1221. int count;
  1222. int retcode = 0;
  1223. alloc_name_list();
  1224. #ifdef CONFIG_FEATURE_CLEAN_UP
  1225. /* Don't bother to free memory. Exit does
  1226. * that automagically, so we can save a few bytes */
  1227. atexit(free_name_list);
  1228. #endif
  1229. if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
  1230. die("bad inode size");
  1231. #ifdef CONFIG_FEATURE_MINIX2
  1232. if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
  1233. die("bad v2 inode size");
  1234. #endif
  1235. while (argc-- > 1) {
  1236. argv++;
  1237. if (argv[0][0] != '-') {
  1238. if (device_name)
  1239. bb_show_usage();
  1240. else
  1241. device_name = argv[0];
  1242. } else
  1243. while (*++argv[0])
  1244. switch (argv[0][0]) {
  1245. case 'l':
  1246. list = 1;
  1247. break;
  1248. case 'a':
  1249. automatic = 1;
  1250. repair = 1;
  1251. break;
  1252. case 'r':
  1253. automatic = 0;
  1254. repair = 1;
  1255. break;
  1256. case 'v':
  1257. verbose = 1;
  1258. break;
  1259. case 's':
  1260. show = 1;
  1261. break;
  1262. case 'm':
  1263. warn_mode = 1;
  1264. break;
  1265. case 'f':
  1266. force = 1;
  1267. break;
  1268. default:
  1269. bb_show_usage();
  1270. }
  1271. }
  1272. if (!device_name)
  1273. bb_show_usage();
  1274. check_mount(); /* trying to check a mounted filesystem? */
  1275. if (repair && !automatic) {
  1276. if (!isatty(0) || !isatty(1))
  1277. die("need terminal for interactive repairs");
  1278. }
  1279. IN = open(device_name, repair ? O_RDWR : O_RDONLY);
  1280. if (IN < 0){
  1281. fprintf(stderr,"unable to open device '%s'.\n",device_name);
  1282. leave(8);
  1283. }
  1284. for (count = 0; count < 3; count++)
  1285. sync();
  1286. read_superblock();
  1287. /*
  1288. * Determine whether or not we should continue with the checking.
  1289. * This is based on the status of the filesystem valid and error
  1290. * flags and whether or not the -f switch was specified on the
  1291. * command line.
  1292. */
  1293. printf("%s, %s\n", bb_applet_name, program_version);
  1294. if (!(Super.s_state & MINIX_ERROR_FS) &&
  1295. (Super.s_state & MINIX_VALID_FS) && !force) {
  1296. if (repair)
  1297. printf("%s is clean, no check.\n", device_name);
  1298. return retcode;
  1299. } else if (force)
  1300. printf("Forcing filesystem check on %s.\n", device_name);
  1301. else if (repair)
  1302. printf("Filesystem on %s is dirty, needs checking.\n",
  1303. device_name);
  1304. read_tables();
  1305. if (repair && !automatic) {
  1306. tcgetattr(0, &termios);
  1307. tmp = termios;
  1308. tmp.c_lflag &= ~(ICANON | ECHO);
  1309. tcsetattr(0, TCSANOW, &tmp);
  1310. termios_set = 1;
  1311. }
  1312. #ifdef CONFIG_FEATURE_MINIX2
  1313. if (version2) {
  1314. check_root2();
  1315. check2();
  1316. } else
  1317. #endif
  1318. {
  1319. check_root();
  1320. check();
  1321. }
  1322. if (verbose) {
  1323. int i, free_cnt;
  1324. for (i = 1, free_cnt = 0; i <= INODES; i++)
  1325. if (!inode_in_use(i))
  1326. free_cnt++;
  1327. printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
  1328. 100 * (INODES - free_cnt) / INODES);
  1329. for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
  1330. if (!zone_in_use(i))
  1331. free_cnt++;
  1332. printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
  1333. 100 * (ZONES - free_cnt) / ZONES);
  1334. printf("\n%6d regular files\n"
  1335. "%6d directories\n"
  1336. "%6d character device files\n"
  1337. "%6d block device files\n"
  1338. "%6d links\n"
  1339. "%6d symbolic links\n"
  1340. "------\n"
  1341. "%6d files\n",
  1342. regular, directory, chardev, blockdev,
  1343. links - 2 * directory + 1, symlinks,
  1344. total - 2 * directory + 1);
  1345. }
  1346. if (changed) {
  1347. write_tables();
  1348. printf("----------------------------\n"
  1349. "FILE SYSTEM HAS BEEN CHANGED\n"
  1350. "----------------------------\n");
  1351. for (count = 0; count < 3; count++)
  1352. sync();
  1353. } else if (repair)
  1354. write_super_block();
  1355. if (repair && !automatic)
  1356. tcsetattr(0, TCSANOW, &termios);
  1357. if (changed)
  1358. retcode += 3;
  1359. if (errors_uncorrected)
  1360. retcode += 4;
  1361. return retcode;
  1362. }