xref: /openbsd-src/sys/nfs/nfs_node.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: nfs_node.c,v 1.19 2001/12/19 08:58:06 art 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 
53 #include <nfs/rpcv2.h>
54 #include <nfs/nfsproto.h>
55 #include <nfs/nfs.h>
56 #include <nfs/nfsnode.h>
57 #include <nfs/nfsmount.h>
58 #include <nfs/nfs_var.h>
59 
60 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61 u_long nfsnodehash;
62 struct lock nfs_hashlock;
63 
64 struct pool nfs_node_pool;
65 
66 extern int prtactive;
67 
68 #define TRUE	1
69 #define	FALSE	0
70 
71 /*
72  * Initialize hash links for nfsnodes
73  * and build nfsnode free list.
74  */
75 void
76 nfs_nhinit()
77 {
78 	nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, M_WAITOK, &nfsnodehash);
79 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
80 
81 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
82 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
83 }
84 
85 /*
86  * Compute an entry in the NFS hash table structure
87  */
88 u_long
89 nfs_hash(fhp, fhsize)
90 	nfsfh_t *fhp;
91 	int fhsize;
92 {
93 	u_char *fhpp;
94 	u_long fhsum;
95 	int i;
96 
97 	fhpp = &fhp->fh_bytes[0];
98 	fhsum = 0;
99 	for (i = 0; i < fhsize; i++)
100 		fhsum += *fhpp++;
101 	return (fhsum);
102 }
103 
104 /*
105  * Look up a vnode/nfsnode by file handle.
106  * Callers must check for mount points!!
107  * In all cases, a pointer to a
108  * nfsnode structure is returned.
109  */
110 int
111 nfs_nget(mntp, fhp, fhsize, npp)
112 	struct mount *mntp;
113 	nfsfh_t *fhp;
114 	int fhsize;
115 	struct nfsnode **npp;
116 {
117 	struct proc *p = curproc;	/* XXX */
118 	struct nfsnode *np;
119 	struct nfsnodehashhead *nhpp;
120 	struct vnode *vp;
121 	extern int (**nfsv2_vnodeop_p)__P((void *));
122 	struct vnode *nvp;
123 	int error;
124 
125 	nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
126 loop:
127 	for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
128 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
129 		    bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
130 			continue;
131 		vp = NFSTOV(np);
132 		if (vget(vp, LK_EXCLUSIVE, p))
133 			goto loop;
134 		*npp = np;
135 		return(0);
136 	}
137 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, NULL, p))
138 		goto loop;
139 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
140 	if (error) {
141 		*npp = 0;
142 		lockmgr(&nfs_hashlock, LK_RELEASE, NULL, p);
143 		return (error);
144 	}
145 	vp = nvp;
146 	np = pool_get(&nfs_node_pool, PR_WAITOK);
147 	bzero((caddr_t)np, sizeof *np);
148 	vp->v_data = np;
149 	np->n_vnode = vp;
150 
151 	/*
152 	 * Are we getting the root? If so, make sure the vnode flags
153 	 * are correct
154 	 */
155 	{
156 		struct nfsmount *nmp = VFSTONFS(mntp);
157 		if ((fhsize == nmp->nm_fhsize) &&
158 		    !bcmp(fhp, nmp->nm_fh, fhsize)) {
159 			if (vp->v_type == VNON)
160 				vp->v_type = VDIR;
161 			vp->v_flag |= VROOT;
162 		}
163 	}
164 
165 	LIST_INSERT_HEAD(nhpp, np, n_hash);
166 	if (fhsize > NFS_SMALLFH) {
167 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
168 	} else
169 		np->n_fhp = &np->n_fh;
170 	bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
171 	np->n_fhsize = fhsize;
172 	lockmgr(&nfs_hashlock, LK_RELEASE, 0, p);
173 	*npp = np;
174 	return (0);
175 }
176 
177 int
178 nfs_inactive(v)
179 	void *v;
180 {
181 	struct vop_inactive_args /* {
182 		struct vnode *a_vp;
183 		struct proc *a_p;
184 	} */ *ap = v;
185 	struct nfsnode *np;
186 	struct sillyrename *sp;
187 	struct proc *p = curproc;	/* XXX */
188 
189 	np = VTONFS(ap->a_vp);
190 	if (prtactive && ap->a_vp->v_usecount != 0)
191 		vprint("nfs_inactive: pushing active", ap->a_vp);
192 	if (ap->a_vp->v_type != VDIR) {
193 		sp = np->n_sillyrename;
194 		np->n_sillyrename = (struct sillyrename *)0;
195 	} else
196 		sp = (struct sillyrename *)0;
197 	if (sp) {
198 		/*
199 		 * Remove the silly file that was rename'd earlier
200 		 */
201 		(void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
202 		nfs_removeit(sp);
203 		crfree(sp->s_cred);
204 		vrele(sp->s_dvp);
205 		FREE((caddr_t)sp, M_NFSREQ);
206 	}
207 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT);
208 
209 	VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
210 	return (0);
211 }
212 
213 /*
214  * Reclaim an nfsnode so that it can be used for other purposes.
215  */
216 int
217 nfs_reclaim(v)
218 	void *v;
219 {
220 	struct vop_reclaim_args /* {
221 		struct vnode *a_vp;
222 	} */ *ap = v;
223 	struct vnode *vp = ap->a_vp;
224 	struct nfsnode *np = VTONFS(vp);
225 	struct nfsdmap *dp, *dp2;
226 
227 	if (prtactive && vp->v_usecount != 0)
228 		vprint("nfs_reclaim: pushing active", vp);
229 
230 	if (np->n_hash.le_prev != NULL)
231 		LIST_REMOVE(np, n_hash);
232 
233 	/*
234 	 * Free up any directory cookie structures and
235 	 * large file handle structures that might be associated with
236 	 * this nfs node.
237 	 */
238 	if (vp->v_type == VDIR) {
239 		dp = np->n_cookies.lh_first;
240 		while (dp) {
241 			dp2 = dp;
242 			dp = dp->ndm_list.le_next;
243 			FREE((caddr_t)dp2, M_NFSDIROFF);
244 		}
245 	}
246 	if (np->n_fhsize > NFS_SMALLFH) {
247 		free(np->n_fhp, M_NFSBIGFH);
248 	}
249 
250 	if (np->n_rcred)
251 		crfree(np->n_rcred);
252 	if (np->n_wcred)
253 		crfree(np->n_wcred);
254 	cache_purge(vp);
255 	pool_put(&nfs_node_pool, vp->v_data);
256 	vp->v_data = NULL;
257 	return (0);
258 }
259 
260