blob: 1307a8cff8dbedce7ae396d367d483a71fad91d1 [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
Kurt Hackel6714d8e2005-12-15 14:31:23 -080046#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
47#include "cluster/masklog.h"
48
Srinivas Eeda1faf2892007-01-29 15:31:35 -080049/*
50 * ocfs2 node maps are array of long int, which limits to send them freely
51 * across the wire due to endianness issues. To workaround this, we convert
52 * long ints to byte arrays. Following 3 routines are helper functions to
53 * set/test/copy bits within those array of bytes
54 */
55static inline void byte_set_bit(u8 nr, u8 map[])
56{
57 map[nr >> 3] |= (1UL << (nr & 7));
58}
59
60static inline int byte_test_bit(u8 nr, u8 map[])
61{
62 return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
63}
64
65static inline void byte_copymap(u8 dmap[], unsigned long smap[],
66 unsigned int sz)
67{
68 unsigned int nn;
69
70 if (!sz)
71 return;
72
73 memset(dmap, 0, ((sz + 7) >> 3));
74 for (nn = 0 ; nn < sz; nn++)
75 if (test_bit(nn, smap))
76 byte_set_bit(nn, dmap);
77}
78
Daniel Phillips03d864c2006-03-10 18:08:16 -080079static void dlm_free_pagevec(void **vec, int pages)
80{
81 while (pages--)
82 free_page((unsigned long)vec[pages]);
83 kfree(vec);
84}
85
86static void **dlm_alloc_pagevec(int pages)
87{
88 void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
89 int i;
90
91 if (!vec)
92 return NULL;
93
94 for (i = 0; i < pages; i++)
95 if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
96 goto out_free;
Joel Beckerc8f33b62006-03-16 17:40:37 -080097
Mark Fasheh685f1ad2006-03-23 11:23:29 -080098 mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
Mark Fashehf5a923d2006-11-28 15:13:18 -080099 pages, (unsigned long)DLM_HASH_PAGES,
100 (unsigned long)DLM_BUCKETS_PER_PAGE);
Daniel Phillips03d864c2006-03-10 18:08:16 -0800101 return vec;
102out_free:
103 dlm_free_pagevec(vec, i);
104 return NULL;
105}
106
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800107/*
108 *
109 * spinlock lock ordering: if multiple locks are needed, obey this ordering:
110 * dlm_domain_lock
111 * struct dlm_ctxt->spinlock
112 * struct dlm_lock_resource->spinlock
113 * struct dlm_ctxt->master_lock
114 * struct dlm_ctxt->ast_lock
115 * dlm_master_list_entry->spinlock
116 * dlm_lock->spinlock
117 *
118 */
119
Ingo Molnar34af9462006-06-27 02:53:55 -0700120DEFINE_SPINLOCK(dlm_domain_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800121LIST_HEAD(dlm_domains);
122static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
123
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800124/*
125 * The supported protocol version for DLM communication. Running domains
126 * will have a negotiated version with the same major number and a minor
127 * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
128 * be used to determine what a running domain is actually using.
Sunil Mushranea203442010-10-09 10:26:23 -0700129 *
130 * New in version 1.1:
131 * - Message DLM_QUERY_REGION added to support global heartbeat
Sunil Mushran18cfdf12010-10-07 16:47:03 -0700132 * - Message DLM_QUERY_NODEINFO added to allow online node removes
Sunil Mushranbddefde2011-05-19 14:34:11 -0700133 * New in version 1.2:
134 * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800135 */
136static const struct dlm_protocol_version dlm_protocol = {
137 .pv_major = 1,
Sunil Mushranbddefde2011-05-19 14:34:11 -0700138 .pv_minor = 2,
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800139};
140
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800141#define DLM_DOMAIN_BACKOFF_MS 200
142
Kurt Hackeld74c9802007-01-17 17:04:25 -0800143static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
144 void **ret_data);
145static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
146 void **ret_data);
147static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
148 void **ret_data);
Sunil Mushranea203442010-10-09 10:26:23 -0700149static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
150 void *data, void **ret_data);
Kurt Hackeld74c9802007-01-17 17:04:25 -0800151static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
152 void **ret_data);
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800153static int dlm_protocol_compare(struct dlm_protocol_version *existing,
154 struct dlm_protocol_version *request);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800155
156static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
157
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700158void __dlm_unhash_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800159{
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700160 if (hlist_unhashed(&res->hash_node))
161 return;
162
163 mlog(0, "%s: Unhash res %.*s\n", dlm->name, res->lockname.len,
164 res->lockname.name);
165 hlist_del_init(&res->hash_node);
166 dlm_lockres_put(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800167}
168
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700169void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800170{
Mark Fasheh81f20942006-02-28 17:31:22 -0800171 struct hlist_head *bucket;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800172 struct qstr *q;
173
174 assert_spin_locked(&dlm->spinlock);
175
176 q = &res->lockname;
Daniel Phillips03d864c2006-03-10 18:08:16 -0800177 bucket = dlm_lockres_hash(dlm, q->hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800178
179 /* get a reference for our hashtable */
180 dlm_lockres_get(res);
181
Mark Fasheh81f20942006-02-28 17:31:22 -0800182 hlist_add_head(&res->hash_node, bucket);
Sunil Mushrane9f0b6a2011-07-24 10:27:54 -0700183
184 mlog(0, "%s: Hash res %.*s\n", dlm->name, res->lockname.len,
185 res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800186}
187
Kurt Hackelba2bf212006-12-01 14:47:20 -0800188struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
189 const char *name,
190 unsigned int len,
191 unsigned int hash)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800192{
Mark Fasheh81f20942006-02-28 17:31:22 -0800193 struct hlist_head *bucket;
Dong Fangdf53cd32013-09-11 14:19:50 -0700194 struct dlm_lock_resource *res;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800195
Tao Maef6b6892011-02-21 11:10:44 +0800196 mlog(0, "%.*s\n", len, name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800197
198 assert_spin_locked(&dlm->spinlock);
199
Daniel Phillips03d864c2006-03-10 18:08:16 -0800200 bucket = dlm_lockres_hash(dlm, hash);
201
Dong Fangdf53cd32013-09-11 14:19:50 -0700202 hlist_for_each_entry(res, bucket, hash_node) {
Daniel Phillips41989852006-03-10 13:31:47 -0800203 if (res->lockname.name[0] != name[0])
204 continue;
205 if (unlikely(res->lockname.len != len))
206 continue;
207 if (memcmp(res->lockname.name + 1, name + 1, len - 1))
208 continue;
209 dlm_lockres_get(res);
210 return res;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800211 }
Daniel Phillips41989852006-03-10 13:31:47 -0800212 return NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800213}
214
Kurt Hackelba2bf212006-12-01 14:47:20 -0800215/* intended to be called by functions which do not care about lock
216 * resources which are being purged (most net _handler functions).
217 * this will return NULL for any lock resource which is found but
218 * currently in the process of dropping its mastery reference.
219 * use __dlm_lookup_lockres_full when you need the lock resource
220 * regardless (e.g. dlm_get_lock_resource) */
221struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
222 const char *name,
223 unsigned int len,
224 unsigned int hash)
225{
226 struct dlm_lock_resource *res = NULL;
227
Tao Maef6b6892011-02-21 11:10:44 +0800228 mlog(0, "%.*s\n", len, name);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800229
230 assert_spin_locked(&dlm->spinlock);
231
232 res = __dlm_lookup_lockres_full(dlm, name, len, hash);
233 if (res) {
234 spin_lock(&res->spinlock);
235 if (res->state & DLM_LOCK_RES_DROPPING_REF) {
236 spin_unlock(&res->spinlock);
237 dlm_lockres_put(res);
238 return NULL;
239 }
240 spin_unlock(&res->spinlock);
241 }
242
243 return res;
244}
245
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800246struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
247 const char *name,
248 unsigned int len)
249{
250 struct dlm_lock_resource *res;
Mark Fasheha3d33292006-03-09 17:55:56 -0800251 unsigned int hash = dlm_lockid_hash(name, len);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800252
253 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -0800254 res = __dlm_lookup_lockres(dlm, name, len, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800255 spin_unlock(&dlm->spinlock);
256 return res;
257}
258
259static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
260{
Dong Fangdf53cd32013-09-11 14:19:50 -0700261 struct dlm_ctxt *tmp;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800262
263 assert_spin_locked(&dlm_domain_lock);
264
265 /* tmp->name here is always NULL terminated,
266 * but domain may not be! */
Dong Fangdf53cd32013-09-11 14:19:50 -0700267 list_for_each_entry(tmp, &dlm_domains, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800268 if (strlen(tmp->name) == len &&
269 memcmp(tmp->name, domain, len)==0)
Dong Fangdf53cd32013-09-11 14:19:50 -0700270 return tmp;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800271 }
272
Dong Fangdf53cd32013-09-11 14:19:50 -0700273 return NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800274}
275
276/* For null terminated domain strings ONLY */
277static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
278{
279 assert_spin_locked(&dlm_domain_lock);
280
281 return __dlm_lookup_domain_full(domain, strlen(domain));
282}
283
284
285/* returns true on one of two conditions:
286 * 1) the domain does not exist
287 * 2) the domain exists and it's state is "joined" */
288static int dlm_wait_on_domain_helper(const char *domain)
289{
290 int ret = 0;
291 struct dlm_ctxt *tmp = NULL;
292
293 spin_lock(&dlm_domain_lock);
294
295 tmp = __dlm_lookup_domain(domain);
296 if (!tmp)
297 ret = 1;
298 else if (tmp->dlm_state == DLM_CTXT_JOINED)
299 ret = 1;
300
301 spin_unlock(&dlm_domain_lock);
302 return ret;
303}
304
305static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
306{
Sunil Mushran6325b4a2008-03-10 15:16:22 -0700307 dlm_destroy_debugfs_subroot(dlm);
308
Mark Fasheh81f20942006-02-28 17:31:22 -0800309 if (dlm->lockres_hash)
Daniel Phillips03d864c2006-03-10 18:08:16 -0800310 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800311
Sunil Mushrane2b66dd2009-02-26 15:00:40 -0800312 if (dlm->master_hash)
313 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
314
Tim Gardnerd787ab02013-02-21 16:42:44 -0800315 kfree(dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800316 kfree(dlm);
317}
318
319/* A little strange - this function will be called while holding
320 * dlm_domain_lock and is expected to be holding it on the way out. We
321 * will however drop and reacquire it multiple times */
322static void dlm_ctxt_release(struct kref *kref)
323{
324 struct dlm_ctxt *dlm;
325
326 dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
327
328 BUG_ON(dlm->num_joins);
329 BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
330
331 /* we may still be in the list if we hit an error during join. */
332 list_del_init(&dlm->list);
333
334 spin_unlock(&dlm_domain_lock);
335
336 mlog(0, "freeing memory from domain %s\n", dlm->name);
337
338 wake_up(&dlm_domain_events);
339
340 dlm_free_ctxt_mem(dlm);
341
342 spin_lock(&dlm_domain_lock);
343}
344
345void dlm_put(struct dlm_ctxt *dlm)
346{
347 spin_lock(&dlm_domain_lock);
348 kref_put(&dlm->dlm_refs, dlm_ctxt_release);
349 spin_unlock(&dlm_domain_lock);
350}
351
352static void __dlm_get(struct dlm_ctxt *dlm)
353{
354 kref_get(&dlm->dlm_refs);
355}
356
357/* given a questionable reference to a dlm object, gets a reference if
358 * it can find it in the list, otherwise returns NULL in which case
359 * you shouldn't trust your pointer. */
360struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
361{
Dong Fangdf53cd32013-09-11 14:19:50 -0700362 struct dlm_ctxt *target;
363 struct dlm_ctxt *ret = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800364
365 spin_lock(&dlm_domain_lock);
366
Dong Fangdf53cd32013-09-11 14:19:50 -0700367 list_for_each_entry(target, &dlm_domains, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800368 if (target == dlm) {
369 __dlm_get(target);
Dong Fangdf53cd32013-09-11 14:19:50 -0700370 ret = target;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800371 break;
372 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800373 }
374
375 spin_unlock(&dlm_domain_lock);
376
Dong Fangdf53cd32013-09-11 14:19:50 -0700377 return ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800378}
379
380int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
381{
382 int ret;
383
384 spin_lock(&dlm_domain_lock);
385 ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
386 (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
387 spin_unlock(&dlm_domain_lock);
388
389 return ret;
390}
391
Kurt Hackel3156d262006-05-01 14:39:29 -0700392static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
393{
394 if (dlm->dlm_worker) {
395 flush_workqueue(dlm->dlm_worker);
396 destroy_workqueue(dlm->dlm_worker);
397 dlm->dlm_worker = NULL;
398 }
399}
400
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800401static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
402{
403 dlm_unregister_domain_handlers(dlm);
Sunil Mushran007dce52008-03-10 15:16:23 -0700404 dlm_debug_shutdown(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800405 dlm_complete_thread(dlm);
406 dlm_complete_recovery_thread(dlm);
Kurt Hackel3156d262006-05-01 14:39:29 -0700407 dlm_destroy_dlm_worker(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800408
409 /* We've left the domain. Now we can take ourselves out of the
410 * list and allow the kref stuff to help us free the
411 * memory. */
412 spin_lock(&dlm_domain_lock);
413 list_del_init(&dlm->list);
414 spin_unlock(&dlm_domain_lock);
415
416 /* Wake up anyone waiting for us to remove this domain */
417 wake_up(&dlm_domain_events);
418}
419
Kurt Hackelba2bf212006-12-01 14:47:20 -0800420static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800421{
Kurt Hackelba2bf212006-12-01 14:47:20 -0800422 int i, num, n, ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800423 struct dlm_lock_resource *res;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800424 struct hlist_node *iter;
425 struct hlist_head *bucket;
426 int dropped;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800427
428 mlog(0, "Migrating locks from domain %s\n", dlm->name);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800429
430 num = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800431 spin_lock(&dlm->spinlock);
Mark Fasheh81f20942006-02-28 17:31:22 -0800432 for (i = 0; i < DLM_HASH_BUCKETS; i++) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800433redo_bucket:
434 n = 0;
435 bucket = dlm_lockres_hash(dlm, i);
436 iter = bucket->first;
437 while (iter) {
438 n++;
439 res = hlist_entry(iter, struct dlm_lock_resource,
440 hash_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800441 dlm_lockres_get(res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800442 /* migrate, if necessary. this will drop the dlm
443 * spinlock and retake it if it does migration. */
444 dropped = dlm_empty_lockres(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800445
Kurt Hackelba2bf212006-12-01 14:47:20 -0800446 spin_lock(&res->spinlock);
Sunil Mushran66effd32011-05-19 14:34:12 -0700447 if (dropped)
448 __dlm_lockres_calc_usage(dlm, res);
449 else
450 iter = res->hash_node.next;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800451 spin_unlock(&res->spinlock);
452
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800453 dlm_lockres_put(res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800454
Sunil Mushran66effd32011-05-19 14:34:12 -0700455 if (dropped) {
456 cond_resched_lock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800457 goto redo_bucket;
Sunil Mushran66effd32011-05-19 14:34:12 -0700458 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800459 }
Sunil Mushran0d01af62007-04-17 13:32:20 -0700460 cond_resched_lock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800461 num += n;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800462 }
463 spin_unlock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800464 wake_up(&dlm->dlm_thread_wq);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800465
Kurt Hackelba2bf212006-12-01 14:47:20 -0800466 /* let the dlm thread take care of purging, keep scanning until
467 * nothing remains in the hash */
468 if (num) {
469 mlog(0, "%s: %d lock resources in hash last pass\n",
470 dlm->name, num);
471 ret = -EAGAIN;
472 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800473 mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800474 return ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800475}
476
477static int dlm_no_joining_node(struct dlm_ctxt *dlm)
478{
479 int ret;
480
481 spin_lock(&dlm->spinlock);
482 ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
483 spin_unlock(&dlm->spinlock);
484
485 return ret;
486}
487
Sunil Mushranbddefde2011-05-19 14:34:11 -0700488static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
489 void *data, void **ret_data)
490{
491 struct dlm_ctxt *dlm = data;
492 unsigned int node;
493 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
494
495 if (!dlm_grab(dlm))
496 return 0;
497
498 node = exit_msg->node_idx;
499 mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
500
501 spin_lock(&dlm->spinlock);
502 set_bit(node, dlm->exit_domain_map);
503 spin_unlock(&dlm->spinlock);
504
505 dlm_put(dlm);
506
507 return 0;
508}
509
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800510static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
511{
512 /* Yikes, a double spinlock! I need domain_lock for the dlm
513 * state and the dlm spinlock for join state... Sorry! */
514again:
515 spin_lock(&dlm_domain_lock);
516 spin_lock(&dlm->spinlock);
517
518 if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
519 mlog(0, "Node %d is joining, we wait on it.\n",
520 dlm->joining_node);
521 spin_unlock(&dlm->spinlock);
522 spin_unlock(&dlm_domain_lock);
523
524 wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
525 goto again;
526 }
527
528 dlm->dlm_state = DLM_CTXT_LEAVING;
529 spin_unlock(&dlm->spinlock);
530 spin_unlock(&dlm_domain_lock);
531}
532
533static void __dlm_print_nodes(struct dlm_ctxt *dlm)
534{
Sunil Mushran8decab32011-07-24 10:23:54 -0700535 int node = -1, num = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800536
537 assert_spin_locked(&dlm->spinlock);
538
Sunil Mushran8decab32011-07-24 10:23:54 -0700539 printk("( ");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800540 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
541 node + 1)) < O2NM_MAX_NODES) {
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700542 printk("%d ", node);
Sunil Mushran8decab32011-07-24 10:23:54 -0700543 ++num;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800544 }
Sunil Mushran8decab32011-07-24 10:23:54 -0700545 printk(") %u nodes\n", num);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800546}
547
Kurt Hackeld74c9802007-01-17 17:04:25 -0800548static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
549 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800550{
551 struct dlm_ctxt *dlm = data;
552 unsigned int node;
553 struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
554
Tao Maef6b6892011-02-21 11:10:44 +0800555 mlog(0, "%p %u %p", msg, len, data);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800556
557 if (!dlm_grab(dlm))
558 return 0;
559
560 node = exit_msg->node_idx;
561
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800562 spin_lock(&dlm->spinlock);
563 clear_bit(node, dlm->domain_map);
Sunil Mushranbddefde2011-05-19 14:34:11 -0700564 clear_bit(node, dlm->exit_domain_map);
Sunil Mushran8decab32011-07-24 10:23:54 -0700565 printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800566 __dlm_print_nodes(dlm);
567
568 /* notify anything attached to the heartbeat events */
569 dlm_hb_event_notify_attached(dlm, node, 0);
570
571 spin_unlock(&dlm->spinlock);
572
573 dlm_put(dlm);
574
575 return 0;
576}
577
Sunil Mushranbddefde2011-05-19 14:34:11 -0700578static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800579 unsigned int node)
580{
581 int status;
582 struct dlm_exit_domain leave_msg;
583
Sunil Mushranbddefde2011-05-19 14:34:11 -0700584 mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
585 msg_type, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800586
587 memset(&leave_msg, 0, sizeof(leave_msg));
588 leave_msg.node_idx = dlm->node_num;
589
Sunil Mushranbddefde2011-05-19 14:34:11 -0700590 status = o2net_send_message(msg_type, dlm->key, &leave_msg,
591 sizeof(leave_msg), node, NULL);
Wengang Wanga5196ec2010-03-30 12:09:22 +0800592 if (status < 0)
Sunil Mushranbddefde2011-05-19 14:34:11 -0700593 mlog(ML_ERROR, "Error %d sending domain exit message %u "
594 "to node %u on domain %s\n", status, msg_type, node,
595 dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800596
597 return status;
598}
599
Sunil Mushranbddefde2011-05-19 14:34:11 -0700600static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
601{
602 int node = -1;
603
604 /* Support for begin exit domain was added in 1.2 */
605 if (dlm->dlm_locking_proto.pv_major == 1 &&
606 dlm->dlm_locking_proto.pv_minor < 2)
607 return;
608
609 /*
610 * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
611 * informational. Meaning if a node does not receive the message,
612 * so be it.
613 */
614 spin_lock(&dlm->spinlock);
615 while (1) {
616 node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
617 if (node >= O2NM_MAX_NODES)
618 break;
619 if (node == dlm->node_num)
620 continue;
621
622 spin_unlock(&dlm->spinlock);
623 dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
624 spin_lock(&dlm->spinlock);
625 }
626 spin_unlock(&dlm->spinlock);
627}
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800628
629static void dlm_leave_domain(struct dlm_ctxt *dlm)
630{
631 int node, clear_node, status;
632
633 /* At this point we've migrated away all our locks and won't
634 * accept mastership of new ones. The dlm is responsible for
635 * almost nothing now. We make sure not to confuse any joining
636 * nodes and then commence shutdown procedure. */
637
638 spin_lock(&dlm->spinlock);
639 /* Clear ourselves from the domain map */
640 clear_bit(dlm->node_num, dlm->domain_map);
641 while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
642 0)) < O2NM_MAX_NODES) {
643 /* Drop the dlm spinlock. This is safe wrt the domain_map.
644 * -nodes cannot be added now as the
645 * query_join_handlers knows to respond with OK_NO_MAP
646 * -we catch the right network errors if a node is
647 * removed from the map while we're sending him the
648 * exit message. */
649 spin_unlock(&dlm->spinlock);
650
651 clear_node = 1;
652
Sunil Mushranbddefde2011-05-19 14:34:11 -0700653 status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
654 node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800655 if (status < 0 &&
656 status != -ENOPROTOOPT &&
657 status != -ENOTCONN) {
658 mlog(ML_NOTICE, "Error %d sending domain exit message "
659 "to node %d\n", status, node);
660
661 /* Not sure what to do here but lets sleep for
662 * a bit in case this was a transient
663 * error... */
664 msleep(DLM_DOMAIN_BACKOFF_MS);
665 clear_node = 0;
666 }
667
668 spin_lock(&dlm->spinlock);
669 /* If we're not clearing the node bit then we intend
670 * to loop back around to try again. */
671 if (clear_node)
672 clear_bit(node, dlm->domain_map);
673 }
674 spin_unlock(&dlm->spinlock);
675}
676
677int dlm_joined(struct dlm_ctxt *dlm)
678{
679 int ret = 0;
680
681 spin_lock(&dlm_domain_lock);
682
683 if (dlm->dlm_state == DLM_CTXT_JOINED)
684 ret = 1;
685
686 spin_unlock(&dlm_domain_lock);
687
688 return ret;
689}
690
691int dlm_shutting_down(struct dlm_ctxt *dlm)
692{
693 int ret = 0;
694
695 spin_lock(&dlm_domain_lock);
696
697 if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
698 ret = 1;
699
700 spin_unlock(&dlm_domain_lock);
701
702 return ret;
703}
704
705void dlm_unregister_domain(struct dlm_ctxt *dlm)
706{
707 int leave = 0;
Sunil Mushran29576f82008-03-10 15:16:21 -0700708 struct dlm_lock_resource *res;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800709
710 spin_lock(&dlm_domain_lock);
711 BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
712 BUG_ON(!dlm->num_joins);
713
714 dlm->num_joins--;
715 if (!dlm->num_joins) {
716 /* We mark it "in shutdown" now so new register
717 * requests wait until we've completely left the
718 * domain. Don't use DLM_CTXT_LEAVING yet as we still
719 * want new domain joins to communicate with us at
720 * least until we've completed migration of our
721 * resources. */
722 dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
723 leave = 1;
724 }
725 spin_unlock(&dlm_domain_lock);
726
727 if (leave) {
728 mlog(0, "shutting down domain %s\n", dlm->name);
Sunil Mushranbddefde2011-05-19 14:34:11 -0700729 dlm_begin_exit_domain(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800730
731 /* We changed dlm state, notify the thread */
732 dlm_kick_thread(dlm, NULL);
733
Kurt Hackelba2bf212006-12-01 14:47:20 -0800734 while (dlm_migrate_all_locks(dlm)) {
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -0700735 /* Give dlm_thread time to purge the lockres' */
736 msleep(500);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800737 mlog(0, "%s: more migration to do\n", dlm->name);
738 }
Sunil Mushran29576f82008-03-10 15:16:21 -0700739
740 /* This list should be empty. If not, print remaining lockres */
741 if (!list_empty(&dlm->tracking_list)) {
742 mlog(ML_ERROR, "Following lockres' are still on the "
743 "tracking list:\n");
744 list_for_each_entry(res, &dlm->tracking_list, tracking)
745 dlm_print_one_lock_resource(res);
746 }
747
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800748 dlm_mark_domain_leaving(dlm);
749 dlm_leave_domain(dlm);
Sunil Mushran8decab32011-07-24 10:23:54 -0700750 printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
Srinivas Eeda5dad6c32010-09-21 16:27:26 -0700751 dlm_force_free_mles(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800752 dlm_complete_dlm_shutdown(dlm);
753 }
754 dlm_put(dlm);
755}
756EXPORT_SYMBOL_GPL(dlm_unregister_domain);
757
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800758static int dlm_query_join_proto_check(char *proto_type, int node,
759 struct dlm_protocol_version *ours,
760 struct dlm_protocol_version *request)
761{
762 int rc;
763 struct dlm_protocol_version proto = *request;
764
765 if (!dlm_protocol_compare(ours, &proto)) {
766 mlog(0,
767 "node %u wanted to join with %s locking protocol "
768 "%u.%u, we respond with %u.%u\n",
769 node, proto_type,
770 request->pv_major,
771 request->pv_minor,
772 proto.pv_major, proto.pv_minor);
773 request->pv_minor = proto.pv_minor;
774 rc = 0;
775 } else {
776 mlog(ML_NOTICE,
777 "Node %u wanted to join with %s locking "
778 "protocol %u.%u, but we have %u.%u, disallowing\n",
779 node, proto_type,
780 request->pv_major,
781 request->pv_minor,
782 ours->pv_major,
783 ours->pv_minor);
784 rc = 1;
785 }
786
787 return rc;
788}
789
Joel Becker0f71b7b2008-02-12 14:56:25 -0800790/*
791 * struct dlm_query_join_packet is made up of four one-byte fields. They
792 * are effectively in big-endian order already. However, little-endian
793 * machines swap them before putting the packet on the wire (because
794 * query_join's response is a status, and that status is treated as a u32
795 * on the wire). Thus, a big-endian and little-endian machines will treat
796 * this structure differently.
797 *
798 * The solution is to have little-endian machines swap the structure when
799 * converting from the structure to the u32 representation. This will
800 * result in the structure having the correct format on the wire no matter
801 * the host endian format.
802 */
803static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
804 u32 *wire)
805{
806 union dlm_query_join_response response;
807
808 response.packet = *packet;
Al Viro85158412012-04-12 18:47:13 -0400809 *wire = be32_to_cpu(response.intval);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800810}
811
812static void dlm_query_join_wire_to_packet(u32 wire,
813 struct dlm_query_join_packet *packet)
814{
815 union dlm_query_join_response response;
816
817 response.intval = cpu_to_be32(wire);
818 *packet = response.packet;
819}
820
Kurt Hackeld74c9802007-01-17 17:04:25 -0800821static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
822 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800823{
824 struct dlm_query_join_request *query;
Joel Becker0f71b7b2008-02-12 14:56:25 -0800825 struct dlm_query_join_packet packet = {
826 .code = JOIN_DISALLOW,
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800827 };
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800828 struct dlm_ctxt *dlm = NULL;
Joel Becker0f71b7b2008-02-12 14:56:25 -0800829 u32 response;
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800830 u8 nodenum;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800831
832 query = (struct dlm_query_join_request *) msg->buf;
833
834 mlog(0, "node %u wants to join domain %s\n", query->node_idx,
835 query->domain);
836
837 /*
838 * If heartbeat doesn't consider the node live, tell it
839 * to back off and try again. This gives heartbeat a chance
840 * to catch up.
841 */
842 if (!o2hb_check_node_heartbeating(query->node_idx)) {
843 mlog(0, "node %u is not in our live map yet\n",
844 query->node_idx);
845
Joel Becker0f71b7b2008-02-12 14:56:25 -0800846 packet.code = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800847 goto respond;
848 }
849
Joel Becker0f71b7b2008-02-12 14:56:25 -0800850 packet.code = JOIN_OK_NO_MAP;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800851
852 spin_lock(&dlm_domain_lock);
853 dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800854 if (!dlm)
855 goto unlock_respond;
856
857 /*
858 * There is a small window where the joining node may not see the
859 * node(s) that just left but still part of the cluster. DISALLOW
860 * join request if joining node has different node map.
861 */
862 nodenum=0;
863 while (nodenum < O2NM_MAX_NODES) {
864 if (test_bit(nodenum, dlm->domain_map)) {
865 if (!byte_test_bit(nodenum, query->node_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800866 mlog(0, "disallow join as node %u does not "
867 "have node %u in its nodemap\n",
868 query->node_idx, nodenum);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800869 packet.code = JOIN_DISALLOW;
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800870 goto unlock_respond;
871 }
872 }
873 nodenum++;
874 }
875
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800876 /* Once the dlm ctxt is marked as leaving then we don't want
Sunil Mushran2bd63212010-01-25 16:57:38 -0800877 * to be put in someone's domain map.
Kurt Hackele2faea42006-01-12 14:24:55 -0800878 * Also, explicitly disallow joining at certain troublesome
879 * times (ie. during recovery). */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800880 if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
Kurt Hackele2faea42006-01-12 14:24:55 -0800881 int bit = query->node_idx;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800882 spin_lock(&dlm->spinlock);
883
884 if (dlm->dlm_state == DLM_CTXT_NEW &&
885 dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
886 /*If this is a brand new context and we
887 * haven't started our join process yet, then
888 * the other node won the race. */
Joel Becker0f71b7b2008-02-12 14:56:25 -0800889 packet.code = JOIN_OK_NO_MAP;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800890 } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
891 /* Disallow parallel joins. */
Joel Becker0f71b7b2008-02-12 14:56:25 -0800892 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800893 } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800894 mlog(0, "node %u trying to join, but recovery "
Kurt Hackele2faea42006-01-12 14:24:55 -0800895 "is ongoing.\n", bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800896 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800897 } else if (test_bit(bit, dlm->recovery_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800898 mlog(0, "node %u trying to join, but it "
Kurt Hackele2faea42006-01-12 14:24:55 -0800899 "still needs recovery.\n", bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800900 packet.code = JOIN_DISALLOW;
Kurt Hackele2faea42006-01-12 14:24:55 -0800901 } else if (test_bit(bit, dlm->domain_map)) {
Sunil Mushrane4968472007-01-29 15:37:02 -0800902 mlog(0, "node %u trying to join, but it "
Kurt Hackele2faea42006-01-12 14:24:55 -0800903 "is still in the domain! needs recovery?\n",
904 bit);
Joel Becker0f71b7b2008-02-12 14:56:25 -0800905 packet.code = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800906 } else {
907 /* Alright we're fully a part of this domain
908 * so we keep some state as to who's joining
909 * and indicate to him that needs to be fixed
910 * up. */
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800911
912 /* Make sure we speak compatible locking protocols. */
913 if (dlm_query_join_proto_check("DLM", bit,
914 &dlm->dlm_locking_proto,
915 &query->dlm_proto)) {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800916 packet.code = JOIN_PROTOCOL_MISMATCH;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800917 } else if (dlm_query_join_proto_check("fs", bit,
918 &dlm->fs_locking_proto,
919 &query->fs_proto)) {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800920 packet.code = JOIN_PROTOCOL_MISMATCH;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800921 } else {
Joel Becker0f71b7b2008-02-12 14:56:25 -0800922 packet.dlm_minor = query->dlm_proto.pv_minor;
923 packet.fs_minor = query->fs_proto.pv_minor;
924 packet.code = JOIN_OK;
Joel Beckerd24fbcd2008-01-25 17:02:21 -0800925 __dlm_set_joining_node(dlm, query->node_idx);
926 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800927 }
928
929 spin_unlock(&dlm->spinlock);
930 }
Srinivas Eeda1faf2892007-01-29 15:31:35 -0800931unlock_respond:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800932 spin_unlock(&dlm_domain_lock);
933
934respond:
Joel Becker0f71b7b2008-02-12 14:56:25 -0800935 mlog(0, "We respond with %u\n", packet.code);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800936
Joel Becker0f71b7b2008-02-12 14:56:25 -0800937 dlm_query_join_packet_to_wire(&packet, &response);
938 return response;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800939}
940
Kurt Hackeld74c9802007-01-17 17:04:25 -0800941static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
942 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800943{
944 struct dlm_assert_joined *assert;
945 struct dlm_ctxt *dlm = NULL;
946
947 assert = (struct dlm_assert_joined *) msg->buf;
948
949 mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
950 assert->domain);
951
952 spin_lock(&dlm_domain_lock);
953 dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
954 /* XXX should we consider no dlm ctxt an error? */
955 if (dlm) {
956 spin_lock(&dlm->spinlock);
957
958 /* Alright, this node has officially joined our
959 * domain. Set him in the map and clean up our
960 * leftover join state. */
961 BUG_ON(dlm->joining_node != assert->node_idx);
962 set_bit(assert->node_idx, dlm->domain_map);
Sunil Mushranbddefde2011-05-19 14:34:11 -0700963 clear_bit(assert->node_idx, dlm->exit_domain_map);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800964 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
965
Sunil Mushran8decab32011-07-24 10:23:54 -0700966 printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
Sunil Mushran781ee3e2006-04-27 16:41:31 -0700967 assert->node_idx, dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800968 __dlm_print_nodes(dlm);
969
970 /* notify anything attached to the heartbeat events */
971 dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
972
973 spin_unlock(&dlm->spinlock);
974 }
975 spin_unlock(&dlm_domain_lock);
976
977 return 0;
978}
979
Sunil Mushranea203442010-10-09 10:26:23 -0700980static int dlm_match_regions(struct dlm_ctxt *dlm,
Sunil Mushran770c4d82011-02-21 14:25:25 -0800981 struct dlm_query_region *qr,
982 char *local, int locallen)
Sunil Mushranea203442010-10-09 10:26:23 -0700983{
Sunil Mushran770c4d82011-02-21 14:25:25 -0800984 char *remote = qr->qr_regions;
Sunil Mushranea203442010-10-09 10:26:23 -0700985 char *l, *r;
986 int localnr, i, j, foundit;
987 int status = 0;
988
989 if (!o2hb_global_heartbeat_active()) {
990 if (qr->qr_numregions) {
991 mlog(ML_ERROR, "Domain %s: Joining node %d has global "
992 "heartbeat enabled but local node %d does not\n",
993 qr->qr_domain, qr->qr_node, dlm->node_num);
994 status = -EINVAL;
995 }
996 goto bail;
997 }
998
999 if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
1000 mlog(ML_ERROR, "Domain %s: Local node %d has global "
1001 "heartbeat enabled but joining node %d does not\n",
1002 qr->qr_domain, dlm->node_num, qr->qr_node);
1003 status = -EINVAL;
1004 goto bail;
1005 }
1006
1007 r = remote;
1008 for (i = 0; i < qr->qr_numregions; ++i) {
1009 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
1010 r += O2HB_MAX_REGION_NAME_LEN;
1011 }
1012
Sunil Mushran770c4d82011-02-21 14:25:25 -08001013 localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
1014 localnr = o2hb_get_all_regions(local, (u8)localnr);
Sunil Mushranea203442010-10-09 10:26:23 -07001015
1016 /* compare local regions with remote */
1017 l = local;
1018 for (i = 0; i < localnr; ++i) {
1019 foundit = 0;
1020 r = remote;
1021 for (j = 0; j <= qr->qr_numregions; ++j) {
1022 if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
1023 foundit = 1;
1024 break;
1025 }
1026 r += O2HB_MAX_REGION_NAME_LEN;
1027 }
1028 if (!foundit) {
1029 status = -EINVAL;
1030 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1031 "in local node %d but not in joining node %d\n",
1032 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
1033 dlm->node_num, qr->qr_node);
1034 goto bail;
1035 }
1036 l += O2HB_MAX_REGION_NAME_LEN;
1037 }
1038
1039 /* compare remote with local regions */
1040 r = remote;
1041 for (i = 0; i < qr->qr_numregions; ++i) {
1042 foundit = 0;
1043 l = local;
1044 for (j = 0; j < localnr; ++j) {
1045 if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
1046 foundit = 1;
1047 break;
1048 }
1049 l += O2HB_MAX_REGION_NAME_LEN;
1050 }
1051 if (!foundit) {
1052 status = -EINVAL;
1053 mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
1054 "in joining node %d but not in local node %d\n",
1055 qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
1056 qr->qr_node, dlm->node_num);
1057 goto bail;
1058 }
1059 r += O2HB_MAX_REGION_NAME_LEN;
1060 }
1061
1062bail:
Sunil Mushranea203442010-10-09 10:26:23 -07001063 return status;
1064}
1065
1066static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
1067{
1068 struct dlm_query_region *qr = NULL;
1069 int status, ret = 0, i;
1070 char *p;
1071
1072 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1073 goto bail;
1074
1075 qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
1076 if (!qr) {
1077 ret = -ENOMEM;
1078 mlog_errno(ret);
1079 goto bail;
1080 }
1081
1082 qr->qr_node = dlm->node_num;
1083 qr->qr_namelen = strlen(dlm->name);
1084 memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
1085 /* if local hb, the numregions will be zero */
1086 if (o2hb_global_heartbeat_active())
1087 qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
1088 O2NM_MAX_REGIONS);
1089
1090 p = qr->qr_regions;
1091 for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
1092 mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
1093
1094 i = -1;
1095 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1096 i + 1)) < O2NM_MAX_NODES) {
1097 if (i == dlm->node_num)
1098 continue;
1099
1100 mlog(0, "Sending regions to node %d\n", i);
1101
1102 ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
1103 sizeof(struct dlm_query_region),
1104 i, &status);
1105 if (ret >= 0)
1106 ret = status;
1107 if (ret) {
1108 mlog(ML_ERROR, "Region mismatch %d, node %d\n",
1109 ret, i);
1110 break;
1111 }
1112 }
1113
1114bail:
1115 kfree(qr);
1116 return ret;
1117}
1118
1119static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
1120 void *data, void **ret_data)
1121{
1122 struct dlm_query_region *qr;
1123 struct dlm_ctxt *dlm = NULL;
Sunil Mushran770c4d82011-02-21 14:25:25 -08001124 char *local = NULL;
Sunil Mushranea203442010-10-09 10:26:23 -07001125 int status = 0;
1126 int locked = 0;
1127
1128 qr = (struct dlm_query_region *) msg->buf;
1129
1130 mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
1131 qr->qr_domain);
1132
Sunil Mushran770c4d82011-02-21 14:25:25 -08001133 /* buffer used in dlm_mast_regions() */
1134 local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
1135 if (!local) {
1136 status = -ENOMEM;
1137 goto bail;
1138 }
1139
Sunil Mushranea203442010-10-09 10:26:23 -07001140 status = -EINVAL;
1141
1142 spin_lock(&dlm_domain_lock);
1143 dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
1144 if (!dlm) {
1145 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1146 "before join domain\n", qr->qr_node, qr->qr_domain);
1147 goto bail;
1148 }
1149
1150 spin_lock(&dlm->spinlock);
1151 locked = 1;
1152 if (dlm->joining_node != qr->qr_node) {
1153 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1154 "but joining node is %d\n", qr->qr_node, qr->qr_domain,
1155 dlm->joining_node);
1156 goto bail;
1157 }
1158
1159 /* Support for global heartbeat was added in 1.1 */
1160 if (dlm->dlm_locking_proto.pv_major == 1 &&
1161 dlm->dlm_locking_proto.pv_minor == 0) {
1162 mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
1163 "but active dlm protocol is %d.%d\n", qr->qr_node,
1164 qr->qr_domain, dlm->dlm_locking_proto.pv_major,
1165 dlm->dlm_locking_proto.pv_minor);
1166 goto bail;
1167 }
1168
Sunil Mushran770c4d82011-02-21 14:25:25 -08001169 status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
Sunil Mushranea203442010-10-09 10:26:23 -07001170
1171bail:
1172 if (locked)
1173 spin_unlock(&dlm->spinlock);
1174 spin_unlock(&dlm_domain_lock);
1175
Sunil Mushran770c4d82011-02-21 14:25:25 -08001176 kfree(local);
1177
Sunil Mushranea203442010-10-09 10:26:23 -07001178 return status;
1179}
1180
Sunil Mushran18cfdf12010-10-07 16:47:03 -07001181static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
1182{
1183 struct o2nm_node *local;
1184 struct dlm_node_info *remote;
1185 int i, j;
1186 int status = 0;
1187
1188 for (j = 0; j < qn->qn_numnodes; ++j)
1189 mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
1190 &(qn->qn_nodes[j].ni_ipv4_address),
1191 ntohs(qn->qn_nodes[j].ni_ipv4_port));
1192
1193 for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
1194 local = o2nm_get_node_by_num(i);
1195 remote = NULL;
1196 for (j = 0; j < qn->qn_numnodes; ++j) {
1197 if (qn->qn_nodes[j].ni_nodenum == i) {
1198 remote = &(qn->qn_nodes[j]);
1199 break;
1200 }
1201 }
1202
1203 if (!local && !remote)
1204 continue;
1205
1206 if ((local && !remote) || (!local && remote))
1207 status = -EINVAL;
1208
1209 if (!status &&
1210 ((remote->ni_nodenum != local->nd_num) ||
1211 (remote->ni_ipv4_port != local->nd_ipv4_port) ||
1212 (remote->ni_ipv4_address != local->nd_ipv4_address)))
1213 status = -EINVAL;
1214
1215 if (status) {
1216 if (remote && !local)
1217 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1218 "registered in joining node %d but not in "
1219 "local node %d\n", qn->qn_domain,
1220 remote->ni_nodenum,
1221 &(remote->ni_ipv4_address),
1222 ntohs(remote->ni_ipv4_port),
1223 qn->qn_nodenum, dlm->node_num);
1224 if (local && !remote)
1225 mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
1226 "registered in local node %d but not in "
1227 "joining node %d\n", qn->qn_domain,
1228 local->nd_num, &(local->nd_ipv4_address),
1229 ntohs(local->nd_ipv4_port),
1230 dlm->node_num, qn->qn_nodenum);
1231 BUG_ON((!local && !remote));
1232 }
1233
1234 if (local)
1235 o2nm_node_put(local);
1236 }
1237
1238 return status;
1239}
1240
1241static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
1242{
1243 struct dlm_query_nodeinfo *qn = NULL;
1244 struct o2nm_node *node;
1245 int ret = 0, status, count, i;
1246
1247 if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
1248 goto bail;
1249
1250 qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
1251 if (!qn) {
1252 ret = -ENOMEM;
1253 mlog_errno(ret);
1254 goto bail;
1255 }
1256
1257 for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
1258 node = o2nm_get_node_by_num(i);
1259 if (!node)
1260 continue;
1261 qn->qn_nodes[count].ni_nodenum = node->nd_num;
1262 qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
1263 qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
1264 mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
1265 &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
1266 ++count;
1267 o2nm_node_put(node);
1268 }
1269
1270 qn->qn_nodenum = dlm->node_num;
1271 qn->qn_numnodes = count;
1272 qn->qn_namelen = strlen(dlm->name);
1273 memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
1274
1275 i = -1;
1276 while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
1277 i + 1)) < O2NM_MAX_NODES) {
1278 if (i == dlm->node_num)
1279 continue;
1280
1281 mlog(0, "Sending nodeinfo to node %d\n", i);
1282
1283 ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
1284 qn, sizeof(struct dlm_query_nodeinfo),
1285 i, &status);
1286 if (ret >= 0)
1287 ret = status;
1288 if (ret) {
1289 mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
1290 break;
1291 }
1292 }
1293
1294bail:
1295 kfree(qn);
1296 return ret;
1297}
1298
1299static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
1300 void *data, void **ret_data)
1301{
1302 struct dlm_query_nodeinfo *qn;
1303 struct dlm_ctxt *dlm = NULL;
1304 int locked = 0, status = -EINVAL;
1305
1306 qn = (struct dlm_query_nodeinfo *) msg->buf;
1307
1308 mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
1309 qn->qn_domain);
1310
1311 spin_lock(&dlm_domain_lock);
1312 dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
1313 if (!dlm) {
1314 mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
1315 "join domain\n", qn->qn_nodenum, qn->qn_domain);
1316 goto bail;
1317 }
1318
1319 spin_lock(&dlm->spinlock);
1320 locked = 1;
1321 if (dlm->joining_node != qn->qn_nodenum) {
1322 mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
1323 "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
1324 dlm->joining_node);
1325 goto bail;
1326 }
1327
1328 /* Support for node query was added in 1.1 */
1329 if (dlm->dlm_locking_proto.pv_major == 1 &&
1330 dlm->dlm_locking_proto.pv_minor == 0) {
1331 mlog(ML_ERROR, "Node %d queried nodes on domain %s "
1332 "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
1333 qn->qn_domain, dlm->dlm_locking_proto.pv_major,
1334 dlm->dlm_locking_proto.pv_minor);
1335 goto bail;
1336 }
1337
1338 status = dlm_match_nodes(dlm, qn);
1339
1340bail:
1341 if (locked)
1342 spin_unlock(&dlm->spinlock);
1343 spin_unlock(&dlm_domain_lock);
1344
1345 return status;
1346}
1347
Kurt Hackeld74c9802007-01-17 17:04:25 -08001348static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
1349 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001350{
1351 struct dlm_cancel_join *cancel;
1352 struct dlm_ctxt *dlm = NULL;
1353
1354 cancel = (struct dlm_cancel_join *) msg->buf;
1355
1356 mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
1357 cancel->domain);
1358
1359 spin_lock(&dlm_domain_lock);
1360 dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
1361
1362 if (dlm) {
1363 spin_lock(&dlm->spinlock);
1364
1365 /* Yikes, this guy wants to cancel his join. No
1366 * problem, we simply cleanup our join state. */
1367 BUG_ON(dlm->joining_node != cancel->node_idx);
1368 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
1369
1370 spin_unlock(&dlm->spinlock);
1371 }
1372 spin_unlock(&dlm_domain_lock);
1373
1374 return 0;
1375}
1376
1377static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
1378 unsigned int node)
1379{
1380 int status;
1381 struct dlm_cancel_join cancel_msg;
1382
1383 memset(&cancel_msg, 0, sizeof(cancel_msg));
1384 cancel_msg.node_idx = dlm->node_num;
1385 cancel_msg.name_len = strlen(dlm->name);
1386 memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
1387
1388 status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1389 &cancel_msg, sizeof(cancel_msg), node,
1390 NULL);
1391 if (status < 0) {
Wengang Wanga5196ec2010-03-30 12:09:22 +08001392 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1393 "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
1394 node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001395 goto bail;
1396 }
1397
1398bail:
1399 return status;
1400}
1401
1402/* map_size should be in bytes. */
1403static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
1404 unsigned long *node_map,
1405 unsigned int map_size)
1406{
1407 int status, tmpstat;
1408 unsigned int node;
1409
1410 if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
1411 sizeof(unsigned long))) {
1412 mlog(ML_ERROR,
1413 "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
Andrew Morton3a4780a2008-02-29 01:56:06 -08001414 map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001415 return -EINVAL;
1416 }
1417
1418 status = 0;
1419 node = -1;
1420 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1421 node + 1)) < O2NM_MAX_NODES) {
1422 if (node == dlm->node_num)
1423 continue;
1424
1425 tmpstat = dlm_send_one_join_cancel(dlm, node);
1426 if (tmpstat) {
1427 mlog(ML_ERROR, "Error return %d cancelling join on "
1428 "node %d\n", tmpstat, node);
1429 if (!status)
1430 status = tmpstat;
1431 }
1432 }
1433
1434 if (status)
1435 mlog_errno(status);
1436 return status;
1437}
1438
1439static int dlm_request_join(struct dlm_ctxt *dlm,
1440 int node,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001441 enum dlm_query_join_response_code *response)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001442{
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001443 int status;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001444 struct dlm_query_join_request join_msg;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001445 struct dlm_query_join_packet packet;
1446 u32 join_resp;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001447
1448 mlog(0, "querying node %d\n", node);
1449
1450 memset(&join_msg, 0, sizeof(join_msg));
1451 join_msg.node_idx = dlm->node_num;
1452 join_msg.name_len = strlen(dlm->name);
1453 memcpy(join_msg.domain, dlm->name, join_msg.name_len);
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001454 join_msg.dlm_proto = dlm->dlm_locking_proto;
1455 join_msg.fs_proto = dlm->fs_locking_proto;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001456
Srinivas Eeda1faf2892007-01-29 15:31:35 -08001457 /* copy live node map to join message */
1458 byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
1459
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001460 status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
Wengang Wanga5196ec2010-03-30 12:09:22 +08001461 sizeof(join_msg), node, &join_resp);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001462 if (status < 0 && status != -ENOPROTOOPT) {
Wengang Wanga5196ec2010-03-30 12:09:22 +08001463 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1464 "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
1465 node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001466 goto bail;
1467 }
Joel Becker0f71b7b2008-02-12 14:56:25 -08001468 dlm_query_join_wire_to_packet(join_resp, &packet);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001469
1470 /* -ENOPROTOOPT from the net code means the other side isn't
1471 listening for our message type -- that's fine, it means
1472 his dlm isn't up, so we can consider him a 'yes' but not
1473 joined into the domain. */
1474 if (status == -ENOPROTOOPT) {
1475 status = 0;
1476 *response = JOIN_OK_NO_MAP;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001477 } else if (packet.code == JOIN_DISALLOW ||
1478 packet.code == JOIN_OK_NO_MAP) {
1479 *response = packet.code;
1480 } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001481 mlog(ML_NOTICE,
1482 "This node requested DLM locking protocol %u.%u and "
1483 "filesystem locking protocol %u.%u. At least one of "
1484 "the protocol versions on node %d is not compatible, "
1485 "disconnecting\n",
1486 dlm->dlm_locking_proto.pv_major,
1487 dlm->dlm_locking_proto.pv_minor,
1488 dlm->fs_locking_proto.pv_major,
1489 dlm->fs_locking_proto.pv_minor,
1490 node);
1491 status = -EPROTO;
Joel Becker0f71b7b2008-02-12 14:56:25 -08001492 *response = packet.code;
1493 } else if (packet.code == JOIN_OK) {
1494 *response = packet.code;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001495 /* Use the same locking protocol as the remote node */
Joel Becker0f71b7b2008-02-12 14:56:25 -08001496 dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
1497 dlm->fs_locking_proto.pv_minor = packet.fs_minor;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001498 mlog(0,
1499 "Node %d responds JOIN_OK with DLM locking protocol "
1500 "%u.%u and fs locking protocol %u.%u\n",
1501 node,
1502 dlm->dlm_locking_proto.pv_major,
1503 dlm->dlm_locking_proto.pv_minor,
1504 dlm->fs_locking_proto.pv_major,
1505 dlm->fs_locking_proto.pv_minor);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001506 } else {
1507 status = -EINVAL;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001508 mlog(ML_ERROR, "invalid response %d from node %u\n",
Joel Becker0f71b7b2008-02-12 14:56:25 -08001509 packet.code, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001510 }
1511
1512 mlog(0, "status %d, node %d response is %d\n", status, node,
Joel Becker0f71b7b2008-02-12 14:56:25 -08001513 *response);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001514
1515bail:
1516 return status;
1517}
1518
1519static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
1520 unsigned int node)
1521{
1522 int status;
1523 struct dlm_assert_joined assert_msg;
1524
1525 mlog(0, "Sending join assert to node %u\n", node);
1526
1527 memset(&assert_msg, 0, sizeof(assert_msg));
1528 assert_msg.node_idx = dlm->node_num;
1529 assert_msg.name_len = strlen(dlm->name);
1530 memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
1531
1532 status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1533 &assert_msg, sizeof(assert_msg), node,
1534 NULL);
1535 if (status < 0)
Wengang Wanga5196ec2010-03-30 12:09:22 +08001536 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
1537 "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
1538 node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001539
1540 return status;
1541}
1542
1543static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
1544 unsigned long *node_map)
1545{
1546 int status, node, live;
1547
1548 status = 0;
1549 node = -1;
1550 while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
1551 node + 1)) < O2NM_MAX_NODES) {
1552 if (node == dlm->node_num)
1553 continue;
1554
1555 do {
1556 /* It is very important that this message be
1557 * received so we spin until either the node
1558 * has died or it gets the message. */
1559 status = dlm_send_one_join_assert(dlm, node);
1560
1561 spin_lock(&dlm->spinlock);
1562 live = test_bit(node, dlm->live_nodes_map);
1563 spin_unlock(&dlm->spinlock);
1564
1565 if (status) {
1566 mlog(ML_ERROR, "Error return %d asserting "
1567 "join on node %d\n", status, node);
1568
1569 /* give us some time between errors... */
1570 if (live)
1571 msleep(DLM_DOMAIN_BACKOFF_MS);
1572 }
1573 } while (status && live);
1574 }
1575}
1576
1577struct domain_join_ctxt {
1578 unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1579 unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
1580};
1581
1582static int dlm_should_restart_join(struct dlm_ctxt *dlm,
1583 struct domain_join_ctxt *ctxt,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001584 enum dlm_query_join_response_code response)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001585{
1586 int ret;
1587
1588 if (response == JOIN_DISALLOW) {
1589 mlog(0, "Latest response of disallow -- should restart\n");
1590 return 1;
1591 }
1592
1593 spin_lock(&dlm->spinlock);
1594 /* For now, we restart the process if the node maps have
1595 * changed at all */
1596 ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
1597 sizeof(dlm->live_nodes_map));
1598 spin_unlock(&dlm->spinlock);
1599
1600 if (ret)
1601 mlog(0, "Node maps changed -- should restart\n");
1602
1603 return ret;
1604}
1605
1606static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
1607{
1608 int status = 0, tmpstat, node;
1609 struct domain_join_ctxt *ctxt;
Joel Beckerd24fbcd2008-01-25 17:02:21 -08001610 enum dlm_query_join_response_code response = JOIN_DISALLOW;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001611
Tao Maef6b6892011-02-21 11:10:44 +08001612 mlog(0, "%p", dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001613
Robert P. J. Daycd861282006-12-13 00:34:52 -08001614 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001615 if (!ctxt) {
1616 status = -ENOMEM;
1617 mlog_errno(status);
1618 goto bail;
1619 }
1620
1621 /* group sem locking should work for us here -- we're already
1622 * registered for heartbeat events so filling this should be
1623 * atomic wrt getting those handlers called. */
1624 o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
1625
1626 spin_lock(&dlm->spinlock);
1627 memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
1628
1629 __dlm_set_joining_node(dlm, dlm->node_num);
1630
1631 spin_unlock(&dlm->spinlock);
1632
1633 node = -1;
1634 while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
1635 node + 1)) < O2NM_MAX_NODES) {
1636 if (node == dlm->node_num)
1637 continue;
1638
1639 status = dlm_request_join(dlm, node, &response);
1640 if (status < 0) {
1641 mlog_errno(status);
1642 goto bail;
1643 }
1644
1645 /* Ok, either we got a response or the node doesn't have a
1646 * dlm up. */
1647 if (response == JOIN_OK)
1648 set_bit(node, ctxt->yes_resp_map);
1649
1650 if (dlm_should_restart_join(dlm, ctxt, response)) {
1651 status = -EAGAIN;
1652 goto bail;
1653 }
1654 }
1655
1656 mlog(0, "Yay, done querying nodes!\n");
1657
1658 /* Yay, everyone agree's we can join the domain. My domain is
1659 * comprised of all nodes who were put in the
1660 * yes_resp_map. Copy that into our domain map and send a join
1661 * assert message to clean up everyone elses state. */
1662 spin_lock(&dlm->spinlock);
1663 memcpy(dlm->domain_map, ctxt->yes_resp_map,
1664 sizeof(ctxt->yes_resp_map));
1665 set_bit(dlm->node_num, dlm->domain_map);
1666 spin_unlock(&dlm->spinlock);
1667
Sunil Mushran18cfdf12010-10-07 16:47:03 -07001668 /* Support for global heartbeat and node info was added in 1.1 */
Sunil Mushran4da6dc22011-05-04 10:27:10 -07001669 if (dlm->dlm_locking_proto.pv_major > 1 ||
1670 dlm->dlm_locking_proto.pv_minor > 0) {
Sunil Mushran18cfdf12010-10-07 16:47:03 -07001671 status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
1672 if (status) {
1673 mlog_errno(status);
1674 goto bail;
1675 }
Sunil Mushranea203442010-10-09 10:26:23 -07001676 status = dlm_send_regions(dlm, ctxt->yes_resp_map);
1677 if (status) {
1678 mlog_errno(status);
1679 goto bail;
1680 }
1681 }
1682
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001683 dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
1684
1685 /* Joined state *must* be set before the joining node
1686 * information, otherwise the query_join handler may read no
1687 * current joiner but a state of NEW and tell joining nodes
1688 * we're not in the domain. */
1689 spin_lock(&dlm_domain_lock);
1690 dlm->dlm_state = DLM_CTXT_JOINED;
1691 dlm->num_joins++;
1692 spin_unlock(&dlm_domain_lock);
1693
1694bail:
1695 spin_lock(&dlm->spinlock);
1696 __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
Sunil Mushran8decab32011-07-24 10:23:54 -07001697 if (!status) {
1698 printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001699 __dlm_print_nodes(dlm);
Sunil Mushran8decab32011-07-24 10:23:54 -07001700 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001701 spin_unlock(&dlm->spinlock);
1702
1703 if (ctxt) {
1704 /* Do we need to send a cancel message to any nodes? */
1705 if (status < 0) {
1706 tmpstat = dlm_send_join_cancels(dlm,
1707 ctxt->yes_resp_map,
1708 sizeof(ctxt->yes_resp_map));
1709 if (tmpstat < 0)
1710 mlog_errno(tmpstat);
1711 }
1712 kfree(ctxt);
1713 }
1714
1715 mlog(0, "returning %d\n", status);
1716 return status;
1717}
1718
1719static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
1720{
Sunil Mushran58a31582010-12-14 14:14:29 -08001721 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
1722 o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001723 o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
1724}
1725
1726static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
1727{
1728 int status;
1729
1730 mlog(0, "registering handlers.\n");
1731
1732 o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
1733 dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
Sunil Mushran58a31582010-12-14 14:14:29 -08001734 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001735 if (status)
1736 goto bail;
1737
1738 o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
1739 dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
Sunil Mushran58a31582010-12-14 14:14:29 -08001740 status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001741 if (status)
1742 goto bail;
1743
1744 status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
1745 sizeof(struct dlm_master_request),
1746 dlm_master_request_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001747 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001748 if (status)
1749 goto bail;
1750
1751 status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
1752 sizeof(struct dlm_assert_master),
1753 dlm_assert_master_handler,
Kurt Hackel3b8118c2007-01-17 17:05:53 -08001754 dlm, dlm_assert_master_post_handler,
1755 &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001756 if (status)
1757 goto bail;
1758
1759 status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
1760 sizeof(struct dlm_create_lock),
1761 dlm_create_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001762 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001763 if (status)
1764 goto bail;
1765
1766 status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
1767 DLM_CONVERT_LOCK_MAX_LEN,
1768 dlm_convert_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001769 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001770 if (status)
1771 goto bail;
1772
1773 status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
1774 DLM_UNLOCK_LOCK_MAX_LEN,
1775 dlm_unlock_lock_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001776 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001777 if (status)
1778 goto bail;
1779
1780 status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
1781 DLM_PROXY_AST_MAX_LEN,
1782 dlm_proxy_ast_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001783 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001784 if (status)
1785 goto bail;
1786
1787 status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
1788 sizeof(struct dlm_exit_domain),
1789 dlm_exit_domain_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001790 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001791 if (status)
1792 goto bail;
1793
Kurt Hackelba2bf212006-12-01 14:47:20 -08001794 status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
1795 sizeof(struct dlm_deref_lockres),
1796 dlm_deref_lockres_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001797 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001798 if (status)
1799 goto bail;
1800
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001801 status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
1802 sizeof(struct dlm_migrate_request),
1803 dlm_migrate_request_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001804 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001805 if (status)
1806 goto bail;
1807
1808 status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
1809 DLM_MIG_LOCKRES_MAX_LEN,
1810 dlm_mig_lockres_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001811 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001812 if (status)
1813 goto bail;
1814
1815 status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
1816 sizeof(struct dlm_master_requery),
1817 dlm_master_requery_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001818 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001819 if (status)
1820 goto bail;
1821
1822 status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
1823 sizeof(struct dlm_lock_request),
1824 dlm_request_all_locks_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001825 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001826 if (status)
1827 goto bail;
1828
1829 status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
1830 sizeof(struct dlm_reco_data_done),
1831 dlm_reco_data_done_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001832 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001833 if (status)
1834 goto bail;
1835
1836 status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
1837 sizeof(struct dlm_begin_reco),
1838 dlm_begin_reco_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001839 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001840 if (status)
1841 goto bail;
1842
1843 status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
1844 sizeof(struct dlm_finalize_reco),
1845 dlm_finalize_reco_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08001846 dlm, NULL, &dlm->dlm_domain_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001847 if (status)
1848 goto bail;
1849
Sunil Mushranbddefde2011-05-19 14:34:11 -07001850 status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
1851 sizeof(struct dlm_exit_domain),
1852 dlm_begin_exit_domain_handler,
1853 dlm, NULL, &dlm->dlm_domain_handlers);
1854 if (status)
1855 goto bail;
1856
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001857bail:
1858 if (status)
1859 dlm_unregister_domain_handlers(dlm);
1860
1861 return status;
1862}
1863
1864static int dlm_join_domain(struct dlm_ctxt *dlm)
1865{
1866 int status;
Sunil Mushran0dd82142007-01-29 15:44:27 -08001867 unsigned int backoff;
1868 unsigned int total_backoff = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001869
1870 BUG_ON(!dlm);
1871
1872 mlog(0, "Join domain %s\n", dlm->name);
1873
1874 status = dlm_register_domain_handlers(dlm);
1875 if (status) {
1876 mlog_errno(status);
1877 goto bail;
1878 }
1879
1880 status = dlm_launch_thread(dlm);
1881 if (status < 0) {
1882 mlog_errno(status);
1883 goto bail;
1884 }
1885
1886 status = dlm_launch_recovery_thread(dlm);
1887 if (status < 0) {
1888 mlog_errno(status);
1889 goto bail;
1890 }
1891
Zongxun Wang181a9a02014-04-03 14:46:45 -07001892 status = dlm_debug_init(dlm);
1893 if (status < 0) {
1894 mlog_errno(status);
1895 goto bail;
1896 }
1897
Kurt Hackel3156d262006-05-01 14:39:29 -07001898 dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
1899 if (!dlm->dlm_worker) {
1900 status = -ENOMEM;
1901 mlog_errno(status);
1902 goto bail;
1903 }
1904
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001905 do {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001906 status = dlm_try_to_join_domain(dlm);
1907
1908 /* If we're racing another node to the join, then we
1909 * need to back off temporarily and let them
1910 * complete. */
Sunil Mushran0dd82142007-01-29 15:44:27 -08001911#define DLM_JOIN_TIMEOUT_MSECS 90000
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001912 if (status == -EAGAIN) {
1913 if (signal_pending(current)) {
1914 status = -ERESTARTSYS;
1915 goto bail;
1916 }
1917
Sunil Mushran0dd82142007-01-29 15:44:27 -08001918 if (total_backoff >
1919 msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
1920 status = -ERESTARTSYS;
1921 mlog(ML_NOTICE, "Timed out joining dlm domain "
1922 "%s after %u msecs\n", dlm->name,
1923 jiffies_to_msecs(total_backoff));
1924 goto bail;
1925 }
1926
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001927 /*
1928 * <chip> After you!
1929 * <dale> No, after you!
1930 * <chip> I insist!
1931 * <dale> But you first!
1932 * ...
1933 */
1934 backoff = (unsigned int)(jiffies & 0x3);
1935 backoff *= DLM_DOMAIN_BACKOFF_MS;
Sunil Mushran0dd82142007-01-29 15:44:27 -08001936 total_backoff += backoff;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001937 mlog(0, "backoff %d\n", backoff);
1938 msleep(backoff);
1939 }
1940 } while (status == -EAGAIN);
1941
1942 if (status < 0) {
1943 mlog_errno(status);
1944 goto bail;
1945 }
1946
1947 status = 0;
1948bail:
1949 wake_up(&dlm_domain_events);
1950
1951 if (status) {
1952 dlm_unregister_domain_handlers(dlm);
Sunil Mushran007dce52008-03-10 15:16:23 -07001953 dlm_debug_shutdown(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001954 dlm_complete_thread(dlm);
1955 dlm_complete_recovery_thread(dlm);
Kurt Hackel3156d262006-05-01 14:39:29 -07001956 dlm_destroy_dlm_worker(dlm);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001957 }
1958
1959 return status;
1960}
1961
1962static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
1963 u32 key)
1964{
1965 int i;
Sunil Mushran6325b4a2008-03-10 15:16:22 -07001966 int ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001967 struct dlm_ctxt *dlm = NULL;
1968
Robert P. J. Daycd861282006-12-13 00:34:52 -08001969 dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001970 if (!dlm) {
1971 mlog_errno(-ENOMEM);
1972 goto leave;
1973 }
1974
Julia Lawall316ce2b2010-05-14 21:30:48 +02001975 dlm->name = kstrdup(domain, GFP_KERNEL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001976 if (dlm->name == NULL) {
1977 mlog_errno(-ENOMEM);
1978 kfree(dlm);
1979 dlm = NULL;
1980 goto leave;
1981 }
1982
Daniel Phillips03d864c2006-03-10 18:08:16 -08001983 dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
Mark Fasheh81f20942006-02-28 17:31:22 -08001984 if (!dlm->lockres_hash) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001985 mlog_errno(-ENOMEM);
1986 kfree(dlm->name);
1987 kfree(dlm);
1988 dlm = NULL;
1989 goto leave;
1990 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001991
Daniel Phillips03d864c2006-03-10 18:08:16 -08001992 for (i = 0; i < DLM_HASH_BUCKETS; i++)
1993 INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001994
Sunil Mushrane2b66dd2009-02-26 15:00:40 -08001995 dlm->master_hash = (struct hlist_head **)
1996 dlm_alloc_pagevec(DLM_HASH_PAGES);
1997 if (!dlm->master_hash) {
1998 mlog_errno(-ENOMEM);
1999 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2000 kfree(dlm->name);
2001 kfree(dlm);
2002 dlm = NULL;
2003 goto leave;
2004 }
2005
2006 for (i = 0; i < DLM_HASH_BUCKETS; i++)
2007 INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
2008
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002009 dlm->key = key;
2010 dlm->node_num = o2nm_this_node();
2011
Sunil Mushran6325b4a2008-03-10 15:16:22 -07002012 ret = dlm_create_debugfs_subroot(dlm);
2013 if (ret < 0) {
Sunil Mushrane2b66dd2009-02-26 15:00:40 -08002014 dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
Sunil Mushran6325b4a2008-03-10 15:16:22 -07002015 dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
2016 kfree(dlm->name);
2017 kfree(dlm);
2018 dlm = NULL;
2019 goto leave;
2020 }
2021
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002022 spin_lock_init(&dlm->spinlock);
2023 spin_lock_init(&dlm->master_lock);
2024 spin_lock_init(&dlm->ast_lock);
Sunil Mushranb0d4f8172008-12-16 15:49:22 -08002025 spin_lock_init(&dlm->track_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002026 INIT_LIST_HEAD(&dlm->list);
2027 INIT_LIST_HEAD(&dlm->dirty_list);
2028 INIT_LIST_HEAD(&dlm->reco.resources);
2029 INIT_LIST_HEAD(&dlm->reco.received);
2030 INIT_LIST_HEAD(&dlm->reco.node_data);
2031 INIT_LIST_HEAD(&dlm->purge_list);
2032 INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
Sunil Mushran29576f82008-03-10 15:16:21 -07002033 INIT_LIST_HEAD(&dlm->tracking_list);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002034 dlm->reco.state = 0;
2035
2036 INIT_LIST_HEAD(&dlm->pending_asts);
2037 INIT_LIST_HEAD(&dlm->pending_basts);
2038
2039 mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
2040 dlm->recovery_map, &(dlm->recovery_map[0]));
2041
2042 memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
2043 memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
2044 memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
2045
2046 dlm->dlm_thread_task = NULL;
2047 dlm->dlm_reco_thread_task = NULL;
Kurt Hackel3156d262006-05-01 14:39:29 -07002048 dlm->dlm_worker = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002049 init_waitqueue_head(&dlm->dlm_thread_wq);
2050 init_waitqueue_head(&dlm->dlm_reco_thread_wq);
2051 init_waitqueue_head(&dlm->reco.event);
2052 init_waitqueue_head(&dlm->ast_wq);
2053 init_waitqueue_head(&dlm->migration_wq);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002054 INIT_LIST_HEAD(&dlm->mle_hb_events);
2055
2056 dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
2057 init_waitqueue_head(&dlm->dlm_join_events);
2058
2059 dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
2060 dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002061
Sunil Mushran68007912009-02-26 15:00:44 -08002062 atomic_set(&dlm->res_tot_count, 0);
2063 atomic_set(&dlm->res_cur_count, 0);
Sunil Mushran2041d8f2009-02-26 15:00:43 -08002064 for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
2065 atomic_set(&dlm->mle_tot_count[i], 0);
2066 atomic_set(&dlm->mle_cur_count[i], 0);
2067 }
2068
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002069 spin_lock_init(&dlm->work_lock);
2070 INIT_LIST_HEAD(&dlm->work_list);
David Howellsc4028952006-11-22 14:57:56 +00002071 INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002072
2073 kref_init(&dlm->dlm_refs);
2074 dlm->dlm_state = DLM_CTXT_NEW;
2075
2076 INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
2077
2078 mlog(0, "context init: refcount %u\n",
2079 atomic_read(&dlm->dlm_refs.refcount));
2080
2081leave:
2082 return dlm;
2083}
2084
2085/*
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002086 * Compare a requested locking protocol version against the current one.
2087 *
2088 * If the major numbers are different, they are incompatible.
2089 * If the current minor is greater than the request, they are incompatible.
2090 * If the current minor is less than or equal to the request, they are
2091 * compatible, and the requester should run at the current minor version.
2092 */
2093static int dlm_protocol_compare(struct dlm_protocol_version *existing,
2094 struct dlm_protocol_version *request)
2095{
2096 if (existing->pv_major != request->pv_major)
2097 return 1;
2098
2099 if (existing->pv_minor > request->pv_minor)
2100 return 1;
2101
2102 if (existing->pv_minor < request->pv_minor)
2103 request->pv_minor = existing->pv_minor;
2104
2105 return 0;
2106}
2107
2108/*
2109 * dlm_register_domain: one-time setup per "domain".
2110 *
2111 * The filesystem passes in the requested locking version via proto.
2112 * If registration was successful, proto will contain the negotiated
2113 * locking protocol.
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002114 */
2115struct dlm_ctxt * dlm_register_domain(const char *domain,
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002116 u32 key,
2117 struct dlm_protocol_version *fs_proto)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002118{
2119 int ret;
2120 struct dlm_ctxt *dlm = NULL;
2121 struct dlm_ctxt *new_ctxt = NULL;
2122
Dan Carpentere3723572010-07-10 16:33:36 +02002123 if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002124 ret = -ENAMETOOLONG;
2125 mlog(ML_ERROR, "domain name length too long\n");
2126 goto leave;
2127 }
2128
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002129 mlog(0, "register called for domain \"%s\"\n", domain);
2130
2131retry:
2132 dlm = NULL;
2133 if (signal_pending(current)) {
2134 ret = -ERESTARTSYS;
2135 mlog_errno(ret);
2136 goto leave;
2137 }
2138
2139 spin_lock(&dlm_domain_lock);
2140
2141 dlm = __dlm_lookup_domain(domain);
2142 if (dlm) {
2143 if (dlm->dlm_state != DLM_CTXT_JOINED) {
2144 spin_unlock(&dlm_domain_lock);
2145
2146 mlog(0, "This ctxt is not joined yet!\n");
2147 wait_event_interruptible(dlm_domain_events,
2148 dlm_wait_on_domain_helper(
2149 domain));
2150 goto retry;
2151 }
2152
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002153 if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
Julia Lawall64692722010-05-26 17:58:53 +02002154 spin_unlock(&dlm_domain_lock);
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002155 mlog(ML_ERROR,
2156 "Requested locking protocol version is not "
2157 "compatible with already registered domain "
2158 "\"%s\"\n", domain);
2159 ret = -EPROTO;
2160 goto leave;
2161 }
2162
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002163 __dlm_get(dlm);
2164 dlm->num_joins++;
2165
2166 spin_unlock(&dlm_domain_lock);
2167
2168 ret = 0;
2169 goto leave;
2170 }
2171
2172 /* doesn't exist */
2173 if (!new_ctxt) {
2174 spin_unlock(&dlm_domain_lock);
2175
2176 new_ctxt = dlm_alloc_ctxt(domain, key);
2177 if (new_ctxt)
2178 goto retry;
2179
2180 ret = -ENOMEM;
2181 mlog_errno(ret);
2182 goto leave;
2183 }
2184
2185 /* a little variable switch-a-roo here... */
2186 dlm = new_ctxt;
2187 new_ctxt = NULL;
2188
2189 /* add the new domain */
2190 list_add_tail(&dlm->list, &dlm_domains);
2191 spin_unlock(&dlm_domain_lock);
2192
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002193 /*
2194 * Pass the locking protocol version into the join. If the join
2195 * succeeds, it will have the negotiated protocol set.
2196 */
2197 dlm->dlm_locking_proto = dlm_protocol;
2198 dlm->fs_locking_proto = *fs_proto;
2199
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002200 ret = dlm_join_domain(dlm);
2201 if (ret) {
2202 mlog_errno(ret);
2203 dlm_put(dlm);
2204 goto leave;
2205 }
2206
Joel Beckerd24fbcd2008-01-25 17:02:21 -08002207 /* Tell the caller what locking protocol we negotiated */
2208 *fs_proto = dlm->fs_locking_proto;
2209
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002210 ret = 0;
2211leave:
2212 if (new_ctxt)
2213 dlm_free_ctxt_mem(new_ctxt);
2214
2215 if (ret < 0)
2216 dlm = ERR_PTR(ret);
2217
2218 return dlm;
2219}
2220EXPORT_SYMBOL_GPL(dlm_register_domain);
2221
2222static LIST_HEAD(dlm_join_handlers);
2223
2224static void dlm_unregister_net_handlers(void)
2225{
2226 o2net_unregister_handler_list(&dlm_join_handlers);
2227}
2228
2229static int dlm_register_net_handlers(void)
2230{
2231 int status = 0;
2232
2233 status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
2234 sizeof(struct dlm_query_join_request),
2235 dlm_query_join_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08002236 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002237 if (status)
2238 goto bail;
2239
2240 status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
2241 sizeof(struct dlm_assert_joined),
2242 dlm_assert_joined_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08002243 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002244 if (status)
2245 goto bail;
2246
2247 status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
2248 sizeof(struct dlm_cancel_join),
2249 dlm_cancel_join_handler,
Kurt Hackeld74c9802007-01-17 17:04:25 -08002250 NULL, NULL, &dlm_join_handlers);
Sunil Mushranea203442010-10-09 10:26:23 -07002251 if (status)
2252 goto bail;
2253
2254 status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
2255 sizeof(struct dlm_query_region),
2256 dlm_query_region_handler,
2257 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002258
Sunil Mushran18cfdf12010-10-07 16:47:03 -07002259 if (status)
2260 goto bail;
2261
2262 status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
2263 sizeof(struct dlm_query_nodeinfo),
2264 dlm_query_nodeinfo_handler,
2265 NULL, NULL, &dlm_join_handlers);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002266bail:
2267 if (status < 0)
2268 dlm_unregister_net_handlers();
2269
2270 return status;
2271}
2272
2273/* Domain eviction callback handling.
2274 *
2275 * The file system requires notification of node death *before* the
2276 * dlm completes it's recovery work, otherwise it may be able to
2277 * acquire locks on resources requiring recovery. Since the dlm can
2278 * evict a node from it's domain *before* heartbeat fires, a similar
2279 * mechanism is required. */
2280
2281/* Eviction is not expected to happen often, so a per-domain lock is
2282 * not necessary. Eviction callbacks are allowed to sleep for short
2283 * periods of time. */
2284static DECLARE_RWSEM(dlm_callback_sem);
2285
2286void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
2287 int node_num)
2288{
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002289 struct dlm_eviction_cb *cb;
2290
2291 down_read(&dlm_callback_sem);
Dong Fangdf53cd32013-09-11 14:19:50 -07002292 list_for_each_entry(cb, &dlm->dlm_eviction_callbacks, ec_item) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002293 cb->ec_func(node_num, cb->ec_data);
2294 }
2295 up_read(&dlm_callback_sem);
2296}
2297
2298void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
2299 dlm_eviction_func *f,
2300 void *data)
2301{
2302 INIT_LIST_HEAD(&cb->ec_item);
2303 cb->ec_func = f;
2304 cb->ec_data = data;
2305}
2306EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
2307
2308void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
2309 struct dlm_eviction_cb *cb)
2310{
2311 down_write(&dlm_callback_sem);
2312 list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
2313 up_write(&dlm_callback_sem);
2314}
2315EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
2316
2317void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
2318{
2319 down_write(&dlm_callback_sem);
2320 list_del_init(&cb->ec_item);
2321 up_write(&dlm_callback_sem);
2322}
2323EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
2324
2325static int __init dlm_init(void)
2326{
2327 int status;
2328
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002329 status = dlm_init_mle_cache();
Sunil Mushran12eb0032008-03-10 15:16:19 -07002330 if (status) {
2331 mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
Sunil Mushran724bdca2008-03-10 15:16:20 -07002332 goto error;
2333 }
2334
2335 status = dlm_init_master_caches();
2336 if (status) {
2337 mlog(ML_ERROR, "Could not create o2dlm_lockres and "
2338 "o2dlm_lockname slabcaches\n");
2339 goto error;
2340 }
2341
2342 status = dlm_init_lock_cache();
2343 if (status) {
2344 mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
2345 goto error;
Sunil Mushran12eb0032008-03-10 15:16:19 -07002346 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002347
2348 status = dlm_register_net_handlers();
2349 if (status) {
Sunil Mushran724bdca2008-03-10 15:16:20 -07002350 mlog(ML_ERROR, "Unable to register network handlers\n");
2351 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002352 }
2353
Sunil Mushran6325b4a2008-03-10 15:16:22 -07002354 status = dlm_create_debugfs_root();
2355 if (status)
2356 goto error;
2357
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002358 return 0;
Sunil Mushran724bdca2008-03-10 15:16:20 -07002359error:
Sunil Mushran6325b4a2008-03-10 15:16:22 -07002360 dlm_unregister_net_handlers();
Sunil Mushran724bdca2008-03-10 15:16:20 -07002361 dlm_destroy_lock_cache();
2362 dlm_destroy_master_caches();
2363 dlm_destroy_mle_cache();
2364 return -1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002365}
2366
2367static void __exit dlm_exit (void)
2368{
Sunil Mushran6325b4a2008-03-10 15:16:22 -07002369 dlm_destroy_debugfs_root();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002370 dlm_unregister_net_handlers();
Sunil Mushran724bdca2008-03-10 15:16:20 -07002371 dlm_destroy_lock_cache();
2372 dlm_destroy_master_caches();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002373 dlm_destroy_mle_cache();
2374}
2375
2376MODULE_AUTHOR("Oracle");
2377MODULE_LICENSE("GPL");
Goldwyn Rodriguesff8fb332014-01-21 15:48:20 -08002378MODULE_DESCRIPTION("OCFS2 Distributed Lock Management");
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002379
2380module_init(dlm_init);
2381module_exit(dlm_exit);