blob: ea27c236ca505a0c9510f19528107f3b8f927702 [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
199#ifdef MINIX2_SUPER_MAGIC2
200#define HAVE_MINIX2 1
201#endif
202
203#ifndef __linux__
204#define volatile
205#endif
206
207#define ROOT_INO 1
208
209#define UPPER(size,n) ((size+((n)-1))/(n))
210#define INODE_SIZE (sizeof(struct minix_inode))
211#ifdef HAVE_MINIX2
212#define INODE_SIZE2 (sizeof(struct minix2_inode))
213#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
214 : MINIX_INODES_PER_BLOCK))
215#else
216#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
217#endif
218#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
219
220#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
221
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222static char *program_version = "1.2 - 11/11/96";
223static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000224static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
226 0, warn_mode = 0, force = 0;
227static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
228 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000229
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230static int changed = 0; /* flags if the filesystem has been changed */
231static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000232static int dirsize = 16;
233static int namelen = 14;
234static int version2 = 0;
235static struct termios termios;
236static int termios_set = 0;
237
238/* File-name data */
John Beppuc1dc5d92000-02-11 12:43:20 +0000239#define MAX_DEPTH 32
Eric Andersene494fdd1999-10-19 20:23:03 +0000240static int name_depth = 0;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000241// static char name_list[MAX_DEPTH][BUFSIZ + 1];
John Beppu5e1b2ca2000-02-11 12:52:55 +0000242static char **name_list = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000243
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244static char *inode_buffer = NULL;
245
Eric Andersene494fdd1999-10-19 20:23:03 +0000246#define Inode (((struct minix_inode *) inode_buffer)-1)
247#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
248static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249
Eric Andersene494fdd1999-10-19 20:23:03 +0000250#define Super (*(struct minix_super_block *)super_block_buffer)
251#define INODES ((unsigned long)Super.s_ninodes)
252#ifdef HAVE_MINIX2
253#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
254#else
255#define ZONES ((unsigned long)(Super.s_nzones))
256#endif
257#define IMAPS ((unsigned long)Super.s_imap_blocks)
258#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
259#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
260#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
261#define MAXSIZE ((unsigned long)Super.s_max_size)
262#define MAGIC (Super.s_magic)
263#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
264
265static char *inode_map;
266static char *zone_map;
267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268static unsigned char *inode_count = NULL;
269static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000270
Eric Andersene674eb71999-10-19 20:52:57 +0000271static void recursive_check(unsigned int ino);
272static void recursive_check2(unsigned int ino);
Eric Andersene494fdd1999-10-19 20:23:03 +0000273
Erik Andersen4f3f7572000-04-28 00:18:56 +0000274#define inode_in_use(x) (isset(inode_map,(x)))
275#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000276
277#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
278#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
279
280#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
281#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
282
Eric Andersene674eb71999-10-19 20:52:57 +0000283static void leave(int) __attribute__ ((noreturn));
284static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000285{
286 if (termios_set)
287 tcsetattr(0, TCSANOW, &termios);
288 exit(status);
289}
290
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000291const char fsck_minix_usage[] =
292 "Usage: fsck.minix [-larvsmf] /dev/name\n"
293#ifndef BB_FEATURE_TRIVIAL_HELP
294 "\nPerforms a consistency check for MINIX filesystems.\n\n"
295 "Options:\n"
296 "\t-l\tLists all filenames\n"
297 "\t-r\tPerform interactive repairs\n"
298 "\t-a\tPerform automatic repairs\n"
299 "\t-v\tverbose\n"
300 "\t-s\tOutputs super-block information\n"
301 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n"
302 "\t-f\tForce file system check.\n\n"
303#endif
304 ;
305
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306static void show_usage(void)
307{
Matt Kraai3bd8bd82000-07-14 23:28:47 +0000308 usage(fsck_minix_usage);
Eric Andersene494fdd1999-10-19 20:23:03 +0000309}
310
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311static void die(const char *str)
312{
Matt Kraaid537a952000-07-14 01:51:25 +0000313 errorMsg("%s\n", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000314 leave(8);
315}
316
317/*
318 * This simply goes through the file-name data and prints out the
319 * current file.
320 */
Eric Andersene674eb71999-10-19 20:52:57 +0000321static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000322{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000324
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 while (i < name_depth)
326 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000327 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000329}
330
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000332{
333 int c;
334
335 if (!repair) {
336 printf("\n");
337 errors_uncorrected = 1;
338 return 0;
339 }
340 if (automatic) {
341 printf("\n");
342 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000344 return def;
345 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000347 for (;;) {
348 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 if ((c = getchar()) == EOF) {
350 if (!def)
351 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000352 return def;
353 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000354 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000355 if (c == 'Y') {
356 def = 1;
357 break;
358 } else if (c == 'N') {
359 def = 0;
360 break;
361 } else if (c == ' ' || c == '\n')
362 break;
363 }
364 if (def)
365 printf("y\n");
366 else {
367 printf("n\n");
368 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000370 return def;
371}
372
373/*
374 * Make certain that we aren't checking a filesystem that is on a
375 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
376 * 1994 Theodore Ts'o. Also licensed under GPL.
377 */
378static void check_mount(void)
379{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 FILE *f;
381 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000382 int cont;
383 int fd;
384
Erik Andersene49d5ec2000-02-08 19:58:47 +0000385 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000387 while ((mnt = getmntent(f)) != NULL)
388 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000391 if (!mnt)
392 return;
393
394 /*
395 * If the root is mounted read-only, then /etc/mtab is
396 * probably not correct; so we won't issue a warning based on
397 * it.
398 */
399 fd = open(MOUNTED, O_RDWR);
400 if (fd < 0 && errno == EROFS)
401 return;
402 else
403 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404
405 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000406 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000407 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000408 else
409 cont = 0;
410 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 printf("check aborted.\n");
412 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000413 }
414 return;
415}
416
417/*
418 * check_zone_nr checks to see that *nr is a valid zone nr. If it
419 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
420 * if an error was corrected, and returns the zone (0 for no zone
421 * or a bad zone-number).
422 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000424{
425 if (!*nr)
426 return 0;
427 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000428 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000429 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000430 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000431 else
432 return *nr;
433 print_current_name();
434 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000436 *nr = 0;
437 *corrected = 1;
438 }
439 return 0;
440}
441
442#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000444{
445 if (!*nr)
446 return 0;
447 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000451 else
452 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 print_current_name();
454 printf("'.");
455 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000456 *nr = 0;
457 *corrected = 1;
458 }
459 return 0;
460}
461#endif
462
463/*
464 * read-block reads block nr into the buffer at addr.
465 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000467{
468 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 return;
471 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000473 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 print_current_name();
475 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000477 errors_uncorrected = 1;
478 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000479 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000480 print_current_name();
481 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000482 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000483 errors_uncorrected = 1;
484 }
485}
486
487/*
488 * write_block writes block nr to disk.
489 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000490static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000491{
492 if (!nr)
493 return;
494 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000495 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000497 errors_uncorrected = 1;
498 return;
499 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000501 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000503 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000504 print_current_name();
505 printf("'\n");
506 errors_uncorrected = 1;
507 }
508}
509
510/*
511 * map-block calculates the absolute block nr of a block in a file.
512 * It sets 'changed' if the inode has needed changing, and re-writes
513 * any indirect blocks with errors.
514 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000516{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000517 unsigned short ind[BLOCK_SIZE >> 1];
518 unsigned short dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000519 int blk_chg, block, result;
520
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000522 return check_zone_nr(inode->i_zone + blknr, &changed);
523 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000525 block = check_zone_nr(inode->i_zone + 7, &changed);
526 read_block(block, (char *) ind);
527 blk_chg = 0;
528 result = check_zone_nr(blknr + ind, &blk_chg);
529 if (blk_chg)
530 write_block(block, (char *) ind);
531 return result;
532 }
533 blknr -= 512;
534 block = check_zone_nr(inode->i_zone + 8, &changed);
535 read_block(block, (char *) dind);
536 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000538 if (blk_chg)
539 write_block(block, (char *) dind);
540 block = result;
541 read_block(block, (char *) ind);
542 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000544 if (blk_chg)
545 write_block(block, (char *) ind);
546 return result;
547}
548
549#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000550static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000551{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000553 unsigned int dind[BLOCK_SIZE >> 2];
554 unsigned int tind[BLOCK_SIZE >> 2];
555 int blk_chg, block, result;
556
557 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000558 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 blknr -= 7;
560 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 block = check_zone_nr2(inode->i_zone + 7, &changed);
562 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000565 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000567 return result;
568 }
569 blknr -= 256;
570 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000571 block = check_zone_nr2(inode->i_zone + 8, &changed);
572 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000577 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000579 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000581 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 return result;
584 }
585 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 block = check_zone_nr2(inode->i_zone + 9, &changed);
587 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000588 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000590 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000592 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000594 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000595 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000596 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000597 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000598 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000601 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000602 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000604 return result;
605}
606#endif
607
Eric Andersene674eb71999-10-19 20:52:57 +0000608static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000609{
610 /*
611 * Set the state of the filesystem based on whether or not there
612 * are uncorrected errors. The filesystem valid flag is
613 * unconditionally set if we get this far.
614 */
615 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000617 Super.s_state |= MINIX_ERROR_FS;
618 else
619 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620
Eric Andersene494fdd1999-10-19 20:23:03 +0000621 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000622 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000623 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000624 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000625
626 return;
627}
628
Eric Andersene674eb71999-10-19 20:52:57 +0000629static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000630{
631 write_super_block();
632
Erik Andersene49d5ec2000-02-08 19:58:47 +0000633 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000634 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000635 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000636 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000637 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000638 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000639}
640
Erik Andersene49d5ec2000-02-08 19:58:47 +0000641static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000642{
643 int block;
644 char blk[BLOCK_SIZE];
645 int size;
646
647#if HAVE_MINIX2
648 if (version2)
649 block = Inode2[ROOT_INO].i_zone[0];
650 else
651#endif
652 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000655 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000656 dirsize = size;
657 namelen = size - 2;
658 return;
659 }
660 }
661 /* use defaults */
662}
663
Eric Andersene674eb71999-10-19 20:52:57 +0000664static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000665{
666 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000667 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000668 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000669 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000670 if (MAGIC == MINIX_SUPER_MAGIC) {
671 namelen = 14;
672 dirsize = 16;
673 version2 = 0;
674 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
675 namelen = 30;
676 dirsize = 32;
677 version2 = 0;
678#ifdef HAVE_MINIX2
679 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
680 namelen = 14;
681 dirsize = 16;
682 version2 = 1;
683 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
684 namelen = 30;
685 dirsize = 32;
686 version2 = 1;
687#endif
688 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000689 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000690 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000691 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000692 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000693 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000694 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000695 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000696}
697
Eric Andersene674eb71999-10-19 20:52:57 +0000698static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000699{
Erik Andersen0d068a22000-03-21 22:32:57 +0000700 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
701 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000702 memset(inode_map, 0, sizeof(inode_map));
703 memset(zone_map, 0, sizeof(zone_map));
Erik Andersen0d068a22000-03-21 22:32:57 +0000704 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
705 inode_count = xmalloc(INODES + 1);
706 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000707 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000708 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000709 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000710 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000711 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000712 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000713 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000714 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000715 errors_uncorrected = 1;
716 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000718 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000719 printf("%ld inodes\n", INODES);
720 printf("%ld blocks\n", ZONES);
721 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
722 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
723 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000724 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000725 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000726 }
727}
728
Erik Andersene49d5ec2000-02-08 19:58:47 +0000729struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000730{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000731 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000732
733 if (!nr || nr > INODES)
734 return NULL;
735 total++;
736 inode = Inode + nr;
737 if (!inode_count[nr]) {
738 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000739 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000740 print_current_name();
741 printf("'\n");
742 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000744 mark_inode(nr);
745 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000746 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000747 }
748 }
749 if (S_ISDIR(inode->i_mode))
750 directory++;
751 else if (S_ISREG(inode->i_mode))
752 regular++;
753 else if (S_ISCHR(inode->i_mode))
754 chardev++;
755 else if (S_ISBLK(inode->i_mode))
756 blockdev++;
757 else if (S_ISLNK(inode->i_mode))
758 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 else if (S_ISSOCK(inode->i_mode));
760 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000761 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 print_current_name();
763 printf(" has mode %05o\n", inode->i_mode);
764 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000765
766 } else
767 links++;
768 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000769 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000770 inode_count[nr]--;
771 errors_uncorrected = 1;
772 }
773 return inode;
774}
775
776#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000777struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000778{
779 struct minix2_inode *inode;
780
781 if (!nr || nr > INODES)
782 return NULL;
783 total++;
784 inode = Inode2 + nr;
785 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000786 if (!inode_in_use(nr)) {
787 printf("Inode %d marked not used, but used for file '", nr);
788 print_current_name();
789 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000790 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000791 if (ask("Mark in use", 1))
792 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000793 else
794 errors_uncorrected = 1;
795 }
796 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000798 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000799 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000800 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000801 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000802 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000804 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000805 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000806 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 else if (S_ISSOCK(inode->i_mode));
808 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000809 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000810 print_current_name();
811 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000812 }
813 } else
814 links++;
815 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000816 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000817 inode_count[nr]--;
818 errors_uncorrected = 1;
819 }
820 return inode;
821}
822#endif
823
Eric Andersene674eb71999-10-19 20:52:57 +0000824static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000825{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000826 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000827
828 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000829 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000830}
831
832#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000833static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000834{
835 struct minix2_inode *inode = Inode2 + ROOT_INO;
836
Erik Andersene49d5ec2000-02-08 19:58:47 +0000837 if (!inode || !S_ISDIR(inode->i_mode))
838 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000839}
840#endif
841
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000843{
844 int result;
845 int block;
846
847 result = 0;
848 block = check_zone_nr(znr, corrected);
849 if (!block)
850 return 0;
851 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000852 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000853 print_current_name();
854 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000856 *znr = 0;
857 block = 0;
858 *corrected = 1;
859 }
860 }
861 if (!block)
862 return 0;
863 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000865 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000866 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000867 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000868 mark_zone(block);
869 }
870 if (!++zone_count[block])
871 zone_count[block]--;
872 return block;
873}
874
875#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000876static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000877{
878 int result;
879 int block;
880
881 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000882 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000883 if (!block)
884 return 0;
885 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000886 printf("Block has been used before. Now in file `");
887 print_current_name();
888 printf("'.");
889 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000890 *znr = 0;
891 block = 0;
892 *corrected = 1;
893 }
894 }
895 if (!block)
896 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 if (!zone_in_use(block)) {
898 printf("Block %d in file `", block);
899 print_current_name();
900 printf("' is marked not in use.");
901 if (ask("Correct", 1))
902 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000903 }
904 if (!++zone_count[block])
905 zone_count[block]--;
906 return block;
907}
908#endif
909
Erik Andersene49d5ec2000-02-08 19:58:47 +0000910static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000911{
912 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000913 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000914 int block;
915
916 block = add_zone(znr, corrected);
917 if (!block)
918 return;
919 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000920 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000921 add_zone(i + (unsigned short *) blk, &chg_blk);
922 if (chg_blk)
923 write_block(block, blk);
924}
925
926#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000927static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000928{
929 static char blk[BLOCK_SIZE];
930 int i, chg_blk = 0;
931 int block;
932
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000934 if (!block)
935 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000936 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000937 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000938 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000939 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000940 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000941}
942#endif
943
Erik Andersene49d5ec2000-02-08 19:58:47 +0000944static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000945{
946 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000948 int block;
949
950 block = add_zone(znr, corrected);
951 if (!block)
952 return;
953 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000955 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
956 if (blk_chg)
957 write_block(block, blk);
958}
959
960#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000962{
963 static char blk[BLOCK_SIZE];
964 int i, blk_chg = 0;
965 int block;
966
Erik Andersene49d5ec2000-02-08 19:58:47 +0000967 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000968 if (!block)
969 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000970 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000971 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000972 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000973 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000974 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000975}
976
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000978{
979 static char blk[BLOCK_SIZE];
980 int i, blk_chg = 0;
981 int block;
982
Erik Andersene49d5ec2000-02-08 19:58:47 +0000983 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000984 if (!block)
985 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000986 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000987 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000988 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000989 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000991}
992#endif
993
Eric Andersene674eb71999-10-19 20:52:57 +0000994static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000995{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000997
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 = Inode + i;
1003 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004 !S_ISLNK(inode->i_mode)) return;
1005 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001006 add_zone(i + inode->i_zone, &changed);
1007 add_zone_ind(7 + inode->i_zone, &changed);
1008 add_zone_dind(8 + inode->i_zone, &changed);
1009}
1010
1011#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001013{
1014 struct minix2_inode *inode;
1015
1016 if (!i || i > INODES)
1017 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001018 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001019 return;
1020 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001021 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1022 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 return;
1024 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001025 add_zone2(i + inode->i_zone, &changed);
1026 add_zone_ind2(7 + inode->i_zone, &changed);
1027 add_zone_dind2(8 + inode->i_zone, &changed);
1028 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001029}
1030#endif
1031
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001033{
1034 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001036 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001038 int block;
1039
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001041 read_block(block, blk);
1042 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001043 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001044 if (ino > INODES) {
1045 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001046 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001047 printf("%.*s'.", namelen, name);
1048 if (ask(" Remove", 1)) {
1049 *(unsigned short *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001050 write_block(block, blk);
1051 }
1052 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001053 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001054 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001055 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001056 name_depth++;
1057 inode = get_inode(ino);
1058 name_depth--;
1059 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001060 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001061 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001062 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001063 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001064 } else
1065 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001066 }
1067 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001068 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001069 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001070 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001071 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001072 } else
1073 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001074 }
1075 if (!inode)
1076 return;
1077 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 strncpy(name_list[name_depth], name, namelen);
1079 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +00001080 if (list) {
1081 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +00001083 print_current_name();
1084 if (S_ISDIR(inode->i_mode))
1085 printf(":\n");
1086 else
1087 printf("\n");
1088 }
1089 check_zones(ino);
1090 if (inode && S_ISDIR(inode->i_mode))
1091 recursive_check(ino);
1092 name_depth--;
1093 return;
1094}
1095
1096#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001098{
1099 static char blk[BLOCK_SIZE];
1100 struct minix2_inode *inode;
1101 int ino;
1102 char *name;
1103 int block;
1104
Erik Andersene49d5ec2000-02-08 19:58:47 +00001105 block = map_block2(dir, offset / BLOCK_SIZE);
1106 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001107 name = blk + (offset % BLOCK_SIZE) + 2;
1108 ino = *(unsigned short *) (name - 2);
1109 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 print_current_name();
1111 printf(" contains a bad inode number for file '");
1112 printf("%.*s'.", namelen, name);
1113 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001114 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001116 }
1117 ino = 0;
1118 }
1119 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001120 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001121 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 name_depth--;
1124 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001125 if (!inode || strcmp(".", name)) {
1126 print_current_name();
1127 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001128 errors_uncorrected = 1;
1129 } else
1130 return;
1131 }
1132 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001133 if (!inode || strcmp("..", name)) {
1134 print_current_name();
1135 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001136 errors_uncorrected = 1;
1137 } else
1138 return;
1139 }
1140 if (!inode)
1141 return;
1142 name_depth++;
1143 if (list) {
1144 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1146 print_current_name();
1147 if (S_ISDIR(inode->i_mode))
1148 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001149 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001150 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001151 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152 check_zones2(ino);
1153 if (inode && S_ISDIR(inode->i_mode))
1154 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001155 name_depth--;
1156 return;
1157}
1158#endif
1159
Eric Andersene674eb71999-10-19 20:52:57 +00001160static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001161{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001163 unsigned int offset;
1164
1165 dir = Inode + ino;
1166 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001167 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001168 if (dir->i_size < 2 * dirsize) {
1169 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001170 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001171 errors_uncorrected = 1;
1172 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001173 for (offset = 0; offset < dir->i_size; offset += dirsize)
1174 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001175}
1176
1177#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001178static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001179{
1180 struct minix2_inode *dir;
1181 unsigned int offset;
1182
1183 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 if (!S_ISDIR(dir->i_mode))
1185 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001186 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001187 print_current_name();
1188 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001189 errors_uncorrected = 1;
1190 }
1191 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001193}
1194#endif
1195
Eric Andersene674eb71999-10-19 20:52:57 +00001196static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001197{
1198 char buffer[1024];
1199
Erik Andersene49d5ec2000-02-08 19:58:47 +00001200 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001201 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001202 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1203}
1204
Eric Andersene674eb71999-10-19 20:52:57 +00001205static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001206{
1207 int i;
1208
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001210 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001211 printf("Inode %d mode not cleared.", i);
1212 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001213 Inode[i].i_mode = 0;
1214 changed = 1;
1215 }
1216 }
1217 if (!inode_count[i]) {
1218 if (!inode_in_use(i))
1219 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 printf("Inode %d not used, marked used in the bitmap.", i);
1221 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001222 unmark_inode(i);
1223 continue;
1224 }
1225 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001226 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001228 mark_inode(i);
1229 }
1230 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001231 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001232 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1233 if (ask("Set i_nlinks to count", 1)) {
1234 Inode[i].i_nlinks = inode_count[i];
1235 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001236 }
1237 }
1238 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001239 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001240 if (zone_in_use(i) == zone_count[i])
1241 continue;
1242 if (!zone_count[i]) {
1243 if (bad_zone(i))
1244 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245 printf("Zone %d: marked in use, no file uses it.", i);
1246 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001247 unmark_zone(i);
1248 continue;
1249 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001250 printf("Zone %d: %sin use, counted=%d\n",
1251 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001252 }
1253}
1254
1255#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001256static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001257{
1258 int i;
1259
1260 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001261 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1262 printf("Inode %d mode not cleared.", i);
1263 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001264 Inode2[i].i_mode = 0;
1265 changed = 1;
1266 }
1267 }
1268 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001269 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001270 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001271 printf("Inode %d not used, marked used in the bitmap.", i);
1272 if (ask("Clear", 1))
1273 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001274 continue;
1275 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001276 if (!inode_in_use(i)) {
1277 printf("Inode %d used, marked unused in the bitmap.", i);
1278 if (ask("Set", 1))
1279 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001280 }
1281 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001282 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1283 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1284 inode_count[i]);
1285 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001286 Inode2[i].i_nlinks = inode_count[i];
1287 changed = 1;
1288 }
1289 }
1290 }
1291 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001292 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001293 continue;
1294 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001295 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001296 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001297 printf("Zone %d: marked in use, no file uses it.", i);
1298 if (ask("Unmark", 1))
1299 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001300 continue;
1301 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001302 printf("Zone %d: %sin use, counted=%d\n",
1303 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001304 }
1305}
1306#endif
1307
Eric Andersene674eb71999-10-19 20:52:57 +00001308static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001309{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001310 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1311 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001312 check_zones(ROOT_INO);
1313 recursive_check(ROOT_INO);
1314 check_counts();
1315}
1316
1317#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001318static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001319{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001320 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1321 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1322 check_zones2(ROOT_INO);
1323 recursive_check2(ROOT_INO);
1324 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001325}
1326#endif
1327
John Beppuc1dc5d92000-02-11 12:43:20 +00001328/* Wed Feb 9 15:17:06 MST 2000 */
1329/* dynamically allocate name_list (instead of making it static) */
1330static void alloc_name_list(void)
1331{
1332 int i;
1333
Erik Andersen0d068a22000-03-21 22:32:57 +00001334 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1335 for (i = 0; i < MAX_DEPTH; i++)
Erik Andersen4f3f7572000-04-28 00:18:56 +00001336 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
John Beppuc1dc5d92000-02-11 12:43:20 +00001337}
1338
Erik Andersen298854f2000-03-23 01:09:18 +00001339#if 0
John Beppu5e1b2ca2000-02-11 12:52:55 +00001340/* execute this atexit() to deallocate name_list[] */
1341/* piptigger was here */
John Beppuc1dc5d92000-02-11 12:43:20 +00001342static void free_name_list(void)
1343{
John Beppu5e1b2ca2000-02-11 12:52:55 +00001344 int i;
1345
1346 if (name_list) {
1347 for (i = 0; i < MAX_DEPTH; i++) {
1348 if (name_list[i]) {
1349 free(name_list[i]);
1350 }
1351 }
1352 free(name_list);
1353 }
John Beppuc1dc5d92000-02-11 12:43:20 +00001354}
Erik Andersen298854f2000-03-23 01:09:18 +00001355#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001356
Erik Andersene49d5ec2000-02-08 19:58:47 +00001357extern int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001358{
1359 struct termios tmp;
1360 int count;
1361 int retcode = 0;
1362
John Beppuc1dc5d92000-02-11 12:43:20 +00001363 alloc_name_list();
Erik Andersen298854f2000-03-23 01:09:18 +00001364 /* Don't bother to free memory. Exit does
1365 * that automagically, so we can save a few bytes */
1366 //atexit(free_name_list);
John Beppuc1dc5d92000-02-11 12:43:20 +00001367
Eric Andersene494fdd1999-10-19 20:23:03 +00001368 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001369 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001370#ifdef HAVE_MINIX2
1371 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001372 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001373#endif
1374 while (argc-- > 1) {
1375 argv++;
1376 if (argv[0][0] != '-') {
1377 if (device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001378 show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001379 else
1380 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001381 } else
1382 while (*++argv[0])
1383 switch (argv[0][0]) {
1384 case 'l':
1385 list = 1;
1386 break;
1387 case 'a':
1388 automatic = 1;
1389 repair = 1;
1390 break;
1391 case 'r':
1392 automatic = 0;
1393 repair = 1;
1394 break;
1395 case 'v':
1396 verbose = 1;
1397 break;
1398 case 's':
1399 show = 1;
1400 break;
1401 case 'm':
1402 warn_mode = 1;
1403 break;
1404 case 'f':
1405 force = 1;
1406 break;
1407 default:
1408 show_usage();
1409 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001410 }
1411 if (!device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001412 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001413 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001414 if (repair && !automatic) {
1415 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001416 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001417 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001418 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersene494fdd1999-10-19 20:23:03 +00001419 if (IN < 0)
Eric Andersene674eb71999-10-19 20:52:57 +00001420 die("unable to open '%s'");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001422 sync();
1423 read_superblock();
1424
1425 /*
1426 * Determine whether or not we should continue with the checking.
1427 * This is based on the status of the filesystem valid and error
1428 * flags and whether or not the -f switch was specified on the
1429 * command line.
1430 */
Matt Kraaie58771e2000-07-12 15:38:49 +00001431 printf("%s, %s\n", applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001432 if (!(Super.s_state & MINIX_ERROR_FS) &&
1433 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001434 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001435 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001436 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001437 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001438 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001439 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001440 printf("Filesystem on %s is dirty, needs checking.\n",
1441 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001442
1443 read_tables();
1444
1445 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001446 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001447 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001448 tmp.c_lflag &= ~(ICANON | ECHO);
1449 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001450 termios_set = 1;
1451 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001452#if HAVE_MINIX2
1453 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001454 check_root2();
1455 check2();
1456 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001457#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001458 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001459 check_root();
1460 check();
1461 }
1462 if (verbose) {
1463 int i, free;
1464
Erik Andersene49d5ec2000-02-08 19:58:47 +00001465 for (i = 1, free = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001466 if (!inode_in_use(i))
1467 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001468 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1469 100 * (INODES - free) / INODES);
1470 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001471 if (!zone_in_use(i))
1472 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001473 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1474 100 * (ZONES - free) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001475 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001476 "%6d directories\n"
1477 "%6d character device files\n"
1478 "%6d block device files\n"
1479 "%6d links\n"
1480 "%6d symbolic links\n"
1481 "------\n"
1482 "%6d files\n",
1483 regular, directory, chardev, blockdev,
1484 links - 2 * directory + 1, symlinks,
1485 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001486 }
1487 if (changed) {
1488 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001489 printf("----------------------------\n"
1490 "FILE SYSTEM HAS BEEN CHANGED\n"
1491 "----------------------------\n");
1492 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001493 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001494 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001495 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001496
Eric Andersene494fdd1999-10-19 20:23:03 +00001497 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001498 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001499
1500 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001501 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001502 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001503 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001504 return retcode;
1505}