dd295f2ec7b3356c8f932540f24c854729152115
[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     if (!VREFCOUNT_GT(avc,0)
19         && avc->opens == 0 && (avc->f.states & CUnlinkedDel) == 0) {
20         /*
21          * vgone() reclaims the vnode, which calls afs_FlushVCache(),
22          * then it puts the vnode on the free list.
23          * If we don't do this we end up with a cleaned vnode that's
24          * not on the free list.
25          * XXX assume FreeBSD is the same for now.
26          */
27         AFS_GUNLOCK();
28
29 #if defined(AFS_FBSD80_ENV)
30         /* vgone() is correct, but v_usecount is assumed not
31          * to be 0, and I suspect that currently our usage ensures that
32          * in fact it will */
33         if (vrefcnt(AFSTOV(avc)) < 1) {
34             vref(AFSTOV(avc));
35         }
36         vn_lock(AFSTOV(avc), LK_EXCLUSIVE | LK_RETRY); /* !glocked */
37 #endif
38
39         vgone(AFSTOV(avc));
40 #if defined(AFS_FBSD80_ENV)
41         VOP_UNLOCK(AFSTOV(avc), 0);
42 #endif
43
44         AFS_GLOCK();
45         return 1;
46     }
47     return 0;
48 }
49
50 struct vcache *
51 osi_NewVnode(void) {
52     struct vcache *tvc;
53
54     tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
55     tvc->v = NULL; /* important to clean this, or use memset 0 */
56
57     return tvc;
58 }
59
60 void
61 osi_PrePopulateVCache(struct vcache *avc) {
62     memset(avc, 0, sizeof(struct vcache));
63 }
64
65 void
66 osi_AttachVnode(struct vcache *avc, int seq) {
67     struct vnode *vp;
68
69     ReleaseWriteLock(&afs_xvcache);
70     AFS_GUNLOCK();
71 #if defined(AFS_FBSD60_ENV)
72     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
73 #else
74     if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
75 #endif
76         panic("afs getnewvnode");       /* can't happen */
77 #ifdef AFS_FBSD70_ENV
78     /* XXX verified on 80--TODO check on 7x */
79     if (!vp->v_mount) {
80         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* !glocked */
81         insmntque(vp, afs_globalVFS);
82         VOP_UNLOCK(vp, 0);
83     }
84 #endif
85     AFS_GLOCK();
86     ObtainWriteLock(&afs_xvcache,339);
87     if (avc->v != NULL) {
88         /* I'd like to know if this ever happens...
89          * We don't drop global for the rest of this function,
90          * so if we do lose the race, the other thread should
91          * have found the same vnode and finished initializing
92          * the vcache entry.  Is it conceivable that this vcache
93          * entry could be recycled during this interval?  If so,
94          * then there probably needs to be some sort of additional
95          * mutual exclusion (an Embryonic flag would suffice).
96          * -GAW */
97         afs_warn("afs_NewVCache: lost the race\n");
98         return;
99     }
100     avc->v = vp;
101     avc->v->v_data = avc;
102     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
103 }
104
105 void
106 osi_PostPopulateVCache(struct vcache *avc) {
107     avc->v->v_mount = afs_globalVFS;
108     vSetType(avc, VREG);
109 }
110