FBSD: do not recurse on the afs_xvcache write lock
[openafs.git] / src / afs / FBSD / osi_vcache.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 #include "afs/sysincludes.h"    /*Standard vendor system headers */
14 #include "afsincludes.h"        /*AFS-based standard headers */
15
16 int
17 osi_TryEvictVCache(struct vcache *avc, int *slept) {
18     struct vnode *vp = AFSTOV(avc);
19
20     if (!VREFCOUNT_GT(avc,0)
21         && avc->opens == 0 && (avc->f.states & CUnlinkedDel) == 0) {
22         /*
23          * vgone() reclaims the vnode, which calls afs_FlushVCache(),
24          * then it puts the vnode on the free list.
25          * If we don't do this we end up with a cleaned vnode that's
26          * not on the free list.
27          * XXX assume FreeBSD is the same for now.
28          */
29         /*
30          * We only have one caller (afs_ShakeLooseVCaches), which already
31          * holds the write lock.  vgonel() sometimes calls VOP_CLOSE(),
32          * so we must drop the write lock around our call to vgone().
33          */
34         ReleaseWriteLock(&afs_xvcache);
35         AFS_GUNLOCK();
36         *slept = 1;
37
38 #if defined(AFS_FBSD80_ENV)
39         /* vgone() is correct, but v_usecount is assumed not
40          * to be 0, and I suspect that currently our usage ensures that
41          * in fact it will */
42         if (vrefcnt(vp) < 1) {
43             vref(vp);
44         }
45         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* !glocked */
46 #endif
47
48         vgone(vp);
49 #if defined(AFS_FBSD80_ENV)
50         VOP_UNLOCK(vp, 0);
51 #endif
52
53         AFS_GLOCK();
54         ObtainWriteLock(&afs_xvcache, 340);
55         return 1;
56     }
57     return 0;
58 }
59
60 struct vcache *
61 osi_NewVnode(void) {
62     struct vcache *tvc;
63
64     tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
65     tvc->v = NULL; /* important to clean this, or use memset 0 */
66
67     return tvc;
68 }
69
70 void
71 osi_PrePopulateVCache(struct vcache *avc) {
72     memset(avc, 0, sizeof(struct vcache));
73 }
74
75 void
76 osi_AttachVnode(struct vcache *avc, int seq) {
77     struct vnode *vp;
78
79     ReleaseWriteLock(&afs_xvcache);
80     AFS_GUNLOCK();
81 #if defined(AFS_FBSD60_ENV)
82     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
83 #else
84     if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
85 #endif
86         panic("afs getnewvnode");       /* can't happen */
87 #ifdef AFS_FBSD70_ENV
88     /* XXX verified on 80--TODO check on 7x */
89     if (!vp->v_mount) {
90         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* !glocked */
91         insmntque(vp, afs_globalVFS);
92         VOP_UNLOCK(vp, 0);
93     }
94 #endif
95     AFS_GLOCK();
96     ObtainWriteLock(&afs_xvcache,339);
97     if (avc->v != NULL) {
98         /* I'd like to know if this ever happens...
99          * We don't drop global for the rest of this function,
100          * so if we do lose the race, the other thread should
101          * have found the same vnode and finished initializing
102          * the vcache entry.  Is it conceivable that this vcache
103          * entry could be recycled during this interval?  If so,
104          * then there probably needs to be some sort of additional
105          * mutual exclusion (an Embryonic flag would suffice).
106          * -GAW */
107         afs_warn("afs_NewVCache: lost the race\n");
108         return;
109     }
110     avc->v = vp;
111     avc->v->v_data = avc;
112     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
113 }
114
115 void
116 osi_PostPopulateVCache(struct vcache *avc) {
117     avc->v->v_mount = afs_globalVFS;
118     vSetType(avc, VREG);
119 }
120