network - Tokenize NFS, fix MP races
[dragonfly.git] / sys / vfs / nfs / nfs_node.c
CommitLineData
984263bc
MD
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 $
b478fdce 38 * $DragonFly: src/sys/vfs/nfs/nfs_node.c,v 1.27 2007/08/08 00:12:51 swildner Exp $
984263bc
MD
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
1f2de5d4
MD
53#include "rpcv2.h"
54#include "nfsproto.h"
55#include "nfs.h"
1f2de5d4 56#include "nfsmount.h"
c1cf1e59 57#include "nfsnode.h"
984263bc
MD
58
59static vm_zone_t nfsnode_zone;
60static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61static u_long nfsnodehash;
62
63#define TRUE 1
64#define FALSE 0
65
e97453f3
MD
66#define NFSNOHASH(fhsum) (&nfsnodehashtbl[(fhsum) & nfsnodehash])
67
984263bc
MD
68/*
69 * Initialize hash links for nfsnodes
70 * and build nfsnode free list.
71 */
72void
e851b29e 73nfs_nhinit(void)
984263bc
MD
74{
75 nfsnode_zone = zinit("NFSNODE", sizeof(struct nfsnode), 0, 0, 1);
76 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSHASH, &nfsnodehash);
77}
78
79/*
80 * Look up a vnode/nfsnode by file handle.
81 * Callers must check for mount points!!
82 * In all cases, a pointer to a
83 * nfsnode structure is returned.
84 */
85static int nfs_node_hash_lock;
86
87int
dadab5e9 88nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
984263bc 89{
984263bc
MD
90 struct nfsnode *np, *np2;
91 struct nfsnodehashhead *nhpp;
dadab5e9 92 struct vnode *vp;
984263bc
MD
93 struct vnode *nvp;
94 int error;
f2770c70 95 int lkflags;
984263bc
MD
96 struct nfsmount *nmp;
97
98 /*
99 * Calculate nfs mount point and figure out whether the rslock should
100 * be interruptable or not.
101 */
102 nmp = VFSTONFS(mntp);
103 if (nmp->nm_flag & NFSMNT_INT)
f2770c70 104 lkflags = LK_PCATCH;
984263bc 105 else
f2770c70 106 lkflags = 0;
984263bc
MD
107
108retry:
109 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
110loop:
5fd012e0 111 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
984263bc 112 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
5fd012e0 113 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
984263bc 114 continue;
5fd012e0 115 }
984263bc 116 vp = NFSTOV(np);
87de5057 117 if (vget(vp, LK_EXCLUSIVE))
5fd012e0
MD
118 goto loop;
119 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
120 if (mntp == NFSTOV(np)->v_mount &&
121 np->n_fhsize == fhsize &&
122 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize) == 0
123 ) {
124 break;
125 }
126 }
127 if (np == NULL || NFSTOV(np) != vp) {
128 vput(vp);
984263bc 129 goto loop;
5fd012e0 130 }
984263bc
MD
131 *npp = np;
132 return(0);
133 }
134 /*
135 * Obtain a lock to prevent a race condition if the getnewvnode()
136 * or MALLOC() below happens to block.
137 */
138 if (nfs_node_hash_lock) {
139 while (nfs_node_hash_lock) {
140 nfs_node_hash_lock = -1;
377d4740 141 tsleep(&nfs_node_hash_lock, 0, "nfsngt", 0);
984263bc
MD
142 }
143 goto loop;
144 }
145 nfs_node_hash_lock = 1;
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
ab6f251b 154 error = getnewvnode(VT_NFS, mntp, &nvp, 0, 0);
984263bc
MD
155 if (error) {
156 if (nfs_node_hash_lock < 0)
157 wakeup(&nfs_node_hash_lock);
158 nfs_node_hash_lock = 0;
159 *npp = 0;
160 zfree(nfsnode_zone, np);
161 return (error);
162 }
163 vp = nvp;
164 bzero((caddr_t)np, sizeof *np);
984263bc 165 np->n_vnode = vp;
57f7b636
MD
166 vp->v_data = np;
167
984263bc
MD
168 /*
169 * Insert the nfsnode in the hash queue for its new file handle
170 */
171 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
172 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
173 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
174 continue;
5fd012e0 175 vx_put(vp);
984263bc
MD
176 if (nfs_node_hash_lock < 0)
177 wakeup(&nfs_node_hash_lock);
178 nfs_node_hash_lock = 0;
179 zfree(nfsnode_zone, np);
180 goto retry;
181 }
182 LIST_INSERT_HEAD(nhpp, np, n_hash);
183 if (fhsize > NFS_SMALLFH) {
184 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
668b43c5 185 } else {
984263bc 186 np->n_fhp = &np->n_fh;
668b43c5 187 }
984263bc
MD
188 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
189 np->n_fhsize = fhsize;
ab6f251b 190 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
5fd012e0
MD
191
192 /*
193 * nvp is locked & refd so effectively so is np.
194 */
984263bc 195 *npp = np;
984263bc
MD
196 if (nfs_node_hash_lock < 0)
197 wakeup(&nfs_node_hash_lock);
198 nfs_node_hash_lock = 0;
199
984263bc
MD
200 return (0);
201}
202
e851b29e 203/*
668b43c5
MD
204 * Nonblocking version of nfs_nget()
205 */
206int
207nfs_nget_nonblock(struct mount *mntp, nfsfh_t *fhp, int fhsize,
208 struct nfsnode **npp)
209{
210 struct nfsnode *np, *np2;
211 struct nfsnodehashhead *nhpp;
212 struct vnode *vp;
213 struct vnode *nvp;
214 int error;
215 int lkflags;
216 struct nfsmount *nmp;
217
218 /*
219 * Calculate nfs mount point and figure out whether the rslock should
220 * be interruptable or not.
221 */
222 nmp = VFSTONFS(mntp);
223 if (nmp->nm_flag & NFSMNT_INT)
224 lkflags = LK_PCATCH;
225 else
226 lkflags = 0;
227 vp = NULL;
228 *npp = NULL;
229retry:
230 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
231loop:
232 for (np = nhpp->lh_first; np; np = np->n_hash.le_next) {
233 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
234 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize)) {
235 continue;
236 }
237 if (vp == NULL) {
238 vp = NFSTOV(np);
239 if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT)) {
240 error = EWOULDBLOCK;
241 goto fail;
242 }
243 goto loop;
244 }
245 if (NFSTOV(np) != vp) {
246 vput(vp);
247 vp = NULL;
248 goto loop;
249 }
250 *npp = np;
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 (nfs_node_hash_lock) {
268 while (nfs_node_hash_lock) {
269 nfs_node_hash_lock = -1;
270 tsleep(&nfs_node_hash_lock, 0, "nfsngt", 0);
271 }
272 goto loop;
273 }
274 nfs_node_hash_lock = 1;
275
276 /*
277 * Entry not found, allocate a new entry.
278 *
279 * Allocate before getnewvnode since doing so afterward
280 * might cause a bogus v_data pointer to get dereferenced
281 * elsewhere if zalloc should block.
282 */
283 np = zalloc(nfsnode_zone);
284
285 error = getnewvnode(VT_NFS, mntp, &nvp, 0, 0);
286 if (error) {
287 if (nfs_node_hash_lock < 0)
288 wakeup(&nfs_node_hash_lock);
289 nfs_node_hash_lock = 0;
290 zfree(nfsnode_zone, np);
291 return (error);
292 }
293 vp = nvp;
294 bzero(np, sizeof (*np));
295 np->n_vnode = vp;
296 vp->v_data = np;
297
298 /*
299 * Insert the nfsnode in the hash queue for its new file handle.
300 * If someone raced us we free np and vp and try again.
301 */
302 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
303 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
304 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize)) {
305 continue;
306 }
307 vx_put(vp);
308 if (nfs_node_hash_lock < 0)
309 wakeup(&nfs_node_hash_lock);
310 nfs_node_hash_lock = 0;
311 zfree(nfsnode_zone, np);
312 goto retry;
313 }
314 LIST_INSERT_HEAD(nhpp, np, n_hash);
315 if (fhsize > NFS_SMALLFH) {
316 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
317 } else {
318 np->n_fhp = &np->n_fh;
319 }
320 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
321 np->n_fhsize = fhsize;
322 lockinit(&np->n_rslock, "nfrslk", 0, lkflags);
323
324 /*
325 * nvp is locked & refd so effectively so is np.
326 */
327 *npp = np;
328 error = 0;
329 if (nfs_node_hash_lock < 0)
330 wakeup(&nfs_node_hash_lock);
331 nfs_node_hash_lock = 0;
332fail:
333 return (error);
334}
335
336/*
b478fdce 337 * nfs_inactive(struct vnode *a_vp)
48b38f01
MD
338 *
339 * NOTE: the passed vnode is locked but not referenced. On return the
340 * vnode must be unlocked and not referenced.
e851b29e 341 */
984263bc 342int
e851b29e 343nfs_inactive(struct vop_inactive_args *ap)
984263bc 344{
c6b43e93 345 struct nfsmount *nmp = VFSTONFS(ap->a_vp->v_mount);
dadab5e9
MD
346 struct nfsnode *np;
347 struct sillyrename *sp;
984263bc 348
c6b43e93
MD
349 lwkt_gettoken(&nmp->nm_token);
350
984263bc 351 np = VTONFS(ap->a_vp);
3c37c940 352 if (prtactive && ap->a_vp->v_sysref.refcnt > 1)
984263bc
MD
353 vprint("nfs_inactive: pushing active", ap->a_vp);
354 if (ap->a_vp->v_type != VDIR) {
355 sp = np->n_sillyrename;
ef2eabd2
MD
356 np->n_sillyrename = NULL;
357 } else {
358 sp = NULL;
359 }
984263bc
MD
360 if (sp) {
361 /*
362 * We need a reference to keep the vnode from being
363 * recycled by getnewvnode while we do the I/O
48b38f01
MD
364 * associated with discarding the buffers. The vnode
365 * is already locked.
984263bc 366 */
87de5057 367 nfs_vinvalbuf(ap->a_vp, 0, 1);
5fd012e0
MD
368
369 /*
984263bc
MD
370 * Remove the silly file that was rename'd earlier
371 */
372 nfs_removeit(sp);
373 crfree(sp->s_cred);
374 vrele(sp->s_dvp);
375 FREE((caddr_t)sp, M_NFSREQ);
376 }
5a9187cb
MD
377
378 np->n_flag &= ~(NWRITEERR | NACC | NUPD | NCHG | NLOCKED | NWANTED);
c6b43e93 379 lwkt_reltoken(&nmp->nm_token);
5a9187cb 380
984263bc
MD
381 return (0);
382}
383
384/*
385 * Reclaim an nfsnode so that it can be used for other purposes.
e851b29e
CP
386 *
387 * nfs_reclaim(struct vnode *a_vp)
984263bc
MD
388 */
389int
e851b29e 390nfs_reclaim(struct vop_reclaim_args *ap)
984263bc 391{
40393ded
RG
392 struct vnode *vp = ap->a_vp;
393 struct nfsnode *np = VTONFS(vp);
40393ded 394 struct nfsdmap *dp, *dp2;
c6b43e93 395 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
984263bc 396
3c37c940 397 if (prtactive && vp->v_sysref.refcnt > 1)
984263bc
MD
398 vprint("nfs_reclaim: pushing active", vp);
399
c6b43e93
MD
400 lwkt_gettoken(&nmp->nm_token);
401
984263bc
MD
402 if (np->n_hash.le_prev != NULL)
403 LIST_REMOVE(np, n_hash);
404
405 /*
984263bc
MD
406 * Free up any directory cookie structures and
407 * large file handle structures that might be associated with
408 * this nfs node.
409 */
410 if (vp->v_type == VDIR) {
411 dp = np->n_cookies.lh_first;
412 while (dp) {
413 dp2 = dp;
414 dp = dp->ndm_list.le_next;
415 FREE((caddr_t)dp2, M_NFSDIROFF);
416 }
417 }
418 if (np->n_fhsize > NFS_SMALLFH) {
419 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
420 }
c1cf1e59
MD
421 if (np->n_rucred) {
422 crfree(np->n_rucred);
423 np->n_rucred = NULL;
424 }
425 if (np->n_wucred) {
426 crfree(np->n_wucred);
427 np->n_wucred = NULL;
428 }
3446c007 429 vp->v_data = NULL;
c6b43e93
MD
430
431 lwkt_reltoken(&nmp->nm_token);
3446c007 432 zfree(nfsnode_zone, np);
c6b43e93 433
984263bc
MD
434 return (0);
435}
436