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"
53 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
54 #include "cluster/masklog.h"
56 static void dlm_mle_node_down(struct dlm_ctxt
*dlm
,
57 struct dlm_master_list_entry
*mle
,
58 struct o2nm_node
*node
,
60 static void dlm_mle_node_up(struct dlm_ctxt
*dlm
,
61 struct dlm_master_list_entry
*mle
,
62 struct o2nm_node
*node
,
65 static void dlm_assert_master_worker(struct dlm_work_item
*item
, void *data
);
66 static int dlm_do_assert_master(struct dlm_ctxt
*dlm
,
67 struct dlm_lock_resource
*res
,
68 void *nodemap
, u32 flags
);
69 static void dlm_deref_lockres_worker(struct dlm_work_item
*item
, void *data
);
71 static inline void __dlm_mle_name(struct dlm_master_list_entry
*mle
,
72 unsigned char **name
, unsigned int *namelen
,
73 unsigned int *namehash
)
75 BUG_ON(mle
->type
!= DLM_MLE_BLOCK
&&
76 mle
->type
!= DLM_MLE_MASTER
&&
77 mle
->type
!= DLM_MLE_MIGRATION
);
79 if (mle
->type
!= DLM_MLE_MASTER
) {
80 *name
= mle
->u
.mlename
.name
;
81 *namelen
= mle
->u
.mlename
.len
;
83 *namehash
= mle
->u
.mlename
.hash
;
85 *name
= (unsigned char *)mle
->u
.mleres
->lockname
.name
;
86 *namelen
= mle
->u
.mleres
->lockname
.len
;
88 *namehash
= mle
->u
.mleres
->lockname
.hash
;
92 static inline int dlm_mle_equal(struct dlm_ctxt
*dlm
,
93 struct dlm_master_list_entry
*mle
,
97 unsigned char *mlename
;
103 __dlm_mle_name(mle
, &mlename
, &mlelen
, NULL
);
105 if (namelen
!= mlelen
|| memcmp(name
, mlename
, namelen
) != 0)
111 static struct kmem_cache
*dlm_lockres_cache
= NULL
;
112 static struct kmem_cache
*dlm_lockname_cache
= NULL
;
113 static struct kmem_cache
*dlm_mle_cache
= NULL
;
115 static void dlm_mle_release(struct kref
*kref
);
116 static void dlm_init_mle(struct dlm_master_list_entry
*mle
,
117 enum dlm_mle_type type
,
118 struct dlm_ctxt
*dlm
,
119 struct dlm_lock_resource
*res
,
121 unsigned int namelen
);
122 static void dlm_put_mle(struct dlm_master_list_entry
*mle
);
123 static void __dlm_put_mle(struct dlm_master_list_entry
*mle
);
124 static int dlm_find_mle(struct dlm_ctxt
*dlm
,
125 struct dlm_master_list_entry
**mle
,
126 char *name
, unsigned int namelen
);
128 static int dlm_do_master_request(struct dlm_lock_resource
*res
,
129 struct dlm_master_list_entry
*mle
, int to
);
132 static int dlm_wait_for_lock_mastery(struct dlm_ctxt
*dlm
,
133 struct dlm_lock_resource
*res
,
134 struct dlm_master_list_entry
*mle
,
136 static int dlm_restart_lock_mastery(struct dlm_ctxt
*dlm
,
137 struct dlm_lock_resource
*res
,
138 struct dlm_master_list_entry
*mle
,
140 static int dlm_add_migration_mle(struct dlm_ctxt
*dlm
,
141 struct dlm_lock_resource
*res
,
142 struct dlm_master_list_entry
*mle
,
143 struct dlm_master_list_entry
**oldmle
,
144 const char *name
, unsigned int namelen
,
145 u8 new_master
, u8 master
);
147 static u8
dlm_pick_migration_target(struct dlm_ctxt
*dlm
,
148 struct dlm_lock_resource
*res
);
149 static void dlm_remove_nonlocal_locks(struct dlm_ctxt
*dlm
,
150 struct dlm_lock_resource
*res
);
151 static int dlm_mark_lockres_migrating(struct dlm_ctxt
*dlm
,
152 struct dlm_lock_resource
*res
,
154 static int dlm_pre_master_reco_lockres(struct dlm_ctxt
*dlm
,
155 struct dlm_lock_resource
*res
);
158 int dlm_is_host_down(int errno
)
175 case -EINVAL
: /* if returned from our tcp code,
176 this means there is no socket */
184 * MASTER LIST FUNCTIONS
189 * regarding master list entries and heartbeat callbacks:
191 * in order to avoid sleeping and allocation that occurs in
192 * heartbeat, master list entries are simply attached to the
193 * dlm's established heartbeat callbacks. the mle is attached
194 * when it is created, and since the dlm->spinlock is held at
195 * that time, any heartbeat event will be properly discovered
196 * by the mle. the mle needs to be detached from the
197 * dlm->mle_hb_events list as soon as heartbeat events are no
198 * longer useful to the mle, and before the mle is freed.
200 * as a general rule, heartbeat events are no longer needed by
201 * the mle once an "answer" regarding the lock master has been
204 static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt
*dlm
,
205 struct dlm_master_list_entry
*mle
)
207 assert_spin_locked(&dlm
->spinlock
);
209 list_add_tail(&mle
->hb_events
, &dlm
->mle_hb_events
);
213 static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt
*dlm
,
214 struct dlm_master_list_entry
*mle
)
216 if (!list_empty(&mle
->hb_events
))
217 list_del_init(&mle
->hb_events
);
221 static inline void dlm_mle_detach_hb_events(struct dlm_ctxt
*dlm
,
222 struct dlm_master_list_entry
*mle
)
224 spin_lock(&dlm
->spinlock
);
225 __dlm_mle_detach_hb_events(dlm
, mle
);
226 spin_unlock(&dlm
->spinlock
);
229 static void dlm_get_mle_inuse(struct dlm_master_list_entry
*mle
)
231 struct dlm_ctxt
*dlm
;
234 assert_spin_locked(&dlm
->spinlock
);
235 assert_spin_locked(&dlm
->master_lock
);
237 kref_get(&mle
->mle_refs
);
240 static void dlm_put_mle_inuse(struct dlm_master_list_entry
*mle
)
242 struct dlm_ctxt
*dlm
;
245 spin_lock(&dlm
->spinlock
);
246 spin_lock(&dlm
->master_lock
);
249 spin_unlock(&dlm
->master_lock
);
250 spin_unlock(&dlm
->spinlock
);
254 /* remove from list and free */
255 static void __dlm_put_mle(struct dlm_master_list_entry
*mle
)
257 struct dlm_ctxt
*dlm
;
260 assert_spin_locked(&dlm
->spinlock
);
261 assert_spin_locked(&dlm
->master_lock
);
262 if (!atomic_read(&mle
->mle_refs
.refcount
)) {
263 /* this may or may not crash, but who cares.
265 mlog(ML_ERROR
, "bad mle: %p\n", mle
);
266 dlm_print_one_mle(mle
);
269 kref_put(&mle
->mle_refs
, dlm_mle_release
);
273 /* must not have any spinlocks coming in */
274 static void dlm_put_mle(struct dlm_master_list_entry
*mle
)
276 struct dlm_ctxt
*dlm
;
279 spin_lock(&dlm
->spinlock
);
280 spin_lock(&dlm
->master_lock
);
282 spin_unlock(&dlm
->master_lock
);
283 spin_unlock(&dlm
->spinlock
);
286 static inline void dlm_get_mle(struct dlm_master_list_entry
*mle
)
288 kref_get(&mle
->mle_refs
);
291 static void dlm_init_mle(struct dlm_master_list_entry
*mle
,
292 enum dlm_mle_type type
,
293 struct dlm_ctxt
*dlm
,
294 struct dlm_lock_resource
*res
,
296 unsigned int namelen
)
298 assert_spin_locked(&dlm
->spinlock
);
302 INIT_HLIST_NODE(&mle
->master_hash_node
);
303 INIT_LIST_HEAD(&mle
->hb_events
);
304 memset(mle
->maybe_map
, 0, sizeof(mle
->maybe_map
));
305 spin_lock_init(&mle
->spinlock
);
306 init_waitqueue_head(&mle
->wq
);
307 atomic_set(&mle
->woken
, 0);
308 kref_init(&mle
->mle_refs
);
309 memset(mle
->response_map
, 0, sizeof(mle
->response_map
));
310 mle
->master
= O2NM_MAX_NODES
;
311 mle
->new_master
= O2NM_MAX_NODES
;
314 BUG_ON(mle
->type
!= DLM_MLE_BLOCK
&&
315 mle
->type
!= DLM_MLE_MASTER
&&
316 mle
->type
!= DLM_MLE_MIGRATION
);
318 if (mle
->type
== DLM_MLE_MASTER
) {
323 memcpy(mle
->u
.mlename
.name
, name
, namelen
);
324 mle
->u
.mlename
.len
= namelen
;
325 mle
->u
.mlename
.hash
= dlm_lockid_hash(name
, namelen
);
328 /* copy off the node_map and register hb callbacks on our copy */
329 memcpy(mle
->node_map
, dlm
->domain_map
, sizeof(mle
->node_map
));
330 memcpy(mle
->vote_map
, dlm
->domain_map
, sizeof(mle
->vote_map
));
331 clear_bit(dlm
->node_num
, mle
->vote_map
);
332 clear_bit(dlm
->node_num
, mle
->node_map
);
334 /* attach the mle to the domain node up/down events */
335 __dlm_mle_attach_hb_events(dlm
, mle
);
338 void __dlm_unlink_mle(struct dlm_ctxt
*dlm
, struct dlm_master_list_entry
*mle
)
340 assert_spin_locked(&dlm
->spinlock
);
341 assert_spin_locked(&dlm
->master_lock
);
343 if (!hlist_unhashed(&mle
->master_hash_node
))
344 hlist_del_init(&mle
->master_hash_node
);
347 void __dlm_insert_mle(struct dlm_ctxt
*dlm
, struct dlm_master_list_entry
*mle
)
349 struct hlist_head
*bucket
;
350 unsigned char *mname
;
351 unsigned int mlen
, hash
;
353 assert_spin_locked(&dlm
->master_lock
);
355 __dlm_mle_name(mle
, &mname
, &mlen
, &hash
);
356 bucket
= dlm_master_hash(dlm
, hash
);
357 hlist_add_head(&mle
->master_hash_node
, bucket
);
360 /* returns 1 if found, 0 if not */
361 static int dlm_find_mle(struct dlm_ctxt
*dlm
,
362 struct dlm_master_list_entry
**mle
,
363 char *name
, unsigned int namelen
)
365 struct dlm_master_list_entry
*tmpmle
;
366 struct hlist_head
*bucket
;
367 struct hlist_node
*list
;
370 assert_spin_locked(&dlm
->master_lock
);
372 hash
= dlm_lockid_hash(name
, namelen
);
373 bucket
= dlm_master_hash(dlm
, hash
);
374 hlist_for_each(list
, bucket
) {
375 tmpmle
= hlist_entry(list
, struct dlm_master_list_entry
,
377 if (!dlm_mle_equal(dlm
, tmpmle
, name
, namelen
))
386 void dlm_hb_event_notify_attached(struct dlm_ctxt
*dlm
, int idx
, int node_up
)
388 struct dlm_master_list_entry
*mle
;
390 assert_spin_locked(&dlm
->spinlock
);
392 list_for_each_entry(mle
, &dlm
->mle_hb_events
, hb_events
) {
394 dlm_mle_node_up(dlm
, mle
, NULL
, idx
);
396 dlm_mle_node_down(dlm
, mle
, NULL
, idx
);
400 static void dlm_mle_node_down(struct dlm_ctxt
*dlm
,
401 struct dlm_master_list_entry
*mle
,
402 struct o2nm_node
*node
, int idx
)
404 spin_lock(&mle
->spinlock
);
406 if (!test_bit(idx
, mle
->node_map
))
407 mlog(0, "node %u already removed from nodemap!\n", idx
);
409 clear_bit(idx
, mle
->node_map
);
411 spin_unlock(&mle
->spinlock
);
414 static void dlm_mle_node_up(struct dlm_ctxt
*dlm
,
415 struct dlm_master_list_entry
*mle
,
416 struct o2nm_node
*node
, int idx
)
418 spin_lock(&mle
->spinlock
);
420 if (test_bit(idx
, mle
->node_map
))
421 mlog(0, "node %u already in node map!\n", idx
);
423 set_bit(idx
, mle
->node_map
);
425 spin_unlock(&mle
->spinlock
);
429 int dlm_init_mle_cache(void)
431 dlm_mle_cache
= kmem_cache_create("o2dlm_mle",
432 sizeof(struct dlm_master_list_entry
),
433 0, SLAB_HWCACHE_ALIGN
,
435 if (dlm_mle_cache
== NULL
)
440 void dlm_destroy_mle_cache(void)
443 kmem_cache_destroy(dlm_mle_cache
);
446 static void dlm_mle_release(struct kref
*kref
)
448 struct dlm_master_list_entry
*mle
;
449 struct dlm_ctxt
*dlm
;
450 unsigned char *mname
;
455 mle
= container_of(kref
, struct dlm_master_list_entry
, mle_refs
);
458 assert_spin_locked(&dlm
->spinlock
);
459 assert_spin_locked(&dlm
->master_lock
);
461 __dlm_mle_name(mle
, &mname
, &mlen
, NULL
);
462 mlog(0, "Releasing mle for %.*s, type %d\n", mlen
, mname
, mle
->type
);
464 /* remove from list if not already */
465 __dlm_unlink_mle(dlm
, mle
);
467 /* detach the mle from the domain node up/down events */
468 __dlm_mle_detach_hb_events(dlm
, mle
);
470 /* NOTE: kfree under spinlock here.
471 * if this is bad, we can move this to a freelist. */
472 kmem_cache_free(dlm_mle_cache
, mle
);
477 * LOCK RESOURCE FUNCTIONS
480 int dlm_init_master_caches(void)
482 dlm_lockres_cache
= kmem_cache_create("o2dlm_lockres",
483 sizeof(struct dlm_lock_resource
),
484 0, SLAB_HWCACHE_ALIGN
, NULL
);
485 if (!dlm_lockres_cache
)
488 dlm_lockname_cache
= kmem_cache_create("o2dlm_lockname",
489 DLM_LOCKID_NAME_MAX
, 0,
490 SLAB_HWCACHE_ALIGN
, NULL
);
491 if (!dlm_lockname_cache
)
496 dlm_destroy_master_caches();
500 void dlm_destroy_master_caches(void)
502 if (dlm_lockname_cache
)
503 kmem_cache_destroy(dlm_lockname_cache
);
505 if (dlm_lockres_cache
)
506 kmem_cache_destroy(dlm_lockres_cache
);
509 static void dlm_set_lockres_owner(struct dlm_ctxt
*dlm
,
510 struct dlm_lock_resource
*res
,
513 assert_spin_locked(&res
->spinlock
);
515 mlog_entry("%.*s, %u\n", res
->lockname
.len
, res
->lockname
.name
, owner
);
517 if (owner
== dlm
->node_num
)
518 atomic_inc(&dlm
->local_resources
);
519 else if (owner
== DLM_LOCK_RES_OWNER_UNKNOWN
)
520 atomic_inc(&dlm
->unknown_resources
);
522 atomic_inc(&dlm
->remote_resources
);
527 void dlm_change_lockres_owner(struct dlm_ctxt
*dlm
,
528 struct dlm_lock_resource
*res
, u8 owner
)
530 assert_spin_locked(&res
->spinlock
);
532 if (owner
== res
->owner
)
535 if (res
->owner
== dlm
->node_num
)
536 atomic_dec(&dlm
->local_resources
);
537 else if (res
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
)
538 atomic_dec(&dlm
->unknown_resources
);
540 atomic_dec(&dlm
->remote_resources
);
542 dlm_set_lockres_owner(dlm
, res
, owner
);
546 static void dlm_lockres_release(struct kref
*kref
)
548 struct dlm_lock_resource
*res
;
549 struct dlm_ctxt
*dlm
;
551 res
= container_of(kref
, struct dlm_lock_resource
, refs
);
554 /* This should not happen -- all lockres' have a name
555 * associated with them at init time. */
556 BUG_ON(!res
->lockname
.name
);
558 mlog(0, "destroying lockres %.*s\n", res
->lockname
.len
,
561 spin_lock(&dlm
->track_lock
);
562 if (!list_empty(&res
->tracking
))
563 list_del_init(&res
->tracking
);
565 mlog(ML_ERROR
, "Resource %.*s not on the Tracking list\n",
566 res
->lockname
.len
, res
->lockname
.name
);
567 dlm_print_one_lock_resource(res
);
569 spin_unlock(&dlm
->track_lock
);
573 if (!hlist_unhashed(&res
->hash_node
) ||
574 !list_empty(&res
->granted
) ||
575 !list_empty(&res
->converting
) ||
576 !list_empty(&res
->blocked
) ||
577 !list_empty(&res
->dirty
) ||
578 !list_empty(&res
->recovering
) ||
579 !list_empty(&res
->purge
)) {
581 "Going to BUG for resource %.*s."
582 " We're on a list! [%c%c%c%c%c%c%c]\n",
583 res
->lockname
.len
, res
->lockname
.name
,
584 !hlist_unhashed(&res
->hash_node
) ? 'H' : ' ',
585 !list_empty(&res
->granted
) ? 'G' : ' ',
586 !list_empty(&res
->converting
) ? 'C' : ' ',
587 !list_empty(&res
->blocked
) ? 'B' : ' ',
588 !list_empty(&res
->dirty
) ? 'D' : ' ',
589 !list_empty(&res
->recovering
) ? 'R' : ' ',
590 !list_empty(&res
->purge
) ? 'P' : ' ');
592 dlm_print_one_lock_resource(res
);
595 /* By the time we're ready to blow this guy away, we shouldn't
596 * be on any lists. */
597 BUG_ON(!hlist_unhashed(&res
->hash_node
));
598 BUG_ON(!list_empty(&res
->granted
));
599 BUG_ON(!list_empty(&res
->converting
));
600 BUG_ON(!list_empty(&res
->blocked
));
601 BUG_ON(!list_empty(&res
->dirty
));
602 BUG_ON(!list_empty(&res
->recovering
));
603 BUG_ON(!list_empty(&res
->purge
));
605 kmem_cache_free(dlm_lockname_cache
, (void *)res
->lockname
.name
);
607 kmem_cache_free(dlm_lockres_cache
, res
);
610 void dlm_lockres_put(struct dlm_lock_resource
*res
)
612 kref_put(&res
->refs
, dlm_lockres_release
);
615 static void dlm_init_lockres(struct dlm_ctxt
*dlm
,
616 struct dlm_lock_resource
*res
,
617 const char *name
, unsigned int namelen
)
621 /* If we memset here, we lose our reference to the kmalloc'd
622 * res->lockname.name, so be sure to init every field
625 qname
= (char *) res
->lockname
.name
;
626 memcpy(qname
, name
, namelen
);
628 res
->lockname
.len
= namelen
;
629 res
->lockname
.hash
= dlm_lockid_hash(name
, namelen
);
631 init_waitqueue_head(&res
->wq
);
632 spin_lock_init(&res
->spinlock
);
633 INIT_HLIST_NODE(&res
->hash_node
);
634 INIT_LIST_HEAD(&res
->granted
);
635 INIT_LIST_HEAD(&res
->converting
);
636 INIT_LIST_HEAD(&res
->blocked
);
637 INIT_LIST_HEAD(&res
->dirty
);
638 INIT_LIST_HEAD(&res
->recovering
);
639 INIT_LIST_HEAD(&res
->purge
);
640 INIT_LIST_HEAD(&res
->tracking
);
641 atomic_set(&res
->asts_reserved
, 0);
642 res
->migration_pending
= 0;
643 res
->inflight_locks
= 0;
645 /* put in dlm_lockres_release */
649 kref_init(&res
->refs
);
651 /* just for consistency */
652 spin_lock(&res
->spinlock
);
653 dlm_set_lockres_owner(dlm
, res
, DLM_LOCK_RES_OWNER_UNKNOWN
);
654 spin_unlock(&res
->spinlock
);
656 res
->state
= DLM_LOCK_RES_IN_PROGRESS
;
660 spin_lock(&dlm
->spinlock
);
661 list_add_tail(&res
->tracking
, &dlm
->tracking_list
);
662 spin_unlock(&dlm
->spinlock
);
664 memset(res
->lvb
, 0, DLM_LVB_LEN
);
665 memset(res
->refmap
, 0, sizeof(res
->refmap
));
668 struct dlm_lock_resource
*dlm_new_lockres(struct dlm_ctxt
*dlm
,
670 unsigned int namelen
)
672 struct dlm_lock_resource
*res
= NULL
;
674 res
= (struct dlm_lock_resource
*)
675 kmem_cache_zalloc(dlm_lockres_cache
, GFP_NOFS
);
679 res
->lockname
.name
= (char *)
680 kmem_cache_zalloc(dlm_lockname_cache
, GFP_NOFS
);
681 if (!res
->lockname
.name
)
684 dlm_init_lockres(dlm
, res
, name
, namelen
);
688 if (res
&& res
->lockname
.name
)
689 kmem_cache_free(dlm_lockname_cache
, (void *)res
->lockname
.name
);
692 kmem_cache_free(dlm_lockres_cache
, res
);
696 void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt
*dlm
,
697 struct dlm_lock_resource
*res
,
703 assert_spin_locked(&res
->spinlock
);
705 if (!test_bit(dlm
->node_num
, res
->refmap
)) {
706 BUG_ON(res
->inflight_locks
!= 0);
707 dlm_lockres_set_refmap_bit(dlm
->node_num
, res
);
709 res
->inflight_locks
++;
710 mlog(0, "%s:%.*s: inflight++: now %u\n",
711 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
,
712 res
->inflight_locks
);
715 void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt
*dlm
,
716 struct dlm_lock_resource
*res
,
720 assert_spin_locked(&res
->spinlock
);
722 BUG_ON(res
->inflight_locks
== 0);
723 res
->inflight_locks
--;
724 mlog(0, "%s:%.*s: inflight--: now %u\n",
725 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
,
726 res
->inflight_locks
);
727 if (res
->inflight_locks
== 0)
728 dlm_lockres_clear_refmap_bit(dlm
->node_num
, res
);
733 * lookup a lock resource by name.
734 * may already exist in the hashtable.
735 * lockid is null terminated
737 * if not, allocate enough for the lockres and for
738 * the temporary structure used in doing the mastering.
740 * also, do a lookup in the dlm->master_list to see
741 * if another node has begun mastering the same lock.
742 * if so, there should be a block entry in there
743 * for this name, and we should *not* attempt to master
744 * the lock here. need to wait around for that node
745 * to assert_master (or die).
748 struct dlm_lock_resource
* dlm_get_lock_resource(struct dlm_ctxt
*dlm
,
753 struct dlm_lock_resource
*tmpres
=NULL
, *res
=NULL
;
754 struct dlm_master_list_entry
*mle
= NULL
;
755 struct dlm_master_list_entry
*alloc_mle
= NULL
;
758 struct dlm_node_iter iter
;
761 int bit
, wait_on_recovery
= 0;
762 int drop_inflight_if_nonlocal
= 0;
766 hash
= dlm_lockid_hash(lockid
, namelen
);
768 mlog(0, "get lockres %s (len %d)\n", lockid
, namelen
);
771 spin_lock(&dlm
->spinlock
);
772 tmpres
= __dlm_lookup_lockres_full(dlm
, lockid
, namelen
, hash
);
774 int dropping_ref
= 0;
776 spin_unlock(&dlm
->spinlock
);
778 spin_lock(&tmpres
->spinlock
);
779 /* We wait for the other thread that is mastering the resource */
780 if (tmpres
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
781 __dlm_wait_on_lockres(tmpres
);
782 BUG_ON(tmpres
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
);
785 if (tmpres
->owner
== dlm
->node_num
) {
786 BUG_ON(tmpres
->state
& DLM_LOCK_RES_DROPPING_REF
);
787 dlm_lockres_grab_inflight_ref(dlm
, tmpres
);
788 } else if (tmpres
->state
& DLM_LOCK_RES_DROPPING_REF
)
790 spin_unlock(&tmpres
->spinlock
);
792 /* wait until done messaging the master, drop our ref to allow
793 * the lockres to be purged, start over. */
795 spin_lock(&tmpres
->spinlock
);
796 __dlm_wait_on_lockres_flags(tmpres
, DLM_LOCK_RES_DROPPING_REF
);
797 spin_unlock(&tmpres
->spinlock
);
798 dlm_lockres_put(tmpres
);
803 mlog(0, "found in hash!\n");
805 dlm_lockres_put(res
);
811 spin_unlock(&dlm
->spinlock
);
812 mlog(0, "allocating a new resource\n");
813 /* nothing found and we need to allocate one. */
814 alloc_mle
= (struct dlm_master_list_entry
*)
815 kmem_cache_alloc(dlm_mle_cache
, GFP_NOFS
);
818 res
= dlm_new_lockres(dlm
, lockid
, namelen
);
824 mlog(0, "no lockres found, allocated our own: %p\n", res
);
826 if (flags
& LKM_LOCAL
) {
827 /* caller knows it's safe to assume it's not mastered elsewhere
828 * DONE! return right away */
829 spin_lock(&res
->spinlock
);
830 dlm_change_lockres_owner(dlm
, res
, dlm
->node_num
);
831 __dlm_insert_lockres(dlm
, res
);
832 dlm_lockres_grab_inflight_ref(dlm
, res
);
833 spin_unlock(&res
->spinlock
);
834 spin_unlock(&dlm
->spinlock
);
835 /* lockres still marked IN_PROGRESS */
839 /* check master list to see if another node has started mastering it */
840 spin_lock(&dlm
->master_lock
);
842 /* if we found a block, wait for lock to be mastered by another node */
843 blocked
= dlm_find_mle(dlm
, &mle
, (char *)lockid
, namelen
);
846 if (mle
->type
== DLM_MLE_MASTER
) {
847 mlog(ML_ERROR
, "master entry for nonexistent lock!\n");
850 mig
= (mle
->type
== DLM_MLE_MIGRATION
);
851 /* if there is a migration in progress, let the migration
852 * finish before continuing. we can wait for the absence
853 * of the MIGRATION mle: either the migrate finished or
854 * one of the nodes died and the mle was cleaned up.
855 * if there is a BLOCK here, but it already has a master
856 * set, we are too late. the master does not have a ref
857 * for us in the refmap. detach the mle and drop it.
858 * either way, go back to the top and start over. */
859 if (mig
|| mle
->master
!= O2NM_MAX_NODES
) {
860 BUG_ON(mig
&& mle
->master
== dlm
->node_num
);
861 /* we arrived too late. the master does not
862 * have a ref for us. retry. */
863 mlog(0, "%s:%.*s: late on %s\n",
864 dlm
->name
, namelen
, lockid
,
865 mig
? "MIGRATION" : "BLOCK");
866 spin_unlock(&dlm
->master_lock
);
867 spin_unlock(&dlm
->spinlock
);
869 /* master is known, detach */
871 dlm_mle_detach_hb_events(dlm
, mle
);
874 /* this is lame, but we cant wait on either
875 * the mle or lockres waitqueue here */
881 /* go ahead and try to master lock on this node */
883 /* make sure this does not get freed below */
885 dlm_init_mle(mle
, DLM_MLE_MASTER
, dlm
, res
, NULL
, 0);
886 set_bit(dlm
->node_num
, mle
->maybe_map
);
887 __dlm_insert_mle(dlm
, mle
);
889 /* still holding the dlm spinlock, check the recovery map
890 * to see if there are any nodes that still need to be
891 * considered. these will not appear in the mle nodemap
892 * but they might own this lockres. wait on them. */
893 bit
= find_next_bit(dlm
->recovery_map
, O2NM_MAX_NODES
, 0);
894 if (bit
< O2NM_MAX_NODES
) {
895 mlog(ML_NOTICE
, "%s:%.*s: at least one node (%d) to "
896 "recover before lock mastery can begin\n",
897 dlm
->name
, namelen
, (char *)lockid
, bit
);
898 wait_on_recovery
= 1;
902 /* at this point there is either a DLM_MLE_BLOCK or a
903 * DLM_MLE_MASTER on the master list, so it's safe to add the
904 * lockres to the hashtable. anyone who finds the lock will
905 * still have to wait on the IN_PROGRESS. */
907 /* finally add the lockres to its hash bucket */
908 __dlm_insert_lockres(dlm
, res
);
909 /* since this lockres is new it doesnt not require the spinlock */
910 dlm_lockres_grab_inflight_ref_new(dlm
, res
);
912 /* if this node does not become the master make sure to drop
913 * this inflight reference below */
914 drop_inflight_if_nonlocal
= 1;
916 /* get an extra ref on the mle in case this is a BLOCK
917 * if so, the creator of the BLOCK may try to put the last
918 * ref at this time in the assert master handler, so we
919 * need an extra one to keep from a bad ptr deref. */
920 dlm_get_mle_inuse(mle
);
921 spin_unlock(&dlm
->master_lock
);
922 spin_unlock(&dlm
->spinlock
);
925 while (wait_on_recovery
) {
926 /* any cluster changes that occurred after dropping the
927 * dlm spinlock would be detectable be a change on the mle,
928 * so we only need to clear out the recovery map once. */
929 if (dlm_is_recovery_lock(lockid
, namelen
)) {
930 mlog(ML_NOTICE
, "%s: recovery map is not empty, but "
931 "must master $RECOVERY lock now\n", dlm
->name
);
932 if (!dlm_pre_master_reco_lockres(dlm
, res
))
933 wait_on_recovery
= 0;
935 mlog(0, "%s: waiting 500ms for heartbeat state "
936 "change\n", dlm
->name
);
942 dlm_kick_recovery_thread(dlm
);
944 dlm_wait_for_recovery(dlm
);
946 spin_lock(&dlm
->spinlock
);
947 bit
= find_next_bit(dlm
->recovery_map
, O2NM_MAX_NODES
, 0);
948 if (bit
< O2NM_MAX_NODES
) {
949 mlog(ML_NOTICE
, "%s:%.*s: at least one node (%d) to "
950 "recover before lock mastery can begin\n",
951 dlm
->name
, namelen
, (char *)lockid
, bit
);
952 wait_on_recovery
= 1;
954 wait_on_recovery
= 0;
955 spin_unlock(&dlm
->spinlock
);
957 if (wait_on_recovery
)
958 dlm_wait_for_node_recovery(dlm
, bit
, 10000);
961 /* must wait for lock to be mastered elsewhere */
966 dlm_node_iter_init(mle
->vote_map
, &iter
);
967 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
968 ret
= dlm_do_master_request(res
, mle
, nodenum
);
971 if (mle
->master
!= O2NM_MAX_NODES
) {
972 /* found a master ! */
973 if (mle
->master
<= nodenum
)
975 /* if our master request has not reached the master
976 * yet, keep going until it does. this is how the
977 * master will know that asserts are needed back to
978 * the lower nodes. */
979 mlog(0, "%s:%.*s: requests only up to %u but master "
980 "is %u, keep going\n", dlm
->name
, namelen
,
981 lockid
, nodenum
, mle
->master
);
986 /* keep going until the response map includes all nodes */
987 ret
= dlm_wait_for_lock_mastery(dlm
, res
, mle
, &blocked
);
989 wait_on_recovery
= 1;
990 mlog(0, "%s:%.*s: node map changed, redo the "
991 "master request now, blocked=%d\n",
992 dlm
->name
, res
->lockname
.len
,
993 res
->lockname
.name
, blocked
);
995 mlog(ML_ERROR
, "%s:%.*s: spinning on "
996 "dlm_wait_for_lock_mastery, blocked=%d\n",
997 dlm
->name
, res
->lockname
.len
,
998 res
->lockname
.name
, blocked
);
999 dlm_print_one_lock_resource(res
);
1000 dlm_print_one_mle(mle
);
1006 mlog(0, "lockres mastered by %u\n", res
->owner
);
1007 /* make sure we never continue without this */
1008 BUG_ON(res
->owner
== O2NM_MAX_NODES
);
1010 /* master is known, detach if not already detached */
1011 dlm_mle_detach_hb_events(dlm
, mle
);
1013 /* put the extra ref */
1014 dlm_put_mle_inuse(mle
);
1017 spin_lock(&res
->spinlock
);
1018 if (res
->owner
!= dlm
->node_num
&& drop_inflight_if_nonlocal
)
1019 dlm_lockres_drop_inflight_ref(dlm
, res
);
1020 res
->state
&= ~DLM_LOCK_RES_IN_PROGRESS
;
1021 spin_unlock(&res
->spinlock
);
1025 /* need to free the unused mle */
1027 kmem_cache_free(dlm_mle_cache
, alloc_mle
);
1033 #define DLM_MASTERY_TIMEOUT_MS 5000
1035 static int dlm_wait_for_lock_mastery(struct dlm_ctxt
*dlm
,
1036 struct dlm_lock_resource
*res
,
1037 struct dlm_master_list_entry
*mle
,
1042 int map_changed
, voting_done
;
1049 /* check if another node has already become the owner */
1050 spin_lock(&res
->spinlock
);
1051 if (res
->owner
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1052 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm
->name
,
1053 res
->lockname
.len
, res
->lockname
.name
, res
->owner
);
1054 spin_unlock(&res
->spinlock
);
1055 /* this will cause the master to re-assert across
1056 * the whole cluster, freeing up mles */
1057 if (res
->owner
!= dlm
->node_num
) {
1058 ret
= dlm_do_master_request(res
, mle
, res
->owner
);
1060 /* give recovery a chance to run */
1061 mlog(ML_ERROR
, "link to %u went down?: %d\n", res
->owner
, ret
);
1069 spin_unlock(&res
->spinlock
);
1071 spin_lock(&mle
->spinlock
);
1073 map_changed
= (memcmp(mle
->vote_map
, mle
->node_map
,
1074 sizeof(mle
->vote_map
)) != 0);
1075 voting_done
= (memcmp(mle
->vote_map
, mle
->response_map
,
1076 sizeof(mle
->vote_map
)) == 0);
1078 /* restart if we hit any errors */
1081 mlog(0, "%s: %.*s: node map changed, restarting\n",
1082 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
1083 ret
= dlm_restart_lock_mastery(dlm
, res
, mle
, *blocked
);
1084 b
= (mle
->type
== DLM_MLE_BLOCK
);
1085 if ((*blocked
&& !b
) || (!*blocked
&& b
)) {
1086 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1087 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
,
1091 spin_unlock(&mle
->spinlock
);
1096 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1097 "rechecking now\n", dlm
->name
, res
->lockname
.len
,
1098 res
->lockname
.name
);
1102 mlog(0, "map not changed and voting not done "
1103 "for %s:%.*s\n", dlm
->name
, res
->lockname
.len
,
1104 res
->lockname
.name
);
1108 if (m
!= O2NM_MAX_NODES
) {
1109 /* another node has done an assert!
1114 /* have all nodes responded? */
1115 if (voting_done
&& !*blocked
) {
1116 bit
= find_next_bit(mle
->maybe_map
, O2NM_MAX_NODES
, 0);
1117 if (dlm
->node_num
<= bit
) {
1118 /* my node number is lowest.
1119 * now tell other nodes that I am
1120 * mastering this. */
1121 mle
->master
= dlm
->node_num
;
1122 /* ref was grabbed in get_lock_resource
1123 * will be dropped in dlmlock_master */
1127 /* if voting is done, but we have not received
1128 * an assert master yet, we must sleep */
1132 spin_unlock(&mle
->spinlock
);
1134 /* sleep if we haven't finished voting yet */
1136 unsigned long timeo
= msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS
);
1139 if (atomic_read(&mle->mle_refs.refcount) < 2)
1140 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1141 atomic_read(&mle->mle_refs.refcount),
1142 res->lockname.len, res->lockname.name);
1144 atomic_set(&mle
->woken
, 0);
1145 (void)wait_event_timeout(mle
->wq
,
1146 (atomic_read(&mle
->woken
) == 1),
1148 if (res
->owner
== O2NM_MAX_NODES
) {
1149 mlog(0, "%s:%.*s: waiting again\n", dlm
->name
,
1150 res
->lockname
.len
, res
->lockname
.name
);
1153 mlog(0, "done waiting, master is %u\n", res
->owner
);
1161 mlog(0, "about to master %.*s here, this=%u\n",
1162 res
->lockname
.len
, res
->lockname
.name
, m
);
1163 ret
= dlm_do_assert_master(dlm
, res
, mle
->vote_map
, 0);
1165 /* This is a failure in the network path,
1166 * not in the response to the assert_master
1167 * (any nonzero response is a BUG on this node).
1168 * Most likely a socket just got disconnected
1169 * due to node death. */
1172 /* no longer need to restart lock mastery.
1173 * all living nodes have been contacted. */
1177 /* set the lockres owner */
1178 spin_lock(&res
->spinlock
);
1179 /* mastery reference obtained either during
1180 * assert_master_handler or in get_lock_resource */
1181 dlm_change_lockres_owner(dlm
, res
, m
);
1182 spin_unlock(&res
->spinlock
);
1188 struct dlm_bitmap_diff_iter
1191 unsigned long *orig_bm
;
1192 unsigned long *cur_bm
;
1193 unsigned long diff_bm
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1196 enum dlm_node_state_change
1203 static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter
*iter
,
1204 unsigned long *orig_bm
,
1205 unsigned long *cur_bm
)
1207 unsigned long p1
, p2
;
1211 iter
->orig_bm
= orig_bm
;
1212 iter
->cur_bm
= cur_bm
;
1214 for (i
= 0; i
< BITS_TO_LONGS(O2NM_MAX_NODES
); i
++) {
1215 p1
= *(iter
->orig_bm
+ i
);
1216 p2
= *(iter
->cur_bm
+ i
);
1217 iter
->diff_bm
[i
] = (p1
& ~p2
) | (p2
& ~p1
);
1221 static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter
*iter
,
1222 enum dlm_node_state_change
*state
)
1226 if (iter
->curnode
>= O2NM_MAX_NODES
)
1229 bit
= find_next_bit(iter
->diff_bm
, O2NM_MAX_NODES
,
1231 if (bit
>= O2NM_MAX_NODES
) {
1232 iter
->curnode
= O2NM_MAX_NODES
;
1236 /* if it was there in the original then this node died */
1237 if (test_bit(bit
, iter
->orig_bm
))
1242 iter
->curnode
= bit
;
1247 static int dlm_restart_lock_mastery(struct dlm_ctxt
*dlm
,
1248 struct dlm_lock_resource
*res
,
1249 struct dlm_master_list_entry
*mle
,
1252 struct dlm_bitmap_diff_iter bdi
;
1253 enum dlm_node_state_change sc
;
1257 mlog(0, "something happened such that the "
1258 "master process may need to be restarted!\n");
1260 assert_spin_locked(&mle
->spinlock
);
1262 dlm_bitmap_diff_iter_init(&bdi
, mle
->vote_map
, mle
->node_map
);
1263 node
= dlm_bitmap_diff_iter_next(&bdi
, &sc
);
1265 if (sc
== NODE_UP
) {
1266 /* a node came up. clear any old vote from
1267 * the response map and set it in the vote map
1268 * then restart the mastery. */
1269 mlog(ML_NOTICE
, "node %d up while restarting\n", node
);
1271 /* redo the master request, but only for the new node */
1272 mlog(0, "sending request to new node\n");
1273 clear_bit(node
, mle
->response_map
);
1274 set_bit(node
, mle
->vote_map
);
1276 mlog(ML_ERROR
, "node down! %d\n", node
);
1278 int lowest
= find_next_bit(mle
->maybe_map
,
1281 /* act like it was never there */
1282 clear_bit(node
, mle
->maybe_map
);
1284 if (node
== lowest
) {
1285 mlog(0, "expected master %u died"
1286 " while this node was blocked "
1287 "waiting on it!\n", node
);
1288 lowest
= find_next_bit(mle
->maybe_map
,
1291 if (lowest
< O2NM_MAX_NODES
) {
1292 mlog(0, "%s:%.*s:still "
1293 "blocked. waiting on %u "
1299 /* mle is an MLE_BLOCK, but
1300 * there is now nothing left to
1301 * block on. we need to return
1302 * all the way back out and try
1303 * again with an MLE_MASTER.
1304 * dlm_do_local_recovery_cleanup
1305 * has already run, so the mle
1307 mlog(0, "%s:%.*s: no "
1308 "longer blocking. try to "
1309 "master this here\n",
1312 res
->lockname
.name
);
1313 mle
->type
= DLM_MLE_MASTER
;
1314 mle
->u
.mleres
= res
;
1319 /* now blank out everything, as if we had never
1320 * contacted anyone */
1321 memset(mle
->maybe_map
, 0, sizeof(mle
->maybe_map
));
1322 memset(mle
->response_map
, 0, sizeof(mle
->response_map
));
1323 /* reset the vote_map to the current node_map */
1324 memcpy(mle
->vote_map
, mle
->node_map
,
1325 sizeof(mle
->node_map
));
1326 /* put myself into the maybe map */
1327 if (mle
->type
!= DLM_MLE_BLOCK
)
1328 set_bit(dlm
->node_num
, mle
->maybe_map
);
1331 node
= dlm_bitmap_diff_iter_next(&bdi
, &sc
);
1338 * DLM_MASTER_REQUEST_MSG
1340 * returns: 0 on success,
1341 * -errno on a network error
1343 * on error, the caller should assume the target node is "dead"
1347 static int dlm_do_master_request(struct dlm_lock_resource
*res
,
1348 struct dlm_master_list_entry
*mle
, int to
)
1350 struct dlm_ctxt
*dlm
= mle
->dlm
;
1351 struct dlm_master_request request
;
1352 int ret
, response
=0, resend
;
1353 unsigned char *mlename
;
1354 unsigned int mlenamelen
;
1356 memset(&request
, 0, sizeof(request
));
1357 request
.node_idx
= dlm
->node_num
;
1359 BUG_ON(mle
->type
== DLM_MLE_MIGRATION
);
1361 __dlm_mle_name(mle
, &mlename
, &mlenamelen
, NULL
);
1363 request
.namelen
= (u8
)mlenamelen
;
1364 memcpy(request
.name
, mlename
, request
.namelen
);
1367 ret
= o2net_send_message(DLM_MASTER_REQUEST_MSG
, dlm
->key
, &request
,
1368 sizeof(request
), to
, &response
);
1370 if (ret
== -ESRCH
) {
1371 /* should never happen */
1372 mlog(ML_ERROR
, "TCP stack not ready!\n");
1374 } else if (ret
== -EINVAL
) {
1375 mlog(ML_ERROR
, "bad args passed to o2net!\n");
1377 } else if (ret
== -ENOMEM
) {
1378 mlog(ML_ERROR
, "out of memory while trying to send "
1379 "network message! retrying\n");
1380 /* this is totally crude */
1383 } else if (!dlm_is_host_down(ret
)) {
1384 /* not a network error. bad. */
1386 mlog(ML_ERROR
, "unhandled error!");
1389 /* all other errors should be network errors,
1390 * and likely indicate node death */
1391 mlog(ML_ERROR
, "link to %d went down!\n", to
);
1397 spin_lock(&mle
->spinlock
);
1399 case DLM_MASTER_RESP_YES
:
1400 set_bit(to
, mle
->response_map
);
1401 mlog(0, "node %u is the master, response=YES\n", to
);
1402 mlog(0, "%s:%.*s: master node %u now knows I have a "
1403 "reference\n", dlm
->name
, res
->lockname
.len
,
1404 res
->lockname
.name
, to
);
1407 case DLM_MASTER_RESP_NO
:
1408 mlog(0, "node %u not master, response=NO\n", to
);
1409 set_bit(to
, mle
->response_map
);
1411 case DLM_MASTER_RESP_MAYBE
:
1412 mlog(0, "node %u not master, response=MAYBE\n", to
);
1413 set_bit(to
, mle
->response_map
);
1414 set_bit(to
, mle
->maybe_map
);
1416 case DLM_MASTER_RESP_ERROR
:
1417 mlog(0, "node %u hit an error, resending\n", to
);
1422 mlog(ML_ERROR
, "bad response! %u\n", response
);
1425 spin_unlock(&mle
->spinlock
);
1427 /* this is also totally crude */
1437 * locks that can be taken here:
1443 * if possible, TRIM THIS DOWN!!!
1445 int dlm_master_request_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
1448 u8 response
= DLM_MASTER_RESP_MAYBE
;
1449 struct dlm_ctxt
*dlm
= data
;
1450 struct dlm_lock_resource
*res
= NULL
;
1451 struct dlm_master_request
*request
= (struct dlm_master_request
*) msg
->buf
;
1452 struct dlm_master_list_entry
*mle
= NULL
, *tmpmle
= NULL
;
1454 unsigned int namelen
, hash
;
1457 int dispatch_assert
= 0;
1460 return DLM_MASTER_RESP_NO
;
1462 if (!dlm_domain_fully_joined(dlm
)) {
1463 response
= DLM_MASTER_RESP_NO
;
1467 name
= request
->name
;
1468 namelen
= request
->namelen
;
1469 hash
= dlm_lockid_hash(name
, namelen
);
1471 if (namelen
> DLM_LOCKID_NAME_MAX
) {
1472 response
= DLM_IVBUFLEN
;
1477 spin_lock(&dlm
->spinlock
);
1478 res
= __dlm_lookup_lockres(dlm
, name
, namelen
, hash
);
1480 spin_unlock(&dlm
->spinlock
);
1482 /* take care of the easy cases up front */
1483 spin_lock(&res
->spinlock
);
1484 if (res
->state
& (DLM_LOCK_RES_RECOVERING
|
1485 DLM_LOCK_RES_MIGRATING
)) {
1486 spin_unlock(&res
->spinlock
);
1487 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
1488 "being recovered/migrated\n");
1489 response
= DLM_MASTER_RESP_ERROR
;
1491 kmem_cache_free(dlm_mle_cache
, mle
);
1495 if (res
->owner
== dlm
->node_num
) {
1496 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1497 dlm
->name
, namelen
, name
, request
->node_idx
);
1498 dlm_lockres_set_refmap_bit(request
->node_idx
, res
);
1499 spin_unlock(&res
->spinlock
);
1500 response
= DLM_MASTER_RESP_YES
;
1502 kmem_cache_free(dlm_mle_cache
, mle
);
1504 /* this node is the owner.
1505 * there is some extra work that needs to
1506 * happen now. the requesting node has
1507 * caused all nodes up to this one to
1508 * create mles. this node now needs to
1509 * go back and clean those up. */
1510 dispatch_assert
= 1;
1512 } else if (res
->owner
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1513 spin_unlock(&res
->spinlock
);
1514 // mlog(0, "node %u is the master\n", res->owner);
1515 response
= DLM_MASTER_RESP_NO
;
1517 kmem_cache_free(dlm_mle_cache
, mle
);
1521 /* ok, there is no owner. either this node is
1522 * being blocked, or it is actively trying to
1523 * master this lock. */
1524 if (!(res
->state
& DLM_LOCK_RES_IN_PROGRESS
)) {
1525 mlog(ML_ERROR
, "lock with no owner should be "
1530 // mlog(0, "lockres is in progress...\n");
1531 spin_lock(&dlm
->master_lock
);
1532 found
= dlm_find_mle(dlm
, &tmpmle
, name
, namelen
);
1534 mlog(ML_ERROR
, "no mle found for this lock!\n");
1538 spin_lock(&tmpmle
->spinlock
);
1539 if (tmpmle
->type
== DLM_MLE_BLOCK
) {
1540 // mlog(0, "this node is waiting for "
1541 // "lockres to be mastered\n");
1542 response
= DLM_MASTER_RESP_NO
;
1543 } else if (tmpmle
->type
== DLM_MLE_MIGRATION
) {
1544 mlog(0, "node %u is master, but trying to migrate to "
1545 "node %u.\n", tmpmle
->master
, tmpmle
->new_master
);
1546 if (tmpmle
->master
== dlm
->node_num
) {
1547 mlog(ML_ERROR
, "no owner on lockres, but this "
1548 "node is trying to migrate it to %u?!\n",
1549 tmpmle
->new_master
);
1552 /* the real master can respond on its own */
1553 response
= DLM_MASTER_RESP_NO
;
1555 } else if (tmpmle
->master
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1557 if (tmpmle
->master
== dlm
->node_num
) {
1558 response
= DLM_MASTER_RESP_YES
;
1559 /* this node will be the owner.
1560 * go back and clean the mles on any
1562 dispatch_assert
= 1;
1563 dlm_lockres_set_refmap_bit(request
->node_idx
, res
);
1564 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1565 dlm
->name
, namelen
, name
,
1568 response
= DLM_MASTER_RESP_NO
;
1570 // mlog(0, "this node is attempting to "
1571 // "master lockres\n");
1572 response
= DLM_MASTER_RESP_MAYBE
;
1575 set_bit(request
->node_idx
, tmpmle
->maybe_map
);
1576 spin_unlock(&tmpmle
->spinlock
);
1578 spin_unlock(&dlm
->master_lock
);
1579 spin_unlock(&res
->spinlock
);
1581 /* keep the mle attached to heartbeat events */
1582 dlm_put_mle(tmpmle
);
1584 kmem_cache_free(dlm_mle_cache
, mle
);
1589 * lockres doesn't exist on this node
1590 * if there is an MLE_BLOCK, return NO
1591 * if there is an MLE_MASTER, return MAYBE
1592 * otherwise, add an MLE_BLOCK, return NO
1594 spin_lock(&dlm
->master_lock
);
1595 found
= dlm_find_mle(dlm
, &tmpmle
, name
, namelen
);
1597 /* this lockid has never been seen on this node yet */
1598 // mlog(0, "no mle found\n");
1600 spin_unlock(&dlm
->master_lock
);
1601 spin_unlock(&dlm
->spinlock
);
1603 mle
= (struct dlm_master_list_entry
*)
1604 kmem_cache_alloc(dlm_mle_cache
, GFP_NOFS
);
1606 response
= DLM_MASTER_RESP_ERROR
;
1607 mlog_errno(-ENOMEM
);
1613 // mlog(0, "this is second time thru, already allocated, "
1614 // "add the block.\n");
1615 dlm_init_mle(mle
, DLM_MLE_BLOCK
, dlm
, NULL
, name
, namelen
);
1616 set_bit(request
->node_idx
, mle
->maybe_map
);
1617 __dlm_insert_mle(dlm
, mle
);
1618 response
= DLM_MASTER_RESP_NO
;
1620 // mlog(0, "mle was found\n");
1622 spin_lock(&tmpmle
->spinlock
);
1623 if (tmpmle
->master
== dlm
->node_num
) {
1624 mlog(ML_ERROR
, "no lockres, but an mle with this node as master!\n");
1627 if (tmpmle
->type
== DLM_MLE_BLOCK
)
1628 response
= DLM_MASTER_RESP_NO
;
1629 else if (tmpmle
->type
== DLM_MLE_MIGRATION
) {
1630 mlog(0, "migration mle was found (%u->%u)\n",
1631 tmpmle
->master
, tmpmle
->new_master
);
1632 /* real master can respond on its own */
1633 response
= DLM_MASTER_RESP_NO
;
1635 response
= DLM_MASTER_RESP_MAYBE
;
1637 set_bit(request
->node_idx
, tmpmle
->maybe_map
);
1638 spin_unlock(&tmpmle
->spinlock
);
1640 spin_unlock(&dlm
->master_lock
);
1641 spin_unlock(&dlm
->spinlock
);
1644 /* keep the mle attached to heartbeat events */
1645 dlm_put_mle(tmpmle
);
1649 * __dlm_lookup_lockres() grabbed a reference to this lockres.
1650 * The reference is released by dlm_assert_master_worker() under
1651 * the call to dlm_dispatch_assert_master(). If
1652 * dlm_assert_master_worker() isn't called, we drop it here.
1654 if (dispatch_assert
) {
1655 if (response
!= DLM_MASTER_RESP_YES
)
1656 mlog(ML_ERROR
, "invalid response %d\n", response
);
1658 mlog(ML_ERROR
, "bad lockres while trying to assert!\n");
1661 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1662 dlm
->node_num
, res
->lockname
.len
, res
->lockname
.name
);
1663 ret
= dlm_dispatch_assert_master(dlm
, res
, 0, request
->node_idx
,
1664 DLM_ASSERT_MASTER_MLE_CLEANUP
);
1666 mlog(ML_ERROR
, "failed to dispatch assert master work\n");
1667 response
= DLM_MASTER_RESP_ERROR
;
1668 dlm_lockres_put(res
);
1672 dlm_lockres_put(res
);
1680 * DLM_ASSERT_MASTER_MSG
1685 * NOTE: this can be used for debugging
1686 * can periodically run all locks owned by this node
1687 * and re-assert across the cluster...
1689 static int dlm_do_assert_master(struct dlm_ctxt
*dlm
,
1690 struct dlm_lock_resource
*res
,
1691 void *nodemap
, u32 flags
)
1693 struct dlm_assert_master
assert;
1695 struct dlm_node_iter iter
;
1698 const char *lockname
= res
->lockname
.name
;
1699 unsigned int namelen
= res
->lockname
.len
;
1701 BUG_ON(namelen
> O2NM_MAX_NAME_LEN
);
1703 spin_lock(&res
->spinlock
);
1704 res
->state
|= DLM_LOCK_RES_SETREF_INPROG
;
1705 spin_unlock(&res
->spinlock
);
1710 /* note that if this nodemap is empty, it returns 0 */
1711 dlm_node_iter_init(nodemap
, &iter
);
1712 while ((to
= dlm_node_iter_next(&iter
)) >= 0) {
1714 struct dlm_master_list_entry
*mle
= NULL
;
1716 mlog(0, "sending assert master to %d (%.*s)\n", to
,
1718 memset(&assert, 0, sizeof(assert));
1719 assert.node_idx
= dlm
->node_num
;
1720 assert.namelen
= namelen
;
1721 memcpy(assert.name
, lockname
, namelen
);
1722 assert.flags
= cpu_to_be32(flags
);
1724 tmpret
= o2net_send_message(DLM_ASSERT_MASTER_MSG
, dlm
->key
,
1725 &assert, sizeof(assert), to
, &r
);
1727 mlog(0, "assert_master returned %d!\n", tmpret
);
1728 if (!dlm_is_host_down(tmpret
)) {
1729 mlog(ML_ERROR
, "unhandled error=%d!\n", tmpret
);
1732 /* a node died. finish out the rest of the nodes. */
1733 mlog(0, "link to %d went down!\n", to
);
1734 /* any nonzero status return will do */
1738 /* ok, something horribly messed. kill thyself. */
1739 mlog(ML_ERROR
,"during assert master of %.*s to %u, "
1740 "got %d.\n", namelen
, lockname
, to
, r
);
1741 spin_lock(&dlm
->spinlock
);
1742 spin_lock(&dlm
->master_lock
);
1743 if (dlm_find_mle(dlm
, &mle
, (char *)lockname
,
1745 dlm_print_one_mle(mle
);
1748 spin_unlock(&dlm
->master_lock
);
1749 spin_unlock(&dlm
->spinlock
);
1753 if (r
& DLM_ASSERT_RESPONSE_REASSERT
&&
1754 !(r
& DLM_ASSERT_RESPONSE_MASTERY_REF
)) {
1755 mlog(ML_ERROR
, "%.*s: very strange, "
1756 "master MLE but no lockres on %u\n",
1757 namelen
, lockname
, to
);
1760 if (r
& DLM_ASSERT_RESPONSE_REASSERT
) {
1761 mlog(0, "%.*s: node %u create mles on other "
1762 "nodes and requests a re-assert\n",
1763 namelen
, lockname
, to
);
1766 if (r
& DLM_ASSERT_RESPONSE_MASTERY_REF
) {
1767 mlog(0, "%.*s: node %u has a reference to this "
1768 "lockres, set the bit in the refmap\n",
1769 namelen
, lockname
, to
);
1770 spin_lock(&res
->spinlock
);
1771 dlm_lockres_set_refmap_bit(to
, res
);
1772 spin_unlock(&res
->spinlock
);
1779 spin_lock(&res
->spinlock
);
1780 res
->state
&= ~DLM_LOCK_RES_SETREF_INPROG
;
1781 spin_unlock(&res
->spinlock
);
1788 * locks that can be taken here:
1794 * if possible, TRIM THIS DOWN!!!
1796 int dlm_assert_master_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
1799 struct dlm_ctxt
*dlm
= data
;
1800 struct dlm_master_list_entry
*mle
= NULL
;
1801 struct dlm_assert_master
*assert = (struct dlm_assert_master
*)msg
->buf
;
1802 struct dlm_lock_resource
*res
= NULL
;
1804 unsigned int namelen
, hash
;
1806 int master_request
= 0, have_lockres_ref
= 0;
1812 name
= assert->name
;
1813 namelen
= assert->namelen
;
1814 hash
= dlm_lockid_hash(name
, namelen
);
1815 flags
= be32_to_cpu(assert->flags
);
1817 if (namelen
> DLM_LOCKID_NAME_MAX
) {
1818 mlog(ML_ERROR
, "Invalid name length!");
1822 spin_lock(&dlm
->spinlock
);
1825 mlog(0, "assert_master with flags: %u\n", flags
);
1828 spin_lock(&dlm
->master_lock
);
1829 if (!dlm_find_mle(dlm
, &mle
, name
, namelen
)) {
1830 /* not an error, could be master just re-asserting */
1831 mlog(0, "just got an assert_master from %u, but no "
1832 "MLE for it! (%.*s)\n", assert->node_idx
,
1835 int bit
= find_next_bit (mle
->maybe_map
, O2NM_MAX_NODES
, 0);
1836 if (bit
>= O2NM_MAX_NODES
) {
1837 /* not necessarily an error, though less likely.
1838 * could be master just re-asserting. */
1839 mlog(0, "no bits set in the maybe_map, but %u "
1840 "is asserting! (%.*s)\n", assert->node_idx
,
1842 } else if (bit
!= assert->node_idx
) {
1843 if (flags
& DLM_ASSERT_MASTER_MLE_CLEANUP
) {
1844 mlog(0, "master %u was found, %u should "
1845 "back off\n", assert->node_idx
, bit
);
1847 /* with the fix for bug 569, a higher node
1848 * number winning the mastery will respond
1849 * YES to mastery requests, but this node
1850 * had no way of knowing. let it pass. */
1851 mlog(0, "%u is the lowest node, "
1852 "%u is asserting. (%.*s) %u must "
1853 "have begun after %u won.\n", bit
,
1854 assert->node_idx
, namelen
, name
, bit
,
1858 if (mle
->type
== DLM_MLE_MIGRATION
) {
1859 if (flags
& DLM_ASSERT_MASTER_MLE_CLEANUP
) {
1860 mlog(0, "%s:%.*s: got cleanup assert"
1861 " from %u for migration\n",
1862 dlm
->name
, namelen
, name
,
1864 } else if (!(flags
& DLM_ASSERT_MASTER_FINISH_MIGRATION
)) {
1865 mlog(0, "%s:%.*s: got unrelated assert"
1866 " from %u for migration, ignoring\n",
1867 dlm
->name
, namelen
, name
,
1870 spin_unlock(&dlm
->master_lock
);
1871 spin_unlock(&dlm
->spinlock
);
1876 spin_unlock(&dlm
->master_lock
);
1878 /* ok everything checks out with the MLE
1879 * now check to see if there is a lockres */
1880 res
= __dlm_lookup_lockres(dlm
, name
, namelen
, hash
);
1882 spin_lock(&res
->spinlock
);
1883 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
1884 mlog(ML_ERROR
, "%u asserting but %.*s is "
1885 "RECOVERING!\n", assert->node_idx
, namelen
, name
);
1889 if (res
->owner
!= DLM_LOCK_RES_OWNER_UNKNOWN
&&
1890 res
->owner
!= assert->node_idx
) {
1891 mlog(ML_ERROR
, "DIE! Mastery assert from %u, "
1892 "but current owner is %u! (%.*s)\n",
1893 assert->node_idx
, res
->owner
, namelen
,
1895 __dlm_print_one_lock_resource(res
);
1898 } else if (mle
->type
!= DLM_MLE_MIGRATION
) {
1899 if (res
->owner
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
1900 /* owner is just re-asserting */
1901 if (res
->owner
== assert->node_idx
) {
1902 mlog(0, "owner %u re-asserting on "
1903 "lock %.*s\n", assert->node_idx
,
1907 mlog(ML_ERROR
, "got assert_master from "
1908 "node %u, but %u is the owner! "
1909 "(%.*s)\n", assert->node_idx
,
1910 res
->owner
, namelen
, name
);
1913 if (!(res
->state
& DLM_LOCK_RES_IN_PROGRESS
)) {
1914 mlog(ML_ERROR
, "got assert from %u, but lock "
1915 "with no owner should be "
1916 "in-progress! (%.*s)\n",
1921 } else /* mle->type == DLM_MLE_MIGRATION */ {
1922 /* should only be getting an assert from new master */
1923 if (assert->node_idx
!= mle
->new_master
) {
1924 mlog(ML_ERROR
, "got assert from %u, but "
1925 "new master is %u, and old master "
1927 assert->node_idx
, mle
->new_master
,
1928 mle
->master
, namelen
, name
);
1934 spin_unlock(&res
->spinlock
);
1936 spin_unlock(&dlm
->spinlock
);
1938 // mlog(0, "woo! got an assert_master from node %u!\n",
1939 // assert->node_idx);
1945 spin_lock(&mle
->spinlock
);
1946 if (mle
->type
== DLM_MLE_BLOCK
|| mle
->type
== DLM_MLE_MIGRATION
)
1949 /* MASTER mle: if any bits set in the response map
1950 * then the calling node needs to re-assert to clear
1951 * up nodes that this node contacted */
1952 while ((nn
= find_next_bit (mle
->response_map
, O2NM_MAX_NODES
,
1953 nn
+1)) < O2NM_MAX_NODES
) {
1954 if (nn
!= dlm
->node_num
&& nn
!= assert->node_idx
)
1958 mle
->master
= assert->node_idx
;
1959 atomic_set(&mle
->woken
, 1);
1961 spin_unlock(&mle
->spinlock
);
1965 spin_lock(&res
->spinlock
);
1966 if (mle
->type
== DLM_MLE_MIGRATION
) {
1967 mlog(0, "finishing off migration of lockres %.*s, "
1969 res
->lockname
.len
, res
->lockname
.name
,
1970 dlm
->node_num
, mle
->new_master
);
1971 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
1973 dlm_change_lockres_owner(dlm
, res
, mle
->new_master
);
1974 BUG_ON(res
->state
& DLM_LOCK_RES_DIRTY
);
1976 dlm_change_lockres_owner(dlm
, res
, mle
->master
);
1978 spin_unlock(&res
->spinlock
);
1979 have_lockres_ref
= 1;
1984 /* master is known, detach if not already detached.
1985 * ensures that only one assert_master call will happen
1987 spin_lock(&dlm
->spinlock
);
1988 spin_lock(&dlm
->master_lock
);
1990 rr
= atomic_read(&mle
->mle_refs
.refcount
);
1991 if (mle
->inuse
> 0) {
1992 if (extra_ref
&& rr
< 3)
1994 else if (!extra_ref
&& rr
< 2)
1997 if (extra_ref
&& rr
< 2)
1999 else if (!extra_ref
&& rr
< 1)
2003 mlog(ML_ERROR
, "%s:%.*s: got assert master from %u "
2004 "that will mess up this node, refs=%d, extra=%d, "
2005 "inuse=%d\n", dlm
->name
, namelen
, name
,
2006 assert->node_idx
, rr
, extra_ref
, mle
->inuse
);
2007 dlm_print_one_mle(mle
);
2009 __dlm_unlink_mle(dlm
, mle
);
2010 __dlm_mle_detach_hb_events(dlm
, mle
);
2013 /* the assert master message now balances the extra
2014 * ref given by the master / migration request message.
2015 * if this is the last put, it will be removed
2019 spin_unlock(&dlm
->master_lock
);
2020 spin_unlock(&dlm
->spinlock
);
2022 if (res
->owner
!= assert->node_idx
) {
2023 mlog(0, "assert_master from %u, but current "
2024 "owner is %u (%.*s), no mle\n", assert->node_idx
,
2025 res
->owner
, namelen
, name
);
2032 spin_lock(&res
->spinlock
);
2033 res
->state
|= DLM_LOCK_RES_SETREF_INPROG
;
2034 spin_unlock(&res
->spinlock
);
2035 *ret_data
= (void *)res
;
2038 if (master_request
) {
2039 mlog(0, "need to tell master to reassert\n");
2040 /* positive. negative would shoot down the node. */
2041 ret
|= DLM_ASSERT_RESPONSE_REASSERT
;
2042 if (!have_lockres_ref
) {
2043 mlog(ML_ERROR
, "strange, got assert from %u, MASTER "
2044 "mle present here for %s:%.*s, but no lockres!\n",
2045 assert->node_idx
, dlm
->name
, namelen
, name
);
2048 if (have_lockres_ref
) {
2049 /* let the master know we have a reference to the lockres */
2050 ret
|= DLM_ASSERT_RESPONSE_MASTERY_REF
;
2051 mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
2052 dlm
->name
, namelen
, name
, assert->node_idx
);
2057 /* kill the caller! */
2058 mlog(ML_ERROR
, "Bad message received from another node. Dumping state "
2059 "and killing the other node now! This node is OK and can continue.\n");
2060 __dlm_print_one_lock_resource(res
);
2061 spin_unlock(&res
->spinlock
);
2062 spin_unlock(&dlm
->spinlock
);
2063 *ret_data
= (void *)res
;
2068 void dlm_assert_master_post_handler(int status
, void *data
, void *ret_data
)
2070 struct dlm_lock_resource
*res
= (struct dlm_lock_resource
*)ret_data
;
2073 spin_lock(&res
->spinlock
);
2074 res
->state
&= ~DLM_LOCK_RES_SETREF_INPROG
;
2075 spin_unlock(&res
->spinlock
);
2077 dlm_lockres_put(res
);
2082 int dlm_dispatch_assert_master(struct dlm_ctxt
*dlm
,
2083 struct dlm_lock_resource
*res
,
2084 int ignore_higher
, u8 request_from
, u32 flags
)
2086 struct dlm_work_item
*item
;
2087 item
= kzalloc(sizeof(*item
), GFP_NOFS
);
2092 /* queue up work for dlm_assert_master_worker */
2093 dlm_grab(dlm
); /* get an extra ref for the work item */
2094 dlm_init_work_item(dlm
, item
, dlm_assert_master_worker
, NULL
);
2095 item
->u
.am
.lockres
= res
; /* already have a ref */
2096 /* can optionally ignore node numbers higher than this node */
2097 item
->u
.am
.ignore_higher
= ignore_higher
;
2098 item
->u
.am
.request_from
= request_from
;
2099 item
->u
.am
.flags
= flags
;
2102 mlog(0, "IGNORE HIGHER: %.*s\n", res
->lockname
.len
,
2103 res
->lockname
.name
);
2105 spin_lock(&dlm
->work_lock
);
2106 list_add_tail(&item
->list
, &dlm
->work_list
);
2107 spin_unlock(&dlm
->work_lock
);
2109 queue_work(dlm
->dlm_worker
, &dlm
->dispatched_work
);
2113 static void dlm_assert_master_worker(struct dlm_work_item
*item
, void *data
)
2115 struct dlm_ctxt
*dlm
= data
;
2117 struct dlm_lock_resource
*res
;
2118 unsigned long nodemap
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
2125 res
= item
->u
.am
.lockres
;
2126 ignore_higher
= item
->u
.am
.ignore_higher
;
2127 request_from
= item
->u
.am
.request_from
;
2128 flags
= item
->u
.am
.flags
;
2130 spin_lock(&dlm
->spinlock
);
2131 memcpy(nodemap
, dlm
->domain_map
, sizeof(nodemap
));
2132 spin_unlock(&dlm
->spinlock
);
2134 clear_bit(dlm
->node_num
, nodemap
);
2135 if (ignore_higher
) {
2136 /* if is this just to clear up mles for nodes below
2137 * this node, do not send the message to the original
2138 * caller or any node number higher than this */
2139 clear_bit(request_from
, nodemap
);
2140 bit
= dlm
->node_num
;
2142 bit
= find_next_bit(nodemap
, O2NM_MAX_NODES
,
2144 if (bit
>= O2NM_MAX_NODES
)
2146 clear_bit(bit
, nodemap
);
2151 * If we're migrating this lock to someone else, we are no
2152 * longer allowed to assert out own mastery. OTOH, we need to
2153 * prevent migration from starting while we're still asserting
2154 * our dominance. The reserved ast delays migration.
2156 spin_lock(&res
->spinlock
);
2157 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
2158 mlog(0, "Someone asked us to assert mastery, but we're "
2159 "in the middle of migration. Skipping assert, "
2160 "the new master will handle that.\n");
2161 spin_unlock(&res
->spinlock
);
2164 __dlm_lockres_reserve_ast(res
);
2165 spin_unlock(&res
->spinlock
);
2167 /* this call now finishes out the nodemap
2168 * even if one or more nodes die */
2169 mlog(0, "worker about to master %.*s here, this=%u\n",
2170 res
->lockname
.len
, res
->lockname
.name
, dlm
->node_num
);
2171 ret
= dlm_do_assert_master(dlm
, res
, nodemap
, flags
);
2173 /* no need to restart, we are done */
2174 if (!dlm_is_host_down(ret
))
2178 /* Ok, we've asserted ourselves. Let's let migration start. */
2179 dlm_lockres_release_ast(dlm
, res
);
2182 dlm_lockres_put(res
);
2184 mlog(0, "finished with dlm_assert_master_worker\n");
2187 /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2188 * We cannot wait for node recovery to complete to begin mastering this
2189 * lockres because this lockres is used to kick off recovery! ;-)
2190 * So, do a pre-check on all living nodes to see if any of those nodes
2191 * think that $RECOVERY is currently mastered by a dead node. If so,
2192 * we wait a short time to allow that node to get notified by its own
2193 * heartbeat stack, then check again. All $RECOVERY lock resources
2194 * mastered by dead nodes are purged when the hearbeat callback is
2195 * fired, so we can know for sure that it is safe to continue once
2196 * the node returns a live node or no node. */
2197 static int dlm_pre_master_reco_lockres(struct dlm_ctxt
*dlm
,
2198 struct dlm_lock_resource
*res
)
2200 struct dlm_node_iter iter
;
2203 u8 master
= DLM_LOCK_RES_OWNER_UNKNOWN
;
2205 spin_lock(&dlm
->spinlock
);
2206 dlm_node_iter_init(dlm
->domain_map
, &iter
);
2207 spin_unlock(&dlm
->spinlock
);
2209 while ((nodenum
= dlm_node_iter_next(&iter
)) >= 0) {
2210 /* do not send to self */
2211 if (nodenum
== dlm
->node_num
)
2213 ret
= dlm_do_master_requery(dlm
, res
, nodenum
, &master
);
2216 if (!dlm_is_host_down(ret
))
2218 /* host is down, so answer for that node would be
2219 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
2223 if (master
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
2224 /* check to see if this master is in the recovery map */
2225 spin_lock(&dlm
->spinlock
);
2226 if (test_bit(master
, dlm
->recovery_map
)) {
2227 mlog(ML_NOTICE
, "%s: node %u has not seen "
2228 "node %u go down yet, and thinks the "
2229 "dead node is mastering the recovery "
2230 "lock. must wait.\n", dlm
->name
,
2234 spin_unlock(&dlm
->spinlock
);
2235 mlog(0, "%s: reco lock master is %u\n", dlm
->name
,
2244 * DLM_DEREF_LOCKRES_MSG
2247 int dlm_drop_lockres_ref(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
)
2249 struct dlm_deref_lockres deref
;
2251 const char *lockname
;
2252 unsigned int namelen
;
2254 lockname
= res
->lockname
.name
;
2255 namelen
= res
->lockname
.len
;
2256 BUG_ON(namelen
> O2NM_MAX_NAME_LEN
);
2258 mlog(0, "%s:%.*s: sending deref to %d\n",
2259 dlm
->name
, namelen
, lockname
, res
->owner
);
2260 memset(&deref
, 0, sizeof(deref
));
2261 deref
.node_idx
= dlm
->node_num
;
2262 deref
.namelen
= namelen
;
2263 memcpy(deref
.name
, lockname
, namelen
);
2265 ret
= o2net_send_message(DLM_DEREF_LOCKRES_MSG
, dlm
->key
,
2266 &deref
, sizeof(deref
), res
->owner
, &r
);
2270 /* BAD. other node says I did not have a ref. */
2271 mlog(ML_ERROR
,"while dropping ref on %s:%.*s "
2272 "(master=%u) got %d.\n", dlm
->name
, namelen
,
2273 lockname
, res
->owner
, r
);
2274 dlm_print_one_lock_resource(res
);
2280 int dlm_deref_lockres_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
2283 struct dlm_ctxt
*dlm
= data
;
2284 struct dlm_deref_lockres
*deref
= (struct dlm_deref_lockres
*)msg
->buf
;
2285 struct dlm_lock_resource
*res
= NULL
;
2287 unsigned int namelen
;
2291 struct dlm_work_item
*item
;
2299 namelen
= deref
->namelen
;
2300 node
= deref
->node_idx
;
2302 if (namelen
> DLM_LOCKID_NAME_MAX
) {
2303 mlog(ML_ERROR
, "Invalid name length!");
2306 if (deref
->node_idx
>= O2NM_MAX_NODES
) {
2307 mlog(ML_ERROR
, "Invalid node number: %u\n", node
);
2311 hash
= dlm_lockid_hash(name
, namelen
);
2313 spin_lock(&dlm
->spinlock
);
2314 res
= __dlm_lookup_lockres_full(dlm
, name
, namelen
, hash
);
2316 spin_unlock(&dlm
->spinlock
);
2317 mlog(ML_ERROR
, "%s:%.*s: bad lockres name\n",
2318 dlm
->name
, namelen
, name
);
2321 spin_unlock(&dlm
->spinlock
);
2323 spin_lock(&res
->spinlock
);
2324 if (res
->state
& DLM_LOCK_RES_SETREF_INPROG
)
2327 BUG_ON(res
->state
& DLM_LOCK_RES_DROPPING_REF
);
2328 if (test_bit(node
, res
->refmap
)) {
2329 dlm_lockres_clear_refmap_bit(node
, res
);
2333 spin_unlock(&res
->spinlock
);
2337 dlm_lockres_calc_usage(dlm
, res
);
2339 mlog(ML_ERROR
, "%s:%.*s: node %u trying to drop ref "
2340 "but it is already dropped!\n", dlm
->name
,
2341 res
->lockname
.len
, res
->lockname
.name
, node
);
2342 dlm_print_one_lock_resource(res
);
2348 item
= kzalloc(sizeof(*item
), GFP_NOFS
);
2355 dlm_init_work_item(dlm
, item
, dlm_deref_lockres_worker
, NULL
);
2356 item
->u
.dl
.deref_res
= res
;
2357 item
->u
.dl
.deref_node
= node
;
2359 spin_lock(&dlm
->work_lock
);
2360 list_add_tail(&item
->list
, &dlm
->work_list
);
2361 spin_unlock(&dlm
->work_lock
);
2363 queue_work(dlm
->dlm_worker
, &dlm
->dispatched_work
);
2368 dlm_lockres_put(res
);
2374 static void dlm_deref_lockres_worker(struct dlm_work_item
*item
, void *data
)
2376 struct dlm_ctxt
*dlm
;
2377 struct dlm_lock_resource
*res
;
2382 res
= item
->u
.dl
.deref_res
;
2383 node
= item
->u
.dl
.deref_node
;
2385 spin_lock(&res
->spinlock
);
2386 BUG_ON(res
->state
& DLM_LOCK_RES_DROPPING_REF
);
2387 if (test_bit(node
, res
->refmap
)) {
2388 __dlm_wait_on_lockres_flags(res
, DLM_LOCK_RES_SETREF_INPROG
);
2389 dlm_lockres_clear_refmap_bit(node
, res
);
2392 spin_unlock(&res
->spinlock
);
2395 mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
2396 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
, node
);
2397 dlm_lockres_calc_usage(dlm
, res
);
2399 mlog(ML_ERROR
, "%s:%.*s: node %u trying to drop ref "
2400 "but it is already dropped!\n", dlm
->name
,
2401 res
->lockname
.len
, res
->lockname
.name
, node
);
2402 dlm_print_one_lock_resource(res
);
2405 dlm_lockres_put(res
);
2408 /* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
2409 * if not. If 0, numlocks is set to the number of locks in the lockres.
2411 static int dlm_is_lockres_migrateable(struct dlm_ctxt
*dlm
,
2412 struct dlm_lock_resource
*res
,
2418 struct list_head
*queue
;
2419 struct dlm_lock
*lock
;
2421 assert_spin_locked(&res
->spinlock
);
2424 if (res
->owner
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
2425 mlog(0, "cannot migrate lockres with unknown owner!\n");
2429 if (res
->owner
!= dlm
->node_num
) {
2430 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2435 queue
= &res
->granted
;
2436 for (i
= 0; i
< 3; i
++) {
2437 list_for_each_entry(lock
, queue
, list
) {
2439 if (lock
->ml
.node
== dlm
->node_num
) {
2440 mlog(0, "found a lock owned by this node still "
2441 "on the %s queue! will not migrate this "
2442 "lockres\n", (i
== 0 ? "granted" :
2443 (i
== 1 ? "converting" :
2453 mlog(0, "migrateable lockres having %d locks\n", *numlocks
);
2460 * DLM_MIGRATE_LOCKRES
2464 static int dlm_migrate_lockres(struct dlm_ctxt
*dlm
,
2465 struct dlm_lock_resource
*res
,
2468 struct dlm_master_list_entry
*mle
= NULL
;
2469 struct dlm_master_list_entry
*oldmle
= NULL
;
2470 struct dlm_migratable_lockres
*mres
= NULL
;
2473 unsigned int namelen
;
2481 name
= res
->lockname
.name
;
2482 namelen
= res
->lockname
.len
;
2484 mlog(0, "migrating %.*s to %u\n", namelen
, name
, target
);
2487 * ensure this lockres is a proper candidate for migration
2489 spin_lock(&res
->spinlock
);
2490 ret
= dlm_is_lockres_migrateable(dlm
, res
, &numlocks
);
2492 spin_unlock(&res
->spinlock
);
2495 spin_unlock(&res
->spinlock
);
2498 if (numlocks
== 0) {
2499 mlog(0, "no locks were found on this lockres! done!\n");
2504 * preallocate up front
2505 * if this fails, abort
2509 mres
= (struct dlm_migratable_lockres
*) __get_free_page(GFP_NOFS
);
2515 mle
= (struct dlm_master_list_entry
*) kmem_cache_alloc(dlm_mle_cache
,
2524 * find a node to migrate the lockres to
2527 mlog(0, "picking a migration node\n");
2528 spin_lock(&dlm
->spinlock
);
2529 /* pick a new node */
2530 if (!test_bit(target
, dlm
->domain_map
) ||
2531 target
>= O2NM_MAX_NODES
) {
2532 target
= dlm_pick_migration_target(dlm
, res
);
2534 mlog(0, "node %u chosen for migration\n", target
);
2536 if (target
>= O2NM_MAX_NODES
||
2537 !test_bit(target
, dlm
->domain_map
)) {
2538 /* target chosen is not alive */
2543 spin_unlock(&dlm
->spinlock
);
2547 mlog(0, "continuing with target = %u\n", target
);
2550 * clear any existing master requests and
2551 * add the migration mle to the list
2553 spin_lock(&dlm
->master_lock
);
2554 ret
= dlm_add_migration_mle(dlm
, res
, mle
, &oldmle
, name
,
2555 namelen
, target
, dlm
->node_num
);
2556 spin_unlock(&dlm
->master_lock
);
2557 spin_unlock(&dlm
->spinlock
);
2559 if (ret
== -EEXIST
) {
2560 mlog(0, "another process is already migrating it\n");
2566 * set the MIGRATING flag and flush asts
2567 * if we fail after this we need to re-dirty the lockres
2569 if (dlm_mark_lockres_migrating(dlm
, res
, target
) < 0) {
2570 mlog(ML_ERROR
, "tried to migrate %.*s to %u, but "
2571 "the target went down.\n", res
->lockname
.len
,
2572 res
->lockname
.name
, target
);
2573 spin_lock(&res
->spinlock
);
2574 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
2576 spin_unlock(&res
->spinlock
);
2582 /* master is known, detach if not already detached */
2583 dlm_mle_detach_hb_events(dlm
, oldmle
);
2584 dlm_put_mle(oldmle
);
2589 dlm_mle_detach_hb_events(dlm
, mle
);
2592 kmem_cache_free(dlm_mle_cache
, mle
);
2598 * at this point, we have a migration target, an mle
2599 * in the master list, and the MIGRATING flag set on
2603 /* now that remote nodes are spinning on the MIGRATING flag,
2604 * ensure that all assert_master work is flushed. */
2605 flush_workqueue(dlm
->dlm_worker
);
2607 /* get an extra reference on the mle.
2608 * otherwise the assert_master from the new
2609 * master will destroy this.
2610 * also, make sure that all callers of dlm_get_mle
2611 * take both dlm->spinlock and dlm->master_lock */
2612 spin_lock(&dlm
->spinlock
);
2613 spin_lock(&dlm
->master_lock
);
2614 dlm_get_mle_inuse(mle
);
2615 spin_unlock(&dlm
->master_lock
);
2616 spin_unlock(&dlm
->spinlock
);
2618 /* notify new node and send all lock state */
2619 /* call send_one_lockres with migration flag.
2620 * this serves as notice to the target node that a
2621 * migration is starting. */
2622 ret
= dlm_send_one_lockres(dlm
, res
, mres
, target
,
2623 DLM_MRES_MIGRATION
);
2626 mlog(0, "migration to node %u failed with %d\n",
2628 /* migration failed, detach and clean up mle */
2629 dlm_mle_detach_hb_events(dlm
, mle
);
2631 dlm_put_mle_inuse(mle
);
2632 spin_lock(&res
->spinlock
);
2633 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
2635 spin_unlock(&res
->spinlock
);
2639 /* at this point, the target sends a message to all nodes,
2640 * (using dlm_do_migrate_request). this node is skipped since
2641 * we had to put an mle in the list to begin the process. this
2642 * node now waits for target to do an assert master. this node
2643 * will be the last one notified, ensuring that the migration
2644 * is complete everywhere. if the target dies while this is
2645 * going on, some nodes could potentially see the target as the
2646 * master, so it is important that my recovery finds the migration
2647 * mle and sets the master to UNKNONWN. */
2650 /* wait for new node to assert master */
2652 ret
= wait_event_interruptible_timeout(mle
->wq
,
2653 (atomic_read(&mle
->woken
) == 1),
2654 msecs_to_jiffies(5000));
2657 if (atomic_read(&mle
->woken
) == 1 ||
2658 res
->owner
== target
)
2661 mlog(0, "%s:%.*s: timed out during migration\n",
2662 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
2663 /* avoid hang during shutdown when migrating lockres
2664 * to a node which also goes down */
2665 if (dlm_is_node_dead(dlm
, target
)) {
2666 mlog(0, "%s:%.*s: expected migration "
2667 "target %u is no longer up, restarting\n",
2668 dlm
->name
, res
->lockname
.len
,
2669 res
->lockname
.name
, target
);
2671 /* migration failed, detach and clean up mle */
2672 dlm_mle_detach_hb_events(dlm
, mle
);
2674 dlm_put_mle_inuse(mle
);
2675 spin_lock(&res
->spinlock
);
2676 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
2678 spin_unlock(&res
->spinlock
);
2682 mlog(0, "%s:%.*s: caught signal during migration\n",
2683 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
2686 /* all done, set the owner, clear the flag */
2687 spin_lock(&res
->spinlock
);
2688 dlm_set_lockres_owner(dlm
, res
, target
);
2689 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
2690 dlm_remove_nonlocal_locks(dlm
, res
);
2691 spin_unlock(&res
->spinlock
);
2694 /* master is known, detach if not already detached */
2695 dlm_mle_detach_hb_events(dlm
, mle
);
2696 dlm_put_mle_inuse(mle
);
2699 dlm_lockres_calc_usage(dlm
, res
);
2702 /* re-dirty the lockres if we failed */
2704 dlm_kick_thread(dlm
, res
);
2706 /* wake up waiters if the MIGRATING flag got set
2707 * but migration failed */
2713 free_page((unsigned long)mres
);
2717 mlog(0, "returning %d\n", ret
);
2721 #define DLM_MIGRATION_RETRY_MS 100
2723 /* Should be called only after beginning the domain leave process.
2724 * There should not be any remaining locks on nonlocal lock resources,
2725 * and there should be no local locks left on locally mastered resources.
2727 * Called with the dlm spinlock held, may drop it to do migration, but
2728 * will re-acquire before exit.
2730 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2731 int dlm_empty_lockres(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
)
2734 int lock_dropped
= 0;
2737 spin_lock(&res
->spinlock
);
2738 if (res
->owner
!= dlm
->node_num
) {
2739 if (!__dlm_lockres_unused(res
)) {
2740 mlog(ML_ERROR
, "%s:%.*s: this node is not master, "
2741 "trying to free this but locks remain\n",
2742 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
);
2744 spin_unlock(&res
->spinlock
);
2748 /* No need to migrate a lockres having no locks */
2749 ret
= dlm_is_lockres_migrateable(dlm
, res
, &numlocks
);
2750 if (ret
>= 0 && numlocks
== 0) {
2751 spin_unlock(&res
->spinlock
);
2754 spin_unlock(&res
->spinlock
);
2756 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2757 spin_unlock(&dlm
->spinlock
);
2760 ret
= dlm_migrate_lockres(dlm
, res
, O2NM_MAX_NODES
);
2763 if (ret
== -ENOTEMPTY
) {
2764 mlog(ML_ERROR
, "lockres %.*s still has local locks!\n",
2765 res
->lockname
.len
, res
->lockname
.name
);
2769 mlog(0, "lockres %.*s: migrate failed, "
2770 "retrying\n", res
->lockname
.len
,
2771 res
->lockname
.name
);
2772 msleep(DLM_MIGRATION_RETRY_MS
);
2774 spin_lock(&dlm
->spinlock
);
2776 return lock_dropped
;
2779 int dlm_lock_basts_flushed(struct dlm_ctxt
*dlm
, struct dlm_lock
*lock
)
2782 spin_lock(&dlm
->ast_lock
);
2783 spin_lock(&lock
->spinlock
);
2784 ret
= (list_empty(&lock
->bast_list
) && !lock
->bast_pending
);
2785 spin_unlock(&lock
->spinlock
);
2786 spin_unlock(&dlm
->ast_lock
);
2790 static int dlm_migration_can_proceed(struct dlm_ctxt
*dlm
,
2791 struct dlm_lock_resource
*res
,
2795 spin_lock(&res
->spinlock
);
2796 can_proceed
= !!(res
->state
& DLM_LOCK_RES_MIGRATING
);
2797 spin_unlock(&res
->spinlock
);
2799 /* target has died, so make the caller break out of the
2800 * wait_event, but caller must recheck the domain_map */
2801 spin_lock(&dlm
->spinlock
);
2802 if (!test_bit(mig_target
, dlm
->domain_map
))
2804 spin_unlock(&dlm
->spinlock
);
2808 static int dlm_lockres_is_dirty(struct dlm_ctxt
*dlm
,
2809 struct dlm_lock_resource
*res
)
2812 spin_lock(&res
->spinlock
);
2813 ret
= !!(res
->state
& DLM_LOCK_RES_DIRTY
);
2814 spin_unlock(&res
->spinlock
);
2819 static int dlm_mark_lockres_migrating(struct dlm_ctxt
*dlm
,
2820 struct dlm_lock_resource
*res
,
2825 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2826 res
->lockname
.len
, res
->lockname
.name
, dlm
->node_num
,
2828 /* need to set MIGRATING flag on lockres. this is done by
2829 * ensuring that all asts have been flushed for this lockres. */
2830 spin_lock(&res
->spinlock
);
2831 BUG_ON(res
->migration_pending
);
2832 res
->migration_pending
= 1;
2833 /* strategy is to reserve an extra ast then release
2834 * it below, letting the release do all of the work */
2835 __dlm_lockres_reserve_ast(res
);
2836 spin_unlock(&res
->spinlock
);
2838 /* now flush all the pending asts */
2839 dlm_kick_thread(dlm
, res
);
2840 /* before waiting on DIRTY, block processes which may
2841 * try to dirty the lockres before MIGRATING is set */
2842 spin_lock(&res
->spinlock
);
2843 BUG_ON(res
->state
& DLM_LOCK_RES_BLOCK_DIRTY
);
2844 res
->state
|= DLM_LOCK_RES_BLOCK_DIRTY
;
2845 spin_unlock(&res
->spinlock
);
2846 /* now wait on any pending asts and the DIRTY state */
2847 wait_event(dlm
->ast_wq
, !dlm_lockres_is_dirty(dlm
, res
));
2848 dlm_lockres_release_ast(dlm
, res
);
2850 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2851 res
->state
& DLM_LOCK_RES_DIRTY
? "yes" : "no");
2852 /* if the extra ref we just put was the final one, this
2853 * will pass thru immediately. otherwise, we need to wait
2854 * for the last ast to finish. */
2856 ret
= wait_event_interruptible_timeout(dlm
->migration_wq
,
2857 dlm_migration_can_proceed(dlm
, res
, target
),
2858 msecs_to_jiffies(1000));
2860 mlog(0, "woken again: migrating? %s, dead? %s\n",
2861 res
->state
& DLM_LOCK_RES_MIGRATING
? "yes":"no",
2862 test_bit(target
, dlm
->domain_map
) ? "no":"yes");
2864 mlog(0, "all is well: migrating? %s, dead? %s\n",
2865 res
->state
& DLM_LOCK_RES_MIGRATING
? "yes":"no",
2866 test_bit(target
, dlm
->domain_map
) ? "no":"yes");
2868 if (!dlm_migration_can_proceed(dlm
, res
, target
)) {
2869 mlog(0, "trying again...\n");
2872 /* now that we are sure the MIGRATING state is there, drop
2873 * the unneded state which blocked threads trying to DIRTY */
2874 spin_lock(&res
->spinlock
);
2875 BUG_ON(!(res
->state
& DLM_LOCK_RES_BLOCK_DIRTY
));
2876 BUG_ON(!(res
->state
& DLM_LOCK_RES_MIGRATING
));
2877 res
->state
&= ~DLM_LOCK_RES_BLOCK_DIRTY
;
2878 spin_unlock(&res
->spinlock
);
2880 /* did the target go down or die? */
2881 spin_lock(&dlm
->spinlock
);
2882 if (!test_bit(target
, dlm
->domain_map
)) {
2883 mlog(ML_ERROR
, "aha. migration target %u just went down\n",
2887 spin_unlock(&dlm
->spinlock
);
2892 * o the DLM_LOCK_RES_MIGRATING flag is set
2893 * o there are no pending asts on this lockres
2894 * o all processes trying to reserve an ast on this
2895 * lockres must wait for the MIGRATING flag to clear
2900 /* last step in the migration process.
2901 * original master calls this to free all of the dlm_lock
2902 * structures that used to be for other nodes. */
2903 static void dlm_remove_nonlocal_locks(struct dlm_ctxt
*dlm
,
2904 struct dlm_lock_resource
*res
)
2906 struct list_head
*queue
= &res
->granted
;
2908 struct dlm_lock
*lock
, *next
;
2910 assert_spin_locked(&res
->spinlock
);
2912 BUG_ON(res
->owner
== dlm
->node_num
);
2914 for (i
=0; i
<3; i
++) {
2915 list_for_each_entry_safe(lock
, next
, queue
, list
) {
2916 if (lock
->ml
.node
!= dlm
->node_num
) {
2917 mlog(0, "putting lock for node %u\n",
2919 /* be extra careful */
2920 BUG_ON(!list_empty(&lock
->ast_list
));
2921 BUG_ON(!list_empty(&lock
->bast_list
));
2922 BUG_ON(lock
->ast_pending
);
2923 BUG_ON(lock
->bast_pending
);
2924 dlm_lockres_clear_refmap_bit(lock
->ml
.node
, res
);
2925 list_del_init(&lock
->list
);
2927 /* In a normal unlock, we would have added a
2928 * DLM_UNLOCK_FREE_LOCK action. Force it. */
2936 bit
= find_next_bit(res
->refmap
, O2NM_MAX_NODES
, bit
);
2937 if (bit
>= O2NM_MAX_NODES
)
2939 /* do not clear the local node reference, if there is a
2940 * process holding this, let it drop the ref itself */
2941 if (bit
!= dlm
->node_num
) {
2942 mlog(0, "%s:%.*s: node %u had a ref to this "
2943 "migrating lockres, clearing\n", dlm
->name
,
2944 res
->lockname
.len
, res
->lockname
.name
, bit
);
2945 dlm_lockres_clear_refmap_bit(bit
, res
);
2951 /* for now this is not too intelligent. we will
2952 * need stats to make this do the right thing.
2953 * this just finds the first lock on one of the
2954 * queues and uses that node as the target. */
2955 static u8
dlm_pick_migration_target(struct dlm_ctxt
*dlm
,
2956 struct dlm_lock_resource
*res
)
2959 struct list_head
*queue
= &res
->granted
;
2960 struct dlm_lock
*lock
;
2963 assert_spin_locked(&dlm
->spinlock
);
2965 spin_lock(&res
->spinlock
);
2966 for (i
=0; i
<3; i
++) {
2967 list_for_each_entry(lock
, queue
, list
) {
2968 /* up to the caller to make sure this node
2970 if (lock
->ml
.node
!= dlm
->node_num
) {
2971 spin_unlock(&res
->spinlock
);
2972 return lock
->ml
.node
;
2977 spin_unlock(&res
->spinlock
);
2978 mlog(0, "have not found a suitable target yet! checking domain map\n");
2980 /* ok now we're getting desperate. pick anyone alive. */
2983 nodenum
= find_next_bit(dlm
->domain_map
,
2984 O2NM_MAX_NODES
, nodenum
+1);
2985 mlog(0, "found %d in domain map\n", nodenum
);
2986 if (nodenum
>= O2NM_MAX_NODES
)
2988 if (nodenum
!= dlm
->node_num
) {
2989 mlog(0, "picking %d\n", nodenum
);
2994 mlog(0, "giving up. no master to migrate to\n");
2995 return DLM_LOCK_RES_OWNER_UNKNOWN
;
3000 /* this is called by the new master once all lockres
3001 * data has been received */
3002 static int dlm_do_migrate_request(struct dlm_ctxt
*dlm
,
3003 struct dlm_lock_resource
*res
,
3004 u8 master
, u8 new_master
,
3005 struct dlm_node_iter
*iter
)
3007 struct dlm_migrate_request migrate
;
3008 int ret
, skip
, status
= 0;
3011 memset(&migrate
, 0, sizeof(migrate
));
3012 migrate
.namelen
= res
->lockname
.len
;
3013 memcpy(migrate
.name
, res
->lockname
.name
, migrate
.namelen
);
3014 migrate
.new_master
= new_master
;
3015 migrate
.master
= master
;
3019 /* send message to all nodes, except the master and myself */
3020 while ((nodenum
= dlm_node_iter_next(iter
)) >= 0) {
3021 if (nodenum
== master
||
3022 nodenum
== new_master
)
3025 /* We could race exit domain. If exited, skip. */
3026 spin_lock(&dlm
->spinlock
);
3027 skip
= (!test_bit(nodenum
, dlm
->domain_map
));
3028 spin_unlock(&dlm
->spinlock
);
3030 clear_bit(nodenum
, iter
->node_map
);
3034 ret
= o2net_send_message(DLM_MIGRATE_REQUEST_MSG
, dlm
->key
,
3035 &migrate
, sizeof(migrate
), nodenum
,
3038 mlog(0, "migrate_request returned %d!\n", ret
);
3039 if (!dlm_is_host_down(ret
)) {
3040 mlog(ML_ERROR
, "unhandled error=%d!\n", ret
);
3043 clear_bit(nodenum
, iter
->node_map
);
3045 } else if (status
< 0) {
3046 mlog(0, "migrate request (node %u) returned %d!\n",
3049 } else if (status
== DLM_MIGRATE_RESPONSE_MASTERY_REF
) {
3050 /* during the migration request we short-circuited
3051 * the mastery of the lockres. make sure we have
3052 * a mastery ref for nodenum */
3053 mlog(0, "%s:%.*s: need ref for node %u\n",
3054 dlm
->name
, res
->lockname
.len
, res
->lockname
.name
,
3056 spin_lock(&res
->spinlock
);
3057 dlm_lockres_set_refmap_bit(nodenum
, res
);
3058 spin_unlock(&res
->spinlock
);
3065 mlog(0, "returning ret=%d\n", ret
);
3070 /* if there is an existing mle for this lockres, we now know who the master is.
3071 * (the one who sent us *this* message) we can clear it up right away.
3072 * since the process that put the mle on the list still has a reference to it,
3073 * we can unhash it now, set the master and wake the process. as a result,
3074 * we will have no mle in the list to start with. now we can add an mle for
3075 * the migration and this should be the only one found for those scanning the
3077 int dlm_migrate_request_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
3080 struct dlm_ctxt
*dlm
= data
;
3081 struct dlm_lock_resource
*res
= NULL
;
3082 struct dlm_migrate_request
*migrate
= (struct dlm_migrate_request
*) msg
->buf
;
3083 struct dlm_master_list_entry
*mle
= NULL
, *oldmle
= NULL
;
3085 unsigned int namelen
, hash
;
3091 name
= migrate
->name
;
3092 namelen
= migrate
->namelen
;
3093 hash
= dlm_lockid_hash(name
, namelen
);
3095 /* preallocate.. if this fails, abort */
3096 mle
= (struct dlm_master_list_entry
*) kmem_cache_alloc(dlm_mle_cache
,
3104 /* check for pre-existing lock */
3105 spin_lock(&dlm
->spinlock
);
3106 res
= __dlm_lookup_lockres(dlm
, name
, namelen
, hash
);
3107 spin_lock(&dlm
->master_lock
);
3110 spin_lock(&res
->spinlock
);
3111 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
3112 /* if all is working ok, this can only mean that we got
3113 * a migrate request from a node that we now see as
3114 * dead. what can we do here? drop it to the floor? */
3115 spin_unlock(&res
->spinlock
);
3116 mlog(ML_ERROR
, "Got a migrate request, but the "
3117 "lockres is marked as recovering!");
3118 kmem_cache_free(dlm_mle_cache
, mle
);
3119 ret
= -EINVAL
; /* need a better solution */
3122 res
->state
|= DLM_LOCK_RES_MIGRATING
;
3123 spin_unlock(&res
->spinlock
);
3126 /* ignore status. only nonzero status would BUG. */
3127 ret
= dlm_add_migration_mle(dlm
, res
, mle
, &oldmle
,
3129 migrate
->new_master
,
3133 spin_unlock(&dlm
->master_lock
);
3134 spin_unlock(&dlm
->spinlock
);
3137 /* master is known, detach if not already detached */
3138 dlm_mle_detach_hb_events(dlm
, oldmle
);
3139 dlm_put_mle(oldmle
);
3143 dlm_lockres_put(res
);
3149 /* must be holding dlm->spinlock and dlm->master_lock
3150 * when adding a migration mle, we can clear any other mles
3151 * in the master list because we know with certainty that
3152 * the master is "master". so we remove any old mle from
3153 * the list after setting it's master field, and then add
3154 * the new migration mle. this way we can hold with the rule
3155 * of having only one mle for a given lock name at all times. */
3156 static int dlm_add_migration_mle(struct dlm_ctxt
*dlm
,
3157 struct dlm_lock_resource
*res
,
3158 struct dlm_master_list_entry
*mle
,
3159 struct dlm_master_list_entry
**oldmle
,
3160 const char *name
, unsigned int namelen
,
3161 u8 new_master
, u8 master
)
3170 assert_spin_locked(&dlm
->spinlock
);
3171 assert_spin_locked(&dlm
->master_lock
);
3173 /* caller is responsible for any ref taken here on oldmle */
3174 found
= dlm_find_mle(dlm
, oldmle
, (char *)name
, namelen
);
3176 struct dlm_master_list_entry
*tmp
= *oldmle
;
3177 spin_lock(&tmp
->spinlock
);
3178 if (tmp
->type
== DLM_MLE_MIGRATION
) {
3179 if (master
== dlm
->node_num
) {
3180 /* ah another process raced me to it */
3181 mlog(0, "tried to migrate %.*s, but some "
3182 "process beat me to it\n",
3186 /* bad. 2 NODES are trying to migrate! */
3187 mlog(ML_ERROR
, "migration error mle: "
3188 "master=%u new_master=%u // request: "
3189 "master=%u new_master=%u // "
3191 tmp
->master
, tmp
->new_master
,
3197 /* this is essentially what assert_master does */
3198 tmp
->master
= master
;
3199 atomic_set(&tmp
->woken
, 1);
3201 /* remove it so that only one mle will be found */
3202 __dlm_unlink_mle(dlm
, tmp
);
3203 __dlm_mle_detach_hb_events(dlm
, tmp
);
3204 ret
= DLM_MIGRATE_RESPONSE_MASTERY_REF
;
3205 mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3206 "telling master to get ref for cleared out mle "
3207 "during migration\n", dlm
->name
, namelen
, name
,
3208 master
, new_master
);
3210 spin_unlock(&tmp
->spinlock
);
3213 /* now add a migration mle to the tail of the list */
3214 dlm_init_mle(mle
, DLM_MLE_MIGRATION
, dlm
, res
, name
, namelen
);
3215 mle
->new_master
= new_master
;
3216 /* the new master will be sending an assert master for this.
3217 * at that point we will get the refmap reference */
3218 mle
->master
= master
;
3219 /* do this for consistency with other mle types */
3220 set_bit(new_master
, mle
->maybe_map
);
3221 __dlm_insert_mle(dlm
, mle
);
3227 * Sets the owner of the lockres, associated to the mle, to UNKNOWN
3229 static struct dlm_lock_resource
*dlm_reset_mleres_owner(struct dlm_ctxt
*dlm
,
3230 struct dlm_master_list_entry
*mle
)
3232 struct dlm_lock_resource
*res
;
3235 /* Find the lockres associated to the mle and set its owner to UNK */
3236 hash
= dlm_lockid_hash(mle
->u
.mlename
.name
, mle
->u
.mlename
.len
);
3237 res
= __dlm_lookup_lockres(dlm
, mle
->u
.mlename
.name
, mle
->u
.mlename
.len
,
3240 spin_unlock(&dlm
->master_lock
);
3242 /* move lockres onto recovery list */
3243 spin_lock(&res
->spinlock
);
3244 dlm_set_lockres_owner(dlm
, res
, DLM_LOCK_RES_OWNER_UNKNOWN
);
3245 dlm_move_lockres_to_recovery_list(dlm
, res
);
3246 spin_unlock(&res
->spinlock
);
3247 dlm_lockres_put(res
);
3249 /* about to get rid of mle, detach from heartbeat */
3250 __dlm_mle_detach_hb_events(dlm
, mle
);
3253 spin_lock(&dlm
->master_lock
);
3255 spin_unlock(&dlm
->master_lock
);
3261 static void dlm_clean_migration_mle(struct dlm_ctxt
*dlm
,
3262 struct dlm_master_list_entry
*mle
)
3264 __dlm_mle_detach_hb_events(dlm
, mle
);
3266 spin_lock(&mle
->spinlock
);
3267 __dlm_unlink_mle(dlm
, mle
);
3268 atomic_set(&mle
->woken
, 1);
3269 spin_unlock(&mle
->spinlock
);
3274 static void dlm_clean_block_mle(struct dlm_ctxt
*dlm
,
3275 struct dlm_master_list_entry
*mle
, u8 dead_node
)
3279 BUG_ON(mle
->type
!= DLM_MLE_BLOCK
);
3281 spin_lock(&mle
->spinlock
);
3282 bit
= find_next_bit(mle
->maybe_map
, O2NM_MAX_NODES
, 0);
3283 if (bit
!= dead_node
) {
3284 mlog(0, "mle found, but dead node %u would not have been "
3285 "master\n", dead_node
);
3286 spin_unlock(&mle
->spinlock
);
3288 /* Must drop the refcount by one since the assert_master will
3289 * never arrive. This may result in the mle being unlinked and
3290 * freed, but there may still be a process waiting in the
3291 * dlmlock path which is fine. */
3292 mlog(0, "node %u was expected master\n", dead_node
);
3293 atomic_set(&mle
->woken
, 1);
3294 spin_unlock(&mle
->spinlock
);
3297 /* Do not need events any longer, so detach from heartbeat */
3298 __dlm_mle_detach_hb_events(dlm
, mle
);
3303 void dlm_clean_master_list(struct dlm_ctxt
*dlm
, u8 dead_node
)
3305 struct dlm_master_list_entry
*mle
;
3306 struct dlm_lock_resource
*res
;
3307 struct hlist_head
*bucket
;
3308 struct hlist_node
*list
;
3311 mlog_entry("dlm=%s, dead node=%u\n", dlm
->name
, dead_node
);
3313 assert_spin_locked(&dlm
->spinlock
);
3315 /* clean the master list */
3316 spin_lock(&dlm
->master_lock
);
3317 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
3318 bucket
= dlm_master_hash(dlm
, i
);
3319 hlist_for_each(list
, bucket
) {
3320 mle
= hlist_entry(list
, struct dlm_master_list_entry
,
3323 BUG_ON(mle
->type
!= DLM_MLE_BLOCK
&&
3324 mle
->type
!= DLM_MLE_MASTER
&&
3325 mle
->type
!= DLM_MLE_MIGRATION
);
3327 /* MASTER mles are initiated locally. The waiting
3328 * process will notice the node map change shortly.
3329 * Let that happen as normal. */
3330 if (mle
->type
== DLM_MLE_MASTER
)
3333 /* BLOCK mles are initiated by other nodes. Need to
3334 * clean up if the dead node would have been the
3336 if (mle
->type
== DLM_MLE_BLOCK
) {
3337 dlm_clean_block_mle(dlm
, mle
, dead_node
);
3341 /* Everything else is a MIGRATION mle */
3343 /* The rule for MIGRATION mles is that the master
3344 * becomes UNKNOWN if *either* the original or the new
3345 * master dies. All UNKNOWN lockres' are sent to
3346 * whichever node becomes the recovery master. The new
3347 * master is responsible for determining if there is
3348 * still a master for this lockres, or if he needs to
3349 * take over mastery. Either way, this node should
3350 * expect another message to resolve this. */
3352 if (mle
->master
!= dead_node
&&
3353 mle
->new_master
!= dead_node
)
3356 /* If we have reached this point, this mle needs to be
3357 * removed from the list and freed. */
3358 dlm_clean_migration_mle(dlm
, mle
);
3360 mlog(0, "%s: node %u died during migration from "
3361 "%u to %u!\n", dlm
->name
, dead_node
, mle
->master
,
3364 /* If we find a lockres associated with the mle, we've
3365 * hit this rare case that messes up our lock ordering.
3366 * If so, we need to drop the master lock so that we can
3367 * take the lockres lock, meaning that we will have to
3368 * restart from the head of list. */
3369 res
= dlm_reset_mleres_owner(dlm
, mle
);
3374 /* This may be the last reference */
3378 spin_unlock(&dlm
->master_lock
);
3381 int dlm_finish_migration(struct dlm_ctxt
*dlm
, struct dlm_lock_resource
*res
,
3384 struct dlm_node_iter iter
;
3387 spin_lock(&dlm
->spinlock
);
3388 dlm_node_iter_init(dlm
->domain_map
, &iter
);
3389 clear_bit(old_master
, iter
.node_map
);
3390 clear_bit(dlm
->node_num
, iter
.node_map
);
3391 spin_unlock(&dlm
->spinlock
);
3393 /* ownership of the lockres is changing. account for the
3394 * mastery reference here since old_master will briefly have
3395 * a reference after the migration completes */
3396 spin_lock(&res
->spinlock
);
3397 dlm_lockres_set_refmap_bit(old_master
, res
);
3398 spin_unlock(&res
->spinlock
);
3400 mlog(0, "now time to do a migrate request to other nodes\n");
3401 ret
= dlm_do_migrate_request(dlm
, res
, old_master
,
3402 dlm
->node_num
, &iter
);
3408 mlog(0, "doing assert master of %.*s to all except the original node\n",
3409 res
->lockname
.len
, res
->lockname
.name
);
3410 /* this call now finishes out the nodemap
3411 * even if one or more nodes die */
3412 ret
= dlm_do_assert_master(dlm
, res
, iter
.node_map
,
3413 DLM_ASSERT_MASTER_FINISH_MIGRATION
);
3415 /* no longer need to retry. all living nodes contacted. */
3420 memset(iter
.node_map
, 0, sizeof(iter
.node_map
));
3421 set_bit(old_master
, iter
.node_map
);
3422 mlog(0, "doing assert master of %.*s back to %u\n",
3423 res
->lockname
.len
, res
->lockname
.name
, old_master
);
3424 ret
= dlm_do_assert_master(dlm
, res
, iter
.node_map
,
3425 DLM_ASSERT_MASTER_FINISH_MIGRATION
);
3427 mlog(0, "assert master to original master failed "
3429 /* the only nonzero status here would be because of
3430 * a dead original node. we're done. */
3434 /* all done, set the owner, clear the flag */
3435 spin_lock(&res
->spinlock
);
3436 dlm_set_lockres_owner(dlm
, res
, dlm
->node_num
);
3437 res
->state
&= ~DLM_LOCK_RES_MIGRATING
;
3438 spin_unlock(&res
->spinlock
);
3439 /* re-dirty it on the new master */
3440 dlm_kick_thread(dlm
, res
);
3447 * LOCKRES AST REFCOUNT
3448 * this is integral to migration
3451 /* for future intent to call an ast, reserve one ahead of time.
3452 * this should be called only after waiting on the lockres
3453 * with dlm_wait_on_lockres, and while still holding the
3454 * spinlock after the call. */
3455 void __dlm_lockres_reserve_ast(struct dlm_lock_resource
*res
)
3457 assert_spin_locked(&res
->spinlock
);
3458 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
3459 __dlm_print_one_lock_resource(res
);
3461 BUG_ON(res
->state
& DLM_LOCK_RES_MIGRATING
);
3463 atomic_inc(&res
->asts_reserved
);
3467 * used to drop the reserved ast, either because it went unused,
3468 * or because the ast/bast was actually called.
3470 * also, if there is a pending migration on this lockres,
3471 * and this was the last pending ast on the lockres,
3472 * atomically set the MIGRATING flag before we drop the lock.
3473 * this is how we ensure that migration can proceed with no
3474 * asts in progress. note that it is ok if the state of the
3475 * queues is such that a lock should be granted in the future
3476 * or that a bast should be fired, because the new master will
3477 * shuffle the lists on this lockres as soon as it is migrated.
3479 void dlm_lockres_release_ast(struct dlm_ctxt
*dlm
,
3480 struct dlm_lock_resource
*res
)
3482 if (!atomic_dec_and_lock(&res
->asts_reserved
, &res
->spinlock
))
3485 if (!res
->migration_pending
) {
3486 spin_unlock(&res
->spinlock
);
3490 BUG_ON(res
->state
& DLM_LOCK_RES_MIGRATING
);
3491 res
->migration_pending
= 0;
3492 res
->state
|= DLM_LOCK_RES_MIGRATING
;
3493 spin_unlock(&res
->spinlock
);
3495 wake_up(&dlm
->migration_wq
);