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