bozo: Introduce bnode_Wait()
[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, int defersleep)
18 {
19     struct vnode *vp;
20     int code;
21     int evicted = 0;
22
23     vp = AFSTOV(avc);
24
25     if (!VI_TRYLOCK(vp))
26         return evicted;
27     code = osi_fbsd_checkinuse(avc);
28     if (code != 0) {
29         VI_UNLOCK(vp);
30         return evicted;
31     }
32
33     if ((vp->v_iflag & VI_DOOMED) != 0) {
34         VI_UNLOCK(vp);
35         evicted = 1;
36         return evicted;
37     }
38
39     vholdl(vp);
40
41     ReleaseWriteLock(&afs_xvcache);
42     AFS_GUNLOCK();
43
44     *slept = 1;
45
46     if (vn_lock(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT) == 0) {
47         /*
48          * vrecycle() will vgone() only if its usecount is 0. If someone grabbed a
49          * new usecount ref just now, the vgone() will be skipped, and vrecycle
50          * will return 0.
51          */
52         if (vrecycle(vp) != 0) {
53             evicted = 1;
54         }
55
56         VOP_UNLOCK(vp, 0);
57     }
58
59     vdrop(vp);
60
61     AFS_GLOCK();
62     ObtainWriteLock(&afs_xvcache, 340);
63
64     return evicted;
65 }
66
67 struct vcache *
68 osi_NewVnode(void)
69 {
70     struct vcache *tvc;
71
72     tvc = afs_osi_Alloc(sizeof(struct vcache));
73     if (tvc == NULL) {
74         return NULL;
75     }
76     tvc->v = NULL; /* important to clean this, or use memset 0 */
77
78     return tvc;
79 }
80
81 void
82 osi_PrePopulateVCache(struct vcache *avc)
83 {
84     memset(avc, 0, sizeof(struct vcache));
85 }
86
87 void
88 osi_AttachVnode(struct vcache *avc, int seq)
89 {
90     struct vnode *vp;
91
92     ReleaseWriteLock(&afs_xvcache);
93     AFS_GUNLOCK();
94     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
95         panic("afs getnewvnode");       /* can't happen */
96     /* XXX verified on 80--TODO check on 7x */
97     if (!vp->v_mount) {
98         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* !glocked */
99         insmntque(vp, afs_globalVFS);
100         VOP_UNLOCK(vp, 0);
101     }
102     AFS_GLOCK();
103     ObtainWriteLock(&afs_xvcache,339);
104     if (avc->v != NULL) {
105         /* I'd like to know if this ever happens...
106          * We don't drop global for the rest of this function,
107          * so if we do lose the race, the other thread should
108          * have found the same vnode and finished initializing
109          * the vcache entry.  Is it conceivable that this vcache
110          * entry could be recycled during this interval?  If so,
111          * then there probably needs to be some sort of additional
112          * mutual exclusion (an Embryonic flag would suffice).
113          * -GAW */
114         afs_warn("afs_NewVCache: lost the race\n");
115         return;
116     }
117     avc->v = vp;
118     avc->v->v_data = avc;
119     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
120 }
121
122 void
123 osi_PostPopulateVCache(struct vcache *avc)
124 {
125     avc->v->v_mount = afs_globalVFS;
126     vSetType(avc, VREG);
127 }
128
129 int
130 osi_vnhold(struct vcache *avc)
131 {
132     struct vnode *vp = AFSTOV(avc);
133
134     vref(vp);
135     VI_LOCK(vp);
136     if ((vp->v_iflag & VI_DOOMED) != 0) {
137         VI_UNLOCK(vp);
138         vrele(vp);
139         return ENOENT;
140     }
141
142     VI_UNLOCK(vp);
143     return 0;
144 }