Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.56
      1 /*	$NetBSD: lfs_alloc.c,v 1.56 2002/05/14 20:03:53 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     71  */
     72 
     73 #include <sys/cdefs.h>
     74 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.56 2002/05/14 20:03:53 perseant Exp $");
     75 
     76 #if defined(_KERNEL_OPT)
     77 #include "opt_quota.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/kernel.h>
     83 #include <sys/buf.h>
     84 #include <sys/lock.h>
     85 #include <sys/vnode.h>
     86 #include <sys/syslog.h>
     87 #include <sys/mount.h>
     88 #include <sys/malloc.h>
     89 #include <sys/pool.h>
     90 #include <sys/proc.h>
     91 
     92 #include <ufs/ufs/quota.h>
     93 #include <ufs/ufs/inode.h>
     94 #include <ufs/ufs/ufsmount.h>
     95 #include <ufs/ufs/ufs_extern.h>
     96 
     97 #include <ufs/lfs/lfs.h>
     98 #include <ufs/lfs/lfs_extern.h>
     99 
    100 extern int lfs_dirvcount;
    101 extern struct lock ufs_hashlock;
    102 
    103 static int extend_ifile(struct lfs *, struct ucred *);
    104 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
    105 
    106 /*
    107  * Allocate a particular inode with a particular version number, freeing
    108  * any previous versions of this inode that may have gone before.
    109  * Used by the roll-forward code.
    110  *
    111  * XXX this function does not have appropriate locking to be used on a live fs;
    112  * XXX but something similar could probably be used for an "undelete" call.
    113  *
    114  * Called with the Ifile inode locked.
    115  */
    116 int
    117 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
    118 	      struct vnode **vpp)
    119 {
    120 	IFILE *ifp;
    121 	struct buf *bp, *cbp;
    122 	struct vnode *vp;
    123 	struct inode *ip;
    124 	ino_t tino, oldnext;
    125 	int error;
    126 	CLEANERINFO *cip;
    127 
    128 	/*
    129 	 * First, just try a vget. If the version number is the one we want,
    130 	 * we don't have to do anything else.  If the version number is wrong,
    131 	 * take appropriate action.
    132 	 */
    133 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    134 	if (error == 0) {
    135 		/* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
    136 
    137 		*vpp = vp;
    138 		ip = VTOI(vp);
    139 		if (ip->i_ffs_gen == version)
    140 			return 0;
    141 		else if (ip->i_ffs_gen < version) {
    142 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    143 			ip->i_ffs_gen = version;
    144 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    145 			return 0;
    146 		} else {
    147 			/* printf("ino %d: asked for version %d but got %d\n",
    148 			       ino, version, ip->i_ffs_gen); */
    149 			vput(vp);
    150 			*vpp = NULLVP;
    151 			return EEXIST;
    152 		}
    153 	}
    154 
    155 	/*
    156 	 * The inode is not in use.  Find it on the free list.
    157 	 */
    158 	/* If the Ifile is too short to contain this inum, extend it */
    159 	while (VTOI(fs->lfs_ivnode)->i_ffs_size <= (ino /
    160 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    161 		<< fs->lfs_bshift) {
    162 		extend_ifile(fs, NOCRED);
    163 	}
    164 
    165 	LFS_IENTRY(ifp, fs, ino, bp);
    166 	oldnext = ifp->if_nextfree;
    167 	ifp->if_version = version;
    168 	brelse(bp);
    169 
    170 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    171 	if (ino) {
    172 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    173 	} else {
    174 		tino = ino;
    175 		while (1) {
    176 			LFS_IENTRY(ifp, fs, tino, bp);
    177 			if (ifp->if_nextfree == ino ||
    178 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    179 				break;
    180 			tino = ifp->if_nextfree;
    181 			brelse(bp);
    182 		}
    183 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    184 			brelse(bp);
    185 			return ENOENT;
    186 		}
    187 		ifp->if_nextfree = oldnext;
    188 		LFS_BWRITE_LOG(bp);
    189 	}
    190 
    191 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
    192 	if (error == 0) {
    193 		/*
    194 		 * Make it VREG so we can put blocks on it.  We will change
    195 		 * this later if it turns out to be some other kind of file.
    196 		 */
    197 		ip = VTOI(vp);
    198 		ip->i_ffs_mode = IFREG;
    199 		ip->i_ffs_nlink = 1;
    200 		ip->i_ffs_effnlink = 1;
    201 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    202 		ip = VTOI(vp);
    203 
    204 		/* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
    205 
    206 		/* The dirop-nature of this vnode is past */
    207 		(void)lfs_vunref(vp);
    208 		--lfs_dirvcount;
    209 		vp->v_flag &= ~VDIROP;
    210 		--fs->lfs_nadirop;
    211 		ip->i_flag &= ~IN_ADIROP;
    212 	}
    213 	*vpp = vp;
    214 	return error;
    215 }
    216 
    217 /*
    218  * Called with the Ifile inode locked.
    219  */
    220 static int
    221 extend_ifile(struct lfs *fs, struct ucred *cred)
    222 {
    223 	struct vnode *vp;
    224 	struct inode *ip;
    225 	IFILE *ifp;
    226 	IFILE_V1 *ifp_v1;
    227 	struct buf *bp, *cbp;
    228 	int error;
    229 	ufs_daddr_t i, blkno, max;
    230 	ino_t oldlast;
    231 	CLEANERINFO *cip;
    232 
    233 	vp = fs->lfs_ivnode;
    234 	ip = VTOI(vp);
    235 	blkno = lblkno(fs, ip->i_ffs_size);
    236 	if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
    237 				&bp)) != 0) {
    238 		return (error);
    239 	}
    240 	ip->i_ffs_size += fs->lfs_bsize;
    241 	uvm_vnp_setsize(vp, ip->i_ffs_size);
    242 
    243 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    244 		fs->lfs_ifpb;
    245 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    246 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    247 #ifdef DIAGNOSTIC
    248 	if (fs->lfs_free == LFS_UNUSED_INUM)
    249 		panic("inode 0 allocated [2]");
    250 #endif /* DIAGNOSTIC */
    251 	max = i + fs->lfs_ifpb;
    252 	/* printf("extend_ifile: new block ino %d--%d\n", i, max - 1); */
    253 
    254 	if (fs->lfs_version == 1) {
    255 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    256 			ifp_v1->if_version = 1;
    257 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    258 			ifp_v1->if_nextfree = ++i;
    259 		}
    260 		ifp_v1--;
    261 		ifp_v1->if_nextfree = oldlast;
    262 	} else {
    263 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    264 			ifp->if_version = 1;
    265 			ifp->if_daddr = LFS_UNUSED_DADDR;
    266 			ifp->if_nextfree = ++i;
    267 		}
    268 		ifp--;
    269 		ifp->if_nextfree = oldlast;
    270 	}
    271 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    272 
    273 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    274 
    275 	return 0;
    276 }
    277 
    278 /* Allocate a new inode. */
    279 /* ARGSUSED */
    280 /* VOP_BWRITE 2i times */
    281 int
    282 lfs_valloc(void *v)
    283 {
    284 	struct vop_valloc_args /* {
    285 				  struct vnode *a_pvp;
    286 				  int a_mode;
    287 				  struct ucred *a_cred;
    288 				  struct vnode **a_vpp;
    289 				  } */ *ap = v;
    290 	struct lfs *fs;
    291 	struct buf *bp, *cbp;
    292 	struct ifile *ifp;
    293 	ino_t new_ino;
    294 	int error;
    295 	int new_gen;
    296 	CLEANERINFO *cip;
    297 
    298 	fs = VTOI(ap->a_pvp)->i_lfs;
    299 	if (fs->lfs_ronly)
    300 		return EROFS;
    301 	*ap->a_vpp = NULL;
    302 
    303 #ifdef LFS_AGGRESSIVE_SEGLOCK
    304 	lfs_seglock(fs, SEGM_PROT);
    305 #else
    306 	if (fs->lfs_version == 1) {
    307 		/*
    308 		 * Use lfs_seglock here, instead of fs->lfs_freelock, to
    309 		 * ensure that the free list is not changed in between
    310 		 * the time that the ifile blocks are written to disk
    311 		 * and the time that the superblock is written to disk.
    312 		 */
    313 		lfs_seglock(fs, SEGM_PROT);
    314 	} else {
    315 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    316 	}
    317 #endif
    318 
    319 	/* Get the head of the freelist. */
    320 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    321 
    322 #ifdef DIAGNOSTIC
    323 	if (new_ino == LFS_UNUSED_INUM) {
    324 #ifdef DEBUG
    325 		lfs_dump_super(fs);
    326 #endif /* DEBUG */
    327 		panic("inode 0 allocated [1]");
    328 	}
    329 #endif /* DIAGNOSTIC */
    330 #ifdef ALLOCPRINT
    331 	printf("lfs_valloc: allocate inode %d\n", new_ino);
    332 #endif
    333 
    334 	/*
    335 	 * Remove the inode from the free list and write the new start
    336 	 * of the free list into the superblock.
    337 	 */
    338 	LFS_IENTRY(ifp, fs, new_ino, bp);
    339 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    340 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    341 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    342 	/* printf("lfs_valloc: headfree %d -> %d\n", new_ino, ifp->if_nextfree); */
    343 
    344 	new_gen = ifp->if_version; /* version was updated by vfree */
    345 	brelse(bp);
    346 
    347 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    348 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    349 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    350 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    351 #ifdef LFS_AGGRESSIVE_SEGLOCK
    352 			lfs_segunlock(fs);
    353 #else
    354 			if (fs->lfs_version == 1)
    355 				lfs_segunlock(fs);
    356 			else
    357 				lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    358 #endif
    359 			return error;
    360 		}
    361 	}
    362 #ifdef DIAGNOSTIC
    363 	if (fs->lfs_free == LFS_UNUSED_INUM)
    364 		panic("inode 0 allocated [3]");
    365 #endif /* DIAGNOSTIC */
    366 
    367 #ifdef LFS_AGGRESSIVE_SEGLOCK
    368 	lfs_segunlock(fs);
    369 #else
    370 	if (fs->lfs_version == 1)
    371 		lfs_segunlock(fs);
    372 	else
    373 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    374 #endif
    375 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    376 }
    377 
    378 static int
    379 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    380 	   struct vnode **vpp)
    381 {
    382 	struct inode *ip;
    383 	struct vnode *vp;
    384 	IFILE *ifp;
    385 	struct buf *bp, *cbp;
    386 	int error;
    387 	CLEANERINFO *cip;
    388 
    389 	error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
    390 	/* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
    391 	if (error)
    392 		goto errout;
    393 
    394 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    395 	/* Create an inode to associate with the vnode. */
    396 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    397 
    398 	ip = VTOI(vp);
    399 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
    400 	/* Zero out the direct and indirect block addresses. */
    401 	bzero(&ip->i_din, sizeof(ip->i_din));
    402 	ip->i_din.ffs_din.di_inumber = new_ino;
    403 
    404 	/* Set a new generation number for this inode. */
    405 	if (new_gen)
    406 		ip->i_ffs_gen = new_gen;
    407 
    408 	/* Insert into the inode hash table. */
    409 	ufs_ihashins(ip);
    410 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    411 
    412 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    413 	ip = VTOI(vp);
    414 	/* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
    415 
    416 	uvm_vnp_setsize(vp, 0);
    417 	*vpp = vp;
    418 #if 1
    419 	if (!(vp->v_flag & VDIROP)) {
    420 		(void)lfs_vref(vp);
    421 		++lfs_dirvcount;
    422 	}
    423 	vp->v_flag |= VDIROP;
    424 
    425 	if (!(ip->i_flag & IN_ADIROP))
    426 		++fs->lfs_nadirop;
    427 	ip->i_flag |= IN_ADIROP;
    428 #endif
    429 	genfs_node_init(vp, &lfs_genfsops);
    430 	VREF(ip->i_devvp);
    431 	/* Set superblock modified bit and increment file count. */
    432 	fs->lfs_fmod = 1;
    433 	++fs->lfs_nfiles;
    434 	return (0);
    435 
    436     errout:
    437 	/*
    438 	 * Put the new inum back on the free list.
    439 	 */
    440 #ifdef LFS_AGGRESSIVE_SEGLOCK
    441 	lfs_seglock(fs, SEGM_PROT);
    442 #endif
    443 	LFS_IENTRY(ifp, fs, new_ino, bp);
    444 	ifp->if_daddr = LFS_UNUSED_DADDR;
    445 	LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    446 	LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    447 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    448 #ifdef LFS_AGGRESSIVE_SEGLOCK
    449 	lfs_segunlock(fs);
    450 #endif
    451 
    452 	*vpp = NULLVP;
    453 	return (error);
    454 }
    455 
    456 /* Create a new vnode/inode pair and initialize what fields we can. */
    457 void
    458 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    459 {
    460 	struct inode *ip;
    461 	struct ufsmount *ump;
    462 #ifdef QUOTA
    463 	int i;
    464 #endif
    465 
    466 	/* Get a pointer to the private mount structure. */
    467 	ump = VFSTOUFS(mp);
    468 
    469 	/* Initialize the inode. */
    470 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    471 	vp->v_data = ip;
    472 	ip->i_vnode = vp;
    473 	ip->i_devvp = ump->um_devvp;
    474 	ip->i_dev = ump->um_dev;
    475 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    476 	ip->i_lfs = ump->um_lfs;
    477 #ifdef QUOTA
    478 	for (i = 0; i < MAXQUOTAS; i++)
    479 		ip->i_dquot[i] = NODQUOT;
    480 #endif
    481 	ip->i_lockf = 0;
    482 	ip->i_diroff = 0;
    483 	ip->i_ffs_mode = 0;
    484 	ip->i_ffs_size = 0;
    485 	ip->i_ffs_blocks = 0;
    486 	ip->i_lfs_effnblks = 0;
    487 	ip->i_flag = 0;
    488 	/* Why was IN_MODIFIED ever set here? */
    489 	/* LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED); */
    490 
    491 #ifdef DEBUG_LFS_VNLOCK
    492 	if (ino == LFS_IFILE_INUM)
    493 		vp->v_vnlock->lk_wmesg = "inlock";
    494 #endif
    495 }
    496 
    497 /* Free an inode. */
    498 /* ARGUSED */
    499 /* VOP_BWRITE 2i times */
    500 int
    501 lfs_vfree(void *v)
    502 {
    503 	struct vop_vfree_args /* {
    504 				 struct vnode *a_pvp;
    505 				 ino_t a_ino;
    506 				 int a_mode;
    507 				 } */ *ap = v;
    508 	SEGUSE *sup;
    509 	CLEANERINFO *cip;
    510 	struct buf *cbp, *bp;
    511 	struct ifile *ifp;
    512 	struct inode *ip;
    513 	struct vnode *vp;
    514 	struct lfs *fs;
    515 	ufs_daddr_t old_iaddr;
    516 	ino_t ino, otail;
    517 	extern int lfs_dirvcount;
    518 	int s;
    519 
    520 	/* Get the inode number and file system. */
    521 	vp = ap->a_pvp;
    522 	ip = VTOI(vp);
    523 	fs = ip->i_lfs;
    524 	ino = ip->i_number;
    525 
    526 	/* Drain of pending writes */
    527 	s = splbio();
    528 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    529 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    530 	splx(s);
    531 
    532 #ifdef LFS_AGGRESSIVE_SEGLOCK
    533 	lfs_seglock(fs, SEGM_PROT); /* XXX */;
    534 #else
    535 	if (fs->lfs_version == 1)
    536 		lfs_seglock(fs, SEGM_PROT);
    537 	else
    538 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    539 #endif
    540 
    541 	if (vp->v_flag & VDIROP) {
    542 		--lfs_dirvcount;
    543 		vp->v_flag &= ~VDIROP;
    544 		wakeup(&lfs_dirvcount);
    545 		lfs_vunref(vp);
    546 	}
    547 	if (ip->i_flag & IN_ADIROP) {
    548 		--fs->lfs_nadirop;
    549 		ip->i_flag &= ~IN_ADIROP;
    550 	}
    551 
    552 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    553 	ip->i_flag &= ~IN_ALLMOD;
    554 
    555 	/*
    556 	 * Set the ifile's inode entry to unused, increment its version number
    557 	 * and link it onto the free chain.
    558 	 */
    559 	LFS_IENTRY(ifp, fs, ino, bp);
    560 	old_iaddr = ifp->if_daddr;
    561 	ifp->if_daddr = LFS_UNUSED_DADDR;
    562 	++ifp->if_version;
    563 	if (fs->lfs_version == 1) {
    564 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    565 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    566 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    567 	} else {
    568 		ifp->if_nextfree = LFS_UNUSED_INUM;
    569 		/*
    570 		 * XXX Writing the freed node here means that it might not
    571 		 * XXX make it into the free list in the event of a crash
    572 		 * XXX (the ifile could be written before the rest of this
    573 		 * XXX completes).
    574 		 */
    575 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    576 		LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    577 		LFS_IENTRY(ifp, fs, otail, bp);
    578 		ifp->if_nextfree = ino;
    579 		LFS_BWRITE_LOG(bp);
    580 		LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    581 		/* printf("lfs_vfree: tailfree %d -> %d\n", otail, ino); */
    582 	}
    583 #ifdef DIAGNOSTIC
    584 	if (ino == LFS_UNUSED_INUM) {
    585 		panic("inode 0 freed");
    586 	}
    587 #endif /* DIAGNOSTIC */
    588 	if (old_iaddr != LFS_UNUSED_DADDR) {
    589 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    590 #ifdef DIAGNOSTIC
    591 		if (sup->su_nbytes < DINODE_SIZE) {
    592 			printf("lfs_vfree: negative byte count"
    593 			       " (segment %d short by %d)\n",
    594 			       dtosn(fs, old_iaddr),
    595 			       (int)DINODE_SIZE - sup->su_nbytes);
    596 			panic("lfs_vfree: negative byte count");
    597 			sup->su_nbytes = DINODE_SIZE;
    598 		}
    599 #endif
    600 		sup->su_nbytes -= DINODE_SIZE;
    601 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    602 	}
    603 
    604 	/* Set superblock modified bit and decrement file count. */
    605 	fs->lfs_fmod = 1;
    606 	--fs->lfs_nfiles;
    607 
    608 #ifdef LFS_AGGRESSIVE_SEGLOCK
    609 	lfs_segunlock(fs);
    610 #else
    611 	if (fs->lfs_version == 1)
    612 		lfs_segunlock(fs);
    613 	else
    614 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    615 #endif
    616 	return (0);
    617 }
    618