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