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