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