Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.55
      1 /*	$NetBSD: lfs_alloc.c,v 1.55 2002/02/04 03:32:16 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.55 2002/02/04 03:32:16 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/vnode.h>
     85 #include <sys/syslog.h>
     86 #include <sys/mount.h>
     87 #include <sys/malloc.h>
     88 #include <sys/pool.h>
     89 #include <sys/proc.h>
     90 
     91 #include <ufs/ufs/quota.h>
     92 #include <ufs/ufs/inode.h>
     93 #include <ufs/ufs/ufsmount.h>
     94 #include <ufs/ufs/ufs_extern.h>
     95 
     96 #include <ufs/lfs/lfs.h>
     97 #include <ufs/lfs/lfs_extern.h>
     98 
     99 extern int lfs_dirvcount;
    100 extern struct lock ufs_hashlock;
    101 
    102 static int extend_ifile(struct lfs *, struct ucred *);
    103 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
    104 
    105 /*
    106  * Allocate a particular inode with a particular version number, freeing
    107  * any previous versions of this inode that may have gone before.
    108  * Used by the roll-forward code.
    109  *
    110  * XXX this function does not have appropriate locking to be used on a live fs;
    111  * XXX but something similar could probably be used for an "undelete" call.
    112  */
    113 int
    114 lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
    115 	      struct vnode **vpp)
    116 {
    117 	IFILE *ifp;
    118 	struct buf *bp, *cbp;
    119 	struct vnode *vp;
    120 	struct inode *ip;
    121 	ino_t tino, oldnext;
    122 	int error;
    123 	CLEANERINFO *cip;
    124 
    125 	/*
    126 	 * First, just try a vget. If the version number is the one we want,
    127 	 * we don't have to do anything else.  If the version number is wrong,
    128 	 * take appropriate action.
    129 	 */
    130 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    131 	if (error == 0) {
    132 		/* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
    133 
    134 		*vpp = vp;
    135 		ip = VTOI(vp);
    136 		if (ip->i_ffs_gen == version)
    137 			return 0;
    138 		else if (ip->i_ffs_gen < version) {
    139 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    140 			ip->i_ffs_gen = version;
    141 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    142 			return 0;
    143 		} else {
    144 			/* printf("ino %d: asked for version %d but got %d\n",
    145 			       ino, version, ip->i_ffs_gen); */
    146 			vput(vp);
    147 			*vpp = NULLVP;
    148 			return EEXIST;
    149 		}
    150 	}
    151 
    152 	/*
    153 	 * The inode is not in use.  Find it on the free list.
    154 	 */
    155 	/* If the Ifile is too short to contain this inum, extend it */
    156 	while (VTOI(fs->lfs_ivnode)->i_ffs_size <= (ino /
    157 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    158 		<< fs->lfs_bshift) {
    159 		extend_ifile(fs, NOCRED);
    160 	}
    161 
    162 	LFS_IENTRY(ifp, fs, ino, bp);
    163 	oldnext = ifp->if_nextfree;
    164 	ifp->if_version = version;
    165 	brelse(bp);
    166 
    167 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    168 	if (ino) {
    169 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    170 	} else {
    171 		tino = ino;
    172 		while (1) {
    173 			LFS_IENTRY(ifp, fs, tino, bp);
    174 			if (ifp->if_nextfree == ino ||
    175 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    176 				break;
    177 			tino = ifp->if_nextfree;
    178 			brelse(bp);
    179 		}
    180 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    181 			brelse(bp);
    182 			return ENOENT;
    183 		}
    184 		ifp->if_nextfree = oldnext;
    185 		VOP_BWRITE(bp);
    186 	}
    187 
    188 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
    189 	if (error == 0) {
    190 		/*
    191 		 * Make it VREG so we can put blocks on it.  We will change
    192 		 * this later if it turns out to be some other kind of file.
    193 		 */
    194 		ip = VTOI(vp);
    195 		ip->i_ffs_mode = IFREG;
    196 		ip->i_ffs_nlink = 1;
    197 		ip->i_ffs_effnlink = 1;
    198 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    199 		ip = VTOI(vp);
    200 
    201 		/* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
    202 
    203 		/* The dirop-nature of this vnode is past */
    204 		(void)lfs_vunref(vp);
    205 		--lfs_dirvcount;
    206 		vp->v_flag &= ~VDIROP;
    207 		--fs->lfs_nadirop;
    208 		ip->i_flag &= ~IN_ADIROP;
    209 	}
    210 	*vpp = vp;
    211 	return error;
    212 }
    213 
    214 static int
    215 extend_ifile(struct lfs *fs, struct ucred *cred)
    216 {
    217 	struct vnode *vp;
    218 	struct inode *ip;
    219 	IFILE *ifp;
    220 	IFILE_V1 *ifp_v1;
    221 	struct buf *bp, *cbp;
    222 	int error;
    223 	ufs_daddr_t i, blkno, max;
    224 	ino_t oldlast;
    225 	CLEANERINFO *cip;
    226 
    227 	vp = fs->lfs_ivnode;
    228 	(void)lfs_vref(vp);
    229 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    230 	ip = VTOI(vp);
    231 	blkno = lblkno(fs, ip->i_ffs_size);
    232 	if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
    233 				&bp)) != 0) {
    234 		VOP_UNLOCK(vp, 0);
    235 		lfs_vunref(vp);
    236 		return (error);
    237 	}
    238 	ip->i_ffs_size += fs->lfs_bsize;
    239 	uvm_vnp_setsize(vp, ip->i_ffs_size);
    240 	VOP_UNLOCK(vp, 0);
    241 
    242 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    243 		fs->lfs_ifpb;
    244 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    245 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    246 #ifdef DIAGNOSTIC
    247 	if (fs->lfs_free == LFS_UNUSED_INUM)
    248 		panic("inode 0 allocated [2]");
    249 #endif /* DIAGNOSTIC */
    250 	max = i + fs->lfs_ifpb;
    251 	/* printf("extend_ifile: new block ino %d--%d\n", i, max - 1); */
    252 
    253 	if (fs->lfs_version == 1) {
    254 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    255 			ifp_v1->if_version = 1;
    256 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    257 			ifp_v1->if_nextfree = ++i;
    258 		}
    259 		ifp_v1--;
    260 		ifp_v1->if_nextfree = oldlast;
    261 	} else {
    262 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    263 			ifp->if_version = 1;
    264 			ifp->if_daddr = LFS_UNUSED_DADDR;
    265 			ifp->if_nextfree = ++i;
    266 		}
    267 		ifp--;
    268 		ifp->if_nextfree = oldlast;
    269 	}
    270 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    271 
    272 	(void) VOP_BWRITE(bp); /* Ifile */
    273 	lfs_vunref(vp);
    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 	if (fs->lfs_version == 1) {
    304 		/*
    305 		 * Use lfs_seglock here, instead of fs->lfs_freelock, to
    306 		 * ensure that the free list is not changed in between
    307 		 * the time that the ifile blocks are written to disk
    308 		 * and the time that the superblock is written to disk.
    309 		 */
    310 		lfs_seglock(fs, SEGM_PROT);
    311 	} else {
    312 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    313 	}
    314 
    315 	/* Get the head of the freelist. */
    316 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    317 
    318 #ifdef DIAGNOSTIC
    319 	if (new_ino == LFS_UNUSED_INUM) {
    320 #ifdef DEBUG
    321 		lfs_dump_super(fs);
    322 #endif /* DEBUG */
    323 		panic("inode 0 allocated [1]");
    324 	}
    325 #endif /* DIAGNOSTIC */
    326 #ifdef ALLOCPRINT
    327 	printf("lfs_valloc: allocate inode %d\n", new_ino);
    328 #endif
    329 
    330 	/*
    331 	 * Remove the inode from the free list and write the new start
    332 	 * of the free list into the superblock.
    333 	 */
    334 	LFS_IENTRY(ifp, fs, new_ino, bp);
    335 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    336 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    337 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    338 	/* printf("lfs_valloc: headfree %d -> %d\n", new_ino, ifp->if_nextfree); */
    339 
    340 	new_gen = ifp->if_version; /* version was updated by vfree */
    341 	brelse(bp);
    342 
    343 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    344 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    345 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    346 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    347 			if (fs->lfs_version == 1)
    348 				lfs_segunlock(fs);
    349 			else
    350 				lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    351 			return error;
    352 		}
    353 	}
    354 #ifdef DIAGNOSTIC
    355 	if (fs->lfs_free == LFS_UNUSED_INUM)
    356 		panic("inode 0 allocated [3]");
    357 #endif /* DIAGNOSTIC */
    358 
    359 	if (fs->lfs_version == 1)
    360 		lfs_segunlock(fs);
    361 	else
    362 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    363 
    364 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    365 }
    366 
    367 static int
    368 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    369 	   struct vnode **vpp)
    370 {
    371 	struct inode *ip;
    372 	struct vnode *vp;
    373 	IFILE *ifp;
    374 	struct buf *bp, *cbp;
    375 	int error;
    376 	CLEANERINFO *cip;
    377 
    378 	error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
    379 	/* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
    380 	if (error)
    381 		goto errout;
    382 
    383 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    384 	/* Create an inode to associate with the vnode. */
    385 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    386 
    387 	ip = VTOI(vp);
    388 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
    389 	/* Zero out the direct and indirect block addresses. */
    390 	bzero(&ip->i_din, sizeof(ip->i_din));
    391 	ip->i_din.ffs_din.di_inumber = new_ino;
    392 
    393 	/* Set a new generation number for this inode. */
    394 	if (new_gen)
    395 		ip->i_ffs_gen = new_gen;
    396 
    397 	/* Insert into the inode hash table. */
    398 	ufs_ihashins(ip);
    399 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    400 
    401 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    402 	ip = VTOI(vp);
    403 	/* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
    404 
    405 	uvm_vnp_setsize(vp, 0);
    406 	*vpp = vp;
    407 #if 1
    408 	if (!(vp->v_flag & VDIROP)) {
    409 		(void)lfs_vref(vp);
    410 		++lfs_dirvcount;
    411 	}
    412 	vp->v_flag |= VDIROP;
    413 
    414 	if (!(ip->i_flag & IN_ADIROP))
    415 		++fs->lfs_nadirop;
    416 	ip->i_flag |= IN_ADIROP;
    417 #endif
    418 	genfs_node_init(vp, &lfs_genfsops);
    419 	VREF(ip->i_devvp);
    420 	/* Set superblock modified bit and increment file count. */
    421 	fs->lfs_fmod = 1;
    422 	++fs->lfs_nfiles;
    423 	return (0);
    424 
    425     errout:
    426 	/*
    427 	 * Put the new inum back on the free list.
    428 	 */
    429 	LFS_IENTRY(ifp, fs, new_ino, bp);
    430 	ifp->if_daddr = LFS_UNUSED_DADDR;
    431 	LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    432 	LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    433 	(void) VOP_BWRITE(bp); /* Ifile */
    434 
    435 	*vpp = NULLVP;
    436 	return (error);
    437 }
    438 
    439 /* Create a new vnode/inode pair and initialize what fields we can. */
    440 void
    441 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    442 {
    443 	struct inode *ip;
    444 	struct ufsmount *ump;
    445 #ifdef QUOTA
    446 	int i;
    447 #endif
    448 
    449 	/* Get a pointer to the private mount structure. */
    450 	ump = VFSTOUFS(mp);
    451 
    452 	/* Initialize the inode. */
    453 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    454 	vp->v_data = ip;
    455 	ip->i_vnode = vp;
    456 	ip->i_devvp = ump->um_devvp;
    457 	ip->i_dev = ump->um_dev;
    458 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    459 	ip->i_lfs = ump->um_lfs;
    460 #ifdef QUOTA
    461 	for (i = 0; i < MAXQUOTAS; i++)
    462 		ip->i_dquot[i] = NODQUOT;
    463 #endif
    464 	ip->i_lockf = 0;
    465 	ip->i_diroff = 0;
    466 	ip->i_ffs_mode = 0;
    467 	ip->i_ffs_size = 0;
    468 	ip->i_ffs_blocks = 0;
    469 	ip->i_lfs_effnblks = 0;
    470 	ip->i_flag = 0;
    471 	/* Why was IN_MODIFIED ever set here? */
    472 	/* LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED); */
    473 }
    474 
    475 /* Free an inode. */
    476 /* ARGUSED */
    477 /* VOP_BWRITE 2i times */
    478 int
    479 lfs_vfree(void *v)
    480 {
    481 	struct vop_vfree_args /* {
    482 				 struct vnode *a_pvp;
    483 				 ino_t a_ino;
    484 				 int a_mode;
    485 				 } */ *ap = v;
    486 	SEGUSE *sup;
    487 	CLEANERINFO *cip;
    488 	struct buf *cbp, *bp;
    489 	struct ifile *ifp;
    490 	struct inode *ip;
    491 	struct vnode *vp;
    492 	struct lfs *fs;
    493 	ufs_daddr_t old_iaddr;
    494 	ino_t ino, otail;
    495 	extern int lfs_dirvcount;
    496 
    497 	/* Get the inode number and file system. */
    498 	vp = ap->a_pvp;
    499 	ip = VTOI(vp);
    500 	fs = ip->i_lfs;
    501 	ino = ip->i_number;
    502 
    503 	/* Drain of pending writes */
    504 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    505 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    506 
    507 	if (fs->lfs_version == 1)
    508 		lfs_seglock(fs, SEGM_PROT);
    509 	else
    510 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    511 
    512 	if (vp->v_flag & VDIROP) {
    513 		--lfs_dirvcount;
    514 		vp->v_flag &= ~VDIROP;
    515 		wakeup(&lfs_dirvcount);
    516 		lfs_vunref(vp);
    517 	}
    518 	if (ip->i_flag & IN_ADIROP) {
    519 		--fs->lfs_nadirop;
    520 		ip->i_flag &= ~IN_ADIROP;
    521 	}
    522 
    523 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    524 	ip->i_flag &= ~IN_ALLMOD;
    525 
    526 	/*
    527 	 * Set the ifile's inode entry to unused, increment its version number
    528 	 * and link it onto the free chain.
    529 	 */
    530 	LFS_IENTRY(ifp, fs, ino, bp);
    531 	old_iaddr = ifp->if_daddr;
    532 	ifp->if_daddr = LFS_UNUSED_DADDR;
    533 	++ifp->if_version;
    534 	if (fs->lfs_version == 1) {
    535 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    536 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    537 		(void) VOP_BWRITE(bp); /* Ifile */
    538 	} else {
    539 		ifp->if_nextfree = LFS_UNUSED_INUM;
    540 		/*
    541 		 * XXX Writing the freed node here means that it might not
    542 		 * XXX make it into the free list in the event of a crash
    543 		 * XXX (the ifile could be written before the rest of this
    544 		 * XXX completes).
    545 		 */
    546 		(void) VOP_BWRITE(bp); /* Ifile */
    547 		LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    548 		LFS_IENTRY(ifp, fs, otail, bp);
    549 		ifp->if_nextfree = ino;
    550 		VOP_BWRITE(bp);
    551 		LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    552 		/* printf("lfs_vfree: tailfree %d -> %d\n", otail, ino); */
    553 	}
    554 #ifdef DIAGNOSTIC
    555 	if (ino == LFS_UNUSED_INUM) {
    556 		panic("inode 0 freed");
    557 	}
    558 #endif /* DIAGNOSTIC */
    559 	if (old_iaddr != LFS_UNUSED_DADDR) {
    560 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    561 #ifdef DIAGNOSTIC
    562 		if (sup->su_nbytes < DINODE_SIZE) {
    563 			printf("lfs_vfree: negative byte count"
    564 			       " (segment %d short by %d)\n",
    565 			       dtosn(fs, old_iaddr),
    566 			       (int)DINODE_SIZE - sup->su_nbytes);
    567 			panic("lfs_vfree: negative byte count");
    568 			sup->su_nbytes = DINODE_SIZE;
    569 		}
    570 #endif
    571 		sup->su_nbytes -= DINODE_SIZE;
    572 		(void) VOP_BWRITE(bp); /* Ifile */
    573 	}
    574 
    575 	/* Set superblock modified bit and decrement file count. */
    576 	fs->lfs_fmod = 1;
    577 	--fs->lfs_nfiles;
    578 
    579 	if (fs->lfs_version == 1)
    580 		lfs_segunlock(fs);
    581 	else
    582 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    583 	return (0);
    584 }
    585