blob: a9c4467d8463a7b11560400ec602925a25ebdcff [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersene494fdd1999-10-19 20:23:03 +00002/*
3 * fsck.c - a file system consistency checker for Linux.
4 *
5 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6 * as per the GNU copyleft.
7 */
8
9/*
Eric Andersenaff114c2004-04-14 17:51:38 +000010 * 09.11.91 - made the first rudimentary functions
Eric Andersene494fdd1999-10-19 20:23:03 +000011 *
12 * 10.11.91 - updated, does checking, no repairs yet.
13 * Sent out to the mailing-list for testing.
14 *
15 * 14.11.91 - Testing seems to have gone well. Added some
16 * correction-code, and changed some functions.
17 *
18 * 15.11.91 - More correction code. Hopefully it notices most
19 * cases now, and tries to do something about them.
20 *
21 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
22 * things seem to work now. Yeah, sure.
23 *
24 *
25 * 19.04.92 - Had to start over again from this old version, as a
26 * kernel bug ate my enhanced fsck in february.
27 *
28 * 28.02.93 - added support for different directory entry sizes..
29 *
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31 * super-block information
32 *
33 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34 * to that required by fsutil
35 *
36 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
37 * Added support for file system valid flag. Also
38 * added program_version variable and output of
39 * program name and version number when program
40 * is executed.
41 *
42 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
44 *
45 * 10.12.94 - added test to prevent checking of mounted fs adapted
46 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
47 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
48 *
49 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
50 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
51 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000052 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
Eric Andersene494fdd1999-10-19 20:23:03 +000053 * (Russell King). He made them for ARM. It would seem
54 * that the ARM is powerful enough to do this in C whereas
55 * i386 and m64k must use assembly to get it fast >:-)
Eric Andersenaff114c2004-04-14 17:51:38 +000056 * This should make minix fsck system-independent.
Eric Andersene494fdd1999-10-19 20:23:03 +000057 * (janl@math.uio.no, Nicolai Langfeldt)
58 *
59 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
Eric Andersenc7bda1c2004-03-15 08:29:22 +000060 * warnings. Added mc68k bitops from
Eric Andersene494fdd1999-10-19 20:23:03 +000061 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
62 *
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
64 * Andreas Schwab.
65 *
66 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67 * - added Native Language Support
68 *
69 *
70 * I've had no time to add comments - hopefully the function names
71 * are comments enough. As with all file system checkers, this assumes
72 * the file system is quiescent - don't use it on a mounted device
73 * unless you can be sure nobody is writing to it (and remember that the
74 * kernel can write to it when it searches for files).
75 *
Eric Andersenaff114c2004-04-14 17:51:38 +000076 * Usage: fsck [-larvsm] device
Eric Andersene494fdd1999-10-19 20:23:03 +000077 * -l for a listing of all the filenames
78 * -a for automatic repairs (not implemented)
79 * -r for repairs (interactive) (not implemented)
80 * -v for verbose (tells how many files)
81 * -s for super-block info
82 * -m for minix-like "mode not cleared" warnings
83 * -f force filesystem check even if filesystem marked as valid
84 *
85 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086 * enforced (but it's not much fun on a character device :-).
Eric Andersene494fdd1999-10-19 20:23:03 +000087 */
88
89#include <stdio.h>
90#include <errno.h>
91#include <unistd.h>
92#include <string.h>
93#include <fcntl.h>
94#include <ctype.h>
95#include <stdlib.h>
96#include <termios.h>
97#include <mntent.h>
Erik Andersen8ea7d8c2000-05-20 00:40:08 +000098#include <sys/param.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000099#include "busybox.h"
Eric Andersen85c55202000-07-10 22:46:55 +0000100
Rob Landleybc68cd12006-03-10 19:22:06 +0000101#define BLOCK_SIZE_BITS 10
102#define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
Eric Andersen85c55202000-07-10 22:46:55 +0000103
104/*
105 * This is the original minix inode layout on disk.
106 * Note the 8-bit gid and atime and ctime.
107 */
108struct minix_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000109 uint16_t i_mode;
110 uint16_t i_uid;
111 uint32_t i_size;
112 uint32_t i_time;
113 uint8_t i_gid;
114 uint8_t i_nlinks;
115 uint16_t i_zone[9];
Eric Andersen85c55202000-07-10 22:46:55 +0000116};
117
118/*
119 * The new minix inode has all the time entries, as well as
120 * long block numbers and a third indirect block (7+1+1+1
121 * instead of 7+1+1). Also, some previously 8-bit values are
122 * now 16-bit. The inode is now 64 bytes instead of 32.
123 */
124struct minix2_inode {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000125 uint16_t i_mode;
126 uint16_t i_nlinks;
127 uint16_t i_uid;
128 uint16_t i_gid;
129 uint32_t i_size;
130 uint32_t i_atime;
131 uint32_t i_mtime;
132 uint32_t i_ctime;
133 uint32_t i_zone[10];
Eric Andersen85c55202000-07-10 22:46:55 +0000134};
135
Rob Landleybc68cd12006-03-10 19:22:06 +0000136enum {
137 MINIX_ROOT_INO = 1,
138 MINIX_LINK_MAX = 250,
139 MINIX2_LINK_MAX = 65530,
140
141 MINIX_I_MAP_SLOTS = 8,
142 MINIX_Z_MAP_SLOTS = 64,
143 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
144 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
145 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
146 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
147 MINIX_VALID_FS = 0x0001, /* Clean fs. */
148 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
149
150 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
151 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
152
153 MINIX_V1 = 0x0001, /* original minix fs */
154 MINIX_V2 = 0x0002 /* minix V2 fs */
155};
156
157#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
158
Eric Andersen85c55202000-07-10 22:46:55 +0000159/*
160 * minix super-block data on disk
161 */
162struct minix_super_block {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000163 uint16_t s_ninodes;
164 uint16_t s_nzones;
165 uint16_t s_imap_blocks;
166 uint16_t s_zmap_blocks;
167 uint16_t s_firstdatazone;
168 uint16_t s_log_zone_size;
169 uint32_t s_max_size;
170 uint16_t s_magic;
171 uint16_t s_state;
172 uint32_t s_zones;
Eric Andersen85c55202000-07-10 22:46:55 +0000173};
174
175struct minix_dir_entry {
Eric Andersendfcb5b02004-01-30 22:54:20 +0000176 uint16_t inode;
Eric Andersen85c55202000-07-10 22:46:55 +0000177 char name[0];
178};
179
Eric Andersen85c55202000-07-10 22:46:55 +0000180
181#define NAME_MAX 255 /* # chars in a file name */
182
183#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
184
Eric Andersen85c55202000-07-10 22:46:55 +0000185#ifndef BLKGETSIZE
186#define BLKGETSIZE _IO(0x12,96) /* return device size */
187#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000188
Eric Andersene494fdd1999-10-19 20:23:03 +0000189#ifndef __linux__
190#define volatile
191#endif
192
Rob Landleybc68cd12006-03-10 19:22:06 +0000193enum { ROOT_INO = 1 };
Eric Andersene494fdd1999-10-19 20:23:03 +0000194
195#define UPPER(size,n) ((size+((n)-1))/(n))
196#define INODE_SIZE (sizeof(struct minix_inode))
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000198#define INODE_SIZE2 (sizeof(struct minix2_inode))
199#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
200 : MINIX_INODES_PER_BLOCK))
201#else
202#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
203#endif
204#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
205
206#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
207
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208static char *program_version = "1.2 - 11/11/96";
Rob Landley8a625492006-04-29 20:05:17 +0000209static char *device_name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000210static int IN;
Rob Landley8a625492006-04-29 20:05:17 +0000211static int repair, automatic, verbose, list, show, warn_mode, force;
212static int directory, regular, blockdev, chardev, links, symlinks, total;
Eric Andersene494fdd1999-10-19 20:23:03 +0000213
Rob Landley8a625492006-04-29 20:05:17 +0000214static int changed; /* flags if the filesystem has been changed */
215static int errors_uncorrected; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000216static int dirsize = 16;
217static int namelen = 14;
Eric Andersene494fdd1999-10-19 20:23:03 +0000218static struct termios termios;
Rob Landley8a625492006-04-29 20:05:17 +0000219static int termios_set;
Eric Andersene494fdd1999-10-19 20:23:03 +0000220
Rob Landleydb2ab892006-05-03 21:39:28 +0000221static char *inode_buffer;
Eric Andersene494fdd1999-10-19 20:23:03 +0000222#define Inode (((struct minix_inode *) inode_buffer)-1)
223#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
224static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225
Eric Andersene494fdd1999-10-19 20:23:03 +0000226#define Super (*(struct minix_super_block *)super_block_buffer)
227#define INODES ((unsigned long)Super.s_ninodes)
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000228#ifdef CONFIG_FEATURE_MINIX2
Rob Landleyc44ab022006-06-06 19:50:16 +0000229static int version2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000230#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
231#else
232#define ZONES ((unsigned long)(Super.s_nzones))
233#endif
234#define IMAPS ((unsigned long)Super.s_imap_blocks)
235#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
236#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
237#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
238#define MAXSIZE ((unsigned long)Super.s_max_size)
239#define MAGIC (Super.s_magic)
240#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
241
242static char *inode_map;
243static char *zone_map;
244
Rob Landley8a625492006-04-29 20:05:17 +0000245static unsigned char *inode_count;
246static unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000247
Eric Andersene674eb71999-10-19 20:52:57 +0000248static void recursive_check(unsigned int ino);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000249#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene674eb71999-10-19 20:52:57 +0000250static void recursive_check2(unsigned int ino);
Pavel Roskinbc0aed72000-07-17 17:52:00 +0000251#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000252
Rob Landleyfd8409f2006-06-13 15:31:39 +0000253static inline int bit(char *a, unsigned int i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000254{
255 return (a[i >> 3] & (1<<(i & 7))) != 0;
256}
257#define inode_in_use(x) (bit(inode_map,(x)))
258#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000259
260#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
261#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
262
263#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
264#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
265
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +0000266static void leave(int) ATTRIBUTE_NORETURN;
Eric Andersene674eb71999-10-19 20:52:57 +0000267static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000268{
269 if (termios_set)
270 tcsetattr(0, TCSANOW, &termios);
271 exit(status);
272}
273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274static void die(const char *str)
275{
Manuel Novoa III cad53642003-03-19 09:13:01 +0000276 bb_error_msg("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000277 leave(8);
278}
279
Rob Landley3ea66a82006-04-29 20:08:40 +0000280/* File-name data */
281enum { MAX_DEPTH = 32 };
282static int name_depth;
283static char *current_name;
284static char *name_component[MAX_DEPTH+1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000285
Rob Landley3ea66a82006-04-29 20:08:40 +0000286/* Wed Feb 9 15:17:06 MST 2000 */
287/* dynamically allocate name_list (instead of making it static) */
288static inline void alloc_current_name(void)
289{
290 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
291 current_name[0] = '/';
292 current_name[1] = '\0';
293 name_component[0] = &current_name[0];
294}
295
296#ifdef CONFIG_FEATURE_CLEAN_UP
297/* execute this atexit() to deallocate name_list[] */
298/* piptigger was here */
299static inline void free_current_name(void)
300{
301 free(current_name);
302}
303#endif
304
305static void push_filename(const char *name)
306{
307 // /dir/dir/dir/file
308 // ^ ^ ^
309 // [0] [1] [2] <-name_component[i]
310 if (name_depth < MAX_DEPTH) {
311 int len;
312 char *p = name_component[name_depth];
313 *p++ = '/';
314 len = sprintf(p, "%.*s", namelen, name);
315 name_component[name_depth + 1] = p + len;
316 }
317 name_depth++;
318}
319
320static void pop_filename(void) {
321 name_depth--;
322 if (name_depth < MAX_DEPTH) {
323 *name_component[name_depth] = '\0';
324 if (!name_depth) {
325 current_name[0] = '/';
326 current_name[1] = '\0';
327 }
328 }
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
Rob Landleyfd8409f2006-06-13 15:31:39 +0000405 printf("%s is mounted. ", device_name);
Rob Landley8a625492006-04-29 20:05:17 +0000406 cont = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000407 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000408 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000409 if (!cont) {
Rob Landley8a625492006-04-29 20:05:17 +0000410 printf("Check aborted\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000412 }
413 return;
414}
415
416/*
417 * check_zone_nr checks to see that *nr is a valid zone nr. If it
418 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
419 * if an error was corrected, and returns the zone (0 for no zone
420 * or a bad zone-number).
421 */
Rob Landley8a625492006-04-29 20:05:17 +0000422static int check_zone_nr2(uint32_t *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000423{
Rob Landley3ea66a82006-04-29 20:08:40 +0000424 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000425 if (!*nr)
426 return 0;
427 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000428 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000429 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000430 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000431 else
432 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000433 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000435 *nr = 0;
436 *corrected = 1;
437 }
438 return 0;
439}
440
Rob Landley8a625492006-04-29 20:05:17 +0000441static int check_zone_nr(uint16_t *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000442{
Rob Landley8a625492006-04-29 20:05:17 +0000443 uint32_t nr32 = *nr;
444 int r = check_zone_nr2(&nr32, corrected);
445 *nr = (uint16_t)nr32;
446 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000447}
Eric Andersene494fdd1999-10-19 20:23:03 +0000448
449/*
450 * read-block reads block nr into the buffer at addr.
451 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000453{
454 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000456 return;
457 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000459 printf("%s: unable to seek to block in file '%s'\n",
460 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000461 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000462 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000463 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000464 printf("%s: bad block in file '%s'\n",
465 bb_msg_read_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000466 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000467 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000468 }
469}
470
471/*
472 * write_block writes block nr to disk.
473 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000475{
476 if (!nr)
477 return;
478 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000479 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000480 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000481 errors_uncorrected = 1;
482 return;
483 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000484 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000485 die("Seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000486 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000487 printf("%s: bad block in file '%s'\n",
488 bb_msg_write_error, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000489 errors_uncorrected = 1;
490 }
491}
492
493/*
Rob Landley8a625492006-04-29 20:05:17 +0000494 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000495 * It sets 'changed' if the inode has needed changing, and re-writes
496 * any indirect blocks with errors.
497 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000499{
Rob Landley8a625492006-04-29 20:05:17 +0000500 uint16_t ind[BLOCK_SIZE >> 1];
501 uint16_t dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 int blk_chg, block, result;
503
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 return check_zone_nr(inode->i_zone + blknr, &changed);
506 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000508 block = check_zone_nr(inode->i_zone + 7, &changed);
509 read_block(block, (char *) ind);
510 blk_chg = 0;
511 result = check_zone_nr(blknr + ind, &blk_chg);
512 if (blk_chg)
513 write_block(block, (char *) ind);
514 return result;
515 }
516 blknr -= 512;
517 block = check_zone_nr(inode->i_zone + 8, &changed);
518 read_block(block, (char *) dind);
519 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000520 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 if (blk_chg)
522 write_block(block, (char *) dind);
523 block = result;
524 read_block(block, (char *) ind);
525 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000527 if (blk_chg)
528 write_block(block, (char *) ind);
529 return result;
530}
531
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000532#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000534{
Rob Landley8a625492006-04-29 20:05:17 +0000535 uint32_t ind[BLOCK_SIZE >> 2];
536 uint32_t dind[BLOCK_SIZE >> 2];
537 uint32_t tind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000538 int blk_chg, block, result;
539
540 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000542 blknr -= 7;
543 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 block = check_zone_nr2(inode->i_zone + 7, &changed);
545 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000546 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000547 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000550 return result;
551 }
552 blknr -= 256;
553 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000554 block = check_zone_nr2(inode->i_zone + 8, &changed);
555 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000556 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000557 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000558 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000559 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000560 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000561 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000562 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000563 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000565 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 return result;
567 }
568 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 block = check_zone_nr2(inode->i_zone + 9, &changed);
570 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000571 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000576 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000577 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000578 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000579 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000580 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000581 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000583 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000584 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000585 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000586 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000587 return result;
588}
589#endif
590
Eric Andersene674eb71999-10-19 20:52:57 +0000591static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000592{
593 /*
594 * Set the state of the filesystem based on whether or not there
595 * are uncorrected errors. The filesystem valid flag is
596 * unconditionally set if we get this far.
597 */
598 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000599 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 Super.s_state |= MINIX_ERROR_FS;
601 else
602 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000603
Eric Andersene494fdd1999-10-19 20:23:03 +0000604 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000605 die("Seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000606 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000607 die("Unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000608}
609
Eric Andersene674eb71999-10-19 20:52:57 +0000610static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000611{
612 write_super_block();
613
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000615 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000617 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000619 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000620}
621
Erik Andersene49d5ec2000-02-08 19:58:47 +0000622static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000623{
624 int block;
625 char blk[BLOCK_SIZE];
626 int size;
627
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000628#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000629 if (version2)
630 block = Inode2[ROOT_INO].i_zone[0];
631 else
632#endif
633 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000634 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000635 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000637 dirsize = size;
638 namelen = size - 2;
639 return;
640 }
641 }
642 /* use defaults */
643}
644
Eric Andersene674eb71999-10-19 20:52:57 +0000645static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000646{
647 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000648 die("Seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000649 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000650 die("Unable to read super block");
Rob Landley3ea66a82006-04-29 20:08:40 +0000651 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000652 namelen = 14;
653 dirsize = 16;
654 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000655 */
Eric Andersene494fdd1999-10-19 20:23:03 +0000656 if (MAGIC == MINIX_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000657 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
658 namelen = 30;
659 dirsize = 32;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000660#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000661 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000662 version2 = 1;
663 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
664 namelen = 30;
665 dirsize = 32;
666 version2 = 1;
667#endif
668 } else
Rob Landleyfd8409f2006-06-13 15:31:39 +0000669 die("Bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000670 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000671 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000672 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Rob Landleyfd8409f2006-06-13 15:31:39 +0000673 die("Bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000674 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Rob Landleyfd8409f2006-06-13 15:31:39 +0000675 die("Bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000676}
677
Eric Andersene674eb71999-10-19 20:52:57 +0000678static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000679{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000680 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
681 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000682 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
683 inode_count = xmalloc(INODES + 1);
684 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000685 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000686 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000687 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000688 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000689 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000690 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000691 if (NORM_FIRSTZONE != FIRSTZONE) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000692 printf("Warning: Firstzone!=Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000693 errors_uncorrected = 1;
694 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000695 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000696 if (show) {
Rob Landley8a625492006-04-29 20:05:17 +0000697 printf("%ld inodes\n"
698 "%ld blocks\n"
699 "Firstdatazone=%ld (%ld)\n"
700 "Zonesize=%d\n"
701 "Maxsize=%ld\n"
702 "Filesystem state=%d\n"
703 "namelen=%d\n\n",
704 INODES,
705 ZONES,
706 FIRSTZONE, NORM_FIRSTZONE,
707 BLOCK_SIZE << ZONESIZE,
708 MAXSIZE,
709 Super.s_state,
710 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000711 }
712}
713
Eric Andersen3e6ff902001-03-09 21:24:12 +0000714static struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000715{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000716 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000717
718 if (!nr || nr > INODES)
719 return NULL;
720 total++;
721 inode = Inode + nr;
722 if (!inode_count[nr]) {
723 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000724 printf("Inode %d is marked as 'unused', but it is used "
725 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000726 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000727 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000728 mark_inode(nr);
729 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000730 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000731 }
732 }
733 if (S_ISDIR(inode->i_mode))
734 directory++;
735 else if (S_ISREG(inode->i_mode))
736 regular++;
737 else if (S_ISCHR(inode->i_mode))
738 chardev++;
739 else if (S_ISBLK(inode->i_mode))
740 blockdev++;
741 else if (S_ISLNK(inode->i_mode))
742 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000743 else if (S_ISSOCK(inode->i_mode));
744 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000745 else {
Rob Landley3ea66a82006-04-29 20:08:40 +0000746 printf("%s has mode %05o\n", current_name, inode->i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000747 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000748
749 } else
750 links++;
751 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000752 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000753 inode_count[nr]--;
754 errors_uncorrected = 1;
755 }
756 return inode;
757}
758
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000759#ifdef CONFIG_FEATURE_MINIX2
Eric Andersen3e6ff902001-03-09 21:24:12 +0000760static struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000761{
762 struct minix2_inode *inode;
763
764 if (!nr || nr > INODES)
765 return NULL;
766 total++;
767 inode = Inode2 + nr;
768 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769 if (!inode_in_use(nr)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000770 printf("Inode %d is marked as 'unused', but it is used "
771 "for file '%s'\n", nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 if (repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000773 if (ask("Mark as 'in use'", 1))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 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 {
Rob Landley3ea66a82006-04-29 20:08:40 +0000792 printf("%s has mode %05o\n", current_name, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000793 }
794 } else
795 links++;
796 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000797 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000798 inode_count[nr]--;
799 errors_uncorrected = 1;
800 }
801 return inode;
802}
803#endif
804
Eric Andersene674eb71999-10-19 20:52:57 +0000805static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000806{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000807 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000808
809 if (!inode || !S_ISDIR(inode->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000810 die("Root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000811}
812
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000813#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000814static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000815{
816 struct minix2_inode *inode = Inode2 + ROOT_INO;
817
Erik Andersene49d5ec2000-02-08 19:58:47 +0000818 if (!inode || !S_ISDIR(inode->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +0000819 die("Root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000820}
821#endif
822
Rob Landley8a625492006-04-29 20:05:17 +0000823static int add_zone(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000824{
825 int result;
826 int block;
827
828 result = 0;
829 block = check_zone_nr(znr, corrected);
830 if (!block)
831 return 0;
832 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000833 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000834 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000835 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000836 *znr = 0;
837 block = 0;
838 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000839 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000840 }
841 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000842 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000843 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000844 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000846 mark_zone(block);
847 }
848 if (!++zone_count[block])
849 zone_count[block]--;
850 return block;
851}
852
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000853#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000854static int add_zone2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000855{
856 int result;
857 int block;
858
859 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000860 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000861 if (!block)
862 return 0;
863 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000864 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000865 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000866 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000867 *znr = 0;
868 block = 0;
869 *corrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000870 return 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000871 }
872 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000873 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000874 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000875 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000876 if (ask("Correct", 1))
877 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000878 }
879 if (!++zone_count[block])
880 zone_count[block]--;
881 return block;
882}
883#endif
884
Rob Landley8a625492006-04-29 20:05:17 +0000885static void add_zone_ind(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000886{
887 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000888 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000889 int block;
890
891 block = add_zone(znr, corrected);
892 if (!block)
893 return;
894 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000895 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000896 add_zone(i + (uint16_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000897 if (chg_blk)
898 write_block(block, blk);
899}
900
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000901#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000902static void add_zone_ind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000903{
904 static char blk[BLOCK_SIZE];
905 int i, chg_blk = 0;
906 int block;
907
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 if (!block)
910 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000912 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000913 add_zone2(i + (uint32_t *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000914 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000915 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000916}
917#endif
918
Rob Landley8a625492006-04-29 20:05:17 +0000919static void add_zone_dind(uint16_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000920{
921 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000923 int block;
924
925 block = add_zone(znr, corrected);
926 if (!block)
927 return;
928 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000929 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Rob Landley8a625492006-04-29 20:05:17 +0000930 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000931 if (blk_chg)
932 write_block(block, blk);
933}
934
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000935#ifdef CONFIG_FEATURE_MINIX2
Rob Landley8a625492006-04-29 20:05:17 +0000936static void add_zone_dind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000937{
938 static char blk[BLOCK_SIZE];
939 int i, blk_chg = 0;
940 int block;
941
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000943 if (!block)
944 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000945 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000946 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000947 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000948 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000949 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000950}
951
Rob Landley8a625492006-04-29 20:05:17 +0000952static void add_zone_tind2(uint32_t *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000953{
954 static char blk[BLOCK_SIZE];
955 int i, blk_chg = 0;
956 int block;
957
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000959 if (!block)
960 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000961 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000962 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Rob Landley8a625492006-04-29 20:05:17 +0000963 add_zone_dind2(i + (uint32_t *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000964 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000966}
967#endif
968
Eric Andersene674eb71999-10-19 20:52:57 +0000969static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000970{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000972
973 if (!i || i > INODES)
974 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000976 return;
977 inode = Inode + i;
978 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979 !S_ISLNK(inode->i_mode)) return;
980 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000981 add_zone(i + inode->i_zone, &changed);
982 add_zone_ind(7 + inode->i_zone, &changed);
983 add_zone_dind(8 + inode->i_zone, &changed);
984}
985
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000986#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000987static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000988{
989 struct minix2_inode *inode;
990
991 if (!i || i > INODES)
992 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000993 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000994 return;
995 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000996 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
997 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000998 return;
999 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001000 add_zone2(i + inode->i_zone, &changed);
1001 add_zone_ind2(7 + inode->i_zone, &changed);
1002 add_zone_dind2(8 + inode->i_zone, &changed);
1003 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +00001004}
1005#endif
1006
Erik Andersene49d5ec2000-02-08 19:58:47 +00001007static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001008{
1009 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001010 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +00001011 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +00001013 int block;
1014
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +00001016 read_block(block, blk);
1017 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001018 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001019 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001020 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001021 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001022 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001023 *(uint16_t *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001024 write_block(block, blk);
1025 }
1026 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001028 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001029 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001030 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001031 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 if (!inode || strcmp(".", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001033 printf("%s: bad directory: '.' isn't first\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001034 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001035 } else
1036 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001037 }
1038 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001039 if (!inode || strcmp("..", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001040 printf("%s: bad directory: '..' isn't second\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001041 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001042 } else
1043 return;
Eric Andersene494fdd1999-10-19 20:23:03 +00001044 }
1045 if (!inode)
1046 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001047 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001048 if (list) {
1049 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001050 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001051 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 }
1053 check_zones(ino);
1054 if (inode && S_ISDIR(inode->i_mode))
1055 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001056 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001057 return;
1058}
1059
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001060#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001061static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001062{
1063 static char blk[BLOCK_SIZE];
1064 struct minix2_inode *inode;
1065 int ino;
1066 char *name;
1067 int block;
1068
Erik Andersene49d5ec2000-02-08 19:58:47 +00001069 block = map_block2(dir, offset / BLOCK_SIZE);
1070 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001071 name = blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001072 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001073 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001074 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001075 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001076 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001077 *(uint16_t *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001078 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001079 }
1080 ino = 0;
1081 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001082 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001083 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001084 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001085 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001086 if (!inode || strcmp(".", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001087 printf("%s: bad directory: '.' isn't first\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001088 errors_uncorrected = 1;
1089 } else
1090 return;
1091 }
1092 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 if (!inode || strcmp("..", name)) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001094 printf("%s: bad directory: '..' isn't second\n", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001095 errors_uncorrected = 1;
1096 } else
1097 return;
1098 }
1099 if (!inode)
1100 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001101 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001102 if (list) {
1103 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001105 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001106 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 check_zones2(ino);
1108 if (inode && S_ISDIR(inode->i_mode))
1109 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001110 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001111 return;
1112}
1113#endif
1114
Eric Andersene674eb71999-10-19 20:52:57 +00001115static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001116{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001118 unsigned int offset;
1119
1120 dir = Inode + ino;
1121 if (!S_ISDIR(dir->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001122 die("Internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001124 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001125 errors_uncorrected = 1;
1126 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 for (offset = 0; offset < dir->i_size; offset += dirsize)
1128 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001129}
1130
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001131#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001133{
1134 struct minix2_inode *dir;
1135 unsigned int offset;
1136
1137 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001138 if (!S_ISDIR(dir->i_mode))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001139 die("Internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001140 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001141 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001142 errors_uncorrected = 1;
1143 }
1144 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001146}
1147#endif
1148
Eric Andersene674eb71999-10-19 20:52:57 +00001149static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001150{
1151 char buffer[1024];
1152
Erik Andersene49d5ec2000-02-08 19:58:47 +00001153 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001154 die("Seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001155 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1156}
1157
Eric Andersene674eb71999-10-19 20:52:57 +00001158static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001159{
1160 int i;
1161
Erik Andersene49d5ec2000-02-08 19:58:47 +00001162 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001163 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001164 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001165 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001166 Inode[i].i_mode = 0;
1167 changed = 1;
1168 }
1169 }
1170 if (!inode_count[i]) {
1171 if (!inode_in_use(i))
1172 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001173 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001175 unmark_inode(i);
1176 continue;
1177 }
1178 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001179 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001180 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001181 mark_inode(i);
1182 }
1183 if (Inode[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001184 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001185 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1186 if (ask("Set i_nlinks to count", 1)) {
1187 Inode[i].i_nlinks = inode_count[i];
1188 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001189 }
1190 }
1191 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001193 if (zone_in_use(i) == zone_count[i])
1194 continue;
1195 if (!zone_count[i]) {
1196 if (bad_zone(i))
1197 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001198 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001200 unmark_zone(i);
1201 continue;
1202 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001203 printf("Zone %d: %sin use, counted=%d\n",
1204 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001205 }
1206}
1207
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001208#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001210{
1211 int i;
1212
1213 for (i = 1; i <= INODES; i++) {
Rob Landley8a625492006-04-29 20:05:17 +00001214 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001215 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001217 Inode2[i].i_mode = 0;
1218 changed = 1;
1219 }
1220 }
1221 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001222 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001223 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001224 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001225 if (ask("Clear", 1))
1226 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001227 continue;
1228 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001229 if (!inode_in_use(i)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001230 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001231 if (ask("Set", 1))
1232 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001233 }
1234 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001235 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001236 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1237 inode_count[i]);
1238 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001239 Inode2[i].i_nlinks = inode_count[i];
1240 changed = 1;
1241 }
1242 }
1243 }
1244 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001246 continue;
1247 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001248 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001249 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001250 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001251 if (ask("Unmark", 1))
1252 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001253 continue;
1254 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001255 printf("Zone %d: %sin use, counted=%d\n",
1256 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001257 }
1258}
1259#endif
1260
Eric Andersene674eb71999-10-19 20:52:57 +00001261static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001262{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001263 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1264 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001265 check_zones(ROOT_INO);
1266 recursive_check(ROOT_INO);
1267 check_counts();
1268}
1269
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001270#ifdef CONFIG_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001271static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001272{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001273 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1274 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1275 check_zones2(ROOT_INO);
1276 recursive_check2(ROOT_INO);
1277 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001278}
1279#endif
1280
Rob Landleydfba7412006-03-06 20:47:33 +00001281int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001282{
1283 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001284 int retcode = 0;
1285
Rob Landley3ea66a82006-04-29 20:08:40 +00001286 alloc_current_name();
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001287#ifdef CONFIG_FEATURE_CLEAN_UP
Erik Andersen298854f2000-03-23 01:09:18 +00001288 /* Don't bother to free memory. Exit does
1289 * that automagically, so we can save a few bytes */
Rob Landley3ea66a82006-04-29 20:08:40 +00001290 atexit(free_current_name);
Eric Andersenb040d4f2000-07-25 18:01:20 +00001291#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001292
Eric Andersene494fdd1999-10-19 20:23:03 +00001293 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001294 die("Bad inode size");
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001295#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001296 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001297 die("Bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001298#endif
1299 while (argc-- > 1) {
1300 argv++;
1301 if (argv[0][0] != '-') {
1302 if (device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001303 bb_show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001304 else
1305 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001306 } else
1307 while (*++argv[0])
1308 switch (argv[0][0]) {
1309 case 'l':
1310 list = 1;
1311 break;
1312 case 'a':
1313 automatic = 1;
1314 repair = 1;
1315 break;
1316 case 'r':
1317 automatic = 0;
1318 repair = 1;
1319 break;
1320 case 'v':
1321 verbose = 1;
1322 break;
1323 case 's':
1324 show = 1;
1325 break;
1326 case 'm':
1327 warn_mode = 1;
1328 break;
1329 case 'f':
1330 force = 1;
1331 break;
1332 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +00001333 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001334 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001335 }
1336 if (!device_name)
Manuel Novoa III cad53642003-03-19 09:13:01 +00001337 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001338 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001339 if (repair && !automatic) {
1340 if (!isatty(0) || !isatty(1))
Rob Landleyfd8409f2006-06-13 15:31:39 +00001341 die("Need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001342 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001343 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersen77508b22000-12-12 23:51:43 +00001344 if (IN < 0){
Rob Landleyfd8409f2006-06-13 15:31:39 +00001345 printf("Unable to open device '%s'\n", device_name);
Eric Andersen77508b22000-12-12 23:51:43 +00001346 leave(8);
1347 }
Rob Landley8a625492006-04-29 20:05:17 +00001348 sync(); /* paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001349 read_superblock();
1350
1351 /*
1352 * Determine whether or not we should continue with the checking.
1353 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001354 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001355 * command line.
1356 */
Manuel Novoa III cad53642003-03-19 09:13:01 +00001357 printf("%s, %s\n", bb_applet_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001358 if (!(Super.s_state & MINIX_ERROR_FS) &&
1359 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001360 if (repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001361 printf("%s is clean, check is skipped\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001362 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001363 } else if (force)
Rob Landley8a625492006-04-29 20:05:17 +00001364 printf("Forcing filesystem check on %s\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001365 else if (repair)
Rob Landley8a625492006-04-29 20:05:17 +00001366 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001367 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001368
1369 read_tables();
1370
1371 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001372 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001373 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001374 tmp.c_lflag &= ~(ICANON | ECHO);
1375 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001376 termios_set = 1;
1377 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001378#ifdef CONFIG_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +00001379 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001380 check_root2();
1381 check2();
1382 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001383#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001384 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001385 check_root();
1386 check();
1387 }
1388 if (verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001389 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001390
Eric Andersen1ca20a72001-03-21 07:34:27 +00001391 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001392 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001393 free_cnt++;
1394 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1395 100 * (INODES - free_cnt) / INODES);
1396 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001397 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001398 free_cnt++;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001399 printf("%6ld zones used (%ld%%)\n\n"
1400 "%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001401 "%6d directories\n"
1402 "%6d character device files\n"
1403 "%6d block device files\n"
1404 "%6d links\n"
1405 "%6d symbolic links\n"
1406 "------\n"
1407 "%6d files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001408 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001409 regular, directory, chardev, blockdev,
1410 links - 2 * directory + 1, symlinks,
1411 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001412 }
1413 if (changed) {
1414 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001415 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001416 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001417 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001418 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001419
Eric Andersene494fdd1999-10-19 20:23:03 +00001420 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001421 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001422
1423 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001424 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001425 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001426 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001427 return retcode;
1428}