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