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 lwkt_token nfsnhash_token =
57 LWKT_TOKEN_INITIALIZER(nfsnhash_token);
58 static struct lock nfsnhash_lock;
59 __read_mostly static struct objcache *nfsnode_objcache;
60 __read_mostly static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61 __read_mostly static u_long nfsnodehash;
66 #define NFSNOHASH(fhsum) (&nfsnodehashtbl[(fhsum) & nfsnodehash])
69 * Initialize hash links for nfsnodes
70 * and build nfsnode free list.
75 int hsize = vfs_inodehashsize();
77 nfsnode_objcache = objcache_create_simple(M_NFSNODE,
78 sizeof(struct nfsnode));
79 nfsnodehashtbl = hashinit(hsize, M_NFSHASH, &nfsnodehash);
80 lockinit(&nfsnhash_lock, "nfsnht", 0, 0);
86 hashdestroy(nfsnodehashtbl, M_NFSHASH, nfsnodehash);
87 objcache_destroy(nfsnode_objcache);
91 * Look up a vnode/nfsnode by file handle.
92 * Callers must check for mount points!!
93 * In all cases, a pointer to a
94 * nfsnode structure is returned.
98 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
101 struct nfsnode *np, *np2;
102 struct nfsnodehashhead *nhpp;
106 struct nfsmount *nmp;
109 * Calculate nfs mount point and figure out whether the rslock should
110 * be interruptable or not.
112 nmp = VFSTONFS(mntp);
113 if (nmp->nm_flag & NFSMNT_INT)
118 lwkt_gettoken(&nfsnhash_token);
121 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
123 LIST_FOREACH(np, nhpp, n_hash) {
124 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
125 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
130 kprintf("nfs warning: client-client collision "
131 "during rename/link/softlink\n");
133 lwkt_reltoken(&nfsnhash_token);
136 if (vget(vp, LK_EXCLUSIVE))
138 LIST_FOREACH(np, nhpp, n_hash) {
139 if (mntp == NFSTOV(np)->v_mount &&
140 np->n_fhsize == fhsize &&
141 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
146 if (np == NULL || NFSTOV(np) != vp) {
151 lwkt_reltoken(&nfsnhash_token);
156 * Obtain a lock to prevent a race condition if the getnewvnode()
157 * or MALLOC() below happens to block.
159 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
163 * Allocate before getnewvnode since doing so afterward
164 * might cause a bogus v_data pointer to get dereferenced
165 * elsewhere if objcache should block.
167 np = objcache_get(nfsnode_objcache, M_WAITOK);
169 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
171 lockmgr(&nfsnhash_lock, LK_RELEASE);
173 objcache_put(nfsnode_objcache, np);
174 lwkt_reltoken(&nfsnhash_token);
179 * Initialize most of (np).
181 bzero(np, sizeof (*np));
182 if (fhsize > NFS_SMALLFH) {
183 np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
185 np->n_fhp = &np->n_fh;
187 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
188 np->n_fhsize = fhsize;
189 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
192 * Validate that we did not race another nfs_nget() due to blocking
195 for (np2 = nhpp->lh_first; np2 != NULL; np2 = np2->n_hash.le_next) {
196 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
197 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
201 lockmgr(&nfsnhash_lock, LK_RELEASE);
203 if (np->n_fhsize > NFS_SMALLFH)
204 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
206 objcache_put(nfsnode_objcache, np);
211 * Finish connecting up (np, vp) and insert the nfsnode in the
212 * hash for its new file handle.
214 * nvp is locked & refd so effectively so is np.
218 LIST_INSERT_HEAD(nhpp, np, n_hash);
220 lockmgr(&nfsnhash_lock, LK_RELEASE);
221 lwkt_reltoken(&nfsnhash_token);
227 * Nonblocking version of nfs_nget()
230 nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
231 struct nfsnode **npp, struct vnode *notvp)
233 struct nfsnode *np, *np2;
234 struct nfsnodehashhead *nhpp;
238 struct nfsmount *nmp;
241 * Calculate nfs mount point and figure out whether the rslock should
242 * be interruptable or not.
244 nmp = VFSTONFS(mntp);
245 if (nmp->nm_flag & NFSMNT_INT)
252 lwkt_gettoken(&nfsnhash_token);
255 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
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)) {
265 kprintf("nfs warning: client-client collision "
266 "during rename/link/softlink\n");
270 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
276 if (NFSTOV(np) != vp) {
281 lwkt_reltoken(&nfsnhash_token);
286 * Not found. If we raced and had acquired a vp we have to release
295 * Obtain a lock to prevent a race condition if the getnewvnode()
296 * or MALLOC() below happens to block.
298 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
302 * Entry not found, allocate a new entry.
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.
308 np = objcache_get(nfsnode_objcache, M_WAITOK);
310 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
312 lockmgr(&nfsnhash_lock, LK_RELEASE);
313 objcache_put(nfsnode_objcache, np);
314 lwkt_reltoken(&nfsnhash_token);
319 * Initialize most of (np).
321 bzero(np, sizeof (*np));
322 if (fhsize > NFS_SMALLFH) {
323 np->n_fhp = kmalloc(fhsize, M_NFSBIGFH, M_WAITOK);
325 np->n_fhp = &np->n_fh;
327 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
328 np->n_fhsize = fhsize;
329 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
332 * Validate that we did not race another nfs_nget() due to blocking
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)) {
341 lockmgr(&nfsnhash_lock, LK_RELEASE);
343 if (np->n_fhsize > NFS_SMALLFH)
344 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
346 objcache_put(nfsnode_objcache, np);
349 * vp state is retained on retry/loop so we must NULL it
350 * out here or fireworks may ensue.
357 * Finish connecting up (np, vp) and insert the nfsnode in the
358 * hash for its new file handle.
360 * nvp is locked & refd so effectively so is np.
364 LIST_INSERT_HEAD(nhpp, np, n_hash);
367 * nvp is locked & refd so effectively so is np.
371 lockmgr(&nfsnhash_lock, LK_RELEASE);
373 lwkt_reltoken(&nfsnhash_token);
378 * nfs_inactive(struct vnode *a_vp)
380 * NOTE: the passed vnode is locked but not referenced. On return the
381 * vnode must be unlocked and not referenced.
384 nfs_inactive(struct vop_inactive_args *ap)
386 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
388 struct sillyrename *sp;
390 lwkt_gettoken(&nmp->nm_token);
392 np = VTONFS(ap->a_vp);
393 if (prtactive && VREFCNT(ap->a_vp) > 1)
394 vprint("nfs_inactive: pushing active", ap->a_vp);
395 if (ap->a_vp->v_type != VDIR) {
396 sp = np->n_sillyrename;
397 np->n_sillyrename = NULL;
403 * We need a reference to keep the vnode from being
404 * recycled by getnewvnode while we do the I/O
405 * associated with discarding the buffers. The vnode
408 nfs_vinvalbuf(ap->a_vp, 0, 1);
411 * Remove the silly file that was rename'd earlier
416 kfree((caddr_t)sp, M_NFSREQ);
419 np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
420 if (np->n_flag & NREMOVED)
422 lwkt_reltoken(&nmp->nm_token);
428 * Reclaim an nfsnode so that it can be used for other purposes.
430 * There should be no direct references to the related nfs node
431 * since nobody is holding the vnode any more, other than hash
432 * lookups which are interlocked against nfsnhash_token and vget().
434 * nfs_reclaim(struct vnode *a_vp)
437 nfs_reclaim(struct vop_reclaim_args *ap)
439 struct vnode *vp = ap->a_vp;
440 struct nfsnode *np = VTONFS(vp);
441 struct nfsdmap *dp, *dp2;
442 /* struct nfsmount *nmp = VFSTONFS(vp->v_mount);*/
444 if (prtactive && VREFCNT(vp) > 1)
445 vprint("nfs_reclaim: pushing active", vp);
449 * Remove from hash table and remove the cross links.
451 * NOTE: Other NFS code may look up a np and vget() the
452 * related vnode, then will check np->n_vnode.
453 * We must clear np->n_vnode here to ensure that all
454 * possible races are dealt with.
456 lwkt_gettoken(&nfsnhash_token);
457 KKASSERT(np->n_vnode == vp);
458 if (np->n_hash.le_prev != NULL)
459 LIST_REMOVE(np, n_hash);
462 lwkt_reltoken(&nfsnhash_token);
465 * Free up any directory cookie structures and
466 * large file handle structures that might be associated with
469 if (vp->v_type == VDIR) {
470 dp = np->n_cookies.lh_first;
473 dp = dp->ndm_list.le_next;
474 kfree((caddr_t)dp2, M_NFSDIROFF);
477 if (np->n_fhsize > NFS_SMALLFH) {
478 kfree((caddr_t)np->n_fhp, M_NFSBIGFH);
481 crfree(np->n_rucred);
485 crfree(np->n_wucred);
488 objcache_put(nfsnode_objcache, np);