nfs_node.c revision 1.105 1 1.105 matt /* $NetBSD: nfs_node.c,v 1.105 2008/10/22 11:36:06 matt Exp $ */
2 1.12 cgd
3 1.1 cgd /*
4 1.9 mycroft * Copyright (c) 1989, 1993
5 1.9 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.70 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd *
34 1.16 fvdl * @(#)nfs_node.c 8.6 (Berkeley) 5/22/95
35 1.1 cgd */
36 1.47 lukem
37 1.47 lukem #include <sys/cdefs.h>
38 1.105 matt __KERNEL_RCSID(0, "$NetBSD: nfs_node.c,v 1.105 2008/10/22 11:36:06 matt Exp $");
39 1.1 cgd
40 1.35 bjh21 #include "opt_nfs.h"
41 1.16 fvdl
42 1.4 mycroft #include <sys/param.h>
43 1.4 mycroft #include <sys/systm.h>
44 1.4 mycroft #include <sys/proc.h>
45 1.4 mycroft #include <sys/mount.h>
46 1.4 mycroft #include <sys/namei.h>
47 1.4 mycroft #include <sys/vnode.h>
48 1.4 mycroft #include <sys/kernel.h>
49 1.28 thorpej #include <sys/pool.h>
50 1.22 fvdl #include <sys/lock.h>
51 1.48 lukem #include <sys/hash.h>
52 1.84 elad #include <sys/kauth.h>
53 1.1 cgd
54 1.9 mycroft #include <nfs/rpcv2.h>
55 1.16 fvdl #include <nfs/nfsproto.h>
56 1.4 mycroft #include <nfs/nfs.h>
57 1.4 mycroft #include <nfs/nfsnode.h>
58 1.4 mycroft #include <nfs/nfsmount.h>
59 1.15 christos #include <nfs/nfs_var.h>
60 1.1 cgd
61 1.104 pooka struct pool nfs_node_pool;
62 1.104 pooka struct pool nfs_vattr_pool;
63 1.57 thorpej
64 1.104 pooka MALLOC_JUSTDEFINE(M_NFSNODE, "NFS node", "NFS vnode private part");
65 1.28 thorpej
66 1.41 tsutsui extern int prtactive;
67 1.1 cgd
68 1.61 perseant void nfs_gop_size(struct vnode *, off_t, off_t *, int);
69 1.84 elad int nfs_gop_alloc(struct vnode *, off_t, off_t, int, kauth_cred_t);
70 1.53 chs int nfs_gop_write(struct vnode *, struct vm_page **, int, int);
71 1.46 chs
72 1.80 yamt static const struct genfs_ops nfs_genfsops = {
73 1.80 yamt .gop_size = nfs_gop_size,
74 1.80 yamt .gop_alloc = nfs_gop_alloc,
75 1.80 yamt .gop_write = nfs_gop_write,
76 1.46 chs };
77 1.46 chs
78 1.1 cgd /*
79 1.105 matt * Reinitialize inode hash table.
80 1.1 cgd */
81 1.15 christos void
82 1.105 matt nfs_node_reinit()
83 1.1 cgd {
84 1.104 pooka malloc_type_attach(M_NFSNODE);
85 1.104 pooka pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
86 1.104 pooka &pool_allocator_nointr, IPL_NONE);
87 1.104 pooka pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
88 1.104 pooka &pool_allocator_nointr, IPL_NONE);
89 1.31 jdolecek }
90 1.31 jdolecek
91 1.31 jdolecek /*
92 1.105 matt * Free resources previously allocated in nfs_node_reinit().
93 1.45 chs */
94 1.105 matt void
95 1.105 matt nfs_node_done()
96 1.105 matt {
97 1.105 matt pool_destroy(&nfs_node_pool);
98 1.105 matt pool_destroy(&nfs_vattr_pool);
99 1.105 matt malloc_type_detach(M_NFSNODE);
100 1.105 matt }
101 1.105 matt
102 1.105 matt #define RBTONFSNODE(node) \
103 1.105 matt (void *)((uintptr_t)(node) - offsetof(struct nfsnode, n_rbnode))
104 1.105 matt
105 1.105 matt struct fh_match {
106 1.105 matt nfsfh_t *fhm_fhp;
107 1.105 matt size_t fhm_fhsize;
108 1.105 matt size_t fhm_fhoffset;
109 1.105 matt };
110 1.105 matt
111 1.105 matt static int
112 1.105 matt nfs_compare_nodes(const struct rb_node *parent, const struct rb_node *node)
113 1.105 matt {
114 1.105 matt const struct nfsnode * const pnp = RBTONFSNODE(parent);
115 1.105 matt const struct nfsnode * const np = RBTONFSNODE(node);
116 1.105 matt
117 1.105 matt if (pnp->n_fhsize != np->n_fhsize)
118 1.105 matt return np->n_fhsize - pnp->n_fhsize;
119 1.45 chs
120 1.105 matt return memcmp(np->n_fhp, pnp->n_fhp, np->n_fhsize);
121 1.105 matt }
122 1.105 matt
123 1.105 matt static int
124 1.105 matt nfs_compare_node_fh(const struct rb_node *b, const void *key)
125 1.45 chs {
126 1.105 matt const struct nfsnode * const pnp = RBTONFSNODE(b);
127 1.105 matt const struct fh_match * const fhm = key;
128 1.45 chs
129 1.105 matt if (pnp->n_fhsize != fhm->fhm_fhsize)
130 1.105 matt return fhm->fhm_fhsize - pnp->n_fhsize;
131 1.79 perry
132 1.105 matt return memcmp(fhm->fhm_fhp, pnp->n_fhp, pnp->n_fhsize);
133 1.45 chs }
134 1.45 chs
135 1.105 matt static const struct rb_tree_ops nfs_node_rbtree_ops = {
136 1.105 matt .rbto_compare_nodes = nfs_compare_nodes,
137 1.105 matt .rbto_compare_key = nfs_compare_node_fh,
138 1.105 matt };
139 1.105 matt
140 1.31 jdolecek void
141 1.105 matt nfs_rbtinit(struct nfsmount *nmp)
142 1.31 jdolecek {
143 1.105 matt rb_tree_init(&nmp->nm_rbtree, &nfs_node_rbtree_ops);
144 1.105 matt }
145 1.104 pooka
146 1.1 cgd
147 1.1 cgd /*
148 1.1 cgd * Look up a vnode/nfsnode by file handle.
149 1.1 cgd * Callers must check for mount points!!
150 1.1 cgd * In all cases, a pointer to a
151 1.1 cgd * nfsnode structure is returned.
152 1.1 cgd */
153 1.15 christos int
154 1.75 yamt nfs_nget1(mntp, fhp, fhsize, npp, lkflags)
155 1.1 cgd struct mount *mntp;
156 1.33 augustss nfsfh_t *fhp;
157 1.16 fvdl int fhsize;
158 1.1 cgd struct nfsnode **npp;
159 1.75 yamt int lkflags;
160 1.1 cgd {
161 1.105 matt struct nfsnode *np;
162 1.33 augustss struct vnode *vp;
163 1.105 matt struct nfsmount *nmp = VFSTONFS(mntp);
164 1.1 cgd int error;
165 1.105 matt struct fh_match fhm;
166 1.105 matt struct rb_node *node;
167 1.105 matt
168 1.105 matt fhm.fhm_fhp = fhp;
169 1.105 matt fhm.fhm_fhsize = fhsize;
170 1.1 cgd
171 1.1 cgd loop:
172 1.105 matt rw_enter(&nmp->nm_rbtlock, RW_READER);
173 1.105 matt node = rb_tree_find_node(&nmp->nm_rbtree, &fhm);
174 1.105 matt if (node != NULL) {
175 1.105 matt np = RBTONFSNODE(node);
176 1.1 cgd vp = NFSTOV(np);
177 1.100 ad mutex_enter(&vp->v_interlock);
178 1.105 matt rw_exit(&nmp->nm_rbtlock);
179 1.100 ad error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK | lkflags);
180 1.75 yamt if (error == EBUSY)
181 1.75 yamt return error;
182 1.75 yamt if (error)
183 1.1 cgd goto loop;
184 1.1 cgd *npp = np;
185 1.1 cgd return(0);
186 1.1 cgd }
187 1.105 matt rw_exit(&nmp->nm_rbtlock);
188 1.100 ad
189 1.73 yamt error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &vp);
190 1.15 christos if (error) {
191 1.1 cgd *npp = 0;
192 1.1 cgd return (error);
193 1.1 cgd }
194 1.28 thorpej np = pool_get(&nfs_node_pool, PR_WAITOK);
195 1.38 chs memset(np, 0, sizeof *np);
196 1.1 cgd np->n_vnode = vp;
197 1.38 chs
198 1.1 cgd /*
199 1.1 cgd * Insert the nfsnode in the hash queue for its new file handle
200 1.1 cgd */
201 1.46 chs
202 1.16 fvdl if (fhsize > NFS_SMALLFH) {
203 1.98 yamt np->n_fhp = kmem_alloc(fhsize, KM_SLEEP);
204 1.16 fvdl } else
205 1.16 fvdl np->n_fhp = &np->n_fh;
206 1.38 chs memcpy(np->n_fhp, fhp, fhsize);
207 1.16 fvdl np->n_fhsize = fhsize;
208 1.30 fvdl np->n_accstamp = -1;
209 1.28 thorpej np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
210 1.71 fvdl
211 1.105 matt rw_enter(&nmp->nm_rbtlock, RW_WRITER);
212 1.105 matt if (NULL != rb_tree_find_node(&nmp->nm_rbtree, &fhm)) {
213 1.105 matt rw_exit(&nmp->nm_rbtlock);
214 1.100 ad if (fhsize > NFS_SMALLFH) {
215 1.100 ad kmem_free(np->n_fhp, fhsize);
216 1.100 ad }
217 1.100 ad pool_put(&nfs_vattr_pool, np->n_vattr);
218 1.100 ad pool_put(&nfs_node_pool, np);
219 1.100 ad ungetnewvnode(vp);
220 1.100 ad goto loop;
221 1.100 ad }
222 1.100 ad vp->v_data = np;
223 1.100 ad genfs_node_init(vp, &nfs_genfsops);
224 1.71 fvdl /*
225 1.71 fvdl * Initalize read/write creds to useful values. VOP_OPEN will
226 1.71 fvdl * overwrite these.
227 1.71 fvdl */
228 1.85 ad np->n_rcred = curlwp->l_cred;
229 1.84 elad kauth_cred_hold(np->n_rcred);
230 1.85 ad np->n_wcred = curlwp->l_cred;
231 1.84 elad kauth_cred_hold(np->n_wcred);
232 1.101 ad vlockmgr(&vp->v_lock, LK_EXCLUSIVE);
233 1.74 yamt NFS_INVALIDATE_ATTRCACHE(np);
234 1.74 yamt uvm_vnp_setsize(vp, 0);
235 1.105 matt rb_tree_insert_node(&nmp->nm_rbtree, &np->n_rbnode);
236 1.105 matt rw_exit(&nmp->nm_rbtlock);
237 1.100 ad
238 1.1 cgd *npp = np;
239 1.1 cgd return (0);
240 1.1 cgd }
241 1.1 cgd
242 1.15 christos int
243 1.15 christos nfs_inactive(v)
244 1.15 christos void *v;
245 1.15 christos {
246 1.9 mycroft struct vop_inactive_args /* {
247 1.9 mycroft struct vnode *a_vp;
248 1.97 ad bool *a_recycle;
249 1.15 christos } */ *ap = v;
250 1.33 augustss struct nfsnode *np;
251 1.33 augustss struct sillyrename *sp;
252 1.40 fvdl struct vnode *vp = ap->a_vp;
253 1.1 cgd
254 1.40 fvdl np = VTONFS(vp);
255 1.40 fvdl if (vp->v_type != VDIR) {
256 1.16 fvdl sp = np->n_sillyrename;
257 1.18 fvdl np->n_sillyrename = (struct sillyrename *)0;
258 1.18 fvdl } else
259 1.44 fvdl sp = NULL;
260 1.44 fvdl if (sp != NULL)
261 1.97 ad nfs_vinvalbuf(vp, 0, sp->s_cred, curlwp, 1);
262 1.97 ad *ap->a_recycle = (np->n_flag & NREMOVED) != 0;
263 1.94 yamt np->n_flag &=
264 1.94 yamt (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NEOFVALID | NTRUNCDELAYED);
265 1.76 yamt
266 1.76 yamt if (vp->v_type == VDIR && np->n_dircache)
267 1.78 yamt nfs_invaldircache(vp,
268 1.78 yamt NFS_INVALDIRCACHE_FORCE | NFS_INVALDIRCACHE_KEEPEOF);
269 1.76 yamt
270 1.44 fvdl VOP_UNLOCK(vp, 0);
271 1.76 yamt
272 1.44 fvdl if (sp != NULL) {
273 1.82 yamt int error;
274 1.19 fvdl
275 1.19 fvdl /*
276 1.1 cgd * Remove the silly file that was rename'd earlier
277 1.72 wrstuden *
278 1.72 wrstuden * Just in case our thread also has the parent node locked,
279 1.82 yamt * we use LK_CANRECURSE.
280 1.1 cgd */
281 1.40 fvdl
282 1.82 yamt error = vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_CANRECURSE);
283 1.82 yamt if (error || sp->s_dvp->v_data == NULL) {
284 1.82 yamt /* XXX should recover */
285 1.95 yamt printf("%s: vp=%p error=%d\n",
286 1.95 yamt __func__, sp->s_dvp, error);
287 1.95 yamt } else {
288 1.95 yamt nfs_removeit(sp);
289 1.82 yamt }
290 1.84 elad kauth_cred_free(sp->s_cred);
291 1.82 yamt vput(sp->s_dvp);
292 1.98 yamt kmem_free(sp, sizeof(*sp));
293 1.1 cgd }
294 1.59 fvdl
295 1.1 cgd return (0);
296 1.1 cgd }
297 1.1 cgd
298 1.1 cgd /*
299 1.1 cgd * Reclaim an nfsnode so that it can be used for other purposes.
300 1.1 cgd */
301 1.15 christos int
302 1.15 christos nfs_reclaim(v)
303 1.15 christos void *v;
304 1.15 christos {
305 1.9 mycroft struct vop_reclaim_args /* {
306 1.9 mycroft struct vnode *a_vp;
307 1.15 christos } */ *ap = v;
308 1.33 augustss struct vnode *vp = ap->a_vp;
309 1.33 augustss struct nfsnode *np = VTONFS(vp);
310 1.105 matt struct nfsmount *nmp = VFSTONFS(vp->v_mount);
311 1.1 cgd
312 1.99 ad if (prtactive && vp->v_usecount > 1)
313 1.1 cgd vprint("nfs_reclaim: pushing active", vp);
314 1.60 drochner
315 1.105 matt rw_enter(&nmp->nm_rbtlock, RW_WRITER);
316 1.105 matt rb_tree_remove_node(&nmp->nm_rbtree, &np->n_rbnode);
317 1.105 matt rw_exit(&nmp->nm_rbtlock);
318 1.16 fvdl
319 1.16 fvdl /*
320 1.16 fvdl * Free up any directory cookie structures and
321 1.16 fvdl * large file handle structures that might be associated with
322 1.16 fvdl * this nfs node.
323 1.16 fvdl */
324 1.103 tron if (vp->v_type == VDIR && np->n_dircache != NULL) {
325 1.103 tron nfs_invaldircache(vp, NFS_INVALDIRCACHE_FORCE);
326 1.102 ad hashdone(np->n_dircache, HASH_LIST, nfsdirhashmask);
327 1.103 tron }
328 1.65 yamt KASSERT(np->n_dirgens == NULL);
329 1.59 fvdl
330 1.59 fvdl if (np->n_fhsize > NFS_SMALLFH)
331 1.98 yamt kmem_free(np->n_fhp, np->n_fhsize);
332 1.16 fvdl
333 1.28 thorpej pool_put(&nfs_vattr_pool, np->n_vattr);
334 1.59 fvdl if (np->n_rcred)
335 1.84 elad kauth_cred_free(np->n_rcred);
336 1.59 fvdl
337 1.59 fvdl if (np->n_wcred)
338 1.84 elad kauth_cred_free(np->n_wcred);
339 1.59 fvdl
340 1.1 cgd cache_purge(vp);
341 1.90 yamt if (vp->v_type == VREG) {
342 1.90 yamt mutex_destroy(&np->n_commitlock);
343 1.90 yamt }
344 1.91 ad genfs_node_destroy(vp);
345 1.90 yamt pool_put(&nfs_node_pool, np);
346 1.38 chs vp->v_data = NULL;
347 1.1 cgd return (0);
348 1.46 chs }
349 1.46 chs
350 1.46 chs void
351 1.87 yamt nfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
352 1.46 chs {
353 1.83 yamt
354 1.46 chs *eobp = MAX(size, vp->v_size);
355 1.46 chs }
356 1.46 chs
357 1.46 chs int
358 1.87 yamt nfs_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
359 1.87 yamt kauth_cred_t cred)
360 1.46 chs {
361 1.87 yamt
362 1.46 chs return 0;
363 1.53 chs }
364 1.53 chs
365 1.53 chs int
366 1.53 chs nfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
367 1.53 chs {
368 1.53 chs int i;
369 1.53 chs
370 1.53 chs for (i = 0; i < npages; i++) {
371 1.53 chs pmap_page_protect(pgs[i], VM_PROT_READ);
372 1.53 chs }
373 1.53 chs return genfs_gop_write(vp, pgs, npages, flags);
374 1.1 cgd }
375