Home | History | Annotate | Line # | Download | only in fsck_ffs
inode.c revision 1.44
      1 /*	$NetBSD: inode.c,v 1.44 2004/01/03 17:27:35 dbj 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)inode.c	8.8 (Berkeley) 4/28/95";
     36 #else
     37 __RCSID("$NetBSD: inode.c,v 1.44 2004/01/03 17:27:35 dbj Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 
     44 #include <ufs/ufs/dinode.h>
     45 #include <ufs/ufs/dir.h>
     46 #include <ufs/ffs/fs.h>
     47 #include <ufs/ffs/ffs_extern.h>
     48 #include <ufs/ufs/ufs_bswap.h>
     49 
     50 #ifndef SMALL
     51 #include <err.h>
     52 #include <pwd.h>
     53 #endif
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <time.h>
     58 
     59 #include "fsck.h"
     60 #include "fsutil.h"
     61 #include "extern.h"
     62 
     63 static ino_t startinum;
     64 
     65 static int iblock __P((struct inodesc *, long, u_int64_t));
     66 static void swap_dinode1(union dinode *, int);
     67 static void swap_dinode2(union dinode *, int);
     68 
     69 int
     70 ckinode(dp, idesc)
     71 	union dinode *dp;
     72 	struct inodesc *idesc;
     73 {
     74 	int ret, offset, i;
     75 	union dinode dino;
     76 	u_int64_t sizepb;
     77 	int64_t remsize;
     78 	daddr_t ndb;
     79 	mode_t mode;
     80 	char pathbuf[MAXPATHLEN + 1];
     81 
     82 	if (idesc->id_fix != IGNORE)
     83 		idesc->id_fix = DONTKNOW;
     84 	idesc->id_entryno = 0;
     85 	idesc->id_filesize = iswap64(DIP(dp, size));
     86 	mode = iswap16(DIP(dp, mode)) & IFMT;
     87 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
     88 	    (idesc->id_filesize < sblock->fs_maxsymlinklen ||
     89 	    (isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
     90 	     (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
     91 		return (KEEPON);
     92 	if (is_ufs2)
     93 		dino.dp2 = dp->dp2;
     94 	else
     95 		dino.dp1 = dp->dp1;
     96 	ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
     97 	for (i = 0; i < NDADDR; i++) {
     98 		if (--ndb == 0 &&
     99 		    (offset = blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
    100 			idesc->id_numfrags =
    101 				numfrags(sblock, fragroundup(sblock, offset));
    102 		else
    103 			idesc->id_numfrags = sblock->fs_frag;
    104 		if (DIP(&dino, db[i]) == 0) {
    105 			if (idesc->id_type == DATA && ndb >= 0) {
    106 				/* An empty block in a directory XXX */
    107 				markclean = 0;
    108 				getpathname(pathbuf, sizeof(pathbuf),
    109 				    idesc->id_number, idesc->id_number);
    110 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS I",
    111 				    pathbuf);
    112 				abort();
    113 				if (reply("ADJUST LENGTH") == 1) {
    114 					dp = ginode(idesc->id_number);
    115 					DIP(dp, size) = iswap64(i *
    116 					    sblock->fs_bsize);
    117 					printf(
    118 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    119 					rerun = 1;
    120 					inodirty();
    121 				}
    122 			}
    123 			continue;
    124 		}
    125 		if (is_ufs2)
    126 			idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
    127 		else
    128 			idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
    129 		if (idesc->id_type == ADDR)
    130 			ret = (*idesc->id_func)(idesc);
    131 		else
    132 			ret = dirscan(idesc);
    133 		if (ret & STOP)
    134 			return (ret);
    135 	}
    136 	idesc->id_numfrags = sblock->fs_frag;
    137 	remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * NDADDR;
    138 	sizepb = sblock->fs_bsize;
    139 	for (i = 0; i < NIADDR; i++) {
    140 		if (DIP(&dino, ib[i])) {
    141 			if (is_ufs2)
    142 				idesc->id_blkno = iswap64(dino.dp2.di_ib[i]);
    143 			else
    144 				idesc->id_blkno = iswap32(dino.dp1.di_ib[i]);
    145 			ret = iblock(idesc, i + 1, remsize);
    146 			if (ret & STOP)
    147 				return (ret);
    148 		} else {
    149 			if (idesc->id_type == DATA && remsize > 0) {
    150 				/* An empty block in a directory XXX */
    151 				markclean = 0;
    152 				getpathname(pathbuf, sizeof(pathbuf),
    153 				    idesc->id_number, idesc->id_number);
    154 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    155 				    pathbuf);
    156 				if (reply("ADJUST LENGTH") == 1) {
    157 					dp = ginode(idesc->id_number);
    158 					DIP(dp, size) =
    159 					    iswap64(iswap64(DIP(dp, size))
    160 						- remsize);
    161 					remsize = 0;
    162 					printf(
    163 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    164 					rerun = 1;
    165 					inodirty();
    166 					break;
    167 				}
    168 			}
    169 		}
    170 		sizepb *= NINDIR(sblock);
    171 		remsize -= sizepb;
    172 	}
    173 	return (KEEPON);
    174 }
    175 
    176 static int
    177 iblock(idesc, ilevel, isize)
    178 	struct inodesc *idesc;
    179 	long ilevel;
    180 	u_int64_t isize;
    181 {
    182 	struct bufarea *bp;
    183 	int i, n, (*func) __P((struct inodesc *)), nif;
    184 	u_int64_t sizepb;
    185 	char buf[BUFSIZ];
    186 	char pathbuf[MAXPATHLEN + 1];
    187 	union dinode *dp;
    188 
    189 	if (idesc->id_type == ADDR) {
    190 		func = idesc->id_func;
    191 		if (((n = (*func)(idesc)) & KEEPON) == 0)
    192 			return (n);
    193 	} else
    194 		func = dirscan;
    195 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    196 		return (SKIP);
    197 	bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
    198 	ilevel--;
    199 	for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
    200 		sizepb *= NINDIR(sblock);
    201 	if (howmany(isize, sizepb) > NINDIR(sblock))
    202 		nif = NINDIR(sblock);
    203 	else
    204 		nif = howmany(isize, sizepb);
    205 	if (do_blkswap) { /* swap byte order of the whole blk */
    206 		if (is_ufs2) {
    207 			for (i = 0; i < nif; i++)
    208 				bp->b_un.b_indir2[i] =
    209 				    bswap64(bp->b_un.b_indir2[i]);
    210 		} else {
    211 			for (i = 0; i < nif; i++)
    212 				bp->b_un.b_indir1[i] =
    213 				    bswap32(bp->b_un.b_indir1[i]);
    214 		}
    215 		dirty(bp);
    216 		flush(fswritefd, bp);
    217 	}
    218 	if (idesc->id_func == pass1check && nif < NINDIR(sblock)) {
    219 		for (i = nif; i < NINDIR(sblock); i++) {
    220 			if (IBLK(bp, i) == 0)
    221 				continue;
    222 			(void)snprintf(buf, sizeof(buf),
    223 			    "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
    224 			if (dofix(idesc, buf)) {
    225 				IBLK(bp, i) = 0;
    226 				dirty(bp);
    227 			} else
    228 				markclean=  0;
    229 		}
    230 		flush(fswritefd, bp);
    231 	}
    232 	for (i = 0; i < nif; i++) {
    233 		if (IBLK(bp, i)) {
    234 			if (is_ufs2)
    235 				idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]);
    236 			else
    237 				idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]);
    238 			if (ilevel == 0)
    239 				n = (*func)(idesc);
    240 			else
    241 				n = iblock(idesc, ilevel, isize);
    242 			if (n & STOP) {
    243 				bp->b_flags &= ~B_INUSE;
    244 				return (n);
    245 			}
    246 		} else {
    247 			if (idesc->id_type == DATA && isize > 0) {
    248 				/* An empty block in a directory XXX */
    249 				markclean=  0;
    250 				getpathname(pathbuf, sizeof(pathbuf),
    251 				    idesc->id_number, idesc->id_number);
    252 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
    253 				    pathbuf);
    254 				if (reply("ADJUST LENGTH") == 1) {
    255 					dp = ginode(idesc->id_number);
    256 					DIP(dp, size) =
    257 					    iswap64(iswap64(DIP(dp, size))
    258 						- isize);
    259 					isize = 0;
    260 					printf(
    261 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    262 					rerun = 1;
    263 					inodirty();
    264 					bp->b_flags &= ~B_INUSE;
    265 					return(STOP);
    266 				}
    267 			}
    268 		}
    269 		isize -= sizepb;
    270 	}
    271 	bp->b_flags &= ~B_INUSE;
    272 	return (KEEPON);
    273 }
    274 
    275 /*
    276  * Check that a block in a legal block number.
    277  * Return 0 if in range, 1 if out of range.
    278  */
    279 int
    280 chkrange(blk, cnt)
    281 	daddr_t blk;
    282 	int cnt;
    283 {
    284 	int c;
    285 
    286 	if ((unsigned)(blk + cnt) > maxfsblock)
    287 		return (1);
    288 	if (cnt > sblock->fs_frag ||
    289 	    fragnum(sblock, blk) + cnt > sblock->fs_frag) {
    290 		if (debug)
    291 			printf("bad size: blk %lld, offset %d, size %d\n",
    292 			    (long long)blk, (int)fragnum(sblock, blk), cnt);
    293 	}
    294 	c = dtog(sblock, blk);
    295 	if (blk < cgdmin(sblock, c)) {
    296 		if ((blk + cnt) > cgsblock(sblock, c)) {
    297 			if (debug) {
    298 				printf("blk %lld < cgdmin %lld;",
    299 				    (long long)blk,
    300 				    (long long)cgdmin(sblock, c));
    301 				printf(" blk + cnt %lld > cgsbase %lld\n",
    302 				    (long long)(blk + cnt),
    303 				    (long long)cgsblock(sblock, c));
    304 			}
    305 			return (1);
    306 		}
    307 	} else {
    308 		if ((blk + cnt) > cgbase(sblock, c+1)) {
    309 			if (debug)  {
    310 				printf("blk %lld >= cgdmin %lld;",
    311 				    (long long)blk,
    312 				    (long long)cgdmin(sblock, c));
    313 				printf(" blk + cnt %lld > sblock->fs_fpg %d\n",
    314 				    (long long)(blk+cnt), sblock->fs_fpg);
    315 			}
    316 			return (1);
    317 		}
    318 	}
    319 	return (0);
    320 }
    321 
    322 /*
    323  * General purpose interface for reading inodes.
    324  */
    325 union dinode *
    326 ginode(inumber)
    327 	ino_t inumber;
    328 {
    329 	daddr_t iblk;
    330 	int blkoff;
    331 
    332 	if (inumber < ROOTINO || inumber > maxino)
    333 		errx(EEXIT, "bad inode number %d to ginode", inumber);
    334 	if (startinum == 0 ||
    335 	    inumber < startinum || inumber >= startinum + INOPB(sblock)) {
    336 		iblk = ino_to_fsba(sblock, inumber);
    337 		if (pbp != 0)
    338 			pbp->b_flags &= ~B_INUSE;
    339 		pbp = getdatablk(iblk, sblock->fs_bsize);
    340 		startinum = (inumber / INOPB(sblock)) * INOPB(sblock);
    341 	}
    342 	if (is_ufs2) {
    343 		blkoff = (inumber % INOPB(sblock)) * DINODE2_SIZE;
    344 		return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
    345 	}
    346 	blkoff = (inumber % INOPB(sblock)) * DINODE1_SIZE;
    347 	return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
    348 }
    349 
    350 static void
    351 swap_dinode1(union dinode *dp, int n)
    352 {
    353 	int i, j;
    354 	struct ufs1_dinode *dp1;
    355 
    356 	dp1 = (struct ufs1_dinode *)&dp->dp1;
    357 	for (i = 0; i < n; i++, dp1++) {
    358 		ffs_dinode1_swap(dp1, dp1);
    359 		if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
    360 		    (isappleufs && (iswap64(dp1->di_size) >
    361 		     APPLEUFS_MAXSYMLINKLEN)) ||
    362 		    doinglevel2 ||
    363 		    (sblock->fs_maxsymlinklen < 0) ||
    364 		    (iswap64(dp1->di_size) > sblock->fs_maxsymlinklen)) {
    365 			for (j = 0; j < (NDADDR + NIADDR); j++)
    366 			    dp1->di_db[j] = bswap32(dp1->di_db[j]);
    367 		}
    368 	}
    369 }
    370 
    371 static void
    372 swap_dinode2(union dinode *dp, int n)
    373 {
    374 	int i, j;
    375 	struct ufs2_dinode *dp2;
    376 
    377 	dp2 = (struct ufs2_dinode *)&dp->dp2;
    378 	for (i = 0; i < n; i++, dp2++) {
    379 		ffs_dinode2_swap(dp2, dp2);
    380 		if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
    381 			for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++)
    382 				dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
    383 		}
    384 	}
    385 }
    386 
    387 /*
    388  * Special purpose version of ginode used to optimize first pass
    389  * over all the inodes in numerical order.
    390  */
    391 ino_t nextino, lastinum, lastvalidinum;
    392 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
    393 union dinode *inodebuf;
    394 
    395 union dinode *
    396 getnextinode(inumber)
    397 	ino_t inumber;
    398 {
    399 	long size;
    400 	daddr_t dblk;
    401 	static union dinode *dp;
    402 	union dinode *ret;
    403 
    404 	if (inumber != nextino++ || inumber > lastvalidinum) {
    405 		abort();
    406 		errx(EEXIT, "bad inode number %d to nextinode", inumber);
    407 	}
    408 
    409 	if (inumber >= lastinum) {
    410 		readcnt++;
    411 		dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
    412 		if (readcnt % readpercg == 0) {
    413 			size = partialsize;
    414 			lastinum += partialcnt;
    415 		} else {
    416 			size = inobufsize;
    417 			lastinum += fullcnt;
    418 		}
    419 		(void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
    420 		if (doswap) {
    421 			if (is_ufs2)
    422 				swap_dinode2(inodebuf, lastinum - inumber);
    423 			else
    424 				swap_dinode1(inodebuf, lastinum - inumber);
    425 			bwrite(fswritefd, (char *)inodebuf, dblk, size);
    426 		}
    427 		dp = (union dinode *)inodebuf;
    428 	}
    429 	ret = dp;
    430 	dp = (union dinode *)
    431 	    ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
    432 	return ret;
    433 }
    434 
    435 void
    436 setinodebuf(inum)
    437 	ino_t inum;
    438 {
    439 
    440 	if (inum % sblock->fs_ipg != 0)
    441 		errx(EEXIT, "bad inode number %d to setinodebuf", inum);
    442 
    443 	lastvalidinum = inum + sblock->fs_ipg - 1;
    444 	startinum = 0;
    445 	nextino = inum;
    446 	lastinum = inum;
    447 	readcnt = 0;
    448 	if (inodebuf != NULL)
    449 		return;
    450 	inobufsize = blkroundup(sblock, INOBUFSIZE);
    451 	fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    452 	readpercg = sblock->fs_ipg / fullcnt;
    453 	partialcnt = sblock->fs_ipg % fullcnt;
    454 	partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
    455 	if (partialcnt != 0) {
    456 		readpercg++;
    457 	} else {
    458 		partialcnt = fullcnt;
    459 		partialsize = inobufsize;
    460 	}
    461 	if (inodebuf == NULL &&
    462 	    (inodebuf = malloc((unsigned)inobufsize)) == NULL)
    463 		errx(EEXIT, "Cannot allocate space for inode buffer");
    464 }
    465 
    466 void
    467 freeinodebuf()
    468 {
    469 
    470 	if (inodebuf != NULL)
    471 		free((char *)inodebuf);
    472 	inodebuf = NULL;
    473 }
    474 
    475 /*
    476  * Routines to maintain information about directory inodes.
    477  * This is built during the first pass and used during the
    478  * second and third passes.
    479  *
    480  * Enter inodes into the cache.
    481  */
    482 void
    483 cacheino(dp, inumber)
    484 	union dinode *dp;
    485 	ino_t inumber;
    486 {
    487 	struct inoinfo *inp;
    488 	struct inoinfo **inpp, **ninpsort;
    489 	unsigned int blks;
    490 	int i;
    491 	int64_t size;
    492 
    493 	size = iswap64(DIP(dp, size));
    494 	blks = howmany(size, sblock->fs_bsize);
    495 	if (blks > NDADDR)
    496 		blks = NDADDR + NIADDR;
    497 
    498 	inp = (struct inoinfo *) malloc(sizeof(*inp) + (blks - 1)
    499 					    * sizeof (int64_t));
    500 	if (inp == NULL)
    501 		return;
    502 	inpp = &inphead[inumber % dirhash];
    503 	inp->i_nexthash = *inpp;
    504 	*inpp = inp;
    505 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    506 	if (inumber == ROOTINO)
    507 		inp->i_parent = ROOTINO;
    508 	else
    509 		inp->i_parent = (ino_t)0;
    510 	inp->i_dotdot = (ino_t)0;
    511 	inp->i_number = inumber;
    512 	inp->i_isize = size;
    513 	inp->i_numblks = blks;
    514 	for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
    515 		inp->i_blks[i] = DIP(dp, db[i]);
    516 	if (blks > NDADDR)
    517 		for (i = 0; i < NIADDR; i++)
    518 			inp->i_blks[NDADDR + i] = DIP(dp, ib[i]);
    519 	if (inplast == listmax) {
    520 		ninpsort = (struct inoinfo **)realloc((char *)inpsort,
    521 		    (unsigned)(listmax + 100) * sizeof(struct inoinfo *));
    522 		if (inpsort == NULL)
    523 			errx(EEXIT, "cannot increase directory list");
    524 		inpsort = ninpsort;
    525 		listmax += 100;
    526 	}
    527 	inpsort[inplast++] = inp;
    528 }
    529 
    530 /*
    531  * Look up an inode cache structure.
    532  */
    533 struct inoinfo *
    534 getinoinfo(inumber)
    535 	ino_t inumber;
    536 {
    537 	struct inoinfo *inp;
    538 
    539 	for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
    540 		if (inp->i_number != inumber)
    541 			continue;
    542 		return (inp);
    543 	}
    544 	errx(EEXIT, "cannot find inode %d", inumber);
    545 	return ((struct inoinfo *)0);
    546 }
    547 
    548 /*
    549  * Clean up all the inode cache structure.
    550  */
    551 void
    552 inocleanup()
    553 {
    554 	struct inoinfo **inpp;
    555 
    556 	if (inphead == NULL)
    557 		return;
    558 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    559 		free((char *)(*inpp));
    560 	free((char *)inphead);
    561 	free((char *)inpsort);
    562 	inphead = inpsort = NULL;
    563 }
    564 
    565 void
    566 inodirty()
    567 {
    568 
    569 	dirty(pbp);
    570 }
    571 
    572 void
    573 clri(idesc, type, flag)
    574 	struct inodesc *idesc;
    575 	char *type;
    576 	int flag;
    577 {
    578 	union dinode *dp;
    579 
    580 	dp = ginode(idesc->id_number);
    581 	if (flag == 1) {
    582 		pwarn("%s %s", type,
    583 		    (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
    584 		pinode(idesc->id_number);
    585 	}
    586 	if (preen || reply("CLEAR") == 1) {
    587 		if (preen)
    588 			printf(" (CLEARED)\n");
    589 		n_files--;
    590 		(void)ckinode(dp, idesc);
    591 		clearinode(dp);
    592 		inoinfo(idesc->id_number)->ino_state = USTATE;
    593 		inodirty();
    594 	} else
    595 		markclean=  0;
    596 }
    597 
    598 int
    599 findname(idesc)
    600 	struct inodesc *idesc;
    601 {
    602 	struct direct *dirp = idesc->id_dirp;
    603 
    604 	if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
    605 		idesc->id_entryno++;
    606 		return (KEEPON);
    607 	}
    608 	memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
    609 	return (STOP|FOUND);
    610 }
    611 
    612 int
    613 findino(idesc)
    614 	struct inodesc *idesc;
    615 {
    616 	struct direct *dirp = idesc->id_dirp;
    617 
    618 	if (dirp->d_ino == 0)
    619 		return (KEEPON);
    620 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    621 	    iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
    622 		idesc->id_parent = iswap32(dirp->d_ino);
    623 		return (STOP|FOUND);
    624 	}
    625 	return (KEEPON);
    626 }
    627 
    628 int
    629 clearentry(idesc)
    630       struct inodesc *idesc;
    631 {
    632 	struct direct *dirp = idesc->id_dirp;
    633 
    634 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
    635 		idesc->id_entryno++;
    636 		return (KEEPON);
    637 	}
    638 	dirp->d_ino = 0;
    639 	return (STOP|FOUND|ALTERED);
    640 }
    641 
    642 void
    643 pinode(ino)
    644 	ino_t ino;
    645 {
    646 	union dinode *dp;
    647 	char *p;
    648 	struct passwd *pw;
    649 	time_t t;
    650 
    651 	printf(" I=%u ", ino);
    652 	if (ino < ROOTINO || ino > maxino)
    653 		return;
    654 	dp = ginode(ino);
    655 	printf(" OWNER=");
    656 #ifndef SMALL
    657 	if ((pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
    658 		printf("%s ", pw->pw_name);
    659 	else
    660 #endif
    661 		printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
    662 	printf("MODE=%o\n", iswap16(DIP(dp, mode)));
    663 	if (preen)
    664 		printf("%s: ", cdevname());
    665 	printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
    666 	t = iswap32(DIP(dp, mtime));
    667 	p = ctime(&t);
    668 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
    669 }
    670 
    671 void
    672 blkerror(ino, type, blk)
    673 	ino_t ino;
    674 	char *type;
    675 	daddr_t blk;
    676 {
    677 	struct inostat *info;
    678 
    679 	pfatal("%lld %s I=%u", (long long)blk, type, ino);
    680 	printf("\n");
    681 	info = inoinfo(ino);
    682 	switch (info->ino_state) {
    683 
    684 	case FSTATE:
    685 		info->ino_state = FCLEAR;
    686 		return;
    687 
    688 	case DSTATE:
    689 		info->ino_state = DCLEAR;
    690 		return;
    691 
    692 	case FCLEAR:
    693 	case DCLEAR:
    694 		return;
    695 
    696 	default:
    697 		errx(EEXIT, "BAD STATE %d TO BLKERR", info->ino_state);
    698 		/* NOTREACHED */
    699 	}
    700 }
    701 
    702 /*
    703  * allocate an unused inode
    704  */
    705 ino_t
    706 allocino(request, type)
    707 	ino_t request;
    708 	int type;
    709 {
    710 	ino_t ino;
    711 	union dinode *dp;
    712 	struct ufs1_dinode *dp1;
    713 	struct ufs2_dinode *dp2;
    714 	time_t t;
    715 	struct cg *cgp = cgrp;
    716 	int cg;
    717 	struct inostat *info;
    718 
    719 	if (request == 0)
    720 		request = ROOTINO;
    721 	else if (inoinfo(request)->ino_state != USTATE)
    722 		return (0);
    723 	for (ino = request; ino < maxino; ino++) {
    724 		info = inoinfo(ino);
    725 		if (info->ino_state == USTATE)
    726 			break;
    727 	}
    728 	if (ino == maxino)
    729 		return (0);
    730 	cg = ino_to_cg(sblock, ino);
    731 	getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
    732 	memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    733 	if ((doswap && !needswap) || (!doswap && needswap))
    734 		ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    735 	if (!cg_chkmagic(cgp, 0))
    736 		pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
    737 	if (doswap)
    738 		cgdirty();
    739 	setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
    740 	cgp->cg_cs.cs_nifree--;
    741 	switch (type & IFMT) {
    742 	case IFDIR:
    743 		info->ino_state = DSTATE;
    744 		cgp->cg_cs.cs_ndir++;
    745 		break;
    746 	case IFREG:
    747 	case IFLNK:
    748 		info->ino_state = FSTATE;
    749 		break;
    750 	default:
    751 		return (0);
    752 	}
    753 	cgdirty();
    754 	dp = ginode(ino);
    755 	if (is_ufs2) {
    756 		dp2 = &dp->dp2;
    757 		dp2->di_db[0] = iswap64(allocblk(1));
    758 		if (dp2->di_db[0] == 0) {
    759 			info->ino_state = USTATE;
    760 			return (0);
    761 		}
    762 		dp2->di_mode = iswap16(type);
    763 		dp2->di_flags = 0;
    764 		(void)time(&t);
    765 		dp2->di_atime = iswap64(t);
    766 		dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
    767 		dp2->di_size = iswap64(sblock->fs_fsize);
    768 		dp2->di_blocks = iswap64(btodb(sblock->fs_fsize));
    769 	} else {
    770 		dp1 = &dp->dp1;
    771 		dp1->di_db[0] = iswap32(allocblk(1));
    772 		if (dp1->di_db[0] == 0) {
    773 			info->ino_state = USTATE;
    774 			return (0);
    775 		}
    776 		dp1->di_mode = iswap16(type);
    777 		dp1->di_flags = 0;
    778 		(void)time(&t);
    779 		dp1->di_atime = iswap32(t);
    780 		dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
    781 		dp1->di_size = iswap64(sblock->fs_fsize);
    782 		dp1->di_blocks = iswap32(btodb(sblock->fs_fsize));
    783 	}
    784 	n_files++;
    785 	inodirty();
    786 	if (newinofmt)
    787 		info->ino_type = IFTODT(type);
    788 	return (ino);
    789 }
    790 
    791 /*
    792  * deallocate an inode
    793  */
    794 void
    795 freeino(ino)
    796 	ino_t ino;
    797 {
    798 	struct inodesc idesc;
    799 	union dinode *dp;
    800 
    801 	memset(&idesc, 0, sizeof(struct inodesc));
    802 	idesc.id_type = ADDR;
    803 	idesc.id_func = pass4check;
    804 	idesc.id_number = ino;
    805 	dp = ginode(ino);
    806 	(void)ckinode(dp, &idesc);
    807 	clearinode(dp);
    808 	inodirty();
    809 	inoinfo(ino)->ino_state = USTATE;
    810 	n_files--;
    811 }
    812