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