xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 69419)
123394Smckusick /*
2*69419Smckusick  * Copyright (c) 1991, 1993, 1995
363375Sbostic  *	The Regents of the University of California.  All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*69419Smckusick  *	@(#)lfs_alloc.c	8.7 (Berkeley) 05/14/95
823394Smckusick  */
94359Smckusick 
1051485Sbostic #include <sys/param.h>
1151485Sbostic #include <sys/kernel.h>
1251485Sbostic #include <sys/buf.h>
1351485Sbostic #include <sys/vnode.h>
1451485Sbostic #include <sys/syslog.h>
1551485Sbostic #include <sys/mount.h>
1651990Smckusick #include <sys/malloc.h>
174359Smckusick 
1853320Smckusick #include <vm/vm.h>
1953320Smckusick 
2051492Sbostic #include <ufs/ufs/quota.h>
2151492Sbostic #include <ufs/ufs/inode.h>
2251492Sbostic #include <ufs/ufs/ufsmount.h>
23*69419Smckusick #include <ufs/ufs/ufs_extern.h>
2447571Skarels 
2551492Sbostic #include <ufs/lfs/lfs.h>
2651492Sbostic #include <ufs/lfs/lfs_extern.h>
2751485Sbostic 
2851492Sbostic extern u_long nextgennumber;
2951492Sbostic 
3051485Sbostic /* Allocate a new inode. */
3151485Sbostic /* ARGSUSED */
3251485Sbostic int
lfs_valloc(ap)3354686Smargo lfs_valloc(ap)
3454686Smargo 	struct vop_valloc_args /* {
3554686Smargo 		struct vnode *a_pvp;
3654686Smargo 		int a_mode;
3754686Smargo 		struct ucred *a_cred;
3854686Smargo 		struct vnode **a_vpp;
3954686Smargo 	} */ *ap;
4051155Sbostic {
4151492Sbostic 	struct lfs *fs;
4252080Sbostic 	struct buf *bp;
4352080Sbostic 	struct ifile *ifp;
4452080Sbostic 	struct inode *ip;
4552080Sbostic 	struct vnode *vp;
4668550Smckusick 	ufs_daddr_t blkno;
4751155Sbostic 	ino_t new_ino;
4852080Sbostic 	u_long i, max;
4965472Sbostic 	int error;
504359Smckusick 
5151215Sbostic 	/* Get the head of the freelist. */
5253591Sheideman 	fs = VTOI(ap->a_pvp)->i_lfs;
5351155Sbostic 	new_ino = fs->lfs_free;
5451485Sbostic #ifdef ALLOCPRINT
5551485Sbostic 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
5651485Sbostic #endif
574359Smckusick 
5851933Sbostic 	/*
5952080Sbostic 	 * Remove the inode from the free list and write the new start
6052080Sbostic 	 * of the free list into the superblock.
6151933Sbostic 	 */
6251155Sbostic 	LFS_IENTRY(ifp, fs, new_ino, bp);
6351155Sbostic 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
6451215Sbostic 		panic("lfs_ialloc: inuse inode on the free list");
6551183Sbostic 	fs->lfs_free = ifp->if_nextfree;
6652080Sbostic 	brelse(bp);
674426Smckusic 
6852080Sbostic 	/* Extend IFILE so that the next lfs_valloc will succeed. */
6952080Sbostic 	if (fs->lfs_free == LFS_UNUSED_INUM) {
7052080Sbostic 		vp = fs->lfs_ivnode;
7152080Sbostic 		ip = VTOI(vp);
7252080Sbostic 		blkno = lblkno(fs, ip->i_size);
7369285Smckusick 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
7457069Smargo 		ip->i_size += fs->lfs_bsize;
7557069Smargo 		vnode_pager_setsize(vp, (u_long)ip->i_size);
7657069Smargo 		vnode_pager_uncache(vp);
7757069Smargo 
7852080Sbostic 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
7952080Sbostic 		    fs->lfs_ifpb;
8052080Sbostic 		fs->lfs_free = i;
8152080Sbostic 		max = i + fs->lfs_ifpb;
8264524Sbostic 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
8352080Sbostic 			ifp->if_version = 1;
8452080Sbostic 			ifp->if_daddr = LFS_UNUSED_DADDR;
8552080Sbostic 			ifp->if_nextfree = ++i;
8652080Sbostic 		}
8752080Sbostic 		ifp--;
8852080Sbostic 		ifp->if_nextfree = LFS_UNUSED_INUM;
8955937Sbostic 		if (error = VOP_BWRITE(bp))
9055937Sbostic 			return (error);
9152080Sbostic 	}
9252080Sbostic 
9351215Sbostic 	/* Create a vnode to associate with the inode. */
9453591Sheideman 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
9537735Smckusick 		return (error);
9656866Smargo 
9757069Smargo 
9851560Smckusick 	ip = VTOI(vp);
9952080Sbostic 	/* Zero out the direct and indirect block addresses. */
10056866Smargo 	bzero(&ip->i_din, sizeof(struct dinode));
10156866Smargo 	ip->i_din.di_inumber = new_ino;
10252080Sbostic 
10351215Sbostic 	/* Set a new generation number for this inode. */
10438255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
10538255Smckusick 		nextgennumber = time.tv_sec;
10638255Smckusick 	ip->i_gen = nextgennumber;
1074359Smckusick 
10851215Sbostic 	/* Insert into the inode hash table. */
10951485Sbostic 	ufs_ihashins(ip);
1104359Smckusick 
11156866Smargo 	if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
11256866Smargo 		vput(vp);
11356866Smargo 		*ap->a_vpp = NULL;
11456866Smargo 		return (error);
11556866Smargo 	}
11656866Smargo 
11756866Smargo 	*ap->a_vpp = vp;
11856866Smargo 	vp->v_flag |= VDIROP;
11956866Smargo 	VREF(ip->i_devvp);
12056866Smargo 
12151215Sbostic 	/* Set superblock modified bit and increment file count. */
12251215Sbostic 	fs->lfs_fmod = 1;
12351215Sbostic 	++fs->lfs_nfiles;
12451155Sbostic 	return (0);
1254359Smckusick }
1264359Smckusick 
12751215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */
12851347Sbostic int
lfs_vcreate(mp,ino,vpp)12951155Sbostic lfs_vcreate(mp, ino, vpp)
13052080Sbostic 	struct mount *mp;
13151155Sbostic 	ino_t ino;
13252080Sbostic 	struct vnode **vpp;
1334359Smckusick {
13453528Sheideman 	extern int (**lfs_vnodeop_p)();
13552080Sbostic 	struct inode *ip;
13652080Sbostic 	struct ufsmount *ump;
13751155Sbostic 	int error, i;
1384359Smckusick 
13951215Sbostic 	/* Create the vnode. */
14053528Sheideman 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
14151990Smckusick 		*vpp = NULL;
14251311Sbostic 		return (error);
14351990Smckusick 	}
1444359Smckusick 
14551215Sbostic 	/* Get a pointer to the private mount structure. */
14651155Sbostic 	ump = VFSTOUFS(mp);
1474651Smckusic 
14851155Sbostic 	/* Initialize the inode. */
14951990Smckusick 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
150*69419Smckusick 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
15151990Smckusick 	(*vpp)->v_data = ip;
15251560Smckusick 	ip->i_vnode = *vpp;
15352273Sbostic 	ip->i_devvp = ump->um_devvp;
15464609Sbostic 	ip->i_flag = IN_MODIFIED;
15551155Sbostic 	ip->i_dev = ump->um_dev;
15656866Smargo 	ip->i_number = ip->i_din.di_inumber = ino;
15751155Sbostic 	ip->i_lfs = ump->um_lfs;
15851155Sbostic #ifdef QUOTA
15951155Sbostic 	for (i = 0; i < MAXQUOTAS; i++)
16051155Sbostic 		ip->i_dquot[i] = NODQUOT;
16151155Sbostic #endif
16252273Sbostic 	ip->i_lockf = 0;
16352273Sbostic 	ip->i_diroff = 0;
16452273Sbostic 	ip->i_mode = 0;
16552273Sbostic 	ip->i_size = 0;
16655590Sbostic 	ip->i_blocks = 0;
16755937Sbostic 	++ump->um_lfs->lfs_uinodes;
16851155Sbostic 	return (0);
1694651Smckusic }
17051183Sbostic 
17151485Sbostic /* Free an inode. */
17251485Sbostic /* ARGUSED */
17353572Sheideman int
lfs_vfree(ap)17454686Smargo lfs_vfree(ap)
17554686Smargo 	struct vop_vfree_args /* {
17654686Smargo 		struct vnode *a_pvp;
17754686Smargo 		ino_t a_ino;
17854686Smargo 		int a_mode;
17954686Smargo 	} */ *ap;
18051215Sbostic {
18152680Sstaelin 	SEGUSE *sup;
18252080Sbostic 	struct buf *bp;
18352080Sbostic 	struct ifile *ifp;
18452080Sbostic 	struct inode *ip;
18551492Sbostic 	struct lfs *fs;
18668550Smckusick 	ufs_daddr_t old_iaddr;
18751215Sbostic 	ino_t ino;
18851215Sbostic 
18955807Sbostic 	/* Get the inode number and file system. */
19053591Sheideman 	ip = VTOI(ap->a_pvp);
19151215Sbostic 	fs = ip->i_lfs;
19251215Sbostic 	ino = ip->i_number;
19364609Sbostic 	if (ip->i_flag & IN_MODIFIED) {
19455937Sbostic 		--fs->lfs_uinodes;
19564609Sbostic 		ip->i_flag &=
19664609Sbostic 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
19755937Sbostic 	}
19851485Sbostic 	/*
19951933Sbostic 	 * Set the ifile's inode entry to unused, increment its version number
20051933Sbostic 	 * and link it into the free chain.
20151485Sbostic 	 */
20251215Sbostic 	LFS_IENTRY(ifp, fs, ino, bp);
20352680Sstaelin 	old_iaddr = ifp->if_daddr;
20451485Sbostic 	ifp->if_daddr = LFS_UNUSED_DADDR;
20551485Sbostic 	++ifp->if_version;
20651485Sbostic 	ifp->if_nextfree = fs->lfs_free;
20751485Sbostic 	fs->lfs_free = ino;
20855937Sbostic 	(void) VOP_BWRITE(bp);
20951215Sbostic 
21052680Sstaelin 	if (old_iaddr != LFS_UNUSED_DADDR) {
21152680Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
21252680Sstaelin #ifdef DIAGNOSTIC
21352680Sstaelin 		if (sup->su_nbytes < sizeof(struct dinode))
21452680Sstaelin 			panic("lfs_vfree: negative byte count (segment %d)\n",
21552680Sstaelin 			    datosn(fs, old_iaddr));
21652680Sstaelin #endif
21752680Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
21855937Sbostic 		(void) VOP_BWRITE(bp);
21952680Sstaelin 	}
22052680Sstaelin 
22151485Sbostic 	/* Set superblock modified bit and decrement file count. */
22251485Sbostic 	fs->lfs_fmod = 1;
22351485Sbostic 	--fs->lfs_nfiles;
22453572Sheideman 	return (0);
22551215Sbostic }
226