xref: /openbsd-src/sys/nfs/nfs_node.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /*	$OpenBSD: nfs_node.c,v 1.67 2018/04/09 09:39:53 mpi 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/timeout.h>
42 #include <sys/mount.h>
43 #include <sys/namei.h>
44 #include <sys/vnode.h>
45 #include <sys/lock.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/pool.h>
49 #include <sys/rwlock.h>
50 #include <sys/queue.h>
51 
52 #include <nfs/rpcv2.h>
53 #include <nfs/nfsproto.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 /* XXX */
62 extern struct vops nfs_vops;
63 
64 /* filehandle to node lookup. */
65 static __inline int
66 nfsnode_cmp(const struct nfsnode *a, const struct nfsnode *b)
67 {
68 	if (a->n_fhsize != b->n_fhsize)
69 		return (a->n_fhsize - b->n_fhsize);
70 	return (memcmp(a->n_fhp, b->n_fhp, a->n_fhsize));
71 }
72 
73 RBT_PROTOTYPE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
74 RBT_GENERATE(nfs_nodetree, nfsnode, n_entry, nfsnode_cmp);
75 
76 void
77 nfs_ninit(struct nfsmount *nmp)
78 {
79 	RBT_INIT(nfs_nodetree, &nmp->nm_ntree);
80 }
81 
82 /*
83  * Look up a vnode/nfsnode by file handle and store the pointer in *npp.
84  * Callers must check for mount points!!
85  * An error number is returned.
86  */
87 int
88 nfs_nget(struct mount *mnt, nfsfh_t *fh, int fhsize, struct nfsnode **npp)
89 {
90 	struct nfsmount		*nmp;
91 	struct nfsnode		*np, find, *np2;
92 	struct vnode		*vp, *nvp;
93 	struct proc		*p = curproc;		/* XXX */
94 	int			 error;
95 
96 	nmp = VFSTONFS(mnt);
97 
98 loop:
99 	find.n_fhp = fh;
100 	find.n_fhsize = fhsize;
101 	np = RBT_FIND(nfs_nodetree, &nmp->nm_ntree, &find);
102 	if (np != NULL) {
103 		vp = NFSTOV(np);
104 		error = vget(vp, LK_EXCLUSIVE, p);
105 		if (error)
106 			goto loop;
107 		*npp = np;
108 		return (0);
109 	}
110 
111 	/*
112 	 * getnewvnode() could recycle a vnode, potentially formerly
113 	 * owned by NFS. This will cause a VOP_RECLAIM() to happen,
114 	 * which will cause recursive locking, so we unlock before
115 	 * calling getnewvnode() lock again afterwards, but must check
116 	 * to see if this nfsnode has been added while we did not hold
117 	 * the lock.
118 	 */
119 	error = getnewvnode(VT_NFS, mnt, &nfs_vops, &nvp);
120 	/* note that we don't have this vnode set up completely yet */
121 	if (error) {
122 		*npp = NULL;
123 		return (error);
124 	}
125 	nvp->v_flag |= VLARVAL;
126 	np = pool_get(&nfs_node_pool, PR_WAITOK | PR_ZERO);
127 	/*
128 	 * getnewvnode() and pool_get() can sleep, check for race.
129 	 */
130 	if (RBT_FIND(nfs_nodetree, &nmp->nm_ntree, &find) != NULL) {
131 		pool_put(&nfs_node_pool, np);
132 		vgone(nvp);
133 		goto loop;
134 	}
135 
136 	vp = nvp;
137 	vp->v_data = np;
138 	/* we now have an nfsnode on this vnode */
139 	vp->v_flag &= ~VLARVAL;
140 	np->n_vnode = vp;
141 	rw_init(&np->n_commitlock, "nfs_commitlk");
142 	np->n_fhp = &np->n_fh;
143 	bcopy(fh, np->n_fhp, fhsize);
144 	np->n_fhsize = fhsize;
145 	np2 = RBT_INSERT(nfs_nodetree, &nmp->nm_ntree, np);
146 	KASSERT(np2 == NULL);
147 	np->n_accstamp = -1;
148 	*npp = np;
149 
150 	return (0);
151 }
152 
153 int
154 nfs_inactive(void *v)
155 {
156 	struct vop_inactive_args	*ap = v;
157 	struct nfsnode			*np;
158 	struct sillyrename		*sp;
159 
160 #ifdef DIAGNOSTIC
161 	if (prtactive && ap->a_vp->v_usecount != 0)
162 		vprint("nfs_inactive: pushing active", ap->a_vp);
163 #endif
164 	if (ap->a_vp->v_flag & VLARVAL)
165 		/*
166 		 * vnode was incompletely set up, just return
167 		 * as we are throwing it away.
168 		 */
169 		return(0);
170 #ifdef DIAGNOSTIC
171 	if (ap->a_vp->v_data == NULL)
172 		panic("NULL v_data (no nfsnode set up?) in vnode %p",
173 		    ap->a_vp);
174 #endif
175 	np = VTONFS(ap->a_vp);
176 	if (ap->a_vp->v_type != VDIR) {
177 		sp = np->n_sillyrename;
178 		np->n_sillyrename = NULL;
179 	} else
180 		sp = NULL;
181 	if (sp) {
182 		/*
183 		 * Remove the silly file that was rename'd earlier
184 		 */
185 		nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, curproc);
186 		nfs_removeit(sp);
187 		crfree(sp->s_cred);
188 		vrele(sp->s_dvp);
189 		free(sp, M_NFSREQ, sizeof(*sp));
190 	}
191 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT);
192 
193 	VOP_UNLOCK(ap->a_vp, ap->a_p);
194 	return (0);
195 }
196 
197 /*
198  * Reclaim an nfsnode so that it can be used for other purposes.
199  */
200 int
201 nfs_reclaim(void *v)
202 {
203 	struct vop_reclaim_args	*ap = v;
204 	struct vnode		*vp = ap->a_vp;
205 	struct nfsmount		*nmp;
206 	struct nfsnode		*np = VTONFS(vp);
207 
208 #ifdef DIAGNOSTIC
209 	if (prtactive && vp->v_usecount != 0)
210 		vprint("nfs_reclaim: pushing active", vp);
211 #endif
212 	if (ap->a_vp->v_flag & VLARVAL)
213 		/*
214 		 * vnode was incompletely set up, just return
215 		 * as we are throwing it away.
216 		 */
217 		return(0);
218 #ifdef DIAGNOSTIC
219 	if (ap->a_vp->v_data == NULL)
220 		panic("NULL v_data (no nfsnode set up?) in vnode %p",
221 		    ap->a_vp);
222 #endif
223 	nmp = VFSTONFS(vp->v_mount);
224 	RBT_REMOVE(nfs_nodetree, &nmp->nm_ntree, np);
225 
226 	if (np->n_rcred)
227 		crfree(np->n_rcred);
228 	if (np->n_wcred)
229 		crfree(np->n_wcred);
230 
231 	cache_purge(vp);
232 	pool_put(&nfs_node_pool, vp->v_data);
233 	vp->v_data = NULL;
234 
235 	return (0);
236 }
237