xref: /openbmc/linux/fs/ocfs2/dlm/dlmrecovery.c (revision f471c9df922a80ca9af1d9a490b4aab3f990ec19)
16714d8e8SKurt Hackel /* -*- mode: c; c-basic-offset: 8; -*-
26714d8e8SKurt Hackel  * vim: noexpandtab sw=8 ts=8 sts=0:
36714d8e8SKurt Hackel  *
46714d8e8SKurt Hackel  * dlmrecovery.c
56714d8e8SKurt Hackel  *
66714d8e8SKurt Hackel  * recovery stuff
76714d8e8SKurt Hackel  *
86714d8e8SKurt Hackel  * Copyright (C) 2004 Oracle.  All rights reserved.
96714d8e8SKurt Hackel  *
106714d8e8SKurt Hackel  * This program is free software; you can redistribute it and/or
116714d8e8SKurt Hackel  * modify it under the terms of the GNU General Public
126714d8e8SKurt Hackel  * License as published by the Free Software Foundation; either
136714d8e8SKurt Hackel  * version 2 of the License, or (at your option) any later version.
146714d8e8SKurt Hackel  *
156714d8e8SKurt Hackel  * This program is distributed in the hope that it will be useful,
166714d8e8SKurt Hackel  * but WITHOUT ANY WARRANTY; without even the implied warranty of
176714d8e8SKurt Hackel  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
186714d8e8SKurt Hackel  * General Public License for more details.
196714d8e8SKurt Hackel  *
206714d8e8SKurt Hackel  * You should have received a copy of the GNU General Public
216714d8e8SKurt Hackel  * License along with this program; if not, write to the
226714d8e8SKurt Hackel  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
236714d8e8SKurt Hackel  * Boston, MA 021110-1307, USA.
246714d8e8SKurt Hackel  *
256714d8e8SKurt Hackel  */
266714d8e8SKurt Hackel 
276714d8e8SKurt Hackel 
286714d8e8SKurt Hackel #include <linux/module.h>
296714d8e8SKurt Hackel #include <linux/fs.h>
306714d8e8SKurt Hackel #include <linux/types.h>
316714d8e8SKurt Hackel #include <linux/slab.h>
326714d8e8SKurt Hackel #include <linux/highmem.h>
336714d8e8SKurt Hackel #include <linux/init.h>
346714d8e8SKurt Hackel #include <linux/sysctl.h>
356714d8e8SKurt Hackel #include <linux/random.h>
366714d8e8SKurt Hackel #include <linux/blkdev.h>
376714d8e8SKurt Hackel #include <linux/socket.h>
386714d8e8SKurt Hackel #include <linux/inet.h>
396714d8e8SKurt Hackel #include <linux/timer.h>
406714d8e8SKurt Hackel #include <linux/kthread.h>
41b4c7f538SAdrian Bunk #include <linux/delay.h>
426714d8e8SKurt Hackel 
436714d8e8SKurt Hackel 
446714d8e8SKurt Hackel #include "cluster/heartbeat.h"
456714d8e8SKurt Hackel #include "cluster/nodemanager.h"
466714d8e8SKurt Hackel #include "cluster/tcp.h"
476714d8e8SKurt Hackel 
486714d8e8SKurt Hackel #include "dlmapi.h"
496714d8e8SKurt Hackel #include "dlmcommon.h"
506714d8e8SKurt Hackel #include "dlmdomain.h"
516714d8e8SKurt Hackel 
526714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
536714d8e8SKurt Hackel #include "cluster/masklog.h"
546714d8e8SKurt Hackel 
556714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
566714d8e8SKurt Hackel 
576714d8e8SKurt Hackel static int dlm_recovery_thread(void *data);
586714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
596714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
60c03872f5SKurt Hackel void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
616714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm);
626714d8e8SKurt Hackel 
636714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
646714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
656714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
666714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm,
676714d8e8SKurt Hackel 				 u8 request_from, u8 dead_node);
686714d8e8SKurt Hackel static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
696714d8e8SKurt Hackel 
706714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
716714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
726714d8e8SKurt Hackel 					const char *lockname, int namelen,
736714d8e8SKurt Hackel 					int total_locks, u64 cookie,
746714d8e8SKurt Hackel 					u8 flags, u8 master);
756714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
766714d8e8SKurt Hackel 				    struct dlm_migratable_lockres *mres,
776714d8e8SKurt Hackel 				    u8 send_to,
786714d8e8SKurt Hackel 				    struct dlm_lock_resource *res,
796714d8e8SKurt Hackel 				    int total_locks);
806714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
816714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
826714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres);
836714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
846714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
856714d8e8SKurt Hackel 				 u8 dead_node, u8 send_to);
866714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
876714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
886714d8e8SKurt Hackel 					struct list_head *list, u8 dead_node);
896714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
906714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master);
916714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata);
926714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type);
936714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
946714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item,
956714d8e8SKurt Hackel 					 void *data);
966714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
978169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
988169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
998169cae5SAdrian Bunk 				      u8 *real_master);
1006714d8e8SKurt Hackel 
1016714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void);
1026714d8e8SKurt Hackel 
10334af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_reco_state_lock);
10434af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
1056714d8e8SKurt Hackel static u64 dlm_mig_cookie = 1;
1066714d8e8SKurt Hackel 
1076714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void)
1086714d8e8SKurt Hackel {
1096714d8e8SKurt Hackel 	u64 c;
1106714d8e8SKurt Hackel 	spin_lock(&dlm_mig_cookie_lock);
1116714d8e8SKurt Hackel 	c = dlm_mig_cookie;
1126714d8e8SKurt Hackel 	if (dlm_mig_cookie == (~0ULL))
1136714d8e8SKurt Hackel 		dlm_mig_cookie = 1;
1146714d8e8SKurt Hackel 	else
1156714d8e8SKurt Hackel 		dlm_mig_cookie++;
1166714d8e8SKurt Hackel 	spin_unlock(&dlm_mig_cookie_lock);
1176714d8e8SKurt Hackel 	return c;
1186714d8e8SKurt Hackel }
1196714d8e8SKurt Hackel 
120ab27eb6fSKurt Hackel static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
121ab27eb6fSKurt Hackel 					  u8 dead_node)
122ab27eb6fSKurt Hackel {
123ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
124ab27eb6fSKurt Hackel 	if (dlm->reco.dead_node != dead_node)
125ab27eb6fSKurt Hackel 		mlog(0, "%s: changing dead_node from %u to %u\n",
126ab27eb6fSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dead_node);
127ab27eb6fSKurt Hackel 	dlm->reco.dead_node = dead_node;
128ab27eb6fSKurt Hackel }
129ab27eb6fSKurt Hackel 
130ab27eb6fSKurt Hackel static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
131ab27eb6fSKurt Hackel 				       u8 master)
132ab27eb6fSKurt Hackel {
133ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
134ab27eb6fSKurt Hackel 	mlog(0, "%s: changing new_master from %u to %u\n",
135ab27eb6fSKurt Hackel 	     dlm->name, dlm->reco.new_master, master);
136ab27eb6fSKurt Hackel 	dlm->reco.new_master = master;
137ab27eb6fSKurt Hackel }
138ab27eb6fSKurt Hackel 
139466d1a45SKurt Hackel static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
1406714d8e8SKurt Hackel {
141466d1a45SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1426714d8e8SKurt Hackel 	clear_bit(dlm->reco.dead_node, dlm->recovery_map);
143ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
144ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
145466d1a45SKurt Hackel }
146466d1a45SKurt Hackel 
147466d1a45SKurt Hackel static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
148466d1a45SKurt Hackel {
149466d1a45SKurt Hackel 	spin_lock(&dlm->spinlock);
150466d1a45SKurt Hackel 	__dlm_reset_recovery(dlm);
1516714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1526714d8e8SKurt Hackel }
1536714d8e8SKurt Hackel 
1546714d8e8SKurt Hackel /* Worker function used during recovery. */
155c4028958SDavid Howells void dlm_dispatch_work(struct work_struct *work)
1566714d8e8SKurt Hackel {
157c4028958SDavid Howells 	struct dlm_ctxt *dlm =
158c4028958SDavid Howells 		container_of(work, struct dlm_ctxt, dispatched_work);
1596714d8e8SKurt Hackel 	LIST_HEAD(tmp_list);
160800deef3SChristoph Hellwig 	struct dlm_work_item *item, *next;
1616714d8e8SKurt Hackel 	dlm_workfunc_t *workfunc;
1623156d267SKurt Hackel 	int tot=0;
1633156d267SKurt Hackel 
1646714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
1656714d8e8SKurt Hackel 	list_splice_init(&dlm->work_list, &tmp_list);
1666714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
1676714d8e8SKurt Hackel 
168800deef3SChristoph Hellwig 	list_for_each_entry(item, &tmp_list, list) {
1693156d267SKurt Hackel 		tot++;
1703156d267SKurt Hackel 	}
1713156d267SKurt Hackel 	mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
1723156d267SKurt Hackel 
173800deef3SChristoph Hellwig 	list_for_each_entry_safe(item, next, &tmp_list, list) {
1746714d8e8SKurt Hackel 		workfunc = item->func;
1756714d8e8SKurt Hackel 		list_del_init(&item->list);
1766714d8e8SKurt Hackel 
1776714d8e8SKurt Hackel 		/* already have ref on dlm to avoid having
1786714d8e8SKurt Hackel 		 * it disappear.  just double-check. */
1796714d8e8SKurt Hackel 		BUG_ON(item->dlm != dlm);
1806714d8e8SKurt Hackel 
1816714d8e8SKurt Hackel 		/* this is allowed to sleep and
1826714d8e8SKurt Hackel 		 * call network stuff */
1836714d8e8SKurt Hackel 		workfunc(item, item->data);
1846714d8e8SKurt Hackel 
1856714d8e8SKurt Hackel 		dlm_put(dlm);
1866714d8e8SKurt Hackel 		kfree(item);
1876714d8e8SKurt Hackel 	}
1886714d8e8SKurt Hackel }
1896714d8e8SKurt Hackel 
1906714d8e8SKurt Hackel /*
1916714d8e8SKurt Hackel  * RECOVERY THREAD
1926714d8e8SKurt Hackel  */
1936714d8e8SKurt Hackel 
194c03872f5SKurt Hackel void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
1956714d8e8SKurt Hackel {
1966714d8e8SKurt Hackel 	/* wake the recovery thread
1976714d8e8SKurt Hackel 	 * this will wake the reco thread in one of three places
1986714d8e8SKurt Hackel 	 * 1) sleeping with no recovery happening
1996714d8e8SKurt Hackel 	 * 2) sleeping with recovery mastered elsewhere
2006714d8e8SKurt Hackel 	 * 3) recovery mastered here, waiting on reco data */
2016714d8e8SKurt Hackel 
2026714d8e8SKurt Hackel 	wake_up(&dlm->dlm_reco_thread_wq);
2036714d8e8SKurt Hackel }
2046714d8e8SKurt Hackel 
2056714d8e8SKurt Hackel /* Launch the recovery thread */
2066714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
2076714d8e8SKurt Hackel {
2086714d8e8SKurt Hackel 	mlog(0, "starting dlm recovery thread...\n");
2096714d8e8SKurt Hackel 
2106714d8e8SKurt Hackel 	dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
2116714d8e8SKurt Hackel 						"dlm_reco_thread");
2126714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_reco_thread_task)) {
2136714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
2146714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2156714d8e8SKurt Hackel 		return -EINVAL;
2166714d8e8SKurt Hackel 	}
2176714d8e8SKurt Hackel 
2186714d8e8SKurt Hackel 	return 0;
2196714d8e8SKurt Hackel }
2206714d8e8SKurt Hackel 
2216714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
2226714d8e8SKurt Hackel {
2236714d8e8SKurt Hackel 	if (dlm->dlm_reco_thread_task) {
2246714d8e8SKurt Hackel 		mlog(0, "waiting for dlm recovery thread to exit\n");
2256714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_reco_thread_task);
2266714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2276714d8e8SKurt Hackel 	}
2286714d8e8SKurt Hackel }
2296714d8e8SKurt Hackel 
2306714d8e8SKurt Hackel 
2316714d8e8SKurt Hackel 
2326714d8e8SKurt Hackel /*
2336714d8e8SKurt Hackel  * this is lame, but here's how recovery works...
2346714d8e8SKurt Hackel  * 1) all recovery threads cluster wide will work on recovering
2356714d8e8SKurt Hackel  *    ONE node at a time
2366714d8e8SKurt Hackel  * 2) negotiate who will take over all the locks for the dead node.
2376714d8e8SKurt Hackel  *    thats right... ALL the locks.
2386714d8e8SKurt Hackel  * 3) once a new master is chosen, everyone scans all locks
2396714d8e8SKurt Hackel  *    and moves aside those mastered by the dead guy
2406714d8e8SKurt Hackel  * 4) each of these locks should be locked until recovery is done
2416714d8e8SKurt Hackel  * 5) the new master collects up all of secondary lock queue info
2426714d8e8SKurt Hackel  *    one lock at a time, forcing each node to communicate back
2436714d8e8SKurt Hackel  *    before continuing
2446714d8e8SKurt Hackel  * 6) each secondary lock queue responds with the full known lock info
2456714d8e8SKurt Hackel  * 7) once the new master has run all its locks, it sends a ALLDONE!
2466714d8e8SKurt Hackel  *    message to everyone
2476714d8e8SKurt Hackel  * 8) upon receiving this message, the secondary queue node unlocks
2486714d8e8SKurt Hackel  *    and responds to the ALLDONE
2496714d8e8SKurt Hackel  * 9) once the new master gets responses from everyone, he unlocks
2506714d8e8SKurt Hackel  *    everything and recovery for this dead node is done
2516714d8e8SKurt Hackel  *10) go back to 2) while there are still dead nodes
2526714d8e8SKurt Hackel  *
2536714d8e8SKurt Hackel  */
2546714d8e8SKurt Hackel 
255d6dea6e9SKurt Hackel static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
256d6dea6e9SKurt Hackel {
257d6dea6e9SKurt Hackel 	struct dlm_reco_node_data *ndata;
258d6dea6e9SKurt Hackel 	struct dlm_lock_resource *res;
259d6dea6e9SKurt Hackel 
260d6dea6e9SKurt Hackel 	mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
261ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
262d6dea6e9SKurt Hackel 	     dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
263d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
264d6dea6e9SKurt Hackel 
265d6dea6e9SKurt Hackel 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
266d6dea6e9SKurt Hackel 		char *st = "unknown";
267d6dea6e9SKurt Hackel 		switch (ndata->state) {
268d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
269d6dea6e9SKurt Hackel 				st = "init";
270d6dea6e9SKurt Hackel 				break;
271d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
272d6dea6e9SKurt Hackel 				st = "requesting";
273d6dea6e9SKurt Hackel 				break;
274d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
275d6dea6e9SKurt Hackel 				st = "dead";
276d6dea6e9SKurt Hackel 				break;
277d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
278d6dea6e9SKurt Hackel 				st = "receiving";
279d6dea6e9SKurt Hackel 				break;
280d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
281d6dea6e9SKurt Hackel 				st = "requested";
282d6dea6e9SKurt Hackel 				break;
283d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
284d6dea6e9SKurt Hackel 				st = "done";
285d6dea6e9SKurt Hackel 				break;
286d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
287d6dea6e9SKurt Hackel 				st = "finalize-sent";
288d6dea6e9SKurt Hackel 				break;
289d6dea6e9SKurt Hackel 			default:
290d6dea6e9SKurt Hackel 				st = "bad";
291d6dea6e9SKurt Hackel 				break;
292d6dea6e9SKurt Hackel 		}
293d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
294d6dea6e9SKurt Hackel 		     dlm->name, ndata->node_num, st);
295d6dea6e9SKurt Hackel 	}
296d6dea6e9SKurt Hackel 	list_for_each_entry(res, &dlm->reco.resources, recovering) {
297d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
298d6dea6e9SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
299d6dea6e9SKurt Hackel 	}
300d6dea6e9SKurt Hackel }
3016714d8e8SKurt Hackel 
3026714d8e8SKurt Hackel #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
3036714d8e8SKurt Hackel 
3046714d8e8SKurt Hackel static int dlm_recovery_thread(void *data)
3056714d8e8SKurt Hackel {
3066714d8e8SKurt Hackel 	int status;
3076714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
3086714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
3096714d8e8SKurt Hackel 
3106714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
3116714d8e8SKurt Hackel 
3126714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
313bc9838c4SSrinivas Eeda 		if (dlm_domain_fully_joined(dlm)) {
3146714d8e8SKurt Hackel 			status = dlm_do_recovery(dlm);
3156714d8e8SKurt Hackel 			if (status == -EAGAIN) {
3166714d8e8SKurt Hackel 				/* do not sleep, recheck immediately. */
3176714d8e8SKurt Hackel 				continue;
3186714d8e8SKurt Hackel 			}
3196714d8e8SKurt Hackel 			if (status < 0)
3206714d8e8SKurt Hackel 				mlog_errno(status);
3216714d8e8SKurt Hackel 		}
3226714d8e8SKurt Hackel 
3236714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
3246714d8e8SKurt Hackel 						 kthread_should_stop(),
3256714d8e8SKurt Hackel 						 timeout);
3266714d8e8SKurt Hackel 	}
3276714d8e8SKurt Hackel 
3286714d8e8SKurt Hackel 	mlog(0, "quitting DLM recovery thread\n");
3296714d8e8SKurt Hackel 	return 0;
3306714d8e8SKurt Hackel }
3316714d8e8SKurt Hackel 
332e2faea4cSKurt Hackel /* returns true when the recovery master has contacted us */
333e2faea4cSKurt Hackel static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
334e2faea4cSKurt Hackel {
335e2faea4cSKurt Hackel 	int ready;
336e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
337e2faea4cSKurt Hackel 	ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
338e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
339e2faea4cSKurt Hackel 	return ready;
340e2faea4cSKurt Hackel }
341e2faea4cSKurt Hackel 
342e2faea4cSKurt Hackel /* returns true if node is no longer in the domain
343e2faea4cSKurt Hackel  * could be dead or just not joined */
344e2faea4cSKurt Hackel int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
345e2faea4cSKurt Hackel {
346e2faea4cSKurt Hackel 	int dead;
347e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
348aba9aac7SKurt Hackel 	dead = !test_bit(node, dlm->domain_map);
349e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
350e2faea4cSKurt Hackel 	return dead;
351e2faea4cSKurt Hackel }
352e2faea4cSKurt Hackel 
353b7084ab5SKurt Hackel /* returns true if node is no longer in the domain
354b7084ab5SKurt Hackel  * could be dead or just not joined */
3553fb5a989SAdrian Bunk static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
356b7084ab5SKurt Hackel {
357b7084ab5SKurt Hackel 	int recovered;
358b7084ab5SKurt Hackel 	spin_lock(&dlm->spinlock);
359b7084ab5SKurt Hackel 	recovered = !test_bit(node, dlm->recovery_map);
360b7084ab5SKurt Hackel 	spin_unlock(&dlm->spinlock);
361b7084ab5SKurt Hackel 	return recovered;
362b7084ab5SKurt Hackel }
363b7084ab5SKurt Hackel 
364b7084ab5SKurt Hackel 
36544465a7dSKurt Hackel int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
36644465a7dSKurt Hackel {
36744465a7dSKurt Hackel 	if (timeout) {
36844465a7dSKurt Hackel 		mlog(ML_NOTICE, "%s: waiting %dms for notification of "
36944465a7dSKurt Hackel 		     "death of node %u\n", dlm->name, timeout, node);
37044465a7dSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
37144465a7dSKurt Hackel 			   dlm_is_node_dead(dlm, node),
37244465a7dSKurt Hackel 			   msecs_to_jiffies(timeout));
37344465a7dSKurt Hackel 	} else {
37444465a7dSKurt Hackel 		mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
37544465a7dSKurt Hackel 		     "of death of node %u\n", dlm->name, node);
37644465a7dSKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
37744465a7dSKurt Hackel 			   dlm_is_node_dead(dlm, node));
37844465a7dSKurt Hackel 	}
37944465a7dSKurt Hackel 	/* for now, return 0 */
38044465a7dSKurt Hackel 	return 0;
38144465a7dSKurt Hackel }
38244465a7dSKurt Hackel 
383b7084ab5SKurt Hackel int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
384b7084ab5SKurt Hackel {
385b7084ab5SKurt Hackel 	if (timeout) {
386b7084ab5SKurt Hackel 		mlog(0, "%s: waiting %dms for notification of "
387b7084ab5SKurt Hackel 		     "recovery of node %u\n", dlm->name, timeout, node);
388b7084ab5SKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
389b7084ab5SKurt Hackel 			   dlm_is_node_recovered(dlm, node),
390b7084ab5SKurt Hackel 			   msecs_to_jiffies(timeout));
391b7084ab5SKurt Hackel 	} else {
392b7084ab5SKurt Hackel 		mlog(0, "%s: waiting indefinitely for notification "
393b7084ab5SKurt Hackel 		     "of recovery of node %u\n", dlm->name, node);
394b7084ab5SKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
395b7084ab5SKurt Hackel 			   dlm_is_node_recovered(dlm, node));
396b7084ab5SKurt Hackel 	}
397b7084ab5SKurt Hackel 	/* for now, return 0 */
398b7084ab5SKurt Hackel 	return 0;
399b7084ab5SKurt Hackel }
400b7084ab5SKurt Hackel 
4016714d8e8SKurt Hackel /* callers of the top-level api calls (dlmlock/dlmunlock) should
4026714d8e8SKurt Hackel  * block on the dlm->reco.event when recovery is in progress.
4036714d8e8SKurt Hackel  * the dlm recovery thread will set this state when it begins
4046714d8e8SKurt Hackel  * recovering a dead node (as the new master or not) and clear
4056714d8e8SKurt Hackel  * the state and wake as soon as all affected lock resources have
4066714d8e8SKurt Hackel  * been marked with the RECOVERY flag */
4076714d8e8SKurt Hackel static int dlm_in_recovery(struct dlm_ctxt *dlm)
4086714d8e8SKurt Hackel {
4096714d8e8SKurt Hackel 	int in_recovery;
4106714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4116714d8e8SKurt Hackel 	in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
4126714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4136714d8e8SKurt Hackel 	return in_recovery;
4146714d8e8SKurt Hackel }
4156714d8e8SKurt Hackel 
4166714d8e8SKurt Hackel 
4176714d8e8SKurt Hackel void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
4186714d8e8SKurt Hackel {
41956a7c104SKurt Hackel 	if (dlm_in_recovery(dlm)) {
4203b3b84a8SKurt Hackel 		mlog(0, "%s: reco thread %d in recovery: "
42156a7c104SKurt Hackel 		     "state=%d, master=%u, dead=%u\n",
422ba25f9dcSPavel Emelyanov 		     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
42356a7c104SKurt Hackel 		     dlm->reco.state, dlm->reco.new_master,
42456a7c104SKurt Hackel 		     dlm->reco.dead_node);
42556a7c104SKurt Hackel 	}
4266714d8e8SKurt Hackel 	wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
4276714d8e8SKurt Hackel }
4286714d8e8SKurt Hackel 
4296714d8e8SKurt Hackel static void dlm_begin_recovery(struct dlm_ctxt *dlm)
4306714d8e8SKurt Hackel {
4316714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4326714d8e8SKurt Hackel 	BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
4336714d8e8SKurt Hackel 	dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
4346714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4356714d8e8SKurt Hackel }
4366714d8e8SKurt Hackel 
4376714d8e8SKurt Hackel static void dlm_end_recovery(struct dlm_ctxt *dlm)
4386714d8e8SKurt Hackel {
4396714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4406714d8e8SKurt Hackel 	BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
4416714d8e8SKurt Hackel 	dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
4426714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4436714d8e8SKurt Hackel 	wake_up(&dlm->reco.event);
4446714d8e8SKurt Hackel }
4456714d8e8SKurt Hackel 
4466714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm)
4476714d8e8SKurt Hackel {
4486714d8e8SKurt Hackel 	int status = 0;
449e2faea4cSKurt Hackel 	int ret;
4506714d8e8SKurt Hackel 
4516714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4526714d8e8SKurt Hackel 
4536714d8e8SKurt Hackel 	/* check to see if the new master has died */
4546714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
4556714d8e8SKurt Hackel 	    test_bit(dlm->reco.new_master, dlm->recovery_map)) {
4566714d8e8SKurt Hackel 		mlog(0, "new master %u died while recovering %u!\n",
4576714d8e8SKurt Hackel 		     dlm->reco.new_master, dlm->reco.dead_node);
4586714d8e8SKurt Hackel 		/* unset the new_master, leave dead_node */
459ab27eb6fSKurt Hackel 		dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
4606714d8e8SKurt Hackel 	}
4616714d8e8SKurt Hackel 
4626714d8e8SKurt Hackel 	/* select a target to recover */
4636714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4646714d8e8SKurt Hackel 		int bit;
4656714d8e8SKurt Hackel 
466*f471c9dfSWengang Wang 		bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES, 0);
4676714d8e8SKurt Hackel 		if (bit >= O2NM_MAX_NODES || bit < 0)
468ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4696714d8e8SKurt Hackel 		else
470ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, bit);
4716714d8e8SKurt Hackel 	} else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
4726714d8e8SKurt Hackel 		/* BUG? */
4736714d8e8SKurt Hackel 		mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
4746714d8e8SKurt Hackel 		     dlm->reco.dead_node);
475ab27eb6fSKurt Hackel 		dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4766714d8e8SKurt Hackel 	}
4776714d8e8SKurt Hackel 
4786714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4796714d8e8SKurt Hackel 		// mlog(0, "nothing to recover!  sleeping now!\n");
4806714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
4816714d8e8SKurt Hackel 		/* return to main thread loop and sleep. */
4826714d8e8SKurt Hackel 		return 0;
4836714d8e8SKurt Hackel 	}
484d6dea6e9SKurt Hackel 	mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
485ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
4866714d8e8SKurt Hackel 	     dlm->reco.dead_node);
4876714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4886714d8e8SKurt Hackel 
4896714d8e8SKurt Hackel 	/* take write barrier */
4906714d8e8SKurt Hackel 	/* (stops the list reshuffling thread, proxy ast handling) */
4916714d8e8SKurt Hackel 	dlm_begin_recovery(dlm);
4926714d8e8SKurt Hackel 
4936714d8e8SKurt Hackel 	if (dlm->reco.new_master == dlm->node_num)
4946714d8e8SKurt Hackel 		goto master_here;
4956714d8e8SKurt Hackel 
4966714d8e8SKurt Hackel 	if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
497e2faea4cSKurt Hackel 		/* choose a new master, returns 0 if this node
498e2faea4cSKurt Hackel 		 * is the master, -EEXIST if it's another node.
499e2faea4cSKurt Hackel 		 * this does not return until a new master is chosen
500e2faea4cSKurt Hackel 		 * or recovery completes entirely. */
501e2faea4cSKurt Hackel 		ret = dlm_pick_recovery_master(dlm);
502e2faea4cSKurt Hackel 		if (!ret) {
5036714d8e8SKurt Hackel 			/* already notified everyone.  go. */
5046714d8e8SKurt Hackel 			goto master_here;
5056714d8e8SKurt Hackel 		}
5066714d8e8SKurt Hackel 		mlog(0, "another node will master this recovery session.\n");
5076714d8e8SKurt Hackel 	}
508d6dea6e9SKurt Hackel 	mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
509ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task), dlm->reco.new_master,
5106714d8e8SKurt Hackel 	     dlm->node_num, dlm->reco.dead_node);
5116714d8e8SKurt Hackel 
5126714d8e8SKurt Hackel 	/* it is safe to start everything back up here
5136714d8e8SKurt Hackel 	 * because all of the dead node's lock resources
5146714d8e8SKurt Hackel 	 * have been marked as in-recovery */
5156714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5166714d8e8SKurt Hackel 
5176714d8e8SKurt Hackel 	/* sleep out in main dlm_recovery_thread loop. */
5186714d8e8SKurt Hackel 	return 0;
5196714d8e8SKurt Hackel 
5206714d8e8SKurt Hackel master_here:
521535f7026SSunil Mushran 	mlog(ML_NOTICE, "(%d) Node %u is the Recovery Master for the Dead Node "
522535f7026SSunil Mushran 	     "%u for Domain %s\n", task_pid_nr(dlm->dlm_reco_thread_task),
523535f7026SSunil Mushran 	     dlm->node_num, dlm->reco.dead_node, dlm->name);
5246714d8e8SKurt Hackel 
5256714d8e8SKurt Hackel 	status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
5266714d8e8SKurt Hackel 	if (status < 0) {
5276a413211SKurt Hackel 		/* we should never hit this anymore */
5286714d8e8SKurt Hackel 		mlog(ML_ERROR, "error %d remastering locks for node %u, "
5296714d8e8SKurt Hackel 		     "retrying.\n", status, dlm->reco.dead_node);
530e2faea4cSKurt Hackel 		/* yield a bit to allow any final network messages
531e2faea4cSKurt Hackel 		 * to get handled on remaining nodes */
532e2faea4cSKurt Hackel 		msleep(100);
5336714d8e8SKurt Hackel 	} else {
5346714d8e8SKurt Hackel 		/* success!  see if any other nodes need recovery */
535e2faea4cSKurt Hackel 		mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
536e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dlm->node_num);
5376714d8e8SKurt Hackel 		dlm_reset_recovery(dlm);
5386714d8e8SKurt Hackel 	}
5396714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5406714d8e8SKurt Hackel 
5416714d8e8SKurt Hackel 	/* continue and look for another dead node */
5426714d8e8SKurt Hackel 	return -EAGAIN;
5436714d8e8SKurt Hackel }
5446714d8e8SKurt Hackel 
5456714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
5466714d8e8SKurt Hackel {
5476714d8e8SKurt Hackel 	int status = 0;
5486714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
5496714d8e8SKurt Hackel 	int all_nodes_done;
5506714d8e8SKurt Hackel 	int destroy = 0;
5516714d8e8SKurt Hackel 	int pass = 0;
5526714d8e8SKurt Hackel 
5536a413211SKurt Hackel 	do {
5546a413211SKurt Hackel 		/* we have become recovery master.  there is no escaping
5556a413211SKurt Hackel 		 * this, so just keep trying until we get it. */
5566714d8e8SKurt Hackel 		status = dlm_init_recovery_area(dlm, dead_node);
5576a413211SKurt Hackel 		if (status < 0) {
5586a413211SKurt Hackel 			mlog(ML_ERROR, "%s: failed to alloc recovery area, "
5596a413211SKurt Hackel 			     "retrying\n", dlm->name);
5606a413211SKurt Hackel 			msleep(1000);
5616a413211SKurt Hackel 		}
5626a413211SKurt Hackel 	} while (status != 0);
5636714d8e8SKurt Hackel 
5646714d8e8SKurt Hackel 	/* safe to access the node data list without a lock, since this
5656714d8e8SKurt Hackel 	 * process is the only one to change the list */
566800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
5676714d8e8SKurt Hackel 		BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
5686714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
5696714d8e8SKurt Hackel 
5706714d8e8SKurt Hackel 		mlog(0, "requesting lock info from node %u\n",
5716714d8e8SKurt Hackel 		     ndata->node_num);
5726714d8e8SKurt Hackel 
5736714d8e8SKurt Hackel 		if (ndata->node_num == dlm->node_num) {
5746714d8e8SKurt Hackel 			ndata->state = DLM_RECO_NODE_DATA_DONE;
5756714d8e8SKurt Hackel 			continue;
5766714d8e8SKurt Hackel 		}
5776714d8e8SKurt Hackel 
5786a413211SKurt Hackel 		do {
5796a413211SKurt Hackel 			status = dlm_request_all_locks(dlm, ndata->node_num,
5806a413211SKurt Hackel 						       dead_node);
5816714d8e8SKurt Hackel 			if (status < 0) {
5826714d8e8SKurt Hackel 				mlog_errno(status);
5836a413211SKurt Hackel 				if (dlm_is_host_down(status)) {
5846a413211SKurt Hackel 					/* node died, ignore it for recovery */
5856a413211SKurt Hackel 					status = 0;
5866714d8e8SKurt Hackel 					ndata->state = DLM_RECO_NODE_DATA_DEAD;
5876a413211SKurt Hackel 					/* wait for the domain map to catch up
5886a413211SKurt Hackel 					 * with the network state. */
5896a413211SKurt Hackel 					wait_event_timeout(dlm->dlm_reco_thread_wq,
5906a413211SKurt Hackel 							   dlm_is_node_dead(dlm,
5916a413211SKurt Hackel 								ndata->node_num),
5926a413211SKurt Hackel 							   msecs_to_jiffies(1000));
5936a413211SKurt Hackel 					mlog(0, "waited 1 sec for %u, "
5946a413211SKurt Hackel 					     "dead? %s\n", ndata->node_num,
5956a413211SKurt Hackel 					     dlm_is_node_dead(dlm, ndata->node_num) ?
5966a413211SKurt Hackel 					     "yes" : "no");
5976a413211SKurt Hackel 				} else {
5986a413211SKurt Hackel 					/* -ENOMEM on the other node */
5996a413211SKurt Hackel 					mlog(0, "%s: node %u returned "
6006a413211SKurt Hackel 					     "%d during recovery, retrying "
6016a413211SKurt Hackel 					     "after a short wait\n",
6026a413211SKurt Hackel 					     dlm->name, ndata->node_num,
6036a413211SKurt Hackel 					     status);
6046a413211SKurt Hackel 					msleep(100);
6056714d8e8SKurt Hackel 				}
6066714d8e8SKurt Hackel 			}
6076a413211SKurt Hackel 		} while (status != 0);
6086714d8e8SKurt Hackel 
609756a1501SSrinivas Eeda 		spin_lock(&dlm_reco_state_lock);
6106714d8e8SKurt Hackel 		switch (ndata->state) {
6116714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
6126714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
6136714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
6146714d8e8SKurt Hackel 				BUG();
6156714d8e8SKurt Hackel 				break;
6166714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
6176714d8e8SKurt Hackel 				mlog(0, "node %u died after requesting "
6186714d8e8SKurt Hackel 				     "recovery info for node %u\n",
6196714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6206a413211SKurt Hackel 				/* fine.  don't need this node's info.
6216a413211SKurt Hackel 				 * continue without it. */
6226a413211SKurt Hackel 				break;
6236714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
6246714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
6256714d8e8SKurt Hackel 				mlog(0, "now receiving recovery data from "
6266714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6276714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6286714d8e8SKurt Hackel 				break;
6296714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
6306714d8e8SKurt Hackel 				mlog(0, "already receiving recovery data from "
6316714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6326714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6336714d8e8SKurt Hackel 				break;
6346714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
6356714d8e8SKurt Hackel 				mlog(0, "already DONE receiving recovery data "
6366714d8e8SKurt Hackel 				     "from node %u for dead node %u\n",
6376714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6386714d8e8SKurt Hackel 				break;
6396714d8e8SKurt Hackel 		}
640756a1501SSrinivas Eeda 		spin_unlock(&dlm_reco_state_lock);
6416714d8e8SKurt Hackel 	}
6426714d8e8SKurt Hackel 
6436714d8e8SKurt Hackel 	mlog(0, "done requesting all lock info\n");
6446714d8e8SKurt Hackel 
6456714d8e8SKurt Hackel 	/* nodes should be sending reco data now
6466714d8e8SKurt Hackel 	 * just need to wait */
6476714d8e8SKurt Hackel 
6486714d8e8SKurt Hackel 	while (1) {
6496714d8e8SKurt Hackel 		/* check all the nodes now to see if we are
6506714d8e8SKurt Hackel 		 * done, or if anyone died */
6516714d8e8SKurt Hackel 		all_nodes_done = 1;
6526714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
653800deef3SChristoph Hellwig 		list_for_each_entry(ndata, &dlm->reco.node_data, list) {
6546714d8e8SKurt Hackel 			mlog(0, "checking recovery state of node %u\n",
6556714d8e8SKurt Hackel 			     ndata->node_num);
6566714d8e8SKurt Hackel 			switch (ndata->state) {
6576714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_INIT:
6586714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTING:
6596714d8e8SKurt Hackel 					mlog(ML_ERROR, "bad ndata state for "
6606714d8e8SKurt Hackel 					     "node %u: state=%d\n",
6616714d8e8SKurt Hackel 					     ndata->node_num, ndata->state);
6626714d8e8SKurt Hackel 					BUG();
6636714d8e8SKurt Hackel 					break;
6646714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DEAD:
6656a413211SKurt Hackel 					mlog(0, "node %u died after "
6666714d8e8SKurt Hackel 					     "requesting recovery info for "
6676714d8e8SKurt Hackel 					     "node %u\n", ndata->node_num,
6686714d8e8SKurt Hackel 					     dead_node);
6696a413211SKurt Hackel 					break;
6706714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_RECEIVING:
6716714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTED:
672d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u still in state %s\n",
673d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num,
674d6dea6e9SKurt Hackel 					     ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
675d6dea6e9SKurt Hackel 					     "receiving" : "requested");
6766714d8e8SKurt Hackel 					all_nodes_done = 0;
6776714d8e8SKurt Hackel 					break;
6786714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DONE:
679d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is done\n",
680d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6816714d8e8SKurt Hackel 					break;
6826714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_FINALIZE_SENT:
683d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is finalize\n",
684d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6856714d8e8SKurt Hackel 					break;
6866714d8e8SKurt Hackel 			}
6876714d8e8SKurt Hackel 		}
6886714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
6896714d8e8SKurt Hackel 
6906714d8e8SKurt Hackel 		mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
6916714d8e8SKurt Hackel 		     all_nodes_done?"yes":"no");
6926714d8e8SKurt Hackel 		if (all_nodes_done) {
6936714d8e8SKurt Hackel 			int ret;
6946714d8e8SKurt Hackel 
6956714d8e8SKurt Hackel 			/* all nodes are now in DLM_RECO_NODE_DATA_DONE state
6966714d8e8SKurt Hackel 	 		 * just send a finalize message to everyone and
6976714d8e8SKurt Hackel 	 		 * clean up */
6986714d8e8SKurt Hackel 			mlog(0, "all nodes are done! send finalize\n");
6996714d8e8SKurt Hackel 			ret = dlm_send_finalize_reco_message(dlm);
7006714d8e8SKurt Hackel 			if (ret < 0)
7016714d8e8SKurt Hackel 				mlog_errno(ret);
7026714d8e8SKurt Hackel 
7036714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7046714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, dead_node,
7056714d8e8SKurt Hackel 							  dlm->node_num);
7066714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
7076714d8e8SKurt Hackel 			mlog(0, "should be done with recovery!\n");
7086714d8e8SKurt Hackel 
7096714d8e8SKurt Hackel 			mlog(0, "finishing recovery of %s at %lu, "
7106714d8e8SKurt Hackel 			     "dead=%u, this=%u, new=%u\n", dlm->name,
7116714d8e8SKurt Hackel 			     jiffies, dlm->reco.dead_node,
7126714d8e8SKurt Hackel 			     dlm->node_num, dlm->reco.new_master);
7136714d8e8SKurt Hackel 			destroy = 1;
7146a413211SKurt Hackel 			status = 0;
7156714d8e8SKurt Hackel 			/* rescan everything marked dirty along the way */
7166714d8e8SKurt Hackel 			dlm_kick_thread(dlm, NULL);
7176714d8e8SKurt Hackel 			break;
7186714d8e8SKurt Hackel 		}
7196714d8e8SKurt Hackel 		/* wait to be signalled, with periodic timeout
7206714d8e8SKurt Hackel 		 * to check for node death */
7216714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
7226714d8e8SKurt Hackel 					 kthread_should_stop(),
7236714d8e8SKurt Hackel 					 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
7246714d8e8SKurt Hackel 
7256714d8e8SKurt Hackel 	}
7266714d8e8SKurt Hackel 
7276714d8e8SKurt Hackel 	if (destroy)
7286714d8e8SKurt Hackel 		dlm_destroy_recovery_area(dlm, dead_node);
7296714d8e8SKurt Hackel 
7306714d8e8SKurt Hackel 	mlog_exit(status);
7316714d8e8SKurt Hackel 	return status;
7326714d8e8SKurt Hackel }
7336714d8e8SKurt Hackel 
7346714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
7356714d8e8SKurt Hackel {
7366714d8e8SKurt Hackel 	int num=0;
7376714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
7386714d8e8SKurt Hackel 
7396714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
7406714d8e8SKurt Hackel 	memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
7416714d8e8SKurt Hackel 	/* nodes can only be removed (by dying) after dropping
7426714d8e8SKurt Hackel 	 * this lock, and death will be trapped later, so this should do */
7436714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
7446714d8e8SKurt Hackel 
7456714d8e8SKurt Hackel 	while (1) {
7466714d8e8SKurt Hackel 		num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
7476714d8e8SKurt Hackel 		if (num >= O2NM_MAX_NODES) {
7486714d8e8SKurt Hackel 			break;
7496714d8e8SKurt Hackel 		}
7506714d8e8SKurt Hackel 		BUG_ON(num == dead_node);
7516714d8e8SKurt Hackel 
752cd861280SRobert P. J. Day 		ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
7536714d8e8SKurt Hackel 		if (!ndata) {
7546714d8e8SKurt Hackel 			dlm_destroy_recovery_area(dlm, dead_node);
7556714d8e8SKurt Hackel 			return -ENOMEM;
7566714d8e8SKurt Hackel 		}
7576714d8e8SKurt Hackel 		ndata->node_num = num;
7586714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_INIT;
7596714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
7606714d8e8SKurt Hackel 		list_add_tail(&ndata->list, &dlm->reco.node_data);
7616714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
7626714d8e8SKurt Hackel 		num++;
7636714d8e8SKurt Hackel 	}
7646714d8e8SKurt Hackel 
7656714d8e8SKurt Hackel 	return 0;
7666714d8e8SKurt Hackel }
7676714d8e8SKurt Hackel 
7686714d8e8SKurt Hackel static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
7696714d8e8SKurt Hackel {
770800deef3SChristoph Hellwig 	struct dlm_reco_node_data *ndata, *next;
7716714d8e8SKurt Hackel 	LIST_HEAD(tmplist);
7726714d8e8SKurt Hackel 
7736714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
7746714d8e8SKurt Hackel 	list_splice_init(&dlm->reco.node_data, &tmplist);
7756714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
7766714d8e8SKurt Hackel 
777800deef3SChristoph Hellwig 	list_for_each_entry_safe(ndata, next, &tmplist, list) {
7786714d8e8SKurt Hackel 		list_del_init(&ndata->list);
7796714d8e8SKurt Hackel 		kfree(ndata);
7806714d8e8SKurt Hackel 	}
7816714d8e8SKurt Hackel }
7826714d8e8SKurt Hackel 
7836714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
7846714d8e8SKurt Hackel 				 u8 dead_node)
7856714d8e8SKurt Hackel {
7866714d8e8SKurt Hackel 	struct dlm_lock_request lr;
7876714d8e8SKurt Hackel 	enum dlm_status ret;
7886714d8e8SKurt Hackel 
7896714d8e8SKurt Hackel 	mlog(0, "\n");
7906714d8e8SKurt Hackel 
7916714d8e8SKurt Hackel 
7926714d8e8SKurt Hackel 	mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
7936714d8e8SKurt Hackel 		  "to %u\n", dead_node, request_from);
7946714d8e8SKurt Hackel 
7956714d8e8SKurt Hackel 	memset(&lr, 0, sizeof(lr));
7966714d8e8SKurt Hackel 	lr.node_idx = dlm->node_num;
7976714d8e8SKurt Hackel 	lr.dead_node = dead_node;
7986714d8e8SKurt Hackel 
7996714d8e8SKurt Hackel 	// send message
8006714d8e8SKurt Hackel 	ret = DLM_NOLOCKMGR;
8016714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
8026714d8e8SKurt Hackel 				 &lr, sizeof(lr), request_from, NULL);
8036714d8e8SKurt Hackel 
8046714d8e8SKurt Hackel 	/* negative status is handled by caller */
8056714d8e8SKurt Hackel 	if (ret < 0)
806a5196ec5SWengang Wang 		mlog(ML_ERROR, "Error %d when sending message %u (key "
807a5196ec5SWengang Wang 		     "0x%x) to node %u\n", ret, DLM_LOCK_REQUEST_MSG,
808a5196ec5SWengang Wang 		     dlm->key, request_from);
8096714d8e8SKurt Hackel 
8106714d8e8SKurt Hackel 	// return from here, then
8116714d8e8SKurt Hackel 	// sleep until all received or error
8126714d8e8SKurt Hackel 	return ret;
8136714d8e8SKurt Hackel 
8146714d8e8SKurt Hackel }
8156714d8e8SKurt Hackel 
816d74c9803SKurt Hackel int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
817d74c9803SKurt Hackel 				  void **ret_data)
8186714d8e8SKurt Hackel {
8196714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
8206714d8e8SKurt Hackel 	struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
8216714d8e8SKurt Hackel 	char *buf = NULL;
8226714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
8236714d8e8SKurt Hackel 
8246714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
8256714d8e8SKurt Hackel 		return -EINVAL;
8266714d8e8SKurt Hackel 
827c3187ce5SKurt Hackel 	if (lr->dead_node != dlm->reco.dead_node) {
828c3187ce5SKurt Hackel 		mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
829c3187ce5SKurt Hackel 		     "dead_node is %u\n", dlm->name, lr->node_idx,
830c3187ce5SKurt Hackel 		     lr->dead_node, dlm->reco.dead_node);
831d6dea6e9SKurt Hackel 		dlm_print_reco_node_status(dlm);
832c3187ce5SKurt Hackel 		/* this is a hack */
833c3187ce5SKurt Hackel 		dlm_put(dlm);
834c3187ce5SKurt Hackel 		return -ENOMEM;
835c3187ce5SKurt Hackel 	}
8366714d8e8SKurt Hackel 	BUG_ON(lr->dead_node != dlm->reco.dead_node);
8376714d8e8SKurt Hackel 
838cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
8396714d8e8SKurt Hackel 	if (!item) {
8406714d8e8SKurt Hackel 		dlm_put(dlm);
8416714d8e8SKurt Hackel 		return -ENOMEM;
8426714d8e8SKurt Hackel 	}
8436714d8e8SKurt Hackel 
8446714d8e8SKurt Hackel 	/* this will get freed by dlm_request_all_locks_worker */
845ad8100e0SKurt Hackel 	buf = (char *) __get_free_page(GFP_NOFS);
8466714d8e8SKurt Hackel 	if (!buf) {
8476714d8e8SKurt Hackel 		kfree(item);
8486714d8e8SKurt Hackel 		dlm_put(dlm);
8496714d8e8SKurt Hackel 		return -ENOMEM;
8506714d8e8SKurt Hackel 	}
8516714d8e8SKurt Hackel 
8526714d8e8SKurt Hackel 	/* queue up work for dlm_request_all_locks_worker */
8536714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
8546714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
8556714d8e8SKurt Hackel 	item->u.ral.reco_master = lr->node_idx;
8566714d8e8SKurt Hackel 	item->u.ral.dead_node = lr->dead_node;
8576714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
8586714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
8596714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
8603156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
8616714d8e8SKurt Hackel 
8626714d8e8SKurt Hackel 	dlm_put(dlm);
8636714d8e8SKurt Hackel 	return 0;
8646714d8e8SKurt Hackel }
8656714d8e8SKurt Hackel 
8666714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
8676714d8e8SKurt Hackel {
8686714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
8696714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
8706714d8e8SKurt Hackel 	struct dlm_ctxt *dlm;
8716714d8e8SKurt Hackel 	LIST_HEAD(resources);
8726714d8e8SKurt Hackel 	int ret;
8736714d8e8SKurt Hackel 	u8 dead_node, reco_master;
87429c0fa0fSKurt Hackel 	int skip_all_done = 0;
8756714d8e8SKurt Hackel 
8766714d8e8SKurt Hackel 	dlm = item->dlm;
8776714d8e8SKurt Hackel 	dead_node = item->u.ral.dead_node;
8786714d8e8SKurt Hackel 	reco_master = item->u.ral.reco_master;
879e2faea4cSKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
880e2faea4cSKurt Hackel 
881d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
882d6dea6e9SKurt Hackel 	     dlm->name, dead_node, reco_master);
883d6dea6e9SKurt Hackel 
884e2faea4cSKurt Hackel 	if (dead_node != dlm->reco.dead_node ||
885e2faea4cSKurt Hackel 	    reco_master != dlm->reco.new_master) {
8866a413211SKurt Hackel 		/* worker could have been created before the recovery master
8876a413211SKurt Hackel 		 * died.  if so, do not continue, but do not error. */
8886a413211SKurt Hackel 		if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
8896a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: will not send recovery state, "
8906a413211SKurt Hackel 			     "recovery master %u died, thread=(dead=%u,mas=%u)"
8916a413211SKurt Hackel 			     " current=(dead=%u,mas=%u)\n", dlm->name,
8926a413211SKurt Hackel 			     reco_master, dead_node, reco_master,
8936a413211SKurt Hackel 			     dlm->reco.dead_node, dlm->reco.new_master);
8946a413211SKurt Hackel 		} else {
8956a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
8966a413211SKurt Hackel 			     "master=%u), request(dead=%u, master=%u)\n",
8976a413211SKurt Hackel 			     dlm->name, dlm->reco.dead_node,
8986a413211SKurt Hackel 			     dlm->reco.new_master, dead_node, reco_master);
899e2faea4cSKurt Hackel 		}
9006a413211SKurt Hackel 		goto leave;
9016a413211SKurt Hackel 	}
9026714d8e8SKurt Hackel 
9036714d8e8SKurt Hackel 	/* lock resources should have already been moved to the
9046714d8e8SKurt Hackel  	 * dlm->reco.resources list.  now move items from that list
9056714d8e8SKurt Hackel  	 * to a temp list if the dead owner matches.  note that the
9066714d8e8SKurt Hackel 	 * whole cluster recovers only one node at a time, so we
9076714d8e8SKurt Hackel 	 * can safely move UNKNOWN lock resources for each recovery
9086714d8e8SKurt Hackel 	 * session. */
9096714d8e8SKurt Hackel 	dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
9106714d8e8SKurt Hackel 
9116714d8e8SKurt Hackel 	/* now we can begin blasting lockreses without the dlm lock */
91229c0fa0fSKurt Hackel 
91329c0fa0fSKurt Hackel 	/* any errors returned will be due to the new_master dying,
91429c0fa0fSKurt Hackel 	 * the dlm_reco_thread should detect this */
915800deef3SChristoph Hellwig 	list_for_each_entry(res, &resources, recovering) {
9166714d8e8SKurt Hackel 		ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
9176714d8e8SKurt Hackel 				   	DLM_MRES_RECOVERY);
91829c0fa0fSKurt Hackel 		if (ret < 0) {
919d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
920d6dea6e9SKurt Hackel 			     "recovery state for dead node %u, ret=%d\n", dlm->name,
921d6dea6e9SKurt Hackel 			     reco_master, dead_node, ret);
92229c0fa0fSKurt Hackel 			skip_all_done = 1;
92329c0fa0fSKurt Hackel 			break;
92429c0fa0fSKurt Hackel 		}
9256714d8e8SKurt Hackel 	}
9266714d8e8SKurt Hackel 
9276714d8e8SKurt Hackel 	/* move the resources back to the list */
9286714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
9296714d8e8SKurt Hackel 	list_splice_init(&resources, &dlm->reco.resources);
9306714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
9316714d8e8SKurt Hackel 
93229c0fa0fSKurt Hackel 	if (!skip_all_done) {
9336714d8e8SKurt Hackel 		ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
93429c0fa0fSKurt Hackel 		if (ret < 0) {
935d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
936d6dea6e9SKurt Hackel 			     "recovery all-done for dead node %u, ret=%d\n",
937d6dea6e9SKurt Hackel 			     dlm->name, reco_master, dead_node, ret);
93829c0fa0fSKurt Hackel 		}
93929c0fa0fSKurt Hackel 	}
9406a413211SKurt Hackel leave:
9416714d8e8SKurt Hackel 	free_page((unsigned long)data);
9426714d8e8SKurt Hackel }
9436714d8e8SKurt Hackel 
9446714d8e8SKurt Hackel 
9456714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
9466714d8e8SKurt Hackel {
9476714d8e8SKurt Hackel 	int ret, tmpret;
9486714d8e8SKurt Hackel 	struct dlm_reco_data_done done_msg;
9496714d8e8SKurt Hackel 
9506714d8e8SKurt Hackel 	memset(&done_msg, 0, sizeof(done_msg));
9516714d8e8SKurt Hackel 	done_msg.node_idx = dlm->node_num;
9526714d8e8SKurt Hackel 	done_msg.dead_node = dead_node;
9536714d8e8SKurt Hackel 	mlog(0, "sending DATA DONE message to %u, "
9546714d8e8SKurt Hackel 	     "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
9556714d8e8SKurt Hackel 	     done_msg.dead_node);
9566714d8e8SKurt Hackel 
9576714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
9586714d8e8SKurt Hackel 				 sizeof(done_msg), send_to, &tmpret);
95929c0fa0fSKurt Hackel 	if (ret < 0) {
960a5196ec5SWengang Wang 		mlog(ML_ERROR, "Error %d when sending message %u (key "
961a5196ec5SWengang Wang 		     "0x%x) to node %u\n", ret, DLM_RECO_DATA_DONE_MSG,
962a5196ec5SWengang Wang 		     dlm->key, send_to);
96329c0fa0fSKurt Hackel 		if (!dlm_is_host_down(ret)) {
96429c0fa0fSKurt Hackel 			BUG();
96529c0fa0fSKurt Hackel 		}
96629c0fa0fSKurt Hackel 	} else
9676714d8e8SKurt Hackel 		ret = tmpret;
9686714d8e8SKurt Hackel 	return ret;
9696714d8e8SKurt Hackel }
9706714d8e8SKurt Hackel 
9716714d8e8SKurt Hackel 
972d74c9803SKurt Hackel int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
973d74c9803SKurt Hackel 			       void **ret_data)
9746714d8e8SKurt Hackel {
9756714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
9766714d8e8SKurt Hackel 	struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
9776714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata = NULL;
9786714d8e8SKurt Hackel 	int ret = -EINVAL;
9796714d8e8SKurt Hackel 
9806714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
9816714d8e8SKurt Hackel 		return -EINVAL;
9826714d8e8SKurt Hackel 
9836714d8e8SKurt Hackel 	mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
9846714d8e8SKurt Hackel 	     "node_idx=%u, this node=%u\n", done->dead_node,
9856714d8e8SKurt Hackel 	     dlm->reco.dead_node, done->node_idx, dlm->node_num);
986d6dea6e9SKurt Hackel 
987d6dea6e9SKurt Hackel 	mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
988d6dea6e9SKurt Hackel 			"Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
989d6dea6e9SKurt Hackel 			"node_idx=%u, this node=%u\n", done->dead_node,
990d6dea6e9SKurt Hackel 			dlm->reco.dead_node, done->node_idx, dlm->node_num);
9916714d8e8SKurt Hackel 
9926714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
993800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
9946714d8e8SKurt Hackel 		if (ndata->node_num != done->node_idx)
9956714d8e8SKurt Hackel 			continue;
9966714d8e8SKurt Hackel 
9976714d8e8SKurt Hackel 		switch (ndata->state) {
998e2faea4cSKurt Hackel 			/* should have moved beyond INIT but not to FINALIZE yet */
9996714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
10006714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
10016714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
10026714d8e8SKurt Hackel 				mlog(ML_ERROR, "bad ndata state for node %u:"
10036714d8e8SKurt Hackel 				     " state=%d\n", ndata->node_num,
10046714d8e8SKurt Hackel 				     ndata->state);
10056714d8e8SKurt Hackel 				BUG();
10066714d8e8SKurt Hackel 				break;
1007e2faea4cSKurt Hackel 			/* these states are possible at this point, anywhere along
1008e2faea4cSKurt Hackel 			 * the line of recovery */
1009e2faea4cSKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
10106714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
10116714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
10126714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
10136714d8e8SKurt Hackel 				mlog(0, "node %u is DONE sending "
10146714d8e8SKurt Hackel 					  "recovery data!\n",
10156714d8e8SKurt Hackel 					  ndata->node_num);
10166714d8e8SKurt Hackel 
10176714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_DONE;
10186714d8e8SKurt Hackel 				ret = 0;
10196714d8e8SKurt Hackel 				break;
10206714d8e8SKurt Hackel 		}
10216714d8e8SKurt Hackel 	}
10226714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
10236714d8e8SKurt Hackel 
10246714d8e8SKurt Hackel 	/* wake the recovery thread, some node is done */
10256714d8e8SKurt Hackel 	if (!ret)
10266714d8e8SKurt Hackel 		dlm_kick_recovery_thread(dlm);
10276714d8e8SKurt Hackel 
10286714d8e8SKurt Hackel 	if (ret < 0)
10296714d8e8SKurt Hackel 		mlog(ML_ERROR, "failed to find recovery node data for node "
10306714d8e8SKurt Hackel 		     "%u\n", done->node_idx);
10316714d8e8SKurt Hackel 	dlm_put(dlm);
10326714d8e8SKurt Hackel 
10336714d8e8SKurt Hackel 	mlog(0, "leaving reco data done handler, ret=%d\n", ret);
10346714d8e8SKurt Hackel 	return ret;
10356714d8e8SKurt Hackel }
10366714d8e8SKurt Hackel 
10376714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
10386714d8e8SKurt Hackel 					struct list_head *list,
10396714d8e8SKurt Hackel 				       	u8 dead_node)
10406714d8e8SKurt Hackel {
1041800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
1042e2faea4cSKurt Hackel 	struct dlm_lock *lock;
10436714d8e8SKurt Hackel 
10446714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1045800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
1046e2faea4cSKurt Hackel 		/* always prune any $RECOVERY entries for dead nodes,
1047e2faea4cSKurt Hackel 		 * otherwise hangs can occur during later recovery */
10486714d8e8SKurt Hackel 		if (dlm_is_recovery_lock(res->lockname.name,
1049e2faea4cSKurt Hackel 					 res->lockname.len)) {
1050e2faea4cSKurt Hackel 			spin_lock(&res->spinlock);
1051e2faea4cSKurt Hackel 			list_for_each_entry(lock, &res->granted, list) {
1052e2faea4cSKurt Hackel 				if (lock->ml.node == dead_node) {
1053e2faea4cSKurt Hackel 					mlog(0, "AHA! there was "
1054e2faea4cSKurt Hackel 					     "a $RECOVERY lock for dead "
1055e2faea4cSKurt Hackel 					     "node %u (%s)!\n",
1056e2faea4cSKurt Hackel 					     dead_node, dlm->name);
1057e2faea4cSKurt Hackel 					list_del_init(&lock->list);
1058e2faea4cSKurt Hackel 					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 
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 
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);
11036714d8e8SKurt Hackel 	int sz, 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 	sz = sizeof(struct dlm_migratable_lockres) +
11126714d8e8SKurt Hackel 		(mres->num_locks * sizeof(struct dlm_migratable_lock));
11136714d8e8SKurt Hackel 
11146714d8e8SKurt Hackel 	/* add an all-done flag if we reached the last lock */
11156714d8e8SKurt Hackel 	orig_flags = mres->flags;
11166714d8e8SKurt Hackel 	BUG_ON(total_locks > mres_total_locks);
11176714d8e8SKurt Hackel 	if (total_locks == mres_total_locks)
11186714d8e8SKurt Hackel 		mres->flags |= DLM_MRES_ALL_DONE;
11196714d8e8SKurt Hackel 
1120ba2bf218SKurt Hackel 	mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1121ba2bf218SKurt Hackel 	     dlm->name, res->lockname.len, res->lockname.name,
112217ae26b6SJeff Liu 	     orig_flags & DLM_MRES_MIGRATION ? "migration" : "recovery",
1123ba2bf218SKurt Hackel 	     send_to);
1124ba2bf218SKurt Hackel 
11256714d8e8SKurt Hackel 	/* send it */
11266714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
11276714d8e8SKurt Hackel 				 sz, send_to, &status);
11286714d8e8SKurt Hackel 	if (ret < 0) {
11296714d8e8SKurt Hackel 		/* XXX: negative status is not handled.
11306714d8e8SKurt Hackel 		 * this will end up killing this node. */
1131a5196ec5SWengang Wang 		mlog(ML_ERROR, "Error %d when sending message %u (key "
1132a5196ec5SWengang Wang 		     "0x%x) to node %u\n", ret, DLM_MIG_LOCKRES_MSG,
1133a5196ec5SWengang Wang 		     dlm->key, send_to);
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 
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 
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 */
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 
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 
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 
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 
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;
13576714d8e8SKurt Hackel 
13586714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
13596714d8e8SKurt Hackel 		return -EINVAL;
13606714d8e8SKurt Hackel 
13616714d8e8SKurt Hackel 	BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
13626714d8e8SKurt Hackel 
13636714d8e8SKurt Hackel 	real_master = mres->master;
13646714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
13656714d8e8SKurt Hackel 		/* cannot migrate a lockres with no master */
13666714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
13676714d8e8SKurt Hackel 	}
13686714d8e8SKurt Hackel 
13696714d8e8SKurt Hackel 	mlog(0, "%s message received from node %u\n",
13706714d8e8SKurt Hackel 		  (mres->flags & DLM_MRES_RECOVERY) ?
13716714d8e8SKurt Hackel 		  "recovery" : "migration", mres->master);
13726714d8e8SKurt Hackel 	if (mres->flags & DLM_MRES_ALL_DONE)
13736714d8e8SKurt Hackel 		mlog(0, "all done flag.  all lockres data received!\n");
13746714d8e8SKurt Hackel 
13756714d8e8SKurt Hackel 	ret = -ENOMEM;
1376ad8100e0SKurt Hackel 	buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1377cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
13786714d8e8SKurt Hackel 	if (!buf || !item)
13796714d8e8SKurt Hackel 		goto leave;
13806714d8e8SKurt Hackel 
13816714d8e8SKurt Hackel 	/* lookup the lock to see if we have a secondary queue for this
13826714d8e8SKurt Hackel 	 * already...  just add the locks in and this will have its owner
13836714d8e8SKurt Hackel 	 * and RECOVERY flag changed when it completes. */
13846714d8e8SKurt Hackel 	res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
13856714d8e8SKurt Hackel 	if (res) {
13866714d8e8SKurt Hackel 	 	/* this will get a ref on res */
13876714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
13886714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
13896714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY) {
13906714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
13916714d8e8SKurt Hackel 		} else {
13926714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_MIGRATING) {
13936714d8e8SKurt Hackel 				/* this is at least the second
13946714d8e8SKurt Hackel 				 * lockres message */
13956714d8e8SKurt Hackel 				mlog(0, "lock %.*s is already migrating\n",
13966714d8e8SKurt Hackel 					  mres->lockname_len,
13976714d8e8SKurt Hackel 					  mres->lockname);
13986714d8e8SKurt Hackel 			} else if (res->state & DLM_LOCK_RES_RECOVERING) {
13996714d8e8SKurt Hackel 				/* caller should BUG */
14006714d8e8SKurt Hackel 				mlog(ML_ERROR, "node is attempting to migrate "
14016714d8e8SKurt Hackel 				     "lock %.*s, but marked as recovering!\n",
14026714d8e8SKurt Hackel 				     mres->lockname_len, mres->lockname);
14036714d8e8SKurt Hackel 				ret = -EFAULT;
14046714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
14056714d8e8SKurt Hackel 				goto leave;
14066714d8e8SKurt Hackel 			}
14076714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
14086714d8e8SKurt Hackel 		}
14096714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
14106714d8e8SKurt Hackel 	} else {
14116714d8e8SKurt Hackel 		/* need to allocate, just like if it was
14126714d8e8SKurt Hackel 		 * mastered here normally  */
14136714d8e8SKurt Hackel 		res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
14146714d8e8SKurt Hackel 		if (!res)
14156714d8e8SKurt Hackel 			goto leave;
14166714d8e8SKurt Hackel 
14176714d8e8SKurt Hackel 		/* to match the ref that we would have gotten if
14186714d8e8SKurt Hackel 		 * dlm_lookup_lockres had succeeded */
14196714d8e8SKurt Hackel 		dlm_lockres_get(res);
14206714d8e8SKurt Hackel 
14216714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
14226714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY)
14236714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
14246714d8e8SKurt Hackel 		else
14256714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
14266714d8e8SKurt Hackel 
14276714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
14286714d8e8SKurt Hackel 		__dlm_insert_lockres(dlm, res);
14296714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
14306714d8e8SKurt Hackel 
143152987e2aSSunil Mushran 		/* Add an extra ref for this lock-less lockres lest the
143252987e2aSSunil Mushran 		 * dlm_thread purges it before we get the chance to add
143352987e2aSSunil Mushran 		 * locks to it */
143452987e2aSSunil Mushran 		dlm_lockres_get(res);
143552987e2aSSunil Mushran 
143652987e2aSSunil Mushran 		/* There are three refs that need to be put.
143752987e2aSSunil Mushran 		 * 1. Taken above.
143852987e2aSSunil Mushran 		 * 2. kref_init in dlm_new_lockres()->dlm_init_lockres().
143952987e2aSSunil Mushran 		 * 3. dlm_lookup_lockres()
144052987e2aSSunil Mushran 		 * The first one is handled at the end of this function. The
144152987e2aSSunil Mushran 		 * other two are handled in the worker thread after locks have
144252987e2aSSunil Mushran 		 * been attached. Yes, we don't wait for purge time to match
144352987e2aSSunil Mushran 		 * kref_init. The lockres will still have atleast one ref
144452987e2aSSunil Mushran 		 * added because it is in the hash __dlm_insert_lockres() */
144552987e2aSSunil Mushran 		extra_refs++;
144652987e2aSSunil Mushran 
14476714d8e8SKurt Hackel 		/* now that the new lockres is inserted,
14486714d8e8SKurt Hackel 		 * make it usable by other processes */
14496714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
14506714d8e8SKurt Hackel 		res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
14516714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
1452a6fa3640SKurt Hackel 		wake_up(&res->wq);
14536714d8e8SKurt Hackel 	}
14546714d8e8SKurt Hackel 
14556714d8e8SKurt Hackel 	/* at this point we have allocated everything we need,
14566714d8e8SKurt Hackel 	 * and we have a hashed lockres with an extra ref and
14576714d8e8SKurt Hackel 	 * the proper res->state flags. */
14586714d8e8SKurt Hackel 	ret = 0;
1459ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
1460ba2bf218SKurt Hackel 	/* drop this either when master requery finds a different master
1461ba2bf218SKurt Hackel 	 * or when a lock is added by the recovery worker */
1462ba2bf218SKurt Hackel 	dlm_lockres_grab_inflight_ref(dlm, res);
14636714d8e8SKurt Hackel 	if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
14646714d8e8SKurt Hackel 		/* migration cannot have an unknown master */
14656714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
14666714d8e8SKurt Hackel 		mlog(0, "recovery has passed me a lockres with an "
14676714d8e8SKurt Hackel 			  "unknown owner.. will need to requery: "
14686714d8e8SKurt Hackel 			  "%.*s\n", mres->lockname_len, mres->lockname);
14696714d8e8SKurt Hackel 	} else {
1470ba2bf218SKurt Hackel 		/* take a reference now to pin the lockres, drop it
1471ba2bf218SKurt Hackel 		 * when locks are added in the worker */
14726714d8e8SKurt Hackel 		dlm_change_lockres_owner(dlm, res, dlm->node_num);
14736714d8e8SKurt Hackel 	}
1474ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
14756714d8e8SKurt Hackel 
14766714d8e8SKurt Hackel 	/* queue up work for dlm_mig_lockres_worker */
14776714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
14786714d8e8SKurt Hackel 	memcpy(buf, msg->buf, be16_to_cpu(msg->data_len));  /* copy the whole message */
14796714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
14806714d8e8SKurt Hackel 	item->u.ml.lockres = res; /* already have a ref */
14816714d8e8SKurt Hackel 	item->u.ml.real_master = real_master;
148252987e2aSSunil Mushran 	item->u.ml.extra_ref = extra_refs;
14836714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
14846714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
14856714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
14863156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
14876714d8e8SKurt Hackel 
14886714d8e8SKurt Hackel leave:
148952987e2aSSunil Mushran 	/* One extra ref taken needs to be put here */
149052987e2aSSunil Mushran 	if (extra_refs)
149152987e2aSSunil Mushran 		dlm_lockres_put(res);
149252987e2aSSunil Mushran 
14936714d8e8SKurt Hackel 	dlm_put(dlm);
14946714d8e8SKurt Hackel 	if (ret < 0) {
14956714d8e8SKurt Hackel 		if (buf)
14966714d8e8SKurt Hackel 			kfree(buf);
14976714d8e8SKurt Hackel 		if (item)
14986714d8e8SKurt Hackel 			kfree(item);
14996714d8e8SKurt Hackel 	}
15006714d8e8SKurt Hackel 
15016714d8e8SKurt Hackel 	mlog_exit(ret);
15026714d8e8SKurt Hackel 	return ret;
15036714d8e8SKurt Hackel }
15046714d8e8SKurt Hackel 
15056714d8e8SKurt Hackel 
15066714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
15076714d8e8SKurt Hackel {
150852987e2aSSunil Mushran 	struct dlm_ctxt *dlm;
15096714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
15106714d8e8SKurt Hackel 	int ret = 0;
15116714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
15126714d8e8SKurt Hackel 	u8 real_master;
151352987e2aSSunil Mushran 	u8 extra_ref;
15146714d8e8SKurt Hackel 
15156714d8e8SKurt Hackel 	dlm = item->dlm;
15166714d8e8SKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
15176714d8e8SKurt Hackel 
15186714d8e8SKurt Hackel 	res = item->u.ml.lockres;
15196714d8e8SKurt Hackel 	real_master = item->u.ml.real_master;
152052987e2aSSunil Mushran 	extra_ref = item->u.ml.extra_ref;
15216714d8e8SKurt Hackel 
15226714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
15236714d8e8SKurt Hackel 		/* this case is super-rare. only occurs if
15246714d8e8SKurt Hackel 		 * node death happens during migration. */
15256714d8e8SKurt Hackel again:
15266714d8e8SKurt Hackel 		ret = dlm_lockres_master_requery(dlm, res, &real_master);
15276714d8e8SKurt Hackel 		if (ret < 0) {
1528e2faea4cSKurt Hackel 			mlog(0, "dlm_lockres_master_requery ret=%d\n",
15296714d8e8SKurt Hackel 				  ret);
15306714d8e8SKurt Hackel 			goto again;
15316714d8e8SKurt Hackel 		}
15326714d8e8SKurt Hackel 		if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
15336714d8e8SKurt Hackel 			mlog(0, "lockres %.*s not claimed.  "
15346714d8e8SKurt Hackel 				   "this node will take it.\n",
15356714d8e8SKurt Hackel 				   res->lockname.len, res->lockname.name);
15366714d8e8SKurt Hackel 		} else {
1537ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
1538ba2bf218SKurt Hackel 			dlm_lockres_drop_inflight_ref(dlm, res);
1539ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
15406714d8e8SKurt Hackel 			mlog(0, "master needs to respond to sender "
15416714d8e8SKurt Hackel 				  "that node %u still owns %.*s\n",
15426714d8e8SKurt Hackel 				  real_master, res->lockname.len,
15436714d8e8SKurt Hackel 				  res->lockname.name);
15446714d8e8SKurt Hackel 			/* cannot touch this lockres */
15456714d8e8SKurt Hackel 			goto leave;
15466714d8e8SKurt Hackel 		}
15476714d8e8SKurt Hackel 	}
15486714d8e8SKurt Hackel 
15496714d8e8SKurt Hackel 	ret = dlm_process_recovery_data(dlm, res, mres);
15506714d8e8SKurt Hackel 	if (ret < 0)
15516714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data returned  %d\n", ret);
15526714d8e8SKurt Hackel 	else
15536714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data succeeded\n");
15546714d8e8SKurt Hackel 
15556714d8e8SKurt Hackel 	if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
15566714d8e8SKurt Hackel 	                   (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
15576714d8e8SKurt Hackel 		ret = dlm_finish_migration(dlm, res, mres->master);
15586714d8e8SKurt Hackel 		if (ret < 0)
15596714d8e8SKurt Hackel 			mlog_errno(ret);
15606714d8e8SKurt Hackel 	}
15616714d8e8SKurt Hackel 
15626714d8e8SKurt Hackel leave:
156352987e2aSSunil Mushran 	/* See comment in dlm_mig_lockres_handler() */
156452987e2aSSunil Mushran 	if (res) {
156552987e2aSSunil Mushran 		if (extra_ref)
156652987e2aSSunil Mushran 			dlm_lockres_put(res);
156752987e2aSSunil Mushran 		dlm_lockres_put(res);
156852987e2aSSunil Mushran 	}
15696714d8e8SKurt Hackel 	kfree(data);
15706714d8e8SKurt Hackel 	mlog_exit(ret);
15716714d8e8SKurt Hackel }
15726714d8e8SKurt Hackel 
15736714d8e8SKurt Hackel 
15746714d8e8SKurt Hackel 
15758169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
15768169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
15778169cae5SAdrian Bunk 				      u8 *real_master)
15786714d8e8SKurt Hackel {
15796714d8e8SKurt Hackel 	struct dlm_node_iter iter;
15806714d8e8SKurt Hackel 	int nodenum;
15816714d8e8SKurt Hackel 	int ret = 0;
15826714d8e8SKurt Hackel 
15836714d8e8SKurt Hackel 	*real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
15846714d8e8SKurt Hackel 
15856714d8e8SKurt Hackel 	/* we only reach here if one of the two nodes in a
15866714d8e8SKurt Hackel 	 * migration died while the migration was in progress.
15876714d8e8SKurt Hackel 	 * at this point we need to requery the master.  we
15886714d8e8SKurt Hackel 	 * know that the new_master got as far as creating
15896714d8e8SKurt Hackel 	 * an mle on at least one node, but we do not know
15906714d8e8SKurt Hackel 	 * if any nodes had actually cleared the mle and set
15916714d8e8SKurt Hackel 	 * the master to the new_master.  the old master
15926714d8e8SKurt Hackel 	 * is supposed to set the owner to UNKNOWN in the
15936714d8e8SKurt Hackel 	 * event of a new_master death, so the only possible
15946714d8e8SKurt Hackel 	 * responses that we can get from nodes here are
15956714d8e8SKurt Hackel 	 * that the master is new_master, or that the master
15966714d8e8SKurt Hackel 	 * is UNKNOWN.
15976714d8e8SKurt Hackel 	 * if all nodes come back with UNKNOWN then we know
15986714d8e8SKurt Hackel 	 * the lock needs remastering here.
15996714d8e8SKurt Hackel 	 * if any node comes back with a valid master, check
16006714d8e8SKurt Hackel 	 * to see if that master is the one that we are
16016714d8e8SKurt Hackel 	 * recovering.  if so, then the new_master died and
16026714d8e8SKurt Hackel 	 * we need to remaster this lock.  if not, then the
16036714d8e8SKurt Hackel 	 * new_master survived and that node will respond to
16046714d8e8SKurt Hackel 	 * other nodes about the owner.
16056714d8e8SKurt Hackel 	 * if there is an owner, this node needs to dump this
16066714d8e8SKurt Hackel 	 * lockres and alert the sender that this lockres
16076714d8e8SKurt Hackel 	 * was rejected. */
16086714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
16096714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
16106714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
16116714d8e8SKurt Hackel 
16126714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
16136714d8e8SKurt Hackel 		/* do not send to self */
16146714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
16156714d8e8SKurt Hackel 			continue;
16166714d8e8SKurt Hackel 		ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
16176714d8e8SKurt Hackel 		if (ret < 0) {
16186714d8e8SKurt Hackel 			mlog_errno(ret);
1619c03872f5SKurt Hackel 			if (!dlm_is_host_down(ret))
16206714d8e8SKurt Hackel 				BUG();
1621c03872f5SKurt Hackel 			/* host is down, so answer for that node would be
1622c03872f5SKurt Hackel 			 * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
16236714d8e8SKurt Hackel 		}
16246714d8e8SKurt Hackel 		if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
16256714d8e8SKurt Hackel 			mlog(0, "lock master is %u\n", *real_master);
16266714d8e8SKurt Hackel 			break;
16276714d8e8SKurt Hackel 		}
16286714d8e8SKurt Hackel 	}
16296714d8e8SKurt Hackel 	return ret;
16306714d8e8SKurt Hackel }
16316714d8e8SKurt Hackel 
16326714d8e8SKurt Hackel 
1633c03872f5SKurt Hackel int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
16346714d8e8SKurt Hackel 			  u8 nodenum, u8 *real_master)
16356714d8e8SKurt Hackel {
16366714d8e8SKurt Hackel 	int ret = -EINVAL;
16376714d8e8SKurt Hackel 	struct dlm_master_requery req;
16386714d8e8SKurt Hackel 	int status = DLM_LOCK_RES_OWNER_UNKNOWN;
16396714d8e8SKurt Hackel 
16406714d8e8SKurt Hackel 	memset(&req, 0, sizeof(req));
16416714d8e8SKurt Hackel 	req.node_idx = dlm->node_num;
16426714d8e8SKurt Hackel 	req.namelen = res->lockname.len;
16436714d8e8SKurt Hackel 	memcpy(req.name, res->lockname.name, res->lockname.len);
16446714d8e8SKurt Hackel 
16456714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
16466714d8e8SKurt Hackel 				 &req, sizeof(req), nodenum, &status);
16476714d8e8SKurt Hackel 	/* XXX: negative status not handled properly here. */
16486714d8e8SKurt Hackel 	if (ret < 0)
1649a5196ec5SWengang Wang 		mlog(ML_ERROR, "Error %d when sending message %u (key "
1650a5196ec5SWengang Wang 		     "0x%x) to node %u\n", ret, DLM_MASTER_REQUERY_MSG,
1651a5196ec5SWengang Wang 		     dlm->key, nodenum);
16526714d8e8SKurt Hackel 	else {
16536714d8e8SKurt Hackel 		BUG_ON(status < 0);
16546714d8e8SKurt Hackel 		BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
16556714d8e8SKurt Hackel 		*real_master = (u8) (status & 0xff);
16566714d8e8SKurt Hackel 		mlog(0, "node %u responded to master requery with %u\n",
16576714d8e8SKurt Hackel 			  nodenum, *real_master);
16586714d8e8SKurt Hackel 		ret = 0;
16596714d8e8SKurt Hackel 	}
16606714d8e8SKurt Hackel 	return ret;
16616714d8e8SKurt Hackel }
16626714d8e8SKurt Hackel 
16636714d8e8SKurt Hackel 
16646714d8e8SKurt Hackel /* this function cannot error, so unless the sending
16656714d8e8SKurt Hackel  * or receiving of the message failed, the owner can
16666714d8e8SKurt Hackel  * be trusted */
1667d74c9803SKurt Hackel int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
1668d74c9803SKurt Hackel 			       void **ret_data)
16696714d8e8SKurt Hackel {
16706714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
16716714d8e8SKurt Hackel 	struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
16726714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
1673a3d33291SMark Fasheh 	unsigned int hash;
16746714d8e8SKurt Hackel 	int master = DLM_LOCK_RES_OWNER_UNKNOWN;
16756714d8e8SKurt Hackel 	u32 flags = DLM_ASSERT_MASTER_REQUERY;
16766714d8e8SKurt Hackel 
16776714d8e8SKurt Hackel 	if (!dlm_grab(dlm)) {
16786714d8e8SKurt Hackel 		/* since the domain has gone away on this
16796714d8e8SKurt Hackel 		 * node, the proper response is UNKNOWN */
16806714d8e8SKurt Hackel 		return master;
16816714d8e8SKurt Hackel 	}
16826714d8e8SKurt Hackel 
1683a3d33291SMark Fasheh 	hash = dlm_lockid_hash(req->name, req->namelen);
1684a3d33291SMark Fasheh 
16856714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1686a3d33291SMark Fasheh 	res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
16876714d8e8SKurt Hackel 	if (res) {
16886714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
16896714d8e8SKurt Hackel 		master = res->owner;
16906714d8e8SKurt Hackel 		if (master == dlm->node_num) {
16916714d8e8SKurt Hackel 			int ret = dlm_dispatch_assert_master(dlm, res,
16926714d8e8SKurt Hackel 							     0, 0, flags);
16936714d8e8SKurt Hackel 			if (ret < 0) {
16946714d8e8SKurt Hackel 				mlog_errno(-ENOMEM);
16956714d8e8SKurt Hackel 				/* retry!? */
16966714d8e8SKurt Hackel 				BUG();
16976714d8e8SKurt Hackel 			}
169852987e2aSSunil Mushran 		} else /* put.. incase we are not the master */
169952987e2aSSunil Mushran 			dlm_lockres_put(res);
17006714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
17016714d8e8SKurt Hackel 	}
17026714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
17036714d8e8SKurt Hackel 
17046714d8e8SKurt Hackel 	dlm_put(dlm);
17056714d8e8SKurt Hackel 	return master;
17066714d8e8SKurt Hackel }
17076714d8e8SKurt Hackel 
17086714d8e8SKurt Hackel static inline struct list_head *
17096714d8e8SKurt Hackel dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
17106714d8e8SKurt Hackel {
17116714d8e8SKurt Hackel 	struct list_head *ret;
17126714d8e8SKurt Hackel 	BUG_ON(list_num < 0);
17136714d8e8SKurt Hackel 	BUG_ON(list_num > 2);
17146714d8e8SKurt Hackel 	ret = &(res->granted);
17156714d8e8SKurt Hackel 	ret += list_num;
17166714d8e8SKurt Hackel 	return ret;
17176714d8e8SKurt Hackel }
17186714d8e8SKurt Hackel /* TODO: do ast flush business
17196714d8e8SKurt Hackel  * TODO: do MIGRATING and RECOVERING spinning
17206714d8e8SKurt Hackel  */
17216714d8e8SKurt Hackel 
17226714d8e8SKurt Hackel /*
17236714d8e8SKurt Hackel * NOTE about in-flight requests during migration:
17246714d8e8SKurt Hackel *
17256714d8e8SKurt Hackel * Before attempting the migrate, the master has marked the lockres as
17266714d8e8SKurt Hackel * MIGRATING and then flushed all of its pending ASTS.  So any in-flight
17276714d8e8SKurt Hackel * requests either got queued before the MIGRATING flag got set, in which
17286714d8e8SKurt Hackel * case the lock data will reflect the change and a return message is on
17296714d8e8SKurt Hackel * the way, or the request failed to get in before MIGRATING got set.  In
17306714d8e8SKurt Hackel * this case, the caller will be told to spin and wait for the MIGRATING
17316714d8e8SKurt Hackel * flag to be dropped, then recheck the master.
17326714d8e8SKurt Hackel * This holds true for the convert, cancel and unlock cases, and since lvb
17336714d8e8SKurt Hackel * updates are tied to these same messages, it applies to lvb updates as
17346714d8e8SKurt Hackel * well.  For the lock case, there is no way a lock can be on the master
17356714d8e8SKurt Hackel * queue and not be on the secondary queue since the lock is always added
17366714d8e8SKurt Hackel * locally first.  This means that the new target node will never be sent
17376714d8e8SKurt Hackel * a lock that he doesn't already have on the list.
17386714d8e8SKurt Hackel * In total, this means that the local lock is correct and should not be
17396714d8e8SKurt Hackel * updated to match the one sent by the master.  Any messages sent back
17406714d8e8SKurt Hackel * from the master before the MIGRATING flag will bring the lock properly
17416714d8e8SKurt Hackel * up-to-date, and the change will be ordered properly for the waiter.
17426714d8e8SKurt Hackel * We will *not* attempt to modify the lock underneath the waiter.
17436714d8e8SKurt Hackel */
17446714d8e8SKurt Hackel 
17456714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
17466714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
17476714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres)
17486714d8e8SKurt Hackel {
17496714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
17506714d8e8SKurt Hackel 	struct list_head *queue;
1751e17e75ecSKurt Hackel 	struct list_head *tmpq = NULL;
17526714d8e8SKurt Hackel 	struct dlm_lock *newlock = NULL;
17536714d8e8SKurt Hackel 	struct dlm_lockstatus *lksb = NULL;
17546714d8e8SKurt Hackel 	int ret = 0;
1755e17e75ecSKurt Hackel 	int i, j, bad;
17566714d8e8SKurt Hackel 	struct dlm_lock *lock = NULL;
1757ba2bf218SKurt Hackel 	u8 from = O2NM_MAX_NODES;
1758ba2bf218SKurt Hackel 	unsigned int added = 0;
175926636bf6SSunil Mushran 	__be64 c;
17606714d8e8SKurt Hackel 
17616714d8e8SKurt Hackel 	mlog(0, "running %d locks for this lockres\n", mres->num_locks);
17626714d8e8SKurt Hackel 	for (i=0; i<mres->num_locks; i++) {
17636714d8e8SKurt Hackel 		ml = &(mres->ml[i]);
1764ba2bf218SKurt Hackel 
1765ba2bf218SKurt Hackel 		if (dlm_is_dummy_lock(dlm, ml, &from)) {
1766ba2bf218SKurt Hackel 			/* placeholder, just need to set the refmap bit */
1767ba2bf218SKurt Hackel 			BUG_ON(mres->num_locks != 1);
1768ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: dummy lock for %u\n",
1769ba2bf218SKurt Hackel 			     dlm->name, mres->lockname_len, mres->lockname,
1770ba2bf218SKurt Hackel 			     from);
1771ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
1772ba2bf218SKurt Hackel 			dlm_lockres_set_refmap_bit(from, res);
1773ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
1774ba2bf218SKurt Hackel 			added++;
1775ba2bf218SKurt Hackel 			break;
1776ba2bf218SKurt Hackel 		}
17776714d8e8SKurt Hackel 		BUG_ON(ml->highest_blocked != LKM_IVMODE);
17786714d8e8SKurt Hackel 		newlock = NULL;
17796714d8e8SKurt Hackel 		lksb = NULL;
17806714d8e8SKurt Hackel 
17816714d8e8SKurt Hackel 		queue = dlm_list_num_to_pointer(res, ml->list);
1782e17e75ecSKurt Hackel 		tmpq = NULL;
17836714d8e8SKurt Hackel 
17846714d8e8SKurt Hackel 		/* if the lock is for the local node it needs to
17856714d8e8SKurt Hackel 		 * be moved to the proper location within the queue.
17866714d8e8SKurt Hackel 		 * do not allocate a new lock structure. */
17876714d8e8SKurt Hackel 		if (ml->node == dlm->node_num) {
17886714d8e8SKurt Hackel 			/* MIGRATION ONLY! */
17896714d8e8SKurt Hackel 			BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
17906714d8e8SKurt Hackel 
17916714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
1792e17e75ecSKurt Hackel 			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
1793e17e75ecSKurt Hackel 				tmpq = dlm_list_idx_to_ptr(res, j);
1794800deef3SChristoph Hellwig 				list_for_each_entry(lock, tmpq, list) {
17956714d8e8SKurt Hackel 					if (lock->ml.cookie != ml->cookie)
17966714d8e8SKurt Hackel 						lock = NULL;
17976714d8e8SKurt Hackel 					else
17986714d8e8SKurt Hackel 						break;
17996714d8e8SKurt Hackel 				}
1800e17e75ecSKurt Hackel 				if (lock)
1801e17e75ecSKurt Hackel 					break;
1802e17e75ecSKurt Hackel 			}
18036714d8e8SKurt Hackel 
18046714d8e8SKurt Hackel 			/* lock is always created locally first, and
18056714d8e8SKurt Hackel 			 * destroyed locally last.  it must be on the list */
18066714d8e8SKurt Hackel 			if (!lock) {
180726636bf6SSunil Mushran 				c = ml->cookie;
180826636bf6SSunil Mushran 				mlog(ML_ERROR, "Could not find local lock "
180926636bf6SSunil Mushran 					       "with cookie %u:%llu, node %u, "
181026636bf6SSunil Mushran 					       "list %u, flags 0x%x, type %d, "
181126636bf6SSunil Mushran 					       "conv %d, highest blocked %d\n",
181274aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
181326636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
181426636bf6SSunil Mushran 				     ml->node, ml->list, ml->flags, ml->type,
181526636bf6SSunil Mushran 				     ml->convert_type, ml->highest_blocked);
181671ac1062SKurt Hackel 				__dlm_print_one_lock_resource(res);
18176714d8e8SKurt Hackel 				BUG();
18186714d8e8SKurt Hackel 			}
181926636bf6SSunil Mushran 
182026636bf6SSunil Mushran 			if (lock->ml.node != ml->node) {
182126636bf6SSunil Mushran 				c = lock->ml.cookie;
182226636bf6SSunil Mushran 				mlog(ML_ERROR, "Mismatched node# in lock "
182326636bf6SSunil Mushran 				     "cookie %u:%llu, name %.*s, node %u\n",
182426636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
182526636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
182626636bf6SSunil Mushran 				     res->lockname.len, res->lockname.name,
182726636bf6SSunil Mushran 				     lock->ml.node);
182826636bf6SSunil Mushran 				c = ml->cookie;
182926636bf6SSunil Mushran 				mlog(ML_ERROR, "Migrate lock cookie %u:%llu, "
183026636bf6SSunil Mushran 				     "node %u, list %u, flags 0x%x, type %d, "
183126636bf6SSunil Mushran 				     "conv %d, highest blocked %d\n",
183226636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
183326636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
183426636bf6SSunil Mushran 				     ml->node, ml->list, ml->flags, ml->type,
183526636bf6SSunil Mushran 				     ml->convert_type, ml->highest_blocked);
183626636bf6SSunil Mushran 				__dlm_print_one_lock_resource(res);
183726636bf6SSunil Mushran 				BUG();
183826636bf6SSunil Mushran 			}
18396714d8e8SKurt Hackel 
1840e17e75ecSKurt Hackel 			if (tmpq != queue) {
184126636bf6SSunil Mushran 				c = ml->cookie;
184226636bf6SSunil Mushran 				mlog(0, "Lock cookie %u:%llu was on list %u "
184326636bf6SSunil Mushran 				     "instead of list %u for %.*s\n",
184426636bf6SSunil Mushran 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
184526636bf6SSunil Mushran 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
184626636bf6SSunil Mushran 				     j, ml->list, res->lockname.len,
184726636bf6SSunil Mushran 				     res->lockname.name);
184826636bf6SSunil Mushran 				__dlm_print_one_lock_resource(res);
1849e17e75ecSKurt Hackel 				spin_unlock(&res->spinlock);
1850e17e75ecSKurt Hackel 				continue;
1851e17e75ecSKurt Hackel 			}
1852e17e75ecSKurt Hackel 
18536714d8e8SKurt Hackel 			/* see NOTE above about why we do not update
18546714d8e8SKurt Hackel 			 * to match the master here */
18556714d8e8SKurt Hackel 
18566714d8e8SKurt Hackel 			/* move the lock to its proper place */
18576714d8e8SKurt Hackel 			/* do not alter lock refcount.  switching lists. */
1858f116629dSAkinobu Mita 			list_move_tail(&lock->list, queue);
18596714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
1860ba2bf218SKurt Hackel 			added++;
18616714d8e8SKurt Hackel 
18626714d8e8SKurt Hackel 			mlog(0, "just reordered a local lock!\n");
18636714d8e8SKurt Hackel 			continue;
18646714d8e8SKurt Hackel 		}
18656714d8e8SKurt Hackel 
18666714d8e8SKurt Hackel 		/* lock is for another node. */
18676714d8e8SKurt Hackel 		newlock = dlm_new_lock(ml->type, ml->node,
18686714d8e8SKurt Hackel 				       be64_to_cpu(ml->cookie), NULL);
18696714d8e8SKurt Hackel 		if (!newlock) {
18706714d8e8SKurt Hackel 			ret = -ENOMEM;
18716714d8e8SKurt Hackel 			goto leave;
18726714d8e8SKurt Hackel 		}
18736714d8e8SKurt Hackel 		lksb = newlock->lksb;
18746714d8e8SKurt Hackel 		dlm_lock_attach_lockres(newlock, res);
18756714d8e8SKurt Hackel 
18766714d8e8SKurt Hackel 		if (ml->convert_type != LKM_IVMODE) {
18776714d8e8SKurt Hackel 			BUG_ON(queue != &res->converting);
18786714d8e8SKurt Hackel 			newlock->ml.convert_type = ml->convert_type;
18796714d8e8SKurt Hackel 		}
18806714d8e8SKurt Hackel 		lksb->flags |= (ml->flags &
18816714d8e8SKurt Hackel 				(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
18826714d8e8SKurt Hackel 
1883ccd8b1f9SKurt Hackel 		if (ml->type == LKM_NLMODE)
1884ccd8b1f9SKurt Hackel 			goto skip_lvb;
1885ccd8b1f9SKurt Hackel 
18868bc674cbSKurt Hackel 		if (!dlm_lvb_is_empty(mres->lvb)) {
18876714d8e8SKurt Hackel 			if (lksb->flags & DLM_LKSB_PUT_LVB) {
18886714d8e8SKurt Hackel 				/* other node was trying to update
18896714d8e8SKurt Hackel 				 * lvb when node died.  recreate the
18906714d8e8SKurt Hackel 				 * lksb with the updated lvb. */
18916714d8e8SKurt Hackel 				memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1892ccd8b1f9SKurt Hackel 				/* the lock resource lvb update must happen
1893ccd8b1f9SKurt Hackel 				 * NOW, before the spinlock is dropped.
1894ccd8b1f9SKurt Hackel 				 * we no longer wait for the AST to update
1895ccd8b1f9SKurt Hackel 				 * the lvb. */
1896ccd8b1f9SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
18976714d8e8SKurt Hackel 			} else {
18986714d8e8SKurt Hackel 				/* otherwise, the node is sending its
18996714d8e8SKurt Hackel 				 * most recent valid lvb info */
19006714d8e8SKurt Hackel 				BUG_ON(ml->type != LKM_EXMODE &&
19016714d8e8SKurt Hackel 				       ml->type != LKM_PRMODE);
19028bc674cbSKurt Hackel 				if (!dlm_lvb_is_empty(res->lvb) &&
19038bc674cbSKurt Hackel  				    (ml->type == LKM_EXMODE ||
19046714d8e8SKurt Hackel  				     memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1905343e26a4SKurt Hackel  					int i;
1906343e26a4SKurt Hackel  					mlog(ML_ERROR, "%s:%.*s: received bad "
1907343e26a4SKurt Hackel  					     "lvb! type=%d\n", dlm->name,
1908343e26a4SKurt Hackel  					     res->lockname.len,
1909343e26a4SKurt Hackel  					     res->lockname.name, ml->type);
1910343e26a4SKurt Hackel  					printk("lockres lvb=[");
1911343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1912343e26a4SKurt Hackel  						printk("%02x", res->lvb[i]);
1913343e26a4SKurt Hackel  					printk("]\nmigrated lvb=[");
1914343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1915343e26a4SKurt Hackel  						printk("%02x", mres->lvb[i]);
1916343e26a4SKurt Hackel  					printk("]\n");
1917343e26a4SKurt Hackel  					dlm_print_one_lock_resource(res);
19186714d8e8SKurt Hackel  					BUG();
19196714d8e8SKurt Hackel 				}
19206714d8e8SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
19216714d8e8SKurt Hackel 			}
19226714d8e8SKurt Hackel 		}
1923ccd8b1f9SKurt Hackel skip_lvb:
19246714d8e8SKurt Hackel 
19256714d8e8SKurt Hackel 		/* NOTE:
19266714d8e8SKurt Hackel 		 * wrt lock queue ordering and recovery:
19276714d8e8SKurt Hackel 		 *    1. order of locks on granted queue is
19286714d8e8SKurt Hackel 		 *       meaningless.
19296714d8e8SKurt Hackel 		 *    2. order of locks on converting queue is
19306714d8e8SKurt Hackel 		 *       LOST with the node death.  sorry charlie.
19316714d8e8SKurt Hackel 		 *    3. order of locks on the blocked queue is
19326714d8e8SKurt Hackel 		 *       also LOST.
19336714d8e8SKurt Hackel 		 * order of locks does not affect integrity, it
19346714d8e8SKurt Hackel 		 * just means that a lock request may get pushed
19356714d8e8SKurt Hackel 		 * back in line as a result of the node death.
19366714d8e8SKurt Hackel 		 * also note that for a given node the lock order
19376714d8e8SKurt Hackel 		 * for its secondary queue locks is preserved
19386714d8e8SKurt Hackel 		 * relative to each other, but clearly *not*
19396714d8e8SKurt Hackel 		 * preserved relative to locks from other nodes.
19406714d8e8SKurt Hackel 		 */
1941c3187ce5SKurt Hackel 		bad = 0;
19426714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
1943c3187ce5SKurt Hackel 		list_for_each_entry(lock, queue, list) {
1944c3187ce5SKurt Hackel 			if (lock->ml.cookie == ml->cookie) {
194526636bf6SSunil Mushran 				c = lock->ml.cookie;
1946c3187ce5SKurt Hackel 				mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1947c3187ce5SKurt Hackel 				     "exists on this lockres!\n", dlm->name,
1948c3187ce5SKurt Hackel 				     res->lockname.len, res->lockname.name,
194974aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
195074aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)));
1951c3187ce5SKurt Hackel 
1952c3187ce5SKurt Hackel 				mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1953c3187ce5SKurt Hackel 				     "node=%u, cookie=%u:%llu, queue=%d\n",
1954c3187ce5SKurt Hackel 	      			     ml->type, ml->convert_type, ml->node,
195574aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
195674aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
1957c3187ce5SKurt Hackel 				     ml->list);
1958c3187ce5SKurt Hackel 
1959c3187ce5SKurt Hackel 				__dlm_print_one_lock_resource(res);
1960c3187ce5SKurt Hackel 				bad = 1;
1961c3187ce5SKurt Hackel 				break;
1962c3187ce5SKurt Hackel 			}
1963c3187ce5SKurt Hackel 		}
1964c3187ce5SKurt Hackel 		if (!bad) {
19656714d8e8SKurt Hackel 			dlm_lock_get(newlock);
19666714d8e8SKurt Hackel 			list_add_tail(&newlock->list, queue);
1967ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: added lock for node %u, "
1968ba2bf218SKurt Hackel 			     "setting refmap bit\n", dlm->name,
1969ba2bf218SKurt Hackel 			     res->lockname.len, res->lockname.name, ml->node);
1970ba2bf218SKurt Hackel 			dlm_lockres_set_refmap_bit(ml->node, res);
1971ba2bf218SKurt Hackel 			added++;
1972c3187ce5SKurt Hackel 		}
19736714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
19746714d8e8SKurt Hackel 	}
19756714d8e8SKurt Hackel 	mlog(0, "done running all the locks\n");
19766714d8e8SKurt Hackel 
19776714d8e8SKurt Hackel leave:
1978ba2bf218SKurt Hackel 	/* balance the ref taken when the work was queued */
1979ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
1980ba2bf218SKurt Hackel 	dlm_lockres_drop_inflight_ref(dlm, res);
1981ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
1982ba2bf218SKurt Hackel 
19836714d8e8SKurt Hackel 	if (ret < 0) {
19846714d8e8SKurt Hackel 		mlog_errno(ret);
19856714d8e8SKurt Hackel 		if (newlock)
19866714d8e8SKurt Hackel 			dlm_lock_put(newlock);
19876714d8e8SKurt Hackel 	}
19886714d8e8SKurt Hackel 
19896714d8e8SKurt Hackel 	mlog_exit(ret);
19906714d8e8SKurt Hackel 	return ret;
19916714d8e8SKurt Hackel }
19926714d8e8SKurt Hackel 
19936714d8e8SKurt Hackel void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
19946714d8e8SKurt Hackel 				       struct dlm_lock_resource *res)
19956714d8e8SKurt Hackel {
19966714d8e8SKurt Hackel 	int i;
1997800deef3SChristoph Hellwig 	struct list_head *queue;
1998800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
19996714d8e8SKurt Hackel 
20006714d8e8SKurt Hackel 	res->state |= DLM_LOCK_RES_RECOVERING;
200169d72b06SKurt Hackel 	if (!list_empty(&res->recovering)) {
200269d72b06SKurt Hackel 		mlog(0,
200369d72b06SKurt Hackel 		     "Recovering res %s:%.*s, is already on recovery list!\n",
200469d72b06SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
20056714d8e8SKurt Hackel 		list_del_init(&res->recovering);
200652987e2aSSunil Mushran 		dlm_lockres_put(res);
200769d72b06SKurt Hackel 	}
200869d72b06SKurt Hackel 	/* We need to hold a reference while on the recovery list */
200969d72b06SKurt Hackel 	dlm_lockres_get(res);
20106714d8e8SKurt Hackel 	list_add_tail(&res->recovering, &dlm->reco.resources);
20116714d8e8SKurt Hackel 
20126714d8e8SKurt Hackel 	/* find any pending locks and put them back on proper list */
20136714d8e8SKurt Hackel 	for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
20146714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
2015800deef3SChristoph Hellwig 		list_for_each_entry_safe(lock, next, queue, list) {
20166714d8e8SKurt Hackel 			dlm_lock_get(lock);
20176714d8e8SKurt Hackel 			if (lock->convert_pending) {
20186714d8e8SKurt Hackel 				/* move converting lock back to granted */
20196714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
20206714d8e8SKurt Hackel 				mlog(0, "node died with convert pending "
20216714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
20226714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20236714d8e8SKurt Hackel 				dlm_revert_pending_convert(res, lock);
20246714d8e8SKurt Hackel 				lock->convert_pending = 0;
20256714d8e8SKurt Hackel 			} else if (lock->lock_pending) {
20266714d8e8SKurt Hackel 				/* remove pending lock requests completely */
20276714d8e8SKurt Hackel 				BUG_ON(i != DLM_BLOCKED_LIST);
20286714d8e8SKurt Hackel 				mlog(0, "node died with lock pending "
20296714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
20306714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20316714d8e8SKurt Hackel 				/* lock will be floating until ref in
20326714d8e8SKurt Hackel 				 * dlmlock_remote is freed after the network
20336714d8e8SKurt Hackel 				 * call returns.  ok for it to not be on any
20346714d8e8SKurt Hackel 				 * list since no ast can be called
20356714d8e8SKurt Hackel 				 * (the master is dead). */
20366714d8e8SKurt Hackel 				dlm_revert_pending_lock(res, lock);
20376714d8e8SKurt Hackel 				lock->lock_pending = 0;
20386714d8e8SKurt Hackel 			} else if (lock->unlock_pending) {
20396714d8e8SKurt Hackel 				/* if an unlock was in progress, treat as
20406714d8e8SKurt Hackel 				 * if this had completed successfully
20416714d8e8SKurt Hackel 				 * before sending this lock state to the
20426714d8e8SKurt Hackel 				 * new master.  note that the dlm_unlock
20436714d8e8SKurt Hackel 				 * call is still responsible for calling
20446714d8e8SKurt Hackel 				 * the unlockast.  that will happen after
20456714d8e8SKurt Hackel 				 * the network call times out.  for now,
20466714d8e8SKurt Hackel 				 * just move lists to prepare the new
20476714d8e8SKurt Hackel 				 * recovery master.  */
20486714d8e8SKurt Hackel 				BUG_ON(i != DLM_GRANTED_LIST);
20496714d8e8SKurt Hackel 				mlog(0, "node died with unlock pending "
20506714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
20516714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20526714d8e8SKurt Hackel 				dlm_commit_pending_unlock(res, lock);
20536714d8e8SKurt Hackel 				lock->unlock_pending = 0;
20546714d8e8SKurt Hackel 			} else if (lock->cancel_pending) {
20556714d8e8SKurt Hackel 				/* if a cancel was in progress, treat as
20566714d8e8SKurt Hackel 				 * if this had completed successfully
20576714d8e8SKurt Hackel 				 * before sending this lock state to the
20586714d8e8SKurt Hackel 				 * new master */
20596714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
20606714d8e8SKurt Hackel 				mlog(0, "node died with cancel pending "
20616714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
20626714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
20636714d8e8SKurt Hackel 				dlm_commit_pending_cancel(res, lock);
20646714d8e8SKurt Hackel 				lock->cancel_pending = 0;
20656714d8e8SKurt Hackel 			}
20666714d8e8SKurt Hackel 			dlm_lock_put(lock);
20676714d8e8SKurt Hackel 		}
20686714d8e8SKurt Hackel 	}
20696714d8e8SKurt Hackel }
20706714d8e8SKurt Hackel 
20716714d8e8SKurt Hackel 
20726714d8e8SKurt Hackel 
20736714d8e8SKurt Hackel /* removes all recovered locks from the recovery list.
20746714d8e8SKurt Hackel  * sets the res->owner to the new master.
20756714d8e8SKurt Hackel  * unsets the RECOVERY flag and wakes waiters. */
20766714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
20776714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master)
20786714d8e8SKurt Hackel {
20796714d8e8SKurt Hackel 	int i;
208081f2094aSMark Fasheh 	struct hlist_node *hash_iter;
208181f2094aSMark Fasheh 	struct hlist_head *bucket;
2082800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
20836714d8e8SKurt Hackel 
20846714d8e8SKurt Hackel 	mlog_entry_void();
20856714d8e8SKurt Hackel 
20866714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
20876714d8e8SKurt Hackel 
2088800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
20896714d8e8SKurt Hackel 		if (res->owner == dead_node) {
20906714d8e8SKurt Hackel 			list_del_init(&res->recovering);
20916714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
2092ba2bf218SKurt Hackel 			/* new_master has our reference from
2093ba2bf218SKurt Hackel 			 * the lock state sent during recovery */
20946714d8e8SKurt Hackel 			dlm_change_lockres_owner(dlm, res, new_master);
20956714d8e8SKurt Hackel 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2096ba2bf218SKurt Hackel 			if (__dlm_lockres_has_locks(res))
20976714d8e8SKurt Hackel 				__dlm_dirty_lockres(dlm, res);
20986714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
20996714d8e8SKurt Hackel 			wake_up(&res->wq);
210069d72b06SKurt Hackel 			dlm_lockres_put(res);
21016714d8e8SKurt Hackel 		}
21026714d8e8SKurt Hackel 	}
21036714d8e8SKurt Hackel 
21046714d8e8SKurt Hackel 	/* this will become unnecessary eventually, but
21056714d8e8SKurt Hackel 	 * for now we need to run the whole hash, clear
21066714d8e8SKurt Hackel 	 * the RECOVERING state and set the owner
21076714d8e8SKurt Hackel 	 * if necessary */
210881f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
210903d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
211081f2094aSMark Fasheh 		hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
21116714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_RECOVERING) {
21126714d8e8SKurt Hackel 				if (res->owner == dead_node) {
21136714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
21146714d8e8SKurt Hackel 					     "was not on recovering list, but "
21156714d8e8SKurt Hackel 					     "clearing state anyway\n",
21166714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
21176714d8e8SKurt Hackel 					     res->lockname.name, new_master);
21186714d8e8SKurt Hackel 				} else if (res->owner == dlm->node_num) {
21196714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
21206714d8e8SKurt Hackel 					     "was not on recovering list, "
21216714d8e8SKurt Hackel 					     "owner is THIS node, clearing\n",
21226714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
21236714d8e8SKurt Hackel 					     res->lockname.name, new_master);
21246714d8e8SKurt Hackel 				} else
21256714d8e8SKurt Hackel 					continue;
21266714d8e8SKurt Hackel 
2127c03872f5SKurt Hackel 				if (!list_empty(&res->recovering)) {
2128c03872f5SKurt Hackel 					mlog(0, "%s:%.*s: lockres was "
2129c03872f5SKurt Hackel 					     "marked RECOVERING, owner=%u\n",
2130c03872f5SKurt Hackel 					     dlm->name, res->lockname.len,
2131c03872f5SKurt Hackel 					     res->lockname.name, res->owner);
2132c03872f5SKurt Hackel 					list_del_init(&res->recovering);
213369d72b06SKurt Hackel 					dlm_lockres_put(res);
2134c03872f5SKurt Hackel 				}
21356714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
2136ba2bf218SKurt Hackel 				/* new_master has our reference from
2137ba2bf218SKurt Hackel 				 * the lock state sent during recovery */
21386714d8e8SKurt Hackel 				dlm_change_lockres_owner(dlm, res, new_master);
21396714d8e8SKurt Hackel 				res->state &= ~DLM_LOCK_RES_RECOVERING;
2140ba2bf218SKurt Hackel 				if (__dlm_lockres_has_locks(res))
21416714d8e8SKurt Hackel 					__dlm_dirty_lockres(dlm, res);
21426714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
21436714d8e8SKurt Hackel 				wake_up(&res->wq);
21446714d8e8SKurt Hackel 			}
21456714d8e8SKurt Hackel 		}
21466714d8e8SKurt Hackel 	}
21476714d8e8SKurt Hackel }
21486714d8e8SKurt Hackel 
21496714d8e8SKurt Hackel static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
21506714d8e8SKurt Hackel {
21516714d8e8SKurt Hackel 	if (local) {
21526714d8e8SKurt Hackel 		if (lock->ml.type != LKM_EXMODE &&
21536714d8e8SKurt Hackel 		    lock->ml.type != LKM_PRMODE)
21546714d8e8SKurt Hackel 			return 1;
21556714d8e8SKurt Hackel 	} else if (lock->ml.type == LKM_EXMODE)
21566714d8e8SKurt Hackel 		return 1;
21576714d8e8SKurt Hackel 	return 0;
21586714d8e8SKurt Hackel }
21596714d8e8SKurt Hackel 
21606714d8e8SKurt Hackel static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
21616714d8e8SKurt Hackel 			       struct dlm_lock_resource *res, u8 dead_node)
21626714d8e8SKurt Hackel {
2163800deef3SChristoph Hellwig 	struct list_head *queue;
21646714d8e8SKurt Hackel 	struct dlm_lock *lock;
21656714d8e8SKurt Hackel 	int blank_lvb = 0, local = 0;
21666714d8e8SKurt Hackel 	int i;
21676714d8e8SKurt Hackel 	u8 search_node;
21686714d8e8SKurt Hackel 
21696714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
21706714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
21716714d8e8SKurt Hackel 
21726714d8e8SKurt Hackel 	if (res->owner == dlm->node_num)
21736714d8e8SKurt Hackel 		/* if this node owned the lockres, and if the dead node
21746714d8e8SKurt Hackel 		 * had an EX when he died, blank out the lvb */
21756714d8e8SKurt Hackel 		search_node = dead_node;
21766714d8e8SKurt Hackel 	else {
21776714d8e8SKurt Hackel 		/* if this is a secondary lockres, and we had no EX or PR
21786714d8e8SKurt Hackel 		 * locks granted, we can no longer trust the lvb */
21796714d8e8SKurt Hackel 		search_node = dlm->node_num;
21806714d8e8SKurt Hackel 		local = 1;  /* check local state for valid lvb */
21816714d8e8SKurt Hackel 	}
21826714d8e8SKurt Hackel 
21836714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
21846714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
2185800deef3SChristoph Hellwig 		list_for_each_entry(lock, queue, list) {
21866714d8e8SKurt Hackel 			if (lock->ml.node == search_node) {
21876714d8e8SKurt Hackel 				if (dlm_lvb_needs_invalidation(lock, local)) {
21886714d8e8SKurt Hackel 					/* zero the lksb lvb and lockres lvb */
21896714d8e8SKurt Hackel 					blank_lvb = 1;
21906714d8e8SKurt Hackel 					memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
21916714d8e8SKurt Hackel 				}
21926714d8e8SKurt Hackel 			}
21936714d8e8SKurt Hackel 		}
21946714d8e8SKurt Hackel 	}
21956714d8e8SKurt Hackel 
21966714d8e8SKurt Hackel 	if (blank_lvb) {
21976714d8e8SKurt Hackel 		mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
21986714d8e8SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
21996714d8e8SKurt Hackel 		memset(res->lvb, 0, DLM_LVB_LEN);
22006714d8e8SKurt Hackel 	}
22016714d8e8SKurt Hackel }
22026714d8e8SKurt Hackel 
22036714d8e8SKurt Hackel static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
22046714d8e8SKurt Hackel 				struct dlm_lock_resource *res, u8 dead_node)
22056714d8e8SKurt Hackel {
2206800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
2207ba2bf218SKurt Hackel 	unsigned int freed = 0;
22086714d8e8SKurt Hackel 
22096714d8e8SKurt Hackel 	/* this node is the lockres master:
22106714d8e8SKurt Hackel 	 * 1) remove any stale locks for the dead node
22116714d8e8SKurt Hackel 	 * 2) if the dead node had an EX when he died, blank out the lvb
22126714d8e8SKurt Hackel 	 */
22136714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
22146714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
22156714d8e8SKurt Hackel 
22162c5c54acSSunil Mushran 	/* We do two dlm_lock_put(). One for removing from list and the other is
22172c5c54acSSunil Mushran 	 * to force the DLM_UNLOCK_FREE_LOCK action so as to free the locks */
22182c5c54acSSunil Mushran 
22196714d8e8SKurt Hackel 	/* TODO: check pending_asts, pending_basts here */
2220800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->granted, list) {
22216714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22226714d8e8SKurt Hackel 			list_del_init(&lock->list);
22236714d8e8SKurt Hackel 			dlm_lock_put(lock);
22242c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22252c5c54acSSunil Mushran 			dlm_lock_put(lock);
2226ba2bf218SKurt Hackel 			freed++;
22276714d8e8SKurt Hackel 		}
22286714d8e8SKurt Hackel 	}
2229800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->converting, list) {
22306714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22316714d8e8SKurt Hackel 			list_del_init(&lock->list);
22326714d8e8SKurt Hackel 			dlm_lock_put(lock);
22332c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22342c5c54acSSunil Mushran 			dlm_lock_put(lock);
2235ba2bf218SKurt Hackel 			freed++;
22366714d8e8SKurt Hackel 		}
22376714d8e8SKurt Hackel 	}
2238800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->blocked, list) {
22396714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
22406714d8e8SKurt Hackel 			list_del_init(&lock->list);
22416714d8e8SKurt Hackel 			dlm_lock_put(lock);
22422c5c54acSSunil Mushran 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
22432c5c54acSSunil Mushran 			dlm_lock_put(lock);
2244ba2bf218SKurt Hackel 			freed++;
22456714d8e8SKurt Hackel 		}
22466714d8e8SKurt Hackel 	}
22476714d8e8SKurt Hackel 
2248ba2bf218SKurt Hackel 	if (freed) {
2249ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2250ba2bf218SKurt Hackel 		     "dropping ref from lockres\n", dlm->name,
2251ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, freed, dead_node);
2252cda70ba8SSunil Mushran 		if(!test_bit(dead_node, res->refmap)) {
2253cda70ba8SSunil Mushran 			mlog(ML_ERROR, "%s:%.*s: freed %u locks for dead node %u, "
2254cda70ba8SSunil Mushran 			     "but ref was not set\n", dlm->name,
2255cda70ba8SSunil Mushran 			     res->lockname.len, res->lockname.name, freed, dead_node);
2256cda70ba8SSunil Mushran 			__dlm_print_one_lock_resource(res);
2257cda70ba8SSunil Mushran 		}
2258ba2bf218SKurt Hackel 		dlm_lockres_clear_refmap_bit(dead_node, res);
2259ba2bf218SKurt Hackel 	} else if (test_bit(dead_node, res->refmap)) {
2260ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2261ba2bf218SKurt Hackel 		     "no locks and had not purged before dying\n", dlm->name,
2262ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
2263ba2bf218SKurt Hackel 		dlm_lockres_clear_refmap_bit(dead_node, res);
2264ba2bf218SKurt Hackel 	}
2265ba2bf218SKurt Hackel 
22666714d8e8SKurt Hackel 	/* do not kick thread yet */
22676714d8e8SKurt Hackel 	__dlm_dirty_lockres(dlm, res);
22686714d8e8SKurt Hackel }
22696714d8e8SKurt Hackel 
22706714d8e8SKurt Hackel /* if this node is the recovery master, and there are no
22716714d8e8SKurt Hackel  * locks for a given lockres owned by this node that are in
22726714d8e8SKurt Hackel  * either PR or EX mode, zero out the lvb before requesting.
22736714d8e8SKurt Hackel  *
22746714d8e8SKurt Hackel  */
22756714d8e8SKurt Hackel 
22766714d8e8SKurt Hackel 
22776714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
22786714d8e8SKurt Hackel {
227981f2094aSMark Fasheh 	struct hlist_node *iter;
22806714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
22816714d8e8SKurt Hackel 	int i;
228281f2094aSMark Fasheh 	struct hlist_head *bucket;
2283e2faea4cSKurt Hackel 	struct dlm_lock *lock;
22846714d8e8SKurt Hackel 
22856714d8e8SKurt Hackel 
22866714d8e8SKurt Hackel 	/* purge any stale mles */
22876714d8e8SKurt Hackel 	dlm_clean_master_list(dlm, dead_node);
22886714d8e8SKurt Hackel 
22896714d8e8SKurt Hackel 	/*
22906714d8e8SKurt Hackel 	 * now clean up all lock resources.  there are two rules:
22916714d8e8SKurt Hackel 	 *
22926714d8e8SKurt Hackel 	 * 1) if the dead node was the master, move the lockres
22936714d8e8SKurt Hackel 	 *    to the recovering list.  set the RECOVERING flag.
22946714d8e8SKurt Hackel 	 *    this lockres needs to be cleaned up before it can
22956714d8e8SKurt Hackel 	 *    be used further.
22966714d8e8SKurt Hackel 	 *
22976714d8e8SKurt Hackel 	 * 2) if this node was the master, remove all locks from
22986714d8e8SKurt Hackel 	 *    each of the lockres queues that were owned by the
22996714d8e8SKurt Hackel 	 *    dead node.  once recovery finishes, the dlm thread
23006714d8e8SKurt Hackel 	 *    can be kicked again to see if any ASTs or BASTs
23016714d8e8SKurt Hackel 	 *    need to be fired as a result.
23026714d8e8SKurt Hackel 	 */
230381f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
230403d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
230581f2094aSMark Fasheh 		hlist_for_each_entry(res, iter, bucket, hash_node) {
2306e2faea4cSKurt Hackel  			/* always prune any $RECOVERY entries for dead nodes,
2307e2faea4cSKurt Hackel  			 * otherwise hangs can occur during later recovery */
23086714d8e8SKurt Hackel 			if (dlm_is_recovery_lock(res->lockname.name,
2309e2faea4cSKurt Hackel 						 res->lockname.len)) {
2310e2faea4cSKurt Hackel 				spin_lock(&res->spinlock);
2311e2faea4cSKurt Hackel 				list_for_each_entry(lock, &res->granted, list) {
2312e2faea4cSKurt Hackel 					if (lock->ml.node == dead_node) {
2313e2faea4cSKurt Hackel 						mlog(0, "AHA! there was "
2314e2faea4cSKurt Hackel 						     "a $RECOVERY lock for dead "
2315e2faea4cSKurt Hackel 						     "node %u (%s)!\n",
2316e2faea4cSKurt Hackel 						     dead_node, dlm->name);
2317e2faea4cSKurt Hackel 						list_del_init(&lock->list);
2318e2faea4cSKurt Hackel 						dlm_lock_put(lock);
2319e2faea4cSKurt Hackel 						break;
2320e2faea4cSKurt Hackel 					}
2321e2faea4cSKurt Hackel 				}
2322e2faea4cSKurt Hackel 				spin_unlock(&res->spinlock);
23236714d8e8SKurt Hackel 				continue;
2324e2faea4cSKurt Hackel 			}
23256714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
23266714d8e8SKurt Hackel 			/* zero the lvb if necessary */
23276714d8e8SKurt Hackel 			dlm_revalidate_lvb(dlm, res, dead_node);
2328ba2bf218SKurt Hackel 			if (res->owner == dead_node) {
2329ba2bf218SKurt Hackel 				if (res->state & DLM_LOCK_RES_DROPPING_REF)
2330ba2bf218SKurt Hackel 					mlog(0, "%s:%.*s: owned by "
2331ba2bf218SKurt Hackel 					     "dead node %u, this node was "
2332ba2bf218SKurt Hackel 					     "dropping its ref when it died. "
2333ba2bf218SKurt Hackel 					     "continue, dropping the flag.\n",
2334ba2bf218SKurt Hackel 					     dlm->name, res->lockname.len,
2335ba2bf218SKurt Hackel 					     res->lockname.name, dead_node);
2336ba2bf218SKurt Hackel 
2337ba2bf218SKurt Hackel 				/* the wake_up for this will happen when the
2338ba2bf218SKurt Hackel 				 * RECOVERING flag is dropped later */
2339ba2bf218SKurt Hackel 				res->state &= ~DLM_LOCK_RES_DROPPING_REF;
2340ba2bf218SKurt Hackel 
23416714d8e8SKurt Hackel 				dlm_move_lockres_to_recovery_list(dlm, res);
2342ba2bf218SKurt Hackel 			} else if (res->owner == dlm->node_num) {
23436714d8e8SKurt Hackel 				dlm_free_dead_locks(dlm, res, dead_node);
23446714d8e8SKurt Hackel 				__dlm_lockres_calc_usage(dlm, res);
23456714d8e8SKurt Hackel 			}
23466714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
23476714d8e8SKurt Hackel 		}
23486714d8e8SKurt Hackel 	}
23496714d8e8SKurt Hackel 
23506714d8e8SKurt Hackel }
23516714d8e8SKurt Hackel 
23526714d8e8SKurt Hackel static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
23536714d8e8SKurt Hackel {
23546714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
23556714d8e8SKurt Hackel 
2356466d1a45SKurt Hackel 	if (dlm->reco.new_master == idx) {
2357466d1a45SKurt Hackel 		mlog(0, "%s: recovery master %d just died\n",
2358466d1a45SKurt Hackel 		     dlm->name, idx);
2359466d1a45SKurt Hackel 		if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2360466d1a45SKurt Hackel 			/* finalize1 was reached, so it is safe to clear
2361466d1a45SKurt Hackel 			 * the new_master and dead_node.  that recovery
2362466d1a45SKurt Hackel 			 * is complete. */
2363466d1a45SKurt Hackel 			mlog(0, "%s: dead master %d had reached "
2364466d1a45SKurt Hackel 			     "finalize1 state, clearing\n", dlm->name, idx);
2365466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2366466d1a45SKurt Hackel 			__dlm_reset_recovery(dlm);
2367466d1a45SKurt Hackel 		}
2368466d1a45SKurt Hackel 	}
2369466d1a45SKurt Hackel 
23702d4b1cbbSTao Ma 	/* Clean up join state on node death. */
23712d4b1cbbSTao Ma 	if (dlm->joining_node == idx) {
23722d4b1cbbSTao Ma 		mlog(0, "Clearing join state for node %u\n", idx);
23732d4b1cbbSTao Ma 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
23742d4b1cbbSTao Ma 	}
23752d4b1cbbSTao Ma 
23766714d8e8SKurt Hackel 	/* check to see if the node is already considered dead */
23776714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->live_nodes_map)) {
23786714d8e8SKurt Hackel 		mlog(0, "for domain %s, node %d is already dead. "
23796714d8e8SKurt Hackel 		     "another node likely did recovery already.\n",
23806714d8e8SKurt Hackel 		     dlm->name, idx);
23816714d8e8SKurt Hackel 		return;
23826714d8e8SKurt Hackel 	}
23836714d8e8SKurt Hackel 
23846714d8e8SKurt Hackel 	/* check to see if we do not care about this node */
23856714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->domain_map)) {
23866714d8e8SKurt Hackel 		/* This also catches the case that we get a node down
23876714d8e8SKurt Hackel 		 * but haven't joined the domain yet. */
23886714d8e8SKurt Hackel 		mlog(0, "node %u already removed from domain!\n", idx);
23896714d8e8SKurt Hackel 		return;
23906714d8e8SKurt Hackel 	}
23916714d8e8SKurt Hackel 
23926714d8e8SKurt Hackel 	clear_bit(idx, dlm->live_nodes_map);
23936714d8e8SKurt Hackel 
23946714d8e8SKurt Hackel 	/* make sure local cleanup occurs before the heartbeat events */
23956714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->recovery_map))
23966714d8e8SKurt Hackel 		dlm_do_local_recovery_cleanup(dlm, idx);
23976714d8e8SKurt Hackel 
23986714d8e8SKurt Hackel 	/* notify anything attached to the heartbeat events */
23996714d8e8SKurt Hackel 	dlm_hb_event_notify_attached(dlm, idx, 0);
24006714d8e8SKurt Hackel 
24016714d8e8SKurt Hackel 	mlog(0, "node %u being removed from domain map!\n", idx);
24026714d8e8SKurt Hackel 	clear_bit(idx, dlm->domain_map);
24036714d8e8SKurt Hackel 	/* wake up migration waiters if a node goes down.
24046714d8e8SKurt Hackel 	 * perhaps later we can genericize this for other waiters. */
24056714d8e8SKurt Hackel 	wake_up(&dlm->migration_wq);
24066714d8e8SKurt Hackel 
24076714d8e8SKurt Hackel 	if (test_bit(idx, dlm->recovery_map))
24086714d8e8SKurt Hackel 		mlog(0, "domain %s, node %u already added "
24096714d8e8SKurt Hackel 		     "to recovery map!\n", dlm->name, idx);
24106714d8e8SKurt Hackel 	else
24116714d8e8SKurt Hackel 		set_bit(idx, dlm->recovery_map);
24126714d8e8SKurt Hackel }
24136714d8e8SKurt Hackel 
24146714d8e8SKurt Hackel void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
24156714d8e8SKurt Hackel {
24166714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
24176714d8e8SKurt Hackel 
24186714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
24196714d8e8SKurt Hackel 		return;
24206714d8e8SKurt Hackel 
24216561168cSMark Fasheh 	/*
24226561168cSMark Fasheh 	 * This will notify any dlm users that a node in our domain
24236561168cSMark Fasheh 	 * went away without notifying us first.
24246561168cSMark Fasheh 	 */
24256561168cSMark Fasheh 	if (test_bit(idx, dlm->domain_map))
24266561168cSMark Fasheh 		dlm_fire_domain_eviction_callbacks(dlm, idx);
24276561168cSMark Fasheh 
24286714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
24296714d8e8SKurt Hackel 	__dlm_hb_node_down(dlm, idx);
24306714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
24316714d8e8SKurt Hackel 
24326714d8e8SKurt Hackel 	dlm_put(dlm);
24336714d8e8SKurt Hackel }
24346714d8e8SKurt Hackel 
24356714d8e8SKurt Hackel void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
24366714d8e8SKurt Hackel {
24376714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
24386714d8e8SKurt Hackel 
24396714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
24406714d8e8SKurt Hackel 		return;
24416714d8e8SKurt Hackel 
24426714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
24436714d8e8SKurt Hackel 	set_bit(idx, dlm->live_nodes_map);
2444e2faea4cSKurt Hackel 	/* do NOT notify mle attached to the heartbeat events.
2445e2faea4cSKurt Hackel 	 * new nodes are not interesting in mastery until joined. */
24466714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
24476714d8e8SKurt Hackel 
24486714d8e8SKurt Hackel 	dlm_put(dlm);
24496714d8e8SKurt Hackel }
24506714d8e8SKurt Hackel 
24516714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata)
24526714d8e8SKurt Hackel {
24536714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
24546714d8e8SKurt Hackel 	mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
24556714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
24566714d8e8SKurt Hackel }
24576714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type)
24586714d8e8SKurt Hackel {
24596714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
24606714d8e8SKurt Hackel 	mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
24616714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
24626714d8e8SKurt Hackel }
24636714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
24646714d8e8SKurt Hackel {
24656714d8e8SKurt Hackel 	mlog(0, "unlockast for recovery lock fired!\n");
24666714d8e8SKurt Hackel }
24676714d8e8SKurt Hackel 
2468e2faea4cSKurt Hackel /*
2469e2faea4cSKurt Hackel  * dlm_pick_recovery_master will continually attempt to use
2470e2faea4cSKurt Hackel  * dlmlock() on the special "$RECOVERY" lockres with the
2471e2faea4cSKurt Hackel  * LKM_NOQUEUE flag to get an EX.  every thread that enters
2472e2faea4cSKurt Hackel  * this function on each node racing to become the recovery
2473e2faea4cSKurt Hackel  * master will not stop attempting this until either:
2474e2faea4cSKurt Hackel  * a) this node gets the EX (and becomes the recovery master),
2475e2faea4cSKurt Hackel  * or b) dlm->reco.new_master gets set to some nodenum
2476e2faea4cSKurt Hackel  * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2477e2faea4cSKurt Hackel  * so each time a recovery master is needed, the entire cluster
2478e2faea4cSKurt Hackel  * will sync at this point.  if the new master dies, that will
2479e2faea4cSKurt Hackel  * be detected in dlm_do_recovery */
24806714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
24816714d8e8SKurt Hackel {
24826714d8e8SKurt Hackel 	enum dlm_status ret;
24836714d8e8SKurt Hackel 	struct dlm_lockstatus lksb;
24846714d8e8SKurt Hackel 	int status = -EINVAL;
24856714d8e8SKurt Hackel 
24866714d8e8SKurt Hackel 	mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
24876714d8e8SKurt Hackel 	     dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2488e2faea4cSKurt Hackel again:
24896714d8e8SKurt Hackel 	memset(&lksb, 0, sizeof(lksb));
24906714d8e8SKurt Hackel 
24916714d8e8SKurt Hackel 	ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
24923384f3dfSMark Fasheh 		      DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
24933384f3dfSMark Fasheh 		      dlm_reco_ast, dlm, dlm_reco_bast);
24946714d8e8SKurt Hackel 
2495e2faea4cSKurt Hackel 	mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2496e2faea4cSKurt Hackel 	     dlm->name, ret, lksb.status);
2497e2faea4cSKurt Hackel 
24986714d8e8SKurt Hackel 	if (ret == DLM_NORMAL) {
24996714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
25006714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
2501e2faea4cSKurt Hackel 
2502e2faea4cSKurt Hackel 		/* got the EX lock.  check to see if another node
2503e2faea4cSKurt Hackel 		 * just became the reco master */
2504e2faea4cSKurt Hackel 		if (dlm_reco_master_ready(dlm)) {
2505e2faea4cSKurt Hackel 			mlog(0, "%s: got reco EX lock, but %u will "
2506e2faea4cSKurt Hackel 			     "do the recovery\n", dlm->name,
2507e2faea4cSKurt Hackel 			     dlm->reco.new_master);
2508e2faea4cSKurt Hackel 			status = -EEXIST;
2509e2faea4cSKurt Hackel 		} else {
2510898effacSKurt Hackel 			status = 0;
2511898effacSKurt Hackel 
2512898effacSKurt Hackel 			/* see if recovery was already finished elsewhere */
2513898effacSKurt Hackel 			spin_lock(&dlm->spinlock);
2514898effacSKurt Hackel 			if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2515898effacSKurt Hackel 				status = -EINVAL;
2516898effacSKurt Hackel 				mlog(0, "%s: got reco EX lock, but "
2517898effacSKurt Hackel 				     "node got recovered already\n", dlm->name);
2518898effacSKurt Hackel 				if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2519898effacSKurt Hackel 					mlog(ML_ERROR, "%s: new master is %u "
2520898effacSKurt Hackel 					     "but no dead node!\n",
2521898effacSKurt Hackel 					     dlm->name, dlm->reco.new_master);
2522898effacSKurt Hackel 					BUG();
2523898effacSKurt Hackel 				}
2524898effacSKurt Hackel 			}
2525898effacSKurt Hackel 			spin_unlock(&dlm->spinlock);
2526898effacSKurt Hackel 		}
2527898effacSKurt Hackel 
2528898effacSKurt Hackel 		/* if this node has actually become the recovery master,
2529898effacSKurt Hackel 		 * set the master and send the messages to begin recovery */
2530898effacSKurt Hackel 		if (!status) {
2531898effacSKurt Hackel 			mlog(0, "%s: dead=%u, this=%u, sending "
2532898effacSKurt Hackel 			     "begin_reco now\n", dlm->name,
2533898effacSKurt Hackel 			     dlm->reco.dead_node, dlm->node_num);
25346714d8e8SKurt Hackel 			status = dlm_send_begin_reco_message(dlm,
25356714d8e8SKurt Hackel 				      dlm->reco.dead_node);
2536e2faea4cSKurt Hackel 			/* this always succeeds */
2537e2faea4cSKurt Hackel 			BUG_ON(status);
2538e2faea4cSKurt Hackel 
2539e2faea4cSKurt Hackel 			/* set the new_master to this node */
2540e2faea4cSKurt Hackel 			spin_lock(&dlm->spinlock);
2541ab27eb6fSKurt Hackel 			dlm_set_reco_master(dlm, dlm->node_num);
2542e2faea4cSKurt Hackel 			spin_unlock(&dlm->spinlock);
2543e2faea4cSKurt Hackel 		}
25446714d8e8SKurt Hackel 
25456714d8e8SKurt Hackel 		/* recovery lock is a special case.  ast will not get fired,
25466714d8e8SKurt Hackel 		 * so just go ahead and unlock it. */
25476714d8e8SKurt Hackel 		ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2548e2faea4cSKurt Hackel 		if (ret == DLM_DENIED) {
2549e2faea4cSKurt Hackel 			mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2550e2faea4cSKurt Hackel 			ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2551e2faea4cSKurt Hackel 		}
25526714d8e8SKurt Hackel 		if (ret != DLM_NORMAL) {
25536714d8e8SKurt Hackel 			/* this would really suck. this could only happen
25546714d8e8SKurt Hackel 			 * if there was a network error during the unlock
25556714d8e8SKurt Hackel 			 * because of node death.  this means the unlock
25566714d8e8SKurt Hackel 			 * is actually "done" and the lock structure is
25576714d8e8SKurt Hackel 			 * even freed.  we can continue, but only
25586714d8e8SKurt Hackel 			 * because this specific lock name is special. */
2559e2faea4cSKurt Hackel 			mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
25606714d8e8SKurt Hackel 		}
25616714d8e8SKurt Hackel 	} else if (ret == DLM_NOTQUEUED) {
25626714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
25636714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
25646714d8e8SKurt Hackel 		/* another node is master. wait on
2565e2faea4cSKurt Hackel 		 * reco.new_master != O2NM_INVALID_NODE_NUM
2566e2faea4cSKurt Hackel 		 * for at most one second */
2567e2faea4cSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
2568e2faea4cSKurt Hackel 					 dlm_reco_master_ready(dlm),
2569e2faea4cSKurt Hackel 					 msecs_to_jiffies(1000));
2570e2faea4cSKurt Hackel 		if (!dlm_reco_master_ready(dlm)) {
2571e2faea4cSKurt Hackel 			mlog(0, "%s: reco master taking awhile\n",
2572e2faea4cSKurt Hackel 			     dlm->name);
2573e2faea4cSKurt Hackel 			goto again;
2574e2faea4cSKurt Hackel 		}
2575e2faea4cSKurt Hackel 		/* another node has informed this one that it is reco master */
2576e2faea4cSKurt Hackel 		mlog(0, "%s: reco master %u is ready to recover %u\n",
2577e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
25786714d8e8SKurt Hackel 		status = -EEXIST;
2579c8df412eSKurt Hackel 	} else if (ret == DLM_RECOVERING) {
2580c8df412eSKurt Hackel 		mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2581c8df412eSKurt Hackel 		     dlm->name, dlm->node_num);
2582c8df412eSKurt Hackel 		goto again;
2583e2faea4cSKurt Hackel 	} else {
2584e2faea4cSKurt Hackel 		struct dlm_lock_resource *res;
2585e2faea4cSKurt Hackel 
2586e2faea4cSKurt Hackel 		/* dlmlock returned something other than NOTQUEUED or NORMAL */
2587e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2588e2faea4cSKurt Hackel 		     "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2589e2faea4cSKurt Hackel 		     dlm_errname(lksb.status));
2590e2faea4cSKurt Hackel 		res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2591e2faea4cSKurt Hackel 					 DLM_RECOVERY_LOCK_NAME_LEN);
2592e2faea4cSKurt Hackel 		if (res) {
2593e2faea4cSKurt Hackel 			dlm_print_one_lock_resource(res);
2594e2faea4cSKurt Hackel 			dlm_lockres_put(res);
2595e2faea4cSKurt Hackel 		} else {
2596e2faea4cSKurt Hackel 			mlog(ML_ERROR, "recovery lock not found\n");
2597e2faea4cSKurt Hackel 		}
2598e2faea4cSKurt Hackel 		BUG();
25996714d8e8SKurt Hackel 	}
26006714d8e8SKurt Hackel 
26016714d8e8SKurt Hackel 	return status;
26026714d8e8SKurt Hackel }
26036714d8e8SKurt Hackel 
26046714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
26056714d8e8SKurt Hackel {
26066714d8e8SKurt Hackel 	struct dlm_begin_reco br;
26076714d8e8SKurt Hackel 	int ret = 0;
26086714d8e8SKurt Hackel 	struct dlm_node_iter iter;
26096714d8e8SKurt Hackel 	int nodenum;
26106714d8e8SKurt Hackel 	int status;
26116714d8e8SKurt Hackel 
26126714d8e8SKurt Hackel 	mlog_entry("%u\n", dead_node);
26136714d8e8SKurt Hackel 
2614d6dea6e9SKurt Hackel 	mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
26156714d8e8SKurt Hackel 
26166714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
26176714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
26186714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
26196714d8e8SKurt Hackel 
26206714d8e8SKurt Hackel 	clear_bit(dead_node, iter.node_map);
26216714d8e8SKurt Hackel 
26226714d8e8SKurt Hackel 	memset(&br, 0, sizeof(br));
26236714d8e8SKurt Hackel 	br.node_idx = dlm->node_num;
26246714d8e8SKurt Hackel 	br.dead_node = dead_node;
26256714d8e8SKurt Hackel 
26266714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
26276714d8e8SKurt Hackel 		ret = 0;
26286714d8e8SKurt Hackel 		if (nodenum == dead_node) {
26296714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to dead node "
26306714d8e8SKurt Hackel 				  "%u\n", dead_node);
26316714d8e8SKurt Hackel 			continue;
26326714d8e8SKurt Hackel 		}
26336714d8e8SKurt Hackel 		if (nodenum == dlm->node_num) {
26346714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to self\n");
26356714d8e8SKurt Hackel 			continue;
26366714d8e8SKurt Hackel 		}
2637e2faea4cSKurt Hackel retry:
26386714d8e8SKurt Hackel 		ret = -EINVAL;
26396714d8e8SKurt Hackel 		mlog(0, "attempting to send begin reco msg to %d\n",
26406714d8e8SKurt Hackel 			  nodenum);
26416714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
26426714d8e8SKurt Hackel 					 &br, sizeof(br), nodenum, &status);
26436714d8e8SKurt Hackel 		/* negative status is handled ok by caller here */
26446714d8e8SKurt Hackel 		if (ret >= 0)
26456714d8e8SKurt Hackel 			ret = status;
2646e2faea4cSKurt Hackel 		if (dlm_is_host_down(ret)) {
2647e2faea4cSKurt Hackel 			/* node is down.  not involved in recovery
2648e2faea4cSKurt Hackel 			 * so just keep going */
2649a5196ec5SWengang Wang 			mlog(ML_NOTICE, "%s: node %u was down when sending "
2650e2faea4cSKurt Hackel 			     "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2651e2faea4cSKurt Hackel 			ret = 0;
2652e2faea4cSKurt Hackel 		}
2653cd34edd8SSunil Mushran 
2654cd34edd8SSunil Mushran 		/*
2655cd34edd8SSunil Mushran 		 * Prior to commit aad1b15310b9bcd59fa81ab8f2b1513b59553ea8,
2656cd34edd8SSunil Mushran 		 * dlm_begin_reco_handler() returned EAGAIN and not -EAGAIN.
2657cd34edd8SSunil Mushran 		 * We are handling both for compatibility reasons.
2658cd34edd8SSunil Mushran 		 */
2659cd34edd8SSunil Mushran 		if (ret == -EAGAIN || ret == EAGAIN) {
2660aad1b153STiger Yang 			mlog(0, "%s: trying to start recovery of node "
2661aad1b153STiger Yang 			     "%u, but node %u is waiting for last recovery "
2662aad1b153STiger Yang 			     "to complete, backoff for a bit\n", dlm->name,
2663aad1b153STiger Yang 			     dead_node, nodenum);
2664aad1b153STiger Yang 			msleep(100);
2665aad1b153STiger Yang 			goto retry;
2666aad1b153STiger Yang 		}
26676714d8e8SKurt Hackel 		if (ret < 0) {
26686714d8e8SKurt Hackel 			struct dlm_lock_resource *res;
2669a5196ec5SWengang Wang 
2670e2faea4cSKurt Hackel 			/* this is now a serious problem, possibly ENOMEM
2671e2faea4cSKurt Hackel 			 * in the network stack.  must retry */
26726714d8e8SKurt Hackel 			mlog_errno(ret);
26736714d8e8SKurt Hackel 			mlog(ML_ERROR, "begin reco of dlm %s to node %u "
26746714d8e8SKurt Hackel 			     "returned %d\n", dlm->name, nodenum, ret);
26756714d8e8SKurt Hackel 			res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
26766714d8e8SKurt Hackel 						 DLM_RECOVERY_LOCK_NAME_LEN);
26776714d8e8SKurt Hackel 			if (res) {
26786714d8e8SKurt Hackel 				dlm_print_one_lock_resource(res);
26796714d8e8SKurt Hackel 				dlm_lockres_put(res);
26806714d8e8SKurt Hackel 			} else {
26816714d8e8SKurt Hackel 				mlog(ML_ERROR, "recovery lock not found\n");
26826714d8e8SKurt Hackel 			}
2683e2faea4cSKurt Hackel 			/* sleep for a bit in hopes that we can avoid
2684e2faea4cSKurt Hackel 			 * another ENOMEM */
2685e2faea4cSKurt Hackel 			msleep(100);
2686e2faea4cSKurt Hackel 			goto retry;
26876714d8e8SKurt Hackel 		}
26886714d8e8SKurt Hackel 	}
26896714d8e8SKurt Hackel 
26906714d8e8SKurt Hackel 	return ret;
26916714d8e8SKurt Hackel }
26926714d8e8SKurt Hackel 
2693d74c9803SKurt Hackel int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2694d74c9803SKurt Hackel 			   void **ret_data)
26956714d8e8SKurt Hackel {
26966714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
26976714d8e8SKurt Hackel 	struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
26986714d8e8SKurt Hackel 
26996714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
27006714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
27016714d8e8SKurt Hackel 		return 0;
27026714d8e8SKurt Hackel 
2703466d1a45SKurt Hackel 	spin_lock(&dlm->spinlock);
2704466d1a45SKurt Hackel 	if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2705466d1a45SKurt Hackel 		mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2706466d1a45SKurt Hackel 		     "but this node is in finalize state, waiting on finalize2\n",
2707466d1a45SKurt Hackel 		     dlm->name, br->node_idx, br->dead_node,
2708466d1a45SKurt Hackel 		     dlm->reco.dead_node, dlm->reco.new_master);
2709466d1a45SKurt Hackel 		spin_unlock(&dlm->spinlock);
2710aad1b153STiger Yang 		return -EAGAIN;
2711466d1a45SKurt Hackel 	}
2712466d1a45SKurt Hackel 	spin_unlock(&dlm->spinlock);
2713466d1a45SKurt Hackel 
2714d6dea6e9SKurt Hackel 	mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2715d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2716d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
27176714d8e8SKurt Hackel 
27186714d8e8SKurt Hackel 	dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
27196714d8e8SKurt Hackel 
27206714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
27216714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2722e2faea4cSKurt Hackel 		if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2723e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u died, changing "
2724e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2725e2faea4cSKurt Hackel 			     br->node_idx);
2726e2faea4cSKurt Hackel 		} else {
2727e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u NOT DEAD, changing "
2728e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2729e2faea4cSKurt Hackel 			     br->node_idx);
2730e2faea4cSKurt Hackel 			/* may not have seen the new master as dead yet */
2731e2faea4cSKurt Hackel 		}
27326714d8e8SKurt Hackel 	}
27336714d8e8SKurt Hackel 	if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2734e2faea4cSKurt Hackel 		mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2735e2faea4cSKurt Hackel 		     "node %u changing it to %u\n", dlm->name,
2736e2faea4cSKurt Hackel 		     dlm->reco.dead_node, br->node_idx, br->dead_node);
27376714d8e8SKurt Hackel 	}
2738ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, br->node_idx);
2739ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, br->dead_node);
27406714d8e8SKurt Hackel 	if (!test_bit(br->dead_node, dlm->recovery_map)) {
2741e2faea4cSKurt Hackel 		mlog(0, "recovery master %u sees %u as dead, but this "
27426714d8e8SKurt Hackel 		     "node has not yet.  marking %u as dead\n",
27436714d8e8SKurt Hackel 		     br->node_idx, br->dead_node, br->dead_node);
2744e2faea4cSKurt Hackel 		if (!test_bit(br->dead_node, dlm->domain_map) ||
2745e2faea4cSKurt Hackel 		    !test_bit(br->dead_node, dlm->live_nodes_map))
2746e2faea4cSKurt Hackel 			mlog(0, "%u not in domain/live_nodes map "
2747e2faea4cSKurt Hackel 			     "so setting it in reco map manually\n",
2748e2faea4cSKurt Hackel 			     br->dead_node);
2749c03872f5SKurt Hackel 		/* force the recovery cleanup in __dlm_hb_node_down
2750c03872f5SKurt Hackel 		 * both of these will be cleared in a moment */
2751c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->domain_map);
2752c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->live_nodes_map);
27536714d8e8SKurt Hackel 		__dlm_hb_node_down(dlm, br->dead_node);
27546714d8e8SKurt Hackel 	}
27556714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
27566714d8e8SKurt Hackel 
27576714d8e8SKurt Hackel 	dlm_kick_recovery_thread(dlm);
2758d6dea6e9SKurt Hackel 
2759d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2760d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2761d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
2762d6dea6e9SKurt Hackel 
27636714d8e8SKurt Hackel 	dlm_put(dlm);
27646714d8e8SKurt Hackel 	return 0;
27656714d8e8SKurt Hackel }
27666714d8e8SKurt Hackel 
2767466d1a45SKurt Hackel #define DLM_FINALIZE_STAGE2  0x01
27686714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
27696714d8e8SKurt Hackel {
27706714d8e8SKurt Hackel 	int ret = 0;
27716714d8e8SKurt Hackel 	struct dlm_finalize_reco fr;
27726714d8e8SKurt Hackel 	struct dlm_node_iter iter;
27736714d8e8SKurt Hackel 	int nodenum;
27746714d8e8SKurt Hackel 	int status;
2775466d1a45SKurt Hackel 	int stage = 1;
27766714d8e8SKurt Hackel 
2777466d1a45SKurt Hackel 	mlog(0, "finishing recovery for node %s:%u, "
2778466d1a45SKurt Hackel 	     "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
27796714d8e8SKurt Hackel 
27806714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
27816714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
27826714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
27836714d8e8SKurt Hackel 
2784466d1a45SKurt Hackel stage2:
27856714d8e8SKurt Hackel 	memset(&fr, 0, sizeof(fr));
27866714d8e8SKurt Hackel 	fr.node_idx = dlm->node_num;
27876714d8e8SKurt Hackel 	fr.dead_node = dlm->reco.dead_node;
2788466d1a45SKurt Hackel 	if (stage == 2)
2789466d1a45SKurt Hackel 		fr.flags |= DLM_FINALIZE_STAGE2;
27906714d8e8SKurt Hackel 
27916714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
27926714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
27936714d8e8SKurt Hackel 			continue;
27946714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
27956714d8e8SKurt Hackel 					 &fr, sizeof(fr), nodenum, &status);
2796466d1a45SKurt Hackel 		if (ret >= 0)
27976714d8e8SKurt Hackel 			ret = status;
2798466d1a45SKurt Hackel 		if (ret < 0) {
2799a5196ec5SWengang Wang 			mlog(ML_ERROR, "Error %d when sending message %u (key "
2800a5196ec5SWengang Wang 			     "0x%x) to node %u\n", ret, DLM_FINALIZE_RECO_MSG,
2801a5196ec5SWengang Wang 			     dlm->key, nodenum);
28026714d8e8SKurt Hackel 			if (dlm_is_host_down(ret)) {
28036714d8e8SKurt Hackel 				/* this has no effect on this recovery
28046714d8e8SKurt Hackel 				 * session, so set the status to zero to
28056714d8e8SKurt Hackel 				 * finish out the last recovery */
28066714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u went down after this "
28076714d8e8SKurt Hackel 				     "node finished recovery.\n", nodenum);
28086714d8e8SKurt Hackel 				ret = 0;
2809c27069e6SKurt Hackel 				continue;
28106714d8e8SKurt Hackel 			}
28116714d8e8SKurt Hackel 			break;
28126714d8e8SKurt Hackel 		}
28136714d8e8SKurt Hackel 	}
2814466d1a45SKurt Hackel 	if (stage == 1) {
2815466d1a45SKurt Hackel 		/* reset the node_iter back to the top and send finalize2 */
2816466d1a45SKurt Hackel 		iter.curnode = -1;
2817466d1a45SKurt Hackel 		stage = 2;
2818466d1a45SKurt Hackel 		goto stage2;
2819466d1a45SKurt Hackel 	}
28206714d8e8SKurt Hackel 
28216714d8e8SKurt Hackel 	return ret;
28226714d8e8SKurt Hackel }
28236714d8e8SKurt Hackel 
2824d74c9803SKurt Hackel int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2825d74c9803SKurt Hackel 			      void **ret_data)
28266714d8e8SKurt Hackel {
28276714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
28286714d8e8SKurt Hackel 	struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2829466d1a45SKurt Hackel 	int stage = 1;
28306714d8e8SKurt Hackel 
28316714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
28326714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
28336714d8e8SKurt Hackel 		return 0;
28346714d8e8SKurt Hackel 
2835466d1a45SKurt Hackel 	if (fr->flags & DLM_FINALIZE_STAGE2)
2836466d1a45SKurt Hackel 		stage = 2;
2837466d1a45SKurt Hackel 
2838466d1a45SKurt Hackel 	mlog(0, "%s: node %u finalizing recovery stage%d of "
2839466d1a45SKurt Hackel 	     "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2840466d1a45SKurt Hackel 	     fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
28416714d8e8SKurt Hackel 
28426714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
28436714d8e8SKurt Hackel 
28446714d8e8SKurt Hackel 	if (dlm->reco.new_master != fr->node_idx) {
28456714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
28466714d8e8SKurt Hackel 		     "%u is supposed to be the new master, dead=%u\n",
28476714d8e8SKurt Hackel 		     fr->node_idx, dlm->reco.new_master, fr->dead_node);
28486714d8e8SKurt Hackel 		BUG();
28496714d8e8SKurt Hackel 	}
28506714d8e8SKurt Hackel 	if (dlm->reco.dead_node != fr->dead_node) {
28516714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
28526714d8e8SKurt Hackel 		     "node %u, but node %u is supposed to be dead\n",
28536714d8e8SKurt Hackel 		     fr->node_idx, fr->dead_node, dlm->reco.dead_node);
28546714d8e8SKurt Hackel 		BUG();
28556714d8e8SKurt Hackel 	}
28566714d8e8SKurt Hackel 
2857466d1a45SKurt Hackel 	switch (stage) {
2858466d1a45SKurt Hackel 		case 1:
28596714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2860466d1a45SKurt Hackel 			if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2861466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize1 from "
2862466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2863466d1a45SKurt Hackel 				     "this node has already received it!\n",
2864466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2865466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2866466d1a45SKurt Hackel 				BUG();
2867466d1a45SKurt Hackel 			}
2868466d1a45SKurt Hackel 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
28696714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
2870466d1a45SKurt Hackel 			break;
2871466d1a45SKurt Hackel 		case 2:
2872466d1a45SKurt Hackel 			if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2873466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize2 from "
2874466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2875466d1a45SKurt Hackel 				     "this node did not have finalize1!\n",
2876466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2877466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2878466d1a45SKurt Hackel 				BUG();
2879466d1a45SKurt Hackel 			}
2880466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2881466d1a45SKurt Hackel 			spin_unlock(&dlm->spinlock);
28826714d8e8SKurt Hackel 			dlm_reset_recovery(dlm);
28836714d8e8SKurt Hackel 			dlm_kick_recovery_thread(dlm);
2884466d1a45SKurt Hackel 			break;
2885466d1a45SKurt Hackel 		default:
2886466d1a45SKurt Hackel 			BUG();
2887466d1a45SKurt Hackel 	}
2888466d1a45SKurt Hackel 
2889d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2890d6dea6e9SKurt Hackel 	     dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2891d6dea6e9SKurt Hackel 
28926714d8e8SKurt Hackel 	dlm_put(dlm);
28936714d8e8SKurt Hackel 	return 0;
28946714d8e8SKurt Hackel }
2895