blob: c1d1b2cc3f40faee143e80297f468f1f6a0b1e74 [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 *
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010016 * 14.11.91 - Testing seems to have gone well. Added some
Eric Andersene494fdd1999-10-19 20:23:03 +000017 * 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 *
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010025 * 19.04.92 - Had to start over again from this old version, as a
Eric Andersene494fdd1999-10-19 20:23:03 +000026 * kernel bug ate my enhanced fsck in february.
27 *
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010028 * 28.02.93 - added support for different directory entry sizes..
Eric Andersene494fdd1999-10-19 20:23:03 +000029 *
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +000031 * superblock information
Eric Andersene494fdd1999-10-19 20:23:03 +000032 *
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)
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010037 * 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.
Eric Andersene494fdd1999-10-19 20:23:03 +000041 *
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010042 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
Eric Andersene494fdd1999-10-19 20:23:03 +000044 *
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010045 * 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)
Eric Andersene494fdd1999-10-19 20:23:03 +000048 *
49 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010050 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
Eric Andersene494fdd1999-10-19 20:23:03 +000051 *
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
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010054 * that the ARM is powerful enough to do this in C whereas
Eric Andersene494fdd1999-10-19 20:23:03 +000055 * i386 and m64k must use assembly to get it fast >:-)
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010056 * This should make minix fsck system-independent.
57 * (janl@math.uio.no, Nicolai Langfeldt)
Eric Andersene494fdd1999-10-19 20:23:03 +000058 *
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
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010061 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
Eric Andersene494fdd1999-10-19 20:23:03 +000062 *
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
64 * Andreas Schwab.
65 *
Denis Vlasenkob44c7902008-03-17 09:29:43 +000066 * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org>
Eric Andersene494fdd1999-10-19 20:23:03 +000067 * - 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)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +000081 * -s for superblock info
Eric Andersene494fdd1999-10-19 20:23:03 +000082 * -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
Pere Orga5bc8c002011-04-11 03:29:49 +020089//usage:#define fsck_minix_trivial_usage
90//usage: "[-larvsmf] BLOCKDEV"
91//usage:#define fsck_minix_full_usage "\n\n"
92//usage: "Check MINIX filesystem\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020093//usage: "\n -l List all filenames"
94//usage: "\n -r Perform interactive repairs"
95//usage: "\n -a Perform automatic repairs"
96//usage: "\n -v Verbose"
97//usage: "\n -s Output superblock information"
98//usage: "\n -m Show \"mode not cleared\" warnings"
99//usage: "\n -f Force file system check"
100
Rob Landleycc9962d2006-09-14 05:27:28 +0000101#include <mntent.h>
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000102#include "libbb.h"
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000103#include "minix.h"
Eric Andersen85c55202000-07-10 22:46:55 +0000104
Eric Andersen85c55202000-07-10 22:46:55 +0000105#ifndef BLKGETSIZE
106#define BLKGETSIZE _IO(0x12,96) /* return device size */
107#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000108
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000109struct BUG_bad_inode_size {
110 char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
111#if ENABLE_FEATURE_MINIX2
112 char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
113#endif
114};
115
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000116enum {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000117#ifdef UNUSED
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000118 MINIX1_LINK_MAX = 250,
119 MINIX2_LINK_MAX = 65530,
120 MINIX_I_MAP_SLOTS = 8,
121 MINIX_Z_MAP_SLOTS = 64,
122 MINIX_V1 = 0x0001, /* original minix fs */
123 MINIX_V2 = 0x0002, /* minix V2 fs */
Eric Andersene494fdd1999-10-19 20:23:03 +0000124#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000125 MINIX_NAME_MAX = 255, /* # chars in a file name */
126};
Eric Andersene494fdd1999-10-19 20:23:03 +0000127
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000128
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000129#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000130enum { version2 = 0 };
131#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000132
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000133enum { MAX_DEPTH = 32 };
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000134
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000135enum { dev_fd = 3 };
136
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000137struct globals {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000138#if ENABLE_FEATURE_MINIX2
139 smallint version2;
140#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000141 smallint changed; /* is filesystem modified? */
142 smallint errors_uncorrected; /* flag if some error was not corrected */
143 smallint termios_set;
144 smallint dirsize;
145 smallint namelen;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000146 const char *device_name;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000147 int directory, regular, blockdev, chardev, links, symlinks, total;
148 char *inode_buffer;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000149
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000150 char *inode_map;
151 char *zone_map;
Eric Andersene494fdd1999-10-19 20:23:03 +0000152
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000153 unsigned char *inode_count;
154 unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000155
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000156 /* File-name data */
157 int name_depth;
158 char *name_component[MAX_DEPTH+1];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000159
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000160 /* Bigger stuff */
161 struct termios sv_termios;
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000162 char superblock_buffer[BLOCK_SIZE];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000163 char add_zone_ind_blk[BLOCK_SIZE];
164 char add_zone_dind_blk[BLOCK_SIZE];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000165 IF_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000166 char check_file_blk[BLOCK_SIZE];
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000167
168 /* File-name data */
169 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
170};
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000171#define G (*ptr_to_globals)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000172#if ENABLE_FEATURE_MINIX2
173#define version2 (G.version2 )
174#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000175#define changed (G.changed )
176#define errors_uncorrected (G.errors_uncorrected )
177#define termios_set (G.termios_set )
178#define dirsize (G.dirsize )
179#define namelen (G.namelen )
180#define device_name (G.device_name )
181#define directory (G.directory )
182#define regular (G.regular )
183#define blockdev (G.blockdev )
184#define chardev (G.chardev )
185#define links (G.links )
186#define symlinks (G.symlinks )
187#define total (G.total )
188#define inode_buffer (G.inode_buffer )
189#define inode_map (G.inode_map )
190#define zone_map (G.zone_map )
191#define inode_count (G.inode_count )
192#define zone_count (G.zone_count )
193#define name_depth (G.name_depth )
194#define name_component (G.name_component )
195#define sv_termios (G.sv_termios )
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000196#define superblock_buffer (G.superblock_buffer )
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000197#define add_zone_ind_blk (G.add_zone_ind_blk )
198#define add_zone_dind_blk (G.add_zone_dind_blk )
199#define add_zone_tind_blk (G.add_zone_tind_blk )
200#define check_file_blk (G.check_file_blk )
201#define current_name (G.current_name )
202#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000203 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000204 dirsize = 16; \
205 namelen = 14; \
206 current_name[0] = '/'; \
207 /*current_name[1] = '\0';*/ \
208 name_component[0] = &current_name[0]; \
209} while (0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000210
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000211
212#define OPTION_STR "larvsmf"
213enum {
214 OPT_l = (1 << 0),
215 OPT_a = (1 << 1),
216 OPT_r = (1 << 2),
217 OPT_v = (1 << 3),
218 OPT_s = (1 << 4),
219 OPT_w = (1 << 5),
220 OPT_f = (1 << 6),
221};
222#define OPT_list (option_mask32 & OPT_l)
223#define OPT_automatic (option_mask32 & OPT_a)
224#define OPT_repair (option_mask32 & OPT_r)
225#define OPT_verbose (option_mask32 & OPT_v)
226#define OPT_show (option_mask32 & OPT_s)
227#define OPT_warn_mode (option_mask32 & OPT_w)
228#define OPT_force (option_mask32 & OPT_f)
229/* non-automatic repairs requested? */
230#define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
231
232
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000233#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
234#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
235
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000236#define Super (*(struct minix_superblock *)(superblock_buffer))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000237
238#if ENABLE_FEATURE_MINIX2
239# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000240#else
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000241# define ZONES ((unsigned)(Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000242#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000243#define INODES ((unsigned)Super.s_ninodes)
244#define IMAPS ((unsigned)Super.s_imap_blocks)
245#define ZMAPS ((unsigned)Super.s_zmap_blocks)
246#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
247#define ZONESIZE ((unsigned)Super.s_log_zone_size)
248#define MAXSIZE ((unsigned)Super.s_max_size)
249#define MAGIC (Super.s_magic)
250
251/* gcc likes this more (code is smaller) than macro variant */
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000252static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000253{
Denis Vlasenko150f4022007-01-13 21:06:21 +0000254 return (size + n-1) / n;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000255}
256
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000257#if !ENABLE_FEATURE_MINIX2
258#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000259#else
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000260#define INODE_BLOCKS div_roundup(INODES, \
261 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000262#endif
263
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000264#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
265#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
Eric Andersene494fdd1999-10-19 20:23:03 +0000266
Denis Vlasenkocc244192007-02-06 00:37:40 +0000267/* Before you ask "where they come from?": */
268/* setbit/clrbit are supplied by sys/param.h */
269
270static int minix_bit(const char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000271{
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000272 return (a[i >> 3] & (1<<(i & 7)));
Eric Andersen56f3e352000-09-19 21:13:55 +0000273}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000274
Denis Vlasenkocc244192007-02-06 00:37:40 +0000275static void minix_setbit(char *a, unsigned i)
276{
277 setbit(a, i);
278 changed = 1;
279}
280static void minix_clrbit(char *a, unsigned i)
281{
282 clrbit(a, i);
283 changed = 1;
284}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000285
286/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenkocc244192007-02-06 00:37:40 +0000287#define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
288#define inode_in_use(x) (minix_bit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000289
Denis Vlasenkocc244192007-02-06 00:37:40 +0000290#define mark_inode(x) (minix_setbit(inode_map,(x)))
291#define unmark_inode(x) (minix_clrbit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000292
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000293#define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
294#define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000295
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000296
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000297static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000298#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000299static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000300#endif
301
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000302static void die(const char *str) NORETURN;
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000303static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000304{
305 if (termios_set)
Denis Vlasenko202ac502008-11-05 13:20:58 +0000306 tcsetattr_stdin_TCSANOW(&sv_termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000307 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000308}
309
Rob Landley3ea66a82006-04-29 20:08:40 +0000310static void push_filename(const char *name)
311{
312 // /dir/dir/dir/file
313 // ^ ^ ^
314 // [0] [1] [2] <-name_component[i]
315 if (name_depth < MAX_DEPTH) {
316 int len;
317 char *p = name_component[name_depth];
318 *p++ = '/';
319 len = sprintf(p, "%.*s", namelen, name);
320 name_component[name_depth + 1] = p + len;
321 }
322 name_depth++;
323}
324
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000325static void pop_filename(void)
326{
Rob Landley3ea66a82006-04-29 20:08:40 +0000327 name_depth--;
328 if (name_depth < MAX_DEPTH) {
329 *name_component[name_depth] = '\0';
330 if (!name_depth) {
331 current_name[0] = '/';
332 current_name[1] = '\0';
333 }
334 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000335}
336
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000338{
339 int c;
340
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000341 if (!OPT_repair) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000342 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000343 errors_uncorrected = 1;
344 return 0;
345 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000346 if (OPT_automatic) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000347 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000348 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000350 return def;
351 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000353 for (;;) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100354 fflush_all();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000355 c = getchar();
356 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 if (!def)
358 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000359 return def;
360 }
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100361 if (c == '\n')
362 break;
363 c |= 0x20; /* tolower */
364 if (c == 'y') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000365 def = 1;
366 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100367 }
368 if (c == 'n') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000369 def = 0;
370 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100371 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000372 }
373 if (def)
374 printf("y\n");
375 else {
376 printf("n\n");
377 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000378 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000379 return def;
380}
381
382/*
383 * Make certain that we aren't checking a filesystem that is on a
384 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
385 * 1994 Theodore Ts'o. Also licensed under GPL.
386 */
387static void check_mount(void)
388{
Denys Vlasenko6ae64262009-07-18 16:22:26 +0200389 if (find_mount_point(device_name, 0)) {
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200390 int cont;
391#if ENABLE_FEATURE_MTAB_SUPPORT
392 /*
393 * If the root is mounted read-only, then /etc/mtab is
394 * probably not correct; so we won't issue a warning based on
395 * it.
396 */
397 int fd = open(bb_path_mtab_file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000398
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200399 if (fd < 0 && errno == EROFS)
400 return;
401 close(fd);
402#endif
403 printf("%s is mounted. ", device_name);
404 cont = 0;
405 if (isatty(0) && isatty(1))
406 cont = ask("Do you really want to continue", 0);
407 if (!cont) {
408 printf("Check aborted\n");
409 exit(EXIT_SUCCESS);
410 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000411 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000412}
413
414/*
415 * check_zone_nr checks to see that *nr is a valid zone nr. If it
416 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
417 * if an error was corrected, and returns the zone (0 for no zone
418 * or a bad zone-number).
419 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000420static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000421{
Rob Landley3ea66a82006-04-29 20:08:40 +0000422 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000423 if (!*nr)
424 return 0;
425 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000426 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000427 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000428 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000429 else
430 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000431 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000433 *nr = 0;
434 *corrected = 1;
435 }
436 return 0;
437}
438
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000439static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000440{
Rob Landley8a625492006-04-29 20:05:17 +0000441 uint32_t nr32 = *nr;
442 int r = check_zone_nr2(&nr32, corrected);
443 *nr = (uint16_t)nr32;
444 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000445}
Eric Andersene494fdd1999-10-19 20:23:03 +0000446
447/*
448 * read-block reads block nr into the buffer at addr.
449 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000450static void read_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000451{
452 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000454 return;
455 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000456 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
457 if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
458 printf("%s: bad block %u in file '%s'\n",
459 bb_msg_read_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000460 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000461 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000462 }
463}
464
465/*
466 * write_block writes block nr to disk.
467 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000468static void write_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000469{
470 if (!nr)
471 return;
472 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000473 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000475 errors_uncorrected = 1;
476 return;
477 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000478 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
479 if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
480 printf("%s: bad block %u in file '%s'\n",
481 bb_msg_write_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000482 errors_uncorrected = 1;
483 }
484}
485
486/*
Rob Landley8a625492006-04-29 20:05:17 +0000487 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000488 * It sets 'changed' if the inode has needed changing, and re-writes
489 * any indirect blocks with errors.
490 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000491static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000492{
Rob Landley8a625492006-04-29 20:05:17 +0000493 uint16_t ind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000494 int block, result;
495 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000496
Erik Andersene49d5ec2000-02-08 19:58:47 +0000497 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000498 return check_zone_nr(inode->i_zone + blknr, &changed);
499 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000501 block = check_zone_nr(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000502 goto common;
Eric Andersene494fdd1999-10-19 20:23:03 +0000503 }
504 blknr -= 512;
505 block = check_zone_nr(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000506 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000508 result = check_zone_nr(&ind[blknr / 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000509 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000510 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000511 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000512 common:
513 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000514 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000515 result = check_zone_nr(&ind[blknr % 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000516 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000517 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000518 return result;
519}
520
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000521#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000522static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000523{
Rob Landley8a625492006-04-29 20:05:17 +0000524 uint32_t ind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000525 int block, result;
526 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000527
528 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000529 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000530 blknr -= 7;
531 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000532 block = check_zone_nr2(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000533 goto common2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000534 }
535 blknr -= 256;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000536 if (blknr < 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000537 block = check_zone_nr2(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000538 goto common1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000539 }
540 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541 block = check_zone_nr2(inode->i_zone + 9, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000542 read_block(block, ind); /* triple indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000543 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000544 result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000545 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000546 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000547 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000548 common1:
549 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000550 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000551 result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000552 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000553 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000554 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000555 common2:
556 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000557 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000558 result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000559 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000560 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 return result;
562}
563#endif
564
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000565static void write_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000566{
567 /*
568 * Set the state of the filesystem based on whether or not there
569 * are uncorrected errors. The filesystem valid flag is
570 * unconditionally set if we get this far.
571 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000572 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
573 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000574 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000575
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000576 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000577 if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100578 die("can't write superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000579}
580
Eric Andersene674eb71999-10-19 20:52:57 +0000581static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000582{
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000583 write_superblock();
Eric Andersene494fdd1999-10-19 20:23:03 +0000584
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000585 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100586 die("can't write inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000587 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100588 die("can't write zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000589 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100590 die("can't write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000591}
592
Erik Andersene49d5ec2000-02-08 19:58:47 +0000593static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000594{
595 int block;
596 char blk[BLOCK_SIZE];
597 int size;
598
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000599#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000600 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000601 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000602 else
603#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000604 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000605 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000606 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000608 dirsize = size;
609 namelen = size - 2;
610 return;
611 }
612 }
613 /* use defaults */
614}
615
Eric Andersene674eb71999-10-19 20:52:57 +0000616static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000617{
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000618 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000619 if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100620 die("can't read superblock");
Rob Landley3ea66a82006-04-29 20:08:40 +0000621 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000622 namelen = 14;
623 dirsize = 16;
624 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000625 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000626 if (MAGIC == MINIX1_SUPER_MAGIC) {
627 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000628 namelen = 30;
629 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000630#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000631 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000632 version2 = 1;
633 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
634 namelen = 30;
635 dirsize = 32;
636 version2 = 1;
637#endif
638 } else
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000639 die("bad magic number in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000640 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000641 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000642 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000643 die("bad s_imap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000644 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000645 die("bad s_zmap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000646}
647
Eric Andersene674eb71999-10-19 20:52:57 +0000648static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000649{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000650 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
651 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000652 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
653 inode_count = xmalloc(INODES + 1);
654 zone_count = xmalloc(ZONES);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000655 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100656 die("can't read inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000657 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100658 die("can't read zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000659 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100660 die("can't read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000661 if (NORM_FIRSTZONE != FIRSTZONE) {
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000662 printf("warning: firstzone!=norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000663 errors_uncorrected = 1;
664 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000665 get_dirsize();
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000666 if (OPT_show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000667 printf("%u inodes\n"
668 "%u blocks\n"
669 "Firstdatazone=%u (%u)\n"
670 "Zonesize=%u\n"
671 "Maxsize=%u\n"
672 "Filesystem state=%u\n"
673 "namelen=%u\n\n",
Rob Landley8a625492006-04-29 20:05:17 +0000674 INODES,
675 ZONES,
676 FIRSTZONE, NORM_FIRSTZONE,
677 BLOCK_SIZE << ZONESIZE,
678 MAXSIZE,
679 Super.s_state,
680 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000681 }
682}
683
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000684static void get_inode_common(unsigned nr, uint16_t i_mode)
Eric Andersene494fdd1999-10-19 20:23:03 +0000685{
Eric Andersene494fdd1999-10-19 20:23:03 +0000686 total++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000687 if (!inode_count[nr]) {
688 if (!inode_in_use(nr)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000689 printf("Inode %d is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000690 "for file '%s'\n", nr, current_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000691 if (OPT_repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000692 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000693 mark_inode(nr);
Denis Vlasenkocc244192007-02-06 00:37:40 +0000694 else
695 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000696 }
697 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000698 if (S_ISDIR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000699 directory++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000700 else if (S_ISREG(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 regular++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000702 else if (S_ISCHR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000703 chardev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000704 else if (S_ISBLK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000705 blockdev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000706 else if (S_ISLNK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000707 symlinks++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000708 else if (S_ISSOCK(i_mode));
709 else if (S_ISFIFO(i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000710 else {
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000711 printf("%s has mode %05o\n", current_name, i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000713 } else
714 links++;
715 if (!++inode_count[nr]) {
Rob Landley8a625492006-04-29 20:05:17 +0000716 printf("Warning: inode count too big\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000717 inode_count[nr]--;
718 errors_uncorrected = 1;
719 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000720}
721
722static struct minix1_inode *get_inode(unsigned nr)
723{
724 struct minix1_inode *inode;
725
726 if (!nr || nr > INODES)
727 return NULL;
728 inode = Inode1 + nr;
729 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000730 return inode;
731}
732
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000733#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000734static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000735{
736 struct minix2_inode *inode;
737
738 if (!nr || nr > INODES)
739 return NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000740 inode = Inode2 + nr;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000741 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000742 return inode;
743}
744#endif
745
Eric Andersene674eb71999-10-19 20:52:57 +0000746static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000747{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000748 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000749
750 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000751 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000752}
753
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000754#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000755static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000756{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000757 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000758
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000760 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000761}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000762#else
763void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000764#endif
765
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000766static int add_zone_common(int block, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000767{
Eric Andersene494fdd1999-10-19 20:23:03 +0000768 if (!block)
769 return 0;
770 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000771 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000772 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000774 block = 0;
775 *corrected = 1;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000776 return -1; /* "please zero out *znr" */
Eric Andersene494fdd1999-10-19 20:23:03 +0000777 }
778 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000779 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000780 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000781 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000782 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000783 mark_zone(block);
784 }
785 if (!++zone_count[block])
786 zone_count[block]--;
787 return block;
788}
789
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000790static int add_zone(uint16_t *znr, smallint *corrected)
791{
792 int block;
793
794 block = check_zone_nr(znr, corrected);
795 block = add_zone_common(block, corrected);
796 if (block == -1) {
797 *znr = 0;
798 block = 0;
799 }
800 return block;
801}
802
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000803#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000804static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000805{
Eric Andersene494fdd1999-10-19 20:23:03 +0000806 int block;
807
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 block = check_zone_nr2(znr, corrected);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000809 block = add_zone_common(block, corrected);
810 if (block == -1) {
811 *znr = 0;
812 block = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000813 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000814 return block;
815}
816#endif
817
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000818static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000819{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000820 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000821 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000822 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000823
824 block = add_zone(znr, corrected);
825 if (!block)
826 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000827 read_block(block, add_zone_ind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000828 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000829 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000830 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000831 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000832}
833
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000834#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000835static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000836{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000837 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000838 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000839 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000840
Erik Andersene49d5ec2000-02-08 19:58:47 +0000841 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000842 if (!block)
843 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000844 read_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000845 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000846 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000847 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000848 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000849}
850#endif
851
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000852static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000853{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000854 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000855 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000856 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000857
858 block = add_zone(znr, corrected);
859 if (!block)
860 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000861 read_block(block, add_zone_dind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000863 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000864 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000865 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000866}
867
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000868#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000869static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000870{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000871 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000872 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000873 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000874
Erik Andersene49d5ec2000-02-08 19:58:47 +0000875 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000876 if (!block)
877 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000878 read_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000879 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000880 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000881 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000882 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000883}
884
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000885static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000886{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000887 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000888 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000889 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000890
Erik Andersene49d5ec2000-02-08 19:58:47 +0000891 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000892 if (!block)
893 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000894 read_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000895 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000896 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000897 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000898 write_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000899}
900#endif
901
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000902static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000903{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000904 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000905
906 if (!i || i > INODES)
907 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000908 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000910 inode = Inode1 + i;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100911 if (!S_ISDIR(inode->i_mode)
912 && !S_ISREG(inode->i_mode)
913 && !S_ISLNK(inode->i_mode)
914 ) {
915 return;
916 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000917 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000918 add_zone(i + inode->i_zone, &changed);
919 add_zone_ind(7 + inode->i_zone, &changed);
920 add_zone_dind(8 + inode->i_zone, &changed);
921}
922
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000923#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000924static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000925{
926 struct minix2_inode *inode;
927
928 if (!i || i > INODES)
929 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000930 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000931 return;
932 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000933 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
934 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000935 return;
936 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000937 add_zone2(i + inode->i_zone, &changed);
938 add_zone_ind2(7 + inode->i_zone, &changed);
939 add_zone_dind2(8 + inode->i_zone, &changed);
940 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000941}
942#endif
943
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000944static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000945{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000946 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000947 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000948 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000949 int block;
950
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 block = map_block(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000952 read_block(block, check_file_blk);
953 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000954 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000955 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000956 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000957 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000958 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000959 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000960 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000961 }
962 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000963 }
Rob Landley3ea66a82006-04-29 20:08:40 +0000964 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000965 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000966 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000967 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000968 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000969 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000970 printf("%s: bad directory: '.' isn't first\n", current_name);
971 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000972 }
973 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000974 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000976 printf("%s: bad directory: '..' isn't second\n", current_name);
977 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000978 }
979 if (!inode)
980 return;
Rob Landley3ea66a82006-04-29 20:08:40 +0000981 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000982 if (OPT_list) {
983 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000984 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +0000985 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +0000986 }
987 check_zones(ino);
988 if (inode && S_ISDIR(inode->i_mode))
989 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000990 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000991}
992
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000993#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000994static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000995{
Eric Andersene494fdd1999-10-19 20:23:03 +0000996 struct minix2_inode *inode;
997 int ino;
998 char *name;
999 int block;
1000
Erik Andersene49d5ec2000-02-08 19:58:47 +00001001 block = map_block2(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001002 read_block(block, check_file_blk);
1003 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001004 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001005 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001006 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001007 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001008 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001009 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001010 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001011 }
1012 ino = 0;
1013 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001014 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001016 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001017 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001018 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001019 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001020 printf("%s: bad directory: '.' isn't first\n", current_name);
1021 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001022 }
1023 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001024 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001025 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001026 printf("%s: bad directory: '..' isn't second\n", current_name);
1027 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001028 }
1029 if (!inode)
1030 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001031 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001032 if (OPT_list) {
1033 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001034 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001035 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001036 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001037 check_zones2(ino);
1038 if (inode && S_ISDIR(inode->i_mode))
1039 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001040 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001041}
1042#endif
1043
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001044static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001045{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001046 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001047 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001048
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001049 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001050 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001051 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001052 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001053 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001054 errors_uncorrected = 1;
1055 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001056 for (offset = 0; offset < dir->i_size; offset += dirsize)
1057 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001058}
1059
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001060#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001061static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001062{
1063 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001064 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001065
1066 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001067 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001068 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001069 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001070 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001071 errors_uncorrected = 1;
1072 }
1073 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001074 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001075}
1076#endif
1077
Eric Andersene674eb71999-10-19 20:52:57 +00001078static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001079{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001080 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001081
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001082 xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
1083 return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
Eric Andersene494fdd1999-10-19 20:23:03 +00001084}
1085
Eric Andersene674eb71999-10-19 20:52:57 +00001086static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001087{
1088 int i;
1089
Erik Andersene49d5ec2000-02-08 19:58:47 +00001090 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001091 if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001092 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001093 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001094 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001095 changed = 1;
1096 }
1097 }
1098 if (!inode_count[i]) {
1099 if (!inode_in_use(i))
1100 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001101 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001102 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001103 unmark_inode(i);
1104 continue;
1105 }
1106 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001107 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001108 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001109 mark_inode(i);
1110 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001111 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001112 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001113 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1114 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001115 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001116 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001117 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001118 }
1119 }
1120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001121 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001122 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 continue;
1124 if (!zone_count[i]) {
1125 if (bad_zone(i))
1126 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001127 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001128 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001129 unmark_zone(i);
1130 continue;
1131 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001132 printf("Zone %d: %sin use, counted=%d\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001133 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001134 }
1135}
1136
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001137#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001138static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001139{
1140 int i;
1141
1142 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001143 if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001144 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001145 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001146 Inode2[i].i_mode = 0;
1147 changed = 1;
1148 }
1149 }
1150 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001151 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001152 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001153 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001154 if (ask("Clear", 1))
1155 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001156 continue;
1157 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001158 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001159 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001160 if (ask("Set", 1))
1161 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001162 }
1163 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001164 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001165 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1166 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001167 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001168 Inode2[i].i_nlinks = inode_count[i];
1169 changed = 1;
1170 }
1171 }
1172 }
1173 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001174 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001175 continue;
1176 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001177 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001178 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001179 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001180 if (ask("Unmark", 1))
1181 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001182 continue;
1183 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001184 printf("Zone %d: %sin use, counted=%d\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001185 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001186 }
1187}
1188#endif
1189
Eric Andersene674eb71999-10-19 20:52:57 +00001190static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001191{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001192 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1193 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001194 check_zones(MINIX_ROOT_INO);
1195 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001196 check_counts();
1197}
1198
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001199#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001200static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001201{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001202 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1203 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001204 check_zones2(MINIX_ROOT_INO);
1205 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001206 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001207}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001208#else
1209void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001210#endif
1211
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001212int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001213int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001214{
1215 struct termios tmp;
Eric Andersene494fdd1999-10-19 20:23:03 +00001216 int retcode = 0;
1217
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001218 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001219
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001220 INIT_G();
John Beppuc1dc5d92000-02-11 12:43:20 +00001221
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001222 opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
1223 getopt32(argv, OPTION_STR);
1224 argv += optind;
1225 device_name = argv[0];
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001226
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001227 check_mount(); /* trying to check a mounted filesystem? */
1228 if (OPT_manual) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001229 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001230 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001231 }
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +00001232 xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001233
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001234 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001235 read_superblock();
1236
1237 /*
1238 * Determine whether or not we should continue with the checking.
1239 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001240 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001241 * command line.
1242 */
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001243 printf("%s: %s\n", applet_name, bb_banner);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001244
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001245 if (!(Super.s_state & MINIX_ERROR_FS)
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001246 && (Super.s_state & MINIX_VALID_FS) && !OPT_force
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001247 ) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001248 if (OPT_repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001249 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001250 return 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001251 } else if (OPT_force)
Rob Landley8a625492006-04-29 20:05:17 +00001252 printf("Forcing filesystem check on %s\n", device_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001253 else if (OPT_repair)
Rob Landley8a625492006-04-29 20:05:17 +00001254 printf("Filesystem on %s is dirty, needs checking\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001255 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001256
1257 read_tables();
1258
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001259 if (OPT_manual) {
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001260 tcgetattr(0, &sv_termios);
1261 tmp = sv_termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001262 tmp.c_lflag &= ~(ICANON | ECHO);
Denis Vlasenko202ac502008-11-05 13:20:58 +00001263 tcsetattr_stdin_TCSANOW(&tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001264 termios_set = 1;
1265 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001266
Eric Andersene494fdd1999-10-19 20:23:03 +00001267 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001268 check_root2();
1269 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001270 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001271 check_root();
1272 check();
1273 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001274
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001275 if (OPT_verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001276 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001277
Eric Andersen1ca20a72001-03-21 07:34:27 +00001278 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001279 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001280 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001281 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001282 100 * (INODES - free_cnt) / INODES);
Eric Andersen1ca20a72001-03-21 07:34:27 +00001283 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001284 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001285 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001286 printf("%6u zones used (%u%%)\n\n"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001287 "%6u regular files\n"
1288 "%6u directories\n"
1289 "%6u character device files\n"
1290 "%6u block device files\n"
1291 "%6u links\n"
1292 "%6u symbolic links\n"
1293 "------\n"
1294 "%6u files\n",
1295 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1296 regular, directory, chardev, blockdev,
1297 links - 2 * directory + 1, symlinks,
1298 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001299 }
1300 if (changed) {
1301 write_tables();
Rob Landleyfd8409f2006-06-13 15:31:39 +00001302 printf("FILE SYSTEM HAS BEEN CHANGED\n");
Rob Landley8a625492006-04-29 20:05:17 +00001303 sync();
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001304 } else if (OPT_repair)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +00001305 write_superblock();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001306
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001307 if (OPT_manual)
Denis Vlasenko202ac502008-11-05 13:20:58 +00001308 tcsetattr_stdin_TCSANOW(&sv_termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001309
1310 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001311 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001312 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001313 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001314 return retcode;
1315}