FreeBSD/Linux Kernel Cross Reference
sys/nfs/nfs_node.c
1 /*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. 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 * @(#)nfs_node.c 8.2 (Berkeley) 12/30/93
37 * $FreeBSD: src/sys/nfs/nfs_node.c,v 1.13.2.5 1999/09/05 08:19:39 peter 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/rpcv2.h>
50 #include <nfs/nfsproto.h>
51 #include <nfs/nfs.h>
52 #include <nfs/nfsnode.h>
53 #include <nfs/nfsmount.h>
54 #include <nfs/nqnfs.h>
55
56 LIST_HEAD(nfsnodehashhead, nfsnode) *nfsnodehashtbl;
57 u_long nfsnodehash;
58
59 #define TRUE 1
60 #define FALSE 0
61
62 /*
63 * Initialize hash links for nfsnodes
64 * and build nfsnode free list.
65 */
66 void
67 nfs_nhinit()
68 {
69
70 #ifndef lint
71 if ((sizeof(struct nfsnode) - 1) & sizeof(struct nfsnode))
72 printf("nfs_nhinit: bad size %d\n", sizeof(struct nfsnode));
73 #endif /* not lint */
74 nfsnodehashtbl = hashinit(desiredvnodes, M_NFSNODE, &nfsnodehash);
75 }
76
77 /*
78 * Compute an entry in the NFS hash table structure
79 */
80 u_long
81 nfs_hash(fhp, fhsize)
82 register nfsfh_t *fhp;
83 int fhsize;
84 {
85 register u_char *fhpp;
86 register u_long fhsum;
87 register int i;
88
89 fhpp = &fhp->fh_bytes[0];
90 fhsum = 0;
91 for (i = 0; i < fhsize; i++)
92 fhsum += *fhpp++;
93 return (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 int nfs_node_hash_lock;
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, *np2;
112 struct nfsnodehashhead *nhpp;
113 register struct vnode *vp;
114 struct vnode *nvp;
115 int error;
116
117 retry:
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, 1))
126 goto loop;
127 *npp = np;
128 return(0);
129 }
130 /*
131 * Obtain a lock to prevent a race condition if the getnewvnode()
132 * or MALLOC() below happens to block.
133 */
134 if (nfs_node_hash_lock) {
135 while (nfs_node_hash_lock) {
136 nfs_node_hash_lock = -1;
137 tsleep(&nfs_node_hash_lock, PVM, "nfsngt", 0);
138 }
139 goto loop;
140 }
141 nfs_node_hash_lock = 1;
142
143 /*
144 * Do the MALLOC before the getnewvnode since doing so afterward
145 * might cause a bogus v_data pointer to get dereferenced
146 * elsewhere if MALLOC should block.
147 */
148 MALLOC(np, struct nfsnode *, sizeof *np, M_NFSNODE, M_WAITOK);
149
150 error = getnewvnode(VT_NFS, mntp, nfsv2_vnodeop_p, &nvp);
151 if (error) {
152 if (nfs_node_hash_lock < 0)
153 wakeup(&nfs_node_hash_lock);
154 nfs_node_hash_lock = 0;
155 *npp = 0;
156 FREE(np, M_NFSNODE);
157 return (error);
158 }
159 vp = nvp;
160 bzero((caddr_t)np, sizeof *np);
161 vp->v_data = np;
162 np->n_vnode = vp;
163 /*
164 * Insert the nfsnode in the hash queue for its new file handle
165 */
166 for (np2 = nhpp->lh_first; np2 != 0; np2 = np2->n_hash.le_next) {
167 if (mntp != NFSTOV(np2)->v_mount || np2->n_fhsize != fhsize ||
168 bcmp((caddr_t)fhp, (caddr_t)np2->n_fhp, fhsize))
169 continue;
170 vrele(vp);
171 if (nfs_node_hash_lock < 0)
172 wakeup(&nfs_node_hash_lock);
173 nfs_node_hash_lock = 0;
174 FREE(np, M_NFSNODE);
175 goto retry;
176 }
177 LIST_INSERT_HEAD(nhpp, np, n_hash);
178 if (fhsize > NFS_SMALLFH) {
179 MALLOC(np->n_fhp, nfsfh_t *, fhsize, M_NFSBIGFH, M_WAITOK);
180 } else
181 np->n_fhp = &np->n_fh;
182 bcopy((caddr_t)fhp, (caddr_t)np->n_fhp, fhsize);
183 np->n_fhsize = fhsize;
184 *npp = np;
185
186 if (nfs_node_hash_lock < 0)
187 wakeup(&nfs_node_hash_lock);
188 nfs_node_hash_lock = 0;
189
190 /*
191 * Lock the new nfsnode.
192 */
193 VOP_LOCK(vp);
194
195 return (0);
196 }
197
198 int
199 nfs_inactive(ap)
200 struct vop_inactive_args /* {
201 struct vnode *a_vp;
202 } */ *ap;
203 {
204 register struct nfsnode *np;
205 register struct sillyrename *sp;
206 struct proc *p = curproc; /* XXX */
207
208 np = VTONFS(ap->a_vp);
209 if (prtactive && ap->a_vp->v_usecount != 0)
210 vprint("nfs_inactive: pushing active", ap->a_vp);
211 if (ap->a_vp->v_type != VDIR) {
212 sp = np->n_sillyrename;
213 np->n_sillyrename = (struct sillyrename *)0;
214 } else
215 sp = (struct sillyrename *)0;
216 if (sp) {
217 /*
218 * XXX We need a reference to keep the vnode from being
219 * recycled by getnewvnode while we do the I/O
220 * associated with discarding the buffers.
221 */
222 if (vget(ap->a_vp, 0))
223 panic("nfs_inactive: lost vnode");
224 /*
225 * Remove the silly file that was rename'd earlier
226 */
227 (void) nfs_vinvalbuf(ap->a_vp, 0, sp->s_cred, p, 1);
228 nfs_removeit(sp);
229 crfree(sp->s_cred);
230 vrele(sp->s_dvp);
231 FREE((caddr_t)sp, M_NFSREQ);
232 vrele(ap->a_vp); /* XXX Undo above reference */
233 }
234 np->n_flag &= (NMODIFIED | NFLUSHINPROG | NFLUSHWANT | NQNFSEVICTED |
235 NQNFSNONCACHE | NQNFSWRITE);
236 return (0);
237 }
238
239 /*
240 * Reclaim an nfsnode so that it can be used for other purposes.
241 */
242 int
243 nfs_reclaim(ap)
244 struct vop_reclaim_args /* {
245 struct vnode *a_vp;
246 } */ *ap;
247 {
248 register struct vnode *vp = ap->a_vp;
249 register struct nfsnode *np = VTONFS(vp);
250 register struct nfsmount *nmp = VFSTONFS(vp->v_mount);
251 register struct nfsdmap *dp, *dp2;
252
253 if (prtactive && vp->v_usecount != 0)
254 vprint("nfs_reclaim: pushing active", vp);
255
256 if (np->n_hash.le_prev != NULL)
257 LIST_REMOVE(np, n_hash);
258
259 /*
260 * For nqnfs, take it off the timer queue as required.
261 */
262 if ((nmp->nm_flag & NFSMNT_NQNFS) && np->n_timer.cqe_next != 0) {
263 CIRCLEQ_REMOVE(&nmp->nm_timerhead, np, n_timer);
264 }
265
266 /*
267 * Free up any directory cookie structures and
268 * large file handle structures that might be associated with
269 * this nfs node.
270 */
271 if (vp->v_type == VDIR) {
272 dp = np->n_cookies.lh_first;
273 while (dp) {
274 dp2 = dp;
275 dp = dp->ndm_list.le_next;
276 FREE((caddr_t)dp2, M_NFSDIROFF);
277 }
278 }
279 if (np->n_fhsize > NFS_SMALLFH) {
280 FREE((caddr_t)np->n_fhp, M_NFSBIGFH);
281 }
282
283 cache_purge(vp);
284 FREE(vp->v_data, M_NFSNODE);
285 vp->v_data = (void *)0;
286 return (0);
287 }
288
289 /*
290 * Lock an nfsnode
291 */
292 int
293 nfs_lock(ap)
294 struct vop_lock_args /* {
295 struct vnode *a_vp;
296 } */ *ap;
297 {
298 register struct vnode *vp = ap->a_vp;
299
300 /*
301 * Ugh, another place where interruptible mounts will get hung.
302 * If you make this sleep interruptible, then you have to fix all
303 * the VOP_LOCK() calls to expect interruptibility.
304 */
305 while (vp->v_flag & VXLOCK) {
306 vp->v_flag |= VXWANT;
307 (void) tsleep((caddr_t)vp, PINOD, "nfslck", 0);
308 }
309 if (vp->v_tag == VT_NON)
310 return (ENOENT);
311
312 #if 0
313 /*
314 * Only lock regular files. If a server crashed while we were
315 * holding a directory lock, we could easily end up sleeping
316 * until the server rebooted while holding a lock on the root.
317 * Locks are only needed for protecting critical sections in
318 * VMIO at the moment.
319 * New vnodes will have type VNON but they should be locked
320 * since they may become VREG. This is checked in loadattrcache
321 * and unwanted locks are released there.
322 */
323 if (vp->v_type == VREG || vp->v_type == VNON) {
324 while (np->n_flag & NLOCKED) {
325 np->n_flag |= NWANTED;
326 (void) tsleep((caddr_t) np, PINOD, "nfslck2", 0);
327 /*
328 * If the vnode has transmuted into a VDIR while we
329 * were asleep, then skip the lock.
330 */
331 if (vp->v_type != VREG && vp->v_type != VNON)
332 return (0);
333 }
334 np->n_flag |= NLOCKED;
335 }
336 #endif
337
338 return (0);
339 }
340
341 /*
342 * Unlock an nfsnode
343 */
344 int
345 nfs_unlock(ap)
346 struct vop_unlock_args /* {
347 struct vnode *a_vp;
348 } */ *ap;
349 {
350 #if 0
351 struct vnode* vp = ap->a_vp;
352 struct nfsnode* np = VTONFS(vp);
353
354 if (vp->v_type == VREG || vp->v_type == VNON) {
355 if (!(np->n_flag & NLOCKED))
356 panic("nfs_unlock: nfsnode not locked");
357 np->n_flag &= ~NLOCKED;
358 if (np->n_flag & NWANTED) {
359 np->n_flag &= ~NWANTED;
360 wakeup((caddr_t) np);
361 }
362 }
363 #endif
364
365 return (0);
366 }
367
368 /*
369 * Check for a locked nfsnode
370 */
371 int
372 nfs_islocked(ap)
373 struct vop_islocked_args /* {
374 struct vnode *a_vp;
375 } */ *ap;
376 {
377 return VTONFS(ap->a_vp)->n_flag & NLOCKED ? 1 : 0;
378 }
379
380 /*
381 * Nfs abort op, called after namei() when a CREATE/DELETE isn't actually
382 * done. Currently nothing to do.
383 */
384 /* ARGSUSED */
385 int
386 nfs_abortop(ap)
387 struct vop_abortop_args /* {
388 struct vnode *a_dvp;
389 struct componentname *a_cnp;
390 } */ *ap;
391 {
392
393 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
394 FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
395 return (0);
396 }
Cache object: f979583846bf0bb672586d11cc181a84
|