blob: 9da79748f5fb7a21d15895e14dc9bcd45da936e9 [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/*
10 * 09.11.91 - made the first rudimetary functions
11 *
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 *
52 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
53 * (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 >:-)
56 * This should make minix fsck systemindependent.
57 * (janl@math.uio.no, Nicolai Langfeldt)
58 *
59 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
60 * warnings. Added mc68k bitops from
61 * 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 *
76 * Usuage: fsck [-larvsm] device
77 * -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
86 * enforced (but it's not much fun on a character device :-).
87 */
88
Eric Andersene674eb71999-10-19 20:52:57 +000089#include "internal.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000090#include <stdio.h>
91#include <errno.h>
92#include <unistd.h>
93#include <string.h>
94#include <fcntl.h>
95#include <ctype.h>
96#include <stdlib.h>
97#include <termios.h>
98#include <mntent.h>
99#include <sys/stat.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100#include <sys/param.h> /* for PATH_MAX */
Eric Andersene494fdd1999-10-19 20:23:03 +0000101
102#include <linux/fs.h>
103#include <linux/minix_fs.h>
Eric Andersene494fdd1999-10-19 20:23:03 +0000104
105#ifdef MINIX2_SUPER_MAGIC2
106#define HAVE_MINIX2 1
107#endif
108
109#ifndef __linux__
110#define volatile
111#endif
112
113#define ROOT_INO 1
114
115#define UPPER(size,n) ((size+((n)-1))/(n))
116#define INODE_SIZE (sizeof(struct minix_inode))
117#ifdef HAVE_MINIX2
118#define INODE_SIZE2 (sizeof(struct minix2_inode))
119#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
120 : MINIX_INODES_PER_BLOCK))
121#else
122#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
123#endif
124#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
125
126#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128static char *program_name = "fsck.minix";
129static char *program_version = "1.2 - 11/11/96";
130static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000131static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
133 0, warn_mode = 0, force = 0;
134static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
135 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000136
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137static int changed = 0; /* flags if the filesystem has been changed */
138static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000139static int dirsize = 16;
140static int namelen = 14;
141static int version2 = 0;
142static struct termios termios;
143static int termios_set = 0;
144
145/* File-name data */
John Beppuc1dc5d92000-02-11 12:43:20 +0000146#define MAX_DEPTH 32
Eric Andersene494fdd1999-10-19 20:23:03 +0000147static int name_depth = 0;
John Beppuc1dc5d92000-02-11 12:43:20 +0000148// static char name_list[MAX_DEPTH][PATH_MAX + 1];
149static char **name_list;
Eric Andersene494fdd1999-10-19 20:23:03 +0000150
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151static char *inode_buffer = NULL;
152
Eric Andersene494fdd1999-10-19 20:23:03 +0000153#define Inode (((struct minix_inode *) inode_buffer)-1)
154#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
155static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156
Eric Andersene494fdd1999-10-19 20:23:03 +0000157#define Super (*(struct minix_super_block *)super_block_buffer)
158#define INODES ((unsigned long)Super.s_ninodes)
159#ifdef HAVE_MINIX2
160#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
161#else
162#define ZONES ((unsigned long)(Super.s_nzones))
163#endif
164#define IMAPS ((unsigned long)Super.s_imap_blocks)
165#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
166#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
167#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
168#define MAXSIZE ((unsigned long)Super.s_max_size)
169#define MAGIC (Super.s_magic)
170#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
171
172static char *inode_map;
173static char *zone_map;
174
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175static unsigned char *inode_count = NULL;
176static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000177
Eric Andersene674eb71999-10-19 20:52:57 +0000178static void recursive_check(unsigned int ino);
179static void recursive_check2(unsigned int ino);
Eric Andersene494fdd1999-10-19 20:23:03 +0000180
181#define inode_in_use(x) (bit(inode_map,(x)))
182#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
183
184#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
185#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
186
187#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
188#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
189
Eric Andersene674eb71999-10-19 20:52:57 +0000190static void leave(int) __attribute__ ((noreturn));
191static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000192{
193 if (termios_set)
194 tcsetattr(0, TCSANOW, &termios);
195 exit(status);
196}
197
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198static void show_usage(void)
199{
200 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
201 BB_VER, BB_BT);
Eric Andersend73dc5b1999-11-10 23:13:02 +0000202 fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n\n", program_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 fprintf(stderr,
204 "Performs a consistency check for MINIX filesystems.\n\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000205 fprintf(stderr, "OPTIONS:\n");
206 fprintf(stderr, "\t-l\tLists all filenames\n");
207 fprintf(stderr, "\t-r\tPerform interactive repairs\n");
208 fprintf(stderr, "\t-a\tPerform automatic repairs\n");
209 fprintf(stderr, "\t-v\tverbose\n");
210 fprintf(stderr, "\t-s\tOutputs super-block information\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 fprintf(stderr,
212 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000213 fprintf(stderr, "\t-f\tForce file system check.\n\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000214 leave(16);
215}
216
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217static void die(const char *str)
218{
Eric Andersene494fdd1999-10-19 20:23:03 +0000219 fprintf(stderr, "%s: %s\n", program_name, str);
220 leave(8);
221}
222
223/*
224 * This simply goes through the file-name data and prints out the
225 * current file.
226 */
Eric Andersene674eb71999-10-19 20:52:57 +0000227static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000228{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000230
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 while (i < name_depth)
232 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000233 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000235}
236
Erik Andersene49d5ec2000-02-08 19:58:47 +0000237static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000238{
239 int c;
240
241 if (!repair) {
242 printf("\n");
243 errors_uncorrected = 1;
244 return 0;
245 }
246 if (automatic) {
247 printf("\n");
248 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000250 return def;
251 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000253 for (;;) {
254 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 if ((c = getchar()) == EOF) {
256 if (!def)
257 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000258 return def;
259 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000261 if (c == 'Y') {
262 def = 1;
263 break;
264 } else if (c == 'N') {
265 def = 0;
266 break;
267 } else if (c == ' ' || c == '\n')
268 break;
269 }
270 if (def)
271 printf("y\n");
272 else {
273 printf("n\n");
274 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000276 return def;
277}
278
279/*
280 * Make certain that we aren't checking a filesystem that is on a
281 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
282 * 1994 Theodore Ts'o. Also licensed under GPL.
283 */
284static void check_mount(void)
285{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 FILE *f;
287 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000288 int cont;
289 int fd;
290
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000292 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 while ((mnt = getmntent(f)) != NULL)
294 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000295 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000296 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000297 if (!mnt)
298 return;
299
300 /*
301 * If the root is mounted read-only, then /etc/mtab is
302 * probably not correct; so we won't issue a warning based on
303 * it.
304 */
305 fd = open(MOUNTED, O_RDWR);
306 if (fd < 0 && errno == EROFS)
307 return;
308 else
309 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310
311 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000312 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000313 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 else
315 cont = 0;
316 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 printf("check aborted.\n");
318 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000319 }
320 return;
321}
322
323/*
324 * check_zone_nr checks to see that *nr is a valid zone nr. If it
325 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
326 * if an error was corrected, and returns the zone (0 for no zone
327 * or a bad zone-number).
328 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000330{
331 if (!*nr)
332 return 0;
333 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000334 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000335 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000336 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000337 else
338 return *nr;
339 print_current_name();
340 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 *nr = 0;
343 *corrected = 1;
344 }
345 return 0;
346}
347
348#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000350{
351 if (!*nr)
352 return 0;
353 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000355 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000357 else
358 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 print_current_name();
360 printf("'.");
361 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000362 *nr = 0;
363 *corrected = 1;
364 }
365 return 0;
366}
367#endif
368
369/*
370 * read-block reads block nr into the buffer at addr.
371 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000373{
374 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000376 return;
377 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000379 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000380 print_current_name();
381 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 errors_uncorrected = 1;
384 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000385 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 print_current_name();
387 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000388 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 errors_uncorrected = 1;
390 }
391}
392
393/*
394 * write_block writes block nr to disk.
395 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000397{
398 if (!nr)
399 return;
400 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000401 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000402 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000403 errors_uncorrected = 1;
404 return;
405 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000407 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000409 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000410 print_current_name();
411 printf("'\n");
412 errors_uncorrected = 1;
413 }
414}
415
416/*
417 * map-block calculates the absolute block nr of a block in a file.
418 * It sets 'changed' if the inode has needed changing, and re-writes
419 * any indirect blocks with errors.
420 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000422{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 unsigned short ind[BLOCK_SIZE >> 1];
424 unsigned short dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000425 int blk_chg, block, result;
426
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000428 return check_zone_nr(inode->i_zone + blknr, &changed);
429 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000431 block = check_zone_nr(inode->i_zone + 7, &changed);
432 read_block(block, (char *) ind);
433 blk_chg = 0;
434 result = check_zone_nr(blknr + ind, &blk_chg);
435 if (blk_chg)
436 write_block(block, (char *) ind);
437 return result;
438 }
439 blknr -= 512;
440 block = check_zone_nr(inode->i_zone + 8, &changed);
441 read_block(block, (char *) dind);
442 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000444 if (blk_chg)
445 write_block(block, (char *) dind);
446 block = result;
447 read_block(block, (char *) ind);
448 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000450 if (blk_chg)
451 write_block(block, (char *) ind);
452 return result;
453}
454
455#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000457{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000459 unsigned int dind[BLOCK_SIZE >> 2];
460 unsigned int tind[BLOCK_SIZE >> 2];
461 int blk_chg, block, result;
462
463 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000465 blknr -= 7;
466 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 block = check_zone_nr2(inode->i_zone + 7, &changed);
468 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000469 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000470 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000471 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000473 return result;
474 }
475 blknr -= 256;
476 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477 block = check_zone_nr2(inode->i_zone + 8, &changed);
478 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000481 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000483 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000485 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000487 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000489 return result;
490 }
491 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000492 block = check_zone_nr2(inode->i_zone + 9, &changed);
493 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000494 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000498 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000500 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000504 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000506 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000508 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000509 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000510 return result;
511}
512#endif
513
Eric Andersene674eb71999-10-19 20:52:57 +0000514static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000515{
516 /*
517 * Set the state of the filesystem based on whether or not there
518 * are uncorrected errors. The filesystem valid flag is
519 * unconditionally set if we get this far.
520 */
521 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000522 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000523 Super.s_state |= MINIX_ERROR_FS;
524 else
525 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526
Eric Andersene494fdd1999-10-19 20:23:03 +0000527 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000528 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000529 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000530 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000531
532 return;
533}
534
Eric Andersene674eb71999-10-19 20:52:57 +0000535static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000536{
537 write_super_block();
538
Erik Andersene49d5ec2000-02-08 19:58:47 +0000539 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000540 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000542 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000544 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000545}
546
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000548{
549 int block;
550 char blk[BLOCK_SIZE];
551 int size;
552
553#if HAVE_MINIX2
554 if (version2)
555 block = Inode2[ROOT_INO].i_zone[0];
556 else
557#endif
558 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 dirsize = size;
563 namelen = size - 2;
564 return;
565 }
566 }
567 /* use defaults */
568}
569
Eric Andersene674eb71999-10-19 20:52:57 +0000570static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000571{
572 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000573 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000575 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000576 if (MAGIC == MINIX_SUPER_MAGIC) {
577 namelen = 14;
578 dirsize = 16;
579 version2 = 0;
580 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
581 namelen = 30;
582 dirsize = 32;
583 version2 = 0;
584#ifdef HAVE_MINIX2
585 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
586 namelen = 14;
587 dirsize = 16;
588 version2 = 1;
589 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
590 namelen = 30;
591 dirsize = 32;
592 version2 = 1;
593#endif
594 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000595 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000597 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000598 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000599 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000601 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000602}
603
Eric Andersene674eb71999-10-19 20:52:57 +0000604static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000605{
606 inode_map = malloc(IMAPS * BLOCK_SIZE);
607 if (!inode_map)
Eric Andersene674eb71999-10-19 20:52:57 +0000608 die("Unable to allocate buffer for inode map");
Eric Andersene494fdd1999-10-19 20:23:03 +0000609 zone_map = malloc(ZMAPS * BLOCK_SIZE);
610 if (!inode_map)
611 die("Unable to allocate buffer for zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000612 memset(inode_map, 0, sizeof(inode_map));
613 memset(zone_map, 0, sizeof(zone_map));
Eric Andersene494fdd1999-10-19 20:23:03 +0000614 inode_buffer = malloc(INODE_BUFFER_SIZE);
615 if (!inode_buffer)
Eric Andersene674eb71999-10-19 20:52:57 +0000616 die("Unable to allocate buffer for inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000617 inode_count = malloc(INODES + 1);
618 if (!inode_count)
Eric Andersene674eb71999-10-19 20:52:57 +0000619 die("Unable to allocate buffer for inode count");
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 zone_count = malloc(ZONES);
621 if (!zone_count)
Eric Andersene674eb71999-10-19 20:52:57 +0000622 die("Unable to allocate buffer for zone count");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000623 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000624 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000625 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000626 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000627 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000628 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000629 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000630 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000631 errors_uncorrected = 1;
632 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000634 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 printf("%ld inodes\n", INODES);
636 printf("%ld blocks\n", ZONES);
637 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
638 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
639 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000640 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000642 }
643}
644
Erik Andersene49d5ec2000-02-08 19:58:47 +0000645struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000646{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000647 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000648
649 if (!nr || nr > INODES)
650 return NULL;
651 total++;
652 inode = Inode + nr;
653 if (!inode_count[nr]) {
654 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000656 print_current_name();
657 printf("'\n");
658 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000659 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000660 mark_inode(nr);
661 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000662 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000663 }
664 }
665 if (S_ISDIR(inode->i_mode))
666 directory++;
667 else if (S_ISREG(inode->i_mode))
668 regular++;
669 else if (S_ISCHR(inode->i_mode))
670 chardev++;
671 else if (S_ISBLK(inode->i_mode))
672 blockdev++;
673 else if (S_ISLNK(inode->i_mode))
674 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675 else if (S_ISSOCK(inode->i_mode));
676 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000677 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 print_current_name();
679 printf(" has mode %05o\n", inode->i_mode);
680 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000681
682 } else
683 links++;
684 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000685 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000686 inode_count[nr]--;
687 errors_uncorrected = 1;
688 }
689 return inode;
690}
691
692#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000694{
695 struct minix2_inode *inode;
696
697 if (!nr || nr > INODES)
698 return NULL;
699 total++;
700 inode = Inode2 + nr;
701 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 if (!inode_in_use(nr)) {
703 printf("Inode %d marked not used, but used for file '", nr);
704 print_current_name();
705 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000706 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 if (ask("Mark in use", 1))
708 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000709 else
710 errors_uncorrected = 1;
711 }
712 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000714 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000716 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000718 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000720 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000721 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000722 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 else if (S_ISSOCK(inode->i_mode));
724 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000725 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 print_current_name();
727 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000728 }
729 } else
730 links++;
731 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000732 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000733 inode_count[nr]--;
734 errors_uncorrected = 1;
735 }
736 return inode;
737}
738#endif
739
Eric Andersene674eb71999-10-19 20:52:57 +0000740static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000741{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000743
744 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000745 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000746}
747
748#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000750{
751 struct minix2_inode *inode = Inode2 + ROOT_INO;
752
Erik Andersene49d5ec2000-02-08 19:58:47 +0000753 if (!inode || !S_ISDIR(inode->i_mode))
754 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000755}
756#endif
757
Erik Andersene49d5ec2000-02-08 19:58:47 +0000758static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000759{
760 int result;
761 int block;
762
763 result = 0;
764 block = check_zone_nr(znr, corrected);
765 if (!block)
766 return 0;
767 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000768 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000769 print_current_name();
770 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 *znr = 0;
773 block = 0;
774 *corrected = 1;
775 }
776 }
777 if (!block)
778 return 0;
779 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000781 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000782 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000784 mark_zone(block);
785 }
786 if (!++zone_count[block])
787 zone_count[block]--;
788 return block;
789}
790
791#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000793{
794 int result;
795 int block;
796
797 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000799 if (!block)
800 return 0;
801 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000802 printf("Block has been used before. Now in file `");
803 print_current_name();
804 printf("'.");
805 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000806 *znr = 0;
807 block = 0;
808 *corrected = 1;
809 }
810 }
811 if (!block)
812 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000813 if (!zone_in_use(block)) {
814 printf("Block %d in file `", block);
815 print_current_name();
816 printf("' is marked not in use.");
817 if (ask("Correct", 1))
818 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000819 }
820 if (!++zone_count[block])
821 zone_count[block]--;
822 return block;
823}
824#endif
825
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000827{
828 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000829 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000830 int block;
831
832 block = add_zone(znr, corrected);
833 if (!block)
834 return;
835 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000836 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000837 add_zone(i + (unsigned short *) blk, &chg_blk);
838 if (chg_blk)
839 write_block(block, blk);
840}
841
842#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000844{
845 static char blk[BLOCK_SIZE];
846 int i, chg_blk = 0;
847 int block;
848
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000850 if (!block)
851 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000852 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000853 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000855 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000856 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000857}
858#endif
859
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000861{
862 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000863 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000864 int block;
865
866 block = add_zone(znr, corrected);
867 if (!block)
868 return;
869 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000870 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000871 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
872 if (blk_chg)
873 write_block(block, blk);
874}
875
876#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000878{
879 static char blk[BLOCK_SIZE];
880 int i, blk_chg = 0;
881 int block;
882
Erik Andersene49d5ec2000-02-08 19:58:47 +0000883 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000884 if (!block)
885 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000886 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000887 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000889 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000890 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000891}
892
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000894{
895 static char blk[BLOCK_SIZE];
896 int i, blk_chg = 0;
897 int block;
898
Erik Andersene49d5ec2000-02-08 19:58:47 +0000899 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000900 if (!block)
901 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000902 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000903 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000904 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000905 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000907}
908#endif
909
Eric Andersene674eb71999-10-19 20:52:57 +0000910static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000911{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000912 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000913
914 if (!i || i > INODES)
915 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000917 return;
918 inode = Inode + i;
919 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 !S_ISLNK(inode->i_mode)) return;
921 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000922 add_zone(i + inode->i_zone, &changed);
923 add_zone_ind(7 + inode->i_zone, &changed);
924 add_zone_dind(8 + inode->i_zone, &changed);
925}
926
927#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000929{
930 struct minix2_inode *inode;
931
932 if (!i || i > INODES)
933 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000934 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000935 return;
936 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
938 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000939 return;
940 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000941 add_zone2(i + inode->i_zone, &changed);
942 add_zone_ind2(7 + inode->i_zone, &changed);
943 add_zone_dind2(8 + inode->i_zone, &changed);
944 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000945}
946#endif
947
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000949{
950 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000952 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000954 int block;
955
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000957 read_block(block, blk);
958 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000960 if (ino > INODES) {
961 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000962 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 printf("%.*s'.", namelen, name);
964 if (ask(" Remove", 1)) {
965 *(unsigned short *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000966 write_block(block, blk);
967 }
968 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000970 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000972 name_depth++;
973 inode = get_inode(ino);
974 name_depth--;
975 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000976 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000977 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000978 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000979 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000980 } else
981 return;
Eric Andersene494fdd1999-10-19 20:23:03 +0000982 }
983 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000985 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000986 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000987 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 } else
989 return;
Eric Andersene494fdd1999-10-19 20:23:03 +0000990 }
991 if (!inode)
992 return;
993 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994 strncpy(name_list[name_depth], name, namelen);
995 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000996 if (list) {
997 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000999 print_current_name();
1000 if (S_ISDIR(inode->i_mode))
1001 printf(":\n");
1002 else
1003 printf("\n");
1004 }
1005 check_zones(ino);
1006 if (inode && S_ISDIR(inode->i_mode))
1007 recursive_check(ino);
1008 name_depth--;
1009 return;
1010}
1011
1012#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001013static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001014{
1015 static char blk[BLOCK_SIZE];
1016 struct minix2_inode *inode;
1017 int ino;
1018 char *name;
1019 int block;
1020
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 block = map_block2(dir, offset / BLOCK_SIZE);
1022 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 name = blk + (offset % BLOCK_SIZE) + 2;
1024 ino = *(unsigned short *) (name - 2);
1025 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026 print_current_name();
1027 printf(" contains a bad inode number for file '");
1028 printf("%.*s'.", namelen, name);
1029 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001030 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001031 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001032 }
1033 ino = 0;
1034 }
1035 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001037 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001039 name_depth--;
1040 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001041 if (!inode || strcmp(".", name)) {
1042 print_current_name();
1043 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001044 errors_uncorrected = 1;
1045 } else
1046 return;
1047 }
1048 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001049 if (!inode || strcmp("..", name)) {
1050 print_current_name();
1051 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 errors_uncorrected = 1;
1053 } else
1054 return;
1055 }
1056 if (!inode)
1057 return;
1058 name_depth++;
1059 if (list) {
1060 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1062 print_current_name();
1063 if (S_ISDIR(inode->i_mode))
1064 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001065 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001066 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001067 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 check_zones2(ino);
1069 if (inode && S_ISDIR(inode->i_mode))
1070 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001071 name_depth--;
1072 return;
1073}
1074#endif
1075
Eric Andersene674eb71999-10-19 20:52:57 +00001076static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001077{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001079 unsigned int offset;
1080
1081 dir = Inode + ino;
1082 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001083 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001084 if (dir->i_size < 2 * dirsize) {
1085 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001086 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001087 errors_uncorrected = 1;
1088 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001089 for (offset = 0; offset < dir->i_size; offset += dirsize)
1090 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001091}
1092
1093#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001095{
1096 struct minix2_inode *dir;
1097 unsigned int offset;
1098
1099 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001100 if (!S_ISDIR(dir->i_mode))
1101 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001102 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001103 print_current_name();
1104 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001105 errors_uncorrected = 1;
1106 }
1107 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001109}
1110#endif
1111
Eric Andersene674eb71999-10-19 20:52:57 +00001112static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001113{
1114 char buffer[1024];
1115
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001117 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001118 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1119}
1120
Eric Andersene674eb71999-10-19 20:52:57 +00001121static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001122{
1123 int i;
1124
Erik Andersene49d5ec2000-02-08 19:58:47 +00001125 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001126 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 printf("Inode %d mode not cleared.", i);
1128 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001129 Inode[i].i_mode = 0;
1130 changed = 1;
1131 }
1132 }
1133 if (!inode_count[i]) {
1134 if (!inode_in_use(i))
1135 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001136 printf("Inode %d not used, marked used in the bitmap.", i);
1137 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001138 unmark_inode(i);
1139 continue;
1140 }
1141 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001142 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001143 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001144 mark_inode(i);
1145 }
1146 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001147 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001148 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1149 if (ask("Set i_nlinks to count", 1)) {
1150 Inode[i].i_nlinks = inode_count[i];
1151 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001152 }
1153 }
1154 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001155 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001156 if (zone_in_use(i) == zone_count[i])
1157 continue;
1158 if (!zone_count[i]) {
1159 if (bad_zone(i))
1160 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161 printf("Zone %d: marked in use, no file uses it.", i);
1162 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001163 unmark_zone(i);
1164 continue;
1165 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 printf("Zone %d: %sin use, counted=%d\n",
1167 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001168 }
1169}
1170
1171#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001173{
1174 int i;
1175
1176 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001177 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1178 printf("Inode %d mode not cleared.", i);
1179 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001180 Inode2[i].i_mode = 0;
1181 changed = 1;
1182 }
1183 }
1184 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001186 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187 printf("Inode %d not used, marked used in the bitmap.", i);
1188 if (ask("Clear", 1))
1189 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001190 continue;
1191 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 if (!inode_in_use(i)) {
1193 printf("Inode %d used, marked unused in the bitmap.", i);
1194 if (ask("Set", 1))
1195 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001196 }
1197 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1199 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1200 inode_count[i]);
1201 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001202 Inode2[i].i_nlinks = inode_count[i];
1203 changed = 1;
1204 }
1205 }
1206 }
1207 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001208 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001209 continue;
1210 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001212 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001213 printf("Zone %d: marked in use, no file uses it.", i);
1214 if (ask("Unmark", 1))
1215 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001216 continue;
1217 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001218 printf("Zone %d: %sin use, counted=%d\n",
1219 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001220 }
1221}
1222#endif
1223
Eric Andersene674eb71999-10-19 20:52:57 +00001224static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001225{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001226 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1227 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001228 check_zones(ROOT_INO);
1229 recursive_check(ROOT_INO);
1230 check_counts();
1231}
1232
1233#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001234static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001235{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1237 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1238 check_zones2(ROOT_INO);
1239 recursive_check2(ROOT_INO);
1240 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001241}
1242#endif
1243
John Beppuc1dc5d92000-02-11 12:43:20 +00001244/* Wed Feb 9 15:17:06 MST 2000 */
1245/* dynamically allocate name_list (instead of making it static) */
1246static void alloc_name_list(void)
1247{
1248 int i;
1249
1250 name_list = malloc(sizeof(char *) * MAX_DEPTH);
1251 for (i = 0; i < MAX_DEPTH; i++) {
1252 name_list[i] = malloc(sizeof(char) * PATH_MAX + 1);
1253 }
1254}
1255
1256static void free_name_list(void)
1257{
1258 if (name_list) free(name_list);
1259}
1260
Erik Andersene49d5ec2000-02-08 19:58:47 +00001261extern int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001262{
1263 struct termios tmp;
1264 int count;
1265 int retcode = 0;
1266
John Beppuc1dc5d92000-02-11 12:43:20 +00001267 alloc_name_list();
1268 atexit(free_name_list);
1269
Eric Andersene494fdd1999-10-19 20:23:03 +00001270 if (argc && *argv)
1271 program_name = *argv;
1272 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001273 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001274#ifdef HAVE_MINIX2
1275 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001276 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001277#endif
1278 while (argc-- > 1) {
1279 argv++;
1280 if (argv[0][0] != '-') {
1281 if (device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001282 show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001283 else
1284 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001285 } else
1286 while (*++argv[0])
1287 switch (argv[0][0]) {
1288 case 'l':
1289 list = 1;
1290 break;
1291 case 'a':
1292 automatic = 1;
1293 repair = 1;
1294 break;
1295 case 'r':
1296 automatic = 0;
1297 repair = 1;
1298 break;
1299 case 'v':
1300 verbose = 1;
1301 break;
1302 case 's':
1303 show = 1;
1304 break;
1305 case 'm':
1306 warn_mode = 1;
1307 break;
1308 case 'f':
1309 force = 1;
1310 break;
1311 default:
1312 show_usage();
1313 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001314 }
1315 if (!device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001316 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001317 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001318 if (repair && !automatic) {
1319 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001320 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001321 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001322 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersene494fdd1999-10-19 20:23:03 +00001323 if (IN < 0)
Eric Andersene674eb71999-10-19 20:52:57 +00001324 die("unable to open '%s'");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001325 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001326 sync();
1327 read_superblock();
1328
1329 /*
1330 * Determine whether or not we should continue with the checking.
1331 * This is based on the status of the filesystem valid and error
1332 * flags and whether or not the -f switch was specified on the
1333 * command line.
1334 */
Eric Andersene674eb71999-10-19 20:52:57 +00001335 printf("%s, %s\n", program_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001336 if (!(Super.s_state & MINIX_ERROR_FS) &&
1337 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001338 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001339 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001340 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001342 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001343 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001344 printf("Filesystem on %s is dirty, needs checking.\n",
1345 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001346
1347 read_tables();
1348
1349 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001350 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001351 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001352 tmp.c_lflag &= ~(ICANON | ECHO);
1353 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001354 termios_set = 1;
1355 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001356#if HAVE_MINIX2
1357 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001358 check_root2();
1359 check2();
1360 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001361#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001362 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001363 check_root();
1364 check();
1365 }
1366 if (verbose) {
1367 int i, free;
1368
Erik Andersene49d5ec2000-02-08 19:58:47 +00001369 for (i = 1, free = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001370 if (!inode_in_use(i))
1371 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001372 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1373 100 * (INODES - free) / INODES);
1374 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001375 if (!zone_in_use(i))
1376 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1378 100 * (ZONES - free) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001379 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001380 "%6d directories\n"
1381 "%6d character device files\n"
1382 "%6d block device files\n"
1383 "%6d links\n"
1384 "%6d symbolic links\n"
1385 "------\n"
1386 "%6d files\n",
1387 regular, directory, chardev, blockdev,
1388 links - 2 * directory + 1, symlinks,
1389 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001390 }
1391 if (changed) {
1392 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001393 printf("----------------------------\n"
1394 "FILE SYSTEM HAS BEEN CHANGED\n"
1395 "----------------------------\n");
1396 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001397 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001398 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001399 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001400
Eric Andersene494fdd1999-10-19 20:23:03 +00001401 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001402 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001403
1404 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001405 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001406 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001407 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001408 return retcode;
1409}