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 * from 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/fcntl.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 <fs/nfs/nfsport.h>
54 #include <fs/nfsclient/nfsnode.h>
55 #include <fs/nfsclient/nfsmount.h>
56 #include <fs/nfsclient/nfs.h>
57
58 #include <nfs/nfs_lock.h>
59
60 extern struct vop_vector newnfs_vnodeops;
61 extern struct buf_ops buf_ops_newnfs;
62 MALLOC_DECLARE(M_NEWNFSREQ);
63
64 uma_zone_t newnfsnode_zone;
65
66 static void nfs_freesillyrename(void *arg, __unused int pending);
67
68 void
69 ncl_nhinit(void)
70 {
71
72 newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
73 NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
74 }
75
76 void
77 ncl_nhuninit(void)
78 {
79 uma_zdestroy(newnfsnode_zone);
80 }
81
82 /*
83 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
84 * function is going to be used to get Regular Files, code must be added
85 * to fill in the "struct nfsv4node".
86 * Look up a vnode/nfsnode by file handle.
87 * Callers must check for mount points!!
88 * In all cases, a pointer to a
89 * nfsnode structure is returned.
90 */
91 int
92 ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
93 int lkflags)
94 {
95 struct thread *td = curthread; /* XXX */
96 struct nfsnode *np;
97 struct vnode *vp;
98 struct vnode *nvp;
99 int error;
100 u_int hash;
101 struct nfsmount *nmp;
102 struct nfsfh *nfhp;
103
104 nmp = VFSTONFS(mntp);
105 *npp = NULL;
106
107 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
108
109 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
110 M_NFSFH, M_WAITOK);
111 bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
112 nfhp->nfh_len = fhsize;
113 error = vfs_hash_get(mntp, hash, lkflags,
114 td, &nvp, newnfs_vncmpf, nfhp);
115 FREE(nfhp, M_NFSFH);
116 if (error)
117 return (error);
118 if (nvp != NULL) {
119 *npp = VTONFS(nvp);
120 return (0);
121 }
122
123 /*
124 * Allocate before getnewvnode since doing so afterward
125 * might cause a bogus v_data pointer to get dereferenced
126 * elsewhere if zalloc should block.
127 */
128 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
129
130 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
131 if (error) {
132 uma_zfree(newnfsnode_zone, np);
133 return (error);
134 }
135 vp = nvp;
136 KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
137 vp->v_bufobj.bo_ops = &buf_ops_newnfs;
138 vp->v_data = np;
139 np->n_vnode = vp;
140 /*
141 * Initialize the mutex even if the vnode is going to be a loser.
142 * This simplifies the logic in reclaim, which can then unconditionally
143 * destroy the mutex (in the case of the loser, or if hash_insert
144 * happened to return an error no special casing is needed).
145 */
146 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
147 /*
148 * NFS supports recursive and shared locking.
149 */
150 VN_LOCK_AREC(vp);
151 VN_LOCK_ASHARE(vp);
152 /*
153 * Are we getting the root? If so, make sure the vnode flags
154 * are correct
155 */
156 if ((fhsize == nmp->nm_fhsize) &&
157 !bcmp(fhp, nmp->nm_fh, fhsize)) {
158 if (vp->v_type == VNON)
159 vp->v_type = VDIR;
160 vp->v_vflag |= VV_ROOT;
161 }
162
163 MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
164 M_NFSFH, M_WAITOK);
165 bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
166 np->n_fhp->nfh_len = fhsize;
167 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
168 error = insmntque(vp, mntp);
169 if (error != 0) {
170 *npp = NULL;
171 FREE((caddr_t)np->n_fhp, M_NFSFH);
172 mtx_destroy(&np->n_mtx);
173 uma_zfree(newnfsnode_zone, np);
174 return (error);
175 }
176 error = vfs_hash_insert(vp, hash, lkflags,
177 td, &nvp, newnfs_vncmpf, np->n_fhp);
178 if (error)
179 return (error);
180 if (nvp != NULL) {
181 *npp = VTONFS(nvp);
182 /* vfs_hash_insert() vput()'s the losing vnode */
183 return (0);
184 }
185 *npp = np;
186
187 return (0);
188 }
189
190 /*
191 * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
192 * deadlock because of a LOR when vrele() locks the directory vnode.
193 */
194 static void
195 nfs_freesillyrename(void *arg, __unused int pending)
196 {
197 struct sillyrename *sp;
198
199 sp = arg;
200 vrele(sp->s_dvp);
201 free(sp, M_NEWNFSREQ);
202 }
203
204 int
205 ncl_inactive(struct vop_inactive_args *ap)
206 {
207 struct nfsnode *np;
208 struct sillyrename *sp;
209 struct vnode *vp = ap->a_vp;
210 boolean_t retv;
211
212 np = VTONFS(vp);
213
214 if (NFS_ISV4(vp) && vp->v_type == VREG) {
215 /*
216 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
217 * Close operations are delayed until now. Any dirty
218 * buffers/pages must be flushed before the close, so that the
219 * stateid is available for the writes.
220 */
221 if (vp->v_object != NULL) {
222 VM_OBJECT_LOCK(vp->v_object);
223 retv = vm_object_page_clean(vp->v_object, 0, 0,
224 OBJPC_SYNC);
225 VM_OBJECT_UNLOCK(vp->v_object);
226 } else
227 retv = TRUE;
228 if (retv == TRUE) {
229 (void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
230 (void)nfsrpc_close(vp, 1, ap->a_td);
231 }
232 }
233
234 mtx_lock(&np->n_mtx);
235 if (vp->v_type != VDIR) {
236 sp = np->n_sillyrename;
237 np->n_sillyrename = NULL;
238 } else
239 sp = NULL;
240 if (sp) {
241 mtx_unlock(&np->n_mtx);
242 (void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
243 /*
244 * Remove the silly file that was rename'd earlier
245 */
246 ncl_removeit(sp, vp);
247 crfree(sp->s_cred);
248 TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
249 taskqueue_enqueue(taskqueue_thread, &sp->s_task);
250 mtx_lock(&np->n_mtx);
251 }
252 np->n_flag &= NMODIFIED;
253 mtx_unlock(&np->n_mtx);
254 return (0);
255 }
256
257 /*
258 * Reclaim an nfsnode so that it can be used for other purposes.
259 */
260 int
261 ncl_reclaim(struct vop_reclaim_args *ap)
262 {
263 struct vnode *vp = ap->a_vp;
264 struct nfsnode *np = VTONFS(vp);
265 struct nfsdmap *dp, *dp2;
266
267 /*
268 * If the NLM is running, give it a chance to abort pending
269 * locks.
270 */
271 if (nfs_reclaim_p != NULL)
272 nfs_reclaim_p(ap);
273
274 /*
275 * Destroy the vm object and flush associated pages.
276 */
277 vnode_destroy_vobject(vp);
278
279 if (NFS_ISV4(vp) && vp->v_type == VREG)
280 /*
281 * We can now safely close any remaining NFSv4 Opens for
282 * this file. Most opens will have already been closed by
283 * ncl_inactive(), but there are cases where it is not
284 * called, so we need to do it again here.
285 */
286 (void) nfsrpc_close(vp, 1, ap->a_td);
287
288 vfs_hash_remove(vp);
289
290 /*
291 * Call nfscl_reclaimnode() to save attributes in the delegation,
292 * as required.
293 */
294 if (vp->v_type == VREG)
295 nfscl_reclaimnode(vp);
296
297 /*
298 * Free up any directory cookie structures and
299 * large file handle structures that might be associated with
300 * this nfs node.
301 */
302 if (vp->v_type == VDIR) {
303 dp = LIST_FIRST(&np->n_cookies);
304 while (dp) {
305 dp2 = dp;
306 dp = LIST_NEXT(dp, ndm_list);
307 FREE((caddr_t)dp2, M_NFSDIROFF);
308 }
309 }
310 if (np->n_writecred != NULL)
311 crfree(np->n_writecred);
312 FREE((caddr_t)np->n_fhp, M_NFSFH);
313 if (np->n_v4 != NULL)
314 FREE((caddr_t)np->n_v4, M_NFSV4NODE);
315 mtx_destroy(&np->n_mtx);
316 uma_zfree(newnfsnode_zone, vp->v_data);
317 vp->v_data = NULL;
318 return (0);
319 }
320
321 /*
322 * Invalidate both the access and attribute caches for this vnode.
323 */
324 void
325 ncl_invalcaches(struct vnode *vp)
326 {
327 struct nfsnode *np = VTONFS(vp);
328 int i;
329
330 mtx_lock(&np->n_mtx);
331 for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
332 np->n_accesscache[i].stamp = 0;
333 np->n_attrstamp = 0;
334 mtx_unlock(&np->n_mtx);
335 }
336
Cache object: 4a935ad616d1e9d433688d152ceb728b
|