xref: /netbsd-src/sys/nfs/nfs_node.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: nfs_node.c,v 1.46 2001/09/15 20:36:39 chs 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 "opt_nfs.h"
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/lock.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 LIST_HEAD(nfsnodehashhead, nfsnode) *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 extern int prtactive;
70 
71 #define TRUE	1
72 #define	FALSE	0
73 
74 void nfs_gop_size(struct vnode *, off_t, off_t *);
75 int nfs_gop_alloc(struct vnode *, off_t, off_t, int, struct ucred *);
76 
77 struct genfs_ops nfs_genfsops = {
78 	nfs_gop_size,
79 	nfs_gop_alloc,
80 	nfs_gop_write,
81 };
82 
83 /*
84  * Initialize hash links for nfsnodes
85  * and build nfsnode free list.
86  */
87 void
88 nfs_nhinit()
89 {
90 
91 	nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
92 	    M_WAITOK, &nfsnodehash);
93 	lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
94 
95 	pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
96 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
97 	pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
98 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
99 }
100 
101 /*
102  * Reinitialize inode hash table.
103  */
104 
105 void
106 nfs_nhreinit()
107 {
108 	struct nfsnode *np;
109 	struct nfsnodehashhead *oldhash, *hash;
110 	u_long oldmask, mask, val;
111 	int i;
112 
113 	hash = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE, M_WAITOK,
114 	    &mask);
115 
116 	lockmgr(&nfs_hashlock, LK_EXCLUSIVE, NULL);
117 	oldhash = nfsnodehashtbl;
118 	oldmask = nfsnodehash;
119 	nfsnodehashtbl = hash;
120 	nfsnodehash = mask;
121 	for (i = 0; i <= oldmask; i++) {
122 		while ((np = LIST_FIRST(&oldhash[i])) != NULL) {
123 			LIST_REMOVE(np, n_hash);
124 			val = NFSNOHASH(nfs_hash(np->n_fhp, np->n_fhsize));
125 			LIST_INSERT_HEAD(&hash[val], np, n_hash);
126 		}
127 	}
128 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
129 	hashdone(oldhash, M_NFSNODE);
130 }
131 
132 /*
133  * Free resources previoslu allocated in nfs_nhinit().
134  */
135 void
136 nfs_nhdone()
137 {
138 	hashdone(nfsnodehashtbl, M_NFSNODE);
139 	pool_destroy(&nfs_node_pool);
140 	pool_destroy(&nfs_vattr_pool);
141 }
142 
143 /*
144  * Compute an entry in the NFS hash table structure
145  */
146 u_long
147 nfs_hash(fhp, fhsize)
148 	nfsfh_t *fhp;
149 	int fhsize;
150 {
151 	u_char *fhpp;
152 	u_long fhsum;
153 	int i;
154 
155 	fhpp = &fhp->fh_bytes[0];
156 	fhsum = 0;
157 	for (i = 0; i < fhsize; i++)
158 		fhsum += *fhpp++;
159 	return (fhsum);
160 }
161 
162 /*
163  * Look up a vnode/nfsnode by file handle.
164  * Callers must check for mount points!!
165  * In all cases, a pointer to a
166  * nfsnode structure is returned.
167  */
168 int
169 nfs_nget(mntp, fhp, fhsize, npp)
170 	struct mount *mntp;
171 	nfsfh_t *fhp;
172 	int fhsize;
173 	struct nfsnode **npp;
174 {
175 	struct nfsnode *np;
176 	struct nfsnodehashhead *nhpp;
177 	struct vnode *vp;
178 	struct vnode *nvp;
179 	int error;
180 
181 	nhpp = &nfsnodehashtbl[NFSNOHASH(nfs_hash(fhp, fhsize))];
182 loop:
183 	LIST_FOREACH(np, nhpp, n_hash) {
184 		if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
185 		    memcmp(fhp, np->n_fhp, fhsize))
186 			continue;
187 		vp = NFSTOV(np);
188 		if (vget(vp, LK_EXCLUSIVE))
189 			goto loop;
190 		*npp = np;
191 		return(0);
192 	}
193 	if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
194 		goto loop;
195 	error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
196 	if (error) {
197 		*npp = 0;
198 		lockmgr(&nfs_hashlock, LK_RELEASE, 0);
199 		return (error);
200 	}
201 	vp = nvp;
202 	np = pool_get(&nfs_node_pool, PR_WAITOK);
203 	memset(np, 0, sizeof *np);
204 	lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
205 	vp->v_data = np;
206 	np->n_vnode = vp;
207 	genfs_node_init(vp, &nfs_genfsops);
208 
209 	/*
210 	 * Insert the nfsnode in the hash queue for its new file handle
211 	 */
212 
213 	LIST_INSERT_HEAD(nhpp, np, n_hash);
214 	if (fhsize > NFS_SMALLFH) {
215 		np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
216 	} else
217 		np->n_fhp = &np->n_fh;
218 	memcpy(np->n_fhp, fhp, fhsize);
219 	np->n_fhsize = fhsize;
220 	np->n_accstamp = -1;
221 	np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
222 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
223 	lockmgr(&nfs_hashlock, LK_RELEASE, NULL);
224 	error = VOP_GETATTR(vp, np->n_vattr, curproc->p_ucred, curproc);
225 	if (error) {
226 		vgone(vp);
227 		return error;
228 	}
229 	uvm_vnp_setsize(vp, np->n_vattr->va_size);
230 	*npp = np;
231 	return (0);
232 }
233 
234 int
235 nfs_inactive(v)
236 	void *v;
237 {
238 	struct vop_inactive_args /* {
239 		struct vnode *a_vp;
240 		struct proc *a_p;
241 	} */ *ap = v;
242 	struct nfsnode *np;
243 	struct sillyrename *sp;
244 	struct proc *p = ap->a_p;
245 	struct vnode *vp = ap->a_vp;
246 
247 	np = VTONFS(vp);
248 	if (prtactive && vp->v_usecount != 0)
249 		vprint("nfs_inactive: pushing active", vp);
250 	if (vp->v_type != VDIR) {
251 		sp = np->n_sillyrename;
252 		np->n_sillyrename = (struct sillyrename *)0;
253 	} else
254 		sp = NULL;
255 	if (sp != NULL)
256 		nfs_vinvalbuf(vp, 0, sp->s_cred, p, 1);
257 	np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
258 		NQNFSNONCACHE | NQNFSWRITE);
259 	VOP_UNLOCK(vp, 0);
260 	if (sp != NULL) {
261 
262 		/*
263 		 * Remove the silly file that was rename'd earlier
264 		 */
265 
266 		vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_RETRY);
267 		nfs_removeit(sp);
268 		crfree(sp->s_cred);
269 		vput(sp->s_dvp);
270 		FREE(sp, M_NFSREQ);
271 	}
272 	return (0);
273 }
274 
275 /*
276  * Reclaim an nfsnode so that it can be used for other purposes.
277  */
278 int
279 nfs_reclaim(v)
280 	void *v;
281 {
282 	struct vop_reclaim_args /* {
283 		struct vnode *a_vp;
284 	} */ *ap = v;
285 	struct vnode *vp = ap->a_vp;
286 	struct nfsnode *np = VTONFS(vp);
287 	struct nfsmount *nmp = VFSTONFS(vp->v_mount);
288 
289 	if (prtactive && vp->v_usecount != 0)
290 		vprint("nfs_reclaim: pushing active", vp);
291 
292 	LIST_REMOVE(np, n_hash);
293 
294 	/*
295 	 * For nqnfs, take it off the timer queue as required.
296 	 */
297 	if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
298 		CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
299 	}
300 
301 	/*
302 	 * Free up any directory cookie structures and
303 	 * large file handle structures that might be associated with
304 	 * this nfs node.
305 	 */
306 	if (vp->v_type == VDIR && np->n_dircache) {
307 		nfs_invaldircache(vp, 1);
308 		FREE(np->n_dircache, M_NFSDIROFF);
309 	}
310 	if (np->n_fhsize > NFS_SMALLFH) {
311 		free(np->n_fhp, M_NFSBIGFH);
312 	}
313 
314 	pool_put(&nfs_vattr_pool, np->n_vattr);
315 	if (np->n_rcred) {
316 		crfree(np->n_rcred);
317 	}
318 	if (np->n_wcred) {
319 		crfree(np->n_wcred);
320 	}
321 	cache_purge(vp);
322 	pool_put(&nfs_node_pool, vp->v_data);
323 	vp->v_data = NULL;
324 	return (0);
325 }
326 
327 void
328 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp)
329 {
330 	*eobp = MAX(size, vp->v_size);
331 }
332 
333 int
334 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
335     struct ucred *cred)
336 {
337 	return 0;
338 }
339