xref: /openbmc/linux/fs/ocfs2/dlm/dlmrecovery.c (revision b4c7f538)
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);
616714d8e8SKurt Hackel static 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_lockres_master_requery(struct dlm_ctxt *dlm,
826714d8e8SKurt Hackel 				      struct dlm_lock_resource *res,
836714d8e8SKurt Hackel 				      u8 *real_master);
846714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
856714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
866714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres);
876714d8e8SKurt Hackel static int dlm_do_master_requery(struct dlm_ctxt *dlm,
886714d8e8SKurt Hackel 				 struct dlm_lock_resource *res,
896714d8e8SKurt Hackel 				 u8 nodenum, u8 *real_master);
906714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
916714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
926714d8e8SKurt Hackel 				 u8 dead_node, u8 send_to);
936714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
946714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
956714d8e8SKurt Hackel 					struct list_head *list, u8 dead_node);
966714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
976714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master);
986714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata);
996714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type);
1006714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
1016714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item,
1026714d8e8SKurt Hackel 					 void *data);
1036714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
1046714d8e8SKurt Hackel 
1056714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void);
1066714d8e8SKurt Hackel 
1076714d8e8SKurt Hackel static spinlock_t dlm_reco_state_lock = SPIN_LOCK_UNLOCKED;
1086714d8e8SKurt Hackel static spinlock_t dlm_mig_cookie_lock = SPIN_LOCK_UNLOCKED;
1096714d8e8SKurt Hackel static u64 dlm_mig_cookie = 1;
1106714d8e8SKurt Hackel 
1116714d8e8SKurt Hackel static u64 dlm_get_next_mig_cookie(void)
1126714d8e8SKurt Hackel {
1136714d8e8SKurt Hackel 	u64 c;
1146714d8e8SKurt Hackel 	spin_lock(&dlm_mig_cookie_lock);
1156714d8e8SKurt Hackel 	c = dlm_mig_cookie;
1166714d8e8SKurt Hackel 	if (dlm_mig_cookie == (~0ULL))
1176714d8e8SKurt Hackel 		dlm_mig_cookie = 1;
1186714d8e8SKurt Hackel 	else
1196714d8e8SKurt Hackel 		dlm_mig_cookie++;
1206714d8e8SKurt Hackel 	spin_unlock(&dlm_mig_cookie_lock);
1216714d8e8SKurt Hackel 	return c;
1226714d8e8SKurt Hackel }
1236714d8e8SKurt Hackel 
1246714d8e8SKurt Hackel static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
1256714d8e8SKurt Hackel {
1266714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
1276714d8e8SKurt Hackel 	clear_bit(dlm->reco.dead_node, dlm->recovery_map);
1286714d8e8SKurt Hackel 	dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1296714d8e8SKurt Hackel 	dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1306714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
1316714d8e8SKurt Hackel }
1326714d8e8SKurt Hackel 
1336714d8e8SKurt Hackel /* Worker function used during recovery. */
1346714d8e8SKurt Hackel void dlm_dispatch_work(void *data)
1356714d8e8SKurt Hackel {
1366714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = (struct dlm_ctxt *)data;
1376714d8e8SKurt Hackel 	LIST_HEAD(tmp_list);
1386714d8e8SKurt Hackel 	struct list_head *iter, *iter2;
1396714d8e8SKurt Hackel 	struct dlm_work_item *item;
1406714d8e8SKurt Hackel 	dlm_workfunc_t *workfunc;
1416714d8e8SKurt Hackel 
1426714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
1436714d8e8SKurt Hackel 	list_splice_init(&dlm->work_list, &tmp_list);
1446714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
1456714d8e8SKurt Hackel 
1466714d8e8SKurt Hackel 	list_for_each_safe(iter, iter2, &tmp_list) {
1476714d8e8SKurt Hackel 		item = list_entry(iter, struct dlm_work_item, list);
1486714d8e8SKurt Hackel 		workfunc = item->func;
1496714d8e8SKurt Hackel 		list_del_init(&item->list);
1506714d8e8SKurt Hackel 
1516714d8e8SKurt Hackel 		/* already have ref on dlm to avoid having
1526714d8e8SKurt Hackel 		 * it disappear.  just double-check. */
1536714d8e8SKurt Hackel 		BUG_ON(item->dlm != dlm);
1546714d8e8SKurt Hackel 
1556714d8e8SKurt Hackel 		/* this is allowed to sleep and
1566714d8e8SKurt Hackel 		 * call network stuff */
1576714d8e8SKurt Hackel 		workfunc(item, item->data);
1586714d8e8SKurt Hackel 
1596714d8e8SKurt Hackel 		dlm_put(dlm);
1606714d8e8SKurt Hackel 		kfree(item);
1616714d8e8SKurt Hackel 	}
1626714d8e8SKurt Hackel }
1636714d8e8SKurt Hackel 
1646714d8e8SKurt Hackel /*
1656714d8e8SKurt Hackel  * RECOVERY THREAD
1666714d8e8SKurt Hackel  */
1676714d8e8SKurt Hackel 
1686714d8e8SKurt Hackel static void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
1696714d8e8SKurt Hackel {
1706714d8e8SKurt Hackel 	/* wake the recovery thread
1716714d8e8SKurt Hackel 	 * this will wake the reco thread in one of three places
1726714d8e8SKurt Hackel 	 * 1) sleeping with no recovery happening
1736714d8e8SKurt Hackel 	 * 2) sleeping with recovery mastered elsewhere
1746714d8e8SKurt Hackel 	 * 3) recovery mastered here, waiting on reco data */
1756714d8e8SKurt Hackel 
1766714d8e8SKurt Hackel 	wake_up(&dlm->dlm_reco_thread_wq);
1776714d8e8SKurt Hackel }
1786714d8e8SKurt Hackel 
1796714d8e8SKurt Hackel /* Launch the recovery thread */
1806714d8e8SKurt Hackel int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
1816714d8e8SKurt Hackel {
1826714d8e8SKurt Hackel 	mlog(0, "starting dlm recovery thread...\n");
1836714d8e8SKurt Hackel 
1846714d8e8SKurt Hackel 	dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
1856714d8e8SKurt Hackel 						"dlm_reco_thread");
1866714d8e8SKurt Hackel 	if (IS_ERR(dlm->dlm_reco_thread_task)) {
1876714d8e8SKurt Hackel 		mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
1886714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
1896714d8e8SKurt Hackel 		return -EINVAL;
1906714d8e8SKurt Hackel 	}
1916714d8e8SKurt Hackel 
1926714d8e8SKurt Hackel 	return 0;
1936714d8e8SKurt Hackel }
1946714d8e8SKurt Hackel 
1956714d8e8SKurt Hackel void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
1966714d8e8SKurt Hackel {
1976714d8e8SKurt Hackel 	if (dlm->dlm_reco_thread_task) {
1986714d8e8SKurt Hackel 		mlog(0, "waiting for dlm recovery thread to exit\n");
1996714d8e8SKurt Hackel 		kthread_stop(dlm->dlm_reco_thread_task);
2006714d8e8SKurt Hackel 		dlm->dlm_reco_thread_task = NULL;
2016714d8e8SKurt Hackel 	}
2026714d8e8SKurt Hackel }
2036714d8e8SKurt Hackel 
2046714d8e8SKurt Hackel 
2056714d8e8SKurt Hackel 
2066714d8e8SKurt Hackel /*
2076714d8e8SKurt Hackel  * this is lame, but here's how recovery works...
2086714d8e8SKurt Hackel  * 1) all recovery threads cluster wide will work on recovering
2096714d8e8SKurt Hackel  *    ONE node at a time
2106714d8e8SKurt Hackel  * 2) negotiate who will take over all the locks for the dead node.
2116714d8e8SKurt Hackel  *    thats right... ALL the locks.
2126714d8e8SKurt Hackel  * 3) once a new master is chosen, everyone scans all locks
2136714d8e8SKurt Hackel  *    and moves aside those mastered by the dead guy
2146714d8e8SKurt Hackel  * 4) each of these locks should be locked until recovery is done
2156714d8e8SKurt Hackel  * 5) the new master collects up all of secondary lock queue info
2166714d8e8SKurt Hackel  *    one lock at a time, forcing each node to communicate back
2176714d8e8SKurt Hackel  *    before continuing
2186714d8e8SKurt Hackel  * 6) each secondary lock queue responds with the full known lock info
2196714d8e8SKurt Hackel  * 7) once the new master has run all its locks, it sends a ALLDONE!
2206714d8e8SKurt Hackel  *    message to everyone
2216714d8e8SKurt Hackel  * 8) upon receiving this message, the secondary queue node unlocks
2226714d8e8SKurt Hackel  *    and responds to the ALLDONE
2236714d8e8SKurt Hackel  * 9) once the new master gets responses from everyone, he unlocks
2246714d8e8SKurt Hackel  *    everything and recovery for this dead node is done
2256714d8e8SKurt Hackel  *10) go back to 2) while there are still dead nodes
2266714d8e8SKurt Hackel  *
2276714d8e8SKurt Hackel  */
2286714d8e8SKurt Hackel 
2296714d8e8SKurt Hackel 
2306714d8e8SKurt Hackel #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
2316714d8e8SKurt Hackel 
2326714d8e8SKurt Hackel static int dlm_recovery_thread(void *data)
2336714d8e8SKurt Hackel {
2346714d8e8SKurt Hackel 	int status;
2356714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
2366714d8e8SKurt Hackel 	unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
2376714d8e8SKurt Hackel 
2386714d8e8SKurt Hackel 	mlog(0, "dlm thread running for %s...\n", dlm->name);
2396714d8e8SKurt Hackel 
2406714d8e8SKurt Hackel 	while (!kthread_should_stop()) {
2416714d8e8SKurt Hackel 		if (dlm_joined(dlm)) {
2426714d8e8SKurt Hackel 			status = dlm_do_recovery(dlm);
2436714d8e8SKurt Hackel 			if (status == -EAGAIN) {
2446714d8e8SKurt Hackel 				/* do not sleep, recheck immediately. */
2456714d8e8SKurt Hackel 				continue;
2466714d8e8SKurt Hackel 			}
2476714d8e8SKurt Hackel 			if (status < 0)
2486714d8e8SKurt Hackel 				mlog_errno(status);
2496714d8e8SKurt Hackel 		}
2506714d8e8SKurt Hackel 
2516714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
2526714d8e8SKurt Hackel 						 kthread_should_stop(),
2536714d8e8SKurt Hackel 						 timeout);
2546714d8e8SKurt Hackel 	}
2556714d8e8SKurt Hackel 
2566714d8e8SKurt Hackel 	mlog(0, "quitting DLM recovery thread\n");
2576714d8e8SKurt Hackel 	return 0;
2586714d8e8SKurt Hackel }
2596714d8e8SKurt Hackel 
260e2faea4cSKurt Hackel /* returns true when the recovery master has contacted us */
261e2faea4cSKurt Hackel static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
262e2faea4cSKurt Hackel {
263e2faea4cSKurt Hackel 	int ready;
264e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
265e2faea4cSKurt Hackel 	ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
266e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
267e2faea4cSKurt Hackel 	return ready;
268e2faea4cSKurt Hackel }
269e2faea4cSKurt Hackel 
270e2faea4cSKurt Hackel /* returns true if node is no longer in the domain
271e2faea4cSKurt Hackel  * could be dead or just not joined */
272e2faea4cSKurt Hackel int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
273e2faea4cSKurt Hackel {
274e2faea4cSKurt Hackel 	int dead;
275e2faea4cSKurt Hackel 	spin_lock(&dlm->spinlock);
276e2faea4cSKurt Hackel 	dead = test_bit(node, dlm->domain_map);
277e2faea4cSKurt Hackel 	spin_unlock(&dlm->spinlock);
278e2faea4cSKurt Hackel 	return dead;
279e2faea4cSKurt Hackel }
280e2faea4cSKurt Hackel 
2816714d8e8SKurt Hackel /* callers of the top-level api calls (dlmlock/dlmunlock) should
2826714d8e8SKurt Hackel  * block on the dlm->reco.event when recovery is in progress.
2836714d8e8SKurt Hackel  * the dlm recovery thread will set this state when it begins
2846714d8e8SKurt Hackel  * recovering a dead node (as the new master or not) and clear
2856714d8e8SKurt Hackel  * the state and wake as soon as all affected lock resources have
2866714d8e8SKurt Hackel  * been marked with the RECOVERY flag */
2876714d8e8SKurt Hackel static int dlm_in_recovery(struct dlm_ctxt *dlm)
2886714d8e8SKurt Hackel {
2896714d8e8SKurt Hackel 	int in_recovery;
2906714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
2916714d8e8SKurt Hackel 	in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
2926714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
2936714d8e8SKurt Hackel 	return in_recovery;
2946714d8e8SKurt Hackel }
2956714d8e8SKurt Hackel 
2966714d8e8SKurt Hackel 
2976714d8e8SKurt Hackel void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
2986714d8e8SKurt Hackel {
2996714d8e8SKurt Hackel 	wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
3006714d8e8SKurt Hackel }
3016714d8e8SKurt Hackel 
3026714d8e8SKurt Hackel static void dlm_begin_recovery(struct dlm_ctxt *dlm)
3036714d8e8SKurt Hackel {
3046714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
3056714d8e8SKurt Hackel 	BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
3066714d8e8SKurt Hackel 	dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
3076714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3086714d8e8SKurt Hackel }
3096714d8e8SKurt Hackel 
3106714d8e8SKurt Hackel static void dlm_end_recovery(struct dlm_ctxt *dlm)
3116714d8e8SKurt Hackel {
3126714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
3136714d8e8SKurt Hackel 	BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
3146714d8e8SKurt Hackel 	dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
3156714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3166714d8e8SKurt Hackel 	wake_up(&dlm->reco.event);
3176714d8e8SKurt Hackel }
3186714d8e8SKurt Hackel 
3196714d8e8SKurt Hackel static int dlm_do_recovery(struct dlm_ctxt *dlm)
3206714d8e8SKurt Hackel {
3216714d8e8SKurt Hackel 	int status = 0;
322e2faea4cSKurt Hackel 	int ret;
3236714d8e8SKurt Hackel 
3246714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
3256714d8e8SKurt Hackel 
3266714d8e8SKurt Hackel 	/* check to see if the new master has died */
3276714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
3286714d8e8SKurt Hackel 	    test_bit(dlm->reco.new_master, dlm->recovery_map)) {
3296714d8e8SKurt Hackel 		mlog(0, "new master %u died while recovering %u!\n",
3306714d8e8SKurt Hackel 		     dlm->reco.new_master, dlm->reco.dead_node);
3316714d8e8SKurt Hackel 		/* unset the new_master, leave dead_node */
3326714d8e8SKurt Hackel 		dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
3336714d8e8SKurt Hackel 	}
3346714d8e8SKurt Hackel 
3356714d8e8SKurt Hackel 	/* select a target to recover */
3366714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
3376714d8e8SKurt Hackel 		int bit;
3386714d8e8SKurt Hackel 
3396714d8e8SKurt Hackel 		bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
3406714d8e8SKurt Hackel 		if (bit >= O2NM_MAX_NODES || bit < 0)
3416714d8e8SKurt Hackel 			dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
3426714d8e8SKurt Hackel 		else
3436714d8e8SKurt Hackel 			dlm->reco.dead_node = bit;
3446714d8e8SKurt Hackel 	} else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
3456714d8e8SKurt Hackel 		/* BUG? */
3466714d8e8SKurt Hackel 		mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
3476714d8e8SKurt Hackel 		     dlm->reco.dead_node);
3486714d8e8SKurt Hackel 		dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
3496714d8e8SKurt Hackel 	}
3506714d8e8SKurt Hackel 
3516714d8e8SKurt Hackel 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
3526714d8e8SKurt Hackel 		// mlog(0, "nothing to recover!  sleeping now!\n");
3536714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
3546714d8e8SKurt Hackel 		/* return to main thread loop and sleep. */
3556714d8e8SKurt Hackel 		return 0;
3566714d8e8SKurt Hackel 	}
3576714d8e8SKurt Hackel 	mlog(0, "recovery thread found node %u in the recovery map!\n",
3586714d8e8SKurt Hackel 	     dlm->reco.dead_node);
3596714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
3606714d8e8SKurt Hackel 
3616714d8e8SKurt Hackel 	/* take write barrier */
3626714d8e8SKurt Hackel 	/* (stops the list reshuffling thread, proxy ast handling) */
3636714d8e8SKurt Hackel 	dlm_begin_recovery(dlm);
3646714d8e8SKurt Hackel 
3656714d8e8SKurt Hackel 	if (dlm->reco.new_master == dlm->node_num)
3666714d8e8SKurt Hackel 		goto master_here;
3676714d8e8SKurt Hackel 
3686714d8e8SKurt Hackel 	if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
369e2faea4cSKurt Hackel 		/* choose a new master, returns 0 if this node
370e2faea4cSKurt Hackel 		 * is the master, -EEXIST if it's another node.
371e2faea4cSKurt Hackel 		 * this does not return until a new master is chosen
372e2faea4cSKurt Hackel 		 * or recovery completes entirely. */
373e2faea4cSKurt Hackel 		ret = dlm_pick_recovery_master(dlm);
374e2faea4cSKurt Hackel 		if (!ret) {
3756714d8e8SKurt Hackel 			/* already notified everyone.  go. */
3766714d8e8SKurt Hackel 			goto master_here;
3776714d8e8SKurt Hackel 		}
3786714d8e8SKurt Hackel 		mlog(0, "another node will master this recovery session.\n");
3796714d8e8SKurt Hackel 	}
3806714d8e8SKurt Hackel 	mlog(0, "dlm=%s, new_master=%u, this node=%u, dead_node=%u\n",
3816714d8e8SKurt Hackel 	     dlm->name, dlm->reco.new_master,
3826714d8e8SKurt Hackel 	     dlm->node_num, dlm->reco.dead_node);
3836714d8e8SKurt Hackel 
3846714d8e8SKurt Hackel 	/* it is safe to start everything back up here
3856714d8e8SKurt Hackel 	 * because all of the dead node's lock resources
3866714d8e8SKurt Hackel 	 * have been marked as in-recovery */
3876714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
3886714d8e8SKurt Hackel 
3896714d8e8SKurt Hackel 	/* sleep out in main dlm_recovery_thread loop. */
3906714d8e8SKurt Hackel 	return 0;
3916714d8e8SKurt Hackel 
3926714d8e8SKurt Hackel master_here:
3936714d8e8SKurt Hackel 	mlog(0, "mastering recovery of %s:%u here(this=%u)!\n",
3946714d8e8SKurt Hackel 	     dlm->name, dlm->reco.dead_node, dlm->node_num);
3956714d8e8SKurt Hackel 
3966714d8e8SKurt Hackel 	status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
3976714d8e8SKurt Hackel 	if (status < 0) {
3986714d8e8SKurt Hackel 		mlog(ML_ERROR, "error %d remastering locks for node %u, "
3996714d8e8SKurt Hackel 		     "retrying.\n", status, dlm->reco.dead_node);
400e2faea4cSKurt Hackel 		/* yield a bit to allow any final network messages
401e2faea4cSKurt Hackel 		 * to get handled on remaining nodes */
402e2faea4cSKurt Hackel 		msleep(100);
4036714d8e8SKurt Hackel 	} else {
4046714d8e8SKurt Hackel 		/* success!  see if any other nodes need recovery */
405e2faea4cSKurt Hackel 		mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
406e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dlm->node_num);
4076714d8e8SKurt Hackel 		dlm_reset_recovery(dlm);
4086714d8e8SKurt Hackel 	}
4096714d8e8SKurt Hackel 	dlm_end_recovery(dlm);
4106714d8e8SKurt Hackel 
4116714d8e8SKurt Hackel 	/* continue and look for another dead node */
4126714d8e8SKurt Hackel 	return -EAGAIN;
4136714d8e8SKurt Hackel }
4146714d8e8SKurt Hackel 
4156714d8e8SKurt Hackel static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
4166714d8e8SKurt Hackel {
4176714d8e8SKurt Hackel 	int status = 0;
4186714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
4196714d8e8SKurt Hackel 	struct list_head *iter;
4206714d8e8SKurt Hackel 	int all_nodes_done;
4216714d8e8SKurt Hackel 	int destroy = 0;
4226714d8e8SKurt Hackel 	int pass = 0;
4236714d8e8SKurt Hackel 
4246714d8e8SKurt Hackel 	status = dlm_init_recovery_area(dlm, dead_node);
4256714d8e8SKurt Hackel 	if (status < 0)
4266714d8e8SKurt Hackel 		goto leave;
4276714d8e8SKurt Hackel 
4286714d8e8SKurt Hackel 	/* safe to access the node data list without a lock, since this
4296714d8e8SKurt Hackel 	 * process is the only one to change the list */
4306714d8e8SKurt Hackel 	list_for_each(iter, &dlm->reco.node_data) {
4316714d8e8SKurt Hackel 		ndata = list_entry (iter, struct dlm_reco_node_data, list);
4326714d8e8SKurt Hackel 		BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
4336714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
4346714d8e8SKurt Hackel 
4356714d8e8SKurt Hackel 		mlog(0, "requesting lock info from node %u\n",
4366714d8e8SKurt Hackel 		     ndata->node_num);
4376714d8e8SKurt Hackel 
4386714d8e8SKurt Hackel 		if (ndata->node_num == dlm->node_num) {
4396714d8e8SKurt Hackel 			ndata->state = DLM_RECO_NODE_DATA_DONE;
4406714d8e8SKurt Hackel 			continue;
4416714d8e8SKurt Hackel 		}
4426714d8e8SKurt Hackel 
4436714d8e8SKurt Hackel 		status = dlm_request_all_locks(dlm, ndata->node_num, dead_node);
4446714d8e8SKurt Hackel 		if (status < 0) {
4456714d8e8SKurt Hackel 			mlog_errno(status);
4466714d8e8SKurt Hackel 			if (dlm_is_host_down(status))
4476714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_DEAD;
4486714d8e8SKurt Hackel 			else {
4496714d8e8SKurt Hackel 				destroy = 1;
4506714d8e8SKurt Hackel 				goto leave;
4516714d8e8SKurt Hackel 			}
4526714d8e8SKurt Hackel 		}
4536714d8e8SKurt Hackel 
4546714d8e8SKurt Hackel 		switch (ndata->state) {
4556714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
4566714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
4576714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
4586714d8e8SKurt Hackel 				BUG();
4596714d8e8SKurt Hackel 				break;
4606714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
4616714d8e8SKurt Hackel 				mlog(0, "node %u died after requesting "
4626714d8e8SKurt Hackel 				     "recovery info for node %u\n",
4636714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
4646714d8e8SKurt Hackel 				// start all over
4656714d8e8SKurt Hackel 				destroy = 1;
4666714d8e8SKurt Hackel 				status = -EAGAIN;
4676714d8e8SKurt Hackel 				goto leave;
4686714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
4696714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
4706714d8e8SKurt Hackel 				mlog(0, "now receiving recovery data from "
4716714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
4726714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
4736714d8e8SKurt Hackel 				break;
4746714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
4756714d8e8SKurt Hackel 				mlog(0, "already receiving recovery data from "
4766714d8e8SKurt Hackel 				     "node %u for dead node %u\n",
4776714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
4786714d8e8SKurt Hackel 				break;
4796714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
4806714d8e8SKurt Hackel 				mlog(0, "already DONE receiving recovery data "
4816714d8e8SKurt Hackel 				     "from node %u for dead node %u\n",
4826714d8e8SKurt Hackel 				     ndata->node_num, dead_node);
4836714d8e8SKurt Hackel 				break;
4846714d8e8SKurt Hackel 		}
4856714d8e8SKurt Hackel 	}
4866714d8e8SKurt Hackel 
4876714d8e8SKurt Hackel 	mlog(0, "done requesting all lock info\n");
4886714d8e8SKurt Hackel 
4896714d8e8SKurt Hackel 	/* nodes should be sending reco data now
4906714d8e8SKurt Hackel 	 * just need to wait */
4916714d8e8SKurt Hackel 
4926714d8e8SKurt Hackel 	while (1) {
4936714d8e8SKurt Hackel 		/* check all the nodes now to see if we are
4946714d8e8SKurt Hackel 		 * done, or if anyone died */
4956714d8e8SKurt Hackel 		all_nodes_done = 1;
4966714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
4976714d8e8SKurt Hackel 		list_for_each(iter, &dlm->reco.node_data) {
4986714d8e8SKurt Hackel 			ndata = list_entry (iter, struct dlm_reco_node_data, list);
4996714d8e8SKurt Hackel 
5006714d8e8SKurt Hackel 			mlog(0, "checking recovery state of node %u\n",
5016714d8e8SKurt Hackel 			     ndata->node_num);
5026714d8e8SKurt Hackel 			switch (ndata->state) {
5036714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_INIT:
5046714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTING:
5056714d8e8SKurt Hackel 					mlog(ML_ERROR, "bad ndata state for "
5066714d8e8SKurt Hackel 					     "node %u: state=%d\n",
5076714d8e8SKurt Hackel 					     ndata->node_num, ndata->state);
5086714d8e8SKurt Hackel 					BUG();
5096714d8e8SKurt Hackel 					break;
5106714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DEAD:
511e2faea4cSKurt Hackel 					mlog(ML_NOTICE, "node %u died after "
5126714d8e8SKurt Hackel 					     "requesting recovery info for "
5136714d8e8SKurt Hackel 					     "node %u\n", ndata->node_num,
5146714d8e8SKurt Hackel 					     dead_node);
5156714d8e8SKurt Hackel 					spin_unlock(&dlm_reco_state_lock);
5166714d8e8SKurt Hackel 					// start all over
5176714d8e8SKurt Hackel 					destroy = 1;
5186714d8e8SKurt Hackel 					status = -EAGAIN;
519e2faea4cSKurt Hackel 					/* instead of spinning like crazy here,
520e2faea4cSKurt Hackel 					 * wait for the domain map to catch up
521e2faea4cSKurt Hackel 					 * with the network state.  otherwise this
522e2faea4cSKurt Hackel 					 * can be hit hundreds of times before
523e2faea4cSKurt Hackel 					 * the node is really seen as dead. */
524e2faea4cSKurt Hackel 					wait_event_timeout(dlm->dlm_reco_thread_wq,
525e2faea4cSKurt Hackel 							   dlm_is_node_dead(dlm,
526e2faea4cSKurt Hackel 								ndata->node_num),
527e2faea4cSKurt Hackel 							   msecs_to_jiffies(1000));
528e2faea4cSKurt Hackel 					mlog(0, "waited 1 sec for %u, "
529e2faea4cSKurt Hackel 					     "dead? %s\n", ndata->node_num,
530e2faea4cSKurt Hackel 					     dlm_is_node_dead(dlm, ndata->node_num) ?
531e2faea4cSKurt Hackel 					     "yes" : "no");
5326714d8e8SKurt Hackel 					goto leave;
5336714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_RECEIVING:
5346714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_REQUESTED:
5356714d8e8SKurt Hackel 					all_nodes_done = 0;
5366714d8e8SKurt Hackel 					break;
5376714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_DONE:
5386714d8e8SKurt Hackel 					break;
5396714d8e8SKurt Hackel 				case DLM_RECO_NODE_DATA_FINALIZE_SENT:
5406714d8e8SKurt Hackel 					break;
5416714d8e8SKurt Hackel 			}
5426714d8e8SKurt Hackel 		}
5436714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
5446714d8e8SKurt Hackel 
5456714d8e8SKurt Hackel 		mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
5466714d8e8SKurt Hackel 		     all_nodes_done?"yes":"no");
5476714d8e8SKurt Hackel 		if (all_nodes_done) {
5486714d8e8SKurt Hackel 			int ret;
5496714d8e8SKurt Hackel 
5506714d8e8SKurt Hackel 			/* all nodes are now in DLM_RECO_NODE_DATA_DONE state
5516714d8e8SKurt Hackel 	 		 * just send a finalize message to everyone and
5526714d8e8SKurt Hackel 	 		 * clean up */
5536714d8e8SKurt Hackel 			mlog(0, "all nodes are done! send finalize\n");
5546714d8e8SKurt Hackel 			ret = dlm_send_finalize_reco_message(dlm);
5556714d8e8SKurt Hackel 			if (ret < 0)
5566714d8e8SKurt Hackel 				mlog_errno(ret);
5576714d8e8SKurt Hackel 
5586714d8e8SKurt Hackel 			spin_lock(&dlm->spinlock);
5596714d8e8SKurt Hackel 			dlm_finish_local_lockres_recovery(dlm, dead_node,
5606714d8e8SKurt Hackel 							  dlm->node_num);
5616714d8e8SKurt Hackel 			spin_unlock(&dlm->spinlock);
5626714d8e8SKurt Hackel 			mlog(0, "should be done with recovery!\n");
5636714d8e8SKurt Hackel 
5646714d8e8SKurt Hackel 			mlog(0, "finishing recovery of %s at %lu, "
5656714d8e8SKurt Hackel 			     "dead=%u, this=%u, new=%u\n", dlm->name,
5666714d8e8SKurt Hackel 			     jiffies, dlm->reco.dead_node,
5676714d8e8SKurt Hackel 			     dlm->node_num, dlm->reco.new_master);
5686714d8e8SKurt Hackel 			destroy = 1;
5696714d8e8SKurt Hackel 			status = ret;
5706714d8e8SKurt Hackel 			/* rescan everything marked dirty along the way */
5716714d8e8SKurt Hackel 			dlm_kick_thread(dlm, NULL);
5726714d8e8SKurt Hackel 			break;
5736714d8e8SKurt Hackel 		}
5746714d8e8SKurt Hackel 		/* wait to be signalled, with periodic timeout
5756714d8e8SKurt Hackel 		 * to check for node death */
5766714d8e8SKurt Hackel 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
5776714d8e8SKurt Hackel 					 kthread_should_stop(),
5786714d8e8SKurt Hackel 					 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
5796714d8e8SKurt Hackel 
5806714d8e8SKurt Hackel 	}
5816714d8e8SKurt Hackel 
5826714d8e8SKurt Hackel leave:
5836714d8e8SKurt Hackel 	if (destroy)
5846714d8e8SKurt Hackel 		dlm_destroy_recovery_area(dlm, dead_node);
5856714d8e8SKurt Hackel 
5866714d8e8SKurt Hackel 	mlog_exit(status);
5876714d8e8SKurt Hackel 	return status;
5886714d8e8SKurt Hackel }
5896714d8e8SKurt Hackel 
5906714d8e8SKurt Hackel static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
5916714d8e8SKurt Hackel {
5926714d8e8SKurt Hackel 	int num=0;
5936714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
5946714d8e8SKurt Hackel 
5956714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
5966714d8e8SKurt Hackel 	memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
5976714d8e8SKurt Hackel 	/* nodes can only be removed (by dying) after dropping
5986714d8e8SKurt Hackel 	 * this lock, and death will be trapped later, so this should do */
5996714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
6006714d8e8SKurt Hackel 
6016714d8e8SKurt Hackel 	while (1) {
6026714d8e8SKurt Hackel 		num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
6036714d8e8SKurt Hackel 		if (num >= O2NM_MAX_NODES) {
6046714d8e8SKurt Hackel 			break;
6056714d8e8SKurt Hackel 		}
6066714d8e8SKurt Hackel 		BUG_ON(num == dead_node);
6076714d8e8SKurt Hackel 
6086714d8e8SKurt Hackel 		ndata = kcalloc(1, sizeof(*ndata), GFP_KERNEL);
6096714d8e8SKurt Hackel 		if (!ndata) {
6106714d8e8SKurt Hackel 			dlm_destroy_recovery_area(dlm, dead_node);
6116714d8e8SKurt Hackel 			return -ENOMEM;
6126714d8e8SKurt Hackel 		}
6136714d8e8SKurt Hackel 		ndata->node_num = num;
6146714d8e8SKurt Hackel 		ndata->state = DLM_RECO_NODE_DATA_INIT;
6156714d8e8SKurt Hackel 		spin_lock(&dlm_reco_state_lock);
6166714d8e8SKurt Hackel 		list_add_tail(&ndata->list, &dlm->reco.node_data);
6176714d8e8SKurt Hackel 		spin_unlock(&dlm_reco_state_lock);
6186714d8e8SKurt Hackel 		num++;
6196714d8e8SKurt Hackel 	}
6206714d8e8SKurt Hackel 
6216714d8e8SKurt Hackel 	return 0;
6226714d8e8SKurt Hackel }
6236714d8e8SKurt Hackel 
6246714d8e8SKurt Hackel static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
6256714d8e8SKurt Hackel {
6266714d8e8SKurt Hackel 	struct list_head *iter, *iter2;
6276714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata;
6286714d8e8SKurt Hackel 	LIST_HEAD(tmplist);
6296714d8e8SKurt Hackel 
6306714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
6316714d8e8SKurt Hackel 	list_splice_init(&dlm->reco.node_data, &tmplist);
6326714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
6336714d8e8SKurt Hackel 
6346714d8e8SKurt Hackel 	list_for_each_safe(iter, iter2, &tmplist) {
6356714d8e8SKurt Hackel 		ndata = list_entry (iter, struct dlm_reco_node_data, list);
6366714d8e8SKurt Hackel 		list_del_init(&ndata->list);
6376714d8e8SKurt Hackel 		kfree(ndata);
6386714d8e8SKurt Hackel 	}
6396714d8e8SKurt Hackel }
6406714d8e8SKurt Hackel 
6416714d8e8SKurt Hackel static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
6426714d8e8SKurt Hackel 				 u8 dead_node)
6436714d8e8SKurt Hackel {
6446714d8e8SKurt Hackel 	struct dlm_lock_request lr;
6456714d8e8SKurt Hackel 	enum dlm_status ret;
6466714d8e8SKurt Hackel 
6476714d8e8SKurt Hackel 	mlog(0, "\n");
6486714d8e8SKurt Hackel 
6496714d8e8SKurt Hackel 
6506714d8e8SKurt Hackel 	mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
6516714d8e8SKurt Hackel 		  "to %u\n", dead_node, request_from);
6526714d8e8SKurt Hackel 
6536714d8e8SKurt Hackel 	memset(&lr, 0, sizeof(lr));
6546714d8e8SKurt Hackel 	lr.node_idx = dlm->node_num;
6556714d8e8SKurt Hackel 	lr.dead_node = dead_node;
6566714d8e8SKurt Hackel 
6576714d8e8SKurt Hackel 	// send message
6586714d8e8SKurt Hackel 	ret = DLM_NOLOCKMGR;
6596714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
6606714d8e8SKurt Hackel 				 &lr, sizeof(lr), request_from, NULL);
6616714d8e8SKurt Hackel 
6626714d8e8SKurt Hackel 	/* negative status is handled by caller */
6636714d8e8SKurt Hackel 	if (ret < 0)
6646714d8e8SKurt Hackel 		mlog_errno(ret);
6656714d8e8SKurt Hackel 
6666714d8e8SKurt Hackel 	// return from here, then
6676714d8e8SKurt Hackel 	// sleep until all received or error
6686714d8e8SKurt Hackel 	return ret;
6696714d8e8SKurt Hackel 
6706714d8e8SKurt Hackel }
6716714d8e8SKurt Hackel 
6726714d8e8SKurt Hackel int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
6736714d8e8SKurt Hackel {
6746714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
6756714d8e8SKurt Hackel 	struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
6766714d8e8SKurt Hackel 	char *buf = NULL;
6776714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
6786714d8e8SKurt Hackel 
6796714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
6806714d8e8SKurt Hackel 		return -EINVAL;
6816714d8e8SKurt Hackel 
6826714d8e8SKurt Hackel 	BUG_ON(lr->dead_node != dlm->reco.dead_node);
6836714d8e8SKurt Hackel 
6846714d8e8SKurt Hackel 	item = kcalloc(1, sizeof(*item), GFP_KERNEL);
6856714d8e8SKurt Hackel 	if (!item) {
6866714d8e8SKurt Hackel 		dlm_put(dlm);
6876714d8e8SKurt Hackel 		return -ENOMEM;
6886714d8e8SKurt Hackel 	}
6896714d8e8SKurt Hackel 
6906714d8e8SKurt Hackel 	/* this will get freed by dlm_request_all_locks_worker */
6916714d8e8SKurt Hackel 	buf = (char *) __get_free_page(GFP_KERNEL);
6926714d8e8SKurt Hackel 	if (!buf) {
6936714d8e8SKurt Hackel 		kfree(item);
6946714d8e8SKurt Hackel 		dlm_put(dlm);
6956714d8e8SKurt Hackel 		return -ENOMEM;
6966714d8e8SKurt Hackel 	}
6976714d8e8SKurt Hackel 
6986714d8e8SKurt Hackel 	/* queue up work for dlm_request_all_locks_worker */
6996714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
7006714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
7016714d8e8SKurt Hackel 	item->u.ral.reco_master = lr->node_idx;
7026714d8e8SKurt Hackel 	item->u.ral.dead_node = lr->dead_node;
7036714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
7046714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
7056714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
7066714d8e8SKurt Hackel 	schedule_work(&dlm->dispatched_work);
7076714d8e8SKurt Hackel 
7086714d8e8SKurt Hackel 	dlm_put(dlm);
7096714d8e8SKurt Hackel 	return 0;
7106714d8e8SKurt Hackel }
7116714d8e8SKurt Hackel 
7126714d8e8SKurt Hackel static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
7136714d8e8SKurt Hackel {
7146714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
7156714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
7166714d8e8SKurt Hackel 	struct dlm_ctxt *dlm;
7176714d8e8SKurt Hackel 	LIST_HEAD(resources);
7186714d8e8SKurt Hackel 	struct list_head *iter;
7196714d8e8SKurt Hackel 	int ret;
7206714d8e8SKurt Hackel 	u8 dead_node, reco_master;
7216714d8e8SKurt Hackel 
7226714d8e8SKurt Hackel 	dlm = item->dlm;
7236714d8e8SKurt Hackel 	dead_node = item->u.ral.dead_node;
7246714d8e8SKurt Hackel 	reco_master = item->u.ral.reco_master;
725e2faea4cSKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
726e2faea4cSKurt Hackel 
727e2faea4cSKurt Hackel 	if (dead_node != dlm->reco.dead_node ||
728e2faea4cSKurt Hackel 	    reco_master != dlm->reco.new_master) {
729e2faea4cSKurt Hackel 		/* show extra debug info if the recovery state is messed */
730e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: bad reco state: reco(dead=%u, master=%u), "
731e2faea4cSKurt Hackel 		     "request(dead=%u, master=%u)\n",
732e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.dead_node, dlm->reco.new_master,
733e2faea4cSKurt Hackel 		     dead_node, reco_master);
734e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: name=%.*s master=%u locks=%u/%u flags=%u "
735e2faea4cSKurt Hackel 		     "entry[0]={c=%"MLFu64",l=%u,f=%u,t=%d,ct=%d,hb=%d,n=%u}\n",
736e2faea4cSKurt Hackel 		     dlm->name, mres->lockname_len, mres->lockname, mres->master,
737e2faea4cSKurt Hackel 		     mres->num_locks, mres->total_locks, mres->flags,
738e2faea4cSKurt Hackel 		     mres->ml[0].cookie, mres->ml[0].list, mres->ml[0].flags,
739e2faea4cSKurt Hackel 		     mres->ml[0].type, mres->ml[0].convert_type,
740e2faea4cSKurt Hackel 		     mres->ml[0].highest_blocked, mres->ml[0].node);
741e2faea4cSKurt Hackel 		BUG();
742e2faea4cSKurt Hackel 	}
7436714d8e8SKurt Hackel 	BUG_ON(dead_node != dlm->reco.dead_node);
7446714d8e8SKurt Hackel 	BUG_ON(reco_master != dlm->reco.new_master);
7456714d8e8SKurt Hackel 
7466714d8e8SKurt Hackel 	/* lock resources should have already been moved to the
7476714d8e8SKurt Hackel  	 * dlm->reco.resources list.  now move items from that list
7486714d8e8SKurt Hackel  	 * to a temp list if the dead owner matches.  note that the
7496714d8e8SKurt Hackel 	 * whole cluster recovers only one node at a time, so we
7506714d8e8SKurt Hackel 	 * can safely move UNKNOWN lock resources for each recovery
7516714d8e8SKurt Hackel 	 * session. */
7526714d8e8SKurt Hackel 	dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
7536714d8e8SKurt Hackel 
7546714d8e8SKurt Hackel 	/* now we can begin blasting lockreses without the dlm lock */
7556714d8e8SKurt Hackel 	list_for_each(iter, &resources) {
7566714d8e8SKurt Hackel 		res = list_entry (iter, struct dlm_lock_resource, recovering);
7576714d8e8SKurt Hackel 		ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
7586714d8e8SKurt Hackel 				   	DLM_MRES_RECOVERY);
7596714d8e8SKurt Hackel 		if (ret < 0)
7606714d8e8SKurt Hackel 			mlog_errno(ret);
7616714d8e8SKurt Hackel 	}
7626714d8e8SKurt Hackel 
7636714d8e8SKurt Hackel 	/* move the resources back to the list */
7646714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
7656714d8e8SKurt Hackel 	list_splice_init(&resources, &dlm->reco.resources);
7666714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
7676714d8e8SKurt Hackel 
7686714d8e8SKurt Hackel 	ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
7696714d8e8SKurt Hackel 	if (ret < 0)
7706714d8e8SKurt Hackel 		mlog_errno(ret);
7716714d8e8SKurt Hackel 
7726714d8e8SKurt Hackel 	free_page((unsigned long)data);
7736714d8e8SKurt Hackel }
7746714d8e8SKurt Hackel 
7756714d8e8SKurt Hackel 
7766714d8e8SKurt Hackel static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
7776714d8e8SKurt Hackel {
7786714d8e8SKurt Hackel 	int ret, tmpret;
7796714d8e8SKurt Hackel 	struct dlm_reco_data_done done_msg;
7806714d8e8SKurt Hackel 
7816714d8e8SKurt Hackel 	memset(&done_msg, 0, sizeof(done_msg));
7826714d8e8SKurt Hackel 	done_msg.node_idx = dlm->node_num;
7836714d8e8SKurt Hackel 	done_msg.dead_node = dead_node;
7846714d8e8SKurt Hackel 	mlog(0, "sending DATA DONE message to %u, "
7856714d8e8SKurt Hackel 	     "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
7866714d8e8SKurt Hackel 	     done_msg.dead_node);
7876714d8e8SKurt Hackel 
7886714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
7896714d8e8SKurt Hackel 				 sizeof(done_msg), send_to, &tmpret);
7906714d8e8SKurt Hackel 	/* negative status is ignored by the caller */
7916714d8e8SKurt Hackel 	if (ret >= 0)
7926714d8e8SKurt Hackel 		ret = tmpret;
7936714d8e8SKurt Hackel 	return ret;
7946714d8e8SKurt Hackel }
7956714d8e8SKurt Hackel 
7966714d8e8SKurt Hackel 
7976714d8e8SKurt Hackel int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
7986714d8e8SKurt Hackel {
7996714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
8006714d8e8SKurt Hackel 	struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
8016714d8e8SKurt Hackel 	struct list_head *iter;
8026714d8e8SKurt Hackel 	struct dlm_reco_node_data *ndata = NULL;
8036714d8e8SKurt Hackel 	int ret = -EINVAL;
8046714d8e8SKurt Hackel 
8056714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
8066714d8e8SKurt Hackel 		return -EINVAL;
8076714d8e8SKurt Hackel 
8086714d8e8SKurt Hackel 	mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
8096714d8e8SKurt Hackel 	     "node_idx=%u, this node=%u\n", done->dead_node,
8106714d8e8SKurt Hackel 	     dlm->reco.dead_node, done->node_idx, dlm->node_num);
8116714d8e8SKurt Hackel 	BUG_ON(done->dead_node != dlm->reco.dead_node);
8126714d8e8SKurt Hackel 
8136714d8e8SKurt Hackel 	spin_lock(&dlm_reco_state_lock);
8146714d8e8SKurt Hackel 	list_for_each(iter, &dlm->reco.node_data) {
8156714d8e8SKurt Hackel 		ndata = list_entry (iter, struct dlm_reco_node_data, list);
8166714d8e8SKurt Hackel 		if (ndata->node_num != done->node_idx)
8176714d8e8SKurt Hackel 			continue;
8186714d8e8SKurt Hackel 
8196714d8e8SKurt Hackel 		switch (ndata->state) {
820e2faea4cSKurt Hackel 			/* should have moved beyond INIT but not to FINALIZE yet */
8216714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_INIT:
8226714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_DEAD:
8236714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
8246714d8e8SKurt Hackel 				mlog(ML_ERROR, "bad ndata state for node %u:"
8256714d8e8SKurt Hackel 				     " state=%d\n", ndata->node_num,
8266714d8e8SKurt Hackel 				     ndata->state);
8276714d8e8SKurt Hackel 				BUG();
8286714d8e8SKurt Hackel 				break;
829e2faea4cSKurt Hackel 			/* these states are possible at this point, anywhere along
830e2faea4cSKurt Hackel 			 * the line of recovery */
831e2faea4cSKurt Hackel 			case DLM_RECO_NODE_DATA_DONE:
8326714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_RECEIVING:
8336714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTED:
8346714d8e8SKurt Hackel 			case DLM_RECO_NODE_DATA_REQUESTING:
8356714d8e8SKurt Hackel 				mlog(0, "node %u is DONE sending "
8366714d8e8SKurt Hackel 					  "recovery data!\n",
8376714d8e8SKurt Hackel 					  ndata->node_num);
8386714d8e8SKurt Hackel 
8396714d8e8SKurt Hackel 				ndata->state = DLM_RECO_NODE_DATA_DONE;
8406714d8e8SKurt Hackel 				ret = 0;
8416714d8e8SKurt Hackel 				break;
8426714d8e8SKurt Hackel 		}
8436714d8e8SKurt Hackel 	}
8446714d8e8SKurt Hackel 	spin_unlock(&dlm_reco_state_lock);
8456714d8e8SKurt Hackel 
8466714d8e8SKurt Hackel 	/* wake the recovery thread, some node is done */
8476714d8e8SKurt Hackel 	if (!ret)
8486714d8e8SKurt Hackel 		dlm_kick_recovery_thread(dlm);
8496714d8e8SKurt Hackel 
8506714d8e8SKurt Hackel 	if (ret < 0)
8516714d8e8SKurt Hackel 		mlog(ML_ERROR, "failed to find recovery node data for node "
8526714d8e8SKurt Hackel 		     "%u\n", done->node_idx);
8536714d8e8SKurt Hackel 	dlm_put(dlm);
8546714d8e8SKurt Hackel 
8556714d8e8SKurt Hackel 	mlog(0, "leaving reco data done handler, ret=%d\n", ret);
8566714d8e8SKurt Hackel 	return ret;
8576714d8e8SKurt Hackel }
8586714d8e8SKurt Hackel 
8596714d8e8SKurt Hackel static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
8606714d8e8SKurt Hackel 					struct list_head *list,
8616714d8e8SKurt Hackel 				       	u8 dead_node)
8626714d8e8SKurt Hackel {
8636714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
8646714d8e8SKurt Hackel 	struct list_head *iter, *iter2;
865e2faea4cSKurt Hackel 	struct dlm_lock *lock;
8666714d8e8SKurt Hackel 
8676714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
8686714d8e8SKurt Hackel 	list_for_each_safe(iter, iter2, &dlm->reco.resources) {
8696714d8e8SKurt Hackel 		res = list_entry (iter, struct dlm_lock_resource, recovering);
870e2faea4cSKurt Hackel 		/* always prune any $RECOVERY entries for dead nodes,
871e2faea4cSKurt Hackel 		 * otherwise hangs can occur during later recovery */
8726714d8e8SKurt Hackel 		if (dlm_is_recovery_lock(res->lockname.name,
873e2faea4cSKurt Hackel 					 res->lockname.len)) {
874e2faea4cSKurt Hackel 			spin_lock(&res->spinlock);
875e2faea4cSKurt Hackel 			list_for_each_entry(lock, &res->granted, list) {
876e2faea4cSKurt Hackel 				if (lock->ml.node == dead_node) {
877e2faea4cSKurt Hackel 					mlog(0, "AHA! there was "
878e2faea4cSKurt Hackel 					     "a $RECOVERY lock for dead "
879e2faea4cSKurt Hackel 					     "node %u (%s)!\n",
880e2faea4cSKurt Hackel 					     dead_node, dlm->name);
881e2faea4cSKurt Hackel 					list_del_init(&lock->list);
882e2faea4cSKurt Hackel 					dlm_lock_put(lock);
883e2faea4cSKurt Hackel 					break;
884e2faea4cSKurt Hackel 				}
885e2faea4cSKurt Hackel 			}
886e2faea4cSKurt Hackel 			spin_unlock(&res->spinlock);
8876714d8e8SKurt Hackel 			continue;
888e2faea4cSKurt Hackel 		}
889e2faea4cSKurt Hackel 
8906714d8e8SKurt Hackel 		if (res->owner == dead_node) {
8916714d8e8SKurt Hackel 			mlog(0, "found lockres owned by dead node while "
8926714d8e8SKurt Hackel 				  "doing recovery for node %u. sending it.\n",
8936714d8e8SKurt Hackel 				  dead_node);
8946714d8e8SKurt Hackel 			list_del_init(&res->recovering);
8956714d8e8SKurt Hackel 			list_add_tail(&res->recovering, list);
8966714d8e8SKurt Hackel 		} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
8976714d8e8SKurt Hackel 			mlog(0, "found UNKNOWN owner while doing recovery "
8986714d8e8SKurt Hackel 				  "for node %u. sending it.\n", dead_node);
8996714d8e8SKurt Hackel 			list_del_init(&res->recovering);
9006714d8e8SKurt Hackel 			list_add_tail(&res->recovering, list);
9016714d8e8SKurt Hackel 		}
9026714d8e8SKurt Hackel 	}
9036714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
9046714d8e8SKurt Hackel }
9056714d8e8SKurt Hackel 
9066714d8e8SKurt Hackel static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
9076714d8e8SKurt Hackel {
9086714d8e8SKurt Hackel 	int total_locks = 0;
9096714d8e8SKurt Hackel 	struct list_head *iter, *queue = &res->granted;
9106714d8e8SKurt Hackel 	int i;
9116714d8e8SKurt Hackel 
9126714d8e8SKurt Hackel 	for (i=0; i<3; i++) {
9136714d8e8SKurt Hackel 		list_for_each(iter, queue)
9146714d8e8SKurt Hackel 			total_locks++;
9156714d8e8SKurt Hackel 		queue++;
9166714d8e8SKurt Hackel 	}
9176714d8e8SKurt Hackel 	return total_locks;
9186714d8e8SKurt Hackel }
9196714d8e8SKurt Hackel 
9206714d8e8SKurt Hackel 
9216714d8e8SKurt Hackel static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
9226714d8e8SKurt Hackel 				      struct dlm_migratable_lockres *mres,
9236714d8e8SKurt Hackel 				      u8 send_to,
9246714d8e8SKurt Hackel 				      struct dlm_lock_resource *res,
9256714d8e8SKurt Hackel 				      int total_locks)
9266714d8e8SKurt Hackel {
9276714d8e8SKurt Hackel 	u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
9286714d8e8SKurt Hackel 	int mres_total_locks = be32_to_cpu(mres->total_locks);
9296714d8e8SKurt Hackel 	int sz, ret = 0, status = 0;
9306714d8e8SKurt Hackel 	u8 orig_flags = mres->flags,
9316714d8e8SKurt Hackel 	   orig_master = mres->master;
9326714d8e8SKurt Hackel 
9336714d8e8SKurt Hackel 	BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
9346714d8e8SKurt Hackel 	if (!mres->num_locks)
9356714d8e8SKurt Hackel 		return 0;
9366714d8e8SKurt Hackel 
9376714d8e8SKurt Hackel 	sz = sizeof(struct dlm_migratable_lockres) +
9386714d8e8SKurt Hackel 		(mres->num_locks * sizeof(struct dlm_migratable_lock));
9396714d8e8SKurt Hackel 
9406714d8e8SKurt Hackel 	/* add an all-done flag if we reached the last lock */
9416714d8e8SKurt Hackel 	orig_flags = mres->flags;
9426714d8e8SKurt Hackel 	BUG_ON(total_locks > mres_total_locks);
9436714d8e8SKurt Hackel 	if (total_locks == mres_total_locks)
9446714d8e8SKurt Hackel 		mres->flags |= DLM_MRES_ALL_DONE;
9456714d8e8SKurt Hackel 
9466714d8e8SKurt Hackel 	/* send it */
9476714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
9486714d8e8SKurt Hackel 				 sz, send_to, &status);
9496714d8e8SKurt Hackel 	if (ret < 0) {
9506714d8e8SKurt Hackel 		/* XXX: negative status is not handled.
9516714d8e8SKurt Hackel 		 * this will end up killing this node. */
9526714d8e8SKurt Hackel 		mlog_errno(ret);
9536714d8e8SKurt Hackel 	} else {
9546714d8e8SKurt Hackel 		/* might get an -ENOMEM back here */
9556714d8e8SKurt Hackel 		ret = status;
9566714d8e8SKurt Hackel 		if (ret < 0) {
9576714d8e8SKurt Hackel 			mlog_errno(ret);
9586714d8e8SKurt Hackel 
9596714d8e8SKurt Hackel 			if (ret == -EFAULT) {
9606714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u told me to kill "
9616714d8e8SKurt Hackel 				     "myself!\n", send_to);
9626714d8e8SKurt Hackel 				BUG();
9636714d8e8SKurt Hackel 			}
9646714d8e8SKurt Hackel 		}
9656714d8e8SKurt Hackel 	}
9666714d8e8SKurt Hackel 
9676714d8e8SKurt Hackel 	/* zero and reinit the message buffer */
9686714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
9696714d8e8SKurt Hackel 				    res->lockname.len, mres_total_locks,
9706714d8e8SKurt Hackel 				    mig_cookie, orig_flags, orig_master);
9716714d8e8SKurt Hackel 	return ret;
9726714d8e8SKurt Hackel }
9736714d8e8SKurt Hackel 
9746714d8e8SKurt Hackel static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
9756714d8e8SKurt Hackel 					const char *lockname, int namelen,
9766714d8e8SKurt Hackel 					int total_locks, u64 cookie,
9776714d8e8SKurt Hackel 					u8 flags, u8 master)
9786714d8e8SKurt Hackel {
9796714d8e8SKurt Hackel 	/* mres here is one full page */
9806714d8e8SKurt Hackel 	memset(mres, 0, PAGE_SIZE);
9816714d8e8SKurt Hackel 	mres->lockname_len = namelen;
9826714d8e8SKurt Hackel 	memcpy(mres->lockname, lockname, namelen);
9836714d8e8SKurt Hackel 	mres->num_locks = 0;
9846714d8e8SKurt Hackel 	mres->total_locks = cpu_to_be32(total_locks);
9856714d8e8SKurt Hackel 	mres->mig_cookie = cpu_to_be64(cookie);
9866714d8e8SKurt Hackel 	mres->flags = flags;
9876714d8e8SKurt Hackel 	mres->master = master;
9886714d8e8SKurt Hackel }
9896714d8e8SKurt Hackel 
9906714d8e8SKurt Hackel 
9916714d8e8SKurt Hackel /* returns 1 if this lock fills the network structure,
9926714d8e8SKurt Hackel  * 0 otherwise */
9936714d8e8SKurt Hackel static int dlm_add_lock_to_array(struct dlm_lock *lock,
9946714d8e8SKurt Hackel 				 struct dlm_migratable_lockres *mres, int queue)
9956714d8e8SKurt Hackel {
9966714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
9976714d8e8SKurt Hackel 	int lock_num = mres->num_locks;
9986714d8e8SKurt Hackel 
9996714d8e8SKurt Hackel 	ml = &(mres->ml[lock_num]);
10006714d8e8SKurt Hackel 	ml->cookie = lock->ml.cookie;
10016714d8e8SKurt Hackel 	ml->type = lock->ml.type;
10026714d8e8SKurt Hackel 	ml->convert_type = lock->ml.convert_type;
10036714d8e8SKurt Hackel 	ml->highest_blocked = lock->ml.highest_blocked;
10046714d8e8SKurt Hackel 	ml->list = queue;
10056714d8e8SKurt Hackel 	if (lock->lksb) {
10066714d8e8SKurt Hackel 		ml->flags = lock->lksb->flags;
10076714d8e8SKurt Hackel 		/* send our current lvb */
10086714d8e8SKurt Hackel 		if (ml->type == LKM_EXMODE ||
10096714d8e8SKurt Hackel 		    ml->type == LKM_PRMODE) {
10106714d8e8SKurt Hackel 			/* if it is already set, this had better be a PR
10116714d8e8SKurt Hackel 			 * and it has to match */
10126714d8e8SKurt Hackel 			if (mres->lvb[0] && (ml->type == LKM_EXMODE ||
10136714d8e8SKurt Hackel 			    memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
10146714d8e8SKurt Hackel 				mlog(ML_ERROR, "mismatched lvbs!\n");
10156714d8e8SKurt Hackel 				__dlm_print_one_lock_resource(lock->lockres);
10166714d8e8SKurt Hackel 				BUG();
10176714d8e8SKurt Hackel 			}
10186714d8e8SKurt Hackel 			memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
10196714d8e8SKurt Hackel 		}
10206714d8e8SKurt Hackel 	}
10216714d8e8SKurt Hackel 	ml->node = lock->ml.node;
10226714d8e8SKurt Hackel 	mres->num_locks++;
10236714d8e8SKurt Hackel 	/* we reached the max, send this network message */
10246714d8e8SKurt Hackel 	if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
10256714d8e8SKurt Hackel 		return 1;
10266714d8e8SKurt Hackel 	return 0;
10276714d8e8SKurt Hackel }
10286714d8e8SKurt Hackel 
10296714d8e8SKurt Hackel 
10306714d8e8SKurt Hackel int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
10316714d8e8SKurt Hackel 			 struct dlm_migratable_lockres *mres,
10326714d8e8SKurt Hackel 			 u8 send_to, u8 flags)
10336714d8e8SKurt Hackel {
10346714d8e8SKurt Hackel 	struct list_head *queue, *iter;
10356714d8e8SKurt Hackel 	int total_locks, i;
10366714d8e8SKurt Hackel 	u64 mig_cookie = 0;
10376714d8e8SKurt Hackel 	struct dlm_lock *lock;
10386714d8e8SKurt Hackel 	int ret = 0;
10396714d8e8SKurt Hackel 
10406714d8e8SKurt Hackel 	BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
10416714d8e8SKurt Hackel 
10426714d8e8SKurt Hackel 	mlog(0, "sending to %u\n", send_to);
10436714d8e8SKurt Hackel 
10446714d8e8SKurt Hackel 	total_locks = dlm_num_locks_in_lockres(res);
10456714d8e8SKurt Hackel 	if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
10466714d8e8SKurt Hackel 		/* rare, but possible */
10476714d8e8SKurt Hackel 		mlog(0, "argh.  lockres has %d locks.  this will "
10486714d8e8SKurt Hackel 			  "require more than one network packet to "
10496714d8e8SKurt Hackel 			  "migrate\n", total_locks);
10506714d8e8SKurt Hackel 		mig_cookie = dlm_get_next_mig_cookie();
10516714d8e8SKurt Hackel 	}
10526714d8e8SKurt Hackel 
10536714d8e8SKurt Hackel 	dlm_init_migratable_lockres(mres, res->lockname.name,
10546714d8e8SKurt Hackel 				    res->lockname.len, total_locks,
10556714d8e8SKurt Hackel 				    mig_cookie, flags, res->owner);
10566714d8e8SKurt Hackel 
10576714d8e8SKurt Hackel 	total_locks = 0;
10586714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
10596714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
10606714d8e8SKurt Hackel 		list_for_each(iter, queue) {
10616714d8e8SKurt Hackel 			lock = list_entry (iter, struct dlm_lock, list);
10626714d8e8SKurt Hackel 
10636714d8e8SKurt Hackel 			/* add another lock. */
10646714d8e8SKurt Hackel 			total_locks++;
10656714d8e8SKurt Hackel 			if (!dlm_add_lock_to_array(lock, mres, i))
10666714d8e8SKurt Hackel 				continue;
10676714d8e8SKurt Hackel 
10686714d8e8SKurt Hackel 			/* this filled the lock message,
10696714d8e8SKurt Hackel 			 * we must send it immediately. */
10706714d8e8SKurt Hackel 			ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
10716714d8e8SKurt Hackel 						       res, total_locks);
10726714d8e8SKurt Hackel 			if (ret < 0) {
10736714d8e8SKurt Hackel 				// TODO
10746714d8e8SKurt Hackel 				mlog(ML_ERROR, "dlm_send_mig_lockres_msg "
10756714d8e8SKurt Hackel 				     "returned %d, TODO\n", ret);
10766714d8e8SKurt Hackel 				BUG();
10776714d8e8SKurt Hackel 			}
10786714d8e8SKurt Hackel 		}
10796714d8e8SKurt Hackel 	}
10806714d8e8SKurt Hackel 	/* flush any remaining locks */
10816714d8e8SKurt Hackel 	ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
10826714d8e8SKurt Hackel 	if (ret < 0) {
10836714d8e8SKurt Hackel 		// TODO
10846714d8e8SKurt Hackel 		mlog(ML_ERROR, "dlm_send_mig_lockres_msg returned %d, "
10856714d8e8SKurt Hackel 		     "TODO\n", ret);
10866714d8e8SKurt Hackel 		BUG();
10876714d8e8SKurt Hackel 	}
10886714d8e8SKurt Hackel 	return ret;
10896714d8e8SKurt Hackel }
10906714d8e8SKurt Hackel 
10916714d8e8SKurt Hackel 
10926714d8e8SKurt Hackel 
10936714d8e8SKurt Hackel /*
10946714d8e8SKurt Hackel  * this message will contain no more than one page worth of
10956714d8e8SKurt Hackel  * recovery data, and it will work on only one lockres.
10966714d8e8SKurt Hackel  * there may be many locks in this page, and we may need to wait
10976714d8e8SKurt Hackel  * for additional packets to complete all the locks (rare, but
10986714d8e8SKurt Hackel  * possible).
10996714d8e8SKurt Hackel  */
11006714d8e8SKurt Hackel /*
11016714d8e8SKurt Hackel  * NOTE: the allocation error cases here are scary
11026714d8e8SKurt Hackel  * we really cannot afford to fail an alloc in recovery
11036714d8e8SKurt Hackel  * do we spin?  returning an error only delays the problem really
11046714d8e8SKurt Hackel  */
11056714d8e8SKurt Hackel 
11066714d8e8SKurt Hackel int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
11076714d8e8SKurt Hackel {
11086714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
11096714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres =
11106714d8e8SKurt Hackel 		(struct dlm_migratable_lockres *)msg->buf;
11116714d8e8SKurt Hackel 	int ret = 0;
11126714d8e8SKurt Hackel 	u8 real_master;
11136714d8e8SKurt Hackel 	char *buf = NULL;
11146714d8e8SKurt Hackel 	struct dlm_work_item *item = NULL;
11156714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
11166714d8e8SKurt Hackel 
11176714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
11186714d8e8SKurt Hackel 		return -EINVAL;
11196714d8e8SKurt Hackel 
11206714d8e8SKurt Hackel 	BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
11216714d8e8SKurt Hackel 
11226714d8e8SKurt Hackel 	real_master = mres->master;
11236714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
11246714d8e8SKurt Hackel 		/* cannot migrate a lockres with no master */
11256714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
11266714d8e8SKurt Hackel 	}
11276714d8e8SKurt Hackel 
11286714d8e8SKurt Hackel 	mlog(0, "%s message received from node %u\n",
11296714d8e8SKurt Hackel 		  (mres->flags & DLM_MRES_RECOVERY) ?
11306714d8e8SKurt Hackel 		  "recovery" : "migration", mres->master);
11316714d8e8SKurt Hackel 	if (mres->flags & DLM_MRES_ALL_DONE)
11326714d8e8SKurt Hackel 		mlog(0, "all done flag.  all lockres data received!\n");
11336714d8e8SKurt Hackel 
11346714d8e8SKurt Hackel 	ret = -ENOMEM;
11356714d8e8SKurt Hackel 	buf = kmalloc(be16_to_cpu(msg->data_len), GFP_KERNEL);
11366714d8e8SKurt Hackel 	item = kcalloc(1, sizeof(*item), GFP_KERNEL);
11376714d8e8SKurt Hackel 	if (!buf || !item)
11386714d8e8SKurt Hackel 		goto leave;
11396714d8e8SKurt Hackel 
11406714d8e8SKurt Hackel 	/* lookup the lock to see if we have a secondary queue for this
11416714d8e8SKurt Hackel 	 * already...  just add the locks in and this will have its owner
11426714d8e8SKurt Hackel 	 * and RECOVERY flag changed when it completes. */
11436714d8e8SKurt Hackel 	res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
11446714d8e8SKurt Hackel 	if (res) {
11456714d8e8SKurt Hackel 	 	/* this will get a ref on res */
11466714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
11476714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
11486714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY) {
11496714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
11506714d8e8SKurt Hackel 		} else {
11516714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_MIGRATING) {
11526714d8e8SKurt Hackel 				/* this is at least the second
11536714d8e8SKurt Hackel 				 * lockres message */
11546714d8e8SKurt Hackel 				mlog(0, "lock %.*s is already migrating\n",
11556714d8e8SKurt Hackel 					  mres->lockname_len,
11566714d8e8SKurt Hackel 					  mres->lockname);
11576714d8e8SKurt Hackel 			} else if (res->state & DLM_LOCK_RES_RECOVERING) {
11586714d8e8SKurt Hackel 				/* caller should BUG */
11596714d8e8SKurt Hackel 				mlog(ML_ERROR, "node is attempting to migrate "
11606714d8e8SKurt Hackel 				     "lock %.*s, but marked as recovering!\n",
11616714d8e8SKurt Hackel 				     mres->lockname_len, mres->lockname);
11626714d8e8SKurt Hackel 				ret = -EFAULT;
11636714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
11646714d8e8SKurt Hackel 				goto leave;
11656714d8e8SKurt Hackel 			}
11666714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
11676714d8e8SKurt Hackel 		}
11686714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
11696714d8e8SKurt Hackel 	} else {
11706714d8e8SKurt Hackel 		/* need to allocate, just like if it was
11716714d8e8SKurt Hackel 		 * mastered here normally  */
11726714d8e8SKurt Hackel 		res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
11736714d8e8SKurt Hackel 		if (!res)
11746714d8e8SKurt Hackel 			goto leave;
11756714d8e8SKurt Hackel 
11766714d8e8SKurt Hackel 		/* to match the ref that we would have gotten if
11776714d8e8SKurt Hackel 		 * dlm_lookup_lockres had succeeded */
11786714d8e8SKurt Hackel 		dlm_lockres_get(res);
11796714d8e8SKurt Hackel 
11806714d8e8SKurt Hackel 		/* mark it as recovering/migrating and hash it */
11816714d8e8SKurt Hackel 		if (mres->flags & DLM_MRES_RECOVERY)
11826714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_RECOVERING;
11836714d8e8SKurt Hackel 		else
11846714d8e8SKurt Hackel 			res->state |= DLM_LOCK_RES_MIGRATING;
11856714d8e8SKurt Hackel 
11866714d8e8SKurt Hackel 		spin_lock(&dlm->spinlock);
11876714d8e8SKurt Hackel 		__dlm_insert_lockres(dlm, res);
11886714d8e8SKurt Hackel 		spin_unlock(&dlm->spinlock);
11896714d8e8SKurt Hackel 
11906714d8e8SKurt Hackel 		/* now that the new lockres is inserted,
11916714d8e8SKurt Hackel 		 * make it usable by other processes */
11926714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
11936714d8e8SKurt Hackel 		res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
11946714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
11956714d8e8SKurt Hackel 
11966714d8e8SKurt Hackel 		/* add an extra ref for just-allocated lockres
11976714d8e8SKurt Hackel 		 * otherwise the lockres will be purged immediately */
11986714d8e8SKurt Hackel 		dlm_lockres_get(res);
11996714d8e8SKurt Hackel 
12006714d8e8SKurt Hackel 	}
12016714d8e8SKurt Hackel 
12026714d8e8SKurt Hackel 	/* at this point we have allocated everything we need,
12036714d8e8SKurt Hackel 	 * and we have a hashed lockres with an extra ref and
12046714d8e8SKurt Hackel 	 * the proper res->state flags. */
12056714d8e8SKurt Hackel 	ret = 0;
12066714d8e8SKurt Hackel 	if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
12076714d8e8SKurt Hackel 		/* migration cannot have an unknown master */
12086714d8e8SKurt Hackel 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
12096714d8e8SKurt Hackel 		mlog(0, "recovery has passed me a lockres with an "
12106714d8e8SKurt Hackel 			  "unknown owner.. will need to requery: "
12116714d8e8SKurt Hackel 			  "%.*s\n", mres->lockname_len, mres->lockname);
12126714d8e8SKurt Hackel 	} else {
12136714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
12146714d8e8SKurt Hackel 		dlm_change_lockres_owner(dlm, res, dlm->node_num);
12156714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
12166714d8e8SKurt Hackel 	}
12176714d8e8SKurt Hackel 
12186714d8e8SKurt Hackel 	/* queue up work for dlm_mig_lockres_worker */
12196714d8e8SKurt Hackel 	dlm_grab(dlm);  /* get an extra ref for the work item */
12206714d8e8SKurt Hackel 	memcpy(buf, msg->buf, be16_to_cpu(msg->data_len));  /* copy the whole message */
12216714d8e8SKurt Hackel 	dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
12226714d8e8SKurt Hackel 	item->u.ml.lockres = res; /* already have a ref */
12236714d8e8SKurt Hackel 	item->u.ml.real_master = real_master;
12246714d8e8SKurt Hackel 	spin_lock(&dlm->work_lock);
12256714d8e8SKurt Hackel 	list_add_tail(&item->list, &dlm->work_list);
12266714d8e8SKurt Hackel 	spin_unlock(&dlm->work_lock);
12276714d8e8SKurt Hackel 	schedule_work(&dlm->dispatched_work);
12286714d8e8SKurt Hackel 
12296714d8e8SKurt Hackel leave:
12306714d8e8SKurt Hackel 	dlm_put(dlm);
12316714d8e8SKurt Hackel 	if (ret < 0) {
12326714d8e8SKurt Hackel 		if (buf)
12336714d8e8SKurt Hackel 			kfree(buf);
12346714d8e8SKurt Hackel 		if (item)
12356714d8e8SKurt Hackel 			kfree(item);
12366714d8e8SKurt Hackel 	}
12376714d8e8SKurt Hackel 
12386714d8e8SKurt Hackel 	mlog_exit(ret);
12396714d8e8SKurt Hackel 	return ret;
12406714d8e8SKurt Hackel }
12416714d8e8SKurt Hackel 
12426714d8e8SKurt Hackel 
12436714d8e8SKurt Hackel static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
12446714d8e8SKurt Hackel {
12456714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
12466714d8e8SKurt Hackel 	struct dlm_migratable_lockres *mres;
12476714d8e8SKurt Hackel 	int ret = 0;
12486714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
12496714d8e8SKurt Hackel 	u8 real_master;
12506714d8e8SKurt Hackel 
12516714d8e8SKurt Hackel 	dlm = item->dlm;
12526714d8e8SKurt Hackel 	mres = (struct dlm_migratable_lockres *)data;
12536714d8e8SKurt Hackel 
12546714d8e8SKurt Hackel 	res = item->u.ml.lockres;
12556714d8e8SKurt Hackel 	real_master = item->u.ml.real_master;
12566714d8e8SKurt Hackel 
12576714d8e8SKurt Hackel 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
12586714d8e8SKurt Hackel 		/* this case is super-rare. only occurs if
12596714d8e8SKurt Hackel 		 * node death happens during migration. */
12606714d8e8SKurt Hackel again:
12616714d8e8SKurt Hackel 		ret = dlm_lockres_master_requery(dlm, res, &real_master);
12626714d8e8SKurt Hackel 		if (ret < 0) {
1263e2faea4cSKurt Hackel 			mlog(0, "dlm_lockres_master_requery ret=%d\n",
12646714d8e8SKurt Hackel 				  ret);
12656714d8e8SKurt Hackel 			goto again;
12666714d8e8SKurt Hackel 		}
12676714d8e8SKurt Hackel 		if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
12686714d8e8SKurt Hackel 			mlog(0, "lockres %.*s not claimed.  "
12696714d8e8SKurt Hackel 				   "this node will take it.\n",
12706714d8e8SKurt Hackel 				   res->lockname.len, res->lockname.name);
12716714d8e8SKurt Hackel 		} else {
12726714d8e8SKurt Hackel 			mlog(0, "master needs to respond to sender "
12736714d8e8SKurt Hackel 				  "that node %u still owns %.*s\n",
12746714d8e8SKurt Hackel 				  real_master, res->lockname.len,
12756714d8e8SKurt Hackel 				  res->lockname.name);
12766714d8e8SKurt Hackel 			/* cannot touch this lockres */
12776714d8e8SKurt Hackel 			goto leave;
12786714d8e8SKurt Hackel 		}
12796714d8e8SKurt Hackel 	}
12806714d8e8SKurt Hackel 
12816714d8e8SKurt Hackel 	ret = dlm_process_recovery_data(dlm, res, mres);
12826714d8e8SKurt Hackel 	if (ret < 0)
12836714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data returned  %d\n", ret);
12846714d8e8SKurt Hackel 	else
12856714d8e8SKurt Hackel 		mlog(0, "dlm_process_recovery_data succeeded\n");
12866714d8e8SKurt Hackel 
12876714d8e8SKurt Hackel 	if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
12886714d8e8SKurt Hackel 	                   (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
12896714d8e8SKurt Hackel 		ret = dlm_finish_migration(dlm, res, mres->master);
12906714d8e8SKurt Hackel 		if (ret < 0)
12916714d8e8SKurt Hackel 			mlog_errno(ret);
12926714d8e8SKurt Hackel 	}
12936714d8e8SKurt Hackel 
12946714d8e8SKurt Hackel leave:
12956714d8e8SKurt Hackel 	kfree(data);
12966714d8e8SKurt Hackel 	mlog_exit(ret);
12976714d8e8SKurt Hackel }
12986714d8e8SKurt Hackel 
12996714d8e8SKurt Hackel 
13006714d8e8SKurt Hackel 
13016714d8e8SKurt Hackel static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
13026714d8e8SKurt Hackel 				      struct dlm_lock_resource *res,
13036714d8e8SKurt Hackel 				      u8 *real_master)
13046714d8e8SKurt Hackel {
13056714d8e8SKurt Hackel 	struct dlm_node_iter iter;
13066714d8e8SKurt Hackel 	int nodenum;
13076714d8e8SKurt Hackel 	int ret = 0;
13086714d8e8SKurt Hackel 
13096714d8e8SKurt Hackel 	*real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
13106714d8e8SKurt Hackel 
13116714d8e8SKurt Hackel 	/* we only reach here if one of the two nodes in a
13126714d8e8SKurt Hackel 	 * migration died while the migration was in progress.
13136714d8e8SKurt Hackel 	 * at this point we need to requery the master.  we
13146714d8e8SKurt Hackel 	 * know that the new_master got as far as creating
13156714d8e8SKurt Hackel 	 * an mle on at least one node, but we do not know
13166714d8e8SKurt Hackel 	 * if any nodes had actually cleared the mle and set
13176714d8e8SKurt Hackel 	 * the master to the new_master.  the old master
13186714d8e8SKurt Hackel 	 * is supposed to set the owner to UNKNOWN in the
13196714d8e8SKurt Hackel 	 * event of a new_master death, so the only possible
13206714d8e8SKurt Hackel 	 * responses that we can get from nodes here are
13216714d8e8SKurt Hackel 	 * that the master is new_master, or that the master
13226714d8e8SKurt Hackel 	 * is UNKNOWN.
13236714d8e8SKurt Hackel 	 * if all nodes come back with UNKNOWN then we know
13246714d8e8SKurt Hackel 	 * the lock needs remastering here.
13256714d8e8SKurt Hackel 	 * if any node comes back with a valid master, check
13266714d8e8SKurt Hackel 	 * to see if that master is the one that we are
13276714d8e8SKurt Hackel 	 * recovering.  if so, then the new_master died and
13286714d8e8SKurt Hackel 	 * we need to remaster this lock.  if not, then the
13296714d8e8SKurt Hackel 	 * new_master survived and that node will respond to
13306714d8e8SKurt Hackel 	 * other nodes about the owner.
13316714d8e8SKurt Hackel 	 * if there is an owner, this node needs to dump this
13326714d8e8SKurt Hackel 	 * lockres and alert the sender that this lockres
13336714d8e8SKurt Hackel 	 * was rejected. */
13346714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
13356714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
13366714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
13376714d8e8SKurt Hackel 
13386714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
13396714d8e8SKurt Hackel 		/* do not send to self */
13406714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
13416714d8e8SKurt Hackel 			continue;
13426714d8e8SKurt Hackel 		ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
13436714d8e8SKurt Hackel 		if (ret < 0) {
13446714d8e8SKurt Hackel 			mlog_errno(ret);
13456714d8e8SKurt Hackel 			BUG();
13466714d8e8SKurt Hackel 			/* TODO: need to figure a way to restart this */
13476714d8e8SKurt Hackel 		}
13486714d8e8SKurt Hackel 		if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
13496714d8e8SKurt Hackel 			mlog(0, "lock master is %u\n", *real_master);
13506714d8e8SKurt Hackel 			break;
13516714d8e8SKurt Hackel 		}
13526714d8e8SKurt Hackel 	}
13536714d8e8SKurt Hackel 	return ret;
13546714d8e8SKurt Hackel }
13556714d8e8SKurt Hackel 
13566714d8e8SKurt Hackel 
13576714d8e8SKurt Hackel static int dlm_do_master_requery(struct dlm_ctxt *dlm,
13586714d8e8SKurt Hackel 				 struct dlm_lock_resource *res,
13596714d8e8SKurt Hackel 				 u8 nodenum, u8 *real_master)
13606714d8e8SKurt Hackel {
13616714d8e8SKurt Hackel 	int ret = -EINVAL;
13626714d8e8SKurt Hackel 	struct dlm_master_requery req;
13636714d8e8SKurt Hackel 	int status = DLM_LOCK_RES_OWNER_UNKNOWN;
13646714d8e8SKurt Hackel 
13656714d8e8SKurt Hackel 	memset(&req, 0, sizeof(req));
13666714d8e8SKurt Hackel 	req.node_idx = dlm->node_num;
13676714d8e8SKurt Hackel 	req.namelen = res->lockname.len;
13686714d8e8SKurt Hackel 	memcpy(req.name, res->lockname.name, res->lockname.len);
13696714d8e8SKurt Hackel 
13706714d8e8SKurt Hackel 	ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
13716714d8e8SKurt Hackel 				 &req, sizeof(req), nodenum, &status);
13726714d8e8SKurt Hackel 	/* XXX: negative status not handled properly here. */
13736714d8e8SKurt Hackel 	if (ret < 0)
13746714d8e8SKurt Hackel 		mlog_errno(ret);
13756714d8e8SKurt Hackel 	else {
13766714d8e8SKurt Hackel 		BUG_ON(status < 0);
13776714d8e8SKurt Hackel 		BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
13786714d8e8SKurt Hackel 		*real_master = (u8) (status & 0xff);
13796714d8e8SKurt Hackel 		mlog(0, "node %u responded to master requery with %u\n",
13806714d8e8SKurt Hackel 			  nodenum, *real_master);
13816714d8e8SKurt Hackel 		ret = 0;
13826714d8e8SKurt Hackel 	}
13836714d8e8SKurt Hackel 	return ret;
13846714d8e8SKurt Hackel }
13856714d8e8SKurt Hackel 
13866714d8e8SKurt Hackel 
13876714d8e8SKurt Hackel /* this function cannot error, so unless the sending
13886714d8e8SKurt Hackel  * or receiving of the message failed, the owner can
13896714d8e8SKurt Hackel  * be trusted */
13906714d8e8SKurt Hackel int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
13916714d8e8SKurt Hackel {
13926714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
13936714d8e8SKurt Hackel 	struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
13946714d8e8SKurt Hackel 	struct dlm_lock_resource *res = NULL;
13956714d8e8SKurt Hackel 	int master = DLM_LOCK_RES_OWNER_UNKNOWN;
13966714d8e8SKurt Hackel 	u32 flags = DLM_ASSERT_MASTER_REQUERY;
13976714d8e8SKurt Hackel 
13986714d8e8SKurt Hackel 	if (!dlm_grab(dlm)) {
13996714d8e8SKurt Hackel 		/* since the domain has gone away on this
14006714d8e8SKurt Hackel 		 * node, the proper response is UNKNOWN */
14016714d8e8SKurt Hackel 		return master;
14026714d8e8SKurt Hackel 	}
14036714d8e8SKurt Hackel 
14046714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
14056714d8e8SKurt Hackel 	res = __dlm_lookup_lockres(dlm, req->name, req->namelen);
14066714d8e8SKurt Hackel 	if (res) {
14076714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
14086714d8e8SKurt Hackel 		master = res->owner;
14096714d8e8SKurt Hackel 		if (master == dlm->node_num) {
14106714d8e8SKurt Hackel 			int ret = dlm_dispatch_assert_master(dlm, res,
14116714d8e8SKurt Hackel 							     0, 0, flags);
14126714d8e8SKurt Hackel 			if (ret < 0) {
14136714d8e8SKurt Hackel 				mlog_errno(-ENOMEM);
14146714d8e8SKurt Hackel 				/* retry!? */
14156714d8e8SKurt Hackel 				BUG();
14166714d8e8SKurt Hackel 			}
14176714d8e8SKurt Hackel 		}
14186714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
14196714d8e8SKurt Hackel 	}
14206714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
14216714d8e8SKurt Hackel 
14226714d8e8SKurt Hackel 	dlm_put(dlm);
14236714d8e8SKurt Hackel 	return master;
14246714d8e8SKurt Hackel }
14256714d8e8SKurt Hackel 
14266714d8e8SKurt Hackel static inline struct list_head *
14276714d8e8SKurt Hackel dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
14286714d8e8SKurt Hackel {
14296714d8e8SKurt Hackel 	struct list_head *ret;
14306714d8e8SKurt Hackel 	BUG_ON(list_num < 0);
14316714d8e8SKurt Hackel 	BUG_ON(list_num > 2);
14326714d8e8SKurt Hackel 	ret = &(res->granted);
14336714d8e8SKurt Hackel 	ret += list_num;
14346714d8e8SKurt Hackel 	return ret;
14356714d8e8SKurt Hackel }
14366714d8e8SKurt Hackel /* TODO: do ast flush business
14376714d8e8SKurt Hackel  * TODO: do MIGRATING and RECOVERING spinning
14386714d8e8SKurt Hackel  */
14396714d8e8SKurt Hackel 
14406714d8e8SKurt Hackel /*
14416714d8e8SKurt Hackel * NOTE about in-flight requests during migration:
14426714d8e8SKurt Hackel *
14436714d8e8SKurt Hackel * Before attempting the migrate, the master has marked the lockres as
14446714d8e8SKurt Hackel * MIGRATING and then flushed all of its pending ASTS.  So any in-flight
14456714d8e8SKurt Hackel * requests either got queued before the MIGRATING flag got set, in which
14466714d8e8SKurt Hackel * case the lock data will reflect the change and a return message is on
14476714d8e8SKurt Hackel * the way, or the request failed to get in before MIGRATING got set.  In
14486714d8e8SKurt Hackel * this case, the caller will be told to spin and wait for the MIGRATING
14496714d8e8SKurt Hackel * flag to be dropped, then recheck the master.
14506714d8e8SKurt Hackel * This holds true for the convert, cancel and unlock cases, and since lvb
14516714d8e8SKurt Hackel * updates are tied to these same messages, it applies to lvb updates as
14526714d8e8SKurt Hackel * well.  For the lock case, there is no way a lock can be on the master
14536714d8e8SKurt Hackel * queue and not be on the secondary queue since the lock is always added
14546714d8e8SKurt Hackel * locally first.  This means that the new target node will never be sent
14556714d8e8SKurt Hackel * a lock that he doesn't already have on the list.
14566714d8e8SKurt Hackel * In total, this means that the local lock is correct and should not be
14576714d8e8SKurt Hackel * updated to match the one sent by the master.  Any messages sent back
14586714d8e8SKurt Hackel * from the master before the MIGRATING flag will bring the lock properly
14596714d8e8SKurt Hackel * up-to-date, and the change will be ordered properly for the waiter.
14606714d8e8SKurt Hackel * We will *not* attempt to modify the lock underneath the waiter.
14616714d8e8SKurt Hackel */
14626714d8e8SKurt Hackel 
14636714d8e8SKurt Hackel static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
14646714d8e8SKurt Hackel 				     struct dlm_lock_resource *res,
14656714d8e8SKurt Hackel 				     struct dlm_migratable_lockres *mres)
14666714d8e8SKurt Hackel {
14676714d8e8SKurt Hackel 	struct dlm_migratable_lock *ml;
14686714d8e8SKurt Hackel 	struct list_head *queue;
14696714d8e8SKurt Hackel 	struct dlm_lock *newlock = NULL;
14706714d8e8SKurt Hackel 	struct dlm_lockstatus *lksb = NULL;
14716714d8e8SKurt Hackel 	int ret = 0;
14726714d8e8SKurt Hackel 	int i;
14736714d8e8SKurt Hackel 	struct list_head *iter;
14746714d8e8SKurt Hackel 	struct dlm_lock *lock = NULL;
14756714d8e8SKurt Hackel 
14766714d8e8SKurt Hackel 	mlog(0, "running %d locks for this lockres\n", mres->num_locks);
14776714d8e8SKurt Hackel 	for (i=0; i<mres->num_locks; i++) {
14786714d8e8SKurt Hackel 		ml = &(mres->ml[i]);
14796714d8e8SKurt Hackel 		BUG_ON(ml->highest_blocked != LKM_IVMODE);
14806714d8e8SKurt Hackel 		newlock = NULL;
14816714d8e8SKurt Hackel 		lksb = NULL;
14826714d8e8SKurt Hackel 
14836714d8e8SKurt Hackel 		queue = dlm_list_num_to_pointer(res, ml->list);
14846714d8e8SKurt Hackel 
14856714d8e8SKurt Hackel 		/* if the lock is for the local node it needs to
14866714d8e8SKurt Hackel 		 * be moved to the proper location within the queue.
14876714d8e8SKurt Hackel 		 * do not allocate a new lock structure. */
14886714d8e8SKurt Hackel 		if (ml->node == dlm->node_num) {
14896714d8e8SKurt Hackel 			/* MIGRATION ONLY! */
14906714d8e8SKurt Hackel 			BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
14916714d8e8SKurt Hackel 
14926714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
14936714d8e8SKurt Hackel 			list_for_each(iter, queue) {
14946714d8e8SKurt Hackel 				lock = list_entry (iter, struct dlm_lock, list);
14956714d8e8SKurt Hackel 				if (lock->ml.cookie != ml->cookie)
14966714d8e8SKurt Hackel 					lock = NULL;
14976714d8e8SKurt Hackel 				else
14986714d8e8SKurt Hackel 					break;
14996714d8e8SKurt Hackel 			}
15006714d8e8SKurt Hackel 
15016714d8e8SKurt Hackel 			/* lock is always created locally first, and
15026714d8e8SKurt Hackel 			 * destroyed locally last.  it must be on the list */
15036714d8e8SKurt Hackel 			if (!lock) {
15046714d8e8SKurt Hackel 				mlog(ML_ERROR, "could not find local lock "
15056714d8e8SKurt Hackel 					       "with cookie %"MLFu64"!\n",
15066714d8e8SKurt Hackel 				     ml->cookie);
15076714d8e8SKurt Hackel 				BUG();
15086714d8e8SKurt Hackel 			}
15096714d8e8SKurt Hackel 			BUG_ON(lock->ml.node != ml->node);
15106714d8e8SKurt Hackel 
15116714d8e8SKurt Hackel 			/* see NOTE above about why we do not update
15126714d8e8SKurt Hackel 			 * to match the master here */
15136714d8e8SKurt Hackel 
15146714d8e8SKurt Hackel 			/* move the lock to its proper place */
15156714d8e8SKurt Hackel 			/* do not alter lock refcount.  switching lists. */
15166714d8e8SKurt Hackel 			list_del_init(&lock->list);
15176714d8e8SKurt Hackel 			list_add_tail(&lock->list, queue);
15186714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
15196714d8e8SKurt Hackel 
15206714d8e8SKurt Hackel 			mlog(0, "just reordered a local lock!\n");
15216714d8e8SKurt Hackel 			continue;
15226714d8e8SKurt Hackel 		}
15236714d8e8SKurt Hackel 
15246714d8e8SKurt Hackel 		/* lock is for another node. */
15256714d8e8SKurt Hackel 		newlock = dlm_new_lock(ml->type, ml->node,
15266714d8e8SKurt Hackel 				       be64_to_cpu(ml->cookie), NULL);
15276714d8e8SKurt Hackel 		if (!newlock) {
15286714d8e8SKurt Hackel 			ret = -ENOMEM;
15296714d8e8SKurt Hackel 			goto leave;
15306714d8e8SKurt Hackel 		}
15316714d8e8SKurt Hackel 		lksb = newlock->lksb;
15326714d8e8SKurt Hackel 		dlm_lock_attach_lockres(newlock, res);
15336714d8e8SKurt Hackel 
15346714d8e8SKurt Hackel 		if (ml->convert_type != LKM_IVMODE) {
15356714d8e8SKurt Hackel 			BUG_ON(queue != &res->converting);
15366714d8e8SKurt Hackel 			newlock->ml.convert_type = ml->convert_type;
15376714d8e8SKurt Hackel 		}
15386714d8e8SKurt Hackel 		lksb->flags |= (ml->flags &
15396714d8e8SKurt Hackel 				(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
15406714d8e8SKurt Hackel 
15416714d8e8SKurt Hackel 		if (mres->lvb[0]) {
15426714d8e8SKurt Hackel 			if (lksb->flags & DLM_LKSB_PUT_LVB) {
15436714d8e8SKurt Hackel 				/* other node was trying to update
15446714d8e8SKurt Hackel 				 * lvb when node died.  recreate the
15456714d8e8SKurt Hackel 				 * lksb with the updated lvb. */
15466714d8e8SKurt Hackel 				memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
15476714d8e8SKurt Hackel 			} else {
15486714d8e8SKurt Hackel 				/* otherwise, the node is sending its
15496714d8e8SKurt Hackel 				 * most recent valid lvb info */
15506714d8e8SKurt Hackel 				BUG_ON(ml->type != LKM_EXMODE &&
15516714d8e8SKurt Hackel 				       ml->type != LKM_PRMODE);
15526714d8e8SKurt Hackel 				if (res->lvb[0] && (ml->type == LKM_EXMODE ||
15536714d8e8SKurt Hackel 				    memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
15546714d8e8SKurt Hackel 					mlog(ML_ERROR, "received bad lvb!\n");
15556714d8e8SKurt Hackel 					__dlm_print_one_lock_resource(res);
15566714d8e8SKurt Hackel 					BUG();
15576714d8e8SKurt Hackel 				}
15586714d8e8SKurt Hackel 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
15596714d8e8SKurt Hackel 			}
15606714d8e8SKurt Hackel 		}
15616714d8e8SKurt Hackel 
15626714d8e8SKurt Hackel 
15636714d8e8SKurt Hackel 		/* NOTE:
15646714d8e8SKurt Hackel 		 * wrt lock queue ordering and recovery:
15656714d8e8SKurt Hackel 		 *    1. order of locks on granted queue is
15666714d8e8SKurt Hackel 		 *       meaningless.
15676714d8e8SKurt Hackel 		 *    2. order of locks on converting queue is
15686714d8e8SKurt Hackel 		 *       LOST with the node death.  sorry charlie.
15696714d8e8SKurt Hackel 		 *    3. order of locks on the blocked queue is
15706714d8e8SKurt Hackel 		 *       also LOST.
15716714d8e8SKurt Hackel 		 * order of locks does not affect integrity, it
15726714d8e8SKurt Hackel 		 * just means that a lock request may get pushed
15736714d8e8SKurt Hackel 		 * back in line as a result of the node death.
15746714d8e8SKurt Hackel 		 * also note that for a given node the lock order
15756714d8e8SKurt Hackel 		 * for its secondary queue locks is preserved
15766714d8e8SKurt Hackel 		 * relative to each other, but clearly *not*
15776714d8e8SKurt Hackel 		 * preserved relative to locks from other nodes.
15786714d8e8SKurt Hackel 		 */
15796714d8e8SKurt Hackel 		spin_lock(&res->spinlock);
15806714d8e8SKurt Hackel 		dlm_lock_get(newlock);
15816714d8e8SKurt Hackel 		list_add_tail(&newlock->list, queue);
15826714d8e8SKurt Hackel 		spin_unlock(&res->spinlock);
15836714d8e8SKurt Hackel 	}
15846714d8e8SKurt Hackel 	mlog(0, "done running all the locks\n");
15856714d8e8SKurt Hackel 
15866714d8e8SKurt Hackel leave:
15876714d8e8SKurt Hackel 	if (ret < 0) {
15886714d8e8SKurt Hackel 		mlog_errno(ret);
15896714d8e8SKurt Hackel 		if (newlock)
15906714d8e8SKurt Hackel 			dlm_lock_put(newlock);
15916714d8e8SKurt Hackel 	}
15926714d8e8SKurt Hackel 
15936714d8e8SKurt Hackel 	mlog_exit(ret);
15946714d8e8SKurt Hackel 	return ret;
15956714d8e8SKurt Hackel }
15966714d8e8SKurt Hackel 
15976714d8e8SKurt Hackel void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
15986714d8e8SKurt Hackel 				       struct dlm_lock_resource *res)
15996714d8e8SKurt Hackel {
16006714d8e8SKurt Hackel 	int i;
16016714d8e8SKurt Hackel 	struct list_head *queue, *iter, *iter2;
16026714d8e8SKurt Hackel 	struct dlm_lock *lock;
16036714d8e8SKurt Hackel 
16046714d8e8SKurt Hackel 	res->state |= DLM_LOCK_RES_RECOVERING;
16056714d8e8SKurt Hackel 	if (!list_empty(&res->recovering))
16066714d8e8SKurt Hackel 		list_del_init(&res->recovering);
16076714d8e8SKurt Hackel 	list_add_tail(&res->recovering, &dlm->reco.resources);
16086714d8e8SKurt Hackel 
16096714d8e8SKurt Hackel 	/* find any pending locks and put them back on proper list */
16106714d8e8SKurt Hackel 	for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
16116714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
16126714d8e8SKurt Hackel 		list_for_each_safe(iter, iter2, queue) {
16136714d8e8SKurt Hackel 			lock = list_entry (iter, struct dlm_lock, list);
16146714d8e8SKurt Hackel 			dlm_lock_get(lock);
16156714d8e8SKurt Hackel 			if (lock->convert_pending) {
16166714d8e8SKurt Hackel 				/* move converting lock back to granted */
16176714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
16186714d8e8SKurt Hackel 				mlog(0, "node died with convert pending "
16196714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
16206714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
16216714d8e8SKurt Hackel 				dlm_revert_pending_convert(res, lock);
16226714d8e8SKurt Hackel 				lock->convert_pending = 0;
16236714d8e8SKurt Hackel 			} else if (lock->lock_pending) {
16246714d8e8SKurt Hackel 				/* remove pending lock requests completely */
16256714d8e8SKurt Hackel 				BUG_ON(i != DLM_BLOCKED_LIST);
16266714d8e8SKurt Hackel 				mlog(0, "node died with lock pending "
16276714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
16286714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
16296714d8e8SKurt Hackel 				/* lock will be floating until ref in
16306714d8e8SKurt Hackel 				 * dlmlock_remote is freed after the network
16316714d8e8SKurt Hackel 				 * call returns.  ok for it to not be on any
16326714d8e8SKurt Hackel 				 * list since no ast can be called
16336714d8e8SKurt Hackel 				 * (the master is dead). */
16346714d8e8SKurt Hackel 				dlm_revert_pending_lock(res, lock);
16356714d8e8SKurt Hackel 				lock->lock_pending = 0;
16366714d8e8SKurt Hackel 			} else if (lock->unlock_pending) {
16376714d8e8SKurt Hackel 				/* if an unlock was in progress, treat as
16386714d8e8SKurt Hackel 				 * if this had completed successfully
16396714d8e8SKurt Hackel 				 * before sending this lock state to the
16406714d8e8SKurt Hackel 				 * new master.  note that the dlm_unlock
16416714d8e8SKurt Hackel 				 * call is still responsible for calling
16426714d8e8SKurt Hackel 				 * the unlockast.  that will happen after
16436714d8e8SKurt Hackel 				 * the network call times out.  for now,
16446714d8e8SKurt Hackel 				 * just move lists to prepare the new
16456714d8e8SKurt Hackel 				 * recovery master.  */
16466714d8e8SKurt Hackel 				BUG_ON(i != DLM_GRANTED_LIST);
16476714d8e8SKurt Hackel 				mlog(0, "node died with unlock pending "
16486714d8e8SKurt Hackel 				     "on %.*s. remove from blocked list and skip.\n",
16496714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
16506714d8e8SKurt Hackel 				dlm_commit_pending_unlock(res, lock);
16516714d8e8SKurt Hackel 				lock->unlock_pending = 0;
16526714d8e8SKurt Hackel 			} else if (lock->cancel_pending) {
16536714d8e8SKurt Hackel 				/* if a cancel was in progress, treat as
16546714d8e8SKurt Hackel 				 * if this had completed successfully
16556714d8e8SKurt Hackel 				 * before sending this lock state to the
16566714d8e8SKurt Hackel 				 * new master */
16576714d8e8SKurt Hackel 				BUG_ON(i != DLM_CONVERTING_LIST);
16586714d8e8SKurt Hackel 				mlog(0, "node died with cancel pending "
16596714d8e8SKurt Hackel 				     "on %.*s. move back to granted list.\n",
16606714d8e8SKurt Hackel 				     res->lockname.len, res->lockname.name);
16616714d8e8SKurt Hackel 				dlm_commit_pending_cancel(res, lock);
16626714d8e8SKurt Hackel 				lock->cancel_pending = 0;
16636714d8e8SKurt Hackel 			}
16646714d8e8SKurt Hackel 			dlm_lock_put(lock);
16656714d8e8SKurt Hackel 		}
16666714d8e8SKurt Hackel 	}
16676714d8e8SKurt Hackel }
16686714d8e8SKurt Hackel 
16696714d8e8SKurt Hackel 
16706714d8e8SKurt Hackel 
16716714d8e8SKurt Hackel /* removes all recovered locks from the recovery list.
16726714d8e8SKurt Hackel  * sets the res->owner to the new master.
16736714d8e8SKurt Hackel  * unsets the RECOVERY flag and wakes waiters. */
16746714d8e8SKurt Hackel static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
16756714d8e8SKurt Hackel 					      u8 dead_node, u8 new_master)
16766714d8e8SKurt Hackel {
16776714d8e8SKurt Hackel 	int i;
16786714d8e8SKurt Hackel 	struct list_head *iter, *iter2, *bucket;
16796714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
16806714d8e8SKurt Hackel 
16816714d8e8SKurt Hackel 	mlog_entry_void();
16826714d8e8SKurt Hackel 
16836714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
16846714d8e8SKurt Hackel 
16856714d8e8SKurt Hackel 	list_for_each_safe(iter, iter2, &dlm->reco.resources) {
16866714d8e8SKurt Hackel 		res = list_entry (iter, struct dlm_lock_resource, recovering);
16876714d8e8SKurt Hackel 		if (res->owner == dead_node) {
16886714d8e8SKurt Hackel 			list_del_init(&res->recovering);
16896714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
16906714d8e8SKurt Hackel 			dlm_change_lockres_owner(dlm, res, new_master);
16916714d8e8SKurt Hackel 			res->state &= ~DLM_LOCK_RES_RECOVERING;
16926714d8e8SKurt Hackel 			__dlm_dirty_lockres(dlm, res);
16936714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
16946714d8e8SKurt Hackel 			wake_up(&res->wq);
16956714d8e8SKurt Hackel 		}
16966714d8e8SKurt Hackel 	}
16976714d8e8SKurt Hackel 
16986714d8e8SKurt Hackel 	/* this will become unnecessary eventually, but
16996714d8e8SKurt Hackel 	 * for now we need to run the whole hash, clear
17006714d8e8SKurt Hackel 	 * the RECOVERING state and set the owner
17016714d8e8SKurt Hackel 	 * if necessary */
17026714d8e8SKurt Hackel 	for (i=0; i<DLM_HASH_SIZE; i++) {
17036714d8e8SKurt Hackel 		bucket = &(dlm->resources[i]);
17046714d8e8SKurt Hackel 		list_for_each(iter, bucket) {
17056714d8e8SKurt Hackel 			res = list_entry (iter, struct dlm_lock_resource, list);
17066714d8e8SKurt Hackel 			if (res->state & DLM_LOCK_RES_RECOVERING) {
17076714d8e8SKurt Hackel 				if (res->owner == dead_node) {
17086714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
17096714d8e8SKurt Hackel 					     "was not on recovering list, but "
17106714d8e8SKurt Hackel 					     "clearing state anyway\n",
17116714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
17126714d8e8SKurt Hackel 					     res->lockname.name, new_master);
17136714d8e8SKurt Hackel 				} else if (res->owner == dlm->node_num) {
17146714d8e8SKurt Hackel 					mlog(0, "(this=%u) res %.*s owner=%u "
17156714d8e8SKurt Hackel 					     "was not on recovering list, "
17166714d8e8SKurt Hackel 					     "owner is THIS node, clearing\n",
17176714d8e8SKurt Hackel 					     dlm->node_num, res->lockname.len,
17186714d8e8SKurt Hackel 					     res->lockname.name, new_master);
17196714d8e8SKurt Hackel 				} else
17206714d8e8SKurt Hackel 					continue;
17216714d8e8SKurt Hackel 
17226714d8e8SKurt Hackel 				spin_lock(&res->spinlock);
17236714d8e8SKurt Hackel 				dlm_change_lockres_owner(dlm, res, new_master);
17246714d8e8SKurt Hackel 				res->state &= ~DLM_LOCK_RES_RECOVERING;
17256714d8e8SKurt Hackel 				__dlm_dirty_lockres(dlm, res);
17266714d8e8SKurt Hackel 				spin_unlock(&res->spinlock);
17276714d8e8SKurt Hackel 				wake_up(&res->wq);
17286714d8e8SKurt Hackel 			}
17296714d8e8SKurt Hackel 		}
17306714d8e8SKurt Hackel 	}
17316714d8e8SKurt Hackel }
17326714d8e8SKurt Hackel 
17336714d8e8SKurt Hackel static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
17346714d8e8SKurt Hackel {
17356714d8e8SKurt Hackel 	if (local) {
17366714d8e8SKurt Hackel 		if (lock->ml.type != LKM_EXMODE &&
17376714d8e8SKurt Hackel 		    lock->ml.type != LKM_PRMODE)
17386714d8e8SKurt Hackel 			return 1;
17396714d8e8SKurt Hackel 	} else if (lock->ml.type == LKM_EXMODE)
17406714d8e8SKurt Hackel 		return 1;
17416714d8e8SKurt Hackel 	return 0;
17426714d8e8SKurt Hackel }
17436714d8e8SKurt Hackel 
17446714d8e8SKurt Hackel static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
17456714d8e8SKurt Hackel 			       struct dlm_lock_resource *res, u8 dead_node)
17466714d8e8SKurt Hackel {
17476714d8e8SKurt Hackel 	struct list_head *iter, *queue;
17486714d8e8SKurt Hackel 	struct dlm_lock *lock;
17496714d8e8SKurt Hackel 	int blank_lvb = 0, local = 0;
17506714d8e8SKurt Hackel 	int i;
17516714d8e8SKurt Hackel 	u8 search_node;
17526714d8e8SKurt Hackel 
17536714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
17546714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
17556714d8e8SKurt Hackel 
17566714d8e8SKurt Hackel 	if (res->owner == dlm->node_num)
17576714d8e8SKurt Hackel 		/* if this node owned the lockres, and if the dead node
17586714d8e8SKurt Hackel 		 * had an EX when he died, blank out the lvb */
17596714d8e8SKurt Hackel 		search_node = dead_node;
17606714d8e8SKurt Hackel 	else {
17616714d8e8SKurt Hackel 		/* if this is a secondary lockres, and we had no EX or PR
17626714d8e8SKurt Hackel 		 * locks granted, we can no longer trust the lvb */
17636714d8e8SKurt Hackel 		search_node = dlm->node_num;
17646714d8e8SKurt Hackel 		local = 1;  /* check local state for valid lvb */
17656714d8e8SKurt Hackel 	}
17666714d8e8SKurt Hackel 
17676714d8e8SKurt Hackel 	for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
17686714d8e8SKurt Hackel 		queue = dlm_list_idx_to_ptr(res, i);
17696714d8e8SKurt Hackel 		list_for_each(iter, queue) {
17706714d8e8SKurt Hackel 			lock = list_entry (iter, struct dlm_lock, list);
17716714d8e8SKurt Hackel 			if (lock->ml.node == search_node) {
17726714d8e8SKurt Hackel 				if (dlm_lvb_needs_invalidation(lock, local)) {
17736714d8e8SKurt Hackel 					/* zero the lksb lvb and lockres lvb */
17746714d8e8SKurt Hackel 					blank_lvb = 1;
17756714d8e8SKurt Hackel 					memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
17766714d8e8SKurt Hackel 				}
17776714d8e8SKurt Hackel 			}
17786714d8e8SKurt Hackel 		}
17796714d8e8SKurt Hackel 	}
17806714d8e8SKurt Hackel 
17816714d8e8SKurt Hackel 	if (blank_lvb) {
17826714d8e8SKurt Hackel 		mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
17836714d8e8SKurt Hackel 		     res->lockname.len, res->lockname.name, dead_node);
17846714d8e8SKurt Hackel 		memset(res->lvb, 0, DLM_LVB_LEN);
17856714d8e8SKurt Hackel 	}
17866714d8e8SKurt Hackel }
17876714d8e8SKurt Hackel 
17886714d8e8SKurt Hackel static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
17896714d8e8SKurt Hackel 				struct dlm_lock_resource *res, u8 dead_node)
17906714d8e8SKurt Hackel {
17916714d8e8SKurt Hackel 	struct list_head *iter, *tmpiter;
17926714d8e8SKurt Hackel 	struct dlm_lock *lock;
17936714d8e8SKurt Hackel 
17946714d8e8SKurt Hackel 	/* this node is the lockres master:
17956714d8e8SKurt Hackel 	 * 1) remove any stale locks for the dead node
17966714d8e8SKurt Hackel 	 * 2) if the dead node had an EX when he died, blank out the lvb
17976714d8e8SKurt Hackel 	 */
17986714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
17996714d8e8SKurt Hackel 	assert_spin_locked(&res->spinlock);
18006714d8e8SKurt Hackel 
18016714d8e8SKurt Hackel 	/* TODO: check pending_asts, pending_basts here */
18026714d8e8SKurt Hackel 	list_for_each_safe(iter, tmpiter, &res->granted) {
18036714d8e8SKurt Hackel 		lock = list_entry (iter, struct dlm_lock, list);
18046714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
18056714d8e8SKurt Hackel 			list_del_init(&lock->list);
18066714d8e8SKurt Hackel 			dlm_lock_put(lock);
18076714d8e8SKurt Hackel 		}
18086714d8e8SKurt Hackel 	}
18096714d8e8SKurt Hackel 	list_for_each_safe(iter, tmpiter, &res->converting) {
18106714d8e8SKurt Hackel 		lock = list_entry (iter, struct dlm_lock, list);
18116714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
18126714d8e8SKurt Hackel 			list_del_init(&lock->list);
18136714d8e8SKurt Hackel 			dlm_lock_put(lock);
18146714d8e8SKurt Hackel 		}
18156714d8e8SKurt Hackel 	}
18166714d8e8SKurt Hackel 	list_for_each_safe(iter, tmpiter, &res->blocked) {
18176714d8e8SKurt Hackel 		lock = list_entry (iter, struct dlm_lock, list);
18186714d8e8SKurt Hackel 		if (lock->ml.node == dead_node) {
18196714d8e8SKurt Hackel 			list_del_init(&lock->list);
18206714d8e8SKurt Hackel 			dlm_lock_put(lock);
18216714d8e8SKurt Hackel 		}
18226714d8e8SKurt Hackel 	}
18236714d8e8SKurt Hackel 
18246714d8e8SKurt Hackel 	/* do not kick thread yet */
18256714d8e8SKurt Hackel 	__dlm_dirty_lockres(dlm, res);
18266714d8e8SKurt Hackel }
18276714d8e8SKurt Hackel 
18286714d8e8SKurt Hackel /* if this node is the recovery master, and there are no
18296714d8e8SKurt Hackel  * locks for a given lockres owned by this node that are in
18306714d8e8SKurt Hackel  * either PR or EX mode, zero out the lvb before requesting.
18316714d8e8SKurt Hackel  *
18326714d8e8SKurt Hackel  */
18336714d8e8SKurt Hackel 
18346714d8e8SKurt Hackel 
18356714d8e8SKurt Hackel static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
18366714d8e8SKurt Hackel {
18376714d8e8SKurt Hackel 	struct list_head *iter;
18386714d8e8SKurt Hackel 	struct dlm_lock_resource *res;
18396714d8e8SKurt Hackel 	int i;
18406714d8e8SKurt Hackel 	struct list_head *bucket;
1841e2faea4cSKurt Hackel 	struct dlm_lock *lock;
18426714d8e8SKurt Hackel 
18436714d8e8SKurt Hackel 
18446714d8e8SKurt Hackel 	/* purge any stale mles */
18456714d8e8SKurt Hackel 	dlm_clean_master_list(dlm, dead_node);
18466714d8e8SKurt Hackel 
18476714d8e8SKurt Hackel 	/*
18486714d8e8SKurt Hackel 	 * now clean up all lock resources.  there are two rules:
18496714d8e8SKurt Hackel 	 *
18506714d8e8SKurt Hackel 	 * 1) if the dead node was the master, move the lockres
18516714d8e8SKurt Hackel 	 *    to the recovering list.  set the RECOVERING flag.
18526714d8e8SKurt Hackel 	 *    this lockres needs to be cleaned up before it can
18536714d8e8SKurt Hackel 	 *    be used further.
18546714d8e8SKurt Hackel 	 *
18556714d8e8SKurt Hackel 	 * 2) if this node was the master, remove all locks from
18566714d8e8SKurt Hackel 	 *    each of the lockres queues that were owned by the
18576714d8e8SKurt Hackel 	 *    dead node.  once recovery finishes, the dlm thread
18586714d8e8SKurt Hackel 	 *    can be kicked again to see if any ASTs or BASTs
18596714d8e8SKurt Hackel 	 *    need to be fired as a result.
18606714d8e8SKurt Hackel 	 */
18616714d8e8SKurt Hackel 	for (i=0; i<DLM_HASH_SIZE; i++) {
18626714d8e8SKurt Hackel 		bucket = &(dlm->resources[i]);
18636714d8e8SKurt Hackel 		list_for_each(iter, bucket) {
18646714d8e8SKurt Hackel 			res = list_entry (iter, struct dlm_lock_resource, list);
1865e2faea4cSKurt Hackel  			/* always prune any $RECOVERY entries for dead nodes,
1866e2faea4cSKurt Hackel  			 * otherwise hangs can occur during later recovery */
18676714d8e8SKurt Hackel 			if (dlm_is_recovery_lock(res->lockname.name,
1868e2faea4cSKurt Hackel 						 res->lockname.len)) {
1869e2faea4cSKurt Hackel 				spin_lock(&res->spinlock);
1870e2faea4cSKurt Hackel 				list_for_each_entry(lock, &res->granted, list) {
1871e2faea4cSKurt Hackel 					if (lock->ml.node == dead_node) {
1872e2faea4cSKurt Hackel 						mlog(0, "AHA! there was "
1873e2faea4cSKurt Hackel 						     "a $RECOVERY lock for dead "
1874e2faea4cSKurt Hackel 						     "node %u (%s)!\n",
1875e2faea4cSKurt Hackel 						     dead_node, dlm->name);
1876e2faea4cSKurt Hackel 						list_del_init(&lock->list);
1877e2faea4cSKurt Hackel 						dlm_lock_put(lock);
1878e2faea4cSKurt Hackel 						break;
1879e2faea4cSKurt Hackel 					}
1880e2faea4cSKurt Hackel 				}
1881e2faea4cSKurt Hackel 				spin_unlock(&res->spinlock);
18826714d8e8SKurt Hackel 				continue;
1883e2faea4cSKurt Hackel 			}
18846714d8e8SKurt Hackel 			spin_lock(&res->spinlock);
18856714d8e8SKurt Hackel 			/* zero the lvb if necessary */
18866714d8e8SKurt Hackel 			dlm_revalidate_lvb(dlm, res, dead_node);
18876714d8e8SKurt Hackel 			if (res->owner == dead_node)
18886714d8e8SKurt Hackel 				dlm_move_lockres_to_recovery_list(dlm, res);
18896714d8e8SKurt Hackel 			else if (res->owner == dlm->node_num) {
18906714d8e8SKurt Hackel 				dlm_free_dead_locks(dlm, res, dead_node);
18916714d8e8SKurt Hackel 				__dlm_lockres_calc_usage(dlm, res);
18926714d8e8SKurt Hackel 			}
18936714d8e8SKurt Hackel 			spin_unlock(&res->spinlock);
18946714d8e8SKurt Hackel 		}
18956714d8e8SKurt Hackel 	}
18966714d8e8SKurt Hackel 
18976714d8e8SKurt Hackel }
18986714d8e8SKurt Hackel 
18996714d8e8SKurt Hackel static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
19006714d8e8SKurt Hackel {
19016714d8e8SKurt Hackel 	assert_spin_locked(&dlm->spinlock);
19026714d8e8SKurt Hackel 
19036714d8e8SKurt Hackel 	/* check to see if the node is already considered dead */
19046714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->live_nodes_map)) {
19056714d8e8SKurt Hackel 		mlog(0, "for domain %s, node %d is already dead. "
19066714d8e8SKurt Hackel 		     "another node likely did recovery already.\n",
19076714d8e8SKurt Hackel 		     dlm->name, idx);
19086714d8e8SKurt Hackel 		return;
19096714d8e8SKurt Hackel 	}
19106714d8e8SKurt Hackel 
19116714d8e8SKurt Hackel 	/* check to see if we do not care about this node */
19126714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->domain_map)) {
19136714d8e8SKurt Hackel 		/* This also catches the case that we get a node down
19146714d8e8SKurt Hackel 		 * but haven't joined the domain yet. */
19156714d8e8SKurt Hackel 		mlog(0, "node %u already removed from domain!\n", idx);
19166714d8e8SKurt Hackel 		return;
19176714d8e8SKurt Hackel 	}
19186714d8e8SKurt Hackel 
19196714d8e8SKurt Hackel 	clear_bit(idx, dlm->live_nodes_map);
19206714d8e8SKurt Hackel 
19216714d8e8SKurt Hackel 	/* Clean up join state on node death. */
19226714d8e8SKurt Hackel 	if (dlm->joining_node == idx) {
19236714d8e8SKurt Hackel 		mlog(0, "Clearing join state for node %u\n", idx);
19246714d8e8SKurt Hackel 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
19256714d8e8SKurt Hackel 	}
19266714d8e8SKurt Hackel 
19276714d8e8SKurt Hackel 	/* make sure local cleanup occurs before the heartbeat events */
19286714d8e8SKurt Hackel 	if (!test_bit(idx, dlm->recovery_map))
19296714d8e8SKurt Hackel 		dlm_do_local_recovery_cleanup(dlm, idx);
19306714d8e8SKurt Hackel 
19316714d8e8SKurt Hackel 	/* notify anything attached to the heartbeat events */
19326714d8e8SKurt Hackel 	dlm_hb_event_notify_attached(dlm, idx, 0);
19336714d8e8SKurt Hackel 
19346714d8e8SKurt Hackel 	mlog(0, "node %u being removed from domain map!\n", idx);
19356714d8e8SKurt Hackel 	clear_bit(idx, dlm->domain_map);
19366714d8e8SKurt Hackel 	/* wake up migration waiters if a node goes down.
19376714d8e8SKurt Hackel 	 * perhaps later we can genericize this for other waiters. */
19386714d8e8SKurt Hackel 	wake_up(&dlm->migration_wq);
19396714d8e8SKurt Hackel 
19406714d8e8SKurt Hackel 	if (test_bit(idx, dlm->recovery_map))
19416714d8e8SKurt Hackel 		mlog(0, "domain %s, node %u already added "
19426714d8e8SKurt Hackel 		     "to recovery map!\n", dlm->name, idx);
19436714d8e8SKurt Hackel 	else
19446714d8e8SKurt Hackel 		set_bit(idx, dlm->recovery_map);
19456714d8e8SKurt Hackel }
19466714d8e8SKurt Hackel 
19476714d8e8SKurt Hackel void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
19486714d8e8SKurt Hackel {
19496714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
19506714d8e8SKurt Hackel 
19516714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
19526714d8e8SKurt Hackel 		return;
19536714d8e8SKurt Hackel 
19546714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
19556714d8e8SKurt Hackel 	__dlm_hb_node_down(dlm, idx);
19566714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
19576714d8e8SKurt Hackel 
19586714d8e8SKurt Hackel 	dlm_put(dlm);
19596714d8e8SKurt Hackel }
19606714d8e8SKurt Hackel 
19616714d8e8SKurt Hackel void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
19626714d8e8SKurt Hackel {
19636714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
19646714d8e8SKurt Hackel 
19656714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
19666714d8e8SKurt Hackel 		return;
19676714d8e8SKurt Hackel 
19686714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
19696714d8e8SKurt Hackel 	set_bit(idx, dlm->live_nodes_map);
1970e2faea4cSKurt Hackel 	/* do NOT notify mle attached to the heartbeat events.
1971e2faea4cSKurt Hackel 	 * new nodes are not interesting in mastery until joined. */
19726714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
19736714d8e8SKurt Hackel 
19746714d8e8SKurt Hackel 	dlm_put(dlm);
19756714d8e8SKurt Hackel }
19766714d8e8SKurt Hackel 
19776714d8e8SKurt Hackel static void dlm_reco_ast(void *astdata)
19786714d8e8SKurt Hackel {
19796714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
19806714d8e8SKurt Hackel 	mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
19816714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
19826714d8e8SKurt Hackel }
19836714d8e8SKurt Hackel static void dlm_reco_bast(void *astdata, int blocked_type)
19846714d8e8SKurt Hackel {
19856714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = astdata;
19866714d8e8SKurt Hackel 	mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
19876714d8e8SKurt Hackel 	     dlm->node_num, dlm->name);
19886714d8e8SKurt Hackel }
19896714d8e8SKurt Hackel static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
19906714d8e8SKurt Hackel {
19916714d8e8SKurt Hackel 	mlog(0, "unlockast for recovery lock fired!\n");
19926714d8e8SKurt Hackel }
19936714d8e8SKurt Hackel 
1994e2faea4cSKurt Hackel /*
1995e2faea4cSKurt Hackel  * dlm_pick_recovery_master will continually attempt to use
1996e2faea4cSKurt Hackel  * dlmlock() on the special "$RECOVERY" lockres with the
1997e2faea4cSKurt Hackel  * LKM_NOQUEUE flag to get an EX.  every thread that enters
1998e2faea4cSKurt Hackel  * this function on each node racing to become the recovery
1999e2faea4cSKurt Hackel  * master will not stop attempting this until either:
2000e2faea4cSKurt Hackel  * a) this node gets the EX (and becomes the recovery master),
2001e2faea4cSKurt Hackel  * or b) dlm->reco.new_master gets set to some nodenum
2002e2faea4cSKurt Hackel  * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2003e2faea4cSKurt Hackel  * so each time a recovery master is needed, the entire cluster
2004e2faea4cSKurt Hackel  * will sync at this point.  if the new master dies, that will
2005e2faea4cSKurt Hackel  * be detected in dlm_do_recovery */
20066714d8e8SKurt Hackel static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
20076714d8e8SKurt Hackel {
20086714d8e8SKurt Hackel 	enum dlm_status ret;
20096714d8e8SKurt Hackel 	struct dlm_lockstatus lksb;
20106714d8e8SKurt Hackel 	int status = -EINVAL;
20116714d8e8SKurt Hackel 
20126714d8e8SKurt Hackel 	mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
20136714d8e8SKurt Hackel 	     dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2014e2faea4cSKurt Hackel again:
20156714d8e8SKurt Hackel 	memset(&lksb, 0, sizeof(lksb));
20166714d8e8SKurt Hackel 
20176714d8e8SKurt Hackel 	ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
20186714d8e8SKurt Hackel 		      DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
20196714d8e8SKurt Hackel 
2020e2faea4cSKurt Hackel 	mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2021e2faea4cSKurt Hackel 	     dlm->name, ret, lksb.status);
2022e2faea4cSKurt Hackel 
20236714d8e8SKurt Hackel 	if (ret == DLM_NORMAL) {
20246714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
20256714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
2026e2faea4cSKurt Hackel 
2027e2faea4cSKurt Hackel 		/* got the EX lock.  check to see if another node
2028e2faea4cSKurt Hackel 		 * just became the reco master */
2029e2faea4cSKurt Hackel 		if (dlm_reco_master_ready(dlm)) {
2030e2faea4cSKurt Hackel 			mlog(0, "%s: got reco EX lock, but %u will "
2031e2faea4cSKurt Hackel 			     "do the recovery\n", dlm->name,
2032e2faea4cSKurt Hackel 			     dlm->reco.new_master);
2033e2faea4cSKurt Hackel 			status = -EEXIST;
2034e2faea4cSKurt Hackel 		} else {
20356714d8e8SKurt Hackel 			status = dlm_send_begin_reco_message(dlm,
20366714d8e8SKurt Hackel 				      dlm->reco.dead_node);
2037e2faea4cSKurt Hackel 			/* this always succeeds */
2038e2faea4cSKurt Hackel 			BUG_ON(status);
2039e2faea4cSKurt Hackel 
2040e2faea4cSKurt Hackel 			/* set the new_master to this node */
2041e2faea4cSKurt Hackel 			spin_lock(&dlm->spinlock);
2042e2faea4cSKurt Hackel 			dlm->reco.new_master = dlm->node_num;
2043e2faea4cSKurt Hackel 			spin_unlock(&dlm->spinlock);
2044e2faea4cSKurt Hackel 		}
20456714d8e8SKurt Hackel 
20466714d8e8SKurt Hackel 		/* recovery lock is a special case.  ast will not get fired,
20476714d8e8SKurt Hackel 		 * so just go ahead and unlock it. */
20486714d8e8SKurt Hackel 		ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2049e2faea4cSKurt Hackel 		if (ret == DLM_DENIED) {
2050e2faea4cSKurt Hackel 			mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2051e2faea4cSKurt Hackel 			ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2052e2faea4cSKurt Hackel 		}
20536714d8e8SKurt Hackel 		if (ret != DLM_NORMAL) {
20546714d8e8SKurt Hackel 			/* this would really suck. this could only happen
20556714d8e8SKurt Hackel 			 * if there was a network error during the unlock
20566714d8e8SKurt Hackel 			 * because of node death.  this means the unlock
20576714d8e8SKurt Hackel 			 * is actually "done" and the lock structure is
20586714d8e8SKurt Hackel 			 * even freed.  we can continue, but only
20596714d8e8SKurt Hackel 			 * because this specific lock name is special. */
2060e2faea4cSKurt Hackel 			mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
20616714d8e8SKurt Hackel 		}
20626714d8e8SKurt Hackel 	} else if (ret == DLM_NOTQUEUED) {
20636714d8e8SKurt Hackel 		mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
20646714d8e8SKurt Hackel 		     dlm->name, dlm->node_num);
20656714d8e8SKurt Hackel 		/* another node is master. wait on
2066e2faea4cSKurt Hackel 		 * reco.new_master != O2NM_INVALID_NODE_NUM
2067e2faea4cSKurt Hackel 		 * for at most one second */
2068e2faea4cSKurt Hackel 		wait_event_timeout(dlm->dlm_reco_thread_wq,
2069e2faea4cSKurt Hackel 					 dlm_reco_master_ready(dlm),
2070e2faea4cSKurt Hackel 					 msecs_to_jiffies(1000));
2071e2faea4cSKurt Hackel 		if (!dlm_reco_master_ready(dlm)) {
2072e2faea4cSKurt Hackel 			mlog(0, "%s: reco master taking awhile\n",
2073e2faea4cSKurt Hackel 			     dlm->name);
2074e2faea4cSKurt Hackel 			goto again;
2075e2faea4cSKurt Hackel 		}
2076e2faea4cSKurt Hackel 		/* another node has informed this one that it is reco master */
2077e2faea4cSKurt Hackel 		mlog(0, "%s: reco master %u is ready to recover %u\n",
2078e2faea4cSKurt Hackel 		     dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
20796714d8e8SKurt Hackel 		status = -EEXIST;
2080e2faea4cSKurt Hackel 	} else {
2081e2faea4cSKurt Hackel 		struct dlm_lock_resource *res;
2082e2faea4cSKurt Hackel 
2083e2faea4cSKurt Hackel 		/* dlmlock returned something other than NOTQUEUED or NORMAL */
2084e2faea4cSKurt Hackel 		mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2085e2faea4cSKurt Hackel 		     "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2086e2faea4cSKurt Hackel 		     dlm_errname(lksb.status));
2087e2faea4cSKurt Hackel 		res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2088e2faea4cSKurt Hackel 					 DLM_RECOVERY_LOCK_NAME_LEN);
2089e2faea4cSKurt Hackel 		if (res) {
2090e2faea4cSKurt Hackel 			dlm_print_one_lock_resource(res);
2091e2faea4cSKurt Hackel 			dlm_lockres_put(res);
2092e2faea4cSKurt Hackel 		} else {
2093e2faea4cSKurt Hackel 			mlog(ML_ERROR, "recovery lock not found\n");
2094e2faea4cSKurt Hackel 		}
2095e2faea4cSKurt Hackel 		BUG();
20966714d8e8SKurt Hackel 	}
20976714d8e8SKurt Hackel 
20986714d8e8SKurt Hackel 	return status;
20996714d8e8SKurt Hackel }
21006714d8e8SKurt Hackel 
21016714d8e8SKurt Hackel static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
21026714d8e8SKurt Hackel {
21036714d8e8SKurt Hackel 	struct dlm_begin_reco br;
21046714d8e8SKurt Hackel 	int ret = 0;
21056714d8e8SKurt Hackel 	struct dlm_node_iter iter;
21066714d8e8SKurt Hackel 	int nodenum;
21076714d8e8SKurt Hackel 	int status;
21086714d8e8SKurt Hackel 
21096714d8e8SKurt Hackel 	mlog_entry("%u\n", dead_node);
21106714d8e8SKurt Hackel 
21116714d8e8SKurt Hackel 	mlog(0, "dead node is %u\n", dead_node);
21126714d8e8SKurt Hackel 
21136714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
21146714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
21156714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
21166714d8e8SKurt Hackel 
21176714d8e8SKurt Hackel 	clear_bit(dead_node, iter.node_map);
21186714d8e8SKurt Hackel 
21196714d8e8SKurt Hackel 	memset(&br, 0, sizeof(br));
21206714d8e8SKurt Hackel 	br.node_idx = dlm->node_num;
21216714d8e8SKurt Hackel 	br.dead_node = dead_node;
21226714d8e8SKurt Hackel 
21236714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
21246714d8e8SKurt Hackel 		ret = 0;
21256714d8e8SKurt Hackel 		if (nodenum == dead_node) {
21266714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to dead node "
21276714d8e8SKurt Hackel 				  "%u\n", dead_node);
21286714d8e8SKurt Hackel 			continue;
21296714d8e8SKurt Hackel 		}
21306714d8e8SKurt Hackel 		if (nodenum == dlm->node_num) {
21316714d8e8SKurt Hackel 			mlog(0, "not sending begin reco to self\n");
21326714d8e8SKurt Hackel 			continue;
21336714d8e8SKurt Hackel 		}
2134e2faea4cSKurt Hackel retry:
21356714d8e8SKurt Hackel 		ret = -EINVAL;
21366714d8e8SKurt Hackel 		mlog(0, "attempting to send begin reco msg to %d\n",
21376714d8e8SKurt Hackel 			  nodenum);
21386714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
21396714d8e8SKurt Hackel 					 &br, sizeof(br), nodenum, &status);
21406714d8e8SKurt Hackel 		/* negative status is handled ok by caller here */
21416714d8e8SKurt Hackel 		if (ret >= 0)
21426714d8e8SKurt Hackel 			ret = status;
2143e2faea4cSKurt Hackel 		if (dlm_is_host_down(ret)) {
2144e2faea4cSKurt Hackel 			/* node is down.  not involved in recovery
2145e2faea4cSKurt Hackel 			 * so just keep going */
2146e2faea4cSKurt Hackel 			mlog(0, "%s: node %u was down when sending "
2147e2faea4cSKurt Hackel 			     "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2148e2faea4cSKurt Hackel 			ret = 0;
2149e2faea4cSKurt Hackel 		}
21506714d8e8SKurt Hackel 		if (ret < 0) {
21516714d8e8SKurt Hackel 			struct dlm_lock_resource *res;
2152e2faea4cSKurt Hackel 			/* this is now a serious problem, possibly ENOMEM
2153e2faea4cSKurt Hackel 			 * in the network stack.  must retry */
21546714d8e8SKurt Hackel 			mlog_errno(ret);
21556714d8e8SKurt Hackel 			mlog(ML_ERROR, "begin reco of dlm %s to node %u "
21566714d8e8SKurt Hackel 			    " returned %d\n", dlm->name, nodenum, ret);
21576714d8e8SKurt Hackel 			res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
21586714d8e8SKurt Hackel 						 DLM_RECOVERY_LOCK_NAME_LEN);
21596714d8e8SKurt Hackel 			if (res) {
21606714d8e8SKurt Hackel 				dlm_print_one_lock_resource(res);
21616714d8e8SKurt Hackel 				dlm_lockres_put(res);
21626714d8e8SKurt Hackel 			} else {
21636714d8e8SKurt Hackel 				mlog(ML_ERROR, "recovery lock not found\n");
21646714d8e8SKurt Hackel 			}
2165e2faea4cSKurt Hackel 			/* sleep for a bit in hopes that we can avoid
2166e2faea4cSKurt Hackel 			 * another ENOMEM */
2167e2faea4cSKurt Hackel 			msleep(100);
2168e2faea4cSKurt Hackel 			goto retry;
21696714d8e8SKurt Hackel 		}
21706714d8e8SKurt Hackel 	}
21716714d8e8SKurt Hackel 
21726714d8e8SKurt Hackel 	return ret;
21736714d8e8SKurt Hackel }
21746714d8e8SKurt Hackel 
21756714d8e8SKurt Hackel int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
21766714d8e8SKurt Hackel {
21776714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
21786714d8e8SKurt Hackel 	struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
21796714d8e8SKurt Hackel 
21806714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
21816714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
21826714d8e8SKurt Hackel 		return 0;
21836714d8e8SKurt Hackel 
21846714d8e8SKurt Hackel 	mlog(0, "node %u wants to recover node %u\n",
21856714d8e8SKurt Hackel 		  br->node_idx, br->dead_node);
21866714d8e8SKurt Hackel 
21876714d8e8SKurt Hackel 	dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
21886714d8e8SKurt Hackel 
21896714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
21906714d8e8SKurt Hackel 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2191e2faea4cSKurt Hackel 		if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2192e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u died, changing "
2193e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2194e2faea4cSKurt Hackel 			     br->node_idx);
2195e2faea4cSKurt Hackel 		} else {
2196e2faea4cSKurt Hackel 			mlog(0, "%s: new_master %u NOT DEAD, changing "
2197e2faea4cSKurt Hackel 			     "to %u\n", dlm->name, dlm->reco.new_master,
2198e2faea4cSKurt Hackel 			     br->node_idx);
2199e2faea4cSKurt Hackel 			/* may not have seen the new master as dead yet */
2200e2faea4cSKurt Hackel 		}
22016714d8e8SKurt Hackel 	}
22026714d8e8SKurt Hackel 	if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2203e2faea4cSKurt Hackel 		mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2204e2faea4cSKurt Hackel 		     "node %u changing it to %u\n", dlm->name,
2205e2faea4cSKurt Hackel 		     dlm->reco.dead_node, br->node_idx, br->dead_node);
22066714d8e8SKurt Hackel 	}
22076714d8e8SKurt Hackel 	dlm->reco.new_master = br->node_idx;
22086714d8e8SKurt Hackel 	dlm->reco.dead_node = br->dead_node;
22096714d8e8SKurt Hackel 	if (!test_bit(br->dead_node, dlm->recovery_map)) {
2210e2faea4cSKurt Hackel 		mlog(0, "recovery master %u sees %u as dead, but this "
22116714d8e8SKurt Hackel 		     "node has not yet.  marking %u as dead\n",
22126714d8e8SKurt Hackel 		     br->node_idx, br->dead_node, br->dead_node);
2213e2faea4cSKurt Hackel 		if (!test_bit(br->dead_node, dlm->domain_map) ||
2214e2faea4cSKurt Hackel 		    !test_bit(br->dead_node, dlm->live_nodes_map))
2215e2faea4cSKurt Hackel 			mlog(0, "%u not in domain/live_nodes map "
2216e2faea4cSKurt Hackel 			     "so setting it in reco map manually\n",
2217e2faea4cSKurt Hackel 			     br->dead_node);
2218e2faea4cSKurt Hackel 		set_bit(br->dead_node, dlm->recovery_map);
22196714d8e8SKurt Hackel 		__dlm_hb_node_down(dlm, br->dead_node);
22206714d8e8SKurt Hackel 	}
22216714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
22226714d8e8SKurt Hackel 
22236714d8e8SKurt Hackel 	dlm_kick_recovery_thread(dlm);
22246714d8e8SKurt Hackel 	dlm_put(dlm);
22256714d8e8SKurt Hackel 	return 0;
22266714d8e8SKurt Hackel }
22276714d8e8SKurt Hackel 
22286714d8e8SKurt Hackel static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
22296714d8e8SKurt Hackel {
22306714d8e8SKurt Hackel 	int ret = 0;
22316714d8e8SKurt Hackel 	struct dlm_finalize_reco fr;
22326714d8e8SKurt Hackel 	struct dlm_node_iter iter;
22336714d8e8SKurt Hackel 	int nodenum;
22346714d8e8SKurt Hackel 	int status;
22356714d8e8SKurt Hackel 
22366714d8e8SKurt Hackel 	mlog(0, "finishing recovery for node %s:%u\n",
22376714d8e8SKurt Hackel 	     dlm->name, dlm->reco.dead_node);
22386714d8e8SKurt Hackel 
22396714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
22406714d8e8SKurt Hackel 	dlm_node_iter_init(dlm->domain_map, &iter);
22416714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
22426714d8e8SKurt Hackel 
22436714d8e8SKurt Hackel 	memset(&fr, 0, sizeof(fr));
22446714d8e8SKurt Hackel 	fr.node_idx = dlm->node_num;
22456714d8e8SKurt Hackel 	fr.dead_node = dlm->reco.dead_node;
22466714d8e8SKurt Hackel 
22476714d8e8SKurt Hackel 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
22486714d8e8SKurt Hackel 		if (nodenum == dlm->node_num)
22496714d8e8SKurt Hackel 			continue;
22506714d8e8SKurt Hackel 		ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
22516714d8e8SKurt Hackel 					 &fr, sizeof(fr), nodenum, &status);
22526714d8e8SKurt Hackel 		if (ret >= 0) {
22536714d8e8SKurt Hackel 			ret = status;
22546714d8e8SKurt Hackel 			if (dlm_is_host_down(ret)) {
22556714d8e8SKurt Hackel 				/* this has no effect on this recovery
22566714d8e8SKurt Hackel 				 * session, so set the status to zero to
22576714d8e8SKurt Hackel 				 * finish out the last recovery */
22586714d8e8SKurt Hackel 				mlog(ML_ERROR, "node %u went down after this "
22596714d8e8SKurt Hackel 				     "node finished recovery.\n", nodenum);
22606714d8e8SKurt Hackel 				ret = 0;
22616714d8e8SKurt Hackel 			}
22626714d8e8SKurt Hackel 		}
22636714d8e8SKurt Hackel 		if (ret < 0) {
22646714d8e8SKurt Hackel 			mlog_errno(ret);
22656714d8e8SKurt Hackel 			break;
22666714d8e8SKurt Hackel 		}
22676714d8e8SKurt Hackel 	}
22686714d8e8SKurt Hackel 
22696714d8e8SKurt Hackel 	return ret;
22706714d8e8SKurt Hackel }
22716714d8e8SKurt Hackel 
22726714d8e8SKurt Hackel int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
22736714d8e8SKurt Hackel {
22746714d8e8SKurt Hackel 	struct dlm_ctxt *dlm = data;
22756714d8e8SKurt Hackel 	struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
22766714d8e8SKurt Hackel 
22776714d8e8SKurt Hackel 	/* ok to return 0, domain has gone away */
22786714d8e8SKurt Hackel 	if (!dlm_grab(dlm))
22796714d8e8SKurt Hackel 		return 0;
22806714d8e8SKurt Hackel 
22816714d8e8SKurt Hackel 	mlog(0, "node %u finalizing recovery of node %u\n",
22826714d8e8SKurt Hackel 	     fr->node_idx, fr->dead_node);
22836714d8e8SKurt Hackel 
22846714d8e8SKurt Hackel 	spin_lock(&dlm->spinlock);
22856714d8e8SKurt Hackel 
22866714d8e8SKurt Hackel 	if (dlm->reco.new_master != fr->node_idx) {
22876714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
22886714d8e8SKurt Hackel 		     "%u is supposed to be the new master, dead=%u\n",
22896714d8e8SKurt Hackel 		     fr->node_idx, dlm->reco.new_master, fr->dead_node);
22906714d8e8SKurt Hackel 		BUG();
22916714d8e8SKurt Hackel 	}
22926714d8e8SKurt Hackel 	if (dlm->reco.dead_node != fr->dead_node) {
22936714d8e8SKurt Hackel 		mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
22946714d8e8SKurt Hackel 		     "node %u, but node %u is supposed to be dead\n",
22956714d8e8SKurt Hackel 		     fr->node_idx, fr->dead_node, dlm->reco.dead_node);
22966714d8e8SKurt Hackel 		BUG();
22976714d8e8SKurt Hackel 	}
22986714d8e8SKurt Hackel 
22996714d8e8SKurt Hackel 	dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
23006714d8e8SKurt Hackel 
23016714d8e8SKurt Hackel 	spin_unlock(&dlm->spinlock);
23026714d8e8SKurt Hackel 
23036714d8e8SKurt Hackel 	dlm_reset_recovery(dlm);
23046714d8e8SKurt Hackel 
23056714d8e8SKurt Hackel 	dlm_kick_recovery_thread(dlm);
23066714d8e8SKurt Hackel 	dlm_put(dlm);
23076714d8e8SKurt Hackel 	return 0;
23086714d8e8SKurt Hackel }
2309