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