blob: d07ddbe4b2836d3e3e6d0f002808d8c36ef6c591 [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 * dlmast.c
5 *
6 * AST and BAST functionality for local and remote nodes
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
28#include <linux/module.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/highmem.h>
33#include <linux/utsname.h>
34#include <linux/init.h>
35#include <linux/sysctl.h>
36#include <linux/random.h>
37#include <linux/blkdev.h>
38#include <linux/socket.h>
39#include <linux/inet.h>
40#include <linux/spinlock.h>
41
42
43#include "cluster/heartbeat.h"
44#include "cluster/nodemanager.h"
45#include "cluster/tcp.h"
Kurt Hackel6714d8e2005-12-15 14:31:23 -080046
47#include "dlmapi.h"
48#include "dlmcommon.h"
49
50#define MLOG_MASK_PREFIX ML_DLM
51#include "cluster/masklog.h"
52
53static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
54 struct dlm_lock *lock);
55static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
56
57/* Should be called as an ast gets queued to see if the new
58 * lock level will obsolete a pending bast.
59 * For example, if dlm_thread queued a bast for an EX lock that
60 * was blocking another EX, but before sending the bast the
61 * lock owner downconverted to NL, the bast is now obsolete.
62 * Only the ast should be sent.
63 * This is needed because the lock and convert paths can queue
64 * asts out-of-band (not waiting for dlm_thread) in order to
65 * allow for LKM_NOQUEUE to get immediate responses. */
66static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
67{
68 assert_spin_locked(&dlm->ast_lock);
69 assert_spin_locked(&lock->spinlock);
70
71 if (lock->ml.highest_blocked == LKM_IVMODE)
72 return 0;
73 BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
74
75 if (lock->bast_pending &&
76 list_empty(&lock->bast_list))
77 /* old bast already sent, ok */
78 return 0;
79
80 if (lock->ml.type == LKM_EXMODE)
81 /* EX blocks anything left, any bast still valid */
82 return 0;
83 else if (lock->ml.type == LKM_NLMODE)
84 /* NL blocks nothing, no reason to send any bast, cancel it */
85 return 1;
86 else if (lock->ml.highest_blocked != LKM_EXMODE)
87 /* PR only blocks EX */
88 return 1;
89
90 return 0;
91}
92
93static void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
94{
95 mlog_entry_void();
96
97 BUG_ON(!dlm);
98 BUG_ON(!lock);
99
100 assert_spin_locked(&dlm->ast_lock);
101 if (!list_empty(&lock->ast_list)) {
102 mlog(ML_ERROR, "ast list not empty!! pending=%d, newlevel=%d\n",
103 lock->ast_pending, lock->ml.type);
104 BUG();
105 }
106 BUG_ON(!list_empty(&lock->ast_list));
107 if (lock->ast_pending)
108 mlog(0, "lock has an ast getting flushed right now\n");
109
110 /* putting lock on list, add a ref */
111 dlm_lock_get(lock);
112 spin_lock(&lock->spinlock);
113
114 /* check to see if this ast obsoletes the bast */
115 if (dlm_should_cancel_bast(dlm, lock)) {
116 struct dlm_lock_resource *res = lock->lockres;
117 mlog(0, "%s: cancelling bast for %.*s\n",
118 dlm->name, res->lockname.len, res->lockname.name);
119 lock->bast_pending = 0;
120 list_del_init(&lock->bast_list);
121 lock->ml.highest_blocked = LKM_IVMODE;
122 /* removing lock from list, remove a ref. guaranteed
123 * this won't be the last ref because of the get above,
124 * so res->spinlock will not be taken here */
125 dlm_lock_put(lock);
126 /* free up the reserved bast that we are cancelling.
127 * guaranteed that this will not be the last reserved
128 * ast because *both* an ast and a bast were reserved
129 * to get to this point. the res->spinlock will not be
130 * taken here */
131 dlm_lockres_release_ast(dlm, res);
132 }
133 list_add_tail(&lock->ast_list, &dlm->pending_asts);
134 lock->ast_pending = 1;
135 spin_unlock(&lock->spinlock);
136}
137
138void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
139{
140 mlog_entry_void();
141
142 BUG_ON(!dlm);
143 BUG_ON(!lock);
144
145 spin_lock(&dlm->ast_lock);
146 __dlm_queue_ast(dlm, lock);
147 spin_unlock(&dlm->ast_lock);
148}
149
150
151static void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
152{
153 mlog_entry_void();
154
155 BUG_ON(!dlm);
156 BUG_ON(!lock);
157 assert_spin_locked(&dlm->ast_lock);
158
159 BUG_ON(!list_empty(&lock->bast_list));
160 if (lock->bast_pending)
161 mlog(0, "lock has a bast getting flushed right now\n");
162
163 /* putting lock on list, add a ref */
164 dlm_lock_get(lock);
165 spin_lock(&lock->spinlock);
166 list_add_tail(&lock->bast_list, &dlm->pending_basts);
167 lock->bast_pending = 1;
168 spin_unlock(&lock->spinlock);
169}
170
171void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
172{
173 mlog_entry_void();
174
175 BUG_ON(!dlm);
176 BUG_ON(!lock);
177
178 spin_lock(&dlm->ast_lock);
179 __dlm_queue_bast(dlm, lock);
180 spin_unlock(&dlm->ast_lock);
181}
182
183static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
184 struct dlm_lock *lock)
185{
186 struct dlm_lockstatus *lksb = lock->lksb;
187 BUG_ON(!lksb);
188
189 /* only updates if this node masters the lockres */
190 if (res->owner == dlm->node_num) {
191
192 spin_lock(&res->spinlock);
193 /* check the lksb flags for the direction */
194 if (lksb->flags & DLM_LKSB_GET_LVB) {
195 mlog(0, "getting lvb from lockres for %s node\n",
196 lock->ml.node == dlm->node_num ? "master" :
197 "remote");
198 memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800199 }
Mark Fashehc0a85202006-04-27 19:07:45 -0700200 /* Do nothing for lvb put requests - they should be done in
201 * place when the lock is downconverted - otherwise we risk
202 * racing gets and puts which could result in old lvb data
203 * being propagated. We leave the put flag set and clear it
204 * here. In the future we might want to clear it at the time
205 * the put is actually done.
206 */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800207 spin_unlock(&res->spinlock);
208 }
209
210 /* reset any lvb flags on the lksb */
211 lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
212}
213
214void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
215 struct dlm_lock *lock)
216{
217 dlm_astlockfunc_t *fn;
218 struct dlm_lockstatus *lksb;
219
220 mlog_entry_void();
221
222 lksb = lock->lksb;
223 fn = lock->ast;
224 BUG_ON(lock->ml.node != dlm->node_num);
225
226 dlm_update_lvb(dlm, res, lock);
227 (*fn)(lock->astdata);
228}
229
230
231int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
232 struct dlm_lock *lock)
233{
234 int ret;
235 struct dlm_lockstatus *lksb;
236 int lksbflags;
237
238 mlog_entry_void();
239
240 lksb = lock->lksb;
241 BUG_ON(lock->ml.node == dlm->node_num);
242
243 lksbflags = lksb->flags;
244 dlm_update_lvb(dlm, res, lock);
245
246 /* lock request came from another node
247 * go do the ast over there */
248 ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
249 return ret;
250}
251
252void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
253 struct dlm_lock *lock, int blocked_type)
254{
255 dlm_bastlockfunc_t *fn = lock->bast;
256
257 mlog_entry_void();
258 BUG_ON(lock->ml.node != dlm->node_num);
259
260 (*fn)(lock->astdata, blocked_type);
261}
262
263
264
Kurt Hackeld74c9802007-01-17 17:04:25 -0800265int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
266 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800267{
268 int ret;
269 unsigned int locklen;
270 struct dlm_ctxt *dlm = data;
271 struct dlm_lock_resource *res = NULL;
272 struct dlm_lock *lock = NULL;
273 struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
274 char *name;
275 struct list_head *iter, *head=NULL;
276 u64 cookie;
277 u32 flags;
Sunil Mushran57dff262008-12-16 15:49:20 -0800278 u8 node;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800279
280 if (!dlm_grab(dlm)) {
281 dlm_error(DLM_REJECTED);
282 return DLM_REJECTED;
283 }
284
285 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
286 "Domain %s not fully joined!\n", dlm->name);
287
288 name = past->name;
289 locklen = past->namelen;
Sunil Mushran57dff262008-12-16 15:49:20 -0800290 cookie = past->cookie;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800291 flags = be32_to_cpu(past->flags);
Sunil Mushran57dff262008-12-16 15:49:20 -0800292 node = past->node_idx;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800293
294 if (locklen > DLM_LOCKID_NAME_MAX) {
295 ret = DLM_IVBUFLEN;
Sunil Mushran57dff262008-12-16 15:49:20 -0800296 mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
297 "handler!\n", locklen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800298 goto leave;
299 }
300
301 if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
302 (LKM_PUT_LVB|LKM_GET_LVB)) {
Sunil Mushran57dff262008-12-16 15:49:20 -0800303 mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
304 flags);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800305 ret = DLM_BADARGS;
306 goto leave;
307 }
308
309 mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
310 (flags & LKM_GET_LVB ? "get lvb" : "none"));
311
312 mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
313
314 if (past->type != DLM_AST &&
315 past->type != DLM_BAST) {
Kurt Hackel29004852006-03-02 16:43:36 -0800316 mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
Sunil Mushran57dff262008-12-16 15:49:20 -0800317 "name=%.*s, node=%u\n", past->type,
318 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
319 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
320 locklen, name, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800321 ret = DLM_IVLOCKID;
322 goto leave;
323 }
324
325 res = dlm_lookup_lockres(dlm, name, locklen);
326 if (!res) {
Sunil Mushran57dff262008-12-16 15:49:20 -0800327 mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
328 "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
329 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
330 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
331 locklen, name, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800332 ret = DLM_IVLOCKID;
333 goto leave;
334 }
335
336 /* cannot get a proxy ast message if this node owns it */
337 BUG_ON(res->owner == dlm->node_num);
338
339 mlog(0, "lockres %.*s\n", res->lockname.len, res->lockname.name);
340
341 spin_lock(&res->spinlock);
342 if (res->state & DLM_LOCK_RES_RECOVERING) {
Sunil Mushran57dff262008-12-16 15:49:20 -0800343 mlog(0, "Responding with DLM_RECOVERING!\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800344 ret = DLM_RECOVERING;
345 goto unlock_out;
346 }
347 if (res->state & DLM_LOCK_RES_MIGRATING) {
Sunil Mushran57dff262008-12-16 15:49:20 -0800348 mlog(0, "Responding with DLM_MIGRATING!\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800349 ret = DLM_MIGRATING;
350 goto unlock_out;
351 }
352 /* try convert queue for both ast/bast */
353 head = &res->converting;
354 lock = NULL;
355 list_for_each(iter, head) {
356 lock = list_entry (iter, struct dlm_lock, list);
Sunil Mushran57dff262008-12-16 15:49:20 -0800357 if (lock->ml.cookie == cookie)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800358 goto do_ast;
359 }
360
361 /* if not on convert, try blocked for ast, granted for bast */
362 if (past->type == DLM_AST)
363 head = &res->blocked;
364 else
365 head = &res->granted;
366
367 list_for_each(iter, head) {
368 lock = list_entry (iter, struct dlm_lock, list);
Sunil Mushran57dff262008-12-16 15:49:20 -0800369 if (lock->ml.cookie == cookie)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800370 goto do_ast;
371 }
372
Sunil Mushran57dff262008-12-16 15:49:20 -0800373 mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
374 "node=%u\n", past->type == DLM_AST ? "" : "b",
375 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
376 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
377 locklen, name, node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800378
379 ret = DLM_NORMAL;
380unlock_out:
381 spin_unlock(&res->spinlock);
382 goto leave;
383
384do_ast:
385 ret = DLM_NORMAL;
386 if (past->type == DLM_AST) {
387 /* do not alter lock refcount. switching lists. */
Akinobu Mitaf1166292006-06-26 00:24:46 -0700388 list_move_tail(&lock->list, &res->granted);
Sunil Mushran57dff262008-12-16 15:49:20 -0800389 mlog(0, "ast: Adding to granted list... type=%d, "
390 "convert_type=%d\n", lock->ml.type, lock->ml.convert_type);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800391 if (lock->ml.convert_type != LKM_IVMODE) {
392 lock->ml.type = lock->ml.convert_type;
393 lock->ml.convert_type = LKM_IVMODE;
394 } else {
395 // should already be there....
396 }
397
398 lock->lksb->status = DLM_NORMAL;
399
400 /* if we requested the lvb, fetch it into our lksb now */
401 if (flags & LKM_GET_LVB) {
402 BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
403 memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
404 }
405 }
406 spin_unlock(&res->spinlock);
407
408 if (past->type == DLM_AST)
409 dlm_do_local_ast(dlm, res, lock);
410 else
411 dlm_do_local_bast(dlm, res, lock, past->blocked_type);
412
413leave:
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800414 if (res)
415 dlm_lockres_put(res);
416
417 dlm_put(dlm);
418 return ret;
419}
420
421
422
423int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
424 struct dlm_lock *lock, int msg_type,
425 int blocked_type, int flags)
426{
427 int ret = 0;
428 struct dlm_proxy_ast past;
429 struct kvec vec[2];
430 size_t veclen = 1;
431 int status;
432
433 mlog_entry("res %.*s, to=%u, type=%d, blocked_type=%d\n",
434 res->lockname.len, res->lockname.name, lock->ml.node,
435 msg_type, blocked_type);
436
437 memset(&past, 0, sizeof(struct dlm_proxy_ast));
438 past.node_idx = dlm->node_num;
439 past.type = msg_type;
440 past.blocked_type = blocked_type;
441 past.namelen = res->lockname.len;
442 memcpy(past.name, res->lockname.name, past.namelen);
443 past.cookie = lock->ml.cookie;
444
445 vec[0].iov_len = sizeof(struct dlm_proxy_ast);
446 vec[0].iov_base = &past;
447 if (flags & DLM_LKSB_GET_LVB) {
448 mlog(0, "returning requested LVB data\n");
449 be32_add_cpu(&past.flags, LKM_GET_LVB);
450 vec[1].iov_len = DLM_LVB_LEN;
451 vec[1].iov_base = lock->lksb->lvb;
452 veclen++;
453 }
454
455 ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
456 lock->ml.node, &status);
457 if (ret < 0)
458 mlog_errno(ret);
459 else {
460 if (status == DLM_RECOVERING) {
461 mlog(ML_ERROR, "sent AST to node %u, it thinks this "
462 "node is dead!\n", lock->ml.node);
463 BUG();
464 } else if (status == DLM_MIGRATING) {
465 mlog(ML_ERROR, "sent AST to node %u, it returned "
466 "DLM_MIGRATING!\n", lock->ml.node);
467 BUG();
Mark Fashehe2c73692006-09-08 11:37:32 -0700468 } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800469 mlog(ML_ERROR, "AST to node %u returned %d!\n",
470 lock->ml.node, status);
471 /* ignore it */
472 }
473 ret = 0;
474 }
475 return ret;
476}