1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * standalone DLM module
8 * Copyright (C) 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/slab.h>
32 #include <linux/highmem.h>
33 #include <linux/utsname.h>
34 #include <linux/init.h>
35 #include <linux/sysctl.h>
36 #include <linux/random.h>
37 #include <linux/blkdev.h>
38 #include <linux/socket.h>
39 #include <linux/inet.h>
40 #include <linux/spinlock.h>
41 #include <linux/delay.h>
44 #include "cluster/heartbeat.h"
45 #include "cluster/nodemanager.h"
46 #include "cluster/tcp.h"
49 #include "dlmcommon.h"
50 #include "dlmdomain.h"
52 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
53 #include "cluster/masklog.h"
64 u8 name[DLM_LOCKID_NAME_MAX];
67 struct dlm_master_list_entry
69 struct list_head list;
70 struct list_head hb_events;
77 unsigned long maybe_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
78 unsigned long vote_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
79 unsigned long response_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
80 unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
83 enum dlm_mle_type type;
84 struct o2hb_callback_func mle_hb_up;
85 struct o2hb_callback_func mle_hb_down;
87 struct dlm_lock_resource *res;
88 struct dlm_lock_name name;
92 static void dlm_mle_node_down(struct dlm_ctxt *dlm,
93 struct dlm_master_list_entry *mle,
94 struct o2nm_node *node,
96 static void dlm_mle_node_up(struct dlm_ctxt *dlm,
97 struct dlm_master_list_entry *mle,
98 struct o2nm_node *node,
101 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
102 static int dlm_do_assert_master(struct dlm_ctxt *dlm,
103 struct dlm_lock_resource *res,
104 void *nodemap, u32 flags);
106 static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
107 struct dlm_master_list_entry *mle,
109 unsigned int namelen)
111 struct dlm_lock_resource *res;
116 if (mle->type == DLM_MLE_BLOCK ||
117 mle->type == DLM_MLE_MIGRATION) {
118 if (namelen != mle->u.name.len ||
119 memcmp(name, mle->u.name.name, namelen)!=0)
123 if (namelen != res->lockname.len ||
124 memcmp(res->lockname.name, name, namelen) != 0)
130 #define dlm_print_nodemap(m) _dlm_print_nodemap(m,#m)
131 static void _dlm_print_nodemap(unsigned long *map, const char *mapname)
134 printk("%s=[ ", mapname);
135 for (i=0; i<O2NM_MAX_NODES; i++)
136 if (test_bit(i, map))
141 static void dlm_print_one_mle(struct dlm_master_list_entry *mle)
147 unsigned int namelen;
150 unsigned long *maybe = mle->maybe_map,
151 *vote = mle->vote_map,
152 *resp = mle->response_map,
153 *node = mle->node_map;
156 if (mle->type == DLM_MLE_BLOCK)
158 else if (mle->type == DLM_MLE_MASTER)
162 refs = atomic_read(&k->refcount);
163 master = mle->master;
164 attached = (list_empty(&mle->hb_events) ? 'N' : 'Y');
166 if (mle->type != DLM_MLE_MASTER) {
167 namelen = mle->u.name.len;
168 name = mle->u.name.name;
170 namelen = mle->u.res->lockname.len;
171 name = mle->u.res->lockname.name;
174 mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
175 namelen, name, type, refs, master, mle->new_master, attached,
177 dlm_print_nodemap(maybe);
179 dlm_print_nodemap(vote);
181 dlm_print_nodemap(resp);
183 dlm_print_nodemap(node);
189 /* Code here is included but defined out as it aids debugging */
191 static void dlm_dump_mles(struct dlm_ctxt *dlm)
193 struct dlm_master_list_entry *mle;
194 struct list_head *iter;
196 mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
197 spin_lock(&dlm->master_lock);
198 list_for_each(iter, &dlm->master_list) {
199 mle = list_entry(iter, struct dlm_master_list_entry, list);
200 dlm_print_one_mle(mle);
202 spin_unlock(&dlm->master_lock);
205 int dlm_dump_all_mles(const char __user *data, unsigned int len)
207 struct list_head *iter;
208 struct dlm_ctxt *dlm;
210 spin_lock(&dlm_domain_lock);
211 list_for_each(iter, &dlm_domains) {
212 dlm = list_entry (iter, struct dlm_ctxt, list);
213 mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
216 spin_unlock(&dlm_domain_lock);
219 EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
224 static struct kmem_cache *dlm_mle_cache = NULL;
227 static void dlm_mle_release(struct kref *kref);
228 static void dlm_init_mle(struct dlm_master_list_entry *mle,
229 enum dlm_mle_type type,
230 struct dlm_ctxt *dlm,
231 struct dlm_lock_resource *res,
233 unsigned int namelen);
234 static void dlm_put_mle(struct dlm_master_list_entry *mle);
235 static void __dlm_put_mle(struct dlm_master_list_entry *mle);
236 static int dlm_find_mle(struct dlm_ctxt *dlm,
237 struct dlm_master_list_entry **mle,
238 char *name, unsigned int namelen);
240 static int dlm_do_master_request(struct dlm_lock_resource *res,
241 struct dlm_master_list_entry *mle, int to);
244 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
245 struct dlm_lock_resource *res,
246 struct dlm_master_list_entry *mle,
248 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
249 struct dlm_lock_resource *res,
250 struct dlm_master_list_entry *mle,
252 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
253 struct dlm_lock_resource *res,
254 struct dlm_master_list_entry *mle,
255 struct dlm_master_list_entry **oldmle,
256 const char *name, unsigned int namelen,
257 u8 new_master, u8 master);
259 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
260 struct dlm_lock_resource *res);
261 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
262 struct dlm_lock_resource *res);
263 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
264 struct dlm_lock_resource *res,
266 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
267 struct dlm_lock_resource *res);
270 int dlm_is_host_down(int errno)
287 case -EINVAL: /* if returned from our tcp code,
288 this means there is no socket */
296 * MASTER LIST FUNCTIONS
301 * regarding master list entries and heartbeat callbacks:
303 * in order to avoid sleeping and allocation that occurs in
304 * heartbeat, master list entries are simply attached to the
305 * dlm's established heartbeat callbacks. the mle is attached
306 * when it is created, and since the dlm->spinlock is held at
307 * that time, any heartbeat event will be properly discovered
308 * by the mle. the mle needs to be detached from the
309 * dlm->mle_hb_events list as soon as heartbeat events are no
310 * longer useful to the mle, and before the mle is freed.
312 * as a general rule, heartbeat events are no longer needed by
313 * the mle once an "answer" regarding the lock master has been
316 static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
317 struct dlm_master_list_entry *mle)
319 assert_spin_locked(&dlm->spinlock);
321 list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
325 static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
326 struct dlm_master_list_entry *mle)
328 if (!list_empty(&mle->hb_events))
329 list_del_init(&mle->hb_events);
333 static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
334 struct dlm_master_list_entry *mle)
336 spin_lock(&dlm->spinlock);
337 __dlm_mle_detach_hb_events(dlm, mle);
338 spin_unlock(&dlm->spinlock);
341 static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
343 struct dlm_ctxt *dlm;
346 assert_spin_locked(&dlm->spinlock);
347 assert_spin_locked(&dlm->master_lock);
349 kref_get(&mle->mle_refs);
352 static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
354 struct dlm_ctxt *dlm;
357 spin_lock(&dlm->spinlock);
358 spin_lock(&dlm->master_lock);
361 spin_unlock(&dlm->master_lock);
362 spin_unlock(&dlm->spinlock);
366 /* remove from list and free */
367 static void __dlm_put_mle(struct dlm_master_list_entry *mle)
369 struct dlm_ctxt *dlm;
372 assert_spin_locked(&dlm->spinlock);
373 assert_spin_locked(&dlm->master_lock);
374 if (!atomic_read(&mle->mle_refs.refcount)) {
375 /* this may or may not crash, but who cares.
377 mlog(ML_ERROR, "bad mle: %p\n", mle);
378 dlm_print_one_mle(mle);
381 kref_put(&mle->mle_refs, dlm_mle_release);
385 /* must not have any spinlocks coming in */
386 static void dlm_put_mle(struct dlm_master_list_entry *mle)
388 struct dlm_ctxt *dlm;
391 spin_lock(&dlm->spinlock);
392 spin_lock(&dlm->master_lock);
394 spin_unlock(&dlm->master_lock);
395 spin_unlock(&dlm->spinlock);
398 static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
400 kref_get(&mle->mle_refs);
403 static void dlm_init_mle(struct dlm_master_list_entry *mle,
404 enum dlm_mle_type type,
405 struct dlm_ctxt *dlm,
406 struct dlm_lock_resource *res,
408 unsigned int namelen)
410 assert_spin_locked(&dlm->spinlock);
414 INIT_LIST_HEAD(&mle->list);
415 INIT_LIST_HEAD(&mle->hb_events);
416 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
417 spin_lock_init(&mle->spinlock);
418 init_waitqueue_head(&mle->wq);
419 atomic_set(&mle->woken, 0);
420 kref_init(&mle->mle_refs);
421 memset(mle->response_map, 0, sizeof(mle->response_map));
422 mle->master = O2NM_MAX_NODES;
423 mle->new_master = O2NM_MAX_NODES;
426 if (mle->type == DLM_MLE_MASTER) {
429 } else if (mle->type == DLM_MLE_BLOCK) {
431 memcpy(mle->u.name.name, name, namelen);
432 mle->u.name.len = namelen;
433 } else /* DLM_MLE_MIGRATION */ {
435 memcpy(mle->u.name.name, name, namelen);
436 mle->u.name.len = namelen;
439 /* copy off the node_map and register hb callbacks on our copy */
440 memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
441 memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
442 clear_bit(dlm->node_num, mle->vote_map);
443 clear_bit(dlm->node_num, mle->node_map);
445 /* attach the mle to the domain node up/down events */
446 __dlm_mle_attach_hb_events(dlm, mle);
450 /* returns 1 if found, 0 if not */
451 static int dlm_find_mle(struct dlm_ctxt *dlm,
452 struct dlm_master_list_entry **mle,
453 char *name, unsigned int namelen)
455 struct dlm_master_list_entry *tmpmle;
456 struct list_head *iter;
458 assert_spin_locked(&dlm->master_lock);
460 list_for_each(iter, &dlm->master_list) {
461 tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
462 if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
471 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
473 struct dlm_master_list_entry *mle;
474 struct list_head *iter;
476 assert_spin_locked(&dlm->spinlock);
478 list_for_each(iter, &dlm->mle_hb_events) {
479 mle = list_entry(iter, struct dlm_master_list_entry,
482 dlm_mle_node_up(dlm, mle, NULL, idx);
484 dlm_mle_node_down(dlm, mle, NULL, idx);
488 static void dlm_mle_node_down(struct dlm_ctxt *dlm,
489 struct dlm_master_list_entry *mle,
490 struct o2nm_node *node, int idx)
492 spin_lock(&mle->spinlock);
494 if (!test_bit(idx, mle->node_map))
495 mlog(0, "node %u already removed from nodemap!\n", idx);
497 clear_bit(idx, mle->node_map);
499 spin_unlock(&mle->spinlock);
502 static void dlm_mle_node_up(struct dlm_ctxt *dlm,
503 struct dlm_master_list_entry *mle,
504 struct o2nm_node *node, int idx)
506 spin_lock(&mle->spinlock);
508 if (test_bit(idx, mle->node_map))
509 mlog(0, "node %u already in node map!\n", idx);
511 set_bit(idx, mle->node_map);
513 spin_unlock(&mle->spinlock);
517 int dlm_init_mle_cache(void)
519 dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
520 sizeof(struct dlm_master_list_entry),
521 0, SLAB_HWCACHE_ALIGN,
523 if (dlm_mle_cache == NULL)
528 void dlm_destroy_mle_cache(void)
531 kmem_cache_destroy(dlm_mle_cache);
534 static void dlm_mle_release(struct kref *kref)
536 struct dlm_master_list_entry *mle;
537 struct dlm_ctxt *dlm;
541 mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
544 if (mle->type != DLM_MLE_MASTER) {
545 mlog(0, "calling mle_release for %.*s, type %d\n",
546 mle->u.name.len, mle->u.name.name, mle->type);
548 mlog(0, "calling mle_release for %.*s, type %d\n",
549 mle->u.res->lockname.len,
550 mle->u.res->lockname.name, mle->type);
552 assert_spin_locked(&dlm->spinlock);
553 assert_spin_locked(&dlm->master_lock);
555 /* remove from list if not already */
556 if (!list_empty(&mle->list))
557 list_del_init(&mle->list);
559 /* detach the mle from the domain node up/down events */
560 __dlm_mle_detach_hb_events(dlm, mle);
562 /* NOTE: kfree under spinlock here.
563 * if this is bad, we can move this to a freelist. */
564 kmem_cache_free(dlm_mle_cache, mle);
569 * LOCK RESOURCE FUNCTIONS
572 static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
573 struct dlm_lock_resource *res,
576 assert_spin_locked(&res->spinlock);
578 mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
580 if (owner == dlm->node_num)
581 atomic_inc(&dlm->local_resources);
582 else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
583 atomic_inc(&dlm->unknown_resources);
585 atomic_inc(&dlm->remote_resources);
590 void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
591 struct dlm_lock_resource *res, u8 owner)
593 assert_spin_locked(&res->spinlock);
595 if (owner == res->owner)
598 if (res->owner == dlm->node_num)
599 atomic_dec(&dlm->local_resources);
600 else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
601 atomic_dec(&dlm->unknown_resources);
603 atomic_dec(&dlm->remote_resources);
605 dlm_set_lockres_owner(dlm, res, owner);
609 static void dlm_lockres_release(struct kref *kref)
611 struct dlm_lock_resource *res;
613 res = container_of(kref, struct dlm_lock_resource, refs);
615 /* This should not happen -- all lockres' have a name
616 * associated with them at init time. */
617 BUG_ON(!res->lockname.name);
619 mlog(0, "destroying lockres %.*s\n", res->lockname.len,
622 if (!hlist_unhashed(&res->hash_node) ||
623 !list_empty(&res->granted) ||
624 !list_empty(&res->converting) ||
625 !list_empty(&res->blocked) ||
626 !list_empty(&res->dirty) ||
627 !list_empty(&res->recovering) ||
628 !list_empty(&res->purge)) {
630 "Going to BUG for resource %.*s."
631 " We're on a list! [%c%c%c%c%c%c%c]\n",
632 res->lockname.len, res->lockname.name,
633 !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
634 !list_empty(&res->granted) ? 'G' : ' ',
635 !list_empty(&res->converting) ? 'C' : ' ',
636 !list_empty(&res->blocked) ? 'B' : ' ',
637 !list_empty(&res->dirty) ? 'D' : ' ',
638 !list_empty(&res->recovering) ? 'R' : ' ',
639 !list_empty(&res->purge) ? 'P' : ' ');
641 dlm_print_one_lock_resource(res);
644 /* By the time we're ready to blow this guy away, we shouldn't
645 * be on any lists. */
646 BUG_ON(!hlist_unhashed(&res->hash_node));
647 BUG_ON(!list_empty(&res->granted));
648 BUG_ON(!list_empty(&res->converting));
649 BUG_ON(!list_empty(&res->blocked));
650 BUG_ON(!list_empty(&res->dirty));
651 BUG_ON(!list_empty(&res->recovering));
652 BUG_ON(!list_empty(&res->purge));
654 kfree(res->lockname.name);
659 void dlm_lockres_put(struct dlm_lock_resource *res)
661 kref_put(&res->refs, dlm_lockres_release);
664 static void dlm_init_lockres(struct dlm_ctxt *dlm,
665 struct dlm_lock_resource *res,
666 const char *name, unsigned int namelen)
670 /* If we memset here, we lose our reference to the kmalloc'd
671 * res->lockname.name, so be sure to init every field
674 qname = (char *) res->lockname.name;
675 memcpy(qname, name, namelen);
677 res->lockname.len = namelen;
678 res->lockname.hash = dlm_lockid_hash(name, namelen);
680 init_waitqueue_head(&res->wq);
681 spin_lock_init(&res->spinlock);
682 INIT_HLIST_NODE(&res->hash_node);
683 INIT_LIST_HEAD(&res->granted);
684 INIT_LIST_HEAD(&res->converting);
685 INIT_LIST_HEAD(&res->blocked);
686 INIT_LIST_HEAD(&res->dirty);
687 INIT_LIST_HEAD(&res->recovering);
688 INIT_LIST_HEAD(&res->purge);
689 atomic_set(&res->asts_reserved, 0);
690 res->migration_pending = 0;
691 res->inflight_locks = 0;
693 kref_init(&res->refs);
695 /* just for consistency */
696 spin_lock(&res->spinlock);
697 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
698 spin_unlock(&res->spinlock);
700 res->state = DLM_LOCK_RES_IN_PROGRESS;
704 memset(res->lvb, 0, DLM_LVB_LEN);
705 memset(res->refmap, 0, sizeof(res->refmap));
708 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
710 unsigned int namelen)
712 struct dlm_lock_resource *res;
714 res = kmalloc(sizeof(struct dlm_lock_resource), GFP_NOFS);
718 res->lockname.name = kmalloc(namelen, GFP_NOFS);
719 if (!res->lockname.name) {
724 dlm_init_lockres(dlm, res, name, namelen);
728 void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
729 struct dlm_lock_resource *res,
735 assert_spin_locked(&res->spinlock);
737 if (!test_bit(dlm->node_num, res->refmap)) {
738 BUG_ON(res->inflight_locks != 0);
739 dlm_lockres_set_refmap_bit(dlm->node_num, res);
741 res->inflight_locks++;
742 mlog(0, "%s:%.*s: inflight++: now %u\n",
743 dlm->name, res->lockname.len, res->lockname.name,
744 res->inflight_locks);
747 void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
748 struct dlm_lock_resource *res,
752 assert_spin_locked(&res->spinlock);
754 BUG_ON(res->inflight_locks == 0);
755 res->inflight_locks--;
756 mlog(0, "%s:%.*s: inflight--: now %u\n",
757 dlm->name, res->lockname.len, res->lockname.name,
758 res->inflight_locks);
759 if (res->inflight_locks == 0)
760 dlm_lockres_clear_refmap_bit(dlm->node_num, res);
765 * lookup a lock resource by name.
766 * may already exist in the hashtable.
767 * lockid is null terminated
769 * if not, allocate enough for the lockres and for
770 * the temporary structure used in doing the mastering.
772 * also, do a lookup in the dlm->master_list to see
773 * if another node has begun mastering the same lock.
774 * if so, there should be a block entry in there
775 * for this name, and we should *not* attempt to master
776 * the lock here. need to wait around for that node
777 * to assert_master (or die).
780 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
785 struct dlm_lock_resource *tmpres=NULL, *res=NULL;
786 struct dlm_master_list_entry *mle = NULL;
787 struct dlm_master_list_entry *alloc_mle = NULL;
790 struct dlm_node_iter iter;
793 int bit, wait_on_recovery = 0;
794 int drop_inflight_if_nonlocal = 0;
798 hash = dlm_lockid_hash(lockid, namelen);
800 mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
803 spin_lock(&dlm->spinlock);
804 tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
806 int dropping_ref = 0;
808 spin_lock(&tmpres->spinlock);
809 if (tmpres->owner == dlm->node_num) {
810 BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
811 dlm_lockres_grab_inflight_ref(dlm, tmpres);
812 } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
814 spin_unlock(&tmpres->spinlock);
815 spin_unlock(&dlm->spinlock);
817 /* wait until done messaging the master, drop our ref to allow
818 * the lockres to be purged, start over. */
820 spin_lock(&tmpres->spinlock);
821 __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
822 spin_unlock(&tmpres->spinlock);
823 dlm_lockres_put(tmpres);
828 mlog(0, "found in hash!\n");
830 dlm_lockres_put(res);
836 spin_unlock(&dlm->spinlock);
837 mlog(0, "allocating a new resource\n");
838 /* nothing found and we need to allocate one. */
839 alloc_mle = (struct dlm_master_list_entry *)
840 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
843 res = dlm_new_lockres(dlm, lockid, namelen);
849 mlog(0, "no lockres found, allocated our own: %p\n", res);
851 if (flags & LKM_LOCAL) {
852 /* caller knows it's safe to assume it's not mastered elsewhere
853 * DONE! return right away */
854 spin_lock(&res->spinlock);
855 dlm_change_lockres_owner(dlm, res, dlm->node_num);
856 __dlm_insert_lockres(dlm, res);
857 dlm_lockres_grab_inflight_ref(dlm, res);
858 spin_unlock(&res->spinlock);
859 spin_unlock(&dlm->spinlock);
860 /* lockres still marked IN_PROGRESS */
864 /* check master list to see if another node has started mastering it */
865 spin_lock(&dlm->master_lock);
867 /* if we found a block, wait for lock to be mastered by another node */
868 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
871 if (mle->type == DLM_MLE_MASTER) {
872 mlog(ML_ERROR, "master entry for nonexistent lock!\n");
875 mig = (mle->type == DLM_MLE_MIGRATION);
876 /* if there is a migration in progress, let the migration
877 * finish before continuing. we can wait for the absence
878 * of the MIGRATION mle: either the migrate finished or
879 * one of the nodes died and the mle was cleaned up.
880 * if there is a BLOCK here, but it already has a master
881 * set, we are too late. the master does not have a ref
882 * for us in the refmap. detach the mle and drop it.
883 * either way, go back to the top and start over. */
884 if (mig || mle->master != O2NM_MAX_NODES) {
885 BUG_ON(mig && mle->master == dlm->node_num);
886 /* we arrived too late. the master does not
887 * have a ref for us. retry. */
888 mlog(0, "%s:%.*s: late on %s\n",
889 dlm->name, namelen, lockid,
890 mig ? "MIGRATION" : "BLOCK");
891 spin_unlock(&dlm->master_lock);
892 spin_unlock(&dlm->spinlock);
894 /* master is known, detach */
896 dlm_mle_detach_hb_events(dlm, mle);
899 /* this is lame, but we cant wait on either
900 * the mle or lockres waitqueue here */
906 /* go ahead and try to master lock on this node */
908 /* make sure this does not get freed below */
910 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
911 set_bit(dlm->node_num, mle->maybe_map);
912 list_add(&mle->list, &dlm->master_list);
914 /* still holding the dlm spinlock, check the recovery map
915 * to see if there are any nodes that still need to be
916 * considered. these will not appear in the mle nodemap
917 * but they might own this lockres. wait on them. */
918 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
919 if (bit < O2NM_MAX_NODES) {
920 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
921 "recover before lock mastery can begin\n",
922 dlm->name, namelen, (char *)lockid, bit);
923 wait_on_recovery = 1;
927 /* at this point there is either a DLM_MLE_BLOCK or a
928 * DLM_MLE_MASTER on the master list, so it's safe to add the
929 * lockres to the hashtable. anyone who finds the lock will
930 * still have to wait on the IN_PROGRESS. */
932 /* finally add the lockres to its hash bucket */
933 __dlm_insert_lockres(dlm, res);
934 /* since this lockres is new it doesnt not require the spinlock */
935 dlm_lockres_grab_inflight_ref_new(dlm, res);
937 /* if this node does not become the master make sure to drop
938 * this inflight reference below */
939 drop_inflight_if_nonlocal = 1;
941 /* get an extra ref on the mle in case this is a BLOCK
942 * if so, the creator of the BLOCK may try to put the last
943 * ref at this time in the assert master handler, so we
944 * need an extra one to keep from a bad ptr deref. */
945 dlm_get_mle_inuse(mle);
946 spin_unlock(&dlm->master_lock);
947 spin_unlock(&dlm->spinlock);
950 while (wait_on_recovery) {
951 /* any cluster changes that occurred after dropping the
952 * dlm spinlock would be detectable be a change on the mle,
953 * so we only need to clear out the recovery map once. */
954 if (dlm_is_recovery_lock(lockid, namelen)) {
955 mlog(ML_NOTICE, "%s: recovery map is not empty, but "
956 "must master $RECOVERY lock now\n", dlm->name);
957 if (!dlm_pre_master_reco_lockres(dlm, res))
958 wait_on_recovery = 0;
960 mlog(0, "%s: waiting 500ms for heartbeat state "
961 "change\n", dlm->name);
967 dlm_kick_recovery_thread(dlm);
969 dlm_wait_for_recovery(dlm);
971 spin_lock(&dlm->spinlock);
972 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
973 if (bit < O2NM_MAX_NODES) {
974 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
975 "recover before lock mastery can begin\n",
976 dlm->name, namelen, (char *)lockid, bit);
977 wait_on_recovery = 1;
979 wait_on_recovery = 0;
980 spin_unlock(&dlm->spinlock);
982 if (wait_on_recovery)
983 dlm_wait_for_node_recovery(dlm, bit, 10000);
986 /* must wait for lock to be mastered elsewhere */
991 dlm_node_iter_init(mle->vote_map, &iter);
992 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
993 ret = dlm_do_master_request(res, mle, nodenum);
996 if (mle->master != O2NM_MAX_NODES) {
997 /* found a master ! */
998 if (mle->master <= nodenum)
1000 /* if our master request has not reached the master
1001 * yet, keep going until it does. this is how the
1002 * master will know that asserts are needed back to
1003 * the lower nodes. */
1004 mlog(0, "%s:%.*s: requests only up to %u but master "
1005 "is %u, keep going\n", dlm->name, namelen,
1006 lockid, nodenum, mle->master);
1011 /* keep going until the response map includes all nodes */
1012 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
1014 wait_on_recovery = 1;
1015 mlog(0, "%s:%.*s: node map changed, redo the "
1016 "master request now, blocked=%d\n",
1017 dlm->name, res->lockname.len,
1018 res->lockname.name, blocked);
1020 mlog(ML_ERROR, "%s:%.*s: spinning on "
1021 "dlm_wait_for_lock_mastery, blocked=%d\n",
1022 dlm->name, res->lockname.len,
1023 res->lockname.name, blocked);
1024 dlm_print_one_lock_resource(res);
1025 dlm_print_one_mle(mle);
1031 mlog(0, "lockres mastered by %u\n", res->owner);
1032 /* make sure we never continue without this */
1033 BUG_ON(res->owner == O2NM_MAX_NODES);
1035 /* master is known, detach if not already detached */
1036 dlm_mle_detach_hb_events(dlm, mle);
1038 /* put the extra ref */
1039 dlm_put_mle_inuse(mle);
1042 spin_lock(&res->spinlock);
1043 if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
1044 dlm_lockres_drop_inflight_ref(dlm, res);
1045 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1046 spin_unlock(&res->spinlock);
1050 /* need to free the unused mle */
1052 kmem_cache_free(dlm_mle_cache, alloc_mle);
1058 #define DLM_MASTERY_TIMEOUT_MS 5000
1060 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
1061 struct dlm_lock_resource *res,
1062 struct dlm_master_list_entry *mle,
1067 int map_changed, voting_done;
1074 /* check if another node has already become the owner */
1075 spin_lock(&res->spinlock);
1076 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1077 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
1078 res->lockname.len, res->lockname.name, res->owner);
1079 spin_unlock(&res->spinlock);
1080 /* this will cause the master to re-assert across
1081 * the whole cluster, freeing up mles */
1082 if (res->owner != dlm->node_num) {
1083 ret = dlm_do_master_request(res, mle, res->owner);
1085 /* give recovery a chance to run */
1086 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
1094 spin_unlock(&res->spinlock);
1096 spin_lock(&mle->spinlock);
1098 map_changed = (memcmp(mle->vote_map, mle->node_map,
1099 sizeof(mle->vote_map)) != 0);
1100 voting_done = (memcmp(mle->vote_map, mle->response_map,
1101 sizeof(mle->vote_map)) == 0);
1103 /* restart if we hit any errors */
1106 mlog(0, "%s: %.*s: node map changed, restarting\n",
1107 dlm->name, res->lockname.len, res->lockname.name);
1108 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
1109 b = (mle->type == DLM_MLE_BLOCK);
1110 if ((*blocked && !b) || (!*blocked && b)) {
1111 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1112 dlm->name, res->lockname.len, res->lockname.name,
1116 spin_unlock(&mle->spinlock);
1121 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1122 "rechecking now\n", dlm->name, res->lockname.len,
1123 res->lockname.name);
1127 mlog(0, "map not changed and voting not done "
1128 "for %s:%.*s\n", dlm->name, res->lockname.len,
1129 res->lockname.name);
1133 if (m != O2NM_MAX_NODES) {
1134 /* another node has done an assert!
1139 /* have all nodes responded? */
1140 if (voting_done && !*blocked) {
1141 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
1142 if (dlm->node_num <= bit) {
1143 /* my node number is lowest.
1144 * now tell other nodes that I am
1145 * mastering this. */
1146 mle->master = dlm->node_num;
1147 /* ref was grabbed in get_lock_resource
1148 * will be dropped in dlmlock_master */
1152 /* if voting is done, but we have not received
1153 * an assert master yet, we must sleep */
1157 spin_unlock(&mle->spinlock);
1159 /* sleep if we haven't finished voting yet */
1161 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1164 if (atomic_read(&mle->mle_refs.refcount) < 2)
1165 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1166 atomic_read(&mle->mle_refs.refcount),
1167 res->lockname.len, res->lockname.name);
1169 atomic_set(&mle->woken, 0);
1170 (void)wait_event_timeout(mle->wq,
1171 (atomic_read(&mle->woken) == 1),
1173 if (res->owner == O2NM_MAX_NODES) {
1174 mlog(0, "%s:%.*s: waiting again\n", dlm->name,
1175 res->lockname.len, res->lockname.name);
1178 mlog(0, "done waiting, master is %u\n", res->owner);
1186 mlog(0, "about to master %.*s here, this=%u\n",
1187 res->lockname.len, res->lockname.name, m);
1188 ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
1190 /* This is a failure in the network path,
1191 * not in the response to the assert_master
1192 * (any nonzero response is a BUG on this node).
1193 * Most likely a socket just got disconnected
1194 * due to node death. */
1197 /* no longer need to restart lock mastery.
1198 * all living nodes have been contacted. */
1202 /* set the lockres owner */
1203 spin_lock(&res->spinlock);
1204 /* mastery reference obtained either during
1205 * assert_master_handler or in get_lock_resource */
1206 dlm_change_lockres_owner(dlm, res, m);
1207 spin_unlock(&res->spinlock);
1213 struct dlm_bitmap_diff_iter
1216 unsigned long *orig_bm;
1217 unsigned long *cur_bm;
1218 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1221 enum dlm_node_state_change
1228 static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1229 unsigned long *orig_bm,
1230 unsigned long *cur_bm)
1232 unsigned long p1, p2;
1236 iter->orig_bm = orig_bm;
1237 iter->cur_bm = cur_bm;
1239 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1240 p1 = *(iter->orig_bm + i);
1241 p2 = *(iter->cur_bm + i);
1242 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1246 static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1247 enum dlm_node_state_change *state)
1251 if (iter->curnode >= O2NM_MAX_NODES)
1254 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1256 if (bit >= O2NM_MAX_NODES) {
1257 iter->curnode = O2NM_MAX_NODES;
1261 /* if it was there in the original then this node died */
1262 if (test_bit(bit, iter->orig_bm))
1267 iter->curnode = bit;
1272 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1273 struct dlm_lock_resource *res,
1274 struct dlm_master_list_entry *mle,
1277 struct dlm_bitmap_diff_iter bdi;
1278 enum dlm_node_state_change sc;
1282 mlog(0, "something happened such that the "
1283 "master process may need to be restarted!\n");
1285 assert_spin_locked(&mle->spinlock);
1287 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1288 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1290 if (sc == NODE_UP) {
1291 /* a node came up. clear any old vote from
1292 * the response map and set it in the vote map
1293 * then restart the mastery. */
1294 mlog(ML_NOTICE, "node %d up while restarting\n", node);
1296 /* redo the master request, but only for the new node */
1297 mlog(0, "sending request to new node\n");
1298 clear_bit(node, mle->response_map);
1299 set_bit(node, mle->vote_map);
1301 mlog(ML_ERROR, "node down! %d\n", node);
1303 int lowest = find_next_bit(mle->maybe_map,
1306 /* act like it was never there */
1307 clear_bit(node, mle->maybe_map);
1309 if (node == lowest) {
1310 mlog(0, "expected master %u died"
1311 " while this node was blocked "
1312 "waiting on it!\n", node);
1313 lowest = find_next_bit(mle->maybe_map,
1316 if (lowest < O2NM_MAX_NODES) {
1317 mlog(0, "%s:%.*s:still "
1318 "blocked. waiting on %u "
1324 /* mle is an MLE_BLOCK, but
1325 * there is now nothing left to
1326 * block on. we need to return
1327 * all the way back out and try
1328 * again with an MLE_MASTER.
1329 * dlm_do_local_recovery_cleanup
1330 * has already run, so the mle
1332 mlog(0, "%s:%.*s: no "
1333 "longer blocking. try to "
1334 "master this here\n",
1337 res->lockname.name);
1338 mle->type = DLM_MLE_MASTER;
1344 /* now blank out everything, as if we had never
1345 * contacted anyone */
1346 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
1347 memset(mle->response_map, 0, sizeof(mle->response_map));
1348 /* reset the vote_map to the current node_map */
1349 memcpy(mle->vote_map, mle->node_map,
1350 sizeof(mle->node_map));
1351 /* put myself into the maybe map */
1352 if (mle->type != DLM_MLE_BLOCK)
1353 set_bit(dlm->node_num, mle->maybe_map);
1356 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1363 * DLM_MASTER_REQUEST_MSG
1365 * returns: 0 on success,
1366 * -errno on a network error
1368 * on error, the caller should assume the target node is "dead"
1372 static int dlm_do_master_request(struct dlm_lock_resource *res,
1373 struct dlm_master_list_entry *mle, int to)
1375 struct dlm_ctxt *dlm = mle->dlm;
1376 struct dlm_master_request request;
1377 int ret, response=0, resend;
1379 memset(&request, 0, sizeof(request));
1380 request.node_idx = dlm->node_num;
1382 BUG_ON(mle->type == DLM_MLE_MIGRATION);
1384 if (mle->type != DLM_MLE_MASTER) {
1385 request.namelen = mle->u.name.len;
1386 memcpy(request.name, mle->u.name.name, request.namelen);
1388 request.namelen = mle->u.res->lockname.len;
1389 memcpy(request.name, mle->u.res->lockname.name,
1394 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1395 sizeof(request), to, &response);
1397 if (ret == -ESRCH) {
1398 /* should never happen */
1399 mlog(ML_ERROR, "TCP stack not ready!\n");
1401 } else if (ret == -EINVAL) {
1402 mlog(ML_ERROR, "bad args passed to o2net!\n");
1404 } else if (ret == -ENOMEM) {
1405 mlog(ML_ERROR, "out of memory while trying to send "
1406 "network message! retrying\n");
1407 /* this is totally crude */
1410 } else if (!dlm_is_host_down(ret)) {
1411 /* not a network error. bad. */
1413 mlog(ML_ERROR, "unhandled error!");
1416 /* all other errors should be network errors,
1417 * and likely indicate node death */
1418 mlog(ML_ERROR, "link to %d went down!\n", to);
1424 spin_lock(&mle->spinlock);
1426 case DLM_MASTER_RESP_YES:
1427 set_bit(to, mle->response_map);
1428 mlog(0, "node %u is the master, response=YES\n", to);
1429 mlog(0, "%s:%.*s: master node %u now knows I have a "
1430 "reference\n", dlm->name, res->lockname.len,
1431 res->lockname.name, to);
1434 case DLM_MASTER_RESP_NO:
1435 mlog(0, "node %u not master, response=NO\n", to);
1436 set_bit(to, mle->response_map);
1438 case DLM_MASTER_RESP_MAYBE:
1439 mlog(0, "node %u not master, response=MAYBE\n", to);
1440 set_bit(to, mle->response_map);
1441 set_bit(to, mle->maybe_map);
1443 case DLM_MASTER_RESP_ERROR:
1444 mlog(0, "node %u hit an error, resending\n", to);
1449 mlog(ML_ERROR, "bad response! %u\n", response);
1452 spin_unlock(&mle->spinlock);
1454 /* this is also totally crude */
1464 * locks that can be taken here:
1470 * if possible, TRIM THIS DOWN!!!
1472 int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
1475 u8 response = DLM_MASTER_RESP_MAYBE;
1476 struct dlm_ctxt *dlm = data;
1477 struct dlm_lock_resource *res = NULL;
1478 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1479 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1481 unsigned int namelen, hash;
1484 int dispatch_assert = 0;
1487 return DLM_MASTER_RESP_NO;
1489 if (!dlm_domain_fully_joined(dlm)) {
1490 response = DLM_MASTER_RESP_NO;
1494 name = request->name;
1495 namelen = request->namelen;
1496 hash = dlm_lockid_hash(name, namelen);
1498 if (namelen > DLM_LOCKID_NAME_MAX) {
1499 response = DLM_IVBUFLEN;
1504 spin_lock(&dlm->spinlock);
1505 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1507 spin_unlock(&dlm->spinlock);
1509 /* take care of the easy cases up front */
1510 spin_lock(&res->spinlock);
1511 if (res->state & (DLM_LOCK_RES_RECOVERING|
1512 DLM_LOCK_RES_MIGRATING)) {
1513 spin_unlock(&res->spinlock);
1514 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1515 "being recovered/migrated\n");
1516 response = DLM_MASTER_RESP_ERROR;
1518 kmem_cache_free(dlm_mle_cache, mle);
1522 if (res->owner == dlm->node_num) {
1523 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1524 dlm->name, namelen, name, request->node_idx);
1525 dlm_lockres_set_refmap_bit(request->node_idx, res);
1526 spin_unlock(&res->spinlock);
1527 response = DLM_MASTER_RESP_YES;
1529 kmem_cache_free(dlm_mle_cache, mle);
1531 /* this node is the owner.
1532 * there is some extra work that needs to
1533 * happen now. the requesting node has
1534 * caused all nodes up to this one to
1535 * create mles. this node now needs to
1536 * go back and clean those up. */
1537 dispatch_assert = 1;
1539 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1540 spin_unlock(&res->spinlock);
1541 // mlog(0, "node %u is the master\n", res->owner);
1542 response = DLM_MASTER_RESP_NO;
1544 kmem_cache_free(dlm_mle_cache, mle);
1548 /* ok, there is no owner. either this node is
1549 * being blocked, or it is actively trying to
1550 * master this lock. */
1551 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1552 mlog(ML_ERROR, "lock with no owner should be "
1557 // mlog(0, "lockres is in progress...\n");
1558 spin_lock(&dlm->master_lock);
1559 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1561 mlog(ML_ERROR, "no mle found for this lock!\n");
1565 spin_lock(&tmpmle->spinlock);
1566 if (tmpmle->type == DLM_MLE_BLOCK) {
1567 // mlog(0, "this node is waiting for "
1568 // "lockres to be mastered\n");
1569 response = DLM_MASTER_RESP_NO;
1570 } else if (tmpmle->type == DLM_MLE_MIGRATION) {
1571 mlog(0, "node %u is master, but trying to migrate to "
1572 "node %u.\n", tmpmle->master, tmpmle->new_master);
1573 if (tmpmle->master == dlm->node_num) {
1574 mlog(ML_ERROR, "no owner on lockres, but this "
1575 "node is trying to migrate it to %u?!\n",
1576 tmpmle->new_master);
1579 /* the real master can respond on its own */
1580 response = DLM_MASTER_RESP_NO;
1582 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1584 if (tmpmle->master == dlm->node_num) {
1585 response = DLM_MASTER_RESP_YES;
1586 /* this node will be the owner.
1587 * go back and clean the mles on any
1589 dispatch_assert = 1;
1590 dlm_lockres_set_refmap_bit(request->node_idx, res);
1591 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1592 dlm->name, namelen, name,
1595 response = DLM_MASTER_RESP_NO;
1597 // mlog(0, "this node is attempting to "
1598 // "master lockres\n");
1599 response = DLM_MASTER_RESP_MAYBE;
1602 set_bit(request->node_idx, tmpmle->maybe_map);
1603 spin_unlock(&tmpmle->spinlock);
1605 spin_unlock(&dlm->master_lock);
1606 spin_unlock(&res->spinlock);
1608 /* keep the mle attached to heartbeat events */
1609 dlm_put_mle(tmpmle);
1611 kmem_cache_free(dlm_mle_cache, mle);
1616 * lockres doesn't exist on this node
1617 * if there is an MLE_BLOCK, return NO
1618 * if there is an MLE_MASTER, return MAYBE
1619 * otherwise, add an MLE_BLOCK, return NO
1621 spin_lock(&dlm->master_lock);
1622 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1624 /* this lockid has never been seen on this node yet */
1625 // mlog(0, "no mle found\n");
1627 spin_unlock(&dlm->master_lock);
1628 spin_unlock(&dlm->spinlock);
1630 mle = (struct dlm_master_list_entry *)
1631 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
1633 response = DLM_MASTER_RESP_ERROR;
1634 mlog_errno(-ENOMEM);
1640 // mlog(0, "this is second time thru, already allocated, "
1641 // "add the block.\n");
1642 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
1643 set_bit(request->node_idx, mle->maybe_map);
1644 list_add(&mle->list, &dlm->master_list);
1645 response = DLM_MASTER_RESP_NO;
1647 // mlog(0, "mle was found\n");
1649 spin_lock(&tmpmle->spinlock);
1650 if (tmpmle->master == dlm->node_num) {
1651 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1654 if (tmpmle->type == DLM_MLE_BLOCK)
1655 response = DLM_MASTER_RESP_NO;
1656 else if (tmpmle->type == DLM_MLE_MIGRATION) {
1657 mlog(0, "migration mle was found (%u->%u)\n",
1658 tmpmle->master, tmpmle->new_master);
1659 /* real master can respond on its own */
1660 response = DLM_MASTER_RESP_NO;
1662 response = DLM_MASTER_RESP_MAYBE;
1664 set_bit(request->node_idx, tmpmle->maybe_map);
1665 spin_unlock(&tmpmle->spinlock);
1667 spin_unlock(&dlm->master_lock);
1668 spin_unlock(&dlm->spinlock);
1671 /* keep the mle attached to heartbeat events */
1672 dlm_put_mle(tmpmle);
1676 if (dispatch_assert) {
1677 if (response != DLM_MASTER_RESP_YES)
1678 mlog(ML_ERROR, "invalid response %d\n", response);
1680 mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1683 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1684 dlm->node_num, res->lockname.len, res->lockname.name);
1685 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1686 DLM_ASSERT_MASTER_MLE_CLEANUP);
1688 mlog(ML_ERROR, "failed to dispatch assert master work\n");
1689 response = DLM_MASTER_RESP_ERROR;
1698 * DLM_ASSERT_MASTER_MSG
1703 * NOTE: this can be used for debugging
1704 * can periodically run all locks owned by this node
1705 * and re-assert across the cluster...
1707 int dlm_do_assert_master(struct dlm_ctxt *dlm,
1708 struct dlm_lock_resource *res,
1709 void *nodemap, u32 flags)
1711 struct dlm_assert_master assert;
1713 struct dlm_node_iter iter;
1716 const char *lockname = res->lockname.name;
1717 unsigned int namelen = res->lockname.len;
1719 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
1723 /* note that if this nodemap is empty, it returns 0 */
1724 dlm_node_iter_init(nodemap, &iter);
1725 while ((to = dlm_node_iter_next(&iter)) >= 0) {
1727 struct dlm_master_list_entry *mle = NULL;
1729 mlog(0, "sending assert master to %d (%.*s)\n", to,
1731 memset(&assert, 0, sizeof(assert));
1732 assert.node_idx = dlm->node_num;
1733 assert.namelen = namelen;
1734 memcpy(assert.name, lockname, namelen);
1735 assert.flags = cpu_to_be32(flags);
1737 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1738 &assert, sizeof(assert), to, &r);
1740 mlog(0, "assert_master returned %d!\n", tmpret);
1741 if (!dlm_is_host_down(tmpret)) {
1742 mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
1745 /* a node died. finish out the rest of the nodes. */
1746 mlog(0, "link to %d went down!\n", to);
1747 /* any nonzero status return will do */
1751 /* ok, something horribly messed. kill thyself. */
1752 mlog(ML_ERROR,"during assert master of %.*s to %u, "
1753 "got %d.\n", namelen, lockname, to, r);
1754 spin_lock(&dlm->spinlock);
1755 spin_lock(&dlm->master_lock);
1756 if (dlm_find_mle(dlm, &mle, (char *)lockname,
1758 dlm_print_one_mle(mle);
1761 spin_unlock(&dlm->master_lock);
1762 spin_unlock(&dlm->spinlock);
1766 if (r & DLM_ASSERT_RESPONSE_REASSERT &&
1767 !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
1768 mlog(ML_ERROR, "%.*s: very strange, "
1769 "master MLE but no lockres on %u\n",
1770 namelen, lockname, to);
1773 if (r & DLM_ASSERT_RESPONSE_REASSERT) {
1774 mlog(0, "%.*s: node %u create mles on other "
1775 "nodes and requests a re-assert\n",
1776 namelen, lockname, to);
1779 if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
1780 mlog(0, "%.*s: node %u has a reference to this "
1781 "lockres, set the bit in the refmap\n",
1782 namelen, lockname, to);
1783 spin_lock(&res->spinlock);
1784 dlm_lockres_set_refmap_bit(to, res);
1785 spin_unlock(&res->spinlock);
1796 * locks that can be taken here:
1802 * if possible, TRIM THIS DOWN!!!
1804 int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
1807 struct dlm_ctxt *dlm = data;
1808 struct dlm_master_list_entry *mle = NULL;
1809 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1810 struct dlm_lock_resource *res = NULL;
1812 unsigned int namelen, hash;
1814 int master_request = 0, have_lockres_ref = 0;
1820 name = assert->name;
1821 namelen = assert->namelen;
1822 hash = dlm_lockid_hash(name, namelen);
1823 flags = be32_to_cpu(assert->flags);
1825 if (namelen > DLM_LOCKID_NAME_MAX) {
1826 mlog(ML_ERROR, "Invalid name length!");
1830 spin_lock(&dlm->spinlock);
1833 mlog(0, "assert_master with flags: %u\n", flags);
1836 spin_lock(&dlm->master_lock);
1837 if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1838 /* not an error, could be master just re-asserting */
1839 mlog(0, "just got an assert_master from %u, but no "
1840 "MLE for it! (%.*s)\n", assert->node_idx,
1843 int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1844 if (bit >= O2NM_MAX_NODES) {
1845 /* not necessarily an error, though less likely.
1846 * could be master just re-asserting. */
1847 mlog(0, "no bits set in the maybe_map, but %u "
1848 "is asserting! (%.*s)\n", assert->node_idx,
1850 } else if (bit != assert->node_idx) {
1851 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1852 mlog(0, "master %u was found, %u should "
1853 "back off\n", assert->node_idx, bit);
1855 /* with the fix for bug 569, a higher node
1856 * number winning the mastery will respond
1857 * YES to mastery requests, but this node
1858 * had no way of knowing. let it pass. */
1859 mlog(0, "%u is the lowest node, "
1860 "%u is asserting. (%.*s) %u must "
1861 "have begun after %u won.\n", bit,
1862 assert->node_idx, namelen, name, bit,
1866 if (mle->type == DLM_MLE_MIGRATION) {
1867 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1868 mlog(0, "%s:%.*s: got cleanup assert"
1869 " from %u for migration\n",
1870 dlm->name, namelen, name,
1872 } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
1873 mlog(0, "%s:%.*s: got unrelated assert"
1874 " from %u for migration, ignoring\n",
1875 dlm->name, namelen, name,
1878 spin_unlock(&dlm->master_lock);
1879 spin_unlock(&dlm->spinlock);
1884 spin_unlock(&dlm->master_lock);
1886 /* ok everything checks out with the MLE
1887 * now check to see if there is a lockres */
1888 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1890 spin_lock(&res->spinlock);
1891 if (res->state & DLM_LOCK_RES_RECOVERING) {
1892 mlog(ML_ERROR, "%u asserting but %.*s is "
1893 "RECOVERING!\n", assert->node_idx, namelen, name);
1897 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
1898 res->owner != assert->node_idx) {
1899 mlog(ML_ERROR, "assert_master from "
1900 "%u, but current owner is "
1902 assert->node_idx, res->owner,
1906 } else if (mle->type != DLM_MLE_MIGRATION) {
1907 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1908 /* owner is just re-asserting */
1909 if (res->owner == assert->node_idx) {
1910 mlog(0, "owner %u re-asserting on "
1911 "lock %.*s\n", assert->node_idx,
1915 mlog(ML_ERROR, "got assert_master from "
1916 "node %u, but %u is the owner! "
1917 "(%.*s)\n", assert->node_idx,
1918 res->owner, namelen, name);
1921 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1922 mlog(ML_ERROR, "got assert from %u, but lock "
1923 "with no owner should be "
1924 "in-progress! (%.*s)\n",
1929 } else /* mle->type == DLM_MLE_MIGRATION */ {
1930 /* should only be getting an assert from new master */
1931 if (assert->node_idx != mle->new_master) {
1932 mlog(ML_ERROR, "got assert from %u, but "
1933 "new master is %u, and old master "
1935 assert->node_idx, mle->new_master,
1936 mle->master, namelen, name);
1942 spin_unlock(&res->spinlock);
1944 spin_unlock(&dlm->spinlock);
1946 // mlog(0, "woo! got an assert_master from node %u!\n",
1947 // assert->node_idx);
1953 spin_lock(&mle->spinlock);
1954 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1957 /* MASTER mle: if any bits set in the response map
1958 * then the calling node needs to re-assert to clear
1959 * up nodes that this node contacted */
1960 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1961 nn+1)) < O2NM_MAX_NODES) {
1962 if (nn != dlm->node_num && nn != assert->node_idx)
1966 mle->master = assert->node_idx;
1967 atomic_set(&mle->woken, 1);
1969 spin_unlock(&mle->spinlock);
1973 spin_lock(&res->spinlock);
1974 if (mle->type == DLM_MLE_MIGRATION) {
1975 mlog(0, "finishing off migration of lockres %.*s, "
1977 res->lockname.len, res->lockname.name,
1978 dlm->node_num, mle->new_master);
1979 res->state &= ~DLM_LOCK_RES_MIGRATING;
1981 dlm_change_lockres_owner(dlm, res, mle->new_master);
1982 BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
1984 dlm_change_lockres_owner(dlm, res, mle->master);
1986 spin_unlock(&res->spinlock);
1987 have_lockres_ref = 1;
1992 /* master is known, detach if not already detached.
1993 * ensures that only one assert_master call will happen
1995 spin_lock(&dlm->spinlock);
1996 spin_lock(&dlm->master_lock);
1998 rr = atomic_read(&mle->mle_refs.refcount);
1999 if (mle->inuse > 0) {
2000 if (extra_ref && rr < 3)
2002 else if (!extra_ref && rr < 2)
2005 if (extra_ref && rr < 2)
2007 else if (!extra_ref && rr < 1)
2011 mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
2012 "that will mess up this node, refs=%d, extra=%d, "
2013 "inuse=%d\n", dlm->name, namelen, name,
2014 assert->node_idx, rr, extra_ref, mle->inuse);
2015 dlm_print_one_mle(mle);
2017 list_del_init(&mle->list);
2018 __dlm_mle_detach_hb_events(dlm, mle);
2021 /* the assert master message now balances the extra
2022 * ref given by the master / migration request message.
2023 * if this is the last put, it will be removed
2027 spin_unlock(&dlm->master_lock);
2028 spin_unlock(&dlm->spinlock);
2030 if (res->owner != assert->node_idx) {
2031 mlog(0, "assert_master from %u, but current "
2032 "owner is %u (%.*s), no mle\n", assert->node_idx,
2033 res->owner, namelen, name);
2040 spin_lock(&res->spinlock);
2041 res->state |= DLM_LOCK_RES_SETREF_INPROG;
2042 spin_unlock(&res->spinlock);
2043 *ret_data = (void *)res;
2046 if (master_request) {
2047 mlog(0, "need to tell master to reassert\n");
2048 /* positive. negative would shoot down the node. */
2049 ret |= DLM_ASSERT_RESPONSE_REASSERT;
2050 if (!have_lockres_ref) {
2051 mlog(ML_ERROR, "strange, got assert from %u, MASTER "
2052 "mle present here for %s:%.*s, but no lockres!\n",
2053 assert->node_idx, dlm->name, namelen, name);
2056 if (have_lockres_ref) {
2057 /* let the master know we have a reference to the lockres */
2058 ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
2059 mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
2060 dlm->name, namelen, name, assert->node_idx);
2065 /* kill the caller! */
2066 mlog(ML_ERROR, "Bad message received from another node. Dumping state "
2067 "and killing the other node now! This node is OK and can continue.\n");
2068 __dlm_print_one_lock_resource(res);
2069 spin_unlock(&res->spinlock);
2070 spin_unlock(&dlm->spinlock);
2071 *ret_data = (void *)res;
2076 void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
2078 struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
2081 spin_lock(&res->spinlock);
2082 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
2083 spin_unlock(&res->spinlock);
2085 dlm_lockres_put(res);
2090 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
2091 struct dlm_lock_resource *res,
2092 int ignore_higher, u8 request_from, u32 flags)
2094 struct dlm_work_item *item;
2095 item = kzalloc(sizeof(*item), GFP_NOFS);
2100 /* queue up work for dlm_assert_master_worker */
2101 dlm_grab(dlm); /* get an extra ref for the work item */
2102 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
2103 item->u.am.lockres = res; /* already have a ref */
2104 /* can optionally ignore node numbers higher than this node */
2105 item->u.am.ignore_higher = ignore_higher;
2106 item->u.am.request_from = request_from;
2107 item->u.am.flags = flags;
2110 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
2111 res->lockname.name);
2113 spin_lock(&dlm->work_lock);
2114 list_add_tail(&item->list, &dlm->work_list);
2115 spin_unlock(&dlm->work_lock);
2117 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2121 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
2123 struct dlm_ctxt *dlm = data;
2125 struct dlm_lock_resource *res;
2126 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
2133 res = item->u.am.lockres;
2134 ignore_higher = item->u.am.ignore_higher;
2135 request_from = item->u.am.request_from;
2136 flags = item->u.am.flags;
2138 spin_lock(&dlm->spinlock);
2139 memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
2140 spin_unlock(&dlm->spinlock);
2142 clear_bit(dlm->node_num, nodemap);
2143 if (ignore_higher) {
2144 /* if is this just to clear up mles for nodes below
2145 * this node, do not send the message to the original
2146 * caller or any node number higher than this */
2147 clear_bit(request_from, nodemap);
2148 bit = dlm->node_num;
2150 bit = find_next_bit(nodemap, O2NM_MAX_NODES,
2152 if (bit >= O2NM_MAX_NODES)
2154 clear_bit(bit, nodemap);
2159 * If we're migrating this lock to someone else, we are no
2160 * longer allowed to assert out own mastery. OTOH, we need to
2161 * prevent migration from starting while we're still asserting
2162 * our dominance. The reserved ast delays migration.
2164 spin_lock(&res->spinlock);
2165 if (res->state & DLM_LOCK_RES_MIGRATING) {
2166 mlog(0, "Someone asked us to assert mastery, but we're "
2167 "in the middle of migration. Skipping assert, "
2168 "the new master will handle that.\n");
2169 spin_unlock(&res->spinlock);
2172 __dlm_lockres_reserve_ast(res);
2173 spin_unlock(&res->spinlock);
2175 /* this call now finishes out the nodemap
2176 * even if one or more nodes die */
2177 mlog(0, "worker about to master %.*s here, this=%u\n",
2178 res->lockname.len, res->lockname.name, dlm->node_num);
2179 ret = dlm_do_assert_master(dlm, res, nodemap, flags);
2181 /* no need to restart, we are done */
2182 if (!dlm_is_host_down(ret))
2186 /* Ok, we've asserted ourselves. Let's let migration start. */
2187 dlm_lockres_release_ast(dlm, res);
2190 dlm_lockres_put(res);
2192 mlog(0, "finished with dlm_assert_master_worker\n");
2195 /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2196 * We cannot wait for node recovery to complete to begin mastering this
2197 * lockres because this lockres is used to kick off recovery! ;-)
2198 * So, do a pre-check on all living nodes to see if any of those nodes
2199 * think that $RECOVERY is currently mastered by a dead node. If so,
2200 * we wait a short time to allow that node to get notified by its own
2201 * heartbeat stack, then check again. All $RECOVERY lock resources
2202 * mastered by dead nodes are purged when the hearbeat callback is
2203 * fired, so we can know for sure that it is safe to continue once
2204 * the node returns a live node or no node. */
2205 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
2206 struct dlm_lock_resource *res)
2208 struct dlm_node_iter iter;
2211 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
2213 spin_lock(&dlm->spinlock);
2214 dlm_node_iter_init(dlm->domain_map, &iter);
2215 spin_unlock(&dlm->spinlock);
2217 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2218 /* do not send to self */
2219 if (nodenum == dlm->node_num)
2221 ret = dlm_do_master_requery(dlm, res, nodenum, &master);
2224 if (!dlm_is_host_down(ret))
2226 /* host is down, so answer for that node would be
2227 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
2231 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
2232 /* check to see if this master is in the recovery map */
2233 spin_lock(&dlm->spinlock);
2234 if (test_bit(master, dlm->recovery_map)) {
2235 mlog(ML_NOTICE, "%s: node %u has not seen "
2236 "node %u go down yet, and thinks the "
2237 "dead node is mastering the recovery "
2238 "lock. must wait.\n", dlm->name,
2242 spin_unlock(&dlm->spinlock);
2243 mlog(0, "%s: reco lock master is %u\n", dlm->name,
2252 * DLM_DEREF_LOCKRES_MSG
2255 int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2257 struct dlm_deref_lockres deref;
2259 const char *lockname;
2260 unsigned int namelen;
2262 lockname = res->lockname.name;
2263 namelen = res->lockname.len;
2264 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
2266 mlog(0, "%s:%.*s: sending deref to %d\n",
2267 dlm->name, namelen, lockname, res->owner);
2268 memset(&deref, 0, sizeof(deref));
2269 deref.node_idx = dlm->node_num;
2270 deref.namelen = namelen;
2271 memcpy(deref.name, lockname, namelen);
2273 ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
2274 &deref, sizeof(deref), res->owner, &r);
2278 /* BAD. other node says I did not have a ref. */
2279 mlog(ML_ERROR,"while dropping ref on %s:%.*s "
2280 "(master=%u) got %d.\n", dlm->name, namelen,
2281 lockname, res->owner, r);
2282 dlm_print_one_lock_resource(res);
2288 int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
2291 struct dlm_ctxt *dlm = data;
2292 struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
2293 struct dlm_lock_resource *res = NULL;
2295 unsigned int namelen;
2304 namelen = deref->namelen;
2305 node = deref->node_idx;
2307 if (namelen > DLM_LOCKID_NAME_MAX) {
2308 mlog(ML_ERROR, "Invalid name length!");
2311 if (deref->node_idx >= O2NM_MAX_NODES) {
2312 mlog(ML_ERROR, "Invalid node number: %u\n", node);
2316 hash = dlm_lockid_hash(name, namelen);
2318 spin_lock(&dlm->spinlock);
2319 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
2321 spin_unlock(&dlm->spinlock);
2322 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
2323 dlm->name, namelen, name);
2326 spin_unlock(&dlm->spinlock);
2328 spin_lock(&res->spinlock);
2329 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2330 if (test_bit(node, res->refmap)) {
2332 dlm_lockres_clear_refmap_bit(node, res);
2334 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2335 "but it is already dropped!\n", dlm->name, namelen,
2337 __dlm_print_one_lock_resource(res);
2339 spin_unlock(&res->spinlock);
2342 dlm_lockres_calc_usage(dlm, res);
2345 dlm_lockres_put(res);
2352 * DLM_MIGRATE_LOCKRES
2356 static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
2357 struct dlm_lock_resource *res,
2360 struct dlm_master_list_entry *mle = NULL;
2361 struct dlm_master_list_entry *oldmle = NULL;
2362 struct dlm_migratable_lockres *mres = NULL;
2365 unsigned int namelen;
2367 struct list_head *queue, *iter;
2369 struct dlm_lock *lock;
2370 int empty = 1, wake = 0;
2375 name = res->lockname.name;
2376 namelen = res->lockname.len;
2378 mlog(0, "migrating %.*s to %u\n", namelen, name, target);
2381 * ensure this lockres is a proper candidate for migration
2383 spin_lock(&res->spinlock);
2384 if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2385 mlog(0, "cannot migrate lockres with unknown owner!\n");
2386 spin_unlock(&res->spinlock);
2389 if (res->owner != dlm->node_num) {
2390 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2391 spin_unlock(&res->spinlock);
2394 mlog(0, "checking queues...\n");
2395 queue = &res->granted;
2396 for (i=0; i<3; i++) {
2397 list_for_each(iter, queue) {
2398 lock = list_entry (iter, struct dlm_lock, list);
2400 if (lock->ml.node == dlm->node_num) {
2401 mlog(0, "found a lock owned by this node "
2402 "still on the %s queue! will not "
2403 "migrate this lockres\n",
2405 (i==1 ? "converting" : "blocked"));
2406 spin_unlock(&res->spinlock);
2413 mlog(0, "all locks on this lockres are nonlocal. continuing\n");
2414 spin_unlock(&res->spinlock);
2418 mlog(0, "no locks were found on this lockres! done!\n");
2424 * preallocate up front
2425 * if this fails, abort
2429 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
2435 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2444 * find a node to migrate the lockres to
2447 mlog(0, "picking a migration node\n");
2448 spin_lock(&dlm->spinlock);
2449 /* pick a new node */
2450 if (!test_bit(target, dlm->domain_map) ||
2451 target >= O2NM_MAX_NODES) {
2452 target = dlm_pick_migration_target(dlm, res);
2454 mlog(0, "node %u chosen for migration\n", target);
2456 if (target >= O2NM_MAX_NODES ||
2457 !test_bit(target, dlm->domain_map)) {
2458 /* target chosen is not alive */
2463 spin_unlock(&dlm->spinlock);
2467 mlog(0, "continuing with target = %u\n", target);
2470 * clear any existing master requests and
2471 * add the migration mle to the list
2473 spin_lock(&dlm->master_lock);
2474 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2475 namelen, target, dlm->node_num);
2476 spin_unlock(&dlm->master_lock);
2477 spin_unlock(&dlm->spinlock);
2479 if (ret == -EEXIST) {
2480 mlog(0, "another process is already migrating it\n");
2486 * set the MIGRATING flag and flush asts
2487 * if we fail after this we need to re-dirty the lockres
2489 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2490 mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2491 "the target went down.\n", res->lockname.len,
2492 res->lockname.name, target);
2493 spin_lock(&res->spinlock);
2494 res->state &= ~DLM_LOCK_RES_MIGRATING;
2496 spin_unlock(&res->spinlock);
2502 /* master is known, detach if not already detached */
2503 dlm_mle_detach_hb_events(dlm, oldmle);
2504 dlm_put_mle(oldmle);
2509 dlm_mle_detach_hb_events(dlm, mle);
2512 kmem_cache_free(dlm_mle_cache, mle);
2518 * at this point, we have a migration target, an mle
2519 * in the master list, and the MIGRATING flag set on
2523 /* now that remote nodes are spinning on the MIGRATING flag,
2524 * ensure that all assert_master work is flushed. */
2525 flush_workqueue(dlm->dlm_worker);
2527 /* get an extra reference on the mle.
2528 * otherwise the assert_master from the new
2529 * master will destroy this.
2530 * also, make sure that all callers of dlm_get_mle
2531 * take both dlm->spinlock and dlm->master_lock */
2532 spin_lock(&dlm->spinlock);
2533 spin_lock(&dlm->master_lock);
2534 dlm_get_mle_inuse(mle);
2535 spin_unlock(&dlm->master_lock);
2536 spin_unlock(&dlm->spinlock);
2538 /* notify new node and send all lock state */
2539 /* call send_one_lockres with migration flag.
2540 * this serves as notice to the target node that a
2541 * migration is starting. */
2542 ret = dlm_send_one_lockres(dlm, res, mres, target,
2543 DLM_MRES_MIGRATION);
2546 mlog(0, "migration to node %u failed with %d\n",
2548 /* migration failed, detach and clean up mle */
2549 dlm_mle_detach_hb_events(dlm, mle);
2551 dlm_put_mle_inuse(mle);
2552 spin_lock(&res->spinlock);
2553 res->state &= ~DLM_LOCK_RES_MIGRATING;
2555 spin_unlock(&res->spinlock);
2559 /* at this point, the target sends a message to all nodes,
2560 * (using dlm_do_migrate_request). this node is skipped since
2561 * we had to put an mle in the list to begin the process. this
2562 * node now waits for target to do an assert master. this node
2563 * will be the last one notified, ensuring that the migration
2564 * is complete everywhere. if the target dies while this is
2565 * going on, some nodes could potentially see the target as the
2566 * master, so it is important that my recovery finds the migration
2567 * mle and sets the master to UNKNONWN. */
2570 /* wait for new node to assert master */
2572 ret = wait_event_interruptible_timeout(mle->wq,
2573 (atomic_read(&mle->woken) == 1),
2574 msecs_to_jiffies(5000));
2577 if (atomic_read(&mle->woken) == 1 ||
2578 res->owner == target)
2581 mlog(0, "%s:%.*s: timed out during migration\n",
2582 dlm->name, res->lockname.len, res->lockname.name);
2583 /* avoid hang during shutdown when migrating lockres
2584 * to a node which also goes down */
2585 if (dlm_is_node_dead(dlm, target)) {
2586 mlog(0, "%s:%.*s: expected migration "
2587 "target %u is no longer up, restarting\n",
2588 dlm->name, res->lockname.len,
2589 res->lockname.name, target);
2591 /* migration failed, detach and clean up mle */
2592 dlm_mle_detach_hb_events(dlm, mle);
2594 dlm_put_mle_inuse(mle);
2595 spin_lock(&res->spinlock);
2596 res->state &= ~DLM_LOCK_RES_MIGRATING;
2598 spin_unlock(&res->spinlock);
2602 mlog(0, "%s:%.*s: caught signal during migration\n",
2603 dlm->name, res->lockname.len, res->lockname.name);
2606 /* all done, set the owner, clear the flag */
2607 spin_lock(&res->spinlock);
2608 dlm_set_lockres_owner(dlm, res, target);
2609 res->state &= ~DLM_LOCK_RES_MIGRATING;
2610 dlm_remove_nonlocal_locks(dlm, res);
2611 spin_unlock(&res->spinlock);
2614 /* master is known, detach if not already detached */
2615 dlm_mle_detach_hb_events(dlm, mle);
2616 dlm_put_mle_inuse(mle);
2619 dlm_lockres_calc_usage(dlm, res);
2622 /* re-dirty the lockres if we failed */
2624 dlm_kick_thread(dlm, res);
2626 /* wake up waiters if the MIGRATING flag got set
2627 * but migration failed */
2633 free_page((unsigned long)mres);
2637 mlog(0, "returning %d\n", ret);
2641 #define DLM_MIGRATION_RETRY_MS 100
2643 /* Should be called only after beginning the domain leave process.
2644 * There should not be any remaining locks on nonlocal lock resources,
2645 * and there should be no local locks left on locally mastered resources.
2647 * Called with the dlm spinlock held, may drop it to do migration, but
2648 * will re-acquire before exit.
2650 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2651 int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2654 int lock_dropped = 0;
2656 if (res->owner != dlm->node_num) {
2657 if (!__dlm_lockres_unused(res)) {
2658 mlog(ML_ERROR, "%s:%.*s: this node is not master, "
2659 "trying to free this but locks remain\n",
2660 dlm->name, res->lockname.len, res->lockname.name);
2665 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2666 spin_unlock(&dlm->spinlock);
2669 ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
2672 if (ret == -ENOTEMPTY) {
2673 mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
2674 res->lockname.len, res->lockname.name);
2678 mlog(0, "lockres %.*s: migrate failed, "
2679 "retrying\n", res->lockname.len,
2680 res->lockname.name);
2681 msleep(DLM_MIGRATION_RETRY_MS);
2683 spin_lock(&dlm->spinlock);
2685 return lock_dropped;
2688 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2691 spin_lock(&dlm->ast_lock);
2692 spin_lock(&lock->spinlock);
2693 ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2694 spin_unlock(&lock->spinlock);
2695 spin_unlock(&dlm->ast_lock);
2699 static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2700 struct dlm_lock_resource *res,
2704 spin_lock(&res->spinlock);
2705 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2706 spin_unlock(&res->spinlock);
2708 /* target has died, so make the caller break out of the
2709 * wait_event, but caller must recheck the domain_map */
2710 spin_lock(&dlm->spinlock);
2711 if (!test_bit(mig_target, dlm->domain_map))
2713 spin_unlock(&dlm->spinlock);
2717 static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
2718 struct dlm_lock_resource *res)
2721 spin_lock(&res->spinlock);
2722 ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2723 spin_unlock(&res->spinlock);
2728 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2729 struct dlm_lock_resource *res,
2734 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2735 res->lockname.len, res->lockname.name, dlm->node_num,
2737 /* need to set MIGRATING flag on lockres. this is done by
2738 * ensuring that all asts have been flushed for this lockres. */
2739 spin_lock(&res->spinlock);
2740 BUG_ON(res->migration_pending);
2741 res->migration_pending = 1;
2742 /* strategy is to reserve an extra ast then release
2743 * it below, letting the release do all of the work */
2744 __dlm_lockres_reserve_ast(res);
2745 spin_unlock(&res->spinlock);
2747 /* now flush all the pending asts */
2748 dlm_kick_thread(dlm, res);
2749 /* before waiting on DIRTY, block processes which may
2750 * try to dirty the lockres before MIGRATING is set */
2751 spin_lock(&res->spinlock);
2752 BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
2753 res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
2754 spin_unlock(&res->spinlock);
2755 /* now wait on any pending asts and the DIRTY state */
2756 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2757 dlm_lockres_release_ast(dlm, res);
2759 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2760 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2761 /* if the extra ref we just put was the final one, this
2762 * will pass thru immediately. otherwise, we need to wait
2763 * for the last ast to finish. */
2765 ret = wait_event_interruptible_timeout(dlm->migration_wq,
2766 dlm_migration_can_proceed(dlm, res, target),
2767 msecs_to_jiffies(1000));
2769 mlog(0, "woken again: migrating? %s, dead? %s\n",
2770 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2771 test_bit(target, dlm->domain_map) ? "no":"yes");
2773 mlog(0, "all is well: migrating? %s, dead? %s\n",
2774 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2775 test_bit(target, dlm->domain_map) ? "no":"yes");
2777 if (!dlm_migration_can_proceed(dlm, res, target)) {
2778 mlog(0, "trying again...\n");
2781 /* now that we are sure the MIGRATING state is there, drop
2782 * the unneded state which blocked threads trying to DIRTY */
2783 spin_lock(&res->spinlock);
2784 BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
2785 BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
2786 res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
2787 spin_unlock(&res->spinlock);
2789 /* did the target go down or die? */
2790 spin_lock(&dlm->spinlock);
2791 if (!test_bit(target, dlm->domain_map)) {
2792 mlog(ML_ERROR, "aha. migration target %u just went down\n",
2796 spin_unlock(&dlm->spinlock);
2801 * o the DLM_LOCK_RES_MIGRATING flag is set
2802 * o there are no pending asts on this lockres
2803 * o all processes trying to reserve an ast on this
2804 * lockres must wait for the MIGRATING flag to clear
2809 /* last step in the migration process.
2810 * original master calls this to free all of the dlm_lock
2811 * structures that used to be for other nodes. */
2812 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2813 struct dlm_lock_resource *res)
2815 struct list_head *iter, *iter2;
2816 struct list_head *queue = &res->granted;
2818 struct dlm_lock *lock;
2820 assert_spin_locked(&res->spinlock);
2822 BUG_ON(res->owner == dlm->node_num);
2824 for (i=0; i<3; i++) {
2825 list_for_each_safe(iter, iter2, queue) {
2826 lock = list_entry (iter, struct dlm_lock, list);
2827 if (lock->ml.node != dlm->node_num) {
2828 mlog(0, "putting lock for node %u\n",
2830 /* be extra careful */
2831 BUG_ON(!list_empty(&lock->ast_list));
2832 BUG_ON(!list_empty(&lock->bast_list));
2833 BUG_ON(lock->ast_pending);
2834 BUG_ON(lock->bast_pending);
2835 dlm_lockres_clear_refmap_bit(lock->ml.node, res);
2836 list_del_init(&lock->list);
2844 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
2845 if (bit >= O2NM_MAX_NODES)
2847 /* do not clear the local node reference, if there is a
2848 * process holding this, let it drop the ref itself */
2849 if (bit != dlm->node_num) {
2850 mlog(0, "%s:%.*s: node %u had a ref to this "
2851 "migrating lockres, clearing\n", dlm->name,
2852 res->lockname.len, res->lockname.name, bit);
2853 dlm_lockres_clear_refmap_bit(bit, res);
2859 /* for now this is not too intelligent. we will
2860 * need stats to make this do the right thing.
2861 * this just finds the first lock on one of the
2862 * queues and uses that node as the target. */
2863 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
2864 struct dlm_lock_resource *res)
2867 struct list_head *queue = &res->granted;
2868 struct list_head *iter;
2869 struct dlm_lock *lock;
2872 assert_spin_locked(&dlm->spinlock);
2874 spin_lock(&res->spinlock);
2875 for (i=0; i<3; i++) {
2876 list_for_each(iter, queue) {
2877 /* up to the caller to make sure this node
2879 lock = list_entry (iter, struct dlm_lock, list);
2880 if (lock->ml.node != dlm->node_num) {
2881 spin_unlock(&res->spinlock);
2882 return lock->ml.node;
2887 spin_unlock(&res->spinlock);
2888 mlog(0, "have not found a suitable target yet! checking domain map\n");
2890 /* ok now we're getting desperate. pick anyone alive. */
2893 nodenum = find_next_bit(dlm->domain_map,
2894 O2NM_MAX_NODES, nodenum+1);
2895 mlog(0, "found %d in domain map\n", nodenum);
2896 if (nodenum >= O2NM_MAX_NODES)
2898 if (nodenum != dlm->node_num) {
2899 mlog(0, "picking %d\n", nodenum);
2904 mlog(0, "giving up. no master to migrate to\n");
2905 return DLM_LOCK_RES_OWNER_UNKNOWN;
2910 /* this is called by the new master once all lockres
2911 * data has been received */
2912 static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
2913 struct dlm_lock_resource *res,
2914 u8 master, u8 new_master,
2915 struct dlm_node_iter *iter)
2917 struct dlm_migrate_request migrate;
2918 int ret, status = 0;
2921 memset(&migrate, 0, sizeof(migrate));
2922 migrate.namelen = res->lockname.len;
2923 memcpy(migrate.name, res->lockname.name, migrate.namelen);
2924 migrate.new_master = new_master;
2925 migrate.master = master;
2929 /* send message to all nodes, except the master and myself */
2930 while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
2931 if (nodenum == master ||
2932 nodenum == new_master)
2935 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
2936 &migrate, sizeof(migrate), nodenum,
2940 else if (status < 0) {
2941 mlog(0, "migrate request (node %u) returned %d!\n",
2944 } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
2945 /* during the migration request we short-circuited
2946 * the mastery of the lockres. make sure we have
2947 * a mastery ref for nodenum */
2948 mlog(0, "%s:%.*s: need ref for node %u\n",
2949 dlm->name, res->lockname.len, res->lockname.name,
2951 spin_lock(&res->spinlock);
2952 dlm_lockres_set_refmap_bit(nodenum, res);
2953 spin_unlock(&res->spinlock);
2960 mlog(0, "returning ret=%d\n", ret);
2965 /* if there is an existing mle for this lockres, we now know who the master is.
2966 * (the one who sent us *this* message) we can clear it up right away.
2967 * since the process that put the mle on the list still has a reference to it,
2968 * we can unhash it now, set the master and wake the process. as a result,
2969 * we will have no mle in the list to start with. now we can add an mle for
2970 * the migration and this should be the only one found for those scanning the
2972 int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
2975 struct dlm_ctxt *dlm = data;
2976 struct dlm_lock_resource *res = NULL;
2977 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
2978 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
2980 unsigned int namelen, hash;
2986 name = migrate->name;
2987 namelen = migrate->namelen;
2988 hash = dlm_lockid_hash(name, namelen);
2990 /* preallocate.. if this fails, abort */
2991 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2999 /* check for pre-existing lock */
3000 spin_lock(&dlm->spinlock);
3001 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
3002 spin_lock(&dlm->master_lock);
3005 spin_lock(&res->spinlock);
3006 if (res->state & DLM_LOCK_RES_RECOVERING) {
3007 /* if all is working ok, this can only mean that we got
3008 * a migrate request from a node that we now see as
3009 * dead. what can we do here? drop it to the floor? */
3010 spin_unlock(&res->spinlock);
3011 mlog(ML_ERROR, "Got a migrate request, but the "
3012 "lockres is marked as recovering!");
3013 kmem_cache_free(dlm_mle_cache, mle);
3014 ret = -EINVAL; /* need a better solution */
3017 res->state |= DLM_LOCK_RES_MIGRATING;
3018 spin_unlock(&res->spinlock);
3021 /* ignore status. only nonzero status would BUG. */
3022 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
3024 migrate->new_master,
3028 spin_unlock(&dlm->master_lock);
3029 spin_unlock(&dlm->spinlock);
3032 /* master is known, detach if not already detached */
3033 dlm_mle_detach_hb_events(dlm, oldmle);
3034 dlm_put_mle(oldmle);
3038 dlm_lockres_put(res);
3044 /* must be holding dlm->spinlock and dlm->master_lock
3045 * when adding a migration mle, we can clear any other mles
3046 * in the master list because we know with certainty that
3047 * the master is "master". so we remove any old mle from
3048 * the list after setting it's master field, and then add
3049 * the new migration mle. this way we can hold with the rule
3050 * of having only one mle for a given lock name at all times. */
3051 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
3052 struct dlm_lock_resource *res,
3053 struct dlm_master_list_entry *mle,
3054 struct dlm_master_list_entry **oldmle,
3055 const char *name, unsigned int namelen,
3056 u8 new_master, u8 master)
3065 assert_spin_locked(&dlm->spinlock);
3066 assert_spin_locked(&dlm->master_lock);
3068 /* caller is responsible for any ref taken here on oldmle */
3069 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
3071 struct dlm_master_list_entry *tmp = *oldmle;
3072 spin_lock(&tmp->spinlock);
3073 if (tmp->type == DLM_MLE_MIGRATION) {
3074 if (master == dlm->node_num) {
3075 /* ah another process raced me to it */
3076 mlog(0, "tried to migrate %.*s, but some "
3077 "process beat me to it\n",
3081 /* bad. 2 NODES are trying to migrate! */
3082 mlog(ML_ERROR, "migration error mle: "
3083 "master=%u new_master=%u // request: "
3084 "master=%u new_master=%u // "
3086 tmp->master, tmp->new_master,
3092 /* this is essentially what assert_master does */
3093 tmp->master = master;
3094 atomic_set(&tmp->woken, 1);
3096 /* remove it from the list so that only one
3097 * mle will be found */
3098 list_del_init(&tmp->list);
3099 /* this was obviously WRONG. mle is uninited here. should be tmp. */
3100 __dlm_mle_detach_hb_events(dlm, tmp);
3101 ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
3102 mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3103 "telling master to get ref for cleared out mle "
3104 "during migration\n", dlm->name, namelen, name,
3105 master, new_master);
3107 spin_unlock(&tmp->spinlock);
3110 /* now add a migration mle to the tail of the list */
3111 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
3112 mle->new_master = new_master;
3113 /* the new master will be sending an assert master for this.
3114 * at that point we will get the refmap reference */
3115 mle->master = master;
3116 /* do this for consistency with other mle types */
3117 set_bit(new_master, mle->maybe_map);
3118 list_add(&mle->list, &dlm->master_list);
3124 void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
3126 struct list_head *iter, *iter2;
3127 struct dlm_master_list_entry *mle;
3128 struct dlm_lock_resource *res;
3131 mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
3133 assert_spin_locked(&dlm->spinlock);
3135 /* clean the master list */
3136 spin_lock(&dlm->master_lock);
3137 list_for_each_safe(iter, iter2, &dlm->master_list) {
3138 mle = list_entry(iter, struct dlm_master_list_entry, list);
3140 BUG_ON(mle->type != DLM_MLE_BLOCK &&
3141 mle->type != DLM_MLE_MASTER &&
3142 mle->type != DLM_MLE_MIGRATION);
3144 /* MASTER mles are initiated locally. the waiting
3145 * process will notice the node map change
3146 * shortly. let that happen as normal. */
3147 if (mle->type == DLM_MLE_MASTER)
3151 /* BLOCK mles are initiated by other nodes.
3152 * need to clean up if the dead node would have
3153 * been the master. */
3154 if (mle->type == DLM_MLE_BLOCK) {
3157 spin_lock(&mle->spinlock);
3158 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
3159 if (bit != dead_node) {
3160 mlog(0, "mle found, but dead node %u would "
3161 "not have been master\n", dead_node);
3162 spin_unlock(&mle->spinlock);
3164 /* must drop the refcount by one since the
3165 * assert_master will never arrive. this
3166 * may result in the mle being unlinked and
3167 * freed, but there may still be a process
3168 * waiting in the dlmlock path which is fine. */
3169 mlog(0, "node %u was expected master\n",
3171 atomic_set(&mle->woken, 1);
3172 spin_unlock(&mle->spinlock);
3174 /* do not need events any longer, so detach
3176 __dlm_mle_detach_hb_events(dlm, mle);
3182 /* everything else is a MIGRATION mle */
3184 /* the rule for MIGRATION mles is that the master
3185 * becomes UNKNOWN if *either* the original or
3186 * the new master dies. all UNKNOWN lockreses
3187 * are sent to whichever node becomes the recovery
3188 * master. the new master is responsible for
3189 * determining if there is still a master for
3190 * this lockres, or if he needs to take over
3191 * mastery. either way, this node should expect
3192 * another message to resolve this. */
3193 if (mle->master != dead_node &&
3194 mle->new_master != dead_node)
3197 /* if we have reached this point, this mle needs to
3198 * be removed from the list and freed. */
3200 /* remove from the list early. NOTE: unlinking
3201 * list_head while in list_for_each_safe */
3202 __dlm_mle_detach_hb_events(dlm, mle);
3203 spin_lock(&mle->spinlock);
3204 list_del_init(&mle->list);
3205 atomic_set(&mle->woken, 1);
3206 spin_unlock(&mle->spinlock);
3209 mlog(0, "%s: node %u died during migration from "
3210 "%u to %u!\n", dlm->name, dead_node,
3211 mle->master, mle->new_master);
3212 /* if there is a lockres associated with this
3213 * mle, find it and set its owner to UNKNOWN */
3214 hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
3215 res = __dlm_lookup_lockres(dlm, mle->u.name.name,
3216 mle->u.name.len, hash);
3218 /* unfortunately if we hit this rare case, our
3219 * lock ordering is messed. we need to drop
3220 * the master lock so that we can take the
3221 * lockres lock, meaning that we will have to
3222 * restart from the head of list. */
3223 spin_unlock(&dlm->master_lock);
3225 /* move lockres onto recovery list */
3226 spin_lock(&res->spinlock);
3227 dlm_set_lockres_owner(dlm, res,
3228 DLM_LOCK_RES_OWNER_UNKNOWN);
3229 dlm_move_lockres_to_recovery_list(dlm, res);
3230 spin_unlock(&res->spinlock);
3231 dlm_lockres_put(res);
3233 /* about to get rid of mle, detach from heartbeat */
3234 __dlm_mle_detach_hb_events(dlm, mle);
3237 spin_lock(&dlm->master_lock);
3239 spin_unlock(&dlm->master_lock);
3245 /* this may be the last reference */
3248 spin_unlock(&dlm->master_lock);
3252 int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
3255 struct dlm_node_iter iter;
3258 spin_lock(&dlm->spinlock);
3259 dlm_node_iter_init(dlm->domain_map, &iter);
3260 clear_bit(old_master, iter.node_map);
3261 clear_bit(dlm->node_num, iter.node_map);
3262 spin_unlock(&dlm->spinlock);
3264 /* ownership of the lockres is changing. account for the
3265 * mastery reference here since old_master will briefly have
3266 * a reference after the migration completes */
3267 spin_lock(&res->spinlock);
3268 dlm_lockres_set_refmap_bit(old_master, res);
3269 spin_unlock(&res->spinlock);
3271 mlog(0, "now time to do a migrate request to other nodes\n");
3272 ret = dlm_do_migrate_request(dlm, res, old_master,
3273 dlm->node_num, &iter);
3279 mlog(0, "doing assert master of %.*s to all except the original node\n",
3280 res->lockname.len, res->lockname.name);
3281 /* this call now finishes out the nodemap
3282 * even if one or more nodes die */
3283 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3284 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3286 /* no longer need to retry. all living nodes contacted. */
3291 memset(iter.node_map, 0, sizeof(iter.node_map));
3292 set_bit(old_master, iter.node_map);
3293 mlog(0, "doing assert master of %.*s back to %u\n",
3294 res->lockname.len, res->lockname.name, old_master);
3295 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3296 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3298 mlog(0, "assert master to original master failed "
3300 /* the only nonzero status here would be because of
3301 * a dead original node. we're done. */
3305 /* all done, set the owner, clear the flag */
3306 spin_lock(&res->spinlock);
3307 dlm_set_lockres_owner(dlm, res, dlm->node_num);
3308 res->state &= ~DLM_LOCK_RES_MIGRATING;
3309 spin_unlock(&res->spinlock);
3310 /* re-dirty it on the new master */
3311 dlm_kick_thread(dlm, res);
3318 * LOCKRES AST REFCOUNT
3319 * this is integral to migration
3322 /* for future intent to call an ast, reserve one ahead of time.
3323 * this should be called only after waiting on the lockres
3324 * with dlm_wait_on_lockres, and while still holding the
3325 * spinlock after the call. */
3326 void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
3328 assert_spin_locked(&res->spinlock);
3329 if (res->state & DLM_LOCK_RES_MIGRATING) {
3330 __dlm_print_one_lock_resource(res);
3332 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3334 atomic_inc(&res->asts_reserved);
3338 * used to drop the reserved ast, either because it went unused,
3339 * or because the ast/bast was actually called.
3341 * also, if there is a pending migration on this lockres,
3342 * and this was the last pending ast on the lockres,
3343 * atomically set the MIGRATING flag before we drop the lock.
3344 * this is how we ensure that migration can proceed with no
3345 * asts in progress. note that it is ok if the state of the
3346 * queues is such that a lock should be granted in the future
3347 * or that a bast should be fired, because the new master will
3348 * shuffle the lists on this lockres as soon as it is migrated.
3350 void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
3351 struct dlm_lock_resource *res)
3353 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
3356 if (!res->migration_pending) {
3357 spin_unlock(&res->spinlock);
3361 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3362 res->migration_pending = 0;
3363 res->state |= DLM_LOCK_RES_MIGRATING;
3364 spin_unlock(&res->spinlock);
3366 wake_up(&dlm->migration_wq);