Whamcloud - gitweb
LU-4269 ldlm: Hold lock when clearing flag
[fs/lustre-release.git] / lustre / ldlm / ldlm_lock.c
index 165fe68..e7a5072 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Whamcloud, Inc.
+ * Copyright (c) 2010, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -112,20 +112,20 @@ void ldlm_convert_policy_to_wire(ldlm_type_t type,
  * Converts lock policy from on the wire lock_desc format to local format
  */
 void ldlm_convert_policy_to_local(struct obd_export *exp, ldlm_type_t type,
-                                  const ldlm_wire_policy_data_t *wpolicy,
-                                  ldlm_policy_data_t *lpolicy)
+                                 const ldlm_wire_policy_data_t *wpolicy,
+                                 ldlm_policy_data_t *lpolicy)
 {
-        ldlm_policy_wire_to_local_t convert;
-        int new_client;
+       ldlm_policy_wire_to_local_t convert;
+       int new_client;
 
-        /** some badnes for 2.0.0 clients, but 2.0.0 isn't supported */
-        new_client = (exp->exp_connect_flags & OBD_CONNECT_FULL20) != 0;
-        if (new_client)
-               convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
-        else
-               convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
+       /** some badness for 2.0.0 clients, but 2.0.0 isn't supported */
+       new_client = (exp_connect_flags(exp) & OBD_CONNECT_FULL20) != 0;
+       if (new_client)
+               convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
+       else
+               convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
 
-        convert(wpolicy, lpolicy);
+       convert(wpolicy, lpolicy);
 }
 
 char *ldlm_it2str(int it)
@@ -156,7 +156,7 @@ char *ldlm_it2str(int it)
 }
 EXPORT_SYMBOL(ldlm_it2str);
 
-extern cfs_mem_cache_t *ldlm_lock_slab;
+extern struct kmem_cache *ldlm_lock_slab;
 
 #ifdef HAVE_SERVER_SUPPORT
 static ldlm_processing_policy ldlm_processing_policy_table[] = {
@@ -186,7 +186,9 @@ EXPORT_SYMBOL(ldlm_register_intent);
  */
 
 
-/*
+/**
+ * Get a reference on a lock.
+ *
  * Lock refcounts, during creation:
  *   - one special one for allocation, dec'd only once in destroy
  *   - one for being a lock that's in-use
@@ -194,25 +196,30 @@ EXPORT_SYMBOL(ldlm_register_intent);
  */
 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
 {
-        cfs_atomic_inc(&lock->l_refc);
+       atomic_inc(&lock->l_refc);
         return lock;
 }
 EXPORT_SYMBOL(ldlm_lock_get);
 
+/**
+ * Release lock reference.
+ *
+ * Also frees the lock if it was last reference.
+ */
 void ldlm_lock_put(struct ldlm_lock *lock)
 {
         ENTRY;
 
         LASSERT(lock->l_resource != LP_POISON);
-        LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
-        if (cfs_atomic_dec_and_test(&lock->l_refc)) {
+       LASSERT(atomic_read(&lock->l_refc) > 0);
+       if (atomic_dec_and_test(&lock->l_refc)) {
                 struct ldlm_resource *res;
 
                 LDLM_DEBUG(lock,
                            "final lock_put on destroyed lock, freeing it.");
 
                 res = lock->l_resource;
-                LASSERT(lock->l_destroyed);
+               LASSERT(ldlm_is_destroyed(lock));
                 LASSERT(cfs_list_empty(&lock->l_res_link));
                 LASSERT(cfs_list_empty(&lock->l_pending_chain));
 
@@ -227,7 +234,7 @@ void ldlm_lock_put(struct ldlm_lock *lock)
                 }
 
                 if (lock->l_lvb_data != NULL)
-                        OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
+                        OBD_FREE_LARGE(lock->l_lvb_data, lock->l_lvb_len);
 
                 ldlm_interval_free(ldlm_interval_detach(lock));
                 lu_ref_fini(&lock->l_reference);
@@ -238,89 +245,119 @@ void ldlm_lock_put(struct ldlm_lock *lock)
 }
 EXPORT_SYMBOL(ldlm_lock_put);
 
+/**
+ * Removes LDLM lock \a lock from LRU. Assumes LRU is already locked.
+ */
 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
 {
-        int rc = 0;
-        if (!cfs_list_empty(&lock->l_lru)) {
-                struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
-
-                LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
-                cfs_list_del_init(&lock->l_lru);
-                if (lock->l_flags & LDLM_FL_SKIPPED)
-                        lock->l_flags &= ~LDLM_FL_SKIPPED;
-                LASSERT(ns->ns_nr_unused > 0);
-                ns->ns_nr_unused--;
-                rc = 1;
-        }
-        return rc;
+       int rc = 0;
+       if (!cfs_list_empty(&lock->l_lru)) {
+               struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
+
+               LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
+               cfs_list_del_init(&lock->l_lru);
+               LASSERT(ns->ns_nr_unused > 0);
+               ns->ns_nr_unused--;
+               rc = 1;
+       }
+       return rc;
 }
 
+/**
+ * Removes LDLM lock \a lock from LRU. Obtains the LRU lock first.
+ */
 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
 {
-        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
-        int rc;
+       struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
+       int rc;
 
-        ENTRY;
-        if (lock->l_ns_srv) {
-                LASSERT(cfs_list_empty(&lock->l_lru));
-                RETURN(0);
-        }
+       ENTRY;
+       if (ldlm_is_ns_srv(lock)) {
+               LASSERT(cfs_list_empty(&lock->l_lru));
+               RETURN(0);
+       }
 
-        cfs_spin_lock(&ns->ns_lock);
-        rc = ldlm_lock_remove_from_lru_nolock(lock);
-        cfs_spin_unlock(&ns->ns_lock);
-        EXIT;
-        return rc;
+       spin_lock(&ns->ns_lock);
+       rc = ldlm_lock_remove_from_lru_nolock(lock);
+       spin_unlock(&ns->ns_lock);
+       EXIT;
+       return rc;
 }
 
+/**
+ * Adds LDLM lock \a lock to namespace LRU. Assumes LRU is already locked.
+ */
 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
 {
-        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
+       struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
 
-        lock->l_last_used = cfs_time_current();
-        LASSERT(cfs_list_empty(&lock->l_lru));
-        LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
-        cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
-        LASSERT(ns->ns_nr_unused >= 0);
-        ns->ns_nr_unused++;
+       lock->l_last_used = cfs_time_current();
+       LASSERT(cfs_list_empty(&lock->l_lru));
+       LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
+       cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
+       ldlm_clear_skipped(lock);
+       LASSERT(ns->ns_nr_unused >= 0);
+       ns->ns_nr_unused++;
 }
 
+/**
+ * Adds LDLM lock \a lock to namespace LRU. Obtains necessary LRU locks
+ * first.
+ */
 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
 {
-        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
+       struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
 
-        ENTRY;
-        cfs_spin_lock(&ns->ns_lock);
-        ldlm_lock_add_to_lru_nolock(lock);
-        cfs_spin_unlock(&ns->ns_lock);
-        EXIT;
+       ENTRY;
+       spin_lock(&ns->ns_lock);
+       ldlm_lock_add_to_lru_nolock(lock);
+       spin_unlock(&ns->ns_lock);
+       EXIT;
 }
 
+/**
+ * Moves LDLM lock \a lock that is already in namespace LRU to the tail of
+ * the LRU. Performs necessary LRU locking
+ */
 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
 {
-        struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
+       struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
 
-        ENTRY;
-        if (lock->l_ns_srv) {
-                LASSERT(cfs_list_empty(&lock->l_lru));
-                EXIT;
-                return;
-        }
+       ENTRY;
+       if (ldlm_is_ns_srv(lock)) {
+               LASSERT(cfs_list_empty(&lock->l_lru));
+               EXIT;
+               return;
+       }
 
-        cfs_spin_lock(&ns->ns_lock);
-        if (!cfs_list_empty(&lock->l_lru)) {
-                ldlm_lock_remove_from_lru_nolock(lock);
-                ldlm_lock_add_to_lru_nolock(lock);
-        }
-        cfs_spin_unlock(&ns->ns_lock);
-        EXIT;
+       spin_lock(&ns->ns_lock);
+       if (!cfs_list_empty(&lock->l_lru)) {
+               ldlm_lock_remove_from_lru_nolock(lock);
+               ldlm_lock_add_to_lru_nolock(lock);
+       }
+       spin_unlock(&ns->ns_lock);
+       EXIT;
 }
 
-/* This used to have a 'strict' flag, which recovery would use to mark an
+/**
+ * Helper to destroy a locked lock.
+ *
+ * Used by ldlm_lock_destroy and ldlm_lock_destroy_nolock
+ * Must be called with l_lock and lr_lock held.
+ *
+ * Does not actually free the lock data, but rather marks the lock as
+ * destroyed by setting l_destroyed field in the lock to 1.  Destroys a
+ * handle->lock association too, so that the lock can no longer be found
+ * and removes the lock from LRU list.  Actual lock freeing occurs when
+ * last lock reference goes away.
+ *
+ * Original comment (of some historical value):
+ * This used to have a 'strict' flag, which recovery would use to mark an
  * in-use lock as needing-to-die.  Lest I am ever tempted to put it back, I
  * shall explain why it's gone: with the new hash table scheme, once you call
  * ldlm_lock_destroy, you can never drop your final references on this lock.
- * Because it's not in the hash table anymore.  -phil */
+ * Because it's not in the hash table anymore.  -phil
+ */
 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
 {
         ENTRY;
@@ -335,12 +372,12 @@ int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
                 LBUG();
         }
 
-        if (lock->l_destroyed) {
-                LASSERT(cfs_list_empty(&lock->l_lru));
-                EXIT;
-                return 0;
-        }
-        lock->l_destroyed = 1;
+       if (ldlm_is_destroyed(lock)) {
+               LASSERT(cfs_list_empty(&lock->l_lru));
+               EXIT;
+               return 0;
+       }
+       ldlm_set_destroyed(lock);
 
        if (lock->l_export && lock->l_export->exp_lock_hash) {
                /* NB: it's safe to call cfs_hash_del() even lock isn't
@@ -369,6 +406,9 @@ int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
         return 1;
 }
 
+/**
+ * Destroys a LDLM lock \a lock. Performs necessary locking first.
+ */
 void ldlm_lock_destroy(struct ldlm_lock *lock)
 {
         int first;
@@ -385,6 +425,9 @@ void ldlm_lock_destroy(struct ldlm_lock *lock)
         EXIT;
 }
 
+/**
+ * Destroys a LDLM lock \a lock that is already locked.
+ */
 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
 {
         int first;
@@ -415,7 +458,10 @@ struct portals_handle_ops lock_handle_ops = {
        .hop_free   = lock_handle_free,
 };
 
-/*
+/**
+ *
+ * Allocate and initialize new lock structure.
+ *
  * usage: pass in a resource on which you have done ldlm_resource_get
  *        new lock will take over the refcount.
  * returns: lock with refcount 2 - one for current caller and one for remote
@@ -428,26 +474,26 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
         if (resource == NULL)
                 LBUG();
 
-        OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
+       OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, __GFP_IO);
         if (lock == NULL)
                 RETURN(NULL);
 
-        cfs_spin_lock_init(&lock->l_lock);
+       spin_lock_init(&lock->l_lock);
         lock->l_resource = resource;
         lu_ref_add(&resource->lr_reference, "lock", lock);
 
-        cfs_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_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_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_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);
+       init_waitqueue_head(&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);
        CFS_INIT_HLIST_NODE(&lock->l_exp_flock_hash);
 
         lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
@@ -469,6 +515,11 @@ static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
         RETURN(lock);
 }
 
+/**
+ * Moves LDLM lock \a lock to another resource.
+ * This is used on client when server returns some other lock than requested
+ * (typically as a result of intent operation)
+ */
 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
                               const struct ldlm_res_id *new_resid)
 {
@@ -506,7 +557,7 @@ int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
          * lock->l_lock, and are taken in the memory address order to avoid
          * dead-locks.
          */
-        cfs_spin_lock(&lock->l_lock);
+       spin_lock(&lock->l_lock);
         oldres = lock->l_resource;
         if (oldres < newres) {
                 lock_res(oldres);
@@ -529,73 +580,87 @@ int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
 }
 EXPORT_SYMBOL(ldlm_lock_change_resource);
 
-/*
- *  HANDLES
+/** \defgroup ldlm_handles LDLM HANDLES
+ * Ways to get hold of locks without any addresses.
+ * @{
  */
 
+/**
+ * Fills in handle for LDLM lock \a lock into supplied \a lockh
+ * Does not take any references.
+ */
 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
 {
-        lockh->cookie = lock->l_handle.h_cookie;
+       lockh->cookie = lock->l_handle.h_cookie;
 }
 EXPORT_SYMBOL(ldlm_lock2handle);
 
-/* if flags: atomically get the lock and set the flags.
- *           Return NULL if flag already set
+/**
+ * Obtain a lock reference by handle.
+ *
+ * if \a flags: atomically get the lock and set the flags.
+ *              Return NULL if flag already set
  */
-
 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
-                                     int flags)
+                                    __u64 flags)
 {
-        struct ldlm_lock *lock;
-        ENTRY;
+       struct ldlm_lock *lock;
+       ENTRY;
 
-        LASSERT(handle);
+       LASSERT(handle);
 
-        lock = class_handle2object(handle->cookie);
-        if (lock == NULL)
-                RETURN(NULL);
+       lock = class_handle2object(handle->cookie, NULL);
+       if (lock == NULL)
+               RETURN(NULL);
 
-        /* It's unlikely but possible that someone marked the lock as
-         * destroyed after we did handle2object on it */
-        if (flags == 0 && !lock->l_destroyed) {
-                lu_ref_add(&lock->l_reference, "handle", cfs_current());
-                RETURN(lock);
-        }
+       /* 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);
+               RETURN(lock);
+       }
 
-        lock_res_and_lock(lock);
+       lock_res_and_lock(lock);
 
-        LASSERT(lock->l_resource != NULL);
+       LASSERT(lock->l_resource != NULL);
 
-        lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
-        if (unlikely(lock->l_destroyed)) {
-                unlock_res_and_lock(lock);
-                CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
-                LDLM_LOCK_PUT(lock);
-                RETURN(NULL);
-        }
+       lu_ref_add_atomic(&lock->l_reference, "handle", current);
+       if (unlikely(ldlm_is_destroyed(lock))) {
+               unlock_res_and_lock(lock);
+               CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
+               LDLM_LOCK_PUT(lock);
+               RETURN(NULL);
+       }
 
-        if (flags && (lock->l_flags & flags)) {
-                unlock_res_and_lock(lock);
-                LDLM_LOCK_PUT(lock);
-                RETURN(NULL);
-        }
+       /* If we're setting flags, make sure none of them are already set. */
+       if (flags != 0) {
+               if ((lock->l_flags & flags) != 0) {
+                       unlock_res_and_lock(lock);
+                       LDLM_LOCK_PUT(lock);
+                       RETURN(NULL);
+               }
 
-        if (flags)
-                lock->l_flags |= flags;
+               lock->l_flags |= flags;
+       }
 
-        unlock_res_and_lock(lock);
-        RETURN(lock);
+       unlock_res_and_lock(lock);
+       RETURN(lock);
 }
 EXPORT_SYMBOL(__ldlm_handle2lock);
+/** @} ldlm_handles */
 
+/**
+ * Fill in "on the wire" representation for given LDLM lock into supplied
+ * lock descriptor \a desc structure.
+ */
 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
 {
-        struct obd_export *exp = lock->l_export?:lock->l_conn_export;
-        /* INODEBITS_INTEROP: If the other side does not support
-         * inodebits, reply with a plain lock descriptor.
-         */
-        if ((lock->l_resource->lr_type == LDLM_IBITS) &&
-            (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
+       struct obd_export *exp = lock->l_export ?: lock->l_conn_export;
+
+       /* INODEBITS_INTEROP: If the other side does not support
+        * inodebits, reply with a plain lock descriptor. */
+       if ((lock->l_resource->lr_type == LDLM_IBITS) &&
+           (exp && !(exp_connect_flags(exp) & OBD_CONNECT_IBITS))) {
                 /* Make sure all the right bits are set in this lock we
                    are going to pass to client */
                 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
@@ -638,16 +703,21 @@ void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
 }
 EXPORT_SYMBOL(ldlm_lock2desc);
 
+/**
+ * Add a lock to list of conflicting locks to send AST to.
+ *
+ * Only add if we have not sent a blocking AST to the lock yet.
+ */
 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
                            cfs_list_t *work_list)
 {
-        if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
-                LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
-                lock->l_flags |= LDLM_FL_AST_SENT;
-                /* If the enqueuing client said so, tell the AST recipient to
-                 * discard dirty data, rather than writing back. */
-                if (new->l_flags & LDLM_AST_DISCARD_DATA)
-                        lock->l_flags |= LDLM_FL_DISCARD_DATA;
+       if (!ldlm_is_ast_sent(lock)) {
+               LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
+               ldlm_set_ast_sent(lock);
+               /* If the enqueuing client said so, tell the AST recipient to
+                * discard dirty data, rather than writing back. */
+               if (ldlm_is_ast_discard_data(new))
+                       ldlm_set_discard_data(lock);
                 LASSERT(cfs_list_empty(&lock->l_bl_ast));
                 cfs_list_add(&lock->l_bl_ast, work_list);
                 LDLM_LOCK_GET(lock);
@@ -656,10 +726,13 @@ void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
         }
 }
 
+/**
+ * Add a lock to list of just granted locks to send completion AST to.
+ */
 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
 {
-        if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
-                lock->l_flags |= LDLM_FL_CP_REQD;
+       if (!ldlm_is_cp_reqd(lock)) {
+               ldlm_set_cp_reqd(lock);
                 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
                 LASSERT(cfs_list_empty(&lock->l_cp_ast));
                 cfs_list_add(&lock->l_cp_ast, work_list);
@@ -667,7 +740,12 @@ void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
         }
 }
 
-/* must be called with lr_lock held */
+/**
+ * Aggregator function to add AST work items into a list. Determines
+ * what sort of an AST work needs to be done and calls the proper
+ * adding function.
+ * Must be called with lr_lock held.
+ */
 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
                             cfs_list_t *work_list)
 {
@@ -680,6 +758,11 @@ void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
         EXIT;
 }
 
+/**
+ * Add specified reader/writer reference to LDLM lock with handle \a lockh.
+ * r/w reference type is determined by \a mode
+ * Calls ldlm_lock_addref_internal.
+ */
 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
 {
         struct ldlm_lock *lock;
@@ -691,6 +774,13 @@ void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
 }
 EXPORT_SYMBOL(ldlm_lock_addref);
 
+/**
+ * Helper function.
+ * Add specified reader/writer reference to LDLM lock \a lock.
+ * r/w reference type is determined by \a mode
+ * Removes lock from LRU if it is there.
+ * Assumes the LDLM lock is already locked.
+ */
 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 {
         ldlm_lock_remove_from_lru(lock);
@@ -708,8 +798,8 @@ void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 }
 
 /**
- * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
- * or destroyed.
+ * Attempts to add reader/writer reference to a lock with handle \a lockh, and
+ * fails if lock is already LDLM_FL_CBPENDING or destroyed.
  *
  * \retval 0 success, lock was addref-ed
  *
@@ -725,7 +815,7 @@ int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
         if (lock != NULL) {
                 lock_res_and_lock(lock);
                 if (lock->l_readers != 0 || lock->l_writers != 0 ||
-                    !(lock->l_flags & LDLM_FL_CBPENDING)) {
+                   !ldlm_is_cbpending(lock)) {
                         ldlm_lock_addref_internal_nolock(lock, mode);
                         result = 0;
                 }
@@ -736,7 +826,11 @@ int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
 }
 EXPORT_SYMBOL(ldlm_lock_addref_try);
 
-/* only called for local locks */
+/**
+ * Add specified reader/writer reference to LDLM lock \a lock.
+ * Locks LDLM lock and calls ldlm_lock_addref_internal_nolock to do the work.
+ * Only called for local locks.
+ */
 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
 {
         lock_res_and_lock(lock);
@@ -744,10 +838,13 @@ void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
         unlock_res_and_lock(lock);
 }
 
-/* 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 */
+/**
+ * Removes reader/writer reference for LDLM lock \a lock.
+ * Assumes LDLM lock is already locked.
+ * only called in ldlm_flock_destroy and for local locks.
+ * Does NOT add lock to LRU if no r/w references left to accomodate flock locks
+ * that cannot be placed in LRU.
+ */
 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
 {
         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
@@ -766,6 +863,14 @@ void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
         LDLM_LOCK_RELEASE(lock);    /* matches the LDLM_LOCK_GET() in addref */
 }
 
+/**
+ * Removes reader/writer reference for LDLM lock \a lock.
+ * Locks LDLM lock first.
+ * If the lock is determined to be client lock on a client and r/w refcount
+ * drops to zero and the lock is not blocked, the lock is added to LRU lock
+ * on the namespace.
+ * For blocked LDLM locks if r/w count drops to zero, blocking_ast is called.
+ */
 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
 {
         struct ldlm_namespace *ns;
@@ -777,19 +882,19 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
 
         ldlm_lock_decref_internal_nolock(lock, mode);
 
-        if (lock->l_flags & LDLM_FL_LOCAL &&
+       if (ldlm_is_local(lock) &&
             !lock->l_readers && !lock->l_writers) {
                 /* If this is a local lock on a server namespace and this was
                  * the last reference, cancel the lock. */
                 CDEBUG(D_INFO, "forcing cancel of local lock\n");
-                lock->l_flags |= LDLM_FL_CBPENDING;
+               ldlm_set_cbpending(lock);
         }
 
         if (!lock->l_readers && !lock->l_writers &&
-            (lock->l_flags & LDLM_FL_CBPENDING)) {
+           ldlm_is_cbpending(lock)) {
                 /* If we received a blocked AST and this was the last reference,
                  * run the callback. */
-                if (lock->l_ns_srv && lock->l_export)
+               if (ldlm_is_ns_srv(lock) && lock->l_export)
                         CERROR("FL_CBPENDING set on non-local lock--just a "
                                "warning\n");
 
@@ -799,16 +904,16 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
                 ldlm_lock_remove_from_lru(lock);
                 unlock_res_and_lock(lock);
 
-                if (lock->l_flags & LDLM_FL_FAIL_LOC)
+               if (ldlm_is_fail_loc(lock))
                         OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
 
-                if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
+               if (ldlm_is_atomic_cb(lock) ||
                     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)) {
+                  !ldlm_is_no_lru(lock) &&
+                  !ldlm_is_bl_ast(lock)) {
 
                 LDLM_DEBUG(lock, "add lock into lru list");
 
@@ -817,7 +922,7 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
                 ldlm_lock_add_to_lru(lock);
                 unlock_res_and_lock(lock);
 
-                if (lock->l_flags & LDLM_FL_FAIL_LOC)
+               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
@@ -825,7 +930,7 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
                  * enqueue. */
                 if (!exp_connect_cancelset(lock->l_conn_export) &&
                     !ns_connect_lru_resize(ns))
-                        ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
+                       ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
         } else {
                 LDLM_DEBUG(lock, "do not add lock into lru list");
                 unlock_res_and_lock(lock);
@@ -834,6 +939,9 @@ void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
         EXIT;
 }
 
+/**
+ * Decrease reader/writer refcount for LDLM lock with handle \a lockh
+ */
 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
 {
         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
@@ -843,8 +951,13 @@ void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
 }
 EXPORT_SYMBOL(ldlm_lock_decref);
 
-/* This will drop a lock reference and mark it for destruction, but will not
- * necessarily cancel the lock before returning. */
+/**
+ * Decrease reader/writer refcount for LDLM lock with handle
+ * \a lockh and mark it for subsequent cancellation once r/w refcount
+ * drops to zero instead of putting into LRU.
+ *
+ * Typical usage is for GROUP locks which we cannot allow to be cached.
+ */
 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
 {
         struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
@@ -854,7 +967,7 @@ void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
 
         LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
         lock_res_and_lock(lock);
-        lock->l_flags |= LDLM_FL_CBPENDING;
+       ldlm_set_cbpending(lock);
         unlock_res_and_lock(lock);
         ldlm_lock_decref_internal(lock, mode);
         LDLM_LOCK_PUT(lock);
@@ -867,11 +980,11 @@ struct sl_insert_point {
         cfs_list_t *policy_link;
 };
 
-/*
- * search_granted_lock
+/**
+ * Finds a position to insert the new lock into granted lock list.
+ *
+ * Used for locks eligible for skiplist optimization.
  *
- * Description:
- *      Finds a position to insert the new lock.
  * Parameters:
  *      queue [input]:  the granted list where search acts on;
  *      req [input]:    the lock whose position to be located;
@@ -962,6 +1075,10 @@ static void search_granted_lock(cfs_list_t *queue,
         return;
 }
 
+/**
+ * Add a lock into resource granted list after a position described by
+ * \a prev.
+ */
 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
                                        struct sl_insert_point *prev)
 {
@@ -973,7 +1090,7 @@ static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
         ldlm_resource_dump(D_INFO, res);
         LDLM_DEBUG(lock, "About to add lock:");
 
-        if (lock->l_destroyed) {
+       if (ldlm_is_destroyed(lock)) {
                 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
                 return;
         }
@@ -982,13 +1099,24 @@ static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
         LASSERT(cfs_list_empty(&lock->l_sl_mode));
         LASSERT(cfs_list_empty(&lock->l_sl_policy));
 
-        cfs_list_add(&lock->l_res_link, prev->res_link);
-        cfs_list_add(&lock->l_sl_mode, prev->mode_link);
-        cfs_list_add(&lock->l_sl_policy, prev->policy_link);
+       /*
+        * lock->link == prev->link means lock is first starting the group.
+        * Don't re-add to itself to suppress kernel warnings.
+        */
+       if (&lock->l_res_link != prev->res_link)
+               cfs_list_add(&lock->l_res_link, prev->res_link);
+       if (&lock->l_sl_mode != prev->mode_link)
+               cfs_list_add(&lock->l_sl_mode, prev->mode_link);
+       if (&lock->l_sl_policy != prev->policy_link)
+               cfs_list_add(&lock->l_sl_policy, prev->policy_link);
 
         EXIT;
 }
 
+/**
+ * Add a lock to granted list on a resource maintaining skiplist
+ * correctness.
+ */
 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
 {
         struct sl_insert_point prev;
@@ -1001,7 +1129,11 @@ static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
         EXIT;
 }
 
-/* NOTE: called by
+/**
+ * Perform lock granting bookkeeping.
+ *
+ * Includes putting the lock into granted list and updating lock mode.
+ * NOTE: called by
  *  - ldlm_lock_enqueue
  *  - ldlm_reprocess_queue
  *  - ldlm_lock_convert
@@ -1033,13 +1165,17 @@ void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
         EXIT;
 }
 
-/* returns a referenced lock or NULL.  See the flag descriptions below, in the
- * comment above ldlm_lock_match */
+/**
+ * Search for a lock with given properties in a queue.
+ *
+ * \retval a referenced lock or NULL.  See the flag descriptions below, in the
+ * comment above ldlm_lock_match
+ */
 static struct ldlm_lock *search_queue(cfs_list_t *queue,
                                       ldlm_mode_t *mode,
                                       ldlm_policy_data_t *policy,
                                       struct ldlm_lock *old_lock,
-                                      int flags, int unref)
+                                     __u64 flags, int unref)
 {
         struct ldlm_lock *lock;
         cfs_list_t       *tmp;
@@ -1052,16 +1188,21 @@ static struct ldlm_lock *search_queue(cfs_list_t *queue,
                 if (lock == old_lock)
                         break;
 
+               /* Check if this lock can be matched.
+                * Used by LU-2919(exclusive open) for open lease lock */
+               if (ldlm_is_excl(lock))
+                       continue;
+
                 /* llite sometimes wants to match locks that will be
                  * canceled when their users drop, but we allow it to match
                  * if it passes in CBPENDING and the lock still has users.
                  * this is generally only going to be used by children
                  * whose parents already hold a lock so forward progress
                  * can still happen. */
-                if (lock->l_flags & LDLM_FL_CBPENDING &&
+               if (ldlm_is_cbpending(lock) &&
                     !(flags & LDLM_FL_CBPENDING))
                         continue;
-                if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
+               if (!unref && ldlm_is_cbpending(lock) &&
                     lock->l_readers == 0 && lock->l_writers == 0)
                         continue;
 
@@ -1088,13 +1229,11 @@ static struct ldlm_lock *search_queue(cfs_list_t *queue,
                       policy->l_inodebits.bits))
                         continue;
 
-                if (!unref &&
-                    (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
-                     lock->l_failed))
+               if (!unref && LDLM_HAVE_MASK(lock, GONE))
                         continue;
 
                 if ((flags & LDLM_FL_LOCAL_ONLY) &&
-                    !(lock->l_flags & LDLM_FL_LOCAL))
+                   !ldlm_is_local(lock))
                         continue;
 
                 if (flags & LDLM_FL_TEST_LOCK) {
@@ -1112,10 +1251,10 @@ static struct ldlm_lock *search_queue(cfs_list_t *queue,
 
 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
 {
-        if (!lock->l_failed) {
-                lock->l_failed = 1;
-                cfs_waitq_broadcast(&lock->l_waitq);
-        }
+       if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
+               lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
+               wake_up_all(&lock->l_waitq);
+       }
 }
 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
 
@@ -1127,13 +1266,24 @@ void ldlm_lock_fail_match(struct ldlm_lock *lock)
 }
 EXPORT_SYMBOL(ldlm_lock_fail_match);
 
+/**
+ * Mark lock as "matchable" by OST.
+ *
+ * Used to prevent certain races in LOV/OSC where the lock is granted, but LVB
+ * is not yet valid.
+ * Assumes LDLM lock is already locked.
+ */
 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
 {
-        lock->l_flags |= LDLM_FL_LVB_READY;
-        cfs_waitq_broadcast(&lock->l_waitq);
+       ldlm_set_lvb_ready(lock);
+       wake_up_all(&lock->l_waitq);
 }
 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
 
+/**
+ * Mark lock as "matchable" by OST.
+ * Locks the lock and then \see ldlm_lock_allow_match_locked
+ */
 void ldlm_lock_allow_match(struct ldlm_lock *lock)
 {
         lock_res_and_lock(lock);
@@ -1142,7 +1292,13 @@ void ldlm_lock_allow_match(struct ldlm_lock *lock)
 }
 EXPORT_SYMBOL(ldlm_lock_allow_match);
 
-/* Can be called in two ways:
+/**
+ * Attempt to find a lock with specified properties.
+ *
+ * Typically returns a reference to matched lock unless LDLM_FL_TEST_LOCK is
+ * set in \a flags
+ *
+ * Can be called in two ways:
  *
  * If 'ns' is NULL, then lockh describes an existing lock that we want to look
  * for a duplicate of.
@@ -1159,14 +1315,14 @@ EXPORT_SYMBOL(ldlm_lock_allow_match);
  * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
  *     just tell us if we would have matched.
  *
- * Returns 1 if it finds an already-existing lock that is compatible; in this
+ * \retval 1 if it finds an already-existing lock that is compatible; in this
  * case, lockh is filled in with a addref()ed lock
  *
- * we also check security context, if that failed we simply return 0 (to keep
- * caller code unchanged), the context failure will be discovered by caller
- * sometime later.
+ * We also check security context, and if that fails we simply return 0 (to
+ * keep caller code unchanged), the context failure will be discovered by
+ * caller sometime later.
  */
-ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
+ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, __u64 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, int unref)
@@ -1219,7 +1375,9 @@ ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
         if (lock) {
                 ldlm_lock2handle(lock, lockh);
                 if ((flags & LDLM_FL_LVB_READY) &&
-                    (!(lock->l_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,
@@ -1239,12 +1397,11 @@ ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
                         lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
                                                NULL, LWI_ON_SIGNAL_NOOP, NULL);
 
-                        /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
-                        l_wait_event(lock->l_waitq,
-                                     lock->l_flags & LDLM_FL_LVB_READY ||
-                                     lock->l_failed,
-                                     &lwi);
-                        if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
+                       /* 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
@@ -1299,11 +1456,10 @@ ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
         lock = ldlm_handle2lock(lockh);
         if (lock != NULL) {
                 lock_res_and_lock(lock);
-                if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
-                    lock->l_failed)
+               if (LDLM_HAVE_MASK(lock, GONE))
                         GOTO(out, mode);
 
-                if (lock->l_flags & LDLM_FL_CBPENDING &&
+               if (ldlm_is_cbpending(lock) &&
                     lock->l_readers == 0 && lock->l_writers == 0)
                         GOTO(out, mode);
 
@@ -1324,13 +1480,116 @@ out:
 }
 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
 
-/* Returns a referenced lock */
+/** The caller must guarantee that the buffer is large enough. */
+int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
+                 enum req_location loc, void *data, int size)
+{
+       void *lvb;
+       ENTRY;
+
+       LASSERT(data != NULL);
+       LASSERT(size >= 0);
+
+       switch (lock->l_lvb_type) {
+       case LVB_T_OST:
+               if (size == sizeof(struct ost_lvb)) {
+                       if (loc == RCL_CLIENT)
+                               lvb = req_capsule_client_swab_get(pill,
+                                               &RMF_DLM_LVB,
+                                               lustre_swab_ost_lvb);
+                       else
+                               lvb = req_capsule_server_swab_get(pill,
+                                               &RMF_DLM_LVB,
+                                               lustre_swab_ost_lvb);
+                       if (unlikely(lvb == NULL)) {
+                               LDLM_ERROR(lock, "no LVB");
+                               RETURN(-EPROTO);
+                       }
+
+                       memcpy(data, lvb, size);
+               } else if (size == sizeof(struct ost_lvb_v1)) {
+                       struct ost_lvb *olvb = data;
+
+                       if (loc == RCL_CLIENT)
+                               lvb = req_capsule_client_swab_get(pill,
+                                               &RMF_DLM_LVB,
+                                               lustre_swab_ost_lvb_v1);
+                       else
+                               lvb = req_capsule_server_sized_swab_get(pill,
+                                               &RMF_DLM_LVB, size,
+                                               lustre_swab_ost_lvb_v1);
+                       if (unlikely(lvb == NULL)) {
+                               LDLM_ERROR(lock, "no LVB");
+                               RETURN(-EPROTO);
+                       }
+
+                       memcpy(data, lvb, size);
+                       olvb->lvb_mtime_ns = 0;
+                       olvb->lvb_atime_ns = 0;
+                       olvb->lvb_ctime_ns = 0;
+               } else {
+                       LDLM_ERROR(lock, "Replied unexpected ost LVB size %d",
+                                  size);
+                       RETURN(-EINVAL);
+               }
+               break;
+       case LVB_T_LQUOTA:
+               if (size == sizeof(struct lquota_lvb)) {
+                       if (loc == RCL_CLIENT)
+                               lvb = req_capsule_client_swab_get(pill,
+                                               &RMF_DLM_LVB,
+                                               lustre_swab_lquota_lvb);
+                       else
+                               lvb = req_capsule_server_swab_get(pill,
+                                               &RMF_DLM_LVB,
+                                               lustre_swab_lquota_lvb);
+                       if (unlikely(lvb == NULL)) {
+                               LDLM_ERROR(lock, "no LVB");
+                               RETURN(-EPROTO);
+                       }
+
+                       memcpy(data, lvb, size);
+               } else {
+                       LDLM_ERROR(lock, "Replied unexpected lquota LVB size %d",
+                                  size);
+                       RETURN(-EINVAL);
+               }
+               break;
+       case LVB_T_LAYOUT:
+               if (size == 0)
+                       break;
+
+               if (loc == RCL_CLIENT)
+                       lvb = req_capsule_client_get(pill, &RMF_DLM_LVB);
+               else
+                       lvb = req_capsule_server_get(pill, &RMF_DLM_LVB);
+               if (unlikely(lvb == NULL)) {
+                       LDLM_ERROR(lock, "no LVB");
+                       RETURN(-EPROTO);
+               }
+
+               memcpy(data, lvb, size);
+               break;
+       default:
+               LDLM_ERROR(lock, "Unknown LVB type: %d\n", lock->l_lvb_type);
+               libcfs_debug_dumpstack(NULL);
+               RETURN(-EINVAL);
+       }
+
+       RETURN(0);
+}
+
+/**
+ * Create and fill in new LDLM lock with specified properties.
+ * Returns a referenced lock
+ */
 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,
                                    const struct ldlm_callback_suite *cbs,
-                                   void *data, __u32 lvb_len)
+                                  void *data, __u32 lvb_len,
+                                  enum lvb_type lvb_type)
 {
         struct ldlm_lock *lock;
         struct ldlm_resource *res;
@@ -1345,15 +1604,15 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
         if (lock == NULL)
                 RETURN(NULL);
 
-        lock->l_req_mode = mode;
-        lock->l_ast_data = data;
-        lock->l_pid = cfs_curproc_pid();
-        lock->l_ns_srv = !!ns_is_server(ns);
+       lock->l_req_mode = mode;
+       lock->l_ast_data = data;
+       lock->l_pid = current_pid();
+       if (ns_is_server(ns))
+               ldlm_set_ns_srv(lock);
         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;
@@ -1365,11 +1624,12 @@ struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
 
         if (lvb_len) {
                 lock->l_lvb_len = lvb_len;
-                OBD_ALLOC(lock->l_lvb_data, lvb_len);
+                OBD_ALLOC_LARGE(lock->l_lvb_data, lvb_len);
                 if (lock->l_lvb_data == NULL)
                         GOTO(out, 0);
         }
 
+       lock->l_lvb_type = lvb_type;
         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
                 GOTO(out, 0);
 
@@ -1381,9 +1641,19 @@ out:
         return NULL;
 }
 
+/**
+ * Enqueue (request) a lock.
+ *
+ * Does not block. As a result of enqueue the lock would be put
+ * into granted or waiting list.
+ *
+ * If namespace has intent policy sent and the lock has LDLM_FL_HAS_INTENT flag
+ * set, skip all the enqueueing and delegate lock processing to intent policy
+ * function.
+ */
 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
                                struct ldlm_lock **lockp,
-                               void *cookie, int *flags)
+                              void *cookie, __u64 *flags)
 {
         struct ldlm_lock *lock = *lockp;
         struct ldlm_resource *res = lock->l_resource;
@@ -1424,7 +1694,7 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
          * 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)
-                OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
+               OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, __GFP_IO);
 
         lock_res_and_lock(lock);
         if (local && lock->l_req_mode == lock->l_granted_mode) {
@@ -1433,7 +1703,7 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
                  * need to do anything else. */
                 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
                             LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
-                GOTO(out, ELDLM_OK);
+               GOTO(out, rc = ELDLM_OK);
         }
 
         ldlm_resource_unlink_lock(lock);
@@ -1448,21 +1718,22 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
                 node = NULL;
         }
 
-        /* Some flags from the enqueue want to make it into the AST, via the
-         * lock's l_flags. */
-        lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
-
-        /* This distinction between local lock trees is very important; a client
-         * namespace only has information about locks taken by that client, and
-         * thus doesn't have enough information to decide for itself if it can
-         * be granted (below).  In this case, we do exactly what the server
-         * tells us to do, as dictated by the 'flags'.
-         *
-         * We do exactly the same thing during recovery, when the server is
-         * more or less trusting the clients not to lie.
-         *
-         * FIXME (bug 268): Detect obvious lies by checking compatibility in
-         * granted/converting queues. */
+       /* Some flags from the enqueue want to make it into the AST, via the
+        * lock's l_flags. */
+       if (*flags & LDLM_FL_AST_DISCARD_DATA)
+               ldlm_set_ast_discard_data(lock);
+
+       /* This distinction between local lock trees is very important; a client
+        * namespace only has information about locks taken by that client, and
+        * thus doesn't have enough information to decide for itself if it can
+        * be granted (below).  In this case, we do exactly what the server
+        * tells us to do, as dictated by the 'flags'.
+        *
+        * We do exactly the same thing during recovery, when the server is
+        * more or less trusting the clients not to lie.
+        *
+        * FIXME (bug 268): Detect obvious lies by checking compatibility in
+        * granted/converting queues. */
         if (local) {
                 if (*flags & LDLM_FL_BLOCK_CONV)
                         ldlm_resource_add_lock(res, &res->lr_converting, lock);
@@ -1470,18 +1741,18 @@ ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
                 else
                         ldlm_grant_lock(lock, NULL);
-                GOTO(out, ELDLM_OK);
+               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, ELDLM_OK);
+                       GOTO(out, rc = ELDLM_OK);
                 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
-                        GOTO(out, ELDLM_OK);
+                       GOTO(out, rc = ELDLM_OK);
                 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
                         ldlm_grant_lock(lock, NULL);
-                        GOTO(out, ELDLM_OK);
+                       GOTO(out, rc = ELDLM_OK);
                 }
                 /* If no flags, fall through to normal enqueue path. */
         }
@@ -1505,13 +1776,18 @@ out:
 }
 
 #ifdef HAVE_SERVER_SUPPORT
-/* Must be called with namespace taken: queue is waiting or converting. */
+/**
+ * Iterate through all waiting locks on a given resource queue and attempt to
+ * grant them.
+ *
+ * Must be called with resource lock held.
+ */
 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
                          cfs_list_t *work_list)
 {
         cfs_list_t *tmp, *pos;
         ldlm_processing_policy policy;
-        int flags;
+       __u64 flags;
         int rc = LDLM_ITER_CONTINUE;
         ldlm_error_t err;
         ENTRY;
@@ -1537,6 +1813,9 @@ int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
 }
 #endif
 
+/**
+ * Process a call to blocking AST callback for a lock in ast_work list
+ */
 static int
 ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
 {
@@ -1555,7 +1834,7 @@ ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        lock_res_and_lock(lock);
        cfs_list_del_init(&lock->l_bl_ast);
 
-       LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
+       LASSERT(ldlm_is_ast_sent(lock));
        LASSERT(lock->l_bl_ast_run == 0);
        LASSERT(lock->l_blocking_lock);
        lock->l_bl_ast_run++;
@@ -1571,6 +1850,9 @@ ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        RETURN(rc);
 }
 
+/**
+ * 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)
 {
@@ -1599,11 +1881,11 @@ ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        /* nobody should touch l_cp_ast */
        lock_res_and_lock(lock);
        cfs_list_del_init(&lock->l_cp_ast);
-       LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
+       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;
-       lock->l_flags &= ~LDLM_FL_CP_REQD;
+       ldlm_clear_cp_reqd(lock);
        unlock_res_and_lock(lock);
 
        if (completion_callback != NULL)
@@ -1613,6 +1895,9 @@ ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        RETURN(rc);
 }
 
+/**
+ * Process a call to revocation AST callback for a lock in ast_work list
+ */
 static int
 ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
 {
@@ -1639,6 +1924,9 @@ ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        RETURN(rc);
 }
 
+/**
+ * Process a call to glimpse AST callback for a lock in ast_work list
+ */
 int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
 {
        struct ldlm_cb_set_arg          *arg = opaq;
@@ -1671,6 +1959,12 @@ int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
        RETURN(rc);
 }
 
+/**
+ * Process list of locks in need of ASTs being sent.
+ *
+ * Used on server to send multiple ASTs together instead of sending one by
+ * one.
+ */
 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
                       ldlm_desc_ast_t ast_type)
 {
@@ -1685,7 +1979,7 @@ int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
        if (arg == NULL)
                RETURN(-ENOMEM);
 
-       cfs_atomic_set(&arg->restart, 0);
+       atomic_set(&arg->restart, 0);
        arg->list = rpc_list;
 
        switch (ast_type) {
@@ -1721,7 +2015,7 @@ int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
        ptlrpc_set_wait(arg->set);
        ptlrpc_set_destroy(arg->set);
 
-       rc = cfs_atomic_read(&arg->restart) ? -ERESTART : 0;
+       rc = atomic_read(&arg->restart) ? -ERESTART : 0;
        GOTO(out, rc);
 out:
        OBD_FREE_PTR(arg);
@@ -1745,6 +2039,10 @@ static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
         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;
@@ -1757,6 +2055,14 @@ void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
 }
 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
 
+/**
+ * Try to grant all waiting locks on a resource.
+ *
+ * Calls ldlm_reprocess_queue on converting and waiting queues.
+ *
+ * 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)
 {
         CFS_LIST_HEAD(rpc_list);
@@ -1793,14 +2099,18 @@ restart:
 #endif
         EXIT;
 }
+EXPORT_SYMBOL(ldlm_reprocess_all);
 
+/**
+ * Helper function to call blocking AST for LDLM lock \a lock in a
+ * "cancelling" mode.
+ */
 void ldlm_cancel_callback(struct ldlm_lock *lock)
 {
-        check_res_locked(lock->l_resource);
-        if (!(lock->l_flags & LDLM_FL_CANCEL)) {
-                lock->l_flags |= LDLM_FL_CANCEL;
-                if (lock->l_blocking_ast) {
-                        // l_check_no_ns_lock(ns);
+       check_res_locked(lock->l_resource);
+       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);
@@ -1809,9 +2119,12 @@ void ldlm_cancel_callback(struct ldlm_lock *lock)
                         LDLM_DEBUG(lock, "no blocking ast");
                 }
         }
-        lock->l_flags |= LDLM_FL_BL_DONE;
+       ldlm_set_bl_done(lock);
 }
 
+/**
+ * Remove skiplist-enabled LDLM lock \a req from granted list
+ */
 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
 {
         if (req->l_resource->lr_type != LDLM_PLAIN &&
@@ -1822,6 +2135,9 @@ void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
         cfs_list_del_init(&req->l_sl_mode);
 }
 
+/**
+ * Attempts to cancel LDLM lock \a lock that has no reader/writer references.
+ */
 void ldlm_lock_cancel(struct ldlm_lock *lock)
 {
         struct ldlm_resource *res;
@@ -1840,15 +2156,15 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
                 LBUG();
         }
 
-       if (lock->l_waited)
+       if (ldlm_is_waited(lock))
                ldlm_del_waiting_lock(lock);
 
         /* Releases cancel callback. */
         ldlm_cancel_callback(lock);
 
-        /* Yes, second time, just in case it was added again while we were
-           running with no res lock in ldlm_cancel_callback */
-       if (lock->l_waited)
+       /* Yes, second time, just in case it was added again while we were
+        * running with no res lock in ldlm_cancel_callback */
+       if (ldlm_is_waited(lock))
                ldlm_del_waiting_lock(lock);
 
         ldlm_resource_unlink_lock(lock);
@@ -1866,6 +2182,9 @@ void ldlm_lock_cancel(struct ldlm_lock *lock)
 }
 EXPORT_SYMBOL(ldlm_lock_cancel);
 
+/**
+ * Set opaque data into the lock that only makes sense to upper layer.
+ */
 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
 {
         struct ldlm_lock *lock = ldlm_handle2lock(lockh);
@@ -1888,6 +2207,10 @@ struct export_cl_data {
        int                     ecl_loop;
 };
 
+/**
+ * Iterator function for ldlm_cancel_locks_for_export.
+ * Cancels passed locks.
+ */
 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
                                     cfs_hlist_node_t *hnode, void *data)
 
@@ -1913,12 +2236,17 @@ int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
                       "Cancel lock %p for export %p (loop %d), still have "
                       "%d locks left on hash table.\n",
                       lock, exp, ecl->ecl_loop,
-                      cfs_atomic_read(&hs->hs_count));
+                      atomic_read(&hs->hs_count));
        }
 
        return 0;
 }
 
+/**
+ * Cancel all locks for given export.
+ *
+ * Typically called on client disconnection/eviction
+ */
 void ldlm_cancel_locks_for_export(struct obd_export *exp)
 {
        struct export_cl_data   ecl = {
@@ -1935,6 +2263,7 @@ void ldlm_cancel_locks_for_export(struct obd_export *exp)
  *
  * A fast variant of ldlm_lock_convert for convertion of exclusive
  * locks. The convertion is always successful.
+ * Used by Commit on Sharing (COS) code.
  *
  * \param lock A lock to convert
  * \param new_mode new lock mode
@@ -1963,6 +2292,13 @@ void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
 }
 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, int new_mode,
                                         __u32 *flags)
 {
@@ -1977,15 +2313,16 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         struct ldlm_interval *node;
         ENTRY;
 
-        if (new_mode == lock->l_granted_mode) { // No changes? Just return.
+       /* Just return if mode is unchanged. */
+       if (new_mode == lock->l_granted_mode) {
                 *flags |= LDLM_FL_BLOCK_GRANTED;
                 RETURN(lock->l_resource);
         }
 
         /* 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, CFS_ALLOC_IO);
-        if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
+       OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, __GFP_IO);
+       if (node == NULL)  /* Actually, this causes EDEADLOCK to be returned */
                 RETURN(NULL);
 
         LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
@@ -2035,7 +2372,7 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
                 } else {
                         /* This should never happen, because of the way the
                          * server handles conversions. */
-                        LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
+                       LDLM_ERROR(lock, "Erroneous flags %x on local lock\n",
                                    *flags);
                         LBUG();
 
@@ -2049,7 +2386,7 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
         } else {
                 int rc;
                 ldlm_error_t err;
-                int pflags = 0;
+               __u64 pflags = 0;
                 ldlm_processing_policy policy;
                 policy = ldlm_processing_policy_table[res->lr_type];
                 rc = policy(lock, &pflags, 0, &err, &rpc_list);
@@ -2083,6 +2420,11 @@ struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
 }
 EXPORT_SYMBOL(ldlm_lock_convert);
 
+/**
+ * Print lock with lock handle \a lockh description into debug log.
+ *
+ * Used when printing all locks on a resource for debug purposes.
+ */
 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
 {
         struct ldlm_lock *lock;
@@ -2100,6 +2442,10 @@ void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
 }
 EXPORT_SYMBOL(ldlm_lock_dump_handle);
 
+/**
+ * Print lock information with custom message into debug log.
+ * Helper function.
+ */
 void _ldlm_lock_debug(struct ldlm_lock *lock,
                       struct libcfs_debug_msg_data *msgdata,
                       const char *fmt, ...)
@@ -2122,109 +2468,109 @@ void _ldlm_lock_debug(struct ldlm_lock *lock,
                 libcfs_debug_vmsg2(msgdata, fmt, args,
                        " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
                        "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" nid: %s "
-                       "remote: "LPX64" expref: %d pid: %u timeout: %lu\n",
+                       "remote: "LPX64" expref: %d pid: %u timeout: %lu "
+                      "lvb_type: %d\n",
                        lock,
-                       lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
+                      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 ? cfs_atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
+                      exp ? atomic_read(&exp->exp_refcount) : -99,
+                       lock->l_pid, lock->l_callback_timeout, 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/"LPX64" lrc: %d/%d,%d mode: %s/%s "
-                       "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
-                       "] (req "LPU64"->"LPU64") flags: "LPX64" nid: %s remote:"
-                       " "LPX64" expref: %d pid: %u timeout %lu\n",
-                       ldlm_lock_to_ns_name(lock), lock,
-                       lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
-                       lock->l_readers, lock->l_writers,
-                       ldlm_lockname[lock->l_granted_mode],
-                       ldlm_lockname[lock->l_req_mode],
-                       resource->lr_name.name[0],
-                       resource->lr_name.name[1],
-                       cfs_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 ? cfs_atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
-                break;
-
-        case LDLM_FLOCK:
-                libcfs_debug_vmsg2(msgdata, 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: "LPX64" nid: %s remote: "LPX64
-                       " expref: %d pid: %u timeout: %lu\n",
-                       ldlm_lock_to_ns_name(lock), lock,
-                       lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
-                       lock->l_readers, lock->l_writers,
-                       ldlm_lockname[lock->l_granted_mode],
-                       ldlm_lockname[lock->l_req_mode],
-                       resource->lr_name.name[0],
-                       resource->lr_name.name[1],
-                       cfs_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 ? cfs_atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
-                break;
-
-        case LDLM_IBITS:
-                libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
-                       "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
-                       "flags: "LPX64" nid: %s remote: "LPX64" expref: %d "
-                       "pid: %u timeout: %lu\n",
-                       ldlm_lock_to_ns_name(lock),
-                       lock, lock->l_handle.h_cookie,
-                       cfs_atomic_read (&lock->l_refc),
-                       lock->l_readers, lock->l_writers,
-                       ldlm_lockname[lock->l_granted_mode],
-                       ldlm_lockname[lock->l_req_mode],
-                       resource->lr_name.name[0],
-                       resource->lr_name.name[1],
-                       lock->l_policy_data.l_inodebits.bits,
-                       cfs_atomic_read(&resource->lr_refcount),
-                       ldlm_typename[resource->lr_type],
-                       lock->l_flags, nid, lock->l_remote_handle.cookie,
-                       exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
-                break;
-
-        default:
-                libcfs_debug_vmsg2(msgdata, fmt, args,
-                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
-                       "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
-                       "nid: %s remote: "LPX64" expref: %d pid: %u timeout %lu"
-                       "\n",
-                       ldlm_lock_to_ns_name(lock),
-                       lock, lock->l_handle.h_cookie,
-                       cfs_atomic_read (&lock->l_refc),
-                       lock->l_readers, lock->l_writers,
-                       ldlm_lockname[lock->l_granted_mode],
-                       ldlm_lockname[lock->l_req_mode],
-                       resource->lr_name.name[0],
-                       resource->lr_name.name[1],
-                       cfs_atomic_read(&resource->lr_refcount),
-                       ldlm_typename[resource->lr_type],
-                       lock->l_flags, nid, lock->l_remote_handle.cookie,
-                       exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
-                       lock->l_pid, lock->l_callback_timeout);
-                break;
-        }
-        va_end(args);
+       switch (resource->lr_type) {
+       case LDLM_EXTENT:
+               libcfs_debug_vmsg2(msgdata, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "DLDLMRES" rrc: %d type: %s ["LPU64"->"LPU64"] "
+                       "(req "LPU64"->"LPU64") flags: "LPX64" nid: %s remote: "
+                       LPX64" 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);
+               break;
+
+       case LDLM_FLOCK:
+               libcfs_debug_vmsg2(msgdata, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "DLDLMRES" rrc: %d type: %s pid: %d "
+                       "["LPU64"->"LPU64"] flags: "LPX64" nid: %s "
+                       "remote: "LPX64" 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);
+               break;
+
+       case LDLM_IBITS:
+               libcfs_debug_vmsg2(msgdata, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "DLDLMRES" bits "LPX64" rrc: %d type: %s "
+                       "flags: "LPX64" nid: %s remote: "LPX64" 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);
+               break;
+
+       default:
+               libcfs_debug_vmsg2(msgdata, fmt, args,
+                       " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
+                       "res: "DLDLMRES" rrc: %d type: %s flags: "LPX64" "
+                       "nid: %s remote: "LPX64" 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);
+               break;
+       }
+       va_end(args);
 }
 EXPORT_SYMBOL(_ldlm_lock_debug);