nfs_clnode.c revision 220732
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 220732 2011-04-16 23:20:21Z rmacklem $");
37191783Srmacklem
38191783Srmacklem#include <sys/param.h>
39191783Srmacklem#include <sys/systm.h>
40214048Srmacklem#include <sys/fcntl.h>
41191783Srmacklem#include <sys/lock.h>
42191783Srmacklem#include <sys/malloc.h>
43191783Srmacklem#include <sys/mount.h>
44191783Srmacklem#include <sys/namei.h>
45191783Srmacklem#include <sys/proc.h>
46191783Srmacklem#include <sys/socket.h>
47191783Srmacklem#include <sys/sysctl.h>
48191783Srmacklem#include <sys/vnode.h>
49191783Srmacklem
50191783Srmacklem#include <vm/uma.h>
51191783Srmacklem
52191783Srmacklem#include <fs/nfs/nfsport.h>
53191783Srmacklem#include <fs/nfsclient/nfsnode.h>
54191783Srmacklem#include <fs/nfsclient/nfsmount.h>
55191783Srmacklem#include <fs/nfsclient/nfs.h>
56191783Srmacklem
57214048Srmacklem#include <nfs/nfs_lock.h>
58214048Srmacklem
59191783Srmacklemextern struct vop_vector newnfs_vnodeops;
60191783Srmacklemextern struct buf_ops buf_ops_newnfs;
61191783SrmacklemMALLOC_DECLARE(M_NEWNFSREQ);
62191783Srmacklem
63191783Srmacklemuma_zone_t newnfsnode_zone;
64191783Srmacklem
65191783Srmacklemvoid
66191783Srmacklemncl_nhinit(void)
67191783Srmacklem{
68191783Srmacklem
69191783Srmacklem	newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
70191783Srmacklem	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
71191783Srmacklem}
72191783Srmacklem
73191783Srmacklemvoid
74191783Srmacklemncl_nhuninit(void)
75191783Srmacklem{
76191783Srmacklem	uma_zdestroy(newnfsnode_zone);
77191783Srmacklem}
78191783Srmacklem
79191783Srmacklem/*
80191783Srmacklem * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
81191783Srmacklem * function is going to be used to get Regular Files, code must be added
82191783Srmacklem * to fill in the "struct nfsv4node".
83191783Srmacklem * Look up a vnode/nfsnode by file handle.
84191783Srmacklem * Callers must check for mount points!!
85191783Srmacklem * In all cases, a pointer to a
86191783Srmacklem * nfsnode structure is returned.
87191783Srmacklem */
88191783Srmacklemint
89220732Srmacklemncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
90220732Srmacklem    int lkflags)
91191783Srmacklem{
92191783Srmacklem	struct thread *td = curthread;	/* XXX */
93191783Srmacklem	struct nfsnode *np;
94191783Srmacklem	struct vnode *vp;
95191783Srmacklem	struct vnode *nvp;
96191783Srmacklem	int error;
97191783Srmacklem	u_int hash;
98191783Srmacklem	struct nfsmount *nmp;
99191783Srmacklem	struct nfsfh *nfhp;
100191783Srmacklem
101191783Srmacklem	nmp = VFSTONFS(mntp);
102191783Srmacklem	*npp = NULL;
103191783Srmacklem
104191783Srmacklem	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
105191783Srmacklem
106191783Srmacklem	MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
107191783Srmacklem	    M_NFSFH, M_WAITOK);
108191783Srmacklem	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
109191783Srmacklem	nfhp->nfh_len = fhsize;
110220732Srmacklem	error = vfs_hash_get(mntp, hash, lkflags,
111191783Srmacklem	    td, &nvp, newnfs_vncmpf, nfhp);
112191783Srmacklem	FREE(nfhp, M_NFSFH);
113191783Srmacklem	if (error)
114191783Srmacklem		return (error);
115191783Srmacklem	if (nvp != NULL) {
116191783Srmacklem		*npp = VTONFS(nvp);
117191783Srmacklem		return (0);
118191783Srmacklem	}
119191783Srmacklem
120191783Srmacklem	/*
121191783Srmacklem	 * Allocate before getnewvnode since doing so afterward
122191783Srmacklem	 * might cause a bogus v_data pointer to get dereferenced
123191783Srmacklem	 * elsewhere if zalloc should block.
124191783Srmacklem	 */
125191783Srmacklem	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
126191783Srmacklem
127191783Srmacklem	error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
128191783Srmacklem	if (error) {
129191783Srmacklem		uma_zfree(newnfsnode_zone, np);
130191783Srmacklem		return (error);
131191783Srmacklem	}
132191783Srmacklem	vp = nvp;
133191783Srmacklem	vp->v_bufobj.bo_ops = &buf_ops_newnfs;
134191783Srmacklem	vp->v_data = np;
135191783Srmacklem	np->n_vnode = vp;
136191783Srmacklem	/*
137191783Srmacklem	 * Initialize the mutex even if the vnode is going to be a loser.
138191783Srmacklem	 * This simplifies the logic in reclaim, which can then unconditionally
139191783Srmacklem	 * destroy the mutex (in the case of the loser, or if hash_insert
140191783Srmacklem	 * happened to return an error no special casing is needed).
141191783Srmacklem	 */
142191783Srmacklem	mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK);
143191783Srmacklem	/*
144191783Srmacklem	 * NFS supports recursive and shared locking.
145191783Srmacklem	 */
146211531Sjhb	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
147191783Srmacklem	VN_LOCK_AREC(vp);
148191783Srmacklem	VN_LOCK_ASHARE(vp);
149191783Srmacklem	/*
150191783Srmacklem	 * Are we getting the root? If so, make sure the vnode flags
151191783Srmacklem	 * are correct
152191783Srmacklem	 */
153191783Srmacklem	if ((fhsize == nmp->nm_fhsize) &&
154191783Srmacklem	    !bcmp(fhp, nmp->nm_fh, fhsize)) {
155191783Srmacklem		if (vp->v_type == VNON)
156191783Srmacklem			vp->v_type = VDIR;
157191783Srmacklem		vp->v_vflag |= VV_ROOT;
158191783Srmacklem	}
159191783Srmacklem
160191783Srmacklem	MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
161191783Srmacklem	    M_NFSFH, M_WAITOK);
162191783Srmacklem	bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
163191783Srmacklem	np->n_fhp->nfh_len = fhsize;
164191783Srmacklem	error = insmntque(vp, mntp);
165191783Srmacklem	if (error != 0) {
166191783Srmacklem		*npp = NULL;
167191783Srmacklem		FREE((caddr_t)np->n_fhp, M_NFSFH);
168191783Srmacklem		mtx_destroy(&np->n_mtx);
169191783Srmacklem		uma_zfree(newnfsnode_zone, np);
170191783Srmacklem		return (error);
171191783Srmacklem	}
172220732Srmacklem	error = vfs_hash_insert(vp, hash, lkflags,
173191783Srmacklem	    td, &nvp, newnfs_vncmpf, np->n_fhp);
174191783Srmacklem	if (error)
175191783Srmacklem		return (error);
176191783Srmacklem	if (nvp != NULL) {
177191783Srmacklem		*npp = VTONFS(nvp);
178191783Srmacklem		/* vfs_hash_insert() vput()'s the losing vnode */
179191783Srmacklem		return (0);
180191783Srmacklem	}
181191783Srmacklem	*npp = np;
182191783Srmacklem
183191783Srmacklem	return (0);
184191783Srmacklem}
185191783Srmacklem
186191783Srmacklemint
187191783Srmacklemncl_inactive(struct vop_inactive_args *ap)
188191783Srmacklem{
189191783Srmacklem	struct nfsnode *np;
190191783Srmacklem	struct sillyrename *sp;
191193125Srmacklem	struct vnode *vp = ap->a_vp;
192191783Srmacklem
193193125Srmacklem	np = VTONFS(vp);
194192337Srmacklem
195193125Srmacklem	if (NFS_ISV4(vp) && vp->v_type == VREG) {
196192928Srmacklem		/*
197192928Srmacklem		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
198192928Srmacklem		 * Close operations are delayed until now. Any dirty buffers
199192928Srmacklem		 * must be flushed before the close, so that the stateid is
200192928Srmacklem		 * available for the writes.
201192928Srmacklem		 */
202207350Srmacklem		(void) ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
203193125Srmacklem		(void) nfsrpc_close(vp, 1, ap->a_td);
204192928Srmacklem	}
205192337Srmacklem
206220731Srmacklem	mtx_lock(&np->n_mtx);
207193125Srmacklem	if (vp->v_type != VDIR) {
208191783Srmacklem		sp = np->n_sillyrename;
209191783Srmacklem		np->n_sillyrename = NULL;
210191783Srmacklem	} else
211191783Srmacklem		sp = NULL;
212191783Srmacklem	if (sp) {
213220731Srmacklem		mtx_unlock(&np->n_mtx);
214193125Srmacklem		(void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
215191783Srmacklem		/*
216191783Srmacklem		 * Remove the silly file that was rename'd earlier
217191783Srmacklem		 */
218193125Srmacklem		ncl_removeit(sp, vp);
219191783Srmacklem		crfree(sp->s_cred);
220191783Srmacklem		vrele(sp->s_dvp);
221191783Srmacklem		FREE((caddr_t)sp, M_NEWNFSREQ);
222220731Srmacklem		mtx_lock(&np->n_mtx);
223191783Srmacklem	}
224191783Srmacklem	np->n_flag &= NMODIFIED;
225220731Srmacklem	mtx_unlock(&np->n_mtx);
226191783Srmacklem	return (0);
227191783Srmacklem}
228191783Srmacklem
229191783Srmacklem/*
230191783Srmacklem * Reclaim an nfsnode so that it can be used for other purposes.
231191783Srmacklem */
232191783Srmacklemint
233191783Srmacklemncl_reclaim(struct vop_reclaim_args *ap)
234191783Srmacklem{
235191783Srmacklem	struct vnode *vp = ap->a_vp;
236191783Srmacklem	struct nfsnode *np = VTONFS(vp);
237191783Srmacklem	struct nfsdmap *dp, *dp2;
238191783Srmacklem
239214511Srmacklem	if (NFS_ISV4(vp) && vp->v_type == VREG)
240214511Srmacklem		/*
241214511Srmacklem		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
242214511Srmacklem		 * Close operations are delayed until ncl_inactive().
243214511Srmacklem		 * However, since VOP_INACTIVE() is not guaranteed to be
244214511Srmacklem		 * called, we need to do it again here.
245214511Srmacklem		 */
246214511Srmacklem		(void) nfsrpc_close(vp, 1, ap->a_td);
247214511Srmacklem
248191783Srmacklem	/*
249191783Srmacklem	 * If the NLM is running, give it a chance to abort pending
250191783Srmacklem	 * locks.
251191783Srmacklem	 */
252214048Srmacklem	if (nfs_reclaim_p != NULL)
253214048Srmacklem		nfs_reclaim_p(ap);
254191783Srmacklem
255191783Srmacklem	/*
256191783Srmacklem	 * Destroy the vm object and flush associated pages.
257191783Srmacklem	 */
258191783Srmacklem	vnode_destroy_vobject(vp);
259191783Srmacklem
260191783Srmacklem	vfs_hash_remove(vp);
261191783Srmacklem
262191783Srmacklem	/*
263191783Srmacklem	 * Call nfscl_reclaimnode() to save attributes in the delegation,
264191783Srmacklem	 * as required.
265191783Srmacklem	 */
266191783Srmacklem	if (vp->v_type == VREG)
267191783Srmacklem		nfscl_reclaimnode(vp);
268191783Srmacklem
269191783Srmacklem	/*
270191783Srmacklem	 * Free up any directory cookie structures and
271191783Srmacklem	 * large file handle structures that might be associated with
272191783Srmacklem	 * this nfs node.
273191783Srmacklem	 */
274191783Srmacklem	if (vp->v_type == VDIR) {
275191783Srmacklem		dp = LIST_FIRST(&np->n_cookies);
276191783Srmacklem		while (dp) {
277191783Srmacklem			dp2 = dp;
278191783Srmacklem			dp = LIST_NEXT(dp, ndm_list);
279191783Srmacklem			FREE((caddr_t)dp2, M_NFSDIROFF);
280191783Srmacklem		}
281191783Srmacklem	}
282191783Srmacklem	FREE((caddr_t)np->n_fhp, M_NFSFH);
283191783Srmacklem	if (np->n_v4 != NULL)
284191783Srmacklem		FREE((caddr_t)np->n_v4, M_NFSV4NODE);
285191783Srmacklem	mtx_destroy(&np->n_mtx);
286191783Srmacklem	uma_zfree(newnfsnode_zone, vp->v_data);
287191783Srmacklem	vp->v_data = NULL;
288191783Srmacklem	return (0);
289191783Srmacklem}
290191783Srmacklem
291191783Srmacklem/*
292191783Srmacklem * Invalidate both the access and attribute caches for this vnode.
293191783Srmacklem */
294191783Srmacklemvoid
295191783Srmacklemncl_invalcaches(struct vnode *vp)
296191783Srmacklem{
297191783Srmacklem	struct nfsnode *np = VTONFS(vp);
298191783Srmacklem	int i;
299191783Srmacklem
300191783Srmacklem	mtx_lock(&np->n_mtx);
301191783Srmacklem	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
302191783Srmacklem		np->n_accesscache[i].stamp = 0;
303191783Srmacklem	np->n_attrstamp = 0;
304191783Srmacklem	mtx_unlock(&np->n_mtx);
305191783Srmacklem}
306191783Srmacklem
307