Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[deliverable/linux.git] / fs / ocfs2 / dlm / dlmast.c
1 /* -*- 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"
46
47 #include "dlmapi.h"
48 #include "dlmcommon.h"
49
50 #define MLOG_MASK_PREFIX ML_DLM
51 #include "cluster/masklog.h"
52
53 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
54 struct dlm_lock *lock);
55 static 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. */
66 static 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
93 static 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 if (lock->ast_pending)
107 mlog(0, "lock has an ast getting flushed right now\n");
108
109 /* putting lock on list, add a ref */
110 dlm_lock_get(lock);
111 spin_lock(&lock->spinlock);
112
113 /* check to see if this ast obsoletes the bast */
114 if (dlm_should_cancel_bast(dlm, lock)) {
115 struct dlm_lock_resource *res = lock->lockres;
116 mlog(0, "%s: cancelling bast for %.*s\n",
117 dlm->name, res->lockname.len, res->lockname.name);
118 lock->bast_pending = 0;
119 list_del_init(&lock->bast_list);
120 lock->ml.highest_blocked = LKM_IVMODE;
121 /* removing lock from list, remove a ref. guaranteed
122 * this won't be the last ref because of the get above,
123 * so res->spinlock will not be taken here */
124 dlm_lock_put(lock);
125 /* free up the reserved bast that we are cancelling.
126 * guaranteed that this will not be the last reserved
127 * ast because *both* an ast and a bast were reserved
128 * to get to this point. the res->spinlock will not be
129 * taken here */
130 dlm_lockres_release_ast(dlm, res);
131 }
132 list_add_tail(&lock->ast_list, &dlm->pending_asts);
133 lock->ast_pending = 1;
134 spin_unlock(&lock->spinlock);
135 }
136
137 void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
138 {
139 mlog_entry_void();
140
141 BUG_ON(!dlm);
142 BUG_ON(!lock);
143
144 spin_lock(&dlm->ast_lock);
145 __dlm_queue_ast(dlm, lock);
146 spin_unlock(&dlm->ast_lock);
147 }
148
149
150 static void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
151 {
152 mlog_entry_void();
153
154 BUG_ON(!dlm);
155 BUG_ON(!lock);
156 assert_spin_locked(&dlm->ast_lock);
157
158 BUG_ON(!list_empty(&lock->bast_list));
159 if (lock->bast_pending)
160 mlog(0, "lock has a bast getting flushed right now\n");
161
162 /* putting lock on list, add a ref */
163 dlm_lock_get(lock);
164 spin_lock(&lock->spinlock);
165 list_add_tail(&lock->bast_list, &dlm->pending_basts);
166 lock->bast_pending = 1;
167 spin_unlock(&lock->spinlock);
168 }
169
170 void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
171 {
172 mlog_entry_void();
173
174 BUG_ON(!dlm);
175 BUG_ON(!lock);
176
177 spin_lock(&dlm->ast_lock);
178 __dlm_queue_bast(dlm, lock);
179 spin_unlock(&dlm->ast_lock);
180 }
181
182 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
183 struct dlm_lock *lock)
184 {
185 struct dlm_lockstatus *lksb = lock->lksb;
186 BUG_ON(!lksb);
187
188 /* only updates if this node masters the lockres */
189 if (res->owner == dlm->node_num) {
190
191 spin_lock(&res->spinlock);
192 /* check the lksb flags for the direction */
193 if (lksb->flags & DLM_LKSB_GET_LVB) {
194 mlog(0, "getting lvb from lockres for %s node\n",
195 lock->ml.node == dlm->node_num ? "master" :
196 "remote");
197 memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
198 }
199 /* Do nothing for lvb put requests - they should be done in
200 * place when the lock is downconverted - otherwise we risk
201 * racing gets and puts which could result in old lvb data
202 * being propagated. We leave the put flag set and clear it
203 * here. In the future we might want to clear it at the time
204 * the put is actually done.
205 */
206 spin_unlock(&res->spinlock);
207 }
208
209 /* reset any lvb flags on the lksb */
210 lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
211 }
212
213 void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
214 struct dlm_lock *lock)
215 {
216 dlm_astlockfunc_t *fn;
217 struct dlm_lockstatus *lksb;
218
219 mlog_entry_void();
220
221 lksb = lock->lksb;
222 fn = lock->ast;
223 BUG_ON(lock->ml.node != dlm->node_num);
224
225 dlm_update_lvb(dlm, res, lock);
226 (*fn)(lock->astdata);
227 }
228
229
230 int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
231 struct dlm_lock *lock)
232 {
233 int ret;
234 struct dlm_lockstatus *lksb;
235 int lksbflags;
236
237 mlog_entry_void();
238
239 lksb = lock->lksb;
240 BUG_ON(lock->ml.node == dlm->node_num);
241
242 lksbflags = lksb->flags;
243 dlm_update_lvb(dlm, res, lock);
244
245 /* lock request came from another node
246 * go do the ast over there */
247 ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
248 return ret;
249 }
250
251 void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
252 struct dlm_lock *lock, int blocked_type)
253 {
254 dlm_bastlockfunc_t *fn = lock->bast;
255
256 mlog_entry_void();
257 BUG_ON(lock->ml.node != dlm->node_num);
258
259 (*fn)(lock->astdata, blocked_type);
260 }
261
262
263
264 int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
265 void **ret_data)
266 {
267 int ret;
268 unsigned int locklen;
269 struct dlm_ctxt *dlm = data;
270 struct dlm_lock_resource *res = NULL;
271 struct dlm_lock *lock = NULL;
272 struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
273 char *name;
274 struct list_head *iter, *head=NULL;
275 u64 cookie;
276 u32 flags;
277 u8 node;
278
279 if (!dlm_grab(dlm)) {
280 dlm_error(DLM_REJECTED);
281 return DLM_REJECTED;
282 }
283
284 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
285 "Domain %s not fully joined!\n", dlm->name);
286
287 name = past->name;
288 locklen = past->namelen;
289 cookie = past->cookie;
290 flags = be32_to_cpu(past->flags);
291 node = past->node_idx;
292
293 if (locklen > DLM_LOCKID_NAME_MAX) {
294 ret = DLM_IVBUFLEN;
295 mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
296 "handler!\n", locklen);
297 goto leave;
298 }
299
300 if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
301 (LKM_PUT_LVB|LKM_GET_LVB)) {
302 mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
303 flags);
304 ret = DLM_BADARGS;
305 goto leave;
306 }
307
308 mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
309 (flags & LKM_GET_LVB ? "get lvb" : "none"));
310
311 mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
312
313 if (past->type != DLM_AST &&
314 past->type != DLM_BAST) {
315 mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
316 "name=%.*s, node=%u\n", past->type,
317 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
318 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
319 locklen, name, node);
320 ret = DLM_IVLOCKID;
321 goto leave;
322 }
323
324 res = dlm_lookup_lockres(dlm, name, locklen);
325 if (!res) {
326 mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
327 "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
328 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
329 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
330 locklen, name, node);
331 ret = DLM_IVLOCKID;
332 goto leave;
333 }
334
335 /* cannot get a proxy ast message if this node owns it */
336 BUG_ON(res->owner == dlm->node_num);
337
338 mlog(0, "lockres %.*s\n", res->lockname.len, res->lockname.name);
339
340 spin_lock(&res->spinlock);
341 if (res->state & DLM_LOCK_RES_RECOVERING) {
342 mlog(0, "Responding with DLM_RECOVERING!\n");
343 ret = DLM_RECOVERING;
344 goto unlock_out;
345 }
346 if (res->state & DLM_LOCK_RES_MIGRATING) {
347 mlog(0, "Responding with DLM_MIGRATING!\n");
348 ret = DLM_MIGRATING;
349 goto unlock_out;
350 }
351 /* try convert queue for both ast/bast */
352 head = &res->converting;
353 lock = NULL;
354 list_for_each(iter, head) {
355 lock = list_entry (iter, struct dlm_lock, list);
356 if (lock->ml.cookie == cookie)
357 goto do_ast;
358 }
359
360 /* if not on convert, try blocked for ast, granted for bast */
361 if (past->type == DLM_AST)
362 head = &res->blocked;
363 else
364 head = &res->granted;
365
366 list_for_each(iter, head) {
367 lock = list_entry (iter, struct dlm_lock, list);
368 if (lock->ml.cookie == cookie)
369 goto do_ast;
370 }
371
372 mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
373 "node=%u\n", past->type == DLM_AST ? "" : "b",
374 dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
375 dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
376 locklen, name, node);
377
378 ret = DLM_NORMAL;
379 unlock_out:
380 spin_unlock(&res->spinlock);
381 goto leave;
382
383 do_ast:
384 ret = DLM_NORMAL;
385 if (past->type == DLM_AST) {
386 /* do not alter lock refcount. switching lists. */
387 list_move_tail(&lock->list, &res->granted);
388 mlog(0, "ast: Adding to granted list... type=%d, "
389 "convert_type=%d\n", lock->ml.type, lock->ml.convert_type);
390 if (lock->ml.convert_type != LKM_IVMODE) {
391 lock->ml.type = lock->ml.convert_type;
392 lock->ml.convert_type = LKM_IVMODE;
393 } else {
394 // should already be there....
395 }
396
397 lock->lksb->status = DLM_NORMAL;
398
399 /* if we requested the lvb, fetch it into our lksb now */
400 if (flags & LKM_GET_LVB) {
401 BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
402 memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
403 }
404 }
405 spin_unlock(&res->spinlock);
406
407 if (past->type == DLM_AST)
408 dlm_do_local_ast(dlm, res, lock);
409 else
410 dlm_do_local_bast(dlm, res, lock, past->blocked_type);
411
412 leave:
413 if (res)
414 dlm_lockres_put(res);
415
416 dlm_put(dlm);
417 return ret;
418 }
419
420
421
422 int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
423 struct dlm_lock *lock, int msg_type,
424 int blocked_type, int flags)
425 {
426 int ret = 0;
427 struct dlm_proxy_ast past;
428 struct kvec vec[2];
429 size_t veclen = 1;
430 int status;
431
432 mlog_entry("res %.*s, to=%u, type=%d, blocked_type=%d\n",
433 res->lockname.len, res->lockname.name, lock->ml.node,
434 msg_type, blocked_type);
435
436 memset(&past, 0, sizeof(struct dlm_proxy_ast));
437 past.node_idx = dlm->node_num;
438 past.type = msg_type;
439 past.blocked_type = blocked_type;
440 past.namelen = res->lockname.len;
441 memcpy(past.name, res->lockname.name, past.namelen);
442 past.cookie = lock->ml.cookie;
443
444 vec[0].iov_len = sizeof(struct dlm_proxy_ast);
445 vec[0].iov_base = &past;
446 if (flags & DLM_LKSB_GET_LVB) {
447 mlog(0, "returning requested LVB data\n");
448 be32_add_cpu(&past.flags, LKM_GET_LVB);
449 vec[1].iov_len = DLM_LVB_LEN;
450 vec[1].iov_base = lock->lksb->lvb;
451 veclen++;
452 }
453
454 ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
455 lock->ml.node, &status);
456 if (ret < 0)
457 mlog_errno(ret);
458 else {
459 if (status == DLM_RECOVERING) {
460 mlog(ML_ERROR, "sent AST to node %u, it thinks this "
461 "node is dead!\n", lock->ml.node);
462 BUG();
463 } else if (status == DLM_MIGRATING) {
464 mlog(ML_ERROR, "sent AST to node %u, it returned "
465 "DLM_MIGRATING!\n", lock->ml.node);
466 BUG();
467 } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
468 mlog(ML_ERROR, "AST to node %u returned %d!\n",
469 lock->ml.node, status);
470 /* ignore it */
471 }
472 ret = 0;
473 }
474 return ret;
475 }
This page took 0.057913 seconds and 6 git commands to generate.