nfs_node.c revision 1.101.6.3 1 /* $NetBSD: nfs_node.c,v 1.101.6.3 2009/01/17 13:29:34 mjf 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.101.6.3 2009/01/17 13:29:34 mjf Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_nfs.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
49 #include <sys/vnode.h>
50 #include <sys/kernel.h>
51 #include <sys/pool.h>
52 #include <sys/lock.h>
53 #include <sys/hash.h>
54 #include <sys/kauth.h>
55
56 #include <nfs/rpcv2.h>
57 #include <nfs/nfsproto.h>
58 #include <nfs/nfs.h>
59 #include <nfs/nfsnode.h>
60 #include <nfs/nfsmount.h>
61 #include <nfs/nfs_var.h>
62
63 struct pool nfs_node_pool;
64 struct pool nfs_vattr_pool;
65 static struct workqueue *nfs_sillyworkq;
66
67 extern int prtactive;
68
69 static void nfs_gop_size(struct vnode *, off_t, off_t *, int);
70 static int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
71 static int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
72 static void nfs_sillyworker(struct work *, void *);
73
74 static const struct genfs_ops nfs_genfsops = {
75 .gop_size = nfs_gop_size,
76 .gop_alloc = nfs_gop_alloc,
77 .gop_write = nfs_gop_write,
78 };
79
80 /*
81 * Reinitialize inode hash table.
82 */
83 void
84 nfs_node_init()
85 {
86
87 pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
88 &pool_allocator_nointr, IPL_NONE);
89 pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
90 &pool_allocator_nointr, IPL_NONE);
91 if (workqueue_create(&nfs_sillyworkq, "nfssilly", nfs_sillyworker,
92 NULL, PRI_NONE, IPL_NONE, 0) != 0) {
93 panic("nfs_node_init");
94 }
95 }
96
97 /*
98 * Free resources previously allocated in nfs_node_reinit().
99 */
100 void
101 nfs_node_done()
102 {
103
104 pool_destroy(&nfs_node_pool);
105 pool_destroy(&nfs_vattr_pool);
106 workqueue_destroy(nfs_sillyworkq);
107 }
108
109 #define RBTONFSNODE(node) \
110 (void *)((uintptr_t)(node) - offsetof(struct nfsnode, n_rbnode))
111
112 struct fh_match {
113 nfsfh_t *fhm_fhp;
114 size_t fhm_fhsize;
115 size_t fhm_fhoffset;
116 };
117
118 static int
119 nfs_compare_nodes(const struct rb_node *parent, const struct rb_node *node)
120 {
121 const struct nfsnode * const pnp = RBTONFSNODE(parent);
122 const struct nfsnode * const np = RBTONFSNODE(node);
123
124 if (pnp->n_fhsize != np->n_fhsize)
125 return np->n_fhsize - pnp->n_fhsize;
126
127 return memcmp(np->n_fhp, pnp->n_fhp, np->n_fhsize);
128 }
129
130 static int
131 nfs_compare_node_fh(const struct rb_node *b, const void *key)
132 {
133 const struct nfsnode * const pnp = RBTONFSNODE(b);
134 const struct fh_match * const fhm = key;
135
136 if (pnp->n_fhsize != fhm->fhm_fhsize)
137 return fhm->fhm_fhsize - pnp->n_fhsize;
138
139 return memcmp(fhm->fhm_fhp, pnp->n_fhp, pnp->n_fhsize);
140 }
141
142 static const struct rb_tree_ops nfs_node_rbtree_ops = {
143 .rbto_compare_nodes = nfs_compare_nodes,
144 .rbto_compare_key = nfs_compare_node_fh,
145 };
146
147 void
148 nfs_rbtinit(struct nfsmount *nmp)
149 {
150 rb_tree_init(&nmp->nm_rbtree, &nfs_node_rbtree_ops);
151 }
152
153
154 /*
155 * Look up a vnode/nfsnode by file handle.
156 * Callers must check for mount points!!
157 * In all cases, a pointer to a
158 * nfsnode structure is returned.
159 */
160 int
161 nfs_nget1(mntp, fhp, fhsize, npp, lkflags)
162 struct mount *mntp;
163 nfsfh_t *fhp;
164 int fhsize;
165 struct nfsnode **npp;
166 int lkflags;
167 {
168 struct nfsnode *np;
169 struct vnode *vp;
170 struct nfsmount *nmp = VFSTONFS(mntp);
171 int error;
172 struct fh_match fhm;
173 struct rb_node *node;
174
175 fhm.fhm_fhp = fhp;
176 fhm.fhm_fhsize = fhsize;
177
178 loop:
179 rw_enter(&nmp->nm_rbtlock, RW_READER);
180 node = rb_tree_find_node(&nmp->nm_rbtree, &fhm);
181 if (node != NULL) {
182 np = RBTONFSNODE(node);
183 vp = NFSTOV(np);
184 mutex_enter(&vp->v_interlock);
185 rw_exit(&nmp->nm_rbtlock);
186 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | lkflags);
187 if (error == EBUSY)
188 return error;
189 if (error)
190 goto loop;
191 *npp = np;
192 return(0);
193 }
194 rw_exit(&nmp->nm_rbtlock);
195
196 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &vp);
197 if (error) {
198 *npp = 0;
199 return (error);
200 }
201 np = pool_get(&nfs_node_pool, PR_WAITOK);
202 memset(np, 0, sizeof *np);
203 np->n_vnode = vp;
204
205 /*
206 * Insert the nfsnode in the hash queue for its new file handle
207 */
208
209 if (fhsize > NFS_SMALLFH) {
210 np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
211 } else
212 np->n_fhp = &np->n_fh;
213 memcpy(np->n_fhp, fhp, fhsize);
214 np->n_fhsize = fhsize;
215 np->n_accstamp = -1;
216 np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
217
218 rw_enter(&nmp->nm_rbtlock, RW_WRITER);
219 if (NULL != rb_tree_find_node(&nmp->nm_rbtree, &fhm)) {
220 rw_exit(&nmp->nm_rbtlock);
221 if (fhsize > NFS_SMALLFH) {
222 kmem_free(np->n_fhp, fhsize);
223 }
224 pool_put(&nfs_vattr_pool, np->n_vattr);
225 pool_put(&nfs_node_pool, np);
226 ungetnewvnode(vp);
227 goto loop;
228 }
229 vp->v_data = np;
230 genfs_node_init(vp, &nfs_genfsops);
231 /*
232 * Initalize read/write creds to useful values. VOP_OPEN will
233 * overwrite these.
234 */
235 np->n_rcred = curlwp->l_cred;
236 kauth_cred_hold(np->n_rcred);
237 np->n_wcred = curlwp->l_cred;
238 kauth_cred_hold(np->n_wcred);
239 vlockmgr(&vp->v_lock, LK_EXCLUSIVE);
240 NFS_INVALIDATE_ATTRCACHE(np);
241 uvm_vnp_setsize(vp, 0);
242 rb_tree_insert_node(&nmp->nm_rbtree, &np->n_rbnode);
243 rw_exit(&nmp->nm_rbtlock);
244
245 *npp = np;
246 return (0);
247 }
248
249 int
250 nfs_inactive(v)
251 void *v;
252 {
253 struct vop_inactive_args /* {
254 struct vnode *a_vp;
255 bool *a_recycle;
256 } */ *ap = v;
257 struct nfsnode *np;
258 struct sillyrename *sp;
259 struct vnode *vp = ap->a_vp;
260
261 np = VTONFS(vp);
262 if (vp->v_type != VDIR) {
263 sp = np->n_sillyrename;
264 np->n_sillyrename = (struct sillyrename *)0;
265 } else
266 sp = NULL;
267 if (sp != NULL)
268 nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
269 *ap->a_recycle = (np->n_flag & NREMOVED) != 0;
270 np->n_flag &=
271 (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
272
273 if (vp->v_type == VDIR && np->n_dircache)
274 nfs_invaldircache(vp,
275 NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
276
277 VOP_UNLOCK(vp, 0);
278
279 if (sp != NULL) {
280 workqueue_enqueue(nfs_sillyworkq, &sp->s_work, NULL);
281 }
282
283 return (0);
284 }
285
286 /*
287 * Reclaim an nfsnode so that it can be used for other purposes.
288 */
289 int
290 nfs_reclaim(v)
291 void *v;
292 {
293 struct vop_reclaim_args /* {
294 struct vnode *a_vp;
295 } */ *ap = v;
296 struct vnode *vp = ap->a_vp;
297 struct nfsnode *np = VTONFS(vp);
298 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
299
300 if (prtactive && vp->v_usecount > 1)
301 vprint("nfs_reclaim: pushing active", vp);
302
303 rw_enter(&nmp->nm_rbtlock, RW_WRITER);
304 rb_tree_remove_node(&nmp->nm_rbtree, &np->n_rbnode);
305 rw_exit(&nmp->nm_rbtlock);
306
307 /*
308 * Free up any directory cookie structures and
309 * large file handle structures that might be associated with
310 * this nfs node.
311 */
312 if (vp->v_type == VDIR && np->n_dircache != NULL) {
313 nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
314 hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
315 }
316 KASSERT(np->n_dirgens == NULL);
317
318 if (np->n_fhsize > NFS_SMALLFH)
319 kmem_free(np->n_fhp, np->n_fhsize);
320
321 pool_put(&nfs_vattr_pool, np->n_vattr);
322 if (np->n_rcred)
323 kauth_cred_free(np->n_rcred);
324
325 if (np->n_wcred)
326 kauth_cred_free(np->n_wcred);
327
328 cache_purge(vp);
329 if (vp->v_type == VREG) {
330 mutex_destroy(&np->n_commitlock);
331 }
332 genfs_node_destroy(vp);
333 pool_put(&nfs_node_pool, np);
334 vp->v_data = NULL;
335 return (0);
336 }
337
338 void
339 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
340 {
341
342 *eobp = MAX(size, vp->v_size);
343 }
344
345 int
346 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
347 kauth_cred_t cred)
348 {
349
350 return 0;
351 }
352
353 int
354 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
355 {
356 int i;
357
358 for (i = 0; i < npages; i++) {
359 pmap_page_protect(pgs[i], VM_PROT_READ);
360 }
361 return genfs_gop_write(vp, pgs, npages, flags);
362 }
363
364 /*
365 * Remove a silly file that was rename'd earlier
366 */
367 static void
368 nfs_sillyworker(struct work *work, void *arg)
369 {
370 struct sillyrename *sp;
371 int error;
372
373 sp = (struct sillyrename *)work;
374 error = vn_lock(sp->s_dvp, LK_EXCLUSIVE);
375 if (error || sp->s_dvp->v_data == NULL) {
376 /* XXX should recover */
377 printf("%s: vp=%p error=%d\n", __func__, sp->s_dvp, error);
378 if (error == 0) {
379 vput(sp->s_dvp);
380 } else {
381 vrele(sp->s_dvp);
382 }
383 } else {
384 nfs_removeit(sp);
385 vput(sp->s_dvp);
386 }
387 kauth_cred_free(sp->s_cred);
388 kmem_free(sp, sizeof(*sp));
389 }
390