blob: 8c2b7d8de7c93078fc30d4bb342304bafc08d7f7 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010088//config:config FSCK_MINIX
89//config: bool "fsck_minix"
90//config: default y
91//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020092//config: The minix filesystem is a nice, small, compact, read-write filesystem
93//config: with little overhead. It is not a journaling filesystem however and
94//config: can experience corruption if it is not properly unmounted or if the
95//config: power goes off in the middle of a write. This utility allows you to
96//config: check for and attempt to repair any corruption that occurs to a minix
97//config: filesystem.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010098
99//applet:IF_FSCK_MINIX(APPLET_ODDNAME(fsck.minix, fsck_minix, BB_DIR_SBIN, BB_SUID_DROP, fsck_minix))
100
101//kbuild:lib-$(CONFIG_FSCK_MINIX) += fsck_minix.o
Eric Andersene494fdd1999-10-19 20:23:03 +0000102
Pere Orga5bc8c002011-04-11 03:29:49 +0200103//usage:#define fsck_minix_trivial_usage
104//usage: "[-larvsmf] BLOCKDEV"
105//usage:#define fsck_minix_full_usage "\n\n"
106//usage: "Check MINIX filesystem\n"
Pere Orga5bc8c002011-04-11 03:29:49 +0200107//usage: "\n -l List all filenames"
108//usage: "\n -r Perform interactive repairs"
109//usage: "\n -a Perform automatic repairs"
110//usage: "\n -v Verbose"
111//usage: "\n -s Output superblock information"
112//usage: "\n -m Show \"mode not cleared\" warnings"
113//usage: "\n -f Force file system check"
114
Rob Landleycc9962d2006-09-14 05:27:28 +0000115#include <mntent.h>
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000116#include "libbb.h"
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000117#include "minix.h"
Eric Andersen85c55202000-07-10 22:46:55 +0000118
Eric Andersen85c55202000-07-10 22:46:55 +0000119#ifndef BLKGETSIZE
120#define BLKGETSIZE _IO(0x12,96) /* return device size */
121#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000122
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000123struct BUG_bad_inode_size {
124 char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
125#if ENABLE_FEATURE_MINIX2
126 char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
127#endif
128};
129
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000130enum {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000131#ifdef UNUSED
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000132 MINIX1_LINK_MAX = 250,
133 MINIX2_LINK_MAX = 65530,
134 MINIX_I_MAP_SLOTS = 8,
135 MINIX_Z_MAP_SLOTS = 64,
136 MINIX_V1 = 0x0001, /* original minix fs */
137 MINIX_V2 = 0x0002, /* minix V2 fs */
Eric Andersene494fdd1999-10-19 20:23:03 +0000138#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000139 MINIX_NAME_MAX = 255, /* # chars in a file name */
140};
Eric Andersene494fdd1999-10-19 20:23:03 +0000141
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000142
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000143#if !ENABLE_FEATURE_MINIX2
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000144enum { version2 = 0 };
145#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000146
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000147enum { MAX_DEPTH = 32 };
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000148
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +0000149enum { dev_fd = 3 };
150
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000151struct globals {
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000152#if ENABLE_FEATURE_MINIX2
153 smallint version2;
154#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000155 smallint changed; /* is filesystem modified? */
156 smallint errors_uncorrected; /* flag if some error was not corrected */
157 smallint termios_set;
158 smallint dirsize;
159 smallint namelen;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000160 const char *device_name;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000161 int directory, regular, blockdev, chardev, links, symlinks, total;
162 char *inode_buffer;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000163
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000164 char *inode_map;
165 char *zone_map;
Eric Andersene494fdd1999-10-19 20:23:03 +0000166
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000167 unsigned char *inode_count;
168 unsigned char *zone_count;
Eric Andersene494fdd1999-10-19 20:23:03 +0000169
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000170 /* File-name data */
171 int name_depth;
172 char *name_component[MAX_DEPTH+1];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000173
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000174 /* Bigger stuff */
175 struct termios sv_termios;
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000176 char superblock_buffer[BLOCK_SIZE];
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000177 char add_zone_ind_blk[BLOCK_SIZE];
178 char add_zone_dind_blk[BLOCK_SIZE];
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000179 IF_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
Denis Vlasenkofad03bc2007-03-13 22:31:28 +0000180 char check_file_blk[BLOCK_SIZE];
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000181
182 /* File-name data */
183 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
184};
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000185#define G (*ptr_to_globals)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000186#if ENABLE_FEATURE_MINIX2
187#define version2 (G.version2 )
188#endif
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000189#define changed (G.changed )
190#define errors_uncorrected (G.errors_uncorrected )
191#define termios_set (G.termios_set )
192#define dirsize (G.dirsize )
193#define namelen (G.namelen )
194#define device_name (G.device_name )
195#define directory (G.directory )
196#define regular (G.regular )
197#define blockdev (G.blockdev )
198#define chardev (G.chardev )
199#define links (G.links )
200#define symlinks (G.symlinks )
201#define total (G.total )
202#define inode_buffer (G.inode_buffer )
203#define inode_map (G.inode_map )
204#define zone_map (G.zone_map )
205#define inode_count (G.inode_count )
206#define zone_count (G.zone_count )
207#define name_depth (G.name_depth )
208#define name_component (G.name_component )
209#define sv_termios (G.sv_termios )
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000210#define superblock_buffer (G.superblock_buffer )
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000211#define add_zone_ind_blk (G.add_zone_ind_blk )
212#define add_zone_dind_blk (G.add_zone_dind_blk )
213#define add_zone_tind_blk (G.add_zone_tind_blk )
214#define check_file_blk (G.check_file_blk )
215#define current_name (G.current_name )
216#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000217 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000218 dirsize = 16; \
219 namelen = 14; \
220 current_name[0] = '/'; \
221 /*current_name[1] = '\0';*/ \
222 name_component[0] = &current_name[0]; \
223} while (0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000225
226#define OPTION_STR "larvsmf"
227enum {
228 OPT_l = (1 << 0),
229 OPT_a = (1 << 1),
230 OPT_r = (1 << 2),
231 OPT_v = (1 << 3),
232 OPT_s = (1 << 4),
233 OPT_w = (1 << 5),
234 OPT_f = (1 << 6),
235};
236#define OPT_list (option_mask32 & OPT_l)
237#define OPT_automatic (option_mask32 & OPT_a)
238#define OPT_repair (option_mask32 & OPT_r)
239#define OPT_verbose (option_mask32 & OPT_v)
240#define OPT_show (option_mask32 & OPT_s)
241#define OPT_warn_mode (option_mask32 & OPT_w)
242#define OPT_force (option_mask32 & OPT_f)
243/* non-automatic repairs requested? */
244#define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
245
246
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000247#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
248#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
249
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000250#define Super (*(struct minix_superblock *)(superblock_buffer))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000251
252#if ENABLE_FEATURE_MINIX2
253# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000254#else
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000255# define ZONES ((unsigned)(Super.s_nzones))
Eric Andersene494fdd1999-10-19 20:23:03 +0000256#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000257#define INODES ((unsigned)Super.s_ninodes)
258#define IMAPS ((unsigned)Super.s_imap_blocks)
259#define ZMAPS ((unsigned)Super.s_zmap_blocks)
260#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
261#define ZONESIZE ((unsigned)Super.s_log_zone_size)
262#define MAXSIZE ((unsigned)Super.s_max_size)
263#define MAGIC (Super.s_magic)
264
265/* gcc likes this more (code is smaller) than macro variant */
Denis Vlasenko3ad5d0c2007-06-12 20:54:54 +0000266static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000267{
Denis Vlasenko150f4022007-01-13 21:06:21 +0000268 return (size + n-1) / n;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000269}
270
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000271#if !ENABLE_FEATURE_MINIX2
272#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000273#else
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000274#define INODE_BLOCKS div_roundup(INODES, \
275 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000276#endif
277
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000278#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
279#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
Eric Andersene494fdd1999-10-19 20:23:03 +0000280
Denis Vlasenkocc244192007-02-06 00:37:40 +0000281/* Before you ask "where they come from?": */
282/* setbit/clrbit are supplied by sys/param.h */
283
284static int minix_bit(const char *a, unsigned i)
Eric Andersen56f3e352000-09-19 21:13:55 +0000285{
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000286 return (a[i >> 3] & (1<<(i & 7)));
Eric Andersen56f3e352000-09-19 21:13:55 +0000287}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000288
Denis Vlasenkocc244192007-02-06 00:37:40 +0000289static void minix_setbit(char *a, unsigned i)
290{
291 setbit(a, i);
292 changed = 1;
293}
294static void minix_clrbit(char *a, unsigned i)
295{
296 clrbit(a, i);
297 changed = 1;
298}
Denis Vlasenkodc485c92007-02-06 00:36:53 +0000299
300/* Note: do not assume 0/1, it is 0/nonzero */
Denis Vlasenkocc244192007-02-06 00:37:40 +0000301#define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
302#define inode_in_use(x) (minix_bit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000303
Denis Vlasenkocc244192007-02-06 00:37:40 +0000304#define mark_inode(x) (minix_setbit(inode_map,(x)))
305#define unmark_inode(x) (minix_clrbit(inode_map,(x)))
Eric Andersene494fdd1999-10-19 20:23:03 +0000306
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000307#define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
308#define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000309
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000310
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000311static void recursive_check(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000312#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000313static void recursive_check2(unsigned ino);
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000314#endif
315
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000316static void die(const char *str) NORETURN;
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000317static void die(const char *str)
Eric Andersene494fdd1999-10-19 20:23:03 +0000318{
319 if (termios_set)
Denis Vlasenko202ac502008-11-05 13:20:58 +0000320 tcsetattr_stdin_TCSANOW(&sv_termios);
Denis Vlasenko75fa4eb2007-01-03 01:36:42 +0000321 bb_error_msg_and_die("%s", str);
Eric Andersene494fdd1999-10-19 20:23:03 +0000322}
323
Rob Landley3ea66a82006-04-29 20:08:40 +0000324static void push_filename(const char *name)
325{
326 // /dir/dir/dir/file
327 // ^ ^ ^
328 // [0] [1] [2] <-name_component[i]
329 if (name_depth < MAX_DEPTH) {
330 int len;
331 char *p = name_component[name_depth];
332 *p++ = '/';
333 len = sprintf(p, "%.*s", namelen, name);
334 name_component[name_depth + 1] = p + len;
335 }
336 name_depth++;
337}
338
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000339static void pop_filename(void)
340{
Rob Landley3ea66a82006-04-29 20:08:40 +0000341 name_depth--;
342 if (name_depth < MAX_DEPTH) {
343 *name_component[name_depth] = '\0';
344 if (!name_depth) {
345 current_name[0] = '/';
346 current_name[1] = '\0';
347 }
348 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000349}
350
Erik Andersene49d5ec2000-02-08 19:58:47 +0000351static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000352{
353 int c;
354
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000355 if (!OPT_repair) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000356 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000357 errors_uncorrected = 1;
358 return 0;
359 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000360 if (OPT_automatic) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000361 bb_putchar('\n');
Eric Andersene494fdd1999-10-19 20:23:03 +0000362 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000363 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000364 return def;
365 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000367 for (;;) {
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100368 fflush_all();
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000369 c = getchar();
370 if (c == EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000371 if (!def)
372 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000373 return def;
374 }
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100375 if (c == '\n')
376 break;
377 c |= 0x20; /* tolower */
378 if (c == 'y') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000379 def = 1;
380 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100381 }
382 if (c == 'n') {
Eric Andersene494fdd1999-10-19 20:23:03 +0000383 def = 0;
384 break;
Denys Vlasenko85ca3272009-10-29 13:27:31 +0100385 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000386 }
387 if (def)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200388 puts("y");
Eric Andersene494fdd1999-10-19 20:23:03 +0000389 else {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200390 puts("n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000391 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000392 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000393 return def;
394}
395
396/*
397 * Make certain that we aren't checking a filesystem that is on a
398 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
399 * 1994 Theodore Ts'o. Also licensed under GPL.
400 */
401static void check_mount(void)
402{
Denys Vlasenko6ae64262009-07-18 16:22:26 +0200403 if (find_mount_point(device_name, 0)) {
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200404 int cont;
405#if ENABLE_FEATURE_MTAB_SUPPORT
406 /*
407 * If the root is mounted read-only, then /etc/mtab is
408 * probably not correct; so we won't issue a warning based on
409 * it.
410 */
411 int fd = open(bb_path_mtab_file, O_RDWR);
Eric Andersene494fdd1999-10-19 20:23:03 +0000412
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200413 if (fd < 0 && errno == EROFS)
414 return;
415 close(fd);
416#endif
417 printf("%s is mounted. ", device_name);
418 cont = 0;
419 if (isatty(0) && isatty(1))
420 cont = ask("Do you really want to continue", 0);
421 if (!cont) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200422 puts("Check aborted");
Denys Vlasenko09e63bb2009-07-05 04:50:36 +0200423 exit(EXIT_SUCCESS);
424 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000425 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000426}
427
428/*
429 * check_zone_nr checks to see that *nr is a valid zone nr. If it
430 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
431 * if an error was corrected, and returns the zone (0 for no zone
432 * or a bad zone-number).
433 */
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000434static int check_zone_nr2(uint32_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000435{
Rob Landley3ea66a82006-04-29 20:08:40 +0000436 const char *msg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000437 if (!*nr)
438 return 0;
439 if (*nr < FIRSTZONE)
Rob Landley3ea66a82006-04-29 20:08:40 +0000440 msg = "< FIRSTZONE";
Eric Andersene494fdd1999-10-19 20:23:03 +0000441 else if (*nr >= ZONES)
Rob Landley3ea66a82006-04-29 20:08:40 +0000442 msg = ">= ZONES";
Eric Andersene494fdd1999-10-19 20:23:03 +0000443 else
444 return *nr;
Rob Landleyfd8409f2006-06-13 15:31:39 +0000445 printf("Zone nr %s in file '%s'. ", msg, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000447 *nr = 0;
448 *corrected = 1;
449 }
450 return 0;
451}
452
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000453static int check_zone_nr(uint16_t *nr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000454{
Rob Landley8a625492006-04-29 20:05:17 +0000455 uint32_t nr32 = *nr;
456 int r = check_zone_nr2(&nr32, corrected);
457 *nr = (uint16_t)nr32;
458 return r;
Eric Andersene494fdd1999-10-19 20:23:03 +0000459}
Eric Andersene494fdd1999-10-19 20:23:03 +0000460
461/*
462 * read-block reads block nr into the buffer at addr.
463 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000464static void read_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000465{
466 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000468 return;
469 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000470 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
471 if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
472 printf("%s: bad block %u in file '%s'\n",
473 bb_msg_read_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 errors_uncorrected = 1;
Rob Landley8a625492006-04-29 20:05:17 +0000475 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000476 }
477}
478
479/*
480 * write_block writes block nr to disk.
481 */
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000482static void write_block(unsigned nr, void *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000483{
484 if (!nr)
485 return;
486 if (nr < FIRSTZONE || nr >= ZONES) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200487 puts("Internal error: trying to write bad block\n"
488 "Write request ignored");
Eric Andersene494fdd1999-10-19 20:23:03 +0000489 errors_uncorrected = 1;
490 return;
491 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000492 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
493 if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
494 printf("%s: bad block %u in file '%s'\n",
495 bb_msg_write_error, nr, current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000496 errors_uncorrected = 1;
497 }
498}
499
500/*
Rob Landley8a625492006-04-29 20:05:17 +0000501 * map_block calculates the absolute block nr of a block in a file.
Eric Andersene494fdd1999-10-19 20:23:03 +0000502 * It sets 'changed' if the inode has needed changing, and re-writes
503 * any indirect blocks with errors.
504 */
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000505static int map_block(struct minix1_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000506{
Rob Landley8a625492006-04-29 20:05:17 +0000507 uint16_t ind[BLOCK_SIZE >> 1];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000508 int block, result;
509 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000510
Erik Andersene49d5ec2000-02-08 19:58:47 +0000511 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000512 return check_zone_nr(inode->i_zone + blknr, &changed);
513 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000515 block = check_zone_nr(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000516 goto common;
Eric Andersene494fdd1999-10-19 20:23:03 +0000517 }
518 blknr -= 512;
519 block = check_zone_nr(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000520 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000521 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000522 result = check_zone_nr(&ind[blknr / 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000523 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000524 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000525 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000526 common:
527 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000528 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000529 result = check_zone_nr(&ind[blknr % 512], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000530 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000531 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000532 return result;
533}
534
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000535#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000536static int map_block2(struct minix2_inode *inode, unsigned blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000537{
Rob Landley8a625492006-04-29 20:05:17 +0000538 uint32_t ind[BLOCK_SIZE >> 2];
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000539 int block, result;
540 smallint blk_chg;
Eric Andersene494fdd1999-10-19 20:23:03 +0000541
542 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000543 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000544 blknr -= 7;
545 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000546 block = check_zone_nr2(inode->i_zone + 7, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000547 goto common2;
Eric Andersene494fdd1999-10-19 20:23:03 +0000548 }
549 blknr -= 256;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000550 if (blknr < 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 block = check_zone_nr2(inode->i_zone + 8, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000552 goto common1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000553 }
554 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000555 block = check_zone_nr2(inode->i_zone + 9, &changed);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000556 read_block(block, ind); /* triple indirect */
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 * 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 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000562 common1:
563 read_block(block, ind); /* double indirect */
Eric Andersene494fdd1999-10-19 20:23:03 +0000564 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000565 result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000566 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000567 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000568 block = result;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000569 common2:
570 read_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000571 blk_chg = 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000572 result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000573 if (blk_chg)
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000574 write_block(block, ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 return result;
576}
577#endif
578
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000579static void write_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000580{
581 /*
582 * Set the state of the filesystem based on whether or not there
583 * are uncorrected errors. The filesystem valid flag is
584 * unconditionally set if we get this far.
585 */
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000586 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
587 if (!errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000588 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000590 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000591 if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100592 die("can't write superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000593}
594
Eric Andersene674eb71999-10-19 20:52:57 +0000595static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000596{
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000597 write_superblock();
Eric Andersene494fdd1999-10-19 20:23:03 +0000598
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000599 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100600 die("can't write inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000601 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100602 die("can't write zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000603 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100604 die("can't write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000605}
606
Erik Andersene49d5ec2000-02-08 19:58:47 +0000607static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000608{
609 int block;
610 char blk[BLOCK_SIZE];
611 int size;
612
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000613#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000614 if (version2)
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000615 block = Inode2[MINIX_ROOT_INO].i_zone[0];
Eric Andersene494fdd1999-10-19 20:23:03 +0000616 else
617#endif
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000618 block = Inode1[MINIX_ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000619 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000621 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000622 dirsize = size;
623 namelen = size - 2;
624 return;
625 }
626 }
627 /* use defaults */
628}
629
Eric Andersene674eb71999-10-19 20:52:57 +0000630static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000631{
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000632 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000633 if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100634 die("can't read superblock");
Rob Landley3ea66a82006-04-29 20:08:40 +0000635 /* already initialized to:
Rob Landley8a625492006-04-29 20:05:17 +0000636 namelen = 14;
637 dirsize = 16;
638 version2 = 0;
Rob Landley3ea66a82006-04-29 20:08:40 +0000639 */
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000640 if (MAGIC == MINIX1_SUPER_MAGIC) {
641 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000642 namelen = 30;
643 dirsize = 32;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000644#if ENABLE_FEATURE_MINIX2
Eric Andersene494fdd1999-10-19 20:23:03 +0000645 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000646 version2 = 1;
647 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
648 namelen = 30;
649 dirsize = 32;
650 version2 = 1;
651#endif
652 } else
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000653 die("bad magic number in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000655 die("only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000656 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000657 die("bad s_imap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000658 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +0000659 die("bad s_zmap_blocks field in superblock");
Eric Andersene494fdd1999-10-19 20:23:03 +0000660}
661
Eric Andersene674eb71999-10-19 20:52:57 +0000662static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000663{
Rob Landley9a1a9de2006-06-13 15:12:14 +0000664 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
665 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
Erik Andersen0d068a22000-03-21 22:32:57 +0000666 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
667 inode_count = xmalloc(INODES + 1);
668 zone_count = xmalloc(ZONES);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000669 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100670 die("can't read inode map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000671 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100672 die("can't read zone map");
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000673 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100674 die("can't read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000675 if (NORM_FIRSTZONE != FIRSTZONE) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200676 puts("warning: firstzone!=norm_firstzone");
Eric Andersene494fdd1999-10-19 20:23:03 +0000677 errors_uncorrected = 1;
678 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000679 get_dirsize();
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000680 if (OPT_show) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000681 printf("%u inodes\n"
682 "%u blocks\n"
683 "Firstdatazone=%u (%u)\n"
684 "Zonesize=%u\n"
685 "Maxsize=%u\n"
686 "Filesystem state=%u\n"
687 "namelen=%u\n\n",
Rob Landley8a625492006-04-29 20:05:17 +0000688 INODES,
689 ZONES,
690 FIRSTZONE, NORM_FIRSTZONE,
691 BLOCK_SIZE << ZONESIZE,
692 MAXSIZE,
693 Super.s_state,
694 namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000695 }
696}
697
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000698static void get_inode_common(unsigned nr, uint16_t i_mode)
Eric Andersene494fdd1999-10-19 20:23:03 +0000699{
Eric Andersene494fdd1999-10-19 20:23:03 +0000700 total++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000701 if (!inode_count[nr]) {
702 if (!inode_in_use(nr)) {
Denys Vlasenko327f5502013-11-29 16:45:45 +0100703 printf("Inode %u is marked as 'unused', but it is used "
Rob Landleyfd8409f2006-06-13 15:31:39 +0000704 "for file '%s'\n", nr, current_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000705 if (OPT_repair) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000706 if (ask("Mark as 'in use'", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000707 mark_inode(nr);
Denis Vlasenkocc244192007-02-06 00:37:40 +0000708 else
709 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000710 }
711 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000712 if (S_ISDIR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000713 directory++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000714 else if (S_ISREG(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000715 regular++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000716 else if (S_ISCHR(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000717 chardev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000718 else if (S_ISBLK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 blockdev++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000720 else if (S_ISLNK(i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000721 symlinks++;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000722 else if (S_ISSOCK(i_mode));
723 else if (S_ISFIFO(i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000724 else {
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000725 printf("%s has mode %05o\n", current_name, i_mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000726 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000727 } else
728 links++;
729 if (!++inode_count[nr]) {
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200730 puts("Warning: inode count too big");
Eric Andersene494fdd1999-10-19 20:23:03 +0000731 inode_count[nr]--;
732 errors_uncorrected = 1;
733 }
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000734}
735
736static struct minix1_inode *get_inode(unsigned nr)
737{
738 struct minix1_inode *inode;
739
740 if (!nr || nr > INODES)
741 return NULL;
742 inode = Inode1 + nr;
743 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000744 return inode;
745}
746
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000747#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000748static struct minix2_inode *get_inode2(unsigned nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000749{
750 struct minix2_inode *inode;
751
752 if (!nr || nr > INODES)
753 return NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000754 inode = Inode2 + nr;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000755 get_inode_common(nr, inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000756 return inode;
757}
758#endif
759
Eric Andersene674eb71999-10-19 20:52:57 +0000760static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000761{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000762 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000763
764 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000765 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000766}
767
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000768#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000769static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000770{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000771 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000772
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 if (!inode || !S_ISDIR(inode->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000774 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000775}
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000776#else
777void check_root2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +0000778#endif
779
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000780static int add_zone_common(int block, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000781{
Eric Andersene494fdd1999-10-19 20:23:03 +0000782 if (!block)
783 return 0;
784 if (zone_count[block]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000785 printf("Already used block is reused in file '%s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000786 current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000787 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000788 block = 0;
789 *corrected = 1;
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000790 return -1; /* "please zero out *znr" */
Eric Andersene494fdd1999-10-19 20:23:03 +0000791 }
792 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000793 if (!zone_in_use(block)) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000794 printf("Block %d in file '%s' is marked as 'unused'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000795 block, current_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000796 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000797 mark_zone(block);
798 }
799 if (!++zone_count[block])
800 zone_count[block]--;
801 return block;
802}
803
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000804static int add_zone(uint16_t *znr, smallint *corrected)
805{
806 int block;
807
808 block = check_zone_nr(znr, corrected);
809 block = add_zone_common(block, corrected);
810 if (block == -1) {
811 *znr = 0;
812 block = 0;
813 }
814 return block;
815}
816
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000817#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000818static int add_zone2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000819{
Eric Andersene494fdd1999-10-19 20:23:03 +0000820 int block;
821
Erik Andersene49d5ec2000-02-08 19:58:47 +0000822 block = check_zone_nr2(znr, corrected);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000823 block = add_zone_common(block, corrected);
824 if (block == -1) {
825 *znr = 0;
826 block = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000827 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000828 return block;
829}
830#endif
831
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000832static void add_zone_ind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000833{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000834 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000835 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000836 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000837
838 block = add_zone(znr, corrected);
839 if (!block)
840 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000841 read_block(block, add_zone_ind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000842 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000843 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000844 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000845 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000846}
847
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000848#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000849static void add_zone_ind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000850{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000851 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000852 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000853 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000854
Erik Andersene49d5ec2000-02-08 19:58:47 +0000855 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000856 if (!block)
857 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000858 read_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000859 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000860 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000861 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000862 write_block(block, add_zone_ind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000863}
864#endif
865
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000866static void add_zone_dind(uint16_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000867{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000868 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000869 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000870 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000871
872 block = add_zone(znr, corrected);
873 if (!block)
874 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000875 read_block(block, add_zone_dind_blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000876 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000877 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000878 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000879 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000880}
881
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000882#if ENABLE_FEATURE_MINIX2
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000883static void add_zone_dind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000884{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000885 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000886 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000887 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000888
Erik Andersene49d5ec2000-02-08 19:58:47 +0000889 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000890 if (!block)
891 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000892 read_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000893 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000894 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000895 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000896 write_block(block, add_zone_dind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000897}
898
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000899static void add_zone_tind2(uint32_t *znr, smallint *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000900{
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000901 int i;
Eric Andersene494fdd1999-10-19 20:23:03 +0000902 int block;
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000903 smallint chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000904
Erik Andersene49d5ec2000-02-08 19:58:47 +0000905 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000906 if (!block)
907 return;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000908 read_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000909 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000910 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +0000911 if (chg_blk)
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000912 write_block(block, add_zone_tind_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000913}
914#endif
915
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000916static void check_zones(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000917{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000918 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000919
920 if (!i || i > INODES)
921 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000922 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000923 return;
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000924 inode = Inode1 + i;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100925 if (!S_ISDIR(inode->i_mode)
926 && !S_ISREG(inode->i_mode)
927 && !S_ISLNK(inode->i_mode)
928 ) {
929 return;
930 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000931 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000932 add_zone(i + inode->i_zone, &changed);
933 add_zone_ind(7 + inode->i_zone, &changed);
934 add_zone_dind(8 + inode->i_zone, &changed);
935}
936
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000937#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000938static void check_zones2(unsigned i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000939{
940 struct minix2_inode *inode;
941
942 if (!i || i > INODES)
943 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000944 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000945 return;
946 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
948 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000949 return;
950 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000951 add_zone2(i + inode->i_zone, &changed);
952 add_zone_ind2(7 + inode->i_zone, &changed);
953 add_zone_dind2(8 + inode->i_zone, &changed);
954 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000955}
956#endif
957
Denis Vlasenko73464ce2007-01-03 00:45:05 +0000958static void check_file(struct minix1_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000959{
Denis Vlasenkoaa959592007-01-03 00:43:19 +0000960 struct minix1_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000961 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000962 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000963 int block;
964
Erik Andersene49d5ec2000-02-08 19:58:47 +0000965 block = map_block(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000966 read_block(block, check_file_blk);
967 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +0000968 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000969 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +0000970 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +0000971 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +0000972 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +0000973 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +0000974 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000975 }
976 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000977 }
Rob Landley3ea66a82006-04-29 20:08:40 +0000978 push_filename(name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000979 inode = get_inode(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +0000980 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +0000981 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000982 if (inode && LONE_CHAR(name, '.'))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000983 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000984 printf("%s: bad directory: '.' isn't first\n", current_name);
985 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000986 }
987 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000988 if (inode && strcmp("..", name) == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +0000990 printf("%s: bad directory: '..' isn't second\n", current_name);
991 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000992 }
993 if (!inode)
994 return;
Rob Landley3ea66a82006-04-29 20:08:40 +0000995 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +0000996 if (OPT_list) {
997 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000998 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +0000999 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001000 }
1001 check_zones(ino);
1002 if (inode && S_ISDIR(inode->i_mode))
1003 recursive_check(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001004 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001005}
1006
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001007#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001008static void check_file2(struct minix2_inode *dir, unsigned offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001009{
Eric Andersene494fdd1999-10-19 20:23:03 +00001010 struct minix2_inode *inode;
1011 int ino;
1012 char *name;
1013 int block;
1014
Erik Andersene49d5ec2000-02-08 19:58:47 +00001015 block = map_block2(dir, offset / BLOCK_SIZE);
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001016 read_block(block, check_file_blk);
1017 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
Rob Landley8a625492006-04-29 20:05:17 +00001018 ino = *(uint16_t *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +00001019 if (ino > INODES) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001020 printf("%s contains a bad inode number for file '%.*s'. ",
Rob Landley3ea66a82006-04-29 20:08:40 +00001021 current_name, namelen, name);
Rob Landleyfd8409f2006-06-13 15:31:39 +00001022 if (ask("Remove", 1)) {
Rob Landley8a625492006-04-29 20:05:17 +00001023 *(uint16_t *) (name - 2) = 0;
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001024 write_block(block, check_file_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001025 }
1026 ino = 0;
1027 }
Rob Landley3ea66a82006-04-29 20:08:40 +00001028 push_filename(name);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 inode = get_inode2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001030 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001031 if (!offset) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001032 if (inode && LONE_CHAR(name, '.'))
Eric Andersene494fdd1999-10-19 20:23:03 +00001033 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001034 printf("%s: bad directory: '.' isn't first\n", current_name);
1035 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001036 }
1037 if (offset == dirsize) {
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001038 if (inode && strcmp("..", name) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +00001039 return;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001040 printf("%s: bad directory: '..' isn't second\n", current_name);
1041 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001042 }
1043 if (!inode)
1044 return;
Rob Landley3ea66a82006-04-29 20:08:40 +00001045 push_filename(name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001046 if (OPT_list) {
1047 if (OPT_verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001048 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Rob Landley3ea66a82006-04-29 20:08:40 +00001049 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
Eric Andersene494fdd1999-10-19 20:23:03 +00001050 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001051 check_zones2(ino);
1052 if (inode && S_ISDIR(inode->i_mode))
1053 recursive_check2(ino);
Rob Landley3ea66a82006-04-29 20:08:40 +00001054 pop_filename();
Eric Andersene494fdd1999-10-19 20:23:03 +00001055}
1056#endif
1057
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001058static void recursive_check(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001059{
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001060 struct minix1_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001061 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001062
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001063 dir = Inode1 + ino;
Eric Andersene494fdd1999-10-19 20:23:03 +00001064 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001065 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001066 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001067 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001068 errors_uncorrected = 1;
1069 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001070 for (offset = 0; offset < dir->i_size; offset += dirsize)
1071 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001072}
1073
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001074#if ENABLE_FEATURE_MINIX2
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001075static void recursive_check2(unsigned ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001076{
1077 struct minix2_inode *dir;
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001078 unsigned offset;
Eric Andersene494fdd1999-10-19 20:23:03 +00001079
1080 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001081 if (!S_ISDIR(dir->i_mode))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001082 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001083 if (dir->i_size < 2 * dirsize) {
Rob Landley3ea66a82006-04-29 20:08:40 +00001084 printf("%s: bad directory: size<32", current_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001085 errors_uncorrected = 1;
1086 }
1087 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001088 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001089}
1090#endif
1091
Eric Andersene674eb71999-10-19 20:52:57 +00001092static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001093{
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001094 char buffer[BLOCK_SIZE];
Eric Andersene494fdd1999-10-19 20:23:03 +00001095
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001096 xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
1097 return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
Eric Andersene494fdd1999-10-19 20:23:03 +00001098}
1099
Eric Andersene674eb71999-10-19 20:52:57 +00001100static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001101{
1102 int i;
1103
Erik Andersene49d5ec2000-02-08 19:58:47 +00001104 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001105 if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001106 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 if (ask("Clear", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001108 Inode1[i].i_mode = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +00001109 changed = 1;
1110 }
1111 }
1112 if (!inode_count[i]) {
1113 if (!inode_in_use(i))
1114 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001115 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001117 unmark_inode(i);
1118 continue;
1119 }
1120 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001121 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001122 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001123 mark_inode(i);
1124 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001125 if (Inode1[i].i_nlinks != inode_count[i]) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001126 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001127 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1128 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001129 if (ask("Set i_nlinks to count", 1)) {
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001130 Inode1[i].i_nlinks = inode_count[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001131 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001132 }
1133 }
1134 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001135 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001136 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001137 continue;
1138 if (!zone_count[i]) {
1139 if (bad_zone(i))
1140 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001141 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001142 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001143 unmark_zone(i);
1144 continue;
1145 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001146 printf("Zone %d: %sin use, counted=%d\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001147 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001148 }
1149}
1150
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001151#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001153{
1154 int i;
1155
1156 for (i = 1; i <= INODES; i++) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001157 if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001158 printf("Inode %d has non-zero mode. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001159 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001160 Inode2[i].i_mode = 0;
1161 changed = 1;
1162 }
1163 }
1164 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001165 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001166 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001167 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001168 if (ask("Clear", 1))
1169 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001170 continue;
1171 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001172 if (!inode_in_use(i)) {
Denis Vlasenko73464ce2007-01-03 00:45:05 +00001173 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001174 if (ask("Set", 1))
1175 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001176 }
1177 if (Inode2[i].i_nlinks != inode_count[i]) {
Rob Landleyfd8409f2006-06-13 15:31:39 +00001178 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001179 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1180 inode_count[i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001181 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001182 Inode2[i].i_nlinks = inode_count[i];
1183 changed = 1;
1184 }
1185 }
1186 }
1187 for (i = FIRSTZONE; i < ZONES; i++) {
Denis Vlasenkodc485c92007-02-06 00:36:53 +00001188 if ((zone_in_use(i) != 0) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001189 continue;
1190 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001191 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001192 continue;
Rob Landleyfd8409f2006-06-13 15:31:39 +00001193 printf("Zone %d is marked 'in use', but no file uses it. ", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001194 if (ask("Unmark", 1))
1195 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001196 continue;
1197 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001198 printf("Zone %d: %sin use, counted=%d\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001199 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001200 }
1201}
1202#endif
1203
Eric Andersene674eb71999-10-19 20:52:57 +00001204static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001205{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001206 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1207 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001208 check_zones(MINIX_ROOT_INO);
1209 recursive_check(MINIX_ROOT_INO);
Eric Andersene494fdd1999-10-19 20:23:03 +00001210 check_counts();
1211}
1212
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001213#if ENABLE_FEATURE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001214static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001215{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001216 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1217 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001218 check_zones2(MINIX_ROOT_INO);
1219 recursive_check2(MINIX_ROOT_INO);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001220 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001221}
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001222#else
1223void check2(void);
Eric Andersene494fdd1999-10-19 20:23:03 +00001224#endif
1225
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001226int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00001227int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001228{
Eric Andersene494fdd1999-10-19 20:23:03 +00001229 int retcode = 0;
1230
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001231 xfunc_error_retval = 8;
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001232
Denis Vlasenkoa6691312007-06-13 12:28:21 +00001233 INIT_G();
John Beppuc1dc5d92000-02-11 12:43:20 +00001234
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001235 opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
1236 getopt32(argv, OPTION_STR);
1237 argv += optind;
1238 device_name = argv[0];
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001239
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001240 check_mount(); /* trying to check a mounted filesystem? */
1241 if (OPT_manual) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001242 if (!isatty(0) || !isatty(1))
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001243 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001244 }
Denis Vlasenkoe06f61d2008-06-25 10:00:58 +00001245 xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001246
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001247 /*sync(); paranoia? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001248 read_superblock();
1249
1250 /*
1251 * Determine whether or not we should continue with the checking.
1252 * This is based on the status of the filesystem valid and error
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001253 * flags and whether or not the -f switch was specified on the
Eric Andersene494fdd1999-10-19 20:23:03 +00001254 * command line.
1255 */
Denis Vlasenkoca525b42007-06-13 12:27:17 +00001256 printf("%s: %s\n", applet_name, bb_banner);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001257
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001258 if (!(Super.s_state & MINIX_ERROR_FS)
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001259 && (Super.s_state & MINIX_VALID_FS) && !OPT_force
Denis Vlasenko24cfe8f2007-01-03 00:39:15 +00001260 ) {
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001261 if (OPT_repair)
Rob Landleyfd8409f2006-06-13 15:31:39 +00001262 printf("%s is clean, check is skipped\n", device_name);
Denis Vlasenko6dd392a2007-01-03 00:41:53 +00001263 return 0;
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001264 } else if (OPT_force)
Rob Landley8a625492006-04-29 20:05:17 +00001265 printf("Forcing filesystem check on %s\n", device_name);
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001266 else if (OPT_repair)
Rob Landley8a625492006-04-29 20:05:17 +00001267 printf("Filesystem on %s is dirty, needs checking\n",
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001268 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001269
1270 read_tables();
1271
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001272 if (OPT_manual) {
Denys Vlasenko01ccdd12017-01-11 16:17:59 +01001273 set_termios_to_raw(STDIN_FILENO, &sv_termios, 0);
Eric Andersene494fdd1999-10-19 20:23:03 +00001274 termios_set = 1;
1275 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001276
Eric Andersene494fdd1999-10-19 20:23:03 +00001277 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001278 check_root2();
1279 check2();
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001280 } else {
Eric Andersene494fdd1999-10-19 20:23:03 +00001281 check_root();
1282 check();
1283 }
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001284
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001285 if (OPT_verbose) {
Eric Andersen1ca20a72001-03-21 07:34:27 +00001286 int i, free_cnt;
Eric Andersene494fdd1999-10-19 20:23:03 +00001287
Eric Andersen1ca20a72001-03-21 07:34:27 +00001288 for (i = 1, free_cnt = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001289 if (!inode_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001290 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001291 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001292 100 * (INODES - free_cnt) / INODES);
Eric Andersen1ca20a72001-03-21 07:34:27 +00001293 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001294 if (!zone_in_use(i))
Eric Andersen1ca20a72001-03-21 07:34:27 +00001295 free_cnt++;
Denis Vlasenkoaa959592007-01-03 00:43:19 +00001296 printf("%6u zones used (%u%%)\n\n"
Denys Vlasenko60cb48c2013-01-14 15:57:44 +01001297 "%6u regular files\n"
1298 "%6u directories\n"
1299 "%6u character device files\n"
1300 "%6u block device files\n"
1301 "%6u links\n"
1302 "%6u symbolic links\n"
1303 "------\n"
1304 "%6u files\n",
1305 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1306 regular, directory, chardev, blockdev,
1307 links - 2 * directory + 1, symlinks,
1308 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001309 }
1310 if (changed) {
1311 write_tables();
Denys Vlasenkod60752f2015-10-07 22:42:45 +02001312 puts("FILE SYSTEM HAS BEEN CHANGED");
Rob Landley8a625492006-04-29 20:05:17 +00001313 sync();
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001314 } else if (OPT_repair)
Bernhard Reutner-Fischere1e51742008-07-21 13:33:22 +00001315 write_superblock();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001316
Denis Vlasenko9f153f62008-03-17 08:32:26 +00001317 if (OPT_manual)
Denis Vlasenko202ac502008-11-05 13:20:58 +00001318 tcsetattr_stdin_TCSANOW(&sv_termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001319
1320 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001321 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001322 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001323 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001324 return retcode;
1325}