blob: 44bcd332f3c1774d5194cecafa7463f87dcd9f8b [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
Eric Andersen85c55202000-07-10 22:46:55 +000093/*
94 * This is the original minix inode layout on disk.
95 * Note the 8-bit gid and atime and ctime.
96 */
97struct minix_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +000098 uint16_t i_mode;
99 uint16_t i_uid;
100 uint32_t i_size;
101 uint32_t i_time;
102 uint8_t i_gid;
103 uint8_t i_nlinks;
104 uint16_t i_zone[9];
Eric Andersen85c55202000-07-10 22:46:55 +0000105};
106
107/*
108 * The new minix inode has all the time entries, as well as
109 * long block numbers and a third indirect block (7+1+1+1
110 * instead of 7+1+1). Also, some previously 8-bit values are
111 * now 16-bit. The inode is now 64 bytes instead of 32.
112 */
113struct minix2_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000114 uint16_t i_mode;
115 uint16_t i_nlinks;
116 uint16_t i_uid;
117 uint16_t i_gid;
118 uint32_t i_size;
119 uint32_t i_atime;
120 uint32_t i_mtime;
121 uint32_t i_ctime;
122 uint32_t i_zone[10];
Eric Andersen85c55202000-07-10 22:46:55 +0000123};
124
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000125/* Believe it or not, but mount.h has this one */
126#undef BLOCK_SIZE
Rob Landleybc68cd12006-03-10 19:22:06 +0000127enum {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000128 BLOCK_SIZE = 1024,
129
Rob Landleybc68cd12006-03-10 19:22:06 +0000130 MINIX_ROOT_INO = 1,
131 MINIX_LINK_MAX = 250,
132 MINIX2_LINK_MAX = 65530,
133
134 MINIX_I_MAP_SLOTS = 8,
135 MINIX_Z_MAP_SLOTS = 64,
136 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
137 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
138 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
139 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
140 MINIX_VALID_FS = 0x0001, /* Clean fs. */
141 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
142
143 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
144 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
145
146 MINIX_V1 = 0x0001, /* original minix fs */
147 MINIX_V2 = 0x0002 /* minix V2 fs */
148};
149
150#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
151
Eric Andersen85c55202000-07-10 22:46:55 +0000152/*
153 * minix super-block data on disk
154 */
155struct minix_super_block {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000156 uint16_t s_ninodes;
157 uint16_t s_nzones;
158 uint16_t s_imap_blocks;
159 uint16_t s_zmap_blocks;
160 uint16_t s_firstdatazone;
161 uint16_t s_log_zone_size;
162 uint32_t s_max_size;
163 uint16_t s_magic;
164 uint16_t s_state;
165 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000166};
167
168struct minix_dir_entry {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000169 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000170 char name[0];
171};
172
Eric Andersen85c55202000-07-10 22:46:55 +0000173
174#define NAME_MAX 255 /* # chars in a file name */
175
176#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
177
Eric Andersen85c55202000-07-10 22:46:55 +0000178#ifndef BLKGETSIZE
179#define BLKGETSIZE _IO(0x12,96) /* return device size */
180#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000181
Rob Landleybc68cd12006-03-10 19:22:06 +0000182enum { ROOT_INO = 1 };
Eric Andersene494fdd1999-10-19 20:23:03 +0000183
184#define UPPER(size,n) ((size+((n)-1))/(n))
185#define INODE_SIZE (sizeof(struct minix_inode))
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000186#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000187#define INODE_SIZE2 (sizeof(struct minix2_inode))
188#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
189 : MINIX_INODES_PER_BLOCK))
190#else
191#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
192#endif
193#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
194
195#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
196
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000197#define PROGRAM_VERSION "1.2 - 11/11/96"
Rob Landley8a625492006-04-29 20:05:17 +0000198static char *device_name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000199static int IN;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000200static smallint repair, automatic, verbose, list, show, warn_mode, force;
Rob Landley8a625492006-04-29 20:05:17 +0000201static int directory, regular, blockdev, chardev, links, symlinks, total;
Eric Andersene494fdd1999-10-19 20:23:03 +0000202
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000203static struct termios termios;
204static smallint termios_set;
205
206static smallint changed; /* flags if the filesystem has been changed */
207static smallint errors_uncorrected; /* flag if some error was not corrected */
208//also smallint?
Eric Andersene494fdd1999-10-19 20:23:03 +0000209static int dirsize = 16;
210static int namelen = 14;
Eric Andersene494fdd1999-10-19 20:23:03 +0000211
Rob Landleydb2ab892006-05-03 21:39:28 +0000212static char *inode_buffer;
Eric Andersene494fdd1999-10-19 20:23:03 +0000213#define Inode (((struct minix_inode *) inode_buffer)-1)
214#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000215//xzalloc?
Eric Andersene494fdd1999-10-19 20:23:03 +0000216static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217
Eric Andersene494fdd1999-10-19 20:23:03 +0000218#define Super (*(struct minix_super_block *)super_block_buffer)
219#define INODES ((unsigned long)Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000220#ifdef CONFIG_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000221static smallint version2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000222#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
223#else
224#define ZONES ((unsigned long)(Super.s_nzones))
225#endif
226#define IMAPS ((unsigned long)Super.s_imap_blocks)
227#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
228#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
229#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
230#define MAXSIZE ((unsigned long)Super.s_max_size)
231#define MAGIC (Super.s_magic)
232#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
233
234static char *inode_map;
235static char *zone_map;
236
Rob Landley8a625492006-04-29 20:05:17 +0000237static unsigned char *inode_count;
238static unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000239
Eric Andersene674eb71999-10-19 20:52:57 +0000240static void recursive_check(unsigned int ino);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000241#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000242static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000243#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000244
Rob Landley88621d72006-08-29 19:41:06 +0000245static int bit(char *a, unsigned int i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000246{
247 return (a[i >> 3] & (1<<(i & 7))) != 0;
248}
249#define inode_in_use(x) (bit(inode_map,(x)))
250#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000251
252#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
253#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
254
255#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
256#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
257
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000258static void leave(int) ATTRIBUTE_NORETURN;
Eric Andersene674eb71999-10-19 20:52:57 +0000259static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000260{
261 if (termios_set)
262 tcsetattr(0, TCSANOW, &termios);
263 exit(status);
264}
265
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266static void die(const char *str)
267{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000268 bb_error_msg("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000269 leave(8);
270}
271
Rob Landley3ea66a82006-04-29 20:08:40 +0000272/* File-name data */
273enum { MAX_DEPTH = 32 };
274static int name_depth;
275static char *current_name;
276static char *name_component[MAX_DEPTH+1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000277
Rob Landley3ea66a82006-04-29 20:08:40 +0000278/* Wed Feb 9 15:17:06 MST 2000 */
279/* dynamically allocate name_list (instead of making it static) */
Rob Landley88621d72006-08-29 19:41:06 +0000280static void alloc_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000281{
282 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
283 current_name[0] = '/';
284 current_name[1] = '\0';
285 name_component[0] = &current_name[0];
286}
287
288#ifdef CONFIG_FEATURE_CLEAN_UP
289/* execute this atexit() to deallocate name_list[] */
290/* piptigger was here */
Rob Landley88621d72006-08-29 19:41:06 +0000291static void free_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000292{
293 free(current_name);
294}
295#endif
296
297static void push_filename(const char *name)
298{
299 // /dir/dir/dir/file
300 // ^ ^ ^
301 // [0] [1] [2] <-name_component[i]
302 if (name_depth < MAX_DEPTH) {
303 int len;
304 char *p = name_component[name_depth];
305 *p++ = '/';
306 len = sprintf(p, "%.*s", namelen, name);
307 name_component[name_depth + 1] = p + len;
308 }
309 name_depth++;
310}
311
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000312static void pop_filename(void)
313{
Rob Landley3ea66a82006-04-29 20:08:40 +0000314 name_depth--;
315 if (name_depth < MAX_DEPTH) {
316 *name_component[name_depth] = '\0';
317 if (!name_depth) {
318 current_name[0] = '/';
319 current_name[1] = '\0';
320 }
321 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000322}
323
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000325{
326 int c;
327
328 if (!repair) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000329 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000330 errors_uncorrected = 1;
331 return 0;
332 }
333 if (automatic) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000334 puts("");
Eric Andersene494fdd1999-10-19 20:23:03 +0000335 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000337 return def;
338 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000339 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000340 for (;;) {
341 fflush(stdout);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000342 c = getchar();
343 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 if (!def)
345 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000346 return def;
347 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000348 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000349 if (c == 'Y') {
350 def = 1;
351 break;
352 } else if (c == 'N') {
353 def = 0;
354 break;
355 } else if (c == ' ' || c == '\n')
356 break;
357 }
358 if (def)
359 printf("y\n");
360 else {
361 printf("n\n");
362 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000364 return def;
365}
366
367/*
368 * Make certain that we aren't checking a filesystem that is on a
369 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
370 * 1994 Theodore Ts'o. Also licensed under GPL.
371 */
372static void check_mount(void)
373{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 FILE *f;
375 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000376 int cont;
377 int fd;
378
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000379 f = setmntent(MOUNTED, "r");
380 if (f == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 while ((mnt = getmntent(f)) != NULL)
383 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000384 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 if (!mnt)
387 return;
388
389 /*
390 * If the root is mounted read-only, then /etc/mtab is
391 * probably not correct; so we won't issue a warning based on
392 * it.
393 */
394 fd = open(MOUNTED, O_RDWR);
395 if (fd < 0 && errno == EROFS)
396 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000397 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398
Rob Landleyfd8409f2006-06-13 15:31:39 +0000399 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000400 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000402 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000403 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000404 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000406 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000407}
408
409/*
410 * check_zone_nr checks to see that *nr is a valid zone nr. If it
411 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
412 * if an error was corrected, and returns the zone (0 for no zone
413 * or a bad zone-number).
414 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000415static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000416{
Rob Landley3ea66a82006-04-29 20:08:40 +0000417 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 if (!*nr)
419 return 0;
420 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000421 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000422 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000423 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000424 else
425 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000426 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000428 *nr = 0;
429 *corrected = 1;
430 }
431 return 0;
432}
433
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000434static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000435{
Rob Landley8a625492006-04-29 20:05:17 +0000436 uint32_t nr32 = *nr;
437 int r = check_zone_nr2(&nr32, corrected);
438 *nr = (uint16_t)nr32;
439 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000440}
Eric Andersene494fdd1999-10-19 20:23:03 +0000441
442/*
443 * read-block reads block nr into the buffer at addr.
444 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000446{
447 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 return;
450 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000452 printf("%s: cannot seek to block in file '%s'\n",
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000453 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000454 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000455 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000456 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000457 printf("%s: bad block in file '%s'\n",
458 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000459 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000460 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000461 }
462}
463
464/*
465 * write_block writes block nr to disk.
466 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000468{
469 if (!nr)
470 return;
471 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000472 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 errors_uncorrected = 1;
475 return;
476 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000478 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000480 printf("%s: bad block in file '%s'\n",
481 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000482 errors_uncorrected = 1;
483 }
484}
485
486/*
Rob Landley8a625492006-04-29 20:05:17 +0000487 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000488 * It sets 'changed' if the inode has needed changing, and re-writes
489 * any indirect blocks with errors.
490 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000491static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000492{
Rob Landley8a625492006-04-29 20:05:17 +0000493 uint16_t ind[BLOCK_SIZE >> 1];
494 uint16_t dind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000495 int block, result;
496 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000497
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000499 return check_zone_nr(inode->i_zone + blknr, &changed);
500 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 block = check_zone_nr(inode->i_zone + 7, &changed);
503 read_block(block, (char *) ind);
504 blk_chg = 0;
505 result = check_zone_nr(blknr + ind, &blk_chg);
506 if (blk_chg)
507 write_block(block, (char *) ind);
508 return result;
509 }
510 blknr -= 512;
511 block = check_zone_nr(inode->i_zone + 8, &changed);
512 read_block(block, (char *) dind);
513 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000515 if (blk_chg)
516 write_block(block, (char *) dind);
517 block = result;
518 read_block(block, (char *) ind);
519 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 if (blk_chg)
522 write_block(block, (char *) ind);
523 return result;
524}
525
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000526#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000528{
Rob Landley8a625492006-04-29 20:05:17 +0000529 uint32_t ind[BLOCK_SIZE >> 2];
530 uint32_t dind[BLOCK_SIZE >> 2];
531 uint32_t tind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000532 int block, result;
533 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000534
535 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000537 blknr -= 7;
538 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 block = check_zone_nr2(inode->i_zone + 7, &changed);
540 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000543 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000545 return result;
546 }
547 blknr -= 256;
548 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 block = check_zone_nr2(inode->i_zone + 8, &changed);
550 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000551 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000553 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000555 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000557 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 return result;
562 }
563 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 block = check_zone_nr2(inode->i_zone + 9, &changed);
565 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000568 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000570 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000572 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000576 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000578 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000580 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000582 return result;
583}
584#endif
585
Eric Andersene674eb71999-10-19 20:52:57 +0000586static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000587{
588 /*
589 * Set the state of the filesystem based on whether or not there
590 * are uncorrected errors. The filesystem valid flag is
591 * unconditionally set if we get this far.
592 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000593 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
594 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000595 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596
Eric Andersene494fdd1999-10-19 20:23:03 +0000597 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000598 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000600 die("cannot write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000601}
602
Eric Andersene674eb71999-10-19 20:52:57 +0000603static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000604{
605 write_super_block();
606
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000608 die("cannot write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000610 die("cannot write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000612 die("cannot write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000613}
614
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000616{
617 int block;
618 char blk[BLOCK_SIZE];
619 int size;
620
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000621#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000622 if (version2)
623 block = Inode2[ROOT_INO].i_zone[0];
624 else
625#endif
626 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000628 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000629 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000630 dirsize = size;
631 namelen = size - 2;
632 return;
633 }
634 }
635 /* use defaults */
636}
637
Eric Andersene674eb71999-10-19 20:52:57 +0000638static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000639{
640 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000641 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000642 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000643 die("cannot read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000644 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000645 namelen = 14;
646 dirsize = 16;
647 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000648 */
Eric Andersene494fdd1999-10-19 20:23:03 +0000649 if (MAGIC == MINIX_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000650 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
651 namelen = 30;
652 dirsize = 32;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000653#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000655 version2 = 1;
656 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
657 namelen = 30;
658 dirsize = 32;
659 version2 = 1;
660#endif
661 } else
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000662 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000663 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000664 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000665 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000666 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000667 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000668 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000669}
670
Eric Andersene674eb71999-10-19 20:52:57 +0000671static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000672{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000673 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
674 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000675 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
676 inode_count = xmalloc(INODES + 1);
677 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000679 die("cannot read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000680 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000681 die("cannot read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000683 die("cannot read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000684 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000685 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000686 errors_uncorrected = 1;
687 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000688 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000689 if (show) {
Rob Landley8a625492006-04-29 20:05:17 +0000690 printf("%ld inodes\n"
691 "%ld blocks\n"
692 "Firstdatazone=%ld (%ld)\n"
693 "Zonesize=%d\n"
694 "Maxsize=%ld\n"
695 "Filesystem state=%d\n"
696 "namelen=%d\n\n",
697 INODES,
698 ZONES,
699 FIRSTZONE, NORM_FIRSTZONE,
700 BLOCK_SIZE << ZONESIZE,
701 MAXSIZE,
702 Super.s_state,
703 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000704 }
705}
706
Eric Andersen3e6ff902001-03-09 21:24:12 +0000707static struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000708{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000710
711 if (!nr || nr > INODES)
712 return NULL;
713 total++;
714 inode = Inode + nr;
715 if (!inode_count[nr]) {
716 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000717 printf("Inode %d is marked as 'unused', but it is used "
718 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000720 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000721 mark_inode(nr);
722 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000724 }
725 }
726 if (S_ISDIR(inode->i_mode))
727 directory++;
728 else if (S_ISREG(inode->i_mode))
729 regular++;
730 else if (S_ISCHR(inode->i_mode))
731 chardev++;
732 else if (S_ISBLK(inode->i_mode))
733 blockdev++;
734 else if (S_ISLNK(inode->i_mode))
735 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 else if (S_ISSOCK(inode->i_mode));
737 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000738 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000739 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000741
742 } else
743 links++;
744 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000745 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000746 inode_count[nr]--;
747 errors_uncorrected = 1;
748 }
749 return inode;
750}
751
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000752#ifdef CONFIG_FEATURE_MINIX2
Eric Andersen3e6ff902001-03-09 21:24:12 +0000753static struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000754{
755 struct minix2_inode *inode;
756
757 if (!nr || nr > INODES)
758 return NULL;
759 total++;
760 inode = Inode2 + nr;
761 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000763 printf("Inode %d is marked as 'unused', but it is used "
764 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000765 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000766 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000768 else
769 errors_uncorrected = 1;
770 }
771 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000772 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000773 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000775 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000777 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000779 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000781 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 else if (S_ISSOCK(inode->i_mode));
783 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000784 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000785 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000786 }
787 } else
788 links++;
789 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000790 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000791 inode_count[nr]--;
792 errors_uncorrected = 1;
793 }
794 return inode;
795}
796#endif
797
Eric Andersene674eb71999-10-19 20:52:57 +0000798static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000799{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000800 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000801
802 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000803 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000804}
805
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000806#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000808{
809 struct minix2_inode *inode = Inode2 + ROOT_INO;
810
Erik Andersene49d5ec2000-02-08 19:58:47 +0000811 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000812 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000813}
814#endif
815
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000816static int add_zone(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000817{
818 int result;
819 int block;
820
821 result = 0;
822 block = check_zone_nr(znr, corrected);
823 if (!block)
824 return 0;
825 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000826 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000827 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000829 *znr = 0;
830 block = 0;
831 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000832 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000833 }
834 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000835 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000836 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000837 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000839 mark_zone(block);
840 }
841 if (!++zone_count[block])
842 zone_count[block]--;
843 return block;
844}
845
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000846#ifdef CONFIG_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000847static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000848{
849 int result;
850 int block;
851
852 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000853 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000854 if (!block)
855 return 0;
856 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000857 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000858 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000860 *znr = 0;
861 block = 0;
862 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000863 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000864 }
865 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000866 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000867 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000868 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 if (ask("Correct", 1))
870 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000871 }
872 if (!++zone_count[block])
873 zone_count[block]--;
874 return block;
875}
876#endif
877
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000878static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000879{
880 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000881 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000882 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000883 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000884
885 block = add_zone(znr, corrected);
886 if (!block)
887 return;
888 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000890 add_zone(i + (uint16_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000891 if (chg_blk)
892 write_block(block, blk);
893}
894
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000895#ifdef CONFIG_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000896static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000897{
898 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000899 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000900 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000901 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000902
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000904 if (!block)
905 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000907 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000908 add_zone2(i + (uint32_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000911}
912#endif
913
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000914static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000915{
916 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000917 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000918 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000919 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000920
921 block = add_zone(znr, corrected);
922 if (!block)
923 return;
924 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000926 add_zone_ind(i + (uint16_t *) blk, &chg_blk);
927 if (chg_blk)
Eric Andersene494fdd1999-10-19 20:23:03 +0000928 write_block(block, blk);
929}
930
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000931#ifdef CONFIG_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000932static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000933{
934 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000935 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000936 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000937 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000938
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000940 if (!block)
941 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000943 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000944 add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
945 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000947}
948
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000949static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000950{
951 static char blk[BLOCK_SIZE];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000952 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000953 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000954 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000955
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000957 if (!block)
958 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000960 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000961 add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
962 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000964}
965#endif
966
Eric Andersene674eb71999-10-19 20:52:57 +0000967static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000968{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000970
971 if (!i || i > INODES)
972 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000973 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000974 return;
975 inode = Inode + i;
976 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977 !S_ISLNK(inode->i_mode)) return;
978 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000979 add_zone(i + inode->i_zone, &changed);
980 add_zone_ind(7 + inode->i_zone, &changed);
981 add_zone_dind(8 + inode->i_zone, &changed);
982}
983
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000984#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000986{
987 struct minix2_inode *inode;
988
989 if (!i || i > INODES)
990 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000991 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000992 return;
993 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
995 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000996 return;
997 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 add_zone2(i + inode->i_zone, &changed);
999 add_zone_ind2(7 + inode->i_zone, &changed);
1000 add_zone_dind2(8 + inode->i_zone, &changed);
1001 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001002}
1003#endif
1004
Erik Andersene49d5ec2000-02-08 19:58:47 +00001005static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001006{
1007 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001009 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001011 int block;
1012
Erik Andersene49d5ec2000-02-08 19:58:47 +00001013 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001014 read_block(block, blk);
1015 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001016 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001017 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001018 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001019 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001020 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001021 *(uint16_t *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001022 write_block(block, blk);
1023 }
1024 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001026 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001027 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001028 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001029 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001030 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001032 printf("%s: bad directory: '.' isn't first\n", current_name);
1033 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001034 }
1035 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001036 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001038 printf("%s: bad directory: '..' isn't second\n", current_name);
1039 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001040 }
1041 if (!inode)
1042 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001043 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001044 if (list) {
1045 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001047 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001048 }
1049 check_zones(ino);
1050 if (inode && S_ISDIR(inode->i_mode))
1051 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001052 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001053}
1054
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001055#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001057{
1058 static char blk[BLOCK_SIZE];
1059 struct minix2_inode *inode;
1060 int ino;
1061 char *name;
1062 int block;
1063
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 block = map_block2(dir, offset / BLOCK_SIZE);
1065 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001066 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001067 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001068 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001069 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001070 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001071 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001072 *(uint16_t *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001073 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001074 }
1075 ino = 0;
1076 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001077 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001079 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001080 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001081 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001082 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001083 printf("%s: bad directory: '.' isn't first\n", current_name);
1084 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001085 }
1086 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001087 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001088 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001089 printf("%s: bad directory: '..' isn't second\n", current_name);
1090 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001091 }
1092 if (!inode)
1093 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001094 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001095 if (list) {
1096 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001098 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001099 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100 check_zones2(ino);
1101 if (inode && S_ISDIR(inode->i_mode))
1102 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001103 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001104}
1105#endif
1106
Eric Andersene674eb71999-10-19 20:52:57 +00001107static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001108{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001109 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001110 unsigned int offset;
1111
1112 dir = Inode + ino;
1113 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001114 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001115 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001116 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001117 errors_uncorrected = 1;
1118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001119 for (offset = 0; offset < dir->i_size; offset += dirsize)
1120 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001121}
1122
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001123#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001124static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001125{
1126 struct minix2_inode *dir;
1127 unsigned int offset;
1128
1129 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001130 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001131 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001132 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001133 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001134 errors_uncorrected = 1;
1135 }
1136 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001137 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001138}
1139#endif
1140
Eric Andersene674eb71999-10-19 20:52:57 +00001141static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001142{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001143 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001144
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001146 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001147 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1148}
1149
Eric Andersene674eb71999-10-19 20:52:57 +00001150static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001151{
1152 int i;
1153
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001155 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001156 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001158 Inode[i].i_mode = 0;
1159 changed = 1;
1160 }
1161 }
1162 if (!inode_count[i]) {
1163 if (!inode_in_use(i))
1164 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001165 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001167 unmark_inode(i);
1168 continue;
1169 }
1170 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001171 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001173 mark_inode(i);
1174 }
1175 if (Inode[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001176 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001177 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1178 if (ask("Set i_nlinks to count", 1)) {
1179 Inode[i].i_nlinks = inode_count[i];
1180 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001181 }
1182 }
1183 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001185 if (zone_in_use(i) == zone_count[i])
1186 continue;
1187 if (!zone_count[i]) {
1188 if (bad_zone(i))
1189 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001190 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001192 unmark_zone(i);
1193 continue;
1194 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 printf("Zone %d: %sin use, counted=%d\n",
1196 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001197 }
1198}
1199
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001200#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001201static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001202{
1203 int i;
1204
1205 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001206 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001207 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001209 Inode2[i].i_mode = 0;
1210 changed = 1;
1211 }
1212 }
1213 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001215 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001216 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 if (ask("Clear", 1))
1218 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001219 continue;
1220 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001221 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001222 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001223 if (ask("Set", 1))
1224 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001225 }
1226 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001227 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001228 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1229 inode_count[i]);
1230 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001231 Inode2[i].i_nlinks = inode_count[i];
1232 changed = 1;
1233 }
1234 }
1235 }
1236 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001237 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001238 continue;
1239 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001241 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001242 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 if (ask("Unmark", 1))
1244 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001245 continue;
1246 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001247 printf("Zone %d: %sin use, counted=%d\n",
1248 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001249 }
1250}
1251#endif
1252
Eric Andersene674eb71999-10-19 20:52:57 +00001253static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001254{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001255 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1256 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001257 check_zones(ROOT_INO);
1258 recursive_check(ROOT_INO);
1259 check_counts();
1260}
1261
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001262#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001263static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001264{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001265 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1266 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1267 check_zones2(ROOT_INO);
1268 recursive_check2(ROOT_INO);
1269 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001270}
1271#endif
1272
Rob Landleydfba7412006-03-06 20:47:33 +00001273int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001274{
1275 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001276 int retcode = 0;
1277
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001278 xfunc_exitcode = 8;
1279
Rob Landley3ea66a82006-04-29 20:08:40 +00001280 alloc_current_name();
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001281#ifdef CONFIG_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001282 /* Don't bother to free memory. Exit does
1283 * that automagically, so we can save a few bytes */
Rob Landley3ea66a82006-04-29 20:08:40 +00001284 atexit(free_current_name);
Eric Andersenb040d4f2000-07-25 18:01:20 +00001285#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001286
Eric Andersene494fdd1999-10-19 20:23:03 +00001287 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001288 die("bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001289#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001290 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001291 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001292#endif
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001293 while (--argc != 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001294 argv++;
1295 if (argv[0][0] != '-') {
1296 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001297 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001298 device_name = argv[0];
1299 } else {
1300 while (*++argv[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001301 switch (argv[0][0]) {
1302 case 'l':
1303 list = 1;
1304 break;
1305 case 'a':
1306 automatic = 1;
1307 repair = 1;
1308 break;
1309 case 'r':
1310 automatic = 0;
1311 repair = 1;
1312 break;
1313 case 'v':
1314 verbose = 1;
1315 break;
1316 case 's':
1317 show = 1;
1318 break;
1319 case 'm':
1320 warn_mode = 1;
1321 break;
1322 case 'f':
1323 force = 1;
1324 break;
1325 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001326 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001327 }
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001328 }
1329 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001330 }
1331 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001332 bb_show_usage();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001333
Erik Andersene49d5ec2000-02-08 19:58:47 +00001334 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001335 if (repair && !automatic) {
1336 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001337 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001338 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001339 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001340 if (IN < 0) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001341 bb_error_msg_and_die("cannot open device '%s'", device_name);
Eric Andersen77508b22000-12-12 23:51:43 +00001342 }
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001343 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001344 read_superblock();
1345
1346 /*
1347 * Determine whether or not we should continue with the checking.
1348 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001349 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001350 * command line.
1351 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001352 printf("%s, "PROGRAM_VERSION"\n", applet_name);
1353
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001354 if (!(Super.s_state & MINIX_ERROR_FS)
1355 && (Super.s_state & MINIX_VALID_FS) && !force
1356 ) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001357 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001358 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001359 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001360 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001361 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001362 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001363 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001364 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001365
1366 read_tables();
1367
1368 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001369 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001370 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001371 tmp.c_lflag &= ~(ICANON | ECHO);
1372 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001373 termios_set = 1;
1374 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001375#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001376 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 check_root2();
1378 check2();
1379 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001380#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001381 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001382 check_root();
1383 check();
1384 }
1385 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001386 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001387
Eric Andersen1ca20a72001-03-21 07:34:27 +00001388 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001389 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001390 free_cnt++;
1391 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1392 100 * (INODES - free_cnt) / INODES);
1393 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001394 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001395 free_cnt++;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001396 printf("%6ld zones used (%ld%%)\n\n"
1397 "%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001398 "%6d directories\n"
1399 "%6d character device files\n"
1400 "%6d block device files\n"
1401 "%6d links\n"
1402 "%6d symbolic links\n"
1403 "------\n"
1404 "%6d files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001405 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406 regular, directory, chardev, blockdev,
1407 links - 2 * directory + 1, symlinks,
1408 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001409 }
1410 if (changed) {
1411 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001412 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001413 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001414 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001415 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001416
Eric Andersene494fdd1999-10-19 20:23:03 +00001417 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001418 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001419
1420 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001422 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001423 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001424 return retcode;
1425}