proc->thread stage 5: BUF/VFS clearance! Remove the ucred argument from
[dragonfly.git] / sys / vfs / nfs / nfs_node.c
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 $
38  * $DragonFly: src/sys/vfs/nfs/nfs_node.c,v 1.4 2003/06/26 05:55:18 dillon Exp $
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
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfsnode.h>
57 #include <nfs/nfsmount.h>
58
59 static vm_zone_t nfsnode_zone;
60 static LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61 static 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  */
70 void
71 nfs_nhinit()
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  */
83 static int nfs_node_hash_lock;
84
85 int
86 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp)
87 {
88         struct thread *td = curthread;  /* XXX */
89         struct nfsnode *np, *np2;
90         struct nfsnodehashhead *nhpp;
91         struct vnode *vp;
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
107 retry:
108         nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
109 loop:
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);
115                 if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL, td))
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;
127                         tsleep(&nfs_node_hash_lock, PVM, "nfsngt", 0);
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                 
140         error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
141         if (error) {
142                 if (nfs_node_hash_lock < 0)
143                         wakeup(&nfs_node_hash_lock);
144                 nfs_node_hash_lock = 0;
145                 *npp = 0;
146                 zfree(nfsnode_zone, np);
147                 return (error);
148         }
149         vp = nvp;
150         bzero((caddr_t)np, sizeof *np);
151         vp->v_data = np;
152         np->n_vnode = vp;
153         /*
154          * Insert the nfsnode in the hash queue for its new file handle
155          */
156         for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
157                 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
158                     bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
159                         continue;
160                 vrele(vp);
161                 if (nfs_node_hash_lock < 0)
162                         wakeup(&nfs_node_hash_lock);
163                 nfs_node_hash_lock = 0;
164                 zfree(nfsnode_zone, np);
165                 goto retry;
166         }
167         LIST_INSERT_HEAD(nhpp, np, n_hash);
168         if (fhsize > NFS_SMALLFH) {
169                 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
170         } else
171                 np->n_fhp = &np->n_fh;
172         bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
173         np->n_fhsize = fhsize;
174         lockinit(&np->n_rslock, PVFS | rsflags, "nfrslk", 0, LK_NOPAUSE);
175         lockinit(&np->n_lock, PVFS, "nfsnlk", 0, LK_NOPAUSE);
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          */
185         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
186
187         return (0);
188 }
189
190 int
191 nfs_inactive(ap)
192         struct vop_inactive_args /* {
193                 struct vnode *a_vp;
194                 struct thread *a_td;
195         } */ *ap;
196 {
197         struct nfsnode *np;
198         struct sillyrename *sp;
199
200         np = VTONFS(ap->a_vp);
201         if (prtactive && ap->a_vp->v_usecount != 0)
202                 vprint("nfs_inactive: pushing active", ap->a_vp);
203         if (ap->a_vp->v_type != VDIR) {
204                 sp = np->n_sillyrename;
205                 np->n_sillyrename = (struct sillyrename *)0;
206         } else
207                 sp = (struct sillyrename *)0;
208         if (sp) {
209                 /*
210                  * We need a reference to keep the vnode from being
211                  * recycled by getnewvnode while we do the I/O
212                  * associated with discarding the buffers unless we
213                  * are being forcibly unmounted in which case we already
214                  * have our own reference.
215                  */
216                 if (ap->a_vp->v_usecount > 0)
217                         (void) nfs_vinvalbuf(ap->a_vp, 0, ap->a_td, 1);
218                 else if (vget(ap->a_vp, 0, ap->a_td))
219                         panic("nfs_inactive: lost vnode");
220                 else {
221                         (void) nfs_vinvalbuf(ap->a_vp, 0, ap->a_td, 1);
222                         vrele(ap->a_vp);
223                 }
224                 /*
225                  * Remove the silly file that was rename'd earlier
226                  */
227                 nfs_removeit(sp);
228                 crfree(sp->s_cred);
229                 vrele(sp->s_dvp);
230                 FREE((caddr_t)sp, M_NFSREQ);
231         }
232         np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
233                 NQNFSNONCACHE | NQNFSWRITE);
234         VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
235         return (0);
236 }
237
238 /*
239  * Reclaim an nfsnode so that it can be used for other purposes.
240  */
241 int
242 nfs_reclaim(ap)
243         struct vop_reclaim_args /* {
244                 struct vnode *a_vp;
245         } */ *ap;
246 {
247         register struct vnode *vp = ap->a_vp;
248         register struct nfsnode *np = VTONFS(vp);
249         register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
250         register struct nfsdmap *dp, *dp2;
251
252         if (prtactive && vp->v_usecount != 0)
253                 vprint("nfs_reclaim: pushing active", vp);
254
255         if (np->n_hash.le_prev != NULL)
256                 LIST_REMOVE(np, n_hash);
257
258         /*
259          * For nqnfs, take it off the timer queue as required.
260          */
261         if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
262                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
263         }
264
265         /*
266          * Free up any directory cookie structures and
267          * large file handle structures that might be associated with
268          * this nfs node.
269          */
270         if (vp->v_type == VDIR) {
271                 dp = np->n_cookies.lh_first;
272                 while (dp) {
273                         dp2 = dp;
274                         dp = dp->ndm_list.le_next;
275                         FREE((caddr_t)dp2, M_NFSDIROFF);
276                 }
277         }
278         if (np->n_fhsize > NFS_SMALLFH) {
279                 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
280         }
281
282         cache_purge(vp);
283         zfree(nfsnode_zone, vp->v_data);
284         vp->v_data = (void *)0;
285         return (0);
286 }
287
288 #if 0
289 /*
290  * Lock an nfsnode
291  */
292 int
293 nfs_lock(ap)
294         struct vop_lock_args /* {
295                 struct vnode *a_vp;
296         } */ *ap;
297 {
298         register struct vnode *vp = ap->a_vp;
299
300         /*
301          * Ugh, another place where interruptible mounts will get hung.
302          * If you make this sleep interruptible, then you have to fix all
303          * the VOP_LOCK() calls to expect interruptibility.
304          */
305         while (vp->v_flag & VXLOCK) {
306                 vp->v_flag |= VXWANT;
307                 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0);
308         }
309         if (vp->v_tag == VT_NON)
310                 return (ENOENT);
311
312 #if 0
313         /*
314          * Only lock regular files.  If a server crashed while we were
315          * holding a directory lock, we could easily end up sleeping
316          * until the server rebooted while holding a lock on the root.
317          * Locks are only needed for protecting critical sections in
318          * VMIO at the moment.
319          * New vnodes will have type VNON but they should be locked
320          * since they may become VREG.  This is checked in loadattrcache
321          * and unwanted locks are released there.
322          */
323         if (vp->v_type == VREG || vp->v_type == VNON) {
324                 while (np->n_flag & NLOCKED) {
325                         np->n_flag |= NWANTED;
326                         (void) tsleep((caddr_t) np, PINOD, "nfslck2", 0);
327                         /*
328                          * If the vnode has transmuted into a VDIR while we
329                          * were asleep, then skip the lock.
330                          */
331                         if (vp->v_type != VREG && vp->v_type != VNON)
332                                 return (0);
333                 }
334                 np->n_flag |= NLOCKED;
335         }
336 #endif
337
338         return (0);
339 }
340
341 /*
342  * Unlock an nfsnode
343  */
344 int
345 nfs_unlock(ap)
346         struct vop_unlock_args /* {
347                 struct vnode *a_vp;
348         } */ *ap;
349 {
350 #if 0
351         struct vnode* vp = ap->a_vp;
352         struct nfsnode* np = VTONFS(vp);
353
354         if (vp->v_type == VREG || vp->v_type == VNON) {
355                 if (!(np->n_flag & NLOCKED))
356                         panic("nfs_unlock: nfsnode not locked");
357                 np->n_flag &= ~NLOCKED;
358                 if (np->n_flag & NWANTED) {
359                         np->n_flag &= ~NWANTED;
360                         wakeup((caddr_t) np);
361                 }
362         }
363 #endif
364
365         return (0);
366 }
367
368 /*
369  * Check for a locked nfsnode
370  */
371 int
372 nfs_islocked(ap)
373         struct vop_islocked_args /* {
374                 struct vnode *a_vp;
375                 struct thread *a_td;
376         } */ *ap;
377 {
378         return VTONFS(ap->a_vp)->n_flag & NLOCKED ? 1 : 0;
379 }
380 #endif
381