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