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