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