blob: 6099a8034b1747cd8cc237d512a905c251384b2f [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmdomain.c
5 *
6 * defines domain join / leave apis
7 *
8 * Copyright (C) 2004 Oracle. All rights reserved.
9 *
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.
14 *
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.
19 *
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.
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31#include <linux/init.h>
32#include <linux/spinlock.h>
33#include <linux/delay.h>
34#include <linux/err.h>
35#include <linux/debugfs.h>
36#include <linux/sched/signal.h>
37
38#include "cluster/heartbeat.h"
39#include "cluster/nodemanager.h"
40#include "cluster/tcp.h"
41
42#include "dlmapi.h"
43#include "dlmcommon.h"
44#include "dlmdomain.h"
45#include "dlmdebug.h"
46
47#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
48#include "cluster/masklog.h"
49
50/*
51 * ocfs2 node maps are array of long int, which limits to send them freely
52 * across the wire due to endianness issues. To workaround this, we convert
53 * long ints to byte arrays. Following 3 routines are helper functions to
54 * set/test/copy bits within those array of bytes
55 */
56static inline void byte_set_bit(u8 nr, u8 map[])
57{
58 map[nr >> 3] |= (1UL << (nr & 7));
59}
60
61static inline int byte_test_bit(u8 nr, u8 map[])
62{
63 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
64}
65
66static inline void byte_copymap(u8 dmap[], unsigned long smap[],
67 unsigned int sz)
68{
69 unsigned int nn;
70
71 if (!sz)
72 return;
73
74 memset(dmap, 0, ((sz + 7) >> 3));
75 for (nn = 0 ; nn < sz; nn++)
76 if (test_bit(nn, smap))
77 byte_set_bit(nn, dmap);
78}
79
80static void dlm_free_pagevec(void **vec, int pages)
81{
82 while (pages--)
83 free_page((unsigned long)vec[pages]);
84 kfree(vec);
85}
86
87static void **dlm_alloc_pagevec(int pages)
88{
89 void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
90 int i;
91
92 if (!vec)
93 return NULL;
94
95 for (i = 0; i < pages; i++)
96 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
97 goto out_free;
98
99 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
100 pages, (unsigned long)DLM_HASH_PAGES,
101 (unsigned long)DLM_BUCKETS_PER_PAGE);
102 return vec;
103out_free:
104 dlm_free_pagevec(vec, i);
105 return NULL;
106}
107
108/*
109 *
110 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
111 * dlm_domain_lock
112 * struct dlm_ctxt->spinlock
113 * struct dlm_lock_resource->spinlock
114 * struct dlm_ctxt->master_lock
115 * struct dlm_ctxt->ast_lock
116 * dlm_master_list_entry->spinlock
117 * dlm_lock->spinlock
118 *
119 */
120
121DEFINE_SPINLOCK(dlm_domain_lock);
122LIST_HEAD(dlm_domains);
123static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
124
125/*
126 * The supported protocol version for DLM communication. Running domains
127 * will have a negotiated version with the same major number and a minor
128 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
129 * be used to determine what a running domain is actually using.
130 *
131 * New in version 1.1:
132 * - Message DLM_QUERY_REGION added to support global heartbeat
133 * - Message DLM_QUERY_NODEINFO added to allow online node removes
134 * New in version 1.2:
135 * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
136 * New in version 1.3:
137 * - Message DLM_DEREF_LOCKRES_DONE added to inform non-master that the
138 * refmap is cleared
139 */
140static const struct dlm_protocol_version dlm_protocol = {
141 .pv_major = 1,
142 .pv_minor = 3,
143};
144
145#define DLM_DOMAIN_BACKOFF_MS 200
146
147static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
148 void **ret_data);
149static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
150 void **ret_data);
151static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
152 void **ret_data);
153static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
154 void *data, void **ret_data);
155static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
156 void **ret_data);
157static int dlm_protocol_compare(struct dlm_protocol_version *existing,
158 struct dlm_protocol_version *request);
159
160static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
161
162void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
163{
164 if (hlist_unhashed(&res->hash_node))
165 return;
166
167 mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
168 res->lockname.name);
169 hlist_del_init(&res->hash_node);
170 dlm_lockres_put(res);
171}
172
173void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
174{
175 struct hlist_head *bucket;
176
177 assert_spin_locked(&dlm->spinlock);
178
179 bucket = dlm_lockres_hash(dlm, res->lockname.hash);
180
181 /* get a reference for our hashtable */
182 dlm_lockres_get(res);
183
184 hlist_add_head(&res->hash_node, bucket);
185
186 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
187 res->lockname.name);
188}
189
190struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
191 const char *name,
192 unsigned int len,
193 unsigned int hash)
194{
195 struct hlist_head *bucket;
196 struct dlm_lock_resource *res;
197
198 mlog(0, "%.*s\n", len, name);
199
200 assert_spin_locked(&dlm->spinlock);
201
202 bucket = dlm_lockres_hash(dlm, hash);
203
204 hlist_for_each_entry(res, bucket, hash_node) {
205 if (res->lockname.name[0] != name[0])
206 continue;
207 if (unlikely(res->lockname.len != len))
208 continue;
209 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
210 continue;
211 dlm_lockres_get(res);
212 return res;
213 }
214 return NULL;
215}
216
217/* intended to be called by functions which do not care about lock
218 * resources which are being purged (most net _handler functions).
219 * this will return NULL for any lock resource which is found but
220 * currently in the process of dropping its mastery reference.
221 * use __dlm_lookup_lockres_full when you need the lock resource
222 * regardless (e.g. dlm_get_lock_resource) */
223struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
224 const char *name,
225 unsigned int len,
226 unsigned int hash)
227{
228 struct dlm_lock_resource *res = NULL;
229
230 mlog(0, "%.*s\n", len, name);
231
232 assert_spin_locked(&dlm->spinlock);
233
234 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
235 if (res) {
236 spin_lock(&res->spinlock);
237 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
238 spin_unlock(&res->spinlock);
239 dlm_lockres_put(res);
240 return NULL;
241 }
242 spin_unlock(&res->spinlock);
243 }
244
245 return res;
246}
247
248struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
249 const char *name,
250 unsigned int len)
251{
252 struct dlm_lock_resource *res;
253 unsigned int hash = dlm_lockid_hash(name, len);
254
255 spin_lock(&dlm->spinlock);
256 res = __dlm_lookup_lockres(dlm, name, len, hash);
257 spin_unlock(&dlm->spinlock);
258 return res;
259}
260
261static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
262{
263 struct dlm_ctxt *tmp;
264
265 assert_spin_locked(&dlm_domain_lock);
266
267 /* tmp->name here is always NULL terminated,
268 * but domain may not be! */
269 list_for_each_entry(tmp, &dlm_domains, list) {
270 if (strlen(tmp->name) == len &&
271 memcmp(tmp->name, domain, len)==0)
272 return tmp;
273 }
274
275 return NULL;
276}
277
278/* For null terminated domain strings ONLY */
279static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
280{
281 assert_spin_locked(&dlm_domain_lock);
282
283 return __dlm_lookup_domain_full(domain, strlen(domain));
284}
285
286
287/* returns true on one of two conditions:
288 * 1) the domain does not exist
289 * 2) the domain exists and it's state is "joined" */
290static int dlm_wait_on_domain_helper(const char *domain)
291{
292 int ret = 0;
293 struct dlm_ctxt *tmp = NULL;
294
295 spin_lock(&dlm_domain_lock);
296
297 tmp = __dlm_lookup_domain(domain);
298 if (!tmp)
299 ret = 1;
300 else if (tmp->dlm_state == DLM_CTXT_JOINED)
301 ret = 1;
302
303 spin_unlock(&dlm_domain_lock);
304 return ret;
305}
306
307static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
308{
309 dlm_destroy_debugfs_subroot(dlm);
310
311 if (dlm->lockres_hash)
312 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
313
314 if (dlm->master_hash)
315 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
316
317 kfree(dlm->name);
318 kfree(dlm);
319}
320
321/* A little strange - this function will be called while holding
322 * dlm_domain_lock and is expected to be holding it on the way out. We
323 * will however drop and reacquire it multiple times */
324static void dlm_ctxt_release(struct kref *kref)
325{
326 struct dlm_ctxt *dlm;
327
328 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
329
330 BUG_ON(dlm->num_joins);
331 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
332
333 /* we may still be in the list if we hit an error during join. */
334 list_del_init(&dlm->list);
335
336 spin_unlock(&dlm_domain_lock);
337
338 mlog(0, "freeing memory from domain %s\n", dlm->name);
339
340 wake_up(&dlm_domain_events);
341
342 dlm_free_ctxt_mem(dlm);
343
344 spin_lock(&dlm_domain_lock);
345}
346
347void dlm_put(struct dlm_ctxt *dlm)
348{
349 spin_lock(&dlm_domain_lock);
350 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
351 spin_unlock(&dlm_domain_lock);
352}
353
354static void __dlm_get(struct dlm_ctxt *dlm)
355{
356 kref_get(&dlm->dlm_refs);
357}
358
359/* given a questionable reference to a dlm object, gets a reference if
360 * it can find it in the list, otherwise returns NULL in which case
361 * you shouldn't trust your pointer. */
362struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
363{
364 struct dlm_ctxt *target;
365 struct dlm_ctxt *ret = NULL;
366
367 spin_lock(&dlm_domain_lock);
368
369 list_for_each_entry(target, &dlm_domains, list) {
370 if (target == dlm) {
371 __dlm_get(target);
372 ret = target;
373 break;
374 }
375 }
376
377 spin_unlock(&dlm_domain_lock);
378
379 return ret;
380}
381
382int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
383{
384 int ret;
385
386 spin_lock(&dlm_domain_lock);
387 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
388 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
389 spin_unlock(&dlm_domain_lock);
390
391 return ret;
392}
393
394static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
395{
396 if (dlm->dlm_worker) {
397 flush_workqueue(dlm->dlm_worker);
398 destroy_workqueue(dlm->dlm_worker);
399 dlm->dlm_worker = NULL;
400 }
401}
402
403static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
404{
405 dlm_unregister_domain_handlers(dlm);
406 dlm_debug_shutdown(dlm);
407 dlm_complete_thread(dlm);
408 dlm_complete_recovery_thread(dlm);
409 dlm_destroy_dlm_worker(dlm);
410
411 /* We've left the domain. Now we can take ourselves out of the
412 * list and allow the kref stuff to help us free the
413 * memory. */
414 spin_lock(&dlm_domain_lock);
415 list_del_init(&dlm->list);
416 spin_unlock(&dlm_domain_lock);
417
418 /* Wake up anyone waiting for us to remove this domain */
419 wake_up(&dlm_domain_events);
420}
421
422static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
423{
424 int i, num, n, ret = 0;
425 struct dlm_lock_resource *res;
426 struct hlist_node *iter;
427 struct hlist_head *bucket;
428 int dropped;
429
430 mlog(0, "Migrating locks from domain %s\n", dlm->name);
431
432 num = 0;
433 spin_lock(&dlm->spinlock);
434 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
435redo_bucket:
436 n = 0;
437 bucket = dlm_lockres_hash(dlm, i);
438 iter = bucket->first;
439 while (iter) {
440 n++;
441 res = hlist_entry(iter, struct dlm_lock_resource,
442 hash_node);
443 dlm_lockres_get(res);
444 /* migrate, if necessary. this will drop the dlm
445 * spinlock and retake it if it does migration. */
446 dropped = dlm_empty_lockres(dlm, res);
447
448 spin_lock(&res->spinlock);
449 if (dropped)
450 __dlm_lockres_calc_usage(dlm, res);
451 else
452 iter = res->hash_node.next;
453 spin_unlock(&res->spinlock);
454
455 dlm_lockres_put(res);
456
457 if (dropped) {
458 cond_resched_lock(&dlm->spinlock);
459 goto redo_bucket;
460 }
461 }
462 cond_resched_lock(&dlm->spinlock);
463 num += n;
464 }
465 spin_unlock(&dlm->spinlock);
466 wake_up(&dlm->dlm_thread_wq);
467
468 /* let the dlm thread take care of purging, keep scanning until
469 * nothing remains in the hash */
470 if (num) {
471 mlog(0, "%s: %d lock resources in hash last pass\n",
472 dlm->name, num);
473 ret = -EAGAIN;
474 }
475 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
476 return ret;
477}
478
479static int dlm_no_joining_node(struct dlm_ctxt *dlm)
480{
481 int ret;
482
483 spin_lock(&dlm->spinlock);
484 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
485 spin_unlock(&dlm->spinlock);
486
487 return ret;
488}
489
490static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
491 void *data, void **ret_data)
492{
493 struct dlm_ctxt *dlm = data;
494 unsigned int node;
495 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
496
497 if (!dlm_grab(dlm))
498 return 0;
499
500 node = exit_msg->node_idx;
501 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
502
503 spin_lock(&dlm->spinlock);
504 set_bit(node, dlm->exit_domain_map);
505 spin_unlock(&dlm->spinlock);
506
507 dlm_put(dlm);
508
509 return 0;
510}
511
512static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
513{
514 /* Yikes, a double spinlock! I need domain_lock for the dlm
515 * state and the dlm spinlock for join state... Sorry! */
516again:
517 spin_lock(&dlm_domain_lock);
518 spin_lock(&dlm->spinlock);
519
520 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
521 mlog(0, "Node %d is joining, we wait on it.\n",
522 dlm->joining_node);
523 spin_unlock(&dlm->spinlock);
524 spin_unlock(&dlm_domain_lock);
525
526 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
527 goto again;
528 }
529
530 dlm->dlm_state = DLM_CTXT_LEAVING;
531 spin_unlock(&dlm->spinlock);
532 spin_unlock(&dlm_domain_lock);
533}
534
535static void __dlm_print_nodes(struct dlm_ctxt *dlm)
536{
537 int node = -1, num = 0;
538
539 assert_spin_locked(&dlm->spinlock);
540
541 printk("( ");
542 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
543 node + 1)) < O2NM_MAX_NODES) {
544 printk("%d ", node);
545 ++num;
546 }
547 printk(") %u nodes\n", num);
548}
549
550static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
551 void **ret_data)
552{
553 struct dlm_ctxt *dlm = data;
554 unsigned int node;
555 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
556
557 mlog(0, "%p %u %p", msg, len, data);
558
559 if (!dlm_grab(dlm))
560 return 0;
561
562 node = exit_msg->node_idx;
563
564 spin_lock(&dlm->spinlock);
565 clear_bit(node, dlm->domain_map);
566 clear_bit(node, dlm->exit_domain_map);
567 printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
568 __dlm_print_nodes(dlm);
569
570 /* notify anything attached to the heartbeat events */
571 dlm_hb_event_notify_attached(dlm, node, 0);
572
573 spin_unlock(&dlm->spinlock);
574
575 dlm_put(dlm);
576
577 return 0;
578}
579
580static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
581 unsigned int node)
582{
583 int status;
584 struct dlm_exit_domain leave_msg;
585
586 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
587 msg_type, node);
588
589 memset(&leave_msg, 0, sizeof(leave_msg));
590 leave_msg.node_idx = dlm->node_num;
591
592 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
593 sizeof(leave_msg), node, NULL);
594 if (status < 0)
595 mlog(ML_ERROR, "Error %d sending domain exit message %u "
596 "to node %u on domain %s\n", status, msg_type, node,
597 dlm->name);
598
599 return status;
600}
601
602static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
603{
604 int node = -1;
605
606 /* Support for begin exit domain was added in 1.2 */
607 if (dlm->dlm_locking_proto.pv_major == 1 &&
608 dlm->dlm_locking_proto.pv_minor < 2)
609 return;
610
611 /*
612 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
613 * informational. Meaning if a node does not receive the message,
614 * so be it.
615 */
616 spin_lock(&dlm->spinlock);
617 while (1) {
618 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
619 if (node >= O2NM_MAX_NODES)
620 break;
621 if (node == dlm->node_num)
622 continue;
623
624 spin_unlock(&dlm->spinlock);
625 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
626 spin_lock(&dlm->spinlock);
627 }
628 spin_unlock(&dlm->spinlock);
629}
630
631static void dlm_leave_domain(struct dlm_ctxt *dlm)
632{
633 int node, clear_node, status;
634
635 /* At this point we've migrated away all our locks and won't
636 * accept mastership of new ones. The dlm is responsible for
637 * almost nothing now. We make sure not to confuse any joining
638 * nodes and then commence shutdown procedure. */
639
640 spin_lock(&dlm->spinlock);
641 /* Clear ourselves from the domain map */
642 clear_bit(dlm->node_num, dlm->domain_map);
643 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
644 0)) < O2NM_MAX_NODES) {
645 /* Drop the dlm spinlock. This is safe wrt the domain_map.
646 * -nodes cannot be added now as the
647 * query_join_handlers knows to respond with OK_NO_MAP
648 * -we catch the right network errors if a node is
649 * removed from the map while we're sending him the
650 * exit message. */
651 spin_unlock(&dlm->spinlock);
652
653 clear_node = 1;
654
655 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
656 node);
657 if (status < 0 &&
658 status != -ENOPROTOOPT &&
659 status != -ENOTCONN) {
660 mlog(ML_NOTICE, "Error %d sending domain exit message "
661 "to node %d\n", status, node);
662
663 /* Not sure what to do here but lets sleep for
664 * a bit in case this was a transient
665 * error... */
666 msleep(DLM_DOMAIN_BACKOFF_MS);
667 clear_node = 0;
668 }
669
670 spin_lock(&dlm->spinlock);
671 /* If we're not clearing the node bit then we intend
672 * to loop back around to try again. */
673 if (clear_node)
674 clear_bit(node, dlm->domain_map);
675 }
676 spin_unlock(&dlm->spinlock);
677}
678
679void dlm_unregister_domain(struct dlm_ctxt *dlm)
680{
681 int leave = 0;
682 struct dlm_lock_resource *res;
683
684 spin_lock(&dlm_domain_lock);
685 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
686 BUG_ON(!dlm->num_joins);
687
688 dlm->num_joins--;
689 if (!dlm->num_joins) {
690 /* We mark it "in shutdown" now so new register
691 * requests wait until we've completely left the
692 * domain. Don't use DLM_CTXT_LEAVING yet as we still
693 * want new domain joins to communicate with us at
694 * least until we've completed migration of our
695 * resources. */
696 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
697 leave = 1;
698 }
699 spin_unlock(&dlm_domain_lock);
700
701 if (leave) {
702 mlog(0, "shutting down domain %s\n", dlm->name);
703 dlm_begin_exit_domain(dlm);
704
705 /* We changed dlm state, notify the thread */
706 dlm_kick_thread(dlm, NULL);
707
708 while (dlm_migrate_all_locks(dlm)) {
709 /* Give dlm_thread time to purge the lockres' */
710 msleep(500);
711 mlog(0, "%s: more migration to do\n", dlm->name);
712 }
713
714 /* This list should be empty. If not, print remaining lockres */
715 if (!list_empty(&dlm->tracking_list)) {
716 mlog(ML_ERROR, "Following lockres' are still on the "
717 "tracking list:\n");
718 list_for_each_entry(res, &dlm->tracking_list, tracking)
719 dlm_print_one_lock_resource(res);
720 }
721
722 dlm_mark_domain_leaving(dlm);
723 dlm_leave_domain(dlm);
724 printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
725 dlm_force_free_mles(dlm);
726 dlm_complete_dlm_shutdown(dlm);
727 }
728 dlm_put(dlm);
729}
730EXPORT_SYMBOL_GPL(dlm_unregister_domain);
731
732static int dlm_query_join_proto_check(char *proto_type, int node,
733 struct dlm_protocol_version *ours,
734 struct dlm_protocol_version *request)
735{
736 int rc;
737 struct dlm_protocol_version proto = *request;
738
739 if (!dlm_protocol_compare(ours, &proto)) {
740 mlog(0,
741 "node %u wanted to join with %s locking protocol "
742 "%u.%u, we respond with %u.%u\n",
743 node, proto_type,
744 request->pv_major,
745 request->pv_minor,
746 proto.pv_major, proto.pv_minor);
747 request->pv_minor = proto.pv_minor;
748 rc = 0;
749 } else {
750 mlog(ML_NOTICE,
751 "Node %u wanted to join with %s locking "
752 "protocol %u.%u, but we have %u.%u, disallowing\n",
753 node, proto_type,
754 request->pv_major,
755 request->pv_minor,
756 ours->pv_major,
757 ours->pv_minor);
758 rc = 1;
759 }
760
761 return rc;
762}
763
764/*
765 * struct dlm_query_join_packet is made up of four one-byte fields. They
766 * are effectively in big-endian order already. However, little-endian
767 * machines swap them before putting the packet on the wire (because
768 * query_join's response is a status, and that status is treated as a u32
769 * on the wire). Thus, a big-endian and little-endian machines will treat
770 * this structure differently.
771 *
772 * The solution is to have little-endian machines swap the structure when
773 * converting from the structure to the u32 representation. This will
774 * result in the structure having the correct format on the wire no matter
775 * the host endian format.
776 */
777static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
778 u32 *wire)
779{
780 union dlm_query_join_response response;
781
782 response.packet = *packet;
783 *wire = be32_to_cpu(response.intval);
784}
785
786static void dlm_query_join_wire_to_packet(u32 wire,
787 struct dlm_query_join_packet *packet)
788{
789 union dlm_query_join_response response;
790
791 response.intval = cpu_to_be32(wire);
792 *packet = response.packet;
793}
794
795static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
796 void **ret_data)
797{
798 struct dlm_query_join_request *query;
799 struct dlm_query_join_packet packet = {
800 .code = JOIN_DISALLOW,
801 };
802 struct dlm_ctxt *dlm = NULL;
803 u32 response;
804 u8 nodenum;
805
806 query = (struct dlm_query_join_request *) msg->buf;
807
808 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
809 query->domain);
810
811 /*
812 * If heartbeat doesn't consider the node live, tell it
813 * to back off and try again. This gives heartbeat a chance
814 * to catch up.
815 */
816 if (!o2hb_check_node_heartbeating_no_sem(query->node_idx)) {
817 mlog(0, "node %u is not in our live map yet\n",
818 query->node_idx);
819
820 packet.code = JOIN_DISALLOW;
821 goto respond;
822 }
823
824 packet.code = JOIN_OK_NO_MAP;
825
826 spin_lock(&dlm_domain_lock);
827 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
828 if (!dlm)
829 goto unlock_respond;
830
831 /*
832 * There is a small window where the joining node may not see the
833 * node(s) that just left but still part of the cluster. DISALLOW
834 * join request if joining node has different node map.
835 */
836 nodenum=0;
837 while (nodenum < O2NM_MAX_NODES) {
838 if (test_bit(nodenum, dlm->domain_map)) {
839 if (!byte_test_bit(nodenum, query->node_map)) {
840 mlog(0, "disallow join as node %u does not "
841 "have node %u in its nodemap\n",
842 query->node_idx, nodenum);
843 packet.code = JOIN_DISALLOW;
844 goto unlock_respond;
845 }
846 }
847 nodenum++;
848 }
849
850 /* Once the dlm ctxt is marked as leaving then we don't want
851 * to be put in someone's domain map.
852 * Also, explicitly disallow joining at certain troublesome
853 * times (ie. during recovery). */
854 if (dlm->dlm_state != DLM_CTXT_LEAVING) {
855 int bit = query->node_idx;
856 spin_lock(&dlm->spinlock);
857
858 if (dlm->dlm_state == DLM_CTXT_NEW &&
859 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
860 /*If this is a brand new context and we
861 * haven't started our join process yet, then
862 * the other node won the race. */
863 packet.code = JOIN_OK_NO_MAP;
864 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
865 /* Disallow parallel joins. */
866 packet.code = JOIN_DISALLOW;
867 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
868 mlog(0, "node %u trying to join, but recovery "
869 "is ongoing.\n", bit);
870 packet.code = JOIN_DISALLOW;
871 } else if (test_bit(bit, dlm->recovery_map)) {
872 mlog(0, "node %u trying to join, but it "
873 "still needs recovery.\n", bit);
874 packet.code = JOIN_DISALLOW;
875 } else if (test_bit(bit, dlm->domain_map)) {
876 mlog(0, "node %u trying to join, but it "
877 "is still in the domain! needs recovery?\n",
878 bit);
879 packet.code = JOIN_DISALLOW;
880 } else {
881 /* Alright we're fully a part of this domain
882 * so we keep some state as to who's joining
883 * and indicate to him that needs to be fixed
884 * up. */
885
886 /* Make sure we speak compatible locking protocols. */
887 if (dlm_query_join_proto_check("DLM", bit,
888 &dlm->dlm_locking_proto,
889 &query->dlm_proto)) {
890 packet.code = JOIN_PROTOCOL_MISMATCH;
891 } else if (dlm_query_join_proto_check("fs", bit,
892 &dlm->fs_locking_proto,
893 &query->fs_proto)) {
894 packet.code = JOIN_PROTOCOL_MISMATCH;
895 } else {
896 packet.dlm_minor = query->dlm_proto.pv_minor;
897 packet.fs_minor = query->fs_proto.pv_minor;
898 packet.code = JOIN_OK;
899 __dlm_set_joining_node(dlm, query->node_idx);
900 }
901 }
902
903 spin_unlock(&dlm->spinlock);
904 }
905unlock_respond:
906 spin_unlock(&dlm_domain_lock);
907
908respond:
909 mlog(0, "We respond with %u\n", packet.code);
910
911 dlm_query_join_packet_to_wire(&packet, &response);
912 return response;
913}
914
915static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
916 void **ret_data)
917{
918 struct dlm_assert_joined *assert;
919 struct dlm_ctxt *dlm = NULL;
920
921 assert = (struct dlm_assert_joined *) msg->buf;
922
923 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
924 assert->domain);
925
926 spin_lock(&dlm_domain_lock);
927 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
928 /* XXX should we consider no dlm ctxt an error? */
929 if (dlm) {
930 spin_lock(&dlm->spinlock);
931
932 /* Alright, this node has officially joined our
933 * domain. Set him in the map and clean up our
934 * leftover join state. */
935 BUG_ON(dlm->joining_node != assert->node_idx);
936
937 if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
938 mlog(0, "dlm recovery is ongoing, disallow join\n");
939 spin_unlock(&dlm->spinlock);
940 spin_unlock(&dlm_domain_lock);
941 return -EAGAIN;
942 }
943
944 set_bit(assert->node_idx, dlm->domain_map);
945 clear_bit(assert->node_idx, dlm->exit_domain_map);
946 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
947
948 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
949 assert->node_idx, dlm->name);
950 __dlm_print_nodes(dlm);
951
952 /* notify anything attached to the heartbeat events */
953 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
954
955 spin_unlock(&dlm->spinlock);
956 }
957 spin_unlock(&dlm_domain_lock);
958
959 return 0;
960}
961
962static int dlm_match_regions(struct dlm_ctxt *dlm,
963 struct dlm_query_region *qr,
964 char *local, int locallen)
965{
966 char *remote = qr->qr_regions;
967 char *l, *r;
968 int localnr, i, j, foundit;
969 int status = 0;
970
971 if (!o2hb_global_heartbeat_active()) {
972 if (qr->qr_numregions) {
973 mlog(ML_ERROR, "Domain %s: Joining node %d has global "
974 "heartbeat enabled but local node %d does not\n",
975 qr->qr_domain, qr->qr_node, dlm->node_num);
976 status = -EINVAL;
977 }
978 goto bail;
979 }
980
981 if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
982 mlog(ML_ERROR, "Domain %s: Local node %d has global "
983 "heartbeat enabled but joining node %d does not\n",
984 qr->qr_domain, dlm->node_num, qr->qr_node);
985 status = -EINVAL;
986 goto bail;
987 }
988
989 r = remote;
990 for (i = 0; i < qr->qr_numregions; ++i) {
991 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
992 r += O2HB_MAX_REGION_NAME_LEN;
993 }
994
995 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
996 localnr = o2hb_get_all_regions(local, (u8)localnr);
997
998 /* compare local regions with remote */
999 l = local;
1000 for (i = 0; i < localnr; ++i) {
1001 foundit = 0;
1002 r = remote;
1003 for (j = 0; j <= qr->qr_numregions; ++j) {
1004 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1005 foundit = 1;
1006 break;
1007 }
1008 r += O2HB_MAX_REGION_NAME_LEN;
1009 }
1010 if (!foundit) {
1011 status = -EINVAL;
1012 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1013 "in local node %d but not in joining node %d\n",
1014 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1015 dlm->node_num, qr->qr_node);
1016 goto bail;
1017 }
1018 l += O2HB_MAX_REGION_NAME_LEN;
1019 }
1020
1021 /* compare remote with local regions */
1022 r = remote;
1023 for (i = 0; i < qr->qr_numregions; ++i) {
1024 foundit = 0;
1025 l = local;
1026 for (j = 0; j < localnr; ++j) {
1027 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1028 foundit = 1;
1029 break;
1030 }
1031 l += O2HB_MAX_REGION_NAME_LEN;
1032 }
1033 if (!foundit) {
1034 status = -EINVAL;
1035 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1036 "in joining node %d but not in local node %d\n",
1037 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1038 qr->qr_node, dlm->node_num);
1039 goto bail;
1040 }
1041 r += O2HB_MAX_REGION_NAME_LEN;
1042 }
1043
1044bail:
1045 return status;
1046}
1047
1048static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1049{
1050 struct dlm_query_region *qr = NULL;
1051 int status, ret = 0, i;
1052 char *p;
1053
1054 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1055 goto bail;
1056
1057 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1058 if (!qr) {
1059 ret = -ENOMEM;
1060 mlog_errno(ret);
1061 goto bail;
1062 }
1063
1064 qr->qr_node = dlm->node_num;
1065 qr->qr_namelen = strlen(dlm->name);
1066 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1067 /* if local hb, the numregions will be zero */
1068 if (o2hb_global_heartbeat_active())
1069 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1070 O2NM_MAX_REGIONS);
1071
1072 p = qr->qr_regions;
1073 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1074 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1075
1076 i = -1;
1077 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1078 i + 1)) < O2NM_MAX_NODES) {
1079 if (i == dlm->node_num)
1080 continue;
1081
1082 mlog(0, "Sending regions to node %d\n", i);
1083
1084 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1085 sizeof(struct dlm_query_region),
1086 i, &status);
1087 if (ret >= 0)
1088 ret = status;
1089 if (ret) {
1090 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1091 ret, i);
1092 break;
1093 }
1094 }
1095
1096bail:
1097 kfree(qr);
1098 return ret;
1099}
1100
1101static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1102 void *data, void **ret_data)
1103{
1104 struct dlm_query_region *qr;
1105 struct dlm_ctxt *dlm = NULL;
1106 char *local = NULL;
1107 int status = 0;
1108
1109 qr = (struct dlm_query_region *) msg->buf;
1110
1111 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1112 qr->qr_domain);
1113
1114 /* buffer used in dlm_mast_regions() */
1115 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1116 if (!local)
1117 return -ENOMEM;
1118
1119 status = -EINVAL;
1120
1121 spin_lock(&dlm_domain_lock);
1122 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1123 if (!dlm) {
1124 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1125 "before join domain\n", qr->qr_node, qr->qr_domain);
1126 goto out_domain_lock;
1127 }
1128
1129 spin_lock(&dlm->spinlock);
1130 if (dlm->joining_node != qr->qr_node) {
1131 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1132 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1133 dlm->joining_node);
1134 goto out_dlm_lock;
1135 }
1136
1137 /* Support for global heartbeat was added in 1.1 */
1138 if (dlm->dlm_locking_proto.pv_major == 1 &&
1139 dlm->dlm_locking_proto.pv_minor == 0) {
1140 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1141 "but active dlm protocol is %d.%d\n", qr->qr_node,
1142 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1143 dlm->dlm_locking_proto.pv_minor);
1144 goto out_dlm_lock;
1145 }
1146
1147 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
1148
1149out_dlm_lock:
1150 spin_unlock(&dlm->spinlock);
1151
1152out_domain_lock:
1153 spin_unlock(&dlm_domain_lock);
1154
1155 kfree(local);
1156
1157 return status;
1158}
1159
1160static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1161{
1162 struct o2nm_node *local;
1163 struct dlm_node_info *remote;
1164 int i, j;
1165 int status = 0;
1166
1167 for (j = 0; j < qn->qn_numnodes; ++j)
1168 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1169 &(qn->qn_nodes[j].ni_ipv4_address),
1170 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1171
1172 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1173 local = o2nm_get_node_by_num(i);
1174 remote = NULL;
1175 for (j = 0; j < qn->qn_numnodes; ++j) {
1176 if (qn->qn_nodes[j].ni_nodenum == i) {
1177 remote = &(qn->qn_nodes[j]);
1178 break;
1179 }
1180 }
1181
1182 if (!local && !remote)
1183 continue;
1184
1185 if ((local && !remote) || (!local && remote))
1186 status = -EINVAL;
1187
1188 if (!status &&
1189 ((remote->ni_nodenum != local->nd_num) ||
1190 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1191 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1192 status = -EINVAL;
1193
1194 if (status) {
1195 if (remote && !local)
1196 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1197 "registered in joining node %d but not in "
1198 "local node %d\n", qn->qn_domain,
1199 remote->ni_nodenum,
1200 &(remote->ni_ipv4_address),
1201 ntohs(remote->ni_ipv4_port),
1202 qn->qn_nodenum, dlm->node_num);
1203 if (local && !remote)
1204 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1205 "registered in local node %d but not in "
1206 "joining node %d\n", qn->qn_domain,
1207 local->nd_num, &(local->nd_ipv4_address),
1208 ntohs(local->nd_ipv4_port),
1209 dlm->node_num, qn->qn_nodenum);
1210 BUG_ON((!local && !remote));
1211 }
1212
1213 if (local)
1214 o2nm_node_put(local);
1215 }
1216
1217 return status;
1218}
1219
1220static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1221{
1222 struct dlm_query_nodeinfo *qn = NULL;
1223 struct o2nm_node *node;
1224 int ret = 0, status, count, i;
1225
1226 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1227 goto bail;
1228
1229 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1230 if (!qn) {
1231 ret = -ENOMEM;
1232 mlog_errno(ret);
1233 goto bail;
1234 }
1235
1236 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1237 node = o2nm_get_node_by_num(i);
1238 if (!node)
1239 continue;
1240 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1241 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1242 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1243 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1244 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1245 ++count;
1246 o2nm_node_put(node);
1247 }
1248
1249 qn->qn_nodenum = dlm->node_num;
1250 qn->qn_numnodes = count;
1251 qn->qn_namelen = strlen(dlm->name);
1252 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1253
1254 i = -1;
1255 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1256 i + 1)) < O2NM_MAX_NODES) {
1257 if (i == dlm->node_num)
1258 continue;
1259
1260 mlog(0, "Sending nodeinfo to node %d\n", i);
1261
1262 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1263 qn, sizeof(struct dlm_query_nodeinfo),
1264 i, &status);
1265 if (ret >= 0)
1266 ret = status;
1267 if (ret) {
1268 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1269 break;
1270 }
1271 }
1272
1273bail:
1274 kfree(qn);
1275 return ret;
1276}
1277
1278static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1279 void *data, void **ret_data)
1280{
1281 struct dlm_query_nodeinfo *qn;
1282 struct dlm_ctxt *dlm = NULL;
1283 int locked = 0, status = -EINVAL;
1284
1285 qn = (struct dlm_query_nodeinfo *) msg->buf;
1286
1287 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1288 qn->qn_domain);
1289
1290 spin_lock(&dlm_domain_lock);
1291 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1292 if (!dlm) {
1293 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1294 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1295 goto bail;
1296 }
1297
1298 spin_lock(&dlm->spinlock);
1299 locked = 1;
1300 if (dlm->joining_node != qn->qn_nodenum) {
1301 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1302 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1303 dlm->joining_node);
1304 goto bail;
1305 }
1306
1307 /* Support for node query was added in 1.1 */
1308 if (dlm->dlm_locking_proto.pv_major == 1 &&
1309 dlm->dlm_locking_proto.pv_minor == 0) {
1310 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1311 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1312 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1313 dlm->dlm_locking_proto.pv_minor);
1314 goto bail;
1315 }
1316
1317 status = dlm_match_nodes(dlm, qn);
1318
1319bail:
1320 if (locked)
1321 spin_unlock(&dlm->spinlock);
1322 spin_unlock(&dlm_domain_lock);
1323
1324 return status;
1325}
1326
1327static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1328 void **ret_data)
1329{
1330 struct dlm_cancel_join *cancel;
1331 struct dlm_ctxt *dlm = NULL;
1332
1333 cancel = (struct dlm_cancel_join *) msg->buf;
1334
1335 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1336 cancel->domain);
1337
1338 spin_lock(&dlm_domain_lock);
1339 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1340
1341 if (dlm) {
1342 spin_lock(&dlm->spinlock);
1343
1344 /* Yikes, this guy wants to cancel his join. No
1345 * problem, we simply cleanup our join state. */
1346 BUG_ON(dlm->joining_node != cancel->node_idx);
1347 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1348
1349 spin_unlock(&dlm->spinlock);
1350 }
1351 spin_unlock(&dlm_domain_lock);
1352
1353 return 0;
1354}
1355
1356static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1357 unsigned int node)
1358{
1359 int status;
1360 struct dlm_cancel_join cancel_msg;
1361
1362 memset(&cancel_msg, 0, sizeof(cancel_msg));
1363 cancel_msg.node_idx = dlm->node_num;
1364 cancel_msg.name_len = strlen(dlm->name);
1365 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1366
1367 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1368 &cancel_msg, sizeof(cancel_msg), node,
1369 NULL);
1370 if (status < 0) {
1371 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1372 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1373 node);
1374 goto bail;
1375 }
1376
1377bail:
1378 return status;
1379}
1380
1381/* map_size should be in bytes. */
1382static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1383 unsigned long *node_map,
1384 unsigned int map_size)
1385{
1386 int status, tmpstat;
1387 int node;
1388
1389 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1390 sizeof(unsigned long))) {
1391 mlog(ML_ERROR,
1392 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
1393 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
1394 return -EINVAL;
1395 }
1396
1397 status = 0;
1398 node = -1;
1399 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1400 node + 1)) < O2NM_MAX_NODES) {
1401 if (node == dlm->node_num)
1402 continue;
1403
1404 tmpstat = dlm_send_one_join_cancel(dlm, node);
1405 if (tmpstat) {
1406 mlog(ML_ERROR, "Error return %d cancelling join on "
1407 "node %d\n", tmpstat, node);
1408 if (!status)
1409 status = tmpstat;
1410 }
1411 }
1412
1413 if (status)
1414 mlog_errno(status);
1415 return status;
1416}
1417
1418static int dlm_request_join(struct dlm_ctxt *dlm,
1419 int node,
1420 enum dlm_query_join_response_code *response)
1421{
1422 int status;
1423 struct dlm_query_join_request join_msg;
1424 struct dlm_query_join_packet packet;
1425 u32 join_resp;
1426
1427 mlog(0, "querying node %d\n", node);
1428
1429 memset(&join_msg, 0, sizeof(join_msg));
1430 join_msg.node_idx = dlm->node_num;
1431 join_msg.name_len = strlen(dlm->name);
1432 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
1433 join_msg.dlm_proto = dlm->dlm_locking_proto;
1434 join_msg.fs_proto = dlm->fs_locking_proto;
1435
1436 /* copy live node map to join message */
1437 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1438
1439 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
1440 sizeof(join_msg), node, &join_resp);
1441 if (status < 0 && status != -ENOPROTOOPT) {
1442 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1443 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1444 node);
1445 goto bail;
1446 }
1447 dlm_query_join_wire_to_packet(join_resp, &packet);
1448
1449 /* -ENOPROTOOPT from the net code means the other side isn't
1450 listening for our message type -- that's fine, it means
1451 his dlm isn't up, so we can consider him a 'yes' but not
1452 joined into the domain. */
1453 if (status == -ENOPROTOOPT) {
1454 status = 0;
1455 *response = JOIN_OK_NO_MAP;
1456 } else {
1457 *response = packet.code;
1458 switch (packet.code) {
1459 case JOIN_DISALLOW:
1460 case JOIN_OK_NO_MAP:
1461 break;
1462 case JOIN_PROTOCOL_MISMATCH:
1463 mlog(ML_NOTICE,
1464 "This node requested DLM locking protocol %u.%u and "
1465 "filesystem locking protocol %u.%u. At least one of "
1466 "the protocol versions on node %d is not compatible, "
1467 "disconnecting\n",
1468 dlm->dlm_locking_proto.pv_major,
1469 dlm->dlm_locking_proto.pv_minor,
1470 dlm->fs_locking_proto.pv_major,
1471 dlm->fs_locking_proto.pv_minor,
1472 node);
1473 status = -EPROTO;
1474 break;
1475 case JOIN_OK:
1476 /* Use the same locking protocol as the remote node */
1477 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1478 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
1479 mlog(0,
1480 "Node %d responds JOIN_OK with DLM locking protocol "
1481 "%u.%u and fs locking protocol %u.%u\n",
1482 node,
1483 dlm->dlm_locking_proto.pv_major,
1484 dlm->dlm_locking_proto.pv_minor,
1485 dlm->fs_locking_proto.pv_major,
1486 dlm->fs_locking_proto.pv_minor);
1487 break;
1488 default:
1489 status = -EINVAL;
1490 mlog(ML_ERROR, "invalid response %d from node %u\n",
1491 packet.code, node);
1492 /* Reset response to JOIN_DISALLOW */
1493 *response = JOIN_DISALLOW;
1494 break;
1495 }
1496 }
1497
1498 mlog(0, "status %d, node %d response is %d\n", status, node,
1499 *response);
1500
1501bail:
1502 return status;
1503}
1504
1505static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1506 unsigned int node)
1507{
1508 int status;
1509 int ret;
1510 struct dlm_assert_joined assert_msg;
1511
1512 mlog(0, "Sending join assert to node %u\n", node);
1513
1514 memset(&assert_msg, 0, sizeof(assert_msg));
1515 assert_msg.node_idx = dlm->node_num;
1516 assert_msg.name_len = strlen(dlm->name);
1517 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1518
1519 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1520 &assert_msg, sizeof(assert_msg), node,
1521 &ret);
1522 if (status < 0)
1523 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1524 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1525 node);
1526 else
1527 status = ret;
1528
1529 return status;
1530}
1531
1532static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1533 unsigned long *node_map)
1534{
1535 int status, node, live;
1536
1537 status = 0;
1538 node = -1;
1539 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1540 node + 1)) < O2NM_MAX_NODES) {
1541 if (node == dlm->node_num)
1542 continue;
1543
1544 do {
1545 /* It is very important that this message be
1546 * received so we spin until either the node
1547 * has died or it gets the message. */
1548 status = dlm_send_one_join_assert(dlm, node);
1549
1550 spin_lock(&dlm->spinlock);
1551 live = test_bit(node, dlm->live_nodes_map);
1552 spin_unlock(&dlm->spinlock);
1553
1554 if (status) {
1555 mlog(ML_ERROR, "Error return %d asserting "
1556 "join on node %d\n", status, node);
1557
1558 /* give us some time between errors... */
1559 if (live)
1560 msleep(DLM_DOMAIN_BACKOFF_MS);
1561 }
1562 } while (status && live);
1563 }
1564}
1565
1566struct domain_join_ctxt {
1567 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1568 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1569};
1570
1571static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1572 struct domain_join_ctxt *ctxt,
1573 enum dlm_query_join_response_code response)
1574{
1575 int ret;
1576
1577 if (response == JOIN_DISALLOW) {
1578 mlog(0, "Latest response of disallow -- should restart\n");
1579 return 1;
1580 }
1581
1582 spin_lock(&dlm->spinlock);
1583 /* For now, we restart the process if the node maps have
1584 * changed at all */
1585 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1586 sizeof(dlm->live_nodes_map));
1587 spin_unlock(&dlm->spinlock);
1588
1589 if (ret)
1590 mlog(0, "Node maps changed -- should restart\n");
1591
1592 return ret;
1593}
1594
1595static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1596{
1597 int status = 0, tmpstat, node;
1598 struct domain_join_ctxt *ctxt;
1599 enum dlm_query_join_response_code response = JOIN_DISALLOW;
1600
1601 mlog(0, "%p", dlm);
1602
1603 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
1604 if (!ctxt) {
1605 status = -ENOMEM;
1606 mlog_errno(status);
1607 goto bail;
1608 }
1609
1610 /* group sem locking should work for us here -- we're already
1611 * registered for heartbeat events so filling this should be
1612 * atomic wrt getting those handlers called. */
1613 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1614
1615 spin_lock(&dlm->spinlock);
1616 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1617
1618 __dlm_set_joining_node(dlm, dlm->node_num);
1619
1620 spin_unlock(&dlm->spinlock);
1621
1622 node = -1;
1623 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1624 node + 1)) < O2NM_MAX_NODES) {
1625 if (node == dlm->node_num)
1626 continue;
1627
1628 status = dlm_request_join(dlm, node, &response);
1629 if (status < 0) {
1630 mlog_errno(status);
1631 goto bail;
1632 }
1633
1634 /* Ok, either we got a response or the node doesn't have a
1635 * dlm up. */
1636 if (response == JOIN_OK)
1637 set_bit(node, ctxt->yes_resp_map);
1638
1639 if (dlm_should_restart_join(dlm, ctxt, response)) {
1640 status = -EAGAIN;
1641 goto bail;
1642 }
1643 }
1644
1645 mlog(0, "Yay, done querying nodes!\n");
1646
1647 /* Yay, everyone agree's we can join the domain. My domain is
1648 * comprised of all nodes who were put in the
1649 * yes_resp_map. Copy that into our domain map and send a join
1650 * assert message to clean up everyone elses state. */
1651 spin_lock(&dlm->spinlock);
1652 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1653 sizeof(ctxt->yes_resp_map));
1654 set_bit(dlm->node_num, dlm->domain_map);
1655 spin_unlock(&dlm->spinlock);
1656
1657 /* Support for global heartbeat and node info was added in 1.1 */
1658 if (dlm->dlm_locking_proto.pv_major > 1 ||
1659 dlm->dlm_locking_proto.pv_minor > 0) {
1660 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1661 if (status) {
1662 mlog_errno(status);
1663 goto bail;
1664 }
1665 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1666 if (status) {
1667 mlog_errno(status);
1668 goto bail;
1669 }
1670 }
1671
1672 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1673
1674 /* Joined state *must* be set before the joining node
1675 * information, otherwise the query_join handler may read no
1676 * current joiner but a state of NEW and tell joining nodes
1677 * we're not in the domain. */
1678 spin_lock(&dlm_domain_lock);
1679 dlm->dlm_state = DLM_CTXT_JOINED;
1680 dlm->num_joins++;
1681 spin_unlock(&dlm_domain_lock);
1682
1683bail:
1684 spin_lock(&dlm->spinlock);
1685 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1686 if (!status) {
1687 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
1688 __dlm_print_nodes(dlm);
1689 }
1690 spin_unlock(&dlm->spinlock);
1691
1692 if (ctxt) {
1693 /* Do we need to send a cancel message to any nodes? */
1694 if (status < 0) {
1695 tmpstat = dlm_send_join_cancels(dlm,
1696 ctxt->yes_resp_map,
1697 sizeof(ctxt->yes_resp_map));
1698 if (tmpstat < 0)
1699 mlog_errno(tmpstat);
1700 }
1701 kfree(ctxt);
1702 }
1703
1704 mlog(0, "returning %d\n", status);
1705 return status;
1706}
1707
1708static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1709{
1710 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1711 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
1712 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1713}
1714
1715static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1716{
1717 int status;
1718
1719 mlog(0, "registering handlers.\n");
1720
1721 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1722 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
1723 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1724 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
1725
1726 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
1727 if (status)
1728 goto bail;
1729
1730 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
1731 if (status)
1732 goto bail;
1733
1734 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1735 sizeof(struct dlm_master_request),
1736 dlm_master_request_handler,
1737 dlm, NULL, &dlm->dlm_domain_handlers);
1738 if (status)
1739 goto bail;
1740
1741 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1742 sizeof(struct dlm_assert_master),
1743 dlm_assert_master_handler,
1744 dlm, dlm_assert_master_post_handler,
1745 &dlm->dlm_domain_handlers);
1746 if (status)
1747 goto bail;
1748
1749 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1750 sizeof(struct dlm_create_lock),
1751 dlm_create_lock_handler,
1752 dlm, NULL, &dlm->dlm_domain_handlers);
1753 if (status)
1754 goto bail;
1755
1756 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1757 DLM_CONVERT_LOCK_MAX_LEN,
1758 dlm_convert_lock_handler,
1759 dlm, NULL, &dlm->dlm_domain_handlers);
1760 if (status)
1761 goto bail;
1762
1763 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1764 DLM_UNLOCK_LOCK_MAX_LEN,
1765 dlm_unlock_lock_handler,
1766 dlm, NULL, &dlm->dlm_domain_handlers);
1767 if (status)
1768 goto bail;
1769
1770 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1771 DLM_PROXY_AST_MAX_LEN,
1772 dlm_proxy_ast_handler,
1773 dlm, NULL, &dlm->dlm_domain_handlers);
1774 if (status)
1775 goto bail;
1776
1777 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1778 sizeof(struct dlm_exit_domain),
1779 dlm_exit_domain_handler,
1780 dlm, NULL, &dlm->dlm_domain_handlers);
1781 if (status)
1782 goto bail;
1783
1784 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1785 sizeof(struct dlm_deref_lockres),
1786 dlm_deref_lockres_handler,
1787 dlm, NULL, &dlm->dlm_domain_handlers);
1788 if (status)
1789 goto bail;
1790
1791 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1792 sizeof(struct dlm_migrate_request),
1793 dlm_migrate_request_handler,
1794 dlm, NULL, &dlm->dlm_domain_handlers);
1795 if (status)
1796 goto bail;
1797
1798 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1799 DLM_MIG_LOCKRES_MAX_LEN,
1800 dlm_mig_lockres_handler,
1801 dlm, NULL, &dlm->dlm_domain_handlers);
1802 if (status)
1803 goto bail;
1804
1805 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1806 sizeof(struct dlm_master_requery),
1807 dlm_master_requery_handler,
1808 dlm, NULL, &dlm->dlm_domain_handlers);
1809 if (status)
1810 goto bail;
1811
1812 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1813 sizeof(struct dlm_lock_request),
1814 dlm_request_all_locks_handler,
1815 dlm, NULL, &dlm->dlm_domain_handlers);
1816 if (status)
1817 goto bail;
1818
1819 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1820 sizeof(struct dlm_reco_data_done),
1821 dlm_reco_data_done_handler,
1822 dlm, NULL, &dlm->dlm_domain_handlers);
1823 if (status)
1824 goto bail;
1825
1826 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1827 sizeof(struct dlm_begin_reco),
1828 dlm_begin_reco_handler,
1829 dlm, NULL, &dlm->dlm_domain_handlers);
1830 if (status)
1831 goto bail;
1832
1833 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1834 sizeof(struct dlm_finalize_reco),
1835 dlm_finalize_reco_handler,
1836 dlm, NULL, &dlm->dlm_domain_handlers);
1837 if (status)
1838 goto bail;
1839
1840 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1841 sizeof(struct dlm_exit_domain),
1842 dlm_begin_exit_domain_handler,
1843 dlm, NULL, &dlm->dlm_domain_handlers);
1844 if (status)
1845 goto bail;
1846
1847 status = o2net_register_handler(DLM_DEREF_LOCKRES_DONE, dlm->key,
1848 sizeof(struct dlm_deref_lockres_done),
1849 dlm_deref_lockres_done_handler,
1850 dlm, NULL, &dlm->dlm_domain_handlers);
1851bail:
1852 if (status)
1853 dlm_unregister_domain_handlers(dlm);
1854
1855 return status;
1856}
1857
1858static int dlm_join_domain(struct dlm_ctxt *dlm)
1859{
1860 int status;
1861 unsigned int backoff;
1862 unsigned int total_backoff = 0;
1863 char wq_name[O2NM_MAX_NAME_LEN];
1864
1865 BUG_ON(!dlm);
1866
1867 mlog(0, "Join domain %s\n", dlm->name);
1868
1869 status = dlm_register_domain_handlers(dlm);
1870 if (status) {
1871 mlog_errno(status);
1872 goto bail;
1873 }
1874
1875 status = dlm_launch_thread(dlm);
1876 if (status < 0) {
1877 mlog_errno(status);
1878 goto bail;
1879 }
1880
1881 status = dlm_launch_recovery_thread(dlm);
1882 if (status < 0) {
1883 mlog_errno(status);
1884 goto bail;
1885 }
1886
1887 status = dlm_debug_init(dlm);
1888 if (status < 0) {
1889 mlog_errno(status);
1890 goto bail;
1891 }
1892
1893 snprintf(wq_name, O2NM_MAX_NAME_LEN, "dlm_wq-%s", dlm->name);
1894 dlm->dlm_worker = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 0);
1895 if (!dlm->dlm_worker) {
1896 status = -ENOMEM;
1897 mlog_errno(status);
1898 goto bail;
1899 }
1900
1901 do {
1902 status = dlm_try_to_join_domain(dlm);
1903
1904 /* If we're racing another node to the join, then we
1905 * need to back off temporarily and let them
1906 * complete. */
1907#define DLM_JOIN_TIMEOUT_MSECS 90000
1908 if (status == -EAGAIN) {
1909 if (signal_pending(current)) {
1910 status = -ERESTARTSYS;
1911 goto bail;
1912 }
1913
1914 if (total_backoff > DLM_JOIN_TIMEOUT_MSECS) {
1915 status = -ERESTARTSYS;
1916 mlog(ML_NOTICE, "Timed out joining dlm domain "
1917 "%s after %u msecs\n", dlm->name,
1918 total_backoff);
1919 goto bail;
1920 }
1921
1922 /*
1923 * <chip> After you!
1924 * <dale> No, after you!
1925 * <chip> I insist!
1926 * <dale> But you first!
1927 * ...
1928 */
1929 backoff = (unsigned int)(jiffies & 0x3);
1930 backoff *= DLM_DOMAIN_BACKOFF_MS;
1931 total_backoff += backoff;
1932 mlog(0, "backoff %d\n", backoff);
1933 msleep(backoff);
1934 }
1935 } while (status == -EAGAIN);
1936
1937 if (status < 0) {
1938 mlog_errno(status);
1939 goto bail;
1940 }
1941
1942 status = 0;
1943bail:
1944 wake_up(&dlm_domain_events);
1945
1946 if (status) {
1947 dlm_unregister_domain_handlers(dlm);
1948 dlm_debug_shutdown(dlm);
1949 dlm_complete_thread(dlm);
1950 dlm_complete_recovery_thread(dlm);
1951 dlm_destroy_dlm_worker(dlm);
1952 }
1953
1954 return status;
1955}
1956
1957static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1958 u32 key)
1959{
1960 int i;
1961 int ret;
1962 struct dlm_ctxt *dlm = NULL;
1963
1964 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
1965 if (!dlm) {
1966 ret = -ENOMEM;
1967 mlog_errno(ret);
1968 goto leave;
1969 }
1970
1971 dlm->name = kstrdup(domain, GFP_KERNEL);
1972 if (dlm->name == NULL) {
1973 ret = -ENOMEM;
1974 mlog_errno(ret);
1975 goto leave;
1976 }
1977
1978 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
1979 if (!dlm->lockres_hash) {
1980 ret = -ENOMEM;
1981 mlog_errno(ret);
1982 goto leave;
1983 }
1984
1985 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1986 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
1987
1988 dlm->master_hash = (struct hlist_head **)
1989 dlm_alloc_pagevec(DLM_HASH_PAGES);
1990 if (!dlm->master_hash) {
1991 ret = -ENOMEM;
1992 mlog_errno(ret);
1993 goto leave;
1994 }
1995
1996 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1997 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1998
1999 dlm->key = key;
2000 dlm->node_num = o2nm_this_node();
2001
2002 ret = dlm_create_debugfs_subroot(dlm);
2003 if (ret < 0)
2004 goto leave;
2005
2006 spin_lock_init(&dlm->spinlock);
2007 spin_lock_init(&dlm->master_lock);
2008 spin_lock_init(&dlm->ast_lock);
2009 spin_lock_init(&dlm->track_lock);
2010 INIT_LIST_HEAD(&dlm->list);
2011 INIT_LIST_HEAD(&dlm->dirty_list);
2012 INIT_LIST_HEAD(&dlm->reco.resources);
2013 INIT_LIST_HEAD(&dlm->reco.node_data);
2014 INIT_LIST_HEAD(&dlm->purge_list);
2015 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
2016 INIT_LIST_HEAD(&dlm->tracking_list);
2017 dlm->reco.state = 0;
2018
2019 INIT_LIST_HEAD(&dlm->pending_asts);
2020 INIT_LIST_HEAD(&dlm->pending_basts);
2021
2022 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2023 dlm->recovery_map, &(dlm->recovery_map[0]));
2024
2025 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2026 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2027 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2028
2029 dlm->dlm_thread_task = NULL;
2030 dlm->dlm_reco_thread_task = NULL;
2031 dlm->dlm_worker = NULL;
2032 init_waitqueue_head(&dlm->dlm_thread_wq);
2033 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2034 init_waitqueue_head(&dlm->reco.event);
2035 init_waitqueue_head(&dlm->ast_wq);
2036 init_waitqueue_head(&dlm->migration_wq);
2037 INIT_LIST_HEAD(&dlm->mle_hb_events);
2038
2039 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2040 init_waitqueue_head(&dlm->dlm_join_events);
2041
2042 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2043 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
2044
2045 atomic_set(&dlm->res_tot_count, 0);
2046 atomic_set(&dlm->res_cur_count, 0);
2047 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2048 atomic_set(&dlm->mle_tot_count[i], 0);
2049 atomic_set(&dlm->mle_cur_count[i], 0);
2050 }
2051
2052 spin_lock_init(&dlm->work_lock);
2053 INIT_LIST_HEAD(&dlm->work_list);
2054 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
2055
2056 kref_init(&dlm->dlm_refs);
2057 dlm->dlm_state = DLM_CTXT_NEW;
2058
2059 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2060
2061 mlog(0, "context init: refcount %u\n",
2062 kref_read(&dlm->dlm_refs));
2063
2064leave:
2065 if (ret < 0 && dlm) {
2066 if (dlm->master_hash)
2067 dlm_free_pagevec((void **)dlm->master_hash,
2068 DLM_HASH_PAGES);
2069
2070 if (dlm->lockres_hash)
2071 dlm_free_pagevec((void **)dlm->lockres_hash,
2072 DLM_HASH_PAGES);
2073
2074 kfree(dlm->name);
2075 kfree(dlm);
2076 dlm = NULL;
2077 }
2078 return dlm;
2079}
2080
2081/*
2082 * Compare a requested locking protocol version against the current one.
2083 *
2084 * If the major numbers are different, they are incompatible.
2085 * If the current minor is greater than the request, they are incompatible.
2086 * If the current minor is less than or equal to the request, they are
2087 * compatible, and the requester should run at the current minor version.
2088 */
2089static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2090 struct dlm_protocol_version *request)
2091{
2092 if (existing->pv_major != request->pv_major)
2093 return 1;
2094
2095 if (existing->pv_minor > request->pv_minor)
2096 return 1;
2097
2098 if (existing->pv_minor < request->pv_minor)
2099 request->pv_minor = existing->pv_minor;
2100
2101 return 0;
2102}
2103
2104/*
2105 * dlm_register_domain: one-time setup per "domain".
2106 *
2107 * The filesystem passes in the requested locking version via proto.
2108 * If registration was successful, proto will contain the negotiated
2109 * locking protocol.
2110 */
2111struct dlm_ctxt * dlm_register_domain(const char *domain,
2112 u32 key,
2113 struct dlm_protocol_version *fs_proto)
2114{
2115 int ret;
2116 struct dlm_ctxt *dlm = NULL;
2117 struct dlm_ctxt *new_ctxt = NULL;
2118
2119 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
2120 ret = -ENAMETOOLONG;
2121 mlog(ML_ERROR, "domain name length too long\n");
2122 goto leave;
2123 }
2124
2125 mlog(0, "register called for domain \"%s\"\n", domain);
2126
2127retry:
2128 dlm = NULL;
2129 if (signal_pending(current)) {
2130 ret = -ERESTARTSYS;
2131 mlog_errno(ret);
2132 goto leave;
2133 }
2134
2135 spin_lock(&dlm_domain_lock);
2136
2137 dlm = __dlm_lookup_domain(domain);
2138 if (dlm) {
2139 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2140 spin_unlock(&dlm_domain_lock);
2141
2142 mlog(0, "This ctxt is not joined yet!\n");
2143 wait_event_interruptible(dlm_domain_events,
2144 dlm_wait_on_domain_helper(
2145 domain));
2146 goto retry;
2147 }
2148
2149 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
2150 spin_unlock(&dlm_domain_lock);
2151 mlog(ML_ERROR,
2152 "Requested locking protocol version is not "
2153 "compatible with already registered domain "
2154 "\"%s\"\n", domain);
2155 ret = -EPROTO;
2156 goto leave;
2157 }
2158
2159 __dlm_get(dlm);
2160 dlm->num_joins++;
2161
2162 spin_unlock(&dlm_domain_lock);
2163
2164 ret = 0;
2165 goto leave;
2166 }
2167
2168 /* doesn't exist */
2169 if (!new_ctxt) {
2170 spin_unlock(&dlm_domain_lock);
2171
2172 new_ctxt = dlm_alloc_ctxt(domain, key);
2173 if (new_ctxt)
2174 goto retry;
2175
2176 ret = -ENOMEM;
2177 mlog_errno(ret);
2178 goto leave;
2179 }
2180
2181 /* a little variable switch-a-roo here... */
2182 dlm = new_ctxt;
2183 new_ctxt = NULL;
2184
2185 /* add the new domain */
2186 list_add_tail(&dlm->list, &dlm_domains);
2187 spin_unlock(&dlm_domain_lock);
2188
2189 /*
2190 * Pass the locking protocol version into the join. If the join
2191 * succeeds, it will have the negotiated protocol set.
2192 */
2193 dlm->dlm_locking_proto = dlm_protocol;
2194 dlm->fs_locking_proto = *fs_proto;
2195
2196 ret = dlm_join_domain(dlm);
2197 if (ret) {
2198 mlog_errno(ret);
2199 dlm_put(dlm);
2200 goto leave;
2201 }
2202
2203 /* Tell the caller what locking protocol we negotiated */
2204 *fs_proto = dlm->fs_locking_proto;
2205
2206 ret = 0;
2207leave:
2208 if (new_ctxt)
2209 dlm_free_ctxt_mem(new_ctxt);
2210
2211 if (ret < 0)
2212 dlm = ERR_PTR(ret);
2213
2214 return dlm;
2215}
2216EXPORT_SYMBOL_GPL(dlm_register_domain);
2217
2218static LIST_HEAD(dlm_join_handlers);
2219
2220static void dlm_unregister_net_handlers(void)
2221{
2222 o2net_unregister_handler_list(&dlm_join_handlers);
2223}
2224
2225static int dlm_register_net_handlers(void)
2226{
2227 int status = 0;
2228
2229 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2230 sizeof(struct dlm_query_join_request),
2231 dlm_query_join_handler,
2232 NULL, NULL, &dlm_join_handlers);
2233 if (status)
2234 goto bail;
2235
2236 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2237 sizeof(struct dlm_assert_joined),
2238 dlm_assert_joined_handler,
2239 NULL, NULL, &dlm_join_handlers);
2240 if (status)
2241 goto bail;
2242
2243 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2244 sizeof(struct dlm_cancel_join),
2245 dlm_cancel_join_handler,
2246 NULL, NULL, &dlm_join_handlers);
2247 if (status)
2248 goto bail;
2249
2250 status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2251 sizeof(struct dlm_query_region),
2252 dlm_query_region_handler,
2253 NULL, NULL, &dlm_join_handlers);
2254
2255 if (status)
2256 goto bail;
2257
2258 status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2259 sizeof(struct dlm_query_nodeinfo),
2260 dlm_query_nodeinfo_handler,
2261 NULL, NULL, &dlm_join_handlers);
2262bail:
2263 if (status < 0)
2264 dlm_unregister_net_handlers();
2265
2266 return status;
2267}
2268
2269/* Domain eviction callback handling.
2270 *
2271 * The file system requires notification of node death *before* the
2272 * dlm completes it's recovery work, otherwise it may be able to
2273 * acquire locks on resources requiring recovery. Since the dlm can
2274 * evict a node from it's domain *before* heartbeat fires, a similar
2275 * mechanism is required. */
2276
2277/* Eviction is not expected to happen often, so a per-domain lock is
2278 * not necessary. Eviction callbacks are allowed to sleep for short
2279 * periods of time. */
2280static DECLARE_RWSEM(dlm_callback_sem);
2281
2282void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2283 int node_num)
2284{
2285 struct dlm_eviction_cb *cb;
2286
2287 down_read(&dlm_callback_sem);
2288 list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
2289 cb->ec_func(node_num, cb->ec_data);
2290 }
2291 up_read(&dlm_callback_sem);
2292}
2293
2294void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2295 dlm_eviction_func *f,
2296 void *data)
2297{
2298 INIT_LIST_HEAD(&cb->ec_item);
2299 cb->ec_func = f;
2300 cb->ec_data = data;
2301}
2302EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2303
2304void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2305 struct dlm_eviction_cb *cb)
2306{
2307 down_write(&dlm_callback_sem);
2308 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2309 up_write(&dlm_callback_sem);
2310}
2311EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2312
2313void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2314{
2315 down_write(&dlm_callback_sem);
2316 list_del_init(&cb->ec_item);
2317 up_write(&dlm_callback_sem);
2318}
2319EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2320
2321static int __init dlm_init(void)
2322{
2323 int status;
2324
2325 status = dlm_init_mle_cache();
2326 if (status) {
2327 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
2328 goto error;
2329 }
2330
2331 status = dlm_init_master_caches();
2332 if (status) {
2333 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2334 "o2dlm_lockname slabcaches\n");
2335 goto error;
2336 }
2337
2338 status = dlm_init_lock_cache();
2339 if (status) {
2340 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2341 goto error;
2342 }
2343
2344 status = dlm_register_net_handlers();
2345 if (status) {
2346 mlog(ML_ERROR, "Unable to register network handlers\n");
2347 goto error;
2348 }
2349
2350 status = dlm_create_debugfs_root();
2351 if (status)
2352 goto error;
2353
2354 return 0;
2355error:
2356 dlm_unregister_net_handlers();
2357 dlm_destroy_lock_cache();
2358 dlm_destroy_master_caches();
2359 dlm_destroy_mle_cache();
2360 return -1;
2361}
2362
2363static void __exit dlm_exit (void)
2364{
2365 dlm_destroy_debugfs_root();
2366 dlm_unregister_net_handlers();
2367 dlm_destroy_lock_cache();
2368 dlm_destroy_master_caches();
2369 dlm_destroy_mle_cache();
2370}
2371
2372MODULE_AUTHOR("Oracle");
2373MODULE_LICENSE("GPL");
2374MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
2375
2376module_init(dlm_init);
2377module_exit(dlm_exit);