afs: Do not supply bogus poll vnodeops for FBSD
[openafs.git] / src / afs / FBSD / osi_vm.c
index 5103891..d475d91 100644 (file)
@@ -10,7 +10,7 @@
 
 /* osi_vm.c implements:
  *
- * osi_VM_FlushVCache(avc, slept)
+ * osi_VM_FlushVCache(avc)
  * osi_ubc_flush_dirty_and_wait(vp, flags)
  * osi_VM_StoreAllSegments(avc)
  * osi_VM_TryToSmush(avc, acred, sync)
 
 #include <afsconfig.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 "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>
+#include <sys/limits.h>
+#if __FreeBSD_version >= 1000030
+#include <sys/rwlock.h>
+#endif
 
 /*
  * 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.
+ * functions require that the object be locked (with VM_OBJECT_*LOCK)
+ * on entry and leave it locked on exit.  The locking protocol
+ * requires that we access vp->v_object with the heavy vnode lock
+ * held and the vnode interlock unlocked.
  *
  * 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.
+ * kern/vnode_if.src and sys/vnode.h; unfortunately, it is not *quite*
+ * constant from version to version so to be properly correct we must
+ * check the VCS history of those files.
  */
 
-#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        lock_vnode(v, f)        vn_lock((v), (f))
+#define ilock_vnode(v) vn_lock((v), LK_INTERLOCK|LK_EXCLUSIVE|LK_RETRY)
 #define unlock_vnode(v)        VOP_UNLOCK((v), 0)
-#elif defined(AFS_FBSD50_ENV)
-#define        lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curthread)
+#define islocked_vnode(v)      VOP_ISLOCKED((v))
+#else
+#define        lock_vnode(v, f)        vn_lock((v), (f), curthread)
+#define ilock_vnode(v) vn_lock((v), LK_INTERLOCK|LK_EXCLUSIVE|LK_RETRY, curthread)
 #define unlock_vnode(v)        VOP_UNLOCK((v), 0, curthread)
+#define islocked_vnode(v)      VOP_ISLOCKED((v), curthread)
+#endif
+
+#if __FreeBSD_version >= 1000030
+#define AFS_VM_OBJECT_WLOCK(o) VM_OBJECT_WLOCK(o)
+#define AFS_VM_OBJECT_WUNLOCK(o)       VM_OBJECT_WUNLOCK(o)
 #else
-#define        lock_vnode(v)   vn_lock((v), LK_EXCLUSIVE | LK_RETRY, curproc)
-#define unlock_vnode(v)        VOP_UNLOCK((v), 0, curproc)
-/* need splvm() protection? */
-#define        VM_OBJECT_LOCK(o)
-#define VM_OBJECT_UNLOCK(o)
+#define AFS_VM_OBJECT_WLOCK(o) VM_OBJECT_LOCK(o)
+#define AFS_VM_OBJECT_WUNLOCK(o)       VM_OBJECT_UNLOCK(o)
 #endif
 
 /* Try to discard pages, in order to recycle a vcache entry.
@@ -79,57 +75,36 @@ RCSID
  * We also do some non-VM-related chores, such as releasing the cred pointer
  * (for AIX and Solaris) and releasing the gnode (for AIX).
  *
- * Locking:  afs_xvcache lock is held.  If it is dropped and re-acquired,
- *   *slept should be set to warn the caller.
+ * Locking:  afs_xvcache lock is held. It must not be dropped.
  *
- * Formerly, afs_xvcache was dropped and re-acquired for Solaris, but now it
- * is not dropped and re-acquired for any platform.  It may be that *slept is
- * therefore obsolescent.
- *
- * OSF/1 Locking:  VN_LOCK has been called.
- * XXX - should FreeBSD have done this, too?  Certainly looks like it.
- * Maybe better to just call vnode_pager_setsize()?
  */
 int
-osi_VM_FlushVCache(struct vcache *avc, int *slept)
+osi_VM_FlushVCache(struct vcache *avc)
 {
-    struct vm_object *obj;
     struct vnode *vp;
-    if (VREFCOUNT(avc) > 1)
-       return EBUSY;
+    int code;
 
-    if (avc->opens)
-       return EBUSY;
+    vp = AFSTOV(avc);
 
-    /* if a lock is held, give up */
-    if (CheckLock(&avc->lock))
+    if (!VI_TRYLOCK(vp))
        return EBUSY;
+    code = osi_fbsd_checkinuse(avc);
+    if (code) {
+       VI_UNLOCK(vp);
+       return code;
+    }
 
-    return(0);
+    /* must hold the vnode before calling cache_purge()
+     * This code largely copied from vfs_subr.c:vlrureclaim() */
+    vholdl(vp);
+    VI_UNLOCK(vp);
 
     AFS_GUNLOCK();
-    vp = AFSTOV(avc);
-#ifndef AFS_FBSD70_ENV
-    lock_vnode(vp);
-#endif
-    if (VOP_GETVOBJECT(vp, &obj) == 0) {
-       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
+    cache_purge(vp);
     AFS_GLOCK();
 
+    vdrop(vp);
+
     return 0;
 }
 
@@ -161,31 +136,20 @@ osi_VM_StoreAllSegments(struct vcache *avc)
      */
     do {
        anyio = 0;
-#ifdef AFS_FBSD80_ENV
-       lock_vnode(vp);
-#endif
-       if (VOP_GETVOBJECT(vp, &obj) == 0 && (obj->flags & OBJ_MIGHTBEDIRTY)) {
-#ifdef AFS_FBSD80_ENV
-           unlock_vnode(vp);
-#endif
-#ifdef AFS_FBSD50_ENV
+       
+       obj = vp->v_object;
+       if (obj != NULL && obj->flags & OBJ_MIGHTBEDIRTY) {
            if (!vget(vp, LK_EXCLUSIVE | LK_RETRY, curthread)) {
-#else
-               if (!vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
-#endif
-                   if (VOP_GETVOBJECT(vp, &obj) == 0) {
-                       VM_OBJECT_LOCK(obj);
+                   obj = vp->v_object;
+                   if (obj != NULL) {
+                       AFS_VM_OBJECT_WLOCK(obj);
                        vm_object_page_clean(obj, 0, 0, OBJPC_SYNC);
-                       VM_OBJECT_UNLOCK(obj);
+                       AFS_VM_OBJECT_WUNLOCK(obj);
                        anyio = 1;
                    }
                    vput(vp);
                }
            }
-#ifdef AFS_FBSD80_ENV
-           else
-               unlock_vnode(vp);
-#endif
     } while (anyio && (--tries > 0));
     AFS_GLOCK();
     ObtainWriteLock(&avc->lock, 94);
@@ -201,48 +165,61 @@ osi_VM_StoreAllSegments(struct vcache *avc)
  * 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)
+osi_VM_TryToSmush(struct vcache *avc, afs_ucred_t *acred, int sync)
 {
     struct vnode *vp;
-    struct vm_object *obj;
-    int anyio, tries, code;
-
-    SPLVAR;
+    int tries, code;
+    int islocked;
 
     vp = AFSTOV(avc);
 
+    VI_LOCK(vp);
     if (vp->v_iflag & VI_DOOMED) {
-      USERPRI;
-      return 0;
+       VI_UNLOCK(vp);
+       return;
     }
+    VI_UNLOCK(vp);
+
+    islocked = islocked_vnode(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");
+       lock_vnode(vp, LK_UPGRADE);
+    } else if (!islocked)
+       lock_vnode(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
-       */
+       AFS_VM_OBJECT_WLOCK(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.
-       */
+       /*
+        * 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);
+       vm_object_page_clean(vp->v_bufobj.bo_object, 0, 0, OBJPC_SYNC);
+       AFS_VM_OBJECT_WUNLOCK(vp->v_bufobj.bo_object);
     }
 
     tries = 5;
-    code = vinvalbuf(vp, V_SAVE, curthread, PCATCH, 0);
+    code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
     while (code && (tries > 0)) {
-      code = vinvalbuf(vp, V_SAVE, curthread, PCATCH, 0);
-      --tries;
+       afs_warn("TryToSmush retrying vinvalbuf");
+       code = osi_vinvalbuf(vp, V_SAVE, PCATCH, 0);
+       --tries;
     }
-    USERPRI;
+    if (islocked == LK_SHARED)
+       lock_vnode(vp, LK_DOWNGRADE);
+    else if (!islocked)
+       unlock_vnode(vp);
 }
 
 /* Purge VM for a file when its callback is revoked.
@@ -250,19 +227,20 @@ osi_VM_TryToSmush(struct vcache *avc, struct AFS_UCRED *acred, int sync)
  * Locking:  No lock is held, not even the global lock.
  */
 void
-osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
+osi_VM_FlushPages(struct vcache *avc, afs_ucred_t *credp)
 {
     struct vnode *vp;
     struct vm_object *obj;
 
     vp = AFSTOV(avc);
     ASSERT_VOP_LOCKED(vp, __func__);
-    if (VOP_GETVOBJECT(vp, &obj) == 0) {
-       VM_OBJECT_LOCK(obj);
+    obj = vp->v_object;
+    if (obj != NULL) {
+       AFS_VM_OBJECT_WLOCK(obj);
        vm_object_page_remove(obj, 0, 0, FALSE);
-       VM_OBJECT_UNLOCK(obj);
+       AFS_VM_OBJECT_WUNLOCK(obj);
     }
-    /*vinvalbuf(AFSTOV(avc),0, NOCRED, curproc, 0,0); */
+    osi_vinvalbuf(vp, 0, 0, 0);
 }
 
 /* Purge pages beyond end-of-file, when truncating a file.
@@ -272,7 +250,7 @@ osi_VM_FlushPages(struct vcache *avc, struct AFS_UCRED *credp)
  * it only works on Solaris.
  */
 void
-osi_VM_Truncate(struct vcache *avc, int alen, struct AFS_UCRED *acred)
+osi_VM_Truncate(struct vcache *avc, int alen, afs_ucred_t *acred)
 {
     vnode_pager_setsize(AFSTOV(avc), alen);
 }