xref: /freebsd/sys/fs/nfsclient/nfs_clnode.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from nfs_node.c	8.6 (Berkeley) 5/22/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/taskqueue.h>
51 #include <sys/vnode.h>
52 
53 #include <vm/uma.h>
54 
55 #include <fs/nfs/nfsport.h>
56 #include <fs/nfsclient/nfsnode.h>
57 #include <fs/nfsclient/nfsmount.h>
58 #include <fs/nfsclient/nfs.h>
59 #include <fs/nfsclient/nfs_kdtrace.h>
60 
61 #include <nfs/nfs_lock.h>
62 
63 extern struct vop_vector newnfs_vnodeops;
64 extern struct buf_ops buf_ops_newnfs;
65 MALLOC_DECLARE(M_NEWNFSREQ);
66 
67 uma_zone_t newnfsnode_zone;
68 
69 const char nfs_vnode_tag[] = "nfs";
70 
71 static void	nfs_freesillyrename(void *arg, __unused int pending);
72 
73 void
74 ncl_nhinit(void)
75 {
76 
77 	newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
78 	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
79 }
80 
81 void
82 ncl_nhuninit(void)
83 {
84 	uma_zdestroy(newnfsnode_zone);
85 }
86 
87 /*
88  * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
89  * function is going to be used to get Regular Files, code must be added
90  * to fill in the "struct nfsv4node".
91  * Look up a vnode/nfsnode by file handle.
92  * Callers must check for mount points!!
93  * In all cases, a pointer to a
94  * nfsnode structure is returned.
95  */
96 int
97 ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
98     int lkflags)
99 {
100 	struct thread *td = curthread;	/* XXX */
101 	struct nfsnode *np;
102 	struct vnode *vp;
103 	struct vnode *nvp;
104 	int error;
105 	u_int hash;
106 	struct nfsmount *nmp;
107 	struct nfsfh *nfhp;
108 
109 	nmp = VFSTONFS(mntp);
110 	*npp = NULL;
111 
112 	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
113 
114 	nfhp = malloc(sizeof (struct nfsfh) + fhsize,
115 	    M_NFSFH, M_WAITOK);
116 	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
117 	nfhp->nfh_len = fhsize;
118 	error = vfs_hash_get(mntp, hash, lkflags,
119 	    td, &nvp, newnfs_vncmpf, nfhp);
120 	free(nfhp, M_NFSFH);
121 	if (error)
122 		return (error);
123 	if (nvp != NULL) {
124 		*npp = VTONFS(nvp);
125 		return (0);
126 	}
127 	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
128 
129 	error = getnewvnode(nfs_vnode_tag, mntp, &newnfs_vnodeops, &nvp);
130 	if (error) {
131 		uma_zfree(newnfsnode_zone, np);
132 		return (error);
133 	}
134 	vp = nvp;
135 	KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
136 	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
137 	vp->v_data = np;
138 	np->n_vnode = vp;
139 	/*
140 	 * Initialize the mutex even if the vnode is going to be a loser.
141 	 * This simplifies the logic in reclaim, which can then unconditionally
142 	 * destroy the mutex (in the case of the loser, or if hash_insert
143 	 * happened to return an error no special casing is needed).
144 	 */
145 	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
146 	lockinit(&np->n_excl, PVFS, "nfsupg", VLKTIMEOUT, LK_NOSHARE |
147 	    LK_CANRECURSE);
148 
149 	/*
150 	 * NFS supports recursive and shared locking.
151 	 */
152 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
153 	VN_LOCK_AREC(vp);
154 	VN_LOCK_ASHARE(vp);
155 	/*
156 	 * Are we getting the root? If so, make sure the vnode flags
157 	 * are correct
158 	 */
159 	if ((fhsize == nmp->nm_fhsize) &&
160 	    !bcmp(fhp, nmp->nm_fh, fhsize)) {
161 		if (vp->v_type == VNON)
162 			vp->v_type = VDIR;
163 		vp->v_vflag |= VV_ROOT;
164 	}
165 
166 	vp->v_vflag |= VV_VMSIZEVNLOCK;
167 
168 	np->n_fhp = malloc(sizeof (struct nfsfh) + fhsize,
169 	    M_NFSFH, M_WAITOK);
170 	bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
171 	np->n_fhp->nfh_len = fhsize;
172 	error = insmntque(vp, mntp);
173 	if (error != 0) {
174 		*npp = NULL;
175 		free(np->n_fhp, M_NFSFH);
176 		mtx_destroy(&np->n_mtx);
177 		lockdestroy(&np->n_excl);
178 		uma_zfree(newnfsnode_zone, np);
179 		return (error);
180 	}
181 	error = vfs_hash_insert(vp, hash, lkflags,
182 	    td, &nvp, newnfs_vncmpf, np->n_fhp);
183 	if (error)
184 		return (error);
185 	if (nvp != NULL) {
186 		*npp = VTONFS(nvp);
187 		/* vfs_hash_insert() vput()'s the losing vnode */
188 		return (0);
189 	}
190 	*npp = np;
191 
192 	return (0);
193 }
194 
195 /*
196  * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
197  * deadlock because of a LOR when vrele() locks the directory vnode.
198  */
199 static void
200 nfs_freesillyrename(void *arg, __unused int pending)
201 {
202 	struct sillyrename *sp;
203 
204 	sp = arg;
205 	vrele(sp->s_dvp);
206 	free(sp, M_NEWNFSREQ);
207 }
208 
209 static void
210 ncl_releasesillyrename(struct vnode *vp, struct thread *td)
211 {
212 	struct nfsnode *np;
213 	struct sillyrename *sp;
214 
215 	ASSERT_VOP_ELOCKED(vp, "releasesillyrename");
216 	np = VTONFS(vp);
217 	NFSASSERTNODE(np);
218 	if (vp->v_type != VDIR) {
219 		sp = np->n_sillyrename;
220 		np->n_sillyrename = NULL;
221 	} else
222 		sp = NULL;
223 	if (sp != NULL) {
224 		NFSUNLOCKNODE(np);
225 		(void) ncl_vinvalbuf(vp, 0, td, 1);
226 		/*
227 		 * Remove the silly file that was rename'd earlier
228 		 */
229 		ncl_removeit(sp, vp);
230 		crfree(sp->s_cred);
231 		TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
232 		taskqueue_enqueue(taskqueue_thread, &sp->s_task);
233 		NFSLOCKNODE(np);
234 	}
235 }
236 
237 int
238 ncl_inactive(struct vop_inactive_args *ap)
239 {
240 	struct vnode *vp = ap->a_vp;
241 	struct nfsnode *np;
242 	struct thread *td;
243 	boolean_t retv;
244 
245 	td = curthread;
246 	np = VTONFS(vp);
247 	if (NFS_ISV4(vp) && vp->v_type == VREG) {
248 		NFSLOCKNODE(np);
249 		np->n_openstateid = NULL;
250 		NFSUNLOCKNODE(np);
251 		/*
252 		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
253 		 * Close operations are delayed until now. Any dirty
254 		 * buffers/pages must be flushed before the close, so that the
255 		 * stateid is available for the writes.
256 		 */
257 		if (vp->v_object != NULL) {
258 			VM_OBJECT_WLOCK(vp->v_object);
259 			retv = vm_object_page_clean(vp->v_object, 0, 0,
260 			    OBJPC_SYNC);
261 			VM_OBJECT_WUNLOCK(vp->v_object);
262 		} else
263 			retv = TRUE;
264 		if (retv == TRUE) {
265 			(void)ncl_flush(vp, MNT_WAIT, td, 1, 0);
266 			(void)nfsrpc_close(vp, 1, td);
267 		}
268 	}
269 
270 	NFSLOCKNODE(np);
271 	ncl_releasesillyrename(vp, td);
272 
273 	/*
274 	 * NMODIFIED means that there might be dirty/stale buffers
275 	 * associated with the NFS vnode.
276 	 * NDSCOMMIT means that the file is on a pNFS server and commits
277 	 * should be done to the DS.
278 	 * None of the other flags are meaningful after the vnode is unused.
279 	 */
280 	np->n_flag &= (NMODIFIED | NDSCOMMIT);
281 	NFSUNLOCKNODE(np);
282 	return (0);
283 }
284 
285 /*
286  * Reclaim an nfsnode so that it can be used for other purposes.
287  */
288 int
289 ncl_reclaim(struct vop_reclaim_args *ap)
290 {
291 	struct vnode *vp = ap->a_vp;
292 	struct nfsnode *np = VTONFS(vp);
293 	struct nfsdmap *dp, *dp2;
294 	struct thread *td;
295 	struct mount *mp;
296 
297 	td = curthread;
298 	mp = vp->v_mount;
299 
300 	/*
301 	 * If the NLM is running, give it a chance to abort pending
302 	 * locks.
303 	 */
304 	if (nfs_reclaim_p != NULL)
305 		nfs_reclaim_p(ap);
306 
307 	NFSLOCKNODE(np);
308 	ncl_releasesillyrename(vp, td);
309 
310 	if (NFS_ISV4(vp) && vp->v_type == VREG) {
311 		np->n_openstateid = NULL;
312 		NFSUNLOCKNODE(np);
313 		/*
314 		 * We can now safely close any remaining NFSv4 Opens for
315 		 * this file. Most opens will have already been closed by
316 		 * ncl_inactive(), but there are cases where it is not
317 		 * called, so we need to do it again here.
318 		 */
319 		(void) nfsrpc_close(vp, 1, td);
320 		/*
321 		 * It it unlikely a delegation will still exist, but
322 		 * if one does, it must be returned before calling
323 		 * vfs_hash_remove(), since it cannot be recalled once the
324 		 * nfs node is no longer available.
325 		 */
326 		MNT_ILOCK(mp);
327 		if ((mp->mnt_kern_flag & MNTK_UNMOUNTF) == 0) {
328 			MNT_IUNLOCK(mp);
329 			nfscl_delegreturnvp(vp, td);
330 		} else
331 			MNT_IUNLOCK(mp);
332 	} else
333 		NFSUNLOCKNODE(np);
334 
335 	vfs_hash_remove(vp);
336 
337 	/*
338 	 * Call nfscl_reclaimnode() to save attributes in the delegation,
339 	 * as required.
340 	 */
341 	if (vp->v_type == VREG)
342 		nfscl_reclaimnode(vp);
343 
344 	/*
345 	 * Free up any directory cookie structures and
346 	 * large file handle structures that might be associated with
347 	 * this nfs node.
348 	 */
349 	if (vp->v_type == VDIR) {
350 		dp = LIST_FIRST(&np->n_cookies);
351 		while (dp) {
352 			dp2 = dp;
353 			dp = LIST_NEXT(dp, ndm_list);
354 			free(dp2, M_NFSDIROFF);
355 		}
356 	}
357 	if (np->n_writecred != NULL)
358 		crfree(np->n_writecred);
359 	free(np->n_fhp, M_NFSFH);
360 	if (np->n_v4 != NULL)
361 		free(np->n_v4, M_NFSV4NODE);
362 	mtx_destroy(&np->n_mtx);
363 	lockdestroy(&np->n_excl);
364 	uma_zfree(newnfsnode_zone, vp->v_data);
365 	vp->v_data = NULL;
366 	return (0);
367 }
368 
369 /*
370  * Invalidate both the access and attribute caches for this vnode.
371  */
372 void
373 ncl_invalcaches(struct vnode *vp)
374 {
375 	struct nfsnode *np = VTONFS(vp);
376 	int i;
377 
378 	NFSLOCKNODE(np);
379 	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
380 		np->n_accesscache[i].stamp = 0;
381 	KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
382 	np->n_attrstamp = 0;
383 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
384 	NFSUNLOCKNODE(np);
385 }
386