stats: incorrect clock square algorithm
[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     tvc->v = NULL; /* important to clean this, or use memset 0 */
74
75     return tvc;
76 }
77
78 void
79 osi_PrePopulateVCache(struct vcache *avc)
80 {
81     memset(avc, 0, sizeof(struct vcache));
82 }
83
84 void
85 osi_AttachVnode(struct vcache *avc, int seq)
86 {
87     struct vnode *vp;
88
89     ReleaseWriteLock(&afs_xvcache);
90     AFS_GUNLOCK();
91     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
92         panic("afs getnewvnode");       /* can't happen */
93     /* XXX verified on 80--TODO check on 7x */
94     if (!vp->v_mount) {
95         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* !glocked */
96         insmntque(vp, afs_globalVFS);
97         VOP_UNLOCK(vp, 0);
98     }
99     AFS_GLOCK();
100     ObtainWriteLock(&afs_xvcache,339);
101     if (avc->v != NULL) {
102         /* I'd like to know if this ever happens...
103          * We don't drop global for the rest of this function,
104          * so if we do lose the race, the other thread should
105          * have found the same vnode and finished initializing
106          * the vcache entry.  Is it conceivable that this vcache
107          * entry could be recycled during this interval?  If so,
108          * then there probably needs to be some sort of additional
109          * mutual exclusion (an Embryonic flag would suffice).
110          * -GAW */
111         afs_warn("afs_NewVCache: lost the race\n");
112         return;
113     }
114     avc->v = vp;
115     avc->v->v_data = avc;
116     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
117 }
118
119 void
120 osi_PostPopulateVCache(struct vcache *avc)
121 {
122     avc->v->v_mount = afs_globalVFS;
123     vSetType(avc, VREG);
124 }
125
126 int
127 osi_vnhold(struct vcache *avc)
128 {
129     struct vnode *vp = AFSTOV(avc);
130
131     VI_LOCK(vp);
132     if ((vp->v_iflag & VI_DOOMED) != 0) {
133         VI_UNLOCK(vp);
134         return ENOENT;
135     }
136
137     vrefl(AFSTOV(avc));
138     VI_UNLOCK(vp);
139     return 0;
140 }