#include "memory.h"
 #include "lock.h"
 #include "recover.h"
+#include "requestqueue.h"
 
 #ifdef CONFIG_DLM_DEBUG
 int dlm_create_debug_file(struct dlm_ls *ls);
         * Free structures on any other lists
         */
 
+       dlm_purge_requestqueue(ls);
        kfree(ls->ls_recover_args);
        dlm_clear_free_entries(ls);
        dlm_clear_members(ls);
 
                goto fail;
        }
 
-       /*
-        * Purge directory-related requests that are saved in requestqueue.
-        * All dir requests from before recovery are invalid now due to the dir
-        * rebuild and will be resent by the requesting nodes.
-        */
-
-       dlm_purge_requestqueue(ls);
-
        /*
         * Wait for all nodes to complete directory rebuild.
         */
 
        dlm_release_root_list(ls);
 
+       /*
+        * Purge directory-related requests that are saved in requestqueue.
+        * All dir requests from before recovery are invalid now due to the dir
+        * rebuild and will be resent by the requesting nodes.
+        */
+
+       dlm_purge_requestqueue(ls);
+
        dlm_set_recover_status(ls, DLM_RS_DONE);
        error = dlm_recover_done_wait(ls);
        if (error) {
 
        int length = hd->h_length;
        int rv = 0;
 
-       if (dlm_is_removed(ls, nodeid))
-               return 0;
-
        e = kmalloc(sizeof(struct rq_entry) + length, GFP_KERNEL);
        if (!e) {
                log_print("dlm_add_requestqueue: out of memory\n");
 {
        uint32_t type = ms->m_type;
 
+       /* the ls is being cleaned up and freed by release_lockspace */
+       if (!ls->ls_count)
+               return 1;
+
        if (dlm_is_removed(ls, nodeid))
                return 1;