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