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