Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.92
      1 /*	$NetBSD: lfs_alloc.c,v 1.92 2006/05/04 04:22:55 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.92 2006/05/04 04:22:55 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 BMSHIFT 5	/* 2 ** 5 = 32 */
    105 #define BMMASK  ((1 << BMSHIFT) - 1)
    106 #define SET_BITMAP_FREE(F, I) do { \
    107 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
    108 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    109 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
    110 } while (0)
    111 #define CLR_BITMAP_FREE(F, I) do { \
    112 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    113 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    114 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    115 } while(0)
    116 
    117 #define ISSET_BITMAP_FREE(F, I) \
    118 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    119 
    120 /*
    121  * Allocate a particular inode with a particular version number, freeing
    122  * any previous versions of this inode that may have gone before.
    123  * Used by the roll-forward code.
    124  *
    125  * XXX this function does not have appropriate locking to be used on a live fs;
    126  * XXX but something similar could probably be used for an "undelete" call.
    127  *
    128  * Called with the Ifile inode locked.
    129  */
    130 int
    131 lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
    132 	      struct vnode **vpp)
    133 {
    134 	IFILE *ifp;
    135 	struct buf *bp, *cbp;
    136 	struct vnode *vp;
    137 	struct inode *ip;
    138 	ino_t tino, oldnext;
    139 	int error;
    140 	CLEANERINFO *cip;
    141 
    142 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    143 
    144 	/*
    145 	 * First, just try a vget. If the version number is the one we want,
    146 	 * we don't have to do anything else.  If the version number is wrong,
    147 	 * take appropriate action.
    148 	 */
    149 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    150 	if (error == 0) {
    151 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    152 
    153 		*vpp = vp;
    154 		ip = VTOI(vp);
    155 		if (ip->i_gen == vers)
    156 			return 0;
    157 		else if (ip->i_gen < vers) {
    158 			lfs_truncate(vp, (off_t)0, 0, NOCRED, l);
    159 			ip->i_gen = ip->i_ffs1_gen = vers;
    160 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    161 			return 0;
    162 		} else {
    163 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    164 			       ino, vers, ip->i_ffs1_gen));
    165 			vput(vp);
    166 			*vpp = NULLVP;
    167 			return EEXIST;
    168 		}
    169 	}
    170 
    171 	/*
    172 	 * The inode is not in use.  Find it on the free list.
    173 	 */
    174 	/* If the Ifile is too short to contain this inum, extend it */
    175 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    176 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    177 		<< fs->lfs_bshift) {
    178 		extend_ifile(fs, NOCRED);
    179 	}
    180 
    181 	LFS_IENTRY(ifp, fs, ino, bp);
    182 	oldnext = ifp->if_nextfree;
    183 	ifp->if_version = vers;
    184 	brelse(bp);
    185 
    186 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    187 	if (ino) {
    188 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    189 	} else {
    190 		tino = ino;
    191 		while (1) {
    192 			LFS_IENTRY(ifp, fs, tino, bp);
    193 			if (ifp->if_nextfree == ino ||
    194 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    195 				break;
    196 			tino = ifp->if_nextfree;
    197 			brelse(bp);
    198 		}
    199 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    200 			brelse(bp);
    201 			return ENOENT;
    202 		}
    203 		ifp->if_nextfree = oldnext;
    204 		LFS_BWRITE_LOG(bp);
    205 	}
    206 
    207 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
    208 	if (error == 0) {
    209 		/*
    210 		 * Make it VREG so we can put blocks on it.  We will change
    211 		 * this later if it turns out to be some other kind of file.
    212 		 */
    213 		ip = VTOI(vp);
    214 		ip->i_mode = ip->i_ffs1_mode = IFREG;
    215 		ip->i_nlink = ip->i_ffs1_nlink = 1;
    216 		ip->i_ffs_effnlink = 1;
    217 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    218 		ip = VTOI(vp);
    219 
    220 		DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
    221 
    222 		/* The dirop-nature of this vnode is past */
    223 		lfs_unmark_vnode(vp);
    224 		(void)lfs_vunref(vp);
    225 		vp->v_flag &= ~VDIROP;
    226 		simple_lock(&fs->lfs_interlock);
    227 		simple_lock(&lfs_subsys_lock);
    228 		--lfs_dirvcount;
    229 		simple_unlock(&lfs_subsys_lock);
    230 		--fs->lfs_dirvcount;
    231 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    232 		wakeup(&lfs_dirvcount);
    233 		wakeup(&fs->lfs_dirvcount);
    234 		simple_unlock(&fs->lfs_interlock);
    235 	}
    236 	*vpp = vp;
    237 	return error;
    238 }
    239 
    240 /*
    241  * Add a new block to the Ifile, to accommodate future file creations.
    242  * Called with the segment lock held.
    243  */
    244 static int
    245 extend_ifile(struct lfs *fs, struct ucred *cred)
    246 {
    247 	struct vnode *vp;
    248 	struct inode *ip;
    249 	IFILE *ifp;
    250 	IFILE_V1 *ifp_v1;
    251 	struct buf *bp, *cbp;
    252 	int error;
    253 	daddr_t i, blkno, xmax;
    254 	ino_t oldlast, maxino;
    255 	CLEANERINFO *cip;
    256 
    257 	ASSERT_SEGLOCK(fs);
    258 
    259 	vp = fs->lfs_ivnode;
    260 	ip = VTOI(vp);
    261 	blkno = lblkno(fs, ip->i_size);
    262 	if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    263 				&bp)) != 0) {
    264 		return (error);
    265 	}
    266 	ip->i_size += fs->lfs_bsize;
    267 	ip->i_ffs1_size = ip->i_size;
    268 	uvm_vnp_setsize(vp, ip->i_size);
    269 
    270 	maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
    271 		  fs->lfs_segtabsz) * fs->lfs_ifpb;
    272 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    273 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    274 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    275 	KASSERT(fs->lfs_ino_bitmap != NULL);
    276 
    277 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    278 		fs->lfs_ifpb;
    279 
    280 	/*
    281 	 * We insert the new inodes at the head of the free list.
    282 	 * Under normal circumstances, the free list is empty here,
    283 	 * so we are also incidentally placing them at the end (which
    284 	 * we must do if we are to keep them in order).
    285 	 */
    286 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    287 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    288 #ifdef DIAGNOSTIC
    289 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    290 		panic("inode 0 allocated [2]");
    291 #endif /* DIAGNOSTIC */
    292 	xmax = i + fs->lfs_ifpb;
    293 
    294 	if (fs->lfs_version == 1) {
    295 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    296 			SET_BITMAP_FREE(fs, i);
    297 			ifp_v1->if_version = 1;
    298 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    299 			ifp_v1->if_nextfree = ++i;
    300 		}
    301 		ifp_v1--;
    302 		ifp_v1->if_nextfree = oldlast;
    303 	} else {
    304 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    305 			SET_BITMAP_FREE(fs, i);
    306 			ifp->if_version = 1;
    307 			ifp->if_daddr = LFS_UNUSED_DADDR;
    308 			ifp->if_nextfree = ++i;
    309 		}
    310 		ifp--;
    311 		ifp->if_nextfree = oldlast;
    312 	}
    313 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    314 
    315 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    316 
    317 	return 0;
    318 }
    319 
    320 /* Allocate a new inode. */
    321 /* ARGSUSED */
    322 /* VOP_BWRITE 2i times */
    323 int
    324 lfs_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
    325 {
    326 	struct lfs *fs;
    327 	struct buf *bp, *cbp;
    328 	struct ifile *ifp;
    329 	ino_t new_ino;
    330 	int error;
    331 	int new_gen;
    332 	CLEANERINFO *cip;
    333 
    334 	fs = VTOI(pvp)->i_lfs;
    335 	if (fs->lfs_ronly)
    336 		return EROFS;
    337 
    338 	ASSERT_NO_SEGLOCK(fs);
    339 
    340 	lfs_seglock(fs, SEGM_PROT);
    341 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    342 
    343 	/* Get the head of the freelist. */
    344 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    345 	KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
    346 
    347 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
    348 	     (long long)new_ino));
    349 
    350 	/*
    351 	 * Remove the inode from the free list and write the new start
    352 	 * of the free list into the superblock.
    353 	 */
    354 	CLR_BITMAP_FREE(fs, new_ino);
    355 	LFS_IENTRY(ifp, fs, new_ino, bp);
    356 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    357 		panic("lfs_valloc: inuse inode %llu on the free list",
    358 		    (unsigned long long)new_ino);
    359 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    360 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
    361 	     (long long)new_ino, (long long)ifp->if_nextfree));
    362 
    363 	new_gen = ifp->if_version; /* version was updated by vfree */
    364 	brelse(bp);
    365 
    366 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    367 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    368 		if ((error = extend_ifile(fs, cred)) != 0) {
    369 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    370 			VOP_UNLOCK(fs->lfs_ivnode, 0);
    371 			lfs_segunlock(fs);
    372 			return error;
    373 		}
    374 	}
    375 #ifdef DIAGNOSTIC
    376 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    377 		panic("inode 0 allocated [3]");
    378 #endif /* DIAGNOSTIC */
    379 
    380 	/* Set superblock modified bit and increment file count. */
    381 	simple_lock(&fs->lfs_interlock);
    382 	fs->lfs_fmod = 1;
    383 	simple_unlock(&fs->lfs_interlock);
    384 	++fs->lfs_nfiles;
    385 
    386 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    387 	lfs_segunlock(fs);
    388 
    389 	return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
    390 }
    391 
    392 /*
    393  * Finish allocating a new inode, given an inode and generation number.
    394  */
    395 static int
    396 lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    397 	   struct vnode **vpp)
    398 {
    399 	struct inode *ip;
    400 	struct vnode *vp;
    401 
    402 	ASSERT_NO_SEGLOCK(fs);
    403 
    404 	vp = *vpp;
    405 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    406 	/* Create an inode to associate with the vnode. */
    407 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    408 
    409 	ip = VTOI(vp);
    410 	LFS_SET_UINO(ip, IN_CHANGE);
    411 	/* on-disk structure has been zeroed out by lfs_vcreate */
    412 	ip->i_din.ffs1_din->di_inumber = new_ino;
    413 
    414 	/* Note no blocks yet */
    415 	ip->i_lfs_hiblk = -1;
    416 
    417 	/* Set a new generation number for this inode. */
    418 	if (new_gen) {
    419 		ip->i_gen = new_gen;
    420 		ip->i_ffs1_gen = new_gen;
    421 	}
    422 
    423 	/* Insert into the inode hash table. */
    424 	ufs_ihashins(ip);
    425 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    426 
    427 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
    428 	vp = *vpp;
    429 	ip = VTOI(vp);
    430 
    431 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    432 
    433 	uvm_vnp_setsize(vp, 0);
    434 	lfs_mark_vnode(vp);
    435 	genfs_node_init(vp, &lfs_genfsops);
    436 	VREF(ip->i_devvp);
    437 	return (0);
    438 }
    439 
    440 /* Create a new vnode/inode pair and initialize what fields we can. */
    441 void
    442 lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    443 {
    444 	struct inode *ip;
    445 	struct ufs1_dinode *dp;
    446 	struct ufsmount *ump;
    447 #ifdef QUOTA
    448 	int i;
    449 #endif
    450 
    451 	/* Get a pointer to the private mount structure. */
    452 	ump = VFSTOUFS(mp);
    453 
    454 	ASSERT_NO_SEGLOCK(ump->um_lfs);
    455 
    456 	/* Initialize the inode. */
    457 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    458 	memset(ip, 0, sizeof(*ip));
    459 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
    460 	memset(dp, 0, sizeof(*dp));
    461 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    462 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
    463 	vp->v_data = ip;
    464 	ip->i_din.ffs1_din = dp;
    465 	ip->i_ump = ump;
    466 	ip->i_vnode = vp;
    467 	ip->i_devvp = ump->um_devvp;
    468 	ip->i_dev = ump->um_dev;
    469 	ip->i_number = dp->di_inumber = ino;
    470 	ip->i_lfs = ump->um_lfs;
    471 	ip->i_lfs_effnblks = 0;
    472 	SPLAY_INIT(&ip->i_lfs_lbtree);
    473 	ip->i_lfs_nbtree = 0;
    474 	LIST_INIT(&ip->i_lfs_segdhd);
    475 #ifdef QUOTA
    476 	for (i = 0; i < MAXQUOTAS; i++)
    477 		ip->i_dquot[i] = NODQUOT;
    478 #endif
    479 #ifdef DEBUG
    480 	if (ino == LFS_IFILE_INUM)
    481 		vp->v_vnlock->lk_wmesg = "inlock";
    482 #endif
    483 }
    484 
    485 #if 0
    486 /*
    487  * Find the highest-numbered allocated inode.
    488  * This will be used to shrink the Ifile.
    489  */
    490 static inline ino_t
    491 lfs_last_alloc_ino(struct lfs *fs)
    492 {
    493 	ino_t ino, maxino;
    494 
    495 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    496 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    497 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    498 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    499 			break;
    500 	}
    501 	return ino;
    502 }
    503 #endif
    504 
    505 /*
    506  * Find the previous (next lowest numbered) free inode, if any.
    507  * If there is none, return LFS_UNUSED_INUM.
    508  */
    509 static inline ino_t
    510 lfs_freelist_prev(struct lfs *fs, ino_t ino)
    511 {
    512 	ino_t tino, bound, bb, freehdbb;
    513 
    514 	if (fs->lfs_freehd == LFS_UNUSED_INUM)	 /* No free inodes at all */
    515 		return LFS_UNUSED_INUM;
    516 
    517 	/* Search our own word first */
    518 	bound = ino & ~BMMASK;
    519 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    520 		if (ISSET_BITMAP_FREE(fs, tino))
    521 			return tino;
    522 	/* If there are no lower words to search, just return */
    523 	if (ino >> BMSHIFT == 0)
    524 		return LFS_UNUSED_INUM;
    525 
    526 	/*
    527 	 * Find a word with a free inode in it.  We have to be a bit
    528 	 * careful here since ino_t is unsigned.
    529 	 */
    530 	freehdbb = (fs->lfs_freehd >> BMSHIFT);
    531 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    532 		if (fs->lfs_ino_bitmap[bb])
    533 			break;
    534 	if (fs->lfs_ino_bitmap[bb] == 0)
    535 		return LFS_UNUSED_INUM;
    536 
    537 	/* Search the word we found */
    538 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    539 	     tino > LFS_UNUSED_INUM; tino--)
    540 		if (ISSET_BITMAP_FREE(fs, tino))
    541 			break;
    542 
    543 	if (tino <= LFS_IFILE_INUM)
    544 		tino = LFS_UNUSED_INUM;
    545 
    546 	return tino;
    547 }
    548 
    549 /* Free an inode. */
    550 /* ARGUSED */
    551 /* VOP_BWRITE 2i times */
    552 int
    553 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    554 {
    555 	SEGUSE *sup;
    556 	CLEANERINFO *cip;
    557 	struct buf *cbp, *bp;
    558 	struct ifile *ifp;
    559 	struct inode *ip;
    560 	struct lfs *fs;
    561 	daddr_t old_iaddr;
    562 	ino_t otail;
    563 	int s;
    564 
    565 	/* Get the inode number and file system. */
    566 	ip = VTOI(vp);
    567 	fs = ip->i_lfs;
    568 	ino = ip->i_number;
    569 
    570 	ASSERT_NO_SEGLOCK(fs);
    571 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    572 
    573 	/* Drain of pending writes */
    574 	simple_lock(&vp->v_interlock);
    575 	s = splbio();
    576 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    577 		ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
    578 	splx(s);
    579 	simple_unlock(&vp->v_interlock);
    580 
    581 	lfs_seglock(fs, SEGM_PROT);
    582 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    583 
    584 	lfs_unmark_vnode(vp);
    585 	if (vp->v_flag & VDIROP) {
    586 		vp->v_flag &= ~VDIROP;
    587 		simple_lock(&fs->lfs_interlock);
    588 		simple_lock(&lfs_subsys_lock);
    589 		--lfs_dirvcount;
    590 		simple_unlock(&lfs_subsys_lock);
    591 		--fs->lfs_dirvcount;
    592 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    593 		simple_unlock(&fs->lfs_interlock);
    594 		wakeup(&fs->lfs_dirvcount);
    595 		wakeup(&lfs_dirvcount);
    596 		lfs_vunref(vp);
    597 
    598 		/*
    599 		 * If this inode is not going to be written any more, any
    600 		 * segment accounting left over from its truncation needs
    601 		 * to occur at the end of the next dirops flush.  Attach
    602 		 * them to the fs-wide list for that purpose.
    603 		 */
    604 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    605 			struct segdelta *sd;
    606 
    607 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    608 				LIST_REMOVE(sd, list);
    609 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    610 			}
    611 		}
    612 	} else {
    613 		/*
    614 		 * If it's not a dirop, we can finalize right away.
    615 		 */
    616 		lfs_finalize_ino_seguse(fs, ip);
    617 	}
    618 
    619 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    620 	ip->i_flag &= ~IN_ALLMOD;
    621 
    622 	/*
    623 	 * Set the ifile's inode entry to unused, increment its version number
    624 	 * and link it onto the free chain.
    625 	 */
    626 	SET_BITMAP_FREE(fs, ino);
    627 	LFS_IENTRY(ifp, fs, ino, bp);
    628 	old_iaddr = ifp->if_daddr;
    629 	ifp->if_daddr = LFS_UNUSED_DADDR;
    630 	++ifp->if_version;
    631 	if (fs->lfs_version == 1) {
    632 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    633 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    634 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    635 	} else {
    636 		ino_t tino, onf;
    637 
    638 		ifp->if_nextfree = LFS_UNUSED_INUM;
    639 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    640 
    641 		tino = lfs_freelist_prev(fs, ino);
    642 		if (tino == LFS_UNUSED_INUM) {
    643 			/* Nothing free below us, put us on the head */
    644 			LFS_IENTRY(ifp, fs, ino, bp);
    645 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    646 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    647 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    648 			     (long long)ifp->if_nextfree, (long long)ino));
    649 			LFS_BWRITE_LOG(bp); /* Ifile */
    650 
    651 			/* If the list was empty, set tail too */
    652 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    653 			if (otail == LFS_UNUSED_INUM) {
    654 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    655 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    656 				      "-> %lld\n", (long long)otail,
    657 				      (long long)ino));
    658 			}
    659 		} else {
    660 			/*
    661 			 * Insert this inode into the list after tino.
    662 			 * We hold the segment lock so we don't have to
    663 			 * worry about blocks being written out of order.
    664 			 */
    665 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    666 			      " after %lld\n", ino, tino));
    667 
    668 			LFS_IENTRY(ifp, fs, tino, bp);
    669 			onf = ifp->if_nextfree;
    670 			ifp->if_nextfree = ino;
    671 			LFS_BWRITE_LOG(bp);	/* Ifile */
    672 
    673 			LFS_IENTRY(ifp, fs, ino, bp);
    674 			ifp->if_nextfree = onf;
    675 			LFS_BWRITE_LOG(bp);	/* Ifile */
    676 
    677 			/* If we're last, put us on the tail */
    678 			if (onf == LFS_UNUSED_INUM) {
    679 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    680 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    681 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    682 				      "-> %lld\n", (long long)otail,
    683 				      (long long)ino));
    684 			}
    685 		}
    686 	}
    687 #ifdef DIAGNOSTIC
    688 	if (ino == LFS_UNUSED_INUM) {
    689 		panic("inode 0 freed");
    690 	}
    691 #endif /* DIAGNOSTIC */
    692 	if (old_iaddr != LFS_UNUSED_DADDR) {
    693 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    694 #ifdef DIAGNOSTIC
    695 		if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
    696 			printf("lfs_vfree: negative byte count"
    697 			       " (segment %" PRIu32 " short by %d)\n",
    698 			       dtosn(fs, old_iaddr),
    699 			       (int)sizeof (struct ufs1_dinode) -
    700 				    sup->su_nbytes);
    701 			panic("lfs_vfree: negative byte count");
    702 			sup->su_nbytes = sizeof (struct ufs1_dinode);
    703 		}
    704 #endif
    705 		sup->su_nbytes -= sizeof (struct ufs1_dinode);
    706 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
    707 	}
    708 
    709 	/* Set superblock modified bit and decrement file count. */
    710 	simple_lock(&fs->lfs_interlock);
    711 	fs->lfs_fmod = 1;
    712 	simple_unlock(&fs->lfs_interlock);
    713 	--fs->lfs_nfiles;
    714 
    715 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    716 	lfs_segunlock(fs);
    717 
    718 	return (0);
    719 }
    720 
    721 /*
    722  * Sort the freelist and set up the free-inode bitmap.
    723  * To be called by lfs_mountfs().
    724  */
    725 void
    726 lfs_order_freelist(struct lfs *fs)
    727 {
    728 	CLEANERINFO *cip;
    729 	IFILE *ifp = NULL;
    730 	struct buf *bp;
    731 	ino_t ino, firstino, lastino, maxino;
    732 
    733 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    734 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    735 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    736 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    737 		       M_SEGMENT, M_WAITOK | M_ZERO);
    738 	KASSERT(fs->lfs_ino_bitmap != NULL);
    739 
    740 	firstino = lastino = LFS_UNUSED_INUM;
    741 	for (ino = 0; ino < maxino; ino++) {
    742 		if (ino % fs->lfs_ifpb == 0)
    743 			LFS_IENTRY(ifp, fs, ino, bp);
    744 		else
    745 			++ifp;
    746 
    747 		/* Don't put zero or ifile on the free list */
    748 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    749 			continue;
    750 
    751 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    752 			if (firstino == LFS_UNUSED_INUM)
    753 				firstino = ino;
    754 			else {
    755 				brelse(bp);
    756 
    757 				LFS_IENTRY(ifp, fs, lastino, bp);
    758 				ifp->if_nextfree = ino;
    759 				LFS_BWRITE_LOG(bp);
    760 
    761 				LFS_IENTRY(ifp, fs, ino, bp);
    762 			}
    763 			lastino = ino;
    764 
    765 			SET_BITMAP_FREE(fs, ino);
    766 		}
    767 
    768 		if ((ino + 1) % fs->lfs_ifpb == 0)
    769 			brelse(bp);
    770 	}
    771 
    772 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    773 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    774 }
    775