xref: /netbsd-src/sys/nfs/nfs_node.c (revision 73704c4ce4ee2a60eb617e693ce7e9f03902613e)
1 /*	$NetBSD: nfs_node.c,v 1.70 2003/08/07 16:33:50 agc 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)nfs_node.c	8.6 (Berkeley) 5/22/95
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.70 2003/08/07 16:33:50 agc Exp $");
39 
40 #include "opt_nfs.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/pool.h>
51 #include <sys/lock.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/nqnfs.h>
60 #include <nfs/nfs_var.h>
61 
62 struct nfsnodehashhead *nfsnodehashtbl;
63 u_long nfsnodehash;
64 struct lock nfs_hashlock;
65 
66 struct pool nfs_node_pool;		/* memory pool for nfs nodes */
67 struct pool nfs_vattr_pool;		/* memory pool for nfs vattrs */
68 
69 MALLOC_DEFINE(M_NFSBIGFH, "NFS bigfh", "NFS big filehandle");
70 MALLOC_DEFINE(M_NFSNODE, "NFS node", "NFS vnode private part");
71 
72 extern int prtactive;
73 
74 #define	nfs_hash(x,y)	hash32_buf((x), (y), HASH32_BUF_INIT)
75 
76 void nfs_gop_size(struct vnode *, off_t, off_t *, int);
77 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *);
78 int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
79 
80 struct genfs_ops nfs_genfsops = {
81 	nfs_gop_size,
82 	nfs_gop_alloc,
83 	nfs_gop_write,
84 };
85 
86 /*
87  * Initialize hash links for nfsnodes
88  * and build nfsnode free list.
89  */
90 void
91 nfs_nhinit()
92 {
93 
94 	nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
95 	    M_WAITOK, &nfsnodehash);
96 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
97 
98 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
99 	    &pool_allocator_nointr);
100 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
101 	    &pool_allocator_nointr);
102 }
103 
104 /*
105  * Reinitialize inode hash table.
106  */
107 
108 void
109 nfs_nhreinit()
110 {
111 	struct nfsnode *np;
112 	struct nfsnodehashhead *oldhash, *hash;
113 	u_long oldmask, mask, val;
114 	int i;
115 
116 	hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK,
117 	    &mask);
118 
119 	lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL);
120 	oldhash = nfsnodehashtbl;
121 	oldmask = nfsnodehash;
122 	nfsnodehashtbl = hash;
123 	nfsnodehash = mask;
124 	for (i = 0; i <= oldmask; i++) {
125 		while ((np = LIST_FIRST(&oldhash[i])) != NULL) {
126 			LIST_REMOVE(np, n_hash);
127 			val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize));
128 			LIST_INSERT_HEAD(&hash[val], np, n_hash);
129 		}
130 	}
131 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
132 	hashdone(oldhash, M_NFSNODE);
133 }
134 
135 /*
136  * Free resources previoslu allocated in nfs_nhinit().
137  */
138 void
139 nfs_nhdone()
140 {
141 	hashdone(nfsnodehashtbl, M_NFSNODE);
142 	pool_destroy(&nfs_node_pool);
143 	pool_destroy(&nfs_vattr_pool);
144 }
145 
146 /*
147  * Look up a vnode/nfsnode by file handle.
148  * Callers must check for mount points!!
149  * In all cases, a pointer to a
150  * nfsnode structure is returned.
151  */
152 int
153 nfs_nget(mntp, fhp, fhsize, npp)
154 	struct mount *mntp;
155 	nfsfh_t *fhp;
156 	int fhsize;
157 	struct nfsnode **npp;
158 {
159 	struct nfsnode *np;
160 	struct nfsnodehashhead *nhpp;
161 	struct vnode *vp;
162 	struct vnode *nvp;
163 	int error;
164 
165 	nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))];
166 loop:
167 	LIST_FOREACH(np, nhpp, n_hash) {
168 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
169 		    memcmp(fhp, np->n_fhp, fhsize))
170 			continue;
171 		vp = NFSTOV(np);
172 		if (vget(vp, LK_EXCLUSIVE))
173 			goto loop;
174 		*npp = np;
175 		return(0);
176 	}
177 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
178 		goto loop;
179 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
180 	if (error) {
181 		*npp = 0;
182 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
183 		return (error);
184 	}
185 	vp = nvp;
186 	np = pool_get(&nfs_node_pool, PR_WAITOK);
187 	memset(np, 0, sizeof *np);
188 	lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
189 	vp->v_data = np;
190 	np->n_vnode = vp;
191 	genfs_node_init(vp, &nfs_genfsops);
192 
193 	/*
194 	 * Insert the nfsnode in the hash queue for its new file handle
195 	 */
196 
197 	LIST_INSERT_HEAD(nhpp, np, n_hash);
198 	if (fhsize > NFS_SMALLFH) {
199 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
200 	} else
201 		np->n_fhp = &np->n_fh;
202 	memcpy(np->n_fhp, fhp, fhsize);
203 	np->n_fhsize = fhsize;
204 	np->n_accstamp = -1;
205 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
206 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
207 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
208 	error = VOP_GETATTR(vp, np->n_vattr, curproc->p_ucred, curproc);
209 	if (error) {
210 		vput(vp);
211 		return error;
212 	}
213 	uvm_vnp_setsize(vp, np->n_vattr->va_size);
214 	*npp = np;
215 	return (0);
216 }
217 
218 int
219 nfs_inactive(v)
220 	void *v;
221 {
222 	struct vop_inactive_args /* {
223 		struct vnode *a_vp;
224 		struct proc *a_p;
225 	} */ *ap = v;
226 	struct nfsnode *np;
227 	struct sillyrename *sp;
228 	struct proc *p = ap->a_p;
229 	struct vnode *vp = ap->a_vp;
230 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
231 	boolean_t removed;
232 
233 	np = VTONFS(vp);
234 	if (prtactive && vp->v_usecount != 0)
235 		vprint("nfs_inactive: pushing active", vp);
236 	if (vp->v_type != VDIR) {
237 		sp = np->n_sillyrename;
238 		np->n_sillyrename = (struct sillyrename *)0;
239 	} else
240 		sp = NULL;
241 	if (sp != NULL)
242 		nfs_vinvalbuf(vp, 0, sp->s_cred, p, 1);
243 	removed = (np->n_flag & NREMOVED) != 0;
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 
260 	if ((nmp->nm_flag & NFSMNT_NQNFS) && CIRCLEQ_NEXT(np, n_timer) != 0) {
261 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
262 	}
263 
264 	if (vp->v_type == VDIR && np->n_dircache)
265 		nfs_invaldircache(vp, 1);
266 
267 	if (removed)
268 		vrecycle(vp, NULL, p);
269 
270 	return (0);
271 }
272 
273 /*
274  * Reclaim an nfsnode so that it can be used for other purposes.
275  */
276 int
277 nfs_reclaim(v)
278 	void *v;
279 {
280 	struct vop_reclaim_args /* {
281 		struct vnode *a_vp;
282 	} */ *ap = v;
283 	struct vnode *vp = ap->a_vp;
284 	struct nfsnode *np = VTONFS(vp);
285 
286 	if (prtactive && vp->v_usecount != 0)
287 		vprint("nfs_reclaim: pushing active", vp);
288 
289 	LIST_REMOVE(np, n_hash);
290 
291 	/*
292 	 * Free up any directory cookie structures and
293 	 * large file handle structures that might be associated with
294 	 * this nfs node.
295 	 */
296 	if (vp->v_type == VDIR && np->n_dircache)
297 		hashdone(np->n_dircache, M_NFSDIROFF);
298 	KASSERT(np->n_dirgens == NULL);
299 
300 	if (np->n_fhsize > NFS_SMALLFH)
301 		free(np->n_fhp, M_NFSBIGFH);
302 
303 	pool_put(&nfs_vattr_pool, np->n_vattr);
304 	if (np->n_rcred)
305 		crfree(np->n_rcred);
306 
307 	if (np->n_wcred)
308 		crfree(np->n_wcred);
309 
310 	cache_purge(vp);
311 	pool_put(&nfs_node_pool, vp->v_data);
312 	vp->v_data = NULL;
313 	return (0);
314 }
315 
316 void
317 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
318 {
319 	KASSERT(flags & (GOP_SIZE_READ | GOP_SIZE_WRITE));
320 	KASSERT((flags & (GOP_SIZE_READ | GOP_SIZE_WRITE))
321 		!= (GOP_SIZE_READ | GOP_SIZE_WRITE));
322 	*eobp = MAX(size, vp->v_size);
323 }
324 
325 int
326 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
327     struct ucred *cred)
328 {
329 	return 0;
330 }
331 
332 int
333 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
334 {
335 	int i;
336 
337 	for (i = 0; i < npages; i++) {
338 		pmap_page_protect(pgs[i], VM_PROT_READ);
339 	}
340 	return genfs_gop_write(vp, pgs, npages, flags);
341 }
342