nfs_clnode.c revision 237244
1191783Srmacklem/*-
2191783Srmacklem * Copyright (c) 1989, 1993
3191783Srmacklem *	The Regents of the University of California.  All rights reserved.
4191783Srmacklem *
5191783Srmacklem * This code is derived from software contributed to Berkeley by
6191783Srmacklem * Rick Macklem at The University of Guelph.
7191783Srmacklem *
8191783Srmacklem * Redistribution and use in source and binary forms, with or without
9191783Srmacklem * modification, are permitted provided that the following conditions
10191783Srmacklem * are met:
11191783Srmacklem * 1. Redistributions of source code must retain the above copyright
12191783Srmacklem *    notice, this list of conditions and the following disclaimer.
13191783Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
14191783Srmacklem *    notice, this list of conditions and the following disclaimer in the
15191783Srmacklem *    documentation and/or other materials provided with the distribution.
16191783Srmacklem * 4. Neither the name of the University nor the names of its contributors
17191783Srmacklem *    may be used to endorse or promote products derived from this software
18191783Srmacklem *    without specific prior written permission.
19191783Srmacklem *
20191783Srmacklem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21191783Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22191783Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23191783Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24191783Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25191783Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26191783Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27191783Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28191783Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29191783Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30191783Srmacklem * SUCH DAMAGE.
31191783Srmacklem *
32191783Srmacklem *	from nfs_node.c	8.6 (Berkeley) 5/22/95
33191783Srmacklem */
34191783Srmacklem
35191783Srmacklem#include <sys/cdefs.h>
36191783Srmacklem__FBSDID("$FreeBSD: head/sys/fs/nfsclient/nfs_clnode.c 237244 2012-06-18 22:17:28Z rmacklem $");
37191783Srmacklem
38223280Srmacklem#include "opt_kdtrace.h"
39223280Srmacklem
40191783Srmacklem#include <sys/param.h>
41191783Srmacklem#include <sys/systm.h>
42214048Srmacklem#include <sys/fcntl.h>
43191783Srmacklem#include <sys/lock.h>
44191783Srmacklem#include <sys/malloc.h>
45191783Srmacklem#include <sys/mount.h>
46191783Srmacklem#include <sys/namei.h>
47191783Srmacklem#include <sys/proc.h>
48191783Srmacklem#include <sys/socket.h>
49191783Srmacklem#include <sys/sysctl.h>
50224606Srmacklem#include <sys/taskqueue.h>
51191783Srmacklem#include <sys/vnode.h>
52191783Srmacklem
53191783Srmacklem#include <vm/uma.h>
54191783Srmacklem
55191783Srmacklem#include <fs/nfs/nfsport.h>
56191783Srmacklem#include <fs/nfsclient/nfsnode.h>
57191783Srmacklem#include <fs/nfsclient/nfsmount.h>
58191783Srmacklem#include <fs/nfsclient/nfs.h>
59223280Srmacklem#include <fs/nfsclient/nfs_kdtrace.h>
60191783Srmacklem
61214048Srmacklem#include <nfs/nfs_lock.h>
62214048Srmacklem
63191783Srmacklemextern struct vop_vector newnfs_vnodeops;
64191783Srmacklemextern struct buf_ops buf_ops_newnfs;
65191783SrmacklemMALLOC_DECLARE(M_NEWNFSREQ);
66191783Srmacklem
67191783Srmacklemuma_zone_t newnfsnode_zone;
68191783Srmacklem
69224606Srmacklemstatic void	nfs_freesillyrename(void *arg, __unused int pending);
70224606Srmacklem
71191783Srmacklemvoid
72191783Srmacklemncl_nhinit(void)
73191783Srmacklem{
74191783Srmacklem
75191783Srmacklem	newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
76191783Srmacklem	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
77191783Srmacklem}
78191783Srmacklem
79191783Srmacklemvoid
80191783Srmacklemncl_nhuninit(void)
81191783Srmacklem{
82191783Srmacklem	uma_zdestroy(newnfsnode_zone);
83191783Srmacklem}
84191783Srmacklem
85191783Srmacklem/*
86191783Srmacklem * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
87191783Srmacklem * function is going to be used to get Regular Files, code must be added
88191783Srmacklem * to fill in the "struct nfsv4node".
89191783Srmacklem * Look up a vnode/nfsnode by file handle.
90191783Srmacklem * Callers must check for mount points!!
91191783Srmacklem * In all cases, a pointer to a
92191783Srmacklem * nfsnode structure is returned.
93191783Srmacklem */
94191783Srmacklemint
95220732Srmacklemncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
96220732Srmacklem    int lkflags)
97191783Srmacklem{
98191783Srmacklem	struct thread *td = curthread;	/* XXX */
99191783Srmacklem	struct nfsnode *np;
100191783Srmacklem	struct vnode *vp;
101191783Srmacklem	struct vnode *nvp;
102191783Srmacklem	int error;
103191783Srmacklem	u_int hash;
104191783Srmacklem	struct nfsmount *nmp;
105191783Srmacklem	struct nfsfh *nfhp;
106191783Srmacklem
107191783Srmacklem	nmp = VFSTONFS(mntp);
108191783Srmacklem	*npp = NULL;
109191783Srmacklem
110191783Srmacklem	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
111191783Srmacklem
112191783Srmacklem	MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
113191783Srmacklem	    M_NFSFH, M_WAITOK);
114191783Srmacklem	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
115191783Srmacklem	nfhp->nfh_len = fhsize;
116220732Srmacklem	error = vfs_hash_get(mntp, hash, lkflags,
117191783Srmacklem	    td, &nvp, newnfs_vncmpf, nfhp);
118191783Srmacklem	FREE(nfhp, M_NFSFH);
119191783Srmacklem	if (error)
120191783Srmacklem		return (error);
121191783Srmacklem	if (nvp != NULL) {
122191783Srmacklem		*npp = VTONFS(nvp);
123191783Srmacklem		return (0);
124191783Srmacklem	}
125191783Srmacklem
126191783Srmacklem	/*
127191783Srmacklem	 * Allocate before getnewvnode since doing so afterward
128191783Srmacklem	 * might cause a bogus v_data pointer to get dereferenced
129191783Srmacklem	 * elsewhere if zalloc should block.
130191783Srmacklem	 */
131191783Srmacklem	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
132191783Srmacklem
133191783Srmacklem	error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
134191783Srmacklem	if (error) {
135191783Srmacklem		uma_zfree(newnfsnode_zone, np);
136191783Srmacklem		return (error);
137191783Srmacklem	}
138191783Srmacklem	vp = nvp;
139230605Srmacklem	KASSERT(vp->v_bufobj.bo_bsize != 0, ("ncl_nget: bo_bsize == 0"));
140191783Srmacklem	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
141191783Srmacklem	vp->v_data = np;
142191783Srmacklem	np->n_vnode = vp;
143191783Srmacklem	/*
144191783Srmacklem	 * Initialize the mutex even if the vnode is going to be a loser.
145191783Srmacklem	 * This simplifies the logic in reclaim, which can then unconditionally
146191783Srmacklem	 * destroy the mutex (in the case of the loser, or if hash_insert
147191783Srmacklem	 * happened to return an error no special casing is needed).
148191783Srmacklem	 */
149191783Srmacklem	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
150191783Srmacklem	/*
151191783Srmacklem	 * NFS supports recursive and shared locking.
152191783Srmacklem	 */
153211531Sjhb	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
154191783Srmacklem	VN_LOCK_AREC(vp);
155191783Srmacklem	VN_LOCK_ASHARE(vp);
156191783Srmacklem	/*
157191783Srmacklem	 * Are we getting the root? If so, make sure the vnode flags
158191783Srmacklem	 * are correct
159191783Srmacklem	 */
160191783Srmacklem	if ((fhsize == nmp->nm_fhsize) &&
161191783Srmacklem	    !bcmp(fhp, nmp->nm_fh, fhsize)) {
162191783Srmacklem		if (vp->v_type == VNON)
163191783Srmacklem			vp->v_type = VDIR;
164191783Srmacklem		vp->v_vflag |= VV_ROOT;
165191783Srmacklem	}
166191783Srmacklem
167191783Srmacklem	MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
168191783Srmacklem	    M_NFSFH, M_WAITOK);
169191783Srmacklem	bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
170191783Srmacklem	np->n_fhp->nfh_len = fhsize;
171191783Srmacklem	error = insmntque(vp, mntp);
172191783Srmacklem	if (error != 0) {
173191783Srmacklem		*npp = NULL;
174191783Srmacklem		FREE((caddr_t)np->n_fhp, M_NFSFH);
175191783Srmacklem		mtx_destroy(&np->n_mtx);
176191783Srmacklem		uma_zfree(newnfsnode_zone, np);
177191783Srmacklem		return (error);
178191783Srmacklem	}
179220732Srmacklem	error = vfs_hash_insert(vp, hash, lkflags,
180191783Srmacklem	    td, &nvp, newnfs_vncmpf, np->n_fhp);
181191783Srmacklem	if (error)
182191783Srmacklem		return (error);
183191783Srmacklem	if (nvp != NULL) {
184191783Srmacklem		*npp = VTONFS(nvp);
185191783Srmacklem		/* vfs_hash_insert() vput()'s the losing vnode */
186191783Srmacklem		return (0);
187191783Srmacklem	}
188191783Srmacklem	*npp = np;
189191783Srmacklem
190191783Srmacklem	return (0);
191191783Srmacklem}
192191783Srmacklem
193224606Srmacklem/*
194224606Srmacklem * Do the vrele(sp->s_dvp) as a separate task in order to avoid a
195224606Srmacklem * deadlock because of a LOR when vrele() locks the directory vnode.
196224606Srmacklem */
197224606Srmacklemstatic void
198224606Srmacklemnfs_freesillyrename(void *arg, __unused int pending)
199224606Srmacklem{
200224606Srmacklem	struct sillyrename *sp;
201224606Srmacklem
202224606Srmacklem	sp = arg;
203224606Srmacklem	vrele(sp->s_dvp);
204224606Srmacklem	free(sp, M_NEWNFSREQ);
205224606Srmacklem}
206224606Srmacklem
207191783Srmacklemint
208191783Srmacklemncl_inactive(struct vop_inactive_args *ap)
209191783Srmacklem{
210191783Srmacklem	struct nfsnode *np;
211191783Srmacklem	struct sillyrename *sp;
212193125Srmacklem	struct vnode *vp = ap->a_vp;
213237244Srmacklem	boolean_t retv;
214191783Srmacklem
215193125Srmacklem	np = VTONFS(vp);
216192337Srmacklem
217193125Srmacklem	if (NFS_ISV4(vp) && vp->v_type == VREG) {
218192928Srmacklem		/*
219192928Srmacklem		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
220237244Srmacklem		 * Close operations are delayed until now. Any dirty
221237244Srmacklem		 * buffers/pages must be flushed before the close, so that the
222237244Srmacklem		 * stateid is available for the writes.
223192928Srmacklem		 */
224237244Srmacklem		if (vp->v_object != NULL) {
225237244Srmacklem			VM_OBJECT_LOCK(vp->v_object);
226237244Srmacklem			retv = vm_object_page_clean(vp->v_object, 0, 0,
227237244Srmacklem			    OBJPC_SYNC);
228237244Srmacklem			VM_OBJECT_UNLOCK(vp->v_object);
229237244Srmacklem		} else
230237244Srmacklem			retv = TRUE;
231237244Srmacklem		if (retv == TRUE) {
232237244Srmacklem			(void)ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
233237244Srmacklem			(void)nfsrpc_close(vp, 1, ap->a_td);
234237244Srmacklem		}
235192928Srmacklem	}
236192337Srmacklem
237220731Srmacklem	mtx_lock(&np->n_mtx);
238193125Srmacklem	if (vp->v_type != VDIR) {
239191783Srmacklem		sp = np->n_sillyrename;
240191783Srmacklem		np->n_sillyrename = NULL;
241191783Srmacklem	} else
242191783Srmacklem		sp = NULL;
243191783Srmacklem	if (sp) {
244220731Srmacklem		mtx_unlock(&np->n_mtx);
245193125Srmacklem		(void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
246191783Srmacklem		/*
247191783Srmacklem		 * Remove the silly file that was rename'd earlier
248191783Srmacklem		 */
249193125Srmacklem		ncl_removeit(sp, vp);
250191783Srmacklem		crfree(sp->s_cred);
251224606Srmacklem		TASK_INIT(&sp->s_task, 0, nfs_freesillyrename, sp);
252224606Srmacklem		taskqueue_enqueue(taskqueue_thread, &sp->s_task);
253220731Srmacklem		mtx_lock(&np->n_mtx);
254191783Srmacklem	}
255191783Srmacklem	np->n_flag &= NMODIFIED;
256220731Srmacklem	mtx_unlock(&np->n_mtx);
257191783Srmacklem	return (0);
258191783Srmacklem}
259191783Srmacklem
260191783Srmacklem/*
261191783Srmacklem * Reclaim an nfsnode so that it can be used for other purposes.
262191783Srmacklem */
263191783Srmacklemint
264191783Srmacklemncl_reclaim(struct vop_reclaim_args *ap)
265191783Srmacklem{
266191783Srmacklem	struct vnode *vp = ap->a_vp;
267191783Srmacklem	struct nfsnode *np = VTONFS(vp);
268191783Srmacklem	struct nfsdmap *dp, *dp2;
269191783Srmacklem
270191783Srmacklem	/*
271191783Srmacklem	 * If the NLM is running, give it a chance to abort pending
272191783Srmacklem	 * locks.
273191783Srmacklem	 */
274214048Srmacklem	if (nfs_reclaim_p != NULL)
275214048Srmacklem		nfs_reclaim_p(ap);
276191783Srmacklem
277191783Srmacklem	/*
278191783Srmacklem	 * Destroy the vm object and flush associated pages.
279191783Srmacklem	 */
280191783Srmacklem	vnode_destroy_vobject(vp);
281191783Srmacklem
282237200Srmacklem	if (NFS_ISV4(vp) && vp->v_type == VREG)
283237200Srmacklem		/*
284237200Srmacklem		 * We can now safely close any remaining NFSv4 Opens for
285237200Srmacklem		 * this file. Most opens will have already been closed by
286237200Srmacklem		 * ncl_inactive(), but there are cases where it is not
287237200Srmacklem		 * called, so we need to do it again here.
288237200Srmacklem		 */
289237200Srmacklem		(void) nfsrpc_close(vp, 1, ap->a_td);
290237200Srmacklem
291191783Srmacklem	vfs_hash_remove(vp);
292191783Srmacklem
293191783Srmacklem	/*
294191783Srmacklem	 * Call nfscl_reclaimnode() to save attributes in the delegation,
295191783Srmacklem	 * as required.
296191783Srmacklem	 */
297191783Srmacklem	if (vp->v_type == VREG)
298191783Srmacklem		nfscl_reclaimnode(vp);
299191783Srmacklem
300191783Srmacklem	/*
301191783Srmacklem	 * Free up any directory cookie structures and
302191783Srmacklem	 * large file handle structures that might be associated with
303191783Srmacklem	 * this nfs node.
304191783Srmacklem	 */
305191783Srmacklem	if (vp->v_type == VDIR) {
306191783Srmacklem		dp = LIST_FIRST(&np->n_cookies);
307191783Srmacklem		while (dp) {
308191783Srmacklem			dp2 = dp;
309191783Srmacklem			dp = LIST_NEXT(dp, ndm_list);
310191783Srmacklem			FREE((caddr_t)dp2, M_NFSDIROFF);
311191783Srmacklem		}
312191783Srmacklem	}
313235332Srmacklem	if (np->n_writecred != NULL)
314235332Srmacklem		crfree(np->n_writecred);
315191783Srmacklem	FREE((caddr_t)np->n_fhp, M_NFSFH);
316191783Srmacklem	if (np->n_v4 != NULL)
317191783Srmacklem		FREE((caddr_t)np->n_v4, M_NFSV4NODE);
318191783Srmacklem	mtx_destroy(&np->n_mtx);
319191783Srmacklem	uma_zfree(newnfsnode_zone, vp->v_data);
320191783Srmacklem	vp->v_data = NULL;
321191783Srmacklem	return (0);
322191783Srmacklem}
323191783Srmacklem
324191783Srmacklem/*
325191783Srmacklem * Invalidate both the access and attribute caches for this vnode.
326191783Srmacklem */
327191783Srmacklemvoid
328191783Srmacklemncl_invalcaches(struct vnode *vp)
329191783Srmacklem{
330191783Srmacklem	struct nfsnode *np = VTONFS(vp);
331191783Srmacklem	int i;
332191783Srmacklem
333191783Srmacklem	mtx_lock(&np->n_mtx);
334191783Srmacklem	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
335191783Srmacklem		np->n_accesscache[i].stamp = 0;
336223280Srmacklem	KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
337191783Srmacklem	np->n_attrstamp = 0;
338223280Srmacklem	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
339191783Srmacklem	mtx_unlock(&np->n_mtx);
340191783Srmacklem}
341191783Srmacklem
342