]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/nfsclient/nfs_node.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / nfsclient / 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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      @(#)nfs_node.c  8.6 (Berkeley) 5/22/95
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/fnv_hash.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/proc.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/vnode.h>
49
50 #include <vm/uma.h>
51
52 #include <rpc/rpcclnt.h>
53
54 #include <nfs/rpcv2.h>
55 #include <nfs/nfsproto.h>
56 #include <nfsclient/nfs.h>
57 #include <nfsclient/nfsnode.h>
58 #include <nfsclient/nfsmount.h>
59
60 static uma_zone_t nfsnode_zone;
61
62 #define TRUE    1
63 #define FALSE   0
64
65 void
66 nfs_nhinit(void)
67 {
68
69         nfsnode_zone = uma_zcreate("NFSNODE", sizeof(struct nfsnode), NULL,
70             NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
71 }
72
73 void
74 nfs_nhuninit(void)
75 {
76         uma_zdestroy(nfsnode_zone);
77 }
78
79 struct nfs_vncmp {
80         int     fhsize;
81         void    *fh;
82 };
83
84 static int
85 nfs_vncmpf(struct vnode *vp, void *arg)
86 {
87         struct nfs_vncmp *a;
88         struct nfsnode *np;
89
90         a = arg;
91         np = VTONFS(vp);
92         return (bcmp(a->fh, np->n_fhp, a->fhsize));
93 }
94
95 /*
96  * Look up a vnode/nfsnode by file handle.
97  * Callers must check for mount points!!
98  * In all cases, a pointer to a
99  * nfsnode structure is returned.
100  */
101 int
102 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp, int flags)
103 {
104         struct thread *td = curthread;  /* XXX */
105         struct nfsnode *np;
106         struct vnode *vp;
107         struct vnode *nvp;
108         int error;
109         u_int hash;
110         struct nfsmount *nmp;
111         struct nfs_vncmp ncmp;
112
113         nmp = VFSTONFS(mntp);
114         *npp = NULL;
115
116         hash = fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT);
117         ncmp.fhsize = fhsize;
118         ncmp.fh = fhp;
119
120         error = vfs_hash_get(mntp, hash, flags,
121             td, &nvp, nfs_vncmpf, &ncmp);
122         if (error)
123                 return (error);
124         if (nvp != NULL) {
125                 *npp = VTONFS(nvp);
126                 return (0);
127         }
128
129         /*
130          * Allocate before getnewvnode since doing so afterward
131          * might cause a bogus v_data pointer to get dereferenced
132          * elsewhere if zalloc should block.
133          */
134         np = uma_zalloc(nfsnode_zone, M_WAITOK | M_ZERO);
135
136         if (nmp->nm_flag & NFSMNT_NFSV4)
137                 error = getnewvnode("nfs4", mntp, &nfs4_vnodeops, &nvp);
138         else
139                 error = getnewvnode("nfs", mntp, &nfs_vnodeops, &nvp);
140         if (error) {
141                 uma_zfree(nfsnode_zone, np);
142                 return (error);
143         }
144         vp = nvp;
145         if (nmp->nm_flag & NFSMNT_NFSV4)
146                 vp->v_bufobj.bo_ops = &buf_ops_nfs4;
147         else
148                 vp->v_bufobj.bo_ops = &buf_ops_nfs;
149         vp->v_data = np;
150         np->n_vnode = vp;
151         /* 
152          * Initialize the mutex even if the vnode is going to be a loser.
153          * This simplifies the logic in reclaim, which can then unconditionally
154          * destroy the mutex (in the case of the loser, or if hash_insert happened
155          * to return an error no special casing is needed).
156          */
157         mtx_init(&np->n_mtx, "NFSnode lock", NULL, MTX_DEF);
158         /*
159          * NFS supports recursive and shared locking.
160          */
161         vp->v_vnlock->lk_flags |= LK_CANRECURSE;
162         vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
163         if (fhsize > NFS_SMALLFH) {
164                 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
165         } else
166                 np->n_fhp = &np->n_fh;
167         bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
168         np->n_fhsize = fhsize;
169         lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
170         error = insmntque(vp, mntp);
171         if (error != 0) {
172                 *npp = NULL;
173                 if (np->n_fhsize > NFS_SMALLFH) {
174                         FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
175                 }
176                 mtx_destroy(&np->n_mtx);
177                 uma_zfree(nfsnode_zone, np);
178                 return (error);
179         }
180         error = vfs_hash_insert(vp, hash, flags, 
181             td, &nvp, nfs_vncmpf, &ncmp);
182         if (error)
183                 return (error);
184         if (nvp != NULL) {
185                 *npp = VTONFS(nvp);
186                 /* vfs_hash_insert() vput()'s the losing vnode */
187                 return (0);
188         }
189         *npp = np;
190
191         return (0);
192 }
193
194 int
195 nfs_inactive(struct vop_inactive_args *ap)
196 {
197         struct nfsnode *np;
198         struct sillyrename *sp;
199         struct thread *td = curthread;  /* XXX */
200
201         np = VTONFS(ap->a_vp);
202         if (prtactive && vrefcnt(ap->a_vp) != 0)
203                 vprint("nfs_inactive: pushing active", ap->a_vp);
204         if (ap->a_vp->v_type != VDIR) {
205                 sp = np->n_sillyrename;
206                 np->n_sillyrename = NULL;
207         } else
208                 sp = NULL;
209         if (sp) {
210                 (void)nfs_vinvalbuf(ap->a_vp, 0, td, 1);
211                 /*
212                  * Remove the silly file that was rename'd earlier
213                  */
214                 (sp->s_removeit)(sp);
215                 crfree(sp->s_cred);
216                 vrele(sp->s_dvp);
217                 FREE((caddr_t)sp, M_NFSREQ);
218         }
219         np->n_flag &= NMODIFIED;
220         return (0);
221 }
222
223 /*
224  * Reclaim an nfsnode so that it can be used for other purposes.
225  */
226 int
227 nfs_reclaim(struct vop_reclaim_args *ap)
228 {
229         struct vnode *vp = ap->a_vp;
230         struct nfsnode *np = VTONFS(vp);
231         struct nfsdmap *dp, *dp2;
232
233         if (prtactive && vrefcnt(vp) != 0)
234                 vprint("nfs_reclaim: pushing active", vp);
235
236         /*
237          * If the NLM is running, give it a chance to abort pending
238          * locks.
239          */
240         if (nfs_reclaim_p)
241                 nfs_reclaim_p(ap);
242
243         /*
244          * Destroy the vm object and flush associated pages.
245          */
246         vnode_destroy_vobject(vp);
247
248         vfs_hash_remove(vp);
249
250         /*
251          * Free up any directory cookie structures and
252          * large file handle structures that might be associated with
253          * this nfs node.
254          */
255         if (vp->v_type == VDIR) {
256                 dp = LIST_FIRST(&np->n_cookies);
257                 while (dp) {
258                         dp2 = dp;
259                         dp = LIST_NEXT(dp, ndm_list);
260                         FREE((caddr_t)dp2, M_NFSDIROFF);
261                 }
262         }
263         if (np->n_fhsize > NFS_SMALLFH) {
264                 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
265         }
266         mtx_destroy(&np->n_mtx);
267         uma_zfree(nfsnode_zone, vp->v_data);
268         vp->v_data = NULL;
269         return (0);
270 }