xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 56866)
123394Smckusick /*
251492Sbostic  * Copyright (c) 1991 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*56866Smargo  *	@(#)lfs_alloc.c	7.55 (Berkeley) 11/17/92
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>
2347571Skarels 
2451492Sbostic #include <ufs/lfs/lfs.h>
2551492Sbostic #include <ufs/lfs/lfs_extern.h>
2651485Sbostic 
2751492Sbostic extern u_long nextgennumber;
2851492Sbostic 
2951485Sbostic /* Allocate a new inode. */
3051485Sbostic /* ARGSUSED */
3151485Sbostic int
3254686Smargo lfs_valloc(ap)
3354686Smargo 	struct vop_valloc_args /* {
3454686Smargo 		struct vnode *a_pvp;
3554686Smargo 		int a_mode;
3654686Smargo 		struct ucred *a_cred;
3754686Smargo 		struct vnode **a_vpp;
3854686Smargo 	} */ *ap;
3951155Sbostic {
4051492Sbostic 	struct lfs *fs;
4152080Sbostic 	struct buf *bp;
4252080Sbostic 	struct ifile *ifp;
4352080Sbostic 	struct inode *ip;
4452080Sbostic 	struct vnode *vp;
4552080Sbostic 	daddr_t blkno;
4651155Sbostic 	ino_t new_ino;
4752080Sbostic 	u_long i, max;
4856157Smargo 	int bb, error;
494359Smckusick 
5051215Sbostic 	/* Get the head of the freelist. */
5153591Sheideman 	fs = VTOI(ap->a_pvp)->i_lfs;
5251155Sbostic 	new_ino = fs->lfs_free;
5351485Sbostic #ifdef ALLOCPRINT
5451485Sbostic 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
5551485Sbostic #endif
564359Smckusick 
5751933Sbostic 	/*
5852080Sbostic 	 * Remove the inode from the free list and write the new start
5952080Sbostic 	 * of the free list into the superblock.
6051933Sbostic 	 */
6151155Sbostic 	LFS_IENTRY(ifp, fs, new_ino, bp);
6251155Sbostic 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
6351215Sbostic 		panic("lfs_ialloc: inuse inode on the free list");
6451183Sbostic 	fs->lfs_free = ifp->if_nextfree;
6552080Sbostic 	brelse(bp);
664426Smckusic 
6752080Sbostic 	/* Extend IFILE so that the next lfs_valloc will succeed. */
6852080Sbostic 	if (fs->lfs_free == LFS_UNUSED_INUM) {
6956157Smargo 		bb = fsbtodb(fs, 1);
7056157Smargo 		if (!ISSPACE(fs, bb, ap->a_cred))
7156157Smargo 			return(ENOSPC);
7252080Sbostic 		vp = fs->lfs_ivnode;
7352080Sbostic 		ip = VTOI(vp);
7452080Sbostic 		blkno = lblkno(fs, ip->i_size);
7552080Sbostic 		bp = getblk(vp, blkno, fs->lfs_bsize);
7652080Sbostic 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
7752080Sbostic 		    fs->lfs_ifpb;
7852080Sbostic 		fs->lfs_free = i;
7952080Sbostic 		max = i + fs->lfs_ifpb;
8052080Sbostic 		for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) {
8152080Sbostic 			ifp->if_version = 1;
8252080Sbostic 			ifp->if_daddr = LFS_UNUSED_DADDR;
8352080Sbostic 			ifp->if_nextfree = ++i;
8452080Sbostic 		}
8552080Sbostic 		ifp--;
8652080Sbostic 		ifp->if_nextfree = LFS_UNUSED_INUM;
8752080Sbostic 
8855455Sbostic 		ip->i_blocks += btodb(fs->lfs_bsize);
8955590Sbostic 		fs->lfs_bfree -= btodb(fs->lfs_bsize);
9052080Sbostic 		ip->i_size += fs->lfs_bsize;
9153320Smckusick 		vnode_pager_setsize(vp, (u_long)ip->i_size);
9252080Sbostic 		vnode_pager_uncache(vp);
9355937Sbostic 		if (error = VOP_BWRITE(bp))
9455937Sbostic 			return (error);
9552080Sbostic 	}
9652080Sbostic 
9751215Sbostic 	/* Create a vnode to associate with the inode. */
9853591Sheideman 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
9937735Smckusick 		return (error);
100*56866Smargo 
10151560Smckusick 	ip = VTOI(vp);
10251155Sbostic 
10352080Sbostic 	/* Zero out the direct and indirect block addresses. */
104*56866Smargo 	bzero(&ip->i_din, sizeof(struct dinode));
105*56866Smargo 	ip->i_din.di_inumber = new_ino;
10652080Sbostic 
10751215Sbostic 	/* Set a new generation number for this inode. */
10838255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
10938255Smckusick 		nextgennumber = time.tv_sec;
11038255Smckusick 	ip->i_gen = nextgennumber;
1114359Smckusick 
11251215Sbostic 	/* Insert into the inode hash table. */
11351485Sbostic 	ufs_ihashins(ip);
1144359Smckusick 
115*56866Smargo 	if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
116*56866Smargo 		vput(vp);
117*56866Smargo 		*ap->a_vpp = NULL;
118*56866Smargo 		return (error);
119*56866Smargo 	}
120*56866Smargo 
121*56866Smargo 	*ap->a_vpp = vp;
122*56866Smargo 	vp->v_flag |= VDIROP;
123*56866Smargo 	VREF(ip->i_devvp);
124*56866Smargo 
12551215Sbostic 	/* Set superblock modified bit and increment file count. */
12651215Sbostic 	fs->lfs_fmod = 1;
12751215Sbostic 	++fs->lfs_nfiles;
12851155Sbostic 	return (0);
1294359Smckusick }
1304359Smckusick 
13151215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */
13251347Sbostic int
13351155Sbostic lfs_vcreate(mp, ino, vpp)
13452080Sbostic 	struct mount *mp;
13551155Sbostic 	ino_t ino;
13652080Sbostic 	struct vnode **vpp;
1374359Smckusick {
13853528Sheideman 	extern int (**lfs_vnodeop_p)();
13952080Sbostic 	struct inode *ip;
14052080Sbostic 	struct ufsmount *ump;
14151155Sbostic 	int error, i;
1424359Smckusick 
14351215Sbostic 	/* Create the vnode. */
14453528Sheideman 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
14551990Smckusick 		*vpp = NULL;
14651311Sbostic 		return (error);
14751990Smckusick 	}
1484359Smckusick 
14951215Sbostic 	/* Get a pointer to the private mount structure. */
15051155Sbostic 	ump = VFSTOUFS(mp);
1514651Smckusic 
15251155Sbostic 	/* Initialize the inode. */
15351990Smckusick 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
15451990Smckusick 	(*vpp)->v_data = ip;
15551560Smckusick 	ip->i_vnode = *vpp;
15652273Sbostic 	ip->i_devvp = ump->um_devvp;
15755937Sbostic 	ip->i_flag = IMOD;
15851155Sbostic 	ip->i_dev = ump->um_dev;
159*56866Smargo 	ip->i_number = ip->i_din.di_inumber = ino;
16051155Sbostic 	ip->i_lfs = ump->um_lfs;
16151155Sbostic #ifdef QUOTA
16251155Sbostic 	for (i = 0; i < MAXQUOTAS; i++)
16351155Sbostic 		ip->i_dquot[i] = NODQUOT;
16451155Sbostic #endif
16552273Sbostic 	ip->i_lockf = 0;
16652273Sbostic 	ip->i_diroff = 0;
16752273Sbostic 	ip->i_mode = 0;
16852273Sbostic 	ip->i_size = 0;
16955590Sbostic 	ip->i_blocks = 0;
17055937Sbostic 	++ump->um_lfs->lfs_uinodes;
17151155Sbostic 	return (0);
1724651Smckusic }
17351183Sbostic 
17451485Sbostic /* Free an inode. */
17551485Sbostic /* ARGUSED */
17653572Sheideman int
17754686Smargo lfs_vfree(ap)
17854686Smargo 	struct vop_vfree_args /* {
17954686Smargo 		struct vnode *a_pvp;
18054686Smargo 		ino_t a_ino;
18154686Smargo 		int a_mode;
18254686Smargo 	} */ *ap;
18351215Sbostic {
18452680Sstaelin 	SEGUSE *sup;
18552080Sbostic 	struct buf *bp;
18652080Sbostic 	struct ifile *ifp;
18752080Sbostic 	struct inode *ip;
18851492Sbostic 	struct lfs *fs;
18952680Sstaelin 	daddr_t old_iaddr;
19051215Sbostic 	ino_t ino;
19155937Sbostic 	int error;
19251215Sbostic 
19355807Sbostic 	/* Get the inode number and file system. */
19453591Sheideman 	ip = VTOI(ap->a_pvp);
19551215Sbostic 	fs = ip->i_lfs;
19651215Sbostic 	ino = ip->i_number;
19755937Sbostic 	if (ip->i_flag & IMOD) {
19855937Sbostic 		--fs->lfs_uinodes;
19955937Sbostic 		ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
20055937Sbostic 	}
20151485Sbostic 	/*
20251933Sbostic 	 * Set the ifile's inode entry to unused, increment its version number
20351933Sbostic 	 * and link it into the free chain.
20451485Sbostic 	 */
20551215Sbostic 	LFS_IENTRY(ifp, fs, ino, bp);
20652680Sstaelin 	old_iaddr = ifp->if_daddr;
20751485Sbostic 	ifp->if_daddr = LFS_UNUSED_DADDR;
20851485Sbostic 	++ifp->if_version;
20951485Sbostic 	ifp->if_nextfree = fs->lfs_free;
21051485Sbostic 	fs->lfs_free = ino;
21155937Sbostic 	(void) VOP_BWRITE(bp);
21251215Sbostic 
21352680Sstaelin 	if (old_iaddr != LFS_UNUSED_DADDR) {
21452680Sstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
21552680Sstaelin #ifdef DIAGNOSTIC
21652680Sstaelin 		if (sup->su_nbytes < sizeof(struct dinode))
21752680Sstaelin 			panic("lfs_vfree: negative byte count (segment %d)\n",
21852680Sstaelin 			    datosn(fs, old_iaddr));
21952680Sstaelin #endif
22052680Sstaelin 		sup->su_nbytes -= sizeof(struct dinode);
22155937Sbostic 		(void) VOP_BWRITE(bp);
22252680Sstaelin 	}
22352680Sstaelin 
22451485Sbostic 	/* Set superblock modified bit and decrement file count. */
22551485Sbostic 	fs->lfs_fmod = 1;
22651485Sbostic 	--fs->lfs_nfiles;
22753572Sheideman 	return (0);
22851215Sbostic }
229