Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.13
      1 /*	$NetBSD: inode.c,v 1.13 1995/03/18 14:55:46 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)inode.c	8.4 (Berkeley) 4/18/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: inode.c,v 1.13 1995/03/18 14:55:46 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/time.h>
     46 #include <ufs/ufs/dinode.h>
     47 #include <ufs/ufs/dir.h>
     48 #include <ufs/ffs/fs.h>
     49 #ifndef SMALL
     50 #include <pwd.h>
     51 #endif
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 
     56 #include "fsck.h"
     57 #include "extern.h"
     58 
     59 static ino_t startinum;
     60 
     61 int iblock __P((struct inodesc *, long, quad_t));
     62 
     63 int
     64 ckinode(dp, idesc)
     65 	struct dinode *dp;
     66 	register struct inodesc *idesc;
     67 {
     68 	register daddr_t *ap;
     69 	long ret, n, ndb, offset;
     70 	struct dinode dino;
     71 	quad_t remsize, sizepb;
     72 	mode_t mode;
     73 
     74 	if (idesc->id_fix != IGNORE)
     75 		idesc->id_fix = DONTKNOW;
     76 	idesc->id_entryno = 0;
     77 	idesc->id_filesize = dp->di_size;
     78 	mode = dp->di_mode & IFMT;
     79 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
     80 	    (dp->di_size < sblock.fs_maxsymlinklen ||
     81 	     (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0))))
     82 		return (KEEPON);
     83 	dino = *dp;
     84 	ndb = howmany(dino.di_size, sblock.fs_bsize);
     85 	for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
     86 		if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0)
     87 			idesc->id_numfrags =
     88 				numfrags(&sblock, fragroundup(&sblock, offset));
     89 		else
     90 			idesc->id_numfrags = sblock.fs_frag;
     91 		if (*ap == 0)
     92 			continue;
     93 		idesc->id_blkno = *ap;
     94 		if (idesc->id_type == ADDR)
     95 			ret = (*idesc->id_func)(idesc);
     96 		else
     97 			ret = dirscan(idesc);
     98 		if (ret & STOP)
     99 			return (ret);
    100 	}
    101 	idesc->id_numfrags = sblock.fs_frag;
    102 	remsize = dino.di_size - sblock.fs_bsize * NDADDR;
    103 	sizepb = sblock.fs_bsize;
    104 	for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
    105 		if (*ap) {
    106 			idesc->id_blkno = *ap;
    107 			ret = iblock(idesc, n, remsize);
    108 			if (ret & STOP)
    109 				return (ret);
    110 		}
    111 		sizepb *= NINDIR(&sblock);
    112 		remsize -= sizepb;
    113 	}
    114 	return (KEEPON);
    115 }
    116 
    117 int
    118 iblock(idesc, ilevel, isize)
    119 	struct inodesc *idesc;
    120 	long ilevel;
    121 	quad_t isize;
    122 {
    123 	register daddr_t *ap;
    124 	register daddr_t *aplim;
    125 	register struct bufarea *bp;
    126 	int i, n, (*func)(), nif;
    127 	quad_t sizepb;
    128 	char buf[BUFSIZ];
    129 	extern int pass1check();
    130 
    131 	if (idesc->id_type == ADDR) {
    132 		func = idesc->id_func;
    133 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    134 			return (n);
    135 	} else
    136 		func = dirscan;
    137 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    138 		return (SKIP);
    139 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize);
    140 	ilevel--;
    141 	for (sizepb = sblock.fs_bsize, i = 0; i < ilevel; i++)
    142 		sizepb *= NINDIR(&sblock);
    143 	nif = howmany(isize , sizepb);
    144 	if (nif > NINDIR(&sblock))
    145 		nif = NINDIR(&sblock);
    146 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
    147 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    148 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    149 			if (*ap == 0)
    150 				continue;
    151 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
    152 				idesc->id_number);
    153 			if (dofix(idesc, buf)) {
    154 				*ap = 0;
    155 				dirty(bp);
    156 			}
    157 		}
    158 		flush(fswritefd, bp);
    159 	}
    160 	aplim = &bp->b_un.b_indir[nif];
    161 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    162 		if (*ap) {
    163 			idesc->id_blkno = *ap;
    164 			if (ilevel == 0)
    165 				n = (*func)(idesc);
    166 			else
    167 				n = iblock(idesc, ilevel, isize);
    168 			if (n & STOP) {
    169 				bp->b_flags &= ~B_INUSE;
    170 				return (n);
    171 			}
    172 		}
    173 		isize -= sizepb;
    174 	}
    175 	bp->b_flags &= ~B_INUSE;
    176 	return (KEEPON);
    177 }
    178 
    179 /*
    180  * Check that a block in a legal block number.
    181  * Return 0 if in range, 1 if out of range.
    182  */
    183 int
    184 chkrange(blk, cnt)
    185 	daddr_t blk;
    186 	int cnt;
    187 {
    188 	register int c;
    189 
    190 	if ((unsigned)(blk + cnt) > maxfsblock)
    191 		return (1);
    192 	c = dtog(&sblock, blk);
    193 	if (blk < cgdmin(&sblock, c)) {
    194 		if ((blk + cnt) > cgsblock(&sblock, c)) {
    195 			if (debug) {
    196 				printf("blk %ld < cgdmin %ld;",
    197 				    blk, cgdmin(&sblock, c));
    198 				printf(" blk + cnt %ld > cgsbase %ld\n",
    199 				    blk + cnt, cgsblock(&sblock, c));
    200 			}
    201 			return (1);
    202 		}
    203 	} else {
    204 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
    205 			if (debug)  {
    206 				printf("blk %ld >= cgdmin %ld;",
    207 				    blk, cgdmin(&sblock, c));
    208 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
    209 				    blk+cnt, sblock.fs_fpg);
    210 			}
    211 			return (1);
    212 		}
    213 	}
    214 	return (0);
    215 }
    216 
    217 /*
    218  * General purpose interface for reading inodes.
    219  */
    220 struct dinode *
    221 ginode(inumber)
    222 	ino_t inumber;
    223 {
    224 	daddr_t iblk;
    225 
    226 	if (inumber < ROOTINO || inumber > maxino)
    227 		errexit("bad inode number %d to ginode\n", inumber);
    228 	if (startinum == 0 ||
    229 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
    230 		iblk = ino_to_fsba(&sblock, inumber);
    231 		if (pbp != 0)
    232 			pbp->b_flags &= ~B_INUSE;
    233 		pbp = getdatablk(iblk, sblock.fs_bsize);
    234 		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
    235 	}
    236 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
    237 }
    238 
    239 /*
    240  * Special purpose version of ginode used to optimize first pass
    241  * over all the inodes in numerical order.
    242  */
    243 ino_t nextino, lastinum;
    244 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    245 struct dinode *inodebuf;
    246 
    247 struct dinode *
    248 getnextinode(inumber)
    249 	ino_t inumber;
    250 {
    251 	long size;
    252 	daddr_t dblk;
    253 	static struct dinode *dp;
    254 
    255 	if (inumber != nextino++ || inumber > maxino)
    256 		errexit("bad inode number %d to nextinode\n", inumber);
    257 	if (inumber >= lastinum) {
    258 		readcnt++;
    259 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    260 		if (readcnt % readpercg == 0) {
    261 			size = partialsize;
    262 			lastinum += partialcnt;
    263 		} else {
    264 			size = inobufsize;
    265 			lastinum += fullcnt;
    266 		}
    267 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size); /* ??? */
    268 		dp = inodebuf;
    269 	}
    270 	return (dp++);
    271 }
    272 
    273 void
    274 resetinodebuf()
    275 {
    276 
    277 	startinum = 0;
    278 	nextino = 0;
    279 	lastinum = 0;
    280 	readcnt = 0;
    281 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    282 	fullcnt = inobufsize / sizeof(struct dinode);
    283 	readpercg = sblock.fs_ipg / fullcnt;
    284 	partialcnt = sblock.fs_ipg % fullcnt;
    285 	partialsize = partialcnt * sizeof(struct dinode);
    286 	if (partialcnt != 0) {
    287 		readpercg++;
    288 	} else {
    289 		partialcnt = fullcnt;
    290 		partialsize = inobufsize;
    291 	}
    292 	if (inodebuf == NULL &&
    293 	    (inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
    294 		errexit("Cannot allocate space for inode buffer\n");
    295 	while (nextino < ROOTINO)
    296 		(void)getnextinode(nextino);
    297 }
    298 
    299 void
    300 freeinodebuf()
    301 {
    302 
    303 	if (inodebuf != NULL)
    304 		free((char *)inodebuf);
    305 	inodebuf = NULL;
    306 }
    307 
    308 /*
    309  * Routines to maintain information about directory inodes.
    310  * This is built during the first pass and used during the
    311  * second and third passes.
    312  *
    313  * Enter inodes into the cache.
    314  */
    315 void
    316 cacheino(dp, inumber)
    317 	register struct dinode *dp;
    318 	ino_t inumber;
    319 {
    320 	register struct inoinfo *inp;
    321 	struct inoinfo **inpp;
    322 	unsigned int blks;
    323 
    324 	blks = howmany(dp->di_size, sblock.fs_bsize);
    325 	if (blks > NDADDR)
    326 		blks = NDADDR + NIADDR;
    327 	inp = (struct inoinfo *)
    328 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    329 	if (inp == NULL)
    330 		return;
    331 	inpp = &inphead[inumber % numdirs];
    332 	inp->i_nexthash = *inpp;
    333 	*inpp = inp;
    334 	inp->i_parent = (ino_t)0;
    335 	inp->i_dotdot = (ino_t)0;
    336 	inp->i_number = inumber;
    337 	inp->i_isize = dp->di_size;
    338 	inp->i_numblks = blks * sizeof(daddr_t);
    339 	memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
    340 	if (inplast == listmax) {
    341 		listmax += 100;
    342 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    343 		    (unsigned)listmax * sizeof(struct inoinfo *));
    344 		if (inpsort == NULL)
    345 			errexit("cannot increase directory list");
    346 	}
    347 	inpsort[inplast++] = inp;
    348 }
    349 
    350 /*
    351  * Look up an inode cache structure.
    352  */
    353 struct inoinfo *
    354 getinoinfo(inumber)
    355 	ino_t inumber;
    356 {
    357 	register struct inoinfo *inp;
    358 
    359 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    360 		if (inp->i_number != inumber)
    361 			continue;
    362 		return (inp);
    363 	}
    364 	errexit("cannot find inode %d\n", inumber);
    365 	return ((struct inoinfo *)0);
    366 }
    367 
    368 /*
    369  * Clean up all the inode cache structure.
    370  */
    371 void
    372 inocleanup()
    373 {
    374 	register struct inoinfo **inpp;
    375 
    376 	if (inphead == NULL)
    377 		return;
    378 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    379 		free((char *)(*inpp));
    380 	free((char *)inphead);
    381 	free((char *)inpsort);
    382 	inphead = inpsort = NULL;
    383 }
    384 
    385 void
    386 inodirty()
    387 {
    388 
    389 	dirty(pbp);
    390 }
    391 
    392 void
    393 clri(idesc, type, flag)
    394 	register struct inodesc *idesc;
    395 	char *type;
    396 	int flag;
    397 {
    398 	register struct dinode *dp;
    399 
    400 	dp = ginode(idesc->id_number);
    401 	if (flag == 1) {
    402 		pwarn("%s %s", type,
    403 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    404 		pinode(idesc->id_number);
    405 	}
    406 	if (preen || reply("CLEAR") == 1) {
    407 		if (preen)
    408 			printf(" (CLEARED)\n");
    409 		n_files--;
    410 		(void)ckinode(dp, idesc);
    411 		clearinode(dp);
    412 		statemap[idesc->id_number] = USTATE;
    413 		inodirty();
    414 	}
    415 }
    416 
    417 int
    418 findname(idesc)
    419 	struct inodesc *idesc;
    420 {
    421 	register struct direct *dirp = idesc->id_dirp;
    422 
    423 	if (dirp->d_ino != idesc->id_parent)
    424 		return (KEEPON);
    425 	memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
    426 	return (STOP|FOUND);
    427 }
    428 
    429 int
    430 findino(idesc)
    431 	struct inodesc *idesc;
    432 {
    433 	register struct direct *dirp = idesc->id_dirp;
    434 
    435 	if (dirp->d_ino == 0)
    436 		return (KEEPON);
    437 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    438 	    dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
    439 		idesc->id_parent = dirp->d_ino;
    440 		return (STOP|FOUND);
    441 	}
    442 	return (KEEPON);
    443 }
    444 
    445 void
    446 pinode(ino)
    447 	ino_t ino;
    448 {
    449 	register struct dinode *dp;
    450 	register char *p;
    451 	struct passwd *pw;
    452 	char *ctime();
    453 
    454 	printf(" I=%lu ", ino);
    455 	if (ino < ROOTINO || ino > maxino)
    456 		return;
    457 	dp = ginode(ino);
    458 	printf(" OWNER=");
    459 #ifndef SMALL
    460 	if ((pw = getpwuid((int)dp->di_uid)) != 0)
    461 		printf("%s ", pw->pw_name);
    462 	else
    463 #endif
    464 		printf("%u ", (unsigned)dp->di_uid);
    465 	printf("MODE=%o\n", dp->di_mode);
    466 	if (preen)
    467 		printf("%s: ", cdevname);
    468 	printf("SIZE=%qu ", dp->di_size);
    469 	p = ctime(&dp->di_mtime.ts_sec);
    470 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    471 }
    472 
    473 void
    474 blkerror(ino, type, blk)
    475 	ino_t ino;
    476 	char *type;
    477 	daddr_t blk;
    478 {
    479 
    480 	pfatal("%ld %s I=%lu", blk, type, ino);
    481 	printf("\n");
    482 	switch (statemap[ino]) {
    483 
    484 	case FSTATE:
    485 		statemap[ino] = FCLEAR;
    486 		return;
    487 
    488 	case DSTATE:
    489 		statemap[ino] = DCLEAR;
    490 		return;
    491 
    492 	case FCLEAR:
    493 	case DCLEAR:
    494 		return;
    495 
    496 	default:
    497 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    498 		/* NOTREACHED */
    499 	}
    500 }
    501 
    502 /*
    503  * allocate an unused inode
    504  */
    505 ino_t
    506 allocino(request, type)
    507 	ino_t request;
    508 	int type;
    509 {
    510 	register ino_t ino;
    511 	register struct dinode *dp;
    512 
    513 	if (request == 0)
    514 		request = ROOTINO;
    515 	else if (statemap[request] != USTATE)
    516 		return (0);
    517 	for (ino = request; ino < maxino; ino++)
    518 		if (statemap[ino] == USTATE)
    519 			break;
    520 	if (ino == maxino)
    521 		return (0);
    522 	switch (type & IFMT) {
    523 	case IFDIR:
    524 		statemap[ino] = DSTATE;
    525 		break;
    526 	case IFREG:
    527 	case IFLNK:
    528 		statemap[ino] = FSTATE;
    529 		break;
    530 	default:
    531 		return (0);
    532 	}
    533 	dp = ginode(ino);
    534 	dp->di_db[0] = allocblk((long)1);
    535 	if (dp->di_db[0] == 0) {
    536 		statemap[ino] = USTATE;
    537 		return (0);
    538 	}
    539 	dp->di_mode = type;
    540 	(void)time(&dp->di_atime.ts_sec);
    541 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    542 	dp->di_size = sblock.fs_fsize;
    543 	dp->di_blocks = btodb(sblock.fs_fsize);
    544 	n_files++;
    545 	inodirty();
    546 	if (newinofmt)
    547 		typemap[ino] = IFTODT(type);
    548 	return (ino);
    549 }
    550 
    551 /*
    552  * deallocate an inode
    553  */
    554 void
    555 freeino(ino)
    556 	ino_t ino;
    557 {
    558 	struct inodesc idesc;
    559 	struct dinode *dp;
    560 
    561 	memset(&idesc, 0, sizeof(struct inodesc));
    562 	idesc.id_type = ADDR;
    563 	idesc.id_func = pass4check;
    564 	idesc.id_number = ino;
    565 	dp = ginode(ino);
    566 	(void)ckinode(dp, &idesc);
    567 	clearinode(dp);
    568 	inodirty();
    569 	statemap[ino] = USTATE;
    570 	n_files--;
    571 }
    572