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