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/utsname.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/delay.h>
35 #include <linux/err.h>
36 #include <linux/debugfs.h>
38 #include "cluster/heartbeat.h"
39 #include "cluster/nodemanager.h"
40 #include "cluster/tcp.h"
43 #include "dlmcommon.h"
44 #include "dlmdomain.h"
49 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
50 #include "cluster/masklog.h"
53 * ocfs2 node maps are array of long int, which limits to send them freely
54 * across the wire due to endianness issues. To workaround this, we convert
55 * long ints to byte arrays. Following 3 routines are helper functions to
56 * set/test/copy bits within those array of bytes
58 static inline void byte_set_bit(u8 nr
, u8 map
[])
60 map
[nr
>> 3] |= (1UL << (nr
& 7));
63 static inline int byte_test_bit(u8 nr
, u8 map
[])
65 return ((1UL << (nr
& 7)) & (map
[nr
>> 3])) != 0;
68 static inline void byte_copymap(u8 dmap
[], unsigned long smap
[],
76 memset(dmap
, 0, ((sz
+ 7) >> 3));
77 for (nn
= 0 ; nn
< sz
; nn
++)
78 if (test_bit(nn
, smap
))
79 byte_set_bit(nn
, dmap
);
82 static void dlm_free_pagevec(void **vec
, int pages
)
85 free_page((unsigned long)vec
[pages
]);
89 static void **dlm_alloc_pagevec(int pages
)
91 void **vec
= kmalloc(pages
* sizeof(void *), GFP_KERNEL
);
97 for (i
= 0; i
< pages
; i
++)
98 if (!(vec
[i
] = (void *)__get_free_page(GFP_KERNEL
)))
101 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
102 pages
, (unsigned long)DLM_HASH_PAGES
,
103 (unsigned long)DLM_BUCKETS_PER_PAGE
);
106 dlm_free_pagevec(vec
, i
);
112 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
114 * struct dlm_ctxt->spinlock
115 * struct dlm_lock_resource->spinlock
116 * struct dlm_ctxt->master_lock
117 * struct dlm_ctxt->ast_lock
118 * dlm_master_list_entry->spinlock
123 DEFINE_SPINLOCK(dlm_domain_lock
);
124 LIST_HEAD(dlm_domains
);
125 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events
);
128 * The supported protocol version for DLM communication. Running domains
129 * will have a negotiated version with the same major number and a minor
130 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
131 * be used to determine what a running domain is actually using.
133 static const struct dlm_protocol_version dlm_protocol
= {
138 #define DLM_DOMAIN_BACKOFF_MS 200
140 static int dlm_query_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
142 static int dlm_assert_joined_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
144 static int dlm_cancel_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
146 static int dlm_exit_domain_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
148 static int dlm_protocol_compare(struct dlm_protocol_version
*existing
,
149 struct dlm_protocol_version
*request
);
151 static void dlm_unregister_domain_handlers(struct dlm_ctxt
*dlm
);
153 void __dlm_unhash_lockres(struct dlm_lock_resource
*lockres
)
155 if (!hlist_unhashed(&lockres
->hash_node
)) {
156 hlist_del_init(&lockres
->hash_node
);
157 dlm_lockres_put(lockres
);
161 void __dlm_insert_lockres(struct dlm_ctxt
*dlm
,
162 struct dlm_lock_resource
*res
)
164 struct hlist_head
*bucket
;
167 assert_spin_locked(&dlm
->spinlock
);
170 bucket
= dlm_lockres_hash(dlm
, q
->hash
);
172 /* get a reference for our hashtable */
173 dlm_lockres_get(res
);
175 hlist_add_head(&res
->hash_node
, bucket
);
178 struct dlm_lock_resource
* __dlm_lookup_lockres_full(struct dlm_ctxt
*dlm
,
183 struct hlist_head
*bucket
;
184 struct hlist_node
*list
;
186 mlog_entry("%.*s\n", len
, name
);
188 assert_spin_locked(&dlm
->spinlock
);
190 bucket
= dlm_lockres_hash(dlm
, hash
);
192 hlist_for_each(list
, bucket
) {
193 struct dlm_lock_resource
*res
= hlist_entry(list
,
194 struct dlm_lock_resource
, hash_node
);
195 if (res
->lockname
.name
[0] != name
[0])
197 if (unlikely(res
->lockname
.len
!= len
))
199 if (memcmp(res
->lockname
.name
+ 1, name
+ 1, len
- 1))
201 dlm_lockres_get(res
);
207 /* intended to be called by functions which do not care about lock
208 * resources which are being purged (most net _handler functions).
209 * this will return NULL for any lock resource which is found but
210 * currently in the process of dropping its mastery reference.
211 * use __dlm_lookup_lockres_full when you need the lock resource
212 * regardless (e.g. dlm_get_lock_resource) */
213 struct dlm_lock_resource
* __dlm_lookup_lockres(struct dlm_ctxt
*dlm
,
218 struct dlm_lock_resource
*res
= NULL
;
220 mlog_entry("%.*s\n", len
, name
);
222 assert_spin_locked(&dlm
->spinlock
);
224 res
= __dlm_lookup_lockres_full(dlm
, name
, len
, hash
);
226 spin_lock(&res
->spinlock
);
227 if (res
->state
& DLM_LOCK_RES_DROPPING_REF
) {
228 spin_unlock(&res
->spinlock
);
229 dlm_lockres_put(res
);
232 spin_unlock(&res
->spinlock
);
238 struct dlm_lock_resource
* dlm_lookup_lockres(struct dlm_ctxt
*dlm
,
242 struct dlm_lock_resource
*res
;
243 unsigned int hash
= dlm_lockid_hash(name
, len
);
245 spin_lock(&dlm
->spinlock
);
246 res
= __dlm_lookup_lockres(dlm
, name
, len
, hash
);
247 spin_unlock(&dlm
->spinlock
);
251 static struct dlm_ctxt
* __dlm_lookup_domain_full(const char *domain
, int len
)
253 struct dlm_ctxt
*tmp
= NULL
;
254 struct list_head
*iter
;
256 assert_spin_locked(&dlm_domain_lock
);
258 /* tmp->name here is always NULL terminated,
259 * but domain may not be! */
260 list_for_each(iter
, &dlm_domains
) {
261 tmp
= list_entry (iter
, struct dlm_ctxt
, list
);
262 if (strlen(tmp
->name
) == len
&&
263 memcmp(tmp
->name
, domain
, len
)==0)
271 /* For null terminated domain strings ONLY */
272 static struct dlm_ctxt
* __dlm_lookup_domain(const char *domain
)
274 assert_spin_locked(&dlm_domain_lock
);
276 return __dlm_lookup_domain_full(domain
, strlen(domain
));
280 /* returns true on one of two conditions:
281 * 1) the domain does not exist
282 * 2) the domain exists and it's state is "joined" */
283 static int dlm_wait_on_domain_helper(const char *domain
)
286 struct dlm_ctxt
*tmp
= NULL
;
288 spin_lock(&dlm_domain_lock
);
290 tmp
= __dlm_lookup_domain(domain
);
293 else if (tmp
->dlm_state
== DLM_CTXT_JOINED
)
296 spin_unlock(&dlm_domain_lock
);
300 static void dlm_free_ctxt_mem(struct dlm_ctxt
*dlm
)
302 dlm_destroy_debugfs_subroot(dlm
);
304 if (dlm
->lockres_hash
)
305 dlm_free_pagevec((void **)dlm
->lockres_hash
, DLM_HASH_PAGES
);
313 /* A little strange - this function will be called while holding
314 * dlm_domain_lock and is expected to be holding it on the way out. We
315 * will however drop and reacquire it multiple times */
316 static void dlm_ctxt_release(struct kref
*kref
)
318 struct dlm_ctxt
*dlm
;
320 dlm
= container_of(kref
, struct dlm_ctxt
, dlm_refs
);
322 BUG_ON(dlm
->num_joins
);
323 BUG_ON(dlm
->dlm_state
== DLM_CTXT_JOINED
);
325 /* we may still be in the list if we hit an error during join. */
326 list_del_init(&dlm
->list
);
328 spin_unlock(&dlm_domain_lock
);
330 mlog(0, "freeing memory from domain %s\n", dlm
->name
);
332 wake_up(&dlm_domain_events
);
334 dlm_free_ctxt_mem(dlm
);
336 spin_lock(&dlm_domain_lock
);
339 void dlm_put(struct dlm_ctxt
*dlm
)
341 spin_lock(&dlm_domain_lock
);
342 kref_put(&dlm
->dlm_refs
, dlm_ctxt_release
);
343 spin_unlock(&dlm_domain_lock
);
346 static void __dlm_get(struct dlm_ctxt
*dlm
)
348 kref_get(&dlm
->dlm_refs
);
351 /* given a questionable reference to a dlm object, gets a reference if
352 * it can find it in the list, otherwise returns NULL in which case
353 * you shouldn't trust your pointer. */
354 struct dlm_ctxt
*dlm_grab(struct dlm_ctxt
*dlm
)
356 struct list_head
*iter
;
357 struct dlm_ctxt
*target
= NULL
;
359 spin_lock(&dlm_domain_lock
);
361 list_for_each(iter
, &dlm_domains
) {
362 target
= list_entry (iter
, struct dlm_ctxt
, list
);
372 spin_unlock(&dlm_domain_lock
);
377 int dlm_domain_fully_joined(struct dlm_ctxt
*dlm
)
381 spin_lock(&dlm_domain_lock
);
382 ret
= (dlm
->dlm_state
== DLM_CTXT_JOINED
) ||
383 (dlm
->dlm_state
== DLM_CTXT_IN_SHUTDOWN
);
384 spin_unlock(&dlm_domain_lock
);
389 static void dlm_destroy_dlm_worker(struct dlm_ctxt
*dlm
)
391 if (dlm
->dlm_worker
) {
392 flush_workqueue(dlm
->dlm_worker
);
393 destroy_workqueue(dlm
->dlm_worker
);
394 dlm
->dlm_worker
= NULL
;
398 static void dlm_complete_dlm_shutdown(struct dlm_ctxt
*dlm
)
400 dlm_unregister_domain_handlers(dlm
);
401 dlm_debug_shutdown(dlm
);
402 dlm_complete_thread(dlm
);
403 dlm_complete_recovery_thread(dlm
);
404 dlm_destroy_dlm_worker(dlm
);
406 /* We've left the domain. Now we can take ourselves out of the
407 * list and allow the kref stuff to help us free the
409 spin_lock(&dlm_domain_lock
);
410 list_del_init(&dlm
->list
);
411 spin_unlock(&dlm_domain_lock
);
413 /* Wake up anyone waiting for us to remove this domain */
414 wake_up(&dlm_domain_events
);
417 static int dlm_migrate_all_locks(struct dlm_ctxt
*dlm
)
419 int i
, num
, n
, ret
= 0;
420 struct dlm_lock_resource
*res
;
421 struct hlist_node
*iter
;
422 struct hlist_head
*bucket
;
425 mlog(0, "Migrating locks from domain %s\n", dlm
->name
);
428 spin_lock(&dlm
->spinlock
);
429 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++) {
432 bucket
= dlm_lockres_hash(dlm
, i
);
433 iter
= bucket
->first
;
436 res
= hlist_entry(iter
, struct dlm_lock_resource
,
438 dlm_lockres_get(res
);
439 /* migrate, if necessary. this will drop the dlm
440 * spinlock and retake it if it does migration. */
441 dropped
= dlm_empty_lockres(dlm
, res
);
443 spin_lock(&res
->spinlock
);
444 __dlm_lockres_calc_usage(dlm
, res
);
445 iter
= res
->hash_node
.next
;
446 spin_unlock(&res
->spinlock
);
448 dlm_lockres_put(res
);
453 cond_resched_lock(&dlm
->spinlock
);
455 mlog(0, "%s: touched %d lockreses in bucket %d "
456 "(tot=%d)\n", dlm
->name
, n
, i
, num
);
458 spin_unlock(&dlm
->spinlock
);
459 wake_up(&dlm
->dlm_thread_wq
);
461 /* let the dlm thread take care of purging, keep scanning until
462 * nothing remains in the hash */
464 mlog(0, "%s: %d lock resources in hash last pass\n",
468 mlog(0, "DONE Migrating locks from domain %s\n", dlm
->name
);
472 static int dlm_no_joining_node(struct dlm_ctxt
*dlm
)
476 spin_lock(&dlm
->spinlock
);
477 ret
= dlm
->joining_node
== DLM_LOCK_RES_OWNER_UNKNOWN
;
478 spin_unlock(&dlm
->spinlock
);
483 static void dlm_mark_domain_leaving(struct dlm_ctxt
*dlm
)
485 /* Yikes, a double spinlock! I need domain_lock for the dlm
486 * state and the dlm spinlock for join state... Sorry! */
488 spin_lock(&dlm_domain_lock
);
489 spin_lock(&dlm
->spinlock
);
491 if (dlm
->joining_node
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
492 mlog(0, "Node %d is joining, we wait on it.\n",
494 spin_unlock(&dlm
->spinlock
);
495 spin_unlock(&dlm_domain_lock
);
497 wait_event(dlm
->dlm_join_events
, dlm_no_joining_node(dlm
));
501 dlm
->dlm_state
= DLM_CTXT_LEAVING
;
502 spin_unlock(&dlm
->spinlock
);
503 spin_unlock(&dlm_domain_lock
);
506 static void __dlm_print_nodes(struct dlm_ctxt
*dlm
)
510 assert_spin_locked(&dlm
->spinlock
);
512 printk(KERN_INFO
"ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm
->name
);
514 while ((node
= find_next_bit(dlm
->domain_map
, O2NM_MAX_NODES
,
515 node
+ 1)) < O2NM_MAX_NODES
) {
521 static int dlm_exit_domain_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
524 struct dlm_ctxt
*dlm
= data
;
526 struct dlm_exit_domain
*exit_msg
= (struct dlm_exit_domain
*) msg
->buf
;
528 mlog_entry("%p %u %p", msg
, len
, data
);
533 node
= exit_msg
->node_idx
;
535 printk(KERN_INFO
"ocfs2_dlm: Node %u leaves domain %s\n", node
, dlm
->name
);
537 spin_lock(&dlm
->spinlock
);
538 clear_bit(node
, dlm
->domain_map
);
539 __dlm_print_nodes(dlm
);
541 /* notify anything attached to the heartbeat events */
542 dlm_hb_event_notify_attached(dlm
, node
, 0);
544 spin_unlock(&dlm
->spinlock
);
551 static int dlm_send_one_domain_exit(struct dlm_ctxt
*dlm
,
555 struct dlm_exit_domain leave_msg
;
557 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
558 node
, dlm
->name
, dlm
->node_num
);
560 memset(&leave_msg
, 0, sizeof(leave_msg
));
561 leave_msg
.node_idx
= dlm
->node_num
;
563 status
= o2net_send_message(DLM_EXIT_DOMAIN_MSG
, dlm
->key
,
564 &leave_msg
, sizeof(leave_msg
), node
,
567 mlog(0, "status return %d from o2net_send_message\n", status
);
573 static void dlm_leave_domain(struct dlm_ctxt
*dlm
)
575 int node
, clear_node
, status
;
577 /* At this point we've migrated away all our locks and won't
578 * accept mastership of new ones. The dlm is responsible for
579 * almost nothing now. We make sure not to confuse any joining
580 * nodes and then commence shutdown procedure. */
582 spin_lock(&dlm
->spinlock
);
583 /* Clear ourselves from the domain map */
584 clear_bit(dlm
->node_num
, dlm
->domain_map
);
585 while ((node
= find_next_bit(dlm
->domain_map
, O2NM_MAX_NODES
,
586 0)) < O2NM_MAX_NODES
) {
587 /* Drop the dlm spinlock. This is safe wrt the domain_map.
588 * -nodes cannot be added now as the
589 * query_join_handlers knows to respond with OK_NO_MAP
590 * -we catch the right network errors if a node is
591 * removed from the map while we're sending him the
593 spin_unlock(&dlm
->spinlock
);
597 status
= dlm_send_one_domain_exit(dlm
, node
);
599 status
!= -ENOPROTOOPT
&&
600 status
!= -ENOTCONN
) {
601 mlog(ML_NOTICE
, "Error %d sending domain exit message "
602 "to node %d\n", status
, node
);
604 /* Not sure what to do here but lets sleep for
605 * a bit in case this was a transient
607 msleep(DLM_DOMAIN_BACKOFF_MS
);
611 spin_lock(&dlm
->spinlock
);
612 /* If we're not clearing the node bit then we intend
613 * to loop back around to try again. */
615 clear_bit(node
, dlm
->domain_map
);
617 spin_unlock(&dlm
->spinlock
);
620 int dlm_joined(struct dlm_ctxt
*dlm
)
624 spin_lock(&dlm_domain_lock
);
626 if (dlm
->dlm_state
== DLM_CTXT_JOINED
)
629 spin_unlock(&dlm_domain_lock
);
634 int dlm_shutting_down(struct dlm_ctxt
*dlm
)
638 spin_lock(&dlm_domain_lock
);
640 if (dlm
->dlm_state
== DLM_CTXT_IN_SHUTDOWN
)
643 spin_unlock(&dlm_domain_lock
);
648 void dlm_unregister_domain(struct dlm_ctxt
*dlm
)
651 struct dlm_lock_resource
*res
;
653 spin_lock(&dlm_domain_lock
);
654 BUG_ON(dlm
->dlm_state
!= DLM_CTXT_JOINED
);
655 BUG_ON(!dlm
->num_joins
);
658 if (!dlm
->num_joins
) {
659 /* We mark it "in shutdown" now so new register
660 * requests wait until we've completely left the
661 * domain. Don't use DLM_CTXT_LEAVING yet as we still
662 * want new domain joins to communicate with us at
663 * least until we've completed migration of our
665 dlm
->dlm_state
= DLM_CTXT_IN_SHUTDOWN
;
668 spin_unlock(&dlm_domain_lock
);
671 mlog(0, "shutting down domain %s\n", dlm
->name
);
673 /* We changed dlm state, notify the thread */
674 dlm_kick_thread(dlm
, NULL
);
676 while (dlm_migrate_all_locks(dlm
)) {
677 /* Give dlm_thread time to purge the lockres' */
679 mlog(0, "%s: more migration to do\n", dlm
->name
);
682 /* This list should be empty. If not, print remaining lockres */
683 if (!list_empty(&dlm
->tracking_list
)) {
684 mlog(ML_ERROR
, "Following lockres' are still on the "
686 list_for_each_entry(res
, &dlm
->tracking_list
, tracking
)
687 dlm_print_one_lock_resource(res
);
690 dlm_mark_domain_leaving(dlm
);
691 dlm_leave_domain(dlm
);
692 dlm_complete_dlm_shutdown(dlm
);
696 EXPORT_SYMBOL_GPL(dlm_unregister_domain
);
698 static int dlm_query_join_proto_check(char *proto_type
, int node
,
699 struct dlm_protocol_version
*ours
,
700 struct dlm_protocol_version
*request
)
703 struct dlm_protocol_version proto
= *request
;
705 if (!dlm_protocol_compare(ours
, &proto
)) {
707 "node %u wanted to join with %s locking protocol "
708 "%u.%u, we respond with %u.%u\n",
712 proto
.pv_major
, proto
.pv_minor
);
713 request
->pv_minor
= proto
.pv_minor
;
717 "Node %u wanted to join with %s locking "
718 "protocol %u.%u, but we have %u.%u, disallowing\n",
731 * struct dlm_query_join_packet is made up of four one-byte fields. They
732 * are effectively in big-endian order already. However, little-endian
733 * machines swap them before putting the packet on the wire (because
734 * query_join's response is a status, and that status is treated as a u32
735 * on the wire). Thus, a big-endian and little-endian machines will treat
736 * this structure differently.
738 * The solution is to have little-endian machines swap the structure when
739 * converting from the structure to the u32 representation. This will
740 * result in the structure having the correct format on the wire no matter
741 * the host endian format.
743 static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet
*packet
,
746 union dlm_query_join_response response
;
748 response
.packet
= *packet
;
749 *wire
= cpu_to_be32(response
.intval
);
752 static void dlm_query_join_wire_to_packet(u32 wire
,
753 struct dlm_query_join_packet
*packet
)
755 union dlm_query_join_response response
;
757 response
.intval
= cpu_to_be32(wire
);
758 *packet
= response
.packet
;
761 static int dlm_query_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
764 struct dlm_query_join_request
*query
;
765 struct dlm_query_join_packet packet
= {
766 .code
= JOIN_DISALLOW
,
768 struct dlm_ctxt
*dlm
= NULL
;
772 query
= (struct dlm_query_join_request
*) msg
->buf
;
774 mlog(0, "node %u wants to join domain %s\n", query
->node_idx
,
778 * If heartbeat doesn't consider the node live, tell it
779 * to back off and try again. This gives heartbeat a chance
782 if (!o2hb_check_node_heartbeating(query
->node_idx
)) {
783 mlog(0, "node %u is not in our live map yet\n",
786 packet
.code
= JOIN_DISALLOW
;
790 packet
.code
= JOIN_OK_NO_MAP
;
792 spin_lock(&dlm_domain_lock
);
793 dlm
= __dlm_lookup_domain_full(query
->domain
, query
->name_len
);
798 * There is a small window where the joining node may not see the
799 * node(s) that just left but still part of the cluster. DISALLOW
800 * join request if joining node has different node map.
803 while (nodenum
< O2NM_MAX_NODES
) {
804 if (test_bit(nodenum
, dlm
->domain_map
)) {
805 if (!byte_test_bit(nodenum
, query
->node_map
)) {
806 mlog(0, "disallow join as node %u does not "
807 "have node %u in its nodemap\n",
808 query
->node_idx
, nodenum
);
809 packet
.code
= JOIN_DISALLOW
;
816 /* Once the dlm ctxt is marked as leaving then we don't want
817 * to be put in someone's domain map.
818 * Also, explicitly disallow joining at certain troublesome
819 * times (ie. during recovery). */
820 if (dlm
&& dlm
->dlm_state
!= DLM_CTXT_LEAVING
) {
821 int bit
= query
->node_idx
;
822 spin_lock(&dlm
->spinlock
);
824 if (dlm
->dlm_state
== DLM_CTXT_NEW
&&
825 dlm
->joining_node
== DLM_LOCK_RES_OWNER_UNKNOWN
) {
826 /*If this is a brand new context and we
827 * haven't started our join process yet, then
828 * the other node won the race. */
829 packet
.code
= JOIN_OK_NO_MAP
;
830 } else if (dlm
->joining_node
!= DLM_LOCK_RES_OWNER_UNKNOWN
) {
831 /* Disallow parallel joins. */
832 packet
.code
= JOIN_DISALLOW
;
833 } else if (dlm
->reco
.state
& DLM_RECO_STATE_ACTIVE
) {
834 mlog(0, "node %u trying to join, but recovery "
835 "is ongoing.\n", bit
);
836 packet
.code
= JOIN_DISALLOW
;
837 } else if (test_bit(bit
, dlm
->recovery_map
)) {
838 mlog(0, "node %u trying to join, but it "
839 "still needs recovery.\n", bit
);
840 packet
.code
= JOIN_DISALLOW
;
841 } else if (test_bit(bit
, dlm
->domain_map
)) {
842 mlog(0, "node %u trying to join, but it "
843 "is still in the domain! needs recovery?\n",
845 packet
.code
= JOIN_DISALLOW
;
847 /* Alright we're fully a part of this domain
848 * so we keep some state as to who's joining
849 * and indicate to him that needs to be fixed
852 /* Make sure we speak compatible locking protocols. */
853 if (dlm_query_join_proto_check("DLM", bit
,
854 &dlm
->dlm_locking_proto
,
855 &query
->dlm_proto
)) {
856 packet
.code
= JOIN_PROTOCOL_MISMATCH
;
857 } else if (dlm_query_join_proto_check("fs", bit
,
858 &dlm
->fs_locking_proto
,
860 packet
.code
= JOIN_PROTOCOL_MISMATCH
;
862 packet
.dlm_minor
= query
->dlm_proto
.pv_minor
;
863 packet
.fs_minor
= query
->fs_proto
.pv_minor
;
864 packet
.code
= JOIN_OK
;
865 __dlm_set_joining_node(dlm
, query
->node_idx
);
869 spin_unlock(&dlm
->spinlock
);
872 spin_unlock(&dlm_domain_lock
);
875 mlog(0, "We respond with %u\n", packet
.code
);
877 dlm_query_join_packet_to_wire(&packet
, &response
);
881 static int dlm_assert_joined_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
884 struct dlm_assert_joined
*assert;
885 struct dlm_ctxt
*dlm
= NULL
;
887 assert = (struct dlm_assert_joined
*) msg
->buf
;
889 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx
,
892 spin_lock(&dlm_domain_lock
);
893 dlm
= __dlm_lookup_domain_full(assert->domain
, assert->name_len
);
894 /* XXX should we consider no dlm ctxt an error? */
896 spin_lock(&dlm
->spinlock
);
898 /* Alright, this node has officially joined our
899 * domain. Set him in the map and clean up our
900 * leftover join state. */
901 BUG_ON(dlm
->joining_node
!= assert->node_idx
);
902 set_bit(assert->node_idx
, dlm
->domain_map
);
903 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
905 printk(KERN_INFO
"ocfs2_dlm: Node %u joins domain %s\n",
906 assert->node_idx
, dlm
->name
);
907 __dlm_print_nodes(dlm
);
909 /* notify anything attached to the heartbeat events */
910 dlm_hb_event_notify_attached(dlm
, assert->node_idx
, 1);
912 spin_unlock(&dlm
->spinlock
);
914 spin_unlock(&dlm_domain_lock
);
919 static int dlm_cancel_join_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
922 struct dlm_cancel_join
*cancel
;
923 struct dlm_ctxt
*dlm
= NULL
;
925 cancel
= (struct dlm_cancel_join
*) msg
->buf
;
927 mlog(0, "node %u cancels join on domain %s\n", cancel
->node_idx
,
930 spin_lock(&dlm_domain_lock
);
931 dlm
= __dlm_lookup_domain_full(cancel
->domain
, cancel
->name_len
);
934 spin_lock(&dlm
->spinlock
);
936 /* Yikes, this guy wants to cancel his join. No
937 * problem, we simply cleanup our join state. */
938 BUG_ON(dlm
->joining_node
!= cancel
->node_idx
);
939 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
941 spin_unlock(&dlm
->spinlock
);
943 spin_unlock(&dlm_domain_lock
);
948 static int dlm_send_one_join_cancel(struct dlm_ctxt
*dlm
,
952 struct dlm_cancel_join cancel_msg
;
954 memset(&cancel_msg
, 0, sizeof(cancel_msg
));
955 cancel_msg
.node_idx
= dlm
->node_num
;
956 cancel_msg
.name_len
= strlen(dlm
->name
);
957 memcpy(cancel_msg
.domain
, dlm
->name
, cancel_msg
.name_len
);
959 status
= o2net_send_message(DLM_CANCEL_JOIN_MSG
, DLM_MOD_KEY
,
960 &cancel_msg
, sizeof(cancel_msg
), node
,
971 /* map_size should be in bytes. */
972 static int dlm_send_join_cancels(struct dlm_ctxt
*dlm
,
973 unsigned long *node_map
,
974 unsigned int map_size
)
979 if (map_size
!= (BITS_TO_LONGS(O2NM_MAX_NODES
) *
980 sizeof(unsigned long))) {
982 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
983 map_size
, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES
));
989 while ((node
= find_next_bit(node_map
, O2NM_MAX_NODES
,
990 node
+ 1)) < O2NM_MAX_NODES
) {
991 if (node
== dlm
->node_num
)
994 tmpstat
= dlm_send_one_join_cancel(dlm
, node
);
996 mlog(ML_ERROR
, "Error return %d cancelling join on "
997 "node %d\n", tmpstat
, node
);
1008 static int dlm_request_join(struct dlm_ctxt
*dlm
,
1010 enum dlm_query_join_response_code
*response
)
1013 struct dlm_query_join_request join_msg
;
1014 struct dlm_query_join_packet packet
;
1017 mlog(0, "querying node %d\n", node
);
1019 memset(&join_msg
, 0, sizeof(join_msg
));
1020 join_msg
.node_idx
= dlm
->node_num
;
1021 join_msg
.name_len
= strlen(dlm
->name
);
1022 memcpy(join_msg
.domain
, dlm
->name
, join_msg
.name_len
);
1023 join_msg
.dlm_proto
= dlm
->dlm_locking_proto
;
1024 join_msg
.fs_proto
= dlm
->fs_locking_proto
;
1026 /* copy live node map to join message */
1027 byte_copymap(join_msg
.node_map
, dlm
->live_nodes_map
, O2NM_MAX_NODES
);
1029 status
= o2net_send_message(DLM_QUERY_JOIN_MSG
, DLM_MOD_KEY
, &join_msg
,
1030 sizeof(join_msg
), node
,
1032 if (status
< 0 && status
!= -ENOPROTOOPT
) {
1036 dlm_query_join_wire_to_packet(join_resp
, &packet
);
1038 /* -ENOPROTOOPT from the net code means the other side isn't
1039 listening for our message type -- that's fine, it means
1040 his dlm isn't up, so we can consider him a 'yes' but not
1041 joined into the domain. */
1042 if (status
== -ENOPROTOOPT
) {
1044 *response
= JOIN_OK_NO_MAP
;
1045 } else if (packet
.code
== JOIN_DISALLOW
||
1046 packet
.code
== JOIN_OK_NO_MAP
) {
1047 *response
= packet
.code
;
1048 } else if (packet
.code
== JOIN_PROTOCOL_MISMATCH
) {
1050 "This node requested DLM locking protocol %u.%u and "
1051 "filesystem locking protocol %u.%u. At least one of "
1052 "the protocol versions on node %d is not compatible, "
1054 dlm
->dlm_locking_proto
.pv_major
,
1055 dlm
->dlm_locking_proto
.pv_minor
,
1056 dlm
->fs_locking_proto
.pv_major
,
1057 dlm
->fs_locking_proto
.pv_minor
,
1060 *response
= packet
.code
;
1061 } else if (packet
.code
== JOIN_OK
) {
1062 *response
= packet
.code
;
1063 /* Use the same locking protocol as the remote node */
1064 dlm
->dlm_locking_proto
.pv_minor
= packet
.dlm_minor
;
1065 dlm
->fs_locking_proto
.pv_minor
= packet
.fs_minor
;
1067 "Node %d responds JOIN_OK with DLM locking protocol "
1068 "%u.%u and fs locking protocol %u.%u\n",
1070 dlm
->dlm_locking_proto
.pv_major
,
1071 dlm
->dlm_locking_proto
.pv_minor
,
1072 dlm
->fs_locking_proto
.pv_major
,
1073 dlm
->fs_locking_proto
.pv_minor
);
1076 mlog(ML_ERROR
, "invalid response %d from node %u\n",
1080 mlog(0, "status %d, node %d response is %d\n", status
, node
,
1087 static int dlm_send_one_join_assert(struct dlm_ctxt
*dlm
,
1091 struct dlm_assert_joined assert_msg
;
1093 mlog(0, "Sending join assert to node %u\n", node
);
1095 memset(&assert_msg
, 0, sizeof(assert_msg
));
1096 assert_msg
.node_idx
= dlm
->node_num
;
1097 assert_msg
.name_len
= strlen(dlm
->name
);
1098 memcpy(assert_msg
.domain
, dlm
->name
, assert_msg
.name_len
);
1100 status
= o2net_send_message(DLM_ASSERT_JOINED_MSG
, DLM_MOD_KEY
,
1101 &assert_msg
, sizeof(assert_msg
), node
,
1109 static void dlm_send_join_asserts(struct dlm_ctxt
*dlm
,
1110 unsigned long *node_map
)
1112 int status
, node
, live
;
1116 while ((node
= find_next_bit(node_map
, O2NM_MAX_NODES
,
1117 node
+ 1)) < O2NM_MAX_NODES
) {
1118 if (node
== dlm
->node_num
)
1122 /* It is very important that this message be
1123 * received so we spin until either the node
1124 * has died or it gets the message. */
1125 status
= dlm_send_one_join_assert(dlm
, node
);
1127 spin_lock(&dlm
->spinlock
);
1128 live
= test_bit(node
, dlm
->live_nodes_map
);
1129 spin_unlock(&dlm
->spinlock
);
1132 mlog(ML_ERROR
, "Error return %d asserting "
1133 "join on node %d\n", status
, node
);
1135 /* give us some time between errors... */
1137 msleep(DLM_DOMAIN_BACKOFF_MS
);
1139 } while (status
&& live
);
1143 struct domain_join_ctxt
{
1144 unsigned long live_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1145 unsigned long yes_resp_map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
1148 static int dlm_should_restart_join(struct dlm_ctxt
*dlm
,
1149 struct domain_join_ctxt
*ctxt
,
1150 enum dlm_query_join_response_code response
)
1154 if (response
== JOIN_DISALLOW
) {
1155 mlog(0, "Latest response of disallow -- should restart\n");
1159 spin_lock(&dlm
->spinlock
);
1160 /* For now, we restart the process if the node maps have
1162 ret
= memcmp(ctxt
->live_map
, dlm
->live_nodes_map
,
1163 sizeof(dlm
->live_nodes_map
));
1164 spin_unlock(&dlm
->spinlock
);
1167 mlog(0, "Node maps changed -- should restart\n");
1172 static int dlm_try_to_join_domain(struct dlm_ctxt
*dlm
)
1174 int status
= 0, tmpstat
, node
;
1175 struct domain_join_ctxt
*ctxt
;
1176 enum dlm_query_join_response_code response
= JOIN_DISALLOW
;
1178 mlog_entry("%p", dlm
);
1180 ctxt
= kzalloc(sizeof(*ctxt
), GFP_KERNEL
);
1187 /* group sem locking should work for us here -- we're already
1188 * registered for heartbeat events so filling this should be
1189 * atomic wrt getting those handlers called. */
1190 o2hb_fill_node_map(dlm
->live_nodes_map
, sizeof(dlm
->live_nodes_map
));
1192 spin_lock(&dlm
->spinlock
);
1193 memcpy(ctxt
->live_map
, dlm
->live_nodes_map
, sizeof(ctxt
->live_map
));
1195 __dlm_set_joining_node(dlm
, dlm
->node_num
);
1197 spin_unlock(&dlm
->spinlock
);
1200 while ((node
= find_next_bit(ctxt
->live_map
, O2NM_MAX_NODES
,
1201 node
+ 1)) < O2NM_MAX_NODES
) {
1202 if (node
== dlm
->node_num
)
1205 status
= dlm_request_join(dlm
, node
, &response
);
1211 /* Ok, either we got a response or the node doesn't have a
1213 if (response
== JOIN_OK
)
1214 set_bit(node
, ctxt
->yes_resp_map
);
1216 if (dlm_should_restart_join(dlm
, ctxt
, response
)) {
1222 mlog(0, "Yay, done querying nodes!\n");
1224 /* Yay, everyone agree's we can join the domain. My domain is
1225 * comprised of all nodes who were put in the
1226 * yes_resp_map. Copy that into our domain map and send a join
1227 * assert message to clean up everyone elses state. */
1228 spin_lock(&dlm
->spinlock
);
1229 memcpy(dlm
->domain_map
, ctxt
->yes_resp_map
,
1230 sizeof(ctxt
->yes_resp_map
));
1231 set_bit(dlm
->node_num
, dlm
->domain_map
);
1232 spin_unlock(&dlm
->spinlock
);
1234 dlm_send_join_asserts(dlm
, ctxt
->yes_resp_map
);
1236 /* Joined state *must* be set before the joining node
1237 * information, otherwise the query_join handler may read no
1238 * current joiner but a state of NEW and tell joining nodes
1239 * we're not in the domain. */
1240 spin_lock(&dlm_domain_lock
);
1241 dlm
->dlm_state
= DLM_CTXT_JOINED
;
1243 spin_unlock(&dlm_domain_lock
);
1246 spin_lock(&dlm
->spinlock
);
1247 __dlm_set_joining_node(dlm
, DLM_LOCK_RES_OWNER_UNKNOWN
);
1249 __dlm_print_nodes(dlm
);
1250 spin_unlock(&dlm
->spinlock
);
1253 /* Do we need to send a cancel message to any nodes? */
1255 tmpstat
= dlm_send_join_cancels(dlm
,
1257 sizeof(ctxt
->yes_resp_map
));
1259 mlog_errno(tmpstat
);
1264 mlog(0, "returning %d\n", status
);
1268 static void dlm_unregister_domain_handlers(struct dlm_ctxt
*dlm
)
1270 o2hb_unregister_callback(NULL
, &dlm
->dlm_hb_up
);
1271 o2hb_unregister_callback(NULL
, &dlm
->dlm_hb_down
);
1272 o2net_unregister_handler_list(&dlm
->dlm_domain_handlers
);
1275 static int dlm_register_domain_handlers(struct dlm_ctxt
*dlm
)
1279 mlog(0, "registering handlers.\n");
1281 o2hb_setup_callback(&dlm
->dlm_hb_down
, O2HB_NODE_DOWN_CB
,
1282 dlm_hb_node_down_cb
, dlm
, DLM_HB_NODE_DOWN_PRI
);
1283 status
= o2hb_register_callback(NULL
, &dlm
->dlm_hb_down
);
1287 o2hb_setup_callback(&dlm
->dlm_hb_up
, O2HB_NODE_UP_CB
,
1288 dlm_hb_node_up_cb
, dlm
, DLM_HB_NODE_UP_PRI
);
1289 status
= o2hb_register_callback(NULL
, &dlm
->dlm_hb_up
);
1293 status
= o2net_register_handler(DLM_MASTER_REQUEST_MSG
, dlm
->key
,
1294 sizeof(struct dlm_master_request
),
1295 dlm_master_request_handler
,
1296 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1300 status
= o2net_register_handler(DLM_ASSERT_MASTER_MSG
, dlm
->key
,
1301 sizeof(struct dlm_assert_master
),
1302 dlm_assert_master_handler
,
1303 dlm
, dlm_assert_master_post_handler
,
1304 &dlm
->dlm_domain_handlers
);
1308 status
= o2net_register_handler(DLM_CREATE_LOCK_MSG
, dlm
->key
,
1309 sizeof(struct dlm_create_lock
),
1310 dlm_create_lock_handler
,
1311 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1315 status
= o2net_register_handler(DLM_CONVERT_LOCK_MSG
, dlm
->key
,
1316 DLM_CONVERT_LOCK_MAX_LEN
,
1317 dlm_convert_lock_handler
,
1318 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1322 status
= o2net_register_handler(DLM_UNLOCK_LOCK_MSG
, dlm
->key
,
1323 DLM_UNLOCK_LOCK_MAX_LEN
,
1324 dlm_unlock_lock_handler
,
1325 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1329 status
= o2net_register_handler(DLM_PROXY_AST_MSG
, dlm
->key
,
1330 DLM_PROXY_AST_MAX_LEN
,
1331 dlm_proxy_ast_handler
,
1332 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1336 status
= o2net_register_handler(DLM_EXIT_DOMAIN_MSG
, dlm
->key
,
1337 sizeof(struct dlm_exit_domain
),
1338 dlm_exit_domain_handler
,
1339 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1343 status
= o2net_register_handler(DLM_DEREF_LOCKRES_MSG
, dlm
->key
,
1344 sizeof(struct dlm_deref_lockres
),
1345 dlm_deref_lockres_handler
,
1346 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1350 status
= o2net_register_handler(DLM_MIGRATE_REQUEST_MSG
, dlm
->key
,
1351 sizeof(struct dlm_migrate_request
),
1352 dlm_migrate_request_handler
,
1353 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1357 status
= o2net_register_handler(DLM_MIG_LOCKRES_MSG
, dlm
->key
,
1358 DLM_MIG_LOCKRES_MAX_LEN
,
1359 dlm_mig_lockres_handler
,
1360 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1364 status
= o2net_register_handler(DLM_MASTER_REQUERY_MSG
, dlm
->key
,
1365 sizeof(struct dlm_master_requery
),
1366 dlm_master_requery_handler
,
1367 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1371 status
= o2net_register_handler(DLM_LOCK_REQUEST_MSG
, dlm
->key
,
1372 sizeof(struct dlm_lock_request
),
1373 dlm_request_all_locks_handler
,
1374 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1378 status
= o2net_register_handler(DLM_RECO_DATA_DONE_MSG
, dlm
->key
,
1379 sizeof(struct dlm_reco_data_done
),
1380 dlm_reco_data_done_handler
,
1381 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1385 status
= o2net_register_handler(DLM_BEGIN_RECO_MSG
, dlm
->key
,
1386 sizeof(struct dlm_begin_reco
),
1387 dlm_begin_reco_handler
,
1388 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1392 status
= o2net_register_handler(DLM_FINALIZE_RECO_MSG
, dlm
->key
,
1393 sizeof(struct dlm_finalize_reco
),
1394 dlm_finalize_reco_handler
,
1395 dlm
, NULL
, &dlm
->dlm_domain_handlers
);
1401 dlm_unregister_domain_handlers(dlm
);
1406 static int dlm_join_domain(struct dlm_ctxt
*dlm
)
1409 unsigned int backoff
;
1410 unsigned int total_backoff
= 0;
1414 mlog(0, "Join domain %s\n", dlm
->name
);
1416 status
= dlm_register_domain_handlers(dlm
);
1422 status
= dlm_debug_init(dlm
);
1428 status
= dlm_launch_thread(dlm
);
1434 status
= dlm_launch_recovery_thread(dlm
);
1440 dlm
->dlm_worker
= create_singlethread_workqueue("dlm_wq");
1441 if (!dlm
->dlm_worker
) {
1448 status
= dlm_try_to_join_domain(dlm
);
1450 /* If we're racing another node to the join, then we
1451 * need to back off temporarily and let them
1453 #define DLM_JOIN_TIMEOUT_MSECS 90000
1454 if (status
== -EAGAIN
) {
1455 if (signal_pending(current
)) {
1456 status
= -ERESTARTSYS
;
1461 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS
)) {
1462 status
= -ERESTARTSYS
;
1463 mlog(ML_NOTICE
, "Timed out joining dlm domain "
1464 "%s after %u msecs\n", dlm
->name
,
1465 jiffies_to_msecs(total_backoff
));
1471 * <dale> No, after you!
1473 * <dale> But you first!
1476 backoff
= (unsigned int)(jiffies
& 0x3);
1477 backoff
*= DLM_DOMAIN_BACKOFF_MS
;
1478 total_backoff
+= backoff
;
1479 mlog(0, "backoff %d\n", backoff
);
1482 } while (status
== -EAGAIN
);
1491 wake_up(&dlm_domain_events
);
1494 dlm_unregister_domain_handlers(dlm
);
1495 dlm_debug_shutdown(dlm
);
1496 dlm_complete_thread(dlm
);
1497 dlm_complete_recovery_thread(dlm
);
1498 dlm_destroy_dlm_worker(dlm
);
1504 static struct dlm_ctxt
*dlm_alloc_ctxt(const char *domain
,
1509 struct dlm_ctxt
*dlm
= NULL
;
1511 dlm
= kzalloc(sizeof(*dlm
), GFP_KERNEL
);
1513 mlog_errno(-ENOMEM
);
1517 dlm
->name
= kmalloc(strlen(domain
) + 1, GFP_KERNEL
);
1518 if (dlm
->name
== NULL
) {
1519 mlog_errno(-ENOMEM
);
1525 dlm
->lockres_hash
= (struct hlist_head
**)dlm_alloc_pagevec(DLM_HASH_PAGES
);
1526 if (!dlm
->lockres_hash
) {
1527 mlog_errno(-ENOMEM
);
1534 for (i
= 0; i
< DLM_HASH_BUCKETS
; i
++)
1535 INIT_HLIST_HEAD(dlm_lockres_hash(dlm
, i
));
1537 strcpy(dlm
->name
, domain
);
1539 dlm
->node_num
= o2nm_this_node();
1541 ret
= dlm_create_debugfs_subroot(dlm
);
1543 dlm_free_pagevec((void **)dlm
->lockres_hash
, DLM_HASH_PAGES
);
1550 spin_lock_init(&dlm
->spinlock
);
1551 spin_lock_init(&dlm
->master_lock
);
1552 spin_lock_init(&dlm
->ast_lock
);
1553 spin_lock_init(&dlm
->track_lock
);
1554 INIT_LIST_HEAD(&dlm
->list
);
1555 INIT_LIST_HEAD(&dlm
->dirty_list
);
1556 INIT_LIST_HEAD(&dlm
->reco
.resources
);
1557 INIT_LIST_HEAD(&dlm
->reco
.received
);
1558 INIT_LIST_HEAD(&dlm
->reco
.node_data
);
1559 INIT_LIST_HEAD(&dlm
->purge_list
);
1560 INIT_LIST_HEAD(&dlm
->dlm_domain_handlers
);
1561 INIT_LIST_HEAD(&dlm
->tracking_list
);
1562 dlm
->reco
.state
= 0;
1564 INIT_LIST_HEAD(&dlm
->pending_asts
);
1565 INIT_LIST_HEAD(&dlm
->pending_basts
);
1567 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1568 dlm
->recovery_map
, &(dlm
->recovery_map
[0]));
1570 memset(dlm
->recovery_map
, 0, sizeof(dlm
->recovery_map
));
1571 memset(dlm
->live_nodes_map
, 0, sizeof(dlm
->live_nodes_map
));
1572 memset(dlm
->domain_map
, 0, sizeof(dlm
->domain_map
));
1574 dlm
->dlm_thread_task
= NULL
;
1575 dlm
->dlm_reco_thread_task
= NULL
;
1576 dlm
->dlm_worker
= NULL
;
1577 init_waitqueue_head(&dlm
->dlm_thread_wq
);
1578 init_waitqueue_head(&dlm
->dlm_reco_thread_wq
);
1579 init_waitqueue_head(&dlm
->reco
.event
);
1580 init_waitqueue_head(&dlm
->ast_wq
);
1581 init_waitqueue_head(&dlm
->migration_wq
);
1582 INIT_LIST_HEAD(&dlm
->master_list
);
1583 INIT_LIST_HEAD(&dlm
->mle_hb_events
);
1585 dlm
->joining_node
= DLM_LOCK_RES_OWNER_UNKNOWN
;
1586 init_waitqueue_head(&dlm
->dlm_join_events
);
1588 dlm
->reco
.new_master
= O2NM_INVALID_NODE_NUM
;
1589 dlm
->reco
.dead_node
= O2NM_INVALID_NODE_NUM
;
1590 atomic_set(&dlm
->local_resources
, 0);
1591 atomic_set(&dlm
->remote_resources
, 0);
1592 atomic_set(&dlm
->unknown_resources
, 0);
1594 spin_lock_init(&dlm
->work_lock
);
1595 INIT_LIST_HEAD(&dlm
->work_list
);
1596 INIT_WORK(&dlm
->dispatched_work
, dlm_dispatch_work
);
1598 kref_init(&dlm
->dlm_refs
);
1599 dlm
->dlm_state
= DLM_CTXT_NEW
;
1601 INIT_LIST_HEAD(&dlm
->dlm_eviction_callbacks
);
1603 mlog(0, "context init: refcount %u\n",
1604 atomic_read(&dlm
->dlm_refs
.refcount
));
1611 * Compare a requested locking protocol version against the current one.
1613 * If the major numbers are different, they are incompatible.
1614 * If the current minor is greater than the request, they are incompatible.
1615 * If the current minor is less than or equal to the request, they are
1616 * compatible, and the requester should run at the current minor version.
1618 static int dlm_protocol_compare(struct dlm_protocol_version
*existing
,
1619 struct dlm_protocol_version
*request
)
1621 if (existing
->pv_major
!= request
->pv_major
)
1624 if (existing
->pv_minor
> request
->pv_minor
)
1627 if (existing
->pv_minor
< request
->pv_minor
)
1628 request
->pv_minor
= existing
->pv_minor
;
1634 * dlm_register_domain: one-time setup per "domain".
1636 * The filesystem passes in the requested locking version via proto.
1637 * If registration was successful, proto will contain the negotiated
1640 struct dlm_ctxt
* dlm_register_domain(const char *domain
,
1642 struct dlm_protocol_version
*fs_proto
)
1645 struct dlm_ctxt
*dlm
= NULL
;
1646 struct dlm_ctxt
*new_ctxt
= NULL
;
1648 if (strlen(domain
) > O2NM_MAX_NAME_LEN
) {
1649 ret
= -ENAMETOOLONG
;
1650 mlog(ML_ERROR
, "domain name length too long\n");
1654 if (!o2hb_check_local_node_heartbeating()) {
1655 mlog(ML_ERROR
, "the local node has not been configured, or is "
1656 "not heartbeating\n");
1661 mlog(0, "register called for domain \"%s\"\n", domain
);
1665 if (signal_pending(current
)) {
1671 spin_lock(&dlm_domain_lock
);
1673 dlm
= __dlm_lookup_domain(domain
);
1675 if (dlm
->dlm_state
!= DLM_CTXT_JOINED
) {
1676 spin_unlock(&dlm_domain_lock
);
1678 mlog(0, "This ctxt is not joined yet!\n");
1679 wait_event_interruptible(dlm_domain_events
,
1680 dlm_wait_on_domain_helper(
1685 if (dlm_protocol_compare(&dlm
->fs_locking_proto
, fs_proto
)) {
1687 "Requested locking protocol version is not "
1688 "compatible with already registered domain "
1689 "\"%s\"\n", domain
);
1697 spin_unlock(&dlm_domain_lock
);
1705 spin_unlock(&dlm_domain_lock
);
1707 new_ctxt
= dlm_alloc_ctxt(domain
, key
);
1716 /* a little variable switch-a-roo here... */
1720 /* add the new domain */
1721 list_add_tail(&dlm
->list
, &dlm_domains
);
1722 spin_unlock(&dlm_domain_lock
);
1725 * Pass the locking protocol version into the join. If the join
1726 * succeeds, it will have the negotiated protocol set.
1728 dlm
->dlm_locking_proto
= dlm_protocol
;
1729 dlm
->fs_locking_proto
= *fs_proto
;
1731 ret
= dlm_join_domain(dlm
);
1738 /* Tell the caller what locking protocol we negotiated */
1739 *fs_proto
= dlm
->fs_locking_proto
;
1744 dlm_free_ctxt_mem(new_ctxt
);
1751 EXPORT_SYMBOL_GPL(dlm_register_domain
);
1753 static LIST_HEAD(dlm_join_handlers
);
1755 static void dlm_unregister_net_handlers(void)
1757 o2net_unregister_handler_list(&dlm_join_handlers
);
1760 static int dlm_register_net_handlers(void)
1764 status
= o2net_register_handler(DLM_QUERY_JOIN_MSG
, DLM_MOD_KEY
,
1765 sizeof(struct dlm_query_join_request
),
1766 dlm_query_join_handler
,
1767 NULL
, NULL
, &dlm_join_handlers
);
1771 status
= o2net_register_handler(DLM_ASSERT_JOINED_MSG
, DLM_MOD_KEY
,
1772 sizeof(struct dlm_assert_joined
),
1773 dlm_assert_joined_handler
,
1774 NULL
, NULL
, &dlm_join_handlers
);
1778 status
= o2net_register_handler(DLM_CANCEL_JOIN_MSG
, DLM_MOD_KEY
,
1779 sizeof(struct dlm_cancel_join
),
1780 dlm_cancel_join_handler
,
1781 NULL
, NULL
, &dlm_join_handlers
);
1785 dlm_unregister_net_handlers();
1790 /* Domain eviction callback handling.
1792 * The file system requires notification of node death *before* the
1793 * dlm completes it's recovery work, otherwise it may be able to
1794 * acquire locks on resources requiring recovery. Since the dlm can
1795 * evict a node from it's domain *before* heartbeat fires, a similar
1796 * mechanism is required. */
1798 /* Eviction is not expected to happen often, so a per-domain lock is
1799 * not necessary. Eviction callbacks are allowed to sleep for short
1800 * periods of time. */
1801 static DECLARE_RWSEM(dlm_callback_sem
);
1803 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt
*dlm
,
1806 struct list_head
*iter
;
1807 struct dlm_eviction_cb
*cb
;
1809 down_read(&dlm_callback_sem
);
1810 list_for_each(iter
, &dlm
->dlm_eviction_callbacks
) {
1811 cb
= list_entry(iter
, struct dlm_eviction_cb
, ec_item
);
1813 cb
->ec_func(node_num
, cb
->ec_data
);
1815 up_read(&dlm_callback_sem
);
1818 void dlm_setup_eviction_cb(struct dlm_eviction_cb
*cb
,
1819 dlm_eviction_func
*f
,
1822 INIT_LIST_HEAD(&cb
->ec_item
);
1826 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb
);
1828 void dlm_register_eviction_cb(struct dlm_ctxt
*dlm
,
1829 struct dlm_eviction_cb
*cb
)
1831 down_write(&dlm_callback_sem
);
1832 list_add_tail(&cb
->ec_item
, &dlm
->dlm_eviction_callbacks
);
1833 up_write(&dlm_callback_sem
);
1835 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb
);
1837 void dlm_unregister_eviction_cb(struct dlm_eviction_cb
*cb
)
1839 down_write(&dlm_callback_sem
);
1840 list_del_init(&cb
->ec_item
);
1841 up_write(&dlm_callback_sem
);
1843 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb
);
1845 static int __init
dlm_init(void)
1849 dlm_print_version();
1851 status
= dlm_init_mle_cache();
1853 mlog(ML_ERROR
, "Could not create o2dlm_mle slabcache\n");
1857 status
= dlm_init_master_caches();
1859 mlog(ML_ERROR
, "Could not create o2dlm_lockres and "
1860 "o2dlm_lockname slabcaches\n");
1864 status
= dlm_init_lock_cache();
1866 mlog(ML_ERROR
, "Count not create o2dlm_lock slabcache\n");
1870 status
= dlm_register_net_handlers();
1872 mlog(ML_ERROR
, "Unable to register network handlers\n");
1876 status
= dlm_create_debugfs_root();
1882 dlm_unregister_net_handlers();
1883 dlm_destroy_lock_cache();
1884 dlm_destroy_master_caches();
1885 dlm_destroy_mle_cache();
1889 static void __exit
dlm_exit (void)
1891 dlm_destroy_debugfs_root();
1892 dlm_unregister_net_handlers();
1893 dlm_destroy_lock_cache();
1894 dlm_destroy_master_caches();
1895 dlm_destroy_mle_cache();
1898 MODULE_AUTHOR("Oracle");
1899 MODULE_LICENSE("GPL");
1901 module_init(dlm_init
);
1902 module_exit(dlm_exit
);