[2.6 patch] ocfs2: make dlm_do_assert_master() static
[safe/jmp/linux-2.6] / fs / ocfs2 / dlm / dlmmaster.c
index 953aa84..c92d1b1 100644 (file)
@@ -47,7 +47,6 @@
 
 #include "dlmapi.h"
 #include "dlmcommon.h"
-#include "dlmdebug.h"
 #include "dlmdomain.h"
 
 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
@@ -74,6 +73,7 @@ struct dlm_master_list_entry
        wait_queue_head_t wq;
        atomic_t woken;
        struct kref mle_refs;
+       int inuse;
        unsigned long maybe_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
        unsigned long vote_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
        unsigned long response_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
@@ -99,9 +99,10 @@ static void dlm_mle_node_up(struct dlm_ctxt *dlm,
                            int idx);
 
 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
-static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
-                               unsigned int namelen, void *nodemap,
-                               u32 flags);
+static int dlm_do_assert_master(struct dlm_ctxt *dlm,
+                               struct dlm_lock_resource *res,
+                               void *nodemap, u32 flags);
+static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
 
 static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
                                struct dlm_master_list_entry *mle,
@@ -127,18 +128,30 @@ static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
        return 1;
 }
 
-#if 0
-/* Code here is included but defined out as it aids debugging */
+#define dlm_print_nodemap(m)  _dlm_print_nodemap(m,#m)
+static void _dlm_print_nodemap(unsigned long *map, const char *mapname)
+{
+       int i;
+       printk("%s=[ ", mapname);
+       for (i=0; i<O2NM_MAX_NODES; i++)
+               if (test_bit(i, map))
+                       printk("%d ", i);
+       printk("]");
+}
 
-void dlm_print_one_mle(struct dlm_master_list_entry *mle)
+static void dlm_print_one_mle(struct dlm_master_list_entry *mle)
 {
-       int i = 0, refs;
+       int refs;
        char *type;
        char attached;
        u8 master;
        unsigned int namelen;
        const char *name;
        struct kref *k;
+       unsigned long *maybe = mle->maybe_map,
+                     *vote = mle->vote_map,
+                     *resp = mle->response_map,
+                     *node = mle->node_map;
 
        k = &mle->mle_refs;
        if (mle->type == DLM_MLE_BLOCK)
@@ -159,34 +172,40 @@ void dlm_print_one_mle(struct dlm_master_list_entry *mle)
                name = mle->u.res->lockname.name;
        }
 
-       mlog(ML_NOTICE, "  #%3d: %3s  %3d  %3u   %3u %c    (%d)%.*s\n",
-                 i, type, refs, master, mle->new_master, attached,
-                 namelen, namelen, name);
+       mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
+                 namelen, name, type, refs, master, mle->new_master, attached,
+                 mle->inuse);
+       dlm_print_nodemap(maybe);
+       printk(", ");
+       dlm_print_nodemap(vote);
+       printk(", ");
+       dlm_print_nodemap(resp);
+       printk(", ");
+       dlm_print_nodemap(node);
+       printk(", ");
+       printk("\n");
 }
 
+#if 0
+/* Code here is included but defined out as it aids debugging */
+
 static void dlm_dump_mles(struct dlm_ctxt *dlm)
 {
        struct dlm_master_list_entry *mle;
-       struct list_head *iter;
        
        mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
-       mlog(ML_NOTICE, "  ####: type refs owner new events? lockname nodemap votemap respmap maybemap\n");
        spin_lock(&dlm->master_lock);
-       list_for_each(iter, &dlm->master_list) {
-               mle = list_entry(iter, struct dlm_master_list_entry, list);
+       list_for_each_entry(mle, &dlm->master_list, list)
                dlm_print_one_mle(mle);
-       }
        spin_unlock(&dlm->master_lock);
 }
 
 int dlm_dump_all_mles(const char __user *data, unsigned int len)
 {
-       struct list_head *iter;
        struct dlm_ctxt *dlm;
 
        spin_lock(&dlm_domain_lock);
-       list_for_each(iter, &dlm_domains) {
-               dlm = list_entry (iter, struct dlm_ctxt, list);
+       list_for_each_entry(dlm, &dlm_domains, list) {
                mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
                dlm_dump_mles(dlm);
        }
@@ -198,7 +217,7 @@ EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
 #endif  /*  0  */
 
 
-static kmem_cache_t *dlm_mle_cache = NULL;
+static struct kmem_cache *dlm_mle_cache = NULL;
 
 
 static void dlm_mle_release(struct kref *kref);
@@ -214,7 +233,8 @@ static int dlm_find_mle(struct dlm_ctxt *dlm,
                        struct dlm_master_list_entry **mle,
                        char *name, unsigned int namelen);
 
-static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to);
+static int dlm_do_master_request(struct dlm_lock_resource *res,
+                                struct dlm_master_list_entry *mle, int to);
 
 
 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
@@ -314,6 +334,31 @@ static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
        spin_unlock(&dlm->spinlock);
 }
 
+static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
+{
+       struct dlm_ctxt *dlm;
+       dlm = mle->dlm;
+
+       assert_spin_locked(&dlm->spinlock);
+       assert_spin_locked(&dlm->master_lock);
+       mle->inuse++;
+       kref_get(&mle->mle_refs);
+}
+
+static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
+{
+       struct dlm_ctxt *dlm;
+       dlm = mle->dlm;
+
+       spin_lock(&dlm->spinlock);
+       spin_lock(&dlm->master_lock);
+       mle->inuse--;
+       __dlm_put_mle(mle);
+       spin_unlock(&dlm->master_lock);
+       spin_unlock(&dlm->spinlock);
+
+}
+
 /* remove from list and free */
 static void __dlm_put_mle(struct dlm_master_list_entry *mle)
 {
@@ -322,9 +367,14 @@ static void __dlm_put_mle(struct dlm_master_list_entry *mle)
 
        assert_spin_locked(&dlm->spinlock);
        assert_spin_locked(&dlm->master_lock);
-       BUG_ON(!atomic_read(&mle->mle_refs.refcount));
-
-       kref_put(&mle->mle_refs, dlm_mle_release);
+       if (!atomic_read(&mle->mle_refs.refcount)) {
+               /* this may or may not crash, but who cares.
+                * it's a BUG. */
+               mlog(ML_ERROR, "bad mle: %p\n", mle);
+               dlm_print_one_mle(mle);
+               BUG();
+       } else
+               kref_put(&mle->mle_refs, dlm_mle_release);
 }
 
 
@@ -367,6 +417,7 @@ static void dlm_init_mle(struct dlm_master_list_entry *mle,
        memset(mle->response_map, 0, sizeof(mle->response_map));
        mle->master = O2NM_MAX_NODES;
        mle->new_master = O2NM_MAX_NODES;
+       mle->inuse = 0;
 
        if (mle->type == DLM_MLE_MASTER) {
                BUG_ON(!res);
@@ -398,12 +449,10 @@ static int dlm_find_mle(struct dlm_ctxt *dlm,
                        char *name, unsigned int namelen)
 {
        struct dlm_master_list_entry *tmpmle;
-       struct list_head *iter;
 
        assert_spin_locked(&dlm->master_lock);
 
-       list_for_each(iter, &dlm->master_list) {
-               tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
+       list_for_each_entry(tmpmle, &dlm->master_list, list) {
                if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
                        continue;
                dlm_get_mle(tmpmle);
@@ -416,13 +465,10 @@ static int dlm_find_mle(struct dlm_ctxt *dlm,
 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
 {
        struct dlm_master_list_entry *mle;
-       struct list_head *iter;
 
        assert_spin_locked(&dlm->spinlock);
        
-       list_for_each(iter, &dlm->mle_hb_events) {
-               mle = list_entry(iter, struct dlm_master_list_entry, 
-                                hb_events);
+       list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) {
                if (node_up)
                        dlm_mle_node_up(dlm, mle, NULL, idx);
                else
@@ -464,7 +510,7 @@ int dlm_init_mle_cache(void)
        dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
                                          sizeof(struct dlm_master_list_entry),
                                          0, SLAB_HWCACHE_ALIGN,
-                                         NULL, NULL);
+                                         NULL);
        if (dlm_mle_cache == NULL)
                return -ENOMEM;
        return 0;
@@ -564,6 +610,28 @@ static void dlm_lockres_release(struct kref *kref)
        mlog(0, "destroying lockres %.*s\n", res->lockname.len,
             res->lockname.name);
 
+       if (!hlist_unhashed(&res->hash_node) ||
+           !list_empty(&res->granted) ||
+           !list_empty(&res->converting) ||
+           !list_empty(&res->blocked) ||
+           !list_empty(&res->dirty) ||
+           !list_empty(&res->recovering) ||
+           !list_empty(&res->purge)) {
+               mlog(ML_ERROR,
+                    "Going to BUG for resource %.*s."
+                    "  We're on a list! [%c%c%c%c%c%c%c]\n",
+                    res->lockname.len, res->lockname.name,
+                    !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
+                    !list_empty(&res->granted) ? 'G' : ' ',
+                    !list_empty(&res->converting) ? 'C' : ' ',
+                    !list_empty(&res->blocked) ? 'B' : ' ',
+                    !list_empty(&res->dirty) ? 'D' : ' ',
+                    !list_empty(&res->recovering) ? 'R' : ' ',
+                    !list_empty(&res->purge) ? 'P' : ' ');
+
+               dlm_print_one_lock_resource(res);
+       }
+
        /* By the time we're ready to blow this guy away, we shouldn't
         * be on any lists. */
        BUG_ON(!hlist_unhashed(&res->hash_node));
@@ -579,11 +647,6 @@ static void dlm_lockres_release(struct kref *kref)
        kfree(res);
 }
 
-void dlm_lockres_get(struct dlm_lock_resource *res)
-{
-       kref_get(&res->refs);
-}
-
 void dlm_lockres_put(struct dlm_lock_resource *res)
 {
        kref_put(&res->refs, dlm_lockres_release);
@@ -616,6 +679,7 @@ static void dlm_init_lockres(struct dlm_ctxt *dlm,
        INIT_LIST_HEAD(&res->purge);
        atomic_set(&res->asts_reserved, 0);
        res->migration_pending = 0;
+       res->inflight_locks = 0;
 
        kref_init(&res->refs);
 
@@ -629,6 +693,7 @@ static void dlm_init_lockres(struct dlm_ctxt *dlm,
        res->last_used = 0;
 
        memset(res->lvb, 0, DLM_LVB_LEN);
+       memset(res->refmap, 0, sizeof(res->refmap));
 }
 
 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
@@ -637,11 +702,11 @@ struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
 {
        struct dlm_lock_resource *res;
 
-       res = kmalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL);
+       res = kmalloc(sizeof(struct dlm_lock_resource), GFP_NOFS);
        if (!res)
                return NULL;
 
-       res->lockname.name = kmalloc(namelen, GFP_KERNEL);
+       res->lockname.name = kmalloc(namelen, GFP_NOFS);
        if (!res->lockname.name) {
                kfree(res);
                return NULL;
@@ -651,6 +716,42 @@ struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
        return res;
 }
 
+void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
+                                  struct dlm_lock_resource *res,
+                                  int new_lockres,
+                                  const char *file,
+                                  int line)
+{
+       if (!new_lockres)
+               assert_spin_locked(&res->spinlock);
+
+       if (!test_bit(dlm->node_num, res->refmap)) {
+               BUG_ON(res->inflight_locks != 0);
+               dlm_lockres_set_refmap_bit(dlm->node_num, res);
+       }
+       res->inflight_locks++;
+       mlog(0, "%s:%.*s: inflight++: now %u\n",
+            dlm->name, res->lockname.len, res->lockname.name,
+            res->inflight_locks);
+}
+
+void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
+                                  struct dlm_lock_resource *res,
+                                  const char *file,
+                                  int line)
+{
+       assert_spin_locked(&res->spinlock);
+
+       BUG_ON(res->inflight_locks == 0);
+       res->inflight_locks--;
+       mlog(0, "%s:%.*s: inflight--: now %u\n",
+            dlm->name, res->lockname.len, res->lockname.name,
+            res->inflight_locks);
+       if (res->inflight_locks == 0)
+               dlm_lockres_clear_refmap_bit(dlm->node_num, res);
+       wake_up(&res->wq);
+}
+
 /*
  * lookup a lock resource by name.
  * may already exist in the hashtable.
@@ -669,6 +770,7 @@ struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
  */
 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
                                          const char *lockid,
+                                         int namelen,
                                          int flags)
 {
        struct dlm_lock_resource *tmpres=NULL, *res=NULL;
@@ -677,22 +779,43 @@ struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
        int blocked = 0;
        int ret, nodenum;
        struct dlm_node_iter iter;
-       unsigned int namelen, hash;
+       unsigned int hash;
        int tries = 0;
        int bit, wait_on_recovery = 0;
+       int drop_inflight_if_nonlocal = 0;
 
        BUG_ON(!lockid);
 
-       namelen = strlen(lockid);
        hash = dlm_lockid_hash(lockid, namelen);
 
        mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
 
 lookup:
        spin_lock(&dlm->spinlock);
-       tmpres = __dlm_lookup_lockres(dlm, lockid, namelen, hash);
+       tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
        if (tmpres) {
+               int dropping_ref = 0;
+
+               spin_lock(&tmpres->spinlock);
+               if (tmpres->owner == dlm->node_num) {
+                       BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
+                       dlm_lockres_grab_inflight_ref(dlm, tmpres);
+               } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
+                       dropping_ref = 1;
+               spin_unlock(&tmpres->spinlock);
                spin_unlock(&dlm->spinlock);
+
+               /* wait until done messaging the master, drop our ref to allow
+                * the lockres to be purged, start over. */
+               if (dropping_ref) {
+                       spin_lock(&tmpres->spinlock);
+                       __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
+                       spin_unlock(&tmpres->spinlock);
+                       dlm_lockres_put(tmpres);
+                       tmpres = NULL;
+                       goto lookup;
+               }
+
                mlog(0, "found in hash!\n");
                if (res)
                        dlm_lockres_put(res);
@@ -705,7 +828,7 @@ lookup:
                mlog(0, "allocating a new resource\n");
                /* nothing found and we need to allocate one. */
                alloc_mle = (struct dlm_master_list_entry *)
-                       kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
+                       kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
                if (!alloc_mle)
                        goto leave;
                res = dlm_new_lockres(dlm, lockid, namelen);
@@ -722,6 +845,7 @@ lookup:
                spin_lock(&res->spinlock);
                dlm_change_lockres_owner(dlm, res, dlm->node_num);
                __dlm_insert_lockres(dlm, res);
+               dlm_lockres_grab_inflight_ref(dlm, res);
                spin_unlock(&res->spinlock);
                spin_unlock(&dlm->spinlock);
                /* lockres still marked IN_PROGRESS */
@@ -734,29 +858,40 @@ lookup:
        /* if we found a block, wait for lock to be mastered by another node */
        blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
        if (blocked) {
+               int mig;
                if (mle->type == DLM_MLE_MASTER) {
                        mlog(ML_ERROR, "master entry for nonexistent lock!\n");
                        BUG();
-               } else if (mle->type == DLM_MLE_MIGRATION) {
-                       /* migration is in progress! */
-                       /* the good news is that we now know the
-                        * "current" master (mle->master). */
-
+               }
+               mig = (mle->type == DLM_MLE_MIGRATION);
+               /* if there is a migration in progress, let the migration
+                * finish before continuing.  we can wait for the absence
+                * of the MIGRATION mle: either the migrate finished or
+                * one of the nodes died and the mle was cleaned up.
+                * if there is a BLOCK here, but it already has a master
+                * set, we are too late.  the master does not have a ref
+                * for us in the refmap.  detach the mle and drop it.
+                * either way, go back to the top and start over. */
+               if (mig || mle->master != O2NM_MAX_NODES) {
+                       BUG_ON(mig && mle->master == dlm->node_num);
+                       /* we arrived too late.  the master does not
+                        * have a ref for us. retry. */
+                       mlog(0, "%s:%.*s: late on %s\n",
+                            dlm->name, namelen, lockid,
+                            mig ?  "MIGRATION" : "BLOCK");
                        spin_unlock(&dlm->master_lock);
-                       assert_spin_locked(&dlm->spinlock);
-
-                       /* set the lockres owner and hash it */
-                       spin_lock(&res->spinlock);
-                       dlm_set_lockres_owner(dlm, res, mle->master);
-                       __dlm_insert_lockres(dlm, res);
-                       spin_unlock(&res->spinlock);
                        spin_unlock(&dlm->spinlock);
 
                        /* master is known, detach */
-                       dlm_mle_detach_hb_events(dlm, mle);
+                       if (!mig)
+                               dlm_mle_detach_hb_events(dlm, mle);
                        dlm_put_mle(mle);
                        mle = NULL;
-                       goto wake_waiters;
+                       /* this is lame, but we cant wait on either
+                        * the mle or lockres waitqueue here */
+                       if (mig)
+                               msleep(100);
+                       goto lookup;
                }
        } else {
                /* go ahead and try to master lock on this node */
@@ -773,7 +908,7 @@ lookup:
                 * but they might own this lockres.  wait on them. */
                bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
                if (bit < O2NM_MAX_NODES) {
-                       mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
+                       mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
                             "recover before lock mastery can begin\n",
                             dlm->name, namelen, (char *)lockid, bit);
                        wait_on_recovery = 1;
@@ -787,14 +922,22 @@ lookup:
 
        /* finally add the lockres to its hash bucket */
        __dlm_insert_lockres(dlm, res);
+       /* since this lockres is new it doesnt not require the spinlock */
+       dlm_lockres_grab_inflight_ref_new(dlm, res);
+
+       /* if this node does not become the master make sure to drop
+        * this inflight reference below */
+       drop_inflight_if_nonlocal = 1;
+
        /* get an extra ref on the mle in case this is a BLOCK
         * if so, the creator of the BLOCK may try to put the last
         * ref at this time in the assert master handler, so we
         * need an extra one to keep from a bad ptr deref. */
-       dlm_get_mle(mle);
+       dlm_get_mle_inuse(mle);
        spin_unlock(&dlm->master_lock);
        spin_unlock(&dlm->spinlock);
 
+redo_request:
        while (wait_on_recovery) {
                /* any cluster changes that occurred after dropping the
                 * dlm spinlock would be detectable be a change on the mle,
@@ -813,30 +956,32 @@ lookup:
                } 
 
                dlm_kick_recovery_thread(dlm);
-               msleep(100);
+               msleep(1000);
                dlm_wait_for_recovery(dlm);
 
                spin_lock(&dlm->spinlock);
                bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
                if (bit < O2NM_MAX_NODES) {
-                       mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
+                       mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
                             "recover before lock mastery can begin\n",
                             dlm->name, namelen, (char *)lockid, bit);
                        wait_on_recovery = 1;
                } else
                        wait_on_recovery = 0;
                spin_unlock(&dlm->spinlock);
+
+               if (wait_on_recovery)
+                       dlm_wait_for_node_recovery(dlm, bit, 10000);
        }
 
        /* must wait for lock to be mastered elsewhere */
        if (blocked)
                goto wait;
 
-redo_request:
        ret = -EINVAL;
        dlm_node_iter_init(mle->vote_map, &iter);
        while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
-               ret = dlm_do_master_request(mle, nodenum);
+               ret = dlm_do_master_request(res, mle, nodenum);
                if (ret < 0)
                        mlog_errno(ret);
                if (mle->master != O2NM_MAX_NODES) {
@@ -857,6 +1002,7 @@ wait:
        /* keep going until the response map includes all nodes */
        ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
        if (ret < 0) {
+               wait_on_recovery = 1;
                mlog(0, "%s:%.*s: node map changed, redo the "
                     "master request now, blocked=%d\n",
                     dlm->name, res->lockname.len,
@@ -867,7 +1013,7 @@ wait:
                             dlm->name, res->lockname.len, 
                             res->lockname.name, blocked);
                        dlm_print_one_lock_resource(res);
-                       /* dlm_print_one_mle(mle); */
+                       dlm_print_one_mle(mle);
                        tries = 0;
                }
                goto redo_request;
@@ -881,10 +1027,12 @@ wait:
        dlm_mle_detach_hb_events(dlm, mle);
        dlm_put_mle(mle);
        /* put the extra ref */
-       dlm_put_mle(mle);
+       dlm_put_mle_inuse(mle);
 
 wake_waiters:
        spin_lock(&res->spinlock);
+       if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
+               dlm_lockres_drop_inflight_ref(dlm, res);
        res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
        spin_unlock(&res->spinlock);
        wake_up(&res->wq);
@@ -922,12 +1070,14 @@ recheck:
                spin_unlock(&res->spinlock);
                /* this will cause the master to re-assert across
                 * the whole cluster, freeing up mles */
-               ret = dlm_do_master_request(mle, res->owner);
-               if (ret < 0) {
-                       /* give recovery a chance to run */
-                       mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
-                       msleep(500);
-                       goto recheck;
+               if (res->owner != dlm->node_num) {
+                       ret = dlm_do_master_request(res, mle, res->owner);
+                       if (ret < 0) {
+                               /* give recovery a chance to run */
+                               mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
+                               msleep(500);
+                               goto recheck;
+                       }
                }
                ret = 0;
                goto leave;
@@ -963,6 +1113,12 @@ recheck:
                     "rechecking now\n", dlm->name, res->lockname.len,
                     res->lockname.name);
                goto recheck;
+       } else {
+               if (!voting_done) {
+                       mlog(0, "map not changed and voting not done "
+                            "for %s:%.*s\n", dlm->name, res->lockname.len,
+                            res->lockname.name);
+               }
        }
 
        if (m != O2NM_MAX_NODES) {
@@ -979,6 +1135,8 @@ recheck:
                                 * now tell other nodes that I am
                                 * mastering this. */
                                mle->master = dlm->node_num;
+                               /* ref was grabbed in get_lock_resource
+                                * will be dropped in dlmlock_master */
                                assert = 1;
                                sleep = 0;
                        }
@@ -1004,7 +1162,8 @@ recheck:
                                         (atomic_read(&mle->woken) == 1),
                                         timeo);
                if (res->owner == O2NM_MAX_NODES) {
-                       mlog(0, "waiting again\n");
+                       mlog(0, "%s:%.*s: waiting again\n", dlm->name,
+                            res->lockname.len, res->lockname.name);
                        goto recheck;
                }
                mlog(0, "done waiting, master is %u\n", res->owner);
@@ -1017,8 +1176,7 @@ recheck:
                m = dlm->node_num;
                mlog(0, "about to master %.*s here, this=%u\n",
                     res->lockname.len, res->lockname.name, m);
-               ret = dlm_do_assert_master(dlm, res->lockname.name,
-                                          res->lockname.len, mle->vote_map, 0);
+               ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
                if (ret) {
                        /* This is a failure in the network path,
                         * not in the response to the assert_master
@@ -1034,6 +1192,8 @@ recheck:
 
        /* set the lockres owner */
        spin_lock(&res->spinlock);
+       /* mastery reference obtained either during
+        * assert_master_handler or in get_lock_resource */
        dlm_change_lockres_owner(dlm, res, m);
        spin_unlock(&res->spinlock);
 
@@ -1130,18 +1290,6 @@ static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
                        set_bit(node, mle->vote_map);
                } else {
                        mlog(ML_ERROR, "node down! %d\n", node);
-
-                       /* if the node wasn't involved in mastery skip it,
-                        * but clear it out from the maps so that it will
-                        * not affect mastery of this lockres */
-                       clear_bit(node, mle->response_map);
-                       clear_bit(node, mle->vote_map);
-                       if (!test_bit(node, mle->maybe_map))
-                               goto next;
-
-                       /* if we're already blocked on lock mastery, and the
-                        * dead node wasn't the expected master, or there is
-                        * another node in the maybe_map, keep waiting */
                        if (blocked) {
                                int lowest = find_next_bit(mle->maybe_map,
                                                       O2NM_MAX_NODES, 0);
@@ -1149,54 +1297,53 @@ static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
                                /* act like it was never there */
                                clear_bit(node, mle->maybe_map);
 
-                               if (node != lowest)
-                                       goto next;
-
-                               mlog(ML_ERROR, "expected master %u died while "
-                                    "this node was blocked waiting on it!\n",
-                                    node);
-                               lowest = find_next_bit(mle->maybe_map,
-                                                      O2NM_MAX_NODES,
-                                                      lowest+1);
-                               if (lowest < O2NM_MAX_NODES) {
-                                       mlog(0, "still blocked. waiting "
-                                            "on %u now\n", lowest);
-                                       goto next;
+                               if (node == lowest) {
+                                       mlog(0, "expected master %u died"
+                                           " while this node was blocked "
+                                           "waiting on it!\n", node);
+                                       lowest = find_next_bit(mle->maybe_map,
+                                                       O2NM_MAX_NODES,
+                                                       lowest+1);
+                                       if (lowest < O2NM_MAX_NODES) {
+                                               mlog(0, "%s:%.*s:still "
+                                                    "blocked. waiting on %u "
+                                                    "now\n", dlm->name,
+                                                    res->lockname.len,
+                                                    res->lockname.name,
+                                                    lowest);
+                                       } else {
+                                               /* mle is an MLE_BLOCK, but
+                                                * there is now nothing left to
+                                                * block on.  we need to return
+                                                * all the way back out and try
+                                                * again with an MLE_MASTER.
+                                                * dlm_do_local_recovery_cleanup
+                                                * has already run, so the mle
+                                                * refcount is ok */
+                                               mlog(0, "%s:%.*s: no "
+                                                    "longer blocking. try to "
+                                                    "master this here\n",
+                                                    dlm->name,
+                                                    res->lockname.len,
+                                                    res->lockname.name);
+                                               mle->type = DLM_MLE_MASTER;
+                                               mle->u.res = res;
+                                       }
                                }
-
-                               /* mle is an MLE_BLOCK, but there is now
-                                * nothing left to block on.  we need to return
-                                * all the way back out and try again with
-                                * an MLE_MASTER. dlm_do_local_recovery_cleanup
-                                * has already run, so the mle refcount is ok */
-                               mlog(0, "no longer blocking. we can "
-                                    "try to master this here\n");
-                               mle->type = DLM_MLE_MASTER;
-                               memset(mle->maybe_map, 0,
-                                      sizeof(mle->maybe_map));
-                               memset(mle->response_map, 0,
-                                      sizeof(mle->maybe_map));
-                               memcpy(mle->vote_map, mle->node_map,
-                                      sizeof(mle->node_map));
-                               mle->u.res = res;
-                               set_bit(dlm->node_num, mle->maybe_map);
-
-                               ret = -EAGAIN;
-                               goto next;
                        }
 
-                       clear_bit(node, mle->maybe_map);
-                       if (node > dlm->node_num)
-                               goto next;
-
-                       mlog(0, "dead node in map!\n");
-                       /* yuck. go back and re-contact all nodes
-                        * in the vote_map, removing this node. */
-                       memset(mle->response_map, 0,
-                              sizeof(mle->response_map));
+                       /* now blank out everything, as if we had never
+                        * contacted anyone */
+                       memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
+                       memset(mle->response_map, 0, sizeof(mle->response_map));
+                       /* reset the vote_map to the current node_map */
+                       memcpy(mle->vote_map, mle->node_map,
+                              sizeof(mle->node_map));
+                       /* put myself into the maybe map */
+                       if (mle->type != DLM_MLE_BLOCK)
+                               set_bit(dlm->node_num, mle->maybe_map);
                }
                ret = -EAGAIN;
-next:
                node = dlm_bitmap_diff_iter_next(&bdi, &sc);
        }
        return ret;
@@ -1213,7 +1360,8 @@ next:
  *
  */
 
-static int dlm_do_master_request(struct dlm_master_list_entry *mle, int to)
+static int dlm_do_master_request(struct dlm_lock_resource *res,
+                                struct dlm_master_list_entry *mle, int to)
 {
        struct dlm_ctxt *dlm = mle->dlm;
        struct dlm_master_request request;
@@ -1269,6 +1417,9 @@ again:
                case DLM_MASTER_RESP_YES:
                        set_bit(to, mle->response_map);
                        mlog(0, "node %u is the master, response=YES\n", to);
+                       mlog(0, "%s:%.*s: master node %u now knows I have a "
+                            "reference\n", dlm->name, res->lockname.len,
+                            res->lockname.name, to);
                        mle->master = to;
                        break;
                case DLM_MASTER_RESP_NO:
@@ -1309,7 +1460,8 @@ out:
  *
  * if possible, TRIM THIS DOWN!!!
  */
-int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data)
+int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
+                              void **ret_data)
 {
        u8 response = DLM_MASTER_RESP_MAYBE;
        struct dlm_ctxt *dlm = data;
@@ -1347,10 +1499,11 @@ way_up_top:
 
                /* take care of the easy cases up front */
                spin_lock(&res->spinlock);
-               if (res->state & DLM_LOCK_RES_RECOVERING) {
+               if (res->state & (DLM_LOCK_RES_RECOVERING|
+                                 DLM_LOCK_RES_MIGRATING)) {
                        spin_unlock(&res->spinlock);
                        mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
-                            "being recovered\n");
+                            "being recovered/migrated\n");
                        response = DLM_MASTER_RESP_ERROR;
                        if (mle)
                                kmem_cache_free(dlm_mle_cache, mle);
@@ -1358,8 +1511,10 @@ way_up_top:
                }
 
                if (res->owner == dlm->node_num) {
+                       mlog(0, "%s:%.*s: setting bit %u in refmap\n",
+                            dlm->name, namelen, name, request->node_idx);
+                       dlm_lockres_set_refmap_bit(request->node_idx, res);
                        spin_unlock(&res->spinlock);
-                       // mlog(0, "this node is the master\n");
                        response = DLM_MASTER_RESP_YES;
                        if (mle)
                                kmem_cache_free(dlm_mle_cache, mle);
@@ -1407,7 +1562,6 @@ way_up_top:
                        mlog(0, "node %u is master, but trying to migrate to "
                             "node %u.\n", tmpmle->master, tmpmle->new_master);
                        if (tmpmle->master == dlm->node_num) {
-                               response = DLM_MASTER_RESP_YES;
                                mlog(ML_ERROR, "no owner on lockres, but this "
                                     "node is trying to migrate it to %u?!\n",
                                     tmpmle->new_master);
@@ -1424,6 +1578,10 @@ way_up_top:
                                 * go back and clean the mles on any
                                 * other nodes */
                                dispatch_assert = 1;
+                               dlm_lockres_set_refmap_bit(request->node_idx, res);
+                               mlog(0, "%s:%.*s: setting bit %u in refmap\n",
+                                    dlm->name, namelen, name,
+                                    request->node_idx);
                        } else
                                response = DLM_MASTER_RESP_NO;
                } else {
@@ -1461,21 +1619,18 @@ way_up_top:
                        spin_unlock(&dlm->spinlock);
 
                        mle = (struct dlm_master_list_entry *)
-                               kmem_cache_alloc(dlm_mle_cache, GFP_KERNEL);
+                               kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
                        if (!mle) {
                                response = DLM_MASTER_RESP_ERROR;
                                mlog_errno(-ENOMEM);
                                goto send_response;
                        }
-                       spin_lock(&dlm->spinlock);
-                       dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL,
-                                        name, namelen);
-                       spin_unlock(&dlm->spinlock);
                        goto way_up_top;
                }
 
                // mlog(0, "this is second time thru, already allocated, "
                // "add the block.\n");
+               dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
                set_bit(request->node_idx, mle->maybe_map);
                list_add(&mle->list, &dlm->master_list);
                response = DLM_MASTER_RESP_NO;
@@ -1540,17 +1695,24 @@ send_response:
  * can periodically run all locks owned by this node
  * and re-assert across the cluster...
  */
-static int dlm_do_assert_master(struct dlm_ctxt *dlm, const char *lockname,
-                               unsigned int namelen, void *nodemap,
-                               u32 flags)
+static int dlm_do_assert_master(struct dlm_ctxt *dlm,
+                               struct dlm_lock_resource *res,
+                               void *nodemap, u32 flags)
 {
        struct dlm_assert_master assert;
        int to, tmpret;
        struct dlm_node_iter iter;
        int ret = 0;
        int reassert;
+       const char *lockname = res->lockname.name;
+       unsigned int namelen = res->lockname.len;
 
        BUG_ON(namelen > O2NM_MAX_NAME_LEN);
+
+       spin_lock(&res->spinlock);
+       res->state |= DLM_LOCK_RES_SETREF_INPROG;
+       spin_unlock(&res->spinlock);
+
 again:
        reassert = 0;
 
@@ -1558,6 +1720,8 @@ again:
        dlm_node_iter_init(nodemap, &iter);
        while ((to = dlm_node_iter_next(&iter)) >= 0) {
                int r = 0;
+               struct dlm_master_list_entry *mle = NULL;
+
                mlog(0, "sending assert master to %d (%.*s)\n", to,
                     namelen, lockname);
                memset(&assert, 0, sizeof(assert));
@@ -1569,32 +1733,63 @@ again:
                tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
                                            &assert, sizeof(assert), to, &r);
                if (tmpret < 0) {
-                       mlog(ML_ERROR, "assert_master returned %d!\n", tmpret);
+                       mlog(0, "assert_master returned %d!\n", tmpret);
                        if (!dlm_is_host_down(tmpret)) {
-                               mlog(ML_ERROR, "unhandled error!\n");
+                               mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
                                BUG();
                        }
                        /* a node died.  finish out the rest of the nodes. */
-                       mlog(ML_ERROR, "link to %d went down!\n", to);
+                       mlog(0, "link to %d went down!\n", to);
                        /* any nonzero status return will do */
                        ret = tmpret;
+                       r = 0;
                } else if (r < 0) {
                        /* ok, something horribly messed.  kill thyself. */
                        mlog(ML_ERROR,"during assert master of %.*s to %u, "
                             "got %d.\n", namelen, lockname, to, r);
-                       dlm_dump_lock_resources(dlm);
+                       spin_lock(&dlm->spinlock);
+                       spin_lock(&dlm->master_lock);
+                       if (dlm_find_mle(dlm, &mle, (char *)lockname,
+                                        namelen)) {
+                               dlm_print_one_mle(mle);
+                               __dlm_put_mle(mle);
+                       }
+                       spin_unlock(&dlm->master_lock);
+                       spin_unlock(&dlm->spinlock);
                        BUG();
-               } else if (r == EAGAIN) {
+               }
+
+               if (r & DLM_ASSERT_RESPONSE_REASSERT &&
+                   !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
+                               mlog(ML_ERROR, "%.*s: very strange, "
+                                    "master MLE but no lockres on %u\n",
+                                    namelen, lockname, to);
+               }
+
+               if (r & DLM_ASSERT_RESPONSE_REASSERT) {
                        mlog(0, "%.*s: node %u create mles on other "
                             "nodes and requests a re-assert\n", 
                             namelen, lockname, to);
                        reassert = 1;
                }
+               if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
+                       mlog(0, "%.*s: node %u has a reference to this "
+                            "lockres, set the bit in the refmap\n",
+                            namelen, lockname, to);
+                       spin_lock(&res->spinlock);
+                       dlm_lockres_set_refmap_bit(to, res);
+                       spin_unlock(&res->spinlock);
+               }
        }
 
        if (reassert)
                goto again;
 
+       spin_lock(&res->spinlock);
+       res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
+       spin_unlock(&res->spinlock);
+       wake_up(&res->wq);
+
        return ret;
 }
 
@@ -1607,7 +1802,8 @@ again:
  *
  * if possible, TRIM THIS DOWN!!!
  */
-int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
+int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
+                             void **ret_data)
 {
        struct dlm_ctxt *dlm = data;
        struct dlm_master_list_entry *mle = NULL;
@@ -1616,7 +1812,7 @@ int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
        char *name;
        unsigned int namelen, hash;
        u32 flags;
-       int master_request = 0;
+       int master_request = 0, have_lockres_ref = 0;
        int ret = 0;
 
        if (!dlm_grab(dlm))
@@ -1649,7 +1845,7 @@ int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
                if (bit >= O2NM_MAX_NODES) {
                        /* not necessarily an error, though less likely.
                         * could be master just re-asserting. */
-                       mlog(ML_ERROR, "no bits set in the maybe_map, but %u "
+                       mlog(0, "no bits set in the maybe_map, but %u "
                             "is asserting! (%.*s)\n", assert->node_idx,
                             namelen, name);
                } else if (bit != assert->node_idx) {
@@ -1661,13 +1857,30 @@ int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
                                 * number winning the mastery will respond
                                 * YES to mastery requests, but this node
                                 * had no way of knowing.  let it pass. */
-                               mlog(ML_ERROR, "%u is the lowest node, "
+                               mlog(0, "%u is the lowest node, "
                                     "%u is asserting. (%.*s)  %u must "
                                     "have begun after %u won.\n", bit,
                                     assert->node_idx, namelen, name, bit,
                                     assert->node_idx);
                        }
                }
+               if (mle->type == DLM_MLE_MIGRATION) {
+                       if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
+                               mlog(0, "%s:%.*s: got cleanup assert"
+                                    " from %u for migration\n",
+                                    dlm->name, namelen, name,
+                                    assert->node_idx);
+                       } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
+                               mlog(0, "%s:%.*s: got unrelated assert"
+                                    " from %u for migration, ignoring\n",
+                                    dlm->name, namelen, name,
+                                    assert->node_idx);
+                               __dlm_put_mle(mle);
+                               spin_unlock(&dlm->master_lock);
+                               spin_unlock(&dlm->spinlock);
+                               goto done;
+                       }       
+               }
        }
        spin_unlock(&dlm->master_lock);
 
@@ -1682,7 +1895,8 @@ int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data)
                        goto kill;
                }
                if (!mle) {
-                       if (res->owner != assert->node_idx) {
+                       if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
+                           res->owner != assert->node_idx) {
                                mlog(ML_ERROR, "assert_master from "
                                          "%u, but current owner is "
                                          "%u! (%.*s)\n",
@@ -1735,6 +1949,7 @@ ok:
        if (mle) {
                int extra_ref = 0;
                int nn = -1;
+               int rr, err = 0;
                
                spin_lock(&mle->spinlock);
                if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
@@ -1754,59 +1969,131 @@ ok:
                wake_up(&mle->wq);
                spin_unlock(&mle->spinlock);
 
-               if (mle->type == DLM_MLE_MIGRATION && res) {
-                       mlog(0, "finishing off migration of lockres %.*s, "
-                            "from %u to %u\n",
-                              res->lockname.len, res->lockname.name,
-                              dlm->node_num, mle->new_master);
+               if (res) {
+                       int wake = 0;
                        spin_lock(&res->spinlock);
-                       res->state &= ~DLM_LOCK_RES_MIGRATING;
-                       dlm_change_lockres_owner(dlm, res, mle->new_master);
-                       BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
+                       if (mle->type == DLM_MLE_MIGRATION) {
+                               mlog(0, "finishing off migration of lockres %.*s, "
+                                       "from %u to %u\n",
+                                       res->lockname.len, res->lockname.name,
+                                       dlm->node_num, mle->new_master);
+                               res->state &= ~DLM_LOCK_RES_MIGRATING;
+                               wake = 1;
+                               dlm_change_lockres_owner(dlm, res, mle->new_master);
+                               BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
+                       } else {
+                               dlm_change_lockres_owner(dlm, res, mle->master);
+                       }
                        spin_unlock(&res->spinlock);
+                       have_lockres_ref = 1;
+                       if (wake)
+                               wake_up(&res->wq);
                }
-               /* master is known, detach if not already detached */
-               dlm_mle_detach_hb_events(dlm, mle);
-               dlm_put_mle(mle);
-               
+
+               /* master is known, detach if not already detached.
+                * ensures that only one assert_master call will happen
+                * on this mle. */
+               spin_lock(&dlm->spinlock);
+               spin_lock(&dlm->master_lock);
+
+               rr = atomic_read(&mle->mle_refs.refcount);
+               if (mle->inuse > 0) {
+                       if (extra_ref && rr < 3)
+                               err = 1;
+                       else if (!extra_ref && rr < 2)
+                               err = 1;
+               } else {
+                       if (extra_ref && rr < 2)
+                               err = 1;
+                       else if (!extra_ref && rr < 1)
+                               err = 1;
+               }
+               if (err) {
+                       mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
+                            "that will mess up this node, refs=%d, extra=%d, "
+                            "inuse=%d\n", dlm->name, namelen, name,
+                            assert->node_idx, rr, extra_ref, mle->inuse);
+                       dlm_print_one_mle(mle);
+               }
+               list_del_init(&mle->list);
+               __dlm_mle_detach_hb_events(dlm, mle);
+               __dlm_put_mle(mle);
                if (extra_ref) {
                        /* the assert master message now balances the extra
                         * ref given by the master / migration request message.
                         * if this is the last put, it will be removed
                         * from the list. */
-                       dlm_put_mle(mle);
+                       __dlm_put_mle(mle);
+               }
+               spin_unlock(&dlm->master_lock);
+               spin_unlock(&dlm->spinlock);
+       } else if (res) {
+               if (res->owner != assert->node_idx) {
+                       mlog(0, "assert_master from %u, but current "
+                            "owner is %u (%.*s), no mle\n", assert->node_idx,
+                            res->owner, namelen, name);
                }
        }
 
 done:
        ret = 0;
-       if (res)
-               dlm_lockres_put(res);
+       if (res) {
+               spin_lock(&res->spinlock);
+               res->state |= DLM_LOCK_RES_SETREF_INPROG;
+               spin_unlock(&res->spinlock);
+               *ret_data = (void *)res;
+       }
        dlm_put(dlm);
        if (master_request) {
                mlog(0, "need to tell master to reassert\n");
-               ret = EAGAIN;  // positive. negative would shoot down the node.
+               /* positive. negative would shoot down the node. */
+               ret |= DLM_ASSERT_RESPONSE_REASSERT;
+               if (!have_lockres_ref) {
+                       mlog(ML_ERROR, "strange, got assert from %u, MASTER "
+                            "mle present here for %s:%.*s, but no lockres!\n",
+                            assert->node_idx, dlm->name, namelen, name);
+               }
+       }
+       if (have_lockres_ref) {
+               /* let the master know we have a reference to the lockres */
+               ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
+               mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
+                    dlm->name, namelen, name, assert->node_idx);
        }
        return ret;
 
 kill:
        /* kill the caller! */
-       spin_unlock(&res->spinlock);
-       spin_unlock(&dlm->spinlock);
-       dlm_lockres_put(res);
        mlog(ML_ERROR, "Bad message received from another node.  Dumping state "
             "and killing the other node now!  This node is OK and can continue.\n");
-       dlm_dump_lock_resources(dlm);
+       __dlm_print_one_lock_resource(res);
+       spin_unlock(&res->spinlock);
+       spin_unlock(&dlm->spinlock);
+       *ret_data = (void *)res; 
        dlm_put(dlm);
        return -EINVAL;
 }
 
+void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
+{
+       struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
+
+       if (ret_data) {
+               spin_lock(&res->spinlock);
+               res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
+               spin_unlock(&res->spinlock);
+               wake_up(&res->wq);
+               dlm_lockres_put(res);
+       }
+       return;
+}
+
 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
                               struct dlm_lock_resource *res,
                               int ignore_higher, u8 request_from, u32 flags)
 {
        struct dlm_work_item *item;
-       item = kcalloc(1, sizeof(*item), GFP_KERNEL);
+       item = kzalloc(sizeof(*item), GFP_NOFS);
        if (!item)
                return -ENOMEM;
 
@@ -1828,7 +2115,7 @@ int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
        list_add_tail(&item->list, &dlm->work_list);
        spin_unlock(&dlm->work_lock);
 
-       schedule_work(&dlm->dispatched_work);
+       queue_work(dlm->dlm_worker, &dlm->dispatched_work);
        return 0;
 }
 
@@ -1869,18 +2156,38 @@ static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
                }
        }
 
+       /*
+        * If we're migrating this lock to someone else, we are no
+        * longer allowed to assert out own mastery.  OTOH, we need to
+        * prevent migration from starting while we're still asserting
+        * our dominance.  The reserved ast delays migration.
+        */
+       spin_lock(&res->spinlock);
+       if (res->state & DLM_LOCK_RES_MIGRATING) {
+               mlog(0, "Someone asked us to assert mastery, but we're "
+                    "in the middle of migration.  Skipping assert, "
+                    "the new master will handle that.\n");
+               spin_unlock(&res->spinlock);
+               goto put;
+       } else
+               __dlm_lockres_reserve_ast(res);
+       spin_unlock(&res->spinlock);
+
        /* this call now finishes out the nodemap
         * even if one or more nodes die */
        mlog(0, "worker about to master %.*s here, this=%u\n",
                     res->lockname.len, res->lockname.name, dlm->node_num);
-       ret = dlm_do_assert_master(dlm, res->lockname.name,
-                                  res->lockname.len,
-                                  nodemap, flags);
+       ret = dlm_do_assert_master(dlm, res, nodemap, flags);
        if (ret < 0) {
                /* no need to restart, we are done */
-               mlog_errno(ret);
+               if (!dlm_is_host_down(ret))
+                       mlog_errno(ret);
        }
 
+       /* Ok, we've asserted ourselves.  Let's let migration start. */
+       dlm_lockres_release_ast(dlm, res);
+
+put:
        dlm_lockres_put(res);
 
        mlog(0, "finished with dlm_assert_master_worker\n");
@@ -1919,6 +2226,7 @@ static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
                                BUG();
                        /* host is down, so answer for that node would be
                         * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
+                       ret = 0;
                }
 
                if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
@@ -1941,26 +2249,240 @@ static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
        return ret;
 }
 
+/*
+ * DLM_DEREF_LOCKRES_MSG
+ */
+
+int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
+{
+       struct dlm_deref_lockres deref;
+       int ret = 0, r;
+       const char *lockname;
+       unsigned int namelen;
+
+       lockname = res->lockname.name;
+       namelen = res->lockname.len;
+       BUG_ON(namelen > O2NM_MAX_NAME_LEN);
+
+       mlog(0, "%s:%.*s: sending deref to %d\n",
+            dlm->name, namelen, lockname, res->owner);
+       memset(&deref, 0, sizeof(deref));
+       deref.node_idx = dlm->node_num;
+       deref.namelen = namelen;
+       memcpy(deref.name, lockname, namelen);
+
+       ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
+                                &deref, sizeof(deref), res->owner, &r);
+       if (ret < 0)
+               mlog_errno(ret);
+       else if (r < 0) {
+               /* BAD.  other node says I did not have a ref. */
+               mlog(ML_ERROR,"while dropping ref on %s:%.*s "
+                   "(master=%u) got %d.\n", dlm->name, namelen,
+                   lockname, res->owner, r);
+               dlm_print_one_lock_resource(res);
+               BUG();
+       }
+       return ret;
+}
+
+int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
+                             void **ret_data)
+{
+       struct dlm_ctxt *dlm = data;
+       struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
+       struct dlm_lock_resource *res = NULL;
+       char *name;
+       unsigned int namelen;
+       int ret = -EINVAL;
+       u8 node;
+       unsigned int hash;
+       struct dlm_work_item *item;
+       int cleared = 0;
+       int dispatch = 0;
+
+       if (!dlm_grab(dlm))
+               return 0;
+
+       name = deref->name;
+       namelen = deref->namelen;
+       node = deref->node_idx;
+
+       if (namelen > DLM_LOCKID_NAME_MAX) {
+               mlog(ML_ERROR, "Invalid name length!");
+               goto done;
+       }
+       if (deref->node_idx >= O2NM_MAX_NODES) {
+               mlog(ML_ERROR, "Invalid node number: %u\n", node);
+               goto done;
+       }
+
+       hash = dlm_lockid_hash(name, namelen);
+
+       spin_lock(&dlm->spinlock);
+       res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
+       if (!res) {
+               spin_unlock(&dlm->spinlock);
+               mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
+                    dlm->name, namelen, name);
+               goto done;
+       }
+       spin_unlock(&dlm->spinlock);
+
+       spin_lock(&res->spinlock);
+       if (res->state & DLM_LOCK_RES_SETREF_INPROG)
+               dispatch = 1;
+       else {
+               BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
+               if (test_bit(node, res->refmap)) {
+                       dlm_lockres_clear_refmap_bit(node, res);
+                       cleared = 1;
+               }
+       }
+       spin_unlock(&res->spinlock);
+
+       if (!dispatch) {
+               if (cleared)
+                       dlm_lockres_calc_usage(dlm, res);
+               else {
+                       mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
+                       "but it is already dropped!\n", dlm->name,
+                       res->lockname.len, res->lockname.name, node);
+                       __dlm_print_one_lock_resource(res);
+               }
+               ret = 0;
+               goto done;
+       }
+
+       item = kzalloc(sizeof(*item), GFP_NOFS);
+       if (!item) {
+               ret = -ENOMEM;
+               mlog_errno(ret);
+               goto done;
+       }
+
+       dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
+       item->u.dl.deref_res = res;
+       item->u.dl.deref_node = node;
+
+       spin_lock(&dlm->work_lock);
+       list_add_tail(&item->list, &dlm->work_list);
+       spin_unlock(&dlm->work_lock);
+
+       queue_work(dlm->dlm_worker, &dlm->dispatched_work);
+       return 0;
+
+done:
+       if (res)
+               dlm_lockres_put(res);
+       dlm_put(dlm);
+
+       return ret;
+}
+
+static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
+{
+       struct dlm_ctxt *dlm;
+       struct dlm_lock_resource *res;
+       u8 node;
+       u8 cleared = 0;
+
+       dlm = item->dlm;
+       res = item->u.dl.deref_res;
+       node = item->u.dl.deref_node;
+
+       spin_lock(&res->spinlock);
+       BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
+       if (test_bit(node, res->refmap)) {
+               __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
+               dlm_lockres_clear_refmap_bit(node, res);
+               cleared = 1;
+       }
+       spin_unlock(&res->spinlock);
+
+       if (cleared) {
+               mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
+                    dlm->name, res->lockname.len, res->lockname.name, node);
+               dlm_lockres_calc_usage(dlm, res);
+       } else {
+               mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
+                    "but it is already dropped!\n", dlm->name,
+                    res->lockname.len, res->lockname.name, node);
+               __dlm_print_one_lock_resource(res);
+       }
+
+       dlm_lockres_put(res);
+}
+
+/* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
+ * if not. If 0, numlocks is set to the number of locks in the lockres.
+ */
+static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
+                                     struct dlm_lock_resource *res,
+                                     int *numlocks)
+{
+       int ret;
+       int i;
+       int count = 0;
+       struct list_head *queue;
+       struct dlm_lock *lock;
+
+       assert_spin_locked(&res->spinlock);
+
+       ret = -EINVAL;
+       if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
+               mlog(0, "cannot migrate lockres with unknown owner!\n");
+               goto leave;
+       }
+
+       if (res->owner != dlm->node_num) {
+               mlog(0, "cannot migrate lockres this node doesn't own!\n");
+               goto leave;
+       }
+
+       ret = 0;
+       queue = &res->granted;
+       for (i = 0; i < 3; i++) {
+               list_for_each_entry(lock, queue, list) {
+                       ++count;
+                       if (lock->ml.node == dlm->node_num) {
+                               mlog(0, "found a lock owned by this node still "
+                                    "on the %s queue!  will not migrate this "
+                                    "lockres\n", (i == 0 ? "granted" :
+                                                  (i == 1 ? "converting" :
+                                                   "blocked")));
+                               ret = -ENOTEMPTY;
+                               goto leave;
+                       }
+               }
+               queue++;
+       }
+
+       *numlocks = count;
+       mlog(0, "migrateable lockres having %d locks\n", *numlocks);
+
+leave:
+       return ret;
+}
 
 /*
  * DLM_MIGRATE_LOCKRES
  */
 
 
-int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
-                       u8 target)
+static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
+                              struct dlm_lock_resource *res,
+                              u8 target)
 {
        struct dlm_master_list_entry *mle = NULL;
        struct dlm_master_list_entry *oldmle = NULL;
        struct dlm_migratable_lockres *mres = NULL;
-       int ret = -EINVAL;
+       int ret = 0;
        const char *name;
        unsigned int namelen;
        int mle_added = 0;
-       struct list_head *queue, *iter;
-       int i;
-       struct dlm_lock *lock;
-       int empty = 1;
+       int numlocks;
+       int wake = 0;
 
        if (!dlm_grab(dlm))
                return -EINVAL;
@@ -1974,42 +2496,16 @@ int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
         * ensure this lockres is a proper candidate for migration
         */
        spin_lock(&res->spinlock);
-       if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
-               mlog(0, "cannot migrate lockres with unknown owner!\n");
-               spin_unlock(&res->spinlock);
-               goto leave;
-       }
-       if (res->owner != dlm->node_num) {
-               mlog(0, "cannot migrate lockres this node doesn't own!\n");
+       ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
+       if (ret < 0) {
                spin_unlock(&res->spinlock);
                goto leave;
        }
-       mlog(0, "checking queues...\n");
-       queue = &res->granted;
-       for (i=0; i<3; i++) {
-               list_for_each(iter, queue) {
-                       lock = list_entry (iter, struct dlm_lock, list);
-                       empty = 0;
-                       if (lock->ml.node == dlm->node_num) {
-                               mlog(0, "found a lock owned by this node "
-                                    "still on the %s queue!  will not "
-                                    "migrate this lockres\n",
-                                    i==0 ? "granted" :
-                                    (i==1 ? "converting" : "blocked"));
-                               spin_unlock(&res->spinlock);
-                               ret = -ENOTEMPTY;
-                               goto leave;
-                       }
-               }
-               queue++;
-       }
-       mlog(0, "all locks on this lockres are nonlocal.  continuing\n");
        spin_unlock(&res->spinlock);
 
        /* no work to do */
-       if (empty) {
+       if (numlocks == 0) {
                mlog(0, "no locks were found on this lockres! done!\n");
-               ret = 0;
                goto leave;
        }
 
@@ -2019,14 +2515,14 @@ int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
         */
 
        ret = -ENOMEM;
-       mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_KERNEL);
+       mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
        if (!mres) {
                mlog_errno(ret);
                goto leave;
        }
 
        mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
-                                                               GFP_KERNEL);
+                                                               GFP_NOFS);
        if (!mle) {
                mlog_errno(ret);
                goto leave;
@@ -2085,6 +2581,7 @@ int dlm_migrate_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
                     res->lockname.name, target);
                spin_lock(&res->spinlock);
                res->state &= ~DLM_LOCK_RES_MIGRATING;
+               wake = 1;
                spin_unlock(&res->spinlock);
                ret = -EINVAL;
        }
@@ -2112,6 +2609,9 @@ fail:
         * the lockres
         */
 
+       /* now that remote nodes are spinning on the MIGRATING flag,
+        * ensure that all assert_master work is flushed. */
+       flush_workqueue(dlm->dlm_worker);
 
        /* get an extra reference on the mle.
         * otherwise the assert_master from the new
@@ -2120,7 +2620,7 @@ fail:
         * take both dlm->spinlock and dlm->master_lock */
        spin_lock(&dlm->spinlock);
        spin_lock(&dlm->master_lock);
-       dlm_get_mle(mle);
+       dlm_get_mle_inuse(mle);
        spin_unlock(&dlm->master_lock);
        spin_unlock(&dlm->spinlock);
 
@@ -2137,7 +2637,11 @@ fail:
                /* migration failed, detach and clean up mle */
                dlm_mle_detach_hb_events(dlm, mle);
                dlm_put_mle(mle);
-               dlm_put_mle(mle);
+               dlm_put_mle_inuse(mle);
+               spin_lock(&res->spinlock);
+               res->state &= ~DLM_LOCK_RES_MIGRATING;
+               wake = 1;
+               spin_unlock(&res->spinlock);
                goto leave;
        }
 
@@ -2163,25 +2667,29 @@ fail:
                            res->owner == target)
                                break;
 
-                       mlog(0, "timed out during migration\n");
+                       mlog(0, "%s:%.*s: timed out during migration\n",
+                            dlm->name, res->lockname.len, res->lockname.name);
                        /* avoid hang during shutdown when migrating lockres 
                         * to a node which also goes down */
                        if (dlm_is_node_dead(dlm, target)) {
-                               mlog(0, "%s:%.*s: expected migration target %u "
-                                    "is no longer up.  restarting.\n",
+                               mlog(0, "%s:%.*s: expected migration "
+                                    "target %u is no longer up, restarting\n",
                                     dlm->name, res->lockname.len,
                                     res->lockname.name, target);
-                               ret = -ERESTARTSYS;
+                               ret = -EINVAL;
+                               /* migration failed, detach and clean up mle */
+                               dlm_mle_detach_hb_events(dlm, mle);
+                               dlm_put_mle(mle);
+                               dlm_put_mle_inuse(mle);
+                               spin_lock(&res->spinlock);
+                               res->state &= ~DLM_LOCK_RES_MIGRATING;
+                               wake = 1;
+                               spin_unlock(&res->spinlock);
+                               goto leave;
                        }
-               }
-               if (ret == -ERESTARTSYS) {
-                       /* migration failed, detach and clean up mle */
-                       dlm_mle_detach_hb_events(dlm, mle);
-                       dlm_put_mle(mle);
-                       dlm_put_mle(mle);
-                       goto leave;
-               }
-               /* TODO: if node died: stop, clean up, return error */
+               } else
+                       mlog(0, "%s:%.*s: caught signal during migration\n",
+                            dlm->name, res->lockname.len, res->lockname.name);
        }
 
        /* all done, set the owner, clear the flag */
@@ -2194,7 +2702,7 @@ fail:
 
        /* master is known, detach if not already detached */
        dlm_mle_detach_hb_events(dlm, mle);
-       dlm_put_mle(mle);
+       dlm_put_mle_inuse(mle);
        ret = 0;
 
        dlm_lockres_calc_usage(dlm, res);
@@ -2204,6 +2712,11 @@ leave:
        if (ret < 0)
                dlm_kick_thread(dlm, res);
 
+       /* wake up waiters if the MIGRATING flag got set
+        * but migration failed */
+       if (wake)
+               wake_up(&res->wq);
+
        /* TODO: cleanup */
        if (mres)
                free_page((unsigned long)mres);
@@ -2213,7 +2726,64 @@ leave:
        mlog(0, "returning %d\n", ret);
        return ret;
 }
-EXPORT_SYMBOL_GPL(dlm_migrate_lockres);
+
+#define DLM_MIGRATION_RETRY_MS  100
+
+/* Should be called only after beginning the domain leave process.
+ * There should not be any remaining locks on nonlocal lock resources,
+ * and there should be no local locks left on locally mastered resources.
+ *
+ * Called with the dlm spinlock held, may drop it to do migration, but
+ * will re-acquire before exit.
+ *
+ * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
+int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
+{
+       int ret;
+       int lock_dropped = 0;
+       int numlocks;
+
+       spin_lock(&res->spinlock);
+       if (res->owner != dlm->node_num) {
+               if (!__dlm_lockres_unused(res)) {
+                       mlog(ML_ERROR, "%s:%.*s: this node is not master, "
+                            "trying to free this but locks remain\n",
+                            dlm->name, res->lockname.len, res->lockname.name);
+               }
+               spin_unlock(&res->spinlock);
+               goto leave;
+       }
+
+       /* No need to migrate a lockres having no locks */
+       ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
+       if (ret >= 0 && numlocks == 0) {
+               spin_unlock(&res->spinlock);
+               goto leave;
+       }
+       spin_unlock(&res->spinlock);
+
+       /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
+       spin_unlock(&dlm->spinlock);
+       lock_dropped = 1;
+       while (1) {
+               ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
+               if (ret >= 0)
+                       break;
+               if (ret == -ENOTEMPTY) {
+                       mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
+                               res->lockname.len, res->lockname.name);
+                       BUG();
+               }
+
+               mlog(0, "lockres %.*s: migrate failed, "
+                    "retrying\n", res->lockname.len,
+                    res->lockname.name);
+               msleep(DLM_MIGRATION_RETRY_MS);
+       }
+       spin_lock(&dlm->spinlock);
+leave:
+       return lock_dropped;
+}
 
 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
 {
@@ -2244,7 +2814,8 @@ static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
        return can_proceed;
 }
 
-int dlm_lockres_is_dirty(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
+static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
+                               struct dlm_lock_resource *res)
 {
        int ret;
        spin_lock(&res->spinlock);
@@ -2273,8 +2844,15 @@ static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
        __dlm_lockres_reserve_ast(res);
        spin_unlock(&res->spinlock);
 
-       /* now flush all the pending asts.. hang out for a bit */
+       /* now flush all the pending asts */
        dlm_kick_thread(dlm, res);
+       /* before waiting on DIRTY, block processes which may
+        * try to dirty the lockres before MIGRATING is set */
+       spin_lock(&res->spinlock);
+       BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
+       res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
+       spin_unlock(&res->spinlock);
+       /* now wait on any pending asts and the DIRTY state */
        wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
        dlm_lockres_release_ast(dlm, res);
 
@@ -2300,6 +2878,13 @@ again:
                mlog(0, "trying again...\n");
                goto again;
        }
+       /* now that we are sure the MIGRATING state is there, drop
+        * the unneded state which blocked threads trying to DIRTY */
+       spin_lock(&res->spinlock);
+       BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
+       BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
+       res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
+       spin_unlock(&res->spinlock);
 
        /* did the target go down or die? */
        spin_lock(&dlm->spinlock);
@@ -2327,18 +2912,16 @@ again:
 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
                                      struct dlm_lock_resource *res)
 {
-       struct list_head *iter, *iter2;
        struct list_head *queue = &res->granted;
-       int i;
-       struct dlm_lock *lock;
+       int i, bit;
+       struct dlm_lock *lock, *next;
 
        assert_spin_locked(&res->spinlock);
 
        BUG_ON(res->owner == dlm->node_num);
 
        for (i=0; i<3; i++) {
-               list_for_each_safe(iter, iter2, queue) {
-                       lock = list_entry (iter, struct dlm_lock, list);
+               list_for_each_entry_safe(lock, next, queue, list) {
                        if (lock->ml.node != dlm->node_num) {
                                mlog(0, "putting lock for node %u\n",
                                     lock->ml.node);
@@ -2347,12 +2930,28 @@ static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
                                BUG_ON(!list_empty(&lock->bast_list));
                                BUG_ON(lock->ast_pending);
                                BUG_ON(lock->bast_pending);
+                               dlm_lockres_clear_refmap_bit(lock->ml.node, res);
                                list_del_init(&lock->list);
                                dlm_lock_put(lock);
                        }
                }
                queue++;
        }
+       bit = 0;
+       while (1) {
+               bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
+               if (bit >= O2NM_MAX_NODES)
+                       break;
+               /* do not clear the local node reference, if there is a
+                * process holding this, let it drop the ref itself */
+               if (bit != dlm->node_num) {
+                       mlog(0, "%s:%.*s: node %u had a ref to this "
+                            "migrating lockres, clearing\n", dlm->name,
+                            res->lockname.len, res->lockname.name, bit);
+                       dlm_lockres_clear_refmap_bit(bit, res);
+               }
+               bit++;
+       }
 }
 
 /* for now this is not too intelligent.  we will
@@ -2364,7 +2963,6 @@ static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
 {
        int i;
        struct list_head *queue = &res->granted;
-       struct list_head *iter;
        struct dlm_lock *lock;
        int nodenum;
 
@@ -2372,10 +2970,9 @@ static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
 
        spin_lock(&res->spinlock);
        for (i=0; i<3; i++) {
-               list_for_each(iter, queue) {
+               list_for_each_entry(lock, queue, list) {
                        /* up to the caller to make sure this node
                         * is alive */
-                       lock = list_entry (iter, struct dlm_lock, list);
                        if (lock->ml.node != dlm->node_num) {
                                spin_unlock(&res->spinlock);
                                return lock->ml.node;
@@ -2440,6 +3037,16 @@ static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
                        mlog(0, "migrate request (node %u) returned %d!\n",
                             nodenum, status);
                        ret = status;
+               } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
+                       /* during the migration request we short-circuited
+                        * the mastery of the lockres.  make sure we have
+                        * a mastery ref for nodenum */
+                       mlog(0, "%s:%.*s: need ref for node %u\n",
+                            dlm->name, res->lockname.len, res->lockname.name,
+                            nodenum);
+                       spin_lock(&res->spinlock);
+                       dlm_lockres_set_refmap_bit(nodenum, res);
+                       spin_unlock(&res->spinlock);
                }
        }
 
@@ -2458,7 +3065,8 @@ static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
  * we will have no mle in the list to start with.  now we can add an mle for
  * the migration and this should be the only one found for those scanning the
  * list.  */
-int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data)
+int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
+                               void **ret_data)
 {
        struct dlm_ctxt *dlm = data;
        struct dlm_lock_resource *res = NULL;
@@ -2477,7 +3085,7 @@ int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data)
 
        /* preallocate.. if this fails, abort */
        mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
-                                                        GFP_KERNEL);
+                                                        GFP_NOFS);
 
        if (!mle) {
                ret = -ENOMEM;
@@ -2584,6 +3192,13 @@ static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
                        /* remove it from the list so that only one
                         * mle will be found */
                        list_del_init(&tmp->list);
+                       /* this was obviously WRONG.  mle is uninited here.  should be tmp. */
+                       __dlm_mle_detach_hb_events(dlm, tmp);
+                       ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
+                       mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
+                           "telling master to get ref for cleared out mle "
+                           "during migration\n", dlm->name, namelen, name,
+                           master, new_master);
                }
                spin_unlock(&tmp->spinlock);
        }
@@ -2591,6 +3206,8 @@ static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
        /* now add a migration mle to the tail of the list */
        dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
        mle->new_master = new_master;
+       /* the new master will be sending an assert master for this.
+        * at that point we will get the refmap reference */
        mle->master = master;
        /* do this for consistency with other mle types */
        set_bit(new_master, mle->maybe_map);
@@ -2602,8 +3219,7 @@ static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
 
 void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
 {
-       struct list_head *iter, *iter2;
-       struct dlm_master_list_entry *mle;
+       struct dlm_master_list_entry *mle, *next;
        struct dlm_lock_resource *res;
        unsigned int hash;
 
@@ -2613,9 +3229,7 @@ top:
 
        /* clean the master list */
        spin_lock(&dlm->master_lock);
-       list_for_each_safe(iter, iter2, &dlm->master_list) {
-               mle = list_entry(iter, struct dlm_master_list_entry, list);
-
+       list_for_each_entry_safe(mle, next, &dlm->master_list, list) {
                BUG_ON(mle->type != DLM_MLE_BLOCK &&
                       mle->type != DLM_MLE_MASTER &&
                       mle->type != DLM_MLE_MIGRATION);
@@ -2645,7 +3259,7 @@ top:
                                 * may result in the mle being unlinked and
                                 * freed, but there may still be a process
                                 * waiting in the dlmlock path which is fine. */
-                               mlog(ML_ERROR, "node %u was expected master\n",
+                               mlog(0, "node %u was expected master\n",
                                     dead_node);
                                atomic_set(&mle->woken, 1);
                                spin_unlock(&mle->spinlock);
@@ -2678,14 +3292,15 @@ top:
 
                /* remove from the list early.  NOTE: unlinking
                 * list_head while in list_for_each_safe */
+               __dlm_mle_detach_hb_events(dlm, mle);
                spin_lock(&mle->spinlock);
                list_del_init(&mle->list);
                atomic_set(&mle->woken, 1);
                spin_unlock(&mle->spinlock);
                wake_up(&mle->wq);
 
-               mlog(0, "node %u died during migration from "
-                    "%u to %u!\n", dead_node,
+               mlog(0, "%s: node %u died during migration from "
+                    "%u to %u!\n", dlm->name, dead_node,
                     mle->master, mle->new_master);
                /* if there is a lockres associated with this
                 * mle, find it and set its owner to UNKNOWN */
@@ -2739,6 +3354,13 @@ int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
        clear_bit(dlm->node_num, iter.node_map);
        spin_unlock(&dlm->spinlock);
 
+       /* ownership of the lockres is changing.  account for the
+        * mastery reference here since old_master will briefly have
+        * a reference after the migration completes */
+       spin_lock(&res->spinlock);
+       dlm_lockres_set_refmap_bit(old_master, res);
+       spin_unlock(&res->spinlock);
+
        mlog(0, "now time to do a migrate request to other nodes\n");
        ret = dlm_do_migrate_request(dlm, res, old_master,
                                     dlm->node_num, &iter);
@@ -2751,8 +3373,7 @@ int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
             res->lockname.len, res->lockname.name);
        /* this call now finishes out the nodemap
         * even if one or more nodes die */
-       ret = dlm_do_assert_master(dlm, res->lockname.name,
-                                  res->lockname.len, iter.node_map,
+       ret = dlm_do_assert_master(dlm, res, iter.node_map,
                                   DLM_ASSERT_MASTER_FINISH_MIGRATION);
        if (ret < 0) {
                /* no longer need to retry.  all living nodes contacted. */
@@ -2764,8 +3385,7 @@ int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
        set_bit(old_master, iter.node_map);
        mlog(0, "doing assert master of %.*s back to %u\n",
             res->lockname.len, res->lockname.name, old_master);
-       ret = dlm_do_assert_master(dlm, res->lockname.name,
-                                  res->lockname.len, iter.node_map,
+       ret = dlm_do_assert_master(dlm, res, iter.node_map,
                                   DLM_ASSERT_MASTER_FINISH_MIGRATION);
        if (ret < 0) {
                mlog(0, "assert master to original master failed "