Home | History | Annotate | Line # | Download | only in fsck_lfs
inode.c revision 1.46
      1 /* $NetBSD: inode.c,v 1.46 2013/06/08 02:09:35 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 #define _SYS_VNODE_H_ /* XXX */
     69 #include <ufs/lfs/ulfs_inode.h>
     70 #include <ufs/lfs/ulfs_dir.h>
     71 #include <ufs/lfs/lfs.h>
     72 #undef vnode
     73 
     74 #include <err.h>
     75 #ifndef SMALL
     76 #include <pwd.h>
     77 #endif
     78 #include <stdio.h>
     79 #include <stdlib.h>
     80 #include <string.h>
     81 #include <util.h>
     82 
     83 #include "bufcache.h"
     84 #include "vnode.h"
     85 #include "lfs_user.h"
     86 
     87 #include "fsck.h"
     88 #include "fsutil.h"
     89 #include "extern.h"
     90 
     91 extern SEGUSE *seg_table;
     92 extern ulfs_daddr_t *din_table;
     93 
     94 static int iblock(struct inodesc *, long, u_int64_t);
     95 int blksreqd(struct lfs *, int);
     96 int lfs_maxino(void);
     97 
     98 /*
     99  * Get a dinode of a given inum.
    100  * XXX combine this function with vget.
    101  */
    102 struct ulfs1_dinode *
    103 ginode(ino_t ino)
    104 {
    105 	struct uvnode *vp;
    106 	struct ubuf *bp;
    107 	IFILE *ifp;
    108 
    109 	vp = vget(fs, ino);
    110 	if (vp == NULL)
    111 		return NULL;
    112 
    113 	if (din_table[ino] == 0x0) {
    114 		LFS_IENTRY(ifp, fs, ino, bp);
    115 		din_table[ino] = ifp->if_daddr;
    116 		seg_table[dtosn(fs, ifp->if_daddr)].su_nbytes += LFS_DINODE1_SIZE;
    117 		brelse(bp, 0);
    118 	}
    119 	return (VTOI(vp)->i_din.ffs1_din);
    120 }
    121 
    122 /*
    123  * Check validity of held blocks in an inode, recursing through all blocks.
    124  */
    125 int
    126 ckinode(struct ulfs1_dinode *dp, struct inodesc *idesc)
    127 {
    128 	ulfs_daddr_t *ap, lbn;
    129 	long ret, n, ndb, offset;
    130 	struct ulfs1_dinode dino;
    131 	u_int64_t remsize, sizepb;
    132 	mode_t mode;
    133 	char pathbuf[MAXPATHLEN + 1];
    134 	struct uvnode *vp, *thisvp;
    135 
    136 	if (idesc->id_fix != IGNORE)
    137 		idesc->id_fix = DONTKNOW;
    138 	idesc->id_entryno = 0;
    139 	idesc->id_filesize = dp->di_size;
    140 	mode = dp->di_mode & IFMT;
    141 	if (mode == IFBLK || mode == IFCHR ||
    142 	    (mode == IFLNK && (dp->di_size < fs->lfs_maxsymlinklen ||
    143 		    (fs->lfs_maxsymlinklen == 0 &&
    144 			dp->di_blocks == 0))))
    145 		return (KEEPON);
    146 	dino = *dp;
    147 	ndb = howmany(dino.di_size, fs->lfs_bsize);
    148 
    149 	thisvp = vget(fs, idesc->id_number);
    150 	for (lbn = 0; lbn < ULFS_NDADDR; lbn++) {
    151 		ap = dino.di_db + lbn;
    152 		if (thisvp)
    153 			idesc->id_numfrags =
    154 				numfrags(fs, VTOI(thisvp)->i_lfs_fragsize[lbn]);
    155 		else {
    156 			if (--ndb == 0 && (offset = blkoff(fs, dino.di_size)) != 0) {
    157 				idesc->id_numfrags =
    158 			    	numfrags(fs, fragroundup(fs, offset));
    159 			} else
    160 				idesc->id_numfrags = fs->lfs_frag;
    161 		}
    162 		if (*ap == 0) {
    163 			if (idesc->id_type == DATA && ndb >= 0) {
    164 				/* An empty block in a directory XXX */
    165 				getpathname(pathbuf, sizeof(pathbuf),
    166 				    idesc->id_number, idesc->id_number);
    167 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [1]",
    168 				    pathbuf, (long long)idesc->id_number);
    169 				if (reply("ADJUST LENGTH") == 1) {
    170 					vp = vget(fs, idesc->id_number);
    171 					dp = VTOD(vp);
    172 					dp->di_size = (ap - &dino.di_db[0]) *
    173 					    fs->lfs_bsize;
    174 					printf(
    175 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    176 					rerun = 1;
    177 					inodirty(VTOI(vp));
    178 				} else
    179 					break;
    180 			}
    181 			continue;
    182 		}
    183 		idesc->id_blkno = *ap;
    184 		idesc->id_lblkno = ap - &dino.di_db[0];
    185 		if (idesc->id_type == ADDR) {
    186 			ret = (*idesc->id_func) (idesc);
    187 		} else
    188 			ret = dirscan(idesc);
    189 		if (ret & STOP)
    190 			return (ret);
    191 	}
    192 	idesc->id_numfrags = fs->lfs_frag;
    193 	remsize = dino.di_size - fs->lfs_bsize * ULFS_NDADDR;
    194 	sizepb = fs->lfs_bsize;
    195 	for (ap = &dino.di_ib[0], n = 1; n <= ULFS_NIADDR; ap++, n++) {
    196 		if (*ap) {
    197 			idesc->id_blkno = *ap;
    198 			ret = iblock(idesc, n, remsize);
    199 			if (ret & STOP)
    200 				return (ret);
    201 		} else {
    202 			if (idesc->id_type == DATA && remsize > 0) {
    203 				/* An empty block in a directory XXX */
    204 				getpathname(pathbuf, sizeof(pathbuf),
    205 				    idesc->id_number, idesc->id_number);
    206 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [2]",
    207 				    pathbuf, (long long)idesc->id_number);
    208 				if (reply("ADJUST LENGTH") == 1) {
    209 					vp = vget(fs, idesc->id_number);
    210 					dp = VTOD(vp);
    211 					dp->di_size -= remsize;
    212 					remsize = 0;
    213 					printf(
    214 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    215 					rerun = 1;
    216 					inodirty(VTOI(vp));
    217 					break;
    218 				} else
    219 					break;
    220 			}
    221 		}
    222 		sizepb *= NINDIR(fs);
    223 		remsize -= sizepb;
    224 	}
    225 	return (KEEPON);
    226 }
    227 
    228 static int
    229 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
    230 {
    231 	ulfs_daddr_t *ap, *aplim;
    232 	struct ubuf *bp;
    233 	int i, n, (*func) (struct inodesc *), nif;
    234 	u_int64_t sizepb;
    235 	char pathbuf[MAXPATHLEN + 1], buf[BUFSIZ];
    236 	struct uvnode *devvp, *vp;
    237 	int diddirty = 0;
    238 
    239 	if (idesc->id_type == ADDR) {
    240 		func = idesc->id_func;
    241 		n = (*func) (idesc);
    242 		if ((n & KEEPON) == 0)
    243 			return (n);
    244 	} else
    245 		func = dirscan;
    246 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
    247 		return (SKIP);
    248 
    249 	devvp = fs->lfs_devvp;
    250 	bread(devvp, fsbtodb(fs, idesc->id_blkno), fs->lfs_bsize,
    251 	    NOCRED, 0, &bp);
    252 	ilevel--;
    253 	for (sizepb = fs->lfs_bsize, i = 0; i < ilevel; i++)
    254 		sizepb *= NINDIR(fs);
    255 	if (isize > sizepb * NINDIR(fs))
    256 		nif = NINDIR(fs);
    257 	else
    258 		nif = howmany(isize, sizepb);
    259 	if (idesc->id_func == pass1check && nif < NINDIR(fs)) {
    260 		aplim = ((ulfs_daddr_t *) bp->b_data) + NINDIR(fs);
    261 		for (ap = ((ulfs_daddr_t *) bp->b_data) + nif; ap < aplim; ap++) {
    262 			if (*ap == 0)
    263 				continue;
    264 			(void) sprintf(buf, "PARTIALLY TRUNCATED INODE I=%llu",
    265 			    (unsigned long long)idesc->id_number);
    266 			if (dofix(idesc, buf)) {
    267 				*ap = 0;
    268 				++diddirty;
    269 			}
    270 		}
    271 	}
    272 	aplim = ((ulfs_daddr_t *) bp->b_data) + nif;
    273 	for (ap = ((ulfs_daddr_t *) bp->b_data); ap < aplim; ap++) {
    274 		if (*ap) {
    275 			idesc->id_blkno = *ap;
    276 			if (ilevel == 0) {
    277 				/*
    278 				 * dirscan needs lblkno.
    279 				 */
    280 				idesc->id_lblkno++;
    281 				n = (*func) (idesc);
    282 			} else {
    283 				n = iblock(idesc, ilevel, isize);
    284 			}
    285 			if (n & STOP) {
    286 				if (diddirty)
    287 					VOP_BWRITE(bp);
    288 				else
    289 					brelse(bp, 0);
    290 				return (n);
    291 			}
    292 		} else {
    293 			if (idesc->id_type == DATA && isize > 0) {
    294 				/* An empty block in a directory XXX */
    295 				getpathname(pathbuf, sizeof(pathbuf),
    296 				    idesc->id_number, idesc->id_number);
    297 				pfatal("DIRECTORY %s INO %lld: CONTAINS EMPTY BLOCKS [3]",
    298 				    pathbuf, (long long)idesc->id_number);
    299 				if (reply("ADJUST LENGTH") == 1) {
    300 					vp = vget(fs, idesc->id_number);
    301 					VTOI(vp)->i_ffs1_size -= isize;
    302 					isize = 0;
    303 					printf(
    304 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
    305 					rerun = 1;
    306 					inodirty(VTOI(vp));
    307 					if (diddirty)
    308 						VOP_BWRITE(bp);
    309 					else
    310 						brelse(bp, 0);
    311 					return (STOP);
    312 				}
    313 			}
    314 		}
    315 		isize -= sizepb;
    316 	}
    317 	if (diddirty)
    318 		VOP_BWRITE(bp);
    319 	else
    320 		brelse(bp, 0);
    321 	return (KEEPON);
    322 }
    323 
    324 /*
    325  * Check that a block in a legal block number.
    326  * Return 0 if in range, 1 if out of range.
    327  */
    328 int
    329 chkrange(daddr_t blk, int cnt)
    330 {
    331 	if (blk < sntod(fs, 0)) {
    332 		return (1);
    333 	}
    334 	if (blk > maxfsblock) {
    335 		return (1);
    336 	}
    337 	if (blk + cnt < sntod(fs, 0)) {
    338 		return (1);
    339 	}
    340 	if (blk + cnt > maxfsblock) {
    341 		return (1);
    342 	}
    343 	return (0);
    344 }
    345 
    346 /*
    347  * Routines to maintain information about directory inodes.
    348  * This is built during the first pass and used during the
    349  * second and third passes.
    350  *
    351  * Enter inodes into the cache.
    352  */
    353 void
    354 cacheino(struct ulfs1_dinode * dp, ino_t inumber)
    355 {
    356 	struct inoinfo *inp;
    357 	struct inoinfo **inpp, **ninpsort;
    358 	unsigned int blks;
    359 
    360 	blks = howmany(dp->di_size, fs->lfs_bsize);
    361 	if (blks > ULFS_NDADDR)
    362 		blks = ULFS_NDADDR + ULFS_NIADDR;
    363 	inp = emalloc(sizeof(*inp) + (blks - 1) * sizeof(ulfs_daddr_t));
    364 	inpp = &inphead[inumber % numdirs];
    365 	inp->i_nexthash = *inpp;
    366 	*inpp = inp;
    367 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
    368 	if (inumber == ULFS_ROOTINO)
    369 		inp->i_parent = ULFS_ROOTINO;
    370 	else
    371 		inp->i_parent = (ino_t) 0;
    372 	inp->i_dotdot = (ino_t) 0;
    373 	inp->i_number = inumber;
    374 	inp->i_isize = dp->di_size;
    375 
    376 	inp->i_numblks = blks * sizeof(ulfs_daddr_t);
    377 	memcpy(&inp->i_blks[0], &dp->di_db[0], (size_t) inp->i_numblks);
    378 	if (inplast == listmax) {
    379 		ninpsort = erealloc(inpsort,
    380 		    (listmax + 100) * sizeof(struct inoinfo *));
    381 		inpsort = ninpsort;
    382 		listmax += 100;
    383 	}
    384 	inpsort[inplast++] = inp;
    385 }
    386 
    387 /*
    388  * Look up an inode cache structure.
    389  */
    390 struct inoinfo *
    391 getinoinfo(ino_t inumber)
    392 {
    393 	struct inoinfo *inp;
    394 
    395 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
    396 		if (inp->i_number != inumber)
    397 			continue;
    398 		return (inp);
    399 	}
    400 	err(EEXIT, "cannot find inode %llu\n", (unsigned long long)inumber);
    401 	return ((struct inoinfo *) 0);
    402 }
    403 
    404 /*
    405  * Clean up all the inode cache structure.
    406  */
    407 void
    408 inocleanup(void)
    409 {
    410 	struct inoinfo **inpp;
    411 
    412 	if (inphead == NULL)
    413 		return;
    414 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
    415 		free((char *) (*inpp));
    416 	free((char *) inphead);
    417 	free((char *) inpsort);
    418 	inphead = inpsort = NULL;
    419 }
    420 
    421 void
    422 inodirty(struct inode *ip)
    423 {
    424 	ip->i_flag |= IN_MODIFIED;
    425 }
    426 
    427 void
    428 clri(struct inodesc * idesc, const char *type, int flag)
    429 {
    430 	struct uvnode *vp;
    431 
    432 	vp = vget(fs, idesc->id_number);
    433 	if (flag & 0x1) {
    434 		pwarn("%s %s", type,
    435 		      (VTOI(vp)->i_ffs1_mode & IFMT) == IFDIR ? "DIR" : "FILE");
    436 		pinode(idesc->id_number);
    437 	}
    438 	if ((flag & 0x2) || preen || reply("CLEAR") == 1) {
    439 		if (preen && flag != 2)
    440 			printf(" (CLEARED)\n");
    441 		n_files--;
    442 		(void) ckinode(VTOD(vp), idesc);
    443 		clearinode(idesc->id_number);
    444 		statemap[idesc->id_number] = USTATE;
    445 		vnode_destroy(vp);
    446 		return;
    447 	}
    448 	return;
    449 }
    450 
    451 void
    452 clearinode(ino_t inumber)
    453 {
    454 	struct ubuf *bp;
    455 	IFILE *ifp;
    456 	daddr_t daddr;
    457 
    458 	/* Send cleared inode to the free list */
    459 
    460 	LFS_IENTRY(ifp, fs, inumber, bp);
    461 	daddr = ifp->if_daddr;
    462 	if (daddr == LFS_UNUSED_DADDR) {
    463 		brelse(bp, 0);
    464 		return;
    465 	}
    466 	ifp->if_daddr = LFS_UNUSED_DADDR;
    467 	ifp->if_nextfree = fs->lfs_freehd;
    468 	fs->lfs_freehd = inumber;
    469 	sbdirty();
    470 	VOP_BWRITE(bp);
    471 
    472 	/*
    473 	 * update segment usage.
    474 	 */
    475 	if (daddr != LFS_UNUSED_DADDR) {
    476 		SEGUSE *sup;
    477 		u_int32_t oldsn = dtosn(fs, daddr);
    478 
    479 		seg_table[oldsn].su_nbytes -= LFS_DINODE1_SIZE;
    480 		LFS_SEGENTRY(sup, fs, oldsn, bp);
    481 		sup->su_nbytes -= LFS_DINODE1_SIZE;
    482 		LFS_WRITESEGENTRY(sup, fs, oldsn, bp);	/* Ifile */
    483 	}
    484 }
    485 
    486 int
    487 findname(struct inodesc * idesc)
    488 {
    489 	struct direct *dirp = idesc->id_dirp;
    490 	size_t len;
    491 	char *buf;
    492 
    493 	if (dirp->d_ino != idesc->id_parent)
    494 		return (KEEPON);
    495 	if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
    496 		/* Truncate it but don't overflow the buffer */
    497 		len = MAXPATHLEN;
    498 	}
    499 	/* this is namebuf with utils.h */
    500 	buf = __UNCONST(idesc->id_name);
    501 	(void)memcpy(buf, dirp->d_name, len);
    502 	return (STOP | FOUND);
    503 }
    504 
    505 int
    506 findino(struct inodesc * idesc)
    507 {
    508 	struct direct *dirp = idesc->id_dirp;
    509 
    510 	if (dirp->d_ino == 0)
    511 		return (KEEPON);
    512 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
    513 	    dirp->d_ino >= ULFS_ROOTINO && dirp->d_ino < maxino) {
    514 		idesc->id_parent = dirp->d_ino;
    515 		return (STOP | FOUND);
    516 	}
    517 	return (KEEPON);
    518 }
    519 
    520 void
    521 pinode(ino_t ino)
    522 {
    523 	struct ulfs1_dinode *dp;
    524 	struct passwd *pw;
    525 
    526 	printf(" I=%llu ", (unsigned long long)ino);
    527 	if (ino < ULFS_ROOTINO || ino >= maxino)
    528 		return;
    529 	dp = ginode(ino);
    530 	if (dp) {
    531 		printf(" OWNER=");
    532 #ifndef SMALL
    533 		if (Uflag && (pw = getpwuid((int) dp->di_uid)) != 0)
    534 			printf("%s ", pw->pw_name);
    535 		else
    536 #endif
    537 			printf("%u ", (unsigned) dp->di_uid);
    538 		printf("MODE=%o\n", dp->di_mode);
    539 		if (preen)
    540 			printf("%s: ", cdevname());
    541 		printf("SIZE=%llu ", (unsigned long long) dp->di_size);
    542 		printf("MTIME=%s ", print_mtime(dp->di_mtime));
    543 	}
    544 }
    545 
    546 void
    547 blkerror(ino_t ino, const char *type, daddr_t blk)
    548 {
    549 
    550 	pfatal("%lld %s I=%llu", (long long) blk, type,
    551 	    (unsigned long long)ino);
    552 	printf("\n");
    553 	if (exitonfail)
    554 		exit(1);
    555 	switch (statemap[ino]) {
    556 
    557 	case FSTATE:
    558 		statemap[ino] = FCLEAR;
    559 		return;
    560 
    561 	case DSTATE:
    562 		statemap[ino] = DCLEAR;
    563 		return;
    564 
    565 	case FCLEAR:
    566 	case DCLEAR:
    567 		return;
    568 
    569 	default:
    570 		err(EEXIT, "BAD STATE %d TO BLKERR\n", statemap[ino]);
    571 		/* NOTREACHED */
    572 	}
    573 }
    574 
    575 /*
    576  * allocate an unused inode
    577  */
    578 ino_t
    579 allocino(ino_t request, int type)
    580 {
    581 	ino_t ino;
    582 	struct ulfs1_dinode *dp;
    583 	time_t t;
    584 	struct uvnode *vp;
    585 	struct ubuf *bp;
    586 
    587 	if (request == 0)
    588 		request = ULFS_ROOTINO;
    589 	else if (statemap[request] != USTATE)
    590 		return (0);
    591 	for (ino = request; ino < maxino; ino++)
    592 		if (statemap[ino] == USTATE)
    593 			break;
    594 	if (ino == maxino)
    595 		extend_ifile(fs);
    596 
    597 	switch (type & IFMT) {
    598 	case IFDIR:
    599 		statemap[ino] = DSTATE;
    600 		break;
    601 	case IFREG:
    602 	case IFLNK:
    603 		statemap[ino] = FSTATE;
    604 		break;
    605 	default:
    606 		return (0);
    607 	}
    608         vp = lfs_valloc(fs, ino);
    609 	if (vp == NULL)
    610 		return (0);
    611 	dp = (VTOI(vp)->i_din.ffs1_din);
    612 	bp = getblk(vp, 0, fs->lfs_fsize);
    613 	VOP_BWRITE(bp);
    614 	dp->di_mode = type;
    615 	(void) time(&t);
    616 	dp->di_atime = t;
    617 	dp->di_mtime = dp->di_ctime = dp->di_atime;
    618 	dp->di_size = fs->lfs_fsize;
    619 	dp->di_blocks = btofsb(fs, fs->lfs_fsize);
    620 	n_files++;
    621 	inodirty(VTOI(vp));
    622 	typemap[ino] = IFTODT(type);
    623 	return (ino);
    624 }
    625 
    626 /*
    627  * deallocate an inode
    628  */
    629 void
    630 freeino(ino_t ino)
    631 {
    632 	struct inodesc idesc;
    633 	struct uvnode *vp;
    634 
    635 	memset(&idesc, 0, sizeof(struct inodesc));
    636 	idesc.id_type = ADDR;
    637 	idesc.id_func = pass4check;
    638 	idesc.id_number = ino;
    639 	vp = vget(fs, ino);
    640 	(void) ckinode(VTOD(vp), &idesc);
    641 	clearinode(ino);
    642 	statemap[ino] = USTATE;
    643 	vnode_destroy(vp);
    644 
    645 	n_files--;
    646 }
    647