[DLM] interface for purge (2/2)
[safe/jmp/linux-2.6] / fs / dlm / lock.c
index 81efb36..9d26b3a 100644 (file)
@@ -1,7 +1,7 @@
 /******************************************************************************
 *******************************************************************************
 **
-**  Copyright (C) 2005 Red Hat, Inc.  All rights reserved.
+**  Copyright (C) 2005-2007 Red Hat, Inc.  All rights reserved.
 **
 **  This copyrighted material is made available to anyone wishing to use,
 **  modify, copy, or redistribute it subject to the terms and conditions
@@ -55,8 +55,9 @@
                                    R: do_xxxx()
    L: receive_xxxx_reply()     <-  R: send_xxxx_reply()
 */
-
+#include <linux/types.h>
 #include "dlm_internal.h"
+#include <linux/dlm_device.h>
 #include "memory.h"
 #include "lowcomms.h"
 #include "requestqueue.h"
@@ -69,6 +70,7 @@
 #include "rcom.h"
 #include "recover.h"
 #include "lvb_table.h"
+#include "user.h"
 #include "config.h"
 
 static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
@@ -83,6 +85,7 @@ static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
                                    struct dlm_message *ms);
 static int receive_extralen(struct dlm_message *ms);
+static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
 
 /*
  * Lock compatibilty matrix - thanks Steve
@@ -124,7 +127,6 @@ const int dlm_lvb_operations[8][8] = {
         {  -1,  0,  0,  0,  0,  0,  0,  0 }, /* EX */
         {  -1,  0,  0,  0,  0,  0,  0,  0 }  /* PD */
 };
-EXPORT_SYMBOL_GPL(dlm_lvb_operations);
 
 #define modes_compat(gr, rq) \
        __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
@@ -152,7 +154,7 @@ static const int __quecvt_compat_matrix[8][8] = {
         {0, 0, 0, 0, 0, 0, 0, 0}        /* PD */
 };
 
-static void dlm_print_lkb(struct dlm_lkb *lkb)
+void dlm_print_lkb(struct dlm_lkb *lkb)
 {
        printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
               "     status %d rqmode %d grmode %d wait_type %d ast_type %d\n",
@@ -168,6 +170,28 @@ void dlm_print_rsb(struct dlm_rsb *r)
               r->res_recover_locks_count, r->res_name);
 }
 
+void dlm_dump_rsb(struct dlm_rsb *r)
+{
+       struct dlm_lkb *lkb;
+
+       dlm_print_rsb(r);
+
+       printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
+              list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
+       printk(KERN_ERR "rsb lookup list\n");
+       list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
+               dlm_print_lkb(lkb);
+       printk(KERN_ERR "rsb grant queue:\n");
+       list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
+               dlm_print_lkb(lkb);
+       printk(KERN_ERR "rsb convert queue:\n");
+       list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
+               dlm_print_lkb(lkb);
+       printk(KERN_ERR "rsb wait queue:\n");
+       list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
+               dlm_print_lkb(lkb);
+}
+
 /* Threads cannot use the lockspace while it's being recovered */
 
 static inline void lock_recovery(struct dlm_ls *ls)
@@ -215,15 +239,15 @@ static inline int is_master_copy(struct dlm_lkb *lkb)
 {
        if (lkb->lkb_flags & DLM_IFL_MSTCPY)
                DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
-       return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? TRUE : FALSE;
+       return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
 }
 
 static inline int middle_conversion(struct dlm_lkb *lkb)
 {
        if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
            (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
-               return TRUE;
-       return FALSE;
+               return 1;
+       return 0;
 }
 
 static inline int down_conversion(struct dlm_lkb *lkb)
@@ -231,6 +255,22 @@ static inline int down_conversion(struct dlm_lkb *lkb)
        return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
 }
 
+static inline int is_overlap_unlock(struct dlm_lkb *lkb)
+{
+       return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
+}
+
+static inline int is_overlap_cancel(struct dlm_lkb *lkb)
+{
+       return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
+}
+
+static inline int is_overlap(struct dlm_lkb *lkb)
+{
+       return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
+                                 DLM_IFL_OVERLAP_CANCEL));
+}
+
 static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
 {
        if (is_master_copy(lkb))
@@ -244,6 +284,12 @@ static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
        dlm_add_ast(lkb, AST_COMP);
 }
 
+static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
+{
+       queue_cast(r, lkb,
+                  is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
+}
+
 static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
 {
        if (is_master_copy(lkb))
@@ -269,7 +315,7 @@ static struct dlm_rsb *create_rsb(struct dlm_ls *ls, char *name, int len)
        r->res_ls = ls;
        r->res_length = len;
        memcpy(r->res_name, name, len);
-       init_MUTEX(&r->res_sem);
+       mutex_init(&r->res_mutex);
 
        INIT_LIST_HEAD(&r->res_lookup);
        INIT_LIST_HEAD(&r->res_grantqueue);
@@ -291,7 +337,7 @@ static int search_rsb_list(struct list_head *head, char *name, int len,
                if (len == r->res_length && !memcmp(name, r->res_name, len))
                        goto found;
        }
-       return -ENOENT;
+       return -EBADR;
 
  found:
        if (r->res_nodeid && (flags & R_MASTER))
@@ -376,7 +422,7 @@ static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
        if (!error)
                goto out;
 
-       if (error == -ENOENT && !(flags & R_CREATE))
+       if (error == -EBADR && !(flags & R_CREATE))
                goto out;
 
        /* the rsb was found but wasn't a master copy */
@@ -475,7 +521,7 @@ static void unhold_rsb(struct dlm_rsb *r)
 {
        int rv;
        rv = kref_put(&r->res_ref, toss_rsb);
-       DLM_ASSERT(!rv, dlm_print_rsb(r););
+       DLM_ASSERT(!rv, dlm_dump_rsb(r););
 }
 
 static void kill_rsb(struct kref *kref)
@@ -485,12 +531,12 @@ static void kill_rsb(struct kref *kref)
        /* All work is done after the return from kref_put() so we
           can release the write_lock before the remove and free. */
 
-       DLM_ASSERT(list_empty(&r->res_lookup),);
-       DLM_ASSERT(list_empty(&r->res_grantqueue),);
-       DLM_ASSERT(list_empty(&r->res_convertqueue),);
-       DLM_ASSERT(list_empty(&r->res_waitqueue),);
-       DLM_ASSERT(list_empty(&r->res_root_list),);
-       DLM_ASSERT(list_empty(&r->res_recover_list),);
+       DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
+       DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
+       DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
+       DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
+       DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
+       DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
 }
 
 /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
@@ -523,6 +569,8 @@ static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
        lkb->lkb_nodeid = -1;
        lkb->lkb_grmode = DLM_LOCK_IV;
        kref_init(&lkb->lkb_ref);
+       INIT_LIST_HEAD(&lkb->lkb_ownqueue);
+       INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
 
        get_random_bytes(&bucket, sizeof(bucket));
        bucket &= (ls->ls_lkbtbl_size - 1);
@@ -591,9 +639,11 @@ static void kill_lkb(struct kref *kref)
        DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
 }
 
-static int put_lkb(struct dlm_lkb *lkb)
+/* __put_lkb() is used when an lkb may not have an rsb attached to
+   it so we need to provide the lockspace explicitly */
+
+static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
 {
-       struct dlm_ls *ls = lkb->lkb_resource->res_ls;
        uint16_t bucket = lkb->lkb_id & 0xFFFF;
 
        write_lock(&ls->ls_lkbtbl[bucket].lock);
@@ -606,8 +656,6 @@ static int put_lkb(struct dlm_lkb *lkb)
                /* for local/process lkbs, lvbptr points to caller's lksb */
                if (lkb->lkb_lvbptr && is_master_copy(lkb))
                        free_lvb(lkb->lkb_lvbptr);
-               if (lkb->lkb_range)
-                       free_range(lkb->lkb_range);
                free_lkb(lkb);
                return 1;
        } else {
@@ -618,7 +666,13 @@ static int put_lkb(struct dlm_lkb *lkb)
 
 int dlm_put_lkb(struct dlm_lkb *lkb)
 {
-       return put_lkb(lkb);
+       struct dlm_ls *ls;
+
+       DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
+       DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
+
+       ls = lkb->lkb_resource->res_ls;
+       return __put_lkb(ls, lkb);
 }
 
 /* This is only called to add a reference when the code already holds
@@ -705,49 +759,158 @@ static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
        unhold_lkb(lkb);
 }
 
+static int msg_reply_type(int mstype)
+{
+       switch (mstype) {
+       case DLM_MSG_REQUEST:
+               return DLM_MSG_REQUEST_REPLY;
+       case DLM_MSG_CONVERT:
+               return DLM_MSG_CONVERT_REPLY;
+       case DLM_MSG_UNLOCK:
+               return DLM_MSG_UNLOCK_REPLY;
+       case DLM_MSG_CANCEL:
+               return DLM_MSG_CANCEL_REPLY;
+       case DLM_MSG_LOOKUP:
+               return DLM_MSG_LOOKUP_REPLY;
+       }
+       return -1;
+}
+
 /* add/remove lkb from global waiters list of lkb's waiting for
    a reply from a remote node */
 
-static void add_to_waiters(struct dlm_lkb *lkb, int mstype)
+static int add_to_waiters(struct dlm_lkb *lkb, int mstype)
 {
        struct dlm_ls *ls = lkb->lkb_resource->res_ls;
+       int error = 0;
 
-       down(&ls->ls_waiters_sem);
-       if (lkb->lkb_wait_type) {
-               log_print("add_to_waiters error %d", lkb->lkb_wait_type);
+       mutex_lock(&ls->ls_waiters_mutex);
+
+       if (is_overlap_unlock(lkb) ||
+           (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
+               error = -EINVAL;
+               goto out;
+       }
+
+       if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
+               switch (mstype) {
+               case DLM_MSG_UNLOCK:
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
+                       break;
+               case DLM_MSG_CANCEL:
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
+                       break;
+               default:
+                       error = -EBUSY;
+                       goto out;
+               }
+               lkb->lkb_wait_count++;
+               hold_lkb(lkb);
+
+               log_debug(ls, "add overlap %x cur %d new %d count %d flags %x",
+                         lkb->lkb_id, lkb->lkb_wait_type, mstype,
+                         lkb->lkb_wait_count, lkb->lkb_flags);
                goto out;
        }
+
+       DLM_ASSERT(!lkb->lkb_wait_count,
+                  dlm_print_lkb(lkb);
+                  printk("wait_count %d\n", lkb->lkb_wait_count););
+
+       lkb->lkb_wait_count++;
        lkb->lkb_wait_type = mstype;
-       kref_get(&lkb->lkb_ref);
+       hold_lkb(lkb);
        list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
  out:
-       up(&ls->ls_waiters_sem);
+       if (error)
+               log_error(ls, "add_to_waiters %x error %d flags %x %d %d %s",
+                         lkb->lkb_id, error, lkb->lkb_flags, mstype,
+                         lkb->lkb_wait_type, lkb->lkb_resource->res_name);
+       mutex_unlock(&ls->ls_waiters_mutex);
+       return error;
 }
 
-static int _remove_from_waiters(struct dlm_lkb *lkb)
+/* We clear the RESEND flag because we might be taking an lkb off the waiters
+   list as part of process_requestqueue (e.g. a lookup that has an optimized
+   request reply on the requestqueue) between dlm_recover_waiters_pre() which
+   set RESEND and dlm_recover_waiters_post() */
+
+static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype)
 {
-       int error = 0;
+       struct dlm_ls *ls = lkb->lkb_resource->res_ls;
+       int overlap_done = 0;
 
-       if (!lkb->lkb_wait_type) {
-               log_print("remove_from_waiters error");
-               error = -EINVAL;
-               goto out;
+       if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
+               overlap_done = 1;
+               goto out_del;
        }
-       lkb->lkb_wait_type = 0;
-       list_del(&lkb->lkb_wait_reply);
+
+       if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
+               overlap_done = 1;
+               goto out_del;
+       }
+
+       /* N.B. type of reply may not always correspond to type of original
+          msg due to lookup->request optimization, verify others? */
+
+       if (lkb->lkb_wait_type) {
+               lkb->lkb_wait_type = 0;
+               goto out_del;
+       }
+
+       log_error(ls, "remove_from_waiters lkid %x flags %x types %d %d",
+                 lkb->lkb_id, lkb->lkb_flags, mstype, lkb->lkb_wait_type);
+       return -1;
+
+ out_del:
+       /* the force-unlock/cancel has completed and we haven't recvd a reply
+          to the op that was in progress prior to the unlock/cancel; we
+          give up on any reply to the earlier op.  FIXME: not sure when/how
+          this would happen */
+
+       if (overlap_done && lkb->lkb_wait_type) {
+               log_error(ls, "remove_from_waiters %x reply %d give up on %d",
+                         lkb->lkb_id, mstype, lkb->lkb_wait_type);
+               lkb->lkb_wait_count--;
+               lkb->lkb_wait_type = 0;
+       }
+
+       DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
+
+       lkb->lkb_flags &= ~DLM_IFL_RESEND;
+       lkb->lkb_wait_count--;
+       if (!lkb->lkb_wait_count)
+               list_del_init(&lkb->lkb_wait_reply);
        unhold_lkb(lkb);
- out:
+       return 0;
+}
+
+static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
+{
+       struct dlm_ls *ls = lkb->lkb_resource->res_ls;
+       int error;
+
+       mutex_lock(&ls->ls_waiters_mutex);
+       error = _remove_from_waiters(lkb, mstype);
+       mutex_unlock(&ls->ls_waiters_mutex);
        return error;
 }
 
-static int remove_from_waiters(struct dlm_lkb *lkb)
+/* Handles situations where we might be processing a "fake" or "stub" reply in
+   which we can't try to take waiters_mutex again. */
+
+static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
 {
        struct dlm_ls *ls = lkb->lkb_resource->res_ls;
        int error;
 
-       down(&ls->ls_waiters_sem);
-       error = _remove_from_waiters(lkb);
-       up(&ls->ls_waiters_sem);
+       if (ms != &ls->ls_stub_ms)
+               mutex_lock(&ls->ls_waiters_mutex);
+       error = _remove_from_waiters(lkb, ms->m_type);
+       if (ms != &ls->ls_stub_ms)
+               mutex_unlock(&ls->ls_waiters_mutex);
        return error;
 }
 
@@ -775,14 +938,14 @@ static int shrink_bucket(struct dlm_ls *ls, int b)
        int count = 0, found;
 
        for (;;) {
-               found = FALSE;
+               found = 0;
                write_lock(&ls->ls_rsbtbl[b].lock);
                list_for_each_entry_reverse(r, &ls->ls_rsbtbl[b].toss,
                                            res_hashchain) {
                        if (!time_after_eq(jiffies, r->res_toss_time +
-                                          dlm_config.toss_secs * HZ))
+                                          dlm_config.ci_toss_secs * HZ))
                                continue;
-                       found = TRUE;
+                       found = 1;
                        break;
                }
 
@@ -914,7 +1077,7 @@ static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
        if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
                return;
 
-       b =  dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
+       b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
        if (b == 1) {
                int len = receive_extralen(ms);
                memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
@@ -952,13 +1115,22 @@ static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
        _remove_lock(r, lkb);
 }
 
-static void revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
+/* returns: 0 did nothing
+           1 moved lock to granted
+          -1 removed lock */
+
+static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
+       int rv = 0;
+
        lkb->lkb_rqmode = DLM_LOCK_IV;
 
        switch (lkb->lkb_status) {
+       case DLM_LKSTS_GRANTED:
+               break;
        case DLM_LKSTS_CONVERT:
                move_lkb(r, lkb, DLM_LKSTS_GRANTED);
+               rv = 1;
                break;
        case DLM_LKSTS_WAITING:
                del_lkb(r, lkb);
@@ -966,15 +1138,17 @@ static void revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
                /* this unhold undoes the original ref from create_lkb()
                   so this leads to the lkb being freed */
                unhold_lkb(lkb);
+               rv = -1;
                break;
        default:
                log_print("invalid status for revert %d", lkb->lkb_status);
        }
+       return rv;
 }
 
-static void revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
+static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
-       revert_lock(r, lkb);
+       return revert_lock(r, lkb);
 }
 
 static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
@@ -988,11 +1162,6 @@ static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
        }
 
        lkb->lkb_rqmode = DLM_LOCK_IV;
-
-       if (lkb->lkb_range) {
-               lkb->lkb_range[GR_RANGE_START] = lkb->lkb_range[RQ_RANGE_START];
-               lkb->lkb_range[GR_RANGE_END] = lkb->lkb_range[RQ_RANGE_END];
-       }
 }
 
 static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
@@ -1027,24 +1196,9 @@ static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
        struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
                                           lkb_statequeue);
        if (lkb->lkb_id == first->lkb_id)
-               return TRUE;
-
-       return FALSE;
-}
-
-/* Return 1 if the locks' ranges overlap.  If the lkb has no range then it is
-   assumed to cover 0-ffffffff.ffffffff */
-
-static inline int ranges_overlap(struct dlm_lkb *lkb1, struct dlm_lkb *lkb2)
-{
-       if (!lkb1->lkb_range || !lkb2->lkb_range)
-               return TRUE;
-
-       if (lkb1->lkb_range[RQ_RANGE_END] < lkb2->lkb_range[GR_RANGE_START] ||
-           lkb1->lkb_range[RQ_RANGE_START] > lkb2->lkb_range[GR_RANGE_END])
-               return FALSE;
+               return 1;
 
-       return TRUE;
+       return 0;
 }
 
 /* Check if the given lkb conflicts with another lkb on the queue. */
@@ -1056,10 +1210,10 @@ static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
        list_for_each_entry(this, head, lkb_statequeue) {
                if (this == lkb)
                        continue;
-               if (ranges_overlap(lkb, this) && !modes_compat(this, lkb))
-                       return TRUE;
+               if (!modes_compat(this, lkb))
+                       return 1;
        }
-       return FALSE;
+       return 0;
 }
 
 /*
@@ -1099,11 +1253,8 @@ static int conversion_deadlock_detect(struct dlm_rsb *rsb, struct dlm_lkb *lkb)
                        continue;
                }
 
-               if (!ranges_overlap(lkb, this))
-                       continue;
-
                if (!modes_compat(this, lkb) && !modes_compat(lkb, this))
-                       return TRUE;
+                       return 1;
        }
 
        /* if lkb is on the convert queue and is preventing the first
@@ -1114,10 +1265,10 @@ static int conversion_deadlock_detect(struct dlm_rsb *rsb, struct dlm_lkb *lkb)
        if (self && self != first) {
                if (!modes_compat(lkb, first) &&
                    !queue_conflict(&rsb->res_grantqueue, first))
-                       return TRUE;
+                       return 1;
        }
 
-       return FALSE;
+       return 0;
 }
 
 /*
@@ -1157,7 +1308,7 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
         */
 
        if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
-               return TRUE;
+               return 1;
 
        /*
         * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
@@ -1200,15 +1351,15 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
         */
 
        if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
-               return TRUE;
+               return 1;
 
        /*
-        * When using range locks the NOORDER flag is set to avoid the standard
-        * vms rules on grant order.
+        * The NOORDER flag is set to avoid the standard vms rules on grant
+        * order.
         */
 
        if (lkb->lkb_exflags & DLM_LKF_NOORDER)
-               return TRUE;
+               return 1;
 
        /*
         * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
@@ -1217,7 +1368,7 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
         */
 
        if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
-               return TRUE;
+               return 1;
 
        /*
         * 6-4: By default, a new request is immediately granted only if all
@@ -1232,7 +1383,7 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
 
        if (now && !conv && list_empty(&r->res_convertqueue) &&
            list_empty(&r->res_waitqueue))
-               return TRUE;
+               return 1;
 
        /*
         * 6-4: Once a lock request is in the queue of ungranted new requests,
@@ -1244,7 +1395,7 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
 
        if (!now && !conv && list_empty(&r->res_convertqueue) &&
            first_in_list(lkb, &r->res_waitqueue))
-               return TRUE;
+               return 1;
 
  out:
        /*
@@ -1257,7 +1408,7 @@ static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
                lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
        }
 
-       return FALSE;
+       return 0;
 }
 
 /*
@@ -1308,7 +1459,7 @@ static int grant_pending_convert(struct dlm_rsb *r, int high)
 
        list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
                demoted = is_demoted(lkb);
-               if (can_be_granted(r, lkb, FALSE)) {
+               if (can_be_granted(r, lkb, 0)) {
                        grant_lock_pending(r, lkb);
                        grant_restart = 1;
                } else {
@@ -1333,7 +1484,7 @@ static int grant_pending_wait(struct dlm_rsb *r, int high)
        struct dlm_lkb *lkb, *s;
 
        list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
-               if (can_be_granted(r, lkb, FALSE))
+               if (can_be_granted(r, lkb, 0))
                        grant_lock_pending(r, lkb);
                 else
                        high = max_t(int, lkb->lkb_rqmode, high);
@@ -1347,7 +1498,7 @@ static void grant_pending_locks(struct dlm_rsb *r)
        struct dlm_lkb *lkb, *s;
        int high = DLM_LOCK_IV;
 
-       DLM_ASSERT(is_master(r), dlm_print_rsb(r););
+       DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
 
        high = grant_pending_convert(r, high);
        high = grant_pending_wait(r, high);
@@ -1358,8 +1509,7 @@ static void grant_pending_locks(struct dlm_rsb *r)
        /*
         * If there are locks left on the wait/convert queue then send blocking
         * ASTs to granted locks based on the largest requested mode (high)
-        * found above.  This can generate spurious blocking ASTs for range
-        * locks. FIXME: highbast < high comparison not valid for PR/CW.
+        * found above. FIXME: highbast < high comparison not valid for PR/CW.
         */
 
        list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
@@ -1379,7 +1529,7 @@ static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
        list_for_each_entry(gr, head, lkb_statequeue) {
                if (gr->lkb_bastaddr &&
                    gr->lkb_highbast < lkb->lkb_rqmode &&
-                   ranges_overlap(lkb, gr) && !modes_compat(gr, lkb)) {
+                   !modes_compat(gr, lkb)) {
                        queue_bast(r, gr, lkb->lkb_rqmode);
                        gr->lkb_highbast = lkb->lkb_rqmode;
                }
@@ -1443,7 +1593,7 @@ static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
                return 0;
        }
 
-       DLM_ASSERT(r->res_nodeid == -1, dlm_print_rsb(r););
+       DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
 
        dir_nodeid = dlm_dir_nodeid(r);
 
@@ -1485,7 +1635,7 @@ static void process_lookup_list(struct dlm_rsb *r)
        struct dlm_lkb *lkb, *safe;
 
        list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
-               list_del(&lkb->lkb_rsb_lookup);
+               list_del_init(&lkb->lkb_rsb_lookup);
                _request_lock(r, lkb);
                schedule();
        }
@@ -1516,7 +1666,7 @@ static void confirm_master(struct dlm_rsb *r, int error)
                if (!list_empty(&r->res_lookup)) {
                        lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
                                         lkb_rsb_lookup);
-                       list_del(&lkb->lkb_rsb_lookup);
+                       list_del_init(&lkb->lkb_rsb_lookup);
                        r->res_first_lkid = lkb->lkb_id;
                        _request_lock(r, lkb);
                } else
@@ -1530,8 +1680,7 @@ static void confirm_master(struct dlm_rsb *r, int error)
 
 static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
                         int namelen, uint32_t parent_lkid, void *ast,
-                        void *astarg, void *bast, struct dlm_range *range,
-                        struct dlm_args *args)
+                        void *astarg, void *bast, struct dlm_args *args)
 {
        int rv = -EINVAL;
 
@@ -1590,7 +1739,6 @@ static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
        args->bastaddr = bast;
        args->mode = mode;
        args->lksb = lksb;
-       args->range = range;
        rv = 0;
  out:
        return rv;
@@ -1602,6 +1750,9 @@ static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
                      DLM_LKF_FORCEUNLOCK))
                return -EINVAL;
 
+       if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
+               return -EINVAL;
+
        args->flags = flags;
        args->astparam = (long) astarg;
        return 0;
@@ -1626,6 +1777,9 @@ static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
 
                if (lkb->lkb_wait_type)
                        goto out;
+
+               if (is_overlap(lkb))
+                       goto out;
        }
 
        lkb->lkb_exflags = args->flags;
@@ -1637,60 +1791,131 @@ static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
        lkb->lkb_lksb = args->lksb;
        lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
        lkb->lkb_ownpid = (int) current->pid;
-
-       rv = 0;
-       if (!args->range)
-               goto out;
-
-       if (!lkb->lkb_range) {
-               rv = -ENOMEM;
-               lkb->lkb_range = allocate_range(ls);
-               if (!lkb->lkb_range)
-                       goto out;
-               /* This is needed for conversions that contain ranges
-                  where the original lock didn't but it's harmless for
-                  new locks too. */
-               lkb->lkb_range[GR_RANGE_START] = 0LL;
-               lkb->lkb_range[GR_RANGE_END] = 0xffffffffffffffffULL;
-       }
-
-       lkb->lkb_range[RQ_RANGE_START] = args->range->ra_start;
-       lkb->lkb_range[RQ_RANGE_END] = args->range->ra_end;
-       lkb->lkb_flags |= DLM_IFL_RANGE;
        rv = 0;
  out:
        return rv;
 }
 
+/* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
+   for success */
+
+/* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
+   because there may be a lookup in progress and it's valid to do
+   cancel/unlockf on it */
+
 static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
 {
+       struct dlm_ls *ls = lkb->lkb_resource->res_ls;
        int rv = -EINVAL;
 
-       if (lkb->lkb_flags & DLM_IFL_MSTCPY)
+       if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
+               log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
+               dlm_print_lkb(lkb);
                goto out;
+       }
 
-       if (args->flags & DLM_LKF_FORCEUNLOCK)
-               goto out_ok;
+       /* an lkb may still exist even though the lock is EOL'ed due to a
+          cancel, unlock or failed noqueue request; an app can't use these
+          locks; return same error as if the lkid had not been found at all */
 
-       if (args->flags & DLM_LKF_CANCEL &&
-           lkb->lkb_status == DLM_LKSTS_GRANTED)
+       if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
+               log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
+               rv = -ENOENT;
                goto out;
+       }
 
-       if (!(args->flags & DLM_LKF_CANCEL) &&
-           lkb->lkb_status != DLM_LKSTS_GRANTED)
-               goto out;
+       /* an lkb may be waiting for an rsb lookup to complete where the
+          lookup was initiated by another lock */
+
+       if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
+               if (!list_empty(&lkb->lkb_rsb_lookup)) {
+                       log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
+                       list_del_init(&lkb->lkb_rsb_lookup);
+                       queue_cast(lkb->lkb_resource, lkb,
+                                  args->flags & DLM_LKF_CANCEL ?
+                                  -DLM_ECANCEL : -DLM_EUNLOCK);
+                       unhold_lkb(lkb); /* undoes create_lkb() */
+                       rv = -EBUSY;
+                       goto out;
+               }
+       }
+
+       /* cancel not allowed with another cancel/unlock in progress */
+
+       if (args->flags & DLM_LKF_CANCEL) {
+               if (lkb->lkb_exflags & DLM_LKF_CANCEL)
+                       goto out;
+
+               if (is_overlap(lkb))
+                       goto out;
+
+               if (lkb->lkb_flags & DLM_IFL_RESEND) {
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
+                       rv = -EBUSY;
+                       goto out;
+               }
+
+               switch (lkb->lkb_wait_type) {
+               case DLM_MSG_LOOKUP:
+               case DLM_MSG_REQUEST:
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
+                       rv = -EBUSY;
+                       goto out;
+               case DLM_MSG_UNLOCK:
+               case DLM_MSG_CANCEL:
+                       goto out;
+               }
+               /* add_to_waiters() will set OVERLAP_CANCEL */
+               goto out_ok;
+       }
+
+       /* do we need to allow a force-unlock if there's a normal unlock
+          already in progress?  in what conditions could the normal unlock
+          fail such that we'd want to send a force-unlock to be sure? */
+
+       if (args->flags & DLM_LKF_FORCEUNLOCK) {
+               if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
+                       goto out;
+
+               if (is_overlap_unlock(lkb))
+                       goto out;
+
+               if (lkb->lkb_flags & DLM_IFL_RESEND) {
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
+                       rv = -EBUSY;
+                       goto out;
+               }
+
+               switch (lkb->lkb_wait_type) {
+               case DLM_MSG_LOOKUP:
+               case DLM_MSG_REQUEST:
+                       lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
+                       rv = -EBUSY;
+                       goto out;
+               case DLM_MSG_UNLOCK:
+                       goto out;
+               }
+               /* add_to_waiters() will set OVERLAP_UNLOCK */
+               goto out_ok;
+       }
 
+       /* normal unlock not allowed if there's any op in progress */
        rv = -EBUSY;
-       if (lkb->lkb_wait_type)
+       if (lkb->lkb_wait_type || lkb->lkb_wait_count)
                goto out;
 
  out_ok:
-       lkb->lkb_exflags = args->flags;
+       /* an overlapping op shouldn't blow away exflags from other op */
+       lkb->lkb_exflags |= args->flags;
        lkb->lkb_sbflags = 0;
        lkb->lkb_astparam = args->astparam;
-
        rv = 0;
  out:
+       if (rv)
+               log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
+                         lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
+                         args->flags, lkb->lkb_wait_type,
+                         lkb->lkb_resource->res_name);
        return rv;
 }
 
@@ -1705,7 +1930,7 @@ static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
        int error = 0;
 
-       if (can_be_granted(r, lkb, TRUE)) {
+       if (can_be_granted(r, lkb, 1)) {
                grant_lock(r, lkb);
                queue_cast(r, lkb, 0);
                goto out;
@@ -1733,7 +1958,7 @@ static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
 
        /* changing an existing lock may allow others to be granted */
 
-       if (can_be_granted(r, lkb, TRUE)) {
+       if (can_be_granted(r, lkb, 1)) {
                grant_lock(r, lkb);
                queue_cast(r, lkb, 0);
                grant_pending_locks(r);
@@ -1767,12 +1992,19 @@ static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
        return -DLM_EUNLOCK;
 }
 
+/* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
 static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
-       revert_lock(r, lkb);
-       queue_cast(r, lkb, -DLM_ECANCEL);
-       grant_pending_locks(r);
-       return -DLM_ECANCEL;
+       int error;
+
+       error = revert_lock(r, lkb);
+       if (error) {
+               queue_cast(r, lkb, -DLM_ECANCEL);
+               grant_pending_locks(r);
+               return -DLM_ECANCEL;
+       }
+       return 0;
 }
 
 /*
@@ -1805,7 +2037,7 @@ static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
        return error;
 }
 
-/* change some property of an existing lkb, e.g. mode, range */
+/* change some property of an existing lkb, e.g. mode */
 
 static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
 {
@@ -1962,8 +2194,7 @@ int dlm_lock(dlm_lockspace_t *lockspace,
             uint32_t parent_lkid,
             void (*ast) (void *astarg),
             void *astarg,
-            void (*bast) (void *astarg, int mode),
-            struct dlm_range *range)
+            void (*bast) (void *astarg, int mode))
 {
        struct dlm_ls *ls;
        struct dlm_lkb *lkb;
@@ -1985,7 +2216,7 @@ int dlm_lock(dlm_lockspace_t *lockspace,
                goto out;
 
        error = set_lock_args(mode, lksb, flags, namelen, parent_lkid, ast,
-                             astarg, bast, range, &args);
+                             astarg, bast, &args);
        if (error)
                goto out_put;
 
@@ -1998,7 +2229,7 @@ int dlm_lock(dlm_lockspace_t *lockspace,
                error = 0;
  out_put:
        if (convert || error)
-               put_lkb(lkb);
+               __put_lkb(ls, lkb);
        if (error == -EAGAIN)
                error = 0;
  out:
@@ -2039,8 +2270,10 @@ int dlm_unlock(dlm_lockspace_t *lockspace,
 
        if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
                error = 0;
+       if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
+               error = 0;
  out_put:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
  out:
        unlock_recovery(ls);
        dlm_put_lockspace(ls);
@@ -2069,31 +2302,14 @@ int dlm_unlock(dlm_lockspace_t *lockspace,
  * receive_lookup_reply                send_lookup_reply
  */
 
-static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
-                         int to_nodeid, int mstype,
-                         struct dlm_message **ms_ret,
-                         struct dlm_mhandle **mh_ret)
+static int _create_message(struct dlm_ls *ls, int mb_len,
+                          int to_nodeid, int mstype,
+                          struct dlm_message **ms_ret,
+                          struct dlm_mhandle **mh_ret)
 {
        struct dlm_message *ms;
        struct dlm_mhandle *mh;
        char *mb;
-       int mb_len = sizeof(struct dlm_message);
-
-       switch (mstype) {
-       case DLM_MSG_REQUEST:
-       case DLM_MSG_LOOKUP:
-       case DLM_MSG_REMOVE:
-               mb_len += r->res_length;
-               break;
-       case DLM_MSG_CONVERT:
-       case DLM_MSG_UNLOCK:
-       case DLM_MSG_REQUEST_REPLY:
-       case DLM_MSG_CONVERT_REPLY:
-       case DLM_MSG_GRANT:
-               if (lkb && lkb->lkb_lvbptr)
-                       mb_len += r->res_ls->ls_lvblen;
-               break;
-       }
 
        /* get_buffer gives us a message handle (mh) that we need to
           pass into lowcomms_commit and a message buffer (mb) that we
@@ -2108,7 +2324,7 @@ static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
        ms = (struct dlm_message *) mb;
 
        ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
-       ms->m_header.h_lockspace = r->res_ls->ls_global_id;
+       ms->m_header.h_lockspace = ls->ls_global_id;
        ms->m_header.h_nodeid = dlm_our_nodeid();
        ms->m_header.h_length = mb_len;
        ms->m_header.h_cmd = DLM_MSG;
@@ -2120,6 +2336,33 @@ static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
        return 0;
 }
 
+static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
+                         int to_nodeid, int mstype,
+                         struct dlm_message **ms_ret,
+                         struct dlm_mhandle **mh_ret)
+{
+       int mb_len = sizeof(struct dlm_message);
+
+       switch (mstype) {
+       case DLM_MSG_REQUEST:
+       case DLM_MSG_LOOKUP:
+       case DLM_MSG_REMOVE:
+               mb_len += r->res_length;
+               break;
+       case DLM_MSG_CONVERT:
+       case DLM_MSG_UNLOCK:
+       case DLM_MSG_REQUEST_REPLY:
+       case DLM_MSG_CONVERT_REPLY:
+       case DLM_MSG_GRANT:
+               if (lkb && lkb->lkb_lvbptr)
+                       mb_len += r->res_ls->ls_lvblen;
+               break;
+       }
+
+       return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
+                              ms_ret, mh_ret);
+}
+
 /* further lowcomms enhancements or alternate implementations may make
    the return value from this function useful at some point */
 
@@ -2154,17 +2397,24 @@ static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
        if (lkb->lkb_astaddr)
                ms->m_asts |= AST_COMP;
 
-       if (lkb->lkb_range) {
-               ms->m_range[0] = lkb->lkb_range[RQ_RANGE_START];
-               ms->m_range[1] = lkb->lkb_range[RQ_RANGE_END];
-       }
+       /* compare with switch in create_message; send_remove() doesn't
+          use send_args() */
 
-       if (ms->m_type == DLM_MSG_REQUEST || ms->m_type == DLM_MSG_LOOKUP)
+       switch (ms->m_type) {
+       case DLM_MSG_REQUEST:
+       case DLM_MSG_LOOKUP:
                memcpy(ms->m_extra, r->res_name, r->res_length);
-
-       else if (lkb->lkb_lvbptr)
+               break;
+       case DLM_MSG_CONVERT:
+       case DLM_MSG_UNLOCK:
+       case DLM_MSG_REQUEST_REPLY:
+       case DLM_MSG_CONVERT_REPLY:
+       case DLM_MSG_GRANT:
+               if (!lkb->lkb_lvbptr)
+                       break;
                memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
-
+               break;
+       }
 }
 
 static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
@@ -2173,7 +2423,9 @@ static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
        struct dlm_mhandle *mh;
        int to_nodeid, error;
 
-       add_to_waiters(lkb, mstype);
+       error = add_to_waiters(lkb, mstype);
+       if (error)
+               return error;
 
        to_nodeid = r->res_nodeid;
 
@@ -2189,7 +2441,7 @@ static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
        return 0;
 
  fail:
-       remove_from_waiters(lkb);
+       remove_from_waiters(lkb, msg_reply_type(mstype));
        return error;
 }
 
@@ -2206,8 +2458,10 @@ static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
 
        /* down conversions go without a reply from the master */
        if (!error && down_conversion(lkb)) {
-               remove_from_waiters(lkb);
+               remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
+               r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
                r->res_ls->ls_stub_ms.m_result = 0;
+               r->res_ls->ls_stub_ms.m_flags = lkb->lkb_flags;
                __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
        }
 
@@ -2276,7 +2530,9 @@ static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
        struct dlm_mhandle *mh;
        int to_nodeid, error;
 
-       add_to_waiters(lkb, DLM_MSG_LOOKUP);
+       error = add_to_waiters(lkb, DLM_MSG_LOOKUP);
+       if (error)
+               return error;
 
        to_nodeid = dlm_dir_nodeid(r);
 
@@ -2292,7 +2548,7 @@ static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
        return 0;
 
  fail:
-       remove_from_waiters(lkb);
+       remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
        return error;
 }
 
@@ -2386,6 +2642,7 @@ static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
 static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
 {
        lkb->lkb_exflags = ms->m_exflags;
+       lkb->lkb_sbflags = ms->m_sbflags;
        lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
                         (ms->m_flags & 0x0000FFFF);
 }
@@ -2402,22 +2659,8 @@ static int receive_extralen(struct dlm_message *ms)
        return (ms->m_header.h_length - sizeof(struct dlm_message));
 }
 
-static int receive_range(struct dlm_ls *ls, struct dlm_lkb *lkb,
-                        struct dlm_message *ms)
-{
-       if (lkb->lkb_flags & DLM_IFL_RANGE) {
-               if (!lkb->lkb_range)
-                       lkb->lkb_range = allocate_range(ls);
-               if (!lkb->lkb_range)
-                       return -ENOMEM;
-               lkb->lkb_range[RQ_RANGE_START] = ms->m_range[0];
-               lkb->lkb_range[RQ_RANGE_END] = ms->m_range[1];
-       }
-       return 0;
-}
-
-static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
-                      struct dlm_message *ms)
+static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
+                      struct dlm_message *ms)
 {
        int len;
 
@@ -2445,11 +2688,12 @@ static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
 
        DLM_ASSERT(is_master_copy(lkb), dlm_print_lkb(lkb););
 
-       if (receive_range(ls, lkb, ms))
-               return -ENOMEM;
-
-       if (receive_lvb(ls, lkb, ms))
-               return -ENOMEM;
+       if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
+               /* lkb was just created so there won't be an lvb yet */
+               lkb->lkb_lvbptr = allocate_lvb(ls);
+               if (!lkb->lkb_lvbptr)
+                       return -ENOMEM;
+       }
 
        return 0;
 }
@@ -2470,13 +2714,6 @@ static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
        if (lkb->lkb_status != DLM_LKSTS_GRANTED)
                return -EBUSY;
 
-       if (receive_range(ls, lkb, ms))
-               return -ENOMEM;
-       if (lkb->lkb_range) {
-               lkb->lkb_range[GR_RANGE_START] = 0LL;
-               lkb->lkb_range[GR_RANGE_END] = 0xffffffffffffffffULL;
-       }
-
        if (receive_lvb(ls, lkb, ms))
                return -ENOMEM;
 
@@ -2520,7 +2757,7 @@ static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
        lkb->lkb_flags |= DLM_IFL_MSTCPY;
        error = receive_request_args(ls, lkb, ms);
        if (error) {
-               put_lkb(lkb);
+               __put_lkb(ls, lkb);
                goto fail;
        }
 
@@ -2528,7 +2765,7 @@ static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
 
        error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
        if (error) {
-               put_lkb(lkb);
+               __put_lkb(ls, lkb);
                goto fail;
        }
 
@@ -2544,7 +2781,7 @@ static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
        if (error == -EINPROGRESS)
                error = 0;
        if (error)
-               put_lkb(lkb);
+               dlm_put_lkb(lkb);
        return;
 
  fail:
@@ -2556,7 +2793,7 @@ static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
 {
        struct dlm_lkb *lkb;
        struct dlm_rsb *r;
-       int error, reply = TRUE;
+       int error, reply = 1;
 
        error = find_lkb(ls, ms->m_remid, &lkb);
        if (error)
@@ -2580,7 +2817,7 @@ static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
 
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
        return;
 
  fail:
@@ -2614,7 +2851,7 @@ static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
 
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
        return;
 
  fail:
@@ -2644,7 +2881,7 @@ static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
 
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
        return;
 
  fail:
@@ -2676,7 +2913,7 @@ static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
 
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
@@ -2701,7 +2938,7 @@ static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
 
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
@@ -2751,11 +2988,16 @@ static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
        dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
 }
 
+static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
+{
+       do_purge(ls, ms->m_nodeid, ms->m_pid);
+}
+
 static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
 {
        struct dlm_lkb *lkb;
        struct dlm_rsb *r;
-       int error, mstype;
+       int error, mstype, result;
 
        error = find_lkb(ls, ms->m_remid, &lkb);
        if (error) {
@@ -2764,20 +3006,15 @@ static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
        }
        DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
 
-       mstype = lkb->lkb_wait_type;
-       error = remove_from_waiters(lkb);
-       if (error) {
-               log_error(ls, "receive_request_reply not on waiters");
-               goto out;
-       }
-
-       /* this is the value returned from do_request() on the master */
-       error = ms->m_result;
-
        r = lkb->lkb_resource;
        hold_rsb(r);
        lock_rsb(r);
 
+       mstype = lkb->lkb_wait_type;
+       error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
+       if (error)
+               goto out;
+
        /* Optimization: the dir node was also the master, so it took our
           lookup as a request and sent request reply instead of lookup reply */
        if (mstype == DLM_MSG_LOOKUP) {
@@ -2785,14 +3022,15 @@ static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
                lkb->lkb_nodeid = r->res_nodeid;
        }
 
-       switch (error) {
+       /* this is the value returned from do_request() on the master */
+       result = ms->m_result;
+
+       switch (result) {
        case -EAGAIN:
-               /* request would block (be queued) on remote master;
-                  the unhold undoes the original ref from create_lkb()
-                  so it leads to the lkb being freed */
+               /* request would block (be queued) on remote master */
                queue_cast(r, lkb, -EAGAIN);
                confirm_master(r, -EAGAIN);
-               unhold_lkb(lkb);
+               unhold_lkb(lkb); /* undoes create_lkb() */
                break;
 
        case -EINPROGRESS:
@@ -2800,41 +3038,62 @@ static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
                /* request was queued or granted on remote master */
                receive_flags_reply(lkb, ms);
                lkb->lkb_remid = ms->m_lkid;
-               if (error)
+               if (result)
                        add_lkb(r, lkb, DLM_LKSTS_WAITING);
                else {
                        grant_lock_pc(r, lkb, ms);
                        queue_cast(r, lkb, 0);
                }
-               confirm_master(r, error);
+               confirm_master(r, result);
                break;
 
-       case -ENOENT:
+       case -EBADR:
        case -ENOTBLK:
                /* find_rsb failed to find rsb or rsb wasn't master */
+               log_debug(ls, "receive_request_reply %x %x master diff %d %d",
+                         lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
                r->res_nodeid = -1;
                lkb->lkb_nodeid = -1;
-               _request_lock(r, lkb);
+
+               if (is_overlap(lkb)) {
+                       /* we'll ignore error in cancel/unlock reply */
+                       queue_cast_overlap(r, lkb);
+                       unhold_lkb(lkb); /* undoes create_lkb() */
+               } else
+                       _request_lock(r, lkb);
                break;
 
        default:
-               log_error(ls, "receive_request_reply error %d", error);
+               log_error(ls, "receive_request_reply %x error %d",
+                         lkb->lkb_id, result);
        }
 
+       if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
+               log_debug(ls, "receive_request_reply %x result %d unlock",
+                         lkb->lkb_id, result);
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
+               send_unlock(r, lkb);
+       } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
+               log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
+               send_cancel(r, lkb);
+       } else {
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
+       }
+ out:
        unlock_rsb(r);
        put_rsb(r);
- out:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
                                    struct dlm_message *ms)
 {
-       int error = ms->m_result;
-
        /* this is the value returned from do_convert() on the master */
-
-       switch (error) {
+       switch (ms->m_result) {
        case -EAGAIN:
                /* convert would block (be queued) on remote master */
                queue_cast(r, lkb, -EAGAIN);
@@ -2854,19 +3113,26 @@ static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
                break;
 
        default:
-               log_error(r->res_ls, "receive_convert_reply error %d", error);
+               log_error(r->res_ls, "receive_convert_reply %x error %d",
+                         lkb->lkb_id, ms->m_result);
        }
 }
 
 static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
 {
        struct dlm_rsb *r = lkb->lkb_resource;
+       int error;
 
        hold_rsb(r);
        lock_rsb(r);
 
-       __receive_convert_reply(r, lkb, ms);
+       /* stub reply can happen with waiters_mutex held */
+       error = remove_from_waiters_ms(lkb, ms);
+       if (error)
+               goto out;
 
+       __receive_convert_reply(r, lkb, ms);
+ out:
        unlock_rsb(r);
        put_rsb(r);
 }
@@ -2883,37 +3149,38 @@ static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
        }
        DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
 
-       error = remove_from_waiters(lkb);
-       if (error) {
-               log_error(ls, "receive_convert_reply not on waiters");
-               goto out;
-       }
-
        _receive_convert_reply(lkb, ms);
- out:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
 {
        struct dlm_rsb *r = lkb->lkb_resource;
-       int error = ms->m_result;
+       int error;
 
        hold_rsb(r);
        lock_rsb(r);
 
+       /* stub reply can happen with waiters_mutex held */
+       error = remove_from_waiters_ms(lkb, ms);
+       if (error)
+               goto out;
+
        /* this is the value returned from do_unlock() on the master */
 
-       switch (error) {
+       switch (ms->m_result) {
        case -DLM_EUNLOCK:
                receive_flags_reply(lkb, ms);
                remove_lock_pc(r, lkb);
                queue_cast(r, lkb, -DLM_EUNLOCK);
                break;
+       case -ENOENT:
+               break;
        default:
-               log_error(r->res_ls, "receive_unlock_reply error %d", error);
+               log_error(r->res_ls, "receive_unlock_reply %x error %d",
+                         lkb->lkb_id, ms->m_result);
        }
-
+ out:
        unlock_rsb(r);
        put_rsb(r);
 }
@@ -2930,37 +3197,39 @@ static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
        }
        DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
 
-       error = remove_from_waiters(lkb);
-       if (error) {
-               log_error(ls, "receive_unlock_reply not on waiters");
-               goto out;
-       }
-
        _receive_unlock_reply(lkb, ms);
- out:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
 {
        struct dlm_rsb *r = lkb->lkb_resource;
-       int error = ms->m_result;
+       int error;
 
        hold_rsb(r);
        lock_rsb(r);
 
+       /* stub reply can happen with waiters_mutex held */
+       error = remove_from_waiters_ms(lkb, ms);
+       if (error)
+               goto out;
+
        /* this is the value returned from do_cancel() on the master */
 
-       switch (error) {
+       switch (ms->m_result) {
        case -DLM_ECANCEL:
                receive_flags_reply(lkb, ms);
                revert_lock_pc(r, lkb);
-               queue_cast(r, lkb, -DLM_ECANCEL);
+               if (ms->m_result)
+                       queue_cast(r, lkb, -DLM_ECANCEL);
+               break;
+       case 0:
                break;
        default:
-               log_error(r->res_ls, "receive_cancel_reply error %d", error);
+               log_error(r->res_ls, "receive_cancel_reply %x error %d",
+                         lkb->lkb_id, ms->m_result);
        }
-
+ out:
        unlock_rsb(r);
        put_rsb(r);
 }
@@ -2977,15 +3246,8 @@ static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
        }
        DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
 
-       error = remove_from_waiters(lkb);
-       if (error) {
-               log_error(ls, "receive_cancel_reply not on waiters");
-               goto out;
-       }
-
        _receive_cancel_reply(lkb, ms);
- out:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
@@ -3000,20 +3262,17 @@ static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
                return;
        }
 
-       error = remove_from_waiters(lkb);
-       if (error) {
-               log_error(ls, "receive_lookup_reply not on waiters");
-               goto out;
-       }
-
-       /* this is the value returned by dlm_dir_lookup on dir node
+       /* ms->m_result is the value returned by dlm_dir_lookup on dir node
           FIXME: will a non-zero error ever be returned? */
-       error = ms->m_result;
 
        r = lkb->lkb_resource;
        hold_rsb(r);
        lock_rsb(r);
 
+       error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
+       if (error)
+               goto out;
+
        ret_nodeid = ms->m_nodeid;
        if (ret_nodeid == dlm_our_nodeid()) {
                r->res_nodeid = 0;
@@ -3024,22 +3283,30 @@ static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
                r->res_nodeid = ret_nodeid;
        }
 
+       if (is_overlap(lkb)) {
+               log_debug(ls, "receive_lookup_reply %x unlock %x",
+                         lkb->lkb_id, lkb->lkb_flags);
+               queue_cast_overlap(r, lkb);
+               unhold_lkb(lkb); /* undoes create_lkb() */
+               goto out_list;
+       }
+
        _request_lock(r, lkb);
 
+ out_list:
        if (!ret_nodeid)
                process_lookup_list(r);
-
+ out:
        unlock_rsb(r);
        put_rsb(r);
- out:
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 }
 
 int dlm_receive_message(struct dlm_header *hd, int nodeid, int recovery)
 {
        struct dlm_message *ms = (struct dlm_message *) hd;
        struct dlm_ls *ls;
-       int error;
+       int error = 0;
 
        if (!recovery)
                dlm_message_in(ms);
@@ -3066,10 +3333,17 @@ int dlm_receive_message(struct dlm_header *hd, int nodeid, int recovery)
 
        while (1) {
                if (dlm_locking_stopped(ls)) {
-                       if (!recovery)
-                               dlm_add_requestqueue(ls, nodeid, hd);
-                       error = -EINTR;
-                       goto out;
+                       if (recovery) {
+                               error = -EINTR;
+                               goto out;
+                       }
+                       error = dlm_add_requestqueue(ls, nodeid, hd);
+                       if (error == -EAGAIN)
+                               continue;
+                       else {
+                               error = -EINTR;
+                               goto out;
+                       }
                }
 
                if (lock_recovery_try(ls))
@@ -3141,6 +3415,12 @@ int dlm_receive_message(struct dlm_header *hd, int nodeid, int recovery)
                receive_lookup_reply(ls, ms);
                break;
 
+       /* other messages */
+
+       case DLM_MSG_PURGE:
+               receive_purge(ls, ms);
+               break;
+
        default:
                log_error(ls, "unknown message type %d", ms->m_type);
        }
@@ -3149,7 +3429,7 @@ int dlm_receive_message(struct dlm_header *hd, int nodeid, int recovery)
  out:
        dlm_put_lockspace(ls);
        dlm_astd_wake();
-       return 0;
+       return error;
 }
 
 
@@ -3161,8 +3441,9 @@ static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb)
 {
        if (middle_conversion(lkb)) {
                hold_lkb(lkb);
+               ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
                ls->ls_stub_ms.m_result = -EINPROGRESS;
-               _remove_from_waiters(lkb);
+               ls->ls_stub_ms.m_flags = lkb->lkb_flags;
                _receive_convert_reply(lkb, &ls->ls_stub_ms);
 
                /* Same special case as in receive_rcom_lock_args() */
@@ -3205,7 +3486,7 @@ void dlm_recover_waiters_pre(struct dlm_ls *ls)
 {
        struct dlm_lkb *lkb, *safe;
 
-       down(&ls->ls_waiters_sem);
+       mutex_lock(&ls->ls_waiters_mutex);
 
        list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
                log_debug(ls, "pre recover waiter lkid %x type %d flags %x",
@@ -3234,59 +3515,72 @@ void dlm_recover_waiters_pre(struct dlm_ls *ls)
 
                case DLM_MSG_UNLOCK:
                        hold_lkb(lkb);
+                       ls->ls_stub_ms.m_type = DLM_MSG_UNLOCK_REPLY;
                        ls->ls_stub_ms.m_result = -DLM_EUNLOCK;
-                       _remove_from_waiters(lkb);
+                       ls->ls_stub_ms.m_flags = lkb->lkb_flags;
                        _receive_unlock_reply(lkb, &ls->ls_stub_ms);
-                       put_lkb(lkb);
+                       dlm_put_lkb(lkb);
                        break;
 
                case DLM_MSG_CANCEL:
                        hold_lkb(lkb);
+                       ls->ls_stub_ms.m_type = DLM_MSG_CANCEL_REPLY;
                        ls->ls_stub_ms.m_result = -DLM_ECANCEL;
-                       _remove_from_waiters(lkb);
+                       ls->ls_stub_ms.m_flags = lkb->lkb_flags;
                        _receive_cancel_reply(lkb, &ls->ls_stub_ms);
-                       put_lkb(lkb);
+                       dlm_put_lkb(lkb);
                        break;
 
                default:
                        log_error(ls, "invalid lkb wait_type %d",
                                  lkb->lkb_wait_type);
                }
+               schedule();
        }
-       up(&ls->ls_waiters_sem);
+       mutex_unlock(&ls->ls_waiters_mutex);
 }
 
-static int remove_resend_waiter(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
+static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
 {
        struct dlm_lkb *lkb;
-       int rv = 0;
+       int found = 0;
 
-       down(&ls->ls_waiters_sem);
+       mutex_lock(&ls->ls_waiters_mutex);
        list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
                if (lkb->lkb_flags & DLM_IFL_RESEND) {
-                       rv = lkb->lkb_wait_type;
-                       _remove_from_waiters(lkb);
-                       lkb->lkb_flags &= ~DLM_IFL_RESEND;
+                       hold_lkb(lkb);
+                       found = 1;
                        break;
                }
        }
-       up(&ls->ls_waiters_sem);
+       mutex_unlock(&ls->ls_waiters_mutex);
 
-       if (!rv)
+       if (!found)
                lkb = NULL;
-       *lkb_ret = lkb;
-       return rv;
+       return lkb;
 }
 
 /* Deal with lookups and lkb's marked RESEND from _pre.  We may now be the
    master or dir-node for r.  Processing the lkb may result in it being placed
    back on waiters. */
 
+/* We do this after normal locking has been enabled and any saved messages
+   (in requestqueue) have been processed.  We should be confident that at
+   this point we won't get or process a reply to any of these waiting
+   operations.  But, new ops may be coming in on the rsbs/locks here from
+   userspace or remotely. */
+
+/* there may have been an overlap unlock/cancel prior to recovery or after
+   recovery.  if before, the lkb may still have a pos wait_count; if after, the
+   overlap flag would just have been set and nothing new sent.  we can be
+   confident here than any replies to either the initial op or overlap ops
+   prior to recovery have been received. */
+
 int dlm_recover_waiters_post(struct dlm_ls *ls)
 {
        struct dlm_lkb *lkb;
        struct dlm_rsb *r;
-       int error = 0, mstype;
+       int error = 0, mstype, err, oc, ou;
 
        while (1) {
                if (dlm_locking_stopped(ls)) {
@@ -3295,46 +3589,78 @@ int dlm_recover_waiters_post(struct dlm_ls *ls)
                        break;
                }
 
-               mstype = remove_resend_waiter(ls, &lkb);
-               if (!mstype)
+               lkb = find_resend_waiter(ls);
+               if (!lkb)
                        break;
 
                r = lkb->lkb_resource;
+               hold_rsb(r);
+               lock_rsb(r);
+
+               mstype = lkb->lkb_wait_type;
+               oc = is_overlap_cancel(lkb);
+               ou = is_overlap_unlock(lkb);
+               err = 0;
 
                log_debug(ls, "recover_waiters_post %x type %d flags %x %s",
                          lkb->lkb_id, mstype, lkb->lkb_flags, r->res_name);
 
-               switch (mstype) {
-
-               case DLM_MSG_LOOKUP:
-                       hold_rsb(r);
-                       lock_rsb(r);
-                       _request_lock(r, lkb);
-                       if (is_master(r))
-                               confirm_master(r, 0);
-                       unlock_rsb(r);
-                       put_rsb(r);
-                       break;
-
-               case DLM_MSG_REQUEST:
-                       hold_rsb(r);
-                       lock_rsb(r);
-                       _request_lock(r, lkb);
-                       unlock_rsb(r);
-                       put_rsb(r);
-                       break;
-
-               case DLM_MSG_CONVERT:
-                       hold_rsb(r);
-                       lock_rsb(r);
-                       _convert_lock(r, lkb);
-                       unlock_rsb(r);
-                       put_rsb(r);
-                       break;
-
-               default:
-                       log_error(ls, "recover_waiters_post type %d", mstype);
+               /* At this point we assume that we won't get a reply to any
+                  previous op or overlap op on this lock.  First, do a big
+                  remove_from_waiters() for all previous ops. */
+
+               lkb->lkb_flags &= ~DLM_IFL_RESEND;
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
+               lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
+               lkb->lkb_wait_type = 0;
+               lkb->lkb_wait_count = 0;
+               mutex_lock(&ls->ls_waiters_mutex);
+               list_del_init(&lkb->lkb_wait_reply);
+               mutex_unlock(&ls->ls_waiters_mutex);
+               unhold_lkb(lkb); /* for waiters list */
+
+               if (oc || ou) {
+                       /* do an unlock or cancel instead of resending */
+                       switch (mstype) {
+                       case DLM_MSG_LOOKUP:
+                       case DLM_MSG_REQUEST:
+                               queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
+                                                       -DLM_ECANCEL);
+                               unhold_lkb(lkb); /* undoes create_lkb() */
+                               break;
+                       case DLM_MSG_CONVERT:
+                               if (oc) {
+                                       queue_cast(r, lkb, -DLM_ECANCEL);
+                               } else {
+                                       lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
+                                       _unlock_lock(r, lkb);
+                               }
+                               break;
+                       default:
+                               err = 1;
+                       }
+               } else {
+                       switch (mstype) {
+                       case DLM_MSG_LOOKUP:
+                       case DLM_MSG_REQUEST:
+                               _request_lock(r, lkb);
+                               if (is_master(r))
+                                       confirm_master(r, 0);
+                               break;
+                       case DLM_MSG_CONVERT:
+                               _convert_lock(r, lkb);
+                               break;
+                       default:
+                               err = 1;
+                       }
                }
+
+               if (err)
+                       log_error(ls, "recover_waiters_post %x %d %x %d %d",
+                                 lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
+               unlock_rsb(r);
+               put_rsb(r);
+               dlm_put_lkb(lkb);
        }
 
        return error;
@@ -3348,9 +3674,10 @@ static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
 
        list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
                if (test(ls, lkb)) {
+                       rsb_set_flag(r, RSB_LOCKS_PURGED);
                        del_lkb(r, lkb);
                        /* this put should free the lkb */
-                       if (!put_lkb(lkb))
+                       if (!dlm_put_lkb(lkb))
                                log_error(ls, "purged lkb not released");
                }
        }
@@ -3404,27 +3731,45 @@ int dlm_purge_locks(struct dlm_ls *ls)
        return 0;
 }
 
-int dlm_grant_after_purge(struct dlm_ls *ls)
+static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
+{
+       struct dlm_rsb *r, *r_ret = NULL;
+
+       read_lock(&ls->ls_rsbtbl[bucket].lock);
+       list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list, res_hashchain) {
+               if (!rsb_flag(r, RSB_LOCKS_PURGED))
+                       continue;
+               hold_rsb(r);
+               rsb_clear_flag(r, RSB_LOCKS_PURGED);
+               r_ret = r;
+               break;
+       }
+       read_unlock(&ls->ls_rsbtbl[bucket].lock);
+       return r_ret;
+}
+
+void dlm_grant_after_purge(struct dlm_ls *ls)
 {
        struct dlm_rsb *r;
-       int i;
+       int bucket = 0;
 
-       for (i = 0; i < ls->ls_rsbtbl_size; i++) {
-               read_lock(&ls->ls_rsbtbl[i].lock);
-               list_for_each_entry(r, &ls->ls_rsbtbl[i].list, res_hashchain) {
-                       hold_rsb(r);
-                       lock_rsb(r);
-                       if (is_master(r)) {
-                               grant_pending_locks(r);
-                               confirm_master(r, 0);
-                       }
-                       unlock_rsb(r);
-                       put_rsb(r);
+       while (1) {
+               r = find_purged_rsb(ls, bucket);
+               if (!r) {
+                       if (bucket == ls->ls_rsbtbl_size - 1)
+                               break;
+                       bucket++;
+                       continue;
+               }
+               lock_rsb(r);
+               if (is_master(r)) {
+                       grant_pending_locks(r);
+                       confirm_master(r, 0);
                }
-               read_unlock(&ls->ls_rsbtbl[i].lock);
+               unlock_rsb(r);
+               put_rsb(r);
+               schedule();
        }
-
-       return 0;
 }
 
 static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
@@ -3476,13 +3821,6 @@ static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
        lkb->lkb_bastaddr = (void *) (long) (rl->rl_asts & AST_BAST);
        lkb->lkb_astaddr = (void *) (long) (rl->rl_asts & AST_COMP);
 
-       if (lkb->lkb_flags & DLM_IFL_RANGE) {
-               lkb->lkb_range = allocate_range(ls);
-               if (!lkb->lkb_range)
-                       return -ENOMEM;
-               memcpy(lkb->lkb_range, rl->rl_range, 4*sizeof(uint64_t));
-       }
-
        if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
                lkb->lkb_lvbptr = allocate_lvb(ls);
                if (!lkb->lkb_lvbptr)
@@ -3541,7 +3879,7 @@ int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
 
        error = receive_rcom_lock_args(ls, lkb, r, rc);
        if (error) {
-               put_lkb(lkb);
+               __put_lkb(ls, lkb);
                goto out_unlock;
        }
 
@@ -3586,6 +3924,14 @@ int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
        lock_rsb(r);
 
        switch (error) {
+       case -EBADR:
+               /* There's a chance the new master received our lock before
+                  dlm_recover_master_reply(), this wouldn't happen if we did
+                  a barrier between recover_masters and recover_locks. */
+               log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
+                         (unsigned long)r, r->res_name);
+               dlm_send_rcom_lock(r, lkb);
+               goto out;
        case -EEXIST:
                log_debug(ls, "master copy exists %x", lkb->lkb_id);
                /* fall through */
@@ -3600,11 +3946,418 @@ int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
        /* an ack for dlm_recover_locks() which waits for replies from
           all the locks it sends to new masters */
        dlm_recovered_lock(r);
-
+ out:
        unlock_rsb(r);
        put_rsb(r);
-       put_lkb(lkb);
+       dlm_put_lkb(lkb);
 
        return 0;
 }
 
+int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
+                    int mode, uint32_t flags, void *name, unsigned int namelen,
+                    uint32_t parent_lkid)
+{
+       struct dlm_lkb *lkb;
+       struct dlm_args args;
+       int error;
+
+       lock_recovery(ls);
+
+       error = create_lkb(ls, &lkb);
+       if (error) {
+               kfree(ua);
+               goto out;
+       }
+
+       if (flags & DLM_LKF_VALBLK) {
+               ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_KERNEL);
+               if (!ua->lksb.sb_lvbptr) {
+                       kfree(ua);
+                       __put_lkb(ls, lkb);
+                       error = -ENOMEM;
+                       goto out;
+               }
+       }
+
+       /* After ua is attached to lkb it will be freed by free_lkb().
+          When DLM_IFL_USER is set, the dlm knows that this is a userspace
+          lock and that lkb_astparam is the dlm_user_args structure. */
+
+       error = set_lock_args(mode, &ua->lksb, flags, namelen, parent_lkid,
+                             DLM_FAKE_USER_AST, ua, DLM_FAKE_USER_AST, &args);
+       lkb->lkb_flags |= DLM_IFL_USER;
+       ua->old_mode = DLM_LOCK_IV;
+
+       if (error) {
+               __put_lkb(ls, lkb);
+               goto out;
+       }
+
+       error = request_lock(ls, lkb, name, namelen, &args);
+
+       switch (error) {
+       case 0:
+               break;
+       case -EINPROGRESS:
+               error = 0;
+               break;
+       case -EAGAIN:
+               error = 0;
+               /* fall through */
+       default:
+               __put_lkb(ls, lkb);
+               goto out;
+       }
+
+       /* add this new lkb to the per-process list of locks */
+       spin_lock(&ua->proc->locks_spin);
+       hold_lkb(lkb);
+       list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
+       spin_unlock(&ua->proc->locks_spin);
+ out:
+       unlock_recovery(ls);
+       return error;
+}
+
+int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
+                    int mode, uint32_t flags, uint32_t lkid, char *lvb_in)
+{
+       struct dlm_lkb *lkb;
+       struct dlm_args args;
+       struct dlm_user_args *ua;
+       int error;
+
+       lock_recovery(ls);
+
+       error = find_lkb(ls, lkid, &lkb);
+       if (error)
+               goto out;
+
+       /* user can change the params on its lock when it converts it, or
+          add an lvb that didn't exist before */
+
+       ua = (struct dlm_user_args *)lkb->lkb_astparam;
+
+       if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
+               ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_KERNEL);
+               if (!ua->lksb.sb_lvbptr) {
+                       error = -ENOMEM;
+                       goto out_put;
+               }
+       }
+       if (lvb_in && ua->lksb.sb_lvbptr)
+               memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
+
+       ua->castparam = ua_tmp->castparam;
+       ua->castaddr = ua_tmp->castaddr;
+       ua->bastparam = ua_tmp->bastparam;
+       ua->bastaddr = ua_tmp->bastaddr;
+       ua->user_lksb = ua_tmp->user_lksb;
+       ua->old_mode = lkb->lkb_grmode;
+
+       error = set_lock_args(mode, &ua->lksb, flags, 0, 0, DLM_FAKE_USER_AST,
+                             ua, DLM_FAKE_USER_AST, &args);
+       if (error)
+               goto out_put;
+
+       error = convert_lock(ls, lkb, &args);
+
+       if (error == -EINPROGRESS || error == -EAGAIN)
+               error = 0;
+ out_put:
+       dlm_put_lkb(lkb);
+ out:
+       unlock_recovery(ls);
+       kfree(ua_tmp);
+       return error;
+}
+
+int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
+                   uint32_t flags, uint32_t lkid, char *lvb_in)
+{
+       struct dlm_lkb *lkb;
+       struct dlm_args args;
+       struct dlm_user_args *ua;
+       int error;
+
+       lock_recovery(ls);
+
+       error = find_lkb(ls, lkid, &lkb);
+       if (error)
+               goto out;
+
+       ua = (struct dlm_user_args *)lkb->lkb_astparam;
+
+       if (lvb_in && ua->lksb.sb_lvbptr)
+               memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
+       ua->castparam = ua_tmp->castparam;
+       ua->user_lksb = ua_tmp->user_lksb;
+
+       error = set_unlock_args(flags, ua, &args);
+       if (error)
+               goto out_put;
+
+       error = unlock_lock(ls, lkb, &args);
+
+       if (error == -DLM_EUNLOCK)
+               error = 0;
+       /* from validate_unlock_args() */
+       if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
+               error = 0;
+       if (error)
+               goto out_put;
+
+       spin_lock(&ua->proc->locks_spin);
+       /* dlm_user_add_ast() may have already taken lkb off the proc list */
+       if (!list_empty(&lkb->lkb_ownqueue))
+               list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
+       spin_unlock(&ua->proc->locks_spin);
+ out_put:
+       dlm_put_lkb(lkb);
+ out:
+       unlock_recovery(ls);
+       kfree(ua_tmp);
+       return error;
+}
+
+int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
+                   uint32_t flags, uint32_t lkid)
+{
+       struct dlm_lkb *lkb;
+       struct dlm_args args;
+       struct dlm_user_args *ua;
+       int error;
+
+       lock_recovery(ls);
+
+       error = find_lkb(ls, lkid, &lkb);
+       if (error)
+               goto out;
+
+       ua = (struct dlm_user_args *)lkb->lkb_astparam;
+       ua->castparam = ua_tmp->castparam;
+       ua->user_lksb = ua_tmp->user_lksb;
+
+       error = set_unlock_args(flags, ua, &args);
+       if (error)
+               goto out_put;
+
+       error = cancel_lock(ls, lkb, &args);
+
+       if (error == -DLM_ECANCEL)
+               error = 0;
+       /* from validate_unlock_args() */
+       if (error == -EBUSY)
+               error = 0;
+ out_put:
+       dlm_put_lkb(lkb);
+ out:
+       unlock_recovery(ls);
+       kfree(ua_tmp);
+       return error;
+}
+
+/* lkb's that are removed from the waiters list by revert are just left on the
+   orphans list with the granted orphan locks, to be freed by purge */
+
+static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
+{
+       struct dlm_user_args *ua = (struct dlm_user_args *)lkb->lkb_astparam;
+       struct dlm_args args;
+       int error;
+
+       hold_lkb(lkb);
+       mutex_lock(&ls->ls_orphans_mutex);
+       list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
+       mutex_unlock(&ls->ls_orphans_mutex);
+
+       set_unlock_args(0, ua, &args);
+
+       error = cancel_lock(ls, lkb, &args);
+       if (error == -DLM_ECANCEL)
+               error = 0;
+       return error;
+}
+
+/* The force flag allows the unlock to go ahead even if the lkb isn't granted.
+   Regardless of what rsb queue the lock is on, it's removed and freed. */
+
+static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
+{
+       struct dlm_user_args *ua = (struct dlm_user_args *)lkb->lkb_astparam;
+       struct dlm_args args;
+       int error;
+
+       set_unlock_args(DLM_LKF_FORCEUNLOCK, ua, &args);
+
+       error = unlock_lock(ls, lkb, &args);
+       if (error == -DLM_EUNLOCK)
+               error = 0;
+       return error;
+}
+
+/* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
+   (which does lock_rsb) due to deadlock with receiving a message that does
+   lock_rsb followed by dlm_user_add_ast() */
+
+static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
+                                    struct dlm_user_proc *proc)
+{
+       struct dlm_lkb *lkb = NULL;
+
+       mutex_lock(&ls->ls_clear_proc_locks);
+       if (list_empty(&proc->locks))
+               goto out;
+
+       lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
+       list_del_init(&lkb->lkb_ownqueue);
+
+       if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
+               lkb->lkb_flags |= DLM_IFL_ORPHAN;
+       else
+               lkb->lkb_flags |= DLM_IFL_DEAD;
+ out:
+       mutex_unlock(&ls->ls_clear_proc_locks);
+       return lkb;
+}
+
+/* The ls_clear_proc_locks mutex protects against dlm_user_add_asts() which
+   1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
+   which we clear here. */
+
+/* proc CLOSING flag is set so no more device_reads should look at proc->asts
+   list, and no more device_writes should add lkb's to proc->locks list; so we
+   shouldn't need to take asts_spin or locks_spin here.  this assumes that
+   device reads/writes/closes are serialized -- FIXME: we may need to serialize
+   them ourself. */
+
+void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
+{
+       struct dlm_lkb *lkb, *safe;
+
+       lock_recovery(ls);
+
+       while (1) {
+               lkb = del_proc_lock(ls, proc);
+               if (!lkb)
+                       break;
+               if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
+                       orphan_proc_lock(ls, lkb);
+               else
+                       unlock_proc_lock(ls, lkb);
+
+               /* this removes the reference for the proc->locks list
+                  added by dlm_user_request, it may result in the lkb
+                  being freed */
+
+               dlm_put_lkb(lkb);
+       }
+
+       mutex_lock(&ls->ls_clear_proc_locks);
+
+       /* in-progress unlocks */
+       list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
+               list_del_init(&lkb->lkb_ownqueue);
+               lkb->lkb_flags |= DLM_IFL_DEAD;
+               dlm_put_lkb(lkb);
+       }
+
+       list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
+               list_del(&lkb->lkb_astqueue);
+               dlm_put_lkb(lkb);
+       }
+
+       mutex_unlock(&ls->ls_clear_proc_locks);
+       unlock_recovery(ls);
+}
+
+static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
+{
+       struct dlm_lkb *lkb, *safe;
+
+       while (1) {
+               lkb = NULL;
+               spin_lock(&proc->locks_spin);
+               if (!list_empty(&proc->locks)) {
+                       lkb = list_entry(proc->locks.next, struct dlm_lkb,
+                                        lkb_ownqueue);
+                       list_del_init(&lkb->lkb_ownqueue);
+               }
+               spin_unlock(&proc->locks_spin);
+
+               if (!lkb)
+                       break;
+
+               lkb->lkb_flags |= DLM_IFL_DEAD;
+               unlock_proc_lock(ls, lkb);
+               dlm_put_lkb(lkb); /* ref from proc->locks list */
+       }
+
+       spin_lock(&proc->locks_spin);
+       list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
+               list_del_init(&lkb->lkb_ownqueue);
+               lkb->lkb_flags |= DLM_IFL_DEAD;
+               dlm_put_lkb(lkb);
+       }
+       spin_unlock(&proc->locks_spin);
+
+       spin_lock(&proc->asts_spin);
+       list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_astqueue) {
+               list_del(&lkb->lkb_astqueue);
+               dlm_put_lkb(lkb);
+       }
+       spin_unlock(&proc->asts_spin);
+}
+
+/* pid of 0 means purge all orphans */
+
+static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
+{
+       struct dlm_lkb *lkb, *safe;
+
+       mutex_lock(&ls->ls_orphans_mutex);
+       list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
+               if (pid && lkb->lkb_ownpid != pid)
+                       continue;
+               unlock_proc_lock(ls, lkb);
+               list_del_init(&lkb->lkb_ownqueue);
+               dlm_put_lkb(lkb);
+       }
+       mutex_unlock(&ls->ls_orphans_mutex);
+}
+
+static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
+{
+       struct dlm_message *ms;
+       struct dlm_mhandle *mh;
+       int error;
+
+       error = _create_message(ls, sizeof(struct dlm_message), nodeid,
+                               DLM_MSG_PURGE, &ms, &mh);
+       if (error)
+               return error;
+       ms->m_nodeid = nodeid;
+       ms->m_pid = pid;
+
+       return send_message(mh, ms);
+}
+
+int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
+                  int nodeid, int pid)
+{
+       int error = 0;
+
+       if (nodeid != dlm_our_nodeid()) {
+               error = send_purge(ls, nodeid, pid);
+       } else {
+               lock_recovery(ls);
+               if (pid == current->pid)
+                       purge_proc_locks(ls, proc);
+               else
+                       do_purge(ls, nodeid, pid);
+               unlock_recovery(ls);
+       }
+       return error;
+}
+