blob: f980fe9ea7c7a19f87fc62556d85b6a5edddde59 [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 Andersen8ea7d8c2000-05-20 00:40:08 +0000100#include <sys/param.h>
Eric Andersen85c55202000-07-10 22:46:55 +0000101
102
103 typedef unsigned char u8;
104typedef unsigned short u16;
105typedef unsigned int u32;
106
107
108#define MINIX_ROOT_INO 1
109#define MINIX_LINK_MAX 250
110#define MINIX2_LINK_MAX 65530
111
112#define MINIX_I_MAP_SLOTS 8
113#define MINIX_Z_MAP_SLOTS 64
114#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
115#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
116#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
117#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
118#define MINIX_VALID_FS 0x0001 /* Clean fs. */
119#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
120
121#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
122#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
123
124#define MINIX_V1 0x0001 /* original minix fs */
125#define MINIX_V2 0x0002 /* minix V2 fs */
126
127#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
128
129/*
130 * This is the original minix inode layout on disk.
131 * Note the 8-bit gid and atime and ctime.
132 */
133struct minix_inode {
134 u16 i_mode;
135 u16 i_uid;
136 u32 i_size;
137 u32 i_time;
138 u8 i_gid;
139 u8 i_nlinks;
140 u16 i_zone[9];
141};
142
143/*
144 * The new minix inode has all the time entries, as well as
145 * long block numbers and a third indirect block (7+1+1+1
146 * instead of 7+1+1). Also, some previously 8-bit values are
147 * now 16-bit. The inode is now 64 bytes instead of 32.
148 */
149struct minix2_inode {
150 u16 i_mode;
151 u16 i_nlinks;
152 u16 i_uid;
153 u16 i_gid;
154 u32 i_size;
155 u32 i_atime;
156 u32 i_mtime;
157 u32 i_ctime;
158 u32 i_zone[10];
159};
160
161/*
162 * minix super-block data on disk
163 */
164struct minix_super_block {
165 u16 s_ninodes;
166 u16 s_nzones;
167 u16 s_imap_blocks;
168 u16 s_zmap_blocks;
169 u16 s_firstdatazone;
170 u16 s_log_zone_size;
171 u32 s_max_size;
172 u16 s_magic;
173 u16 s_state;
174 u32 s_zones;
175};
176
177struct minix_dir_entry {
178 u16 inode;
179 char name[0];
180};
181
182#define BLOCK_SIZE_BITS 10
183#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
184
185#define NAME_MAX 255 /* # chars in a file name */
186
187#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
188
189#define MINIX_VALID_FS 0x0001 /* Clean fs. */
190#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
191
192#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
193#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
194
195#ifndef BLKGETSIZE
196#define BLKGETSIZE _IO(0x12,96) /* return device size */
197#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000198
Eric Andersene494fdd1999-10-19 20:23:03 +0000199#ifndef __linux__
200#define volatile
201#endif
202
203#define ROOT_INO 1
204
205#define UPPER(size,n) ((size+((n)-1))/(n))
206#define INODE_SIZE (sizeof(struct minix_inode))
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000207#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000208#define INODE_SIZE2 (sizeof(struct minix2_inode))
209#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
210 : MINIX_INODES_PER_BLOCK))
211#else
212#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
213#endif
214#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
215
216#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218static char *program_version = "1.2 - 11/11/96";
219static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000220static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
222 0, warn_mode = 0, force = 0;
223static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
224 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226static int changed = 0; /* flags if the filesystem has been changed */
227static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000228static int dirsize = 16;
229static int namelen = 14;
230static int version2 = 0;
231static struct termios termios;
232static int termios_set = 0;
233
234/* File-name data */
John Beppuc1dc5d92000-02-11 12:43:20 +0000235#define MAX_DEPTH 32
Eric Andersene494fdd1999-10-19 20:23:03 +0000236static int name_depth = 0;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000237// static char name_list[MAX_DEPTH][BUFSIZ + 1];
John Beppu5e1b2ca2000-02-11 12:52:55 +0000238static char **name_list = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000239
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240static char *inode_buffer = NULL;
241
Eric Andersene494fdd1999-10-19 20:23:03 +0000242#define Inode (((struct minix_inode *) inode_buffer)-1)
243#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
244static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245
Eric Andersene494fdd1999-10-19 20:23:03 +0000246#define Super (*(struct minix_super_block *)super_block_buffer)
247#define INODES ((unsigned long)Super.s_ninodes)
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000248#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000249#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
250#else
251#define ZONES ((unsigned long)(Super.s_nzones))
252#endif
253#define IMAPS ((unsigned long)Super.s_imap_blocks)
254#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
255#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
256#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
257#define MAXSIZE ((unsigned long)Super.s_max_size)
258#define MAGIC (Super.s_magic)
259#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
260
261static char *inode_map;
262static char *zone_map;
263
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264static unsigned char *inode_count = NULL;
265static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000266
Eric Andersene674eb71999-10-19 20:52:57 +0000267static void recursive_check(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000268#ifdef BB_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000269static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000270#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000271
Erik Andersen4f3f7572000-04-28 00:18:56 +0000272#define inode_in_use(x) (isset(inode_map,(x)))
273#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000274
275#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
276#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
277
278#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
279#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
280
Eric Andersene674eb71999-10-19 20:52:57 +0000281static void leave(int) __attribute__ ((noreturn));
282static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000283{
284 if (termios_set)
285 tcsetattr(0, TCSANOW, &termios);
286 exit(status);
287}
288
Erik Andersene49d5ec2000-02-08 19:58:47 +0000289static void show_usage(void)
290{
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000291 usage(fsck_minix_usage);
Eric Andersene494fdd1999-10-19 20:23:03 +0000292}
293
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294static void die(const char *str)
295{
Matt Kraaid537a952000-07-14 01:51:25 +0000296 errorMsg("%s\n", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000297 leave(8);
298}
299
300/*
301 * This simply goes through the file-name data and prints out the
302 * current file.
303 */
Eric Andersene674eb71999-10-19 20:52:57 +0000304static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000305{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000307
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 while (i < name_depth)
309 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000310 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000312}
313
Erik Andersene49d5ec2000-02-08 19:58:47 +0000314static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000315{
316 int c;
317
318 if (!repair) {
319 printf("\n");
320 errors_uncorrected = 1;
321 return 0;
322 }
323 if (automatic) {
324 printf("\n");
325 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000327 return def;
328 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000329 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000330 for (;;) {
331 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 if ((c = getchar()) == EOF) {
333 if (!def)
334 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000335 return def;
336 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000338 if (c == 'Y') {
339 def = 1;
340 break;
341 } else if (c == 'N') {
342 def = 0;
343 break;
344 } else if (c == ' ' || c == '\n')
345 break;
346 }
347 if (def)
348 printf("y\n");
349 else {
350 printf("n\n");
351 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000353 return def;
354}
355
356/*
357 * Make certain that we aren't checking a filesystem that is on a
358 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
359 * 1994 Theodore Ts'o. Also licensed under GPL.
360 */
361static void check_mount(void)
362{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 FILE *f;
364 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000365 int cont;
366 int fd;
367
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000369 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 while ((mnt = getmntent(f)) != NULL)
371 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000372 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000374 if (!mnt)
375 return;
376
377 /*
378 * If the root is mounted read-only, then /etc/mtab is
379 * probably not correct; so we won't issue a warning based on
380 * it.
381 */
382 fd = open(MOUNTED, O_RDWR);
383 if (fd < 0 && errno == EROFS)
384 return;
385 else
386 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387
388 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000390 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000391 else
392 cont = 0;
393 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000394 printf("check aborted.\n");
395 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000396 }
397 return;
398}
399
400/*
401 * check_zone_nr checks to see that *nr is a valid zone nr. If it
402 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
403 * if an error was corrected, and returns the zone (0 for no zone
404 * or a bad zone-number).
405 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000406static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000407{
408 if (!*nr)
409 return 0;
410 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000411 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000412 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000413 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000414 else
415 return *nr;
416 print_current_name();
417 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000418 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000419 *nr = 0;
420 *corrected = 1;
421 }
422 return 0;
423}
424
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000425#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000426static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000427{
428 if (!*nr)
429 return 0;
430 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000432 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000434 else
435 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000436 print_current_name();
437 printf("'.");
438 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000439 *nr = 0;
440 *corrected = 1;
441 }
442 return 0;
443}
444#endif
445
446/*
447 * read-block reads block nr into the buffer at addr.
448 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000450{
451 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000453 return;
454 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000456 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000457 print_current_name();
458 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000460 errors_uncorrected = 1;
461 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000462 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000463 print_current_name();
464 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000466 errors_uncorrected = 1;
467 }
468}
469
470/*
471 * write_block writes block nr to disk.
472 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000474{
475 if (!nr)
476 return;
477 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000478 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000479 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000480 errors_uncorrected = 1;
481 return;
482 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000484 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000485 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000486 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000487 print_current_name();
488 printf("'\n");
489 errors_uncorrected = 1;
490 }
491}
492
493/*
494 * map-block calculates the absolute block nr of a block in a file.
495 * It sets 'changed' if the inode has needed changing, and re-writes
496 * any indirect blocks with errors.
497 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000499{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 unsigned short ind[BLOCK_SIZE >> 1];
501 unsigned short dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 int blk_chg, block, result;
503
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 return check_zone_nr(inode->i_zone + blknr, &changed);
506 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000508 block = check_zone_nr(inode->i_zone + 7, &changed);
509 read_block(block, (char *) ind);
510 blk_chg = 0;
511 result = check_zone_nr(blknr + ind, &blk_chg);
512 if (blk_chg)
513 write_block(block, (char *) ind);
514 return result;
515 }
516 blknr -= 512;
517 block = check_zone_nr(inode->i_zone + 8, &changed);
518 read_block(block, (char *) dind);
519 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 if (blk_chg)
522 write_block(block, (char *) dind);
523 block = result;
524 read_block(block, (char *) ind);
525 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000527 if (blk_chg)
528 write_block(block, (char *) ind);
529 return result;
530}
531
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000532#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000534{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000536 unsigned int dind[BLOCK_SIZE >> 2];
537 unsigned int tind[BLOCK_SIZE >> 2];
538 int blk_chg, block, result;
539
540 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000542 blknr -= 7;
543 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 block = check_zone_nr2(inode->i_zone + 7, &changed);
545 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000546 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000550 return result;
551 }
552 blknr -= 256;
553 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 block = check_zone_nr2(inode->i_zone + 8, &changed);
555 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 return result;
567 }
568 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 block = check_zone_nr2(inode->i_zone + 9, &changed);
570 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000571 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000577 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000579 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000581 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000585 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 return result;
588}
589#endif
590
Eric Andersene674eb71999-10-19 20:52:57 +0000591static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000592{
593 /*
594 * Set the state of the filesystem based on whether or not there
595 * are uncorrected errors. The filesystem valid flag is
596 * unconditionally set if we get this far.
597 */
598 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 Super.s_state |= MINIX_ERROR_FS;
601 else
602 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603
Eric Andersene494fdd1999-10-19 20:23:03 +0000604 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000605 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000606 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000607 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000608
609 return;
610}
611
Eric Andersene674eb71999-10-19 20:52:57 +0000612static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000613{
614 write_super_block();
615
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000617 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000619 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000621 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000622}
623
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000625{
626 int block;
627 char blk[BLOCK_SIZE];
628 int size;
629
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000630#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000631 if (version2)
632 block = Inode2[ROOT_INO].i_zone[0];
633 else
634#endif
635 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000637 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000639 dirsize = size;
640 namelen = size - 2;
641 return;
642 }
643 }
644 /* use defaults */
645}
646
Eric Andersene674eb71999-10-19 20:52:57 +0000647static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000648{
649 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000650 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000652 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000653 if (MAGIC == MINIX_SUPER_MAGIC) {
654 namelen = 14;
655 dirsize = 16;
656 version2 = 0;
657 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
658 namelen = 30;
659 dirsize = 32;
660 version2 = 0;
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000661#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000662 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
663 namelen = 14;
664 dirsize = 16;
665 version2 = 1;
666 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
667 namelen = 30;
668 dirsize = 32;
669 version2 = 1;
670#endif
671 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000672 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000673 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000674 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000675 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000676 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000677 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000678 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000679}
680
Eric Andersene674eb71999-10-19 20:52:57 +0000681static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000682{
Erik Andersen0d068a22000-03-21 22:32:57 +0000683 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
684 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 memset(inode_map, 0, sizeof(inode_map));
686 memset(zone_map, 0, sizeof(zone_map));
Erik Andersen0d068a22000-03-21 22:32:57 +0000687 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
688 inode_count = xmalloc(INODES + 1);
689 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000690 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000691 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000692 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000693 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000694 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000695 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000696 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000697 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000698 errors_uncorrected = 1;
699 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 printf("%ld inodes\n", INODES);
703 printf("%ld blocks\n", ZONES);
704 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
705 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
706 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000707 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000709 }
710}
711
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000713{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000715
716 if (!nr || nr > INODES)
717 return NULL;
718 total++;
719 inode = Inode + nr;
720 if (!inode_count[nr]) {
721 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000723 print_current_name();
724 printf("'\n");
725 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000727 mark_inode(nr);
728 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000730 }
731 }
732 if (S_ISDIR(inode->i_mode))
733 directory++;
734 else if (S_ISREG(inode->i_mode))
735 regular++;
736 else if (S_ISCHR(inode->i_mode))
737 chardev++;
738 else if (S_ISBLK(inode->i_mode))
739 blockdev++;
740 else if (S_ISLNK(inode->i_mode))
741 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 else if (S_ISSOCK(inode->i_mode));
743 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000744 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000745 print_current_name();
746 printf(" has mode %05o\n", inode->i_mode);
747 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000748
749 } else
750 links++;
751 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000752 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000753 inode_count[nr]--;
754 errors_uncorrected = 1;
755 }
756 return inode;
757}
758
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000759#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000760struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000761{
762 struct minix2_inode *inode;
763
764 if (!nr || nr > INODES)
765 return NULL;
766 total++;
767 inode = Inode2 + nr;
768 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 if (!inode_in_use(nr)) {
770 printf("Inode %d marked not used, but used for file '", nr);
771 print_current_name();
772 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000773 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 if (ask("Mark in use", 1))
775 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000776 else
777 errors_uncorrected = 1;
778 }
779 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000780 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000781 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000783 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000784 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000785 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000786 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000787 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000788 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000789 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000790 else if (S_ISSOCK(inode->i_mode));
791 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000792 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793 print_current_name();
794 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000795 }
796 } else
797 links++;
798 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000800 inode_count[nr]--;
801 errors_uncorrected = 1;
802 }
803 return inode;
804}
805#endif
806
Eric Andersene674eb71999-10-19 20:52:57 +0000807static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000808{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000809 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000810
811 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000812 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000813}
814
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000815#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000817{
818 struct minix2_inode *inode = Inode2 + ROOT_INO;
819
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 if (!inode || !S_ISDIR(inode->i_mode))
821 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000822}
823#endif
824
Erik Andersene49d5ec2000-02-08 19:58:47 +0000825static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000826{
827 int result;
828 int block;
829
830 result = 0;
831 block = check_zone_nr(znr, corrected);
832 if (!block)
833 return 0;
834 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000835 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000836 print_current_name();
837 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000838 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000839 *znr = 0;
840 block = 0;
841 *corrected = 1;
842 }
843 }
844 if (!block)
845 return 0;
846 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000848 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000849 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000850 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000851 mark_zone(block);
852 }
853 if (!++zone_count[block])
854 zone_count[block]--;
855 return block;
856}
857
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000858#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000859static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000860{
861 int result;
862 int block;
863
864 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000865 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000866 if (!block)
867 return 0;
868 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000869 printf("Block has been used before. Now in file `");
870 print_current_name();
871 printf("'.");
872 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000873 *znr = 0;
874 block = 0;
875 *corrected = 1;
876 }
877 }
878 if (!block)
879 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 if (!zone_in_use(block)) {
881 printf("Block %d in file `", block);
882 print_current_name();
883 printf("' is marked not in use.");
884 if (ask("Correct", 1))
885 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000886 }
887 if (!++zone_count[block])
888 zone_count[block]--;
889 return block;
890}
891#endif
892
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000894{
895 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000896 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000897 int block;
898
899 block = add_zone(znr, corrected);
900 if (!block)
901 return;
902 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000904 add_zone(i + (unsigned short *) blk, &chg_blk);
905 if (chg_blk)
906 write_block(block, blk);
907}
908
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000909#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000911{
912 static char blk[BLOCK_SIZE];
913 int i, chg_blk = 0;
914 int block;
915
Erik Andersene49d5ec2000-02-08 19:58:47 +0000916 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000917 if (!block)
918 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000920 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000921 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000922 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000923 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000924}
925#endif
926
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000928{
929 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000931 int block;
932
933 block = add_zone(znr, corrected);
934 if (!block)
935 return;
936 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000938 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
939 if (blk_chg)
940 write_block(block, blk);
941}
942
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000943#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000944static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000945{
946 static char blk[BLOCK_SIZE];
947 int i, blk_chg = 0;
948 int block;
949
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000951 if (!block)
952 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000953 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000954 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000955 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000956 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000957 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000958}
959
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000961{
962 static char blk[BLOCK_SIZE];
963 int i, blk_chg = 0;
964 int block;
965
Erik Andersene49d5ec2000-02-08 19:58:47 +0000966 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000967 if (!block)
968 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000970 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000972 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000973 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000974}
975#endif
976
Eric Andersene674eb71999-10-19 20:52:57 +0000977static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000978{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000980
981 if (!i || i > INODES)
982 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000983 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000984 return;
985 inode = Inode + i;
986 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000987 !S_ISLNK(inode->i_mode)) return;
988 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000989 add_zone(i + inode->i_zone, &changed);
990 add_zone_ind(7 + inode->i_zone, &changed);
991 add_zone_dind(8 + inode->i_zone, &changed);
992}
993
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000994#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000995static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000996{
997 struct minix2_inode *inode;
998
999 if (!i || i > INODES)
1000 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001002 return;
1003 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1005 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +00001006 return;
1007 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001008 add_zone2(i + inode->i_zone, &changed);
1009 add_zone_ind2(7 + inode->i_zone, &changed);
1010 add_zone_dind2(8 + inode->i_zone, &changed);
1011 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001012}
1013#endif
1014
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001016{
1017 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001018 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001019 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001020 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001021 int block;
1022
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001024 read_block(block, blk);
1025 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001027 if (ino > INODES) {
1028 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001029 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001030 printf("%.*s'.", namelen, name);
1031 if (ask(" Remove", 1)) {
1032 *(unsigned short *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001033 write_block(block, blk);
1034 }
1035 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001036 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001037 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001038 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001039 name_depth++;
1040 inode = get_inode(ino);
1041 name_depth--;
1042 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001044 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001045 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001046 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 } else
1048 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001049 }
1050 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001053 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001054 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 } else
1056 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001057 }
1058 if (!inode)
1059 return;
1060 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061 strncpy(name_list[name_depth], name, namelen);
1062 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +00001063 if (list) {
1064 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001065 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +00001066 print_current_name();
1067 if (S_ISDIR(inode->i_mode))
1068 printf(":\n");
1069 else
1070 printf("\n");
1071 }
1072 check_zones(ino);
1073 if (inode && S_ISDIR(inode->i_mode))
1074 recursive_check(ino);
1075 name_depth--;
1076 return;
1077}
1078
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001079#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001081{
1082 static char blk[BLOCK_SIZE];
1083 struct minix2_inode *inode;
1084 int ino;
1085 char *name;
1086 int block;
1087
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 block = map_block2(dir, offset / BLOCK_SIZE);
1089 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001090 name = blk + (offset % BLOCK_SIZE) + 2;
1091 ino = *(unsigned short *) (name - 2);
1092 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 print_current_name();
1094 printf(" contains a bad inode number for file '");
1095 printf("%.*s'.", namelen, name);
1096 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001097 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001098 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001099 }
1100 ino = 0;
1101 }
1102 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001103 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001104 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001106 name_depth--;
1107 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 if (!inode || strcmp(".", name)) {
1109 print_current_name();
1110 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001111 errors_uncorrected = 1;
1112 } else
1113 return;
1114 }
1115 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 if (!inode || strcmp("..", name)) {
1117 print_current_name();
1118 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001119 errors_uncorrected = 1;
1120 } else
1121 return;
1122 }
1123 if (!inode)
1124 return;
1125 name_depth++;
1126 if (list) {
1127 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1129 print_current_name();
1130 if (S_ISDIR(inode->i_mode))
1131 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001132 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001134 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 check_zones2(ino);
1136 if (inode && S_ISDIR(inode->i_mode))
1137 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001138 name_depth--;
1139 return;
1140}
1141#endif
1142
Eric Andersene674eb71999-10-19 20:52:57 +00001143static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001144{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001146 unsigned int offset;
1147
1148 dir = Inode + ino;
1149 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001150 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001151 if (dir->i_size < 2 * dirsize) {
1152 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001153 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001154 errors_uncorrected = 1;
1155 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 for (offset = 0; offset < dir->i_size; offset += dirsize)
1157 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001158}
1159
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001160#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001161static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001162{
1163 struct minix2_inode *dir;
1164 unsigned int offset;
1165
1166 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 if (!S_ISDIR(dir->i_mode))
1168 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001169 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001170 print_current_name();
1171 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001172 errors_uncorrected = 1;
1173 }
1174 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001175 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001176}
1177#endif
1178
Eric Andersene674eb71999-10-19 20:52:57 +00001179static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001180{
1181 char buffer[1024];
1182
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001184 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001185 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1186}
1187
Eric Andersene674eb71999-10-19 20:52:57 +00001188static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001189{
1190 int i;
1191
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001193 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 printf("Inode %d mode not cleared.", i);
1195 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001196 Inode[i].i_mode = 0;
1197 changed = 1;
1198 }
1199 }
1200 if (!inode_count[i]) {
1201 if (!inode_in_use(i))
1202 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001203 printf("Inode %d not used, marked used in the bitmap.", i);
1204 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001205 unmark_inode(i);
1206 continue;
1207 }
1208 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001209 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001210 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001211 mark_inode(i);
1212 }
1213 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001214 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001215 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1216 if (ask("Set i_nlinks to count", 1)) {
1217 Inode[i].i_nlinks = inode_count[i];
1218 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001219 }
1220 }
1221 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001223 if (zone_in_use(i) == zone_count[i])
1224 continue;
1225 if (!zone_count[i]) {
1226 if (bad_zone(i))
1227 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001228 printf("Zone %d: marked in use, no file uses it.", i);
1229 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001230 unmark_zone(i);
1231 continue;
1232 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001233 printf("Zone %d: %sin use, counted=%d\n",
1234 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001235 }
1236}
1237
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001238#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001239static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001240{
1241 int i;
1242
1243 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1245 printf("Inode %d mode not cleared.", i);
1246 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001247 Inode2[i].i_mode = 0;
1248 changed = 1;
1249 }
1250 }
1251 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001252 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001253 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001254 printf("Inode %d not used, marked used in the bitmap.", i);
1255 if (ask("Clear", 1))
1256 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001257 continue;
1258 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001259 if (!inode_in_use(i)) {
1260 printf("Inode %d used, marked unused in the bitmap.", i);
1261 if (ask("Set", 1))
1262 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001263 }
1264 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001265 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1266 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1267 inode_count[i]);
1268 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001269 Inode2[i].i_nlinks = inode_count[i];
1270 changed = 1;
1271 }
1272 }
1273 }
1274 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001275 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001276 continue;
1277 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001278 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001279 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001280 printf("Zone %d: marked in use, no file uses it.", i);
1281 if (ask("Unmark", 1))
1282 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001283 continue;
1284 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001285 printf("Zone %d: %sin use, counted=%d\n",
1286 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001287 }
1288}
1289#endif
1290
Eric Andersene674eb71999-10-19 20:52:57 +00001291static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001292{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001293 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1294 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001295 check_zones(ROOT_INO);
1296 recursive_check(ROOT_INO);
1297 check_counts();
1298}
1299
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001300#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001301static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001302{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001303 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1304 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1305 check_zones2(ROOT_INO);
1306 recursive_check2(ROOT_INO);
1307 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001308}
1309#endif
1310
John Beppuc1dc5d92000-02-11 12:43:20 +00001311/* Wed Feb 9 15:17:06 MST 2000 */
1312/* dynamically allocate name_list (instead of making it static) */
1313static void alloc_name_list(void)
1314{
1315 int i;
1316
Erik Andersen0d068a22000-03-21 22:32:57 +00001317 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1318 for (i = 0; i < MAX_DEPTH; i++)
Erik Andersen4f3f7572000-04-28 00:18:56 +00001319 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
John Beppuc1dc5d92000-02-11 12:43:20 +00001320}
1321
Eric Andersenb040d4f2000-07-25 18:01:20 +00001322#ifdef BB_FEATURE_CLEAN_UP
John Beppu5e1b2ca2000-02-11 12:52:55 +00001323/* execute this atexit() to deallocate name_list[] */
1324/* piptigger was here */
John Beppuc1dc5d92000-02-11 12:43:20 +00001325static void free_name_list(void)
1326{
John Beppu5e1b2ca2000-02-11 12:52:55 +00001327 int i;
1328
1329 if (name_list) {
1330 for (i = 0; i < MAX_DEPTH; i++) {
1331 if (name_list[i]) {
1332 free(name_list[i]);
1333 }
1334 }
1335 free(name_list);
1336 }
John Beppuc1dc5d92000-02-11 12:43:20 +00001337}
Erik Andersen298854f2000-03-23 01:09:18 +00001338#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001339
Erik Andersene49d5ec2000-02-08 19:58:47 +00001340extern int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001341{
1342 struct termios tmp;
1343 int count;
1344 int retcode = 0;
1345
John Beppuc1dc5d92000-02-11 12:43:20 +00001346 alloc_name_list();
Eric Andersenb040d4f2000-07-25 18:01:20 +00001347#ifdef BB_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001348 /* Don't bother to free memory. Exit does
1349 * that automagically, so we can save a few bytes */
Eric Andersenb040d4f2000-07-25 18:01:20 +00001350 atexit(free_name_list);
1351#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001352
Eric Andersene494fdd1999-10-19 20:23:03 +00001353 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001354 die("bad inode size");
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001355#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001356 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001357 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001358#endif
1359 while (argc-- > 1) {
1360 argv++;
1361 if (argv[0][0] != '-') {
1362 if (device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001363 show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001364 else
1365 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001366 } else
1367 while (*++argv[0])
1368 switch (argv[0][0]) {
1369 case 'l':
1370 list = 1;
1371 break;
1372 case 'a':
1373 automatic = 1;
1374 repair = 1;
1375 break;
1376 case 'r':
1377 automatic = 0;
1378 repair = 1;
1379 break;
1380 case 'v':
1381 verbose = 1;
1382 break;
1383 case 's':
1384 show = 1;
1385 break;
1386 case 'm':
1387 warn_mode = 1;
1388 break;
1389 case 'f':
1390 force = 1;
1391 break;
1392 default:
1393 show_usage();
1394 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001395 }
1396 if (!device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001397 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001398 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001399 if (repair && !automatic) {
1400 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001401 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001402 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001403 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersene494fdd1999-10-19 20:23:03 +00001404 if (IN < 0)
Eric Andersene674eb71999-10-19 20:52:57 +00001405 die("unable to open '%s'");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001406 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001407 sync();
1408 read_superblock();
1409
1410 /*
1411 * Determine whether or not we should continue with the checking.
1412 * This is based on the status of the filesystem valid and error
1413 * flags and whether or not the -f switch was specified on the
1414 * command line.
1415 */
Matt Kraaie58771e2000-07-12 15:38:49 +00001416 printf("%s, %s\n", applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001417 if (!(Super.s_state & MINIX_ERROR_FS) &&
1418 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001419 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001420 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001421 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001422 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001423 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001424 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001425 printf("Filesystem on %s is dirty, needs checking.\n",
1426 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001427
1428 read_tables();
1429
1430 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001431 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001432 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001433 tmp.c_lflag &= ~(ICANON | ECHO);
1434 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001435 termios_set = 1;
1436 }
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001437#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001438 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001439 check_root2();
1440 check2();
1441 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001442#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001443 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001444 check_root();
1445 check();
1446 }
1447 if (verbose) {
1448 int i, free;
1449
Erik Andersene49d5ec2000-02-08 19:58:47 +00001450 for (i = 1, free = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001451 if (!inode_in_use(i))
1452 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001453 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1454 100 * (INODES - free) / INODES);
1455 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001456 if (!zone_in_use(i))
1457 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001458 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1459 100 * (ZONES - free) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001460 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001461 "%6d directories\n"
1462 "%6d character device files\n"
1463 "%6d block device files\n"
1464 "%6d links\n"
1465 "%6d symbolic links\n"
1466 "------\n"
1467 "%6d files\n",
1468 regular, directory, chardev, blockdev,
1469 links - 2 * directory + 1, symlinks,
1470 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001471 }
1472 if (changed) {
1473 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001474 printf("----------------------------\n"
1475 "FILE SYSTEM HAS BEEN CHANGED\n"
1476 "----------------------------\n");
1477 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001478 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001479 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001480 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001481
Eric Andersene494fdd1999-10-19 20:23:03 +00001482 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001483 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001484
1485 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001486 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001487 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001488 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001489 return retcode;
1490}