[PATCH] fs/ocfs2/dlm/dlmrecovery.c must #include <linux/delay.h>
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / dlm / dlmrecovery.c
blob186e9a76aa5807565ad09e31e0bd0fa7584dcab5
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmrecovery.c
6 * recovery stuff
8 * Copyright (C) 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/highmem.h>
33 #include <linux/utsname.h>
34 #include <linux/init.h>
35 #include <linux/sysctl.h>
36 #include <linux/random.h>
37 #include <linux/blkdev.h>
38 #include <linux/socket.h>
39 #include <linux/inet.h>
40 #include <linux/timer.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
45 #include "cluster/heartbeat.h"
46 #include "cluster/nodemanager.h"
47 #include "cluster/tcp.h"
49 #include "dlmapi.h"
50 #include "dlmcommon.h"
51 #include "dlmdomain.h"
53 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
54 #include "cluster/masklog.h"
56 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
58 static int dlm_recovery_thread(void *data);
59 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
60 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
61 static void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
62 static int dlm_do_recovery(struct dlm_ctxt *dlm);
64 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
65 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
66 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
67 static int dlm_request_all_locks(struct dlm_ctxt *dlm,
68 u8 request_from, u8 dead_node);
69 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
71 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
72 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
73 const char *lockname, int namelen,
74 int total_locks, u64 cookie,
75 u8 flags, u8 master);
76 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
77 struct dlm_migratable_lockres *mres,
78 u8 send_to,
79 struct dlm_lock_resource *res,
80 int total_locks);
81 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
82 struct dlm_lock_resource *res,
83 u8 *real_master);
84 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
85 struct dlm_lock_resource *res,
86 struct dlm_migratable_lockres *mres);
87 static int dlm_do_master_requery(struct dlm_ctxt *dlm,
88 struct dlm_lock_resource *res,
89 u8 nodenum, u8 *real_master);
90 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
91 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
92 u8 dead_node, u8 send_to);
93 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
94 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
95 struct list_head *list, u8 dead_node);
96 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
97 u8 dead_node, u8 new_master);
98 static void dlm_reco_ast(void *astdata);
99 static void dlm_reco_bast(void *astdata, int blocked_type);
100 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
101 static void dlm_request_all_locks_worker(struct dlm_work_item *item,
102 void *data);
103 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
105 static u64 dlm_get_next_mig_cookie(void);
107 static spinlock_t dlm_reco_state_lock = SPIN_LOCK_UNLOCKED;
108 static spinlock_t dlm_mig_cookie_lock = SPIN_LOCK_UNLOCKED;
109 static u64 dlm_mig_cookie = 1;
111 static u64 dlm_get_next_mig_cookie(void)
113 u64 c;
114 spin_lock(&dlm_mig_cookie_lock);
115 c = dlm_mig_cookie;
116 if (dlm_mig_cookie == (~0ULL))
117 dlm_mig_cookie = 1;
118 else
119 dlm_mig_cookie++;
120 spin_unlock(&dlm_mig_cookie_lock);
121 return c;
124 static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
126 spin_lock(&dlm->spinlock);
127 clear_bit(dlm->reco.dead_node, dlm->recovery_map);
128 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
129 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
130 spin_unlock(&dlm->spinlock);
133 /* Worker function used during recovery. */
134 void dlm_dispatch_work(void *data)
136 struct dlm_ctxt *dlm = (struct dlm_ctxt *)data;
137 LIST_HEAD(tmp_list);
138 struct list_head *iter, *iter2;
139 struct dlm_work_item *item;
140 dlm_workfunc_t *workfunc;
142 spin_lock(&dlm->work_lock);
143 list_splice_init(&dlm->work_list, &tmp_list);
144 spin_unlock(&dlm->work_lock);
146 list_for_each_safe(iter, iter2, &tmp_list) {
147 item = list_entry(iter, struct dlm_work_item, list);
148 workfunc = item->func;
149 list_del_init(&item->list);
151 /* already have ref on dlm to avoid having
152 * it disappear. just double-check. */
153 BUG_ON(item->dlm != dlm);
155 /* this is allowed to sleep and
156 * call network stuff */
157 workfunc(item, item->data);
159 dlm_put(dlm);
160 kfree(item);
165 * RECOVERY THREAD
168 static void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
170 /* wake the recovery thread
171 * this will wake the reco thread in one of three places
172 * 1) sleeping with no recovery happening
173 * 2) sleeping with recovery mastered elsewhere
174 * 3) recovery mastered here, waiting on reco data */
176 wake_up(&dlm->dlm_reco_thread_wq);
179 /* Launch the recovery thread */
180 int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
182 mlog(0, "starting dlm recovery thread...\n");
184 dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
185 "dlm_reco_thread");
186 if (IS_ERR(dlm->dlm_reco_thread_task)) {
187 mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
188 dlm->dlm_reco_thread_task = NULL;
189 return -EINVAL;
192 return 0;
195 void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
197 if (dlm->dlm_reco_thread_task) {
198 mlog(0, "waiting for dlm recovery thread to exit\n");
199 kthread_stop(dlm->dlm_reco_thread_task);
200 dlm->dlm_reco_thread_task = NULL;
207 * this is lame, but here's how recovery works...
208 * 1) all recovery threads cluster wide will work on recovering
209 * ONE node at a time
210 * 2) negotiate who will take over all the locks for the dead node.
211 * thats right... ALL the locks.
212 * 3) once a new master is chosen, everyone scans all locks
213 * and moves aside those mastered by the dead guy
214 * 4) each of these locks should be locked until recovery is done
215 * 5) the new master collects up all of secondary lock queue info
216 * one lock at a time, forcing each node to communicate back
217 * before continuing
218 * 6) each secondary lock queue responds with the full known lock info
219 * 7) once the new master has run all its locks, it sends a ALLDONE!
220 * message to everyone
221 * 8) upon receiving this message, the secondary queue node unlocks
222 * and responds to the ALLDONE
223 * 9) once the new master gets responses from everyone, he unlocks
224 * everything and recovery for this dead node is done
225 *10) go back to 2) while there are still dead nodes
230 #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
232 static int dlm_recovery_thread(void *data)
234 int status;
235 struct dlm_ctxt *dlm = data;
236 unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
238 mlog(0, "dlm thread running for %s...\n", dlm->name);
240 while (!kthread_should_stop()) {
241 if (dlm_joined(dlm)) {
242 status = dlm_do_recovery(dlm);
243 if (status == -EAGAIN) {
244 /* do not sleep, recheck immediately. */
245 continue;
247 if (status < 0)
248 mlog_errno(status);
251 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
252 kthread_should_stop(),
253 timeout);
256 mlog(0, "quitting DLM recovery thread\n");
257 return 0;
260 /* returns true when the recovery master has contacted us */
261 static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
263 int ready;
264 spin_lock(&dlm->spinlock);
265 ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
266 spin_unlock(&dlm->spinlock);
267 return ready;
270 /* returns true if node is no longer in the domain
271 * could be dead or just not joined */
272 int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
274 int dead;
275 spin_lock(&dlm->spinlock);
276 dead = test_bit(node, dlm->domain_map);
277 spin_unlock(&dlm->spinlock);
278 return dead;
281 /* callers of the top-level api calls (dlmlock/dlmunlock) should
282 * block on the dlm->reco.event when recovery is in progress.
283 * the dlm recovery thread will set this state when it begins
284 * recovering a dead node (as the new master or not) and clear
285 * the state and wake as soon as all affected lock resources have
286 * been marked with the RECOVERY flag */
287 static int dlm_in_recovery(struct dlm_ctxt *dlm)
289 int in_recovery;
290 spin_lock(&dlm->spinlock);
291 in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
292 spin_unlock(&dlm->spinlock);
293 return in_recovery;
297 void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
299 wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
302 static void dlm_begin_recovery(struct dlm_ctxt *dlm)
304 spin_lock(&dlm->spinlock);
305 BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
306 dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
307 spin_unlock(&dlm->spinlock);
310 static void dlm_end_recovery(struct dlm_ctxt *dlm)
312 spin_lock(&dlm->spinlock);
313 BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
314 dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
315 spin_unlock(&dlm->spinlock);
316 wake_up(&dlm->reco.event);
319 static int dlm_do_recovery(struct dlm_ctxt *dlm)
321 int status = 0;
322 int ret;
324 spin_lock(&dlm->spinlock);
326 /* check to see if the new master has died */
327 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
328 test_bit(dlm->reco.new_master, dlm->recovery_map)) {
329 mlog(0, "new master %u died while recovering %u!\n",
330 dlm->reco.new_master, dlm->reco.dead_node);
331 /* unset the new_master, leave dead_node */
332 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
335 /* select a target to recover */
336 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
337 int bit;
339 bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
340 if (bit >= O2NM_MAX_NODES || bit < 0)
341 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
342 else
343 dlm->reco.dead_node = bit;
344 } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
345 /* BUG? */
346 mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
347 dlm->reco.dead_node);
348 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
351 if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
352 // mlog(0, "nothing to recover! sleeping now!\n");
353 spin_unlock(&dlm->spinlock);
354 /* return to main thread loop and sleep. */
355 return 0;
357 mlog(0, "recovery thread found node %u in the recovery map!\n",
358 dlm->reco.dead_node);
359 spin_unlock(&dlm->spinlock);
361 /* take write barrier */
362 /* (stops the list reshuffling thread, proxy ast handling) */
363 dlm_begin_recovery(dlm);
365 if (dlm->reco.new_master == dlm->node_num)
366 goto master_here;
368 if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
369 /* choose a new master, returns 0 if this node
370 * is the master, -EEXIST if it's another node.
371 * this does not return until a new master is chosen
372 * or recovery completes entirely. */
373 ret = dlm_pick_recovery_master(dlm);
374 if (!ret) {
375 /* already notified everyone. go. */
376 goto master_here;
378 mlog(0, "another node will master this recovery session.\n");
380 mlog(0, "dlm=%s, new_master=%u, this node=%u, dead_node=%u\n",
381 dlm->name, dlm->reco.new_master,
382 dlm->node_num, dlm->reco.dead_node);
384 /* it is safe to start everything back up here
385 * because all of the dead node's lock resources
386 * have been marked as in-recovery */
387 dlm_end_recovery(dlm);
389 /* sleep out in main dlm_recovery_thread loop. */
390 return 0;
392 master_here:
393 mlog(0, "mastering recovery of %s:%u here(this=%u)!\n",
394 dlm->name, dlm->reco.dead_node, dlm->node_num);
396 status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
397 if (status < 0) {
398 mlog(ML_ERROR, "error %d remastering locks for node %u, "
399 "retrying.\n", status, dlm->reco.dead_node);
400 /* yield a bit to allow any final network messages
401 * to get handled on remaining nodes */
402 msleep(100);
403 } else {
404 /* success! see if any other nodes need recovery */
405 mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
406 dlm->name, dlm->reco.dead_node, dlm->node_num);
407 dlm_reset_recovery(dlm);
409 dlm_end_recovery(dlm);
411 /* continue and look for another dead node */
412 return -EAGAIN;
415 static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
417 int status = 0;
418 struct dlm_reco_node_data *ndata;
419 struct list_head *iter;
420 int all_nodes_done;
421 int destroy = 0;
422 int pass = 0;
424 status = dlm_init_recovery_area(dlm, dead_node);
425 if (status < 0)
426 goto leave;
428 /* safe to access the node data list without a lock, since this
429 * process is the only one to change the list */
430 list_for_each(iter, &dlm->reco.node_data) {
431 ndata = list_entry (iter, struct dlm_reco_node_data, list);
432 BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
433 ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
435 mlog(0, "requesting lock info from node %u\n",
436 ndata->node_num);
438 if (ndata->node_num == dlm->node_num) {
439 ndata->state = DLM_RECO_NODE_DATA_DONE;
440 continue;
443 status = dlm_request_all_locks(dlm, ndata->node_num, dead_node);
444 if (status < 0) {
445 mlog_errno(status);
446 if (dlm_is_host_down(status))
447 ndata->state = DLM_RECO_NODE_DATA_DEAD;
448 else {
449 destroy = 1;
450 goto leave;
454 switch (ndata->state) {
455 case DLM_RECO_NODE_DATA_INIT:
456 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
457 case DLM_RECO_NODE_DATA_REQUESTED:
458 BUG();
459 break;
460 case DLM_RECO_NODE_DATA_DEAD:
461 mlog(0, "node %u died after requesting "
462 "recovery info for node %u\n",
463 ndata->node_num, dead_node);
464 // start all over
465 destroy = 1;
466 status = -EAGAIN;
467 goto leave;
468 case DLM_RECO_NODE_DATA_REQUESTING:
469 ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
470 mlog(0, "now receiving recovery data from "
471 "node %u for dead node %u\n",
472 ndata->node_num, dead_node);
473 break;
474 case DLM_RECO_NODE_DATA_RECEIVING:
475 mlog(0, "already receiving recovery data from "
476 "node %u for dead node %u\n",
477 ndata->node_num, dead_node);
478 break;
479 case DLM_RECO_NODE_DATA_DONE:
480 mlog(0, "already DONE receiving recovery data "
481 "from node %u for dead node %u\n",
482 ndata->node_num, dead_node);
483 break;
487 mlog(0, "done requesting all lock info\n");
489 /* nodes should be sending reco data now
490 * just need to wait */
492 while (1) {
493 /* check all the nodes now to see if we are
494 * done, or if anyone died */
495 all_nodes_done = 1;
496 spin_lock(&dlm_reco_state_lock);
497 list_for_each(iter, &dlm->reco.node_data) {
498 ndata = list_entry (iter, struct dlm_reco_node_data, list);
500 mlog(0, "checking recovery state of node %u\n",
501 ndata->node_num);
502 switch (ndata->state) {
503 case DLM_RECO_NODE_DATA_INIT:
504 case DLM_RECO_NODE_DATA_REQUESTING:
505 mlog(ML_ERROR, "bad ndata state for "
506 "node %u: state=%d\n",
507 ndata->node_num, ndata->state);
508 BUG();
509 break;
510 case DLM_RECO_NODE_DATA_DEAD:
511 mlog(ML_NOTICE, "node %u died after "
512 "requesting recovery info for "
513 "node %u\n", ndata->node_num,
514 dead_node);
515 spin_unlock(&dlm_reco_state_lock);
516 // start all over
517 destroy = 1;
518 status = -EAGAIN;
519 /* instead of spinning like crazy here,
520 * wait for the domain map to catch up
521 * with the network state. otherwise this
522 * can be hit hundreds of times before
523 * the node is really seen as dead. */
524 wait_event_timeout(dlm->dlm_reco_thread_wq,
525 dlm_is_node_dead(dlm,
526 ndata->node_num),
527 msecs_to_jiffies(1000));
528 mlog(0, "waited 1 sec for %u, "
529 "dead? %s\n", ndata->node_num,
530 dlm_is_node_dead(dlm, ndata->node_num) ?
531 "yes" : "no");
532 goto leave;
533 case DLM_RECO_NODE_DATA_RECEIVING:
534 case DLM_RECO_NODE_DATA_REQUESTED:
535 all_nodes_done = 0;
536 break;
537 case DLM_RECO_NODE_DATA_DONE:
538 break;
539 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
540 break;
543 spin_unlock(&dlm_reco_state_lock);
545 mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
546 all_nodes_done?"yes":"no");
547 if (all_nodes_done) {
548 int ret;
550 /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
551 * just send a finalize message to everyone and
552 * clean up */
553 mlog(0, "all nodes are done! send finalize\n");
554 ret = dlm_send_finalize_reco_message(dlm);
555 if (ret < 0)
556 mlog_errno(ret);
558 spin_lock(&dlm->spinlock);
559 dlm_finish_local_lockres_recovery(dlm, dead_node,
560 dlm->node_num);
561 spin_unlock(&dlm->spinlock);
562 mlog(0, "should be done with recovery!\n");
564 mlog(0, "finishing recovery of %s at %lu, "
565 "dead=%u, this=%u, new=%u\n", dlm->name,
566 jiffies, dlm->reco.dead_node,
567 dlm->node_num, dlm->reco.new_master);
568 destroy = 1;
569 status = ret;
570 /* rescan everything marked dirty along the way */
571 dlm_kick_thread(dlm, NULL);
572 break;
574 /* wait to be signalled, with periodic timeout
575 * to check for node death */
576 wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
577 kthread_should_stop(),
578 msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
582 leave:
583 if (destroy)
584 dlm_destroy_recovery_area(dlm, dead_node);
586 mlog_exit(status);
587 return status;
590 static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
592 int num=0;
593 struct dlm_reco_node_data *ndata;
595 spin_lock(&dlm->spinlock);
596 memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
597 /* nodes can only be removed (by dying) after dropping
598 * this lock, and death will be trapped later, so this should do */
599 spin_unlock(&dlm->spinlock);
601 while (1) {
602 num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
603 if (num >= O2NM_MAX_NODES) {
604 break;
606 BUG_ON(num == dead_node);
608 ndata = kcalloc(1, sizeof(*ndata), GFP_KERNEL);
609 if (!ndata) {
610 dlm_destroy_recovery_area(dlm, dead_node);
611 return -ENOMEM;
613 ndata->node_num = num;
614 ndata->state = DLM_RECO_NODE_DATA_INIT;
615 spin_lock(&dlm_reco_state_lock);
616 list_add_tail(&ndata->list, &dlm->reco.node_data);
617 spin_unlock(&dlm_reco_state_lock);
618 num++;
621 return 0;
624 static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
626 struct list_head *iter, *iter2;
627 struct dlm_reco_node_data *ndata;
628 LIST_HEAD(tmplist);
630 spin_lock(&dlm_reco_state_lock);
631 list_splice_init(&dlm->reco.node_data, &tmplist);
632 spin_unlock(&dlm_reco_state_lock);
634 list_for_each_safe(iter, iter2, &tmplist) {
635 ndata = list_entry (iter, struct dlm_reco_node_data, list);
636 list_del_init(&ndata->list);
637 kfree(ndata);
641 static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
642 u8 dead_node)
644 struct dlm_lock_request lr;
645 enum dlm_status ret;
647 mlog(0, "\n");
650 mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
651 "to %u\n", dead_node, request_from);
653 memset(&lr, 0, sizeof(lr));
654 lr.node_idx = dlm->node_num;
655 lr.dead_node = dead_node;
657 // send message
658 ret = DLM_NOLOCKMGR;
659 ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
660 &lr, sizeof(lr), request_from, NULL);
662 /* negative status is handled by caller */
663 if (ret < 0)
664 mlog_errno(ret);
666 // return from here, then
667 // sleep until all received or error
668 return ret;
672 int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
674 struct dlm_ctxt *dlm = data;
675 struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
676 char *buf = NULL;
677 struct dlm_work_item *item = NULL;
679 if (!dlm_grab(dlm))
680 return -EINVAL;
682 BUG_ON(lr->dead_node != dlm->reco.dead_node);
684 item = kcalloc(1, sizeof(*item), GFP_KERNEL);
685 if (!item) {
686 dlm_put(dlm);
687 return -ENOMEM;
690 /* this will get freed by dlm_request_all_locks_worker */
691 buf = (char *) __get_free_page(GFP_KERNEL);
692 if (!buf) {
693 kfree(item);
694 dlm_put(dlm);
695 return -ENOMEM;
698 /* queue up work for dlm_request_all_locks_worker */
699 dlm_grab(dlm); /* get an extra ref for the work item */
700 dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
701 item->u.ral.reco_master = lr->node_idx;
702 item->u.ral.dead_node = lr->dead_node;
703 spin_lock(&dlm->work_lock);
704 list_add_tail(&item->list, &dlm->work_list);
705 spin_unlock(&dlm->work_lock);
706 schedule_work(&dlm->dispatched_work);
708 dlm_put(dlm);
709 return 0;
712 static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
714 struct dlm_migratable_lockres *mres;
715 struct dlm_lock_resource *res;
716 struct dlm_ctxt *dlm;
717 LIST_HEAD(resources);
718 struct list_head *iter;
719 int ret;
720 u8 dead_node, reco_master;
722 dlm = item->dlm;
723 dead_node = item->u.ral.dead_node;
724 reco_master = item->u.ral.reco_master;
725 mres = (struct dlm_migratable_lockres *)data;
727 if (dead_node != dlm->reco.dead_node ||
728 reco_master != dlm->reco.new_master) {
729 /* show extra debug info if the recovery state is messed */
730 mlog(ML_ERROR, "%s: bad reco state: reco(dead=%u, master=%u), "
731 "request(dead=%u, master=%u)\n",
732 dlm->name, dlm->reco.dead_node, dlm->reco.new_master,
733 dead_node, reco_master);
734 mlog(ML_ERROR, "%s: name=%.*s master=%u locks=%u/%u flags=%u "
735 "entry[0]={c=%"MLFu64",l=%u,f=%u,t=%d,ct=%d,hb=%d,n=%u}\n",
736 dlm->name, mres->lockname_len, mres->lockname, mres->master,
737 mres->num_locks, mres->total_locks, mres->flags,
738 mres->ml[0].cookie, mres->ml[0].list, mres->ml[0].flags,
739 mres->ml[0].type, mres->ml[0].convert_type,
740 mres->ml[0].highest_blocked, mres->ml[0].node);
741 BUG();
743 BUG_ON(dead_node != dlm->reco.dead_node);
744 BUG_ON(reco_master != dlm->reco.new_master);
746 /* lock resources should have already been moved to the
747 * dlm->reco.resources list. now move items from that list
748 * to a temp list if the dead owner matches. note that the
749 * whole cluster recovers only one node at a time, so we
750 * can safely move UNKNOWN lock resources for each recovery
751 * session. */
752 dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
754 /* now we can begin blasting lockreses without the dlm lock */
755 list_for_each(iter, &resources) {
756 res = list_entry (iter, struct dlm_lock_resource, recovering);
757 ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
758 DLM_MRES_RECOVERY);
759 if (ret < 0)
760 mlog_errno(ret);
763 /* move the resources back to the list */
764 spin_lock(&dlm->spinlock);
765 list_splice_init(&resources, &dlm->reco.resources);
766 spin_unlock(&dlm->spinlock);
768 ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
769 if (ret < 0)
770 mlog_errno(ret);
772 free_page((unsigned long)data);
776 static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
778 int ret, tmpret;
779 struct dlm_reco_data_done done_msg;
781 memset(&done_msg, 0, sizeof(done_msg));
782 done_msg.node_idx = dlm->node_num;
783 done_msg.dead_node = dead_node;
784 mlog(0, "sending DATA DONE message to %u, "
785 "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
786 done_msg.dead_node);
788 ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
789 sizeof(done_msg), send_to, &tmpret);
790 /* negative status is ignored by the caller */
791 if (ret >= 0)
792 ret = tmpret;
793 return ret;
797 int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
799 struct dlm_ctxt *dlm = data;
800 struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
801 struct list_head *iter;
802 struct dlm_reco_node_data *ndata = NULL;
803 int ret = -EINVAL;
805 if (!dlm_grab(dlm))
806 return -EINVAL;
808 mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
809 "node_idx=%u, this node=%u\n", done->dead_node,
810 dlm->reco.dead_node, done->node_idx, dlm->node_num);
811 BUG_ON(done->dead_node != dlm->reco.dead_node);
813 spin_lock(&dlm_reco_state_lock);
814 list_for_each(iter, &dlm->reco.node_data) {
815 ndata = list_entry (iter, struct dlm_reco_node_data, list);
816 if (ndata->node_num != done->node_idx)
817 continue;
819 switch (ndata->state) {
820 /* should have moved beyond INIT but not to FINALIZE yet */
821 case DLM_RECO_NODE_DATA_INIT:
822 case DLM_RECO_NODE_DATA_DEAD:
823 case DLM_RECO_NODE_DATA_FINALIZE_SENT:
824 mlog(ML_ERROR, "bad ndata state for node %u:"
825 " state=%d\n", ndata->node_num,
826 ndata->state);
827 BUG();
828 break;
829 /* these states are possible at this point, anywhere along
830 * the line of recovery */
831 case DLM_RECO_NODE_DATA_DONE:
832 case DLM_RECO_NODE_DATA_RECEIVING:
833 case DLM_RECO_NODE_DATA_REQUESTED:
834 case DLM_RECO_NODE_DATA_REQUESTING:
835 mlog(0, "node %u is DONE sending "
836 "recovery data!\n",
837 ndata->node_num);
839 ndata->state = DLM_RECO_NODE_DATA_DONE;
840 ret = 0;
841 break;
844 spin_unlock(&dlm_reco_state_lock);
846 /* wake the recovery thread, some node is done */
847 if (!ret)
848 dlm_kick_recovery_thread(dlm);
850 if (ret < 0)
851 mlog(ML_ERROR, "failed to find recovery node data for node "
852 "%u\n", done->node_idx);
853 dlm_put(dlm);
855 mlog(0, "leaving reco data done handler, ret=%d\n", ret);
856 return ret;
859 static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
860 struct list_head *list,
861 u8 dead_node)
863 struct dlm_lock_resource *res;
864 struct list_head *iter, *iter2;
865 struct dlm_lock *lock;
867 spin_lock(&dlm->spinlock);
868 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
869 res = list_entry (iter, struct dlm_lock_resource, recovering);
870 /* always prune any $RECOVERY entries for dead nodes,
871 * otherwise hangs can occur during later recovery */
872 if (dlm_is_recovery_lock(res->lockname.name,
873 res->lockname.len)) {
874 spin_lock(&res->spinlock);
875 list_for_each_entry(lock, &res->granted, list) {
876 if (lock->ml.node == dead_node) {
877 mlog(0, "AHA! there was "
878 "a $RECOVERY lock for dead "
879 "node %u (%s)!\n",
880 dead_node, dlm->name);
881 list_del_init(&lock->list);
882 dlm_lock_put(lock);
883 break;
886 spin_unlock(&res->spinlock);
887 continue;
890 if (res->owner == dead_node) {
891 mlog(0, "found lockres owned by dead node while "
892 "doing recovery for node %u. sending it.\n",
893 dead_node);
894 list_del_init(&res->recovering);
895 list_add_tail(&res->recovering, list);
896 } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
897 mlog(0, "found UNKNOWN owner while doing recovery "
898 "for node %u. sending it.\n", dead_node);
899 list_del_init(&res->recovering);
900 list_add_tail(&res->recovering, list);
903 spin_unlock(&dlm->spinlock);
906 static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
908 int total_locks = 0;
909 struct list_head *iter, *queue = &res->granted;
910 int i;
912 for (i=0; i<3; i++) {
913 list_for_each(iter, queue)
914 total_locks++;
915 queue++;
917 return total_locks;
921 static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
922 struct dlm_migratable_lockres *mres,
923 u8 send_to,
924 struct dlm_lock_resource *res,
925 int total_locks)
927 u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
928 int mres_total_locks = be32_to_cpu(mres->total_locks);
929 int sz, ret = 0, status = 0;
930 u8 orig_flags = mres->flags,
931 orig_master = mres->master;
933 BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
934 if (!mres->num_locks)
935 return 0;
937 sz = sizeof(struct dlm_migratable_lockres) +
938 (mres->num_locks * sizeof(struct dlm_migratable_lock));
940 /* add an all-done flag if we reached the last lock */
941 orig_flags = mres->flags;
942 BUG_ON(total_locks > mres_total_locks);
943 if (total_locks == mres_total_locks)
944 mres->flags |= DLM_MRES_ALL_DONE;
946 /* send it */
947 ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
948 sz, send_to, &status);
949 if (ret < 0) {
950 /* XXX: negative status is not handled.
951 * this will end up killing this node. */
952 mlog_errno(ret);
953 } else {
954 /* might get an -ENOMEM back here */
955 ret = status;
956 if (ret < 0) {
957 mlog_errno(ret);
959 if (ret == -EFAULT) {
960 mlog(ML_ERROR, "node %u told me to kill "
961 "myself!\n", send_to);
962 BUG();
967 /* zero and reinit the message buffer */
968 dlm_init_migratable_lockres(mres, res->lockname.name,
969 res->lockname.len, mres_total_locks,
970 mig_cookie, orig_flags, orig_master);
971 return ret;
974 static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
975 const char *lockname, int namelen,
976 int total_locks, u64 cookie,
977 u8 flags, u8 master)
979 /* mres here is one full page */
980 memset(mres, 0, PAGE_SIZE);
981 mres->lockname_len = namelen;
982 memcpy(mres->lockname, lockname, namelen);
983 mres->num_locks = 0;
984 mres->total_locks = cpu_to_be32(total_locks);
985 mres->mig_cookie = cpu_to_be64(cookie);
986 mres->flags = flags;
987 mres->master = master;
991 /* returns 1 if this lock fills the network structure,
992 * 0 otherwise */
993 static int dlm_add_lock_to_array(struct dlm_lock *lock,
994 struct dlm_migratable_lockres *mres, int queue)
996 struct dlm_migratable_lock *ml;
997 int lock_num = mres->num_locks;
999 ml = &(mres->ml[lock_num]);
1000 ml->cookie = lock->ml.cookie;
1001 ml->type = lock->ml.type;
1002 ml->convert_type = lock->ml.convert_type;
1003 ml->highest_blocked = lock->ml.highest_blocked;
1004 ml->list = queue;
1005 if (lock->lksb) {
1006 ml->flags = lock->lksb->flags;
1007 /* send our current lvb */
1008 if (ml->type == LKM_EXMODE ||
1009 ml->type == LKM_PRMODE) {
1010 /* if it is already set, this had better be a PR
1011 * and it has to match */
1012 if (mres->lvb[0] && (ml->type == LKM_EXMODE ||
1013 memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
1014 mlog(ML_ERROR, "mismatched lvbs!\n");
1015 __dlm_print_one_lock_resource(lock->lockres);
1016 BUG();
1018 memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
1021 ml->node = lock->ml.node;
1022 mres->num_locks++;
1023 /* we reached the max, send this network message */
1024 if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
1025 return 1;
1026 return 0;
1030 int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
1031 struct dlm_migratable_lockres *mres,
1032 u8 send_to, u8 flags)
1034 struct list_head *queue, *iter;
1035 int total_locks, i;
1036 u64 mig_cookie = 0;
1037 struct dlm_lock *lock;
1038 int ret = 0;
1040 BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1042 mlog(0, "sending to %u\n", send_to);
1044 total_locks = dlm_num_locks_in_lockres(res);
1045 if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
1046 /* rare, but possible */
1047 mlog(0, "argh. lockres has %d locks. this will "
1048 "require more than one network packet to "
1049 "migrate\n", total_locks);
1050 mig_cookie = dlm_get_next_mig_cookie();
1053 dlm_init_migratable_lockres(mres, res->lockname.name,
1054 res->lockname.len, total_locks,
1055 mig_cookie, flags, res->owner);
1057 total_locks = 0;
1058 for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
1059 queue = dlm_list_idx_to_ptr(res, i);
1060 list_for_each(iter, queue) {
1061 lock = list_entry (iter, struct dlm_lock, list);
1063 /* add another lock. */
1064 total_locks++;
1065 if (!dlm_add_lock_to_array(lock, mres, i))
1066 continue;
1068 /* this filled the lock message,
1069 * we must send it immediately. */
1070 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
1071 res, total_locks);
1072 if (ret < 0) {
1073 // TODO
1074 mlog(ML_ERROR, "dlm_send_mig_lockres_msg "
1075 "returned %d, TODO\n", ret);
1076 BUG();
1080 /* flush any remaining locks */
1081 ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
1082 if (ret < 0) {
1083 // TODO
1084 mlog(ML_ERROR, "dlm_send_mig_lockres_msg returned %d, "
1085 "TODO\n", ret);
1086 BUG();
1088 return ret;
1094 * this message will contain no more than one page worth of
1095 * recovery data, and it will work on only one lockres.
1096 * there may be many locks in this page, and we may need to wait
1097 * for additional packets to complete all the locks (rare, but
1098 * possible).
1101 * NOTE: the allocation error cases here are scary
1102 * we really cannot afford to fail an alloc in recovery
1103 * do we spin? returning an error only delays the problem really
1106 int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
1108 struct dlm_ctxt *dlm = data;
1109 struct dlm_migratable_lockres *mres =
1110 (struct dlm_migratable_lockres *)msg->buf;
1111 int ret = 0;
1112 u8 real_master;
1113 char *buf = NULL;
1114 struct dlm_work_item *item = NULL;
1115 struct dlm_lock_resource *res = NULL;
1117 if (!dlm_grab(dlm))
1118 return -EINVAL;
1120 BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
1122 real_master = mres->master;
1123 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1124 /* cannot migrate a lockres with no master */
1125 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1128 mlog(0, "%s message received from node %u\n",
1129 (mres->flags & DLM_MRES_RECOVERY) ?
1130 "recovery" : "migration", mres->master);
1131 if (mres->flags & DLM_MRES_ALL_DONE)
1132 mlog(0, "all done flag. all lockres data received!\n");
1134 ret = -ENOMEM;
1135 buf = kmalloc(be16_to_cpu(msg->data_len), GFP_KERNEL);
1136 item = kcalloc(1, sizeof(*item), GFP_KERNEL);
1137 if (!buf || !item)
1138 goto leave;
1140 /* lookup the lock to see if we have a secondary queue for this
1141 * already... just add the locks in and this will have its owner
1142 * and RECOVERY flag changed when it completes. */
1143 res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
1144 if (res) {
1145 /* this will get a ref on res */
1146 /* mark it as recovering/migrating and hash it */
1147 spin_lock(&res->spinlock);
1148 if (mres->flags & DLM_MRES_RECOVERY) {
1149 res->state |= DLM_LOCK_RES_RECOVERING;
1150 } else {
1151 if (res->state & DLM_LOCK_RES_MIGRATING) {
1152 /* this is at least the second
1153 * lockres message */
1154 mlog(0, "lock %.*s is already migrating\n",
1155 mres->lockname_len,
1156 mres->lockname);
1157 } else if (res->state & DLM_LOCK_RES_RECOVERING) {
1158 /* caller should BUG */
1159 mlog(ML_ERROR, "node is attempting to migrate "
1160 "lock %.*s, but marked as recovering!\n",
1161 mres->lockname_len, mres->lockname);
1162 ret = -EFAULT;
1163 spin_unlock(&res->spinlock);
1164 goto leave;
1166 res->state |= DLM_LOCK_RES_MIGRATING;
1168 spin_unlock(&res->spinlock);
1169 } else {
1170 /* need to allocate, just like if it was
1171 * mastered here normally */
1172 res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
1173 if (!res)
1174 goto leave;
1176 /* to match the ref that we would have gotten if
1177 * dlm_lookup_lockres had succeeded */
1178 dlm_lockres_get(res);
1180 /* mark it as recovering/migrating and hash it */
1181 if (mres->flags & DLM_MRES_RECOVERY)
1182 res->state |= DLM_LOCK_RES_RECOVERING;
1183 else
1184 res->state |= DLM_LOCK_RES_MIGRATING;
1186 spin_lock(&dlm->spinlock);
1187 __dlm_insert_lockres(dlm, res);
1188 spin_unlock(&dlm->spinlock);
1190 /* now that the new lockres is inserted,
1191 * make it usable by other processes */
1192 spin_lock(&res->spinlock);
1193 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1194 spin_unlock(&res->spinlock);
1196 /* add an extra ref for just-allocated lockres
1197 * otherwise the lockres will be purged immediately */
1198 dlm_lockres_get(res);
1202 /* at this point we have allocated everything we need,
1203 * and we have a hashed lockres with an extra ref and
1204 * the proper res->state flags. */
1205 ret = 0;
1206 if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1207 /* migration cannot have an unknown master */
1208 BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
1209 mlog(0, "recovery has passed me a lockres with an "
1210 "unknown owner.. will need to requery: "
1211 "%.*s\n", mres->lockname_len, mres->lockname);
1212 } else {
1213 spin_lock(&res->spinlock);
1214 dlm_change_lockres_owner(dlm, res, dlm->node_num);
1215 spin_unlock(&res->spinlock);
1218 /* queue up work for dlm_mig_lockres_worker */
1219 dlm_grab(dlm); /* get an extra ref for the work item */
1220 memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
1221 dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
1222 item->u.ml.lockres = res; /* already have a ref */
1223 item->u.ml.real_master = real_master;
1224 spin_lock(&dlm->work_lock);
1225 list_add_tail(&item->list, &dlm->work_list);
1226 spin_unlock(&dlm->work_lock);
1227 schedule_work(&dlm->dispatched_work);
1229 leave:
1230 dlm_put(dlm);
1231 if (ret < 0) {
1232 if (buf)
1233 kfree(buf);
1234 if (item)
1235 kfree(item);
1238 mlog_exit(ret);
1239 return ret;
1243 static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
1245 struct dlm_ctxt *dlm = data;
1246 struct dlm_migratable_lockres *mres;
1247 int ret = 0;
1248 struct dlm_lock_resource *res;
1249 u8 real_master;
1251 dlm = item->dlm;
1252 mres = (struct dlm_migratable_lockres *)data;
1254 res = item->u.ml.lockres;
1255 real_master = item->u.ml.real_master;
1257 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1258 /* this case is super-rare. only occurs if
1259 * node death happens during migration. */
1260 again:
1261 ret = dlm_lockres_master_requery(dlm, res, &real_master);
1262 if (ret < 0) {
1263 mlog(0, "dlm_lockres_master_requery ret=%d\n",
1264 ret);
1265 goto again;
1267 if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
1268 mlog(0, "lockres %.*s not claimed. "
1269 "this node will take it.\n",
1270 res->lockname.len, res->lockname.name);
1271 } else {
1272 mlog(0, "master needs to respond to sender "
1273 "that node %u still owns %.*s\n",
1274 real_master, res->lockname.len,
1275 res->lockname.name);
1276 /* cannot touch this lockres */
1277 goto leave;
1281 ret = dlm_process_recovery_data(dlm, res, mres);
1282 if (ret < 0)
1283 mlog(0, "dlm_process_recovery_data returned %d\n", ret);
1284 else
1285 mlog(0, "dlm_process_recovery_data succeeded\n");
1287 if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
1288 (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
1289 ret = dlm_finish_migration(dlm, res, mres->master);
1290 if (ret < 0)
1291 mlog_errno(ret);
1294 leave:
1295 kfree(data);
1296 mlog_exit(ret);
1301 static int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
1302 struct dlm_lock_resource *res,
1303 u8 *real_master)
1305 struct dlm_node_iter iter;
1306 int nodenum;
1307 int ret = 0;
1309 *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
1311 /* we only reach here if one of the two nodes in a
1312 * migration died while the migration was in progress.
1313 * at this point we need to requery the master. we
1314 * know that the new_master got as far as creating
1315 * an mle on at least one node, but we do not know
1316 * if any nodes had actually cleared the mle and set
1317 * the master to the new_master. the old master
1318 * is supposed to set the owner to UNKNOWN in the
1319 * event of a new_master death, so the only possible
1320 * responses that we can get from nodes here are
1321 * that the master is new_master, or that the master
1322 * is UNKNOWN.
1323 * if all nodes come back with UNKNOWN then we know
1324 * the lock needs remastering here.
1325 * if any node comes back with a valid master, check
1326 * to see if that master is the one that we are
1327 * recovering. if so, then the new_master died and
1328 * we need to remaster this lock. if not, then the
1329 * new_master survived and that node will respond to
1330 * other nodes about the owner.
1331 * if there is an owner, this node needs to dump this
1332 * lockres and alert the sender that this lockres
1333 * was rejected. */
1334 spin_lock(&dlm->spinlock);
1335 dlm_node_iter_init(dlm->domain_map, &iter);
1336 spin_unlock(&dlm->spinlock);
1338 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
1339 /* do not send to self */
1340 if (nodenum == dlm->node_num)
1341 continue;
1342 ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
1343 if (ret < 0) {
1344 mlog_errno(ret);
1345 BUG();
1346 /* TODO: need to figure a way to restart this */
1348 if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1349 mlog(0, "lock master is %u\n", *real_master);
1350 break;
1353 return ret;
1357 static int dlm_do_master_requery(struct dlm_ctxt *dlm,
1358 struct dlm_lock_resource *res,
1359 u8 nodenum, u8 *real_master)
1361 int ret = -EINVAL;
1362 struct dlm_master_requery req;
1363 int status = DLM_LOCK_RES_OWNER_UNKNOWN;
1365 memset(&req, 0, sizeof(req));
1366 req.node_idx = dlm->node_num;
1367 req.namelen = res->lockname.len;
1368 memcpy(req.name, res->lockname.name, res->lockname.len);
1370 ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
1371 &req, sizeof(req), nodenum, &status);
1372 /* XXX: negative status not handled properly here. */
1373 if (ret < 0)
1374 mlog_errno(ret);
1375 else {
1376 BUG_ON(status < 0);
1377 BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
1378 *real_master = (u8) (status & 0xff);
1379 mlog(0, "node %u responded to master requery with %u\n",
1380 nodenum, *real_master);
1381 ret = 0;
1383 return ret;
1387 /* this function cannot error, so unless the sending
1388 * or receiving of the message failed, the owner can
1389 * be trusted */
1390 int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
1392 struct dlm_ctxt *dlm = data;
1393 struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
1394 struct dlm_lock_resource *res = NULL;
1395 int master = DLM_LOCK_RES_OWNER_UNKNOWN;
1396 u32 flags = DLM_ASSERT_MASTER_REQUERY;
1398 if (!dlm_grab(dlm)) {
1399 /* since the domain has gone away on this
1400 * node, the proper response is UNKNOWN */
1401 return master;
1404 spin_lock(&dlm->spinlock);
1405 res = __dlm_lookup_lockres(dlm, req->name, req->namelen);
1406 if (res) {
1407 spin_lock(&res->spinlock);
1408 master = res->owner;
1409 if (master == dlm->node_num) {
1410 int ret = dlm_dispatch_assert_master(dlm, res,
1411 0, 0, flags);
1412 if (ret < 0) {
1413 mlog_errno(-ENOMEM);
1414 /* retry!? */
1415 BUG();
1418 spin_unlock(&res->spinlock);
1420 spin_unlock(&dlm->spinlock);
1422 dlm_put(dlm);
1423 return master;
1426 static inline struct list_head *
1427 dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
1429 struct list_head *ret;
1430 BUG_ON(list_num < 0);
1431 BUG_ON(list_num > 2);
1432 ret = &(res->granted);
1433 ret += list_num;
1434 return ret;
1436 /* TODO: do ast flush business
1437 * TODO: do MIGRATING and RECOVERING spinning
1441 * NOTE about in-flight requests during migration:
1443 * Before attempting the migrate, the master has marked the lockres as
1444 * MIGRATING and then flushed all of its pending ASTS. So any in-flight
1445 * requests either got queued before the MIGRATING flag got set, in which
1446 * case the lock data will reflect the change and a return message is on
1447 * the way, or the request failed to get in before MIGRATING got set. In
1448 * this case, the caller will be told to spin and wait for the MIGRATING
1449 * flag to be dropped, then recheck the master.
1450 * This holds true for the convert, cancel and unlock cases, and since lvb
1451 * updates are tied to these same messages, it applies to lvb updates as
1452 * well. For the lock case, there is no way a lock can be on the master
1453 * queue and not be on the secondary queue since the lock is always added
1454 * locally first. This means that the new target node will never be sent
1455 * a lock that he doesn't already have on the list.
1456 * In total, this means that the local lock is correct and should not be
1457 * updated to match the one sent by the master. Any messages sent back
1458 * from the master before the MIGRATING flag will bring the lock properly
1459 * up-to-date, and the change will be ordered properly for the waiter.
1460 * We will *not* attempt to modify the lock underneath the waiter.
1463 static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
1464 struct dlm_lock_resource *res,
1465 struct dlm_migratable_lockres *mres)
1467 struct dlm_migratable_lock *ml;
1468 struct list_head *queue;
1469 struct dlm_lock *newlock = NULL;
1470 struct dlm_lockstatus *lksb = NULL;
1471 int ret = 0;
1472 int i;
1473 struct list_head *iter;
1474 struct dlm_lock *lock = NULL;
1476 mlog(0, "running %d locks for this lockres\n", mres->num_locks);
1477 for (i=0; i<mres->num_locks; i++) {
1478 ml = &(mres->ml[i]);
1479 BUG_ON(ml->highest_blocked != LKM_IVMODE);
1480 newlock = NULL;
1481 lksb = NULL;
1483 queue = dlm_list_num_to_pointer(res, ml->list);
1485 /* if the lock is for the local node it needs to
1486 * be moved to the proper location within the queue.
1487 * do not allocate a new lock structure. */
1488 if (ml->node == dlm->node_num) {
1489 /* MIGRATION ONLY! */
1490 BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
1492 spin_lock(&res->spinlock);
1493 list_for_each(iter, queue) {
1494 lock = list_entry (iter, struct dlm_lock, list);
1495 if (lock->ml.cookie != ml->cookie)
1496 lock = NULL;
1497 else
1498 break;
1501 /* lock is always created locally first, and
1502 * destroyed locally last. it must be on the list */
1503 if (!lock) {
1504 mlog(ML_ERROR, "could not find local lock "
1505 "with cookie %"MLFu64"!\n",
1506 ml->cookie);
1507 BUG();
1509 BUG_ON(lock->ml.node != ml->node);
1511 /* see NOTE above about why we do not update
1512 * to match the master here */
1514 /* move the lock to its proper place */
1515 /* do not alter lock refcount. switching lists. */
1516 list_del_init(&lock->list);
1517 list_add_tail(&lock->list, queue);
1518 spin_unlock(&res->spinlock);
1520 mlog(0, "just reordered a local lock!\n");
1521 continue;
1524 /* lock is for another node. */
1525 newlock = dlm_new_lock(ml->type, ml->node,
1526 be64_to_cpu(ml->cookie), NULL);
1527 if (!newlock) {
1528 ret = -ENOMEM;
1529 goto leave;
1531 lksb = newlock->lksb;
1532 dlm_lock_attach_lockres(newlock, res);
1534 if (ml->convert_type != LKM_IVMODE) {
1535 BUG_ON(queue != &res->converting);
1536 newlock->ml.convert_type = ml->convert_type;
1538 lksb->flags |= (ml->flags &
1539 (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
1541 if (mres->lvb[0]) {
1542 if (lksb->flags & DLM_LKSB_PUT_LVB) {
1543 /* other node was trying to update
1544 * lvb when node died. recreate the
1545 * lksb with the updated lvb. */
1546 memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
1547 } else {
1548 /* otherwise, the node is sending its
1549 * most recent valid lvb info */
1550 BUG_ON(ml->type != LKM_EXMODE &&
1551 ml->type != LKM_PRMODE);
1552 if (res->lvb[0] && (ml->type == LKM_EXMODE ||
1553 memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
1554 mlog(ML_ERROR, "received bad lvb!\n");
1555 __dlm_print_one_lock_resource(res);
1556 BUG();
1558 memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
1563 /* NOTE:
1564 * wrt lock queue ordering and recovery:
1565 * 1. order of locks on granted queue is
1566 * meaningless.
1567 * 2. order of locks on converting queue is
1568 * LOST with the node death. sorry charlie.
1569 * 3. order of locks on the blocked queue is
1570 * also LOST.
1571 * order of locks does not affect integrity, it
1572 * just means that a lock request may get pushed
1573 * back in line as a result of the node death.
1574 * also note that for a given node the lock order
1575 * for its secondary queue locks is preserved
1576 * relative to each other, but clearly *not*
1577 * preserved relative to locks from other nodes.
1579 spin_lock(&res->spinlock);
1580 dlm_lock_get(newlock);
1581 list_add_tail(&newlock->list, queue);
1582 spin_unlock(&res->spinlock);
1584 mlog(0, "done running all the locks\n");
1586 leave:
1587 if (ret < 0) {
1588 mlog_errno(ret);
1589 if (newlock)
1590 dlm_lock_put(newlock);
1593 mlog_exit(ret);
1594 return ret;
1597 void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
1598 struct dlm_lock_resource *res)
1600 int i;
1601 struct list_head *queue, *iter, *iter2;
1602 struct dlm_lock *lock;
1604 res->state |= DLM_LOCK_RES_RECOVERING;
1605 if (!list_empty(&res->recovering))
1606 list_del_init(&res->recovering);
1607 list_add_tail(&res->recovering, &dlm->reco.resources);
1609 /* find any pending locks and put them back on proper list */
1610 for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
1611 queue = dlm_list_idx_to_ptr(res, i);
1612 list_for_each_safe(iter, iter2, queue) {
1613 lock = list_entry (iter, struct dlm_lock, list);
1614 dlm_lock_get(lock);
1615 if (lock->convert_pending) {
1616 /* move converting lock back to granted */
1617 BUG_ON(i != DLM_CONVERTING_LIST);
1618 mlog(0, "node died with convert pending "
1619 "on %.*s. move back to granted list.\n",
1620 res->lockname.len, res->lockname.name);
1621 dlm_revert_pending_convert(res, lock);
1622 lock->convert_pending = 0;
1623 } else if (lock->lock_pending) {
1624 /* remove pending lock requests completely */
1625 BUG_ON(i != DLM_BLOCKED_LIST);
1626 mlog(0, "node died with lock pending "
1627 "on %.*s. remove from blocked list and skip.\n",
1628 res->lockname.len, res->lockname.name);
1629 /* lock will be floating until ref in
1630 * dlmlock_remote is freed after the network
1631 * call returns. ok for it to not be on any
1632 * list since no ast can be called
1633 * (the master is dead). */
1634 dlm_revert_pending_lock(res, lock);
1635 lock->lock_pending = 0;
1636 } else if (lock->unlock_pending) {
1637 /* if an unlock was in progress, treat as
1638 * if this had completed successfully
1639 * before sending this lock state to the
1640 * new master. note that the dlm_unlock
1641 * call is still responsible for calling
1642 * the unlockast. that will happen after
1643 * the network call times out. for now,
1644 * just move lists to prepare the new
1645 * recovery master. */
1646 BUG_ON(i != DLM_GRANTED_LIST);
1647 mlog(0, "node died with unlock pending "
1648 "on %.*s. remove from blocked list and skip.\n",
1649 res->lockname.len, res->lockname.name);
1650 dlm_commit_pending_unlock(res, lock);
1651 lock->unlock_pending = 0;
1652 } else if (lock->cancel_pending) {
1653 /* if a cancel was in progress, treat as
1654 * if this had completed successfully
1655 * before sending this lock state to the
1656 * new master */
1657 BUG_ON(i != DLM_CONVERTING_LIST);
1658 mlog(0, "node died with cancel pending "
1659 "on %.*s. move back to granted list.\n",
1660 res->lockname.len, res->lockname.name);
1661 dlm_commit_pending_cancel(res, lock);
1662 lock->cancel_pending = 0;
1664 dlm_lock_put(lock);
1671 /* removes all recovered locks from the recovery list.
1672 * sets the res->owner to the new master.
1673 * unsets the RECOVERY flag and wakes waiters. */
1674 static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
1675 u8 dead_node, u8 new_master)
1677 int i;
1678 struct list_head *iter, *iter2, *bucket;
1679 struct dlm_lock_resource *res;
1681 mlog_entry_void();
1683 assert_spin_locked(&dlm->spinlock);
1685 list_for_each_safe(iter, iter2, &dlm->reco.resources) {
1686 res = list_entry (iter, struct dlm_lock_resource, recovering);
1687 if (res->owner == dead_node) {
1688 list_del_init(&res->recovering);
1689 spin_lock(&res->spinlock);
1690 dlm_change_lockres_owner(dlm, res, new_master);
1691 res->state &= ~DLM_LOCK_RES_RECOVERING;
1692 __dlm_dirty_lockres(dlm, res);
1693 spin_unlock(&res->spinlock);
1694 wake_up(&res->wq);
1698 /* this will become unnecessary eventually, but
1699 * for now we need to run the whole hash, clear
1700 * the RECOVERING state and set the owner
1701 * if necessary */
1702 for (i=0; i<DLM_HASH_SIZE; i++) {
1703 bucket = &(dlm->resources[i]);
1704 list_for_each(iter, bucket) {
1705 res = list_entry (iter, struct dlm_lock_resource, list);
1706 if (res->state & DLM_LOCK_RES_RECOVERING) {
1707 if (res->owner == dead_node) {
1708 mlog(0, "(this=%u) res %.*s owner=%u "
1709 "was not on recovering list, but "
1710 "clearing state anyway\n",
1711 dlm->node_num, res->lockname.len,
1712 res->lockname.name, new_master);
1713 } else if (res->owner == dlm->node_num) {
1714 mlog(0, "(this=%u) res %.*s owner=%u "
1715 "was not on recovering list, "
1716 "owner is THIS node, clearing\n",
1717 dlm->node_num, res->lockname.len,
1718 res->lockname.name, new_master);
1719 } else
1720 continue;
1722 spin_lock(&res->spinlock);
1723 dlm_change_lockres_owner(dlm, res, new_master);
1724 res->state &= ~DLM_LOCK_RES_RECOVERING;
1725 __dlm_dirty_lockres(dlm, res);
1726 spin_unlock(&res->spinlock);
1727 wake_up(&res->wq);
1733 static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
1735 if (local) {
1736 if (lock->ml.type != LKM_EXMODE &&
1737 lock->ml.type != LKM_PRMODE)
1738 return 1;
1739 } else if (lock->ml.type == LKM_EXMODE)
1740 return 1;
1741 return 0;
1744 static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
1745 struct dlm_lock_resource *res, u8 dead_node)
1747 struct list_head *iter, *queue;
1748 struct dlm_lock *lock;
1749 int blank_lvb = 0, local = 0;
1750 int i;
1751 u8 search_node;
1753 assert_spin_locked(&dlm->spinlock);
1754 assert_spin_locked(&res->spinlock);
1756 if (res->owner == dlm->node_num)
1757 /* if this node owned the lockres, and if the dead node
1758 * had an EX when he died, blank out the lvb */
1759 search_node = dead_node;
1760 else {
1761 /* if this is a secondary lockres, and we had no EX or PR
1762 * locks granted, we can no longer trust the lvb */
1763 search_node = dlm->node_num;
1764 local = 1; /* check local state for valid lvb */
1767 for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
1768 queue = dlm_list_idx_to_ptr(res, i);
1769 list_for_each(iter, queue) {
1770 lock = list_entry (iter, struct dlm_lock, list);
1771 if (lock->ml.node == search_node) {
1772 if (dlm_lvb_needs_invalidation(lock, local)) {
1773 /* zero the lksb lvb and lockres lvb */
1774 blank_lvb = 1;
1775 memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
1781 if (blank_lvb) {
1782 mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
1783 res->lockname.len, res->lockname.name, dead_node);
1784 memset(res->lvb, 0, DLM_LVB_LEN);
1788 static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
1789 struct dlm_lock_resource *res, u8 dead_node)
1791 struct list_head *iter, *tmpiter;
1792 struct dlm_lock *lock;
1794 /* this node is the lockres master:
1795 * 1) remove any stale locks for the dead node
1796 * 2) if the dead node had an EX when he died, blank out the lvb
1798 assert_spin_locked(&dlm->spinlock);
1799 assert_spin_locked(&res->spinlock);
1801 /* TODO: check pending_asts, pending_basts here */
1802 list_for_each_safe(iter, tmpiter, &res->granted) {
1803 lock = list_entry (iter, struct dlm_lock, list);
1804 if (lock->ml.node == dead_node) {
1805 list_del_init(&lock->list);
1806 dlm_lock_put(lock);
1809 list_for_each_safe(iter, tmpiter, &res->converting) {
1810 lock = list_entry (iter, struct dlm_lock, list);
1811 if (lock->ml.node == dead_node) {
1812 list_del_init(&lock->list);
1813 dlm_lock_put(lock);
1816 list_for_each_safe(iter, tmpiter, &res->blocked) {
1817 lock = list_entry (iter, struct dlm_lock, list);
1818 if (lock->ml.node == dead_node) {
1819 list_del_init(&lock->list);
1820 dlm_lock_put(lock);
1824 /* do not kick thread yet */
1825 __dlm_dirty_lockres(dlm, res);
1828 /* if this node is the recovery master, and there are no
1829 * locks for a given lockres owned by this node that are in
1830 * either PR or EX mode, zero out the lvb before requesting.
1835 static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
1837 struct list_head *iter;
1838 struct dlm_lock_resource *res;
1839 int i;
1840 struct list_head *bucket;
1841 struct dlm_lock *lock;
1844 /* purge any stale mles */
1845 dlm_clean_master_list(dlm, dead_node);
1848 * now clean up all lock resources. there are two rules:
1850 * 1) if the dead node was the master, move the lockres
1851 * to the recovering list. set the RECOVERING flag.
1852 * this lockres needs to be cleaned up before it can
1853 * be used further.
1855 * 2) if this node was the master, remove all locks from
1856 * each of the lockres queues that were owned by the
1857 * dead node. once recovery finishes, the dlm thread
1858 * can be kicked again to see if any ASTs or BASTs
1859 * need to be fired as a result.
1861 for (i=0; i<DLM_HASH_SIZE; i++) {
1862 bucket = &(dlm->resources[i]);
1863 list_for_each(iter, bucket) {
1864 res = list_entry (iter, struct dlm_lock_resource, list);
1865 /* always prune any $RECOVERY entries for dead nodes,
1866 * otherwise hangs can occur during later recovery */
1867 if (dlm_is_recovery_lock(res->lockname.name,
1868 res->lockname.len)) {
1869 spin_lock(&res->spinlock);
1870 list_for_each_entry(lock, &res->granted, list) {
1871 if (lock->ml.node == dead_node) {
1872 mlog(0, "AHA! there was "
1873 "a $RECOVERY lock for dead "
1874 "node %u (%s)!\n",
1875 dead_node, dlm->name);
1876 list_del_init(&lock->list);
1877 dlm_lock_put(lock);
1878 break;
1881 spin_unlock(&res->spinlock);
1882 continue;
1884 spin_lock(&res->spinlock);
1885 /* zero the lvb if necessary */
1886 dlm_revalidate_lvb(dlm, res, dead_node);
1887 if (res->owner == dead_node)
1888 dlm_move_lockres_to_recovery_list(dlm, res);
1889 else if (res->owner == dlm->node_num) {
1890 dlm_free_dead_locks(dlm, res, dead_node);
1891 __dlm_lockres_calc_usage(dlm, res);
1893 spin_unlock(&res->spinlock);
1899 static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
1901 assert_spin_locked(&dlm->spinlock);
1903 /* check to see if the node is already considered dead */
1904 if (!test_bit(idx, dlm->live_nodes_map)) {
1905 mlog(0, "for domain %s, node %d is already dead. "
1906 "another node likely did recovery already.\n",
1907 dlm->name, idx);
1908 return;
1911 /* check to see if we do not care about this node */
1912 if (!test_bit(idx, dlm->domain_map)) {
1913 /* This also catches the case that we get a node down
1914 * but haven't joined the domain yet. */
1915 mlog(0, "node %u already removed from domain!\n", idx);
1916 return;
1919 clear_bit(idx, dlm->live_nodes_map);
1921 /* Clean up join state on node death. */
1922 if (dlm->joining_node == idx) {
1923 mlog(0, "Clearing join state for node %u\n", idx);
1924 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1927 /* make sure local cleanup occurs before the heartbeat events */
1928 if (!test_bit(idx, dlm->recovery_map))
1929 dlm_do_local_recovery_cleanup(dlm, idx);
1931 /* notify anything attached to the heartbeat events */
1932 dlm_hb_event_notify_attached(dlm, idx, 0);
1934 mlog(0, "node %u being removed from domain map!\n", idx);
1935 clear_bit(idx, dlm->domain_map);
1936 /* wake up migration waiters if a node goes down.
1937 * perhaps later we can genericize this for other waiters. */
1938 wake_up(&dlm->migration_wq);
1940 if (test_bit(idx, dlm->recovery_map))
1941 mlog(0, "domain %s, node %u already added "
1942 "to recovery map!\n", dlm->name, idx);
1943 else
1944 set_bit(idx, dlm->recovery_map);
1947 void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
1949 struct dlm_ctxt *dlm = data;
1951 if (!dlm_grab(dlm))
1952 return;
1954 spin_lock(&dlm->spinlock);
1955 __dlm_hb_node_down(dlm, idx);
1956 spin_unlock(&dlm->spinlock);
1958 dlm_put(dlm);
1961 void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
1963 struct dlm_ctxt *dlm = data;
1965 if (!dlm_grab(dlm))
1966 return;
1968 spin_lock(&dlm->spinlock);
1969 set_bit(idx, dlm->live_nodes_map);
1970 /* do NOT notify mle attached to the heartbeat events.
1971 * new nodes are not interesting in mastery until joined. */
1972 spin_unlock(&dlm->spinlock);
1974 dlm_put(dlm);
1977 static void dlm_reco_ast(void *astdata)
1979 struct dlm_ctxt *dlm = astdata;
1980 mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
1981 dlm->node_num, dlm->name);
1983 static void dlm_reco_bast(void *astdata, int blocked_type)
1985 struct dlm_ctxt *dlm = astdata;
1986 mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
1987 dlm->node_num, dlm->name);
1989 static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
1991 mlog(0, "unlockast for recovery lock fired!\n");
1995 * dlm_pick_recovery_master will continually attempt to use
1996 * dlmlock() on the special "$RECOVERY" lockres with the
1997 * LKM_NOQUEUE flag to get an EX. every thread that enters
1998 * this function on each node racing to become the recovery
1999 * master will not stop attempting this until either:
2000 * a) this node gets the EX (and becomes the recovery master),
2001 * or b) dlm->reco.new_master gets set to some nodenum
2002 * != O2NM_INVALID_NODE_NUM (another node will do the reco).
2003 * so each time a recovery master is needed, the entire cluster
2004 * will sync at this point. if the new master dies, that will
2005 * be detected in dlm_do_recovery */
2006 static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
2008 enum dlm_status ret;
2009 struct dlm_lockstatus lksb;
2010 int status = -EINVAL;
2012 mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
2013 dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
2014 again:
2015 memset(&lksb, 0, sizeof(lksb));
2017 ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
2018 DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
2020 mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
2021 dlm->name, ret, lksb.status);
2023 if (ret == DLM_NORMAL) {
2024 mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
2025 dlm->name, dlm->node_num);
2027 /* got the EX lock. check to see if another node
2028 * just became the reco master */
2029 if (dlm_reco_master_ready(dlm)) {
2030 mlog(0, "%s: got reco EX lock, but %u will "
2031 "do the recovery\n", dlm->name,
2032 dlm->reco.new_master);
2033 status = -EEXIST;
2034 } else {
2035 status = dlm_send_begin_reco_message(dlm,
2036 dlm->reco.dead_node);
2037 /* this always succeeds */
2038 BUG_ON(status);
2040 /* set the new_master to this node */
2041 spin_lock(&dlm->spinlock);
2042 dlm->reco.new_master = dlm->node_num;
2043 spin_unlock(&dlm->spinlock);
2046 /* recovery lock is a special case. ast will not get fired,
2047 * so just go ahead and unlock it. */
2048 ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
2049 if (ret == DLM_DENIED) {
2050 mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
2051 ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
2053 if (ret != DLM_NORMAL) {
2054 /* this would really suck. this could only happen
2055 * if there was a network error during the unlock
2056 * because of node death. this means the unlock
2057 * is actually "done" and the lock structure is
2058 * even freed. we can continue, but only
2059 * because this specific lock name is special. */
2060 mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
2062 } else if (ret == DLM_NOTQUEUED) {
2063 mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
2064 dlm->name, dlm->node_num);
2065 /* another node is master. wait on
2066 * reco.new_master != O2NM_INVALID_NODE_NUM
2067 * for at most one second */
2068 wait_event_timeout(dlm->dlm_reco_thread_wq,
2069 dlm_reco_master_ready(dlm),
2070 msecs_to_jiffies(1000));
2071 if (!dlm_reco_master_ready(dlm)) {
2072 mlog(0, "%s: reco master taking awhile\n",
2073 dlm->name);
2074 goto again;
2076 /* another node has informed this one that it is reco master */
2077 mlog(0, "%s: reco master %u is ready to recover %u\n",
2078 dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
2079 status = -EEXIST;
2080 } else {
2081 struct dlm_lock_resource *res;
2083 /* dlmlock returned something other than NOTQUEUED or NORMAL */
2084 mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
2085 "lksb.status=%s\n", dlm->name, dlm_errname(ret),
2086 dlm_errname(lksb.status));
2087 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2088 DLM_RECOVERY_LOCK_NAME_LEN);
2089 if (res) {
2090 dlm_print_one_lock_resource(res);
2091 dlm_lockres_put(res);
2092 } else {
2093 mlog(ML_ERROR, "recovery lock not found\n");
2095 BUG();
2098 return status;
2101 static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
2103 struct dlm_begin_reco br;
2104 int ret = 0;
2105 struct dlm_node_iter iter;
2106 int nodenum;
2107 int status;
2109 mlog_entry("%u\n", dead_node);
2111 mlog(0, "dead node is %u\n", dead_node);
2113 spin_lock(&dlm->spinlock);
2114 dlm_node_iter_init(dlm->domain_map, &iter);
2115 spin_unlock(&dlm->spinlock);
2117 clear_bit(dead_node, iter.node_map);
2119 memset(&br, 0, sizeof(br));
2120 br.node_idx = dlm->node_num;
2121 br.dead_node = dead_node;
2123 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2124 ret = 0;
2125 if (nodenum == dead_node) {
2126 mlog(0, "not sending begin reco to dead node "
2127 "%u\n", dead_node);
2128 continue;
2130 if (nodenum == dlm->node_num) {
2131 mlog(0, "not sending begin reco to self\n");
2132 continue;
2134 retry:
2135 ret = -EINVAL;
2136 mlog(0, "attempting to send begin reco msg to %d\n",
2137 nodenum);
2138 ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
2139 &br, sizeof(br), nodenum, &status);
2140 /* negative status is handled ok by caller here */
2141 if (ret >= 0)
2142 ret = status;
2143 if (dlm_is_host_down(ret)) {
2144 /* node is down. not involved in recovery
2145 * so just keep going */
2146 mlog(0, "%s: node %u was down when sending "
2147 "begin reco msg (%d)\n", dlm->name, nodenum, ret);
2148 ret = 0;
2150 if (ret < 0) {
2151 struct dlm_lock_resource *res;
2152 /* this is now a serious problem, possibly ENOMEM
2153 * in the network stack. must retry */
2154 mlog_errno(ret);
2155 mlog(ML_ERROR, "begin reco of dlm %s to node %u "
2156 " returned %d\n", dlm->name, nodenum, ret);
2157 res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
2158 DLM_RECOVERY_LOCK_NAME_LEN);
2159 if (res) {
2160 dlm_print_one_lock_resource(res);
2161 dlm_lockres_put(res);
2162 } else {
2163 mlog(ML_ERROR, "recovery lock not found\n");
2165 /* sleep for a bit in hopes that we can avoid
2166 * another ENOMEM */
2167 msleep(100);
2168 goto retry;
2172 return ret;
2175 int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2177 struct dlm_ctxt *dlm = data;
2178 struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
2180 /* ok to return 0, domain has gone away */
2181 if (!dlm_grab(dlm))
2182 return 0;
2184 mlog(0, "node %u wants to recover node %u\n",
2185 br->node_idx, br->dead_node);
2187 dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
2189 spin_lock(&dlm->spinlock);
2190 if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
2191 if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
2192 mlog(0, "%s: new_master %u died, changing "
2193 "to %u\n", dlm->name, dlm->reco.new_master,
2194 br->node_idx);
2195 } else {
2196 mlog(0, "%s: new_master %u NOT DEAD, changing "
2197 "to %u\n", dlm->name, dlm->reco.new_master,
2198 br->node_idx);
2199 /* may not have seen the new master as dead yet */
2202 if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
2203 mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
2204 "node %u changing it to %u\n", dlm->name,
2205 dlm->reco.dead_node, br->node_idx, br->dead_node);
2207 dlm->reco.new_master = br->node_idx;
2208 dlm->reco.dead_node = br->dead_node;
2209 if (!test_bit(br->dead_node, dlm->recovery_map)) {
2210 mlog(0, "recovery master %u sees %u as dead, but this "
2211 "node has not yet. marking %u as dead\n",
2212 br->node_idx, br->dead_node, br->dead_node);
2213 if (!test_bit(br->dead_node, dlm->domain_map) ||
2214 !test_bit(br->dead_node, dlm->live_nodes_map))
2215 mlog(0, "%u not in domain/live_nodes map "
2216 "so setting it in reco map manually\n",
2217 br->dead_node);
2218 set_bit(br->dead_node, dlm->recovery_map);
2219 __dlm_hb_node_down(dlm, br->dead_node);
2221 spin_unlock(&dlm->spinlock);
2223 dlm_kick_recovery_thread(dlm);
2224 dlm_put(dlm);
2225 return 0;
2228 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
2230 int ret = 0;
2231 struct dlm_finalize_reco fr;
2232 struct dlm_node_iter iter;
2233 int nodenum;
2234 int status;
2236 mlog(0, "finishing recovery for node %s:%u\n",
2237 dlm->name, dlm->reco.dead_node);
2239 spin_lock(&dlm->spinlock);
2240 dlm_node_iter_init(dlm->domain_map, &iter);
2241 spin_unlock(&dlm->spinlock);
2243 memset(&fr, 0, sizeof(fr));
2244 fr.node_idx = dlm->node_num;
2245 fr.dead_node = dlm->reco.dead_node;
2247 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2248 if (nodenum == dlm->node_num)
2249 continue;
2250 ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
2251 &fr, sizeof(fr), nodenum, &status);
2252 if (ret >= 0) {
2253 ret = status;
2254 if (dlm_is_host_down(ret)) {
2255 /* this has no effect on this recovery
2256 * session, so set the status to zero to
2257 * finish out the last recovery */
2258 mlog(ML_ERROR, "node %u went down after this "
2259 "node finished recovery.\n", nodenum);
2260 ret = 0;
2263 if (ret < 0) {
2264 mlog_errno(ret);
2265 break;
2269 return ret;
2272 int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
2274 struct dlm_ctxt *dlm = data;
2275 struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
2277 /* ok to return 0, domain has gone away */
2278 if (!dlm_grab(dlm))
2279 return 0;
2281 mlog(0, "node %u finalizing recovery of node %u\n",
2282 fr->node_idx, fr->dead_node);
2284 spin_lock(&dlm->spinlock);
2286 if (dlm->reco.new_master != fr->node_idx) {
2287 mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
2288 "%u is supposed to be the new master, dead=%u\n",
2289 fr->node_idx, dlm->reco.new_master, fr->dead_node);
2290 BUG();
2292 if (dlm->reco.dead_node != fr->dead_node) {
2293 mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
2294 "node %u, but node %u is supposed to be dead\n",
2295 fr->node_idx, fr->dead_node, dlm->reco.dead_node);
2296 BUG();
2299 dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
2301 spin_unlock(&dlm->spinlock);
2303 dlm_reset_recovery(dlm);
2305 dlm_kick_recovery_thread(dlm);
2306 dlm_put(dlm);
2307 return 0;