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
11 /* osi_vm.c implements:
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)
21 #include <afsconfig.h>
22 #include "afs/param.h"
24 #include <sys/param.h>
25 #include <sys/vnode.h>
27 vgonel(struct vnode *vp, struct thread *td);
31 #include "afs/sysincludes.h" /* Standard vendor system headers */
32 #include "afsincludes.h" /* Afs-based standard headers */
33 #include "afs/afs_stats.h" /* statistics */
34 #include <vm/vm_object.h>
35 #include <vm/vm_map.h>
40 * FreeBSD implementation notes:
41 * Most of these operations require us to frob vm_objects. Most
42 * functions require that the object be locked (with VM_OBJECT_LOCK)
43 * on entry and leave it locked on exit. In order to get the
44 * vm_object itself we call VOP_GETVOBJECT on the vnode; the
45 * locking protocol requires that we do so with the heavy vnode lock
46 * held and the vnode interlock unlocked, and it returns the same
49 * The locking protocol for vnodes is defined in
50 * kern/vnode_if.src and sys/vnode.h; the locking is still a work in
51 * progress, so some fields are (as of 5.1) still protected by Giant
52 * rather than an explicit lock.
56 #define VOP_GETVOBJECT(vp, objp) (*(objp) = (vp)->v_object)
59 #if defined(AFS_FBSD80_ENV)
60 #define lock_vnode(v) vn_lock((v), LK_EXCLUSIVE | LK_RETRY)
61 #define unlock_vnode(v) VOP_UNLOCK((v), 0)
63 #define lock_vnode(v) vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curthread)
64 #define unlock_vnode(v) VOP_UNLOCK((v), 0, curthread)
67 /* Try to discard pages, in order to recycle a vcache entry.
69 * We also make some sanity checks: ref count, open count, held locks.
71 * We also do some non-VM-related chores, such as releasing the cred pointer
72 * (for AIX and Solaris) and releasing the gnode (for AIX).
74 * Locking: afs_xvcache lock is held. If it is dropped and re-acquired,
75 * *slept should be set to warn the caller.
77 * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
78 * is not dropped and re-acquired for any platform. It may be that *slept is
79 * therefore obsolescent.
81 * OSF/1 Locking: VN_LOCK has been called.
82 * We do not lock the vnode here, but instead require that it be exclusive
83 * locked by code calling osi_VM_StoreAllSegments directly, or scheduling it
84 * from the bqueue - Matt
85 * Maybe better to just call vnode_pager_setsize()?
88 osi_VM_FlushVCache(struct vcache *avc, int *slept)
90 struct vm_object *obj;
92 if (VREFCOUNT(avc) > 1)
98 /* if a lock is held, give up */
99 if (CheckLock(&avc->lock))
106 #ifndef AFS_FBSD70_ENV
109 if (VOP_GETVOBJECT(vp, &obj) == 0) {
111 vm_object_page_remove(obj, 0, 0, FALSE);
113 if (obj->ref_count == 0) {
114 simple_lock(&vp->v_interlock);
115 vgonel(vp, curthread);
120 VM_OBJECT_UNLOCK(obj);
122 #ifndef AFS_FBSD70_ENV
130 /* Try to store pages to cache, in order to store a file back to the server.
132 * Locking: the vcache entry's lock is held. It will usually be dropped and
136 osi_VM_StoreAllSegments(struct vcache *avc)
139 struct vm_object *obj;
142 ReleaseWriteLock(&avc->lock);
148 * I don't understand this. Why not just call vm_object_page_clean()
149 * and be done with it? I particularly don't understand why we're calling
150 * vget() here. Is there some reason to believe that the vnode might
151 * be being recycled at this point? I don't think there's any need for
152 * this loop, either -- if we keep the vnode locked all the time,
153 * that and the object lock will prevent any new pages from appearing.
154 * The loop is what causes the race condition. -GAW
158 if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
159 if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
160 if (VOP_GETVOBJECT(vp, &obj) == 0) {
162 vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
163 VM_OBJECT_UNLOCK(obj);
169 } while (anyio && (--tries > 0));
171 ObtainWriteLock(&avc->lock, 94);
174 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
175 * try to free pages, when deleting a file.
177 * Locking: the vcache entry's lock is held. It may be dropped and
180 * Since we drop and re-obtain the lock, we can't guarantee that there won't
181 * be some pages around when we return, newly created by concurrent activity.
184 osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
195 if (vp->v_iflag & VI_DOOMED) {
202 islocked = VOP_ISLOCKED(vp);
203 if (islocked == LK_EXCLOTHER)
204 panic("Trying to Smush over someone else's lock");
205 else if (islocked == LK_SHARED) {
206 afs_warn("Trying to Smush with a shared lock");
207 vn_lock(vp, LK_UPGRADE);
208 } else if (!islocked)
209 vn_lock(vp, LK_EXCLUSIVE);
211 if (vp->v_bufobj.bo_object != NULL) {
212 VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
214 * Do we really want OBJPC_SYNC? OBJPC_INVAL would be
215 * faster, if invalidation is really what we are being
216 * asked to do. (It would make more sense, too, since
217 * otherwise this function is practically identical to
218 * osi_VM_StoreAllSegments().) -GAW
222 * Dunno. We no longer resemble osi_VM_StoreAllSegments,
223 * though maybe that's wrong, now. And OBJPC_SYNC is the
224 * common thing in 70 file systems, it seems. Matt.
227 vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
228 VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
232 code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
233 while (code && (tries > 0)) {
234 afs_warn("TryToSmush retrying vinvalbuf");
235 code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
238 if (islocked == LK_SHARED)
239 vn_lock(vp, LK_DOWNGRADE);
245 /* Purge VM for a file when its callback is revoked.
247 * Locking: No lock is held, not even the global lock.
250 osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
253 struct vm_object *obj;
256 ASSERT_VOP_LOCKED(vp, __func__);
257 if (VOP_GETVOBJECT(vp, &obj) == 0) {
259 vm_object_page_remove(obj, 0, 0, FALSE);
260 VM_OBJECT_UNLOCK(obj);
262 osi_vinvalbuf(vp, 0, 0, 0);
265 /* Purge pages beyond end-of-file, when truncating a file.
267 * Locking: no lock is held, not even the global lock.
268 * activeV is raised. This is supposed to block pageins, but at present
269 * it only works on Solaris.
272 osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
274 vnode_pager_setsize(AFSTOV(avc), alen);