macos: bulkstat redux
[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     struct vcache *tvc;
19
20     tvc = afs_osi_Alloc(sizeof(struct vcache));
21     osi_Assert(tvc != NULL);
22     tvc->v = NULL; /* important to clean this, or use memset 0 */
23
24     return tvc;
25 }
26
27
28 #if defined(AFS_DARWIN80_ENV)
29 int
30 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep) {
31     *slept = 0;
32
33     /* we ignore defersleep, as we *always* need to sleep */
34     if (!VREFCOUNT_GT(avc, 0) && avc->opens == 0 &&
35         (avc->f.states & CUnlinkedDel) == 0) {
36
37         vnode_t tvp = AFSTOV(avc);
38         /* VREFCOUNT_GT only sees usecounts, not iocounts */
39         /* so this may fail to actually recycle the vnode now */
40         /* must call vnode_get to avoid races. */
41         if (vnode_get(tvp) == 0) {
42             *slept=1;
43             /* must release lock, since vnode_put will immediately
44                reclaim if there are no other users */
45             ReleaseWriteLock(&afs_xvcache);
46             AFS_GUNLOCK();
47             vnode_recycle(tvp);
48             vnode_put(tvp);
49             AFS_GLOCK();
50             ObtainWriteLock(&afs_xvcache, 336);
51         }
52         /* we can't use the vnode_recycle return value to figure
53          * this out, since the iocount we have to hold makes it
54          * always "fail" */
55         if (AFSTOV(avc) == tvp) {
56             if (*slept) {
57                 QRemove(&avc->vlruq);
58                 QAdd(&VLRU, &avc->vlruq);
59             }
60             return 0;
61         } else
62             return 1;
63     }
64     return 0;
65 }
66 #else
67 int
68 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep) {
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     memset(avc, 0, sizeof(struct vcache));
91
92     /* PPC Darwin 80 seems to be a BOZONLOCK environment, so we need this
93      * here ... */
94 #if defined(AFS_BOZONLOCK_ENV)
95     afs_BozonInit(&avc->pvnLock, avc);
96 #endif
97 }
98
99 void
100 osi_AttachVnode(struct vcache *avc, int seq) {
101     ReleaseWriteLock(&afs_xvcache);
102     AFS_GUNLOCK();
103     afs_darwin_getnewvnode(avc);  /* includes one refcount */
104     AFS_GLOCK();
105     ObtainWriteLock(&afs_xvcache,338);
106 #ifdef AFS_DARWIN80_ENV
107     LOCKINIT(avc->rwlock);
108 #else
109     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
110 #endif
111 }
112
113 void
114 osi_PostPopulateVCache(struct vcache *avc) {
115 #if !defined(AFS_DARWIN80_ENV)
116    avc->v->v_mount = afs_globalVFS;
117    vSetType(avc, VREG);
118 #else
119    vSetType(avc, VNON);
120 #endif
121 }