xref: /openbmc/linux/fs/ocfs2/dlm/dlmrecovery.c (revision 6561168c)
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/utsname.h>
346714d8e8SKurt Hackel #include <linux/init.h>
356714d8e8SKurt Hackel #include <linux/sysctl.h>
366714d8e8SKurt Hackel #include <linux/random.h>
376714d8e8SKurt Hackel #include <linux/blkdev.h>
386714d8e8SKurt Hackel #include <linux/socket.h>
396714d8e8SKurt Hackel #include <linux/inet.h>
406714d8e8SKurt Hackel #include <linux/timer.h>
416714d8e8SKurt Hackel #include <linux/kthread.h>
42b4c7f538SAdrian Bunk #include <linux/delay.h>
436714d8e8SKurt Hackel 
446714d8e8SKurt Hackel 
456714d8e8SKurt Hackel #include "cluster/heartbeat.h"
466714d8e8SKurt Hackel #include "cluster/nodemanager.h"
476714d8e8SKurt Hackel #include "cluster/tcp.h"
486714d8e8SKurt Hackel 
496714d8e8SKurt Hackel #include "dlmapi.h"
506714d8e8SKurt Hackel #include "dlmcommon.h"
516714d8e8SKurt Hackel #include "dlmdomain.h"
526714d8e8SKurt Hackel 
536714d8e8SKurt Hackel #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
546714d8e8SKurt Hackel #include "cluster/masklog.h"
556714d8e8SKurt Hackel 
566714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
576714d8e8SKurt Hackel 
586714d8e8SKurt Hackel static int dlm_recovery_thread(void *data);
596714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
606714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
61c03872f5SKurt Hackel void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
626714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm);
636714d8e8SKurt Hackel 
646714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
656714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
666714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
676714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm,
686714d8e8SKurt Hackel 				 u8 request_from, u8 dead_node);
696714d8e8SKurt Hackel static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
706714d8e8SKurt Hackel 
716714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
726714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
736714d8e8SKurt Hackel 					const char *lockname, int namelen,
746714d8e8SKurt Hackel 					int total_locks, u64 cookie,
756714d8e8SKurt Hackel 					u8 flags, u8 master);
766714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
776714d8e8SKurt Hackel 				    struct dlm_migratable_lockres *mres,
786714d8e8SKurt Hackel 				    u8 send_to,
796714d8e8SKurt Hackel 				    struct dlm_lock_resource *res,
806714d8e8SKurt Hackel 				    int total_locks);
816714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
826714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
836714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres);
846714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
856714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
866714d8e8SKurt Hackel 				 u8 dead_node, u8 send_to);
876714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
886714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
896714d8e8SKurt Hackel 					struct list_head *list, u8 dead_node);
906714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
916714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master);
926714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata);
936714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type);
946714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
956714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item,
966714d8e8SKurt Hackel 					 void *data);
976714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
988169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
998169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
1008169cae5SAdrian Bunk 				      u8 *real_master);
1016714d8e8SKurt Hackel 
1026714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void);
1036714d8e8SKurt Hackel 
10434af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_reco_state_lock);
10534af946aSIngo Molnar static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
1066714d8e8SKurt Hackel static u64 dlm_mig_cookie = 1;
1076714d8e8SKurt Hackel 
1086714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void)
1096714d8e8SKurt Hackel {
1106714d8e8SKurt Hackel 	u64 c;
1116714d8e8SKurt Hackel 	spin_lock(&dlm_mig_cookie_lock);
1126714d8e8SKurt Hackel 	c = dlm_mig_cookie;
1136714d8e8SKurt Hackel 	if (dlm_mig_cookie == (~0ULL))
1146714d8e8SKurt Hackel 		dlm_mig_cookie = 1;
1156714d8e8SKurt Hackel 	else
1166714d8e8SKurt Hackel 		dlm_mig_cookie++;
1176714d8e8SKurt Hackel 	spin_unlock(&dlm_mig_cookie_lock);
1186714d8e8SKurt Hackel 	return c;
1196714d8e8SKurt Hackel }
1206714d8e8SKurt Hackel 
121ab27eb6fSKurt Hackel static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
122ab27eb6fSKurt Hackel 					  u8 dead_node)
123ab27eb6fSKurt Hackel {
124ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
125ab27eb6fSKurt Hackel 	if (dlm->reco.dead_node != dead_node)
126ab27eb6fSKurt Hackel 		mlog(0, "%s: changing dead_node from %u to %u\n",
127ab27eb6fSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dead_node);
128ab27eb6fSKurt Hackel 	dlm->reco.dead_node = dead_node;
129ab27eb6fSKurt Hackel }
130ab27eb6fSKurt Hackel 
131ab27eb6fSKurt Hackel static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
132ab27eb6fSKurt Hackel 				       u8 master)
133ab27eb6fSKurt Hackel {
134ab27eb6fSKurt Hackel 	assert_spin_locked(&dlm->spinlock);
135ab27eb6fSKurt Hackel 	mlog(0, "%s: changing new_master from %u to %u\n",
136ab27eb6fSKurt Hackel 	     dlm->name, dlm->reco.new_master, master);
137ab27eb6fSKurt Hackel 	dlm->reco.new_master = master;
138ab27eb6fSKurt Hackel }
139ab27eb6fSKurt Hackel 
140466d1a45SKurt Hackel static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
1416714d8e8SKurt Hackel {
142466d1a45SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
1436714d8e8SKurt Hackel 	clear_bit(dlm->reco.dead_node, dlm->recovery_map);
144ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
145ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
146466d1a45SKurt Hackel }
147466d1a45SKurt Hackel 
148466d1a45SKurt Hackel static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
149466d1a45SKurt Hackel {
150466d1a45SKurt Hackel 	spin_lock(&dlm->spinlock);
151466d1a45SKurt Hackel 	__dlm_reset_recovery(dlm);
1526714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1536714d8e8SKurt Hackel }
1546714d8e8SKurt Hackel 
1556714d8e8SKurt Hackel /* Worker function used during recovery. */
156c4028958SDavid Howells void dlm_dispatch_work(struct work_struct *work)
1576714d8e8SKurt Hackel {
158c4028958SDavid Howells 	struct dlm_ctxt *dlm =
159c4028958SDavid Howells 		container_of(work, struct dlm_ctxt, dispatched_work);
1606714d8e8SKurt Hackel 	LIST_HEAD(tmp_list);
161800deef3SChristoph Hellwig 	struct dlm_work_item *item, *next;
1626714d8e8SKurt Hackel 	dlm_workfunc_t *workfunc;
1633156d267SKurt Hackel 	int tot=0;
1643156d267SKurt Hackel 
1656714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
1666714d8e8SKurt Hackel 	list_splice_init(&dlm->work_list, &tmp_list);
1676714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
1686714d8e8SKurt Hackel 
169800deef3SChristoph Hellwig 	list_for_each_entry(item, &tmp_list, list) {
1703156d267SKurt Hackel 		tot++;
1713156d267SKurt Hackel 	}
1723156d267SKurt Hackel 	mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
1733156d267SKurt Hackel 
174800deef3SChristoph Hellwig 	list_for_each_entry_safe(item, next, &tmp_list, list) {
1756714d8e8SKurt Hackel 		workfunc = item->func;
1766714d8e8SKurt Hackel 		list_del_init(&item->list);
1776714d8e8SKurt Hackel 
1786714d8e8SKurt Hackel 		/* already have ref on dlm to avoid having
1796714d8e8SKurt Hackel 		 * it disappear.  just double-check. */
1806714d8e8SKurt Hackel 		BUG_ON(item->dlm != dlm);
1816714d8e8SKurt Hackel 
1826714d8e8SKurt Hackel 		/* this is allowed to sleep and
1836714d8e8SKurt Hackel 		 * call network stuff */
1846714d8e8SKurt Hackel 		workfunc(item, item->data);
1856714d8e8SKurt Hackel 
1866714d8e8SKurt Hackel 		dlm_put(dlm);
1876714d8e8SKurt Hackel 		kfree(item);
1886714d8e8SKurt Hackel 	}
1896714d8e8SKurt Hackel }
1906714d8e8SKurt Hackel 
1916714d8e8SKurt Hackel /*
1926714d8e8SKurt Hackel  * RECOVERY THREAD
1936714d8e8SKurt Hackel  */
1946714d8e8SKurt Hackel 
195c03872f5SKurt Hackel void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
1966714d8e8SKurt Hackel {
1976714d8e8SKurt Hackel 	/* wake the recovery thread
1986714d8e8SKurt Hackel 	 * this will wake the reco thread in one of three places
1996714d8e8SKurt Hackel 	 * 1) sleeping with no recovery happening
2006714d8e8SKurt Hackel 	 * 2) sleeping with recovery mastered elsewhere
2016714d8e8SKurt Hackel 	 * 3) recovery mastered here, waiting on reco data */
2026714d8e8SKurt Hackel 
2036714d8e8SKurt Hackel 	wake_up(&dlm->dlm_reco_thread_wq);
2046714d8e8SKurt Hackel }
2056714d8e8SKurt Hackel 
2066714d8e8SKurt Hackel /* Launch the recovery thread */
2076714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
2086714d8e8SKurt Hackel {
2096714d8e8SKurt Hackel 	mlog(0, "starting dlm recovery thread...\n");
2106714d8e8SKurt Hackel 
2116714d8e8SKurt Hackel 	dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
2126714d8e8SKurt Hackel 						"dlm_reco_thread");
2136714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_reco_thread_task)) {
2146714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
2156714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2166714d8e8SKurt Hackel 		return -EINVAL;
2176714d8e8SKurt Hackel 	}
2186714d8e8SKurt Hackel 
2196714d8e8SKurt Hackel 	return 0;
2206714d8e8SKurt Hackel }
2216714d8e8SKurt Hackel 
2226714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
2236714d8e8SKurt Hackel {
2246714d8e8SKurt Hackel 	if (dlm->dlm_reco_thread_task) {
2256714d8e8SKurt Hackel 		mlog(0, "waiting for dlm recovery thread to exit\n");
2266714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_reco_thread_task);
2276714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2286714d8e8SKurt Hackel 	}
2296714d8e8SKurt Hackel }
2306714d8e8SKurt Hackel 
2316714d8e8SKurt Hackel 
2326714d8e8SKurt Hackel 
2336714d8e8SKurt Hackel /*
2346714d8e8SKurt Hackel  * this is lame, but here's how recovery works...
2356714d8e8SKurt Hackel  * 1) all recovery threads cluster wide will work on recovering
2366714d8e8SKurt Hackel  *    ONE node at a time
2376714d8e8SKurt Hackel  * 2) negotiate who will take over all the locks for the dead node.
2386714d8e8SKurt Hackel  *    thats right... ALL the locks.
2396714d8e8SKurt Hackel  * 3) once a new master is chosen, everyone scans all locks
2406714d8e8SKurt Hackel  *    and moves aside those mastered by the dead guy
2416714d8e8SKurt Hackel  * 4) each of these locks should be locked until recovery is done
2426714d8e8SKurt Hackel  * 5) the new master collects up all of secondary lock queue info
2436714d8e8SKurt Hackel  *    one lock at a time, forcing each node to communicate back
2446714d8e8SKurt Hackel  *    before continuing
2456714d8e8SKurt Hackel  * 6) each secondary lock queue responds with the full known lock info
2466714d8e8SKurt Hackel  * 7) once the new master has run all its locks, it sends a ALLDONE!
2476714d8e8SKurt Hackel  *    message to everyone
2486714d8e8SKurt Hackel  * 8) upon receiving this message, the secondary queue node unlocks
2496714d8e8SKurt Hackel  *    and responds to the ALLDONE
2506714d8e8SKurt Hackel  * 9) once the new master gets responses from everyone, he unlocks
2516714d8e8SKurt Hackel  *    everything and recovery for this dead node is done
2526714d8e8SKurt Hackel  *10) go back to 2) while there are still dead nodes
2536714d8e8SKurt Hackel  *
2546714d8e8SKurt Hackel  */
2556714d8e8SKurt Hackel 
256d6dea6e9SKurt Hackel static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
257d6dea6e9SKurt Hackel {
258d6dea6e9SKurt Hackel 	struct dlm_reco_node_data *ndata;
259d6dea6e9SKurt Hackel 	struct dlm_lock_resource *res;
260d6dea6e9SKurt Hackel 
261d6dea6e9SKurt Hackel 	mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
262ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
263d6dea6e9SKurt Hackel 	     dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
264d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
265d6dea6e9SKurt Hackel 
266d6dea6e9SKurt Hackel 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
267d6dea6e9SKurt Hackel 		char *st = "unknown";
268d6dea6e9SKurt Hackel 		switch (ndata->state) {
269d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
270d6dea6e9SKurt Hackel 				st = "init";
271d6dea6e9SKurt Hackel 				break;
272d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
273d6dea6e9SKurt Hackel 				st = "requesting";
274d6dea6e9SKurt Hackel 				break;
275d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
276d6dea6e9SKurt Hackel 				st = "dead";
277d6dea6e9SKurt Hackel 				break;
278d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
279d6dea6e9SKurt Hackel 				st = "receiving";
280d6dea6e9SKurt Hackel 				break;
281d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
282d6dea6e9SKurt Hackel 				st = "requested";
283d6dea6e9SKurt Hackel 				break;
284d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
285d6dea6e9SKurt Hackel 				st = "done";
286d6dea6e9SKurt Hackel 				break;
287d6dea6e9SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
288d6dea6e9SKurt Hackel 				st = "finalize-sent";
289d6dea6e9SKurt Hackel 				break;
290d6dea6e9SKurt Hackel 			default:
291d6dea6e9SKurt Hackel 				st = "bad";
292d6dea6e9SKurt Hackel 				break;
293d6dea6e9SKurt Hackel 		}
294d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
295d6dea6e9SKurt Hackel 		     dlm->name, ndata->node_num, st);
296d6dea6e9SKurt Hackel 	}
297d6dea6e9SKurt Hackel 	list_for_each_entry(res, &dlm->reco.resources, recovering) {
298d6dea6e9SKurt Hackel 		mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
299d6dea6e9SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
300d6dea6e9SKurt Hackel 	}
301d6dea6e9SKurt Hackel }
3026714d8e8SKurt Hackel 
3036714d8e8SKurt Hackel #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
3046714d8e8SKurt Hackel 
3056714d8e8SKurt Hackel static int dlm_recovery_thread(void *data)
3066714d8e8SKurt Hackel {
3076714d8e8SKurt Hackel 	int status;
3086714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
3096714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
3106714d8e8SKurt Hackel 
3116714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
3126714d8e8SKurt Hackel 
3136714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
3146714d8e8SKurt Hackel 		if (dlm_joined(dlm)) {
3156714d8e8SKurt Hackel 			status = dlm_do_recovery(dlm);
3166714d8e8SKurt Hackel 			if (status == -EAGAIN) {
3176714d8e8SKurt Hackel 				/* do not sleep, recheck immediately. */
3186714d8e8SKurt Hackel 				continue;
3196714d8e8SKurt Hackel 			}
3206714d8e8SKurt Hackel 			if (status < 0)
3216714d8e8SKurt Hackel 				mlog_errno(status);
3226714d8e8SKurt Hackel 		}
3236714d8e8SKurt Hackel 
3246714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
3256714d8e8SKurt Hackel 						 kthread_should_stop(),
3266714d8e8SKurt Hackel 						 timeout);
3276714d8e8SKurt Hackel 	}
3286714d8e8SKurt Hackel 
3296714d8e8SKurt Hackel 	mlog(0, "quitting DLM recovery thread\n");
3306714d8e8SKurt Hackel 	return 0;
3316714d8e8SKurt Hackel }
3326714d8e8SKurt Hackel 
333e2faea4cSKurt Hackel /* returns true when the recovery master has contacted us */
334e2faea4cSKurt Hackel static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
335e2faea4cSKurt Hackel {
336e2faea4cSKurt Hackel 	int ready;
337e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
338e2faea4cSKurt Hackel 	ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
339e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
340e2faea4cSKurt Hackel 	return ready;
341e2faea4cSKurt Hackel }
342e2faea4cSKurt Hackel 
343e2faea4cSKurt Hackel /* returns true if node is no longer in the domain
344e2faea4cSKurt Hackel  * could be dead or just not joined */
345e2faea4cSKurt Hackel int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
346e2faea4cSKurt Hackel {
347e2faea4cSKurt Hackel 	int dead;
348e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
349aba9aac7SKurt Hackel 	dead = !test_bit(node, dlm->domain_map);
350e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
351e2faea4cSKurt Hackel 	return dead;
352e2faea4cSKurt Hackel }
353e2faea4cSKurt Hackel 
354b7084ab5SKurt Hackel /* returns true if node is no longer in the domain
355b7084ab5SKurt Hackel  * could be dead or just not joined */
3563fb5a989SAdrian Bunk static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
357b7084ab5SKurt Hackel {
358b7084ab5SKurt Hackel 	int recovered;
359b7084ab5SKurt Hackel 	spin_lock(&dlm->spinlock);
360b7084ab5SKurt Hackel 	recovered = !test_bit(node, dlm->recovery_map);
361b7084ab5SKurt Hackel 	spin_unlock(&dlm->spinlock);
362b7084ab5SKurt Hackel 	return recovered;
363b7084ab5SKurt Hackel }
364b7084ab5SKurt Hackel 
365b7084ab5SKurt Hackel 
36644465a7dSKurt Hackel int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
36744465a7dSKurt Hackel {
36844465a7dSKurt Hackel 	if (timeout) {
36944465a7dSKurt Hackel 		mlog(ML_NOTICE, "%s: waiting %dms for notification of "
37044465a7dSKurt Hackel 		     "death of node %u\n", dlm->name, timeout, node);
37144465a7dSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
37244465a7dSKurt Hackel 			   dlm_is_node_dead(dlm, node),
37344465a7dSKurt Hackel 			   msecs_to_jiffies(timeout));
37444465a7dSKurt Hackel 	} else {
37544465a7dSKurt Hackel 		mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
37644465a7dSKurt Hackel 		     "of death of node %u\n", dlm->name, node);
37744465a7dSKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
37844465a7dSKurt Hackel 			   dlm_is_node_dead(dlm, node));
37944465a7dSKurt Hackel 	}
38044465a7dSKurt Hackel 	/* for now, return 0 */
38144465a7dSKurt Hackel 	return 0;
38244465a7dSKurt Hackel }
38344465a7dSKurt Hackel 
384b7084ab5SKurt Hackel int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
385b7084ab5SKurt Hackel {
386b7084ab5SKurt Hackel 	if (timeout) {
387b7084ab5SKurt Hackel 		mlog(0, "%s: waiting %dms for notification of "
388b7084ab5SKurt Hackel 		     "recovery of node %u\n", dlm->name, timeout, node);
389b7084ab5SKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
390b7084ab5SKurt Hackel 			   dlm_is_node_recovered(dlm, node),
391b7084ab5SKurt Hackel 			   msecs_to_jiffies(timeout));
392b7084ab5SKurt Hackel 	} else {
393b7084ab5SKurt Hackel 		mlog(0, "%s: waiting indefinitely for notification "
394b7084ab5SKurt Hackel 		     "of recovery of node %u\n", dlm->name, node);
395b7084ab5SKurt Hackel 		wait_event(dlm->dlm_reco_thread_wq,
396b7084ab5SKurt Hackel 			   dlm_is_node_recovered(dlm, node));
397b7084ab5SKurt Hackel 	}
398b7084ab5SKurt Hackel 	/* for now, return 0 */
399b7084ab5SKurt Hackel 	return 0;
400b7084ab5SKurt Hackel }
401b7084ab5SKurt Hackel 
4026714d8e8SKurt Hackel /* callers of the top-level api calls (dlmlock/dlmunlock) should
4036714d8e8SKurt Hackel  * block on the dlm->reco.event when recovery is in progress.
4046714d8e8SKurt Hackel  * the dlm recovery thread will set this state when it begins
4056714d8e8SKurt Hackel  * recovering a dead node (as the new master or not) and clear
4066714d8e8SKurt Hackel  * the state and wake as soon as all affected lock resources have
4076714d8e8SKurt Hackel  * been marked with the RECOVERY flag */
4086714d8e8SKurt Hackel static int dlm_in_recovery(struct dlm_ctxt *dlm)
4096714d8e8SKurt Hackel {
4106714d8e8SKurt Hackel 	int in_recovery;
4116714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4126714d8e8SKurt Hackel 	in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
4136714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4146714d8e8SKurt Hackel 	return in_recovery;
4156714d8e8SKurt Hackel }
4166714d8e8SKurt Hackel 
4176714d8e8SKurt Hackel 
4186714d8e8SKurt Hackel void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
4196714d8e8SKurt Hackel {
42056a7c104SKurt Hackel 	if (dlm_in_recovery(dlm)) {
4213b3b84a8SKurt Hackel 		mlog(0, "%s: reco thread %d in recovery: "
42256a7c104SKurt Hackel 		     "state=%d, master=%u, dead=%u\n",
423ba25f9dcSPavel Emelyanov 		     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
42456a7c104SKurt Hackel 		     dlm->reco.state, dlm->reco.new_master,
42556a7c104SKurt Hackel 		     dlm->reco.dead_node);
42656a7c104SKurt Hackel 	}
4276714d8e8SKurt Hackel 	wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
4286714d8e8SKurt Hackel }
4296714d8e8SKurt Hackel 
4306714d8e8SKurt Hackel static void dlm_begin_recovery(struct dlm_ctxt *dlm)
4316714d8e8SKurt Hackel {
4326714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4336714d8e8SKurt Hackel 	BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
4346714d8e8SKurt Hackel 	dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
4356714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4366714d8e8SKurt Hackel }
4376714d8e8SKurt Hackel 
4386714d8e8SKurt Hackel static void dlm_end_recovery(struct dlm_ctxt *dlm)
4396714d8e8SKurt Hackel {
4406714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4416714d8e8SKurt Hackel 	BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
4426714d8e8SKurt Hackel 	dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
4436714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4446714d8e8SKurt Hackel 	wake_up(&dlm->reco.event);
4456714d8e8SKurt Hackel }
4466714d8e8SKurt Hackel 
4476714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm)
4486714d8e8SKurt Hackel {
4496714d8e8SKurt Hackel 	int status = 0;
450e2faea4cSKurt Hackel 	int ret;
4516714d8e8SKurt Hackel 
4526714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
4536714d8e8SKurt Hackel 
4546714d8e8SKurt Hackel 	/* check to see if the new master has died */
4556714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
4566714d8e8SKurt Hackel 	    test_bit(dlm->reco.new_master, dlm->recovery_map)) {
4576714d8e8SKurt Hackel 		mlog(0, "new master %u died while recovering %u!\n",
4586714d8e8SKurt Hackel 		     dlm->reco.new_master, dlm->reco.dead_node);
4596714d8e8SKurt Hackel 		/* unset the new_master, leave dead_node */
460ab27eb6fSKurt Hackel 		dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
4616714d8e8SKurt Hackel 	}
4626714d8e8SKurt Hackel 
4636714d8e8SKurt Hackel 	/* select a target to recover */
4646714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4656714d8e8SKurt Hackel 		int bit;
4666714d8e8SKurt Hackel 
4676714d8e8SKurt Hackel 		bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
4686714d8e8SKurt Hackel 		if (bit >= O2NM_MAX_NODES || bit < 0)
469ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4706714d8e8SKurt Hackel 		else
471ab27eb6fSKurt Hackel 			dlm_set_reco_dead_node(dlm, bit);
4726714d8e8SKurt Hackel 	} else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
4736714d8e8SKurt Hackel 		/* BUG? */
4746714d8e8SKurt Hackel 		mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
4756714d8e8SKurt Hackel 		     dlm->reco.dead_node);
476ab27eb6fSKurt Hackel 		dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
4776714d8e8SKurt Hackel 	}
4786714d8e8SKurt Hackel 
4796714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
4806714d8e8SKurt Hackel 		// mlog(0, "nothing to recover!  sleeping now!\n");
4816714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
4826714d8e8SKurt Hackel 		/* return to main thread loop and sleep. */
4836714d8e8SKurt Hackel 		return 0;
4846714d8e8SKurt Hackel 	}
485d6dea6e9SKurt Hackel 	mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
486ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
4876714d8e8SKurt Hackel 	     dlm->reco.dead_node);
4886714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
4896714d8e8SKurt Hackel 
4906714d8e8SKurt Hackel 	/* take write barrier */
4916714d8e8SKurt Hackel 	/* (stops the list reshuffling thread, proxy ast handling) */
4926714d8e8SKurt Hackel 	dlm_begin_recovery(dlm);
4936714d8e8SKurt Hackel 
4946714d8e8SKurt Hackel 	if (dlm->reco.new_master == dlm->node_num)
4956714d8e8SKurt Hackel 		goto master_here;
4966714d8e8SKurt Hackel 
4976714d8e8SKurt Hackel 	if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
498e2faea4cSKurt Hackel 		/* choose a new master, returns 0 if this node
499e2faea4cSKurt Hackel 		 * is the master, -EEXIST if it's another node.
500e2faea4cSKurt Hackel 		 * this does not return until a new master is chosen
501e2faea4cSKurt Hackel 		 * or recovery completes entirely. */
502e2faea4cSKurt Hackel 		ret = dlm_pick_recovery_master(dlm);
503e2faea4cSKurt Hackel 		if (!ret) {
5046714d8e8SKurt Hackel 			/* already notified everyone.  go. */
5056714d8e8SKurt Hackel 			goto master_here;
5066714d8e8SKurt Hackel 		}
5076714d8e8SKurt Hackel 		mlog(0, "another node will master this recovery session.\n");
5086714d8e8SKurt Hackel 	}
509d6dea6e9SKurt Hackel 	mlog(0, "dlm=%s (%d), new_master=%u, this node=%u, dead_node=%u\n",
510ba25f9dcSPavel Emelyanov 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task), dlm->reco.new_master,
5116714d8e8SKurt Hackel 	     dlm->node_num, dlm->reco.dead_node);
5126714d8e8SKurt Hackel 
5136714d8e8SKurt Hackel 	/* it is safe to start everything back up here
5146714d8e8SKurt Hackel 	 * because all of the dead node's lock resources
5156714d8e8SKurt Hackel 	 * have been marked as in-recovery */
5166714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5176714d8e8SKurt Hackel 
5186714d8e8SKurt Hackel 	/* sleep out in main dlm_recovery_thread loop. */
5196714d8e8SKurt Hackel 	return 0;
5206714d8e8SKurt Hackel 
5216714d8e8SKurt Hackel master_here:
522d6dea6e9SKurt Hackel 	mlog(0, "(%d) mastering recovery of %s:%u here(this=%u)!\n",
523ba25f9dcSPavel Emelyanov 	     task_pid_nr(dlm->dlm_reco_thread_task),
5246714d8e8SKurt Hackel 	     dlm->name, dlm->reco.dead_node, dlm->node_num);
5256714d8e8SKurt Hackel 
5266714d8e8SKurt Hackel 	status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
5276714d8e8SKurt Hackel 	if (status < 0) {
5286a413211SKurt Hackel 		/* we should never hit this anymore */
5296714d8e8SKurt Hackel 		mlog(ML_ERROR, "error %d remastering locks for node %u, "
5306714d8e8SKurt Hackel 		     "retrying.\n", status, dlm->reco.dead_node);
531e2faea4cSKurt Hackel 		/* yield a bit to allow any final network messages
532e2faea4cSKurt Hackel 		 * to get handled on remaining nodes */
533e2faea4cSKurt Hackel 		msleep(100);
5346714d8e8SKurt Hackel 	} else {
5356714d8e8SKurt Hackel 		/* success!  see if any other nodes need recovery */
536e2faea4cSKurt Hackel 		mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
537e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dlm->node_num);
5386714d8e8SKurt Hackel 		dlm_reset_recovery(dlm);
5396714d8e8SKurt Hackel 	}
5406714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
5416714d8e8SKurt Hackel 
5426714d8e8SKurt Hackel 	/* continue and look for another dead node */
5436714d8e8SKurt Hackel 	return -EAGAIN;
5446714d8e8SKurt Hackel }
5456714d8e8SKurt Hackel 
5466714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
5476714d8e8SKurt Hackel {
5486714d8e8SKurt Hackel 	int status = 0;
5496714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
5506714d8e8SKurt Hackel 	int all_nodes_done;
5516714d8e8SKurt Hackel 	int destroy = 0;
5526714d8e8SKurt Hackel 	int pass = 0;
5536714d8e8SKurt Hackel 
5546a413211SKurt Hackel 	do {
5556a413211SKurt Hackel 		/* we have become recovery master.  there is no escaping
5566a413211SKurt Hackel 		 * this, so just keep trying until we get it. */
5576714d8e8SKurt Hackel 		status = dlm_init_recovery_area(dlm, dead_node);
5586a413211SKurt Hackel 		if (status < 0) {
5596a413211SKurt Hackel 			mlog(ML_ERROR, "%s: failed to alloc recovery area, "
5606a413211SKurt Hackel 			     "retrying\n", dlm->name);
5616a413211SKurt Hackel 			msleep(1000);
5626a413211SKurt Hackel 		}
5636a413211SKurt Hackel 	} while (status != 0);
5646714d8e8SKurt Hackel 
5656714d8e8SKurt Hackel 	/* safe to access the node data list without a lock, since this
5666714d8e8SKurt Hackel 	 * process is the only one to change the list */
567800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
5686714d8e8SKurt Hackel 		BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
5696714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
5706714d8e8SKurt Hackel 
5716714d8e8SKurt Hackel 		mlog(0, "requesting lock info from node %u\n",
5726714d8e8SKurt Hackel 		     ndata->node_num);
5736714d8e8SKurt Hackel 
5746714d8e8SKurt Hackel 		if (ndata->node_num == dlm->node_num) {
5756714d8e8SKurt Hackel 			ndata->state = DLM_RECO_NODE_DATA_DONE;
5766714d8e8SKurt Hackel 			continue;
5776714d8e8SKurt Hackel 		}
5786714d8e8SKurt Hackel 
5796a413211SKurt Hackel 		do {
5806a413211SKurt Hackel 			status = dlm_request_all_locks(dlm, ndata->node_num,
5816a413211SKurt Hackel 						       dead_node);
5826714d8e8SKurt Hackel 			if (status < 0) {
5836714d8e8SKurt Hackel 				mlog_errno(status);
5846a413211SKurt Hackel 				if (dlm_is_host_down(status)) {
5856a413211SKurt Hackel 					/* node died, ignore it for recovery */
5866a413211SKurt Hackel 					status = 0;
5876714d8e8SKurt Hackel 					ndata->state = DLM_RECO_NODE_DATA_DEAD;
5886a413211SKurt Hackel 					/* wait for the domain map to catch up
5896a413211SKurt Hackel 					 * with the network state. */
5906a413211SKurt Hackel 					wait_event_timeout(dlm->dlm_reco_thread_wq,
5916a413211SKurt Hackel 							   dlm_is_node_dead(dlm,
5926a413211SKurt Hackel 								ndata->node_num),
5936a413211SKurt Hackel 							   msecs_to_jiffies(1000));
5946a413211SKurt Hackel 					mlog(0, "waited 1 sec for %u, "
5956a413211SKurt Hackel 					     "dead? %s\n", ndata->node_num,
5966a413211SKurt Hackel 					     dlm_is_node_dead(dlm, ndata->node_num) ?
5976a413211SKurt Hackel 					     "yes" : "no");
5986a413211SKurt Hackel 				} else {
5996a413211SKurt Hackel 					/* -ENOMEM on the other node */
6006a413211SKurt Hackel 					mlog(0, "%s: node %u returned "
6016a413211SKurt Hackel 					     "%d during recovery, retrying "
6026a413211SKurt Hackel 					     "after a short wait\n",
6036a413211SKurt Hackel 					     dlm->name, ndata->node_num,
6046a413211SKurt Hackel 					     status);
6056a413211SKurt Hackel 					msleep(100);
6066714d8e8SKurt Hackel 				}
6076714d8e8SKurt Hackel 			}
6086a413211SKurt Hackel 		} while (status != 0);
6096714d8e8SKurt Hackel 
610756a1501SSrinivas Eeda 		spin_lock(&dlm_reco_state_lock);
6116714d8e8SKurt Hackel 		switch (ndata->state) {
6126714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
6136714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
6146714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
6156714d8e8SKurt Hackel 				BUG();
6166714d8e8SKurt Hackel 				break;
6176714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
6186714d8e8SKurt Hackel 				mlog(0, "node %u died after requesting "
6196714d8e8SKurt Hackel 				     "recovery info for node %u\n",
6206714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6216a413211SKurt Hackel 				/* fine.  don't need this node's info.
6226a413211SKurt Hackel 				 * continue without it. */
6236a413211SKurt Hackel 				break;
6246714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
6256714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
6266714d8e8SKurt Hackel 				mlog(0, "now receiving recovery data from "
6276714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6286714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6296714d8e8SKurt Hackel 				break;
6306714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
6316714d8e8SKurt Hackel 				mlog(0, "already receiving recovery data from "
6326714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
6336714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6346714d8e8SKurt Hackel 				break;
6356714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
6366714d8e8SKurt Hackel 				mlog(0, "already DONE receiving recovery data "
6376714d8e8SKurt Hackel 				     "from node %u for dead node %u\n",
6386714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
6396714d8e8SKurt Hackel 				break;
6406714d8e8SKurt Hackel 		}
641756a1501SSrinivas Eeda 		spin_unlock(&dlm_reco_state_lock);
6426714d8e8SKurt Hackel 	}
6436714d8e8SKurt Hackel 
6446714d8e8SKurt Hackel 	mlog(0, "done requesting all lock info\n");
6456714d8e8SKurt Hackel 
6466714d8e8SKurt Hackel 	/* nodes should be sending reco data now
6476714d8e8SKurt Hackel 	 * just need to wait */
6486714d8e8SKurt Hackel 
6496714d8e8SKurt Hackel 	while (1) {
6506714d8e8SKurt Hackel 		/* check all the nodes now to see if we are
6516714d8e8SKurt Hackel 		 * done, or if anyone died */
6526714d8e8SKurt Hackel 		all_nodes_done = 1;
6536714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
654800deef3SChristoph Hellwig 		list_for_each_entry(ndata, &dlm->reco.node_data, list) {
6556714d8e8SKurt Hackel 			mlog(0, "checking recovery state of node %u\n",
6566714d8e8SKurt Hackel 			     ndata->node_num);
6576714d8e8SKurt Hackel 			switch (ndata->state) {
6586714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_INIT:
6596714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTING:
6606714d8e8SKurt Hackel 					mlog(ML_ERROR, "bad ndata state for "
6616714d8e8SKurt Hackel 					     "node %u: state=%d\n",
6626714d8e8SKurt Hackel 					     ndata->node_num, ndata->state);
6636714d8e8SKurt Hackel 					BUG();
6646714d8e8SKurt Hackel 					break;
6656714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DEAD:
6666a413211SKurt Hackel 					mlog(0, "node %u died after "
6676714d8e8SKurt Hackel 					     "requesting recovery info for "
6686714d8e8SKurt Hackel 					     "node %u\n", ndata->node_num,
6696714d8e8SKurt Hackel 					     dead_node);
6706a413211SKurt Hackel 					break;
6716714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_RECEIVING:
6726714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTED:
673d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u still in state %s\n",
674d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num,
675d6dea6e9SKurt Hackel 					     ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
676d6dea6e9SKurt Hackel 					     "receiving" : "requested");
6776714d8e8SKurt Hackel 					all_nodes_done = 0;
6786714d8e8SKurt Hackel 					break;
6796714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DONE:
680d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is done\n",
681d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6826714d8e8SKurt Hackel 					break;
6836714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_FINALIZE_SENT:
684d6dea6e9SKurt Hackel 					mlog(0, "%s: node %u state is finalize\n",
685d6dea6e9SKurt Hackel 					     dlm->name, ndata->node_num);
6866714d8e8SKurt Hackel 					break;
6876714d8e8SKurt Hackel 			}
6886714d8e8SKurt Hackel 		}
6896714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
6906714d8e8SKurt Hackel 
6916714d8e8SKurt Hackel 		mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
6926714d8e8SKurt Hackel 		     all_nodes_done?"yes":"no");
6936714d8e8SKurt Hackel 		if (all_nodes_done) {
6946714d8e8SKurt Hackel 			int ret;
6956714d8e8SKurt Hackel 
6966714d8e8SKurt Hackel 			/* all nodes are now in DLM_RECO_NODE_DATA_DONE state
6976714d8e8SKurt Hackel 	 		 * just send a finalize message to everyone and
6986714d8e8SKurt Hackel 	 		 * clean up */
6996714d8e8SKurt Hackel 			mlog(0, "all nodes are done! send finalize\n");
7006714d8e8SKurt Hackel 			ret = dlm_send_finalize_reco_message(dlm);
7016714d8e8SKurt Hackel 			if (ret < 0)
7026714d8e8SKurt Hackel 				mlog_errno(ret);
7036714d8e8SKurt Hackel 
7046714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
7056714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, dead_node,
7066714d8e8SKurt Hackel 							  dlm->node_num);
7076714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
7086714d8e8SKurt Hackel 			mlog(0, "should be done with recovery!\n");
7096714d8e8SKurt Hackel 
7106714d8e8SKurt Hackel 			mlog(0, "finishing recovery of %s at %lu, "
7116714d8e8SKurt Hackel 			     "dead=%u, this=%u, new=%u\n", dlm->name,
7126714d8e8SKurt Hackel 			     jiffies, dlm->reco.dead_node,
7136714d8e8SKurt Hackel 			     dlm->node_num, dlm->reco.new_master);
7146714d8e8SKurt Hackel 			destroy = 1;
7156a413211SKurt Hackel 			status = 0;
7166714d8e8SKurt Hackel 			/* rescan everything marked dirty along the way */
7176714d8e8SKurt Hackel 			dlm_kick_thread(dlm, NULL);
7186714d8e8SKurt Hackel 			break;
7196714d8e8SKurt Hackel 		}
7206714d8e8SKurt Hackel 		/* wait to be signalled, with periodic timeout
7216714d8e8SKurt Hackel 		 * to check for node death */
7226714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
7236714d8e8SKurt Hackel 					 kthread_should_stop(),
7246714d8e8SKurt Hackel 					 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
7256714d8e8SKurt Hackel 
7266714d8e8SKurt Hackel 	}
7276714d8e8SKurt Hackel 
7286714d8e8SKurt Hackel 	if (destroy)
7296714d8e8SKurt Hackel 		dlm_destroy_recovery_area(dlm, dead_node);
7306714d8e8SKurt Hackel 
7316714d8e8SKurt Hackel 	mlog_exit(status);
7326714d8e8SKurt Hackel 	return status;
7336714d8e8SKurt Hackel }
7346714d8e8SKurt Hackel 
7356714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
7366714d8e8SKurt Hackel {
7376714d8e8SKurt Hackel 	int num=0;
7386714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
7396714d8e8SKurt Hackel 
7406714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
7416714d8e8SKurt Hackel 	memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
7426714d8e8SKurt Hackel 	/* nodes can only be removed (by dying) after dropping
7436714d8e8SKurt Hackel 	 * this lock, and death will be trapped later, so this should do */
7446714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
7456714d8e8SKurt Hackel 
7466714d8e8SKurt Hackel 	while (1) {
7476714d8e8SKurt Hackel 		num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
7486714d8e8SKurt Hackel 		if (num >= O2NM_MAX_NODES) {
7496714d8e8SKurt Hackel 			break;
7506714d8e8SKurt Hackel 		}
7516714d8e8SKurt Hackel 		BUG_ON(num == dead_node);
7526714d8e8SKurt Hackel 
753cd861280SRobert P. J. Day 		ndata = kzalloc(sizeof(*ndata), GFP_NOFS);
7546714d8e8SKurt Hackel 		if (!ndata) {
7556714d8e8SKurt Hackel 			dlm_destroy_recovery_area(dlm, dead_node);
7566714d8e8SKurt Hackel 			return -ENOMEM;
7576714d8e8SKurt Hackel 		}
7586714d8e8SKurt Hackel 		ndata->node_num = num;
7596714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_INIT;
7606714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
7616714d8e8SKurt Hackel 		list_add_tail(&ndata->list, &dlm->reco.node_data);
7626714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
7636714d8e8SKurt Hackel 		num++;
7646714d8e8SKurt Hackel 	}
7656714d8e8SKurt Hackel 
7666714d8e8SKurt Hackel 	return 0;
7676714d8e8SKurt Hackel }
7686714d8e8SKurt Hackel 
7696714d8e8SKurt Hackel static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
7706714d8e8SKurt Hackel {
771800deef3SChristoph Hellwig 	struct dlm_reco_node_data *ndata, *next;
7726714d8e8SKurt Hackel 	LIST_HEAD(tmplist);
7736714d8e8SKurt Hackel 
7746714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
7756714d8e8SKurt Hackel 	list_splice_init(&dlm->reco.node_data, &tmplist);
7766714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
7776714d8e8SKurt Hackel 
778800deef3SChristoph Hellwig 	list_for_each_entry_safe(ndata, next, &tmplist, list) {
7796714d8e8SKurt Hackel 		list_del_init(&ndata->list);
7806714d8e8SKurt Hackel 		kfree(ndata);
7816714d8e8SKurt Hackel 	}
7826714d8e8SKurt Hackel }
7836714d8e8SKurt Hackel 
7846714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
7856714d8e8SKurt Hackel 				 u8 dead_node)
7866714d8e8SKurt Hackel {
7876714d8e8SKurt Hackel 	struct dlm_lock_request lr;
7886714d8e8SKurt Hackel 	enum dlm_status ret;
7896714d8e8SKurt Hackel 
7906714d8e8SKurt Hackel 	mlog(0, "\n");
7916714d8e8SKurt Hackel 
7926714d8e8SKurt Hackel 
7936714d8e8SKurt Hackel 	mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
7946714d8e8SKurt Hackel 		  "to %u\n", dead_node, request_from);
7956714d8e8SKurt Hackel 
7966714d8e8SKurt Hackel 	memset(&lr, 0, sizeof(lr));
7976714d8e8SKurt Hackel 	lr.node_idx = dlm->node_num;
7986714d8e8SKurt Hackel 	lr.dead_node = dead_node;
7996714d8e8SKurt Hackel 
8006714d8e8SKurt Hackel 	// send message
8016714d8e8SKurt Hackel 	ret = DLM_NOLOCKMGR;
8026714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
8036714d8e8SKurt Hackel 				 &lr, sizeof(lr), request_from, NULL);
8046714d8e8SKurt Hackel 
8056714d8e8SKurt Hackel 	/* negative status is handled by caller */
8066714d8e8SKurt Hackel 	if (ret < 0)
8076714d8e8SKurt Hackel 		mlog_errno(ret);
8086714d8e8SKurt Hackel 
8096714d8e8SKurt Hackel 	// return from here, then
8106714d8e8SKurt Hackel 	// sleep until all received or error
8116714d8e8SKurt Hackel 	return ret;
8126714d8e8SKurt Hackel 
8136714d8e8SKurt Hackel }
8146714d8e8SKurt Hackel 
815d74c9803SKurt Hackel int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
816d74c9803SKurt Hackel 				  void **ret_data)
8176714d8e8SKurt Hackel {
8186714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
8196714d8e8SKurt Hackel 	struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
8206714d8e8SKurt Hackel 	char *buf = NULL;
8216714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
8226714d8e8SKurt Hackel 
8236714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
8246714d8e8SKurt Hackel 		return -EINVAL;
8256714d8e8SKurt Hackel 
826c3187ce5SKurt Hackel 	if (lr->dead_node != dlm->reco.dead_node) {
827c3187ce5SKurt Hackel 		mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
828c3187ce5SKurt Hackel 		     "dead_node is %u\n", dlm->name, lr->node_idx,
829c3187ce5SKurt Hackel 		     lr->dead_node, dlm->reco.dead_node);
830d6dea6e9SKurt Hackel 		dlm_print_reco_node_status(dlm);
831c3187ce5SKurt Hackel 		/* this is a hack */
832c3187ce5SKurt Hackel 		dlm_put(dlm);
833c3187ce5SKurt Hackel 		return -ENOMEM;
834c3187ce5SKurt Hackel 	}
8356714d8e8SKurt Hackel 	BUG_ON(lr->dead_node != dlm->reco.dead_node);
8366714d8e8SKurt Hackel 
837cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
8386714d8e8SKurt Hackel 	if (!item) {
8396714d8e8SKurt Hackel 		dlm_put(dlm);
8406714d8e8SKurt Hackel 		return -ENOMEM;
8416714d8e8SKurt Hackel 	}
8426714d8e8SKurt Hackel 
8436714d8e8SKurt Hackel 	/* this will get freed by dlm_request_all_locks_worker */
844ad8100e0SKurt Hackel 	buf = (char *) __get_free_page(GFP_NOFS);
8456714d8e8SKurt Hackel 	if (!buf) {
8466714d8e8SKurt Hackel 		kfree(item);
8476714d8e8SKurt Hackel 		dlm_put(dlm);
8486714d8e8SKurt Hackel 		return -ENOMEM;
8496714d8e8SKurt Hackel 	}
8506714d8e8SKurt Hackel 
8516714d8e8SKurt Hackel 	/* queue up work for dlm_request_all_locks_worker */
8526714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
8536714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
8546714d8e8SKurt Hackel 	item->u.ral.reco_master = lr->node_idx;
8556714d8e8SKurt Hackel 	item->u.ral.dead_node = lr->dead_node;
8566714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
8576714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
8586714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
8593156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
8606714d8e8SKurt Hackel 
8616714d8e8SKurt Hackel 	dlm_put(dlm);
8626714d8e8SKurt Hackel 	return 0;
8636714d8e8SKurt Hackel }
8646714d8e8SKurt Hackel 
8656714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
8666714d8e8SKurt Hackel {
8676714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
8686714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
8696714d8e8SKurt Hackel 	struct dlm_ctxt *dlm;
8706714d8e8SKurt Hackel 	LIST_HEAD(resources);
8716714d8e8SKurt Hackel 	int ret;
8726714d8e8SKurt Hackel 	u8 dead_node, reco_master;
87329c0fa0fSKurt Hackel 	int skip_all_done = 0;
8746714d8e8SKurt Hackel 
8756714d8e8SKurt Hackel 	dlm = item->dlm;
8766714d8e8SKurt Hackel 	dead_node = item->u.ral.dead_node;
8776714d8e8SKurt Hackel 	reco_master = item->u.ral.reco_master;
878e2faea4cSKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
879e2faea4cSKurt Hackel 
880d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
881d6dea6e9SKurt Hackel 	     dlm->name, dead_node, reco_master);
882d6dea6e9SKurt Hackel 
883e2faea4cSKurt Hackel 	if (dead_node != dlm->reco.dead_node ||
884e2faea4cSKurt Hackel 	    reco_master != dlm->reco.new_master) {
8856a413211SKurt Hackel 		/* worker could have been created before the recovery master
8866a413211SKurt Hackel 		 * died.  if so, do not continue, but do not error. */
8876a413211SKurt Hackel 		if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
8886a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: will not send recovery state, "
8896a413211SKurt Hackel 			     "recovery master %u died, thread=(dead=%u,mas=%u)"
8906a413211SKurt Hackel 			     " current=(dead=%u,mas=%u)\n", dlm->name,
8916a413211SKurt Hackel 			     reco_master, dead_node, reco_master,
8926a413211SKurt Hackel 			     dlm->reco.dead_node, dlm->reco.new_master);
8936a413211SKurt Hackel 		} else {
8946a413211SKurt Hackel 			mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
8956a413211SKurt Hackel 			     "master=%u), request(dead=%u, master=%u)\n",
8966a413211SKurt Hackel 			     dlm->name, dlm->reco.dead_node,
8976a413211SKurt Hackel 			     dlm->reco.new_master, dead_node, reco_master);
898e2faea4cSKurt Hackel 		}
8996a413211SKurt Hackel 		goto leave;
9006a413211SKurt Hackel 	}
9016714d8e8SKurt Hackel 
9026714d8e8SKurt Hackel 	/* lock resources should have already been moved to the
9036714d8e8SKurt Hackel  	 * dlm->reco.resources list.  now move items from that list
9046714d8e8SKurt Hackel  	 * to a temp list if the dead owner matches.  note that the
9056714d8e8SKurt Hackel 	 * whole cluster recovers only one node at a time, so we
9066714d8e8SKurt Hackel 	 * can safely move UNKNOWN lock resources for each recovery
9076714d8e8SKurt Hackel 	 * session. */
9086714d8e8SKurt Hackel 	dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
9096714d8e8SKurt Hackel 
9106714d8e8SKurt Hackel 	/* now we can begin blasting lockreses without the dlm lock */
91129c0fa0fSKurt Hackel 
91229c0fa0fSKurt Hackel 	/* any errors returned will be due to the new_master dying,
91329c0fa0fSKurt Hackel 	 * the dlm_reco_thread should detect this */
914800deef3SChristoph Hellwig 	list_for_each_entry(res, &resources, recovering) {
9156714d8e8SKurt Hackel 		ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
9166714d8e8SKurt Hackel 				   	DLM_MRES_RECOVERY);
91729c0fa0fSKurt Hackel 		if (ret < 0) {
918d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
919d6dea6e9SKurt Hackel 			     "recovery state for dead node %u, ret=%d\n", dlm->name,
920d6dea6e9SKurt Hackel 			     reco_master, dead_node, ret);
92129c0fa0fSKurt Hackel 			skip_all_done = 1;
92229c0fa0fSKurt Hackel 			break;
92329c0fa0fSKurt Hackel 		}
9246714d8e8SKurt Hackel 	}
9256714d8e8SKurt Hackel 
9266714d8e8SKurt Hackel 	/* move the resources back to the list */
9276714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
9286714d8e8SKurt Hackel 	list_splice_init(&resources, &dlm->reco.resources);
9296714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
9306714d8e8SKurt Hackel 
93129c0fa0fSKurt Hackel 	if (!skip_all_done) {
9326714d8e8SKurt Hackel 		ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
93329c0fa0fSKurt Hackel 		if (ret < 0) {
934d6dea6e9SKurt Hackel 			mlog(ML_ERROR, "%s: node %u went down while sending "
935d6dea6e9SKurt Hackel 			     "recovery all-done for dead node %u, ret=%d\n",
936d6dea6e9SKurt Hackel 			     dlm->name, reco_master, dead_node, ret);
93729c0fa0fSKurt Hackel 		}
93829c0fa0fSKurt Hackel 	}
9396a413211SKurt Hackel leave:
9406714d8e8SKurt Hackel 	free_page((unsigned long)data);
9416714d8e8SKurt Hackel }
9426714d8e8SKurt Hackel 
9436714d8e8SKurt Hackel 
9446714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
9456714d8e8SKurt Hackel {
9466714d8e8SKurt Hackel 	int ret, tmpret;
9476714d8e8SKurt Hackel 	struct dlm_reco_data_done done_msg;
9486714d8e8SKurt Hackel 
9496714d8e8SKurt Hackel 	memset(&done_msg, 0, sizeof(done_msg));
9506714d8e8SKurt Hackel 	done_msg.node_idx = dlm->node_num;
9516714d8e8SKurt Hackel 	done_msg.dead_node = dead_node;
9526714d8e8SKurt Hackel 	mlog(0, "sending DATA DONE message to %u, "
9536714d8e8SKurt Hackel 	     "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
9546714d8e8SKurt Hackel 	     done_msg.dead_node);
9556714d8e8SKurt Hackel 
9566714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
9576714d8e8SKurt Hackel 				 sizeof(done_msg), send_to, &tmpret);
95829c0fa0fSKurt Hackel 	if (ret < 0) {
95929c0fa0fSKurt Hackel 		if (!dlm_is_host_down(ret)) {
96029c0fa0fSKurt Hackel 			mlog_errno(ret);
96129c0fa0fSKurt Hackel 			mlog(ML_ERROR, "%s: unknown error sending data-done "
96229c0fa0fSKurt Hackel 			     "to %u\n", dlm->name, send_to);
96329c0fa0fSKurt Hackel 			BUG();
96429c0fa0fSKurt Hackel 		}
96529c0fa0fSKurt Hackel 	} else
9666714d8e8SKurt Hackel 		ret = tmpret;
9676714d8e8SKurt Hackel 	return ret;
9686714d8e8SKurt Hackel }
9696714d8e8SKurt Hackel 
9706714d8e8SKurt Hackel 
971d74c9803SKurt Hackel int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
972d74c9803SKurt Hackel 			       void **ret_data)
9736714d8e8SKurt Hackel {
9746714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
9756714d8e8SKurt Hackel 	struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
9766714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata = NULL;
9776714d8e8SKurt Hackel 	int ret = -EINVAL;
9786714d8e8SKurt Hackel 
9796714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
9806714d8e8SKurt Hackel 		return -EINVAL;
9816714d8e8SKurt Hackel 
9826714d8e8SKurt Hackel 	mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
9836714d8e8SKurt Hackel 	     "node_idx=%u, this node=%u\n", done->dead_node,
9846714d8e8SKurt Hackel 	     dlm->reco.dead_node, done->node_idx, dlm->node_num);
985d6dea6e9SKurt Hackel 
986d6dea6e9SKurt Hackel 	mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
987d6dea6e9SKurt Hackel 			"Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
988d6dea6e9SKurt Hackel 			"node_idx=%u, this node=%u\n", done->dead_node,
989d6dea6e9SKurt Hackel 			dlm->reco.dead_node, done->node_idx, dlm->node_num);
9906714d8e8SKurt Hackel 
9916714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
992800deef3SChristoph Hellwig 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
9936714d8e8SKurt Hackel 		if (ndata->node_num != done->node_idx)
9946714d8e8SKurt Hackel 			continue;
9956714d8e8SKurt Hackel 
9966714d8e8SKurt Hackel 		switch (ndata->state) {
997e2faea4cSKurt Hackel 			/* should have moved beyond INIT but not to FINALIZE yet */
9986714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
9996714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
10006714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
10016714d8e8SKurt Hackel 				mlog(ML_ERROR, "bad ndata state for node %u:"
10026714d8e8SKurt Hackel 				     " state=%d\n", ndata->node_num,
10036714d8e8SKurt Hackel 				     ndata->state);
10046714d8e8SKurt Hackel 				BUG();
10056714d8e8SKurt Hackel 				break;
1006e2faea4cSKurt Hackel 			/* these states are possible at this point, anywhere along
1007e2faea4cSKurt Hackel 			 * the line of recovery */
1008e2faea4cSKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
10096714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
10106714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
10116714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
10126714d8e8SKurt Hackel 				mlog(0, "node %u is DONE sending "
10136714d8e8SKurt Hackel 					  "recovery data!\n",
10146714d8e8SKurt Hackel 					  ndata->node_num);
10156714d8e8SKurt Hackel 
10166714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_DONE;
10176714d8e8SKurt Hackel 				ret = 0;
10186714d8e8SKurt Hackel 				break;
10196714d8e8SKurt Hackel 		}
10206714d8e8SKurt Hackel 	}
10216714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
10226714d8e8SKurt Hackel 
10236714d8e8SKurt Hackel 	/* wake the recovery thread, some node is done */
10246714d8e8SKurt Hackel 	if (!ret)
10256714d8e8SKurt Hackel 		dlm_kick_recovery_thread(dlm);
10266714d8e8SKurt Hackel 
10276714d8e8SKurt Hackel 	if (ret < 0)
10286714d8e8SKurt Hackel 		mlog(ML_ERROR, "failed to find recovery node data for node "
10296714d8e8SKurt Hackel 		     "%u\n", done->node_idx);
10306714d8e8SKurt Hackel 	dlm_put(dlm);
10316714d8e8SKurt Hackel 
10326714d8e8SKurt Hackel 	mlog(0, "leaving reco data done handler, ret=%d\n", ret);
10336714d8e8SKurt Hackel 	return ret;
10346714d8e8SKurt Hackel }
10356714d8e8SKurt Hackel 
10366714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
10376714d8e8SKurt Hackel 					struct list_head *list,
10386714d8e8SKurt Hackel 				       	u8 dead_node)
10396714d8e8SKurt Hackel {
1040800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
1041e2faea4cSKurt Hackel 	struct dlm_lock *lock;
10426714d8e8SKurt Hackel 
10436714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1044800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
1045e2faea4cSKurt Hackel 		/* always prune any $RECOVERY entries for dead nodes,
1046e2faea4cSKurt Hackel 		 * otherwise hangs can occur during later recovery */
10476714d8e8SKurt Hackel 		if (dlm_is_recovery_lock(res->lockname.name,
1048e2faea4cSKurt Hackel 					 res->lockname.len)) {
1049e2faea4cSKurt Hackel 			spin_lock(&res->spinlock);
1050e2faea4cSKurt Hackel 			list_for_each_entry(lock, &res->granted, list) {
1051e2faea4cSKurt Hackel 				if (lock->ml.node == dead_node) {
1052e2faea4cSKurt Hackel 					mlog(0, "AHA! there was "
1053e2faea4cSKurt Hackel 					     "a $RECOVERY lock for dead "
1054e2faea4cSKurt Hackel 					     "node %u (%s)!\n",
1055e2faea4cSKurt Hackel 					     dead_node, dlm->name);
1056e2faea4cSKurt Hackel 					list_del_init(&lock->list);
1057e2faea4cSKurt Hackel 					dlm_lock_put(lock);
1058e2faea4cSKurt Hackel 					break;
1059e2faea4cSKurt Hackel 				}
1060e2faea4cSKurt Hackel 			}
1061e2faea4cSKurt Hackel 			spin_unlock(&res->spinlock);
10626714d8e8SKurt Hackel 			continue;
1063e2faea4cSKurt Hackel 		}
1064e2faea4cSKurt Hackel 
10656714d8e8SKurt Hackel 		if (res->owner == dead_node) {
10666714d8e8SKurt Hackel 			mlog(0, "found lockres owned by dead node while "
10676714d8e8SKurt Hackel 				  "doing recovery for node %u. sending it.\n",
10686714d8e8SKurt Hackel 				  dead_node);
1069f116629dSAkinobu Mita 			list_move_tail(&res->recovering, list);
10706714d8e8SKurt Hackel 		} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
10716714d8e8SKurt Hackel 			mlog(0, "found UNKNOWN owner while doing recovery "
10726714d8e8SKurt Hackel 				  "for node %u. sending it.\n", dead_node);
1073f116629dSAkinobu Mita 			list_move_tail(&res->recovering, list);
10746714d8e8SKurt Hackel 		}
10756714d8e8SKurt Hackel 	}
10766714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
10776714d8e8SKurt Hackel }
10786714d8e8SKurt Hackel 
10796714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
10806714d8e8SKurt Hackel {
10816714d8e8SKurt Hackel 	int total_locks = 0;
10826714d8e8SKurt Hackel 	struct list_head *iter, *queue = &res->granted;
10836714d8e8SKurt Hackel 	int i;
10846714d8e8SKurt Hackel 
10856714d8e8SKurt Hackel 	for (i=0; i<3; i++) {
10866714d8e8SKurt Hackel 		list_for_each(iter, queue)
10876714d8e8SKurt Hackel 			total_locks++;
10886714d8e8SKurt Hackel 		queue++;
10896714d8e8SKurt Hackel 	}
10906714d8e8SKurt Hackel 	return total_locks;
10916714d8e8SKurt Hackel }
10926714d8e8SKurt Hackel 
10936714d8e8SKurt Hackel 
10946714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
10956714d8e8SKurt Hackel 				      struct dlm_migratable_lockres *mres,
10966714d8e8SKurt Hackel 				      u8 send_to,
10976714d8e8SKurt Hackel 				      struct dlm_lock_resource *res,
10986714d8e8SKurt Hackel 				      int total_locks)
10996714d8e8SKurt Hackel {
11006714d8e8SKurt Hackel 	u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
11016714d8e8SKurt Hackel 	int mres_total_locks = be32_to_cpu(mres->total_locks);
11026714d8e8SKurt Hackel 	int sz, ret = 0, status = 0;
11036714d8e8SKurt Hackel 	u8 orig_flags = mres->flags,
11046714d8e8SKurt Hackel 	   orig_master = mres->master;
11056714d8e8SKurt Hackel 
11066714d8e8SKurt Hackel 	BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
11076714d8e8SKurt Hackel 	if (!mres->num_locks)
11086714d8e8SKurt Hackel 		return 0;
11096714d8e8SKurt Hackel 
11106714d8e8SKurt Hackel 	sz = sizeof(struct dlm_migratable_lockres) +
11116714d8e8SKurt Hackel 		(mres->num_locks * sizeof(struct dlm_migratable_lock));
11126714d8e8SKurt Hackel 
11136714d8e8SKurt Hackel 	/* add an all-done flag if we reached the last lock */
11146714d8e8SKurt Hackel 	orig_flags = mres->flags;
11156714d8e8SKurt Hackel 	BUG_ON(total_locks > mres_total_locks);
11166714d8e8SKurt Hackel 	if (total_locks == mres_total_locks)
11176714d8e8SKurt Hackel 		mres->flags |= DLM_MRES_ALL_DONE;
11186714d8e8SKurt Hackel 
1119ba2bf218SKurt Hackel 	mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1120ba2bf218SKurt Hackel 	     dlm->name, res->lockname.len, res->lockname.name,
1121ba2bf218SKurt Hackel 	     orig_flags & DLM_MRES_MIGRATION ? "migrate" : "recovery",
1122ba2bf218SKurt Hackel 	     send_to);
1123ba2bf218SKurt Hackel 
11246714d8e8SKurt Hackel 	/* send it */
11256714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
11266714d8e8SKurt Hackel 				 sz, send_to, &status);
11276714d8e8SKurt Hackel 	if (ret < 0) {
11286714d8e8SKurt Hackel 		/* XXX: negative status is not handled.
11296714d8e8SKurt Hackel 		 * this will end up killing this node. */
11306714d8e8SKurt Hackel 		mlog_errno(ret);
11316714d8e8SKurt Hackel 	} else {
11326714d8e8SKurt Hackel 		/* might get an -ENOMEM back here */
11336714d8e8SKurt Hackel 		ret = status;
11346714d8e8SKurt Hackel 		if (ret < 0) {
11356714d8e8SKurt Hackel 			mlog_errno(ret);
11366714d8e8SKurt Hackel 
11376714d8e8SKurt Hackel 			if (ret == -EFAULT) {
11386714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u told me to kill "
11396714d8e8SKurt Hackel 				     "myself!\n", send_to);
11406714d8e8SKurt Hackel 				BUG();
11416714d8e8SKurt Hackel 			}
11426714d8e8SKurt Hackel 		}
11436714d8e8SKurt Hackel 	}
11446714d8e8SKurt Hackel 
11456714d8e8SKurt Hackel 	/* zero and reinit the message buffer */
11466714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
11476714d8e8SKurt Hackel 				    res->lockname.len, mres_total_locks,
11486714d8e8SKurt Hackel 				    mig_cookie, orig_flags, orig_master);
11496714d8e8SKurt Hackel 	return ret;
11506714d8e8SKurt Hackel }
11516714d8e8SKurt Hackel 
11526714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
11536714d8e8SKurt Hackel 					const char *lockname, int namelen,
11546714d8e8SKurt Hackel 					int total_locks, u64 cookie,
11556714d8e8SKurt Hackel 					u8 flags, u8 master)
11566714d8e8SKurt Hackel {
11576714d8e8SKurt Hackel 	/* mres here is one full page */
11585fb0f7f0SShani Moideen 	clear_page(mres);
11596714d8e8SKurt Hackel 	mres->lockname_len = namelen;
11606714d8e8SKurt Hackel 	memcpy(mres->lockname, lockname, namelen);
11616714d8e8SKurt Hackel 	mres->num_locks = 0;
11626714d8e8SKurt Hackel 	mres->total_locks = cpu_to_be32(total_locks);
11636714d8e8SKurt Hackel 	mres->mig_cookie = cpu_to_be64(cookie);
11646714d8e8SKurt Hackel 	mres->flags = flags;
11656714d8e8SKurt Hackel 	mres->master = master;
11666714d8e8SKurt Hackel }
11676714d8e8SKurt Hackel 
11686714d8e8SKurt Hackel 
11696714d8e8SKurt Hackel /* returns 1 if this lock fills the network structure,
11706714d8e8SKurt Hackel  * 0 otherwise */
11716714d8e8SKurt Hackel static int dlm_add_lock_to_array(struct dlm_lock *lock,
11726714d8e8SKurt Hackel 				 struct dlm_migratable_lockres *mres, int queue)
11736714d8e8SKurt Hackel {
11746714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
11756714d8e8SKurt Hackel 	int lock_num = mres->num_locks;
11766714d8e8SKurt Hackel 
11776714d8e8SKurt Hackel 	ml = &(mres->ml[lock_num]);
11786714d8e8SKurt Hackel 	ml->cookie = lock->ml.cookie;
11796714d8e8SKurt Hackel 	ml->type = lock->ml.type;
11806714d8e8SKurt Hackel 	ml->convert_type = lock->ml.convert_type;
11816714d8e8SKurt Hackel 	ml->highest_blocked = lock->ml.highest_blocked;
11826714d8e8SKurt Hackel 	ml->list = queue;
11836714d8e8SKurt Hackel 	if (lock->lksb) {
11846714d8e8SKurt Hackel 		ml->flags = lock->lksb->flags;
11856714d8e8SKurt Hackel 		/* send our current lvb */
11866714d8e8SKurt Hackel 		if (ml->type == LKM_EXMODE ||
11876714d8e8SKurt Hackel 		    ml->type == LKM_PRMODE) {
11886714d8e8SKurt Hackel 			/* if it is already set, this had better be a PR
11896714d8e8SKurt Hackel 			 * and it has to match */
11908bc674cbSKurt Hackel 			if (!dlm_lvb_is_empty(mres->lvb) &&
11918bc674cbSKurt Hackel 			    (ml->type == LKM_EXMODE ||
11926714d8e8SKurt Hackel 			     memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
11936714d8e8SKurt Hackel 				mlog(ML_ERROR, "mismatched lvbs!\n");
11946714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(lock->lockres);
11956714d8e8SKurt Hackel 				BUG();
11966714d8e8SKurt Hackel 			}
11976714d8e8SKurt Hackel 			memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
11986714d8e8SKurt Hackel 		}
11996714d8e8SKurt Hackel 	}
12006714d8e8SKurt Hackel 	ml->node = lock->ml.node;
12016714d8e8SKurt Hackel 	mres->num_locks++;
12026714d8e8SKurt Hackel 	/* we reached the max, send this network message */
12036714d8e8SKurt Hackel 	if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
12046714d8e8SKurt Hackel 		return 1;
12056714d8e8SKurt Hackel 	return 0;
12066714d8e8SKurt Hackel }
12076714d8e8SKurt Hackel 
1208ba2bf218SKurt Hackel static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
1209ba2bf218SKurt Hackel 			       struct dlm_migratable_lockres *mres)
1210ba2bf218SKurt Hackel {
1211ba2bf218SKurt Hackel 	struct dlm_lock dummy;
1212ba2bf218SKurt Hackel 	memset(&dummy, 0, sizeof(dummy));
1213ba2bf218SKurt Hackel 	dummy.ml.cookie = 0;
1214ba2bf218SKurt Hackel 	dummy.ml.type = LKM_IVMODE;
1215ba2bf218SKurt Hackel 	dummy.ml.convert_type = LKM_IVMODE;
1216ba2bf218SKurt Hackel 	dummy.ml.highest_blocked = LKM_IVMODE;
1217ba2bf218SKurt Hackel 	dummy.lksb = NULL;
1218ba2bf218SKurt Hackel 	dummy.ml.node = dlm->node_num;
1219ba2bf218SKurt Hackel 	dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
1220ba2bf218SKurt Hackel }
1221ba2bf218SKurt Hackel 
1222ba2bf218SKurt Hackel static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
1223ba2bf218SKurt Hackel 				    struct dlm_migratable_lock *ml,
1224ba2bf218SKurt Hackel 				    u8 *nodenum)
1225ba2bf218SKurt Hackel {
1226ba2bf218SKurt Hackel 	if (unlikely(ml->cookie == 0 &&
1227ba2bf218SKurt Hackel 	    ml->type == LKM_IVMODE &&
1228ba2bf218SKurt Hackel 	    ml->convert_type == LKM_IVMODE &&
1229ba2bf218SKurt Hackel 	    ml->highest_blocked == LKM_IVMODE &&
1230ba2bf218SKurt Hackel 	    ml->list == DLM_BLOCKED_LIST)) {
1231ba2bf218SKurt Hackel 		*nodenum = ml->node;
1232ba2bf218SKurt Hackel 		return 1;
1233ba2bf218SKurt Hackel 	}
1234ba2bf218SKurt Hackel 	return 0;
1235ba2bf218SKurt Hackel }
12366714d8e8SKurt Hackel 
12376714d8e8SKurt Hackel int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
12386714d8e8SKurt Hackel 			 struct dlm_migratable_lockres *mres,
12396714d8e8SKurt Hackel 			 u8 send_to, u8 flags)
12406714d8e8SKurt Hackel {
1241800deef3SChristoph Hellwig 	struct list_head *queue;
12426714d8e8SKurt Hackel 	int total_locks, i;
12436714d8e8SKurt Hackel 	u64 mig_cookie = 0;
12446714d8e8SKurt Hackel 	struct dlm_lock *lock;
12456714d8e8SKurt Hackel 	int ret = 0;
12466714d8e8SKurt Hackel 
12476714d8e8SKurt Hackel 	BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
12486714d8e8SKurt Hackel 
12496714d8e8SKurt Hackel 	mlog(0, "sending to %u\n", send_to);
12506714d8e8SKurt Hackel 
12516714d8e8SKurt Hackel 	total_locks = dlm_num_locks_in_lockres(res);
12526714d8e8SKurt Hackel 	if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
12536714d8e8SKurt Hackel 		/* rare, but possible */
12546714d8e8SKurt Hackel 		mlog(0, "argh.  lockres has %d locks.  this will "
12556714d8e8SKurt Hackel 			  "require more than one network packet to "
12566714d8e8SKurt Hackel 			  "migrate\n", total_locks);
12576714d8e8SKurt Hackel 		mig_cookie = dlm_get_next_mig_cookie();
12586714d8e8SKurt Hackel 	}
12596714d8e8SKurt Hackel 
12606714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
12616714d8e8SKurt Hackel 				    res->lockname.len, total_locks,
12626714d8e8SKurt Hackel 				    mig_cookie, flags, res->owner);
12636714d8e8SKurt Hackel 
12646714d8e8SKurt Hackel 	total_locks = 0;
12656714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
12666714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
1267800deef3SChristoph Hellwig 		list_for_each_entry(lock, queue, list) {
12686714d8e8SKurt Hackel 			/* add another lock. */
12696714d8e8SKurt Hackel 			total_locks++;
12706714d8e8SKurt Hackel 			if (!dlm_add_lock_to_array(lock, mres, i))
12716714d8e8SKurt Hackel 				continue;
12726714d8e8SKurt Hackel 
12736714d8e8SKurt Hackel 			/* this filled the lock message,
12746714d8e8SKurt Hackel 			 * we must send it immediately. */
12756714d8e8SKurt Hackel 			ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
12766714d8e8SKurt Hackel 						       res, total_locks);
127729c0fa0fSKurt Hackel 			if (ret < 0)
127829c0fa0fSKurt Hackel 				goto error;
12796714d8e8SKurt Hackel 		}
12806714d8e8SKurt Hackel 	}
1281ba2bf218SKurt Hackel 	if (total_locks == 0) {
1282ba2bf218SKurt Hackel 		/* send a dummy lock to indicate a mastery reference only */
1283ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
1284ba2bf218SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name,
1285ba2bf218SKurt Hackel 		     send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
1286ba2bf218SKurt Hackel 		     "migration");
1287ba2bf218SKurt Hackel 		dlm_add_dummy_lock(dlm, mres);
1288ba2bf218SKurt Hackel 	}
12896714d8e8SKurt Hackel 	/* flush any remaining locks */
12906714d8e8SKurt Hackel 	ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
129129c0fa0fSKurt Hackel 	if (ret < 0)
129229c0fa0fSKurt Hackel 		goto error;
129329c0fa0fSKurt Hackel 	return ret;
129429c0fa0fSKurt Hackel 
129529c0fa0fSKurt Hackel error:
129629c0fa0fSKurt Hackel 	mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
129729c0fa0fSKurt Hackel 	     dlm->name, ret);
129829c0fa0fSKurt Hackel 	if (!dlm_is_host_down(ret))
12996714d8e8SKurt Hackel 		BUG();
130029c0fa0fSKurt Hackel 	mlog(0, "%s: node %u went down while sending %s "
130129c0fa0fSKurt Hackel 	     "lockres %.*s\n", dlm->name, send_to,
130229c0fa0fSKurt Hackel 	     flags & DLM_MRES_RECOVERY ?  "recovery" : "migration",
130329c0fa0fSKurt Hackel 	     res->lockname.len, res->lockname.name);
13046714d8e8SKurt Hackel 	return ret;
13056714d8e8SKurt Hackel }
13066714d8e8SKurt Hackel 
13076714d8e8SKurt Hackel 
13086714d8e8SKurt Hackel 
13096714d8e8SKurt Hackel /*
13106714d8e8SKurt Hackel  * this message will contain no more than one page worth of
13116714d8e8SKurt Hackel  * recovery data, and it will work on only one lockres.
13126714d8e8SKurt Hackel  * there may be many locks in this page, and we may need to wait
13136714d8e8SKurt Hackel  * for additional packets to complete all the locks (rare, but
13146714d8e8SKurt Hackel  * possible).
13156714d8e8SKurt Hackel  */
13166714d8e8SKurt Hackel /*
13176714d8e8SKurt Hackel  * NOTE: the allocation error cases here are scary
13186714d8e8SKurt Hackel  * we really cannot afford to fail an alloc in recovery
13196714d8e8SKurt Hackel  * do we spin?  returning an error only delays the problem really
13206714d8e8SKurt Hackel  */
13216714d8e8SKurt Hackel 
1322d74c9803SKurt Hackel int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
1323d74c9803SKurt Hackel 			    void **ret_data)
13246714d8e8SKurt Hackel {
13256714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
13266714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres =
13276714d8e8SKurt Hackel 		(struct dlm_migratable_lockres *)msg->buf;
13286714d8e8SKurt Hackel 	int ret = 0;
13296714d8e8SKurt Hackel 	u8 real_master;
13306714d8e8SKurt Hackel 	char *buf = NULL;
13316714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
13326714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
13336714d8e8SKurt Hackel 
13346714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
13356714d8e8SKurt Hackel 		return -EINVAL;
13366714d8e8SKurt Hackel 
13376714d8e8SKurt Hackel 	BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
13386714d8e8SKurt Hackel 
13396714d8e8SKurt Hackel 	real_master = mres->master;
13406714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
13416714d8e8SKurt Hackel 		/* cannot migrate a lockres with no master */
13426714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
13436714d8e8SKurt Hackel 	}
13446714d8e8SKurt Hackel 
13456714d8e8SKurt Hackel 	mlog(0, "%s message received from node %u\n",
13466714d8e8SKurt Hackel 		  (mres->flags & DLM_MRES_RECOVERY) ?
13476714d8e8SKurt Hackel 		  "recovery" : "migration", mres->master);
13486714d8e8SKurt Hackel 	if (mres->flags & DLM_MRES_ALL_DONE)
13496714d8e8SKurt Hackel 		mlog(0, "all done flag.  all lockres data received!\n");
13506714d8e8SKurt Hackel 
13516714d8e8SKurt Hackel 	ret = -ENOMEM;
1352ad8100e0SKurt Hackel 	buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1353cd861280SRobert P. J. Day 	item = kzalloc(sizeof(*item), GFP_NOFS);
13546714d8e8SKurt Hackel 	if (!buf || !item)
13556714d8e8SKurt Hackel 		goto leave;
13566714d8e8SKurt Hackel 
13576714d8e8SKurt Hackel 	/* lookup the lock to see if we have a secondary queue for this
13586714d8e8SKurt Hackel 	 * already...  just add the locks in and this will have its owner
13596714d8e8SKurt Hackel 	 * and RECOVERY flag changed when it completes. */
13606714d8e8SKurt Hackel 	res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
13616714d8e8SKurt Hackel 	if (res) {
13626714d8e8SKurt Hackel 	 	/* this will get a ref on res */
13636714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
13646714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
13656714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY) {
13666714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
13676714d8e8SKurt Hackel 		} else {
13686714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_MIGRATING) {
13696714d8e8SKurt Hackel 				/* this is at least the second
13706714d8e8SKurt Hackel 				 * lockres message */
13716714d8e8SKurt Hackel 				mlog(0, "lock %.*s is already migrating\n",
13726714d8e8SKurt Hackel 					  mres->lockname_len,
13736714d8e8SKurt Hackel 					  mres->lockname);
13746714d8e8SKurt Hackel 			} else if (res->state & DLM_LOCK_RES_RECOVERING) {
13756714d8e8SKurt Hackel 				/* caller should BUG */
13766714d8e8SKurt Hackel 				mlog(ML_ERROR, "node is attempting to migrate "
13776714d8e8SKurt Hackel 				     "lock %.*s, but marked as recovering!\n",
13786714d8e8SKurt Hackel 				     mres->lockname_len, mres->lockname);
13796714d8e8SKurt Hackel 				ret = -EFAULT;
13806714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
13816714d8e8SKurt Hackel 				goto leave;
13826714d8e8SKurt Hackel 			}
13836714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
13846714d8e8SKurt Hackel 		}
13856714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
13866714d8e8SKurt Hackel 	} else {
13876714d8e8SKurt Hackel 		/* need to allocate, just like if it was
13886714d8e8SKurt Hackel 		 * mastered here normally  */
13896714d8e8SKurt Hackel 		res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
13906714d8e8SKurt Hackel 		if (!res)
13916714d8e8SKurt Hackel 			goto leave;
13926714d8e8SKurt Hackel 
13936714d8e8SKurt Hackel 		/* to match the ref that we would have gotten if
13946714d8e8SKurt Hackel 		 * dlm_lookup_lockres had succeeded */
13956714d8e8SKurt Hackel 		dlm_lockres_get(res);
13966714d8e8SKurt Hackel 
13976714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
13986714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY)
13996714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
14006714d8e8SKurt Hackel 		else
14016714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
14026714d8e8SKurt Hackel 
14036714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
14046714d8e8SKurt Hackel 		__dlm_insert_lockres(dlm, res);
14056714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
14066714d8e8SKurt Hackel 
14076714d8e8SKurt Hackel 		/* now that the new lockres is inserted,
14086714d8e8SKurt Hackel 		 * make it usable by other processes */
14096714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
14106714d8e8SKurt Hackel 		res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
14116714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
1412a6fa3640SKurt Hackel 		wake_up(&res->wq);
14136714d8e8SKurt Hackel 
14146714d8e8SKurt Hackel 		/* add an extra ref for just-allocated lockres
14156714d8e8SKurt Hackel 		 * otherwise the lockres will be purged immediately */
14166714d8e8SKurt Hackel 		dlm_lockres_get(res);
14176714d8e8SKurt Hackel 	}
14186714d8e8SKurt Hackel 
14196714d8e8SKurt Hackel 	/* at this point we have allocated everything we need,
14206714d8e8SKurt Hackel 	 * and we have a hashed lockres with an extra ref and
14216714d8e8SKurt Hackel 	 * the proper res->state flags. */
14226714d8e8SKurt Hackel 	ret = 0;
1423ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
1424ba2bf218SKurt Hackel 	/* drop this either when master requery finds a different master
1425ba2bf218SKurt Hackel 	 * or when a lock is added by the recovery worker */
1426ba2bf218SKurt Hackel 	dlm_lockres_grab_inflight_ref(dlm, res);
14276714d8e8SKurt Hackel 	if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
14286714d8e8SKurt Hackel 		/* migration cannot have an unknown master */
14296714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
14306714d8e8SKurt Hackel 		mlog(0, "recovery has passed me a lockres with an "
14316714d8e8SKurt Hackel 			  "unknown owner.. will need to requery: "
14326714d8e8SKurt Hackel 			  "%.*s\n", mres->lockname_len, mres->lockname);
14336714d8e8SKurt Hackel 	} else {
1434ba2bf218SKurt Hackel 		/* take a reference now to pin the lockres, drop it
1435ba2bf218SKurt Hackel 		 * when locks are added in the worker */
14366714d8e8SKurt Hackel 		dlm_change_lockres_owner(dlm, res, dlm->node_num);
14376714d8e8SKurt Hackel 	}
1438ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
14396714d8e8SKurt Hackel 
14406714d8e8SKurt Hackel 	/* queue up work for dlm_mig_lockres_worker */
14416714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
14426714d8e8SKurt Hackel 	memcpy(buf, msg->buf, be16_to_cpu(msg->data_len));  /* copy the whole message */
14436714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
14446714d8e8SKurt Hackel 	item->u.ml.lockres = res; /* already have a ref */
14456714d8e8SKurt Hackel 	item->u.ml.real_master = real_master;
14466714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
14476714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
14486714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
14493156d267SKurt Hackel 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
14506714d8e8SKurt Hackel 
14516714d8e8SKurt Hackel leave:
14526714d8e8SKurt Hackel 	dlm_put(dlm);
14536714d8e8SKurt Hackel 	if (ret < 0) {
14546714d8e8SKurt Hackel 		if (buf)
14556714d8e8SKurt Hackel 			kfree(buf);
14566714d8e8SKurt Hackel 		if (item)
14576714d8e8SKurt Hackel 			kfree(item);
14586714d8e8SKurt Hackel 	}
14596714d8e8SKurt Hackel 
14606714d8e8SKurt Hackel 	mlog_exit(ret);
14616714d8e8SKurt Hackel 	return ret;
14626714d8e8SKurt Hackel }
14636714d8e8SKurt Hackel 
14646714d8e8SKurt Hackel 
14656714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
14666714d8e8SKurt Hackel {
14676714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
14686714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
14696714d8e8SKurt Hackel 	int ret = 0;
14706714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
14716714d8e8SKurt Hackel 	u8 real_master;
14726714d8e8SKurt Hackel 
14736714d8e8SKurt Hackel 	dlm = item->dlm;
14746714d8e8SKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
14756714d8e8SKurt Hackel 
14766714d8e8SKurt Hackel 	res = item->u.ml.lockres;
14776714d8e8SKurt Hackel 	real_master = item->u.ml.real_master;
14786714d8e8SKurt Hackel 
14796714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
14806714d8e8SKurt Hackel 		/* this case is super-rare. only occurs if
14816714d8e8SKurt Hackel 		 * node death happens during migration. */
14826714d8e8SKurt Hackel again:
14836714d8e8SKurt Hackel 		ret = dlm_lockres_master_requery(dlm, res, &real_master);
14846714d8e8SKurt Hackel 		if (ret < 0) {
1485e2faea4cSKurt Hackel 			mlog(0, "dlm_lockres_master_requery ret=%d\n",
14866714d8e8SKurt Hackel 				  ret);
14876714d8e8SKurt Hackel 			goto again;
14886714d8e8SKurt Hackel 		}
14896714d8e8SKurt Hackel 		if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
14906714d8e8SKurt Hackel 			mlog(0, "lockres %.*s not claimed.  "
14916714d8e8SKurt Hackel 				   "this node will take it.\n",
14926714d8e8SKurt Hackel 				   res->lockname.len, res->lockname.name);
14936714d8e8SKurt Hackel 		} else {
1494ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
1495ba2bf218SKurt Hackel 			dlm_lockres_drop_inflight_ref(dlm, res);
1496ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
14976714d8e8SKurt Hackel 			mlog(0, "master needs to respond to sender "
14986714d8e8SKurt Hackel 				  "that node %u still owns %.*s\n",
14996714d8e8SKurt Hackel 				  real_master, res->lockname.len,
15006714d8e8SKurt Hackel 				  res->lockname.name);
15016714d8e8SKurt Hackel 			/* cannot touch this lockres */
15026714d8e8SKurt Hackel 			goto leave;
15036714d8e8SKurt Hackel 		}
15046714d8e8SKurt Hackel 	}
15056714d8e8SKurt Hackel 
15066714d8e8SKurt Hackel 	ret = dlm_process_recovery_data(dlm, res, mres);
15076714d8e8SKurt Hackel 	if (ret < 0)
15086714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data returned  %d\n", ret);
15096714d8e8SKurt Hackel 	else
15106714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data succeeded\n");
15116714d8e8SKurt Hackel 
15126714d8e8SKurt Hackel 	if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
15136714d8e8SKurt Hackel 	                   (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
15146714d8e8SKurt Hackel 		ret = dlm_finish_migration(dlm, res, mres->master);
15156714d8e8SKurt Hackel 		if (ret < 0)
15166714d8e8SKurt Hackel 			mlog_errno(ret);
15176714d8e8SKurt Hackel 	}
15186714d8e8SKurt Hackel 
15196714d8e8SKurt Hackel leave:
15206714d8e8SKurt Hackel 	kfree(data);
15216714d8e8SKurt Hackel 	mlog_exit(ret);
15226714d8e8SKurt Hackel }
15236714d8e8SKurt Hackel 
15246714d8e8SKurt Hackel 
15256714d8e8SKurt Hackel 
15268169cae5SAdrian Bunk static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
15278169cae5SAdrian Bunk 				      struct dlm_lock_resource *res,
15288169cae5SAdrian Bunk 				      u8 *real_master)
15296714d8e8SKurt Hackel {
15306714d8e8SKurt Hackel 	struct dlm_node_iter iter;
15316714d8e8SKurt Hackel 	int nodenum;
15326714d8e8SKurt Hackel 	int ret = 0;
15336714d8e8SKurt Hackel 
15346714d8e8SKurt Hackel 	*real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
15356714d8e8SKurt Hackel 
15366714d8e8SKurt Hackel 	/* we only reach here if one of the two nodes in a
15376714d8e8SKurt Hackel 	 * migration died while the migration was in progress.
15386714d8e8SKurt Hackel 	 * at this point we need to requery the master.  we
15396714d8e8SKurt Hackel 	 * know that the new_master got as far as creating
15406714d8e8SKurt Hackel 	 * an mle on at least one node, but we do not know
15416714d8e8SKurt Hackel 	 * if any nodes had actually cleared the mle and set
15426714d8e8SKurt Hackel 	 * the master to the new_master.  the old master
15436714d8e8SKurt Hackel 	 * is supposed to set the owner to UNKNOWN in the
15446714d8e8SKurt Hackel 	 * event of a new_master death, so the only possible
15456714d8e8SKurt Hackel 	 * responses that we can get from nodes here are
15466714d8e8SKurt Hackel 	 * that the master is new_master, or that the master
15476714d8e8SKurt Hackel 	 * is UNKNOWN.
15486714d8e8SKurt Hackel 	 * if all nodes come back with UNKNOWN then we know
15496714d8e8SKurt Hackel 	 * the lock needs remastering here.
15506714d8e8SKurt Hackel 	 * if any node comes back with a valid master, check
15516714d8e8SKurt Hackel 	 * to see if that master is the one that we are
15526714d8e8SKurt Hackel 	 * recovering.  if so, then the new_master died and
15536714d8e8SKurt Hackel 	 * we need to remaster this lock.  if not, then the
15546714d8e8SKurt Hackel 	 * new_master survived and that node will respond to
15556714d8e8SKurt Hackel 	 * other nodes about the owner.
15566714d8e8SKurt Hackel 	 * if there is an owner, this node needs to dump this
15576714d8e8SKurt Hackel 	 * lockres and alert the sender that this lockres
15586714d8e8SKurt Hackel 	 * was rejected. */
15596714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
15606714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
15616714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
15626714d8e8SKurt Hackel 
15636714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
15646714d8e8SKurt Hackel 		/* do not send to self */
15656714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
15666714d8e8SKurt Hackel 			continue;
15676714d8e8SKurt Hackel 		ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
15686714d8e8SKurt Hackel 		if (ret < 0) {
15696714d8e8SKurt Hackel 			mlog_errno(ret);
1570c03872f5SKurt Hackel 			if (!dlm_is_host_down(ret))
15716714d8e8SKurt Hackel 				BUG();
1572c03872f5SKurt Hackel 			/* host is down, so answer for that node would be
1573c03872f5SKurt Hackel 			 * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
15746714d8e8SKurt Hackel 		}
15756714d8e8SKurt Hackel 		if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
15766714d8e8SKurt Hackel 			mlog(0, "lock master is %u\n", *real_master);
15776714d8e8SKurt Hackel 			break;
15786714d8e8SKurt Hackel 		}
15796714d8e8SKurt Hackel 	}
15806714d8e8SKurt Hackel 	return ret;
15816714d8e8SKurt Hackel }
15826714d8e8SKurt Hackel 
15836714d8e8SKurt Hackel 
1584c03872f5SKurt Hackel int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
15856714d8e8SKurt Hackel 			  u8 nodenum, u8 *real_master)
15866714d8e8SKurt Hackel {
15876714d8e8SKurt Hackel 	int ret = -EINVAL;
15886714d8e8SKurt Hackel 	struct dlm_master_requery req;
15896714d8e8SKurt Hackel 	int status = DLM_LOCK_RES_OWNER_UNKNOWN;
15906714d8e8SKurt Hackel 
15916714d8e8SKurt Hackel 	memset(&req, 0, sizeof(req));
15926714d8e8SKurt Hackel 	req.node_idx = dlm->node_num;
15936714d8e8SKurt Hackel 	req.namelen = res->lockname.len;
15946714d8e8SKurt Hackel 	memcpy(req.name, res->lockname.name, res->lockname.len);
15956714d8e8SKurt Hackel 
15966714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
15976714d8e8SKurt Hackel 				 &req, sizeof(req), nodenum, &status);
15986714d8e8SKurt Hackel 	/* XXX: negative status not handled properly here. */
15996714d8e8SKurt Hackel 	if (ret < 0)
16006714d8e8SKurt Hackel 		mlog_errno(ret);
16016714d8e8SKurt Hackel 	else {
16026714d8e8SKurt Hackel 		BUG_ON(status < 0);
16036714d8e8SKurt Hackel 		BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
16046714d8e8SKurt Hackel 		*real_master = (u8) (status & 0xff);
16056714d8e8SKurt Hackel 		mlog(0, "node %u responded to master requery with %u\n",
16066714d8e8SKurt Hackel 			  nodenum, *real_master);
16076714d8e8SKurt Hackel 		ret = 0;
16086714d8e8SKurt Hackel 	}
16096714d8e8SKurt Hackel 	return ret;
16106714d8e8SKurt Hackel }
16116714d8e8SKurt Hackel 
16126714d8e8SKurt Hackel 
16136714d8e8SKurt Hackel /* this function cannot error, so unless the sending
16146714d8e8SKurt Hackel  * or receiving of the message failed, the owner can
16156714d8e8SKurt Hackel  * be trusted */
1616d74c9803SKurt Hackel int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
1617d74c9803SKurt Hackel 			       void **ret_data)
16186714d8e8SKurt Hackel {
16196714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
16206714d8e8SKurt Hackel 	struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
16216714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
1622a3d33291SMark Fasheh 	unsigned int hash;
16236714d8e8SKurt Hackel 	int master = DLM_LOCK_RES_OWNER_UNKNOWN;
16246714d8e8SKurt Hackel 	u32 flags = DLM_ASSERT_MASTER_REQUERY;
16256714d8e8SKurt Hackel 
16266714d8e8SKurt Hackel 	if (!dlm_grab(dlm)) {
16276714d8e8SKurt Hackel 		/* since the domain has gone away on this
16286714d8e8SKurt Hackel 		 * node, the proper response is UNKNOWN */
16296714d8e8SKurt Hackel 		return master;
16306714d8e8SKurt Hackel 	}
16316714d8e8SKurt Hackel 
1632a3d33291SMark Fasheh 	hash = dlm_lockid_hash(req->name, req->namelen);
1633a3d33291SMark Fasheh 
16346714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1635a3d33291SMark Fasheh 	res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
16366714d8e8SKurt Hackel 	if (res) {
16376714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
16386714d8e8SKurt Hackel 		master = res->owner;
16396714d8e8SKurt Hackel 		if (master == dlm->node_num) {
16406714d8e8SKurt Hackel 			int ret = dlm_dispatch_assert_master(dlm, res,
16416714d8e8SKurt Hackel 							     0, 0, flags);
16426714d8e8SKurt Hackel 			if (ret < 0) {
16436714d8e8SKurt Hackel 				mlog_errno(-ENOMEM);
16446714d8e8SKurt Hackel 				/* retry!? */
16456714d8e8SKurt Hackel 				BUG();
16466714d8e8SKurt Hackel 			}
16476714d8e8SKurt Hackel 		}
16486714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
16496714d8e8SKurt Hackel 	}
16506714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
16516714d8e8SKurt Hackel 
16526714d8e8SKurt Hackel 	dlm_put(dlm);
16536714d8e8SKurt Hackel 	return master;
16546714d8e8SKurt Hackel }
16556714d8e8SKurt Hackel 
16566714d8e8SKurt Hackel static inline struct list_head *
16576714d8e8SKurt Hackel dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
16586714d8e8SKurt Hackel {
16596714d8e8SKurt Hackel 	struct list_head *ret;
16606714d8e8SKurt Hackel 	BUG_ON(list_num < 0);
16616714d8e8SKurt Hackel 	BUG_ON(list_num > 2);
16626714d8e8SKurt Hackel 	ret = &(res->granted);
16636714d8e8SKurt Hackel 	ret += list_num;
16646714d8e8SKurt Hackel 	return ret;
16656714d8e8SKurt Hackel }
16666714d8e8SKurt Hackel /* TODO: do ast flush business
16676714d8e8SKurt Hackel  * TODO: do MIGRATING and RECOVERING spinning
16686714d8e8SKurt Hackel  */
16696714d8e8SKurt Hackel 
16706714d8e8SKurt Hackel /*
16716714d8e8SKurt Hackel * NOTE about in-flight requests during migration:
16726714d8e8SKurt Hackel *
16736714d8e8SKurt Hackel * Before attempting the migrate, the master has marked the lockres as
16746714d8e8SKurt Hackel * MIGRATING and then flushed all of its pending ASTS.  So any in-flight
16756714d8e8SKurt Hackel * requests either got queued before the MIGRATING flag got set, in which
16766714d8e8SKurt Hackel * case the lock data will reflect the change and a return message is on
16776714d8e8SKurt Hackel * the way, or the request failed to get in before MIGRATING got set.  In
16786714d8e8SKurt Hackel * this case, the caller will be told to spin and wait for the MIGRATING
16796714d8e8SKurt Hackel * flag to be dropped, then recheck the master.
16806714d8e8SKurt Hackel * This holds true for the convert, cancel and unlock cases, and since lvb
16816714d8e8SKurt Hackel * updates are tied to these same messages, it applies to lvb updates as
16826714d8e8SKurt Hackel * well.  For the lock case, there is no way a lock can be on the master
16836714d8e8SKurt Hackel * queue and not be on the secondary queue since the lock is always added
16846714d8e8SKurt Hackel * locally first.  This means that the new target node will never be sent
16856714d8e8SKurt Hackel * a lock that he doesn't already have on the list.
16866714d8e8SKurt Hackel * In total, this means that the local lock is correct and should not be
16876714d8e8SKurt Hackel * updated to match the one sent by the master.  Any messages sent back
16886714d8e8SKurt Hackel * from the master before the MIGRATING flag will bring the lock properly
16896714d8e8SKurt Hackel * up-to-date, and the change will be ordered properly for the waiter.
16906714d8e8SKurt Hackel * We will *not* attempt to modify the lock underneath the waiter.
16916714d8e8SKurt Hackel */
16926714d8e8SKurt Hackel 
16936714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
16946714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
16956714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres)
16966714d8e8SKurt Hackel {
16976714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
16986714d8e8SKurt Hackel 	struct list_head *queue;
1699e17e75ecSKurt Hackel 	struct list_head *tmpq = NULL;
17006714d8e8SKurt Hackel 	struct dlm_lock *newlock = NULL;
17016714d8e8SKurt Hackel 	struct dlm_lockstatus *lksb = NULL;
17026714d8e8SKurt Hackel 	int ret = 0;
1703e17e75ecSKurt Hackel 	int i, j, bad;
17046714d8e8SKurt Hackel 	struct dlm_lock *lock = NULL;
1705ba2bf218SKurt Hackel 	u8 from = O2NM_MAX_NODES;
1706ba2bf218SKurt Hackel 	unsigned int added = 0;
17076714d8e8SKurt Hackel 
17086714d8e8SKurt Hackel 	mlog(0, "running %d locks for this lockres\n", mres->num_locks);
17096714d8e8SKurt Hackel 	for (i=0; i<mres->num_locks; i++) {
17106714d8e8SKurt Hackel 		ml = &(mres->ml[i]);
1711ba2bf218SKurt Hackel 
1712ba2bf218SKurt Hackel 		if (dlm_is_dummy_lock(dlm, ml, &from)) {
1713ba2bf218SKurt Hackel 			/* placeholder, just need to set the refmap bit */
1714ba2bf218SKurt Hackel 			BUG_ON(mres->num_locks != 1);
1715ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: dummy lock for %u\n",
1716ba2bf218SKurt Hackel 			     dlm->name, mres->lockname_len, mres->lockname,
1717ba2bf218SKurt Hackel 			     from);
1718ba2bf218SKurt Hackel 			spin_lock(&res->spinlock);
1719ba2bf218SKurt Hackel 			dlm_lockres_set_refmap_bit(from, res);
1720ba2bf218SKurt Hackel 			spin_unlock(&res->spinlock);
1721ba2bf218SKurt Hackel 			added++;
1722ba2bf218SKurt Hackel 			break;
1723ba2bf218SKurt Hackel 		}
17246714d8e8SKurt Hackel 		BUG_ON(ml->highest_blocked != LKM_IVMODE);
17256714d8e8SKurt Hackel 		newlock = NULL;
17266714d8e8SKurt Hackel 		lksb = NULL;
17276714d8e8SKurt Hackel 
17286714d8e8SKurt Hackel 		queue = dlm_list_num_to_pointer(res, ml->list);
1729e17e75ecSKurt Hackel 		tmpq = NULL;
17306714d8e8SKurt Hackel 
17316714d8e8SKurt Hackel 		/* if the lock is for the local node it needs to
17326714d8e8SKurt Hackel 		 * be moved to the proper location within the queue.
17336714d8e8SKurt Hackel 		 * do not allocate a new lock structure. */
17346714d8e8SKurt Hackel 		if (ml->node == dlm->node_num) {
17356714d8e8SKurt Hackel 			/* MIGRATION ONLY! */
17366714d8e8SKurt Hackel 			BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
17376714d8e8SKurt Hackel 
17386714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
1739e17e75ecSKurt Hackel 			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
1740e17e75ecSKurt Hackel 				tmpq = dlm_list_idx_to_ptr(res, j);
1741800deef3SChristoph Hellwig 				list_for_each_entry(lock, tmpq, list) {
17426714d8e8SKurt Hackel 					if (lock->ml.cookie != ml->cookie)
17436714d8e8SKurt Hackel 						lock = NULL;
17446714d8e8SKurt Hackel 					else
17456714d8e8SKurt Hackel 						break;
17466714d8e8SKurt Hackel 				}
1747e17e75ecSKurt Hackel 				if (lock)
1748e17e75ecSKurt Hackel 					break;
1749e17e75ecSKurt Hackel 			}
17506714d8e8SKurt Hackel 
17516714d8e8SKurt Hackel 			/* lock is always created locally first, and
17526714d8e8SKurt Hackel 			 * destroyed locally last.  it must be on the list */
17536714d8e8SKurt Hackel 			if (!lock) {
1754a7d25539SMark Fasheh 				__be64 c = ml->cookie;
17556714d8e8SKurt Hackel 				mlog(ML_ERROR, "could not find local lock "
175629004858SKurt Hackel 					       "with cookie %u:%llu!\n",
175774aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
175874aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)));
175971ac1062SKurt Hackel 				__dlm_print_one_lock_resource(res);
17606714d8e8SKurt Hackel 				BUG();
17616714d8e8SKurt Hackel 			}
17626714d8e8SKurt Hackel 			BUG_ON(lock->ml.node != ml->node);
17636714d8e8SKurt Hackel 
1764e17e75ecSKurt Hackel 			if (tmpq != queue) {
1765e17e75ecSKurt Hackel 				mlog(0, "lock was on %u instead of %u for %.*s\n",
1766e17e75ecSKurt Hackel 				     j, ml->list, res->lockname.len, res->lockname.name);
1767e17e75ecSKurt Hackel 				spin_unlock(&res->spinlock);
1768e17e75ecSKurt Hackel 				continue;
1769e17e75ecSKurt Hackel 			}
1770e17e75ecSKurt Hackel 
17716714d8e8SKurt Hackel 			/* see NOTE above about why we do not update
17726714d8e8SKurt Hackel 			 * to match the master here */
17736714d8e8SKurt Hackel 
17746714d8e8SKurt Hackel 			/* move the lock to its proper place */
17756714d8e8SKurt Hackel 			/* do not alter lock refcount.  switching lists. */
1776f116629dSAkinobu Mita 			list_move_tail(&lock->list, queue);
17776714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
1778ba2bf218SKurt Hackel 			added++;
17796714d8e8SKurt Hackel 
17806714d8e8SKurt Hackel 			mlog(0, "just reordered a local lock!\n");
17816714d8e8SKurt Hackel 			continue;
17826714d8e8SKurt Hackel 		}
17836714d8e8SKurt Hackel 
17846714d8e8SKurt Hackel 		/* lock is for another node. */
17856714d8e8SKurt Hackel 		newlock = dlm_new_lock(ml->type, ml->node,
17866714d8e8SKurt Hackel 				       be64_to_cpu(ml->cookie), NULL);
17876714d8e8SKurt Hackel 		if (!newlock) {
17886714d8e8SKurt Hackel 			ret = -ENOMEM;
17896714d8e8SKurt Hackel 			goto leave;
17906714d8e8SKurt Hackel 		}
17916714d8e8SKurt Hackel 		lksb = newlock->lksb;
17926714d8e8SKurt Hackel 		dlm_lock_attach_lockres(newlock, res);
17936714d8e8SKurt Hackel 
17946714d8e8SKurt Hackel 		if (ml->convert_type != LKM_IVMODE) {
17956714d8e8SKurt Hackel 			BUG_ON(queue != &res->converting);
17966714d8e8SKurt Hackel 			newlock->ml.convert_type = ml->convert_type;
17976714d8e8SKurt Hackel 		}
17986714d8e8SKurt Hackel 		lksb->flags |= (ml->flags &
17996714d8e8SKurt Hackel 				(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
18006714d8e8SKurt Hackel 
1801ccd8b1f9SKurt Hackel 		if (ml->type == LKM_NLMODE)
1802ccd8b1f9SKurt Hackel 			goto skip_lvb;
1803ccd8b1f9SKurt Hackel 
18048bc674cbSKurt Hackel 		if (!dlm_lvb_is_empty(mres->lvb)) {
18056714d8e8SKurt Hackel 			if (lksb->flags & DLM_LKSB_PUT_LVB) {
18066714d8e8SKurt Hackel 				/* other node was trying to update
18076714d8e8SKurt Hackel 				 * lvb when node died.  recreate the
18086714d8e8SKurt Hackel 				 * lksb with the updated lvb. */
18096714d8e8SKurt Hackel 				memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1810ccd8b1f9SKurt Hackel 				/* the lock resource lvb update must happen
1811ccd8b1f9SKurt Hackel 				 * NOW, before the spinlock is dropped.
1812ccd8b1f9SKurt Hackel 				 * we no longer wait for the AST to update
1813ccd8b1f9SKurt Hackel 				 * the lvb. */
1814ccd8b1f9SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
18156714d8e8SKurt Hackel 			} else {
18166714d8e8SKurt Hackel 				/* otherwise, the node is sending its
18176714d8e8SKurt Hackel 				 * most recent valid lvb info */
18186714d8e8SKurt Hackel 				BUG_ON(ml->type != LKM_EXMODE &&
18196714d8e8SKurt Hackel 				       ml->type != LKM_PRMODE);
18208bc674cbSKurt Hackel 				if (!dlm_lvb_is_empty(res->lvb) &&
18218bc674cbSKurt Hackel  				    (ml->type == LKM_EXMODE ||
18226714d8e8SKurt Hackel  				     memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1823343e26a4SKurt Hackel  					int i;
1824343e26a4SKurt Hackel  					mlog(ML_ERROR, "%s:%.*s: received bad "
1825343e26a4SKurt Hackel  					     "lvb! type=%d\n", dlm->name,
1826343e26a4SKurt Hackel  					     res->lockname.len,
1827343e26a4SKurt Hackel  					     res->lockname.name, ml->type);
1828343e26a4SKurt Hackel  					printk("lockres lvb=[");
1829343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1830343e26a4SKurt Hackel  						printk("%02x", res->lvb[i]);
1831343e26a4SKurt Hackel  					printk("]\nmigrated lvb=[");
1832343e26a4SKurt Hackel  					for (i=0; i<DLM_LVB_LEN; i++)
1833343e26a4SKurt Hackel  						printk("%02x", mres->lvb[i]);
1834343e26a4SKurt Hackel  					printk("]\n");
1835343e26a4SKurt Hackel  					dlm_print_one_lock_resource(res);
18366714d8e8SKurt Hackel  					BUG();
18376714d8e8SKurt Hackel 				}
18386714d8e8SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
18396714d8e8SKurt Hackel 			}
18406714d8e8SKurt Hackel 		}
1841ccd8b1f9SKurt Hackel skip_lvb:
18426714d8e8SKurt Hackel 
18436714d8e8SKurt Hackel 		/* NOTE:
18446714d8e8SKurt Hackel 		 * wrt lock queue ordering and recovery:
18456714d8e8SKurt Hackel 		 *    1. order of locks on granted queue is
18466714d8e8SKurt Hackel 		 *       meaningless.
18476714d8e8SKurt Hackel 		 *    2. order of locks on converting queue is
18486714d8e8SKurt Hackel 		 *       LOST with the node death.  sorry charlie.
18496714d8e8SKurt Hackel 		 *    3. order of locks on the blocked queue is
18506714d8e8SKurt Hackel 		 *       also LOST.
18516714d8e8SKurt Hackel 		 * order of locks does not affect integrity, it
18526714d8e8SKurt Hackel 		 * just means that a lock request may get pushed
18536714d8e8SKurt Hackel 		 * back in line as a result of the node death.
18546714d8e8SKurt Hackel 		 * also note that for a given node the lock order
18556714d8e8SKurt Hackel 		 * for its secondary queue locks is preserved
18566714d8e8SKurt Hackel 		 * relative to each other, but clearly *not*
18576714d8e8SKurt Hackel 		 * preserved relative to locks from other nodes.
18586714d8e8SKurt Hackel 		 */
1859c3187ce5SKurt Hackel 		bad = 0;
18606714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
1861c3187ce5SKurt Hackel 		list_for_each_entry(lock, queue, list) {
1862c3187ce5SKurt Hackel 			if (lock->ml.cookie == ml->cookie) {
1863a7d25539SMark Fasheh 				__be64 c = lock->ml.cookie;
1864c3187ce5SKurt Hackel 				mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
1865c3187ce5SKurt Hackel 				     "exists on this lockres!\n", dlm->name,
1866c3187ce5SKurt Hackel 				     res->lockname.len, res->lockname.name,
186774aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
186874aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)));
1869c3187ce5SKurt Hackel 
1870c3187ce5SKurt Hackel 				mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
1871c3187ce5SKurt Hackel 				     "node=%u, cookie=%u:%llu, queue=%d\n",
1872c3187ce5SKurt Hackel 	      			     ml->type, ml->convert_type, ml->node,
187374aa2585SKurt Hackel 				     dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
187474aa2585SKurt Hackel 				     dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
1875c3187ce5SKurt Hackel 				     ml->list);
1876c3187ce5SKurt Hackel 
1877c3187ce5SKurt Hackel 				__dlm_print_one_lock_resource(res);
1878c3187ce5SKurt Hackel 				bad = 1;
1879c3187ce5SKurt Hackel 				break;
1880c3187ce5SKurt Hackel 			}
1881c3187ce5SKurt Hackel 		}
1882c3187ce5SKurt Hackel 		if (!bad) {
18836714d8e8SKurt Hackel 			dlm_lock_get(newlock);
18846714d8e8SKurt Hackel 			list_add_tail(&newlock->list, queue);
1885ba2bf218SKurt Hackel 			mlog(0, "%s:%.*s: added lock for node %u, "
1886ba2bf218SKurt Hackel 			     "setting refmap bit\n", dlm->name,
1887ba2bf218SKurt Hackel 			     res->lockname.len, res->lockname.name, ml->node);
1888ba2bf218SKurt Hackel 			dlm_lockres_set_refmap_bit(ml->node, res);
1889ba2bf218SKurt Hackel 			added++;
1890c3187ce5SKurt Hackel 		}
18916714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
18926714d8e8SKurt Hackel 	}
18936714d8e8SKurt Hackel 	mlog(0, "done running all the locks\n");
18946714d8e8SKurt Hackel 
18956714d8e8SKurt Hackel leave:
1896ba2bf218SKurt Hackel 	/* balance the ref taken when the work was queued */
1897ba2bf218SKurt Hackel 	spin_lock(&res->spinlock);
1898ba2bf218SKurt Hackel 	dlm_lockres_drop_inflight_ref(dlm, res);
1899ba2bf218SKurt Hackel 	spin_unlock(&res->spinlock);
1900ba2bf218SKurt Hackel 
19016714d8e8SKurt Hackel 	if (ret < 0) {
19026714d8e8SKurt Hackel 		mlog_errno(ret);
19036714d8e8SKurt Hackel 		if (newlock)
19046714d8e8SKurt Hackel 			dlm_lock_put(newlock);
19056714d8e8SKurt Hackel 	}
19066714d8e8SKurt Hackel 
19076714d8e8SKurt Hackel 	mlog_exit(ret);
19086714d8e8SKurt Hackel 	return ret;
19096714d8e8SKurt Hackel }
19106714d8e8SKurt Hackel 
19116714d8e8SKurt Hackel void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
19126714d8e8SKurt Hackel 				       struct dlm_lock_resource *res)
19136714d8e8SKurt Hackel {
19146714d8e8SKurt Hackel 	int i;
1915800deef3SChristoph Hellwig 	struct list_head *queue;
1916800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
19176714d8e8SKurt Hackel 
19186714d8e8SKurt Hackel 	res->state |= DLM_LOCK_RES_RECOVERING;
191969d72b06SKurt Hackel 	if (!list_empty(&res->recovering)) {
192069d72b06SKurt Hackel 		mlog(0,
192169d72b06SKurt Hackel 		     "Recovering res %s:%.*s, is already on recovery list!\n",
192269d72b06SKurt Hackel 		     dlm->name, res->lockname.len, res->lockname.name);
19236714d8e8SKurt Hackel 		list_del_init(&res->recovering);
192469d72b06SKurt Hackel 	}
192569d72b06SKurt Hackel 	/* We need to hold a reference while on the recovery list */
192669d72b06SKurt Hackel 	dlm_lockres_get(res);
19276714d8e8SKurt Hackel 	list_add_tail(&res->recovering, &dlm->reco.resources);
19286714d8e8SKurt Hackel 
19296714d8e8SKurt Hackel 	/* find any pending locks and put them back on proper list */
19306714d8e8SKurt Hackel 	for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
19316714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
1932800deef3SChristoph Hellwig 		list_for_each_entry_safe(lock, next, queue, list) {
19336714d8e8SKurt Hackel 			dlm_lock_get(lock);
19346714d8e8SKurt Hackel 			if (lock->convert_pending) {
19356714d8e8SKurt Hackel 				/* move converting lock back to granted */
19366714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
19376714d8e8SKurt Hackel 				mlog(0, "node died with convert pending "
19386714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
19396714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
19406714d8e8SKurt Hackel 				dlm_revert_pending_convert(res, lock);
19416714d8e8SKurt Hackel 				lock->convert_pending = 0;
19426714d8e8SKurt Hackel 			} else if (lock->lock_pending) {
19436714d8e8SKurt Hackel 				/* remove pending lock requests completely */
19446714d8e8SKurt Hackel 				BUG_ON(i != DLM_BLOCKED_LIST);
19456714d8e8SKurt Hackel 				mlog(0, "node died with lock pending "
19466714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
19476714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
19486714d8e8SKurt Hackel 				/* lock will be floating until ref in
19496714d8e8SKurt Hackel 				 * dlmlock_remote is freed after the network
19506714d8e8SKurt Hackel 				 * call returns.  ok for it to not be on any
19516714d8e8SKurt Hackel 				 * list since no ast can be called
19526714d8e8SKurt Hackel 				 * (the master is dead). */
19536714d8e8SKurt Hackel 				dlm_revert_pending_lock(res, lock);
19546714d8e8SKurt Hackel 				lock->lock_pending = 0;
19556714d8e8SKurt Hackel 			} else if (lock->unlock_pending) {
19566714d8e8SKurt Hackel 				/* if an unlock was in progress, treat as
19576714d8e8SKurt Hackel 				 * if this had completed successfully
19586714d8e8SKurt Hackel 				 * before sending this lock state to the
19596714d8e8SKurt Hackel 				 * new master.  note that the dlm_unlock
19606714d8e8SKurt Hackel 				 * call is still responsible for calling
19616714d8e8SKurt Hackel 				 * the unlockast.  that will happen after
19626714d8e8SKurt Hackel 				 * the network call times out.  for now,
19636714d8e8SKurt Hackel 				 * just move lists to prepare the new
19646714d8e8SKurt Hackel 				 * recovery master.  */
19656714d8e8SKurt Hackel 				BUG_ON(i != DLM_GRANTED_LIST);
19666714d8e8SKurt Hackel 				mlog(0, "node died with unlock pending "
19676714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
19686714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
19696714d8e8SKurt Hackel 				dlm_commit_pending_unlock(res, lock);
19706714d8e8SKurt Hackel 				lock->unlock_pending = 0;
19716714d8e8SKurt Hackel 			} else if (lock->cancel_pending) {
19726714d8e8SKurt Hackel 				/* if a cancel was in progress, treat as
19736714d8e8SKurt Hackel 				 * if this had completed successfully
19746714d8e8SKurt Hackel 				 * before sending this lock state to the
19756714d8e8SKurt Hackel 				 * new master */
19766714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
19776714d8e8SKurt Hackel 				mlog(0, "node died with cancel pending "
19786714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
19796714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
19806714d8e8SKurt Hackel 				dlm_commit_pending_cancel(res, lock);
19816714d8e8SKurt Hackel 				lock->cancel_pending = 0;
19826714d8e8SKurt Hackel 			}
19836714d8e8SKurt Hackel 			dlm_lock_put(lock);
19846714d8e8SKurt Hackel 		}
19856714d8e8SKurt Hackel 	}
19866714d8e8SKurt Hackel }
19876714d8e8SKurt Hackel 
19886714d8e8SKurt Hackel 
19896714d8e8SKurt Hackel 
19906714d8e8SKurt Hackel /* removes all recovered locks from the recovery list.
19916714d8e8SKurt Hackel  * sets the res->owner to the new master.
19926714d8e8SKurt Hackel  * unsets the RECOVERY flag and wakes waiters. */
19936714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
19946714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master)
19956714d8e8SKurt Hackel {
19966714d8e8SKurt Hackel 	int i;
199781f2094aSMark Fasheh 	struct hlist_node *hash_iter;
199881f2094aSMark Fasheh 	struct hlist_head *bucket;
1999800deef3SChristoph Hellwig 	struct dlm_lock_resource *res, *next;
20006714d8e8SKurt Hackel 
20016714d8e8SKurt Hackel 	mlog_entry_void();
20026714d8e8SKurt Hackel 
20036714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
20046714d8e8SKurt Hackel 
2005800deef3SChristoph Hellwig 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
20066714d8e8SKurt Hackel 		if (res->owner == dead_node) {
20076714d8e8SKurt Hackel 			list_del_init(&res->recovering);
20086714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
2009ba2bf218SKurt Hackel 			/* new_master has our reference from
2010ba2bf218SKurt Hackel 			 * the lock state sent during recovery */
20116714d8e8SKurt Hackel 			dlm_change_lockres_owner(dlm, res, new_master);
20126714d8e8SKurt Hackel 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2013ba2bf218SKurt Hackel 			if (__dlm_lockres_has_locks(res))
20146714d8e8SKurt Hackel 				__dlm_dirty_lockres(dlm, res);
20156714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
20166714d8e8SKurt Hackel 			wake_up(&res->wq);
201769d72b06SKurt Hackel 			dlm_lockres_put(res);
20186714d8e8SKurt Hackel 		}
20196714d8e8SKurt Hackel 	}
20206714d8e8SKurt Hackel 
20216714d8e8SKurt Hackel 	/* this will become unnecessary eventually, but
20226714d8e8SKurt Hackel 	 * for now we need to run the whole hash, clear
20236714d8e8SKurt Hackel 	 * the RECOVERING state and set the owner
20246714d8e8SKurt Hackel 	 * if necessary */
202581f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
202603d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
202781f2094aSMark Fasheh 		hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
20286714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_RECOVERING) {
20296714d8e8SKurt Hackel 				if (res->owner == dead_node) {
20306714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
20316714d8e8SKurt Hackel 					     "was not on recovering list, but "
20326714d8e8SKurt Hackel 					     "clearing state anyway\n",
20336714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
20346714d8e8SKurt Hackel 					     res->lockname.name, new_master);
20356714d8e8SKurt Hackel 				} else if (res->owner == dlm->node_num) {
20366714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
20376714d8e8SKurt Hackel 					     "was not on recovering list, "
20386714d8e8SKurt Hackel 					     "owner is THIS node, clearing\n",
20396714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
20406714d8e8SKurt Hackel 					     res->lockname.name, new_master);
20416714d8e8SKurt Hackel 				} else
20426714d8e8SKurt Hackel 					continue;
20436714d8e8SKurt Hackel 
2044c03872f5SKurt Hackel 				if (!list_empty(&res->recovering)) {
2045c03872f5SKurt Hackel 					mlog(0, "%s:%.*s: lockres was "
2046c03872f5SKurt Hackel 					     "marked RECOVERING, owner=%u\n",
2047c03872f5SKurt Hackel 					     dlm->name, res->lockname.len,
2048c03872f5SKurt Hackel 					     res->lockname.name, res->owner);
2049c03872f5SKurt Hackel 					list_del_init(&res->recovering);
205069d72b06SKurt Hackel 					dlm_lockres_put(res);
2051c03872f5SKurt Hackel 				}
20526714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
2053ba2bf218SKurt Hackel 				/* new_master has our reference from
2054ba2bf218SKurt Hackel 				 * the lock state sent during recovery */
20556714d8e8SKurt Hackel 				dlm_change_lockres_owner(dlm, res, new_master);
20566714d8e8SKurt Hackel 				res->state &= ~DLM_LOCK_RES_RECOVERING;
2057ba2bf218SKurt Hackel 				if (__dlm_lockres_has_locks(res))
20586714d8e8SKurt Hackel 					__dlm_dirty_lockres(dlm, res);
20596714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
20606714d8e8SKurt Hackel 				wake_up(&res->wq);
20616714d8e8SKurt Hackel 			}
20626714d8e8SKurt Hackel 		}
20636714d8e8SKurt Hackel 	}
20646714d8e8SKurt Hackel }
20656714d8e8SKurt Hackel 
20666714d8e8SKurt Hackel static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
20676714d8e8SKurt Hackel {
20686714d8e8SKurt Hackel 	if (local) {
20696714d8e8SKurt Hackel 		if (lock->ml.type != LKM_EXMODE &&
20706714d8e8SKurt Hackel 		    lock->ml.type != LKM_PRMODE)
20716714d8e8SKurt Hackel 			return 1;
20726714d8e8SKurt Hackel 	} else if (lock->ml.type == LKM_EXMODE)
20736714d8e8SKurt Hackel 		return 1;
20746714d8e8SKurt Hackel 	return 0;
20756714d8e8SKurt Hackel }
20766714d8e8SKurt Hackel 
20776714d8e8SKurt Hackel static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
20786714d8e8SKurt Hackel 			       struct dlm_lock_resource *res, u8 dead_node)
20796714d8e8SKurt Hackel {
2080800deef3SChristoph Hellwig 	struct list_head *queue;
20816714d8e8SKurt Hackel 	struct dlm_lock *lock;
20826714d8e8SKurt Hackel 	int blank_lvb = 0, local = 0;
20836714d8e8SKurt Hackel 	int i;
20846714d8e8SKurt Hackel 	u8 search_node;
20856714d8e8SKurt Hackel 
20866714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
20876714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
20886714d8e8SKurt Hackel 
20896714d8e8SKurt Hackel 	if (res->owner == dlm->node_num)
20906714d8e8SKurt Hackel 		/* if this node owned the lockres, and if the dead node
20916714d8e8SKurt Hackel 		 * had an EX when he died, blank out the lvb */
20926714d8e8SKurt Hackel 		search_node = dead_node;
20936714d8e8SKurt Hackel 	else {
20946714d8e8SKurt Hackel 		/* if this is a secondary lockres, and we had no EX or PR
20956714d8e8SKurt Hackel 		 * locks granted, we can no longer trust the lvb */
20966714d8e8SKurt Hackel 		search_node = dlm->node_num;
20976714d8e8SKurt Hackel 		local = 1;  /* check local state for valid lvb */
20986714d8e8SKurt Hackel 	}
20996714d8e8SKurt Hackel 
21006714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
21016714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
2102800deef3SChristoph Hellwig 		list_for_each_entry(lock, queue, list) {
21036714d8e8SKurt Hackel 			if (lock->ml.node == search_node) {
21046714d8e8SKurt Hackel 				if (dlm_lvb_needs_invalidation(lock, local)) {
21056714d8e8SKurt Hackel 					/* zero the lksb lvb and lockres lvb */
21066714d8e8SKurt Hackel 					blank_lvb = 1;
21076714d8e8SKurt Hackel 					memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
21086714d8e8SKurt Hackel 				}
21096714d8e8SKurt Hackel 			}
21106714d8e8SKurt Hackel 		}
21116714d8e8SKurt Hackel 	}
21126714d8e8SKurt Hackel 
21136714d8e8SKurt Hackel 	if (blank_lvb) {
21146714d8e8SKurt Hackel 		mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
21156714d8e8SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
21166714d8e8SKurt Hackel 		memset(res->lvb, 0, DLM_LVB_LEN);
21176714d8e8SKurt Hackel 	}
21186714d8e8SKurt Hackel }
21196714d8e8SKurt Hackel 
21206714d8e8SKurt Hackel static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
21216714d8e8SKurt Hackel 				struct dlm_lock_resource *res, u8 dead_node)
21226714d8e8SKurt Hackel {
2123800deef3SChristoph Hellwig 	struct dlm_lock *lock, *next;
2124ba2bf218SKurt Hackel 	unsigned int freed = 0;
21256714d8e8SKurt Hackel 
21266714d8e8SKurt Hackel 	/* this node is the lockres master:
21276714d8e8SKurt Hackel 	 * 1) remove any stale locks for the dead node
21286714d8e8SKurt Hackel 	 * 2) if the dead node had an EX when he died, blank out the lvb
21296714d8e8SKurt Hackel 	 */
21306714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
21316714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
21326714d8e8SKurt Hackel 
21336714d8e8SKurt Hackel 	/* TODO: check pending_asts, pending_basts here */
2134800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->granted, list) {
21356714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
21366714d8e8SKurt Hackel 			list_del_init(&lock->list);
21376714d8e8SKurt Hackel 			dlm_lock_put(lock);
2138ba2bf218SKurt Hackel 			freed++;
21396714d8e8SKurt Hackel 		}
21406714d8e8SKurt Hackel 	}
2141800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->converting, list) {
21426714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
21436714d8e8SKurt Hackel 			list_del_init(&lock->list);
21446714d8e8SKurt Hackel 			dlm_lock_put(lock);
2145ba2bf218SKurt Hackel 			freed++;
21466714d8e8SKurt Hackel 		}
21476714d8e8SKurt Hackel 	}
2148800deef3SChristoph Hellwig 	list_for_each_entry_safe(lock, next, &res->blocked, list) {
21496714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
21506714d8e8SKurt Hackel 			list_del_init(&lock->list);
21516714d8e8SKurt Hackel 			dlm_lock_put(lock);
2152ba2bf218SKurt Hackel 			freed++;
21536714d8e8SKurt Hackel 		}
21546714d8e8SKurt Hackel 	}
21556714d8e8SKurt Hackel 
2156ba2bf218SKurt Hackel 	if (freed) {
2157ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2158ba2bf218SKurt Hackel 		     "dropping ref from lockres\n", dlm->name,
2159ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, freed, dead_node);
2160ba2bf218SKurt Hackel 		BUG_ON(!test_bit(dead_node, res->refmap));
2161ba2bf218SKurt Hackel 		dlm_lockres_clear_refmap_bit(dead_node, res);
2162ba2bf218SKurt Hackel 	} else if (test_bit(dead_node, res->refmap)) {
2163ba2bf218SKurt Hackel 		mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2164ba2bf218SKurt Hackel 		     "no locks and had not purged before dying\n", dlm->name,
2165ba2bf218SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
2166ba2bf218SKurt Hackel 		dlm_lockres_clear_refmap_bit(dead_node, res);
2167ba2bf218SKurt Hackel 	}
2168ba2bf218SKurt Hackel 
21696714d8e8SKurt Hackel 	/* do not kick thread yet */
21706714d8e8SKurt Hackel 	__dlm_dirty_lockres(dlm, res);
21716714d8e8SKurt Hackel }
21726714d8e8SKurt Hackel 
21736714d8e8SKurt Hackel /* if this node is the recovery master, and there are no
21746714d8e8SKurt Hackel  * locks for a given lockres owned by this node that are in
21756714d8e8SKurt Hackel  * either PR or EX mode, zero out the lvb before requesting.
21766714d8e8SKurt Hackel  *
21776714d8e8SKurt Hackel  */
21786714d8e8SKurt Hackel 
21796714d8e8SKurt Hackel 
21806714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
21816714d8e8SKurt Hackel {
218281f2094aSMark Fasheh 	struct hlist_node *iter;
21836714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
21846714d8e8SKurt Hackel 	int i;
218581f2094aSMark Fasheh 	struct hlist_head *bucket;
2186e2faea4cSKurt Hackel 	struct dlm_lock *lock;
21876714d8e8SKurt Hackel 
21886714d8e8SKurt Hackel 
21896714d8e8SKurt Hackel 	/* purge any stale mles */
21906714d8e8SKurt Hackel 	dlm_clean_master_list(dlm, dead_node);
21916714d8e8SKurt Hackel 
21926714d8e8SKurt Hackel 	/*
21936714d8e8SKurt Hackel 	 * now clean up all lock resources.  there are two rules:
21946714d8e8SKurt Hackel 	 *
21956714d8e8SKurt Hackel 	 * 1) if the dead node was the master, move the lockres
21966714d8e8SKurt Hackel 	 *    to the recovering list.  set the RECOVERING flag.
21976714d8e8SKurt Hackel 	 *    this lockres needs to be cleaned up before it can
21986714d8e8SKurt Hackel 	 *    be used further.
21996714d8e8SKurt Hackel 	 *
22006714d8e8SKurt Hackel 	 * 2) if this node was the master, remove all locks from
22016714d8e8SKurt Hackel 	 *    each of the lockres queues that were owned by the
22026714d8e8SKurt Hackel 	 *    dead node.  once recovery finishes, the dlm thread
22036714d8e8SKurt Hackel 	 *    can be kicked again to see if any ASTs or BASTs
22046714d8e8SKurt Hackel 	 *    need to be fired as a result.
22056714d8e8SKurt Hackel 	 */
220681f2094aSMark Fasheh 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
220703d864c0SDaniel Phillips 		bucket = dlm_lockres_hash(dlm, i);
220881f2094aSMark Fasheh 		hlist_for_each_entry(res, iter, bucket, hash_node) {
2209e2faea4cSKurt Hackel  			/* always prune any $RECOVERY entries for dead nodes,
2210e2faea4cSKurt Hackel  			 * otherwise hangs can occur during later recovery */
22116714d8e8SKurt Hackel 			if (dlm_is_recovery_lock(res->lockname.name,
2212e2faea4cSKurt Hackel 						 res->lockname.len)) {
2213e2faea4cSKurt Hackel 				spin_lock(&res->spinlock);
2214e2faea4cSKurt Hackel 				list_for_each_entry(lock, &res->granted, list) {
2215e2faea4cSKurt Hackel 					if (lock->ml.node == dead_node) {
2216e2faea4cSKurt Hackel 						mlog(0, "AHA! there was "
2217e2faea4cSKurt Hackel 						     "a $RECOVERY lock for dead "
2218e2faea4cSKurt Hackel 						     "node %u (%s)!\n",
2219e2faea4cSKurt Hackel 						     dead_node, dlm->name);
2220e2faea4cSKurt Hackel 						list_del_init(&lock->list);
2221e2faea4cSKurt Hackel 						dlm_lock_put(lock);
2222e2faea4cSKurt Hackel 						break;
2223e2faea4cSKurt Hackel 					}
2224e2faea4cSKurt Hackel 				}
2225e2faea4cSKurt Hackel 				spin_unlock(&res->spinlock);
22266714d8e8SKurt Hackel 				continue;
2227e2faea4cSKurt Hackel 			}
22286714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
22296714d8e8SKurt Hackel 			/* zero the lvb if necessary */
22306714d8e8SKurt Hackel 			dlm_revalidate_lvb(dlm, res, dead_node);
2231ba2bf218SKurt Hackel 			if (res->owner == dead_node) {
2232ba2bf218SKurt Hackel 				if (res->state & DLM_LOCK_RES_DROPPING_REF)
2233ba2bf218SKurt Hackel 					mlog(0, "%s:%.*s: owned by "
2234ba2bf218SKurt Hackel 					     "dead node %u, this node was "
2235ba2bf218SKurt Hackel 					     "dropping its ref when it died. "
2236ba2bf218SKurt Hackel 					     "continue, dropping the flag.\n",
2237ba2bf218SKurt Hackel 					     dlm->name, res->lockname.len,
2238ba2bf218SKurt Hackel 					     res->lockname.name, dead_node);
2239ba2bf218SKurt Hackel 
2240ba2bf218SKurt Hackel 				/* the wake_up for this will happen when the
2241ba2bf218SKurt Hackel 				 * RECOVERING flag is dropped later */
2242ba2bf218SKurt Hackel 				res->state &= ~DLM_LOCK_RES_DROPPING_REF;
2243ba2bf218SKurt Hackel 
22446714d8e8SKurt Hackel 				dlm_move_lockres_to_recovery_list(dlm, res);
2245ba2bf218SKurt Hackel 			} else if (res->owner == dlm->node_num) {
22466714d8e8SKurt Hackel 				dlm_free_dead_locks(dlm, res, dead_node);
22476714d8e8SKurt Hackel 				__dlm_lockres_calc_usage(dlm, res);
22486714d8e8SKurt Hackel 			}
22496714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
22506714d8e8SKurt Hackel 		}
22516714d8e8SKurt Hackel 	}
22526714d8e8SKurt Hackel 
22536714d8e8SKurt Hackel }
22546714d8e8SKurt Hackel 
22556714d8e8SKurt Hackel static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
22566714d8e8SKurt Hackel {
22576714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
22586714d8e8SKurt Hackel 
2259466d1a45SKurt Hackel 	if (dlm->reco.new_master == idx) {
2260466d1a45SKurt Hackel 		mlog(0, "%s: recovery master %d just died\n",
2261466d1a45SKurt Hackel 		     dlm->name, idx);
2262466d1a45SKurt Hackel 		if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2263466d1a45SKurt Hackel 			/* finalize1 was reached, so it is safe to clear
2264466d1a45SKurt Hackel 			 * the new_master and dead_node.  that recovery
2265466d1a45SKurt Hackel 			 * is complete. */
2266466d1a45SKurt Hackel 			mlog(0, "%s: dead master %d had reached "
2267466d1a45SKurt Hackel 			     "finalize1 state, clearing\n", dlm->name, idx);
2268466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2269466d1a45SKurt Hackel 			__dlm_reset_recovery(dlm);
2270466d1a45SKurt Hackel 		}
2271466d1a45SKurt Hackel 	}
2272466d1a45SKurt Hackel 
22736714d8e8SKurt Hackel 	/* check to see if the node is already considered dead */
22746714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->live_nodes_map)) {
22756714d8e8SKurt Hackel 		mlog(0, "for domain %s, node %d is already dead. "
22766714d8e8SKurt Hackel 		     "another node likely did recovery already.\n",
22776714d8e8SKurt Hackel 		     dlm->name, idx);
22786714d8e8SKurt Hackel 		return;
22796714d8e8SKurt Hackel 	}
22806714d8e8SKurt Hackel 
22816714d8e8SKurt Hackel 	/* check to see if we do not care about this node */
22826714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->domain_map)) {
22836714d8e8SKurt Hackel 		/* This also catches the case that we get a node down
22846714d8e8SKurt Hackel 		 * but haven't joined the domain yet. */
22856714d8e8SKurt Hackel 		mlog(0, "node %u already removed from domain!\n", idx);
22866714d8e8SKurt Hackel 		return;
22876714d8e8SKurt Hackel 	}
22886714d8e8SKurt Hackel 
22896714d8e8SKurt Hackel 	clear_bit(idx, dlm->live_nodes_map);
22906714d8e8SKurt Hackel 
22916714d8e8SKurt Hackel 	/* Clean up join state on node death. */
22926714d8e8SKurt Hackel 	if (dlm->joining_node == idx) {
22936714d8e8SKurt Hackel 		mlog(0, "Clearing join state for node %u\n", idx);
22946714d8e8SKurt Hackel 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
22956714d8e8SKurt Hackel 	}
22966714d8e8SKurt Hackel 
22976714d8e8SKurt Hackel 	/* make sure local cleanup occurs before the heartbeat events */
22986714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->recovery_map))
22996714d8e8SKurt Hackel 		dlm_do_local_recovery_cleanup(dlm, idx);
23006714d8e8SKurt Hackel 
23016714d8e8SKurt Hackel 	/* notify anything attached to the heartbeat events */
23026714d8e8SKurt Hackel 	dlm_hb_event_notify_attached(dlm, idx, 0);
23036714d8e8SKurt Hackel 
23046714d8e8SKurt Hackel 	mlog(0, "node %u being removed from domain map!\n", idx);
23056714d8e8SKurt Hackel 	clear_bit(idx, dlm->domain_map);
23066714d8e8SKurt Hackel 	/* wake up migration waiters if a node goes down.
23076714d8e8SKurt Hackel 	 * perhaps later we can genericize this for other waiters. */
23086714d8e8SKurt Hackel 	wake_up(&dlm->migration_wq);
23096714d8e8SKurt Hackel 
23106714d8e8SKurt Hackel 	if (test_bit(idx, dlm->recovery_map))
23116714d8e8SKurt Hackel 		mlog(0, "domain %s, node %u already added "
23126714d8e8SKurt Hackel 		     "to recovery map!\n", dlm->name, idx);
23136714d8e8SKurt Hackel 	else
23146714d8e8SKurt Hackel 		set_bit(idx, dlm->recovery_map);
23156714d8e8SKurt Hackel }
23166714d8e8SKurt Hackel 
23176714d8e8SKurt Hackel void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
23186714d8e8SKurt Hackel {
23196714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
23206714d8e8SKurt Hackel 
23216714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
23226714d8e8SKurt Hackel 		return;
23236714d8e8SKurt Hackel 
23246561168cSMark Fasheh 	/*
23256561168cSMark Fasheh 	 * This will notify any dlm users that a node in our domain
23266561168cSMark Fasheh 	 * went away without notifying us first.
23276561168cSMark Fasheh 	 */
23286561168cSMark Fasheh 	if (test_bit(idx, dlm->domain_map))
23296561168cSMark Fasheh 		dlm_fire_domain_eviction_callbacks(dlm, idx);
23306561168cSMark Fasheh 
23316714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
23326714d8e8SKurt Hackel 	__dlm_hb_node_down(dlm, idx);
23336714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
23346714d8e8SKurt Hackel 
23356714d8e8SKurt Hackel 	dlm_put(dlm);
23366714d8e8SKurt Hackel }
23376714d8e8SKurt Hackel 
23386714d8e8SKurt Hackel void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
23396714d8e8SKurt Hackel {
23406714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
23416714d8e8SKurt Hackel 
23426714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
23436714d8e8SKurt Hackel 		return;
23446714d8e8SKurt Hackel 
23456714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
23466714d8e8SKurt Hackel 	set_bit(idx, dlm->live_nodes_map);
2347e2faea4cSKurt Hackel 	/* do NOT notify mle attached to the heartbeat events.
2348e2faea4cSKurt Hackel 	 * new nodes are not interesting in mastery until joined. */
23496714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
23506714d8e8SKurt Hackel 
23516714d8e8SKurt Hackel 	dlm_put(dlm);
23526714d8e8SKurt Hackel }
23536714d8e8SKurt Hackel 
23546714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata)
23556714d8e8SKurt Hackel {
23566714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
23576714d8e8SKurt Hackel 	mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
23586714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
23596714d8e8SKurt Hackel }
23606714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type)
23616714d8e8SKurt Hackel {
23626714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
23636714d8e8SKurt Hackel 	mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
23646714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
23656714d8e8SKurt Hackel }
23666714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
23676714d8e8SKurt Hackel {
23686714d8e8SKurt Hackel 	mlog(0, "unlockast for recovery lock fired!\n");
23696714d8e8SKurt Hackel }
23706714d8e8SKurt Hackel 
2371e2faea4cSKurt Hackel /*
2372e2faea4cSKurt Hackel  * dlm_pick_recovery_master will continually attempt to use
2373e2faea4cSKurt Hackel  * dlmlock() on the special "$RECOVERY" lockres with the
2374e2faea4cSKurt Hackel  * LKM_NOQUEUE flag to get an EX.  every thread that enters
2375e2faea4cSKurt Hackel  * this function on each node racing to become the recovery
2376e2faea4cSKurt Hackel  * master will not stop attempting this until either:
2377e2faea4cSKurt Hackel  * a) this node gets the EX (and becomes the recovery master),
2378e2faea4cSKurt Hackel  * or b) dlm->reco.new_master gets set to some nodenum
2379e2faea4cSKurt Hackel  * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2380e2faea4cSKurt Hackel  * so each time a recovery master is needed, the entire cluster
2381e2faea4cSKurt Hackel  * will sync at this point.  if the new master dies, that will
2382e2faea4cSKurt Hackel  * be detected in dlm_do_recovery */
23836714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
23846714d8e8SKurt Hackel {
23856714d8e8SKurt Hackel 	enum dlm_status ret;
23866714d8e8SKurt Hackel 	struct dlm_lockstatus lksb;
23876714d8e8SKurt Hackel 	int status = -EINVAL;
23886714d8e8SKurt Hackel 
23896714d8e8SKurt Hackel 	mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
23906714d8e8SKurt Hackel 	     dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2391e2faea4cSKurt Hackel again:
23926714d8e8SKurt Hackel 	memset(&lksb, 0, sizeof(lksb));
23936714d8e8SKurt Hackel 
23946714d8e8SKurt Hackel 	ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
23953384f3dfSMark Fasheh 		      DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
23963384f3dfSMark Fasheh 		      dlm_reco_ast, dlm, dlm_reco_bast);
23976714d8e8SKurt Hackel 
2398e2faea4cSKurt Hackel 	mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2399e2faea4cSKurt Hackel 	     dlm->name, ret, lksb.status);
2400e2faea4cSKurt Hackel 
24016714d8e8SKurt Hackel 	if (ret == DLM_NORMAL) {
24026714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
24036714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
2404e2faea4cSKurt Hackel 
2405e2faea4cSKurt Hackel 		/* got the EX lock.  check to see if another node
2406e2faea4cSKurt Hackel 		 * just became the reco master */
2407e2faea4cSKurt Hackel 		if (dlm_reco_master_ready(dlm)) {
2408e2faea4cSKurt Hackel 			mlog(0, "%s: got reco EX lock, but %u will "
2409e2faea4cSKurt Hackel 			     "do the recovery\n", dlm->name,
2410e2faea4cSKurt Hackel 			     dlm->reco.new_master);
2411e2faea4cSKurt Hackel 			status = -EEXIST;
2412e2faea4cSKurt Hackel 		} else {
2413898effacSKurt Hackel 			status = 0;
2414898effacSKurt Hackel 
2415898effacSKurt Hackel 			/* see if recovery was already finished elsewhere */
2416898effacSKurt Hackel 			spin_lock(&dlm->spinlock);
2417898effacSKurt Hackel 			if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2418898effacSKurt Hackel 				status = -EINVAL;
2419898effacSKurt Hackel 				mlog(0, "%s: got reco EX lock, but "
2420898effacSKurt Hackel 				     "node got recovered already\n", dlm->name);
2421898effacSKurt Hackel 				if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2422898effacSKurt Hackel 					mlog(ML_ERROR, "%s: new master is %u "
2423898effacSKurt Hackel 					     "but no dead node!\n",
2424898effacSKurt Hackel 					     dlm->name, dlm->reco.new_master);
2425898effacSKurt Hackel 					BUG();
2426898effacSKurt Hackel 				}
2427898effacSKurt Hackel 			}
2428898effacSKurt Hackel 			spin_unlock(&dlm->spinlock);
2429898effacSKurt Hackel 		}
2430898effacSKurt Hackel 
2431898effacSKurt Hackel 		/* if this node has actually become the recovery master,
2432898effacSKurt Hackel 		 * set the master and send the messages to begin recovery */
2433898effacSKurt Hackel 		if (!status) {
2434898effacSKurt Hackel 			mlog(0, "%s: dead=%u, this=%u, sending "
2435898effacSKurt Hackel 			     "begin_reco now\n", dlm->name,
2436898effacSKurt Hackel 			     dlm->reco.dead_node, dlm->node_num);
24376714d8e8SKurt Hackel 			status = dlm_send_begin_reco_message(dlm,
24386714d8e8SKurt Hackel 				      dlm->reco.dead_node);
2439e2faea4cSKurt Hackel 			/* this always succeeds */
2440e2faea4cSKurt Hackel 			BUG_ON(status);
2441e2faea4cSKurt Hackel 
2442e2faea4cSKurt Hackel 			/* set the new_master to this node */
2443e2faea4cSKurt Hackel 			spin_lock(&dlm->spinlock);
2444ab27eb6fSKurt Hackel 			dlm_set_reco_master(dlm, dlm->node_num);
2445e2faea4cSKurt Hackel 			spin_unlock(&dlm->spinlock);
2446e2faea4cSKurt Hackel 		}
24476714d8e8SKurt Hackel 
24486714d8e8SKurt Hackel 		/* recovery lock is a special case.  ast will not get fired,
24496714d8e8SKurt Hackel 		 * so just go ahead and unlock it. */
24506714d8e8SKurt Hackel 		ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2451e2faea4cSKurt Hackel 		if (ret == DLM_DENIED) {
2452e2faea4cSKurt Hackel 			mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2453e2faea4cSKurt Hackel 			ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2454e2faea4cSKurt Hackel 		}
24556714d8e8SKurt Hackel 		if (ret != DLM_NORMAL) {
24566714d8e8SKurt Hackel 			/* this would really suck. this could only happen
24576714d8e8SKurt Hackel 			 * if there was a network error during the unlock
24586714d8e8SKurt Hackel 			 * because of node death.  this means the unlock
24596714d8e8SKurt Hackel 			 * is actually "done" and the lock structure is
24606714d8e8SKurt Hackel 			 * even freed.  we can continue, but only
24616714d8e8SKurt Hackel 			 * because this specific lock name is special. */
2462e2faea4cSKurt Hackel 			mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
24636714d8e8SKurt Hackel 		}
24646714d8e8SKurt Hackel 	} else if (ret == DLM_NOTQUEUED) {
24656714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
24666714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
24676714d8e8SKurt Hackel 		/* another node is master. wait on
2468e2faea4cSKurt Hackel 		 * reco.new_master != O2NM_INVALID_NODE_NUM
2469e2faea4cSKurt Hackel 		 * for at most one second */
2470e2faea4cSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
2471e2faea4cSKurt Hackel 					 dlm_reco_master_ready(dlm),
2472e2faea4cSKurt Hackel 					 msecs_to_jiffies(1000));
2473e2faea4cSKurt Hackel 		if (!dlm_reco_master_ready(dlm)) {
2474e2faea4cSKurt Hackel 			mlog(0, "%s: reco master taking awhile\n",
2475e2faea4cSKurt Hackel 			     dlm->name);
2476e2faea4cSKurt Hackel 			goto again;
2477e2faea4cSKurt Hackel 		}
2478e2faea4cSKurt Hackel 		/* another node has informed this one that it is reco master */
2479e2faea4cSKurt Hackel 		mlog(0, "%s: reco master %u is ready to recover %u\n",
2480e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
24816714d8e8SKurt Hackel 		status = -EEXIST;
2482c8df412eSKurt Hackel 	} else if (ret == DLM_RECOVERING) {
2483c8df412eSKurt Hackel 		mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2484c8df412eSKurt Hackel 		     dlm->name, dlm->node_num);
2485c8df412eSKurt Hackel 		goto again;
2486e2faea4cSKurt Hackel 	} else {
2487e2faea4cSKurt Hackel 		struct dlm_lock_resource *res;
2488e2faea4cSKurt Hackel 
2489e2faea4cSKurt Hackel 		/* dlmlock returned something other than NOTQUEUED or NORMAL */
2490e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2491e2faea4cSKurt Hackel 		     "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2492e2faea4cSKurt Hackel 		     dlm_errname(lksb.status));
2493e2faea4cSKurt Hackel 		res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2494e2faea4cSKurt Hackel 					 DLM_RECOVERY_LOCK_NAME_LEN);
2495e2faea4cSKurt Hackel 		if (res) {
2496e2faea4cSKurt Hackel 			dlm_print_one_lock_resource(res);
2497e2faea4cSKurt Hackel 			dlm_lockres_put(res);
2498e2faea4cSKurt Hackel 		} else {
2499e2faea4cSKurt Hackel 			mlog(ML_ERROR, "recovery lock not found\n");
2500e2faea4cSKurt Hackel 		}
2501e2faea4cSKurt Hackel 		BUG();
25026714d8e8SKurt Hackel 	}
25036714d8e8SKurt Hackel 
25046714d8e8SKurt Hackel 	return status;
25056714d8e8SKurt Hackel }
25066714d8e8SKurt Hackel 
25076714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
25086714d8e8SKurt Hackel {
25096714d8e8SKurt Hackel 	struct dlm_begin_reco br;
25106714d8e8SKurt Hackel 	int ret = 0;
25116714d8e8SKurt Hackel 	struct dlm_node_iter iter;
25126714d8e8SKurt Hackel 	int nodenum;
25136714d8e8SKurt Hackel 	int status;
25146714d8e8SKurt Hackel 
25156714d8e8SKurt Hackel 	mlog_entry("%u\n", dead_node);
25166714d8e8SKurt Hackel 
2517d6dea6e9SKurt Hackel 	mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
25186714d8e8SKurt Hackel 
25196714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
25206714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
25216714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
25226714d8e8SKurt Hackel 
25236714d8e8SKurt Hackel 	clear_bit(dead_node, iter.node_map);
25246714d8e8SKurt Hackel 
25256714d8e8SKurt Hackel 	memset(&br, 0, sizeof(br));
25266714d8e8SKurt Hackel 	br.node_idx = dlm->node_num;
25276714d8e8SKurt Hackel 	br.dead_node = dead_node;
25286714d8e8SKurt Hackel 
25296714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
25306714d8e8SKurt Hackel 		ret = 0;
25316714d8e8SKurt Hackel 		if (nodenum == dead_node) {
25326714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to dead node "
25336714d8e8SKurt Hackel 				  "%u\n", dead_node);
25346714d8e8SKurt Hackel 			continue;
25356714d8e8SKurt Hackel 		}
25366714d8e8SKurt Hackel 		if (nodenum == dlm->node_num) {
25376714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to self\n");
25386714d8e8SKurt Hackel 			continue;
25396714d8e8SKurt Hackel 		}
2540e2faea4cSKurt Hackel retry:
25416714d8e8SKurt Hackel 		ret = -EINVAL;
25426714d8e8SKurt Hackel 		mlog(0, "attempting to send begin reco msg to %d\n",
25436714d8e8SKurt Hackel 			  nodenum);
25446714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
25456714d8e8SKurt Hackel 					 &br, sizeof(br), nodenum, &status);
25466714d8e8SKurt Hackel 		/* negative status is handled ok by caller here */
25476714d8e8SKurt Hackel 		if (ret >= 0)
25486714d8e8SKurt Hackel 			ret = status;
2549e2faea4cSKurt Hackel 		if (dlm_is_host_down(ret)) {
2550e2faea4cSKurt Hackel 			/* node is down.  not involved in recovery
2551e2faea4cSKurt Hackel 			 * so just keep going */
2552e2faea4cSKurt Hackel 			mlog(0, "%s: node %u was down when sending "
2553e2faea4cSKurt Hackel 			     "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2554e2faea4cSKurt Hackel 			ret = 0;
2555e2faea4cSKurt Hackel 		}
25566714d8e8SKurt Hackel 		if (ret < 0) {
25576714d8e8SKurt Hackel 			struct dlm_lock_resource *res;
2558e2faea4cSKurt Hackel 			/* this is now a serious problem, possibly ENOMEM
2559e2faea4cSKurt Hackel 			 * in the network stack.  must retry */
25606714d8e8SKurt Hackel 			mlog_errno(ret);
25616714d8e8SKurt Hackel 			mlog(ML_ERROR, "begin reco of dlm %s to node %u "
25626714d8e8SKurt Hackel 			    " returned %d\n", dlm->name, nodenum, ret);
25636714d8e8SKurt Hackel 			res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
25646714d8e8SKurt Hackel 						 DLM_RECOVERY_LOCK_NAME_LEN);
25656714d8e8SKurt Hackel 			if (res) {
25666714d8e8SKurt Hackel 				dlm_print_one_lock_resource(res);
25676714d8e8SKurt Hackel 				dlm_lockres_put(res);
25686714d8e8SKurt Hackel 			} else {
25696714d8e8SKurt Hackel 				mlog(ML_ERROR, "recovery lock not found\n");
25706714d8e8SKurt Hackel 			}
2571e2faea4cSKurt Hackel 			/* sleep for a bit in hopes that we can avoid
2572e2faea4cSKurt Hackel 			 * another ENOMEM */
2573e2faea4cSKurt Hackel 			msleep(100);
2574e2faea4cSKurt Hackel 			goto retry;
2575466d1a45SKurt Hackel 		} else if (ret == EAGAIN) {
2576466d1a45SKurt Hackel 			mlog(0, "%s: trying to start recovery of node "
2577466d1a45SKurt Hackel 			     "%u, but node %u is waiting for last recovery "
2578466d1a45SKurt Hackel 			     "to complete, backoff for a bit\n", dlm->name,
2579466d1a45SKurt Hackel 			     dead_node, nodenum);
2580466d1a45SKurt Hackel 			/* TODO Look into replacing msleep with cond_resched() */
2581466d1a45SKurt Hackel 			msleep(100);
2582466d1a45SKurt Hackel 			goto retry;
25836714d8e8SKurt Hackel 		}
25846714d8e8SKurt Hackel 	}
25856714d8e8SKurt Hackel 
25866714d8e8SKurt Hackel 	return ret;
25876714d8e8SKurt Hackel }
25886714d8e8SKurt Hackel 
2589d74c9803SKurt Hackel int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2590d74c9803SKurt Hackel 			   void **ret_data)
25916714d8e8SKurt Hackel {
25926714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
25936714d8e8SKurt Hackel 	struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
25946714d8e8SKurt Hackel 
25956714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
25966714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
25976714d8e8SKurt Hackel 		return 0;
25986714d8e8SKurt Hackel 
2599466d1a45SKurt Hackel 	spin_lock(&dlm->spinlock);
2600466d1a45SKurt Hackel 	if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2601466d1a45SKurt Hackel 		mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2602466d1a45SKurt Hackel 		     "but this node is in finalize state, waiting on finalize2\n",
2603466d1a45SKurt Hackel 		     dlm->name, br->node_idx, br->dead_node,
2604466d1a45SKurt Hackel 		     dlm->reco.dead_node, dlm->reco.new_master);
2605466d1a45SKurt Hackel 		spin_unlock(&dlm->spinlock);
2606466d1a45SKurt Hackel 		return EAGAIN;
2607466d1a45SKurt Hackel 	}
2608466d1a45SKurt Hackel 	spin_unlock(&dlm->spinlock);
2609466d1a45SKurt Hackel 
2610d6dea6e9SKurt Hackel 	mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2611d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2612d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
26136714d8e8SKurt Hackel 
26146714d8e8SKurt Hackel 	dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
26156714d8e8SKurt Hackel 
26166714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
26176714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2618e2faea4cSKurt Hackel 		if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2619e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u died, changing "
2620e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2621e2faea4cSKurt Hackel 			     br->node_idx);
2622e2faea4cSKurt Hackel 		} else {
2623e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u NOT DEAD, changing "
2624e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2625e2faea4cSKurt Hackel 			     br->node_idx);
2626e2faea4cSKurt Hackel 			/* may not have seen the new master as dead yet */
2627e2faea4cSKurt Hackel 		}
26286714d8e8SKurt Hackel 	}
26296714d8e8SKurt Hackel 	if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2630e2faea4cSKurt Hackel 		mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2631e2faea4cSKurt Hackel 		     "node %u changing it to %u\n", dlm->name,
2632e2faea4cSKurt Hackel 		     dlm->reco.dead_node, br->node_idx, br->dead_node);
26336714d8e8SKurt Hackel 	}
2634ab27eb6fSKurt Hackel 	dlm_set_reco_master(dlm, br->node_idx);
2635ab27eb6fSKurt Hackel 	dlm_set_reco_dead_node(dlm, br->dead_node);
26366714d8e8SKurt Hackel 	if (!test_bit(br->dead_node, dlm->recovery_map)) {
2637e2faea4cSKurt Hackel 		mlog(0, "recovery master %u sees %u as dead, but this "
26386714d8e8SKurt Hackel 		     "node has not yet.  marking %u as dead\n",
26396714d8e8SKurt Hackel 		     br->node_idx, br->dead_node, br->dead_node);
2640e2faea4cSKurt Hackel 		if (!test_bit(br->dead_node, dlm->domain_map) ||
2641e2faea4cSKurt Hackel 		    !test_bit(br->dead_node, dlm->live_nodes_map))
2642e2faea4cSKurt Hackel 			mlog(0, "%u not in domain/live_nodes map "
2643e2faea4cSKurt Hackel 			     "so setting it in reco map manually\n",
2644e2faea4cSKurt Hackel 			     br->dead_node);
2645c03872f5SKurt Hackel 		/* force the recovery cleanup in __dlm_hb_node_down
2646c03872f5SKurt Hackel 		 * both of these will be cleared in a moment */
2647c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->domain_map);
2648c03872f5SKurt Hackel 		set_bit(br->dead_node, dlm->live_nodes_map);
26496714d8e8SKurt Hackel 		__dlm_hb_node_down(dlm, br->dead_node);
26506714d8e8SKurt Hackel 	}
26516714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
26526714d8e8SKurt Hackel 
26536714d8e8SKurt Hackel 	dlm_kick_recovery_thread(dlm);
2654d6dea6e9SKurt Hackel 
2655d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2656d6dea6e9SKurt Hackel 	     dlm->name, br->node_idx, br->dead_node,
2657d6dea6e9SKurt Hackel 	     dlm->reco.dead_node, dlm->reco.new_master);
2658d6dea6e9SKurt Hackel 
26596714d8e8SKurt Hackel 	dlm_put(dlm);
26606714d8e8SKurt Hackel 	return 0;
26616714d8e8SKurt Hackel }
26626714d8e8SKurt Hackel 
2663466d1a45SKurt Hackel #define DLM_FINALIZE_STAGE2  0x01
26646714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
26656714d8e8SKurt Hackel {
26666714d8e8SKurt Hackel 	int ret = 0;
26676714d8e8SKurt Hackel 	struct dlm_finalize_reco fr;
26686714d8e8SKurt Hackel 	struct dlm_node_iter iter;
26696714d8e8SKurt Hackel 	int nodenum;
26706714d8e8SKurt Hackel 	int status;
2671466d1a45SKurt Hackel 	int stage = 1;
26726714d8e8SKurt Hackel 
2673466d1a45SKurt Hackel 	mlog(0, "finishing recovery for node %s:%u, "
2674466d1a45SKurt Hackel 	     "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
26756714d8e8SKurt Hackel 
26766714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
26776714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
26786714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
26796714d8e8SKurt Hackel 
2680466d1a45SKurt Hackel stage2:
26816714d8e8SKurt Hackel 	memset(&fr, 0, sizeof(fr));
26826714d8e8SKurt Hackel 	fr.node_idx = dlm->node_num;
26836714d8e8SKurt Hackel 	fr.dead_node = dlm->reco.dead_node;
2684466d1a45SKurt Hackel 	if (stage == 2)
2685466d1a45SKurt Hackel 		fr.flags |= DLM_FINALIZE_STAGE2;
26866714d8e8SKurt Hackel 
26876714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
26886714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
26896714d8e8SKurt Hackel 			continue;
26906714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
26916714d8e8SKurt Hackel 					 &fr, sizeof(fr), nodenum, &status);
2692466d1a45SKurt Hackel 		if (ret >= 0)
26936714d8e8SKurt Hackel 			ret = status;
2694466d1a45SKurt Hackel 		if (ret < 0) {
2695466d1a45SKurt Hackel 			mlog_errno(ret);
26966714d8e8SKurt Hackel 			if (dlm_is_host_down(ret)) {
26976714d8e8SKurt Hackel 				/* this has no effect on this recovery
26986714d8e8SKurt Hackel 				 * session, so set the status to zero to
26996714d8e8SKurt Hackel 				 * finish out the last recovery */
27006714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u went down after this "
27016714d8e8SKurt Hackel 				     "node finished recovery.\n", nodenum);
27026714d8e8SKurt Hackel 				ret = 0;
2703c27069e6SKurt Hackel 				continue;
27046714d8e8SKurt Hackel 			}
27056714d8e8SKurt Hackel 			break;
27066714d8e8SKurt Hackel 		}
27076714d8e8SKurt Hackel 	}
2708466d1a45SKurt Hackel 	if (stage == 1) {
2709466d1a45SKurt Hackel 		/* reset the node_iter back to the top and send finalize2 */
2710466d1a45SKurt Hackel 		iter.curnode = -1;
2711466d1a45SKurt Hackel 		stage = 2;
2712466d1a45SKurt Hackel 		goto stage2;
2713466d1a45SKurt Hackel 	}
27146714d8e8SKurt Hackel 
27156714d8e8SKurt Hackel 	return ret;
27166714d8e8SKurt Hackel }
27176714d8e8SKurt Hackel 
2718d74c9803SKurt Hackel int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2719d74c9803SKurt Hackel 			      void **ret_data)
27206714d8e8SKurt Hackel {
27216714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
27226714d8e8SKurt Hackel 	struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2723466d1a45SKurt Hackel 	int stage = 1;
27246714d8e8SKurt Hackel 
27256714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
27266714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
27276714d8e8SKurt Hackel 		return 0;
27286714d8e8SKurt Hackel 
2729466d1a45SKurt Hackel 	if (fr->flags & DLM_FINALIZE_STAGE2)
2730466d1a45SKurt Hackel 		stage = 2;
2731466d1a45SKurt Hackel 
2732466d1a45SKurt Hackel 	mlog(0, "%s: node %u finalizing recovery stage%d of "
2733466d1a45SKurt Hackel 	     "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2734466d1a45SKurt Hackel 	     fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
27356714d8e8SKurt Hackel 
27366714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
27376714d8e8SKurt Hackel 
27386714d8e8SKurt Hackel 	if (dlm->reco.new_master != fr->node_idx) {
27396714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
27406714d8e8SKurt Hackel 		     "%u is supposed to be the new master, dead=%u\n",
27416714d8e8SKurt Hackel 		     fr->node_idx, dlm->reco.new_master, fr->dead_node);
27426714d8e8SKurt Hackel 		BUG();
27436714d8e8SKurt Hackel 	}
27446714d8e8SKurt Hackel 	if (dlm->reco.dead_node != fr->dead_node) {
27456714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
27466714d8e8SKurt Hackel 		     "node %u, but node %u is supposed to be dead\n",
27476714d8e8SKurt Hackel 		     fr->node_idx, fr->dead_node, dlm->reco.dead_node);
27486714d8e8SKurt Hackel 		BUG();
27496714d8e8SKurt Hackel 	}
27506714d8e8SKurt Hackel 
2751466d1a45SKurt Hackel 	switch (stage) {
2752466d1a45SKurt Hackel 		case 1:
27536714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2754466d1a45SKurt Hackel 			if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2755466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize1 from "
2756466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2757466d1a45SKurt Hackel 				     "this node has already received it!\n",
2758466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2759466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2760466d1a45SKurt Hackel 				BUG();
2761466d1a45SKurt Hackel 			}
2762466d1a45SKurt Hackel 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
27636714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
2764466d1a45SKurt Hackel 			break;
2765466d1a45SKurt Hackel 		case 2:
2766466d1a45SKurt Hackel 			if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2767466d1a45SKurt Hackel 				mlog(ML_ERROR, "%s: received finalize2 from "
2768466d1a45SKurt Hackel 				     "new master %u for dead node %u, but "
2769466d1a45SKurt Hackel 				     "this node did not have finalize1!\n",
2770466d1a45SKurt Hackel 				     dlm->name, fr->node_idx, fr->dead_node);
2771466d1a45SKurt Hackel 				dlm_print_reco_node_status(dlm);
2772466d1a45SKurt Hackel 				BUG();
2773466d1a45SKurt Hackel 			}
2774466d1a45SKurt Hackel 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2775466d1a45SKurt Hackel 			spin_unlock(&dlm->spinlock);
27766714d8e8SKurt Hackel 			dlm_reset_recovery(dlm);
27776714d8e8SKurt Hackel 			dlm_kick_recovery_thread(dlm);
2778466d1a45SKurt Hackel 			break;
2779466d1a45SKurt Hackel 		default:
2780466d1a45SKurt Hackel 			BUG();
2781466d1a45SKurt Hackel 	}
2782466d1a45SKurt Hackel 
2783d6dea6e9SKurt Hackel 	mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2784d6dea6e9SKurt Hackel 	     dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2785d6dea6e9SKurt Hackel 
27866714d8e8SKurt Hackel 	dlm_put(dlm);
27876714d8e8SKurt Hackel 	return 0;
27886714d8e8SKurt Hackel }
2789