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