Limited the scope of some local variables in afs_CacheStoreProc
[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(register struct vcache *avc)
23 {
24     AFS_STATCNT(osi_Active);
25 #if defined(AFS_AIX_ENV) || defined(AFS_OSF_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(register struct vcache *avc, struct AFS_UCRED *credp)
49 {
50 #ifdef AFS_FBSD70_ENV
51     int vfslocked;
52 #endif
53     afs_hyper_t origDV;
54 #if defined(AFS_CACHE_BYPASS)
55     /* The optimization to check DV under read lock below is identical a 
56      * change in CITI cache bypass work.  The problem CITI found in 1999 
57      * was that this code and background daemon doing prefetching competed 
58      * for the vcache entry shared lock.  It's not clear to me from the 
59      * tech report, but it looks like CITI fixed the general prefetch code
60      * path as a bonus when experimenting on prefetch for cache bypass, see
61      * citi-tr-01-3.
62      */
63 #endif        
64     ObtainReadLock(&avc->lock);
65     /* If we've already purged this version, or if we're the ones
66      * writing this version, don't flush it (could lose the
67      * data we're writing). */
68     if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
69         || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
70         ReleaseReadLock(&avc->lock);
71         return;
72     }
73     ReleaseReadLock(&avc->lock);
74     ObtainWriteLock(&avc->lock, 10);
75     /* Check again */
76     if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
77         || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
78         ReleaseWriteLock(&avc->lock);
79         return;
80     }
81     if (hiszero(avc->mapDV)) {
82         hset(avc->mapDV, avc->f.m.DataVersion);
83         ReleaseWriteLock(&avc->lock);
84         return;
85     }
86
87     AFS_STATCNT(osi_FlushPages);
88     hset(origDV, avc->f.m.DataVersion);
89     afs_Trace3(afs_iclSetp, CM_TRACE_FLUSHPAGES, ICL_TYPE_POINTER, avc,
90                ICL_TYPE_INT32, origDV.low, ICL_TYPE_INT32, avc->f.m.Length);
91
92     ReleaseWriteLock(&avc->lock);
93 #ifdef AFS_FBSD70_ENV
94     vfslocked = VFS_LOCK_GIANT(AFSTOV(avc)->v_mount);
95 #endif
96 #ifndef AFS_FBSD70_ENV
97     AFS_GUNLOCK();
98 #endif
99     osi_VM_FlushPages(avc, credp);
100 #ifndef AFS_FBSD70_ENV
101     AFS_GLOCK();
102 #endif
103 #ifdef AFS_FBSD70_ENV
104     VFS_UNLOCK_GIANT(vfslocked);
105 #endif
106     ObtainWriteLock(&avc->lock, 88);
107
108     /* do this last, and to original version, since stores may occur
109      * while executing above PUTPAGE call */
110     hset(avc->mapDV, origDV);
111     ReleaseWriteLock(&avc->lock);
112 }
113
114 #ifdef  AFS_TEXT_ENV
115
116 /* This call is supposed to flush all caches that might be invalidated
117  * by either a local write operation or a write operation done on
118  * another client.  This call may be called repeatedly on the same
119  * version of a file, even while a file is being written, so it
120  * shouldn't do anything that would discard newly written data before
121  * it is written to the file system. */
122
123 void
124 osi_FlushText_really(register struct vcache *vp)
125 {
126     afs_hyper_t fdv;            /* version before which we'll flush */
127
128     AFS_STATCNT(osi_FlushText);
129     /* see if we've already flushed this data version */
130     if (hcmp(vp->f.m.DataVersion, vp->flushDV) <= 0)
131         return;
132
133     MObtainWriteLock(&afs_ftf, 317);
134     hset(fdv, vp->f.m.DataVersion);
135
136     /* why this disgusting code below?
137      *    xuntext, called by xrele, doesn't notice when it is called
138      * with a freed text object.  Sun continually calls xrele or xuntext
139      * without any locking, as long as VTEXT is set on the
140      * corresponding vnode.
141      *    But, if the text object is locked when you check the VTEXT
142      * flag, several processes can wait in xuntext, waiting for the
143      * text lock; when the second one finally enters xuntext's
144      * critical region, the text object is already free, but the check
145      * was already done by xuntext's caller.
146      *    Even worse, it turns out that xalloc locks the text object
147      * before reading or stating a file via the vnode layer.  Thus, we
148      * could end up in getdcache, being asked to bring in a new
149      * version of a file, but the corresponding text object could be
150      * locked.  We can't flush the text object without causing
151      * deadlock, so now we just don't try to lock the text object
152      * unless it is guaranteed to work.  And we try to flush the text
153      * when we need to a bit more often at the vnode layer.  Sun
154      * really blew the vm-cache flushing interface.
155      */
156
157 #if defined (AFS_HPUX_ENV)
158     if (vp->v.v_flag & VTEXT) {
159         xrele(vp);
160
161         if (vp->v.v_flag & VTEXT) {     /* still has a text object? */
162             MReleaseWriteLock(&afs_ftf);
163             return;
164         }
165     }
166 #endif
167
168     /* next do the stuff that need not check for deadlock problems */
169     mpurge(vp);
170
171     /* finally, record that we've done it */
172     hset(vp->flushDV, fdv);
173     MReleaseWriteLock(&afs_ftf);
174
175 }
176 #endif /* AFS_TEXT_ENV */
177
178 /* ? is it moderately likely that there are dirty VM pages associated with
179  * this vnode?
180  *
181  *  Prereqs:  avc must be write-locked
182  *
183  *  System Dependencies:  - *must* support each type of system for which
184  *                          memory mapped files are supported, even if all
185  *                          it does is return TRUE;
186  *
187  * NB:  this routine should err on the side of caution for ProcessFS to work
188  *      correctly (or at least, not to introduce worse bugs than already exist)
189  */
190 #ifdef  notdef
191 int
192 osi_VMDirty_p(struct vcache *avc)
193 {
194     int dirtyPages;
195
196     if (avc->execsOrWriters <= 0)
197         return 0;               /* can't be many dirty pages here, I guess */
198
199 #if defined (AFS_AIX32_ENV)
200 #ifdef  notdef
201     /* because of the level of hardware involvment with VM and all the
202      * warnings about "This routine must be called at VMM interrupt
203      * level", I thought it would be safest to disable interrupts while
204      * looking at the software page fault table.  */
205
206     /* convert vm handle into index into array:  I think that stoinio is
207      * always zero...  Look into this XXX  */
208 #define VMHASH(handle) ( \
209                         ( ((handle) & ~vmker.stoinio)  \
210                          ^ ((((handle) & ~vmker.stoinio) & vmker.stoimask) << vmker.stoihash) \
211                          ) & 0x000fffff)
212
213     if (avc->segid) {
214         unsigned int pagef, pri, index, next;
215
216         index = VMHASH(avc->segid);
217         if (scb_valid(index)) { /* could almost be an ASSERT */
218
219             pri = disable_ints();
220             for (pagef = scb_sidlist(index); pagef >= 0; pagef = next) {
221                 next = pft_sidfwd(pagef);
222                 if (pft_modbit(pagef)) {        /* has page frame been modified? */
223                     enable_ints(pri);
224                     return 1;
225                 }
226             }
227             enable_ints(pri);
228         }
229     }
230 #undef VMHASH
231 #endif
232 #endif /* AFS_AIX32_ENV */
233
234 #if defined (AFS_SUN5_ENV)
235     if (avc->f.states & CMAPPED) {
236         struct page *pg;
237         for (pg = avc->v.v_s.v_Pages; pg; pg = pg->p_vpnext) {
238             if (pg->p_mod) {
239                 return 1;
240             }
241         }
242     }
243 #endif
244     return 0;
245 }
246 #endif /* notdef */
247
248
249 /*
250  * Solaris osi_ReleaseVM should not drop and re-obtain the vcache entry lock.
251  * This leads to bad races when osi_ReleaseVM() is called from
252  * afs_InvalidateAllSegments().
253
254  * We can do this because Solaris osi_VM_Truncate() doesn't care whether the
255  * vcache entry lock is held or not.
256  *
257  * For other platforms, in some cases osi_VM_Truncate() doesn't care, but
258  * there may be cases where it does care.  If so, it would be good to fix
259  * them so they don't care.  Until then, we assume the worst.
260  *
261  * Locking:  the vcache entry lock is held.  It is dropped and re-obtained.
262  */
263 void
264 osi_ReleaseVM(struct vcache *avc, struct AFS_UCRED *acred)
265 {
266 #ifdef  AFS_SUN5_ENV
267     AFS_GUNLOCK();
268     osi_VM_Truncate(avc, 0, acred);
269     AFS_GLOCK();
270 #else
271     ReleaseWriteLock(&avc->lock);
272     AFS_GUNLOCK();
273     osi_VM_Truncate(avc, 0, acred);
274     AFS_GLOCK();
275     ObtainWriteLock(&avc->lock, 80);
276 #endif
277 }