Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:54 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(mntp, fhp, fhsize, npp)
87         struct mount *mntp;
88         register nfsfh_t *fhp;
89         int fhsize;
90         struct nfsnode **npp;
91 {
92         struct proc *p = curproc;       /* XXX */
93         struct nfsnode *np, *np2;
94         struct nfsnodehashhead *nhpp;
95         register struct vnode *vp;
96         struct vnode *nvp;
97         int error;
98         int rsflags;
99         struct nfsmount *nmp;
100
101         /*
102          * Calculate nfs mount point and figure out whether the rslock should
103          * be interruptable or not.
104          */
105         nmp = VFSTONFS(mntp);
106         if (nmp->nm_flag & NFSMNT_INT)
107                 rsflags = PCATCH;
108         else
109                 rsflags = 0;
110
111 retry:
112         nhpp = NFSNOHASH(fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT));
113 loop:
114         for (np = nhpp->lh_first; np != 0; 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))
117                         continue;
118                 vp = NFSTOV(np);
119                 if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL, p))
120                         goto loop;
121                 *npp = np;
122                 return(0);
123         }
124         /*
125          * Obtain a lock to prevent a race condition if the getnewvnode()
126          * or MALLOC() below happens to block.
127          */
128         if (nfs_node_hash_lock) {
129                 while (nfs_node_hash_lock) {
130                         nfs_node_hash_lock = -1;
131                         tsleep(&nfs_node_hash_lock, PVM, "nfsngt", 0);
132                 }
133                 goto loop;
134         }
135         nfs_node_hash_lock = 1;
136
137         /*
138          * Allocate before getnewvnode since doing so afterward
139          * might cause a bogus v_data pointer to get dereferenced
140          * elsewhere if zalloc should block.
141          */
142         np = zalloc(nfsnode_zone);
143                 
144         error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
145         if (error) {
146                 if (nfs_node_hash_lock < 0)
147                         wakeup(&nfs_node_hash_lock);
148                 nfs_node_hash_lock = 0;
149                 *npp = 0;
150                 zfree(nfsnode_zone, np);
151                 return (error);
152         }
153         vp = nvp;
154         bzero((caddr_t)np, sizeof *np);
155         vp->v_data = np;
156         np->n_vnode = vp;
157         /*
158          * Insert the nfsnode in the hash queue for its new file handle
159          */
160         for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
161                 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
162                     bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
163                         continue;
164                 vrele(vp);
165                 if (nfs_node_hash_lock < 0)
166                         wakeup(&nfs_node_hash_lock);
167                 nfs_node_hash_lock = 0;
168                 zfree(nfsnode_zone, np);
169                 goto retry;
170         }
171         LIST_INSERT_HEAD(nhpp, np, n_hash);
172         if (fhsize > NFS_SMALLFH) {
173                 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
174         } else
175                 np->n_fhp = &np->n_fh;
176         bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
177         np->n_fhsize = fhsize;
178         lockinit(&np->n_rslock, PVFS | rsflags, "nfrslk", 0, LK_NOPAUSE);
179         lockinit(&np->n_lock, PVFS, "nfsnlk", 0, LK_NOPAUSE);
180         *npp = np;
181
182         if (nfs_node_hash_lock < 0)
183                 wakeup(&nfs_node_hash_lock);
184         nfs_node_hash_lock = 0;
185
186         /*
187          * Lock the new nfsnode.
188          */
189         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
190
191         return (0);
192 }
193
194 int
195 nfs_inactive(ap)
196         struct vop_inactive_args /* {
197                 struct vnode *a_vp;
198                 struct proc *a_p;
199         } */ *ap;
200 {
201         register struct nfsnode *np;
202         register struct sillyrename *sp;
203         struct proc *p = curproc;       /* XXX */
204
205         np = VTONFS(ap->a_vp);
206         if (prtactive && ap->a_vp->v_usecount != 0)
207                 vprint("nfs_inactive: pushing active", ap->a_vp);
208         if (ap->a_vp->v_type != VDIR) {
209                 sp = np->n_sillyrename;
210                 np->n_sillyrename = (struct sillyrename *)0;
211         } else
212                 sp = (struct sillyrename *)0;
213         if (sp) {
214                 /*
215                  * We need a reference to keep the vnode from being
216                  * recycled by getnewvnode while we do the I/O
217                  * associated with discarding the buffers unless we
218                  * are being forcibly unmounted in which case we already
219                  * have our own reference.
220                  */
221                 if (ap->a_vp->v_usecount > 0)
222                         (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
223                 else if (vget(ap->a_vp, 0, p))
224                         panic("nfs_inactive: lost vnode");
225                 else {
226                         (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
227                         vrele(ap->a_vp);
228                 }
229                 /*
230                  * Remove the silly file that was rename'd earlier
231                  */
232                 nfs_removeit(sp);
233                 crfree(sp->s_cred);
234                 vrele(sp->s_dvp);
235                 FREE((caddr_t)sp, M_NFSREQ);
236         }
237         np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
238                 NQNFSNONCACHE | NQNFSWRITE);
239         VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
240         return (0);
241 }
242
243 /*
244  * Reclaim an nfsnode so that it can be used for other purposes.
245  */
246 int
247 nfs_reclaim(ap)
248         struct vop_reclaim_args /* {
249                 struct vnode *a_vp;
250         } */ *ap;
251 {
252         register struct vnode *vp = ap->a_vp;
253         register struct nfsnode *np = VTONFS(vp);
254         register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
255         register struct nfsdmap *dp, *dp2;
256
257         if (prtactive && vp->v_usecount != 0)
258                 vprint("nfs_reclaim: pushing active", vp);
259
260         if (np->n_hash.le_prev != NULL)
261                 LIST_REMOVE(np, n_hash);
262
263         /*
264          * For nqnfs, take it off the timer queue as required.
265          */
266         if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
267                 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
268         }
269
270         /*
271          * Free up any directory cookie structures and
272          * large file handle structures that might be associated with
273          * this nfs node.
274          */
275         if (vp->v_type == VDIR) {
276                 dp = np->n_cookies.lh_first;
277                 while (dp) {
278                         dp2 = dp;
279                         dp = dp->ndm_list.le_next;
280                         FREE((caddr_t)dp2, M_NFSDIROFF);
281                 }
282         }
283         if (np->n_fhsize > NFS_SMALLFH) {
284                 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
285         }
286
287         cache_purge(vp);
288         zfree(nfsnode_zone, vp->v_data);
289         vp->v_data = (void *)0;
290         return (0);
291 }
292
293 #if 0
294 /*
295  * Lock an nfsnode
296  */
297 int
298 nfs_lock(ap)
299         struct vop_lock_args /* {
300                 struct vnode *a_vp;
301         } */ *ap;
302 {
303         register struct vnode *vp = ap->a_vp;
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;
312                 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0);
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;
331                         (void) tsleep((caddr_t) np, PINOD, "nfslck2", 0);
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
348  */
349 int
350 nfs_unlock(ap)
351         struct vop_unlock_args /* {
352                 struct vnode *a_vp;
353         } */ *ap;
354 {
355 #if 0
356         struct vnode* vp = ap->a_vp;
357         struct nfsnode* np = VTONFS(vp);
358
359         if (vp->v_type == VREG || vp->v_type == VNON) {
360                 if (!(np->n_flag & NLOCKED))
361                         panic("nfs_unlock: nfsnode not locked");
362                 np->n_flag &= ~NLOCKED;
363                 if (np->n_flag & NWANTED) {
364                         np->n_flag &= ~NWANTED;
365                         wakeup((caddr_t) np);
366                 }
367         }
368 #endif
369
370         return (0);
371 }
372
373 /*
374  * Check for a locked nfsnode
375  */
376 int
377 nfs_islocked(ap)
378         struct vop_islocked_args /* {
379                 struct vnode *a_vp;
380                 struct proc *a_p;
381         } */ *ap;
382 {
383         return VTONFS(ap->a_vp)->n_flag & NLOCKED ? 1 : 0;
384 }
385 #endif
386