1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * underlying calls for unlocking locks
8 * Copyright (C) 2004 Oracle. All rights reserved.
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.
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.
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.
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/highmem.h>
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 #include <linux/delay.h>
41 #include "cluster/heartbeat.h"
42 #include "cluster/nodemanager.h"
43 #include "cluster/tcp.h"
46 #include "dlmcommon.h"
48 #define MLOG_MASK_PREFIX ML_DLM
49 #include "cluster/masklog.h"
51 #define DLM_UNLOCK_FREE_LOCK 0x00000001
52 #define DLM_UNLOCK_CALL_AST 0x00000002
53 #define DLM_UNLOCK_REMOVE_LOCK 0x00000004
54 #define DLM_UNLOCK_REGRANT_LOCK 0x00000008
55 #define DLM_UNLOCK_CLEAR_CONVERT_TYPE 0x00000010
58 static enum dlm_status
dlm_get_cancel_actions(struct dlm_ctxt
*dlm
,
59 struct dlm_lock_resource
*res
,
60 struct dlm_lock
*lock
,
61 struct dlm_lockstatus
*lksb
,
63 static enum dlm_status
dlm_get_unlock_actions(struct dlm_ctxt
*dlm
,
64 struct dlm_lock_resource
*res
,
65 struct dlm_lock
*lock
,
66 struct dlm_lockstatus
*lksb
,
69 static enum dlm_status
dlm_send_remote_unlock_request(struct dlm_ctxt
*dlm
,
70 struct dlm_lock_resource
*res
,
71 struct dlm_lock
*lock
,
72 struct dlm_lockstatus
*lksb
,
78 * according to the spec:
79 * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
81 * flags & LKM_CANCEL != 0: must be converting or blocked
82 * flags & LKM_CANCEL == 0: must be granted
84 * So to unlock a converting lock, you must first cancel the
85 * convert (passing LKM_CANCEL in flags), then call the unlock
86 * again (with no LKM_CANCEL in flags).
93 * taken: res->spinlock and lock->spinlock taken and dropped
95 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
96 * all callers should have taken an extra ref on lock coming in
98 static enum dlm_status
dlmunlock_common(struct dlm_ctxt
*dlm
,
99 struct dlm_lock_resource
*res
,
100 struct dlm_lock
*lock
,
101 struct dlm_lockstatus
*lksb
,
102 int flags
, int *call_ast
,
105 enum dlm_status status
;
110 mlog(0, "master_node = %d, valblk = %d\n", master_node
,
114 BUG_ON(res
->owner
!= dlm
->node_num
);
116 BUG_ON(res
->owner
== dlm
->node_num
);
118 spin_lock(&dlm
->ast_lock
);
119 /* We want to be sure that we're not freeing a lock
120 * that still has AST's pending... */
121 in_use
= !list_empty(&lock
->ast_list
);
122 spin_unlock(&dlm
->ast_lock
);
123 if (in_use
&& !(flags
& LKM_CANCEL
)) {
124 mlog(ML_ERROR
, "lockres %.*s: Someone is calling dlmunlock "
125 "while waiting for an ast!", res
->lockname
.len
,
130 spin_lock(&res
->spinlock
);
131 if (res
->state
& DLM_LOCK_RES_IN_PROGRESS
) {
132 if (master_node
&& !(flags
& LKM_CANCEL
)) {
133 mlog(ML_ERROR
, "lockres in progress!\n");
134 spin_unlock(&res
->spinlock
);
137 /* ok for this to sleep if not in a network handler */
138 __dlm_wait_on_lockres(res
);
139 res
->state
|= DLM_LOCK_RES_IN_PROGRESS
;
141 spin_lock(&lock
->spinlock
);
143 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
144 status
= DLM_RECOVERING
;
148 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
149 status
= DLM_MIGRATING
;
153 /* see above for what the spec says about
154 * LKM_CANCEL and the lock queue state */
155 if (flags
& LKM_CANCEL
)
156 status
= dlm_get_cancel_actions(dlm
, res
, lock
, lksb
, &actions
);
158 status
= dlm_get_unlock_actions(dlm
, res
, lock
, lksb
, &actions
);
160 if (status
!= DLM_NORMAL
&& (status
!= DLM_CANCELGRANT
|| !master_node
))
163 /* By now this has been masked out of cancel requests. */
164 if (flags
& LKM_VALBLK
) {
165 /* make the final update to the lvb */
167 memcpy(res
->lvb
, lksb
->lvb
, DLM_LVB_LEN
);
169 flags
|= LKM_PUT_LVB
; /* let the send function
175 /* drop locks and send message */
176 if (flags
& LKM_CANCEL
)
177 lock
->cancel_pending
= 1;
179 lock
->unlock_pending
= 1;
180 spin_unlock(&lock
->spinlock
);
181 spin_unlock(&res
->spinlock
);
182 status
= dlm_send_remote_unlock_request(dlm
, res
, lock
, lksb
,
184 spin_lock(&res
->spinlock
);
185 spin_lock(&lock
->spinlock
);
186 /* if the master told us the lock was already granted,
187 * let the ast handle all of these actions */
188 if (status
== DLM_CANCELGRANT
) {
189 actions
&= ~(DLM_UNLOCK_REMOVE_LOCK
|
190 DLM_UNLOCK_REGRANT_LOCK
|
191 DLM_UNLOCK_CLEAR_CONVERT_TYPE
);
192 } else if (status
== DLM_RECOVERING
||
193 status
== DLM_MIGRATING
||
194 status
== DLM_FORWARD
||
195 status
== DLM_NOLOCKMGR
197 /* must clear the actions because this unlock
198 * is about to be retried. cannot free or do
199 * any list manipulation. */
200 mlog(0, "%s:%.*s: clearing actions, %s\n",
201 dlm
->name
, res
->lockname
.len
,
203 status
==DLM_RECOVERING
?"recovering":
204 (status
==DLM_MIGRATING
?"migrating":
205 (status
== DLM_FORWARD
? "forward" :
209 if (flags
& LKM_CANCEL
)
210 lock
->cancel_pending
= 0;
212 lock
->unlock_pending
= 0;
216 /* get an extra ref on lock. if we are just switching
217 * lists here, we dont want the lock to go away. */
220 if (actions
& DLM_UNLOCK_REMOVE_LOCK
) {
221 list_del_init(&lock
->list
);
224 if (actions
& DLM_UNLOCK_REGRANT_LOCK
) {
226 list_add_tail(&lock
->list
, &res
->granted
);
228 if (actions
& DLM_UNLOCK_CLEAR_CONVERT_TYPE
) {
229 mlog(0, "clearing convert_type at %smaster node\n",
230 master_node
? "" : "non-");
231 lock
->ml
.convert_type
= LKM_IVMODE
;
234 /* remove the extra ref on lock */
238 res
->state
&= ~DLM_LOCK_RES_IN_PROGRESS
;
239 if (!dlm_lock_on_list(&res
->converting
, lock
))
240 BUG_ON(lock
->ml
.convert_type
!= LKM_IVMODE
);
242 BUG_ON(lock
->ml
.convert_type
== LKM_IVMODE
);
243 spin_unlock(&lock
->spinlock
);
244 spin_unlock(&res
->spinlock
);
247 /* let the caller's final dlm_lock_put handle the actual kfree */
248 if (actions
& DLM_UNLOCK_FREE_LOCK
) {
249 /* this should always be coupled with list removal */
250 BUG_ON(!(actions
& DLM_UNLOCK_REMOVE_LOCK
));
251 mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
252 dlm_get_lock_cookie_node(be64_to_cpu(lock
->ml
.cookie
)),
253 dlm_get_lock_cookie_seq(be64_to_cpu(lock
->ml
.cookie
)),
254 kref_read(&lock
->lock_refs
)-1);
257 if (actions
& DLM_UNLOCK_CALL_AST
)
260 /* if cancel or unlock succeeded, lvb work is done */
261 if (status
== DLM_NORMAL
)
262 lksb
->flags
&= ~(DLM_LKSB_PUT_LVB
|DLM_LKSB_GET_LVB
);
267 void dlm_commit_pending_unlock(struct dlm_lock_resource
*res
,
268 struct dlm_lock
*lock
)
270 /* leave DLM_LKSB_PUT_LVB on the lksb so any final
271 * update of the lvb will be sent to the new master */
272 list_del_init(&lock
->list
);
275 void dlm_commit_pending_cancel(struct dlm_lock_resource
*res
,
276 struct dlm_lock
*lock
)
278 list_move_tail(&lock
->list
, &res
->granted
);
279 lock
->ml
.convert_type
= LKM_IVMODE
;
283 static inline enum dlm_status
dlmunlock_master(struct dlm_ctxt
*dlm
,
284 struct dlm_lock_resource
*res
,
285 struct dlm_lock
*lock
,
286 struct dlm_lockstatus
*lksb
,
290 return dlmunlock_common(dlm
, res
, lock
, lksb
, flags
, call_ast
, 1);
293 static inline enum dlm_status
dlmunlock_remote(struct dlm_ctxt
*dlm
,
294 struct dlm_lock_resource
*res
,
295 struct dlm_lock
*lock
,
296 struct dlm_lockstatus
*lksb
,
297 int flags
, int *call_ast
)
299 return dlmunlock_common(dlm
, res
, lock
, lksb
, flags
, call_ast
, 0);
307 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
309 static enum dlm_status
dlm_send_remote_unlock_request(struct dlm_ctxt
*dlm
,
310 struct dlm_lock_resource
*res
,
311 struct dlm_lock
*lock
,
312 struct dlm_lockstatus
*lksb
,
316 struct dlm_unlock_lock unlock
;
323 mlog(0, "%.*s\n", res
->lockname
.len
, res
->lockname
.name
);
325 if (owner
== dlm
->node_num
) {
326 /* ended up trying to contact ourself. this means
327 * that the lockres had been remote but became local
328 * via a migration. just retry it, now as local */
329 mlog(0, "%s:%.*s: this node became the master due to a "
330 "migration, re-evaluate now\n", dlm
->name
,
331 res
->lockname
.len
, res
->lockname
.name
);
335 memset(&unlock
, 0, sizeof(unlock
));
336 unlock
.node_idx
= dlm
->node_num
;
337 unlock
.flags
= cpu_to_be32(flags
);
338 unlock
.cookie
= lock
->ml
.cookie
;
339 unlock
.namelen
= res
->lockname
.len
;
340 memcpy(unlock
.name
, res
->lockname
.name
, unlock
.namelen
);
342 vec
[0].iov_len
= sizeof(struct dlm_unlock_lock
);
343 vec
[0].iov_base
= &unlock
;
345 if (flags
& LKM_PUT_LVB
) {
346 /* extra data to send if we are updating lvb */
347 vec
[1].iov_len
= DLM_LVB_LEN
;
348 vec
[1].iov_base
= lock
->lksb
->lvb
;
352 tmpret
= o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG
, dlm
->key
,
353 vec
, veclen
, owner
, &status
);
355 // successfully sent and received
356 if (status
== DLM_FORWARD
)
357 mlog(0, "master was in-progress. retry\n");
360 mlog(ML_ERROR
, "Error %d when sending message %u (key 0x%x) to "
361 "node %u\n", tmpret
, DLM_UNLOCK_LOCK_MSG
, dlm
->key
, owner
);
362 if (dlm_is_host_down(tmpret
)) {
363 /* NOTE: this seems strange, but it is what we want.
364 * when the master goes down during a cancel or
365 * unlock, the recovery code completes the operation
366 * as if the master had not died, then passes the
367 * updated state to the recovery master. this thread
368 * just needs to finish out the operation and call
370 if (dlm_is_node_dead(dlm
, owner
))
375 /* something bad. this will BUG in ocfs2 */
376 ret
= dlm_err_to_dlm_status(tmpret
);
386 * taken: takes and drops res->spinlock
388 * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
389 * return value from dlmunlock_master
391 int dlm_unlock_lock_handler(struct o2net_msg
*msg
, u32 len
, void *data
,
394 struct dlm_ctxt
*dlm
= data
;
395 struct dlm_unlock_lock
*unlock
= (struct dlm_unlock_lock
*)msg
->buf
;
396 struct dlm_lock_resource
*res
= NULL
;
397 struct dlm_lock
*lock
= NULL
;
398 enum dlm_status status
= DLM_NORMAL
;
400 struct dlm_lockstatus
*lksb
= NULL
;
403 struct list_head
*queue
;
405 flags
= be32_to_cpu(unlock
->flags
);
407 if (flags
& LKM_GET_LVB
) {
408 mlog(ML_ERROR
, "bad args! GET_LVB specified on unlock!\n");
412 if ((flags
& (LKM_PUT_LVB
|LKM_CANCEL
)) == (LKM_PUT_LVB
|LKM_CANCEL
)) {
413 mlog(ML_ERROR
, "bad args! cannot modify lvb on a CANCEL "
418 if (unlock
->namelen
> DLM_LOCKID_NAME_MAX
) {
419 mlog(ML_ERROR
, "Invalid name length in unlock handler!\n");
426 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm
),
427 "Domain %s not fully joined!\n", dlm
->name
);
429 mlog(0, "lvb: %s\n", flags
& LKM_PUT_LVB
? "put lvb" : "none");
431 res
= dlm_lookup_lockres(dlm
, unlock
->name
, unlock
->namelen
);
433 /* We assume here that a no lock resource simply means
434 * it was migrated away and destroyed before the other
435 * node could detect it. */
436 mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
437 status
= DLM_FORWARD
;
443 spin_lock(&res
->spinlock
);
444 if (res
->state
& DLM_LOCK_RES_RECOVERING
) {
445 spin_unlock(&res
->spinlock
);
446 mlog(0, "returning DLM_RECOVERING\n");
447 status
= DLM_RECOVERING
;
451 if (res
->state
& DLM_LOCK_RES_MIGRATING
) {
452 spin_unlock(&res
->spinlock
);
453 mlog(0, "returning DLM_MIGRATING\n");
454 status
= DLM_MIGRATING
;
458 if (res
->owner
!= dlm
->node_num
) {
459 spin_unlock(&res
->spinlock
);
460 mlog(0, "returning DLM_FORWARD -- not master\n");
461 status
= DLM_FORWARD
;
465 for (i
=0; i
<3; i
++) {
466 list_for_each_entry(lock
, queue
, list
) {
467 if (lock
->ml
.cookie
== unlock
->cookie
&&
468 lock
->ml
.node
== unlock
->node_idx
) {
476 /* scan granted -> converting -> blocked queues */
479 spin_unlock(&res
->spinlock
);
481 status
= DLM_IVLOCKID
;
485 /* lock was found on queue */
487 if (flags
& (LKM_VALBLK
|LKM_PUT_LVB
) &&
488 lock
->ml
.type
!= LKM_EXMODE
)
489 flags
&= ~(LKM_VALBLK
|LKM_PUT_LVB
);
491 /* unlockast only called on originating node */
492 if (flags
& LKM_PUT_LVB
) {
493 lksb
->flags
|= DLM_LKSB_PUT_LVB
;
494 memcpy(&lksb
->lvb
[0], &unlock
->lvb
[0], DLM_LVB_LEN
);
497 /* if this is in-progress, propagate the DLM_FORWARD
498 * all the way back out */
499 status
= dlmunlock_master(dlm
, res
, lock
, lksb
, flags
, &ignore
);
500 if (status
== DLM_FORWARD
)
501 mlog(0, "lockres is in progress\n");
503 if (flags
& LKM_PUT_LVB
)
504 lksb
->flags
&= ~DLM_LKSB_PUT_LVB
;
506 dlm_lockres_calc_usage(dlm
, res
);
507 dlm_kick_thread(dlm
, res
);
511 mlog(ML_ERROR
, "failed to find lock to unlock! "
513 dlm_get_lock_cookie_node(be64_to_cpu(unlock
->cookie
)),
514 dlm_get_lock_cookie_seq(be64_to_cpu(unlock
->cookie
)));
520 dlm_lockres_put(res
);
528 static enum dlm_status
dlm_get_cancel_actions(struct dlm_ctxt
*dlm
,
529 struct dlm_lock_resource
*res
,
530 struct dlm_lock
*lock
,
531 struct dlm_lockstatus
*lksb
,
534 enum dlm_status status
;
536 if (dlm_lock_on_list(&res
->blocked
, lock
)) {
537 /* cancel this outright */
539 *actions
= (DLM_UNLOCK_CALL_AST
|
540 DLM_UNLOCK_REMOVE_LOCK
);
541 } else if (dlm_lock_on_list(&res
->converting
, lock
)) {
542 /* cancel the request, put back on granted */
544 *actions
= (DLM_UNLOCK_CALL_AST
|
545 DLM_UNLOCK_REMOVE_LOCK
|
546 DLM_UNLOCK_REGRANT_LOCK
|
547 DLM_UNLOCK_CLEAR_CONVERT_TYPE
);
548 } else if (dlm_lock_on_list(&res
->granted
, lock
)) {
549 /* too late, already granted. */
550 status
= DLM_CANCELGRANT
;
551 *actions
= DLM_UNLOCK_CALL_AST
;
553 mlog(ML_ERROR
, "lock to cancel is not on any list!\n");
554 status
= DLM_IVLOCKID
;
560 static enum dlm_status
dlm_get_unlock_actions(struct dlm_ctxt
*dlm
,
561 struct dlm_lock_resource
*res
,
562 struct dlm_lock
*lock
,
563 struct dlm_lockstatus
*lksb
,
566 enum dlm_status status
;
569 if (!dlm_lock_on_list(&res
->granted
, lock
)) {
574 /* unlock granted lock */
576 *actions
= (DLM_UNLOCK_FREE_LOCK
|
577 DLM_UNLOCK_CALL_AST
|
578 DLM_UNLOCK_REMOVE_LOCK
);
583 /* there seems to be no point in doing this async
584 * since (even for the remote case) there is really
585 * no work to queue up... so just do it and fire the
586 * unlockast by hand when done... */
587 enum dlm_status
dlmunlock(struct dlm_ctxt
*dlm
, struct dlm_lockstatus
*lksb
,
588 int flags
, dlm_astunlockfunc_t
*unlockast
, void *data
)
590 enum dlm_status status
;
591 struct dlm_lock_resource
*res
;
592 struct dlm_lock
*lock
= NULL
;
593 int call_ast
, is_master
;
596 dlm_error(DLM_BADARGS
);
600 if (flags
& ~(LKM_CANCEL
| LKM_VALBLK
| LKM_INVVALBLK
)) {
601 dlm_error(DLM_BADPARAM
);
605 if ((flags
& (LKM_VALBLK
| LKM_CANCEL
)) == (LKM_VALBLK
| LKM_CANCEL
)) {
606 mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
607 flags
&= ~LKM_VALBLK
;
610 if (!lksb
->lockid
|| !lksb
->lockid
->lockres
) {
611 dlm_error(DLM_BADPARAM
);
621 dlm_lockres_get(res
);
624 /* need to retry up here because owner may have changed */
625 mlog(0, "lock=%p res=%p\n", lock
, res
);
627 spin_lock(&res
->spinlock
);
628 is_master
= (res
->owner
== dlm
->node_num
);
629 if (flags
& LKM_VALBLK
&& lock
->ml
.type
!= LKM_EXMODE
)
630 flags
&= ~LKM_VALBLK
;
631 spin_unlock(&res
->spinlock
);
634 status
= dlmunlock_master(dlm
, res
, lock
, lksb
, flags
,
636 mlog(0, "done calling dlmunlock_master: returned %d, "
637 "call_ast is %d\n", status
, call_ast
);
639 status
= dlmunlock_remote(dlm
, res
, lock
, lksb
, flags
,
641 mlog(0, "done calling dlmunlock_remote: returned %d, "
642 "call_ast is %d\n", status
, call_ast
);
645 if (status
== DLM_RECOVERING
||
646 status
== DLM_MIGRATING
||
647 status
== DLM_FORWARD
||
648 status
== DLM_NOLOCKMGR
) {
650 /* We want to go away for a tiny bit to allow recovery
651 * / migration to complete on this resource. I don't
652 * know of any wait queue we could sleep on as this
653 * may be happening on another node. Perhaps the
654 * proper solution is to queue up requests on the
657 /* do we want to yield(); ?? */
660 mlog(0, "retrying unlock due to pending recovery/"
661 "migration/in-progress/reconnect\n");
666 mlog(0, "calling unlockast(%p, %d)\n", data
, status
);
668 /* it is possible that there is one last bast
669 * pending. make sure it is flushed, then
670 * call the unlockast.
671 * not an issue if this is a mastered remotely,
672 * since this lock has been removed from the
673 * lockres queues and cannot be found. */
674 dlm_kick_thread(dlm
, NULL
);
675 wait_event(dlm
->ast_wq
,
676 dlm_lock_basts_flushed(dlm
, lock
));
678 (*unlockast
)(data
, status
);
681 if (status
== DLM_CANCELGRANT
)
684 if (status
== DLM_NORMAL
) {
685 mlog(0, "kicking the thread\n");
686 dlm_kick_thread(dlm
, res
);
690 dlm_lockres_calc_usage(dlm
, res
);
691 dlm_lockres_put(res
);
694 mlog(0, "returning status=%d!\n", status
);
697 EXPORT_SYMBOL_GPL(dlmunlock
);