freebsd-almost-working-client-20020216
[openafs.git] / src / afs / FBSD / osi_vm.c
index 560dfd5..e898014 100644 (file)
@@ -26,7 +26,8 @@ RCSID("$Header$");
 #include "../afs/sysincludes.h"        /* Standard vendor system headers */
 #include "../afs/afsincludes.h"        /* Afs-based standard headers */
 #include "../afs/afs_stats.h"  /* statistics */
-/* #include <vm/vm_ubc.h> */
+#include <vm/vm_object.h>
+#include <vm/vm_map.h>
 #include <limits.h>
 #include <float.h>
 
@@ -51,8 +52,9 @@ osi_VM_FlushVCache(avc, slept)
     struct vcache *avc;
     int *slept;
 {
-#ifdef SECRETLY_OSF1
-    if (avc->vrefCount > 1)
+    struct vm_object *obj;
+    struct vnode *vp;
+    if (VREFCOUNT(avc) > 1)
        return EBUSY;
 
     if (avc->opens)
@@ -63,80 +65,25 @@ osi_VM_FlushVCache(avc, slept)
        return EBUSY;
 
     AFS_GUNLOCK();
-    ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
+    vp=avc;
+    simple_lock(&vp->v_interlock);
+    if (VOP_GETVOBJECT(vp, &obj) == 0) {
+       vm_object_page_remove(obj, 0, 0, FALSE);
+#if 0
+        if (obj->ref_count == 0) {
+           vgonel(vp,curproc);
+           simple_lock(&vp->v_interlock);
+           vp->v_tag=VT_AFS;
+           SetAfsVnode(vp);
+        }
+#endif
+    } 
+    simple_unlock(&vp->v_interlock);
     AFS_GLOCK();
-#endif /* SECRETLY_OSF1 */
 
     return 0;
 }
 
-/*
- * osi_ubc_flush_dirty_and_wait -- ensure all dirty pages cleaned
- *
- * Alpha OSF/1 doesn't make it easy to wait for all dirty pages to be cleaned.
- * NFS tries to do this by calling waitforio(), which waits for v_numoutput
- * to go to zero.  But that isn't good enough, because afs_putpage() doesn't
- * increment v_numoutput until it has obtained the vcache entry lock.  Suppose
- * that Process A, trying to flush a page, is waiting for that lock, and
- * Process B tries to close the file.  Process B calls waitforio() which thinks
- * that everything is cool because v_numoutput is still zero.  Process B then
- * proceeds to call afs_StoreAllSegments().  Finally when B is finished, A gets
- * to proceed and flush its page.  But then it's too late because the file is
- * already closed.
- *
- * (I suspect that waitforio() is not adequate for NFS, just as it isn't
- * adequate for us.  But that's not my problem.)
- *
- * The only way we can be sure that there are no more dirty pages is if there
- * are no more pages with pg_busy set.  We look for them on the cleanpl.
- *
- * For some reason, ubc_flush_dirty() only looks at the dirtypl, not the
- * dirtywpl.  I don't know why this is good enough, but I assume it is.  By
- * the same token, I only look for busy pages on the cleanpl, not the cleanwpl.
- *
- * Called with the global lock NOT held.
- */
-void
-osi_ubc_flush_dirty_and_wait(vp, flags)
-struct vnode *vp;
-int flags; {
-    int retry;
-    vm_page_t pp;
-    int first;
-
-#ifdef SECRETLY_OSF1
-    do {
-       struct vm_ubc_object* vop;
-       vop = (struct vm_ubc_object*)(vp->v_object);
-       ubc_flush_dirty(vop, flags); 
-
-       vm_object_lock(vop);
-       if (vop->vu_dirtypl)
-           /* shouldn't happen, but who knows */
-           retry = 1;
-       else {
-           retry = 0;
-           if (vop->vu_cleanpl) {
-               for (first = 1, pp = vop->vu_cleanpl;
-                    first || pp != vop->vu_cleanpl;
-                    first = 0, pp = pp->pg_onext) {
-                   if (pp->pg_busy) {
-                       retry = 1;
-                       pp->pg_wait = 1;
-                       assert_wait_mesg((vm_offset_t)pp, FALSE, "pg_wait");
-                       vm_object_unlock(vop);
-                       thread_block();
-                       break;
-                   }
-               }
-           }
-           if (retry) continue;
-       }
-       vm_object_unlock(vop);
-    } while (retry);
-#endif /* SECRETLY_OSF1 */
-}
-
 /* Try to store pages to cache, in order to store a file back to the server.
  *
  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
@@ -146,13 +93,32 @@ void
 osi_VM_StoreAllSegments(avc)
     struct vcache *avc;
 {
-#ifdef SECRETLY_OSF1
+    struct vnode *vp;
+    struct vm_object *obj;
+    int anyio,tries;
     ReleaseWriteLock(&avc->lock);
     AFS_GUNLOCK();
-    osi_ubc_flush_dirty_and_wait((struct vnode *)avc, 0);
+    tries=5;
+    vp=avc;
+    do {
+    anyio=0;
+    simple_lock(&vp->v_interlock);
+    if (VOP_GETVOBJECT(vp, &obj) == 0 &&
+       (obj->flags & OBJ_MIGHTBEDIRTY)) {
+        if (!vget(vp,
+            LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
+            if (VOP_GETVOBJECT(vp, &obj) == 0) {
+               vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
+               anyio = 1;
+            }
+            vput(vp);
+        }
+    } else {
+        simple_unlock(&vp->v_interlock);
+    }
+    } while (anyio && (--tries > 0));
     AFS_GLOCK();
     ObtainWriteLock(&avc->lock,94);
-#endif /* SECRETLY_OSF1 */
 }
 
 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
@@ -170,14 +136,38 @@ osi_VM_TryToSmush(avc, acred, sync)
     struct AFS_UCRED *acred;
     int sync;
 {
-#ifdef SECRETLY_OSF1
+    struct vnode *vp;
+    struct vm_object *obj;
+    int anyio, tries;
     ReleaseWriteLock(&avc->lock);
     AFS_GUNLOCK();
-    osi_ubc_flush_dirty_and_wait((struct vnode *)avc, 0);
-    ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
+    tries=5;
+    vp=avc;
+    do {
+       anyio=0;
+       simple_lock(&vp->v_interlock);
+       if (VOP_GETVOBJECT(vp, &obj) == 0 &&
+          (obj->flags & OBJ_MIGHTBEDIRTY)) {
+           if (!vget(vp,
+               LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
+               if (VOP_GETVOBJECT(vp, &obj) == 0) {
+                  vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
+                  anyio = 1;
+               }
+               vput(vp);
+           }
+       } else {
+           simple_unlock(&vp->v_interlock);
+       }
+    } while (anyio && (--tries > 0));
+    simple_lock(&vp->v_interlock);
+    if (VOP_GETVOBJECT(vp, &obj) == 0) {
+       vm_object_page_remove(obj, 0, 0, FALSE);
+    }
+    simple_unlock(&vp->v_interlock);
+    /*vinvalbuf(((struct vnode *)avc),0, NOCRED, curproc, 0,0);*/
     AFS_GLOCK();
     ObtainWriteLock(&avc->lock,59);
-#endif /* SECRETLY_OSF1 */
 }
 
 /* Purge VM for a file when its callback is revoked.
@@ -189,10 +179,17 @@ osi_VM_FlushPages(avc, credp)
     struct vcache *avc;
     struct AFS_UCRED *credp;
 {
-#ifdef SECRETLY_OSF1
-    ubc_flush_dirty(((struct vnode *)avc)->v_object, 0);
-    ubc_invalidate(((struct vnode *)avc)->v_object, 0, 0, B_INVAL);
-#endif /* SECRETLY_OSF1 */
+    struct vnode *vp;
+    struct vm_object *obj;
+    printf("|");
+    vp=avc;
+    simple_lock(&vp->v_interlock);
+    if (VOP_GETVOBJECT(vp, &obj) == 0) {
+       printf("X|");
+       vm_object_page_remove(obj, 0, 0, FALSE);
+    }
+    simple_unlock(&vp->v_interlock);
+    /*vinvalbuf(((struct vnode *)avc),0, NOCRED, curproc, 0,0);*/
 }
 
 /* Purge pages beyond end-of-file, when truncating a file.
@@ -207,8 +204,5 @@ osi_VM_Truncate(avc, alen, acred)
     int alen;
     struct AFS_UCRED *acred;
 {
-#ifdef SECRETLY_OSF1
-    ubc_invalidate(((struct vnode *)avc)->v_object, alen,
-                        MAXINT - alen, B_INVAL);
-#endif /* SECRETLY_OSF1 */
+    vnode_pager_setsize(((struct vnode *)avc), alen);
 }