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