nfs_node.c revision 1.26 1 /* $NetBSD: nfs_node.c,v 1.26 1998/03/01 02:24:27 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, M_WAITOK, &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 register struct nfsnode *np;
112 struct nfsnodehashhead *nhpp;
113 register struct vnode *vp;
114 extern int (**nfsv2_vnodeop_p)__P((void *));
115 struct vnode *nvp;
116 int error;
117
118 nhpp = NFSNOHASH(nfs_hash(fhp, fhsize));
119 loop:
120 for (np = nhpp->lh_first; np != 0; np = np->n_hash.le_next) {
121 if (mntp != NFSTOV(np)->v_mount || np->n_fhsize != fhsize ||
122 bcmp((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize))
123 continue;
124 vp = NFSTOV(np);
125 if (vget(vp, LK_EXCLUSIVE))
126 goto loop;
127 *npp = np;
128 return(0);
129 }
130 if (lockmgr(&nfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0))
131 goto loop;
132 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
133 if (error) {
134 *npp = 0;
135 lockmgr(&nfs_hashlock, LK_RELEASE, 0);
136 return (error);
137 }
138 vp = nvp;
139 MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
140 bzero((caddr_t)np, sizeof *np);
141 vp->v_data = np;
142 np->n_vnode = vp;
143 /*
144 * Insert the nfsnode in the hash queue for its new file handle
145 */
146 LIST_INSERT_HEAD(nhpp, np, n_hash);
147 if (fhsize > NFS_SMALLFH) {
148 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
149 } else
150 np->n_fhp = &np->n_fh;
151 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
152 np->n_fhsize = fhsize;
153 MALLOC(np->n_vattr, struct vattr *, sizeof (struct vattr),
154 M_NFSNODE, M_WAITOK);
155 bzero(np->n_vattr, sizeof (struct vattr));
156 lockmgr(&nfs_hashlock, LK_RELEASE, 0);
157 *npp = np;
158 return (0);
159 }
160
161 int
162 nfs_inactive(v)
163 void *v;
164 {
165 struct vop_inactive_args /* {
166 struct vnode *a_vp;
167 struct proc *a_p;
168 } */ *ap = v;
169 register struct nfsnode *np;
170 register struct sillyrename *sp;
171 struct proc *p = ap->a_p;
172 extern int prtactive;
173
174 np = VTONFS(ap->a_vp);
175 if (prtactive && ap->a_vp->v_usecount != 0)
176 vprint("nfs_inactive: pushing active", ap->a_vp);
177 if (ap->a_vp->v_type != VDIR) {
178 sp = np->n_sillyrename;
179 np->n_sillyrename = (struct sillyrename *)0;
180 } else
181 sp = (struct sillyrename *)0;
182 if (sp) {
183 /*
184 * If the usecount is greater than zero, then we are
185 * being inactivated by a forcible unmount and do not
186 * have to get our own reference. In the normal case,
187 * we need a reference to keep the vnode from being
188 * recycled by getnewvnode while we do the I/O
189 * associated with discarding the buffers.
190 */
191 if (ap->a_vp->v_usecount > 0)
192 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
193 else if (vget(ap->a_vp, 0))
194 panic("nfs_inactive: lost vnode");
195 else {
196 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
197 vrele(ap->a_vp);
198 }
199
200
201 /*
202 * Remove the silly file that was rename'd earlier
203 */
204 nfs_removeit(sp);
205 crfree(sp->s_cred);
206 vrele(sp->s_dvp);
207 FREE((caddr_t)sp, M_NFSREQ);
208 }
209 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
210 NQNFSNONCACHE | NQNFSWRITE);
211 VOP_UNLOCK(ap->a_vp, 0);
212 return (0);
213 }
214
215 /*
216 * Reclaim an nfsnode so that it can be used for other purposes.
217 */
218 int
219 nfs_reclaim(v)
220 void *v;
221 {
222 struct vop_reclaim_args /* {
223 struct vnode *a_vp;
224 } */ *ap = v;
225 register struct vnode *vp = ap->a_vp;
226 register struct nfsnode *np = VTONFS(vp);
227 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
228 extern int prtactive;
229
230 if (prtactive && vp->v_usecount != 0)
231 vprint("nfs_reclaim: pushing active", vp);
232
233 LIST_REMOVE(np, n_hash);
234
235 /*
236 * For nqnfs, take it off the timer queue as required.
237 */
238 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
239 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
240 }
241
242 /*
243 * Free up any directory cookie structures and
244 * large file handle structures that might be associated with
245 * this nfs node.
246 */
247 if (vp->v_type == VDIR && np->n_dircache) {
248 nfs_invaldircache(vp, 1);
249 FREE(np->n_dircache, M_NFSDIROFF);
250 }
251 if (np->n_fhsize > NFS_SMALLFH) {
252 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
253 }
254
255 FREE(np->n_vattr, M_NFSNODE);
256 cache_purge(vp);
257 FREE(vp->v_data, M_NFSNODE);
258 vp->v_data = (void *)0;
259 return (0);
260 }
261