afs: clean afs_osi_Alloc() usage
[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) {
31     *slept = 0;
32
33     if (!VREFCOUNT_GT(avc, 0) && avc->opens == 0 &&
34         (avc->f.states & CUnlinkedDel) == 0) {
35
36         vnode_t tvp = AFSTOV(avc);
37         /* VREFCOUNT_GT only sees usecounts, not iocounts */
38         /* so this may fail to actually recycle the vnode now */
39         /* must call vnode_get to avoid races. */
40         if (vnode_get(tvp) == 0) {
41             *slept=1;
42             /* must release lock, since vnode_put will immediately
43                reclaim if there are no other users */
44             ReleaseWriteLock(&afs_xvcache);
45             AFS_GUNLOCK();
46             vnode_recycle(tvp);
47             vnode_put(tvp);
48             AFS_GLOCK();
49             ObtainWriteLock(&afs_xvcache, 336);
50         }
51         /* we can't use the vnode_recycle return value to figure
52          * this out, since the iocount we have to hold makes it
53          * always "fail" */
54         if (AFSTOV(avc) == tvp) {
55             if (*slept) {
56                 QRemove(&avc->vlruq);
57                 QAdd(&VLRU, &avc->vlruq);
58             }
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) {
68     if (!VREFCOUNT_GT(avc,0)
69         || ((VREFCOUNT(avc) == 1) && (UBCINFOEXISTS(AFSTOV(avc))))
70         && avc->opens == 0 && (avc->f.states & CUnlinkedDel) == 0)
71     {
72         /*
73          * vgone() reclaims the vnode, which calls afs_FlushVCache(),
74          * then it puts the vnode on the free list.
75          * If we don't do this we end up with a cleaned vnode that's
76          * not on the free list.
77          */
78         AFS_GUNLOCK();
79         vgone(AFSTOV(avc));
80         AFS_GLOCK();
81         return 1;
82     }
83     return 0;
84 }
85 #endif /* AFS_DARWIN80_ENV */
86
87 void
88 osi_PrePopulateVCache(struct vcache *avc) {
89     memset(avc, 0, sizeof(struct vcache));
90
91     /* PPC Darwin 80 seems to be a BOZONLOCK environment, so we need this
92      * here ... */
93 #if defined(AFS_BOZONLOCK_ENV)
94     afs_BozonInit(&avc->pvnLock, avc);
95 #endif
96 }
97
98 void
99 osi_AttachVnode(struct vcache *avc, int seq) {
100     ReleaseWriteLock(&afs_xvcache);
101     AFS_GUNLOCK();
102     afs_darwin_getnewvnode(avc, seq ? 0 : 1);  /* includes one refcount */
103     AFS_GLOCK();
104     ObtainWriteLock(&afs_xvcache,338);
105 #ifdef AFS_DARWIN80_ENV
106     LOCKINIT(avc->rwlock);
107 #else
108     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
109 #endif
110 }
111
112 void
113 osi_PostPopulateVCache(struct vcache *avc) {
114 #if !defined(AFS_DARWIN80_ENV)
115    avc->v->v_mount = afs_globalVFS;
116 #endif
117    vSetType(avc, VREG);
118 }