2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h" /* statistics */
21 /* Try to discard pages, in order to recycle a vcache entry.
23 * We also make some sanity checks: ref count, open count, held locks.
25 * We also do some non-VM-related chores, such as releasing the cred pointer
26 * (for AIX and Solaris) and releasing the gnode (for AIX).
28 * Locking: afs_xvcache lock is held. If it is dropped and re-acquired,
29 * *slept should be set to warn the caller.
31 * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
32 * is not dropped and re-acquired for any platform. It may be that *slept is
33 * therefore obsolescent.
35 * OSF/1 Locking: VN_LOCK has been called.
38 osi_VM_FlushVCache(struct vcache *avc, int *slept)
40 struct vnode *vp = AFSTOV(avc);
41 #ifdef AFS_DARWIN14_ENV
42 if (UBCINFOEXISTS(vp))
51 /* if a lock is held, give up */
52 if (CheckLock(&avc->lock) || afs_CheckBozonLock(&avc->pvnLock))
57 #ifndef AFS_DARWIN14_ENV
58 if (UBCINFOEXISTS(vp)) {
72 /* Try to store pages to cache, in order to store a file back to the server.
74 * Locking: the vcache entry's lock is held. It will usually be dropped and
78 osi_VM_StoreAllSegments(struct vcache *avc)
80 struct vnode *vp = AFSTOV(avc);
81 ReleaseWriteLock(&avc->lock);
83 if (UBCINFOEXISTS(vp)) {
87 ObtainWriteLock(&avc->lock, 94);
90 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
91 * try to free pages, when deleting a file.
93 * Locking: the vcache entry's lock is held. It may be dropped and
96 * Since we drop and re-obtain the lock, we can't guarantee that there won't
97 * be some pages around when we return, newly created by concurrent activity.
100 osi_VM_TryToSmush(struct vcache *avc, struct AFS_UCRED *acred, int sync)
102 struct vnode *vp = AFSTOV(avc);
107 ReleaseWriteLock(&avc->lock);
109 if (UBCINFOEXISTS(vp)) {
110 size = ubc_getsize(vp);
111 kret = ubc_invalidate(vp, 0, size);
112 if (kret != 1) /* should be KERN_SUCCESS */
113 printf("TryToSmush: invalidate failed (error = %d)\n", kret);
116 ObtainWriteLock(&avc->lock, 59);
119 /* Purge VM for a file when its callback is revoked.
121 * Locking: No lock is held, not even the global lock.
123 /* XXX this seems to not be referenced anywhere. *somebody* ought to be calling
124 this, and also making sure that ubc's idea of the filesize is right more
127 osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
129 struct vnode *vp = AFSTOV(avc);
133 if (UBCINFOEXISTS(vp)) {
134 size = ubc_getsize(vp);
135 kret = ubc_invalidate(vp, 0, size);
136 if (kret != 1) /* Should be KERN_SUCCESS */
137 printf("VMFlushPages: invalidate failed (error = %d)\n", kret);
138 /* XXX what about when not CStatd */
139 if (avc->states & CStatd && size != avc->m.Length)
140 ubc_setsize(vp, avc->m.Length);
144 /* Purge pages beyond end-of-file, when truncating a file.
146 * Locking: no lock is held, not even the global lock.
147 * activeV is raised. This is supposed to block pageins, but at present
148 * it only works on Solaris.
151 osi_VM_Truncate(struct vcache *avc, int alen, struct AFS_UCRED *acred)
153 struct vnode *vp = AFSTOV(avc);
154 if (UBCINFOEXISTS(vp)) {
155 ubc_setsize(vp, alen);
159 /* vnreclaim and vinactive are probably not aggressive enough to keep
160 enough afs vcaches free, so we try to do some of it ourselves */
161 /* XXX there's probably not nearly enough locking here */
163 osi_VM_TryReclaim(struct vcache *avc, int *slept)
165 struct proc *p = current_proc();
166 struct vnode *vp = AFSTOV(avc);
171 VN_HOLD(vp); /* remove from inactive list */
172 if (!simple_lock_try(&vp->v_interlock)) {
176 if (!UBCINFOEXISTS(vp) || vp->v_count != 2) {
177 simple_unlock(&vp->v_interlock);
181 #ifdef AFS_DARWIN14_ENV
182 if (vp->v_ubcinfo->ui_refcount > 1 || vp->v_ubcinfo->ui_mapped) {
183 simple_unlock(&vp->v_interlock);
188 if (vp->v_ubcinfo->ui_holdcnt) {
189 simple_unlock(&vp->v_interlock);
194 if (slept && ubc_issetflags(vp, UI_WASMAPPED)) {
195 /* We can't possibly release this in time for this NewVCache to get it */
196 simple_unlock(&vp->v_interlock);
201 vp->v_usecount--; /* we want the usecount to be 1 */
204 ReleaseWriteLock(&afs_xvcache);
207 ReleaseReadLock(&afs_xvcache);
210 if (ubc_issetflags(vp, UI_WASMAPPED)) {
211 simple_unlock(&vp->v_interlock);
212 #ifdef AFS_DARWIN14_ENV
213 ubc_release_named(vp);
217 if (ubc_issetflags(vp, UI_HASOBJREF))
218 printf("ubc_release didn't release the reference?!\n");
219 } else if (!vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, current_proc())) {
220 #ifdef AFS_DARWIN14_ENV
221 obj = ubc_getobject(vp, UBC_HOLDOBJECT);
223 #ifdef AFS_DARWIN13_ENV
224 obj = ubc_getobject(vp, (UBC_NOREACTIVATE | UBC_HOLDOBJECT));
226 obj = ubc_getobject(vp);
229 (void)ubc_clean(vp, 1);
230 vinvalbuf(vp, V_SAVE, &afs_osi_cred, p, 0, 0);
231 if (vp->v_usecount == 1)
234 VOP_UNLOCK(vp, 0, p);
236 if (ISSET(vp->v_flag, VTERMINATE))
237 panic("afs_vnreclaim: already teminating");
238 SET(vp->v_flag, VTERMINATE);
239 memory_object_destroy(obj, 0);
240 while (ISSET(vp->v_flag, VTERMINATE)) {
241 SET(vp->v_flag, VTERMWANT);
242 tsleep((caddr_t) & vp->v_ubcinfo, PINOD, "afs_vnreclaim", 0);
246 if (simple_lock_try(&vp->v_interlock))
247 panic("afs_vnreclaim: slept, but did no work :(");
248 if (UBCINFOEXISTS(vp) && vp->v_count == 1) {
250 simple_unlock(&vp->v_interlock);
253 simple_unlock(&vp->v_interlock);
257 ObtainWriteLock(&afs_xvcache, 175);
259 ObtainReadLock(&afs_xvcache);
263 osi_VM_NukePages(struct vnode *vp, off_t offset, off_t size)
266 struct vcache *avc = VTOAFS(vp);
268 #ifdef AFS_DARWIN14_ENV
269 offset = trunc_page(offset);
270 size = round_page(size + 1);
272 ubc_page_op(vp, (vm_offset_t) offset,
273 UPL_POP_SET | UPL_POP_BUSY | UPL_POP_DUMP, 0, 0);
279 #ifdef AFS_DARWIN13_ENV
280 if (UBCINFOEXISTS(vp))
281 object = ubc_getobject(vp, UBC_NOREACTIVATE);
283 if (UBCINFOEXISTS(vp))
284 object = ubc_getobject(vp);
289 offset = trunc_page(offset);
290 size = round_page(size + 1);
292 #ifdef AFS_DARWIN13_ENV
294 memory_object_page_op(object, (vm_offset_t) offset,
295 UPL_POP_SET | UPL_POP_BUSY | UPL_POP_DUMP, 0,
301 /* This is all we can do, and it's not enough. sucks to be us */
302 ubc_setsize(vp, offset);
303 size = (offset + size > avc->m.Length) ? offset + size : avc->m.Length;
304 ubc_setsize(vp, size);
310 osi_VM_Setup(struct vcache *avc, int force)
313 struct vnode *vp = AFSTOV(avc);
315 if (UBCISVALID(vp) && ((avc->states & CStatd) || force)) {
316 if (!UBCINFOEXISTS(vp) && !ISSET(vp->v_flag, VTERMINATE)) {
318 avc->states |= CUBCinit;
320 if ((error = ubc_info_init(&avc->v))) {
322 avc->states &= ~CUBCinit;
326 #ifndef AFS_DARWIN14_ENV
327 simple_lock(&avc->v.v_interlock);
328 if (!ubc_issetflags(&avc->v, UI_HASOBJREF))
329 #ifdef AFS_DARWIN13_ENV
331 (&avc->v, (UBC_NOREACTIVATE | UBC_HOLDOBJECT)))
332 panic("VM_Setup: null object");
334 (void)_ubc_getobject(&avc->v, 1); /* return value not used */
336 simple_unlock(&avc->v.v_interlock);
339 avc->states &= ~CUBCinit;
342 if (UBCINFOEXISTS(&avc->v))
343 ubc_setsize(&avc->v, avc->m.Length);