6d837028eb1c913578782addaad55fed4181f101
[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 #if defined(AFS_FBSD80_ENV)
17 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags))
18 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags))
19 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags))
20 #else
21 #define ma_vn_lock(vp, flags, p) (vn_lock(vp, flags, p))
22 #define MA_VOP_LOCK(vp, flags, p) (VOP_LOCK(vp, flags, p))
23 #define MA_VOP_UNLOCK(vp, flags, p) (VOP_UNLOCK(vp, flags, p))
24 #endif
25
26 int
27 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep)
28 {
29     struct vnode *vp;
30     int code;
31
32     vp = AFSTOV(avc);
33
34     if (!VI_TRYLOCK(vp))
35         return 0;
36     code = osi_fbsd_checkinuse(avc);
37     if (code != 0) {
38         VI_UNLOCK(vp);
39         return 0;
40     }
41
42     if ((vp->v_iflag & VI_DOOMED) != 0) {
43         VI_UNLOCK(vp);
44         return 1;
45     }
46
47     /* must hold the vnode before calling vgone()
48      * This code largely copied from vfs_subr.c:vlrureclaim() */
49     vholdl(vp);
50     AFS_GUNLOCK();
51     *slept = 1;
52     /* use the interlock while locking, so no one else can DOOM this */
53     ma_vn_lock(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_RETRY, curthread);
54     vgone(vp);
55     MA_VOP_UNLOCK(vp, 0, curthread);
56     vdrop(vp);
57
58     AFS_GLOCK();
59     return 1;
60 }
61
62 struct vcache *
63 osi_NewVnode(void) {
64     struct vcache *tvc;
65
66     tvc = afs_osi_Alloc(sizeof(struct vcache));
67     tvc->v = NULL; /* important to clean this, or use memset 0 */
68
69     return tvc;
70 }
71
72 void
73 osi_PrePopulateVCache(struct vcache *avc) {
74     memset(avc, 0, sizeof(struct vcache));
75 }
76
77 void
78 osi_AttachVnode(struct vcache *avc, int seq) {
79     struct vnode *vp;
80 #if !defined(AFS_FBSD80_ENV)
81     struct thread *p = curthread;
82 #endif
83
84     ReleaseWriteLock(&afs_xvcache);
85     AFS_GUNLOCK();
86 #if defined(AFS_FBSD60_ENV)
87     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
88 #else
89     if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
90 #endif
91         panic("afs getnewvnode");       /* can't happen */
92 #ifdef AFS_FBSD70_ENV
93     /* XXX verified on 80--TODO check on 7x */
94     if (!vp->v_mount) {
95         ma_vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* !glocked */
96         insmntque(vp, afs_globalVFS);
97         MA_VOP_UNLOCK(vp, 0, p);
98     }
99 #endif
100     AFS_GLOCK();
101     ObtainWriteLock(&afs_xvcache,339);
102     if (avc->v != NULL) {
103         /* I'd like to know if this ever happens...
104          * We don't drop global for the rest of this function,
105          * so if we do lose the race, the other thread should
106          * have found the same vnode and finished initializing
107          * the vcache entry.  Is it conceivable that this vcache
108          * entry could be recycled during this interval?  If so,
109          * then there probably needs to be some sort of additional
110          * mutual exclusion (an Embryonic flag would suffice).
111          * -GAW */
112         afs_warn("afs_NewVCache: lost the race\n");
113         return;
114     }
115     avc->v = vp;
116     avc->v->v_data = avc;
117     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
118 }
119
120 void
121 osi_PostPopulateVCache(struct vcache *avc) {
122     avc->v->v_mount = afs_globalVFS;
123     vSetType(avc, VREG);
124 }
125