blob: 350c957862f4dab1cabb284f4694b96900f665c5 [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 *
5 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6 * as per the GNU copyleft.
7 */
8
9/*
Eric Andersenaff114c2004-04-14 17:51:38 +000010 * 09.11.91 - made the first rudimentary functions
Eric Andersene494fdd1999-10-19 20:23:03 +000011 *
12 * 10.11.91 - updated, does checking, no repairs yet.
13 * Sent out to the mailing-list for testing.
14 *
15 * 14.11.91 - Testing seems to have gone well. Added some
16 * correction-code, and changed some functions.
17 *
18 * 15.11.91 - More correction code. Hopefully it notices most
19 * cases now, and tries to do something about them.
20 *
21 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
22 * things seem to work now. Yeah, sure.
23 *
24 *
25 * 19.04.92 - Had to start over again from this old version, as a
26 * kernel bug ate my enhanced fsck in february.
27 *
28 * 28.02.93 - added support for different directory entry sizes..
29 *
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31 * super-block information
32 *
33 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34 * to that required by fsutil
35 *
36 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
37 * Added support for file system valid flag. Also
38 * added program_version variable and output of
39 * program name and version number when program
40 * is executed.
41 *
42 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
44 *
45 * 10.12.94 - added test to prevent checking of mounted fs adapted
46 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
47 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
48 *
49 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
50 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
51 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000052 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
Eric Andersene494fdd1999-10-19 20:23:03 +000053 * (Russell King). He made them for ARM. It would seem
54 * that the ARM is powerful enough to do this in C whereas
55 * i386 and m64k must use assembly to get it fast >:-)
Eric Andersenaff114c2004-04-14 17:51:38 +000056 * This should make minix fsck system-independent.
Eric Andersene494fdd1999-10-19 20:23:03 +000057 * (janl@math.uio.no, Nicolai Langfeldt)
58 *
59 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 * warnings. Added mc68k bitops from
Eric Andersene494fdd1999-10-19 20:23:03 +000061 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
62 *
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
64 * Andreas Schwab.
65 *
66 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67 * - added Native Language Support
68 *
69 *
70 * I've had no time to add comments - hopefully the function names
71 * are comments enough. As with all file system checkers, this assumes
72 * the file system is quiescent - don't use it on a mounted device
73 * unless you can be sure nobody is writing to it (and remember that the
74 * kernel can write to it when it searches for files).
75 *
Eric Andersenaff114c2004-04-14 17:51:38 +000076 * Usage: fsck [-larvsm] device
Eric Andersene494fdd1999-10-19 20:23:03 +000077 * -l for a listing of all the filenames
78 * -a for automatic repairs (not implemented)
79 * -r for repairs (interactive) (not implemented)
80 * -v for verbose (tells how many files)
81 * -s for super-block info
82 * -m for minix-like "mode not cleared" warnings
83 * -f force filesystem check even if filesystem marked as valid
84 *
85 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086 * enforced (but it's not much fun on a character device :-).
Eric Andersene494fdd1999-10-19 20:23:03 +000087 */
88
89#include <stdio.h>
90#include <errno.h>
91#include <unistd.h>
92#include <string.h>
93#include <fcntl.h>
94#include <ctype.h>
95#include <stdlib.h>
96#include <termios.h>
97#include <mntent.h>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000098#include <sys/param.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000099#include "busybox.h"
Eric Andersen85c55202000-07-10 22:46:55 +0000100
Rob Landleybc68cd12006-03-10 19:22:06 +0000101#define BLOCK_SIZE_BITS 10
102#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
Eric Andersen85c55202000-07-10 22:46:55 +0000103
104/*
105 * This is the original minix inode layout on disk.
106 * Note the 8-bit gid and atime and ctime.
107 */
108struct minix_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000109 uint16_t i_mode;
110 uint16_t i_uid;
111 uint32_t i_size;
112 uint32_t i_time;
113 uint8_t i_gid;
114 uint8_t i_nlinks;
115 uint16_t i_zone[9];
Eric Andersen85c55202000-07-10 22:46:55 +0000116};
117
118/*
119 * The new minix inode has all the time entries, as well as
120 * long block numbers and a third indirect block (7+1+1+1
121 * instead of 7+1+1). Also, some previously 8-bit values are
122 * now 16-bit. The inode is now 64 bytes instead of 32.
123 */
124struct minix2_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000125 uint16_t i_mode;
126 uint16_t i_nlinks;
127 uint16_t i_uid;
128 uint16_t i_gid;
129 uint32_t i_size;
130 uint32_t i_atime;
131 uint32_t i_mtime;
132 uint32_t i_ctime;
133 uint32_t i_zone[10];
Eric Andersen85c55202000-07-10 22:46:55 +0000134};
135
Rob Landleybc68cd12006-03-10 19:22:06 +0000136enum {
137 MINIX_ROOT_INO = 1,
138 MINIX_LINK_MAX = 250,
139 MINIX2_LINK_MAX = 65530,
140
141 MINIX_I_MAP_SLOTS = 8,
142 MINIX_Z_MAP_SLOTS = 64,
143 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
144 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
145 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
146 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
147 MINIX_VALID_FS = 0x0001, /* Clean fs. */
148 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
149
150 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
151 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
152
153 MINIX_V1 = 0x0001, /* original minix fs */
154 MINIX_V2 = 0x0002 /* minix V2 fs */
155};
156
157#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
158
Eric Andersen85c55202000-07-10 22:46:55 +0000159/*
160 * minix super-block data on disk
161 */
162struct minix_super_block {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000163 uint16_t s_ninodes;
164 uint16_t s_nzones;
165 uint16_t s_imap_blocks;
166 uint16_t s_zmap_blocks;
167 uint16_t s_firstdatazone;
168 uint16_t s_log_zone_size;
169 uint32_t s_max_size;
170 uint16_t s_magic;
171 uint16_t s_state;
172 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000173};
174
175struct minix_dir_entry {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000176 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000177 char name[0];
178};
179
Eric Andersen85c55202000-07-10 22:46:55 +0000180
181#define NAME_MAX 255 /* # chars in a file name */
182
183#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
184
Eric Andersen85c55202000-07-10 22:46:55 +0000185#ifndef BLKGETSIZE
186#define BLKGETSIZE _IO(0x12,96) /* return device size */
187#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000188
Eric Andersene494fdd1999-10-19 20:23:03 +0000189#ifndef __linux__
190#define volatile
191#endif
192
Rob Landleybc68cd12006-03-10 19:22:06 +0000193enum { ROOT_INO = 1 };
Eric Andersene494fdd1999-10-19 20:23:03 +0000194
195#define UPPER(size,n) ((size+((n)-1))/(n))
196#define INODE_SIZE (sizeof(struct minix_inode))
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000198#define INODE_SIZE2 (sizeof(struct minix2_inode))
199#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
200 : MINIX_INODES_PER_BLOCK))
201#else
202#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
203#endif
204#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
205
206#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208static char *program_version = "1.2 - 11/11/96";
209static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000210static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
212 0, warn_mode = 0, force = 0;
213static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
214 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000215
Erik Andersene49d5ec2000-02-08 19:58:47 +0000216static int changed = 0; /* flags if the filesystem has been changed */
217static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000218static int dirsize = 16;
219static int namelen = 14;
220static int version2 = 0;
221static struct termios termios;
222static int termios_set = 0;
223
224/* File-name data */
Rob Landleybc68cd12006-03-10 19:22:06 +0000225enum { MAX_DEPTH = 32 };
Eric Andersene494fdd1999-10-19 20:23:03 +0000226static int name_depth = 0;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000227// static char name_list[MAX_DEPTH][BUFSIZ + 1];
John Beppu5e1b2ca2000-02-11 12:52:55 +0000228static char **name_list = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230static char *inode_buffer = NULL;
231
Eric Andersene494fdd1999-10-19 20:23:03 +0000232#define Inode (((struct minix_inode *) inode_buffer)-1)
233#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
234static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235
Eric Andersene494fdd1999-10-19 20:23:03 +0000236#define Super (*(struct minix_super_block *)super_block_buffer)
237#define INODES ((unsigned long)Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000238#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000239#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
240#else
241#define ZONES ((unsigned long)(Super.s_nzones))
242#endif
243#define IMAPS ((unsigned long)Super.s_imap_blocks)
244#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
245#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
246#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
247#define MAXSIZE ((unsigned long)Super.s_max_size)
248#define MAGIC (Super.s_magic)
249#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
250
251static char *inode_map;
252static char *zone_map;
253
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254static unsigned char *inode_count = NULL;
255static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000256
Eric Andersene674eb71999-10-19 20:52:57 +0000257static void recursive_check(unsigned int ino);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000258#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000259static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000260#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000261
Eric Andersen56f3e352000-09-19 21:13:55 +0000262static inline int bit(char * a,unsigned int i)
263{
264 return (a[i >> 3] & (1<<(i & 7))) != 0;
265}
266#define inode_in_use(x) (bit(inode_map,(x)))
267#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000268
269#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
270#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
271
272#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
273#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
274
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000275static void leave(int) ATTRIBUTE_NORETURN;
Eric Andersene674eb71999-10-19 20:52:57 +0000276static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000277{
278 if (termios_set)
279 tcsetattr(0, TCSANOW, &termios);
280 exit(status);
281}
282
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283static void die(const char *str)
284{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000285 bb_error_msg("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000286 leave(8);
287}
288
289/*
290 * This simply goes through the file-name data and prints out the
291 * current file.
292 */
Eric Andersene674eb71999-10-19 20:52:57 +0000293static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000294{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000295 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 while (i < name_depth)
298 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000299 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000301}
302
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304{
305 int c;
306
307 if (!repair) {
308 printf("\n");
309 errors_uncorrected = 1;
310 return 0;
311 }
312 if (automatic) {
313 printf("\n");
314 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000316 return def;
317 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000319 for (;;) {
320 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 if ((c = getchar()) == EOF) {
322 if (!def)
323 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000324 return def;
325 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000327 if (c == 'Y') {
328 def = 1;
329 break;
330 } else if (c == 'N') {
331 def = 0;
332 break;
333 } else if (c == ' ' || c == '\n')
334 break;
335 }
336 if (def)
337 printf("y\n");
338 else {
339 printf("n\n");
340 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 return def;
343}
344
345/*
346 * Make certain that we aren't checking a filesystem that is on a
347 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
348 * 1994 Theodore Ts'o. Also licensed under GPL.
349 */
350static void check_mount(void)
351{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 FILE *f;
353 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000354 int cont;
355 int fd;
356
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 while ((mnt = getmntent(f)) != NULL)
360 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000363 if (!mnt)
364 return;
365
366 /*
367 * If the root is mounted read-only, then /etc/mtab is
368 * probably not correct; so we won't issue a warning based on
369 * it.
370 */
371 fd = open(MOUNTED, O_RDWR);
372 if (fd < 0 && errno == EROFS)
373 return;
374 else
375 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376
377 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000378 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000379 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000380 else
381 cont = 0;
382 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 printf("check aborted.\n");
384 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000385 }
386 return;
387}
388
389/*
390 * check_zone_nr checks to see that *nr is a valid zone nr. If it
391 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
392 * if an error was corrected, and returns the zone (0 for no zone
393 * or a bad zone-number).
394 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000395static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000396{
397 if (!*nr)
398 return 0;
399 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000400 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000401 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000402 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000403 else
404 return *nr;
405 print_current_name();
406 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 *nr = 0;
409 *corrected = 1;
410 }
411 return 0;
412}
413
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000414#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000416{
417 if (!*nr)
418 return 0;
419 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000421 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000423 else
424 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 print_current_name();
426 printf("'.");
427 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000428 *nr = 0;
429 *corrected = 1;
430 }
431 return 0;
432}
433#endif
434
435/*
436 * read-block reads block nr into the buffer at addr.
437 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000438static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000439{
440 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000441 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000442 return;
443 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000445 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000446 print_current_name();
447 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 errors_uncorrected = 1;
450 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000451 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000452 print_current_name();
453 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000455 errors_uncorrected = 1;
456 }
457}
458
459/*
460 * write_block writes block nr to disk.
461 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000463{
464 if (!nr)
465 return;
466 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000467 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000469 errors_uncorrected = 1;
470 return;
471 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000473 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000475 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000476 print_current_name();
477 printf("'\n");
478 errors_uncorrected = 1;
479 }
480}
481
482/*
483 * map-block calculates the absolute block nr of a block in a file.
484 * 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{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 unsigned short ind[BLOCK_SIZE >> 1];
490 unsigned short 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{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000525 unsigned int dind[BLOCK_SIZE >> 2];
526 unsigned int tind[BLOCK_SIZE >> 2];
527 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))
Eric Andersene674eb71999-10-19 20:52:57 +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))
Eric Andersene674eb71999-10-19 20:52:57 +0000596 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000597
598 return;
599}
600
Eric Andersene674eb71999-10-19 20:52:57 +0000601static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000602{
603 write_super_block();
604
Erik Andersene49d5ec2000-02-08 19:58:47 +0000605 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000606 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000608 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000610 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000611}
612
Erik Andersene49d5ec2000-02-08 19:58:47 +0000613static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000614{
615 int block;
616 char blk[BLOCK_SIZE];
617 int size;
618
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000619#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 if (version2)
621 block = Inode2[ROOT_INO].i_zone[0];
622 else
623#endif
624 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000626 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000628 dirsize = size;
629 namelen = size - 2;
630 return;
631 }
632 }
633 /* use defaults */
634}
635
Eric Andersene674eb71999-10-19 20:52:57 +0000636static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000637{
638 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000639 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000640 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000641 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000642 if (MAGIC == MINIX_SUPER_MAGIC) {
643 namelen = 14;
644 dirsize = 16;
645 version2 = 0;
646 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
647 namelen = 30;
648 dirsize = 32;
649 version2 = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000650#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
652 namelen = 14;
653 dirsize = 16;
654 version2 = 1;
655 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
656 namelen = 30;
657 dirsize = 32;
658 version2 = 1;
659#endif
660 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000661 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000662 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000663 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000664 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000665 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000666 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000667 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000668}
669
Eric Andersene674eb71999-10-19 20:52:57 +0000670static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000671{
Erik Andersen0d068a22000-03-21 22:32:57 +0000672 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
673 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Rob Landley9950cab2006-04-29 20:03:54 +0000674 memset(inode_map, 0, IMAPS * BLOCK_SIZE);
675 memset(zone_map, 0, ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000676 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
677 inode_count = xmalloc(INODES + 1);
678 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000680 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000681 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000682 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000683 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000684 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000685 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000686 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000687 errors_uncorrected = 1;
688 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000690 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 printf("%ld inodes\n", INODES);
692 printf("%ld blocks\n", ZONES);
693 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
694 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
695 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000696 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000698 }
699}
700
Eric Andersen3e6ff902001-03-09 21:24:12 +0000701static struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000702{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000703 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000704
705 if (!nr || nr > INODES)
706 return NULL;
707 total++;
708 inode = Inode + nr;
709 if (!inode_count[nr]) {
710 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000712 print_current_name();
713 printf("'\n");
714 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000716 mark_inode(nr);
717 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 }
720 }
721 if (S_ISDIR(inode->i_mode))
722 directory++;
723 else if (S_ISREG(inode->i_mode))
724 regular++;
725 else if (S_ISCHR(inode->i_mode))
726 chardev++;
727 else if (S_ISBLK(inode->i_mode))
728 blockdev++;
729 else if (S_ISLNK(inode->i_mode))
730 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731 else if (S_ISSOCK(inode->i_mode));
732 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000733 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000734 print_current_name();
735 printf(" has mode %05o\n", inode->i_mode);
736 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000737
738 } else
739 links++;
740 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +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)) {
759 printf("Inode %d marked not used, but used for file '", nr);
760 print_current_name();
761 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000762 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000763 if (ask("Mark in use", 1))
764 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000765 else
766 errors_uncorrected = 1;
767 }
768 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000770 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000774 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000775 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000776 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000778 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 else if (S_ISSOCK(inode->i_mode));
780 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000781 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 print_current_name();
783 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000784 }
785 } else
786 links++;
787 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000789 inode_count[nr]--;
790 errors_uncorrected = 1;
791 }
792 return inode;
793}
794#endif
795
Eric Andersene674eb71999-10-19 20:52:57 +0000796static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000797{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000799
800 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000801 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000802}
803
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000804#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000806{
807 struct minix2_inode *inode = Inode2 + ROOT_INO;
808
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 if (!inode || !S_ISDIR(inode->i_mode))
810 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000811}
812#endif
813
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000815{
816 int result;
817 int block;
818
819 result = 0;
820 block = check_zone_nr(znr, corrected);
821 if (!block)
822 return 0;
823 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000824 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000825 print_current_name();
826 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000828 *znr = 0;
829 block = 0;
830 *corrected = 1;
831 }
832 }
833 if (!block)
834 return 0;
835 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000837 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000838 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000839 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000840 mark_zone(block);
841 }
842 if (!++zone_count[block])
843 zone_count[block]--;
844 return block;
845}
846
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000847#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000848static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000849{
850 int result;
851 int block;
852
853 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000855 if (!block)
856 return 0;
857 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000858 printf("Block has been used before. Now in file `");
859 print_current_name();
860 printf("'.");
861 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000862 *znr = 0;
863 block = 0;
864 *corrected = 1;
865 }
866 }
867 if (!block)
868 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 if (!zone_in_use(block)) {
870 printf("Block %d in file `", block);
871 print_current_name();
872 printf("' is marked not in use.");
873 if (ask("Correct", 1))
874 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000875 }
876 if (!++zone_count[block])
877 zone_count[block]--;
878 return block;
879}
880#endif
881
Erik Andersene49d5ec2000-02-08 19:58:47 +0000882static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000883{
884 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000885 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000886 int block;
887
888 block = add_zone(znr, corrected);
889 if (!block)
890 return;
891 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000893 add_zone(i + (unsigned short *) blk, &chg_blk);
894 if (chg_blk)
895 write_block(block, blk);
896}
897
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000898#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000900{
901 static char blk[BLOCK_SIZE];
902 int i, chg_blk = 0;
903 int block;
904
Erik Andersene49d5ec2000-02-08 19:58:47 +0000905 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000906 if (!block)
907 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000911 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000913}
914#endif
915
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000917{
918 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000920 int block;
921
922 block = add_zone(znr, corrected);
923 if (!block)
924 return;
925 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000927 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
928 if (blk_chg)
929 write_block(block, blk);
930}
931
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000932#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000934{
935 static char blk[BLOCK_SIZE];
936 int i, blk_chg = 0;
937 int block;
938
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++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000944 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000945 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000946 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000947}
948
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000950{
951 static char blk[BLOCK_SIZE];
952 int i, blk_chg = 0;
953 int block;
954
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000956 if (!block)
957 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000959 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000961 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000962 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000963}
964#endif
965
Eric Andersene674eb71999-10-19 20:52:57 +0000966static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000967{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000969
970 if (!i || i > INODES)
971 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000973 return;
974 inode = Inode + i;
975 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 !S_ISLNK(inode->i_mode)) return;
977 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000978 add_zone(i + inode->i_zone, &changed);
979 add_zone_ind(7 + inode->i_zone, &changed);
980 add_zone_dind(8 + inode->i_zone, &changed);
981}
982
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000983#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000985{
986 struct minix2_inode *inode;
987
988 if (!i || i > INODES)
989 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000991 return;
992 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
994 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000995 return;
996 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000997 add_zone2(i + inode->i_zone, &changed);
998 add_zone_ind2(7 + inode->i_zone, &changed);
999 add_zone_dind2(8 + inode->i_zone, &changed);
1000 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001001}
1002#endif
1003
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001005{
1006 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001008 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001009 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001010 int block;
1011
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001013 read_block(block, blk);
1014 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001016 if (ino > INODES) {
1017 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001018 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001019 printf("%.*s'.", namelen, name);
1020 if (ask(" Remove", 1)) {
1021 *(unsigned short *) (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 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001026 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001028 name_depth++;
1029 inode = get_inode(ino);
1030 name_depth--;
1031 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001033 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001034 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001035 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 } else
1037 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001038 }
1039 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001041 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001042 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001043 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001044 } else
1045 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001046 }
1047 if (!inode)
1048 return;
1049 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 strncpy(name_list[name_depth], name, namelen);
1051 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 if (list) {
1053 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +00001055 print_current_name();
1056 if (S_ISDIR(inode->i_mode))
1057 printf(":\n");
1058 else
1059 printf("\n");
1060 }
1061 check_zones(ino);
1062 if (inode && S_ISDIR(inode->i_mode))
1063 recursive_check(ino);
1064 name_depth--;
1065 return;
1066}
1067
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001068#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001069static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001070{
1071 static char blk[BLOCK_SIZE];
1072 struct minix2_inode *inode;
1073 int ino;
1074 char *name;
1075 int block;
1076
Erik Andersene49d5ec2000-02-08 19:58:47 +00001077 block = map_block2(dir, offset / BLOCK_SIZE);
1078 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001079 name = blk + (offset % BLOCK_SIZE) + 2;
1080 ino = *(unsigned short *) (name - 2);
1081 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 print_current_name();
1083 printf(" contains a bad inode number for file '");
1084 printf("%.*s'.", namelen, name);
1085 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001086 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001087 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001088 }
1089 ino = 0;
1090 }
1091 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001093 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001095 name_depth--;
1096 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097 if (!inode || strcmp(".", name)) {
1098 print_current_name();
1099 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001100 errors_uncorrected = 1;
1101 } else
1102 return;
1103 }
1104 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105 if (!inode || strcmp("..", name)) {
1106 print_current_name();
1107 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001108 errors_uncorrected = 1;
1109 } else
1110 return;
1111 }
1112 if (!inode)
1113 return;
1114 name_depth++;
1115 if (list) {
1116 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1118 print_current_name();
1119 if (S_ISDIR(inode->i_mode))
1120 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001121 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001124 check_zones2(ino);
1125 if (inode && S_ISDIR(inode->i_mode))
1126 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001127 name_depth--;
1128 return;
1129}
1130#endif
1131
Eric Andersene674eb71999-10-19 20:52:57 +00001132static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001133{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001135 unsigned int offset;
1136
1137 dir = Inode + ino;
1138 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001139 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001140 if (dir->i_size < 2 * dirsize) {
1141 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001142 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001143 errors_uncorrected = 1;
1144 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 for (offset = 0; offset < dir->i_size; offset += dirsize)
1146 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001147}
1148
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001149#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001150static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001151{
1152 struct minix2_inode *dir;
1153 unsigned int offset;
1154
1155 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 if (!S_ISDIR(dir->i_mode))
1157 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001158 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001159 print_current_name();
1160 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001161 errors_uncorrected = 1;
1162 }
1163 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001164 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001165}
1166#endif
1167
Eric Andersene674eb71999-10-19 20:52:57 +00001168static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001169{
1170 char buffer[1024];
1171
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001173 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001174 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1175}
1176
Eric Andersene674eb71999-10-19 20:52:57 +00001177static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001178{
1179 int i;
1180
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001182 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 printf("Inode %d mode not cleared.", i);
1184 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001185 Inode[i].i_mode = 0;
1186 changed = 1;
1187 }
1188 }
1189 if (!inode_count[i]) {
1190 if (!inode_in_use(i))
1191 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 printf("Inode %d not used, marked used in the bitmap.", i);
1193 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001194 unmark_inode(i);
1195 continue;
1196 }
1197 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001198 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001200 mark_inode(i);
1201 }
1202 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001203 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1205 if (ask("Set i_nlinks to count", 1)) {
1206 Inode[i].i_nlinks = inode_count[i];
1207 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001208 }
1209 }
1210 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001212 if (zone_in_use(i) == zone_count[i])
1213 continue;
1214 if (!zone_count[i]) {
1215 if (bad_zone(i))
1216 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 printf("Zone %d: marked in use, no file uses it.", i);
1218 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001219 unmark_zone(i);
1220 continue;
1221 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 printf("Zone %d: %sin use, counted=%d\n",
1223 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001224 }
1225}
1226
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001227#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001228static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001229{
1230 int i;
1231
1232 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001233 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1234 printf("Inode %d mode not cleared.", i);
1235 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001236 Inode2[i].i_mode = 0;
1237 changed = 1;
1238 }
1239 }
1240 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001241 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001242 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 printf("Inode %d not used, marked used in the bitmap.", i);
1244 if (ask("Clear", 1))
1245 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001246 continue;
1247 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001248 if (!inode_in_use(i)) {
1249 printf("Inode %d used, marked unused in the bitmap.", i);
1250 if (ask("Set", 1))
1251 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001252 }
1253 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001254 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1255 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1256 inode_count[i]);
1257 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001258 Inode2[i].i_nlinks = inode_count[i];
1259 changed = 1;
1260 }
1261 }
1262 }
1263 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001265 continue;
1266 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001267 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001268 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001269 printf("Zone %d: marked in use, no file uses it.", i);
1270 if (ask("Unmark", 1))
1271 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001272 continue;
1273 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001274 printf("Zone %d: %sin use, counted=%d\n",
1275 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001276 }
1277}
1278#endif
1279
Eric Andersene674eb71999-10-19 20:52:57 +00001280static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001281{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001282 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1283 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001284 check_zones(ROOT_INO);
1285 recursive_check(ROOT_INO);
1286 check_counts();
1287}
1288
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001289#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001290static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001291{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001292 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1293 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1294 check_zones2(ROOT_INO);
1295 recursive_check2(ROOT_INO);
1296 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001297}
1298#endif
1299
John Beppuc1dc5d92000-02-11 12:43:20 +00001300/* Wed Feb 9 15:17:06 MST 2000 */
1301/* dynamically allocate name_list (instead of making it static) */
1302static void alloc_name_list(void)
1303{
1304 int i;
1305
Erik Andersen0d068a22000-03-21 22:32:57 +00001306 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1307 for (i = 0; i < MAX_DEPTH; i++)
Erik Andersen4f3f7572000-04-28 00:18:56 +00001308 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
John Beppuc1dc5d92000-02-11 12:43:20 +00001309}
1310
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001311#ifdef CONFIG_FEATURE_CLEAN_UP
John Beppu5e1b2ca2000-02-11 12:52:55 +00001312/* execute this atexit() to deallocate name_list[] */
1313/* piptigger was here */
John Beppuc1dc5d92000-02-11 12:43:20 +00001314static void free_name_list(void)
1315{
John Beppu5e1b2ca2000-02-11 12:52:55 +00001316 int i;
1317
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001318 if (name_list) {
John Beppu5e1b2ca2000-02-11 12:52:55 +00001319 for (i = 0; i < MAX_DEPTH; i++) {
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001320 free(name_list[i]);
John Beppu5e1b2ca2000-02-11 12:52:55 +00001321 }
1322 free(name_list);
1323 }
John Beppuc1dc5d92000-02-11 12:43:20 +00001324}
Erik Andersen298854f2000-03-23 01:09:18 +00001325#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001326
Rob Landleydfba7412006-03-06 20:47:33 +00001327int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001328{
1329 struct termios tmp;
1330 int count;
1331 int retcode = 0;
1332
John Beppuc1dc5d92000-02-11 12:43:20 +00001333 alloc_name_list();
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001334#ifdef CONFIG_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001335 /* Don't bother to free memory. Exit does
1336 * that automagically, so we can save a few bytes */
Eric Andersenb040d4f2000-07-25 18:01:20 +00001337 atexit(free_name_list);
1338#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001339
Eric Andersene494fdd1999-10-19 20:23:03 +00001340 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001341 die("bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001342#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001343 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001344 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001345#endif
1346 while (argc-- > 1) {
1347 argv++;
1348 if (argv[0][0] != '-') {
1349 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001350 bb_show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001351 else
1352 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001353 } else
1354 while (*++argv[0])
1355 switch (argv[0][0]) {
1356 case 'l':
1357 list = 1;
1358 break;
1359 case 'a':
1360 automatic = 1;
1361 repair = 1;
1362 break;
1363 case 'r':
1364 automatic = 0;
1365 repair = 1;
1366 break;
1367 case 'v':
1368 verbose = 1;
1369 break;
1370 case 's':
1371 show = 1;
1372 break;
1373 case 'm':
1374 warn_mode = 1;
1375 break;
1376 case 'f':
1377 force = 1;
1378 break;
1379 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001380 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001381 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001382 }
1383 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001384 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001385 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001386 if (repair && !automatic) {
1387 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001388 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001389 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001390 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersen77508b22000-12-12 23:51:43 +00001391 if (IN < 0){
1392 fprintf(stderr,"unable to open device '%s'.\n",device_name);
1393 leave(8);
1394 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001395 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001396 sync();
1397 read_superblock();
1398
1399 /*
1400 * Determine whether or not we should continue with the checking.
1401 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001402 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001403 * command line.
1404 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001405 printf("%s, %s\n", bb_applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406 if (!(Super.s_state & MINIX_ERROR_FS) &&
1407 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001408 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001409 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001410 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001411 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001412 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001413 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001414 printf("Filesystem on %s is dirty, needs checking.\n",
1415 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001416
1417 read_tables();
1418
1419 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001420 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001421 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001422 tmp.c_lflag &= ~(ICANON | ECHO);
1423 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001424 termios_set = 1;
1425 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001426#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001427 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001428 check_root2();
1429 check2();
1430 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001431#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001433 check_root();
1434 check();
1435 }
1436 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001437 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001438
Eric Andersen1ca20a72001-03-21 07:34:27 +00001439 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001440 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001441 free_cnt++;
1442 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1443 100 * (INODES - free_cnt) / INODES);
1444 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001445 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001446 free_cnt++;
1447 printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
1448 100 * (ZONES - free_cnt) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001449 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450 "%6d directories\n"
1451 "%6d character device files\n"
1452 "%6d block device files\n"
1453 "%6d links\n"
1454 "%6d symbolic links\n"
1455 "------\n"
1456 "%6d files\n",
1457 regular, directory, chardev, blockdev,
1458 links - 2 * directory + 1, symlinks,
1459 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001460 }
1461 if (changed) {
1462 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001463 printf("----------------------------\n"
1464 "FILE SYSTEM HAS BEEN CHANGED\n"
1465 "----------------------------\n");
1466 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001467 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001468 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001469 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001470
Eric Andersene494fdd1999-10-19 20:23:03 +00001471 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001472 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001473
1474 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001475 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001476 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001477 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001478 return retcode;
1479}