nfs_node.c revision 1.12 1 /* $NetBSD: nfs_node.c,v 1.12 1994/06/29 06:42:09 cgd 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.2 (Berkeley) 12/30/93
39 */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49
50 #include <nfs/rpcv2.h>
51 #include <nfs/nfsv2.h>
52 #include <nfs/nfs.h>
53 #include <nfs/nfsnode.h>
54 #include <nfs/nfsmount.h>
55 #include <nfs/nqnfs.h>
56
57 struct nfsnode **nheadhashtbl;
58 u_long nheadhash;
59 #define NFSNOHASH(fhsum) ((fhsum)&nheadhash)
60
61 #define TRUE 1
62 #define FALSE 0
63
64 /*
65 * Initialize hash links for nfsnodes
66 * and build nfsnode free list.
67 */
68 nfs_nhinit()
69 {
70
71 #ifndef lint
72 if ((sizeof(struct nfsnode) - 1) & sizeof(struct nfsnode))
73 printf("nfs_nhinit: bad size %d\n", sizeof(struct nfsnode));
74 #endif /* not lint */
75 nheadhashtbl = hashinit(desiredvnodes, M_NFSNODE, &nheadhash);
76 }
77
78 /*
79 * Compute an entry in the NFS hash table structure
80 */
81 struct nfsnode **
82 nfs_hash(fhp)
83 register nfsv2fh_t *fhp;
84 {
85 register u_char *fhpp;
86 register u_long fhsum;
87 int i;
88
89 fhpp = &fhp->fh_bytes[0];
90 fhsum = 0;
91 for (i = 0; i < NFSX_FH; i++)
92 fhsum += *fhpp++;
93 return (&nheadhashtbl[NFSNOHASH(fhsum)]);
94 }
95
96 /*
97 * Look up a vnode/nfsnode by file handle.
98 * Callers must check for mount points!!
99 * In all cases, a pointer to a
100 * nfsnode structure is returned.
101 */
102 nfs_nget(mntp, fhp, npp)
103 struct mount *mntp;
104 register nfsv2fh_t *fhp;
105 struct nfsnode **npp;
106 {
107 register struct nfsnode *np, *nq, **nhpp;
108 register struct vnode *vp;
109 extern int (**nfsv2_vnodeop_p)();
110 struct vnode *nvp;
111 int error;
112
113 nhpp = nfs_hash(fhp);
114 loop:
115 for (np = *nhpp; np; np = np->n_forw) {
116 if (mntp != NFSTOV(np)->v_mount ||
117 bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
118 continue;
119 vp = NFSTOV(np);
120 if (vget(vp, 1))
121 goto loop;
122 *npp = np;
123 return(0);
124 }
125 if (error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp)) {
126 *npp = 0;
127 return (error);
128 }
129 vp = nvp;
130 MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
131 vp->v_data = np;
132 np->n_vnode = vp;
133 /*
134 * Insert the nfsnode in the hash queue for its new file handle
135 */
136 np->n_flag = 0;
137 if (nq = *nhpp)
138 nq->n_back = &np->n_forw;
139 np->n_forw = nq;
140 np->n_back = nhpp;
141 *nhpp = np;
142 bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
143 np->n_attrstamp = 0;
144 np->n_direofoffset = 0;
145 np->n_sillyrename = (struct sillyrename *)0;
146 np->n_size = 0;
147 np->n_mtime = 0;
148 np->n_lockf = 0;
149 if (VFSTONFS(mntp)->nm_flag & NFSMNT_NQNFS) {
150 np->n_brev = 0;
151 np->n_lrev = 0;
152 np->n_expiry = (time_t)0;
153 np->n_tnext = (struct nfsnode *)0;
154 }
155 *npp = np;
156 return (0);
157 }
158
159 nfs_inactive(ap)
160 struct vop_inactive_args /* {
161 struct vnode *a_vp;
162 } */ *ap;
163 {
164 register struct nfsnode *np;
165 register struct sillyrename *sp;
166 struct proc *p = curproc; /* XXX */
167 extern int prtactive;
168
169 np = VTONFS(ap->a_vp);
170 if (prtactive && ap->a_vp->v_usecount != 0)
171 vprint("nfs_inactive: pushing active", ap->a_vp);
172 sp = np->n_sillyrename;
173 np->n_sillyrename = (struct sillyrename *)0;
174 if (sp) {
175 /*
176 * Remove the silly file that was rename'd earlier
177 */
178 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
179 nfs_removeit(sp);
180 crfree(sp->s_cred);
181 vrele(sp->s_dvp);
182 #ifdef SILLYSEPARATE
183 free((caddr_t)sp, M_NFSREQ);
184 #endif
185 }
186 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
187 NQNFSNONCACHE | NQNFSWRITE);
188 return (0);
189 }
190
191 /*
192 * Reclaim an nfsnode so that it can be used for other purposes.
193 */
194 nfs_reclaim(ap)
195 struct vop_reclaim_args /* {
196 struct vnode *a_vp;
197 } */ *ap;
198 {
199 register struct vnode *vp = ap->a_vp;
200 register struct nfsnode *np = VTONFS(vp);
201 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
202 register struct nfsnode *nq;
203 extern int prtactive;
204
205 if (prtactive && vp->v_usecount != 0)
206 vprint("nfs_reclaim: pushing active", vp);
207 /*
208 * Remove the nfsnode from its hash chain.
209 */
210 if (nq = np->n_forw)
211 nq->n_back = np->n_back;
212 *np->n_back = nq;
213
214 /*
215 * For nqnfs, take it off the timer queue as required.
216 */
217 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_tnext) {
218 if (np->n_tnext == (struct nfsnode *)nmp)
219 nmp->nm_tprev = np->n_tprev;
220 else
221 np->n_tnext->n_tprev = np->n_tprev;
222 if (np->n_tprev == (struct nfsnode *)nmp)
223 nmp->nm_tnext = np->n_tnext;
224 else
225 np->n_tprev->n_tnext = np->n_tnext;
226 }
227 cache_purge(vp);
228 FREE(vp->v_data, M_NFSNODE);
229 vp->v_data = (void *)0;
230 return (0);
231 }
232
233 /*
234 * Lock an nfsnode
235 */
236 nfs_lock(ap)
237 struct vop_lock_args /* {
238 struct vnode *a_vp;
239 } */ *ap;
240 {
241 register struct vnode *vp = ap->a_vp;
242
243 /*
244 * Ugh, another place where interruptible mounts will get hung.
245 * If you make this sleep interruptible, then you have to fix all
246 * the VOP_LOCK() calls to expect interruptibility.
247 */
248 while (vp->v_flag & VXLOCK) {
249 vp->v_flag |= VXWANT;
250 sleep((caddr_t)vp, PINOD);
251 }
252 if (vp->v_tag == VT_NON)
253 return (ENOENT);
254 return (0);
255 }
256
257 /*
258 * Unlock an nfsnode
259 */
260 nfs_unlock(ap)
261 struct vop_unlock_args /* {
262 struct vnode *a_vp;
263 } */ *ap;
264 {
265
266 return (0);
267 }
268
269 /*
270 * Check for a locked nfsnode
271 */
272 nfs_islocked(ap)
273 struct vop_islocked_args /* {
274 struct vnode *a_vp;
275 } */ *ap;
276 {
277
278 return (0);
279 }
280
281 /*
282 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
283 * done. Currently nothing to do.
284 */
285 /* ARGSUSED */
286 int
287 nfs_abortop(ap)
288 struct vop_abortop_args /* {
289 struct vnode *a_dvp;
290 struct componentname *a_cnp;
291 } */ *ap;
292 {
293
294 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
295 FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
296 return (0);
297 }
298