Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.62
      1  1.62      yamt /*	$NetBSD: lfs_alloc.c,v 1.62 2003/01/27 23:17:56 yamt Exp $	*/
      2   1.2       cgd 
      3  1.17  perseant /*-
      4  1.38  perseant  * Copyright (c) 1999, 2000 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.17  perseant  *      This product includes software developed by the NetBSD
     21  1.17  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.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     51   1.1   mycroft  *    must display the following acknowledgement:
     52   1.1   mycroft  *	This product includes software developed by the University of
     53   1.1   mycroft  *	California, Berkeley and its contributors.
     54   1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     55   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     56   1.1   mycroft  *    without specific prior written permission.
     57   1.1   mycroft  *
     58   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1   mycroft  * SUCH DAMAGE.
     69   1.1   mycroft  *
     70   1.2       cgd  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     71   1.1   mycroft  */
     72  1.52     lukem 
     73  1.52     lukem #include <sys/cdefs.h>
     74  1.62      yamt __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.62 2003/01/27 23:17:56 yamt Exp $");
     75  1.12    scottr 
     76  1.47       mrg #if defined(_KERNEL_OPT)
     77  1.12    scottr #include "opt_quota.h"
     78  1.13    scottr #endif
     79   1.1   mycroft 
     80   1.1   mycroft #include <sys/param.h>
     81   1.3  christos #include <sys/systm.h>
     82   1.1   mycroft #include <sys/kernel.h>
     83   1.1   mycroft #include <sys/buf.h>
     84  1.56  perseant #include <sys/lock.h>
     85   1.1   mycroft #include <sys/vnode.h>
     86   1.1   mycroft #include <sys/syslog.h>
     87   1.1   mycroft #include <sys/mount.h>
     88   1.1   mycroft #include <sys/malloc.h>
     89  1.15   thorpej #include <sys/pool.h>
     90  1.50       chs #include <sys/proc.h>
     91   1.1   mycroft 
     92   1.1   mycroft #include <ufs/ufs/quota.h>
     93   1.1   mycroft #include <ufs/ufs/inode.h>
     94   1.1   mycroft #include <ufs/ufs/ufsmount.h>
     95   1.3  christos #include <ufs/ufs/ufs_extern.h>
     96   1.1   mycroft 
     97   1.1   mycroft #include <ufs/lfs/lfs.h>
     98   1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     99   1.1   mycroft 
    100  1.38  perseant extern int lfs_dirvcount;
    101  1.17  perseant extern struct lock ufs_hashlock;
    102  1.17  perseant 
    103  1.44  perseant static int extend_ifile(struct lfs *, struct ucred *);
    104  1.44  perseant static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
    105  1.44  perseant 
    106  1.44  perseant /*
    107  1.44  perseant  * Allocate a particular inode with a particular version number, freeing
    108  1.44  perseant  * any previous versions of this inode that may have gone before.
    109  1.44  perseant  * Used by the roll-forward code.
    110  1.44  perseant  *
    111  1.44  perseant  * XXX this function does not have appropriate locking to be used on a live fs;
    112  1.44  perseant  * XXX but something similar could probably be used for an "undelete" call.
    113  1.56  perseant  *
    114  1.56  perseant  * Called with the Ifile inode locked.
    115  1.44  perseant  */
    116  1.44  perseant int
    117  1.44  perseant lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
    118  1.44  perseant 	      struct vnode **vpp)
    119  1.44  perseant {
    120  1.44  perseant 	IFILE *ifp;
    121  1.48  perseant 	struct buf *bp, *cbp;
    122  1.44  perseant 	struct vnode *vp;
    123  1.44  perseant 	struct inode *ip;
    124  1.44  perseant 	ino_t tino, oldnext;
    125  1.44  perseant 	int error;
    126  1.48  perseant 	CLEANERINFO *cip;
    127  1.44  perseant 
    128  1.44  perseant 	/*
    129  1.44  perseant 	 * First, just try a vget. If the version number is the one we want,
    130  1.44  perseant 	 * we don't have to do anything else.  If the version number is wrong,
    131  1.44  perseant 	 * take appropriate action.
    132  1.44  perseant 	 */
    133  1.44  perseant 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    134  1.44  perseant 	if (error == 0) {
    135  1.44  perseant 		/* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
    136  1.44  perseant 
    137  1.44  perseant 		*vpp = vp;
    138  1.44  perseant 		ip = VTOI(vp);
    139  1.44  perseant 		if (ip->i_ffs_gen == version)
    140  1.44  perseant 			return 0;
    141  1.44  perseant 		else if (ip->i_ffs_gen < version) {
    142  1.44  perseant 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    143  1.44  perseant 			ip->i_ffs_gen = version;
    144  1.44  perseant 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    145  1.44  perseant 			return 0;
    146  1.44  perseant 		} else {
    147  1.44  perseant 			/* printf("ino %d: asked for version %d but got %d\n",
    148  1.44  perseant 			       ino, version, ip->i_ffs_gen); */
    149  1.44  perseant 			vput(vp);
    150  1.44  perseant 			*vpp = NULLVP;
    151  1.44  perseant 			return EEXIST;
    152  1.44  perseant 		}
    153  1.44  perseant 	}
    154  1.44  perseant 
    155  1.44  perseant 	/*
    156  1.44  perseant 	 * The inode is not in use.  Find it on the free list.
    157  1.44  perseant 	 */
    158  1.44  perseant 	/* If the Ifile is too short to contain this inum, extend it */
    159  1.48  perseant 	while (VTOI(fs->lfs_ivnode)->i_ffs_size <= (ino /
    160  1.48  perseant 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    161  1.48  perseant 		<< fs->lfs_bshift) {
    162  1.44  perseant 		extend_ifile(fs, NOCRED);
    163  1.44  perseant 	}
    164  1.44  perseant 
    165  1.44  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    166  1.44  perseant 	oldnext = ifp->if_nextfree;
    167  1.44  perseant 	ifp->if_version = version;
    168  1.44  perseant 	brelse(bp);
    169  1.44  perseant 
    170  1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    171  1.48  perseant 	if (ino) {
    172  1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    173  1.44  perseant 	} else {
    174  1.48  perseant 		tino = ino;
    175  1.53       chs 		while (1) {
    176  1.44  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    177  1.44  perseant 			if (ifp->if_nextfree == ino ||
    178  1.44  perseant 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    179  1.44  perseant 				break;
    180  1.44  perseant 			tino = ifp->if_nextfree;
    181  1.44  perseant 			brelse(bp);
    182  1.44  perseant 		}
    183  1.44  perseant 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    184  1.44  perseant 			brelse(bp);
    185  1.44  perseant 			return ENOENT;
    186  1.44  perseant 		}
    187  1.44  perseant 		ifp->if_nextfree = oldnext;
    188  1.56  perseant 		LFS_BWRITE_LOG(bp);
    189  1.44  perseant 	}
    190  1.44  perseant 
    191  1.44  perseant 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
    192  1.44  perseant 	if (error == 0) {
    193  1.44  perseant 		/*
    194  1.44  perseant 		 * Make it VREG so we can put blocks on it.  We will change
    195  1.44  perseant 		 * this later if it turns out to be some other kind of file.
    196  1.44  perseant 		 */
    197  1.44  perseant 		ip = VTOI(vp);
    198  1.44  perseant 		ip->i_ffs_mode = IFREG;
    199  1.44  perseant 		ip->i_ffs_nlink = 1;
    200  1.44  perseant 		ip->i_ffs_effnlink = 1;
    201  1.44  perseant 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    202  1.44  perseant 		ip = VTOI(vp);
    203  1.44  perseant 
    204  1.44  perseant 		/* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
    205  1.44  perseant 
    206  1.44  perseant 		/* The dirop-nature of this vnode is past */
    207  1.44  perseant 		(void)lfs_vunref(vp);
    208  1.44  perseant 		--lfs_dirvcount;
    209  1.44  perseant 		vp->v_flag &= ~VDIROP;
    210  1.44  perseant 		--fs->lfs_nadirop;
    211  1.44  perseant 		ip->i_flag &= ~IN_ADIROP;
    212  1.44  perseant 	}
    213  1.44  perseant 	*vpp = vp;
    214  1.44  perseant 	return error;
    215  1.44  perseant }
    216  1.44  perseant 
    217  1.56  perseant /*
    218  1.56  perseant  * Called with the Ifile inode locked.
    219  1.56  perseant  */
    220  1.44  perseant static int
    221  1.44  perseant extend_ifile(struct lfs *fs, struct ucred *cred)
    222  1.44  perseant {
    223  1.44  perseant 	struct vnode *vp;
    224  1.44  perseant 	struct inode *ip;
    225  1.44  perseant 	IFILE *ifp;
    226  1.48  perseant 	IFILE_V1 *ifp_v1;
    227  1.48  perseant 	struct buf *bp, *cbp;
    228  1.44  perseant 	int error;
    229  1.59      fvdl 	daddr_t i, blkno, max;
    230  1.44  perseant 	ino_t oldlast;
    231  1.48  perseant 	CLEANERINFO *cip;
    232  1.44  perseant 
    233  1.44  perseant 	vp = fs->lfs_ivnode;
    234  1.44  perseant 	ip = VTOI(vp);
    235  1.44  perseant 	blkno = lblkno(fs, ip->i_ffs_size);
    236  1.44  perseant 	if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
    237  1.44  perseant 				&bp)) != 0) {
    238  1.44  perseant 		return (error);
    239  1.44  perseant 	}
    240  1.44  perseant 	ip->i_ffs_size += fs->lfs_bsize;
    241  1.44  perseant 	uvm_vnp_setsize(vp, ip->i_ffs_size);
    242  1.44  perseant 
    243  1.44  perseant 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    244  1.44  perseant 		fs->lfs_ifpb;
    245  1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    246  1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    247  1.44  perseant #ifdef DIAGNOSTIC
    248  1.53       chs 	if (fs->lfs_free == LFS_UNUSED_INUM)
    249  1.44  perseant 		panic("inode 0 allocated [2]");
    250  1.44  perseant #endif /* DIAGNOSTIC */
    251  1.44  perseant 	max = i + fs->lfs_ifpb;
    252  1.55  perseant 	/* printf("extend_ifile: new block ino %d--%d\n", i, max - 1); */
    253  1.48  perseant 
    254  1.53       chs 	if (fs->lfs_version == 1) {
    255  1.48  perseant 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < max; ++ifp_v1) {
    256  1.48  perseant 			ifp_v1->if_version = 1;
    257  1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    258  1.48  perseant 			ifp_v1->if_nextfree = ++i;
    259  1.48  perseant 		}
    260  1.48  perseant 		ifp_v1--;
    261  1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    262  1.48  perseant 	} else {
    263  1.48  perseant 		for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
    264  1.48  perseant 			ifp->if_version = 1;
    265  1.48  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    266  1.48  perseant 			ifp->if_nextfree = ++i;
    267  1.48  perseant 		}
    268  1.48  perseant 		ifp--;
    269  1.48  perseant 		ifp->if_nextfree = oldlast;
    270  1.44  perseant 	}
    271  1.55  perseant 	LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
    272  1.48  perseant 
    273  1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    274  1.44  perseant 
    275  1.44  perseant 	return 0;
    276  1.44  perseant }
    277  1.44  perseant 
    278   1.1   mycroft /* Allocate a new inode. */
    279   1.1   mycroft /* ARGSUSED */
    280  1.43  perseant /* VOP_BWRITE 2i times */
    281   1.1   mycroft int
    282  1.48  perseant lfs_valloc(void *v)
    283   1.3  christos {
    284   1.1   mycroft 	struct vop_valloc_args /* {
    285  1.17  perseant 				  struct vnode *a_pvp;
    286  1.17  perseant 				  int a_mode;
    287  1.17  perseant 				  struct ucred *a_cred;
    288  1.17  perseant 				  struct vnode **a_vpp;
    289  1.17  perseant 				  } */ *ap = v;
    290   1.1   mycroft 	struct lfs *fs;
    291  1.48  perseant 	struct buf *bp, *cbp;
    292   1.1   mycroft 	struct ifile *ifp;
    293   1.1   mycroft 	ino_t new_ino;
    294   1.1   mycroft 	int error;
    295  1.36  perseant 	int new_gen;
    296  1.48  perseant 	CLEANERINFO *cip;
    297   1.1   mycroft 
    298  1.17  perseant 	fs = VTOI(ap->a_pvp)->i_lfs;
    299  1.38  perseant 	if (fs->lfs_ronly)
    300  1.38  perseant 		return EROFS;
    301  1.38  perseant 	*ap->a_vpp = NULL;
    302  1.17  perseant 
    303  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    304  1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    305  1.56  perseant #else
    306  1.48  perseant 	if (fs->lfs_version == 1) {
    307  1.48  perseant 		/*
    308  1.48  perseant 		 * Use lfs_seglock here, instead of fs->lfs_freelock, to
    309  1.48  perseant 		 * ensure that the free list is not changed in between
    310  1.48  perseant 		 * the time that the ifile blocks are written to disk
    311  1.48  perseant 		 * and the time that the superblock is written to disk.
    312  1.48  perseant 		 */
    313  1.48  perseant 		lfs_seglock(fs, SEGM_PROT);
    314  1.48  perseant 	} else {
    315  1.48  perseant 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    316  1.48  perseant 	}
    317  1.56  perseant #endif
    318  1.17  perseant 
    319   1.1   mycroft 	/* Get the head of the freelist. */
    320  1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    321  1.48  perseant 
    322  1.17  perseant #ifdef DIAGNOSTIC
    323  1.53       chs 	if (new_ino == LFS_UNUSED_INUM) {
    324  1.17  perseant #ifdef DEBUG
    325  1.17  perseant 		lfs_dump_super(fs);
    326  1.17  perseant #endif /* DEBUG */
    327  1.17  perseant 		panic("inode 0 allocated [1]");
    328  1.17  perseant 	}
    329  1.17  perseant #endif /* DIAGNOSTIC */
    330   1.1   mycroft #ifdef ALLOCPRINT
    331  1.44  perseant 	printf("lfs_valloc: allocate inode %d\n", new_ino);
    332   1.1   mycroft #endif
    333  1.17  perseant 
    334   1.1   mycroft 	/*
    335   1.1   mycroft 	 * Remove the inode from the free list and write the new start
    336   1.1   mycroft 	 * of the free list into the superblock.
    337   1.1   mycroft 	 */
    338   1.1   mycroft 	LFS_IENTRY(ifp, fs, new_ino, bp);
    339   1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    340  1.44  perseant 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    341  1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    342  1.55  perseant 	/* printf("lfs_valloc: headfree %d -> %d\n", new_ino, ifp->if_nextfree); */
    343  1.48  perseant 
    344  1.37  perseant 	new_gen = ifp->if_version; /* version was updated by vfree */
    345  1.28  perseant 	brelse(bp);
    346  1.30  perseant 
    347   1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    348   1.1   mycroft 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    349  1.44  perseant 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    350  1.48  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    351  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    352  1.56  perseant 			lfs_segunlock(fs);
    353  1.56  perseant #else
    354  1.48  perseant 			if (fs->lfs_version == 1)
    355  1.48  perseant 				lfs_segunlock(fs);
    356  1.48  perseant 			else
    357  1.48  perseant 				lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    358  1.56  perseant #endif
    359  1.44  perseant 			return error;
    360   1.1   mycroft 		}
    361   1.1   mycroft 	}
    362  1.17  perseant #ifdef DIAGNOSTIC
    363  1.53       chs 	if (fs->lfs_free == LFS_UNUSED_INUM)
    364  1.17  perseant 		panic("inode 0 allocated [3]");
    365  1.17  perseant #endif /* DIAGNOSTIC */
    366  1.48  perseant 
    367  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    368  1.56  perseant 	lfs_segunlock(fs);
    369  1.56  perseant #else
    370  1.48  perseant 	if (fs->lfs_version == 1)
    371  1.48  perseant 		lfs_segunlock(fs);
    372  1.48  perseant 	else
    373  1.48  perseant 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    374  1.56  perseant #endif
    375  1.44  perseant 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    376  1.44  perseant }
    377  1.44  perseant 
    378  1.44  perseant static int
    379  1.44  perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    380  1.44  perseant 	   struct vnode **vpp)
    381  1.44  perseant {
    382  1.44  perseant 	struct inode *ip;
    383  1.44  perseant 	struct vnode *vp;
    384  1.44  perseant 	IFILE *ifp;
    385  1.48  perseant 	struct buf *bp, *cbp;
    386  1.44  perseant 	int error;
    387  1.48  perseant 	CLEANERINFO *cip;
    388  1.44  perseant 
    389  1.44  perseant 	error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
    390  1.44  perseant 	/* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
    391  1.44  perseant 	if (error)
    392  1.40      fvdl 		goto errout;
    393  1.40      fvdl 
    394  1.31  perseant 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    395  1.40      fvdl 	/* Create an inode to associate with the vnode. */
    396  1.44  perseant 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    397  1.51       chs 
    398   1.1   mycroft 	ip = VTOI(vp);
    399  1.48  perseant 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
    400   1.1   mycroft 	/* Zero out the direct and indirect block addresses. */
    401  1.16   thorpej 	bzero(&ip->i_din, sizeof(ip->i_din));
    402   1.8    bouyer 	ip->i_din.ffs_din.di_inumber = new_ino;
    403  1.51       chs 
    404   1.1   mycroft 	/* Set a new generation number for this inode. */
    405  1.44  perseant 	if (new_gen)
    406  1.44  perseant 		ip->i_ffs_gen = new_gen;
    407  1.51       chs 
    408   1.1   mycroft 	/* Insert into the inode hash table. */
    409   1.1   mycroft 	ufs_ihashins(ip);
    410  1.26  perseant 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    411  1.44  perseant 
    412  1.49       chs 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    413  1.44  perseant 	ip = VTOI(vp);
    414  1.44  perseant 	/* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
    415  1.57      yamt 
    416  1.57      yamt 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    417  1.51       chs 
    418  1.51       chs 	uvm_vnp_setsize(vp, 0);
    419  1.44  perseant 	*vpp = vp;
    420  1.38  perseant #if 1
    421  1.53       chs 	if (!(vp->v_flag & VDIROP)) {
    422  1.35  perseant 		(void)lfs_vref(vp);
    423  1.26  perseant 		++lfs_dirvcount;
    424  1.17  perseant 	}
    425   1.1   mycroft 	vp->v_flag |= VDIROP;
    426  1.51       chs 
    427  1.53       chs 	if (!(ip->i_flag & IN_ADIROP))
    428  1.32  perseant 		++fs->lfs_nadirop;
    429  1.32  perseant 	ip->i_flag |= IN_ADIROP;
    430  1.38  perseant #endif
    431  1.54       chs 	genfs_node_init(vp, &lfs_genfsops);
    432  1.38  perseant 	VREF(ip->i_devvp);
    433   1.1   mycroft 	/* Set superblock modified bit and increment file count. */
    434   1.1   mycroft 	fs->lfs_fmod = 1;
    435   1.1   mycroft 	++fs->lfs_nfiles;
    436   1.1   mycroft 	return (0);
    437  1.38  perseant 
    438  1.38  perseant     errout:
    439  1.38  perseant 	/*
    440  1.38  perseant 	 * Put the new inum back on the free list.
    441  1.38  perseant 	 */
    442  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    443  1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    444  1.56  perseant #endif
    445  1.38  perseant 	LFS_IENTRY(ifp, fs, new_ino, bp);
    446  1.38  perseant 	ifp->if_daddr = LFS_UNUSED_DADDR;
    447  1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    448  1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    449  1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    450  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    451  1.56  perseant 	lfs_segunlock(fs);
    452  1.56  perseant #endif
    453  1.38  perseant 
    454  1.44  perseant 	*vpp = NULLVP;
    455  1.38  perseant 	return (error);
    456   1.1   mycroft }
    457   1.1   mycroft 
    458   1.1   mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
    459  1.40      fvdl void
    460  1.48  perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    461   1.1   mycroft {
    462   1.1   mycroft 	struct inode *ip;
    463   1.1   mycroft 	struct ufsmount *ump;
    464   1.4        pk #ifdef QUOTA
    465   1.4        pk 	int i;
    466   1.4        pk #endif
    467  1.17  perseant 
    468   1.1   mycroft 	/* Get a pointer to the private mount structure. */
    469   1.1   mycroft 	ump = VFSTOUFS(mp);
    470  1.17  perseant 
    471   1.1   mycroft 	/* Initialize the inode. */
    472  1.15   thorpej 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    473  1.40      fvdl 	vp->v_data = ip;
    474  1.40      fvdl 	ip->i_vnode = vp;
    475   1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    476   1.1   mycroft 	ip->i_dev = ump->um_dev;
    477   1.8    bouyer 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    478   1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    479   1.1   mycroft #ifdef QUOTA
    480   1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    481   1.1   mycroft 		ip->i_dquot[i] = NODQUOT;
    482   1.1   mycroft #endif
    483   1.1   mycroft 	ip->i_lockf = 0;
    484   1.1   mycroft 	ip->i_diroff = 0;
    485   1.8    bouyer 	ip->i_ffs_mode = 0;
    486   1.8    bouyer 	ip->i_ffs_size = 0;
    487   1.8    bouyer 	ip->i_ffs_blocks = 0;
    488  1.41  perseant 	ip->i_lfs_effnblks = 0;
    489  1.42  perseant 	ip->i_flag = 0;
    490  1.48  perseant 	/* Why was IN_MODIFIED ever set here? */
    491  1.48  perseant 	/* LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED); */
    492  1.56  perseant 
    493  1.56  perseant #ifdef DEBUG_LFS_VNLOCK
    494  1.56  perseant 	if (ino == LFS_IFILE_INUM)
    495  1.56  perseant 		vp->v_vnlock->lk_wmesg = "inlock";
    496  1.56  perseant #endif
    497   1.1   mycroft }
    498   1.1   mycroft 
    499   1.1   mycroft /* Free an inode. */
    500   1.1   mycroft /* ARGUSED */
    501  1.43  perseant /* VOP_BWRITE 2i times */
    502   1.1   mycroft int
    503  1.48  perseant lfs_vfree(void *v)
    504   1.3  christos {
    505   1.1   mycroft 	struct vop_vfree_args /* {
    506  1.17  perseant 				 struct vnode *a_pvp;
    507  1.17  perseant 				 ino_t a_ino;
    508  1.17  perseant 				 int a_mode;
    509  1.17  perseant 				 } */ *ap = v;
    510   1.1   mycroft 	SEGUSE *sup;
    511  1.48  perseant 	CLEANERINFO *cip;
    512  1.48  perseant 	struct buf *cbp, *bp;
    513   1.1   mycroft 	struct ifile *ifp;
    514   1.1   mycroft 	struct inode *ip;
    515  1.30  perseant 	struct vnode *vp;
    516   1.1   mycroft 	struct lfs *fs;
    517  1.59      fvdl 	daddr_t old_iaddr;
    518  1.48  perseant 	ino_t ino, otail;
    519  1.30  perseant 	extern int lfs_dirvcount;
    520  1.56  perseant 	int s;
    521  1.17  perseant 
    522   1.1   mycroft 	/* Get the inode number and file system. */
    523  1.30  perseant 	vp = ap->a_pvp;
    524  1.30  perseant 	ip = VTOI(vp);
    525   1.1   mycroft 	fs = ip->i_lfs;
    526   1.1   mycroft 	ino = ip->i_number;
    527  1.34  perseant 
    528  1.48  perseant 	/* Drain of pending writes */
    529  1.56  perseant 	s = splbio();
    530  1.48  perseant 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    531  1.34  perseant 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    532  1.56  perseant 	splx(s);
    533  1.48  perseant 
    534  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    535  1.56  perseant 	lfs_seglock(fs, SEGM_PROT); /* XXX */;
    536  1.56  perseant #else
    537  1.48  perseant 	if (fs->lfs_version == 1)
    538  1.48  perseant 		lfs_seglock(fs, SEGM_PROT);
    539  1.48  perseant 	else
    540  1.48  perseant 		lockmgr(&fs->lfs_freelock, LK_EXCLUSIVE, 0);
    541  1.56  perseant #endif
    542  1.17  perseant 
    543  1.53       chs 	if (vp->v_flag & VDIROP) {
    544  1.30  perseant 		--lfs_dirvcount;
    545  1.30  perseant 		vp->v_flag &= ~VDIROP;
    546  1.30  perseant 		wakeup(&lfs_dirvcount);
    547  1.30  perseant 		lfs_vunref(vp);
    548  1.38  perseant 	}
    549  1.58      yamt 	lfs_unmark_vnode(vp);
    550  1.30  perseant 
    551  1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    552  1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    553  1.42  perseant 
    554   1.1   mycroft 	/*
    555   1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    556  1.48  perseant 	 * and link it onto the free chain.
    557   1.1   mycroft 	 */
    558   1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    559   1.1   mycroft 	old_iaddr = ifp->if_daddr;
    560   1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    561   1.1   mycroft 	++ifp->if_version;
    562  1.48  perseant 	if (fs->lfs_version == 1) {
    563  1.48  perseant 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    564  1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    565  1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    566  1.48  perseant 	} else {
    567  1.48  perseant 		ifp->if_nextfree = LFS_UNUSED_INUM;
    568  1.48  perseant 		/*
    569  1.48  perseant 		 * XXX Writing the freed node here means that it might not
    570  1.48  perseant 		 * XXX make it into the free list in the event of a crash
    571  1.48  perseant 		 * XXX (the ifile could be written before the rest of this
    572  1.48  perseant 		 * XXX completes).
    573  1.48  perseant 		 */
    574  1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    575  1.48  perseant 		LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    576  1.48  perseant 		LFS_IENTRY(ifp, fs, otail, bp);
    577  1.48  perseant 		ifp->if_nextfree = ino;
    578  1.56  perseant 		LFS_BWRITE_LOG(bp);
    579  1.48  perseant 		LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    580  1.55  perseant 		/* printf("lfs_vfree: tailfree %d -> %d\n", otail, ino); */
    581  1.48  perseant 	}
    582  1.17  perseant #ifdef DIAGNOSTIC
    583  1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    584  1.17  perseant 		panic("inode 0 freed");
    585  1.17  perseant 	}
    586  1.17  perseant #endif /* DIAGNOSTIC */
    587   1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    588  1.48  perseant 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    589  1.22  perseant #ifdef DIAGNOSTIC
    590  1.17  perseant 		if (sup->su_nbytes < DINODE_SIZE) {
    591  1.43  perseant 			printf("lfs_vfree: negative byte count"
    592  1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    593  1.48  perseant 			       dtosn(fs, old_iaddr),
    594  1.43  perseant 			       (int)DINODE_SIZE - sup->su_nbytes);
    595  1.22  perseant 			panic("lfs_vfree: negative byte count");
    596  1.17  perseant 			sup->su_nbytes = DINODE_SIZE;
    597  1.17  perseant 		}
    598  1.22  perseant #endif
    599  1.16   thorpej 		sup->su_nbytes -= DINODE_SIZE;
    600  1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    601   1.1   mycroft 	}
    602  1.17  perseant 
    603   1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    604   1.1   mycroft 	fs->lfs_fmod = 1;
    605   1.1   mycroft 	--fs->lfs_nfiles;
    606  1.17  perseant 
    607  1.56  perseant #ifdef LFS_AGGRESSIVE_SEGLOCK
    608  1.56  perseant 	lfs_segunlock(fs);
    609  1.56  perseant #else
    610  1.48  perseant 	if (fs->lfs_version == 1)
    611  1.48  perseant 		lfs_segunlock(fs);
    612  1.48  perseant 	else
    613  1.48  perseant 		lockmgr(&fs->lfs_freelock, LK_RELEASE, 0);
    614  1.56  perseant #endif
    615   1.1   mycroft 	return (0);
    616   1.1   mycroft }
    617