xref: /openbsd-src/sys/nfs/nfs_node.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: nfs_node.c,v 1.25 2002/07/02 04:23:25 ericj 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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
40  */
41 
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/pool.h>
52 #include <sys/hash.h>
53 
54 #include <nfs/rpcv2.h>
55 #include <nfs/nfsproto.h>
56 #include <nfs/nfs.h>
57 #include <nfs/nfsnode.h>
58 #include <nfs/nfsmount.h>
59 #include <nfs/nfs_var.h>
60 
61 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
62 u_long nfsnodehash;
63 struct lock nfs_hashlock;
64 
65 struct pool nfs_node_pool;
66 
67 extern int prtactive;
68 
69 #define TRUE	1
70 #define	FALSE	0
71 
72 #define	nfs_hash(x,y)	hash32_buf((x), (y), HASHINIT)
73 
74 /*
75  * Initialize hash links for nfsnodes
76  * and build nfsnode free list.
77  */
78 void
79 nfs_nhinit()
80 {
81 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, M_WAITOK, &nfsnodehash);
82 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
83 
84 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
85 	    &pool_allocator_nointr);
86 }
87 
88 /*
89  * Look up a vnode/nfsnode by file handle.
90  * Callers must check for mount points!!
91  * In all cases, a pointer to a
92  * nfsnode structure is returned.
93  */
94 int
95 nfs_nget(mntp, fhp, fhsize, npp)
96 	struct mount *mntp;
97 	nfsfh_t *fhp;
98 	int fhsize;
99 	struct nfsnode **npp;
100 {
101 	struct proc *p = curproc;	/* XXX */
102 	struct nfsnode *np;
103 	struct nfsnodehashhead *nhpp;
104 	struct vnode *vp;
105 	extern int (**nfsv2_vnodeop_p)(void *);
106 	struct vnode *nvp;
107 	int error;
108 
109 	nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
110 loop:
111 	for (np = LIST_FIRST(nhpp); np != NULL; np = LIST_NEXT(np, n_hash)) {
112 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
113 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
114 			continue;
115 		vp = NFSTOV(np);
116 		if (vget(vp, LK_EXCLUSIVE, p))
117 			goto loop;
118 		*npp = np;
119 		return(0);
120 	}
121 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, NULL, p))
122 		goto loop;
123 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
124 	if (error) {
125 		*npp = 0;
126 		lockmgr(&nfs_hashlock, LK_RELEASE, NULL, p);
127 		return (error);
128 	}
129 	vp = nvp;
130 	np = pool_get(&nfs_node_pool, PR_WAITOK);
131 	bzero((caddr_t)np, sizeof *np);
132 	vp->v_data = np;
133 	np->n_vnode = vp;
134 
135 	/*
136 	 * Are we getting the root? If so, make sure the vnode flags
137 	 * are correct
138 	 */
139 	{
140 		struct nfsmount *nmp = VFSTONFS(mntp);
141 		if ((fhsize == nmp->nm_fhsize) &&
142 		    !bcmp(fhp, nmp->nm_fh, fhsize)) {
143 			if (vp->v_type == VNON)
144 				vp->v_type = VDIR;
145 			vp->v_flag |= VROOT;
146 		}
147 	}
148 
149 	LIST_INSERT_HEAD(nhpp, np, n_hash);
150 	if (fhsize > NFS_SMALLFH) {
151 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
152 	} else
153 		np->n_fhp = &np->n_fh;
154 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
155 	np->n_fhsize = fhsize;
156 	lockmgr(&nfs_hashlock, LK_RELEASE, 0, p);
157 	*npp = np;
158 	return (0);
159 }
160 
161 int
162 nfs_inactive(v)
163 	void *v;
164 {
165 	struct vop_inactive_args /* {
166 		struct vnode *a_vp;
167 		struct proc *a_p;
168 	} */ *ap = v;
169 	struct nfsnode *np;
170 	struct sillyrename *sp;
171 	struct proc *p = curproc;	/* XXX */
172 
173 	np = VTONFS(ap->a_vp);
174 	if (prtactive && ap->a_vp->v_usecount != 0)
175 		vprint("nfs_inactive: pushing active", ap->a_vp);
176 	if (ap->a_vp->v_type != VDIR) {
177 		sp = np->n_sillyrename;
178 		np->n_sillyrename = (struct sillyrename *)0;
179 	} else
180 		sp = (struct sillyrename *)0;
181 	if (sp) {
182 		/*
183 		 * Remove the silly file that was rename'd earlier
184 		 */
185 		(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
186 		nfs_removeit(sp);
187 		crfree(sp->s_cred);
188 		vrele(sp->s_dvp);
189 		FREE((caddr_t)sp, M_NFSREQ);
190 	}
191 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT);
192 
193 	VOP_UNLOCK(ap->a_vp, 0, 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(v)
202 	void *v;
203 {
204 	struct vop_reclaim_args /* {
205 		struct vnode *a_vp;
206 	} */ *ap = v;
207 	struct vnode *vp = ap->a_vp;
208 	struct nfsnode *np = VTONFS(vp);
209 	struct nfsdmap *dp, *dp2;
210 
211 	if (prtactive && vp->v_usecount != 0)
212 		vprint("nfs_reclaim: pushing active", vp);
213 
214 	if (np->n_hash.le_prev != NULL)
215 		LIST_REMOVE(np, n_hash);
216 
217 	/*
218 	 * Free up any directory cookie structures and
219 	 * large file handle structures that might be associated with
220 	 * this nfs node.
221 	 */
222 	if (vp->v_type == VDIR) {
223 		dp = LIST_FIRST(&np->n_cookies);
224 		while (dp) {
225 			dp2 = dp;
226 			dp = dp->ndm_list.le_next;
227 			FREE((caddr_t)dp2, M_NFSDIROFF);
228 		}
229 	}
230 	if (np->n_fhsize > NFS_SMALLFH) {
231 		free(np->n_fhp, M_NFSBIGFH);
232 	}
233 
234 	if (np->n_rcred)
235 		crfree(np->n_rcred);
236 	if (np->n_wcred)
237 		crfree(np->n_wcred);
238 	cache_purge(vp);
239 	pool_put(&nfs_node_pool, vp->v_data);
240 	vp->v_data = NULL;
241 	return (0);
242 }
243 
244