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