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