include-afsconfig-before-param-h-20010712
[openafs.git] / src / afs / FBSD / 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
11 /* osi_vm.c implements:
12  *
13  * osi_VM_FlushVCache(avc, slept)
14  * osi_ubc_flush_dirty_and_wait(vp, flags)
15  * osi_VM_StoreAllSegments(avc)
16  * osi_VM_TryToSmush(avc, acred, sync)
17  * osi_VM_FlushPages(avc, credp)
18  * osi_VM_Truncate(avc, alen, acred)
19  */
20
21 #include <afsconfig.h>
22 #include "../afs/param.h"
23
24 RCSID("$Header$");
25
26 #include "../afs/sysincludes.h" /* Standard vendor system headers */
27 #include "../afs/afsincludes.h" /* Afs-based standard headers */
28 #include "../afs/afs_stats.h"  /* statistics */
29 /* #include <vm/vm_ubc.h> */
30 #include <limits.h>
31 #include <float.h>
32
33 /* Try to discard pages, in order to recycle a vcache entry.
34  *
35  * We also make some sanity checks:  ref count, open count, held locks.
36  *
37  * We also do some non-VM-related chores, such as releasing the cred pointer
38  * (for AIX and Solaris) and releasing the gnode (for AIX).
39  *
40  * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
41  *   *slept should be set to warn the caller.
42  *
43  * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
44  * is not dropped and re-acquired for any platform.  It may be that *slept is
45  * therefore obsolescent.
46  *
47  * OSF/1 Locking:  VN_LOCK has been called.
48  */
49 int
50 osi_VM_FlushVCache(avc, slept)
51     struct vcache *avc;
52     int *slept;
53 {
54 #ifdef SECRETLY_OSF1
55     if (avc->vrefCount > 1)
56         return EBUSY;
57
58     if (avc->opens)
59         return EBUSY;
60
61     /* if a lock is held, give up */
62     if (CheckLock(&avc->lock) || afs_CheckBozonLock(&avc->pvnLock))
63         return EBUSY;
64
65     AFS_GUNLOCK();
66     ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
67     AFS_GLOCK();
68 #endif /* SECRETLY_OSF1 */
69
70     return 0;
71 }
72
73 /*
74  * osi_ubc_flush_dirty_and_wait -- ensure all dirty pages cleaned
75  *
76  * Alpha OSF/1 doesn't make it easy to wait for all dirty pages to be cleaned.
77  * NFS tries to do this by calling waitforio(), which waits for v_numoutput
78  * to go to zero.  But that isn't good enough, because afs_putpage() doesn't
79  * increment v_numoutput until it has obtained the vcache entry lock.  Suppose
80  * that Process A, trying to flush a page, is waiting for that lock, and
81  * Process B tries to close the file.  Process B calls waitforio() which thinks
82  * that everything is cool because v_numoutput is still zero.  Process B then
83  * proceeds to call afs_StoreAllSegments().  Finally when B is finished, A gets
84  * to proceed and flush its page.  But then it's too late because the file is
85  * already closed.
86  *
87  * (I suspect that waitforio() is not adequate for NFS, just as it isn't
88  * adequate for us.  But that's not my problem.)
89  *
90  * The only way we can be sure that there are no more dirty pages is if there
91  * are no more pages with pg_busy set.  We look for them on the cleanpl.
92  *
93  * For some reason, ubc_flush_dirty() only looks at the dirtypl, not the
94  * dirtywpl.  I don't know why this is good enough, but I assume it is.  By
95  * the same token, I only look for busy pages on the cleanpl, not the cleanwpl.
96  *
97  * Called with the global lock NOT held.
98  */
99 void
100 osi_ubc_flush_dirty_and_wait(vp, flags)
101 struct vnode *vp;
102 int flags; {
103     int retry;
104     vm_page_t pp;
105     int first;
106
107 #ifdef SECRETLY_OSF1
108     do {
109         struct vm_ubc_object* vop;
110         vop = (struct vm_ubc_object*)(vp->v_object);
111         ubc_flush_dirty(vop, flags); 
112
113         vm_object_lock(vop);
114         if (vop->vu_dirtypl)
115             /* shouldn't happen, but who knows */
116             retry = 1;
117         else {
118             retry = 0;
119             if (vop->vu_cleanpl) {
120                 for (first = 1, pp = vop->vu_cleanpl;
121                      first || pp != vop->vu_cleanpl;
122                      first = 0, pp = pp->pg_onext) {
123                     if (pp->pg_busy) {
124                         retry = 1;
125                         pp->pg_wait = 1;
126                         assert_wait_mesg((vm_offset_t)pp, FALSE, "pg_wait");
127                         vm_object_unlock(vop);
128                         thread_block();
129                         break;
130                     }
131                 }
132             }
133             if (retry) continue;
134         }
135         vm_object_unlock(vop);
136     } while (retry);
137 #endif /* SECRETLY_OSF1 */
138 }
139
140 /* Try to store pages to cache, in order to store a file back to the server.
141  *
142  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
143  * re-obtained.
144  */
145 void
146 osi_VM_StoreAllSegments(avc)
147     struct vcache *avc;
148 {
149 #ifdef SECRETLY_OSF1
150     ReleaseWriteLock(&avc->lock);
151     AFS_GUNLOCK();
152     osi_ubc_flush_dirty_and_wait((struct vnode *)avc, 0);
153     AFS_GLOCK();
154     ObtainWriteLock(&avc->lock,94);
155 #endif /* SECRETLY_OSF1 */
156 }
157
158 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
159  * try to free pages, when deleting a file.
160  *
161  * Locking:  the vcache entry's lock is held.  It may be dropped and 
162  * re-obtained.
163  *
164  * Since we drop and re-obtain the lock, we can't guarantee that there won't
165  * be some pages around when we return, newly created by concurrent activity.
166  */
167 void
168 osi_VM_TryToSmush(avc, acred, sync)
169     struct vcache *avc;
170     struct AFS_UCRED *acred;
171     int sync;
172 {
173 #ifdef SECRETLY_OSF1
174     ReleaseWriteLock(&avc->lock);
175     AFS_GUNLOCK();
176     osi_ubc_flush_dirty_and_wait((struct vnode *)avc, 0);
177     ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
178     AFS_GLOCK();
179     ObtainWriteLock(&avc->lock,59);
180 #endif /* SECRETLY_OSF1 */
181 }
182
183 /* Purge VM for a file when its callback is revoked.
184  *
185  * Locking:  No lock is held, not even the global lock.
186  */
187 void
188 osi_VM_FlushPages(avc, credp)
189     struct vcache *avc;
190     struct AFS_UCRED *credp;
191 {
192 #ifdef SECRETLY_OSF1
193     ubc_flush_dirty(((struct vnode *)avc)->v_object, 0);
194     ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
195 #endif /* SECRETLY_OSF1 */
196 }
197
198 /* Purge pages beyond end-of-file, when truncating a file.
199  *
200  * Locking:  no lock is held, not even the global lock.
201  * activeV is raised.  This is supposed to block pageins, but at present
202  * it only works on Solaris.
203  */
204 void
205 osi_VM_Truncate(avc, alen, acred)
206     struct vcache *avc;
207     int alen;
208     struct AFS_UCRED *acred;
209 {
210 #ifdef SECRETLY_OSF1
211     ubc_invalidate(((struct vnode *)avc)->v_object, alen,
212                         MAXINT - alen, B_INVAL);
213 #endif /* SECRETLY_OSF1 */
214 }