nfs_node.c revision 1.116 1 /* $NetBSD: nfs_node.c,v 1.116 2011/06/12 03:35:59 rmind 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.116 2011/06/12 03:35:59 rmind 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(void)
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(void)
102 {
103
104 pool_destroy(&nfs_node_pool);
105 pool_destroy(&nfs_vattr_pool);
106 workqueue_destroy(nfs_sillyworkq);
107 }
108
109 struct fh_match {
110 nfsfh_t *fhm_fhp;
111 size_t fhm_fhsize;
112 size_t fhm_fhoffset;
113 };
114
115 static int
116 nfs_compare_nodes(void *ctx, const void *parent, const void *node)
117 {
118 const struct nfsnode * const pnp = parent;
119 const struct nfsnode * const np = node;
120
121 if (pnp->n_fhsize != np->n_fhsize)
122 return np->n_fhsize - pnp->n_fhsize;
123
124 return memcmp(np->n_fhp, pnp->n_fhp, np->n_fhsize);
125 }
126
127 static int
128 nfs_compare_node_fh(void *ctx, const void *b, const void *key)
129 {
130 const struct nfsnode * const pnp = b;
131 const struct fh_match * const fhm = key;
132
133 if (pnp->n_fhsize != fhm->fhm_fhsize)
134 return fhm->fhm_fhsize - pnp->n_fhsize;
135
136 return memcmp(fhm->fhm_fhp, pnp->n_fhp, pnp->n_fhsize);
137 }
138
139 static const rb_tree_ops_t nfs_node_rbtree_ops = {
140 .rbto_compare_nodes = nfs_compare_nodes,
141 .rbto_compare_key = nfs_compare_node_fh,
142 .rbto_node_offset = offsetof(struct nfsnode, n_rbnode),
143 .rbto_context = NULL
144 };
145
146 void
147 nfs_rbtinit(struct nfsmount *nmp)
148 {
149
150 rb_tree_init(&nmp->nm_rbtree, &nfs_node_rbtree_ops);
151 }
152
153 /*
154 * Look up a vnode/nfsnode by file handle.
155 * Callers must check for mount points!!
156 * In all cases, a pointer to a
157 * nfsnode structure is returned.
158 */
159 int
160 nfs_nget1(struct mount *mntp, nfsfh_t *fhp, int fhsize, struct nfsnode **npp,
161 int lkflags)
162 {
163 struct nfsnode *np;
164 struct vnode *vp;
165 struct nfsmount *nmp = VFSTONFS(mntp);
166 int error;
167 struct fh_match fhm;
168
169 fhm.fhm_fhp = fhp;
170 fhm.fhm_fhsize = fhsize;
171
172 loop:
173 rw_enter(&nmp->nm_rbtlock, RW_READER);
174 np = rb_tree_find_node(&nmp->nm_rbtree, &fhm);
175 if (np != NULL) {
176 vp = NFSTOV(np);
177 mutex_enter(vp->v_interlock);
178 rw_exit(&nmp->nm_rbtlock);
179 error = vget(vp, LK_EXCLUSIVE | lkflags);
180 if (error == EBUSY)
181 return error;
182 if (error)
183 goto loop;
184 *npp = np;
185 return(0);
186 }
187 rw_exit(&nmp->nm_rbtlock);
188
189 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, NULL, &vp);
190 if (error) {
191 *npp = 0;
192 return (error);
193 }
194 np = pool_get(&nfs_node_pool, PR_WAITOK);
195 memset(np, 0, sizeof *np);
196 np->n_vnode = vp;
197
198 /*
199 * Insert the nfsnode in the hash queue for its new file handle
200 */
201
202 if (fhsize > NFS_SMALLFH) {
203 np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
204 } else
205 np->n_fhp = &np->n_fh;
206 memcpy(np->n_fhp, fhp, fhsize);
207 np->n_fhsize = fhsize;
208 np->n_accstamp = -1;
209 np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
210
211 rw_enter(&nmp->nm_rbtlock, RW_WRITER);
212 if (NULL != rb_tree_find_node(&nmp->nm_rbtree, &fhm)) {
213 rw_exit(&nmp->nm_rbtlock);
214 if (fhsize > NFS_SMALLFH) {
215 kmem_free(np->n_fhp, fhsize);
216 }
217 pool_put(&nfs_vattr_pool, np->n_vattr);
218 pool_put(&nfs_node_pool, np);
219 ungetnewvnode(vp);
220 goto loop;
221 }
222 vp->v_data = np;
223 genfs_node_init(vp, &nfs_genfsops);
224 /*
225 * Initalize read/write creds to useful values. VOP_OPEN will
226 * overwrite these.
227 */
228 np->n_rcred = curlwp->l_cred;
229 kauth_cred_hold(np->n_rcred);
230 np->n_wcred = curlwp->l_cred;
231 kauth_cred_hold(np->n_wcred);
232 VOP_LOCK(vp, LK_EXCLUSIVE);
233 NFS_INVALIDATE_ATTRCACHE(np);
234 uvm_vnp_setsize(vp, 0);
235 (void)rb_tree_insert_node(&nmp->nm_rbtree, np);
236 rw_exit(&nmp->nm_rbtlock);
237
238 *npp = np;
239 return (0);
240 }
241
242 int
243 nfs_inactive(void *v)
244 {
245 struct vop_inactive_args /* {
246 struct vnode *a_vp;
247 bool *a_recycle;
248 } */ *ap = v;
249 struct nfsnode *np;
250 struct sillyrename *sp;
251 struct vnode *vp = ap->a_vp;
252
253 np = VTONFS(vp);
254 if (vp->v_type != VDIR) {
255 sp = np->n_sillyrename;
256 np->n_sillyrename = (struct sillyrename *)0;
257 } else
258 sp = NULL;
259 if (sp != NULL)
260 nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
261 *ap->a_recycle = (np->n_flag & NREMOVED) != 0;
262 np->n_flag &=
263 (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
264
265 if (vp->v_type == VDIR && np->n_dircache)
266 nfs_invaldircache(vp,
267 NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
268
269 VOP_UNLOCK(vp);
270
271 if (sp != NULL) {
272 workqueue_enqueue(nfs_sillyworkq, &sp->s_work, NULL);
273 }
274
275 return (0);
276 }
277
278 /*
279 * Reclaim an nfsnode so that it can be used for other purposes.
280 */
281 int
282 nfs_reclaim(void *v)
283 {
284 struct vop_reclaim_args /* {
285 struct vnode *a_vp;
286 } */ *ap = v;
287 struct vnode *vp = ap->a_vp;
288 struct nfsnode *np = VTONFS(vp);
289 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
290
291 if (prtactive && vp->v_usecount > 1)
292 vprint("nfs_reclaim: pushing active", vp);
293
294 rw_enter(&nmp->nm_rbtlock, RW_WRITER);
295 rb_tree_remove_node(&nmp->nm_rbtree, np);
296 rw_exit(&nmp->nm_rbtlock);
297
298 /*
299 * Free up any directory cookie structures and
300 * large file handle structures that might be associated with
301 * this nfs node.
302 */
303 if (vp->v_type == VDIR && np->n_dircache != NULL) {
304 nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
305 hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
306 }
307 KASSERT(np->n_dirgens == NULL);
308
309 if (np->n_fhsize > NFS_SMALLFH)
310 kmem_free(np->n_fhp, np->n_fhsize);
311
312 pool_put(&nfs_vattr_pool, np->n_vattr);
313 if (np->n_rcred)
314 kauth_cred_free(np->n_rcred);
315
316 if (np->n_wcred)
317 kauth_cred_free(np->n_wcred);
318
319 if (vp->v_type == VREG) {
320 mutex_destroy(&np->n_commitlock);
321 }
322 genfs_node_destroy(vp);
323 pool_put(&nfs_node_pool, np);
324 vp->v_data = NULL;
325 return (0);
326 }
327
328 void
329 nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
330 {
331
332 *eobp = MAX(size, vp->v_size);
333 }
334
335 int
336 nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
337 kauth_cred_t cred)
338 {
339
340 return 0;
341 }
342
343 int
344 nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
345 {
346 int i;
347
348 mutex_enter(vp->v_interlock);
349 for (i = 0; i < npages; i++) {
350 pmap_page_protect(pgs[i], VM_PROT_READ);
351 }
352 mutex_exit(vp->v_interlock);
353
354 return genfs_gop_write(vp, pgs, npages, flags);
355 }
356
357 /*
358 * Remove a silly file that was rename'd earlier
359 */
360 static void
361 nfs_sillyworker(struct work *work, void *arg)
362 {
363 struct sillyrename *sp;
364 int error;
365
366 sp = (struct sillyrename *)work;
367 error = vn_lock(sp->s_dvp, LK_EXCLUSIVE);
368 if (error || sp->s_dvp->v_data == NULL) {
369 /* XXX should recover */
370 printf("%s: vp=%p error=%d\n", __func__, sp->s_dvp, error);
371 if (error == 0) {
372 vput(sp->s_dvp);
373 } else {
374 vrele(sp->s_dvp);
375 }
376 } else {
377 nfs_removeit(sp);
378 vput(sp->s_dvp);
379 }
380 kauth_cred_free(sp->s_cred);
381 kmem_free(sp, sizeof(*sp));
382 }
383