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);
105 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
107 static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
108 struct dlm_master_list_entry *mle,
110 unsigned int namelen)
112 struct dlm_lock_resource *res;
117 if (mle->type == DLM_MLE_BLOCK ||
118 mle->type == DLM_MLE_MIGRATION) {
119 if (namelen != mle->u.name.len ||
120 memcmp(name, mle->u.name.name, namelen)!=0)
124 if (namelen != res->lockname.len ||
125 memcmp(res->lockname.name, name, namelen) != 0)
131 #define dlm_print_nodemap(m) _dlm_print_nodemap(m,#m)
132 static void _dlm_print_nodemap(unsigned long *map, const char *mapname)
135 printk("%s=[ ", mapname);
136 for (i=0; i<O2NM_MAX_NODES; i++)
137 if (test_bit(i, map))
142 static void dlm_print_one_mle(struct dlm_master_list_entry *mle)
148 unsigned int namelen;
151 unsigned long *maybe = mle->maybe_map,
152 *vote = mle->vote_map,
153 *resp = mle->response_map,
154 *node = mle->node_map;
157 if (mle->type == DLM_MLE_BLOCK)
159 else if (mle->type == DLM_MLE_MASTER)
163 refs = atomic_read(&k->refcount);
164 master = mle->master;
165 attached = (list_empty(&mle->hb_events) ? 'N' : 'Y');
167 if (mle->type != DLM_MLE_MASTER) {
168 namelen = mle->u.name.len;
169 name = mle->u.name.name;
171 namelen = mle->u.res->lockname.len;
172 name = mle->u.res->lockname.name;
175 mlog(ML_NOTICE, "%.*s: %3s refs=%3d mas=%3u new=%3u evt=%c inuse=%d ",
176 namelen, name, type, refs, master, mle->new_master, attached,
178 dlm_print_nodemap(maybe);
180 dlm_print_nodemap(vote);
182 dlm_print_nodemap(resp);
184 dlm_print_nodemap(node);
190 /* Code here is included but defined out as it aids debugging */
192 static void dlm_dump_mles(struct dlm_ctxt *dlm)
194 struct dlm_master_list_entry *mle;
195 struct list_head *iter;
197 mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
198 spin_lock(&dlm->master_lock);
199 list_for_each(iter, &dlm->master_list) {
200 mle = list_entry(iter, struct dlm_master_list_entry, list);
201 dlm_print_one_mle(mle);
203 spin_unlock(&dlm->master_lock);
206 int dlm_dump_all_mles(const char __user *data, unsigned int len)
208 struct list_head *iter;
209 struct dlm_ctxt *dlm;
211 spin_lock(&dlm_domain_lock);
212 list_for_each(iter, &dlm_domains) {
213 dlm = list_entry (iter, struct dlm_ctxt, list);
214 mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
217 spin_unlock(&dlm_domain_lock);
220 EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
225 static struct kmem_cache *dlm_mle_cache = NULL;
228 static void dlm_mle_release(struct kref *kref);
229 static void dlm_init_mle(struct dlm_master_list_entry *mle,
230 enum dlm_mle_type type,
231 struct dlm_ctxt *dlm,
232 struct dlm_lock_resource *res,
234 unsigned int namelen);
235 static void dlm_put_mle(struct dlm_master_list_entry *mle);
236 static void __dlm_put_mle(struct dlm_master_list_entry *mle);
237 static int dlm_find_mle(struct dlm_ctxt *dlm,
238 struct dlm_master_list_entry **mle,
239 char *name, unsigned int namelen);
241 static int dlm_do_master_request(struct dlm_lock_resource *res,
242 struct dlm_master_list_entry *mle, int to);
245 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
246 struct dlm_lock_resource *res,
247 struct dlm_master_list_entry *mle,
249 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
250 struct dlm_lock_resource *res,
251 struct dlm_master_list_entry *mle,
253 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
254 struct dlm_lock_resource *res,
255 struct dlm_master_list_entry *mle,
256 struct dlm_master_list_entry **oldmle,
257 const char *name, unsigned int namelen,
258 u8 new_master, u8 master);
260 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
261 struct dlm_lock_resource *res);
262 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
263 struct dlm_lock_resource *res);
264 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
265 struct dlm_lock_resource *res,
267 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
268 struct dlm_lock_resource *res);
271 int dlm_is_host_down(int errno)
288 case -EINVAL: /* if returned from our tcp code,
289 this means there is no socket */
297 * MASTER LIST FUNCTIONS
302 * regarding master list entries and heartbeat callbacks:
304 * in order to avoid sleeping and allocation that occurs in
305 * heartbeat, master list entries are simply attached to the
306 * dlm's established heartbeat callbacks. the mle is attached
307 * when it is created, and since the dlm->spinlock is held at
308 * that time, any heartbeat event will be properly discovered
309 * by the mle. the mle needs to be detached from the
310 * dlm->mle_hb_events list as soon as heartbeat events are no
311 * longer useful to the mle, and before the mle is freed.
313 * as a general rule, heartbeat events are no longer needed by
314 * the mle once an "answer" regarding the lock master has been
317 static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
318 struct dlm_master_list_entry *mle)
320 assert_spin_locked(&dlm->spinlock);
322 list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
326 static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
327 struct dlm_master_list_entry *mle)
329 if (!list_empty(&mle->hb_events))
330 list_del_init(&mle->hb_events);
334 static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
335 struct dlm_master_list_entry *mle)
337 spin_lock(&dlm->spinlock);
338 __dlm_mle_detach_hb_events(dlm, mle);
339 spin_unlock(&dlm->spinlock);
342 static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
344 struct dlm_ctxt *dlm;
347 assert_spin_locked(&dlm->spinlock);
348 assert_spin_locked(&dlm->master_lock);
350 kref_get(&mle->mle_refs);
353 static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
355 struct dlm_ctxt *dlm;
358 spin_lock(&dlm->spinlock);
359 spin_lock(&dlm->master_lock);
362 spin_unlock(&dlm->master_lock);
363 spin_unlock(&dlm->spinlock);
367 /* remove from list and free */
368 static void __dlm_put_mle(struct dlm_master_list_entry *mle)
370 struct dlm_ctxt *dlm;
373 assert_spin_locked(&dlm->spinlock);
374 assert_spin_locked(&dlm->master_lock);
375 if (!atomic_read(&mle->mle_refs.refcount)) {
376 /* this may or may not crash, but who cares.
378 mlog(ML_ERROR, "bad mle: %p\n", mle);
379 dlm_print_one_mle(mle);
382 kref_put(&mle->mle_refs, dlm_mle_release);
386 /* must not have any spinlocks coming in */
387 static void dlm_put_mle(struct dlm_master_list_entry *mle)
389 struct dlm_ctxt *dlm;
392 spin_lock(&dlm->spinlock);
393 spin_lock(&dlm->master_lock);
395 spin_unlock(&dlm->master_lock);
396 spin_unlock(&dlm->spinlock);
399 static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
401 kref_get(&mle->mle_refs);
404 static void dlm_init_mle(struct dlm_master_list_entry *mle,
405 enum dlm_mle_type type,
406 struct dlm_ctxt *dlm,
407 struct dlm_lock_resource *res,
409 unsigned int namelen)
411 assert_spin_locked(&dlm->spinlock);
415 INIT_LIST_HEAD(&mle->list);
416 INIT_LIST_HEAD(&mle->hb_events);
417 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
418 spin_lock_init(&mle->spinlock);
419 init_waitqueue_head(&mle->wq);
420 atomic_set(&mle->woken, 0);
421 kref_init(&mle->mle_refs);
422 memset(mle->response_map, 0, sizeof(mle->response_map));
423 mle->master = O2NM_MAX_NODES;
424 mle->new_master = O2NM_MAX_NODES;
427 if (mle->type == DLM_MLE_MASTER) {
430 } else if (mle->type == DLM_MLE_BLOCK) {
432 memcpy(mle->u.name.name, name, namelen);
433 mle->u.name.len = namelen;
434 } else /* DLM_MLE_MIGRATION */ {
436 memcpy(mle->u.name.name, name, namelen);
437 mle->u.name.len = namelen;
440 /* copy off the node_map and register hb callbacks on our copy */
441 memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
442 memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
443 clear_bit(dlm->node_num, mle->vote_map);
444 clear_bit(dlm->node_num, mle->node_map);
446 /* attach the mle to the domain node up/down events */
447 __dlm_mle_attach_hb_events(dlm, mle);
451 /* returns 1 if found, 0 if not */
452 static int dlm_find_mle(struct dlm_ctxt *dlm,
453 struct dlm_master_list_entry **mle,
454 char *name, unsigned int namelen)
456 struct dlm_master_list_entry *tmpmle;
457 struct list_head *iter;
459 assert_spin_locked(&dlm->master_lock);
461 list_for_each(iter, &dlm->master_list) {
462 tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
463 if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
472 void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
474 struct dlm_master_list_entry *mle;
475 struct list_head *iter;
477 assert_spin_locked(&dlm->spinlock);
479 list_for_each(iter, &dlm->mle_hb_events) {
480 mle = list_entry(iter, struct dlm_master_list_entry,
483 dlm_mle_node_up(dlm, mle, NULL, idx);
485 dlm_mle_node_down(dlm, mle, NULL, idx);
489 static void dlm_mle_node_down(struct dlm_ctxt *dlm,
490 struct dlm_master_list_entry *mle,
491 struct o2nm_node *node, int idx)
493 spin_lock(&mle->spinlock);
495 if (!test_bit(idx, mle->node_map))
496 mlog(0, "node %u already removed from nodemap!\n", idx);
498 clear_bit(idx, mle->node_map);
500 spin_unlock(&mle->spinlock);
503 static void dlm_mle_node_up(struct dlm_ctxt *dlm,
504 struct dlm_master_list_entry *mle,
505 struct o2nm_node *node, int idx)
507 spin_lock(&mle->spinlock);
509 if (test_bit(idx, mle->node_map))
510 mlog(0, "node %u already in node map!\n", idx);
512 set_bit(idx, mle->node_map);
514 spin_unlock(&mle->spinlock);
518 int dlm_init_mle_cache(void)
520 dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
521 sizeof(struct dlm_master_list_entry),
522 0, SLAB_HWCACHE_ALIGN,
524 if (dlm_mle_cache == NULL)
529 void dlm_destroy_mle_cache(void)
532 kmem_cache_destroy(dlm_mle_cache);
535 static void dlm_mle_release(struct kref *kref)
537 struct dlm_master_list_entry *mle;
538 struct dlm_ctxt *dlm;
542 mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
545 if (mle->type != DLM_MLE_MASTER) {
546 mlog(0, "calling mle_release for %.*s, type %d\n",
547 mle->u.name.len, mle->u.name.name, mle->type);
549 mlog(0, "calling mle_release for %.*s, type %d\n",
550 mle->u.res->lockname.len,
551 mle->u.res->lockname.name, mle->type);
553 assert_spin_locked(&dlm->spinlock);
554 assert_spin_locked(&dlm->master_lock);
556 /* remove from list if not already */
557 if (!list_empty(&mle->list))
558 list_del_init(&mle->list);
560 /* detach the mle from the domain node up/down events */
561 __dlm_mle_detach_hb_events(dlm, mle);
563 /* NOTE: kfree under spinlock here.
564 * if this is bad, we can move this to a freelist. */
565 kmem_cache_free(dlm_mle_cache, mle);
570 * LOCK RESOURCE FUNCTIONS
573 static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
574 struct dlm_lock_resource *res,
577 assert_spin_locked(&res->spinlock);
579 mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
581 if (owner == dlm->node_num)
582 atomic_inc(&dlm->local_resources);
583 else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
584 atomic_inc(&dlm->unknown_resources);
586 atomic_inc(&dlm->remote_resources);
591 void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
592 struct dlm_lock_resource *res, u8 owner)
594 assert_spin_locked(&res->spinlock);
596 if (owner == res->owner)
599 if (res->owner == dlm->node_num)
600 atomic_dec(&dlm->local_resources);
601 else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
602 atomic_dec(&dlm->unknown_resources);
604 atomic_dec(&dlm->remote_resources);
606 dlm_set_lockres_owner(dlm, res, owner);
610 static void dlm_lockres_release(struct kref *kref)
612 struct dlm_lock_resource *res;
614 res = container_of(kref, struct dlm_lock_resource, refs);
616 /* This should not happen -- all lockres' have a name
617 * associated with them at init time. */
618 BUG_ON(!res->lockname.name);
620 mlog(0, "destroying lockres %.*s\n", res->lockname.len,
623 if (!hlist_unhashed(&res->hash_node) ||
624 !list_empty(&res->granted) ||
625 !list_empty(&res->converting) ||
626 !list_empty(&res->blocked) ||
627 !list_empty(&res->dirty) ||
628 !list_empty(&res->recovering) ||
629 !list_empty(&res->purge)) {
631 "Going to BUG for resource %.*s."
632 " We're on a list! [%c%c%c%c%c%c%c]\n",
633 res->lockname.len, res->lockname.name,
634 !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
635 !list_empty(&res->granted) ? 'G' : ' ',
636 !list_empty(&res->converting) ? 'C' : ' ',
637 !list_empty(&res->blocked) ? 'B' : ' ',
638 !list_empty(&res->dirty) ? 'D' : ' ',
639 !list_empty(&res->recovering) ? 'R' : ' ',
640 !list_empty(&res->purge) ? 'P' : ' ');
642 dlm_print_one_lock_resource(res);
645 /* By the time we're ready to blow this guy away, we shouldn't
646 * be on any lists. */
647 BUG_ON(!hlist_unhashed(&res->hash_node));
648 BUG_ON(!list_empty(&res->granted));
649 BUG_ON(!list_empty(&res->converting));
650 BUG_ON(!list_empty(&res->blocked));
651 BUG_ON(!list_empty(&res->dirty));
652 BUG_ON(!list_empty(&res->recovering));
653 BUG_ON(!list_empty(&res->purge));
655 kfree(res->lockname.name);
660 void dlm_lockres_put(struct dlm_lock_resource *res)
662 kref_put(&res->refs, dlm_lockres_release);
665 static void dlm_init_lockres(struct dlm_ctxt *dlm,
666 struct dlm_lock_resource *res,
667 const char *name, unsigned int namelen)
671 /* If we memset here, we lose our reference to the kmalloc'd
672 * res->lockname.name, so be sure to init every field
675 qname = (char *) res->lockname.name;
676 memcpy(qname, name, namelen);
678 res->lockname.len = namelen;
679 res->lockname.hash = dlm_lockid_hash(name, namelen);
681 init_waitqueue_head(&res->wq);
682 spin_lock_init(&res->spinlock);
683 INIT_HLIST_NODE(&res->hash_node);
684 INIT_LIST_HEAD(&res->granted);
685 INIT_LIST_HEAD(&res->converting);
686 INIT_LIST_HEAD(&res->blocked);
687 INIT_LIST_HEAD(&res->dirty);
688 INIT_LIST_HEAD(&res->recovering);
689 INIT_LIST_HEAD(&res->purge);
690 atomic_set(&res->asts_reserved, 0);
691 res->migration_pending = 0;
692 res->inflight_locks = 0;
694 kref_init(&res->refs);
696 /* just for consistency */
697 spin_lock(&res->spinlock);
698 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
699 spin_unlock(&res->spinlock);
701 res->state = DLM_LOCK_RES_IN_PROGRESS;
705 memset(res->lvb, 0, DLM_LVB_LEN);
706 memset(res->refmap, 0, sizeof(res->refmap));
709 struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
711 unsigned int namelen)
713 struct dlm_lock_resource *res;
715 res = kmalloc(sizeof(struct dlm_lock_resource), GFP_NOFS);
719 res->lockname.name = kmalloc(namelen, GFP_NOFS);
720 if (!res->lockname.name) {
725 dlm_init_lockres(dlm, res, name, namelen);
729 void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
730 struct dlm_lock_resource *res,
736 assert_spin_locked(&res->spinlock);
738 if (!test_bit(dlm->node_num, res->refmap)) {
739 BUG_ON(res->inflight_locks != 0);
740 dlm_lockres_set_refmap_bit(dlm->node_num, res);
742 res->inflight_locks++;
743 mlog(0, "%s:%.*s: inflight++: now %u\n",
744 dlm->name, res->lockname.len, res->lockname.name,
745 res->inflight_locks);
748 void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
749 struct dlm_lock_resource *res,
753 assert_spin_locked(&res->spinlock);
755 BUG_ON(res->inflight_locks == 0);
756 res->inflight_locks--;
757 mlog(0, "%s:%.*s: inflight--: now %u\n",
758 dlm->name, res->lockname.len, res->lockname.name,
759 res->inflight_locks);
760 if (res->inflight_locks == 0)
761 dlm_lockres_clear_refmap_bit(dlm->node_num, res);
766 * lookup a lock resource by name.
767 * may already exist in the hashtable.
768 * lockid is null terminated
770 * if not, allocate enough for the lockres and for
771 * the temporary structure used in doing the mastering.
773 * also, do a lookup in the dlm->master_list to see
774 * if another node has begun mastering the same lock.
775 * if so, there should be a block entry in there
776 * for this name, and we should *not* attempt to master
777 * the lock here. need to wait around for that node
778 * to assert_master (or die).
781 struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
786 struct dlm_lock_resource *tmpres=NULL, *res=NULL;
787 struct dlm_master_list_entry *mle = NULL;
788 struct dlm_master_list_entry *alloc_mle = NULL;
791 struct dlm_node_iter iter;
794 int bit, wait_on_recovery = 0;
795 int drop_inflight_if_nonlocal = 0;
799 hash = dlm_lockid_hash(lockid, namelen);
801 mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
804 spin_lock(&dlm->spinlock);
805 tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
807 int dropping_ref = 0;
809 spin_lock(&tmpres->spinlock);
810 if (tmpres->owner == dlm->node_num) {
811 BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
812 dlm_lockres_grab_inflight_ref(dlm, tmpres);
813 } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
815 spin_unlock(&tmpres->spinlock);
816 spin_unlock(&dlm->spinlock);
818 /* wait until done messaging the master, drop our ref to allow
819 * the lockres to be purged, start over. */
821 spin_lock(&tmpres->spinlock);
822 __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
823 spin_unlock(&tmpres->spinlock);
824 dlm_lockres_put(tmpres);
829 mlog(0, "found in hash!\n");
831 dlm_lockres_put(res);
837 spin_unlock(&dlm->spinlock);
838 mlog(0, "allocating a new resource\n");
839 /* nothing found and we need to allocate one. */
840 alloc_mle = (struct dlm_master_list_entry *)
841 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
844 res = dlm_new_lockres(dlm, lockid, namelen);
850 mlog(0, "no lockres found, allocated our own: %p\n", res);
852 if (flags & LKM_LOCAL) {
853 /* caller knows it's safe to assume it's not mastered elsewhere
854 * DONE! return right away */
855 spin_lock(&res->spinlock);
856 dlm_change_lockres_owner(dlm, res, dlm->node_num);
857 __dlm_insert_lockres(dlm, res);
858 dlm_lockres_grab_inflight_ref(dlm, res);
859 spin_unlock(&res->spinlock);
860 spin_unlock(&dlm->spinlock);
861 /* lockres still marked IN_PROGRESS */
865 /* check master list to see if another node has started mastering it */
866 spin_lock(&dlm->master_lock);
868 /* if we found a block, wait for lock to be mastered by another node */
869 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
872 if (mle->type == DLM_MLE_MASTER) {
873 mlog(ML_ERROR, "master entry for nonexistent lock!\n");
876 mig = (mle->type == DLM_MLE_MIGRATION);
877 /* if there is a migration in progress, let the migration
878 * finish before continuing. we can wait for the absence
879 * of the MIGRATION mle: either the migrate finished or
880 * one of the nodes died and the mle was cleaned up.
881 * if there is a BLOCK here, but it already has a master
882 * set, we are too late. the master does not have a ref
883 * for us in the refmap. detach the mle and drop it.
884 * either way, go back to the top and start over. */
885 if (mig || mle->master != O2NM_MAX_NODES) {
886 BUG_ON(mig && mle->master == dlm->node_num);
887 /* we arrived too late. the master does not
888 * have a ref for us. retry. */
889 mlog(0, "%s:%.*s: late on %s\n",
890 dlm->name, namelen, lockid,
891 mig ? "MIGRATION" : "BLOCK");
892 spin_unlock(&dlm->master_lock);
893 spin_unlock(&dlm->spinlock);
895 /* master is known, detach */
897 dlm_mle_detach_hb_events(dlm, mle);
900 /* this is lame, but we cant wait on either
901 * the mle or lockres waitqueue here */
907 /* go ahead and try to master lock on this node */
909 /* make sure this does not get freed below */
911 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
912 set_bit(dlm->node_num, mle->maybe_map);
913 list_add(&mle->list, &dlm->master_list);
915 /* still holding the dlm spinlock, check the recovery map
916 * to see if there are any nodes that still need to be
917 * considered. these will not appear in the mle nodemap
918 * but they might own this lockres. wait on them. */
919 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
920 if (bit < O2NM_MAX_NODES) {
921 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
922 "recover before lock mastery can begin\n",
923 dlm->name, namelen, (char *)lockid, bit);
924 wait_on_recovery = 1;
928 /* at this point there is either a DLM_MLE_BLOCK or a
929 * DLM_MLE_MASTER on the master list, so it's safe to add the
930 * lockres to the hashtable. anyone who finds the lock will
931 * still have to wait on the IN_PROGRESS. */
933 /* finally add the lockres to its hash bucket */
934 __dlm_insert_lockres(dlm, res);
935 /* since this lockres is new it doesnt not require the spinlock */
936 dlm_lockres_grab_inflight_ref_new(dlm, res);
938 /* if this node does not become the master make sure to drop
939 * this inflight reference below */
940 drop_inflight_if_nonlocal = 1;
942 /* get an extra ref on the mle in case this is a BLOCK
943 * if so, the creator of the BLOCK may try to put the last
944 * ref at this time in the assert master handler, so we
945 * need an extra one to keep from a bad ptr deref. */
946 dlm_get_mle_inuse(mle);
947 spin_unlock(&dlm->master_lock);
948 spin_unlock(&dlm->spinlock);
951 while (wait_on_recovery) {
952 /* any cluster changes that occurred after dropping the
953 * dlm spinlock would be detectable be a change on the mle,
954 * so we only need to clear out the recovery map once. */
955 if (dlm_is_recovery_lock(lockid, namelen)) {
956 mlog(ML_NOTICE, "%s: recovery map is not empty, but "
957 "must master $RECOVERY lock now\n", dlm->name);
958 if (!dlm_pre_master_reco_lockres(dlm, res))
959 wait_on_recovery = 0;
961 mlog(0, "%s: waiting 500ms for heartbeat state "
962 "change\n", dlm->name);
968 dlm_kick_recovery_thread(dlm);
970 dlm_wait_for_recovery(dlm);
972 spin_lock(&dlm->spinlock);
973 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
974 if (bit < O2NM_MAX_NODES) {
975 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
976 "recover before lock mastery can begin\n",
977 dlm->name, namelen, (char *)lockid, bit);
978 wait_on_recovery = 1;
980 wait_on_recovery = 0;
981 spin_unlock(&dlm->spinlock);
983 if (wait_on_recovery)
984 dlm_wait_for_node_recovery(dlm, bit, 10000);
987 /* must wait for lock to be mastered elsewhere */
992 dlm_node_iter_init(mle->vote_map, &iter);
993 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
994 ret = dlm_do_master_request(res, mle, nodenum);
997 if (mle->master != O2NM_MAX_NODES) {
998 /* found a master ! */
999 if (mle->master <= nodenum)
1001 /* if our master request has not reached the master
1002 * yet, keep going until it does. this is how the
1003 * master will know that asserts are needed back to
1004 * the lower nodes. */
1005 mlog(0, "%s:%.*s: requests only up to %u but master "
1006 "is %u, keep going\n", dlm->name, namelen,
1007 lockid, nodenum, mle->master);
1012 /* keep going until the response map includes all nodes */
1013 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
1015 wait_on_recovery = 1;
1016 mlog(0, "%s:%.*s: node map changed, redo the "
1017 "master request now, blocked=%d\n",
1018 dlm->name, res->lockname.len,
1019 res->lockname.name, blocked);
1021 mlog(ML_ERROR, "%s:%.*s: spinning on "
1022 "dlm_wait_for_lock_mastery, blocked=%d\n",
1023 dlm->name, res->lockname.len,
1024 res->lockname.name, blocked);
1025 dlm_print_one_lock_resource(res);
1026 dlm_print_one_mle(mle);
1032 mlog(0, "lockres mastered by %u\n", res->owner);
1033 /* make sure we never continue without this */
1034 BUG_ON(res->owner == O2NM_MAX_NODES);
1036 /* master is known, detach if not already detached */
1037 dlm_mle_detach_hb_events(dlm, mle);
1039 /* put the extra ref */
1040 dlm_put_mle_inuse(mle);
1043 spin_lock(&res->spinlock);
1044 if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
1045 dlm_lockres_drop_inflight_ref(dlm, res);
1046 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
1047 spin_unlock(&res->spinlock);
1051 /* need to free the unused mle */
1053 kmem_cache_free(dlm_mle_cache, alloc_mle);
1059 #define DLM_MASTERY_TIMEOUT_MS 5000
1061 static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
1062 struct dlm_lock_resource *res,
1063 struct dlm_master_list_entry *mle,
1068 int map_changed, voting_done;
1075 /* check if another node has already become the owner */
1076 spin_lock(&res->spinlock);
1077 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1078 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
1079 res->lockname.len, res->lockname.name, res->owner);
1080 spin_unlock(&res->spinlock);
1081 /* this will cause the master to re-assert across
1082 * the whole cluster, freeing up mles */
1083 if (res->owner != dlm->node_num) {
1084 ret = dlm_do_master_request(res, mle, res->owner);
1086 /* give recovery a chance to run */
1087 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
1095 spin_unlock(&res->spinlock);
1097 spin_lock(&mle->spinlock);
1099 map_changed = (memcmp(mle->vote_map, mle->node_map,
1100 sizeof(mle->vote_map)) != 0);
1101 voting_done = (memcmp(mle->vote_map, mle->response_map,
1102 sizeof(mle->vote_map)) == 0);
1104 /* restart if we hit any errors */
1107 mlog(0, "%s: %.*s: node map changed, restarting\n",
1108 dlm->name, res->lockname.len, res->lockname.name);
1109 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
1110 b = (mle->type == DLM_MLE_BLOCK);
1111 if ((*blocked && !b) || (!*blocked && b)) {
1112 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1113 dlm->name, res->lockname.len, res->lockname.name,
1117 spin_unlock(&mle->spinlock);
1122 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1123 "rechecking now\n", dlm->name, res->lockname.len,
1124 res->lockname.name);
1128 mlog(0, "map not changed and voting not done "
1129 "for %s:%.*s\n", dlm->name, res->lockname.len,
1130 res->lockname.name);
1134 if (m != O2NM_MAX_NODES) {
1135 /* another node has done an assert!
1140 /* have all nodes responded? */
1141 if (voting_done && !*blocked) {
1142 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
1143 if (dlm->node_num <= bit) {
1144 /* my node number is lowest.
1145 * now tell other nodes that I am
1146 * mastering this. */
1147 mle->master = dlm->node_num;
1148 /* ref was grabbed in get_lock_resource
1149 * will be dropped in dlmlock_master */
1153 /* if voting is done, but we have not received
1154 * an assert master yet, we must sleep */
1158 spin_unlock(&mle->spinlock);
1160 /* sleep if we haven't finished voting yet */
1162 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1165 if (atomic_read(&mle->mle_refs.refcount) < 2)
1166 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1167 atomic_read(&mle->mle_refs.refcount),
1168 res->lockname.len, res->lockname.name);
1170 atomic_set(&mle->woken, 0);
1171 (void)wait_event_timeout(mle->wq,
1172 (atomic_read(&mle->woken) == 1),
1174 if (res->owner == O2NM_MAX_NODES) {
1175 mlog(0, "%s:%.*s: waiting again\n", dlm->name,
1176 res->lockname.len, res->lockname.name);
1179 mlog(0, "done waiting, master is %u\n", res->owner);
1187 mlog(0, "about to master %.*s here, this=%u\n",
1188 res->lockname.len, res->lockname.name, m);
1189 ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
1191 /* This is a failure in the network path,
1192 * not in the response to the assert_master
1193 * (any nonzero response is a BUG on this node).
1194 * Most likely a socket just got disconnected
1195 * due to node death. */
1198 /* no longer need to restart lock mastery.
1199 * all living nodes have been contacted. */
1203 /* set the lockres owner */
1204 spin_lock(&res->spinlock);
1205 /* mastery reference obtained either during
1206 * assert_master_handler or in get_lock_resource */
1207 dlm_change_lockres_owner(dlm, res, m);
1208 spin_unlock(&res->spinlock);
1214 struct dlm_bitmap_diff_iter
1217 unsigned long *orig_bm;
1218 unsigned long *cur_bm;
1219 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1222 enum dlm_node_state_change
1229 static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1230 unsigned long *orig_bm,
1231 unsigned long *cur_bm)
1233 unsigned long p1, p2;
1237 iter->orig_bm = orig_bm;
1238 iter->cur_bm = cur_bm;
1240 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1241 p1 = *(iter->orig_bm + i);
1242 p2 = *(iter->cur_bm + i);
1243 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1247 static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1248 enum dlm_node_state_change *state)
1252 if (iter->curnode >= O2NM_MAX_NODES)
1255 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1257 if (bit >= O2NM_MAX_NODES) {
1258 iter->curnode = O2NM_MAX_NODES;
1262 /* if it was there in the original then this node died */
1263 if (test_bit(bit, iter->orig_bm))
1268 iter->curnode = bit;
1273 static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1274 struct dlm_lock_resource *res,
1275 struct dlm_master_list_entry *mle,
1278 struct dlm_bitmap_diff_iter bdi;
1279 enum dlm_node_state_change sc;
1283 mlog(0, "something happened such that the "
1284 "master process may need to be restarted!\n");
1286 assert_spin_locked(&mle->spinlock);
1288 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1289 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1291 if (sc == NODE_UP) {
1292 /* a node came up. clear any old vote from
1293 * the response map and set it in the vote map
1294 * then restart the mastery. */
1295 mlog(ML_NOTICE, "node %d up while restarting\n", node);
1297 /* redo the master request, but only for the new node */
1298 mlog(0, "sending request to new node\n");
1299 clear_bit(node, mle->response_map);
1300 set_bit(node, mle->vote_map);
1302 mlog(ML_ERROR, "node down! %d\n", node);
1304 int lowest = find_next_bit(mle->maybe_map,
1307 /* act like it was never there */
1308 clear_bit(node, mle->maybe_map);
1310 if (node == lowest) {
1311 mlog(0, "expected master %u died"
1312 " while this node was blocked "
1313 "waiting on it!\n", node);
1314 lowest = find_next_bit(mle->maybe_map,
1317 if (lowest < O2NM_MAX_NODES) {
1318 mlog(0, "%s:%.*s:still "
1319 "blocked. waiting on %u "
1325 /* mle is an MLE_BLOCK, but
1326 * there is now nothing left to
1327 * block on. we need to return
1328 * all the way back out and try
1329 * again with an MLE_MASTER.
1330 * dlm_do_local_recovery_cleanup
1331 * has already run, so the mle
1333 mlog(0, "%s:%.*s: no "
1334 "longer blocking. try to "
1335 "master this here\n",
1338 res->lockname.name);
1339 mle->type = DLM_MLE_MASTER;
1345 /* now blank out everything, as if we had never
1346 * contacted anyone */
1347 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
1348 memset(mle->response_map, 0, sizeof(mle->response_map));
1349 /* reset the vote_map to the current node_map */
1350 memcpy(mle->vote_map, mle->node_map,
1351 sizeof(mle->node_map));
1352 /* put myself into the maybe map */
1353 if (mle->type != DLM_MLE_BLOCK)
1354 set_bit(dlm->node_num, mle->maybe_map);
1357 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1364 * DLM_MASTER_REQUEST_MSG
1366 * returns: 0 on success,
1367 * -errno on a network error
1369 * on error, the caller should assume the target node is "dead"
1373 static int dlm_do_master_request(struct dlm_lock_resource *res,
1374 struct dlm_master_list_entry *mle, int to)
1376 struct dlm_ctxt *dlm = mle->dlm;
1377 struct dlm_master_request request;
1378 int ret, response=0, resend;
1380 memset(&request, 0, sizeof(request));
1381 request.node_idx = dlm->node_num;
1383 BUG_ON(mle->type == DLM_MLE_MIGRATION);
1385 if (mle->type != DLM_MLE_MASTER) {
1386 request.namelen = mle->u.name.len;
1387 memcpy(request.name, mle->u.name.name, request.namelen);
1389 request.namelen = mle->u.res->lockname.len;
1390 memcpy(request.name, mle->u.res->lockname.name,
1395 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1396 sizeof(request), to, &response);
1398 if (ret == -ESRCH) {
1399 /* should never happen */
1400 mlog(ML_ERROR, "TCP stack not ready!\n");
1402 } else if (ret == -EINVAL) {
1403 mlog(ML_ERROR, "bad args passed to o2net!\n");
1405 } else if (ret == -ENOMEM) {
1406 mlog(ML_ERROR, "out of memory while trying to send "
1407 "network message! retrying\n");
1408 /* this is totally crude */
1411 } else if (!dlm_is_host_down(ret)) {
1412 /* not a network error. bad. */
1414 mlog(ML_ERROR, "unhandled error!");
1417 /* all other errors should be network errors,
1418 * and likely indicate node death */
1419 mlog(ML_ERROR, "link to %d went down!\n", to);
1425 spin_lock(&mle->spinlock);
1427 case DLM_MASTER_RESP_YES:
1428 set_bit(to, mle->response_map);
1429 mlog(0, "node %u is the master, response=YES\n", to);
1430 mlog(0, "%s:%.*s: master node %u now knows I have a "
1431 "reference\n", dlm->name, res->lockname.len,
1432 res->lockname.name, to);
1435 case DLM_MASTER_RESP_NO:
1436 mlog(0, "node %u not master, response=NO\n", to);
1437 set_bit(to, mle->response_map);
1439 case DLM_MASTER_RESP_MAYBE:
1440 mlog(0, "node %u not master, response=MAYBE\n", to);
1441 set_bit(to, mle->response_map);
1442 set_bit(to, mle->maybe_map);
1444 case DLM_MASTER_RESP_ERROR:
1445 mlog(0, "node %u hit an error, resending\n", to);
1450 mlog(ML_ERROR, "bad response! %u\n", response);
1453 spin_unlock(&mle->spinlock);
1455 /* this is also totally crude */
1465 * locks that can be taken here:
1471 * if possible, TRIM THIS DOWN!!!
1473 int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
1476 u8 response = DLM_MASTER_RESP_MAYBE;
1477 struct dlm_ctxt *dlm = data;
1478 struct dlm_lock_resource *res = NULL;
1479 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1480 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1482 unsigned int namelen, hash;
1485 int dispatch_assert = 0;
1488 return DLM_MASTER_RESP_NO;
1490 if (!dlm_domain_fully_joined(dlm)) {
1491 response = DLM_MASTER_RESP_NO;
1495 name = request->name;
1496 namelen = request->namelen;
1497 hash = dlm_lockid_hash(name, namelen);
1499 if (namelen > DLM_LOCKID_NAME_MAX) {
1500 response = DLM_IVBUFLEN;
1505 spin_lock(&dlm->spinlock);
1506 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1508 spin_unlock(&dlm->spinlock);
1510 /* take care of the easy cases up front */
1511 spin_lock(&res->spinlock);
1512 if (res->state & (DLM_LOCK_RES_RECOVERING|
1513 DLM_LOCK_RES_MIGRATING)) {
1514 spin_unlock(&res->spinlock);
1515 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1516 "being recovered/migrated\n");
1517 response = DLM_MASTER_RESP_ERROR;
1519 kmem_cache_free(dlm_mle_cache, mle);
1523 if (res->owner == dlm->node_num) {
1524 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1525 dlm->name, namelen, name, request->node_idx);
1526 dlm_lockres_set_refmap_bit(request->node_idx, res);
1527 spin_unlock(&res->spinlock);
1528 response = DLM_MASTER_RESP_YES;
1530 kmem_cache_free(dlm_mle_cache, mle);
1532 /* this node is the owner.
1533 * there is some extra work that needs to
1534 * happen now. the requesting node has
1535 * caused all nodes up to this one to
1536 * create mles. this node now needs to
1537 * go back and clean those up. */
1538 dispatch_assert = 1;
1540 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1541 spin_unlock(&res->spinlock);
1542 // mlog(0, "node %u is the master\n", res->owner);
1543 response = DLM_MASTER_RESP_NO;
1545 kmem_cache_free(dlm_mle_cache, mle);
1549 /* ok, there is no owner. either this node is
1550 * being blocked, or it is actively trying to
1551 * master this lock. */
1552 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1553 mlog(ML_ERROR, "lock with no owner should be "
1558 // mlog(0, "lockres is in progress...\n");
1559 spin_lock(&dlm->master_lock);
1560 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1562 mlog(ML_ERROR, "no mle found for this lock!\n");
1566 spin_lock(&tmpmle->spinlock);
1567 if (tmpmle->type == DLM_MLE_BLOCK) {
1568 // mlog(0, "this node is waiting for "
1569 // "lockres to be mastered\n");
1570 response = DLM_MASTER_RESP_NO;
1571 } else if (tmpmle->type == DLM_MLE_MIGRATION) {
1572 mlog(0, "node %u is master, but trying to migrate to "
1573 "node %u.\n", tmpmle->master, tmpmle->new_master);
1574 if (tmpmle->master == dlm->node_num) {
1575 mlog(ML_ERROR, "no owner on lockres, but this "
1576 "node is trying to migrate it to %u?!\n",
1577 tmpmle->new_master);
1580 /* the real master can respond on its own */
1581 response = DLM_MASTER_RESP_NO;
1583 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1585 if (tmpmle->master == dlm->node_num) {
1586 response = DLM_MASTER_RESP_YES;
1587 /* this node will be the owner.
1588 * go back and clean the mles on any
1590 dispatch_assert = 1;
1591 dlm_lockres_set_refmap_bit(request->node_idx, res);
1592 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1593 dlm->name, namelen, name,
1596 response = DLM_MASTER_RESP_NO;
1598 // mlog(0, "this node is attempting to "
1599 // "master lockres\n");
1600 response = DLM_MASTER_RESP_MAYBE;
1603 set_bit(request->node_idx, tmpmle->maybe_map);
1604 spin_unlock(&tmpmle->spinlock);
1606 spin_unlock(&dlm->master_lock);
1607 spin_unlock(&res->spinlock);
1609 /* keep the mle attached to heartbeat events */
1610 dlm_put_mle(tmpmle);
1612 kmem_cache_free(dlm_mle_cache, mle);
1617 * lockres doesn't exist on this node
1618 * if there is an MLE_BLOCK, return NO
1619 * if there is an MLE_MASTER, return MAYBE
1620 * otherwise, add an MLE_BLOCK, return NO
1622 spin_lock(&dlm->master_lock);
1623 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1625 /* this lockid has never been seen on this node yet */
1626 // mlog(0, "no mle found\n");
1628 spin_unlock(&dlm->master_lock);
1629 spin_unlock(&dlm->spinlock);
1631 mle = (struct dlm_master_list_entry *)
1632 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
1634 response = DLM_MASTER_RESP_ERROR;
1635 mlog_errno(-ENOMEM);
1641 // mlog(0, "this is second time thru, already allocated, "
1642 // "add the block.\n");
1643 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
1644 set_bit(request->node_idx, mle->maybe_map);
1645 list_add(&mle->list, &dlm->master_list);
1646 response = DLM_MASTER_RESP_NO;
1648 // mlog(0, "mle was found\n");
1650 spin_lock(&tmpmle->spinlock);
1651 if (tmpmle->master == dlm->node_num) {
1652 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1655 if (tmpmle->type == DLM_MLE_BLOCK)
1656 response = DLM_MASTER_RESP_NO;
1657 else if (tmpmle->type == DLM_MLE_MIGRATION) {
1658 mlog(0, "migration mle was found (%u->%u)\n",
1659 tmpmle->master, tmpmle->new_master);
1660 /* real master can respond on its own */
1661 response = DLM_MASTER_RESP_NO;
1663 response = DLM_MASTER_RESP_MAYBE;
1665 set_bit(request->node_idx, tmpmle->maybe_map);
1666 spin_unlock(&tmpmle->spinlock);
1668 spin_unlock(&dlm->master_lock);
1669 spin_unlock(&dlm->spinlock);
1672 /* keep the mle attached to heartbeat events */
1673 dlm_put_mle(tmpmle);
1677 if (dispatch_assert) {
1678 if (response != DLM_MASTER_RESP_YES)
1679 mlog(ML_ERROR, "invalid response %d\n", response);
1681 mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1684 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1685 dlm->node_num, res->lockname.len, res->lockname.name);
1686 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1687 DLM_ASSERT_MASTER_MLE_CLEANUP);
1689 mlog(ML_ERROR, "failed to dispatch assert master work\n");
1690 response = DLM_MASTER_RESP_ERROR;
1699 * DLM_ASSERT_MASTER_MSG
1704 * NOTE: this can be used for debugging
1705 * can periodically run all locks owned by this node
1706 * and re-assert across the cluster...
1708 int dlm_do_assert_master(struct dlm_ctxt *dlm,
1709 struct dlm_lock_resource *res,
1710 void *nodemap, u32 flags)
1712 struct dlm_assert_master assert;
1714 struct dlm_node_iter iter;
1717 const char *lockname = res->lockname.name;
1718 unsigned int namelen = res->lockname.len;
1720 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
1722 spin_lock(&res->spinlock);
1723 res->state |= DLM_LOCK_RES_SETREF_INPROG;
1724 spin_unlock(&res->spinlock);
1729 /* note that if this nodemap is empty, it returns 0 */
1730 dlm_node_iter_init(nodemap, &iter);
1731 while ((to = dlm_node_iter_next(&iter)) >= 0) {
1733 struct dlm_master_list_entry *mle = NULL;
1735 mlog(0, "sending assert master to %d (%.*s)\n", to,
1737 memset(&assert, 0, sizeof(assert));
1738 assert.node_idx = dlm->node_num;
1739 assert.namelen = namelen;
1740 memcpy(assert.name, lockname, namelen);
1741 assert.flags = cpu_to_be32(flags);
1743 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1744 &assert, sizeof(assert), to, &r);
1746 mlog(0, "assert_master returned %d!\n", tmpret);
1747 if (!dlm_is_host_down(tmpret)) {
1748 mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
1751 /* a node died. finish out the rest of the nodes. */
1752 mlog(0, "link to %d went down!\n", to);
1753 /* any nonzero status return will do */
1757 /* ok, something horribly messed. kill thyself. */
1758 mlog(ML_ERROR,"during assert master of %.*s to %u, "
1759 "got %d.\n", namelen, lockname, to, r);
1760 spin_lock(&dlm->spinlock);
1761 spin_lock(&dlm->master_lock);
1762 if (dlm_find_mle(dlm, &mle, (char *)lockname,
1764 dlm_print_one_mle(mle);
1767 spin_unlock(&dlm->master_lock);
1768 spin_unlock(&dlm->spinlock);
1772 if (r & DLM_ASSERT_RESPONSE_REASSERT &&
1773 !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
1774 mlog(ML_ERROR, "%.*s: very strange, "
1775 "master MLE but no lockres on %u\n",
1776 namelen, lockname, to);
1779 if (r & DLM_ASSERT_RESPONSE_REASSERT) {
1780 mlog(0, "%.*s: node %u create mles on other "
1781 "nodes and requests a re-assert\n",
1782 namelen, lockname, to);
1785 if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
1786 mlog(0, "%.*s: node %u has a reference to this "
1787 "lockres, set the bit in the refmap\n",
1788 namelen, lockname, to);
1789 spin_lock(&res->spinlock);
1790 dlm_lockres_set_refmap_bit(to, res);
1791 spin_unlock(&res->spinlock);
1798 spin_lock(&res->spinlock);
1799 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
1800 spin_unlock(&res->spinlock);
1807 * locks that can be taken here:
1813 * if possible, TRIM THIS DOWN!!!
1815 int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
1818 struct dlm_ctxt *dlm = data;
1819 struct dlm_master_list_entry *mle = NULL;
1820 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1821 struct dlm_lock_resource *res = NULL;
1823 unsigned int namelen, hash;
1825 int master_request = 0, have_lockres_ref = 0;
1831 name = assert->name;
1832 namelen = assert->namelen;
1833 hash = dlm_lockid_hash(name, namelen);
1834 flags = be32_to_cpu(assert->flags);
1836 if (namelen > DLM_LOCKID_NAME_MAX) {
1837 mlog(ML_ERROR, "Invalid name length!");
1841 spin_lock(&dlm->spinlock);
1844 mlog(0, "assert_master with flags: %u\n", flags);
1847 spin_lock(&dlm->master_lock);
1848 if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1849 /* not an error, could be master just re-asserting */
1850 mlog(0, "just got an assert_master from %u, but no "
1851 "MLE for it! (%.*s)\n", assert->node_idx,
1854 int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1855 if (bit >= O2NM_MAX_NODES) {
1856 /* not necessarily an error, though less likely.
1857 * could be master just re-asserting. */
1858 mlog(0, "no bits set in the maybe_map, but %u "
1859 "is asserting! (%.*s)\n", assert->node_idx,
1861 } else if (bit != assert->node_idx) {
1862 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1863 mlog(0, "master %u was found, %u should "
1864 "back off\n", assert->node_idx, bit);
1866 /* with the fix for bug 569, a higher node
1867 * number winning the mastery will respond
1868 * YES to mastery requests, but this node
1869 * had no way of knowing. let it pass. */
1870 mlog(0, "%u is the lowest node, "
1871 "%u is asserting. (%.*s) %u must "
1872 "have begun after %u won.\n", bit,
1873 assert->node_idx, namelen, name, bit,
1877 if (mle->type == DLM_MLE_MIGRATION) {
1878 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1879 mlog(0, "%s:%.*s: got cleanup assert"
1880 " from %u for migration\n",
1881 dlm->name, namelen, name,
1883 } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
1884 mlog(0, "%s:%.*s: got unrelated assert"
1885 " from %u for migration, ignoring\n",
1886 dlm->name, namelen, name,
1889 spin_unlock(&dlm->master_lock);
1890 spin_unlock(&dlm->spinlock);
1895 spin_unlock(&dlm->master_lock);
1897 /* ok everything checks out with the MLE
1898 * now check to see if there is a lockres */
1899 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
1901 spin_lock(&res->spinlock);
1902 if (res->state & DLM_LOCK_RES_RECOVERING) {
1903 mlog(ML_ERROR, "%u asserting but %.*s is "
1904 "RECOVERING!\n", assert->node_idx, namelen, name);
1908 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
1909 res->owner != assert->node_idx) {
1910 mlog(ML_ERROR, "assert_master from "
1911 "%u, but current owner is "
1913 assert->node_idx, res->owner,
1917 } else if (mle->type != DLM_MLE_MIGRATION) {
1918 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1919 /* owner is just re-asserting */
1920 if (res->owner == assert->node_idx) {
1921 mlog(0, "owner %u re-asserting on "
1922 "lock %.*s\n", assert->node_idx,
1926 mlog(ML_ERROR, "got assert_master from "
1927 "node %u, but %u is the owner! "
1928 "(%.*s)\n", assert->node_idx,
1929 res->owner, namelen, name);
1932 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1933 mlog(ML_ERROR, "got assert from %u, but lock "
1934 "with no owner should be "
1935 "in-progress! (%.*s)\n",
1940 } else /* mle->type == DLM_MLE_MIGRATION */ {
1941 /* should only be getting an assert from new master */
1942 if (assert->node_idx != mle->new_master) {
1943 mlog(ML_ERROR, "got assert from %u, but "
1944 "new master is %u, and old master "
1946 assert->node_idx, mle->new_master,
1947 mle->master, namelen, name);
1953 spin_unlock(&res->spinlock);
1955 spin_unlock(&dlm->spinlock);
1957 // mlog(0, "woo! got an assert_master from node %u!\n",
1958 // assert->node_idx);
1964 spin_lock(&mle->spinlock);
1965 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1968 /* MASTER mle: if any bits set in the response map
1969 * then the calling node needs to re-assert to clear
1970 * up nodes that this node contacted */
1971 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1972 nn+1)) < O2NM_MAX_NODES) {
1973 if (nn != dlm->node_num && nn != assert->node_idx)
1977 mle->master = assert->node_idx;
1978 atomic_set(&mle->woken, 1);
1980 spin_unlock(&mle->spinlock);
1984 spin_lock(&res->spinlock);
1985 if (mle->type == DLM_MLE_MIGRATION) {
1986 mlog(0, "finishing off migration of lockres %.*s, "
1988 res->lockname.len, res->lockname.name,
1989 dlm->node_num, mle->new_master);
1990 res->state &= ~DLM_LOCK_RES_MIGRATING;
1992 dlm_change_lockres_owner(dlm, res, mle->new_master);
1993 BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
1995 dlm_change_lockres_owner(dlm, res, mle->master);
1997 spin_unlock(&res->spinlock);
1998 have_lockres_ref = 1;
2003 /* master is known, detach if not already detached.
2004 * ensures that only one assert_master call will happen
2006 spin_lock(&dlm->spinlock);
2007 spin_lock(&dlm->master_lock);
2009 rr = atomic_read(&mle->mle_refs.refcount);
2010 if (mle->inuse > 0) {
2011 if (extra_ref && rr < 3)
2013 else if (!extra_ref && rr < 2)
2016 if (extra_ref && rr < 2)
2018 else if (!extra_ref && rr < 1)
2022 mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
2023 "that will mess up this node, refs=%d, extra=%d, "
2024 "inuse=%d\n", dlm->name, namelen, name,
2025 assert->node_idx, rr, extra_ref, mle->inuse);
2026 dlm_print_one_mle(mle);
2028 list_del_init(&mle->list);
2029 __dlm_mle_detach_hb_events(dlm, mle);
2032 /* the assert master message now balances the extra
2033 * ref given by the master / migration request message.
2034 * if this is the last put, it will be removed
2038 spin_unlock(&dlm->master_lock);
2039 spin_unlock(&dlm->spinlock);
2041 if (res->owner != assert->node_idx) {
2042 mlog(0, "assert_master from %u, but current "
2043 "owner is %u (%.*s), no mle\n", assert->node_idx,
2044 res->owner, namelen, name);
2051 spin_lock(&res->spinlock);
2052 res->state |= DLM_LOCK_RES_SETREF_INPROG;
2053 spin_unlock(&res->spinlock);
2054 *ret_data = (void *)res;
2057 if (master_request) {
2058 mlog(0, "need to tell master to reassert\n");
2059 /* positive. negative would shoot down the node. */
2060 ret |= DLM_ASSERT_RESPONSE_REASSERT;
2061 if (!have_lockres_ref) {
2062 mlog(ML_ERROR, "strange, got assert from %u, MASTER "
2063 "mle present here for %s:%.*s, but no lockres!\n",
2064 assert->node_idx, dlm->name, namelen, name);
2067 if (have_lockres_ref) {
2068 /* let the master know we have a reference to the lockres */
2069 ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
2070 mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
2071 dlm->name, namelen, name, assert->node_idx);
2076 /* kill the caller! */
2077 mlog(ML_ERROR, "Bad message received from another node. Dumping state "
2078 "and killing the other node now! This node is OK and can continue.\n");
2079 __dlm_print_one_lock_resource(res);
2080 spin_unlock(&res->spinlock);
2081 spin_unlock(&dlm->spinlock);
2082 *ret_data = (void *)res;
2087 void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
2089 struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
2092 spin_lock(&res->spinlock);
2093 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
2094 spin_unlock(&res->spinlock);
2096 dlm_lockres_put(res);
2101 int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
2102 struct dlm_lock_resource *res,
2103 int ignore_higher, u8 request_from, u32 flags)
2105 struct dlm_work_item *item;
2106 item = kzalloc(sizeof(*item), GFP_NOFS);
2111 /* queue up work for dlm_assert_master_worker */
2112 dlm_grab(dlm); /* get an extra ref for the work item */
2113 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
2114 item->u.am.lockres = res; /* already have a ref */
2115 /* can optionally ignore node numbers higher than this node */
2116 item->u.am.ignore_higher = ignore_higher;
2117 item->u.am.request_from = request_from;
2118 item->u.am.flags = flags;
2121 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
2122 res->lockname.name);
2124 spin_lock(&dlm->work_lock);
2125 list_add_tail(&item->list, &dlm->work_list);
2126 spin_unlock(&dlm->work_lock);
2128 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2132 static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
2134 struct dlm_ctxt *dlm = data;
2136 struct dlm_lock_resource *res;
2137 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
2144 res = item->u.am.lockres;
2145 ignore_higher = item->u.am.ignore_higher;
2146 request_from = item->u.am.request_from;
2147 flags = item->u.am.flags;
2149 spin_lock(&dlm->spinlock);
2150 memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
2151 spin_unlock(&dlm->spinlock);
2153 clear_bit(dlm->node_num, nodemap);
2154 if (ignore_higher) {
2155 /* if is this just to clear up mles for nodes below
2156 * this node, do not send the message to the original
2157 * caller or any node number higher than this */
2158 clear_bit(request_from, nodemap);
2159 bit = dlm->node_num;
2161 bit = find_next_bit(nodemap, O2NM_MAX_NODES,
2163 if (bit >= O2NM_MAX_NODES)
2165 clear_bit(bit, nodemap);
2170 * If we're migrating this lock to someone else, we are no
2171 * longer allowed to assert out own mastery. OTOH, we need to
2172 * prevent migration from starting while we're still asserting
2173 * our dominance. The reserved ast delays migration.
2175 spin_lock(&res->spinlock);
2176 if (res->state & DLM_LOCK_RES_MIGRATING) {
2177 mlog(0, "Someone asked us to assert mastery, but we're "
2178 "in the middle of migration. Skipping assert, "
2179 "the new master will handle that.\n");
2180 spin_unlock(&res->spinlock);
2183 __dlm_lockres_reserve_ast(res);
2184 spin_unlock(&res->spinlock);
2186 /* this call now finishes out the nodemap
2187 * even if one or more nodes die */
2188 mlog(0, "worker about to master %.*s here, this=%u\n",
2189 res->lockname.len, res->lockname.name, dlm->node_num);
2190 ret = dlm_do_assert_master(dlm, res, nodemap, flags);
2192 /* no need to restart, we are done */
2193 if (!dlm_is_host_down(ret))
2197 /* Ok, we've asserted ourselves. Let's let migration start. */
2198 dlm_lockres_release_ast(dlm, res);
2201 dlm_lockres_put(res);
2203 mlog(0, "finished with dlm_assert_master_worker\n");
2206 /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2207 * We cannot wait for node recovery to complete to begin mastering this
2208 * lockres because this lockres is used to kick off recovery! ;-)
2209 * So, do a pre-check on all living nodes to see if any of those nodes
2210 * think that $RECOVERY is currently mastered by a dead node. If so,
2211 * we wait a short time to allow that node to get notified by its own
2212 * heartbeat stack, then check again. All $RECOVERY lock resources
2213 * mastered by dead nodes are purged when the hearbeat callback is
2214 * fired, so we can know for sure that it is safe to continue once
2215 * the node returns a live node or no node. */
2216 static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
2217 struct dlm_lock_resource *res)
2219 struct dlm_node_iter iter;
2222 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
2224 spin_lock(&dlm->spinlock);
2225 dlm_node_iter_init(dlm->domain_map, &iter);
2226 spin_unlock(&dlm->spinlock);
2228 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2229 /* do not send to self */
2230 if (nodenum == dlm->node_num)
2232 ret = dlm_do_master_requery(dlm, res, nodenum, &master);
2235 if (!dlm_is_host_down(ret))
2237 /* host is down, so answer for that node would be
2238 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
2242 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
2243 /* check to see if this master is in the recovery map */
2244 spin_lock(&dlm->spinlock);
2245 if (test_bit(master, dlm->recovery_map)) {
2246 mlog(ML_NOTICE, "%s: node %u has not seen "
2247 "node %u go down yet, and thinks the "
2248 "dead node is mastering the recovery "
2249 "lock. must wait.\n", dlm->name,
2253 spin_unlock(&dlm->spinlock);
2254 mlog(0, "%s: reco lock master is %u\n", dlm->name,
2263 * DLM_DEREF_LOCKRES_MSG
2266 int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2268 struct dlm_deref_lockres deref;
2270 const char *lockname;
2271 unsigned int namelen;
2273 lockname = res->lockname.name;
2274 namelen = res->lockname.len;
2275 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
2277 mlog(0, "%s:%.*s: sending deref to %d\n",
2278 dlm->name, namelen, lockname, res->owner);
2279 memset(&deref, 0, sizeof(deref));
2280 deref.node_idx = dlm->node_num;
2281 deref.namelen = namelen;
2282 memcpy(deref.name, lockname, namelen);
2284 ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
2285 &deref, sizeof(deref), res->owner, &r);
2289 /* BAD. other node says I did not have a ref. */
2290 mlog(ML_ERROR,"while dropping ref on %s:%.*s "
2291 "(master=%u) got %d.\n", dlm->name, namelen,
2292 lockname, res->owner, r);
2293 dlm_print_one_lock_resource(res);
2299 int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
2302 struct dlm_ctxt *dlm = data;
2303 struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
2304 struct dlm_lock_resource *res = NULL;
2306 unsigned int namelen;
2310 struct dlm_work_item *item;
2318 namelen = deref->namelen;
2319 node = deref->node_idx;
2321 if (namelen > DLM_LOCKID_NAME_MAX) {
2322 mlog(ML_ERROR, "Invalid name length!");
2325 if (deref->node_idx >= O2NM_MAX_NODES) {
2326 mlog(ML_ERROR, "Invalid node number: %u\n", node);
2330 hash = dlm_lockid_hash(name, namelen);
2332 spin_lock(&dlm->spinlock);
2333 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
2335 spin_unlock(&dlm->spinlock);
2336 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
2337 dlm->name, namelen, name);
2340 spin_unlock(&dlm->spinlock);
2342 spin_lock(&res->spinlock);
2343 if (res->state & DLM_LOCK_RES_SETREF_INPROG)
2346 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2347 if (test_bit(node, res->refmap)) {
2348 dlm_lockres_clear_refmap_bit(node, res);
2352 spin_unlock(&res->spinlock);
2356 dlm_lockres_calc_usage(dlm, res);
2358 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2359 "but it is already dropped!\n", dlm->name,
2360 res->lockname.len, res->lockname.name, node);
2361 __dlm_print_one_lock_resource(res);
2367 item = kzalloc(sizeof(*item), GFP_NOFS);
2374 dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
2375 item->u.dl.deref_res = res;
2376 item->u.dl.deref_node = node;
2378 spin_lock(&dlm->work_lock);
2379 list_add_tail(&item->list, &dlm->work_list);
2380 spin_unlock(&dlm->work_lock);
2382 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2387 dlm_lockres_put(res);
2393 static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
2395 struct dlm_ctxt *dlm;
2396 struct dlm_lock_resource *res;
2401 res = item->u.dl.deref_res;
2402 node = item->u.dl.deref_node;
2404 spin_lock(&res->spinlock);
2405 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2406 if (test_bit(node, res->refmap)) {
2407 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2408 dlm_lockres_clear_refmap_bit(node, res);
2411 spin_unlock(&res->spinlock);
2414 mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
2415 dlm->name, res->lockname.len, res->lockname.name, node);
2416 dlm_lockres_calc_usage(dlm, res);
2418 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2419 "but it is already dropped!\n", dlm->name,
2420 res->lockname.len, res->lockname.name, node);
2421 __dlm_print_one_lock_resource(res);
2424 dlm_lockres_put(res);
2427 /* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
2428 * if not. If 0, numlocks is set to the number of locks in the lockres.
2430 static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
2431 struct dlm_lock_resource *res,
2437 struct list_head *queue, *iter;
2438 struct dlm_lock *lock;
2440 assert_spin_locked(&res->spinlock);
2443 if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2444 mlog(0, "cannot migrate lockres with unknown owner!\n");
2448 if (res->owner != dlm->node_num) {
2449 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2454 queue = &res->granted;
2455 for (i = 0; i < 3; i++) {
2456 list_for_each(iter, queue) {
2457 lock = list_entry(iter, struct dlm_lock, list);
2459 if (lock->ml.node == dlm->node_num) {
2460 mlog(0, "found a lock owned by this node still "
2461 "on the %s queue! will not migrate this "
2462 "lockres\n", (i == 0 ? "granted" :
2463 (i == 1 ? "converting" :
2473 mlog(0, "migrateable lockres having %d locks\n", *numlocks);
2480 * DLM_MIGRATE_LOCKRES
2484 static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
2485 struct dlm_lock_resource *res,
2488 struct dlm_master_list_entry *mle = NULL;
2489 struct dlm_master_list_entry *oldmle = NULL;
2490 struct dlm_migratable_lockres *mres = NULL;
2493 unsigned int namelen;
2501 name = res->lockname.name;
2502 namelen = res->lockname.len;
2504 mlog(0, "migrating %.*s to %u\n", namelen, name, target);
2507 * ensure this lockres is a proper candidate for migration
2509 spin_lock(&res->spinlock);
2510 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2512 spin_unlock(&res->spinlock);
2515 spin_unlock(&res->spinlock);
2518 if (numlocks == 0) {
2519 mlog(0, "no locks were found on this lockres! done!\n");
2524 * preallocate up front
2525 * if this fails, abort
2529 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
2535 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
2544 * find a node to migrate the lockres to
2547 mlog(0, "picking a migration node\n");
2548 spin_lock(&dlm->spinlock);
2549 /* pick a new node */
2550 if (!test_bit(target, dlm->domain_map) ||
2551 target >= O2NM_MAX_NODES) {
2552 target = dlm_pick_migration_target(dlm, res);
2554 mlog(0, "node %u chosen for migration\n", target);
2556 if (target >= O2NM_MAX_NODES ||
2557 !test_bit(target, dlm->domain_map)) {
2558 /* target chosen is not alive */
2563 spin_unlock(&dlm->spinlock);
2567 mlog(0, "continuing with target = %u\n", target);
2570 * clear any existing master requests and
2571 * add the migration mle to the list
2573 spin_lock(&dlm->master_lock);
2574 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2575 namelen, target, dlm->node_num);
2576 spin_unlock(&dlm->master_lock);
2577 spin_unlock(&dlm->spinlock);
2579 if (ret == -EEXIST) {
2580 mlog(0, "another process is already migrating it\n");
2586 * set the MIGRATING flag and flush asts
2587 * if we fail after this we need to re-dirty the lockres
2589 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2590 mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2591 "the target went down.\n", res->lockname.len,
2592 res->lockname.name, target);
2593 spin_lock(&res->spinlock);
2594 res->state &= ~DLM_LOCK_RES_MIGRATING;
2596 spin_unlock(&res->spinlock);
2602 /* master is known, detach if not already detached */
2603 dlm_mle_detach_hb_events(dlm, oldmle);
2604 dlm_put_mle(oldmle);
2609 dlm_mle_detach_hb_events(dlm, mle);
2612 kmem_cache_free(dlm_mle_cache, mle);
2618 * at this point, we have a migration target, an mle
2619 * in the master list, and the MIGRATING flag set on
2623 /* now that remote nodes are spinning on the MIGRATING flag,
2624 * ensure that all assert_master work is flushed. */
2625 flush_workqueue(dlm->dlm_worker);
2627 /* get an extra reference on the mle.
2628 * otherwise the assert_master from the new
2629 * master will destroy this.
2630 * also, make sure that all callers of dlm_get_mle
2631 * take both dlm->spinlock and dlm->master_lock */
2632 spin_lock(&dlm->spinlock);
2633 spin_lock(&dlm->master_lock);
2634 dlm_get_mle_inuse(mle);
2635 spin_unlock(&dlm->master_lock);
2636 spin_unlock(&dlm->spinlock);
2638 /* notify new node and send all lock state */
2639 /* call send_one_lockres with migration flag.
2640 * this serves as notice to the target node that a
2641 * migration is starting. */
2642 ret = dlm_send_one_lockres(dlm, res, mres, target,
2643 DLM_MRES_MIGRATION);
2646 mlog(0, "migration to node %u failed with %d\n",
2648 /* migration failed, detach and clean up mle */
2649 dlm_mle_detach_hb_events(dlm, mle);
2651 dlm_put_mle_inuse(mle);
2652 spin_lock(&res->spinlock);
2653 res->state &= ~DLM_LOCK_RES_MIGRATING;
2655 spin_unlock(&res->spinlock);
2659 /* at this point, the target sends a message to all nodes,
2660 * (using dlm_do_migrate_request). this node is skipped since
2661 * we had to put an mle in the list to begin the process. this
2662 * node now waits for target to do an assert master. this node
2663 * will be the last one notified, ensuring that the migration
2664 * is complete everywhere. if the target dies while this is
2665 * going on, some nodes could potentially see the target as the
2666 * master, so it is important that my recovery finds the migration
2667 * mle and sets the master to UNKNONWN. */
2670 /* wait for new node to assert master */
2672 ret = wait_event_interruptible_timeout(mle->wq,
2673 (atomic_read(&mle->woken) == 1),
2674 msecs_to_jiffies(5000));
2677 if (atomic_read(&mle->woken) == 1 ||
2678 res->owner == target)
2681 mlog(0, "%s:%.*s: timed out during migration\n",
2682 dlm->name, res->lockname.len, res->lockname.name);
2683 /* avoid hang during shutdown when migrating lockres
2684 * to a node which also goes down */
2685 if (dlm_is_node_dead(dlm, target)) {
2686 mlog(0, "%s:%.*s: expected migration "
2687 "target %u is no longer up, restarting\n",
2688 dlm->name, res->lockname.len,
2689 res->lockname.name, target);
2691 /* migration failed, detach and clean up mle */
2692 dlm_mle_detach_hb_events(dlm, mle);
2694 dlm_put_mle_inuse(mle);
2695 spin_lock(&res->spinlock);
2696 res->state &= ~DLM_LOCK_RES_MIGRATING;
2698 spin_unlock(&res->spinlock);
2702 mlog(0, "%s:%.*s: caught signal during migration\n",
2703 dlm->name, res->lockname.len, res->lockname.name);
2706 /* all done, set the owner, clear the flag */
2707 spin_lock(&res->spinlock);
2708 dlm_set_lockres_owner(dlm, res, target);
2709 res->state &= ~DLM_LOCK_RES_MIGRATING;
2710 dlm_remove_nonlocal_locks(dlm, res);
2711 spin_unlock(&res->spinlock);
2714 /* master is known, detach if not already detached */
2715 dlm_mle_detach_hb_events(dlm, mle);
2716 dlm_put_mle_inuse(mle);
2719 dlm_lockres_calc_usage(dlm, res);
2722 /* re-dirty the lockres if we failed */
2724 dlm_kick_thread(dlm, res);
2726 /* wake up waiters if the MIGRATING flag got set
2727 * but migration failed */
2733 free_page((unsigned long)mres);
2737 mlog(0, "returning %d\n", ret);
2741 #define DLM_MIGRATION_RETRY_MS 100
2743 /* Should be called only after beginning the domain leave process.
2744 * There should not be any remaining locks on nonlocal lock resources,
2745 * and there should be no local locks left on locally mastered resources.
2747 * Called with the dlm spinlock held, may drop it to do migration, but
2748 * will re-acquire before exit.
2750 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2751 int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2754 int lock_dropped = 0;
2757 spin_lock(&res->spinlock);
2758 if (res->owner != dlm->node_num) {
2759 if (!__dlm_lockres_unused(res)) {
2760 mlog(ML_ERROR, "%s:%.*s: this node is not master, "
2761 "trying to free this but locks remain\n",
2762 dlm->name, res->lockname.len, res->lockname.name);
2764 spin_unlock(&res->spinlock);
2768 /* No need to migrate a lockres having no locks */
2769 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2770 if (ret >= 0 && numlocks == 0) {
2771 spin_unlock(&res->spinlock);
2774 spin_unlock(&res->spinlock);
2776 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2777 spin_unlock(&dlm->spinlock);
2780 ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
2783 if (ret == -ENOTEMPTY) {
2784 mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
2785 res->lockname.len, res->lockname.name);
2789 mlog(0, "lockres %.*s: migrate failed, "
2790 "retrying\n", res->lockname.len,
2791 res->lockname.name);
2792 msleep(DLM_MIGRATION_RETRY_MS);
2794 spin_lock(&dlm->spinlock);
2796 return lock_dropped;
2799 int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2802 spin_lock(&dlm->ast_lock);
2803 spin_lock(&lock->spinlock);
2804 ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2805 spin_unlock(&lock->spinlock);
2806 spin_unlock(&dlm->ast_lock);
2810 static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2811 struct dlm_lock_resource *res,
2815 spin_lock(&res->spinlock);
2816 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2817 spin_unlock(&res->spinlock);
2819 /* target has died, so make the caller break out of the
2820 * wait_event, but caller must recheck the domain_map */
2821 spin_lock(&dlm->spinlock);
2822 if (!test_bit(mig_target, dlm->domain_map))
2824 spin_unlock(&dlm->spinlock);
2828 static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
2829 struct dlm_lock_resource *res)
2832 spin_lock(&res->spinlock);
2833 ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2834 spin_unlock(&res->spinlock);
2839 static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2840 struct dlm_lock_resource *res,
2845 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2846 res->lockname.len, res->lockname.name, dlm->node_num,
2848 /* need to set MIGRATING flag on lockres. this is done by
2849 * ensuring that all asts have been flushed for this lockres. */
2850 spin_lock(&res->spinlock);
2851 BUG_ON(res->migration_pending);
2852 res->migration_pending = 1;
2853 /* strategy is to reserve an extra ast then release
2854 * it below, letting the release do all of the work */
2855 __dlm_lockres_reserve_ast(res);
2856 spin_unlock(&res->spinlock);
2858 /* now flush all the pending asts */
2859 dlm_kick_thread(dlm, res);
2860 /* before waiting on DIRTY, block processes which may
2861 * try to dirty the lockres before MIGRATING is set */
2862 spin_lock(&res->spinlock);
2863 BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
2864 res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
2865 spin_unlock(&res->spinlock);
2866 /* now wait on any pending asts and the DIRTY state */
2867 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2868 dlm_lockres_release_ast(dlm, res);
2870 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2871 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2872 /* if the extra ref we just put was the final one, this
2873 * will pass thru immediately. otherwise, we need to wait
2874 * for the last ast to finish. */
2876 ret = wait_event_interruptible_timeout(dlm->migration_wq,
2877 dlm_migration_can_proceed(dlm, res, target),
2878 msecs_to_jiffies(1000));
2880 mlog(0, "woken again: migrating? %s, dead? %s\n",
2881 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2882 test_bit(target, dlm->domain_map) ? "no":"yes");
2884 mlog(0, "all is well: migrating? %s, dead? %s\n",
2885 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2886 test_bit(target, dlm->domain_map) ? "no":"yes");
2888 if (!dlm_migration_can_proceed(dlm, res, target)) {
2889 mlog(0, "trying again...\n");
2892 /* now that we are sure the MIGRATING state is there, drop
2893 * the unneded state which blocked threads trying to DIRTY */
2894 spin_lock(&res->spinlock);
2895 BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
2896 BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
2897 res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
2898 spin_unlock(&res->spinlock);
2900 /* did the target go down or die? */
2901 spin_lock(&dlm->spinlock);
2902 if (!test_bit(target, dlm->domain_map)) {
2903 mlog(ML_ERROR, "aha. migration target %u just went down\n",
2907 spin_unlock(&dlm->spinlock);
2912 * o the DLM_LOCK_RES_MIGRATING flag is set
2913 * o there are no pending asts on this lockres
2914 * o all processes trying to reserve an ast on this
2915 * lockres must wait for the MIGRATING flag to clear
2920 /* last step in the migration process.
2921 * original master calls this to free all of the dlm_lock
2922 * structures that used to be for other nodes. */
2923 static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2924 struct dlm_lock_resource *res)
2926 struct list_head *iter, *iter2;
2927 struct list_head *queue = &res->granted;
2929 struct dlm_lock *lock;
2931 assert_spin_locked(&res->spinlock);
2933 BUG_ON(res->owner == dlm->node_num);
2935 for (i=0; i<3; i++) {
2936 list_for_each_safe(iter, iter2, queue) {
2937 lock = list_entry (iter, struct dlm_lock, list);
2938 if (lock->ml.node != dlm->node_num) {
2939 mlog(0, "putting lock for node %u\n",
2941 /* be extra careful */
2942 BUG_ON(!list_empty(&lock->ast_list));
2943 BUG_ON(!list_empty(&lock->bast_list));
2944 BUG_ON(lock->ast_pending);
2945 BUG_ON(lock->bast_pending);
2946 dlm_lockres_clear_refmap_bit(lock->ml.node, res);
2947 list_del_init(&lock->list);
2955 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
2956 if (bit >= O2NM_MAX_NODES)
2958 /* do not clear the local node reference, if there is a
2959 * process holding this, let it drop the ref itself */
2960 if (bit != dlm->node_num) {
2961 mlog(0, "%s:%.*s: node %u had a ref to this "
2962 "migrating lockres, clearing\n", dlm->name,
2963 res->lockname.len, res->lockname.name, bit);
2964 dlm_lockres_clear_refmap_bit(bit, res);
2970 /* for now this is not too intelligent. we will
2971 * need stats to make this do the right thing.
2972 * this just finds the first lock on one of the
2973 * queues and uses that node as the target. */
2974 static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
2975 struct dlm_lock_resource *res)
2978 struct list_head *queue = &res->granted;
2979 struct list_head *iter;
2980 struct dlm_lock *lock;
2983 assert_spin_locked(&dlm->spinlock);
2985 spin_lock(&res->spinlock);
2986 for (i=0; i<3; i++) {
2987 list_for_each(iter, queue) {
2988 /* up to the caller to make sure this node
2990 lock = list_entry (iter, struct dlm_lock, list);
2991 if (lock->ml.node != dlm->node_num) {
2992 spin_unlock(&res->spinlock);
2993 return lock->ml.node;
2998 spin_unlock(&res->spinlock);
2999 mlog(0, "have not found a suitable target yet! checking domain map\n");
3001 /* ok now we're getting desperate. pick anyone alive. */
3004 nodenum = find_next_bit(dlm->domain_map,
3005 O2NM_MAX_NODES, nodenum+1);
3006 mlog(0, "found %d in domain map\n", nodenum);
3007 if (nodenum >= O2NM_MAX_NODES)
3009 if (nodenum != dlm->node_num) {
3010 mlog(0, "picking %d\n", nodenum);
3015 mlog(0, "giving up. no master to migrate to\n");
3016 return DLM_LOCK_RES_OWNER_UNKNOWN;
3021 /* this is called by the new master once all lockres
3022 * data has been received */
3023 static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
3024 struct dlm_lock_resource *res,
3025 u8 master, u8 new_master,
3026 struct dlm_node_iter *iter)
3028 struct dlm_migrate_request migrate;
3029 int ret, status = 0;
3032 memset(&migrate, 0, sizeof(migrate));
3033 migrate.namelen = res->lockname.len;
3034 memcpy(migrate.name, res->lockname.name, migrate.namelen);
3035 migrate.new_master = new_master;
3036 migrate.master = master;
3040 /* send message to all nodes, except the master and myself */
3041 while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
3042 if (nodenum == master ||
3043 nodenum == new_master)
3046 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
3047 &migrate, sizeof(migrate), nodenum,
3051 else if (status < 0) {
3052 mlog(0, "migrate request (node %u) returned %d!\n",
3055 } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
3056 /* during the migration request we short-circuited
3057 * the mastery of the lockres. make sure we have
3058 * a mastery ref for nodenum */
3059 mlog(0, "%s:%.*s: need ref for node %u\n",
3060 dlm->name, res->lockname.len, res->lockname.name,
3062 spin_lock(&res->spinlock);
3063 dlm_lockres_set_refmap_bit(nodenum, res);
3064 spin_unlock(&res->spinlock);
3071 mlog(0, "returning ret=%d\n", ret);
3076 /* if there is an existing mle for this lockres, we now know who the master is.
3077 * (the one who sent us *this* message) we can clear it up right away.
3078 * since the process that put the mle on the list still has a reference to it,
3079 * we can unhash it now, set the master and wake the process. as a result,
3080 * we will have no mle in the list to start with. now we can add an mle for
3081 * the migration and this should be the only one found for those scanning the
3083 int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
3086 struct dlm_ctxt *dlm = data;
3087 struct dlm_lock_resource *res = NULL;
3088 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
3089 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
3091 unsigned int namelen, hash;
3097 name = migrate->name;
3098 namelen = migrate->namelen;
3099 hash = dlm_lockid_hash(name, namelen);
3101 /* preallocate.. if this fails, abort */
3102 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
3110 /* check for pre-existing lock */
3111 spin_lock(&dlm->spinlock);
3112 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
3113 spin_lock(&dlm->master_lock);
3116 spin_lock(&res->spinlock);
3117 if (res->state & DLM_LOCK_RES_RECOVERING) {
3118 /* if all is working ok, this can only mean that we got
3119 * a migrate request from a node that we now see as
3120 * dead. what can we do here? drop it to the floor? */
3121 spin_unlock(&res->spinlock);
3122 mlog(ML_ERROR, "Got a migrate request, but the "
3123 "lockres is marked as recovering!");
3124 kmem_cache_free(dlm_mle_cache, mle);
3125 ret = -EINVAL; /* need a better solution */
3128 res->state |= DLM_LOCK_RES_MIGRATING;
3129 spin_unlock(&res->spinlock);
3132 /* ignore status. only nonzero status would BUG. */
3133 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
3135 migrate->new_master,
3139 spin_unlock(&dlm->master_lock);
3140 spin_unlock(&dlm->spinlock);
3143 /* master is known, detach if not already detached */
3144 dlm_mle_detach_hb_events(dlm, oldmle);
3145 dlm_put_mle(oldmle);
3149 dlm_lockres_put(res);
3155 /* must be holding dlm->spinlock and dlm->master_lock
3156 * when adding a migration mle, we can clear any other mles
3157 * in the master list because we know with certainty that
3158 * the master is "master". so we remove any old mle from
3159 * the list after setting it's master field, and then add
3160 * the new migration mle. this way we can hold with the rule
3161 * of having only one mle for a given lock name at all times. */
3162 static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
3163 struct dlm_lock_resource *res,
3164 struct dlm_master_list_entry *mle,
3165 struct dlm_master_list_entry **oldmle,
3166 const char *name, unsigned int namelen,
3167 u8 new_master, u8 master)
3176 assert_spin_locked(&dlm->spinlock);
3177 assert_spin_locked(&dlm->master_lock);
3179 /* caller is responsible for any ref taken here on oldmle */
3180 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
3182 struct dlm_master_list_entry *tmp = *oldmle;
3183 spin_lock(&tmp->spinlock);
3184 if (tmp->type == DLM_MLE_MIGRATION) {
3185 if (master == dlm->node_num) {
3186 /* ah another process raced me to it */
3187 mlog(0, "tried to migrate %.*s, but some "
3188 "process beat me to it\n",
3192 /* bad. 2 NODES are trying to migrate! */
3193 mlog(ML_ERROR, "migration error mle: "
3194 "master=%u new_master=%u // request: "
3195 "master=%u new_master=%u // "
3197 tmp->master, tmp->new_master,
3203 /* this is essentially what assert_master does */
3204 tmp->master = master;
3205 atomic_set(&tmp->woken, 1);
3207 /* remove it from the list so that only one
3208 * mle will be found */
3209 list_del_init(&tmp->list);
3210 /* this was obviously WRONG. mle is uninited here. should be tmp. */
3211 __dlm_mle_detach_hb_events(dlm, tmp);
3212 ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
3213 mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3214 "telling master to get ref for cleared out mle "
3215 "during migration\n", dlm->name, namelen, name,
3216 master, new_master);
3218 spin_unlock(&tmp->spinlock);
3221 /* now add a migration mle to the tail of the list */
3222 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
3223 mle->new_master = new_master;
3224 /* the new master will be sending an assert master for this.
3225 * at that point we will get the refmap reference */
3226 mle->master = master;
3227 /* do this for consistency with other mle types */
3228 set_bit(new_master, mle->maybe_map);
3229 list_add(&mle->list, &dlm->master_list);
3235 void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
3237 struct list_head *iter, *iter2;
3238 struct dlm_master_list_entry *mle;
3239 struct dlm_lock_resource *res;
3242 mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
3244 assert_spin_locked(&dlm->spinlock);
3246 /* clean the master list */
3247 spin_lock(&dlm->master_lock);
3248 list_for_each_safe(iter, iter2, &dlm->master_list) {
3249 mle = list_entry(iter, struct dlm_master_list_entry, list);
3251 BUG_ON(mle->type != DLM_MLE_BLOCK &&
3252 mle->type != DLM_MLE_MASTER &&
3253 mle->type != DLM_MLE_MIGRATION);
3255 /* MASTER mles are initiated locally. the waiting
3256 * process will notice the node map change
3257 * shortly. let that happen as normal. */
3258 if (mle->type == DLM_MLE_MASTER)
3262 /* BLOCK mles are initiated by other nodes.
3263 * need to clean up if the dead node would have
3264 * been the master. */
3265 if (mle->type == DLM_MLE_BLOCK) {
3268 spin_lock(&mle->spinlock);
3269 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
3270 if (bit != dead_node) {
3271 mlog(0, "mle found, but dead node %u would "
3272 "not have been master\n", dead_node);
3273 spin_unlock(&mle->spinlock);
3275 /* must drop the refcount by one since the
3276 * assert_master will never arrive. this
3277 * may result in the mle being unlinked and
3278 * freed, but there may still be a process
3279 * waiting in the dlmlock path which is fine. */
3280 mlog(0, "node %u was expected master\n",
3282 atomic_set(&mle->woken, 1);
3283 spin_unlock(&mle->spinlock);
3285 /* do not need events any longer, so detach
3287 __dlm_mle_detach_hb_events(dlm, mle);
3293 /* everything else is a MIGRATION mle */
3295 /* the rule for MIGRATION mles is that the master
3296 * becomes UNKNOWN if *either* the original or
3297 * the new master dies. all UNKNOWN lockreses
3298 * are sent to whichever node becomes the recovery
3299 * master. the new master is responsible for
3300 * determining if there is still a master for
3301 * this lockres, or if he needs to take over
3302 * mastery. either way, this node should expect
3303 * another message to resolve this. */
3304 if (mle->master != dead_node &&
3305 mle->new_master != dead_node)
3308 /* if we have reached this point, this mle needs to
3309 * be removed from the list and freed. */
3311 /* remove from the list early. NOTE: unlinking
3312 * list_head while in list_for_each_safe */
3313 __dlm_mle_detach_hb_events(dlm, mle);
3314 spin_lock(&mle->spinlock);
3315 list_del_init(&mle->list);
3316 atomic_set(&mle->woken, 1);
3317 spin_unlock(&mle->spinlock);
3320 mlog(0, "%s: node %u died during migration from "
3321 "%u to %u!\n", dlm->name, dead_node,
3322 mle->master, mle->new_master);
3323 /* if there is a lockres associated with this
3324 * mle, find it and set its owner to UNKNOWN */
3325 hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
3326 res = __dlm_lookup_lockres(dlm, mle->u.name.name,
3327 mle->u.name.len, hash);
3329 /* unfortunately if we hit this rare case, our
3330 * lock ordering is messed. we need to drop
3331 * the master lock so that we can take the
3332 * lockres lock, meaning that we will have to
3333 * restart from the head of list. */
3334 spin_unlock(&dlm->master_lock);
3336 /* move lockres onto recovery list */
3337 spin_lock(&res->spinlock);
3338 dlm_set_lockres_owner(dlm, res,
3339 DLM_LOCK_RES_OWNER_UNKNOWN);
3340 dlm_move_lockres_to_recovery_list(dlm, res);
3341 spin_unlock(&res->spinlock);
3342 dlm_lockres_put(res);
3344 /* about to get rid of mle, detach from heartbeat */
3345 __dlm_mle_detach_hb_events(dlm, mle);
3348 spin_lock(&dlm->master_lock);
3350 spin_unlock(&dlm->master_lock);
3356 /* this may be the last reference */
3359 spin_unlock(&dlm->master_lock);
3363 int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
3366 struct dlm_node_iter iter;
3369 spin_lock(&dlm->spinlock);
3370 dlm_node_iter_init(dlm->domain_map, &iter);
3371 clear_bit(old_master, iter.node_map);
3372 clear_bit(dlm->node_num, iter.node_map);
3373 spin_unlock(&dlm->spinlock);
3375 /* ownership of the lockres is changing. account for the
3376 * mastery reference here since old_master will briefly have
3377 * a reference after the migration completes */
3378 spin_lock(&res->spinlock);
3379 dlm_lockres_set_refmap_bit(old_master, res);
3380 spin_unlock(&res->spinlock);
3382 mlog(0, "now time to do a migrate request to other nodes\n");
3383 ret = dlm_do_migrate_request(dlm, res, old_master,
3384 dlm->node_num, &iter);
3390 mlog(0, "doing assert master of %.*s to all except the original node\n",
3391 res->lockname.len, res->lockname.name);
3392 /* this call now finishes out the nodemap
3393 * even if one or more nodes die */
3394 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3395 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3397 /* no longer need to retry. all living nodes contacted. */
3402 memset(iter.node_map, 0, sizeof(iter.node_map));
3403 set_bit(old_master, iter.node_map);
3404 mlog(0, "doing assert master of %.*s back to %u\n",
3405 res->lockname.len, res->lockname.name, old_master);
3406 ret = dlm_do_assert_master(dlm, res, iter.node_map,
3407 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3409 mlog(0, "assert master to original master failed "
3411 /* the only nonzero status here would be because of
3412 * a dead original node. we're done. */
3416 /* all done, set the owner, clear the flag */
3417 spin_lock(&res->spinlock);
3418 dlm_set_lockres_owner(dlm, res, dlm->node_num);
3419 res->state &= ~DLM_LOCK_RES_MIGRATING;
3420 spin_unlock(&res->spinlock);
3421 /* re-dirty it on the new master */
3422 dlm_kick_thread(dlm, res);
3429 * LOCKRES AST REFCOUNT
3430 * this is integral to migration
3433 /* for future intent to call an ast, reserve one ahead of time.
3434 * this should be called only after waiting on the lockres
3435 * with dlm_wait_on_lockres, and while still holding the
3436 * spinlock after the call. */
3437 void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
3439 assert_spin_locked(&res->spinlock);
3440 if (res->state & DLM_LOCK_RES_MIGRATING) {
3441 __dlm_print_one_lock_resource(res);
3443 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3445 atomic_inc(&res->asts_reserved);
3449 * used to drop the reserved ast, either because it went unused,
3450 * or because the ast/bast was actually called.
3452 * also, if there is a pending migration on this lockres,
3453 * and this was the last pending ast on the lockres,
3454 * atomically set the MIGRATING flag before we drop the lock.
3455 * this is how we ensure that migration can proceed with no
3456 * asts in progress. note that it is ok if the state of the
3457 * queues is such that a lock should be granted in the future
3458 * or that a bast should be fired, because the new master will
3459 * shuffle the lists on this lockres as soon as it is migrated.
3461 void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
3462 struct dlm_lock_resource *res)
3464 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
3467 if (!res->migration_pending) {
3468 spin_unlock(&res->spinlock);
3472 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3473 res->migration_pending = 0;
3474 res->state |= DLM_LOCK_RES_MIGRATING;
3475 spin_unlock(&res->spinlock);
3477 wake_up(&dlm->migration_wq);