Home | History | Annotate | Line # | Download | only in fsck_ext2fs
pass1.c revision 1.6
      1  1.6  bouyer /*	$NetBSD: pass1.c,v 1.6 1999/02/17 13:11:19 bouyer Exp $	*/
      2  1.1  bouyer 
      3  1.1  bouyer /*
      4  1.1  bouyer  * Copyright (c) 1997 Manuel Bouyer.
      5  1.1  bouyer  * Copyright (c) 1980, 1986, 1993
      6  1.1  bouyer  *	The Regents of the University of California.  All rights reserved.
      7  1.1  bouyer  *
      8  1.1  bouyer  * Redistribution and use in source and binary forms, with or without
      9  1.1  bouyer  * modification, are permitted provided that the following conditions
     10  1.1  bouyer  * are met:
     11  1.1  bouyer  * 1. Redistributions of source code must retain the above copyright
     12  1.1  bouyer  *    notice, this list of conditions and the following disclaimer.
     13  1.1  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  bouyer  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  bouyer  *    documentation and/or other materials provided with the distribution.
     16  1.1  bouyer  * 3. All advertising materials mentioning features or use of this software
     17  1.1  bouyer  *    must display the following acknowledgement:
     18  1.1  bouyer  *	This product includes software developed by the University of
     19  1.1  bouyer  *	California, Berkeley and its contributors.
     20  1.1  bouyer  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  bouyer  *    may be used to endorse or promote products derived from this software
     22  1.1  bouyer  *    without specific prior written permission.
     23  1.1  bouyer  *
     24  1.1  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  bouyer  * SUCH DAMAGE.
     35  1.1  bouyer  */
     36  1.1  bouyer 
     37  1.3   lukem #include <sys/cdefs.h>
     38  1.1  bouyer #ifndef lint
     39  1.1  bouyer #if 0
     40  1.1  bouyer static char sccsid[] = "@(#)pass1.c	8.1 (Berkeley) 6/5/93";
     41  1.1  bouyer #else
     42  1.6  bouyer __RCSID("$NetBSD: pass1.c,v 1.6 1999/02/17 13:11:19 bouyer Exp $");
     43  1.1  bouyer #endif
     44  1.1  bouyer #endif /* not lint */
     45  1.1  bouyer 
     46  1.1  bouyer #include <sys/param.h>
     47  1.1  bouyer #include <sys/time.h>
     48  1.1  bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
     49  1.1  bouyer #include <ufs/ext2fs/ext2fs_dir.h>
     50  1.1  bouyer #include <ufs/ext2fs/ext2fs.h>
     51  1.1  bouyer 
     52  1.1  bouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
     53  1.1  bouyer 
     54  1.1  bouyer #include <stdio.h>
     55  1.1  bouyer #include <stdlib.h>
     56  1.1  bouyer #include <string.h>
     57  1.5  kleink #include <time.h>
     58  1.1  bouyer 
     59  1.1  bouyer #include "fsck.h"
     60  1.1  bouyer #include "extern.h"
     61  1.1  bouyer #include "fsutil.h"
     62  1.1  bouyer 
     63  1.1  bouyer static daddr_t badblk;
     64  1.1  bouyer static daddr_t dupblk;
     65  1.1  bouyer static void checkinode __P((ino_t, struct inodesc *));
     66  1.1  bouyer 
     67  1.1  bouyer void
     68  1.1  bouyer pass1()
     69  1.1  bouyer {
     70  1.1  bouyer 	ino_t inumber;
     71  1.1  bouyer 	int c, i, cgd;
     72  1.1  bouyer 	struct inodesc idesc;
     73  1.1  bouyer 
     74  1.1  bouyer 	/*
     75  1.1  bouyer 	 * Set file system reserved blocks in used block map.
     76  1.1  bouyer 	 */
     77  1.1  bouyer 	for (c = 0; c < sblock.e2fs_ncg; c++) {
     78  1.1  bouyer 		i = c * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock;
     79  1.1  bouyer 		cgd = i + cgoverhead;
     80  1.1  bouyer 
     81  1.1  bouyer 		if (c == 0)
     82  1.1  bouyer 			i = 0;
     83  1.1  bouyer 		for (; i < cgd; i++)
     84  1.1  bouyer 			setbmap(i);
     85  1.1  bouyer 	}
     86  1.1  bouyer 
     87  1.1  bouyer 	/*
     88  1.1  bouyer 	 * Find all allocated blocks.
     89  1.1  bouyer 	 */
     90  1.1  bouyer 	memset(&idesc, 0, sizeof(struct inodesc));
     91  1.1  bouyer 	idesc.id_type = ADDR;
     92  1.1  bouyer 	idesc.id_func = pass1check;
     93  1.1  bouyer 	inumber = 1;
     94  1.1  bouyer 	n_files = n_blks = 0;
     95  1.1  bouyer 	resetinodebuf();
     96  1.1  bouyer 	for (c = 0; c < sblock.e2fs_ncg; c++) {
     97  1.1  bouyer 		for (i = 0;
     98  1.1  bouyer 			i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
     99  1.1  bouyer 			i++, inumber++) {
    100  1.1  bouyer 			if (inumber < EXT2_ROOTINO) /* XXX */
    101  1.1  bouyer 				continue;
    102  1.1  bouyer 			checkinode(inumber, &idesc);
    103  1.1  bouyer 		}
    104  1.1  bouyer 	}
    105  1.1  bouyer 	freeinodebuf();
    106  1.1  bouyer }
    107  1.1  bouyer 
    108  1.1  bouyer static void
    109  1.1  bouyer checkinode(inumber, idesc)
    110  1.1  bouyer 	ino_t inumber;
    111  1.3   lukem 	struct inodesc *idesc;
    112  1.1  bouyer {
    113  1.3   lukem 	struct ext2fs_dinode *dp;
    114  1.1  bouyer 	struct zlncnt *zlnp;
    115  1.1  bouyer 	int ndb, j;
    116  1.1  bouyer 	mode_t mode;
    117  1.1  bouyer 
    118  1.1  bouyer 	dp = getnextinode(inumber);
    119  1.1  bouyer 	if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
    120  1.1  bouyer 		return;
    121  1.1  bouyer 
    122  1.4  bouyer 	mode = fs2h16(dp->e2di_mode) & IFMT;
    123  1.1  bouyer 	if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
    124  1.1  bouyer 		if (mode == 0 && (
    125  1.1  bouyer 			memcmp(dp->e2di_blocks, zino.e2di_blocks,
    126  1.1  bouyer 			(NDADDR + NIADDR) * sizeof(u_int32_t)) ||
    127  1.1  bouyer 		    dp->e2di_mode || dp->e2di_size)) {
    128  1.1  bouyer 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    129  1.1  bouyer 			if (reply("CLEAR") == 1) {
    130  1.1  bouyer 				dp = ginode(inumber);
    131  1.1  bouyer 				clearinode(dp);
    132  1.1  bouyer 				inodirty();
    133  1.1  bouyer 			}
    134  1.1  bouyer 		}
    135  1.1  bouyer #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
    136  1.1  bouyer 		if (dp->e2di_dtime == 0) {
    137  1.1  bouyer 			pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber);
    138  1.1  bouyer 			if (preen) {
    139  1.1  bouyer 				printf(" (CORRECTED)\n");
    140  1.1  bouyer 			}
    141  1.1  bouyer 			if (preen || reply("CORRECT")) {
    142  1.1  bouyer 				time_t t;
    143  1.1  bouyer 				time(&t);
    144  1.4  bouyer 				dp->e2di_dtime = h2fs32(t);
    145  1.1  bouyer 				dp = ginode(inumber);
    146  1.1  bouyer 				inodirty();
    147  1.1  bouyer 			}
    148  1.1  bouyer 		}
    149  1.1  bouyer #endif
    150  1.1  bouyer 		statemap[inumber] = USTATE;
    151  1.1  bouyer 		return;
    152  1.1  bouyer 	}
    153  1.1  bouyer 	lastino = inumber;
    154  1.1  bouyer 	if (dp->e2di_dtime != 0) {
    155  1.4  bouyer 		time_t t = fs2h32(dp->e2di_dtime);
    156  1.1  bouyer 		char *p = ctime(&t);
    157  1.1  bouyer 		pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]);
    158  1.1  bouyer 		if (preen) {
    159  1.1  bouyer 			printf(" (CORRECTED)\n");
    160  1.1  bouyer 		}
    161  1.1  bouyer 		if (preen || reply("CORRECT")) {
    162  1.1  bouyer 			dp = ginode(inumber);
    163  1.1  bouyer 			dp->e2di_dtime = 0;
    164  1.1  bouyer 			inodirty();
    165  1.1  bouyer 		}
    166  1.1  bouyer 	}
    167  1.1  bouyer 	if (/* dp->di_size < 0 || */
    168  1.4  bouyer 	    fs2h32(dp->e2di_size) + sblock.e2fs_bsize - 1 <
    169  1.4  bouyer 		fs2h32(dp->e2di_size)) {
    170  1.1  bouyer 		if (debug)
    171  1.4  bouyer 			printf("bad size %lu:", (u_long)fs2h32(dp->e2di_size));
    172  1.1  bouyer 		goto unknown;
    173  1.1  bouyer 	}
    174  1.1  bouyer 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    175  1.1  bouyer 		dp = ginode(inumber);
    176  1.4  bouyer 		dp->e2di_size = h2fs32(sblock.e2fs_bsize);
    177  1.4  bouyer 		dp->e2di_mode = h2fs16(IFREG|0600);
    178  1.1  bouyer 		inodirty();
    179  1.1  bouyer 	}
    180  1.4  bouyer 	ndb = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
    181  1.1  bouyer 	if (ndb < 0) {
    182  1.1  bouyer 		if (debug)
    183  1.2   mikel 			printf("bad size %lu ndb %d:",
    184  1.4  bouyer 			    (u_long)fs2h32(dp->e2di_size), ndb);
    185  1.1  bouyer 		goto unknown;
    186  1.1  bouyer 	}
    187  1.1  bouyer 	if (mode == IFBLK || mode == IFCHR)
    188  1.1  bouyer 		ndb++;
    189  1.1  bouyer 	if (mode == IFLNK) {
    190  1.1  bouyer 		/*
    191  1.1  bouyer 		 * Fake ndb value so direct/indirect block checks below
    192  1.1  bouyer 		 * will detect any garbage after symlink string.
    193  1.1  bouyer 		 */
    194  1.4  bouyer 		if (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN ||
    195  1.1  bouyer 		    (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) {
    196  1.4  bouyer 			ndb = howmany(fs2h32(dp->e2di_size), sizeof(u_int32_t));
    197  1.1  bouyer 			if (ndb > NDADDR) {
    198  1.1  bouyer 				j = ndb - NDADDR;
    199  1.1  bouyer 				for (ndb = 1; j > 1; j--)
    200  1.1  bouyer 					ndb *= NINDIR(&sblock);
    201  1.1  bouyer 				ndb += NDADDR;
    202  1.1  bouyer 			}
    203  1.1  bouyer 		}
    204  1.1  bouyer 	}
    205  1.6  bouyer 	/* Linux puts things in blocks for FIFO, so skip this check */
    206  1.6  bouyer 	if (mode != IFIFO) {
    207  1.6  bouyer 		for (j = ndb; j < NDADDR; j++)
    208  1.6  bouyer 			if (dp->e2di_blocks[j] != 0) {
    209  1.6  bouyer 				if (debug)
    210  1.6  bouyer 					printf("bad direct addr: %d\n",
    211  1.6  bouyer 					    fs2h32(dp->e2di_blocks[j]));
    212  1.6  bouyer 				goto unknown;
    213  1.6  bouyer 			}
    214  1.6  bouyer 		for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    215  1.6  bouyer 			ndb /= NINDIR(&sblock);
    216  1.6  bouyer 		for (; j < NIADDR; j++) {
    217  1.6  bouyer 			if (dp->e2di_blocks[j+NDADDR] != 0) {
    218  1.6  bouyer 				if (debug)
    219  1.6  bouyer 					printf("bad indirect addr: %d\n",
    220  1.6  bouyer 					    fs2h32(dp->e2di_blocks[j+NDADDR]));
    221  1.6  bouyer 				goto unknown;
    222  1.6  bouyer 			}
    223  1.1  bouyer 		}
    224  1.6  bouyer 	}
    225  1.1  bouyer 	if (ftypeok(dp) == 0)
    226  1.1  bouyer 		goto unknown;
    227  1.1  bouyer 	n_files++;
    228  1.4  bouyer 	lncntp[inumber] = fs2h16(dp->e2di_nlink);
    229  1.4  bouyer 	if (dp->e2di_nlink == 0) {
    230  1.1  bouyer 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    231  1.1  bouyer 		if (zlnp == NULL) {
    232  1.1  bouyer 			pfatal("LINK COUNT TABLE OVERFLOW");
    233  1.1  bouyer 			if (reply("CONTINUE") == 0)
    234  1.1  bouyer 				errexit("%s\n", "");
    235  1.1  bouyer 		} else {
    236  1.1  bouyer 			zlnp->zlncnt = inumber;
    237  1.1  bouyer 			zlnp->next = zlnhead;
    238  1.1  bouyer 			zlnhead = zlnp;
    239  1.1  bouyer 		}
    240  1.1  bouyer 	}
    241  1.1  bouyer 	if (mode == IFDIR) {
    242  1.1  bouyer 		if (dp->e2di_size == 0)
    243  1.1  bouyer 			statemap[inumber] = DCLEAR;
    244  1.1  bouyer 		else
    245  1.1  bouyer 			statemap[inumber] = DSTATE;
    246  1.1  bouyer 		cacheino(dp, inumber);
    247  1.1  bouyer 	} else {
    248  1.1  bouyer 		statemap[inumber] = FSTATE;
    249  1.1  bouyer 	}
    250  1.1  bouyer 	badblk = dupblk = 0;
    251  1.1  bouyer 	idesc->id_number = inumber;
    252  1.1  bouyer 	(void)ckinode(dp, idesc);
    253  1.1  bouyer 	idesc->id_entryno *= btodb(sblock.e2fs_bsize);
    254  1.4  bouyer 	if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) {
    255  1.1  bouyer 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    256  1.4  bouyer 		    inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno);
    257  1.1  bouyer 		if (preen)
    258  1.1  bouyer 			printf(" (CORRECTED)\n");
    259  1.1  bouyer 		else if (reply("CORRECT") == 0)
    260  1.1  bouyer 			return;
    261  1.1  bouyer 		dp = ginode(inumber);
    262  1.4  bouyer 		dp->e2di_nblock = h2fs32(idesc->id_entryno);
    263  1.1  bouyer 		inodirty();
    264  1.1  bouyer 	}
    265  1.1  bouyer 	return;
    266  1.1  bouyer unknown:
    267  1.1  bouyer 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    268  1.1  bouyer 	statemap[inumber] = FCLEAR;
    269  1.1  bouyer 	if (reply("CLEAR") == 1) {
    270  1.1  bouyer 		statemap[inumber] = USTATE;
    271  1.1  bouyer 		dp = ginode(inumber);
    272  1.1  bouyer 		clearinode(dp);
    273  1.1  bouyer 		inodirty();
    274  1.1  bouyer 	}
    275  1.1  bouyer }
    276  1.1  bouyer 
    277  1.1  bouyer int
    278  1.1  bouyer pass1check(idesc)
    279  1.3   lukem 	struct inodesc *idesc;
    280  1.1  bouyer {
    281  1.1  bouyer 	int res = KEEPON;
    282  1.1  bouyer 	int anyout, nfrags;
    283  1.1  bouyer 	daddr_t blkno = idesc->id_blkno;
    284  1.3   lukem 	struct dups *dlp;
    285  1.1  bouyer 	struct dups *new;
    286  1.1  bouyer 
    287  1.1  bouyer 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    288  1.1  bouyer 		blkerror(idesc->id_number, "BAD", blkno);
    289  1.1  bouyer 		if (badblk++ >= MAXBAD) {
    290  1.1  bouyer 			pwarn("EXCESSIVE BAD BLKS I=%u",
    291  1.1  bouyer 				idesc->id_number);
    292  1.1  bouyer 			if (preen)
    293  1.1  bouyer 				printf(" (SKIPPING)\n");
    294  1.1  bouyer 			else if (reply("CONTINUE") == 0)
    295  1.1  bouyer 				errexit("%s\n", "");
    296  1.1  bouyer 			return (STOP);
    297  1.1  bouyer 		}
    298  1.1  bouyer 	}
    299  1.1  bouyer 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    300  1.1  bouyer 		if (anyout && chkrange(blkno, 1)) {
    301  1.1  bouyer 			res = SKIP;
    302  1.1  bouyer 		} else if (!testbmap(blkno)) {
    303  1.1  bouyer 			n_blks++;
    304  1.1  bouyer 			setbmap(blkno);
    305  1.1  bouyer 		} else {
    306  1.1  bouyer 			blkerror(idesc->id_number, "DUP", blkno);
    307  1.1  bouyer 			if (dupblk++ >= MAXDUP) {
    308  1.1  bouyer 				pwarn("EXCESSIVE DUP BLKS I=%u",
    309  1.1  bouyer 					idesc->id_number);
    310  1.1  bouyer 				if (preen)
    311  1.1  bouyer 					printf(" (SKIPPING)\n");
    312  1.1  bouyer 				else if (reply("CONTINUE") == 0)
    313  1.1  bouyer 					errexit("%s\n", "");
    314  1.1  bouyer 				return (STOP);
    315  1.1  bouyer 			}
    316  1.1  bouyer 			new = (struct dups *)malloc(sizeof(struct dups));
    317  1.1  bouyer 			if (new == NULL) {
    318  1.1  bouyer 				pfatal("DUP TABLE OVERFLOW.");
    319  1.1  bouyer 				if (reply("CONTINUE") == 0)
    320  1.1  bouyer 					errexit("%s\n", "");
    321  1.1  bouyer 				return (STOP);
    322  1.1  bouyer 			}
    323  1.1  bouyer 			new->dup = blkno;
    324  1.1  bouyer 			if (muldup == 0) {
    325  1.1  bouyer 				duplist = muldup = new;
    326  1.1  bouyer 				new->next = 0;
    327  1.1  bouyer 			} else {
    328  1.1  bouyer 				new->next = muldup->next;
    329  1.1  bouyer 				muldup->next = new;
    330  1.1  bouyer 			}
    331  1.1  bouyer 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    332  1.1  bouyer 				if (dlp->dup == blkno)
    333  1.1  bouyer 					break;
    334  1.1  bouyer 			if (dlp == muldup && dlp->dup != blkno)
    335  1.1  bouyer 				muldup = new;
    336  1.1  bouyer 		}
    337  1.1  bouyer 		/*
    338  1.1  bouyer 		 * count the number of blocks found in id_entryno
    339  1.1  bouyer 		 */
    340  1.1  bouyer 		idesc->id_entryno++;
    341  1.1  bouyer 	}
    342  1.1  bouyer 	return (res);
    343  1.1  bouyer }
    344