blob: 0334000676d3ad7f48765ff1fcde2e6d8e1f3a03 [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- 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>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080031#include <linux/init.h>
32#include <linux/spinlock.h>
33#include <linux/delay.h>
34#include <linux/err.h>
Sunil Mushran6325b4a2008-03-10 15:16:22 -070035#include <linux/debugfs.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080036
37#include "cluster/heartbeat.h"
38#include "cluster/nodemanager.h"
39#include "cluster/tcp.h"
40
41#include "dlmapi.h"
42#include "dlmcommon.h"
Kurt Hackel6714d8e2005-12-15 14:31:23 -080043#include "dlmdomain.h"
Sunil Mushran6325b4a2008-03-10 15:16:22 -070044#include "dlmdebug.h"
Kurt Hackel6714d8e2005-12-15 14:31:23 -080045
46#include "dlmver.h"
47
48#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
49#include "cluster/masklog.h"
50
Srinivas Eeda1faf2892007-01-29 15:31:35 -080051/*
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 */
57static inline void byte_set_bit(u8 nr, u8 map[])
58{
59 map[nr >> 3] |= (1UL << (nr & 7));
60}
61
62static inline int byte_test_bit(u8 nr, u8 map[])
63{
64 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
65}
66
67static 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
Daniel Phillips03d864c2006-03-10 18:08:16 -080081static void dlm_free_pagevec(void **vec, int pages)
82{
83 while (pages--)
84 free_page((unsigned long)vec[pages]);
85 kfree(vec);
86}
87
88static 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;
Joel Beckerc8f33b62006-03-16 17:40:37 -080099
Mark Fasheh685f1ad2006-03-23 11:23:29 -0800100 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
Mark Fashehf5a923d2006-11-28 15:13:18 -0800101 pages, (unsigned long)DLM_HASH_PAGES,
102 (unsigned long)DLM_BUCKETS_PER_PAGE);
Daniel Phillips03d864c2006-03-10 18:08:16 -0800103 return vec;
104out_free:
105 dlm_free_pagevec(vec, i);
106 return NULL;
107}
108
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800109/*
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
Ingo Molnar34af9462006-06-27 02:53:55 -0700122DEFINE_SPINLOCK(dlm_domain_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800123LIST_HEAD(dlm_domains);
124static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
125
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800126/*
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 */
132static const struct dlm_protocol_version dlm_protocol = {
133 .pv_major = 1,
134 .pv_minor = 0,
135};
136
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800137#define DLM_DOMAIN_BACKOFF_MS 200
138
Kurt Hackeld74c9802007-01-17 17:04:25 -0800139static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
140 void **ret_data);
141static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
142 void **ret_data);
143static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
144 void **ret_data);
145static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
146 void **ret_data);
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800147static int dlm_protocol_compare(struct dlm_protocol_version *existing,
148 struct dlm_protocol_version *request);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800149
150static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
151
152void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
153{
Sunil Mushran78062cb2007-03-22 17:01:07 -0700154 if (!hlist_unhashed(&lockres->hash_node)) {
155 hlist_del_init(&lockres->hash_node);
156 dlm_lockres_put(lockres);
157 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800158}
159
160void __dlm_insert_lockres(struct dlm_ctxt *dlm,
161 struct dlm_lock_resource *res)
162{
Mark Fasheh81f20942006-02-28 17:31:22 -0800163 struct hlist_head *bucket;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800164 struct qstr *q;
165
166 assert_spin_locked(&dlm->spinlock);
167
168 q = &res->lockname;
Daniel Phillips03d864c2006-03-10 18:08:16 -0800169 bucket = dlm_lockres_hash(dlm, q->hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800170
171 /* get a reference for our hashtable */
172 dlm_lockres_get(res);
173
Mark Fasheh81f20942006-02-28 17:31:22 -0800174 hlist_add_head(&res->hash_node, bucket);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800175}
176
Kurt Hackelba2bf212006-12-01 14:47:20 -0800177struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
178 const char *name,
179 unsigned int len,
180 unsigned int hash)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800181{
Mark Fasheh81f20942006-02-28 17:31:22 -0800182 struct hlist_head *bucket;
Daniel Phillips41989852006-03-10 13:31:47 -0800183 struct hlist_node *list;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800184
185 mlog_entry("%.*s\n", len, name);
186
187 assert_spin_locked(&dlm->spinlock);
188
Daniel Phillips03d864c2006-03-10 18:08:16 -0800189 bucket = dlm_lockres_hash(dlm, hash);
190
Daniel Phillips41989852006-03-10 13:31:47 -0800191 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;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800202 }
Daniel Phillips41989852006-03-10 13:31:47 -0800203 return NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800204}
205
Kurt Hackelba2bf212006-12-01 14:47:20 -0800206/* 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) */
212struct 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
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800237struct 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;
Mark Fasheha3d33292006-03-09 17:55:56 -0800242 unsigned int hash = dlm_lockid_hash(name, len);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800243
244 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -0800245 res = __dlm_lookup_lockres(dlm, name, len, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800246 spin_unlock(&dlm->spinlock);
247 return res;
248}
249
250static 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 */
271static 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" */
282static 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
299static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
300{
Sunil Mushran6325b4a2008-03-10 15:16:22 -0700301 dlm_destroy_debugfs_subroot(dlm);
302
Mark Fasheh81f20942006-02-28 17:31:22 -0800303 if (dlm->lockres_hash)
Daniel Phillips03d864c2006-03-10 18:08:16 -0800304 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800305
Sunil Mushrane2b66dd2009-02-26 15:00:40 -0800306 if (dlm->master_hash)
307 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
308
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800309 if (dlm->name)
310 kfree(dlm->name);
311
312 kfree(dlm);
313}
314
315/* A little strange - this function will be called while holding
316 * dlm_domain_lock and is expected to be holding it on the way out. We
317 * will however drop and reacquire it multiple times */
318static void dlm_ctxt_release(struct kref *kref)
319{
320 struct dlm_ctxt *dlm;
321
322 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
323
324 BUG_ON(dlm->num_joins);
325 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
326
327 /* we may still be in the list if we hit an error during join. */
328 list_del_init(&dlm->list);
329
330 spin_unlock(&dlm_domain_lock);
331
332 mlog(0, "freeing memory from domain %s\n", dlm->name);
333
334 wake_up(&dlm_domain_events);
335
336 dlm_free_ctxt_mem(dlm);
337
338 spin_lock(&dlm_domain_lock);
339}
340
341void dlm_put(struct dlm_ctxt *dlm)
342{
343 spin_lock(&dlm_domain_lock);
344 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
345 spin_unlock(&dlm_domain_lock);
346}
347
348static void __dlm_get(struct dlm_ctxt *dlm)
349{
350 kref_get(&dlm->dlm_refs);
351}
352
353/* given a questionable reference to a dlm object, gets a reference if
354 * it can find it in the list, otherwise returns NULL in which case
355 * you shouldn't trust your pointer. */
356struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
357{
358 struct list_head *iter;
359 struct dlm_ctxt *target = NULL;
360
361 spin_lock(&dlm_domain_lock);
362
363 list_for_each(iter, &dlm_domains) {
364 target = list_entry (iter, struct dlm_ctxt, list);
365
366 if (target == dlm) {
367 __dlm_get(target);
368 break;
369 }
370
371 target = NULL;
372 }
373
374 spin_unlock(&dlm_domain_lock);
375
376 return target;
377}
378
379int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
380{
381 int ret;
382
383 spin_lock(&dlm_domain_lock);
384 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
385 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
386 spin_unlock(&dlm_domain_lock);
387
388 return ret;
389}
390
Kurt Hackel3156d262006-05-01 14:39:29 -0700391static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
392{
393 if (dlm->dlm_worker) {
394 flush_workqueue(dlm->dlm_worker);
395 destroy_workqueue(dlm->dlm_worker);
396 dlm->dlm_worker = NULL;
397 }
398}
399
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800400static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
401{
402 dlm_unregister_domain_handlers(dlm);
Sunil Mushran007dce52008-03-10 15:16:23 -0700403 dlm_debug_shutdown(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800404 dlm_complete_thread(dlm);
405 dlm_complete_recovery_thread(dlm);
Kurt Hackel3156d262006-05-01 14:39:29 -0700406 dlm_destroy_dlm_worker(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800407
408 /* We've left the domain. Now we can take ourselves out of the
409 * list and allow the kref stuff to help us free the
410 * memory. */
411 spin_lock(&dlm_domain_lock);
412 list_del_init(&dlm->list);
413 spin_unlock(&dlm_domain_lock);
414
415 /* Wake up anyone waiting for us to remove this domain */
416 wake_up(&dlm_domain_events);
417}
418
Kurt Hackelba2bf212006-12-01 14:47:20 -0800419static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800420{
Kurt Hackelba2bf212006-12-01 14:47:20 -0800421 int i, num, n, ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800422 struct dlm_lock_resource *res;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800423 struct hlist_node *iter;
424 struct hlist_head *bucket;
425 int dropped;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800426
427 mlog(0, "Migrating locks from domain %s\n", dlm->name);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800428
429 num = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800430 spin_lock(&dlm->spinlock);
Mark Fasheh81f20942006-02-28 17:31:22 -0800431 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800432redo_bucket:
433 n = 0;
434 bucket = dlm_lockres_hash(dlm, i);
435 iter = bucket->first;
436 while (iter) {
437 n++;
438 res = hlist_entry(iter, struct dlm_lock_resource,
439 hash_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800440 dlm_lockres_get(res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800441 /* migrate, if necessary. this will drop the dlm
442 * spinlock and retake it if it does migration. */
443 dropped = dlm_empty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800444
Kurt Hackelba2bf212006-12-01 14:47:20 -0800445 spin_lock(&res->spinlock);
446 __dlm_lockres_calc_usage(dlm, res);
447 iter = res->hash_node.next;
448 spin_unlock(&res->spinlock);
449
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800450 dlm_lockres_put(res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800451
Kurt Hackelba2bf212006-12-01 14:47:20 -0800452 if (dropped)
453 goto redo_bucket;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800454 }
Sunil Mushran0d01af62007-04-17 13:32:20 -0700455 cond_resched_lock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800456 num += n;
457 mlog(0, "%s: touched %d lockreses in bucket %d "
458 "(tot=%d)\n", dlm->name, n, i, num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800459 }
460 spin_unlock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800461 wake_up(&dlm->dlm_thread_wq);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800462
Kurt Hackelba2bf212006-12-01 14:47:20 -0800463 /* let the dlm thread take care of purging, keep scanning until
464 * nothing remains in the hash */
465 if (num) {
466 mlog(0, "%s: %d lock resources in hash last pass\n",
467 dlm->name, num);
468 ret = -EAGAIN;
469 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800470 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800471 return ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800472}
473
474static int dlm_no_joining_node(struct dlm_ctxt *dlm)
475{
476 int ret;
477
478 spin_lock(&dlm->spinlock);
479 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
480 spin_unlock(&dlm->spinlock);
481
482 return ret;
483}
484
485static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
486{
487 /* Yikes, a double spinlock! I need domain_lock for the dlm
488 * state and the dlm spinlock for join state... Sorry! */
489again:
490 spin_lock(&dlm_domain_lock);
491 spin_lock(&dlm->spinlock);
492
493 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
494 mlog(0, "Node %d is joining, we wait on it.\n",
495 dlm->joining_node);
496 spin_unlock(&dlm->spinlock);
497 spin_unlock(&dlm_domain_lock);
498
499 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
500 goto again;
501 }
502
503 dlm->dlm_state = DLM_CTXT_LEAVING;
504 spin_unlock(&dlm->spinlock);
505 spin_unlock(&dlm_domain_lock);
506}
507
508static void __dlm_print_nodes(struct dlm_ctxt *dlm)
509{
510 int node = -1;
511
512 assert_spin_locked(&dlm->spinlock);
513
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700514 printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800515
516 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
517 node + 1)) < O2NM_MAX_NODES) {
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700518 printk("%d ", node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800519 }
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700520 printk("\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800521}
522
Kurt Hackeld74c9802007-01-17 17:04:25 -0800523static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
524 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800525{
526 struct dlm_ctxt *dlm = data;
527 unsigned int node;
528 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
529
530 mlog_entry("%p %u %p", msg, len, data);
531
532 if (!dlm_grab(dlm))
533 return 0;
534
535 node = exit_msg->node_idx;
536
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700537 printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800538
539 spin_lock(&dlm->spinlock);
540 clear_bit(node, dlm->domain_map);
541 __dlm_print_nodes(dlm);
542
543 /* notify anything attached to the heartbeat events */
544 dlm_hb_event_notify_attached(dlm, node, 0);
545
546 spin_unlock(&dlm->spinlock);
547
548 dlm_put(dlm);
549
550 return 0;
551}
552
553static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
554 unsigned int node)
555{
556 int status;
557 struct dlm_exit_domain leave_msg;
558
559 mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
560 node, dlm->name, dlm->node_num);
561
562 memset(&leave_msg, 0, sizeof(leave_msg));
563 leave_msg.node_idx = dlm->node_num;
564
565 status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
566 &leave_msg, sizeof(leave_msg), node,
567 NULL);
568
569 mlog(0, "status return %d from o2net_send_message\n", status);
570
571 return status;
572}
573
574
575static void dlm_leave_domain(struct dlm_ctxt *dlm)
576{
577 int node, clear_node, status;
578
579 /* At this point we've migrated away all our locks and won't
580 * accept mastership of new ones. The dlm is responsible for
581 * almost nothing now. We make sure not to confuse any joining
582 * nodes and then commence shutdown procedure. */
583
584 spin_lock(&dlm->spinlock);
585 /* Clear ourselves from the domain map */
586 clear_bit(dlm->node_num, dlm->domain_map);
587 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
588 0)) < O2NM_MAX_NODES) {
589 /* Drop the dlm spinlock. This is safe wrt the domain_map.
590 * -nodes cannot be added now as the
591 * query_join_handlers knows to respond with OK_NO_MAP
592 * -we catch the right network errors if a node is
593 * removed from the map while we're sending him the
594 * exit message. */
595 spin_unlock(&dlm->spinlock);
596
597 clear_node = 1;
598
599 status = dlm_send_one_domain_exit(dlm, node);
600 if (status < 0 &&
601 status != -ENOPROTOOPT &&
602 status != -ENOTCONN) {
603 mlog(ML_NOTICE, "Error %d sending domain exit message "
604 "to node %d\n", status, node);
605
606 /* Not sure what to do here but lets sleep for
607 * a bit in case this was a transient
608 * error... */
609 msleep(DLM_DOMAIN_BACKOFF_MS);
610 clear_node = 0;
611 }
612
613 spin_lock(&dlm->spinlock);
614 /* If we're not clearing the node bit then we intend
615 * to loop back around to try again. */
616 if (clear_node)
617 clear_bit(node, dlm->domain_map);
618 }
619 spin_unlock(&dlm->spinlock);
620}
621
622int dlm_joined(struct dlm_ctxt *dlm)
623{
624 int ret = 0;
625
626 spin_lock(&dlm_domain_lock);
627
628 if (dlm->dlm_state == DLM_CTXT_JOINED)
629 ret = 1;
630
631 spin_unlock(&dlm_domain_lock);
632
633 return ret;
634}
635
636int dlm_shutting_down(struct dlm_ctxt *dlm)
637{
638 int ret = 0;
639
640 spin_lock(&dlm_domain_lock);
641
642 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
643 ret = 1;
644
645 spin_unlock(&dlm_domain_lock);
646
647 return ret;
648}
649
650void dlm_unregister_domain(struct dlm_ctxt *dlm)
651{
652 int leave = 0;
Sunil Mushran29576f82008-03-10 15:16:21 -0700653 struct dlm_lock_resource *res;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800654
655 spin_lock(&dlm_domain_lock);
656 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
657 BUG_ON(!dlm->num_joins);
658
659 dlm->num_joins--;
660 if (!dlm->num_joins) {
661 /* We mark it "in shutdown" now so new register
662 * requests wait until we've completely left the
663 * domain. Don't use DLM_CTXT_LEAVING yet as we still
664 * want new domain joins to communicate with us at
665 * least until we've completed migration of our
666 * resources. */
667 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
668 leave = 1;
669 }
670 spin_unlock(&dlm_domain_lock);
671
672 if (leave) {
673 mlog(0, "shutting down domain %s\n", dlm->name);
674
675 /* We changed dlm state, notify the thread */
676 dlm_kick_thread(dlm, NULL);
677
Kurt Hackelba2bf212006-12-01 14:47:20 -0800678 while (dlm_migrate_all_locks(dlm)) {
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -0700679 /* Give dlm_thread time to purge the lockres' */
680 msleep(500);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800681 mlog(0, "%s: more migration to do\n", dlm->name);
682 }
Sunil Mushran29576f82008-03-10 15:16:21 -0700683
684 /* This list should be empty. If not, print remaining lockres */
685 if (!list_empty(&dlm->tracking_list)) {
686 mlog(ML_ERROR, "Following lockres' are still on the "
687 "tracking list:\n");
688 list_for_each_entry(res, &dlm->tracking_list, tracking)
689 dlm_print_one_lock_resource(res);
690 }
691
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800692 dlm_mark_domain_leaving(dlm);
693 dlm_leave_domain(dlm);
694 dlm_complete_dlm_shutdown(dlm);
695 }
696 dlm_put(dlm);
697}
698EXPORT_SYMBOL_GPL(dlm_unregister_domain);
699
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800700static int dlm_query_join_proto_check(char *proto_type, int node,
701 struct dlm_protocol_version *ours,
702 struct dlm_protocol_version *request)
703{
704 int rc;
705 struct dlm_protocol_version proto = *request;
706
707 if (!dlm_protocol_compare(ours, &proto)) {
708 mlog(0,
709 "node %u wanted to join with %s locking protocol "
710 "%u.%u, we respond with %u.%u\n",
711 node, proto_type,
712 request->pv_major,
713 request->pv_minor,
714 proto.pv_major, proto.pv_minor);
715 request->pv_minor = proto.pv_minor;
716 rc = 0;
717 } else {
718 mlog(ML_NOTICE,
719 "Node %u wanted to join with %s locking "
720 "protocol %u.%u, but we have %u.%u, disallowing\n",
721 node, proto_type,
722 request->pv_major,
723 request->pv_minor,
724 ours->pv_major,
725 ours->pv_minor);
726 rc = 1;
727 }
728
729 return rc;
730}
731
Joel Becker0f71b7b2008-02-12 14:56:25 -0800732/*
733 * struct dlm_query_join_packet is made up of four one-byte fields. They
734 * are effectively in big-endian order already. However, little-endian
735 * machines swap them before putting the packet on the wire (because
736 * query_join's response is a status, and that status is treated as a u32
737 * on the wire). Thus, a big-endian and little-endian machines will treat
738 * this structure differently.
739 *
740 * The solution is to have little-endian machines swap the structure when
741 * converting from the structure to the u32 representation. This will
742 * result in the structure having the correct format on the wire no matter
743 * the host endian format.
744 */
745static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
746 u32 *wire)
747{
748 union dlm_query_join_response response;
749
750 response.packet = *packet;
751 *wire = cpu_to_be32(response.intval);
752}
753
754static void dlm_query_join_wire_to_packet(u32 wire,
755 struct dlm_query_join_packet *packet)
756{
757 union dlm_query_join_response response;
758
759 response.intval = cpu_to_be32(wire);
760 *packet = response.packet;
761}
762
Kurt Hackeld74c9802007-01-17 17:04:25 -0800763static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
764 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800765{
766 struct dlm_query_join_request *query;
Joel Becker0f71b7b2008-02-12 14:56:25 -0800767 struct dlm_query_join_packet packet = {
768 .code = JOIN_DISALLOW,
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800769 };
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800770 struct dlm_ctxt *dlm = NULL;
Joel Becker0f71b7b2008-02-12 14:56:25 -0800771 u32 response;
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800772 u8 nodenum;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800773
774 query = (struct dlm_query_join_request *) msg->buf;
775
776 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
777 query->domain);
778
779 /*
780 * If heartbeat doesn't consider the node live, tell it
781 * to back off and try again. This gives heartbeat a chance
782 * to catch up.
783 */
784 if (!o2hb_check_node_heartbeating(query->node_idx)) {
785 mlog(0, "node %u is not in our live map yet\n",
786 query->node_idx);
787
Joel Becker0f71b7b2008-02-12 14:56:25 -0800788 packet.code = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800789 goto respond;
790 }
791
Joel Becker0f71b7b2008-02-12 14:56:25 -0800792 packet.code = JOIN_OK_NO_MAP;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800793
794 spin_lock(&dlm_domain_lock);
795 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800796 if (!dlm)
797 goto unlock_respond;
798
799 /*
800 * There is a small window where the joining node may not see the
801 * node(s) that just left but still part of the cluster. DISALLOW
802 * join request if joining node has different node map.
803 */
804 nodenum=0;
805 while (nodenum < O2NM_MAX_NODES) {
806 if (test_bit(nodenum, dlm->domain_map)) {
807 if (!byte_test_bit(nodenum, query->node_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800808 mlog(0, "disallow join as node %u does not "
809 "have node %u in its nodemap\n",
810 query->node_idx, nodenum);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800811 packet.code = JOIN_DISALLOW;
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800812 goto unlock_respond;
813 }
814 }
815 nodenum++;
816 }
817
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800818 /* Once the dlm ctxt is marked as leaving then we don't want
Kurt Hackele2faea42006-01-12 14:24:55 -0800819 * to be put in someone's domain map.
820 * Also, explicitly disallow joining at certain troublesome
821 * times (ie. during recovery). */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800822 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
Kurt Hackele2faea42006-01-12 14:24:55 -0800823 int bit = query->node_idx;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800824 spin_lock(&dlm->spinlock);
825
826 if (dlm->dlm_state == DLM_CTXT_NEW &&
827 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
828 /*If this is a brand new context and we
829 * haven't started our join process yet, then
830 * the other node won the race. */
Joel Becker0f71b7b2008-02-12 14:56:25 -0800831 packet.code = JOIN_OK_NO_MAP;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800832 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
833 /* Disallow parallel joins. */
Joel Becker0f71b7b2008-02-12 14:56:25 -0800834 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800835 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800836 mlog(0, "node %u trying to join, but recovery "
Kurt Hackele2faea42006-01-12 14:24:55 -0800837 "is ongoing.\n", bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800838 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800839 } else if (test_bit(bit, dlm->recovery_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800840 mlog(0, "node %u trying to join, but it "
Kurt Hackele2faea42006-01-12 14:24:55 -0800841 "still needs recovery.\n", bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800842 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800843 } else if (test_bit(bit, dlm->domain_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800844 mlog(0, "node %u trying to join, but it "
Kurt Hackele2faea42006-01-12 14:24:55 -0800845 "is still in the domain! needs recovery?\n",
846 bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800847 packet.code = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800848 } else {
849 /* Alright we're fully a part of this domain
850 * so we keep some state as to who's joining
851 * and indicate to him that needs to be fixed
852 * up. */
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800853
854 /* Make sure we speak compatible locking protocols. */
855 if (dlm_query_join_proto_check("DLM", bit,
856 &dlm->dlm_locking_proto,
857 &query->dlm_proto)) {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800858 packet.code = JOIN_PROTOCOL_MISMATCH;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800859 } else if (dlm_query_join_proto_check("fs", bit,
860 &dlm->fs_locking_proto,
861 &query->fs_proto)) {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800862 packet.code = JOIN_PROTOCOL_MISMATCH;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800863 } else {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800864 packet.dlm_minor = query->dlm_proto.pv_minor;
865 packet.fs_minor = query->fs_proto.pv_minor;
866 packet.code = JOIN_OK;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800867 __dlm_set_joining_node(dlm, query->node_idx);
868 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800869 }
870
871 spin_unlock(&dlm->spinlock);
872 }
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800873unlock_respond:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800874 spin_unlock(&dlm_domain_lock);
875
876respond:
Joel Becker0f71b7b2008-02-12 14:56:25 -0800877 mlog(0, "We respond with %u\n", packet.code);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800878
Joel Becker0f71b7b2008-02-12 14:56:25 -0800879 dlm_query_join_packet_to_wire(&packet, &response);
880 return response;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800881}
882
Kurt Hackeld74c9802007-01-17 17:04:25 -0800883static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
884 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800885{
886 struct dlm_assert_joined *assert;
887 struct dlm_ctxt *dlm = NULL;
888
889 assert = (struct dlm_assert_joined *) msg->buf;
890
891 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
892 assert->domain);
893
894 spin_lock(&dlm_domain_lock);
895 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
896 /* XXX should we consider no dlm ctxt an error? */
897 if (dlm) {
898 spin_lock(&dlm->spinlock);
899
900 /* Alright, this node has officially joined our
901 * domain. Set him in the map and clean up our
902 * leftover join state. */
903 BUG_ON(dlm->joining_node != assert->node_idx);
904 set_bit(assert->node_idx, dlm->domain_map);
905 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
906
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700907 printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
908 assert->node_idx, dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800909 __dlm_print_nodes(dlm);
910
911 /* notify anything attached to the heartbeat events */
912 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
913
914 spin_unlock(&dlm->spinlock);
915 }
916 spin_unlock(&dlm_domain_lock);
917
918 return 0;
919}
920
Kurt Hackeld74c9802007-01-17 17:04:25 -0800921static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
922 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800923{
924 struct dlm_cancel_join *cancel;
925 struct dlm_ctxt *dlm = NULL;
926
927 cancel = (struct dlm_cancel_join *) msg->buf;
928
929 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
930 cancel->domain);
931
932 spin_lock(&dlm_domain_lock);
933 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
934
935 if (dlm) {
936 spin_lock(&dlm->spinlock);
937
938 /* Yikes, this guy wants to cancel his join. No
939 * problem, we simply cleanup our join state. */
940 BUG_ON(dlm->joining_node != cancel->node_idx);
941 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
942
943 spin_unlock(&dlm->spinlock);
944 }
945 spin_unlock(&dlm_domain_lock);
946
947 return 0;
948}
949
950static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
951 unsigned int node)
952{
953 int status;
954 struct dlm_cancel_join cancel_msg;
955
956 memset(&cancel_msg, 0, sizeof(cancel_msg));
957 cancel_msg.node_idx = dlm->node_num;
958 cancel_msg.name_len = strlen(dlm->name);
959 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
960
961 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
962 &cancel_msg, sizeof(cancel_msg), node,
963 NULL);
964 if (status < 0) {
965 mlog_errno(status);
966 goto bail;
967 }
968
969bail:
970 return status;
971}
972
973/* map_size should be in bytes. */
974static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
975 unsigned long *node_map,
976 unsigned int map_size)
977{
978 int status, tmpstat;
979 unsigned int node;
980
981 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
982 sizeof(unsigned long))) {
983 mlog(ML_ERROR,
984 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
Andrew Morton3a4780a2008-02-29 01:56:06 -0800985 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800986 return -EINVAL;
987 }
988
989 status = 0;
990 node = -1;
991 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
992 node + 1)) < O2NM_MAX_NODES) {
993 if (node == dlm->node_num)
994 continue;
995
996 tmpstat = dlm_send_one_join_cancel(dlm, node);
997 if (tmpstat) {
998 mlog(ML_ERROR, "Error return %d cancelling join on "
999 "node %d\n", tmpstat, node);
1000 if (!status)
1001 status = tmpstat;
1002 }
1003 }
1004
1005 if (status)
1006 mlog_errno(status);
1007 return status;
1008}
1009
1010static int dlm_request_join(struct dlm_ctxt *dlm,
1011 int node,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001012 enum dlm_query_join_response_code *response)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001013{
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001014 int status;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001015 struct dlm_query_join_request join_msg;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001016 struct dlm_query_join_packet packet;
1017 u32 join_resp;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001018
1019 mlog(0, "querying node %d\n", node);
1020
1021 memset(&join_msg, 0, sizeof(join_msg));
1022 join_msg.node_idx = dlm->node_num;
1023 join_msg.name_len = strlen(dlm->name);
1024 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001025 join_msg.dlm_proto = dlm->dlm_locking_proto;
1026 join_msg.fs_proto = dlm->fs_locking_proto;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001027
Srinivas Eeda1faf2892007-01-29 15:31:35 -08001028 /* copy live node map to join message */
1029 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1030
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001031 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001032 sizeof(join_msg), node,
Joel Becker0f71b7b2008-02-12 14:56:25 -08001033 &join_resp);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001034 if (status < 0 && status != -ENOPROTOOPT) {
1035 mlog_errno(status);
1036 goto bail;
1037 }
Joel Becker0f71b7b2008-02-12 14:56:25 -08001038 dlm_query_join_wire_to_packet(join_resp, &packet);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001039
1040 /* -ENOPROTOOPT from the net code means the other side isn't
1041 listening for our message type -- that's fine, it means
1042 his dlm isn't up, so we can consider him a 'yes' but not
1043 joined into the domain. */
1044 if (status == -ENOPROTOOPT) {
1045 status = 0;
1046 *response = JOIN_OK_NO_MAP;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001047 } else if (packet.code == JOIN_DISALLOW ||
1048 packet.code == JOIN_OK_NO_MAP) {
1049 *response = packet.code;
1050 } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001051 mlog(ML_NOTICE,
1052 "This node requested DLM locking protocol %u.%u and "
1053 "filesystem locking protocol %u.%u. At least one of "
1054 "the protocol versions on node %d is not compatible, "
1055 "disconnecting\n",
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 node);
1061 status = -EPROTO;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001062 *response = packet.code;
1063 } else if (packet.code == JOIN_OK) {
1064 *response = packet.code;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001065 /* Use the same locking protocol as the remote node */
Joel Becker0f71b7b2008-02-12 14:56:25 -08001066 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1067 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001068 mlog(0,
1069 "Node %d responds JOIN_OK with DLM locking protocol "
1070 "%u.%u and fs locking protocol %u.%u\n",
1071 node,
1072 dlm->dlm_locking_proto.pv_major,
1073 dlm->dlm_locking_proto.pv_minor,
1074 dlm->fs_locking_proto.pv_major,
1075 dlm->fs_locking_proto.pv_minor);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001076 } else {
1077 status = -EINVAL;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001078 mlog(ML_ERROR, "invalid response %d from node %u\n",
Joel Becker0f71b7b2008-02-12 14:56:25 -08001079 packet.code, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001080 }
1081
1082 mlog(0, "status %d, node %d response is %d\n", status, node,
Joel Becker0f71b7b2008-02-12 14:56:25 -08001083 *response);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001084
1085bail:
1086 return status;
1087}
1088
1089static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1090 unsigned int node)
1091{
1092 int status;
1093 struct dlm_assert_joined assert_msg;
1094
1095 mlog(0, "Sending join assert to node %u\n", node);
1096
1097 memset(&assert_msg, 0, sizeof(assert_msg));
1098 assert_msg.node_idx = dlm->node_num;
1099 assert_msg.name_len = strlen(dlm->name);
1100 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1101
1102 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1103 &assert_msg, sizeof(assert_msg), node,
1104 NULL);
1105 if (status < 0)
1106 mlog_errno(status);
1107
1108 return status;
1109}
1110
1111static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1112 unsigned long *node_map)
1113{
1114 int status, node, live;
1115
1116 status = 0;
1117 node = -1;
1118 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1119 node + 1)) < O2NM_MAX_NODES) {
1120 if (node == dlm->node_num)
1121 continue;
1122
1123 do {
1124 /* It is very important that this message be
1125 * received so we spin until either the node
1126 * has died or it gets the message. */
1127 status = dlm_send_one_join_assert(dlm, node);
1128
1129 spin_lock(&dlm->spinlock);
1130 live = test_bit(node, dlm->live_nodes_map);
1131 spin_unlock(&dlm->spinlock);
1132
1133 if (status) {
1134 mlog(ML_ERROR, "Error return %d asserting "
1135 "join on node %d\n", status, node);
1136
1137 /* give us some time between errors... */
1138 if (live)
1139 msleep(DLM_DOMAIN_BACKOFF_MS);
1140 }
1141 } while (status && live);
1142 }
1143}
1144
1145struct domain_join_ctxt {
1146 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1147 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1148};
1149
1150static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1151 struct domain_join_ctxt *ctxt,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001152 enum dlm_query_join_response_code response)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001153{
1154 int ret;
1155
1156 if (response == JOIN_DISALLOW) {
1157 mlog(0, "Latest response of disallow -- should restart\n");
1158 return 1;
1159 }
1160
1161 spin_lock(&dlm->spinlock);
1162 /* For now, we restart the process if the node maps have
1163 * changed at all */
1164 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1165 sizeof(dlm->live_nodes_map));
1166 spin_unlock(&dlm->spinlock);
1167
1168 if (ret)
1169 mlog(0, "Node maps changed -- should restart\n");
1170
1171 return ret;
1172}
1173
1174static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1175{
1176 int status = 0, tmpstat, node;
1177 struct domain_join_ctxt *ctxt;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001178 enum dlm_query_join_response_code response = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001179
1180 mlog_entry("%p", dlm);
1181
Robert P. J. Daycd861282006-12-13 00:34:52 -08001182 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001183 if (!ctxt) {
1184 status = -ENOMEM;
1185 mlog_errno(status);
1186 goto bail;
1187 }
1188
1189 /* group sem locking should work for us here -- we're already
1190 * registered for heartbeat events so filling this should be
1191 * atomic wrt getting those handlers called. */
1192 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1193
1194 spin_lock(&dlm->spinlock);
1195 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1196
1197 __dlm_set_joining_node(dlm, dlm->node_num);
1198
1199 spin_unlock(&dlm->spinlock);
1200
1201 node = -1;
1202 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1203 node + 1)) < O2NM_MAX_NODES) {
1204 if (node == dlm->node_num)
1205 continue;
1206
1207 status = dlm_request_join(dlm, node, &response);
1208 if (status < 0) {
1209 mlog_errno(status);
1210 goto bail;
1211 }
1212
1213 /* Ok, either we got a response or the node doesn't have a
1214 * dlm up. */
1215 if (response == JOIN_OK)
1216 set_bit(node, ctxt->yes_resp_map);
1217
1218 if (dlm_should_restart_join(dlm, ctxt, response)) {
1219 status = -EAGAIN;
1220 goto bail;
1221 }
1222 }
1223
1224 mlog(0, "Yay, done querying nodes!\n");
1225
1226 /* Yay, everyone agree's we can join the domain. My domain is
1227 * comprised of all nodes who were put in the
1228 * yes_resp_map. Copy that into our domain map and send a join
1229 * assert message to clean up everyone elses state. */
1230 spin_lock(&dlm->spinlock);
1231 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1232 sizeof(ctxt->yes_resp_map));
1233 set_bit(dlm->node_num, dlm->domain_map);
1234 spin_unlock(&dlm->spinlock);
1235
1236 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1237
1238 /* Joined state *must* be set before the joining node
1239 * information, otherwise the query_join handler may read no
1240 * current joiner but a state of NEW and tell joining nodes
1241 * we're not in the domain. */
1242 spin_lock(&dlm_domain_lock);
1243 dlm->dlm_state = DLM_CTXT_JOINED;
1244 dlm->num_joins++;
1245 spin_unlock(&dlm_domain_lock);
1246
1247bail:
1248 spin_lock(&dlm->spinlock);
1249 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1250 if (!status)
1251 __dlm_print_nodes(dlm);
1252 spin_unlock(&dlm->spinlock);
1253
1254 if (ctxt) {
1255 /* Do we need to send a cancel message to any nodes? */
1256 if (status < 0) {
1257 tmpstat = dlm_send_join_cancels(dlm,
1258 ctxt->yes_resp_map,
1259 sizeof(ctxt->yes_resp_map));
1260 if (tmpstat < 0)
1261 mlog_errno(tmpstat);
1262 }
1263 kfree(ctxt);
1264 }
1265
1266 mlog(0, "returning %d\n", status);
1267 return status;
1268}
1269
1270static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1271{
Joel Becker14829422007-06-14 21:40:49 -07001272 o2hb_unregister_callback(NULL, &dlm->dlm_hb_up);
1273 o2hb_unregister_callback(NULL, &dlm->dlm_hb_down);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001274 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1275}
1276
1277static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1278{
1279 int status;
1280
1281 mlog(0, "registering handlers.\n");
1282
1283 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1284 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
Joel Becker14829422007-06-14 21:40:49 -07001285 status = o2hb_register_callback(NULL, &dlm->dlm_hb_down);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001286 if (status)
1287 goto bail;
1288
1289 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1290 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
Joel Becker14829422007-06-14 21:40:49 -07001291 status = o2hb_register_callback(NULL, &dlm->dlm_hb_up);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001292 if (status)
1293 goto bail;
1294
1295 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1296 sizeof(struct dlm_master_request),
1297 dlm_master_request_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001298 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001299 if (status)
1300 goto bail;
1301
1302 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1303 sizeof(struct dlm_assert_master),
1304 dlm_assert_master_handler,
Kurt Hackel3b8118c2007-01-17 17:05:53 -08001305 dlm, dlm_assert_master_post_handler,
1306 &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001307 if (status)
1308 goto bail;
1309
1310 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1311 sizeof(struct dlm_create_lock),
1312 dlm_create_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001313 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001314 if (status)
1315 goto bail;
1316
1317 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1318 DLM_CONVERT_LOCK_MAX_LEN,
1319 dlm_convert_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001320 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001321 if (status)
1322 goto bail;
1323
1324 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1325 DLM_UNLOCK_LOCK_MAX_LEN,
1326 dlm_unlock_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001327 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001328 if (status)
1329 goto bail;
1330
1331 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1332 DLM_PROXY_AST_MAX_LEN,
1333 dlm_proxy_ast_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001334 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001335 if (status)
1336 goto bail;
1337
1338 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1339 sizeof(struct dlm_exit_domain),
1340 dlm_exit_domain_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001341 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001342 if (status)
1343 goto bail;
1344
Kurt Hackelba2bf212006-12-01 14:47:20 -08001345 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1346 sizeof(struct dlm_deref_lockres),
1347 dlm_deref_lockres_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001348 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001349 if (status)
1350 goto bail;
1351
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001352 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1353 sizeof(struct dlm_migrate_request),
1354 dlm_migrate_request_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001355 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001356 if (status)
1357 goto bail;
1358
1359 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1360 DLM_MIG_LOCKRES_MAX_LEN,
1361 dlm_mig_lockres_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001362 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001363 if (status)
1364 goto bail;
1365
1366 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1367 sizeof(struct dlm_master_requery),
1368 dlm_master_requery_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001369 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001370 if (status)
1371 goto bail;
1372
1373 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1374 sizeof(struct dlm_lock_request),
1375 dlm_request_all_locks_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001376 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001377 if (status)
1378 goto bail;
1379
1380 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1381 sizeof(struct dlm_reco_data_done),
1382 dlm_reco_data_done_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001383 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001384 if (status)
1385 goto bail;
1386
1387 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1388 sizeof(struct dlm_begin_reco),
1389 dlm_begin_reco_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001390 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001391 if (status)
1392 goto bail;
1393
1394 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1395 sizeof(struct dlm_finalize_reco),
1396 dlm_finalize_reco_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001397 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001398 if (status)
1399 goto bail;
1400
1401bail:
1402 if (status)
1403 dlm_unregister_domain_handlers(dlm);
1404
1405 return status;
1406}
1407
1408static int dlm_join_domain(struct dlm_ctxt *dlm)
1409{
1410 int status;
Sunil Mushran0dd82142007-01-29 15:44:27 -08001411 unsigned int backoff;
1412 unsigned int total_backoff = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001413
1414 BUG_ON(!dlm);
1415
1416 mlog(0, "Join domain %s\n", dlm->name);
1417
1418 status = dlm_register_domain_handlers(dlm);
1419 if (status) {
1420 mlog_errno(status);
1421 goto bail;
1422 }
1423
Sunil Mushran007dce52008-03-10 15:16:23 -07001424 status = dlm_debug_init(dlm);
1425 if (status < 0) {
1426 mlog_errno(status);
1427 goto bail;
1428 }
1429
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001430 status = dlm_launch_thread(dlm);
1431 if (status < 0) {
1432 mlog_errno(status);
1433 goto bail;
1434 }
1435
1436 status = dlm_launch_recovery_thread(dlm);
1437 if (status < 0) {
1438 mlog_errno(status);
1439 goto bail;
1440 }
1441
Kurt Hackel3156d262006-05-01 14:39:29 -07001442 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1443 if (!dlm->dlm_worker) {
1444 status = -ENOMEM;
1445 mlog_errno(status);
1446 goto bail;
1447 }
1448
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001449 do {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001450 status = dlm_try_to_join_domain(dlm);
1451
1452 /* If we're racing another node to the join, then we
1453 * need to back off temporarily and let them
1454 * complete. */
Sunil Mushran0dd82142007-01-29 15:44:27 -08001455#define DLM_JOIN_TIMEOUT_MSECS 90000
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001456 if (status == -EAGAIN) {
1457 if (signal_pending(current)) {
1458 status = -ERESTARTSYS;
1459 goto bail;
1460 }
1461
Sunil Mushran0dd82142007-01-29 15:44:27 -08001462 if (total_backoff >
1463 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1464 status = -ERESTARTSYS;
1465 mlog(ML_NOTICE, "Timed out joining dlm domain "
1466 "%s after %u msecs\n", dlm->name,
1467 jiffies_to_msecs(total_backoff));
1468 goto bail;
1469 }
1470
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001471 /*
1472 * <chip> After you!
1473 * <dale> No, after you!
1474 * <chip> I insist!
1475 * <dale> But you first!
1476 * ...
1477 */
1478 backoff = (unsigned int)(jiffies & 0x3);
1479 backoff *= DLM_DOMAIN_BACKOFF_MS;
Sunil Mushran0dd82142007-01-29 15:44:27 -08001480 total_backoff += backoff;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001481 mlog(0, "backoff %d\n", backoff);
1482 msleep(backoff);
1483 }
1484 } while (status == -EAGAIN);
1485
1486 if (status < 0) {
1487 mlog_errno(status);
1488 goto bail;
1489 }
1490
1491 status = 0;
1492bail:
1493 wake_up(&dlm_domain_events);
1494
1495 if (status) {
1496 dlm_unregister_domain_handlers(dlm);
Sunil Mushran007dce52008-03-10 15:16:23 -07001497 dlm_debug_shutdown(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001498 dlm_complete_thread(dlm);
1499 dlm_complete_recovery_thread(dlm);
Kurt Hackel3156d262006-05-01 14:39:29 -07001500 dlm_destroy_dlm_worker(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001501 }
1502
1503 return status;
1504}
1505
1506static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1507 u32 key)
1508{
1509 int i;
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001510 int ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001511 struct dlm_ctxt *dlm = NULL;
1512
Robert P. J. Daycd861282006-12-13 00:34:52 -08001513 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001514 if (!dlm) {
1515 mlog_errno(-ENOMEM);
1516 goto leave;
1517 }
1518
1519 dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
1520 if (dlm->name == NULL) {
1521 mlog_errno(-ENOMEM);
1522 kfree(dlm);
1523 dlm = NULL;
1524 goto leave;
1525 }
1526
Daniel Phillips03d864c2006-03-10 18:08:16 -08001527 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
Mark Fasheh81f20942006-02-28 17:31:22 -08001528 if (!dlm->lockres_hash) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001529 mlog_errno(-ENOMEM);
1530 kfree(dlm->name);
1531 kfree(dlm);
1532 dlm = NULL;
1533 goto leave;
1534 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001535
Daniel Phillips03d864c2006-03-10 18:08:16 -08001536 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1537 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001538
Sunil Mushrane2b66dd2009-02-26 15:00:40 -08001539 dlm->master_hash = (struct hlist_head **)
1540 dlm_alloc_pagevec(DLM_HASH_PAGES);
1541 if (!dlm->master_hash) {
1542 mlog_errno(-ENOMEM);
1543 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1544 kfree(dlm->name);
1545 kfree(dlm);
1546 dlm = NULL;
1547 goto leave;
1548 }
1549
1550 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1551 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
1552
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001553 strcpy(dlm->name, domain);
1554 dlm->key = key;
1555 dlm->node_num = o2nm_this_node();
1556
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001557 ret = dlm_create_debugfs_subroot(dlm);
1558 if (ret < 0) {
Sunil Mushrane2b66dd2009-02-26 15:00:40 -08001559 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001560 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
1561 kfree(dlm->name);
1562 kfree(dlm);
1563 dlm = NULL;
1564 goto leave;
1565 }
1566
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001567 spin_lock_init(&dlm->spinlock);
1568 spin_lock_init(&dlm->master_lock);
1569 spin_lock_init(&dlm->ast_lock);
Sunil Mushranb0d4f8172008-12-16 15:49:22 -08001570 spin_lock_init(&dlm->track_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001571 INIT_LIST_HEAD(&dlm->list);
1572 INIT_LIST_HEAD(&dlm->dirty_list);
1573 INIT_LIST_HEAD(&dlm->reco.resources);
1574 INIT_LIST_HEAD(&dlm->reco.received);
1575 INIT_LIST_HEAD(&dlm->reco.node_data);
1576 INIT_LIST_HEAD(&dlm->purge_list);
1577 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
Sunil Mushran29576f82008-03-10 15:16:21 -07001578 INIT_LIST_HEAD(&dlm->tracking_list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001579 dlm->reco.state = 0;
1580
1581 INIT_LIST_HEAD(&dlm->pending_asts);
1582 INIT_LIST_HEAD(&dlm->pending_basts);
1583
1584 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
1585 dlm->recovery_map, &(dlm->recovery_map[0]));
1586
1587 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
1588 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
1589 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
1590
1591 dlm->dlm_thread_task = NULL;
1592 dlm->dlm_reco_thread_task = NULL;
Kurt Hackel3156d262006-05-01 14:39:29 -07001593 dlm->dlm_worker = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001594 init_waitqueue_head(&dlm->dlm_thread_wq);
1595 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
1596 init_waitqueue_head(&dlm->reco.event);
1597 init_waitqueue_head(&dlm->ast_wq);
1598 init_waitqueue_head(&dlm->migration_wq);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001599 INIT_LIST_HEAD(&dlm->mle_hb_events);
1600
1601 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
1602 init_waitqueue_head(&dlm->dlm_join_events);
1603
1604 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
1605 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001606
Sunil Mushran68007912009-02-26 15:00:44 -08001607 atomic_set(&dlm->res_tot_count, 0);
1608 atomic_set(&dlm->res_cur_count, 0);
Sunil Mushran2041d8f2009-02-26 15:00:43 -08001609 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
1610 atomic_set(&dlm->mle_tot_count[i], 0);
1611 atomic_set(&dlm->mle_cur_count[i], 0);
1612 }
1613
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001614 spin_lock_init(&dlm->work_lock);
1615 INIT_LIST_HEAD(&dlm->work_list);
David Howellsc4028952006-11-22 14:57:56 +00001616 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001617
1618 kref_init(&dlm->dlm_refs);
1619 dlm->dlm_state = DLM_CTXT_NEW;
1620
1621 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
1622
1623 mlog(0, "context init: refcount %u\n",
1624 atomic_read(&dlm->dlm_refs.refcount));
1625
1626leave:
1627 return dlm;
1628}
1629
1630/*
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001631 * Compare a requested locking protocol version against the current one.
1632 *
1633 * If the major numbers are different, they are incompatible.
1634 * If the current minor is greater than the request, they are incompatible.
1635 * If the current minor is less than or equal to the request, they are
1636 * compatible, and the requester should run at the current minor version.
1637 */
1638static int dlm_protocol_compare(struct dlm_protocol_version *existing,
1639 struct dlm_protocol_version *request)
1640{
1641 if (existing->pv_major != request->pv_major)
1642 return 1;
1643
1644 if (existing->pv_minor > request->pv_minor)
1645 return 1;
1646
1647 if (existing->pv_minor < request->pv_minor)
1648 request->pv_minor = existing->pv_minor;
1649
1650 return 0;
1651}
1652
1653/*
1654 * dlm_register_domain: one-time setup per "domain".
1655 *
1656 * The filesystem passes in the requested locking version via proto.
1657 * If registration was successful, proto will contain the negotiated
1658 * locking protocol.
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001659 */
1660struct dlm_ctxt * dlm_register_domain(const char *domain,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001661 u32 key,
1662 struct dlm_protocol_version *fs_proto)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001663{
1664 int ret;
1665 struct dlm_ctxt *dlm = NULL;
1666 struct dlm_ctxt *new_ctxt = NULL;
1667
1668 if (strlen(domain) > O2NM_MAX_NAME_LEN) {
1669 ret = -ENAMETOOLONG;
1670 mlog(ML_ERROR, "domain name length too long\n");
1671 goto leave;
1672 }
1673
1674 if (!o2hb_check_local_node_heartbeating()) {
1675 mlog(ML_ERROR, "the local node has not been configured, or is "
1676 "not heartbeating\n");
1677 ret = -EPROTO;
1678 goto leave;
1679 }
1680
1681 mlog(0, "register called for domain \"%s\"\n", domain);
1682
1683retry:
1684 dlm = NULL;
1685 if (signal_pending(current)) {
1686 ret = -ERESTARTSYS;
1687 mlog_errno(ret);
1688 goto leave;
1689 }
1690
1691 spin_lock(&dlm_domain_lock);
1692
1693 dlm = __dlm_lookup_domain(domain);
1694 if (dlm) {
1695 if (dlm->dlm_state != DLM_CTXT_JOINED) {
1696 spin_unlock(&dlm_domain_lock);
1697
1698 mlog(0, "This ctxt is not joined yet!\n");
1699 wait_event_interruptible(dlm_domain_events,
1700 dlm_wait_on_domain_helper(
1701 domain));
1702 goto retry;
1703 }
1704
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001705 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
1706 mlog(ML_ERROR,
1707 "Requested locking protocol version is not "
1708 "compatible with already registered domain "
1709 "\"%s\"\n", domain);
1710 ret = -EPROTO;
1711 goto leave;
1712 }
1713
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001714 __dlm_get(dlm);
1715 dlm->num_joins++;
1716
1717 spin_unlock(&dlm_domain_lock);
1718
1719 ret = 0;
1720 goto leave;
1721 }
1722
1723 /* doesn't exist */
1724 if (!new_ctxt) {
1725 spin_unlock(&dlm_domain_lock);
1726
1727 new_ctxt = dlm_alloc_ctxt(domain, key);
1728 if (new_ctxt)
1729 goto retry;
1730
1731 ret = -ENOMEM;
1732 mlog_errno(ret);
1733 goto leave;
1734 }
1735
1736 /* a little variable switch-a-roo here... */
1737 dlm = new_ctxt;
1738 new_ctxt = NULL;
1739
1740 /* add the new domain */
1741 list_add_tail(&dlm->list, &dlm_domains);
1742 spin_unlock(&dlm_domain_lock);
1743
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001744 /*
1745 * Pass the locking protocol version into the join. If the join
1746 * succeeds, it will have the negotiated protocol set.
1747 */
1748 dlm->dlm_locking_proto = dlm_protocol;
1749 dlm->fs_locking_proto = *fs_proto;
1750
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001751 ret = dlm_join_domain(dlm);
1752 if (ret) {
1753 mlog_errno(ret);
1754 dlm_put(dlm);
1755 goto leave;
1756 }
1757
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001758 /* Tell the caller what locking protocol we negotiated */
1759 *fs_proto = dlm->fs_locking_proto;
1760
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001761 ret = 0;
1762leave:
1763 if (new_ctxt)
1764 dlm_free_ctxt_mem(new_ctxt);
1765
1766 if (ret < 0)
1767 dlm = ERR_PTR(ret);
1768
1769 return dlm;
1770}
1771EXPORT_SYMBOL_GPL(dlm_register_domain);
1772
1773static LIST_HEAD(dlm_join_handlers);
1774
1775static void dlm_unregister_net_handlers(void)
1776{
1777 o2net_unregister_handler_list(&dlm_join_handlers);
1778}
1779
1780static int dlm_register_net_handlers(void)
1781{
1782 int status = 0;
1783
1784 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1785 sizeof(struct dlm_query_join_request),
1786 dlm_query_join_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001787 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001788 if (status)
1789 goto bail;
1790
1791 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1792 sizeof(struct dlm_assert_joined),
1793 dlm_assert_joined_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001794 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001795 if (status)
1796 goto bail;
1797
1798 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1799 sizeof(struct dlm_cancel_join),
1800 dlm_cancel_join_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001801 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001802
1803bail:
1804 if (status < 0)
1805 dlm_unregister_net_handlers();
1806
1807 return status;
1808}
1809
1810/* Domain eviction callback handling.
1811 *
1812 * The file system requires notification of node death *before* the
1813 * dlm completes it's recovery work, otherwise it may be able to
1814 * acquire locks on resources requiring recovery. Since the dlm can
1815 * evict a node from it's domain *before* heartbeat fires, a similar
1816 * mechanism is required. */
1817
1818/* Eviction is not expected to happen often, so a per-domain lock is
1819 * not necessary. Eviction callbacks are allowed to sleep for short
1820 * periods of time. */
1821static DECLARE_RWSEM(dlm_callback_sem);
1822
1823void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
1824 int node_num)
1825{
1826 struct list_head *iter;
1827 struct dlm_eviction_cb *cb;
1828
1829 down_read(&dlm_callback_sem);
1830 list_for_each(iter, &dlm->dlm_eviction_callbacks) {
1831 cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
1832
1833 cb->ec_func(node_num, cb->ec_data);
1834 }
1835 up_read(&dlm_callback_sem);
1836}
1837
1838void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
1839 dlm_eviction_func *f,
1840 void *data)
1841{
1842 INIT_LIST_HEAD(&cb->ec_item);
1843 cb->ec_func = f;
1844 cb->ec_data = data;
1845}
1846EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
1847
1848void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
1849 struct dlm_eviction_cb *cb)
1850{
1851 down_write(&dlm_callback_sem);
1852 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
1853 up_write(&dlm_callback_sem);
1854}
1855EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
1856
1857void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
1858{
1859 down_write(&dlm_callback_sem);
1860 list_del_init(&cb->ec_item);
1861 up_write(&dlm_callback_sem);
1862}
1863EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
1864
1865static int __init dlm_init(void)
1866{
1867 int status;
1868
1869 dlm_print_version();
1870
1871 status = dlm_init_mle_cache();
Sunil Mushran12eb0032008-03-10 15:16:19 -07001872 if (status) {
1873 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
Sunil Mushran724bdca2008-03-10 15:16:20 -07001874 goto error;
1875 }
1876
1877 status = dlm_init_master_caches();
1878 if (status) {
1879 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
1880 "o2dlm_lockname slabcaches\n");
1881 goto error;
1882 }
1883
1884 status = dlm_init_lock_cache();
1885 if (status) {
1886 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
1887 goto error;
Sunil Mushran12eb0032008-03-10 15:16:19 -07001888 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001889
1890 status = dlm_register_net_handlers();
1891 if (status) {
Sunil Mushran724bdca2008-03-10 15:16:20 -07001892 mlog(ML_ERROR, "Unable to register network handlers\n");
1893 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001894 }
1895
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001896 status = dlm_create_debugfs_root();
1897 if (status)
1898 goto error;
1899
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001900 return 0;
Sunil Mushran724bdca2008-03-10 15:16:20 -07001901error:
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001902 dlm_unregister_net_handlers();
Sunil Mushran724bdca2008-03-10 15:16:20 -07001903 dlm_destroy_lock_cache();
1904 dlm_destroy_master_caches();
1905 dlm_destroy_mle_cache();
1906 return -1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001907}
1908
1909static void __exit dlm_exit (void)
1910{
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001911 dlm_destroy_debugfs_root();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001912 dlm_unregister_net_handlers();
Sunil Mushran724bdca2008-03-10 15:16:20 -07001913 dlm_destroy_lock_cache();
1914 dlm_destroy_master_caches();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001915 dlm_destroy_mle_cache();
1916}
1917
1918MODULE_AUTHOR("Oracle");
1919MODULE_LICENSE("GPL");
1920
1921module_init(dlm_init);
1922module_exit(dlm_exit);