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