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>
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
42 #include "dlmcommon.h"
44 #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);
126 #define DLM_DOMAIN_BACKOFF_MS 200
128 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
130 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
132 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
134 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
137 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
139 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
141 if (!hlist_unhashed(&lockres->hash_node)) {
142 hlist_del_init(&lockres->hash_node);
143 dlm_lockres_put(lockres);
147 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
148 struct dlm_lock_resource *res)
150 struct hlist_head *bucket;
153 assert_spin_locked(&dlm->spinlock);
156 bucket = dlm_lockres_hash(dlm, q->hash);
158 /* get a reference for our hashtable */
159 dlm_lockres_get(res);
161 hlist_add_head(&res->hash_node, bucket);
164 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
169 struct hlist_head *bucket;
170 struct hlist_node *list;
172 mlog_entry("%.*s\n", len, name);
174 assert_spin_locked(&dlm->spinlock);
176 bucket = dlm_lockres_hash(dlm, hash);
178 hlist_for_each(list, bucket) {
179 struct dlm_lock_resource *res = hlist_entry(list,
180 struct dlm_lock_resource, hash_node);
181 if (res->lockname.name[0] != name[0])
183 if (unlikely(res->lockname.len != len))
185 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
187 dlm_lockres_get(res);
193 /* intended to be called by functions which do not care about lock
194 * resources which are being purged (most net _handler functions).
195 * this will return NULL for any lock resource which is found but
196 * currently in the process of dropping its mastery reference.
197 * use __dlm_lookup_lockres_full when you need the lock resource
198 * regardless (e.g. dlm_get_lock_resource) */
199 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
204 struct dlm_lock_resource *res = NULL;
206 mlog_entry("%.*s\n", len, name);
208 assert_spin_locked(&dlm->spinlock);
210 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
212 spin_lock(&res->spinlock);
213 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
214 spin_unlock(&res->spinlock);
215 dlm_lockres_put(res);
218 spin_unlock(&res->spinlock);
224 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
228 struct dlm_lock_resource *res;
229 unsigned int hash = dlm_lockid_hash(name, len);
231 spin_lock(&dlm->spinlock);
232 res = __dlm_lookup_lockres(dlm, name, len, hash);
233 spin_unlock(&dlm->spinlock);
237 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
239 struct dlm_ctxt *tmp = NULL;
240 struct list_head *iter;
242 assert_spin_locked(&dlm_domain_lock);
244 /* tmp->name here is always NULL terminated,
245 * but domain may not be! */
246 list_for_each(iter, &dlm_domains) {
247 tmp = list_entry (iter, struct dlm_ctxt, list);
248 if (strlen(tmp->name) == len &&
249 memcmp(tmp->name, domain, len)==0)
257 /* For null terminated domain strings ONLY */
258 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
260 assert_spin_locked(&dlm_domain_lock);
262 return __dlm_lookup_domain_full(domain, strlen(domain));
266 /* returns true on one of two conditions:
267 * 1) the domain does not exist
268 * 2) the domain exists and it's state is "joined" */
269 static int dlm_wait_on_domain_helper(const char *domain)
272 struct dlm_ctxt *tmp = NULL;
274 spin_lock(&dlm_domain_lock);
276 tmp = __dlm_lookup_domain(domain);
279 else if (tmp->dlm_state == DLM_CTXT_JOINED)
282 spin_unlock(&dlm_domain_lock);
286 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
288 if (dlm->lockres_hash)
289 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
297 /* A little strange - this function will be called while holding
298 * dlm_domain_lock and is expected to be holding it on the way out. We
299 * will however drop and reacquire it multiple times */
300 static void dlm_ctxt_release(struct kref *kref)
302 struct dlm_ctxt *dlm;
304 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
306 BUG_ON(dlm->num_joins);
307 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
309 /* we may still be in the list if we hit an error during join. */
310 list_del_init(&dlm->list);
312 spin_unlock(&dlm_domain_lock);
314 mlog(0, "freeing memory from domain %s\n", dlm->name);
316 wake_up(&dlm_domain_events);
318 dlm_free_ctxt_mem(dlm);
320 spin_lock(&dlm_domain_lock);
323 void dlm_put(struct dlm_ctxt *dlm)
325 spin_lock(&dlm_domain_lock);
326 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
327 spin_unlock(&dlm_domain_lock);
330 static void __dlm_get(struct dlm_ctxt *dlm)
332 kref_get(&dlm->dlm_refs);
335 /* given a questionable reference to a dlm object, gets a reference if
336 * it can find it in the list, otherwise returns NULL in which case
337 * you shouldn't trust your pointer. */
338 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
340 struct list_head *iter;
341 struct dlm_ctxt *target = NULL;
343 spin_lock(&dlm_domain_lock);
345 list_for_each(iter, &dlm_domains) {
346 target = list_entry (iter, struct dlm_ctxt, list);
356 spin_unlock(&dlm_domain_lock);
361 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
365 spin_lock(&dlm_domain_lock);
366 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
367 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
368 spin_unlock(&dlm_domain_lock);
373 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
375 if (dlm->dlm_worker) {
376 flush_workqueue(dlm->dlm_worker);
377 destroy_workqueue(dlm->dlm_worker);
378 dlm->dlm_worker = NULL;
382 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
384 dlm_unregister_domain_handlers(dlm);
385 dlm_complete_thread(dlm);
386 dlm_complete_recovery_thread(dlm);
387 dlm_destroy_dlm_worker(dlm);
389 /* We've left the domain. Now we can take ourselves out of the
390 * list and allow the kref stuff to help us free the
392 spin_lock(&dlm_domain_lock);
393 list_del_init(&dlm->list);
394 spin_unlock(&dlm_domain_lock);
396 /* Wake up anyone waiting for us to remove this domain */
397 wake_up(&dlm_domain_events);
400 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
402 int i, num, n, ret = 0;
403 struct dlm_lock_resource *res;
404 struct hlist_node *iter;
405 struct hlist_head *bucket;
408 mlog(0, "Migrating locks from domain %s\n", dlm->name);
411 spin_lock(&dlm->spinlock);
412 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
415 bucket = dlm_lockres_hash(dlm, i);
416 iter = bucket->first;
419 res = hlist_entry(iter, struct dlm_lock_resource,
421 dlm_lockres_get(res);
422 /* migrate, if necessary. this will drop the dlm
423 * spinlock and retake it if it does migration. */
424 dropped = dlm_empty_lockres(dlm, res);
426 spin_lock(&res->spinlock);
427 __dlm_lockres_calc_usage(dlm, res);
428 iter = res->hash_node.next;
429 spin_unlock(&res->spinlock);
431 dlm_lockres_put(res);
433 cond_resched_lock(&dlm->spinlock);
439 mlog(0, "%s: touched %d lockreses in bucket %d "
440 "(tot=%d)\n", dlm->name, n, i, num);
442 spin_unlock(&dlm->spinlock);
443 wake_up(&dlm->dlm_thread_wq);
445 /* let the dlm thread take care of purging, keep scanning until
446 * nothing remains in the hash */
448 mlog(0, "%s: %d lock resources in hash last pass\n",
452 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
456 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
460 spin_lock(&dlm->spinlock);
461 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
462 spin_unlock(&dlm->spinlock);
467 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
469 /* Yikes, a double spinlock! I need domain_lock for the dlm
470 * state and the dlm spinlock for join state... Sorry! */
472 spin_lock(&dlm_domain_lock);
473 spin_lock(&dlm->spinlock);
475 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
476 mlog(0, "Node %d is joining, we wait on it.\n",
478 spin_unlock(&dlm->spinlock);
479 spin_unlock(&dlm_domain_lock);
481 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
485 dlm->dlm_state = DLM_CTXT_LEAVING;
486 spin_unlock(&dlm->spinlock);
487 spin_unlock(&dlm_domain_lock);
490 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
494 assert_spin_locked(&dlm->spinlock);
496 printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
498 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
499 node + 1)) < O2NM_MAX_NODES) {
505 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
508 struct dlm_ctxt *dlm = data;
510 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
512 mlog_entry("%p %u %p", msg, len, data);
517 node = exit_msg->node_idx;
519 printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
521 spin_lock(&dlm->spinlock);
522 clear_bit(node, dlm->domain_map);
523 __dlm_print_nodes(dlm);
525 /* notify anything attached to the heartbeat events */
526 dlm_hb_event_notify_attached(dlm, node, 0);
528 spin_unlock(&dlm->spinlock);
535 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
539 struct dlm_exit_domain leave_msg;
541 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
542 node, dlm->name, dlm->node_num);
544 memset(&leave_msg, 0, sizeof(leave_msg));
545 leave_msg.node_idx = dlm->node_num;
547 status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
548 &leave_msg, sizeof(leave_msg), node,
551 mlog(0, "status return %d from o2net_send_message\n", status);
557 static void dlm_leave_domain(struct dlm_ctxt *dlm)
559 int node, clear_node, status;
561 /* At this point we've migrated away all our locks and won't
562 * accept mastership of new ones. The dlm is responsible for
563 * almost nothing now. We make sure not to confuse any joining
564 * nodes and then commence shutdown procedure. */
566 spin_lock(&dlm->spinlock);
567 /* Clear ourselves from the domain map */
568 clear_bit(dlm->node_num, dlm->domain_map);
569 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
570 0)) < O2NM_MAX_NODES) {
571 /* Drop the dlm spinlock. This is safe wrt the domain_map.
572 * -nodes cannot be added now as the
573 * query_join_handlers knows to respond with OK_NO_MAP
574 * -we catch the right network errors if a node is
575 * removed from the map while we're sending him the
577 spin_unlock(&dlm->spinlock);
581 status = dlm_send_one_domain_exit(dlm, node);
583 status != -ENOPROTOOPT &&
584 status != -ENOTCONN) {
585 mlog(ML_NOTICE, "Error %d sending domain exit message "
586 "to node %d\n", status, node);
588 /* Not sure what to do here but lets sleep for
589 * a bit in case this was a transient
591 msleep(DLM_DOMAIN_BACKOFF_MS);
595 spin_lock(&dlm->spinlock);
596 /* If we're not clearing the node bit then we intend
597 * to loop back around to try again. */
599 clear_bit(node, dlm->domain_map);
601 spin_unlock(&dlm->spinlock);
604 int dlm_joined(struct dlm_ctxt *dlm)
608 spin_lock(&dlm_domain_lock);
610 if (dlm->dlm_state == DLM_CTXT_JOINED)
613 spin_unlock(&dlm_domain_lock);
618 int dlm_shutting_down(struct dlm_ctxt *dlm)
622 spin_lock(&dlm_domain_lock);
624 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
627 spin_unlock(&dlm_domain_lock);
632 void dlm_unregister_domain(struct dlm_ctxt *dlm)
636 spin_lock(&dlm_domain_lock);
637 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
638 BUG_ON(!dlm->num_joins);
641 if (!dlm->num_joins) {
642 /* We mark it "in shutdown" now so new register
643 * requests wait until we've completely left the
644 * domain. Don't use DLM_CTXT_LEAVING yet as we still
645 * want new domain joins to communicate with us at
646 * least until we've completed migration of our
648 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
651 spin_unlock(&dlm_domain_lock);
654 mlog(0, "shutting down domain %s\n", dlm->name);
656 /* We changed dlm state, notify the thread */
657 dlm_kick_thread(dlm, NULL);
659 while (dlm_migrate_all_locks(dlm)) {
660 mlog(0, "%s: more migration to do\n", dlm->name);
662 dlm_mark_domain_leaving(dlm);
663 dlm_leave_domain(dlm);
664 dlm_complete_dlm_shutdown(dlm);
668 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
670 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
673 struct dlm_query_join_request *query;
674 enum dlm_query_join_response response;
675 struct dlm_ctxt *dlm = NULL;
678 query = (struct dlm_query_join_request *) msg->buf;
680 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
684 * If heartbeat doesn't consider the node live, tell it
685 * to back off and try again. This gives heartbeat a chance
688 if (!o2hb_check_node_heartbeating(query->node_idx)) {
689 mlog(0, "node %u is not in our live map yet\n",
692 response = JOIN_DISALLOW;
696 response = JOIN_OK_NO_MAP;
698 spin_lock(&dlm_domain_lock);
699 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
704 * There is a small window where the joining node may not see the
705 * node(s) that just left but still part of the cluster. DISALLOW
706 * join request if joining node has different node map.
709 while (nodenum < O2NM_MAX_NODES) {
710 if (test_bit(nodenum, dlm->domain_map)) {
711 if (!byte_test_bit(nodenum, query->node_map)) {
712 mlog(0, "disallow join as node %u does not "
713 "have node %u in its nodemap\n",
714 query->node_idx, nodenum);
715 response = JOIN_DISALLOW;
722 /* Once the dlm ctxt is marked as leaving then we don't want
723 * to be put in someone's domain map.
724 * Also, explicitly disallow joining at certain troublesome
725 * times (ie. during recovery). */
726 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
727 int bit = query->node_idx;
728 spin_lock(&dlm->spinlock);
730 if (dlm->dlm_state == DLM_CTXT_NEW &&
731 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
732 /*If this is a brand new context and we
733 * haven't started our join process yet, then
734 * the other node won the race. */
735 response = JOIN_OK_NO_MAP;
736 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
737 /* Disallow parallel joins. */
738 response = JOIN_DISALLOW;
739 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
740 mlog(0, "node %u trying to join, but recovery "
741 "is ongoing.\n", bit);
742 response = JOIN_DISALLOW;
743 } else if (test_bit(bit, dlm->recovery_map)) {
744 mlog(0, "node %u trying to join, but it "
745 "still needs recovery.\n", bit);
746 response = JOIN_DISALLOW;
747 } else if (test_bit(bit, dlm->domain_map)) {
748 mlog(0, "node %u trying to join, but it "
749 "is still in the domain! needs recovery?\n",
751 response = JOIN_DISALLOW;
753 /* Alright we're fully a part of this domain
754 * so we keep some state as to who's joining
755 * and indicate to him that needs to be fixed
758 __dlm_set_joining_node(dlm, query->node_idx);
761 spin_unlock(&dlm->spinlock);
764 spin_unlock(&dlm_domain_lock);
767 mlog(0, "We respond with %u\n", response);
772 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
775 struct dlm_assert_joined *assert;
776 struct dlm_ctxt *dlm = NULL;
778 assert = (struct dlm_assert_joined *) msg->buf;
780 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
783 spin_lock(&dlm_domain_lock);
784 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
785 /* XXX should we consider no dlm ctxt an error? */
787 spin_lock(&dlm->spinlock);
789 /* Alright, this node has officially joined our
790 * domain. Set him in the map and clean up our
791 * leftover join state. */
792 BUG_ON(dlm->joining_node != assert->node_idx);
793 set_bit(assert->node_idx, dlm->domain_map);
794 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
796 printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
797 assert->node_idx, dlm->name);
798 __dlm_print_nodes(dlm);
800 /* notify anything attached to the heartbeat events */
801 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
803 spin_unlock(&dlm->spinlock);
805 spin_unlock(&dlm_domain_lock);
810 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
813 struct dlm_cancel_join *cancel;
814 struct dlm_ctxt *dlm = NULL;
816 cancel = (struct dlm_cancel_join *) msg->buf;
818 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
821 spin_lock(&dlm_domain_lock);
822 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
825 spin_lock(&dlm->spinlock);
827 /* Yikes, this guy wants to cancel his join. No
828 * problem, we simply cleanup our join state. */
829 BUG_ON(dlm->joining_node != cancel->node_idx);
830 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
832 spin_unlock(&dlm->spinlock);
834 spin_unlock(&dlm_domain_lock);
839 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
843 struct dlm_cancel_join cancel_msg;
845 memset(&cancel_msg, 0, sizeof(cancel_msg));
846 cancel_msg.node_idx = dlm->node_num;
847 cancel_msg.name_len = strlen(dlm->name);
848 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
850 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
851 &cancel_msg, sizeof(cancel_msg), node,
862 /* map_size should be in bytes. */
863 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
864 unsigned long *node_map,
865 unsigned int map_size)
870 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
871 sizeof(unsigned long))) {
873 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
874 map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
880 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
881 node + 1)) < O2NM_MAX_NODES) {
882 if (node == dlm->node_num)
885 tmpstat = dlm_send_one_join_cancel(dlm, node);
887 mlog(ML_ERROR, "Error return %d cancelling join on "
888 "node %d\n", tmpstat, node);
899 static int dlm_request_join(struct dlm_ctxt *dlm,
901 enum dlm_query_join_response *response)
904 struct dlm_query_join_request join_msg;
906 mlog(0, "querying node %d\n", node);
908 memset(&join_msg, 0, sizeof(join_msg));
909 join_msg.node_idx = dlm->node_num;
910 join_msg.name_len = strlen(dlm->name);
911 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
913 /* copy live node map to join message */
914 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
916 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
917 sizeof(join_msg), node, &retval);
918 if (status < 0 && status != -ENOPROTOOPT) {
923 /* -ENOPROTOOPT from the net code means the other side isn't
924 listening for our message type -- that's fine, it means
925 his dlm isn't up, so we can consider him a 'yes' but not
926 joined into the domain. */
927 if (status == -ENOPROTOOPT) {
929 *response = JOIN_OK_NO_MAP;
930 } else if (retval == JOIN_DISALLOW ||
932 retval == JOIN_OK_NO_MAP) {
936 mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
940 mlog(0, "status %d, node %d response is %d\n", status, node,
947 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
951 struct dlm_assert_joined assert_msg;
953 mlog(0, "Sending join assert to node %u\n", node);
955 memset(&assert_msg, 0, sizeof(assert_msg));
956 assert_msg.node_idx = dlm->node_num;
957 assert_msg.name_len = strlen(dlm->name);
958 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
960 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
961 &assert_msg, sizeof(assert_msg), node,
969 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
970 unsigned long *node_map)
972 int status, node, live;
976 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
977 node + 1)) < O2NM_MAX_NODES) {
978 if (node == dlm->node_num)
982 /* It is very important that this message be
983 * received so we spin until either the node
984 * has died or it gets the message. */
985 status = dlm_send_one_join_assert(dlm, node);
987 spin_lock(&dlm->spinlock);
988 live = test_bit(node, dlm->live_nodes_map);
989 spin_unlock(&dlm->spinlock);
992 mlog(ML_ERROR, "Error return %d asserting "
993 "join on node %d\n", status, node);
995 /* give us some time between errors... */
997 msleep(DLM_DOMAIN_BACKOFF_MS);
999 } while (status && live);
1003 struct domain_join_ctxt {
1004 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1005 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1008 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1009 struct domain_join_ctxt *ctxt,
1010 enum dlm_query_join_response response)
1014 if (response == JOIN_DISALLOW) {
1015 mlog(0, "Latest response of disallow -- should restart\n");
1019 spin_lock(&dlm->spinlock);
1020 /* For now, we restart the process if the node maps have
1022 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1023 sizeof(dlm->live_nodes_map));
1024 spin_unlock(&dlm->spinlock);
1027 mlog(0, "Node maps changed -- should restart\n");
1032 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1034 int status = 0, tmpstat, node;
1035 struct domain_join_ctxt *ctxt;
1036 enum dlm_query_join_response response;
1038 mlog_entry("%p", dlm);
1040 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1047 /* group sem locking should work for us here -- we're already
1048 * registered for heartbeat events so filling this should be
1049 * atomic wrt getting those handlers called. */
1050 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1052 spin_lock(&dlm->spinlock);
1053 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1055 __dlm_set_joining_node(dlm, dlm->node_num);
1057 spin_unlock(&dlm->spinlock);
1060 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1061 node + 1)) < O2NM_MAX_NODES) {
1062 if (node == dlm->node_num)
1065 status = dlm_request_join(dlm, node, &response);
1071 /* Ok, either we got a response or the node doesn't have a
1073 if (response == JOIN_OK)
1074 set_bit(node, ctxt->yes_resp_map);
1076 if (dlm_should_restart_join(dlm, ctxt, response)) {
1082 mlog(0, "Yay, done querying nodes!\n");
1084 /* Yay, everyone agree's we can join the domain. My domain is
1085 * comprised of all nodes who were put in the
1086 * yes_resp_map. Copy that into our domain map and send a join
1087 * assert message to clean up everyone elses state. */
1088 spin_lock(&dlm->spinlock);
1089 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1090 sizeof(ctxt->yes_resp_map));
1091 set_bit(dlm->node_num, dlm->domain_map);
1092 spin_unlock(&dlm->spinlock);
1094 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1096 /* Joined state *must* be set before the joining node
1097 * information, otherwise the query_join handler may read no
1098 * current joiner but a state of NEW and tell joining nodes
1099 * we're not in the domain. */
1100 spin_lock(&dlm_domain_lock);
1101 dlm->dlm_state = DLM_CTXT_JOINED;
1103 spin_unlock(&dlm_domain_lock);
1106 spin_lock(&dlm->spinlock);
1107 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1109 __dlm_print_nodes(dlm);
1110 spin_unlock(&dlm->spinlock);
1113 /* Do we need to send a cancel message to any nodes? */
1115 tmpstat = dlm_send_join_cancels(dlm,
1117 sizeof(ctxt->yes_resp_map));
1119 mlog_errno(tmpstat);
1124 mlog(0, "returning %d\n", status);
1128 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1130 o2hb_unregister_callback(&dlm->dlm_hb_up);
1131 o2hb_unregister_callback(&dlm->dlm_hb_down);
1132 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1135 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1139 mlog(0, "registering handlers.\n");
1141 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1142 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1143 status = o2hb_register_callback(&dlm->dlm_hb_down);
1147 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1148 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1149 status = o2hb_register_callback(&dlm->dlm_hb_up);
1153 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1154 sizeof(struct dlm_master_request),
1155 dlm_master_request_handler,
1156 dlm, NULL, &dlm->dlm_domain_handlers);
1160 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1161 sizeof(struct dlm_assert_master),
1162 dlm_assert_master_handler,
1163 dlm, dlm_assert_master_post_handler,
1164 &dlm->dlm_domain_handlers);
1168 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1169 sizeof(struct dlm_create_lock),
1170 dlm_create_lock_handler,
1171 dlm, NULL, &dlm->dlm_domain_handlers);
1175 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1176 DLM_CONVERT_LOCK_MAX_LEN,
1177 dlm_convert_lock_handler,
1178 dlm, NULL, &dlm->dlm_domain_handlers);
1182 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1183 DLM_UNLOCK_LOCK_MAX_LEN,
1184 dlm_unlock_lock_handler,
1185 dlm, NULL, &dlm->dlm_domain_handlers);
1189 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1190 DLM_PROXY_AST_MAX_LEN,
1191 dlm_proxy_ast_handler,
1192 dlm, NULL, &dlm->dlm_domain_handlers);
1196 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1197 sizeof(struct dlm_exit_domain),
1198 dlm_exit_domain_handler,
1199 dlm, NULL, &dlm->dlm_domain_handlers);
1203 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1204 sizeof(struct dlm_deref_lockres),
1205 dlm_deref_lockres_handler,
1206 dlm, NULL, &dlm->dlm_domain_handlers);
1210 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1211 sizeof(struct dlm_migrate_request),
1212 dlm_migrate_request_handler,
1213 dlm, NULL, &dlm->dlm_domain_handlers);
1217 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1218 DLM_MIG_LOCKRES_MAX_LEN,
1219 dlm_mig_lockres_handler,
1220 dlm, NULL, &dlm->dlm_domain_handlers);
1224 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1225 sizeof(struct dlm_master_requery),
1226 dlm_master_requery_handler,
1227 dlm, NULL, &dlm->dlm_domain_handlers);
1231 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1232 sizeof(struct dlm_lock_request),
1233 dlm_request_all_locks_handler,
1234 dlm, NULL, &dlm->dlm_domain_handlers);
1238 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1239 sizeof(struct dlm_reco_data_done),
1240 dlm_reco_data_done_handler,
1241 dlm, NULL, &dlm->dlm_domain_handlers);
1245 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1246 sizeof(struct dlm_begin_reco),
1247 dlm_begin_reco_handler,
1248 dlm, NULL, &dlm->dlm_domain_handlers);
1252 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1253 sizeof(struct dlm_finalize_reco),
1254 dlm_finalize_reco_handler,
1255 dlm, NULL, &dlm->dlm_domain_handlers);
1261 dlm_unregister_domain_handlers(dlm);
1266 static int dlm_join_domain(struct dlm_ctxt *dlm)
1269 unsigned int backoff;
1270 unsigned int total_backoff = 0;
1274 mlog(0, "Join domain %s\n", dlm->name);
1276 status = dlm_register_domain_handlers(dlm);
1282 status = dlm_launch_thread(dlm);
1288 status = dlm_launch_recovery_thread(dlm);
1294 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1295 if (!dlm->dlm_worker) {
1302 status = dlm_try_to_join_domain(dlm);
1304 /* If we're racing another node to the join, then we
1305 * need to back off temporarily and let them
1307 #define DLM_JOIN_TIMEOUT_MSECS 90000
1308 if (status == -EAGAIN) {
1309 if (signal_pending(current)) {
1310 status = -ERESTARTSYS;
1315 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1316 status = -ERESTARTSYS;
1317 mlog(ML_NOTICE, "Timed out joining dlm domain "
1318 "%s after %u msecs\n", dlm->name,
1319 jiffies_to_msecs(total_backoff));
1325 * <dale> No, after you!
1327 * <dale> But you first!
1330 backoff = (unsigned int)(jiffies & 0x3);
1331 backoff *= DLM_DOMAIN_BACKOFF_MS;
1332 total_backoff += backoff;
1333 mlog(0, "backoff %d\n", backoff);
1336 } while (status == -EAGAIN);
1345 wake_up(&dlm_domain_events);
1348 dlm_unregister_domain_handlers(dlm);
1349 dlm_complete_thread(dlm);
1350 dlm_complete_recovery_thread(dlm);
1351 dlm_destroy_dlm_worker(dlm);
1357 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1361 struct dlm_ctxt *dlm = NULL;
1363 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1365 mlog_errno(-ENOMEM);
1369 dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1370 if (dlm->name == NULL) {
1371 mlog_errno(-ENOMEM);
1377 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1378 if (!dlm->lockres_hash) {
1379 mlog_errno(-ENOMEM);
1386 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1387 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1389 strcpy(dlm->name, domain);
1391 dlm->node_num = o2nm_this_node();
1393 spin_lock_init(&dlm->spinlock);
1394 spin_lock_init(&dlm->master_lock);
1395 spin_lock_init(&dlm->ast_lock);
1396 INIT_LIST_HEAD(&dlm->list);
1397 INIT_LIST_HEAD(&dlm->dirty_list);
1398 INIT_LIST_HEAD(&dlm->reco.resources);
1399 INIT_LIST_HEAD(&dlm->reco.received);
1400 INIT_LIST_HEAD(&dlm->reco.node_data);
1401 INIT_LIST_HEAD(&dlm->purge_list);
1402 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1403 dlm->reco.state = 0;
1405 INIT_LIST_HEAD(&dlm->pending_asts);
1406 INIT_LIST_HEAD(&dlm->pending_basts);
1408 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1409 dlm->recovery_map, &(dlm->recovery_map[0]));
1411 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1412 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1413 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1415 dlm->dlm_thread_task = NULL;
1416 dlm->dlm_reco_thread_task = NULL;
1417 dlm->dlm_worker = NULL;
1418 init_waitqueue_head(&dlm->dlm_thread_wq);
1419 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1420 init_waitqueue_head(&dlm->reco.event);
1421 init_waitqueue_head(&dlm->ast_wq);
1422 init_waitqueue_head(&dlm->migration_wq);
1423 INIT_LIST_HEAD(&dlm->master_list);
1424 INIT_LIST_HEAD(&dlm->mle_hb_events);
1426 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1427 init_waitqueue_head(&dlm->dlm_join_events);
1429 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1430 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1431 atomic_set(&dlm->local_resources, 0);
1432 atomic_set(&dlm->remote_resources, 0);
1433 atomic_set(&dlm->unknown_resources, 0);
1435 spin_lock_init(&dlm->work_lock);
1436 INIT_LIST_HEAD(&dlm->work_list);
1437 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
1439 kref_init(&dlm->dlm_refs);
1440 dlm->dlm_state = DLM_CTXT_NEW;
1442 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1444 mlog(0, "context init: refcount %u\n",
1445 atomic_read(&dlm->dlm_refs.refcount));
1452 * dlm_register_domain: one-time setup per "domain"
1454 struct dlm_ctxt * dlm_register_domain(const char *domain,
1458 struct dlm_ctxt *dlm = NULL;
1459 struct dlm_ctxt *new_ctxt = NULL;
1461 if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1462 ret = -ENAMETOOLONG;
1463 mlog(ML_ERROR, "domain name length too long\n");
1467 if (!o2hb_check_local_node_heartbeating()) {
1468 mlog(ML_ERROR, "the local node has not been configured, or is "
1469 "not heartbeating\n");
1474 mlog(0, "register called for domain \"%s\"\n", domain);
1478 if (signal_pending(current)) {
1484 spin_lock(&dlm_domain_lock);
1486 dlm = __dlm_lookup_domain(domain);
1488 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1489 spin_unlock(&dlm_domain_lock);
1491 mlog(0, "This ctxt is not joined yet!\n");
1492 wait_event_interruptible(dlm_domain_events,
1493 dlm_wait_on_domain_helper(
1501 spin_unlock(&dlm_domain_lock);
1509 spin_unlock(&dlm_domain_lock);
1511 new_ctxt = dlm_alloc_ctxt(domain, key);
1520 /* a little variable switch-a-roo here... */
1524 /* add the new domain */
1525 list_add_tail(&dlm->list, &dlm_domains);
1526 spin_unlock(&dlm_domain_lock);
1528 ret = dlm_join_domain(dlm);
1538 dlm_free_ctxt_mem(new_ctxt);
1545 EXPORT_SYMBOL_GPL(dlm_register_domain);
1547 static LIST_HEAD(dlm_join_handlers);
1549 static void dlm_unregister_net_handlers(void)
1551 o2net_unregister_handler_list(&dlm_join_handlers);
1554 static int dlm_register_net_handlers(void)
1558 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1559 sizeof(struct dlm_query_join_request),
1560 dlm_query_join_handler,
1561 NULL, NULL, &dlm_join_handlers);
1565 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1566 sizeof(struct dlm_assert_joined),
1567 dlm_assert_joined_handler,
1568 NULL, NULL, &dlm_join_handlers);
1572 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1573 sizeof(struct dlm_cancel_join),
1574 dlm_cancel_join_handler,
1575 NULL, NULL, &dlm_join_handlers);
1579 dlm_unregister_net_handlers();
1584 /* Domain eviction callback handling.
1586 * The file system requires notification of node death *before* the
1587 * dlm completes it's recovery work, otherwise it may be able to
1588 * acquire locks on resources requiring recovery. Since the dlm can
1589 * evict a node from it's domain *before* heartbeat fires, a similar
1590 * mechanism is required. */
1592 /* Eviction is not expected to happen often, so a per-domain lock is
1593 * not necessary. Eviction callbacks are allowed to sleep for short
1594 * periods of time. */
1595 static DECLARE_RWSEM(dlm_callback_sem);
1597 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1600 struct list_head *iter;
1601 struct dlm_eviction_cb *cb;
1603 down_read(&dlm_callback_sem);
1604 list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1605 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1607 cb->ec_func(node_num, cb->ec_data);
1609 up_read(&dlm_callback_sem);
1612 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1613 dlm_eviction_func *f,
1616 INIT_LIST_HEAD(&cb->ec_item);
1620 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1622 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1623 struct dlm_eviction_cb *cb)
1625 down_write(&dlm_callback_sem);
1626 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1627 up_write(&dlm_callback_sem);
1629 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1631 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1633 down_write(&dlm_callback_sem);
1634 list_del_init(&cb->ec_item);
1635 up_write(&dlm_callback_sem);
1637 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1639 static int __init dlm_init(void)
1643 dlm_print_version();
1645 status = dlm_init_mle_cache();
1649 status = dlm_register_net_handlers();
1651 dlm_destroy_mle_cache();
1658 static void __exit dlm_exit (void)
1660 dlm_unregister_net_handlers();
1661 dlm_destroy_mle_cache();
1664 MODULE_AUTHOR("Oracle");
1665 MODULE_LICENSE("GPL");
1667 module_init(dlm_init);
1668 module_exit(dlm_exit);