linux: defer vcache evictions when sleep would be needed
[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 #if defined(AFS_FBSD80_ENV)
17 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags))
18 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags))
19 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags))
20 #else
21 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags, p))
22 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags, p))
23 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags, p))
24 #endif
25
26 int
27 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep) {
28
29     /*
30      * essentially all we want to do here is check that the
31      * vcache is not in use, then call vgone() (which will call
32      * inactive and reclaim as needed).  This requires some
33      * kind of complicated locking, which we already need to implement
34      * for FlushVCache, so just call that routine here and check
35      * its return value for whether the vcache was evict-able.
36      */
37     if (osi_VM_FlushVCache(avc, slept) != 0)
38         return 0;
39     else
40         return 1;
41 }
42
43 struct vcache *
44 osi_NewVnode(void) {
45     struct vcache *tvc;
46
47     tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
48     tvc->v = NULL; /* important to clean this, or use memset 0 */
49
50     return tvc;
51 }
52
53 void
54 osi_PrePopulateVCache(struct vcache *avc) {
55     memset(avc, 0, sizeof(struct vcache));
56 }
57
58 void
59 osi_AttachVnode(struct vcache *avc, int seq) {
60     struct vnode *vp;
61     struct thread *p = curthread;
62
63     ReleaseWriteLock(&afs_xvcache);
64     AFS_GUNLOCK();
65 #if defined(AFS_FBSD60_ENV)
66     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
67 #else
68     if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
69 #endif
70         panic("afs getnewvnode");       /* can't happen */
71 #ifdef AFS_FBSD70_ENV
72     /* XXX verified on 80--TODO check on 7x */
73     if (!vp->v_mount) {
74         ma_vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* !glocked */
75         insmntque(vp, afs_globalVFS);
76         MA_VOP_UNLOCK(vp, 0, p);
77     }
78 #endif
79     AFS_GLOCK();
80     ObtainWriteLock(&afs_xvcache,339);
81     if (avc->v != NULL) {
82         /* I'd like to know if this ever happens...
83          * We don't drop global for the rest of this function,
84          * so if we do lose the race, the other thread should
85          * have found the same vnode and finished initializing
86          * the vcache entry.  Is it conceivable that this vcache
87          * entry could be recycled during this interval?  If so,
88          * then there probably needs to be some sort of additional
89          * mutual exclusion (an Embryonic flag would suffice).
90          * -GAW */
91         afs_warn("afs_NewVCache: lost the race\n");
92         return;
93     }
94     avc->v = vp;
95     avc->v->v_data = avc;
96     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
97 }
98
99 void
100 osi_PostPopulateVCache(struct vcache *avc) {
101     avc->v->v_mount = afs_globalVFS;
102     vSetType(avc, VREG);
103 }
104