Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.7
      1 /*
      2  * Copyright (c) 1980, 1986, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)pass1.c	8.1 (Berkeley) 6/5/93";*/
     36 static char *rcsid = "$Id: pass1.c,v 1.7 1994/06/08 19:00:25 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 #include <sys/time.h>
     41 #include <ufs/ufs/dinode.h>
     42 #include <ufs/ufs/dir.h>
     43 #include <ufs/ffs/fs.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include "fsck.h"
     47 
     48 static daddr_t badblk;
     49 static daddr_t dupblk;
     50 int pass1check();
     51 struct dinode *getnextinode();
     52 
     53 pass1()
     54 {
     55 	ino_t inumber;
     56 	int c, i, cgd;
     57 	struct inodesc idesc;
     58 
     59 	/*
     60 	 * Set file system reserved blocks in used block map.
     61 	 */
     62 	for (c = 0; c < sblock.fs_ncg; c++) {
     63 		cgd = cgdmin(&sblock, c);
     64 		if (c == 0) {
     65 			i = cgbase(&sblock, c);
     66 			cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
     67 		} else
     68 			i = cgsblock(&sblock, c);
     69 		for (; i < cgd; i++)
     70 			setbmap(i);
     71 	}
     72 	/*
     73 	 * Find all allocated blocks.
     74 	 */
     75 	bzero((char *)&idesc, sizeof(struct inodesc));
     76 	idesc.id_type = ADDR;
     77 	idesc.id_func = pass1check;
     78 	inumber = 0;
     79 	n_files = n_blks = 0;
     80 	resetinodebuf();
     81 	for (c = 0; c < sblock.fs_ncg; c++) {
     82 		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
     83 			if (inumber < ROOTINO)
     84 				continue;
     85 			checkinode(inumber, &idesc);
     86 		}
     87 	}
     88 	freeinodebuf();
     89 }
     90 
     91 checkinode(inumber, idesc)
     92 	ino_t inumber;
     93 	register struct inodesc *idesc;
     94 {
     95 	register struct dinode *dp;
     96 	struct zlncnt *zlnp;
     97 	int ndb, j;
     98 	mode_t mode;
     99 	char symbuf[MAXSYMLINKLEN];
    100 
    101 	dp = getnextinode(inumber);
    102 	mode = dp->di_mode & IFMT;
    103 	if (mode == 0) {
    104 		if (bcmp((char *)dp->di_db, (char *)zino.di_db,
    105 			NDADDR * sizeof(daddr_t)) ||
    106 		    bcmp((char *)dp->di_ib, (char *)zino.di_ib,
    107 			NIADDR * sizeof(daddr_t)) ||
    108 		    dp->di_mode || dp->di_size) {
    109 			pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
    110 			if (reply("CLEAR") == 1) {
    111 				dp = ginode(inumber);
    112 				clearinode(dp);
    113 				inodirty();
    114 			}
    115 		}
    116 		statemap[inumber] = USTATE;
    117 		return;
    118 	}
    119 	lastino = inumber;
    120 	if (/* dp->di_size < 0 || */
    121 	    dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
    122 		if (debug)
    123 			printf("bad size %qu:", dp->di_size);
    124 		goto unknown;
    125 	}
    126 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    127 		dp = ginode(inumber);
    128 		dp->di_size = sblock.fs_fsize;
    129 		dp->di_mode = IFREG|0600;
    130 		inodirty();
    131 	}
    132 	ndb = howmany(dp->di_size, sblock.fs_bsize);
    133 	if (ndb < 0) {
    134 		if (debug)
    135 			printf("bad size %qu ndb %d:",
    136 				dp->di_size, ndb);
    137 		goto unknown;
    138 	}
    139 	if (mode == IFBLK || mode == IFCHR)
    140 		ndb++;
    141 	if (mode == IFLNK) {
    142 		/*
    143 		 * Note that the old fastlink format always had di_blocks set
    144 		 * to 0.  Other than that we no longer use the `spare' field
    145 		 * (which is now the extended uid) for sanity checking, the
    146 		 * new format is the same as the old.  We simply ignore the
    147 		 * conversion altogether.  - mycroft, 19MAY1994
    148 		 */
    149 		if (doinglevel2 &&
    150 		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
    151 		    dp->di_blocks != 0) {
    152 			if (bread(fsreadfd, symbuf,
    153 			    fsbtodb(&sblock, dp->di_db[0]),
    154 			    (long)dp->di_size) != 0)
    155 				errexit("cannot read symlink");
    156 			if (debug) {
    157 				symbuf[dp->di_size] = 0;
    158 				printf("convert symlink %d(%s) of size %d\n",
    159 					inumber, symbuf, (long)dp->di_size);
    160 			}
    161 			dp = ginode(inumber);
    162 			bcopy(symbuf, (caddr_t)dp->di_shortlink,
    163 			    (long)dp->di_size);
    164 			dp->di_blocks = 0;
    165 			inodirty();
    166 		}
    167 		/*
    168 		 * Fake ndb value so direct/indirect block checks below
    169 		 * will detect any garbage after symlink string.
    170 		 */
    171 		if (dp->di_size < sblock.fs_maxsymlinklen ||
    172 		    (sblock.fs_maxsymlinklen == 0 && OLDFASTLINK(dp))) {
    173 			ndb = howmany(dp->di_size, sizeof(daddr_t));
    174 			if (ndb > NDADDR) {
    175 				j = ndb - NDADDR;
    176 				for (ndb = 1; j > 1; j--)
    177 					ndb *= NINDIR(&sblock);
    178 				ndb += NDADDR;
    179 			}
    180 		}
    181 	}
    182 	for (j = ndb; j < NDADDR; j++)
    183 		if (dp->di_db[j] != 0) {
    184 			if (debug)
    185 				printf("bad direct addr: %ld\n", dp->di_db[j]);
    186 			goto unknown;
    187 		}
    188 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    189 		ndb /= NINDIR(&sblock);
    190 	for (; j < NIADDR; j++)
    191 		if (dp->di_ib[j] != 0) {
    192 			if (debug)
    193 				printf("bad indirect addr: %ld\n",
    194 					dp->di_ib[j]);
    195 			goto unknown;
    196 		}
    197 	if (ftypeok(dp) == 0)
    198 		goto unknown;
    199 	n_files++;
    200 	lncntp[inumber] = dp->di_nlink;
    201 	if (dp->di_nlink <= 0) {
    202 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    203 		if (zlnp == NULL) {
    204 			pfatal("LINK COUNT TABLE OVERFLOW");
    205 			if (reply("CONTINUE") == 0)
    206 				errexit("");
    207 		} else {
    208 			zlnp->zlncnt = inumber;
    209 			zlnp->next = zlnhead;
    210 			zlnhead = zlnp;
    211 		}
    212 	}
    213 	if (mode == IFDIR) {
    214 		if (dp->di_size == 0)
    215 			statemap[inumber] = DCLEAR;
    216 		else
    217 			statemap[inumber] = DSTATE;
    218 		cacheino(dp, inumber);
    219 	} else
    220 		statemap[inumber] = FSTATE;
    221 	typemap[inumber] = IFTODT(mode);
    222 	if (doinglevel2 &&
    223 	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
    224 		dp = ginode(inumber);
    225 		dp->di_uid = dp->di_ouid;
    226 		dp->di_ouid = -1;
    227 		dp->di_gid = dp->di_ogid;
    228 		dp->di_ogid = -1;
    229 		inodirty();
    230 	}
    231 	badblk = dupblk = 0;
    232 	idesc->id_number = inumber;
    233 	(void)ckinode(dp, idesc);
    234 	idesc->id_entryno *= btodb(sblock.fs_fsize);
    235 	if (dp->di_blocks != idesc->id_entryno) {
    236 		pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
    237 		    inumber, dp->di_blocks, idesc->id_entryno);
    238 		if (preen)
    239 			printf(" (CORRECTED)\n");
    240 		else if (reply("CORRECT") == 0)
    241 			return;
    242 		dp = ginode(inumber);
    243 		dp->di_blocks = idesc->id_entryno;
    244 		inodirty();
    245 	}
    246 	return;
    247 unknown:
    248 	pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
    249 	statemap[inumber] = FCLEAR;
    250 	if (reply("CLEAR") == 1) {
    251 		statemap[inumber] = USTATE;
    252 		dp = ginode(inumber);
    253 		clearinode(dp);
    254 		inodirty();
    255 	}
    256 }
    257 
    258 pass1check(idesc)
    259 	register struct inodesc *idesc;
    260 {
    261 	int res = KEEPON;
    262 	int anyout, nfrags;
    263 	daddr_t blkno = idesc->id_blkno;
    264 	register struct dups *dlp;
    265 	struct dups *new;
    266 
    267 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    268 		blkerror(idesc->id_number, "BAD", blkno);
    269 		if (badblk++ >= MAXBAD) {
    270 			pwarn("EXCESSIVE BAD BLKS I=%lu",
    271 				idesc->id_number);
    272 			if (preen)
    273 				printf(" (SKIPPING)\n");
    274 			else if (reply("CONTINUE") == 0)
    275 				errexit("");
    276 			return (STOP);
    277 		}
    278 	}
    279 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    280 		if (anyout && chkrange(blkno, 1)) {
    281 			res = SKIP;
    282 		} else if (!testbmap(blkno)) {
    283 			n_blks++;
    284 			setbmap(blkno);
    285 		} else {
    286 			blkerror(idesc->id_number, "DUP", blkno);
    287 			if (dupblk++ >= MAXDUP) {
    288 				pwarn("EXCESSIVE DUP BLKS I=%lu",
    289 					idesc->id_number);
    290 				if (preen)
    291 					printf(" (SKIPPING)\n");
    292 				else if (reply("CONTINUE") == 0)
    293 					errexit("");
    294 				return (STOP);
    295 			}
    296 			new = (struct dups *)malloc(sizeof(struct dups));
    297 			if (new == NULL) {
    298 				pfatal("DUP TABLE OVERFLOW.");
    299 				if (reply("CONTINUE") == 0)
    300 					errexit("");
    301 				return (STOP);
    302 			}
    303 			new->dup = blkno;
    304 			if (muldup == 0) {
    305 				duplist = muldup = new;
    306 				new->next = 0;
    307 			} else {
    308 				new->next = muldup->next;
    309 				muldup->next = new;
    310 			}
    311 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    312 				if (dlp->dup == blkno)
    313 					break;
    314 			if (dlp == muldup && dlp->dup != blkno)
    315 				muldup = new;
    316 		}
    317 		/*
    318 		 * count the number of blocks found in id_entryno
    319 		 */
    320 		idesc->id_entryno++;
    321 	}
    322 	return (res);
    323 }
    324