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. 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.
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
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 $
42 #include <sys/param.h>
43 #include <sys/systm.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>
51 #include <vm/vm_zone.h>
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_INITIALIZER(nfsnhash_token);
63 static struct lock nfsnhash_lock;
68 #define NFSNOHASH(fhsum) (&nfsnodehashtbl[(fhsum) & nfsnodehash])
71 * Initialize hash links for nfsnodes
72 * and build nfsnode free list.
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);
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.
90 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
92 struct nfsnode *np, *np2;
93 struct nfsnodehashhead *nhpp;
100 * Calculate nfs mount point and figure out whether the rslock should
101 * be interruptable or not.
103 nmp = VFSTONFS(mntp);
104 if (nmp->nm_flag & NFSMNT_INT)
109 lwkt_gettoken(&nfsnhash_token);
112 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
114 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
115 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
116 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
120 if (vget(vp, LK_EXCLUSIVE))
122 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
123 if (mntp == NFSTOV(np)->v_mount &&
124 np->n_fhsize == fhsize &&
125 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
130 if (np == NULL || NFSTOV(np) != vp) {
135 lwkt_reltoken(&nfsnhash_token);
140 * Obtain a lock to prevent a race condition if the getnewvnode()
141 * or MALLOC() below happens to block.
143 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
147 * Allocate before getnewvnode since doing so afterward
148 * might cause a bogus v_data pointer to get dereferenced
149 * elsewhere if zalloc should block.
151 np = zalloc(nfsnode_zone);
153 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
155 lockmgr(&nfsnhash_lock, LK_RELEASE);
157 zfree(nfsnode_zone, np);
158 lwkt_reltoken(&nfsnhash_token);
163 * Initialize most of (np).
165 bzero(np, sizeof (*np));
166 if (fhsize > NFS_SMALLFH) {
167 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
169 np->n_fhp = &np->n_fh;
171 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
172 np->n_fhsize = fhsize;
173 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
176 * Validate that we did not race another nfs_nget() due to blocking
179 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
180 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
181 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
185 lockmgr(&nfsnhash_lock, LK_RELEASE);
187 if (np->n_fhsize > NFS_SMALLFH)
188 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
190 zfree(nfsnode_zone, np);
195 * Finish connecting up (np, vp) and insert the nfsnode in the
196 * hash for its new file handle.
198 * nvp is locked & refd so effectively so is np.
202 LIST_INSERT_HEAD(nhpp, np, n_hash);
204 lockmgr(&nfsnhash_lock, LK_RELEASE);
205 lwkt_reltoken(&nfsnhash_token);
211 * Nonblocking version of nfs_nget()
214 nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
215 struct nfsnode **npp)
217 struct nfsnode *np, *np2;
218 struct nfsnodehashhead *nhpp;
222 struct nfsmount *nmp;
225 * Calculate nfs mount point and figure out whether the rslock should
226 * be interruptable or not.
228 nmp = VFSTONFS(mntp);
229 if (nmp->nm_flag & NFSMNT_INT)
236 lwkt_gettoken(&nfsnhash_token);
239 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
241 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
242 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
243 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
248 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
254 if (NFSTOV(np) != vp) {
259 lwkt_reltoken(&nfsnhash_token);
264 * Not found. If we raced and had acquired a vp we have to release
273 * Obtain a lock to prevent a race condition if the getnewvnode()
274 * or MALLOC() below happens to block.
276 if (lockmgr(&nfsnhash_lock, LK_EXCLUSIVE | LK_SLEEPFAIL))
280 * Entry not found, allocate a new entry.
282 * Allocate before getnewvnode since doing so afterward
283 * might cause a bogus v_data pointer to get dereferenced
284 * elsewhere if zalloc should block.
286 np = zalloc(nfsnode_zone);
288 error = getnewvnode(VT_NFS, mntp, &vp, 0, 0);
290 lockmgr(&nfsnhash_lock, LK_RELEASE);
291 zfree(nfsnode_zone, np);
292 lwkt_reltoken(&nfsnhash_token);
297 * Initialize most of (np).
299 bzero(np, sizeof (*np));
300 if (fhsize > NFS_SMALLFH) {
301 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
303 np->n_fhp = &np->n_fh;
305 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
306 np->n_fhsize = fhsize;
307 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
310 * Validate that we did not race another nfs_nget() due to blocking
313 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
314 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
315 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
319 lockmgr(&nfsnhash_lock, LK_RELEASE);
321 if (np->n_fhsize > NFS_SMALLFH)
322 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
324 zfree(nfsnode_zone, np);
327 * vp state is retained on retry/loop so we must NULL it
328 * out here or fireworks may ensue.
335 * Finish connecting up (np, vp) and insert the nfsnode in the
336 * hash for its new file handle.
338 * nvp is locked & refd so effectively so is np.
342 LIST_INSERT_HEAD(nhpp, np, n_hash);
345 * nvp is locked & refd so effectively so is np.
349 lockmgr(&nfsnhash_lock, LK_RELEASE);
351 lwkt_reltoken(&nfsnhash_token);
356 * nfs_inactive(struct vnode *a_vp)
358 * NOTE: the passed vnode is locked but not referenced. On return the
359 * vnode must be unlocked and not referenced.
362 nfs_inactive(struct vop_inactive_args *ap)
364 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
366 struct sillyrename *sp;
368 lwkt_gettoken(&nmp->nm_token);
370 np = VTONFS(ap->a_vp);
371 if (prtactive && ap->a_vp->v_sysref.refcnt > 1)
372 vprint("nfs_inactive: pushing active", ap->a_vp);
373 if (ap->a_vp->v_type != VDIR) {
374 sp = np->n_sillyrename;
375 np->n_sillyrename = NULL;
381 * We need a reference to keep the vnode from being
382 * recycled by getnewvnode while we do the I/O
383 * associated with discarding the buffers. The vnode
386 nfs_vinvalbuf(ap->a_vp, 0, 1);
389 * Remove the silly file that was rename'd earlier
394 FREE((caddr_t)sp, M_NFSREQ);
397 np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
398 lwkt_reltoken(&nmp->nm_token);
404 * Reclaim an nfsnode so that it can be used for other purposes.
406 * nfs_reclaim(struct vnode *a_vp)
409 nfs_reclaim(struct vop_reclaim_args *ap)
411 struct vnode *vp = ap->a_vp;
412 struct nfsnode *np = VTONFS(vp);
413 struct nfsdmap *dp, *dp2;
414 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
416 if (prtactive && vp->v_sysref.refcnt > 1)
417 vprint("nfs_reclaim: pushing active", vp);
421 * Remove from hash table
423 lwkt_gettoken(&nfsnhash_token);
424 if (np->n_hash.le_prev != NULL)
425 LIST_REMOVE(np, n_hash);
426 lwkt_reltoken(&nfsnhash_token);
429 * Free up any directory cookie structures and
430 * large file handle structures that might be associated with
433 lwkt_gettoken(&nmp->nm_token);
434 if (vp->v_type == VDIR) {
435 dp = np->n_cookies.lh_first;
438 dp = dp->ndm_list.le_next;
439 FREE((caddr_t)dp2, M_NFSDIROFF);
442 if (np->n_fhsize > NFS_SMALLFH) {
443 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
446 crfree(np->n_rucred);
450 crfree(np->n_wucred);
455 lwkt_reltoken(&nmp->nm_token);
456 zfree(nfsnode_zone, np);