blob: 795a5493c395c6ffaeb5d959f1ed8f048f486f02 [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
Rob Landleycc9962d2006-09-14 05:27:28 +000090#include <mntent.h>
Denis Vlasenkoa6691312007-06-13 12:28:21 +000091#include "libbb.h"
Denis Vlasenkoaa959592007-01-03 00:43:19 +000092#include "minix.h"
Eric Andersen85c55202000-07-10 22:46:55 +000093
Eric Andersen85c55202000-07-10 22:46:55 +000094#ifndef BLKGETSIZE
95#define BLKGETSIZE _IO(0x12,96) /* return device size */
96#endif
Eric Andersene494fdd1999-10-19 20:23:03 +000097
Denis Vlasenkoaa959592007-01-03 00:43:19 +000098enum {
Denis Vlasenkoa6691312007-06-13 12:28:21 +000099#ifdef UNUSED
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000100 MINIX1_LINK_MAX = 250,
101 MINIX2_LINK_MAX = 65530,
102 MINIX_I_MAP_SLOTS = 8,
103 MINIX_Z_MAP_SLOTS = 64,
104 MINIX_V1 = 0x0001, /* original minix fs */
105 MINIX_V2 = 0x0002, /* minix V2 fs */
Eric Andersene494fdd1999-10-19 20:23:03 +0000106#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000107 MINIX_NAME_MAX = 255, /* # chars in a file name */
108};
Eric Andersene494fdd1999-10-19 20:23:03 +0000109
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000110#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000111enum { version2 = 0 };
112#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000113
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000114enum { MAX_DEPTH = 32 };
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000115
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000116struct globals {
117 int dev_fd;
118#if ENABLE_FEATURE_MINIX2
119 smallint version2;
120#endif
121 smallint repair, automatic, verbose, list, show, warn_mode, force;
122 smallint changed; /* is filesystem modified? */
123 smallint errors_uncorrected; /* flag if some error was not corrected */
124 smallint termios_set;
125 smallint dirsize;
126 smallint namelen;
127 char *device_name;
128 int directory, regular, blockdev, chardev, links, symlinks, total;
129 char *inode_buffer;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000130
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000131 char *inode_map;
132 char *zone_map;
Eric Andersene494fdd1999-10-19 20:23:03 +0000133
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000134 unsigned char *inode_count;
135 unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000136
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000137 /* File-name data */
138 int name_depth;
139 char *name_component[MAX_DEPTH+1];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000140
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000141 /* Bigger stuff */
142 struct termios sv_termios;
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000143 char super_block_buffer[BLOCK_SIZE];
144 char add_zone_ind_blk[BLOCK_SIZE];
145 char add_zone_dind_blk[BLOCK_SIZE];
146 USE_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
147 char check_file_blk[BLOCK_SIZE];
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000148
149 /* File-name data */
150 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
151};
152
153#define G (*ptr_to_globals)
154#define dev_fd (G.dev_fd )
155#if ENABLE_FEATURE_MINIX2
156#define version2 (G.version2 )
157#endif
158#define repair (G.repair )
159#define automatic (G.automatic )
160#define verbose (G.verbose )
161#define list (G.list )
162#define show (G.show )
163#define warn_mode (G.warn_mode )
164#define force (G.force )
165#define changed (G.changed )
166#define errors_uncorrected (G.errors_uncorrected )
167#define termios_set (G.termios_set )
168#define dirsize (G.dirsize )
169#define namelen (G.namelen )
170#define device_name (G.device_name )
171#define directory (G.directory )
172#define regular (G.regular )
173#define blockdev (G.blockdev )
174#define chardev (G.chardev )
175#define links (G.links )
176#define symlinks (G.symlinks )
177#define total (G.total )
178#define inode_buffer (G.inode_buffer )
179#define inode_map (G.inode_map )
180#define zone_map (G.zone_map )
181#define inode_count (G.inode_count )
182#define zone_count (G.zone_count )
183#define name_depth (G.name_depth )
184#define name_component (G.name_component )
185#define sv_termios (G.sv_termios )
186#define super_block_buffer (G.super_block_buffer )
187#define add_zone_ind_blk (G.add_zone_ind_blk )
188#define add_zone_dind_blk (G.add_zone_dind_blk )
189#define add_zone_tind_blk (G.add_zone_tind_blk )
190#define check_file_blk (G.check_file_blk )
191#define current_name (G.current_name )
192#define INIT_G() do { \
193 dirsize = 16; \
194 namelen = 14; \
195 current_name[0] = '/'; \
196 /*current_name[1] = '\0';*/ \
197 name_component[0] = &current_name[0]; \
198} while (0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000200#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
201#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
202
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000203#define Super (*(struct minix_super_block *)(super_block_buffer))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000204
205#if ENABLE_FEATURE_MINIX2
206# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000207#else
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000208# define ZONES ((unsigned)(Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000209#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000210#define INODES ((unsigned)Super.s_ninodes)
211#define IMAPS ((unsigned)Super.s_imap_blocks)
212#define ZMAPS ((unsigned)Super.s_zmap_blocks)
213#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
214#define ZONESIZE ((unsigned)Super.s_log_zone_size)
215#define MAXSIZE ((unsigned)Super.s_max_size)
216#define MAGIC (Super.s_magic)
217
218/* gcc likes this more (code is smaller) than macro variant */
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000219static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000220{
Denis Vlasenko150f4022007-01-13 21:06:21 +0000221 return (size + n-1) / n;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000222}
223
224#if ENABLE_FEATURE_MINIX2
225#define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
226 : MINIX1_INODES_PER_BLOCK))
227#else
228#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
229#endif
230
231#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
232#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
Eric Andersene494fdd1999-10-19 20:23:03 +0000233
Denis Vlasenkocc244192007-02-06 00:37:40 +0000234/* Before you ask "where they come from?": */
235/* setbit/clrbit are supplied by sys/param.h */
236
237static int minix_bit(const char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000238{
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000239 return (a[i >> 3] & (1<<(i & 7)));
Eric Andersen56f3e352000-09-19 21:13:55 +0000240}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000241
Denis Vlasenkocc244192007-02-06 00:37:40 +0000242static void minix_setbit(char *a, unsigned i)
243{
244 setbit(a, i);
245 changed = 1;
246}
247static void minix_clrbit(char *a, unsigned i)
248{
249 clrbit(a, i);
250 changed = 1;
251}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000252
253/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenkocc244192007-02-06 00:37:40 +0000254#define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
255#define inode_in_use(x) (minix_bit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000256
Denis Vlasenkocc244192007-02-06 00:37:40 +0000257#define mark_inode(x) (minix_setbit(inode_map,(x)))
258#define unmark_inode(x) (minix_clrbit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000259
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000260#define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
261#define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000262
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000263
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000264static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000265#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000266static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000267#endif
268
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000269static void die(const char *str) ATTRIBUTE_NORETURN;
270static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000271{
272 if (termios_set)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000273 tcsetattr(0, TCSANOW, &sv_termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000274 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000275}
276
Rob Landley3ea66a82006-04-29 20:08:40 +0000277static void push_filename(const char *name)
278{
279 // /dir/dir/dir/file
280 // ^ ^ ^
281 // [0] [1] [2] <-name_component[i]
282 if (name_depth < MAX_DEPTH) {
283 int len;
284 char *p = name_component[name_depth];
285 *p++ = '/';
286 len = sprintf(p, "%.*s", namelen, name);
287 name_component[name_depth + 1] = p + len;
288 }
289 name_depth++;
290}
291
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000292static void pop_filename(void)
293{
Rob Landley3ea66a82006-04-29 20:08:40 +0000294 name_depth--;
295 if (name_depth < MAX_DEPTH) {
296 *name_component[name_depth] = '\0';
297 if (!name_depth) {
298 current_name[0] = '/';
299 current_name[1] = '\0';
300 }
301 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000302}
303
Erik Andersene49d5ec2000-02-08 19:58:47 +0000304static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000305{
306 int c;
307
308 if (!repair) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000309 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000310 errors_uncorrected = 1;
311 return 0;
312 }
313 if (automatic) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000314 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000315 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000317 return def;
318 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000320 for (;;) {
321 fflush(stdout);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000322 c = getchar();
323 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 if (!def)
325 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000326 return def;
327 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000329 if (c == 'Y') {
330 def = 1;
331 break;
332 } else if (c == 'N') {
333 def = 0;
334 break;
335 } else if (c == ' ' || c == '\n')
336 break;
337 }
338 if (def)
339 printf("y\n");
340 else {
341 printf("n\n");
342 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000344 return def;
345}
346
347/*
348 * Make certain that we aren't checking a filesystem that is on a
349 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
350 * 1994 Theodore Ts'o. Also licensed under GPL.
351 */
352static void check_mount(void)
353{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 FILE *f;
355 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000356 int cont;
357 int fd;
358
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000359 f = setmntent(MOUNTED, "r");
360 if (f == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 while ((mnt = getmntent(f)) != NULL)
363 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000364 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000366 if (!mnt)
367 return;
368
369 /*
370 * If the root is mounted read-only, then /etc/mtab is
371 * probably not correct; so we won't issue a warning based on
372 * it.
373 */
374 fd = open(MOUNTED, O_RDWR);
375 if (fd < 0 && errno == EROFS)
376 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000377 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378
Rob Landleyfd8409f2006-06-13 15:31:39 +0000379 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000380 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000382 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000384 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000387}
388
389/*
390 * check_zone_nr checks to see that *nr is a valid zone nr. If it
391 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
392 * if an error was corrected, and returns the zone (0 for no zone
393 * or a bad zone-number).
394 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000395static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000396{
Rob Landley3ea66a82006-04-29 20:08:40 +0000397 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000398 if (!*nr)
399 return 0;
400 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000401 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000402 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000403 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000404 else
405 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000406 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 *nr = 0;
409 *corrected = 1;
410 }
411 return 0;
412}
413
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000414static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000415{
Rob Landley8a625492006-04-29 20:05:17 +0000416 uint32_t nr32 = *nr;
417 int r = check_zone_nr2(&nr32, corrected);
418 *nr = (uint16_t)nr32;
419 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000420}
Eric Andersene494fdd1999-10-19 20:23:03 +0000421
422/*
423 * read-block reads block nr into the buffer at addr.
424 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000425static void read_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000426{
427 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000429 return;
430 }
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000431 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000432 printf("%s: cannot seek to block in file '%s'\n",
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000433 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000434 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000435 memset(addr, 0, BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000436 } else if (BLOCK_SIZE != read(dev_fd, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000437 printf("%s: bad block in file '%s'\n",
438 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000439 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000440 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000441 }
442}
443
444/*
445 * write_block writes block nr to disk.
446 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000447static void write_block(unsigned nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000448{
449 if (!nr)
450 return;
451 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000452 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000454 errors_uncorrected = 1;
455 return;
456 }
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000457 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000458 die("seek failed in write_block");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000459 if (BLOCK_SIZE != write(dev_fd, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000460 printf("%s: bad block in file '%s'\n",
461 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000462 errors_uncorrected = 1;
463 }
464}
465
466/*
Rob Landley8a625492006-04-29 20:05:17 +0000467 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000468 * It sets 'changed' if the inode has needed changing, and re-writes
469 * any indirect blocks with errors.
470 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000471static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000472{
Rob Landley8a625492006-04-29 20:05:17 +0000473 uint16_t ind[BLOCK_SIZE >> 1];
474 uint16_t dind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000475 int block, result;
476 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000477
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 return check_zone_nr(inode->i_zone + blknr, &changed);
480 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000482 block = check_zone_nr(inode->i_zone + 7, &changed);
483 read_block(block, (char *) ind);
484 blk_chg = 0;
485 result = check_zone_nr(blknr + ind, &blk_chg);
486 if (blk_chg)
487 write_block(block, (char *) ind);
488 return result;
489 }
490 blknr -= 512;
491 block = check_zone_nr(inode->i_zone + 8, &changed);
492 read_block(block, (char *) dind);
493 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000495 if (blk_chg)
496 write_block(block, (char *) dind);
497 block = result;
498 read_block(block, (char *) ind);
499 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000501 if (blk_chg)
502 write_block(block, (char *) ind);
503 return result;
504}
505
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000506#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000507static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000508{
Rob Landley8a625492006-04-29 20:05:17 +0000509 uint32_t ind[BLOCK_SIZE >> 2];
510 uint32_t dind[BLOCK_SIZE >> 2];
511 uint32_t tind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000512 int block, result;
513 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000514
515 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000516 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000517 blknr -= 7;
518 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 block = check_zone_nr2(inode->i_zone + 7, &changed);
520 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000523 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000525 return result;
526 }
527 blknr -= 256;
528 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 block = check_zone_nr2(inode->i_zone + 8, &changed);
530 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000531 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000533 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000535 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000537 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000539 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 return result;
542 }
543 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 block = check_zone_nr2(inode->i_zone + 9, &changed);
545 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000546 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000550 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000552 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000554 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 return result;
563}
564#endif
565
Eric Andersene674eb71999-10-19 20:52:57 +0000566static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000567{
568 /*
569 * Set the state of the filesystem based on whether or not there
570 * are uncorrected errors. The filesystem valid flag is
571 * unconditionally set if we get this far.
572 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000573 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
574 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000577 if (BLOCK_SIZE != lseek(dev_fd, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000578 die("seek failed in write_super_block");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000579 if (BLOCK_SIZE != write(dev_fd, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000580 die("cannot write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000581}
582
Eric Andersene674eb71999-10-19 20:52:57 +0000583static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000584{
585 write_super_block();
586
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000587 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000588 die("cannot write inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000589 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000590 die("cannot write zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000591 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000592 die("cannot write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000593}
594
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000596{
597 int block;
598 char blk[BLOCK_SIZE];
599 int size;
600
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000601#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000602 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000603 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000604 else
605#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000606 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000608 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000610 dirsize = size;
611 namelen = size - 2;
612 return;
613 }
614 }
615 /* use defaults */
616}
617
Eric Andersene674eb71999-10-19 20:52:57 +0000618static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000619{
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000620 if (BLOCK_SIZE != lseek(dev_fd, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000621 die("seek failed");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000622 if (BLOCK_SIZE != read(dev_fd, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000623 die("cannot read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000624 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000625 namelen = 14;
626 dirsize = 16;
627 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000628 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000629 if (MAGIC == MINIX1_SUPER_MAGIC) {
630 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000631 namelen = 30;
632 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000633#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000634 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000635 version2 = 1;
636 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
637 namelen = 30;
638 dirsize = 32;
639 version2 = 1;
640#endif
641 } else
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000642 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000643 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000644 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000645 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000646 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000647 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000648 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000649}
650
Eric Andersene674eb71999-10-19 20:52:57 +0000651static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000652{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000653 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
654 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000655 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
656 inode_count = xmalloc(INODES + 1);
657 zone_count = xmalloc(ZONES);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000658 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000659 die("cannot read inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000660 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000661 die("cannot read zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000662 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000663 die("cannot read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000664 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000665 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000666 errors_uncorrected = 1;
667 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000668 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000669 if (show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000670 printf("%u inodes\n"
671 "%u blocks\n"
672 "Firstdatazone=%u (%u)\n"
673 "Zonesize=%u\n"
674 "Maxsize=%u\n"
675 "Filesystem state=%u\n"
676 "namelen=%u\n\n",
Rob Landley8a625492006-04-29 20:05:17 +0000677 INODES,
678 ZONES,
679 FIRSTZONE, NORM_FIRSTZONE,
680 BLOCK_SIZE << ZONESIZE,
681 MAXSIZE,
682 Super.s_state,
683 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000684 }
685}
686
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000687static struct minix1_inode *get_inode(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000688{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000689 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000690
691 if (!nr || nr > INODES)
692 return NULL;
693 total++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000694 inode = Inode1 + nr;
Eric Andersene494fdd1999-10-19 20:23:03 +0000695 if (!inode_count[nr]) {
696 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000697 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000698 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000699 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000700 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 mark_inode(nr);
Denis Vlasenkocc244192007-02-06 00:37:40 +0000702 else
703 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000704 }
705 }
706 if (S_ISDIR(inode->i_mode))
707 directory++;
708 else if (S_ISREG(inode->i_mode))
709 regular++;
710 else if (S_ISCHR(inode->i_mode))
711 chardev++;
712 else if (S_ISBLK(inode->i_mode))
713 blockdev++;
714 else if (S_ISLNK(inode->i_mode))
715 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 else if (S_ISSOCK(inode->i_mode));
717 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000718 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000719 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000720 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000721
722 } else
723 links++;
724 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000725 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000726 inode_count[nr]--;
727 errors_uncorrected = 1;
728 }
729 return inode;
730}
731
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000732#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000733static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000734{
735 struct minix2_inode *inode;
736
737 if (!nr || nr > INODES)
738 return NULL;
739 total++;
740 inode = Inode2 + nr;
741 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000743 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000744 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000745 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000746 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000748 else
749 errors_uncorrected = 1;
750 }
751 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000752 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000753 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000754 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000755 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000757 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000759 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000761 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 else if (S_ISSOCK(inode->i_mode));
763 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000764 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000765 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000766 }
767 } else
768 links++;
769 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000770 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000771 inode_count[nr]--;
772 errors_uncorrected = 1;
773 }
774 return inode;
775}
776#endif
777
Eric Andersene674eb71999-10-19 20:52:57 +0000778static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000779{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000780 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000781
782 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000783 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000784}
785
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000786#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000788{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000789 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000790
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000792 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000793}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000794#else
795void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000796#endif
797
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000798static int add_zone(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000799{
800 int result;
801 int block;
802
803 result = 0;
804 block = check_zone_nr(znr, corrected);
805 if (!block)
806 return 0;
807 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000808 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000809 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000810 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000811 *znr = 0;
812 block = 0;
813 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000814 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000815 }
816 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000817 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000818 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000819 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000821 mark_zone(block);
822 }
823 if (!++zone_count[block])
824 zone_count[block]--;
825 return block;
826}
827
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000828#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000829static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000830{
831 int result;
832 int block;
833
834 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000836 if (!block)
837 return 0;
838 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000839 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000840 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000842 *znr = 0;
843 block = 0;
844 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000845 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000846 }
847 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000849 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000850 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 if (ask("Correct", 1))
852 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000853 }
854 if (!++zone_count[block])
855 zone_count[block]--;
856 return block;
857}
858#endif
859
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000860static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000861{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000862 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000863 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000864 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000865
866 block = add_zone(znr, corrected);
867 if (!block)
868 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000869 read_block(block, add_zone_ind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000870 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000871 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000872 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000873 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000874}
875
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000876#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000877static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000878{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000879 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000880 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000881 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000882
Erik Andersene49d5ec2000-02-08 19:58:47 +0000883 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000884 if (!block)
885 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000886 read_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000887 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000888 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000889 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000890 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000891}
892#endif
893
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000894static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000895{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000896 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000897 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000898 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000899
900 block = add_zone(znr, corrected);
901 if (!block)
902 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000903 read_block(block, add_zone_dind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000905 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000906 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000907 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000908}
909
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000910#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000911static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000912{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000913 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000914 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000915 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000916
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000918 if (!block)
919 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000920 read_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000921 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000922 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000923 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000924 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000925}
926
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000927static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000928{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000929 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000930 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000931 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000932
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000934 if (!block)
935 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000936 read_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000937 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000938 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000939 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000940 write_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000941}
942#endif
943
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000944static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000945{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000946 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000947
948 if (!i || i > INODES)
949 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000951 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000952 inode = Inode1 + i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000953 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 !S_ISLNK(inode->i_mode)) return;
955 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000956 add_zone(i + inode->i_zone, &changed);
957 add_zone_ind(7 + inode->i_zone, &changed);
958 add_zone_dind(8 + inode->i_zone, &changed);
959}
960
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000961#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000962static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000963{
964 struct minix2_inode *inode;
965
966 if (!i || i > INODES)
967 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000969 return;
970 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
972 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000973 return;
974 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 add_zone2(i + inode->i_zone, &changed);
976 add_zone_ind2(7 + inode->i_zone, &changed);
977 add_zone_dind2(8 + inode->i_zone, &changed);
978 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000979}
980#endif
981
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000982static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000983{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000984 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000985 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000987 int block;
988
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 block = map_block(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000990 read_block(block, check_file_blk);
991 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000992 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000993 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000994 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000995 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000996 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000997 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000998 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000999 }
1000 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001002 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001003 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001004 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001005 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001006 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001008 printf("%s: bad directory: '.' isn't first\n", current_name);
1009 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001010 }
1011 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001012 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001013 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001014 printf("%s: bad directory: '..' isn't second\n", current_name);
1015 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001016 }
1017 if (!inode)
1018 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001019 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001020 if (list) {
1021 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001023 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001024 }
1025 check_zones(ino);
1026 if (inode && S_ISDIR(inode->i_mode))
1027 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001028 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001029}
1030
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001031#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001032static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001033{
Eric Andersene494fdd1999-10-19 20:23:03 +00001034 struct minix2_inode *inode;
1035 int ino;
1036 char *name;
1037 int block;
1038
Erik Andersene49d5ec2000-02-08 19:58:47 +00001039 block = map_block2(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001040 read_block(block, check_file_blk);
1041 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001042 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001043 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001044 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001045 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001046 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001047 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001048 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001049 }
1050 ino = 0;
1051 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001052 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001054 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001055 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001056 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001057 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001058 printf("%s: bad directory: '.' isn't first\n", current_name);
1059 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001060 }
1061 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001062 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001063 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001064 printf("%s: bad directory: '..' isn't second\n", current_name);
1065 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001066 }
1067 if (!inode)
1068 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001069 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001070 if (list) {
1071 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001073 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001074 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075 check_zones2(ino);
1076 if (inode && S_ISDIR(inode->i_mode))
1077 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001078 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001079}
1080#endif
1081
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001082static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001083{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001084 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001085 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001086
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001087 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001088 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001089 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001090 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001091 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001092 errors_uncorrected = 1;
1093 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 for (offset = 0; offset < dir->i_size; offset += dirsize)
1095 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001096}
1097
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001098#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001099static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001100{
1101 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001102 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001103
1104 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001106 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001107 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001108 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001109 errors_uncorrected = 1;
1110 }
1111 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001112 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001113}
1114#endif
1115
Eric Andersene674eb71999-10-19 20:52:57 +00001116static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001117{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001118 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001119
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001120 if (BLOCK_SIZE * i != lseek(dev_fd, BLOCK_SIZE * i, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001121 die("seek failed in bad_zone");
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001122 return (BLOCK_SIZE != read(dev_fd, buffer, BLOCK_SIZE));
Eric Andersene494fdd1999-10-19 20:23:03 +00001123}
1124
Eric Andersene674eb71999-10-19 20:52:57 +00001125static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001126{
1127 int i;
1128
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 for (i = 1; i <= INODES; i++) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001130 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001131 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001133 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001134 changed = 1;
1135 }
1136 }
1137 if (!inode_count[i]) {
1138 if (!inode_in_use(i))
1139 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001140 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001141 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001142 unmark_inode(i);
1143 continue;
1144 }
1145 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001146 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001148 mark_inode(i);
1149 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001150 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001151 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001152 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1153 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001155 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001157 }
1158 }
1159 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001161 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001162 continue;
1163 if (!zone_count[i]) {
1164 if (bad_zone(i))
1165 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001166 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001168 unmark_zone(i);
1169 continue;
1170 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001171 printf("Zone %d: %sin use, counted=%d\n",
1172 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001173 }
1174}
1175
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001176#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001177static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001178{
1179 int i;
1180
1181 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001182 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001183 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001185 Inode2[i].i_mode = 0;
1186 changed = 1;
1187 }
1188 }
1189 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001190 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001191 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001192 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 if (ask("Clear", 1))
1194 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001195 continue;
1196 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001197 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001198 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 if (ask("Set", 1))
1200 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001201 }
1202 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001203 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001204 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1205 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001206 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001207 Inode2[i].i_nlinks = inode_count[i];
1208 changed = 1;
1209 }
1210 }
1211 }
1212 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001213 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001214 continue;
1215 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001217 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001218 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001219 if (ask("Unmark", 1))
1220 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001221 continue;
1222 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001223 printf("Zone %d: %sin use, counted=%d\n",
1224 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001225 }
1226}
1227#endif
1228
Eric Andersene674eb71999-10-19 20:52:57 +00001229static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001230{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001231 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1232 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001233 check_zones(MINIX_ROOT_INO);
1234 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001235 check_counts();
1236}
1237
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001238#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001239static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001240{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001241 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1242 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001243 check_zones2(MINIX_ROOT_INO);
1244 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001246}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001247#else
1248void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001249#endif
1250
Denis Vlasenko06af2162007-02-03 17:28:39 +00001251int fsck_minix_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +00001252int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001253{
1254 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001255 int retcode = 0;
1256
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001257 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001258
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001259 INIT_G();
John Beppuc1dc5d92000-02-11 12:43:20 +00001260
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001261 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001262 die("bad inode size");
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001263#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001264 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001265 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001266#endif
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001267 while (--argc != 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001268 argv++;
1269 if (argv[0][0] != '-') {
1270 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001271 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001272 device_name = argv[0];
1273 } else {
1274 while (*++argv[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001275 switch (argv[0][0]) {
1276 case 'l':
1277 list = 1;
1278 break;
1279 case 'a':
1280 automatic = 1;
1281 repair = 1;
1282 break;
1283 case 'r':
1284 automatic = 0;
1285 repair = 1;
1286 break;
1287 case 'v':
1288 verbose = 1;
1289 break;
1290 case 's':
1291 show = 1;
1292 break;
1293 case 'm':
1294 warn_mode = 1;
1295 break;
1296 case 'f':
1297 force = 1;
1298 break;
1299 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001300 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001301 }
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001302 }
1303 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001304 }
1305 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001306 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001307
Erik Andersene49d5ec2000-02-08 19:58:47 +00001308 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001309 if (repair && !automatic) {
1310 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001311 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001312 }
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001313 dev_fd = xopen(device_name, repair ? O_RDWR : O_RDONLY);
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001314
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001315 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001316 read_superblock();
1317
1318 /*
1319 * Determine whether or not we should continue with the checking.
1320 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001321 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001322 * command line.
1323 */
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001324 printf("%s: %s\n", applet_name, bb_banner);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001325
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001326 if (!(Super.s_state & MINIX_ERROR_FS)
1327 && (Super.s_state & MINIX_VALID_FS) && !force
1328 ) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001329 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001330 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001331 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001332 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001333 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001334 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001335 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001336 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001337
1338 read_tables();
1339
1340 if (repair && !automatic) {
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001341 tcgetattr(0, &sv_termios);
1342 tmp = sv_termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001343 tmp.c_lflag &= ~(ICANON | ECHO);
1344 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001345 termios_set = 1;
1346 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001347
Eric Andersene494fdd1999-10-19 20:23:03 +00001348 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001349 check_root2();
1350 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001351 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001352 check_root();
1353 check();
1354 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001355
Eric Andersene494fdd1999-10-19 20:23:03 +00001356 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001357 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001358
Eric Andersen1ca20a72001-03-21 07:34:27 +00001359 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001360 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001361 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001362 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Eric Andersen1ca20a72001-03-21 07:34:27 +00001363 100 * (INODES - free_cnt) / INODES);
1364 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001365 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001366 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001367 printf("%6u zones used (%u%%)\n\n"
1368 "%6u regular files\n"
1369 "%6u directories\n"
1370 "%6u character device files\n"
1371 "%6u block device files\n"
1372 "%6u links\n"
1373 "%6u symbolic links\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001374 "------\n"
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001375 "%6u files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001376 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 regular, directory, chardev, blockdev,
1378 links - 2 * directory + 1, symlinks,
1379 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001380 }
1381 if (changed) {
1382 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001383 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001384 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001385 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001386 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001387
Eric Andersene494fdd1999-10-19 20:23:03 +00001388 if (repair && !automatic)
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001389 tcsetattr(0, TCSANOW, &sv_termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001390
1391 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001392 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001393 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001394 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001395 return retcode;
1396}