xref: /freebsd/sys/fs/nfsclient/nfs_clnode.c (revision 7cd2dcf07629713e5a3d60472cfe4701b705a167)
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 "opt_kdtrace.h"
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 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 
126 	/*
127 	 * Allocate before getnewvnode since doing so afterward
128 	 * might cause a bogus v_data pointer to get dereferenced
129 	 * elsewhere if zalloc should block.
130 	 */
131 	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
132 
133 	error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
134 	if (error) {
135 		uma_zfree(newnfsnode_zone, np);
136 		return (error);
137 	}
138 	vp = nvp;
139 	KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
140 	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
141 	vp->v_data = np;
142 	np->n_vnode = vp;
143 	/*
144 	 * Initialize the mutex even if the vnode is going to be a loser.
145 	 * This simplifies the logic in reclaim, which can then unconditionally
146 	 * destroy the mutex (in the case of the loser, or if hash_insert
147 	 * happened to return an error no special casing is needed).
148 	 */
149 	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
150 	/*
151 	 * NFS supports recursive and shared locking.
152 	 */
153 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
154 	VN_LOCK_AREC(vp);
155 	VN_LOCK_ASHARE(vp);
156 	/*
157 	 * Are we getting the root? If so, make sure the vnode flags
158 	 * are correct
159 	 */
160 	if ((fhsize == nmp->nm_fhsize) &&
161 	    !bcmp(fhp, nmp->nm_fh, fhsize)) {
162 		if (vp->v_type == VNON)
163 			vp->v_type = VDIR;
164 		vp->v_vflag |= VV_ROOT;
165 	}
166 
167 	MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
168 	    M_NFSFH, M_WAITOK);
169 	bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
170 	np->n_fhp->nfh_len = fhsize;
171 	error = insmntque(vp, mntp);
172 	if (error != 0) {
173 		*npp = NULL;
174 		FREE((caddr_t)np->n_fhp, M_NFSFH);
175 		mtx_destroy(&np->n_mtx);
176 		uma_zfree(newnfsnode_zone, np);
177 		return (error);
178 	}
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 int
208 ncl_inactive(struct vop_inactive_args *ap)
209 {
210 	struct nfsnode *np;
211 	struct sillyrename *sp;
212 	struct vnode *vp = ap->a_vp;
213 	boolean_t retv;
214 
215 	np = VTONFS(vp);
216 
217 	if (NFS_ISV4(vp) && vp->v_type == VREG) {
218 		/*
219 		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
220 		 * Close operations are delayed until now. Any dirty
221 		 * buffers/pages must be flushed before the close, so that the
222 		 * stateid is available for the writes.
223 		 */
224 		if (vp->v_object != NULL) {
225 			VM_OBJECT_LOCK(vp->v_object);
226 			retv = vm_object_page_clean(vp->v_object, 0, 0,
227 			    OBJPC_SYNC);
228 			VM_OBJECT_UNLOCK(vp->v_object);
229 		} else
230 			retv = TRUE;
231 		if (retv == TRUE) {
232 			(void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
233 			(void)nfsrpc_close(vp, 1, ap->a_td);
234 		}
235 	}
236 
237 	mtx_lock(&np->n_mtx);
238 	if (vp->v_type != VDIR) {
239 		sp = np->n_sillyrename;
240 		np->n_sillyrename = NULL;
241 	} else
242 		sp = NULL;
243 	if (sp) {
244 		mtx_unlock(&np->n_mtx);
245 		(void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
246 		/*
247 		 * Remove the silly file that was rename'd earlier
248 		 */
249 		ncl_removeit(sp, vp);
250 		crfree(sp->s_cred);
251 		TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
252 		taskqueue_enqueue(taskqueue_thread, &sp->s_task);
253 		mtx_lock(&np->n_mtx);
254 	}
255 	np->n_flag &= NMODIFIED;
256 	mtx_unlock(&np->n_mtx);
257 	return (0);
258 }
259 
260 /*
261  * Reclaim an nfsnode so that it can be used for other purposes.
262  */
263 int
264 ncl_reclaim(struct vop_reclaim_args *ap)
265 {
266 	struct vnode *vp = ap->a_vp;
267 	struct nfsnode *np = VTONFS(vp);
268 	struct nfsdmap *dp, *dp2;
269 
270 	/*
271 	 * If the NLM is running, give it a chance to abort pending
272 	 * locks.
273 	 */
274 	if (nfs_reclaim_p != NULL)
275 		nfs_reclaim_p(ap);
276 
277 	/*
278 	 * Destroy the vm object and flush associated pages.
279 	 */
280 	vnode_destroy_vobject(vp);
281 
282 	if (NFS_ISV4(vp) && vp->v_type == VREG)
283 		/*
284 		 * We can now safely close any remaining NFSv4 Opens for
285 		 * this file. Most opens will have already been closed by
286 		 * ncl_inactive(), but there are cases where it is not
287 		 * called, so we need to do it again here.
288 		 */
289 		(void) nfsrpc_close(vp, 1, ap->a_td);
290 
291 	vfs_hash_remove(vp);
292 
293 	/*
294 	 * Call nfscl_reclaimnode() to save attributes in the delegation,
295 	 * as required.
296 	 */
297 	if (vp->v_type == VREG)
298 		nfscl_reclaimnode(vp);
299 
300 	/*
301 	 * Free up any directory cookie structures and
302 	 * large file handle structures that might be associated with
303 	 * this nfs node.
304 	 */
305 	if (vp->v_type == VDIR) {
306 		dp = LIST_FIRST(&np->n_cookies);
307 		while (dp) {
308 			dp2 = dp;
309 			dp = LIST_NEXT(dp, ndm_list);
310 			FREE((caddr_t)dp2, M_NFSDIROFF);
311 		}
312 	}
313 	if (np->n_writecred != NULL)
314 		crfree(np->n_writecred);
315 	FREE((caddr_t)np->n_fhp, M_NFSFH);
316 	if (np->n_v4 != NULL)
317 		FREE((caddr_t)np->n_v4, M_NFSV4NODE);
318 	mtx_destroy(&np->n_mtx);
319 	uma_zfree(newnfsnode_zone, vp->v_data);
320 	vp->v_data = NULL;
321 	return (0);
322 }
323 
324 /*
325  * Invalidate both the access and attribute caches for this vnode.
326  */
327 void
328 ncl_invalcaches(struct vnode *vp)
329 {
330 	struct nfsnode *np = VTONFS(vp);
331 	int i;
332 
333 	mtx_lock(&np->n_mtx);
334 	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
335 		np->n_accesscache[i].stamp = 0;
336 	KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
337 	np->n_attrstamp = 0;
338 	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
339 	mtx_unlock(&np->n_mtx);
340 }
341 
342