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