1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * dlmmod.c 4 * 5 * standalone DLM module 6 * 7 * Copyright (C) 2004 Oracle. All rights reserved. 8 */ 9 10 11 #include <linux/module.h> 12 #include <linux/fs.h> 13 #include <linux/types.h> 14 #include <linux/slab.h> 15 #include <linux/highmem.h> 16 #include <linux/init.h> 17 #include <linux/sysctl.h> 18 #include <linux/random.h> 19 #include <linux/blkdev.h> 20 #include <linux/socket.h> 21 #include <linux/inet.h> 22 #include <linux/spinlock.h> 23 #include <linux/delay.h> 24 #include <linux/string_choices.h> 25 26 #include "../cluster/heartbeat.h" 27 #include "../cluster/nodemanager.h" 28 #include "../cluster/tcp.h" 29 30 #include "dlmapi.h" 31 #include "dlmcommon.h" 32 #include "dlmdomain.h" 33 #include "dlmdebug.h" 34 35 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER) 36 #include "../cluster/masklog.h" 37 38 static void dlm_mle_node_down(struct dlm_ctxt *dlm, 39 struct dlm_master_list_entry *mle, 40 struct o2nm_node *node, 41 int idx); 42 static void dlm_mle_node_up(struct dlm_ctxt *dlm, 43 struct dlm_master_list_entry *mle, 44 struct o2nm_node *node, 45 int idx); 46 47 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data); 48 static int dlm_do_assert_master(struct dlm_ctxt *dlm, 49 struct dlm_lock_resource *res, 50 void *nodemap, u32 flags); 51 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data); 52 53 static inline int dlm_mle_equal(struct dlm_ctxt *dlm, 54 struct dlm_master_list_entry *mle, 55 const char *name, 56 unsigned int namelen) 57 { 58 if (dlm != mle->dlm) 59 return 0; 60 61 if (namelen != mle->mnamelen || 62 memcmp(name, mle->mname, namelen) != 0) 63 return 0; 64 65 return 1; 66 } 67 68 static struct kmem_cache *dlm_lockres_cache; 69 static struct kmem_cache *dlm_lockname_cache; 70 static struct kmem_cache *dlm_mle_cache; 71 72 static void dlm_mle_release(struct kref *kref); 73 static void dlm_init_mle(struct dlm_master_list_entry *mle, 74 enum dlm_mle_type type, 75 struct dlm_ctxt *dlm, 76 struct dlm_lock_resource *res, 77 const char *name, 78 unsigned int namelen); 79 static void dlm_put_mle(struct dlm_master_list_entry *mle); 80 static void __dlm_put_mle(struct dlm_master_list_entry *mle); 81 static int dlm_find_mle(struct dlm_ctxt *dlm, 82 struct dlm_master_list_entry **mle, 83 char *name, unsigned int namelen); 84 85 static int dlm_do_master_request(struct dlm_lock_resource *res, 86 struct dlm_master_list_entry *mle, int to); 87 88 89 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm, 90 struct dlm_lock_resource *res, 91 struct dlm_master_list_entry *mle, 92 int *blocked); 93 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm, 94 struct dlm_lock_resource *res, 95 struct dlm_master_list_entry *mle, 96 int blocked); 97 static int dlm_add_migration_mle(struct dlm_ctxt *dlm, 98 struct dlm_lock_resource *res, 99 struct dlm_master_list_entry *mle, 100 struct dlm_master_list_entry **oldmle, 101 const char *name, unsigned int namelen, 102 u8 new_master, u8 master); 103 104 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm, 105 struct dlm_lock_resource *res); 106 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm, 107 struct dlm_lock_resource *res); 108 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm, 109 struct dlm_lock_resource *res, 110 u8 target); 111 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm, 112 struct dlm_lock_resource *res); 113 114 115 int dlm_is_host_down(int errno) 116 { 117 switch (errno) { 118 case -EBADF: 119 case -ECONNREFUSED: 120 case -ENOTCONN: 121 case -ECONNRESET: 122 case -EPIPE: 123 case -EHOSTDOWN: 124 case -EHOSTUNREACH: 125 case -ETIMEDOUT: 126 case -ECONNABORTED: 127 case -ENETDOWN: 128 case -ENETUNREACH: 129 case -ENETRESET: 130 case -ESHUTDOWN: 131 case -ENOPROTOOPT: 132 case -EINVAL: /* if returned from our tcp code, 133 this means there is no socket */ 134 return 1; 135 } 136 return 0; 137 } 138 139 140 /* 141 * MASTER LIST FUNCTIONS 142 */ 143 144 145 /* 146 * regarding master list entries and heartbeat callbacks: 147 * 148 * in order to avoid sleeping and allocation that occurs in 149 * heartbeat, master list entries are simply attached to the 150 * dlm's established heartbeat callbacks. the mle is attached 151 * when it is created, and since the dlm->spinlock is held at 152 * that time, any heartbeat event will be properly discovered 153 * by the mle. the mle needs to be detached from the 154 * dlm->mle_hb_events list as soon as heartbeat events are no 155 * longer useful to the mle, and before the mle is freed. 156 * 157 * as a general rule, heartbeat events are no longer needed by 158 * the mle once an "answer" regarding the lock master has been 159 * received. 160 */ 161 static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm, 162 struct dlm_master_list_entry *mle) 163 { 164 assert_spin_locked(&dlm->spinlock); 165 166 list_add_tail(&mle->hb_events, &dlm->mle_hb_events); 167 } 168 169 170 static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm, 171 struct dlm_master_list_entry *mle) 172 { 173 if (!list_empty(&mle->hb_events)) 174 list_del_init(&mle->hb_events); 175 } 176 177 178 static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm, 179 struct dlm_master_list_entry *mle) 180 { 181 spin_lock(&dlm->spinlock); 182 __dlm_mle_detach_hb_events(dlm, mle); 183 spin_unlock(&dlm->spinlock); 184 } 185 186 static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle) 187 { 188 struct dlm_ctxt *dlm; 189 dlm = mle->dlm; 190 191 assert_spin_locked(&dlm->spinlock); 192 assert_spin_locked(&dlm->master_lock); 193 mle->inuse++; 194 kref_get(&mle->mle_refs); 195 } 196 197 static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle) 198 { 199 struct dlm_ctxt *dlm; 200 dlm = mle->dlm; 201 202 spin_lock(&dlm->spinlock); 203 spin_lock(&dlm->master_lock); 204 mle->inuse--; 205 __dlm_put_mle(mle); 206 spin_unlock(&dlm->master_lock); 207 spin_unlock(&dlm->spinlock); 208 209 } 210 211 /* remove from list and free */ 212 static void __dlm_put_mle(struct dlm_master_list_entry *mle) 213 { 214 struct dlm_ctxt *dlm; 215 dlm = mle->dlm; 216 217 assert_spin_locked(&dlm->spinlock); 218 assert_spin_locked(&dlm->master_lock); 219 if (!kref_read(&mle->mle_refs)) { 220 /* this may or may not crash, but who cares. 221 * it's a BUG. */ 222 mlog(ML_ERROR, "bad mle: %p\n", mle); 223 dlm_print_one_mle(mle); 224 BUG(); 225 } else 226 kref_put(&mle->mle_refs, dlm_mle_release); 227 } 228 229 230 /* must not have any spinlocks coming in */ 231 static void dlm_put_mle(struct dlm_master_list_entry *mle) 232 { 233 struct dlm_ctxt *dlm; 234 dlm = mle->dlm; 235 236 spin_lock(&dlm->spinlock); 237 spin_lock(&dlm->master_lock); 238 __dlm_put_mle(mle); 239 spin_unlock(&dlm->master_lock); 240 spin_unlock(&dlm->spinlock); 241 } 242 243 static inline void dlm_get_mle(struct dlm_master_list_entry *mle) 244 { 245 kref_get(&mle->mle_refs); 246 } 247 248 static void dlm_init_mle(struct dlm_master_list_entry *mle, 249 enum dlm_mle_type type, 250 struct dlm_ctxt *dlm, 251 struct dlm_lock_resource *res, 252 const char *name, 253 unsigned int namelen) 254 { 255 assert_spin_locked(&dlm->spinlock); 256 257 mle->dlm = dlm; 258 mle->type = type; 259 INIT_HLIST_NODE(&mle->master_hash_node); 260 INIT_LIST_HEAD(&mle->hb_events); 261 bitmap_zero(mle->maybe_map, O2NM_MAX_NODES); 262 spin_lock_init(&mle->spinlock); 263 init_waitqueue_head(&mle->wq); 264 atomic_set(&mle->woken, 0); 265 kref_init(&mle->mle_refs); 266 bitmap_zero(mle->response_map, O2NM_MAX_NODES); 267 mle->master = O2NM_MAX_NODES; 268 mle->new_master = O2NM_MAX_NODES; 269 mle->inuse = 0; 270 271 BUG_ON(mle->type != DLM_MLE_BLOCK && 272 mle->type != DLM_MLE_MASTER && 273 mle->type != DLM_MLE_MIGRATION); 274 275 if (mle->type == DLM_MLE_MASTER) { 276 BUG_ON(!res); 277 mle->mleres = res; 278 memcpy(mle->mname, res->lockname.name, res->lockname.len); 279 mle->mnamelen = res->lockname.len; 280 mle->mnamehash = res->lockname.hash; 281 } else { 282 BUG_ON(!name); 283 mle->mleres = NULL; 284 memcpy(mle->mname, name, namelen); 285 mle->mnamelen = namelen; 286 mle->mnamehash = dlm_lockid_hash(name, namelen); 287 } 288 289 atomic_inc(&dlm->mle_tot_count[mle->type]); 290 atomic_inc(&dlm->mle_cur_count[mle->type]); 291 292 /* copy off the node_map and register hb callbacks on our copy */ 293 bitmap_copy(mle->node_map, dlm->domain_map, O2NM_MAX_NODES); 294 bitmap_copy(mle->vote_map, dlm->domain_map, O2NM_MAX_NODES); 295 clear_bit(dlm->node_num, mle->vote_map); 296 clear_bit(dlm->node_num, mle->node_map); 297 298 /* attach the mle to the domain node up/down events */ 299 __dlm_mle_attach_hb_events(dlm, mle); 300 } 301 302 void __dlm_unlink_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle) 303 { 304 assert_spin_locked(&dlm->spinlock); 305 assert_spin_locked(&dlm->master_lock); 306 307 if (!hlist_unhashed(&mle->master_hash_node)) 308 hlist_del_init(&mle->master_hash_node); 309 } 310 311 void __dlm_insert_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle) 312 { 313 struct hlist_head *bucket; 314 315 assert_spin_locked(&dlm->master_lock); 316 317 bucket = dlm_master_hash(dlm, mle->mnamehash); 318 hlist_add_head(&mle->master_hash_node, bucket); 319 } 320 321 /* returns 1 if found, 0 if not */ 322 static int dlm_find_mle(struct dlm_ctxt *dlm, 323 struct dlm_master_list_entry **mle, 324 char *name, unsigned int namelen) 325 { 326 struct dlm_master_list_entry *tmpmle; 327 struct hlist_head *bucket; 328 unsigned int hash; 329 330 assert_spin_locked(&dlm->master_lock); 331 332 hash = dlm_lockid_hash(name, namelen); 333 bucket = dlm_master_hash(dlm, hash); 334 hlist_for_each_entry(tmpmle, bucket, master_hash_node) { 335 if (!dlm_mle_equal(dlm, tmpmle, name, namelen)) 336 continue; 337 dlm_get_mle(tmpmle); 338 *mle = tmpmle; 339 return 1; 340 } 341 return 0; 342 } 343 344 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up) 345 { 346 struct dlm_master_list_entry *mle; 347 348 assert_spin_locked(&dlm->spinlock); 349 350 list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) { 351 if (node_up) 352 dlm_mle_node_up(dlm, mle, NULL, idx); 353 else 354 dlm_mle_node_down(dlm, mle, NULL, idx); 355 } 356 } 357 358 static void dlm_mle_node_down(struct dlm_ctxt *dlm, 359 struct dlm_master_list_entry *mle, 360 struct o2nm_node *node, int idx) 361 { 362 spin_lock(&mle->spinlock); 363 364 if (!test_bit(idx, mle->node_map)) 365 mlog(0, "node %u already removed from nodemap!\n", idx); 366 else 367 clear_bit(idx, mle->node_map); 368 369 spin_unlock(&mle->spinlock); 370 } 371 372 static void dlm_mle_node_up(struct dlm_ctxt *dlm, 373 struct dlm_master_list_entry *mle, 374 struct o2nm_node *node, int idx) 375 { 376 spin_lock(&mle->spinlock); 377 378 if (test_bit(idx, mle->node_map)) 379 mlog(0, "node %u already in node map!\n", idx); 380 else 381 set_bit(idx, mle->node_map); 382 383 spin_unlock(&mle->spinlock); 384 } 385 386 387 int dlm_init_mle_cache(void) 388 { 389 dlm_mle_cache = kmem_cache_create("o2dlm_mle", 390 sizeof(struct dlm_master_list_entry), 391 0, SLAB_HWCACHE_ALIGN, 392 NULL); 393 if (dlm_mle_cache == NULL) 394 return -ENOMEM; 395 return 0; 396 } 397 398 void dlm_destroy_mle_cache(void) 399 { 400 kmem_cache_destroy(dlm_mle_cache); 401 } 402 403 static void dlm_mle_release(struct kref *kref) 404 { 405 struct dlm_master_list_entry *mle; 406 struct dlm_ctxt *dlm; 407 408 mle = container_of(kref, struct dlm_master_list_entry, mle_refs); 409 dlm = mle->dlm; 410 411 assert_spin_locked(&dlm->spinlock); 412 assert_spin_locked(&dlm->master_lock); 413 414 mlog(0, "Releasing mle for %.*s, type %d\n", mle->mnamelen, mle->mname, 415 mle->type); 416 417 /* remove from list if not already */ 418 __dlm_unlink_mle(dlm, mle); 419 420 /* detach the mle from the domain node up/down events */ 421 __dlm_mle_detach_hb_events(dlm, mle); 422 423 atomic_dec(&dlm->mle_cur_count[mle->type]); 424 425 /* NOTE: kfree under spinlock here. 426 * if this is bad, we can move this to a freelist. */ 427 kmem_cache_free(dlm_mle_cache, mle); 428 } 429 430 431 /* 432 * LOCK RESOURCE FUNCTIONS 433 */ 434 435 int dlm_init_master_caches(void) 436 { 437 dlm_lockres_cache = kmem_cache_create("o2dlm_lockres", 438 sizeof(struct dlm_lock_resource), 439 0, SLAB_HWCACHE_ALIGN, NULL); 440 if (!dlm_lockres_cache) 441 goto bail; 442 443 dlm_lockname_cache = kmem_cache_create("o2dlm_lockname", 444 DLM_LOCKID_NAME_MAX, 0, 445 SLAB_HWCACHE_ALIGN, NULL); 446 if (!dlm_lockname_cache) 447 goto bail; 448 449 return 0; 450 bail: 451 dlm_destroy_master_caches(); 452 return -ENOMEM; 453 } 454 455 void dlm_destroy_master_caches(void) 456 { 457 kmem_cache_destroy(dlm_lockname_cache); 458 dlm_lockname_cache = NULL; 459 460 kmem_cache_destroy(dlm_lockres_cache); 461 dlm_lockres_cache = NULL; 462 } 463 464 static void dlm_lockres_release(struct kref *kref) 465 { 466 struct dlm_lock_resource *res; 467 struct dlm_ctxt *dlm; 468 469 res = container_of(kref, struct dlm_lock_resource, refs); 470 dlm = res->dlm; 471 472 /* This should not happen -- all lockres' have a name 473 * associated with them at init time. */ 474 BUG_ON(!res->lockname.name); 475 476 mlog(0, "destroying lockres %.*s\n", res->lockname.len, 477 res->lockname.name); 478 479 atomic_dec(&dlm->res_cur_count); 480 481 if (!hlist_unhashed(&res->hash_node) || 482 !list_empty(&res->granted) || 483 !list_empty(&res->converting) || 484 !list_empty(&res->blocked) || 485 !list_empty(&res->dirty) || 486 !list_empty(&res->recovering) || 487 !list_empty(&res->purge)) { 488 mlog(ML_ERROR, 489 "Going to BUG for resource %.*s." 490 " We're on a list! [%c%c%c%c%c%c%c]\n", 491 res->lockname.len, res->lockname.name, 492 !hlist_unhashed(&res->hash_node) ? 'H' : ' ', 493 !list_empty(&res->granted) ? 'G' : ' ', 494 !list_empty(&res->converting) ? 'C' : ' ', 495 !list_empty(&res->blocked) ? 'B' : ' ', 496 !list_empty(&res->dirty) ? 'D' : ' ', 497 !list_empty(&res->recovering) ? 'R' : ' ', 498 !list_empty(&res->purge) ? 'P' : ' '); 499 500 dlm_print_one_lock_resource(res); 501 } 502 503 /* By the time we're ready to blow this guy away, we shouldn't 504 * be on any lists. */ 505 BUG_ON(!hlist_unhashed(&res->hash_node)); 506 BUG_ON(!list_empty(&res->granted)); 507 BUG_ON(!list_empty(&res->converting)); 508 BUG_ON(!list_empty(&res->blocked)); 509 BUG_ON(!list_empty(&res->dirty)); 510 BUG_ON(!list_empty(&res->recovering)); 511 BUG_ON(!list_empty(&res->purge)); 512 513 kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name); 514 515 kmem_cache_free(dlm_lockres_cache, res); 516 } 517 518 void dlm_lockres_put(struct dlm_lock_resource *res) 519 { 520 kref_put(&res->refs, dlm_lockres_release); 521 } 522 523 static void dlm_init_lockres(struct dlm_ctxt *dlm, 524 struct dlm_lock_resource *res, 525 const char *name, unsigned int namelen) 526 { 527 char *qname; 528 529 /* If we memset here, we lose our reference to the kmalloc'd 530 * res->lockname.name, so be sure to init every field 531 * correctly! */ 532 533 qname = (char *) res->lockname.name; 534 memcpy(qname, name, namelen); 535 536 res->lockname.len = namelen; 537 res->lockname.hash = dlm_lockid_hash(name, namelen); 538 539 init_waitqueue_head(&res->wq); 540 spin_lock_init(&res->spinlock); 541 INIT_HLIST_NODE(&res->hash_node); 542 INIT_LIST_HEAD(&res->granted); 543 INIT_LIST_HEAD(&res->converting); 544 INIT_LIST_HEAD(&res->blocked); 545 INIT_LIST_HEAD(&res->dirty); 546 INIT_LIST_HEAD(&res->recovering); 547 INIT_LIST_HEAD(&res->purge); 548 INIT_LIST_HEAD(&res->tracking); 549 atomic_set(&res->asts_reserved, 0); 550 res->migration_pending = 0; 551 res->inflight_locks = 0; 552 res->inflight_assert_workers = 0; 553 554 res->dlm = dlm; 555 556 kref_init(&res->refs); 557 558 atomic_inc(&dlm->res_tot_count); 559 atomic_inc(&dlm->res_cur_count); 560 561 /* just for consistency */ 562 spin_lock(&res->spinlock); 563 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN); 564 spin_unlock(&res->spinlock); 565 566 res->state = DLM_LOCK_RES_IN_PROGRESS; 567 568 res->last_used = 0; 569 570 spin_lock(&dlm->track_lock); 571 list_add_tail(&res->tracking, &dlm->tracking_list); 572 spin_unlock(&dlm->track_lock); 573 574 memset(res->lvb, 0, DLM_LVB_LEN); 575 bitmap_zero(res->refmap, O2NM_MAX_NODES); 576 } 577 578 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm, 579 const char *name, 580 unsigned int namelen) 581 { 582 struct dlm_lock_resource *res = NULL; 583 584 res = kmem_cache_zalloc(dlm_lockres_cache, GFP_NOFS); 585 if (!res) 586 goto error; 587 588 res->lockname.name = kmem_cache_zalloc(dlm_lockname_cache, GFP_NOFS); 589 if (!res->lockname.name) 590 goto error; 591 592 dlm_init_lockres(dlm, res, name, namelen); 593 return res; 594 595 error: 596 if (res) 597 kmem_cache_free(dlm_lockres_cache, res); 598 return NULL; 599 } 600 601 void dlm_lockres_set_refmap_bit(struct dlm_ctxt *dlm, 602 struct dlm_lock_resource *res, int bit) 603 { 604 assert_spin_locked(&res->spinlock); 605 606 mlog(0, "res %.*s, set node %u, %ps()\n", res->lockname.len, 607 res->lockname.name, bit, __builtin_return_address(0)); 608 609 set_bit(bit, res->refmap); 610 } 611 612 void dlm_lockres_clear_refmap_bit(struct dlm_ctxt *dlm, 613 struct dlm_lock_resource *res, int bit) 614 { 615 assert_spin_locked(&res->spinlock); 616 617 mlog(0, "res %.*s, clr node %u, %ps()\n", res->lockname.len, 618 res->lockname.name, bit, __builtin_return_address(0)); 619 620 clear_bit(bit, res->refmap); 621 } 622 623 static void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm, 624 struct dlm_lock_resource *res) 625 { 626 res->inflight_locks++; 627 628 mlog(0, "%s: res %.*s, inflight++: now %u, %ps()\n", dlm->name, 629 res->lockname.len, res->lockname.name, res->inflight_locks, 630 __builtin_return_address(0)); 631 } 632 633 void dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm, 634 struct dlm_lock_resource *res) 635 { 636 assert_spin_locked(&res->spinlock); 637 __dlm_lockres_grab_inflight_ref(dlm, res); 638 } 639 640 void dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm, 641 struct dlm_lock_resource *res) 642 { 643 assert_spin_locked(&res->spinlock); 644 645 BUG_ON(res->inflight_locks == 0); 646 647 res->inflight_locks--; 648 649 mlog(0, "%s: res %.*s, inflight--: now %u, %ps()\n", dlm->name, 650 res->lockname.len, res->lockname.name, res->inflight_locks, 651 __builtin_return_address(0)); 652 653 wake_up(&res->wq); 654 } 655 656 void __dlm_lockres_grab_inflight_worker(struct dlm_ctxt *dlm, 657 struct dlm_lock_resource *res) 658 { 659 assert_spin_locked(&res->spinlock); 660 res->inflight_assert_workers++; 661 mlog(0, "%s:%.*s: inflight assert worker++: now %u\n", 662 dlm->name, res->lockname.len, res->lockname.name, 663 res->inflight_assert_workers); 664 } 665 666 static void __dlm_lockres_drop_inflight_worker(struct dlm_ctxt *dlm, 667 struct dlm_lock_resource *res) 668 { 669 assert_spin_locked(&res->spinlock); 670 BUG_ON(res->inflight_assert_workers == 0); 671 res->inflight_assert_workers--; 672 mlog(0, "%s:%.*s: inflight assert worker--: now %u\n", 673 dlm->name, res->lockname.len, res->lockname.name, 674 res->inflight_assert_workers); 675 } 676 677 static void dlm_lockres_drop_inflight_worker(struct dlm_ctxt *dlm, 678 struct dlm_lock_resource *res) 679 { 680 spin_lock(&res->spinlock); 681 __dlm_lockres_drop_inflight_worker(dlm, res); 682 spin_unlock(&res->spinlock); 683 } 684 685 /* 686 * lookup a lock resource by name. 687 * may already exist in the hashtable. 688 * lockid is null terminated 689 * 690 * if not, allocate enough for the lockres and for 691 * the temporary structure used in doing the mastering. 692 * 693 * also, do a lookup in the dlm->master_list to see 694 * if another node has begun mastering the same lock. 695 * if so, there should be a block entry in there 696 * for this name, and we should *not* attempt to master 697 * the lock here. need to wait around for that node 698 * to assert_master (or die). 699 * 700 */ 701 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm, 702 const char *lockid, 703 int namelen, 704 int flags) 705 { 706 struct dlm_lock_resource *tmpres=NULL, *res=NULL; 707 struct dlm_master_list_entry *mle = NULL; 708 struct dlm_master_list_entry *alloc_mle = NULL; 709 int blocked = 0; 710 int ret, nodenum; 711 struct dlm_node_iter iter; 712 unsigned int hash; 713 int tries = 0; 714 int bit, wait_on_recovery = 0; 715 716 BUG_ON(!lockid); 717 718 hash = dlm_lockid_hash(lockid, namelen); 719 720 mlog(0, "get lockres %s (len %d)\n", lockid, namelen); 721 722 lookup: 723 spin_lock(&dlm->spinlock); 724 tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash); 725 if (tmpres) { 726 spin_unlock(&dlm->spinlock); 727 spin_lock(&tmpres->spinlock); 728 729 /* 730 * Right after dlm spinlock was released, dlm_thread could have 731 * purged the lockres. Check if lockres got unhashed. If so 732 * start over. 733 */ 734 if (hlist_unhashed(&tmpres->hash_node)) { 735 spin_unlock(&tmpres->spinlock); 736 dlm_lockres_put(tmpres); 737 tmpres = NULL; 738 goto lookup; 739 } 740 741 /* Wait on the thread that is mastering the resource */ 742 if (tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN) { 743 __dlm_wait_on_lockres(tmpres); 744 BUG_ON(tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN); 745 spin_unlock(&tmpres->spinlock); 746 dlm_lockres_put(tmpres); 747 tmpres = NULL; 748 goto lookup; 749 } 750 751 /* Wait on the resource purge to complete before continuing */ 752 if (tmpres->state & DLM_LOCK_RES_DROPPING_REF) { 753 BUG_ON(tmpres->owner == dlm->node_num); 754 __dlm_wait_on_lockres_flags(tmpres, 755 DLM_LOCK_RES_DROPPING_REF); 756 spin_unlock(&tmpres->spinlock); 757 dlm_lockres_put(tmpres); 758 tmpres = NULL; 759 goto lookup; 760 } 761 762 /* Grab inflight ref to pin the resource */ 763 dlm_lockres_grab_inflight_ref(dlm, tmpres); 764 765 spin_unlock(&tmpres->spinlock); 766 if (res) { 767 spin_lock(&dlm->track_lock); 768 if (!list_empty(&res->tracking)) 769 list_del_init(&res->tracking); 770 else 771 mlog(ML_ERROR, "Resource %.*s not " 772 "on the Tracking list\n", 773 res->lockname.len, 774 res->lockname.name); 775 spin_unlock(&dlm->track_lock); 776 dlm_lockres_put(res); 777 } 778 res = tmpres; 779 goto leave; 780 } 781 782 if (!res) { 783 spin_unlock(&dlm->spinlock); 784 mlog(0, "allocating a new resource\n"); 785 /* nothing found and we need to allocate one. */ 786 alloc_mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS); 787 if (!alloc_mle) 788 goto leave; 789 res = dlm_new_lockres(dlm, lockid, namelen); 790 if (!res) 791 goto leave; 792 goto lookup; 793 } 794 795 mlog(0, "no lockres found, allocated our own: %p\n", res); 796 797 if (flags & LKM_LOCAL) { 798 /* caller knows it's safe to assume it's not mastered elsewhere 799 * DONE! return right away */ 800 spin_lock(&res->spinlock); 801 dlm_change_lockres_owner(dlm, res, dlm->node_num); 802 __dlm_insert_lockres(dlm, res); 803 dlm_lockres_grab_inflight_ref(dlm, res); 804 spin_unlock(&res->spinlock); 805 spin_unlock(&dlm->spinlock); 806 /* lockres still marked IN_PROGRESS */ 807 goto wake_waiters; 808 } 809 810 /* check master list to see if another node has started mastering it */ 811 spin_lock(&dlm->master_lock); 812 813 /* if we found a block, wait for lock to be mastered by another node */ 814 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen); 815 if (blocked) { 816 int mig; 817 if (mle->type == DLM_MLE_MASTER) { 818 mlog(ML_ERROR, "master entry for nonexistent lock!\n"); 819 BUG(); 820 } 821 mig = (mle->type == DLM_MLE_MIGRATION); 822 /* if there is a migration in progress, let the migration 823 * finish before continuing. we can wait for the absence 824 * of the MIGRATION mle: either the migrate finished or 825 * one of the nodes died and the mle was cleaned up. 826 * if there is a BLOCK here, but it already has a master 827 * set, we are too late. the master does not have a ref 828 * for us in the refmap. detach the mle and drop it. 829 * either way, go back to the top and start over. */ 830 if (mig || mle->master != O2NM_MAX_NODES) { 831 BUG_ON(mig && mle->master == dlm->node_num); 832 /* we arrived too late. the master does not 833 * have a ref for us. retry. */ 834 mlog(0, "%s:%.*s: late on %s\n", 835 dlm->name, namelen, lockid, 836 mig ? "MIGRATION" : "BLOCK"); 837 spin_unlock(&dlm->master_lock); 838 spin_unlock(&dlm->spinlock); 839 840 /* master is known, detach */ 841 if (!mig) 842 dlm_mle_detach_hb_events(dlm, mle); 843 dlm_put_mle(mle); 844 mle = NULL; 845 /* this is lame, but we can't wait on either 846 * the mle or lockres waitqueue here */ 847 if (mig) 848 msleep(100); 849 goto lookup; 850 } 851 } else { 852 /* go ahead and try to master lock on this node */ 853 mle = alloc_mle; 854 /* make sure this does not get freed below */ 855 alloc_mle = NULL; 856 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0); 857 set_bit(dlm->node_num, mle->maybe_map); 858 __dlm_insert_mle(dlm, mle); 859 860 /* still holding the dlm spinlock, check the recovery map 861 * to see if there are any nodes that still need to be 862 * considered. these will not appear in the mle nodemap 863 * but they might own this lockres. wait on them. */ 864 bit = find_first_bit(dlm->recovery_map, O2NM_MAX_NODES); 865 if (bit < O2NM_MAX_NODES) { 866 mlog(0, "%s: res %.*s, At least one node (%d) " 867 "to recover before lock mastery can begin\n", 868 dlm->name, namelen, (char *)lockid, bit); 869 wait_on_recovery = 1; 870 } 871 } 872 873 /* at this point there is either a DLM_MLE_BLOCK or a 874 * DLM_MLE_MASTER on the master list, so it's safe to add the 875 * lockres to the hashtable. anyone who finds the lock will 876 * still have to wait on the IN_PROGRESS. */ 877 878 /* finally add the lockres to its hash bucket */ 879 __dlm_insert_lockres(dlm, res); 880 881 /* since this lockres is new it doesn't not require the spinlock */ 882 __dlm_lockres_grab_inflight_ref(dlm, res); 883 884 /* get an extra ref on the mle in case this is a BLOCK 885 * if so, the creator of the BLOCK may try to put the last 886 * ref at this time in the assert master handler, so we 887 * need an extra one to keep from a bad ptr deref. */ 888 dlm_get_mle_inuse(mle); 889 spin_unlock(&dlm->master_lock); 890 spin_unlock(&dlm->spinlock); 891 892 redo_request: 893 while (wait_on_recovery) { 894 /* any cluster changes that occurred after dropping the 895 * dlm spinlock would be detectable be a change on the mle, 896 * so we only need to clear out the recovery map once. */ 897 if (dlm_is_recovery_lock(lockid, namelen)) { 898 mlog(0, "%s: Recovery map is not empty, but must " 899 "master $RECOVERY lock now\n", dlm->name); 900 if (!dlm_pre_master_reco_lockres(dlm, res)) 901 wait_on_recovery = 0; 902 else { 903 mlog(0, "%s: waiting 500ms for heartbeat state " 904 "change\n", dlm->name); 905 msleep(500); 906 } 907 continue; 908 } 909 910 dlm_kick_recovery_thread(dlm); 911 msleep(1000); 912 dlm_wait_for_recovery(dlm); 913 914 spin_lock(&dlm->spinlock); 915 bit = find_first_bit(dlm->recovery_map, O2NM_MAX_NODES); 916 if (bit < O2NM_MAX_NODES) { 917 mlog(0, "%s: res %.*s, At least one node (%d) " 918 "to recover before lock mastery can begin\n", 919 dlm->name, namelen, (char *)lockid, bit); 920 wait_on_recovery = 1; 921 } else 922 wait_on_recovery = 0; 923 spin_unlock(&dlm->spinlock); 924 925 if (wait_on_recovery) 926 dlm_wait_for_node_recovery(dlm, bit, 10000); 927 } 928 929 /* must wait for lock to be mastered elsewhere */ 930 if (blocked) 931 goto wait; 932 933 dlm_node_iter_init(mle->vote_map, &iter); 934 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) { 935 ret = dlm_do_master_request(res, mle, nodenum); 936 if (ret < 0) 937 mlog_errno(ret); 938 if (mle->master != O2NM_MAX_NODES) { 939 /* found a master ! */ 940 if (mle->master <= nodenum) 941 break; 942 /* if our master request has not reached the master 943 * yet, keep going until it does. this is how the 944 * master will know that asserts are needed back to 945 * the lower nodes. */ 946 mlog(0, "%s: res %.*s, Requests only up to %u but " 947 "master is %u, keep going\n", dlm->name, namelen, 948 lockid, nodenum, mle->master); 949 } 950 } 951 952 wait: 953 /* keep going until the response map includes all nodes */ 954 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked); 955 if (ret < 0) { 956 wait_on_recovery = 1; 957 mlog(0, "%s: res %.*s, Node map changed, redo the master " 958 "request now, blocked=%d\n", dlm->name, res->lockname.len, 959 res->lockname.name, blocked); 960 if (++tries > 20) { 961 mlog(ML_ERROR, "%s: res %.*s, Spinning on " 962 "dlm_wait_for_lock_mastery, blocked = %d\n", 963 dlm->name, res->lockname.len, 964 res->lockname.name, blocked); 965 dlm_print_one_lock_resource(res); 966 dlm_print_one_mle(mle); 967 tries = 0; 968 } 969 goto redo_request; 970 } 971 972 mlog(0, "%s: res %.*s, Mastered by %u\n", dlm->name, res->lockname.len, 973 res->lockname.name, res->owner); 974 /* make sure we never continue without this */ 975 BUG_ON(res->owner == O2NM_MAX_NODES); 976 977 /* master is known, detach if not already detached */ 978 dlm_mle_detach_hb_events(dlm, mle); 979 dlm_put_mle(mle); 980 /* put the extra ref */ 981 dlm_put_mle_inuse(mle); 982 983 wake_waiters: 984 spin_lock(&res->spinlock); 985 res->state &= ~DLM_LOCK_RES_IN_PROGRESS; 986 spin_unlock(&res->spinlock); 987 wake_up(&res->wq); 988 989 leave: 990 /* need to free the unused mle */ 991 if (alloc_mle) 992 kmem_cache_free(dlm_mle_cache, alloc_mle); 993 994 return res; 995 } 996 997 998 #define DLM_MASTERY_TIMEOUT_MS 5000 999 1000 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm, 1001 struct dlm_lock_resource *res, 1002 struct dlm_master_list_entry *mle, 1003 int *blocked) 1004 { 1005 u8 m; 1006 int ret, bit; 1007 int map_changed, voting_done; 1008 int assert, sleep; 1009 1010 recheck: 1011 ret = 0; 1012 assert = 0; 1013 1014 /* check if another node has already become the owner */ 1015 spin_lock(&res->spinlock); 1016 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) { 1017 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name, 1018 res->lockname.len, res->lockname.name, res->owner); 1019 spin_unlock(&res->spinlock); 1020 /* this will cause the master to re-assert across 1021 * the whole cluster, freeing up mles */ 1022 if (res->owner != dlm->node_num) { 1023 ret = dlm_do_master_request(res, mle, res->owner); 1024 if (ret < 0) { 1025 /* give recovery a chance to run */ 1026 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret); 1027 msleep(500); 1028 goto recheck; 1029 } 1030 } 1031 ret = 0; 1032 goto leave; 1033 } 1034 spin_unlock(&res->spinlock); 1035 1036 spin_lock(&mle->spinlock); 1037 m = mle->master; 1038 map_changed = !bitmap_equal(mle->vote_map, mle->node_map, 1039 O2NM_MAX_NODES); 1040 voting_done = bitmap_equal(mle->vote_map, mle->response_map, 1041 O2NM_MAX_NODES); 1042 1043 /* restart if we hit any errors */ 1044 if (map_changed) { 1045 int b; 1046 mlog(0, "%s: %.*s: node map changed, restarting\n", 1047 dlm->name, res->lockname.len, res->lockname.name); 1048 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked); 1049 b = (mle->type == DLM_MLE_BLOCK); 1050 if ((*blocked && !b) || (!*blocked && b)) { 1051 mlog(0, "%s:%.*s: status change: old=%d new=%d\n", 1052 dlm->name, res->lockname.len, res->lockname.name, 1053 *blocked, b); 1054 *blocked = b; 1055 } 1056 spin_unlock(&mle->spinlock); 1057 if (ret < 0) { 1058 mlog_errno(ret); 1059 goto leave; 1060 } 1061 mlog(0, "%s:%.*s: restart lock mastery succeeded, " 1062 "rechecking now\n", dlm->name, res->lockname.len, 1063 res->lockname.name); 1064 goto recheck; 1065 } else { 1066 if (!voting_done) { 1067 mlog(0, "map not changed and voting not done " 1068 "for %s:%.*s\n", dlm->name, res->lockname.len, 1069 res->lockname.name); 1070 } 1071 } 1072 1073 if (m != O2NM_MAX_NODES) { 1074 /* another node has done an assert! 1075 * all done! */ 1076 sleep = 0; 1077 } else { 1078 sleep = 1; 1079 /* have all nodes responded? */ 1080 if (voting_done && !*blocked) { 1081 bit = find_first_bit(mle->maybe_map, O2NM_MAX_NODES); 1082 if (dlm->node_num <= bit) { 1083 /* my node number is lowest. 1084 * now tell other nodes that I am 1085 * mastering this. */ 1086 mle->master = dlm->node_num; 1087 /* ref was grabbed in get_lock_resource 1088 * will be dropped in dlmlock_master */ 1089 assert = 1; 1090 sleep = 0; 1091 } 1092 /* if voting is done, but we have not received 1093 * an assert master yet, we must sleep */ 1094 } 1095 } 1096 1097 spin_unlock(&mle->spinlock); 1098 1099 /* sleep if we haven't finished voting yet */ 1100 if (sleep) { 1101 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS); 1102 atomic_set(&mle->woken, 0); 1103 (void)wait_event_timeout(mle->wq, 1104 (atomic_read(&mle->woken) == 1), 1105 timeo); 1106 if (res->owner == O2NM_MAX_NODES) { 1107 mlog(0, "%s:%.*s: waiting again\n", dlm->name, 1108 res->lockname.len, res->lockname.name); 1109 goto recheck; 1110 } 1111 mlog(0, "done waiting, master is %u\n", res->owner); 1112 ret = 0; 1113 goto leave; 1114 } 1115 1116 ret = 0; /* done */ 1117 if (assert) { 1118 m = dlm->node_num; 1119 mlog(0, "about to master %.*s here, this=%u\n", 1120 res->lockname.len, res->lockname.name, m); 1121 ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0); 1122 if (ret) { 1123 /* This is a failure in the network path, 1124 * not in the response to the assert_master 1125 * (any nonzero response is a BUG on this node). 1126 * Most likely a socket just got disconnected 1127 * due to node death. */ 1128 mlog_errno(ret); 1129 } 1130 /* no longer need to restart lock mastery. 1131 * all living nodes have been contacted. */ 1132 ret = 0; 1133 } 1134 1135 /* set the lockres owner */ 1136 spin_lock(&res->spinlock); 1137 /* mastery reference obtained either during 1138 * assert_master_handler or in get_lock_resource */ 1139 dlm_change_lockres_owner(dlm, res, m); 1140 spin_unlock(&res->spinlock); 1141 1142 leave: 1143 return ret; 1144 } 1145 1146 struct dlm_bitmap_diff_iter 1147 { 1148 int curnode; 1149 unsigned long *orig_bm; 1150 unsigned long *cur_bm; 1151 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)]; 1152 }; 1153 1154 enum dlm_node_state_change 1155 { 1156 NODE_DOWN = -1, 1157 NODE_NO_CHANGE = 0, 1158 NODE_UP 1159 }; 1160 1161 static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter, 1162 unsigned long *orig_bm, 1163 unsigned long *cur_bm) 1164 { 1165 unsigned long p1, p2; 1166 int i; 1167 1168 iter->curnode = -1; 1169 iter->orig_bm = orig_bm; 1170 iter->cur_bm = cur_bm; 1171 1172 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) { 1173 p1 = *(iter->orig_bm + i); 1174 p2 = *(iter->cur_bm + i); 1175 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1); 1176 } 1177 } 1178 1179 static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter, 1180 enum dlm_node_state_change *state) 1181 { 1182 int bit; 1183 1184 if (iter->curnode >= O2NM_MAX_NODES) 1185 return -ENOENT; 1186 1187 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES, 1188 iter->curnode+1); 1189 if (bit >= O2NM_MAX_NODES) { 1190 iter->curnode = O2NM_MAX_NODES; 1191 return -ENOENT; 1192 } 1193 1194 /* if it was there in the original then this node died */ 1195 if (test_bit(bit, iter->orig_bm)) 1196 *state = NODE_DOWN; 1197 else 1198 *state = NODE_UP; 1199 1200 iter->curnode = bit; 1201 return bit; 1202 } 1203 1204 1205 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm, 1206 struct dlm_lock_resource *res, 1207 struct dlm_master_list_entry *mle, 1208 int blocked) 1209 { 1210 struct dlm_bitmap_diff_iter bdi; 1211 enum dlm_node_state_change sc; 1212 int node; 1213 int ret = 0; 1214 1215 mlog(0, "something happened such that the " 1216 "master process may need to be restarted!\n"); 1217 1218 assert_spin_locked(&mle->spinlock); 1219 1220 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map); 1221 node = dlm_bitmap_diff_iter_next(&bdi, &sc); 1222 while (node >= 0) { 1223 if (sc == NODE_UP) { 1224 /* a node came up. clear any old vote from 1225 * the response map and set it in the vote map 1226 * then restart the mastery. */ 1227 mlog(ML_NOTICE, "node %d up while restarting\n", node); 1228 1229 /* redo the master request, but only for the new node */ 1230 mlog(0, "sending request to new node\n"); 1231 clear_bit(node, mle->response_map); 1232 set_bit(node, mle->vote_map); 1233 } else { 1234 mlog(ML_ERROR, "node down! %d\n", node); 1235 if (blocked) { 1236 int lowest = find_first_bit(mle->maybe_map, 1237 O2NM_MAX_NODES); 1238 1239 /* act like it was never there */ 1240 clear_bit(node, mle->maybe_map); 1241 1242 if (node == lowest) { 1243 mlog(0, "expected master %u died" 1244 " while this node was blocked " 1245 "waiting on it!\n", node); 1246 lowest = find_next_bit(mle->maybe_map, 1247 O2NM_MAX_NODES, 1248 lowest+1); 1249 if (lowest < O2NM_MAX_NODES) { 1250 mlog(0, "%s:%.*s:still " 1251 "blocked. waiting on %u " 1252 "now\n", dlm->name, 1253 res->lockname.len, 1254 res->lockname.name, 1255 lowest); 1256 } else { 1257 /* mle is an MLE_BLOCK, but 1258 * there is now nothing left to 1259 * block on. we need to return 1260 * all the way back out and try 1261 * again with an MLE_MASTER. 1262 * dlm_do_local_recovery_cleanup 1263 * has already run, so the mle 1264 * refcount is ok */ 1265 mlog(0, "%s:%.*s: no " 1266 "longer blocking. try to " 1267 "master this here\n", 1268 dlm->name, 1269 res->lockname.len, 1270 res->lockname.name); 1271 mle->type = DLM_MLE_MASTER; 1272 mle->mleres = res; 1273 } 1274 } 1275 } 1276 1277 /* now blank out everything, as if we had never 1278 * contacted anyone */ 1279 bitmap_zero(mle->maybe_map, O2NM_MAX_NODES); 1280 bitmap_zero(mle->response_map, O2NM_MAX_NODES); 1281 /* reset the vote_map to the current node_map */ 1282 bitmap_copy(mle->vote_map, mle->node_map, 1283 O2NM_MAX_NODES); 1284 /* put myself into the maybe map */ 1285 if (mle->type != DLM_MLE_BLOCK) 1286 set_bit(dlm->node_num, mle->maybe_map); 1287 } 1288 ret = -EAGAIN; 1289 node = dlm_bitmap_diff_iter_next(&bdi, &sc); 1290 } 1291 return ret; 1292 } 1293 1294 1295 /* 1296 * DLM_MASTER_REQUEST_MSG 1297 * 1298 * returns: 0 on success, 1299 * -errno on a network error 1300 * 1301 * on error, the caller should assume the target node is "dead" 1302 * 1303 */ 1304 1305 static int dlm_do_master_request(struct dlm_lock_resource *res, 1306 struct dlm_master_list_entry *mle, int to) 1307 { 1308 struct dlm_ctxt *dlm = mle->dlm; 1309 struct dlm_master_request request; 1310 int ret, response=0, resend; 1311 1312 memset(&request, 0, sizeof(request)); 1313 request.node_idx = dlm->node_num; 1314 1315 BUG_ON(mle->type == DLM_MLE_MIGRATION); 1316 1317 request.namelen = (u8)mle->mnamelen; 1318 memcpy(request.name, mle->mname, request.namelen); 1319 1320 again: 1321 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request, 1322 sizeof(request), to, &response); 1323 if (ret < 0) { 1324 if (ret == -ESRCH) { 1325 /* should never happen */ 1326 mlog(ML_ERROR, "TCP stack not ready!\n"); 1327 BUG(); 1328 } else if (ret == -EINVAL) { 1329 mlog(ML_ERROR, "bad args passed to o2net!\n"); 1330 BUG(); 1331 } else if (ret == -ENOMEM) { 1332 mlog(ML_ERROR, "out of memory while trying to send " 1333 "network message! retrying\n"); 1334 /* this is totally crude */ 1335 msleep(50); 1336 goto again; 1337 } else if (!dlm_is_host_down(ret)) { 1338 /* not a network error. bad. */ 1339 mlog_errno(ret); 1340 mlog(ML_ERROR, "unhandled error!"); 1341 BUG(); 1342 } 1343 /* all other errors should be network errors, 1344 * and likely indicate node death */ 1345 mlog(ML_ERROR, "link to %d went down!\n", to); 1346 goto out; 1347 } 1348 1349 ret = 0; 1350 resend = 0; 1351 spin_lock(&mle->spinlock); 1352 switch (response) { 1353 case DLM_MASTER_RESP_YES: 1354 set_bit(to, mle->response_map); 1355 mlog(0, "node %u is the master, response=YES\n", to); 1356 mlog(0, "%s:%.*s: master node %u now knows I have a " 1357 "reference\n", dlm->name, res->lockname.len, 1358 res->lockname.name, to); 1359 mle->master = to; 1360 break; 1361 case DLM_MASTER_RESP_NO: 1362 mlog(0, "node %u not master, response=NO\n", to); 1363 set_bit(to, mle->response_map); 1364 break; 1365 case DLM_MASTER_RESP_MAYBE: 1366 mlog(0, "node %u not master, response=MAYBE\n", to); 1367 set_bit(to, mle->response_map); 1368 set_bit(to, mle->maybe_map); 1369 break; 1370 case DLM_MASTER_RESP_ERROR: 1371 mlog(0, "node %u hit an error, resending\n", to); 1372 resend = 1; 1373 response = 0; 1374 break; 1375 default: 1376 mlog(ML_ERROR, "bad response! %u\n", response); 1377 BUG(); 1378 } 1379 spin_unlock(&mle->spinlock); 1380 if (resend) { 1381 /* this is also totally crude */ 1382 msleep(50); 1383 goto again; 1384 } 1385 1386 out: 1387 return ret; 1388 } 1389 1390 /* 1391 * locks that can be taken here: 1392 * dlm->spinlock 1393 * res->spinlock 1394 * mle->spinlock 1395 * dlm->master_list 1396 * 1397 * if possible, TRIM THIS DOWN!!! 1398 */ 1399 int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data, 1400 void **ret_data) 1401 { 1402 u8 response = DLM_MASTER_RESP_MAYBE; 1403 struct dlm_ctxt *dlm = data; 1404 struct dlm_lock_resource *res = NULL; 1405 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf; 1406 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL; 1407 char *name; 1408 unsigned int namelen, hash; 1409 int found, ret; 1410 int set_maybe; 1411 int dispatch_assert = 0; 1412 int dispatched = 0; 1413 1414 if (!dlm_grab(dlm)) 1415 return DLM_MASTER_RESP_NO; 1416 1417 if (!dlm_domain_fully_joined(dlm)) { 1418 response = DLM_MASTER_RESP_NO; 1419 goto send_response; 1420 } 1421 1422 name = request->name; 1423 namelen = request->namelen; 1424 hash = dlm_lockid_hash(name, namelen); 1425 1426 if (namelen > DLM_LOCKID_NAME_MAX) { 1427 response = DLM_IVBUFLEN; 1428 goto send_response; 1429 } 1430 1431 way_up_top: 1432 spin_lock(&dlm->spinlock); 1433 res = __dlm_lookup_lockres(dlm, name, namelen, hash); 1434 if (res) { 1435 spin_unlock(&dlm->spinlock); 1436 1437 /* take care of the easy cases up front */ 1438 spin_lock(&res->spinlock); 1439 1440 /* 1441 * Right after dlm spinlock was released, dlm_thread could have 1442 * purged the lockres. Check if lockres got unhashed. If so 1443 * start over. 1444 */ 1445 if (hlist_unhashed(&res->hash_node)) { 1446 spin_unlock(&res->spinlock); 1447 dlm_lockres_put(res); 1448 goto way_up_top; 1449 } 1450 1451 if (res->state & (DLM_LOCK_RES_RECOVERING| 1452 DLM_LOCK_RES_MIGRATING)) { 1453 spin_unlock(&res->spinlock); 1454 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is " 1455 "being recovered/migrated\n"); 1456 response = DLM_MASTER_RESP_ERROR; 1457 if (mle) 1458 kmem_cache_free(dlm_mle_cache, mle); 1459 goto send_response; 1460 } 1461 1462 if (res->owner == dlm->node_num) { 1463 dlm_lockres_set_refmap_bit(dlm, res, request->node_idx); 1464 spin_unlock(&res->spinlock); 1465 response = DLM_MASTER_RESP_YES; 1466 if (mle) 1467 kmem_cache_free(dlm_mle_cache, mle); 1468 1469 /* this node is the owner. 1470 * there is some extra work that needs to 1471 * happen now. the requesting node has 1472 * caused all nodes up to this one to 1473 * create mles. this node now needs to 1474 * go back and clean those up. */ 1475 dispatch_assert = 1; 1476 goto send_response; 1477 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) { 1478 spin_unlock(&res->spinlock); 1479 response = DLM_MASTER_RESP_NO; 1480 if (mle) 1481 kmem_cache_free(dlm_mle_cache, mle); 1482 goto send_response; 1483 } 1484 1485 /* ok, there is no owner. either this node is 1486 * being blocked, or it is actively trying to 1487 * master this lock. */ 1488 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) { 1489 mlog(ML_ERROR, "lock with no owner should be " 1490 "in-progress!\n"); 1491 BUG(); 1492 } 1493 1494 spin_lock(&dlm->master_lock); 1495 found = dlm_find_mle(dlm, &tmpmle, name, namelen); 1496 if (!found) { 1497 mlog(ML_ERROR, "no mle found for this lock!\n"); 1498 BUG(); 1499 } 1500 set_maybe = 1; 1501 spin_lock(&tmpmle->spinlock); 1502 if (tmpmle->type == DLM_MLE_BLOCK) { 1503 response = DLM_MASTER_RESP_NO; 1504 } else if (tmpmle->type == DLM_MLE_MIGRATION) { 1505 mlog(0, "node %u is master, but trying to migrate to " 1506 "node %u.\n", tmpmle->master, tmpmle->new_master); 1507 if (tmpmle->master == dlm->node_num) { 1508 mlog(ML_ERROR, "no owner on lockres, but this " 1509 "node is trying to migrate it to %u?!\n", 1510 tmpmle->new_master); 1511 BUG(); 1512 } else { 1513 /* the real master can respond on its own */ 1514 response = DLM_MASTER_RESP_NO; 1515 } 1516 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) { 1517 set_maybe = 0; 1518 if (tmpmle->master == dlm->node_num) { 1519 response = DLM_MASTER_RESP_YES; 1520 /* this node will be the owner. 1521 * go back and clean the mles on any 1522 * other nodes */ 1523 dispatch_assert = 1; 1524 dlm_lockres_set_refmap_bit(dlm, res, 1525 request->node_idx); 1526 } else 1527 response = DLM_MASTER_RESP_NO; 1528 } else { 1529 response = DLM_MASTER_RESP_MAYBE; 1530 } 1531 if (set_maybe) 1532 set_bit(request->node_idx, tmpmle->maybe_map); 1533 spin_unlock(&tmpmle->spinlock); 1534 1535 spin_unlock(&dlm->master_lock); 1536 spin_unlock(&res->spinlock); 1537 1538 /* keep the mle attached to heartbeat events */ 1539 dlm_put_mle(tmpmle); 1540 if (mle) 1541 kmem_cache_free(dlm_mle_cache, mle); 1542 goto send_response; 1543 } 1544 1545 /* 1546 * lockres doesn't exist on this node 1547 * if there is an MLE_BLOCK, return NO 1548 * if there is an MLE_MASTER, return MAYBE 1549 * otherwise, add an MLE_BLOCK, return NO 1550 */ 1551 spin_lock(&dlm->master_lock); 1552 found = dlm_find_mle(dlm, &tmpmle, name, namelen); 1553 if (!found) { 1554 /* this lockid has never been seen on this node yet */ 1555 if (!mle) { 1556 spin_unlock(&dlm->master_lock); 1557 spin_unlock(&dlm->spinlock); 1558 1559 mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS); 1560 if (!mle) { 1561 response = DLM_MASTER_RESP_ERROR; 1562 mlog_errno(-ENOMEM); 1563 goto send_response; 1564 } 1565 goto way_up_top; 1566 } 1567 1568 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen); 1569 set_bit(request->node_idx, mle->maybe_map); 1570 __dlm_insert_mle(dlm, mle); 1571 response = DLM_MASTER_RESP_NO; 1572 } else { 1573 spin_lock(&tmpmle->spinlock); 1574 if (tmpmle->master == dlm->node_num) { 1575 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n"); 1576 BUG(); 1577 } 1578 if (tmpmle->type == DLM_MLE_BLOCK) 1579 response = DLM_MASTER_RESP_NO; 1580 else if (tmpmle->type == DLM_MLE_MIGRATION) { 1581 mlog(0, "migration mle was found (%u->%u)\n", 1582 tmpmle->master, tmpmle->new_master); 1583 /* real master can respond on its own */ 1584 response = DLM_MASTER_RESP_NO; 1585 } else 1586 response = DLM_MASTER_RESP_MAYBE; 1587 set_bit(request->node_idx, tmpmle->maybe_map); 1588 spin_unlock(&tmpmle->spinlock); 1589 } 1590 spin_unlock(&dlm->master_lock); 1591 spin_unlock(&dlm->spinlock); 1592 1593 if (found) { 1594 /* keep the mle attached to heartbeat events */ 1595 dlm_put_mle(tmpmle); 1596 } 1597 send_response: 1598 /* 1599 * __dlm_lookup_lockres() grabbed a reference to this lockres. 1600 * The reference is released by dlm_assert_master_worker() under 1601 * the call to dlm_dispatch_assert_master(). If 1602 * dlm_assert_master_worker() isn't called, we drop it here. 1603 */ 1604 if (dispatch_assert) { 1605 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n", 1606 dlm->node_num, res->lockname.len, res->lockname.name); 1607 spin_lock(&res->spinlock); 1608 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx, 1609 DLM_ASSERT_MASTER_MLE_CLEANUP); 1610 if (ret < 0) { 1611 mlog(ML_ERROR, "failed to dispatch assert master work\n"); 1612 response = DLM_MASTER_RESP_ERROR; 1613 spin_unlock(&res->spinlock); 1614 dlm_lockres_put(res); 1615 } else { 1616 dispatched = 1; 1617 __dlm_lockres_grab_inflight_worker(dlm, res); 1618 spin_unlock(&res->spinlock); 1619 } 1620 } else { 1621 if (res) 1622 dlm_lockres_put(res); 1623 } 1624 1625 if (!dispatched) 1626 dlm_put(dlm); 1627 return response; 1628 } 1629 1630 /* 1631 * DLM_ASSERT_MASTER_MSG 1632 */ 1633 1634 1635 /* 1636 * NOTE: this can be used for debugging 1637 * can periodically run all locks owned by this node 1638 * and re-assert across the cluster... 1639 */ 1640 static int dlm_do_assert_master(struct dlm_ctxt *dlm, 1641 struct dlm_lock_resource *res, 1642 void *nodemap, u32 flags) 1643 { 1644 struct dlm_assert_master assert; 1645 int to, tmpret; 1646 struct dlm_node_iter iter; 1647 int ret = 0; 1648 int reassert; 1649 const char *lockname = res->lockname.name; 1650 unsigned int namelen = res->lockname.len; 1651 1652 BUG_ON(namelen > O2NM_MAX_NAME_LEN); 1653 1654 spin_lock(&res->spinlock); 1655 res->state |= DLM_LOCK_RES_SETREF_INPROG; 1656 spin_unlock(&res->spinlock); 1657 1658 again: 1659 reassert = 0; 1660 1661 /* note that if this nodemap is empty, it returns 0 */ 1662 dlm_node_iter_init(nodemap, &iter); 1663 while ((to = dlm_node_iter_next(&iter)) >= 0) { 1664 int r = 0; 1665 struct dlm_master_list_entry *mle = NULL; 1666 1667 mlog(0, "sending assert master to %d (%.*s)\n", to, 1668 namelen, lockname); 1669 memset(&assert, 0, sizeof(assert)); 1670 assert.node_idx = dlm->node_num; 1671 assert.namelen = namelen; 1672 memcpy(assert.name, lockname, namelen); 1673 assert.flags = cpu_to_be32(flags); 1674 1675 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key, 1676 &assert, sizeof(assert), to, &r); 1677 if (tmpret < 0) { 1678 mlog(ML_ERROR, "Error %d when sending message %u (key " 1679 "0x%x) to node %u\n", tmpret, 1680 DLM_ASSERT_MASTER_MSG, dlm->key, to); 1681 if (!dlm_is_host_down(tmpret)) { 1682 mlog(ML_ERROR, "unhandled error=%d!\n", tmpret); 1683 BUG(); 1684 } 1685 /* a node died. finish out the rest of the nodes. */ 1686 mlog(0, "link to %d went down!\n", to); 1687 /* any nonzero status return will do */ 1688 ret = tmpret; 1689 r = 0; 1690 } else if (r < 0) { 1691 /* ok, something horribly messed. kill thyself. */ 1692 mlog(ML_ERROR,"during assert master of %.*s to %u, " 1693 "got %d.\n", namelen, lockname, to, r); 1694 spin_lock(&dlm->spinlock); 1695 spin_lock(&dlm->master_lock); 1696 if (dlm_find_mle(dlm, &mle, (char *)lockname, 1697 namelen)) { 1698 dlm_print_one_mle(mle); 1699 __dlm_put_mle(mle); 1700 } 1701 spin_unlock(&dlm->master_lock); 1702 spin_unlock(&dlm->spinlock); 1703 BUG(); 1704 } 1705 1706 if (r & DLM_ASSERT_RESPONSE_REASSERT && 1707 !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) { 1708 mlog(ML_ERROR, "%.*s: very strange, " 1709 "master MLE but no lockres on %u\n", 1710 namelen, lockname, to); 1711 } 1712 1713 if (r & DLM_ASSERT_RESPONSE_REASSERT) { 1714 mlog(0, "%.*s: node %u create mles on other " 1715 "nodes and requests a re-assert\n", 1716 namelen, lockname, to); 1717 reassert = 1; 1718 } 1719 if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) { 1720 mlog(0, "%.*s: node %u has a reference to this " 1721 "lockres, set the bit in the refmap\n", 1722 namelen, lockname, to); 1723 spin_lock(&res->spinlock); 1724 dlm_lockres_set_refmap_bit(dlm, res, to); 1725 spin_unlock(&res->spinlock); 1726 } 1727 } 1728 1729 if (reassert) 1730 goto again; 1731 1732 spin_lock(&res->spinlock); 1733 res->state &= ~DLM_LOCK_RES_SETREF_INPROG; 1734 spin_unlock(&res->spinlock); 1735 wake_up(&res->wq); 1736 1737 return ret; 1738 } 1739 1740 /* 1741 * locks that can be taken here: 1742 * dlm->spinlock 1743 * res->spinlock 1744 * mle->spinlock 1745 * dlm->master_list 1746 * 1747 * if possible, TRIM THIS DOWN!!! 1748 */ 1749 int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data, 1750 void **ret_data) 1751 { 1752 struct dlm_ctxt *dlm = data; 1753 struct dlm_master_list_entry *mle = NULL; 1754 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf; 1755 struct dlm_lock_resource *res = NULL; 1756 char *name; 1757 unsigned int namelen, hash; 1758 u32 flags; 1759 int master_request = 0, have_lockres_ref = 0; 1760 int ret = 0; 1761 1762 if (!dlm_grab(dlm)) 1763 return 0; 1764 1765 name = assert->name; 1766 namelen = assert->namelen; 1767 hash = dlm_lockid_hash(name, namelen); 1768 flags = be32_to_cpu(assert->flags); 1769 1770 if (namelen > DLM_LOCKID_NAME_MAX) { 1771 mlog(ML_ERROR, "Invalid name length!"); 1772 goto done; 1773 } 1774 1775 spin_lock(&dlm->spinlock); 1776 1777 if (flags) 1778 mlog(0, "assert_master with flags: %u\n", flags); 1779 1780 /* find the MLE */ 1781 spin_lock(&dlm->master_lock); 1782 if (!dlm_find_mle(dlm, &mle, name, namelen)) { 1783 /* not an error, could be master just re-asserting */ 1784 mlog(0, "just got an assert_master from %u, but no " 1785 "MLE for it! (%.*s)\n", assert->node_idx, 1786 namelen, name); 1787 } else { 1788 int bit = find_first_bit(mle->maybe_map, O2NM_MAX_NODES); 1789 if (bit >= O2NM_MAX_NODES) { 1790 /* not necessarily an error, though less likely. 1791 * could be master just re-asserting. */ 1792 mlog(0, "no bits set in the maybe_map, but %u " 1793 "is asserting! (%.*s)\n", assert->node_idx, 1794 namelen, name); 1795 } else if (bit != assert->node_idx) { 1796 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) { 1797 mlog(0, "master %u was found, %u should " 1798 "back off\n", assert->node_idx, bit); 1799 } else { 1800 /* with the fix for bug 569, a higher node 1801 * number winning the mastery will respond 1802 * YES to mastery requests, but this node 1803 * had no way of knowing. let it pass. */ 1804 mlog(0, "%u is the lowest node, " 1805 "%u is asserting. (%.*s) %u must " 1806 "have begun after %u won.\n", bit, 1807 assert->node_idx, namelen, name, bit, 1808 assert->node_idx); 1809 } 1810 } 1811 if (mle->type == DLM_MLE_MIGRATION) { 1812 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) { 1813 mlog(0, "%s:%.*s: got cleanup assert" 1814 " from %u for migration\n", 1815 dlm->name, namelen, name, 1816 assert->node_idx); 1817 } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) { 1818 mlog(0, "%s:%.*s: got unrelated assert" 1819 " from %u for migration, ignoring\n", 1820 dlm->name, namelen, name, 1821 assert->node_idx); 1822 __dlm_put_mle(mle); 1823 spin_unlock(&dlm->master_lock); 1824 spin_unlock(&dlm->spinlock); 1825 goto done; 1826 } 1827 } 1828 } 1829 spin_unlock(&dlm->master_lock); 1830 1831 /* ok everything checks out with the MLE 1832 * now check to see if there is a lockres */ 1833 res = __dlm_lookup_lockres(dlm, name, namelen, hash); 1834 if (res) { 1835 spin_lock(&res->spinlock); 1836 if (res->state & DLM_LOCK_RES_RECOVERING) { 1837 mlog(ML_ERROR, "%u asserting but %.*s is " 1838 "RECOVERING!\n", assert->node_idx, namelen, name); 1839 goto kill; 1840 } 1841 if (!mle) { 1842 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN && 1843 res->owner != assert->node_idx) { 1844 mlog(ML_ERROR, "DIE! Mastery assert from %u, " 1845 "but current owner is %u! (%.*s)\n", 1846 assert->node_idx, res->owner, namelen, 1847 name); 1848 __dlm_print_one_lock_resource(res); 1849 BUG(); 1850 } 1851 } else if (mle->type != DLM_MLE_MIGRATION) { 1852 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) { 1853 /* owner is just re-asserting */ 1854 if (res->owner == assert->node_idx) { 1855 mlog(0, "owner %u re-asserting on " 1856 "lock %.*s\n", assert->node_idx, 1857 namelen, name); 1858 goto ok; 1859 } 1860 mlog(ML_ERROR, "got assert_master from " 1861 "node %u, but %u is the owner! " 1862 "(%.*s)\n", assert->node_idx, 1863 res->owner, namelen, name); 1864 goto kill; 1865 } 1866 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) { 1867 mlog(ML_ERROR, "got assert from %u, but lock " 1868 "with no owner should be " 1869 "in-progress! (%.*s)\n", 1870 assert->node_idx, 1871 namelen, name); 1872 goto kill; 1873 } 1874 } else /* mle->type == DLM_MLE_MIGRATION */ { 1875 /* should only be getting an assert from new master */ 1876 if (assert->node_idx != mle->new_master) { 1877 mlog(ML_ERROR, "got assert from %u, but " 1878 "new master is %u, and old master " 1879 "was %u (%.*s)\n", 1880 assert->node_idx, mle->new_master, 1881 mle->master, namelen, name); 1882 goto kill; 1883 } 1884 1885 } 1886 ok: 1887 spin_unlock(&res->spinlock); 1888 } 1889 1890 if (mle) { 1891 int extra_ref = 0; 1892 int nn = -1; 1893 int rr, err = 0; 1894 1895 spin_lock(&mle->spinlock); 1896 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION) 1897 extra_ref = 1; 1898 else { 1899 /* MASTER mle: if any bits set in the response map 1900 * then the calling node needs to re-assert to clear 1901 * up nodes that this node contacted */ 1902 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES, 1903 nn+1)) < O2NM_MAX_NODES) { 1904 if (nn != dlm->node_num && nn != assert->node_idx) { 1905 master_request = 1; 1906 break; 1907 } 1908 } 1909 } 1910 mle->master = assert->node_idx; 1911 atomic_set(&mle->woken, 1); 1912 wake_up(&mle->wq); 1913 spin_unlock(&mle->spinlock); 1914 1915 if (res) { 1916 int wake = 0; 1917 spin_lock(&res->spinlock); 1918 if (mle->type == DLM_MLE_MIGRATION) { 1919 mlog(0, "finishing off migration of lockres %.*s, " 1920 "from %u to %u\n", 1921 res->lockname.len, res->lockname.name, 1922 dlm->node_num, mle->new_master); 1923 res->state &= ~DLM_LOCK_RES_MIGRATING; 1924 wake = 1; 1925 dlm_change_lockres_owner(dlm, res, mle->new_master); 1926 BUG_ON(res->state & DLM_LOCK_RES_DIRTY); 1927 } else { 1928 dlm_change_lockres_owner(dlm, res, mle->master); 1929 } 1930 spin_unlock(&res->spinlock); 1931 have_lockres_ref = 1; 1932 if (wake) 1933 wake_up(&res->wq); 1934 } 1935 1936 /* master is known, detach if not already detached. 1937 * ensures that only one assert_master call will happen 1938 * on this mle. */ 1939 spin_lock(&dlm->master_lock); 1940 1941 rr = kref_read(&mle->mle_refs); 1942 if (mle->inuse > 0) { 1943 if (extra_ref && rr < 3) 1944 err = 1; 1945 else if (!extra_ref && rr < 2) 1946 err = 1; 1947 } else { 1948 if (extra_ref && rr < 2) 1949 err = 1; 1950 else if (!extra_ref && rr < 1) 1951 err = 1; 1952 } 1953 if (err) { 1954 mlog(ML_ERROR, "%s:%.*s: got assert master from %u " 1955 "that will mess up this node, refs=%d, extra=%d, " 1956 "inuse=%d\n", dlm->name, namelen, name, 1957 assert->node_idx, rr, extra_ref, mle->inuse); 1958 dlm_print_one_mle(mle); 1959 } 1960 __dlm_unlink_mle(dlm, mle); 1961 __dlm_mle_detach_hb_events(dlm, mle); 1962 __dlm_put_mle(mle); 1963 if (extra_ref) { 1964 /* the assert master message now balances the extra 1965 * ref given by the master / migration request message. 1966 * if this is the last put, it will be removed 1967 * from the list. */ 1968 __dlm_put_mle(mle); 1969 } 1970 spin_unlock(&dlm->master_lock); 1971 } else if (res) { 1972 if (res->owner != assert->node_idx) { 1973 mlog(0, "assert_master from %u, but current " 1974 "owner is %u (%.*s), no mle\n", assert->node_idx, 1975 res->owner, namelen, name); 1976 } 1977 } 1978 spin_unlock(&dlm->spinlock); 1979 1980 done: 1981 ret = 0; 1982 if (res) { 1983 spin_lock(&res->spinlock); 1984 res->state |= DLM_LOCK_RES_SETREF_INPROG; 1985 spin_unlock(&res->spinlock); 1986 *ret_data = (void *)res; 1987 } 1988 dlm_put(dlm); 1989 if (master_request) { 1990 mlog(0, "need to tell master to reassert\n"); 1991 /* positive. negative would shoot down the node. */ 1992 ret |= DLM_ASSERT_RESPONSE_REASSERT; 1993 if (!have_lockres_ref) { 1994 mlog(ML_ERROR, "strange, got assert from %u, MASTER " 1995 "mle present here for %s:%.*s, but no lockres!\n", 1996 assert->node_idx, dlm->name, namelen, name); 1997 } 1998 } 1999 if (have_lockres_ref) { 2000 /* let the master know we have a reference to the lockres */ 2001 ret |= DLM_ASSERT_RESPONSE_MASTERY_REF; 2002 mlog(0, "%s:%.*s: got assert from %u, need a ref\n", 2003 dlm->name, namelen, name, assert->node_idx); 2004 } 2005 return ret; 2006 2007 kill: 2008 /* kill the caller! */ 2009 mlog(ML_ERROR, "Bad message received from another node. Dumping state " 2010 "and killing the other node now! This node is OK and can continue.\n"); 2011 __dlm_print_one_lock_resource(res); 2012 spin_unlock(&res->spinlock); 2013 spin_lock(&dlm->master_lock); 2014 if (mle) 2015 __dlm_put_mle(mle); 2016 spin_unlock(&dlm->master_lock); 2017 spin_unlock(&dlm->spinlock); 2018 *ret_data = (void *)res; 2019 dlm_put(dlm); 2020 return -EINVAL; 2021 } 2022 2023 void dlm_assert_master_post_handler(int status, void *data, void *ret_data) 2024 { 2025 struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data; 2026 2027 if (ret_data) { 2028 spin_lock(&res->spinlock); 2029 res->state &= ~DLM_LOCK_RES_SETREF_INPROG; 2030 spin_unlock(&res->spinlock); 2031 wake_up(&res->wq); 2032 dlm_lockres_put(res); 2033 } 2034 return; 2035 } 2036 2037 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm, 2038 struct dlm_lock_resource *res, 2039 int ignore_higher, u8 request_from, u32 flags) 2040 { 2041 struct dlm_work_item *item; 2042 item = kzalloc_obj(*item, GFP_ATOMIC); 2043 if (!item) 2044 return -ENOMEM; 2045 2046 2047 /* queue up work for dlm_assert_master_worker */ 2048 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL); 2049 item->u.am.lockres = res; /* already have a ref */ 2050 /* can optionally ignore node numbers higher than this node */ 2051 item->u.am.ignore_higher = ignore_higher; 2052 item->u.am.request_from = request_from; 2053 item->u.am.flags = flags; 2054 2055 if (ignore_higher) 2056 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len, 2057 res->lockname.name); 2058 2059 spin_lock(&dlm->work_lock); 2060 list_add_tail(&item->list, &dlm->work_list); 2061 spin_unlock(&dlm->work_lock); 2062 2063 queue_work(dlm->dlm_worker, &dlm->dispatched_work); 2064 return 0; 2065 } 2066 2067 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data) 2068 { 2069 struct dlm_ctxt *dlm = data; 2070 int ret = 0; 2071 struct dlm_lock_resource *res; 2072 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)]; 2073 int ignore_higher; 2074 int bit; 2075 u8 request_from; 2076 u32 flags; 2077 2078 dlm = item->dlm; 2079 res = item->u.am.lockres; 2080 ignore_higher = item->u.am.ignore_higher; 2081 request_from = item->u.am.request_from; 2082 flags = item->u.am.flags; 2083 2084 spin_lock(&dlm->spinlock); 2085 bitmap_copy(nodemap, dlm->domain_map, O2NM_MAX_NODES); 2086 spin_unlock(&dlm->spinlock); 2087 2088 clear_bit(dlm->node_num, nodemap); 2089 if (ignore_higher) { 2090 /* if is this just to clear up mles for nodes below 2091 * this node, do not send the message to the original 2092 * caller or any node number higher than this */ 2093 clear_bit(request_from, nodemap); 2094 bit = dlm->node_num; 2095 while (1) { 2096 bit = find_next_bit(nodemap, O2NM_MAX_NODES, 2097 bit+1); 2098 if (bit >= O2NM_MAX_NODES) 2099 break; 2100 clear_bit(bit, nodemap); 2101 } 2102 } 2103 2104 /* 2105 * If we're migrating this lock to someone else, we are no 2106 * longer allowed to assert out own mastery. OTOH, we need to 2107 * prevent migration from starting while we're still asserting 2108 * our dominance. The reserved ast delays migration. 2109 */ 2110 spin_lock(&res->spinlock); 2111 if (res->state & DLM_LOCK_RES_MIGRATING) { 2112 mlog(0, "Someone asked us to assert mastery, but we're " 2113 "in the middle of migration. Skipping assert, " 2114 "the new master will handle that.\n"); 2115 spin_unlock(&res->spinlock); 2116 goto put; 2117 } else 2118 __dlm_lockres_reserve_ast(res); 2119 spin_unlock(&res->spinlock); 2120 2121 /* this call now finishes out the nodemap 2122 * even if one or more nodes die */ 2123 mlog(0, "worker about to master %.*s here, this=%u\n", 2124 res->lockname.len, res->lockname.name, dlm->node_num); 2125 ret = dlm_do_assert_master(dlm, res, nodemap, flags); 2126 if (ret < 0) { 2127 /* no need to restart, we are done */ 2128 if (!dlm_is_host_down(ret)) 2129 mlog_errno(ret); 2130 } 2131 2132 /* Ok, we've asserted ourselves. Let's let migration start. */ 2133 dlm_lockres_release_ast(dlm, res); 2134 2135 put: 2136 dlm_lockres_drop_inflight_worker(dlm, res); 2137 2138 dlm_lockres_put(res); 2139 2140 mlog(0, "finished with dlm_assert_master_worker\n"); 2141 } 2142 2143 /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread. 2144 * We cannot wait for node recovery to complete to begin mastering this 2145 * lockres because this lockres is used to kick off recovery! ;-) 2146 * So, do a pre-check on all living nodes to see if any of those nodes 2147 * think that $RECOVERY is currently mastered by a dead node. If so, 2148 * we wait a short time to allow that node to get notified by its own 2149 * heartbeat stack, then check again. All $RECOVERY lock resources 2150 * mastered by dead nodes are purged when the heartbeat callback is 2151 * fired, so we can know for sure that it is safe to continue once 2152 * the node returns a live node or no node. */ 2153 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm, 2154 struct dlm_lock_resource *res) 2155 { 2156 struct dlm_node_iter iter; 2157 int nodenum; 2158 int ret = 0; 2159 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN; 2160 2161 spin_lock(&dlm->spinlock); 2162 dlm_node_iter_init(dlm->domain_map, &iter); 2163 spin_unlock(&dlm->spinlock); 2164 2165 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) { 2166 /* do not send to self */ 2167 if (nodenum == dlm->node_num) 2168 continue; 2169 ret = dlm_do_master_requery(dlm, res, nodenum, &master); 2170 if (ret < 0) { 2171 mlog_errno(ret); 2172 if (!dlm_is_host_down(ret)) 2173 BUG(); 2174 /* host is down, so answer for that node would be 2175 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */ 2176 ret = 0; 2177 } 2178 2179 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) { 2180 /* check to see if this master is in the recovery map */ 2181 spin_lock(&dlm->spinlock); 2182 if (test_bit(master, dlm->recovery_map)) { 2183 mlog(ML_NOTICE, "%s: node %u has not seen " 2184 "node %u go down yet, and thinks the " 2185 "dead node is mastering the recovery " 2186 "lock. must wait.\n", dlm->name, 2187 nodenum, master); 2188 ret = -EAGAIN; 2189 } 2190 spin_unlock(&dlm->spinlock); 2191 mlog(0, "%s: reco lock master is %u\n", dlm->name, 2192 master); 2193 break; 2194 } 2195 } 2196 return ret; 2197 } 2198 2199 /* 2200 * DLM_DEREF_LOCKRES_MSG 2201 */ 2202 2203 int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) 2204 { 2205 struct dlm_deref_lockres deref; 2206 int ret = 0, r; 2207 const char *lockname; 2208 unsigned int namelen; 2209 2210 lockname = res->lockname.name; 2211 namelen = res->lockname.len; 2212 BUG_ON(namelen > O2NM_MAX_NAME_LEN); 2213 2214 memset(&deref, 0, sizeof(deref)); 2215 deref.node_idx = dlm->node_num; 2216 deref.namelen = namelen; 2217 memcpy(deref.name, lockname, namelen); 2218 2219 ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key, 2220 &deref, sizeof(deref), res->owner, &r); 2221 if (ret < 0) 2222 mlog(ML_ERROR, "%s: res %.*s, error %d send DEREF to node %u\n", 2223 dlm->name, namelen, lockname, ret, res->owner); 2224 else if (r < 0) { 2225 /* BAD. other node says I did not have a ref. */ 2226 mlog(ML_ERROR, "%s: res %.*s, DEREF to node %u got %d\n", 2227 dlm->name, namelen, lockname, res->owner, r); 2228 dlm_print_one_lock_resource(res); 2229 if (r == -ENOMEM) 2230 BUG(); 2231 } else 2232 ret = r; 2233 2234 return ret; 2235 } 2236 2237 int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data, 2238 void **ret_data) 2239 { 2240 struct dlm_ctxt *dlm = data; 2241 struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf; 2242 struct dlm_lock_resource *res = NULL; 2243 char *name; 2244 unsigned int namelen; 2245 int ret = -EINVAL; 2246 u8 node; 2247 unsigned int hash; 2248 struct dlm_work_item *item; 2249 int cleared = 0; 2250 int dispatch = 0; 2251 2252 if (!dlm_grab(dlm)) 2253 return 0; 2254 2255 name = deref->name; 2256 namelen = deref->namelen; 2257 node = deref->node_idx; 2258 2259 if (namelen > DLM_LOCKID_NAME_MAX) { 2260 mlog(ML_ERROR, "Invalid name length!"); 2261 goto done; 2262 } 2263 if (deref->node_idx >= O2NM_MAX_NODES) { 2264 mlog(ML_ERROR, "Invalid node number: %u\n", node); 2265 goto done; 2266 } 2267 2268 hash = dlm_lockid_hash(name, namelen); 2269 2270 spin_lock(&dlm->spinlock); 2271 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash); 2272 if (!res) { 2273 spin_unlock(&dlm->spinlock); 2274 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n", 2275 dlm->name, namelen, name); 2276 goto done; 2277 } 2278 spin_unlock(&dlm->spinlock); 2279 2280 spin_lock(&res->spinlock); 2281 if (res->state & DLM_LOCK_RES_SETREF_INPROG) 2282 dispatch = 1; 2283 else { 2284 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF); 2285 if (test_bit(node, res->refmap)) { 2286 dlm_lockres_clear_refmap_bit(dlm, res, node); 2287 cleared = 1; 2288 } 2289 } 2290 spin_unlock(&res->spinlock); 2291 2292 if (!dispatch) { 2293 if (cleared) 2294 dlm_lockres_calc_usage(dlm, res); 2295 else { 2296 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref " 2297 "but it is already dropped!\n", dlm->name, 2298 res->lockname.len, res->lockname.name, node); 2299 dlm_print_one_lock_resource(res); 2300 } 2301 ret = DLM_DEREF_RESPONSE_DONE; 2302 goto done; 2303 } 2304 2305 item = kzalloc_obj(*item, GFP_NOFS); 2306 if (!item) { 2307 ret = -ENOMEM; 2308 mlog_errno(ret); 2309 goto done; 2310 } 2311 2312 dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL); 2313 item->u.dl.deref_res = res; 2314 item->u.dl.deref_node = node; 2315 2316 spin_lock(&dlm->work_lock); 2317 list_add_tail(&item->list, &dlm->work_list); 2318 spin_unlock(&dlm->work_lock); 2319 2320 queue_work(dlm->dlm_worker, &dlm->dispatched_work); 2321 return DLM_DEREF_RESPONSE_INPROG; 2322 2323 done: 2324 if (res) 2325 dlm_lockres_put(res); 2326 dlm_put(dlm); 2327 2328 return ret; 2329 } 2330 2331 int dlm_deref_lockres_done_handler(struct o2net_msg *msg, u32 len, void *data, 2332 void **ret_data) 2333 { 2334 struct dlm_ctxt *dlm = data; 2335 struct dlm_deref_lockres_done *deref 2336 = (struct dlm_deref_lockres_done *)msg->buf; 2337 struct dlm_lock_resource *res = NULL; 2338 char *name; 2339 unsigned int namelen; 2340 int ret = -EINVAL; 2341 u8 node; 2342 unsigned int hash; 2343 2344 if (!dlm_grab(dlm)) 2345 return 0; 2346 2347 name = deref->name; 2348 namelen = deref->namelen; 2349 node = deref->node_idx; 2350 2351 if (namelen > DLM_LOCKID_NAME_MAX) { 2352 mlog(ML_ERROR, "Invalid name length!"); 2353 goto done; 2354 } 2355 if (deref->node_idx >= O2NM_MAX_NODES) { 2356 mlog(ML_ERROR, "Invalid node number: %u\n", node); 2357 goto done; 2358 } 2359 2360 hash = dlm_lockid_hash(name, namelen); 2361 2362 spin_lock(&dlm->spinlock); 2363 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash); 2364 if (!res) { 2365 spin_unlock(&dlm->spinlock); 2366 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n", 2367 dlm->name, namelen, name); 2368 goto done; 2369 } 2370 2371 spin_lock(&res->spinlock); 2372 if (!(res->state & DLM_LOCK_RES_DROPPING_REF)) { 2373 spin_unlock(&res->spinlock); 2374 spin_unlock(&dlm->spinlock); 2375 mlog(ML_NOTICE, "%s:%.*s: node %u sends deref done " 2376 "but it is already derefed!\n", dlm->name, 2377 res->lockname.len, res->lockname.name, node); 2378 ret = 0; 2379 goto done; 2380 } 2381 2382 __dlm_do_purge_lockres(dlm, res); 2383 spin_unlock(&res->spinlock); 2384 wake_up(&res->wq); 2385 2386 spin_unlock(&dlm->spinlock); 2387 2388 ret = 0; 2389 done: 2390 if (res) 2391 dlm_lockres_put(res); 2392 dlm_put(dlm); 2393 return ret; 2394 } 2395 2396 static void dlm_drop_lockres_ref_done(struct dlm_ctxt *dlm, 2397 struct dlm_lock_resource *res, u8 node) 2398 { 2399 struct dlm_deref_lockres_done deref; 2400 int ret = 0, r; 2401 const char *lockname; 2402 unsigned int namelen; 2403 2404 lockname = res->lockname.name; 2405 namelen = res->lockname.len; 2406 BUG_ON(namelen > O2NM_MAX_NAME_LEN); 2407 2408 memset(&deref, 0, sizeof(deref)); 2409 deref.node_idx = dlm->node_num; 2410 deref.namelen = namelen; 2411 memcpy(deref.name, lockname, namelen); 2412 2413 ret = o2net_send_message(DLM_DEREF_LOCKRES_DONE, dlm->key, 2414 &deref, sizeof(deref), node, &r); 2415 if (ret < 0) { 2416 mlog(ML_ERROR, "%s: res %.*s, error %d send DEREF DONE " 2417 " to node %u\n", dlm->name, namelen, 2418 lockname, ret, node); 2419 } else if (r < 0) { 2420 /* ignore the error */ 2421 mlog(ML_ERROR, "%s: res %.*s, DEREF to node %u got %d\n", 2422 dlm->name, namelen, lockname, node, r); 2423 dlm_print_one_lock_resource(res); 2424 } 2425 } 2426 2427 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data) 2428 { 2429 struct dlm_ctxt *dlm; 2430 struct dlm_lock_resource *res; 2431 u8 node; 2432 u8 cleared = 0; 2433 2434 dlm = item->dlm; 2435 res = item->u.dl.deref_res; 2436 node = item->u.dl.deref_node; 2437 2438 spin_lock(&res->spinlock); 2439 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF); 2440 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG); 2441 if (test_bit(node, res->refmap)) { 2442 dlm_lockres_clear_refmap_bit(dlm, res, node); 2443 cleared = 1; 2444 } 2445 spin_unlock(&res->spinlock); 2446 2447 dlm_drop_lockres_ref_done(dlm, res, node); 2448 2449 if (cleared) { 2450 mlog(0, "%s:%.*s node %u ref dropped in dispatch\n", 2451 dlm->name, res->lockname.len, res->lockname.name, node); 2452 dlm_lockres_calc_usage(dlm, res); 2453 } else { 2454 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref " 2455 "but it is already dropped!\n", dlm->name, 2456 res->lockname.len, res->lockname.name, node); 2457 dlm_print_one_lock_resource(res); 2458 } 2459 2460 dlm_lockres_put(res); 2461 } 2462 2463 /* 2464 * A migratable resource is one that is : 2465 * 1. locally mastered, and, 2466 * 2. zero local locks, and, 2467 * 3. one or more non-local locks, or, one or more references 2468 * Returns 1 if yes, 0 if not. 2469 */ 2470 static int dlm_is_lockres_migratable(struct dlm_ctxt *dlm, 2471 struct dlm_lock_resource *res) 2472 { 2473 enum dlm_lockres_list idx; 2474 int nonlocal = 0, node_ref; 2475 struct list_head *queue; 2476 struct dlm_lock *lock; 2477 u64 cookie; 2478 2479 assert_spin_locked(&res->spinlock); 2480 2481 /* delay migration when the lockres is in MIGRATING state */ 2482 if (res->state & DLM_LOCK_RES_MIGRATING) 2483 return 0; 2484 2485 /* delay migration when the lockres is in RECOCERING state */ 2486 if (res->state & (DLM_LOCK_RES_RECOVERING| 2487 DLM_LOCK_RES_RECOVERY_WAITING)) 2488 return 0; 2489 2490 if (res->owner != dlm->node_num) 2491 return 0; 2492 2493 for (idx = DLM_GRANTED_LIST; idx <= DLM_BLOCKED_LIST; idx++) { 2494 queue = dlm_list_idx_to_ptr(res, idx); 2495 list_for_each_entry(lock, queue, list) { 2496 if (lock->ml.node != dlm->node_num) { 2497 nonlocal++; 2498 continue; 2499 } 2500 cookie = be64_to_cpu(lock->ml.cookie); 2501 mlog(0, "%s: Not migratable res %.*s, lock %u:%llu on " 2502 "%s list\n", dlm->name, res->lockname.len, 2503 res->lockname.name, 2504 dlm_get_lock_cookie_node(cookie), 2505 dlm_get_lock_cookie_seq(cookie), 2506 dlm_list_in_text(idx)); 2507 return 0; 2508 } 2509 } 2510 2511 if (!nonlocal) { 2512 node_ref = find_first_bit(res->refmap, O2NM_MAX_NODES); 2513 if (node_ref >= O2NM_MAX_NODES) 2514 return 0; 2515 } 2516 2517 mlog(0, "%s: res %.*s, Migratable\n", dlm->name, res->lockname.len, 2518 res->lockname.name); 2519 2520 return 1; 2521 } 2522 2523 /* 2524 * DLM_MIGRATE_LOCKRES 2525 */ 2526 2527 2528 static int dlm_migrate_lockres(struct dlm_ctxt *dlm, 2529 struct dlm_lock_resource *res, u8 target) 2530 { 2531 struct dlm_master_list_entry *mle = NULL; 2532 struct dlm_master_list_entry *oldmle = NULL; 2533 struct dlm_migratable_lockres *mres = NULL; 2534 int ret = 0; 2535 const char *name; 2536 unsigned int namelen; 2537 int mle_added = 0; 2538 int wake = 0; 2539 2540 if (!dlm_grab(dlm)) 2541 return -EINVAL; 2542 2543 name = res->lockname.name; 2544 namelen = res->lockname.len; 2545 2546 mlog(0, "%s: Migrating %.*s to node %u\n", dlm->name, namelen, name, 2547 target); 2548 2549 /* preallocate up front. if this fails, abort */ 2550 ret = -ENOMEM; 2551 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS); 2552 if (!mres) { 2553 mlog_errno(ret); 2554 goto leave; 2555 } 2556 2557 mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS); 2558 if (!mle) { 2559 mlog_errno(ret); 2560 goto leave; 2561 } 2562 ret = 0; 2563 2564 /* 2565 * clear any existing master requests and 2566 * add the migration mle to the list 2567 */ 2568 spin_lock(&dlm->spinlock); 2569 spin_lock(&dlm->master_lock); 2570 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name, 2571 namelen, target, dlm->node_num); 2572 /* get an extra reference on the mle. 2573 * otherwise the assert_master from the new 2574 * master will destroy this. 2575 */ 2576 if (ret != -EEXIST) 2577 dlm_get_mle_inuse(mle); 2578 2579 spin_unlock(&dlm->master_lock); 2580 spin_unlock(&dlm->spinlock); 2581 2582 if (ret == -EEXIST) { 2583 mlog(0, "another process is already migrating it\n"); 2584 goto fail; 2585 } 2586 mle_added = 1; 2587 2588 /* 2589 * set the MIGRATING flag and flush asts 2590 * if we fail after this we need to re-dirty the lockres 2591 */ 2592 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) { 2593 mlog(ML_ERROR, "tried to migrate %.*s to %u, but " 2594 "the target went down.\n", res->lockname.len, 2595 res->lockname.name, target); 2596 spin_lock(&res->spinlock); 2597 res->state &= ~DLM_LOCK_RES_MIGRATING; 2598 wake = 1; 2599 spin_unlock(&res->spinlock); 2600 ret = -EINVAL; 2601 } 2602 2603 fail: 2604 if (ret != -EEXIST && oldmle) { 2605 /* master is known, detach if not already detached */ 2606 dlm_mle_detach_hb_events(dlm, oldmle); 2607 dlm_put_mle(oldmle); 2608 } 2609 2610 if (ret < 0) { 2611 if (mle_added) { 2612 dlm_mle_detach_hb_events(dlm, mle); 2613 dlm_put_mle(mle); 2614 dlm_put_mle_inuse(mle); 2615 } else if (mle) { 2616 kmem_cache_free(dlm_mle_cache, mle); 2617 mle = NULL; 2618 } 2619 goto leave; 2620 } 2621 2622 /* 2623 * at this point, we have a migration target, an mle 2624 * in the master list, and the MIGRATING flag set on 2625 * the lockres 2626 */ 2627 2628 /* now that remote nodes are spinning on the MIGRATING flag, 2629 * ensure that all assert_master work is flushed. */ 2630 flush_workqueue(dlm->dlm_worker); 2631 2632 /* notify new node and send all lock state */ 2633 /* call send_one_lockres with migration flag. 2634 * this serves as notice to the target node that a 2635 * migration is starting. */ 2636 ret = dlm_send_one_lockres(dlm, res, mres, target, 2637 DLM_MRES_MIGRATION); 2638 2639 if (ret < 0) { 2640 mlog(0, "migration to node %u failed with %d\n", 2641 target, ret); 2642 /* migration failed, detach and clean up mle */ 2643 dlm_mle_detach_hb_events(dlm, mle); 2644 dlm_put_mle(mle); 2645 dlm_put_mle_inuse(mle); 2646 spin_lock(&res->spinlock); 2647 res->state &= ~DLM_LOCK_RES_MIGRATING; 2648 wake = 1; 2649 spin_unlock(&res->spinlock); 2650 if (dlm_is_host_down(ret)) 2651 dlm_wait_for_node_death(dlm, target, 2652 DLM_NODE_DEATH_WAIT_MAX); 2653 goto leave; 2654 } 2655 2656 /* at this point, the target sends a message to all nodes, 2657 * (using dlm_do_migrate_request). this node is skipped since 2658 * we had to put an mle in the list to begin the process. this 2659 * node now waits for target to do an assert master. this node 2660 * will be the last one notified, ensuring that the migration 2661 * is complete everywhere. if the target dies while this is 2662 * going on, some nodes could potentially see the target as the 2663 * master, so it is important that my recovery finds the migration 2664 * mle and sets the master to UNKNOWN. */ 2665 2666 2667 /* wait for new node to assert master */ 2668 while (1) { 2669 ret = wait_event_interruptible_timeout(mle->wq, 2670 (atomic_read(&mle->woken) == 1), 2671 msecs_to_jiffies(5000)); 2672 2673 if (ret >= 0) { 2674 if (atomic_read(&mle->woken) == 1 || 2675 res->owner == target) 2676 break; 2677 2678 mlog(0, "%s:%.*s: timed out during migration\n", 2679 dlm->name, res->lockname.len, res->lockname.name); 2680 /* avoid hang during shutdown when migrating lockres 2681 * to a node which also goes down */ 2682 if (dlm_is_node_dead(dlm, target)) { 2683 mlog(0, "%s:%.*s: expected migration " 2684 "target %u is no longer up, restarting\n", 2685 dlm->name, res->lockname.len, 2686 res->lockname.name, target); 2687 ret = -EINVAL; 2688 /* migration failed, detach and clean up mle */ 2689 dlm_mle_detach_hb_events(dlm, mle); 2690 dlm_put_mle(mle); 2691 dlm_put_mle_inuse(mle); 2692 spin_lock(&res->spinlock); 2693 res->state &= ~DLM_LOCK_RES_MIGRATING; 2694 wake = 1; 2695 spin_unlock(&res->spinlock); 2696 goto leave; 2697 } 2698 } else 2699 mlog(0, "%s:%.*s: caught signal during migration\n", 2700 dlm->name, res->lockname.len, res->lockname.name); 2701 } 2702 2703 /* all done, set the owner, clear the flag */ 2704 spin_lock(&res->spinlock); 2705 dlm_set_lockres_owner(dlm, res, target); 2706 res->state &= ~DLM_LOCK_RES_MIGRATING; 2707 dlm_remove_nonlocal_locks(dlm, res); 2708 spin_unlock(&res->spinlock); 2709 wake_up(&res->wq); 2710 2711 /* master is known, detach if not already detached */ 2712 dlm_mle_detach_hb_events(dlm, mle); 2713 dlm_put_mle_inuse(mle); 2714 ret = 0; 2715 2716 dlm_lockres_calc_usage(dlm, res); 2717 2718 leave: 2719 /* re-dirty the lockres if we failed */ 2720 if (ret < 0) 2721 dlm_kick_thread(dlm, res); 2722 2723 /* wake up waiters if the MIGRATING flag got set 2724 * but migration failed */ 2725 if (wake) 2726 wake_up(&res->wq); 2727 2728 if (mres) 2729 free_page((unsigned long)mres); 2730 2731 dlm_put(dlm); 2732 2733 mlog(0, "%s: Migrating %.*s to %u, returns %d\n", dlm->name, namelen, 2734 name, target, ret); 2735 return ret; 2736 } 2737 2738 /* 2739 * Should be called only after beginning the domain leave process. 2740 * There should not be any remaining locks on nonlocal lock resources, 2741 * and there should be no local locks left on locally mastered resources. 2742 * 2743 * Called with the dlm spinlock held, may drop it to do migration, but 2744 * will re-acquire before exit. 2745 * 2746 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped 2747 */ 2748 int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) 2749 __must_hold(&dlm->spinlock) 2750 { 2751 int ret; 2752 int lock_dropped = 0; 2753 u8 target = O2NM_MAX_NODES; 2754 2755 assert_spin_locked(&dlm->spinlock); 2756 2757 spin_lock(&res->spinlock); 2758 if (dlm_is_lockres_migratable(dlm, res)) 2759 target = dlm_pick_migration_target(dlm, res); 2760 spin_unlock(&res->spinlock); 2761 2762 if (target == O2NM_MAX_NODES) 2763 goto leave; 2764 2765 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */ 2766 spin_unlock(&dlm->spinlock); 2767 lock_dropped = 1; 2768 ret = dlm_migrate_lockres(dlm, res, target); 2769 if (ret) 2770 mlog(0, "%s: res %.*s, Migrate to node %u failed with %d\n", 2771 dlm->name, res->lockname.len, res->lockname.name, 2772 target, ret); 2773 spin_lock(&dlm->spinlock); 2774 leave: 2775 return lock_dropped; 2776 } 2777 2778 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock) 2779 { 2780 int ret; 2781 spin_lock(&dlm->ast_lock); 2782 spin_lock(&lock->spinlock); 2783 ret = (list_empty(&lock->bast_list) && !lock->bast_pending); 2784 spin_unlock(&lock->spinlock); 2785 spin_unlock(&dlm->ast_lock); 2786 return ret; 2787 } 2788 2789 static int dlm_migration_can_proceed(struct dlm_ctxt *dlm, 2790 struct dlm_lock_resource *res, 2791 u8 mig_target) 2792 { 2793 int can_proceed; 2794 spin_lock(&res->spinlock); 2795 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING); 2796 spin_unlock(&res->spinlock); 2797 2798 /* target has died, so make the caller break out of the 2799 * wait_event, but caller must recheck the domain_map */ 2800 spin_lock(&dlm->spinlock); 2801 if (!test_bit(mig_target, dlm->domain_map)) 2802 can_proceed = 1; 2803 spin_unlock(&dlm->spinlock); 2804 return can_proceed; 2805 } 2806 2807 static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm, 2808 struct dlm_lock_resource *res) 2809 { 2810 int ret; 2811 spin_lock(&res->spinlock); 2812 ret = !!(res->state & DLM_LOCK_RES_DIRTY); 2813 spin_unlock(&res->spinlock); 2814 return ret; 2815 } 2816 2817 2818 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm, 2819 struct dlm_lock_resource *res, 2820 u8 target) 2821 { 2822 int ret = 0; 2823 2824 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n", 2825 res->lockname.len, res->lockname.name, dlm->node_num, 2826 target); 2827 /* need to set MIGRATING flag on lockres. this is done by 2828 * ensuring that all asts have been flushed for this lockres. */ 2829 spin_lock(&res->spinlock); 2830 BUG_ON(res->migration_pending); 2831 res->migration_pending = 1; 2832 /* strategy is to reserve an extra ast then release 2833 * it below, letting the release do all of the work */ 2834 __dlm_lockres_reserve_ast(res); 2835 spin_unlock(&res->spinlock); 2836 2837 /* now flush all the pending asts */ 2838 dlm_kick_thread(dlm, res); 2839 /* before waiting on DIRTY, block processes which may 2840 * try to dirty the lockres before MIGRATING is set */ 2841 spin_lock(&res->spinlock); 2842 BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY); 2843 res->state |= DLM_LOCK_RES_BLOCK_DIRTY; 2844 spin_unlock(&res->spinlock); 2845 /* now wait on any pending asts and the DIRTY state */ 2846 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res)); 2847 dlm_lockres_release_ast(dlm, res); 2848 2849 mlog(0, "about to wait on migration_wq, dirty=%s\n", 2850 str_yes_no(res->state & DLM_LOCK_RES_DIRTY)); 2851 /* if the extra ref we just put was the final one, this 2852 * will pass thru immediately. otherwise, we need to wait 2853 * for the last ast to finish. */ 2854 again: 2855 ret = wait_event_interruptible_timeout(dlm->migration_wq, 2856 dlm_migration_can_proceed(dlm, res, target), 2857 msecs_to_jiffies(1000)); 2858 if (ret < 0) { 2859 mlog(0, "woken again: migrating? %s, dead? %s\n", 2860 str_yes_no(res->state & DLM_LOCK_RES_MIGRATING), 2861 str_no_yes(test_bit(target, dlm->domain_map))); 2862 } else { 2863 mlog(0, "all is well: migrating? %s, dead? %s\n", 2864 str_yes_no(res->state & DLM_LOCK_RES_MIGRATING), 2865 str_no_yes(test_bit(target, dlm->domain_map))); 2866 } 2867 if (!dlm_migration_can_proceed(dlm, res, target)) { 2868 mlog(0, "trying again...\n"); 2869 goto again; 2870 } 2871 2872 ret = 0; 2873 /* did the target go down or die? */ 2874 spin_lock(&dlm->spinlock); 2875 if (!test_bit(target, dlm->domain_map)) { 2876 mlog(ML_ERROR, "aha. migration target %u just went down\n", 2877 target); 2878 ret = -EHOSTDOWN; 2879 } 2880 spin_unlock(&dlm->spinlock); 2881 2882 /* 2883 * if target is down, we need to clear DLM_LOCK_RES_BLOCK_DIRTY for 2884 * another try; otherwise, we are sure the MIGRATING state is there, 2885 * drop the unneeded state which blocked threads trying to DIRTY 2886 */ 2887 spin_lock(&res->spinlock); 2888 BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY)); 2889 res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY; 2890 if (!ret) 2891 BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING)); 2892 else 2893 res->migration_pending = 0; 2894 spin_unlock(&res->spinlock); 2895 2896 /* 2897 * at this point: 2898 * 2899 * o the DLM_LOCK_RES_MIGRATING flag is set if target not down 2900 * o there are no pending asts on this lockres 2901 * o all processes trying to reserve an ast on this 2902 * lockres must wait for the MIGRATING flag to clear 2903 */ 2904 return ret; 2905 } 2906 2907 /* last step in the migration process. 2908 * original master calls this to free all of the dlm_lock 2909 * structures that used to be for other nodes. */ 2910 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm, 2911 struct dlm_lock_resource *res) 2912 { 2913 struct list_head *queue = &res->granted; 2914 int i, bit; 2915 struct dlm_lock *lock, *next; 2916 2917 assert_spin_locked(&res->spinlock); 2918 2919 BUG_ON(res->owner == dlm->node_num); 2920 2921 for (i=0; i<3; i++) { 2922 list_for_each_entry_safe(lock, next, queue, list) { 2923 if (lock->ml.node != dlm->node_num) { 2924 mlog(0, "putting lock for node %u\n", 2925 lock->ml.node); 2926 /* be extra careful */ 2927 BUG_ON(!list_empty(&lock->ast_list)); 2928 BUG_ON(!list_empty(&lock->bast_list)); 2929 BUG_ON(lock->ast_pending); 2930 BUG_ON(lock->bast_pending); 2931 dlm_lockres_clear_refmap_bit(dlm, res, 2932 lock->ml.node); 2933 list_del_init(&lock->list); 2934 dlm_lock_put(lock); 2935 /* In a normal unlock, we would have added a 2936 * DLM_UNLOCK_FREE_LOCK action. Force it. */ 2937 dlm_lock_put(lock); 2938 } 2939 } 2940 queue++; 2941 } 2942 bit = 0; 2943 while (1) { 2944 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit); 2945 if (bit >= O2NM_MAX_NODES) 2946 break; 2947 /* do not clear the local node reference, if there is a 2948 * process holding this, let it drop the ref itself */ 2949 if (bit != dlm->node_num) { 2950 mlog(0, "%s:%.*s: node %u had a ref to this " 2951 "migrating lockres, clearing\n", dlm->name, 2952 res->lockname.len, res->lockname.name, bit); 2953 dlm_lockres_clear_refmap_bit(dlm, res, bit); 2954 } 2955 bit++; 2956 } 2957 } 2958 2959 /* 2960 * Pick a node to migrate the lock resource to. This function selects a 2961 * potential target based first on the locks and then on refmap. It skips 2962 * nodes that are in the process of exiting the domain. 2963 */ 2964 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm, 2965 struct dlm_lock_resource *res) 2966 { 2967 enum dlm_lockres_list idx; 2968 struct list_head *queue; 2969 struct dlm_lock *lock; 2970 int noderef; 2971 u8 nodenum = O2NM_MAX_NODES; 2972 2973 assert_spin_locked(&dlm->spinlock); 2974 assert_spin_locked(&res->spinlock); 2975 2976 /* Go through all the locks */ 2977 for (idx = DLM_GRANTED_LIST; idx <= DLM_BLOCKED_LIST; idx++) { 2978 queue = dlm_list_idx_to_ptr(res, idx); 2979 list_for_each_entry(lock, queue, list) { 2980 if (lock->ml.node == dlm->node_num) 2981 continue; 2982 if (test_bit(lock->ml.node, dlm->exit_domain_map)) 2983 continue; 2984 nodenum = lock->ml.node; 2985 goto bail; 2986 } 2987 } 2988 2989 /* Go thru the refmap */ 2990 noderef = -1; 2991 while (1) { 2992 noderef = find_next_bit(res->refmap, O2NM_MAX_NODES, 2993 noderef + 1); 2994 if (noderef >= O2NM_MAX_NODES) 2995 break; 2996 if (noderef == dlm->node_num) 2997 continue; 2998 if (test_bit(noderef, dlm->exit_domain_map)) 2999 continue; 3000 nodenum = noderef; 3001 goto bail; 3002 } 3003 3004 bail: 3005 return nodenum; 3006 } 3007 3008 /* this is called by the new master once all lockres 3009 * data has been received */ 3010 static int dlm_do_migrate_request(struct dlm_ctxt *dlm, 3011 struct dlm_lock_resource *res, 3012 u8 master, u8 new_master, 3013 struct dlm_node_iter *iter) 3014 { 3015 struct dlm_migrate_request migrate; 3016 int ret, skip, status = 0; 3017 int nodenum; 3018 3019 memset(&migrate, 0, sizeof(migrate)); 3020 migrate.namelen = res->lockname.len; 3021 memcpy(migrate.name, res->lockname.name, migrate.namelen); 3022 migrate.new_master = new_master; 3023 migrate.master = master; 3024 3025 ret = 0; 3026 3027 /* send message to all nodes, except the master and myself */ 3028 while ((nodenum = dlm_node_iter_next(iter)) >= 0) { 3029 if (nodenum == master || 3030 nodenum == new_master) 3031 continue; 3032 3033 /* We could race exit domain. If exited, skip. */ 3034 spin_lock(&dlm->spinlock); 3035 skip = (!test_bit(nodenum, dlm->domain_map)); 3036 spin_unlock(&dlm->spinlock); 3037 if (skip) { 3038 clear_bit(nodenum, iter->node_map); 3039 continue; 3040 } 3041 3042 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key, 3043 &migrate, sizeof(migrate), nodenum, 3044 &status); 3045 if (ret < 0) { 3046 mlog(ML_ERROR, "%s: res %.*s, Error %d send " 3047 "MIGRATE_REQUEST to node %u\n", dlm->name, 3048 migrate.namelen, migrate.name, ret, nodenum); 3049 if (!dlm_is_host_down(ret)) { 3050 mlog(ML_ERROR, "unhandled error=%d!\n", ret); 3051 BUG(); 3052 } 3053 clear_bit(nodenum, iter->node_map); 3054 ret = 0; 3055 } else if (status < 0) { 3056 mlog(0, "migrate request (node %u) returned %d!\n", 3057 nodenum, status); 3058 ret = status; 3059 } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) { 3060 /* during the migration request we short-circuited 3061 * the mastery of the lockres. make sure we have 3062 * a mastery ref for nodenum */ 3063 mlog(0, "%s:%.*s: need ref for node %u\n", 3064 dlm->name, res->lockname.len, res->lockname.name, 3065 nodenum); 3066 spin_lock(&res->spinlock); 3067 dlm_lockres_set_refmap_bit(dlm, res, nodenum); 3068 spin_unlock(&res->spinlock); 3069 } 3070 } 3071 3072 if (ret < 0) 3073 mlog_errno(ret); 3074 3075 mlog(0, "returning ret=%d\n", ret); 3076 return ret; 3077 } 3078 3079 3080 /* if there is an existing mle for this lockres, we now know who the master is. 3081 * (the one who sent us *this* message) we can clear it up right away. 3082 * since the process that put the mle on the list still has a reference to it, 3083 * we can unhash it now, set the master and wake the process. as a result, 3084 * we will have no mle in the list to start with. now we can add an mle for 3085 * the migration and this should be the only one found for those scanning the 3086 * list. */ 3087 int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data, 3088 void **ret_data) 3089 { 3090 struct dlm_ctxt *dlm = data; 3091 struct dlm_lock_resource *res = NULL; 3092 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf; 3093 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL; 3094 const char *name; 3095 unsigned int namelen, hash; 3096 int ret = 0; 3097 3098 if (!dlm_grab(dlm)) 3099 return 0; 3100 3101 name = migrate->name; 3102 namelen = migrate->namelen; 3103 hash = dlm_lockid_hash(name, namelen); 3104 3105 /* preallocate.. if this fails, abort */ 3106 mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS); 3107 3108 if (!mle) { 3109 ret = -ENOMEM; 3110 goto leave; 3111 } 3112 3113 /* check for pre-existing lock */ 3114 spin_lock(&dlm->spinlock); 3115 res = __dlm_lookup_lockres(dlm, name, namelen, hash); 3116 if (res) { 3117 spin_lock(&res->spinlock); 3118 if (res->state & DLM_LOCK_RES_RECOVERING) { 3119 /* if all is working ok, this can only mean that we got 3120 * a migrate request from a node that we now see as 3121 * dead. what can we do here? drop it to the floor? */ 3122 spin_unlock(&res->spinlock); 3123 mlog(ML_ERROR, "Got a migrate request, but the " 3124 "lockres is marked as recovering!"); 3125 kmem_cache_free(dlm_mle_cache, mle); 3126 ret = -EINVAL; /* need a better solution */ 3127 goto unlock; 3128 } 3129 res->state |= DLM_LOCK_RES_MIGRATING; 3130 spin_unlock(&res->spinlock); 3131 } 3132 3133 spin_lock(&dlm->master_lock); 3134 /* ignore status. only nonzero status would BUG. */ 3135 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, 3136 name, namelen, 3137 migrate->new_master, 3138 migrate->master); 3139 3140 if (ret < 0) 3141 kmem_cache_free(dlm_mle_cache, mle); 3142 3143 spin_unlock(&dlm->master_lock); 3144 unlock: 3145 spin_unlock(&dlm->spinlock); 3146 3147 if (oldmle) { 3148 /* master is known, detach if not already detached */ 3149 dlm_mle_detach_hb_events(dlm, oldmle); 3150 dlm_put_mle(oldmle); 3151 } 3152 3153 if (res) 3154 dlm_lockres_put(res); 3155 leave: 3156 dlm_put(dlm); 3157 return ret; 3158 } 3159 3160 /* must be holding dlm->spinlock and dlm->master_lock 3161 * when adding a migration mle, we can clear any other mles 3162 * in the master list because we know with certainty that 3163 * the master is "master". so we remove any old mle from 3164 * the list after setting it's master field, and then add 3165 * the new migration mle. this way we can hold with the rule 3166 * of having only one mle for a given lock name at all times. */ 3167 static int dlm_add_migration_mle(struct dlm_ctxt *dlm, 3168 struct dlm_lock_resource *res, 3169 struct dlm_master_list_entry *mle, 3170 struct dlm_master_list_entry **oldmle, 3171 const char *name, unsigned int namelen, 3172 u8 new_master, u8 master) 3173 { 3174 int found; 3175 int ret = 0; 3176 3177 *oldmle = NULL; 3178 3179 assert_spin_locked(&dlm->spinlock); 3180 assert_spin_locked(&dlm->master_lock); 3181 3182 /* caller is responsible for any ref taken here on oldmle */ 3183 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen); 3184 if (found) { 3185 struct dlm_master_list_entry *tmp = *oldmle; 3186 spin_lock(&tmp->spinlock); 3187 if (tmp->type == DLM_MLE_MIGRATION) { 3188 if (master == dlm->node_num) { 3189 /* ah another process raced me to it */ 3190 mlog(0, "tried to migrate %.*s, but some " 3191 "process beat me to it\n", 3192 namelen, name); 3193 spin_unlock(&tmp->spinlock); 3194 return -EEXIST; 3195 } else { 3196 /* bad. 2 NODES are trying to migrate! */ 3197 mlog(ML_ERROR, "migration error mle: " 3198 "master=%u new_master=%u // request: " 3199 "master=%u new_master=%u // " 3200 "lockres=%.*s\n", 3201 tmp->master, tmp->new_master, 3202 master, new_master, 3203 namelen, name); 3204 BUG(); 3205 } 3206 } else { 3207 /* this is essentially what assert_master does */ 3208 tmp->master = master; 3209 atomic_set(&tmp->woken, 1); 3210 wake_up(&tmp->wq); 3211 /* remove it so that only one mle will be found */ 3212 __dlm_unlink_mle(dlm, tmp); 3213 __dlm_mle_detach_hb_events(dlm, tmp); 3214 if (tmp->type == DLM_MLE_MASTER) { 3215 ret = DLM_MIGRATE_RESPONSE_MASTERY_REF; 3216 mlog(0, "%s:%.*s: master=%u, newmaster=%u, " 3217 "telling master to get ref " 3218 "for cleared out mle during " 3219 "migration\n", dlm->name, 3220 namelen, name, master, 3221 new_master); 3222 } 3223 } 3224 spin_unlock(&tmp->spinlock); 3225 } 3226 3227 /* now add a migration mle to the tail of the list */ 3228 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen); 3229 mle->new_master = new_master; 3230 /* the new master will be sending an assert master for this. 3231 * at that point we will get the refmap reference */ 3232 mle->master = master; 3233 /* do this for consistency with other mle types */ 3234 set_bit(new_master, mle->maybe_map); 3235 __dlm_insert_mle(dlm, mle); 3236 3237 return ret; 3238 } 3239 3240 /* 3241 * Sets the owner of the lockres, associated to the mle, to UNKNOWN 3242 */ 3243 static struct dlm_lock_resource *dlm_reset_mleres_owner(struct dlm_ctxt *dlm, 3244 struct dlm_master_list_entry *mle) 3245 { 3246 struct dlm_lock_resource *res; 3247 3248 /* Find the lockres associated to the mle and set its owner to UNK */ 3249 res = __dlm_lookup_lockres(dlm, mle->mname, mle->mnamelen, 3250 mle->mnamehash); 3251 if (res) { 3252 spin_unlock(&dlm->master_lock); 3253 3254 /* move lockres onto recovery list */ 3255 spin_lock(&res->spinlock); 3256 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN); 3257 dlm_move_lockres_to_recovery_list(dlm, res); 3258 spin_unlock(&res->spinlock); 3259 dlm_lockres_put(res); 3260 3261 /* about to get rid of mle, detach from heartbeat */ 3262 __dlm_mle_detach_hb_events(dlm, mle); 3263 3264 /* dump the mle */ 3265 spin_lock(&dlm->master_lock); 3266 __dlm_put_mle(mle); 3267 spin_unlock(&dlm->master_lock); 3268 } 3269 3270 return res; 3271 } 3272 3273 static void dlm_clean_migration_mle(struct dlm_ctxt *dlm, 3274 struct dlm_master_list_entry *mle) 3275 { 3276 __dlm_mle_detach_hb_events(dlm, mle); 3277 3278 spin_lock(&mle->spinlock); 3279 __dlm_unlink_mle(dlm, mle); 3280 atomic_set(&mle->woken, 1); 3281 spin_unlock(&mle->spinlock); 3282 3283 wake_up(&mle->wq); 3284 } 3285 3286 static void dlm_clean_block_mle(struct dlm_ctxt *dlm, 3287 struct dlm_master_list_entry *mle, u8 dead_node) 3288 { 3289 int bit; 3290 3291 BUG_ON(mle->type != DLM_MLE_BLOCK); 3292 3293 spin_lock(&mle->spinlock); 3294 bit = find_first_bit(mle->maybe_map, O2NM_MAX_NODES); 3295 if (bit != dead_node) { 3296 mlog(0, "mle found, but dead node %u would not have been " 3297 "master\n", dead_node); 3298 spin_unlock(&mle->spinlock); 3299 } else { 3300 /* Must drop the refcount by one since the assert_master will 3301 * never arrive. This may result in the mle being unlinked and 3302 * freed, but there may still be a process waiting in the 3303 * dlmlock path which is fine. */ 3304 mlog(0, "node %u was expected master\n", dead_node); 3305 atomic_set(&mle->woken, 1); 3306 spin_unlock(&mle->spinlock); 3307 wake_up(&mle->wq); 3308 3309 /* Do not need events any longer, so detach from heartbeat */ 3310 __dlm_mle_detach_hb_events(dlm, mle); 3311 __dlm_put_mle(mle); 3312 } 3313 } 3314 3315 void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node) 3316 { 3317 struct dlm_master_list_entry *mle; 3318 struct dlm_lock_resource *res; 3319 struct hlist_head *bucket; 3320 struct hlist_node *tmp; 3321 unsigned int i; 3322 3323 mlog(0, "dlm=%s, dead node=%u\n", dlm->name, dead_node); 3324 top: 3325 assert_spin_locked(&dlm->spinlock); 3326 3327 /* clean the master list */ 3328 spin_lock(&dlm->master_lock); 3329 for (i = 0; i < DLM_HASH_BUCKETS; i++) { 3330 bucket = dlm_master_hash(dlm, i); 3331 hlist_for_each_entry_safe(mle, tmp, bucket, master_hash_node) { 3332 BUG_ON(mle->type != DLM_MLE_BLOCK && 3333 mle->type != DLM_MLE_MASTER && 3334 mle->type != DLM_MLE_MIGRATION); 3335 3336 /* MASTER mles are initiated locally. The waiting 3337 * process will notice the node map change shortly. 3338 * Let that happen as normal. */ 3339 if (mle->type == DLM_MLE_MASTER) 3340 continue; 3341 3342 /* BLOCK mles are initiated by other nodes. Need to 3343 * clean up if the dead node would have been the 3344 * master. */ 3345 if (mle->type == DLM_MLE_BLOCK) { 3346 dlm_clean_block_mle(dlm, mle, dead_node); 3347 continue; 3348 } 3349 3350 /* Everything else is a MIGRATION mle */ 3351 3352 /* The rule for MIGRATION mles is that the master 3353 * becomes UNKNOWN if *either* the original or the new 3354 * master dies. All UNKNOWN lockres' are sent to 3355 * whichever node becomes the recovery master. The new 3356 * master is responsible for determining if there is 3357 * still a master for this lockres, or if he needs to 3358 * take over mastery. Either way, this node should 3359 * expect another message to resolve this. */ 3360 3361 if (mle->master != dead_node && 3362 mle->new_master != dead_node) 3363 continue; 3364 3365 if (mle->new_master == dead_node && mle->inuse) { 3366 mlog(ML_NOTICE, "%s: target %u died during " 3367 "migration from %u, the MLE is " 3368 "still keep used, ignore it!\n", 3369 dlm->name, dead_node, 3370 mle->master); 3371 continue; 3372 } 3373 3374 /* If we have reached this point, this mle needs to be 3375 * removed from the list and freed. */ 3376 dlm_clean_migration_mle(dlm, mle); 3377 3378 mlog(0, "%s: node %u died during migration from " 3379 "%u to %u!\n", dlm->name, dead_node, mle->master, 3380 mle->new_master); 3381 3382 /* If we find a lockres associated with the mle, we've 3383 * hit this rare case that messes up our lock ordering. 3384 * If so, we need to drop the master lock so that we can 3385 * take the lockres lock, meaning that we will have to 3386 * restart from the head of list. */ 3387 res = dlm_reset_mleres_owner(dlm, mle); 3388 if (res) 3389 /* restart */ 3390 goto top; 3391 3392 /* This may be the last reference */ 3393 __dlm_put_mle(mle); 3394 } 3395 } 3396 spin_unlock(&dlm->master_lock); 3397 } 3398 3399 int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, 3400 u8 old_master) 3401 { 3402 struct dlm_node_iter iter; 3403 int ret = 0; 3404 3405 spin_lock(&dlm->spinlock); 3406 dlm_node_iter_init(dlm->domain_map, &iter); 3407 clear_bit(old_master, iter.node_map); 3408 clear_bit(dlm->node_num, iter.node_map); 3409 spin_unlock(&dlm->spinlock); 3410 3411 /* ownership of the lockres is changing. account for the 3412 * mastery reference here since old_master will briefly have 3413 * a reference after the migration completes */ 3414 spin_lock(&res->spinlock); 3415 dlm_lockres_set_refmap_bit(dlm, res, old_master); 3416 spin_unlock(&res->spinlock); 3417 3418 mlog(0, "now time to do a migrate request to other nodes\n"); 3419 ret = dlm_do_migrate_request(dlm, res, old_master, 3420 dlm->node_num, &iter); 3421 if (ret < 0) { 3422 mlog_errno(ret); 3423 goto leave; 3424 } 3425 3426 mlog(0, "doing assert master of %.*s to all except the original node\n", 3427 res->lockname.len, res->lockname.name); 3428 /* this call now finishes out the nodemap 3429 * even if one or more nodes die */ 3430 ret = dlm_do_assert_master(dlm, res, iter.node_map, 3431 DLM_ASSERT_MASTER_FINISH_MIGRATION); 3432 if (ret < 0) { 3433 /* no longer need to retry. all living nodes contacted. */ 3434 mlog_errno(ret); 3435 ret = 0; 3436 } 3437 3438 bitmap_zero(iter.node_map, O2NM_MAX_NODES); 3439 set_bit(old_master, iter.node_map); 3440 mlog(0, "doing assert master of %.*s back to %u\n", 3441 res->lockname.len, res->lockname.name, old_master); 3442 ret = dlm_do_assert_master(dlm, res, iter.node_map, 3443 DLM_ASSERT_MASTER_FINISH_MIGRATION); 3444 if (ret < 0) { 3445 mlog(0, "assert master to original master failed " 3446 "with %d.\n", ret); 3447 /* the only nonzero status here would be because of 3448 * a dead original node. we're done. */ 3449 ret = 0; 3450 } 3451 3452 /* all done, set the owner, clear the flag */ 3453 spin_lock(&res->spinlock); 3454 dlm_set_lockres_owner(dlm, res, dlm->node_num); 3455 res->state &= ~DLM_LOCK_RES_MIGRATING; 3456 spin_unlock(&res->spinlock); 3457 /* re-dirty it on the new master */ 3458 dlm_kick_thread(dlm, res); 3459 wake_up(&res->wq); 3460 leave: 3461 return ret; 3462 } 3463 3464 /* 3465 * LOCKRES AST REFCOUNT 3466 * this is integral to migration 3467 */ 3468 3469 /* for future intent to call an ast, reserve one ahead of time. 3470 * this should be called only after waiting on the lockres 3471 * with dlm_wait_on_lockres, and while still holding the 3472 * spinlock after the call. */ 3473 void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res) 3474 { 3475 assert_spin_locked(&res->spinlock); 3476 if (res->state & DLM_LOCK_RES_MIGRATING) { 3477 __dlm_print_one_lock_resource(res); 3478 } 3479 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING); 3480 3481 atomic_inc(&res->asts_reserved); 3482 } 3483 3484 /* 3485 * used to drop the reserved ast, either because it went unused, 3486 * or because the ast/bast was actually called. 3487 * 3488 * also, if there is a pending migration on this lockres, 3489 * and this was the last pending ast on the lockres, 3490 * atomically set the MIGRATING flag before we drop the lock. 3491 * this is how we ensure that migration can proceed with no 3492 * asts in progress. note that it is ok if the state of the 3493 * queues is such that a lock should be granted in the future 3494 * or that a bast should be fired, because the new master will 3495 * shuffle the lists on this lockres as soon as it is migrated. 3496 */ 3497 void dlm_lockres_release_ast(struct dlm_ctxt *dlm, 3498 struct dlm_lock_resource *res) 3499 { 3500 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock)) 3501 return; 3502 3503 if (!res->migration_pending) { 3504 spin_unlock(&res->spinlock); 3505 return; 3506 } 3507 3508 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING); 3509 res->migration_pending = 0; 3510 res->state |= DLM_LOCK_RES_MIGRATING; 3511 spin_unlock(&res->spinlock); 3512 wake_up(&res->wq); 3513 wake_up(&dlm->migration_wq); 3514 } 3515 3516 void dlm_force_free_mles(struct dlm_ctxt *dlm) 3517 { 3518 int i; 3519 struct hlist_head *bucket; 3520 struct dlm_master_list_entry *mle; 3521 struct hlist_node *tmp; 3522 3523 /* 3524 * We notified all other nodes that we are exiting the domain and 3525 * marked the dlm state to DLM_CTXT_LEAVING. If any mles are still 3526 * around we force free them and wake any processes that are waiting 3527 * on the mles 3528 */ 3529 spin_lock(&dlm->spinlock); 3530 spin_lock(&dlm->master_lock); 3531 3532 BUG_ON(dlm->dlm_state != DLM_CTXT_LEAVING); 3533 BUG_ON((find_first_bit(dlm->domain_map, O2NM_MAX_NODES) < O2NM_MAX_NODES)); 3534 3535 for (i = 0; i < DLM_HASH_BUCKETS; i++) { 3536 bucket = dlm_master_hash(dlm, i); 3537 hlist_for_each_entry_safe(mle, tmp, bucket, master_hash_node) { 3538 if (mle->type != DLM_MLE_BLOCK) { 3539 mlog(ML_ERROR, "bad mle: %p\n", mle); 3540 dlm_print_one_mle(mle); 3541 } 3542 atomic_set(&mle->woken, 1); 3543 wake_up(&mle->wq); 3544 3545 __dlm_unlink_mle(dlm, mle); 3546 __dlm_mle_detach_hb_events(dlm, mle); 3547 __dlm_put_mle(mle); 3548 } 3549 } 3550 spin_unlock(&dlm->master_lock); 3551 spin_unlock(&dlm->spinlock); 3552 } 3553