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