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