xref: /csrg-svn/sys/ufs/ffs/ufs_inode.c (revision 51987)
123399Smckusick /*
251509Sbostic  * Copyright (c) 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*51987Smckusick  *	@(#)ufs_inode.c	7.43 (Berkeley) 12/16/91
823399Smckusick  */
924Sbill 
1051509Sbostic #include <sys/param.h>
1151509Sbostic #include <sys/systm.h>
1251509Sbostic #include <sys/proc.h>
1351509Sbostic #include <sys/vnode.h>
1451509Sbostic #include <sys/mount.h>
1551509Sbostic #include <sys/kernel.h>
16*51987Smckusick #include <sys/malloc.h>
1724Sbill 
1851509Sbostic #include <ufs/ufs/quota.h>
1951509Sbostic #include <ufs/ufs/inode.h>
2051509Sbostic #include <ufs/ufs/ufsmount.h>
2151509Sbostic #include <ufs/ufs/ufs_extern.h>
2247571Skarels 
2351509Sbostic u_long	nextgennumber;		/* Next generation number to assign. */
2451547Smckusick int	prtactive = 0;		/* 1 => print out reclaim of active vnodes */
2524Sbill 
2651509Sbostic int
2739440Smckusick ufs_init()
2824Sbill {
2951509Sbostic 	static int first = 1;
3024Sbill 
3151509Sbostic 	if (!first)
3251509Sbostic 		return (0);
3351509Sbostic 	first = 0;
3451509Sbostic 
3551509Sbostic #ifdef DIAGNOSTIC
36*51987Smckusick 	if ((sizeof(struct inode) - 1) & sizeof(struct inode))
37*51987Smckusick 		printf("ufs_init: bad size %d\n", sizeof(struct inode));
3851509Sbostic #endif
3951509Sbostic 	ufs_ihashinit();
4041313Smckusick 	dqinit();
4137736Smckusick 	return (0);
4237736Smckusick }
437334Skre 
4437736Smckusick /*
4539392Smckusick  * Unlock and decrement the reference count of an inode structure.
4624Sbill  */
4751509Sbostic void
4851509Sbostic ufs_iput(ip)
494818Swnj 	register struct inode *ip;
5024Sbill {
517118Smckusick 
528452Sroot 	if ((ip->i_flag & ILOCKED) == 0)
537118Smckusick 		panic("iput");
5416665Smckusick 	IUNLOCK(ip);
5537736Smckusick 	vrele(ITOV(ip));
567118Smckusick }
577118Smckusick 
5839392Smckusick /*
5939392Smckusick  * Reclaim an inode so that it can be used for other purposes.
6024Sbill  */
6151509Sbostic int
6239392Smckusick ufs_reclaim(vp)
6339392Smckusick 	register struct vnode *vp;
6439392Smckusick {
6551509Sbostic 	register struct inode *ip;
66*51987Smckusick 	int i, type;
6739392Smckusick 
6839816Smckusick 	if (prtactive && vp->v_usecount != 0)
6939676Smckusick 		vprint("ufs_reclaim: pushing active", vp);
7039392Smckusick 	/*
7139392Smckusick 	 * Remove the inode from its hash chain.
7239392Smckusick 	 */
7351509Sbostic 	ip = VTOI(vp);
7439392Smckusick 	remque(ip);
7539392Smckusick 	/*
7639392Smckusick 	 * Purge old data structures associated with the inode.
7739392Smckusick 	 */
7839392Smckusick 	cache_purge(vp);
7939392Smckusick 	if (ip->i_devvp) {
8039392Smckusick 		vrele(ip->i_devvp);
8139392Smckusick 		ip->i_devvp = 0;
8239392Smckusick 	}
8339392Smckusick #ifdef QUOTA
8441313Smckusick 	for (i = 0; i < MAXQUOTAS; i++) {
8541313Smckusick 		if (ip->i_dquot[i] != NODQUOT) {
8641313Smckusick 			dqrele(vp, ip->i_dquot[i]);
8741313Smckusick 			ip->i_dquot[i] = NODQUOT;
8841313Smckusick 		}
8941313Smckusick 	}
9039392Smckusick #endif
91*51987Smckusick 	switch (vp->v_mount->mnt_stat.f_type) {
92*51987Smckusick 	case MOUNT_UFS:
93*51987Smckusick 		type = M_FFSNODE;
94*51987Smckusick 		break;
95*51987Smckusick 	case MOUNT_MFS:
96*51987Smckusick 		type = M_MFSNODE;
97*51987Smckusick 		break;
98*51987Smckusick 	case MOUNT_LFS:
99*51987Smckusick 		type = M_LFSNODE;
100*51987Smckusick 		break;
101*51987Smckusick 	default:
102*51987Smckusick 		panic("ufs_reclaim: not ufs file");
103*51987Smckusick 	}
104*51987Smckusick 	FREE(vp->v_data, type);
105*51987Smckusick 	vp->v_data = NULL;
10639392Smckusick 	return (0);
10739392Smckusick }
10839392Smckusick 
10939392Smckusick /*
1104818Swnj  * Lock an inode. If its already locked, set the WANT bit and sleep.
1113617Sroot  */
11251509Sbostic void
11351509Sbostic ufs_ilock(ip)
1144818Swnj 	register struct inode *ip;
1153617Sroot {
1163617Sroot 
11737736Smckusick 	while (ip->i_flag & ILOCKED) {
11837736Smckusick 		ip->i_flag |= IWANT;
119*51987Smckusick 		if (ip->i_lockholder == curproc->p_pid)
12039795Smckusick 			panic("locking against myself");
121*51987Smckusick 		ip->i_lockwaiter = curproc->p_pid;
12237736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
12337736Smckusick 	}
124*51987Smckusick 	ip->i_lockwaiter = 0;
125*51987Smckusick 	ip->i_lockholder = curproc->p_pid;
12637736Smckusick 	ip->i_flag |= ILOCKED;
1273617Sroot }
1283617Sroot 
1293617Sroot /*
1304818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
1313617Sroot  */
13251509Sbostic void
13351509Sbostic ufs_iunlock(ip)
1344818Swnj 	register struct inode *ip;
1353617Sroot {
1363617Sroot 
13737736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
13851509Sbostic 		vprint("ufs_iunlock: unlocked inode", ITOV(ip));
139*51987Smckusick 	ip->i_lockholder = 0;
14037736Smckusick 	ip->i_flag &= ~ILOCKED;
14137736Smckusick 	if (ip->i_flag&IWANT) {
14237736Smckusick 		ip->i_flag &= ~IWANT;
14337736Smckusick 		wakeup((caddr_t)ip);
14437736Smckusick 	}
1453617Sroot }
146