VFS messaging/interfacing work stage 4/99. This stage goes a long ways
[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 $
3446c007 38 * $DragonFly: src/sys/vfs/nfs/nfs_node.c,v 1.13 2004/08/28 19:02:20 dillon 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
66/*
67 * Initialize hash links for nfsnodes
68 * and build nfsnode free list.
69 */
70void
e851b29e 71nfs_nhinit(void)
984263bc
MD
72{
73 nfsnode_zone = zinit("NFSNODE", sizeof(struct nfsnode), 0, 0, 1);
74 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSHASH, &nfsnodehash);
75}
76
77/*
78 * Look up a vnode/nfsnode by file handle.
79 * Callers must check for mount points!!
80 * In all cases, a pointer to a
81 * nfsnode structure is returned.
82 */
83static int nfs_node_hash_lock;
84
85int
dadab5e9 86nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
984263bc 87{
dadab5e9 88 struct thread *td = curthread; /* XXX */
984263bc
MD
89 struct nfsnode *np, *np2;
90 struct nfsnodehashhead *nhpp;
dadab5e9 91 struct vnode *vp;
984263bc
MD
92 struct vnode *nvp;
93 int error;
94 int rsflags;
95 struct nfsmount *nmp;
96
97 /*
98 * Calculate nfs mount point and figure out whether the rslock should
99 * be interruptable or not.
100 */
101 nmp = VFSTONFS(mntp);
102 if (nmp->nm_flag & NFSMNT_INT)
103 rsflags = PCATCH;
104 else
105 rsflags = 0;
106
107retry:
108 nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
109loop:
110 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
111 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
112 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
113 continue;
114 vp = NFSTOV(np);
41a01a4d 115 if (vget(vp, NULL, LK_EXCLUSIVE|LK_SLEEPFAIL, td))
984263bc
MD
116 goto loop;
117 *npp = np;
118 return(0);
119 }
120 /*
121 * Obtain a lock to prevent a race condition if the getnewvnode()
122 * or MALLOC() below happens to block.
123 */
124 if (nfs_node_hash_lock) {
125 while (nfs_node_hash_lock) {
126 nfs_node_hash_lock = -1;
377d4740 127 tsleep(&nfs_node_hash_lock, 0, "nfsngt", 0);
984263bc
MD
128 }
129 goto loop;
130 }
131 nfs_node_hash_lock = 1;
132
133 /*
134 * Allocate before getnewvnode since doing so afterward
135 * might cause a bogus v_data pointer to get dereferenced
136 * elsewhere if zalloc should block.
137 */
138 np = zalloc(nfsnode_zone);
139
3446c007
MD
140 error = getnewvnode(VT_NFS, mntp, mntp->mnt_vn_ops, &nvp,
141 0, LK_NOPAUSE);
984263bc
MD
142 if (error) {
143 if (nfs_node_hash_lock < 0)
144 wakeup(&nfs_node_hash_lock);
145 nfs_node_hash_lock = 0;
146 *npp = 0;
147 zfree(nfsnode_zone, np);
148 return (error);
149 }
150 vp = nvp;
151 bzero((caddr_t)np, sizeof *np);
152 vp->v_data = np;
153 np->n_vnode = vp;
154 /*
155 * Insert the nfsnode in the hash queue for its new file handle
156 */
157 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
158 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
159 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
160 continue;
161 vrele(vp);
162 if (nfs_node_hash_lock < 0)
163 wakeup(&nfs_node_hash_lock);
164 nfs_node_hash_lock = 0;
165 zfree(nfsnode_zone, np);
166 goto retry;
167 }
168 LIST_INSERT_HEAD(nhpp, np, n_hash);
169 if (fhsize > NFS_SMALLFH) {
170 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
171 } else
172 np->n_fhp = &np->n_fh;
173 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
174 np->n_fhsize = fhsize;
377d4740 175 lockinit(&np->n_rslock, rsflags, "nfrslk", 0, LK_NOPAUSE);
984263bc
MD
176 *npp = np;
177
178 if (nfs_node_hash_lock < 0)
179 wakeup(&nfs_node_hash_lock);
180 nfs_node_hash_lock = 0;
181
182 /*
183 * Lock the new nfsnode.
184 */
41a01a4d 185 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
186
187 return (0);
188}
189
e851b29e
CP
190/*
191 * nfs_inactive(struct vnode *a_vp, struct thread *a_td)
192 */
984263bc 193int
e851b29e 194nfs_inactive(struct vop_inactive_args *ap)
984263bc 195{
dadab5e9
MD
196 struct nfsnode *np;
197 struct sillyrename *sp;
984263bc
MD
198
199 np = VTONFS(ap->a_vp);
200 if (prtactive && ap->a_vp->v_usecount != 0)
201 vprint("nfs_inactive: pushing active", ap->a_vp);
202 if (ap->a_vp->v_type != VDIR) {
203 sp = np->n_sillyrename;
204 np->n_sillyrename = (struct sillyrename *)0;
205 } else
206 sp = (struct sillyrename *)0;
207 if (sp) {
208 /*
209 * We need a reference to keep the vnode from being
210 * recycled by getnewvnode while we do the I/O
211 * associated with discarding the buffers unless we
212 * are being forcibly unmounted in which case we already
213 * have our own reference.
214 */
215 if (ap->a_vp->v_usecount > 0)
3b568787 216 (void) nfs_vinvalbuf(ap->a_vp, 0, ap->a_td, 1);
41a01a4d 217 else if (vget(ap->a_vp, NULL, 0, ap->a_td))
984263bc
MD
218 panic("nfs_inactive: lost vnode");
219 else {
3b568787 220 (void) nfs_vinvalbuf(ap->a_vp, 0, ap->a_td, 1);
984263bc
MD
221 vrele(ap->a_vp);
222 }
223 /*
224 * Remove the silly file that was rename'd earlier
225 */
226 nfs_removeit(sp);
227 crfree(sp->s_cred);
228 vrele(sp->s_dvp);
229 FREE((caddr_t)sp, M_NFSREQ);
230 }
231 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
232 NQNFSNONCACHE | NQNFSWRITE);
41a01a4d 233 VOP_UNLOCK(ap->a_vp, NULL, 0, ap->a_td);
984263bc
MD
234 return (0);
235}
236
237/*
238 * Reclaim an nfsnode so that it can be used for other purposes.
e851b29e
CP
239 *
240 * nfs_reclaim(struct vnode *a_vp)
984263bc
MD
241 */
242int
e851b29e 243nfs_reclaim(struct vop_reclaim_args *ap)
984263bc 244{
40393ded
RG
245 struct vnode *vp = ap->a_vp;
246 struct nfsnode *np = VTONFS(vp);
247 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
248 struct nfsdmap *dp, *dp2;
984263bc
MD
249
250 if (prtactive && vp->v_usecount != 0)
251 vprint("nfs_reclaim: pushing active", vp);
252
253 if (np->n_hash.le_prev != NULL)
254 LIST_REMOVE(np, n_hash);
255
256 /*
257 * For nqnfs, take it off the timer queue as required.
258 */
259 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
260 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
261 }
262
263 /*
264 * Free up any directory cookie structures and
265 * large file handle structures that might be associated with
266 * this nfs node.
267 */
268 if (vp->v_type == VDIR) {
269 dp = np->n_cookies.lh_first;
270 while (dp) {
271 dp2 = dp;
272 dp = dp->ndm_list.le_next;
273 FREE((caddr_t)dp2, M_NFSDIROFF);
274 }
275 }
276 if (np->n_fhsize > NFS_SMALLFH) {
277 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
278 }
c1cf1e59
MD
279 if (np->n_rucred) {
280 crfree(np->n_rucred);
281 np->n_rucred = NULL;
282 }
283 if (np->n_wucred) {
284 crfree(np->n_wucred);
285 np->n_wucred = NULL;
286 }
984263bc
MD
287
288 cache_purge(vp);
3446c007
MD
289 vp->v_data = NULL;
290 zfree(nfsnode_zone, np);
984263bc
MD
291 return (0);
292}
293
294#if 0
295/*
296 * Lock an nfsnode
e851b29e
CP
297 *
298 * nfs_lock(struct vnode *a_vp)
984263bc
MD
299 */
300int
e851b29e 301nfs_lock(struct vop_lock_args *ap)
984263bc 302{
40393ded 303 struct vnode *vp = ap->a_vp;
984263bc
MD
304
305 /*
306 * Ugh, another place where interruptible mounts will get hung.
307 * If you make this sleep interruptible, then you have to fix all
308 * the VOP_LOCK() calls to expect interruptibility.
309 */
310 while (vp->v_flag & VXLOCK) {
311 vp->v_flag |= VXWANT;
377d4740 312 (void) tsleep((caddr_t)vp, 0, "nfslck", 0);
984263bc
MD
313 }
314 if (vp->v_tag == VT_NON)
315 return (ENOENT);
316
317#if 0
318 /*
319 * Only lock regular files. If a server crashed while we were
320 * holding a directory lock, we could easily end up sleeping
321 * until the server rebooted while holding a lock on the root.
322 * Locks are only needed for protecting critical sections in
323 * VMIO at the moment.
324 * New vnodes will have type VNON but they should be locked
325 * since they may become VREG. This is checked in loadattrcache
326 * and unwanted locks are released there.
327 */
328 if (vp->v_type == VREG || vp->v_type == VNON) {
329 while (np->n_flag & NLOCKED) {
330 np->n_flag |= NWANTED;
377d4740 331 (void) tsleep((caddr_t) np, 0, "nfslck2", 0);
984263bc
MD
332 /*
333 * If the vnode has transmuted into a VDIR while we
334 * were asleep, then skip the lock.
335 */
336 if (vp->v_type != VREG && vp->v_type != VNON)
337 return (0);
338 }
339 np->n_flag |= NLOCKED;
340 }
341#endif
342
343 return (0);
344}
345
346/*
347 * Unlock an nfsnode
e851b29e
CP
348 *
349 * nfs_unlock(struct vnode *a_vp)
984263bc
MD
350 */
351int
e851b29e 352nfs_unlock(struct vop_unlock_args *ap)
984263bc
MD
353{
354#if 0
355 struct vnode* vp = ap->a_vp;
356 struct nfsnode* np = VTONFS(vp);
357
358 if (vp->v_type == VREG || vp->v_type == VNON) {
359 if (!(np->n_flag & NLOCKED))
360 panic("nfs_unlock: nfsnode not locked");
361 np->n_flag &= ~NLOCKED;
362 if (np->n_flag & NWANTED) {
363 np->n_flag &= ~NWANTED;
364 wakeup((caddr_t) np);
365 }
366 }
367#endif
368
369 return (0);
370}
371
372/*
373 * Check for a locked nfsnode
e851b29e
CP
374 *
375 * nfs_islocked(struct vnode *a_vp, struct thread *a_td)
984263bc
MD
376 */
377int
e851b29e 378nfs_islocked(struct vop_islocked_args *ap)
984263bc
MD
379{
380 return VTONFS(ap->a_vp)->n_flag & NLOCKED ? 1 : 0;
381}
382#endif
383