nfs_clnode.c revision 220732
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: head/sys/fs/nfsclient/nfs_clnode.c 220732 2011-04-16 23:20:21Z rmacklem $");
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/fcntl.h>
41#include <sys/lock.h>
42#include <sys/malloc.h>
43#include <sys/mount.h>
44#include <sys/namei.h>
45#include <sys/proc.h>
46#include <sys/socket.h>
47#include <sys/sysctl.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
57#include <nfs/nfs_lock.h>
58
59extern struct vop_vector newnfs_vnodeops;
60extern struct buf_ops buf_ops_newnfs;
61MALLOC_DECLARE(M_NEWNFSREQ);
62
63uma_zone_t newnfsnode_zone;
64
65void
66ncl_nhinit(void)
67{
68
69	newnfsnode_zone = uma_zcreate("NCLNODE", sizeof(struct nfsnode), NULL,
70	    NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
71}
72
73void
74ncl_nhuninit(void)
75{
76	uma_zdestroy(newnfsnode_zone);
77}
78
79/*
80 * ONLY USED FOR THE ROOT DIRECTORY. nfscl_nget() does the rest. If this
81 * function is going to be used to get Regular Files, code must be added
82 * to fill in the "struct nfsv4node".
83 * Look up a vnode/nfsnode by file handle.
84 * Callers must check for mount points!!
85 * In all cases, a pointer to a
86 * nfsnode structure is returned.
87 */
88int
89ncl_nget(struct mount *mntp, u_int8_t *fhp, int fhsize, struct nfsnode **npp,
90    int lkflags)
91{
92	struct thread *td = curthread;	/* XXX */
93	struct nfsnode *np;
94	struct vnode *vp;
95	struct vnode *nvp;
96	int error;
97	u_int hash;
98	struct nfsmount *nmp;
99	struct nfsfh *nfhp;
100
101	nmp = VFSTONFS(mntp);
102	*npp = NULL;
103
104	hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT);
105
106	MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
107	    M_NFSFH, M_WAITOK);
108	bcopy(fhp, &nfhp->nfh_fh[0], fhsize);
109	nfhp->nfh_len = fhsize;
110	error = vfs_hash_get(mntp, hash, lkflags,
111	    td, &nvp, newnfs_vncmpf, nfhp);
112	FREE(nfhp, M_NFSFH);
113	if (error)
114		return (error);
115	if (nvp != NULL) {
116		*npp = VTONFS(nvp);
117		return (0);
118	}
119
120	/*
121	 * Allocate before getnewvnode since doing so afterward
122	 * might cause a bogus v_data pointer to get dereferenced
123	 * elsewhere if zalloc should block.
124	 */
125	np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO);
126
127	error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp);
128	if (error) {
129		uma_zfree(newnfsnode_zone, np);
130		return (error);
131	}
132	vp = nvp;
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	/*
144	 * NFS supports recursive and shared locking.
145	 */
146	lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL);
147	VN_LOCK_AREC(vp);
148	VN_LOCK_ASHARE(vp);
149	/*
150	 * Are we getting the root? If so, make sure the vnode flags
151	 * are correct
152	 */
153	if ((fhsize == nmp->nm_fhsize) &&
154	    !bcmp(fhp, nmp->nm_fh, fhsize)) {
155		if (vp->v_type == VNON)
156			vp->v_type = VDIR;
157		vp->v_vflag |= VV_ROOT;
158	}
159
160	MALLOC(np->n_fhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize,
161	    M_NFSFH, M_WAITOK);
162	bcopy(fhp, np->n_fhp->nfh_fh, fhsize);
163	np->n_fhp->nfh_len = fhsize;
164	error = insmntque(vp, mntp);
165	if (error != 0) {
166		*npp = NULL;
167		FREE((caddr_t)np->n_fhp, M_NFSFH);
168		mtx_destroy(&np->n_mtx);
169		uma_zfree(newnfsnode_zone, np);
170		return (error);
171	}
172	error = vfs_hash_insert(vp, hash, lkflags,
173	    td, &nvp, newnfs_vncmpf, np->n_fhp);
174	if (error)
175		return (error);
176	if (nvp != NULL) {
177		*npp = VTONFS(nvp);
178		/* vfs_hash_insert() vput()'s the losing vnode */
179		return (0);
180	}
181	*npp = np;
182
183	return (0);
184}
185
186int
187ncl_inactive(struct vop_inactive_args *ap)
188{
189	struct nfsnode *np;
190	struct sillyrename *sp;
191	struct vnode *vp = ap->a_vp;
192
193	np = VTONFS(vp);
194
195	if (NFS_ISV4(vp) && vp->v_type == VREG) {
196		/*
197		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
198		 * Close operations are delayed until now. Any dirty buffers
199		 * must be flushed before the close, so that the stateid is
200		 * available for the writes.
201		 */
202		(void) ncl_flush(vp, MNT_WAIT, NULL, ap->a_td, 1, 0);
203		(void) nfsrpc_close(vp, 1, ap->a_td);
204	}
205
206	mtx_lock(&np->n_mtx);
207	if (vp->v_type != VDIR) {
208		sp = np->n_sillyrename;
209		np->n_sillyrename = NULL;
210	} else
211		sp = NULL;
212	if (sp) {
213		mtx_unlock(&np->n_mtx);
214		(void) ncl_vinvalbuf(vp, 0, ap->a_td, 1);
215		/*
216		 * Remove the silly file that was rename'd earlier
217		 */
218		ncl_removeit(sp, vp);
219		crfree(sp->s_cred);
220		vrele(sp->s_dvp);
221		FREE((caddr_t)sp, M_NEWNFSREQ);
222		mtx_lock(&np->n_mtx);
223	}
224	np->n_flag &= NMODIFIED;
225	mtx_unlock(&np->n_mtx);
226	return (0);
227}
228
229/*
230 * Reclaim an nfsnode so that it can be used for other purposes.
231 */
232int
233ncl_reclaim(struct vop_reclaim_args *ap)
234{
235	struct vnode *vp = ap->a_vp;
236	struct nfsnode *np = VTONFS(vp);
237	struct nfsdmap *dp, *dp2;
238
239	if (NFS_ISV4(vp) && vp->v_type == VREG)
240		/*
241		 * Since mmap()'d files do I/O after VOP_CLOSE(), the NFSv4
242		 * Close operations are delayed until ncl_inactive().
243		 * However, since VOP_INACTIVE() is not guaranteed to be
244		 * called, we need to do it again here.
245		 */
246		(void) nfsrpc_close(vp, 1, ap->a_td);
247
248	/*
249	 * If the NLM is running, give it a chance to abort pending
250	 * locks.
251	 */
252	if (nfs_reclaim_p != NULL)
253		nfs_reclaim_p(ap);
254
255	/*
256	 * Destroy the vm object and flush associated pages.
257	 */
258	vnode_destroy_vobject(vp);
259
260	vfs_hash_remove(vp);
261
262	/*
263	 * Call nfscl_reclaimnode() to save attributes in the delegation,
264	 * as required.
265	 */
266	if (vp->v_type == VREG)
267		nfscl_reclaimnode(vp);
268
269	/*
270	 * Free up any directory cookie structures and
271	 * large file handle structures that might be associated with
272	 * this nfs node.
273	 */
274	if (vp->v_type == VDIR) {
275		dp = LIST_FIRST(&np->n_cookies);
276		while (dp) {
277			dp2 = dp;
278			dp = LIST_NEXT(dp, ndm_list);
279			FREE((caddr_t)dp2, M_NFSDIROFF);
280		}
281	}
282	FREE((caddr_t)np->n_fhp, M_NFSFH);
283	if (np->n_v4 != NULL)
284		FREE((caddr_t)np->n_v4, M_NFSV4NODE);
285	mtx_destroy(&np->n_mtx);
286	uma_zfree(newnfsnode_zone, vp->v_data);
287	vp->v_data = NULL;
288	return (0);
289}
290
291/*
292 * Invalidate both the access and attribute caches for this vnode.
293 */
294void
295ncl_invalcaches(struct vnode *vp)
296{
297	struct nfsnode *np = VTONFS(vp);
298	int i;
299
300	mtx_lock(&np->n_mtx);
301	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
302		np->n_accesscache[i].stamp = 0;
303	np->n_attrstamp = 0;
304	mtx_unlock(&np->n_mtx);
305}
306
307