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