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