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