Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.18
      1 /*	$NetBSD: inode.c,v 1.18 1996/05/21 16:58:12 mycroft 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.5 (Berkeley) 2/8/95";
     39 #else
     40 static char rcsid[] = "$NetBSD: inode.c,v 1.18 1996/05/21 16:58:12 mycroft 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 ufs_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 	if (isize > sizepb * NINDIR(&sblock))
    144 		nif = NINDIR(&sblock);
    145 	else
    146 		nif = howmany(isize, sizepb);
    147 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
    148 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    149 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    150 			if (*ap == 0)
    151 				continue;
    152 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
    153 				idesc->id_number);
    154 			if (dofix(idesc, buf)) {
    155 				*ap = 0;
    156 				dirty(bp);
    157 			}
    158 		}
    159 		flush(fswritefd, bp);
    160 	}
    161 	aplim = &bp->b_un.b_indir[nif];
    162 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    163 		if (*ap) {
    164 			idesc->id_blkno = *ap;
    165 			if (ilevel == 0)
    166 				n = (*func)(idesc);
    167 			else
    168 				n = iblock(idesc, ilevel, isize);
    169 			if (n & STOP) {
    170 				bp->b_flags &= ~B_INUSE;
    171 				return (n);
    172 			}
    173 		}
    174 		isize -= sizepb;
    175 	}
    176 	bp->b_flags &= ~B_INUSE;
    177 	return (KEEPON);
    178 }
    179 
    180 /*
    181  * Check that a block in a legal block number.
    182  * Return 0 if in range, 1 if out of range.
    183  */
    184 int
    185 chkrange(blk, cnt)
    186 	daddr_t blk;
    187 	int cnt;
    188 {
    189 	register int c;
    190 
    191 	if ((unsigned)(blk + cnt) > maxfsblock)
    192 		return (1);
    193 	c = dtog(&sblock, blk);
    194 	if (blk < cgdmin(&sblock, c)) {
    195 		if ((blk + cnt) > cgsblock(&sblock, c)) {
    196 			if (debug) {
    197 				printf("blk %ld < cgdmin %ld;",
    198 				    blk, cgdmin(&sblock, c));
    199 				printf(" blk + cnt %ld > cgsbase %ld\n",
    200 				    blk + cnt, cgsblock(&sblock, c));
    201 			}
    202 			return (1);
    203 		}
    204 	} else {
    205 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
    206 			if (debug)  {
    207 				printf("blk %ld >= cgdmin %ld;",
    208 				    blk, cgdmin(&sblock, c));
    209 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
    210 				    blk+cnt, sblock.fs_fpg);
    211 			}
    212 			return (1);
    213 		}
    214 	}
    215 	return (0);
    216 }
    217 
    218 /*
    219  * General purpose interface for reading inodes.
    220  */
    221 struct dinode *
    222 ginode(inumber)
    223 	ino_t inumber;
    224 {
    225 	daddr_t iblk;
    226 
    227 	if (inumber < ROOTINO || inumber > maxino)
    228 		errexit("bad inode number %d to ginode\n", inumber);
    229 	if (startinum == 0 ||
    230 	    inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
    231 		iblk = ino_to_fsba(&sblock, inumber);
    232 		if (pbp != 0)
    233 			pbp->b_flags &= ~B_INUSE;
    234 		pbp = getdatablk(iblk, sblock.fs_bsize);
    235 		startinum = (inumber / INOPB(&sblock)) * INOPB(&sblock);
    236 	}
    237 	return (&pbp->b_un.b_dinode[inumber % INOPB(&sblock)]);
    238 }
    239 
    240 /*
    241  * Special purpose version of ginode used to optimize first pass
    242  * over all the inodes in numerical order.
    243  */
    244 ino_t nextino, lastinum;
    245 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    246 struct dinode *inodebuf;
    247 
    248 struct dinode *
    249 getnextinode(inumber)
    250 	ino_t inumber;
    251 {
    252 	long size;
    253 	daddr_t dblk;
    254 	static struct dinode *dp;
    255 
    256 	if (inumber != nextino++ || inumber > maxino)
    257 		errexit("bad inode number %d to nextinode\n", inumber);
    258 	if (inumber >= lastinum) {
    259 		readcnt++;
    260 		dblk = fsbtodb(&sblock, ino_to_fsba(&sblock, lastinum));
    261 		if (readcnt % readpercg == 0) {
    262 			size = partialsize;
    263 			lastinum += partialcnt;
    264 		} else {
    265 			size = inobufsize;
    266 			lastinum += fullcnt;
    267 		}
    268 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size); /* ??? */
    269 		dp = inodebuf;
    270 	}
    271 	return (dp++);
    272 }
    273 
    274 void
    275 resetinodebuf()
    276 {
    277 
    278 	startinum = 0;
    279 	nextino = 0;
    280 	lastinum = 0;
    281 	readcnt = 0;
    282 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
    283 	fullcnt = inobufsize / sizeof(struct dinode);
    284 	readpercg = sblock.fs_ipg / fullcnt;
    285 	partialcnt = sblock.fs_ipg % fullcnt;
    286 	partialsize = partialcnt * sizeof(struct dinode);
    287 	if (partialcnt != 0) {
    288 		readpercg++;
    289 	} else {
    290 		partialcnt = fullcnt;
    291 		partialsize = inobufsize;
    292 	}
    293 	if (inodebuf == NULL &&
    294 	    (inodebuf = (struct dinode *)malloc((unsigned)inobufsize)) == NULL)
    295 		errexit("Cannot allocate space for inode buffer\n");
    296 	while (nextino < ROOTINO)
    297 		(void)getnextinode(nextino);
    298 }
    299 
    300 void
    301 freeinodebuf()
    302 {
    303 
    304 	if (inodebuf != NULL)
    305 		free((char *)inodebuf);
    306 	inodebuf = NULL;
    307 }
    308 
    309 /*
    310  * Routines to maintain information about directory inodes.
    311  * This is built during the first pass and used during the
    312  * second and third passes.
    313  *
    314  * Enter inodes into the cache.
    315  */
    316 void
    317 cacheino(dp, inumber)
    318 	register struct dinode *dp;
    319 	ino_t inumber;
    320 {
    321 	register struct inoinfo *inp;
    322 	struct inoinfo **inpp;
    323 	unsigned int blks;
    324 
    325 	blks = howmany(dp->di_size, sblock.fs_bsize);
    326 	if (blks > NDADDR)
    327 		blks = NDADDR + NIADDR;
    328 	inp = (struct inoinfo *)
    329 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    330 	if (inp == NULL)
    331 		return;
    332 	inpp = &inphead[inumber % numdirs];
    333 	inp->i_nexthash = *inpp;
    334 	*inpp = inp;
    335 	if (inumber == ROOTINO)
    336 		inp->i_parent = ROOTINO;
    337 	else
    338 		inp->i_parent = (ino_t)0;
    339 	inp->i_dotdot = (ino_t)0;
    340 	inp->i_number = inumber;
    341 	inp->i_isize = dp->di_size;
    342 	inp->i_numblks = blks * sizeof(daddr_t);
    343 	memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
    344 	if (inplast == listmax) {
    345 		listmax += 100;
    346 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
    347 		    (unsigned)listmax * sizeof(struct inoinfo *));
    348 		if (inpsort == NULL)
    349 			errexit("cannot increase directory list");
    350 	}
    351 	inpsort[inplast++] = inp;
    352 }
    353 
    354 /*
    355  * Look up an inode cache structure.
    356  */
    357 struct inoinfo *
    358 getinoinfo(inumber)
    359 	ino_t inumber;
    360 {
    361 	register struct inoinfo *inp;
    362 
    363 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    364 		if (inp->i_number != inumber)
    365 			continue;
    366 		return (inp);
    367 	}
    368 	errexit("cannot find inode %d\n", inumber);
    369 	return ((struct inoinfo *)0);
    370 }
    371 
    372 /*
    373  * Clean up all the inode cache structure.
    374  */
    375 void
    376 inocleanup()
    377 {
    378 	register struct inoinfo **inpp;
    379 
    380 	if (inphead == NULL)
    381 		return;
    382 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    383 		free((char *)(*inpp));
    384 	free((char *)inphead);
    385 	free((char *)inpsort);
    386 	inphead = inpsort = NULL;
    387 }
    388 
    389 void
    390 inodirty()
    391 {
    392 
    393 	dirty(pbp);
    394 }
    395 
    396 void
    397 clri(idesc, type, flag)
    398 	register struct inodesc *idesc;
    399 	char *type;
    400 	int flag;
    401 {
    402 	register struct dinode *dp;
    403 
    404 	dp = ginode(idesc->id_number);
    405 	if (flag == 1) {
    406 		pwarn("%s %s", type,
    407 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    408 		pinode(idesc->id_number);
    409 	}
    410 	if (preen || reply("CLEAR") == 1) {
    411 		if (preen)
    412 			printf(" (CLEARED)\n");
    413 		n_files--;
    414 		(void)ckinode(dp, idesc);
    415 		clearinode(dp);
    416 		statemap[idesc->id_number] = USTATE;
    417 		inodirty();
    418 	}
    419 }
    420 
    421 int
    422 findname(idesc)
    423 	struct inodesc *idesc;
    424 {
    425 	register struct direct *dirp = idesc->id_dirp;
    426 
    427 	if (dirp->d_ino != idesc->id_parent)
    428 		return (KEEPON);
    429 	memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
    430 	return (STOP|FOUND);
    431 }
    432 
    433 int
    434 findino(idesc)
    435 	struct inodesc *idesc;
    436 {
    437 	register struct direct *dirp = idesc->id_dirp;
    438 
    439 	if (dirp->d_ino == 0)
    440 		return (KEEPON);
    441 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    442 	    dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
    443 		idesc->id_parent = dirp->d_ino;
    444 		return (STOP|FOUND);
    445 	}
    446 	return (KEEPON);
    447 }
    448 
    449 void
    450 pinode(ino)
    451 	ino_t ino;
    452 {
    453 	register struct dinode *dp;
    454 	register char *p;
    455 	struct passwd *pw;
    456 	time_t t;
    457 
    458 	printf(" I=%lu ", ino);
    459 	if (ino < ROOTINO || ino > maxino)
    460 		return;
    461 	dp = ginode(ino);
    462 	printf(" OWNER=");
    463 #ifndef SMALL
    464 	if ((pw = getpwuid((int)dp->di_uid)) != 0)
    465 		printf("%s ", pw->pw_name);
    466 	else
    467 #endif
    468 		printf("%u ", (unsigned)dp->di_uid);
    469 	printf("MODE=%o\n", dp->di_mode);
    470 	if (preen)
    471 		printf("%s: ", cdevname);
    472 	printf("SIZE=%qu ", dp->di_size);
    473 	t = dp->di_mtime;
    474 	p = ctime(&t);
    475 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    476 }
    477 
    478 void
    479 blkerror(ino, type, blk)
    480 	ino_t ino;
    481 	char *type;
    482 	daddr_t blk;
    483 {
    484 
    485 	pfatal("%ld %s I=%lu", blk, type, ino);
    486 	printf("\n");
    487 	switch (statemap[ino]) {
    488 
    489 	case FSTATE:
    490 		statemap[ino] = FCLEAR;
    491 		return;
    492 
    493 	case DSTATE:
    494 		statemap[ino] = DCLEAR;
    495 		return;
    496 
    497 	case FCLEAR:
    498 	case DCLEAR:
    499 		return;
    500 
    501 	default:
    502 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    503 		/* NOTREACHED */
    504 	}
    505 }
    506 
    507 /*
    508  * allocate an unused inode
    509  */
    510 ino_t
    511 allocino(request, type)
    512 	ino_t request;
    513 	int type;
    514 {
    515 	register ino_t ino;
    516 	register struct dinode *dp;
    517 	time_t t;
    518 
    519 	if (request == 0)
    520 		request = ROOTINO;
    521 	else if (statemap[request] != USTATE)
    522 		return (0);
    523 	for (ino = request; ino < maxino; ino++)
    524 		if (statemap[ino] == USTATE)
    525 			break;
    526 	if (ino == maxino)
    527 		return (0);
    528 	switch (type & IFMT) {
    529 	case IFDIR:
    530 		statemap[ino] = DSTATE;
    531 		break;
    532 	case IFREG:
    533 	case IFLNK:
    534 		statemap[ino] = FSTATE;
    535 		break;
    536 	default:
    537 		return (0);
    538 	}
    539 	dp = ginode(ino);
    540 	dp->di_db[0] = allocblk((long)1);
    541 	if (dp->di_db[0] == 0) {
    542 		statemap[ino] = USTATE;
    543 		return (0);
    544 	}
    545 	dp->di_mode = type;
    546 	(void)time(&t);
    547 	dp->di_atime = t;
    548 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    549 	dp->di_size = sblock.fs_fsize;
    550 	dp->di_blocks = btodb(sblock.fs_fsize);
    551 	n_files++;
    552 	inodirty();
    553 	if (newinofmt)
    554 		typemap[ino] = IFTODT(type);
    555 	return (ino);
    556 }
    557 
    558 /*
    559  * deallocate an inode
    560  */
    561 void
    562 freeino(ino)
    563 	ino_t ino;
    564 {
    565 	struct inodesc idesc;
    566 	struct dinode *dp;
    567 
    568 	memset(&idesc, 0, sizeof(struct inodesc));
    569 	idesc.id_type = ADDR;
    570 	idesc.id_func = pass4check;
    571 	idesc.id_number = ino;
    572 	dp = ginode(ino);
    573 	(void)ckinode(dp, &idesc);
    574 	clearinode(dp);
    575 	inodirty();
    576 	statemap[ino] = USTATE;
    577 	n_files--;
    578 }
    579