blob: a2ca7a5668ebacd62e6647a1ef21664bb3b7cdcf [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
Rob Landleybc68cd12006-03-10 19:22:06 +0000125enum {
126 MINIX_ROOT_INO = 1,
127 MINIX_LINK_MAX = 250,
128 MINIX2_LINK_MAX = 65530,
129
130 MINIX_I_MAP_SLOTS = 8,
131 MINIX_Z_MAP_SLOTS = 64,
132 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
133 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
134 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
135 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
136 MINIX_VALID_FS = 0x0001, /* Clean fs. */
137 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
138
139 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
140 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
141
142 MINIX_V1 = 0x0001, /* original minix fs */
143 MINIX_V2 = 0x0002 /* minix V2 fs */
144};
145
146#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
147
Eric Andersen85c55202000-07-10 22:46:55 +0000148/*
149 * minix super-block data on disk
150 */
151struct minix_super_block {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000152 uint16_t s_ninodes;
153 uint16_t s_nzones;
154 uint16_t s_imap_blocks;
155 uint16_t s_zmap_blocks;
156 uint16_t s_firstdatazone;
157 uint16_t s_log_zone_size;
158 uint32_t s_max_size;
159 uint16_t s_magic;
160 uint16_t s_state;
161 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000162};
163
164struct minix_dir_entry {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000165 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000166 char name[0];
167};
168
Eric Andersen85c55202000-07-10 22:46:55 +0000169
170#define NAME_MAX 255 /* # chars in a file name */
171
172#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
173
Eric Andersen85c55202000-07-10 22:46:55 +0000174#ifndef BLKGETSIZE
175#define BLKGETSIZE _IO(0x12,96) /* return device size */
176#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000177
Eric Andersene494fdd1999-10-19 20:23:03 +0000178#ifndef __linux__
179#define volatile
180#endif
181
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
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197static char *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;
Rob Landley8a625492006-04-29 20:05:17 +0000200static int repair, automatic, verbose, list, show, warn_mode, force;
201static int directory, regular, blockdev, chardev, links, symlinks, total;
Eric Andersene494fdd1999-10-19 20:23:03 +0000202
Rob Landley8a625492006-04-29 20:05:17 +0000203static int changed; /* flags if the filesystem has been changed */
204static int errors_uncorrected; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000205static int dirsize = 16;
206static int namelen = 14;
Eric Andersene494fdd1999-10-19 20:23:03 +0000207static struct termios termios;
Rob Landley8a625492006-04-29 20:05:17 +0000208static int termios_set;
Eric Andersene494fdd1999-10-19 20:23:03 +0000209
Rob Landleydb2ab892006-05-03 21:39:28 +0000210static char *inode_buffer;
Eric Andersene494fdd1999-10-19 20:23:03 +0000211#define Inode (((struct minix_inode *) inode_buffer)-1)
212#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
213static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214
Eric Andersene494fdd1999-10-19 20:23:03 +0000215#define Super (*(struct minix_super_block *)super_block_buffer)
216#define INODES ((unsigned long)Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000217#ifdef CONFIG_FEATURE_MINIX2
Rob Landleyc44ab022006-06-06 19:50:16 +0000218static int version2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000219#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
220#else
221#define ZONES ((unsigned long)(Super.s_nzones))
222#endif
223#define IMAPS ((unsigned long)Super.s_imap_blocks)
224#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
225#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
226#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
227#define MAXSIZE ((unsigned long)Super.s_max_size)
228#define MAGIC (Super.s_magic)
229#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
230
231static char *inode_map;
232static char *zone_map;
233
Rob Landley8a625492006-04-29 20:05:17 +0000234static unsigned char *inode_count;
235static unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000236
Eric Andersene674eb71999-10-19 20:52:57 +0000237static void recursive_check(unsigned int ino);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000238#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000239static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000240#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000241
Rob Landley88621d72006-08-29 19:41:06 +0000242static int bit(char *a, unsigned int i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000243{
244 return (a[i >> 3] & (1<<(i & 7))) != 0;
245}
246#define inode_in_use(x) (bit(inode_map,(x)))
247#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000248
249#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
250#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
251
252#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
253#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
254
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000255static void leave(int) ATTRIBUTE_NORETURN;
Eric Andersene674eb71999-10-19 20:52:57 +0000256static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000257{
258 if (termios_set)
259 tcsetattr(0, TCSANOW, &termios);
260 exit(status);
261}
262
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263static void die(const char *str)
264{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000265 bb_error_msg("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000266 leave(8);
267}
268
Rob Landley3ea66a82006-04-29 20:08:40 +0000269/* File-name data */
270enum { MAX_DEPTH = 32 };
271static int name_depth;
272static char *current_name;
273static char *name_component[MAX_DEPTH+1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000274
Rob Landley3ea66a82006-04-29 20:08:40 +0000275/* Wed Feb 9 15:17:06 MST 2000 */
276/* dynamically allocate name_list (instead of making it static) */
Rob Landley88621d72006-08-29 19:41:06 +0000277static void alloc_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000278{
279 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
280 current_name[0] = '/';
281 current_name[1] = '\0';
282 name_component[0] = &current_name[0];
283}
284
285#ifdef CONFIG_FEATURE_CLEAN_UP
286/* execute this atexit() to deallocate name_list[] */
287/* piptigger was here */
Rob Landley88621d72006-08-29 19:41:06 +0000288static void free_current_name(void)
Rob Landley3ea66a82006-04-29 20:08:40 +0000289{
290 free(current_name);
291}
292#endif
293
294static void push_filename(const char *name)
295{
296 // /dir/dir/dir/file
297 // ^ ^ ^
298 // [0] [1] [2] <-name_component[i]
299 if (name_depth < MAX_DEPTH) {
300 int len;
301 char *p = name_component[name_depth];
302 *p++ = '/';
303 len = sprintf(p, "%.*s", namelen, name);
304 name_component[name_depth + 1] = p + len;
305 }
306 name_depth++;
307}
308
309static void pop_filename(void) {
310 name_depth--;
311 if (name_depth < MAX_DEPTH) {
312 *name_component[name_depth] = '\0';
313 if (!name_depth) {
314 current_name[0] = '/';
315 current_name[1] = '\0';
316 }
317 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000318}
319
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000321{
322 int c;
323
324 if (!repair) {
325 printf("\n");
326 errors_uncorrected = 1;
327 return 0;
328 }
329 if (automatic) {
330 printf("\n");
331 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000333 return def;
334 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000336 for (;;) {
337 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 if ((c = getchar()) == EOF) {
339 if (!def)
340 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000341 return def;
342 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000344 if (c == 'Y') {
345 def = 1;
346 break;
347 } else if (c == 'N') {
348 def = 0;
349 break;
350 } else if (c == ' ' || c == '\n')
351 break;
352 }
353 if (def)
354 printf("y\n");
355 else {
356 printf("n\n");
357 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000359 return def;
360}
361
362/*
363 * Make certain that we aren't checking a filesystem that is on a
364 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
365 * 1994 Theodore Ts'o. Also licensed under GPL.
366 */
367static void check_mount(void)
368{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 FILE *f;
370 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 int cont;
372 int fd;
373
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000375 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 while ((mnt = getmntent(f)) != NULL)
377 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000378 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000380 if (!mnt)
381 return;
382
383 /*
384 * If the root is mounted read-only, then /etc/mtab is
385 * probably not correct; so we won't issue a warning based on
386 * it.
387 */
388 fd = open(MOUNTED, O_RDWR);
389 if (fd < 0 && errno == EROFS)
390 return;
391 else
392 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393
Rob Landleyfd8409f2006-06-13 15:31:39 +0000394 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000395 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000396 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000397 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000398 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000399 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 }
402 return;
403}
404
405/*
406 * check_zone_nr checks to see that *nr is a valid zone nr. If it
407 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
408 * if an error was corrected, and returns the zone (0 for no zone
409 * or a bad zone-number).
410 */
Rob Landley8a625492006-04-29 20:05:17 +0000411static int check_zone_nr2(uint32_t *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000412{
Rob Landley3ea66a82006-04-29 20:08:40 +0000413 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000414 if (!*nr)
415 return 0;
416 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000417 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000419 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000420 else
421 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000422 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000424 *nr = 0;
425 *corrected = 1;
426 }
427 return 0;
428}
429
Rob Landley8a625492006-04-29 20:05:17 +0000430static int check_zone_nr(uint16_t *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000431{
Rob Landley8a625492006-04-29 20:05:17 +0000432 uint32_t nr32 = *nr;
433 int r = check_zone_nr2(&nr32, corrected);
434 *nr = (uint16_t)nr32;
435 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000436}
Eric Andersene494fdd1999-10-19 20:23:03 +0000437
438/*
439 * read-block reads block nr into the buffer at addr.
440 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000442{
443 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000445 return;
446 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000448 printf("%s: unable to seek to block in file '%s'\n",
449 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000450 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000451 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000452 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000453 printf("%s: bad block in file '%s'\n",
454 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000455 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000456 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000457 }
458}
459
460/*
461 * write_block writes block nr to disk.
462 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000464{
465 if (!nr)
466 return;
467 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000468 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 errors_uncorrected = 1;
471 return;
472 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000474 die("Seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000475 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000476 printf("%s: bad block in file '%s'\n",
477 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000478 errors_uncorrected = 1;
479 }
480}
481
482/*
Rob Landley8a625492006-04-29 20:05:17 +0000483 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000484 * It sets 'changed' if the inode has needed changing, and re-writes
485 * any indirect blocks with errors.
486 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000488{
Rob Landley8a625492006-04-29 20:05:17 +0000489 uint16_t ind[BLOCK_SIZE >> 1];
490 uint16_t dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000491 int blk_chg, block, result;
492
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000494 return check_zone_nr(inode->i_zone + blknr, &changed);
495 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000497 block = check_zone_nr(inode->i_zone + 7, &changed);
498 read_block(block, (char *) ind);
499 blk_chg = 0;
500 result = check_zone_nr(blknr + ind, &blk_chg);
501 if (blk_chg)
502 write_block(block, (char *) ind);
503 return result;
504 }
505 blknr -= 512;
506 block = check_zone_nr(inode->i_zone + 8, &changed);
507 read_block(block, (char *) dind);
508 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000510 if (blk_chg)
511 write_block(block, (char *) dind);
512 block = result;
513 read_block(block, (char *) ind);
514 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000516 if (blk_chg)
517 write_block(block, (char *) ind);
518 return result;
519}
520
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000521#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000523{
Rob Landley8a625492006-04-29 20:05:17 +0000524 uint32_t ind[BLOCK_SIZE >> 2];
525 uint32_t dind[BLOCK_SIZE >> 2];
526 uint32_t tind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000527 int blk_chg, block, result;
528
529 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000531 blknr -= 7;
532 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 block = check_zone_nr2(inode->i_zone + 7, &changed);
534 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000535 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000536 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000537 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000538 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000539 return result;
540 }
541 blknr -= 256;
542 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 block = check_zone_nr2(inode->i_zone + 8, &changed);
544 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000545 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000547 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000549 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000551 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 result = check_zone_nr2(ind + 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 *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000555 return result;
556 }
557 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 block = check_zone_nr2(inode->i_zone + 9, &changed);
559 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 result = check_zone_nr2(dind + (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 *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000570 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000572 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 result = check_zone_nr2(ind + blknr % 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 *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000576 return result;
577}
578#endif
579
Eric Andersene674eb71999-10-19 20:52:57 +0000580static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000581{
582 /*
583 * Set the state of the filesystem based on whether or not there
584 * are uncorrected errors. The filesystem valid flag is
585 * unconditionally set if we get this far.
586 */
587 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000588 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000589 Super.s_state |= MINIX_ERROR_FS;
590 else
591 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000592
Eric Andersene494fdd1999-10-19 20:23:03 +0000593 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000594 die("Seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000595 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000596 die("Unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000597}
598
Eric Andersene674eb71999-10-19 20:52:57 +0000599static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000600{
601 write_super_block();
602
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000604 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000605 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000606 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000608 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000609}
610
Erik Andersene49d5ec2000-02-08 19:58:47 +0000611static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000612{
613 int block;
614 char blk[BLOCK_SIZE];
615 int size;
616
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000617#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000618 if (version2)
619 block = Inode2[ROOT_INO].i_zone[0];
620 else
621#endif
622 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000623 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000624 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000626 dirsize = size;
627 namelen = size - 2;
628 return;
629 }
630 }
631 /* use defaults */
632}
633
Eric Andersene674eb71999-10-19 20:52:57 +0000634static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000635{
636 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000637 die("Seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000638 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000639 die("Unable to read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000640 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000641 namelen = 14;
642 dirsize = 16;
643 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000644 */
Eric Andersene494fdd1999-10-19 20:23:03 +0000645 if (MAGIC == MINIX_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000646 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
647 namelen = 30;
648 dirsize = 32;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000649#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000650 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 version2 = 1;
652 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
653 namelen = 30;
654 dirsize = 32;
655 version2 = 1;
656#endif
657 } else
Rob Landleyfd8409f2006-06-13 15:31:39 +0000658 die("Bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000659 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000660 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000661 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Rob Landleyfd8409f2006-06-13 15:31:39 +0000662 die("Bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000663 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Rob Landleyfd8409f2006-06-13 15:31:39 +0000664 die("Bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000665}
666
Eric Andersene674eb71999-10-19 20:52:57 +0000667static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000668{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000669 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
670 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000671 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
672 inode_count = xmalloc(INODES + 1);
673 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000674 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000675 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000676 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000677 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000679 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000680 if (NORM_FIRSTZONE != FIRSTZONE) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000681 printf("Warning: Firstzone!=Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000682 errors_uncorrected = 1;
683 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000684 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000685 if (show) {
Rob Landley8a625492006-04-29 20:05:17 +0000686 printf("%ld inodes\n"
687 "%ld blocks\n"
688 "Firstdatazone=%ld (%ld)\n"
689 "Zonesize=%d\n"
690 "Maxsize=%ld\n"
691 "Filesystem state=%d\n"
692 "namelen=%d\n\n",
693 INODES,
694 ZONES,
695 FIRSTZONE, NORM_FIRSTZONE,
696 BLOCK_SIZE << ZONESIZE,
697 MAXSIZE,
698 Super.s_state,
699 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000700 }
701}
702
Eric Andersen3e6ff902001-03-09 21:24:12 +0000703static struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000704{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000705 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000706
707 if (!nr || nr > INODES)
708 return NULL;
709 total++;
710 inode = Inode + nr;
711 if (!inode_count[nr]) {
712 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000713 printf("Inode %d is marked as 'unused', but it is used "
714 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000715 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000716 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000717 mark_inode(nr);
718 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000720 }
721 }
722 if (S_ISDIR(inode->i_mode))
723 directory++;
724 else if (S_ISREG(inode->i_mode))
725 regular++;
726 else if (S_ISCHR(inode->i_mode))
727 chardev++;
728 else if (S_ISBLK(inode->i_mode))
729 blockdev++;
730 else if (S_ISLNK(inode->i_mode))
731 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 else if (S_ISSOCK(inode->i_mode));
733 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000734 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000735 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000736 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000737
738 } else
739 links++;
740 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000741 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000742 inode_count[nr]--;
743 errors_uncorrected = 1;
744 }
745 return inode;
746}
747
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000748#ifdef CONFIG_FEATURE_MINIX2
Eric Andersen3e6ff902001-03-09 21:24:12 +0000749static struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000750{
751 struct minix2_inode *inode;
752
753 if (!nr || nr > INODES)
754 return NULL;
755 total++;
756 inode = Inode2 + nr;
757 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000759 printf("Inode %d is marked as 'unused', but it is used "
760 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000761 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000762 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000764 else
765 errors_uncorrected = 1;
766 }
767 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000769 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000770 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000771 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000772 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000773 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000775 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000776 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000777 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000778 else if (S_ISSOCK(inode->i_mode));
779 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000780 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000781 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000782 }
783 } else
784 links++;
785 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000786 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000787 inode_count[nr]--;
788 errors_uncorrected = 1;
789 }
790 return inode;
791}
792#endif
793
Eric Andersene674eb71999-10-19 20:52:57 +0000794static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000795{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000797
798 if (!inode || !S_ISDIR(inode->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000799 die("Root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000800}
801
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000802#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000804{
805 struct minix2_inode *inode = Inode2 + ROOT_INO;
806
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 if (!inode || !S_ISDIR(inode->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000808 die("Root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000809}
810#endif
811
Rob Landley8a625492006-04-29 20:05:17 +0000812static int add_zone(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000813{
814 int result;
815 int block;
816
817 result = 0;
818 block = check_zone_nr(znr, corrected);
819 if (!block)
820 return 0;
821 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000822 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000823 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000825 *znr = 0;
826 block = 0;
827 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000828 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000829 }
830 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000831 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000832 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000833 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000834 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000835 mark_zone(block);
836 }
837 if (!++zone_count[block])
838 zone_count[block]--;
839 return block;
840}
841
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000842#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000843static int add_zone2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000844{
845 int result;
846 int block;
847
848 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000850 if (!block)
851 return 0;
852 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000853 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000854 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000856 *znr = 0;
857 block = 0;
858 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000859 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000860 }
861 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000863 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000864 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 if (ask("Correct", 1))
866 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000867 }
868 if (!++zone_count[block])
869 zone_count[block]--;
870 return block;
871}
872#endif
873
Rob Landley8a625492006-04-29 20:05:17 +0000874static void add_zone_ind(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000875{
876 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000878 int block;
879
880 block = add_zone(znr, corrected);
881 if (!block)
882 return;
883 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000884 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000885 add_zone(i + (uint16_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000886 if (chg_blk)
887 write_block(block, blk);
888}
889
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000890#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000891static void add_zone_ind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000892{
893 static char blk[BLOCK_SIZE];
894 int i, chg_blk = 0;
895 int block;
896
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000898 if (!block)
899 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000900 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000901 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000902 add_zone2(i + (uint32_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000903 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000905}
906#endif
907
Rob Landley8a625492006-04-29 20:05:17 +0000908static void add_zone_dind(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000909{
910 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000912 int block;
913
914 block = add_zone(znr, corrected);
915 if (!block)
916 return;
917 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000919 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000920 if (blk_chg)
921 write_block(block, blk);
922}
923
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000924#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000925static void add_zone_dind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000926{
927 static char blk[BLOCK_SIZE];
928 int i, blk_chg = 0;
929 int block;
930
Erik Andersene49d5ec2000-02-08 19:58:47 +0000931 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000932 if (!block)
933 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000935 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000936 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000937 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000939}
940
Rob Landley8a625492006-04-29 20:05:17 +0000941static void add_zone_tind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000942{
943 static char blk[BLOCK_SIZE];
944 int i, blk_chg = 0;
945 int block;
946
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000948 if (!block)
949 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000951 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000952 add_zone_dind2(i + (uint32_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000953 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000955}
956#endif
957
Eric Andersene674eb71999-10-19 20:52:57 +0000958static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000959{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000961
962 if (!i || i > INODES)
963 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000965 return;
966 inode = Inode + i;
967 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 !S_ISLNK(inode->i_mode)) return;
969 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000970 add_zone(i + inode->i_zone, &changed);
971 add_zone_ind(7 + inode->i_zone, &changed);
972 add_zone_dind(8 + inode->i_zone, &changed);
973}
974
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000975#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000977{
978 struct minix2_inode *inode;
979
980 if (!i || i > INODES)
981 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000982 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000983 return;
984 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
986 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000987 return;
988 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 add_zone2(i + inode->i_zone, &changed);
990 add_zone_ind2(7 + inode->i_zone, &changed);
991 add_zone_dind2(8 + inode->i_zone, &changed);
992 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000993}
994#endif
995
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000997{
998 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000999 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001000 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001002 int block;
1003
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001005 read_block(block, blk);
1006 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001007 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001008 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001009 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001010 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001011 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001012 *(uint16_t *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001013 write_block(block, blk);
1014 }
1015 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001016 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001017 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001018 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001019 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001020 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 if (!inode || strcmp(".", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001022 printf("%s: bad directory: '.' isn't first\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001024 } else
1025 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001026 }
1027 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001028 if (!inode || strcmp("..", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001029 printf("%s: bad directory: '..' isn't second\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001030 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 } else
1032 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001033 }
1034 if (!inode)
1035 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001036 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001037 if (list) {
1038 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001039 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001040 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001041 }
1042 check_zones(ino);
1043 if (inode && S_ISDIR(inode->i_mode))
1044 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001045 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001046 return;
1047}
1048
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001049#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001051{
1052 static char blk[BLOCK_SIZE];
1053 struct minix2_inode *inode;
1054 int ino;
1055 char *name;
1056 int block;
1057
Erik Andersene49d5ec2000-02-08 19:58:47 +00001058 block = map_block2(dir, offset / BLOCK_SIZE);
1059 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001060 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001061 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001062 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001063 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001064 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001065 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001066 *(uint16_t *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001067 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001068 }
1069 ino = 0;
1070 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001071 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001073 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001074 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001075 if (!inode || strcmp(".", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001076 printf("%s: bad directory: '.' isn't first\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001077 errors_uncorrected = 1;
1078 } else
1079 return;
1080 }
1081 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 if (!inode || strcmp("..", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001083 printf("%s: bad directory: '..' isn't second\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001084 errors_uncorrected = 1;
1085 } else
1086 return;
1087 }
1088 if (!inode)
1089 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001090 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001091 if (list) {
1092 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001094 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001095 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001096 check_zones2(ino);
1097 if (inode && S_ISDIR(inode->i_mode))
1098 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001099 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001100 return;
1101}
1102#endif
1103
Eric Andersene674eb71999-10-19 20:52:57 +00001104static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001105{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001106 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001107 unsigned int offset;
1108
1109 dir = Inode + ino;
1110 if (!S_ISDIR(dir->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001111 die("Internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001112 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001113 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001114 errors_uncorrected = 1;
1115 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 for (offset = 0; offset < dir->i_size; offset += dirsize)
1117 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001118}
1119
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001120#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001122{
1123 struct minix2_inode *dir;
1124 unsigned int offset;
1125
1126 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 if (!S_ISDIR(dir->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001128 die("Internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001129 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001130 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001131 errors_uncorrected = 1;
1132 }
1133 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001135}
1136#endif
1137
Eric Andersene674eb71999-10-19 20:52:57 +00001138static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001139{
1140 char buffer[1024];
1141
Erik Andersene49d5ec2000-02-08 19:58:47 +00001142 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001143 die("Seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001144 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1145}
1146
Eric Andersene674eb71999-10-19 20:52:57 +00001147static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001148{
1149 int i;
1150
Erik Andersene49d5ec2000-02-08 19:58:47 +00001151 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001152 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001153 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001155 Inode[i].i_mode = 0;
1156 changed = 1;
1157 }
1158 }
1159 if (!inode_count[i]) {
1160 if (!inode_in_use(i))
1161 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001162 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001164 unmark_inode(i);
1165 continue;
1166 }
1167 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001168 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001170 mark_inode(i);
1171 }
1172 if (Inode[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001173 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1175 if (ask("Set i_nlinks to count", 1)) {
1176 Inode[i].i_nlinks = inode_count[i];
1177 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001178 }
1179 }
1180 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001182 if (zone_in_use(i) == zone_count[i])
1183 continue;
1184 if (!zone_count[i]) {
1185 if (bad_zone(i))
1186 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001187 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001188 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001189 unmark_zone(i);
1190 continue;
1191 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 printf("Zone %d: %sin use, counted=%d\n",
1193 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001194 }
1195}
1196
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001197#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001199{
1200 int i;
1201
1202 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001203 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001204 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001205 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001206 Inode2[i].i_mode = 0;
1207 changed = 1;
1208 }
1209 }
1210 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001212 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001213 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 if (ask("Clear", 1))
1215 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001216 continue;
1217 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001219 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 if (ask("Set", 1))
1221 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001222 }
1223 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001224 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001225 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1226 inode_count[i]);
1227 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001228 Inode2[i].i_nlinks = inode_count[i];
1229 changed = 1;
1230 }
1231 }
1232 }
1233 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001235 continue;
1236 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001237 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001238 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001239 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001240 if (ask("Unmark", 1))
1241 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001242 continue;
1243 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 printf("Zone %d: %sin use, counted=%d\n",
1245 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001246 }
1247}
1248#endif
1249
Eric Andersene674eb71999-10-19 20:52:57 +00001250static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001251{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001252 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1253 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001254 check_zones(ROOT_INO);
1255 recursive_check(ROOT_INO);
1256 check_counts();
1257}
1258
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001259#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001260static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001261{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001262 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1263 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1264 check_zones2(ROOT_INO);
1265 recursive_check2(ROOT_INO);
1266 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001267}
1268#endif
1269
Rob Landleydfba7412006-03-06 20:47:33 +00001270int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001271{
1272 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001273 int retcode = 0;
1274
Rob Landley3ea66a82006-04-29 20:08:40 +00001275 alloc_current_name();
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001276#ifdef CONFIG_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001277 /* Don't bother to free memory. Exit does
1278 * that automagically, so we can save a few bytes */
Rob Landley3ea66a82006-04-29 20:08:40 +00001279 atexit(free_current_name);
Eric Andersenb040d4f2000-07-25 18:01:20 +00001280#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001281
Eric Andersene494fdd1999-10-19 20:23:03 +00001282 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001283 die("Bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001284#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001285 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001286 die("Bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001287#endif
1288 while (argc-- > 1) {
1289 argv++;
1290 if (argv[0][0] != '-') {
1291 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001292 bb_show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001293 else
1294 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001295 } else
1296 while (*++argv[0])
1297 switch (argv[0][0]) {
1298 case 'l':
1299 list = 1;
1300 break;
1301 case 'a':
1302 automatic = 1;
1303 repair = 1;
1304 break;
1305 case 'r':
1306 automatic = 0;
1307 repair = 1;
1308 break;
1309 case 'v':
1310 verbose = 1;
1311 break;
1312 case 's':
1313 show = 1;
1314 break;
1315 case 'm':
1316 warn_mode = 1;
1317 break;
1318 case 'f':
1319 force = 1;
1320 break;
1321 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001322 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001323 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001324 }
1325 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001326 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001327 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001328 if (repair && !automatic) {
1329 if (!isatty(0) || !isatty(1))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001330 die("Need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001331 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001332 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersen77508b22000-12-12 23:51:43 +00001333 if (IN < 0){
Rob Landleyfd8409f2006-06-13 15:31:39 +00001334 printf("Unable to open device '%s'\n", device_name);
Eric Andersen77508b22000-12-12 23:51:43 +00001335 leave(8);
1336 }
Rob Landley8a625492006-04-29 20:05:17 +00001337 sync(); /* paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001338 read_superblock();
1339
1340 /*
1341 * Determine whether or not we should continue with the checking.
1342 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001343 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001344 * command line.
1345 */
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001346 printf("%s, %s\n", applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001347 if (!(Super.s_state & MINIX_ERROR_FS) &&
1348 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001349 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001350 printf("%s is clean, check is skipped\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001351 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001352 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001353 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001354 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001355 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001356 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001357
1358 read_tables();
1359
1360 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001361 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001362 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001363 tmp.c_lflag &= ~(ICANON | ECHO);
1364 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001365 termios_set = 1;
1366 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001367#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001368 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001369 check_root2();
1370 check2();
1371 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001372#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001373 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001374 check_root();
1375 check();
1376 }
1377 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001378 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001379
Eric Andersen1ca20a72001-03-21 07:34:27 +00001380 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001381 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001382 free_cnt++;
1383 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1384 100 * (INODES - free_cnt) / INODES);
1385 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001386 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001387 free_cnt++;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001388 printf("%6ld zones used (%ld%%)\n\n"
1389 "%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001390 "%6d directories\n"
1391 "%6d character device files\n"
1392 "%6d block device files\n"
1393 "%6d links\n"
1394 "%6d symbolic links\n"
1395 "------\n"
1396 "%6d files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001397 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001398 regular, directory, chardev, blockdev,
1399 links - 2 * directory + 1, symlinks,
1400 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001401 }
1402 if (changed) {
1403 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001404 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001405 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001407 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001408
Eric Andersene494fdd1999-10-19 20:23:03 +00001409 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001411
1412 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001413 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001414 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001415 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001416 return retcode;
1417}