afs: Change VerifyVCache2 calls to VerifyVCache
[openafs.git] / src / afs / afs_osi_vm.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
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"      /* afs statistics */
17 #ifdef AFS_AIX_ENV
18 #include <sys/adspace.h>        /* for vm_att(), vm_det() */
19 #endif
20
21 int
22 osi_Active(struct vcache *avc)
23 {
24     AFS_STATCNT(osi_Active);
25 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || (AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
26     if ((avc->opens > 0) || (avc->f.states & CMAPPED))
27         return 1;               /* XXX: Warning, verify this XXX  */
28 #elif defined(AFS_SGI_ENV)
29     if ((avc->opens > 0) || AFS_VN_MAPPED(AFSTOV(avc)))
30         return 1;
31 #else
32     if (avc->opens > 0 || (AFSTOV(avc)->v_flag & VTEXT))
33         return (1);
34 #endif
35     return 0;
36 }
37
38 /* this call, unlike osi_FlushText, is supposed to discard caches that may
39    contain invalid information if a file is written remotely, but that may
40    contain valid information that needs to be written back if the file is
41    being written locally.  It doesn't subsume osi_FlushText, since the latter
42    function may be needed to flush caches that are invalidated by local writes.
43
44    avc->pvnLock is already held, avc->lock is guaranteed not to be held (by
45    us, of course).
46 */
47 void
48 osi_FlushPages(struct vcache *avc, afs_ucred_t *credp)
49 {
50     afs_hyper_t origDV;
51 #if defined(AFS_CACHE_BYPASS)
52     /* The optimization to check DV under read lock below is identical a
53      * change in CITI cache bypass work.  The problem CITI found in 1999
54      * was that this code and background daemon doing prefetching competed
55      * for the vcache entry shared lock.  It's not clear to me from the
56      * tech report, but it looks like CITI fixed the general prefetch code
57      * path as a bonus when experimenting on prefetch for cache bypass, see
58      * citi-tr-01-3.
59      */
60 #endif
61     if (vType(avc) == VDIR) {
62         /* not applicable to directories; they're never mapped or stored in
63          * pages */
64         return;
65     }
66     ObtainReadLock(&avc->lock);
67     /* If we've already purged this version, or if we're the ones
68      * writing this version, don't flush it (could lose the
69      * data we're writing). */
70     if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
71         || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
72         ReleaseReadLock(&avc->lock);
73         return;
74     }
75     ReleaseReadLock(&avc->lock);
76     ObtainWriteLock(&avc->lock, 10);
77     /* Check again */
78     if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
79         || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
80         ReleaseWriteLock(&avc->lock);
81         return;
82     }
83
84     /* At this point, you might think that we can skip trying to flush pages
85      * if mapDV is zero, since a file with a zero DV will not have any data in
86      * it. However, some platforms (notably Linux 2.6.22+) will keep a page
87      * full of zeroes around for an empty file. So play it safe and always
88      * flush pages. */
89
90     AFS_STATCNT(osi_FlushPages);
91     hset(origDV, avc->f.m.DataVersion);
92     afs_Trace3(afs_iclSetp, CM_TRACE_FLUSHPAGES, ICL_TYPE_POINTER, avc,
93                ICL_TYPE_INT32, origDV.low, ICL_TYPE_INT32, avc->f.m.Length);
94
95     ReleaseWriteLock(&avc->lock);
96     AFS_GUNLOCK();
97     osi_VM_FlushPages(avc, credp);
98     AFS_GLOCK();
99     ObtainWriteLock(&avc->lock, 88);
100
101     /* do this last, and to original version, since stores may occur
102      * while executing above PUTPAGE call */
103     hset(avc->mapDV, origDV);
104     ReleaseWriteLock(&avc->lock);
105 }
106
107 #ifdef  AFS_TEXT_ENV
108
109 /* This call is supposed to flush all caches that might be invalidated
110  * by either a local write operation or a write operation done on
111  * another client.  This call may be called repeatedly on the same
112  * version of a file, even while a file is being written, so it
113  * shouldn't do anything that would discard newly written data before
114  * it is written to the file system. */
115
116 void
117 osi_FlushText_really(struct vcache *vp)
118 {
119     afs_hyper_t fdv;            /* version before which we'll flush */
120
121     AFS_STATCNT(osi_FlushText);
122     /* see if we've already flushed this data version */
123     if (hcmp(vp->f.m.DataVersion, vp->flushDV) <= 0)
124         return;
125
126     ObtainWriteLock(&afs_ftf, 317);
127     hset(fdv, vp->f.m.DataVersion);
128
129     /* why this disgusting code below?
130      *    xuntext, called by xrele, doesn't notice when it is called
131      * with a freed text object.  Sun continually calls xrele or xuntext
132      * without any locking, as long as VTEXT is set on the
133      * corresponding vnode.
134      *    But, if the text object is locked when you check the VTEXT
135      * flag, several processes can wait in xuntext, waiting for the
136      * text lock; when the second one finally enters xuntext's
137      * critical region, the text object is already free, but the check
138      * was already done by xuntext's caller.
139      *    Even worse, it turns out that xalloc locks the text object
140      * before reading or stating a file via the vnode layer.  Thus, we
141      * could end up in getdcache, being asked to bring in a new
142      * version of a file, but the corresponding text object could be
143      * locked.  We can't flush the text object without causing
144      * deadlock, so now we just don't try to lock the text object
145      * unless it is guaranteed to work.  And we try to flush the text
146      * when we need to a bit more often at the vnode layer.  Sun
147      * really blew the vm-cache flushing interface.
148      */
149
150 #if defined (AFS_HPUX_ENV)
151     if (vp->v.v_flag & VTEXT) {
152         xrele(vp);
153
154         if (vp->v.v_flag & VTEXT) {     /* still has a text object? */
155             ReleaseWriteLock(&afs_ftf);
156             return;
157         }
158     }
159 #endif
160
161     /* next do the stuff that need not check for deadlock problems */
162     mpurge(vp);
163
164     /* finally, record that we've done it */
165     hset(vp->flushDV, fdv);
166     ReleaseWriteLock(&afs_ftf);
167
168 }
169 #endif /* AFS_TEXT_ENV */
170
171 /*
172  * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
173  * This leads to bad races when osi_ReleaseVM() is called from
174  * afs_InvalidateAllSegments().
175
176  * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
177  * vcache entry lock is held or not.
178  *
179  * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
180  * there may be cases where it does care.  If so, it would be good to fix
181  * them so they don't care.  Until then, we assume the worst.
182  *
183  * Locking:  the vcache entry lock is held.  It is dropped and re-obtained.
184  */
185 void
186 osi_ReleaseVM(struct vcache *avc, afs_ucred_t *acred)
187 {
188 #ifdef  AFS_SUN5_ENV
189     AFS_GUNLOCK();
190     osi_VM_Truncate(avc, 0, acred);
191     AFS_GLOCK();
192 #else
193     ReleaseWriteLock(&avc->lock);
194     AFS_GUNLOCK();
195     osi_VM_Truncate(avc, 0, acred);
196     AFS_GLOCK();
197     ObtainWriteLock(&avc->lock, 80);
198 #endif
199 }