xref: /dragonfly/sys/vfs/nfs/nfs_node.c (revision 8af44722)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
33  * $FreeBSD: src/sys/nfs/nfs_node.c,v 1.36.2.3 2002/01/05 22:25:04 dillon Exp $
34  */
35 
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc.h>
40 #include <sys/mount.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/fnv_hash.h>
45 #include <sys/objcache.h>
46 
47 #include "rpcv2.h"
48 #include "nfsproto.h"
49 #include "nfs.h"
50 #include "nfsmount.h"
51 #include "nfsnode.h"
52 
53 static MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS node");
54 
55 static struct lwkt_token nfsnhash_token =
56 			LWKT_TOKEN_INITIALIZER(nfsnhash_token);
57 static struct lock nfsnhash_lock;
58 __read_mostly static struct objcache *nfsnode_objcache;
59 __read_mostly static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
60 __read_mostly static u_long nfsnodehash;
61 
62 #define TRUE	1
63 #define	FALSE	0
64 
65 #define NFSNOHASH(fhsum)	(&nfsnodehashtbl[(fhsum) & nfsnodehash])
66 
67 /*
68  * Initialize hash links for nfsnodes
69  * and build nfsnode free list.
70  */
71 void
72 nfs_nhinit(void)
73 {
74 	int hsize = vfs_inodehashsize();
75 
76 	nfsnode_objcache = objcache_create_simple(M_NFSNODE,
77 						  sizeof(struct nfsnode));
78 	nfsnodehashtbl = hashinit(hsize, M_NFSHASH, &nfsnodehash);
79 	lockinit(&nfsnhash_lock, "nfsnht", 0, 0);
80 }
81 
82 void
83 nfs_nhdestroy(void)
84 {
85 	hashdestroy(nfsnodehashtbl, M_NFSHASH, nfsnodehash);
86 	objcache_destroy(nfsnode_objcache);
87 }
88 
89 /*
90  * Look up a vnode/nfsnode by file handle.
91  * Callers must check for mount points!!
92  * In all cases, a pointer to a
93  * nfsnode structure is returned.
94  */
95 
96 int
97 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
98 	 struct vnode *notvp)
99 {
100 	struct nfsnode *np, *np2;
101 	struct nfsnodehashhead *nhpp;
102 	struct vnode *vp;
103 	int error;
104 	int lkflags;
105 	struct nfsmount *nmp;
106 
107 	/*
108 	 * Calculate nfs mount point and figure out whether the rslock should
109 	 * be interruptable or not.
110 	 */
111 	nmp = VFSTONFS(mntp);
112 	if (nmp->nm_flag & NFSMNT_INT)
113 		lkflags = LK_PCATCH;
114 	else
115 		lkflags = 0;
116 
117 	lwkt_gettoken(&nfsnhash_token);
118 
119 retry:
120 	nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
121 loop:
122 	LIST_FOREACH(np, nhpp, n_hash) {
123 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
124 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
125 			continue;
126 		}
127 		vp = NFSTOV(np);
128 		if (vp == notvp) {
129 			kprintf("nfs warning: client-client collision "
130 				"during rename/link/softlink\n");
131 			*npp = NULL;
132 			lwkt_reltoken(&nfsnhash_token);
133 			return (ESTALE);
134 		}
135 		if (vget(vp, LK_EXCLUSIVE))
136 			goto loop;
137 		LIST_FOREACH(np, nhpp, n_hash) {
138 			if (mntp == NFSTOV(np)->v_mount &&
139 			    np->n_fhsize == fhsize &&
140 			    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
141 			) {
142 				break;
143 			}
144 		}
145 		if (np == NULL || NFSTOV(np) != vp) {
146 			vput(vp);
147 			goto loop;
148 		}
149 		*npp = np;
150 		lwkt_reltoken(&nfsnhash_token);
151 		return(0);
152 	}
153 
154 	/*
155 	 * Obtain a lock to prevent a race condition if the getnewvnode()
156 	 * or MALLOC() below happens to block.
157 	 */
158 	if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
159 		goto loop;
160 
161 	/*
162 	 * Allocate before getnewvnode since doing so afterward
163 	 * might cause a bogus v_data pointer to get dereferenced
164 	 * elsewhere if objcache should block.
165 	 */
166 	np = objcache_get(nfsnode_objcache, M_WAITOK);
167 
168 	error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
169 	if (error) {
170 		lockmgr(&nfsnhash_lock, LK_RELEASE);
171 		*npp = NULL;
172 		objcache_put(nfsnode_objcache, np);
173 		lwkt_reltoken(&nfsnhash_token);
174 		return (error);
175 	}
176 
177 	/*
178 	 * Initialize most of (np).
179 	 */
180 	bzero(np, sizeof (*np));
181 	if (fhsize > NFS_SMALLFH) {
182 		np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
183 	} else {
184 		np->n_fhp = &np->n_fh;
185 	}
186 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
187 	np->n_fhsize = fhsize;
188 	lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
189 
190 	/*
191 	 * Validate that we did not race another nfs_nget() due to blocking
192 	 * here and there.
193 	 */
194 	for (np2 = nhpp->lh_first; np2 != NULL; np2 = np2->n_hash.le_next) {
195 		if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
196 		    bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
197 			continue;
198 		}
199 		vx_put(vp);
200 		lockmgr(&nfsnhash_lock, LK_RELEASE);
201 
202 		if (np->n_fhsize > NFS_SMALLFH)
203 			kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
204 		np->n_fhp = NULL;
205 		objcache_put(nfsnode_objcache, np);
206 		goto retry;
207 	}
208 
209 	/*
210 	 * Finish connecting up (np, vp) and insert the nfsnode in the
211 	 * hash for its new file handle.
212 	 *
213 	 * nvp is locked & refd so effectively so is np.
214 	 */
215 	np->n_vnode = vp;
216 	vp->v_data = np;
217 	LIST_INSERT_HEAD(nhpp, np, n_hash);
218 	*npp = np;
219 	lockmgr(&nfsnhash_lock, LK_RELEASE);
220 	lwkt_reltoken(&nfsnhash_token);
221 	vx_downgrade(vp);
222 
223 	return (0);
224 }
225 
226 /*
227  * Nonblocking version of nfs_nget()
228  */
229 int
230 nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
231 		  struct nfsnode **npp, struct vnode *notvp)
232 {
233 	struct nfsnode *np, *np2;
234 	struct nfsnodehashhead *nhpp;
235 	struct vnode *vp;
236 	int error;
237 	int lkflags;
238 	struct nfsmount *nmp;
239 
240 	/*
241 	 * Calculate nfs mount point and figure out whether the rslock should
242 	 * be interruptable or not.
243 	 */
244 	nmp = VFSTONFS(mntp);
245 	if (nmp->nm_flag & NFSMNT_INT)
246 		lkflags = LK_PCATCH;
247 	else
248 		lkflags = 0;
249 	vp = NULL;
250 	*npp = NULL;
251 
252 	lwkt_gettoken(&nfsnhash_token);
253 
254 retry:
255 	nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
256 loop:
257 	LIST_FOREACH(np, nhpp, n_hash) {
258 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
259 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
260 			continue;
261 		}
262 		if (vp == NULL) {
263 			vp = NFSTOV(np);
264 			if (vp == notvp) {
265 				kprintf("nfs warning: client-client collision "
266 					"during rename/link/softlink\n");
267 				error = ESTALE;
268 				goto fail;
269 			}
270 			if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
271 				error = EWOULDBLOCK;
272 				goto fail;
273 			}
274 			goto loop;
275 		}
276 		if (NFSTOV(np) != vp) {
277 			vput(vp);
278 			goto loop;
279 		}
280 		*npp = np;
281 		lwkt_reltoken(&nfsnhash_token);
282 		return(0);
283 	}
284 
285 	/*
286 	 * Not found.  If we raced and had acquired a vp we have to release
287 	 * it here.
288 	 */
289 	if (vp) {
290 		vput(vp);
291 		vp = NULL;
292 	}
293 
294 	/*
295 	 * Obtain a lock to prevent a race condition if the getnewvnode()
296 	 * or MALLOC() below happens to block.
297 	 */
298 	if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
299 		goto loop;
300 
301 	/*
302 	 * Entry not found, allocate a new entry.
303 	 *
304 	 * Allocate before getnewvnode since doing so afterward
305 	 * might cause a bogus v_data pointer to get dereferenced
306 	 * elsewhere if objcache should block.
307 	 */
308 	np = objcache_get(nfsnode_objcache, M_WAITOK);
309 
310 	error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
311 	if (error) {
312 		lockmgr(&nfsnhash_lock, LK_RELEASE);
313 		objcache_put(nfsnode_objcache, np);
314 		lwkt_reltoken(&nfsnhash_token);
315 		return (error);
316 	}
317 
318 	/*
319 	 * Initialize most of (np).
320 	 */
321 	bzero(np, sizeof (*np));
322 	if (fhsize > NFS_SMALLFH) {
323 		np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
324 	} else {
325 		np->n_fhp = &np->n_fh;
326 	}
327 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
328 	np->n_fhsize = fhsize;
329 	lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
330 
331 	/*
332 	 * Validate that we did not race another nfs_nget() due to blocking
333 	 * here and there.
334 	 */
335 	for (np2 = nhpp->lh_first; np2 != NULL; np2 = np2->n_hash.le_next) {
336 		if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
337 		    bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
338 			continue;
339 		}
340 		vx_put(vp);
341 		lockmgr(&nfsnhash_lock, LK_RELEASE);
342 
343 		if (np->n_fhsize > NFS_SMALLFH)
344 			kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
345 		np->n_fhp = NULL;
346 		objcache_put(nfsnode_objcache, np);
347 
348 		/*
349 		 * vp state is retained on retry/loop so we must NULL it
350 		 * out here or fireworks may ensue.
351 		 */
352 		vp = NULL;
353 		goto retry;
354 	}
355 
356 	/*
357 	 * Finish connecting up (np, vp) and insert the nfsnode in the
358 	 * hash for its new file handle.
359 	 *
360 	 * nvp is locked & refd so effectively so is np.
361 	 */
362 	np->n_vnode = vp;
363 	vp->v_data = np;
364 	LIST_INSERT_HEAD(nhpp, np, n_hash);
365 
366 	/*
367 	 * nvp is locked & refd so effectively so is np.
368 	 */
369 	*npp = np;
370 	error = 0;
371 	lockmgr(&nfsnhash_lock, LK_RELEASE);
372 	vx_downgrade(vp);
373 fail:
374 	lwkt_reltoken(&nfsnhash_token);
375 	return (error);
376 }
377 
378 /*
379  * nfs_inactive(struct vnode *a_vp)
380  *
381  * NOTE: the passed vnode is locked but not referenced.  On return the
382  * vnode must be unlocked and not referenced.
383  */
384 int
385 nfs_inactive(struct vop_inactive_args *ap)
386 {
387 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
388 	struct nfsnode *np;
389 	struct sillyrename *sp;
390 
391 	lwkt_gettoken(&nmp->nm_token);
392 
393 	np = VTONFS(ap->a_vp);
394 	if (prtactive && VREFCNT(ap->a_vp) > 1)
395 		vprint("nfs_inactive: pushing active", ap->a_vp);
396 	if (ap->a_vp->v_type != VDIR) {
397 		sp = np->n_sillyrename;
398 		np->n_sillyrename = NULL;
399 	} else {
400 		sp = NULL;
401 	}
402 	if (sp) {
403 		/*
404 		 * We need a reference to keep the vnode from being
405 		 * recycled by getnewvnode while we do the I/O
406 		 * associated with discarding the buffers.  The vnode
407 		 * is already locked.
408 		 */
409 		nfs_vinvalbuf(ap->a_vp, 0, 1);
410 
411 		/*
412 		 * Remove the silly file that was rename'd earlier
413 		 */
414 		nfs_removeit(sp);
415 		crfree(sp->s_cred);
416 		vrele(sp->s_dvp);
417 		kfree((caddr_t)sp, M_NFSREQ);
418 	}
419 
420 	np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
421 	if (np->n_flag & NREMOVED)
422 		vrecycle(ap->a_vp);
423 	lwkt_reltoken(&nmp->nm_token);
424 
425 	return (0);
426 }
427 
428 /*
429  * Reclaim an nfsnode so that it can be used for other purposes.
430  *
431  * There should be no direct references to the related nfs node
432  * since nobody is holding the vnode any more, other than hash
433  * lookups which are interlocked against nfsnhash_token and vget().
434  *
435  * nfs_reclaim(struct vnode *a_vp)
436  */
437 int
438 nfs_reclaim(struct vop_reclaim_args *ap)
439 {
440 	struct vnode *vp = ap->a_vp;
441 	struct nfsnode *np = VTONFS(vp);
442 	struct nfsdmap *dp, *dp2;
443 /*	struct nfsmount *nmp = VFSTONFS(vp->v_mount);*/
444 
445 	if (prtactive && VREFCNT(vp) > 1)
446 		vprint("nfs_reclaim: pushing active", vp);
447 
448 
449 	/*
450 	 * Remove from hash table and remove the cross links.
451 	 *
452 	 * NOTE: Other NFS code may look up a np and vget() the
453 	 *	 related vnode, then will check np->n_vnode.
454 	 *	 We must clear np->n_vnode here to ensure that all
455 	 *	 possible races are dealt with.
456 	 */
457 	lwkt_gettoken(&nfsnhash_token);
458 	KKASSERT(np->n_vnode == vp);
459 	if (np->n_hash.le_prev != NULL)
460 		LIST_REMOVE(np, n_hash);
461 	np->n_vnode = NULL;
462 	vp->v_data = NULL;
463 	lwkt_reltoken(&nfsnhash_token);
464 
465 	/*
466 	 * Free up any directory cookie structures and
467 	 * large file handle structures that might be associated with
468 	 * this nfs node.
469 	 */
470 	if (vp->v_type == VDIR) {
471 		dp = np->n_cookies.lh_first;
472 		while (dp) {
473 			dp2 = dp;
474 			dp = dp->ndm_list.le_next;
475 			kfree((caddr_t)dp2, M_NFSDIROFF);
476 		}
477 	}
478 	if (np->n_fhsize > NFS_SMALLFH) {
479 		kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
480 	}
481 	if (np->n_rucred) {
482 		crfree(np->n_rucred);
483 		np->n_rucred = NULL;
484 	}
485 	if (np->n_wucred) {
486 		crfree(np->n_wucred);
487 		np->n_wucred = NULL;
488 	}
489 	objcache_put(nfsnode_objcache, np);
490 
491 	return (0);
492 }
493 
494