xref: /netbsd-src/sys/nfs/nfs_node.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: nfs_node.c,v 1.29 1999/07/08 01:06:03 wrstuden Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
39  */
40 
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/pool.h>
51 #include <sys/lock.h>
52 
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfsnode.h>
57 #include <nfs/nfsmount.h>
58 #include <nfs/nqnfs.h>
59 #include <nfs/nfs_var.h>
60 
61 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
62 u_long nfsnodehash;
63 struct lock nfs_hashlock;
64 
65 struct pool nfs_node_pool;		/* memory pool for nfs nodes */
66 struct pool nfs_vattr_pool;		/* memory pool for nfs vattrs */
67 
68 #define TRUE	1
69 #define	FALSE	0
70 
71 /*
72  * Initialize hash links for nfsnodes
73  * and build nfsnode free list.
74  */
75 void
76 nfs_nhinit()
77 {
78 
79 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, M_WAITOK, &nfsnodehash);
80 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
81 
82 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
83 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
84 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
85 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
86 }
87 
88 /*
89  * Compute an entry in the NFS hash table structure
90  */
91 u_long
92 nfs_hash(fhp, fhsize)
93 	register nfsfh_t *fhp;
94 	int fhsize;
95 {
96 	register u_char *fhpp;
97 	register u_long fhsum;
98 	register int i;
99 
100 	fhpp = &fhp->fh_bytes[0];
101 	fhsum = 0;
102 	for (i = 0; i < fhsize; i++)
103 		fhsum += *fhpp++;
104 	return (fhsum);
105 }
106 
107 /*
108  * Look up a vnode/nfsnode by file handle.
109  * Callers must check for mount points!!
110  * In all cases, a pointer to a
111  * nfsnode structure is returned.
112  */
113 int
114 nfs_nget(mntp, fhp, fhsize, npp)
115 	struct mount *mntp;
116 	register nfsfh_t *fhp;
117 	int fhsize;
118 	struct nfsnode **npp;
119 {
120 	register struct nfsnode *np;
121 	struct nfsnodehashhead *nhpp;
122 	register struct vnode *vp;
123 	extern int (**nfsv2_vnodeop_p)__P((void *));
124 	struct vnode *nvp;
125 	int error;
126 
127 	nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
128 loop:
129 	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
130 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
131 		    memcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
132 			continue;
133 		vp = NFSTOV(np);
134 		if (vget(vp, LK_EXCLUSIVE))
135 			goto loop;
136 		*npp = np;
137 		return(0);
138 	}
139 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
140 		goto loop;
141 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
142 	if (error) {
143 		*npp = 0;
144 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
145 		return (error);
146 	}
147 	nvp->v_vnlock = 0;	/* XXX At least untill we do locking */
148 	vp = nvp;
149 	np = pool_get(&nfs_node_pool, PR_WAITOK);
150 	memset((caddr_t)np, 0, sizeof *np);
151 	vp->v_data = np;
152 	np->n_vnode = vp;
153 	/*
154 	 * Insert the nfsnode in the hash queue for its new file handle
155 	 */
156 	LIST_INSERT_HEAD(nhpp, np, n_hash);
157 	if (fhsize > NFS_SMALLFH) {
158 		MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
159 	} else
160 		np->n_fhp = &np->n_fh;
161 	memcpy((caddr_t)np->n_fhp, (caddr_t)fhp, fhsize);
162 	np->n_fhsize = fhsize;
163 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
164 	memset(np->n_vattr, 0, sizeof (struct vattr));
165 	lockmgr(&nfs_hashlock, LK_RELEASE, 0);
166 	*npp = np;
167 	return (0);
168 }
169 
170 int
171 nfs_inactive(v)
172 	void *v;
173 {
174 	struct vop_inactive_args /* {
175 		struct vnode *a_vp;
176 		struct proc *a_p;
177 	} */ *ap = v;
178 	register struct nfsnode *np;
179 	register struct sillyrename *sp;
180 	struct proc *p = ap->a_p;
181 	extern int prtactive;
182 
183 	np = VTONFS(ap->a_vp);
184 	if (prtactive && ap->a_vp->v_usecount != 0)
185 		vprint("nfs_inactive: pushing active", ap->a_vp);
186 	if (ap->a_vp->v_type != VDIR) {
187 		sp = np->n_sillyrename;
188 		np->n_sillyrename = (struct sillyrename *)0;
189 	} else
190 		sp = (struct sillyrename *)0;
191 	if (sp) {
192 		/*
193 		 * If the usecount is greater than zero, then we are
194 		 * being inactivated by a forcible unmount and do not
195 		 * have to get our own reference. In the normal case,
196 		 * we need a reference to keep the vnode from being
197 		 * recycled by getnewvnode while we do the I/O
198 		 * associated with discarding the buffers.
199 		 */
200 		if (ap->a_vp->v_usecount > 0)
201 			(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
202 		else if (vget(ap->a_vp, 0))
203                         panic("nfs_inactive: lost vnode");
204 		else {
205 			(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
206 			vrele(ap->a_vp);
207 		}
208 
209 
210 		/*
211 		 * Remove the silly file that was rename'd earlier
212 		 */
213 		nfs_removeit(sp);
214 		crfree(sp->s_cred);
215 		vrele(sp->s_dvp);
216 		FREE((caddr_t)sp, M_NFSREQ);
217 	}
218 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
219 		NQNFSNONCACHE | NQNFSWRITE);
220 	VOP_UNLOCK(ap->a_vp, 0);
221 	return (0);
222 }
223 
224 /*
225  * Reclaim an nfsnode so that it can be used for other purposes.
226  */
227 int
228 nfs_reclaim(v)
229 	void *v;
230 {
231 	struct vop_reclaim_args /* {
232 		struct vnode *a_vp;
233 	} */ *ap = v;
234 	register struct vnode *vp = ap->a_vp;
235 	register struct nfsnode *np = VTONFS(vp);
236 	register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
237 	extern int prtactive;
238 
239 	if (prtactive && vp->v_usecount != 0)
240 		vprint("nfs_reclaim: pushing active", vp);
241 
242 	LIST_REMOVE(np, n_hash);
243 
244 	/*
245 	 * For nqnfs, take it off the timer queue as required.
246 	 */
247 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
248 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
249 	}
250 
251 	/*
252 	 * Free up any directory cookie structures and
253 	 * large file handle structures that might be associated with
254 	 * this nfs node.
255 	 */
256 	if (vp->v_type == VDIR && np->n_dircache) {
257 		nfs_invaldircache(vp, 1);
258 		FREE(np->n_dircache, M_NFSDIROFF);
259 	}
260 	if (np->n_fhsize > NFS_SMALLFH) {
261 		FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
262 	}
263 
264 	pool_put(&nfs_vattr_pool, np->n_vattr);
265 	cache_purge(vp);
266 	pool_put(&nfs_node_pool, vp->v_data);
267 	vp->v_data = (void *)0;
268 	return (0);
269 }
270