blob: 049bda4ccabb3e0bdeed309175a361b179a6eb47 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersene494fdd1999-10-19 20:23:03 +00002/*
3 * fsck.c - a file system consistency checker for Linux.
4 *
Rob Landleycc9962d2006-09-14 05:27:28 +00005 * (C) 1991, 1992 Linus Torvalds.
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Eric Andersene494fdd1999-10-19 20:23:03 +00008 */
9
10/*
Eric Andersenaff114c2004-04-14 17:51:38 +000011 * 09.11.91 - made the first rudimentary functions
Eric Andersene494fdd1999-10-19 20:23:03 +000012 *
13 * 10.11.91 - updated, does checking, no repairs yet.
14 * Sent out to the mailing-list for testing.
15 *
16 * 14.11.91 - Testing seems to have gone well. Added some
17 * correction-code, and changed some functions.
18 *
19 * 15.11.91 - More correction code. Hopefully it notices most
20 * cases now, and tries to do something about them.
21 *
22 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
23 * things seem to work now. Yeah, sure.
24 *
25 *
26 * 19.04.92 - Had to start over again from this old version, as a
27 * kernel bug ate my enhanced fsck in february.
28 *
29 * 28.02.93 - added support for different directory entry sizes..
30 *
31 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
32 * super-block information
33 *
34 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
35 * to that required by fsutil
36 *
37 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
38 * Added support for file system valid flag. Also
39 * added program_version variable and output of
40 * program name and version number when program
41 * is executed.
42 *
43 * 30.10.94 - added support for v2 filesystem
44 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
45 *
46 * 10.12.94 - added test to prevent checking of mounted fs adapted
47 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
48 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
49 *
50 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
51 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
52 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
Eric Andersene494fdd1999-10-19 20:23:03 +000054 * (Russell King). He made them for ARM. It would seem
55 * that the ARM is powerful enough to do this in C whereas
56 * i386 and m64k must use assembly to get it fast >:-)
Eric Andersenaff114c2004-04-14 17:51:38 +000057 * This should make minix fsck system-independent.
Eric Andersene494fdd1999-10-19 20:23:03 +000058 * (janl@math.uio.no, Nicolai Langfeldt)
59 *
60 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 * warnings. Added mc68k bitops from
Eric Andersene494fdd1999-10-19 20:23:03 +000062 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
63 *
64 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
65 * Andreas Schwab.
66 *
67 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
68 * - added Native Language Support
69 *
70 *
71 * I've had no time to add comments - hopefully the function names
72 * are comments enough. As with all file system checkers, this assumes
73 * the file system is quiescent - don't use it on a mounted device
74 * unless you can be sure nobody is writing to it (and remember that the
75 * kernel can write to it when it searches for files).
76 *
Eric Andersenaff114c2004-04-14 17:51:38 +000077 * Usage: fsck [-larvsm] device
Eric Andersene494fdd1999-10-19 20:23:03 +000078 * -l for a listing of all the filenames
79 * -a for automatic repairs (not implemented)
80 * -r for repairs (interactive) (not implemented)
81 * -v for verbose (tells how many files)
82 * -s for super-block info
83 * -m for minix-like "mode not cleared" warnings
84 * -f force filesystem check even if filesystem marked as valid
85 *
86 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087 * enforced (but it's not much fun on a character device :-).
Eric Andersene494fdd1999-10-19 20:23:03 +000088 */
89
Eric Andersencbe31da2001-02-20 06:14:08 +000090#include "busybox.h"
Rob Landleycc9962d2006-09-14 05:27:28 +000091#include <mntent.h>
Eric Andersen85c55202000-07-10 22:46:55 +000092
Denis Vlasenkoaa959592007-01-03 00:43:19 +000093#include "minix.h"
Eric Andersen85c55202000-07-10 22:46:55 +000094
Eric Andersen85c55202000-07-10 22:46:55 +000095#ifndef BLKGETSIZE
96#define BLKGETSIZE _IO(0x12,96) /* return device size */
97#endif
Eric Andersene494fdd1999-10-19 20:23:03 +000098
Denis Vlasenkoaa959592007-01-03 00:43:19 +000099#ifdef UNUSED
100enum {
101 MINIX1_LINK_MAX = 250,
102 MINIX2_LINK_MAX = 65530,
103 MINIX_I_MAP_SLOTS = 8,
104 MINIX_Z_MAP_SLOTS = 64,
105 MINIX_V1 = 0x0001, /* original minix fs */
106 MINIX_V2 = 0x0002, /* minix V2 fs */
107 NAME_MAX = 255, /* # chars in a file name */
108};
Eric Andersene494fdd1999-10-19 20:23:03 +0000109#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000110
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000111#if ENABLE_FEATURE_MINIX2
112static smallint version2;
113#else
114enum { version2 = 0 };
115#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000116
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000117#define PROGRAM_VERSION "1.2 - 11/11/96"
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000118static smallint repair, automatic, verbose, list, show, warn_mode, force;
119static smallint changed; /* is filesystem modified? */
120static smallint errors_uncorrected; /* flag if some error was not corrected */
121
122static smallint termios_set;
123static struct termios termios;
124
Rob Landley8a625492006-04-29 20:05:17 +0000125static char *device_name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000126static int IN;
Rob Landley8a625492006-04-29 20:05:17 +0000127static int directory, regular, blockdev, chardev, links, symlinks, total;
Eric Andersene494fdd1999-10-19 20:23:03 +0000128
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000129//also smallint?
Eric Andersene494fdd1999-10-19 20:23:03 +0000130static int dirsize = 16;
131static int namelen = 14;
Eric Andersene494fdd1999-10-19 20:23:03 +0000132
Rob Landleydb2ab892006-05-03 21:39:28 +0000133static char *inode_buffer;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000134//xzalloc?
Eric Andersene494fdd1999-10-19 20:23:03 +0000135static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000137#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
138#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
139
Eric Andersene494fdd1999-10-19 20:23:03 +0000140#define Super (*(struct minix_super_block *)super_block_buffer)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000141
142#if ENABLE_FEATURE_MINIX2
143# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000144#else
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000145# define ZONES ((unsigned)(Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000146#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000147#define INODES ((unsigned)Super.s_ninodes)
148#define IMAPS ((unsigned)Super.s_imap_blocks)
149#define ZMAPS ((unsigned)Super.s_zmap_blocks)
150#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
151#define ZONESIZE ((unsigned)Super.s_log_zone_size)
152#define MAXSIZE ((unsigned)Super.s_max_size)
153#define MAGIC (Super.s_magic)
154
155/* gcc likes this more (code is smaller) than macro variant */
156static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
157{
Denis Vlasenko150f4022007-01-13 21:06:21 +0000158 return (size + n-1) / n;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000159}
160
161#if ENABLE_FEATURE_MINIX2
162#define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
163 : MINIX1_INODES_PER_BLOCK))
164#else
165#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
166#endif
167
168#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
169#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
Eric Andersene494fdd1999-10-19 20:23:03 +0000170
171static char *inode_map;
172static char *zone_map;
173
Rob Landley8a625492006-04-29 20:05:17 +0000174static unsigned char *inode_count;
175static unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000176
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000177static int bit(char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000178{
Denis Vlasenko87033fb2007-01-03 00:47:47 +0000179 return (a[i >> 3] & (1<<(i & 7))) != 0;
Eric Andersen56f3e352000-09-19 21:13:55 +0000180}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000181
Eric Andersen56f3e352000-09-19 21:13:55 +0000182#define inode_in_use(x) (bit(inode_map,(x)))
183#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000184
185#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
186#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
187
188#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
189#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
190
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000191
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000192static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000193#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000194static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000195#endif
196
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000197static void die(const char *str) ATTRIBUTE_NORETURN;
198static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000199{
200 if (termios_set)
201 tcsetattr(0, TCSANOW, &termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000202 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000203}
204
Rob Landley3ea66a82006-04-29 20:08:40 +0000205/* File-name data */
206enum { MAX_DEPTH = 32 };
207static int name_depth;
208static char *current_name;
209static char *name_component[MAX_DEPTH+1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000210
Rob Landley3ea66a82006-04-29 20:08:40 +0000211/* Wed Feb 9 15:17:06 MST 2000 */
212/* dynamically allocate name_list (instead of making it static) */
Rob Landley88621d72006-08-29 19:41:06 +0000213static void alloc_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000214{
215 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
216 current_name[0] = '/';
217 current_name[1] = '\0';
218 name_component[0] = &current_name[0];
219}
220
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000221#if ENABLE_FEATURE_CLEAN_UP
Rob Landley3ea66a82006-04-29 20:08:40 +0000222/* execute this atexit() to deallocate name_list[] */
223/* piptigger was here */
Rob Landley88621d72006-08-29 19:41:06 +0000224static void free_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000225{
226 free(current_name);
227}
228#endif
229
230static void push_filename(const char *name)
231{
232 // /dir/dir/dir/file
233 // ^ ^ ^
234 // [0] [1] [2] <-name_component[i]
235 if (name_depth < MAX_DEPTH) {
236 int len;
237 char *p = name_component[name_depth];
238 *p++ = '/';
239 len = sprintf(p, "%.*s", namelen, name);
240 name_component[name_depth + 1] = p + len;
241 }
242 name_depth++;
243}
244
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000245static void pop_filename(void)
246{
Rob Landley3ea66a82006-04-29 20:08:40 +0000247 name_depth--;
248 if (name_depth < MAX_DEPTH) {
249 *name_component[name_depth] = '\0';
250 if (!name_depth) {
251 current_name[0] = '/';
252 current_name[1] = '\0';
253 }
254 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000255}
256
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000258{
259 int c;
260
261 if (!repair) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000262 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000263 errors_uncorrected = 1;
264 return 0;
265 }
266 if (automatic) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000267 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000268 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000269 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000270 return def;
271 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000273 for (;;) {
274 fflush(stdout);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000275 c = getchar();
276 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 if (!def)
278 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000279 return def;
280 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000281 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000282 if (c == 'Y') {
283 def = 1;
284 break;
285 } else if (c == 'N') {
286 def = 0;
287 break;
288 } else if (c == ' ' || c == '\n')
289 break;
290 }
291 if (def)
292 printf("y\n");
293 else {
294 printf("n\n");
295 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000297 return def;
298}
299
300/*
301 * Make certain that we aren't checking a filesystem that is on a
302 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
303 * 1994 Theodore Ts'o. Also licensed under GPL.
304 */
305static void check_mount(void)
306{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 FILE *f;
308 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000309 int cont;
310 int fd;
311
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000312 f = setmntent(MOUNTED, "r");
313 if (f == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 while ((mnt = getmntent(f)) != NULL)
316 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000317 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000319 if (!mnt)
320 return;
321
322 /*
323 * If the root is mounted read-only, then /etc/mtab is
324 * probably not correct; so we won't issue a warning based on
325 * it.
326 */
327 fd = open(MOUNTED, O_RDWR);
328 if (fd < 0 && errno == EROFS)
329 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000330 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331
Rob Landleyfd8409f2006-06-13 15:31:39 +0000332 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000333 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000334 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000335 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000336 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000337 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000339 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000340}
341
342/*
343 * check_zone_nr checks to see that *nr is a valid zone nr. If it
344 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
345 * if an error was corrected, and returns the zone (0 for no zone
346 * or a bad zone-number).
347 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000348static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000349{
Rob Landley3ea66a82006-04-29 20:08:40 +0000350 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000351 if (!*nr)
352 return 0;
353 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000354 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000355 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000356 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000357 else
358 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000359 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 *nr = 0;
362 *corrected = 1;
363 }
364 return 0;
365}
366
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000367static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000368{
Rob Landley8a625492006-04-29 20:05:17 +0000369 uint32_t nr32 = *nr;
370 int r = check_zone_nr2(&nr32, corrected);
371 *nr = (uint16_t)nr32;
372 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000373}
Eric Andersene494fdd1999-10-19 20:23:03 +0000374
375/*
376 * read-block reads block nr into the buffer at addr.
377 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000378static void read_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000379{
380 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000382 return;
383 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000385 printf("%s: cannot seek to block in file '%s'\n",
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000386 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000387 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000388 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000390 printf("%s: bad block in file '%s'\n",
391 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000392 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000393 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000394 }
395}
396
397/*
398 * write_block writes block nr to disk.
399 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000400static void write_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000401{
402 if (!nr)
403 return;
404 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000405 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000407 errors_uncorrected = 1;
408 return;
409 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000411 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000412 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000413 printf("%s: bad block in file '%s'\n",
414 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000415 errors_uncorrected = 1;
416 }
417}
418
419/*
Rob Landley8a625492006-04-29 20:05:17 +0000420 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000421 * It sets 'changed' if the inode has needed changing, and re-writes
422 * any indirect blocks with errors.
423 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000424static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000425{
Rob Landley8a625492006-04-29 20:05:17 +0000426 uint16_t ind[BLOCK_SIZE >> 1];
427 uint16_t dind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000428 int block, result;
429 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000430
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000432 return check_zone_nr(inode->i_zone + blknr, &changed);
433 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000435 block = check_zone_nr(inode->i_zone + 7, &changed);
436 read_block(block, (char *) ind);
437 blk_chg = 0;
438 result = check_zone_nr(blknr + ind, &blk_chg);
439 if (blk_chg)
440 write_block(block, (char *) ind);
441 return result;
442 }
443 blknr -= 512;
444 block = check_zone_nr(inode->i_zone + 8, &changed);
445 read_block(block, (char *) dind);
446 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000448 if (blk_chg)
449 write_block(block, (char *) dind);
450 block = result;
451 read_block(block, (char *) ind);
452 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000454 if (blk_chg)
455 write_block(block, (char *) ind);
456 return result;
457}
458
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000459#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000460static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000461{
Rob Landley8a625492006-04-29 20:05:17 +0000462 uint32_t ind[BLOCK_SIZE >> 2];
463 uint32_t dind[BLOCK_SIZE >> 2];
464 uint32_t tind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000465 int block, result;
466 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000467
468 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 blknr -= 7;
471 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 block = check_zone_nr2(inode->i_zone + 7, &changed);
473 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000476 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000478 return result;
479 }
480 blknr -= 256;
481 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 block = check_zone_nr2(inode->i_zone + 8, &changed);
483 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000484 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000486 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000488 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000490 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000492 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000494 return result;
495 }
496 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 block = check_zone_nr2(inode->i_zone + 9, &changed);
498 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000499 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000501 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000503 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000509 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000511 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000513 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000515 return result;
516}
517#endif
518
Eric Andersene674eb71999-10-19 20:52:57 +0000519static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000520{
521 /*
522 * Set the state of the filesystem based on whether or not there
523 * are uncorrected errors. The filesystem valid flag is
524 * unconditionally set if we get this far.
525 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000526 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
527 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000528 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529
Eric Andersene494fdd1999-10-19 20:23:03 +0000530 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000531 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000532 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000533 die("cannot write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000534}
535
Eric Andersene674eb71999-10-19 20:52:57 +0000536static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000537{
538 write_super_block();
539
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000541 die("cannot write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000543 die("cannot write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000545 die("cannot write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000546}
547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000549{
550 int block;
551 char blk[BLOCK_SIZE];
552 int size;
553
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000554#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000555 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000556 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000557 else
558#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000559 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 dirsize = size;
564 namelen = size - 2;
565 return;
566 }
567 }
568 /* use defaults */
569}
570
Eric Andersene674eb71999-10-19 20:52:57 +0000571static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000572{
573 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000574 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000576 die("cannot read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000577 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000578 namelen = 14;
579 dirsize = 16;
580 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000581 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000582 if (MAGIC == MINIX1_SUPER_MAGIC) {
583 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000584 namelen = 30;
585 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000586#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000588 version2 = 1;
589 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
590 namelen = 30;
591 dirsize = 32;
592 version2 = 1;
593#endif
594 } else
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000595 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000597 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000598 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000599 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000601 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000602}
603
Eric Andersene674eb71999-10-19 20:52:57 +0000604static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000605{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000606 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
607 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000608 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
609 inode_count = xmalloc(INODES + 1);
610 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000612 die("cannot read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000614 die("cannot read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000616 die("cannot read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000617 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000618 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000619 errors_uncorrected = 1;
620 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000621 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000622 if (show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000623 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",
Rob Landley8a625492006-04-29 20:05:17 +0000630 INODES,
631 ZONES,
632 FIRSTZONE, NORM_FIRSTZONE,
633 BLOCK_SIZE << ZONESIZE,
634 MAXSIZE,
635 Super.s_state,
636 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000637 }
638}
639
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000640static struct minix1_inode *get_inode(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000641{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000642 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000643
644 if (!nr || nr > INODES)
645 return NULL;
646 total++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000647 inode = Inode1 + nr;
Eric Andersene494fdd1999-10-19 20:23:03 +0000648 if (!inode_count[nr]) {
649 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000650 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000651 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000652 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000653 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 mark_inode(nr);
655 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000656 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000657 }
658 }
659 if (S_ISDIR(inode->i_mode))
660 directory++;
661 else if (S_ISREG(inode->i_mode))
662 regular++;
663 else if (S_ISCHR(inode->i_mode))
664 chardev++;
665 else if (S_ISBLK(inode->i_mode))
666 blockdev++;
667 else if (S_ISLNK(inode->i_mode))
668 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 else if (S_ISSOCK(inode->i_mode));
670 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000671 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000672 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000674
675 } else
676 links++;
677 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000678 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000679 inode_count[nr]--;
680 errors_uncorrected = 1;
681 }
682 return inode;
683}
684
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000685#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000686static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000687{
688 struct minix2_inode *inode;
689
690 if (!nr || nr > INODES)
691 return NULL;
692 total++;
693 inode = Inode2 + nr;
694 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000696 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000697 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000698 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000699 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 else
702 errors_uncorrected = 1;
703 }
704 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000706 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000708 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000710 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000712 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000714 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 else if (S_ISSOCK(inode->i_mode));
716 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000717 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000718 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 }
720 } else
721 links++;
722 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000723 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000724 inode_count[nr]--;
725 errors_uncorrected = 1;
726 }
727 return inode;
728}
729#endif
730
Eric Andersene674eb71999-10-19 20:52:57 +0000731static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000732{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000733 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000734
735 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000736 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000737}
738
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000739#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000741{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000742 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000743
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000745 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000746}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000747#else
748void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000749#endif
750
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000751static int add_zone(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000752{
753 int result;
754 int block;
755
756 result = 0;
757 block = check_zone_nr(znr, corrected);
758 if (!block)
759 return 0;
760 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000761 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000762 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000764 *znr = 0;
765 block = 0;
766 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000767 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000768 }
769 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000770 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000771 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000772 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000774 mark_zone(block);
775 }
776 if (!++zone_count[block])
777 zone_count[block]--;
778 return block;
779}
780
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000781#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000782static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000783{
784 int result;
785 int block;
786
787 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000789 if (!block)
790 return 0;
791 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000792 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000793 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000794 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000795 *znr = 0;
796 block = 0;
797 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000798 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000799 }
800 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000801 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000802 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000803 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (ask("Correct", 1))
805 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000806 }
807 if (!++zone_count[block])
808 zone_count[block]--;
809 return block;
810}
811#endif
812
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000813static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000814{
815 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000816 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000817 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000818 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000819
820 block = add_zone(znr, corrected);
821 if (!block)
822 return;
823 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000825 add_zone(i + (uint16_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000826 if (chg_blk)
827 write_block(block, blk);
828}
829
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000830#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000831static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000832{
833 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000834 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000835 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000836 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000837
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000839 if (!block)
840 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000842 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000843 add_zone2(i + (uint32_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000844 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000846}
847#endif
848
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000849static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000850{
851 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000852 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000853 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000854 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000855
856 block = add_zone(znr, corrected);
857 if (!block)
858 return;
859 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000861 add_zone_ind(i + (uint16_t *) blk, &chg_blk);
862 if (chg_blk)
Eric Andersene494fdd1999-10-19 20:23:03 +0000863 write_block(block, blk);
864}
865
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000866#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000867static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000868{
869 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000870 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000871 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000872 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000875 if (!block)
876 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000878 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000879 add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
880 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000881 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000882}
883
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000884static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000885{
886 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000887 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000888 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000889 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000890
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000892 if (!block)
893 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000895 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000896 add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
897 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000898 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000899}
900#endif
901
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000902static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000903{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000904 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000905
906 if (!i || i > INODES)
907 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000910 inode = Inode1 + i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000911 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 !S_ISLNK(inode->i_mode)) return;
913 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000914 add_zone(i + inode->i_zone, &changed);
915 add_zone_ind(7 + inode->i_zone, &changed);
916 add_zone_dind(8 + inode->i_zone, &changed);
917}
918
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000919#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000920static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000921{
922 struct minix2_inode *inode;
923
924 if (!i || i > INODES)
925 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000927 return;
928 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
930 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000931 return;
932 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 add_zone2(i + inode->i_zone, &changed);
934 add_zone_ind2(7 + inode->i_zone, &changed);
935 add_zone_dind2(8 + inode->i_zone, &changed);
936 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000937}
938#endif
939
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000940static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000941{
942 static char blk[BLOCK_SIZE];
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000943 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000944 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000945 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000946 int block;
947
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000949 read_block(block, blk);
950 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000951 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000952 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000953 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000954 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000955 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000956 *(uint16_t *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000957 write_block(block, blk);
958 }
959 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 }
Rob Landley3ea66a82006-04-29 20:08:40 +0000961 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000962 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000963 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000964 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000965 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000966 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000967 printf("%s: bad directory: '.' isn't first\n", current_name);
968 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000969 }
970 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000971 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000973 printf("%s: bad directory: '..' isn't second\n", current_name);
974 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000975 }
976 if (!inode)
977 return;
Rob Landley3ea66a82006-04-29 20:08:40 +0000978 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000979 if (list) {
980 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000981 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +0000982 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +0000983 }
984 check_zones(ino);
985 if (inode && S_ISDIR(inode->i_mode))
986 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000987 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000988}
989
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000990#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000991static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000992{
993 static char blk[BLOCK_SIZE];
994 struct minix2_inode *inode;
995 int ino;
996 char *name;
997 int block;
998
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 block = map_block2(dir, offset / BLOCK_SIZE);
1000 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001001 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001002 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001003 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001004 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001005 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001006 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001007 *(uint16_t *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001009 }
1010 ino = 0;
1011 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001012 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001013 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001014 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001015 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001016 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001017 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001018 printf("%s: bad directory: '.' isn't first\n", current_name);
1019 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001020 }
1021 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001022 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001024 printf("%s: bad directory: '..' isn't second\n", current_name);
1025 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001026 }
1027 if (!inode)
1028 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001029 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001030 if (list) {
1031 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001033 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001034 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 check_zones2(ino);
1036 if (inode && S_ISDIR(inode->i_mode))
1037 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001038 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001039}
1040#endif
1041
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001042static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001043{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001044 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001045 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001046
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001047 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001048 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001049 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001050 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001051 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 errors_uncorrected = 1;
1053 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 for (offset = 0; offset < dir->i_size; offset += dirsize)
1055 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001056}
1057
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001058#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001059static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001060{
1061 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001062 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001063
1064 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001066 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001067 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001068 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001069 errors_uncorrected = 1;
1070 }
1071 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001073}
1074#endif
1075
Eric Andersene674eb71999-10-19 20:52:57 +00001076static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001077{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001078 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001079
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001081 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001082 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1083}
1084
Eric Andersene674eb71999-10-19 20:52:57 +00001085static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001086{
1087 int i;
1088
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 for (i = 1; i <= INODES; i++) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001090 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001091 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001093 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001094 changed = 1;
1095 }
1096 }
1097 if (!inode_count[i]) {
1098 if (!inode_in_use(i))
1099 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001100 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001101 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001102 unmark_inode(i);
1103 continue;
1104 }
1105 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001106 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001108 mark_inode(i);
1109 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001110 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001111 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001112 i, Inode1[i].i_mode, Inode1[i].i_nlinks, inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001113 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001114 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001116 }
1117 }
1118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001120 if (zone_in_use(i) == zone_count[i])
1121 continue;
1122 if (!zone_count[i]) {
1123 if (bad_zone(i))
1124 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001125 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001126 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001127 unmark_zone(i);
1128 continue;
1129 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130 printf("Zone %d: %sin use, counted=%d\n",
1131 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001132 }
1133}
1134
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001135#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001136static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001137{
1138 int i;
1139
1140 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001141 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001142 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001143 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001144 Inode2[i].i_mode = 0;
1145 changed = 1;
1146 }
1147 }
1148 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001150 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001151 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152 if (ask("Clear", 1))
1153 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001154 continue;
1155 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001157 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 if (ask("Set", 1))
1159 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001160 }
1161 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001162 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1164 inode_count[i]);
1165 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001166 Inode2[i].i_nlinks = inode_count[i];
1167 changed = 1;
1168 }
1169 }
1170 }
1171 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001173 continue;
1174 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001176 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001177 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001178 if (ask("Unmark", 1))
1179 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001180 continue;
1181 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 printf("Zone %d: %sin use, counted=%d\n",
1183 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001184 }
1185}
1186#endif
1187
Eric Andersene674eb71999-10-19 20:52:57 +00001188static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001189{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1191 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001192 check_zones(MINIX_ROOT_INO);
1193 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001194 check_counts();
1195}
1196
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001197#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001199{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001200 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1201 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001202 check_zones2(MINIX_ROOT_INO);
1203 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001205}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001206#else
1207void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001208#endif
1209
Denis Vlasenko06af2162007-02-03 17:28:39 +00001210int fsck_minix_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +00001211int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001212{
1213 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001214 int retcode = 0;
1215
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001216 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001217
Rob Landley3ea66a82006-04-29 20:08:40 +00001218 alloc_current_name();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001219#if ENABLE_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001220 /* Don't bother to free memory. Exit does
1221 * that automagically, so we can save a few bytes */
Rob Landley3ea66a82006-04-29 20:08:40 +00001222 atexit(free_current_name);
Eric Andersenb040d4f2000-07-25 18:01:20 +00001223#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001224
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001225 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001226 die("bad inode size");
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001227#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001228 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001229 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001230#endif
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001231 while (--argc != 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001232 argv++;
1233 if (argv[0][0] != '-') {
1234 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001235 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001236 device_name = argv[0];
1237 } else {
1238 while (*++argv[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001239 switch (argv[0][0]) {
1240 case 'l':
1241 list = 1;
1242 break;
1243 case 'a':
1244 automatic = 1;
1245 repair = 1;
1246 break;
1247 case 'r':
1248 automatic = 0;
1249 repair = 1;
1250 break;
1251 case 'v':
1252 verbose = 1;
1253 break;
1254 case 's':
1255 show = 1;
1256 break;
1257 case 'm':
1258 warn_mode = 1;
1259 break;
1260 case 'f':
1261 force = 1;
1262 break;
1263 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001264 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001265 }
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001266 }
1267 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001268 }
1269 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001270 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001271
Erik Andersene49d5ec2000-02-08 19:58:47 +00001272 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001273 if (repair && !automatic) {
1274 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001275 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001276 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001277 IN = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1278
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001279 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001280 read_superblock();
1281
1282 /*
1283 * Determine whether or not we should continue with the checking.
1284 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001285 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001286 * command line.
1287 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001288 printf("%s, "PROGRAM_VERSION"\n", applet_name);
1289
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001290 if (!(Super.s_state & MINIX_ERROR_FS)
1291 && (Super.s_state & MINIX_VALID_FS) && !force
1292 ) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001293 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001294 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001295 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001296 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001297 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001298 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001299 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001300 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001301
1302 read_tables();
1303
1304 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001305 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001306 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001307 tmp.c_lflag &= ~(ICANON | ECHO);
1308 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001309 termios_set = 1;
1310 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001311
Eric Andersene494fdd1999-10-19 20:23:03 +00001312 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001313 check_root2();
1314 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001315 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001316 check_root();
1317 check();
1318 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001319
Eric Andersene494fdd1999-10-19 20:23:03 +00001320 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001321 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001322
Eric Andersen1ca20a72001-03-21 07:34:27 +00001323 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001324 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001325 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001326 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Eric Andersen1ca20a72001-03-21 07:34:27 +00001327 100 * (INODES - free_cnt) / INODES);
1328 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001329 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001330 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001331 printf("%6u zones used (%u%%)\n\n"
1332 "%6u regular files\n"
1333 "%6u directories\n"
1334 "%6u character device files\n"
1335 "%6u block device files\n"
1336 "%6u links\n"
1337 "%6u symbolic links\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001338 "------\n"
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001339 "%6u files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001340 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 regular, directory, chardev, blockdev,
1342 links - 2 * directory + 1, symlinks,
1343 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001344 }
1345 if (changed) {
1346 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001347 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001348 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001349 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001350 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001351
Eric Andersene494fdd1999-10-19 20:23:03 +00001352 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001353 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001354
1355 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001356 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001357 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001358 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001359 return retcode;
1360}