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