blob: e02e05fa47bdcbb41e9115c2eafa77908fb8f00f [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 *
Rob Landleycc9962d2006-09-14 05:27:28 +00005 * (C) 1991, 1992 Linus Torvalds.
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersene494fdd1999-10-19 20:23:03 +00008 */
9
10/*
Eric Andersenaff114c2004-04-14 17:51:38 +000011 * 09.11.91 - made the first rudimentary functions
Eric Andersene494fdd1999-10-19 20:23:03 +000012 *
13 * 10.11.91 - updated, does checking, no repairs yet.
14 * Sent out to the mailing-list for testing.
15 *
16 * 14.11.91 - Testing seems to have gone well. Added some
17 * correction-code, and changed some functions.
18 *
19 * 15.11.91 - More correction code. Hopefully it notices most
20 * cases now, and tries to do something about them.
21 *
22 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
23 * things seem to work now. Yeah, sure.
24 *
25 *
26 * 19.04.92 - Had to start over again from this old version, as a
27 * kernel bug ate my enhanced fsck in february.
28 *
29 * 28.02.93 - added support for different directory entry sizes..
30 *
31 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +000032 * superblock information
Eric Andersene494fdd1999-10-19 20:23:03 +000033 *
34 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
35 * to that required by fsutil
36 *
37 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
38 * Added support for file system valid flag. Also
39 * added program_version variable and output of
40 * program name and version number when program
41 * is executed.
42 *
43 * 30.10.94 - added support for v2 filesystem
44 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
45 *
46 * 10.12.94 - added test to prevent checking of mounted fs adapted
47 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
48 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
49 *
50 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
51 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
52 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000053 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
Eric Andersene494fdd1999-10-19 20:23:03 +000054 * (Russell King). He made them for ARM. It would seem
55 * that the ARM is powerful enough to do this in C whereas
56 * i386 and m64k must use assembly to get it fast >:-)
Eric Andersenaff114c2004-04-14 17:51:38 +000057 * This should make minix fsck system-independent.
Eric Andersene494fdd1999-10-19 20:23:03 +000058 * (janl@math.uio.no, Nicolai Langfeldt)
59 *
60 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061 * warnings. Added mc68k bitops from
Eric Andersene494fdd1999-10-19 20:23:03 +000062 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
63 *
64 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
65 * Andreas Schwab.
66 *
Denis Vlasenkob44c7902008-03-17 09:29:43 +000067 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
Eric Andersene494fdd1999-10-19 20:23:03 +000068 * - added Native Language Support
69 *
70 *
71 * I've had no time to add comments - hopefully the function names
72 * are comments enough. As with all file system checkers, this assumes
73 * the file system is quiescent - don't use it on a mounted device
74 * unless you can be sure nobody is writing to it (and remember that the
75 * kernel can write to it when it searches for files).
76 *
Eric Andersenaff114c2004-04-14 17:51:38 +000077 * Usage: fsck [-larvsm] device
Eric Andersene494fdd1999-10-19 20:23:03 +000078 * -l for a listing of all the filenames
79 * -a for automatic repairs (not implemented)
80 * -r for repairs (interactive) (not implemented)
81 * -v for verbose (tells how many files)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +000082 * -s for superblock info
Eric Andersene494fdd1999-10-19 20:23:03 +000083 * -m for minix-like "mode not cleared" warnings
84 * -f force filesystem check even if filesystem marked as valid
85 *
86 * The device may be a block device or a image of one, but this isn't
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087 * enforced (but it's not much fun on a character device :-).
Eric Andersene494fdd1999-10-19 20:23:03 +000088 */
89
Rob Landleycc9962d2006-09-14 05:27:28 +000090#include <mntent.h>
Denis Vlasenkoa6691312007-06-13 12:28:21 +000091#include "libbb.h"
Denis Vlasenkoaa959592007-01-03 00:43:19 +000092#include "minix.h"
Eric Andersen85c55202000-07-10 22:46:55 +000093
Eric Andersen85c55202000-07-10 22:46:55 +000094#ifndef BLKGETSIZE
95#define BLKGETSIZE _IO(0x12,96) /* return device size */
96#endif
Eric Andersene494fdd1999-10-19 20:23:03 +000097
Denis Vlasenko9f153f62008-03-17 08:32:26 +000098struct BUG_bad_inode_size {
99 char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
100#if ENABLE_FEATURE_MINIX2
101 char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
102#endif
103};
104
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000105enum {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000106#ifdef UNUSED
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000107 MINIX1_LINK_MAX = 250,
108 MINIX2_LINK_MAX = 65530,
109 MINIX_I_MAP_SLOTS = 8,
110 MINIX_Z_MAP_SLOTS = 64,
111 MINIX_V1 = 0x0001, /* original minix fs */
112 MINIX_V2 = 0x0002, /* minix V2 fs */
Eric Andersene494fdd1999-10-19 20:23:03 +0000113#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000114 MINIX_NAME_MAX = 255, /* # chars in a file name */
115};
Eric Andersene494fdd1999-10-19 20:23:03 +0000116
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000117
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000118#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000119enum { version2 = 0 };
120#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000121
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000122enum { MAX_DEPTH = 32 };
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000123
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000124enum { dev_fd = 3 };
125
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000126struct globals {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000127#if ENABLE_FEATURE_MINIX2
128 smallint version2;
129#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000130 smallint changed; /* is filesystem modified? */
131 smallint errors_uncorrected; /* flag if some error was not corrected */
132 smallint termios_set;
133 smallint dirsize;
134 smallint namelen;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000135 const char *device_name;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000136 int directory, regular, blockdev, chardev, links, symlinks, total;
137 char *inode_buffer;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000138
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000139 char *inode_map;
140 char *zone_map;
Eric Andersene494fdd1999-10-19 20:23:03 +0000141
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000142 unsigned char *inode_count;
143 unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000144
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000145 /* File-name data */
146 int name_depth;
147 char *name_component[MAX_DEPTH+1];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000148
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000149 /* Bigger stuff */
150 struct termios sv_termios;
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000151 char superblock_buffer[BLOCK_SIZE];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000152 char add_zone_ind_blk[BLOCK_SIZE];
153 char add_zone_dind_blk[BLOCK_SIZE];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000154 IF_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000155 char check_file_blk[BLOCK_SIZE];
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000156
157 /* File-name data */
158 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
159};
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000160#define G (*ptr_to_globals)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000161#if ENABLE_FEATURE_MINIX2
162#define version2 (G.version2 )
163#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000164#define changed (G.changed )
165#define errors_uncorrected (G.errors_uncorrected )
166#define termios_set (G.termios_set )
167#define dirsize (G.dirsize )
168#define namelen (G.namelen )
169#define device_name (G.device_name )
170#define directory (G.directory )
171#define regular (G.regular )
172#define blockdev (G.blockdev )
173#define chardev (G.chardev )
174#define links (G.links )
175#define symlinks (G.symlinks )
176#define total (G.total )
177#define inode_buffer (G.inode_buffer )
178#define inode_map (G.inode_map )
179#define zone_map (G.zone_map )
180#define inode_count (G.inode_count )
181#define zone_count (G.zone_count )
182#define name_depth (G.name_depth )
183#define name_component (G.name_component )
184#define sv_termios (G.sv_termios )
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000185#define superblock_buffer (G.superblock_buffer )
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000186#define add_zone_ind_blk (G.add_zone_ind_blk )
187#define add_zone_dind_blk (G.add_zone_dind_blk )
188#define add_zone_tind_blk (G.add_zone_tind_blk )
189#define check_file_blk (G.check_file_blk )
190#define current_name (G.current_name )
191#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000192 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000193 dirsize = 16; \
194 namelen = 14; \
195 current_name[0] = '/'; \
196 /*current_name[1] = '\0';*/ \
197 name_component[0] = &current_name[0]; \
198} while (0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000200
201#define OPTION_STR "larvsmf"
202enum {
203 OPT_l = (1 << 0),
204 OPT_a = (1 << 1),
205 OPT_r = (1 << 2),
206 OPT_v = (1 << 3),
207 OPT_s = (1 << 4),
208 OPT_w = (1 << 5),
209 OPT_f = (1 << 6),
210};
211#define OPT_list (option_mask32 & OPT_l)
212#define OPT_automatic (option_mask32 & OPT_a)
213#define OPT_repair (option_mask32 & OPT_r)
214#define OPT_verbose (option_mask32 & OPT_v)
215#define OPT_show (option_mask32 & OPT_s)
216#define OPT_warn_mode (option_mask32 & OPT_w)
217#define OPT_force (option_mask32 & OPT_f)
218/* non-automatic repairs requested? */
219#define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
220
221
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000222#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
223#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
224
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000225#define Super (*(struct minix_superblock *)(superblock_buffer))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000226
227#if ENABLE_FEATURE_MINIX2
228# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000229#else
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000230# define ZONES ((unsigned)(Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000231#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000232#define INODES ((unsigned)Super.s_ninodes)
233#define IMAPS ((unsigned)Super.s_imap_blocks)
234#define ZMAPS ((unsigned)Super.s_zmap_blocks)
235#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
236#define ZONESIZE ((unsigned)Super.s_log_zone_size)
237#define MAXSIZE ((unsigned)Super.s_max_size)
238#define MAGIC (Super.s_magic)
239
240/* gcc likes this more (code is smaller) than macro variant */
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000241static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000242{
Denis Vlasenko150f4022007-01-13 21:06:21 +0000243 return (size + n-1) / n;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000244}
245
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000246#if !ENABLE_FEATURE_MINIX2
247#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000248#else
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000249#define INODE_BLOCKS div_roundup(INODES, \
250 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000251#endif
252
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000253#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
254#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
Eric Andersene494fdd1999-10-19 20:23:03 +0000255
Denis Vlasenkocc244192007-02-06 00:37:40 +0000256/* Before you ask "where they come from?": */
257/* setbit/clrbit are supplied by sys/param.h */
258
259static int minix_bit(const char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000260{
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000261 return (a[i >> 3] & (1<<(i & 7)));
Eric Andersen56f3e352000-09-19 21:13:55 +0000262}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000263
Denis Vlasenkocc244192007-02-06 00:37:40 +0000264static void minix_setbit(char *a, unsigned i)
265{
266 setbit(a, i);
267 changed = 1;
268}
269static void minix_clrbit(char *a, unsigned i)
270{
271 clrbit(a, i);
272 changed = 1;
273}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000274
275/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenkocc244192007-02-06 00:37:40 +0000276#define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
277#define inode_in_use(x) (minix_bit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000278
Denis Vlasenkocc244192007-02-06 00:37:40 +0000279#define mark_inode(x) (minix_setbit(inode_map,(x)))
280#define unmark_inode(x) (minix_clrbit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000281
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000282#define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
283#define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000284
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000285
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000286static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000287#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000288static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000289#endif
290
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000291static void die(const char *str) NORETURN;
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000292static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000293{
294 if (termios_set)
Denis Vlasenko202ac502008-11-05 13:20:58 +0000295 tcsetattr_stdin_TCSANOW(&sv_termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000296 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000297}
298
Rob Landley3ea66a82006-04-29 20:08:40 +0000299static void push_filename(const char *name)
300{
301 // /dir/dir/dir/file
302 // ^ ^ ^
303 // [0] [1] [2] <-name_component[i]
304 if (name_depth < MAX_DEPTH) {
305 int len;
306 char *p = name_component[name_depth];
307 *p++ = '/';
308 len = sprintf(p, "%.*s", namelen, name);
309 name_component[name_depth + 1] = p + len;
310 }
311 name_depth++;
312}
313
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000314static void pop_filename(void)
315{
Rob Landley3ea66a82006-04-29 20:08:40 +0000316 name_depth--;
317 if (name_depth < MAX_DEPTH) {
318 *name_component[name_depth] = '\0';
319 if (!name_depth) {
320 current_name[0] = '/';
321 current_name[1] = '\0';
322 }
323 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000324}
325
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000327{
328 int c;
329
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000330 if (!OPT_repair) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000331 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000332 errors_uncorrected = 1;
333 return 0;
334 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000335 if (OPT_automatic) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000336 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000337 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000338 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000339 return def;
340 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000342 for (;;) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100343 fflush_all();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000344 c = getchar();
345 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 if (!def)
347 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000348 return def;
349 }
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100350 if (c == '\n')
351 break;
352 c |= 0x20; /* tolower */
353 if (c == 'y') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000354 def = 1;
355 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100356 }
357 if (c == 'n') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 def = 0;
359 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100360 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000361 }
362 if (def)
363 printf("y\n");
364 else {
365 printf("n\n");
366 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000368 return def;
369}
370
371/*
372 * Make certain that we aren't checking a filesystem that is on a
373 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
374 * 1994 Theodore Ts'o. Also licensed under GPL.
375 */
376static void check_mount(void)
377{
Denys Vlasenko6ae64262009-07-18 16:22:26 +0200378 if (find_mount_point(device_name, 0)) {
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200379 int cont;
380#if ENABLE_FEATURE_MTAB_SUPPORT
381 /*
382 * If the root is mounted read-only, then /etc/mtab is
383 * probably not correct; so we won't issue a warning based on
384 * it.
385 */
386 int fd = open(bb_path_mtab_file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000387
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200388 if (fd < 0 && errno == EROFS)
389 return;
390 close(fd);
391#endif
392 printf("%s is mounted. ", device_name);
393 cont = 0;
394 if (isatty(0) && isatty(1))
395 cont = ask("Do you really want to continue", 0);
396 if (!cont) {
397 printf("Check aborted\n");
398 exit(EXIT_SUCCESS);
399 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000400 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000401}
402
403/*
404 * check_zone_nr checks to see that *nr is a valid zone nr. If it
405 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
406 * if an error was corrected, and returns the zone (0 for no zone
407 * or a bad zone-number).
408 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000409static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000410{
Rob Landley3ea66a82006-04-29 20:08:40 +0000411 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000412 if (!*nr)
413 return 0;
414 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000415 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000416 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000417 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000418 else
419 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000420 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000422 *nr = 0;
423 *corrected = 1;
424 }
425 return 0;
426}
427
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000428static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000429{
Rob Landley8a625492006-04-29 20:05:17 +0000430 uint32_t nr32 = *nr;
431 int r = check_zone_nr2(&nr32, corrected);
432 *nr = (uint16_t)nr32;
433 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000434}
Eric Andersene494fdd1999-10-19 20:23:03 +0000435
436/*
437 * read-block reads block nr into the buffer at addr.
438 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000439static void read_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000440{
441 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000443 return;
444 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000445 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
446 if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
447 printf("%s: bad block %u in file '%s'\n",
448 bb_msg_read_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000449 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000450 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000451 }
452}
453
454/*
455 * write_block writes block nr to disk.
456 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000457static void write_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000458{
459 if (!nr)
460 return;
461 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000462 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000463 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000464 errors_uncorrected = 1;
465 return;
466 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000467 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
468 if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
469 printf("%s: bad block %u in file '%s'\n",
470 bb_msg_write_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000471 errors_uncorrected = 1;
472 }
473}
474
475/*
Rob Landley8a625492006-04-29 20:05:17 +0000476 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000477 * It sets 'changed' if the inode has needed changing, and re-writes
478 * any indirect blocks with errors.
479 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000480static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000481{
Rob Landley8a625492006-04-29 20:05:17 +0000482 uint16_t ind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000483 int block, result;
484 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000485
Erik Andersene49d5ec2000-02-08 19:58:47 +0000486 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000487 return check_zone_nr(inode->i_zone + blknr, &changed);
488 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000490 block = check_zone_nr(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000491 goto common;
Eric Andersene494fdd1999-10-19 20:23:03 +0000492 }
493 blknr -= 512;
494 block = check_zone_nr(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000495 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000497 result = check_zone_nr(&ind[blknr / 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000498 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000499 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000500 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000501 common:
502 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000503 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000504 result = check_zone_nr(&ind[blknr % 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000506 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 return result;
508}
509
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000510#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000511static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000512{
Rob Landley8a625492006-04-29 20:05:17 +0000513 uint32_t ind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000514 int block, result;
515 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000516
517 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000518 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000519 blknr -= 7;
520 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 block = check_zone_nr2(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000522 goto common2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000523 }
524 blknr -= 256;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000525 if (blknr < 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 block = check_zone_nr2(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000527 goto common1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000528 }
529 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 block = check_zone_nr2(inode->i_zone + 9, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000531 read_block(block, ind); /* triple indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000532 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000533 result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000534 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000535 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000536 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000537 common1:
538 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000539 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000540 result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000541 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000542 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000543 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000544 common2:
545 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000546 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000547 result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000549 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000550 return result;
551}
552#endif
553
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000554static void write_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000555{
556 /*
557 * Set the state of the filesystem based on whether or not there
558 * are uncorrected errors. The filesystem valid flag is
559 * unconditionally set if we get this far.
560 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000561 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
562 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000564
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000565 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000566 if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100567 die("can't write superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000568}
569
Eric Andersene674eb71999-10-19 20:52:57 +0000570static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000571{
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000572 write_superblock();
Eric Andersene494fdd1999-10-19 20:23:03 +0000573
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000574 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100575 die("can't write inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000576 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100577 die("can't write zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000578 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100579 die("can't write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000580}
581
Erik Andersene49d5ec2000-02-08 19:58:47 +0000582static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000583{
584 int block;
585 char blk[BLOCK_SIZE];
586 int size;
587
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000588#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000589 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000590 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000591 else
592#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000593 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000594 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000595 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000596 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000597 dirsize = size;
598 namelen = size - 2;
599 return;
600 }
601 }
602 /* use defaults */
603}
604
Eric Andersene674eb71999-10-19 20:52:57 +0000605static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000606{
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000607 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000608 if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100609 die("can't read superblock");
Rob Landley3ea66a82006-04-29 20:08:40 +0000610 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000611 namelen = 14;
612 dirsize = 16;
613 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000614 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000615 if (MAGIC == MINIX1_SUPER_MAGIC) {
616 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000617 namelen = 30;
618 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000619#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000621 version2 = 1;
622 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
623 namelen = 30;
624 dirsize = 32;
625 version2 = 1;
626#endif
627 } else
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000628 die("bad magic number in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000629 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000630 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000631 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000632 die("bad s_imap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000633 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000634 die("bad s_zmap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000635}
636
Eric Andersene674eb71999-10-19 20:52:57 +0000637static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000638{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000639 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
640 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000641 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
642 inode_count = xmalloc(INODES + 1);
643 zone_count = xmalloc(ZONES);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000644 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100645 die("can't read inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000646 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100647 die("can't read zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000648 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100649 die("can't read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000650 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000651 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000652 errors_uncorrected = 1;
653 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000654 get_dirsize();
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000655 if (OPT_show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000656 printf("%u inodes\n"
657 "%u blocks\n"
658 "Firstdatazone=%u (%u)\n"
659 "Zonesize=%u\n"
660 "Maxsize=%u\n"
661 "Filesystem state=%u\n"
662 "namelen=%u\n\n",
Rob Landley8a625492006-04-29 20:05:17 +0000663 INODES,
664 ZONES,
665 FIRSTZONE, NORM_FIRSTZONE,
666 BLOCK_SIZE << ZONESIZE,
667 MAXSIZE,
668 Super.s_state,
669 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000670 }
671}
672
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000673static void get_inode_common(unsigned nr, uint16_t i_mode)
Eric Andersene494fdd1999-10-19 20:23:03 +0000674{
Eric Andersene494fdd1999-10-19 20:23:03 +0000675 total++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000676 if (!inode_count[nr]) {
677 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000678 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000679 "for file '%s'\n", nr, current_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000680 if (OPT_repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000681 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000682 mark_inode(nr);
Denis Vlasenkocc244192007-02-06 00:37:40 +0000683 else
684 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000685 }
686 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000687 if (S_ISDIR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000688 directory++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000689 else if (S_ISREG(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000690 regular++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000691 else if (S_ISCHR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000692 chardev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000693 else if (S_ISBLK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000694 blockdev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000695 else if (S_ISLNK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000696 symlinks++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000697 else if (S_ISSOCK(i_mode));
698 else if (S_ISFIFO(i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000699 else {
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000700 printf("%s has mode %05o\n", current_name, i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000701 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000702 } else
703 links++;
704 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000705 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000706 inode_count[nr]--;
707 errors_uncorrected = 1;
708 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000709}
710
711static struct minix1_inode *get_inode(unsigned nr)
712{
713 struct minix1_inode *inode;
714
715 if (!nr || nr > INODES)
716 return NULL;
717 inode = Inode1 + nr;
718 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 return inode;
720}
721
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000722#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000723static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000724{
725 struct minix2_inode *inode;
726
727 if (!nr || nr > INODES)
728 return NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000729 inode = Inode2 + nr;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000730 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000731 return inode;
732}
733#endif
734
Eric Andersene674eb71999-10-19 20:52:57 +0000735static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000736{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000737 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000738
739 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000740 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000741}
742
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000743#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000745{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000746 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000747
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000749 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000750}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000751#else
752void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000753#endif
754
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000755static int add_zone_common(int block, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000756{
Eric Andersene494fdd1999-10-19 20:23:03 +0000757 if (!block)
758 return 0;
759 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000760 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000761 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000763 block = 0;
764 *corrected = 1;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000765 return -1; /* "please zero out *znr" */
Eric Andersene494fdd1999-10-19 20:23:03 +0000766 }
767 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000768 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000769 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000770 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 mark_zone(block);
773 }
774 if (!++zone_count[block])
775 zone_count[block]--;
776 return block;
777}
778
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000779static int add_zone(uint16_t *znr, smallint *corrected)
780{
781 int block;
782
783 block = check_zone_nr(znr, corrected);
784 block = add_zone_common(block, corrected);
785 if (block == -1) {
786 *znr = 0;
787 block = 0;
788 }
789 return block;
790}
791
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000792#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000793static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000794{
Eric Andersene494fdd1999-10-19 20:23:03 +0000795 int block;
796
Erik Andersene49d5ec2000-02-08 19:58:47 +0000797 block = check_zone_nr2(znr, corrected);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000798 block = add_zone_common(block, corrected);
799 if (block == -1) {
800 *znr = 0;
801 block = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000802 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000803 return block;
804}
805#endif
806
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000807static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000808{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000809 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000810 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000811 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000812
813 block = add_zone(znr, corrected);
814 if (!block)
815 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000816 read_block(block, add_zone_ind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000818 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000819 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000820 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000821}
822
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000823#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000824static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000825{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000826 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000827 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000828 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000829
Erik Andersene49d5ec2000-02-08 19:58:47 +0000830 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000831 if (!block)
832 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000833 read_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000834 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000835 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000836 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000837 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000838}
839#endif
840
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000841static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000842{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000843 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000844 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000845 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000846
847 block = add_zone(znr, corrected);
848 if (!block)
849 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000850 read_block(block, add_zone_dind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000852 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000853 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000854 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000855}
856
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000857#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000858static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000859{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000860 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000861 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000862 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000863
Erik Andersene49d5ec2000-02-08 19:58:47 +0000864 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000865 if (!block)
866 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000867 read_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000868 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000869 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000870 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000871 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000872}
873
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000874static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000875{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000876 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000877 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000878 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000879
Erik Andersene49d5ec2000-02-08 19:58:47 +0000880 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000881 if (!block)
882 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000883 read_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000884 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000885 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000886 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000887 write_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000888}
889#endif
890
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000891static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000892{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000893 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000894
895 if (!i || i > INODES)
896 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000898 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000899 inode = Inode1 + i;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100900 if (!S_ISDIR(inode->i_mode)
901 && !S_ISREG(inode->i_mode)
902 && !S_ISLNK(inode->i_mode)
903 ) {
904 return;
905 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000906 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000907 add_zone(i + inode->i_zone, &changed);
908 add_zone_ind(7 + inode->i_zone, &changed);
909 add_zone_dind(8 + inode->i_zone, &changed);
910}
911
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000912#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000913static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000914{
915 struct minix2_inode *inode;
916
917 if (!i || i > INODES)
918 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000920 return;
921 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
923 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000924 return;
925 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000926 add_zone2(i + inode->i_zone, &changed);
927 add_zone_ind2(7 + inode->i_zone, &changed);
928 add_zone_dind2(8 + inode->i_zone, &changed);
929 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000930}
931#endif
932
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000933static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000934{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000935 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000936 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000938 int block;
939
Erik Andersene49d5ec2000-02-08 19:58:47 +0000940 block = map_block(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000941 read_block(block, check_file_blk);
942 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000943 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000944 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000945 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000946 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000947 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000948 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000949 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000950 }
951 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000952 }
Rob Landley3ea66a82006-04-29 20:08:40 +0000953 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000954 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000955 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000956 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000957 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000958 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000959 printf("%s: bad directory: '.' isn't first\n", current_name);
960 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000961 }
962 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000963 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000964 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000965 printf("%s: bad directory: '..' isn't second\n", current_name);
966 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000967 }
968 if (!inode)
969 return;
Rob Landley3ea66a82006-04-29 20:08:40 +0000970 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000971 if (OPT_list) {
972 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000973 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +0000974 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +0000975 }
976 check_zones(ino);
977 if (inode && S_ISDIR(inode->i_mode))
978 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000979 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000980}
981
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000982#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000983static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000984{
Eric Andersene494fdd1999-10-19 20:23:03 +0000985 struct minix2_inode *inode;
986 int ino;
987 char *name;
988 int block;
989
Erik Andersene49d5ec2000-02-08 19:58:47 +0000990 block = map_block2(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000991 read_block(block, check_file_blk);
992 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000993 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000994 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000995 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000996 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000997 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000998 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000999 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001000 }
1001 ino = 0;
1002 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001003 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001005 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001006 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001007 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001008 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001009 printf("%s: bad directory: '.' isn't first\n", current_name);
1010 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001011 }
1012 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001013 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001014 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001015 printf("%s: bad directory: '..' isn't second\n", current_name);
1016 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001017 }
1018 if (!inode)
1019 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001020 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001021 if (OPT_list) {
1022 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001023 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001024 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001025 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001026 check_zones2(ino);
1027 if (inode && S_ISDIR(inode->i_mode))
1028 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001029 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001030}
1031#endif
1032
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001033static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001034{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001035 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001036 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001037
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001038 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001039 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001040 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001041 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001042 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001043 errors_uncorrected = 1;
1044 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001045 for (offset = 0; offset < dir->i_size; offset += dirsize)
1046 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001047}
1048
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001049#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001050static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001051{
1052 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001053 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001054
1055 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001057 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001058 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001059 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001060 errors_uncorrected = 1;
1061 }
1062 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001063 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001064}
1065#endif
1066
Eric Andersene674eb71999-10-19 20:52:57 +00001067static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001068{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001069 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001070
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001071 xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
1072 return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
Eric Andersene494fdd1999-10-19 20:23:03 +00001073}
1074
Eric Andersene674eb71999-10-19 20:52:57 +00001075static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001076{
1077 int i;
1078
Erik Andersene49d5ec2000-02-08 19:58:47 +00001079 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001080 if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001081 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001082 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001083 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001084 changed = 1;
1085 }
1086 }
1087 if (!inode_count[i]) {
1088 if (!inode_in_use(i))
1089 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001090 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001092 unmark_inode(i);
1093 continue;
1094 }
1095 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001096 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001097 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001098 mark_inode(i);
1099 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001100 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001101 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001102 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1103 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001105 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001106 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001107 }
1108 }
1109 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001110 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001111 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001112 continue;
1113 if (!zone_count[i]) {
1114 if (bad_zone(i))
1115 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001116 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001118 unmark_zone(i);
1119 continue;
1120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 printf("Zone %d: %sin use, counted=%d\n",
1122 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 }
1124}
1125
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001126#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001128{
1129 int i;
1130
1131 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001132 if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001133 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001135 Inode2[i].i_mode = 0;
1136 changed = 1;
1137 }
1138 }
1139 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001140 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001141 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001142 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001143 if (ask("Clear", 1))
1144 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001145 continue;
1146 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001147 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001148 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001149 if (ask("Set", 1))
1150 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001151 }
1152 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001153 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001154 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1155 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001156 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001157 Inode2[i].i_nlinks = inode_count[i];
1158 changed = 1;
1159 }
1160 }
1161 }
1162 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001163 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001164 continue;
1165 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001166 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001167 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001168 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001169 if (ask("Unmark", 1))
1170 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001171 continue;
1172 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001173 printf("Zone %d: %sin use, counted=%d\n",
1174 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001175 }
1176}
1177#endif
1178
Eric Andersene674eb71999-10-19 20:52:57 +00001179static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001180{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1182 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001183 check_zones(MINIX_ROOT_INO);
1184 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001185 check_counts();
1186}
1187
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001188#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001190{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1192 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001193 check_zones2(MINIX_ROOT_INO);
1194 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001195 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001196}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001197#else
1198void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001199#endif
1200
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001201int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001202int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001203{
1204 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001205 int retcode = 0;
1206
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001207 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001208
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001209 INIT_G();
John Beppuc1dc5d92000-02-11 12:43:20 +00001210
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001211 opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
1212 getopt32(argv, OPTION_STR);
1213 argv += optind;
1214 device_name = argv[0];
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001215
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001216 check_mount(); /* trying to check a mounted filesystem? */
1217 if (OPT_manual) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001218 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001219 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001220 }
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +00001221 xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001222
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001223 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001224 read_superblock();
1225
1226 /*
1227 * Determine whether or not we should continue with the checking.
1228 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001229 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001230 * command line.
1231 */
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001232 printf("%s: %s\n", applet_name, bb_banner);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001233
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001234 if (!(Super.s_state & MINIX_ERROR_FS)
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001235 && (Super.s_state & MINIX_VALID_FS) && !OPT_force
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001236 ) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001237 if (OPT_repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001238 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001239 return 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001240 } else if (OPT_force)
Rob Landley8a625492006-04-29 20:05:17 +00001241 printf("Forcing filesystem check on %s\n", device_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001242 else if (OPT_repair)
Rob Landley8a625492006-04-29 20:05:17 +00001243 printf("Filesystem on %s is dirty, needs checking\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001244 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001245
1246 read_tables();
1247
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001248 if (OPT_manual) {
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001249 tcgetattr(0, &sv_termios);
1250 tmp = sv_termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001251 tmp.c_lflag &= ~(ICANON | ECHO);
Denis Vlasenko202ac502008-11-05 13:20:58 +00001252 tcsetattr_stdin_TCSANOW(&tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001253 termios_set = 1;
1254 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001255
Eric Andersene494fdd1999-10-19 20:23:03 +00001256 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001257 check_root2();
1258 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001259 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001260 check_root();
1261 check();
1262 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001263
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001264 if (OPT_verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001265 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001266
Eric Andersen1ca20a72001-03-21 07:34:27 +00001267 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001268 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001269 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001270 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Eric Andersen1ca20a72001-03-21 07:34:27 +00001271 100 * (INODES - free_cnt) / INODES);
1272 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001273 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001274 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001275 printf("%6u zones used (%u%%)\n\n"
1276 "%6u regular files\n"
1277 "%6u directories\n"
1278 "%6u character device files\n"
1279 "%6u block device files\n"
1280 "%6u links\n"
1281 "%6u symbolic links\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001282 "------\n"
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001283 "%6u files\n",
Rob Landleyfd8409f2006-06-13 15:31:39 +00001284 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
Erik Andersene49d5ec2000-02-08 19:58:47 +00001285 regular, directory, chardev, blockdev,
1286 links - 2 * directory + 1, symlinks,
1287 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001288 }
1289 if (changed) {
1290 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001291 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001292 sync();
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001293 } else if (OPT_repair)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +00001294 write_superblock();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001295
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001296 if (OPT_manual)
Denis Vlasenko202ac502008-11-05 13:20:58 +00001297 tcsetattr_stdin_TCSANOW(&sv_termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001298
1299 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001300 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001301 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001302 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001303 return retcode;
1304}