26493e6d6e5470ff098116ff4ebab5540f21bd60
[openafs.git] / src / afs / DARWIN / 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 struct vcache *
17 osi_NewVnode(void)
18 {
19     struct vcache *tvc;
20
21     tvc = afs_osi_Alloc(sizeof(struct vcache));
22     if (tvc == NULL) {
23         return NULL;
24     }
25     tvc->v = NULL; /* important to clean this, or use memset 0 */
26
27     return tvc;
28 }
29
30
31 #if defined(AFS_DARWIN80_ENV)
32 int
33 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep)
34 {
35     *slept = 0;
36
37     /* we ignore defersleep, as we *always* need to sleep */
38     if (!VREFCOUNT_GT(avc, 0) && avc->opens == 0 &&
39         (avc->f.states & CUnlinkedDel) == 0) {
40
41         vnode_t tvp = AFSTOV(avc);
42         /* VREFCOUNT_GT only sees usecounts, not iocounts */
43         /* so this may fail to actually recycle the vnode now */
44         /* must call vnode_get to avoid races. */
45         if (vnode_get(tvp) == 0) {
46             *slept=1;
47             /* must release lock, since vnode_put will immediately
48                reclaim if there are no other users */
49             ReleaseWriteLock(&afs_xvcache);
50             AFS_GUNLOCK();
51             vnode_recycle(tvp);
52             vnode_put(tvp);
53             AFS_GLOCK();
54             ObtainWriteLock(&afs_xvcache, 336);
55         }
56         /* we can't use the vnode_recycle return value to figure
57          * this out, since the iocount we have to hold makes it
58          * always "fail" */
59         if (AFSTOV(avc) == tvp) {
60             /* Caller will move this vcache to the head of the VLRU. */
61             return 0;
62         } else
63             return 1;
64     }
65     return 0;
66 }
67 #else
68 int
69 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep)
70 {
71     if (!VREFCOUNT_GT(avc,0)
72         || ((VREFCOUNT(avc) == 1) && (UBCINFOEXISTS(AFSTOV(avc))))
73         && avc->opens == 0 && (avc->f.states & CUnlinkedDel) == 0)
74     {
75         /*
76          * vgone() reclaims the vnode, which calls afs_FlushVCache(),
77          * then it puts the vnode on the free list.
78          * If we don't do this we end up with a cleaned vnode that's
79          * not on the free list.
80          */
81         AFS_GUNLOCK();
82         vgone(AFSTOV(avc));
83         AFS_GLOCK();
84         return 1;
85     }
86     return 0;
87 }
88 #endif /* AFS_DARWIN80_ENV */
89
90 void
91 osi_PrePopulateVCache(struct vcache *avc)
92 {
93     memset(avc, 0, sizeof(struct vcache));
94 }
95
96 void
97 osi_AttachVnode(struct vcache *avc, int seq)
98 {
99     ReleaseWriteLock(&afs_xvcache);
100     AFS_GUNLOCK();
101     afs_darwin_getnewvnode(avc);  /* includes one refcount */
102     AFS_GLOCK();
103     ObtainWriteLock(&afs_xvcache,338);
104 #ifdef AFS_DARWIN80_ENV
105     LOCKINIT(avc->rwlock);
106 #else
107     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
108 #endif
109 }
110
111 void
112 osi_PostPopulateVCache(struct vcache *avc)
113 {
114 #if !defined(AFS_DARWIN80_ENV)
115    avc->v->v_mount = afs_globalVFS;
116    vSetType(avc, VREG);
117 #else
118    vSetType(avc, VNON);
119 #endif
120 }
121
122 int
123 osi_vnhold(struct vcache *avc)
124 {
125     VN_HOLD(AFSTOV(avc));
126     return 0;
127 }