xref: /linux/fs/ocfs2/dlm/dlmrecovery.c (revision 1dbb7422053fa0f4fb5f9acc3931617fc0091939)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dlmrecovery.c
4  *
5  * recovery stuff
6  *
7  * Copyright (C) 2004 Oracle.  All rights reserved.
8  */
9 
10 
11 #include <linux/module.h>
12 #include <linux/fs.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/highmem.h>
16 #include <linux/init.h>
17 #include <linux/sysctl.h>
18 #include <linux/random.h>
19 #include <linux/blkdev.h>
20 #include <linux/socket.h>
21 #include <linux/inet.h>
22 #include <linux/timer.h>
23 #include <linux/kthread.h>
24 #include <linux/delay.h>
25 #include <linux/string_choices.h>
26 
27 #include "../cluster/heartbeat.h"
28 #include "../cluster/nodemanager.h"
29 #include "../cluster/tcp.h"
30 
31 #include "dlmapi.h"
32 #include "dlmcommon.h"
33 #include "dlmdomain.h"
34 
35 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
36 #include "../cluster/masklog.h"
37 
38 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
39 
40 static int dlm_recovery_thread(void *data);
41 static int dlm_do_recovery(struct dlm_ctxt *dlm);
42 
43 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
44 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
45 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
46 static int dlm_request_all_locks(struct dlm_ctxt *dlm,
47 				 u8 request_from, u8 dead_node);
48 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm);
49 
50 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
51 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
52 					const char *lockname, int namelen,
53 					int total_locks, u64 cookie,
54 					u8 flags, u8 master);
55 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
56 				    struct dlm_migratable_lockres *mres,
57 				    u8 send_to,
58 				    struct dlm_lock_resource *res,
59 				    int total_locks);
60 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
61 				     struct dlm_lock_resource *res,
62 				     struct dlm_migratable_lockres *mres);
63 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
64 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
65 				 u8 dead_node, u8 send_to);
66 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
67 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
68 					struct list_head *list, u8 dead_node);
69 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
70 					      u8 dead_node, u8 new_master);
71 static void dlm_reco_ast(void *astdata);
72 static void dlm_reco_bast(void *astdata, int blocked_type);
73 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
74 static void dlm_request_all_locks_worker(struct dlm_work_item *item,
75 					 void *data);
76 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
77 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
78 				      struct dlm_lock_resource *res,
79 				      u8 *real_master);
80 
81 static u64 dlm_get_next_mig_cookie(void);
82 
83 DEFINE_SPINLOCK(dlm_reco_state_lock);
84 static DEFINE_SPINLOCK(dlm_mig_cookie_lock);
85 static u64 dlm_mig_cookie = 1;
86 
dlm_get_next_mig_cookie(void)87 static u64 dlm_get_next_mig_cookie(void)
88 {
89 	u64 c;
90 	spin_lock(&dlm_mig_cookie_lock);
91 	c = dlm_mig_cookie;
92 	if (dlm_mig_cookie == (~0ULL))
93 		dlm_mig_cookie = 1;
94 	else
95 		dlm_mig_cookie++;
96 	spin_unlock(&dlm_mig_cookie_lock);
97 	return c;
98 }
99 
dlm_set_reco_dead_node(struct dlm_ctxt * dlm,u8 dead_node)100 static inline void dlm_set_reco_dead_node(struct dlm_ctxt *dlm,
101 					  u8 dead_node)
102 {
103 	assert_spin_locked(&dlm->spinlock);
104 	if (dlm->reco.dead_node != dead_node)
105 		mlog(0, "%s: changing dead_node from %u to %u\n",
106 		     dlm->name, dlm->reco.dead_node, dead_node);
107 	dlm->reco.dead_node = dead_node;
108 }
109 
dlm_set_reco_master(struct dlm_ctxt * dlm,u8 master)110 static inline void dlm_set_reco_master(struct dlm_ctxt *dlm,
111 				       u8 master)
112 {
113 	assert_spin_locked(&dlm->spinlock);
114 	mlog(0, "%s: changing new_master from %u to %u\n",
115 	     dlm->name, dlm->reco.new_master, master);
116 	dlm->reco.new_master = master;
117 }
118 
__dlm_reset_recovery(struct dlm_ctxt * dlm)119 static inline void __dlm_reset_recovery(struct dlm_ctxt *dlm)
120 {
121 	assert_spin_locked(&dlm->spinlock);
122 	clear_bit(dlm->reco.dead_node, dlm->recovery_map);
123 	dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
124 	dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
125 }
126 
127 /* Worker function used during recovery. */
dlm_dispatch_work(struct work_struct * work)128 void dlm_dispatch_work(struct work_struct *work)
129 {
130 	struct dlm_ctxt *dlm =
131 		container_of(work, struct dlm_ctxt, dispatched_work);
132 	LIST_HEAD(tmp_list);
133 	struct dlm_work_item *item, *next;
134 	dlm_workfunc_t *workfunc;
135 	int tot=0;
136 
137 	spin_lock(&dlm->work_lock);
138 	list_splice_init(&dlm->work_list, &tmp_list);
139 	spin_unlock(&dlm->work_lock);
140 
141 	list_for_each_entry(item, &tmp_list, list) {
142 		tot++;
143 	}
144 	mlog(0, "%s: work thread has %d work items\n", dlm->name, tot);
145 
146 	list_for_each_entry_safe(item, next, &tmp_list, list) {
147 		workfunc = item->func;
148 		list_del_init(&item->list);
149 
150 		/* already have ref on dlm to avoid having
151 		 * it disappear.  just double-check. */
152 		BUG_ON(item->dlm != dlm);
153 
154 		/* this is allowed to sleep and
155 		 * call network stuff */
156 		workfunc(item, item->data);
157 
158 		dlm_put(dlm);
159 		kfree(item);
160 	}
161 }
162 
163 /*
164  * RECOVERY THREAD
165  */
166 
dlm_kick_recovery_thread(struct dlm_ctxt * dlm)167 void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
168 {
169 	/* wake the recovery thread
170 	 * this will wake the reco thread in one of three places
171 	 * 1) sleeping with no recovery happening
172 	 * 2) sleeping with recovery mastered elsewhere
173 	 * 3) recovery mastered here, waiting on reco data */
174 
175 	wake_up(&dlm->dlm_reco_thread_wq);
176 }
177 
178 /* Launch the recovery thread */
dlm_launch_recovery_thread(struct dlm_ctxt * dlm)179 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
180 {
181 	mlog(0, "starting dlm recovery thread...\n");
182 
183 	dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
184 			"dlm_reco-%s", dlm->name);
185 	if (IS_ERR(dlm->dlm_reco_thread_task)) {
186 		mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
187 		dlm->dlm_reco_thread_task = NULL;
188 		return -EINVAL;
189 	}
190 
191 	return 0;
192 }
193 
dlm_complete_recovery_thread(struct dlm_ctxt * dlm)194 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
195 {
196 	if (dlm->dlm_reco_thread_task) {
197 		mlog(0, "waiting for dlm recovery thread to exit\n");
198 		kthread_stop(dlm->dlm_reco_thread_task);
199 		dlm->dlm_reco_thread_task = NULL;
200 	}
201 }
202 
203 
204 
205 /*
206  * this is lame, but here's how recovery works...
207  * 1) all recovery threads cluster wide will work on recovering
208  *    ONE node at a time
209  * 2) negotiate who will take over all the locks for the dead node.
210  *    that's right... ALL the locks.
211  * 3) once a new master is chosen, everyone scans all locks
212  *    and moves aside those mastered by the dead guy
213  * 4) each of these locks should be locked until recovery is done
214  * 5) the new master collects up all of secondary lock queue info
215  *    one lock at a time, forcing each node to communicate back
216  *    before continuing
217  * 6) each secondary lock queue responds with the full known lock info
218  * 7) once the new master has run all its locks, it sends a ALLDONE!
219  *    message to everyone
220  * 8) upon receiving this message, the secondary queue node unlocks
221  *    and responds to the ALLDONE
222  * 9) once the new master gets responses from everyone, he unlocks
223  *    everything and recovery for this dead node is done
224  *10) go back to 2) while there are still dead nodes
225  *
226  */
227 
dlm_print_reco_node_status(struct dlm_ctxt * dlm)228 static void dlm_print_reco_node_status(struct dlm_ctxt *dlm)
229 {
230 	struct dlm_reco_node_data *ndata;
231 	struct dlm_lock_resource *res;
232 
233 	mlog(ML_NOTICE, "%s(%d): recovery info, state=%s, dead=%u, master=%u\n",
234 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
235 	     dlm->reco.state & DLM_RECO_STATE_ACTIVE ? "ACTIVE" : "inactive",
236 	     dlm->reco.dead_node, dlm->reco.new_master);
237 
238 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
239 		char *st = "unknown";
240 		switch (ndata->state) {
241 			case DLM_RECO_NODE_DATA_INIT:
242 				st = "init";
243 				break;
244 			case DLM_RECO_NODE_DATA_REQUESTING:
245 				st = "requesting";
246 				break;
247 			case DLM_RECO_NODE_DATA_DEAD:
248 				st = "dead";
249 				break;
250 			case DLM_RECO_NODE_DATA_RECEIVING:
251 				st = "receiving";
252 				break;
253 			case DLM_RECO_NODE_DATA_REQUESTED:
254 				st = "requested";
255 				break;
256 			case DLM_RECO_NODE_DATA_DONE:
257 				st = "done";
258 				break;
259 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
260 				st = "finalize-sent";
261 				break;
262 			default:
263 				st = "bad";
264 				break;
265 		}
266 		mlog(ML_NOTICE, "%s: reco state, node %u, state=%s\n",
267 		     dlm->name, ndata->node_num, st);
268 	}
269 	list_for_each_entry(res, &dlm->reco.resources, recovering) {
270 		mlog(ML_NOTICE, "%s: lockres %.*s on recovering list\n",
271 		     dlm->name, res->lockname.len, res->lockname.name);
272 	}
273 }
274 
275 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
276 
dlm_recovery_thread(void * data)277 static int dlm_recovery_thread(void *data)
278 {
279 	int status;
280 	struct dlm_ctxt *dlm = data;
281 	unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
282 
283 	mlog(0, "dlm thread running for %s...\n", dlm->name);
284 
285 	while (!kthread_should_stop()) {
286 		if (dlm_domain_fully_joined(dlm)) {
287 			status = dlm_do_recovery(dlm);
288 			if (status == -EAGAIN) {
289 				/* do not sleep, recheck immediately. */
290 				continue;
291 			}
292 			if (status < 0)
293 				mlog_errno(status);
294 		}
295 
296 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
297 						 kthread_should_stop(),
298 						 timeout);
299 	}
300 
301 	mlog(0, "quitting DLM recovery thread\n");
302 	return 0;
303 }
304 
305 /* returns true when the recovery master has contacted us */
dlm_reco_master_ready(struct dlm_ctxt * dlm)306 static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
307 {
308 	int ready;
309 	spin_lock(&dlm->spinlock);
310 	ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
311 	spin_unlock(&dlm->spinlock);
312 	return ready;
313 }
314 
315 /* returns true if node is no longer in the domain
316  * could be dead or just not joined */
dlm_is_node_dead(struct dlm_ctxt * dlm,u8 node)317 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
318 {
319 	int dead;
320 	spin_lock(&dlm->spinlock);
321 	dead = !test_bit(node, dlm->domain_map);
322 	spin_unlock(&dlm->spinlock);
323 	return dead;
324 }
325 
326 /* returns true if node is no longer in the domain
327  * could be dead or just not joined */
dlm_is_node_recovered(struct dlm_ctxt * dlm,u8 node)328 static int dlm_is_node_recovered(struct dlm_ctxt *dlm, u8 node)
329 {
330 	int recovered;
331 	spin_lock(&dlm->spinlock);
332 	recovered = !test_bit(node, dlm->recovery_map);
333 	spin_unlock(&dlm->spinlock);
334 	return recovered;
335 }
336 
337 
dlm_wait_for_node_death(struct dlm_ctxt * dlm,u8 node,int timeout)338 void dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
339 {
340 	if (dlm_is_node_dead(dlm, node))
341 		return;
342 
343 	printk(KERN_NOTICE "o2dlm: Waiting on the death of node %u in "
344 	       "domain %s\n", node, dlm->name);
345 
346 	if (timeout)
347 		wait_event_timeout(dlm->dlm_reco_thread_wq,
348 				   dlm_is_node_dead(dlm, node),
349 				   msecs_to_jiffies(timeout));
350 	else
351 		wait_event(dlm->dlm_reco_thread_wq,
352 			   dlm_is_node_dead(dlm, node));
353 }
354 
dlm_wait_for_node_recovery(struct dlm_ctxt * dlm,u8 node,int timeout)355 void dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout)
356 {
357 	if (dlm_is_node_recovered(dlm, node))
358 		return;
359 
360 	printk(KERN_NOTICE "o2dlm: Waiting on the recovery of node %u in "
361 	       "domain %s\n", node, dlm->name);
362 
363 	if (timeout)
364 		wait_event_timeout(dlm->dlm_reco_thread_wq,
365 				   dlm_is_node_recovered(dlm, node),
366 				   msecs_to_jiffies(timeout));
367 	else
368 		wait_event(dlm->dlm_reco_thread_wq,
369 			   dlm_is_node_recovered(dlm, node));
370 }
371 
372 /* callers of the top-level api calls (dlmlock/dlmunlock) should
373  * block on the dlm->reco.event when recovery is in progress.
374  * the dlm recovery thread will set this state when it begins
375  * recovering a dead node (as the new master or not) and clear
376  * the state and wake as soon as all affected lock resources have
377  * been marked with the RECOVERY flag */
dlm_in_recovery(struct dlm_ctxt * dlm)378 static int dlm_in_recovery(struct dlm_ctxt *dlm)
379 {
380 	int in_recovery;
381 	spin_lock(&dlm->spinlock);
382 	in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
383 	spin_unlock(&dlm->spinlock);
384 	return in_recovery;
385 }
386 
387 
dlm_wait_for_recovery(struct dlm_ctxt * dlm)388 void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
389 {
390 	if (dlm_in_recovery(dlm)) {
391 		mlog(0, "%s: reco thread %d in recovery: "
392 		     "state=%d, master=%u, dead=%u\n",
393 		     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
394 		     dlm->reco.state, dlm->reco.new_master,
395 		     dlm->reco.dead_node);
396 	}
397 	wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
398 }
399 
dlm_begin_recovery(struct dlm_ctxt * dlm)400 static void dlm_begin_recovery(struct dlm_ctxt *dlm)
401 {
402 	assert_spin_locked(&dlm->spinlock);
403 	BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
404 	printk(KERN_NOTICE "o2dlm: Begin recovery on domain %s for node %u\n",
405 	       dlm->name, dlm->reco.dead_node);
406 	dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
407 }
408 
dlm_end_recovery(struct dlm_ctxt * dlm)409 static void dlm_end_recovery(struct dlm_ctxt *dlm)
410 {
411 	spin_lock(&dlm->spinlock);
412 	BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
413 	dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
414 	spin_unlock(&dlm->spinlock);
415 	printk(KERN_NOTICE "o2dlm: End recovery on domain %s\n", dlm->name);
416 	wake_up(&dlm->reco.event);
417 }
418 
dlm_print_recovery_master(struct dlm_ctxt * dlm)419 static void dlm_print_recovery_master(struct dlm_ctxt *dlm)
420 {
421 	printk(KERN_NOTICE "o2dlm: Node %u (%s) is the Recovery Master for the "
422 	       "dead node %u in domain %s\n", dlm->reco.new_master,
423 	       (dlm->node_num == dlm->reco.new_master ? "me" : "he"),
424 	       dlm->reco.dead_node, dlm->name);
425 }
426 
dlm_do_recovery(struct dlm_ctxt * dlm)427 static int dlm_do_recovery(struct dlm_ctxt *dlm)
428 {
429 	int status = 0;
430 	int ret;
431 
432 	spin_lock(&dlm->spinlock);
433 
434 	if (dlm->migrate_done) {
435 		mlog(0, "%s: no need do recovery after migrating all "
436 		     "lock resources\n", dlm->name);
437 		spin_unlock(&dlm->spinlock);
438 		return 0;
439 	}
440 
441 	/* check to see if the new master has died */
442 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
443 	    test_bit(dlm->reco.new_master, dlm->recovery_map)) {
444 		mlog(0, "new master %u died while recovering %u!\n",
445 		     dlm->reco.new_master, dlm->reco.dead_node);
446 		/* unset the new_master, leave dead_node */
447 		dlm_set_reco_master(dlm, O2NM_INVALID_NODE_NUM);
448 	}
449 
450 	/* select a target to recover */
451 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
452 		int bit;
453 
454 		bit = find_first_bit(dlm->recovery_map, O2NM_MAX_NODES);
455 		if (bit >= O2NM_MAX_NODES || bit < 0)
456 			dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
457 		else
458 			dlm_set_reco_dead_node(dlm, bit);
459 	} else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
460 		/* BUG? */
461 		mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
462 		     dlm->reco.dead_node);
463 		dlm_set_reco_dead_node(dlm, O2NM_INVALID_NODE_NUM);
464 	}
465 
466 	if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
467 		spin_unlock(&dlm->spinlock);
468 		/* return to main thread loop and sleep. */
469 		return 0;
470 	}
471 	mlog(0, "%s(%d):recovery thread found node %u in the recovery map!\n",
472 	     dlm->name, task_pid_nr(dlm->dlm_reco_thread_task),
473 	     dlm->reco.dead_node);
474 
475 	/* take write barrier */
476 	/* (stops the list reshuffling thread, proxy ast handling) */
477 	dlm_begin_recovery(dlm);
478 
479 	spin_unlock(&dlm->spinlock);
480 
481 	if (dlm->reco.new_master == dlm->node_num)
482 		goto master_here;
483 
484 	if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
485 		/* choose a new master, returns 0 if this node
486 		 * is the master, -EEXIST if it's another node.
487 		 * this does not return until a new master is chosen
488 		 * or recovery completes entirely. */
489 		ret = dlm_pick_recovery_master(dlm);
490 		if (!ret) {
491 			/* already notified everyone.  go. */
492 			goto master_here;
493 		}
494 		mlog(0, "another node will master this recovery session.\n");
495 	}
496 
497 	dlm_print_recovery_master(dlm);
498 
499 	/* it is safe to start everything back up here
500 	 * because all of the dead node's lock resources
501 	 * have been marked as in-recovery */
502 	dlm_end_recovery(dlm);
503 
504 	/* sleep out in main dlm_recovery_thread loop. */
505 	return 0;
506 
507 master_here:
508 	dlm_print_recovery_master(dlm);
509 
510 	status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
511 	if (status < 0) {
512 		/* we should never hit this anymore */
513 		mlog(ML_ERROR, "%s: Error %d remastering locks for node %u, "
514 		     "retrying.\n", dlm->name, status, dlm->reco.dead_node);
515 		/* yield a bit to allow any final network messages
516 		 * to get handled on remaining nodes */
517 		msleep(100);
518 	} else {
519 		/* success!  see if any other nodes need recovery */
520 		mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
521 		     dlm->name, dlm->reco.dead_node, dlm->node_num);
522 		spin_lock(&dlm->spinlock);
523 		__dlm_reset_recovery(dlm);
524 		dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
525 		spin_unlock(&dlm->spinlock);
526 	}
527 	dlm_end_recovery(dlm);
528 
529 	/* continue and look for another dead node */
530 	return -EAGAIN;
531 }
532 
dlm_remaster_locks(struct dlm_ctxt * dlm,u8 dead_node)533 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
534 {
535 	int status = 0;
536 	struct dlm_reco_node_data *ndata;
537 	int all_nodes_done;
538 	int destroy = 0;
539 	int pass = 0;
540 
541 	do {
542 		/* we have become recovery master.  there is no escaping
543 		 * this, so just keep trying until we get it. */
544 		status = dlm_init_recovery_area(dlm, dead_node);
545 		if (status < 0) {
546 			mlog(ML_ERROR, "%s: failed to alloc recovery area, "
547 			     "retrying\n", dlm->name);
548 			msleep(1000);
549 		}
550 	} while (status != 0);
551 
552 	/* safe to access the node data list without a lock, since this
553 	 * process is the only one to change the list */
554 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
555 		BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
556 		ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
557 
558 		mlog(0, "%s: Requesting lock info from node %u\n", dlm->name,
559 		     ndata->node_num);
560 
561 		if (ndata->node_num == dlm->node_num) {
562 			ndata->state = DLM_RECO_NODE_DATA_DONE;
563 			continue;
564 		}
565 
566 		do {
567 			status = dlm_request_all_locks(dlm, ndata->node_num,
568 						       dead_node);
569 			if (status < 0) {
570 				mlog_errno(status);
571 				if (dlm_is_host_down(status)) {
572 					/* node died, ignore it for recovery */
573 					status = 0;
574 					ndata->state = DLM_RECO_NODE_DATA_DEAD;
575 					/* wait for the domain map to catch up
576 					 * with the network state. */
577 					wait_event_timeout(dlm->dlm_reco_thread_wq,
578 							   dlm_is_node_dead(dlm,
579 								ndata->node_num),
580 							   msecs_to_jiffies(1000));
581 					mlog(0, "waited 1 sec for %u, "
582 					     "dead? %s\n", ndata->node_num,
583 					     str_yes_no(dlm_is_node_dead(dlm, ndata->node_num)));
584 				} else {
585 					/* -ENOMEM on the other node */
586 					mlog(0, "%s: node %u returned "
587 					     "%d during recovery, retrying "
588 					     "after a short wait\n",
589 					     dlm->name, ndata->node_num,
590 					     status);
591 					msleep(100);
592 				}
593 			}
594 		} while (status != 0);
595 
596 		spin_lock(&dlm_reco_state_lock);
597 		switch (ndata->state) {
598 			case DLM_RECO_NODE_DATA_INIT:
599 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
600 			case DLM_RECO_NODE_DATA_REQUESTED:
601 				BUG();
602 				break;
603 			case DLM_RECO_NODE_DATA_DEAD:
604 				mlog(0, "node %u died after requesting "
605 				     "recovery info for node %u\n",
606 				     ndata->node_num, dead_node);
607 				/* fine.  don't need this node's info.
608 				 * continue without it. */
609 				break;
610 			case DLM_RECO_NODE_DATA_REQUESTING:
611 				ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
612 				mlog(0, "now receiving recovery data from "
613 				     "node %u for dead node %u\n",
614 				     ndata->node_num, dead_node);
615 				break;
616 			case DLM_RECO_NODE_DATA_RECEIVING:
617 				mlog(0, "already receiving recovery data from "
618 				     "node %u for dead node %u\n",
619 				     ndata->node_num, dead_node);
620 				break;
621 			case DLM_RECO_NODE_DATA_DONE:
622 				mlog(0, "already DONE receiving recovery data "
623 				     "from node %u for dead node %u\n",
624 				     ndata->node_num, dead_node);
625 				break;
626 		}
627 		spin_unlock(&dlm_reco_state_lock);
628 	}
629 
630 	mlog(0, "%s: Done requesting all lock info\n", dlm->name);
631 
632 	/* nodes should be sending reco data now
633 	 * just need to wait */
634 
635 	while (1) {
636 		/* check all the nodes now to see if we are
637 		 * done, or if anyone died */
638 		all_nodes_done = 1;
639 		spin_lock(&dlm_reco_state_lock);
640 		list_for_each_entry(ndata, &dlm->reco.node_data, list) {
641 			mlog(0, "checking recovery state of node %u\n",
642 			     ndata->node_num);
643 			switch (ndata->state) {
644 				case DLM_RECO_NODE_DATA_INIT:
645 				case DLM_RECO_NODE_DATA_REQUESTING:
646 					mlog(ML_ERROR, "bad ndata state for "
647 					     "node %u: state=%d\n",
648 					     ndata->node_num, ndata->state);
649 					BUG();
650 					break;
651 				case DLM_RECO_NODE_DATA_DEAD:
652 					mlog(0, "node %u died after "
653 					     "requesting recovery info for "
654 					     "node %u\n", ndata->node_num,
655 					     dead_node);
656 					break;
657 				case DLM_RECO_NODE_DATA_RECEIVING:
658 				case DLM_RECO_NODE_DATA_REQUESTED:
659 					mlog(0, "%s: node %u still in state %s\n",
660 					     dlm->name, ndata->node_num,
661 					     ndata->state==DLM_RECO_NODE_DATA_RECEIVING ?
662 					     "receiving" : "requested");
663 					all_nodes_done = 0;
664 					break;
665 				case DLM_RECO_NODE_DATA_DONE:
666 					mlog(0, "%s: node %u state is done\n",
667 					     dlm->name, ndata->node_num);
668 					break;
669 				case DLM_RECO_NODE_DATA_FINALIZE_SENT:
670 					mlog(0, "%s: node %u state is finalize\n",
671 					     dlm->name, ndata->node_num);
672 					break;
673 			}
674 		}
675 		spin_unlock(&dlm_reco_state_lock);
676 
677 		mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
678 		     str_yes_no(all_nodes_done));
679 		if (all_nodes_done) {
680 			int ret;
681 
682 			/* Set this flag on recovery master to avoid
683 			 * a new recovery for another dead node start
684 			 * before the recovery is not done. That may
685 			 * cause recovery hung.*/
686 			spin_lock(&dlm->spinlock);
687 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
688 			spin_unlock(&dlm->spinlock);
689 
690 			/* all nodes are now in DLM_RECO_NODE_DATA_DONE state
691 	 		 * just send a finalize message to everyone and
692 	 		 * clean up */
693 			mlog(0, "all nodes are done! send finalize\n");
694 			ret = dlm_send_finalize_reco_message(dlm);
695 			if (ret < 0)
696 				mlog_errno(ret);
697 
698 			spin_lock(&dlm->spinlock);
699 			dlm_finish_local_lockres_recovery(dlm, dead_node,
700 							  dlm->node_num);
701 			spin_unlock(&dlm->spinlock);
702 			mlog(0, "should be done with recovery!\n");
703 
704 			mlog(0, "finishing recovery of %s at %lu, "
705 			     "dead=%u, this=%u, new=%u\n", dlm->name,
706 			     jiffies, dlm->reco.dead_node,
707 			     dlm->node_num, dlm->reco.new_master);
708 			destroy = 1;
709 			status = 0;
710 			/* rescan everything marked dirty along the way */
711 			dlm_kick_thread(dlm, NULL);
712 			break;
713 		}
714 		/* wait to be signalled, with periodic timeout
715 		 * to check for node death */
716 		wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
717 					 kthread_should_stop(),
718 					 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
719 
720 	}
721 
722 	if (destroy)
723 		dlm_destroy_recovery_area(dlm);
724 
725 	return status;
726 }
727 
dlm_init_recovery_area(struct dlm_ctxt * dlm,u8 dead_node)728 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
729 {
730 	int num=0;
731 	struct dlm_reco_node_data *ndata;
732 
733 	spin_lock(&dlm->spinlock);
734 	bitmap_copy(dlm->reco.node_map, dlm->domain_map, O2NM_MAX_NODES);
735 	/* nodes can only be removed (by dying) after dropping
736 	 * this lock, and death will be trapped later, so this should do */
737 	spin_unlock(&dlm->spinlock);
738 
739 	while (1) {
740 		num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
741 		if (num >= O2NM_MAX_NODES) {
742 			break;
743 		}
744 		BUG_ON(num == dead_node);
745 
746 		ndata = kzalloc_obj(*ndata, GFP_NOFS);
747 		if (!ndata) {
748 			dlm_destroy_recovery_area(dlm);
749 			return -ENOMEM;
750 		}
751 		ndata->node_num = num;
752 		ndata->state = DLM_RECO_NODE_DATA_INIT;
753 		spin_lock(&dlm_reco_state_lock);
754 		list_add_tail(&ndata->list, &dlm->reco.node_data);
755 		spin_unlock(&dlm_reco_state_lock);
756 		num++;
757 	}
758 
759 	return 0;
760 }
761 
dlm_destroy_recovery_area(struct dlm_ctxt * dlm)762 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm)
763 {
764 	struct dlm_reco_node_data *ndata, *next;
765 	LIST_HEAD(tmplist);
766 
767 	spin_lock(&dlm_reco_state_lock);
768 	list_splice_init(&dlm->reco.node_data, &tmplist);
769 	spin_unlock(&dlm_reco_state_lock);
770 
771 	list_for_each_entry_safe(ndata, next, &tmplist, list) {
772 		list_del_init(&ndata->list);
773 		kfree(ndata);
774 	}
775 }
776 
dlm_request_all_locks(struct dlm_ctxt * dlm,u8 request_from,u8 dead_node)777 static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
778 				 u8 dead_node)
779 {
780 	struct dlm_lock_request lr;
781 	int ret;
782 	int status;
783 
784 	mlog(0, "\n");
785 
786 
787 	mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
788 		  "to %u\n", dead_node, request_from);
789 
790 	memset(&lr, 0, sizeof(lr));
791 	lr.node_idx = dlm->node_num;
792 	lr.dead_node = dead_node;
793 
794 	// send message
795 	ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
796 				 &lr, sizeof(lr), request_from, &status);
797 
798 	/* negative status is handled by caller */
799 	if (ret < 0)
800 		mlog(ML_ERROR, "%s: Error %d send LOCK_REQUEST to node %u "
801 		     "to recover dead node %u\n", dlm->name, ret,
802 		     request_from, dead_node);
803 	else
804 		ret = status;
805 	// return from here, then
806 	// sleep until all received or error
807 	return ret;
808 
809 }
810 
dlm_request_all_locks_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)811 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
812 				  void **ret_data)
813 {
814 	struct dlm_ctxt *dlm = data;
815 	struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
816 	char *buf = NULL;
817 	struct dlm_work_item *item = NULL;
818 
819 	if (!dlm_grab(dlm))
820 		return -EINVAL;
821 
822 	if (lr->dead_node != dlm->reco.dead_node) {
823 		mlog(ML_ERROR, "%s: node %u sent dead_node=%u, but local "
824 		     "dead_node is %u\n", dlm->name, lr->node_idx,
825 		     lr->dead_node, dlm->reco.dead_node);
826 		dlm_print_reco_node_status(dlm);
827 		/* this is a hack */
828 		dlm_put(dlm);
829 		return -ENOMEM;
830 	}
831 	BUG_ON(lr->dead_node != dlm->reco.dead_node);
832 
833 	item = kzalloc_obj(*item, GFP_NOFS);
834 	if (!item) {
835 		dlm_put(dlm);
836 		return -ENOMEM;
837 	}
838 
839 	/* this will get freed by dlm_request_all_locks_worker */
840 	buf = kmalloc(PAGE_SIZE, GFP_NOFS);
841 	if (!buf) {
842 		kfree(item);
843 		dlm_put(dlm);
844 		return -ENOMEM;
845 	}
846 
847 	/* queue up work for dlm_request_all_locks_worker */
848 	dlm_grab(dlm);  /* get an extra ref for the work item */
849 	dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
850 	item->u.ral.reco_master = lr->node_idx;
851 	item->u.ral.dead_node = lr->dead_node;
852 	spin_lock(&dlm->work_lock);
853 	list_add_tail(&item->list, &dlm->work_list);
854 	spin_unlock(&dlm->work_lock);
855 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
856 
857 	dlm_put(dlm);
858 	return 0;
859 }
860 
dlm_request_all_locks_worker(struct dlm_work_item * item,void * data)861 static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
862 {
863 	struct dlm_migratable_lockres *mres;
864 	struct dlm_lock_resource *res;
865 	struct dlm_ctxt *dlm;
866 	LIST_HEAD(resources);
867 	int ret;
868 	u8 dead_node, reco_master;
869 	int skip_all_done = 0;
870 
871 	dlm = item->dlm;
872 	dead_node = item->u.ral.dead_node;
873 	reco_master = item->u.ral.reco_master;
874 	mres = (struct dlm_migratable_lockres *)data;
875 
876 	mlog(0, "%s: recovery worker started, dead=%u, master=%u\n",
877 	     dlm->name, dead_node, reco_master);
878 
879 	if (dead_node != dlm->reco.dead_node ||
880 	    reco_master != dlm->reco.new_master) {
881 		/* worker could have been created before the recovery master
882 		 * died.  if so, do not continue, but do not error. */
883 		if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
884 			mlog(ML_NOTICE, "%s: will not send recovery state, "
885 			     "recovery master %u died, thread=(dead=%u,mas=%u)"
886 			     " current=(dead=%u,mas=%u)\n", dlm->name,
887 			     reco_master, dead_node, reco_master,
888 			     dlm->reco.dead_node, dlm->reco.new_master);
889 		} else {
890 			mlog(ML_NOTICE, "%s: reco state invalid: reco(dead=%u, "
891 			     "master=%u), request(dead=%u, master=%u)\n",
892 			     dlm->name, dlm->reco.dead_node,
893 			     dlm->reco.new_master, dead_node, reco_master);
894 		}
895 		goto leave;
896 	}
897 
898 	/* lock resources should have already been moved to the
899  	 * dlm->reco.resources list.  now move items from that list
900  	 * to a temp list if the dead owner matches.  note that the
901 	 * whole cluster recovers only one node at a time, so we
902 	 * can safely move UNKNOWN lock resources for each recovery
903 	 * session. */
904 	dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
905 
906 	/* now we can begin blasting lockreses without the dlm lock */
907 
908 	/* any errors returned will be due to the new_master dying,
909 	 * the dlm_reco_thread should detect this */
910 	list_for_each_entry(res, &resources, recovering) {
911 		ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
912 				   	DLM_MRES_RECOVERY);
913 		if (ret < 0) {
914 			mlog(ML_ERROR, "%s: node %u went down while sending "
915 			     "recovery state for dead node %u, ret=%d\n", dlm->name,
916 			     reco_master, dead_node, ret);
917 			skip_all_done = 1;
918 			break;
919 		}
920 	}
921 
922 	/* move the resources back to the list */
923 	spin_lock(&dlm->spinlock);
924 	list_splice_init(&resources, &dlm->reco.resources);
925 	spin_unlock(&dlm->spinlock);
926 
927 	if (!skip_all_done) {
928 		ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
929 		if (ret < 0) {
930 			mlog(ML_ERROR, "%s: node %u went down while sending "
931 			     "recovery all-done for dead node %u, ret=%d\n",
932 			     dlm->name, reco_master, dead_node, ret);
933 		}
934 	}
935 leave:
936 	kfree(data);
937 }
938 
939 
dlm_send_all_done_msg(struct dlm_ctxt * dlm,u8 dead_node,u8 send_to)940 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
941 {
942 	int ret, tmpret;
943 	struct dlm_reco_data_done done_msg;
944 
945 	memset(&done_msg, 0, sizeof(done_msg));
946 	done_msg.node_idx = dlm->node_num;
947 	done_msg.dead_node = dead_node;
948 	mlog(0, "sending DATA DONE message to %u, "
949 	     "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
950 	     done_msg.dead_node);
951 
952 	ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
953 				 sizeof(done_msg), send_to, &tmpret);
954 	if (ret < 0) {
955 		mlog(ML_ERROR, "%s: Error %d send RECO_DATA_DONE to node %u "
956 		     "to recover dead node %u\n", dlm->name, ret, send_to,
957 		     dead_node);
958 		if (!dlm_is_host_down(ret)) {
959 			BUG();
960 		}
961 	} else
962 		ret = tmpret;
963 	return ret;
964 }
965 
966 
dlm_reco_data_done_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)967 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
968 			       void **ret_data)
969 {
970 	struct dlm_ctxt *dlm = data;
971 	struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
972 	struct dlm_reco_node_data *ndata = NULL;
973 	int ret = -EINVAL;
974 
975 	if (!dlm_grab(dlm))
976 		return -EINVAL;
977 
978 	mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
979 	     "node_idx=%u, this node=%u\n", done->dead_node,
980 	     dlm->reco.dead_node, done->node_idx, dlm->node_num);
981 
982 	mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node),
983 			"Got DATA DONE: dead_node=%u, reco.dead_node=%u, "
984 			"node_idx=%u, this node=%u\n", done->dead_node,
985 			dlm->reco.dead_node, done->node_idx, dlm->node_num);
986 
987 	spin_lock(&dlm_reco_state_lock);
988 	list_for_each_entry(ndata, &dlm->reco.node_data, list) {
989 		if (ndata->node_num != done->node_idx)
990 			continue;
991 
992 		switch (ndata->state) {
993 			/* should have moved beyond INIT but not to FINALIZE yet */
994 			case DLM_RECO_NODE_DATA_INIT:
995 			case DLM_RECO_NODE_DATA_DEAD:
996 			case DLM_RECO_NODE_DATA_FINALIZE_SENT:
997 				mlog(ML_ERROR, "bad ndata state for node %u:"
998 				     " state=%d\n", ndata->node_num,
999 				     ndata->state);
1000 				BUG();
1001 				break;
1002 			/* these states are possible at this point, anywhere along
1003 			 * the line of recovery */
1004 			case DLM_RECO_NODE_DATA_DONE:
1005 			case DLM_RECO_NODE_DATA_RECEIVING:
1006 			case DLM_RECO_NODE_DATA_REQUESTED:
1007 			case DLM_RECO_NODE_DATA_REQUESTING:
1008 				mlog(0, "node %u is DONE sending "
1009 					  "recovery data!\n",
1010 					  ndata->node_num);
1011 
1012 				ndata->state = DLM_RECO_NODE_DATA_DONE;
1013 				ret = 0;
1014 				break;
1015 		}
1016 	}
1017 	spin_unlock(&dlm_reco_state_lock);
1018 
1019 	/* wake the recovery thread, some node is done */
1020 	if (!ret)
1021 		dlm_kick_recovery_thread(dlm);
1022 
1023 	if (ret < 0)
1024 		mlog(ML_ERROR, "failed to find recovery node data for node "
1025 		     "%u\n", done->node_idx);
1026 	dlm_put(dlm);
1027 
1028 	mlog(0, "leaving reco data done handler, ret=%d\n", ret);
1029 	return ret;
1030 }
1031 
dlm_move_reco_locks_to_list(struct dlm_ctxt * dlm,struct list_head * list,u8 dead_node)1032 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
1033 					struct list_head *list,
1034 				       	u8 dead_node)
1035 {
1036 	struct dlm_lock_resource *res, *next;
1037 	struct dlm_lock *lock;
1038 
1039 	spin_lock(&dlm->spinlock);
1040 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
1041 		/* always prune any $RECOVERY entries for dead nodes,
1042 		 * otherwise hangs can occur during later recovery */
1043 		if (dlm_is_recovery_lock(res->lockname.name,
1044 					 res->lockname.len)) {
1045 			spin_lock(&res->spinlock);
1046 			list_for_each_entry(lock, &res->granted, list) {
1047 				if (lock->ml.node == dead_node) {
1048 					mlog(0, "AHA! there was "
1049 					     "a $RECOVERY lock for dead "
1050 					     "node %u (%s)!\n",
1051 					     dead_node, dlm->name);
1052 					list_del_init(&lock->list);
1053 					dlm_lock_put(lock);
1054 					/* Can't schedule DLM_UNLOCK_FREE_LOCK
1055 					 * - do manually */
1056 					dlm_lock_put(lock);
1057 					break;
1058 				}
1059 			}
1060 			spin_unlock(&res->spinlock);
1061 			continue;
1062 		}
1063 
1064 		if (res->owner == dead_node) {
1065 			mlog(0, "found lockres owned by dead node while "
1066 				  "doing recovery for node %u. sending it.\n",
1067 				  dead_node);
1068 			list_move_tail(&res->recovering, list);
1069 		} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
1070 			mlog(0, "found UNKNOWN owner while doing recovery "
1071 				  "for node %u. sending it.\n", dead_node);
1072 			list_move_tail(&res->recovering, list);
1073 		}
1074 	}
1075 	spin_unlock(&dlm->spinlock);
1076 }
1077 
dlm_num_locks_in_lockres(struct dlm_lock_resource * res)1078 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
1079 {
1080 	int total_locks = 0;
1081 	struct list_head *iter, *queue = &res->granted;
1082 	int i;
1083 
1084 	for (i=0; i<3; i++) {
1085 		list_for_each(iter, queue)
1086 			total_locks++;
1087 		queue++;
1088 	}
1089 	return total_locks;
1090 }
1091 
1092 
dlm_send_mig_lockres_msg(struct dlm_ctxt * dlm,struct dlm_migratable_lockres * mres,u8 send_to,struct dlm_lock_resource * res,int total_locks)1093 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
1094 				      struct dlm_migratable_lockres *mres,
1095 				      u8 send_to,
1096 				      struct dlm_lock_resource *res,
1097 				      int total_locks)
1098 {
1099 	u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
1100 	int mres_total_locks = be32_to_cpu(mres->total_locks);
1101 	int ret = 0, status = 0;
1102 	u8 orig_flags = mres->flags,
1103 	   orig_master = mres->master;
1104 
1105 	BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
1106 	if (!mres->num_locks)
1107 		return 0;
1108 
1109 	/* add an all-done flag if we reached the last lock */
1110 	orig_flags = mres->flags;
1111 	BUG_ON(total_locks > mres_total_locks);
1112 	if (total_locks == mres_total_locks)
1113 		mres->flags |= DLM_MRES_ALL_DONE;
1114 
1115 	mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n",
1116 	     dlm->name, res->lockname.len, res->lockname.name,
1117 	     orig_flags & DLM_MRES_MIGRATION ? "migration" : "recovery",
1118 	     send_to);
1119 
1120 	/* send it */
1121 	ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
1122 				 struct_size(mres, ml, mres->num_locks),
1123 				 send_to, &status);
1124 	if (ret < 0) {
1125 		/* XXX: negative status is not handled.
1126 		 * this will end up killing this node. */
1127 		mlog(ML_ERROR, "%s: res %.*s, Error %d send MIG_LOCKRES to "
1128 		     "node %u (%s)\n", dlm->name, mres->lockname_len,
1129 		     mres->lockname, ret, send_to,
1130 		     (orig_flags & DLM_MRES_MIGRATION ?
1131 		      "migration" : "recovery"));
1132 	} else {
1133 		/* might get an -ENOMEM back here */
1134 		ret = status;
1135 		if (ret < 0) {
1136 			mlog_errno(ret);
1137 
1138 			if (ret == -EFAULT) {
1139 				mlog(ML_ERROR, "node %u told me to kill "
1140 				     "myself!\n", send_to);
1141 				BUG();
1142 			}
1143 		}
1144 	}
1145 
1146 	/* zero and reinit the message buffer */
1147 	dlm_init_migratable_lockres(mres, res->lockname.name,
1148 				    res->lockname.len, mres_total_locks,
1149 				    mig_cookie, orig_flags, orig_master);
1150 	return ret;
1151 }
1152 
dlm_init_migratable_lockres(struct dlm_migratable_lockres * mres,const char * lockname,int namelen,int total_locks,u64 cookie,u8 flags,u8 master)1153 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
1154 					const char *lockname, int namelen,
1155 					int total_locks, u64 cookie,
1156 					u8 flags, u8 master)
1157 {
1158 	/* mres here is one full page */
1159 	clear_page(mres);
1160 	mres->lockname_len = namelen;
1161 	memcpy(mres->lockname, lockname, namelen);
1162 	mres->num_locks = 0;
1163 	mres->total_locks = cpu_to_be32(total_locks);
1164 	mres->mig_cookie = cpu_to_be64(cookie);
1165 	mres->flags = flags;
1166 	mres->master = master;
1167 }
1168 
dlm_prepare_lvb_for_migration(struct dlm_lock * lock,struct dlm_migratable_lockres * mres,int queue)1169 static void dlm_prepare_lvb_for_migration(struct dlm_lock *lock,
1170 					  struct dlm_migratable_lockres *mres,
1171 					  int queue)
1172 {
1173 	if (!lock->lksb)
1174 	       return;
1175 
1176 	/* Ignore lvb in all locks in the blocked list */
1177 	if (queue == DLM_BLOCKED_LIST)
1178 		return;
1179 
1180 	/* Only consider lvbs in locks with granted EX or PR lock levels */
1181 	if (lock->ml.type != LKM_EXMODE && lock->ml.type != LKM_PRMODE)
1182 		return;
1183 
1184 	if (dlm_lvb_is_empty(mres->lvb)) {
1185 		memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1186 		return;
1187 	}
1188 
1189 	/* Ensure the lvb copied for migration matches in other valid locks */
1190 	if (!memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))
1191 		return;
1192 
1193 	mlog(ML_ERROR, "Mismatched lvb in lock cookie=%u:%llu, name=%.*s, "
1194 	     "node=%u\n",
1195 	     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
1196 	     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
1197 	     lock->lockres->lockname.len, lock->lockres->lockname.name,
1198 	     lock->ml.node);
1199 	dlm_print_one_lock_resource(lock->lockres);
1200 	BUG();
1201 }
1202 
1203 /* returns 1 if this lock fills the network structure,
1204  * 0 otherwise */
dlm_add_lock_to_array(struct dlm_lock * lock,struct dlm_migratable_lockres * mres,int queue)1205 static int dlm_add_lock_to_array(struct dlm_lock *lock,
1206 				 struct dlm_migratable_lockres *mres, int queue)
1207 {
1208 	struct dlm_migratable_lock *ml;
1209 	int lock_num = mres->num_locks;
1210 
1211 	ml = &(mres->ml[lock_num]);
1212 	ml->cookie = lock->ml.cookie;
1213 	ml->type = lock->ml.type;
1214 	ml->convert_type = lock->ml.convert_type;
1215 	ml->highest_blocked = lock->ml.highest_blocked;
1216 	ml->list = queue;
1217 	if (lock->lksb) {
1218 		ml->flags = lock->lksb->flags;
1219 		dlm_prepare_lvb_for_migration(lock, mres, queue);
1220 	}
1221 	ml->node = lock->ml.node;
1222 	mres->num_locks++;
1223 	/* we reached the max, send this network message */
1224 	if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1225 		return 1;
1226 	return 0;
1227 }
1228 
dlm_add_dummy_lock(struct dlm_ctxt * dlm,struct dlm_migratable_lockres * mres)1229 static void dlm_add_dummy_lock(struct dlm_ctxt *dlm,
1230 			       struct dlm_migratable_lockres *mres)
1231 {
1232 	struct dlm_lock dummy;
1233 	memset(&dummy, 0, sizeof(dummy));
1234 	dummy.ml.cookie = 0;
1235 	dummy.ml.type = LKM_IVMODE;
1236 	dummy.ml.convert_type = LKM_IVMODE;
1237 	dummy.ml.highest_blocked = LKM_IVMODE;
1238 	dummy.lksb = NULL;
1239 	dummy.ml.node = dlm->node_num;
1240 	dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST);
1241 }
1242 
dlm_is_dummy_lock(struct dlm_ctxt * dlm,struct dlm_migratable_lock * ml,u8 * nodenum)1243 static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm,
1244 				    struct dlm_migratable_lock *ml,
1245 				    u8 *nodenum)
1246 {
1247 	if (unlikely(ml->cookie == 0 &&
1248 	    ml->type == LKM_IVMODE &&
1249 	    ml->convert_type == LKM_IVMODE &&
1250 	    ml->highest_blocked == LKM_IVMODE &&
1251 	    ml->list == DLM_BLOCKED_LIST)) {
1252 		*nodenum = ml->node;
1253 		return 1;
1254 	}
1255 	return 0;
1256 }
1257 
dlm_send_one_lockres(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_migratable_lockres * mres,u8 send_to,u8 flags)1258 int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1259 			 struct dlm_migratable_lockres *mres,
1260 			 u8 send_to, u8 flags)
1261 {
1262 	struct list_head *queue;
1263 	int total_locks, i;
1264 	u64 mig_cookie = 0;
1265 	struct dlm_lock *lock;
1266 	int ret = 0;
1267 
1268 	BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1269 
1270 	mlog(0, "sending to %u\n", send_to);
1271 
1272 	total_locks = dlm_num_locks_in_lockres(res);
1273 	if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1274 		/* rare, but possible */
1275 		mlog(0, "argh.  lockres has %d locks.  this will "
1276 			  "require more than one network packet to "
1277 			  "migrate\n", total_locks);
1278 		mig_cookie = dlm_get_next_mig_cookie();
1279 	}
1280 
1281 	dlm_init_migratable_lockres(mres, res->lockname.name,
1282 				    res->lockname.len, total_locks,
1283 				    mig_cookie, flags, res->owner);
1284 
1285 	total_locks = 0;
1286 	for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1287 		queue = dlm_list_idx_to_ptr(res, i);
1288 		list_for_each_entry(lock, queue, list) {
1289 			/* add another lock. */
1290 			total_locks++;
1291 			if (!dlm_add_lock_to_array(lock, mres, i))
1292 				continue;
1293 
1294 			/* this filled the lock message,
1295 			 * we must send it immediately. */
1296 			ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1297 						       res, total_locks);
1298 			if (ret < 0)
1299 				goto error;
1300 		}
1301 	}
1302 	if (total_locks == 0) {
1303 		/* send a dummy lock to indicate a mastery reference only */
1304 		mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n",
1305 		     dlm->name, res->lockname.len, res->lockname.name,
1306 		     send_to, flags & DLM_MRES_RECOVERY ? "recovery" :
1307 		     "migration");
1308 		dlm_add_dummy_lock(dlm, mres);
1309 	}
1310 	/* flush any remaining locks */
1311 	ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
1312 	if (ret < 0)
1313 		goto error;
1314 	return ret;
1315 
1316 error:
1317 	mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n",
1318 	     dlm->name, ret);
1319 	if (!dlm_is_host_down(ret))
1320 		BUG();
1321 	mlog(0, "%s: node %u went down while sending %s "
1322 	     "lockres %.*s\n", dlm->name, send_to,
1323 	     flags & DLM_MRES_RECOVERY ?  "recovery" : "migration",
1324 	     res->lockname.len, res->lockname.name);
1325 	return ret;
1326 }
1327 
1328 
1329 
1330 /*
1331  * this message will contain no more than one page worth of
1332  * recovery data, and it will work on only one lockres.
1333  * there may be many locks in this page, and we may need to wait
1334  * for additional packets to complete all the locks (rare, but
1335  * possible).
1336  */
1337 /*
1338  * NOTE: the allocation error cases here are scary
1339  * we really cannot afford to fail an alloc in recovery
1340  * do we spin?  returning an error only delays the problem really
1341  */
1342 
dlm_mig_lockres_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)1343 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
1344 			    void **ret_data)
1345 {
1346 	struct dlm_ctxt *dlm = data;
1347 	struct dlm_migratable_lockres *mres =
1348 		(struct dlm_migratable_lockres *)msg->buf;
1349 	int ret = 0;
1350 	u8 real_master;
1351 	u8 extra_refs = 0;
1352 	char *buf = NULL;
1353 	struct dlm_work_item *item = NULL;
1354 	struct dlm_lock_resource *res = NULL;
1355 	unsigned int hash;
1356 
1357 	if (!dlm_grab(dlm))
1358 		return -EINVAL;
1359 
1360 	if (mres->lockname_len > DLM_LOCKID_NAME_MAX ||
1361 	    mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS ||
1362 	    be16_to_cpu(msg->data_len) < struct_size(mres, ml, mres->num_locks)) {
1363 		mlog(ML_ERROR, "%s: invalid lockres migration message from %u\n",
1364 		     dlm->name, mres->master);
1365 		dlm_put(dlm);
1366 		return -EINVAL;
1367 	}
1368 
1369 	if (!dlm_joined(dlm)) {
1370 		mlog(ML_ERROR, "Domain %s not joined! "
1371 			  "lockres %.*s, master %u\n",
1372 			  dlm->name, mres->lockname_len,
1373 			  mres->lockname, mres->master);
1374 		dlm_put(dlm);
1375 		return -EINVAL;
1376 	}
1377 
1378 	BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1379 
1380 	real_master = mres->master;
1381 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1382 		/* cannot migrate a lockres with no master */
1383 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1384 	}
1385 
1386 	mlog(0, "%s message received from node %u\n",
1387 		  (mres->flags & DLM_MRES_RECOVERY) ?
1388 		  "recovery" : "migration", mres->master);
1389 	if (mres->flags & DLM_MRES_ALL_DONE)
1390 		mlog(0, "all done flag.  all lockres data received!\n");
1391 
1392 	ret = -ENOMEM;
1393 	buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS);
1394 	item = kzalloc_obj(*item, GFP_NOFS);
1395 	if (!buf || !item)
1396 		goto leave;
1397 
1398 	/* lookup the lock to see if we have a secondary queue for this
1399 	 * already...  just add the locks in and this will have its owner
1400 	 * and RECOVERY flag changed when it completes. */
1401 	hash = dlm_lockid_hash(mres->lockname, mres->lockname_len);
1402 	spin_lock(&dlm->spinlock);
1403 	res = __dlm_lookup_lockres_full(dlm, mres->lockname, mres->lockname_len,
1404 			hash);
1405 	if (res) {
1406 	 	/* this will get a ref on res */
1407 		/* mark it as recovering/migrating and hash it */
1408 		spin_lock(&res->spinlock);
1409 		if (res->state & DLM_LOCK_RES_DROPPING_REF) {
1410 			mlog(0, "%s: node is attempting to migrate "
1411 				"lockres %.*s, but marked as dropping "
1412 				" ref!\n", dlm->name,
1413 				mres->lockname_len, mres->lockname);
1414 			ret = -EINVAL;
1415 			spin_unlock(&res->spinlock);
1416 			spin_unlock(&dlm->spinlock);
1417 			dlm_lockres_put(res);
1418 			goto leave;
1419 		}
1420 
1421 		if (mres->flags & DLM_MRES_RECOVERY) {
1422 			res->state |= DLM_LOCK_RES_RECOVERING;
1423 		} else {
1424 			if (res->state & DLM_LOCK_RES_MIGRATING) {
1425 				/* this is at least the second
1426 				 * lockres message */
1427 				mlog(0, "lock %.*s is already migrating\n",
1428 					  mres->lockname_len,
1429 					  mres->lockname);
1430 			} else if (res->state & DLM_LOCK_RES_RECOVERING) {
1431 				/* caller should BUG */
1432 				mlog(ML_ERROR, "node is attempting to migrate "
1433 				     "lock %.*s, but marked as recovering!\n",
1434 				     mres->lockname_len, mres->lockname);
1435 				ret = -EFAULT;
1436 				spin_unlock(&res->spinlock);
1437 				spin_unlock(&dlm->spinlock);
1438 				dlm_lockres_put(res);
1439 				goto leave;
1440 			}
1441 			res->state |= DLM_LOCK_RES_MIGRATING;
1442 		}
1443 		spin_unlock(&res->spinlock);
1444 		spin_unlock(&dlm->spinlock);
1445 	} else {
1446 		spin_unlock(&dlm->spinlock);
1447 		/* need to allocate, just like if it was
1448 		 * mastered here normally  */
1449 		res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1450 		if (!res)
1451 			goto leave;
1452 
1453 		/* to match the ref that we would have gotten if
1454 		 * dlm_lookup_lockres had succeeded */
1455 		dlm_lockres_get(res);
1456 
1457 		/* mark it as recovering/migrating and hash it */
1458 		if (mres->flags & DLM_MRES_RECOVERY)
1459 			res->state |= DLM_LOCK_RES_RECOVERING;
1460 		else
1461 			res->state |= DLM_LOCK_RES_MIGRATING;
1462 
1463 		spin_lock(&dlm->spinlock);
1464 		__dlm_insert_lockres(dlm, res);
1465 		spin_unlock(&dlm->spinlock);
1466 
1467 		/* Add an extra ref for this lock-less lockres lest the
1468 		 * dlm_thread purges it before we get the chance to add
1469 		 * locks to it */
1470 		dlm_lockres_get(res);
1471 
1472 		/* There are three refs that need to be put.
1473 		 * 1. Taken above.
1474 		 * 2. kref_init in dlm_new_lockres()->dlm_init_lockres().
1475 		 * 3. dlm_lookup_lockres()
1476 		 * The first one is handled at the end of this function. The
1477 		 * other two are handled in the worker thread after locks have
1478 		 * been attached. Yes, we don't wait for purge time to match
1479 		 * kref_init. The lockres will still have at least one ref
1480 		 * added because it is in the hash __dlm_insert_lockres() */
1481 		extra_refs++;
1482 
1483 		/* now that the new lockres is inserted,
1484 		 * make it usable by other processes */
1485 		spin_lock(&res->spinlock);
1486 		res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1487 		spin_unlock(&res->spinlock);
1488 		wake_up(&res->wq);
1489 	}
1490 
1491 	/* at this point we have allocated everything we need,
1492 	 * and we have a hashed lockres with an extra ref and
1493 	 * the proper res->state flags. */
1494 	ret = 0;
1495 	spin_lock(&res->spinlock);
1496 	/* drop this either when master requery finds a different master
1497 	 * or when a lock is added by the recovery worker */
1498 	dlm_lockres_grab_inflight_ref(dlm, res);
1499 	if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1500 		/* migration cannot have an unknown master */
1501 		BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1502 		mlog(0, "recovery has passed me a lockres with an "
1503 			  "unknown owner.. will need to requery: "
1504 			  "%.*s\n", mres->lockname_len, mres->lockname);
1505 	} else {
1506 		/* take a reference now to pin the lockres, drop it
1507 		 * when locks are added in the worker */
1508 		dlm_change_lockres_owner(dlm, res, dlm->node_num);
1509 	}
1510 	spin_unlock(&res->spinlock);
1511 
1512 	/* queue up work for dlm_mig_lockres_worker */
1513 	dlm_grab(dlm);  /* get an extra ref for the work item */
1514 	memcpy(buf, msg->buf, be16_to_cpu(msg->data_len));  /* copy the whole message */
1515 	dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1516 	item->u.ml.lockres = res; /* already have a ref */
1517 	item->u.ml.real_master = real_master;
1518 	item->u.ml.extra_ref = extra_refs;
1519 	spin_lock(&dlm->work_lock);
1520 	list_add_tail(&item->list, &dlm->work_list);
1521 	spin_unlock(&dlm->work_lock);
1522 	queue_work(dlm->dlm_worker, &dlm->dispatched_work);
1523 
1524 leave:
1525 	/* One extra ref taken needs to be put here */
1526 	if (extra_refs)
1527 		dlm_lockres_put(res);
1528 
1529 	dlm_put(dlm);
1530 	if (ret < 0) {
1531 		kfree(buf);
1532 		kfree(item);
1533 		mlog_errno(ret);
1534 	}
1535 
1536 	return ret;
1537 }
1538 
1539 
dlm_mig_lockres_worker(struct dlm_work_item * item,void * data)1540 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1541 {
1542 	struct dlm_ctxt *dlm;
1543 	struct dlm_migratable_lockres *mres;
1544 	int ret = 0;
1545 	struct dlm_lock_resource *res;
1546 	u8 real_master;
1547 	u8 extra_ref;
1548 
1549 	dlm = item->dlm;
1550 	mres = (struct dlm_migratable_lockres *)data;
1551 
1552 	res = item->u.ml.lockres;
1553 	real_master = item->u.ml.real_master;
1554 	extra_ref = item->u.ml.extra_ref;
1555 
1556 	if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1557 		/* this case is super-rare. only occurs if
1558 		 * node death happens during migration. */
1559 again:
1560 		ret = dlm_lockres_master_requery(dlm, res, &real_master);
1561 		if (ret < 0) {
1562 			mlog(0, "dlm_lockres_master_requery ret=%d\n",
1563 				  ret);
1564 			goto again;
1565 		}
1566 		if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1567 			mlog(0, "lockres %.*s not claimed.  "
1568 				   "this node will take it.\n",
1569 				   res->lockname.len, res->lockname.name);
1570 		} else {
1571 			spin_lock(&res->spinlock);
1572 			dlm_lockres_drop_inflight_ref(dlm, res);
1573 			spin_unlock(&res->spinlock);
1574 			mlog(0, "master needs to respond to sender "
1575 				  "that node %u still owns %.*s\n",
1576 				  real_master, res->lockname.len,
1577 				  res->lockname.name);
1578 			/* cannot touch this lockres */
1579 			goto leave;
1580 		}
1581 	}
1582 
1583 	ret = dlm_process_recovery_data(dlm, res, mres);
1584 	if (ret < 0)
1585 		mlog(0, "dlm_process_recovery_data returned  %d\n", ret);
1586 	else
1587 		mlog(0, "dlm_process_recovery_data succeeded\n");
1588 
1589 	if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1590 	                   (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1591 		ret = dlm_finish_migration(dlm, res, mres->master);
1592 		if (ret < 0)
1593 			mlog_errno(ret);
1594 	}
1595 
1596 leave:
1597 	/* See comment in dlm_mig_lockres_handler() */
1598 	if (res) {
1599 		if (extra_ref)
1600 			dlm_lockres_put(res);
1601 		dlm_lockres_put(res);
1602 	}
1603 	kfree(data);
1604 }
1605 
1606 
1607 
dlm_lockres_master_requery(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 * real_master)1608 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1609 				      struct dlm_lock_resource *res,
1610 				      u8 *real_master)
1611 {
1612 	struct dlm_node_iter iter;
1613 	int nodenum;
1614 	int ret = 0;
1615 
1616 	*real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1617 
1618 	/* we only reach here if one of the two nodes in a
1619 	 * migration died while the migration was in progress.
1620 	 * at this point we need to requery the master.  we
1621 	 * know that the new_master got as far as creating
1622 	 * an mle on at least one node, but we do not know
1623 	 * if any nodes had actually cleared the mle and set
1624 	 * the master to the new_master.  the old master
1625 	 * is supposed to set the owner to UNKNOWN in the
1626 	 * event of a new_master death, so the only possible
1627 	 * responses that we can get from nodes here are
1628 	 * that the master is new_master, or that the master
1629 	 * is UNKNOWN.
1630 	 * if all nodes come back with UNKNOWN then we know
1631 	 * the lock needs remastering here.
1632 	 * if any node comes back with a valid master, check
1633 	 * to see if that master is the one that we are
1634 	 * recovering.  if so, then the new_master died and
1635 	 * we need to remaster this lock.  if not, then the
1636 	 * new_master survived and that node will respond to
1637 	 * other nodes about the owner.
1638 	 * if there is an owner, this node needs to dump this
1639 	 * lockres and alert the sender that this lockres
1640 	 * was rejected. */
1641 	spin_lock(&dlm->spinlock);
1642 	dlm_node_iter_init(dlm->domain_map, &iter);
1643 	spin_unlock(&dlm->spinlock);
1644 
1645 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1646 		/* do not send to self */
1647 		if (nodenum == dlm->node_num)
1648 			continue;
1649 		ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1650 		if (ret < 0) {
1651 			mlog_errno(ret);
1652 			if (!dlm_is_host_down(ret))
1653 				BUG();
1654 			/* host is down, so answer for that node would be
1655 			 * DLM_LOCK_RES_OWNER_UNKNOWN.  continue. */
1656 		}
1657 		if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1658 			mlog(0, "lock master is %u\n", *real_master);
1659 			break;
1660 		}
1661 	}
1662 	return ret;
1663 }
1664 
1665 
dlm_do_master_requery(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 nodenum,u8 * real_master)1666 int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1667 			  u8 nodenum, u8 *real_master)
1668 {
1669 	int ret;
1670 	struct dlm_master_requery req;
1671 	int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1672 
1673 	memset(&req, 0, sizeof(req));
1674 	req.node_idx = dlm->node_num;
1675 	req.namelen = res->lockname.len;
1676 	memcpy(req.name, res->lockname.name, res->lockname.len);
1677 
1678 resend:
1679 	ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1680 				 &req, sizeof(req), nodenum, &status);
1681 	if (ret < 0)
1682 		mlog(ML_ERROR, "Error %d when sending message %u (key "
1683 		     "0x%x) to node %u\n", ret, DLM_MASTER_REQUERY_MSG,
1684 		     dlm->key, nodenum);
1685 	else if (status == -ENOMEM) {
1686 		mlog_errno(status);
1687 		msleep(50);
1688 		goto resend;
1689 	} else {
1690 		BUG_ON(status < 0);
1691 		BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1692 		*real_master = (u8) (status & 0xff);
1693 		mlog(0, "node %u responded to master requery with %u\n",
1694 			  nodenum, *real_master);
1695 		ret = 0;
1696 	}
1697 	return ret;
1698 }
1699 
1700 
1701 /* this function cannot error, so unless the sending
1702  * or receiving of the message failed, the owner can
1703  * be trusted */
dlm_master_requery_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)1704 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
1705 			       void **ret_data)
1706 {
1707 	struct dlm_ctxt *dlm = data;
1708 	struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1709 	struct dlm_lock_resource *res = NULL;
1710 	unsigned int hash;
1711 	int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1712 	u32 flags = DLM_ASSERT_MASTER_REQUERY;
1713 	int dispatched = 0;
1714 
1715 	if (!dlm_grab(dlm)) {
1716 		/* since the domain has gone away on this
1717 		 * node, the proper response is UNKNOWN */
1718 		return master;
1719 	}
1720 
1721 	hash = dlm_lockid_hash(req->name, req->namelen);
1722 
1723 	spin_lock(&dlm->spinlock);
1724 	res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash);
1725 	if (res) {
1726 		spin_lock(&res->spinlock);
1727 		master = res->owner;
1728 		if (master == dlm->node_num) {
1729 			int ret = dlm_dispatch_assert_master(dlm, res,
1730 							     0, 0, flags);
1731 			if (ret < 0) {
1732 				mlog_errno(ret);
1733 				spin_unlock(&res->spinlock);
1734 				dlm_lockres_put(res);
1735 				spin_unlock(&dlm->spinlock);
1736 				dlm_put(dlm);
1737 				/* sender will take care of this and retry */
1738 				return ret;
1739 			} else {
1740 				dispatched = 1;
1741 				__dlm_lockres_grab_inflight_worker(dlm, res);
1742 				spin_unlock(&res->spinlock);
1743 			}
1744 		} else {
1745 			/* put.. in case we are not the master */
1746 			spin_unlock(&res->spinlock);
1747 			dlm_lockres_put(res);
1748 		}
1749 	}
1750 	spin_unlock(&dlm->spinlock);
1751 
1752 	if (!dispatched)
1753 		dlm_put(dlm);
1754 	return master;
1755 }
1756 
1757 static inline struct list_head *
dlm_list_num_to_pointer(struct dlm_lock_resource * res,int list_num)1758 dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1759 {
1760 	struct list_head *ret;
1761 	BUG_ON(list_num < 0);
1762 	BUG_ON(list_num > 2);
1763 	ret = &(res->granted);
1764 	ret += list_num;
1765 	return ret;
1766 }
1767 /* TODO: do ast flush business
1768  * TODO: do MIGRATING and RECOVERING spinning
1769  */
1770 
1771 /*
1772 * NOTE about in-flight requests during migration:
1773 *
1774 * Before attempting the migrate, the master has marked the lockres as
1775 * MIGRATING and then flushed all of its pending ASTS.  So any in-flight
1776 * requests either got queued before the MIGRATING flag got set, in which
1777 * case the lock data will reflect the change and a return message is on
1778 * the way, or the request failed to get in before MIGRATING got set.  In
1779 * this case, the caller will be told to spin and wait for the MIGRATING
1780 * flag to be dropped, then recheck the master.
1781 * This holds true for the convert, cancel and unlock cases, and since lvb
1782 * updates are tied to these same messages, it applies to lvb updates as
1783 * well.  For the lock case, there is no way a lock can be on the master
1784 * queue and not be on the secondary queue since the lock is always added
1785 * locally first.  This means that the new target node will never be sent
1786 * a lock that he doesn't already have on the list.
1787 * In total, this means that the local lock is correct and should not be
1788 * updated to match the one sent by the master.  Any messages sent back
1789 * from the master before the MIGRATING flag will bring the lock properly
1790 * up-to-date, and the change will be ordered properly for the waiter.
1791 * We will *not* attempt to modify the lock underneath the waiter.
1792 */
1793 
dlm_process_recovery_data(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,struct dlm_migratable_lockres * mres)1794 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1795 				     struct dlm_lock_resource *res,
1796 				     struct dlm_migratable_lockres *mres)
1797 {
1798 	struct dlm_migratable_lock *ml;
1799 	struct list_head *queue, *iter;
1800 	struct list_head *tmpq = NULL;
1801 	struct dlm_lock *newlock = NULL;
1802 	struct dlm_lockstatus *lksb = NULL;
1803 	int ret = 0;
1804 	int i, j, bad;
1805 	struct dlm_lock *lock;
1806 	u8 from = O2NM_MAX_NODES;
1807 	__be64 c;
1808 
1809 	mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1810 	for (i=0; i<mres->num_locks; i++) {
1811 		ml = &(mres->ml[i]);
1812 
1813 		if (dlm_is_dummy_lock(dlm, ml, &from)) {
1814 			/* placeholder, just need to set the refmap bit */
1815 			BUG_ON(mres->num_locks != 1);
1816 			mlog(0, "%s:%.*s: dummy lock for %u\n",
1817 			     dlm->name, mres->lockname_len, mres->lockname,
1818 			     from);
1819 			spin_lock(&res->spinlock);
1820 			dlm_lockres_set_refmap_bit(dlm, res, from);
1821 			spin_unlock(&res->spinlock);
1822 			break;
1823 		}
1824 		BUG_ON(ml->highest_blocked != LKM_IVMODE);
1825 		newlock = NULL;
1826 		lksb = NULL;
1827 
1828 		queue = dlm_list_num_to_pointer(res, ml->list);
1829 		tmpq = NULL;
1830 
1831 		/* if the lock is for the local node it needs to
1832 		 * be moved to the proper location within the queue.
1833 		 * do not allocate a new lock structure. */
1834 		if (ml->node == dlm->node_num) {
1835 			/* MIGRATION ONLY! */
1836 			BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1837 
1838 			lock = NULL;
1839 			spin_lock(&res->spinlock);
1840 			for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) {
1841 				tmpq = dlm_list_idx_to_ptr(res, j);
1842 				list_for_each(iter, tmpq) {
1843 					lock = list_entry(iter,
1844 						  struct dlm_lock, list);
1845 					if (lock->ml.cookie == ml->cookie)
1846 						break;
1847 					lock = NULL;
1848 				}
1849 				if (lock)
1850 					break;
1851 			}
1852 
1853 			/* lock is always created locally first, and
1854 			 * destroyed locally last.  it must be on the list */
1855 			if (!lock) {
1856 				c = ml->cookie;
1857 				mlog(ML_ERROR, "Could not find local lock "
1858 					       "with cookie %u:%llu, node %u, "
1859 					       "list %u, flags 0x%x, type %d, "
1860 					       "conv %d, highest blocked %d\n",
1861 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
1862 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
1863 				     ml->node, ml->list, ml->flags, ml->type,
1864 				     ml->convert_type, ml->highest_blocked);
1865 				__dlm_print_one_lock_resource(res);
1866 				BUG();
1867 			}
1868 
1869 			if (lock->ml.node != ml->node) {
1870 				c = lock->ml.cookie;
1871 				mlog(ML_ERROR, "Mismatched node# in lock "
1872 				     "cookie %u:%llu, name %.*s, node %u\n",
1873 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
1874 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
1875 				     res->lockname.len, res->lockname.name,
1876 				     lock->ml.node);
1877 				c = ml->cookie;
1878 				mlog(ML_ERROR, "Migrate lock cookie %u:%llu, "
1879 				     "node %u, list %u, flags 0x%x, type %d, "
1880 				     "conv %d, highest blocked %d\n",
1881 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
1882 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
1883 				     ml->node, ml->list, ml->flags, ml->type,
1884 				     ml->convert_type, ml->highest_blocked);
1885 				__dlm_print_one_lock_resource(res);
1886 				BUG();
1887 			}
1888 
1889 			if (tmpq != queue) {
1890 				c = ml->cookie;
1891 				mlog(0, "Lock cookie %u:%llu was on list %u "
1892 				     "instead of list %u for %.*s\n",
1893 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
1894 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)),
1895 				     j, ml->list, res->lockname.len,
1896 				     res->lockname.name);
1897 				__dlm_print_one_lock_resource(res);
1898 				spin_unlock(&res->spinlock);
1899 				continue;
1900 			}
1901 
1902 			/* see NOTE above about why we do not update
1903 			 * to match the master here */
1904 
1905 			/* move the lock to its proper place */
1906 			/* do not alter lock refcount.  switching lists. */
1907 			list_move_tail(&lock->list, queue);
1908 			spin_unlock(&res->spinlock);
1909 
1910 			mlog(0, "just reordered a local lock!\n");
1911 			continue;
1912 		}
1913 
1914 		/* lock is for another node. */
1915 		newlock = dlm_new_lock(ml->type, ml->node,
1916 				       be64_to_cpu(ml->cookie), NULL);
1917 		if (!newlock) {
1918 			ret = -ENOMEM;
1919 			goto leave;
1920 		}
1921 		lksb = newlock->lksb;
1922 		dlm_lock_attach_lockres(newlock, res);
1923 
1924 		if (ml->convert_type != LKM_IVMODE) {
1925 			BUG_ON(queue != &res->converting);
1926 			newlock->ml.convert_type = ml->convert_type;
1927 		}
1928 		lksb->flags |= (ml->flags &
1929 				(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1930 
1931 		if (ml->type == LKM_NLMODE)
1932 			goto skip_lvb;
1933 
1934 		/*
1935 		 * If the lock is in the blocked list it can't have a valid lvb,
1936 		 * so skip it
1937 		 */
1938 		if (ml->list == DLM_BLOCKED_LIST)
1939 			goto skip_lvb;
1940 
1941 		if (!dlm_lvb_is_empty(mres->lvb)) {
1942 			if (lksb->flags & DLM_LKSB_PUT_LVB) {
1943 				/* other node was trying to update
1944 				 * lvb when node died.  recreate the
1945 				 * lksb with the updated lvb. */
1946 				memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1947 				/* the lock resource lvb update must happen
1948 				 * NOW, before the spinlock is dropped.
1949 				 * we no longer wait for the AST to update
1950 				 * the lvb. */
1951 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1952 			} else {
1953 				/* otherwise, the node is sending its
1954 				 * most recent valid lvb info */
1955 				BUG_ON(ml->type != LKM_EXMODE &&
1956 				       ml->type != LKM_PRMODE);
1957 				if (!dlm_lvb_is_empty(res->lvb) &&
1958  				    (ml->type == LKM_EXMODE ||
1959  				     memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1960  					int i;
1961  					mlog(ML_ERROR, "%s:%.*s: received bad "
1962  					     "lvb! type=%d\n", dlm->name,
1963  					     res->lockname.len,
1964  					     res->lockname.name, ml->type);
1965  					printk("lockres lvb=[");
1966  					for (i=0; i<DLM_LVB_LEN; i++)
1967  						printk("%02x", res->lvb[i]);
1968  					printk("]\nmigrated lvb=[");
1969  					for (i=0; i<DLM_LVB_LEN; i++)
1970  						printk("%02x", mres->lvb[i]);
1971  					printk("]\n");
1972  					dlm_print_one_lock_resource(res);
1973  					BUG();
1974 				}
1975 				memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1976 			}
1977 		}
1978 skip_lvb:
1979 
1980 		/* NOTE:
1981 		 * wrt lock queue ordering and recovery:
1982 		 *    1. order of locks on granted queue is
1983 		 *       meaningless.
1984 		 *    2. order of locks on converting queue is
1985 		 *       LOST with the node death.  sorry charlie.
1986 		 *    3. order of locks on the blocked queue is
1987 		 *       also LOST.
1988 		 * order of locks does not affect integrity, it
1989 		 * just means that a lock request may get pushed
1990 		 * back in line as a result of the node death.
1991 		 * also note that for a given node the lock order
1992 		 * for its secondary queue locks is preserved
1993 		 * relative to each other, but clearly *not*
1994 		 * preserved relative to locks from other nodes.
1995 		 */
1996 		bad = 0;
1997 		spin_lock(&res->spinlock);
1998 		list_for_each_entry(lock, queue, list) {
1999 			if (lock->ml.cookie == ml->cookie) {
2000 				c = lock->ml.cookie;
2001 				mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already "
2002 				     "exists on this lockres!\n", dlm->name,
2003 				     res->lockname.len, res->lockname.name,
2004 				     dlm_get_lock_cookie_node(be64_to_cpu(c)),
2005 				     dlm_get_lock_cookie_seq(be64_to_cpu(c)));
2006 
2007 				mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, "
2008 				     "node=%u, cookie=%u:%llu, queue=%d\n",
2009 	      			     ml->type, ml->convert_type, ml->node,
2010 				     dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)),
2011 				     dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)),
2012 				     ml->list);
2013 
2014 				__dlm_print_one_lock_resource(res);
2015 				bad = 1;
2016 				break;
2017 			}
2018 		}
2019 		if (!bad) {
2020 			dlm_lock_get(newlock);
2021 			if (mres->flags & DLM_MRES_RECOVERY &&
2022 					ml->list == DLM_CONVERTING_LIST &&
2023 					newlock->ml.type >
2024 					newlock->ml.convert_type) {
2025 				/* newlock is doing downconvert, add it to the
2026 				 * head of converting list */
2027 				list_add(&newlock->list, queue);
2028 			} else
2029 				list_add_tail(&newlock->list, queue);
2030 			mlog(0, "%s:%.*s: added lock for node %u, "
2031 			     "setting refmap bit\n", dlm->name,
2032 			     res->lockname.len, res->lockname.name, ml->node);
2033 			dlm_lockres_set_refmap_bit(dlm, res, ml->node);
2034 		}
2035 		spin_unlock(&res->spinlock);
2036 	}
2037 	mlog(0, "done running all the locks\n");
2038 
2039 leave:
2040 	/* balance the ref taken when the work was queued */
2041 	spin_lock(&res->spinlock);
2042 	dlm_lockres_drop_inflight_ref(dlm, res);
2043 	spin_unlock(&res->spinlock);
2044 
2045 	if (ret < 0)
2046 		mlog_errno(ret);
2047 
2048 	return ret;
2049 }
2050 
dlm_move_lockres_to_recovery_list(struct dlm_ctxt * dlm,struct dlm_lock_resource * res)2051 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
2052 				       struct dlm_lock_resource *res)
2053 {
2054 	int i;
2055 	struct list_head *queue;
2056 	struct dlm_lock *lock, *next;
2057 
2058 	assert_spin_locked(&dlm->spinlock);
2059 	assert_spin_locked(&res->spinlock);
2060 	res->state |= DLM_LOCK_RES_RECOVERING;
2061 	if (!list_empty(&res->recovering)) {
2062 		mlog(0,
2063 		     "Recovering res %s:%.*s, is already on recovery list!\n",
2064 		     dlm->name, res->lockname.len, res->lockname.name);
2065 		list_del_init(&res->recovering);
2066 		dlm_lockres_put(res);
2067 	}
2068 	/* We need to hold a reference while on the recovery list */
2069 	dlm_lockres_get(res);
2070 	list_add_tail(&res->recovering, &dlm->reco.resources);
2071 
2072 	/* find any pending locks and put them back on proper list */
2073 	for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
2074 		queue = dlm_list_idx_to_ptr(res, i);
2075 		list_for_each_entry_safe(lock, next, queue, list) {
2076 			dlm_lock_get(lock);
2077 			if (lock->convert_pending) {
2078 				/* move converting lock back to granted */
2079 				mlog(0, "node died with convert pending "
2080 				     "on %.*s. move back to granted list.\n",
2081 				     res->lockname.len, res->lockname.name);
2082 				dlm_revert_pending_convert(res, lock);
2083 				lock->convert_pending = 0;
2084 			} else if (lock->lock_pending) {
2085 				/* remove pending lock requests completely */
2086 				BUG_ON(i != DLM_BLOCKED_LIST);
2087 				mlog(0, "node died with lock pending "
2088 				     "on %.*s. remove from blocked list and skip.\n",
2089 				     res->lockname.len, res->lockname.name);
2090 				/* lock will be floating until ref in
2091 				 * dlmlock_remote is freed after the network
2092 				 * call returns.  ok for it to not be on any
2093 				 * list since no ast can be called
2094 				 * (the master is dead). */
2095 				dlm_revert_pending_lock(res, lock);
2096 				lock->lock_pending = 0;
2097 			} else if (lock->unlock_pending) {
2098 				/* if an unlock was in progress, treat as
2099 				 * if this had completed successfully
2100 				 * before sending this lock state to the
2101 				 * new master.  note that the dlm_unlock
2102 				 * call is still responsible for calling
2103 				 * the unlockast.  that will happen after
2104 				 * the network call times out.  for now,
2105 				 * just move lists to prepare the new
2106 				 * recovery master.  */
2107 				BUG_ON(i != DLM_GRANTED_LIST);
2108 				mlog(0, "node died with unlock pending "
2109 				     "on %.*s. remove from blocked list and skip.\n",
2110 				     res->lockname.len, res->lockname.name);
2111 				dlm_commit_pending_unlock(res, lock);
2112 				lock->unlock_pending = 0;
2113 			} else if (lock->cancel_pending) {
2114 				/* if a cancel was in progress, treat as
2115 				 * if this had completed successfully
2116 				 * before sending this lock state to the
2117 				 * new master */
2118 				BUG_ON(i != DLM_CONVERTING_LIST);
2119 				mlog(0, "node died with cancel pending "
2120 				     "on %.*s. move back to granted list.\n",
2121 				     res->lockname.len, res->lockname.name);
2122 				dlm_commit_pending_cancel(res, lock);
2123 				lock->cancel_pending = 0;
2124 			}
2125 			dlm_lock_put(lock);
2126 		}
2127 	}
2128 }
2129 
2130 
2131 
2132 /* removes all recovered locks from the recovery list.
2133  * sets the res->owner to the new master.
2134  * unsets the RECOVERY flag and wakes waiters. */
dlm_finish_local_lockres_recovery(struct dlm_ctxt * dlm,u8 dead_node,u8 new_master)2135 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
2136 					      u8 dead_node, u8 new_master)
2137 {
2138 	int i;
2139 	struct hlist_head *bucket;
2140 	struct dlm_lock_resource *res, *next;
2141 
2142 	assert_spin_locked(&dlm->spinlock);
2143 
2144 	list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) {
2145 		if (res->owner == dead_node) {
2146 			mlog(0, "%s: res %.*s, Changing owner from %u to %u\n",
2147 			     dlm->name, res->lockname.len, res->lockname.name,
2148 			     res->owner, new_master);
2149 			list_del_init(&res->recovering);
2150 			spin_lock(&res->spinlock);
2151 			/* new_master has our reference from
2152 			 * the lock state sent during recovery */
2153 			dlm_change_lockres_owner(dlm, res, new_master);
2154 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2155 			if (__dlm_lockres_has_locks(res))
2156 				__dlm_dirty_lockres(dlm, res);
2157 			spin_unlock(&res->spinlock);
2158 			wake_up(&res->wq);
2159 			dlm_lockres_put(res);
2160 		}
2161 	}
2162 
2163 	/* this will become unnecessary eventually, but
2164 	 * for now we need to run the whole hash, clear
2165 	 * the RECOVERING state and set the owner
2166 	 * if necessary */
2167 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2168 		bucket = dlm_lockres_hash(dlm, i);
2169 		hlist_for_each_entry(res, bucket, hash_node) {
2170 			if (res->state & DLM_LOCK_RES_RECOVERY_WAITING) {
2171 				spin_lock(&res->spinlock);
2172 				res->state &= ~DLM_LOCK_RES_RECOVERY_WAITING;
2173 				spin_unlock(&res->spinlock);
2174 				wake_up(&res->wq);
2175 			}
2176 
2177 			if (!(res->state & DLM_LOCK_RES_RECOVERING))
2178 				continue;
2179 
2180 			if (res->owner != dead_node &&
2181 			    res->owner != dlm->node_num)
2182 				continue;
2183 
2184 			if (!list_empty(&res->recovering)) {
2185 				list_del_init(&res->recovering);
2186 				dlm_lockres_put(res);
2187 			}
2188 
2189 			/* new_master has our reference from
2190 			 * the lock state sent during recovery */
2191 			mlog(0, "%s: res %.*s, Changing owner from %u to %u\n",
2192 			     dlm->name, res->lockname.len, res->lockname.name,
2193 			     res->owner, new_master);
2194 			spin_lock(&res->spinlock);
2195 			dlm_change_lockres_owner(dlm, res, new_master);
2196 			res->state &= ~DLM_LOCK_RES_RECOVERING;
2197 			if (__dlm_lockres_has_locks(res))
2198 				__dlm_dirty_lockres(dlm, res);
2199 			spin_unlock(&res->spinlock);
2200 			wake_up(&res->wq);
2201 		}
2202 	}
2203 }
2204 
dlm_lvb_needs_invalidation(struct dlm_lock * lock,int local)2205 static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
2206 {
2207 	if (local) {
2208 		if (lock->ml.type != LKM_EXMODE &&
2209 		    lock->ml.type != LKM_PRMODE)
2210 			return 1;
2211 	} else if (lock->ml.type == LKM_EXMODE)
2212 		return 1;
2213 	return 0;
2214 }
2215 
dlm_revalidate_lvb(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 dead_node)2216 static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
2217 			       struct dlm_lock_resource *res, u8 dead_node)
2218 {
2219 	struct list_head *queue;
2220 	struct dlm_lock *lock;
2221 	int blank_lvb = 0, local = 0;
2222 	int i;
2223 	u8 search_node;
2224 
2225 	assert_spin_locked(&dlm->spinlock);
2226 	assert_spin_locked(&res->spinlock);
2227 
2228 	if (res->owner == dlm->node_num)
2229 		/* if this node owned the lockres, and if the dead node
2230 		 * had an EX when he died, blank out the lvb */
2231 		search_node = dead_node;
2232 	else {
2233 		/* if this is a secondary lockres, and we had no EX or PR
2234 		 * locks granted, we can no longer trust the lvb */
2235 		search_node = dlm->node_num;
2236 		local = 1;  /* check local state for valid lvb */
2237 	}
2238 
2239 	for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
2240 		queue = dlm_list_idx_to_ptr(res, i);
2241 		list_for_each_entry(lock, queue, list) {
2242 			if (lock->ml.node == search_node) {
2243 				if (dlm_lvb_needs_invalidation(lock, local)) {
2244 					/* zero the lksb lvb and lockres lvb */
2245 					blank_lvb = 1;
2246 					memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
2247 				}
2248 			}
2249 		}
2250 	}
2251 
2252 	if (blank_lvb) {
2253 		mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
2254 		     res->lockname.len, res->lockname.name, dead_node);
2255 		memset(res->lvb, 0, DLM_LVB_LEN);
2256 	}
2257 }
2258 
dlm_free_dead_locks(struct dlm_ctxt * dlm,struct dlm_lock_resource * res,u8 dead_node)2259 static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
2260 				struct dlm_lock_resource *res, u8 dead_node)
2261 {
2262 	struct dlm_lock *lock, *next;
2263 	unsigned int freed = 0;
2264 
2265 	/* this node is the lockres master:
2266 	 * 1) remove any stale locks for the dead node
2267 	 * 2) if the dead node had an EX when he died, blank out the lvb
2268 	 */
2269 	assert_spin_locked(&dlm->spinlock);
2270 	assert_spin_locked(&res->spinlock);
2271 
2272 	/* We do two dlm_lock_put(). One for removing from list and the other is
2273 	 * to force the DLM_UNLOCK_FREE_LOCK action so as to free the locks */
2274 
2275 	/* TODO: check pending_asts, pending_basts here */
2276 	list_for_each_entry_safe(lock, next, &res->granted, list) {
2277 		if (lock->ml.node == dead_node) {
2278 			list_del_init(&lock->list);
2279 			dlm_lock_put(lock);
2280 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
2281 			dlm_lock_put(lock);
2282 			freed++;
2283 		}
2284 	}
2285 	list_for_each_entry_safe(lock, next, &res->converting, list) {
2286 		if (lock->ml.node == dead_node) {
2287 			list_del_init(&lock->list);
2288 			dlm_lock_put(lock);
2289 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
2290 			dlm_lock_put(lock);
2291 			freed++;
2292 		}
2293 	}
2294 	list_for_each_entry_safe(lock, next, &res->blocked, list) {
2295 		if (lock->ml.node == dead_node) {
2296 			list_del_init(&lock->list);
2297 			dlm_lock_put(lock);
2298 			/* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */
2299 			dlm_lock_put(lock);
2300 			freed++;
2301 		}
2302 	}
2303 
2304 	if (freed) {
2305 		mlog(0, "%s:%.*s: freed %u locks for dead node %u, "
2306 		     "dropping ref from lockres\n", dlm->name,
2307 		     res->lockname.len, res->lockname.name, freed, dead_node);
2308 		if(!test_bit(dead_node, res->refmap)) {
2309 			mlog(ML_ERROR, "%s:%.*s: freed %u locks for dead node %u, "
2310 			     "but ref was not set\n", dlm->name,
2311 			     res->lockname.len, res->lockname.name, freed, dead_node);
2312 			__dlm_print_one_lock_resource(res);
2313 		}
2314 		res->state |= DLM_LOCK_RES_RECOVERY_WAITING;
2315 		dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2316 	} else if (test_bit(dead_node, res->refmap)) {
2317 		mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2318 		     "no locks and had not purged before dying\n", dlm->name,
2319 		     res->lockname.len, res->lockname.name, dead_node);
2320 		dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2321 	}
2322 
2323 	/* do not kick thread yet */
2324 	__dlm_dirty_lockres(dlm, res);
2325 }
2326 
dlm_do_local_recovery_cleanup(struct dlm_ctxt * dlm,u8 dead_node)2327 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
2328 {
2329 	struct dlm_lock_resource *res;
2330 	int i;
2331 	struct hlist_head *bucket;
2332 	struct hlist_node *tmp;
2333 	struct dlm_lock *lock;
2334 
2335 
2336 	/* purge any stale mles */
2337 	dlm_clean_master_list(dlm, dead_node);
2338 
2339 	/*
2340 	 * now clean up all lock resources.  there are two rules:
2341 	 *
2342 	 * 1) if the dead node was the master, move the lockres
2343 	 *    to the recovering list.  set the RECOVERING flag.
2344 	 *    this lockres needs to be cleaned up before it can
2345 	 *    be used further.
2346 	 *
2347 	 * 2) if this node was the master, remove all locks from
2348 	 *    each of the lockres queues that were owned by the
2349 	 *    dead node.  once recovery finishes, the dlm thread
2350 	 *    can be kicked again to see if any ASTs or BASTs
2351 	 *    need to be fired as a result.
2352 	 */
2353 	for (i = 0; i < DLM_HASH_BUCKETS; i++) {
2354 		bucket = dlm_lockres_hash(dlm, i);
2355 		hlist_for_each_entry_safe(res, tmp, bucket, hash_node) {
2356  			/* always prune any $RECOVERY entries for dead nodes,
2357  			 * otherwise hangs can occur during later recovery */
2358 			if (dlm_is_recovery_lock(res->lockname.name,
2359 						 res->lockname.len)) {
2360 				spin_lock(&res->spinlock);
2361 				list_for_each_entry(lock, &res->granted, list) {
2362 					if (lock->ml.node == dead_node) {
2363 						mlog(0, "AHA! there was "
2364 						     "a $RECOVERY lock for dead "
2365 						     "node %u (%s)!\n",
2366 						     dead_node, dlm->name);
2367 						list_del_init(&lock->list);
2368 						dlm_lock_put(lock);
2369 						/* Can't schedule
2370 						 * DLM_UNLOCK_FREE_LOCK
2371 						 * - do manually */
2372 						dlm_lock_put(lock);
2373 						break;
2374 					}
2375 				}
2376 
2377 				if ((res->owner == dead_node) &&
2378 							(res->state & DLM_LOCK_RES_DROPPING_REF)) {
2379 					dlm_lockres_get(res);
2380 					__dlm_do_purge_lockres(dlm, res);
2381 					spin_unlock(&res->spinlock);
2382 					wake_up(&res->wq);
2383 					dlm_lockres_put(res);
2384 					continue;
2385 				} else if (res->owner == dlm->node_num)
2386 					dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2387 				spin_unlock(&res->spinlock);
2388 				continue;
2389 			}
2390 			spin_lock(&res->spinlock);
2391 			/* zero the lvb if necessary */
2392 			dlm_revalidate_lvb(dlm, res, dead_node);
2393 			if (res->owner == dead_node) {
2394 				if (res->state & DLM_LOCK_RES_DROPPING_REF) {
2395 					mlog(0, "%s:%.*s: owned by "
2396 						"dead node %u, this node was "
2397 						"dropping its ref when master died. "
2398 						"continue, purging the lockres.\n",
2399 						dlm->name, res->lockname.len,
2400 						res->lockname.name, dead_node);
2401 					dlm_lockres_get(res);
2402 					__dlm_do_purge_lockres(dlm, res);
2403 					spin_unlock(&res->spinlock);
2404 					wake_up(&res->wq);
2405 					dlm_lockres_put(res);
2406 					continue;
2407 				}
2408 				dlm_move_lockres_to_recovery_list(dlm, res);
2409 			} else if (res->owner == dlm->node_num) {
2410 				dlm_free_dead_locks(dlm, res, dead_node);
2411 				__dlm_lockres_calc_usage(dlm, res);
2412 			} else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2413 				if (test_bit(dead_node, res->refmap)) {
2414 					mlog(0, "%s:%.*s: dead node %u had a ref, but had "
2415 						"no locks and had not purged before dying\n",
2416 						dlm->name, res->lockname.len,
2417 						res->lockname.name, dead_node);
2418 					dlm_lockres_clear_refmap_bit(dlm, res, dead_node);
2419 				}
2420 			}
2421 			spin_unlock(&res->spinlock);
2422 		}
2423 	}
2424 
2425 }
2426 
__dlm_hb_node_down(struct dlm_ctxt * dlm,int idx)2427 static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
2428 {
2429 	assert_spin_locked(&dlm->spinlock);
2430 
2431 	if (dlm->reco.new_master == idx) {
2432 		mlog(0, "%s: recovery master %d just died\n",
2433 		     dlm->name, idx);
2434 		if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2435 			/* finalize1 was reached, so it is safe to clear
2436 			 * the new_master and dead_node.  that recovery
2437 			 * is complete. */
2438 			mlog(0, "%s: dead master %d had reached "
2439 			     "finalize1 state, clearing\n", dlm->name, idx);
2440 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2441 			__dlm_reset_recovery(dlm);
2442 		}
2443 	}
2444 
2445 	/* Clean up join state on node death. */
2446 	if (dlm->joining_node == idx) {
2447 		mlog(0, "Clearing join state for node %u\n", idx);
2448 		__dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
2449 	}
2450 
2451 	/* check to see if the node is already considered dead */
2452 	if (!test_bit(idx, dlm->live_nodes_map)) {
2453 		mlog(0, "for domain %s, node %d is already dead. "
2454 		     "another node likely did recovery already.\n",
2455 		     dlm->name, idx);
2456 		return;
2457 	}
2458 
2459 	/* check to see if we do not care about this node */
2460 	if (!test_bit(idx, dlm->domain_map)) {
2461 		/* This also catches the case that we get a node down
2462 		 * but haven't joined the domain yet. */
2463 		mlog(0, "node %u already removed from domain!\n", idx);
2464 		return;
2465 	}
2466 
2467 	clear_bit(idx, dlm->live_nodes_map);
2468 
2469 	/* make sure local cleanup occurs before the heartbeat events */
2470 	if (!test_bit(idx, dlm->recovery_map))
2471 		dlm_do_local_recovery_cleanup(dlm, idx);
2472 
2473 	/* notify anything attached to the heartbeat events */
2474 	dlm_hb_event_notify_attached(dlm, idx, 0);
2475 
2476 	mlog(0, "node %u being removed from domain map!\n", idx);
2477 	clear_bit(idx, dlm->domain_map);
2478 	clear_bit(idx, dlm->exit_domain_map);
2479 	/* wake up migration waiters if a node goes down.
2480 	 * perhaps later we can genericize this for other waiters. */
2481 	wake_up(&dlm->migration_wq);
2482 
2483 	set_bit(idx, dlm->recovery_map);
2484 }
2485 
dlm_hb_node_down_cb(struct o2nm_node * node,int idx,void * data)2486 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
2487 {
2488 	struct dlm_ctxt *dlm = data;
2489 
2490 	if (!dlm_grab(dlm))
2491 		return;
2492 
2493 	/*
2494 	 * This will notify any dlm users that a node in our domain
2495 	 * went away without notifying us first.
2496 	 */
2497 	if (test_bit(idx, dlm->domain_map))
2498 		dlm_fire_domain_eviction_callbacks(dlm, idx);
2499 
2500 	spin_lock(&dlm->spinlock);
2501 	__dlm_hb_node_down(dlm, idx);
2502 	spin_unlock(&dlm->spinlock);
2503 
2504 	dlm_put(dlm);
2505 }
2506 
dlm_hb_node_up_cb(struct o2nm_node * node,int idx,void * data)2507 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
2508 {
2509 	struct dlm_ctxt *dlm = data;
2510 
2511 	if (!dlm_grab(dlm))
2512 		return;
2513 
2514 	spin_lock(&dlm->spinlock);
2515 	set_bit(idx, dlm->live_nodes_map);
2516 	/* do NOT notify mle attached to the heartbeat events.
2517 	 * new nodes are not interesting in mastery until joined. */
2518 	spin_unlock(&dlm->spinlock);
2519 
2520 	dlm_put(dlm);
2521 }
2522 
dlm_reco_ast(void * astdata)2523 static void dlm_reco_ast(void *astdata)
2524 {
2525 	struct dlm_ctxt *dlm = astdata;
2526 	mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
2527 	     dlm->node_num, dlm->name);
2528 }
dlm_reco_bast(void * astdata,int blocked_type)2529 static void dlm_reco_bast(void *astdata, int blocked_type)
2530 {
2531 	struct dlm_ctxt *dlm = astdata;
2532 	mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
2533 	     dlm->node_num, dlm->name);
2534 }
dlm_reco_unlock_ast(void * astdata,enum dlm_status st)2535 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
2536 {
2537 	mlog(0, "unlockast for recovery lock fired!\n");
2538 }
2539 
2540 /*
2541  * dlm_pick_recovery_master will continually attempt to use
2542  * dlmlock() on the special "$RECOVERY" lockres with the
2543  * LKM_NOQUEUE flag to get an EX.  every thread that enters
2544  * this function on each node racing to become the recovery
2545  * master will not stop attempting this until either:
2546  * a) this node gets the EX (and becomes the recovery master),
2547  * or b) dlm->reco.new_master gets set to some nodenum
2548  * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2549  * so each time a recovery master is needed, the entire cluster
2550  * will sync at this point.  if the new master dies, that will
2551  * be detected in dlm_do_recovery */
dlm_pick_recovery_master(struct dlm_ctxt * dlm)2552 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2553 {
2554 	enum dlm_status ret;
2555 	struct dlm_lockstatus lksb;
2556 	int status = -EINVAL;
2557 
2558 	mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2559 	     dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2560 again:
2561 	memset(&lksb, 0, sizeof(lksb));
2562 
2563 	ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2564 		      DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN,
2565 		      dlm_reco_ast, dlm, dlm_reco_bast);
2566 
2567 	mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2568 	     dlm->name, ret, lksb.status);
2569 
2570 	if (ret == DLM_NORMAL) {
2571 		mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2572 		     dlm->name, dlm->node_num);
2573 
2574 		/* got the EX lock.  check to see if another node
2575 		 * just became the reco master */
2576 		if (dlm_reco_master_ready(dlm)) {
2577 			mlog(0, "%s: got reco EX lock, but %u will "
2578 			     "do the recovery\n", dlm->name,
2579 			     dlm->reco.new_master);
2580 			status = -EEXIST;
2581 		} else {
2582 			status = 0;
2583 
2584 			/* see if recovery was already finished elsewhere */
2585 			spin_lock(&dlm->spinlock);
2586 			if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
2587 				status = -EINVAL;
2588 				mlog(0, "%s: got reco EX lock, but "
2589 				     "node got recovered already\n", dlm->name);
2590 				if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2591 					mlog(ML_ERROR, "%s: new master is %u "
2592 					     "but no dead node!\n",
2593 					     dlm->name, dlm->reco.new_master);
2594 					BUG();
2595 				}
2596 			}
2597 			spin_unlock(&dlm->spinlock);
2598 		}
2599 
2600 		/* if this node has actually become the recovery master,
2601 		 * set the master and send the messages to begin recovery */
2602 		if (!status) {
2603 			mlog(0, "%s: dead=%u, this=%u, sending "
2604 			     "begin_reco now\n", dlm->name,
2605 			     dlm->reco.dead_node, dlm->node_num);
2606 			status = dlm_send_begin_reco_message(dlm,
2607 				      dlm->reco.dead_node);
2608 			/* this always succeeds */
2609 			BUG_ON(status);
2610 
2611 			/* set the new_master to this node */
2612 			spin_lock(&dlm->spinlock);
2613 			dlm_set_reco_master(dlm, dlm->node_num);
2614 			spin_unlock(&dlm->spinlock);
2615 		}
2616 
2617 		/* recovery lock is a special case.  ast will not get fired,
2618 		 * so just go ahead and unlock it. */
2619 		ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2620 		if (ret == DLM_DENIED) {
2621 			mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2622 			ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2623 		}
2624 		if (ret != DLM_NORMAL) {
2625 			/* this would really suck. this could only happen
2626 			 * if there was a network error during the unlock
2627 			 * because of node death.  this means the unlock
2628 			 * is actually "done" and the lock structure is
2629 			 * even freed.  we can continue, but only
2630 			 * because this specific lock name is special. */
2631 			mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
2632 		}
2633 	} else if (ret == DLM_NOTQUEUED) {
2634 		mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2635 		     dlm->name, dlm->node_num);
2636 		/* another node is master. wait on
2637 		 * reco.new_master != O2NM_INVALID_NODE_NUM
2638 		 * for at most one second */
2639 		wait_event_timeout(dlm->dlm_reco_thread_wq,
2640 					 dlm_reco_master_ready(dlm),
2641 					 msecs_to_jiffies(1000));
2642 		if (!dlm_reco_master_ready(dlm)) {
2643 			mlog(0, "%s: reco master taking a while\n",
2644 			     dlm->name);
2645 			goto again;
2646 		}
2647 		/* another node has informed this one that it is reco master */
2648 		mlog(0, "%s: reco master %u is ready to recover %u\n",
2649 		     dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
2650 		status = -EEXIST;
2651 	} else if (ret == DLM_RECOVERING) {
2652 		mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n",
2653 		     dlm->name, dlm->node_num);
2654 		goto again;
2655 	} else {
2656 		struct dlm_lock_resource *res;
2657 
2658 		/* dlmlock returned something other than NOTQUEUED or NORMAL */
2659 		mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2660 		     "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2661 		     dlm_errname(lksb.status));
2662 		res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2663 					 DLM_RECOVERY_LOCK_NAME_LEN);
2664 		if (res) {
2665 			dlm_print_one_lock_resource(res);
2666 			dlm_lockres_put(res);
2667 		} else {
2668 			mlog(ML_ERROR, "recovery lock not found\n");
2669 		}
2670 		BUG();
2671 	}
2672 
2673 	return status;
2674 }
2675 
dlm_send_begin_reco_message(struct dlm_ctxt * dlm,u8 dead_node)2676 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2677 {
2678 	struct dlm_begin_reco br;
2679 	int ret = 0;
2680 	struct dlm_node_iter iter;
2681 	int nodenum;
2682 	int status;
2683 
2684 	mlog(0, "%s: dead node is %u\n", dlm->name, dead_node);
2685 
2686 	spin_lock(&dlm->spinlock);
2687 	dlm_node_iter_init(dlm->domain_map, &iter);
2688 	spin_unlock(&dlm->spinlock);
2689 
2690 	clear_bit(dead_node, iter.node_map);
2691 
2692 	memset(&br, 0, sizeof(br));
2693 	br.node_idx = dlm->node_num;
2694 	br.dead_node = dead_node;
2695 
2696 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2697 		ret = 0;
2698 		if (nodenum == dead_node) {
2699 			mlog(0, "not sending begin reco to dead node "
2700 				  "%u\n", dead_node);
2701 			continue;
2702 		}
2703 		if (nodenum == dlm->node_num) {
2704 			mlog(0, "not sending begin reco to self\n");
2705 			continue;
2706 		}
2707 retry:
2708 		mlog(0, "attempting to send begin reco msg to %d\n",
2709 			  nodenum);
2710 		ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2711 					 &br, sizeof(br), nodenum, &status);
2712 		/* negative status is handled ok by caller here */
2713 		if (ret >= 0)
2714 			ret = status;
2715 		if (dlm_is_host_down(ret)) {
2716 			/* node is down.  not involved in recovery
2717 			 * so just keep going */
2718 			mlog(ML_NOTICE, "%s: node %u was down when sending "
2719 			     "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2720 			ret = 0;
2721 		}
2722 
2723 		/*
2724 		 * Prior to commit aad1b15310b9bcd59fa81ab8f2b1513b59553ea8,
2725 		 * dlm_begin_reco_handler() returned EAGAIN and not -EAGAIN.
2726 		 * We are handling both for compatibility reasons.
2727 		 */
2728 		if (ret == -EAGAIN || ret == EAGAIN) {
2729 			mlog(0, "%s: trying to start recovery of node "
2730 			     "%u, but node %u is waiting for last recovery "
2731 			     "to complete, backoff for a bit\n", dlm->name,
2732 			     dead_node, nodenum);
2733 			msleep(100);
2734 			goto retry;
2735 		}
2736 		if (ret < 0) {
2737 			struct dlm_lock_resource *res;
2738 
2739 			/* this is now a serious problem, possibly ENOMEM
2740 			 * in the network stack.  must retry */
2741 			mlog_errno(ret);
2742 			mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2743 			     "returned %d\n", dlm->name, nodenum, ret);
2744 			res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2745 						 DLM_RECOVERY_LOCK_NAME_LEN);
2746 			if (res) {
2747 				dlm_print_one_lock_resource(res);
2748 				dlm_lockres_put(res);
2749 			} else {
2750 				mlog(ML_ERROR, "recovery lock not found\n");
2751 			}
2752 			/* sleep for a bit in hopes that we can avoid
2753 			 * another ENOMEM */
2754 			msleep(100);
2755 			goto retry;
2756 		}
2757 	}
2758 
2759 	return ret;
2760 }
2761 
dlm_begin_reco_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)2762 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2763 			   void **ret_data)
2764 {
2765 	struct dlm_ctxt *dlm = data;
2766 	struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2767 
2768 	/* ok to return 0, domain has gone away */
2769 	if (!dlm_grab(dlm))
2770 		return 0;
2771 
2772 	spin_lock(&dlm->spinlock);
2773 	if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2774 		mlog(0, "%s: node %u wants to recover node %u (%u:%u) "
2775 		     "but this node is in finalize state, waiting on finalize2\n",
2776 		     dlm->name, br->node_idx, br->dead_node,
2777 		     dlm->reco.dead_node, dlm->reco.new_master);
2778 		spin_unlock(&dlm->spinlock);
2779 		dlm_put(dlm);
2780 		return -EAGAIN;
2781 	}
2782 	spin_unlock(&dlm->spinlock);
2783 
2784 	mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n",
2785 	     dlm->name, br->node_idx, br->dead_node,
2786 	     dlm->reco.dead_node, dlm->reco.new_master);
2787 
2788 	dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2789 
2790 	spin_lock(&dlm->spinlock);
2791 	if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2792 		if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2793 			mlog(0, "%s: new_master %u died, changing "
2794 			     "to %u\n", dlm->name, dlm->reco.new_master,
2795 			     br->node_idx);
2796 		} else {
2797 			mlog(0, "%s: new_master %u NOT DEAD, changing "
2798 			     "to %u\n", dlm->name, dlm->reco.new_master,
2799 			     br->node_idx);
2800 			/* may not have seen the new master as dead yet */
2801 		}
2802 	}
2803 	if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2804 		mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2805 		     "node %u changing it to %u\n", dlm->name,
2806 		     dlm->reco.dead_node, br->node_idx, br->dead_node);
2807 	}
2808 	dlm_set_reco_master(dlm, br->node_idx);
2809 	dlm_set_reco_dead_node(dlm, br->dead_node);
2810 	if (!test_bit(br->dead_node, dlm->recovery_map)) {
2811 		mlog(0, "recovery master %u sees %u as dead, but this "
2812 		     "node has not yet.  marking %u as dead\n",
2813 		     br->node_idx, br->dead_node, br->dead_node);
2814 		if (!test_bit(br->dead_node, dlm->domain_map) ||
2815 		    !test_bit(br->dead_node, dlm->live_nodes_map))
2816 			mlog(0, "%u not in domain/live_nodes map "
2817 			     "so setting it in reco map manually\n",
2818 			     br->dead_node);
2819 		/* force the recovery cleanup in __dlm_hb_node_down
2820 		 * both of these will be cleared in a moment */
2821 		set_bit(br->dead_node, dlm->domain_map);
2822 		set_bit(br->dead_node, dlm->live_nodes_map);
2823 		__dlm_hb_node_down(dlm, br->dead_node);
2824 	}
2825 	spin_unlock(&dlm->spinlock);
2826 
2827 	dlm_kick_recovery_thread(dlm);
2828 
2829 	mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n",
2830 	     dlm->name, br->node_idx, br->dead_node,
2831 	     dlm->reco.dead_node, dlm->reco.new_master);
2832 
2833 	dlm_put(dlm);
2834 	return 0;
2835 }
2836 
2837 #define DLM_FINALIZE_STAGE2  0x01
dlm_send_finalize_reco_message(struct dlm_ctxt * dlm)2838 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2839 {
2840 	int ret = 0;
2841 	struct dlm_finalize_reco fr;
2842 	struct dlm_node_iter iter;
2843 	int nodenum;
2844 	int status;
2845 	int stage = 1;
2846 
2847 	mlog(0, "finishing recovery for node %s:%u, "
2848 	     "stage %d\n", dlm->name, dlm->reco.dead_node, stage);
2849 
2850 	spin_lock(&dlm->spinlock);
2851 	dlm_node_iter_init(dlm->domain_map, &iter);
2852 	spin_unlock(&dlm->spinlock);
2853 
2854 stage2:
2855 	memset(&fr, 0, sizeof(fr));
2856 	fr.node_idx = dlm->node_num;
2857 	fr.dead_node = dlm->reco.dead_node;
2858 	if (stage == 2)
2859 		fr.flags |= DLM_FINALIZE_STAGE2;
2860 
2861 	while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2862 		if (nodenum == dlm->node_num)
2863 			continue;
2864 		ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2865 					 &fr, sizeof(fr), nodenum, &status);
2866 		if (ret >= 0)
2867 			ret = status;
2868 		if (ret < 0) {
2869 			mlog(ML_ERROR, "Error %d when sending message %u (key "
2870 			     "0x%x) to node %u\n", ret, DLM_FINALIZE_RECO_MSG,
2871 			     dlm->key, nodenum);
2872 			if (dlm_is_host_down(ret)) {
2873 				/* this has no effect on this recovery
2874 				 * session, so set the status to zero to
2875 				 * finish out the last recovery */
2876 				mlog(ML_ERROR, "node %u went down after this "
2877 				     "node finished recovery.\n", nodenum);
2878 				ret = 0;
2879 				continue;
2880 			}
2881 			break;
2882 		}
2883 	}
2884 	if (stage == 1) {
2885 		/* reset the node_iter back to the top and send finalize2 */
2886 		iter.curnode = -1;
2887 		stage = 2;
2888 		goto stage2;
2889 	}
2890 
2891 	return ret;
2892 }
2893 
dlm_finalize_reco_handler(struct o2net_msg * msg,u32 len,void * data,void ** ret_data)2894 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
2895 			      void **ret_data)
2896 {
2897 	struct dlm_ctxt *dlm = data;
2898 	struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2899 	int stage = 1;
2900 
2901 	/* ok to return 0, domain has gone away */
2902 	if (!dlm_grab(dlm))
2903 		return 0;
2904 
2905 	if (fr->flags & DLM_FINALIZE_STAGE2)
2906 		stage = 2;
2907 
2908 	mlog(0, "%s: node %u finalizing recovery stage%d of "
2909 	     "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage,
2910 	     fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master);
2911 
2912 	spin_lock(&dlm->spinlock);
2913 
2914 	if (dlm->reco.new_master != fr->node_idx) {
2915 		mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2916 		     "%u is supposed to be the new master, dead=%u\n",
2917 		     fr->node_idx, dlm->reco.new_master, fr->dead_node);
2918 		BUG();
2919 	}
2920 	if (dlm->reco.dead_node != fr->dead_node) {
2921 		mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2922 		     "node %u, but node %u is supposed to be dead\n",
2923 		     fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2924 		BUG();
2925 	}
2926 
2927 	switch (stage) {
2928 		case 1:
2929 			dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2930 			if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) {
2931 				mlog(ML_ERROR, "%s: received finalize1 from "
2932 				     "new master %u for dead node %u, but "
2933 				     "this node has already received it!\n",
2934 				     dlm->name, fr->node_idx, fr->dead_node);
2935 				dlm_print_reco_node_status(dlm);
2936 				BUG();
2937 			}
2938 			dlm->reco.state |= DLM_RECO_STATE_FINALIZE;
2939 			spin_unlock(&dlm->spinlock);
2940 			break;
2941 		case 2:
2942 			if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) {
2943 				mlog(ML_ERROR, "%s: received finalize2 from "
2944 				     "new master %u for dead node %u, but "
2945 				     "this node did not have finalize1!\n",
2946 				     dlm->name, fr->node_idx, fr->dead_node);
2947 				dlm_print_reco_node_status(dlm);
2948 				BUG();
2949 			}
2950 			dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE;
2951 			__dlm_reset_recovery(dlm);
2952 			spin_unlock(&dlm->spinlock);
2953 			dlm_kick_recovery_thread(dlm);
2954 			break;
2955 	}
2956 
2957 	mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n",
2958 	     dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master);
2959 
2960 	dlm_put(dlm);
2961 	return 0;
2962 }
2963