Merge branch 'master'
[linux-2.6] / fs / ocfs2 / dlm / dlmdomain.c
1 /* -*- 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/utsname.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
34 #include <linux/delay.h>
35 #include <linux/err.h>
36
37 #include "cluster/heartbeat.h"
38 #include "cluster/nodemanager.h"
39 #include "cluster/tcp.h"
40
41 #include "dlmapi.h"
42 #include "dlmcommon.h"
43
44 #include "dlmdebug.h"
45 #include "dlmdomain.h"
46
47 #include "dlmver.h"
48
49 #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
50 #include "cluster/masklog.h"
51
52 /*
53  *
54  * spinlock lock ordering: if multiple locks are needed, obey this ordering:
55  *    dlm_domain_lock
56  *    struct dlm_ctxt->spinlock
57  *    struct dlm_lock_resource->spinlock
58  *    struct dlm_ctxt->master_lock
59  *    struct dlm_ctxt->ast_lock
60  *    dlm_master_list_entry->spinlock
61  *    dlm_lock->spinlock
62  *
63  */
64
65 spinlock_t dlm_domain_lock = SPIN_LOCK_UNLOCKED;
66 LIST_HEAD(dlm_domains);
67 static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
68
69 #define DLM_DOMAIN_BACKOFF_MS 200
70
71 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data);
72 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data);
73 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data);
74 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data);
75
76 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
77
78 void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
79 {
80         hlist_del_init(&lockres->hash_node);
81         dlm_lockres_put(lockres);
82 }
83
84 void __dlm_insert_lockres(struct dlm_ctxt *dlm,
85                        struct dlm_lock_resource *res)
86 {
87         struct hlist_head *bucket;
88         struct qstr *q;
89
90         assert_spin_locked(&dlm->spinlock);
91
92         q = &res->lockname;
93         q->hash = full_name_hash(q->name, q->len);
94         bucket = &(dlm->lockres_hash[q->hash % DLM_HASH_BUCKETS]);
95
96         /* get a reference for our hashtable */
97         dlm_lockres_get(res);
98
99         hlist_add_head(&res->hash_node, bucket);
100 }
101
102 struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
103                                          const char *name,
104                                          unsigned int len)
105 {
106         unsigned int hash;
107         struct hlist_node *iter;
108         struct dlm_lock_resource *tmpres=NULL;
109         struct hlist_head *bucket;
110
111         mlog_entry("%.*s\n", len, name);
112
113         assert_spin_locked(&dlm->spinlock);
114
115         hash = full_name_hash(name, len);
116
117         bucket = &(dlm->lockres_hash[hash % DLM_HASH_BUCKETS]);
118
119         /* check for pre-existing lock */
120         hlist_for_each(iter, bucket) {
121                 tmpres = hlist_entry(iter, struct dlm_lock_resource, hash_node);
122                 if (tmpres->lockname.len == len &&
123                     memcmp(tmpres->lockname.name, name, len) == 0) {
124                         dlm_lockres_get(tmpres);
125                         break;
126                 }
127
128                 tmpres = NULL;
129         }
130         return tmpres;
131 }
132
133 struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
134                                     const char *name,
135                                     unsigned int len)
136 {
137         struct dlm_lock_resource *res;
138
139         spin_lock(&dlm->spinlock);
140         res = __dlm_lookup_lockres(dlm, name, len);
141         spin_unlock(&dlm->spinlock);
142         return res;
143 }
144
145 static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
146 {
147         struct dlm_ctxt *tmp = NULL;
148         struct list_head *iter;
149
150         assert_spin_locked(&dlm_domain_lock);
151
152         /* tmp->name here is always NULL terminated,
153          * but domain may not be! */
154         list_for_each(iter, &dlm_domains) {
155                 tmp = list_entry (iter, struct dlm_ctxt, list);
156                 if (strlen(tmp->name) == len &&
157                     memcmp(tmp->name, domain, len)==0)
158                         break;
159                 tmp = NULL;
160         }
161
162         return tmp;
163 }
164
165 /* For null terminated domain strings ONLY */
166 static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
167 {
168         assert_spin_locked(&dlm_domain_lock);
169
170         return __dlm_lookup_domain_full(domain, strlen(domain));
171 }
172
173
174 /* returns true on one of two conditions:
175  * 1) the domain does not exist
176  * 2) the domain exists and it's state is "joined" */
177 static int dlm_wait_on_domain_helper(const char *domain)
178 {
179         int ret = 0;
180         struct dlm_ctxt *tmp = NULL;
181
182         spin_lock(&dlm_domain_lock);
183
184         tmp = __dlm_lookup_domain(domain);
185         if (!tmp)
186                 ret = 1;
187         else if (tmp->dlm_state == DLM_CTXT_JOINED)
188                 ret = 1;
189
190         spin_unlock(&dlm_domain_lock);
191         return ret;
192 }
193
194 static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
195 {
196         if (dlm->lockres_hash)
197                 free_page((unsigned long) dlm->lockres_hash);
198
199         if (dlm->name)
200                 kfree(dlm->name);
201
202         kfree(dlm);
203 }
204
205 /* A little strange - this function will be called while holding
206  * dlm_domain_lock and is expected to be holding it on the way out. We
207  * will however drop and reacquire it multiple times */
208 static void dlm_ctxt_release(struct kref *kref)
209 {
210         struct dlm_ctxt *dlm;
211
212         dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
213
214         BUG_ON(dlm->num_joins);
215         BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
216
217         /* we may still be in the list if we hit an error during join. */
218         list_del_init(&dlm->list);
219
220         spin_unlock(&dlm_domain_lock);
221
222         mlog(0, "freeing memory from domain %s\n", dlm->name);
223
224         wake_up(&dlm_domain_events);
225
226         dlm_free_ctxt_mem(dlm);
227
228         spin_lock(&dlm_domain_lock);
229 }
230
231 void dlm_put(struct dlm_ctxt *dlm)
232 {
233         spin_lock(&dlm_domain_lock);
234         kref_put(&dlm->dlm_refs, dlm_ctxt_release);
235         spin_unlock(&dlm_domain_lock);
236 }
237
238 static void __dlm_get(struct dlm_ctxt *dlm)
239 {
240         kref_get(&dlm->dlm_refs);
241 }
242
243 /* given a questionable reference to a dlm object, gets a reference if
244  * it can find it in the list, otherwise returns NULL in which case
245  * you shouldn't trust your pointer. */
246 struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
247 {
248         struct list_head *iter;
249         struct dlm_ctxt *target = NULL;
250
251         spin_lock(&dlm_domain_lock);
252
253         list_for_each(iter, &dlm_domains) {
254                 target = list_entry (iter, struct dlm_ctxt, list);
255
256                 if (target == dlm) {
257                         __dlm_get(target);
258                         break;
259                 }
260
261                 target = NULL;
262         }
263
264         spin_unlock(&dlm_domain_lock);
265
266         return target;
267 }
268
269 int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
270 {
271         int ret;
272
273         spin_lock(&dlm_domain_lock);
274         ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
275                 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
276         spin_unlock(&dlm_domain_lock);
277
278         return ret;
279 }
280
281 static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
282 {
283         dlm_unregister_domain_handlers(dlm);
284         dlm_complete_thread(dlm);
285         dlm_complete_recovery_thread(dlm);
286
287         /* We've left the domain. Now we can take ourselves out of the
288          * list and allow the kref stuff to help us free the
289          * memory. */
290         spin_lock(&dlm_domain_lock);
291         list_del_init(&dlm->list);
292         spin_unlock(&dlm_domain_lock);
293
294         /* Wake up anyone waiting for us to remove this domain */
295         wake_up(&dlm_domain_events);
296 }
297
298 static void dlm_migrate_all_locks(struct dlm_ctxt *dlm)
299 {
300         int i;
301         struct dlm_lock_resource *res;
302
303         mlog(0, "Migrating locks from domain %s\n", dlm->name);
304 restart:
305         spin_lock(&dlm->spinlock);
306         for (i = 0; i < DLM_HASH_BUCKETS; i++) {
307                 while (!hlist_empty(&dlm->lockres_hash[i])) {
308                         res = hlist_entry(dlm->lockres_hash[i].first,
309                                           struct dlm_lock_resource, hash_node);
310                         /* need reference when manually grabbing lockres */
311                         dlm_lockres_get(res);
312                         /* this should unhash the lockres
313                          * and exit with dlm->spinlock */
314                         mlog(0, "purging res=%p\n", res);
315                         if (dlm_lockres_is_dirty(dlm, res)) {
316                                 /* HACK!  this should absolutely go.
317                                  * need to figure out why some empty
318                                  * lockreses are still marked dirty */
319                                 mlog(ML_ERROR, "lockres %.*s dirty!\n",
320                                      res->lockname.len, res->lockname.name);
321
322                                 spin_unlock(&dlm->spinlock);
323                                 dlm_kick_thread(dlm, res);
324                                 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
325                                 dlm_lockres_put(res);
326                                 goto restart;
327                         }
328                         dlm_purge_lockres(dlm, res);
329                         dlm_lockres_put(res);
330                 }
331         }
332         spin_unlock(&dlm->spinlock);
333
334         mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
335 }
336
337 static int dlm_no_joining_node(struct dlm_ctxt *dlm)
338 {
339         int ret;
340
341         spin_lock(&dlm->spinlock);
342         ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
343         spin_unlock(&dlm->spinlock);
344
345         return ret;
346 }
347
348 static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
349 {
350         /* Yikes, a double spinlock! I need domain_lock for the dlm
351          * state and the dlm spinlock for join state... Sorry! */
352 again:
353         spin_lock(&dlm_domain_lock);
354         spin_lock(&dlm->spinlock);
355
356         if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
357                 mlog(0, "Node %d is joining, we wait on it.\n",
358                           dlm->joining_node);
359                 spin_unlock(&dlm->spinlock);
360                 spin_unlock(&dlm_domain_lock);
361
362                 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
363                 goto again;
364         }
365
366         dlm->dlm_state = DLM_CTXT_LEAVING;
367         spin_unlock(&dlm->spinlock);
368         spin_unlock(&dlm_domain_lock);
369 }
370
371 static void __dlm_print_nodes(struct dlm_ctxt *dlm)
372 {
373         int node = -1;
374
375         assert_spin_locked(&dlm->spinlock);
376
377         mlog(ML_NOTICE, "Nodes in my domain (\"%s\"):\n", dlm->name);
378
379         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
380                                      node + 1)) < O2NM_MAX_NODES) {
381                 mlog(ML_NOTICE, " node %d\n", node);
382         }
383 }
384
385 static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data)
386 {
387         struct dlm_ctxt *dlm = data;
388         unsigned int node;
389         struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
390
391         mlog_entry("%p %u %p", msg, len, data);
392
393         if (!dlm_grab(dlm))
394                 return 0;
395
396         node = exit_msg->node_idx;
397
398         mlog(0, "Node %u leaves domain %s\n", node, dlm->name);
399
400         spin_lock(&dlm->spinlock);
401         clear_bit(node, dlm->domain_map);
402         __dlm_print_nodes(dlm);
403
404         /* notify anything attached to the heartbeat events */
405         dlm_hb_event_notify_attached(dlm, node, 0);
406
407         spin_unlock(&dlm->spinlock);
408
409         dlm_put(dlm);
410
411         return 0;
412 }
413
414 static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
415                                     unsigned int node)
416 {
417         int status;
418         struct dlm_exit_domain leave_msg;
419
420         mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
421                   node, dlm->name, dlm->node_num);
422
423         memset(&leave_msg, 0, sizeof(leave_msg));
424         leave_msg.node_idx = dlm->node_num;
425
426         status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
427                                     &leave_msg, sizeof(leave_msg), node,
428                                     NULL);
429
430         mlog(0, "status return %d from o2net_send_message\n", status);
431
432         return status;
433 }
434
435
436 static void dlm_leave_domain(struct dlm_ctxt *dlm)
437 {
438         int node, clear_node, status;
439
440         /* At this point we've migrated away all our locks and won't
441          * accept mastership of new ones. The dlm is responsible for
442          * almost nothing now. We make sure not to confuse any joining
443          * nodes and then commence shutdown procedure. */
444
445         spin_lock(&dlm->spinlock);
446         /* Clear ourselves from the domain map */
447         clear_bit(dlm->node_num, dlm->domain_map);
448         while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
449                                      0)) < O2NM_MAX_NODES) {
450                 /* Drop the dlm spinlock. This is safe wrt the domain_map.
451                  * -nodes cannot be added now as the
452                  *   query_join_handlers knows to respond with OK_NO_MAP
453                  * -we catch the right network errors if a node is
454                  *   removed from the map while we're sending him the
455                  *   exit message. */
456                 spin_unlock(&dlm->spinlock);
457
458                 clear_node = 1;
459
460                 status = dlm_send_one_domain_exit(dlm, node);
461                 if (status < 0 &&
462                     status != -ENOPROTOOPT &&
463                     status != -ENOTCONN) {
464                         mlog(ML_NOTICE, "Error %d sending domain exit message "
465                              "to node %d\n", status, node);
466
467                         /* Not sure what to do here but lets sleep for
468                          * a bit in case this was a transient
469                          * error... */
470                         msleep(DLM_DOMAIN_BACKOFF_MS);
471                         clear_node = 0;
472                 }
473
474                 spin_lock(&dlm->spinlock);
475                 /* If we're not clearing the node bit then we intend
476                  * to loop back around to try again. */
477                 if (clear_node)
478                         clear_bit(node, dlm->domain_map);
479         }
480         spin_unlock(&dlm->spinlock);
481 }
482
483 int dlm_joined(struct dlm_ctxt *dlm)
484 {
485         int ret = 0;
486
487         spin_lock(&dlm_domain_lock);
488
489         if (dlm->dlm_state == DLM_CTXT_JOINED)
490                 ret = 1;
491
492         spin_unlock(&dlm_domain_lock);
493
494         return ret;
495 }
496
497 int dlm_shutting_down(struct dlm_ctxt *dlm)
498 {
499         int ret = 0;
500
501         spin_lock(&dlm_domain_lock);
502
503         if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
504                 ret = 1;
505
506         spin_unlock(&dlm_domain_lock);
507
508         return ret;
509 }
510
511 void dlm_unregister_domain(struct dlm_ctxt *dlm)
512 {
513         int leave = 0;
514
515         spin_lock(&dlm_domain_lock);
516         BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
517         BUG_ON(!dlm->num_joins);
518
519         dlm->num_joins--;
520         if (!dlm->num_joins) {
521                 /* We mark it "in shutdown" now so new register
522                  * requests wait until we've completely left the
523                  * domain. Don't use DLM_CTXT_LEAVING yet as we still
524                  * want new domain joins to communicate with us at
525                  * least until we've completed migration of our
526                  * resources. */
527                 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
528                 leave = 1;
529         }
530         spin_unlock(&dlm_domain_lock);
531
532         if (leave) {
533                 mlog(0, "shutting down domain %s\n", dlm->name);
534
535                 /* We changed dlm state, notify the thread */
536                 dlm_kick_thread(dlm, NULL);
537
538                 dlm_migrate_all_locks(dlm);
539                 dlm_mark_domain_leaving(dlm);
540                 dlm_leave_domain(dlm);
541                 dlm_complete_dlm_shutdown(dlm);
542         }
543         dlm_put(dlm);
544 }
545 EXPORT_SYMBOL_GPL(dlm_unregister_domain);
546
547 static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data)
548 {
549         struct dlm_query_join_request *query;
550         enum dlm_query_join_response response;
551         struct dlm_ctxt *dlm = NULL;
552
553         query = (struct dlm_query_join_request *) msg->buf;
554
555         mlog(0, "node %u wants to join domain %s\n", query->node_idx,
556                   query->domain);
557
558         /*
559          * If heartbeat doesn't consider the node live, tell it
560          * to back off and try again.  This gives heartbeat a chance
561          * to catch up.
562          */
563         if (!o2hb_check_node_heartbeating(query->node_idx)) {
564                 mlog(0, "node %u is not in our live map yet\n",
565                      query->node_idx);
566
567                 response = JOIN_DISALLOW;
568                 goto respond;
569         }
570
571         response = JOIN_OK_NO_MAP;
572
573         spin_lock(&dlm_domain_lock);
574         dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
575         /* Once the dlm ctxt is marked as leaving then we don't want
576          * to be put in someone's domain map. 
577          * Also, explicitly disallow joining at certain troublesome
578          * times (ie. during recovery). */
579         if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
580                 int bit = query->node_idx;
581                 spin_lock(&dlm->spinlock);
582
583                 if (dlm->dlm_state == DLM_CTXT_NEW &&
584                     dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
585                         /*If this is a brand new context and we
586                          * haven't started our join process yet, then
587                          * the other node won the race. */
588                         response = JOIN_OK_NO_MAP;
589                 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
590                         /* Disallow parallel joins. */
591                         response = JOIN_DISALLOW;
592                 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
593                         mlog(ML_NOTICE, "node %u trying to join, but recovery "
594                              "is ongoing.\n", bit);
595                         response = JOIN_DISALLOW;
596                 } else if (test_bit(bit, dlm->recovery_map)) {
597                         mlog(ML_NOTICE, "node %u trying to join, but it "
598                              "still needs recovery.\n", bit);
599                         response = JOIN_DISALLOW;
600                 } else if (test_bit(bit, dlm->domain_map)) {
601                         mlog(ML_NOTICE, "node %u trying to join, but it "
602                              "is still in the domain! needs recovery?\n",
603                              bit);
604                         response = JOIN_DISALLOW;
605                 } else {
606                         /* Alright we're fully a part of this domain
607                          * so we keep some state as to who's joining
608                          * and indicate to him that needs to be fixed
609                          * up. */
610                         response = JOIN_OK;
611                         __dlm_set_joining_node(dlm, query->node_idx);
612                 }
613
614                 spin_unlock(&dlm->spinlock);
615         }
616         spin_unlock(&dlm_domain_lock);
617
618 respond:
619         mlog(0, "We respond with %u\n", response);
620
621         return response;
622 }
623
624 static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
625 {
626         struct dlm_assert_joined *assert;
627         struct dlm_ctxt *dlm = NULL;
628
629         assert = (struct dlm_assert_joined *) msg->buf;
630
631         mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
632                   assert->domain);
633
634         spin_lock(&dlm_domain_lock);
635         dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
636         /* XXX should we consider no dlm ctxt an error? */
637         if (dlm) {
638                 spin_lock(&dlm->spinlock);
639
640                 /* Alright, this node has officially joined our
641                  * domain. Set him in the map and clean up our
642                  * leftover join state. */
643                 BUG_ON(dlm->joining_node != assert->node_idx);
644                 set_bit(assert->node_idx, dlm->domain_map);
645                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
646
647                 __dlm_print_nodes(dlm);
648
649                 /* notify anything attached to the heartbeat events */
650                 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
651
652                 spin_unlock(&dlm->spinlock);
653         }
654         spin_unlock(&dlm_domain_lock);
655
656         return 0;
657 }
658
659 static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
660 {
661         struct dlm_cancel_join *cancel;
662         struct dlm_ctxt *dlm = NULL;
663
664         cancel = (struct dlm_cancel_join *) msg->buf;
665
666         mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
667                   cancel->domain);
668
669         spin_lock(&dlm_domain_lock);
670         dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
671
672         if (dlm) {
673                 spin_lock(&dlm->spinlock);
674
675                 /* Yikes, this guy wants to cancel his join. No
676                  * problem, we simply cleanup our join state. */
677                 BUG_ON(dlm->joining_node != cancel->node_idx);
678                 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
679
680                 spin_unlock(&dlm->spinlock);
681         }
682         spin_unlock(&dlm_domain_lock);
683
684         return 0;
685 }
686
687 static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
688                                     unsigned int node)
689 {
690         int status;
691         struct dlm_cancel_join cancel_msg;
692
693         memset(&cancel_msg, 0, sizeof(cancel_msg));
694         cancel_msg.node_idx = dlm->node_num;
695         cancel_msg.name_len = strlen(dlm->name);
696         memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
697
698         status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
699                                     &cancel_msg, sizeof(cancel_msg), node,
700                                     NULL);
701         if (status < 0) {
702                 mlog_errno(status);
703                 goto bail;
704         }
705
706 bail:
707         return status;
708 }
709
710 /* map_size should be in bytes. */
711 static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
712                                  unsigned long *node_map,
713                                  unsigned int map_size)
714 {
715         int status, tmpstat;
716         unsigned int node;
717
718         if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
719                          sizeof(unsigned long))) {
720                 mlog(ML_ERROR,
721                      "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
722                      map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
723                 return -EINVAL;
724         }
725
726         status = 0;
727         node = -1;
728         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
729                                      node + 1)) < O2NM_MAX_NODES) {
730                 if (node == dlm->node_num)
731                         continue;
732
733                 tmpstat = dlm_send_one_join_cancel(dlm, node);
734                 if (tmpstat) {
735                         mlog(ML_ERROR, "Error return %d cancelling join on "
736                              "node %d\n", tmpstat, node);
737                         if (!status)
738                                 status = tmpstat;
739                 }
740         }
741
742         if (status)
743                 mlog_errno(status);
744         return status;
745 }
746
747 static int dlm_request_join(struct dlm_ctxt *dlm,
748                             int node,
749                             enum dlm_query_join_response *response)
750 {
751         int status, retval;
752         struct dlm_query_join_request join_msg;
753
754         mlog(0, "querying node %d\n", node);
755
756         memset(&join_msg, 0, sizeof(join_msg));
757         join_msg.node_idx = dlm->node_num;
758         join_msg.name_len = strlen(dlm->name);
759         memcpy(join_msg.domain, dlm->name, join_msg.name_len);
760
761         status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
762                                     sizeof(join_msg), node, &retval);
763         if (status < 0 && status != -ENOPROTOOPT) {
764                 mlog_errno(status);
765                 goto bail;
766         }
767
768         /* -ENOPROTOOPT from the net code means the other side isn't
769             listening for our message type -- that's fine, it means
770             his dlm isn't up, so we can consider him a 'yes' but not
771             joined into the domain.  */
772         if (status == -ENOPROTOOPT) {
773                 status = 0;
774                 *response = JOIN_OK_NO_MAP;
775         } else if (retval == JOIN_DISALLOW ||
776                    retval == JOIN_OK ||
777                    retval == JOIN_OK_NO_MAP) {
778                 *response = retval;
779         } else {
780                 status = -EINVAL;
781                 mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
782                      node);
783         }
784
785         mlog(0, "status %d, node %d response is %d\n", status, node,
786                   *response);
787
788 bail:
789         return status;
790 }
791
792 static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
793                                     unsigned int node)
794 {
795         int status;
796         struct dlm_assert_joined assert_msg;
797
798         mlog(0, "Sending join assert to node %u\n", node);
799
800         memset(&assert_msg, 0, sizeof(assert_msg));
801         assert_msg.node_idx = dlm->node_num;
802         assert_msg.name_len = strlen(dlm->name);
803         memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
804
805         status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
806                                     &assert_msg, sizeof(assert_msg), node,
807                                     NULL);
808         if (status < 0)
809                 mlog_errno(status);
810
811         return status;
812 }
813
814 static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
815                                   unsigned long *node_map)
816 {
817         int status, node, live;
818
819         status = 0;
820         node = -1;
821         while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
822                                      node + 1)) < O2NM_MAX_NODES) {
823                 if (node == dlm->node_num)
824                         continue;
825
826                 do {
827                         /* It is very important that this message be
828                          * received so we spin until either the node
829                          * has died or it gets the message. */
830                         status = dlm_send_one_join_assert(dlm, node);
831
832                         spin_lock(&dlm->spinlock);
833                         live = test_bit(node, dlm->live_nodes_map);
834                         spin_unlock(&dlm->spinlock);
835
836                         if (status) {
837                                 mlog(ML_ERROR, "Error return %d asserting "
838                                      "join on node %d\n", status, node);
839
840                                 /* give us some time between errors... */
841                                 if (live)
842                                         msleep(DLM_DOMAIN_BACKOFF_MS);
843                         }
844                 } while (status && live);
845         }
846 }
847
848 struct domain_join_ctxt {
849         unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
850         unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
851 };
852
853 static int dlm_should_restart_join(struct dlm_ctxt *dlm,
854                                    struct domain_join_ctxt *ctxt,
855                                    enum dlm_query_join_response response)
856 {
857         int ret;
858
859         if (response == JOIN_DISALLOW) {
860                 mlog(0, "Latest response of disallow -- should restart\n");
861                 return 1;
862         }
863
864         spin_lock(&dlm->spinlock);
865         /* For now, we restart the process if the node maps have
866          * changed at all */
867         ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
868                      sizeof(dlm->live_nodes_map));
869         spin_unlock(&dlm->spinlock);
870
871         if (ret)
872                 mlog(0, "Node maps changed -- should restart\n");
873
874         return ret;
875 }
876
877 static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
878 {
879         int status = 0, tmpstat, node;
880         struct domain_join_ctxt *ctxt;
881         enum dlm_query_join_response response;
882
883         mlog_entry("%p", dlm);
884
885         ctxt = kcalloc(1, sizeof(*ctxt), GFP_KERNEL);
886         if (!ctxt) {
887                 status = -ENOMEM;
888                 mlog_errno(status);
889                 goto bail;
890         }
891
892         /* group sem locking should work for us here -- we're already
893          * registered for heartbeat events so filling this should be
894          * atomic wrt getting those handlers called. */
895         o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
896
897         spin_lock(&dlm->spinlock);
898         memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
899
900         __dlm_set_joining_node(dlm, dlm->node_num);
901
902         spin_unlock(&dlm->spinlock);
903
904         node = -1;
905         while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
906                                      node + 1)) < O2NM_MAX_NODES) {
907                 if (node == dlm->node_num)
908                         continue;
909
910                 status = dlm_request_join(dlm, node, &response);
911                 if (status < 0) {
912                         mlog_errno(status);
913                         goto bail;
914                 }
915
916                 /* Ok, either we got a response or the node doesn't have a
917                  * dlm up. */
918                 if (response == JOIN_OK)
919                         set_bit(node, ctxt->yes_resp_map);
920
921                 if (dlm_should_restart_join(dlm, ctxt, response)) {
922                         status = -EAGAIN;
923                         goto bail;
924                 }
925         }
926
927         mlog(0, "Yay, done querying nodes!\n");
928
929         /* Yay, everyone agree's we can join the domain. My domain is
930          * comprised of all nodes who were put in the
931          * yes_resp_map. Copy that into our domain map and send a join
932          * assert message to clean up everyone elses state. */
933         spin_lock(&dlm->spinlock);
934         memcpy(dlm->domain_map, ctxt->yes_resp_map,
935                sizeof(ctxt->yes_resp_map));
936         set_bit(dlm->node_num, dlm->domain_map);
937         spin_unlock(&dlm->spinlock);
938
939         dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
940
941         /* Joined state *must* be set before the joining node
942          * information, otherwise the query_join handler may read no
943          * current joiner but a state of NEW and tell joining nodes
944          * we're not in the domain. */
945         spin_lock(&dlm_domain_lock);
946         dlm->dlm_state = DLM_CTXT_JOINED;
947         dlm->num_joins++;
948         spin_unlock(&dlm_domain_lock);
949
950 bail:
951         spin_lock(&dlm->spinlock);
952         __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
953         if (!status)
954                 __dlm_print_nodes(dlm);
955         spin_unlock(&dlm->spinlock);
956
957         if (ctxt) {
958                 /* Do we need to send a cancel message to any nodes? */
959                 if (status < 0) {
960                         tmpstat = dlm_send_join_cancels(dlm,
961                                                         ctxt->yes_resp_map,
962                                                         sizeof(ctxt->yes_resp_map));
963                         if (tmpstat < 0)
964                                 mlog_errno(tmpstat);
965                 }
966                 kfree(ctxt);
967         }
968
969         mlog(0, "returning %d\n", status);
970         return status;
971 }
972
973 static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
974 {
975         o2hb_unregister_callback(&dlm->dlm_hb_up);
976         o2hb_unregister_callback(&dlm->dlm_hb_down);
977         o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
978 }
979
980 static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
981 {
982         int status;
983
984         mlog(0, "registering handlers.\n");
985
986         o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
987                             dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
988         status = o2hb_register_callback(&dlm->dlm_hb_down);
989         if (status)
990                 goto bail;
991
992         o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
993                             dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
994         status = o2hb_register_callback(&dlm->dlm_hb_up);
995         if (status)
996                 goto bail;
997
998         status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
999                                         sizeof(struct dlm_master_request),
1000                                         dlm_master_request_handler,
1001                                         dlm, &dlm->dlm_domain_handlers);
1002         if (status)
1003                 goto bail;
1004
1005         status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1006                                         sizeof(struct dlm_assert_master),
1007                                         dlm_assert_master_handler,
1008                                         dlm, &dlm->dlm_domain_handlers);
1009         if (status)
1010                 goto bail;
1011
1012         status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1013                                         sizeof(struct dlm_create_lock),
1014                                         dlm_create_lock_handler,
1015                                         dlm, &dlm->dlm_domain_handlers);
1016         if (status)
1017                 goto bail;
1018
1019         status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1020                                         DLM_CONVERT_LOCK_MAX_LEN,
1021                                         dlm_convert_lock_handler,
1022                                         dlm, &dlm->dlm_domain_handlers);
1023         if (status)
1024                 goto bail;
1025
1026         status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1027                                         DLM_UNLOCK_LOCK_MAX_LEN,
1028                                         dlm_unlock_lock_handler,
1029                                         dlm, &dlm->dlm_domain_handlers);
1030         if (status)
1031                 goto bail;
1032
1033         status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1034                                         DLM_PROXY_AST_MAX_LEN,
1035                                         dlm_proxy_ast_handler,
1036                                         dlm, &dlm->dlm_domain_handlers);
1037         if (status)
1038                 goto bail;
1039
1040         status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1041                                         sizeof(struct dlm_exit_domain),
1042                                         dlm_exit_domain_handler,
1043                                         dlm, &dlm->dlm_domain_handlers);
1044         if (status)
1045                 goto bail;
1046
1047         status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1048                                         sizeof(struct dlm_migrate_request),
1049                                         dlm_migrate_request_handler,
1050                                         dlm, &dlm->dlm_domain_handlers);
1051         if (status)
1052                 goto bail;
1053
1054         status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1055                                         DLM_MIG_LOCKRES_MAX_LEN,
1056                                         dlm_mig_lockres_handler,
1057                                         dlm, &dlm->dlm_domain_handlers);
1058         if (status)
1059                 goto bail;
1060
1061         status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1062                                         sizeof(struct dlm_master_requery),
1063                                         dlm_master_requery_handler,
1064                                         dlm, &dlm->dlm_domain_handlers);
1065         if (status)
1066                 goto bail;
1067
1068         status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1069                                         sizeof(struct dlm_lock_request),
1070                                         dlm_request_all_locks_handler,
1071                                         dlm, &dlm->dlm_domain_handlers);
1072         if (status)
1073                 goto bail;
1074
1075         status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1076                                         sizeof(struct dlm_reco_data_done),
1077                                         dlm_reco_data_done_handler,
1078                                         dlm, &dlm->dlm_domain_handlers);
1079         if (status)
1080                 goto bail;
1081
1082         status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1083                                         sizeof(struct dlm_begin_reco),
1084                                         dlm_begin_reco_handler,
1085                                         dlm, &dlm->dlm_domain_handlers);
1086         if (status)
1087                 goto bail;
1088
1089         status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1090                                         sizeof(struct dlm_finalize_reco),
1091                                         dlm_finalize_reco_handler,
1092                                         dlm, &dlm->dlm_domain_handlers);
1093         if (status)
1094                 goto bail;
1095
1096 bail:
1097         if (status)
1098                 dlm_unregister_domain_handlers(dlm);
1099
1100         return status;
1101 }
1102
1103 static int dlm_join_domain(struct dlm_ctxt *dlm)
1104 {
1105         int status;
1106
1107         BUG_ON(!dlm);
1108
1109         mlog(0, "Join domain %s\n", dlm->name);
1110
1111         status = dlm_register_domain_handlers(dlm);
1112         if (status) {
1113                 mlog_errno(status);
1114                 goto bail;
1115         }
1116
1117         status = dlm_launch_thread(dlm);
1118         if (status < 0) {
1119                 mlog_errno(status);
1120                 goto bail;
1121         }
1122
1123         status = dlm_launch_recovery_thread(dlm);
1124         if (status < 0) {
1125                 mlog_errno(status);
1126                 goto bail;
1127         }
1128
1129         do {
1130                 unsigned int backoff;
1131                 status = dlm_try_to_join_domain(dlm);
1132
1133                 /* If we're racing another node to the join, then we
1134                  * need to back off temporarily and let them
1135                  * complete. */
1136                 if (status == -EAGAIN) {
1137                         if (signal_pending(current)) {
1138                                 status = -ERESTARTSYS;
1139                                 goto bail;
1140                         }
1141
1142                         /*
1143                          * <chip> After you!
1144                          * <dale> No, after you!
1145                          * <chip> I insist!
1146                          * <dale> But you first!
1147                          * ...
1148                          */
1149                         backoff = (unsigned int)(jiffies & 0x3);
1150                         backoff *= DLM_DOMAIN_BACKOFF_MS;
1151                         mlog(0, "backoff %d\n", backoff);
1152                         msleep(backoff);
1153                 }
1154         } while (status == -EAGAIN);
1155
1156         if (status < 0) {
1157                 mlog_errno(status);
1158                 goto bail;
1159         }
1160
1161         status = 0;
1162 bail:
1163         wake_up(&dlm_domain_events);
1164
1165         if (status) {
1166                 dlm_unregister_domain_handlers(dlm);
1167                 dlm_complete_thread(dlm);
1168                 dlm_complete_recovery_thread(dlm);
1169         }
1170
1171         return status;
1172 }
1173
1174 static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1175                                 u32 key)
1176 {
1177         int i;
1178         struct dlm_ctxt *dlm = NULL;
1179
1180         dlm = kcalloc(1, sizeof(*dlm), GFP_KERNEL);
1181         if (!dlm) {
1182                 mlog_errno(-ENOMEM);
1183                 goto leave;
1184         }
1185
1186         dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1187         if (dlm->name == NULL) {
1188                 mlog_errno(-ENOMEM);
1189                 kfree(dlm);
1190                 dlm = NULL;
1191                 goto leave;
1192         }
1193
1194         dlm->lockres_hash = (struct hlist_head *) __get_free_page(GFP_KERNEL);
1195         if (!dlm->lockres_hash) {
1196                 mlog_errno(-ENOMEM);
1197                 kfree(dlm->name);
1198                 kfree(dlm);
1199                 dlm = NULL;
1200                 goto leave;
1201         }
1202
1203         for (i=0; i<DLM_HASH_BUCKETS; i++)
1204                 INIT_HLIST_HEAD(&dlm->lockres_hash[i]);
1205
1206         strcpy(dlm->name, domain);
1207         dlm->key = key;
1208         dlm->node_num = o2nm_this_node();
1209
1210         spin_lock_init(&dlm->spinlock);
1211         spin_lock_init(&dlm->master_lock);
1212         spin_lock_init(&dlm->ast_lock);
1213         INIT_LIST_HEAD(&dlm->list);
1214         INIT_LIST_HEAD(&dlm->dirty_list);
1215         INIT_LIST_HEAD(&dlm->reco.resources);
1216         INIT_LIST_HEAD(&dlm->reco.received);
1217         INIT_LIST_HEAD(&dlm->reco.node_data);
1218         INIT_LIST_HEAD(&dlm->purge_list);
1219         INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
1220         dlm->reco.state = 0;
1221
1222         INIT_LIST_HEAD(&dlm->pending_asts);
1223         INIT_LIST_HEAD(&dlm->pending_basts);
1224
1225         mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1226                   dlm->recovery_map, &(dlm->recovery_map[0]));
1227
1228         memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1229         memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1230         memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1231
1232         dlm->dlm_thread_task = NULL;
1233         dlm->dlm_reco_thread_task = NULL;
1234         init_waitqueue_head(&dlm->dlm_thread_wq);
1235         init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1236         init_waitqueue_head(&dlm->reco.event);
1237         init_waitqueue_head(&dlm->ast_wq);
1238         init_waitqueue_head(&dlm->migration_wq);
1239         INIT_LIST_HEAD(&dlm->master_list);
1240         INIT_LIST_HEAD(&dlm->mle_hb_events);
1241
1242         dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1243         init_waitqueue_head(&dlm->dlm_join_events);
1244
1245         dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1246         dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
1247         atomic_set(&dlm->local_resources, 0);
1248         atomic_set(&dlm->remote_resources, 0);
1249         atomic_set(&dlm->unknown_resources, 0);
1250
1251         spin_lock_init(&dlm->work_lock);
1252         INIT_LIST_HEAD(&dlm->work_list);
1253         INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work, dlm);
1254
1255         kref_init(&dlm->dlm_refs);
1256         dlm->dlm_state = DLM_CTXT_NEW;
1257
1258         INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1259
1260         mlog(0, "context init: refcount %u\n",
1261                   atomic_read(&dlm->dlm_refs.refcount));
1262
1263 leave:
1264         return dlm;
1265 }
1266
1267 /*
1268  * dlm_register_domain: one-time setup per "domain"
1269  */
1270 struct dlm_ctxt * dlm_register_domain(const char *domain,
1271                                u32 key)
1272 {
1273         int ret;
1274         struct dlm_ctxt *dlm = NULL;
1275         struct dlm_ctxt *new_ctxt = NULL;
1276
1277         if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1278                 ret = -ENAMETOOLONG;
1279                 mlog(ML_ERROR, "domain name length too long\n");
1280                 goto leave;
1281         }
1282
1283         if (!o2hb_check_local_node_heartbeating()) {
1284                 mlog(ML_ERROR, "the local node has not been configured, or is "
1285                      "not heartbeating\n");
1286                 ret = -EPROTO;
1287                 goto leave;
1288         }
1289
1290         mlog(0, "register called for domain \"%s\"\n", domain);
1291
1292 retry:
1293         dlm = NULL;
1294         if (signal_pending(current)) {
1295                 ret = -ERESTARTSYS;
1296                 mlog_errno(ret);
1297                 goto leave;
1298         }
1299
1300         spin_lock(&dlm_domain_lock);
1301
1302         dlm = __dlm_lookup_domain(domain);
1303         if (dlm) {
1304                 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1305                         spin_unlock(&dlm_domain_lock);
1306
1307                         mlog(0, "This ctxt is not joined yet!\n");
1308                         wait_event_interruptible(dlm_domain_events,
1309                                                  dlm_wait_on_domain_helper(
1310                                                          domain));
1311                         goto retry;
1312                 }
1313
1314                 __dlm_get(dlm);
1315                 dlm->num_joins++;
1316
1317                 spin_unlock(&dlm_domain_lock);
1318
1319                 ret = 0;
1320                 goto leave;
1321         }
1322
1323         /* doesn't exist */
1324         if (!new_ctxt) {
1325                 spin_unlock(&dlm_domain_lock);
1326
1327                 new_ctxt = dlm_alloc_ctxt(domain, key);
1328                 if (new_ctxt)
1329                         goto retry;
1330
1331                 ret = -ENOMEM;
1332                 mlog_errno(ret);
1333                 goto leave;
1334         }
1335
1336         /* a little variable switch-a-roo here... */
1337         dlm = new_ctxt;
1338         new_ctxt = NULL;
1339
1340         /* add the new domain */
1341         list_add_tail(&dlm->list, &dlm_domains);
1342         spin_unlock(&dlm_domain_lock);
1343
1344         ret = dlm_join_domain(dlm);
1345         if (ret) {
1346                 mlog_errno(ret);
1347                 dlm_put(dlm);
1348                 goto leave;
1349         }
1350
1351         ret = 0;
1352 leave:
1353         if (new_ctxt)
1354                 dlm_free_ctxt_mem(new_ctxt);
1355
1356         if (ret < 0)
1357                 dlm = ERR_PTR(ret);
1358
1359         return dlm;
1360 }
1361 EXPORT_SYMBOL_GPL(dlm_register_domain);
1362
1363 static LIST_HEAD(dlm_join_handlers);
1364
1365 static void dlm_unregister_net_handlers(void)
1366 {
1367         o2net_unregister_handler_list(&dlm_join_handlers);
1368 }
1369
1370 static int dlm_register_net_handlers(void)
1371 {
1372         int status = 0;
1373
1374         status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1375                                         sizeof(struct dlm_query_join_request),
1376                                         dlm_query_join_handler,
1377                                         NULL, &dlm_join_handlers);
1378         if (status)
1379                 goto bail;
1380
1381         status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1382                                         sizeof(struct dlm_assert_joined),
1383                                         dlm_assert_joined_handler,
1384                                         NULL, &dlm_join_handlers);
1385         if (status)
1386                 goto bail;
1387
1388         status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1389                                         sizeof(struct dlm_cancel_join),
1390                                         dlm_cancel_join_handler,
1391                                         NULL, &dlm_join_handlers);
1392
1393 bail:
1394         if (status < 0)
1395                 dlm_unregister_net_handlers();
1396
1397         return status;
1398 }
1399
1400 /* Domain eviction callback handling.
1401  *
1402  * The file system requires notification of node death *before* the
1403  * dlm completes it's recovery work, otherwise it may be able to
1404  * acquire locks on resources requiring recovery. Since the dlm can
1405  * evict a node from it's domain *before* heartbeat fires, a similar
1406  * mechanism is required. */
1407
1408 /* Eviction is not expected to happen often, so a per-domain lock is
1409  * not necessary. Eviction callbacks are allowed to sleep for short
1410  * periods of time. */
1411 static DECLARE_RWSEM(dlm_callback_sem);
1412
1413 void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1414                                         int node_num)
1415 {
1416         struct list_head *iter;
1417         struct dlm_eviction_cb *cb;
1418
1419         down_read(&dlm_callback_sem);
1420         list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1421                 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1422
1423                 cb->ec_func(node_num, cb->ec_data);
1424         }
1425         up_read(&dlm_callback_sem);
1426 }
1427
1428 void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1429                            dlm_eviction_func *f,
1430                            void *data)
1431 {
1432         INIT_LIST_HEAD(&cb->ec_item);
1433         cb->ec_func = f;
1434         cb->ec_data = data;
1435 }
1436 EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1437
1438 void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1439                               struct dlm_eviction_cb *cb)
1440 {
1441         down_write(&dlm_callback_sem);
1442         list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1443         up_write(&dlm_callback_sem);
1444 }
1445 EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1446
1447 void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1448 {
1449         down_write(&dlm_callback_sem);
1450         list_del_init(&cb->ec_item);
1451         up_write(&dlm_callback_sem);
1452 }
1453 EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1454
1455 static int __init dlm_init(void)
1456 {
1457         int status;
1458
1459         dlm_print_version();
1460
1461         status = dlm_init_mle_cache();
1462         if (status)
1463                 return -1;
1464
1465         status = dlm_register_net_handlers();
1466         if (status) {
1467                 dlm_destroy_mle_cache();
1468                 return -1;
1469         }
1470
1471         return 0;
1472 }
1473
1474 static void __exit dlm_exit (void)
1475 {
1476         dlm_unregister_net_handlers();
1477         dlm_destroy_mle_cache();
1478 }
1479
1480 MODULE_AUTHOR("Oracle");
1481 MODULE_LICENSE("GPL");
1482
1483 module_init(dlm_init);
1484 module_exit(dlm_exit);