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