nfs_node.c revision 1.5 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)nfs_node.c 7.34 (Berkeley) 5/15/91
37 * $Id: nfs_node.c,v 1.5 1994/02/15 21:07:10 pk Exp $
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48
49 #include <nfs/nfsv2.h>
50 #include <nfs/nfs.h>
51 #include <nfs/nfsnode.h>
52 #include <nfs/nfsmount.h>
53
54 /* The request list head */
55 extern struct nfsreq nfsreqh;
56
57 #define NFSNOHSZ 512
58 #if ((NFSNOHSZ&(NFSNOHSZ-1)) == 0)
59 #define NFSNOHASH(fhsum) ((fhsum)&(NFSNOHSZ-1))
60 #else
61 #define NFSNOHASH(fhsum) (((unsigned)(fhsum))%NFSNOHSZ)
62 #endif
63
64 union nhead {
65 union nhead *nh_head[2];
66 struct nfsnode *nh_chain[2];
67 } nhead[NFSNOHSZ];
68
69 #define TRUE 1
70 #define FALSE 0
71
72 /*
73 * Initialize hash links for nfsnodes
74 * and build nfsnode free list.
75 */
76 nfs_nhinit()
77 {
78 register int i;
79 register union nhead *nh = nhead;
80
81 #ifndef lint
82 if (VN_MAXPRIVATE < sizeof(struct nfsnode))
83 panic("nfs_nhinit: too small");
84 #endif /* not lint */
85 for (i = NFSNOHSZ; --i >= 0; nh++) {
86 nh->nh_head[0] = nh;
87 nh->nh_head[1] = nh;
88 }
89 }
90
91 /*
92 * Compute an entry in the NFS hash table structure
93 */
94 union nhead *
95 nfs_hash(fhp)
96 register nfsv2fh_t *fhp;
97 {
98 register u_char *fhpp;
99 register u_long fhsum;
100 int i;
101
102 fhpp = &fhp->fh_bytes[0];
103 fhsum = 0;
104 for (i = 0; i < NFSX_FH; i++)
105 fhsum += *fhpp++;
106 return (&nhead[NFSNOHASH(fhsum)]);
107 }
108
109 /*
110 * Look up a vnode/nfsnode by file handle.
111 * Callers must check for mount points!!
112 * In all cases, a pointer to a
113 * nfsnode structure is returned.
114 */
115 nfs_nget(mntp, fhp, npp)
116 struct mount *mntp;
117 register nfsv2fh_t *fhp;
118 struct nfsnode **npp;
119 {
120 register struct nfsnode *np;
121 register struct vnode *vp;
122 extern struct vnodeops nfsv2_vnodeops;
123 struct vnode *nvp;
124 union nhead *nh;
125 int error;
126
127 nh = nfs_hash(fhp);
128 loop:
129 for (np = nh->nh_chain[0]; np != (struct nfsnode *)nh; np = np->n_forw) {
130 if (mntp != NFSTOV(np)->v_mount ||
131 bcmp((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH))
132 continue;
133 if ((np->n_flag & NLOCKED) != 0) {
134 np->n_flag |= NWANT;
135 (void) tsleep((caddr_t)np, PINOD, "nfsnode", 0);
136 goto loop;
137 }
138 vp = NFSTOV(np);
139 if (vget(vp))
140 goto loop;
141 *npp = np;
142 return(0);
143 }
144 if (error = getnewvnode(VT_NFS, mntp, &nfsv2_vnodeops, &nvp)) {
145 *npp = 0;
146 return (error);
147 }
148 vp = nvp;
149 np = VTONFS(vp);
150 np->n_vnode = vp;
151 /*
152 * Insert the nfsnode in the hash queue for its new file handle
153 */
154 np->n_flag = 0;
155 insque(np, nh);
156 nfs_lock(vp);
157 bcopy((caddr_t)fhp, (caddr_t)&np->n_fh, NFSX_FH);
158 np->n_attrstamp = 0;
159 np->n_direofoffset = 0;
160 np->n_sillyrename = (struct sillyrename *)0;
161 np->n_size = 0;
162 np->n_mtime = 0;
163 np->n_lockf = 0;
164 np->n_delayed_mtime.tv_sec = VNOVAL;
165 np->n_delayed_atime.tv_sec = VNOVAL;
166 *npp = np;
167 return (0);
168 }
169
170 nfs_inactive(vp, p)
171 struct vnode *vp;
172 struct proc *p;
173 {
174 register struct nfsnode *np;
175 register struct sillyrename *sp;
176 struct nfsnode *dnp;
177 extern int prtactive;
178
179 np = VTONFS(vp);
180 if (prtactive && vp->v_usecount != 0)
181 vprint("nfs_inactive: pushing active", vp);
182 nfs_lock(vp);
183 sp = np->n_sillyrename;
184 np->n_sillyrename = (struct sillyrename *)0;
185 if (sp) {
186 /*
187 * Remove the silly file that was rename'd earlier
188 */
189 if (!nfs_nget(vp->v_mount, &sp->s_fh, &dnp)) {
190 sp->s_dvp = NFSTOV(dnp);
191 nfs_removeit(sp, p);
192 nfs_nput(sp->s_dvp);
193 }
194 crfree(sp->s_cred);
195 vrele(sp->s_dvp);
196 free((caddr_t)sp, M_NFSREQ);
197 }
198 nfs_unlock(vp);
199 np->n_flag &= NMODIFIED;
200 #ifdef notdef
201 /*
202 * Scan the request list for any requests left hanging about
203 */
204 s = splnet();
205 rep = nfsreqh.r_next;
206 while (rep && rep != &nfsreqh) {
207 if (rep->r_vp == vp) {
208 rep->r_prev->r_next = rep2 = rep->r_next;
209 rep->r_next->r_prev = rep->r_prev;
210 m_freem(rep->r_mreq);
211 if (rep->r_mrep != NULL)
212 m_freem(rep->r_mrep);
213 free((caddr_t)rep, M_NFSREQ);
214 rep = rep2;
215 } else
216 rep = rep->r_next;
217 }
218 splx(s);
219 #endif
220 return (0);
221 }
222
223 /*
224 * Reclaim an nfsnode so that it can be used for other purposes.
225 */
226 nfs_reclaim(vp)
227 register struct vnode *vp;
228 {
229 register struct nfsnode *np = VTONFS(vp);
230 extern int prtactive;
231
232 if (prtactive && vp->v_usecount != 0)
233 vprint("nfs_reclaim: pushing active", vp);
234 /*
235 * Remove the nfsnode from its hash chain.
236 */
237 remque(np);
238 np->n_forw = np;
239 np->n_back = np;
240 cache_purge(vp);
241 np->n_flag = 0;
242 np->n_direofoffset = 0;
243 return (0);
244 }
245
246 /*
247 * In theory, NFS does not need locking, but we make provision
248 * for doing it just in case it is needed.
249 */
250 int donfslocking = 0;
251 /*
252 * Lock an nfsnode
253 */
254
255 nfs_lock(vp)
256 struct vnode *vp;
257 {
258 register struct nfsnode *np = VTONFS(vp);
259
260 if (!donfslocking)
261 return;
262 while (np->n_flag & NLOCKED) {
263 np->n_flag |= NWANT;
264 if (np->n_lockholder == curproc->p_pid)
265 panic("locking against myself");
266 np->n_lockwaiter = curproc->p_pid;
267 (void) tsleep((caddr_t)np, PINOD, "nfslock", 0);
268 }
269 np->n_lockwaiter = 0;
270 np->n_lockholder = curproc->p_pid;
271 np->n_flag |= NLOCKED;
272 }
273
274 /*
275 * Unlock an nfsnode
276 */
277 nfs_unlock(vp)
278 struct vnode *vp;
279 {
280 register struct nfsnode *np = VTONFS(vp);
281
282 np->n_lockholder = 0;
283 np->n_flag &= ~NLOCKED;
284 if (np->n_flag & NWANT) {
285 np->n_flag &= ~NWANT;
286 wakeup((caddr_t)np);
287 }
288 }
289
290 /*
291 * Check for a locked nfsnode
292 */
293 nfs_islocked(vp)
294 struct vnode *vp;
295 {
296
297 if (VTONFS(vp)->n_flag & NLOCKED)
298 return (1);
299 return (0);
300 }
301
302 /*
303 * Unlock and vrele()
304 * since I can't decide if dirs. should be locked, I will check for
305 * the lock and be flexible
306 */
307 nfs_nput(vp)
308 struct vnode *vp;
309 {
310 register struct nfsnode *np = VTONFS(vp);
311
312 if (np->n_flag & NLOCKED)
313 nfs_unlock(vp);
314 vrele(vp);
315 }
316
317 /*
318 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
319 * done. Currently nothing to do.
320 */
321 /* ARGSUSED */
322 nfs_abortop(ndp)
323 struct nameidata *ndp;
324 {
325
326 if ((ndp->ni_nameiop & (HASBUF | SAVESTART)) == HASBUF)
327 FREE(ndp->ni_pnbuf, M_NAMEI);
328 return (0);
329 }
330