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