xref: /linux/fs/ocfs2/dlm/dlmrecovery.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1328970deSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fa60ce2cSMasahiro Yamada /*
36714d8e8SKurt Hackel  * dlmrecovery.c
46714d8e8SKurt Hackel  *
56714d8e8SKurt Hackel  * recovery stuff
66714d8e8SKurt Hackel  *
76714d8e8SKurt Hackel  * Copyright (C) 2004 Oracle.  All rights reserved.
86714d8e8SKurt Hackel  */
96714d8e8SKurt Hackel 
106714d8e8SKurt Hackel 
116714d8e8SKurt Hackel #include <linux/module.h>
126714d8e8SKurt Hackel #include <linux/fs.h>
136714d8e8SKurt Hackel #include <linux/types.h>
146714d8e8SKurt Hackel #include <linux/slab.h>
156714d8e8SKurt Hackel #include <linux/highmem.h>
166714d8e8SKurt Hackel #include <linux/init.h>
176714d8e8SKurt Hackel #include <linux/sysctl.h>
186714d8e8SKurt Hackel #include <linux/random.h>
196714d8e8SKurt Hackel #include <linux/blkdev.h>
206714d8e8SKurt Hackel #include <linux/socket.h>
216714d8e8SKurt Hackel #include <linux/inet.h>
226714d8e8SKurt Hackel #include <linux/timer.h>
236714d8e8SKurt Hackel #include <linux/kthread.h>
24b4c7f538SAdrian Bunk #include <linux/delay.h>
256714d8e8SKurt Hackel 
266714d8e8SKurt Hackel 
27ca322fb6SMasahiro Yamada #include "../cluster/heartbeat.h"
28ca322fb6SMasahiro Yamada #include "../cluster/nodemanager.h"
29ca322fb6SMasahiro Yamada #include "../cluster/tcp.h"
306714d8e8SKurt Hackel 
316714d8e8SKurt Hackel #include "dlmapi.h"
326714d8e8SKurt Hackel #include "dlmcommon.h"
336714d8e8SKurt Hackel #include "dlmdomain.h"
346714d8e8SKurt Hackel 
356714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
36ca322fb6SMasahiro Yamada #include "../cluster/masklog.h"
376714d8e8SKurt Hackel 
386714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
396714d8e8SKurt Hackel 
406714d8e8SKurt Hackel static int dlm_recovery_thread(void *data);
416714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm);
426714d8e8SKurt Hackel 
436714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
446714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
456714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
466714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm,
476714d8e8SKurt Hackel 				 u8 request_from, u8 dead_node);
482bcb654cSChangwei Ge static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm);
496714d8e8SKurt Hackel 
506714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
516714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
526714d8e8SKurt Hackel 					const char *lockname, int namelen,
536714d8e8SKurt Hackel 					int total_locks, u64 cookie,
546714d8e8SKurt Hackel 					u8 flags, u8 master);
556714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
566714d8e8SKurt Hackel 				    struct dlm_migratable_lockres *mres,
576714d8e8SKurt Hackel 				    u8 send_to,
586714d8e8SKurt Hackel 				    struct dlm_lock_resource *res,
596714d8e8SKurt Hackel 				    int total_locks);
606714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
616714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
626714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres);
636714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
646714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
656714d8e8SKurt Hackel 				 u8 dead_node, u8 send_to);
666714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
676714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
686714d8e8SKurt Hackel 					struct list_head *list, u8 dead_node);
696714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
706714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master);
716714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata);
726714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type);
736714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
746714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item,
756714d8e8SKurt Hackel 					 void *data);
766714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
778169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
788169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
798169cae5SAdrian Bunk 				      u8 *real_master);
806714d8e8SKurt Hackel 
816714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void);
826714d8e8SKurt Hackel 
8334af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_reco_state_lock);
8434af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
856714d8e8SKurt Hackel static u64 dlm_mig_cookie = 1;
866714d8e8SKurt Hackel 
dlm_get_next_mig_cookie(void)876714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void)
886714d8e8SKurt Hackel {
896714d8e8SKurt Hackel 	u64 c;
906714d8e8SKurt Hackel 	spin_lock(&dlm_mig_cookie_lock);
916714d8e8SKurt Hackel 	c = dlm_mig_cookie;
926714d8e8SKurt Hackel 	if (dlm_mig_cookie == (~0ULL))
936714d8e8SKurt Hackel 		dlm_mig_cookie = 1;
946714d8e8SKurt Hackel 	else
956714d8e8SKurt Hackel 		dlm_mig_cookie++;
966714d8e8SKurt Hackel 	spin_unlock(&dlm_mig_cookie_lock);
976714d8e8SKurt Hackel 	return c;
986714d8e8SKurt Hackel }
996714d8e8SKurt Hackel 
dlm_set_reco_dead_node(struct dlm_ctxt * dlm,u8 dead_node)100ab27eb6fSKurt Hackel static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
101ab27eb6fSKurt Hackel 					  u8 dead_node)
102ab27eb6fSKurt Hackel {
103ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
104ab27eb6fSKurt Hackel 	if (dlm->reco.dead_node != dead_node)
105ab27eb6fSKurt Hackel 		mlog(0, "%s: changing dead_node from %u to %u\n",
106ab27eb6fSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dead_node);
107ab27eb6fSKurt Hackel 	dlm->reco.dead_node = dead_node;
108ab27eb6fSKurt Hackel }
109ab27eb6fSKurt Hackel 
dlm_set_reco_master(struct dlm_ctxt * dlm,u8 master)110ab27eb6fSKurt Hackel static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
111ab27eb6fSKurt Hackel 				       u8 master)
112ab27eb6fSKurt Hackel {
113ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
114ab27eb6fSKurt Hackel 	mlog(0, "%s: changing new_master from %u to %u\n",
115ab27eb6fSKurt Hackel 	     dlm->name, dlm->reco.new_master, master);
116ab27eb6fSKurt Hackel 	dlm->reco.new_master = master;
117ab27eb6fSKurt Hackel }
118ab27eb6fSKurt Hackel 
__dlm_reset_recovery(struct dlm_ctxt * dlm)119466d1a45SKurt Hackel static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
1206714d8e8SKurt Hackel {
121466d1a45SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1226714d8e8SKurt Hackel 	clear_bit(dlm->reco.dead_node, dlm->recovery_map);
123ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
124ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
125466d1a45SKurt Hackel }
126466d1a45SKurt Hackel 
1276714d8e8SKurt Hackel /* Worker function used during recovery. */
dlm_dispatch_work(struct work_struct * work)128c4028958SDavid Howells void dlm_dispatch_work(struct work_struct *work)
1296714d8e8SKurt Hackel {
130c4028958SDavid Howells 	struct dlm_ctxt *dlm =
131c4028958SDavid Howells 		container_of(work, struct dlm_ctxt, dispatched_work);
1326714d8e8SKurt Hackel 	LIST_HEAD(tmp_list);
133800deef3SChristoph Hellwig 	struct dlm_work_item *item, *next;
1346714d8e8SKurt Hackel 	dlm_workfunc_t *workfunc;
1353156d267SKurt Hackel 	int tot=0;
1363156d267SKurt Hackel 
1376714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
1386714d8e8SKurt Hackel 	list_splice_init(&dlm->work_list, &tmp_list);
1396714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
1406714d8e8SKurt Hackel 
141800deef3SChristoph Hellwig 	list_for_each_entry(item, &tmp_list, list) {
1423156d267SKurt Hackel 		tot++;
1433156d267SKurt Hackel 	}
1443156d267SKurt Hackel 	mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
1453156d267SKurt Hackel 
146800deef3SChristoph Hellwig 	list_for_each_entry_safe(item, next, &tmp_list, list) {
1476714d8e8SKurt Hackel 		workfunc = item->func;
1486714d8e8SKurt Hackel 		list_del_init(&item->list);
1496714d8e8SKurt Hackel 
1506714d8e8SKurt Hackel 		/* already have ref on dlm to avoid having
1516714d8e8SKurt Hackel 		 * it disappear.  just double-check. */
1526714d8e8SKurt Hackel 		BUG_ON(item->dlm != dlm);
1536714d8e8SKurt Hackel 
1546714d8e8SKurt Hackel 		/* this is allowed to sleep and
1556714d8e8SKurt Hackel 		 * call network stuff */
1566714d8e8SKurt Hackel 		workfunc(item, item->data);
1576714d8e8SKurt Hackel 
1586714d8e8SKurt Hackel 		dlm_put(dlm);
1596714d8e8SKurt Hackel 		kfree(item);
1606714d8e8SKurt Hackel 	}
1616714d8e8SKurt Hackel }
1626714d8e8SKurt Hackel 
1636714d8e8SKurt Hackel /*
1646714d8e8SKurt Hackel  * RECOVERY THREAD
1656714d8e8SKurt Hackel  */
1666714d8e8SKurt Hackel 
dlm_kick_recovery_thread(struct dlm_ctxt * dlm)167c03872f5SKurt Hackel void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
1686714d8e8SKurt Hackel {
1696714d8e8SKurt Hackel 	/* wake the recovery thread
1706714d8e8SKurt Hackel 	 * this will wake the reco thread in one of three places
1716714d8e8SKurt Hackel 	 * 1) sleeping with no recovery happening
1726714d8e8SKurt Hackel 	 * 2) sleeping with recovery mastered elsewhere
1736714d8e8SKurt Hackel 	 * 3) recovery mastered here, waiting on reco data */
1746714d8e8SKurt Hackel 
1756714d8e8SKurt Hackel 	wake_up(&dlm->dlm_reco_thread_wq);
1766714d8e8SKurt Hackel }
1776714d8e8SKurt Hackel 
1786714d8e8SKurt Hackel /* Launch the recovery thread */
dlm_launch_recovery_thread(struct dlm_ctxt * dlm)1796714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
1806714d8e8SKurt Hackel {
1816714d8e8SKurt Hackel 	mlog(0, "starting dlm recovery thread...\n");
1826714d8e8SKurt Hackel 
1836714d8e8SKurt Hackel 	dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
1845afc44e2SJoseph Qi 			"dlm_reco-%s", dlm->name);
1856714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_reco_thread_task)) {
1866714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
1876714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
1886714d8e8SKurt Hackel 		return -EINVAL;
1896714d8e8SKurt Hackel 	}
1906714d8e8SKurt Hackel 
1916714d8e8SKurt Hackel 	return 0;
1926714d8e8SKurt Hackel }
1936714d8e8SKurt Hackel 
dlm_complete_recovery_thread(struct dlm_ctxt * dlm)1946714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
1956714d8e8SKurt Hackel {
1966714d8e8SKurt Hackel 	if (dlm->dlm_reco_thread_task) {
1976714d8e8SKurt Hackel 		mlog(0, "waiting for dlm recovery thread to exit\n");
1986714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_reco_thread_task);
1996714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2006714d8e8SKurt Hackel 	}
2016714d8e8SKurt Hackel }
2026714d8e8SKurt Hackel 
2036714d8e8SKurt Hackel 
2046714d8e8SKurt Hackel 
2056714d8e8SKurt Hackel /*
2066714d8e8SKurt Hackel  * this is lame, but here's how recovery works...
2076714d8e8SKurt Hackel  * 1) all recovery threads cluster wide will work on recovering
2086714d8e8SKurt Hackel  *    ONE node at a time
2096714d8e8SKurt Hackel  * 2) negotiate who will take over all the locks for the dead node.
2106714d8e8SKurt Hackel  *    thats right... ALL the locks.
2116714d8e8SKurt Hackel  * 3) once a new master is chosen, everyone scans all locks
2126714d8e8SKurt Hackel  *    and moves aside those mastered by the dead guy
2136714d8e8SKurt Hackel  * 4) each of these locks should be locked until recovery is done
2146714d8e8SKurt Hackel  * 5) the new master collects up all of secondary lock queue info
2156714d8e8SKurt Hackel  *    one lock at a time, forcing each node to communicate back
2166714d8e8SKurt Hackel  *    before continuing
2176714d8e8SKurt Hackel  * 6) each secondary lock queue responds with the full known lock info
2186714d8e8SKurt Hackel  * 7) once the new master has run all its locks, it sends a ALLDONE!
2196714d8e8SKurt Hackel  *    message to everyone
2206714d8e8SKurt Hackel  * 8) upon receiving this message, the secondary queue node unlocks
2216714d8e8SKurt Hackel  *    and responds to the ALLDONE
2226714d8e8SKurt Hackel  * 9) once the new master gets responses from everyone, he unlocks
2236714d8e8SKurt Hackel  *    everything and recovery for this dead node is done
2246714d8e8SKurt Hackel  *10) go back to 2) while there are still dead nodes
2256714d8e8SKurt Hackel  *
2266714d8e8SKurt Hackel  */
2276714d8e8SKurt Hackel 
dlm_print_reco_node_status(struct dlm_ctxt * dlm)228d6dea6e9SKurt Hackel static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
229d6dea6e9SKurt Hackel {
230d6dea6e9SKurt Hackel 	struct dlm_reco_node_data *ndata;
231d6dea6e9SKurt Hackel 	struct dlm_lock_resource *res;
232d6dea6e9SKurt Hackel 
233d6dea6e9SKurt Hackel 	mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
234ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
235d6dea6e9SKurt Hackel 	     dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
236d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
237d6dea6e9SKurt Hackel 
238d6dea6e9SKurt Hackel 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
239d6dea6e9SKurt Hackel 		char *st = "unknown";
240d6dea6e9SKurt Hackel 		switch (ndata->state) {
241d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
242d6dea6e9SKurt Hackel 				st = "init";
243d6dea6e9SKurt Hackel 				break;
244d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
245d6dea6e9SKurt Hackel 				st = "requesting";
246d6dea6e9SKurt Hackel 				break;
247d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
248d6dea6e9SKurt Hackel 				st = "dead";
249d6dea6e9SKurt Hackel 				break;
250d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
251d6dea6e9SKurt Hackel 				st = "receiving";
252d6dea6e9SKurt Hackel 				break;
253d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
254d6dea6e9SKurt Hackel 				st = "requested";
255d6dea6e9SKurt Hackel 				break;
256d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
257d6dea6e9SKurt Hackel 				st = "done";
258d6dea6e9SKurt Hackel 				break;
259d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
260d6dea6e9SKurt Hackel 				st = "finalize-sent";
261d6dea6e9SKurt Hackel 				break;
262d6dea6e9SKurt Hackel 			default:
263d6dea6e9SKurt Hackel 				st = "bad";
264d6dea6e9SKurt Hackel 				break;
265d6dea6e9SKurt Hackel 		}
266d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
267d6dea6e9SKurt Hackel 		     dlm->name, ndata->node_num, st);
268d6dea6e9SKurt Hackel 	}
269d6dea6e9SKurt Hackel 	list_for_each_entry(res, &dlm->reco.resources, recovering) {
270d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
271d6dea6e9SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
272d6dea6e9SKurt Hackel 	}
273d6dea6e9SKurt Hackel }
2746714d8e8SKurt Hackel 
2756714d8e8SKurt Hackel #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
2766714d8e8SKurt Hackel 
dlm_recovery_thread(void * data)2776714d8e8SKurt Hackel static int dlm_recovery_thread(void *data)
2786714d8e8SKurt Hackel {
2796714d8e8SKurt Hackel 	int status;
2806714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
2816714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
2826714d8e8SKurt Hackel 
2836714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
2846714d8e8SKurt Hackel 
2856714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
286bc9838c4SSrinivas Eeda 		if (dlm_domain_fully_joined(dlm)) {
2876714d8e8SKurt Hackel 			status = dlm_do_recovery(dlm);
2886714d8e8SKurt Hackel 			if (status == -EAGAIN) {
2896714d8e8SKurt Hackel 				/* do not sleep, recheck immediately. */
2906714d8e8SKurt Hackel 				continue;
2916714d8e8SKurt Hackel 			}
2926714d8e8SKurt Hackel 			if (status < 0)
2936714d8e8SKurt Hackel 				mlog_errno(status);
2946714d8e8SKurt Hackel 		}
2956714d8e8SKurt Hackel 
2966714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
2976714d8e8SKurt Hackel 						 kthread_should_stop(),
2986714d8e8SKurt Hackel 						 timeout);
2996714d8e8SKurt Hackel 	}
3006714d8e8SKurt Hackel 
3016714d8e8SKurt Hackel 	mlog(0, "quitting DLM recovery thread\n");
3026714d8e8SKurt Hackel 	return 0;
3036714d8e8SKurt Hackel }
3046714d8e8SKurt Hackel 
305e2faea4cSKurt Hackel /* returns true when the recovery master has contacted us */
dlm_reco_master_ready(struct dlm_ctxt * dlm)306e2faea4cSKurt Hackel static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
307e2faea4cSKurt Hackel {
308e2faea4cSKurt Hackel 	int ready;
309e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
310e2faea4cSKurt Hackel 	ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
311e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
312e2faea4cSKurt Hackel 	return ready;
313e2faea4cSKurt Hackel }
314e2faea4cSKurt Hackel 
315e2faea4cSKurt Hackel /* returns true if node is no longer in the domain
316e2faea4cSKurt Hackel  * could be dead or just not joined */
dlm_is_node_dead(struct dlm_ctxt * dlm,u8 node)317e2faea4cSKurt Hackel int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
318e2faea4cSKurt Hackel {
319e2faea4cSKurt Hackel 	int dead;
320e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
321aba9aac7SKurt Hackel 	dead = !test_bit(node, dlm->domain_map);
322e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
323e2faea4cSKurt Hackel 	return dead;
324e2faea4cSKurt Hackel }
325e2faea4cSKurt Hackel 
326b7084ab5SKurt Hackel /* returns true if node is no longer in the domain
327b7084ab5SKurt Hackel  * could be dead or just not joined */
dlm_is_node_recovered(struct dlm_ctxt * dlm,u8 node)3283fb5a989SAdrian Bunk static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
329b7084ab5SKurt Hackel {
330b7084ab5SKurt Hackel 	int recovered;
331b7084ab5SKurt Hackel 	spin_lock(&dlm->spinlock);
332b7084ab5SKurt Hackel 	recovered = !test_bit(node, dlm->recovery_map);
333b7084ab5SKurt Hackel 	spin_unlock(&dlm->spinlock);
334b7084ab5SKurt Hackel 	return recovered;
335b7084ab5SKurt Hackel }
336b7084ab5SKurt Hackel 
337b7084ab5SKurt Hackel 
dlm_wait_for_node_death(struct dlm_ctxt * dlm,u8 node,int timeout)338ed8625c6SSunil Mushran void dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
33944465a7dSKurt Hackel {
340ed8625c6SSunil Mushran 	if (dlm_is_node_dead(dlm, node))
341ed8625c6SSunil Mushran 		return;
342ed8625c6SSunil Mushran 
343ed8625c6SSunil Mushran 	printk(KERN_NOTICE "o2dlm: Waiting on the death of node %u in "
344ed8625c6SSunil Mushran 	       "domain %s\n", node, dlm->name);
345ed8625c6SSunil Mushran 
346ed8625c6SSunil Mushran 	if (timeout)
34744465a7dSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
34844465a7dSKurt Hackel 				   dlm_is_node_dead(dlm, node),
34944465a7dSKurt Hackel 				   msecs_to_jiffies(timeout));
350ed8625c6SSunil Mushran 	else
35144465a7dSKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
35244465a7dSKurt Hackel 			   dlm_is_node_dead(dlm, node));
35344465a7dSKurt Hackel }
35444465a7dSKurt Hackel 
dlm_wait_for_node_recovery(struct dlm_ctxt * dlm,u8 node,int timeout)355ed8625c6SSunil Mushran void dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
356b7084ab5SKurt Hackel {
357ed8625c6SSunil Mushran 	if (dlm_is_node_recovered(dlm, node))
358ed8625c6SSunil Mushran 		return;
359ed8625c6SSunil Mushran 
360ed8625c6SSunil Mushran 	printk(KERN_NOTICE "o2dlm: Waiting on the recovery of node %u in "
361ed8625c6SSunil Mushran 	       "domain %s\n", node, dlm->name);
362ed8625c6SSunil Mushran 
363ed8625c6SSunil Mushran 	if (timeout)
364b7084ab5SKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
365b7084ab5SKurt Hackel 				   dlm_is_node_recovered(dlm, node),
366b7084ab5SKurt Hackel 				   msecs_to_jiffies(timeout));
367ed8625c6SSunil Mushran 	else
368b7084ab5SKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
369b7084ab5SKurt Hackel 			   dlm_is_node_recovered(dlm, node));
370b7084ab5SKurt Hackel }
371b7084ab5SKurt Hackel 
3726714d8e8SKurt Hackel /* callers of the top-level api calls (dlmlock/dlmunlock) should
3736714d8e8SKurt Hackel  * block on the dlm->reco.event when recovery is in progress.
3746714d8e8SKurt Hackel  * the dlm recovery thread will set this state when it begins
3756714d8e8SKurt Hackel  * recovering a dead node (as the new master or not) and clear
3766714d8e8SKurt Hackel  * the state and wake as soon as all affected lock resources have
3776714d8e8SKurt Hackel  * been marked with the RECOVERY flag */
dlm_in_recovery(struct dlm_ctxt * dlm)3786714d8e8SKurt Hackel static int dlm_in_recovery(struct dlm_ctxt *dlm)
3796714d8e8SKurt Hackel {
3806714d8e8SKurt Hackel 	int in_recovery;
3816714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
3826714d8e8SKurt Hackel 	in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
3836714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3846714d8e8SKurt Hackel 	return in_recovery;
3856714d8e8SKurt Hackel }
3866714d8e8SKurt Hackel 
3876714d8e8SKurt Hackel 
dlm_wait_for_recovery(struct dlm_ctxt * dlm)3886714d8e8SKurt Hackel void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
3896714d8e8SKurt Hackel {
39056a7c104SKurt Hackel 	if (dlm_in_recovery(dlm)) {
3913b3b84a8SKurt Hackel 		mlog(0, "%s: reco thread %d in recovery: "
39256a7c104SKurt Hackel 		     "state=%d, master=%u, dead=%u\n",
393ba25f9dcSPavel Emelyanov 		     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
39456a7c104SKurt Hackel 		     dlm->reco.state, dlm->reco.new_master,
39556a7c104SKurt Hackel 		     dlm->reco.dead_node);
39656a7c104SKurt Hackel 	}
3976714d8e8SKurt Hackel 	wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
3986714d8e8SKurt Hackel }
3996714d8e8SKurt Hackel 
dlm_begin_recovery(struct dlm_ctxt * dlm)4006714d8e8SKurt Hackel static void dlm_begin_recovery(struct dlm_ctxt *dlm)
4016714d8e8SKurt Hackel {
40260c7ec9eSpiaojun 	assert_spin_locked(&dlm->spinlock);
4036714d8e8SKurt Hackel 	BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
4048decab3cSSunil Mushran 	printk(KERN_NOTICE "o2dlm: Begin recovery on domain %s for node %u\n",
4058decab3cSSunil Mushran 	       dlm->name, dlm->reco.dead_node);
4066714d8e8SKurt Hackel 	dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
4076714d8e8SKurt Hackel }
4086714d8e8SKurt Hackel 
dlm_end_recovery(struct dlm_ctxt * dlm)4096714d8e8SKurt Hackel static void dlm_end_recovery(struct dlm_ctxt *dlm)
4106714d8e8SKurt Hackel {
4116714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4126714d8e8SKurt Hackel 	BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
4136714d8e8SKurt Hackel 	dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
4146714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4158decab3cSSunil Mushran 	printk(KERN_NOTICE "o2dlm: End recovery on domain %s\n", dlm->name);
4166714d8e8SKurt Hackel 	wake_up(&dlm->reco.event);
4176714d8e8SKurt Hackel }
4186714d8e8SKurt Hackel 
dlm_print_recovery_master(struct dlm_ctxt * dlm)4198decab3cSSunil Mushran static void dlm_print_recovery_master(struct dlm_ctxt *dlm)
4208decab3cSSunil Mushran {
4218decab3cSSunil Mushran 	printk(KERN_NOTICE "o2dlm: Node %u (%s) is the Recovery Master for the "
4228decab3cSSunil Mushran 	       "dead node %u in domain %s\n", dlm->reco.new_master,
4238decab3cSSunil Mushran 	       (dlm->node_num == dlm->reco.new_master ? "me" : "he"),
4248decab3cSSunil Mushran 	       dlm->reco.dead_node, dlm->name);
4258decab3cSSunil Mushran }
4268decab3cSSunil Mushran 
dlm_do_recovery(struct dlm_ctxt * dlm)4276714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm)
4286714d8e8SKurt Hackel {
4296714d8e8SKurt Hackel 	int status = 0;
430e2faea4cSKurt Hackel 	int ret;
4316714d8e8SKurt Hackel 
4326714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4336714d8e8SKurt Hackel 
43460c7ec9eSpiaojun 	if (dlm->migrate_done) {
43560c7ec9eSpiaojun 		mlog(0, "%s: no need do recovery after migrating all "
43660c7ec9eSpiaojun 		     "lock resources\n", dlm->name);
43760c7ec9eSpiaojun 		spin_unlock(&dlm->spinlock);
43860c7ec9eSpiaojun 		return 0;
43960c7ec9eSpiaojun 	}
44060c7ec9eSpiaojun 
4416714d8e8SKurt Hackel 	/* check to see if the new master has died */
4426714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
4436714d8e8SKurt Hackel 	    test_bit(dlm->reco.new_master, dlm->recovery_map)) {
4446714d8e8SKurt Hackel 		mlog(0, "new master %u died while recovering %u!\n",
4456714d8e8SKurt Hackel 		     dlm->reco.new_master, dlm->reco.dead_node);
4466714d8e8SKurt Hackel 		/* unset the new_master, leave dead_node */
447ab27eb6fSKurt Hackel 		dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
4486714d8e8SKurt Hackel 	}
4496714d8e8SKurt Hackel 
4506714d8e8SKurt Hackel 	/* select a target to recover */
4516714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4526714d8e8SKurt Hackel 		int bit;
4536714d8e8SKurt Hackel 
454b5c7e7ecSYury Norov 		bit = find_first_bit(dlm->recovery_map, O2NM_MAX_NODES);
4556714d8e8SKurt Hackel 		if (bit >= O2NM_MAX_NODES || bit < 0)
456ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4576714d8e8SKurt Hackel 		else
458ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, bit);
4596714d8e8SKurt Hackel 	} else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
4606714d8e8SKurt Hackel 		/* BUG? */
4616714d8e8SKurt Hackel 		mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
4626714d8e8SKurt Hackel 		     dlm->reco.dead_node);
463ab27eb6fSKurt Hackel 		dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4646714d8e8SKurt Hackel 	}
4656714d8e8SKurt Hackel 
4666714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4676714d8e8SKurt Hackel 		// mlog(0, "nothing to recover!  sleeping now!\n");
4686714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
4696714d8e8SKurt Hackel 		/* return to main thread loop and sleep. */
4706714d8e8SKurt Hackel 		return 0;
4716714d8e8SKurt Hackel 	}
472d6dea6e9SKurt Hackel 	mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
473ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
4746714d8e8SKurt Hackel 	     dlm->reco.dead_node);
4756714d8e8SKurt Hackel 
4766714d8e8SKurt Hackel 	/* take write barrier */
4776714d8e8SKurt Hackel 	/* (stops the list reshuffling thread, proxy ast handling) */
4786714d8e8SKurt Hackel 	dlm_begin_recovery(dlm);
4796714d8e8SKurt Hackel 
48060c7ec9eSpiaojun 	spin_unlock(&dlm->spinlock);
48160c7ec9eSpiaojun 
4826714d8e8SKurt Hackel 	if (dlm->reco.new_master == dlm->node_num)
4836714d8e8SKurt Hackel 		goto master_here;
4846714d8e8SKurt Hackel 
4856714d8e8SKurt Hackel 	if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
486e2faea4cSKurt Hackel 		/* choose a new master, returns 0 if this node
487e2faea4cSKurt Hackel 		 * is the master, -EEXIST if it's another node.
488e2faea4cSKurt Hackel 		 * this does not return until a new master is chosen
489e2faea4cSKurt Hackel 		 * or recovery completes entirely. */
490e2faea4cSKurt Hackel 		ret = dlm_pick_recovery_master(dlm);
491e2faea4cSKurt Hackel 		if (!ret) {
4926714d8e8SKurt Hackel 			/* already notified everyone.  go. */
4936714d8e8SKurt Hackel 			goto master_here;
4946714d8e8SKurt Hackel 		}
4956714d8e8SKurt Hackel 		mlog(0, "another node will master this recovery session.\n");
4966714d8e8SKurt Hackel 	}
4978decab3cSSunil Mushran 
4988decab3cSSunil Mushran 	dlm_print_recovery_master(dlm);
4996714d8e8SKurt Hackel 
5006714d8e8SKurt Hackel 	/* it is safe to start everything back up here
5016714d8e8SKurt Hackel 	 * because all of the dead node's lock resources
5026714d8e8SKurt Hackel 	 * have been marked as in-recovery */
5036714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5046714d8e8SKurt Hackel 
5056714d8e8SKurt Hackel 	/* sleep out in main dlm_recovery_thread loop. */
5066714d8e8SKurt Hackel 	return 0;
5076714d8e8SKurt Hackel 
5086714d8e8SKurt Hackel master_here:
5098decab3cSSunil Mushran 	dlm_print_recovery_master(dlm);
5106714d8e8SKurt Hackel 
5116714d8e8SKurt Hackel 	status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
5126714d8e8SKurt Hackel 	if (status < 0) {
5136a413211SKurt Hackel 		/* we should never hit this anymore */
5148decab3cSSunil Mushran 		mlog(ML_ERROR, "%s: Error %d remastering locks for node %u, "
5158decab3cSSunil Mushran 		     "retrying.\n", dlm->name, status, dlm->reco.dead_node);
516e2faea4cSKurt Hackel 		/* yield a bit to allow any final network messages
517e2faea4cSKurt Hackel 		 * to get handled on remaining nodes */
518e2faea4cSKurt Hackel 		msleep(100);
5196714d8e8SKurt Hackel 	} else {
5206714d8e8SKurt Hackel 		/* success!  see if any other nodes need recovery */
521e2faea4cSKurt Hackel 		mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
522e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dlm->node_num);
523ded2cf71SJunxiao Bi 		spin_lock(&dlm->spinlock);
524ded2cf71SJunxiao Bi 		__dlm_reset_recovery(dlm);
525ded2cf71SJunxiao Bi 		dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
526ded2cf71SJunxiao Bi 		spin_unlock(&dlm->spinlock);
5276714d8e8SKurt Hackel 	}
5286714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5296714d8e8SKurt Hackel 
5306714d8e8SKurt Hackel 	/* continue and look for another dead node */
5316714d8e8SKurt Hackel 	return -EAGAIN;
5326714d8e8SKurt Hackel }
5336714d8e8SKurt Hackel 
dlm_remaster_locks(struct dlm_ctxt * dlm,u8 dead_node)5346714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
5356714d8e8SKurt Hackel {
5366714d8e8SKurt Hackel 	int status = 0;
5376714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
5386714d8e8SKurt Hackel 	int all_nodes_done;
5396714d8e8SKurt Hackel 	int destroy = 0;
5406714d8e8SKurt Hackel 	int pass = 0;
5416714d8e8SKurt Hackel 
5426a413211SKurt Hackel 	do {
5436a413211SKurt Hackel 		/* we have become recovery master.  there is no escaping
5446a413211SKurt Hackel 		 * this, so just keep trying until we get it. */
5456714d8e8SKurt Hackel 		status = dlm_init_recovery_area(dlm, dead_node);
5466a413211SKurt Hackel 		if (status < 0) {
5476a413211SKurt Hackel 			mlog(ML_ERROR, "%s: failed to alloc recovery area, "
5486a413211SKurt Hackel 			     "retrying\n", dlm->name);
5496a413211SKurt Hackel 			msleep(1000);
5506a413211SKurt Hackel 		}
5516a413211SKurt Hackel 	} while (status != 0);
5526714d8e8SKurt Hackel 
5536714d8e8SKurt Hackel 	/* safe to access the node data list without a lock, since this
5546714d8e8SKurt Hackel 	 * process is the only one to change the list */
555800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
5566714d8e8SKurt Hackel 		BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
5576714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
5586714d8e8SKurt Hackel 
5598decab3cSSunil Mushran 		mlog(0, "%s: Requesting lock info from node %u\n", dlm->name,
5606714d8e8SKurt Hackel 		     ndata->node_num);
5616714d8e8SKurt Hackel 
5626714d8e8SKurt Hackel 		if (ndata->node_num == dlm->node_num) {
5636714d8e8SKurt Hackel 			ndata->state = DLM_RECO_NODE_DATA_DONE;
5646714d8e8SKurt Hackel 			continue;
5656714d8e8SKurt Hackel 		}
5666714d8e8SKurt Hackel 
5676a413211SKurt Hackel 		do {
5686a413211SKurt Hackel 			status = dlm_request_all_locks(dlm, ndata->node_num,
5696a413211SKurt Hackel 						       dead_node);
5706714d8e8SKurt Hackel 			if (status < 0) {
5716714d8e8SKurt Hackel 				mlog_errno(status);
5726a413211SKurt Hackel 				if (dlm_is_host_down(status)) {
5736a413211SKurt Hackel 					/* node died, ignore it for recovery */
5746a413211SKurt Hackel 					status = 0;
5756714d8e8SKurt Hackel 					ndata->state = DLM_RECO_NODE_DATA_DEAD;
5766a413211SKurt Hackel 					/* wait for the domain map to catch up
5776a413211SKurt Hackel 					 * with the network state. */
5786a413211SKurt Hackel 					wait_event_timeout(dlm->dlm_reco_thread_wq,
5796a413211SKurt Hackel 							   dlm_is_node_dead(dlm,
5806a413211SKurt Hackel 								ndata->node_num),
5816a413211SKurt Hackel 							   msecs_to_jiffies(1000));
5826a413211SKurt Hackel 					mlog(0, "waited 1 sec for %u, "
5836a413211SKurt Hackel 					     "dead? %s\n", ndata->node_num,
5846a413211SKurt Hackel 					     dlm_is_node_dead(dlm, ndata->node_num) ?
5856a413211SKurt Hackel 					     "yes" : "no");
5866a413211SKurt Hackel 				} else {
5876a413211SKurt Hackel 					/* -ENOMEM on the other node */
5886a413211SKurt Hackel 					mlog(0, "%s: node %u returned "
5896a413211SKurt Hackel 					     "%d during recovery, retrying "
5906a413211SKurt Hackel 					     "after a short wait\n",
5916a413211SKurt Hackel 					     dlm->name, ndata->node_num,
5926a413211SKurt Hackel 					     status);
5936a413211SKurt Hackel 					msleep(100);
5946714d8e8SKurt Hackel 				}
5956714d8e8SKurt Hackel 			}
5966a413211SKurt Hackel 		} while (status != 0);
5976714d8e8SKurt Hackel 
598756a1501SSrinivas Eeda 		spin_lock(&dlm_reco_state_lock);
5996714d8e8SKurt Hackel 		switch (ndata->state) {
6006714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
6016714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
6026714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
6036714d8e8SKurt Hackel 				BUG();
6046714d8e8SKurt Hackel 				break;
6056714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
6066714d8e8SKurt Hackel 				mlog(0, "node %u died after requesting "
6076714d8e8SKurt Hackel 				     "recovery info for node %u\n",
6086714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6096a413211SKurt Hackel 				/* fine.  don't need this node's info.
6106a413211SKurt Hackel 				 * continue without it. */
6116a413211SKurt Hackel 				break;
6126714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
6136714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
6146714d8e8SKurt Hackel 				mlog(0, "now receiving recovery data from "
6156714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6166714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6176714d8e8SKurt Hackel 				break;
6186714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
6196714d8e8SKurt Hackel 				mlog(0, "already receiving recovery data from "
6206714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6216714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6226714d8e8SKurt Hackel 				break;
6236714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
6246714d8e8SKurt Hackel 				mlog(0, "already DONE receiving recovery data "
6256714d8e8SKurt Hackel 				     "from node %u for dead node %u\n",
6266714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6276714d8e8SKurt Hackel 				break;
6286714d8e8SKurt Hackel 		}
629756a1501SSrinivas Eeda 		spin_unlock(&dlm_reco_state_lock);
6306714d8e8SKurt Hackel 	}
6316714d8e8SKurt Hackel 
6328decab3cSSunil Mushran 	mlog(0, "%s: Done requesting all lock info\n", dlm->name);
6336714d8e8SKurt Hackel 
6346714d8e8SKurt Hackel 	/* nodes should be sending reco data now
6356714d8e8SKurt Hackel 	 * just need to wait */
6366714d8e8SKurt Hackel 
6376714d8e8SKurt Hackel 	while (1) {
6386714d8e8SKurt Hackel 		/* check all the nodes now to see if we are
6396714d8e8SKurt Hackel 		 * done, or if anyone died */
6406714d8e8SKurt Hackel 		all_nodes_done = 1;
6416714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
642800deef3SChristoph Hellwig 		list_for_each_entry(ndata, &dlm->reco.node_data, list) {
6436714d8e8SKurt Hackel 			mlog(0, "checking recovery state of node %u\n",
6446714d8e8SKurt Hackel 			     ndata->node_num);
6456714d8e8SKurt Hackel 			switch (ndata->state) {
6466714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_INIT:
6476714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTING:
6486714d8e8SKurt Hackel 					mlog(ML_ERROR, "bad ndata state for "
6496714d8e8SKurt Hackel 					     "node %u: state=%d\n",
6506714d8e8SKurt Hackel 					     ndata->node_num, ndata->state);
6516714d8e8SKurt Hackel 					BUG();
6526714d8e8SKurt Hackel 					break;
6536714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DEAD:
6546a413211SKurt Hackel 					mlog(0, "node %u died after "
6556714d8e8SKurt Hackel 					     "requesting recovery info for "
6566714d8e8SKurt Hackel 					     "node %u\n", ndata->node_num,
6576714d8e8SKurt Hackel 					     dead_node);
6586a413211SKurt Hackel 					break;
6596714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_RECEIVING:
6606714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTED:
661d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u still in state %s\n",
662d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num,
663d6dea6e9SKurt Hackel 					     ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
664d6dea6e9SKurt Hackel 					     "receiving" : "requested");
6656714d8e8SKurt Hackel 					all_nodes_done = 0;
6666714d8e8SKurt Hackel 					break;
6676714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DONE:
668d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is done\n",
669d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6706714d8e8SKurt Hackel 					break;
6716714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_FINALIZE_SENT:
672d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is finalize\n",
673d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6746714d8e8SKurt Hackel 					break;
6756714d8e8SKurt Hackel 			}
6766714d8e8SKurt Hackel 		}
6776714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
6786714d8e8SKurt Hackel 
6796714d8e8SKurt Hackel 		mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
6806714d8e8SKurt Hackel 		     all_nodes_done?"yes":"no");
6816714d8e8SKurt Hackel 		if (all_nodes_done) {
6826714d8e8SKurt Hackel 			int ret;
6836714d8e8SKurt Hackel 
684ded2cf71SJunxiao Bi 			/* Set this flag on recovery master to avoid
685ded2cf71SJunxiao Bi 			 * a new recovery for another dead node start
686ded2cf71SJunxiao Bi 			 * before the recovery is not done. That may
687ded2cf71SJunxiao Bi 			 * cause recovery hung.*/
688ded2cf71SJunxiao Bi 			spin_lock(&dlm->spinlock);
689ded2cf71SJunxiao Bi 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
690ded2cf71SJunxiao Bi 			spin_unlock(&dlm->spinlock);
691ded2cf71SJunxiao Bi 
6926714d8e8SKurt Hackel 			/* all nodes are now in DLM_RECO_NODE_DATA_DONE state
6936714d8e8SKurt Hackel 	 		 * just send a finalize message to everyone and
6946714d8e8SKurt Hackel 	 		 * clean up */
6956714d8e8SKurt Hackel 			mlog(0, "all nodes are done! send finalize\n");
6966714d8e8SKurt Hackel 			ret = dlm_send_finalize_reco_message(dlm);
6976714d8e8SKurt Hackel 			if (ret < 0)
6986714d8e8SKurt Hackel 				mlog_errno(ret);
6996714d8e8SKurt Hackel 
7006714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7016714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, dead_node,
7026714d8e8SKurt Hackel 							  dlm->node_num);
7036714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
7046714d8e8SKurt Hackel 			mlog(0, "should be done with recovery!\n");
7056714d8e8SKurt Hackel 
7066714d8e8SKurt Hackel 			mlog(0, "finishing recovery of %s at %lu, "
7076714d8e8SKurt Hackel 			     "dead=%u, this=%u, new=%u\n", dlm->name,
7086714d8e8SKurt Hackel 			     jiffies, dlm->reco.dead_node,
7096714d8e8SKurt Hackel 			     dlm->node_num, dlm->reco.new_master);
7106714d8e8SKurt Hackel 			destroy = 1;
7116a413211SKurt Hackel 			status = 0;
7126714d8e8SKurt Hackel 			/* rescan everything marked dirty along the way */
7136714d8e8SKurt Hackel 			dlm_kick_thread(dlm, NULL);
7146714d8e8SKurt Hackel 			break;
7156714d8e8SKurt Hackel 		}
7166714d8e8SKurt Hackel 		/* wait to be signalled, with periodic timeout
7176714d8e8SKurt Hackel 		 * to check for node death */
7186714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
7196714d8e8SKurt Hackel 					 kthread_should_stop(),
7206714d8e8SKurt Hackel 					 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
7216714d8e8SKurt Hackel 
7226714d8e8SKurt Hackel 	}
7236714d8e8SKurt Hackel 
7246714d8e8SKurt Hackel 	if (destroy)
7252bcb654cSChangwei Ge 		dlm_destroy_recovery_area(dlm);
7266714d8e8SKurt Hackel 
7276714d8e8SKurt Hackel 	return status;
7286714d8e8SKurt Hackel }
7296714d8e8SKurt Hackel 
dlm_init_recovery_area(struct dlm_ctxt * dlm,u8 dead_node)7306714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
7316714d8e8SKurt Hackel {
7326714d8e8SKurt Hackel 	int num=0;
7336714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
7346714d8e8SKurt Hackel 
7356714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
736*b270f492SJoseph Qi 	bitmap_copy(dlm->reco.node_map, dlm->domain_map, O2NM_MAX_NODES);
7376714d8e8SKurt Hackel 	/* nodes can only be removed (by dying) after dropping
7386714d8e8SKurt Hackel 	 * this lock, and death will be trapped later, so this should do */
7396714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
7406714d8e8SKurt Hackel 
7416714d8e8SKurt Hackel 	while (1) {
7426714d8e8SKurt Hackel 		num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
7436714d8e8SKurt Hackel 		if (num >= O2NM_MAX_NODES) {
7446714d8e8SKurt Hackel 			break;
7456714d8e8SKurt Hackel 		}
7466714d8e8SKurt Hackel 		BUG_ON(num == dead_node);
7476714d8e8SKurt Hackel 
748cd861280SRobert P. J. Day 		ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
7496714d8e8SKurt Hackel 		if (!ndata) {
7502bcb654cSChangwei Ge 			dlm_destroy_recovery_area(dlm);
7516714d8e8SKurt Hackel 			return -ENOMEM;
7526714d8e8SKurt Hackel 		}
7536714d8e8SKurt Hackel 		ndata->node_num = num;
7546714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_INIT;
7556714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
7566714d8e8SKurt Hackel 		list_add_tail(&ndata->list, &dlm->reco.node_data);
7576714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
7586714d8e8SKurt Hackel 		num++;
7596714d8e8SKurt Hackel 	}
7606714d8e8SKurt Hackel 
7616714d8e8SKurt Hackel 	return 0;
7626714d8e8SKurt Hackel }
7636714d8e8SKurt Hackel 
dlm_destroy_recovery_area(struct dlm_ctxt * dlm)7642bcb654cSChangwei Ge static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm)
7656714d8e8SKurt Hackel {
766800deef3SChristoph Hellwig 	struct dlm_reco_node_data *ndata, *next;
7676714d8e8SKurt Hackel 	LIST_HEAD(tmplist);
7686714d8e8SKurt Hackel 
7696714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
7706714d8e8SKurt Hackel 	list_splice_init(&dlm->reco.node_data, &tmplist);
7716714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
7726714d8e8SKurt Hackel 
773800deef3SChristoph Hellwig 	list_for_each_entry_safe(ndata, next, &tmplist, list) {
7746714d8e8SKurt Hackel 		list_del_init(&ndata->list);
7756714d8e8SKurt Hackel 		kfree(ndata);
7766714d8e8SKurt Hackel 	}
7776714d8e8SKurt Hackel }
7786714d8e8SKurt Hackel 
dlm_request_all_locks(struct dlm_ctxt * dlm,u8 request_from,u8 dead_node)7796714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
7806714d8e8SKurt Hackel 				 u8 dead_node)
7816714d8e8SKurt Hackel {
7826714d8e8SKurt Hackel 	struct dlm_lock_request lr;
78322ab9014SJoseph Qi 	int ret;
78498ac9125SXue jiufei 	int status;
7856714d8e8SKurt Hackel 
7866714d8e8SKurt Hackel 	mlog(0, "\n");
7876714d8e8SKurt Hackel 
7886714d8e8SKurt Hackel 
7896714d8e8SKurt Hackel 	mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
7906714d8e8SKurt Hackel 		  "to %u\n", dead_node, request_from);
7916714d8e8SKurt Hackel 
7926714d8e8SKurt Hackel 	memset(&lr, 0, sizeof(lr));
7936714d8e8SKurt Hackel 	lr.node_idx = dlm->node_num;
7946714d8e8SKurt Hackel 	lr.dead_node = dead_node;
7956714d8e8SKurt Hackel 
7966714d8e8SKurt Hackel 	// send message
7976714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
79898ac9125SXue jiufei 				 &lr, sizeof(lr), request_from, &status);
7996714d8e8SKurt Hackel 
8006714d8e8SKurt Hackel 	/* negative status is handled by caller */
8016714d8e8SKurt Hackel 	if (ret < 0)
8028decab3cSSunil Mushran 		mlog(ML_ERROR, "%s: Error %d send LOCK_REQUEST to node %u "
8038decab3cSSunil Mushran 		     "to recover dead node %u\n", dlm->name, ret,
8048decab3cSSunil Mushran 		     request_from, dead_node);
80598ac9125SXue jiufei 	else
80698ac9125SXue jiufei 		ret = status;
8076714d8e8SKurt Hackel 	// return from here, then
8086714d8e8SKurt Hackel 	// sleep until all received or error
8096714d8e8SKurt Hackel 	return ret;
8106714d8e8SKurt Hackel 
8116714d8e8SKurt Hackel }
8126714d8e8SKurt Hackel 
dlm_request_all_locks_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)813d74c9803SKurt Hackel int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
814d74c9803SKurt Hackel 				  void **ret_data)
8156714d8e8SKurt Hackel {
8166714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
8176714d8e8SKurt Hackel 	struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
8186714d8e8SKurt Hackel 	char *buf = NULL;
8196714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
8206714d8e8SKurt Hackel 
8216714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
8226714d8e8SKurt Hackel 		return -EINVAL;
8236714d8e8SKurt Hackel 
824c3187ce5SKurt Hackel 	if (lr->dead_node != dlm->reco.dead_node) {
825c3187ce5SKurt Hackel 		mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
826c3187ce5SKurt Hackel 		     "dead_node is %u\n", dlm->name, lr->node_idx,
827c3187ce5SKurt Hackel 		     lr->dead_node, dlm->reco.dead_node);
828d6dea6e9SKurt Hackel 		dlm_print_reco_node_status(dlm);
829c3187ce5SKurt Hackel 		/* this is a hack */
830c3187ce5SKurt Hackel 		dlm_put(dlm);
831c3187ce5SKurt Hackel 		return -ENOMEM;
832c3187ce5SKurt Hackel 	}
8336714d8e8SKurt Hackel 	BUG_ON(lr->dead_node != dlm->reco.dead_node);
8346714d8e8SKurt Hackel 
835cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
8366714d8e8SKurt Hackel 	if (!item) {
8376714d8e8SKurt Hackel 		dlm_put(dlm);
8386714d8e8SKurt Hackel 		return -ENOMEM;
8396714d8e8SKurt Hackel 	}
8406714d8e8SKurt Hackel 
8416714d8e8SKurt Hackel 	/* this will get freed by dlm_request_all_locks_worker */
842ad8100e0SKurt Hackel 	buf = (char *) __get_free_page(GFP_NOFS);
8436714d8e8SKurt Hackel 	if (!buf) {
8446714d8e8SKurt Hackel 		kfree(item);
8456714d8e8SKurt Hackel 		dlm_put(dlm);
8466714d8e8SKurt Hackel 		return -ENOMEM;
8476714d8e8SKurt Hackel 	}
8486714d8e8SKurt Hackel 
8496714d8e8SKurt Hackel 	/* queue up work for dlm_request_all_locks_worker */
8506714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
8516714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
8526714d8e8SKurt Hackel 	item->u.ral.reco_master = lr->node_idx;
8536714d8e8SKurt Hackel 	item->u.ral.dead_node = lr->dead_node;
8546714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
8556714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
8566714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
8573156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
8586714d8e8SKurt Hackel 
8596714d8e8SKurt Hackel 	dlm_put(dlm);
8606714d8e8SKurt Hackel 	return 0;
8616714d8e8SKurt Hackel }
8626714d8e8SKurt Hackel 
dlm_request_all_locks_worker(struct dlm_work_item * item,void * data)8636714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
8646714d8e8SKurt Hackel {
8656714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
8666714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
8676714d8e8SKurt Hackel 	struct dlm_ctxt *dlm;
8686714d8e8SKurt Hackel 	LIST_HEAD(resources);
8696714d8e8SKurt Hackel 	int ret;
8706714d8e8SKurt Hackel 	u8 dead_node, reco_master;
87129c0fa0fSKurt Hackel 	int skip_all_done = 0;
8726714d8e8SKurt Hackel 
8736714d8e8SKurt Hackel 	dlm = item->dlm;
8746714d8e8SKurt Hackel 	dead_node = item->u.ral.dead_node;
8756714d8e8SKurt Hackel 	reco_master = item->u.ral.reco_master;
876e2faea4cSKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
877e2faea4cSKurt Hackel 
878d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
879d6dea6e9SKurt Hackel 	     dlm->name, dead_node, reco_master);
880d6dea6e9SKurt Hackel 
881e2faea4cSKurt Hackel 	if (dead_node != dlm->reco.dead_node ||
882e2faea4cSKurt Hackel 	    reco_master != dlm->reco.new_master) {
8836a413211SKurt Hackel 		/* worker could have been created before the recovery master
8846a413211SKurt Hackel 		 * died.  if so, do not continue, but do not error. */
8856a413211SKurt Hackel 		if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
8866a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: will not send recovery state, "
8876a413211SKurt Hackel 			     "recovery master %u died, thread=(dead=%u,mas=%u)"
8886a413211SKurt Hackel 			     " current=(dead=%u,mas=%u)\n", dlm->name,
8896a413211SKurt Hackel 			     reco_master, dead_node, reco_master,
8906a413211SKurt Hackel 			     dlm->reco.dead_node, dlm->reco.new_master);
8916a413211SKurt Hackel 		} else {
8926a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
8936a413211SKurt Hackel 			     "master=%u), request(dead=%u, master=%u)\n",
8946a413211SKurt Hackel 			     dlm->name, dlm->reco.dead_node,
8956a413211SKurt Hackel 			     dlm->reco.new_master, dead_node, reco_master);
896e2faea4cSKurt Hackel 		}
8976a413211SKurt Hackel 		goto leave;
8986a413211SKurt Hackel 	}
8996714d8e8SKurt Hackel 
9006714d8e8SKurt Hackel 	/* lock resources should have already been moved to the
9016714d8e8SKurt Hackel  	 * dlm->reco.resources list.  now move items from that list
9026714d8e8SKurt Hackel  	 * to a temp list if the dead owner matches.  note that the
9036714d8e8SKurt Hackel 	 * whole cluster recovers only one node at a time, so we
9046714d8e8SKurt Hackel 	 * can safely move UNKNOWN lock resources for each recovery
9056714d8e8SKurt Hackel 	 * session. */
9066714d8e8SKurt Hackel 	dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
9076714d8e8SKurt Hackel 
9086714d8e8SKurt Hackel 	/* now we can begin blasting lockreses without the dlm lock */
90929c0fa0fSKurt Hackel 
91029c0fa0fSKurt Hackel 	/* any errors returned will be due to the new_master dying,
91129c0fa0fSKurt Hackel 	 * the dlm_reco_thread should detect this */
912800deef3SChristoph Hellwig 	list_for_each_entry(res, &resources, recovering) {
9136714d8e8SKurt Hackel 		ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
9146714d8e8SKurt Hackel 				   	DLM_MRES_RECOVERY);
91529c0fa0fSKurt Hackel 		if (ret < 0) {
916d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
917d6dea6e9SKurt Hackel 			     "recovery state for dead node %u, ret=%d\n", dlm->name,
918d6dea6e9SKurt Hackel 			     reco_master, dead_node, ret);
91929c0fa0fSKurt Hackel 			skip_all_done = 1;
92029c0fa0fSKurt Hackel 			break;
92129c0fa0fSKurt Hackel 		}
9226714d8e8SKurt Hackel 	}
9236714d8e8SKurt Hackel 
9246714d8e8SKurt Hackel 	/* move the resources back to the list */
9256714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
9266714d8e8SKurt Hackel 	list_splice_init(&resources, &dlm->reco.resources);
9276714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
9286714d8e8SKurt Hackel 
92929c0fa0fSKurt Hackel 	if (!skip_all_done) {
9306714d8e8SKurt Hackel 		ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
93129c0fa0fSKurt Hackel 		if (ret < 0) {
932d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
933d6dea6e9SKurt Hackel 			     "recovery all-done for dead node %u, ret=%d\n",
934d6dea6e9SKurt Hackel 			     dlm->name, reco_master, dead_node, ret);
93529c0fa0fSKurt Hackel 		}
93629c0fa0fSKurt Hackel 	}
9376a413211SKurt Hackel leave:
9386714d8e8SKurt Hackel 	free_page((unsigned long)data);
9396714d8e8SKurt Hackel }
9406714d8e8SKurt Hackel 
9416714d8e8SKurt Hackel 
dlm_send_all_done_msg(struct dlm_ctxt * dlm,u8 dead_node,u8 send_to)9426714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
9436714d8e8SKurt Hackel {
9446714d8e8SKurt Hackel 	int ret, tmpret;
9456714d8e8SKurt Hackel 	struct dlm_reco_data_done done_msg;
9466714d8e8SKurt Hackel 
9476714d8e8SKurt Hackel 	memset(&done_msg, 0, sizeof(done_msg));
9486714d8e8SKurt Hackel 	done_msg.node_idx = dlm->node_num;
9496714d8e8SKurt Hackel 	done_msg.dead_node = dead_node;
9506714d8e8SKurt Hackel 	mlog(0, "sending DATA DONE message to %u, "
9516714d8e8SKurt Hackel 	     "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
9526714d8e8SKurt Hackel 	     done_msg.dead_node);
9536714d8e8SKurt Hackel 
9546714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
9556714d8e8SKurt Hackel 				 sizeof(done_msg), send_to, &tmpret);
95629c0fa0fSKurt Hackel 	if (ret < 0) {
9578decab3cSSunil Mushran 		mlog(ML_ERROR, "%s: Error %d send RECO_DATA_DONE to node %u "
9588decab3cSSunil Mushran 		     "to recover dead node %u\n", dlm->name, ret, send_to,
9598decab3cSSunil Mushran 		     dead_node);
96029c0fa0fSKurt Hackel 		if (!dlm_is_host_down(ret)) {
96129c0fa0fSKurt Hackel 			BUG();
96229c0fa0fSKurt Hackel 		}
96329c0fa0fSKurt Hackel 	} else
9646714d8e8SKurt Hackel 		ret = tmpret;
9656714d8e8SKurt Hackel 	return ret;
9666714d8e8SKurt Hackel }
9676714d8e8SKurt Hackel 
9686714d8e8SKurt Hackel 
dlm_reco_data_done_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)969d74c9803SKurt Hackel int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
970d74c9803SKurt Hackel 			       void **ret_data)
9716714d8e8SKurt Hackel {
9726714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
9736714d8e8SKurt Hackel 	struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
9746714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata = NULL;
9756714d8e8SKurt Hackel 	int ret = -EINVAL;
9766714d8e8SKurt Hackel 
9776714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
9786714d8e8SKurt Hackel 		return -EINVAL;
9796714d8e8SKurt Hackel 
9806714d8e8SKurt Hackel 	mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
9816714d8e8SKurt Hackel 	     "node_idx=%u, this node=%u\n", done->dead_node,
9826714d8e8SKurt Hackel 	     dlm->reco.dead_node, done->node_idx, dlm->node_num);
983d6dea6e9SKurt Hackel 
984d6dea6e9SKurt Hackel 	mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
985d6dea6e9SKurt Hackel 			"Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
986d6dea6e9SKurt Hackel 			"node_idx=%u, this node=%u\n", done->dead_node,
987d6dea6e9SKurt Hackel 			dlm->reco.dead_node, done->node_idx, dlm->node_num);
9886714d8e8SKurt Hackel 
9896714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
990800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
9916714d8e8SKurt Hackel 		if (ndata->node_num != done->node_idx)
9926714d8e8SKurt Hackel 			continue;
9936714d8e8SKurt Hackel 
9946714d8e8SKurt Hackel 		switch (ndata->state) {
995e2faea4cSKurt Hackel 			/* should have moved beyond INIT but not to FINALIZE yet */
9966714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
9976714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
9986714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
9996714d8e8SKurt Hackel 				mlog(ML_ERROR, "bad ndata state for node %u:"
10006714d8e8SKurt Hackel 				     " state=%d\n", ndata->node_num,
10016714d8e8SKurt Hackel 				     ndata->state);
10026714d8e8SKurt Hackel 				BUG();
10036714d8e8SKurt Hackel 				break;
1004e2faea4cSKurt Hackel 			/* these states are possible at this point, anywhere along
1005e2faea4cSKurt Hackel 			 * the line of recovery */
1006e2faea4cSKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
10076714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
10086714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
10096714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
10106714d8e8SKurt Hackel 				mlog(0, "node %u is DONE sending "
10116714d8e8SKurt Hackel 					  "recovery data!\n",
10126714d8e8SKurt Hackel 					  ndata->node_num);
10136714d8e8SKurt Hackel 
10146714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_DONE;
10156714d8e8SKurt Hackel 				ret = 0;
10166714d8e8SKurt Hackel 				break;
10176714d8e8SKurt Hackel 		}
10186714d8e8SKurt Hackel 	}
10196714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
10206714d8e8SKurt Hackel 
10216714d8e8SKurt Hackel 	/* wake the recovery thread, some node is done */
10226714d8e8SKurt Hackel 	if (!ret)
10236714d8e8SKurt Hackel 		dlm_kick_recovery_thread(dlm);
10246714d8e8SKurt Hackel 
10256714d8e8SKurt Hackel 	if (ret < 0)
10266714d8e8SKurt Hackel 		mlog(ML_ERROR, "failed to find recovery node data for node "
10276714d8e8SKurt Hackel 		     "%u\n", done->node_idx);
10286714d8e8SKurt Hackel 	dlm_put(dlm);
10296714d8e8SKurt Hackel 
10306714d8e8SKurt Hackel 	mlog(0, "leaving reco data done handler, ret=%d\n", ret);
10316714d8e8SKurt Hackel 	return ret;
10326714d8e8SKurt Hackel }
10336714d8e8SKurt Hackel 
dlm_move_reco_locks_to_list(struct dlm_ctxt * dlm,struct list_head * list,u8 dead_node)10346714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
10356714d8e8SKurt Hackel 					struct list_head *list,
10366714d8e8SKurt Hackel 				       	u8 dead_node)
10376714d8e8SKurt Hackel {
1038800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
1039e2faea4cSKurt Hackel 	struct dlm_lock *lock;
10406714d8e8SKurt Hackel 
10416714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1042800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
1043e2faea4cSKurt Hackel 		/* always prune any $RECOVERY entries for dead nodes,
1044e2faea4cSKurt Hackel 		 * otherwise hangs can occur during later recovery */
10456714d8e8SKurt Hackel 		if (dlm_is_recovery_lock(res->lockname.name,
1046e2faea4cSKurt Hackel 					 res->lockname.len)) {
1047e2faea4cSKurt Hackel 			spin_lock(&res->spinlock);
1048e2faea4cSKurt Hackel 			list_for_each_entry(lock, &res->granted, list) {
1049e2faea4cSKurt Hackel 				if (lock->ml.node == dead_node) {
1050e2faea4cSKurt Hackel 					mlog(0, "AHA! there was "
1051e2faea4cSKurt Hackel 					     "a $RECOVERY lock for dead "
1052e2faea4cSKurt Hackel 					     "node %u (%s)!\n",
1053e2faea4cSKurt Hackel 					     dead_node, dlm->name);
1054e2faea4cSKurt Hackel 					list_del_init(&lock->list);
1055e2faea4cSKurt Hackel 					dlm_lock_put(lock);
1056b934beafSXue jiufei 					/* Can't schedule DLM_UNLOCK_FREE_LOCK
1057b934beafSXue jiufei 					 * - do manually */
1058b934beafSXue jiufei 					dlm_lock_put(lock);
1059e2faea4cSKurt Hackel 					break;
1060e2faea4cSKurt Hackel 				}
1061e2faea4cSKurt Hackel 			}
1062e2faea4cSKurt Hackel 			spin_unlock(&res->spinlock);
10636714d8e8SKurt Hackel 			continue;
1064e2faea4cSKurt Hackel 		}
1065e2faea4cSKurt Hackel 
10666714d8e8SKurt Hackel 		if (res->owner == dead_node) {
10676714d8e8SKurt Hackel 			mlog(0, "found lockres owned by dead node while "
10686714d8e8SKurt Hackel 				  "doing recovery for node %u. sending it.\n",
10696714d8e8SKurt Hackel 				  dead_node);
1070f116629dSAkinobu Mita 			list_move_tail(&res->recovering, list);
10716714d8e8SKurt Hackel 		} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
10726714d8e8SKurt Hackel 			mlog(0, "found UNKNOWN owner while doing recovery "
10736714d8e8SKurt Hackel 				  "for node %u. sending it.\n", dead_node);
1074f116629dSAkinobu Mita 			list_move_tail(&res->recovering, list);
10756714d8e8SKurt Hackel 		}
10766714d8e8SKurt Hackel 	}
10776714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
10786714d8e8SKurt Hackel }
10796714d8e8SKurt Hackel 
dlm_num_locks_in_lockres(struct dlm_lock_resource * res)10806714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
10816714d8e8SKurt Hackel {
10826714d8e8SKurt Hackel 	int total_locks = 0;
10836714d8e8SKurt Hackel 	struct list_head *iter, *queue = &res->granted;
10846714d8e8SKurt Hackel 	int i;
10856714d8e8SKurt Hackel 
10866714d8e8SKurt Hackel 	for (i=0; i<3; i++) {
10876714d8e8SKurt Hackel 		list_for_each(iter, queue)
10886714d8e8SKurt Hackel 			total_locks++;
10896714d8e8SKurt Hackel 		queue++;
10906714d8e8SKurt Hackel 	}
10916714d8e8SKurt Hackel 	return total_locks;
10926714d8e8SKurt Hackel }
10936714d8e8SKurt Hackel 
10946714d8e8SKurt Hackel 
dlm_send_mig_lockres_msg(struct dlm_ctxt * dlm,struct dlm_migratable_lockres * mres,u8 send_to,struct dlm_lock_resource * res,int total_locks)10956714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
10966714d8e8SKurt Hackel 				      struct dlm_migratable_lockres *mres,
10976714d8e8SKurt Hackel 				      u8 send_to,
10986714d8e8SKurt Hackel 				      struct dlm_lock_resource *res,
10996714d8e8SKurt Hackel 				      int total_locks)
11006714d8e8SKurt Hackel {
11016714d8e8SKurt Hackel 	u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
11026714d8e8SKurt Hackel 	int mres_total_locks = be32_to_cpu(mres->total_locks);
11030e71666bSGustavo A. R. Silva 	int ret = 0, status = 0;
11046714d8e8SKurt Hackel 	u8 orig_flags = mres->flags,
11056714d8e8SKurt Hackel 	   orig_master = mres->master;
11066714d8e8SKurt Hackel 
11076714d8e8SKurt Hackel 	BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
11086714d8e8SKurt Hackel 	if (!mres->num_locks)
11096714d8e8SKurt Hackel 		return 0;
11106714d8e8SKurt Hackel 
11116714d8e8SKurt Hackel 	/* add an all-done flag if we reached the last lock */
11126714d8e8SKurt Hackel 	orig_flags = mres->flags;
11136714d8e8SKurt Hackel 	BUG_ON(total_locks > mres_total_locks);
11146714d8e8SKurt Hackel 	if (total_locks == mres_total_locks)
11156714d8e8SKurt Hackel 		mres->flags |= DLM_MRES_ALL_DONE;
11166714d8e8SKurt Hackel 
1117ba2bf218SKurt Hackel 	mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1118ba2bf218SKurt Hackel 	     dlm->name, res->lockname.len, res->lockname.name,
111917ae26b6SJeff Liu 	     orig_flags & DLM_MRES_MIGRATION ? "migration" : "recovery",
1120ba2bf218SKurt Hackel 	     send_to);
1121ba2bf218SKurt Hackel 
11226714d8e8SKurt Hackel 	/* send it */
11236714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
11240e71666bSGustavo A. R. Silva 				 struct_size(mres, ml, mres->num_locks),
11250e71666bSGustavo A. R. Silva 				 send_to, &status);
11266714d8e8SKurt Hackel 	if (ret < 0) {
11276714d8e8SKurt Hackel 		/* XXX: negative status is not handled.
11286714d8e8SKurt Hackel 		 * this will end up killing this node. */
11298decab3cSSunil Mushran 		mlog(ML_ERROR, "%s: res %.*s, Error %d send MIG_LOCKRES to "
11308decab3cSSunil Mushran 		     "node %u (%s)\n", dlm->name, mres->lockname_len,
11318decab3cSSunil Mushran 		     mres->lockname, ret, send_to,
11328decab3cSSunil Mushran 		     (orig_flags & DLM_MRES_MIGRATION ?
11338decab3cSSunil Mushran 		      "migration" : "recovery"));
11346714d8e8SKurt Hackel 	} else {
11356714d8e8SKurt Hackel 		/* might get an -ENOMEM back here */
11366714d8e8SKurt Hackel 		ret = status;
11376714d8e8SKurt Hackel 		if (ret < 0) {
11386714d8e8SKurt Hackel 			mlog_errno(ret);
11396714d8e8SKurt Hackel 
11406714d8e8SKurt Hackel 			if (ret == -EFAULT) {
11416714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u told me to kill "
11426714d8e8SKurt Hackel 				     "myself!\n", send_to);
11436714d8e8SKurt Hackel 				BUG();
11446714d8e8SKurt Hackel 			}
11456714d8e8SKurt Hackel 		}
11466714d8e8SKurt Hackel 	}
11476714d8e8SKurt Hackel 
11486714d8e8SKurt Hackel 	/* zero and reinit the message buffer */
11496714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
11506714d8e8SKurt Hackel 				    res->lockname.len, mres_total_locks,
11516714d8e8SKurt Hackel 				    mig_cookie, orig_flags, orig_master);
11526714d8e8SKurt Hackel 	return ret;
11536714d8e8SKurt Hackel }
11546714d8e8SKurt Hackel 
dlm_init_migratable_lockres(struct dlm_migratable_lockres * mres,const char * lockname,int namelen,int total_locks,u64 cookie,u8 flags,u8 master)11556714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
11566714d8e8SKurt Hackel 					const char *lockname, int namelen,
11576714d8e8SKurt Hackel 					int total_locks, u64 cookie,
11586714d8e8SKurt Hackel 					u8 flags, u8 master)
11596714d8e8SKurt Hackel {
11606714d8e8SKurt Hackel 	/* mres here is one full page */
11615fb0f7f0SShani Moideen 	clear_page(mres);
11626714d8e8SKurt Hackel 	mres->lockname_len = namelen;
11636714d8e8SKurt Hackel 	memcpy(mres->lockname, lockname, namelen);
11646714d8e8SKurt Hackel 	mres->num_locks = 0;
11656714d8e8SKurt Hackel 	mres->total_locks = cpu_to_be32(total_locks);
11666714d8e8SKurt Hackel 	mres->mig_cookie = cpu_to_be64(cookie);
11676714d8e8SKurt Hackel 	mres->flags = flags;
11686714d8e8SKurt Hackel 	mres->master = master;
11696714d8e8SKurt Hackel }
11706714d8e8SKurt Hackel 
dlm_prepare_lvb_for_migration(struct dlm_lock * lock,struct dlm_migratable_lockres * mres,int queue)117171656fa6SSunil Mushran static void dlm_prepare_lvb_for_migration(struct dlm_lock *lock,
117271656fa6SSunil Mushran 					  struct dlm_migratable_lockres *mres,
117371656fa6SSunil Mushran 					  int queue)
117471656fa6SSunil Mushran {
117571656fa6SSunil Mushran 	if (!lock->lksb)
117671656fa6SSunil Mushran 	       return;
117771656fa6SSunil Mushran 
117871656fa6SSunil Mushran 	/* Ignore lvb in all locks in the blocked list */
117971656fa6SSunil Mushran 	if (queue == DLM_BLOCKED_LIST)
118071656fa6SSunil Mushran 		return;
118171656fa6SSunil Mushran 
118271656fa6SSunil Mushran 	/* Only consider lvbs in locks with granted EX or PR lock levels */
118371656fa6SSunil Mushran 	if (lock->ml.type != LKM_EXMODE && lock->ml.type != LKM_PRMODE)
118471656fa6SSunil Mushran 		return;
118571656fa6SSunil Mushran 
118671656fa6SSunil Mushran 	if (dlm_lvb_is_empty(mres->lvb)) {
118771656fa6SSunil Mushran 		memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
118871656fa6SSunil Mushran 		return;
118971656fa6SSunil Mushran 	}
119071656fa6SSunil Mushran 
119171656fa6SSunil Mushran 	/* Ensure the lvb copied for migration matches in other valid locks */
119271656fa6SSunil Mushran 	if (!memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))
119371656fa6SSunil Mushran 		return;
119471656fa6SSunil Mushran 
119571656fa6SSunil Mushran 	mlog(ML_ERROR, "Mismatched lvb in lock cookie=%u:%llu, name=%.*s, "
119671656fa6SSunil Mushran 	     "node=%u\n",
119771656fa6SSunil Mushran 	     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
119871656fa6SSunil Mushran 	     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
119971656fa6SSunil Mushran 	     lock->lockres->lockname.len, lock->lockres->lockname.name,
120071656fa6SSunil Mushran 	     lock->ml.node);
120171656fa6SSunil Mushran 	dlm_print_one_lock_resource(lock->lockres);
120271656fa6SSunil Mushran 	BUG();
120371656fa6SSunil Mushran }
12046714d8e8SKurt Hackel 
12056714d8e8SKurt Hackel /* returns 1 if this lock fills the network structure,
12066714d8e8SKurt Hackel  * 0 otherwise */
dlm_add_lock_to_array(struct dlm_lock * lock,struct dlm_migratable_lockres * mres,int queue)12076714d8e8SKurt Hackel static int dlm_add_lock_to_array(struct dlm_lock *lock,
12086714d8e8SKurt Hackel 				 struct dlm_migratable_lockres *mres, int queue)
12096714d8e8SKurt Hackel {
12106714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
12116714d8e8SKurt Hackel 	int lock_num = mres->num_locks;
12126714d8e8SKurt Hackel 
12136714d8e8SKurt Hackel 	ml = &(mres->ml[lock_num]);
12146714d8e8SKurt Hackel 	ml->cookie = lock->ml.cookie;
12156714d8e8SKurt Hackel 	ml->type = lock->ml.type;
12166714d8e8SKurt Hackel 	ml->convert_type = lock->ml.convert_type;
12176714d8e8SKurt Hackel 	ml->highest_blocked = lock->ml.highest_blocked;
12186714d8e8SKurt Hackel 	ml->list = queue;
12196714d8e8SKurt Hackel 	if (lock->lksb) {
12206714d8e8SKurt Hackel 		ml->flags = lock->lksb->flags;
122171656fa6SSunil Mushran 		dlm_prepare_lvb_for_migration(lock, mres, queue);
12226714d8e8SKurt Hackel 	}
12236714d8e8SKurt Hackel 	ml->node = lock->ml.node;
12246714d8e8SKurt Hackel 	mres->num_locks++;
12256714d8e8SKurt Hackel 	/* we reached the max, send this network message */
12266714d8e8SKurt Hackel 	if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
12276714d8e8SKurt Hackel 		return 1;
12286714d8e8SKurt Hackel 	return 0;
12296714d8e8SKurt Hackel }
12306714d8e8SKurt Hackel 
dlm_add_dummy_lock(struct dlm_ctxt * dlm,struct dlm_migratable_lockres * mres)1231ba2bf218SKurt Hackel static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
1232ba2bf218SKurt Hackel 			       struct dlm_migratable_lockres *mres)
1233ba2bf218SKurt Hackel {
1234ba2bf218SKurt Hackel 	struct dlm_lock dummy;
1235ba2bf218SKurt Hackel 	memset(&dummy, 0, sizeof(dummy));
1236ba2bf218SKurt Hackel 	dummy.ml.cookie = 0;
1237ba2bf218SKurt Hackel 	dummy.ml.type = LKM_IVMODE;
1238ba2bf218SKurt Hackel 	dummy.ml.convert_type = LKM_IVMODE;
1239ba2bf218SKurt Hackel 	dummy.ml.highest_blocked = LKM_IVMODE;
1240ba2bf218SKurt Hackel 	dummy.lksb = NULL;
1241ba2bf218SKurt Hackel 	dummy.ml.node = dlm->node_num;
1242ba2bf218SKurt Hackel 	dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
1243ba2bf218SKurt Hackel }
1244ba2bf218SKurt Hackel 
dlm_is_dummy_lock(struct dlm_ctxt * dlm,struct dlm_migratable_lock * ml,u8 * nodenum)1245ba2bf218SKurt Hackel static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
1246ba2bf218SKurt Hackel 				    struct dlm_migratable_lock *ml,
1247ba2bf218SKurt Hackel 				    u8 *nodenum)
1248ba2bf218SKurt Hackel {
1249ba2bf218SKurt Hackel 	if (unlikely(ml->cookie == 0 &&
1250ba2bf218SKurt Hackel 	    ml->type == LKM_IVMODE &&
1251ba2bf218SKurt Hackel 	    ml->convert_type == LKM_IVMODE &&
1252ba2bf218SKurt Hackel 	    ml->highest_blocked == LKM_IVMODE &&
1253ba2bf218SKurt Hackel 	    ml->list == DLM_BLOCKED_LIST)) {
1254ba2bf218SKurt Hackel 		*nodenum = ml->node;
1255ba2bf218SKurt Hackel 		return 1;
1256ba2bf218SKurt Hackel 	}
1257ba2bf218SKurt Hackel 	return 0;
1258ba2bf218SKurt Hackel }
12596714d8e8SKurt Hackel 
dlm_send_one_lockres(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_migratable_lockres * mres,u8 send_to,u8 flags)12606714d8e8SKurt Hackel int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
12616714d8e8SKurt Hackel 			 struct dlm_migratable_lockres *mres,
12626714d8e8SKurt Hackel 			 u8 send_to, u8 flags)
12636714d8e8SKurt Hackel {
1264800deef3SChristoph Hellwig 	struct list_head *queue;
12656714d8e8SKurt Hackel 	int total_locks, i;
12666714d8e8SKurt Hackel 	u64 mig_cookie = 0;
12676714d8e8SKurt Hackel 	struct dlm_lock *lock;
12686714d8e8SKurt Hackel 	int ret = 0;
12696714d8e8SKurt Hackel 
12706714d8e8SKurt Hackel 	BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
12716714d8e8SKurt Hackel 
12726714d8e8SKurt Hackel 	mlog(0, "sending to %u\n", send_to);
12736714d8e8SKurt Hackel 
12746714d8e8SKurt Hackel 	total_locks = dlm_num_locks_in_lockres(res);
12756714d8e8SKurt Hackel 	if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
12766714d8e8SKurt Hackel 		/* rare, but possible */
12776714d8e8SKurt Hackel 		mlog(0, "argh.  lockres has %d locks.  this will "
12786714d8e8SKurt Hackel 			  "require more than one network packet to "
12796714d8e8SKurt Hackel 			  "migrate\n", total_locks);
12806714d8e8SKurt Hackel 		mig_cookie = dlm_get_next_mig_cookie();
12816714d8e8SKurt Hackel 	}
12826714d8e8SKurt Hackel 
12836714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
12846714d8e8SKurt Hackel 				    res->lockname.len, total_locks,
12856714d8e8SKurt Hackel 				    mig_cookie, flags, res->owner);
12866714d8e8SKurt Hackel 
12876714d8e8SKurt Hackel 	total_locks = 0;
12886714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
12896714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
1290800deef3SChristoph Hellwig 		list_for_each_entry(lock, queue, list) {
12916714d8e8SKurt Hackel 			/* add another lock. */
12926714d8e8SKurt Hackel 			total_locks++;
12936714d8e8SKurt Hackel 			if (!dlm_add_lock_to_array(lock, mres, i))
12946714d8e8SKurt Hackel 				continue;
12956714d8e8SKurt Hackel 
12966714d8e8SKurt Hackel 			/* this filled the lock message,
12976714d8e8SKurt Hackel 			 * we must send it immediately. */
12986714d8e8SKurt Hackel 			ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
12996714d8e8SKurt Hackel 						       res, total_locks);
130029c0fa0fSKurt Hackel 			if (ret < 0)
130129c0fa0fSKurt Hackel 				goto error;
13026714d8e8SKurt Hackel 		}
13036714d8e8SKurt Hackel 	}
1304ba2bf218SKurt Hackel 	if (total_locks == 0) {
1305ba2bf218SKurt Hackel 		/* send a dummy lock to indicate a mastery reference only */
1306ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
1307ba2bf218SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name,
1308ba2bf218SKurt Hackel 		     send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
1309ba2bf218SKurt Hackel 		     "migration");
1310ba2bf218SKurt Hackel 		dlm_add_dummy_lock(dlm, mres);
1311ba2bf218SKurt Hackel 	}
13126714d8e8SKurt Hackel 	/* flush any remaining locks */
13136714d8e8SKurt Hackel 	ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
131429c0fa0fSKurt Hackel 	if (ret < 0)
131529c0fa0fSKurt Hackel 		goto error;
131629c0fa0fSKurt Hackel 	return ret;
131729c0fa0fSKurt Hackel 
131829c0fa0fSKurt Hackel error:
131929c0fa0fSKurt Hackel 	mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
132029c0fa0fSKurt Hackel 	     dlm->name, ret);
132129c0fa0fSKurt Hackel 	if (!dlm_is_host_down(ret))
13226714d8e8SKurt Hackel 		BUG();
132329c0fa0fSKurt Hackel 	mlog(0, "%s: node %u went down while sending %s "
132429c0fa0fSKurt Hackel 	     "lockres %.*s\n", dlm->name, send_to,
132529c0fa0fSKurt Hackel 	     flags & DLM_MRES_RECOVERY ?  "recovery" : "migration",
132629c0fa0fSKurt Hackel 	     res->lockname.len, res->lockname.name);
13276714d8e8SKurt Hackel 	return ret;
13286714d8e8SKurt Hackel }
13296714d8e8SKurt Hackel 
13306714d8e8SKurt Hackel 
13316714d8e8SKurt Hackel 
13326714d8e8SKurt Hackel /*
13336714d8e8SKurt Hackel  * this message will contain no more than one page worth of
13346714d8e8SKurt Hackel  * recovery data, and it will work on only one lockres.
13356714d8e8SKurt Hackel  * there may be many locks in this page, and we may need to wait
13366714d8e8SKurt Hackel  * for additional packets to complete all the locks (rare, but
13376714d8e8SKurt Hackel  * possible).
13386714d8e8SKurt Hackel  */
13396714d8e8SKurt Hackel /*
13406714d8e8SKurt Hackel  * NOTE: the allocation error cases here are scary
13416714d8e8SKurt Hackel  * we really cannot afford to fail an alloc in recovery
13426714d8e8SKurt Hackel  * do we spin?  returning an error only delays the problem really
13436714d8e8SKurt Hackel  */
13446714d8e8SKurt Hackel 
dlm_mig_lockres_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)1345d74c9803SKurt Hackel int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
1346d74c9803SKurt Hackel 			    void **ret_data)
13476714d8e8SKurt Hackel {
13486714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
13496714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres =
13506714d8e8SKurt Hackel 		(struct dlm_migratable_lockres *)msg->buf;
13516714d8e8SKurt Hackel 	int ret = 0;
13526714d8e8SKurt Hackel 	u8 real_master;
135352987e2aSSunil Mushran 	u8 extra_refs = 0;
13546714d8e8SKurt Hackel 	char *buf = NULL;
13556714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
13566714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
135730bee898SXue jiufei 	unsigned int hash;
13586714d8e8SKurt Hackel 
13596714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
13606714d8e8SKurt Hackel 		return -EINVAL;
13616714d8e8SKurt Hackel 
1362bb34f24cSJun Piao 	if (!dlm_joined(dlm)) {
1363bb34f24cSJun Piao 		mlog(ML_ERROR, "Domain %s not joined! "
1364bb34f24cSJun Piao 			  "lockres %.*s, master %u\n",
1365bb34f24cSJun Piao 			  dlm->name, mres->lockname_len,
1366bb34f24cSJun Piao 			  mres->lockname, mres->master);
1367bb34f24cSJun Piao 		dlm_put(dlm);
1368bb34f24cSJun Piao 		return -EINVAL;
1369bb34f24cSJun Piao 	}
1370bb34f24cSJun Piao 
13716714d8e8SKurt Hackel 	BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
13726714d8e8SKurt Hackel 
13736714d8e8SKurt Hackel 	real_master = mres->master;
13746714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
13756714d8e8SKurt Hackel 		/* cannot migrate a lockres with no master */
13766714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
13776714d8e8SKurt Hackel 	}
13786714d8e8SKurt Hackel 
13796714d8e8SKurt Hackel 	mlog(0, "%s message received from node %u\n",
13806714d8e8SKurt Hackel 		  (mres->flags & DLM_MRES_RECOVERY) ?
13816714d8e8SKurt Hackel 		  "recovery" : "migration", mres->master);
13826714d8e8SKurt Hackel 	if (mres->flags & DLM_MRES_ALL_DONE)
13836714d8e8SKurt Hackel 		mlog(0, "all done flag.  all lockres data received!\n");
13846714d8e8SKurt Hackel 
13856714d8e8SKurt Hackel 	ret = -ENOMEM;
1386ad8100e0SKurt Hackel 	buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1387cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
13886714d8e8SKurt Hackel 	if (!buf || !item)
13896714d8e8SKurt Hackel 		goto leave;
13906714d8e8SKurt Hackel 
13916714d8e8SKurt Hackel 	/* lookup the lock to see if we have a secondary queue for this
13926714d8e8SKurt Hackel 	 * already...  just add the locks in and this will have its owner
13936714d8e8SKurt Hackel 	 * and RECOVERY flag changed when it completes. */
139430bee898SXue jiufei 	hash = dlm_lockid_hash(mres->lockname, mres->lockname_len);
139530bee898SXue jiufei 	spin_lock(&dlm->spinlock);
1396d277f33eSxuejiufei 	res = __dlm_lookup_lockres_full(dlm, mres->lockname, mres->lockname_len,
139730bee898SXue jiufei 			hash);
13986714d8e8SKurt Hackel 	if (res) {
13996714d8e8SKurt Hackel 	 	/* this will get a ref on res */
14006714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
14016714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
1402d277f33eSxuejiufei 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
1403d277f33eSxuejiufei 			mlog(0, "%s: node is attempting to migrate "
1404d277f33eSxuejiufei 				"lockres %.*s, but marked as dropping "
1405d277f33eSxuejiufei 				" ref!\n", dlm->name,
1406d277f33eSxuejiufei 				mres->lockname_len, mres->lockname);
1407d277f33eSxuejiufei 			ret = -EINVAL;
1408d277f33eSxuejiufei 			spin_unlock(&res->spinlock);
1409d277f33eSxuejiufei 			spin_unlock(&dlm->spinlock);
1410d277f33eSxuejiufei 			dlm_lockres_put(res);
1411d277f33eSxuejiufei 			goto leave;
1412d277f33eSxuejiufei 		}
1413d277f33eSxuejiufei 
14146714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY) {
14156714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
14166714d8e8SKurt Hackel 		} else {
14176714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_MIGRATING) {
14186714d8e8SKurt Hackel 				/* this is at least the second
14196714d8e8SKurt Hackel 				 * lockres message */
14206714d8e8SKurt Hackel 				mlog(0, "lock %.*s is already migrating\n",
14216714d8e8SKurt Hackel 					  mres->lockname_len,
14226714d8e8SKurt Hackel 					  mres->lockname);
14236714d8e8SKurt Hackel 			} else if (res->state & DLM_LOCK_RES_RECOVERING) {
14246714d8e8SKurt Hackel 				/* caller should BUG */
14256714d8e8SKurt Hackel 				mlog(ML_ERROR, "node is attempting to migrate "
14266714d8e8SKurt Hackel 				     "lock %.*s, but marked as recovering!\n",
14276714d8e8SKurt Hackel 				     mres->lockname_len, mres->lockname);
14286714d8e8SKurt Hackel 				ret = -EFAULT;
14296714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
143030bee898SXue jiufei 				spin_unlock(&dlm->spinlock);
143127749f2fSXue jiufei 				dlm_lockres_put(res);
14326714d8e8SKurt Hackel 				goto leave;
14336714d8e8SKurt Hackel 			}
14346714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
14356714d8e8SKurt Hackel 		}
14366714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
143730bee898SXue jiufei 		spin_unlock(&dlm->spinlock);
14386714d8e8SKurt Hackel 	} else {
143930bee898SXue jiufei 		spin_unlock(&dlm->spinlock);
14406714d8e8SKurt Hackel 		/* need to allocate, just like if it was
14416714d8e8SKurt Hackel 		 * mastered here normally  */
14426714d8e8SKurt Hackel 		res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
14436714d8e8SKurt Hackel 		if (!res)
14446714d8e8SKurt Hackel 			goto leave;
14456714d8e8SKurt Hackel 
14466714d8e8SKurt Hackel 		/* to match the ref that we would have gotten if
14476714d8e8SKurt Hackel 		 * dlm_lookup_lockres had succeeded */
14486714d8e8SKurt Hackel 		dlm_lockres_get(res);
14496714d8e8SKurt Hackel 
14506714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
14516714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY)
14526714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
14536714d8e8SKurt Hackel 		else
14546714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
14556714d8e8SKurt Hackel 
14566714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
14576714d8e8SKurt Hackel 		__dlm_insert_lockres(dlm, res);
14586714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
14596714d8e8SKurt Hackel 
146052987e2aSSunil Mushran 		/* Add an extra ref for this lock-less lockres lest the
146152987e2aSSunil Mushran 		 * dlm_thread purges it before we get the chance to add
146252987e2aSSunil Mushran 		 * locks to it */
146352987e2aSSunil Mushran 		dlm_lockres_get(res);
146452987e2aSSunil Mushran 
146552987e2aSSunil Mushran 		/* There are three refs that need to be put.
146652987e2aSSunil Mushran 		 * 1. Taken above.
146752987e2aSSunil Mushran 		 * 2. kref_init in dlm_new_lockres()->dlm_init_lockres().
146852987e2aSSunil Mushran 		 * 3. dlm_lookup_lockres()
146952987e2aSSunil Mushran 		 * The first one is handled at the end of this function. The
147052987e2aSSunil Mushran 		 * other two are handled in the worker thread after locks have
147152987e2aSSunil Mushran 		 * been attached. Yes, we don't wait for purge time to match
147252987e2aSSunil Mushran 		 * kref_init. The lockres will still have atleast one ref
147352987e2aSSunil Mushran 		 * added because it is in the hash __dlm_insert_lockres() */
147452987e2aSSunil Mushran 		extra_refs++;
147552987e2aSSunil Mushran 
14766714d8e8SKurt Hackel 		/* now that the new lockres is inserted,
14776714d8e8SKurt Hackel 		 * make it usable by other processes */
14786714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
14796714d8e8SKurt Hackel 		res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
14806714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
1481a6fa3640SKurt Hackel 		wake_up(&res->wq);
14826714d8e8SKurt Hackel 	}
14836714d8e8SKurt Hackel 
14846714d8e8SKurt Hackel 	/* at this point we have allocated everything we need,
14856714d8e8SKurt Hackel 	 * and we have a hashed lockres with an extra ref and
14866714d8e8SKurt Hackel 	 * the proper res->state flags. */
14876714d8e8SKurt Hackel 	ret = 0;
1488ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
1489ba2bf218SKurt Hackel 	/* drop this either when master requery finds a different master
1490ba2bf218SKurt Hackel 	 * or when a lock is added by the recovery worker */
1491ba2bf218SKurt Hackel 	dlm_lockres_grab_inflight_ref(dlm, res);
14926714d8e8SKurt Hackel 	if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
14936714d8e8SKurt Hackel 		/* migration cannot have an unknown master */
14946714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
14956714d8e8SKurt Hackel 		mlog(0, "recovery has passed me a lockres with an "
14966714d8e8SKurt Hackel 			  "unknown owner.. will need to requery: "
14976714d8e8SKurt Hackel 			  "%.*s\n", mres->lockname_len, mres->lockname);
14986714d8e8SKurt Hackel 	} else {
1499ba2bf218SKurt Hackel 		/* take a reference now to pin the lockres, drop it
1500ba2bf218SKurt Hackel 		 * when locks are added in the worker */
15016714d8e8SKurt Hackel 		dlm_change_lockres_owner(dlm, res, dlm->node_num);
15026714d8e8SKurt Hackel 	}
1503ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
15046714d8e8SKurt Hackel 
15056714d8e8SKurt Hackel 	/* queue up work for dlm_mig_lockres_worker */
15066714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
15076714d8e8SKurt Hackel 	memcpy(buf, msg->buf, be16_to_cpu(msg->data_len));  /* copy the whole message */
15086714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
15096714d8e8SKurt Hackel 	item->u.ml.lockres = res; /* already have a ref */
15106714d8e8SKurt Hackel 	item->u.ml.real_master = real_master;
151152987e2aSSunil Mushran 	item->u.ml.extra_ref = extra_refs;
15126714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
15136714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
15146714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
15153156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
15166714d8e8SKurt Hackel 
15176714d8e8SKurt Hackel leave:
151852987e2aSSunil Mushran 	/* One extra ref taken needs to be put here */
151952987e2aSSunil Mushran 	if (extra_refs)
152052987e2aSSunil Mushran 		dlm_lockres_put(res);
152152987e2aSSunil Mushran 
15226714d8e8SKurt Hackel 	dlm_put(dlm);
15236714d8e8SKurt Hackel 	if (ret < 0) {
15246714d8e8SKurt Hackel 		kfree(buf);
15256714d8e8SKurt Hackel 		kfree(item);
1526c1e8d35eSTao Ma 		mlog_errno(ret);
15276714d8e8SKurt Hackel 	}
15286714d8e8SKurt Hackel 
15296714d8e8SKurt Hackel 	return ret;
15306714d8e8SKurt Hackel }
15316714d8e8SKurt Hackel 
15326714d8e8SKurt Hackel 
dlm_mig_lockres_worker(struct dlm_work_item * item,void * data)15336714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
15346714d8e8SKurt Hackel {
153552987e2aSSunil Mushran 	struct dlm_ctxt *dlm;
15366714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
15376714d8e8SKurt Hackel 	int ret = 0;
15386714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
15396714d8e8SKurt Hackel 	u8 real_master;
154052987e2aSSunil Mushran 	u8 extra_ref;
15416714d8e8SKurt Hackel 
15426714d8e8SKurt Hackel 	dlm = item->dlm;
15436714d8e8SKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
15446714d8e8SKurt Hackel 
15456714d8e8SKurt Hackel 	res = item->u.ml.lockres;
15466714d8e8SKurt Hackel 	real_master = item->u.ml.real_master;
154752987e2aSSunil Mushran 	extra_ref = item->u.ml.extra_ref;
15486714d8e8SKurt Hackel 
15496714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
15506714d8e8SKurt Hackel 		/* this case is super-rare. only occurs if
15516714d8e8SKurt Hackel 		 * node death happens during migration. */
15526714d8e8SKurt Hackel again:
15536714d8e8SKurt Hackel 		ret = dlm_lockres_master_requery(dlm, res, &real_master);
15546714d8e8SKurt Hackel 		if (ret < 0) {
1555e2faea4cSKurt Hackel 			mlog(0, "dlm_lockres_master_requery ret=%d\n",
15566714d8e8SKurt Hackel 				  ret);
15576714d8e8SKurt Hackel 			goto again;
15586714d8e8SKurt Hackel 		}
15596714d8e8SKurt Hackel 		if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
15606714d8e8SKurt Hackel 			mlog(0, "lockres %.*s not claimed.  "
15616714d8e8SKurt Hackel 				   "this node will take it.\n",
15626714d8e8SKurt Hackel 				   res->lockname.len, res->lockname.name);
15636714d8e8SKurt Hackel 		} else {
1564ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
1565ba2bf218SKurt Hackel 			dlm_lockres_drop_inflight_ref(dlm, res);
1566ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
15676714d8e8SKurt Hackel 			mlog(0, "master needs to respond to sender "
15686714d8e8SKurt Hackel 				  "that node %u still owns %.*s\n",
15696714d8e8SKurt Hackel 				  real_master, res->lockname.len,
15706714d8e8SKurt Hackel 				  res->lockname.name);
15716714d8e8SKurt Hackel 			/* cannot touch this lockres */
15726714d8e8SKurt Hackel 			goto leave;
15736714d8e8SKurt Hackel 		}
15746714d8e8SKurt Hackel 	}
15756714d8e8SKurt Hackel 
15766714d8e8SKurt Hackel 	ret = dlm_process_recovery_data(dlm, res, mres);
15776714d8e8SKurt Hackel 	if (ret < 0)
15786714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data returned  %d\n", ret);
15796714d8e8SKurt Hackel 	else
15806714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data succeeded\n");
15816714d8e8SKurt Hackel 
15826714d8e8SKurt Hackel 	if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
15836714d8e8SKurt Hackel 	                   (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
15846714d8e8SKurt Hackel 		ret = dlm_finish_migration(dlm, res, mres->master);
15856714d8e8SKurt Hackel 		if (ret < 0)
15866714d8e8SKurt Hackel 			mlog_errno(ret);
15876714d8e8SKurt Hackel 	}
15886714d8e8SKurt Hackel 
15896714d8e8SKurt Hackel leave:
159052987e2aSSunil Mushran 	/* See comment in dlm_mig_lockres_handler() */
159152987e2aSSunil Mushran 	if (res) {
159252987e2aSSunil Mushran 		if (extra_ref)
159352987e2aSSunil Mushran 			dlm_lockres_put(res);
159452987e2aSSunil Mushran 		dlm_lockres_put(res);
159552987e2aSSunil Mushran 	}
15966714d8e8SKurt Hackel 	kfree(data);
15976714d8e8SKurt Hackel }
15986714d8e8SKurt Hackel 
15996714d8e8SKurt Hackel 
16006714d8e8SKurt Hackel 
dlm_lockres_master_requery(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 * real_master)16018169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
16028169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
16038169cae5SAdrian Bunk 				      u8 *real_master)
16046714d8e8SKurt Hackel {
16056714d8e8SKurt Hackel 	struct dlm_node_iter iter;
16066714d8e8SKurt Hackel 	int nodenum;
16076714d8e8SKurt Hackel 	int ret = 0;
16086714d8e8SKurt Hackel 
16096714d8e8SKurt Hackel 	*real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
16106714d8e8SKurt Hackel 
16116714d8e8SKurt Hackel 	/* we only reach here if one of the two nodes in a
16126714d8e8SKurt Hackel 	 * migration died while the migration was in progress.
16136714d8e8SKurt Hackel 	 * at this point we need to requery the master.  we
16146714d8e8SKurt Hackel 	 * know that the new_master got as far as creating
16156714d8e8SKurt Hackel 	 * an mle on at least one node, but we do not know
16166714d8e8SKurt Hackel 	 * if any nodes had actually cleared the mle and set
16176714d8e8SKurt Hackel 	 * the master to the new_master.  the old master
16186714d8e8SKurt Hackel 	 * is supposed to set the owner to UNKNOWN in the
16196714d8e8SKurt Hackel 	 * event of a new_master death, so the only possible
16206714d8e8SKurt Hackel 	 * responses that we can get from nodes here are
16216714d8e8SKurt Hackel 	 * that the master is new_master, or that the master
16226714d8e8SKurt Hackel 	 * is UNKNOWN.
16236714d8e8SKurt Hackel 	 * if all nodes come back with UNKNOWN then we know
16246714d8e8SKurt Hackel 	 * the lock needs remastering here.
16256714d8e8SKurt Hackel 	 * if any node comes back with a valid master, check
16266714d8e8SKurt Hackel 	 * to see if that master is the one that we are
16276714d8e8SKurt Hackel 	 * recovering.  if so, then the new_master died and
16286714d8e8SKurt Hackel 	 * we need to remaster this lock.  if not, then the
16296714d8e8SKurt Hackel 	 * new_master survived and that node will respond to
16306714d8e8SKurt Hackel 	 * other nodes about the owner.
16316714d8e8SKurt Hackel 	 * if there is an owner, this node needs to dump this
16326714d8e8SKurt Hackel 	 * lockres and alert the sender that this lockres
16336714d8e8SKurt Hackel 	 * was rejected. */
16346714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
16356714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
16366714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
16376714d8e8SKurt Hackel 
16386714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
16396714d8e8SKurt Hackel 		/* do not send to self */
16406714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
16416714d8e8SKurt Hackel 			continue;
16426714d8e8SKurt Hackel 		ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
16436714d8e8SKurt Hackel 		if (ret < 0) {
16446714d8e8SKurt Hackel 			mlog_errno(ret);
1645c03872f5SKurt Hackel 			if (!dlm_is_host_down(ret))
16466714d8e8SKurt Hackel 				BUG();
1647c03872f5SKurt Hackel 			/* host is down, so answer for that node would be
1648c03872f5SKurt Hackel 			 * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
16496714d8e8SKurt Hackel 		}
16506714d8e8SKurt Hackel 		if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
16516714d8e8SKurt Hackel 			mlog(0, "lock master is %u\n", *real_master);
16526714d8e8SKurt Hackel 			break;
16536714d8e8SKurt Hackel 		}
16546714d8e8SKurt Hackel 	}
16556714d8e8SKurt Hackel 	return ret;
16566714d8e8SKurt Hackel }
16576714d8e8SKurt Hackel 
16586714d8e8SKurt Hackel 
dlm_do_master_requery(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 nodenum,u8 * real_master)1659c03872f5SKurt Hackel int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
16606714d8e8SKurt Hackel 			  u8 nodenum, u8 *real_master)
16616714d8e8SKurt Hackel {
1662d8f18750SColin Ian King 	int ret;
16636714d8e8SKurt Hackel 	struct dlm_master_requery req;
16646714d8e8SKurt Hackel 	int status = DLM_LOCK_RES_OWNER_UNKNOWN;
16656714d8e8SKurt Hackel 
16666714d8e8SKurt Hackel 	memset(&req, 0, sizeof(req));
16676714d8e8SKurt Hackel 	req.node_idx = dlm->node_num;
16686714d8e8SKurt Hackel 	req.namelen = res->lockname.len;
16696714d8e8SKurt Hackel 	memcpy(req.name, res->lockname.name, res->lockname.len);
16706714d8e8SKurt Hackel 
1671f08736bdSJoseph Qi resend:
16726714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
16736714d8e8SKurt Hackel 				 &req, sizeof(req), nodenum, &status);
16746714d8e8SKurt Hackel 	if (ret < 0)
1675a5196ec5SWengang Wang 		mlog(ML_ERROR, "Error %d when sending message %u (key "
1676a5196ec5SWengang Wang 		     "0x%x) to node %u\n", ret, DLM_MASTER_REQUERY_MSG,
1677a5196ec5SWengang Wang 		     dlm->key, nodenum);
1678f08736bdSJoseph Qi 	else if (status == -ENOMEM) {
1679f08736bdSJoseph Qi 		mlog_errno(status);
1680f08736bdSJoseph Qi 		msleep(50);
1681f08736bdSJoseph Qi 		goto resend;
1682f08736bdSJoseph Qi 	} else {
16836714d8e8SKurt Hackel 		BUG_ON(status < 0);
16846714d8e8SKurt Hackel 		BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
16856714d8e8SKurt Hackel 		*real_master = (u8) (status & 0xff);
16866714d8e8SKurt Hackel 		mlog(0, "node %u responded to master requery with %u\n",
16876714d8e8SKurt Hackel 			  nodenum, *real_master);
16886714d8e8SKurt Hackel 		ret = 0;
16896714d8e8SKurt Hackel 	}
16906714d8e8SKurt Hackel 	return ret;
16916714d8e8SKurt Hackel }
16926714d8e8SKurt Hackel 
16936714d8e8SKurt Hackel 
16946714d8e8SKurt Hackel /* this function cannot error, so unless the sending
16956714d8e8SKurt Hackel  * or receiving of the message failed, the owner can
16966714d8e8SKurt Hackel  * be trusted */
dlm_master_requery_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)1697d74c9803SKurt Hackel int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
1698d74c9803SKurt Hackel 			       void **ret_data)
16996714d8e8SKurt Hackel {
17006714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
17016714d8e8SKurt Hackel 	struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
17026714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
1703a3d33291SMark Fasheh 	unsigned int hash;
17046714d8e8SKurt Hackel 	int master = DLM_LOCK_RES_OWNER_UNKNOWN;
17056714d8e8SKurt Hackel 	u32 flags = DLM_ASSERT_MASTER_REQUERY;
1706012572d4SJoseph Qi 	int dispatched = 0;
17076714d8e8SKurt Hackel 
17086714d8e8SKurt Hackel 	if (!dlm_grab(dlm)) {
17096714d8e8SKurt Hackel 		/* since the domain has gone away on this
17106714d8e8SKurt Hackel 		 * node, the proper response is UNKNOWN */
17116714d8e8SKurt Hackel 		return master;
17126714d8e8SKurt Hackel 	}
17136714d8e8SKurt Hackel 
1714a3d33291SMark Fasheh 	hash = dlm_lockid_hash(req->name, req->namelen);
1715a3d33291SMark Fasheh 
17166714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1717a3d33291SMark Fasheh 	res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
17186714d8e8SKurt Hackel 	if (res) {
17196714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
17206714d8e8SKurt Hackel 		master = res->owner;
17216714d8e8SKurt Hackel 		if (master == dlm->node_num) {
17226714d8e8SKurt Hackel 			int ret = dlm_dispatch_assert_master(dlm, res,
17236714d8e8SKurt Hackel 							     0, 0, flags);
17246714d8e8SKurt Hackel 			if (ret < 0) {
1725f08736bdSJoseph Qi 				mlog_errno(ret);
1726f08736bdSJoseph Qi 				spin_unlock(&res->spinlock);
1727f08736bdSJoseph Qi 				dlm_lockres_put(res);
1728f08736bdSJoseph Qi 				spin_unlock(&dlm->spinlock);
1729f08736bdSJoseph Qi 				dlm_put(dlm);
1730f08736bdSJoseph Qi 				/* sender will take care of this and retry */
1731f08736bdSJoseph Qi 				return ret;
1732012572d4SJoseph Qi 			} else {
1733012572d4SJoseph Qi 				dispatched = 1;
1734ac4fef4dSXue jiufei 				__dlm_lockres_grab_inflight_worker(dlm, res);
17356714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
1736b67de018SJoseph Qi 			}
17379a7e6b5aSalex chen 		} else {
17389a7e6b5aSalex chen 			/* put.. incase we are not the master */
17399a7e6b5aSalex chen 			spin_unlock(&res->spinlock);
17409a7e6b5aSalex chen 			dlm_lockres_put(res);
17419a7e6b5aSalex chen 		}
17426714d8e8SKurt Hackel 	}
17436714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
17446714d8e8SKurt Hackel 
1745012572d4SJoseph Qi 	if (!dispatched)
17466714d8e8SKurt Hackel 		dlm_put(dlm);
17476714d8e8SKurt Hackel 	return master;
17486714d8e8SKurt Hackel }
17496714d8e8SKurt Hackel 
17506714d8e8SKurt Hackel static inline struct list_head *
dlm_list_num_to_pointer(struct dlm_lock_resource * res,int list_num)17516714d8e8SKurt Hackel dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
17526714d8e8SKurt Hackel {
17536714d8e8SKurt Hackel 	struct list_head *ret;
17546714d8e8SKurt Hackel 	BUG_ON(list_num < 0);
17556714d8e8SKurt Hackel 	BUG_ON(list_num > 2);
17566714d8e8SKurt Hackel 	ret = &(res->granted);
17576714d8e8SKurt Hackel 	ret += list_num;
17586714d8e8SKurt Hackel 	return ret;
17596714d8e8SKurt Hackel }
17606714d8e8SKurt Hackel /* TODO: do ast flush business
17616714d8e8SKurt Hackel  * TODO: do MIGRATING and RECOVERING spinning
17626714d8e8SKurt Hackel  */
17636714d8e8SKurt Hackel 
17646714d8e8SKurt Hackel /*
17656714d8e8SKurt Hackel * NOTE about in-flight requests during migration:
17666714d8e8SKurt Hackel *
17676714d8e8SKurt Hackel * Before attempting the migrate, the master has marked the lockres as
17686714d8e8SKurt Hackel * MIGRATING and then flushed all of its pending ASTS.  So any in-flight
17696714d8e8SKurt Hackel * requests either got queued before the MIGRATING flag got set, in which
17706714d8e8SKurt Hackel * case the lock data will reflect the change and a return message is on
17716714d8e8SKurt Hackel * the way, or the request failed to get in before MIGRATING got set.  In
17726714d8e8SKurt Hackel * this case, the caller will be told to spin and wait for the MIGRATING
17736714d8e8SKurt Hackel * flag to be dropped, then recheck the master.
17746714d8e8SKurt Hackel * This holds true for the convert, cancel and unlock cases, and since lvb
17756714d8e8SKurt Hackel * updates are tied to these same messages, it applies to lvb updates as
17766714d8e8SKurt Hackel * well.  For the lock case, there is no way a lock can be on the master
17776714d8e8SKurt Hackel * queue and not be on the secondary queue since the lock is always added
17786714d8e8SKurt Hackel * locally first.  This means that the new target node will never be sent
17796714d8e8SKurt Hackel * a lock that he doesn't already have on the list.
17806714d8e8SKurt Hackel * In total, this means that the local lock is correct and should not be
17816714d8e8SKurt Hackel * updated to match the one sent by the master.  Any messages sent back
17826714d8e8SKurt Hackel * from the master before the MIGRATING flag will bring the lock properly
17836714d8e8SKurt Hackel * up-to-date, and the change will be ordered properly for the waiter.
17846714d8e8SKurt Hackel * We will *not* attempt to modify the lock underneath the waiter.
17856714d8e8SKurt Hackel */
17866714d8e8SKurt Hackel 
dlm_process_recovery_data(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_migratable_lockres * mres)17876714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
17886714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
17896714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres)
17906714d8e8SKurt Hackel {
17916714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
1792e527b22cSAndrew Morton 	struct list_head *queue, *iter;
1793e17e75ecSKurt Hackel 	struct list_head *tmpq = NULL;
17946714d8e8SKurt Hackel 	struct dlm_lock *newlock = NULL;
17956714d8e8SKurt Hackel 	struct dlm_lockstatus *lksb = NULL;
17966714d8e8SKurt Hackel 	int ret = 0;
1797e17e75ecSKurt Hackel 	int i, j, bad;
179834aa8dacSJunxiao Bi 	struct dlm_lock *lock;
1799ba2bf218SKurt Hackel 	u8 from = O2NM_MAX_NODES;
180026636bf6SSunil Mushran 	__be64 c;
18016714d8e8SKurt Hackel 
18026714d8e8SKurt Hackel 	mlog(0, "running %d locks for this lockres\n", mres->num_locks);
18036714d8e8SKurt Hackel 	for (i=0; i<mres->num_locks; i++) {
18046714d8e8SKurt Hackel 		ml = &(mres->ml[i]);
1805ba2bf218SKurt Hackel 
1806ba2bf218SKurt Hackel 		if (dlm_is_dummy_lock(dlm, ml, &from)) {
1807ba2bf218SKurt Hackel 			/* placeholder, just need to set the refmap bit */
1808ba2bf218SKurt Hackel 			BUG_ON(mres->num_locks != 1);
1809ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: dummy lock for %u\n",
1810ba2bf218SKurt Hackel 			     dlm->name, mres->lockname_len, mres->lockname,
1811ba2bf218SKurt Hackel 			     from);
1812ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
18138d400b81SSunil Mushran 			dlm_lockres_set_refmap_bit(dlm, res, from);
1814ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
1815ba2bf218SKurt Hackel 			break;
1816ba2bf218SKurt Hackel 		}
18176714d8e8SKurt Hackel 		BUG_ON(ml->highest_blocked != LKM_IVMODE);
18186714d8e8SKurt Hackel 		newlock = NULL;
18196714d8e8SKurt Hackel 		lksb = NULL;
18206714d8e8SKurt Hackel 
18216714d8e8SKurt Hackel 		queue = dlm_list_num_to_pointer(res, ml->list);
1822e17e75ecSKurt Hackel 		tmpq = NULL;
18236714d8e8SKurt Hackel 
18246714d8e8SKurt Hackel 		/* if the lock is for the local node it needs to
18256714d8e8SKurt Hackel 		 * be moved to the proper location within the queue.
18266714d8e8SKurt Hackel 		 * do not allocate a new lock structure. */
18276714d8e8SKurt Hackel 		if (ml->node == dlm->node_num) {
18286714d8e8SKurt Hackel 			/* MIGRATION ONLY! */
18296714d8e8SKurt Hackel 			BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
18306714d8e8SKurt Hackel 
183134aa8dacSJunxiao Bi 			lock = NULL;
18326714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
1833e17e75ecSKurt Hackel 			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
1834e17e75ecSKurt Hackel 				tmpq = dlm_list_idx_to_ptr(res, j);
1835e527b22cSAndrew Morton 				list_for_each(iter, tmpq) {
1836e527b22cSAndrew Morton 					lock = list_entry(iter,
1837e527b22cSAndrew Morton 						  struct dlm_lock, list);
183834aa8dacSJunxiao Bi 					if (lock->ml.cookie == ml->cookie)
18396714d8e8SKurt Hackel 						break;
184034aa8dacSJunxiao Bi 					lock = NULL;
18416714d8e8SKurt Hackel 				}
1842e17e75ecSKurt Hackel 				if (lock)
1843e17e75ecSKurt Hackel 					break;
1844e17e75ecSKurt Hackel 			}
18456714d8e8SKurt Hackel 
18466714d8e8SKurt Hackel 			/* lock is always created locally first, and
18476714d8e8SKurt Hackel 			 * destroyed locally last.  it must be on the list */
18486714d8e8SKurt Hackel 			if (!lock) {
184926636bf6SSunil Mushran 				c = ml->cookie;
185026636bf6SSunil Mushran 				mlog(ML_ERROR, "Could not find local lock "
185126636bf6SSunil Mushran 					       "with cookie %u:%llu, node %u, "
185226636bf6SSunil Mushran 					       "list %u, flags 0x%x, type %d, "
185326636bf6SSunil Mushran 					       "conv %d, highest blocked %d\n",
185474aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
185526636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
185626636bf6SSunil Mushran 				     ml->node, ml->list, ml->flags, ml->type,
185726636bf6SSunil Mushran 				     ml->convert_type, ml->highest_blocked);
185871ac1062SKurt Hackel 				__dlm_print_one_lock_resource(res);
18596714d8e8SKurt Hackel 				BUG();
18606714d8e8SKurt Hackel 			}
186126636bf6SSunil Mushran 
186226636bf6SSunil Mushran 			if (lock->ml.node != ml->node) {
186326636bf6SSunil Mushran 				c = lock->ml.cookie;
186426636bf6SSunil Mushran 				mlog(ML_ERROR, "Mismatched node# in lock "
186526636bf6SSunil Mushran 				     "cookie %u:%llu, name %.*s, node %u\n",
186626636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
186726636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
186826636bf6SSunil Mushran 				     res->lockname.len, res->lockname.name,
186926636bf6SSunil Mushran 				     lock->ml.node);
187026636bf6SSunil Mushran 				c = ml->cookie;
187126636bf6SSunil Mushran 				mlog(ML_ERROR, "Migrate lock cookie %u:%llu, "
187226636bf6SSunil Mushran 				     "node %u, list %u, flags 0x%x, type %d, "
187326636bf6SSunil Mushran 				     "conv %d, highest blocked %d\n",
187426636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
187526636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
187626636bf6SSunil Mushran 				     ml->node, ml->list, ml->flags, ml->type,
187726636bf6SSunil Mushran 				     ml->convert_type, ml->highest_blocked);
187826636bf6SSunil Mushran 				__dlm_print_one_lock_resource(res);
187926636bf6SSunil Mushran 				BUG();
188026636bf6SSunil Mushran 			}
18816714d8e8SKurt Hackel 
1882e17e75ecSKurt Hackel 			if (tmpq != queue) {
188326636bf6SSunil Mushran 				c = ml->cookie;
188426636bf6SSunil Mushran 				mlog(0, "Lock cookie %u:%llu was on list %u "
188526636bf6SSunil Mushran 				     "instead of list %u for %.*s\n",
188626636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
188726636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
188826636bf6SSunil Mushran 				     j, ml->list, res->lockname.len,
188926636bf6SSunil Mushran 				     res->lockname.name);
189026636bf6SSunil Mushran 				__dlm_print_one_lock_resource(res);
1891e17e75ecSKurt Hackel 				spin_unlock(&res->spinlock);
1892e17e75ecSKurt Hackel 				continue;
1893e17e75ecSKurt Hackel 			}
1894e17e75ecSKurt Hackel 
18956714d8e8SKurt Hackel 			/* see NOTE above about why we do not update
18966714d8e8SKurt Hackel 			 * to match the master here */
18976714d8e8SKurt Hackel 
18986714d8e8SKurt Hackel 			/* move the lock to its proper place */
18996714d8e8SKurt Hackel 			/* do not alter lock refcount.  switching lists. */
1900f116629dSAkinobu Mita 			list_move_tail(&lock->list, queue);
19016714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
19026714d8e8SKurt Hackel 
19036714d8e8SKurt Hackel 			mlog(0, "just reordered a local lock!\n");
19046714d8e8SKurt Hackel 			continue;
19056714d8e8SKurt Hackel 		}
19066714d8e8SKurt Hackel 
19076714d8e8SKurt Hackel 		/* lock is for another node. */
19086714d8e8SKurt Hackel 		newlock = dlm_new_lock(ml->type, ml->node,
19096714d8e8SKurt Hackel 				       be64_to_cpu(ml->cookie), NULL);
19106714d8e8SKurt Hackel 		if (!newlock) {
19116714d8e8SKurt Hackel 			ret = -ENOMEM;
19126714d8e8SKurt Hackel 			goto leave;
19136714d8e8SKurt Hackel 		}
19146714d8e8SKurt Hackel 		lksb = newlock->lksb;
19156714d8e8SKurt Hackel 		dlm_lock_attach_lockres(newlock, res);
19166714d8e8SKurt Hackel 
19176714d8e8SKurt Hackel 		if (ml->convert_type != LKM_IVMODE) {
19186714d8e8SKurt Hackel 			BUG_ON(queue != &res->converting);
19196714d8e8SKurt Hackel 			newlock->ml.convert_type = ml->convert_type;
19206714d8e8SKurt Hackel 		}
19216714d8e8SKurt Hackel 		lksb->flags |= (ml->flags &
19226714d8e8SKurt Hackel 				(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
19236714d8e8SKurt Hackel 
1924ccd8b1f9SKurt Hackel 		if (ml->type == LKM_NLMODE)
1925ccd8b1f9SKurt Hackel 			goto skip_lvb;
1926ccd8b1f9SKurt Hackel 
1927750e3c65SXue jiufei 		/*
1928750e3c65SXue jiufei 		 * If the lock is in the blocked list it can't have a valid lvb,
1929750e3c65SXue jiufei 		 * so skip it
1930750e3c65SXue jiufei 		 */
1931750e3c65SXue jiufei 		if (ml->list == DLM_BLOCKED_LIST)
1932750e3c65SXue jiufei 			goto skip_lvb;
1933750e3c65SXue jiufei 
19348bc674cbSKurt Hackel 		if (!dlm_lvb_is_empty(mres->lvb)) {
19356714d8e8SKurt Hackel 			if (lksb->flags & DLM_LKSB_PUT_LVB) {
19366714d8e8SKurt Hackel 				/* other node was trying to update
19376714d8e8SKurt Hackel 				 * lvb when node died.  recreate the
19386714d8e8SKurt Hackel 				 * lksb with the updated lvb. */
19396714d8e8SKurt Hackel 				memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1940ccd8b1f9SKurt Hackel 				/* the lock resource lvb update must happen
1941ccd8b1f9SKurt Hackel 				 * NOW, before the spinlock is dropped.
1942ccd8b1f9SKurt Hackel 				 * we no longer wait for the AST to update
1943ccd8b1f9SKurt Hackel 				 * the lvb. */
1944ccd8b1f9SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
19456714d8e8SKurt Hackel 			} else {
19466714d8e8SKurt Hackel 				/* otherwise, the node is sending its
19476714d8e8SKurt Hackel 				 * most recent valid lvb info */
19486714d8e8SKurt Hackel 				BUG_ON(ml->type != LKM_EXMODE &&
19496714d8e8SKurt Hackel 				       ml->type != LKM_PRMODE);
19508bc674cbSKurt Hackel 				if (!dlm_lvb_is_empty(res->lvb) &&
19518bc674cbSKurt Hackel  				    (ml->type == LKM_EXMODE ||
19526714d8e8SKurt Hackel  				     memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1953343e26a4SKurt Hackel  					int i;
1954343e26a4SKurt Hackel  					mlog(ML_ERROR, "%s:%.*s: received bad "
1955343e26a4SKurt Hackel  					     "lvb! type=%d\n", dlm->name,
1956343e26a4SKurt Hackel  					     res->lockname.len,
1957343e26a4SKurt Hackel  					     res->lockname.name, ml->type);
1958343e26a4SKurt Hackel  					printk("lockres lvb=[");
1959343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1960343e26a4SKurt Hackel  						printk("%02x", res->lvb[i]);
1961343e26a4SKurt Hackel  					printk("]\nmigrated lvb=[");
1962343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1963343e26a4SKurt Hackel  						printk("%02x", mres->lvb[i]);
1964343e26a4SKurt Hackel  					printk("]\n");
1965343e26a4SKurt Hackel  					dlm_print_one_lock_resource(res);
19666714d8e8SKurt Hackel  					BUG();
19676714d8e8SKurt Hackel 				}
19686714d8e8SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
19696714d8e8SKurt Hackel 			}
19706714d8e8SKurt Hackel 		}
1971ccd8b1f9SKurt Hackel skip_lvb:
19726714d8e8SKurt Hackel 
19736714d8e8SKurt Hackel 		/* NOTE:
19746714d8e8SKurt Hackel 		 * wrt lock queue ordering and recovery:
19756714d8e8SKurt Hackel 		 *    1. order of locks on granted queue is
19766714d8e8SKurt Hackel 		 *       meaningless.
19776714d8e8SKurt Hackel 		 *    2. order of locks on converting queue is
19786714d8e8SKurt Hackel 		 *       LOST with the node death.  sorry charlie.
19796714d8e8SKurt Hackel 		 *    3. order of locks on the blocked queue is
19806714d8e8SKurt Hackel 		 *       also LOST.
19816714d8e8SKurt Hackel 		 * order of locks does not affect integrity, it
19826714d8e8SKurt Hackel 		 * just means that a lock request may get pushed
19836714d8e8SKurt Hackel 		 * back in line as a result of the node death.
19846714d8e8SKurt Hackel 		 * also note that for a given node the lock order
19856714d8e8SKurt Hackel 		 * for its secondary queue locks is preserved
19866714d8e8SKurt Hackel 		 * relative to each other, but clearly *not*
19876714d8e8SKurt Hackel 		 * preserved relative to locks from other nodes.
19886714d8e8SKurt Hackel 		 */
1989c3187ce5SKurt Hackel 		bad = 0;
19906714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
1991c3187ce5SKurt Hackel 		list_for_each_entry(lock, queue, list) {
1992c3187ce5SKurt Hackel 			if (lock->ml.cookie == ml->cookie) {
199326636bf6SSunil Mushran 				c = lock->ml.cookie;
1994c3187ce5SKurt Hackel 				mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1995c3187ce5SKurt Hackel 				     "exists on this lockres!\n", dlm->name,
1996c3187ce5SKurt Hackel 				     res->lockname.len, res->lockname.name,
199774aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
199874aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)));
1999c3187ce5SKurt Hackel 
2000c3187ce5SKurt Hackel 				mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
2001c3187ce5SKurt Hackel 				     "node=%u, cookie=%u:%llu, queue=%d\n",
2002c3187ce5SKurt Hackel 	      			     ml->type, ml->convert_type, ml->node,
200374aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
200474aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
2005c3187ce5SKurt Hackel 				     ml->list);
2006c3187ce5SKurt Hackel 
2007c3187ce5SKurt Hackel 				__dlm_print_one_lock_resource(res);
2008c3187ce5SKurt Hackel 				bad = 1;
2009c3187ce5SKurt Hackel 				break;
2010c3187ce5SKurt Hackel 			}
2011c3187ce5SKurt Hackel 		}
2012c3187ce5SKurt Hackel 		if (!bad) {
20136714d8e8SKurt Hackel 			dlm_lock_get(newlock);
20146718cb5eSXue jiufei 			if (mres->flags & DLM_MRES_RECOVERY &&
20156718cb5eSXue jiufei 					ml->list == DLM_CONVERTING_LIST &&
20166718cb5eSXue jiufei 					newlock->ml.type >
20176718cb5eSXue jiufei 					newlock->ml.convert_type) {
20186718cb5eSXue jiufei 				/* newlock is doing downconvert, add it to the
20196718cb5eSXue jiufei 				 * head of converting list */
20206718cb5eSXue jiufei 				list_add(&newlock->list, queue);
20216718cb5eSXue jiufei 			} else
20226714d8e8SKurt Hackel 				list_add_tail(&newlock->list, queue);
2023ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: added lock for node %u, "
2024ba2bf218SKurt Hackel 			     "setting refmap bit\n", dlm->name,
2025ba2bf218SKurt Hackel 			     res->lockname.len, res->lockname.name, ml->node);
20268d400b81SSunil Mushran 			dlm_lockres_set_refmap_bit(dlm, res, ml->node);
2027c3187ce5SKurt Hackel 		}
20286714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
20296714d8e8SKurt Hackel 	}
20306714d8e8SKurt Hackel 	mlog(0, "done running all the locks\n");
20316714d8e8SKurt Hackel 
20326714d8e8SKurt Hackel leave:
2033ba2bf218SKurt Hackel 	/* balance the ref taken when the work was queued */
2034ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
2035ba2bf218SKurt Hackel 	dlm_lockres_drop_inflight_ref(dlm, res);
2036ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
2037ba2bf218SKurt Hackel 
2038eb4f73b4SJoseph Qi 	if (ret < 0)
20396714d8e8SKurt Hackel 		mlog_errno(ret);
20406714d8e8SKurt Hackel 
20416714d8e8SKurt Hackel 	return ret;
20426714d8e8SKurt Hackel }
20436714d8e8SKurt Hackel 
dlm_move_lockres_to_recovery_list(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)20446714d8e8SKurt Hackel void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
20456714d8e8SKurt Hackel 				       struct dlm_lock_resource *res)
20466714d8e8SKurt Hackel {
20476714d8e8SKurt Hackel 	int i;
2048800deef3SChristoph Hellwig 	struct list_head *queue;
2049800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
20506714d8e8SKurt Hackel 
2051a524812bSWengang Wang 	assert_spin_locked(&dlm->spinlock);
2052a524812bSWengang Wang 	assert_spin_locked(&res->spinlock);
20536714d8e8SKurt Hackel 	res->state |= DLM_LOCK_RES_RECOVERING;
205469d72b06SKurt Hackel 	if (!list_empty(&res->recovering)) {
205569d72b06SKurt Hackel 		mlog(0,
205669d72b06SKurt Hackel 		     "Recovering res %s:%.*s, is already on recovery list!\n",
205769d72b06SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
20586714d8e8SKurt Hackel 		list_del_init(&res->recovering);
205952987e2aSSunil Mushran 		dlm_lockres_put(res);
206069d72b06SKurt Hackel 	}
206169d72b06SKurt Hackel 	/* We need to hold a reference while on the recovery list */
206269d72b06SKurt Hackel 	dlm_lockres_get(res);
20636714d8e8SKurt Hackel 	list_add_tail(&res->recovering, &dlm->reco.resources);
20646714d8e8SKurt Hackel 
20656714d8e8SKurt Hackel 	/* find any pending locks and put them back on proper list */
20666714d8e8SKurt Hackel 	for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
20676714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
2068800deef3SChristoph Hellwig 		list_for_each_entry_safe(lock, next, queue, list) {
20696714d8e8SKurt Hackel 			dlm_lock_get(lock);
20706714d8e8SKurt Hackel 			if (lock->convert_pending) {
20716714d8e8SKurt Hackel 				/* move converting lock back to granted */
20726714d8e8SKurt Hackel 				mlog(0, "node died with convert pending "
20736714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
20746714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20756714d8e8SKurt Hackel 				dlm_revert_pending_convert(res, lock);
20766714d8e8SKurt Hackel 				lock->convert_pending = 0;
20776714d8e8SKurt Hackel 			} else if (lock->lock_pending) {
20786714d8e8SKurt Hackel 				/* remove pending lock requests completely */
20796714d8e8SKurt Hackel 				BUG_ON(i != DLM_BLOCKED_LIST);
20806714d8e8SKurt Hackel 				mlog(0, "node died with lock pending "
20816714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
20826714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20836714d8e8SKurt Hackel 				/* lock will be floating until ref in
20846714d8e8SKurt Hackel 				 * dlmlock_remote is freed after the network
20856714d8e8SKurt Hackel 				 * call returns.  ok for it to not be on any
20866714d8e8SKurt Hackel 				 * list since no ast can be called
20876714d8e8SKurt Hackel 				 * (the master is dead). */
20886714d8e8SKurt Hackel 				dlm_revert_pending_lock(res, lock);
20896714d8e8SKurt Hackel 				lock->lock_pending = 0;
20906714d8e8SKurt Hackel 			} else if (lock->unlock_pending) {
20916714d8e8SKurt Hackel 				/* if an unlock was in progress, treat as
20926714d8e8SKurt Hackel 				 * if this had completed successfully
20936714d8e8SKurt Hackel 				 * before sending this lock state to the
20946714d8e8SKurt Hackel 				 * new master.  note that the dlm_unlock
20956714d8e8SKurt Hackel 				 * call is still responsible for calling
20966714d8e8SKurt Hackel 				 * the unlockast.  that will happen after
20976714d8e8SKurt Hackel 				 * the network call times out.  for now,
20986714d8e8SKurt Hackel 				 * just move lists to prepare the new
20996714d8e8SKurt Hackel 				 * recovery master.  */
21006714d8e8SKurt Hackel 				BUG_ON(i != DLM_GRANTED_LIST);
21016714d8e8SKurt Hackel 				mlog(0, "node died with unlock pending "
21026714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
21036714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
21046714d8e8SKurt Hackel 				dlm_commit_pending_unlock(res, lock);
21056714d8e8SKurt Hackel 				lock->unlock_pending = 0;
21066714d8e8SKurt Hackel 			} else if (lock->cancel_pending) {
21076714d8e8SKurt Hackel 				/* if a cancel was in progress, treat as
21086714d8e8SKurt Hackel 				 * if this had completed successfully
21096714d8e8SKurt Hackel 				 * before sending this lock state to the
21106714d8e8SKurt Hackel 				 * new master */
21116714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
21126714d8e8SKurt Hackel 				mlog(0, "node died with cancel pending "
21136714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
21146714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
21156714d8e8SKurt Hackel 				dlm_commit_pending_cancel(res, lock);
21166714d8e8SKurt Hackel 				lock->cancel_pending = 0;
21176714d8e8SKurt Hackel 			}
21186714d8e8SKurt Hackel 			dlm_lock_put(lock);
21196714d8e8SKurt Hackel 		}
21206714d8e8SKurt Hackel 	}
21216714d8e8SKurt Hackel }
21226714d8e8SKurt Hackel 
21236714d8e8SKurt Hackel 
21246714d8e8SKurt Hackel 
21256714d8e8SKurt Hackel /* removes all recovered locks from the recovery list.
21266714d8e8SKurt Hackel  * sets the res->owner to the new master.
21276714d8e8SKurt Hackel  * unsets the RECOVERY flag and wakes waiters. */
dlm_finish_local_lockres_recovery(struct dlm_ctxt * dlm,u8 dead_node,u8 new_master)21286714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
21296714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master)
21306714d8e8SKurt Hackel {
21316714d8e8SKurt Hackel 	int i;
213281f2094aSMark Fasheh 	struct hlist_head *bucket;
2133800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
21346714d8e8SKurt Hackel 
21356714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
21366714d8e8SKurt Hackel 
2137800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
21386714d8e8SKurt Hackel 		if (res->owner == dead_node) {
21390afbba13SSunil Mushran 			mlog(0, "%s: res %.*s, Changing owner from %u to %u\n",
21400afbba13SSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name,
21410afbba13SSunil Mushran 			     res->owner, new_master);
21426714d8e8SKurt Hackel 			list_del_init(&res->recovering);
21436714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
2144ba2bf218SKurt Hackel 			/* new_master has our reference from
2145ba2bf218SKurt Hackel 			 * the lock state sent during recovery */
21466714d8e8SKurt Hackel 			dlm_change_lockres_owner(dlm, res, new_master);
21476714d8e8SKurt Hackel 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2148ba2bf218SKurt Hackel 			if (__dlm_lockres_has_locks(res))
21496714d8e8SKurt Hackel 				__dlm_dirty_lockres(dlm, res);
21506714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
21516714d8e8SKurt Hackel 			wake_up(&res->wq);
215269d72b06SKurt Hackel 			dlm_lockres_put(res);
21536714d8e8SKurt Hackel 		}
21546714d8e8SKurt Hackel 	}
21556714d8e8SKurt Hackel 
21566714d8e8SKurt Hackel 	/* this will become unnecessary eventually, but
21576714d8e8SKurt Hackel 	 * for now we need to run the whole hash, clear
21586714d8e8SKurt Hackel 	 * the RECOVERING state and set the owner
21596714d8e8SKurt Hackel 	 * if necessary */
216081f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
216103d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
2162b67bfe0dSSasha Levin 		hlist_for_each_entry(res, bucket, hash_node) {
2163814ce694SJiufei Xue 			if (res->state & DLM_LOCK_RES_RECOVERY_WAITING) {
2164814ce694SJiufei Xue 				spin_lock(&res->spinlock);
2165814ce694SJiufei Xue 				res->state &= ~DLM_LOCK_RES_RECOVERY_WAITING;
2166814ce694SJiufei Xue 				spin_unlock(&res->spinlock);
2167814ce694SJiufei Xue 				wake_up(&res->wq);
2168814ce694SJiufei Xue 			}
2169814ce694SJiufei Xue 
21700afbba13SSunil Mushran 			if (!(res->state & DLM_LOCK_RES_RECOVERING))
21710afbba13SSunil Mushran 				continue;
21720afbba13SSunil Mushran 
21730afbba13SSunil Mushran 			if (res->owner != dead_node &&
21740afbba13SSunil Mushran 			    res->owner != dlm->node_num)
21756714d8e8SKurt Hackel 				continue;
21766714d8e8SKurt Hackel 
2177c03872f5SKurt Hackel 			if (!list_empty(&res->recovering)) {
2178c03872f5SKurt Hackel 				list_del_init(&res->recovering);
217969d72b06SKurt Hackel 				dlm_lockres_put(res);
2180c03872f5SKurt Hackel 			}
21810afbba13SSunil Mushran 
2182ba2bf218SKurt Hackel 			/* new_master has our reference from
2183ba2bf218SKurt Hackel 			 * the lock state sent during recovery */
21840afbba13SSunil Mushran 			mlog(0, "%s: res %.*s, Changing owner from %u to %u\n",
21850afbba13SSunil Mushran 			     dlm->name, res->lockname.len, res->lockname.name,
21860afbba13SSunil Mushran 			     res->owner, new_master);
21870afbba13SSunil Mushran 			spin_lock(&res->spinlock);
21886714d8e8SKurt Hackel 			dlm_change_lockres_owner(dlm, res, new_master);
21896714d8e8SKurt Hackel 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2190ba2bf218SKurt Hackel 			if (__dlm_lockres_has_locks(res))
21916714d8e8SKurt Hackel 				__dlm_dirty_lockres(dlm, res);
21926714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
21936714d8e8SKurt Hackel 			wake_up(&res->wq);
21946714d8e8SKurt Hackel 		}
21956714d8e8SKurt Hackel 	}
21966714d8e8SKurt Hackel }
21976714d8e8SKurt Hackel 
dlm_lvb_needs_invalidation(struct dlm_lock * lock,int local)21986714d8e8SKurt Hackel static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
21996714d8e8SKurt Hackel {
22006714d8e8SKurt Hackel 	if (local) {
22016714d8e8SKurt Hackel 		if (lock->ml.type != LKM_EXMODE &&
22026714d8e8SKurt Hackel 		    lock->ml.type != LKM_PRMODE)
22036714d8e8SKurt Hackel 			return 1;
22046714d8e8SKurt Hackel 	} else if (lock->ml.type == LKM_EXMODE)
22056714d8e8SKurt Hackel 		return 1;
22066714d8e8SKurt Hackel 	return 0;
22076714d8e8SKurt Hackel }
22086714d8e8SKurt Hackel 
dlm_revalidate_lvb(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 dead_node)22096714d8e8SKurt Hackel static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
22106714d8e8SKurt Hackel 			       struct dlm_lock_resource *res, u8 dead_node)
22116714d8e8SKurt Hackel {
2212800deef3SChristoph Hellwig 	struct list_head *queue;
22136714d8e8SKurt Hackel 	struct dlm_lock *lock;
22146714d8e8SKurt Hackel 	int blank_lvb = 0, local = 0;
22156714d8e8SKurt Hackel 	int i;
22166714d8e8SKurt Hackel 	u8 search_node;
22176714d8e8SKurt Hackel 
22186714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
22196714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
22206714d8e8SKurt Hackel 
22216714d8e8SKurt Hackel 	if (res->owner == dlm->node_num)
22226714d8e8SKurt Hackel 		/* if this node owned the lockres, and if the dead node
22236714d8e8SKurt Hackel 		 * had an EX when he died, blank out the lvb */
22246714d8e8SKurt Hackel 		search_node = dead_node;
22256714d8e8SKurt Hackel 	else {
22266714d8e8SKurt Hackel 		/* if this is a secondary lockres, and we had no EX or PR
22276714d8e8SKurt Hackel 		 * locks granted, we can no longer trust the lvb */
22286714d8e8SKurt Hackel 		search_node = dlm->node_num;
22296714d8e8SKurt Hackel 		local = 1;  /* check local state for valid lvb */
22306714d8e8SKurt Hackel 	}
22316714d8e8SKurt Hackel 
22326714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
22336714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
2234800deef3SChristoph Hellwig 		list_for_each_entry(lock, queue, list) {
22356714d8e8SKurt Hackel 			if (lock->ml.node == search_node) {
22366714d8e8SKurt Hackel 				if (dlm_lvb_needs_invalidation(lock, local)) {
22376714d8e8SKurt Hackel 					/* zero the lksb lvb and lockres lvb */
22386714d8e8SKurt Hackel 					blank_lvb = 1;
22396714d8e8SKurt Hackel 					memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
22406714d8e8SKurt Hackel 				}
22416714d8e8SKurt Hackel 			}
22426714d8e8SKurt Hackel 		}
22436714d8e8SKurt Hackel 	}
22446714d8e8SKurt Hackel 
22456714d8e8SKurt Hackel 	if (blank_lvb) {
22466714d8e8SKurt Hackel 		mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
22476714d8e8SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
22486714d8e8SKurt Hackel 		memset(res->lvb, 0, DLM_LVB_LEN);
22496714d8e8SKurt Hackel 	}
22506714d8e8SKurt Hackel }
22516714d8e8SKurt Hackel 
dlm_free_dead_locks(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 dead_node)22526714d8e8SKurt Hackel static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
22536714d8e8SKurt Hackel 				struct dlm_lock_resource *res, u8 dead_node)
22546714d8e8SKurt Hackel {
2255800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
2256ba2bf218SKurt Hackel 	unsigned int freed = 0;
22576714d8e8SKurt Hackel 
22586714d8e8SKurt Hackel 	/* this node is the lockres master:
22596714d8e8SKurt Hackel 	 * 1) remove any stale locks for the dead node
22606714d8e8SKurt Hackel 	 * 2) if the dead node had an EX when he died, blank out the lvb
22616714d8e8SKurt Hackel 	 */
22626714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
22636714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
22646714d8e8SKurt Hackel 
22652c5c54acSSunil Mushran 	/* We do two dlm_lock_put(). One for removing from list and the other is
22662c5c54acSSunil Mushran 	 * to force the DLM_UNLOCK_FREE_LOCK action so as to free the locks */
22672c5c54acSSunil Mushran 
22686714d8e8SKurt Hackel 	/* TODO: check pending_asts, pending_basts here */
2269800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->granted, list) {
22706714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22716714d8e8SKurt Hackel 			list_del_init(&lock->list);
22726714d8e8SKurt Hackel 			dlm_lock_put(lock);
22732c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22742c5c54acSSunil Mushran 			dlm_lock_put(lock);
2275ba2bf218SKurt Hackel 			freed++;
22766714d8e8SKurt Hackel 		}
22776714d8e8SKurt Hackel 	}
2278800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->converting, list) {
22796714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22806714d8e8SKurt Hackel 			list_del_init(&lock->list);
22816714d8e8SKurt Hackel 			dlm_lock_put(lock);
22822c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22832c5c54acSSunil Mushran 			dlm_lock_put(lock);
2284ba2bf218SKurt Hackel 			freed++;
22856714d8e8SKurt Hackel 		}
22866714d8e8SKurt Hackel 	}
2287800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->blocked, list) {
22886714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22896714d8e8SKurt Hackel 			list_del_init(&lock->list);
22906714d8e8SKurt Hackel 			dlm_lock_put(lock);
22912c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22922c5c54acSSunil Mushran 			dlm_lock_put(lock);
2293ba2bf218SKurt Hackel 			freed++;
22946714d8e8SKurt Hackel 		}
22956714d8e8SKurt Hackel 	}
22966714d8e8SKurt Hackel 
2297ba2bf218SKurt Hackel 	if (freed) {
2298ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2299ba2bf218SKurt Hackel 		     "dropping ref from lockres\n", dlm->name,
2300ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, freed, dead_node);
2301cda70ba8SSunil Mushran 		if(!test_bit(dead_node, res->refmap)) {
2302cda70ba8SSunil Mushran 			mlog(ML_ERROR, "%s:%.*s: freed %u locks for dead node %u, "
2303cda70ba8SSunil Mushran 			     "but ref was not set\n", dlm->name,
2304cda70ba8SSunil Mushran 			     res->lockname.len, res->lockname.name, freed, dead_node);
2305cda70ba8SSunil Mushran 			__dlm_print_one_lock_resource(res);
2306cda70ba8SSunil Mushran 		}
2307814ce694SJiufei Xue 		res->state |= DLM_LOCK_RES_RECOVERY_WAITING;
23088d400b81SSunil Mushran 		dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2309ba2bf218SKurt Hackel 	} else if (test_bit(dead_node, res->refmap)) {
2310ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2311ba2bf218SKurt Hackel 		     "no locks and had not purged before dying\n", dlm->name,
2312ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
23138d400b81SSunil Mushran 		dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2314ba2bf218SKurt Hackel 	}
2315ba2bf218SKurt Hackel 
23166714d8e8SKurt Hackel 	/* do not kick thread yet */
23176714d8e8SKurt Hackel 	__dlm_dirty_lockres(dlm, res);
23186714d8e8SKurt Hackel }
23196714d8e8SKurt Hackel 
dlm_do_local_recovery_cleanup(struct dlm_ctxt * dlm,u8 dead_node)23206714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
23216714d8e8SKurt Hackel {
23226714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
23236714d8e8SKurt Hackel 	int i;
232481f2094aSMark Fasheh 	struct hlist_head *bucket;
2325ee8f7fcbSpiaojun 	struct hlist_node *tmp;
2326e2faea4cSKurt Hackel 	struct dlm_lock *lock;
23276714d8e8SKurt Hackel 
23286714d8e8SKurt Hackel 
23296714d8e8SKurt Hackel 	/* purge any stale mles */
23306714d8e8SKurt Hackel 	dlm_clean_master_list(dlm, dead_node);
23316714d8e8SKurt Hackel 
23326714d8e8SKurt Hackel 	/*
23336714d8e8SKurt Hackel 	 * now clean up all lock resources.  there are two rules:
23346714d8e8SKurt Hackel 	 *
23356714d8e8SKurt Hackel 	 * 1) if the dead node was the master, move the lockres
23366714d8e8SKurt Hackel 	 *    to the recovering list.  set the RECOVERING flag.
23376714d8e8SKurt Hackel 	 *    this lockres needs to be cleaned up before it can
23386714d8e8SKurt Hackel 	 *    be used further.
23396714d8e8SKurt Hackel 	 *
23406714d8e8SKurt Hackel 	 * 2) if this node was the master, remove all locks from
23416714d8e8SKurt Hackel 	 *    each of the lockres queues that were owned by the
23426714d8e8SKurt Hackel 	 *    dead node.  once recovery finishes, the dlm thread
23436714d8e8SKurt Hackel 	 *    can be kicked again to see if any ASTs or BASTs
23446714d8e8SKurt Hackel 	 *    need to be fired as a result.
23456714d8e8SKurt Hackel 	 */
234681f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
234703d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
2348ee8f7fcbSpiaojun 		hlist_for_each_entry_safe(res, tmp, bucket, hash_node) {
2349e2faea4cSKurt Hackel  			/* always prune any $RECOVERY entries for dead nodes,
2350e2faea4cSKurt Hackel  			 * otherwise hangs can occur during later recovery */
23516714d8e8SKurt Hackel 			if (dlm_is_recovery_lock(res->lockname.name,
2352e2faea4cSKurt Hackel 						 res->lockname.len)) {
2353e2faea4cSKurt Hackel 				spin_lock(&res->spinlock);
2354e2faea4cSKurt Hackel 				list_for_each_entry(lock, &res->granted, list) {
2355e2faea4cSKurt Hackel 					if (lock->ml.node == dead_node) {
2356e2faea4cSKurt Hackel 						mlog(0, "AHA! there was "
2357e2faea4cSKurt Hackel 						     "a $RECOVERY lock for dead "
2358e2faea4cSKurt Hackel 						     "node %u (%s)!\n",
2359e2faea4cSKurt Hackel 						     dead_node, dlm->name);
2360e2faea4cSKurt Hackel 						list_del_init(&lock->list);
2361e2faea4cSKurt Hackel 						dlm_lock_put(lock);
2362b934beafSXue jiufei 						/* Can't schedule
2363b934beafSXue jiufei 						 * DLM_UNLOCK_FREE_LOCK
2364b934beafSXue jiufei 						 * - do manually */
2365b934beafSXue jiufei 						dlm_lock_put(lock);
2366e2faea4cSKurt Hackel 						break;
2367e2faea4cSKurt Hackel 					}
2368e2faea4cSKurt Hackel 				}
2369ee8f7fcbSpiaojun 
2370ee8f7fcbSpiaojun 				if ((res->owner == dead_node) &&
2371ee8f7fcbSpiaojun 							(res->state & DLM_LOCK_RES_DROPPING_REF)) {
2372ee8f7fcbSpiaojun 					dlm_lockres_get(res);
2373ee8f7fcbSpiaojun 					__dlm_do_purge_lockres(dlm, res);
2374ee8f7fcbSpiaojun 					spin_unlock(&res->spinlock);
2375ee8f7fcbSpiaojun 					wake_up(&res->wq);
2376ee8f7fcbSpiaojun 					dlm_lockres_put(res);
2377ee8f7fcbSpiaojun 					continue;
2378ee8f7fcbSpiaojun 				} else if (res->owner == dlm->node_num)
2379ee8f7fcbSpiaojun 					dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2380e2faea4cSKurt Hackel 				spin_unlock(&res->spinlock);
23816714d8e8SKurt Hackel 				continue;
2382e2faea4cSKurt Hackel 			}
23836714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
23846714d8e8SKurt Hackel 			/* zero the lvb if necessary */
23856714d8e8SKurt Hackel 			dlm_revalidate_lvb(dlm, res, dead_node);
2386ba2bf218SKurt Hackel 			if (res->owner == dead_node) {
2387a524812bSWengang Wang 				if (res->state & DLM_LOCK_RES_DROPPING_REF) {
23888c034396Sxuejiufei 					mlog(0, "%s:%.*s: owned by "
23898c034396Sxuejiufei 						"dead node %u, this node was "
2390ee8f7fcbSpiaojun 						"dropping its ref when master died. "
2391ee8f7fcbSpiaojun 						"continue, purging the lockres.\n",
23928decab3cSSunil Mushran 						dlm->name, res->lockname.len,
23938c034396Sxuejiufei 						res->lockname.name, dead_node);
2394ee8f7fcbSpiaojun 					dlm_lockres_get(res);
2395ee8f7fcbSpiaojun 					__dlm_do_purge_lockres(dlm, res);
2396ee8f7fcbSpiaojun 					spin_unlock(&res->spinlock);
2397ee8f7fcbSpiaojun 					wake_up(&res->wq);
2398ee8f7fcbSpiaojun 					dlm_lockres_put(res);
2399ee8f7fcbSpiaojun 					continue;
24008c034396Sxuejiufei 				}
24011c019671SChangwei Ge 				dlm_move_lockres_to_recovery_list(dlm, res);
2402ba2bf218SKurt Hackel 			} else if (res->owner == dlm->node_num) {
24036714d8e8SKurt Hackel 				dlm_free_dead_locks(dlm, res, dead_node);
24046714d8e8SKurt Hackel 				__dlm_lockres_calc_usage(dlm, res);
240569b2bd16SXue jiufei 			} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
240669b2bd16SXue jiufei 				if (test_bit(dead_node, res->refmap)) {
240769b2bd16SXue jiufei 					mlog(0, "%s:%.*s: dead node %u had a ref, but had "
240869b2bd16SXue jiufei 						"no locks and had not purged before dying\n",
240969b2bd16SXue jiufei 						dlm->name, res->lockname.len,
241069b2bd16SXue jiufei 						res->lockname.name, dead_node);
241169b2bd16SXue jiufei 					dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
241269b2bd16SXue jiufei 				}
24136714d8e8SKurt Hackel 			}
24146714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
24156714d8e8SKurt Hackel 		}
24166714d8e8SKurt Hackel 	}
24176714d8e8SKurt Hackel 
24186714d8e8SKurt Hackel }
24196714d8e8SKurt Hackel 
__dlm_hb_node_down(struct dlm_ctxt * dlm,int idx)24206714d8e8SKurt Hackel static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
24216714d8e8SKurt Hackel {
24226714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
24236714d8e8SKurt Hackel 
2424466d1a45SKurt Hackel 	if (dlm->reco.new_master == idx) {
2425466d1a45SKurt Hackel 		mlog(0, "%s: recovery master %d just died\n",
2426466d1a45SKurt Hackel 		     dlm->name, idx);
2427466d1a45SKurt Hackel 		if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2428466d1a45SKurt Hackel 			/* finalize1 was reached, so it is safe to clear
2429466d1a45SKurt Hackel 			 * the new_master and dead_node.  that recovery
2430466d1a45SKurt Hackel 			 * is complete. */
2431466d1a45SKurt Hackel 			mlog(0, "%s: dead master %d had reached "
2432466d1a45SKurt Hackel 			     "finalize1 state, clearing\n", dlm->name, idx);
2433466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2434466d1a45SKurt Hackel 			__dlm_reset_recovery(dlm);
2435466d1a45SKurt Hackel 		}
2436466d1a45SKurt Hackel 	}
2437466d1a45SKurt Hackel 
24382d4b1cbbSTao Ma 	/* Clean up join state on node death. */
24392d4b1cbbSTao Ma 	if (dlm->joining_node == idx) {
24402d4b1cbbSTao Ma 		mlog(0, "Clearing join state for node %u\n", idx);
24412d4b1cbbSTao Ma 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
24422d4b1cbbSTao Ma 	}
24432d4b1cbbSTao Ma 
24446714d8e8SKurt Hackel 	/* check to see if the node is already considered dead */
24456714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->live_nodes_map)) {
24466714d8e8SKurt Hackel 		mlog(0, "for domain %s, node %d is already dead. "
24476714d8e8SKurt Hackel 		     "another node likely did recovery already.\n",
24486714d8e8SKurt Hackel 		     dlm->name, idx);
24496714d8e8SKurt Hackel 		return;
24506714d8e8SKurt Hackel 	}
24516714d8e8SKurt Hackel 
24526714d8e8SKurt Hackel 	/* check to see if we do not care about this node */
24536714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->domain_map)) {
24546714d8e8SKurt Hackel 		/* This also catches the case that we get a node down
24556714d8e8SKurt Hackel 		 * but haven't joined the domain yet. */
24566714d8e8SKurt Hackel 		mlog(0, "node %u already removed from domain!\n", idx);
24576714d8e8SKurt Hackel 		return;
24586714d8e8SKurt Hackel 	}
24596714d8e8SKurt Hackel 
24606714d8e8SKurt Hackel 	clear_bit(idx, dlm->live_nodes_map);
24616714d8e8SKurt Hackel 
24626714d8e8SKurt Hackel 	/* make sure local cleanup occurs before the heartbeat events */
24636714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->recovery_map))
24646714d8e8SKurt Hackel 		dlm_do_local_recovery_cleanup(dlm, idx);
24656714d8e8SKurt Hackel 
24666714d8e8SKurt Hackel 	/* notify anything attached to the heartbeat events */
24676714d8e8SKurt Hackel 	dlm_hb_event_notify_attached(dlm, idx, 0);
24686714d8e8SKurt Hackel 
24696714d8e8SKurt Hackel 	mlog(0, "node %u being removed from domain map!\n", idx);
24706714d8e8SKurt Hackel 	clear_bit(idx, dlm->domain_map);
2471bddefdeeSSunil Mushran 	clear_bit(idx, dlm->exit_domain_map);
24726714d8e8SKurt Hackel 	/* wake up migration waiters if a node goes down.
24736714d8e8SKurt Hackel 	 * perhaps later we can genericize this for other waiters. */
24746714d8e8SKurt Hackel 	wake_up(&dlm->migration_wq);
24756714d8e8SKurt Hackel 
24766714d8e8SKurt Hackel 	set_bit(idx, dlm->recovery_map);
24776714d8e8SKurt Hackel }
24786714d8e8SKurt Hackel 
dlm_hb_node_down_cb(struct o2nm_node * node,int idx,void * data)24796714d8e8SKurt Hackel void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
24806714d8e8SKurt Hackel {
24816714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
24826714d8e8SKurt Hackel 
24836714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
24846714d8e8SKurt Hackel 		return;
24856714d8e8SKurt Hackel 
24866561168cSMark Fasheh 	/*
24876561168cSMark Fasheh 	 * This will notify any dlm users that a node in our domain
24886561168cSMark Fasheh 	 * went away without notifying us first.
24896561168cSMark Fasheh 	 */
24906561168cSMark Fasheh 	if (test_bit(idx, dlm->domain_map))
24916561168cSMark Fasheh 		dlm_fire_domain_eviction_callbacks(dlm, idx);
24926561168cSMark Fasheh 
24936714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
24946714d8e8SKurt Hackel 	__dlm_hb_node_down(dlm, idx);
24956714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
24966714d8e8SKurt Hackel 
24976714d8e8SKurt Hackel 	dlm_put(dlm);
24986714d8e8SKurt Hackel }
24996714d8e8SKurt Hackel 
dlm_hb_node_up_cb(struct o2nm_node * node,int idx,void * data)25006714d8e8SKurt Hackel void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
25016714d8e8SKurt Hackel {
25026714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
25036714d8e8SKurt Hackel 
25046714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
25056714d8e8SKurt Hackel 		return;
25066714d8e8SKurt Hackel 
25076714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
25086714d8e8SKurt Hackel 	set_bit(idx, dlm->live_nodes_map);
2509e2faea4cSKurt Hackel 	/* do NOT notify mle attached to the heartbeat events.
2510e2faea4cSKurt Hackel 	 * new nodes are not interesting in mastery until joined. */
25116714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
25126714d8e8SKurt Hackel 
25136714d8e8SKurt Hackel 	dlm_put(dlm);
25146714d8e8SKurt Hackel }
25156714d8e8SKurt Hackel 
dlm_reco_ast(void * astdata)25166714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata)
25176714d8e8SKurt Hackel {
25186714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
25196714d8e8SKurt Hackel 	mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
25206714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
25216714d8e8SKurt Hackel }
dlm_reco_bast(void * astdata,int blocked_type)25226714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type)
25236714d8e8SKurt Hackel {
25246714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
25256714d8e8SKurt Hackel 	mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
25266714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
25276714d8e8SKurt Hackel }
dlm_reco_unlock_ast(void * astdata,enum dlm_status st)25286714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
25296714d8e8SKurt Hackel {
25306714d8e8SKurt Hackel 	mlog(0, "unlockast for recovery lock fired!\n");
25316714d8e8SKurt Hackel }
25326714d8e8SKurt Hackel 
2533e2faea4cSKurt Hackel /*
2534e2faea4cSKurt Hackel  * dlm_pick_recovery_master will continually attempt to use
2535e2faea4cSKurt Hackel  * dlmlock() on the special "$RECOVERY" lockres with the
2536e2faea4cSKurt Hackel  * LKM_NOQUEUE flag to get an EX.  every thread that enters
2537e2faea4cSKurt Hackel  * this function on each node racing to become the recovery
2538e2faea4cSKurt Hackel  * master will not stop attempting this until either:
2539e2faea4cSKurt Hackel  * a) this node gets the EX (and becomes the recovery master),
2540e2faea4cSKurt Hackel  * or b) dlm->reco.new_master gets set to some nodenum
2541e2faea4cSKurt Hackel  * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2542e2faea4cSKurt Hackel  * so each time a recovery master is needed, the entire cluster
2543e2faea4cSKurt Hackel  * will sync at this point.  if the new master dies, that will
2544e2faea4cSKurt Hackel  * be detected in dlm_do_recovery */
dlm_pick_recovery_master(struct dlm_ctxt * dlm)25456714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
25466714d8e8SKurt Hackel {
25476714d8e8SKurt Hackel 	enum dlm_status ret;
25486714d8e8SKurt Hackel 	struct dlm_lockstatus lksb;
25496714d8e8SKurt Hackel 	int status = -EINVAL;
25506714d8e8SKurt Hackel 
25516714d8e8SKurt Hackel 	mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
25526714d8e8SKurt Hackel 	     dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2553e2faea4cSKurt Hackel again:
25546714d8e8SKurt Hackel 	memset(&lksb, 0, sizeof(lksb));
25556714d8e8SKurt Hackel 
25566714d8e8SKurt Hackel 	ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
25573384f3dfSMark Fasheh 		      DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
25583384f3dfSMark Fasheh 		      dlm_reco_ast, dlm, dlm_reco_bast);
25596714d8e8SKurt Hackel 
2560e2faea4cSKurt Hackel 	mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2561e2faea4cSKurt Hackel 	     dlm->name, ret, lksb.status);
2562e2faea4cSKurt Hackel 
25636714d8e8SKurt Hackel 	if (ret == DLM_NORMAL) {
25646714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
25656714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
2566e2faea4cSKurt Hackel 
2567e2faea4cSKurt Hackel 		/* got the EX lock.  check to see if another node
2568e2faea4cSKurt Hackel 		 * just became the reco master */
2569e2faea4cSKurt Hackel 		if (dlm_reco_master_ready(dlm)) {
2570e2faea4cSKurt Hackel 			mlog(0, "%s: got reco EX lock, but %u will "
2571e2faea4cSKurt Hackel 			     "do the recovery\n", dlm->name,
2572e2faea4cSKurt Hackel 			     dlm->reco.new_master);
2573e2faea4cSKurt Hackel 			status = -EEXIST;
2574e2faea4cSKurt Hackel 		} else {
2575898effacSKurt Hackel 			status = 0;
2576898effacSKurt Hackel 
2577898effacSKurt Hackel 			/* see if recovery was already finished elsewhere */
2578898effacSKurt Hackel 			spin_lock(&dlm->spinlock);
2579898effacSKurt Hackel 			if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2580898effacSKurt Hackel 				status = -EINVAL;
2581898effacSKurt Hackel 				mlog(0, "%s: got reco EX lock, but "
2582898effacSKurt Hackel 				     "node got recovered already\n", dlm->name);
2583898effacSKurt Hackel 				if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2584898effacSKurt Hackel 					mlog(ML_ERROR, "%s: new master is %u "
2585898effacSKurt Hackel 					     "but no dead node!\n",
2586898effacSKurt Hackel 					     dlm->name, dlm->reco.new_master);
2587898effacSKurt Hackel 					BUG();
2588898effacSKurt Hackel 				}
2589898effacSKurt Hackel 			}
2590898effacSKurt Hackel 			spin_unlock(&dlm->spinlock);
2591898effacSKurt Hackel 		}
2592898effacSKurt Hackel 
2593898effacSKurt Hackel 		/* if this node has actually become the recovery master,
2594898effacSKurt Hackel 		 * set the master and send the messages to begin recovery */
2595898effacSKurt Hackel 		if (!status) {
2596898effacSKurt Hackel 			mlog(0, "%s: dead=%u, this=%u, sending "
2597898effacSKurt Hackel 			     "begin_reco now\n", dlm->name,
2598898effacSKurt Hackel 			     dlm->reco.dead_node, dlm->node_num);
25996714d8e8SKurt Hackel 			status = dlm_send_begin_reco_message(dlm,
26006714d8e8SKurt Hackel 				      dlm->reco.dead_node);
2601e2faea4cSKurt Hackel 			/* this always succeeds */
2602e2faea4cSKurt Hackel 			BUG_ON(status);
2603e2faea4cSKurt Hackel 
2604e2faea4cSKurt Hackel 			/* set the new_master to this node */
2605e2faea4cSKurt Hackel 			spin_lock(&dlm->spinlock);
2606ab27eb6fSKurt Hackel 			dlm_set_reco_master(dlm, dlm->node_num);
2607e2faea4cSKurt Hackel 			spin_unlock(&dlm->spinlock);
2608e2faea4cSKurt Hackel 		}
26096714d8e8SKurt Hackel 
26106714d8e8SKurt Hackel 		/* recovery lock is a special case.  ast will not get fired,
26116714d8e8SKurt Hackel 		 * so just go ahead and unlock it. */
26126714d8e8SKurt Hackel 		ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2613e2faea4cSKurt Hackel 		if (ret == DLM_DENIED) {
2614e2faea4cSKurt Hackel 			mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2615e2faea4cSKurt Hackel 			ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2616e2faea4cSKurt Hackel 		}
26176714d8e8SKurt Hackel 		if (ret != DLM_NORMAL) {
26186714d8e8SKurt Hackel 			/* this would really suck. this could only happen
26196714d8e8SKurt Hackel 			 * if there was a network error during the unlock
26206714d8e8SKurt Hackel 			 * because of node death.  this means the unlock
26216714d8e8SKurt Hackel 			 * is actually "done" and the lock structure is
26226714d8e8SKurt Hackel 			 * even freed.  we can continue, but only
26236714d8e8SKurt Hackel 			 * because this specific lock name is special. */
2624e2faea4cSKurt Hackel 			mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
26256714d8e8SKurt Hackel 		}
26266714d8e8SKurt Hackel 	} else if (ret == DLM_NOTQUEUED) {
26276714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
26286714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
26296714d8e8SKurt Hackel 		/* another node is master. wait on
2630e2faea4cSKurt Hackel 		 * reco.new_master != O2NM_INVALID_NODE_NUM
2631e2faea4cSKurt Hackel 		 * for at most one second */
2632e2faea4cSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
2633e2faea4cSKurt Hackel 					 dlm_reco_master_ready(dlm),
2634e2faea4cSKurt Hackel 					 msecs_to_jiffies(1000));
2635e2faea4cSKurt Hackel 		if (!dlm_reco_master_ready(dlm)) {
2636e2faea4cSKurt Hackel 			mlog(0, "%s: reco master taking awhile\n",
2637e2faea4cSKurt Hackel 			     dlm->name);
2638e2faea4cSKurt Hackel 			goto again;
2639e2faea4cSKurt Hackel 		}
2640e2faea4cSKurt Hackel 		/* another node has informed this one that it is reco master */
2641e2faea4cSKurt Hackel 		mlog(0, "%s: reco master %u is ready to recover %u\n",
2642e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
26436714d8e8SKurt Hackel 		status = -EEXIST;
2644c8df412eSKurt Hackel 	} else if (ret == DLM_RECOVERING) {
2645c8df412eSKurt Hackel 		mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2646c8df412eSKurt Hackel 		     dlm->name, dlm->node_num);
2647c8df412eSKurt Hackel 		goto again;
2648e2faea4cSKurt Hackel 	} else {
2649e2faea4cSKurt Hackel 		struct dlm_lock_resource *res;
2650e2faea4cSKurt Hackel 
2651e2faea4cSKurt Hackel 		/* dlmlock returned something other than NOTQUEUED or NORMAL */
2652e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2653e2faea4cSKurt Hackel 		     "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2654e2faea4cSKurt Hackel 		     dlm_errname(lksb.status));
2655e2faea4cSKurt Hackel 		res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2656e2faea4cSKurt Hackel 					 DLM_RECOVERY_LOCK_NAME_LEN);
2657e2faea4cSKurt Hackel 		if (res) {
2658e2faea4cSKurt Hackel 			dlm_print_one_lock_resource(res);
2659e2faea4cSKurt Hackel 			dlm_lockres_put(res);
2660e2faea4cSKurt Hackel 		} else {
2661e2faea4cSKurt Hackel 			mlog(ML_ERROR, "recovery lock not found\n");
2662e2faea4cSKurt Hackel 		}
2663e2faea4cSKurt Hackel 		BUG();
26646714d8e8SKurt Hackel 	}
26656714d8e8SKurt Hackel 
26666714d8e8SKurt Hackel 	return status;
26676714d8e8SKurt Hackel }
26686714d8e8SKurt Hackel 
dlm_send_begin_reco_message(struct dlm_ctxt * dlm,u8 dead_node)26696714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
26706714d8e8SKurt Hackel {
26716714d8e8SKurt Hackel 	struct dlm_begin_reco br;
26726714d8e8SKurt Hackel 	int ret = 0;
26736714d8e8SKurt Hackel 	struct dlm_node_iter iter;
26746714d8e8SKurt Hackel 	int nodenum;
26756714d8e8SKurt Hackel 	int status;
26766714d8e8SKurt Hackel 
2677d6dea6e9SKurt Hackel 	mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
26786714d8e8SKurt Hackel 
26796714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
26806714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
26816714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
26826714d8e8SKurt Hackel 
26836714d8e8SKurt Hackel 	clear_bit(dead_node, iter.node_map);
26846714d8e8SKurt Hackel 
26856714d8e8SKurt Hackel 	memset(&br, 0, sizeof(br));
26866714d8e8SKurt Hackel 	br.node_idx = dlm->node_num;
26876714d8e8SKurt Hackel 	br.dead_node = dead_node;
26886714d8e8SKurt Hackel 
26896714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
26906714d8e8SKurt Hackel 		ret = 0;
26916714d8e8SKurt Hackel 		if (nodenum == dead_node) {
26926714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to dead node "
26936714d8e8SKurt Hackel 				  "%u\n", dead_node);
26946714d8e8SKurt Hackel 			continue;
26956714d8e8SKurt Hackel 		}
26966714d8e8SKurt Hackel 		if (nodenum == dlm->node_num) {
26976714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to self\n");
26986714d8e8SKurt Hackel 			continue;
26996714d8e8SKurt Hackel 		}
2700e2faea4cSKurt Hackel retry:
27016714d8e8SKurt Hackel 		mlog(0, "attempting to send begin reco msg to %d\n",
27026714d8e8SKurt Hackel 			  nodenum);
27036714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
27046714d8e8SKurt Hackel 					 &br, sizeof(br), nodenum, &status);
27056714d8e8SKurt Hackel 		/* negative status is handled ok by caller here */
27066714d8e8SKurt Hackel 		if (ret >= 0)
27076714d8e8SKurt Hackel 			ret = status;
2708e2faea4cSKurt Hackel 		if (dlm_is_host_down(ret)) {
2709e2faea4cSKurt Hackel 			/* node is down.  not involved in recovery
2710e2faea4cSKurt Hackel 			 * so just keep going */
2711a5196ec5SWengang Wang 			mlog(ML_NOTICE, "%s: node %u was down when sending "
2712e2faea4cSKurt Hackel 			     "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2713e2faea4cSKurt Hackel 			ret = 0;
2714e2faea4cSKurt Hackel 		}
2715cd34edd8SSunil Mushran 
2716cd34edd8SSunil Mushran 		/*
2717cd34edd8SSunil Mushran 		 * Prior to commit aad1b15310b9bcd59fa81ab8f2b1513b59553ea8,
2718cd34edd8SSunil Mushran 		 * dlm_begin_reco_handler() returned EAGAIN and not -EAGAIN.
2719cd34edd8SSunil Mushran 		 * We are handling both for compatibility reasons.
2720cd34edd8SSunil Mushran 		 */
2721cd34edd8SSunil Mushran 		if (ret == -EAGAIN || ret == EAGAIN) {
2722aad1b153STiger Yang 			mlog(0, "%s: trying to start recovery of node "
2723aad1b153STiger Yang 			     "%u, but node %u is waiting for last recovery "
2724aad1b153STiger Yang 			     "to complete, backoff for a bit\n", dlm->name,
2725aad1b153STiger Yang 			     dead_node, nodenum);
2726aad1b153STiger Yang 			msleep(100);
2727aad1b153STiger Yang 			goto retry;
2728aad1b153STiger Yang 		}
27296714d8e8SKurt Hackel 		if (ret < 0) {
27306714d8e8SKurt Hackel 			struct dlm_lock_resource *res;
2731a5196ec5SWengang Wang 
2732e2faea4cSKurt Hackel 			/* this is now a serious problem, possibly ENOMEM
2733e2faea4cSKurt Hackel 			 * in the network stack.  must retry */
27346714d8e8SKurt Hackel 			mlog_errno(ret);
27356714d8e8SKurt Hackel 			mlog(ML_ERROR, "begin reco of dlm %s to node %u "
27366714d8e8SKurt Hackel 			     "returned %d\n", dlm->name, nodenum, ret);
27376714d8e8SKurt Hackel 			res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
27386714d8e8SKurt Hackel 						 DLM_RECOVERY_LOCK_NAME_LEN);
27396714d8e8SKurt Hackel 			if (res) {
27406714d8e8SKurt Hackel 				dlm_print_one_lock_resource(res);
27416714d8e8SKurt Hackel 				dlm_lockres_put(res);
27426714d8e8SKurt Hackel 			} else {
27436714d8e8SKurt Hackel 				mlog(ML_ERROR, "recovery lock not found\n");
27446714d8e8SKurt Hackel 			}
2745e2faea4cSKurt Hackel 			/* sleep for a bit in hopes that we can avoid
2746e2faea4cSKurt Hackel 			 * another ENOMEM */
2747e2faea4cSKurt Hackel 			msleep(100);
2748e2faea4cSKurt Hackel 			goto retry;
27496714d8e8SKurt Hackel 		}
27506714d8e8SKurt Hackel 	}
27516714d8e8SKurt Hackel 
27526714d8e8SKurt Hackel 	return ret;
27536714d8e8SKurt Hackel }
27546714d8e8SKurt Hackel 
dlm_begin_reco_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)2755d74c9803SKurt Hackel int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2756d74c9803SKurt Hackel 			   void **ret_data)
27576714d8e8SKurt Hackel {
27586714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
27596714d8e8SKurt Hackel 	struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
27606714d8e8SKurt Hackel 
27616714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
27626714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
27636714d8e8SKurt Hackel 		return 0;
27646714d8e8SKurt Hackel 
2765466d1a45SKurt Hackel 	spin_lock(&dlm->spinlock);
2766466d1a45SKurt Hackel 	if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2767466d1a45SKurt Hackel 		mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2768466d1a45SKurt Hackel 		     "but this node is in finalize state, waiting on finalize2\n",
2769466d1a45SKurt Hackel 		     dlm->name, br->node_idx, br->dead_node,
2770466d1a45SKurt Hackel 		     dlm->reco.dead_node, dlm->reco.new_master);
2771466d1a45SKurt Hackel 		spin_unlock(&dlm->spinlock);
277240c7f2eaSXue jiufei 		dlm_put(dlm);
2773aad1b153STiger Yang 		return -EAGAIN;
2774466d1a45SKurt Hackel 	}
2775466d1a45SKurt Hackel 	spin_unlock(&dlm->spinlock);
2776466d1a45SKurt Hackel 
2777d6dea6e9SKurt Hackel 	mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2778d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2779d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
27806714d8e8SKurt Hackel 
27816714d8e8SKurt Hackel 	dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
27826714d8e8SKurt Hackel 
27836714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
27846714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2785e2faea4cSKurt Hackel 		if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2786e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u died, changing "
2787e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2788e2faea4cSKurt Hackel 			     br->node_idx);
2789e2faea4cSKurt Hackel 		} else {
2790e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u NOT DEAD, changing "
2791e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2792e2faea4cSKurt Hackel 			     br->node_idx);
2793e2faea4cSKurt Hackel 			/* may not have seen the new master as dead yet */
2794e2faea4cSKurt Hackel 		}
27956714d8e8SKurt Hackel 	}
27966714d8e8SKurt Hackel 	if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2797e2faea4cSKurt Hackel 		mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2798e2faea4cSKurt Hackel 		     "node %u changing it to %u\n", dlm->name,
2799e2faea4cSKurt Hackel 		     dlm->reco.dead_node, br->node_idx, br->dead_node);
28006714d8e8SKurt Hackel 	}
2801ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, br->node_idx);
2802ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, br->dead_node);
28036714d8e8SKurt Hackel 	if (!test_bit(br->dead_node, dlm->recovery_map)) {
2804e2faea4cSKurt Hackel 		mlog(0, "recovery master %u sees %u as dead, but this "
28056714d8e8SKurt Hackel 		     "node has not yet.  marking %u as dead\n",
28066714d8e8SKurt Hackel 		     br->node_idx, br->dead_node, br->dead_node);
2807e2faea4cSKurt Hackel 		if (!test_bit(br->dead_node, dlm->domain_map) ||
2808e2faea4cSKurt Hackel 		    !test_bit(br->dead_node, dlm->live_nodes_map))
2809e2faea4cSKurt Hackel 			mlog(0, "%u not in domain/live_nodes map "
2810e2faea4cSKurt Hackel 			     "so setting it in reco map manually\n",
2811e2faea4cSKurt Hackel 			     br->dead_node);
2812c03872f5SKurt Hackel 		/* force the recovery cleanup in __dlm_hb_node_down
2813c03872f5SKurt Hackel 		 * both of these will be cleared in a moment */
2814c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->domain_map);
2815c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->live_nodes_map);
28166714d8e8SKurt Hackel 		__dlm_hb_node_down(dlm, br->dead_node);
28176714d8e8SKurt Hackel 	}
28186714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
28196714d8e8SKurt Hackel 
28206714d8e8SKurt Hackel 	dlm_kick_recovery_thread(dlm);
2821d6dea6e9SKurt Hackel 
2822d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2823d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2824d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
2825d6dea6e9SKurt Hackel 
28266714d8e8SKurt Hackel 	dlm_put(dlm);
28276714d8e8SKurt Hackel 	return 0;
28286714d8e8SKurt Hackel }
28296714d8e8SKurt Hackel 
2830466d1a45SKurt Hackel #define DLM_FINALIZE_STAGE2  0x01
dlm_send_finalize_reco_message(struct dlm_ctxt * dlm)28316714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
28326714d8e8SKurt Hackel {
28336714d8e8SKurt Hackel 	int ret = 0;
28346714d8e8SKurt Hackel 	struct dlm_finalize_reco fr;
28356714d8e8SKurt Hackel 	struct dlm_node_iter iter;
28366714d8e8SKurt Hackel 	int nodenum;
28376714d8e8SKurt Hackel 	int status;
2838466d1a45SKurt Hackel 	int stage = 1;
28396714d8e8SKurt Hackel 
2840466d1a45SKurt Hackel 	mlog(0, "finishing recovery for node %s:%u, "
2841466d1a45SKurt Hackel 	     "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
28426714d8e8SKurt Hackel 
28436714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
28446714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
28456714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
28466714d8e8SKurt Hackel 
2847466d1a45SKurt Hackel stage2:
28486714d8e8SKurt Hackel 	memset(&fr, 0, sizeof(fr));
28496714d8e8SKurt Hackel 	fr.node_idx = dlm->node_num;
28506714d8e8SKurt Hackel 	fr.dead_node = dlm->reco.dead_node;
2851466d1a45SKurt Hackel 	if (stage == 2)
2852466d1a45SKurt Hackel 		fr.flags |= DLM_FINALIZE_STAGE2;
28536714d8e8SKurt Hackel 
28546714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
28556714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
28566714d8e8SKurt Hackel 			continue;
28576714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
28586714d8e8SKurt Hackel 					 &fr, sizeof(fr), nodenum, &status);
2859466d1a45SKurt Hackel 		if (ret >= 0)
28606714d8e8SKurt Hackel 			ret = status;
2861466d1a45SKurt Hackel 		if (ret < 0) {
2862a5196ec5SWengang Wang 			mlog(ML_ERROR, "Error %d when sending message %u (key "
2863a5196ec5SWengang Wang 			     "0x%x) to node %u\n", ret, DLM_FINALIZE_RECO_MSG,
2864a5196ec5SWengang Wang 			     dlm->key, nodenum);
28656714d8e8SKurt Hackel 			if (dlm_is_host_down(ret)) {
28666714d8e8SKurt Hackel 				/* this has no effect on this recovery
28676714d8e8SKurt Hackel 				 * session, so set the status to zero to
28686714d8e8SKurt Hackel 				 * finish out the last recovery */
28696714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u went down after this "
28706714d8e8SKurt Hackel 				     "node finished recovery.\n", nodenum);
28716714d8e8SKurt Hackel 				ret = 0;
2872c27069e6SKurt Hackel 				continue;
28736714d8e8SKurt Hackel 			}
28746714d8e8SKurt Hackel 			break;
28756714d8e8SKurt Hackel 		}
28766714d8e8SKurt Hackel 	}
2877466d1a45SKurt Hackel 	if (stage == 1) {
2878466d1a45SKurt Hackel 		/* reset the node_iter back to the top and send finalize2 */
2879466d1a45SKurt Hackel 		iter.curnode = -1;
2880466d1a45SKurt Hackel 		stage = 2;
2881466d1a45SKurt Hackel 		goto stage2;
2882466d1a45SKurt Hackel 	}
28836714d8e8SKurt Hackel 
28846714d8e8SKurt Hackel 	return ret;
28856714d8e8SKurt Hackel }
28866714d8e8SKurt Hackel 
dlm_finalize_reco_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)2887d74c9803SKurt Hackel int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2888d74c9803SKurt Hackel 			      void **ret_data)
28896714d8e8SKurt Hackel {
28906714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
28916714d8e8SKurt Hackel 	struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2892466d1a45SKurt Hackel 	int stage = 1;
28936714d8e8SKurt Hackel 
28946714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
28956714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
28966714d8e8SKurt Hackel 		return 0;
28976714d8e8SKurt Hackel 
2898466d1a45SKurt Hackel 	if (fr->flags & DLM_FINALIZE_STAGE2)
2899466d1a45SKurt Hackel 		stage = 2;
2900466d1a45SKurt Hackel 
2901466d1a45SKurt Hackel 	mlog(0, "%s: node %u finalizing recovery stage%d of "
2902466d1a45SKurt Hackel 	     "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2903466d1a45SKurt Hackel 	     fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
29046714d8e8SKurt Hackel 
29056714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
29066714d8e8SKurt Hackel 
29076714d8e8SKurt Hackel 	if (dlm->reco.new_master != fr->node_idx) {
29086714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
29096714d8e8SKurt Hackel 		     "%u is supposed to be the new master, dead=%u\n",
29106714d8e8SKurt Hackel 		     fr->node_idx, dlm->reco.new_master, fr->dead_node);
29116714d8e8SKurt Hackel 		BUG();
29126714d8e8SKurt Hackel 	}
29136714d8e8SKurt Hackel 	if (dlm->reco.dead_node != fr->dead_node) {
29146714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
29156714d8e8SKurt Hackel 		     "node %u, but node %u is supposed to be dead\n",
29166714d8e8SKurt Hackel 		     fr->node_idx, fr->dead_node, dlm->reco.dead_node);
29176714d8e8SKurt Hackel 		BUG();
29186714d8e8SKurt Hackel 	}
29196714d8e8SKurt Hackel 
2920466d1a45SKurt Hackel 	switch (stage) {
2921466d1a45SKurt Hackel 		case 1:
29226714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2923466d1a45SKurt Hackel 			if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2924466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize1 from "
2925466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2926466d1a45SKurt Hackel 				     "this node has already received it!\n",
2927466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2928466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2929466d1a45SKurt Hackel 				BUG();
2930466d1a45SKurt Hackel 			}
2931466d1a45SKurt Hackel 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
29326714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
2933466d1a45SKurt Hackel 			break;
2934466d1a45SKurt Hackel 		case 2:
2935466d1a45SKurt Hackel 			if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2936466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize2 from "
2937466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2938466d1a45SKurt Hackel 				     "this node did not have finalize1!\n",
2939466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2940466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2941466d1a45SKurt Hackel 				BUG();
2942466d1a45SKurt Hackel 			}
2943466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2944ded2cf71SJunxiao Bi 			__dlm_reset_recovery(dlm);
2945466d1a45SKurt Hackel 			spin_unlock(&dlm->spinlock);
29466714d8e8SKurt Hackel 			dlm_kick_recovery_thread(dlm);
2947466d1a45SKurt Hackel 			break;
2948466d1a45SKurt Hackel 	}
2949466d1a45SKurt Hackel 
2950d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2951d6dea6e9SKurt Hackel 	     dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2952d6dea6e9SKurt Hackel 
29536714d8e8SKurt Hackel 	dlm_put(dlm);
29546714d8e8SKurt Hackel 	return 0;
29556714d8e8SKurt Hackel }
2956