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:
7 * underlying calls for unlocking locks
9 * Copyright (C) 2004 Oracle. All rights reserved.
13 #include <linux/module.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 #include <linux/delay.h>
26 #include "cluster/heartbeat.h"
27 #include "cluster/nodemanager.h"
28 #include "cluster/tcp.h"
31 #include "dlmcommon.h"
33 #define MLOG_MASK_PREFIX ML_DLM
34 #include "cluster/masklog.h"
36 #define DLM_UNLOCK_FREE_LOCK 0x00000001
37 #define DLM_UNLOCK_CALL_AST 0x00000002
38 #define DLM_UNLOCK_REMOVE_LOCK 0x00000004
39 #define DLM_UNLOCK_REGRANT_LOCK 0x00000008
40 #define DLM_UNLOCK_CLEAR_CONVERT_TYPE 0x00000010
43 static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
44 struct dlm_lock_resource *res,
45 struct dlm_lock *lock,
46 struct dlm_lockstatus *lksb,
48 static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
49 struct dlm_lock_resource *res,
50 struct dlm_lock *lock,
51 struct dlm_lockstatus *lksb,
54 static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
55 struct dlm_lock_resource *res,
56 struct dlm_lock *lock,
57 struct dlm_lockstatus *lksb,
63 * according to the spec:
64 * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
66 * flags & LKM_CANCEL != 0: must be converting or blocked
67 * flags & LKM_CANCEL == 0: must be granted
69 * So to unlock a converting lock, you must first cancel the
70 * convert (passing LKM_CANCEL in flags), then call the unlock
71 * again (with no LKM_CANCEL in flags).
78 * taken: res->spinlock and lock->spinlock taken and dropped
80 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
81 * all callers should have taken an extra ref on lock coming in
83 static enum dlm_status dlmunlock_common(struct dlm_ctxt *dlm,
84 struct dlm_lock_resource *res,
85 struct dlm_lock *lock,
86 struct dlm_lockstatus *lksb,
87 int flags, int *call_ast,
90 enum dlm_status status;
95 mlog(0, "master_node = %d, valblk = %d\n", master_node,
99 BUG_ON(res->owner != dlm->node_num);
101 BUG_ON(res->owner == dlm->node_num);
103 spin_lock(&dlm->ast_lock);
104 /* We want to be sure that we're not freeing a lock
105 * that still has AST's pending... */
106 in_use = !list_empty(&lock->ast_list);
107 spin_unlock(&dlm->ast_lock);
108 if (in_use && !(flags & LKM_CANCEL)) {
109 mlog(ML_ERROR, "lockres %.*s: Someone is calling dlmunlock "
110 "while waiting for an ast!", res->lockname.len,
115 spin_lock(&res->spinlock);
116 if (res->state & DLM_LOCK_RES_IN_PROGRESS) {
117 if (master_node && !(flags & LKM_CANCEL)) {
118 mlog(ML_ERROR, "lockres in progress!\n");
119 spin_unlock(&res->spinlock);
122 /* ok for this to sleep if not in a network handler */
123 __dlm_wait_on_lockres(res);
124 res->state |= DLM_LOCK_RES_IN_PROGRESS;
126 spin_lock(&lock->spinlock);
128 if (res->state & DLM_LOCK_RES_RECOVERING) {
129 status = DLM_RECOVERING;
133 if (res->state & DLM_LOCK_RES_MIGRATING) {
134 status = DLM_MIGRATING;
138 /* see above for what the spec says about
139 * LKM_CANCEL and the lock queue state */
140 if (flags & LKM_CANCEL)
141 status = dlm_get_cancel_actions(dlm, res, lock, lksb, &actions);
143 status = dlm_get_unlock_actions(dlm, res, lock, lksb, &actions);
145 if (status != DLM_NORMAL && (status != DLM_CANCELGRANT || !master_node))
148 /* By now this has been masked out of cancel requests. */
149 if (flags & LKM_VALBLK) {
150 /* make the final update to the lvb */
152 memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
154 flags |= LKM_PUT_LVB; /* let the send function
160 /* drop locks and send message */
161 if (flags & LKM_CANCEL)
162 lock->cancel_pending = 1;
164 lock->unlock_pending = 1;
165 spin_unlock(&lock->spinlock);
166 spin_unlock(&res->spinlock);
167 status = dlm_send_remote_unlock_request(dlm, res, lock, lksb,
169 spin_lock(&res->spinlock);
170 spin_lock(&lock->spinlock);
171 /* if the master told us the lock was already granted,
172 * let the ast handle all of these actions */
173 if (status == DLM_CANCELGRANT) {
174 actions &= ~(DLM_UNLOCK_REMOVE_LOCK|
175 DLM_UNLOCK_REGRANT_LOCK|
176 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
177 } else if (status == DLM_RECOVERING ||
178 status == DLM_MIGRATING ||
179 status == DLM_FORWARD ||
180 status == DLM_NOLOCKMGR
182 /* must clear the actions because this unlock
183 * is about to be retried. cannot free or do
184 * any list manipulation. */
185 mlog(0, "%s:%.*s: clearing actions, %s\n",
186 dlm->name, res->lockname.len,
188 status==DLM_RECOVERING?"recovering":
189 (status==DLM_MIGRATING?"migrating":
190 (status == DLM_FORWARD ? "forward" :
194 if (flags & LKM_CANCEL)
195 lock->cancel_pending = 0;
197 lock->unlock_pending = 0;
201 /* get an extra ref on lock. if we are just switching
202 * lists here, we dont want the lock to go away. */
205 if (actions & DLM_UNLOCK_REMOVE_LOCK) {
206 list_del_init(&lock->list);
209 if (actions & DLM_UNLOCK_REGRANT_LOCK) {
211 list_add_tail(&lock->list, &res->granted);
213 if (actions & DLM_UNLOCK_CLEAR_CONVERT_TYPE) {
214 mlog(0, "clearing convert_type at %smaster node\n",
215 master_node ? "" : "non-");
216 lock->ml.convert_type = LKM_IVMODE;
219 /* remove the extra ref on lock */
223 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
224 if (!dlm_lock_on_list(&res->converting, lock))
225 BUG_ON(lock->ml.convert_type != LKM_IVMODE);
227 BUG_ON(lock->ml.convert_type == LKM_IVMODE);
228 spin_unlock(&lock->spinlock);
229 spin_unlock(&res->spinlock);
232 /* let the caller's final dlm_lock_put handle the actual kfree */
233 if (actions & DLM_UNLOCK_FREE_LOCK) {
234 /* this should always be coupled with list removal */
235 BUG_ON(!(actions & DLM_UNLOCK_REMOVE_LOCK));
236 mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
237 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
238 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
239 kref_read(&lock->lock_refs)-1);
242 if (actions & DLM_UNLOCK_CALL_AST)
245 /* if cancel or unlock succeeded, lvb work is done */
246 if (status == DLM_NORMAL)
247 lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
252 void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
253 struct dlm_lock *lock)
255 /* leave DLM_LKSB_PUT_LVB on the lksb so any final
256 * update of the lvb will be sent to the new master */
257 list_del_init(&lock->list);
260 void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
261 struct dlm_lock *lock)
263 list_move_tail(&lock->list, &res->granted);
264 lock->ml.convert_type = LKM_IVMODE;
268 static inline enum dlm_status dlmunlock_master(struct dlm_ctxt *dlm,
269 struct dlm_lock_resource *res,
270 struct dlm_lock *lock,
271 struct dlm_lockstatus *lksb,
275 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 1);
278 static inline enum dlm_status dlmunlock_remote(struct dlm_ctxt *dlm,
279 struct dlm_lock_resource *res,
280 struct dlm_lock *lock,
281 struct dlm_lockstatus *lksb,
282 int flags, int *call_ast)
284 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 0);
292 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
294 static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
295 struct dlm_lock_resource *res,
296 struct dlm_lock *lock,
297 struct dlm_lockstatus *lksb,
301 struct dlm_unlock_lock unlock;
308 mlog(0, "%.*s\n", res->lockname.len, res->lockname.name);
310 if (owner == dlm->node_num) {
311 /* ended up trying to contact ourself. this means
312 * that the lockres had been remote but became local
313 * via a migration. just retry it, now as local */
314 mlog(0, "%s:%.*s: this node became the master due to a "
315 "migration, re-evaluate now\n", dlm->name,
316 res->lockname.len, res->lockname.name);
320 memset(&unlock, 0, sizeof(unlock));
321 unlock.node_idx = dlm->node_num;
322 unlock.flags = cpu_to_be32(flags);
323 unlock.cookie = lock->ml.cookie;
324 unlock.namelen = res->lockname.len;
325 memcpy(unlock.name, res->lockname.name, unlock.namelen);
327 vec[0].iov_len = sizeof(struct dlm_unlock_lock);
328 vec[0].iov_base = &unlock;
330 if (flags & LKM_PUT_LVB) {
331 /* extra data to send if we are updating lvb */
332 vec[1].iov_len = DLM_LVB_LEN;
333 vec[1].iov_base = lock->lksb->lvb;
337 tmpret = o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG, dlm->key,
338 vec, veclen, owner, &status);
340 // successfully sent and received
341 if (status == DLM_FORWARD)
342 mlog(0, "master was in-progress. retry\n");
345 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
346 "node %u\n", tmpret, DLM_UNLOCK_LOCK_MSG, dlm->key, owner);
347 if (dlm_is_host_down(tmpret)) {
348 /* NOTE: this seems strange, but it is what we want.
349 * when the master goes down during a cancel or
350 * unlock, the recovery code completes the operation
351 * as if the master had not died, then passes the
352 * updated state to the recovery master. this thread
353 * just needs to finish out the operation and call
355 if (dlm_is_node_dead(dlm, owner))
360 /* something bad. this will BUG in ocfs2 */
361 ret = dlm_err_to_dlm_status(tmpret);
371 * taken: takes and drops res->spinlock
373 * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
374 * return value from dlmunlock_master
376 int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
379 struct dlm_ctxt *dlm = data;
380 struct dlm_unlock_lock *unlock = (struct dlm_unlock_lock *)msg->buf;
381 struct dlm_lock_resource *res = NULL;
382 struct dlm_lock *lock = NULL;
383 enum dlm_status status = DLM_NORMAL;
385 struct dlm_lockstatus *lksb = NULL;
388 struct list_head *queue;
390 flags = be32_to_cpu(unlock->flags);
392 if (flags & LKM_GET_LVB) {
393 mlog(ML_ERROR, "bad args! GET_LVB specified on unlock!\n");
397 if ((flags & (LKM_PUT_LVB|LKM_CANCEL)) == (LKM_PUT_LVB|LKM_CANCEL)) {
398 mlog(ML_ERROR, "bad args! cannot modify lvb on a CANCEL "
403 if (unlock->namelen > DLM_LOCKID_NAME_MAX) {
404 mlog(ML_ERROR, "Invalid name length in unlock handler!\n");
411 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
412 "Domain %s not fully joined!\n", dlm->name);
414 mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" : "none");
416 res = dlm_lookup_lockres(dlm, unlock->name, unlock->namelen);
418 /* We assume here that a no lock resource simply means
419 * it was migrated away and destroyed before the other
420 * node could detect it. */
421 mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
422 status = DLM_FORWARD;
428 spin_lock(&res->spinlock);
429 if (res->state & DLM_LOCK_RES_RECOVERING) {
430 spin_unlock(&res->spinlock);
431 mlog(0, "returning DLM_RECOVERING\n");
432 status = DLM_RECOVERING;
436 if (res->state & DLM_LOCK_RES_MIGRATING) {
437 spin_unlock(&res->spinlock);
438 mlog(0, "returning DLM_MIGRATING\n");
439 status = DLM_MIGRATING;
443 if (res->owner != dlm->node_num) {
444 spin_unlock(&res->spinlock);
445 mlog(0, "returning DLM_FORWARD -- not master\n");
446 status = DLM_FORWARD;
450 for (i=0; i<3; i++) {
451 list_for_each_entry(lock, queue, list) {
452 if (lock->ml.cookie == unlock->cookie &&
453 lock->ml.node == unlock->node_idx) {
461 /* scan granted -> converting -> blocked queues */
464 spin_unlock(&res->spinlock);
466 status = DLM_IVLOCKID;
470 /* lock was found on queue */
472 if (flags & (LKM_VALBLK|LKM_PUT_LVB) &&
473 lock->ml.type != LKM_EXMODE)
474 flags &= ~(LKM_VALBLK|LKM_PUT_LVB);
476 /* unlockast only called on originating node */
477 if (flags & LKM_PUT_LVB) {
478 lksb->flags |= DLM_LKSB_PUT_LVB;
479 memcpy(&lksb->lvb[0], &unlock->lvb[0], DLM_LVB_LEN);
482 /* if this is in-progress, propagate the DLM_FORWARD
483 * all the way back out */
484 status = dlmunlock_master(dlm, res, lock, lksb, flags, &ignore);
485 if (status == DLM_FORWARD)
486 mlog(0, "lockres is in progress\n");
488 if (flags & LKM_PUT_LVB)
489 lksb->flags &= ~DLM_LKSB_PUT_LVB;
491 dlm_lockres_calc_usage(dlm, res);
492 dlm_kick_thread(dlm, res);
496 mlog(ML_ERROR, "failed to find lock to unlock! "
498 dlm_get_lock_cookie_node(be64_to_cpu(unlock->cookie)),
499 dlm_get_lock_cookie_seq(be64_to_cpu(unlock->cookie)));
505 dlm_lockres_put(res);
513 static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
514 struct dlm_lock_resource *res,
515 struct dlm_lock *lock,
516 struct dlm_lockstatus *lksb,
519 enum dlm_status status;
521 if (dlm_lock_on_list(&res->blocked, lock)) {
522 /* cancel this outright */
524 *actions = (DLM_UNLOCK_CALL_AST |
525 DLM_UNLOCK_REMOVE_LOCK);
526 } else if (dlm_lock_on_list(&res->converting, lock)) {
527 /* cancel the request, put back on granted */
529 *actions = (DLM_UNLOCK_CALL_AST |
530 DLM_UNLOCK_REMOVE_LOCK |
531 DLM_UNLOCK_REGRANT_LOCK |
532 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
533 } else if (dlm_lock_on_list(&res->granted, lock)) {
534 /* too late, already granted. */
535 status = DLM_CANCELGRANT;
536 *actions = DLM_UNLOCK_CALL_AST;
538 mlog(ML_ERROR, "lock to cancel is not on any list!\n");
539 status = DLM_IVLOCKID;
545 static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
546 struct dlm_lock_resource *res,
547 struct dlm_lock *lock,
548 struct dlm_lockstatus *lksb,
551 enum dlm_status status;
554 if (!dlm_lock_on_list(&res->granted, lock)) {
559 /* unlock granted lock */
561 *actions = (DLM_UNLOCK_FREE_LOCK |
562 DLM_UNLOCK_CALL_AST |
563 DLM_UNLOCK_REMOVE_LOCK);
568 /* there seems to be no point in doing this async
569 * since (even for the remote case) there is really
570 * no work to queue up... so just do it and fire the
571 * unlockast by hand when done... */
572 enum dlm_status dlmunlock(struct dlm_ctxt *dlm, struct dlm_lockstatus *lksb,
573 int flags, dlm_astunlockfunc_t *unlockast, void *data)
575 enum dlm_status status;
576 struct dlm_lock_resource *res;
577 struct dlm_lock *lock = NULL;
578 int call_ast, is_master;
581 dlm_error(DLM_BADARGS);
585 if (flags & ~(LKM_CANCEL | LKM_VALBLK | LKM_INVVALBLK)) {
586 dlm_error(DLM_BADPARAM);
590 if ((flags & (LKM_VALBLK | LKM_CANCEL)) == (LKM_VALBLK | LKM_CANCEL)) {
591 mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
592 flags &= ~LKM_VALBLK;
595 if (!lksb->lockid || !lksb->lockid->lockres) {
596 dlm_error(DLM_BADPARAM);
606 dlm_lockres_get(res);
609 /* need to retry up here because owner may have changed */
610 mlog(0, "lock=%p res=%p\n", lock, res);
612 spin_lock(&res->spinlock);
613 is_master = (res->owner == dlm->node_num);
614 if (flags & LKM_VALBLK && lock->ml.type != LKM_EXMODE)
615 flags &= ~LKM_VALBLK;
616 spin_unlock(&res->spinlock);
619 status = dlmunlock_master(dlm, res, lock, lksb, flags,
621 mlog(0, "done calling dlmunlock_master: returned %d, "
622 "call_ast is %d\n", status, call_ast);
624 status = dlmunlock_remote(dlm, res, lock, lksb, flags,
626 mlog(0, "done calling dlmunlock_remote: returned %d, "
627 "call_ast is %d\n", status, call_ast);
630 if (status == DLM_RECOVERING ||
631 status == DLM_MIGRATING ||
632 status == DLM_FORWARD ||
633 status == DLM_NOLOCKMGR) {
635 /* We want to go away for a tiny bit to allow recovery
636 * / migration to complete on this resource. I don't
637 * know of any wait queue we could sleep on as this
638 * may be happening on another node. Perhaps the
639 * proper solution is to queue up requests on the
642 /* do we want to yield(); ?? */
645 mlog(0, "retrying unlock due to pending recovery/"
646 "migration/in-progress/reconnect\n");
651 mlog(0, "calling unlockast(%p, %d)\n", data, status);
653 /* it is possible that there is one last bast
654 * pending. make sure it is flushed, then
655 * call the unlockast.
656 * not an issue if this is a mastered remotely,
657 * since this lock has been removed from the
658 * lockres queues and cannot be found. */
659 dlm_kick_thread(dlm, NULL);
660 wait_event(dlm->ast_wq,
661 dlm_lock_basts_flushed(dlm, lock));
663 (*unlockast)(data, status);
666 if (status == DLM_CANCELGRANT)
669 if (status == DLM_NORMAL) {
670 mlog(0, "kicking the thread\n");
671 dlm_kick_thread(dlm, res);
675 dlm_lockres_calc_usage(dlm, res);
676 dlm_lockres_put(res);
679 mlog(0, "returning status=%d!\n", status);
682 EXPORT_SYMBOL_GPL(dlmunlock);