xref: /openbsd-src/sys/nfs/nfs_node.c (revision cd1eb269cafb12c415be1749cd4a4b5422710415)
1 /*	$OpenBSD: nfs_node.c,v 1.51 2009/12/17 16:30:47 beck Exp $	*/
2 /*	$NetBSD: nfs_node.c,v 1.16 1996/02/18 11:53:42 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
36  */
37 
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/pool.h>
48 #include <sys/rwlock.h>
49 #include <sys/queue.h>
50 
51 #include <nfs/rpcv2.h>
52 #include <nfs/nfsproto.h>
53 #include <nfs/nfs.h>
54 #include <nfs/nfsnode.h>
55 #include <nfs/nfsmount.h>
56 #include <nfs/nfs_var.h>
57 
58 struct pool nfs_node_pool;
59 extern int prtactive;
60 
61 struct rwlock nfs_hashlock = RWLOCK_INITIALIZER("nfshshlk");
62 
63 /* filehandle to node lookup. */
64 static __inline int
65 nfsnode_cmp(const struct nfsnode *a, const struct nfsnode *b)
66 {
67 	if (a->n_fhsize != b->n_fhsize)
68 		return (a->n_fhsize - b->n_fhsize);
69 	return (memcmp(a->n_fhp, b->n_fhp, a->n_fhsize));
70 }
71 
72 RB_PROTOTYPE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
73 RB_GENERATE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
74 
75 /*
76  * Look up a vnode/nfsnode by file handle.
77  * Callers must check for mount points!!
78  * In all cases, a pointer to a
79  * nfsnode structure is returned.
80  */
81 int
82 nfs_nget(struct mount *mnt, nfsfh_t *fh, int fhsize, struct nfsnode **npp)
83 {
84 	extern int (**nfsv2_vnodeop_p)(void *);		/* XXX */
85 	struct nfsmount		*nmp;
86 	struct nfsnode		*np, find, *np2;
87 	struct vnode		*vp, *nvp;
88 	struct proc		*p = curproc;		/* XXX */
89 	int			 error;
90 
91 	nmp = VFSTONFS(mnt);
92 
93 loop:
94 	rw_enter_write(&nfs_hashlock);
95 	find.n_fhp = fh;
96 	find.n_fhsize = fhsize;
97 	np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find);
98 	if (np != NULL) {
99 		rw_exit_write(&nfs_hashlock);
100 		vp = NFSTOV(np);
101 		error = vget(vp, LK_EXCLUSIVE, p);
102 		if (error)
103 			goto loop;
104 		*npp = np;
105 		return (0);
106 	}
107 
108 	/*
109 	 * getnewvnode() could recycle a vnode, potentially formerly
110 	 * owned by NFS. This will cause a VOP_RECLAIM() to happen,
111 	 * which will cause recursive locking, so we unlock before
112 	 * calling getnewvnode() lock again afterwards, but must check
113 	 * to see if this nfsnode has been added while we did not hold
114 	 * the lock.
115 	 */
116 	rw_exit_write(&nfs_hashlock);
117 	error = getnewvnode(VT_NFS, mnt, nfsv2_vnodeop_p, &nvp);
118 	/* note that we don't have this vnode set up completely yet */
119 	rw_enter_write(&nfs_hashlock);
120 	if (error) {
121 		*npp = NULL;
122 		rw_exit_write(&nfs_hashlock);
123 		return (error);
124 	}
125 	nvp->v_flag |= VLARVAL;
126 	np = RB_FIND(nfs_nodetree, &nmp->nm_ntree, &find);
127 	if (np != NULL) {
128 		vgone(nvp);
129 		rw_exit_write(&nfs_hashlock);
130 		goto loop;
131 	}
132 
133 	vp = nvp;
134 	np = pool_get(&nfs_node_pool, PR_WAITOK | PR_ZERO);
135 	vp->v_data = np;
136 	/* we now have an nfsnode on this vnode */
137 	vp->v_flag &= ~VLARVAL;
138 	np->n_vnode = vp;
139 
140 	rw_init(&np->n_commitlock, "nfs_commitlk");
141 
142 	/*
143 	 * Are we getting the root? If so, make sure the vnode flags
144 	 * are correct
145 	 */
146 	if ((fhsize == nmp->nm_fhsize) && !bcmp(fh, nmp->nm_fh, fhsize)) {
147 		if (vp->v_type == VNON)
148 			vp->v_type = VDIR;
149 		vp->v_flag |= VROOT;
150 	}
151 
152 	np->n_fhp = &np->n_fh;
153 	bcopy(fh, np->n_fhp, fhsize);
154 	np->n_fhsize = fhsize;
155 	np2 = RB_INSERT(nfs_nodetree, &nmp->nm_ntree, np);
156 	KASSERT(np2 == NULL);
157 	np->n_accstamp = -1;
158 	rw_exit(&nfs_hashlock);
159 	*npp = np;
160 
161 	return (0);
162 }
163 
164 int
165 nfs_inactive(void *v)
166 {
167 	struct vop_inactive_args	*ap = v;
168 	struct nfsnode			*np;
169 	struct sillyrename		*sp;
170 
171 #ifdef DIAGNOSTIC
172 	if (prtactive && ap->a_vp->v_usecount != 0)
173 		vprint("nfs_inactive: pushing active", ap->a_vp);
174 #endif
175 	if (ap->a_vp->v_flag & VLARVAL)
176 		/*
177 		 * vnode was incompletely set up, just return
178 		 * as we are throwing it away.
179 		 */
180 		return(0);
181 #ifdef DIAGNOSTIC
182 	if (ap->a_vp->v_data == NULL)
183 		panic("NULL v_data (no nfsnode set up?) in vnode %p\n",
184 		    ap->a_vp);
185 #endif
186 	np = VTONFS(ap->a_vp);
187 	if (ap->a_vp->v_type != VDIR) {
188 		sp = np->n_sillyrename;
189 		np->n_sillyrename = NULL;
190 	} else
191 		sp = NULL;
192 	if (sp) {
193 		/*
194 		 * Remove the silly file that was rename'd earlier
195 		 */
196 		nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, curproc);
197 		nfs_removeit(sp);
198 		crfree(sp->s_cred);
199 		vrele(sp->s_dvp);
200 		free(sp, M_NFSREQ);
201 	}
202 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT);
203 
204 	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
205 	return (0);
206 }
207 
208 /*
209  * Reclaim an nfsnode so that it can be used for other purposes.
210  */
211 int
212 nfs_reclaim(void *v)
213 {
214 	struct vop_reclaim_args	*ap = v;
215 	struct vnode		*vp = ap->a_vp;
216 	struct nfsmount		*nmp;
217 	struct nfsnode		*np = VTONFS(vp);
218 
219 #ifdef DIAGNOSTIC
220 	if (prtactive && vp->v_usecount != 0)
221 		vprint("nfs_reclaim: pushing active", vp);
222 #endif
223 	if (ap->a_vp->v_flag & VLARVAL)
224 		/*
225 		 * vnode was incompletely set up, just return
226 		 * as we are throwing it away.
227 		 */
228 		return(0);
229 #ifdef DIAGNOSTIC
230 	if (ap->a_vp->v_data == NULL)
231 		panic("NULL v_data (no nfsnode set up?) in vnode %p\n",
232 		    ap->a_vp);
233 #endif
234 	nmp = VFSTONFS(vp->v_mount);
235 	rw_enter_write(&nfs_hashlock);
236 	RB_REMOVE(nfs_nodetree, &nmp->nm_ntree, np);
237 	rw_exit_write(&nfs_hashlock);
238 
239 	if (np->n_rcred)
240 		crfree(np->n_rcred);
241 	if (np->n_wcred)
242 		crfree(np->n_wcred);
243 
244 	cache_purge(vp);
245 	pool_put(&nfs_node_pool, vp->v_data);
246 	vp->v_data = NULL;
247 
248 	return (0);
249 }
250