xref: /dflybsd-src/sys/vfs/nfs/nfs_node.c (revision 498432c875b11af5696001bb2a7413ed7c0bf334)
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
37  * $FreeBSD: src/sys/nfs/nfs_node.c,v 1.36.2.3 2002/01/05 22:25:04 dillon Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_node.c,v 1.27 2007/08/08 00:12:51 swildner Exp $
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/malloc.h>
49 #include <sys/fnv_hash.h>
50 
51 #include <vm/vm_zone.h>
52 
53 #include "rpcv2.h"
54 #include "nfsproto.h"
55 #include "nfs.h"
56 #include "nfsmount.h"
57 #include "nfsnode.h"
58 
59 static vm_zone_t nfsnode_zone;
60 static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61 static u_long nfsnodehash;
62 static lwkt_token nfsnhash_token = LWKT_TOKEN_MP_INITIALIZER(nfsnhash_token);
63 static struct lock nfsnhash_lock;
64 
65 #define TRUE	1
66 #define	FALSE	0
67 
68 #define NFSNOHASH(fhsum)	(&nfsnodehashtbl[(fhsum) & nfsnodehash])
69 
70 /*
71  * Initialize hash links for nfsnodes
72  * and build nfsnode free list.
73  */
74 void
75 nfs_nhinit(void)
76 {
77 	nfsnode_zone = zinit("NFSNODE", sizeof(struct nfsnode), 0, 0, 1);
78 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSHASH, &nfsnodehash);
79 	lockinit(&nfsnhash_lock, "nfsnht", 0, 0);
80 }
81 
82 /*
83  * Look up a vnode/nfsnode by file handle.
84  * Callers must check for mount points!!
85  * In all cases, a pointer to a
86  * nfsnode structure is returned.
87  */
88 
89 int
90 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
91 {
92 	struct nfsnode *np, *np2;
93 	struct nfsnodehashhead *nhpp;
94 	struct vnode *vp;
95 	struct vnode *nvp;
96 	int error;
97 	int lkflags;
98 	struct nfsmount *nmp;
99 
100 	/*
101 	 * Calculate nfs mount point and figure out whether the rslock should
102 	 * be interruptable or not.
103 	 */
104 	nmp = VFSTONFS(mntp);
105 	if (nmp->nm_flag & NFSMNT_INT)
106 		lkflags = LK_PCATCH;
107 	else
108 		lkflags = 0;
109 
110 	lwkt_gettoken(&nfsnhash_token);
111 
112 retry:
113 	nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
114 loop:
115 	for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
116 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
117 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
118 			continue;
119 		}
120 		vp = NFSTOV(np);
121 		if (vget(vp, LK_EXCLUSIVE))
122 			goto loop;
123 		for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
124 			if (mntp == NFSTOV(np)->v_mount &&
125 			    np->n_fhsize == fhsize &&
126 			    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
127 			) {
128 				break;
129 			}
130 		}
131 		if (np == NULL || NFSTOV(np) != vp) {
132 			vput(vp);
133 			goto loop;
134 		}
135 		*npp = np;
136 		lwkt_reltoken(&nfsnhash_token);
137 		return(0);
138 	}
139 
140 	/*
141 	 * Obtain a lock to prevent a race condition if the getnewvnode()
142 	 * or MALLOC() below happens to block.
143 	 */
144 	if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
145 		goto loop;
146 
147 	/*
148 	 * Allocate before getnewvnode since doing so afterward
149 	 * might cause a bogus v_data pointer to get dereferenced
150 	 * elsewhere if zalloc should block.
151 	 */
152 	np = zalloc(nfsnode_zone);
153 
154 	error = getnewvnode(VT_NFS, mntp, &nvp, 0, 0);
155 	if (error) {
156 		lockmgr(&nfsnhash_lock, LK_RELEASE);
157 		*npp = 0;
158 		zfree(nfsnode_zone, np);
159 		lwkt_reltoken(&nfsnhash_token);
160 		return (error);
161 	}
162 	vp = nvp;
163 	bzero((caddr_t)np, sizeof *np);
164 	np->n_vnode = vp;
165 	vp->v_data = np;
166 
167 	/*
168 	 * Insert the nfsnode in the hash queue for its new file handle
169 	 */
170 	for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
171 		if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
172 		    bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
173 			continue;
174 		vx_put(vp);
175 		lockmgr(&nfsnhash_lock, LK_RELEASE);
176 		zfree(nfsnode_zone, np);
177 		goto retry;
178 	}
179 	LIST_INSERT_HEAD(nhpp, np, n_hash);
180 	if (fhsize > NFS_SMALLFH) {
181 		MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
182 	} else {
183 		np->n_fhp = &np->n_fh;
184 	}
185 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
186 	np->n_fhsize = fhsize;
187 	lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
188 
189 	/*
190 	 * nvp is locked & refd so effectively so is np.
191 	 */
192 	*npp = np;
193 	lockmgr(&nfsnhash_lock, LK_RELEASE);
194 	lwkt_reltoken(&nfsnhash_token);
195 
196 	return (0);
197 }
198 
199 /*
200  * Nonblocking version of nfs_nget()
201  */
202 int
203 nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
204 		  struct nfsnode **npp)
205 {
206 	struct nfsnode *np, *np2;
207 	struct nfsnodehashhead *nhpp;
208 	struct vnode *vp;
209 	struct vnode *nvp;
210 	int error;
211 	int lkflags;
212 	struct nfsmount *nmp;
213 
214 	/*
215 	 * Calculate nfs mount point and figure out whether the rslock should
216 	 * be interruptable or not.
217 	 */
218 	nmp = VFSTONFS(mntp);
219 	if (nmp->nm_flag & NFSMNT_INT)
220 		lkflags = LK_PCATCH;
221 	else
222 		lkflags = 0;
223 	vp = NULL;
224 	*npp = NULL;
225 
226 	lwkt_gettoken(&nfsnhash_token);
227 
228 retry:
229 	nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
230 loop:
231 	for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
232 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
233 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
234 			continue;
235 		}
236 		if (vp == NULL) {
237 			vp = NFSTOV(np);
238 			if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
239 				error = EWOULDBLOCK;
240 				goto fail;
241 			}
242 			goto loop;
243 		}
244 		if (NFSTOV(np) != vp) {
245 			vput(vp);
246 			vp = NULL;
247 			goto loop;
248 		}
249 		*npp = np;
250 		lwkt_reltoken(&nfsnhash_token);
251 		return(0);
252 	}
253 
254 	/*
255 	 * Not found.  If we raced and had acquired a vp we have to release
256 	 * it here.
257 	 */
258 	if (vp) {
259 		vput(vp);
260 		vp = NULL;
261 	}
262 
263 	/*
264 	 * Obtain a lock to prevent a race condition if the getnewvnode()
265 	 * or MALLOC() below happens to block.
266 	 */
267 	if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
268 		goto loop;
269 
270 	/*
271 	 * Entry not found, allocate a new entry.
272 	 *
273 	 * Allocate before getnewvnode since doing so afterward
274 	 * might cause a bogus v_data pointer to get dereferenced
275 	 * elsewhere if zalloc should block.
276 	 */
277 	np = zalloc(nfsnode_zone);
278 
279 	error = getnewvnode(VT_NFS, mntp, &nvp, 0, 0);
280 	if (error) {
281 		lockmgr(&nfsnhash_lock, LK_RELEASE);
282 		zfree(nfsnode_zone, np);
283 		lwkt_reltoken(&nfsnhash_token);
284 		return (error);
285 	}
286 	vp = nvp;
287 	bzero(np, sizeof (*np));
288 	np->n_vnode = vp;
289 	vp->v_data = np;
290 
291 	/*
292 	 * Insert the nfsnode in the hash queue for its new file handle.
293 	 * If someone raced us we free np and vp and try again.
294 	 */
295 	for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
296 		if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
297 		    bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
298 			continue;
299 		}
300 		vx_put(vp);
301 		lockmgr(&nfsnhash_lock, LK_RELEASE);
302 		zfree(nfsnode_zone, np);
303 		goto retry;
304 	}
305 	LIST_INSERT_HEAD(nhpp, np, n_hash);
306 	if (fhsize > NFS_SMALLFH) {
307 		MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
308 	} else {
309 		np->n_fhp = &np->n_fh;
310 	}
311 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
312 	np->n_fhsize = fhsize;
313 	lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
314 
315 	/*
316 	 * nvp is locked & refd so effectively so is np.
317 	 */
318 	*npp = np;
319 	error = 0;
320 	lockmgr(&nfsnhash_lock, LK_RELEASE);
321 fail:
322 	lwkt_reltoken(&nfsnhash_token);
323 	return (error);
324 }
325 
326 /*
327  * nfs_inactive(struct vnode *a_vp)
328  *
329  * NOTE: the passed vnode is locked but not referenced.  On return the
330  * vnode must be unlocked and not referenced.
331  */
332 int
333 nfs_inactive(struct vop_inactive_args *ap)
334 {
335 	struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
336 	struct nfsnode *np;
337 	struct sillyrename *sp;
338 
339 	lwkt_gettoken(&nmp->nm_token);
340 
341 	np = VTONFS(ap->a_vp);
342 	if (prtactive && ap->a_vp->v_sysref.refcnt > 1)
343 		vprint("nfs_inactive: pushing active", ap->a_vp);
344 	if (ap->a_vp->v_type != VDIR) {
345 		sp = np->n_sillyrename;
346 		np->n_sillyrename = NULL;
347 	} else {
348 		sp = NULL;
349 	}
350 	if (sp) {
351 		/*
352 		 * We need a reference to keep the vnode from being
353 		 * recycled by getnewvnode while we do the I/O
354 		 * associated with discarding the buffers.  The vnode
355 		 * is already locked.
356 		 */
357 		nfs_vinvalbuf(ap->a_vp, 0, 1);
358 
359 		/*
360 		 * Remove the silly file that was rename'd earlier
361 		 */
362 		nfs_removeit(sp);
363 		crfree(sp->s_cred);
364 		vrele(sp->s_dvp);
365 		FREE((caddr_t)sp, M_NFSREQ);
366 	}
367 
368 	np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
369 	lwkt_reltoken(&nmp->nm_token);
370 
371 	return (0);
372 }
373 
374 /*
375  * Reclaim an nfsnode so that it can be used for other purposes.
376  *
377  * nfs_reclaim(struct vnode *a_vp)
378  */
379 int
380 nfs_reclaim(struct vop_reclaim_args *ap)
381 {
382 	struct vnode *vp = ap->a_vp;
383 	struct nfsnode *np = VTONFS(vp);
384 	struct nfsdmap *dp, *dp2;
385 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
386 
387 	if (prtactive && vp->v_sysref.refcnt > 1)
388 		vprint("nfs_reclaim: pushing active", vp);
389 
390 
391 	/*
392 	 * Remove from hash table
393 	 */
394 	lwkt_gettoken(&nfsnhash_token);
395 	if (np->n_hash.le_prev != NULL)
396 		LIST_REMOVE(np, n_hash);
397 	lwkt_reltoken(&nfsnhash_token);
398 
399 	/*
400 	 * Free up any directory cookie structures and
401 	 * large file handle structures that might be associated with
402 	 * this nfs node.
403 	 */
404 	lwkt_gettoken(&nmp->nm_token);
405 	if (vp->v_type == VDIR) {
406 		dp = np->n_cookies.lh_first;
407 		while (dp) {
408 			dp2 = dp;
409 			dp = dp->ndm_list.le_next;
410 			FREE((caddr_t)dp2, M_NFSDIROFF);
411 		}
412 	}
413 	if (np->n_fhsize > NFS_SMALLFH) {
414 		FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
415 	}
416 	if (np->n_rucred) {
417 		crfree(np->n_rucred);
418 		np->n_rucred = NULL;
419 	}
420 	if (np->n_wucred) {
421 		crfree(np->n_wucred);
422 		np->n_wucred = NULL;
423 	}
424 	vp->v_data = NULL;
425 
426 	lwkt_reltoken(&nmp->nm_token);
427 	zfree(nfsnode_zone, np);
428 
429 	return (0);
430 }
431 
432