FBSD CM: don't call afs_close when recycling
[openafs.git] / src / afs / FBSD / osi_vm.c
index ff2b5d2..6f49d7d 100644 (file)
  */
 
 #include <afsconfig.h>
-#include "../afs/param.h"
+#include "afs/param.h"
+#ifdef AFS_FBSD70_ENV
+#include <sys/param.h>
+#include <sys/vnode.h>
+     void
+     vgonel(struct vnode *vp, struct thread *td);
+#endif
 
-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 "afs/sysincludes.h"   /* Standard vendor system headers */
+#include "afsincludes.h"       /* Afs-based standard headers */
+#include "afs/afs_stats.h"     /* statistics */
 #include <vm/vm_object.h>
 #include <vm/vm_map.h>
 #include <limits.h>
 #include <float.h>
 
+/*
+ * FreeBSD implementation notes:
+ * Most of these operations require us to frob vm_objects.  Most
+ * functions require that the object be locked (with VM_OBJECT_LOCK)
+ * on entry and leave it locked on exit.  In order to get the
+ * vm_object itself we call VOP_GETVOBJECT on the vnode; the
+ * locking protocol requires that we do so with the heavy vnode lock
+ * held and the vnode interlock unlocked, and it returns the same
+ * way.
+ *
+ * The locking protocol for vnodes is defined in
+ * kern/vnode_if.src and sys/vnode.h; the locking is still a work in 
+ * progress, so some fields are (as of 5.1) still protected by Giant
+ * rather than an explicit lock.
+ */
+
+#ifdef AFS_FBSD60_ENV
+#define VOP_GETVOBJECT(vp, objp) (*(objp) = (vp)->v_object)
+#endif
+
+#if defined(AFS_FBSD80_ENV)
+#define        lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY)
+#define unlock_vnode(v)        VOP_UNLOCK((v), 0)
+#else
+#define        lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curthread)
+#define unlock_vnode(v)        VOP_UNLOCK((v), 0, curthread)
+#endif
+
 /* Try to discard pages, in order to recycle a vcache entry.
  *
  * We also make some sanity checks:  ref count, open count, held locks.
@@ -46,36 +79,56 @@ RCSID("$Header$");
  * therefore obsolescent.
  *
  * OSF/1 Locking:  VN_LOCK has been called.
+ * We do not lock the vnode here, but instead require that it be exclusive
+ * locked by code calling osi_VM_StoreAllSegments directly, or scheduling it
+ * from the bqueue - Matt
+ * Maybe better to just call vnode_pager_setsize()?
  */
-int osi_VM_FlushVCache(struct vcache *avc, int *slept)
+int
+osi_VM_FlushVCache(struct vcache *avc, int *slept)
 {
     struct vm_object *obj;
     struct vnode *vp;
-    if (VREFCOUNT(avc) > 1)
+    if (VREFCOUNT(avc) > 1) {
        return EBUSY;
+    }
 
-    if (avc->opens)
+    /* XXX
+     * The value of avc->opens here came to be, at some point,
+     * typically -1.  This was caused by incorrectly performing afs_close
+     * processing on vnodes being recycled */
+    if (avc->opens) {
        return EBUSY;
+    }
 
     /* if a lock is held, give up */
-    if (CheckLock(&avc->lock) || afs_CheckBozonLock(&avc->pvnLock))
+    if (CheckLock(&avc->lock)) {
        return EBUSY;
+    }
+
+    return(0);
 
     AFS_GUNLOCK();
-    vp=avc;
-    simple_lock(&vp->v_interlock);
+    vp = AFSTOV(avc);
+#ifndef AFS_FBSD70_ENV
+    lock_vnode(vp);
+#endif
     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);
-        }
+       VM_OBJECT_LOCK(obj);
+       vm_object_page_remove(obj, 0, 0, FALSE);
+#if 1
+       if (obj->ref_count == 0) {
+           simple_lock(&vp->v_interlock);
+           vgonel(vp, curthread);
+           vp->v_tag = VT_AFS;
+           SetAfsVnode(vp);
+       }
+#endif
+       VM_OBJECT_UNLOCK(obj);
+    }
+#ifndef AFS_FBSD70_ENV
+    unlock_vnode(vp);
 #endif
-    } 
-    simple_unlock(&vp->v_interlock);
     AFS_GLOCK();
 
     return 0;
@@ -86,34 +139,43 @@ int osi_VM_FlushVCache(struct vcache *avc, int *slept)
  * Locking:  the vcache entry's lock is held.  It will usually be dropped and
  * re-obtained.
  */
-void osi_VM_StoreAllSegments(struct vcache *avc)
+void
+osi_VM_StoreAllSegments(struct vcache *avc)
 {
     struct vnode *vp;
     struct vm_object *obj;
-    int anyio,tries;
+    int anyio, tries;
+
     ReleaseWriteLock(&avc->lock);
     AFS_GUNLOCK();
-    tries=5;
-    vp=avc;
+    tries = 5;
+    vp = AFSTOV(avc);
+
+    /*
+     * I don't understand this.  Why not just call vm_object_page_clean()
+     * and be done with it?  I particularly don't understand why we're calling
+     * vget() here.  Is there some reason to believe that the vnode might
+     * be being recycled at this point?  I don't think there's any need for
+     * this loop, either -- if we keep the vnode locked all the time,
+     * that and the object lock will prevent any new pages from appearing.
+     * The loop is what causes the race condition.  -GAW
+     */
     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);
-    }
+       anyio = 0;
+       if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
+           if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
+                   if (VOP_GETVOBJECT(vp, &obj) == 0) {
+                       VM_OBJECT_LOCK(obj);
+                       vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
+                       VM_OBJECT_UNLOCK(obj);
+                       anyio = 1;
+                   }
+                   vput(vp);
+               }
+           }
     } while (anyio && (--tries > 0));
     AFS_GLOCK();
-    ObtainWriteLock(&avc->lock,94);
+    ObtainWriteLock(&avc->lock, 94);
 }
 
 /* Try to invalidate pages, for "fs flush" or "fs flushv"; or
@@ -125,57 +187,86 @@ void osi_VM_StoreAllSegments(struct vcache *avc)
  * Since we drop and re-obtain the lock, we can't guarantee that there won't
  * be some pages around when we return, newly created by concurrent activity.
  */
-void osi_VM_TryToSmush(struct vcache *avc, struct AFS_UCRED *acred, int sync)
+void
+osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
 {
     struct vnode *vp;
-    struct vm_object *obj;
-    int anyio, tries;
-    ReleaseWriteLock(&avc->lock);
-    AFS_GUNLOCK();
-    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);
+    int tries, code;
+    int islocked;
+
+    SPLVAR;
+
+    vp = AFSTOV(avc);
+
+    VI_LOCK(vp);
+    if (vp->v_iflag & VI_DOOMED) {
+       VI_UNLOCK(vp);
+       USERPRI;
+       return;
     }
-    simple_unlock(&vp->v_interlock);
-    /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0);*/
-    AFS_GLOCK();
-    ObtainWriteLock(&avc->lock,59);
+    VI_UNLOCK(vp);
+
+    islocked = VOP_ISLOCKED(vp);
+    if (islocked == LK_EXCLOTHER)
+       panic("Trying to Smush over someone else's lock");
+    else if (islocked == LK_SHARED) {
+       afs_warn("Trying to Smush with a shared lock");
+       vn_lock(vp, LK_UPGRADE);
+    } else if (!islocked)
+       vn_lock(vp, LK_EXCLUSIVE);
+
+    if (vp->v_bufobj.bo_object != NULL) {
+       VM_OBJECT_LOCK(vp->v_bufobj.bo_object);
+       /*
+        * Do we really want OBJPC_SYNC?  OBJPC_INVAL would be
+        * faster, if invalidation is really what we are being
+        * asked to do.  (It would make more sense, too, since
+        * otherwise this function is practically identical to
+        * osi_VM_StoreAllSegments().)  -GAW
+        */
+
+       /*
+        * Dunno.  We no longer resemble osi_VM_StoreAllSegments,
+        * though maybe that's wrong, now.  And OBJPC_SYNC is the
+        * common thing in 70 file systems, it seems.  Matt.
+        */
+
+       vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
+       VM_OBJECT_UNLOCK(vp->v_bufobj.bo_object);
+    }
+
+    tries = 5;
+    code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
+    while (code && (tries > 0)) {
+       afs_warn("TryToSmush retrying vinvalbuf");
+       code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
+       --tries;
+    }
+    if (islocked == LK_SHARED)
+       vn_lock(vp, LK_DOWNGRADE);
+    else if (!islocked)
+       VOP_UNLOCK(vp, 0);
+    USERPRI;
 }
 
 /* Purge VM for a file when its callback is revoked.
  *
  * Locking:  No lock is held, not even the global lock.
  */
-void osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
+void
+osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
 {
     struct vnode *vp;
     struct vm_object *obj;
-    vp=avc;
-    simple_lock(&vp->v_interlock);
+
+    vp = AFSTOV(avc);
+    ASSERT_VOP_LOCKED(vp, __func__);
     if (VOP_GETVOBJECT(vp, &obj) == 0) {
-       vm_object_page_remove(obj, 0, 0, FALSE);
+       VM_OBJECT_LOCK(obj);
+       vm_object_page_remove(obj, 0, 0, FALSE);
+       VM_OBJECT_UNLOCK(obj);
     }
-    simple_unlock(&vp->v_interlock);
-    /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0);*/
+    osi_vinvalbuf(vp, 0, 0, 0);
 }
 
 /* Purge pages beyond end-of-file, when truncating a file.
@@ -184,7 +275,8 @@ void osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
  * activeV is raised.  This is supposed to block pageins, but at present
  * it only works on Solaris.
  */
-void osi_VM_Truncate(struct vcache *avc, int alen, struct AFS_UCRED *acred)
+void
+osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
 {
     vnode_pager_setsize(AFSTOV(avc), alen);
 }