xref: /original-bsd/sys/ufs/lfs/lfs_alloc.c (revision 8a5f275b)
178ca1e03Smckusick /*
2*8a5f275bSmckusick  * Copyright (c) 1991, 1993, 1995
3b6149518Sbostic  *	The Regents of the University of California.  All rights reserved.
478ca1e03Smckusick  *
588f5dc9aSbostic  * %sccs.include.redist.c%
66fd81102Sbostic  *
7*8a5f275bSmckusick  *	@(#)lfs_alloc.c	8.7 (Berkeley) 05/14/95
878ca1e03Smckusick  */
95a6964aaSmckusick 
1021a17e3bSbostic #include <sys/param.h>
1121a17e3bSbostic #include <sys/kernel.h>
1221a17e3bSbostic #include <sys/buf.h>
1321a17e3bSbostic #include <sys/vnode.h>
1421a17e3bSbostic #include <sys/syslog.h>
1521a17e3bSbostic #include <sys/mount.h>
16356acbf4Smckusick #include <sys/malloc.h>
1726a59bf8Skarels 
18500b75a9Smckusick #include <vm/vm.h>
19500b75a9Smckusick 
203191b746Sbostic #include <ufs/ufs/quota.h>
213191b746Sbostic #include <ufs/ufs/inode.h>
223191b746Sbostic #include <ufs/ufs/ufsmount.h>
23*8a5f275bSmckusick #include <ufs/ufs/ufs_extern.h>
245a6964aaSmckusick 
253191b746Sbostic #include <ufs/lfs/lfs.h>
263191b746Sbostic #include <ufs/lfs/lfs_extern.h>
273191b746Sbostic 
283191b746Sbostic extern u_long nextgennumber;
2921a17e3bSbostic 
3021a17e3bSbostic /* Allocate a new inode. */
3121a17e3bSbostic /* ARGSUSED */
3221a17e3bSbostic int
lfs_valloc(ap)3399ddf719Sheideman lfs_valloc(ap)
343713f046Smargo 	struct vop_valloc_args /* {
353713f046Smargo 		struct vnode *a_pvp;
363713f046Smargo 		int a_mode;
373713f046Smargo 		struct ucred *a_cred;
383713f046Smargo 		struct vnode **a_vpp;
393713f046Smargo 	} */ *ap;
405a6964aaSmckusick {
413191b746Sbostic 	struct lfs *fs;
424caa5015Sbostic 	struct buf *bp;
434caa5015Sbostic 	struct ifile *ifp;
444caa5015Sbostic 	struct inode *ip;
454caa5015Sbostic 	struct vnode *vp;
466b8e5290Smckusick 	ufs_daddr_t blkno;
4783dca3beSbostic 	ino_t new_ino;
484caa5015Sbostic 	u_long i, max;
49dbb16a89Sbostic 	int error;
505a6964aaSmckusick 
5168bfaaa9Sbostic 	/* Get the head of the freelist. */
5248d3215bSheideman 	fs = VTOI(ap->a_pvp)->i_lfs;
5383dca3beSbostic 	new_ino = fs->lfs_free;
5421a17e3bSbostic #ifdef ALLOCPRINT
5568bfaaa9Sbostic 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
5621a17e3bSbostic #endif
5783dca3beSbostic 
58035b5cddSbostic 	/*
594caa5015Sbostic 	 * Remove the inode from the free list and write the new start
604caa5015Sbostic 	 * of the free list into the superblock.
61035b5cddSbostic 	 */
6283dca3beSbostic 	LFS_IENTRY(ifp, fs, new_ino, bp);
6383dca3beSbostic 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
6468bfaaa9Sbostic 		panic("lfs_ialloc: inuse inode on the free list");
6583dca3beSbostic 	fs->lfs_free = ifp->if_nextfree;
664caa5015Sbostic 	brelse(bp);
674caa5015Sbostic 
684caa5015Sbostic 	/* Extend IFILE so that the next lfs_valloc will succeed. */
694caa5015Sbostic 	if (fs->lfs_free == LFS_UNUSED_INUM) {
704caa5015Sbostic 		vp = fs->lfs_ivnode;
714caa5015Sbostic 		ip = VTOI(vp);
724caa5015Sbostic 		blkno = lblkno(fs, ip->i_size);
730b0d8c6cSmckusick 		lfs_balloc(vp, 0, fs->lfs_bsize, blkno, &bp);
7446b3e676Smargo 		ip->i_size += fs->lfs_bsize;
7546b3e676Smargo 		vnode_pager_setsize(vp, (u_long)ip->i_size);
7646b3e676Smargo 		vnode_pager_uncache(vp);
7746b3e676Smargo 
784caa5015Sbostic 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
794caa5015Sbostic 		    fs->lfs_ifpb;
804caa5015Sbostic 		fs->lfs_free = i;
814caa5015Sbostic 		max = i + fs->lfs_ifpb;
8260fd6326Sbostic 		for (ifp = (struct ifile *)bp->b_data; i < max; ++ifp) {
834caa5015Sbostic 			ifp->if_version = 1;
844caa5015Sbostic 			ifp->if_daddr = LFS_UNUSED_DADDR;
854caa5015Sbostic 			ifp->if_nextfree = ++i;
864caa5015Sbostic 		}
874caa5015Sbostic 		ifp--;
884caa5015Sbostic 		ifp->if_nextfree = LFS_UNUSED_INUM;
89cfe7342fSbostic 		if (error = VOP_BWRITE(bp))
90cfe7342fSbostic 			return (error);
914caa5015Sbostic 	}
9283dca3beSbostic 
9368bfaaa9Sbostic 	/* Create a vnode to associate with the inode. */
9448d3215bSheideman 	if (error = lfs_vcreate(ap->a_pvp->v_mount, new_ino, &vp))
955dfd3931Smckusick 		return (error);
969dfc7babSmargo 
9783dca3beSbostic 
9846b3e676Smargo 	ip = VTOI(vp);
994caa5015Sbostic 	/* Zero out the direct and indirect block addresses. */
1009dfc7babSmargo 	bzero(&ip->i_din, sizeof(struct dinode));
1019dfc7babSmargo 	ip->i_din.di_inumber = new_ino;
1024caa5015Sbostic 
10368bfaaa9Sbostic 	/* Set a new generation number for this inode. */
10430b4b3f8Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
10530b4b3f8Smckusick 		nextgennumber = time.tv_sec;
10630b4b3f8Smckusick 	ip->i_gen = nextgennumber;
10783dca3beSbostic 
10868bfaaa9Sbostic 	/* Insert into the inode hash table. */
10921a17e3bSbostic 	ufs_ihashins(ip);
11083dca3beSbostic 
1119dfc7babSmargo 	if (error = ufs_vinit(vp->v_mount, lfs_specop_p, LFS_FIFOOPS, &vp)) {
1129dfc7babSmargo 		vput(vp);
1139dfc7babSmargo 		*ap->a_vpp = NULL;
1149dfc7babSmargo 		return (error);
1159dfc7babSmargo 	}
1169dfc7babSmargo 
1179dfc7babSmargo 	*ap->a_vpp = vp;
1189dfc7babSmargo 	vp->v_flag |= VDIROP;
1199dfc7babSmargo 	VREF(ip->i_devvp);
1209dfc7babSmargo 
12168bfaaa9Sbostic 	/* Set superblock modified bit and increment file count. */
12268bfaaa9Sbostic 	fs->lfs_fmod = 1;
12368bfaaa9Sbostic 	++fs->lfs_nfiles;
1245dfd3931Smckusick 	return (0);
1255a6964aaSmckusick }
1265a6964aaSmckusick 
12768bfaaa9Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */
128ad1fdb78Sbostic int
lfs_vcreate(mp,ino,vpp)12983dca3beSbostic lfs_vcreate(mp, ino, vpp)
1304caa5015Sbostic 	struct mount *mp;
13183dca3beSbostic 	ino_t ino;
1324caa5015Sbostic 	struct vnode **vpp;
1335a6964aaSmckusick {
13499ddf719Sheideman 	extern int (**lfs_vnodeop_p)();
1354caa5015Sbostic 	struct inode *ip;
1364caa5015Sbostic 	struct ufsmount *ump;
13783dca3beSbostic 	int error, i;
1385a6964aaSmckusick 
13968bfaaa9Sbostic 	/* Create the vnode. */
14099ddf719Sheideman 	if (error = getnewvnode(VT_LFS, mp, lfs_vnodeop_p, vpp)) {
141356acbf4Smckusick 		*vpp = NULL;
14283dca3beSbostic 		return (error);
143356acbf4Smckusick 	}
14483dca3beSbostic 
14568bfaaa9Sbostic 	/* Get a pointer to the private mount structure. */
14683dca3beSbostic 	ump = VFSTOUFS(mp);
14783dca3beSbostic 
14883dca3beSbostic 	/* Initialize the inode. */
149356acbf4Smckusick 	MALLOC(ip, struct inode *, sizeof(struct inode), M_LFSNODE, M_WAITOK);
150*8a5f275bSmckusick 	lockinit(&ip->i_lock, PINOD, "lfsinode", 0, 0);
151356acbf4Smckusick 	(*vpp)->v_data = ip;
15283dca3beSbostic 	ip->i_vnode = *vpp;
153fe4f6ef3Sbostic 	ip->i_devvp = ump->um_devvp;
154db44b3c8Sbostic 	ip->i_flag = IN_MODIFIED;
1556e8e506dSmckusick 	ip->i_dev = ump->um_dev;
1569dfc7babSmargo 	ip->i_number = ip->i_din.di_inumber = ino;
1576e8e506dSmckusick 	ip->i_lfs = ump->um_lfs;
15883dca3beSbostic #ifdef QUOTA
15983dca3beSbostic 	for (i = 0; i < MAXQUOTAS; i++)
16083dca3beSbostic 		ip->i_dquot[i] = NODQUOT;
16183dca3beSbostic #endif
162fe4f6ef3Sbostic 	ip->i_lockf = 0;
163fe4f6ef3Sbostic 	ip->i_diroff = 0;
164fe4f6ef3Sbostic 	ip->i_mode = 0;
165fe4f6ef3Sbostic 	ip->i_size = 0;
166fa524dfbSbostic 	ip->i_blocks = 0;
167cfe7342fSbostic 	++ump->um_lfs->lfs_uinodes;
16883dca3beSbostic 	return (0);
1695a6964aaSmckusick }
170005e0680Sbostic 
17121a17e3bSbostic /* Free an inode. */
17221a17e3bSbostic /* ARGUSED */
173b05cacb6Sheideman int
lfs_vfree(ap)17499ddf719Sheideman lfs_vfree(ap)
1753713f046Smargo 	struct vop_vfree_args /* {
1763713f046Smargo 		struct vnode *a_pvp;
1773713f046Smargo 		ino_t a_ino;
1783713f046Smargo 		int a_mode;
1793713f046Smargo 	} */ *ap;
18068bfaaa9Sbostic {
18119c8f39cSstaelin 	SEGUSE *sup;
1824caa5015Sbostic 	struct buf *bp;
1834caa5015Sbostic 	struct ifile *ifp;
1844caa5015Sbostic 	struct inode *ip;
1853191b746Sbostic 	struct lfs *fs;
1866b8e5290Smckusick 	ufs_daddr_t old_iaddr;
18768bfaaa9Sbostic 	ino_t ino;
18868bfaaa9Sbostic 
18921a17e3bSbostic 	/* Get the inode number and file system. */
190f17f556eSbostic 	ip = VTOI(ap->a_pvp);
19168bfaaa9Sbostic 	fs = ip->i_lfs;
19268bfaaa9Sbostic 	ino = ip->i_number;
193db44b3c8Sbostic 	if (ip->i_flag & IN_MODIFIED) {
194cfe7342fSbostic 		--fs->lfs_uinodes;
195db44b3c8Sbostic 		ip->i_flag &=
196db44b3c8Sbostic 		    ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
197cfe7342fSbostic 	}
19821a17e3bSbostic 	/*
199035b5cddSbostic 	 * Set the ifile's inode entry to unused, increment its version number
200035b5cddSbostic 	 * and link it into the free chain.
20121a17e3bSbostic 	 */
20221a17e3bSbostic 	LFS_IENTRY(ifp, fs, ino, bp);
20319c8f39cSstaelin 	old_iaddr = ifp->if_daddr;
20421a17e3bSbostic 	ifp->if_daddr = LFS_UNUSED_DADDR;
20521a17e3bSbostic 	++ifp->if_version;
20621a17e3bSbostic 	ifp->if_nextfree = fs->lfs_free;
20721a17e3bSbostic 	fs->lfs_free = ino;
208cfe7342fSbostic 	(void) VOP_BWRITE(bp);
20921a17e3bSbostic 
21019c8f39cSstaelin 	if (old_iaddr != LFS_UNUSED_DADDR) {
21119c8f39cSstaelin 		LFS_SEGENTRY(sup, fs, datosn(fs, old_iaddr), bp);
21219c8f39cSstaelin #ifdef DIAGNOSTIC
21319c8f39cSstaelin 		if (sup->su_nbytes < sizeof(struct dinode))
21419c8f39cSstaelin 			panic("lfs_vfree: negative byte count (segment %d)\n",
21519c8f39cSstaelin 			    datosn(fs, old_iaddr));
21619c8f39cSstaelin #endif
21719c8f39cSstaelin 		sup->su_nbytes -= sizeof(struct dinode);
218cfe7342fSbostic 		(void) VOP_BWRITE(bp);
21919c8f39cSstaelin 	}
22019c8f39cSstaelin 
22121a17e3bSbostic 	/* Set superblock modified bit and decrement file count. */
22221a17e3bSbostic 	fs->lfs_fmod = 1;
22321a17e3bSbostic 	--fs->lfs_nfiles;
224b05cacb6Sheideman 	return (0);
22568bfaaa9Sbostic }
226