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