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