blob: bc92f20b7721317ba9ae52755dea86d8efb70c94 [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>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000099#include <sys/param.h>
Eric Andersen85c55202000-07-10 22:46:55 +0000100
101
102 typedef unsigned char u8;
103typedef unsigned short u16;
104typedef unsigned int u32;
105
106
107#define MINIX_ROOT_INO 1
108#define MINIX_LINK_MAX 250
109#define MINIX2_LINK_MAX 65530
110
111#define MINIX_I_MAP_SLOTS 8
112#define MINIX_Z_MAP_SLOTS 64
113#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
114#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
115#define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
116#define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
117#define MINIX_VALID_FS 0x0001 /* Clean fs. */
118#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
119
120#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
121#define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
122
123#define MINIX_V1 0x0001 /* original minix fs */
124#define MINIX_V2 0x0002 /* minix V2 fs */
125
126#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
127
128/*
129 * This is the original minix inode layout on disk.
130 * Note the 8-bit gid and atime and ctime.
131 */
132struct minix_inode {
133 u16 i_mode;
134 u16 i_uid;
135 u32 i_size;
136 u32 i_time;
137 u8 i_gid;
138 u8 i_nlinks;
139 u16 i_zone[9];
140};
141
142/*
143 * The new minix inode has all the time entries, as well as
144 * long block numbers and a third indirect block (7+1+1+1
145 * instead of 7+1+1). Also, some previously 8-bit values are
146 * now 16-bit. The inode is now 64 bytes instead of 32.
147 */
148struct minix2_inode {
149 u16 i_mode;
150 u16 i_nlinks;
151 u16 i_uid;
152 u16 i_gid;
153 u32 i_size;
154 u32 i_atime;
155 u32 i_mtime;
156 u32 i_ctime;
157 u32 i_zone[10];
158};
159
160/*
161 * minix super-block data on disk
162 */
163struct minix_super_block {
164 u16 s_ninodes;
165 u16 s_nzones;
166 u16 s_imap_blocks;
167 u16 s_zmap_blocks;
168 u16 s_firstdatazone;
169 u16 s_log_zone_size;
170 u32 s_max_size;
171 u16 s_magic;
172 u16 s_state;
173 u32 s_zones;
174};
175
176struct minix_dir_entry {
177 u16 inode;
178 char name[0];
179};
180
181#define BLOCK_SIZE_BITS 10
182#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
183
184#define NAME_MAX 255 /* # chars in a file name */
185
186#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
187
188#define MINIX_VALID_FS 0x0001 /* Clean fs. */
189#define MINIX_ERROR_FS 0x0002 /* fs has errors. */
190
191#define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
192#define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
193
194#ifndef BLKGETSIZE
195#define BLKGETSIZE _IO(0x12,96) /* return device size */
196#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000197
Eric Andersene494fdd1999-10-19 20:23:03 +0000198#ifndef __linux__
199#define volatile
200#endif
201
202#define ROOT_INO 1
203
204#define UPPER(size,n) ((size+((n)-1))/(n))
205#define INODE_SIZE (sizeof(struct minix_inode))
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000206#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000207#define INODE_SIZE2 (sizeof(struct minix2_inode))
208#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
209 : MINIX_INODES_PER_BLOCK))
210#else
211#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
212#endif
213#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
214
215#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
216
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217static char *program_version = "1.2 - 11/11/96";
218static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000219static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
221 0, warn_mode = 0, force = 0;
222static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
223 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000224
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static int changed = 0; /* flags if the filesystem has been changed */
226static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000227static int dirsize = 16;
228static int namelen = 14;
229static int version2 = 0;
230static struct termios termios;
231static int termios_set = 0;
232
233/* File-name data */
John Beppuc1dc5d92000-02-11 12:43:20 +0000234#define MAX_DEPTH 32
Eric Andersene494fdd1999-10-19 20:23:03 +0000235static int name_depth = 0;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000236// static char name_list[MAX_DEPTH][BUFSIZ + 1];
John Beppu5e1b2ca2000-02-11 12:52:55 +0000237static char **name_list = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000238
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239static char *inode_buffer = NULL;
240
Eric Andersene494fdd1999-10-19 20:23:03 +0000241#define Inode (((struct minix_inode *) inode_buffer)-1)
242#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
243static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244
Eric Andersene494fdd1999-10-19 20:23:03 +0000245#define Super (*(struct minix_super_block *)super_block_buffer)
246#define INODES ((unsigned long)Super.s_ninodes)
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000247#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000248#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
249#else
250#define ZONES ((unsigned long)(Super.s_nzones))
251#endif
252#define IMAPS ((unsigned long)Super.s_imap_blocks)
253#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
254#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
255#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
256#define MAXSIZE ((unsigned long)Super.s_max_size)
257#define MAGIC (Super.s_magic)
258#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
259
260static char *inode_map;
261static char *zone_map;
262
Erik Andersene49d5ec2000-02-08 19:58:47 +0000263static unsigned char *inode_count = NULL;
264static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000265
Eric Andersene674eb71999-10-19 20:52:57 +0000266static void recursive_check(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000267#ifdef BB_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000268static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000269#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000270
Erik Andersen4f3f7572000-04-28 00:18:56 +0000271#define inode_in_use(x) (isset(inode_map,(x)))
272#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000273
274#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
275#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
276
277#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
278#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
279
Eric Andersene674eb71999-10-19 20:52:57 +0000280static void leave(int) __attribute__ ((noreturn));
281static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000282{
283 if (termios_set)
284 tcsetattr(0, TCSANOW, &termios);
285 exit(status);
286}
287
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288static void show_usage(void)
289{
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000290 usage(fsck_minix_usage);
Eric Andersene494fdd1999-10-19 20:23:03 +0000291}
292
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293static void die(const char *str)
294{
Matt Kraaid537a952000-07-14 01:51:25 +0000295 errorMsg("%s\n", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000296 leave(8);
297}
298
299/*
300 * This simply goes through the file-name data and prints out the
301 * current file.
302 */
Eric Andersene674eb71999-10-19 20:52:57 +0000303static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000305 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000306
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 while (i < name_depth)
308 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000309 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000311}
312
Erik Andersene49d5ec2000-02-08 19:58:47 +0000313static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000314{
315 int c;
316
317 if (!repair) {
318 printf("\n");
319 errors_uncorrected = 1;
320 return 0;
321 }
322 if (automatic) {
323 printf("\n");
324 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000326 return def;
327 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000329 for (;;) {
330 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 if ((c = getchar()) == EOF) {
332 if (!def)
333 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000334 return def;
335 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000337 if (c == 'Y') {
338 def = 1;
339 break;
340 } else if (c == 'N') {
341 def = 0;
342 break;
343 } else if (c == ' ' || c == '\n')
344 break;
345 }
346 if (def)
347 printf("y\n");
348 else {
349 printf("n\n");
350 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000352 return def;
353}
354
355/*
356 * Make certain that we aren't checking a filesystem that is on a
357 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
358 * 1994 Theodore Ts'o. Also licensed under GPL.
359 */
360static void check_mount(void)
361{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000362 FILE *f;
363 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000364 int cont;
365 int fd;
366
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000368 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 while ((mnt = getmntent(f)) != NULL)
370 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000371 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000373 if (!mnt)
374 return;
375
376 /*
377 * If the root is mounted read-only, then /etc/mtab is
378 * probably not correct; so we won't issue a warning based on
379 * it.
380 */
381 fd = open(MOUNTED, O_RDWR);
382 if (fd < 0 && errno == EROFS)
383 return;
384 else
385 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386
387 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000388 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000389 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000390 else
391 cont = 0;
392 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000393 printf("check aborted.\n");
394 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000395 }
396 return;
397}
398
399/*
400 * check_zone_nr checks to see that *nr is a valid zone nr. If it
401 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
402 * if an error was corrected, and returns the zone (0 for no zone
403 * or a bad zone-number).
404 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000406{
407 if (!*nr)
408 return 0;
409 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000410 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000411 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000412 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000413 else
414 return *nr;
415 print_current_name();
416 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000417 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 *nr = 0;
419 *corrected = 1;
420 }
421 return 0;
422}
423
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000424#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000426{
427 if (!*nr)
428 return 0;
429 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000431 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000433 else
434 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 print_current_name();
436 printf("'.");
437 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000438 *nr = 0;
439 *corrected = 1;
440 }
441 return 0;
442}
443#endif
444
445/*
446 * read-block reads block nr into the buffer at addr.
447 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000449{
450 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000452 return;
453 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000454 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000455 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000456 print_current_name();
457 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000459 errors_uncorrected = 1;
460 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000461 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000462 print_current_name();
463 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000465 errors_uncorrected = 1;
466 }
467}
468
469/*
470 * write_block writes block nr to disk.
471 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000473{
474 if (!nr)
475 return;
476 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000477 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000479 errors_uncorrected = 1;
480 return;
481 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000483 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000484 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000485 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000486 print_current_name();
487 printf("'\n");
488 errors_uncorrected = 1;
489 }
490}
491
492/*
493 * map-block calculates the absolute block nr of a block in a file.
494 * It sets 'changed' if the inode has needed changing, and re-writes
495 * any indirect blocks with errors.
496 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000498{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000499 unsigned short ind[BLOCK_SIZE >> 1];
500 unsigned short dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000501 int blk_chg, block, result;
502
Erik Andersene49d5ec2000-02-08 19:58:47 +0000503 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000504 return check_zone_nr(inode->i_zone + blknr, &changed);
505 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 block = check_zone_nr(inode->i_zone + 7, &changed);
508 read_block(block, (char *) ind);
509 blk_chg = 0;
510 result = check_zone_nr(blknr + ind, &blk_chg);
511 if (blk_chg)
512 write_block(block, (char *) ind);
513 return result;
514 }
515 blknr -= 512;
516 block = check_zone_nr(inode->i_zone + 8, &changed);
517 read_block(block, (char *) dind);
518 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000519 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000520 if (blk_chg)
521 write_block(block, (char *) dind);
522 block = result;
523 read_block(block, (char *) ind);
524 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000525 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000526 if (blk_chg)
527 write_block(block, (char *) ind);
528 return result;
529}
530
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000531#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000533{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000534 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000535 unsigned int dind[BLOCK_SIZE >> 2];
536 unsigned int tind[BLOCK_SIZE >> 2];
537 int blk_chg, block, result;
538
539 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 blknr -= 7;
542 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 block = check_zone_nr2(inode->i_zone + 7, &changed);
544 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000545 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000547 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000549 return result;
550 }
551 blknr -= 256;
552 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000553 block = check_zone_nr2(inode->i_zone + 8, &changed);
554 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000555 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000556 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000557 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000565 return result;
566 }
567 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000568 block = check_zone_nr2(inode->i_zone + 9, &changed);
569 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000570 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000572 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000573 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000576 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000577 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000578 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000580 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000581 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000582 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000583 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000584 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000585 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000586 return result;
587}
588#endif
589
Eric Andersene674eb71999-10-19 20:52:57 +0000590static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000591{
592 /*
593 * Set the state of the filesystem based on whether or not there
594 * are uncorrected errors. The filesystem valid flag is
595 * unconditionally set if we get this far.
596 */
597 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 Super.s_state |= MINIX_ERROR_FS;
600 else
601 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000602
Eric Andersene494fdd1999-10-19 20:23:03 +0000603 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000604 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000605 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000606 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000607
608 return;
609}
610
Eric Andersene674eb71999-10-19 20:52:57 +0000611static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000612{
613 write_super_block();
614
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000616 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000617 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000618 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000619 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000620 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000621}
622
Erik Andersene49d5ec2000-02-08 19:58:47 +0000623static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000624{
625 int block;
626 char blk[BLOCK_SIZE];
627 int size;
628
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000629#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000630 if (version2)
631 block = Inode2[ROOT_INO].i_zone[0];
632 else
633#endif
634 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000636 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000638 dirsize = size;
639 namelen = size - 2;
640 return;
641 }
642 }
643 /* use defaults */
644}
645
Eric Andersene674eb71999-10-19 20:52:57 +0000646static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000647{
648 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000649 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000650 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000651 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000652 if (MAGIC == MINIX_SUPER_MAGIC) {
653 namelen = 14;
654 dirsize = 16;
655 version2 = 0;
656 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
657 namelen = 30;
658 dirsize = 32;
659 version2 = 0;
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000660#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000661 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
662 namelen = 14;
663 dirsize = 16;
664 version2 = 1;
665 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
666 namelen = 30;
667 dirsize = 32;
668 version2 = 1;
669#endif
670 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000671 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000672 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000673 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000674 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000675 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000676 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000677 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000678}
679
Eric Andersene674eb71999-10-19 20:52:57 +0000680static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000681{
Erik Andersen0d068a22000-03-21 22:32:57 +0000682 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
683 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000684 memset(inode_map, 0, sizeof(inode_map));
685 memset(zone_map, 0, sizeof(zone_map));
Erik Andersen0d068a22000-03-21 22:32:57 +0000686 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
687 inode_count = xmalloc(INODES + 1);
688 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000690 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000691 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000692 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000694 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000695 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000696 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000697 errors_uncorrected = 1;
698 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000699 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000700 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 printf("%ld inodes\n", INODES);
702 printf("%ld blocks\n", ZONES);
703 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
704 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
705 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000706 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000708 }
709}
710
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000712{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000714
715 if (!nr || nr > INODES)
716 return NULL;
717 total++;
718 inode = Inode + nr;
719 if (!inode_count[nr]) {
720 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000721 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000722 print_current_name();
723 printf("'\n");
724 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000726 mark_inode(nr);
727 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000728 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000729 }
730 }
731 if (S_ISDIR(inode->i_mode))
732 directory++;
733 else if (S_ISREG(inode->i_mode))
734 regular++;
735 else if (S_ISCHR(inode->i_mode))
736 chardev++;
737 else if (S_ISBLK(inode->i_mode))
738 blockdev++;
739 else if (S_ISLNK(inode->i_mode))
740 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000741 else if (S_ISSOCK(inode->i_mode));
742 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000743 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 print_current_name();
745 printf(" has mode %05o\n", inode->i_mode);
746 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000747
748 } else
749 links++;
750 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000751 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000752 inode_count[nr]--;
753 errors_uncorrected = 1;
754 }
755 return inode;
756}
757
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000758#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000760{
761 struct minix2_inode *inode;
762
763 if (!nr || nr > INODES)
764 return NULL;
765 total++;
766 inode = Inode2 + nr;
767 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000768 if (!inode_in_use(nr)) {
769 printf("Inode %d marked not used, but used for file '", nr);
770 print_current_name();
771 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 if (ask("Mark in use", 1))
774 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000775 else
776 errors_uncorrected = 1;
777 }
778 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000779 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000780 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000781 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000782 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000784 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000785 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000786 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000788 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 else if (S_ISSOCK(inode->i_mode));
790 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000791 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000792 print_current_name();
793 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000794 }
795 } else
796 links++;
797 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000798 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000799 inode_count[nr]--;
800 errors_uncorrected = 1;
801 }
802 return inode;
803}
804#endif
805
Eric Andersene674eb71999-10-19 20:52:57 +0000806static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000807{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000809
810 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000811 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000812}
813
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000814#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000815static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000816{
817 struct minix2_inode *inode = Inode2 + ROOT_INO;
818
Erik Andersene49d5ec2000-02-08 19:58:47 +0000819 if (!inode || !S_ISDIR(inode->i_mode))
820 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000821}
822#endif
823
Erik Andersene49d5ec2000-02-08 19:58:47 +0000824static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000825{
826 int result;
827 int block;
828
829 result = 0;
830 block = check_zone_nr(znr, corrected);
831 if (!block)
832 return 0;
833 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000834 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000835 print_current_name();
836 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000837 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000838 *znr = 0;
839 block = 0;
840 *corrected = 1;
841 }
842 }
843 if (!block)
844 return 0;
845 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000846 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000847 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000848 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000849 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000850 mark_zone(block);
851 }
852 if (!++zone_count[block])
853 zone_count[block]--;
854 return block;
855}
856
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000857#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000858static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000859{
860 int result;
861 int block;
862
863 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000865 if (!block)
866 return 0;
867 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868 printf("Block has been used before. Now in file `");
869 print_current_name();
870 printf("'.");
871 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000872 *znr = 0;
873 block = 0;
874 *corrected = 1;
875 }
876 }
877 if (!block)
878 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000879 if (!zone_in_use(block)) {
880 printf("Block %d in file `", block);
881 print_current_name();
882 printf("' is marked not in use.");
883 if (ask("Correct", 1))
884 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000885 }
886 if (!++zone_count[block])
887 zone_count[block]--;
888 return block;
889}
890#endif
891
Erik Andersene49d5ec2000-02-08 19:58:47 +0000892static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000893{
894 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000895 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000896 int block;
897
898 block = add_zone(znr, corrected);
899 if (!block)
900 return;
901 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000902 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000903 add_zone(i + (unsigned short *) blk, &chg_blk);
904 if (chg_blk)
905 write_block(block, blk);
906}
907
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000908#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000909static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000910{
911 static char blk[BLOCK_SIZE];
912 int i, chg_blk = 0;
913 int block;
914
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000916 if (!block)
917 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000918 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000919 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000921 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000923}
924#endif
925
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000927{
928 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000930 int block;
931
932 block = add_zone(znr, corrected);
933 if (!block)
934 return;
935 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000937 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
938 if (blk_chg)
939 write_block(block, blk);
940}
941
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000942#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000943static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000944{
945 static char blk[BLOCK_SIZE];
946 int i, blk_chg = 0;
947 int block;
948
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000950 if (!block)
951 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000952 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000953 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000955 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000956 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000957}
958
Erik Andersene49d5ec2000-02-08 19:58:47 +0000959static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000960{
961 static char blk[BLOCK_SIZE];
962 int i, blk_chg = 0;
963 int block;
964
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000966 if (!block)
967 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000968 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000969 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000971 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000973}
974#endif
975
Eric Andersene674eb71999-10-19 20:52:57 +0000976static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000977{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000978 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000979
980 if (!i || i > INODES)
981 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000982 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000983 return;
984 inode = Inode + i;
985 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986 !S_ISLNK(inode->i_mode)) return;
987 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000988 add_zone(i + inode->i_zone, &changed);
989 add_zone_ind(7 + inode->i_zone, &changed);
990 add_zone_dind(8 + inode->i_zone, &changed);
991}
992
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000993#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000994static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000995{
996 struct minix2_inode *inode;
997
998 if (!i || i > INODES)
999 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001000 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001001 return;
1002 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001003 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1004 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +00001005 return;
1006 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007 add_zone2(i + inode->i_zone, &changed);
1008 add_zone_ind2(7 + inode->i_zone, &changed);
1009 add_zone_dind2(8 + inode->i_zone, &changed);
1010 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001011}
1012#endif
1013
Erik Andersene49d5ec2000-02-08 19:58:47 +00001014static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001015{
1016 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001017 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001018 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001019 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001020 int block;
1021
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 read_block(block, blk);
1024 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001026 if (ino > INODES) {
1027 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001028 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 printf("%.*s'.", namelen, name);
1030 if (ask(" Remove", 1)) {
1031 *(unsigned short *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001032 write_block(block, blk);
1033 }
1034 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001036 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001038 name_depth++;
1039 inode = get_inode(ino);
1040 name_depth--;
1041 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001042 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001043 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001044 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001045 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001046 } else
1047 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001048 }
1049 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001051 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001052 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001053 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001054 } else
1055 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001056 }
1057 if (!inode)
1058 return;
1059 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 strncpy(name_list[name_depth], name, namelen);
1061 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +00001062 if (list) {
1063 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +00001065 print_current_name();
1066 if (S_ISDIR(inode->i_mode))
1067 printf(":\n");
1068 else
1069 printf("\n");
1070 }
1071 check_zones(ino);
1072 if (inode && S_ISDIR(inode->i_mode))
1073 recursive_check(ino);
1074 name_depth--;
1075 return;
1076}
1077
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001078#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001079static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001080{
1081 static char blk[BLOCK_SIZE];
1082 struct minix2_inode *inode;
1083 int ino;
1084 char *name;
1085 int block;
1086
Erik Andersene49d5ec2000-02-08 19:58:47 +00001087 block = map_block2(dir, offset / BLOCK_SIZE);
1088 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001089 name = blk + (offset % BLOCK_SIZE) + 2;
1090 ino = *(unsigned short *) (name - 2);
1091 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001092 print_current_name();
1093 printf(" contains a bad inode number for file '");
1094 printf("%.*s'.", namelen, name);
1095 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001096 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001098 }
1099 ino = 0;
1100 }
1101 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001103 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001105 name_depth--;
1106 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 if (!inode || strcmp(".", name)) {
1108 print_current_name();
1109 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001110 errors_uncorrected = 1;
1111 } else
1112 return;
1113 }
1114 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 if (!inode || strcmp("..", name)) {
1116 print_current_name();
1117 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001118 errors_uncorrected = 1;
1119 } else
1120 return;
1121 }
1122 if (!inode)
1123 return;
1124 name_depth++;
1125 if (list) {
1126 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1128 print_current_name();
1129 if (S_ISDIR(inode->i_mode))
1130 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001131 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001133 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 check_zones2(ino);
1135 if (inode && S_ISDIR(inode->i_mode))
1136 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001137 name_depth--;
1138 return;
1139}
1140#endif
1141
Eric Andersene674eb71999-10-19 20:52:57 +00001142static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001143{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001144 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001145 unsigned int offset;
1146
1147 dir = Inode + ino;
1148 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001149 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001150 if (dir->i_size < 2 * dirsize) {
1151 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001152 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001153 errors_uncorrected = 1;
1154 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001155 for (offset = 0; offset < dir->i_size; offset += dirsize)
1156 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001157}
1158
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001159#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001161{
1162 struct minix2_inode *dir;
1163 unsigned int offset;
1164
1165 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 if (!S_ISDIR(dir->i_mode))
1167 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001168 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 print_current_name();
1170 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001171 errors_uncorrected = 1;
1172 }
1173 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001175}
1176#endif
1177
Eric Andersene674eb71999-10-19 20:52:57 +00001178static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001179{
1180 char buffer[1024];
1181
Erik Andersene49d5ec2000-02-08 19:58:47 +00001182 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001183 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001184 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1185}
1186
Eric Andersene674eb71999-10-19 20:52:57 +00001187static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001188{
1189 int i;
1190
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001192 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001193 printf("Inode %d mode not cleared.", i);
1194 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001195 Inode[i].i_mode = 0;
1196 changed = 1;
1197 }
1198 }
1199 if (!inode_count[i]) {
1200 if (!inode_in_use(i))
1201 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001202 printf("Inode %d not used, marked used in the bitmap.", i);
1203 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001204 unmark_inode(i);
1205 continue;
1206 }
1207 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001208 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001210 mark_inode(i);
1211 }
1212 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001213 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1215 if (ask("Set i_nlinks to count", 1)) {
1216 Inode[i].i_nlinks = inode_count[i];
1217 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001218 }
1219 }
1220 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001221 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001222 if (zone_in_use(i) == zone_count[i])
1223 continue;
1224 if (!zone_count[i]) {
1225 if (bad_zone(i))
1226 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 printf("Zone %d: marked in use, no file uses it.", i);
1228 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001229 unmark_zone(i);
1230 continue;
1231 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 printf("Zone %d: %sin use, counted=%d\n",
1233 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001234 }
1235}
1236
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001237#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001238static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001239{
1240 int i;
1241
1242 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001243 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1244 printf("Inode %d mode not cleared.", i);
1245 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001246 Inode2[i].i_mode = 0;
1247 changed = 1;
1248 }
1249 }
1250 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001251 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001252 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001253 printf("Inode %d not used, marked used in the bitmap.", i);
1254 if (ask("Clear", 1))
1255 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001256 continue;
1257 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001258 if (!inode_in_use(i)) {
1259 printf("Inode %d used, marked unused in the bitmap.", i);
1260 if (ask("Set", 1))
1261 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001262 }
1263 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1265 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1266 inode_count[i]);
1267 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001268 Inode2[i].i_nlinks = inode_count[i];
1269 changed = 1;
1270 }
1271 }
1272 }
1273 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001274 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001275 continue;
1276 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001277 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001278 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001279 printf("Zone %d: marked in use, no file uses it.", i);
1280 if (ask("Unmark", 1))
1281 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001282 continue;
1283 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001284 printf("Zone %d: %sin use, counted=%d\n",
1285 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001286 }
1287}
1288#endif
1289
Eric Andersene674eb71999-10-19 20:52:57 +00001290static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001291{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001292 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1293 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001294 check_zones(ROOT_INO);
1295 recursive_check(ROOT_INO);
1296 check_counts();
1297}
1298
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001299#ifdef BB_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001300static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001301{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001302 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1303 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1304 check_zones2(ROOT_INO);
1305 recursive_check2(ROOT_INO);
1306 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001307}
1308#endif
1309
John Beppuc1dc5d92000-02-11 12:43:20 +00001310/* Wed Feb 9 15:17:06 MST 2000 */
1311/* dynamically allocate name_list (instead of making it static) */
1312static void alloc_name_list(void)
1313{
1314 int i;
1315
Erik Andersen0d068a22000-03-21 22:32:57 +00001316 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1317 for (i = 0; i < MAX_DEPTH; i++)
Erik Andersen4f3f7572000-04-28 00:18:56 +00001318 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
John Beppuc1dc5d92000-02-11 12:43:20 +00001319}
1320
Eric Andersenb040d4f2000-07-25 18:01:20 +00001321#ifdef BB_FEATURE_CLEAN_UP
John Beppu5e1b2ca2000-02-11 12:52:55 +00001322/* execute this atexit() to deallocate name_list[] */
1323/* piptigger was here */
John Beppuc1dc5d92000-02-11 12:43:20 +00001324static void free_name_list(void)
1325{
John Beppu5e1b2ca2000-02-11 12:52:55 +00001326 int i;
1327
1328 if (name_list) {
1329 for (i = 0; i < MAX_DEPTH; i++) {
1330 if (name_list[i]) {
1331 free(name_list[i]);
1332 }
1333 }
1334 free(name_list);
1335 }
John Beppuc1dc5d92000-02-11 12:43:20 +00001336}
Erik Andersen298854f2000-03-23 01:09:18 +00001337#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001338
Erik Andersene49d5ec2000-02-08 19:58:47 +00001339extern int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001340{
1341 struct termios tmp;
1342 int count;
1343 int retcode = 0;
1344
John Beppuc1dc5d92000-02-11 12:43:20 +00001345 alloc_name_list();
Eric Andersenb040d4f2000-07-25 18:01:20 +00001346#ifdef BB_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001347 /* Don't bother to free memory. Exit does
1348 * that automagically, so we can save a few bytes */
Eric Andersenb040d4f2000-07-25 18:01:20 +00001349 atexit(free_name_list);
1350#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001351
Eric Andersene494fdd1999-10-19 20:23:03 +00001352 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001353 die("bad inode size");
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001354#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001355 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001356 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001357#endif
1358 while (argc-- > 1) {
1359 argv++;
1360 if (argv[0][0] != '-') {
1361 if (device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001362 show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001363 else
1364 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001365 } else
1366 while (*++argv[0])
1367 switch (argv[0][0]) {
1368 case 'l':
1369 list = 1;
1370 break;
1371 case 'a':
1372 automatic = 1;
1373 repair = 1;
1374 break;
1375 case 'r':
1376 automatic = 0;
1377 repair = 1;
1378 break;
1379 case 'v':
1380 verbose = 1;
1381 break;
1382 case 's':
1383 show = 1;
1384 break;
1385 case 'm':
1386 warn_mode = 1;
1387 break;
1388 case 'f':
1389 force = 1;
1390 break;
1391 default:
1392 show_usage();
1393 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001394 }
1395 if (!device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001396 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001397 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001398 if (repair && !automatic) {
1399 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001400 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001401 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001402 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersene494fdd1999-10-19 20:23:03 +00001403 if (IN < 0)
Eric Andersene674eb71999-10-19 20:52:57 +00001404 die("unable to open '%s'");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001405 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001406 sync();
1407 read_superblock();
1408
1409 /*
1410 * Determine whether or not we should continue with the checking.
1411 * This is based on the status of the filesystem valid and error
1412 * flags and whether or not the -f switch was specified on the
1413 * command line.
1414 */
Matt Kraaie58771e2000-07-12 15:38:49 +00001415 printf("%s, %s\n", applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001416 if (!(Super.s_state & MINIX_ERROR_FS) &&
1417 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001418 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001419 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001420 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001422 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001423 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424 printf("Filesystem on %s is dirty, needs checking.\n",
1425 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001426
1427 read_tables();
1428
1429 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001430 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001431 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 tmp.c_lflag &= ~(ICANON | ECHO);
1433 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001434 termios_set = 1;
1435 }
Pavel Roskinbc0aed72000-07-17 17:52:00 +00001436#ifdef BB_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001437 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001438 check_root2();
1439 check2();
1440 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001441#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001442 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001443 check_root();
1444 check();
1445 }
1446 if (verbose) {
1447 int i, free;
1448
Erik Andersene49d5ec2000-02-08 19:58:47 +00001449 for (i = 1, free = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001450 if (!inode_in_use(i))
1451 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001452 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1453 100 * (INODES - free) / INODES);
1454 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001455 if (!zone_in_use(i))
1456 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001457 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1458 100 * (ZONES - free) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001459 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001460 "%6d directories\n"
1461 "%6d character device files\n"
1462 "%6d block device files\n"
1463 "%6d links\n"
1464 "%6d symbolic links\n"
1465 "------\n"
1466 "%6d files\n",
1467 regular, directory, chardev, blockdev,
1468 links - 2 * directory + 1, symlinks,
1469 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001470 }
1471 if (changed) {
1472 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473 printf("----------------------------\n"
1474 "FILE SYSTEM HAS BEEN CHANGED\n"
1475 "----------------------------\n");
1476 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001477 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001478 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001479 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001480
Eric Andersene494fdd1999-10-19 20:23:03 +00001481 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001482 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001483
1484 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001485 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001486 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001487 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001488 return retcode;
1489}