1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * defines domain join / leave apis
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.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/delay.h>
34 #include <linux/err.h>
35 #include <linux/debugfs.h>
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
42 #include "dlmcommon.h"
43 #include "dlmdomain.h"
48 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
49 #include "cluster/masklog.h"
52 * ocfs2 node maps are array of long int, which limits to send them freely
53 * across the wire due to endianness issues. To workaround this, we convert
54 * long ints to byte arrays. Following 3 routines are helper functions to
55 * set/test/copy bits within those array of bytes
57 static inline void byte_set_bit(u8 nr
, u8 map
[])
59 map
[nr
>> 3] |= (1UL << (nr
& 7));
62 static inline int byte_test_bit(u8 nr
, u8 map
[])
64 return ((1UL << (nr
& 7)) & (map
[nr
>> 3])) != 0;
67 static inline void byte_copymap(u8 dmap
[], unsigned long smap
[],
75 memset(dmap
, 0, ((sz
+ 7) >> 3));
76 for (nn
= 0 ; nn
< sz
; nn
++)
77 if (test_bit(nn
, smap
))
78 byte_set_bit(nn
, dmap
);
81 static void dlm_free_pagevec(void **vec
, int pages
)
84 free_page((unsigned long)vec
[pages
]);
88 static void **dlm_alloc_pagevec(int pages
)
90 void **vec
= kmalloc(pages
* sizeof(void *), GFP_KERNEL
);
96 for (i
= 0; i
< pages
; i
++)
97 if (!(vec
[i
] = (void *)__get_free_page(GFP_KERNEL
)))
100 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
101 pages
, (unsigned long)DLM_HASH_PAGES
,
102 (unsigned long)DLM_BUCKETS_PER_PAGE
);
105 dlm_free_pagevec(vec
, i
);
111 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
113 * struct dlm_ctxt->spinlock
114 * struct dlm_lock_resource->spinlock
115 * struct dlm_ctxt->master_lock
116 * struct dlm_ctxt->ast_lock
117 * dlm_master_list_entry->spinlock
122 DEFINE_SPINLOCK(dlm_domain_lock
);
123 LIST_HEAD(dlm_domains
);
124 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events
);
127 * The supported protocol version for DLM communication. Running domains
128 * will have a negotiated version with the same major number and a minor
129 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
130 * be used to determine what a running domain is actually using.
132 * New in version 1.1:
133 * - Message DLM_QUERY_REGION added to support global heartbeat
134 * - Message DLM_QUERY_NODEINFO added to allow online node removes
136 static const struct dlm_protocol_version dlm_protocol
= {
141 #define DLM_DOMAIN_BACKOFF_MS 200
143 static int dlm_query_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
145 static int dlm_assert_joined_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
147 static int dlm_cancel_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
149 static int dlm_query_region_handler(struct o2net_msg
*msg
, u32 len
,
150 void *data
, void **ret_data
);
151 static int dlm_exit_domain_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
153 static int dlm_protocol_compare(struct dlm_protocol_version
*existing
,
154 struct dlm_protocol_version
*request
);
156 static void dlm_unregister_domain_handlers(struct dlm_ctxt
*dlm
);
158 void __dlm_unhash_lockres(struct dlm_lock_resource
*lockres
)
160 if (!hlist_unhashed(&lockres
->hash_node
)) {
161 hlist_del_init(&lockres
->hash_node
);
162 dlm_lockres_put(lockres
);
166 void __dlm_insert_lockres(struct dlm_ctxt
*dlm
,
167 struct dlm_lock_resource
*res
)
169 struct hlist_head
*bucket
;
172 assert_spin_locked(&dlm
->spinlock
);
175 bucket
= dlm_lockres_hash(dlm
, q
->hash
);
177 /* get a reference for our hashtable */
178 dlm_lockres_get(res
);
180 hlist_add_head(&res
->hash_node
, bucket
);
183 struct dlm_lock_resource
* __dlm_lookup_lockres_full(struct dlm_ctxt
*dlm
,
188 struct hlist_head
*bucket
;
189 struct hlist_node
*list
;
191 mlog(0, "%.*s\n", len
, name
);
193 assert_spin_locked(&dlm
->spinlock
);
195 bucket
= dlm_lockres_hash(dlm
, hash
);
197 hlist_for_each(list
, bucket
) {
198 struct dlm_lock_resource
*res
= hlist_entry(list
,
199 struct dlm_lock_resource
, hash_node
);
200 if (res
->lockname
.name
[0] != name
[0])
202 if (unlikely(res
->lockname
.len
!= len
))
204 if (memcmp(res
->lockname
.name
+ 1, name
+ 1, len
- 1))
206 dlm_lockres_get(res
);
212 /* intended to be called by functions which do not care about lock
213 * resources which are being purged (most net _handler functions).
214 * this will return NULL for any lock resource which is found but
215 * currently in the process of dropping its mastery reference.
216 * use __dlm_lookup_lockres_full when you need the lock resource
217 * regardless (e.g. dlm_get_lock_resource) */
218 struct dlm_lock_resource
* __dlm_lookup_lockres(struct dlm_ctxt
*dlm
,
223 struct dlm_lock_resource
*res
= NULL
;
225 mlog(0, "%.*s\n", len
, name
);
227 assert_spin_locked(&dlm
->spinlock
);
229 res
= __dlm_lookup_lockres_full(dlm
, name
, len
, hash
);
231 spin_lock(&res
->spinlock
);
232 if (res
->state
& DLM_LOCK_RES_DROPPING_REF
) {
233 spin_unlock(&res
->spinlock
);
234 dlm_lockres_put(res
);
237 spin_unlock(&res
->spinlock
);
243 struct dlm_lock_resource
* dlm_lookup_lockres(struct dlm_ctxt
*dlm
,
247 struct dlm_lock_resource
*res
;
248 unsigned int hash
= dlm_lockid_hash(name
, len
);
250 spin_lock(&dlm
->spinlock
);
251 res
= __dlm_lookup_lockres(dlm
, name
, len
, hash
);
252 spin_unlock(&dlm
->spinlock
);
256 static struct dlm_ctxt
* __dlm_lookup_domain_full(const char *domain
, int len
)
258 struct dlm_ctxt
*tmp
= NULL
;
259 struct list_head
*iter
;
261 assert_spin_locked(&dlm_domain_lock
);
263 /* tmp->name here is always NULL terminated,
264 * but domain may not be! */
265 list_for_each(iter
, &dlm_domains
) {
266 tmp
= list_entry (iter
, struct dlm_ctxt
, list
);
267 if (strlen(tmp
->name
) == len
&&
268 memcmp(tmp
->name
, domain
, len
)==0)
276 /* For null terminated domain strings ONLY */
277 static struct dlm_ctxt
* __dlm_lookup_domain(const char *domain
)
279 assert_spin_locked(&dlm_domain_lock
);
281 return __dlm_lookup_domain_full(domain
, strlen(domain
));
285 /* returns true on one of two conditions:
286 * 1) the domain does not exist
287 * 2) the domain exists and it's state is "joined" */
288 static int dlm_wait_on_domain_helper(const char *domain
)
291 struct dlm_ctxt
*tmp
= NULL
;
293 spin_lock(&dlm_domain_lock
);
295 tmp
= __dlm_lookup_domain(domain
);
298 else if (tmp
->dlm_state
== DLM_CTXT_JOINED
)
301 spin_unlock(&dlm_domain_lock
);
305 static void dlm_free_ctxt_mem(struct dlm_ctxt
*dlm
)
307 dlm_destroy_debugfs_subroot(dlm
);
309 if (dlm
->lockres_hash
)
310 dlm_free_pagevec((void **)dlm
->lockres_hash
, DLM_HASH_PAGES
);
312 if (dlm
->master_hash
)
313 dlm_free_pagevec((void **)dlm
->master_hash
, DLM_HASH_PAGES
);
321 /* A little strange - this function will be called while holding
322 * dlm_domain_lock and is expected to be holding it on the way out. We
323 * will however drop and reacquire it multiple times */
324 static void dlm_ctxt_release(struct kref
*kref
)
326 struct dlm_ctxt
*dlm
;
328 dlm
= container_of(kref
, struct dlm_ctxt
, dlm_refs
);
330 BUG_ON(dlm
->num_joins
);
331 BUG_ON(dlm
->dlm_state
== DLM_CTXT_JOINED
);
333 /* we may still be in the list if we hit an error during join. */
334 list_del_init(&dlm
->list
);
336 spin_unlock(&dlm_domain_lock
);
338 mlog(0, "freeing memory from domain %s\n", dlm
->name
);
340 wake_up(&dlm_domain_events
);
342 dlm_free_ctxt_mem(dlm
);
344 spin_lock(&dlm_domain_lock
);
347 void dlm_put(struct dlm_ctxt
*dlm
)
349 spin_lock(&dlm_domain_lock
);
350 kref_put(&dlm
->dlm_refs
, dlm_ctxt_release
);
351 spin_unlock(&dlm_domain_lock
);
354 static void __dlm_get(struct dlm_ctxt
*dlm
)
356 kref_get(&dlm
->dlm_refs
);
359 /* given a questionable reference to a dlm object, gets a reference if
360 * it can find it in the list, otherwise returns NULL in which case
361 * you shouldn't trust your pointer. */
362 struct dlm_ctxt
*dlm_grab(struct dlm_ctxt
*dlm
)
364 struct list_head
*iter
;
365 struct dlm_ctxt
*target
= NULL
;
367 spin_lock(&dlm_domain_lock
);
369 list_for_each(iter
, &dlm_domains
) {
370 target
= list_entry (iter
, struct dlm_ctxt
, list
);
380 spin_unlock(&dlm_domain_lock
);
385 int dlm_domain_fully_joined(struct dlm_ctxt
*dlm
)
389 spin_lock(&dlm_domain_lock
);
390 ret
= (dlm
->dlm_state
== DLM_CTXT_JOINED
) ||
391 (dlm
->dlm_state
== DLM_CTXT_IN_SHUTDOWN
);
392 spin_unlock(&dlm_domain_lock
);
397 static void dlm_destroy_dlm_worker(struct dlm_ctxt
*dlm
)
399 if (dlm
->dlm_worker
) {
400 flush_workqueue(dlm
->dlm_worker
);
401 destroy_workqueue(dlm
->dlm_worker
);
402 dlm
->dlm_worker
= NULL
;
406 static void dlm_complete_dlm_shutdown(struct dlm_ctxt
*dlm
)
408 dlm_unregister_domain_handlers(dlm
);
409 dlm_debug_shutdown(dlm
);
410 dlm_complete_thread(dlm
);
411 dlm_complete_recovery_thread(dlm
);
412 dlm_destroy_dlm_worker(dlm
);
414 /* We've left the domain. Now we can take ourselves out of the
415 * list and allow the kref stuff to help us free the
417 spin_lock(&dlm_domain_lock
);
418 list_del_init(&dlm
->list
);
419 spin_unlock(&dlm_domain_lock
);
421 /* Wake up anyone waiting for us to remove this domain */
422 wake_up(&dlm_domain_events
);
425 static int dlm_migrate_all_locks(struct dlm_ctxt
*dlm
)
427 int i
, num
, n
, ret
= 0;
428 struct dlm_lock_resource
*res
;
429 struct hlist_node
*iter
;
430 struct hlist_head
*bucket
;
433 mlog(0, "Migrating locks from domain %s\n", dlm
->name
);
436 spin_lock(&dlm
->spinlock
);
437 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
440 bucket
= dlm_lockres_hash(dlm
, i
);
441 iter
= bucket
->first
;
444 res
= hlist_entry(iter
, struct dlm_lock_resource
,
446 dlm_lockres_get(res
);
447 /* migrate, if necessary. this will drop the dlm
448 * spinlock and retake it if it does migration. */
449 dropped
= dlm_empty_lockres(dlm
, res
);
451 spin_lock(&res
->spinlock
);
452 __dlm_lockres_calc_usage(dlm
, res
);
453 iter
= res
->hash_node
.next
;
454 spin_unlock(&res
->spinlock
);
456 dlm_lockres_put(res
);
461 cond_resched_lock(&dlm
->spinlock
);
464 spin_unlock(&dlm
->spinlock
);
465 wake_up(&dlm
->dlm_thread_wq
);
467 /* let the dlm thread take care of purging, keep scanning until
468 * nothing remains in the hash */
470 mlog(0, "%s: %d lock resources in hash last pass\n",
474 mlog(0, "DONE Migrating locks from domain %s\n", dlm
->name
);
478 static int dlm_no_joining_node(struct dlm_ctxt
*dlm
)
482 spin_lock(&dlm
->spinlock
);
483 ret
= dlm
->joining_node
== DLM_LOCK_RES_OWNER_UNKNOWN
;
484 spin_unlock(&dlm
->spinlock
);
489 static void dlm_mark_domain_leaving(struct dlm_ctxt
*dlm
)
491 /* Yikes, a double spinlock! I need domain_lock for the dlm
492 * state and the dlm spinlock for join state... Sorry! */
494 spin_lock(&dlm_domain_lock
);
495 spin_lock(&dlm
->spinlock
);
497 if (dlm
->joining_node
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
498 mlog(0, "Node %d is joining, we wait on it.\n",
500 spin_unlock(&dlm
->spinlock
);
501 spin_unlock(&dlm_domain_lock
);
503 wait_event(dlm
->dlm_join_events
, dlm_no_joining_node(dlm
));
507 dlm
->dlm_state
= DLM_CTXT_LEAVING
;
508 spin_unlock(&dlm
->spinlock
);
509 spin_unlock(&dlm_domain_lock
);
512 static void __dlm_print_nodes(struct dlm_ctxt
*dlm
)
516 assert_spin_locked(&dlm
->spinlock
);
518 printk(KERN_NOTICE
"o2dlm: Nodes in domain %s: ", dlm
->name
);
520 while ((node
= find_next_bit(dlm
->domain_map
, O2NM_MAX_NODES
,
521 node
+ 1)) < O2NM_MAX_NODES
) {
527 static int dlm_exit_domain_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
530 struct dlm_ctxt
*dlm
= data
;
532 struct dlm_exit_domain
*exit_msg
= (struct dlm_exit_domain
*) msg
->buf
;
534 mlog(0, "%p %u %p", msg
, len
, data
);
539 node
= exit_msg
->node_idx
;
541 printk(KERN_NOTICE
"o2dlm: Node %u leaves domain %s\n", node
, dlm
->name
);
543 spin_lock(&dlm
->spinlock
);
544 clear_bit(node
, dlm
->domain_map
);
545 __dlm_print_nodes(dlm
);
547 /* notify anything attached to the heartbeat events */
548 dlm_hb_event_notify_attached(dlm
, node
, 0);
550 spin_unlock(&dlm
->spinlock
);
557 static int dlm_send_one_domain_exit(struct dlm_ctxt
*dlm
,
561 struct dlm_exit_domain leave_msg
;
563 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
564 node
, dlm
->name
, dlm
->node_num
);
566 memset(&leave_msg
, 0, sizeof(leave_msg
));
567 leave_msg
.node_idx
= dlm
->node_num
;
569 status
= o2net_send_message(DLM_EXIT_DOMAIN_MSG
, dlm
->key
,
570 &leave_msg
, sizeof(leave_msg
), node
,
573 mlog(ML_ERROR
, "Error %d when sending message %u (key 0x%x) to "
574 "node %u\n", status
, DLM_EXIT_DOMAIN_MSG
, dlm
->key
, node
);
575 mlog(0, "status return %d from o2net_send_message\n", status
);
581 static void dlm_leave_domain(struct dlm_ctxt
*dlm
)
583 int node
, clear_node
, status
;
585 /* At this point we've migrated away all our locks and won't
586 * accept mastership of new ones. The dlm is responsible for
587 * almost nothing now. We make sure not to confuse any joining
588 * nodes and then commence shutdown procedure. */
590 spin_lock(&dlm
->spinlock
);
591 /* Clear ourselves from the domain map */
592 clear_bit(dlm
->node_num
, dlm
->domain_map
);
593 while ((node
= find_next_bit(dlm
->domain_map
, O2NM_MAX_NODES
,
594 0)) < O2NM_MAX_NODES
) {
595 /* Drop the dlm spinlock. This is safe wrt the domain_map.
596 * -nodes cannot be added now as the
597 * query_join_handlers knows to respond with OK_NO_MAP
598 * -we catch the right network errors if a node is
599 * removed from the map while we're sending him the
601 spin_unlock(&dlm
->spinlock
);
605 status
= dlm_send_one_domain_exit(dlm
, node
);
607 status
!= -ENOPROTOOPT
&&
608 status
!= -ENOTCONN
) {
609 mlog(ML_NOTICE
, "Error %d sending domain exit message "
610 "to node %d\n", status
, node
);
612 /* Not sure what to do here but lets sleep for
613 * a bit in case this was a transient
615 msleep(DLM_DOMAIN_BACKOFF_MS
);
619 spin_lock(&dlm
->spinlock
);
620 /* If we're not clearing the node bit then we intend
621 * to loop back around to try again. */
623 clear_bit(node
, dlm
->domain_map
);
625 spin_unlock(&dlm
->spinlock
);
628 int dlm_joined(struct dlm_ctxt
*dlm
)
632 spin_lock(&dlm_domain_lock
);
634 if (dlm
->dlm_state
== DLM_CTXT_JOINED
)
637 spin_unlock(&dlm_domain_lock
);
642 int dlm_shutting_down(struct dlm_ctxt
*dlm
)
646 spin_lock(&dlm_domain_lock
);
648 if (dlm
->dlm_state
== DLM_CTXT_IN_SHUTDOWN
)
651 spin_unlock(&dlm_domain_lock
);
656 void dlm_unregister_domain(struct dlm_ctxt
*dlm
)
659 struct dlm_lock_resource
*res
;
661 spin_lock(&dlm_domain_lock
);
662 BUG_ON(dlm
->dlm_state
!= DLM_CTXT_JOINED
);
663 BUG_ON(!dlm
->num_joins
);
666 if (!dlm
->num_joins
) {
667 /* We mark it "in shutdown" now so new register
668 * requests wait until we've completely left the
669 * domain. Don't use DLM_CTXT_LEAVING yet as we still
670 * want new domain joins to communicate with us at
671 * least until we've completed migration of our
673 dlm
->dlm_state
= DLM_CTXT_IN_SHUTDOWN
;
676 spin_unlock(&dlm_domain_lock
);
679 mlog(0, "shutting down domain %s\n", dlm
->name
);
681 /* We changed dlm state, notify the thread */
682 dlm_kick_thread(dlm
, NULL
);
684 while (dlm_migrate_all_locks(dlm
)) {
685 /* Give dlm_thread time to purge the lockres' */
687 mlog(0, "%s: more migration to do\n", dlm
->name
);
690 /* This list should be empty. If not, print remaining lockres */
691 if (!list_empty(&dlm
->tracking_list
)) {
692 mlog(ML_ERROR
, "Following lockres' are still on the "
694 list_for_each_entry(res
, &dlm
->tracking_list
, tracking
)
695 dlm_print_one_lock_resource(res
);
698 dlm_mark_domain_leaving(dlm
);
699 dlm_leave_domain(dlm
);
700 dlm_force_free_mles(dlm
);
701 dlm_complete_dlm_shutdown(dlm
);
705 EXPORT_SYMBOL_GPL(dlm_unregister_domain
);
707 static int dlm_query_join_proto_check(char *proto_type
, int node
,
708 struct dlm_protocol_version
*ours
,
709 struct dlm_protocol_version
*request
)
712 struct dlm_protocol_version proto
= *request
;
714 if (!dlm_protocol_compare(ours
, &proto
)) {
716 "node %u wanted to join with %s locking protocol "
717 "%u.%u, we respond with %u.%u\n",
721 proto
.pv_major
, proto
.pv_minor
);
722 request
->pv_minor
= proto
.pv_minor
;
726 "Node %u wanted to join with %s locking "
727 "protocol %u.%u, but we have %u.%u, disallowing\n",
740 * struct dlm_query_join_packet is made up of four one-byte fields. They
741 * are effectively in big-endian order already. However, little-endian
742 * machines swap them before putting the packet on the wire (because
743 * query_join's response is a status, and that status is treated as a u32
744 * on the wire). Thus, a big-endian and little-endian machines will treat
745 * this structure differently.
747 * The solution is to have little-endian machines swap the structure when
748 * converting from the structure to the u32 representation. This will
749 * result in the structure having the correct format on the wire no matter
750 * the host endian format.
752 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet
*packet
,
755 union dlm_query_join_response response
;
757 response
.packet
= *packet
;
758 *wire
= cpu_to_be32(response
.intval
);
761 static void dlm_query_join_wire_to_packet(u32 wire
,
762 struct dlm_query_join_packet
*packet
)
764 union dlm_query_join_response response
;
766 response
.intval
= cpu_to_be32(wire
);
767 *packet
= response
.packet
;
770 static int dlm_query_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
773 struct dlm_query_join_request
*query
;
774 struct dlm_query_join_packet packet
= {
775 .code
= JOIN_DISALLOW
,
777 struct dlm_ctxt
*dlm
= NULL
;
781 query
= (struct dlm_query_join_request
*) msg
->buf
;
783 mlog(0, "node %u wants to join domain %s\n", query
->node_idx
,
787 * If heartbeat doesn't consider the node live, tell it
788 * to back off and try again. This gives heartbeat a chance
791 if (!o2hb_check_node_heartbeating(query
->node_idx
)) {
792 mlog(0, "node %u is not in our live map yet\n",
795 packet
.code
= JOIN_DISALLOW
;
799 packet
.code
= JOIN_OK_NO_MAP
;
801 spin_lock(&dlm_domain_lock
);
802 dlm
= __dlm_lookup_domain_full(query
->domain
, query
->name_len
);
807 * There is a small window where the joining node may not see the
808 * node(s) that just left but still part of the cluster. DISALLOW
809 * join request if joining node has different node map.
812 while (nodenum
< O2NM_MAX_NODES
) {
813 if (test_bit(nodenum
, dlm
->domain_map
)) {
814 if (!byte_test_bit(nodenum
, query
->node_map
)) {
815 mlog(0, "disallow join as node %u does not "
816 "have node %u in its nodemap\n",
817 query
->node_idx
, nodenum
);
818 packet
.code
= JOIN_DISALLOW
;
825 /* Once the dlm ctxt is marked as leaving then we don't want
826 * to be put in someone's domain map.
827 * Also, explicitly disallow joining at certain troublesome
828 * times (ie. during recovery). */
829 if (dlm
&& dlm
->dlm_state
!= DLM_CTXT_LEAVING
) {
830 int bit
= query
->node_idx
;
831 spin_lock(&dlm
->spinlock
);
833 if (dlm
->dlm_state
== DLM_CTXT_NEW
&&
834 dlm
->joining_node
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
835 /*If this is a brand new context and we
836 * haven't started our join process yet, then
837 * the other node won the race. */
838 packet
.code
= JOIN_OK_NO_MAP
;
839 } else if (dlm
->joining_node
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
840 /* Disallow parallel joins. */
841 packet
.code
= JOIN_DISALLOW
;
842 } else if (dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
) {
843 mlog(0, "node %u trying to join, but recovery "
844 "is ongoing.\n", bit
);
845 packet
.code
= JOIN_DISALLOW
;
846 } else if (test_bit(bit
, dlm
->recovery_map
)) {
847 mlog(0, "node %u trying to join, but it "
848 "still needs recovery.\n", bit
);
849 packet
.code
= JOIN_DISALLOW
;
850 } else if (test_bit(bit
, dlm
->domain_map
)) {
851 mlog(0, "node %u trying to join, but it "
852 "is still in the domain! needs recovery?\n",
854 packet
.code
= JOIN_DISALLOW
;
856 /* Alright we're fully a part of this domain
857 * so we keep some state as to who's joining
858 * and indicate to him that needs to be fixed
861 /* Make sure we speak compatible locking protocols. */
862 if (dlm_query_join_proto_check("DLM", bit
,
863 &dlm
->dlm_locking_proto
,
864 &query
->dlm_proto
)) {
865 packet
.code
= JOIN_PROTOCOL_MISMATCH
;
866 } else if (dlm_query_join_proto_check("fs", bit
,
867 &dlm
->fs_locking_proto
,
869 packet
.code
= JOIN_PROTOCOL_MISMATCH
;
871 packet
.dlm_minor
= query
->dlm_proto
.pv_minor
;
872 packet
.fs_minor
= query
->fs_proto
.pv_minor
;
873 packet
.code
= JOIN_OK
;
874 __dlm_set_joining_node(dlm
, query
->node_idx
);
878 spin_unlock(&dlm
->spinlock
);
881 spin_unlock(&dlm_domain_lock
);
884 mlog(0, "We respond with %u\n", packet
.code
);
886 dlm_query_join_packet_to_wire(&packet
, &response
);
890 static int dlm_assert_joined_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
893 struct dlm_assert_joined
*assert;
894 struct dlm_ctxt
*dlm
= NULL
;
896 assert = (struct dlm_assert_joined
*) msg
->buf
;
898 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx
,
901 spin_lock(&dlm_domain_lock
);
902 dlm
= __dlm_lookup_domain_full(assert->domain
, assert->name_len
);
903 /* XXX should we consider no dlm ctxt an error? */
905 spin_lock(&dlm
->spinlock
);
907 /* Alright, this node has officially joined our
908 * domain. Set him in the map and clean up our
909 * leftover join state. */
910 BUG_ON(dlm
->joining_node
!= assert->node_idx
);
911 set_bit(assert->node_idx
, dlm
->domain_map
);
912 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
914 printk(KERN_NOTICE
"o2dlm: Node %u joins domain %s\n",
915 assert->node_idx
, dlm
->name
);
916 __dlm_print_nodes(dlm
);
918 /* notify anything attached to the heartbeat events */
919 dlm_hb_event_notify_attached(dlm
, assert->node_idx
, 1);
921 spin_unlock(&dlm
->spinlock
);
923 spin_unlock(&dlm_domain_lock
);
928 static int dlm_match_regions(struct dlm_ctxt
*dlm
,
929 struct dlm_query_region
*qr
,
930 char *local
, int locallen
)
932 char *remote
= qr
->qr_regions
;
934 int localnr
, i
, j
, foundit
;
937 if (!o2hb_global_heartbeat_active()) {
938 if (qr
->qr_numregions
) {
939 mlog(ML_ERROR
, "Domain %s: Joining node %d has global "
940 "heartbeat enabled but local node %d does not\n",
941 qr
->qr_domain
, qr
->qr_node
, dlm
->node_num
);
947 if (o2hb_global_heartbeat_active() && !qr
->qr_numregions
) {
948 mlog(ML_ERROR
, "Domain %s: Local node %d has global "
949 "heartbeat enabled but joining node %d does not\n",
950 qr
->qr_domain
, dlm
->node_num
, qr
->qr_node
);
956 for (i
= 0; i
< qr
->qr_numregions
; ++i
) {
957 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN
, r
);
958 r
+= O2HB_MAX_REGION_NAME_LEN
;
961 localnr
= min(O2NM_MAX_REGIONS
, locallen
/O2HB_MAX_REGION_NAME_LEN
);
962 localnr
= o2hb_get_all_regions(local
, (u8
)localnr
);
964 /* compare local regions with remote */
966 for (i
= 0; i
< localnr
; ++i
) {
969 for (j
= 0; j
<= qr
->qr_numregions
; ++j
) {
970 if (!memcmp(l
, r
, O2HB_MAX_REGION_NAME_LEN
)) {
974 r
+= O2HB_MAX_REGION_NAME_LEN
;
978 mlog(ML_ERROR
, "Domain %s: Region '%.*s' registered "
979 "in local node %d but not in joining node %d\n",
980 qr
->qr_domain
, O2HB_MAX_REGION_NAME_LEN
, l
,
981 dlm
->node_num
, qr
->qr_node
);
984 l
+= O2HB_MAX_REGION_NAME_LEN
;
987 /* compare remote with local regions */
989 for (i
= 0; i
< qr
->qr_numregions
; ++i
) {
992 for (j
= 0; j
< localnr
; ++j
) {
993 if (!memcmp(r
, l
, O2HB_MAX_REGION_NAME_LEN
)) {
997 l
+= O2HB_MAX_REGION_NAME_LEN
;
1001 mlog(ML_ERROR
, "Domain %s: Region '%.*s' registered "
1002 "in joining node %d but not in local node %d\n",
1003 qr
->qr_domain
, O2HB_MAX_REGION_NAME_LEN
, r
,
1004 qr
->qr_node
, dlm
->node_num
);
1007 r
+= O2HB_MAX_REGION_NAME_LEN
;
1014 static int dlm_send_regions(struct dlm_ctxt
*dlm
, unsigned long *node_map
)
1016 struct dlm_query_region
*qr
= NULL
;
1017 int status
, ret
= 0, i
;
1020 if (find_next_bit(node_map
, O2NM_MAX_NODES
, 0) >= O2NM_MAX_NODES
)
1023 qr
= kzalloc(sizeof(struct dlm_query_region
), GFP_KERNEL
);
1030 qr
->qr_node
= dlm
->node_num
;
1031 qr
->qr_namelen
= strlen(dlm
->name
);
1032 memcpy(qr
->qr_domain
, dlm
->name
, qr
->qr_namelen
);
1033 /* if local hb, the numregions will be zero */
1034 if (o2hb_global_heartbeat_active())
1035 qr
->qr_numregions
= o2hb_get_all_regions(qr
->qr_regions
,
1039 for (i
= 0; i
< qr
->qr_numregions
; ++i
, p
+= O2HB_MAX_REGION_NAME_LEN
)
1040 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN
, p
);
1043 while ((i
= find_next_bit(node_map
, O2NM_MAX_NODES
,
1044 i
+ 1)) < O2NM_MAX_NODES
) {
1045 if (i
== dlm
->node_num
)
1048 mlog(0, "Sending regions to node %d\n", i
);
1050 ret
= o2net_send_message(DLM_QUERY_REGION
, DLM_MOD_KEY
, qr
,
1051 sizeof(struct dlm_query_region
),
1056 mlog(ML_ERROR
, "Region mismatch %d, node %d\n",
1067 static int dlm_query_region_handler(struct o2net_msg
*msg
, u32 len
,
1068 void *data
, void **ret_data
)
1070 struct dlm_query_region
*qr
;
1071 struct dlm_ctxt
*dlm
= NULL
;
1076 qr
= (struct dlm_query_region
*) msg
->buf
;
1078 mlog(0, "Node %u queries hb regions on domain %s\n", qr
->qr_node
,
1081 /* buffer used in dlm_mast_regions() */
1082 local
= kmalloc(sizeof(qr
->qr_regions
), GFP_KERNEL
);
1090 spin_lock(&dlm_domain_lock
);
1091 dlm
= __dlm_lookup_domain_full(qr
->qr_domain
, qr
->qr_namelen
);
1093 mlog(ML_ERROR
, "Node %d queried hb regions on domain %s "
1094 "before join domain\n", qr
->qr_node
, qr
->qr_domain
);
1098 spin_lock(&dlm
->spinlock
);
1100 if (dlm
->joining_node
!= qr
->qr_node
) {
1101 mlog(ML_ERROR
, "Node %d queried hb regions on domain %s "
1102 "but joining node is %d\n", qr
->qr_node
, qr
->qr_domain
,
1107 /* Support for global heartbeat was added in 1.1 */
1108 if (dlm
->dlm_locking_proto
.pv_major
== 1 &&
1109 dlm
->dlm_locking_proto
.pv_minor
== 0) {
1110 mlog(ML_ERROR
, "Node %d queried hb regions on domain %s "
1111 "but active dlm protocol is %d.%d\n", qr
->qr_node
,
1112 qr
->qr_domain
, dlm
->dlm_locking_proto
.pv_major
,
1113 dlm
->dlm_locking_proto
.pv_minor
);
1117 status
= dlm_match_regions(dlm
, qr
, local
, sizeof(qr
->qr_regions
));
1121 spin_unlock(&dlm
->spinlock
);
1122 spin_unlock(&dlm_domain_lock
);
1129 static int dlm_match_nodes(struct dlm_ctxt
*dlm
, struct dlm_query_nodeinfo
*qn
)
1131 struct o2nm_node
*local
;
1132 struct dlm_node_info
*remote
;
1136 for (j
= 0; j
< qn
->qn_numnodes
; ++j
)
1137 mlog(0, "Node %3d, %pI4:%u\n", qn
->qn_nodes
[j
].ni_nodenum
,
1138 &(qn
->qn_nodes
[j
].ni_ipv4_address
),
1139 ntohs(qn
->qn_nodes
[j
].ni_ipv4_port
));
1141 for (i
= 0; i
< O2NM_MAX_NODES
&& !status
; ++i
) {
1142 local
= o2nm_get_node_by_num(i
);
1144 for (j
= 0; j
< qn
->qn_numnodes
; ++j
) {
1145 if (qn
->qn_nodes
[j
].ni_nodenum
== i
) {
1146 remote
= &(qn
->qn_nodes
[j
]);
1151 if (!local
&& !remote
)
1154 if ((local
&& !remote
) || (!local
&& remote
))
1158 ((remote
->ni_nodenum
!= local
->nd_num
) ||
1159 (remote
->ni_ipv4_port
!= local
->nd_ipv4_port
) ||
1160 (remote
->ni_ipv4_address
!= local
->nd_ipv4_address
)))
1164 if (remote
&& !local
)
1165 mlog(ML_ERROR
, "Domain %s: Node %d (%pI4:%u) "
1166 "registered in joining node %d but not in "
1167 "local node %d\n", qn
->qn_domain
,
1169 &(remote
->ni_ipv4_address
),
1170 ntohs(remote
->ni_ipv4_port
),
1171 qn
->qn_nodenum
, dlm
->node_num
);
1172 if (local
&& !remote
)
1173 mlog(ML_ERROR
, "Domain %s: Node %d (%pI4:%u) "
1174 "registered in local node %d but not in "
1175 "joining node %d\n", qn
->qn_domain
,
1176 local
->nd_num
, &(local
->nd_ipv4_address
),
1177 ntohs(local
->nd_ipv4_port
),
1178 dlm
->node_num
, qn
->qn_nodenum
);
1179 BUG_ON((!local
&& !remote
));
1183 o2nm_node_put(local
);
1189 static int dlm_send_nodeinfo(struct dlm_ctxt
*dlm
, unsigned long *node_map
)
1191 struct dlm_query_nodeinfo
*qn
= NULL
;
1192 struct o2nm_node
*node
;
1193 int ret
= 0, status
, count
, i
;
1195 if (find_next_bit(node_map
, O2NM_MAX_NODES
, 0) >= O2NM_MAX_NODES
)
1198 qn
= kzalloc(sizeof(struct dlm_query_nodeinfo
), GFP_KERNEL
);
1205 for (i
= 0, count
= 0; i
< O2NM_MAX_NODES
; ++i
) {
1206 node
= o2nm_get_node_by_num(i
);
1209 qn
->qn_nodes
[count
].ni_nodenum
= node
->nd_num
;
1210 qn
->qn_nodes
[count
].ni_ipv4_port
= node
->nd_ipv4_port
;
1211 qn
->qn_nodes
[count
].ni_ipv4_address
= node
->nd_ipv4_address
;
1212 mlog(0, "Node %3d, %pI4:%u\n", node
->nd_num
,
1213 &(node
->nd_ipv4_address
), ntohs(node
->nd_ipv4_port
));
1215 o2nm_node_put(node
);
1218 qn
->qn_nodenum
= dlm
->node_num
;
1219 qn
->qn_numnodes
= count
;
1220 qn
->qn_namelen
= strlen(dlm
->name
);
1221 memcpy(qn
->qn_domain
, dlm
->name
, qn
->qn_namelen
);
1224 while ((i
= find_next_bit(node_map
, O2NM_MAX_NODES
,
1225 i
+ 1)) < O2NM_MAX_NODES
) {
1226 if (i
== dlm
->node_num
)
1229 mlog(0, "Sending nodeinfo to node %d\n", i
);
1231 ret
= o2net_send_message(DLM_QUERY_NODEINFO
, DLM_MOD_KEY
,
1232 qn
, sizeof(struct dlm_query_nodeinfo
),
1237 mlog(ML_ERROR
, "node mismatch %d, node %d\n", ret
, i
);
1247 static int dlm_query_nodeinfo_handler(struct o2net_msg
*msg
, u32 len
,
1248 void *data
, void **ret_data
)
1250 struct dlm_query_nodeinfo
*qn
;
1251 struct dlm_ctxt
*dlm
= NULL
;
1252 int locked
= 0, status
= -EINVAL
;
1254 qn
= (struct dlm_query_nodeinfo
*) msg
->buf
;
1256 mlog(0, "Node %u queries nodes on domain %s\n", qn
->qn_nodenum
,
1259 spin_lock(&dlm_domain_lock
);
1260 dlm
= __dlm_lookup_domain_full(qn
->qn_domain
, qn
->qn_namelen
);
1262 mlog(ML_ERROR
, "Node %d queried nodes on domain %s before "
1263 "join domain\n", qn
->qn_nodenum
, qn
->qn_domain
);
1267 spin_lock(&dlm
->spinlock
);
1269 if (dlm
->joining_node
!= qn
->qn_nodenum
) {
1270 mlog(ML_ERROR
, "Node %d queried nodes on domain %s but "
1271 "joining node is %d\n", qn
->qn_nodenum
, qn
->qn_domain
,
1276 /* Support for node query was added in 1.1 */
1277 if (dlm
->dlm_locking_proto
.pv_major
== 1 &&
1278 dlm
->dlm_locking_proto
.pv_minor
== 0) {
1279 mlog(ML_ERROR
, "Node %d queried nodes on domain %s "
1280 "but active dlm protocol is %d.%d\n", qn
->qn_nodenum
,
1281 qn
->qn_domain
, dlm
->dlm_locking_proto
.pv_major
,
1282 dlm
->dlm_locking_proto
.pv_minor
);
1286 status
= dlm_match_nodes(dlm
, qn
);
1290 spin_unlock(&dlm
->spinlock
);
1291 spin_unlock(&dlm_domain_lock
);
1296 static int dlm_cancel_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
1299 struct dlm_cancel_join
*cancel
;
1300 struct dlm_ctxt
*dlm
= NULL
;
1302 cancel
= (struct dlm_cancel_join
*) msg
->buf
;
1304 mlog(0, "node %u cancels join on domain %s\n", cancel
->node_idx
,
1307 spin_lock(&dlm_domain_lock
);
1308 dlm
= __dlm_lookup_domain_full(cancel
->domain
, cancel
->name_len
);
1311 spin_lock(&dlm
->spinlock
);
1313 /* Yikes, this guy wants to cancel his join. No
1314 * problem, we simply cleanup our join state. */
1315 BUG_ON(dlm
->joining_node
!= cancel
->node_idx
);
1316 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
1318 spin_unlock(&dlm
->spinlock
);
1320 spin_unlock(&dlm_domain_lock
);
1325 static int dlm_send_one_join_cancel(struct dlm_ctxt
*dlm
,
1329 struct dlm_cancel_join cancel_msg
;
1331 memset(&cancel_msg
, 0, sizeof(cancel_msg
));
1332 cancel_msg
.node_idx
= dlm
->node_num
;
1333 cancel_msg
.name_len
= strlen(dlm
->name
);
1334 memcpy(cancel_msg
.domain
, dlm
->name
, cancel_msg
.name_len
);
1336 status
= o2net_send_message(DLM_CANCEL_JOIN_MSG
, DLM_MOD_KEY
,
1337 &cancel_msg
, sizeof(cancel_msg
), node
,
1340 mlog(ML_ERROR
, "Error %d when sending message %u (key 0x%x) to "
1341 "node %u\n", status
, DLM_CANCEL_JOIN_MSG
, DLM_MOD_KEY
,
1350 /* map_size should be in bytes. */
1351 static int dlm_send_join_cancels(struct dlm_ctxt
*dlm
,
1352 unsigned long *node_map
,
1353 unsigned int map_size
)
1355 int status
, tmpstat
;
1358 if (map_size
!= (BITS_TO_LONGS(O2NM_MAX_NODES
) *
1359 sizeof(unsigned long))) {
1361 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1362 map_size
, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES
));
1368 while ((node
= find_next_bit(node_map
, O2NM_MAX_NODES
,
1369 node
+ 1)) < O2NM_MAX_NODES
) {
1370 if (node
== dlm
->node_num
)
1373 tmpstat
= dlm_send_one_join_cancel(dlm
, node
);
1375 mlog(ML_ERROR
, "Error return %d cancelling join on "
1376 "node %d\n", tmpstat
, node
);
1387 static int dlm_request_join(struct dlm_ctxt
*dlm
,
1389 enum dlm_query_join_response_code
*response
)
1392 struct dlm_query_join_request join_msg
;
1393 struct dlm_query_join_packet packet
;
1396 mlog(0, "querying node %d\n", node
);
1398 memset(&join_msg
, 0, sizeof(join_msg
));
1399 join_msg
.node_idx
= dlm
->node_num
;
1400 join_msg
.name_len
= strlen(dlm
->name
);
1401 memcpy(join_msg
.domain
, dlm
->name
, join_msg
.name_len
);
1402 join_msg
.dlm_proto
= dlm
->dlm_locking_proto
;
1403 join_msg
.fs_proto
= dlm
->fs_locking_proto
;
1405 /* copy live node map to join message */
1406 byte_copymap(join_msg
.node_map
, dlm
->live_nodes_map
, O2NM_MAX_NODES
);
1408 status
= o2net_send_message(DLM_QUERY_JOIN_MSG
, DLM_MOD_KEY
, &join_msg
,
1409 sizeof(join_msg
), node
, &join_resp
);
1410 if (status
< 0 && status
!= -ENOPROTOOPT
) {
1411 mlog(ML_ERROR
, "Error %d when sending message %u (key 0x%x) to "
1412 "node %u\n", status
, DLM_QUERY_JOIN_MSG
, DLM_MOD_KEY
,
1416 dlm_query_join_wire_to_packet(join_resp
, &packet
);
1418 /* -ENOPROTOOPT from the net code means the other side isn't
1419 listening for our message type -- that's fine, it means
1420 his dlm isn't up, so we can consider him a 'yes' but not
1421 joined into the domain. */
1422 if (status
== -ENOPROTOOPT
) {
1424 *response
= JOIN_OK_NO_MAP
;
1425 } else if (packet
.code
== JOIN_DISALLOW
||
1426 packet
.code
== JOIN_OK_NO_MAP
) {
1427 *response
= packet
.code
;
1428 } else if (packet
.code
== JOIN_PROTOCOL_MISMATCH
) {
1430 "This node requested DLM locking protocol %u.%u and "
1431 "filesystem locking protocol %u.%u. At least one of "
1432 "the protocol versions on node %d is not compatible, "
1434 dlm
->dlm_locking_proto
.pv_major
,
1435 dlm
->dlm_locking_proto
.pv_minor
,
1436 dlm
->fs_locking_proto
.pv_major
,
1437 dlm
->fs_locking_proto
.pv_minor
,
1440 *response
= packet
.code
;
1441 } else if (packet
.code
== JOIN_OK
) {
1442 *response
= packet
.code
;
1443 /* Use the same locking protocol as the remote node */
1444 dlm
->dlm_locking_proto
.pv_minor
= packet
.dlm_minor
;
1445 dlm
->fs_locking_proto
.pv_minor
= packet
.fs_minor
;
1447 "Node %d responds JOIN_OK with DLM locking protocol "
1448 "%u.%u and fs locking protocol %u.%u\n",
1450 dlm
->dlm_locking_proto
.pv_major
,
1451 dlm
->dlm_locking_proto
.pv_minor
,
1452 dlm
->fs_locking_proto
.pv_major
,
1453 dlm
->fs_locking_proto
.pv_minor
);
1456 mlog(ML_ERROR
, "invalid response %d from node %u\n",
1460 mlog(0, "status %d, node %d response is %d\n", status
, node
,
1467 static int dlm_send_one_join_assert(struct dlm_ctxt
*dlm
,
1471 struct dlm_assert_joined assert_msg
;
1473 mlog(0, "Sending join assert to node %u\n", node
);
1475 memset(&assert_msg
, 0, sizeof(assert_msg
));
1476 assert_msg
.node_idx
= dlm
->node_num
;
1477 assert_msg
.name_len
= strlen(dlm
->name
);
1478 memcpy(assert_msg
.domain
, dlm
->name
, assert_msg
.name_len
);
1480 status
= o2net_send_message(DLM_ASSERT_JOINED_MSG
, DLM_MOD_KEY
,
1481 &assert_msg
, sizeof(assert_msg
), node
,
1484 mlog(ML_ERROR
, "Error %d when sending message %u (key 0x%x) to "
1485 "node %u\n", status
, DLM_ASSERT_JOINED_MSG
, DLM_MOD_KEY
,
1491 static void dlm_send_join_asserts(struct dlm_ctxt
*dlm
,
1492 unsigned long *node_map
)
1494 int status
, node
, live
;
1498 while ((node
= find_next_bit(node_map
, O2NM_MAX_NODES
,
1499 node
+ 1)) < O2NM_MAX_NODES
) {
1500 if (node
== dlm
->node_num
)
1504 /* It is very important that this message be
1505 * received so we spin until either the node
1506 * has died or it gets the message. */
1507 status
= dlm_send_one_join_assert(dlm
, node
);
1509 spin_lock(&dlm
->spinlock
);
1510 live
= test_bit(node
, dlm
->live_nodes_map
);
1511 spin_unlock(&dlm
->spinlock
);
1514 mlog(ML_ERROR
, "Error return %d asserting "
1515 "join on node %d\n", status
, node
);
1517 /* give us some time between errors... */
1519 msleep(DLM_DOMAIN_BACKOFF_MS
);
1521 } while (status
&& live
);
1525 struct domain_join_ctxt
{
1526 unsigned long live_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1527 unsigned long yes_resp_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1530 static int dlm_should_restart_join(struct dlm_ctxt
*dlm
,
1531 struct domain_join_ctxt
*ctxt
,
1532 enum dlm_query_join_response_code response
)
1536 if (response
== JOIN_DISALLOW
) {
1537 mlog(0, "Latest response of disallow -- should restart\n");
1541 spin_lock(&dlm
->spinlock
);
1542 /* For now, we restart the process if the node maps have
1544 ret
= memcmp(ctxt
->live_map
, dlm
->live_nodes_map
,
1545 sizeof(dlm
->live_nodes_map
));
1546 spin_unlock(&dlm
->spinlock
);
1549 mlog(0, "Node maps changed -- should restart\n");
1554 static int dlm_try_to_join_domain(struct dlm_ctxt
*dlm
)
1556 int status
= 0, tmpstat
, node
;
1557 struct domain_join_ctxt
*ctxt
;
1558 enum dlm_query_join_response_code response
= JOIN_DISALLOW
;
1562 ctxt
= kzalloc(sizeof(*ctxt
), GFP_KERNEL
);
1569 /* group sem locking should work for us here -- we're already
1570 * registered for heartbeat events so filling this should be
1571 * atomic wrt getting those handlers called. */
1572 o2hb_fill_node_map(dlm
->live_nodes_map
, sizeof(dlm
->live_nodes_map
));
1574 spin_lock(&dlm
->spinlock
);
1575 memcpy(ctxt
->live_map
, dlm
->live_nodes_map
, sizeof(ctxt
->live_map
));
1577 __dlm_set_joining_node(dlm
, dlm
->node_num
);
1579 spin_unlock(&dlm
->spinlock
);
1582 while ((node
= find_next_bit(ctxt
->live_map
, O2NM_MAX_NODES
,
1583 node
+ 1)) < O2NM_MAX_NODES
) {
1584 if (node
== dlm
->node_num
)
1587 status
= dlm_request_join(dlm
, node
, &response
);
1593 /* Ok, either we got a response or the node doesn't have a
1595 if (response
== JOIN_OK
)
1596 set_bit(node
, ctxt
->yes_resp_map
);
1598 if (dlm_should_restart_join(dlm
, ctxt
, response
)) {
1604 mlog(0, "Yay, done querying nodes!\n");
1606 /* Yay, everyone agree's we can join the domain. My domain is
1607 * comprised of all nodes who were put in the
1608 * yes_resp_map. Copy that into our domain map and send a join
1609 * assert message to clean up everyone elses state. */
1610 spin_lock(&dlm
->spinlock
);
1611 memcpy(dlm
->domain_map
, ctxt
->yes_resp_map
,
1612 sizeof(ctxt
->yes_resp_map
));
1613 set_bit(dlm
->node_num
, dlm
->domain_map
);
1614 spin_unlock(&dlm
->spinlock
);
1616 /* Support for global heartbeat and node info was added in 1.1 */
1617 if (dlm_protocol
.pv_major
> 1 || dlm_protocol
.pv_minor
> 0) {
1618 status
= dlm_send_nodeinfo(dlm
, ctxt
->yes_resp_map
);
1623 status
= dlm_send_regions(dlm
, ctxt
->yes_resp_map
);
1630 dlm_send_join_asserts(dlm
, ctxt
->yes_resp_map
);
1632 /* Joined state *must* be set before the joining node
1633 * information, otherwise the query_join handler may read no
1634 * current joiner but a state of NEW and tell joining nodes
1635 * we're not in the domain. */
1636 spin_lock(&dlm_domain_lock
);
1637 dlm
->dlm_state
= DLM_CTXT_JOINED
;
1639 spin_unlock(&dlm_domain_lock
);
1642 spin_lock(&dlm
->spinlock
);
1643 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
1645 __dlm_print_nodes(dlm
);
1646 spin_unlock(&dlm
->spinlock
);
1649 /* Do we need to send a cancel message to any nodes? */
1651 tmpstat
= dlm_send_join_cancels(dlm
,
1653 sizeof(ctxt
->yes_resp_map
));
1655 mlog_errno(tmpstat
);
1660 mlog(0, "returning %d\n", status
);
1664 static void dlm_unregister_domain_handlers(struct dlm_ctxt
*dlm
)
1666 o2hb_unregister_callback(dlm
->name
, &dlm
->dlm_hb_up
);
1667 o2hb_unregister_callback(dlm
->name
, &dlm
->dlm_hb_down
);
1668 o2net_unregister_handler_list(&dlm
->dlm_domain_handlers
);
1671 static int dlm_register_domain_handlers(struct dlm_ctxt
*dlm
)
1675 mlog(0, "registering handlers.\n");
1677 o2hb_setup_callback(&dlm
->dlm_hb_down
, O2HB_NODE_DOWN_CB
,
1678 dlm_hb_node_down_cb
, dlm
, DLM_HB_NODE_DOWN_PRI
);
1679 status
= o2hb_register_callback(dlm
->name
, &dlm
->dlm_hb_down
);
1683 o2hb_setup_callback(&dlm
->dlm_hb_up
, O2HB_NODE_UP_CB
,
1684 dlm_hb_node_up_cb
, dlm
, DLM_HB_NODE_UP_PRI
);
1685 status
= o2hb_register_callback(dlm
->name
, &dlm
->dlm_hb_up
);
1689 status
= o2net_register_handler(DLM_MASTER_REQUEST_MSG
, dlm
->key
,
1690 sizeof(struct dlm_master_request
),
1691 dlm_master_request_handler
,
1692 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1696 status
= o2net_register_handler(DLM_ASSERT_MASTER_MSG
, dlm
->key
,
1697 sizeof(struct dlm_assert_master
),
1698 dlm_assert_master_handler
,
1699 dlm
, dlm_assert_master_post_handler
,
1700 &dlm
->dlm_domain_handlers
);
1704 status
= o2net_register_handler(DLM_CREATE_LOCK_MSG
, dlm
->key
,
1705 sizeof(struct dlm_create_lock
),
1706 dlm_create_lock_handler
,
1707 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1711 status
= o2net_register_handler(DLM_CONVERT_LOCK_MSG
, dlm
->key
,
1712 DLM_CONVERT_LOCK_MAX_LEN
,
1713 dlm_convert_lock_handler
,
1714 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1718 status
= o2net_register_handler(DLM_UNLOCK_LOCK_MSG
, dlm
->key
,
1719 DLM_UNLOCK_LOCK_MAX_LEN
,
1720 dlm_unlock_lock_handler
,
1721 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1725 status
= o2net_register_handler(DLM_PROXY_AST_MSG
, dlm
->key
,
1726 DLM_PROXY_AST_MAX_LEN
,
1727 dlm_proxy_ast_handler
,
1728 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1732 status
= o2net_register_handler(DLM_EXIT_DOMAIN_MSG
, dlm
->key
,
1733 sizeof(struct dlm_exit_domain
),
1734 dlm_exit_domain_handler
,
1735 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1739 status
= o2net_register_handler(DLM_DEREF_LOCKRES_MSG
, dlm
->key
,
1740 sizeof(struct dlm_deref_lockres
),
1741 dlm_deref_lockres_handler
,
1742 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1746 status
= o2net_register_handler(DLM_MIGRATE_REQUEST_MSG
, dlm
->key
,
1747 sizeof(struct dlm_migrate_request
),
1748 dlm_migrate_request_handler
,
1749 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1753 status
= o2net_register_handler(DLM_MIG_LOCKRES_MSG
, dlm
->key
,
1754 DLM_MIG_LOCKRES_MAX_LEN
,
1755 dlm_mig_lockres_handler
,
1756 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1760 status
= o2net_register_handler(DLM_MASTER_REQUERY_MSG
, dlm
->key
,
1761 sizeof(struct dlm_master_requery
),
1762 dlm_master_requery_handler
,
1763 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1767 status
= o2net_register_handler(DLM_LOCK_REQUEST_MSG
, dlm
->key
,
1768 sizeof(struct dlm_lock_request
),
1769 dlm_request_all_locks_handler
,
1770 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1774 status
= o2net_register_handler(DLM_RECO_DATA_DONE_MSG
, dlm
->key
,
1775 sizeof(struct dlm_reco_data_done
),
1776 dlm_reco_data_done_handler
,
1777 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1781 status
= o2net_register_handler(DLM_BEGIN_RECO_MSG
, dlm
->key
,
1782 sizeof(struct dlm_begin_reco
),
1783 dlm_begin_reco_handler
,
1784 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1788 status
= o2net_register_handler(DLM_FINALIZE_RECO_MSG
, dlm
->key
,
1789 sizeof(struct dlm_finalize_reco
),
1790 dlm_finalize_reco_handler
,
1791 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1797 dlm_unregister_domain_handlers(dlm
);
1802 static int dlm_join_domain(struct dlm_ctxt
*dlm
)
1805 unsigned int backoff
;
1806 unsigned int total_backoff
= 0;
1810 mlog(0, "Join domain %s\n", dlm
->name
);
1812 status
= dlm_register_domain_handlers(dlm
);
1818 status
= dlm_debug_init(dlm
);
1824 status
= dlm_launch_thread(dlm
);
1830 status
= dlm_launch_recovery_thread(dlm
);
1836 dlm
->dlm_worker
= create_singlethread_workqueue("dlm_wq");
1837 if (!dlm
->dlm_worker
) {
1844 status
= dlm_try_to_join_domain(dlm
);
1846 /* If we're racing another node to the join, then we
1847 * need to back off temporarily and let them
1849 #define DLM_JOIN_TIMEOUT_MSECS 90000
1850 if (status
== -EAGAIN
) {
1851 if (signal_pending(current
)) {
1852 status
= -ERESTARTSYS
;
1857 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS
)) {
1858 status
= -ERESTARTSYS
;
1859 mlog(ML_NOTICE
, "Timed out joining dlm domain "
1860 "%s after %u msecs\n", dlm
->name
,
1861 jiffies_to_msecs(total_backoff
));
1867 * <dale> No, after you!
1869 * <dale> But you first!
1872 backoff
= (unsigned int)(jiffies
& 0x3);
1873 backoff
*= DLM_DOMAIN_BACKOFF_MS
;
1874 total_backoff
+= backoff
;
1875 mlog(0, "backoff %d\n", backoff
);
1878 } while (status
== -EAGAIN
);
1887 wake_up(&dlm_domain_events
);
1890 dlm_unregister_domain_handlers(dlm
);
1891 dlm_debug_shutdown(dlm
);
1892 dlm_complete_thread(dlm
);
1893 dlm_complete_recovery_thread(dlm
);
1894 dlm_destroy_dlm_worker(dlm
);
1900 static struct dlm_ctxt
*dlm_alloc_ctxt(const char *domain
,
1905 struct dlm_ctxt
*dlm
= NULL
;
1907 dlm
= kzalloc(sizeof(*dlm
), GFP_KERNEL
);
1909 mlog_errno(-ENOMEM
);
1913 dlm
->name
= kstrdup(domain
, GFP_KERNEL
);
1914 if (dlm
->name
== NULL
) {
1915 mlog_errno(-ENOMEM
);
1921 dlm
->lockres_hash
= (struct hlist_head
**)dlm_alloc_pagevec(DLM_HASH_PAGES
);
1922 if (!dlm
->lockres_hash
) {
1923 mlog_errno(-ENOMEM
);
1930 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++)
1931 INIT_HLIST_HEAD(dlm_lockres_hash(dlm
, i
));
1933 dlm
->master_hash
= (struct hlist_head
**)
1934 dlm_alloc_pagevec(DLM_HASH_PAGES
);
1935 if (!dlm
->master_hash
) {
1936 mlog_errno(-ENOMEM
);
1937 dlm_free_pagevec((void **)dlm
->lockres_hash
, DLM_HASH_PAGES
);
1944 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++)
1945 INIT_HLIST_HEAD(dlm_master_hash(dlm
, i
));
1948 dlm
->node_num
= o2nm_this_node();
1950 ret
= dlm_create_debugfs_subroot(dlm
);
1952 dlm_free_pagevec((void **)dlm
->master_hash
, DLM_HASH_PAGES
);
1953 dlm_free_pagevec((void **)dlm
->lockres_hash
, DLM_HASH_PAGES
);
1960 spin_lock_init(&dlm
->spinlock
);
1961 spin_lock_init(&dlm
->master_lock
);
1962 spin_lock_init(&dlm
->ast_lock
);
1963 spin_lock_init(&dlm
->track_lock
);
1964 INIT_LIST_HEAD(&dlm
->list
);
1965 INIT_LIST_HEAD(&dlm
->dirty_list
);
1966 INIT_LIST_HEAD(&dlm
->reco
.resources
);
1967 INIT_LIST_HEAD(&dlm
->reco
.received
);
1968 INIT_LIST_HEAD(&dlm
->reco
.node_data
);
1969 INIT_LIST_HEAD(&dlm
->purge_list
);
1970 INIT_LIST_HEAD(&dlm
->dlm_domain_handlers
);
1971 INIT_LIST_HEAD(&dlm
->tracking_list
);
1972 dlm
->reco
.state
= 0;
1974 INIT_LIST_HEAD(&dlm
->pending_asts
);
1975 INIT_LIST_HEAD(&dlm
->pending_basts
);
1977 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1978 dlm
->recovery_map
, &(dlm
->recovery_map
[0]));
1980 memset(dlm
->recovery_map
, 0, sizeof(dlm
->recovery_map
));
1981 memset(dlm
->live_nodes_map
, 0, sizeof(dlm
->live_nodes_map
));
1982 memset(dlm
->domain_map
, 0, sizeof(dlm
->domain_map
));
1984 dlm
->dlm_thread_task
= NULL
;
1985 dlm
->dlm_reco_thread_task
= NULL
;
1986 dlm
->dlm_worker
= NULL
;
1987 init_waitqueue_head(&dlm
->dlm_thread_wq
);
1988 init_waitqueue_head(&dlm
->dlm_reco_thread_wq
);
1989 init_waitqueue_head(&dlm
->reco
.event
);
1990 init_waitqueue_head(&dlm
->ast_wq
);
1991 init_waitqueue_head(&dlm
->migration_wq
);
1992 INIT_LIST_HEAD(&dlm
->mle_hb_events
);
1994 dlm
->joining_node
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1995 init_waitqueue_head(&dlm
->dlm_join_events
);
1997 dlm
->reco
.new_master
= O2NM_INVALID_NODE_NUM
;
1998 dlm
->reco
.dead_node
= O2NM_INVALID_NODE_NUM
;
2000 atomic_set(&dlm
->res_tot_count
, 0);
2001 atomic_set(&dlm
->res_cur_count
, 0);
2002 for (i
= 0; i
< DLM_MLE_NUM_TYPES
; ++i
) {
2003 atomic_set(&dlm
->mle_tot_count
[i
], 0);
2004 atomic_set(&dlm
->mle_cur_count
[i
], 0);
2007 spin_lock_init(&dlm
->work_lock
);
2008 INIT_LIST_HEAD(&dlm
->work_list
);
2009 INIT_WORK(&dlm
->dispatched_work
, dlm_dispatch_work
);
2011 kref_init(&dlm
->dlm_refs
);
2012 dlm
->dlm_state
= DLM_CTXT_NEW
;
2014 INIT_LIST_HEAD(&dlm
->dlm_eviction_callbacks
);
2016 mlog(0, "context init: refcount %u\n",
2017 atomic_read(&dlm
->dlm_refs
.refcount
));
2024 * Compare a requested locking protocol version against the current one.
2026 * If the major numbers are different, they are incompatible.
2027 * If the current minor is greater than the request, they are incompatible.
2028 * If the current minor is less than or equal to the request, they are
2029 * compatible, and the requester should run at the current minor version.
2031 static int dlm_protocol_compare(struct dlm_protocol_version
*existing
,
2032 struct dlm_protocol_version
*request
)
2034 if (existing
->pv_major
!= request
->pv_major
)
2037 if (existing
->pv_minor
> request
->pv_minor
)
2040 if (existing
->pv_minor
< request
->pv_minor
)
2041 request
->pv_minor
= existing
->pv_minor
;
2047 * dlm_register_domain: one-time setup per "domain".
2049 * The filesystem passes in the requested locking version via proto.
2050 * If registration was successful, proto will contain the negotiated
2053 struct dlm_ctxt
* dlm_register_domain(const char *domain
,
2055 struct dlm_protocol_version
*fs_proto
)
2058 struct dlm_ctxt
*dlm
= NULL
;
2059 struct dlm_ctxt
*new_ctxt
= NULL
;
2061 if (strlen(domain
) >= O2NM_MAX_NAME_LEN
) {
2062 ret
= -ENAMETOOLONG
;
2063 mlog(ML_ERROR
, "domain name length too long\n");
2067 if (!o2hb_check_local_node_heartbeating()) {
2068 mlog(ML_ERROR
, "the local node has not been configured, or is "
2069 "not heartbeating\n");
2074 mlog(0, "register called for domain \"%s\"\n", domain
);
2078 if (signal_pending(current
)) {
2084 spin_lock(&dlm_domain_lock
);
2086 dlm
= __dlm_lookup_domain(domain
);
2088 if (dlm
->dlm_state
!= DLM_CTXT_JOINED
) {
2089 spin_unlock(&dlm_domain_lock
);
2091 mlog(0, "This ctxt is not joined yet!\n");
2092 wait_event_interruptible(dlm_domain_events
,
2093 dlm_wait_on_domain_helper(
2098 if (dlm_protocol_compare(&dlm
->fs_locking_proto
, fs_proto
)) {
2099 spin_unlock(&dlm_domain_lock
);
2101 "Requested locking protocol version is not "
2102 "compatible with already registered domain "
2103 "\"%s\"\n", domain
);
2111 spin_unlock(&dlm_domain_lock
);
2119 spin_unlock(&dlm_domain_lock
);
2121 new_ctxt
= dlm_alloc_ctxt(domain
, key
);
2130 /* a little variable switch-a-roo here... */
2134 /* add the new domain */
2135 list_add_tail(&dlm
->list
, &dlm_domains
);
2136 spin_unlock(&dlm_domain_lock
);
2139 * Pass the locking protocol version into the join. If the join
2140 * succeeds, it will have the negotiated protocol set.
2142 dlm
->dlm_locking_proto
= dlm_protocol
;
2143 dlm
->fs_locking_proto
= *fs_proto
;
2145 ret
= dlm_join_domain(dlm
);
2152 /* Tell the caller what locking protocol we negotiated */
2153 *fs_proto
= dlm
->fs_locking_proto
;
2158 dlm_free_ctxt_mem(new_ctxt
);
2165 EXPORT_SYMBOL_GPL(dlm_register_domain
);
2167 static LIST_HEAD(dlm_join_handlers
);
2169 static void dlm_unregister_net_handlers(void)
2171 o2net_unregister_handler_list(&dlm_join_handlers
);
2174 static int dlm_register_net_handlers(void)
2178 status
= o2net_register_handler(DLM_QUERY_JOIN_MSG
, DLM_MOD_KEY
,
2179 sizeof(struct dlm_query_join_request
),
2180 dlm_query_join_handler
,
2181 NULL
, NULL
, &dlm_join_handlers
);
2185 status
= o2net_register_handler(DLM_ASSERT_JOINED_MSG
, DLM_MOD_KEY
,
2186 sizeof(struct dlm_assert_joined
),
2187 dlm_assert_joined_handler
,
2188 NULL
, NULL
, &dlm_join_handlers
);
2192 status
= o2net_register_handler(DLM_CANCEL_JOIN_MSG
, DLM_MOD_KEY
,
2193 sizeof(struct dlm_cancel_join
),
2194 dlm_cancel_join_handler
,
2195 NULL
, NULL
, &dlm_join_handlers
);
2199 status
= o2net_register_handler(DLM_QUERY_REGION
, DLM_MOD_KEY
,
2200 sizeof(struct dlm_query_region
),
2201 dlm_query_region_handler
,
2202 NULL
, NULL
, &dlm_join_handlers
);
2207 status
= o2net_register_handler(DLM_QUERY_NODEINFO
, DLM_MOD_KEY
,
2208 sizeof(struct dlm_query_nodeinfo
),
2209 dlm_query_nodeinfo_handler
,
2210 NULL
, NULL
, &dlm_join_handlers
);
2213 dlm_unregister_net_handlers();
2218 /* Domain eviction callback handling.
2220 * The file system requires notification of node death *before* the
2221 * dlm completes it's recovery work, otherwise it may be able to
2222 * acquire locks on resources requiring recovery. Since the dlm can
2223 * evict a node from it's domain *before* heartbeat fires, a similar
2224 * mechanism is required. */
2226 /* Eviction is not expected to happen often, so a per-domain lock is
2227 * not necessary. Eviction callbacks are allowed to sleep for short
2228 * periods of time. */
2229 static DECLARE_RWSEM(dlm_callback_sem
);
2231 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt
*dlm
,
2234 struct list_head
*iter
;
2235 struct dlm_eviction_cb
*cb
;
2237 down_read(&dlm_callback_sem
);
2238 list_for_each(iter
, &dlm
->dlm_eviction_callbacks
) {
2239 cb
= list_entry(iter
, struct dlm_eviction_cb
, ec_item
);
2241 cb
->ec_func(node_num
, cb
->ec_data
);
2243 up_read(&dlm_callback_sem
);
2246 void dlm_setup_eviction_cb(struct dlm_eviction_cb
*cb
,
2247 dlm_eviction_func
*f
,
2250 INIT_LIST_HEAD(&cb
->ec_item
);
2254 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb
);
2256 void dlm_register_eviction_cb(struct dlm_ctxt
*dlm
,
2257 struct dlm_eviction_cb
*cb
)
2259 down_write(&dlm_callback_sem
);
2260 list_add_tail(&cb
->ec_item
, &dlm
->dlm_eviction_callbacks
);
2261 up_write(&dlm_callback_sem
);
2263 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb
);
2265 void dlm_unregister_eviction_cb(struct dlm_eviction_cb
*cb
)
2267 down_write(&dlm_callback_sem
);
2268 list_del_init(&cb
->ec_item
);
2269 up_write(&dlm_callback_sem
);
2271 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb
);
2273 static int __init
dlm_init(void)
2277 dlm_print_version();
2279 status
= dlm_init_mle_cache();
2281 mlog(ML_ERROR
, "Could not create o2dlm_mle slabcache\n");
2285 status
= dlm_init_master_caches();
2287 mlog(ML_ERROR
, "Could not create o2dlm_lockres and "
2288 "o2dlm_lockname slabcaches\n");
2292 status
= dlm_init_lock_cache();
2294 mlog(ML_ERROR
, "Count not create o2dlm_lock slabcache\n");
2298 status
= dlm_register_net_handlers();
2300 mlog(ML_ERROR
, "Unable to register network handlers\n");
2304 status
= dlm_create_debugfs_root();
2310 dlm_unregister_net_handlers();
2311 dlm_destroy_lock_cache();
2312 dlm_destroy_master_caches();
2313 dlm_destroy_mle_cache();
2317 static void __exit
dlm_exit (void)
2319 dlm_destroy_debugfs_root();
2320 dlm_unregister_net_handlers();
2321 dlm_destroy_lock_cache();
2322 dlm_destroy_master_caches();
2323 dlm_destroy_mle_cache();
2326 MODULE_AUTHOR("Oracle");
2327 MODULE_LICENSE("GPL");
2329 module_init(dlm_init
);
2330 module_exit(dlm_exit
);