Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.25.2.2
      1  1.25.2.2    bouyer /*	$NetBSD: lfs_alloc.c,v 1.25.2.2 2000/12/08 09:20:13 bouyer Exp $	*/
      2       1.2       cgd 
      3      1.17  perseant /*-
      4  1.25.2.1    bouyer  * 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.12    scottr 
     73      1.13    scottr #if defined(_KERNEL) && !defined(_LKM)
     74      1.12    scottr #include "opt_quota.h"
     75      1.13    scottr #endif
     76       1.1   mycroft 
     77       1.1   mycroft #include <sys/param.h>
     78       1.3  christos #include <sys/systm.h>
     79       1.1   mycroft #include <sys/kernel.h>
     80       1.1   mycroft #include <sys/buf.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.1   mycroft #include <sys/malloc.h>
     85      1.15   thorpej #include <sys/pool.h>
     86       1.1   mycroft 
     87       1.1   mycroft #include <ufs/ufs/quota.h>
     88       1.1   mycroft #include <ufs/ufs/inode.h>
     89       1.1   mycroft #include <ufs/ufs/ufsmount.h>
     90       1.3  christos #include <ufs/ufs/ufs_extern.h>
     91       1.1   mycroft 
     92       1.1   mycroft #include <ufs/lfs/lfs.h>
     93       1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     94       1.1   mycroft 
     95  1.25.2.1    bouyer extern int lfs_dirvcount;
     96      1.17  perseant extern struct lock ufs_hashlock;
     97      1.17  perseant 
     98  1.25.2.2    bouyer static int extend_ifile(struct lfs *, struct ucred *);
     99  1.25.2.2    bouyer static int lfs_ialloc(struct lfs *, struct vnode *, ino_t, int, struct vnode **);
    100  1.25.2.2    bouyer 
    101  1.25.2.2    bouyer /*
    102  1.25.2.2    bouyer  * Allocate a particular inode with a particular version number, freeing
    103  1.25.2.2    bouyer  * any previous versions of this inode that may have gone before.
    104  1.25.2.2    bouyer  * Used by the roll-forward code.
    105  1.25.2.2    bouyer  *
    106  1.25.2.2    bouyer  * XXX this function does not have appropriate locking to be used on a live fs;
    107  1.25.2.2    bouyer  * XXX but something similar could probably be used for an "undelete" call.
    108  1.25.2.2    bouyer  */
    109  1.25.2.2    bouyer int
    110  1.25.2.2    bouyer lfs_rf_valloc(struct lfs *fs, ino_t ino, int version, struct proc *p,
    111  1.25.2.2    bouyer 	      struct vnode **vpp)
    112  1.25.2.2    bouyer {
    113  1.25.2.2    bouyer 	IFILE *ifp;
    114  1.25.2.2    bouyer 	struct buf *bp;
    115  1.25.2.2    bouyer 	struct vnode *vp;
    116  1.25.2.2    bouyer 	struct inode *ip;
    117  1.25.2.2    bouyer 	ino_t tino, oldnext;
    118  1.25.2.2    bouyer 	int error;
    119  1.25.2.2    bouyer 
    120  1.25.2.2    bouyer 	/*
    121  1.25.2.2    bouyer 	 * First, just try a vget. If the version number is the one we want,
    122  1.25.2.2    bouyer 	 * we don't have to do anything else.  If the version number is wrong,
    123  1.25.2.2    bouyer 	 * take appropriate action.
    124  1.25.2.2    bouyer 	 */
    125  1.25.2.2    bouyer 	error = VFS_VGET(fs->lfs_ivnode->v_mount, ino, &vp);
    126  1.25.2.2    bouyer 	if (error == 0) {
    127  1.25.2.2    bouyer 		/* printf("lfs_rf_valloc[1]: ino %d vp %p\n", ino, vp); */
    128  1.25.2.2    bouyer 
    129  1.25.2.2    bouyer 		*vpp = vp;
    130  1.25.2.2    bouyer 		ip = VTOI(vp);
    131  1.25.2.2    bouyer 		if (ip->i_ffs_gen == version)
    132  1.25.2.2    bouyer 			return 0;
    133  1.25.2.2    bouyer 		else if (ip->i_ffs_gen < version) {
    134  1.25.2.2    bouyer 			VOP_TRUNCATE(vp, (off_t)0, 0, NOCRED, p);
    135  1.25.2.2    bouyer 			ip->i_ffs_gen = version;
    136  1.25.2.2    bouyer 			LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    137  1.25.2.2    bouyer 			return 0;
    138  1.25.2.2    bouyer 		} else {
    139  1.25.2.2    bouyer 			/* printf("ino %d: asked for version %d but got %d\n",
    140  1.25.2.2    bouyer 			       ino, version, ip->i_ffs_gen); */
    141  1.25.2.2    bouyer 			vput(vp);
    142  1.25.2.2    bouyer 			*vpp = NULLVP;
    143  1.25.2.2    bouyer 			return EEXIST;
    144  1.25.2.2    bouyer 		}
    145  1.25.2.2    bouyer 	}
    146  1.25.2.2    bouyer 
    147  1.25.2.2    bouyer 	/*
    148  1.25.2.2    bouyer 	 * The inode is not in use.  Find it on the free list.
    149  1.25.2.2    bouyer 	 */
    150  1.25.2.2    bouyer 	/* If the Ifile is too short to contain this inum, extend it */
    151  1.25.2.2    bouyer 	while (VTOI(fs->lfs_ivnode)->i_ffs_size <=
    152  1.25.2.2    bouyer 	       dbtob(fsbtodb(fs, ino / fs->lfs_ifpb + fs->lfs_cleansz +
    153  1.25.2.2    bouyer 			     fs->lfs_segtabsz))) {
    154  1.25.2.2    bouyer 		extend_ifile(fs, NOCRED);
    155  1.25.2.2    bouyer 	}
    156  1.25.2.2    bouyer 
    157  1.25.2.2    bouyer 	LFS_IENTRY(ifp, fs, ino, bp);
    158  1.25.2.2    bouyer 	oldnext = ifp->if_nextfree;
    159  1.25.2.2    bouyer 	ifp->if_version = version;
    160  1.25.2.2    bouyer 	brelse(bp);
    161  1.25.2.2    bouyer 
    162  1.25.2.2    bouyer 	if (ino == fs->lfs_free) {
    163  1.25.2.2    bouyer 		fs->lfs_free = oldnext;
    164  1.25.2.2    bouyer 	} else {
    165  1.25.2.2    bouyer 		tino = fs->lfs_free;
    166  1.25.2.2    bouyer 		while(1) {
    167  1.25.2.2    bouyer 			LFS_IENTRY(ifp, fs, tino, bp);
    168  1.25.2.2    bouyer 			if (ifp->if_nextfree == ino ||
    169  1.25.2.2    bouyer 			    ifp->if_nextfree == LFS_UNUSED_INUM)
    170  1.25.2.2    bouyer 				break;
    171  1.25.2.2    bouyer 			tino = ifp->if_nextfree;
    172  1.25.2.2    bouyer 			brelse(bp);
    173  1.25.2.2    bouyer 		}
    174  1.25.2.2    bouyer 		if (ifp->if_nextfree == LFS_UNUSED_INUM) {
    175  1.25.2.2    bouyer 			brelse(bp);
    176  1.25.2.2    bouyer 			return ENOENT;
    177  1.25.2.2    bouyer 		}
    178  1.25.2.2    bouyer 		ifp->if_nextfree = oldnext;
    179  1.25.2.2    bouyer 		VOP_BWRITE(bp);
    180  1.25.2.2    bouyer 	}
    181  1.25.2.2    bouyer 
    182  1.25.2.2    bouyer 	error = lfs_ialloc(fs, fs->lfs_ivnode, ino, version, &vp);
    183  1.25.2.2    bouyer 	if (error == 0) {
    184  1.25.2.2    bouyer 		/*
    185  1.25.2.2    bouyer 		 * Make it VREG so we can put blocks on it.  We will change
    186  1.25.2.2    bouyer 		 * this later if it turns out to be some other kind of file.
    187  1.25.2.2    bouyer 		 */
    188  1.25.2.2    bouyer 		ip = VTOI(vp);
    189  1.25.2.2    bouyer 		ip->i_ffs_mode = IFREG;
    190  1.25.2.2    bouyer 		ip->i_ffs_nlink = 1;
    191  1.25.2.2    bouyer 		ip->i_ffs_effnlink = 1;
    192  1.25.2.2    bouyer 		ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    193  1.25.2.2    bouyer 		ip = VTOI(vp);
    194  1.25.2.2    bouyer 
    195  1.25.2.2    bouyer 		/* printf("lfs_rf_valloc: ino %d vp %p\n", ino, vp); */
    196  1.25.2.2    bouyer 
    197  1.25.2.2    bouyer 		/* The dirop-nature of this vnode is past */
    198  1.25.2.2    bouyer 		(void)lfs_vunref(vp);
    199  1.25.2.2    bouyer 		--lfs_dirvcount;
    200  1.25.2.2    bouyer 		vp->v_flag &= ~VDIROP;
    201  1.25.2.2    bouyer 		--fs->lfs_nadirop;
    202  1.25.2.2    bouyer 		ip->i_flag &= ~IN_ADIROP;
    203  1.25.2.2    bouyer 	}
    204  1.25.2.2    bouyer 	*vpp = vp;
    205  1.25.2.2    bouyer 	return error;
    206  1.25.2.2    bouyer }
    207  1.25.2.2    bouyer 
    208  1.25.2.2    bouyer static int
    209  1.25.2.2    bouyer extend_ifile(struct lfs *fs, struct ucred *cred)
    210  1.25.2.2    bouyer {
    211  1.25.2.2    bouyer 	struct vnode *vp;
    212  1.25.2.2    bouyer 	struct inode *ip;
    213  1.25.2.2    bouyer 	IFILE *ifp;
    214  1.25.2.2    bouyer 	struct buf *bp;
    215  1.25.2.2    bouyer 	int error;
    216  1.25.2.2    bouyer 	ufs_daddr_t i, blkno, max;
    217  1.25.2.2    bouyer 	ino_t oldlast;
    218  1.25.2.2    bouyer 
    219  1.25.2.2    bouyer 	vp = fs->lfs_ivnode;
    220  1.25.2.2    bouyer 	(void)lfs_vref(vp);
    221  1.25.2.2    bouyer 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    222  1.25.2.2    bouyer 	ip = VTOI(vp);
    223  1.25.2.2    bouyer 	blkno = lblkno(fs, ip->i_ffs_size);
    224  1.25.2.2    bouyer 	if ((error = VOP_BALLOC(vp, ip->i_ffs_size, fs->lfs_bsize, cred, 0,
    225  1.25.2.2    bouyer 				&bp)) != 0) {
    226  1.25.2.2    bouyer 		VOP_UNLOCK(vp, 0);
    227  1.25.2.2    bouyer 		lfs_vunref(vp);
    228  1.25.2.2    bouyer 		return (error);
    229  1.25.2.2    bouyer 	}
    230  1.25.2.2    bouyer 	ip->i_ffs_size += fs->lfs_bsize;
    231  1.25.2.2    bouyer 	uvm_vnp_setsize(vp, ip->i_ffs_size);
    232  1.25.2.2    bouyer 	VOP_UNLOCK(vp, 0);
    233  1.25.2.2    bouyer 
    234  1.25.2.2    bouyer 	i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    235  1.25.2.2    bouyer 		fs->lfs_ifpb;
    236  1.25.2.2    bouyer 	oldlast = fs->lfs_free;
    237  1.25.2.2    bouyer 	fs->lfs_free = i;
    238  1.25.2.2    bouyer #ifdef DIAGNOSTIC
    239  1.25.2.2    bouyer 	if(fs->lfs_free == LFS_UNUSED_INUM)
    240  1.25.2.2    bouyer 		panic("inode 0 allocated [2]");
    241  1.25.2.2    bouyer #endif /* DIAGNOSTIC */
    242  1.25.2.2    bouyer 	max = i + fs->lfs_ifpb;
    243  1.25.2.2    bouyer 	/* printf("extend ifile for ino %d--%d\n", i, max); */
    244  1.25.2.2    bouyer 	for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    245  1.25.2.2    bouyer 		ifp->if_version = 1;
    246  1.25.2.2    bouyer 		ifp->if_daddr = LFS_UNUSED_DADDR;
    247  1.25.2.2    bouyer 		ifp->if_nextfree = ++i;
    248  1.25.2.2    bouyer 	}
    249  1.25.2.2    bouyer 	ifp--;
    250  1.25.2.2    bouyer 	ifp->if_nextfree = oldlast;
    251  1.25.2.2    bouyer 	(void) VOP_BWRITE(bp); /* Ifile */
    252  1.25.2.2    bouyer 	lfs_vunref(vp);
    253  1.25.2.2    bouyer 
    254  1.25.2.2    bouyer 	return 0;
    255  1.25.2.2    bouyer }
    256  1.25.2.2    bouyer 
    257       1.1   mycroft /* Allocate a new inode. */
    258       1.1   mycroft /* ARGSUSED */
    259  1.25.2.1    bouyer /* VOP_BWRITE 2i times */
    260       1.1   mycroft int
    261       1.3  christos lfs_valloc(v)
    262       1.3  christos 	void *v;
    263       1.3  christos {
    264       1.1   mycroft 	struct vop_valloc_args /* {
    265      1.17  perseant 				  struct vnode *a_pvp;
    266      1.17  perseant 				  int a_mode;
    267      1.17  perseant 				  struct ucred *a_cred;
    268      1.17  perseant 				  struct vnode **a_vpp;
    269      1.17  perseant 				  } */ *ap = v;
    270       1.1   mycroft 	struct lfs *fs;
    271       1.1   mycroft 	struct buf *bp;
    272       1.1   mycroft 	struct ifile *ifp;
    273       1.1   mycroft 	ino_t new_ino;
    274       1.1   mycroft 	int error;
    275  1.25.2.1    bouyer 	int new_gen;
    276       1.1   mycroft 
    277      1.17  perseant 	fs = VTOI(ap->a_pvp)->i_lfs;
    278  1.25.2.1    bouyer 	if (fs->lfs_ronly)
    279  1.25.2.1    bouyer 		return EROFS;
    280  1.25.2.1    bouyer 	*ap->a_vpp = NULL;
    281      1.17  perseant 
    282      1.17  perseant 	/*
    283  1.25.2.1    bouyer 	 * Use lfs_seglock here, instead of fs->lfs_freelock, to ensure that
    284  1.25.2.1    bouyer 	 * the free list is not changed in between the time that the ifile
    285  1.25.2.1    bouyer 	 * blocks are written to disk and the time that the superblock is
    286  1.25.2.1    bouyer 	 * written to disk.
    287  1.25.2.1    bouyer 	 *
    288  1.25.2.1    bouyer 	 * XXX this sucks.  We should instead encode the head of the free
    289  1.25.2.2    bouyer 	 * list into the CLEANERINFO block of the Ifile. [XXX v2]
    290      1.17  perseant 	 */
    291  1.25.2.1    bouyer 	lfs_seglock(fs, SEGM_PROT);
    292      1.17  perseant 
    293       1.1   mycroft 	/* Get the head of the freelist. */
    294       1.1   mycroft 	new_ino = fs->lfs_free;
    295      1.17  perseant #ifdef DIAGNOSTIC
    296      1.17  perseant 	if(new_ino == LFS_UNUSED_INUM) {
    297      1.17  perseant #ifdef DEBUG
    298      1.17  perseant 		lfs_dump_super(fs);
    299      1.17  perseant #endif /* DEBUG */
    300      1.17  perseant 		panic("inode 0 allocated [1]");
    301      1.17  perseant 	}
    302      1.17  perseant #endif /* DIAGNOSTIC */
    303       1.1   mycroft #ifdef ALLOCPRINT
    304  1.25.2.2    bouyer 	printf("lfs_valloc: allocate inode %d\n", new_ino);
    305       1.1   mycroft #endif
    306      1.17  perseant 
    307       1.1   mycroft 	/*
    308       1.1   mycroft 	 * Remove the inode from the free list and write the new start
    309       1.1   mycroft 	 * of the free list into the superblock.
    310       1.1   mycroft 	 */
    311       1.1   mycroft 	LFS_IENTRY(ifp, fs, new_ino, bp);
    312       1.1   mycroft 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
    313  1.25.2.2    bouyer 		panic("lfs_valloc: inuse inode %d on the free list", new_ino);
    314       1.1   mycroft 	fs->lfs_free = ifp->if_nextfree;
    315  1.25.2.1    bouyer 	new_gen = ifp->if_version; /* version was updated by vfree */
    316       1.1   mycroft 	brelse(bp);
    317  1.25.2.1    bouyer 
    318       1.1   mycroft 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    319       1.1   mycroft 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    320  1.25.2.2    bouyer 		if ((error = extend_ifile(fs, ap->a_cred)) != 0) {
    321  1.25.2.1    bouyer 			fs->lfs_free = new_ino;
    322  1.25.2.2    bouyer 			lfs_segunlock(fs);
    323  1.25.2.2    bouyer 			return error;
    324       1.1   mycroft 		}
    325       1.1   mycroft 	}
    326      1.17  perseant #ifdef DIAGNOSTIC
    327      1.17  perseant 	if(fs->lfs_free == LFS_UNUSED_INUM)
    328      1.17  perseant 		panic("inode 0 allocated [3]");
    329      1.17  perseant #endif /* DIAGNOSTIC */
    330      1.17  perseant 
    331  1.25.2.1    bouyer 	lfs_segunlock(fs);
    332  1.25.2.1    bouyer 
    333  1.25.2.2    bouyer 	return lfs_ialloc(fs, ap->a_pvp, new_ino, new_gen, ap->a_vpp);
    334  1.25.2.2    bouyer }
    335  1.25.2.2    bouyer 
    336  1.25.2.2    bouyer static int
    337  1.25.2.2    bouyer lfs_ialloc(struct lfs *fs, struct vnode *pvp, ino_t new_ino, int new_gen,
    338  1.25.2.2    bouyer 	   struct vnode **vpp)
    339  1.25.2.2    bouyer {
    340  1.25.2.2    bouyer 	struct inode *ip;
    341  1.25.2.2    bouyer 	struct vnode *vp;
    342  1.25.2.2    bouyer 	IFILE *ifp;
    343  1.25.2.2    bouyer 	struct buf *bp;
    344  1.25.2.2    bouyer 	int error;
    345  1.25.2.2    bouyer 
    346  1.25.2.2    bouyer 	error = getnewvnode(VT_LFS, pvp->v_mount, lfs_vnodeop_p, &vp);
    347  1.25.2.2    bouyer 	/* printf("lfs_ialloc: ino %d vp %p error %d\n", new_ino, vp, error);*/
    348  1.25.2.2    bouyer 	if (error)
    349  1.25.2.1    bouyer 		goto errout;
    350  1.25.2.1    bouyer 
    351  1.25.2.1    bouyer 	lockmgr(&ufs_hashlock, LK_EXCLUSIVE, 0);
    352  1.25.2.1    bouyer 	/* Create an inode to associate with the vnode. */
    353  1.25.2.2    bouyer 	lfs_vcreate(pvp->v_mount, new_ino, vp);
    354      1.17  perseant 
    355       1.1   mycroft 	ip = VTOI(vp);
    356       1.1   mycroft 	/* Zero out the direct and indirect block addresses. */
    357      1.16   thorpej 	bzero(&ip->i_din, sizeof(ip->i_din));
    358       1.8    bouyer 	ip->i_din.ffs_din.di_inumber = new_ino;
    359      1.17  perseant 
    360       1.1   mycroft 	/* Set a new generation number for this inode. */
    361  1.25.2.2    bouyer 	if (new_gen)
    362  1.25.2.2    bouyer 		ip->i_ffs_gen = new_gen;
    363      1.17  perseant 
    364       1.1   mycroft 	/* Insert into the inode hash table. */
    365       1.1   mycroft 	ufs_ihashins(ip);
    366  1.25.2.1    bouyer 	lockmgr(&ufs_hashlock, LK_RELEASE, 0);
    367  1.25.2.2    bouyer 
    368      1.14  sommerfe 	error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    369  1.25.2.2    bouyer 	ip = VTOI(vp);
    370       1.3  christos 	if (error) {
    371       1.1   mycroft 		vput(vp);
    372  1.25.2.1    bouyer 		goto errout;
    373       1.1   mycroft 	}
    374  1.25.2.2    bouyer 	/* printf("lfs_ialloc[2]: ino %d vp %p\n", new_ino, vp);*/
    375      1.17  perseant 
    376  1.25.2.2    bouyer 	*vpp = vp;
    377  1.25.2.1    bouyer #if 1
    378      1.17  perseant 	if(!(vp->v_flag & VDIROP)) {
    379  1.25.2.1    bouyer 		(void)lfs_vref(vp);
    380  1.25.2.1    bouyer 		++lfs_dirvcount;
    381      1.17  perseant 	}
    382       1.1   mycroft 	vp->v_flag |= VDIROP;
    383      1.17  perseant 
    384  1.25.2.1    bouyer 	if(!(ip->i_flag & IN_ADIROP))
    385  1.25.2.1    bouyer 		++fs->lfs_nadirop;
    386  1.25.2.1    bouyer 	ip->i_flag |= IN_ADIROP;
    387  1.25.2.1    bouyer #endif
    388  1.25.2.1    bouyer 	VREF(ip->i_devvp);
    389       1.1   mycroft 	/* Set superblock modified bit and increment file count. */
    390       1.1   mycroft 	fs->lfs_fmod = 1;
    391       1.1   mycroft 	++fs->lfs_nfiles;
    392       1.1   mycroft 	return (0);
    393  1.25.2.1    bouyer 
    394  1.25.2.1    bouyer     errout:
    395  1.25.2.1    bouyer 	/*
    396  1.25.2.1    bouyer 	 * Put the new inum back on the free list.
    397  1.25.2.1    bouyer 	 */
    398  1.25.2.1    bouyer 	LFS_IENTRY(ifp, fs, new_ino, bp);
    399  1.25.2.1    bouyer 	ifp->if_daddr = LFS_UNUSED_DADDR;
    400  1.25.2.1    bouyer 	ifp->if_nextfree = fs->lfs_free;
    401  1.25.2.1    bouyer 	fs->lfs_free = new_ino;
    402  1.25.2.1    bouyer 	(void) VOP_BWRITE(bp); /* Ifile */
    403  1.25.2.1    bouyer 
    404  1.25.2.2    bouyer 	*vpp = NULLVP;
    405  1.25.2.1    bouyer 	return (error);
    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.25.2.1    bouyer void
    410  1.25.2.1    bouyer lfs_vcreate(mp, ino, vp)
    411       1.1   mycroft 	struct mount *mp;
    412       1.1   mycroft 	ino_t ino;
    413  1.25.2.1    bouyer 	struct vnode *vp;
    414       1.1   mycroft {
    415       1.1   mycroft 	struct inode *ip;
    416       1.1   mycroft 	struct ufsmount *ump;
    417       1.4        pk #ifdef QUOTA
    418       1.4        pk 	int i;
    419       1.4        pk #endif
    420      1.17  perseant 
    421       1.1   mycroft 	/* Get a pointer to the private mount structure. */
    422       1.1   mycroft 	ump = VFSTOUFS(mp);
    423      1.17  perseant 
    424       1.1   mycroft 	/* Initialize the inode. */
    425      1.15   thorpej 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    426  1.25.2.1    bouyer 	vp->v_data = ip;
    427  1.25.2.1    bouyer 	ip->i_vnode = vp;
    428       1.1   mycroft 	ip->i_devvp = ump->um_devvp;
    429       1.1   mycroft 	ip->i_dev = ump->um_dev;
    430       1.8    bouyer 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    431       1.1   mycroft 	ip->i_lfs = ump->um_lfs;
    432       1.1   mycroft #ifdef QUOTA
    433       1.1   mycroft 	for (i = 0; i < MAXQUOTAS; i++)
    434       1.1   mycroft 		ip->i_dquot[i] = NODQUOT;
    435       1.1   mycroft #endif
    436       1.1   mycroft 	ip->i_lockf = 0;
    437       1.1   mycroft 	ip->i_diroff = 0;
    438       1.8    bouyer 	ip->i_ffs_mode = 0;
    439       1.8    bouyer 	ip->i_ffs_size = 0;
    440       1.8    bouyer 	ip->i_ffs_blocks = 0;
    441  1.25.2.1    bouyer 	ip->i_lfs_effnblks = 0;
    442  1.25.2.1    bouyer 	ip->i_flag = 0;
    443  1.25.2.2    bouyer 	LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED);
    444       1.1   mycroft }
    445       1.1   mycroft 
    446       1.1   mycroft /* Free an inode. */
    447       1.1   mycroft /* ARGUSED */
    448  1.25.2.1    bouyer /* VOP_BWRITE 2i times */
    449       1.1   mycroft int
    450       1.3  christos lfs_vfree(v)
    451       1.3  christos 	void *v;
    452       1.3  christos {
    453       1.1   mycroft 	struct vop_vfree_args /* {
    454      1.17  perseant 				 struct vnode *a_pvp;
    455      1.17  perseant 				 ino_t a_ino;
    456      1.17  perseant 				 int a_mode;
    457      1.17  perseant 				 } */ *ap = v;
    458       1.1   mycroft 	SEGUSE *sup;
    459       1.1   mycroft 	struct buf *bp;
    460       1.1   mycroft 	struct ifile *ifp;
    461       1.1   mycroft 	struct inode *ip;
    462  1.25.2.1    bouyer 	struct vnode *vp;
    463       1.1   mycroft 	struct lfs *fs;
    464      1.11      fvdl 	ufs_daddr_t old_iaddr;
    465       1.1   mycroft 	ino_t ino;
    466  1.25.2.1    bouyer 	extern int lfs_dirvcount;
    467      1.17  perseant 
    468       1.1   mycroft 	/* Get the inode number and file system. */
    469  1.25.2.1    bouyer 	vp = ap->a_pvp;
    470  1.25.2.1    bouyer 	ip = VTOI(vp);
    471       1.1   mycroft 	fs = ip->i_lfs;
    472       1.1   mycroft 	ino = ip->i_number;
    473  1.25.2.1    bouyer 
    474  1.25.2.1    bouyer #if 0
    475  1.25.2.1    bouyer 	/*
    476  1.25.2.1    bouyer 	 * Right now this is unnecessary since we take the seglock.
    477  1.25.2.1    bouyer 	 * But if the seglock is no longer necessary (e.g. we put the
    478  1.25.2.1    bouyer 	 * head of the free list into the Ifile) we will need to drain
    479  1.25.2.1    bouyer 	 * this vnode of any pending writes.
    480  1.25.2.1    bouyer 	 */
    481  1.25.2.1    bouyer 	if (WRITEINPROG(vp))
    482  1.25.2.1    bouyer 		tsleep(vp, (PRIBIO+1), "lfs_vfree", 0);
    483  1.25.2.1    bouyer #endif
    484  1.25.2.1    bouyer 	lfs_seglock(fs, SEGM_PROT);
    485      1.17  perseant 
    486  1.25.2.1    bouyer 	if(vp->v_flag & VDIROP) {
    487  1.25.2.1    bouyer 		--lfs_dirvcount;
    488  1.25.2.1    bouyer 		vp->v_flag &= ~VDIROP;
    489  1.25.2.1    bouyer 		wakeup(&lfs_dirvcount);
    490  1.25.2.1    bouyer 		lfs_vunref(vp);
    491       1.1   mycroft 	}
    492  1.25.2.1    bouyer 	if (ip->i_flag & IN_ADIROP) {
    493  1.25.2.1    bouyer 		--fs->lfs_nadirop;
    494  1.25.2.1    bouyer 		ip->i_flag &= ~IN_ADIROP;
    495      1.17  perseant 	}
    496  1.25.2.1    bouyer 
    497  1.25.2.1    bouyer 	LFS_CLR_UINO(ip, IN_ACCESSED|IN_CLEANING|IN_MODIFIED);
    498  1.25.2.1    bouyer 	ip->i_flag &= ~IN_ALLMOD;
    499  1.25.2.1    bouyer 
    500       1.1   mycroft 	/*
    501       1.1   mycroft 	 * Set the ifile's inode entry to unused, increment its version number
    502       1.1   mycroft 	 * and link it into the free chain.
    503       1.1   mycroft 	 */
    504       1.1   mycroft 	LFS_IENTRY(ifp, fs, ino, bp);
    505       1.1   mycroft 	old_iaddr = ifp->if_daddr;
    506       1.1   mycroft 	ifp->if_daddr = LFS_UNUSED_DADDR;
    507       1.1   mycroft 	++ifp->if_version;
    508       1.1   mycroft 	ifp->if_nextfree = fs->lfs_free;
    509       1.1   mycroft 	fs->lfs_free = ino;
    510  1.25.2.1    bouyer 	(void) VOP_BWRITE(bp); /* Ifile */
    511      1.17  perseant #ifdef DIAGNOSTIC
    512      1.17  perseant 	if(fs->lfs_free == LFS_UNUSED_INUM) {
    513      1.17  perseant 		panic("inode 0 freed");
    514      1.17  perseant 	}
    515      1.17  perseant #endif /* DIAGNOSTIC */
    516       1.1   mycroft 	if (old_iaddr != LFS_UNUSED_DADDR) {
    517       1.1   mycroft 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    518      1.22  perseant #ifdef DIAGNOSTIC
    519      1.17  perseant 		if (sup->su_nbytes < DINODE_SIZE) {
    520  1.25.2.1    bouyer 			printf("lfs_vfree: negative byte count"
    521  1.25.2.1    bouyer 			       " (segment %d short by %d)\n",
    522  1.25.2.1    bouyer 			       datosn(fs, old_iaddr),
    523  1.25.2.1    bouyer 			       (int)DINODE_SIZE - sup->su_nbytes);
    524      1.22  perseant 			panic("lfs_vfree: negative byte count");
    525      1.17  perseant 			sup->su_nbytes = DINODE_SIZE;
    526      1.17  perseant 		}
    527      1.22  perseant #endif
    528      1.16   thorpej 		sup->su_nbytes -= DINODE_SIZE;
    529  1.25.2.1    bouyer 		(void) VOP_BWRITE(bp); /* Ifile */
    530       1.1   mycroft 	}
    531      1.17  perseant 
    532       1.1   mycroft 	/* Set superblock modified bit and decrement file count. */
    533       1.1   mycroft 	fs->lfs_fmod = 1;
    534       1.1   mycroft 	--fs->lfs_nfiles;
    535      1.17  perseant 
    536  1.25.2.1    bouyer 	lfs_segunlock(fs);
    537       1.1   mycroft 	return (0);
    538       1.1   mycroft }
    539