Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.76.2.11
      1  1.76.2.11       riz /*	$NetBSD: lfs_alloc.c,v 1.76.2.11 2006/05/20 22:43:42 riz Exp $	*/
      2        1.2       cgd 
      3       1.17  perseant /*-
      4       1.63  perseant  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5       1.17  perseant  * All rights reserved.
      6       1.17  perseant  *
      7       1.17  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8       1.17  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9       1.17  perseant  *
     10       1.17  perseant  * Redistribution and use in source and binary forms, with or without
     11       1.17  perseant  * modification, are permitted provided that the following conditions
     12       1.17  perseant  * are met:
     13       1.17  perseant  * 1. Redistributions of source code must retain the above copyright
     14       1.17  perseant  *    notice, this list of conditions and the following disclaimer.
     15       1.17  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.17  perseant  *    notice, this list of conditions and the following disclaimer in the
     17       1.17  perseant  *    documentation and/or other materials provided with the distribution.
     18       1.17  perseant  * 3. All advertising materials mentioning features or use of this software
     19       1.17  perseant  *    must display the following acknowledgement:
     20       1.64  perseant  *	This product includes software developed by the NetBSD
     21       1.64  perseant  *	Foundation, Inc. and its contributors.
     22       1.17  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.17  perseant  *    contributors may be used to endorse or promote products derived
     24       1.17  perseant  *    from this software without specific prior written permission.
     25       1.17  perseant  *
     26       1.17  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.17  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.17  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.17  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.17  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.17  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.17  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.17  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.17  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.17  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.17  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37       1.17  perseant  */
     38        1.1   mycroft /*
     39        1.1   mycroft  * Copyright (c) 1991, 1993
     40        1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41        1.1   mycroft  *
     42        1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43        1.1   mycroft  * modification, are permitted provided that the following conditions
     44        1.1   mycroft  * are met:
     45        1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46        1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47        1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48        1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49        1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50       1.71       agc  * 3. Neither the name of the University nor the names of its contributors
     51        1.1   mycroft  *    may be used to endorse or promote products derived from this software
     52        1.1   mycroft  *    without specific prior written permission.
     53        1.1   mycroft  *
     54        1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55        1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56        1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57        1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58        1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59        1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60        1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61        1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62        1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63        1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64        1.1   mycroft  * SUCH DAMAGE.
     65        1.1   mycroft  *
     66        1.2       cgd  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     67        1.1   mycroft  */
     68       1.52     lukem 
     69       1.52     lukem #include <sys/cdefs.h>
     70  1.76.2.11       riz __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.76.2.11 2006/05/20 22:43:42 riz Exp $");
     71       1.12    scottr 
     72       1.47       mrg #if defined(_KERNEL_OPT)
     73       1.12    scottr #include "opt_quota.h"
     74       1.13    scottr #endif
     75        1.1   mycroft 
     76        1.1   mycroft #include <sys/param.h>
     77        1.3  christos #include <sys/systm.h>
     78        1.1   mycroft #include <sys/kernel.h>
     79        1.1   mycroft #include <sys/buf.h>
     80       1.56  perseant #include <sys/lock.h>
     81        1.1   mycroft #include <sys/vnode.h>
     82        1.1   mycroft #include <sys/syslog.h>
     83        1.1   mycroft #include <sys/mount.h>
     84   1.76.2.5       riz #include <sys/malloc.h>
     85       1.15   thorpej #include <sys/pool.h>
     86       1.50       chs #include <sys/proc.h>
     87   1.76.2.2      tron #include <sys/tree.h>
     88        1.1   mycroft 
     89        1.1   mycroft #include <ufs/ufs/quota.h>
     90        1.1   mycroft #include <ufs/ufs/inode.h>
     91        1.1   mycroft #include <ufs/ufs/ufsmount.h>
     92        1.3  christos #include <ufs/ufs/ufs_extern.h>
     93        1.1   mycroft 
     94        1.1   mycroft #include <ufs/lfs/lfs.h>
     95        1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     96        1.1   mycroft 
     97       1.17  perseant extern struct lock ufs_hashlock;
     98       1.17  perseant 
     99       1.44  perseant static int extend_ifile(struct lfs *, struct ucred *);
    100       1.70      yamt static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
    101       1.70      yamt     struct vnode **);
    102       1.44  perseant 
    103   1.76.2.5       riz /* Constants for inode free bitmap */
    104   1.76.2.6       riz #define BMSHIFT 5	/* 2 ** 5 = 32 */
    105   1.76.2.6       riz #define BMMASK  ((1 << BMSHIFT) - 1)
    106   1.76.2.6       riz #define SET_BITMAP_FREE(F, I) do { \
    107   1.76.2.6       riz 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d set\n", (int)(I), 	\
    108   1.76.2.6       riz 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    109   1.76.2.6       riz 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] |= (1 << ((I) & BMMASK));	\
    110   1.76.2.6       riz } while (0)
    111   1.76.2.6       riz #define CLR_BITMAP_FREE(F, I) do { \
    112   1.76.2.6       riz 	DLOG((DLOG_ALLOC, "lfs: ino %d wrd %d bit %d clr\n", (int)(I), 	\
    113   1.76.2.6       riz 	     (int)((I) >> BMSHIFT), (int)((I) & BMMASK)));		\
    114   1.76.2.6       riz 	(F)->lfs_ino_bitmap[(I) >> BMSHIFT] &= ~(1 << ((I) & BMMASK));	\
    115   1.76.2.6       riz } while(0)
    116   1.76.2.6       riz 
    117   1.76.2.5       riz #define ISSET_BITMAP_FREE(F, I) \
    118   1.76.2.6       riz 	((F)->lfs_ino_bitmap[(I) >> BMSHIFT] & (1 << ((I) & BMMASK)))
    119   1.76.2.5       riz 
    120       1.44  perseant /*
    121       1.44  perseant  * Allocate a particular inode with a particular version number, freeing
    122       1.44  perseant  * any previous versions of this inode that may have gone before.
    123       1.44  perseant  * Used by the roll-forward code.
    124       1.44  perseant  *
    125       1.44  perseant  * XXX this function does not have appropriate locking to be used on a live fs;
    126       1.44  perseant  * XXX but something similar could probably be used for an "undelete" call.
    127       1.56  perseant  *
    128       1.56  perseant  * Called with the Ifile inode locked.
    129       1.44  perseant  */
    130       1.44  perseant int
    131   1.76.2.3       riz lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct proc *p,
    132       1.44  perseant 	      struct vnode **vpp)
    133       1.44  perseant {
    134       1.44  perseant 	IFILE *ifp;
    135       1.48  perseant 	struct buf *bp, *cbp;
    136       1.44  perseant 	struct vnode *vp;
    137       1.44  perseant 	struct inode *ip;
    138       1.44  perseant 	ino_t tino, oldnext;
    139       1.44  perseant 	int error;
    140       1.48  perseant 	CLEANERINFO *cip;
    141       1.44  perseant 
    142   1.76.2.2      tron 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    143   1.76.2.2      tron 
    144       1.44  perseant 	/*
    145       1.44  perseant 	 * First, just try a vget. If the version number is the one we want,
    146       1.44  perseant 	 * we don't have to do anything else.  If the version number is wrong,
    147       1.44  perseant 	 * take appropriate action.
    148       1.44  perseant 	 */
    149       1.68   thorpej 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    150       1.44  perseant 	if (error == 0) {
    151       1.76  perseant 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    152       1.44  perseant 
    153       1.44  perseant 		*vpp = vp;
    154       1.44  perseant 		ip = VTOI(vp);
    155   1.76.2.3       riz 		if (ip->i_gen == vers)
    156       1.44  perseant 			return 0;
    157   1.76.2.3       riz 		else if (ip->i_gen < vers) {
    158       1.69      fvdl 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    159   1.76.2.3       riz 			ip->i_gen = ip->i_ffs1_gen = vers;
    160       1.73   mycroft 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    161       1.44  perseant 			return 0;
    162       1.44  perseant 		} else {
    163       1.76  perseant 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    164   1.76.2.3       riz 			       ino, vers, ip->i_ffs1_gen));
    165       1.44  perseant 			vput(vp);
    166       1.44  perseant 			*vpp = NULLVP;
    167       1.44  perseant 			return EEXIST;
    168       1.44  perseant 		}
    169       1.44  perseant 	}
    170       1.44  perseant 
    171       1.44  perseant 	/*
    172       1.44  perseant 	 * The inode is not in use.  Find it on the free list.
    173       1.44  perseant 	 */
    174       1.44  perseant 	/* If the Ifile is too short to contain this inum, extend it */
    175       1.75     perry 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    176       1.75     perry 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    177       1.48  perseant 		<< fs->lfs_bshift) {
    178       1.44  perseant 		extend_ifile(fs, NOCRED);
    179       1.44  perseant 	}
    180       1.44  perseant 
    181       1.44  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    182       1.44  perseant 	oldnext = ifp->if_nextfree;
    183   1.76.2.3       riz 	ifp->if_version = vers;
    184       1.44  perseant 	brelse(bp);
    185       1.44  perseant 
    186       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    187       1.48  perseant 	if (ino) {
    188       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    189       1.44  perseant 	} else {
    190       1.48  perseant 		tino = ino;
    191       1.53       chs 		while (1) {
    192       1.44  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    193       1.44  perseant 			if (ifp->if_nextfree == ino ||
    194       1.44  perseant 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    195       1.44  perseant 				break;
    196       1.44  perseant 			tino = ifp->if_nextfree;
    197       1.44  perseant 			brelse(bp);
    198       1.44  perseant 		}
    199       1.44  perseant 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    200       1.44  perseant 			brelse(bp);
    201       1.44  perseant 			return ENOENT;
    202       1.44  perseant 		}
    203       1.44  perseant 		ifp->if_nextfree = oldnext;
    204       1.56  perseant 		LFS_BWRITE_LOG(bp);
    205       1.44  perseant 	}
    206       1.44  perseant 
    207   1.76.2.3       riz 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
    208       1.44  perseant 	if (error == 0) {
    209       1.44  perseant 		/*
    210       1.44  perseant 		 * Make it VREG so we can put blocks on it.  We will change
    211       1.44  perseant 		 * this later if it turns out to be some other kind of file.
    212       1.44  perseant 		 */
    213       1.44  perseant 		ip = VTOI(vp);
    214       1.66      fvdl 		ip->i_mode = ip->i_ffs1_mode = IFREG;
    215       1.66      fvdl 		ip->i_nlink = ip->i_ffs1_nlink = 1;
    216       1.44  perseant 		ip->i_ffs_effnlink = 1;
    217       1.44  perseant 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    218       1.44  perseant 		ip = VTOI(vp);
    219       1.44  perseant 
    220       1.76  perseant 		DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
    221       1.44  perseant 
    222       1.44  perseant 		/* The dirop-nature of this vnode is past */
    223       1.72      yamt 		lfs_unmark_vnode(vp);
    224       1.44  perseant 		(void)lfs_vunref(vp);
    225       1.44  perseant 		vp->v_flag &= ~VDIROP;
    226   1.76.2.2      tron 		simple_lock(&fs->lfs_interlock);
    227   1.76.2.2      tron 		simple_lock(&lfs_subsys_lock);
    228   1.76.2.2      tron 		--lfs_dirvcount;
    229   1.76.2.2      tron 		simple_unlock(&lfs_subsys_lock);
    230  1.76.2.10       riz 		--fs->lfs_dirvcount;
    231       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    232  1.76.2.10       riz 		wakeup(&lfs_dirvcount);
    233  1.76.2.10       riz 		wakeup(&fs->lfs_dirvcount);
    234   1.76.2.2      tron 		simple_unlock(&fs->lfs_interlock);
    235       1.44  perseant 	}
    236       1.44  perseant 	*vpp = vp;
    237       1.44  perseant 	return error;
    238       1.44  perseant }
    239       1.44  perseant 
    240       1.56  perseant /*
    241       1.65  perseant  * Add a new block to the Ifile, to accommodate future file creations.
    242       1.65  perseant  * Called with the segment lock held.
    243       1.56  perseant  */
    244       1.44  perseant static int
    245       1.44  perseant extend_ifile(struct lfs *fs, struct ucred *cred)
    246       1.44  perseant {
    247       1.44  perseant 	struct vnode *vp;
    248       1.44  perseant 	struct inode *ip;
    249       1.44  perseant 	IFILE *ifp;
    250       1.48  perseant 	IFILE_V1 *ifp_v1;
    251       1.48  perseant 	struct buf *bp, *cbp;
    252       1.44  perseant 	int error;
    253   1.76.2.3       riz 	daddr_t i, blkno, xmax;
    254   1.76.2.5       riz 	ino_t oldlast, maxino;
    255       1.48  perseant 	CLEANERINFO *cip;
    256       1.44  perseant 
    257   1.76.2.2      tron 	ASSERT_SEGLOCK(fs);
    258   1.76.2.2      tron 
    259       1.44  perseant 	vp = fs->lfs_ivnode;
    260       1.44  perseant 	ip = VTOI(vp);
    261       1.66      fvdl 	blkno = lblkno(fs, ip->i_size);
    262       1.66      fvdl 	if ((error = VOP_BALLOC(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    263       1.44  perseant 				&bp)) != 0) {
    264       1.44  perseant 		return (error);
    265       1.44  perseant 	}
    266       1.66      fvdl 	ip->i_size += fs->lfs_bsize;
    267       1.66      fvdl 	ip->i_ffs1_size = ip->i_size;
    268       1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    269       1.75     perry 
    270   1.76.2.6       riz 	maxino = ((ip->i_size >> fs->lfs_bshift) - fs->lfs_cleansz -
    271   1.76.2.6       riz 		  fs->lfs_segtabsz) * fs->lfs_ifpb;
    272   1.76.2.6       riz 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    273   1.76.2.6       riz 		realloc(fs->lfs_ino_bitmap, ((maxino + BMMASK) >> BMSHIFT) *
    274   1.76.2.6       riz 			sizeof(lfs_bm_t), M_SEGMENT, M_WAITOK);
    275   1.76.2.6       riz 	KASSERT(fs->lfs_ino_bitmap != NULL);
    276   1.76.2.5       riz 
    277       1.44  perseant 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    278       1.44  perseant 		fs->lfs_ifpb;
    279   1.76.2.5       riz 
    280   1.76.2.5       riz 	/*
    281   1.76.2.5       riz 	 * We insert the new inodes at the head of the free list.
    282   1.76.2.5       riz 	 * Under normal circumstances, the free list is empty here,
    283   1.76.2.5       riz 	 * so we are also incidentally placing them at the end (which
    284   1.76.2.5       riz 	 * we must do if we are to keep them in order).
    285   1.76.2.5       riz 	 */
    286       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    287       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    288       1.44  perseant #ifdef DIAGNOSTIC
    289       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    290       1.44  perseant 		panic("inode 0 allocated [2]");
    291       1.44  perseant #endif /* DIAGNOSTIC */
    292   1.76.2.3       riz 	xmax = i + fs->lfs_ifpb;
    293       1.48  perseant 
    294       1.53       chs 	if (fs->lfs_version == 1) {
    295   1.76.2.3       riz 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    296   1.76.2.7       riz 			SET_BITMAP_FREE(fs, i);
    297       1.48  perseant 			ifp_v1->if_version = 1;
    298       1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    299       1.48  perseant 			ifp_v1->if_nextfree = ++i;
    300       1.48  perseant 		}
    301       1.48  perseant 		ifp_v1--;
    302       1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    303       1.48  perseant 	} else {
    304   1.76.2.3       riz 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    305   1.76.2.7       riz 			SET_BITMAP_FREE(fs, i);
    306       1.48  perseant 			ifp->if_version = 1;
    307       1.48  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    308       1.48  perseant 			ifp->if_nextfree = ++i;
    309       1.48  perseant 		}
    310       1.48  perseant 		ifp--;
    311       1.48  perseant 		ifp->if_nextfree = oldlast;
    312       1.44  perseant 	}
    313   1.76.2.3       riz 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    314       1.48  perseant 
    315       1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    316       1.44  perseant 
    317       1.44  perseant 	return 0;
    318       1.44  perseant }
    319       1.44  perseant 
    320        1.1   mycroft /* Allocate a new inode. */
    321        1.1   mycroft /* ARGSUSED */
    322       1.43  perseant /* VOP_BWRITE 2i times */
    323        1.1   mycroft int
    324       1.48  perseant lfs_valloc(void *v)
    325        1.3  christos {
    326        1.1   mycroft 	struct vop_valloc_args /* {
    327       1.17  perseant 				  struct vnode *a_pvp;
    328       1.17  perseant 				  int a_mode;
    329       1.17  perseant 				  struct ucred *a_cred;
    330       1.17  perseant 				  struct vnode **a_vpp;
    331       1.17  perseant 				  } */ *ap = v;
    332        1.1   mycroft 	struct lfs *fs;
    333       1.48  perseant 	struct buf *bp, *cbp;
    334        1.1   mycroft 	struct ifile *ifp;
    335        1.1   mycroft 	ino_t new_ino;
    336        1.1   mycroft 	int error;
    337       1.36  perseant 	int new_gen;
    338       1.48  perseant 	CLEANERINFO *cip;
    339        1.1   mycroft 
    340       1.17  perseant 	fs = VTOI(ap->a_pvp)->i_lfs;
    341       1.38  perseant 	if (fs->lfs_ronly)
    342       1.38  perseant 		return EROFS;
    343       1.75     perry 
    344   1.76.2.2      tron 	ASSERT_NO_SEGLOCK(fs);
    345   1.76.2.2      tron 
    346       1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    347   1.76.2.2      tron 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    348       1.17  perseant 
    349        1.1   mycroft 	/* Get the head of the freelist. */
    350       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    351   1.76.2.5       riz 	KASSERT(new_ino != LFS_UNUSED_INUM && new_ino != LFS_IFILE_INUM);
    352       1.48  perseant 
    353   1.76.2.5       riz 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %lld\n",
    354   1.76.2.5       riz 	     (long long)new_ino));
    355       1.75     perry 
    356        1.1   mycroft 	/*
    357        1.1   mycroft 	 * Remove the inode from the free list and write the new start
    358        1.1   mycroft 	 * of the free list into the superblock.
    359        1.1   mycroft 	 */
    360   1.76.2.5       riz 	CLR_BITMAP_FREE(fs, new_ino);
    361        1.1   mycroft 	LFS_IENTRY(ifp, fs, new_ino, bp);
    362        1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    363   1.76.2.4       riz 		panic("lfs_valloc: inuse inode %llu on the free list",
    364   1.76.2.4       riz 		    (unsigned long long)new_ino);
    365       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    366   1.76.2.5       riz 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %lld -> %lld\n",
    367   1.76.2.5       riz 	     (long long)new_ino, (long long)ifp->if_nextfree));
    368       1.48  perseant 
    369       1.37  perseant 	new_gen = ifp->if_version; /* version was updated by vfree */
    370       1.28  perseant 	brelse(bp);
    371       1.30  perseant 
    372        1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    373       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    374       1.44  perseant 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    375       1.48  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    376   1.76.2.2      tron 			VOP_UNLOCK(fs->lfs_ivnode, 0);
    377       1.56  perseant 			lfs_segunlock(fs);
    378       1.44  perseant 			return error;
    379        1.1   mycroft 		}
    380        1.1   mycroft 	}
    381       1.17  perseant #ifdef DIAGNOSTIC
    382       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    383       1.17  perseant 		panic("inode 0 allocated [3]");
    384       1.17  perseant #endif /* DIAGNOSTIC */
    385       1.48  perseant 
    386   1.76.2.2      tron 	/* Set superblock modified bit and increment file count. */
    387   1.76.2.2      tron 	simple_lock(&fs->lfs_interlock);
    388   1.76.2.2      tron 	fs->lfs_fmod = 1;
    389   1.76.2.2      tron 	simple_unlock(&fs->lfs_interlock);
    390   1.76.2.2      tron 	++fs->lfs_nfiles;
    391   1.76.2.2      tron 
    392   1.76.2.2      tron 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    393       1.56  perseant 	lfs_segunlock(fs);
    394       1.63  perseant 
    395       1.44  perseant 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    396       1.44  perseant }
    397       1.44  perseant 
    398       1.65  perseant /*
    399       1.65  perseant  * Finish allocating a new inode, given an inode and generation number.
    400       1.65  perseant  */
    401       1.44  perseant static int
    402       1.44  perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    403       1.44  perseant 	   struct vnode **vpp)
    404       1.44  perseant {
    405       1.44  perseant 	struct inode *ip;
    406       1.44  perseant 	struct vnode *vp;
    407       1.40      fvdl 
    408   1.76.2.2      tron 	ASSERT_NO_SEGLOCK(fs);
    409   1.76.2.2      tron 
    410   1.76.2.1      tron 	vp = *vpp;
    411       1.31  perseant 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    412       1.40      fvdl 	/* Create an inode to associate with the vnode. */
    413       1.44  perseant 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    414       1.51       chs 
    415        1.1   mycroft 	ip = VTOI(vp);
    416       1.73   mycroft 	LFS_SET_UINO(ip, IN_CHANGE);
    417       1.66      fvdl 	/* on-disk structure has been zeroed out by lfs_vcreate */
    418       1.66      fvdl 	ip->i_din.ffs1_din->di_inumber = new_ino;
    419       1.51       chs 
    420   1.76.2.2      tron 	/* Note no blocks yet */
    421   1.76.2.2      tron 	ip->i_lfs_hiblk = -1;
    422   1.76.2.2      tron 
    423        1.1   mycroft 	/* Set a new generation number for this inode. */
    424       1.66      fvdl 	if (new_gen) {
    425       1.66      fvdl 		ip->i_gen = new_gen;
    426       1.66      fvdl 		ip->i_ffs1_gen = new_gen;
    427       1.66      fvdl 	}
    428       1.51       chs 
    429        1.1   mycroft 	/* Insert into the inode hash table. */
    430        1.1   mycroft 	ufs_ihashins(ip);
    431       1.26  perseant 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    432       1.44  perseant 
    433   1.76.2.1      tron 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
    434   1.76.2.1      tron 	vp = *vpp;
    435       1.44  perseant 	ip = VTOI(vp);
    436       1.57      yamt 
    437       1.57      yamt 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    438       1.51       chs 
    439       1.51       chs 	uvm_vnp_setsize(vp, 0);
    440       1.72      yamt 	lfs_mark_vnode(vp);
    441       1.54       chs 	genfs_node_init(vp, &lfs_genfsops);
    442       1.38  perseant 	VREF(ip->i_devvp);
    443        1.1   mycroft 	return (0);
    444        1.1   mycroft }
    445        1.1   mycroft 
    446        1.1   mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
    447       1.40      fvdl void
    448       1.48  perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    449        1.1   mycroft {
    450        1.1   mycroft 	struct inode *ip;
    451       1.66      fvdl 	struct ufs1_dinode *dp;
    452        1.1   mycroft 	struct ufsmount *ump;
    453   1.76.2.2      tron #ifdef QUOTA
    454        1.4        pk 	int i;
    455   1.76.2.2      tron #endif
    456       1.75     perry 
    457        1.1   mycroft 	/* Get a pointer to the private mount structure. */
    458        1.1   mycroft 	ump = VFSTOUFS(mp);
    459       1.75     perry 
    460   1.76.2.2      tron 	ASSERT_NO_SEGLOCK(ump->um_lfs);
    461   1.76.2.2      tron 
    462        1.1   mycroft 	/* Initialize the inode. */
    463       1.15   thorpej 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    464       1.66      fvdl 	memset(ip, 0, sizeof(*ip));
    465       1.66      fvdl 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
    466       1.66      fvdl 	memset(dp, 0, sizeof(*dp));
    467       1.63  perseant 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    468       1.74  perseant 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
    469       1.40      fvdl 	vp->v_data = ip;
    470       1.66      fvdl 	ip->i_din.ffs1_din = dp;
    471       1.66      fvdl 	ip->i_ump = ump;
    472       1.40      fvdl 	ip->i_vnode = vp;
    473        1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    474        1.1   mycroft 	ip->i_dev = ump->um_dev;
    475       1.66      fvdl 	ip->i_number = dp->di_inumber = ino;
    476        1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    477       1.66      fvdl 	ip->i_lfs_effnblks = 0;
    478   1.76.2.2      tron 	SPLAY_INIT(&ip->i_lfs_lbtree);
    479   1.76.2.2      tron 	ip->i_lfs_nbtree = 0;
    480   1.76.2.9       riz 	LIST_INIT(&ip->i_lfs_segdhd);
    481        1.1   mycroft #ifdef QUOTA
    482        1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    483        1.1   mycroft 		ip->i_dquot[i] = NODQUOT;
    484        1.1   mycroft #endif
    485       1.76  perseant #ifdef DEBUG
    486       1.56  perseant 	if (ino == LFS_IFILE_INUM)
    487       1.56  perseant 		vp->v_vnlock->lk_wmesg = "inlock";
    488       1.56  perseant #endif
    489        1.1   mycroft }
    490        1.1   mycroft 
    491   1.76.2.5       riz #if 0
    492   1.76.2.5       riz /*
    493   1.76.2.5       riz  * Find the highest-numbered allocated inode.
    494   1.76.2.5       riz  * This will be used to shrink the Ifile.
    495   1.76.2.5       riz  */
    496   1.76.2.5       riz static inline ino_t
    497   1.76.2.5       riz lfs_last_alloc_ino(struct lfs *fs)
    498   1.76.2.5       riz {
    499   1.76.2.5       riz 	ino_t ino, maxino;
    500   1.76.2.5       riz 
    501   1.76.2.5       riz 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    502   1.76.2.5       riz 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    503   1.76.2.5       riz 	for (ino = maxino - 1; ino > LFS_UNUSED_INUM; --ino) {
    504   1.76.2.5       riz 		if (ISSET_BITMAP_FREE(fs, ino) == 0)
    505   1.76.2.5       riz 			break;
    506   1.76.2.5       riz 	}
    507   1.76.2.5       riz 	return ino;
    508   1.76.2.5       riz }
    509   1.76.2.5       riz #endif
    510   1.76.2.5       riz 
    511   1.76.2.5       riz /*
    512   1.76.2.5       riz  * Find the previous (next lowest numbered) free inode, if any.
    513   1.76.2.5       riz  * If there is none, return LFS_UNUSED_INUM.
    514   1.76.2.5       riz  */
    515   1.76.2.5       riz static inline ino_t
    516   1.76.2.5       riz lfs_freelist_prev(struct lfs *fs, ino_t ino)
    517   1.76.2.5       riz {
    518   1.76.2.6       riz 	ino_t tino, bound, bb, freehdbb;
    519   1.76.2.6       riz 
    520   1.76.2.6       riz 	if (fs->lfs_freehd == LFS_UNUSED_INUM)	 /* No free inodes at all */
    521   1.76.2.6       riz 		return LFS_UNUSED_INUM;
    522   1.76.2.6       riz 
    523   1.76.2.6       riz 	/* Search our own word first */
    524   1.76.2.6       riz 	bound = ino & ~BMMASK;
    525   1.76.2.7       riz 	for (tino = ino - 1; tino >= bound && tino > LFS_UNUSED_INUM; tino--)
    526   1.76.2.6       riz 		if (ISSET_BITMAP_FREE(fs, tino))
    527   1.76.2.6       riz 			return tino;
    528   1.76.2.6       riz 	/* If there are no lower words to search, just return */
    529   1.76.2.6       riz 	if (ino >> BMSHIFT == 0)
    530   1.76.2.6       riz 		return LFS_UNUSED_INUM;
    531   1.76.2.6       riz 
    532   1.76.2.6       riz 	/*
    533   1.76.2.6       riz 	 * Find a word with a free inode in it.  We have to be a bit
    534   1.76.2.6       riz 	 * careful here since ino_t is unsigned.
    535   1.76.2.6       riz 	 */
    536   1.76.2.6       riz 	freehdbb = (fs->lfs_freehd >> BMSHIFT);
    537   1.76.2.6       riz 	for (bb = (ino >> BMSHIFT) - 1; bb >= freehdbb && bb > 0; --bb)
    538   1.76.2.6       riz 		if (fs->lfs_ino_bitmap[bb])
    539   1.76.2.6       riz 			break;
    540   1.76.2.6       riz 	if (fs->lfs_ino_bitmap[bb] == 0)
    541   1.76.2.6       riz 		return LFS_UNUSED_INUM;
    542   1.76.2.6       riz 
    543   1.76.2.6       riz 	/* Search the word we found */
    544   1.76.2.7       riz 	for (tino = (bb << BMSHIFT) | BMMASK; tino >= (bb << BMSHIFT) &&
    545   1.76.2.7       riz 	     tino > LFS_UNUSED_INUM; tino--)
    546   1.76.2.6       riz 		if (ISSET_BITMAP_FREE(fs, tino))
    547   1.76.2.6       riz 			break;
    548   1.76.2.5       riz 
    549   1.76.2.5       riz 	if (tino <= LFS_IFILE_INUM)
    550   1.76.2.5       riz 		tino = LFS_UNUSED_INUM;
    551   1.76.2.5       riz 
    552   1.76.2.5       riz 	return tino;
    553   1.76.2.5       riz }
    554   1.76.2.5       riz 
    555        1.1   mycroft /* Free an inode. */
    556        1.1   mycroft /* ARGUSED */
    557       1.43  perseant /* VOP_BWRITE 2i times */
    558        1.1   mycroft int
    559       1.48  perseant lfs_vfree(void *v)
    560        1.3  christos {
    561        1.1   mycroft 	struct vop_vfree_args /* {
    562       1.17  perseant 				 struct vnode *a_pvp;
    563       1.17  perseant 				 ino_t a_ino;
    564       1.17  perseant 				 int a_mode;
    565       1.17  perseant 				 } */ *ap = v;
    566        1.1   mycroft 	SEGUSE *sup;
    567       1.48  perseant 	CLEANERINFO *cip;
    568       1.48  perseant 	struct buf *cbp, *bp;
    569        1.1   mycroft 	struct ifile *ifp;
    570        1.1   mycroft 	struct inode *ip;
    571       1.30  perseant 	struct vnode *vp;
    572        1.1   mycroft 	struct lfs *fs;
    573       1.59      fvdl 	daddr_t old_iaddr;
    574       1.48  perseant 	ino_t ino, otail;
    575       1.56  perseant 	int s;
    576       1.75     perry 
    577        1.1   mycroft 	/* Get the inode number and file system. */
    578       1.30  perseant 	vp = ap->a_pvp;
    579       1.30  perseant 	ip = VTOI(vp);
    580        1.1   mycroft 	fs = ip->i_lfs;
    581        1.1   mycroft 	ino = ip->i_number;
    582       1.34  perseant 
    583   1.76.2.2      tron 	ASSERT_NO_SEGLOCK(fs);
    584   1.76.2.6       riz 	DLOG((DLOG_ALLOC, "lfs_vfree: free ino %lld\n", (long long)ino));
    585   1.76.2.2      tron 
    586       1.48  perseant 	/* Drain of pending writes */
    587   1.76.2.2      tron 	simple_lock(&vp->v_interlock);
    588       1.56  perseant 	s = splbio();
    589       1.48  perseant 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    590   1.76.2.2      tron 		ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
    591       1.56  perseant 	splx(s);
    592   1.76.2.2      tron 	simple_unlock(&vp->v_interlock);
    593       1.48  perseant 
    594       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    595   1.76.2.2      tron 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    596       1.75     perry 
    597       1.72      yamt 	lfs_unmark_vnode(vp);
    598       1.53       chs 	if (vp->v_flag & VDIROP) {
    599       1.30  perseant 		vp->v_flag &= ~VDIROP;
    600   1.76.2.2      tron 		simple_lock(&fs->lfs_interlock);
    601   1.76.2.2      tron 		simple_lock(&lfs_subsys_lock);
    602   1.76.2.2      tron 		--lfs_dirvcount;
    603   1.76.2.2      tron 		simple_unlock(&lfs_subsys_lock);
    604  1.76.2.10       riz 		--fs->lfs_dirvcount;
    605       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    606   1.76.2.2      tron 		simple_unlock(&fs->lfs_interlock);
    607  1.76.2.10       riz 		wakeup(&fs->lfs_dirvcount);
    608       1.30  perseant 		wakeup(&lfs_dirvcount);
    609       1.30  perseant 		lfs_vunref(vp);
    610   1.76.2.8       riz 
    611   1.76.2.8       riz 		/*
    612   1.76.2.8       riz 		 * If this inode is not going to be written any more, any
    613   1.76.2.8       riz 		 * segment accounting left over from its truncation needs
    614   1.76.2.8       riz 		 * to occur at the end of the next dirops flush.  Attach
    615   1.76.2.8       riz 		 * them to the fs-wide list for that purpose.
    616   1.76.2.8       riz 		 */
    617   1.76.2.8       riz 		if (LIST_FIRST(&ip->i_lfs_segdhd) != NULL) {
    618   1.76.2.8       riz 			struct segdelta *sd;
    619   1.76.2.8       riz 
    620   1.76.2.8       riz 			while((sd = LIST_FIRST(&ip->i_lfs_segdhd)) != NULL) {
    621   1.76.2.8       riz 				LIST_REMOVE(sd, list);
    622   1.76.2.8       riz 				LIST_INSERT_HEAD(&fs->lfs_segdhd, sd, list);
    623   1.76.2.8       riz 			}
    624   1.76.2.8       riz 		}
    625   1.76.2.8       riz 	} else {
    626   1.76.2.8       riz 		/*
    627   1.76.2.8       riz 		 * If it's not a dirop, we can finalize right away.
    628   1.76.2.8       riz 		 */
    629   1.76.2.8       riz 		lfs_finalize_ino_seguse(fs, ip);
    630       1.38  perseant 	}
    631       1.30  perseant 
    632       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    633       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    634  1.76.2.11       riz 	ip->i_lfs_iflags |= LFSI_DELETED;
    635  1.76.2.11       riz 
    636        1.1   mycroft 	/*
    637        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    638       1.48  perseant 	 * and link it onto the free chain.
    639        1.1   mycroft 	 */
    640   1.76.2.5       riz 	SET_BITMAP_FREE(fs, ino);
    641        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    642        1.1   mycroft 	old_iaddr = ifp->if_daddr;
    643        1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    644        1.1   mycroft 	++ifp->if_version;
    645       1.48  perseant 	if (fs->lfs_version == 1) {
    646       1.48  perseant 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    647       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    648       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    649       1.48  perseant 	} else {
    650   1.76.2.5       riz 		ino_t tino, onf;
    651   1.76.2.5       riz 
    652       1.48  perseant 		ifp->if_nextfree = LFS_UNUSED_INUM;
    653       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    654   1.76.2.5       riz 
    655   1.76.2.5       riz 		tino = lfs_freelist_prev(fs, ino);
    656   1.76.2.5       riz 		if (tino == LFS_UNUSED_INUM) {
    657   1.76.2.5       riz 			/* Nothing free below us, put us on the head */
    658   1.76.2.5       riz 			LFS_IENTRY(ifp, fs, ino, bp);
    659   1.76.2.5       riz 			LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    660   1.76.2.5       riz 			LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    661   1.76.2.6       riz 			DLOG((DLOG_ALLOC, "lfs_vfree: headfree %lld -> %lld\n",
    662   1.76.2.5       riz 			     (long long)ifp->if_nextfree, (long long)ino));
    663   1.76.2.5       riz 			LFS_BWRITE_LOG(bp); /* Ifile */
    664   1.76.2.5       riz 
    665   1.76.2.5       riz 			/* If the list was empty, set tail too */
    666   1.76.2.5       riz 			LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    667   1.76.2.5       riz 			if (otail == LFS_UNUSED_INUM) {
    668   1.76.2.5       riz 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    669   1.76.2.5       riz 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    670   1.76.2.5       riz 				      "-> %lld\n", (long long)otail,
    671   1.76.2.5       riz 				      (long long)ino));
    672   1.76.2.5       riz 			}
    673   1.76.2.5       riz 		} else {
    674   1.76.2.5       riz 			/*
    675   1.76.2.5       riz 			 * Insert this inode into the list after tino.
    676   1.76.2.5       riz 			 * We hold the segment lock so we don't have to
    677   1.76.2.5       riz 			 * worry about blocks being written out of order.
    678   1.76.2.5       riz 			 */
    679   1.76.2.5       riz 			DLOG((DLOG_ALLOC, "lfs_vfree: insert ino %lld "
    680   1.76.2.5       riz 			      " after %lld\n", ino, tino));
    681   1.76.2.5       riz 
    682   1.76.2.5       riz 			LFS_IENTRY(ifp, fs, tino, bp);
    683   1.76.2.5       riz 			onf = ifp->if_nextfree;
    684   1.76.2.5       riz 			ifp->if_nextfree = ino;
    685   1.76.2.5       riz 			LFS_BWRITE_LOG(bp);	/* Ifile */
    686   1.76.2.5       riz 
    687   1.76.2.5       riz 			LFS_IENTRY(ifp, fs, ino, bp);
    688   1.76.2.5       riz 			ifp->if_nextfree = onf;
    689   1.76.2.5       riz 			LFS_BWRITE_LOG(bp);	/* Ifile */
    690   1.76.2.5       riz 
    691   1.76.2.5       riz 			/* If we're last, put us on the tail */
    692   1.76.2.5       riz 			if (onf == LFS_UNUSED_INUM) {
    693   1.76.2.5       riz 				LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    694   1.76.2.5       riz 				LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    695   1.76.2.5       riz 				DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %lld "
    696   1.76.2.5       riz 				      "-> %lld\n", (long long)otail,
    697   1.76.2.5       riz 				      (long long)ino));
    698   1.76.2.5       riz 			}
    699   1.76.2.5       riz 		}
    700       1.48  perseant 	}
    701       1.17  perseant #ifdef DIAGNOSTIC
    702       1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    703       1.17  perseant 		panic("inode 0 freed");
    704       1.17  perseant 	}
    705       1.17  perseant #endif /* DIAGNOSTIC */
    706        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    707       1.48  perseant 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    708       1.22  perseant #ifdef DIAGNOSTIC
    709       1.66      fvdl 		if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
    710       1.43  perseant 			printf("lfs_vfree: negative byte count"
    711       1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    712       1.48  perseant 			       dtosn(fs, old_iaddr),
    713       1.66      fvdl 			       (int)sizeof (struct ufs1_dinode) -
    714       1.66      fvdl 				    sup->su_nbytes);
    715       1.22  perseant 			panic("lfs_vfree: negative byte count");
    716       1.66      fvdl 			sup->su_nbytes = sizeof (struct ufs1_dinode);
    717       1.17  perseant 		}
    718       1.22  perseant #endif
    719       1.66      fvdl 		sup->su_nbytes -= sizeof (struct ufs1_dinode);
    720       1.63  perseant 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
    721        1.1   mycroft 	}
    722       1.75     perry 
    723        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    724   1.76.2.2      tron 	simple_lock(&fs->lfs_interlock);
    725        1.1   mycroft 	fs->lfs_fmod = 1;
    726   1.76.2.2      tron 	simple_unlock(&fs->lfs_interlock);
    727        1.1   mycroft 	--fs->lfs_nfiles;
    728       1.75     perry 
    729   1.76.2.2      tron 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    730       1.56  perseant 	lfs_segunlock(fs);
    731       1.63  perseant 
    732        1.1   mycroft 	return (0);
    733        1.1   mycroft }
    734   1.76.2.5       riz 
    735   1.76.2.5       riz /*
    736   1.76.2.5       riz  * Sort the freelist and set up the free-inode bitmap.
    737   1.76.2.5       riz  * To be called by lfs_mountfs().
    738   1.76.2.5       riz  */
    739   1.76.2.5       riz void
    740   1.76.2.5       riz lfs_order_freelist(struct lfs *fs)
    741   1.76.2.5       riz {
    742   1.76.2.5       riz 	CLEANERINFO *cip;
    743   1.76.2.5       riz 	IFILE *ifp = NULL;
    744   1.76.2.5       riz 	struct buf *bp;
    745   1.76.2.5       riz 	ino_t ino, firstino, lastino, maxino;
    746   1.76.2.5       riz 
    747   1.76.2.5       riz 	maxino = ((fs->lfs_ivnode->v_size >> fs->lfs_bshift) -
    748   1.76.2.5       riz 		  fs->lfs_cleansz - fs->lfs_segtabsz) * fs->lfs_ifpb;
    749   1.76.2.6       riz 	fs->lfs_ino_bitmap = (lfs_bm_t *)
    750   1.76.2.6       riz 		malloc(((maxino + BMMASK) >> BMSHIFT) * sizeof(lfs_bm_t),
    751   1.76.2.6       riz 		       M_SEGMENT, M_WAITOK | M_ZERO);
    752   1.76.2.6       riz 	KASSERT(fs->lfs_ino_bitmap != NULL);
    753   1.76.2.5       riz 
    754   1.76.2.5       riz 	firstino = lastino = LFS_UNUSED_INUM;
    755   1.76.2.5       riz 	for (ino = 0; ino < maxino; ino++) {
    756   1.76.2.5       riz 		if (ino % fs->lfs_ifpb == 0)
    757   1.76.2.5       riz 			LFS_IENTRY(ifp, fs, ino, bp);
    758   1.76.2.5       riz 		else
    759   1.76.2.5       riz 			++ifp;
    760   1.76.2.5       riz 
    761   1.76.2.5       riz 		/* Don't put zero or ifile on the free list */
    762   1.76.2.5       riz 		if (ino == LFS_UNUSED_INUM || ino == LFS_IFILE_INUM)
    763   1.76.2.5       riz 			continue;
    764   1.76.2.5       riz 
    765   1.76.2.5       riz 		if (ifp->if_daddr == LFS_UNUSED_DADDR) {
    766   1.76.2.5       riz 			if (firstino == LFS_UNUSED_INUM)
    767   1.76.2.5       riz 				firstino = ino;
    768   1.76.2.5       riz 			else {
    769   1.76.2.5       riz 				brelse(bp);
    770   1.76.2.5       riz 
    771   1.76.2.5       riz 				LFS_IENTRY(ifp, fs, lastino, bp);
    772   1.76.2.5       riz 				ifp->if_nextfree = ino;
    773   1.76.2.5       riz 				LFS_BWRITE_LOG(bp);
    774   1.76.2.5       riz 
    775   1.76.2.5       riz 				LFS_IENTRY(ifp, fs, ino, bp);
    776   1.76.2.5       riz 			}
    777   1.76.2.5       riz 			lastino = ino;
    778   1.76.2.5       riz 
    779   1.76.2.5       riz 			SET_BITMAP_FREE(fs, ino);
    780   1.76.2.5       riz 		}
    781   1.76.2.5       riz 
    782   1.76.2.5       riz 		if ((ino + 1) % fs->lfs_ifpb == 0)
    783   1.76.2.5       riz 			brelse(bp);
    784   1.76.2.5       riz 	}
    785   1.76.2.5       riz 
    786   1.76.2.5       riz 	LFS_PUT_HEADFREE(fs, cip, bp, firstino);
    787   1.76.2.5       riz 	LFS_PUT_TAILFREE(fs, cip, bp, lastino);
    788   1.76.2.5       riz }
    789