Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.11
      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: @(#)inode.c	8.4 (Berkeley) 4/18/94";*/
     36 static char *rcsid = "$Id: inode.c,v 1.11 1994/09/23 14:27:12 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 #ifndef SMALL
     45 #include <pwd.h>
     46 #endif
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include "fsck.h"
     50 
     51 static ino_t startinum;
     52 
     53 ckinode(dp, idesc)
     54 	struct dinode *dp;
     55 	register struct inodesc *idesc;
     56 {
     57 	register daddr_t *ap;
     58 	long ret, n, ndb, offset;
     59 	struct dinode dino;
     60 	quad_t remsize, sizepb;
     61 	mode_t mode;
     62 
     63 	if (idesc->id_fix != IGNORE)
     64 		idesc->id_fix = DONTKNOW;
     65 	idesc->id_entryno = 0;
     66 	idesc->id_filesize = dp->di_size;
     67 	mode = dp->di_mode & IFMT;
     68 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
     69 	    (dp->di_size < sblock.fs_maxsymlinklen ||
     70 	     (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0))))
     71 		return (KEEPON);
     72 	dino = *dp;
     73 	ndb = howmany(dino.di_size, sblock.fs_bsize);
     74 	for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
     75 		if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0)
     76 			idesc->id_numfrags =
     77 				numfrags(&sblock, fragroundup(&sblock, offset));
     78 		else
     79 			idesc->id_numfrags = sblock.fs_frag;
     80 		if (*ap == 0)
     81 			continue;
     82 		idesc->id_blkno = *ap;
     83 		if (idesc->id_type == ADDR)
     84 			ret = (*idesc->id_func)(idesc);
     85 		else
     86 			ret = dirscan(idesc);
     87 		if (ret & STOP)
     88 			return (ret);
     89 	}
     90 	idesc->id_numfrags = sblock.fs_frag;
     91 	remsize = dino.di_size - sblock.fs_bsize * NDADDR;
     92 	sizepb = sblock.fs_bsize;
     93 	for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
     94 		if (*ap) {
     95 			idesc->id_blkno = *ap;
     96 			ret = iblock(idesc, n, remsize);
     97 			if (ret & STOP)
     98 				return (ret);
     99 		}
    100 		sizepb *= NINDIR(&sblock);
    101 		remsize -= sizepb;
    102 	}
    103 	return (KEEPON);
    104 }
    105 
    106 iblock(idesc, ilevel, isize)
    107 	struct inodesc *idesc;
    108 	long ilevel;
    109 	quad_t isize;
    110 {
    111 	register daddr_t *ap;
    112 	register daddr_t *aplim;
    113 	register struct bufarea *bp;
    114 	int i, n, (*func)(), nif;
    115 	quad_t sizepb;
    116 	char buf[BUFSIZ];
    117 	extern int dirscan(), pass1check();
    118 
    119 	if (idesc->id_type == ADDR) {
    120 		func = idesc->id_func;
    121 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    122 			return (n);
    123 	} else
    124 		func = dirscan;
    125 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    126 		return (SKIP);
    127 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
    128 	ilevel--;
    129 	for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
    130 		sizepb *= NINDIR(&sblock);
    131 	nif = howmany(isize , sizepb);
    132 	if (nif > NINDIR(&sblock))
    133 		nif = NINDIR(&sblock);
    134 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
    135 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    136 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    137 			if (*ap == 0)
    138 				continue;
    139 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
    140 				idesc->id_number);
    141 			if (dofix(idesc, buf)) {
    142 				*ap = 0;
    143 				dirty(bp);
    144 			}
    145 		}
    146 		flush(fswritefd, bp);
    147 	}
    148 	aplim = &bp->b_un.b_indir[nif];
    149 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    150 		if (*ap) {
    151 			idesc->id_blkno = *ap;
    152 			if (ilevel == 0)
    153 				n = (*func)(idesc);
    154 			else
    155 				n = iblock(idesc, ilevel, isize);
    156 			if (n & STOP) {
    157 				bp->b_flags &= ~B_INUSE;
    158 				return (n);
    159 			}
    160 		}
    161 		isize -= sizepb;
    162 	}
    163 	bp->b_flags &= ~B_INUSE;
    164 	return (KEEPON);
    165 }
    166 
    167 /*
    168  * Check that a block in a legal block number.
    169  * Return 0 if in range, 1 if out of range.
    170  */
    171 chkrange(blk, cnt)
    172 	daddr_t blk;
    173 	int cnt;
    174 {
    175 	register int c;
    176 
    177 	if ((unsigned)(blk + cnt) > maxfsblock)
    178 		return (1);
    179 	c = dtog(&sblock, blk);
    180 	if (blk < cgdmin(&sblock, c)) {
    181 		if ((blk + cnt) > cgsblock(&sblock, c)) {
    182 			if (debug) {
    183 				printf("blk %ld < cgdmin %ld;",
    184 				    blk, cgdmin(&sblock, c));
    185 				printf(" blk + cnt %ld > cgsbase %ld\n",
    186 				    blk + cnt, cgsblock(&sblock, c));
    187 			}
    188 			return (1);
    189 		}
    190 	} else {
    191 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
    192 			if (debug)  {
    193 				printf("blk %ld >= cgdmin %ld;",
    194 				    blk, cgdmin(&sblock, c));
    195 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
    196 				    blk+cnt, sblock.fs_fpg);
    197 			}
    198 			return (1);
    199 		}
    200 	}
    201 	return (0);
    202 }
    203 
    204 /*
    205  * General purpose interface for reading inodes.
    206  */
    207 struct dinode *
    208 ginode(inumber)
    209 	ino_t inumber;
    210 {
    211 	daddr_t iblk;
    212 
    213 	if (inumber < ROOTINO || inumber > maxino)
    214 		errexit("bad inode number %d to ginode\n", inumber);
    215 	if (startinum == 0 ||
    216 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
    217 		iblk = ino_to_fsba(&sblock, inumber);
    218 		if (pbp != 0)
    219 			pbp->b_flags &= ~B_INUSE;
    220 		pbp = getdatablk(iblk, sblock.fs_bsize);
    221 		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
    222 	}
    223 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
    224 }
    225 
    226 /*
    227  * Special purpose version of ginode used to optimize first pass
    228  * over all the inodes in numerical order.
    229  */
    230 ino_t nextino, lastinum;
    231 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    232 struct dinode *inodebuf;
    233 
    234 struct dinode *
    235 getnextinode(inumber)
    236 	ino_t inumber;
    237 {
    238 	long size;
    239 	daddr_t dblk;
    240 	static struct dinode *dp;
    241 
    242 	if (inumber != nextino++ || inumber > maxino)
    243 		errexit("bad inode number %d to nextinode\n", inumber);
    244 	if (inumber >= lastinum) {
    245 		readcnt++;
    246 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    247 		if (readcnt % readpercg == 0) {
    248 			size = partialsize;
    249 			lastinum += partialcnt;
    250 		} else {
    251 			size = inobufsize;
    252 			lastinum += fullcnt;
    253 		}
    254 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size); /* ??? */
    255 		dp = inodebuf;
    256 	}
    257 	return (dp++);
    258 }
    259 
    260 resetinodebuf()
    261 {
    262 
    263 	startinum = 0;
    264 	nextino = 0;
    265 	lastinum = 0;
    266 	readcnt = 0;
    267 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    268 	fullcnt = inobufsize / sizeof(struct dinode);
    269 	readpercg = sblock.fs_ipg / fullcnt;
    270 	partialcnt = sblock.fs_ipg % fullcnt;
    271 	partialsize = partialcnt * sizeof(struct dinode);
    272 	if (partialcnt != 0) {
    273 		readpercg++;
    274 	} else {
    275 		partialcnt = fullcnt;
    276 		partialsize = inobufsize;
    277 	}
    278 	if (inodebuf == NULL &&
    279 	    (inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
    280 		errexit("Cannot allocate space for inode buffer\n");
    281 	while (nextino < ROOTINO)
    282 		(void)getnextinode(nextino);
    283 }
    284 
    285 freeinodebuf()
    286 {
    287 
    288 	if (inodebuf != NULL)
    289 		free((char *)inodebuf);
    290 	inodebuf = NULL;
    291 }
    292 
    293 /*
    294  * Routines to maintain information about directory inodes.
    295  * This is built during the first pass and used during the
    296  * second and third passes.
    297  *
    298  * Enter inodes into the cache.
    299  */
    300 cacheino(dp, inumber)
    301 	register struct dinode *dp;
    302 	ino_t inumber;
    303 {
    304 	register struct inoinfo *inp;
    305 	struct inoinfo **inpp;
    306 	unsigned int blks;
    307 
    308 	blks = howmany(dp->di_size, sblock.fs_bsize);
    309 	if (blks > NDADDR)
    310 		blks = NDADDR + NIADDR;
    311 	inp = (struct inoinfo *)
    312 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    313 	if (inp == NULL)
    314 		return;
    315 	inpp = &inphead[inumber % numdirs];
    316 	inp->i_nexthash = *inpp;
    317 	*inpp = inp;
    318 	inp->i_parent = (ino_t)0;
    319 	inp->i_dotdot = (ino_t)0;
    320 	inp->i_number = inumber;
    321 	inp->i_isize = dp->di_size;
    322 	inp->i_numblks = blks * sizeof(daddr_t);
    323 	memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
    324 	if (inplast == listmax) {
    325 		listmax += 100;
    326 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    327 		    (unsigned)listmax * sizeof(struct inoinfo *));
    328 		if (inpsort == NULL)
    329 			errexit("cannot increase directory list");
    330 	}
    331 	inpsort[inplast++] = inp;
    332 }
    333 
    334 /*
    335  * Look up an inode cache structure.
    336  */
    337 struct inoinfo *
    338 getinoinfo(inumber)
    339 	ino_t inumber;
    340 {
    341 	register struct inoinfo *inp;
    342 
    343 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    344 		if (inp->i_number != inumber)
    345 			continue;
    346 		return (inp);
    347 	}
    348 	errexit("cannot find inode %d\n", inumber);
    349 	return ((struct inoinfo *)0);
    350 }
    351 
    352 /*
    353  * Clean up all the inode cache structure.
    354  */
    355 inocleanup()
    356 {
    357 	register struct inoinfo **inpp;
    358 
    359 	if (inphead == NULL)
    360 		return;
    361 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    362 		free((char *)(*inpp));
    363 	free((char *)inphead);
    364 	free((char *)inpsort);
    365 	inphead = inpsort = NULL;
    366 }
    367 
    368 inodirty()
    369 {
    370 
    371 	dirty(pbp);
    372 }
    373 
    374 clri(idesc, type, flag)
    375 	register struct inodesc *idesc;
    376 	char *type;
    377 	int flag;
    378 {
    379 	register struct dinode *dp;
    380 
    381 	dp = ginode(idesc->id_number);
    382 	if (flag == 1) {
    383 		pwarn("%s %s", type,
    384 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    385 		pinode(idesc->id_number);
    386 	}
    387 	if (preen || reply("CLEAR") == 1) {
    388 		if (preen)
    389 			printf(" (CLEARED)\n");
    390 		n_files--;
    391 		(void)ckinode(dp, idesc);
    392 		clearinode(dp);
    393 		statemap[idesc->id_number] = USTATE;
    394 		inodirty();
    395 	}
    396 }
    397 
    398 findname(idesc)
    399 	struct inodesc *idesc;
    400 {
    401 	register struct direct *dirp = idesc->id_dirp;
    402 
    403 	if (dirp->d_ino != idesc->id_parent)
    404 		return (KEEPON);
    405 	memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
    406 	return (STOP|FOUND);
    407 }
    408 
    409 findino(idesc)
    410 	struct inodesc *idesc;
    411 {
    412 	register struct direct *dirp = idesc->id_dirp;
    413 
    414 	if (dirp->d_ino == 0)
    415 		return (KEEPON);
    416 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    417 	    dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
    418 		idesc->id_parent = dirp->d_ino;
    419 		return (STOP|FOUND);
    420 	}
    421 	return (KEEPON);
    422 }
    423 
    424 pinode(ino)
    425 	ino_t ino;
    426 {
    427 	register struct dinode *dp;
    428 	register char *p;
    429 	struct passwd *pw;
    430 	char *ctime();
    431 
    432 	printf(" I=%lu ", ino);
    433 	if (ino < ROOTINO || ino > maxino)
    434 		return;
    435 	dp = ginode(ino);
    436 	printf(" OWNER=");
    437 #ifndef SMALL
    438 	if ((pw = getpwuid((int)dp->di_uid)) != 0)
    439 		printf("%s ", pw->pw_name);
    440 	else
    441 #endif
    442 		printf("%u ", (unsigned)dp->di_uid);
    443 	printf("MODE=%o\n", dp->di_mode);
    444 	if (preen)
    445 		printf("%s: ", cdevname);
    446 	printf("SIZE=%qu ", dp->di_size);
    447 	p = ctime(&dp->di_mtime.ts_sec);
    448 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    449 }
    450 
    451 blkerror(ino, type, blk)
    452 	ino_t ino;
    453 	char *type;
    454 	daddr_t blk;
    455 {
    456 
    457 	pfatal("%ld %s I=%lu", blk, type, ino);
    458 	printf("\n");
    459 	switch (statemap[ino]) {
    460 
    461 	case FSTATE:
    462 		statemap[ino] = FCLEAR;
    463 		return;
    464 
    465 	case DSTATE:
    466 		statemap[ino] = DCLEAR;
    467 		return;
    468 
    469 	case FCLEAR:
    470 	case DCLEAR:
    471 		return;
    472 
    473 	default:
    474 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    475 		/* NOTREACHED */
    476 	}
    477 }
    478 
    479 /*
    480  * allocate an unused inode
    481  */
    482 ino_t
    483 allocino(request, type)
    484 	ino_t request;
    485 	int type;
    486 {
    487 	register ino_t ino;
    488 	register struct dinode *dp;
    489 
    490 	if (request == 0)
    491 		request = ROOTINO;
    492 	else if (statemap[request] != USTATE)
    493 		return (0);
    494 	for (ino = request; ino < maxino; ino++)
    495 		if (statemap[ino] == USTATE)
    496 			break;
    497 	if (ino == maxino)
    498 		return (0);
    499 	switch (type & IFMT) {
    500 	case IFDIR:
    501 		statemap[ino] = DSTATE;
    502 		break;
    503 	case IFREG:
    504 	case IFLNK:
    505 		statemap[ino] = FSTATE;
    506 		break;
    507 	default:
    508 		return (0);
    509 	}
    510 	dp = ginode(ino);
    511 	dp->di_db[0] = allocblk((long)1);
    512 	if (dp->di_db[0] == 0) {
    513 		statemap[ino] = USTATE;
    514 		return (0);
    515 	}
    516 	dp->di_mode = type;
    517 	(void)time(&dp->di_atime.ts_sec);
    518 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    519 	dp->di_size = sblock.fs_fsize;
    520 	dp->di_blocks = btodb(sblock.fs_fsize);
    521 	n_files++;
    522 	inodirty();
    523 	if (newinofmt)
    524 		typemap[ino] = IFTODT(type);
    525 	return (ino);
    526 }
    527 
    528 /*
    529  * deallocate an inode
    530  */
    531 freeino(ino)
    532 	ino_t ino;
    533 {
    534 	struct inodesc idesc;
    535 	extern int pass4check();
    536 	struct dinode *dp;
    537 
    538 	memset(&idesc, 0, sizeof(struct inodesc));
    539 	idesc.id_type = ADDR;
    540 	idesc.id_func = pass4check;
    541 	idesc.id_number = ino;
    542 	dp = ginode(ino);
    543 	(void)ckinode(dp, &idesc);
    544 	clearinode(dp);
    545 	inodirty();
    546 	statemap[ino] = USTATE;
    547 	n_files--;
    548 }
    549