xref: /netbsd-src/sys/nfs/nfs_node.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: nfs_node.c,v 1.50 2002/01/21 13:48:51 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.50 2002/01/21 13:48:51 fvdl Exp $");
43 
44 #include "opt_nfs.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/pool.h>
55 #include <sys/lock.h>
56 #include <sys/hash.h>
57 
58 #include <nfs/rpcv2.h>
59 #include <nfs/nfsproto.h>
60 #include <nfs/nfs.h>
61 #include <nfs/nfsnode.h>
62 #include <nfs/nfsmount.h>
63 #include <nfs/nqnfs.h>
64 #include <nfs/nfs_var.h>
65 
66 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
67 u_long nfsnodehash;
68 struct lock nfs_hashlock;
69 
70 struct pool nfs_node_pool;		/* memory pool for nfs nodes */
71 struct pool nfs_vattr_pool;		/* memory pool for nfs vattrs */
72 
73 extern int prtactive;
74 
75 #define TRUE	1
76 #define	FALSE	0
77 
78 #define	nfs_hash(x,y)	hash32_buf((x), (y), HASH32_BUF_INIT)
79 
80 void nfs_gop_size(struct vnode *, off_t, off_t *);
81 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *);
82 
83 struct genfs_ops nfs_genfsops = {
84 	nfs_gop_size,
85 	nfs_gop_alloc,
86 	nfs_gop_write,
87 };
88 
89 /*
90  * Initialize hash links for nfsnodes
91  * and build nfsnode free list.
92  */
93 void
94 nfs_nhinit()
95 {
96 
97 	nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
98 	    M_WAITOK, &nfsnodehash);
99 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
100 
101 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
102 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
103 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
104 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
105 }
106 
107 /*
108  * Reinitialize inode hash table.
109  */
110 
111 void
112 nfs_nhreinit()
113 {
114 	struct nfsnode *np;
115 	struct nfsnodehashhead *oldhash, *hash;
116 	u_long oldmask, mask, val;
117 	int i;
118 
119 	hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK,
120 	    &mask);
121 
122 	lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL);
123 	oldhash = nfsnodehashtbl;
124 	oldmask = nfsnodehash;
125 	nfsnodehashtbl = hash;
126 	nfsnodehash = mask;
127 	for (i = 0; i <= oldmask; i++) {
128 		while ((np = LIST_FIRST(&oldhash[i])) != NULL) {
129 			LIST_REMOVE(np, n_hash);
130 			val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize));
131 			LIST_INSERT_HEAD(&hash[val], np, n_hash);
132 		}
133 	}
134 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
135 	hashdone(oldhash, M_NFSNODE);
136 }
137 
138 /*
139  * Free resources previoslu allocated in nfs_nhinit().
140  */
141 void
142 nfs_nhdone()
143 {
144 	hashdone(nfsnodehashtbl, M_NFSNODE);
145 	pool_destroy(&nfs_node_pool);
146 	pool_destroy(&nfs_vattr_pool);
147 }
148 
149 /*
150  * Look up a vnode/nfsnode by file handle.
151  * Callers must check for mount points!!
152  * In all cases, a pointer to a
153  * nfsnode structure is returned.
154  */
155 int
156 nfs_nget(mntp, fhp, fhsize, npp)
157 	struct mount *mntp;
158 	nfsfh_t *fhp;
159 	int fhsize;
160 	struct nfsnode **npp;
161 {
162 	struct nfsnode *np;
163 	struct nfsnodehashhead *nhpp;
164 	struct vnode *vp;
165 	struct vnode *nvp;
166 	int error;
167 
168 	nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))];
169 loop:
170 	LIST_FOREACH(np, nhpp, n_hash) {
171 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
172 		    memcmp(fhp, np->n_fhp, fhsize))
173 			continue;
174 		vp = NFSTOV(np);
175 		if (vget(vp, LK_EXCLUSIVE))
176 			goto loop;
177 		*npp = np;
178 		return(0);
179 	}
180 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
181 		goto loop;
182 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
183 	if (error) {
184 		*npp = 0;
185 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
186 		return (error);
187 	}
188 	vp = nvp;
189 	np = pool_get(&nfs_node_pool, PR_WAITOK);
190 	memset(np, 0, sizeof *np);
191 	lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
192 	vp->v_data = np;
193 	np->n_vnode = vp;
194 	genfs_node_init(vp, &nfs_genfsops);
195 
196 	/*
197 	 * Insert the nfsnode in the hash queue for its new file handle
198 	 */
199 
200 	LIST_INSERT_HEAD(nhpp, np, n_hash);
201 	if (fhsize > NFS_SMALLFH) {
202 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
203 	} else
204 		np->n_fhp = &np->n_fh;
205 	memcpy(np->n_fhp, fhp, fhsize);
206 	np->n_fhsize = fhsize;
207 	np->n_accstamp = -1;
208 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
209 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
210 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
211 	error = VOP_GETATTR(vp, np->n_vattr, curproc->p_ucred, curproc);
212 	if (error) {
213 		vput(vp);
214 		return error;
215 	}
216 	uvm_vnp_setsize(vp, np->n_vattr->va_size);
217 	*npp = np;
218 	return (0);
219 }
220 
221 int
222 nfs_inactive(v)
223 	void *v;
224 {
225 	struct vop_inactive_args /* {
226 		struct vnode *a_vp;
227 		struct proc *a_p;
228 	} */ *ap = v;
229 	struct nfsnode *np;
230 	struct sillyrename *sp;
231 	struct proc *p = ap->a_p;
232 	struct vnode *vp = ap->a_vp;
233 
234 	np = VTONFS(vp);
235 	if (prtactive && vp->v_usecount != 0)
236 		vprint("nfs_inactive: pushing active", vp);
237 	if (vp->v_type != VDIR) {
238 		sp = np->n_sillyrename;
239 		np->n_sillyrename = (struct sillyrename *)0;
240 	} else
241 		sp = NULL;
242 	if (sp != NULL)
243 		nfs_vinvalbuf(vp, 0, sp->s_cred, p, 1);
244 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
245 		NQNFSNONCACHE | NQNFSWRITE);
246 	VOP_UNLOCK(vp, 0);
247 	if (sp != NULL) {
248 
249 		/*
250 		 * Remove the silly file that was rename'd earlier
251 		 */
252 
253 		vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_RETRY);
254 		nfs_removeit(sp);
255 		crfree(sp->s_cred);
256 		vput(sp->s_dvp);
257 		FREE(sp, M_NFSREQ);
258 	}
259 	return (0);
260 }
261 
262 /*
263  * Reclaim an nfsnode so that it can be used for other purposes.
264  */
265 int
266 nfs_reclaim(v)
267 	void *v;
268 {
269 	struct vop_reclaim_args /* {
270 		struct vnode *a_vp;
271 	} */ *ap = v;
272 	struct vnode *vp = ap->a_vp;
273 	struct nfsnode *np = VTONFS(vp);
274 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
275 
276 	if (prtactive && vp->v_usecount != 0)
277 		vprint("nfs_reclaim: pushing active", vp);
278 
279 	LIST_REMOVE(np, n_hash);
280 
281 	/*
282 	 * For nqnfs, take it off the timer queue as required.
283 	 */
284 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
285 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
286 	}
287 
288 	/*
289 	 * Free up any directory cookie structures and
290 	 * large file handle structures that might be associated with
291 	 * this nfs node.
292 	 */
293 	if (vp->v_type == VDIR && np->n_dircache) {
294 		nfs_invaldircache(vp, 1);
295 		FREE(np->n_dircache, M_NFSDIROFF);
296 	}
297 	if (np->n_fhsize > NFS_SMALLFH) {
298 		free(np->n_fhp, M_NFSBIGFH);
299 	}
300 
301 	pool_put(&nfs_vattr_pool, np->n_vattr);
302 	if (np->n_rcred) {
303 		crfree(np->n_rcred);
304 	}
305 	if (np->n_wcred) {
306 		crfree(np->n_wcred);
307 	}
308 	cache_purge(vp);
309 	pool_put(&nfs_node_pool, vp->v_data);
310 	vp->v_data = NULL;
311 	return (0);
312 }
313 
314 void
315 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp)
316 {
317 	*eobp = MAX(size, vp->v_size);
318 }
319 
320 int
321 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
322     struct ucred *cred)
323 {
324 	return 0;
325 }
326