Whamcloud - gitweb
b=17887
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.c
index 286197f..b3b36a8 100644 (file)
@@ -1,27 +1,42 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
- *   Author: Peter Braam <braam@clusterfs.com>
- *   Author: Phil Schwan <phil@clusterfs.com>
+ * GPL HEADER START
  *
- *   This file is part of the Lustre file system, http://www.lustre.org
- *   Lustre is a trademark of Cluster File Systems, Inc.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   You may have signed or agreed to another license before downloading
- *   this software.  If so, you are bound by the terms and conditions
- *   of that agreement, and the following does not apply to you.  See the
- *   LICENSE file included with this distribution for more information.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   If you did not agree to a different license, then this copy of Lustre
- *   is open source software; you can redistribute it and/or modify it
- *   under the terms of version 2 of the GNU General Public License as
- *   published by the Free Software Foundation.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   In either case, Lustre is distributed in the hope that it will be
- *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
- *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   license text for more details.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/ldlm/ldlm_lock.c
+ *
+ * Author: Peter Braam <braam@clusterfs.com>
+ * Author: Phil Schwan <phil@clusterfs.com>
  */
 
 #define DEBUG_SUBSYSTEM S_LDLM
@@ -33,7 +48,6 @@
 # endif
 #else
 # include <liblustre.h>
-# include <libcfs/kp30.h>
 #endif
 
 #include <obd_class.h>
@@ -48,7 +62,8 @@ char *ldlm_lockname[] = {
         [LCK_CW] "CW",
         [LCK_CR] "CR",
         [LCK_NL] "NL",
-        [LCK_GROUP] "GROUP"
+        [LCK_GROUP] "GROUP",
+        [LCK_COS] "COS"
 };
 
 char *ldlm_typename[] = {
@@ -145,6 +160,7 @@ void ldlm_lock_put(struct ldlm_lock *lock)
                 LASSERT(list_empty(&lock->l_pending_chain));
 
                 atomic_dec(&res->lr_namespace->ns_locks);
+                lu_ref_del(&res->lr_reference, "lock", lock);
                 ldlm_resource_putref(res);
                 lock->l_resource = NULL;
                 if (lock->l_export) {
@@ -156,8 +172,9 @@ void ldlm_lock_put(struct ldlm_lock *lock)
                         OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
 
                 ldlm_interval_free(ldlm_interval_detach(lock));
-                OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle, 
-                               ldlm_lock_free);
+                lu_ref_fini(&lock->l_reference);
+                OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
+                                ldlm_lock_free);
         }
 
         EXIT;
@@ -170,8 +187,8 @@ int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
                 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
                 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
                 list_del_init(&lock->l_lru);
+                LASSERT(ns->ns_nr_unused > 0);
                 ns->ns_nr_unused--;
-                LASSERT(ns->ns_nr_unused >= 0);
                 rc = 1;
         }
         return rc;
@@ -194,6 +211,7 @@ void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
         struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
         lock->l_last_used = cfs_time_current();
         LASSERT(list_empty(&lock->l_lru));
+        LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
         list_add_tail(&lock->l_lru, &ns->ns_unused_list);
         LASSERT(ns->ns_nr_unused >= 0);
         ns->ns_nr_unused++;
@@ -250,11 +268,10 @@ int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
         }
         lock->l_destroyed = 1;
 
-        if (lock->l_export)
-                spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
-        list_del_init(&lock->l_export_chain);
-        if (lock->l_export)
-                spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
+        if (lock->l_export && lock->l_export->exp_lock_hash &&
+            !hlist_unhashed(&lock->l_exp_hash))
+                lustre_hash_del(lock->l_export->exp_lock_hash,
+                                &lock->l_remote_handle, &lock->l_exp_hash);
 
         ldlm_lock_remove_from_lru(lock);
         class_handle_unhash(&lock->l_handle);
@@ -282,8 +299,10 @@ void ldlm_lock_destroy(struct ldlm_lock *lock)
         unlock_res_and_lock(lock);
 
         /* drop reference from hashtable only for first destroy */
-        if (first)
-                LDLM_LOCK_PUT(lock);
+        if (first) {
+                lu_ref_del(&lock->l_reference, "hash", lock);
+                LDLM_LOCK_RELEASE(lock);
+        }
         EXIT;
 }
 
@@ -293,8 +312,10 @@ void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
         ENTRY;
         first = ldlm_lock_destroy_internal(lock);
         /* drop reference from hashtable only for first destroy */
-        if (first)
-                LDLM_LOCK_PUT(lock);
+        if (first) {
+                lu_ref_del(&lock->l_reference, "hash", lock);
+                LDLM_LOCK_RELEASE(lock);
+        }
         EXIT;
 }
 
@@ -324,23 +345,32 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
 
         spin_lock_init(&lock->l_lock);
         lock->l_resource = ldlm_resource_getref(resource);
+        lu_ref_add(&resource->lr_reference, "lock", lock);
 
         atomic_set(&lock->l_refc, 2);
         CFS_INIT_LIST_HEAD(&lock->l_res_link);
         CFS_INIT_LIST_HEAD(&lock->l_lru);
-        CFS_INIT_LIST_HEAD(&lock->l_export_chain);
         CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
         CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
         CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
+        CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
         cfs_waitq_init(&lock->l_waitq);
         lock->l_blocking_lock = NULL;
         CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
         CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
+        CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
 
         atomic_inc(&resource->lr_namespace->ns_locks);
         CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
         class_handle_hash(&lock->l_handle, lock_handle_addref);
 
+        CFS_INIT_LIST_HEAD(&lock->l_extents_list);
+        spin_lock_init(&lock->l_extents_list_lock);
+        CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
+        lu_ref_init(&lock->l_reference);
+        lu_ref_add(&lock->l_reference, "hash", lock);
+        lock->l_callback_timeout = 0;
+
         RETURN(lock);
 }
 
@@ -371,18 +401,32 @@ int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
         unlock_res_and_lock(lock);
 
         newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
+        lu_ref_add(&newres->lr_reference, "lock", lock);
         if (newres == NULL)
                 RETURN(-ENOMEM);
-
-        lock_res_and_lock(lock);
-        LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
-                       sizeof(lock->l_resource->lr_name)) != 0);
-        lock_res(newres);
+        /*
+         * 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.
+         */
+        spin_lock(&lock->l_lock);
+        oldres = lock->l_resource;
+        if (oldres < newres) {
+                lock_res(oldres);
+                lock_res_nested(newres, LRT_NEW);
+        } else {
+                lock_res(newres);
+                lock_res_nested(oldres, LRT_NEW);
+        }
+        LASSERT(memcmp(new_resid, &oldres->lr_name,
+                       sizeof oldres->lr_name) != 0);
         lock->l_resource = newres;
         unlock_res(oldres);
         unlock_res_and_lock(lock);
 
         /* ...and the flowers are still standing! */
+        lu_ref_del(&oldres->lr_reference, "lock", lock);
         ldlm_resource_putref(oldres);
 
         RETURN(0);
@@ -418,6 +462,7 @@ struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
         ns = lock->l_resource->lr_namespace;
         LASSERT(ns != NULL);
 
+        lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
         lock_res_and_lock(lock);
 
         /* It's unlikely but possible that someone marked the lock as
@@ -445,14 +490,6 @@ struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
         return retval;
 }
 
-struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
-                                      const struct lustre_handle *handle)
-{
-        struct ldlm_lock *retval = NULL;
-        retval = __ldlm_handle2lock(handle, 0);
-        return retval;
-}
-
 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
 {
         struct obd_export *exp = lock->l_export?:lock->l_conn_export;
@@ -554,14 +591,47 @@ void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 {
         ldlm_lock_remove_from_lru(lock);
-        if (mode & (LCK_NL | LCK_CR | LCK_PR))
+        if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
                 lock->l_readers++;
-        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
+                lu_ref_add_atomic(&lock->l_reference, "reader", lock);
+        }
+        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
                 lock->l_writers++;
+                lu_ref_add_atomic(&lock->l_reference, "writer", lock);
+        }
         LDLM_LOCK_GET(lock);
+        lu_ref_add_atomic(&lock->l_reference, "user", lock);
         LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
 }
 
+/**
+ * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
+ * or destroyed.
+ *
+ * \retval 0 success, lock was addref-ed
+ *
+ * \retval -EAGAIN lock is being canceled.
+ */
+int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
+{
+        struct ldlm_lock *lock;
+        int               result;
+
+        result = -EAGAIN;
+        lock = ldlm_handle2lock(lockh);
+        if (lock != NULL) {
+                lock_res_and_lock(lock);
+                if (lock->l_readers != 0 || lock->l_writers != 0 ||
+                    !(lock->l_flags & LDLM_FL_CBPENDING)) {
+                        ldlm_lock_addref_internal_nolock(lock, mode);
+                        result = 0;
+                }
+                unlock_res_and_lock(lock);
+                LDLM_LOCK_PUT(lock);
+        }
+        return result;
+}
+
 /* only called for local locks */
 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
 {
@@ -570,25 +640,39 @@ void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
         unlock_res_and_lock(lock);
 }
 
-void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
+/* only called in ldlm_flock_destroy and for local locks.
+ *  * for LDLM_FLOCK type locks, l_blocking_ast is null, and
+ *   * ldlm_lock_remove_from_lru() does nothing, it is safe
+ *    * for ldlm_flock_destroy usage by dropping some code */
+void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 {
-        struct ldlm_namespace *ns;
-        ENTRY;
-
-        lock_res_and_lock(lock);
-
-        ns = lock->l_resource->lr_namespace;
-
         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
         if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
                 LASSERT(lock->l_readers > 0);
+                lu_ref_del(&lock->l_reference, "reader", lock);
                 lock->l_readers--;
         }
-        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
+        if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
                 LASSERT(lock->l_writers > 0);
+                lu_ref_del(&lock->l_reference, "writer", lock);
                 lock->l_writers--;
         }
 
+        lu_ref_del(&lock->l_reference, "user", lock);
+        LDLM_LOCK_RELEASE(lock);    /* matches the LDLM_LOCK_GET() in addref */
+}
+
+void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
+{
+        struct ldlm_namespace *ns;
+        ENTRY;
+
+        lock_res_and_lock(lock);
+
+        ns = lock->l_resource->lr_namespace;
+
+        ldlm_lock_decref_internal_nolock(lock, mode);
+
         if (lock->l_flags & LDLM_FL_LOCAL &&
             !lock->l_readers && !lock->l_writers) {
                 /* If this is a local lock on a server namespace and this was
@@ -610,29 +694,34 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
                 ldlm_lock_remove_from_lru(lock);
                 unlock_res_and_lock(lock);
+
+                if (lock->l_flags & LDLM_FL_FAIL_LOC)
+                        OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
+
                 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
                     ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
                         ldlm_handle_bl_callback(ns, NULL, lock);
         } else if (ns_is_client(ns) &&
                    !lock->l_readers && !lock->l_writers &&
-                   !(lock->l_flags & LDLM_FL_NO_LRU) &&
                    !(lock->l_flags & LDLM_FL_BL_AST)) {
                 /* If this is a client-side namespace and this was the last
                  * reference, put it on the LRU. */
                 ldlm_lock_add_to_lru(lock);
                 unlock_res_and_lock(lock);
-                /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE 
-                 * are not supported by the server, otherwise, it is done on 
+
+                if (lock->l_flags & LDLM_FL_FAIL_LOC)
+                        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) && 
+                if (!exp_connect_cancelset(lock->l_conn_export) &&
                     !ns_connect_lru_resize(ns))
                         ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
         } else {
                 unlock_res_and_lock(lock);
         }
 
-        LDLM_LOCK_PUT(lock);    /* matches the ldlm_lock_get in addref */
-
         EXIT;
 }
 
@@ -733,9 +822,9 @@ static void search_granted_lock(struct list_head *queue,
                                         /* done with mode group */
                                         break;
 
-                                /* jump to next policy group within the mode group */
+                                /* go to next policy group within mode group */
                                 tmp = policy_end->l_res_link.next;
-                                lock = list_entry(tmp, struct ldlm_lock, 
+                                lock = list_entry(tmp, struct ldlm_lock,
                                                   l_res_link);
                         }  /* loop over policy groups within the mode group */
 
@@ -747,7 +836,7 @@ static void search_granted_lock(struct list_head *queue,
                         EXIT;
                         return;
                 } else {
-                        LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
+                        LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
                         LBUG();
                 }
         }
@@ -761,7 +850,7 @@ static void search_granted_lock(struct list_head *queue,
         return;
 }
 
-static void ldlm_granted_list_add_lock(struct ldlm_lock *lock, 
+static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
                                        struct sl_insert_point *prev)
 {
         struct ldlm_resource *res = lock->l_resource;
@@ -769,7 +858,7 @@ static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
 
         check_res_locked(res);
 
-        ldlm_resource_dump(D_OTHER, res);
+        ldlm_resource_dump(D_INFO, res);
         CDEBUG(D_OTHER, "About to add this lock:\n");
         ldlm_lock_dump(D_OTHER, lock, 0);
 
@@ -838,7 +927,8 @@ void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
 static struct ldlm_lock *search_queue(struct list_head *queue,
                                       ldlm_mode_t *mode,
                                       ldlm_policy_data_t *policy,
-                                      struct ldlm_lock *old_lock, int flags)
+                                      struct ldlm_lock *old_lock,
+                                      int flags, int unref)
 {
         struct ldlm_lock *lock;
         struct list_head *tmp;
@@ -860,7 +950,7 @@ static struct ldlm_lock *search_queue(struct list_head *queue,
                 if (lock->l_flags & LDLM_FL_CBPENDING &&
                     !(flags & LDLM_FL_CBPENDING))
                         continue;
-                if (lock->l_flags & LDLM_FL_CBPENDING &&
+                if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
                     lock->l_readers == 0 && lock->l_writers == 0)
                         continue;
 
@@ -887,7 +977,8 @@ static struct ldlm_lock *search_queue(struct list_head *queue,
                       policy->l_inodebits.bits))
                         continue;
 
-                if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
+                if (!unref &&
+                    (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
                         continue;
 
                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
@@ -907,11 +998,16 @@ static struct ldlm_lock *search_queue(struct list_head *queue,
         return NULL;
 }
 
-void ldlm_lock_allow_match(struct ldlm_lock *lock)
+void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
 {
-        lock_res_and_lock(lock);
         lock->l_flags |= LDLM_FL_LVB_READY;
         cfs_waitq_signal(&lock->l_waitq);
+}
+
+void ldlm_lock_allow_match(struct ldlm_lock *lock)
+{
+        lock_res_and_lock(lock);
+        ldlm_lock_allow_match_locked(lock);
         unlock_res_and_lock(lock);
 }
 
@@ -942,7 +1038,7 @@ void ldlm_lock_allow_match(struct ldlm_lock *lock)
 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
                             const struct ldlm_res_id *res_id, ldlm_type_t type,
                             ldlm_policy_data_t *policy, ldlm_mode_t mode,
-                            struct lustre_handle *lockh)
+                            struct lustre_handle *lockh, int unref)
 {
         struct ldlm_resource *res;
         struct ldlm_lock *lock, *old_lock = NULL;
@@ -965,23 +1061,28 @@ ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
                 RETURN(0);
         }
 
+        LDLM_RESOURCE_ADDREF(res);
         lock_res(res);
 
-        lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
+        lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
+                            flags, unref);
         if (lock != NULL)
                 GOTO(out, rc = 1);
         if (flags & LDLM_FL_BLOCK_GRANTED)
                 GOTO(out, rc = 0);
-        lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
+        lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
+                            flags, unref);
         if (lock != NULL)
                 GOTO(out, rc = 1);
-        lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
+        lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
+                            flags, unref);
         if (lock != NULL)
                 GOTO(out, rc = 1);
 
         EXIT;
  out:
         unlock_res(res);
+        LDLM_RESOURCE_DELREF(res);
         ldlm_resource_putref(res);
 
         if (lock) {
@@ -995,16 +1096,17 @@ ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
                                                                  NULL);
                                 if (err) {
                                         if (flags & LDLM_FL_TEST_LOCK)
-                                                LDLM_LOCK_PUT(lock);
+                                                LDLM_LOCK_RELEASE(lock);
                                         else
-                                                ldlm_lock_decref_internal(lock, mode);
+                                                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);
+                        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,
@@ -1029,7 +1131,7 @@ ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
                 }
 
                 if (flags & LDLM_FL_TEST_LOCK)
-                        LDLM_LOCK_PUT(lock);
+                        LDLM_LOCK_RELEASE(lock);
 
         } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
                 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
@@ -1051,9 +1153,7 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
                                    const struct ldlm_res_id *res_id,
                                    ldlm_type_t type,
                                    ldlm_mode_t mode,
-                                   ldlm_blocking_callback blocking,
-                                   ldlm_completion_callback completion,
-                                   ldlm_glimpse_callback glimpse,
+                                   const struct ldlm_callback_suite *cbs,
                                    void *data, __u32 lvb_len)
 {
         struct ldlm_lock *lock;
@@ -1072,10 +1172,13 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
 
         lock->l_req_mode = mode;
         lock->l_ast_data = data;
-        lock->l_blocking_ast = blocking;
-        lock->l_completion_ast = completion;
-        lock->l_glimpse_ast = glimpse;
         lock->l_pid = cfs_curproc_pid();
+        if (cbs) {
+                lock->l_blocking_ast = cbs->lcs_blocking;
+                lock->l_completion_ast = cbs->lcs_completion;
+                lock->l_glimpse_ast = cbs->lcs_glimpse;
+                lock->l_weigh_ast = cbs->lcs_weigh;
+        }
 
         lock->l_tree_node = NULL;
         /* if this is the extent lock, allocate the interval tree node */
@@ -1113,7 +1216,7 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
         struct ldlm_interval *node = NULL;
         ENTRY;
 
-        do_gettimeofday(&lock->l_enqueued_time);
+        lock->l_last_activity = cfs_time_current_sec();
         /* 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) {
@@ -1126,7 +1229,7 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
                          * work here is done. */
                         if (lock != *lockp) {
                                 ldlm_lock_destroy(lock);
-                                LDLM_LOCK_PUT(lock);
+                                LDLM_LOCK_RELEASE(lock);
                         }
                         *flags |= LDLM_FL_LOCK_CHANGED;
                         RETURN(0);
@@ -1246,7 +1349,7 @@ int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
 }
 
 /* Helper function for ldlm_run_ast_work().
- * 
+ *
  * Send an existing rpc set specified by @arg->set and then
  * destroy it. Create new one if @do_create flag is set. */
 static void
@@ -1284,11 +1387,11 @@ ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
 
         ldlm_lock2desc(lock->l_blocking_lock, &d);
 
-        LDLM_LOCK_PUT(lock->l_blocking_lock);
-        lock->l_blocking_lock = NULL;
-        lock->l_blocking_ast(lock, &d, (void *)arg, 
+        lock->l_blocking_ast(lock, &d, (void *)arg,
                              LDLM_CB_BLOCKING);
-        LDLM_LOCK_PUT(lock);
+        LDLM_LOCK_RELEASE(lock->l_blocking_lock);
+        lock->l_blocking_lock = NULL;
+        LDLM_LOCK_RELEASE(lock);
 
         RETURN(1);
 }
@@ -1326,7 +1429,7 @@ ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
                 completion_callback(lock, 0, (void *)arg);
                 rc = 1;
         }
-        LDLM_LOCK_PUT(lock);
+        LDLM_LOCK_RELEASE(lock);
 
         RETURN(rc);
 }
@@ -1335,10 +1438,10 @@ static int
 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
 {
         struct ldlm_lock_desc desc;
-        struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
+        struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
         ENTRY;
 
-        list_del_init(&lock->l_export_chain);
+        list_del_init(&lock->l_rk_ast);
 
         /* the desc just pretend to exclusive */
         ldlm_lock2desc(lock, &desc);
@@ -1346,7 +1449,7 @@ ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
         desc.l_granted_mode = 0;
 
         lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
-        LDLM_LOCK_PUT(lock);
+        LDLM_LOCK_RELEASE(lock);
 
         RETURN(1);
 }
@@ -1355,7 +1458,7 @@ int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
 {
         struct ldlm_cb_set_arg arg;
         struct list_head *tmp, *pos;
-        int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
+        int (*work_ast_lock)(struct list_head *tmp,struct ldlm_cb_set_arg *arg);
         int ast_count;
         ENTRY;
 
@@ -1396,7 +1499,7 @@ int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
         else
                 /* In case when number of ASTs is multiply of
                  * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
-                 * @arg.set must be destroyed here, otherwise we get 
+                 * @arg.set must be destroyed here, otherwise we get
                  * write memory leaking. */
                 ptlrpc_set_destroy(arg.set);
 
@@ -1427,9 +1530,11 @@ void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
 
                         ldlm_resource_getref(res);
                         spin_unlock(&ns->ns_hash_lock);
+                        LDLM_RESOURCE_ADDREF(res);
 
                         rc = reprocess_one_queue(res, NULL);
 
+                        LDLM_RESOURCE_DELREF(res);
                         spin_lock(&ns->ns_hash_lock);
                         tmp = tmp->next;
                         ldlm_resource_putref_locked(res);
@@ -1523,7 +1628,7 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
 
         /* Yes, second time, just in case it was added again while we were
            running with no res lock in ldlm_cancel_callback */
-        ldlm_del_waiting_lock(lock); 
+        ldlm_del_waiting_lock(lock);
         ldlm_resource_unlink_lock(lock);
         ldlm_lock_destroy_nolock(lock);
 
@@ -1532,7 +1637,7 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
 
         /* Make sure we will not be called again for same lock what is possible
          * if not to zero out lock->l_granted_mode */
-        lock->l_granted_mode = 0;
+        lock->l_granted_mode = LCK_MINMODE;
         unlock_res_and_lock(lock);
 
         EXIT;
@@ -1551,30 +1656,61 @@ int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
         RETURN(0);
 }
 
-void ldlm_cancel_locks_for_export(struct obd_export *exp)
+void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
 {
-        struct ldlm_lock *lock;
+        struct obd_export    *exp = data;
+        struct ldlm_lock     *lock = obj;
         struct ldlm_resource *res;
 
-        spin_lock(&exp->exp_ldlm_data.led_lock);
-        while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
-                lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
-                                  struct ldlm_lock, l_export_chain);
-                res = ldlm_resource_getref(lock->l_resource);
-                LDLM_LOCK_GET(lock);
-                spin_unlock(&exp->exp_ldlm_data.led_lock);
+        res = ldlm_resource_getref(lock->l_resource);
+        LDLM_LOCK_GET(lock);
 
-                LDLM_DEBUG(lock, "export %p", exp);
-                ldlm_res_lvbo_update(res, NULL, 0, 1);
+        LDLM_DEBUG(lock, "export %p", exp);
+        ldlm_res_lvbo_update(res, NULL, 0, 1);
+        ldlm_lock_cancel(lock);
+        ldlm_reprocess_all(res);
+        ldlm_resource_putref(res);
+        LDLM_LOCK_RELEASE(lock);
+}
 
-                ldlm_lock_cancel(lock);
-                ldlm_reprocess_all(res);
+void ldlm_cancel_locks_for_export(struct obd_export *exp)
+{
+        lustre_hash_for_each_empty(exp->exp_lock_hash,
+                                   ldlm_cancel_locks_for_export_cb, exp);
+}
 
-                ldlm_resource_putref(res);
-                LDLM_LOCK_PUT(lock);
-                spin_lock(&exp->exp_ldlm_data.led_lock);
-        }
-        spin_unlock(&exp->exp_ldlm_data.led_lock);
+/**
+ * Downgrade an exclusive lock.
+ *
+ * A fast variant of ldlm_lock_convert for convertion of exclusive
+ * locks. The convertion is always successful.
+ *
+ * \param lock A lock to convert
+ * \param new_mode new lock mode
+ */
+void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
+{
+        struct ldlm_namespace *ns;
+        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.
+         */
+        ns = lock->l_resource->lr_namespace;
+        ldlm_pool_del(&ns->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;
 }
 
 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
@@ -1601,7 +1737,7 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
                 RETURN(NULL);
 
-        LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
+        LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
                  "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
 
         lock_res_and_lock(lock);
@@ -1612,8 +1748,8 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         old_mode = lock->l_req_mode;
         lock->l_req_mode = new_mode;
         if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
-                /* remember the lock position where the lock might be 
-                 * added back to the granted list later and also 
+                /* remember the 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;
@@ -1622,7 +1758,7 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         } else {
                 ldlm_resource_unlink_lock(lock);
                 if (res->lr_type == LDLM_EXTENT) {
-                        /* FIXME: ugly code, I have to attach the lock to a 
+                        /* FIXME: ugly code, I have to attach the lock to a
                          * interval node again since perhaps it will be granted
                          * soon */
                         CFS_INIT_LIST_HEAD(&node->li_group);
@@ -1630,6 +1766,12 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
                         node = NULL;
                 }
         }
+        
+        /* 
+         * Remove old lock from the pool before adding the lock with new
+         * mode below in ->policy()
+         */
+        ldlm_pool_del(&ns->ns_pool, lock);
 
         /* If this is a local resource, put it on the appropriate list. */
         if (ns_is_client(res->lr_namespace)) {
@@ -1710,7 +1852,7 @@ void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
                   lock->l_resource->lr_name.name[1],
                   lock->l_resource->lr_name.name[2]);
         CDEBUG(level, "  Req mode: %s, grant mode: %s, rc: %u, read: %d, "
-               "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
+               "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
                ldlm_lockname[lock->l_granted_mode],
                atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
                lock->l_flags);
@@ -1752,15 +1894,15 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
 {
         va_list args;
         cfs_debug_limit_state_t *cdls = data->msg_cdls;
-        
+
         va_start(args, fmt);
 
         if (lock->l_resource == NULL) {
-                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
                                    data->msg_fn, data->msg_line, fmt, args,
                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
-                       "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
-                       LPX64" expref: %d pid: %u\n", lock,
+                                   "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
+                       LPX64" expref: %d pid: %u timeout: %lu\n", lock,
                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
                        lock->l_readers, lock->l_writers,
                        ldlm_lockname[lock->l_granted_mode],
@@ -1768,19 +1910,19 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
                        lock->l_flags, lock->l_remote_handle.cookie,
                        lock->l_export ?
                        atomic_read(&lock->l_export->exp_refcount) : -99,
-                       lock->l_pid);
+                       lock->l_pid, lock->l_callback_timeout);
                 va_end(args);
                 return;
         }
 
         switch (lock->l_resource->lr_type) {
         case LDLM_EXTENT:
-                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
                                    data->msg_fn, data->msg_line, fmt, args,
                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
                        "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
-                       "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
-                       " expref: %d pid: %u\n",
+                                   "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
+                       " expref: %d pid: %u timeout %lu\n",
                        lock->l_resource->lr_namespace->ns_name, lock,
                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
                        lock->l_readers, lock->l_writers,
@@ -1796,16 +1938,16 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
                        lock->l_flags, lock->l_remote_handle.cookie,
                        lock->l_export ?
                        atomic_read(&lock->l_export->exp_refcount) : -99,
-                       lock->l_pid);
+                       lock->l_pid, lock->l_callback_timeout);
                 break;
 
         case LDLM_FLOCK:
-                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
                                    data->msg_fn, data->msg_line, fmt, args,
                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
                        "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
-                       "["LPU64"->"LPU64"] flags: %x remote: "LPX64
-                       " expref: %d pid: %u\n",
+                                   "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
+                       " expref: %d pid: %u timeout: %lu\n",
                        lock->l_resource->lr_namespace->ns_name, lock,
                        lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
                        lock->l_readers, lock->l_writers,
@@ -1821,16 +1963,16 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
                        lock->l_flags, lock->l_remote_handle.cookie,
                        lock->l_export ?
                        atomic_read(&lock->l_export->exp_refcount) : -99,
-                       lock->l_pid);
+                       lock->l_pid, lock->l_callback_timeout);
                 break;
 
         case LDLM_IBITS:
-                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
                                    data->msg_fn, data->msg_line, fmt, args,
                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
                        "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
-                       "flags: %x remote: "LPX64" expref: %d "
-                       "pid %u\n",
+                                   "flags: "LPX64" remote: "LPX64" expref: %d "
+                       "pid: %u timeout: %lu\n",
                        lock->l_resource->lr_namespace->ns_name,
                        lock, lock->l_handle.h_cookie,
                        atomic_read (&lock->l_refc),
@@ -1845,15 +1987,15 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
                        lock->l_flags, lock->l_remote_handle.cookie,
                        lock->l_export ?
                        atomic_read(&lock->l_export->exp_refcount) : -99,
-                       lock->l_pid);
+                       lock->l_pid, lock->l_callback_timeout);
                 break;
 
         default:
-                libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
+                libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
                                    data->msg_fn, data->msg_line, fmt, args,
                        " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
-                       "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
-                       "remote: "LPX64" expref: %d pid: %u\n",
+                                   "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
+                       "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
                        lock->l_resource->lr_namespace->ns_name,
                        lock, lock->l_handle.h_cookie,
                        atomic_read (&lock->l_refc),
@@ -1867,7 +2009,7 @@ void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
                        lock->l_flags, lock->l_remote_handle.cookie,
                        lock->l_export ?
                        atomic_read(&lock->l_export->exp_refcount) : -99,
-                       lock->l_pid);
+                       lock->l_pid, lock->l_callback_timeout);
                 break;
         }
         va_end(args);