2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/mount.h>
41 #include <sys/namei.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/fnv_hash.h>
46 #include <sys/objcache.h>
54 static MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS node");
56 static struct objcache *nfsnode_objcache;
57 static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
58 static u_long nfsnodehash;
59 static lwkt_token nfsnhash_token = LWKT_TOKEN_INITIALIZER(nfsnhash_token);
60 static struct lock nfsnhash_lock;
65 #define NFSNOHASH(fhsum) (&nfsnodehashtbl[(fhsum) & nfsnodehash])
68 * Initialize hash links for nfsnodes
69 * and build nfsnode free list.
74 int hsize = vfs_inodehashsize();
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);
85 hashdestroy(nfsnodehashtbl, M_NFSHASH, nfsnodehash);
86 objcache_destroy(nfsnode_objcache);
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.
97 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
100 struct nfsnode *np, *np2;
101 struct nfsnodehashhead *nhpp;
105 struct nfsmount *nmp;
108 * Calculate nfs mount point and figure out whether the rslock should
109 * be interruptable or not.
111 nmp = VFSTONFS(mntp);
112 if (nmp->nm_flag & NFSMNT_INT)
117 lwkt_gettoken(&nfsnhash_token);
120 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
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)) {
129 kprintf("nfs warning: client-client collision "
130 "during rename/link/softlink\n");
132 lwkt_reltoken(&nfsnhash_token);
135 if (vget(vp, LK_EXCLUSIVE))
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
145 if (np == NULL || NFSTOV(np) != vp) {
150 lwkt_reltoken(&nfsnhash_token);
155 * Obtain a lock to prevent a race condition if the getnewvnode()
156 * or MALLOC() below happens to block.
158 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
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.
166 np = objcache_get(nfsnode_objcache, M_WAITOK);
168 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
170 lockmgr(&nfsnhash_lock, LK_RELEASE);
172 objcache_put(nfsnode_objcache, np);
173 lwkt_reltoken(&nfsnhash_token);
178 * Initialize most of (np).
180 bzero(np, sizeof (*np));
181 if (fhsize > NFS_SMALLFH) {
182 np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
184 np->n_fhp = &np->n_fh;
186 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
187 np->n_fhsize = fhsize;
188 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
191 * Validate that we did not race another nfs_nget() due to blocking
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)) {
200 lockmgr(&nfsnhash_lock, LK_RELEASE);
202 if (np->n_fhsize > NFS_SMALLFH)
203 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
205 objcache_put(nfsnode_objcache, np);
210 * Finish connecting up (np, vp) and insert the nfsnode in the
211 * hash for its new file handle.
213 * nvp is locked & refd so effectively so is np.
217 LIST_INSERT_HEAD(nhpp, np, n_hash);
219 lockmgr(&nfsnhash_lock, LK_RELEASE);
220 lwkt_reltoken(&nfsnhash_token);
226 * Nonblocking version of nfs_nget()
229 nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
230 struct nfsnode **npp, struct vnode *notvp)
232 struct nfsnode *np, *np2;
233 struct nfsnodehashhead *nhpp;
237 struct nfsmount *nmp;
240 * Calculate nfs mount point and figure out whether the rslock should
241 * be interruptable or not.
243 nmp = VFSTONFS(mntp);
244 if (nmp->nm_flag & NFSMNT_INT)
251 lwkt_gettoken(&nfsnhash_token);
254 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
256 LIST_FOREACH(np, nhpp, n_hash) {
257 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
258 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
264 kprintf("nfs warning: client-client collision "
265 "during rename/link/softlink\n");
269 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
275 if (NFSTOV(np) != vp) {
280 lwkt_reltoken(&nfsnhash_token);
285 * Not found. If we raced and had acquired a vp we have to release
294 * Obtain a lock to prevent a race condition if the getnewvnode()
295 * or MALLOC() below happens to block.
297 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
301 * Entry not found, allocate a new entry.
303 * Allocate before getnewvnode since doing so afterward
304 * might cause a bogus v_data pointer to get dereferenced
305 * elsewhere if objcache should block.
307 np = objcache_get(nfsnode_objcache, M_WAITOK);
309 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
311 lockmgr(&nfsnhash_lock, LK_RELEASE);
312 objcache_put(nfsnode_objcache, np);
313 lwkt_reltoken(&nfsnhash_token);
318 * Initialize most of (np).
320 bzero(np, sizeof (*np));
321 if (fhsize > NFS_SMALLFH) {
322 np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
324 np->n_fhp = &np->n_fh;
326 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
327 np->n_fhsize = fhsize;
328 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
331 * Validate that we did not race another nfs_nget() due to blocking
334 for (np2 = nhpp->lh_first; np2 != NULL; np2 = np2->n_hash.le_next) {
335 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
336 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
340 lockmgr(&nfsnhash_lock, LK_RELEASE);
342 if (np->n_fhsize > NFS_SMALLFH)
343 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
345 objcache_put(nfsnode_objcache, np);
348 * vp state is retained on retry/loop so we must NULL it
349 * out here or fireworks may ensue.
356 * Finish connecting up (np, vp) and insert the nfsnode in the
357 * hash for its new file handle.
359 * nvp is locked & refd so effectively so is np.
363 LIST_INSERT_HEAD(nhpp, np, n_hash);
366 * nvp is locked & refd so effectively so is np.
370 lockmgr(&nfsnhash_lock, LK_RELEASE);
372 lwkt_reltoken(&nfsnhash_token);
377 * nfs_inactive(struct vnode *a_vp)
379 * NOTE: the passed vnode is locked but not referenced. On return the
380 * vnode must be unlocked and not referenced.
383 nfs_inactive(struct vop_inactive_args *ap)
385 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
387 struct sillyrename *sp;
389 lwkt_gettoken(&nmp->nm_token);
391 np = VTONFS(ap->a_vp);
392 if (prtactive && VREFCNT(ap->a_vp) > 1)
393 vprint("nfs_inactive: pushing active", ap->a_vp);
394 if (ap->a_vp->v_type != VDIR) {
395 sp = np->n_sillyrename;
396 np->n_sillyrename = NULL;
402 * We need a reference to keep the vnode from being
403 * recycled by getnewvnode while we do the I/O
404 * associated with discarding the buffers. The vnode
407 nfs_vinvalbuf(ap->a_vp, 0, 1);
410 * Remove the silly file that was rename'd earlier
415 kfree((caddr_t)sp, M_NFSREQ);
418 np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
419 if (np->n_flag & NREMOVED)
421 lwkt_reltoken(&nmp->nm_token);
427 * Reclaim an nfsnode so that it can be used for other purposes.
429 * There should be no direct references to the related nfs node
430 * since nobody is holding the vnode any more, other than hash
431 * lookups which are interlocked against nfsnhash_token and vget().
433 * nfs_reclaim(struct vnode *a_vp)
436 nfs_reclaim(struct vop_reclaim_args *ap)
438 struct vnode *vp = ap->a_vp;
439 struct nfsnode *np = VTONFS(vp);
440 struct nfsdmap *dp, *dp2;
441 /* struct nfsmount *nmp = VFSTONFS(vp->v_mount);*/
443 if (prtactive && VREFCNT(vp) > 1)
444 vprint("nfs_reclaim: pushing active", vp);
448 * Remove from hash table and remove the cross links.
450 * NOTE: Other NFS code may look up a np and vget() the
451 * related vnode, then will check np->n_vnode.
452 * We must clear np->n_vnode here to ensure that all
453 * possible races are dealt with.
455 lwkt_gettoken(&nfsnhash_token);
456 KKASSERT(np->n_vnode == vp);
457 if (np->n_hash.le_prev != NULL)
458 LIST_REMOVE(np, n_hash);
461 lwkt_reltoken(&nfsnhash_token);
464 * Free up any directory cookie structures and
465 * large file handle structures that might be associated with
468 if (vp->v_type == VDIR) {
469 dp = np->n_cookies.lh_first;
472 dp = dp->ndm_list.le_next;
473 kfree((caddr_t)dp2, M_NFSDIROFF);
476 if (np->n_fhsize > NFS_SMALLFH) {
477 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
480 crfree(np->n_rucred);
484 crfree(np->n_wucred);
487 objcache_put(nfsnode_objcache, np);