FBSD: Add osi_fbsd_checkinuse
[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     struct thread *p = curthread;
81
82     ReleaseWriteLock(&afs_xvcache);
83     AFS_GUNLOCK();
84 #if defined(AFS_FBSD60_ENV)
85     if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
86 #else
87     if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
88 #endif
89         panic("afs getnewvnode");       /* can't happen */
90 #ifdef AFS_FBSD70_ENV
91     /* XXX verified on 80--TODO check on 7x */
92     if (!vp->v_mount) {
93         ma_vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); /* !glocked */
94         insmntque(vp, afs_globalVFS);
95         MA_VOP_UNLOCK(vp, 0, p);
96     }
97 #endif
98     AFS_GLOCK();
99     ObtainWriteLock(&afs_xvcache,339);
100     if (avc->v != NULL) {
101         /* I'd like to know if this ever happens...
102          * We don't drop global for the rest of this function,
103          * so if we do lose the race, the other thread should
104          * have found the same vnode and finished initializing
105          * the vcache entry.  Is it conceivable that this vcache
106          * entry could be recycled during this interval?  If so,
107          * then there probably needs to be some sort of additional
108          * mutual exclusion (an Embryonic flag would suffice).
109          * -GAW */
110         afs_warn("afs_NewVCache: lost the race\n");
111         return;
112     }
113     avc->v = vp;
114     avc->v->v_data = avc;
115     lockinit(&avc->rwlock, PINOD, "vcache", 0, 0);
116 }
117
118 void
119 osi_PostPopulateVCache(struct vcache *avc) {
120     avc->v->v_mount = afs_globalVFS;
121     vSetType(avc, VREG);
122 }
123