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