blob: 4c486d08e213deff55812c7c86b24e9f2f7c876b [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 Vlasenkodc485c92007-02-06 00:36:53 +0000177static int bit(const char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000178{
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000179 return (a[i >> 3] & (1<<(i & 7)));
Eric Andersen56f3e352000-09-19 21:13:55 +0000180}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000181
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000182/* setbit/clrbit are supplied by sys/param.h */
183
184/* Note: do not assume 0/1, it is 0/nonzero */
Eric Andersen56f3e352000-09-19 21:13:55 +0000185#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000186#define inode_in_use(x) (bit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000187
188#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
189#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
190
191#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
192#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
193
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000194
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000195static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000196#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000197static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000198#endif
199
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000200static void die(const char *str) ATTRIBUTE_NORETURN;
201static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000202{
203 if (termios_set)
204 tcsetattr(0, TCSANOW, &termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000205 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000206}
207
Rob Landley3ea66a82006-04-29 20:08:40 +0000208/* File-name data */
209enum { MAX_DEPTH = 32 };
210static int name_depth;
211static char *current_name;
212static char *name_component[MAX_DEPTH+1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000213
Rob Landley3ea66a82006-04-29 20:08:40 +0000214/* Wed Feb 9 15:17:06 MST 2000 */
215/* dynamically allocate name_list (instead of making it static) */
Rob Landley88621d72006-08-29 19:41:06 +0000216static void alloc_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000217{
218 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
219 current_name[0] = '/';
220 current_name[1] = '\0';
221 name_component[0] = &current_name[0];
222}
223
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000224#if ENABLE_FEATURE_CLEAN_UP
Rob Landley3ea66a82006-04-29 20:08:40 +0000225/* execute this atexit() to deallocate name_list[] */
226/* piptigger was here */
Rob Landley88621d72006-08-29 19:41:06 +0000227static void free_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000228{
229 free(current_name);
230}
231#endif
232
233static void push_filename(const char *name)
234{
235 // /dir/dir/dir/file
236 // ^ ^ ^
237 // [0] [1] [2] <-name_component[i]
238 if (name_depth < MAX_DEPTH) {
239 int len;
240 char *p = name_component[name_depth];
241 *p++ = '/';
242 len = sprintf(p, "%.*s", namelen, name);
243 name_component[name_depth + 1] = p + len;
244 }
245 name_depth++;
246}
247
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000248static void pop_filename(void)
249{
Rob Landley3ea66a82006-04-29 20:08:40 +0000250 name_depth--;
251 if (name_depth < MAX_DEPTH) {
252 *name_component[name_depth] = '\0';
253 if (!name_depth) {
254 current_name[0] = '/';
255 current_name[1] = '\0';
256 }
257 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000258}
259
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000261{
262 int c;
263
264 if (!repair) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000265 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000266 errors_uncorrected = 1;
267 return 0;
268 }
269 if (automatic) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000270 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000271 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000273 return def;
274 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000276 for (;;) {
277 fflush(stdout);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000278 c = getchar();
279 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 if (!def)
281 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000282 return def;
283 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000284 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000285 if (c == 'Y') {
286 def = 1;
287 break;
288 } else if (c == 'N') {
289 def = 0;
290 break;
291 } else if (c == ' ' || c == '\n')
292 break;
293 }
294 if (def)
295 printf("y\n");
296 else {
297 printf("n\n");
298 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000300 return def;
301}
302
303/*
304 * Make certain that we aren't checking a filesystem that is on a
305 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
306 * 1994 Theodore Ts'o. Also licensed under GPL.
307 */
308static void check_mount(void)
309{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 FILE *f;
311 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000312 int cont;
313 int fd;
314
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000315 f = setmntent(MOUNTED, "r");
316 if (f == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000317 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 while ((mnt = getmntent(f)) != NULL)
319 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000320 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000322 if (!mnt)
323 return;
324
325 /*
326 * If the root is mounted read-only, then /etc/mtab is
327 * probably not correct; so we won't issue a warning based on
328 * it.
329 */
330 fd = open(MOUNTED, O_RDWR);
331 if (fd < 0 && errno == EROFS)
332 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000333 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334
Rob Landleyfd8409f2006-06-13 15:31:39 +0000335 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000336 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000337 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000338 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000339 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000340 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000343}
344
345/*
346 * check_zone_nr checks to see that *nr is a valid zone nr. If it
347 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
348 * if an error was corrected, and returns the zone (0 for no zone
349 * or a bad zone-number).
350 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000351static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000352{
Rob Landley3ea66a82006-04-29 20:08:40 +0000353 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000354 if (!*nr)
355 return 0;
356 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000357 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000359 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000360 else
361 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000362 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000364 *nr = 0;
365 *corrected = 1;
366 }
367 return 0;
368}
369
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000370static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000371{
Rob Landley8a625492006-04-29 20:05:17 +0000372 uint32_t nr32 = *nr;
373 int r = check_zone_nr2(&nr32, corrected);
374 *nr = (uint16_t)nr32;
375 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000376}
Eric Andersene494fdd1999-10-19 20:23:03 +0000377
378/*
379 * read-block reads block nr into the buffer at addr.
380 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000381static void read_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000382{
383 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000385 return;
386 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000388 printf("%s: cannot seek to block in file '%s'\n",
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000389 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000390 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000391 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000392 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000393 printf("%s: bad block in file '%s'\n",
394 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000395 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000396 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000397 }
398}
399
400/*
401 * write_block writes block nr to disk.
402 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000403static void write_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000404{
405 if (!nr)
406 return;
407 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000408 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000410 errors_uncorrected = 1;
411 return;
412 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000414 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000415 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000416 printf("%s: bad block in file '%s'\n",
417 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 errors_uncorrected = 1;
419 }
420}
421
422/*
Rob Landley8a625492006-04-29 20:05:17 +0000423 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000424 * It sets 'changed' if the inode has needed changing, and re-writes
425 * any indirect blocks with errors.
426 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000427static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000428{
Rob Landley8a625492006-04-29 20:05:17 +0000429 uint16_t ind[BLOCK_SIZE >> 1];
430 uint16_t dind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000431 int block, result;
432 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000433
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000435 return check_zone_nr(inode->i_zone + blknr, &changed);
436 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000437 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000438 block = check_zone_nr(inode->i_zone + 7, &changed);
439 read_block(block, (char *) ind);
440 blk_chg = 0;
441 result = check_zone_nr(blknr + ind, &blk_chg);
442 if (blk_chg)
443 write_block(block, (char *) ind);
444 return result;
445 }
446 blknr -= 512;
447 block = check_zone_nr(inode->i_zone + 8, &changed);
448 read_block(block, (char *) dind);
449 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000451 if (blk_chg)
452 write_block(block, (char *) dind);
453 block = result;
454 read_block(block, (char *) ind);
455 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000457 if (blk_chg)
458 write_block(block, (char *) ind);
459 return result;
460}
461
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000462#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000463static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000464{
Rob Landley8a625492006-04-29 20:05:17 +0000465 uint32_t ind[BLOCK_SIZE >> 2];
466 uint32_t dind[BLOCK_SIZE >> 2];
467 uint32_t tind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000468 int block, result;
469 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000470
471 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000473 blknr -= 7;
474 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000475 block = check_zone_nr2(inode->i_zone + 7, &changed);
476 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000477 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000481 return result;
482 }
483 blknr -= 256;
484 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 block = check_zone_nr2(inode->i_zone + 8, &changed);
486 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000487 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000489 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000491 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000493 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000495 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000497 return result;
498 }
499 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 block = check_zone_nr2(inode->i_zone + 9, &changed);
501 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000504 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000506 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000508 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000510 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000512 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000514 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000516 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000518 return result;
519}
520#endif
521
Eric Andersene674eb71999-10-19 20:52:57 +0000522static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000523{
524 /*
525 * Set the state of the filesystem based on whether or not there
526 * are uncorrected errors. The filesystem valid flag is
527 * unconditionally set if we get this far.
528 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000529 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
530 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000531 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532
Eric Andersene494fdd1999-10-19 20:23:03 +0000533 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000534 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000535 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000536 die("cannot write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000537}
538
Eric Andersene674eb71999-10-19 20:52:57 +0000539static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000540{
541 write_super_block();
542
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000544 die("cannot write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000545 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000546 die("cannot write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000548 die("cannot write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000549}
550
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000552{
553 int block;
554 char blk[BLOCK_SIZE];
555 int size;
556
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000557#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000559 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 else
561#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000562 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 dirsize = size;
567 namelen = size - 2;
568 return;
569 }
570 }
571 /* use defaults */
572}
573
Eric Andersene674eb71999-10-19 20:52:57 +0000574static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000575{
576 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000577 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000578 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000579 die("cannot read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000580 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000581 namelen = 14;
582 dirsize = 16;
583 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000584 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000585 if (MAGIC == MINIX1_SUPER_MAGIC) {
586 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 namelen = 30;
588 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000589#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000590 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000591 version2 = 1;
592 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
593 namelen = 30;
594 dirsize = 32;
595 version2 = 1;
596#endif
597 } else
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000598 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000600 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000601 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000602 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000603 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000604 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000605}
606
Eric Andersene674eb71999-10-19 20:52:57 +0000607static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000608{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000609 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
610 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000611 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
612 inode_count = xmalloc(INODES + 1);
613 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000615 die("cannot read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000617 die("cannot read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000619 die("cannot read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000621 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000622 errors_uncorrected = 1;
623 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000625 if (show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000626 printf("%u inodes\n"
627 "%u blocks\n"
628 "Firstdatazone=%u (%u)\n"
629 "Zonesize=%u\n"
630 "Maxsize=%u\n"
631 "Filesystem state=%u\n"
632 "namelen=%u\n\n",
Rob Landley8a625492006-04-29 20:05:17 +0000633 INODES,
634 ZONES,
635 FIRSTZONE, NORM_FIRSTZONE,
636 BLOCK_SIZE << ZONESIZE,
637 MAXSIZE,
638 Super.s_state,
639 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000640 }
641}
642
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000643static struct minix1_inode *get_inode(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000644{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000645 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000646
647 if (!nr || nr > INODES)
648 return NULL;
649 total++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000650 inode = Inode1 + nr;
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 if (!inode_count[nr]) {
652 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000653 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000654 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000655 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000656 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000657 mark_inode(nr);
658 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000660 }
661 }
662 if (S_ISDIR(inode->i_mode))
663 directory++;
664 else if (S_ISREG(inode->i_mode))
665 regular++;
666 else if (S_ISCHR(inode->i_mode))
667 chardev++;
668 else if (S_ISBLK(inode->i_mode))
669 blockdev++;
670 else if (S_ISLNK(inode->i_mode))
671 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000672 else if (S_ISSOCK(inode->i_mode));
673 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000674 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000675 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000677
678 } else
679 links++;
680 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000681 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000682 inode_count[nr]--;
683 errors_uncorrected = 1;
684 }
685 return inode;
686}
687
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000688#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000689static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000690{
691 struct minix2_inode *inode;
692
693 if (!nr || nr > INODES)
694 return NULL;
695 total++;
696 inode = Inode2 + nr;
697 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000699 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000700 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000702 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000704 else
705 errors_uncorrected = 1;
706 }
707 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000709 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000711 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000713 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000715 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000717 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 else if (S_ISSOCK(inode->i_mode));
719 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000720 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000721 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000722 }
723 } else
724 links++;
725 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000726 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000727 inode_count[nr]--;
728 errors_uncorrected = 1;
729 }
730 return inode;
731}
732#endif
733
Eric Andersene674eb71999-10-19 20:52:57 +0000734static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000735{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000736 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000737
738 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000739 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000740}
741
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000742#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000744{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000745 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000746
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000748 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000749}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000750#else
751void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000752#endif
753
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000754static int add_zone(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000755{
756 int result;
757 int block;
758
759 result = 0;
760 block = check_zone_nr(znr, corrected);
761 if (!block)
762 return 0;
763 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000764 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000765 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000766 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000767 *znr = 0;
768 block = 0;
769 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000770 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000771 }
772 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000773 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000774 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000775 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000777 mark_zone(block);
778 }
779 if (!++zone_count[block])
780 zone_count[block]--;
781 return block;
782}
783
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000784#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000785static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000786{
787 int result;
788 int block;
789
790 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000792 if (!block)
793 return 0;
794 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000795 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000796 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000798 *znr = 0;
799 block = 0;
800 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000801 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000802 }
803 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000805 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000806 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 if (ask("Correct", 1))
808 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000809 }
810 if (!++zone_count[block])
811 zone_count[block]--;
812 return block;
813}
814#endif
815
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000816static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000817{
818 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000819 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000820 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000821 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000822
823 block = add_zone(znr, corrected);
824 if (!block)
825 return;
826 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000828 add_zone(i + (uint16_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000829 if (chg_blk)
830 write_block(block, blk);
831}
832
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000833#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000834static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000835{
836 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000837 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000838 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000839 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000840
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000842 if (!block)
843 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000844 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000845 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000846 add_zone2(i + (uint32_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000847 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000849}
850#endif
851
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000852static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000853{
854 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000855 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000856 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000857 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000858
859 block = add_zone(znr, corrected);
860 if (!block)
861 return;
862 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000864 add_zone_ind(i + (uint16_t *) blk, &chg_blk);
865 if (chg_blk)
Eric Andersene494fdd1999-10-19 20:23:03 +0000866 write_block(block, blk);
867}
868
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000869#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000870static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000871{
872 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000873 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000874 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000875 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000876
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000878 if (!block)
879 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000881 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000882 add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
883 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000884 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000885}
886
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000887static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000888{
889 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000890 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000891 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000892 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000893
Erik Andersene49d5ec2000-02-08 19:58:47 +0000894 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000895 if (!block)
896 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000898 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000899 add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
900 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000901 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000902}
903#endif
904
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000905static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000906{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000907 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000908
909 if (!i || i > INODES)
910 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000912 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000913 inode = Inode1 + i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000914 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915 !S_ISLNK(inode->i_mode)) return;
916 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000917 add_zone(i + inode->i_zone, &changed);
918 add_zone_ind(7 + inode->i_zone, &changed);
919 add_zone_dind(8 + inode->i_zone, &changed);
920}
921
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000922#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000923static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000924{
925 struct minix2_inode *inode;
926
927 if (!i || i > INODES)
928 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000930 return;
931 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000932 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
933 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000934 return;
935 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 add_zone2(i + inode->i_zone, &changed);
937 add_zone_ind2(7 + inode->i_zone, &changed);
938 add_zone_dind2(8 + inode->i_zone, &changed);
939 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000940}
941#endif
942
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000943static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000944{
945 static char blk[BLOCK_SIZE];
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000946 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000947 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000949 int block;
950
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000952 read_block(block, blk);
953 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000954 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000955 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000956 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000957 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000958 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000959 *(uint16_t *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000960 write_block(block, blk);
961 }
962 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 }
Rob Landley3ea66a82006-04-29 20:08:40 +0000964 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000965 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000966 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000967 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000968 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000970 printf("%s: bad directory: '.' isn't first\n", current_name);
971 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000972 }
973 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000974 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000976 printf("%s: bad directory: '..' isn't second\n", current_name);
977 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000978 }
979 if (!inode)
980 return;
Rob Landley3ea66a82006-04-29 20:08:40 +0000981 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000982 if (list) {
983 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +0000985 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +0000986 }
987 check_zones(ino);
988 if (inode && S_ISDIR(inode->i_mode))
989 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000990 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000991}
992
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000993#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000994static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000995{
996 static char blk[BLOCK_SIZE];
997 struct minix2_inode *inode;
998 int ino;
999 char *name;
1000 int block;
1001
Erik Andersene49d5ec2000-02-08 19:58:47 +00001002 block = map_block2(dir, offset / BLOCK_SIZE);
1003 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001004 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001005 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001006 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001007 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001008 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001009 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001010 *(uint16_t *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001011 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001012 }
1013 ino = 0;
1014 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001015 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001016 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001017 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001018 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001019 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001020 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001021 printf("%s: bad directory: '.' isn't first\n", current_name);
1022 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 }
1024 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001025 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001026 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001027 printf("%s: bad directory: '..' isn't second\n", current_name);
1028 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001029 }
1030 if (!inode)
1031 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001032 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001033 if (list) {
1034 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001036 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001037 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 check_zones2(ino);
1039 if (inode && S_ISDIR(inode->i_mode))
1040 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001041 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001042}
1043#endif
1044
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001045static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001046{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001047 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001048 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001049
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001050 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001051 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001052 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001053 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001054 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001055 errors_uncorrected = 1;
1056 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 for (offset = 0; offset < dir->i_size; offset += dirsize)
1058 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001059}
1060
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001061#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001062static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001063{
1064 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001065 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001066
1067 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001069 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001070 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001071 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001072 errors_uncorrected = 1;
1073 }
1074 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001076}
1077#endif
1078
Eric Andersene674eb71999-10-19 20:52:57 +00001079static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001080{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001081 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001082
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001084 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001085 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1086}
1087
Eric Andersene674eb71999-10-19 20:52:57 +00001088static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001089{
1090 int i;
1091
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 for (i = 1; i <= INODES; i++) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001093 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001094 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001095 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001096 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001097 changed = 1;
1098 }
1099 }
1100 if (!inode_count[i]) {
1101 if (!inode_in_use(i))
1102 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001103 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001105 unmark_inode(i);
1106 continue;
1107 }
1108 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001109 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001111 mark_inode(i);
1112 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001113 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001114 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001115 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1116 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001118 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001120 }
1121 }
1122 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001123 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001124 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001125 continue;
1126 if (!zone_count[i]) {
1127 if (bad_zone(i))
1128 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001129 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001131 unmark_zone(i);
1132 continue;
1133 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 printf("Zone %d: %sin use, counted=%d\n",
1135 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001136 }
1137}
1138
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001139#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001140static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001141{
1142 int i;
1143
1144 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001145 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001146 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001148 Inode2[i].i_mode = 0;
1149 changed = 1;
1150 }
1151 }
1152 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001154 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001155 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 if (ask("Clear", 1))
1157 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001158 continue;
1159 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001161 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162 if (ask("Set", 1))
1163 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001164 }
1165 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001166 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001167 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1168 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001170 Inode2[i].i_nlinks = inode_count[i];
1171 changed = 1;
1172 }
1173 }
1174 }
1175 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001176 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001177 continue;
1178 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001179 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001180 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001181 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 if (ask("Unmark", 1))
1183 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001184 continue;
1185 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001186 printf("Zone %d: %sin use, counted=%d\n",
1187 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001188 }
1189}
1190#endif
1191
Eric Andersene674eb71999-10-19 20:52:57 +00001192static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001193{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1195 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001196 check_zones(MINIX_ROOT_INO);
1197 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001198 check_counts();
1199}
1200
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001201#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001202static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001203{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1205 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001206 check_zones2(MINIX_ROOT_INO);
1207 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001209}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001210#else
1211void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001212#endif
1213
Denis Vlasenko06af2162007-02-03 17:28:39 +00001214int fsck_minix_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +00001215int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001216{
1217 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001218 int retcode = 0;
1219
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001220 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001221
Rob Landley3ea66a82006-04-29 20:08:40 +00001222 alloc_current_name();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001223#if ENABLE_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001224 /* Don't bother to free memory. Exit does
1225 * that automagically, so we can save a few bytes */
Rob Landley3ea66a82006-04-29 20:08:40 +00001226 atexit(free_current_name);
Eric Andersenb040d4f2000-07-25 18:01:20 +00001227#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001228
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001229 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001230 die("bad inode size");
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001231#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001232 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001233 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001234#endif
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001235 while (--argc != 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001236 argv++;
1237 if (argv[0][0] != '-') {
1238 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001239 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001240 device_name = argv[0];
1241 } else {
1242 while (*++argv[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 switch (argv[0][0]) {
1244 case 'l':
1245 list = 1;
1246 break;
1247 case 'a':
1248 automatic = 1;
1249 repair = 1;
1250 break;
1251 case 'r':
1252 automatic = 0;
1253 repair = 1;
1254 break;
1255 case 'v':
1256 verbose = 1;
1257 break;
1258 case 's':
1259 show = 1;
1260 break;
1261 case 'm':
1262 warn_mode = 1;
1263 break;
1264 case 'f':
1265 force = 1;
1266 break;
1267 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001268 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001269 }
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001270 }
1271 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001272 }
1273 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001274 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001275
Erik Andersene49d5ec2000-02-08 19:58:47 +00001276 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001277 if (repair && !automatic) {
1278 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001279 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001280 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001281 IN = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1282
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001283 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001284 read_superblock();
1285
1286 /*
1287 * Determine whether or not we should continue with the checking.
1288 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001289 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001290 * command line.
1291 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001292 printf("%s, "PROGRAM_VERSION"\n", applet_name);
1293
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001294 if (!(Super.s_state & MINIX_ERROR_FS)
1295 && (Super.s_state & MINIX_VALID_FS) && !force
1296 ) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001297 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001298 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001299 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001300 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001301 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001302 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001303 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001304 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001305
1306 read_tables();
1307
1308 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001309 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001310 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001311 tmp.c_lflag &= ~(ICANON | ECHO);
1312 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001313 termios_set = 1;
1314 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001315
Eric Andersene494fdd1999-10-19 20:23:03 +00001316 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001317 check_root2();
1318 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001319 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001320 check_root();
1321 check();
1322 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001323
Eric Andersene494fdd1999-10-19 20:23:03 +00001324 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001325 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001326
Eric Andersen1ca20a72001-03-21 07:34:27 +00001327 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001328 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001329 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001330 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Eric Andersen1ca20a72001-03-21 07:34:27 +00001331 100 * (INODES - free_cnt) / INODES);
1332 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001333 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001334 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001335 printf("%6u zones used (%u%%)\n\n"
1336 "%6u regular files\n"
1337 "%6u directories\n"
1338 "%6u character device files\n"
1339 "%6u block device files\n"
1340 "%6u links\n"
1341 "%6u symbolic links\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001342 "------\n"
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001343 "%6u files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001344 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001345 regular, directory, chardev, blockdev,
1346 links - 2 * directory + 1, symlinks,
1347 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001348 }
1349 if (changed) {
1350 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001351 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001352 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001353 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001354 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001355
Eric Andersene494fdd1999-10-19 20:23:03 +00001356 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001357 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001358
1359 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001360 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001361 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001362 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001363 return retcode;
1364}