Home | History | Annotate | Line # | Download | only in fsck_lfs
inode.c revision 1.60
      1 /* $NetBSD: inode.c,v 1.60 2015/08/19 20:33:29 dholland Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1980, 1986, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/types.h>
     62 #include <sys/param.h>
     63 #include <sys/time.h>
     64 #include <sys/buf.h>
     65 #include <sys/mount.h>
     66 
     67 #define vnode uvnode
     68 #include <ufs/lfs/lfs.h>
     69 #include <ufs/lfs/lfs_accessors.h>
     70 #include <ufs/lfs/lfs_inode.h>
     71 #undef vnode
     72 
     73 #include <err.h>
     74 #ifndef SMALL
     75 #include <pwd.h>
     76 #endif
     77 #include <stdio.h>
     78 #include <stdlib.h>
     79 #include <string.h>
     80 #include <util.h>
     81 
     82 #include "bufcache.h"
     83 #include "vnode.h"
     84 #include "lfs_user.h"
     85 
     86 #include "fsck.h"
     87 #include "fsutil.h"
     88 #include "extern.h"
     89 
     90 extern SEGUSE *seg_table;
     91 extern ulfs_daddr_t *din_table;
     92 
     93 static int iblock(struct inodesc *, long, u_int64_t);
     94 int blksreqd(struct lfs *, int);
     95 int lfs_maxino(void);
     96 
     97 /*
     98  * Get a dinode of a given inum.
     99  * XXX combine this function with vget.
    100  */
    101 union lfs_dinode *
    102 ginode(ino_t ino)
    103 {
    104 	struct uvnode *vp;
    105 	struct ubuf *bp;
    106 	IFILE *ifp;
    107 	daddr_t daddr;
    108 	unsigned segno;
    109 
    110 	vp = vget(fs, ino);
    111 	if (vp == NULL)
    112 		return NULL;
    113 
    114 	if (din_table[ino] == 0x0) {
    115 		LFS_IENTRY(ifp, fs, ino, bp);
    116 		daddr = lfs_if_getdaddr(fs, ifp);
    117 		segno = lfs_dtosn(fs, daddr);
    118 		din_table[ino] = daddr;
    119 		seg_table[segno].su_nbytes += DINOSIZE(fs);
    120 		brelse(bp, 0);
    121 	}
    122 	return VTOI(vp)->i_din;
    123 }
    124 
    125 /*
    126  * Check validity of held blocks in an inode, recursing through all blocks.
    127  */
    128 int
    129 ckinode(union lfs_dinode *dp, struct inodesc *idesc)
    130 {
    131 	ulfs_daddr_t lbn, pbn;
    132 	long ret, n, ndb, offset;
    133 	union lfs_dinode dino;
    134 	u_int64_t remsize, sizepb;
    135 	mode_t mode;
    136 	char pathbuf[MAXPATHLEN + 1];
    137 	struct uvnode *vp, *thisvp;
    138 
    139 	if (idesc->id_fix != IGNORE)
    140 		idesc->id_fix = DONTKNOW;
    141 	idesc->id_entryno = 0;
    142 	idesc->id_filesize = lfs_dino_getsize(fs, dp);
    143 	mode = lfs_dino_getmode(fs, dp) & LFS_IFMT;
    144 	if (mode == LFS_IFBLK || mode == LFS_IFCHR ||
    145 	    (mode == LFS_IFLNK && (lfs_dino_getsize(fs, dp) < lfs_sb_getmaxsymlinklen(fs) ||
    146 		    (lfs_sb_getmaxsymlinklen(fs) == 0 &&
    147 			lfs_dino_getblocks(fs, dp) == 0))))
    148 		return (KEEPON);
    149 	/* XXX is this safe if we're 32-bit? */
    150 	dino = *dp;
    151 	ndb = howmany(lfs_dino_getsize(fs, &dino), lfs_sb_getbsize(fs));
    152 
    153 	thisvp = vget(fs, idesc->id_number);
    154 	for (lbn = 0; lbn < ULFS_NDADDR; lbn++) {
    155 		pbn = lfs_dino_getdb(fs, &dino, lbn);
    156 		if (thisvp)
    157 			idesc->id_numfrags =
    158 				lfs_numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
    159 		else {
    160 			if (--ndb == 0 && (offset = lfs_blkoff(fs, lfs_dino_getsize(fs, &dino))) != 0) {
    161 				idesc->id_numfrags =
    162 			    	lfs_numfrags(fs, lfs_fragroundup(fs, offset));
    163 			} else
    164 				idesc->id_numfrags = lfs_sb_getfrag(fs);
    165 		}
    166 		if (pbn == 0) {
    167 			if (idesc->id_type == DATA && ndb >= 0) {
    168 				/* An empty block in a directory XXX */
    169 				getpathname(pathbuf, sizeof(pathbuf),
    170 				    idesc->id_number, idesc->id_number);
    171 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]",
    172 				    pathbuf, (long long)idesc->id_number);
    173 				if (reply("ADJUST LENGTH") == 1) {
    174 					vp = vget(fs, idesc->id_number);
    175 					dp = VTOD(vp);
    176 					lfs_dino_setsize(fs, dp,
    177 					    lbn * lfs_sb_getbsize(fs));
    178 					printf(
    179 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    180 					rerun = 1;
    181 					inodirty(VTOI(vp));
    182 				} else
    183 					break;
    184 			}
    185 			continue;
    186 		}
    187 		idesc->id_blkno = pbn;
    188 		idesc->id_lblkno = lbn;
    189 		if (idesc->id_type == ADDR) {
    190 			ret = (*idesc->id_func) (idesc);
    191 		} else
    192 			ret = dirscan(idesc);
    193 		if (ret & STOP)
    194 			return (ret);
    195 	}
    196 	idesc->id_numfrags = lfs_sb_getfrag(fs);
    197 	remsize = lfs_dino_getsize(fs, &dino) - lfs_sb_getbsize(fs) * ULFS_NDADDR;
    198 	sizepb = lfs_sb_getbsize(fs);
    199 	for (n = 1; n <= ULFS_NIADDR; n++) {
    200 		pbn = lfs_dino_getib(fs, &dino, n-1);
    201 		if (pbn) {
    202 			idesc->id_blkno = pbn;
    203 			ret = iblock(idesc, n, remsize);
    204 			if (ret & STOP)
    205 				return (ret);
    206 		} else {
    207 			if (idesc->id_type == DATA && remsize > 0) {
    208 				/* An empty block in a directory XXX */
    209 				getpathname(pathbuf, sizeof(pathbuf),
    210 				    idesc->id_number, idesc->id_number);
    211 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]",
    212 				    pathbuf, (long long)idesc->id_number);
    213 				if (reply("ADJUST LENGTH") == 1) {
    214 					vp = vget(fs, idesc->id_number);
    215 					dp = VTOD(vp);
    216 					lfs_dino_setsize(fs, dp,
    217 					    lfs_dino_getsize(fs, dp) - remsize);
    218 					remsize = 0;
    219 					printf(
    220 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    221 					rerun = 1;
    222 					inodirty(VTOI(vp));
    223 					break;
    224 				} else
    225 					break;
    226 			}
    227 		}
    228 		sizepb *= LFS_NINDIR(fs);
    229 		remsize -= sizepb;
    230 	}
    231 	return (KEEPON);
    232 }
    233 
    234 static int
    235 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    236 {
    237 	ulfs_daddr_t *ap, *aplim;
    238 	struct ubuf *bp;
    239 	int i, n, (*func) (struct inodesc *), nif;
    240 	u_int64_t sizepb;
    241 	char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
    242 	struct uvnode *devvp, *vp;
    243 	int diddirty = 0;
    244 
    245 	if (idesc->id_type == ADDR) {
    246 		func = idesc->id_func;
    247 		n = (*func) (idesc);
    248 		if ((n & KEEPON) == 0)
    249 			return (n);
    250 	} else
    251 		func = dirscan;
    252 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    253 		return (SKIP);
    254 
    255 	devvp = fs->lfs_devvp;
    256 	bread(devvp, LFS_FSBTODB(fs, idesc->id_blkno), lfs_sb_getbsize(fs),
    257 	    0, &bp);
    258 	ilevel--;
    259 	for (sizepb = lfs_sb_getbsize(fs), i = 0; i < ilevel; i++)
    260 		sizepb *= LFS_NINDIR(fs);
    261 	if (isize > sizepb * LFS_NINDIR(fs))
    262 		nif = LFS_NINDIR(fs);
    263 	else
    264 		nif = howmany(isize, sizepb);
    265 	if (idesc->id_func == pass1check && nif < LFS_NINDIR(fs)) {
    266 		aplim = ((ulfs_daddr_t *) bp->b_data) + LFS_NINDIR(fs);
    267 		for (ap = ((ulfs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) {
    268 			if (*ap == 0)
    269 				continue;
    270 			(void)snprintf(buf, sizeof(buf),
    271 			    "PARTIALLY TRUNCATED INODE I=%llu",
    272 			    (unsigned long long)idesc->id_number);
    273 			if (dofix(idesc, buf)) {
    274 				*ap = 0;
    275 				++diddirty;
    276 			}
    277 		}
    278 	}
    279 	aplim = ((ulfs_daddr_t *) bp->b_data) + nif;
    280 	for (ap = ((ulfs_daddr_t *) bp->b_data); ap < aplim; ap++) {
    281 		if (*ap) {
    282 			idesc->id_blkno = *ap;
    283 			if (ilevel == 0) {
    284 				/*
    285 				 * dirscan needs lfs_lblkno.
    286 				 */
    287 				idesc->id_lblkno++;
    288 				n = (*func) (idesc);
    289 			} else {
    290 				n = iblock(idesc, ilevel, isize);
    291 			}
    292 			if (n & STOP) {
    293 				if (diddirty)
    294 					VOP_BWRITE(bp);
    295 				else
    296 					brelse(bp, 0);
    297 				return (n);
    298 			}
    299 		} else {
    300 			if (idesc->id_type == DATA && isize > 0) {
    301 				/* An empty block in a directory XXX */
    302 				getpathname(pathbuf, sizeof(pathbuf),
    303 				    idesc->id_number, idesc->id_number);
    304 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]",
    305 				    pathbuf, (long long)idesc->id_number);
    306 				if (reply("ADJUST LENGTH") == 1) {
    307 					vp = vget(fs, idesc->id_number);
    308 					VTOI(vp)->i_ffs1_size -= isize;
    309 					isize = 0;
    310 					printf(
    311 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    312 					rerun = 1;
    313 					inodirty(VTOI(vp));
    314 					if (diddirty)
    315 						VOP_BWRITE(bp);
    316 					else
    317 						brelse(bp, 0);
    318 					return (STOP);
    319 				}
    320 			}
    321 		}
    322 		isize -= sizepb;
    323 	}
    324 	if (diddirty)
    325 		VOP_BWRITE(bp);
    326 	else
    327 		brelse(bp, 0);
    328 	return (KEEPON);
    329 }
    330 
    331 /*
    332  * Check that a block in a legal block number.
    333  * Return 0 if in range, 1 if out of range.
    334  */
    335 int
    336 chkrange(daddr_t blk, int cnt)
    337 {
    338 	if (blk < lfs_sntod(fs, 0)) {
    339 		return (1);
    340 	}
    341 	if (blk > maxfsblock) {
    342 		return (1);
    343 	}
    344 	if (blk + cnt < lfs_sntod(fs, 0)) {
    345 		return (1);
    346 	}
    347 	if (blk + cnt > maxfsblock) {
    348 		return (1);
    349 	}
    350 	return (0);
    351 }
    352 
    353 /*
    354  * Routines to maintain information about directory inodes.
    355  * This is built during the first pass and used during the
    356  * second and third passes.
    357  *
    358  * Enter inodes into the cache.
    359  */
    360 void
    361 cacheino(union lfs_dinode *dp, ino_t inumber)
    362 {
    363 	struct inoinfo *inp;
    364 	struct inoinfo **inpp, **ninpsort;
    365 	unsigned int blks, i;
    366 
    367 	blks = howmany(lfs_dino_getsize(fs, dp), lfs_sb_getbsize(fs));
    368 	if (blks > ULFS_NDADDR)
    369 		blks = ULFS_NDADDR + ULFS_NIADDR;
    370 	inp = emalloc(sizeof(*inp) + (blks - 1) * sizeof(ulfs_daddr_t));
    371 	inpp = &inphead[inumber % numdirs];
    372 	inp->i_nexthash = *inpp;
    373 	*inpp = inp;
    374 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    375 	if (inumber == ULFS_ROOTINO)
    376 		inp->i_parent = ULFS_ROOTINO;
    377 	else
    378 		inp->i_parent = (ino_t) 0;
    379 	inp->i_dotdot = (ino_t) 0;
    380 	inp->i_number = inumber;
    381 	inp->i_isize = lfs_dino_getsize(fs, dp);
    382 
    383 	inp->i_numblks = blks * sizeof(ulfs_daddr_t);
    384 	for (i=0; i<blks && i<ULFS_NDADDR; i++) {
    385 		inp->i_blks[i] = lfs_dino_getdb(fs, dp, i);
    386 	}
    387 	for (; i<blks; i++) {
    388 		inp->i_blks[i] = lfs_dino_getib(fs, dp, i - ULFS_NDADDR);
    389 	}
    390 	if (inplast == listmax) {
    391 		ninpsort = erealloc(inpsort,
    392 		    (listmax + 100) * sizeof(struct inoinfo *));
    393 		inpsort = ninpsort;
    394 		listmax += 100;
    395 	}
    396 	inpsort[inplast++] = inp;
    397 }
    398 
    399 /*
    400  * Look up an inode cache structure.
    401  */
    402 struct inoinfo *
    403 getinoinfo(ino_t inumber)
    404 {
    405 	struct inoinfo *inp;
    406 
    407 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    408 		if (inp->i_number != inumber)
    409 			continue;
    410 		return (inp);
    411 	}
    412 	err(EEXIT, "cannot find inode %llu", (unsigned long long)inumber);
    413 	return ((struct inoinfo *) 0);
    414 }
    415 
    416 /*
    417  * Clean up all the inode cache structure.
    418  */
    419 void
    420 inocleanup(void)
    421 {
    422 	struct inoinfo **inpp;
    423 
    424 	if (inphead == NULL)
    425 		return;
    426 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    427 		free((char *) (*inpp));
    428 	free((char *) inphead);
    429 	free((char *) inpsort);
    430 	inphead = inpsort = NULL;
    431 }
    432 
    433 void
    434 inodirty(struct inode *ip)
    435 {
    436 	ip->i_flag |= IN_MODIFIED;
    437 }
    438 
    439 void
    440 clri(struct inodesc * idesc, const char *type, int flag)
    441 {
    442 	struct uvnode *vp;
    443 
    444 	vp = vget(fs, idesc->id_number);
    445 	if (flag & 0x1) {
    446 		pwarn("%s %s", type,
    447 		      (VTOI(vp)->i_ffs1_mode & LFS_IFMT) == LFS_IFDIR ? "DIR" : "FILE");
    448 		pinode(idesc->id_number);
    449 	}
    450 	if ((flag & 0x2) || preen || reply("CLEAR") == 1) {
    451 		if (preen && flag != 2)
    452 			printf(" (CLEARED)\n");
    453 		n_files--;
    454 		(void) ckinode(VTOD(vp), idesc);
    455 		clearinode(idesc->id_number);
    456 		statemap[idesc->id_number] = USTATE;
    457 		vnode_destroy(vp);
    458 		return;
    459 	}
    460 	return;
    461 }
    462 
    463 void
    464 clearinode(ino_t inumber)
    465 {
    466 	struct ubuf *bp;
    467 	IFILE *ifp;
    468 	daddr_t daddr;
    469 
    470 	/* Send cleared inode to the free list */
    471 
    472 	LFS_IENTRY(ifp, fs, inumber, bp);
    473 	daddr = lfs_if_getdaddr(fs, ifp);
    474 	if (daddr == LFS_UNUSED_DADDR) {
    475 		brelse(bp, 0);
    476 		return;
    477 	}
    478 	lfs_if_setdaddr(fs, ifp, LFS_UNUSED_DADDR);
    479 	lfs_if_setnextfree(fs, ifp, lfs_sb_getfreehd(fs));
    480 	lfs_sb_setfreehd(fs, inumber);
    481 	sbdirty();
    482 	VOP_BWRITE(bp);
    483 
    484 	/*
    485 	 * update segment usage.
    486 	 */
    487 	if (daddr != LFS_UNUSED_DADDR) {
    488 		SEGUSE *sup;
    489 		u_int32_t oldsn = lfs_dtosn(fs, daddr);
    490 
    491 		seg_table[oldsn].su_nbytes -= DINOSIZE(fs);
    492 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    493 		sup->su_nbytes -= DINOSIZE(fs);
    494 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
    495 	}
    496 }
    497 
    498 int
    499 findname(struct inodesc * idesc)
    500 {
    501 	struct lfs_direct *dirp = idesc->id_dirp;
    502 	size_t len;
    503 	char *buf;
    504 
    505 	if (dirp->d_ino != idesc->id_parent)
    506 		return (KEEPON);
    507 	if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
    508 		/* Truncate it but don't overflow the buffer */
    509 		len = MAXPATHLEN;
    510 	}
    511 	/* this is namebuf with utils.h */
    512 	buf = __UNCONST(idesc->id_name);
    513 	(void)memcpy(buf, dirp->d_name, len);
    514 	return (STOP | FOUND);
    515 }
    516 
    517 int
    518 findino(struct inodesc * idesc)
    519 {
    520 	struct lfs_direct *dirp = idesc->id_dirp;
    521 
    522 	if (dirp->d_ino == 0)
    523 		return (KEEPON);
    524 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    525 	    dirp->d_ino >= ULFS_ROOTINO && dirp->d_ino < maxino) {
    526 		idesc->id_parent = dirp->d_ino;
    527 		return (STOP | FOUND);
    528 	}
    529 	return (KEEPON);
    530 }
    531 
    532 void
    533 pinode(ino_t ino)
    534 {
    535 	union lfs_dinode *dp;
    536 	struct passwd *pw;
    537 
    538 	printf(" I=%llu ", (unsigned long long)ino);
    539 	if (ino < ULFS_ROOTINO || ino >= maxino)
    540 		return;
    541 	dp = ginode(ino);
    542 	if (dp) {
    543 		printf(" OWNER=");
    544 #ifndef SMALL
    545 		if (Uflag && (pw = getpwuid(lfs_dino_getuid(fs, dp))) != 0)
    546 			printf("%s ", pw->pw_name);
    547 		else
    548 #endif
    549 			printf("%u ", (unsigned)lfs_dino_getuid(fs, dp));
    550 		printf("MODE=%o\n", lfs_dino_getmode(fs, dp));
    551 		if (preen)
    552 			printf("%s: ", cdevname());
    553 		printf("SIZE=%ju ", (uintmax_t) lfs_dino_getsize(fs, dp));
    554 		printf("MTIME=%s ", print_mtime(lfs_dino_getmtime(fs, dp)));
    555 	}
    556 }
    557 
    558 void
    559 blkerror(ino_t ino, const char *type, daddr_t blk)
    560 {
    561 
    562 	pfatal("%lld %s I=%llu", (long long) blk, type,
    563 	    (unsigned long long)ino);
    564 	printf("\n");
    565 	if (exitonfail)
    566 		exit(1);
    567 	switch (statemap[ino]) {
    568 
    569 	case FSTATE:
    570 		statemap[ino] = FCLEAR;
    571 		return;
    572 
    573 	case DSTATE:
    574 		statemap[ino] = DCLEAR;
    575 		return;
    576 
    577 	case FCLEAR:
    578 	case DCLEAR:
    579 		return;
    580 
    581 	default:
    582 		err(EEXIT, "BAD STATE %d TO BLKERR", statemap[ino]);
    583 		/* NOTREACHED */
    584 	}
    585 }
    586 
    587 /*
    588  * allocate an unused inode
    589  */
    590 ino_t
    591 allocino(ino_t request, int type)
    592 {
    593 	ino_t ino;
    594 	union lfs_dinode *dp;
    595 	time_t t;
    596 	struct uvnode *vp;
    597 	struct ubuf *bp;
    598 
    599 	if (request == 0)
    600 		request = ULFS_ROOTINO;
    601 	else if (statemap[request] != USTATE)
    602 		return (0);
    603 	for (ino = request; ino < maxino; ino++)
    604 		if (statemap[ino] == USTATE)
    605 			break;
    606 	if (ino == maxino)
    607 		extend_ifile(fs);
    608 
    609 	switch (type & LFS_IFMT) {
    610 	case LFS_IFDIR:
    611 		statemap[ino] = DSTATE;
    612 		break;
    613 	case LFS_IFREG:
    614 	case LFS_IFLNK:
    615 		statemap[ino] = FSTATE;
    616 		break;
    617 	default:
    618 		return (0);
    619 	}
    620         vp = lfs_valloc(fs, ino);
    621 	if (vp == NULL)
    622 		return (0);
    623 	dp = VTOI(vp)->i_din;
    624 	bp = getblk(vp, 0, lfs_sb_getfsize(fs));
    625 	VOP_BWRITE(bp);
    626 	lfs_dino_setmode(fs, dp, type);
    627 	(void) time(&t);
    628 	lfs_dino_setatime(fs, dp, t);
    629 	lfs_dino_setctime(fs, dp, t);
    630 	lfs_dino_setmtime(fs, dp, t);
    631 	lfs_dino_setsize(fs, dp, lfs_sb_getfsize(fs));
    632 	lfs_dino_setblocks(fs, dp, lfs_btofsb(fs, lfs_sb_getfsize(fs)));
    633 	n_files++;
    634 	inodirty(VTOI(vp));
    635 	typemap[ino] = LFS_IFTODT(type);
    636 	return (ino);
    637 }
    638 
    639 /*
    640  * deallocate an inode
    641  */
    642 void
    643 freeino(ino_t ino)
    644 {
    645 	struct inodesc idesc;
    646 	struct uvnode *vp;
    647 
    648 	memset(&idesc, 0, sizeof(struct inodesc));
    649 	idesc.id_type = ADDR;
    650 	idesc.id_func = pass4check;
    651 	idesc.id_number = ino;
    652 	vp = vget(fs, ino);
    653 	(void) ckinode(VTOD(vp), &idesc);
    654 	clearinode(ino);
    655 	statemap[ino] = USTATE;
    656 	vnode_destroy(vp);
    657 
    658 	n_files--;
    659 }
    660