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