Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.11
      1 /*	$NetBSD: lfs_alloc.c,v 1.11 1998/03/01 02:23:23 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)lfs_alloc.c	8.4 (Berkeley) 1/4/94
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/buf.h>
     42 #include <sys/vnode.h>
     43 #include <sys/syslog.h>
     44 #include <sys/mount.h>
     45 #include <sys/malloc.h>
     46 
     47 #include <vm/vm.h>
     48 
     49 #include <ufs/ufs/quota.h>
     50 #include <ufs/ufs/inode.h>
     51 #include <ufs/ufs/ufsmount.h>
     52 #include <ufs/ufs/ufs_extern.h>
     53 
     54 #include <ufs/lfs/lfs.h>
     55 #include <ufs/lfs/lfs_extern.h>
     56 
     57 /* Allocate a new inode. */
     58 /* ARGSUSED */
     59 int
     60 lfs_valloc(v)
     61 	void *v;
     62 {
     63 	struct vop_valloc_args /* {
     64 		struct vnode *a_pvp;
     65 		int a_mode;
     66 		struct ucred *a_cred;
     67 		struct vnode **a_vpp;
     68 	} */ *ap = v;
     69 	struct lfs *fs;
     70 	struct buf *bp;
     71 	struct ifile *ifp;
     72 	struct inode *ip;
     73 	struct vnode *vp;
     74 	ufs_daddr_t blkno;
     75 	ino_t new_ino;
     76 	u_long i, max;
     77 	int error;
     78 
     79 	/* Get the head of the freelist. */
     80 	fs = VTOI(ap->a_pvp)->i_lfs;
     81 	new_ino = fs->lfs_free;
     82 #ifdef ALLOCPRINT
     83 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
     84 #endif
     85 
     86 	/*
     87 	 * Remove the inode from the free list and write the new start
     88 	 * of the free list into the superblock.
     89 	 */
     90 	LFS_IENTRY(ifp, fs, new_ino, bp);
     91 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
     92 		panic("lfs_ialloc: inuse inode on the free list");
     93 	fs->lfs_free = ifp->if_nextfree;
     94 	brelse(bp);
     95 
     96 	/* Extend IFILE so that the next lfs_valloc will succeed. */
     97 	if (fs->lfs_free == LFS_UNUSED_INUM) {
     98 		vp = fs->lfs_ivnode;
     99 		ip = VTOI(vp);
    100 		blkno = lblkno(fs, ip->i_ffs_size);
    101 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
    102 		ip->i_ffs_size += fs->lfs_bsize;
    103 #ifdef UVM
    104 		uvm_vnp_setsize(vp, ip->i_ffs_size);
    105 		(void)uvm_vnp_uncache(vp);
    106 #else
    107 		vnode_pager_setsize(vp, ip->i_ffs_size);
    108 		vnode_pager_uncache(vp);
    109 #endif
    110 
    111 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    112 		    fs->lfs_ifpb;
    113 		fs->lfs_free = i;
    114 		max = i + fs->lfs_ifpb;
    115 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    116 			ifp->if_version = 1;
    117 			ifp->if_daddr = LFS_UNUSED_DADDR;
    118 			ifp->if_nextfree = ++i;
    119 		}
    120 		ifp--;
    121 		ifp->if_nextfree = LFS_UNUSED_INUM;
    122 		if ((error = VOP_BWRITE(bp)) != 0)
    123 			return (error);
    124 	}
    125 
    126 	/* Create a vnode to associate with the inode. */
    127 	if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0)
    128 		return (error);
    129 
    130 
    131 	ip = VTOI(vp);
    132 	/* Zero out the direct and indirect block addresses. */
    133 	bzero(&ip->i_din.ffs_din, sizeof(struct dinode));
    134 	ip->i_din.ffs_din.di_inumber = new_ino;
    135 
    136 	/* Set a new generation number for this inode. */
    137 	ip->i_ffs_gen++;
    138 
    139 	/* Insert into the inode hash table. */
    140 	ufs_ihashins(ip);
    141 
    142 	error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp);
    143 	if (error) {
    144 		vput(vp);
    145 		*ap->a_vpp = NULL;
    146 		return (error);
    147 	}
    148 
    149 	*ap->a_vpp = vp;
    150 	vp->v_flag |= VDIROP;
    151 	VREF(ip->i_devvp);
    152 
    153 	/* Set superblock modified bit and increment file count. */
    154 	fs->lfs_fmod = 1;
    155 	++fs->lfs_nfiles;
    156 	return (0);
    157 }
    158 
    159 /* Create a new vnode/inode pair and initialize what fields we can. */
    160 int
    161 lfs_vcreate(mp, ino, vpp)
    162 	struct mount *mp;
    163 	ino_t ino;
    164 	struct vnode **vpp;
    165 {
    166 	extern int (**lfs_vnodeop_p) __P((void *));
    167 	struct inode *ip;
    168 	struct ufsmount *ump;
    169 	int error;
    170 #ifdef QUOTA
    171 	int i;
    172 #endif
    173 
    174 	/* Create the vnode. */
    175 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
    176 		*vpp = NULL;
    177 		return (error);
    178 	}
    179 
    180 	/* Get a pointer to the private mount structure. */
    181 	ump = VFSTOUFS(mp);
    182 
    183 	/* Initialize the inode. */
    184 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
    185 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
    186 	(*vpp)->v_data = ip;
    187 	ip->i_vnode = *vpp;
    188 	ip->i_devvp = ump->um_devvp;
    189 	ip->i_flag = IN_MODIFIED;
    190 	ip->i_dev = ump->um_dev;
    191 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    192 	ip->i_lfs = ump->um_lfs;
    193 #ifdef QUOTA
    194 	for (i = 0; i < MAXQUOTAS; i++)
    195 		ip->i_dquot[i] = NODQUOT;
    196 #endif
    197 	ip->i_lockf = 0;
    198 	ip->i_diroff = 0;
    199 	ip->i_ffs_mode = 0;
    200 	ip->i_ffs_size = 0;
    201 	ip->i_ffs_blocks = 0;
    202 	++ump->um_lfs->lfs_uinodes;
    203 	return (0);
    204 }
    205 
    206 /* Free an inode. */
    207 /* ARGUSED */
    208 int
    209 lfs_vfree(v)
    210 	void *v;
    211 {
    212 	struct vop_vfree_args /* {
    213 		struct vnode *a_pvp;
    214 		ino_t a_ino;
    215 		int a_mode;
    216 	} */ *ap = v;
    217 	SEGUSE *sup;
    218 	struct buf *bp;
    219 	struct ifile *ifp;
    220 	struct inode *ip;
    221 	struct lfs *fs;
    222 	ufs_daddr_t old_iaddr;
    223 	ino_t ino;
    224 
    225 	/* Get the inode number and file system. */
    226 	ip = VTOI(ap->a_pvp);
    227 	fs = ip->i_lfs;
    228 	ino = ip->i_number;
    229 	if (ip->i_flag & IN_MODIFIED) {
    230 		--fs->lfs_uinodes;
    231 		ip->i_flag &=
    232 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    233 	}
    234 	/*
    235 	 * Set the ifile's inode entry to unused, increment its version number
    236 	 * and link it into the free chain.
    237 	 */
    238 	LFS_IENTRY(ifp, fs, ino, bp);
    239 	old_iaddr = ifp->if_daddr;
    240 	ifp->if_daddr = LFS_UNUSED_DADDR;
    241 	++ifp->if_version;
    242 	ifp->if_nextfree = fs->lfs_free;
    243 	fs->lfs_free = ino;
    244 	(void) VOP_BWRITE(bp);
    245 
    246 	if (old_iaddr != LFS_UNUSED_DADDR) {
    247 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    248 #ifdef DIAGNOSTIC
    249 		if (sup->su_nbytes < sizeof(struct dinode))
    250 			panic("lfs_vfree: negative byte count (segment %d)\n",
    251 			    datosn(fs, old_iaddr));
    252 #endif
    253 		sup->su_nbytes -= sizeof(struct dinode);
    254 		(void) VOP_BWRITE(bp);
    255 	}
    256 
    257 	/* Set superblock modified bit and decrement file count. */
    258 	fs->lfs_fmod = 1;
    259 	--fs->lfs_nfiles;
    260 	return (0);
    261 }
    262