Initial commit
[wrt350n-kernel.git] / fs / ocfs2 / dlm / dlmdomain.c
blob638d2ebb892bbdbb20c17f7bd4ba0d9f4aea4aee
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * dlmdomain.c
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"
41 #include "dlmapi.h"
42 #include "dlmcommon.h"
44 #include "dlmdomain.h"
46 #include "dlmver.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[],
68 unsigned int sz)
70 unsigned int nn;
72 if (!sz)
73 return;
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)
83 while (pages--)
84 free_page((unsigned long)vec[pages]);
85 kfree(vec);
88 static void **dlm_alloc_pagevec(int pages)
90 void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
91 int i;
93 if (!vec)
94 return NULL;
96 for (i = 0; i < pages; i++)
97 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
98 goto out_free;
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);
103 return vec;
104 out_free:
105 dlm_free_pagevec(vec, i);
106 return NULL;
111 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
112 * dlm_domain_lock
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
118 * dlm_lock->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 static const struct dlm_protocol_version dlm_protocol = {
133 .pv_major = 1,
134 .pv_minor = 0,
137 #define DLM_DOMAIN_BACKOFF_MS 200
139 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
140 void **ret_data);
141 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
142 void **ret_data);
143 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
144 void **ret_data);
145 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
146 void **ret_data);
147 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
148 struct dlm_protocol_version *request);
150 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
152 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
154 if (!hlist_unhashed(&lockres->hash_node)) {
155 hlist_del_init(&lockres->hash_node);
156 dlm_lockres_put(lockres);
160 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
161 struct dlm_lock_resource *res)
163 struct hlist_head *bucket;
164 struct qstr *q;
166 assert_spin_locked(&dlm->spinlock);
168 q = &res->lockname;
169 bucket = dlm_lockres_hash(dlm, q->hash);
171 /* get a reference for our hashtable */
172 dlm_lockres_get(res);
174 hlist_add_head(&res->hash_node, bucket);
177 struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
178 const char *name,
179 unsigned int len,
180 unsigned int hash)
182 struct hlist_head *bucket;
183 struct hlist_node *list;
185 mlog_entry("%.*s\n", len, name);
187 assert_spin_locked(&dlm->spinlock);
189 bucket = dlm_lockres_hash(dlm, hash);
191 hlist_for_each(list, bucket) {
192 struct dlm_lock_resource *res = hlist_entry(list,
193 struct dlm_lock_resource, hash_node);
194 if (res->lockname.name[0] != name[0])
195 continue;
196 if (unlikely(res->lockname.len != len))
197 continue;
198 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
199 continue;
200 dlm_lockres_get(res);
201 return res;
203 return NULL;
206 /* intended to be called by functions which do not care about lock
207 * resources which are being purged (most net _handler functions).
208 * this will return NULL for any lock resource which is found but
209 * currently in the process of dropping its mastery reference.
210 * use __dlm_lookup_lockres_full when you need the lock resource
211 * regardless (e.g. dlm_get_lock_resource) */
212 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
213 const char *name,
214 unsigned int len,
215 unsigned int hash)
217 struct dlm_lock_resource *res = NULL;
219 mlog_entry("%.*s\n", len, name);
221 assert_spin_locked(&dlm->spinlock);
223 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
224 if (res) {
225 spin_lock(&res->spinlock);
226 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
227 spin_unlock(&res->spinlock);
228 dlm_lockres_put(res);
229 return NULL;
231 spin_unlock(&res->spinlock);
234 return res;
237 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
238 const char *name,
239 unsigned int len)
241 struct dlm_lock_resource *res;
242 unsigned int hash = dlm_lockid_hash(name, len);
244 spin_lock(&dlm->spinlock);
245 res = __dlm_lookup_lockres(dlm, name, len, hash);
246 spin_unlock(&dlm->spinlock);
247 return res;
250 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
252 struct dlm_ctxt *tmp = NULL;
253 struct list_head *iter;
255 assert_spin_locked(&dlm_domain_lock);
257 /* tmp->name here is always NULL terminated,
258 * but domain may not be! */
259 list_for_each(iter, &dlm_domains) {
260 tmp = list_entry (iter, struct dlm_ctxt, list);
261 if (strlen(tmp->name) == len &&
262 memcmp(tmp->name, domain, len)==0)
263 break;
264 tmp = NULL;
267 return tmp;
270 /* For null terminated domain strings ONLY */
271 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
273 assert_spin_locked(&dlm_domain_lock);
275 return __dlm_lookup_domain_full(domain, strlen(domain));
279 /* returns true on one of two conditions:
280 * 1) the domain does not exist
281 * 2) the domain exists and it's state is "joined" */
282 static int dlm_wait_on_domain_helper(const char *domain)
284 int ret = 0;
285 struct dlm_ctxt *tmp = NULL;
287 spin_lock(&dlm_domain_lock);
289 tmp = __dlm_lookup_domain(domain);
290 if (!tmp)
291 ret = 1;
292 else if (tmp->dlm_state == DLM_CTXT_JOINED)
293 ret = 1;
295 spin_unlock(&dlm_domain_lock);
296 return ret;
299 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
301 if (dlm->lockres_hash)
302 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
304 if (dlm->name)
305 kfree(dlm->name);
307 kfree(dlm);
310 /* A little strange - this function will be called while holding
311 * dlm_domain_lock and is expected to be holding it on the way out. We
312 * will however drop and reacquire it multiple times */
313 static void dlm_ctxt_release(struct kref *kref)
315 struct dlm_ctxt *dlm;
317 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
319 BUG_ON(dlm->num_joins);
320 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
322 /* we may still be in the list if we hit an error during join. */
323 list_del_init(&dlm->list);
325 spin_unlock(&dlm_domain_lock);
327 mlog(0, "freeing memory from domain %s\n", dlm->name);
329 wake_up(&dlm_domain_events);
331 dlm_free_ctxt_mem(dlm);
333 spin_lock(&dlm_domain_lock);
336 void dlm_put(struct dlm_ctxt *dlm)
338 spin_lock(&dlm_domain_lock);
339 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
340 spin_unlock(&dlm_domain_lock);
343 static void __dlm_get(struct dlm_ctxt *dlm)
345 kref_get(&dlm->dlm_refs);
348 /* given a questionable reference to a dlm object, gets a reference if
349 * it can find it in the list, otherwise returns NULL in which case
350 * you shouldn't trust your pointer. */
351 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
353 struct list_head *iter;
354 struct dlm_ctxt *target = NULL;
356 spin_lock(&dlm_domain_lock);
358 list_for_each(iter, &dlm_domains) {
359 target = list_entry (iter, struct dlm_ctxt, list);
361 if (target == dlm) {
362 __dlm_get(target);
363 break;
366 target = NULL;
369 spin_unlock(&dlm_domain_lock);
371 return target;
374 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
376 int ret;
378 spin_lock(&dlm_domain_lock);
379 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
380 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
381 spin_unlock(&dlm_domain_lock);
383 return ret;
386 static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
388 if (dlm->dlm_worker) {
389 flush_workqueue(dlm->dlm_worker);
390 destroy_workqueue(dlm->dlm_worker);
391 dlm->dlm_worker = NULL;
395 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
397 dlm_unregister_domain_handlers(dlm);
398 dlm_complete_thread(dlm);
399 dlm_complete_recovery_thread(dlm);
400 dlm_destroy_dlm_worker(dlm);
402 /* We've left the domain. Now we can take ourselves out of the
403 * list and allow the kref stuff to help us free the
404 * memory. */
405 spin_lock(&dlm_domain_lock);
406 list_del_init(&dlm->list);
407 spin_unlock(&dlm_domain_lock);
409 /* Wake up anyone waiting for us to remove this domain */
410 wake_up(&dlm_domain_events);
413 static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
415 int i, num, n, ret = 0;
416 struct dlm_lock_resource *res;
417 struct hlist_node *iter;
418 struct hlist_head *bucket;
419 int dropped;
421 mlog(0, "Migrating locks from domain %s\n", dlm->name);
423 num = 0;
424 spin_lock(&dlm->spinlock);
425 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
426 redo_bucket:
427 n = 0;
428 bucket = dlm_lockres_hash(dlm, i);
429 iter = bucket->first;
430 while (iter) {
431 n++;
432 res = hlist_entry(iter, struct dlm_lock_resource,
433 hash_node);
434 dlm_lockres_get(res);
435 /* migrate, if necessary. this will drop the dlm
436 * spinlock and retake it if it does migration. */
437 dropped = dlm_empty_lockres(dlm, res);
439 spin_lock(&res->spinlock);
440 __dlm_lockres_calc_usage(dlm, res);
441 iter = res->hash_node.next;
442 spin_unlock(&res->spinlock);
444 dlm_lockres_put(res);
446 if (dropped)
447 goto redo_bucket;
449 cond_resched_lock(&dlm->spinlock);
450 num += n;
451 mlog(0, "%s: touched %d lockreses in bucket %d "
452 "(tot=%d)\n", dlm->name, n, i, num);
454 spin_unlock(&dlm->spinlock);
455 wake_up(&dlm->dlm_thread_wq);
457 /* let the dlm thread take care of purging, keep scanning until
458 * nothing remains in the hash */
459 if (num) {
460 mlog(0, "%s: %d lock resources in hash last pass\n",
461 dlm->name, num);
462 ret = -EAGAIN;
464 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
465 return ret;
468 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
470 int ret;
472 spin_lock(&dlm->spinlock);
473 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
474 spin_unlock(&dlm->spinlock);
476 return ret;
479 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
481 /* Yikes, a double spinlock! I need domain_lock for the dlm
482 * state and the dlm spinlock for join state... Sorry! */
483 again:
484 spin_lock(&dlm_domain_lock);
485 spin_lock(&dlm->spinlock);
487 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
488 mlog(0, "Node %d is joining, we wait on it.\n",
489 dlm->joining_node);
490 spin_unlock(&dlm->spinlock);
491 spin_unlock(&dlm_domain_lock);
493 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
494 goto again;
497 dlm->dlm_state = DLM_CTXT_LEAVING;
498 spin_unlock(&dlm->spinlock);
499 spin_unlock(&dlm_domain_lock);
502 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
504 int node = -1;
506 assert_spin_locked(&dlm->spinlock);
508 printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
510 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
511 node + 1)) < O2NM_MAX_NODES) {
512 printk("%d ", node);
514 printk("\n");
517 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
518 void **ret_data)
520 struct dlm_ctxt *dlm = data;
521 unsigned int node;
522 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
524 mlog_entry("%p %u %p", msg, len, data);
526 if (!dlm_grab(dlm))
527 return 0;
529 node = exit_msg->node_idx;
531 printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
533 spin_lock(&dlm->spinlock);
534 clear_bit(node, dlm->domain_map);
535 __dlm_print_nodes(dlm);
537 /* notify anything attached to the heartbeat events */
538 dlm_hb_event_notify_attached(dlm, node, 0);
540 spin_unlock(&dlm->spinlock);
542 dlm_put(dlm);
544 return 0;
547 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
548 unsigned int node)
550 int status;
551 struct dlm_exit_domain leave_msg;
553 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
554 node, dlm->name, dlm->node_num);
556 memset(&leave_msg, 0, sizeof(leave_msg));
557 leave_msg.node_idx = dlm->node_num;
559 status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
560 &leave_msg, sizeof(leave_msg), node,
561 NULL);
563 mlog(0, "status return %d from o2net_send_message\n", status);
565 return status;
569 static void dlm_leave_domain(struct dlm_ctxt *dlm)
571 int node, clear_node, status;
573 /* At this point we've migrated away all our locks and won't
574 * accept mastership of new ones. The dlm is responsible for
575 * almost nothing now. We make sure not to confuse any joining
576 * nodes and then commence shutdown procedure. */
578 spin_lock(&dlm->spinlock);
579 /* Clear ourselves from the domain map */
580 clear_bit(dlm->node_num, dlm->domain_map);
581 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
582 0)) < O2NM_MAX_NODES) {
583 /* Drop the dlm spinlock. This is safe wrt the domain_map.
584 * -nodes cannot be added now as the
585 * query_join_handlers knows to respond with OK_NO_MAP
586 * -we catch the right network errors if a node is
587 * removed from the map while we're sending him the
588 * exit message. */
589 spin_unlock(&dlm->spinlock);
591 clear_node = 1;
593 status = dlm_send_one_domain_exit(dlm, node);
594 if (status < 0 &&
595 status != -ENOPROTOOPT &&
596 status != -ENOTCONN) {
597 mlog(ML_NOTICE, "Error %d sending domain exit message "
598 "to node %d\n", status, node);
600 /* Not sure what to do here but lets sleep for
601 * a bit in case this was a transient
602 * error... */
603 msleep(DLM_DOMAIN_BACKOFF_MS);
604 clear_node = 0;
607 spin_lock(&dlm->spinlock);
608 /* If we're not clearing the node bit then we intend
609 * to loop back around to try again. */
610 if (clear_node)
611 clear_bit(node, dlm->domain_map);
613 spin_unlock(&dlm->spinlock);
616 int dlm_joined(struct dlm_ctxt *dlm)
618 int ret = 0;
620 spin_lock(&dlm_domain_lock);
622 if (dlm->dlm_state == DLM_CTXT_JOINED)
623 ret = 1;
625 spin_unlock(&dlm_domain_lock);
627 return ret;
630 int dlm_shutting_down(struct dlm_ctxt *dlm)
632 int ret = 0;
634 spin_lock(&dlm_domain_lock);
636 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
637 ret = 1;
639 spin_unlock(&dlm_domain_lock);
641 return ret;
644 void dlm_unregister_domain(struct dlm_ctxt *dlm)
646 int leave = 0;
648 spin_lock(&dlm_domain_lock);
649 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
650 BUG_ON(!dlm->num_joins);
652 dlm->num_joins--;
653 if (!dlm->num_joins) {
654 /* We mark it "in shutdown" now so new register
655 * requests wait until we've completely left the
656 * domain. Don't use DLM_CTXT_LEAVING yet as we still
657 * want new domain joins to communicate with us at
658 * least until we've completed migration of our
659 * resources. */
660 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
661 leave = 1;
663 spin_unlock(&dlm_domain_lock);
665 if (leave) {
666 mlog(0, "shutting down domain %s\n", dlm->name);
668 /* We changed dlm state, notify the thread */
669 dlm_kick_thread(dlm, NULL);
671 while (dlm_migrate_all_locks(dlm)) {
672 /* Give dlm_thread time to purge the lockres' */
673 msleep(500);
674 mlog(0, "%s: more migration to do\n", dlm->name);
676 dlm_mark_domain_leaving(dlm);
677 dlm_leave_domain(dlm);
678 dlm_complete_dlm_shutdown(dlm);
680 dlm_put(dlm);
682 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
684 static int dlm_query_join_proto_check(char *proto_type, int node,
685 struct dlm_protocol_version *ours,
686 struct dlm_protocol_version *request)
688 int rc;
689 struct dlm_protocol_version proto = *request;
691 if (!dlm_protocol_compare(ours, &proto)) {
692 mlog(0,
693 "node %u wanted to join with %s locking protocol "
694 "%u.%u, we respond with %u.%u\n",
695 node, proto_type,
696 request->pv_major,
697 request->pv_minor,
698 proto.pv_major, proto.pv_minor);
699 request->pv_minor = proto.pv_minor;
700 rc = 0;
701 } else {
702 mlog(ML_NOTICE,
703 "Node %u wanted to join with %s locking "
704 "protocol %u.%u, but we have %u.%u, disallowing\n",
705 node, proto_type,
706 request->pv_major,
707 request->pv_minor,
708 ours->pv_major,
709 ours->pv_minor);
710 rc = 1;
713 return rc;
716 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
717 void **ret_data)
719 struct dlm_query_join_request *query;
720 union dlm_query_join_response response = {
721 .packet.code = JOIN_DISALLOW,
723 struct dlm_ctxt *dlm = NULL;
724 u8 nodenum;
726 query = (struct dlm_query_join_request *) msg->buf;
728 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
729 query->domain);
732 * If heartbeat doesn't consider the node live, tell it
733 * to back off and try again. This gives heartbeat a chance
734 * to catch up.
736 if (!o2hb_check_node_heartbeating(query->node_idx)) {
737 mlog(0, "node %u is not in our live map yet\n",
738 query->node_idx);
740 response.packet.code = JOIN_DISALLOW;
741 goto respond;
744 response.packet.code = JOIN_OK_NO_MAP;
746 spin_lock(&dlm_domain_lock);
747 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
748 if (!dlm)
749 goto unlock_respond;
752 * There is a small window where the joining node may not see the
753 * node(s) that just left but still part of the cluster. DISALLOW
754 * join request if joining node has different node map.
756 nodenum=0;
757 while (nodenum < O2NM_MAX_NODES) {
758 if (test_bit(nodenum, dlm->domain_map)) {
759 if (!byte_test_bit(nodenum, query->node_map)) {
760 mlog(0, "disallow join as node %u does not "
761 "have node %u in its nodemap\n",
762 query->node_idx, nodenum);
763 response.packet.code = JOIN_DISALLOW;
764 goto unlock_respond;
767 nodenum++;
770 /* Once the dlm ctxt is marked as leaving then we don't want
771 * to be put in someone's domain map.
772 * Also, explicitly disallow joining at certain troublesome
773 * times (ie. during recovery). */
774 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
775 int bit = query->node_idx;
776 spin_lock(&dlm->spinlock);
778 if (dlm->dlm_state == DLM_CTXT_NEW &&
779 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
780 /*If this is a brand new context and we
781 * haven't started our join process yet, then
782 * the other node won the race. */
783 response.packet.code = JOIN_OK_NO_MAP;
784 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
785 /* Disallow parallel joins. */
786 response.packet.code = JOIN_DISALLOW;
787 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
788 mlog(0, "node %u trying to join, but recovery "
789 "is ongoing.\n", bit);
790 response.packet.code = JOIN_DISALLOW;
791 } else if (test_bit(bit, dlm->recovery_map)) {
792 mlog(0, "node %u trying to join, but it "
793 "still needs recovery.\n", bit);
794 response.packet.code = JOIN_DISALLOW;
795 } else if (test_bit(bit, dlm->domain_map)) {
796 mlog(0, "node %u trying to join, but it "
797 "is still in the domain! needs recovery?\n",
798 bit);
799 response.packet.code = JOIN_DISALLOW;
800 } else {
801 /* Alright we're fully a part of this domain
802 * so we keep some state as to who's joining
803 * and indicate to him that needs to be fixed
804 * up. */
806 /* Make sure we speak compatible locking protocols. */
807 if (dlm_query_join_proto_check("DLM", bit,
808 &dlm->dlm_locking_proto,
809 &query->dlm_proto)) {
810 response.packet.code =
811 JOIN_PROTOCOL_MISMATCH;
812 } else if (dlm_query_join_proto_check("fs", bit,
813 &dlm->fs_locking_proto,
814 &query->fs_proto)) {
815 response.packet.code =
816 JOIN_PROTOCOL_MISMATCH;
817 } else {
818 response.packet.dlm_minor =
819 query->dlm_proto.pv_minor;
820 response.packet.fs_minor =
821 query->fs_proto.pv_minor;
822 response.packet.code = JOIN_OK;
823 __dlm_set_joining_node(dlm, query->node_idx);
827 spin_unlock(&dlm->spinlock);
829 unlock_respond:
830 spin_unlock(&dlm_domain_lock);
832 respond:
833 mlog(0, "We respond with %u\n", response.packet.code);
835 return response.intval;
838 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
839 void **ret_data)
841 struct dlm_assert_joined *assert;
842 struct dlm_ctxt *dlm = NULL;
844 assert = (struct dlm_assert_joined *) msg->buf;
846 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
847 assert->domain);
849 spin_lock(&dlm_domain_lock);
850 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
851 /* XXX should we consider no dlm ctxt an error? */
852 if (dlm) {
853 spin_lock(&dlm->spinlock);
855 /* Alright, this node has officially joined our
856 * domain. Set him in the map and clean up our
857 * leftover join state. */
858 BUG_ON(dlm->joining_node != assert->node_idx);
859 set_bit(assert->node_idx, dlm->domain_map);
860 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
862 printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
863 assert->node_idx, dlm->name);
864 __dlm_print_nodes(dlm);
866 /* notify anything attached to the heartbeat events */
867 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
869 spin_unlock(&dlm->spinlock);
871 spin_unlock(&dlm_domain_lock);
873 return 0;
876 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
877 void **ret_data)
879 struct dlm_cancel_join *cancel;
880 struct dlm_ctxt *dlm = NULL;
882 cancel = (struct dlm_cancel_join *) msg->buf;
884 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
885 cancel->domain);
887 spin_lock(&dlm_domain_lock);
888 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
890 if (dlm) {
891 spin_lock(&dlm->spinlock);
893 /* Yikes, this guy wants to cancel his join. No
894 * problem, we simply cleanup our join state. */
895 BUG_ON(dlm->joining_node != cancel->node_idx);
896 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
898 spin_unlock(&dlm->spinlock);
900 spin_unlock(&dlm_domain_lock);
902 return 0;
905 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
906 unsigned int node)
908 int status;
909 struct dlm_cancel_join cancel_msg;
911 memset(&cancel_msg, 0, sizeof(cancel_msg));
912 cancel_msg.node_idx = dlm->node_num;
913 cancel_msg.name_len = strlen(dlm->name);
914 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
916 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
917 &cancel_msg, sizeof(cancel_msg), node,
918 NULL);
919 if (status < 0) {
920 mlog_errno(status);
921 goto bail;
924 bail:
925 return status;
928 /* map_size should be in bytes. */
929 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
930 unsigned long *node_map,
931 unsigned int map_size)
933 int status, tmpstat;
934 unsigned int node;
936 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
937 sizeof(unsigned long))) {
938 mlog(ML_ERROR,
939 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
940 map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
941 return -EINVAL;
944 status = 0;
945 node = -1;
946 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
947 node + 1)) < O2NM_MAX_NODES) {
948 if (node == dlm->node_num)
949 continue;
951 tmpstat = dlm_send_one_join_cancel(dlm, node);
952 if (tmpstat) {
953 mlog(ML_ERROR, "Error return %d cancelling join on "
954 "node %d\n", tmpstat, node);
955 if (!status)
956 status = tmpstat;
960 if (status)
961 mlog_errno(status);
962 return status;
965 static int dlm_request_join(struct dlm_ctxt *dlm,
966 int node,
967 enum dlm_query_join_response_code *response)
969 int status;
970 struct dlm_query_join_request join_msg;
971 union dlm_query_join_response join_resp;
973 mlog(0, "querying node %d\n", node);
975 memset(&join_msg, 0, sizeof(join_msg));
976 join_msg.node_idx = dlm->node_num;
977 join_msg.name_len = strlen(dlm->name);
978 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
979 join_msg.dlm_proto = dlm->dlm_locking_proto;
980 join_msg.fs_proto = dlm->fs_locking_proto;
982 /* copy live node map to join message */
983 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
985 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
986 sizeof(join_msg), node,
987 &join_resp.intval);
988 if (status < 0 && status != -ENOPROTOOPT) {
989 mlog_errno(status);
990 goto bail;
993 /* -ENOPROTOOPT from the net code means the other side isn't
994 listening for our message type -- that's fine, it means
995 his dlm isn't up, so we can consider him a 'yes' but not
996 joined into the domain. */
997 if (status == -ENOPROTOOPT) {
998 status = 0;
999 *response = JOIN_OK_NO_MAP;
1000 } else if (join_resp.packet.code == JOIN_DISALLOW ||
1001 join_resp.packet.code == JOIN_OK_NO_MAP) {
1002 *response = join_resp.packet.code;
1003 } else if (join_resp.packet.code == JOIN_PROTOCOL_MISMATCH) {
1004 mlog(ML_NOTICE,
1005 "This node requested DLM locking protocol %u.%u and "
1006 "filesystem locking protocol %u.%u. At least one of "
1007 "the protocol versions on node %d is not compatible, "
1008 "disconnecting\n",
1009 dlm->dlm_locking_proto.pv_major,
1010 dlm->dlm_locking_proto.pv_minor,
1011 dlm->fs_locking_proto.pv_major,
1012 dlm->fs_locking_proto.pv_minor,
1013 node);
1014 status = -EPROTO;
1015 *response = join_resp.packet.code;
1016 } else if (join_resp.packet.code == JOIN_OK) {
1017 *response = join_resp.packet.code;
1018 /* Use the same locking protocol as the remote node */
1019 dlm->dlm_locking_proto.pv_minor =
1020 join_resp.packet.dlm_minor;
1021 dlm->fs_locking_proto.pv_minor =
1022 join_resp.packet.fs_minor;
1023 mlog(0,
1024 "Node %d responds JOIN_OK with DLM locking protocol "
1025 "%u.%u and fs locking protocol %u.%u\n",
1026 node,
1027 dlm->dlm_locking_proto.pv_major,
1028 dlm->dlm_locking_proto.pv_minor,
1029 dlm->fs_locking_proto.pv_major,
1030 dlm->fs_locking_proto.pv_minor);
1031 } else {
1032 status = -EINVAL;
1033 mlog(ML_ERROR, "invalid response %d from node %u\n",
1034 join_resp.packet.code, node);
1037 mlog(0, "status %d, node %d response is %d\n", status, node,
1038 *response);
1040 bail:
1041 return status;
1044 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1045 unsigned int node)
1047 int status;
1048 struct dlm_assert_joined assert_msg;
1050 mlog(0, "Sending join assert to node %u\n", node);
1052 memset(&assert_msg, 0, sizeof(assert_msg));
1053 assert_msg.node_idx = dlm->node_num;
1054 assert_msg.name_len = strlen(dlm->name);
1055 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1057 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1058 &assert_msg, sizeof(assert_msg), node,
1059 NULL);
1060 if (status < 0)
1061 mlog_errno(status);
1063 return status;
1066 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1067 unsigned long *node_map)
1069 int status, node, live;
1071 status = 0;
1072 node = -1;
1073 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1074 node + 1)) < O2NM_MAX_NODES) {
1075 if (node == dlm->node_num)
1076 continue;
1078 do {
1079 /* It is very important that this message be
1080 * received so we spin until either the node
1081 * has died or it gets the message. */
1082 status = dlm_send_one_join_assert(dlm, node);
1084 spin_lock(&dlm->spinlock);
1085 live = test_bit(node, dlm->live_nodes_map);
1086 spin_unlock(&dlm->spinlock);
1088 if (status) {
1089 mlog(ML_ERROR, "Error return %d asserting "
1090 "join on node %d\n", status, node);
1092 /* give us some time between errors... */
1093 if (live)
1094 msleep(DLM_DOMAIN_BACKOFF_MS);
1096 } while (status && live);
1100 struct domain_join_ctxt {
1101 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1102 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1105 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1106 struct domain_join_ctxt *ctxt,
1107 enum dlm_query_join_response_code response)
1109 int ret;
1111 if (response == JOIN_DISALLOW) {
1112 mlog(0, "Latest response of disallow -- should restart\n");
1113 return 1;
1116 spin_lock(&dlm->spinlock);
1117 /* For now, we restart the process if the node maps have
1118 * changed at all */
1119 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1120 sizeof(dlm->live_nodes_map));
1121 spin_unlock(&dlm->spinlock);
1123 if (ret)
1124 mlog(0, "Node maps changed -- should restart\n");
1126 return ret;
1129 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1131 int status = 0, tmpstat, node;
1132 struct domain_join_ctxt *ctxt;
1133 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1135 mlog_entry("%p", dlm);
1137 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1138 if (!ctxt) {
1139 status = -ENOMEM;
1140 mlog_errno(status);
1141 goto bail;
1144 /* group sem locking should work for us here -- we're already
1145 * registered for heartbeat events so filling this should be
1146 * atomic wrt getting those handlers called. */
1147 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1149 spin_lock(&dlm->spinlock);
1150 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1152 __dlm_set_joining_node(dlm, dlm->node_num);
1154 spin_unlock(&dlm->spinlock);
1156 node = -1;
1157 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1158 node + 1)) < O2NM_MAX_NODES) {
1159 if (node == dlm->node_num)
1160 continue;
1162 status = dlm_request_join(dlm, node, &response);
1163 if (status < 0) {
1164 mlog_errno(status);
1165 goto bail;
1168 /* Ok, either we got a response or the node doesn't have a
1169 * dlm up. */
1170 if (response == JOIN_OK)
1171 set_bit(node, ctxt->yes_resp_map);
1173 if (dlm_should_restart_join(dlm, ctxt, response)) {
1174 status = -EAGAIN;
1175 goto bail;
1179 mlog(0, "Yay, done querying nodes!\n");
1181 /* Yay, everyone agree's we can join the domain. My domain is
1182 * comprised of all nodes who were put in the
1183 * yes_resp_map. Copy that into our domain map and send a join
1184 * assert message to clean up everyone elses state. */
1185 spin_lock(&dlm->spinlock);
1186 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1187 sizeof(ctxt->yes_resp_map));
1188 set_bit(dlm->node_num, dlm->domain_map);
1189 spin_unlock(&dlm->spinlock);
1191 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1193 /* Joined state *must* be set before the joining node
1194 * information, otherwise the query_join handler may read no
1195 * current joiner but a state of NEW and tell joining nodes
1196 * we're not in the domain. */
1197 spin_lock(&dlm_domain_lock);
1198 dlm->dlm_state = DLM_CTXT_JOINED;
1199 dlm->num_joins++;
1200 spin_unlock(&dlm_domain_lock);
1202 bail:
1203 spin_lock(&dlm->spinlock);
1204 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1205 if (!status)
1206 __dlm_print_nodes(dlm);
1207 spin_unlock(&dlm->spinlock);
1209 if (ctxt) {
1210 /* Do we need to send a cancel message to any nodes? */
1211 if (status < 0) {
1212 tmpstat = dlm_send_join_cancels(dlm,
1213 ctxt->yes_resp_map,
1214 sizeof(ctxt->yes_resp_map));
1215 if (tmpstat < 0)
1216 mlog_errno(tmpstat);
1218 kfree(ctxt);
1221 mlog(0, "returning %d\n", status);
1222 return status;
1225 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1227 o2hb_unregister_callback(NULL, &dlm->dlm_hb_up);
1228 o2hb_unregister_callback(NULL, &dlm->dlm_hb_down);
1229 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1232 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1234 int status;
1236 mlog(0, "registering handlers.\n");
1238 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1239 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1240 status = o2hb_register_callback(NULL, &dlm->dlm_hb_down);
1241 if (status)
1242 goto bail;
1244 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1245 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1246 status = o2hb_register_callback(NULL, &dlm->dlm_hb_up);
1247 if (status)
1248 goto bail;
1250 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1251 sizeof(struct dlm_master_request),
1252 dlm_master_request_handler,
1253 dlm, NULL, &dlm->dlm_domain_handlers);
1254 if (status)
1255 goto bail;
1257 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1258 sizeof(struct dlm_assert_master),
1259 dlm_assert_master_handler,
1260 dlm, dlm_assert_master_post_handler,
1261 &dlm->dlm_domain_handlers);
1262 if (status)
1263 goto bail;
1265 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1266 sizeof(struct dlm_create_lock),
1267 dlm_create_lock_handler,
1268 dlm, NULL, &dlm->dlm_domain_handlers);
1269 if (status)
1270 goto bail;
1272 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1273 DLM_CONVERT_LOCK_MAX_LEN,
1274 dlm_convert_lock_handler,
1275 dlm, NULL, &dlm->dlm_domain_handlers);
1276 if (status)
1277 goto bail;
1279 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1280 DLM_UNLOCK_LOCK_MAX_LEN,
1281 dlm_unlock_lock_handler,
1282 dlm, NULL, &dlm->dlm_domain_handlers);
1283 if (status)
1284 goto bail;
1286 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1287 DLM_PROXY_AST_MAX_LEN,
1288 dlm_proxy_ast_handler,
1289 dlm, NULL, &dlm->dlm_domain_handlers);
1290 if (status)
1291 goto bail;
1293 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1294 sizeof(struct dlm_exit_domain),
1295 dlm_exit_domain_handler,
1296 dlm, NULL, &dlm->dlm_domain_handlers);
1297 if (status)
1298 goto bail;
1300 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1301 sizeof(struct dlm_deref_lockres),
1302 dlm_deref_lockres_handler,
1303 dlm, NULL, &dlm->dlm_domain_handlers);
1304 if (status)
1305 goto bail;
1307 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1308 sizeof(struct dlm_migrate_request),
1309 dlm_migrate_request_handler,
1310 dlm, NULL, &dlm->dlm_domain_handlers);
1311 if (status)
1312 goto bail;
1314 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1315 DLM_MIG_LOCKRES_MAX_LEN,
1316 dlm_mig_lockres_handler,
1317 dlm, NULL, &dlm->dlm_domain_handlers);
1318 if (status)
1319 goto bail;
1321 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1322 sizeof(struct dlm_master_requery),
1323 dlm_master_requery_handler,
1324 dlm, NULL, &dlm->dlm_domain_handlers);
1325 if (status)
1326 goto bail;
1328 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1329 sizeof(struct dlm_lock_request),
1330 dlm_request_all_locks_handler,
1331 dlm, NULL, &dlm->dlm_domain_handlers);
1332 if (status)
1333 goto bail;
1335 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1336 sizeof(struct dlm_reco_data_done),
1337 dlm_reco_data_done_handler,
1338 dlm, NULL, &dlm->dlm_domain_handlers);
1339 if (status)
1340 goto bail;
1342 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1343 sizeof(struct dlm_begin_reco),
1344 dlm_begin_reco_handler,
1345 dlm, NULL, &dlm->dlm_domain_handlers);
1346 if (status)
1347 goto bail;
1349 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1350 sizeof(struct dlm_finalize_reco),
1351 dlm_finalize_reco_handler,
1352 dlm, NULL, &dlm->dlm_domain_handlers);
1353 if (status)
1354 goto bail;
1356 bail:
1357 if (status)
1358 dlm_unregister_domain_handlers(dlm);
1360 return status;
1363 static int dlm_join_domain(struct dlm_ctxt *dlm)
1365 int status;
1366 unsigned int backoff;
1367 unsigned int total_backoff = 0;
1369 BUG_ON(!dlm);
1371 mlog(0, "Join domain %s\n", dlm->name);
1373 status = dlm_register_domain_handlers(dlm);
1374 if (status) {
1375 mlog_errno(status);
1376 goto bail;
1379 status = dlm_launch_thread(dlm);
1380 if (status < 0) {
1381 mlog_errno(status);
1382 goto bail;
1385 status = dlm_launch_recovery_thread(dlm);
1386 if (status < 0) {
1387 mlog_errno(status);
1388 goto bail;
1391 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1392 if (!dlm->dlm_worker) {
1393 status = -ENOMEM;
1394 mlog_errno(status);
1395 goto bail;
1398 do {
1399 status = dlm_try_to_join_domain(dlm);
1401 /* If we're racing another node to the join, then we
1402 * need to back off temporarily and let them
1403 * complete. */
1404 #define DLM_JOIN_TIMEOUT_MSECS 90000
1405 if (status == -EAGAIN) {
1406 if (signal_pending(current)) {
1407 status = -ERESTARTSYS;
1408 goto bail;
1411 if (total_backoff >
1412 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1413 status = -ERESTARTSYS;
1414 mlog(ML_NOTICE, "Timed out joining dlm domain "
1415 "%s after %u msecs\n", dlm->name,
1416 jiffies_to_msecs(total_backoff));
1417 goto bail;
1421 * <chip> After you!
1422 * <dale> No, after you!
1423 * <chip> I insist!
1424 * <dale> But you first!
1425 * ...
1427 backoff = (unsigned int)(jiffies & 0x3);
1428 backoff *= DLM_DOMAIN_BACKOFF_MS;
1429 total_backoff += backoff;
1430 mlog(0, "backoff %d\n", backoff);
1431 msleep(backoff);
1433 } while (status == -EAGAIN);
1435 if (status < 0) {
1436 mlog_errno(status);
1437 goto bail;
1440 status = 0;
1441 bail:
1442 wake_up(&dlm_domain_events);
1444 if (status) {
1445 dlm_unregister_domain_handlers(dlm);
1446 dlm_complete_thread(dlm);
1447 dlm_complete_recovery_thread(dlm);
1448 dlm_destroy_dlm_worker(dlm);
1451 return status;
1454 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1455 u32 key)
1457 int i;
1458 struct dlm_ctxt *dlm = NULL;
1460 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1461 if (!dlm) {
1462 mlog_errno(-ENOMEM);
1463 goto leave;
1466 dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1467 if (dlm->name == NULL) {
1468 mlog_errno(-ENOMEM);
1469 kfree(dlm);
1470 dlm = NULL;
1471 goto leave;
1474 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1475 if (!dlm->lockres_hash) {
1476 mlog_errno(-ENOMEM);
1477 kfree(dlm->name);
1478 kfree(dlm);
1479 dlm = NULL;
1480 goto leave;
1483 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1484 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1486 strcpy(dlm->name, domain);
1487 dlm->key = key;
1488 dlm->node_num = o2nm_this_node();
1490 spin_lock_init(&dlm->spinlock);
1491 spin_lock_init(&dlm->master_lock);
1492 spin_lock_init(&dlm->ast_lock);
1493 INIT_LIST_HEAD(&dlm->list);
1494 INIT_LIST_HEAD(&dlm->dirty_list);
1495 INIT_LIST_HEAD(&dlm->reco.resources);
1496 INIT_LIST_HEAD(&dlm->reco.received);
1497 INIT_LIST_HEAD(&dlm->reco.node_data);
1498 INIT_LIST_HEAD(&dlm->purge_list);
1499 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1500 dlm->reco.state = 0;
1502 INIT_LIST_HEAD(&dlm->pending_asts);
1503 INIT_LIST_HEAD(&dlm->pending_basts);
1505 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1506 dlm->recovery_map, &(dlm->recovery_map[0]));
1508 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1509 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1510 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1512 dlm->dlm_thread_task = NULL;
1513 dlm->dlm_reco_thread_task = NULL;
1514 dlm->dlm_worker = NULL;
1515 init_waitqueue_head(&dlm->dlm_thread_wq);
1516 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1517 init_waitqueue_head(&dlm->reco.event);
1518 init_waitqueue_head(&dlm->ast_wq);
1519 init_waitqueue_head(&dlm->migration_wq);
1520 INIT_LIST_HEAD(&dlm->master_list);
1521 INIT_LIST_HEAD(&dlm->mle_hb_events);
1523 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1524 init_waitqueue_head(&dlm->dlm_join_events);
1526 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1527 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1528 atomic_set(&dlm->local_resources, 0);
1529 atomic_set(&dlm->remote_resources, 0);
1530 atomic_set(&dlm->unknown_resources, 0);
1532 spin_lock_init(&dlm->work_lock);
1533 INIT_LIST_HEAD(&dlm->work_list);
1534 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
1536 kref_init(&dlm->dlm_refs);
1537 dlm->dlm_state = DLM_CTXT_NEW;
1539 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1541 mlog(0, "context init: refcount %u\n",
1542 atomic_read(&dlm->dlm_refs.refcount));
1544 leave:
1545 return dlm;
1549 * Compare a requested locking protocol version against the current one.
1551 * If the major numbers are different, they are incompatible.
1552 * If the current minor is greater than the request, they are incompatible.
1553 * If the current minor is less than or equal to the request, they are
1554 * compatible, and the requester should run at the current minor version.
1556 static int dlm_protocol_compare(struct dlm_protocol_version *existing,
1557 struct dlm_protocol_version *request)
1559 if (existing->pv_major != request->pv_major)
1560 return 1;
1562 if (existing->pv_minor > request->pv_minor)
1563 return 1;
1565 if (existing->pv_minor < request->pv_minor)
1566 request->pv_minor = existing->pv_minor;
1568 return 0;
1572 * dlm_register_domain: one-time setup per "domain".
1574 * The filesystem passes in the requested locking version via proto.
1575 * If registration was successful, proto will contain the negotiated
1576 * locking protocol.
1578 struct dlm_ctxt * dlm_register_domain(const char *domain,
1579 u32 key,
1580 struct dlm_protocol_version *fs_proto)
1582 int ret;
1583 struct dlm_ctxt *dlm = NULL;
1584 struct dlm_ctxt *new_ctxt = NULL;
1586 if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1587 ret = -ENAMETOOLONG;
1588 mlog(ML_ERROR, "domain name length too long\n");
1589 goto leave;
1592 if (!o2hb_check_local_node_heartbeating()) {
1593 mlog(ML_ERROR, "the local node has not been configured, or is "
1594 "not heartbeating\n");
1595 ret = -EPROTO;
1596 goto leave;
1599 mlog(0, "register called for domain \"%s\"\n", domain);
1601 retry:
1602 dlm = NULL;
1603 if (signal_pending(current)) {
1604 ret = -ERESTARTSYS;
1605 mlog_errno(ret);
1606 goto leave;
1609 spin_lock(&dlm_domain_lock);
1611 dlm = __dlm_lookup_domain(domain);
1612 if (dlm) {
1613 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1614 spin_unlock(&dlm_domain_lock);
1616 mlog(0, "This ctxt is not joined yet!\n");
1617 wait_event_interruptible(dlm_domain_events,
1618 dlm_wait_on_domain_helper(
1619 domain));
1620 goto retry;
1623 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
1624 mlog(ML_ERROR,
1625 "Requested locking protocol version is not "
1626 "compatible with already registered domain "
1627 "\"%s\"\n", domain);
1628 ret = -EPROTO;
1629 goto leave;
1632 __dlm_get(dlm);
1633 dlm->num_joins++;
1635 spin_unlock(&dlm_domain_lock);
1637 ret = 0;
1638 goto leave;
1641 /* doesn't exist */
1642 if (!new_ctxt) {
1643 spin_unlock(&dlm_domain_lock);
1645 new_ctxt = dlm_alloc_ctxt(domain, key);
1646 if (new_ctxt)
1647 goto retry;
1649 ret = -ENOMEM;
1650 mlog_errno(ret);
1651 goto leave;
1654 /* a little variable switch-a-roo here... */
1655 dlm = new_ctxt;
1656 new_ctxt = NULL;
1658 /* add the new domain */
1659 list_add_tail(&dlm->list, &dlm_domains);
1660 spin_unlock(&dlm_domain_lock);
1663 * Pass the locking protocol version into the join. If the join
1664 * succeeds, it will have the negotiated protocol set.
1666 dlm->dlm_locking_proto = dlm_protocol;
1667 dlm->fs_locking_proto = *fs_proto;
1669 ret = dlm_join_domain(dlm);
1670 if (ret) {
1671 mlog_errno(ret);
1672 dlm_put(dlm);
1673 goto leave;
1676 /* Tell the caller what locking protocol we negotiated */
1677 *fs_proto = dlm->fs_locking_proto;
1679 ret = 0;
1680 leave:
1681 if (new_ctxt)
1682 dlm_free_ctxt_mem(new_ctxt);
1684 if (ret < 0)
1685 dlm = ERR_PTR(ret);
1687 return dlm;
1689 EXPORT_SYMBOL_GPL(dlm_register_domain);
1691 static LIST_HEAD(dlm_join_handlers);
1693 static void dlm_unregister_net_handlers(void)
1695 o2net_unregister_handler_list(&dlm_join_handlers);
1698 static int dlm_register_net_handlers(void)
1700 int status = 0;
1702 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1703 sizeof(struct dlm_query_join_request),
1704 dlm_query_join_handler,
1705 NULL, NULL, &dlm_join_handlers);
1706 if (status)
1707 goto bail;
1709 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1710 sizeof(struct dlm_assert_joined),
1711 dlm_assert_joined_handler,
1712 NULL, NULL, &dlm_join_handlers);
1713 if (status)
1714 goto bail;
1716 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1717 sizeof(struct dlm_cancel_join),
1718 dlm_cancel_join_handler,
1719 NULL, NULL, &dlm_join_handlers);
1721 bail:
1722 if (status < 0)
1723 dlm_unregister_net_handlers();
1725 return status;
1728 /* Domain eviction callback handling.
1730 * The file system requires notification of node death *before* the
1731 * dlm completes it's recovery work, otherwise it may be able to
1732 * acquire locks on resources requiring recovery. Since the dlm can
1733 * evict a node from it's domain *before* heartbeat fires, a similar
1734 * mechanism is required. */
1736 /* Eviction is not expected to happen often, so a per-domain lock is
1737 * not necessary. Eviction callbacks are allowed to sleep for short
1738 * periods of time. */
1739 static DECLARE_RWSEM(dlm_callback_sem);
1741 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1742 int node_num)
1744 struct list_head *iter;
1745 struct dlm_eviction_cb *cb;
1747 down_read(&dlm_callback_sem);
1748 list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1749 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1751 cb->ec_func(node_num, cb->ec_data);
1753 up_read(&dlm_callback_sem);
1756 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1757 dlm_eviction_func *f,
1758 void *data)
1760 INIT_LIST_HEAD(&cb->ec_item);
1761 cb->ec_func = f;
1762 cb->ec_data = data;
1764 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1766 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1767 struct dlm_eviction_cb *cb)
1769 down_write(&dlm_callback_sem);
1770 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1771 up_write(&dlm_callback_sem);
1773 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1775 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1777 down_write(&dlm_callback_sem);
1778 list_del_init(&cb->ec_item);
1779 up_write(&dlm_callback_sem);
1781 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1783 static int __init dlm_init(void)
1785 int status;
1787 dlm_print_version();
1789 status = dlm_init_mle_cache();
1790 if (status)
1791 return -1;
1793 status = dlm_register_net_handlers();
1794 if (status) {
1795 dlm_destroy_mle_cache();
1796 return -1;
1799 return 0;
1802 static void __exit dlm_exit (void)
1804 dlm_unregister_net_handlers();
1805 dlm_destroy_mle_cache();
1808 MODULE_AUTHOR("Oracle");
1809 MODULE_LICENSE("GPL");
1811 module_init(dlm_init);
1812 module_exit(dlm_exit);