xref: /openbmc/linux/fs/ocfs2/dlm/dlmast.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3  * vim: noexpandtab sw=8 ts=8 sts=0:
4  *
5  * dlmast.c
6  *
7  * AST and BAST functionality for local and remote nodes
8  *
9  * Copyright (C) 2004 Oracle.  All rights reserved.
10  */
11 
12 
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/types.h>
16 #include <linux/highmem.h>
17 #include <linux/init.h>
18 #include <linux/sysctl.h>
19 #include <linux/random.h>
20 #include <linux/blkdev.h>
21 #include <linux/socket.h>
22 #include <linux/inet.h>
23 #include <linux/spinlock.h>
24 
25 
26 #include "../cluster/heartbeat.h"
27 #include "../cluster/nodemanager.h"
28 #include "../cluster/tcp.h"
29 
30 #include "dlmapi.h"
31 #include "dlmcommon.h"
32 
33 #define MLOG_MASK_PREFIX ML_DLM
34 #include "../cluster/masklog.h"
35 
36 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
37 			   struct dlm_lock *lock);
38 static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
39 
40 /* Should be called as an ast gets queued to see if the new
41  * lock level will obsolete a pending bast.
42  * For example, if dlm_thread queued a bast for an EX lock that
43  * was blocking another EX, but before sending the bast the
44  * lock owner downconverted to NL, the bast is now obsolete.
45  * Only the ast should be sent.
46  * This is needed because the lock and convert paths can queue
47  * asts out-of-band (not waiting for dlm_thread) in order to
48  * allow for LKM_NOQUEUE to get immediate responses. */
49 static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
50 {
51 	assert_spin_locked(&dlm->ast_lock);
52 	assert_spin_locked(&lock->spinlock);
53 
54 	if (lock->ml.highest_blocked == LKM_IVMODE)
55 		return 0;
56 	BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
57 
58 	if (lock->bast_pending &&
59 	    list_empty(&lock->bast_list))
60 		/* old bast already sent, ok */
61 		return 0;
62 
63 	if (lock->ml.type == LKM_EXMODE)
64 		/* EX blocks anything left, any bast still valid */
65 		return 0;
66 	else if (lock->ml.type == LKM_NLMODE)
67 		/* NL blocks nothing, no reason to send any bast, cancel it */
68 		return 1;
69 	else if (lock->ml.highest_blocked != LKM_EXMODE)
70 		/* PR only blocks EX */
71 		return 1;
72 
73 	return 0;
74 }
75 
76 void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
77 {
78 	struct dlm_lock_resource *res;
79 
80 	BUG_ON(!dlm);
81 	BUG_ON(!lock);
82 
83 	res = lock->lockres;
84 
85 	assert_spin_locked(&dlm->ast_lock);
86 
87 	if (!list_empty(&lock->ast_list)) {
88 		mlog(ML_ERROR, "%s: res %.*s, lock %u:%llu, "
89 		     "AST list not empty, pending %d, newlevel %d\n",
90 		     dlm->name, res->lockname.len, res->lockname.name,
91 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
92 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
93 		     lock->ast_pending, lock->ml.type);
94 		BUG();
95 	}
96 	if (lock->ast_pending)
97 		mlog(0, "%s: res %.*s, lock %u:%llu, AST getting flushed\n",
98 		     dlm->name, res->lockname.len, res->lockname.name,
99 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
100 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
101 
102 	/* putting lock on list, add a ref */
103 	dlm_lock_get(lock);
104 	spin_lock(&lock->spinlock);
105 
106 	/* check to see if this ast obsoletes the bast */
107 	if (dlm_should_cancel_bast(dlm, lock)) {
108 		mlog(0, "%s: res %.*s, lock %u:%llu, Cancelling BAST\n",
109 		     dlm->name, res->lockname.len, res->lockname.name,
110 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
111 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
112 		lock->bast_pending = 0;
113 		list_del_init(&lock->bast_list);
114 		lock->ml.highest_blocked = LKM_IVMODE;
115 		/* removing lock from list, remove a ref.  guaranteed
116 		 * this won't be the last ref because of the get above,
117 		 * so res->spinlock will not be taken here */
118 		dlm_lock_put(lock);
119 		/* free up the reserved bast that we are cancelling.
120 		 * guaranteed that this will not be the last reserved
121 		 * ast because *both* an ast and a bast were reserved
122 		 * to get to this point.  the res->spinlock will not be
123 		 * taken here */
124 		dlm_lockres_release_ast(dlm, res);
125 	}
126 	list_add_tail(&lock->ast_list, &dlm->pending_asts);
127 	lock->ast_pending = 1;
128 	spin_unlock(&lock->spinlock);
129 }
130 
131 void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
132 {
133 	BUG_ON(!dlm);
134 	BUG_ON(!lock);
135 
136 	spin_lock(&dlm->ast_lock);
137 	__dlm_queue_ast(dlm, lock);
138 	spin_unlock(&dlm->ast_lock);
139 }
140 
141 
142 void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
143 {
144 	struct dlm_lock_resource *res;
145 
146 	BUG_ON(!dlm);
147 	BUG_ON(!lock);
148 
149 	assert_spin_locked(&dlm->ast_lock);
150 
151 	res = lock->lockres;
152 
153 	BUG_ON(!list_empty(&lock->bast_list));
154 	if (lock->bast_pending)
155 		mlog(0, "%s: res %.*s, lock %u:%llu, BAST getting flushed\n",
156 		     dlm->name, res->lockname.len, res->lockname.name,
157 		     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
158 		     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
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 static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
169 			   struct dlm_lock *lock)
170 {
171 	struct dlm_lockstatus *lksb = lock->lksb;
172 	BUG_ON(!lksb);
173 
174 	/* only updates if this node masters the lockres */
175 	spin_lock(&res->spinlock);
176 	if (res->owner == dlm->node_num) {
177 		/* check the lksb flags for the direction */
178 		if (lksb->flags & DLM_LKSB_GET_LVB) {
179 			mlog(0, "getting lvb from lockres for %s node\n",
180 				  lock->ml.node == dlm->node_num ? "master" :
181 				  "remote");
182 			memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
183 		}
184 		/* Do nothing for lvb put requests - they should be done in
185  		 * place when the lock is downconverted - otherwise we risk
186  		 * racing gets and puts which could result in old lvb data
187  		 * being propagated. We leave the put flag set and clear it
188  		 * here. In the future we might want to clear it at the time
189  		 * the put is actually done.
190 		 */
191 	}
192 	spin_unlock(&res->spinlock);
193 
194 	/* reset any lvb flags on the lksb */
195 	lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
196 }
197 
198 void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
199 		      struct dlm_lock *lock)
200 {
201 	dlm_astlockfunc_t *fn;
202 
203 	mlog(0, "%s: res %.*s, lock %u:%llu, Local AST\n", dlm->name,
204 	     res->lockname.len, res->lockname.name,
205 	     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
206 	     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
207 
208 	fn = lock->ast;
209 	BUG_ON(lock->ml.node != dlm->node_num);
210 
211 	dlm_update_lvb(dlm, res, lock);
212 	(*fn)(lock->astdata);
213 }
214 
215 
216 int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
217 		      struct dlm_lock *lock)
218 {
219 	int ret;
220 	struct dlm_lockstatus *lksb;
221 	int lksbflags;
222 
223 	mlog(0, "%s: res %.*s, lock %u:%llu, Remote AST\n", dlm->name,
224 	     res->lockname.len, res->lockname.name,
225 	     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
226 	     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
227 
228 	lksb = lock->lksb;
229 	BUG_ON(lock->ml.node == dlm->node_num);
230 
231 	lksbflags = lksb->flags;
232 	dlm_update_lvb(dlm, res, lock);
233 
234 	/* lock request came from another node
235 	 * go do the ast over there */
236 	ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
237 	return ret;
238 }
239 
240 void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
241 		       struct dlm_lock *lock, int blocked_type)
242 {
243 	dlm_bastlockfunc_t *fn = lock->bast;
244 
245 	BUG_ON(lock->ml.node != dlm->node_num);
246 
247 	mlog(0, "%s: res %.*s, lock %u:%llu, Local BAST, blocked %d\n",
248 	     dlm->name, res->lockname.len, res->lockname.name,
249 	     dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
250 	     dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
251 	     blocked_type);
252 
253 	(*fn)(lock->astdata, blocked_type);
254 }
255 
256 
257 
258 int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
259 			  void **ret_data)
260 {
261 	int ret;
262 	unsigned int locklen;
263 	struct dlm_ctxt *dlm = data;
264 	struct dlm_lock_resource *res = NULL;
265 	struct dlm_lock *lock = NULL;
266 	struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
267 	char *name;
268 	struct list_head *head = NULL;
269 	__be64 cookie;
270 	u32 flags;
271 	u8 node;
272 
273 	if (!dlm_grab(dlm)) {
274 		dlm_error(DLM_REJECTED);
275 		return DLM_REJECTED;
276 	}
277 
278 	mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
279 			"Domain %s not fully joined!\n", dlm->name);
280 
281 	name = past->name;
282 	locklen = past->namelen;
283 	cookie = past->cookie;
284 	flags = be32_to_cpu(past->flags);
285 	node = past->node_idx;
286 
287 	if (locklen > DLM_LOCKID_NAME_MAX) {
288 		ret = DLM_IVBUFLEN;
289 		mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
290 		     "handler!\n", locklen);
291 		goto leave;
292 	}
293 
294 	if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
295 	     (LKM_PUT_LVB|LKM_GET_LVB)) {
296 		mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
297 		     flags);
298 		ret = DLM_BADARGS;
299 		goto leave;
300 	}
301 
302 	mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
303 		  (flags & LKM_GET_LVB ? "get lvb" : "none"));
304 
305 	mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
306 
307 	if (past->type != DLM_AST &&
308 	    past->type != DLM_BAST) {
309 		mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
310 		     "name=%.*s, node=%u\n", past->type,
311 		     dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
312 		     dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
313 		     locklen, name, node);
314 		ret = DLM_IVLOCKID;
315 		goto leave;
316 	}
317 
318 	res = dlm_lookup_lockres(dlm, name, locklen);
319 	if (!res) {
320 		mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
321 		     "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
322 		     dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
323 		     dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
324 		     locklen, name, node);
325 		ret = DLM_IVLOCKID;
326 		goto leave;
327 	}
328 
329 	/* cannot get a proxy ast message if this node owns it */
330 	BUG_ON(res->owner == dlm->node_num);
331 
332 	mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
333 	     res->lockname.name);
334 
335 	spin_lock(&res->spinlock);
336 	if (res->state & DLM_LOCK_RES_RECOVERING) {
337 		mlog(0, "Responding with DLM_RECOVERING!\n");
338 		ret = DLM_RECOVERING;
339 		goto unlock_out;
340 	}
341 	if (res->state & DLM_LOCK_RES_MIGRATING) {
342 		mlog(0, "Responding with DLM_MIGRATING!\n");
343 		ret = DLM_MIGRATING;
344 		goto unlock_out;
345 	}
346 	/* try convert queue for both ast/bast */
347 	head = &res->converting;
348 	lock = NULL;
349 	list_for_each_entry(lock, head, list) {
350 		if (lock->ml.cookie == cookie)
351 			goto do_ast;
352 	}
353 
354 	/* if not on convert, try blocked for ast, granted for bast */
355 	if (past->type == DLM_AST)
356 		head = &res->blocked;
357 	else
358 		head = &res->granted;
359 
360 	list_for_each_entry(lock, head, list) {
361 		/* if lock is found but unlock is pending ignore the bast */
362 		if (lock->ml.cookie == cookie) {
363 			if (lock->unlock_pending)
364 				break;
365 			goto do_ast;
366 		}
367 	}
368 
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);
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. */
384 		list_move_tail(&lock->list, &res->granted);
385 		mlog(0, "%s: res %.*s, lock %u:%llu, Granted type %d => %d\n",
386 		     dlm->name, res->lockname.len, res->lockname.name,
387 		     dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
388 		     dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
389 		     lock->ml.type, lock->ml.convert_type);
390 
391 		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 
413 leave:
414 	if (res)
415 		dlm_lockres_put(res);
416 
417 	dlm_put(dlm);
418 	return ret;
419 }
420 
421 
422 
423 int 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(0, "%s: res %.*s, to %u, type %d, blocked_type %d\n", dlm->name,
434 	     res->lockname.len, res->lockname.name, lock->ml.node, msg_type,
435 	     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 		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(ML_ERROR, "%s: res %.*s, error %d send AST to node %u\n",
458 		     dlm->name, res->lockname.len, res->lockname.name, ret,
459 		     lock->ml.node);
460 	else {
461 		if (status == DLM_RECOVERING) {
462 			mlog(ML_ERROR, "sent AST to node %u, it thinks this "
463 			     "node is dead!\n", lock->ml.node);
464 			BUG();
465 		} else if (status == DLM_MIGRATING) {
466 			mlog(ML_ERROR, "sent AST to node %u, it returned "
467 			     "DLM_MIGRATING!\n", lock->ml.node);
468 			BUG();
469 		} else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
470 			mlog(ML_ERROR, "AST to node %u returned %d!\n",
471 			     lock->ml.node, status);
472 			/* ignore it */
473 		}
474 		ret = 0;
475 	}
476 	return ret;
477 }
478