Whamcloud - gitweb
LU-13456 ldlm: fix reprocessing of locks with more bits
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.c
index 834b68f..d9b1ca2 100644 (file)
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2010, 2015, Intel Corporation.
+ * Copyright (c) 2010, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
- * Lustre is a trademark of Sun Microsystems, Inc.
  *
  * lustre/ldlm/ldlm_lock.c
  *
@@ -44,6 +43,9 @@
 
 #include "ldlm_internal.h"
 
+struct kmem_cache *ldlm_glimpse_work_kmem;
+EXPORT_SYMBOL(ldlm_glimpse_work_kmem);
+
 /* lock types */
 char *ldlm_lockname[] = {
        [0] = "--",
@@ -122,8 +124,6 @@ const char *ldlm_it2str(enum ldlm_intent_flags it)
                return "getattr";
        case IT_LOOKUP:
                return "lookup";
-       case IT_UNLINK:
-               return "unlink";
        case IT_GETXATTR:
                return "getxattr";
        case IT_LAYOUT:
@@ -135,8 +135,6 @@ const char *ldlm_it2str(enum ldlm_intent_flags it)
 }
 EXPORT_SYMBOL(ldlm_it2str);
 
-extern struct kmem_cache *ldlm_lock_slab;
-
 #ifdef HAVE_SERVER_SUPPORT
 static ldlm_processing_policy ldlm_processing_policy_table[] = {
        [LDLM_PLAIN]    = ldlm_process_plain_lock,
@@ -150,6 +148,19 @@ ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
         return ldlm_processing_policy_table[res->lr_type];
 }
 EXPORT_SYMBOL(ldlm_get_processing_policy);
+
+static ldlm_reprocessing_policy ldlm_reprocessing_policy_table[] = {
+       [LDLM_PLAIN]    = ldlm_reprocess_queue,
+       [LDLM_EXTENT]   = ldlm_reprocess_queue,
+       [LDLM_FLOCK]    = ldlm_reprocess_queue,
+       [LDLM_IBITS]    = ldlm_reprocess_inodebits_queue,
+};
+
+ldlm_reprocessing_policy ldlm_get_reprocessing_policy(struct ldlm_resource *res)
+{
+       return ldlm_reprocessing_policy_table[res->lr_type];
+}
+
 #endif /* HAVE_SERVER_SUPPORT */
 
 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
@@ -173,11 +184,20 @@ EXPORT_SYMBOL(ldlm_register_intent);
  */
 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
 {
-       atomic_inc(&lock->l_refc);
+       refcount_inc(&lock->l_handle.h_ref);
         return lock;
 }
 EXPORT_SYMBOL(ldlm_lock_get);
 
+static void lock_handle_free(struct rcu_head *rcu)
+{
+       struct ldlm_lock *lock = container_of(rcu, struct ldlm_lock,
+                                             l_handle.h_rcu);
+
+       OBD_FREE_PRE(lock, sizeof(*lock), "slab-freed");
+       kmem_cache_free(ldlm_lock_slab, lock);
+}
+
 /**
  * Release lock reference.
  *
@@ -188,8 +208,8 @@ void ldlm_lock_put(struct ldlm_lock *lock)
         ENTRY;
 
         LASSERT(lock->l_resource != LP_POISON);
-       LASSERT(atomic_read(&lock->l_refc) > 0);
-       if (atomic_dec_and_test(&lock->l_refc)) {
+       LASSERT(refcount_read(&lock->l_handle.h_ref) > 0);
+       if (refcount_dec_and_test(&lock->l_handle.h_ref)) {
                 struct ldlm_resource *res;
 
                 LDLM_DEBUG(lock,
@@ -204,8 +224,6 @@ void ldlm_lock_put(struct ldlm_lock *lock)
                 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
                                      LDLM_NSS_LOCKS);
                 lu_ref_del(&res->lr_reference, "lock", lock);
-                ldlm_resource_putref(res);
-                lock->l_resource = NULL;
                 if (lock->l_export) {
                         class_export_lock_put(lock->l_export, lock);
                         lock->l_export = NULL;
@@ -214,9 +232,17 @@ void ldlm_lock_put(struct ldlm_lock *lock)
                 if (lock->l_lvb_data != NULL)
                         OBD_FREE_LARGE(lock->l_lvb_data, lock->l_lvb_len);
 
-                ldlm_interval_free(ldlm_interval_detach(lock));
+               if (res->lr_type == LDLM_EXTENT) {
+                       ldlm_interval_free(ldlm_interval_detach(lock));
+               } else if (res->lr_type == LDLM_IBITS) {
+                       if (lock->l_ibits_node != NULL)
+                               OBD_SLAB_FREE_PTR(lock->l_ibits_node,
+                                                 ldlm_inodebits_slab);
+               }
+               ldlm_resource_putref(res);
+               lock->l_resource = NULL;
                 lu_ref_fini(&lock->l_reference);
-               OBD_FREE_RCU(lock, sizeof(*lock), &lock->l_handle);
+               call_rcu(&lock->l_handle.h_rcu, lock_handle_free);
         }
 
         EXIT;
@@ -233,6 +259,8 @@ int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
                struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
 
                LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
+               if (ns->ns_last_pos == &lock->l_lru)
+                       ns->ns_last_pos = lock->l_lru.prev;
                list_del_init(&lock->l_lru);
                LASSERT(ns->ns_nr_unused > 0);
                ns->ns_nr_unused--;
@@ -252,7 +280,7 @@ int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
  *           otherwise, the lock hasn't been in the LRU list.
  * \retval 1 the lock was in LRU list and removed.
  */
-int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, cfs_time_t last_use)
+int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, ktime_t last_use)
 {
        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
        int rc = 0;
@@ -264,7 +292,8 @@ int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, cfs_time_t last_use)
        }
 
        spin_lock(&ns->ns_lock);
-       if (last_use == 0 || last_use == lock->l_last_used)
+       if (!ktime_compare(last_use, ktime_set(0, 0)) ||
+           !ktime_compare(last_use, lock->l_last_used))
                rc = ldlm_lock_remove_from_lru_nolock(lock);
        spin_unlock(&ns->ns_lock);
 
@@ -278,11 +307,10 @@ void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
 {
        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
 
-       lock->l_last_used = cfs_time_current();
+       lock->l_last_used = ktime_get();
        LASSERT(list_empty(&lock->l_lru));
        LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
        list_add_tail(&lock->l_lru, &ns->ns_unused_list);
-       ldlm_clear_skipped(lock);
        LASSERT(ns->ns_nr_unused >= 0);
        ns->ns_nr_unused++;
 }
@@ -418,22 +446,7 @@ void ldlm_lock_destroy_nolock(struct ldlm_lock *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);
-}
-
-static void lock_handle_free(void *lock, int size)
-{
-       LASSERT(size == sizeof(struct ldlm_lock));
-       OBD_SLAB_FREE(lock, ldlm_lock_slab, size);
-}
-
-static struct portals_handle_ops lock_handle_ops = {
-       .hop_addref = lock_handle_addref,
-       .hop_free   = lock_handle_free,
-};
+static const char lock_handle_owner[] = "ldlm";
 
 /**
  *
@@ -455,11 +468,10 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
        if (lock == NULL)
                RETURN(NULL);
 
-       spin_lock_init(&lock->l_lock);
-       lock->l_resource = resource;
+       RCU_INIT_POINTER(lock->l_resource, resource);
        lu_ref_add(&resource->lr_reference, "lock", lock);
 
-       atomic_set(&lock->l_refc, 2);
+       refcount_set(&lock->l_handle.h_ref, 2);
        INIT_LIST_HEAD(&lock->l_res_link);
        INIT_LIST_HEAD(&lock->l_lru);
        INIT_LIST_HEAD(&lock->l_pending_chain);
@@ -473,23 +485,24 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
        INIT_HLIST_NODE(&lock->l_exp_hash);
        INIT_HLIST_NODE(&lock->l_exp_flock_hash);
 
-        lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
-                             LDLM_NSS_LOCKS);
-       INIT_LIST_HEAD(&lock->l_handle.h_link);
-       class_handle_hash(&lock->l_handle, &lock_handle_ops);
+       lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
+                            LDLM_NSS_LOCKS);
+       INIT_HLIST_NODE(&lock->l_handle.h_link);
+       class_handle_hash(&lock->l_handle, lock_handle_owner);
 
-        lu_ref_init(&lock->l_reference);
-        lu_ref_add(&lock->l_reference, "hash", lock);
-        lock->l_callback_timeout = 0;
+       lu_ref_init(&lock->l_reference);
+       lu_ref_add(&lock->l_reference, "hash", lock);
+       lock->l_callback_timestamp = 0;
+       lock->l_activity = 0;
 
 #if LUSTRE_TRACKS_LOCK_EXP_REFS
        INIT_LIST_HEAD(&lock->l_exp_refs_link);
-        lock->l_exp_refs_nr = 0;
-        lock->l_exp_refs_target = NULL;
+       lock->l_exp_refs_nr = 0;
+       lock->l_exp_refs_target = NULL;
 #endif
        INIT_LIST_HEAD(&lock->l_exp_list);
 
-        RETURN(lock);
+       RETURN(lock);
 }
 
 /**
@@ -500,16 +513,16 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
                               const struct ldlm_res_id *new_resid)
 {
-        struct ldlm_resource *oldres = lock->l_resource;
+       struct ldlm_resource *oldres;
         struct ldlm_resource *newres;
         int type;
         ENTRY;
 
         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) {
+       oldres = lock_res_and_lock(lock);
+       if (memcmp(new_resid, &oldres->lr_name,
+                  sizeof(oldres->lr_name)) == 0) {
                 /* Nothing to do */
                 unlock_res_and_lock(lock);
                 RETURN(0);
@@ -529,12 +542,13 @@ int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
 
         lu_ref_add(&newres->lr_reference, "lock", lock);
         /*
-         * To flip the lock from the old to the new resource, lock, oldres and
-         * newres have to be locked. Resource spin-locks are nested within
-         * lock->l_lock, and are taken in the memory address order to avoid
-         * dead-locks.
+        * To flip the lock from the old to the new resource, oldres
+        * and newres have to be locked. Resource spin-locks are taken
+        * in the memory address order to avoid dead-locks.
+        * As this is the only circumstance where ->l_resource
+        * can change, and this cannot race with itself, it is safe
+        * to access lock->l_resource without being careful about locking.
          */
-       spin_lock(&lock->l_lock);
         oldres = lock->l_resource;
         if (oldres < newres) {
                 lock_res(oldres);
@@ -545,9 +559,9 @@ int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
         }
         LASSERT(memcmp(new_resid, &oldres->lr_name,
                        sizeof oldres->lr_name) != 0);
-        lock->l_resource = newres;
+       rcu_assign_pointer(lock->l_resource, newres);
         unlock_res(oldres);
-        unlock_res_and_lock(lock);
+       unlock_res(newres);
 
         /* ...and the flowers are still standing! */
         lu_ref_del(&oldres->lr_reference, "lock", lock);
@@ -585,7 +599,10 @@ struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
 
        LASSERT(handle);
 
-       lock = class_handle2object(handle->cookie, NULL);
+       if (!lustre_handle_is_used(handle))
+               RETURN(NULL);
+
+       lock = class_handle2object(handle->cookie, lock_handle_owner);
        if (lock == NULL)
                RETURN(NULL);
 
@@ -599,7 +616,7 @@ struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
        /* It's unlikely but possible that someone marked the lock as
         * destroyed after we did handle2object on it */
        if ((flags == 0) && !ldlm_is_destroyed(lock)) {
-               lu_ref_add(&lock->l_reference, "handle", current);
+               lu_ref_add_atomic(&lock->l_reference, "handle", lock);
                RETURN(lock);
        }
 
@@ -607,7 +624,7 @@ struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
 
        LASSERT(lock->l_resource != NULL);
 
-       lu_ref_add_atomic(&lock->l_reference, "handle", current);
+       lu_ref_add_atomic(&lock->l_reference, "handle", lock);
        if (unlikely(ldlm_is_destroyed(lock))) {
                unlock_res_and_lock(lock);
                CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
@@ -661,12 +678,19 @@ static void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
                 * discard dirty data, rather than writing back. */
                if (ldlm_is_ast_discard_data(new))
                        ldlm_set_discard_data(lock);
-               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);
-        }
+
+               /* Lock can be converted from a blocking state back to granted
+                * after lock convert or COS downgrade but still be in an
+                * older bl_list because it is controlled only by
+                * ldlm_work_bl_ast_lock(), let it be processed there.
+                */
+               if (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);
+       }
 }
 
 /**
@@ -819,14 +843,15 @@ void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock,
  */
 void ldlm_lock_decref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
 {
-        struct ldlm_namespace *ns;
-        ENTRY;
+       struct ldlm_namespace *ns;
 
-        lock_res_and_lock(lock);
+       ENTRY;
+
+       lock_res_and_lock(lock);
 
-        ns = ldlm_lock_to_ns(lock);
+       ns = ldlm_lock_to_ns(lock);
 
-        ldlm_lock_decref_internal_nolock(lock, mode);
+       ldlm_lock_decref_internal_nolock(lock, mode);
 
        if ((ldlm_is_local(lock) || lock->l_req_mode == LCK_GROUP) &&
            !lock->l_readers && !lock->l_writers) {
@@ -843,51 +868,49 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
        }
 
        if (!lock->l_readers && !lock->l_writers && ldlm_is_cbpending(lock)) {
+               unsigned int mask = D_DLMTRACE;
+
                /* If we received a blocked AST and this was the last reference,
                 * run the callback. */
                if (ldlm_is_ns_srv(lock) && lock->l_export)
-                        CERROR("FL_CBPENDING set on non-local lock--just a "
-                               "warning\n");
-
-                LDLM_DEBUG(lock, "final decref done on cbpending lock");
+                       mask |= D_WARNING;
+               LDLM_DEBUG_LIMIT(mask, lock,
+                                "final decref done on %sCBPENDING lock",
+                                mask & D_WARNING ? "non-local " : "");
 
-                LDLM_LOCK_GET(lock); /* dropped by bl thread */
-                ldlm_lock_remove_from_lru(lock);
-                unlock_res_and_lock(lock);
+               LDLM_LOCK_GET(lock); /* dropped by bl thread */
+               ldlm_lock_remove_from_lru(lock);
+               unlock_res_and_lock(lock);
 
                if (ldlm_is_fail_loc(lock))
-                        OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
+                       OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
 
                if (ldlm_is_atomic_cb(lock) ||
                     ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
-                        ldlm_handle_bl_callback(ns, NULL, lock);
+                       ldlm_handle_bl_callback(ns, NULL, lock);
         } else if (ns_is_client(ns) &&
-                   !lock->l_readers && !lock->l_writers &&
+                  !lock->l_readers && !lock->l_writers &&
                   !ldlm_is_no_lru(lock) &&
-                  !ldlm_is_bl_ast(lock)) {
+                  !ldlm_is_bl_ast(lock) &&
+                  !ldlm_is_converting(lock)) {
 
-                LDLM_DEBUG(lock, "add lock into lru list");
-
-                /* 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);
+               /* 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);
+               LDLM_DEBUG(lock, "add lock into lru list");
 
                if (ldlm_is_fail_loc(lock))
-                        OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
-
-                /* 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) &&
-                    !ns_connect_lru_resize(ns))
-                       ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
-        } else {
-                LDLM_DEBUG(lock, "do not add lock into lru list");
-                unlock_res_and_lock(lock);
-        }
+                       OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
 
-        EXIT;
+               ldlm_pool_recalc(&ns->ns_pool, true);
+       } else {
+               LDLM_DEBUG(lock, "do not add lock into lru list");
+               unlock_res_and_lock(lock);
+       }
+
+       EXIT;
 }
 
 /**
@@ -1023,7 +1046,6 @@ static void search_granted_lock(struct list_head *queue,
         prev->mode_link = &req->l_sl_mode;
         prev->policy_link = &req->l_sl_policy;
         EXIT;
-        return;
 }
 
 /**
@@ -1068,16 +1090,14 @@ static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
  * Add a lock to granted list on a resource maintaining skiplist
  * correctness.
  */
-static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
+void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
 {
-        struct sl_insert_point prev;
-        ENTRY;
+       struct sl_insert_point prev;
 
-        LASSERT(lock->l_req_mode == lock->l_granted_mode);
+       LASSERT(ldlm_is_granted(lock));
 
-        search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
-        ldlm_granted_list_add_lock(lock, &prev);
-        EXIT;
+       search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
+       ldlm_granted_list_add_lock(lock, &prev);
 }
 
 /**
@@ -1087,7 +1107,6 @@ static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
  * NOTE: called by
  *  - ldlm_lock_enqueue
  *  - ldlm_reprocess_queue
- *  - ldlm_lock_convert
  *
  * must be called with lr_lock held
  */
@@ -1129,36 +1148,26 @@ void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
 }
 
 /**
- * Describe the overlap between two locks.  itree_overlap_cb data.
- */
-struct lock_match_data {
-       struct ldlm_lock        *lmd_old;
-       struct ldlm_lock        *lmd_lock;
-       enum ldlm_mode          *lmd_mode;
-       union ldlm_policy_data  *lmd_policy;
-       __u64                    lmd_flags;
-       int                      lmd_unref;
-};
-
-/**
  * Check if the given @lock meets the criteria for a match.
  * A reference on the lock is taken if matched.
  *
- * \param lock     test-against this lock
- * \param data    parameters
+ * @lock       test-against this lock
+ * @data       parameters
+ *
+ * RETURN      returns true if @lock matches @data, false otherwise
  */
-static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
+static bool lock_matches(struct ldlm_lock *lock, struct ldlm_match_data *data)
 {
        union ldlm_policy_data *lpol = &lock->l_policy_data;
-       enum ldlm_mode match;
+       enum ldlm_mode match = LCK_MINMODE;
 
        if (lock == data->lmd_old)
-               return INTERVAL_ITER_STOP;
+               return true;
 
        /* Check if this lock can be matched.
         * Used by LU-2919(exclusive open) for open lease lock */
        if (ldlm_is_excl(lock))
-               return INTERVAL_ITER_CONT;
+               return false;
 
        /* llite sometimes wants to match locks that will be
         * canceled when their users drop, but we allow it to match
@@ -1168,25 +1177,39 @@ static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
         * can still happen. */
        if (ldlm_is_cbpending(lock) &&
            !(data->lmd_flags & LDLM_FL_CBPENDING))
-               return INTERVAL_ITER_CONT;
-       if (!data->lmd_unref && ldlm_is_cbpending(lock) &&
+               return false;
+
+       if (!(data->lmd_match & LDLM_MATCH_UNREF) && ldlm_is_cbpending(lock) &&
            lock->l_readers == 0 && lock->l_writers == 0)
-               return INTERVAL_ITER_CONT;
+               return false;
 
        if (!(lock->l_req_mode & *data->lmd_mode))
-               return INTERVAL_ITER_CONT;
+               return false;
+
+       /* When we search for ast_data, we are not doing a traditional match,
+        * so we don't worry about IBITS or extent matching.
+        */
+       if (data->lmd_match & (LDLM_MATCH_AST | LDLM_MATCH_AST_ANY)) {
+               if (!lock->l_ast_data)
+                       return false;
+
+               if (data->lmd_match & LDLM_MATCH_AST_ANY)
+                       goto matched;
+       }
+
        match = lock->l_req_mode;
 
        switch (lock->l_resource->lr_type) {
        case LDLM_EXTENT:
-               if (lpol->l_extent.start > data->lmd_policy->l_extent.start ||
-                   lpol->l_extent.end < data->lmd_policy->l_extent.end)
-                       return INTERVAL_ITER_CONT;
+               if (!(data->lmd_match & LDLM_MATCH_RIGHT) &&
+                   (lpol->l_extent.start > data->lmd_policy->l_extent.start ||
+                    lpol->l_extent.end < data->lmd_policy->l_extent.end))
+                       return false;
 
                if (unlikely(match == LCK_GROUP) &&
                    data->lmd_policy->l_extent.gid != LDLM_GID_ANY &&
                    lpol->l_extent.gid != data->lmd_policy->l_extent.gid)
-                       return INTERVAL_ITER_CONT;
+                       return false;
                break;
        case LDLM_IBITS:
                /* We match if we have existing lock with same or wider set
@@ -1194,7 +1217,13 @@ static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
                if ((lpol->l_inodebits.bits &
                     data->lmd_policy->l_inodebits.bits) !=
                    data->lmd_policy->l_inodebits.bits)
-                       return INTERVAL_ITER_CONT;
+                       return false;
+
+               if (unlikely(match == LCK_GROUP) &&
+                   data->lmd_policy->l_inodebits.li_gid != LDLM_GID_ANY &&
+                   lpol->l_inodebits.li_gid !=
+                   data->lmd_policy->l_inodebits.li_gid)
+                       return false;
                break;
        default:
                ;
@@ -1202,12 +1231,17 @@ static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
 
        /* We match if we have existing lock with same or wider set
           of bits. */
-       if (!data->lmd_unref && LDLM_HAVE_MASK(lock, GONE))
-               return INTERVAL_ITER_CONT;
+       if (!(data->lmd_match & LDLM_MATCH_UNREF) && LDLM_HAVE_MASK(lock, GONE))
+               return false;
 
        if (!equi(data->lmd_flags & LDLM_FL_LOCAL_ONLY, ldlm_is_local(lock)))
-               return INTERVAL_ITER_CONT;
+               return false;
 
+       /* Filter locks by skipping flags */
+       if (data->lmd_skip_flags & lock->l_flags)
+               return false;
+
+matched:
        if (data->lmd_flags & LDLM_FL_TEST_LOCK) {
                LDLM_LOCK_GET(lock);
                ldlm_lock_touch_in_lru(lock);
@@ -1218,19 +1252,17 @@ static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
        *data->lmd_mode = match;
        data->lmd_lock = lock;
 
-       return INTERVAL_ITER_STOP;
+       return true;
 }
 
 static unsigned int itree_overlap_cb(struct interval_node *in, void *args)
 {
        struct ldlm_interval *node = to_ldlm_interval(in);
-       struct lock_match_data *data = args;
+       struct ldlm_match_data *data = args;
        struct ldlm_lock *lock;
-       int rc;
 
        list_for_each_entry(lock, &node->li_group, l_sl_policy) {
-               rc = lock_matches(lock, data);
-               if (rc == INTERVAL_ITER_STOP)
+               if (lock_matches(lock, data))
                        return INTERVAL_ITER_STOP;
        }
        return INTERVAL_ITER_CONT;
@@ -1244,8 +1276,8 @@ static unsigned int itree_overlap_cb(struct interval_node *in, void *args)
  *
  * \retval a referenced lock or NULL.
  */
-static struct ldlm_lock *search_itree(struct ldlm_resource *res,
-                                     struct lock_match_data *data)
+struct ldlm_lock *search_itree(struct ldlm_resource *res,
+                              struct ldlm_match_data *data)
 {
        struct interval_node_extent ext = {
                .start     = data->lmd_policy->l_extent.start,
@@ -1253,6 +1285,11 @@ static struct ldlm_lock *search_itree(struct ldlm_resource *res,
        };
        int idx;
 
+       data->lmd_lock = NULL;
+
+       if (data->lmd_match & LDLM_MATCH_RIGHT)
+               ext.end = OBD_OBJECT_EOF;
+
        for (idx = 0; idx < LCK_MODE_NUM; idx++) {
                struct ldlm_interval_tree *tree = &res->lr_itree[idx];
 
@@ -1264,9 +1301,13 @@ static struct ldlm_lock *search_itree(struct ldlm_resource *res,
 
                interval_search(tree->lit_root, &ext,
                                itree_overlap_cb, data);
+               if (data->lmd_lock)
+                       return data->lmd_lock;
        }
-       return data->lmd_lock;
+
+       return NULL;
 }
+EXPORT_SYMBOL(search_itree);
 
 
 /**
@@ -1278,16 +1319,16 @@ static struct ldlm_lock *search_itree(struct ldlm_resource *res,
  * \retval a referenced lock or NULL.
  */
 static struct ldlm_lock *search_queue(struct list_head *queue,
-                                     struct lock_match_data *data)
+                                     struct ldlm_match_data *data)
 {
        struct ldlm_lock *lock;
-       int rc;
 
-       list_for_each_entry(lock, queue, l_res_link) {
-               rc = lock_matches(lock, data);
-               if (rc == INTERVAL_ITER_STOP)
+       data->lmd_lock = NULL;
+
+       list_for_each_entry(lock, queue, l_res_link)
+               if (lock_matches(lock, data))
                        return data->lmd_lock;
-       }
+
        return NULL;
 }
 
@@ -1295,7 +1336,7 @@ void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
 {
        if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
                lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
-               wake_up_all(&lock->l_waitq);
+               wake_up(&lock->l_waitq);
        }
 }
 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
@@ -1317,7 +1358,7 @@ void ldlm_lock_fail_match(struct ldlm_lock *lock)
 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
 {
        ldlm_set_lvb_ready(lock);
-       wake_up_all(&lock->l_waitq);
+       wake_up(&lock->l_waitq);
 }
 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
 
@@ -1363,24 +1404,28 @@ EXPORT_SYMBOL(ldlm_lock_allow_match);
  * keep caller code unchanged), the context failure will be discovered by
  * caller sometime later.
  */
-enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
-                              const struct ldlm_res_id *res_id,
-                              enum ldlm_type type,
-                              union ldlm_policy_data *policy,
-                              enum ldlm_mode mode,
-                              struct lustre_handle *lockh, int unref)
-{
-       struct lock_match_data data = {
-               .lmd_old        = NULL,
-               .lmd_lock       = NULL,
-               .lmd_mode       = &mode,
-               .lmd_policy     = policy,
-               .lmd_flags      = flags,
-               .lmd_unref      = unref,
+enum ldlm_mode ldlm_lock_match_with_skip(struct ldlm_namespace *ns,
+                                        __u64 flags, __u64 skip_flags,
+                                        const struct ldlm_res_id *res_id,
+                                        enum ldlm_type type,
+                                        union ldlm_policy_data *policy,
+                                        enum ldlm_mode mode,
+                                        struct lustre_handle *lockh,
+                                        enum ldlm_match_flags match_flags)
+{
+       struct ldlm_match_data data = {
+               .lmd_old = NULL,
+               .lmd_lock = NULL,
+               .lmd_mode = &mode,
+               .lmd_policy = policy,
+               .lmd_flags = flags,
+               .lmd_skip_flags = skip_flags,
+               .lmd_match = match_flags,
        };
        struct ldlm_resource *res;
        struct ldlm_lock *lock;
-       int rc = 0;
+       int matched;
+
        ENTRY;
 
        if (ns == NULL) {
@@ -1401,101 +1446,76 @@ enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
 
        LDLM_RESOURCE_ADDREF(res);
        lock_res(res);
-
        if (res->lr_type == LDLM_EXTENT)
                lock = search_itree(res, &data);
        else
                lock = search_queue(&res->lr_granted, &data);
-       if (lock != NULL)
-               GOTO(out, rc = 1);
-       if (flags & LDLM_FL_BLOCK_GRANTED)
-               GOTO(out, rc = 0);
-       lock = search_queue(&res->lr_converting, &data);
-       if (lock != NULL)
-               GOTO(out, rc = 1);
-       lock = search_queue(&res->lr_waiting, &data);
-       if (lock != NULL)
-               GOTO(out, rc = 1);
-
-        EXIT;
- out:
-        unlock_res(res);
-        LDLM_RESOURCE_DELREF(res);
-        ldlm_resource_putref(res);
+       if (!lock && !(flags & LDLM_FL_BLOCK_GRANTED))
+               lock = search_queue(&res->lr_waiting, &data);
+       matched = lock ? mode : 0;
+       unlock_res(res);
+       LDLM_RESOURCE_DELREF(res);
+       ldlm_resource_putref(res);
 
-        if (lock) {
-                ldlm_lock2handle(lock, lockh);
-                if ((flags & LDLM_FL_LVB_READY) &&
+       if (lock) {
+               ldlm_lock2handle(lock, lockh);
+               if ((flags & LDLM_FL_LVB_READY) &&
                    (!ldlm_is_lvb_ready(lock))) {
                        __u64 wait_flags = LDLM_FL_LVB_READY |
                                LDLM_FL_DESTROYED | LDLM_FL_FAIL_NOTIFIED;
-                        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_RELEASE(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 & wait_flags,
-                                    &lwi);
-                       if (!ldlm_is_lvb_ready(lock)) {
-                                if (flags & LDLM_FL_TEST_LOCK)
-                                        LDLM_LOCK_RELEASE(lock);
-                                else
-                                        ldlm_lock_decref_internal(lock, mode);
-                                rc = 0;
-                        }
-                }
-        }
- out2:
-        if (rc) {
-               LDLM_DEBUG(lock, "matched (%llu %llu)",
-                           (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 (lock->l_completion_ast) {
+                               int err = lock->l_completion_ast(lock,
+                                                       LDLM_FL_WAIT_NOREPROC,
+                                                       NULL);
+                               if (err)
+                                       GOTO(out_fail_match, matched = 0);
+                       }
 
-                if (flags & LDLM_FL_TEST_LOCK)
-                        LDLM_LOCK_RELEASE(lock);
+                       wait_event_idle_timeout(
+                               lock->l_waitq,
+                               lock->l_flags & wait_flags,
+                               cfs_time_seconds(obd_timeout));
 
-        } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
-                LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
+                       if (!ldlm_is_lvb_ready(lock))
+                               GOTO(out_fail_match, matched = 0);
+               }
+
+               /* check user's security context */
+               if (lock->l_conn_export &&
+                   sptlrpc_import_check_ctx(
+                               class_exp2cliimp(lock->l_conn_export)))
+                       GOTO(out_fail_match, matched = 0);
+
+               LDLM_DEBUG(lock, "matched (%llu %llu)",
+                          (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);
+
+out_fail_match:
+               if (flags & LDLM_FL_TEST_LOCK)
+                       LDLM_LOCK_RELEASE(lock);
+               else if (!matched)
+                       ldlm_lock_decref_internal(lock, mode);
+       }
+
+       /* less verbose for test-only */
+       if (!matched && !(flags & LDLM_FL_TEST_LOCK)) {
+               LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
                                  "%llu/%llu (%llu %llu)", 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);
-        }
+                                 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 (data.lmd_old != NULL)
                LDLM_LOCK_PUT(data.lmd_old);
 
-       return rc ? mode : 0;
+       return matched;
 }
-EXPORT_SYMBOL(ldlm_lock_match);
+EXPORT_SYMBOL(ldlm_lock_match_with_skip);
 
 enum ldlm_mode ldlm_revalidate_lock_handle(const struct lustre_handle *lockh,
                                           __u64 *bits)
@@ -1657,7 +1677,7 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
 
        lock->l_req_mode = mode;
        lock->l_ast_data = data;
-       lock->l_pid = current_pid();
+       lock->l_pid = current->pid;
        if (ns_is_server(ns))
                ldlm_set_ns_srv(lock);
        if (cbs) {
@@ -1666,11 +1686,18 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
                lock->l_glimpse_ast = cbs->lcs_glimpse;
        }
 
-       lock->l_tree_node = NULL;
-       /* if this is the extent lock, allocate the interval tree node */
-       if (type == LDLM_EXTENT)
-               if (ldlm_interval_alloc(lock) == NULL)
-                       GOTO(out, rc = -ENOMEM);
+       switch (type) {
+       case LDLM_EXTENT:
+               rc = ldlm_extent_alloc_lock(lock);
+               break;
+       case LDLM_IBITS:
+               rc = ldlm_inodebits_alloc_lock(lock);
+               break;
+       default:
+               rc = 0;
+       }
+       if (rc)
+               GOTO(out, rc);
 
        if (lvb_len) {
                lock->l_lvb_len = lvb_len;
@@ -1691,6 +1718,30 @@ out:
        RETURN(ERR_PTR(rc));
 }
 
+#ifdef HAVE_SERVER_SUPPORT
+static enum ldlm_error ldlm_lock_enqueue_helper(struct ldlm_lock *lock,
+                                            __u64 *flags)
+{
+       struct ldlm_resource *res = lock->l_resource;
+       enum ldlm_error rc = ELDLM_OK;
+       LIST_HEAD(rpc_list);
+       ldlm_processing_policy policy;
+
+       ENTRY;
+
+       policy = ldlm_get_processing_policy(res);
+       policy(lock, flags, LDLM_PROCESS_ENQUEUE, &rc, &rpc_list);
+       if (rc == ELDLM_OK && lock->l_granted_mode != lock->l_req_mode &&
+           res->lr_type != LDLM_FLOCK)
+               rc = ldlm_handle_conflict_lock(lock, flags, &rpc_list);
+
+       if (!list_empty(&rpc_list))
+               ldlm_discard_bl_list(&rpc_list);
+
+       RETURN(rc);
+}
+#endif
+
 /**
  * Enqueue (request) a lock.
  *
@@ -1701,25 +1752,26 @@ out:
  * set, skip all the enqueueing and delegate lock processing to intent policy
  * function.
  */
-enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
+enum ldlm_error ldlm_lock_enqueue(const struct lu_env *env,
+                                 struct ldlm_namespace *ns,
                                  struct ldlm_lock **lockp,
                                  void *cookie, __u64 *flags)
 {
        struct ldlm_lock *lock = *lockp;
-       struct ldlm_resource *res = lock->l_resource;
-       int local = ns_is_client(ldlm_res_to_ns(res));
-#ifdef HAVE_SERVER_SUPPORT
-       ldlm_processing_policy policy;
-#endif
+       struct ldlm_resource *res;
+       int local = ns_is_client(ns);
        enum ldlm_error rc = ELDLM_OK;
        struct ldlm_interval *node = NULL;
+#ifdef HAVE_SERVER_SUPPORT
+       bool reconstruct = false;
+#endif
        ENTRY;
 
         /* 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);
+               rc = ns->ns_policy(env, 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
@@ -1732,7 +1784,7 @@ enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
                         *flags |= LDLM_FL_LOCK_CHANGED;
                         RETURN(0);
                } else if (rc != ELDLM_OK &&
-                          lock->l_req_mode == lock->l_granted_mode) {
+                          ldlm_is_granted(lock)) {
                        LASSERT(*flags & LDLM_FL_RESENT);
                        /* It may happen that ns_policy returns an error in
                         * resend case, object may be unlinked or just some
@@ -1755,21 +1807,39 @@ enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
                 * Take NO_TIMEOUT from the lock as it is inherited through
                 * LDLM_FL_INHERIT_MASK */
                *flags |= LDLM_FL_LOCK_CHANGED;
-               if (lock->l_req_mode != lock->l_granted_mode)
+               if (!ldlm_is_granted(lock))
                        *flags |= LDLM_FL_BLOCK_GRANTED;
                *flags |= lock->l_flags & LDLM_FL_NO_TIMEOUT;
                RETURN(ELDLM_OK);
        }
 
+#ifdef HAVE_SERVER_SUPPORT
        /* For a replaying lock, it might be already in granted list. So
         * unlinking the lock will cause the interval node to be freed, we
         * have to allocate the interval node early otherwise we can't regrant
-        * this lock in the future. - jay */
-       if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
+        * this lock in the future. - jay
+        *
+        * The only time the ldlm_resource changes for the ldlm_lock is when
+        * ldlm_lock_change_resource() is called and that only happens for
+        * the Lustre client case.
+        */
+       if (!local && (*flags & LDLM_FL_REPLAY) &&
+           lock->l_resource->lr_type == LDLM_EXTENT)
                OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, GFP_NOFS);
 
-        lock_res_and_lock(lock);
-        if (local && lock->l_req_mode == lock->l_granted_mode) {
+       reconstruct = !local && lock->l_resource->lr_type == LDLM_FLOCK &&
+                     !(*flags & LDLM_FL_TEST_LOCK);
+       if (reconstruct) {
+               rc = req_can_reconstruct(cookie, NULL);
+               if (rc != 0) {
+                       if (rc == 1)
+                               rc = 0;
+                       RETURN(rc);
+               }
+       }
+#endif
+       res = lock_res_and_lock(lock);
+       if (local && ldlm_is_granted(lock)) {
                 /* 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. */
@@ -1810,33 +1880,27 @@ enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
         * more or less trusting the clients not to lie.
         *
         * FIXME (bug 268): Detect obvious lies by checking compatibility in
-        * granted/converting queues. */
+        * granted queue. */
         if (local) {
-                if (*flags & LDLM_FL_BLOCK_CONV)
-                        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, lock);
-                else
-                        ldlm_grant_lock(lock, NULL);
+               if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
+                       ldlm_resource_add_lock(res, &res->lr_waiting, lock);
+               else
+                       ldlm_grant_lock(lock, NULL);
                GOTO(out, rc = ELDLM_OK);
 #ifdef HAVE_SERVER_SUPPORT
-        } else if (*flags & LDLM_FL_REPLAY) {
-                if (*flags & LDLM_FL_BLOCK_CONV) {
-                        ldlm_resource_add_lock(res, &res->lr_converting, lock);
-                       GOTO(out, rc = ELDLM_OK);
-                } else if (*flags & LDLM_FL_BLOCK_WAIT) {
-                        ldlm_resource_add_lock(res, &res->lr_waiting, lock);
+       } else if (*flags & LDLM_FL_REPLAY) {
+               if (*flags & LDLM_FL_BLOCK_WAIT) {
+                       ldlm_resource_add_lock(res, &res->lr_waiting, lock);
                        GOTO(out, rc = ELDLM_OK);
-                } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
-                        ldlm_grant_lock(lock, NULL);
+               } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
+                       ldlm_grant_lock(lock, NULL);
                        GOTO(out, rc = ELDLM_OK);
-                }
-                /* If no flags, fall through to normal enqueue path. */
-        }
+               }
+               /* If no flags, fall through to normal enqueue path. */
+       }
 
-        policy = ldlm_processing_policy_table[res->lr_type];
-        policy(lock, flags, 1, &rc, NULL);
-        GOTO(out, rc);
+       rc = ldlm_lock_enqueue_helper(lock, flags);
+       GOTO(out, rc);
 #else
         } else {
                 CERROR("This is client-side-only module, cannot handle "
@@ -1847,6 +1911,16 @@ enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
 
 out:
         unlock_res_and_lock(lock);
+
+#ifdef HAVE_SERVER_SUPPORT
+       if (reconstruct) {
+               struct ptlrpc_request *req = cookie;
+
+               tgt_mk_reply_data(NULL, NULL,
+                                 &req->rq_export->exp_target_data,
+                                 req, 0, NULL, false, 0);
+       }
+#endif
         if (node)
                 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
         return rc;
@@ -1860,114 +1934,223 @@ out:
  * Must be called with resource lock held.
  */
 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
-                        struct list_head *work_list)
+                        struct list_head *work_list,
+                        enum ldlm_process_intention intention, __u64 hint)
 {
        struct list_head *tmp, *pos;
        ldlm_processing_policy policy;
        __u64 flags;
        int rc = LDLM_ITER_CONTINUE;
        enum ldlm_error err;
+       LIST_HEAD(bl_ast_list);
+
        ENTRY;
 
        check_res_locked(res);
 
-       policy = ldlm_processing_policy_table[res->lr_type];
+       policy = ldlm_get_processing_policy(res);
        LASSERT(policy);
+       LASSERT(intention == LDLM_PROCESS_RESCAN ||
+               intention == LDLM_PROCESS_RECOVERY);
 
+restart:
        list_for_each_safe(tmp, pos, queue) {
                struct ldlm_lock *pending;
+               LIST_HEAD(rpc_list);
 
                pending = list_entry(tmp, struct ldlm_lock, l_res_link);
 
                 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
 
                 flags = 0;
-                rc = policy(pending, &flags, 0, &err, work_list);
-                if (rc != LDLM_ITER_CONTINUE)
-                        break;
+               rc = policy(pending, &flags, intention, &err, &rpc_list);
+               if (pending->l_granted_mode == pending->l_req_mode ||
+                   res->lr_type == LDLM_FLOCK) {
+                       list_splice(&rpc_list, work_list);
+               } else {
+                       list_splice(&rpc_list, &bl_ast_list);
+               }
+               /*
+                * When this is called from recovery done, we always want
+                * to scan the whole list no matter what 'rc' is returned.
+                */
+               if (rc != LDLM_ITER_CONTINUE &&
+                   intention == LDLM_PROCESS_RESCAN)
+                       break;
         }
 
-        RETURN(rc);
+       if (!list_empty(&bl_ast_list)) {
+               unlock_res(res);
+
+               rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &bl_ast_list,
+                                      LDLM_WORK_BL_AST);
+
+               lock_res(res);
+               if (rc == -ERESTART)
+                       GOTO(restart, rc);
+       }
+
+       if (!list_empty(&bl_ast_list))
+               ldlm_discard_bl_list(&bl_ast_list);
+
+        RETURN(intention == LDLM_PROCESS_RESCAN ? rc : LDLM_ITER_CONTINUE);
 }
-#endif
 
 /**
- * Process a call to blocking AST callback for a lock in ast_work list
+ * Conflicting locks are detected for a lock to be enqueued, add the lock
+ * into waiting list and send blocking ASTs to the conflicting locks.
+ *
+ * \param[in] lock             The lock to be enqueued.
+ * \param[out] flags           Lock flags for the lock to be enqueued.
+ * \param[in] rpc_list         Conflicting locks list.
+ *
+ * \retval -ERESTART:  Some lock was instantly canceled while sending
+ *                     blocking ASTs, caller needs to re-check conflicting
+ *                     locks.
+ * \retval -EAGAIN:    Lock was destroyed, caller should return error.
+ * \reval 0:           Lock is successfully added in waiting list.
  */
-static int
-ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
+int ldlm_handle_conflict_lock(struct ldlm_lock *lock, __u64 *flags,
+                             struct list_head *rpc_list)
 {
-       struct ldlm_cb_set_arg *arg = opaq;
-       struct ldlm_lock_desc   d;
-       int                     rc;
-       struct ldlm_lock       *lock;
+       struct ldlm_resource *res = lock->l_resource;
+       int rc;
        ENTRY;
 
-       if (list_empty(arg->list))
-               RETURN(-ENOENT);
+       check_res_locked(res);
 
-       lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
+       /* If either of the compat_queue()s returned failure, then we
+        * have ASTs to send and must go onto the waiting list.
+        *
+        * bug 2322: we used to unlink and re-add here, which was a
+        * terrible folly -- if we goto restart, we could get
+        * re-ordered!  Causes deadlock, because ASTs aren't sent! */
+       if (list_empty(&lock->l_res_link))
+               ldlm_resource_add_lock(res, &res->lr_waiting, lock);
+       unlock_res(res);
 
-       /* nobody should touch l_bl_ast */
-       lock_res_and_lock(lock);
-       list_del_init(&lock->l_bl_ast);
+       rc = ldlm_run_ast_work(ldlm_res_to_ns(res), rpc_list,
+                              LDLM_WORK_BL_AST);
 
-       LASSERT(ldlm_is_ast_sent(lock));
-       LASSERT(lock->l_bl_ast_run == 0);
-       LASSERT(lock->l_blocking_lock);
-       lock->l_bl_ast_run++;
-       unlock_res_and_lock(lock);
+       if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_OST_FAIL_RACE) &&
+           !ns_is_client(ldlm_res_to_ns(res)))
+               class_fail_export(lock->l_export);
 
-       ldlm_lock2desc(lock->l_blocking_lock, &d);
+       if (rc == -ERESTART)
+               ldlm_reprocess_all(res, 0);
 
-       rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
-       LDLM_LOCK_RELEASE(lock->l_blocking_lock);
-       lock->l_blocking_lock = NULL;
-       LDLM_LOCK_RELEASE(lock);
+       lock_res(res);
+       if (rc == -ERESTART) {
+               /* 15715: The lock was granted and destroyed after
+                * resource lock was dropped. Interval node was freed
+                * in ldlm_lock_destroy. Anyway, this always happens
+                * when a client is being evicted. So it would be
+                * ok to return an error. -jay */
+               if (ldlm_is_destroyed(lock))
+                       RETURN(-EAGAIN);
+
+               /* lock was granted while resource was unlocked. */
+               if (ldlm_is_granted(lock)) {
+                       /* bug 11300: if the lock has been granted,
+                        * break earlier because otherwise, we will go
+                        * to restart and ldlm_resource_unlink will be
+                        * called and it causes the interval node to be
+                        * freed. Then we will fail at
+                        * ldlm_extent_add_lock() */
+                       *flags &= ~LDLM_FL_BLOCKED_MASK;
+               }
 
-       RETURN(rc);
+       }
+       *flags |= LDLM_FL_BLOCK_GRANTED;
+
+       RETURN(0);
 }
 
 /**
- * Process a call to completion AST callback for a lock in ast_work list
+ * Discard all AST work items from list.
+ *
+ * If for whatever reason we do not want to send ASTs to conflicting locks
+ * anymore, disassemble the list with this function.
+ */
+void ldlm_discard_bl_list(struct list_head *bl_list)
+{
+       struct ldlm_lock *lock, *tmp;
+
+       ENTRY;
+
+       list_for_each_entry_safe(lock, tmp, bl_list, l_bl_ast) {
+               LASSERT(!list_empty(&lock->l_bl_ast));
+               list_del_init(&lock->l_bl_ast);
+               ldlm_clear_ast_sent(lock);
+               LASSERT(lock->l_bl_ast_run == 0);
+               ldlm_clear_blocking_lock(lock);
+               LDLM_LOCK_RELEASE(lock);
+       }
+       EXIT;
+}
+
+/**
+ * Process a call to blocking AST callback for a lock in ast_work list
  */
 static int
-ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
+ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
 {
-       struct ldlm_cb_set_arg  *arg = opaq;
-       int                      rc = 0;
-       struct ldlm_lock        *lock;
-       ldlm_completion_callback completion_callback;
+       struct ldlm_cb_set_arg *arg = opaq;
+       struct ldlm_lock *lock;
+       struct ldlm_lock_desc d;
+       struct ldlm_bl_desc bld;
+       int rc;
+
        ENTRY;
 
        if (list_empty(arg->list))
                RETURN(-ENOENT);
 
-       lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
-
-       /* 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. */
+       lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
 
-       /* nobody should touch l_cp_ast */
+       /* nobody should touch l_bl_ast but some locks in the list may become
+        * granted after lock convert or COS downgrade, these locks should be
+        * just skipped here and removed from the list.
+        */
        lock_res_and_lock(lock);
-       list_del_init(&lock->l_cp_ast);
-       LASSERT(ldlm_is_cp_reqd(lock));
-       /* save l_completion_ast since it can be changed by
-        * mds_intent_policy(), see bug 14225 */
-       completion_callback = lock->l_completion_ast;
-       ldlm_clear_cp_reqd(lock);
+       list_del_init(&lock->l_bl_ast);
+
+       /* lock is not blocking lock anymore, but was kept in the list because
+        * it can managed only here.
+        */
+       if (!ldlm_is_ast_sent(lock)) {
+               unlock_res_and_lock(lock);
+               LDLM_LOCK_RELEASE(lock);
+               RETURN(0);
+       }
+
+       LASSERT(lock->l_blocking_lock);
+       ldlm_lock2desc(lock->l_blocking_lock, &d);
+       /* copy blocking lock ibits in cancel_bits as well,
+        * new client may use them for lock convert and it is
+        * important to use new field to convert locks from
+        * new servers only
+        */
+       d.l_policy_data.l_inodebits.cancel_bits =
+               lock->l_blocking_lock->l_policy_data.l_inodebits.bits;
+
+       /* Blocking lock is being destroyed here but some information about it
+        * may be needed inside l_blocking_ast() function below,
+        * e.g. in mdt_blocking_ast(). So save needed data in bl_desc.
+        */
+       bld.bl_same_client = lock->l_client_cookie ==
+                            lock->l_blocking_lock->l_client_cookie;
+       bld.bl_cos_incompat = ldlm_is_cos_incompat(lock->l_blocking_lock);
+       arg->bl_desc = &bld;
+
+       LASSERT(ldlm_is_ast_sent(lock));
+       LASSERT(lock->l_bl_ast_run == 0);
+       lock->l_bl_ast_run++;
+       ldlm_clear_blocking_lock(lock);
        unlock_res_and_lock(lock);
 
-       if (completion_callback != NULL)
-               rc = completion_callback(lock, 0, (void *)arg);
+       rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
+
        LDLM_LOCK_RELEASE(lock);
 
        RETURN(rc);
@@ -2024,18 +2207,71 @@ int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
 
        /* transfer the glimpse descriptor to ldlm_cb_set_arg */
        arg->gl_desc = gl_work->gl_desc;
+       arg->gl_interpret_reply = gl_work->gl_interpret_reply;
+       arg->gl_interpret_data = gl_work->gl_interpret_data;
 
        /* invoke the actual glimpse callback */
-       if (lock->l_glimpse_ast(lock, (void*)arg) == 0)
-               rc = 1;
+       rc = lock->l_glimpse_ast(lock, (void *)arg);
+       if (rc == 0)
+               rc = 1; /* update LVB if this is server lock */
+       else if (rc == -ELDLM_NO_LOCK_DATA)
+               ldlm_lvbo_update(lock->l_resource, lock, NULL, 1);
 
        LDLM_LOCK_RELEASE(lock);
-
-       if ((gl_work->gl_flags & LDLM_GL_WORK_NOFREE) == 0)
+       if (gl_work->gl_flags & LDLM_GL_WORK_SLAB_ALLOCATED)
+               OBD_SLAB_FREE_PTR(gl_work, ldlm_glimpse_work_kmem);
+       else
                OBD_FREE_PTR(gl_work);
 
        RETURN(rc);
 }
+#endif
+
+/**
+ * Process a call to completion AST callback for a lock in ast_work list
+ */
+static int
+ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
+{
+       struct ldlm_cb_set_arg *arg = opaq;
+       struct ldlm_lock *lock;
+       ldlm_completion_callback completion_callback;
+       int rc = 0;
+
+       ENTRY;
+
+       if (list_empty(arg->list))
+               RETURN(-ENOENT);
+
+       lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
+
+       /* 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. */
+
+       /* nobody should touch l_cp_ast */
+       lock_res_and_lock(lock);
+       list_del_init(&lock->l_cp_ast);
+       LASSERT(ldlm_is_cp_reqd(lock));
+       /* save l_completion_ast since it can be changed by
+        * mds_intent_policy(), see bug 14225 */
+       completion_callback = lock->l_completion_ast;
+       ldlm_clear_cp_reqd(lock);
+       unlock_res_and_lock(lock);
+
+       if (completion_callback != NULL)
+               rc = completion_callback(lock, 0, (void *)arg);
+       LDLM_LOCK_RELEASE(lock);
+
+       RETURN(rc);
+}
 
 /**
  * Process list of locks in need of ASTs being sent.
@@ -2044,11 +2280,11 @@ int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
  * one.
  */
 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
-                      ldlm_desc_ast_t ast_type)
+                     ldlm_desc_ast_t ast_type)
 {
        struct ldlm_cb_set_arg *arg;
-       set_producer_func       work_ast_lock;
-       int                     rc;
+       set_producer_func work_ast_lock;
+       int rc;
 
        if (list_empty(rpc_list))
                RETURN(0);
@@ -2061,24 +2297,26 @@ int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
        arg->list = rpc_list;
 
        switch (ast_type) {
-               case LDLM_WORK_BL_AST:
-                       arg->type = LDLM_BL_CALLBACK;
-                       work_ast_lock = ldlm_work_bl_ast_lock;
-                       break;
-               case LDLM_WORK_CP_AST:
-                       arg->type = LDLM_CP_CALLBACK;
-                       work_ast_lock = ldlm_work_cp_ast_lock;
-                       break;
-               case LDLM_WORK_REVOKE_AST:
-                       arg->type = LDLM_BL_CALLBACK;
-                       work_ast_lock = ldlm_work_revoke_ast_lock;
-                       break;
-               case LDLM_WORK_GL_AST:
-                       arg->type = LDLM_GL_CALLBACK;
-                       work_ast_lock = ldlm_work_gl_ast_lock;
-                       break;
-               default:
-                       LBUG();
+       case LDLM_WORK_CP_AST:
+               arg->type = LDLM_CP_CALLBACK;
+               work_ast_lock = ldlm_work_cp_ast_lock;
+               break;
+#ifdef HAVE_SERVER_SUPPORT
+       case LDLM_WORK_BL_AST:
+               arg->type = LDLM_BL_CALLBACK;
+               work_ast_lock = ldlm_work_bl_ast_lock;
+               break;
+       case LDLM_WORK_REVOKE_AST:
+               arg->type = LDLM_BL_CALLBACK;
+               work_ast_lock = ldlm_work_revoke_ast_lock;
+               break;
+       case LDLM_WORK_GL_AST:
+               arg->type = LDLM_GL_CALLBACK;
+               work_ast_lock = ldlm_work_gl_ast_lock;
+               break;
+#endif
+       default:
+               LBUG();
        }
 
        /* We create a ptlrpc request set with flow control extension.
@@ -2090,7 +2328,7 @@ int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
        if (arg->set == NULL)
                GOTO(out, rc = -ENOMEM);
 
-       ptlrpc_set_wait(arg->set);
+       ptlrpc_set_wait(NULL, arg->set);
        ptlrpc_set_destroy(arg->set);
 
        rc = atomic_read(&arg->restart) ? -ERESTART : 0;
@@ -2100,98 +2338,93 @@ out:
        return rc;
 }
 
-static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
-{
-        ldlm_reprocess_all(res);
-        return LDLM_ITER_CONTINUE;
-}
-
-static int ldlm_reprocess_res(struct cfs_hash *hs, struct cfs_hash_bd *bd,
-                             struct hlist_node *hnode, void *arg)
-{
-        struct ldlm_resource *res = cfs_hash_object(hs, hnode);
-        int    rc;
-
-        rc = reprocess_one_queue(res, arg);
-
-        return rc == LDLM_ITER_STOP;
-}
-
-/**
- * Iterate through all resources on a namespace attempting to grant waiting
- * locks.
- */
-void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
-{
-       ENTRY;
-
-       if (ns != NULL) {
-               cfs_hash_for_each_nolock(ns->ns_rs_hash,
-                                        ldlm_reprocess_res, NULL, 0);
-       }
-       EXIT;
-}
-
 /**
  * Try to grant all waiting locks on a resource.
  *
- * Calls ldlm_reprocess_queue on converting and waiting queues.
+ * Calls ldlm_reprocess_queue on waiting queue.
  *
  * Typically called after some resource locks are cancelled to see
  * if anything could be granted as a result of the cancellation.
  */
-void ldlm_reprocess_all(struct ldlm_resource *res)
+static void __ldlm_reprocess_all(struct ldlm_resource *res,
+                                enum ldlm_process_intention intention,
+                                __u64 hint)
 {
-       struct list_head rpc_list;
+       LIST_HEAD(rpc_list);
 #ifdef HAVE_SERVER_SUPPORT
-        int rc;
-        ENTRY;
+       ldlm_reprocessing_policy reprocess;
+       struct obd_device *obd;
+       int rc;
 
-       INIT_LIST_HEAD(&rpc_list);
-        /* Local lock trees don't get reprocessed. */
-        if (ns_is_client(ldlm_res_to_ns(res))) {
-                EXIT;
-                return;
-        }
+       ENTRY;
+
+       /* Local lock trees don't get reprocessed. */
+       if (ns_is_client(ldlm_res_to_ns(res))) {
+               EXIT;
+               return;
+       }
 
+       /* Disable reprocess during lock replay stage but allow during
+        * request replay stage.
+        */
+       obd = ldlm_res_to_ns(res)->ns_obd;
+       if (obd->obd_recovering &&
+           atomic_read(&obd->obd_req_replay_clients) == 0)
+               RETURN_EXIT;
 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_ast_work(ldlm_res_to_ns(res), &rpc_list,
-                               LDLM_WORK_CP_AST);
-        if (rc == -ERESTART) {
+       lock_res(res);
+       reprocess = ldlm_get_reprocessing_policy(res);
+       reprocess(res, &res->lr_waiting, &rpc_list, intention, hint);
+       unlock_res(res);
+
+       rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
+                              LDLM_WORK_CP_AST);
+       if (rc == -ERESTART) {
                LASSERT(list_empty(&rpc_list));
-                goto restart;
-        }
+               hint = 0;
+               goto restart;
+       }
 #else
-        ENTRY;
+       ENTRY;
 
-       INIT_LIST_HEAD(&rpc_list);
-        if (!ns_is_client(ldlm_res_to_ns(res))) {
-                CERROR("This is client-side-only module, cannot handle "
-                       "LDLM_NAMESPACE_SERVER resource type lock.\n");
-                LBUG();
-        }
+       if (!ns_is_client(ldlm_res_to_ns(res))) {
+               CERROR("This is client-side-only module, cannot handle "
+                      "LDLM_NAMESPACE_SERVER resource type lock.\n");
+               LBUG();
+       }
 #endif
-        EXIT;
+       EXIT;
+}
+
+void ldlm_reprocess_all(struct ldlm_resource *res, __u64 hint)
+{
+       __ldlm_reprocess_all(res, LDLM_PROCESS_RESCAN, hint);
 }
 EXPORT_SYMBOL(ldlm_reprocess_all);
 
-static bool is_bl_done(struct ldlm_lock *lock)
+static int ldlm_reprocess_res(struct cfs_hash *hs, struct cfs_hash_bd *bd,
+                             struct hlist_node *hnode, void *arg)
 {
-       bool bl_done = true;
+       struct ldlm_resource *res = cfs_hash_object(hs, hnode);
 
-       if (!ldlm_is_bl_done(lock)) {
-               lock_res_and_lock(lock);
-               bl_done = ldlm_is_bl_done(lock);
-               unlock_res_and_lock(lock);
-       }
+       /* This is only called once after recovery done. LU-8306. */
+       __ldlm_reprocess_all(res, LDLM_PROCESS_RECOVERY, 0);
+       return 0;
+}
+
+/**
+ * Iterate through all resources on a namespace attempting to grant waiting
+ * locks.
+ */
+void ldlm_reprocess_recovery_done(struct ldlm_namespace *ns)
+{
+       ENTRY;
 
-       return bl_done;
+       if (ns != NULL) {
+               cfs_hash_for_each_nolock(ns->ns_rs_hash,
+                                        ldlm_reprocess_res, NULL, 0);
+       }
+       EXIT;
 }
 
 /**
@@ -2204,24 +2437,22 @@ void ldlm_cancel_callback(struct ldlm_lock *lock)
        if (!ldlm_is_cancel(lock)) {
                ldlm_set_cancel(lock);
                if (lock->l_blocking_ast) {
-                        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");
-                }
+                       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");
+               }
 
                /* only canceller can set bl_done bit */
                ldlm_set_bl_done(lock);
-               wake_up_all(&lock->l_waitq);
+               wake_up(&lock->l_waitq);
        } else if (!ldlm_is_bl_done(lock)) {
-               struct l_wait_info lwi = { 0 };
-
                /* The lock is guaranteed to have been canceled once
                 * returning from this function. */
                unlock_res_and_lock(lock);
-               l_wait_event(lock->l_waitq, is_bl_done(lock), &lwi);
+               wait_event_idle(lock->l_waitq, is_bl_done(lock));
                lock_res_and_lock(lock);
        }
 }
@@ -2257,6 +2488,7 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
          * talking to me first. -phik */
         if (lock->l_readers || lock->l_writers) {
                 LDLM_ERROR(lock, "lock still has references");
+               unlock_res_and_lock(lock);
                 LBUG();
         }
 
@@ -2274,8 +2506,8 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
         ldlm_resource_unlink_lock(lock);
         ldlm_lock_destroy_nolock(lock);
 
-        if (lock->l_granted_mode == lock->l_req_mode)
-                ldlm_pool_del(&ns->ns_pool, lock);
+       if (ldlm_is_granted(lock))
+               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 */
@@ -2307,6 +2539,7 @@ int ldlm_lock_set_data(const struct lustre_handle *lockh, void *data)
 EXPORT_SYMBOL(ldlm_lock_set_data);
 
 struct export_cl_data {
+       const struct lu_env     *ecl_env;
        struct obd_export       *ecl_exp;
        int                     ecl_loop;
 };
@@ -2319,10 +2552,10 @@ static void ldlm_cancel_lock_for_export(struct obd_export *exp,
 
        res = ldlm_resource_getref(lock->l_resource);
 
-       ldlm_res_lvbo_update(res, NULL, 1);
+       ldlm_lvbo_update(res, lock, NULL, 1);
        ldlm_lock_cancel(lock);
        if (!exp->exp_obd->obd_stopping)
-               ldlm_reprocess_all(res);
+               ldlm_reprocess_all(res, lock->l_policy_data.l_inodebits.bits);
        ldlm_resource_putref(res);
 
        ecl->ecl_loop++;
@@ -2359,10 +2592,17 @@ ldlm_cancel_locks_for_export_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
  */
 int ldlm_export_cancel_blocked_locks(struct obd_export *exp)
 {
+       struct lu_env env;
        struct export_cl_data   ecl = {
                .ecl_exp        = exp,
                .ecl_loop       = 0,
        };
+       int rc;
+
+       rc = lu_env_init(&env, LCT_DT_THREAD);
+       if (rc)
+               RETURN(rc);
+       ecl.ecl_env = &env;
 
        while (!list_empty(&exp->exp_bl_list)) {
                struct ldlm_lock *lock;
@@ -2385,6 +2625,8 @@ int ldlm_export_cancel_blocked_locks(struct obd_export *exp)
                LDLM_LOCK_RELEASE(lock);
        }
 
+       lu_env_fini(&env);
+
        CDEBUG(D_DLMTRACE, "Export %p, canceled %d locks, "
               "left on hash table %d.\n", exp, ecl.ecl_loop,
               atomic_read(&exp->exp_lock_hash->hs_count));
@@ -2399,10 +2641,16 @@ int ldlm_export_cancel_blocked_locks(struct obd_export *exp)
  */
 int ldlm_export_cancel_locks(struct obd_export *exp)
 {
-       struct export_cl_data   ecl = {
-               .ecl_exp        = exp,
-               .ecl_loop       = 0,
-       };
+       struct export_cl_data ecl;
+       struct lu_env env;
+       int rc;
+
+       rc = lu_env_init(&env, LCT_DT_THREAD);
+       if (rc)
+               RETURN(rc);
+       ecl.ecl_env = &env;
+       ecl.ecl_exp = exp;
+       ecl.ecl_loop = 0;
 
        cfs_hash_for_each_empty(exp->exp_lock_hash,
                                ldlm_cancel_locks_for_export_cb, &ecl);
@@ -2411,171 +2659,74 @@ int ldlm_export_cancel_locks(struct obd_export *exp)
               "left on hash table %d.\n", exp, ecl.ecl_loop,
               atomic_read(&exp->exp_lock_hash->hs_count));
 
+       if (ecl.ecl_loop > 0 &&
+           atomic_read(&exp->exp_lock_hash->hs_count) == 0 &&
+           exp->exp_obd->obd_stopping)
+               ldlm_reprocess_recovery_done(exp->exp_obd->obd_namespace);
+
+       lu_env_fini(&env);
+
        return ecl.ecl_loop;
 }
 
 /**
- * Downgrade an exclusive lock.
+ * Downgrade an PW/EX lock to COS | CR mode.
+ *
+ * A lock mode convertion from PW/EX mode to less conflict mode. The
+ * convertion may fail if lock was canceled before downgrade, but it doesn't
+ * indicate any problem, because such lock has no reader or writer, and will
+ * be released soon.
+ *
+ * Used by Commit on Sharing (COS) code to force object changes commit in case
+ * of conflict. Converted lock is considered as new lock and all blocking AST
+ * things are cleared, so any pending or new blocked lock on that lock will
+ * cause new call to blocking_ast and force resource object commit.
  *
- * A fast variant of ldlm_lock_convert for convertion of exclusive
- * locks. The convertion is always successful.
- * Used by Commit on Sharing (COS) code.
+ * Also used by layout_change to replace EX lock to CR lock.
  *
  * \param lock A lock to convert
  * \param new_mode new lock mode
  */
-void ldlm_lock_downgrade(struct ldlm_lock *lock, enum ldlm_mode new_mode)
+void ldlm_lock_mode_downgrade(struct ldlm_lock *lock, enum ldlm_mode new_mode)
 {
-        ENTRY;
-
-        LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
-        LASSERT(new_mode == LCK_COS);
-
-        lock_res_and_lock(lock);
-        ldlm_resource_unlink_lock(lock);
-        /*
-         * Remove the lock from pool as it will be added again in
-         * ldlm_grant_lock() called below.
-         */
-        ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
-
-        lock->l_req_mode = new_mode;
-        ldlm_grant_lock(lock, NULL);
-        unlock_res_and_lock(lock);
-        ldlm_reprocess_all(lock->l_resource);
-
-        EXIT;
-}
-EXPORT_SYMBOL(ldlm_lock_downgrade);
-
-/**
- * Attempt to convert already granted lock to a different mode.
- *
- * While lock conversion is not currently used, future client-side
- * optimizations could take advantage of it to avoid discarding cached
- * pages on a file.
- */
-struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock,
-                                       enum ldlm_mode new_mode, __u32 *flags)
-{
-       struct list_head rpc_list;
-       struct ldlm_resource *res;
-       struct ldlm_namespace *ns;
-       int granted = 0;
 #ifdef HAVE_SERVER_SUPPORT
-       int old_mode;
-       struct sl_insert_point prev;
-#endif
-       struct ldlm_interval *node;
        ENTRY;
 
-       INIT_LIST_HEAD(&rpc_list);
-       /* Just return if mode is unchanged. */
-       if (new_mode == lock->l_granted_mode) {
-               *flags |= LDLM_FL_BLOCK_GRANTED;
-               RETURN(lock->l_resource);
-       }
+       LASSERT(new_mode == LCK_COS || new_mode == LCK_CR);
 
-       /* I can't check the type of lock here because the bitlock of lock
-        * is not held here, so do the allocation blindly. -jay */
-       OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, GFP_NOFS);
-       if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
-               RETURN(NULL);
+       lock_res_and_lock(lock);
 
-       LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
-                "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
+       if (!(lock->l_granted_mode & (LCK_PW | LCK_EX))) {
+               unlock_res_and_lock(lock);
 
-       lock_res_and_lock(lock);
+               LASSERT(lock->l_granted_mode == LCK_MINMODE);
+               LDLM_DEBUG(lock, "lock was canceled before downgrade");
+               RETURN_EXIT;
+       }
 
-       res = lock->l_resource;
-       ns  = ldlm_res_to_ns(res);
+       ldlm_resource_unlink_lock(lock);
+       /*
+        * Remove the lock from pool as it will be added again in
+        * ldlm_grant_lock() called below.
+        */
+       ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
 
-#ifdef HAVE_SERVER_SUPPORT
-       old_mode = lock->l_req_mode;
-#endif
-       lock->l_req_mode = new_mode;
-       if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
-#ifdef HAVE_SERVER_SUPPORT
-               /* 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. */
-               prev.res_link = lock->l_res_link.prev;
-               prev.mode_link = lock->l_sl_mode.prev;
-               prev.policy_link = lock->l_sl_policy.prev;
-#endif
-                ldlm_resource_unlink_lock(lock);
-        } else {
-                ldlm_resource_unlink_lock(lock);
-                if (res->lr_type == LDLM_EXTENT) {
-                        /* FIXME: ugly code, I have to attach the lock to a
-                         * interval node again since perhaps it will be granted
-                         * soon */
-                       INIT_LIST_HEAD(&node->li_group);
-                        ldlm_interval_attach(node, lock);
-                        node = NULL;
-                }
-        }
+       /* Consider downgraded lock as a new lock and clear all states
+        * related to a previous blocking AST processing.
+        */
+       ldlm_clear_blocking_data(lock);
 
-        /*
-         * Remove old lock from the pool before adding the lock with new
-         * mode below in ->policy()
-         */
-        ldlm_pool_del(&ns->ns_pool, lock);
+       lock->l_req_mode = new_mode;
+       ldlm_grant_lock(lock, NULL);
+       unlock_res_and_lock(lock);
 
-        /* If this is a local resource, put it on the appropriate list. */
-        if (ns_is_client(ldlm_res_to_ns(res))) {
-                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 %x on local lock\n",
-                                   *flags);
-                        LBUG();
+       ldlm_reprocess_all(lock->l_resource,
+                          lock->l_policy_data.l_inodebits.bits);
 
-                        ldlm_grant_lock(lock, &rpc_list);
-                        granted = 1;
-                        /* FIXME: completion handling not with lr_lock held ! */
-                        if (lock->l_completion_ast)
-                                lock->l_completion_ast(lock, 0, NULL);
-                }
-#ifdef HAVE_SERVER_SUPPORT
-       } else {
-               int rc;
-               enum ldlm_error err;
-               __u64 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;
-                        if (res->lr_type == LDLM_EXTENT)
-                                ldlm_extent_add_lock(res, lock);
-                        else
-                                ldlm_granted_list_add_lock(lock, &prev);
-
-                        res = NULL;
-                } else {
-                        *flags |= LDLM_FL_BLOCK_GRANTED;
-                        granted = 1;
-                }
-        }
-#else
-        } else {
-                CERROR("This is client-side-only module, cannot handle "
-                       "LDLM_NAMESPACE_SERVER resource type lock.\n");
-                LBUG();
-        }
+       EXIT;
 #endif
-        unlock_res_and_lock(lock);
-
-        if (granted)
-                ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
-        if (node)
-                OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
-        RETURN(res);
 }
+EXPORT_SYMBOL(ldlm_lock_mode_downgrade);
 
 /**
  * Print lock with lock handle \a lockh description into debug log.
@@ -2609,125 +2760,137 @@ void _ldlm_lock_debug(struct ldlm_lock *lock,
 {
         va_list args;
         struct obd_export *exp = lock->l_export;
-        struct ldlm_resource *resource = lock->l_resource;
+       struct ldlm_resource *resource = NULL;
+       struct va_format vaf;
         char *nid = "local";
 
+       rcu_read_lock();
+       resource = rcu_dereference(lock->l_resource);
+       if (resource && !atomic_inc_not_zero(&resource->lr_refcount))
+               resource = NULL;
+       rcu_read_unlock();
+
         va_start(args, fmt);
+       vaf.fmt = fmt;
+       vaf.va = &args;
 
         if (exp && exp->exp_connection) {
-                nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
+               nid = obd_export_nid2str(exp);
         } else if (exp && exp->exp_obd != NULL) {
                 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
-                nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
+               nid = obd_import_nid2str(imp);
         }
 
         if (resource == NULL) {
-                libcfs_debug_vmsg2(msgdata, fmt, args,
-                      " ns: \?\? lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s "
-                      "res: \?\? rrc=\?\? type: \?\?\? flags: %#llx nid: %s "
-                      "remote: %#llx expref: %d pid: %u timeout: %lu "
-                      "lvb_type: %d\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, nid, lock->l_remote_handle.cookie,
-                      exp ? atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout, lock->l_lvb_type);
+               libcfs_debug_msg(msgdata,
+                                "%pV ns: \?\? lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: \?\? rrc=\?\? type: \?\?\? flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lld lvb_type: %d\n",
+                                &vaf,
+                                lock,
+                                lock->l_handle.h_cookie,
+                                refcount_read(&lock->l_handle.h_ref),
+                                lock->l_readers, lock->l_writers,
+                                ldlm_lockname[lock->l_granted_mode],
+                                ldlm_lockname[lock->l_req_mode],
+                                lock->l_flags, nid,
+                                lock->l_remote_handle.cookie,
+                                exp ? refcount_read(&exp->exp_handle.h_ref) : -99,
+                                lock->l_pid, lock->l_callback_timestamp,
+                                lock->l_lvb_type);
                 va_end(args);
                 return;
         }
 
        switch (resource->lr_type) {
        case LDLM_EXTENT:
-               libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s "
-                       "res: "DLDLMRES" rrc: %d type: %s [%llu->%llu] "
-                       "(req %llu->%llu) flags: %#llx nid: %s remote: "
-                       "%#llx expref: %d pid: %u timeout: %lu lvb_type: %d\n",
-                       ldlm_lock_to_ns_name(lock), 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],
-                       PLDLMRES(resource),
-                       atomic_read(&resource->lr_refcount),
-                       ldlm_typename[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, nid, lock->l_remote_handle.cookie,
-                       exp ? atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout,
-                       lock->l_lvb_type);
+               libcfs_debug_msg(msgdata,
+                                "%pV ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s [%llu->%llu] (req %llu->%llu) gid %llu flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lld lvb_type: %d\n",
+                                &vaf,
+                                ldlm_lock_to_ns_name(lock), lock,
+                                lock->l_handle.h_cookie,
+                                refcount_read(&lock->l_handle.h_ref),
+                                lock->l_readers, lock->l_writers,
+                                ldlm_lockname[lock->l_granted_mode],
+                                ldlm_lockname[lock->l_req_mode],
+                                PLDLMRES(resource),
+                                atomic_read(&resource->lr_refcount),
+                                ldlm_typename[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_req_extent.gid,
+                                lock->l_flags, nid,
+                                lock->l_remote_handle.cookie,
+                                exp ? refcount_read(&exp->exp_handle.h_ref) : -99,
+                                lock->l_pid, lock->l_callback_timestamp,
+                                lock->l_lvb_type);
                break;
 
        case LDLM_FLOCK:
-               libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s "
-                       "res: "DLDLMRES" rrc: %d type: %s pid: %d "
-                       "[%llu->%llu] flags: %#llx nid: %s "
-                       "remote: %#llx expref: %d pid: %u timeout: %lu\n",
-                       ldlm_lock_to_ns_name(lock), 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],
-                       PLDLMRES(resource),
-                       atomic_read(&resource->lr_refcount),
-                       ldlm_typename[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, nid, lock->l_remote_handle.cookie,
-                       exp ? atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
+               libcfs_debug_msg(msgdata,
+                                "%pV ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s pid: %d [%llu->%llu] flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lld\n",
+                                &vaf,
+                                ldlm_lock_to_ns_name(lock), lock,
+                                lock->l_handle.h_cookie,
+                                refcount_read(&lock->l_handle.h_ref),
+                                lock->l_readers, lock->l_writers,
+                                ldlm_lockname[lock->l_granted_mode],
+                                ldlm_lockname[lock->l_req_mode],
+                                PLDLMRES(resource),
+                                atomic_read(&resource->lr_refcount),
+                                ldlm_typename[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, nid,
+                                lock->l_remote_handle.cookie,
+                                exp ? refcount_read(&exp->exp_handle.h_ref) : -99,
+                                lock->l_pid, lock->l_callback_timestamp);
                break;
 
        case LDLM_IBITS:
-               libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s "
-                       "res: "DLDLMRES" bits %#llx rrc: %d type: %s "
-                       "flags: %#llx nid: %s remote: %#llx expref: %d "
-                       "pid: %u timeout: %lu lvb_type: %d\n",
-                       ldlm_lock_to_ns_name(lock),
-                       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],
-                       PLDLMRES(resource),
-                       lock->l_policy_data.l_inodebits.bits,
-                       atomic_read(&resource->lr_refcount),
-                       ldlm_typename[resource->lr_type],
-                       lock->l_flags, nid, lock->l_remote_handle.cookie,
-                       exp ? atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout,
-                       lock->l_lvb_type);
+               libcfs_debug_msg(msgdata,
+                                "%pV ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " bits %#llx/%#llx rrc: %d type: %s gid %llu flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lld lvb_type: %d\n",
+                                &vaf,
+                                ldlm_lock_to_ns_name(lock),
+                                lock, lock->l_handle.h_cookie,
+                                refcount_read(&lock->l_handle.h_ref),
+                                lock->l_readers, lock->l_writers,
+                                ldlm_lockname[lock->l_granted_mode],
+                                ldlm_lockname[lock->l_req_mode],
+                                PLDLMRES(resource),
+                                lock->l_policy_data.l_inodebits.bits,
+                                lock->l_policy_data.l_inodebits.try_bits,
+                                atomic_read(&resource->lr_refcount),
+                                ldlm_typename[resource->lr_type],
+                                lock->l_policy_data.l_inodebits.li_gid,
+                                lock->l_flags, nid,
+                                lock->l_remote_handle.cookie,
+                                exp ? refcount_read(&exp->exp_handle.h_ref) : -99,
+                                lock->l_pid, lock->l_callback_timestamp,
+                                lock->l_lvb_type);
                break;
 
        default:
-               libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s "
-                       "res: "DLDLMRES" rrc: %d type: %s flags: %#llx "
-                       "nid: %s remote: %#llx expref: %d pid: %u "
-                       "timeout: %lu lvb_type: %d\n",
-                       ldlm_lock_to_ns_name(lock),
-                       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],
-                       PLDLMRES(resource),
-                       atomic_read(&resource->lr_refcount),
-                       ldlm_typename[resource->lr_type],
-                       lock->l_flags, nid, lock->l_remote_handle.cookie,
-                       exp ? atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout,
-                       lock->l_lvb_type);
+               libcfs_debug_msg(msgdata,
+                                "%pV ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lld lvb_type: %d\n",
+                                &vaf,
+                                ldlm_lock_to_ns_name(lock),
+                                lock, lock->l_handle.h_cookie,
+                                refcount_read(&lock->l_handle.h_ref),
+                                lock->l_readers, lock->l_writers,
+                                ldlm_lockname[lock->l_granted_mode],
+                                ldlm_lockname[lock->l_req_mode],
+                                PLDLMRES(resource),
+                                atomic_read(&resource->lr_refcount),
+                                ldlm_typename[resource->lr_type],
+                                lock->l_flags, nid,
+                                lock->l_remote_handle.cookie,
+                                exp ? refcount_read(&exp->exp_handle.h_ref) : -99,
+                                lock->l_pid, lock->l_callback_timestamp,
+                                lock->l_lvb_type);
                break;
        }
        va_end(args);
+       ldlm_resource_putref(resource);
 }
 EXPORT_SYMBOL(_ldlm_lock_debug);