Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.86.10.1
      1  1.86.10.1      elad /*	$NetBSD: lfs_alloc.c,v 1.86.10.1 2006/03/08 01:39:12 elad 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.86.10.1      elad __KERNEL_RCSID(0, "$NetBSD: lfs_alloc.c,v 1.86.10.1 2006/03/08 01:39:12 elad 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.15   thorpej #include <sys/pool.h>
     85       1.50       chs #include <sys/proc.h>
     86       1.81  perseant #include <sys/tree.h>
     87        1.1   mycroft 
     88        1.1   mycroft #include <ufs/ufs/quota.h>
     89        1.1   mycroft #include <ufs/ufs/inode.h>
     90        1.1   mycroft #include <ufs/ufs/ufsmount.h>
     91        1.3  christos #include <ufs/ufs/ufs_extern.h>
     92        1.1   mycroft 
     93        1.1   mycroft #include <ufs/lfs/lfs.h>
     94        1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     95        1.1   mycroft 
     96       1.17  perseant extern struct lock ufs_hashlock;
     97       1.17  perseant 
     98  1.86.10.1      elad static int extend_ifile(struct lfs *, kauth_cred_t);
     99       1.70      yamt static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int,
    100       1.70      yamt     struct vnode **);
    101       1.44  perseant 
    102       1.44  perseant /*
    103       1.44  perseant  * Allocate a particular inode with a particular version number, freeing
    104       1.44  perseant  * any previous versions of this inode that may have gone before.
    105       1.44  perseant  * Used by the roll-forward code.
    106       1.44  perseant  *
    107       1.44  perseant  * XXX this function does not have appropriate locking to be used on a live fs;
    108       1.44  perseant  * XXX but something similar could probably be used for an "undelete" call.
    109       1.56  perseant  *
    110       1.56  perseant  * Called with the Ifile inode locked.
    111       1.44  perseant  */
    112       1.44  perseant int
    113       1.86  christos lfs_rf_valloc(struct lfs *fs, ino_t ino, int vers, struct lwp *l,
    114       1.44  perseant 	      struct vnode **vpp)
    115       1.44  perseant {
    116       1.44  perseant 	IFILE *ifp;
    117       1.48  perseant 	struct buf *bp, *cbp;
    118       1.44  perseant 	struct vnode *vp;
    119       1.44  perseant 	struct inode *ip;
    120       1.44  perseant 	ino_t tino, oldnext;
    121       1.44  perseant 	int error;
    122       1.48  perseant 	CLEANERINFO *cip;
    123       1.44  perseant 
    124       1.78  perseant 	ASSERT_SEGLOCK(fs); /* XXX it doesn't, really */
    125       1.78  perseant 
    126       1.44  perseant 	/*
    127       1.44  perseant 	 * First, just try a vget. If the version number is the one we want,
    128       1.44  perseant 	 * we don't have to do anything else.  If the version number is wrong,
    129       1.44  perseant 	 * take appropriate action.
    130       1.44  perseant 	 */
    131       1.68   thorpej 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    132       1.44  perseant 	if (error == 0) {
    133       1.76  perseant 		DLOG((DLOG_RF, "lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp));
    134       1.44  perseant 
    135       1.44  perseant 		*vpp = vp;
    136       1.44  perseant 		ip = VTOI(vp);
    137       1.83  christos 		if (ip->i_gen == vers)
    138       1.44  perseant 			return 0;
    139       1.83  christos 		else if (ip->i_gen < vers) {
    140       1.86  christos 			lfs_truncate(vp, (off_t)0, 0, NOCRED, l);
    141       1.83  christos 			ip->i_gen = ip->i_ffs1_gen = vers;
    142       1.73   mycroft 			LFS_SET_UINO(ip, IN_CHANGE | IN_UPDATE);
    143       1.44  perseant 			return 0;
    144       1.44  perseant 		} else {
    145       1.76  perseant 			DLOG((DLOG_RF, "ino %d: sought version %d, got %d\n",
    146       1.83  christos 			       ino, vers, ip->i_ffs1_gen));
    147       1.44  perseant 			vput(vp);
    148       1.44  perseant 			*vpp = NULLVP;
    149       1.44  perseant 			return EEXIST;
    150       1.44  perseant 		}
    151       1.44  perseant 	}
    152       1.44  perseant 
    153       1.44  perseant 	/*
    154       1.44  perseant 	 * The inode is not in use.  Find it on the free list.
    155       1.44  perseant 	 */
    156       1.44  perseant 	/* If the Ifile is too short to contain this inum, extend it */
    157       1.75     perry 	while (VTOI(fs->lfs_ivnode)->i_size <= (ino /
    158       1.75     perry 		fs->lfs_ifpb + fs->lfs_cleansz + fs->lfs_segtabsz)
    159       1.48  perseant 		<< fs->lfs_bshift) {
    160       1.44  perseant 		extend_ifile(fs, NOCRED);
    161       1.44  perseant 	}
    162       1.44  perseant 
    163       1.44  perseant 	LFS_IENTRY(ifp, fs, ino, bp);
    164       1.44  perseant 	oldnext = ifp->if_nextfree;
    165       1.83  christos 	ifp->if_version = vers;
    166       1.44  perseant 	brelse(bp);
    167       1.44  perseant 
    168       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &ino);
    169       1.48  perseant 	if (ino) {
    170       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, oldnext);
    171       1.44  perseant 	} else {
    172       1.48  perseant 		tino = ino;
    173       1.53       chs 		while (1) {
    174       1.44  perseant 			LFS_IENTRY(ifp, fs, tino, bp);
    175       1.44  perseant 			if (ifp->if_nextfree == ino ||
    176       1.44  perseant 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    177       1.44  perseant 				break;
    178       1.44  perseant 			tino = ifp->if_nextfree;
    179       1.44  perseant 			brelse(bp);
    180       1.44  perseant 		}
    181       1.44  perseant 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    182       1.44  perseant 			brelse(bp);
    183       1.44  perseant 			return ENOENT;
    184       1.44  perseant 		}
    185       1.44  perseant 		ifp->if_nextfree = oldnext;
    186       1.56  perseant 		LFS_BWRITE_LOG(bp);
    187       1.44  perseant 	}
    188       1.44  perseant 
    189       1.83  christos 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, vers, &vp);
    190       1.44  perseant 	if (error == 0) {
    191       1.44  perseant 		/*
    192       1.44  perseant 		 * Make it VREG so we can put blocks on it.  We will change
    193       1.44  perseant 		 * this later if it turns out to be some other kind of file.
    194       1.44  perseant 		 */
    195       1.44  perseant 		ip = VTOI(vp);
    196       1.66      fvdl 		ip->i_mode = ip->i_ffs1_mode = IFREG;
    197       1.66      fvdl 		ip->i_nlink = ip->i_ffs1_nlink = 1;
    198       1.44  perseant 		ip->i_ffs_effnlink = 1;
    199       1.44  perseant 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    200       1.44  perseant 		ip = VTOI(vp);
    201       1.44  perseant 
    202       1.76  perseant 		DLOG((DLOG_RF, "lfs_rf_valloc: ino %d vp %p\n", ino, vp));
    203       1.44  perseant 
    204       1.44  perseant 		/* The dirop-nature of this vnode is past */
    205       1.72      yamt 		lfs_unmark_vnode(vp);
    206       1.44  perseant 		(void)lfs_vunref(vp);
    207       1.78  perseant 		vp->v_flag &= ~VDIROP;
    208       1.78  perseant 		simple_lock(&fs->lfs_interlock);
    209       1.78  perseant 		simple_lock(&lfs_subsys_lock);
    210       1.44  perseant 		--lfs_dirvcount;
    211       1.78  perseant 		simple_unlock(&lfs_subsys_lock);
    212       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    213       1.78  perseant 		simple_unlock(&fs->lfs_interlock);
    214       1.44  perseant 	}
    215       1.44  perseant 	*vpp = vp;
    216       1.44  perseant 	return error;
    217       1.44  perseant }
    218       1.44  perseant 
    219       1.56  perseant /*
    220       1.65  perseant  * Add a new block to the Ifile, to accommodate future file creations.
    221       1.65  perseant  * Called with the segment lock held.
    222       1.56  perseant  */
    223       1.44  perseant static int
    224  1.86.10.1      elad extend_ifile(struct lfs *fs, kauth_cred_t cred)
    225       1.44  perseant {
    226       1.44  perseant 	struct vnode *vp;
    227       1.44  perseant 	struct inode *ip;
    228       1.44  perseant 	IFILE *ifp;
    229       1.48  perseant 	IFILE_V1 *ifp_v1;
    230       1.48  perseant 	struct buf *bp, *cbp;
    231       1.44  perseant 	int error;
    232       1.83  christos 	daddr_t i, blkno, xmax;
    233       1.44  perseant 	ino_t oldlast;
    234       1.48  perseant 	CLEANERINFO *cip;
    235       1.44  perseant 
    236       1.78  perseant 	ASSERT_SEGLOCK(fs);
    237       1.78  perseant 
    238       1.44  perseant 	vp = fs->lfs_ivnode;
    239       1.44  perseant 	ip = VTOI(vp);
    240       1.66      fvdl 	blkno = lblkno(fs, ip->i_size);
    241       1.85      yamt 	if ((error = lfs_balloc(vp, ip->i_size, fs->lfs_bsize, cred, 0,
    242       1.44  perseant 				&bp)) != 0) {
    243       1.44  perseant 		return (error);
    244       1.44  perseant 	}
    245       1.66      fvdl 	ip->i_size += fs->lfs_bsize;
    246       1.66      fvdl 	ip->i_ffs1_size = ip->i_size;
    247       1.66      fvdl 	uvm_vnp_setsize(vp, ip->i_size);
    248       1.75     perry 
    249       1.44  perseant 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    250       1.44  perseant 		fs->lfs_ifpb;
    251       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &oldlast);
    252       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, i);
    253       1.44  perseant #ifdef DIAGNOSTIC
    254       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    255       1.44  perseant 		panic("inode 0 allocated [2]");
    256       1.44  perseant #endif /* DIAGNOSTIC */
    257       1.83  christos 	xmax = i + fs->lfs_ifpb;
    258       1.48  perseant 
    259       1.53       chs 	if (fs->lfs_version == 1) {
    260       1.83  christos 		for (ifp_v1 = (IFILE_V1 *)bp->b_data; i < xmax; ++ifp_v1) {
    261       1.48  perseant 			ifp_v1->if_version = 1;
    262       1.48  perseant 			ifp_v1->if_daddr = LFS_UNUSED_DADDR;
    263       1.48  perseant 			ifp_v1->if_nextfree = ++i;
    264       1.48  perseant 		}
    265       1.48  perseant 		ifp_v1--;
    266       1.48  perseant 		ifp_v1->if_nextfree = oldlast;
    267       1.48  perseant 	} else {
    268       1.83  christos 		for (ifp = (IFILE *)bp->b_data; i < xmax; ++ifp) {
    269       1.48  perseant 			ifp->if_version = 1;
    270       1.48  perseant 			ifp->if_daddr = LFS_UNUSED_DADDR;
    271       1.48  perseant 			ifp->if_nextfree = ++i;
    272       1.48  perseant 		}
    273       1.48  perseant 		ifp--;
    274       1.48  perseant 		ifp->if_nextfree = oldlast;
    275       1.44  perseant 	}
    276       1.83  christos 	LFS_PUT_TAILFREE(fs, cip, cbp, xmax - 1);
    277       1.48  perseant 
    278       1.56  perseant 	(void) LFS_BWRITE_LOG(bp); /* Ifile */
    279       1.44  perseant 
    280       1.44  perseant 	return 0;
    281       1.44  perseant }
    282       1.44  perseant 
    283        1.1   mycroft /* Allocate a new inode. */
    284        1.1   mycroft /* ARGSUSED */
    285       1.43  perseant /* VOP_BWRITE 2i times */
    286        1.1   mycroft int
    287  1.86.10.1      elad lfs_valloc(struct vnode *pvp, int mode, kauth_cred_t cred, struct vnode **vpp)
    288        1.3  christos {
    289        1.1   mycroft 	struct lfs *fs;
    290       1.48  perseant 	struct buf *bp, *cbp;
    291        1.1   mycroft 	struct ifile *ifp;
    292        1.1   mycroft 	ino_t new_ino;
    293        1.1   mycroft 	int error;
    294       1.36  perseant 	int new_gen;
    295       1.48  perseant 	CLEANERINFO *cip;
    296        1.1   mycroft 
    297       1.85      yamt 	fs = VTOI(pvp)->i_lfs;
    298       1.38  perseant 	if (fs->lfs_ronly)
    299       1.38  perseant 		return EROFS;
    300       1.75     perry 
    301       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    302       1.78  perseant 
    303       1.56  perseant 	lfs_seglock(fs, SEGM_PROT);
    304       1.78  perseant 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    305       1.17  perseant 
    306        1.1   mycroft 	/* Get the head of the freelist. */
    307       1.48  perseant 	LFS_GET_HEADFREE(fs, cip, cbp, &new_ino);
    308       1.48  perseant 
    309       1.17  perseant #ifdef DIAGNOSTIC
    310       1.53       chs 	if (new_ino == LFS_UNUSED_INUM) {
    311       1.17  perseant #ifdef DEBUG
    312       1.17  perseant 		lfs_dump_super(fs);
    313       1.17  perseant #endif /* DEBUG */
    314       1.17  perseant 		panic("inode 0 allocated [1]");
    315       1.17  perseant 	}
    316       1.17  perseant #endif /* DIAGNOSTIC */
    317       1.76  perseant 	DLOG((DLOG_ALLOC, "lfs_valloc: allocate inode %d\n", new_ino));
    318       1.75     perry 
    319        1.1   mycroft 	/*
    320        1.1   mycroft 	 * Remove the inode from the free list and write the new start
    321        1.1   mycroft 	 * of the free list into the superblock.
    322        1.1   mycroft 	 */
    323        1.1   mycroft 	LFS_IENTRY(ifp, fs, new_ino, bp);
    324        1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    325       1.84  christos 		panic("lfs_valloc: inuse inode %llu on the free list",
    326       1.84  christos 		    (unsigned long long)new_ino);
    327       1.48  perseant 	LFS_PUT_HEADFREE(fs, cip, cbp, ifp->if_nextfree);
    328       1.76  perseant 	DLOG((DLOG_ALLOC, "lfs_valloc: headfree %d -> %d\n", new_ino,
    329       1.76  perseant 	      ifp->if_nextfree));
    330       1.48  perseant 
    331       1.37  perseant 	new_gen = ifp->if_version; /* version was updated by vfree */
    332       1.28  perseant 	brelse(bp);
    333       1.30  perseant 
    334        1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    335       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM) {
    336       1.85      yamt 		if ((error = extend_ifile(fs, cred)) != 0) {
    337       1.48  perseant 			LFS_PUT_HEADFREE(fs, cip, cbp, new_ino);
    338       1.78  perseant 			VOP_UNLOCK(fs->lfs_ivnode, 0);
    339       1.56  perseant 			lfs_segunlock(fs);
    340       1.44  perseant 			return error;
    341        1.1   mycroft 		}
    342        1.1   mycroft 	}
    343       1.17  perseant #ifdef DIAGNOSTIC
    344       1.63  perseant 	if (fs->lfs_freehd == LFS_UNUSED_INUM)
    345       1.17  perseant 		panic("inode 0 allocated [3]");
    346       1.17  perseant #endif /* DIAGNOSTIC */
    347       1.48  perseant 
    348       1.78  perseant 	/* Set superblock modified bit and increment file count. */
    349       1.78  perseant 	simple_lock(&fs->lfs_interlock);
    350       1.78  perseant 	fs->lfs_fmod = 1;
    351       1.78  perseant 	simple_unlock(&fs->lfs_interlock);
    352       1.78  perseant 	++fs->lfs_nfiles;
    353       1.78  perseant 
    354       1.78  perseant 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    355       1.56  perseant 	lfs_segunlock(fs);
    356       1.63  perseant 
    357       1.85      yamt 	return lfs_ialloc(fs, pvp, new_ino, new_gen, vpp);
    358       1.44  perseant }
    359       1.44  perseant 
    360       1.65  perseant /*
    361       1.65  perseant  * Finish allocating a new inode, given an inode and generation number.
    362       1.65  perseant  */
    363       1.44  perseant static int
    364       1.44  perseant lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    365       1.44  perseant 	   struct vnode **vpp)
    366       1.44  perseant {
    367       1.44  perseant 	struct inode *ip;
    368       1.44  perseant 	struct vnode *vp;
    369       1.40      fvdl 
    370       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    371       1.78  perseant 
    372       1.77  perseant 	vp = *vpp;
    373       1.31  perseant 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    374       1.40      fvdl 	/* Create an inode to associate with the vnode. */
    375       1.44  perseant 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    376       1.51       chs 
    377        1.1   mycroft 	ip = VTOI(vp);
    378       1.73   mycroft 	LFS_SET_UINO(ip, IN_CHANGE);
    379       1.66      fvdl 	/* on-disk structure has been zeroed out by lfs_vcreate */
    380       1.66      fvdl 	ip->i_din.ffs1_din->di_inumber = new_ino;
    381       1.51       chs 
    382       1.79  perseant 	/* Note no blocks yet */
    383       1.79  perseant 	ip->i_lfs_hiblk = -1;
    384       1.79  perseant 
    385        1.1   mycroft 	/* Set a new generation number for this inode. */
    386       1.66      fvdl 	if (new_gen) {
    387       1.66      fvdl 		ip->i_gen = new_gen;
    388       1.66      fvdl 		ip->i_ffs1_gen = new_gen;
    389       1.66      fvdl 	}
    390       1.51       chs 
    391        1.1   mycroft 	/* Insert into the inode hash table. */
    392        1.1   mycroft 	ufs_ihashins(ip);
    393       1.26  perseant 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    394       1.44  perseant 
    395       1.77  perseant 	ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, vpp);
    396       1.77  perseant 	vp = *vpp;
    397       1.44  perseant 	ip = VTOI(vp);
    398       1.57      yamt 
    399       1.57      yamt 	memset(ip->i_lfs_fragsize, 0, NDADDR * sizeof(*ip->i_lfs_fragsize));
    400       1.51       chs 
    401       1.51       chs 	uvm_vnp_setsize(vp, 0);
    402       1.72      yamt 	lfs_mark_vnode(vp);
    403       1.54       chs 	genfs_node_init(vp, &lfs_genfsops);
    404       1.38  perseant 	VREF(ip->i_devvp);
    405        1.1   mycroft 	return (0);
    406        1.1   mycroft }
    407        1.1   mycroft 
    408        1.1   mycroft /* Create a new vnode/inode pair and initialize what fields we can. */
    409       1.40      fvdl void
    410       1.48  perseant lfs_vcreate(struct mount *mp, ino_t ino, struct vnode *vp)
    411        1.1   mycroft {
    412        1.1   mycroft 	struct inode *ip;
    413       1.66      fvdl 	struct ufs1_dinode *dp;
    414        1.1   mycroft 	struct ufsmount *ump;
    415       1.80  perseant #ifdef QUOTA
    416        1.4        pk 	int i;
    417       1.80  perseant #endif
    418       1.75     perry 
    419        1.1   mycroft 	/* Get a pointer to the private mount structure. */
    420        1.1   mycroft 	ump = VFSTOUFS(mp);
    421       1.75     perry 
    422       1.78  perseant 	ASSERT_NO_SEGLOCK(ump->um_lfs);
    423       1.78  perseant 
    424        1.1   mycroft 	/* Initialize the inode. */
    425       1.15   thorpej 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    426       1.66      fvdl 	memset(ip, 0, sizeof(*ip));
    427       1.66      fvdl 	dp = pool_get(&lfs_dinode_pool, PR_WAITOK);
    428       1.66      fvdl 	memset(dp, 0, sizeof(*dp));
    429       1.63  perseant 	ip->inode_ext.lfs = pool_get(&lfs_inoext_pool, PR_WAITOK);
    430       1.74  perseant 	memset(ip->inode_ext.lfs, 0, sizeof(*ip->inode_ext.lfs));
    431       1.40      fvdl 	vp->v_data = ip;
    432       1.66      fvdl 	ip->i_din.ffs1_din = dp;
    433       1.66      fvdl 	ip->i_ump = ump;
    434       1.40      fvdl 	ip->i_vnode = vp;
    435        1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    436        1.1   mycroft 	ip->i_dev = ump->um_dev;
    437       1.66      fvdl 	ip->i_number = dp->di_inumber = ino;
    438        1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    439       1.66      fvdl 	ip->i_lfs_effnblks = 0;
    440       1.81  perseant 	SPLAY_INIT(&ip->i_lfs_lbtree);
    441       1.82  perseant 	ip->i_lfs_nbtree = 0;
    442        1.1   mycroft #ifdef QUOTA
    443        1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    444        1.1   mycroft 		ip->i_dquot[i] = NODQUOT;
    445        1.1   mycroft #endif
    446       1.76  perseant #ifdef DEBUG
    447       1.56  perseant 	if (ino == LFS_IFILE_INUM)
    448       1.56  perseant 		vp->v_vnlock->lk_wmesg = "inlock";
    449       1.56  perseant #endif
    450        1.1   mycroft }
    451        1.1   mycroft 
    452        1.1   mycroft /* Free an inode. */
    453        1.1   mycroft /* ARGUSED */
    454       1.43  perseant /* VOP_BWRITE 2i times */
    455        1.1   mycroft int
    456       1.85      yamt lfs_vfree(struct vnode *vp, ino_t ino, int mode)
    457        1.3  christos {
    458        1.1   mycroft 	SEGUSE *sup;
    459       1.48  perseant 	CLEANERINFO *cip;
    460       1.48  perseant 	struct buf *cbp, *bp;
    461        1.1   mycroft 	struct ifile *ifp;
    462        1.1   mycroft 	struct inode *ip;
    463        1.1   mycroft 	struct lfs *fs;
    464       1.59      fvdl 	daddr_t old_iaddr;
    465       1.85      yamt 	ino_t otail;
    466       1.56  perseant 	int s;
    467       1.75     perry 
    468        1.1   mycroft 	/* Get the inode number and file system. */
    469       1.30  perseant 	ip = VTOI(vp);
    470        1.1   mycroft 	fs = ip->i_lfs;
    471        1.1   mycroft 	ino = ip->i_number;
    472       1.34  perseant 
    473       1.78  perseant 	ASSERT_NO_SEGLOCK(fs);
    474       1.78  perseant 
    475       1.48  perseant 	/* Drain of pending writes */
    476       1.78  perseant 	simple_lock(&vp->v_interlock);
    477       1.56  perseant 	s = splbio();
    478       1.48  perseant 	if (fs->lfs_version > 1 && WRITEINPROG(vp))
    479       1.78  perseant 		ltsleep(vp, (PRIBIO+1), "lfs_vfree", 0, &vp->v_interlock);
    480       1.56  perseant 	splx(s);
    481       1.78  perseant 	simple_unlock(&vp->v_interlock);
    482       1.48  perseant 
    483       1.63  perseant 	lfs_seglock(fs, SEGM_PROT);
    484       1.78  perseant 	vn_lock(fs->lfs_ivnode, LK_EXCLUSIVE);
    485       1.75     perry 
    486       1.72      yamt 	lfs_unmark_vnode(vp);
    487       1.53       chs 	if (vp->v_flag & VDIROP) {
    488       1.78  perseant 		vp->v_flag &= ~VDIROP;
    489       1.78  perseant 		simple_lock(&fs->lfs_interlock);
    490       1.78  perseant 		simple_lock(&lfs_subsys_lock);
    491       1.30  perseant 		--lfs_dirvcount;
    492       1.78  perseant 		simple_unlock(&lfs_subsys_lock);
    493       1.63  perseant 		TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
    494       1.78  perseant 		simple_unlock(&fs->lfs_interlock);
    495       1.30  perseant 		wakeup(&lfs_dirvcount);
    496       1.30  perseant 		lfs_vunref(vp);
    497       1.38  perseant 	}
    498       1.30  perseant 
    499       1.42  perseant 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    500       1.33  perseant 	ip->i_flag &= ~IN_ALLMOD;
    501       1.42  perseant 
    502        1.1   mycroft 	/*
    503        1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    504       1.48  perseant 	 * and link it onto the free chain.
    505        1.1   mycroft 	 */
    506        1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    507        1.1   mycroft 	old_iaddr = ifp->if_daddr;
    508        1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    509        1.1   mycroft 	++ifp->if_version;
    510       1.48  perseant 	if (fs->lfs_version == 1) {
    511       1.48  perseant 		LFS_GET_HEADFREE(fs, cip, cbp, &(ifp->if_nextfree));
    512       1.48  perseant 		LFS_PUT_HEADFREE(fs, cip, cbp, ino);
    513       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    514       1.48  perseant 	} else {
    515       1.48  perseant 		ifp->if_nextfree = LFS_UNUSED_INUM;
    516       1.48  perseant 		/*
    517       1.48  perseant 		 * XXX Writing the freed node here means that it might not
    518       1.48  perseant 		 * XXX make it into the free list in the event of a crash
    519       1.48  perseant 		 * XXX (the ifile could be written before the rest of this
    520       1.48  perseant 		 * XXX completes).
    521       1.48  perseant 		 */
    522       1.56  perseant 		(void) LFS_BWRITE_LOG(bp); /* Ifile */
    523       1.48  perseant 		LFS_GET_TAILFREE(fs, cip, cbp, &otail);
    524       1.48  perseant 		LFS_IENTRY(ifp, fs, otail, bp);
    525       1.48  perseant 		ifp->if_nextfree = ino;
    526       1.56  perseant 		LFS_BWRITE_LOG(bp);
    527       1.48  perseant 		LFS_PUT_TAILFREE(fs, cip, cbp, ino);
    528       1.76  perseant 		DLOG((DLOG_ALLOC, "lfs_vfree: tailfree %d -> %d\n", otail,
    529       1.76  perseant 		      ino));
    530       1.48  perseant 	}
    531       1.17  perseant #ifdef DIAGNOSTIC
    532       1.53       chs 	if (ino == LFS_UNUSED_INUM) {
    533       1.17  perseant 		panic("inode 0 freed");
    534       1.17  perseant 	}
    535       1.17  perseant #endif /* DIAGNOSTIC */
    536        1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    537       1.48  perseant 		LFS_SEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp);
    538       1.22  perseant #ifdef DIAGNOSTIC
    539       1.66      fvdl 		if (sup->su_nbytes < sizeof (struct ufs1_dinode)) {
    540       1.43  perseant 			printf("lfs_vfree: negative byte count"
    541       1.62      yamt 			       " (segment %" PRIu32 " short by %d)\n",
    542       1.48  perseant 			       dtosn(fs, old_iaddr),
    543       1.66      fvdl 			       (int)sizeof (struct ufs1_dinode) -
    544       1.66      fvdl 				    sup->su_nbytes);
    545       1.22  perseant 			panic("lfs_vfree: negative byte count");
    546       1.66      fvdl 			sup->su_nbytes = sizeof (struct ufs1_dinode);
    547       1.17  perseant 		}
    548       1.22  perseant #endif
    549       1.66      fvdl 		sup->su_nbytes -= sizeof (struct ufs1_dinode);
    550       1.63  perseant 		LFS_WRITESEGENTRY(sup, fs, dtosn(fs, old_iaddr), bp); /* Ifile */
    551        1.1   mycroft 	}
    552       1.75     perry 
    553        1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    554       1.78  perseant 	simple_lock(&fs->lfs_interlock);
    555        1.1   mycroft 	fs->lfs_fmod = 1;
    556       1.78  perseant 	simple_unlock(&fs->lfs_interlock);
    557        1.1   mycroft 	--fs->lfs_nfiles;
    558       1.75     perry 
    559       1.78  perseant 	VOP_UNLOCK(fs->lfs_ivnode, 0);
    560       1.56  perseant 	lfs_segunlock(fs);
    561       1.63  perseant 
    562        1.1   mycroft 	return (0);
    563        1.1   mycroft }
    564