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