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