nfs_node.c revision 1.22 1 /* $NetBSD: nfs_node.c,v 1.22 1997/07/07 23:34:55 fvdl 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
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
47 #include <sys/vnode.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/lock.h>
51
52 #include <nfs/rpcv2.h>
53 #include <nfs/nfsproto.h>
54 #include <nfs/nfs.h>
55 #include <nfs/nfsnode.h>
56 #include <nfs/nfsmount.h>
57 #include <nfs/nqnfs.h>
58 #include <nfs/nfs_var.h>
59
60 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
61 u_long nfsnodehash;
62 struct lock nfs_hashlock;
63
64 #define TRUE 1
65 #define FALSE 0
66
67 /*
68 * Initialize hash links for nfsnodes
69 * and build nfsnode free list.
70 */
71 void
72 nfs_nhinit()
73 {
74
75 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
76 lockinit(&nfs_hashlock, PINOD, "nfs_hashlock", 0, 0);
77 }
78
79 /*
80 * Compute an entry in the NFS hash table structure
81 */
82 u_long
83 nfs_hash(fhp, fhsize)
84 register nfsfh_t *fhp;
85 int fhsize;
86 {
87 register u_char *fhpp;
88 register u_long fhsum;
89 register int i;
90
91 fhpp = &fhp->fh_bytes[0];
92 fhsum = 0;
93 for (i = 0; i < fhsize; i++)
94 fhsum += *fhpp++;
95 return (fhsum);
96 }
97
98 /*
99 * Look up a vnode/nfsnode by file handle.
100 * Callers must check for mount points!!
101 * In all cases, a pointer to a
102 * nfsnode structure is returned.
103 */
104 int
105 nfs_nget(mntp, fhp, fhsize, npp)
106 struct mount *mntp;
107 register nfsfh_t *fhp;
108 int fhsize;
109 struct nfsnode **npp;
110 {
111 struct proc *p = curproc; /* XXX */
112 register struct nfsnode *np;
113 struct nfsnodehashhead *nhpp;
114 register struct vnode *vp;
115 extern int (**nfsv2_vnodeop_p)__P((void *));
116 struct vnode *nvp;
117 int error;
118
119 nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
120 loop:
121 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
122 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
123 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
124 continue;
125 vp = NFSTOV(np);
126 #ifdef Lite2_integrated
127 if (vget(vp, LK_EXCLUSIVE, p))
128 #else
129 if (vget(vp, 1))
130 #endif
131 goto loop;
132 *npp = np;
133 return(0);
134 }
135 if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0, p))
136 goto loop;
137 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
138 if (error) {
139 *npp = 0;
140 lockmgr(&nfs_hashlock, LK_RELEASE, 0, p);
141 return (error);
142 }
143 vp = nvp;
144 MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
145 bzero((caddr_t)np, sizeof *np);
146 vp->v_data = np;
147 np->n_vnode = vp;
148 /*
149 * Insert the nfsnode in the hash queue for its new file handle
150 */
151 LIST_INSERT_HEAD(nhpp, np, n_hash);
152 if (fhsize > NFS_SMALLFH) {
153 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
154 } else
155 np->n_fhp = &np->n_fh;
156 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
157 np->n_fhsize = fhsize;
158 lockmgr(&nfs_hashlock, LK_RELEASE, 0, p);
159 *npp = np;
160 return (0);
161 }
162
163 int
164 nfs_inactive(v)
165 void *v;
166 {
167 struct vop_inactive_args /* {
168 struct vnode *a_vp;
169 #ifdef Lite2_integrated
170 struct proc *a_p;
171 #endif
172 } */ *ap = v;
173 register struct nfsnode *np;
174 register struct sillyrename *sp;
175 struct proc *p = curproc; /* XXX */
176 extern int prtactive;
177
178 np = VTONFS(ap->a_vp);
179 if (prtactive && ap->a_vp->v_usecount != 0)
180 vprint("nfs_inactive: pushing active", ap->a_vp);
181 if (ap->a_vp->v_type != VDIR) {
182 sp = np->n_sillyrename;
183 np->n_sillyrename = (struct sillyrename *)0;
184 } else
185 sp = (struct sillyrename *)0;
186 #ifdef Lite2_integrated
187 VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
188 #else
189 VOP_UNLOCK(ap->a_vp);
190 #endif
191 if (sp) {
192 /*
193 * If the usecount is greater than zero, then we are
194 * being inactivated by a forcible unmount and do not
195 * have to get our own reference. In the normal case,
196 * we need a reference to keep the vnode from being
197 * recycled by getnewvnode while we do the I/O
198 * associated with discarding the buffers.
199 */
200 if (ap->a_vp->v_usecount > 0)
201 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
202 #ifdef Lite2_integrated
203 else if (vget(ap->a_vp, 0, p))
204 #else
205 else if (vget(ap->a_vp, 0))
206 #endif
207 panic("nfs_inactive: lost vnode");
208 else {
209 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
210 vrele(ap->a_vp);
211 }
212
213
214 /*
215 * Remove the silly file that was rename'd earlier
216 */
217 nfs_removeit(sp);
218 crfree(sp->s_cred);
219 vrele(sp->s_dvp);
220 FREE((caddr_t)sp, M_NFSREQ);
221 }
222 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
223 NQNFSNONCACHE | NQNFSWRITE);
224 return (0);
225 }
226
227 /*
228 * Reclaim an nfsnode so that it can be used for other purposes.
229 */
230 int
231 nfs_reclaim(v)
232 void *v;
233 {
234 struct vop_reclaim_args /* {
235 struct vnode *a_vp;
236 } */ *ap = v;
237 register struct vnode *vp = ap->a_vp;
238 register struct nfsnode *np = VTONFS(vp);
239 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
240 register struct nfsdmap *dp, *dp2;
241 extern int prtactive;
242
243 if (prtactive && vp->v_usecount != 0)
244 vprint("nfs_reclaim: pushing active", vp);
245
246 LIST_REMOVE(np, n_hash);
247
248 /*
249 * For nqnfs, take it off the timer queue as required.
250 */
251 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
252 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
253 }
254
255 /*
256 * Free up any directory cookie structures and
257 * large file handle structures that might be associated with
258 * this nfs node.
259 */
260 if (vp->v_type == VDIR) {
261 dp = np->n_cookies.lh_first;
262 while (dp) {
263 dp2 = dp;
264 dp = dp->ndm_list.le_next;
265 FREE((caddr_t)dp2, M_NFSDIROFF);
266 }
267 }
268 if (np->n_fhsize > NFS_SMALLFH) {
269 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
270 }
271
272 cache_purge(vp);
273 FREE(vp->v_data, M_NFSNODE);
274 vp->v_data = (void *)0;
275 return (0);
276 }
277
278 #ifndef Lite2_integrated
279 /*
280 * Lock an nfsnode
281 */
282 int
283 nfs_lock(v)
284 void *v;
285 {
286 struct vop_lock_args /* {
287 struct vnode *a_vp;
288 } */ *ap = v;
289 register struct vnode *vp = ap->a_vp;
290
291 /*
292 * Ugh, another place where interruptible mounts will get hung.
293 * If you make this sleep interruptible, then you have to fix all
294 * the VOP_LOCK() calls to expect interruptibility.
295 */
296 while (vp->v_flag & VXLOCK) {
297 vp->v_flag |= VXWANT;
298 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0);
299 }
300 if (vp->v_tag == VT_NON)
301 return (ENOENT);
302 return (0);
303 }
304
305 /*
306 * Unlock an nfsnode
307 */
308 int
309 nfs_unlock(v)
310 void *v;
311 {
312 #if 0
313 struct vop_unlock_args /* {
314 struct vnode *a_vp;
315 } */ *ap = v;
316 #endif
317 return (0);
318 }
319
320 /*
321 * Check for a locked nfsnode
322 */
323 int
324 nfs_islocked(v)
325 void *v;
326 {
327 #if 0
328 struct vop_islocked_args /* {
329 struct vnode *a_vp;
330 } */ *ap = v;
331 #endif
332 return (0);
333 }
334 #endif /* Lite2_integrated */
335