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: releng/8.0/sys/nfsclient/nfs_node.c 197192 2009-09-14 15:16:17Z rmacklem $");
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/mbuf.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/proc.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50
51 #include <vm/uma.h>
52
53 #include <nfs/nfsproto.h>
54 #include <nfsclient/nfs.h>
55 #include <nfsclient/nfsnode.h>
56 #include <nfsclient/nfsmount.h>
57
58 static uma_zone_t nfsnode_zone;
59
60 #define TRUE 1
61 #define FALSE 0
62
63 void
64 nfs_nhinit(void)
65 {
66
67 nfsnode_zone = uma_zcreate("NFSNODE", sizeof(struct nfsnode), NULL,
68 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
69 }
70
71 void
72 nfs_nhuninit(void)
73 {
74 uma_zdestroy(nfsnode_zone);
75 }
76
77 struct nfs_vncmp {
78 int fhsize;
79 void *fh;
80 };
81
82 static int
83 nfs_vncmpf(struct vnode *vp, void *arg)
84 {
85 struct nfs_vncmp *a;
86 struct nfsnode *np;
87
88 a = arg;
89 np = VTONFS(vp);
90 return (bcmp(a->fh, np->n_fhp, a->fhsize));
91 }
92
93 /*
94 * Look up a vnode/nfsnode by file handle.
95 * Callers must check for mount points!!
96 * In all cases, a pointer to a
97 * nfsnode structure is returned.
98 */
99 int
100 nfs_nget(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp, int flags)
101 {
102 struct thread *td = curthread; /* XXX */
103 struct nfsnode *np;
104 struct vnode *vp;
105 struct vnode *nvp;
106 int error;
107 u_int hash;
108 struct nfsmount *nmp;
109 struct nfs_vncmp ncmp;
110
111 nmp = VFSTONFS(mntp);
112 *npp = NULL;
113
114 hash = fnv_32_buf(fhp->fh_bytes, fhsize, FNV1_32_INIT);
115 ncmp.fhsize = fhsize;
116 ncmp.fh = fhp;
117
118 error = vfs_hash_get(mntp, hash, flags,
119 td, &nvp, nfs_vncmpf, &ncmp);
120 if (error)
121 return (error);
122 if (nvp != NULL) {
123 *npp = VTONFS(nvp);
124 return (0);
125 }
126
127 /*
128 * Allocate before getnewvnode since doing so afterward
129 * might cause a bogus v_data pointer to get dereferenced
130 * elsewhere if zalloc should block.
131 */
132 np = uma_zalloc(nfsnode_zone, M_WAITOK | M_ZERO);
133
134 error = getnewvnode("nfs", mntp, &nfs_vnodeops, &nvp);
135 if (error) {
136 uma_zfree(nfsnode_zone, np);
137 return (error);
138 }
139 vp = nvp;
140 vp->v_bufobj.bo_ops = &buf_ops_nfs;
141 vp->v_data = np;
142 np->n_vnode = vp;
143 /*
144 * Initialize the mutex even if the vnode is going to be a loser.
145 * This simplifies the logic in reclaim, which can then unconditionally
146 * destroy the mutex (in the case of the loser, or if hash_insert happened
147 * to return an error no special casing is needed).
148 */
149 mtx_init(&np->n_mtx, "NFSnode lock", NULL, MTX_DEF);
150 /*
151 * NFS supports recursive and shared locking.
152 */
153 VN_LOCK_AREC(vp);
154 VN_LOCK_ASHARE(vp);
155 if (fhsize > NFS_SMALLFH) {
156 np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
157 } else
158 np->n_fhp = &np->n_fh;
159 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
160 np->n_fhsize = fhsize;
161 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
162 error = insmntque(vp, mntp);
163 if (error != 0) {
164 *npp = NULL;
165 if (np->n_fhsize > NFS_SMALLFH) {
166 free((caddr_t)np->n_fhp, M_NFSBIGFH);
167 }
168 mtx_destroy(&np->n_mtx);
169 uma_zfree(nfsnode_zone, np);
170 return (error);
171 }
172 error = vfs_hash_insert(vp, hash, flags,
173 td, &nvp, nfs_vncmpf, &ncmp);
174 if (error)
175 return (error);
176 if (nvp != NULL) {
177 *npp = VTONFS(nvp);
178 /* vfs_hash_insert() vput()'s the losing vnode */
179 return (0);
180 }
181 *npp = np;
182
183 return (0);
184 }
185
186 int
187 nfs_inactive(struct vop_inactive_args *ap)
188 {
189 struct nfsnode *np;
190 struct sillyrename *sp;
191 struct thread *td = curthread; /* XXX */
192
193 np = VTONFS(ap->a_vp);
194 if (prtactive && vrefcnt(ap->a_vp) != 0)
195 vprint("nfs_inactive: pushing active", ap->a_vp);
196 if (ap->a_vp->v_type != VDIR) {
197 sp = np->n_sillyrename;
198 np->n_sillyrename = NULL;
199 } else
200 sp = NULL;
201 if (sp) {
202 (void)nfs_vinvalbuf(ap->a_vp, 0, td, 1);
203 /*
204 * Remove the silly file that was rename'd earlier
205 */
206 (sp->s_removeit)(sp);
207 crfree(sp->s_cred);
208 vrele(sp->s_dvp);
209 free((caddr_t)sp, M_NFSREQ);
210 }
211 np->n_flag &= NMODIFIED;
212 return (0);
213 }
214
215 /*
216 * Reclaim an nfsnode so that it can be used for other purposes.
217 */
218 int
219 nfs_reclaim(struct vop_reclaim_args *ap)
220 {
221 struct vnode *vp = ap->a_vp;
222 struct nfsnode *np = VTONFS(vp);
223 struct nfsdmap *dp, *dp2;
224
225 if (prtactive && vrefcnt(vp) != 0)
226 vprint("nfs_reclaim: pushing active", vp);
227
228 /*
229 * If the NLM is running, give it a chance to abort pending
230 * locks.
231 */
232 if (nfs_reclaim_p)
233 nfs_reclaim_p(ap);
234
235 /*
236 * Destroy the vm object and flush associated pages.
237 */
238 vnode_destroy_vobject(vp);
239
240 vfs_hash_remove(vp);
241
242 /*
243 * Free up any directory cookie structures and
244 * large file handle structures that might be associated with
245 * this nfs node.
246 */
247 if (vp->v_type == VDIR) {
248 dp = LIST_FIRST(&np->n_cookies);
249 while (dp) {
250 dp2 = dp;
251 dp = LIST_NEXT(dp, ndm_list);
252 free((caddr_t)dp2, M_NFSDIROFF);
253 }
254 }
255 if (np->n_fhsize > NFS_SMALLFH) {
256 free((caddr_t)np->n_fhp, M_NFSBIGFH);
257 }
258 mtx_destroy(&np->n_mtx);
259 uma_zfree(nfsnode_zone, vp->v_data);
260 vp->v_data = NULL;
261 return (0);
262 }
Cache object: 68e144060779efc5131b018c2fc6218a
|