xref: /csrg-svn/sys/ufs/ffs/ufs_inode.c (revision 52037)
123399Smckusick /*
251509Sbostic  * Copyright (c) 1991 Regents of the University of California.
337736Smckusick  * All rights reserved.
423399Smckusick  *
544537Sbostic  * %sccs.include.redist.c%
637736Smckusick  *
7*52037Smckusick  *	@(#)ufs_inode.c	7.44 (Berkeley) 12/19/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>
1651987Smckusick #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
3651987Smckusick 	if ((sizeof(struct inode) - 1) & sizeof(struct inode))
3751987Smckusick 		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;
6651987Smckusick 	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
9151987Smckusick 	switch (vp->v_mount->mnt_stat.f_type) {
9251987Smckusick 	case MOUNT_UFS:
9351987Smckusick 		type = M_FFSNODE;
9451987Smckusick 		break;
9551987Smckusick 	case MOUNT_MFS:
9651987Smckusick 		type = M_MFSNODE;
9751987Smckusick 		break;
9851987Smckusick 	case MOUNT_LFS:
9951987Smckusick 		type = M_LFSNODE;
10051987Smckusick 		break;
10151987Smckusick 	default:
10251987Smckusick 		panic("ufs_reclaim: not ufs file");
10351987Smckusick 	}
10451987Smckusick 	FREE(vp->v_data, type);
10551987Smckusick 	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 {
116*52037Smckusick 	struct proc *p = curproc;	/* XXX */
1173617Sroot 
11837736Smckusick 	while (ip->i_flag & ILOCKED) {
11937736Smckusick 		ip->i_flag |= IWANT;
120*52037Smckusick #ifdef DIAGNOSTIC
121*52037Smckusick 		if (p) {
122*52037Smckusick 			if (p->p_pid == ip->i_lockholder)
123*52037Smckusick 				panic("locking against myself");
124*52037Smckusick 			ip->i_lockwaiter = p->p_pid;
125*52037Smckusick 		}
126*52037Smckusick #endif
12737736Smckusick 		(void) sleep((caddr_t)ip, PINOD);
12837736Smckusick 	}
129*52037Smckusick #ifdef DIAGNOSTIC
13051987Smckusick 	ip->i_lockwaiter = 0;
131*52037Smckusick 	if (p)
132*52037Smckusick 		ip->i_lockholder = p->p_pid;
133*52037Smckusick #endif
13437736Smckusick 	ip->i_flag |= ILOCKED;
1353617Sroot }
1363617Sroot 
1373617Sroot /*
1384818Swnj  * Unlock an inode.  If WANT bit is on, wakeup.
1393617Sroot  */
14051509Sbostic void
14151509Sbostic ufs_iunlock(ip)
1424818Swnj 	register struct inode *ip;
1433617Sroot {
1443617Sroot 
14537736Smckusick 	if ((ip->i_flag & ILOCKED) == 0)
14651509Sbostic 		vprint("ufs_iunlock: unlocked inode", ITOV(ip));
147*52037Smckusick #ifdef DIAGNOSTIC
14851987Smckusick 	ip->i_lockholder = 0;
149*52037Smckusick #endif
15037736Smckusick 	ip->i_flag &= ~ILOCKED;
15137736Smckusick 	if (ip->i_flag&IWANT) {
15237736Smckusick 		ip->i_flag &= ~IWANT;
15337736Smckusick 		wakeup((caddr_t)ip);
15437736Smckusick 	}
1553617Sroot }
156