Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.87
      1 /*	$NetBSD: lfs_alloc.c,v 1.87 2006/04/08 00:16:56 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003 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. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.87 2006/04/08 00:16:56 perseant Exp $");
     71 
     72 #if defined(_KERNEL_OPT)
     73 #include "opt_quota.h"
     74 #endif
     75 
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/kernel.h>
     79 #include <sys/buf.h>
     80 #include <sys/lock.h>
     81 #include <sys/vnode.h>
     82 #include <sys/syslog.h>
     83 #include <sys/mount.h>
     84 #include <sys/malloc.h>
     85 #include <sys/pool.h>
     86 #include <sys/proc.h>
     87 #include <sys/tree.h>
     88 
     89 #include <ufs/ufs/quota.h>
     90 #include <ufs/ufs/inode.h>
     91 #include <ufs/ufs/ufsmount.h>
     92 #include <ufs/ufs/ufs_extern.h>
     93 
     94 #include <ufs/lfs/lfs.h>
     95 #include <ufs/lfs/lfs_extern.h>
     96 
     97 extern struct lock ufs_hashlock;
     98 
     99 static int extend_ifile(struct lfs *, struct ucred *);
    100 static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
    101     struct vnode **);
    102 
    103 /* Constants for inode free bitmap */
    104 #define BYTESHIFT 3	/* 8 = 2 ** 3 */
    105 #define BYTEMASK  0x7	/* 8 - 1 */
    106 #define SET_BITMAP_FREE(F, I) \
    107 	(F)->lfs_ino_bitmap[(I) >> BYTESHIFT] |= (1 << ((I) & BYTEMASK))
    108 #define CLR_BITMAP_FREE(F, I) \
    109 	(F)->lfs_ino_bitmap[(I) >> BYTESHIFT] &= ~(1 << ((I) & BYTEMASK))
    110 #define ISSET_BITMAP_FREE(F, I) \
    111 	((F)->lfs_ino_bitmap[(I) >> BYTESHIFT] & (1 << ((I) & BYTEMASK)))
    112 
    113 /*
    114  * Allocate a particular inode with a particular version number, freeing
    115  * any previous versions of this inode that may have gone before.
    116  * Used by the roll-forward code.
    117  *
    118  * XXX this function does not have appropriate locking to be used on a live fs;
    119  * XXX but something similar could probably be used for an "undelete" call.
    120  *
    121  * Called with the Ifile inode locked.
    122  */
    123 int
    124 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
    125 	      struct vnode **vpp)
    126 {
    127 	IFILE *ifp;
    128 	struct buf *bp, *cbp;
    129 	struct vnode *vp;
    130 	struct inode *ip;
    131 	ino_t tino, oldnext;
    132 	int error;
    133 	CLEANERINFO *cip;
    134 
    135 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    136 
    137 	/*
    138 	 * First, just try a vget. If the version number is the one we want,
    139 	 * we don't have to do anything else.  If the version number is wrong,
    140 	 * take appropriate action.
    141 	 */
    142 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    143 	if (error == 0) {
    144 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    145 
    146 		*vpp = vp;
    147 		ip = VTOI(vp);
    148 		if (ip->i_gen == vers)
    149 			return 0;
    150 		else if (ip->i_gen < vers) {
    151 			lfs_truncate(vp, (off_t)0, 0, NOCRED, l);
    152 			ip->i_gen = ip->i_ffs1_gen = vers;
    153 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    154 			return 0;
    155 		} else {
    156 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    157 			       ino, vers, ip->i_ffs1_gen));
    158 			vput(vp);
    159 			*vpp = NULLVP;
    160 			return EEXIST;
    161 		}
    162 	}
    163 
    164 	/*
    165 	 * The inode is not in use.  Find it on the free list.
    166 	 */
    167 	/* If the Ifile is too short to contain this inum, extend it */
    168 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    169 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    170 		<< fs->lfs_bshift) {
    171 		extend_ifile(fs, NOCRED);
    172 	}
    173 
    174 	LFS_IENTRY(ifp, fs, ino, bp);
    175 	oldnext = ifp->if_nextfree;
    176 	ifp->if_version = vers;
    177 	brelse(bp);
    178 
    179 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    180 	if (ino) {
    181 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    182 	} else {
    183 		tino = ino;
    184 		while (1) {
    185 			LFS_IENTRY(ifp, fs, tino, bp);
    186 			if (ifp->if_nextfree == ino ||
    187 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    188 				break;
    189 			tino = ifp->if_nextfree;
    190 			brelse(bp);
    191 		}
    192 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    193 			brelse(bp);
    194 			return ENOENT;
    195 		}
    196 		ifp->if_nextfree = oldnext;
    197 		LFS_BWRITE_LOG(bp);
    198 	}
    199 
    200 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
    201 	if (error == 0) {
    202 		/*
    203 		 * Make it VREG so we can put blocks on it.  We will change
    204 		 * this later if it turns out to be some other kind of file.
    205 		 */
    206 		ip = VTOI(vp);
    207 		ip->i_mode = ip->i_ffs1_mode = IFREG;
    208 		ip->i_nlink = ip->i_ffs1_nlink = 1;
    209 		ip->i_ffs_effnlink = 1;
    210 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    211 		ip = VTOI(vp);
    212 
    213 		DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
    214 
    215 		/* The dirop-nature of this vnode is past */
    216 		lfs_unmark_vnode(vp);
    217 		(void)lfs_vunref(vp);
    218 		vp->v_flag &= ~VDIROP;
    219 		simple_lock(&fs->lfs_interlock);
    220 		simple_lock(&lfs_subsys_lock);
    221 		--lfs_dirvcount;
    222 		simple_unlock(&lfs_subsys_lock);
    223 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    224 		simple_unlock(&fs->lfs_interlock);
    225 	}
    226 	*vpp = vp;
    227 	return error;
    228 }
    229 
    230 /*
    231  * Add a new block to the Ifile, to accommodate future file creations.
    232  * Called with the segment lock held.
    233  */
    234 static int
    235 extend_ifile(struct lfs *fs, struct ucred *cred)
    236 {
    237 	struct vnode *vp;
    238 	struct inode *ip;
    239 	IFILE *ifp;
    240 	IFILE_V1 *ifp_v1;
    241 	struct buf *bp, *cbp;
    242 	int error;
    243 	daddr_t i, blkno, xmax;
    244 	ino_t oldlast, maxino;
    245 	CLEANERINFO *cip;
    246 
    247 	ASSERT_SEGLOCK(fs);
    248 
    249 	vp = fs->lfs_ivnode;
    250 	ip = VTOI(vp);
    251 	blkno = lblkno(fs, ip->i_size);
    252 	if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    253 				&bp)) != 0) {
    254 		return (error);
    255 	}
    256 	ip->i_size += fs->lfs_bsize;
    257 	ip->i_ffs1_size = ip->i_size;
    258 	uvm_vnp_setsize(vp, ip->i_size);
    259 
    260 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    261 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    262 	fs->lfs_ino_bitmap = realloc(fs->lfs_ino_bitmap,
    263 				     (maxino + BYTEMASK) >> BYTESHIFT,
    264 				     M_SEGMENT, M_WAITOK);
    265 
    266 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    267 		fs->lfs_ifpb;
    268 
    269 	/*
    270 	 * We insert the new inodes at the head of the free list.
    271 	 * Under normal circumstances, the free list is empty here,
    272 	 * so we are also incidentally placing them at the end (which
    273 	 * we must do if we are to keep them in order).
    274 	 */
    275 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    276 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    277 #ifdef DIAGNOSTIC
    278 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    279 		panic("inode 0 allocated [2]");
    280 #endif /* DIAGNOSTIC */
    281 	xmax = i + fs->lfs_ifpb;
    282 
    283 	if (fs->lfs_version == 1) {
    284 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    285 			ifp_v1->if_version = 1;
    286 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    287 			ifp_v1->if_nextfree = ++i;
    288 			SET_BITMAP_FREE(fs, i);
    289 		}
    290 		ifp_v1--;
    291 		ifp_v1->if_nextfree = oldlast;
    292 	} else {
    293 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    294 			ifp->if_version = 1;
    295 			ifp->if_daddr = LFS_UNUSED_DADDR;
    296 			ifp->if_nextfree = ++i;
    297 			SET_BITMAP_FREE(fs, i);
    298 		}
    299 		ifp--;
    300 		ifp->if_nextfree = oldlast;
    301 	}
    302 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    303 
    304 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    305 
    306 	return 0;
    307 }
    308 
    309 /* Allocate a new inode. */
    310 /* ARGSUSED */
    311 /* VOP_BWRITE 2i times */
    312 int
    313 lfs_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
    314 {
    315 	struct lfs *fs;
    316 	struct buf *bp, *cbp;
    317 	struct ifile *ifp;
    318 	ino_t new_ino;
    319 	int error;
    320 	int new_gen;
    321 	CLEANERINFO *cip;
    322 
    323 	fs = VTOI(pvp)->i_lfs;
    324 	if (fs->lfs_ronly)
    325 		return EROFS;
    326 
    327 	ASSERT_NO_SEGLOCK(fs);
    328 
    329 	lfs_seglock(fs, SEGM_PROT);
    330 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    331 
    332 	/* Get the head of the freelist. */
    333 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    334 	KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
    335 
    336 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
    337 	     (long long)new_ino));
    338 
    339 	/*
    340 	 * Remove the inode from the free list and write the new start
    341 	 * of the free list into the superblock.
    342 	 */
    343 	CLR_BITMAP_FREE(fs, new_ino);
    344 	LFS_IENTRY(ifp, fs, new_ino, bp);
    345 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    346 		panic("lfs_valloc: inuse inode %llu on the free list",
    347 		    (unsigned long long)new_ino);
    348 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    349 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
    350 	     (long long)new_ino, (long long)ifp->if_nextfree));
    351 
    352 	new_gen = ifp->if_version; /* version was updated by vfree */
    353 	brelse(bp);
    354 
    355 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    356 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    357 		if ((error = extend_ifile(fs, cred)) != 0) {
    358 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    359 			VOP_UNLOCK(fs->lfs_ivnode, 0);
    360 			lfs_segunlock(fs);
    361 			return error;
    362 		}
    363 	}
    364 #ifdef DIAGNOSTIC
    365 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    366 		panic("inode 0 allocated [3]");
    367 #endif /* DIAGNOSTIC */
    368 
    369 	/* Set superblock modified bit and increment file count. */
    370 	simple_lock(&fs->lfs_interlock);
    371 	fs->lfs_fmod = 1;
    372 	simple_unlock(&fs->lfs_interlock);
    373 	++fs->lfs_nfiles;
    374 
    375 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    376 	lfs_segunlock(fs);
    377 
    378 	return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
    379 }
    380 
    381 /*
    382  * Finish allocating a new inode, given an inode and generation number.
    383  */
    384 static int
    385 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    386 	   struct vnode **vpp)
    387 {
    388 	struct inode *ip;
    389 	struct vnode *vp;
    390 
    391 	ASSERT_NO_SEGLOCK(fs);
    392 
    393 	vp = *vpp;
    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);
    400 	/* on-disk structure has been zeroed out by lfs_vcreate */
    401 	ip->i_din.ffs1_din->di_inumber = new_ino;
    402 
    403 	/* Note no blocks yet */
    404 	ip->i_lfs_hiblk = -1;
    405 
    406 	/* Set a new generation number for this inode. */
    407 	if (new_gen) {
    408 		ip->i_gen = new_gen;
    409 		ip->i_ffs1_gen = new_gen;
    410 	}
    411 
    412 	/* Insert into the inode hash table. */
    413 	ufs_ihashins(ip);
    414 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    415 
    416 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
    417 	vp = *vpp;
    418 	ip = VTOI(vp);
    419 
    420 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    421 
    422 	uvm_vnp_setsize(vp, 0);
    423 	lfs_mark_vnode(vp);
    424 	genfs_node_init(vp, &lfs_genfsops);
    425 	VREF(ip->i_devvp);
    426 	return (0);
    427 }
    428 
    429 /* Create a new vnode/inode pair and initialize what fields we can. */
    430 void
    431 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    432 {
    433 	struct inode *ip;
    434 	struct ufs1_dinode *dp;
    435 	struct ufsmount *ump;
    436 #ifdef QUOTA
    437 	int i;
    438 #endif
    439 
    440 	/* Get a pointer to the private mount structure. */
    441 	ump = VFSTOUFS(mp);
    442 
    443 	ASSERT_NO_SEGLOCK(ump->um_lfs);
    444 
    445 	/* Initialize the inode. */
    446 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    447 	memset(ip, 0, sizeof(*ip));
    448 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
    449 	memset(dp, 0, sizeof(*dp));
    450 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    451 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
    452 	vp->v_data = ip;
    453 	ip->i_din.ffs1_din = dp;
    454 	ip->i_ump = ump;
    455 	ip->i_vnode = vp;
    456 	ip->i_devvp = ump->um_devvp;
    457 	ip->i_dev = ump->um_dev;
    458 	ip->i_number = dp->di_inumber = ino;
    459 	ip->i_lfs = ump->um_lfs;
    460 	ip->i_lfs_effnblks = 0;
    461 	SPLAY_INIT(&ip->i_lfs_lbtree);
    462 	ip->i_lfs_nbtree = 0;
    463 #ifdef QUOTA
    464 	for (i = 0; i < MAXQUOTAS; i++)
    465 		ip->i_dquot[i] = NODQUOT;
    466 #endif
    467 #ifdef DEBUG
    468 	if (ino == LFS_IFILE_INUM)
    469 		vp->v_vnlock->lk_wmesg = "inlock";
    470 #endif
    471 }
    472 
    473 #if 0
    474 /*
    475  * Find the highest-numbered allocated inode.
    476  * This will be used to shrink the Ifile.
    477  */
    478 static inline ino_t
    479 lfs_last_alloc_ino(struct lfs *fs)
    480 {
    481 	ino_t ino, maxino;
    482 
    483 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    484 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    485 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    486 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    487 			break;
    488 	}
    489 	return ino;
    490 }
    491 #endif
    492 
    493 /*
    494  * Find the previous (next lowest numbered) free inode, if any.
    495  * If there is none, return LFS_UNUSED_INUM.
    496  */
    497 static inline ino_t
    498 lfs_freelist_prev(struct lfs *fs, ino_t ino)
    499 {
    500 	ino_t tino;
    501 	struct buf *bp;
    502 	IFILE *ifp;
    503 
    504 	for (tino = ino - 1; tino > LFS_UNUSED_INUM; tino--) {
    505 		if (fs->lfs_ino_bitmap) {
    506 			if (ISSET_BITMAP_FREE(fs, tino))
    507 				break;
    508 		} else {
    509 			LFS_IENTRY(ifp, fs, tino, bp);
    510 			if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    511 				brelse(bp);
    512 				break;
    513 			}
    514 			brelse(bp);
    515 		}
    516 	}
    517 	if (tino <= LFS_IFILE_INUM)
    518 		tino = LFS_UNUSED_INUM;
    519 
    520 	return tino;
    521 }
    522 
    523 /* Free an inode. */
    524 /* ARGUSED */
    525 /* VOP_BWRITE 2i times */
    526 int
    527 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    528 {
    529 	SEGUSE *sup;
    530 	CLEANERINFO *cip;
    531 	struct buf *cbp, *bp;
    532 	struct ifile *ifp;
    533 	struct inode *ip;
    534 	struct lfs *fs;
    535 	daddr_t old_iaddr;
    536 	ino_t otail;
    537 	int s;
    538 
    539 	/* Get the inode number and file system. */
    540 	ip = VTOI(vp);
    541 	fs = ip->i_lfs;
    542 	ino = ip->i_number;
    543 
    544 	ASSERT_NO_SEGLOCK(fs);
    545 
    546 	/* Drain of pending writes */
    547 	simple_lock(&vp->v_interlock);
    548 	s = splbio();
    549 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    550 		ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
    551 	splx(s);
    552 	simple_unlock(&vp->v_interlock);
    553 
    554 	lfs_seglock(fs, SEGM_PROT);
    555 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    556 
    557 	lfs_unmark_vnode(vp);
    558 	if (vp->v_flag & VDIROP) {
    559 		vp->v_flag &= ~VDIROP;
    560 		simple_lock(&fs->lfs_interlock);
    561 		simple_lock(&lfs_subsys_lock);
    562 		--lfs_dirvcount;
    563 		simple_unlock(&lfs_subsys_lock);
    564 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    565 		simple_unlock(&fs->lfs_interlock);
    566 		wakeup(&lfs_dirvcount);
    567 		lfs_vunref(vp);
    568 	}
    569 
    570 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    571 	ip->i_flag &= ~IN_ALLMOD;
    572 
    573 	/*
    574 	 * Set the ifile's inode entry to unused, increment its version number
    575 	 * and link it onto the free chain.
    576 	 */
    577 	SET_BITMAP_FREE(fs, ino);
    578 	LFS_IENTRY(ifp, fs, ino, bp);
    579 	old_iaddr = ifp->if_daddr;
    580 	ifp->if_daddr = LFS_UNUSED_DADDR;
    581 	++ifp->if_version;
    582 	if (fs->lfs_version == 1) {
    583 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    584 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    585 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    586 	} else {
    587 		ino_t tino, onf;
    588 
    589 		ifp->if_nextfree = LFS_UNUSED_INUM;
    590 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    591 
    592 		tino = lfs_freelist_prev(fs, ino);
    593 		if (tino == LFS_UNUSED_INUM) {
    594 			/* Nothing free below us, put us on the head */
    595 			LFS_IENTRY(ifp, fs, ino, bp);
    596 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    597 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    598 			DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
    599 			     (long long)ifp->if_nextfree, (long long)ino));
    600 			LFS_BWRITE_LOG(bp); /* Ifile */
    601 
    602 			/* If the list was empty, set tail too */
    603 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    604 			if (otail == LFS_UNUSED_INUM) {
    605 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    606 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    607 				      "-> %lld\n", (long long)otail,
    608 				      (long long)ino));
    609 			}
    610 		} else {
    611 			/*
    612 			 * Insert this inode into the list after tino.
    613 			 * We hold the segment lock so we don't have to
    614 			 * worry about blocks being written out of order.
    615 			 */
    616 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    617 			      " after %lld\n", ino, tino));
    618 
    619 			LFS_IENTRY(ifp, fs, tino, bp);
    620 			onf = ifp->if_nextfree;
    621 			ifp->if_nextfree = ino;
    622 			LFS_BWRITE_LOG(bp);	/* Ifile */
    623 
    624 			LFS_IENTRY(ifp, fs, ino, bp);
    625 			ifp->if_nextfree = onf;
    626 			LFS_BWRITE_LOG(bp);	/* Ifile */
    627 
    628 			/* If we're last, put us on the tail */
    629 			if (onf == LFS_UNUSED_INUM) {
    630 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    631 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    632 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    633 				      "-> %lld\n", (long long)otail,
    634 				      (long long)ino));
    635 			}
    636 		}
    637 	}
    638 #ifdef DIAGNOSTIC
    639 	if (ino == LFS_UNUSED_INUM) {
    640 		panic("inode 0 freed");
    641 	}
    642 #endif /* DIAGNOSTIC */
    643 	if (old_iaddr != LFS_UNUSED_DADDR) {
    644 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    645 #ifdef DIAGNOSTIC
    646 		if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
    647 			printf("lfs_vfree: negative byte count"
    648 			       " (segment %" PRIu32 " short by %d)\n",
    649 			       dtosn(fs, old_iaddr),
    650 			       (int)sizeof (struct ufs1_dinode) -
    651 				    sup->su_nbytes);
    652 			panic("lfs_vfree: negative byte count");
    653 			sup->su_nbytes = sizeof (struct ufs1_dinode);
    654 		}
    655 #endif
    656 		sup->su_nbytes -= sizeof (struct ufs1_dinode);
    657 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
    658 	}
    659 
    660 	/* Set superblock modified bit and decrement file count. */
    661 	simple_lock(&fs->lfs_interlock);
    662 	fs->lfs_fmod = 1;
    663 	simple_unlock(&fs->lfs_interlock);
    664 	--fs->lfs_nfiles;
    665 
    666 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    667 	lfs_segunlock(fs);
    668 
    669 	return (0);
    670 }
    671 
    672 /*
    673  * Sort the freelist and set up the free-inode bitmap.
    674  * To be called by lfs_mountfs().
    675  */
    676 void
    677 lfs_order_freelist(struct lfs *fs)
    678 {
    679 	CLEANERINFO *cip;
    680 	IFILE *ifp = NULL;
    681 	struct buf *bp;
    682 	ino_t ino, firstino, lastino, maxino;
    683 
    684 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    685 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    686 	fs->lfs_ino_bitmap = malloc((maxino + BYTEMASK) >> BYTESHIFT,
    687 				    M_SEGMENT, M_WAITOK | M_ZERO);
    688 
    689 	firstino = lastino = LFS_UNUSED_INUM;
    690 	for (ino = 0; ino < maxino; ino++) {
    691 		if (ino % fs->lfs_ifpb == 0)
    692 			LFS_IENTRY(ifp, fs, ino, bp);
    693 		else
    694 			++ifp;
    695 
    696 		/* Don't put zero or ifile on the free list */
    697 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    698 			continue;
    699 
    700 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    701 			if (firstino == LFS_UNUSED_INUM)
    702 				firstino = ino;
    703 			else {
    704 				brelse(bp);
    705 
    706 				LFS_IENTRY(ifp, fs, lastino, bp);
    707 				ifp->if_nextfree = ino;
    708 				LFS_BWRITE_LOG(bp);
    709 
    710 				LFS_IENTRY(ifp, fs, ino, bp);
    711 			}
    712 			lastino = ino;
    713 
    714 			SET_BITMAP_FREE(fs, ino);
    715 		}
    716 
    717 		if ((ino + 1) % fs->lfs_ifpb == 0)
    718 			brelse(bp);
    719 	}
    720 
    721 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    722 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    723 }
    724