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