Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.121
      1 /*	$NetBSD: lfs_alloc.c,v 1.121 2015/07/16 08:31:45 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.121 2015/07/16 08:31:45 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_extern.h>
     90 #include <ufs/lfs/lfs_kernel.h>
     91 
     92 /* Constants for inode free bitmap */
     93 #define BMSHIFT 5	/* 2 ** 5 = 32 */
     94 #define BMMASK  ((1 << BMSHIFT) - 1)
     95 #define SET_BITMAP_FREE(F, I) do { \
     96 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
     97 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
     98 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
     99 } while (0)
    100 #define CLR_BITMAP_FREE(F, I) do { \
    101 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    102 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    103 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    104 } while(0)
    105 
    106 #define ISSET_BITMAP_FREE(F, I) \
    107 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    108 
    109 /*
    110  * Add a new block to the Ifile, to accommodate future file creations.
    111  * Called with the segment lock held.
    112  */
    113 int
    114 lfs_extend_ifile(struct lfs *fs, kauth_cred_t cred)
    115 {
    116 	struct vnode *vp;
    117 	struct inode *ip;
    118 	IFILE *ifp;
    119 	IFILE_V1 *ifp_v1;
    120 	struct buf *bp, *cbp;
    121 	int error;
    122 	daddr_t i, blkno, xmax;
    123 	ino_t oldlast, maxino;
    124 	CLEANERINFO *cip;
    125 
    126 	ASSERT_SEGLOCK(fs);
    127 
    128 	vp = fs->lfs_ivnode;
    129 	ip = VTOI(vp);
    130 	blkno = lfs_lblkno(fs, ip->i_size);
    131 	if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    132 				&bp)) != 0) {
    133 		return (error);
    134 	}
    135 	ip->i_size += fs->lfs_bsize;
    136 	ip->i_ffs1_size = ip->i_size;
    137 	uvm_vnp_setsize(vp, ip->i_size);
    138 
    139 	maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
    140 		  fs->lfs_segtabsz) * fs->lfs_ifpb;
    141 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    142 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    143 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    144 	KASSERT(fs->lfs_ino_bitmap != NULL);
    145 
    146 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    147 		fs->lfs_ifpb;
    148 
    149 	/*
    150 	 * We insert the new inodes at the head of the free list.
    151 	 * Under normal circumstances, the free list is empty here,
    152 	 * so we are also incidentally placing them at the end (which
    153 	 * we must do if we are to keep them in order).
    154 	 */
    155 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    156 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    157 #ifdef DIAGNOSTIC
    158 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    159 		panic("inode 0 allocated [2]");
    160 #endif /* DIAGNOSTIC */
    161 	xmax = i + fs->lfs_ifpb;
    162 
    163 	if (fs->lfs_version == 1) {
    164 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    165 			SET_BITMAP_FREE(fs, i);
    166 			ifp_v1->if_version = 1;
    167 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    168 			ifp_v1->if_nextfree = ++i;
    169 		}
    170 		ifp_v1--;
    171 		ifp_v1->if_nextfree = oldlast;
    172 	} else {
    173 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    174 			SET_BITMAP_FREE(fs, i);
    175 			ifp->if_version = 1;
    176 			ifp->if_daddr = LFS_UNUSED_DADDR;
    177 			ifp->if_nextfree = ++i;
    178 		}
    179 		ifp--;
    180 		ifp->if_nextfree = oldlast;
    181 	}
    182 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    183 
    184 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    185 
    186 	return 0;
    187 }
    188 
    189 /* Allocate a new inode. */
    190 /* ARGSUSED */
    191 /* VOP_BWRITE 2i times */
    192 int
    193 lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred,
    194     ino_t *ino, int *gen)
    195 {
    196 	struct lfs *fs;
    197 	struct buf *bp, *cbp;
    198 	struct ifile *ifp;
    199 	int error;
    200 	CLEANERINFO *cip;
    201 
    202 	fs = VTOI(pvp)->i_lfs;
    203 	if (fs->lfs_ronly)
    204 		return EROFS;
    205 
    206 	ASSERT_NO_SEGLOCK(fs);
    207 
    208 	lfs_seglock(fs, SEGM_PROT);
    209 
    210 	/* Get the head of the freelist. */
    211 	LFS_GET_HEADFREE(fs, cip, cbp, ino);
    212 	KASSERT(*ino != LFS_UNUSED_INUM && *ino != LFS_IFILE_INUM);
    213 
    214 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %" PRId64 "\n",
    215 	     *ino));
    216 
    217 	/*
    218 	 * Remove the inode from the free list and write the new start
    219 	 * of the free list into the superblock.
    220 	 */
    221 	CLR_BITMAP_FREE(fs, *ino);
    222 	LFS_IENTRY(ifp, fs, *ino, bp);
    223 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    224 		panic("lfs_valloc: inuse inode %" PRId64 " on the free list",
    225 		    *ino);
    226 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    227 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %" PRId64 " -> %u\n",
    228 	     *ino, ifp->if_nextfree));
    229 
    230 	*gen = ifp->if_version; /* version was updated by vfree */
    231 	brelse(bp, 0);
    232 
    233 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    234 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    235 		if ((error = lfs_extend_ifile(fs, cred)) != 0) {
    236 			LFS_PUT_HEADFREE(fs, cip, cbp, *ino);
    237 			lfs_segunlock(fs);
    238 			return error;
    239 		}
    240 	}
    241 #ifdef DIAGNOSTIC
    242 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    243 		panic("inode 0 allocated [3]");
    244 #endif /* DIAGNOSTIC */
    245 
    246 	/* Set superblock modified bit and increment file count. */
    247 	mutex_enter(&lfs_lock);
    248 	fs->lfs_fmod = 1;
    249 	mutex_exit(&lfs_lock);
    250 	++fs->lfs_nfiles;
    251 
    252 	lfs_segunlock(fs);
    253 
    254 	return 0;
    255 }
    256 
    257 /*
    258  * Allocate a new inode with given inode number and version.
    259  */
    260 int
    261 lfs_valloc_fixed(struct lfs *fs, ino_t ino, int vers)
    262 {
    263 	IFILE *ifp;
    264 	struct buf *bp, *cbp;
    265 	ino_t tino, oldnext;
    266 	CLEANERINFO *cip;
    267 
    268 	/* If the Ifile is too short to contain this inum, extend it */
    269 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    270 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    271 		<< fs->lfs_bshift) {
    272 		lfs_extend_ifile(fs, NOCRED);
    273 	}
    274 
    275 	LFS_IENTRY(ifp, fs, ino, bp);
    276 	oldnext = ifp->if_nextfree;
    277 	ifp->if_version = vers;
    278 	brelse(bp, 0);
    279 
    280 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    281 	if (ino) {
    282 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    283 	} else {
    284 		tino = ino;
    285 		while (1) {
    286 			LFS_IENTRY(ifp, fs, tino, bp);
    287 			if (ifp->if_nextfree == ino ||
    288 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    289 				break;
    290 			tino = ifp->if_nextfree;
    291 			brelse(bp, 0);
    292 		}
    293 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    294 			brelse(bp, 0);
    295 			return ENOENT;
    296 		}
    297 		ifp->if_nextfree = oldnext;
    298 		LFS_BWRITE_LOG(bp);
    299 	}
    300 
    301 	return 0;
    302 }
    303 
    304 #if 0
    305 /*
    306  * Find the highest-numbered allocated inode.
    307  * This will be used to shrink the Ifile.
    308  */
    309 static inline ino_t
    310 lfs_last_alloc_ino(struct lfs *fs)
    311 {
    312 	ino_t ino, maxino;
    313 
    314 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    315 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    316 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    317 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    318 			break;
    319 	}
    320 	return ino;
    321 }
    322 #endif
    323 
    324 /*
    325  * Find the previous (next lowest numbered) free inode, if any.
    326  * If there is none, return LFS_UNUSED_INUM.
    327  */
    328 static inline ino_t
    329 lfs_freelist_prev(struct lfs *fs, ino_t ino)
    330 {
    331 	ino_t tino, bound, bb, freehdbb;
    332 
    333 	if (fs->lfs_freehd == LFS_UNUSED_INUM)	 /* No free inodes at all */
    334 		return LFS_UNUSED_INUM;
    335 
    336 	/* Search our own word first */
    337 	bound = ino & ~BMMASK;
    338 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    339 		if (ISSET_BITMAP_FREE(fs, tino))
    340 			return tino;
    341 	/* If there are no lower words to search, just return */
    342 	if (ino >> BMSHIFT == 0)
    343 		return LFS_UNUSED_INUM;
    344 
    345 	/*
    346 	 * Find a word with a free inode in it.  We have to be a bit
    347 	 * careful here since ino_t is unsigned.
    348 	 */
    349 	freehdbb = (fs->lfs_freehd >> BMSHIFT);
    350 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    351 		if (fs->lfs_ino_bitmap[bb])
    352 			break;
    353 	if (fs->lfs_ino_bitmap[bb] == 0)
    354 		return LFS_UNUSED_INUM;
    355 
    356 	/* Search the word we found */
    357 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    358 	     tino > LFS_UNUSED_INUM; tino--)
    359 		if (ISSET_BITMAP_FREE(fs, tino))
    360 			break;
    361 
    362 	if (tino <= LFS_IFILE_INUM)
    363 		tino = LFS_UNUSED_INUM;
    364 
    365 	return tino;
    366 }
    367 
    368 /* Free an inode. */
    369 /* ARGUSED */
    370 /* VOP_BWRITE 2i times */
    371 int
    372 lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    373 {
    374 	SEGUSE *sup;
    375 	CLEANERINFO *cip;
    376 	struct buf *cbp, *bp;
    377 	struct ifile *ifp;
    378 	struct inode *ip;
    379 	struct lfs *fs;
    380 	daddr_t old_iaddr;
    381 	ino_t otail;
    382 
    383 	/* Get the inode number and file system. */
    384 	ip = VTOI(vp);
    385 	fs = ip->i_lfs;
    386 	ino = ip->i_number;
    387 
    388 	ASSERT_NO_SEGLOCK(fs);
    389 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    390 
    391 	/* Drain of pending writes */
    392 	mutex_enter(vp->v_interlock);
    393 	while (fs->lfs_version > 1 && WRITEINPROG(vp)) {
    394 		cv_wait(&vp->v_cv, vp->v_interlock);
    395 	}
    396 	mutex_exit(vp->v_interlock);
    397 
    398 	lfs_seglock(fs, SEGM_PROT);
    399 
    400 	lfs_unmark_vnode(vp);
    401 	mutex_enter(&lfs_lock);
    402 	if (vp->v_uflag & VU_DIROP) {
    403 		vp->v_uflag &= ~VU_DIROP;
    404 		--lfs_dirvcount;
    405 		--fs->lfs_dirvcount;
    406 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    407 		wakeup(&fs->lfs_dirvcount);
    408 		wakeup(&lfs_dirvcount);
    409 		mutex_exit(&lfs_lock);
    410 		vrele(vp);
    411 
    412 		/*
    413 		 * If this inode is not going to be written any more, any
    414 		 * segment accounting left over from its truncation needs
    415 		 * to occur at the end of the next dirops flush.  Attach
    416 		 * them to the fs-wide list for that purpose.
    417 		 */
    418 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    419 			struct segdelta *sd;
    420 
    421 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    422 				LIST_REMOVE(sd, list);
    423 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    424 			}
    425 		}
    426 	} else {
    427 		/*
    428 		 * If it's not a dirop, we can finalize right away.
    429 		 */
    430 		mutex_exit(&lfs_lock);
    431 		lfs_finalize_ino_seguse(fs, ip);
    432 	}
    433 
    434 	mutex_enter(&lfs_lock);
    435 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    436 	mutex_exit(&lfs_lock);
    437 	ip->i_flag &= ~IN_ALLMOD;
    438 	ip->i_lfs_iflags |= LFSI_DELETED;
    439 
    440 	/*
    441 	 * Set the ifile's inode entry to unused, increment its version number
    442 	 * and link it onto the free chain.
    443 	 */
    444 	SET_BITMAP_FREE(fs, ino);
    445 	LFS_IENTRY(ifp, fs, ino, bp);
    446 	old_iaddr = ifp->if_daddr;
    447 	ifp->if_daddr = LFS_UNUSED_DADDR;
    448 	++ifp->if_version;
    449 	if (fs->lfs_version == 1) {
    450 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    451 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    452 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    453 	} else {
    454 		ino_t tino, onf;
    455 
    456 		ifp->if_nextfree = LFS_UNUSED_INUM;
    457 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    458 
    459 		tino = lfs_freelist_prev(fs, ino);
    460 		if (tino == LFS_UNUSED_INUM) {
    461 			/* Nothing free below us, put us on the head */
    462 			LFS_IENTRY(ifp, fs, ino, bp);
    463 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    464 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    465 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    466 			     (long long)ifp->if_nextfree, (long long)ino));
    467 			LFS_BWRITE_LOG(bp); /* Ifile */
    468 
    469 			/* If the list was empty, set tail too */
    470 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    471 			if (otail == LFS_UNUSED_INUM) {
    472 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    473 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    474 				      "-> %lld\n", (long long)otail,
    475 				      (long long)ino));
    476 			}
    477 		} else {
    478 			/*
    479 			 * Insert this inode into the list after tino.
    480 			 * We hold the segment lock so we don't have to
    481 			 * worry about blocks being written out of order.
    482 			 */
    483 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    484 			      " after %lld\n", ino, tino));
    485 
    486 			LFS_IENTRY(ifp, fs, tino, bp);
    487 			onf = ifp->if_nextfree;
    488 			ifp->if_nextfree = ino;
    489 			LFS_BWRITE_LOG(bp);	/* Ifile */
    490 
    491 			LFS_IENTRY(ifp, fs, ino, bp);
    492 			ifp->if_nextfree = onf;
    493 			LFS_BWRITE_LOG(bp);	/* Ifile */
    494 
    495 			/* If we're last, put us on the tail */
    496 			if (onf == LFS_UNUSED_INUM) {
    497 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    498 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    499 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    500 				      "-> %lld\n", (long long)otail,
    501 				      (long long)ino));
    502 			}
    503 		}
    504 	}
    505 #ifdef DIAGNOSTIC
    506 	if (ino == LFS_UNUSED_INUM) {
    507 		panic("inode 0 freed");
    508 	}
    509 #endif /* DIAGNOSTIC */
    510 	if (old_iaddr != LFS_UNUSED_DADDR) {
    511 		LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp);
    512 #ifdef DIAGNOSTIC
    513 		if (sup->su_nbytes < sizeof (struct ulfs1_dinode)) {
    514 			printf("lfs_vfree: negative byte count"
    515 			       " (segment %" PRIu32 " short by %d)\n",
    516 			       lfs_dtosn(fs, old_iaddr),
    517 			       (int)sizeof (struct ulfs1_dinode) -
    518 				    sup->su_nbytes);
    519 			panic("lfs_vfree: negative byte count");
    520 			sup->su_nbytes = sizeof (struct ulfs1_dinode);
    521 		}
    522 #endif
    523 		sup->su_nbytes -= sizeof (struct ulfs1_dinode);
    524 		LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, old_iaddr), bp); /* Ifile */
    525 	}
    526 
    527 	/* Set superblock modified bit and decrement file count. */
    528 	mutex_enter(&lfs_lock);
    529 	fs->lfs_fmod = 1;
    530 	mutex_exit(&lfs_lock);
    531 	--fs->lfs_nfiles;
    532 
    533 	lfs_segunlock(fs);
    534 
    535 	return (0);
    536 }
    537 
    538 /*
    539  * Sort the freelist and set up the free-inode bitmap.
    540  * To be called by lfs_mountfs().
    541  */
    542 void
    543 lfs_order_freelist(struct lfs *fs)
    544 {
    545 	CLEANERINFO *cip;
    546 	IFILE *ifp = NULL;
    547 	struct buf *bp;
    548 	ino_t ino, firstino, lastino, maxino;
    549 #ifdef notyet
    550 	struct vnode *vp;
    551 #endif
    552 
    553 	ASSERT_NO_SEGLOCK(fs);
    554 	lfs_seglock(fs, SEGM_PROT);
    555 
    556 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    557 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    558 	fs->lfs_ino_bitmap =
    559 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    560 		       M_SEGMENT, M_WAITOK | M_ZERO);
    561 	KASSERT(fs->lfs_ino_bitmap != NULL);
    562 
    563 	firstino = lastino = LFS_UNUSED_INUM;
    564 	for (ino = 0; ino < maxino; ino++) {
    565 		if (ino % fs->lfs_ifpb == 0)
    566 			LFS_IENTRY(ifp, fs, ino, bp);
    567 		else
    568 			++ifp;
    569 
    570 		/* Don't put zero or ifile on the free list */
    571 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    572 			continue;
    573 
    574 #ifdef notyet
    575 		/* Address orphaned files */
    576 		if (ifp->if_nextfree == LFS_ORPHAN_NEXTFREE &&
    577 		    VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp) == 0) {
    578 			lfs_truncate(vp, 0, 0, NOCRED);
    579 			vput(vp);
    580 			LFS_SEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    581 			KASSERT(sup->su_nbytes >= DINODE1_SIZE);
    582 			sup->su_nbytes -= DINODE1_SIZE;
    583 			LFS_WRITESEGENTRY(sup, fs, lfs_dtosn(fs, ifp->if_daddr), bp);
    584 
    585 			/* Set up to fall through to next section */
    586 			ifp->if_daddr = LFS_UNUSED_DADDR;
    587 			LFS_BWRITE_LOG(bp);
    588 			LFS_IENTRY(ifp, fs, ino, bp);
    589 		}
    590 #endif
    591 
    592 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    593 			if (firstino == LFS_UNUSED_INUM)
    594 				firstino = ino;
    595 			else {
    596 				brelse(bp, 0);
    597 
    598 				LFS_IENTRY(ifp, fs, lastino, bp);
    599 				ifp->if_nextfree = ino;
    600 				LFS_BWRITE_LOG(bp);
    601 
    602 				LFS_IENTRY(ifp, fs, ino, bp);
    603 			}
    604 			lastino = ino;
    605 
    606 			SET_BITMAP_FREE(fs, ino);
    607 		}
    608 
    609 		if ((ino + 1) % fs->lfs_ifpb == 0)
    610 			brelse(bp, 0);
    611 	}
    612 
    613 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    614 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    615 
    616 	lfs_segunlock(fs);
    617 }
    618 
    619 void
    620 lfs_orphan(struct lfs *fs, ino_t ino)
    621 {
    622 	IFILE *ifp;
    623 	struct buf *bp;
    624 
    625 	LFS_IENTRY(ifp, fs, ino, bp);
    626 	ifp->if_nextfree = LFS_ORPHAN_NEXTFREE;
    627 	LFS_BWRITE_LOG(bp);
    628 }
    629