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