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