Whamcloud - gitweb
b=13872
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.c
index ce8127a..e83d396 100644 (file)
@@ -1,37 +1,48 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (c) 2002 Cluster File Systems, Inc.
+ *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
  *   Author: Peter Braam <braam@clusterfs.com>
  *   Author: Phil Schwan <phil@clusterfs.com>
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ *   This file is part of the Lustre file system, http://www.lustre.org
+ *   Lustre is a trademark of Cluster File Systems, Inc.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ *   You may have signed or agreed to another license before downloading
+ *   this software.  If so, you are bound by the terms and conditions
+ *   of that agreement, and the following does not apply to you.  See the
+ *   LICENSE file included with this distribution for more information.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ *   If you did not agree to a different license, then this copy of Lustre
+ *   is open source software; you can redistribute it and/or modify it
+ *   under the terms of version 2 of the GNU General Public License as
+ *   published by the Free Software Foundation.
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *   In either case, Lustre is distributed in the hope that it will be
+ *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   license text for more details.
  */
 
 #define DEBUG_SUBSYSTEM S_LDLM
 
-#include <linux/slab.h>
-#include <linux/module.h>
-#include <linux/random.h>
-#include <linux/lustre_dlm.h>
-#include <linux/lustre_mds.h>
-#include <linux/obd_class.h>
+#ifdef __KERNEL__
+# include <libcfs/libcfs.h>
+# include <linux/lustre_intent.h>
+#else
+# include <liblustre.h>
+# include <libcfs/kp30.h>
+#endif
 
-/* this lock protects ldlm_handle2lock's integrity */
-//static spinlock_t ldlm_handle_lock = SPIN_LOCK_UNLOCKED;
+#include <obd_class.h>
+#include "ldlm_internal.h"
+
+/* lock's skip list pointers fix mode */
+#define LDLM_JOIN_NONE          0
+#define LDLM_MODE_JOIN_RIGHT    1
+#define LDLM_MODE_JOIN_LEFT     (1 << 1)
+#define LDLM_POLICY_JOIN_RIGHT  (1 << 2)
+#define LDLM_POLICY_JOIN_LEFT   (1 << 3)
 
 /* lock types */
 char *ldlm_lockname[] = {
@@ -41,12 +52,15 @@ char *ldlm_lockname[] = {
         [LCK_PR] "PR",
         [LCK_CW] "CW",
         [LCK_CR] "CR",
-        [LCK_NL] "NL"
+        [LCK_NL] "NL",
+        [LCK_GROUP] "GROUP"
 };
+
 char *ldlm_typename[] = {
         [LDLM_PLAIN] "PLN",
         [LDLM_EXTENT] "EXT",
-        [LDLM_MDSINTENT] "INT"
+        [LDLM_FLOCK] "FLK",
+        [LDLM_IBITS] "IBT",
 };
 
 char *ldlm_it2str(int it)
@@ -58,65 +72,41 @@ char *ldlm_it2str(int it)
                 return "creat";
         case (IT_OPEN | IT_CREAT):
                 return "open|creat";
-        case IT_MKDIR:
-                return "mkdir";
-        case IT_LINK:
-                return "link";
-        case IT_LINK2:
-                return "link2";
-        case IT_SYMLINK:
-                return "symlink";
-        case IT_UNLINK:
-                return "unlink";
-        case IT_RMDIR:
-                return "rmdir";
-        case IT_RENAME:
-                return "rename";
-        case IT_RENAME2:
-                return "rename2";
         case IT_READDIR:
                 return "readdir";
         case IT_GETATTR:
                 return "getattr";
-        case IT_SETATTR:
-                return "setattr";
-        case IT_READLINK:
-                return "readlink";
-        case IT_MKNOD:
-                return "mknod";
         case IT_LOOKUP:
                 return "lookup";
+        case IT_UNLINK:
+                return "unlink";
+        case IT_GETXATTR:
+                return "getxattr";
         default:
                 CERROR("Unknown intent %d\n", it);
                 return "UNKNOWN";
         }
 }
 
-extern kmem_cache_t *ldlm_lock_slab;
+extern cfs_mem_cache_t *ldlm_lock_slab;
 
-static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
-
-ldlm_res_compat ldlm_res_compat_table[] = {
-        [LDLM_PLAIN] ldlm_plain_compat,
-        [LDLM_EXTENT] ldlm_extent_compat,
-        [LDLM_MDSINTENT] ldlm_plain_compat
-};
-
-ldlm_res_policy ldlm_res_policy_table[] = {
-        [LDLM_PLAIN] NULL,
-        [LDLM_EXTENT] ldlm_extent_policy,
-        [LDLM_MDSINTENT] NULL
+static ldlm_processing_policy ldlm_processing_policy_table[] = {
+        [LDLM_PLAIN] ldlm_process_plain_lock,
+        [LDLM_EXTENT] ldlm_process_extent_lock,
+#ifdef __KERNEL__
+        [LDLM_FLOCK] ldlm_process_flock_lock,
+#endif
+        [LDLM_IBITS] ldlm_process_inodebits_lock,
 };
 
-void ldlm_register_intent(int (*arg) (struct ldlm_lock * lock, void *req_cookie,
-                                      ldlm_mode_t mode, void *data))
+ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
 {
-        ldlm_res_policy_table[LDLM_MDSINTENT] = arg;
+        return ldlm_processing_policy_table[res->lr_type];
 }
 
-void ldlm_unregister_intent(void)
+void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
 {
-        ldlm_res_policy_table[LDLM_MDSINTENT] = NULL;
+        ns->ns_policy = arg;
 }
 
 /*
@@ -132,97 +122,199 @@ void ldlm_unregister_intent(void)
  */
 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
 {
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        lock->l_refc++;
-        ldlm_resource_getref(lock->l_resource);
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+        atomic_inc(&lock->l_refc);
         return lock;
 }
 
+static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
+{
+        LASSERT(size == sizeof(*lock));
+        OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
+}
+
 void ldlm_lock_put(struct ldlm_lock *lock)
 {
-        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
         ENTRY;
 
-        l_lock(&ns->ns_lock);
-        lock->l_refc--;
-        //LDLM_DEBUG(lock, "after refc--");
-        if (lock->l_refc < 0)
-                LBUG();
+        LASSERT(lock->l_resource != LP_POISON);
+        LASSERT(atomic_read(&lock->l_refc) > 0);
+        if (atomic_dec_and_test(&lock->l_refc)) {
+                struct ldlm_resource *res;
 
-        if (ldlm_resource_put(lock->l_resource))
+                LDLM_DEBUG(lock,
+                           "final lock_put on destroyed lock, freeing it.");
+
+                res = lock->l_resource;
+                LASSERT(lock->l_destroyed);
+                LASSERT(list_empty(&lock->l_res_link));
+                LASSERT(list_empty(&lock->l_pending_chain));
+
+                atomic_dec(&res->lr_namespace->ns_locks);
+                ldlm_resource_putref(res);
                 lock->l_resource = NULL;
-        if (lock->l_parent)
-                LDLM_LOCK_PUT(lock->l_parent);
+                if (lock->l_export) {
+                        class_export_put(lock->l_export);
+                        lock->l_export = NULL;
+                }
 
-        if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
-                l_unlock(&ns->ns_lock);
-                LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
+                if (lock->l_lvb_data != NULL)
+                        OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
 
-                //spin_lock(&ldlm_handle_lock);
-                spin_lock(&ns->ns_counter_lock);
-                ns->ns_locks--;
-                spin_unlock(&ns->ns_counter_lock);
+                OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle, 
+                               ldlm_lock_free);
+        }
 
-                lock->l_resource = NULL;
-                lock->l_random = 0xdeadbeefcafebabe;
-                if (lock->l_export && lock->l_export->exp_connection)
-                        ptlrpc_put_connection(lock->l_export->exp_connection);
-                kmem_cache_free(ldlm_lock_slab, lock);
-                //spin_unlock(&ldlm_handle_lock);
-                CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 0).\n",
-                       sizeof(*lock), lock);
-        } else
-                l_unlock(&ns->ns_lock);
+        EXIT;
+}
 
+int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
+{
+        int rc = 0;
+        if (!list_empty(&lock->l_lru)) {
+                struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+                LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
+                list_del_init(&lock->l_lru);
+                ns->ns_nr_unused--;
+                LASSERT(ns->ns_nr_unused >= 0);
+                rc = 1;
+        }
+        return rc;
+}
+
+int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
+{
+        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+        int rc;
+        ENTRY;
+        spin_lock(&ns->ns_unused_lock);
+        rc = ldlm_lock_remove_from_lru_nolock(lock);
+        spin_unlock(&ns->ns_unused_lock);
         EXIT;
+        return rc;
 }
 
-void ldlm_lock_destroy(struct ldlm_lock *lock)
+void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
 {
+        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+        lock->l_last_used = cfs_time_current();
+        LASSERT(list_empty(&lock->l_lru));
+        list_add_tail(&lock->l_lru, &ns->ns_unused_list);
+        LASSERT(ns->ns_nr_unused >= 0);
+        ns->ns_nr_unused++;
+}
+
+void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
+{
+        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
         ENTRY;
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
+        spin_lock(&ns->ns_unused_lock);
+        ldlm_lock_add_to_lru_nolock(lock);
+        spin_unlock(&ns->ns_unused_lock);
+        EXIT;
+}
 
-        if (!list_empty(&lock->l_children)) {
-                LDLM_DEBUG(lock, "still has children (%p)!",
-                           lock->l_children.next);
-                ldlm_lock_dump(lock);
-                LBUG();
+void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
+{
+        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
+        ENTRY;
+        spin_lock(&ns->ns_unused_lock);
+        if (!list_empty(&lock->l_lru)) {
+                ldlm_lock_remove_from_lru_nolock(lock);
+                ldlm_lock_add_to_lru_nolock(lock);
         }
+        spin_unlock(&ns->ns_unused_lock);
+        EXIT;
+}
+
+/* This used to have a 'strict' flag, which recovery would use to mark an
+ * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
+ * shall explain why it's gone: with the new hash table scheme, once you call
+ * ldlm_lock_destroy, you can never drop your final references on this lock.
+ * Because it's not in the hash table anymore.  -phil */
+int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
+{
+        ENTRY;
+
         if (lock->l_readers || lock->l_writers) {
-                LDLM_DEBUG(lock, "lock still has references");
-                ldlm_lock_dump(lock);
+                LDLM_ERROR(lock, "lock still has references");
+                ldlm_lock_dump(D_ERROR, lock, 0);
                 LBUG();
         }
 
         if (!list_empty(&lock->l_res_link)) {
-                ldlm_lock_dump(lock);
+                LDLM_ERROR(lock, "lock still on resource");
+                ldlm_lock_dump(D_ERROR, lock, 0);
                 LBUG();
         }
 
-        if (lock->l_flags & LDLM_FL_DESTROYED) {
-                l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+        if (lock->l_destroyed) {
+                LASSERT(list_empty(&lock->l_lru));
                 EXIT;
-                return;
+                return 0;
         }
-
-        list_del(&lock->l_export_chain);
+        lock->l_destroyed = 1;
+
+        if (lock->l_export)
+                spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
+        list_del_init(&lock->l_export_chain);
+        if (lock->l_export)
+                spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
+
+        ldlm_lock_remove_from_lru(lock);
+        class_handle_unhash(&lock->l_handle);
+
+#if 0
+        /* Wake anyone waiting for this lock */
+        /* FIXME: I should probably add yet another flag, instead of using
+         * l_export to only call this on clients */
+        if (lock->l_export)
+                class_export_put(lock->l_export);
         lock->l_export = NULL;
-        lock->l_flags |= LDLM_FL_DESTROYED;
+        if (lock->l_export && lock->l_completion_ast)
+                lock->l_completion_ast(lock, 0);
+#endif
+        EXIT;
+        return 1;
+}
 
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
-        LDLM_LOCK_PUT(lock);
+void ldlm_lock_destroy(struct ldlm_lock *lock)
+{
+        int first;
+        ENTRY;
+        lock_res_and_lock(lock);
+        first = ldlm_lock_destroy_internal(lock);
+        unlock_res_and_lock(lock);
+
+        /* drop reference from hashtable only for first destroy */
+        if (first)
+                LDLM_LOCK_PUT(lock);
+        EXIT;
+}
+
+void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
+{
+        int first;
+        ENTRY;
+        first = ldlm_lock_destroy_internal(lock);
+        /* drop reference from hashtable only for first destroy */
+        if (first)
+                LDLM_LOCK_PUT(lock);
         EXIT;
 }
 
+/* this is called by portals_handle2object with the handle lock taken */
+static void lock_handle_addref(void *lock)
+{
+        LDLM_LOCK_GET((struct ldlm_lock *)lock);
+}
+
 /*
-   usage: pass in a resource on which you have done get
-          pass in a parent lock on which you have done a get
-          do not put the resource or the parent
-   returns: lock with refcount 1
-*/
-static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
-                                       struct ldlm_resource *resource)
+ * usage: pass in a resource on which you have done ldlm_resource_get
+ *        pass in a parent lock on which you have done a ldlm_lock_get
+ *        after return, ldlm_*_put the resource and parent
+ * returns: lock with refcount 2 - one for current caller and one for remote
+ */
+static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
 {
         struct ldlm_lock *lock;
         ENTRY;
@@ -230,77 +322,77 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
         if (resource == NULL)
                 LBUG();
 
-        lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
+        OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
         if (lock == NULL)
                 RETURN(NULL);
 
-        memset(lock, 0, sizeof(*lock));
-        get_random_bytes(&lock->l_random, sizeof(__u64));
-
-        lock->l_resource = resource;
-        /* this refcount matches the one of the resource passed
-           in which is not being put away */
-        lock->l_refc = 1;
-        INIT_LIST_HEAD(&lock->l_children);
-        INIT_LIST_HEAD(&lock->l_res_link);
-        INIT_LIST_HEAD(&lock->l_export_chain);
-        INIT_LIST_HEAD(&lock->l_pending_chain);
-        init_waitqueue_head(&lock->l_waitq);
-
-        spin_lock(&resource->lr_namespace->ns_counter_lock);
-        resource->lr_namespace->ns_locks++;
-        spin_unlock(&resource->lr_namespace->ns_counter_lock);
-
-        if (parent != NULL) {
-                l_lock(&parent->l_resource->lr_namespace->ns_lock);
-                lock->l_parent = parent;
-                list_add(&lock->l_childof, &parent->l_children);
-                l_unlock(&parent->l_resource->lr_namespace->ns_lock);
-        }
-
-        CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
-               "%p (tot %d).\n", sizeof(*lock), lock, 1);
-        /* this is the extra refcount, to prevent the lock from evaporating */
-        LDLM_LOCK_GET(lock);
+        spin_lock_init(&lock->l_lock);
+        lock->l_resource = ldlm_resource_getref(resource);
+
+        atomic_set(&lock->l_refc, 2);
+        CFS_INIT_LIST_HEAD(&lock->l_res_link);
+        CFS_INIT_LIST_HEAD(&lock->l_lru);
+        CFS_INIT_LIST_HEAD(&lock->l_export_chain);
+        CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
+        CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
+        CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
+        cfs_waitq_init(&lock->l_waitq);
+        lock->l_blocking_lock = NULL;
+        lock->l_sl_mode.prev = NULL;
+        lock->l_sl_mode.next = NULL;
+        lock->l_sl_policy.prev = NULL;
+        lock->l_sl_policy.next = NULL;
+
+        atomic_inc(&resource->lr_namespace->ns_locks);
+        CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
+        class_handle_hash(&lock->l_handle, lock_handle_addref);
+
         RETURN(lock);
 }
 
-int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
+int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
+                              const struct ldlm_res_id *new_resid)
 {
-        struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
         struct ldlm_resource *oldres = lock->l_resource;
-        int type, i;
+        struct ldlm_resource *newres;
+        int type;
         ENTRY;
 
-        l_lock(&ns->ns_lock);
-        if (memcmp(new_resid, lock->l_resource->lr_name,
+        LASSERT(ns_is_client(ns));
+
+        lock_res_and_lock(lock);
+        if (memcmp(new_resid, &lock->l_resource->lr_name,
                    sizeof(lock->l_resource->lr_name)) == 0) {
                 /* Nothing to do */
-                l_unlock(&ns->ns_lock);
+                unlock_res_and_lock(lock);
                 RETURN(0);
         }
 
-        type = lock->l_resource->lr_type;
-        if (new_resid[0] == 0)
-                LBUG();
-        lock->l_resource = ldlm_resource_get(ns, NULL, new_resid, type, 1);
-        if (lock->l_resource == NULL) {
+        LASSERT(new_resid->name[0] != 0);
+
+        /* This function assumes that the lock isn't on any lists */
+        LASSERT(list_empty(&lock->l_res_link));
+
+        type = oldres->lr_type;
+        unlock_res_and_lock(lock);
+
+        newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
+        if (newres == NULL) {
                 LBUG();
                 RETURN(-ENOMEM);
         }
 
-        /* move references over */
-        for (i = 0; i < lock->l_refc; i++) {
-                int rc;
-                ldlm_resource_getref(lock->l_resource);
-                rc = ldlm_resource_put(oldres);
-                if (rc == 1 && i != lock->l_refc - 1)
-                        LBUG();
-        }
-        /* compensate for the initial get above.. */
-        ldlm_resource_put(lock->l_resource);
+        lock_res_and_lock(lock);
+        LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
+                       sizeof(lock->l_resource->lr_name)) != 0);
+        lock_res(newres);
+        lock->l_resource = newres;
+        unlock_res(oldres);
+        unlock_res_and_lock(lock);
+
+        /* ...and the flowers are still standing! */
+        ldlm_resource_putref(oldres);
 
-        l_unlock(&ns->ns_lock);
         RETURN(0);
 }
 
@@ -308,102 +400,153 @@ int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
  *  HANDLES
  */
 
-void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
+void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
 {
-        lockh->addr = (__u64) (unsigned long)lock;
-        lockh->cookie = lock->l_random;
+        lockh->cookie = lock->l_handle.h_cookie;
 }
 
-struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle)
+/* if flags: atomically get the lock and set the flags.
+ *           Return NULL if flag already set
+ */
+
+struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
+                                     int flags)
 {
-        struct ldlm_lock *lock = NULL, *retval = NULL;
+        struct ldlm_namespace *ns;
+        struct ldlm_lock *lock, *retval = NULL;
         ENTRY;
 
-        if (!handle || !handle->addr)
+        LASSERT(handle);
+
+        lock = class_handle2object(handle->cookie);
+        if (lock == NULL)
                 RETURN(NULL);
 
-        //spin_lock(&ldlm_handle_lock);
-        lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
-        if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
-                CERROR("bogus lock %p\n", lock);
-                GOTO(out2, retval);
-        }
+        LASSERT(lock->l_resource != NULL);
+        ns = lock->l_resource->lr_namespace;
+        LASSERT(ns != NULL);
 
-        if (lock->l_random != handle->cookie) {
-                CERROR("bogus cookie: lock "LPX64", handle "LPX64"\n",
-                       lock->l_random, handle->cookie);
-                GOTO(out, NULL);
-        }
-        if (!lock->l_resource) {
-                CERROR("trying to lock bogus resource: lock %p\n", lock);
-                LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
-                GOTO(out2, retval);
-        }
-        if (!lock->l_resource->lr_namespace) {
-                CERROR("trying to lock bogus namespace: lock %p\n", lock);
-                LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
-                GOTO(out2, retval);
+        lock_res_and_lock(lock);
+
+        /* It's unlikely but possible that someone marked the lock as
+         * destroyed after we did handle2object on it */
+        if (lock->l_destroyed) {
+                unlock_res_and_lock(lock);
+                CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
+                LDLM_LOCK_PUT(lock);
+                GOTO(out, retval);
         }
 
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        if (lock->l_flags & LDLM_FL_DESTROYED) {
-                CERROR("lock already destroyed: lock %p\n", lock);
-                LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
-                GOTO(out, NULL);
+        if (flags && (lock->l_flags & flags)) {
+                unlock_res_and_lock(lock);
+                LDLM_LOCK_PUT(lock);
+                GOTO(out, retval);
         }
 
-        retval = LDLM_LOCK_GET(lock);
-        if (!retval)
-                CERROR("lock disappeared below us!!! %p\n", lock);
+        if (flags)
+                lock->l_flags |= flags;
+
+        unlock_res_and_lock(lock);
+        retval = lock;
         EXIT;
  out:
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
- out2:
-        //spin_unlock(&ldlm_handle_lock);
         return retval;
 }
 
-static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
+struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
+                                      const struct lustre_handle *handle)
 {
-        return lockmode_compat(a->l_req_mode, b->l_req_mode);
+        struct ldlm_lock *retval = NULL;
+        retval = __ldlm_handle2lock(handle, 0);
+        return retval;
 }
 
 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
 {
-        ldlm_res2desc(lock->l_resource, &desc->l_resource);
-        desc->l_req_mode = lock->l_req_mode;
-        desc->l_granted_mode = lock->l_granted_mode;
-        memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
-        memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
+        struct obd_export *exp = lock->l_export?:lock->l_conn_export;
+        /* INODEBITS_INTEROP: If the other side does not support
+         * inodebits, reply with a plain lock descriptor.
+         */
+        if ((lock->l_resource->lr_type == LDLM_IBITS) &&
+            (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
+                struct ldlm_resource res = *lock->l_resource;
+
+                /* Make sure all the right bits are set in this lock we
+                   are going to pass to client */
+                LASSERTF(lock->l_policy_data.l_inodebits.bits ==
+                         (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
+                         "Inappropriate inode lock bits during "
+                         "conversion " LPU64 "\n",
+                         lock->l_policy_data.l_inodebits.bits);
+                res.lr_type = LDLM_PLAIN;
+                ldlm_res2desc(&res, &desc->l_resource);
+                /* Convert "new" lock mode to something old client can
+                   understand */
+                if ((lock->l_req_mode == LCK_CR) ||
+                    (lock->l_req_mode == LCK_CW))
+                        desc->l_req_mode = LCK_PR;
+                else
+                        desc->l_req_mode = lock->l_req_mode;
+                if ((lock->l_granted_mode == LCK_CR) ||
+                    (lock->l_granted_mode == LCK_CW)) {
+                        desc->l_granted_mode = LCK_PR;
+                } else {
+                        /* We never grant PW/EX locks to clients */
+                        LASSERT((lock->l_granted_mode != LCK_PW) &&
+                                (lock->l_granted_mode != LCK_EX));
+                        desc->l_granted_mode = lock->l_granted_mode;
+                }
+
+                /* We do not copy policy here, because there is no
+                   policy for plain locks */
+        } else {
+                ldlm_res2desc(lock->l_resource, &desc->l_resource);
+                desc->l_req_mode = lock->l_req_mode;
+                desc->l_granted_mode = lock->l_granted_mode;
+                desc->l_policy_data = lock->l_policy_data;
+        }
 }
 
-static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
-                                   struct ldlm_lock *new)
+void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
+                           struct list_head *work_list)
 {
-        struct ldlm_ast_work *w;
-        ENTRY;
-
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        if (new && (lock->l_flags & LDLM_FL_AST_SENT))
-                GOTO(out, 0);
-
-        OBD_ALLOC(w, sizeof(*w));
-        if (!w) {
-                LBUG();
-                GOTO(out, 0);
+        if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
+                LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
+                lock->l_flags |= LDLM_FL_AST_SENT;
+                /* If the enqueuing client said so, tell the AST recipient to
+                 * discard dirty data, rather than writing back. */
+                if (new->l_flags & LDLM_AST_DISCARD_DATA)
+                        lock->l_flags |= LDLM_FL_DISCARD_DATA;
+                LASSERT(list_empty(&lock->l_bl_ast));
+                list_add(&lock->l_bl_ast, work_list);
+                LDLM_LOCK_GET(lock);
+                LASSERT(lock->l_blocking_lock == NULL);
+                lock->l_blocking_lock = LDLM_LOCK_GET(new);
         }
+}
 
-        if (new) {
-                lock->l_flags |= LDLM_FL_AST_SENT;
-                w->w_blocking = 1;
-                ldlm_lock2desc(new, &w->w_desc);
+void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
+{
+        if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
+                lock->l_flags |= LDLM_FL_CP_REQD;
+                LDLM_DEBUG(lock, "lock granted; sending completion AST.");
+                LASSERT(list_empty(&lock->l_cp_ast));
+                list_add(&lock->l_cp_ast, work_list);
+                LDLM_LOCK_GET(lock);
         }
+}
 
-        w->w_lock = LDLM_LOCK_GET(lock);
-        list_add(&w->w_list, lock->l_resource->lr_tmp);
-      out:
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
-        return;
+/* must be called with lr_lock held */
+void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
+                                struct list_head *work_list)
+{
+        ENTRY;
+        check_res_locked(lock->l_resource);
+        if (new)
+                ldlm_add_bl_work_item(lock, new, work_list);
+        else
+                ldlm_add_cp_work_item(lock, work_list);
+        EXIT;
 }
 
 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
@@ -411,417 +554,933 @@ void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
         struct ldlm_lock *lock;
 
         lock = ldlm_handle2lock(lockh);
+        LASSERT(lock != NULL);
         ldlm_lock_addref_internal(lock, mode);
         LDLM_LOCK_PUT(lock);
 }
 
-/* only called for local locks */
-void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
+void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 {
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
+        ldlm_lock_remove_from_lru(lock);
+        if (mode & (LCK_NL | LCK_CR | LCK_PR))
                 lock->l_readers++;
-        else
+        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
                 lock->l_writers++;
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
         LDLM_LOCK_GET(lock);
         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
 }
 
-/* Args: unlocked lock */
-void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
+/* only called for local locks */
+void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
 {
-        struct ldlm_lock *lock = ldlm_handle2lock(lockh);
+        lock_res_and_lock(lock);
+        ldlm_lock_addref_internal_nolock(lock, mode);
+        unlock_res_and_lock(lock);
+}
+
+void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
+{
+        struct ldlm_namespace *ns;
         ENTRY;
 
-        if (lock == NULL)
-                LBUG();
+        lock_res_and_lock(lock);
+
+        ns = lock->l_resource->lr_namespace;
 
         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
+        if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
+                LASSERT(lock->l_readers > 0);
                 lock->l_readers--;
-        else
+        }
+        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
+                LASSERT(lock->l_writers > 0);
                 lock->l_writers--;
+        }
+
+        if (lock->l_flags & LDLM_FL_LOCAL &&
+            !lock->l_readers && !lock->l_writers) {
+                /* If this is a local lock on a server namespace and this was
+                 * the last reference, cancel the lock. */
+                CDEBUG(D_INFO, "forcing cancel of local lock\n");
+                lock->l_flags |= LDLM_FL_CBPENDING;
+        }
 
-        /* If we received a blocked AST and this was the last reference,
-         * run the callback. */
         if (!lock->l_readers && !lock->l_writers &&
             (lock->l_flags & LDLM_FL_CBPENDING)) {
-                if (!lock->l_resource->lr_namespace->ns_client &&
-                    lock->l_export)
+                /* If we received a blocked AST and this was the last reference,
+                 * run the callback. */
+                if (ns_is_server(ns) && lock->l_export)
                         CERROR("FL_CBPENDING set on non-local lock--just a "
                                "warning\n");
 
                 LDLM_DEBUG(lock, "final decref done on cbpending lock");
-                l_unlock(&lock->l_resource->lr_namespace->ns_lock);
 
-                /* FIXME: need a real 'desc' here */
-                lock->l_blocking_ast(lock, NULL, lock->l_data,
-                                     lock->l_data_len, LDLM_CB_BLOCKING);
-        } else
-                l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+                LDLM_LOCK_GET(lock); /* dropped by bl thread */
+                ldlm_lock_remove_from_lru(lock);
+                unlock_res_and_lock(lock);
+                if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
+                    ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
+                        ldlm_handle_bl_callback(ns, NULL, lock);
+        } else if (ns_is_client(ns) &&
+                   !lock->l_readers && !lock->l_writers &&
+                   !(lock->l_flags & LDLM_FL_NO_LRU)) {
+                /* If this is a client-side namespace and this was the last
+                 * reference, put it on the LRU. */
+                ldlm_lock_add_to_lru(lock);
+                unlock_res_and_lock(lock);
+                /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE 
+                 * are not supported by the server, otherwise, it is done on 
+                 * enqueue. */
+                if (!exp_connect_cancelset(lock->l_conn_export) && 
+                    !exp_connect_lru_resize(lock->l_conn_export))
+                        ldlm_cancel_lru(ns, 0, LDLM_ASYNC);
+        } else {
+                unlock_res_and_lock(lock);
+        }
 
         LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
-        LDLM_LOCK_PUT(lock);    /* matches the handle2lock above */
 
         EXIT;
 }
 
-static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
-                                 struct list_head *queue)
+void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
 {
-        struct list_head *tmp, *pos;
-        int rc = 1;
+        struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
+        LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
+        ldlm_lock_decref_internal(lock, mode);
+        LDLM_LOCK_PUT(lock);
+}
 
-        list_for_each_safe(tmp, pos, queue) {
-                struct ldlm_lock *child;
-                ldlm_res_compat compat;
+/* This will drop a lock reference and mark it for destruction, but will not
+ * necessarily cancel the lock before returning. */
+void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
+{
+        struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
+        ENTRY;
 
-                child = list_entry(tmp, struct ldlm_lock, l_res_link);
-                if (lock == child)
-                        continue;
+        LASSERT(lock != NULL);
 
-                compat = ldlm_res_compat_table[child->l_resource->lr_type];
-                if (compat && compat(child, lock)) {
-                        CDEBUG(D_OTHER, "compat function succeded, next.\n");
+        LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
+        lock_res_and_lock(lock);
+        lock->l_flags |= LDLM_FL_CBPENDING;
+        unlock_res_and_lock(lock);
+        ldlm_lock_decref_internal(lock, mode);
+        LDLM_LOCK_PUT(lock);
+}
+
+/*
+ * search_granted_lock
+ *
+ * Description:
+ *      Finds a position to insert the new lock.
+ * Parameters:
+ *      queue [input]:  the granted list where search acts on;
+ *      req [input]:    the lock whose position to be located;
+ *      lockp [output]: the position where the lock should be inserted before, or
+ *                      NULL indicating @req should be appended to @queue.
+ * Return Values:
+ *      Bit-masks combination of following values indicating in which way the 
+ *      lock need to be inserted.
+ *      - LDLM_JOIN_NONE:       noting about skip list needs to be fixed;
+ *      - LDLM_MODE_JOIN_RIGHT: @req needs join right becoming the head of a 
+ *                              mode group;
+ *      - LDLM_POLICY_JOIN_RIGHT: @req needs join right becoming the head of
+ *                                a policy group.
+ * NOTE: called by
+ *  - ldlm_grant_lock_with_skiplist
+ */
+static int search_granted_lock(struct list_head *queue, 
+                        struct ldlm_lock *req,
+                        struct ldlm_lock **lockp)
+{
+        struct list_head *tmp, *tmp_tail;
+        struct ldlm_lock *lock, *mode_head_lock;
+        int rc = LDLM_JOIN_NONE;
+        ENTRY;
+
+        list_for_each(tmp, queue) {
+                lock = list_entry(tmp, struct ldlm_lock, l_res_link);
+
+                if (lock->l_req_mode != req->l_req_mode) {
+                        if (LDLM_SL_HEAD(&lock->l_sl_mode))
+                                tmp = &list_entry(lock->l_sl_mode.next,
+                                                  struct ldlm_lock,
+                                                  l_sl_mode)->l_res_link;
                         continue;
                 }
-                if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
-                        CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
-                        continue;
+                
+                /* found the same mode group */
+                if (lock->l_resource->lr_type == LDLM_PLAIN) {
+                        *lockp = lock;
+                        rc = LDLM_MODE_JOIN_RIGHT;
+                        GOTO(out, rc);
+                } else if (lock->l_resource->lr_type == LDLM_IBITS) {
+                        tmp_tail = tmp;
+                        if (LDLM_SL_HEAD(&lock->l_sl_mode))
+                                tmp_tail = &list_entry(lock->l_sl_mode.next,
+                                                       struct ldlm_lock,
+                                                       l_sl_mode)->l_res_link;
+                        mode_head_lock = lock;
+                        for (;;) {
+                                if (lock->l_policy_data.l_inodebits.bits ==
+                                    req->l_policy_data.l_inodebits.bits) {
+                                        /* matched policy lock is found */
+                                        *lockp = lock;
+                                        rc |= LDLM_POLICY_JOIN_RIGHT;
+
+                                        /* if the policy group head is also a 
+                                         * mode group head or a single mode
+                                         * group lock */
+                                        if (LDLM_SL_HEAD(&lock->l_sl_mode) ||
+                                            (tmp == tmp_tail &&
+                                             LDLM_SL_EMPTY(&lock->l_sl_mode)))
+                                                rc |= LDLM_MODE_JOIN_RIGHT;
+                                        GOTO(out, rc);
+                                }
+
+                                if (LDLM_SL_HEAD(&lock->l_sl_policy))
+                                        tmp = &list_entry(lock->l_sl_policy.next,
+                                                          struct ldlm_lock,
+                                                          l_sl_policy)->l_res_link;
+
+                                if (tmp == tmp_tail)
+                                        break;
+                                else
+                                        tmp = tmp->next;
+                                lock = list_entry(tmp, struct ldlm_lock, 
+                                                  l_res_link);
+                        }  /* for all locks in the matched mode group */
+
+                        /* no matched policy group is found, insert before
+                         * the mode group head lock */
+                        *lockp = mode_head_lock;
+                        rc = LDLM_MODE_JOIN_RIGHT;
+                        GOTO(out, rc);
+                } else {
+                        LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
+                        LBUG();
                 }
+        }
+
+        /* no matched mode group is found, append to the end */
+        *lockp = NULL;
+        rc = LDLM_JOIN_NONE;
+        EXIT;
+out:
+        return rc;
+}
+
+static void ldlm_granted_list_add_lock(struct ldlm_lock *lock, 
+                                       struct ldlm_lock *lockp,
+                                       int join)
+{
+        struct ldlm_resource *res = lock->l_resource;
+        ENTRY;
 
-                rc = 0;
+        LASSERT(lockp || join == LDLM_JOIN_NONE);
 
-                if (send_cbs && child->l_blocking_ast != NULL) {
-                        CDEBUG(D_OTHER, "lock %p incompatible; sending "
-                               "blocking AST.\n", child);
-                        ldlm_add_ast_work_item(child, lock);
+        check_res_locked(res);
+
+        ldlm_resource_dump(D_OTHER, res);
+        CDEBUG(D_OTHER, "About to add this lock:\n");
+        ldlm_lock_dump(D_OTHER, lock, 0);
+
+        if (lock->l_destroyed) {
+                CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
+                return;
+        }
+
+        LASSERT(list_empty(&lock->l_res_link));
+
+        if (!lockp)
+                list_add_tail(&lock->l_res_link, &lock->l_resource->lr_granted);
+        else if ((join & LDLM_MODE_JOIN_LEFT) || (join & LDLM_POLICY_JOIN_LEFT))
+                list_add(&lock->l_res_link, &lockp->l_res_link);
+        else
+                list_add_tail(&lock->l_res_link, &lockp->l_res_link);
+
+        /* fix skip lists */
+        if (join & LDLM_MODE_JOIN_RIGHT) {
+                LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_mode));
+                if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
+                        lock->l_sl_mode.next = &lockp->l_sl_mode;
+                        lockp->l_sl_mode.prev = &lock->l_sl_mode;
+                } else if (LDLM_SL_HEAD(&lockp->l_sl_mode)) {
+                        lock->l_sl_mode.next = lockp->l_sl_mode.next;
+                        lockp->l_sl_mode.next = NULL;
+                        lock->l_sl_mode.next->prev = &lock->l_sl_mode;
+                }
+        } else if (join & LDLM_MODE_JOIN_LEFT) {
+                LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_mode));
+               if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
+                        lock->l_sl_mode.prev = &lockp->l_sl_mode;
+                        lockp->l_sl_mode.next = &lock->l_sl_mode;
+                } else if (LDLM_SL_TAIL(&lockp->l_sl_mode)) {
+                        lock->l_sl_mode.prev = lockp->l_sl_mode.prev;
+                        lockp->l_sl_mode.prev = NULL;
+                        lock->l_sl_mode.prev->next = &lock->l_sl_mode;
+                }
+        }
+        
+        if (join & LDLM_POLICY_JOIN_RIGHT) {
+                LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_policy));
+                if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
+                        lock->l_sl_policy.next = &lockp->l_sl_policy;
+                        lockp->l_sl_policy.prev = &lock->l_sl_policy;
+                } else if (LDLM_SL_HEAD(&lockp->l_sl_policy)) {
+                        lock->l_sl_policy.next = lockp->l_sl_policy.next;
+                        lockp->l_sl_policy.next = NULL;
+                        lock->l_sl_policy.next->prev = &lock->l_sl_policy;
+                }
+        } else if (join & LDLM_POLICY_JOIN_LEFT) {
+                LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_policy));
+                if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
+                        lock->l_sl_policy.prev = &lockp->l_sl_policy;
+                        lockp->l_sl_policy.next = &lock->l_sl_policy;
+                } else if (LDLM_SL_TAIL(&lockp->l_sl_policy)) {
+                        lock->l_sl_policy.prev = lockp->l_sl_policy.prev;
+                        lockp->l_sl_policy.prev = NULL;
+                        lock->l_sl_policy.prev->next = &lock->l_sl_policy;
                 }
         }
 
-        return rc;
+        EXIT;
 }
 
-static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
+static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
 {
-        int rc;
+        int join = LDLM_JOIN_NONE;
+        struct ldlm_lock *lockp = NULL;
         ENTRY;
 
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        rc = ldlm_lock_compat_list(lock, send_cbs,
-                                   &lock->l_resource->lr_granted);
-        /* FIXME: should we be sending ASTs to converting? */
-        if (rc)
-                rc = ldlm_lock_compat_list
-                        (lock, send_cbs, &lock->l_resource->lr_converting);
+        LASSERT(lock->l_req_mode == lock->l_granted_mode);
 
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
-        RETURN(rc);
+        join = search_granted_lock(&lock->l_resource->lr_granted, lock, &lockp);
+        ldlm_granted_list_add_lock(lock, lockp, join);
+        EXIT;
 }
 
 /* NOTE: called by
-   - ldlm_handle_enqueuque - resource
-*/
-void ldlm_grant_lock(struct ldlm_lock *lock)
+ *  - ldlm_lock_enqueue
+ *  - ldlm_reprocess_queue
+ *  - ldlm_lock_convert
+ *
+ * must be called with lr_lock held
+ */
+void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
 {
         struct ldlm_resource *res = lock->l_resource;
         ENTRY;
 
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
-        ldlm_resource_add_lock(res, &res->lr_granted, lock);
+        check_res_locked(res);
+
         lock->l_granted_mode = lock->l_req_mode;
+        if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
+                ldlm_grant_lock_with_skiplist(lock);
+        else
+                ldlm_resource_add_lock(res, &res->lr_granted, lock);
 
         if (lock->l_granted_mode < res->lr_most_restr)
                 res->lr_most_restr = lock->l_granted_mode;
 
-        if (lock->l_completion_ast) {
-                ldlm_add_ast_work_item(lock, NULL);
-        }
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
+        if (work_list && lock->l_completion_ast != NULL)
+                ldlm_add_ast_work_item(lock, NULL, work_list);
+
+        ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
         EXIT;
 }
 
-/* returns a referenced lock or NULL */
-static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
-                                      struct ldlm_extent *extent)
+/* returns a referenced lock or NULL.  See the flag descriptions below, in the
+ * comment above ldlm_lock_match */
+static struct ldlm_lock *search_queue(struct list_head *queue,
+                                      ldlm_mode_t *mode,
+                                      ldlm_policy_data_t *policy,
+                                      struct ldlm_lock *old_lock, int flags)
 {
         struct ldlm_lock *lock;
         struct list_head *tmp;
 
         list_for_each(tmp, queue) {
+                ldlm_mode_t match;
+
                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
 
-                if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
+                if (lock == old_lock)
+                        break;
+
+                /* llite sometimes wants to match locks that will be
+                 * canceled when their users drop, but we allow it to match
+                 * if it passes in CBPENDING and the lock still has users.
+                 * this is generally only going to be used by children
+                 * whose parents already hold a lock so forward progress
+                 * can still happen. */
+                if (lock->l_flags & LDLM_FL_CBPENDING &&
+                    !(flags & LDLM_FL_CBPENDING))
+                        continue;
+                if (lock->l_flags & LDLM_FL_CBPENDING &&
+                    lock->l_readers == 0 && lock->l_writers == 0)
                         continue;
 
-                /* lock_convert() takes the resource lock, so we're sure that
-                 * req_mode, lr_type, and l_cookie won't change beneath us */
-                if (lock->l_req_mode != mode)
+                if (!(lock->l_req_mode & *mode))
                         continue;
+                match = lock->l_req_mode;
 
                 if (lock->l_resource->lr_type == LDLM_EXTENT &&
-                    (lock->l_extent.start > extent->start ||
-                     lock->l_extent.end < extent->end))
+                    (lock->l_policy_data.l_extent.start >
+                     policy->l_extent.start ||
+                     lock->l_policy_data.l_extent.end < policy->l_extent.end))
+                        continue;
+
+                if (unlikely(match == LCK_GROUP) &&
+                    lock->l_resource->lr_type == LDLM_EXTENT &&
+                    lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
+                        continue;
+
+                /* We match if we have existing lock with same or wider set
+                   of bits. */
+                if (lock->l_resource->lr_type == LDLM_IBITS &&
+                     ((lock->l_policy_data.l_inodebits.bits &
+                      policy->l_inodebits.bits) !=
+                      policy->l_inodebits.bits))
+                        continue;
+
+                if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
+                        continue;
+
+                if ((flags & LDLM_FL_LOCAL_ONLY) &&
+                    !(lock->l_flags & LDLM_FL_LOCAL))
                         continue;
 
-                ldlm_lock_addref_internal(lock, mode);
+                if (flags & LDLM_FL_TEST_LOCK) {
+                        LDLM_LOCK_GET(lock);
+                        ldlm_lock_touch_in_lru(lock);
+                } else {
+                        ldlm_lock_addref_internal_nolock(lock, match);
+                }
+                *mode = match;
                 return lock;
         }
 
         return NULL;
 }
 
-/* Must be called with no resource or lock locks held.
+void ldlm_lock_allow_match(struct ldlm_lock *lock)
+{
+        lock_res_and_lock(lock);
+        lock->l_flags |= LDLM_FL_LVB_READY;
+        cfs_waitq_signal(&lock->l_waitq);
+        unlock_res_and_lock(lock);
+}
+
+/* Can be called in two ways:
+ *
+ * If 'ns' is NULL, then lockh describes an existing lock that we want to look
+ * for a duplicate of.
+ *
+ * Otherwise, all of the fields must be filled in, to match against.
+ *
+ * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
+ *     server (ie, connh is NULL)
+ * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
+ *     list will be considered
+ * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
+ *     to be canceled can still be matched as long as they still have reader
+ *     or writer refernces
+ * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
+ *     just tell us if we would have matched.
  *
  * Returns 1 if it finds an already-existing lock that is compatible; in this
  * case, lockh is filled in with a addref()ed lock
-*/
-int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
-                    void *cookie, int cookielen, ldlm_mode_t mode,
-                    struct lustre_handle *lockh)
+ *
+ * we also check security context, if that failed we simply return 0 (to keep
+ * caller code unchanged), the context failure will be discovered by caller
+ * sometime later.
+ */
+ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
+                            const struct ldlm_res_id *res_id, ldlm_type_t type,
+                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
+                            struct lustre_handle *lockh)
 {
         struct ldlm_resource *res;
-        struct ldlm_lock *lock;
+        struct ldlm_lock *lock, *old_lock = NULL;
         int rc = 0;
         ENTRY;
 
+        if (ns == NULL) {
+                old_lock = ldlm_handle2lock(lockh);
+                LASSERT(old_lock);
+
+                ns = old_lock->l_resource->lr_namespace;
+                res_id = &old_lock->l_resource->lr_name;
+                type = old_lock->l_resource->lr_type;
+                mode = old_lock->l_req_mode;
+        }
+
         res = ldlm_resource_get(ns, NULL, res_id, type, 0);
-        if (res == NULL)
+        if (res == NULL) {
+                LASSERT(old_lock == NULL);
                 RETURN(0);
+        }
 
-        ns = res->lr_namespace;
-        l_lock(&ns->ns_lock);
+        lock_res(res);
 
-        if ((lock = search_queue(&res->lr_granted, mode, cookie)))
+        lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
+        if (lock != NULL)
                 GOTO(out, rc = 1);
-        if ((lock = search_queue(&res->lr_converting, mode, cookie)))
+        if (flags & LDLM_FL_BLOCK_GRANTED)
+                GOTO(out, rc = 0);
+        lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
+        if (lock != NULL)
                 GOTO(out, rc = 1);
-        if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
+        lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
+        if (lock != NULL)
                 GOTO(out, rc = 1);
 
         EXIT;
     out:
-        ldlm_resource_put(res);
-        l_unlock(&ns->ns_lock);
+ out:
+        unlock_res(res);
+        ldlm_resource_putref(res);
 
         if (lock) {
                 ldlm_lock2handle(lock, lockh);
-                if (lock->l_completion_ast)
-                        lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
+                if ((flags & LDLM_FL_LVB_READY) &&
+                    (!(lock->l_flags & LDLM_FL_LVB_READY))) {
+                        struct l_wait_info lwi;
+                        if (lock->l_completion_ast) {
+                                int err = lock->l_completion_ast(lock,
+                                                          LDLM_FL_WAIT_NOREPROC,
+                                                                 NULL);
+                                if (err) {
+                                        if (flags & LDLM_FL_TEST_LOCK)
+                                                LDLM_LOCK_PUT(lock);
+                                        else
+                                                ldlm_lock_decref_internal(lock, mode);
+                                        rc = 0;
+                                        goto out2;
+                                }
+                        }
+
+                        lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
+                                               LWI_ON_SIGNAL_NOOP, NULL);
+
+                        /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
+                        l_wait_event(lock->l_waitq,
+                                     (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
+                }
         }
-        if (rc)
-                LDLM_DEBUG(lock, "matched");
-        else
-                LDLM_DEBUG_NOLOCK("not matched");
-        return rc;
+ out2:
+        if (rc) {
+                LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
+                           (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+                                res_id->name[2] : policy->l_extent.start,
+                           (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+                                res_id->name[3] : policy->l_extent.end);
+
+                /* check user's security context */
+                if (lock->l_conn_export &&
+                    sptlrpc_import_check_ctx(
+                                class_exp2cliimp(lock->l_conn_export))) {
+                        if (!(flags & LDLM_FL_TEST_LOCK))
+                                ldlm_lock_decref_internal(lock, mode);
+                        rc = 0;
+                }
+
+                if (flags & LDLM_FL_TEST_LOCK)
+                        LDLM_LOCK_PUT(lock);
+
+        } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
+                LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
+                                  LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
+                                  type, mode, res_id->name[0], res_id->name[1],
+                                  (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+                                        res_id->name[2] :policy->l_extent.start,
+                                (type == LDLM_PLAIN || type == LDLM_IBITS) ?
+                                        res_id->name[3] : policy->l_extent.end);
+        }
+        if (old_lock)
+                LDLM_LOCK_PUT(old_lock);
+
+        return rc ? mode : 0;
 }
 
 /* Returns a referenced lock */
 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
-                                   struct lustre_handle *parent_lock_handle,
-                                   __u64 * res_id, __u32 type,
-                                   ldlm_mode_t mode, void *data, __u32 data_len)
+                                   const struct ldlm_res_id *res_id,
+                                   ldlm_type_t type,
+                                   ldlm_mode_t mode,
+                                   ldlm_blocking_callback blocking,
+                                   ldlm_completion_callback completion,
+                                   ldlm_glimpse_callback glimpse,
+                                   void *data, __u32 lvb_len)
 {
-        struct ldlm_resource *res, *parent_res = NULL;
-        struct ldlm_lock *lock, *parent_lock;
-
-        parent_lock = ldlm_handle2lock(parent_lock_handle);
-        if (parent_lock)
-                parent_res = parent_lock->l_resource;
+        struct ldlm_lock *lock;
+        struct ldlm_resource *res;
+        ENTRY;
 
-        res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
+        res = ldlm_resource_get(ns, NULL, res_id, type, 1);
         if (res == NULL)
                 RETURN(NULL);
 
-        lock = ldlm_lock_new(parent_lock, res);
-        if (lock == NULL) {
-                ldlm_resource_put(res);
+        lock = ldlm_lock_new(res);
+        ldlm_resource_putref(res);
+
+        if (lock == NULL)
                 RETURN(NULL);
-        }
 
         lock->l_req_mode = mode;
-        lock->l_data = data;
-        lock->l_data_len = data_len;
+        lock->l_ast_data = data;
+        lock->l_blocking_ast = blocking;
+        lock->l_completion_ast = completion;
+        lock->l_glimpse_ast = glimpse;
+        lock->l_pid = cfs_curproc_pid();
+
+        if (lvb_len) {
+                lock->l_lvb_len = lvb_len;
+                OBD_ALLOC(lock->l_lvb_data, lvb_len);
+                if (lock->l_lvb_data == NULL) {
+                        OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
+                        RETURN(NULL);
+                }
+        }
 
-        return lock;
+        RETURN(lock);
 }
 
-/* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
-ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
-                               void *cookie, int cookie_len,
-                               int *flags,
-                               ldlm_completion_callback completion,
-                               ldlm_blocking_callback blocking)
+ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
+                               struct ldlm_lock **lockp,
+                               void *cookie, int *flags)
 {
-        struct ldlm_resource *res;
-        int local;
-        ldlm_res_policy policy;
+        struct ldlm_lock *lock = *lockp;
+        struct ldlm_resource *res = lock->l_resource;
+        int local = ns_is_client(res->lr_namespace);
+        ldlm_processing_policy policy;
+        ldlm_error_t rc = ELDLM_OK;
         ENTRY;
 
-        res = lock->l_resource;
-        lock->l_blocking_ast = blocking;
-
-        if (res->lr_type == LDLM_EXTENT)
-                memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
-
-        /* policies are not executed on the client */
-        local = res->lr_namespace->ns_client;
-        if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
-                int rc;
-                rc = policy(lock, cookie, lock->l_req_mode, NULL);
-
-                if (rc == ELDLM_LOCK_CHANGED) {
-                        res = lock->l_resource;
+        do_gettimeofday(&lock->l_enqueued_time);
+        /* policies are not executed on the client or during replay */
+        if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
+            && !local && ns->ns_policy) {
+                rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
+                                   NULL);
+                if (rc == ELDLM_LOCK_REPLACED) {
+                        /* The lock that was returned has already been granted,
+                         * and placed into lockp.  If it's not the same as the
+                         * one we passed in, then destroy the old one and our
+                         * work here is done. */
+                        if (lock != *lockp) {
+                                ldlm_lock_destroy(lock);
+                                LDLM_LOCK_PUT(lock);
+                        }
                         *flags |= LDLM_FL_LOCK_CHANGED;
-                } else if (rc == ELDLM_LOCK_ABORTED) {
+                        RETURN(0);
+                } else if (rc != ELDLM_OK ||
+                           (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
                         ldlm_lock_destroy(lock);
                         RETURN(rc);
                 }
         }
 
-        lock->l_cookie = cookie;
-        lock->l_cookie_len = cookie_len;
-
-        l_lock(&res->lr_namespace->ns_lock);
+        lock_res_and_lock(lock);
         if (local && lock->l_req_mode == lock->l_granted_mode) {
                 /* The server returned a blocked lock, but it was granted before
                  * we got a chance to actually enqueue it.  We don't need to do
                  * anything else. */
-                *flags &= ~(LDLM_FL_BLOCK_GRANTED | 
-                          LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
+                *flags &= ~(LDLM_FL_BLOCK_GRANTED |
+                            LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
                 GOTO(out, ELDLM_OK);
         }
 
+        /* Some flags from the enqueue want to make it into the AST, via the
+         * lock's l_flags. */
+        lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
+
         /* This distinction between local lock trees is very important; a client
          * namespace only has information about locks taken by that client, and
          * thus doesn't have enough information to decide for itself if it can
          * be granted (below).  In this case, we do exactly what the server
-         * tells us to do, as dictated by the 'flags' */
+         * tells us to do, as dictated by the 'flags'.
+         *
+         * We do exactly the same thing during recovery, when the server is
+         * more or less trusting the clients not to lie.
+         *
+         * FIXME (bug 268): Detect obvious lies by checking compatibility in
+         * granted/converting queues. */
         ldlm_resource_unlink_lock(lock);
         if (local) {
                 if (*flags & LDLM_FL_BLOCK_CONV)
-                        ldlm_resource_add_lock(res, res->lr_converting.prev,
-                                               lock);
+                        ldlm_resource_add_lock(res, &res->lr_converting, lock);
                 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
-                        ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
+                        ldlm_resource_add_lock(res, &res->lr_waiting, lock);
                 else
-                        ldlm_grant_lock(lock);
-                GOTO(out, ELDLM_OK);
-        }
-
-        /* FIXME: We may want to optimize by checking lr_most_restr */
-        if (!list_empty(&res->lr_converting)) {
-                ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
-                *flags |= LDLM_FL_BLOCK_CONV;
-                GOTO(out, ELDLM_OK);
-        }
-        if (!list_empty(&res->lr_waiting)) {
-                ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
-                *flags |= LDLM_FL_BLOCK_WAIT;
-                GOTO(out, ELDLM_OK);
-        }
-        if (!ldlm_lock_compat(lock, 0)) {
-                ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
-                *flags |= LDLM_FL_BLOCK_GRANTED;
+                        ldlm_grant_lock(lock, NULL);
                 GOTO(out, ELDLM_OK);
+        } else if (*flags & LDLM_FL_REPLAY) {
+                if (*flags & LDLM_FL_BLOCK_CONV) {
+                        ldlm_resource_add_lock(res, &res->lr_converting, lock);
+                        GOTO(out, ELDLM_OK);
+                } else if (*flags & LDLM_FL_BLOCK_WAIT) {
+                        ldlm_resource_add_lock(res, &res->lr_waiting, lock);
+                        GOTO(out, ELDLM_OK);
+                } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
+                        ldlm_grant_lock(lock, NULL);
+                        GOTO(out, ELDLM_OK);
+                }
+                /* If no flags, fall through to normal enqueue path. */
         }
 
-        ldlm_grant_lock(lock);
-        EXIT;
-      out:
-        l_unlock(&res->lr_namespace->ns_lock);
-        /* Don't set 'completion_ast' until here so that if the lock is granted
-         * immediately we don't do an unnecessary completion call. */
-        lock->l_completion_ast = completion;
-        return ELDLM_OK;
+        policy = ldlm_processing_policy_table[res->lr_type];
+        policy(lock, flags, 1, &rc, NULL);
+        GOTO(out, rc);
+out:
+        unlock_res_and_lock(lock);
+        return rc;
 }
 
 /* Must be called with namespace taken: queue is waiting or converting. */
-static int ldlm_reprocess_queue(struct ldlm_resource *res,
-                                struct list_head *queue)
+int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
+                         struct list_head *work_list)
 {
         struct list_head *tmp, *pos;
+        ldlm_processing_policy policy;
+        int flags;
+        int rc = LDLM_ITER_CONTINUE;
+        ldlm_error_t err;
         ENTRY;
 
+        check_res_locked(res);
+
+        policy = ldlm_processing_policy_table[res->lr_type];
+        LASSERT(policy);
+
         list_for_each_safe(tmp, pos, queue) {
                 struct ldlm_lock *pending;
                 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
 
                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
 
-                if (!ldlm_lock_compat(pending, 1))
-                        RETURN(1);
-
-                list_del_init(&pending->l_res_link);
-                ldlm_grant_lock(pending);
+                flags = 0;
+                rc = policy(pending, &flags, 0, &err, work_list);
+                if (rc != LDLM_ITER_CONTINUE)
+                        break;
         }
 
-        RETURN(0);
+        RETURN(rc);
 }
 
-void ldlm_run_ast_work(struct list_head *rpc_list)
+int ldlm_run_bl_ast_work(struct list_head *rpc_list)
 {
         struct list_head *tmp, *pos;
-        int rc;
+        struct ldlm_lock_desc d;
+        int rc = 0, retval = 0;
         ENTRY;
 
         list_for_each_safe(tmp, pos, rpc_list) {
-                struct ldlm_ast_work *w =
-                        list_entry(tmp, struct ldlm_ast_work, w_list);
+                struct ldlm_lock *lock =
+                        list_entry(tmp, struct ldlm_lock, l_bl_ast);
+
+                /* nobody should touch l_bl_ast */
+                lock_res_and_lock(lock);
+                list_del_init(&lock->l_bl_ast);
+
+                LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
+                LASSERT(lock->l_bl_ast_run == 0);
+                LASSERT(lock->l_blocking_lock);
+                lock->l_bl_ast_run++;
+                unlock_res_and_lock(lock);
+
+                ldlm_lock2desc(lock->l_blocking_lock, &d);
+
+                LDLM_LOCK_PUT(lock->l_blocking_lock);
+                lock->l_blocking_lock = NULL;
+                rc = lock->l_blocking_ast(lock, &d, NULL, LDLM_CB_BLOCKING);
+
+                if (rc == -ERESTART)
+                        retval = rc;
+                else if (rc)
+                        CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
+                               "disconnect client\n");
+                LDLM_LOCK_PUT(lock);
+        }
+        RETURN(retval);
+}
 
-                if (w->w_blocking)
-                        rc = w->w_lock->l_blocking_ast
-                                (w->w_lock, &w->w_desc, w->w_data,
-                                 w->w_datalen, LDLM_CB_BLOCKING);
-                else
-                        rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
-                if (rc)
-                        CERROR("Failed AST - should clean & disconnect "
-                               "client\n");
-                LDLM_LOCK_PUT(w->w_lock);
-                list_del(&w->w_list);
-                OBD_FREE(w, sizeof(*w));
+int ldlm_run_cp_ast_work(struct list_head *rpc_list)
+{
+        struct list_head *tmp, *pos;
+        int rc = 0, retval = 0;
+        ENTRY;
+
+        /* It's possible to receive a completion AST before we've set
+         * the l_completion_ast pointer: either because the AST arrived
+         * before the reply, or simply because there's a small race
+         * window between receiving the reply and finishing the local
+         * enqueue. (bug 842)
+         *
+         * This can't happen with the blocking_ast, however, because we
+         * will never call the local blocking_ast until we drop our
+         * reader/writer reference, which we won't do until we get the
+         * reply and finish enqueueing. */
+
+        list_for_each_safe(tmp, pos, rpc_list) {
+                struct ldlm_lock *lock =
+                        list_entry(tmp, struct ldlm_lock, l_cp_ast);
+
+                /* nobody should touch l_cp_ast */
+                lock_res_and_lock(lock);
+                list_del_init(&lock->l_cp_ast);
+                LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
+                lock->l_flags &= ~LDLM_FL_CP_REQD;
+                unlock_res_and_lock(lock);
+
+                if (lock->l_completion_ast != NULL)
+                        rc = lock->l_completion_ast(lock, 0, 0);
+                if (rc == -ERESTART)
+                        retval = rc;
+                else if (rc)
+                        CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
+                               "disconnect client\n");
+                LDLM_LOCK_PUT(lock);
+        }
+        RETURN(retval);
+}
+
+static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
+{
+        ldlm_reprocess_all(res);
+        return LDLM_ITER_CONTINUE;
+}
+
+void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
+{
+        struct list_head *tmp;
+        int i, rc;
+
+        if (ns == NULL)
+                return;
+
+        ENTRY;
+        spin_lock(&ns->ns_hash_lock);
+        for (i = 0; i < RES_HASH_SIZE; i++) {
+                tmp = ns->ns_hash[i].next;
+                while (tmp != &(ns->ns_hash[i])) {
+                        struct ldlm_resource *res =
+                                list_entry(tmp, struct ldlm_resource, lr_hash);
+
+                        ldlm_resource_getref(res);
+                        spin_unlock(&ns->ns_hash_lock);
+
+                        rc = reprocess_one_queue(res, NULL);
+
+                        spin_lock(&ns->ns_hash_lock);
+                        tmp = tmp->next;
+                        ldlm_resource_putref_locked(res);
+
+                        if (rc == LDLM_ITER_STOP)
+                                GOTO(out, rc);
+                }
         }
+ out:
+        spin_unlock(&ns->ns_hash_lock);
         EXIT;
 }
 
-/* Must be called with resource->lr_lock not taken. */
 void ldlm_reprocess_all(struct ldlm_resource *res)
 {
         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
+        int rc;
         ENTRY;
 
         /* Local lock trees don't get reprocessed. */
-        if (res->lr_namespace->ns_client) {
+        if (ns_is_client(res->lr_namespace)) {
                 EXIT;
                 return;
         }
 
-        l_lock(&res->lr_namespace->ns_lock);
-        res->lr_tmp = &rpc_list;
-
-        ldlm_reprocess_queue(res, &res->lr_converting);
-        if (list_empty(&res->lr_converting))
-                ldlm_reprocess_queue(res, &res->lr_waiting);
-
-        res->lr_tmp = NULL;
-        l_unlock(&res->lr_namespace->ns_lock);
-
-        ldlm_run_ast_work(&rpc_list);
+ restart:
+        lock_res(res);
+        rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
+        if (rc == LDLM_ITER_CONTINUE)
+                ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
+        unlock_res(res);
+
+        rc = ldlm_run_cp_ast_work(&rpc_list);
+        if (rc == -ERESTART) {
+                LASSERT(list_empty(&rpc_list));
+                goto restart;
+        }
         EXIT;
 }
 
 void ldlm_cancel_callback(struct ldlm_lock *lock)
 {
-        l_lock(&lock->l_resource->lr_namespace->ns_lock);
+        check_res_locked(lock->l_resource);
         if (!(lock->l_flags & LDLM_FL_CANCEL)) {
                 lock->l_flags |= LDLM_FL_CANCEL;
-                lock->l_blocking_ast(lock, NULL, lock->l_data,
-                                     lock->l_data_len, LDLM_CB_CANCELING);
+                if (lock->l_blocking_ast) {
+                        // l_check_no_ns_lock(ns);
+                        unlock_res_and_lock(lock);
+                        lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
+                                             LDLM_CB_CANCELING);
+                        lock_res_and_lock(lock);
+                } else {
+                        LDLM_DEBUG(lock, "no blocking ast");
+                }
+        }
+        lock->l_flags |= LDLM_FL_BL_DONE;
+}
+
+void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
+{
+        struct ldlm_lock *lock;
+
+        if (req->l_resource->lr_type != LDLM_PLAIN &&
+            req->l_resource->lr_type != LDLM_IBITS)
+                return;
+        
+        if (LDLM_SL_HEAD(&req->l_sl_mode)) {
+                lock = list_entry(req->l_res_link.next, struct ldlm_lock,
+                                  l_res_link);
+                if (req->l_sl_mode.next == &lock->l_sl_mode) {
+                        lock->l_sl_mode.prev = NULL;
+                } else {
+                        lock->l_sl_mode.next = req->l_sl_mode.next;
+                        lock->l_sl_mode.next->prev = &lock->l_sl_mode;
+                }
+                req->l_sl_mode.next = NULL;
+        } else if (LDLM_SL_TAIL(&req->l_sl_mode)) {
+                lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
+                                  l_res_link);
+                if (req->l_sl_mode.prev == &lock->l_sl_mode) {
+                        lock->l_sl_mode.next = NULL;
+                } else {
+                        lock->l_sl_mode.prev = req->l_sl_mode.prev;
+                        lock->l_sl_mode.prev->next = &lock->l_sl_mode;
+                }
+                req->l_sl_mode.prev = NULL;
+        }
+
+        if (LDLM_SL_HEAD(&req->l_sl_policy)) {
+                lock = list_entry(req->l_res_link.next, struct ldlm_lock,
+                                  l_res_link);
+                if (req->l_sl_policy.next == &lock->l_sl_policy) {
+                        lock->l_sl_policy.prev = NULL;
+                } else {
+                        lock->l_sl_policy.next = req->l_sl_policy.next;
+                        lock->l_sl_policy.next->prev = &lock->l_sl_policy;
+                }
+                req->l_sl_policy.next = NULL;
+        } else if (LDLM_SL_TAIL(&req->l_sl_policy)) {
+                lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
+                                  l_res_link);
+                if (req->l_sl_policy.prev == &lock->l_sl_policy) {
+                        lock->l_sl_policy.next = NULL;
+                } else {
+                        lock->l_sl_policy.prev = req->l_sl_policy.prev;
+                        lock->l_sl_policy.prev->next = &lock->l_sl_policy;
+                }
+                req->l_sl_policy.prev = NULL;
         }
-        l_unlock(&lock->l_resource->lr_namespace->ns_lock);
 }
 
 void ldlm_lock_cancel(struct ldlm_lock *lock)
@@ -830,37 +1489,77 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
         struct ldlm_namespace *ns;
         ENTRY;
 
+        lock_res_and_lock(lock);
+
         res = lock->l_resource;
         ns = res->lr_namespace;
 
-        l_lock(&ns->ns_lock);
-        if (lock->l_readers || lock->l_writers)
-                CDEBUG(D_INFO, "lock still has references (%d readers, %d "
-                       "writers)\n", lock->l_readers, lock->l_writers);
+        /* Please do not, no matter how tempting, remove this LBUG without
+         * talking to me first. -phik */
+        if (lock->l_readers || lock->l_writers) {
+                LDLM_ERROR(lock, "lock still has references");
+                LBUG();
+        }
+
+        ldlm_del_waiting_lock(lock);
 
+        /* Releases cancel callback. */
         ldlm_cancel_callback(lock);
 
-        ldlm_del_waiting_lock(lock);
+        /* Yes, second time, just in case it was added again while we were
+           running with no res lock in ldlm_cancel_callback */
+        ldlm_del_waiting_lock(lock); 
         ldlm_resource_unlink_lock(lock);
-        ldlm_lock_destroy(lock);
-        l_unlock(&ns->ns_lock);
+        ldlm_lock_destroy_nolock(lock);
+
+        if (lock->l_granted_mode == lock->l_req_mode)
+                ldlm_pool_del(&ns->ns_pool, lock);
+
+        /* Make sure we will not be called again for same lock what is possible
+         * if not to zero out lock->l_granted_mode */
+        lock->l_granted_mode = 0;
+        unlock_res_and_lock(lock);
+
         EXIT;
 }
 
+int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
+{
+        struct ldlm_lock *lock = ldlm_handle2lock(lockh);
+        ENTRY;
+
+        if (lock == NULL)
+                RETURN(-EINVAL);
+
+        lock->l_ast_data = data;
+        LDLM_LOCK_PUT(lock);
+        RETURN(0);
+}
+
 void ldlm_cancel_locks_for_export(struct obd_export *exp)
 {
-        struct list_head *iter, *n; /* MUST BE CALLED "n"! */
+        struct ldlm_lock *lock;
+        struct ldlm_resource *res;
 
-        list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
-                struct ldlm_lock *lock;
-                struct ldlm_resource *res;
-                lock = list_entry(iter, struct ldlm_lock, l_export_chain);
+        spin_lock(&exp->exp_ldlm_data.led_lock);
+        while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
+                lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
+                                  struct ldlm_lock, l_export_chain);
                 res = ldlm_resource_getref(lock->l_resource);
-                LDLM_DEBUG(lock, "cancelling lock for export %p", exp);
+                LDLM_LOCK_GET(lock);
+                spin_unlock(&exp->exp_ldlm_data.led_lock);
+
+                LDLM_DEBUG(lock, "export %p", exp);
+                ldlm_res_lvbo_update(res, NULL, 0, 1);
+
                 ldlm_lock_cancel(lock);
                 ldlm_reprocess_all(res);
-                ldlm_resource_put(res);
+
+                ldlm_resource_putref(res);
+                LDLM_LOCK_PUT(lock);
+                spin_lock(&exp->exp_ldlm_data.led_lock);
         }
+        spin_unlock(&exp->exp_ldlm_data.led_lock);
 }
 
 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
@@ -870,83 +1569,288 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         struct ldlm_resource *res;
         struct ldlm_namespace *ns;
         int granted = 0;
+        int old_mode, rc;
+        struct ldlm_lock *mark_lock = NULL;
+        int join = LDLM_JOIN_NONE;
+        ldlm_error_t err;
         ENTRY;
 
+        if (new_mode == lock->l_granted_mode) { // No changes? Just return.
+                *flags |= LDLM_FL_BLOCK_GRANTED;
+                RETURN(lock->l_resource);
+        }
+
+        LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
+                 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
+
+        lock_res_and_lock(lock);
+
         res = lock->l_resource;
         ns = res->lr_namespace;
 
-        l_lock(&ns->ns_lock);
-
+        old_mode = lock->l_req_mode;
         lock->l_req_mode = new_mode;
+        if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
+                /* remember the lock position where the lock might be 
+                 * added back to the granted list later and also 
+                 * remember the join mode for skiplist fixing. */
+                if (LDLM_SL_HEAD(&lock->l_sl_mode))
+                        join = LDLM_MODE_JOIN_RIGHT;
+                else if (LDLM_SL_TAIL(&lock->l_sl_mode))
+                        join = LDLM_MODE_JOIN_LEFT;
+                if (LDLM_SL_HEAD(&lock->l_sl_policy))
+                        join |= LDLM_POLICY_JOIN_RIGHT;
+                else if (LDLM_SL_TAIL(&lock->l_sl_policy))
+                        join |= LDLM_POLICY_JOIN_LEFT;
+
+                LASSERT(!((join & LDLM_MODE_JOIN_RIGHT) &&
+                          (join & LDLM_POLICY_JOIN_LEFT)));
+                LASSERT(!((join & LDLM_MODE_JOIN_LEFT) &&
+                          (join & LDLM_POLICY_JOIN_RIGHT)));
+
+                if ((join & LDLM_MODE_JOIN_LEFT) ||
+                    (join & LDLM_POLICY_JOIN_LEFT))
+                        mark_lock = list_entry(lock->l_res_link.prev,
+                                               struct ldlm_lock, l_res_link);
+                else if (lock->l_res_link.next != &res->lr_granted)
+                        mark_lock = list_entry(lock->l_res_link.next,
+                                               struct ldlm_lock, l_res_link);
+        }
         ldlm_resource_unlink_lock(lock);
 
         /* If this is a local resource, put it on the appropriate list. */
-        if (res->lr_namespace->ns_client) {
-                if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
-                        ldlm_resource_add_lock(res, res->lr_converting.prev,
-                                               lock);
-                else {
+        if (ns_is_client(res->lr_namespace)) {
+                if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
+                        ldlm_resource_add_lock(res, &res->lr_converting, lock);
+                } else {
                         /* This should never happen, because of the way the
                          * server handles conversions. */
+                        LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
+                                   *flags);
                         LBUG();
 
-                        res->lr_tmp = &rpc_list;
-                        ldlm_grant_lock(lock);
-                        res->lr_tmp = NULL;
+                        ldlm_grant_lock(lock, &rpc_list);
                         granted = 1;
                         /* FIXME: completion handling not with ns_lock held ! */
                         if (lock->l_completion_ast)
-                                lock->l_completion_ast(lock, 0);
+                                lock->l_completion_ast(lock, 0, NULL);
                 }
         } else {
-                /* FIXME: We should try the conversion right away and possibly
-                 * return success without the need for an extra AST */
-                ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
-                *flags |= LDLM_FL_BLOCK_CONV;
+                int pflags = 0;
+                ldlm_processing_policy policy;
+                policy = ldlm_processing_policy_table[res->lr_type];
+                rc = policy(lock, &pflags, 0, &err, &rpc_list);
+                if (rc == LDLM_ITER_STOP) {
+                        lock->l_req_mode = old_mode;
+                        ldlm_granted_list_add_lock(lock, mark_lock, join);
+                        res = NULL;
+                } else {
+                        *flags |= LDLM_FL_BLOCK_GRANTED;
+                        granted = 1;
+                }
         }
-
-        l_unlock(&ns->ns_lock);
+        unlock_res_and_lock(lock);
 
         if (granted)
-                ldlm_run_ast_work(&rpc_list);
+                ldlm_run_cp_ast_work(&rpc_list);
         RETURN(res);
 }
 
-void ldlm_lock_dump(struct ldlm_lock *lock)
+void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
 {
-        char ver[128];
+        struct obd_device *obd = NULL;
 
-        if (!(portal_debug & D_OTHER))
+        if (!((libcfs_debug | D_ERROR) & level))
                 return;
 
-        if (RES_VERSION_SIZE != 4)
-                LBUG();
-
         if (!lock) {
-                CDEBUG(D_OTHER, "  NULL LDLM lock\n");
+                CDEBUG(level, "  NULL LDLM lock\n");
                 return;
         }
 
-        snprintf(ver, sizeof(ver), "%x %x %x %x",
-                 lock->l_version[0], lock->l_version[1],
-                 lock->l_version[2], lock->l_version[3]);
-
-        CDEBUG(D_OTHER, "  -- Lock dump: %p (%s)\n", lock, ver);
-        if (lock->l_export && lock->l_export->exp_connection)
-                CDEBUG(D_OTHER, "  Node: NID %x (rhandle: "LPX64")\n",
-                       lock->l_export->exp_connection->c_peer.peer_nid,
-                       lock->l_remote_handle.addr);
-        else
-                CDEBUG(D_OTHER, "  Node: local\n");
-        CDEBUG(D_OTHER, "  Parent: %p\n", lock->l_parent);
-        CDEBUG(D_OTHER, "  Resource: %p ("LPD64")\n", lock->l_resource,
-               lock->l_resource->lr_name[0]);
-        CDEBUG(D_OTHER, "  Requested mode: %d, granted mode: %d\n",
-               (int)lock->l_req_mode, (int)lock->l_granted_mode);
-        CDEBUG(D_OTHER, "  Readers: %u ; Writers; %u\n",
-               lock->l_readers, lock->l_writers);
+        CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
+               lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+               pos, lock->l_pid);
+        if (lock->l_conn_export != NULL)
+                obd = lock->l_conn_export->exp_obd;
+        if (lock->l_export && lock->l_export->exp_connection) {
+                CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
+                     libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
+                     lock->l_remote_handle.cookie);
+        } else if (obd == NULL) {
+                CDEBUG(level, "  Node: local\n");
+        } else {
+                struct obd_import *imp = obd->u.cli.cl_import;
+                CDEBUG(level, "  Node: NID %s (rhandle: "LPX64")\n",
+                       libcfs_nid2str(imp->imp_connection->c_peer.nid),
+                       lock->l_remote_handle.cookie);
+        }
+        CDEBUG(level, "  Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
+                  lock->l_resource,
+                  lock->l_resource->lr_name.name[0],
+                  lock->l_resource->lr_name.name[1],
+                  lock->l_resource->lr_name.name[2]);
+        CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
+               "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
+               ldlm_lockname[lock->l_granted_mode],
+               atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
+               lock->l_flags);
         if (lock->l_resource->lr_type == LDLM_EXTENT)
-                CDEBUG(D_OTHER, "  Extent: %Lu -> %Lu\n",
-                       (unsigned long long)lock->l_extent.start,
-                       (unsigned long long)lock->l_extent.end);
+                CDEBUG(level, "  Extent: "LPU64" -> "LPU64
+                       " (req "LPU64"-"LPU64")\n",
+                       lock->l_policy_data.l_extent.start,
+                       lock->l_policy_data.l_extent.end,
+                       lock->l_req_extent.start, lock->l_req_extent.end);
+        else if (lock->l_resource->lr_type == LDLM_FLOCK)
+                CDEBUG(level, "  Pid: %d Extent: "LPU64" -> "LPU64"\n",
+                       lock->l_policy_data.l_flock.pid,
+                       lock->l_policy_data.l_flock.start,
+                       lock->l_policy_data.l_flock.end);
+       else if (lock->l_resource->lr_type == LDLM_IBITS)
+                CDEBUG(level, "  Bits: "LPX64"\n",
+                       lock->l_policy_data.l_inodebits.bits);
+}
+
+void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
+{
+        struct ldlm_lock *lock;
+
+        if (!((libcfs_debug | D_ERROR) & level))
+                return;
+
+        lock = ldlm_handle2lock(lockh);
+        if (lock == NULL)
+                return;
+
+        ldlm_lock_dump(D_OTHER, lock, 0);
+
+        LDLM_LOCK_PUT(lock);
+}
+
+void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
+                     struct libcfs_debug_msg_data *data, const char *fmt,
+                      ...)
+{
+        va_list args;
+        cfs_debug_limit_state_t *cdls = data->msg_cdls;
+        
+        va_start(args, fmt);
+
+        if (lock->l_resource == NULL) {
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                                   data->msg_fn, data->msg_line, fmt, args,
+                       " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
+                       LPX64" expref: %d pid: %u\n", lock,
+                       lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+                       lock->l_readers, lock->l_writers,
+                       ldlm_lockname[lock->l_granted_mode],
+                       ldlm_lockname[lock->l_req_mode],
+                       lock->l_flags, lock->l_remote_handle.cookie,
+                       lock->l_export ?
+                       atomic_read(&lock->l_export->exp_refcount) : -99,
+                       lock->l_pid);
+                va_end(args);
+                return;
+        }
+
+        switch (lock->l_resource->lr_type) {
+        case LDLM_EXTENT:
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                                   data->msg_fn, data->msg_line, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
+                       "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
+                       " expref: %d pid: %u\n",
+                       lock->l_resource->lr_namespace->ns_name, lock,
+                       lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+                       lock->l_readers, lock->l_writers,
+                       ldlm_lockname[lock->l_granted_mode],
+                       ldlm_lockname[lock->l_req_mode],
+                       lock->l_resource->lr_name.name[0],
+                       lock->l_resource->lr_name.name[1],
+                       atomic_read(&lock->l_resource->lr_refcount),
+                       ldlm_typename[lock->l_resource->lr_type],
+                       lock->l_policy_data.l_extent.start,
+                       lock->l_policy_data.l_extent.end,
+                       lock->l_req_extent.start, lock->l_req_extent.end,
+                       lock->l_flags, lock->l_remote_handle.cookie,
+                       lock->l_export ?
+                       atomic_read(&lock->l_export->exp_refcount) : -99,
+                       lock->l_pid);
+                break;
+
+        case LDLM_FLOCK:
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                                   data->msg_fn, data->msg_line, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
+                       "["LPU64"->"LPU64"] flags: %x remote: "LPX64
+                       " expref: %d pid: %u\n",
+                       lock->l_resource->lr_namespace->ns_name, lock,
+                       lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
+                       lock->l_readers, lock->l_writers,
+                       ldlm_lockname[lock->l_granted_mode],
+                       ldlm_lockname[lock->l_req_mode],
+                       lock->l_resource->lr_name.name[0],
+                       lock->l_resource->lr_name.name[1],
+                       atomic_read(&lock->l_resource->lr_refcount),
+                       ldlm_typename[lock->l_resource->lr_type],
+                       lock->l_policy_data.l_flock.pid,
+                       lock->l_policy_data.l_flock.start,
+                       lock->l_policy_data.l_flock.end,
+                       lock->l_flags, lock->l_remote_handle.cookie,
+                       lock->l_export ?
+                       atomic_read(&lock->l_export->exp_refcount) : -99,
+                       lock->l_pid);
+                break;
+
+        case LDLM_IBITS:
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                                   data->msg_fn, data->msg_line, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
+                       "flags: %x remote: "LPX64" expref: %d "
+                       "pid %u\n",
+                       lock->l_resource->lr_namespace->ns_name,
+                       lock, lock->l_handle.h_cookie,
+                       atomic_read (&lock->l_refc),
+                       lock->l_readers, lock->l_writers,
+                       ldlm_lockname[lock->l_granted_mode],
+                       ldlm_lockname[lock->l_req_mode],
+                       lock->l_resource->lr_name.name[0],
+                       lock->l_resource->lr_name.name[1],
+                       lock->l_policy_data.l_inodebits.bits,
+                       atomic_read(&lock->l_resource->lr_refcount),
+                       ldlm_typename[lock->l_resource->lr_type],
+                       lock->l_flags, lock->l_remote_handle.cookie,
+                       lock->l_export ?
+                       atomic_read(&lock->l_export->exp_refcount) : -99,
+                       lock->l_pid);
+                break;
+
+        default:
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                                   data->msg_fn, data->msg_line, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
+                       "remote: "LPX64" expref: %d pid: %u\n",
+                       lock->l_resource->lr_namespace->ns_name,
+                       lock, lock->l_handle.h_cookie,
+                       atomic_read (&lock->l_refc),
+                       lock->l_readers, lock->l_writers,
+                       ldlm_lockname[lock->l_granted_mode],
+                       ldlm_lockname[lock->l_req_mode],
+                       lock->l_resource->lr_name.name[0],
+                       lock->l_resource->lr_name.name[1],
+                       atomic_read(&lock->l_resource->lr_refcount),
+                       ldlm_typename[lock->l_resource->lr_type],
+                       lock->l_flags, lock->l_remote_handle.cookie,
+                       lock->l_export ?
+                       atomic_read(&lock->l_export->exp_refcount) : -99,
+                       lock->l_pid);
+                break;
+        }
+        va_end(args);
 }
+EXPORT_SYMBOL(_ldlm_lock_debug);