xref: /csrg-svn/sys/nfs/nfs_node.c (revision 38425)
138415Smckusick /*
238415Smckusick  * Copyright (c) 1989 The Regents of the University of California.
338415Smckusick  * All rights reserved.
438415Smckusick  *
538415Smckusick  * This code is derived from software contributed to Berkeley by
638415Smckusick  * Rick Macklem at The University of Guelph.
738415Smckusick  *
838415Smckusick  * Redistribution and use in source and binary forms are permitted
938415Smckusick  * provided that the above copyright notice and this paragraph are
1038415Smckusick  * duplicated in all such forms and that any documentation,
1138415Smckusick  * advertising materials, and other materials related to such
1238415Smckusick  * distribution and use acknowledge that the software was developed
1338415Smckusick  * by the University of California, Berkeley.  The name of the
1438415Smckusick  * University may not be used to endorse or promote products derived
1538415Smckusick  * from this software without specific prior written permission.
1638415Smckusick  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1738415Smckusick  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1838415Smckusick  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1938415Smckusick  *
20*38425Smckusick  *	@(#)nfs_node.c	7.3 (Berkeley) 07/06/89
2138415Smckusick  */
2238415Smckusick 
2338415Smckusick #include "param.h"
2438415Smckusick #include "systm.h"
2538415Smckusick #include "user.h"
2638415Smckusick #include "proc.h"
2738415Smckusick #include "mount.h"
2838415Smckusick #include "vnode.h"
29*38425Smckusick #include "../ufs/dir.h"
3038415Smckusick #include "namei.h"
3138415Smckusick #include "errno.h"
3238415Smckusick #include "nfsv2.h"
3338415Smckusick #include "nfs.h"
3438415Smckusick #include "nfsnode.h"
3538415Smckusick #include "nfsmount.h"
3638415Smckusick #include "kernel.h"
3738415Smckusick #include "malloc.h"
3838415Smckusick 
3938415Smckusick /* The request list head */
4038415Smckusick extern struct nfsreq nfsreqh;
4138415Smckusick 
4238415Smckusick #define	NFSNOHSZ	512
4338415Smckusick #if	((NFSNOHSZ&(NFSNOHSZ-1)) == 0)
4438415Smckusick #define	NFSNOHASH(fhsum)	((fhsum)&(NFSNOHSZ-1))
4538415Smckusick #else
4638415Smckusick #define	NFSNOHASH(fhsum)	(((unsigned)(fhsum))%NFSNOHSZ)
4738415Smckusick #endif
4838415Smckusick 
4938415Smckusick union nhead {				/* inode LRU cache, Chris Maltby */
5038415Smckusick 	union  nhead *nh_head[2];
5138415Smckusick 	struct nfsnode *nh_chain[2];
5238415Smckusick } nhead[NFSNOHSZ];
5338415Smckusick 
5438415Smckusick struct nfsnode *nfreeh, **nfreet;
5538415Smckusick 
5638415Smckusick /*
5738415Smckusick  * Initialize hash links for nfsnodes
5838415Smckusick  * and build nfsnode free list.
5938415Smckusick  */
6038415Smckusick nfs_nhinit()
6138415Smckusick {
6238415Smckusick 	register int i;
6338415Smckusick 	register struct nfsnode *np = nfsnode;
6438415Smckusick 	register union  nhead *nh = nhead;
6538415Smckusick 
6638415Smckusick 	for (i = NFSNOHSZ; --i >= 0; nh++) {
6738415Smckusick 		nh->nh_head[0] = nh;
6838415Smckusick 		nh->nh_head[1] = nh;
6938415Smckusick 	}
7038415Smckusick 	nfreeh = np;
7138415Smckusick 	nfreet = &np->n_freef;
7238415Smckusick 	np->n_freeb = &nfreeh;
7338415Smckusick 	np->n_forw = np;
7438415Smckusick 	np->n_back = np;
7538415Smckusick 	NFSTOV(np)->v_data = (qaddr_t)np;
7638415Smckusick 	for (i = nnfsnode; --i > 0; ) {
7738415Smckusick 		++np;
7838415Smckusick 		np->n_forw = np;
7938415Smckusick 		np->n_back = np;
8038415Smckusick 		NFSTOV(np)->v_data = (qaddr_t)np;
8138415Smckusick 		*nfreet = np;
8238415Smckusick 		np->n_freeb = nfreet;
8338415Smckusick 		nfreet = &np->n_freef;
8438415Smckusick 	}
8538415Smckusick 	np->n_freef = NULL;
8638415Smckusick }
8738415Smckusick 
8838415Smckusick /*
8938415Smckusick  * Look up an vnode/nfsnode by file handle.
9038415Smckusick  * Callers must check for mount points!!
9138415Smckusick  * In all cases, a pointer to a
9238415Smckusick  * nfsnode structure is returned.
9338415Smckusick  */
9438415Smckusick nfs_nget(mntp, fhp, npp)
9538415Smckusick 	struct mount *mntp;
9638415Smckusick 	register nfsv2fh_t *fhp;
9738415Smckusick 	struct nfsnode **npp;
9838415Smckusick {
9938415Smckusick 	register struct nfsnode *np;
10038415Smckusick 	register struct vnode *vp;
10138415Smckusick 	register struct nfsnode *nq;
10238415Smckusick 	register u_char *fhpp;
10338415Smckusick 	register u_long fhsum;
10438415Smckusick 	register int i;
10538415Smckusick 	union  nhead *nh;
10638415Smckusick 	int error;
10738415Smckusick 
10838415Smckusick 	fhpp = &fhp->fh_bytes[0];
10938415Smckusick 	fhsum = 0;
11038415Smckusick 	for (i = 0; i < NFSX_FH; i++)
11138415Smckusick 		fhsum += *fhpp++;
11238415Smckusick loop:
11338415Smckusick 	nh = &nhead[NFSNOHASH(fhsum)];
11438415Smckusick 	for (np = nh->nh_chain[0]; np != (struct nfsnode *)nh; np = np->n_forw)
11538415Smckusick 		if (mntp == NFSTOV(np)->v_mount &&
11638415Smckusick 		    !bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH)) {
11738415Smckusick 			/*
11838415Smckusick 			 * Following is essentially an inline expanded
11938415Smckusick 			 * copy of ngrab(), expanded inline for speed,
12038415Smckusick 			 * and so that the test for a mounted on nfsnode
12138415Smckusick 			 * can be deferred until after we are sure that
12238415Smckusick 			 * the nfsnode isn't busy.
12338415Smckusick 			 */
12438415Smckusick 			if ((np->n_flag & NLOCKED) != 0) {
12538415Smckusick 				np->n_flag |= NWANT;
12638415Smckusick 				sleep((caddr_t)np, PINOD);
12738415Smckusick 				goto loop;
12838415Smckusick 			}
12938415Smckusick 			vp = NFSTOV(np);
13038415Smckusick 			if (vp->v_count == 0) {		/* nfsno on free list */
13138415Smckusick 				if (nq = np->n_freef)
13238415Smckusick 					nq->n_freeb = np->n_freeb;
13338415Smckusick 				else
13438415Smckusick 					nfreet = np->n_freeb;
13538415Smckusick 				*np->n_freeb = nq;
13638415Smckusick 				np->n_freef = NULL;
13738415Smckusick 				np->n_freeb = NULL;
13838415Smckusick 			}
13938415Smckusick 			np->n_flag |= NLOCKED;
140*38425Smckusick 			VREF(vp);
14138415Smckusick 			*npp = np;
14238415Smckusick 			return(0);
14338415Smckusick 		}
14438415Smckusick 
14538415Smckusick 	if ((np = nfreeh) == NULL) {
14638415Smckusick 		tablefull("nfsnode");
14738415Smckusick 		*npp = 0;
14838415Smckusick 		return(ENFILE);
14938415Smckusick 	}
15038415Smckusick 	vp = NFSTOV(np);
15138415Smckusick 	if (vp->v_count)
15238415Smckusick 		panic("free nfsnode isn't");
15338415Smckusick 	if (nq = np->n_freef)
15438415Smckusick 		nq->n_freeb = &nfreeh;
15538415Smckusick 	nfreeh = nq;
15638415Smckusick 	np->n_freef = NULL;
15738415Smckusick 	np->n_freeb = NULL;
15838415Smckusick 	/*
15938415Smckusick 	 * Now to take nfsnode off the hash chain it was on
16038415Smckusick 	 * (initially, or after an nflush, it is on a "hash chain"
16138415Smckusick 	 * consisting entirely of itself, and pointed to by no-one,
16238415Smckusick 	 * but that doesn't matter), and put it on the chain for
16338415Smckusick 	 * its new file handle
16438415Smckusick 	 */
16538415Smckusick 	remque(np);
16638415Smckusick 	insque(np, nh);
16738415Smckusick 	bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
16838415Smckusick #ifdef notyet
16938415Smckusick 	cache_purge(vp);
17038415Smckusick #endif
17138415Smckusick 	np->n_flag = NLOCKED;
17238415Smckusick 	np->n_attrstamp = 0;
17338415Smckusick 	np->n_sillyrename = (struct sillyrename *)0;
17438415Smckusick 	np->n_id = ++nextnfsnodeid;
17538415Smckusick 	/*
17638415Smckusick 	 * Initialize the associated vnode
17738415Smckusick 	 */
17838415Smckusick 	vinit(vp, mntp, VNON, &nfsv2_vnodeops);
17938415Smckusick 	*npp = np;
18038415Smckusick 	return (0);
18138415Smckusick }
18238415Smckusick 
18338415Smckusick /*
18438415Smckusick  * Convert a pointer to an nfsnode into a reference to an nfsnode.
18538415Smckusick  *
18638415Smckusick  * This is basically the internal piece of nget (after the
18738415Smckusick  * nfsnode pointer is located) but without the test for mounted
18838415Smckusick  * filesystems.  It is caller's responsibility to check that
18938415Smckusick  * the nfsnode pointer is valid.
19038415Smckusick  */
19138415Smckusick nfs_ngrab(np)
19238415Smckusick 	register struct nfsnode *np;
19338415Smckusick {
19438415Smckusick 	register struct vnode *vp = NFSTOV(np);
19538415Smckusick 
19638415Smckusick 	while ((np->n_flag & NLOCKED) != 0) {
19738415Smckusick 		np->n_flag |= NWANT;
19838415Smckusick 		sleep((caddr_t)np, PINOD);
19938415Smckusick 	}
20038415Smckusick 	if (vp->v_count == 0) {		/* ino on free list */
20138415Smckusick 		register struct nfsnode *nq;
20238415Smckusick 
20338415Smckusick 		if (nq = np->n_freef)
20438415Smckusick 			nq->n_freeb = np->n_freeb;
20538415Smckusick 		else
20638415Smckusick 			nfreet = np->n_freeb;
20738415Smckusick 		*np->n_freeb = nq;
20838415Smckusick 		np->n_freef = NULL;
20938415Smckusick 		np->n_freeb = NULL;
21038415Smckusick 	}
211*38425Smckusick 	VREF(vp);
21238415Smckusick 	np->n_flag |= NLOCKED;
21338415Smckusick }
21438415Smckusick 
21538415Smckusick nfs_inactive(vp)
21638415Smckusick 	struct vnode *vp;
21738415Smckusick {
21838415Smckusick 	register struct nfsnode *np;
21938415Smckusick 	register struct nameidata *ndp;
22038415Smckusick 	register struct sillyrename *sp;
22138415Smckusick 	register struct nfsreq *rep;
22238415Smckusick 	struct nfsreq *rep2;
22338415Smckusick 	struct nfsnode *dnp;
22438415Smckusick 	int s;
22538415Smckusick 
22638415Smckusick 	if (vp == NULL)
22738415Smckusick 		panic("nfs_inactive NULL vp");
22838415Smckusick 	if (vp->v_count == 0) {
22938415Smckusick 		np = VTONFS(vp);
23038415Smckusick 		np->n_flag |= NLOCKED;
23138415Smckusick 		if (np->n_sillyrename) {
23238415Smckusick 			/*
23338415Smckusick 			 * Remove the silly file that was rename'd earlier
23438415Smckusick 			 */
23538415Smckusick 			sp = np->n_sillyrename;
23638415Smckusick 			ndp = &sp->s_namei;
23738415Smckusick 			if (!nfs_nget(vp->v_mount, &sp->s_fh, &dnp)) {
23838415Smckusick 				ndp->ni_dvp = NFSTOV(dnp);
23938415Smckusick 				if (sp->s_flag == REMOVE)
24038415Smckusick 					nfs_removeit(ndp);
24138415Smckusick 				else
24238415Smckusick 					nfs_rmdirit(ndp);
24338415Smckusick 				nfs_nput(ndp->ni_dvp);
24438415Smckusick 			}
24538415Smckusick 			crfree(ndp->ni_cred);
24638415Smckusick 			free((caddr_t)sp, M_TEMP);
24738415Smckusick 			np->n_sillyrename = (struct sillyrename *)0;
24838415Smckusick 		}
24938415Smckusick 		nfs_unlock(vp);
25038415Smckusick 		np->n_flag = 0;
25138415Smckusick 		/*
25238415Smckusick 		 * Scan the request list for any requests left hanging about
25338415Smckusick 		 */
25438415Smckusick 		s = splnet();
25538415Smckusick 		rep = nfsreqh.r_next;
25638415Smckusick 		while (rep) {
25738415Smckusick 			if (rep->r_vp == vp) {
25838415Smckusick 				rep->r_prev->r_next = rep2 = rep->r_next;
25938415Smckusick 				if (rep->r_next != NULL)
26038415Smckusick 					rep->r_next->r_prev = rep->r_prev;
26138415Smckusick 				m_freem(rep->r_mreq);
26238415Smckusick 				if (rep->r_mrep != NULL)
26338415Smckusick 					m_freem(rep->r_mrep);
26438415Smckusick 				free((caddr_t)rep, M_NFSREQ);
26538415Smckusick 				rep = rep2;
26638415Smckusick 			} else
26738415Smckusick 				rep = rep->r_next;
26838415Smckusick 		}
26938415Smckusick 		splx(s);
27038415Smckusick 		/*
27138415Smckusick 		 * Put the nfsnode on the end of the free list.
27238415Smckusick 		 */
27338415Smckusick 		if (nfreeh) {
27438415Smckusick 			*nfreet = np;
27538415Smckusick 			np->n_freeb = nfreet;
27638415Smckusick 		} else {
27738415Smckusick 			nfreeh = np;
27838415Smckusick 			np->n_freeb = &nfreeh;
27938415Smckusick 		}
28038415Smckusick 		np->n_freef = NULL;
28138415Smckusick 		nfreet = &np->n_freef;
28238415Smckusick 	}
28338415Smckusick 	return (0);
28438415Smckusick }
28538415Smckusick 
28638415Smckusick /*
28738415Smckusick  * Remove any nfsnodes in the nfsnode cache belonging to mount.
28838415Smckusick  *
28938415Smckusick  * There should not be any active ones, return error if any are found
29038415Smckusick  * (nb: this is a user error, not a system err).
29138415Smckusick  */
29238415Smckusick nfs_nflush(mntp)
29338415Smckusick 	struct mount *mntp;
29438415Smckusick {
29538415Smckusick 	register struct nfsnode *np;
29638415Smckusick 	register struct vnode *vp;
29738415Smckusick 
29838415Smckusick 	for (np = nfsnode; np < nfsnodeNNFSNODE; np++) {
29938415Smckusick 		vp = NFSTOV(np);
30038415Smckusick 		if (vp->v_mount == mntp)
30138415Smckusick 			if (vp->v_count)
30238415Smckusick 				return (EBUSY);
30338415Smckusick 			else {
30438415Smckusick 				remque(np);
30538415Smckusick 				np->n_forw = np;
30638415Smckusick 				np->n_back = np;
30738415Smckusick 				/*
30838415Smckusick 				 * as v_count == 0, the inode was on the free
30938415Smckusick 				 * list already, just leave it there, it will
31038415Smckusick 				 * fall off the bottom eventually. We could
31138415Smckusick 				 * perhaps move it to the head of the free
31238415Smckusick 				 * list, but as umounts are done so
31338415Smckusick 				 * infrequently, we would gain very little,
31438415Smckusick 				 * while making the code bigger.
31538415Smckusick 				 */
31638415Smckusick 			}
31738415Smckusick 	}
31838415Smckusick 	return (0);
31938415Smckusick }
32038415Smckusick 
32138415Smckusick /*
32238415Smckusick  * Lock an nfsnode
32338415Smckusick  */
32438415Smckusick nfs_lock(vp)
32538415Smckusick 	struct vnode *vp;
32638415Smckusick {
32738415Smckusick 	register struct nfsnode *np = VTONFS(vp);
32838415Smckusick 
32938415Smckusick 	if (np->n_flag & NLOCKED)
33038415Smckusick 		printf("pid %d hit locked nfsnode=0x%x\n",
33138415Smckusick 		    u.u_procp->p_pid, np);
33238415Smckusick 	while (np->n_flag & NLOCKED) {
33338415Smckusick 		np->n_flag |= NWANT;
33438415Smckusick 		sleep((caddr_t)np, PINOD);
33538415Smckusick 	}
33638415Smckusick 	np->n_flag |= NLOCKED;
33738415Smckusick }
33838415Smckusick 
33938415Smckusick /*
34038415Smckusick  * Unlock an nfsnode
34138415Smckusick  */
34238415Smckusick nfs_unlock(vp)
34338415Smckusick 	struct vnode *vp;
34438415Smckusick {
34538415Smckusick 	register struct nfsnode *np = VTONFS(vp);
34638415Smckusick 
34738415Smckusick 	if ((np->n_flag & NLOCKED) == 0) {
34838415Smckusick 		printf("pid %d unlocking unlocked nfsnode=0x%x ",
34938415Smckusick 		    u.u_procp->p_pid, np);
35038415Smckusick 		printf("fh0=0x%x fh1=0x%x fh2=0x%x fh3=0x%x fh4=0x%x fh5=0x%x fh6=0x%x fh7=0x%x\n",
35138415Smckusick 			np->n_fh.fh_bytes[0],np->n_fh.fh_bytes[1],
35238415Smckusick 			np->n_fh.fh_bytes[2],np->n_fh.fh_bytes[3],
35338415Smckusick 			np->n_fh.fh_bytes[4],np->n_fh.fh_bytes[5],
35438415Smckusick 			np->n_fh.fh_bytes[6],np->n_fh.fh_bytes[7]);
35538415Smckusick 	}
35638415Smckusick 	np->n_flag &= ~NLOCKED;
35738415Smckusick 	if (np->n_flag & NWANT) {
35838415Smckusick 		np->n_flag &= ~NWANT;
35938415Smckusick 		wakeup((caddr_t)np);
36038415Smckusick 	}
36138415Smckusick }
36238415Smckusick 
36338415Smckusick /*
36438415Smckusick  * Unlock and vrele()
36538415Smckusick  * since I can't decide if dirs. should be locked, I will check for
36638415Smckusick  * the lock and be flexible
36738415Smckusick  */
36838415Smckusick nfs_nput(vp)
36938415Smckusick 	struct vnode *vp;
37038415Smckusick {
37138415Smckusick 	register struct nfsnode *np = VTONFS(vp);
37238415Smckusick 
37338415Smckusick 	if (np->n_flag & NLOCKED)
37438415Smckusick 		nfs_unlock(vp);
37538415Smckusick 	vrele(vp);
37638415Smckusick }
37738415Smckusick 
37838415Smckusick nfs_abortop(ndp)
37938415Smckusick 	register struct nameidata *ndp;
38038415Smckusick {
38138415Smckusick 	register struct nfsnode *np;
38238415Smckusick 
38338415Smckusick 	if (ndp->ni_vp != NULL) {
38438415Smckusick 		np = VTONFS(ndp->ni_vp);
38538415Smckusick 		if (np->n_flag & NLOCKED)
38638415Smckusick 			nfs_unlock(ndp->ni_vp);
38738415Smckusick 		vrele(ndp->ni_vp);
38838415Smckusick 	}
38938415Smckusick 	if (ndp->ni_dvp != NULL) {
39038415Smckusick 		np = VTONFS(ndp->ni_dvp);
39138415Smckusick 		if (np->n_flag & NLOCKED)
39238415Smckusick 			nfs_unlock(ndp->ni_dvp);
39338415Smckusick 		vrele(ndp->ni_dvp);
39438415Smckusick 	}
39538415Smckusick }
39638415Smckusick 
39738415Smckusick /*
39838415Smckusick  * This is silly, but if you use a macro and try and use it in a file
39938415Smckusick  * that has mbuf.h included, m_data --> m_hdr.mh_data and this is not
40038415Smckusick  * a good thing
40138415Smckusick  */
40238415Smckusick struct nfsmount *vfs_to_nfs(mp)
40338415Smckusick 	struct mount *mp;
40438415Smckusick {
40538415Smckusick 	return ((struct nfsmount *)mp->m_data);
40638415Smckusick }
407