Home | History | Annotate | Line # | Download | only in lfs
lfs_alloc.c revision 1.15
      1 /*	$NetBSD: lfs_alloc.c,v 1.15 1998/09/01 03:26:05 thorpej 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 #if defined(_KERNEL) && !defined(_LKM)
     39 #include "opt_quota.h"
     40 #endif
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/buf.h>
     46 #include <sys/vnode.h>
     47 #include <sys/syslog.h>
     48 #include <sys/mount.h>
     49 #include <sys/malloc.h>
     50 #include <sys/pool.h>
     51 
     52 #include <vm/vm.h>
     53 
     54 #include <ufs/ufs/quota.h>
     55 #include <ufs/ufs/inode.h>
     56 #include <ufs/ufs/ufsmount.h>
     57 #include <ufs/ufs/ufs_extern.h>
     58 
     59 #include <ufs/lfs/lfs.h>
     60 #include <ufs/lfs/lfs_extern.h>
     61 
     62 /* Allocate a new inode. */
     63 /* ARGSUSED */
     64 int
     65 lfs_valloc(v)
     66 	void *v;
     67 {
     68 	struct vop_valloc_args /* {
     69 		struct vnode *a_pvp;
     70 		int a_mode;
     71 		struct ucred *a_cred;
     72 		struct vnode **a_vpp;
     73 	} */ *ap = v;
     74 	struct lfs *fs;
     75 	struct buf *bp;
     76 	struct ifile *ifp;
     77 	struct inode *ip;
     78 	struct vnode *vp;
     79 	ufs_daddr_t blkno;
     80 	ino_t new_ino;
     81 	u_long i, max;
     82 	int error;
     83 
     84 	/* Get the head of the freelist. */
     85 	fs = VTOI(ap->a_pvp)->i_lfs;
     86 	new_ino = fs->lfs_free;
     87 #ifdef ALLOCPRINT
     88 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
     89 #endif
     90 
     91 	/*
     92 	 * Remove the inode from the free list and write the new start
     93 	 * of the free list into the superblock.
     94 	 */
     95 	LFS_IENTRY(ifp, fs, new_ino, bp);
     96 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
     97 		panic("lfs_ialloc: inuse inode on the free list");
     98 	fs->lfs_free = ifp->if_nextfree;
     99 	brelse(bp);
    100 
    101 	/* Extend IFILE so that the next lfs_valloc will succeed. */
    102 	if (fs->lfs_free == LFS_UNUSED_INUM) {
    103 		vp = fs->lfs_ivnode;
    104 		ip = VTOI(vp);
    105 		blkno = lblkno(fs, ip->i_ffs_size);
    106 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
    107 		ip->i_ffs_size += fs->lfs_bsize;
    108 #ifdef UVM
    109 		uvm_vnp_setsize(vp, ip->i_ffs_size);
    110 		(void)uvm_vnp_uncache(vp);
    111 #else
    112 		vnode_pager_setsize(vp, ip->i_ffs_size);
    113 		vnode_pager_uncache(vp);
    114 #endif
    115 
    116 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
    117 		    fs->lfs_ifpb;
    118 		fs->lfs_free = i;
    119 		max = i + fs->lfs_ifpb;
    120 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
    121 			ifp->if_version = 1;
    122 			ifp->if_daddr = LFS_UNUSED_DADDR;
    123 			ifp->if_nextfree = ++i;
    124 		}
    125 		ifp--;
    126 		ifp->if_nextfree = LFS_UNUSED_INUM;
    127 		if ((error = VOP_BWRITE(bp)) != 0)
    128 			return (error);
    129 	}
    130 
    131 	/* Create a vnode to associate with the inode. */
    132 	if ((error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp)) != 0)
    133 		return (error);
    134 
    135 
    136 	ip = VTOI(vp);
    137 	/* Zero out the direct and indirect block addresses. */
    138 	bzero(&ip->i_din.ffs_din, sizeof(struct dinode));
    139 	ip->i_din.ffs_din.di_inumber = new_ino;
    140 
    141 	/* Set a new generation number for this inode. */
    142 	ip->i_ffs_gen++;
    143 
    144 	/* Insert into the inode hash table. */
    145 	ufs_ihashins(ip);
    146 
    147 	error = ufs_vinit(vp->v_mount, lfs_specop_p, lfs_fifoop_p, &vp);
    148 	if (error) {
    149 		vput(vp);
    150 		*ap->a_vpp = NULL;
    151 		return (error);
    152 	}
    153 
    154 	*ap->a_vpp = vp;
    155 	vp->v_flag |= VDIROP;
    156 	VREF(ip->i_devvp);
    157 
    158 	/* Set superblock modified bit and increment file count. */
    159 	fs->lfs_fmod = 1;
    160 	++fs->lfs_nfiles;
    161 	return (0);
    162 }
    163 
    164 /* Create a new vnode/inode pair and initialize what fields we can. */
    165 int
    166 lfs_vcreate(mp, ino, vpp)
    167 	struct mount *mp;
    168 	ino_t ino;
    169 	struct vnode **vpp;
    170 {
    171 	extern int (**lfs_vnodeop_p) __P((void *));
    172 	struct inode *ip;
    173 	struct ufsmount *ump;
    174 	int error;
    175 #ifdef QUOTA
    176 	int i;
    177 #endif
    178 
    179 	/* Create the vnode. */
    180 	if ((error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) != 0) {
    181 		*vpp = NULL;
    182 		return (error);
    183 	}
    184 
    185 	/* Get a pointer to the private mount structure. */
    186 	ump = VFSTOUFS(mp);
    187 
    188 	/* Initialize the inode. */
    189 	ip = pool_get(&lfs_inode_pool, PR_WAITOK);
    190 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
    191 	(*vpp)->v_data = ip;
    192 	ip->i_vnode = *vpp;
    193 	ip->i_devvp = ump->um_devvp;
    194 	ip->i_flag = IN_MODIFIED;
    195 	ip->i_dev = ump->um_dev;
    196 	ip->i_number = ip->i_din.ffs_din.di_inumber = ino;
    197 	ip->i_lfs = ump->um_lfs;
    198 #ifdef QUOTA
    199 	for (i = 0; i < MAXQUOTAS; i++)
    200 		ip->i_dquot[i] = NODQUOT;
    201 #endif
    202 	ip->i_lockf = 0;
    203 	ip->i_diroff = 0;
    204 	ip->i_ffs_mode = 0;
    205 	ip->i_ffs_size = 0;
    206 	ip->i_ffs_blocks = 0;
    207 	++ump->um_lfs->lfs_uinodes;
    208 	return (0);
    209 }
    210 
    211 /* Free an inode. */
    212 /* ARGUSED */
    213 int
    214 lfs_vfree(v)
    215 	void *v;
    216 {
    217 	struct vop_vfree_args /* {
    218 		struct vnode *a_pvp;
    219 		ino_t a_ino;
    220 		int a_mode;
    221 	} */ *ap = v;
    222 	SEGUSE *sup;
    223 	struct buf *bp;
    224 	struct ifile *ifp;
    225 	struct inode *ip;
    226 	struct lfs *fs;
    227 	ufs_daddr_t old_iaddr;
    228 	ino_t ino;
    229 
    230 	/* Get the inode number and file system. */
    231 	ip = VTOI(ap->a_pvp);
    232 	fs = ip->i_lfs;
    233 	ino = ip->i_number;
    234 	if (ip->i_flag & IN_MODIFIED) {
    235 		--fs->lfs_uinodes;
    236 		ip->i_flag &=
    237 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
    238 	}
    239 	/*
    240 	 * Set the ifile's inode entry to unused, increment its version number
    241 	 * and link it into the free chain.
    242 	 */
    243 	LFS_IENTRY(ifp, fs, ino, bp);
    244 	old_iaddr = ifp->if_daddr;
    245 	ifp->if_daddr = LFS_UNUSED_DADDR;
    246 	++ifp->if_version;
    247 	ifp->if_nextfree = fs->lfs_free;
    248 	fs->lfs_free = ino;
    249 	(void) VOP_BWRITE(bp);
    250 
    251 	if (old_iaddr != LFS_UNUSED_DADDR) {
    252 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
    253 #ifdef DIAGNOSTIC
    254 		if (sup->su_nbytes < sizeof(struct dinode))
    255 			panic("lfs_vfree: negative byte count (segment %d)\n",
    256 			    datosn(fs, old_iaddr));
    257 #endif
    258 		sup->su_nbytes -= sizeof(struct dinode);
    259 		(void) VOP_BWRITE(bp);
    260 	}
    261 
    262 	/* Set superblock modified bit and decrement file count. */
    263 	fs->lfs_fmod = 1;
    264 	--fs->lfs_nfiles;
    265 	return (0);
    266 }
    267