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