Home | History | Annotate | Line # | Download | only in fsck_lfs
inode.c revision 1.10.2.1
      1 /* $NetBSD: inode.c,v 1.10.2.1 2001/06/27 03:49:41 perseant Exp $	 */
      2 
      3 /*
      4  * Copyright (c) 1997, 1998
      5  *      Konrad Schroder.  All rights reserved.
      6  * Copyright (c) 1980, 1986, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the University of
     20  *	California, Berkeley and its contributors.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/time.h>
     40 #include <ufs/ufs/dinode.h>
     41 #include <ufs/ufs/dir.h>
     42 #include <sys/mount.h>		/* XXX */
     43 #include <ufs/lfs/lfs.h>
     44 #ifndef SMALL
     45 #include <pwd.h>
     46 #endif
     47 #include <stdio.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 
     51 #include "fsck.h"
     52 #include "fsutil.h"
     53 #include "extern.h"
     54 
     55 extern SEGUSE  *seg_table;
     56 extern daddr_t *din_table;
     57 
     58 static int      iblock(struct inodesc *, long, u_int64_t);
     59 int             blksreqd(struct lfs *, int);
     60 int             lfs_maxino(void);
     61 /* static void dump_inoblk (struct lfs *, struct dinode *); */
     62 
     63 /* stolen from lfs_inode.c */
     64 /* Search a block for a specific dinode. */
     65 struct dinode  *
     66 lfs_difind(struct lfs * fs, ino_t ino, struct dinode * dip)
     67 {
     68 	struct dinode *ldip, *fin;
     69 
     70 	if (fs->lfs_version == 1)
     71 		fin = dip + INOPB(fs);
     72 	else
     73 		fin = dip + INOPS(fs);
     74 
     75 	for (ldip = dip; ldip < fin; ++ldip) {
     76 		if (ldip->di_inumber == ino)
     77 			return ldip;
     78 	}
     79 	/* printf("lfs_difind: dinode %u not found\n", ino); */
     80 	return NULL;
     81 }
     82 
     83 /*
     84  * Calculate the number of blocks required to be able to address data block
     85  * blkno (counting, of course, indirect blocks).  blkno must >=0.
     86  */
     87 int
     88 blksreqd(struct lfs * fs, int blkno)
     89 {
     90 	long            n = blkno;
     91 
     92 	if (blkno < NDADDR)
     93 		return blkno;
     94 	n -= NDADDR;
     95 	if (n < NINDIR(fs))
     96 		return blkno + 1;
     97 	n -= NINDIR(fs);
     98 	if (n < NINDIR(fs) * NINDIR(fs))
     99 		return blkno + 2 + n / NINDIR(fs) + 1;
    100 	n -= NINDIR(fs) * NINDIR(fs);
    101 	return blkno + 2 + NINDIR(fs) + n / (NINDIR(fs) * NINDIR(fs)) + 1;
    102 }
    103 
    104 #define BASE_SINDIR (NDADDR)
    105 #define BASE_DINDIR (NDADDR+NINDIR(fs))
    106 #define BASE_TINDIR (NDADDR+NINDIR(fs)+NINDIR(fs)*NINDIR(fs))
    107 
    108 #define D_UNITS (NINDIR(fs))
    109 #define T_UNITS (NINDIR(fs)*NINDIR(fs))
    110 
    111 ufs_daddr_t     lfs_bmap(struct lfs *, struct dinode *, ufs_daddr_t);
    112 
    113 ufs_daddr_t
    114 lfs_bmap(struct lfs * fs, struct dinode * idinode, ufs_daddr_t lbn)
    115 {
    116 	ufs_daddr_t     residue, up, off = 0;
    117 	struct bufarea *bp;
    118 
    119 	if (lbn > 0 && lbn > (idinode->di_size - 1) / dev_bsize) {
    120 		return UNASSIGNED;
    121 	}
    122 	/*
    123 	 * Indirect blocks: if it is a first-level indirect, pull its
    124 	 * address from the inode; otherwise, call ourselves to find the
    125 	 * address of the parent indirect block, and load that to find
    126 	 * the desired address.
    127 	 */
    128 	if (lbn < 0) {
    129 		lbn *= -1;
    130 		if (lbn == NDADDR) {
    131 			/* printf("lbn %d: single indir base\n", -lbn); */
    132 			return idinode->di_ib[0];	/* single indirect */
    133 		} else if (lbn == BASE_DINDIR + 1) {
    134 			/* printf("lbn %d: double indir base\n", -lbn); */
    135 			return idinode->di_ib[1];	/* double indirect */
    136 		} else if (lbn == BASE_TINDIR + 2) {
    137 			/* printf("lbn %d: triple indir base\n", -lbn); */
    138 			return idinode->di_ib[2];	/* triple indirect */
    139 		}
    140 		/*
    141 		 * Find the immediate parent. This is essentially finding the
    142 		 * residue of modulus, and then rounding accordingly.
    143 		 */
    144 		residue = (lbn - NDADDR) % NINDIR(fs);
    145 		if (residue == 1) {
    146 			/* Double indirect.  Parent is the triple. */
    147 			up = idinode->di_ib[2];
    148 			off = (lbn - 2 - BASE_TINDIR) / (NINDIR(fs) * NINDIR(fs));
    149 			if (up == UNASSIGNED || up == LFS_UNUSED_DADDR)
    150 				return UNASSIGNED;
    151 			/* printf("lbn %d: parent is the triple\n", -lbn); */
    152 			bp = getddblk(up, sblock.lfs_bsize);
    153 			bp->b_flags &= ~B_INUSE;
    154 			return ((daddr_t *)(bp->b_un.b_buf))[off];
    155 		} else {	/* residue == 0 */
    156 			/* Single indirect.  Two cases. */
    157 			if (lbn < BASE_TINDIR) {
    158 				/* Parent is the double, simple */
    159 				up = -(BASE_DINDIR) - 1;
    160 				off = (lbn - BASE_DINDIR) / D_UNITS;
    161 				/*
    162 				 * printf("lbn %d: parent is %d/%d\n", -lbn,
    163 				 * up,off);
    164 				 */
    165 			} else {
    166 				/* Ancestor is the triple, more complex */
    167 				up = ((lbn - BASE_TINDIR) / T_UNITS)
    168 					* T_UNITS + BASE_TINDIR + 1;
    169 				off = (lbn / D_UNITS) - (up / D_UNITS);
    170 				up = -up;
    171 				/*
    172 				 * printf("lbn %d: parent is %d/%d\n", -lbn,
    173 				 * up,off);
    174 				 */
    175 			}
    176 		}
    177 	} else {
    178 		/* Direct block.  Its parent must be a single indirect. */
    179 		if (lbn < NDADDR)
    180 			return idinode->di_db[lbn];
    181 		else {
    182 			/* Parent is an indirect block. */
    183 			up = -(((lbn - NDADDR) / D_UNITS) * D_UNITS + NDADDR);
    184 			off = (lbn - NDADDR) % D_UNITS;
    185 			/* printf("lbn %d: parent is %d/%d\n", lbn,up,off); */
    186 		}
    187 	}
    188 	up = lfs_bmap(fs, idinode, up);
    189 	if (up == UNASSIGNED || up == LFS_UNUSED_DADDR)
    190 		return UNASSIGNED;
    191 	bp = getddblk(up, sblock.lfs_bsize);
    192 	bp->b_flags &= ~B_INUSE;
    193 	return ((daddr_t *)(bp->b_un.b_buf))[off];
    194 }
    195 
    196 /*
    197  * This is kind of gross.  We use this to find the nth block
    198  * from a file whose inode has disk address idaddr.  In practice
    199  * we will only use this to find blocks of the ifile.
    200  */
    201 static struct bufarea empty;
    202 
    203 struct bufarea *
    204 getfileblk(struct lfs * fs, struct dinode * idinode, ino_t lbn)
    205 {
    206 	struct bufarea *bp;
    207 	ufs_daddr_t     blkno;
    208 	static char     empty_buf[65536];
    209 
    210 	empty.b_un.b_buf = &(empty_buf[0]);
    211 
    212 	blkno = lfs_bmap(fs, idinode, lbn);
    213 	if (blkno == UNASSIGNED || blkno == LFS_UNUSED_DADDR) {
    214 		printf("Warning: ifile lbn %d unassigned!\n", lbn);
    215 		return &empty;
    216 	}
    217 	bp = getddblk(blkno, sblock.lfs_bsize);
    218 	return bp;
    219 }
    220 
    221 #if 0
    222 static struct dinode *
    223 gidinode(void)
    224 {
    225 	static struct dinode *idinode;
    226 
    227 	if (!idinode) {		/* only need to do this once */
    228 		idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
    229 	}
    230 	return idinode;
    231 }
    232 #endif
    233 
    234 struct ifile   *
    235 lfs_ientry(ino_t ino, struct bufarea ** bpp)
    236 {
    237 	IFILE *ifp;
    238 
    239 	*bpp = getfileblk(&sblock, lfs_ginode(LFS_IFILE_INUM),
    240 			  ino / sblock.lfs_ifpb + sblock.lfs_cleansz +
    241 			  sblock.lfs_segtabsz);
    242 	if (*bpp == &empty) {
    243 		printf("Warning: ino %d ientry in unassigned block\n", ino);
    244 	}
    245 	if (*bpp) {
    246 		if (sblock.lfs_version > 1) {
    247 			ifp = (((IFILE *)((*bpp)->b_un.b_buf)) +
    248 			       (ino % sblock.lfs_ifpb));
    249 		} else {
    250 			ifp = (IFILE *)(((IFILE_V1 *)
    251 					 ((*bpp)->b_un.b_buf)) +
    252 					(ino % sblock.lfs_ifpb));
    253 		}
    254 		return ifp;
    255 	} else
    256 		return NULL;
    257 }
    258 
    259 SEGUSE         *
    260 lfs_gseguse(int segnum, struct bufarea ** bpp)
    261 {
    262 	int             blkno;
    263 	struct bufarea *bp;
    264 
    265 	blkno = segnum / sblock.lfs_sepb + sblock.lfs_cleansz;
    266 	(*bpp) = bp = getfileblk(&sblock, lfs_ginode(LFS_IFILE_INUM), blkno);
    267 	if (sblock.lfs_version == 1)
    268 		return (SEGUSE *)((SEGUSE_V1 *)(bp->b_un.b_buf) +
    269 				  segnum % sblock.lfs_sepb);
    270 	else
    271 		return (SEGUSE *)(bp->b_un.b_buf) + segnum % sblock.lfs_sepb;
    272 }
    273 
    274 daddr_t
    275 lfs_ino_daddr(ino_t inumber)
    276 {
    277 	daddr_t         daddr;
    278 	IFILE          *ifp;
    279 	struct bufarea *bp;
    280 
    281 	if (din_table[inumber]) {
    282 		daddr = din_table[inumber];
    283 	} else {
    284 		if (inumber == LFS_IFILE_INUM)
    285 			daddr = idaddr;
    286 		else {
    287 			ifp = lfs_ientry(inumber, &bp);
    288 			if (ifp == NULL) {
    289 				return NULL;
    290 			}
    291 			if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    292 				bp->b_flags &= ~B_INUSE;
    293 				return NULL;
    294 			}
    295 			bp->b_flags &= ~B_INUSE;
    296 			daddr = ifp->if_daddr;
    297 		}
    298 
    299 		din_table[inumber] = daddr;
    300 		seg_table[datosn(&sblock, daddr)].su_nbytes += DINODE_SIZE;
    301 	}
    302 	return daddr;
    303 }
    304 
    305 struct dinode  *
    306 lfs_ginode(ino_t inumber)
    307 {
    308 	struct ifile   *ifp;
    309 	struct dinode  *din;
    310 	struct bufarea *bp;
    311 	daddr_t         daddr;
    312 
    313 	if (inumber >= maxino)
    314 		errexit("bad inode number %d to lfs_ginode\n", inumber);
    315 
    316 #if 0
    317 	if (inumber == LFS_IFILE_INUM) {
    318 		daddr = idaddr;
    319 		if (din_table[LFS_IFILE_INUM] == 0) {
    320 			din_table[LFS_IFILE_INUM] = daddr;
    321 			seg_table[datosn(&sblock, daddr)].su_nbytes += DINODE_SIZE;
    322 		}
    323 		return gidinode();
    324 	}
    325 #endif
    326 
    327 	daddr = lfs_ino_daddr(inumber);
    328 	if (daddr == 0)
    329 		return NULL;
    330 
    331 	if (pbp)
    332 		pbp->b_flags &= ~B_INUSE;
    333 
    334 	pbp = getddblk(daddr, sblock.lfs_bsize);
    335 	din = lfs_difind(&sblock, inumber, pbp->b_un.b_dinode);
    336 
    337 	if (din == NULL) {
    338 		pfatal("INODE %d NOT FOUND\n", inumber);
    339 		if (reply("free")) {
    340 			ifp = lfs_ientry(inumber, &bp);
    341 			ifp->if_daddr = LFS_UNUSED_DADDR;
    342 			ifp->if_nextfree = sblock.lfs_free;
    343 			sblock.lfs_free = inumber;
    344 			sbdirty();
    345 			dirty(bp);
    346 			bp->b_flags &= ~B_INUSE;
    347 		}
    348 	}
    349 	return din;
    350 }
    351 
    352 /* imported from lfs_vfsops.c */
    353 int
    354 ino_to_fsba(struct lfs * fs, ino_t ino)
    355 {
    356 	daddr_t         daddr = LFS_UNUSED_DADDR;
    357 	struct ifile   *ifp;
    358 	struct bufarea *bp;
    359 
    360 	/* Translate the inode number to a disk address. */
    361 	if (ino == LFS_IFILE_INUM)
    362 		daddr = fs->lfs_idaddr;
    363 	else {
    364 		ifp = lfs_ientry(ino, &bp);
    365 		if (ifp) {
    366 			daddr = ifp->if_daddr;
    367 		} else {
    368 			pwarn("Can't locate inode #%ud\n", ino);
    369 		}
    370 		bp->b_flags &= ~B_INUSE;
    371 	}
    372 	return daddr;
    373 }
    374 
    375 /*
    376  * Check validity of held (direct) blocks in an inode.
    377  */
    378 int
    379 ckinode(struct dinode *dp, struct inodesc *idesc)
    380 {
    381 	register ufs_daddr_t *ap;
    382 	long            ret, n, ndb, offset;
    383 	struct dinode   dino;
    384 	u_int64_t       remsize, sizepb;
    385 	mode_t          mode;
    386 	char            pathbuf[MAXPATHLEN + 1];
    387 
    388 	if (idesc->id_fix != IGNORE)
    389 		idesc->id_fix = DONTKNOW;
    390 	idesc->id_entryno = 0;
    391 	idesc->id_filesize = dp->di_size;
    392 	mode = dp->di_mode & IFMT;
    393 	if (mode == IFBLK || mode == IFCHR ||
    394 	    (mode == IFLNK && (dp->di_size < sblock.lfs_maxsymlinklen ||
    395 	                       (sblock.lfs_maxsymlinklen == 0 &&
    396 				dp->di_blocks == 0))))
    397 		return (KEEPON);
    398 	dino = *dp;
    399 	ndb = howmany(dino.di_size, sblock.lfs_bsize);
    400 
    401 	for (ap = &dino.di_db[0]; ap < &dino.di_db[NDADDR]; ap++) {
    402 		if (--ndb == 0 && (offset = blkoff(&sblock, dino.di_size)) != 0) {
    403 			idesc->id_numfrags =
    404 				numfrags(&sblock, fragroundup(&sblock, offset));
    405 		} else
    406 			idesc->id_numfrags = sblock.lfs_frag;
    407 		if (*ap == 0) {
    408 			if (idesc->id_type == DATA && ndb >= 0) {
    409 				/* An empty block in a directory XXX */
    410 				getpathname(pathbuf, idesc->id_number,
    411 					    idesc->id_number);
    412 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    413 				       pathbuf);
    414 				if (reply("ADJUST LENGTH") == 1) {
    415 					dp = ginode(idesc->id_number);
    416 					dp->di_size = (ap - &dino.di_db[0]) *
    417 						sblock.lfs_bsize;
    418 					printf(
    419 					"YOU MUST RERUN FSCK AFTERWARDS\n");
    420 					rerun = 1;
    421 					inodirty();
    422 				}
    423 			}
    424 			continue;
    425 		}
    426 		idesc->id_blkno = *ap;
    427 		idesc->id_lblkno = ap - &dino.di_db[0];
    428 		if (idesc->id_type == ADDR) {
    429 			ret = (*idesc->id_func)(idesc);
    430 		} else
    431 			ret = dirscan(idesc);
    432 		idesc->id_lblkno = 0;
    433 		if (ret & STOP)
    434 			return (ret);
    435 	}
    436 	idesc->id_numfrags = sblock.lfs_frag;
    437 	remsize = dino.di_size - sblock.lfs_bsize * NDADDR;
    438 	sizepb = sblock.lfs_bsize;
    439 	for (ap = &dino.di_ib[0], n = 1; n <= NIADDR; ap++, n++) {
    440 		if (*ap) {
    441 			idesc->id_blkno = *ap;
    442 			ret = iblock(idesc, n, remsize);
    443 			if (ret & STOP)
    444 				return (ret);
    445 		} else {
    446 			if (idesc->id_type == DATA && remsize > 0) {
    447 				/* An empty block in a directory XXX */
    448 				getpathname(pathbuf, idesc->id_number,
    449 					    idesc->id_number);
    450 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    451 				       pathbuf);
    452 				if (reply("ADJUST LENGTH") == 1) {
    453 					dp = ginode(idesc->id_number);
    454 					dp->di_size -= remsize;
    455 					remsize = 0;
    456 					printf(
    457 					"YOU MUST RERUN FSCK AFTERWARDS\n");
    458 					rerun = 1;
    459 					inodirty();
    460 					break;
    461 				}
    462 			}
    463 		}
    464 		sizepb *= NINDIR(&sblock);
    465 		remsize -= sizepb;
    466 	}
    467 	return (KEEPON);
    468 }
    469 
    470 static int
    471 iblock(struct inodesc * idesc, long ilevel, u_int64_t isize)
    472 {
    473 	daddr_t	       *ap, *aplim;
    474 	struct bufarea *bp;
    475 	int             i, n, (*func)(struct inodesc *), nif;
    476 	u_int64_t       sizepb;
    477 	char            pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
    478 	struct dinode  *dp;
    479 
    480 	if (idesc->id_type == ADDR) {
    481 		func = idesc->id_func;
    482 		n = (*func)(idesc);
    483 		if ((n & KEEPON) == 0)
    484 			return (n);
    485 	} else
    486 		func = dirscan;
    487 	if (chkrange(idesc->id_blkno, fragstodb(&sblock, idesc->id_numfrags)))
    488 		return (SKIP);
    489 	bp = getddblk(idesc->id_blkno, sblock.lfs_bsize);
    490 	ilevel--;
    491 	for (sizepb = sblock.lfs_bsize, i = 0; i < ilevel; i++)
    492 		sizepb *= NINDIR(&sblock);
    493 	if (isize > sizepb * NINDIR(&sblock))
    494 		nif = NINDIR(&sblock);
    495 	else
    496 		nif = howmany(isize, sizepb);
    497 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
    498 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
    499 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
    500 			if (*ap == 0)
    501 				continue;
    502 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%u",
    503 					      idesc->id_number);
    504 			if (dofix(idesc, buf)) {
    505 				*ap = 0;
    506 				dirty(bp);
    507 			}
    508 		}
    509 		flush(fswritefd, bp);
    510 	}
    511 	aplim = &bp->b_un.b_indir[nif];
    512 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
    513 		if (*ap) {
    514 			idesc->id_blkno = *ap;
    515 			if (ilevel == 0)
    516 				n = (*func)(idesc);
    517 			else
    518 				n = iblock(idesc, ilevel, isize);
    519 			if (n & STOP) {
    520 				bp->b_flags &= ~B_INUSE;
    521 				return (n);
    522 			}
    523 		} else {
    524 			if (idesc->id_type == DATA && isize > 0) {
    525 				/* An empty block in a directory XXX */
    526 				getpathname(pathbuf, idesc->id_number,
    527 					    idesc->id_number);
    528 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    529 				       pathbuf);
    530 				if (reply("ADJUST LENGTH") == 1) {
    531 					dp = ginode(idesc->id_number);
    532 					dp->di_size -= isize;
    533 					isize = 0;
    534 					printf(
    535 					"YOU MUST RERUN FSCK AFTERWARDS\n");
    536 					rerun = 1;
    537 					inodirty();
    538 					bp->b_flags &= ~B_INUSE;
    539 					return (STOP);
    540 				}
    541 			}
    542 		}
    543 		isize -= sizepb;
    544 	}
    545 	bp->b_flags &= ~B_INUSE;
    546 	return (KEEPON);
    547 }
    548 
    549 /*
    550  * Check that a block in a legal block number.
    551  * Return 0 if in range, 1 if out of range.
    552  */
    553 int
    554 chkrange(daddr_t blk, int cnt)
    555 {
    556 	if (blk < btodb(LFS_LABELPAD+LFS_SBPAD)) {
    557 		return (1);
    558 	}
    559 	if (blk > fsbtodb(&sblock, maxfsblock)) {
    560 		return (1);
    561 	}
    562 	return (0);
    563 }
    564 
    565 /*
    566  * General purpose interface for reading inodes.
    567  */
    568 struct dinode  *
    569 ginode(ino_t inumber)
    570 {
    571 	return lfs_ginode(inumber);
    572 }
    573 
    574 /*
    575  * Routines to maintain information about directory inodes.
    576  * This is built during the first pass and used during the
    577  * second and third passes.
    578  *
    579  * Enter inodes into the cache.
    580  */
    581 void
    582 cacheino(struct dinode *dp, ino_t inumber)
    583 {
    584 	register struct inoinfo *inp;
    585 	struct inoinfo **inpp;
    586 	unsigned int    blks;
    587 
    588 	blks = howmany(dp->di_size, sblock.lfs_bsize);
    589 	if (blks > NDADDR)
    590 		blks = NDADDR + NIADDR;
    591 	inp = (struct inoinfo *)
    592 		malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
    593 	if (inp == NULL)
    594 		return;
    595 	inpp = &inphead[inumber % numdirs];
    596 	inp->i_nexthash = *inpp;
    597 	*inpp = inp;
    598 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    599 	if (inumber == ROOTINO)
    600 		inp->i_parent = ROOTINO;
    601 	else
    602 		inp->i_parent = (ino_t)0;
    603 	inp->i_dotdot = (ino_t)0;
    604 	inp->i_number = inumber;
    605 	inp->i_isize = dp->di_size;
    606 	inp->i_numblks = blks * sizeof(daddr_t);
    607 	memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t)inp->i_numblks);
    608 	if (inplast == listmax) {
    609 		listmax += 100;
    610 		inpsort = (struct inoinfo **)realloc((char *) inpsort,
    611 			     (unsigned)listmax * sizeof(struct inoinfo *));
    612 		if (inpsort == NULL)
    613 			errexit("cannot increase directory list");
    614 	}
    615 	inpsort[inplast++] = inp;
    616 }
    617 
    618 /*
    619  * Look up an inode cache structure.
    620  */
    621 struct inoinfo *
    622 getinoinfo(ino_t inumber)
    623 {
    624 	register struct inoinfo *inp;
    625 
    626 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    627 		if (inp->i_number != inumber)
    628 			continue;
    629 		return (inp);
    630 	}
    631 	errexit("cannot find inode %d\n", inumber);
    632 	return ((struct inoinfo *)0);
    633 }
    634 
    635 /*
    636  * Clean up all the inode cache structure.
    637  */
    638 void
    639 inocleanup()
    640 {
    641 	register struct inoinfo **inpp;
    642 
    643 	if (inphead == NULL)
    644 		return;
    645 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    646 		free((char *)(*inpp));
    647 	free((char *)inphead);
    648 	free((char *)inpsort);
    649 	inphead = inpsort = NULL;
    650 }
    651 
    652 void
    653 inodirty()
    654 {
    655 	dirty(pbp);
    656 }
    657 
    658 void
    659 clri(struct inodesc *idesc, char *type, int flag)
    660 {
    661 	register struct dinode *dp;
    662 	struct bufarea *bp;
    663 	IFILE          *ifp;
    664 
    665 	dp = ginode(idesc->id_number);
    666 	if (flag == 1) {
    667 		pwarn("%s %s", type,
    668 		      (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    669 		pinode(idesc->id_number);
    670 	}
    671 	if (preen || reply("CLEAR") == 1) {
    672 		if (preen)
    673 			printf(" (CLEARED)\n");
    674 		n_files--;
    675 		(void)ckinode(dp, idesc);
    676 		clearinode(dp);
    677 		statemap[idesc->id_number] = USTATE;
    678 		inodirty();
    679 
    680 		/* Send cleared inode to the free list */
    681 
    682 		ifp = lfs_ientry(idesc->id_number, &bp);
    683 		ifp->if_daddr = LFS_UNUSED_DADDR;
    684 		ifp->if_nextfree = sblock.lfs_free;
    685 		sblock.lfs_free = idesc->id_number;
    686 		sbdirty();
    687 		dirty(bp);
    688 		bp->b_flags &= ~B_INUSE;
    689 	}
    690 }
    691 
    692 int
    693 findname(struct inodesc *idesc)
    694 {
    695 	register struct direct *dirp = idesc->id_dirp;
    696 
    697 	if (dirp->d_ino != idesc->id_parent)
    698 		return (KEEPON);
    699 	memcpy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
    700 	return (STOP | FOUND);
    701 }
    702 
    703 int
    704 findino(struct inodesc *idesc)
    705 {
    706 	register struct direct *dirp = idesc->id_dirp;
    707 
    708 	if (dirp->d_ino == 0)
    709 		return (KEEPON);
    710 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    711 	    dirp->d_ino >= ROOTINO && dirp->d_ino < maxino) {
    712 		idesc->id_parent = dirp->d_ino;
    713 		return (STOP | FOUND);
    714 	}
    715 	return (KEEPON);
    716 }
    717 
    718 void
    719 pinode(ino_t ino)
    720 {
    721 	register struct dinode *dp;
    722 	register char  *p;
    723 	struct passwd  *pw;
    724 	time_t          t;
    725 
    726 	printf(" I=%u ", ino);
    727 	if (ino < ROOTINO || ino >= maxino)
    728 		return;
    729 	dp = ginode(ino);
    730 	if (dp) {
    731 		printf(" OWNER=");
    732 #ifndef SMALL
    733 		if ((pw = getpwuid((int)dp->di_uid)) != 0)
    734 			printf("%s ", pw->pw_name);
    735 		else
    736 #endif
    737 			printf("%u ", (unsigned)dp->di_uid);
    738 		printf("MODE=%o\n", dp->di_mode);
    739 		if (preen)
    740 			printf("%s: ", cdevname());
    741 		printf("SIZE=%llu ", (unsigned long long)dp->di_size);
    742 		t = dp->di_mtime;
    743 		p = ctime(&t);
    744 		printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    745 	}
    746 }
    747 
    748 void
    749 blkerror(ino_t ino, char *type, daddr_t blk)
    750 {
    751 
    752 	pfatal("%d %s I=%u", blk, type, ino);
    753 	printf("\n");
    754 	if (exitonfail)
    755 		exit(1);
    756 	switch (statemap[ino]) {
    757 
    758 	case FSTATE:
    759 		statemap[ino] = FCLEAR;
    760 		return;
    761 
    762 	case DSTATE:
    763 		statemap[ino] = DCLEAR;
    764 		return;
    765 
    766 	case FCLEAR:
    767 	case DCLEAR:
    768 		return;
    769 
    770 	default:
    771 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
    772 		/* NOTREACHED */
    773 	}
    774 }
    775 
    776 /*
    777  * allocate an unused inode
    778  */
    779 ino_t
    780 allocino(ino_t request, int type)
    781 {
    782 	register ino_t  ino;
    783 	register struct dinode *dp;
    784 	time_t          t;
    785 
    786 	if (request == 0)
    787 		request = ROOTINO;
    788 	else if (statemap[request] != USTATE)
    789 		return (0);
    790 	for (ino = request; ino < maxino; ino++)
    791 		if (statemap[ino] == USTATE)
    792 			break;
    793 	if (ino == maxino)
    794 		return (0);
    795 	switch (type & IFMT) {
    796 	case IFDIR:
    797 		statemap[ino] = DSTATE;
    798 		break;
    799 	case IFREG:
    800 	case IFLNK:
    801 		statemap[ino] = FSTATE;
    802 		break;
    803 	default:
    804 		return (0);
    805 	}
    806 	dp = ginode(ino);
    807 	dp->di_db[0] = allocblk((long)1);
    808 	if (dp->di_db[0] == 0) {
    809 		statemap[ino] = USTATE;
    810 		return (0);
    811 	}
    812 	dp->di_mode = type;
    813 	(void)time(&t);
    814 	dp->di_atime = t;
    815 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    816 	dp->di_size = sblock.lfs_fsize;
    817 	dp->di_blocks = btodb(sblock.lfs_fsize);
    818 	n_files++;
    819 	inodirty();
    820 	if (newinofmt)
    821 		typemap[ino] = IFTODT(type);
    822 	return (ino);
    823 }
    824 
    825 /*
    826  * deallocate an inode
    827  */
    828 void
    829 freeino(ino_t ino)
    830 {
    831 	struct inodesc  idesc;
    832 	struct dinode  *dp;
    833 
    834 	memset(&idesc, 0, sizeof(struct inodesc));
    835 	idesc.id_type = ADDR;
    836 	idesc.id_func = pass4check;
    837 	idesc.id_number = ino;
    838 	dp = ginode(ino);
    839 	(void)ckinode(dp, &idesc);
    840 	clearinode(dp);
    841 	inodirty();
    842 	statemap[ino] = USTATE;
    843 
    844 	n_files--;
    845 }
    846