Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.125
      1 /*	$NetBSD: lfs_alloc.c,v 1.125 2015/08/02 18:14:16 dholland Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2007 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Copyright (c) 1991, 1993
     33  *	The Regents of the University of California.  All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. Neither the name of the University nor the names of its contributors
     44  *    may be used to endorse or promote products derived from this software
     45  *    without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57  * SUCH DAMAGE.
     58  *
     59  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.125 2015/08/02 18:14:16 dholland Exp $");
     64 
     65 #if defined(_KERNEL_OPT)
     66 #include "opt_quota.h"
     67 #endif
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/buf.h>
     73 #include <sys/lock.h>
     74 #include <sys/vnode.h>
     75 #include <sys/syslog.h>
     76 #include <sys/mount.h>
     77 #include <sys/malloc.h>
     78 #include <sys/pool.h>
     79 #include <sys/proc.h>
     80 #include <sys/tree.h>
     81 #include <sys/kauth.h>
     82 
     83 #include <ufs/lfs/ulfs_quotacommon.h>
     84 #include <ufs/lfs/ulfs_inode.h>
     85 #include <ufs/lfs/ulfsmount.h>
     86 #include <ufs/lfs/ulfs_extern.h>
     87 
     88 #include <ufs/lfs/lfs.h>
     89 #include <ufs/lfs/lfs_accessors.h>
     90 #include <ufs/lfs/lfs_extern.h>
     91 #include <ufs/lfs/lfs_kernel.h>
     92 
     93 /* Constants for inode free bitmap */
     94 #define BMSHIFT 5	/* 2 ** 5 = 32 */
     95 #define BMMASK  ((1 << BMSHIFT) - 1)
     96 #define SET_BITMAP_FREE(F, I) do { \
     97 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
     98 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
     99 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
    100 } while (0)
    101 #define CLR_BITMAP_FREE(F, I) do { \
    102 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    103 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    104 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    105 } while(0)
    106 
    107 #define ISSET_BITMAP_FREE(F, I) \
    108 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    109 
    110 /*
    111  * Add a new block to the Ifile, to accommodate future file creations.
    112  * Called with the segment lock held.
    113  */
    114 int
    115 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
    116 {
    117 	struct vnode *vp;
    118 	struct inode *ip;
    119 	IFILE *ifp;
    120 	IFILE_V1 *ifp_v1;
    121 	struct buf *bp, *cbp;
    122 	int error;
    123 	daddr_t i, blkno, xmax;
    124 	ino_t oldlast, maxino;
    125 	CLEANERINFO *cip;
    126 
    127 	ASSERT_SEGLOCK(fs);
    128 
    129 	vp = fs->lfs_ivnode;
    130 	ip = VTOI(vp);
    131 	blkno = lfs_lblkno(fs, ip->i_size);
    132 	if ((error = lfs_balloc(vp, ip->i_size, lfs_sb_getbsize(fs), cred, 0,
    133 				&bp)) != 0) {
    134 		return (error);
    135 	}
    136 	ip->i_size += lfs_sb_getbsize(fs);
    137 	ip->i_ffs1_size = ip->i_size;
    138 	uvm_vnp_setsize(vp, ip->i_size);
    139 
    140 	maxino = ((ip->i_size >> lfs_sb_getbshift(fs)) - lfs_sb_getcleansz(fs) -
    141 		  lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    142 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    143 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    144 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    145 	KASSERT(fs->lfs_ino_bitmap != NULL);
    146 
    147 	i = (blkno - lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs)) *
    148 		lfs_sb_getifpb(fs);
    149 
    150 	/*
    151 	 * We insert the new inodes at the head of the free list.
    152 	 * Under normal circumstances, the free list is empty here,
    153 	 * so we are also incidentally placing them at the end (which
    154 	 * we must do if we are to keep them in order).
    155 	 */
    156 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    157 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    158 #ifdef DIAGNOSTIC
    159 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    160 		panic("inode 0 allocated [2]");
    161 #endif /* DIAGNOSTIC */
    162 	xmax = i + lfs_sb_getifpb(fs);
    163 
    164 	if (lfs_sb_getversion(fs) == 1) {
    165 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    166 			SET_BITMAP_FREE(fs, i);
    167 			ifp_v1->if_version = 1;
    168 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    169 			ifp_v1->if_nextfree = ++i;
    170 		}
    171 		ifp_v1--;
    172 		ifp_v1->if_nextfree = oldlast;
    173 	} else {
    174 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    175 			SET_BITMAP_FREE(fs, i);
    176 			ifp->if_version = 1;
    177 			ifp->if_daddr = LFS_UNUSED_DADDR;
    178 			ifp->if_nextfree = ++i;
    179 		}
    180 		ifp--;
    181 		ifp->if_nextfree = oldlast;
    182 	}
    183 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    184 
    185 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    186 
    187 	return 0;
    188 }
    189 
    190 /* Allocate a new inode. */
    191 /* ARGSUSED */
    192 /* VOP_BWRITE 2i times */
    193 int
    194 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    195     ino_t *ino, int *gen)
    196 {
    197 	struct lfs *fs;
    198 	struct buf *bp, *cbp;
    199 	struct ifile *ifp;
    200 	int error;
    201 	CLEANERINFO *cip;
    202 
    203 	fs = VTOI(pvp)->i_lfs;
    204 	if (fs->lfs_ronly)
    205 		return EROFS;
    206 
    207 	ASSERT_NO_SEGLOCK(fs);
    208 
    209 	lfs_seglock(fs, SEGM_PROT);
    210 
    211 	/* Get the head of the freelist. */
    212 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    213 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    214 
    215 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    216 	     *ino));
    217 
    218 	/*
    219 	 * Remove the inode from the free list and write the new start
    220 	 * of the free list into the superblock.
    221 	 */
    222 	CLR_BITMAP_FREE(fs, *ino);
    223 	LFS_IENTRY(ifp, fs, *ino, bp);
    224 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    225 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    226 		    *ino);
    227 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    228 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %u\n",
    229 	     *ino, ifp->if_nextfree));
    230 
    231 	*gen = ifp->if_version; /* version was updated by vfree */
    232 	brelse(bp, 0);
    233 
    234 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    235 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM) {
    236 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    237 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
    238 			lfs_segunlock(fs);
    239 			return error;
    240 		}
    241 	}
    242 #ifdef DIAGNOSTIC
    243 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)
    244 		panic("inode 0 allocated [3]");
    245 #endif /* DIAGNOSTIC */
    246 
    247 	/* Set superblock modified bit and increment file count. */
    248 	mutex_enter(&lfs_lock);
    249 	fs->lfs_fmod = 1;
    250 	mutex_exit(&lfs_lock);
    251 	lfs_sb_addnfiles(fs, 1);
    252 
    253 	lfs_segunlock(fs);
    254 
    255 	return 0;
    256 }
    257 
    258 /*
    259  * Allocate a new inode with given inode number and version.
    260  */
    261 int
    262 lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    263 {
    264 	IFILE *ifp;
    265 	struct buf *bp, *cbp;
    266 	ino_t tino, oldnext;
    267 	CLEANERINFO *cip;
    268 
    269 	/* If the Ifile is too short to contain this inum, extend it */
    270 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    271 		lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    272 		<< lfs_sb_getbshift(fs)) {
    273 		lfs_extend_ifile(fs, NOCRED);
    274 	}
    275 
    276 	LFS_IENTRY(ifp, fs, ino, bp);
    277 	oldnext = ifp->if_nextfree;
    278 	ifp->if_version = vers;
    279 	brelse(bp, 0);
    280 
    281 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    282 	if (ino) {
    283 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    284 	} else {
    285 		tino = ino;
    286 		while (1) {
    287 			LFS_IENTRY(ifp, fs, tino, bp);
    288 			if (ifp->if_nextfree == ino ||
    289 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    290 				break;
    291 			tino = ifp->if_nextfree;
    292 			brelse(bp, 0);
    293 		}
    294 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    295 			brelse(bp, 0);
    296 			return ENOENT;
    297 		}
    298 		ifp->if_nextfree = oldnext;
    299 		LFS_BWRITE_LOG(bp);
    300 	}
    301 
    302 	return 0;
    303 }
    304 
    305 #if 0
    306 /*
    307  * Find the highest-numbered allocated inode.
    308  * This will be used to shrink the Ifile.
    309  */
    310 static inline ino_t
    311 lfs_last_alloc_ino(struct lfs *fs)
    312 {
    313 	ino_t ino, maxino;
    314 
    315 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    316 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * fs->lfs_ifpb;
    317 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    318 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    319 			break;
    320 	}
    321 	return ino;
    322 }
    323 #endif
    324 
    325 /*
    326  * Find the previous (next lowest numbered) free inode, if any.
    327  * If there is none, return LFS_UNUSED_INUM.
    328  */
    329 static inline ino_t
    330 lfs_freelist_prev(struct lfs *fs, ino_t ino)
    331 {
    332 	ino_t tino, bound, bb, freehdbb;
    333 
    334 	if (lfs_sb_getfreehd(fs) == LFS_UNUSED_INUM)	 /* No free inodes at all */
    335 		return LFS_UNUSED_INUM;
    336 
    337 	/* Search our own word first */
    338 	bound = ino & ~BMMASK;
    339 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    340 		if (ISSET_BITMAP_FREE(fs, tino))
    341 			return tino;
    342 	/* If there are no lower words to search, just return */
    343 	if (ino >> BMSHIFT == 0)
    344 		return LFS_UNUSED_INUM;
    345 
    346 	/*
    347 	 * Find a word with a free inode in it.  We have to be a bit
    348 	 * careful here since ino_t is unsigned.
    349 	 */
    350 	freehdbb = (lfs_sb_getfreehd(fs) >> BMSHIFT);
    351 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    352 		if (fs->lfs_ino_bitmap[bb])
    353 			break;
    354 	if (fs->lfs_ino_bitmap[bb] == 0)
    355 		return LFS_UNUSED_INUM;
    356 
    357 	/* Search the word we found */
    358 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    359 	     tino > LFS_UNUSED_INUM; tino--)
    360 		if (ISSET_BITMAP_FREE(fs, tino))
    361 			break;
    362 
    363 	if (tino <= LFS_IFILE_INUM)
    364 		tino = LFS_UNUSED_INUM;
    365 
    366 	return tino;
    367 }
    368 
    369 /* Free an inode. */
    370 /* ARGUSED */
    371 /* VOP_BWRITE 2i times */
    372 int
    373 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    374 {
    375 	SEGUSE *sup;
    376 	CLEANERINFO *cip;
    377 	struct buf *cbp, *bp;
    378 	struct ifile *ifp;
    379 	struct inode *ip;
    380 	struct lfs *fs;
    381 	daddr_t old_iaddr;
    382 	ino_t otail;
    383 
    384 	/* Get the inode number and file system. */
    385 	ip = VTOI(vp);
    386 	fs = ip->i_lfs;
    387 	ino = ip->i_number;
    388 
    389 	ASSERT_NO_SEGLOCK(fs);
    390 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    391 
    392 	/* Drain of pending writes */
    393 	mutex_enter(vp->v_interlock);
    394 	while (lfs_sb_getversion(fs) > 1 && WRITEINPROG(vp)) {
    395 		cv_wait(&vp->v_cv, vp->v_interlock);
    396 	}
    397 	mutex_exit(vp->v_interlock);
    398 
    399 	lfs_seglock(fs, SEGM_PROT);
    400 
    401 	lfs_unmark_vnode(vp);
    402 	mutex_enter(&lfs_lock);
    403 	if (vp->v_uflag & VU_DIROP) {
    404 		vp->v_uflag &= ~VU_DIROP;
    405 		--lfs_dirvcount;
    406 		--fs->lfs_dirvcount;
    407 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    408 		wakeup(&fs->lfs_dirvcount);
    409 		wakeup(&lfs_dirvcount);
    410 		mutex_exit(&lfs_lock);
    411 		vrele(vp);
    412 
    413 		/*
    414 		 * If this inode is not going to be written any more, any
    415 		 * segment accounting left over from its truncation needs
    416 		 * to occur at the end of the next dirops flush.  Attach
    417 		 * them to the fs-wide list for that purpose.
    418 		 */
    419 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    420 			struct segdelta *sd;
    421 
    422 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    423 				LIST_REMOVE(sd, list);
    424 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    425 			}
    426 		}
    427 	} else {
    428 		/*
    429 		 * If it's not a dirop, we can finalize right away.
    430 		 */
    431 		mutex_exit(&lfs_lock);
    432 		lfs_finalize_ino_seguse(fs, ip);
    433 	}
    434 
    435 	mutex_enter(&lfs_lock);
    436 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    437 	mutex_exit(&lfs_lock);
    438 	ip->i_flag &= ~IN_ALLMOD;
    439 	ip->i_lfs_iflags |= LFSI_DELETED;
    440 
    441 	/*
    442 	 * Set the ifile's inode entry to unused, increment its version number
    443 	 * and link it onto the free chain.
    444 	 */
    445 	SET_BITMAP_FREE(fs, ino);
    446 	LFS_IENTRY(ifp, fs, ino, bp);
    447 	old_iaddr = ifp->if_daddr;
    448 	ifp->if_daddr = LFS_UNUSED_DADDR;
    449 	++ifp->if_version;
    450 	if (lfs_sb_getversion(fs) == 1) {
    451 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    452 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    453 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    454 	} else {
    455 		ino_t tino, onf;
    456 
    457 		ifp->if_nextfree = LFS_UNUSED_INUM;
    458 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    459 
    460 		tino = lfs_freelist_prev(fs, ino);
    461 		if (tino == LFS_UNUSED_INUM) {
    462 			/* Nothing free below us, put us on the head */
    463 			LFS_IENTRY(ifp, fs, ino, bp);
    464 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    465 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    466 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    467 			     (long long)ifp->if_nextfree, (long long)ino));
    468 			LFS_BWRITE_LOG(bp); /* Ifile */
    469 
    470 			/* If the list was empty, set tail too */
    471 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    472 			if (otail == LFS_UNUSED_INUM) {
    473 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    474 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    475 				      "-> %lld\n", (long long)otail,
    476 				      (long long)ino));
    477 			}
    478 		} else {
    479 			/*
    480 			 * Insert this inode into the list after tino.
    481 			 * We hold the segment lock so we don't have to
    482 			 * worry about blocks being written out of order.
    483 			 */
    484 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    485 			      " after %lld\n", ino, tino));
    486 
    487 			LFS_IENTRY(ifp, fs, tino, bp);
    488 			onf = ifp->if_nextfree;
    489 			ifp->if_nextfree = ino;
    490 			LFS_BWRITE_LOG(bp);	/* Ifile */
    491 
    492 			LFS_IENTRY(ifp, fs, ino, bp);
    493 			ifp->if_nextfree = onf;
    494 			LFS_BWRITE_LOG(bp);	/* Ifile */
    495 
    496 			/* If we're last, put us on the tail */
    497 			if (onf == LFS_UNUSED_INUM) {
    498 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    499 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    500 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    501 				      "-> %lld\n", (long long)otail,
    502 				      (long long)ino));
    503 			}
    504 		}
    505 	}
    506 #ifdef DIAGNOSTIC
    507 	if (ino == LFS_UNUSED_INUM) {
    508 		panic("inode 0 freed");
    509 	}
    510 #endif /* DIAGNOSTIC */
    511 	if (old_iaddr != LFS_UNUSED_DADDR) {
    512 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    513 #ifdef DIAGNOSTIC
    514 		if (sup->su_nbytes < sizeof (struct ulfs1_dinode)) {
    515 			printf("lfs_vfree: negative byte count"
    516 			       " (segment %" PRIu32 " short by %d)\n",
    517 			       lfs_dtosn(fs, old_iaddr),
    518 			       (int)sizeof (struct ulfs1_dinode) -
    519 				    sup->su_nbytes);
    520 			panic("lfs_vfree: negative byte count");
    521 			sup->su_nbytes = sizeof (struct ulfs1_dinode);
    522 		}
    523 #endif
    524 		sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    525 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    526 	}
    527 
    528 	/* Set superblock modified bit and decrement file count. */
    529 	mutex_enter(&lfs_lock);
    530 	fs->lfs_fmod = 1;
    531 	mutex_exit(&lfs_lock);
    532 	lfs_sb_subnfiles(fs, 1);
    533 
    534 	lfs_segunlock(fs);
    535 
    536 	return (0);
    537 }
    538 
    539 /*
    540  * Sort the freelist and set up the free-inode bitmap.
    541  * To be called by lfs_mountfs().
    542  */
    543 void
    544 lfs_order_freelist(struct lfs *fs)
    545 {
    546 	CLEANERINFO *cip;
    547 	IFILE *ifp = NULL;
    548 	struct buf *bp;
    549 	ino_t ino, firstino, lastino, maxino;
    550 #ifdef notyet
    551 	struct vnode *vp;
    552 #endif
    553 
    554 	ASSERT_NO_SEGLOCK(fs);
    555 	lfs_seglock(fs, SEGM_PROT);
    556 
    557 	maxino = ((fs->lfs_ivnode->v_size >> lfs_sb_getbshift(fs)) -
    558 		  lfs_sb_getcleansz(fs) - lfs_sb_getsegtabsz(fs)) * lfs_sb_getifpb(fs);
    559 	fs->lfs_ino_bitmap =
    560 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    561 		       M_SEGMENT, M_WAITOK | M_ZERO);
    562 	KASSERT(fs->lfs_ino_bitmap != NULL);
    563 
    564 	firstino = lastino = LFS_UNUSED_INUM;
    565 	for (ino = 0; ino < maxino; ino++) {
    566 		if (ino % lfs_sb_getifpb(fs) == 0)
    567 			LFS_IENTRY(ifp, fs, ino, bp);
    568 		else
    569 			++ifp;
    570 
    571 		/* Don't put zero or ifile on the free list */
    572 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    573 			continue;
    574 
    575 #ifdef notyet
    576 		/* Address orphaned files */
    577 		if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
    578 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    579 			lfs_truncate(vp, 0, 0, NOCRED);
    580 			vput(vp);
    581 			LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    582 			KASSERT(sup->su_nbytes >= DINODE1_SIZE);
    583 			sup->su_nbytes -= DINODE1_SIZE;
    584 			LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    585 
    586 			/* Set up to fall through to next section */
    587 			ifp->if_daddr = LFS_UNUSED_DADDR;
    588 			LFS_BWRITE_LOG(bp);
    589 			LFS_IENTRY(ifp, fs, ino, bp);
    590 		}
    591 #endif
    592 
    593 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    594 			if (firstino == LFS_UNUSED_INUM)
    595 				firstino = ino;
    596 			else {
    597 				brelse(bp, 0);
    598 
    599 				LFS_IENTRY(ifp, fs, lastino, bp);
    600 				ifp->if_nextfree = ino;
    601 				LFS_BWRITE_LOG(bp);
    602 
    603 				LFS_IENTRY(ifp, fs, ino, bp);
    604 			}
    605 			lastino = ino;
    606 
    607 			SET_BITMAP_FREE(fs, ino);
    608 		}
    609 
    610 		if ((ino + 1) % lfs_sb_getifpb(fs) == 0)
    611 			brelse(bp, 0);
    612 	}
    613 
    614 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    615 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    616 
    617 	lfs_segunlock(fs);
    618 }
    619 
    620 void
    621 lfs_orphan(struct lfs *fs, ino_t ino)
    622 {
    623 	IFILE *ifp;
    624 	struct buf *bp;
    625 
    626 	LFS_IENTRY(ifp, fs, ino, bp);
    627 	ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
    628 	LFS_BWRITE_LOG(bp);
    629 }
    630