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