nfs_node.c revision 1.41.2.2 1 /* $NetBSD: nfs_node.c,v 1.41.2.2 2001/06/21 20:09:33 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 /*
76 * Initialize hash links for nfsnodes
77 * and build nfsnode free list.
78 */
79 void
80 nfs_nhinit()
81 {
82
83 nfsnodehashtbl = hashinit(desiredvnodes, HASH_LIST, M_NFSNODE,
84 M_WAITOK, &nfsnodehash);
85 lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
86
87 pool_init(&nfs_node_pool, sizeof(struct nfsnode), 0, 0, 0, "nfsnodepl",
88 0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
89 pool_init(&nfs_vattr_pool, sizeof(struct vattr), 0, 0, 0, "nfsvapl",
90 0, pool_page_alloc_nointr, pool_page_free_nointr, M_NFSNODE);
91 }
92
93 /*
94 * Free resources previoslu allocated in nfs_nhinit().
95 */
96 void
97 nfs_nhdone()
98 {
99 hashdone(nfsnodehashtbl, M_NFSNODE);
100 pool_destroy(&nfs_node_pool);
101 pool_destroy(&nfs_vattr_pool);
102 }
103
104 /*
105 * Compute an entry in the NFS hash table structure
106 */
107 u_long
108 nfs_hash(fhp, fhsize)
109 nfsfh_t *fhp;
110 int fhsize;
111 {
112 u_char *fhpp;
113 u_long fhsum;
114 int i;
115
116 fhpp = &fhp->fh_bytes[0];
117 fhsum = 0;
118 for (i = 0; i < fhsize; i++)
119 fhsum += *fhpp++;
120 return (fhsum);
121 }
122
123 /*
124 * Look up a vnode/nfsnode by file handle.
125 * Callers must check for mount points!!
126 * In all cases, a pointer to a
127 * nfsnode structure is returned.
128 */
129 int
130 nfs_nget(mntp, fhp, fhsize, npp)
131 struct mount *mntp;
132 nfsfh_t *fhp;
133 int fhsize;
134 struct nfsnode **npp;
135 {
136 struct nfsnode *np;
137 struct nfsnodehashhead *nhpp;
138 struct vnode *vp;
139 struct vnode *nvp;
140 int error;
141
142 nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
143 loop:
144 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
145 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
146 memcmp(fhp, np->n_fhp, fhsize))
147 continue;
148 vp = NFSTOV(np);
149 if (vget(vp, LK_EXCLUSIVE))
150 goto loop;
151 *npp = np;
152 return(0);
153 }
154 if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
155 goto loop;
156 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
157 if (error) {
158 *npp = 0;
159 lockmgr(&nfs_hashlock, LK_RELEASE, 0);
160 return (error);
161 }
162 vp = nvp;
163 np = pool_get(&nfs_node_pool, PR_WAITOK);
164 memset(np, 0, sizeof *np);
165 lockinit(&np->n_commitlock, PINOD, "nfsclock", 0, 0);
166 vp->v_data = np;
167 np->n_vnode = vp;
168
169 /*
170 * Insert the nfsnode in the hash queue for its new file handle
171 */
172 LIST_INSERT_HEAD(nhpp, np, n_hash);
173 if (fhsize > NFS_SMALLFH) {
174 np->n_fhp = malloc(fhsize, M_NFSBIGFH, M_WAITOK);
175 } else
176 np->n_fhp = &np->n_fh;
177 memcpy(np->n_fhp, fhp, fhsize);
178 np->n_fhsize = fhsize;
179 np->n_accstamp = -1;
180 np->n_vattr = pool_get(&nfs_vattr_pool, PR_WAITOK);
181
182 lockmgr(&vp->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
183
184 /*
185 * XXXUBC doing this while holding the nfs_hashlock is bad,
186 * but there's no alternative at the moment.
187 */
188 error = VOP_GETATTR(vp, np->n_vattr, curproc->l_proc->p_ucred,
189 curproc->l_proc);
190 if (error) {
191 lockmgr(&vp->v_lock, LK_RELEASE, 0);
192 lockmgr(&nfs_hashlock, LK_RELEASE, 0);
193 vgone(vp);
194 return error;
195 }
196 uvm_vnp_setsize(vp, np->n_vattr->va_size);
197
198 lockmgr(&nfs_hashlock, LK_RELEASE, 0);
199 *npp = np;
200 return (0);
201 }
202
203 int
204 nfs_inactive(v)
205 void *v;
206 {
207 struct vop_inactive_args /* {
208 struct vnode *a_vp;
209 struct proc *a_p;
210 } */ *ap = v;
211 struct nfsnode *np;
212 struct sillyrename *sp;
213 struct proc *p = ap->a_p;
214 struct vnode *vp = ap->a_vp;
215
216 np = VTONFS(vp);
217 if (prtactive && vp->v_usecount != 0)
218 vprint("nfs_inactive: pushing active", vp);
219 if (vp->v_type != VDIR) {
220 sp = np->n_sillyrename;
221 np->n_sillyrename = (struct sillyrename *)0;
222 } else
223 sp = NULL;
224 if (sp != NULL)
225 nfs_vinvalbuf(vp, 0, sp->s_cred, p, 1);
226 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
227 NQNFSNONCACHE | NQNFSWRITE);
228 VOP_UNLOCK(vp, 0);
229 if (sp != NULL) {
230
231 /*
232 * Remove the silly file that was rename'd earlier
233 */
234
235 vn_lock(sp->s_dvp, LK_EXCLUSIVE | LK_RETRY);
236 nfs_removeit(sp);
237 crfree(sp->s_cred);
238 vput(sp->s_dvp);
239 FREE(sp, M_NFSREQ);
240 }
241 return (0);
242 }
243
244 /*
245 * Reclaim an nfsnode so that it can be used for other purposes.
246 */
247 int
248 nfs_reclaim(v)
249 void *v;
250 {
251 struct vop_reclaim_args /* {
252 struct vnode *a_vp;
253 } */ *ap = v;
254 struct vnode *vp = ap->a_vp;
255 struct nfsnode *np = VTONFS(vp);
256 struct nfsmount *nmp = VFSTONFS(vp->v_mount);
257
258 if (prtactive && vp->v_usecount != 0)
259 vprint("nfs_reclaim: pushing active", vp);
260
261 LIST_REMOVE(np, n_hash);
262
263 /*
264 * For nqnfs, take it off the timer queue as required.
265 */
266 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
267 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
268 }
269
270 /*
271 * Free up any directory cookie structures and
272 * large file handle structures that might be associated with
273 * this nfs node.
274 */
275 if (vp->v_type == VDIR && np->n_dircache) {
276 nfs_invaldircache(vp, 1);
277 FREE(np->n_dircache, M_NFSDIROFF);
278 }
279 if (np->n_fhsize > NFS_SMALLFH) {
280 free(np->n_fhp, M_NFSBIGFH);
281 }
282
283 pool_put(&nfs_vattr_pool, np->n_vattr);
284 if (np->n_rcred) {
285 crfree(np->n_rcred);
286 }
287 if (np->n_wcred) {
288 crfree(np->n_wcred);
289 }
290 cache_purge(vp);
291 pool_put(&nfs_node_pool, vp->v_data);
292 vp->v_data = NULL;
293 return (0);
294 }
295