xref: /csrg-svn/sys/ufs/lfs/lfs_alloc.c (revision 52080)
123394Smckusick /*
251492Sbostic  * Copyright (c) 1991 Regents of the University of California.
334432Sbostic  * All rights reserved.
423394Smckusick  *
544536Sbostic  * %sccs.include.redist.c%
634432Sbostic  *
7*52080Sbostic  *	@(#)lfs_alloc.c	7.39 (Berkeley) 12/31/91
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 
1851492Sbostic #include <ufs/ufs/quota.h>
1951492Sbostic #include <ufs/ufs/inode.h>
2051492Sbostic #include <ufs/ufs/ufsmount.h>
2147571Skarels 
2251492Sbostic #include <ufs/lfs/lfs.h>
2351492Sbostic #include <ufs/lfs/lfs_extern.h>
2451485Sbostic 
2551492Sbostic extern u_long nextgennumber;
2651492Sbostic 
2751485Sbostic /* Allocate a new inode. */
2851485Sbostic /* ARGSUSED */
2951485Sbostic int
3051560Smckusick lfs_valloc(pvp, notused, cred, vpp)
31*52080Sbostic 	struct vnode *pvp, **vpp;
3251485Sbostic 	int notused;
33*52080Sbostic 	struct ucred *cred;
3451155Sbostic {
3551492Sbostic 	struct lfs *fs;
36*52080Sbostic 	struct buf *bp;
37*52080Sbostic 	struct ifile *ifp;
38*52080Sbostic 	struct inode *ip;
39*52080Sbostic 	struct vnode *vp;
40*52080Sbostic 	daddr_t blkno;
4151155Sbostic 	ino_t new_ino;
42*52080Sbostic 	u_long i, max;
4351155Sbostic 	int error;
444359Smckusick 
4551854Sbostic #ifdef VERBOSE
4651854Sbostic 	printf("lfs_valloc\n");
4751854Sbostic #endif
4851215Sbostic 	/* Get the head of the freelist. */
4951560Smckusick 	fs = VTOI(pvp)->i_lfs;
5051155Sbostic 	new_ino = fs->lfs_free;
5151485Sbostic #ifdef ALLOCPRINT
5251485Sbostic 	printf("lfs_ialloc: allocate inode %d\n", new_ino);
5351485Sbostic #endif
544359Smckusick 
5551933Sbostic 	/*
56*52080Sbostic 	 * Remove the inode from the free list and write the new start
57*52080Sbostic 	 * of the free list into the superblock.
5851933Sbostic 	 */
5951155Sbostic 	LFS_IENTRY(ifp, fs, new_ino, bp);
6051155Sbostic 	if (ifp->if_daddr != LFS_UNUSED_DADDR)
6151215Sbostic 		panic("lfs_ialloc: inuse inode on the free list");
6251183Sbostic 	fs->lfs_free = ifp->if_nextfree;
63*52080Sbostic 	brelse(bp);
644426Smckusic 
65*52080Sbostic 	/* Extend IFILE so that the next lfs_valloc will succeed. */
66*52080Sbostic 	if (fs->lfs_free == LFS_UNUSED_INUM) {
67*52080Sbostic 		vp = fs->lfs_ivnode;
68*52080Sbostic 		ip = VTOI(vp);
69*52080Sbostic 		blkno = lblkno(fs, ip->i_size);
70*52080Sbostic printf("Extending ifile: blkno = %d\n", blkno);
71*52080Sbostic 		bp = getblk(vp, blkno, fs->lfs_bsize);
72*52080Sbostic 		if (!bp) {
73*52080Sbostic 			uprintf("\n%s: no inodes left\n", fs->lfs_fsmnt);
74*52080Sbostic 			log(LOG_ERR, "uid %d on %s: out of inodes\n",
75*52080Sbostic 			    cred->cr_uid, fs->lfs_fsmnt);
76*52080Sbostic 			return (ENOSPC);
77*52080Sbostic 		}
78*52080Sbostic 		i = (blkno - fs->lfs_segtabsz - fs->lfs_cleansz) *
79*52080Sbostic 		    fs->lfs_ifpb;
80*52080Sbostic printf("Extending ifile: first inum = %d\n", i);
81*52080Sbostic 		fs->lfs_free = i;
82*52080Sbostic 		max = i + fs->lfs_ifpb;
83*52080Sbostic printf("Extending ifile: max inum = %d\n", max);
84*52080Sbostic 		for (ifp = (struct ifile *)bp->b_un.b_words; i < max; ++ifp) {
85*52080Sbostic 			ifp->if_version = 1;
86*52080Sbostic 			ifp->if_daddr = LFS_UNUSED_DADDR;
87*52080Sbostic 			ifp->if_nextfree = ++i;
88*52080Sbostic 		}
89*52080Sbostic 		ifp--;
90*52080Sbostic 		ifp->if_nextfree = LFS_UNUSED_INUM;
91*52080Sbostic 
92*52080Sbostic 		++ip->i_blocks;			/* XXX This may not be right. */
93*52080Sbostic 		ip->i_size += fs->lfs_bsize;
94*52080Sbostic printf("Extending ifile: blocks = %d size = %d\n", ip->i_blocks, ip->i_size);
95*52080Sbostic 		vnode_pager_setsize(vp, ip->i_size);
96*52080Sbostic 		vnode_pager_uncache(vp);
97*52080Sbostic 		LFS_UBWRITE(bp);
98*52080Sbostic 	}
99*52080Sbostic 
10051215Sbostic 	/* Create a vnode to associate with the inode. */
10151560Smckusick 	if (error = lfs_vcreate(pvp->v_mount, new_ino, &vp))
10237735Smckusick 		return (error);
10351560Smckusick 	*vpp = vp;
10451560Smckusick 	ip = VTOI(vp);
10551560Smckusick 	VREF(ip->i_devvp);
10651155Sbostic 
107*52080Sbostic 	/* Zero out the direct and indirect block addresses. */
108*52080Sbostic 	bzero(ip->i_db, (NDADDR + NIADDR) * sizeof(daddr_t));
109*52080Sbostic 
11051215Sbostic 	/* Set a new generation number for this inode. */
11138255Smckusick 	if (++nextgennumber < (u_long)time.tv_sec)
11238255Smckusick 		nextgennumber = time.tv_sec;
11338255Smckusick 	ip->i_gen = nextgennumber;
1144359Smckusick 
11551215Sbostic 	/* Insert into the inode hash table. */
11651485Sbostic 	ufs_ihashins(ip);
1174359Smckusick 
11851215Sbostic 	/* Set superblock modified bit and increment file count. */
11951215Sbostic 	fs->lfs_fmod = 1;
12051215Sbostic 	++fs->lfs_nfiles;
12151155Sbostic 	return (0);
1224359Smckusick }
1234359Smckusick 
12451215Sbostic /* Create a new vnode/inode pair and initialize what fields we can. */
12551347Sbostic int
12651155Sbostic lfs_vcreate(mp, ino, vpp)
127*52080Sbostic 	struct mount *mp;
12851155Sbostic 	ino_t ino;
129*52080Sbostic 	struct vnode **vpp;
1304359Smckusick {
13151560Smckusick 	extern struct vnodeops lfs_vnodeops;
132*52080Sbostic 	struct inode *ip;
133*52080Sbostic 	struct ufsmount *ump;
13451155Sbostic 	int error, i;
1354359Smckusick 
13651854Sbostic #ifdef VERBOSE
13751485Sbostic 	printf("lfs_vcreate: ino %d\n", ino);
13851485Sbostic #endif
13951215Sbostic 	/* Create the vnode. */
14051990Smckusick 	if (error = getnewvnode(VT_LFS, mp, &lfs_vnodeops, 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);
15051990Smckusick 	(*vpp)->v_data = ip;
15151560Smckusick 	ip->i_vnode = *vpp;
15251560Smckusick 	ip->i_flag = 0;
15351560Smckusick 	ip->i_mode = 0;
15451155Sbostic 	ip->i_diroff = 0;
15551560Smckusick 	ip->i_lockf = 0;
15651155Sbostic 	ip->i_dev = ump->um_dev;
15751560Smckusick 	ip->i_number = ip->i_din.di_inum = ino;
15851155Sbostic 	ip->i_lfs = ump->um_lfs;
15951560Smckusick 	ip->i_devvp = ump->um_devvp;
16051155Sbostic #ifdef QUOTA
16151155Sbostic 	for (i = 0; i < MAXQUOTAS; i++)
16251155Sbostic 		ip->i_dquot[i] = NODQUOT;
16351155Sbostic #endif
16451155Sbostic 	return (0);
1654651Smckusic }
16651183Sbostic 
16751485Sbostic /* Free an inode. */
16851485Sbostic /* ARGUSED */
16951215Sbostic void
17051560Smckusick lfs_vfree(vp, notused1, notused2)
171*52080Sbostic 	struct vnode *vp;
17251485Sbostic 	ino_t notused1;
17351485Sbostic 	int notused2;
17451215Sbostic {
175*52080Sbostic 	struct buf *bp;
176*52080Sbostic 	struct ifile *ifp;
177*52080Sbostic 	struct inode *ip;
17851492Sbostic 	struct lfs *fs;
17951215Sbostic 	ino_t ino;
18051215Sbostic 
18151560Smckusick 	ip = VTOI(vp);
18251854Sbostic #ifdef VERBOSE
18351854Sbostic 	printf("lfs_vfree: free %d\n", ip->i_number);
18451485Sbostic #endif
18551485Sbostic 	/* Get the inode number and file system. */
18651215Sbostic 	fs = ip->i_lfs;
18751215Sbostic 	ino = ip->i_number;
18851485Sbostic 
18951485Sbostic 	/*
19051933Sbostic 	 * Set the ifile's inode entry to unused, increment its version number
19151933Sbostic 	 * and link it into the free chain.
19251485Sbostic 	 */
19351215Sbostic 	LFS_IENTRY(ifp, fs, ino, bp);
19451485Sbostic 	ifp->if_daddr = LFS_UNUSED_DADDR;
19551485Sbostic 	++ifp->if_version;
19651485Sbostic 	ifp->if_nextfree = fs->lfs_free;
19751485Sbostic 	fs->lfs_free = ino;
198*52080Sbostic 	LFS_UBWRITE(bp);
19951215Sbostic 
20051485Sbostic 	/* Set superblock modified bit and decrement file count. */
20151485Sbostic 	fs->lfs_fmod = 1;
20251485Sbostic 	--fs->lfs_nfiles;
20351215Sbostic }
204