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/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 #include <linux/delay.h>
43 #include "cluster/heartbeat.h"
44 #include "cluster/nodemanager.h"
45 #include "cluster/tcp.h"
48 #include "dlmcommon.h"
50 #define MLOG_MASK_PREFIX ML_DLM
51 #include "cluster/masklog.h"
53 #define DLM_UNLOCK_FREE_LOCK 0x00000001
54 #define DLM_UNLOCK_CALL_AST 0x00000002
55 #define DLM_UNLOCK_REMOVE_LOCK 0x00000004
56 #define DLM_UNLOCK_REGRANT_LOCK 0x00000008
57 #define DLM_UNLOCK_CLEAR_CONVERT_TYPE 0x00000010
60 static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
61 struct dlm_lock_resource *res,
62 struct dlm_lock *lock,
63 struct dlm_lockstatus *lksb,
65 static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
66 struct dlm_lock_resource *res,
67 struct dlm_lock *lock,
68 struct dlm_lockstatus *lksb,
71 static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
72 struct dlm_lock_resource *res,
73 struct dlm_lock *lock,
74 struct dlm_lockstatus *lksb,
80 * according to the spec:
81 * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
83 * flags & LKM_CANCEL != 0: must be converting or blocked
84 * flags & LKM_CANCEL == 0: must be granted
86 * So to unlock a converting lock, you must first cancel the
87 * convert (passing LKM_CANCEL in flags), then call the unlock
88 * again (with no LKM_CANCEL in flags).
95 * taken: res->spinlock and lock->spinlock taken and dropped
97 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
98 * all callers should have taken an extra ref on lock coming in
100 static enum dlm_status dlmunlock_common(struct dlm_ctxt *dlm,
101 struct dlm_lock_resource *res,
102 struct dlm_lock *lock,
103 struct dlm_lockstatus *lksb,
104 int flags, int *call_ast,
107 enum dlm_status status;
112 mlog(0, "master_node = %d, valblk = %d\n", master_node,
116 BUG_ON(res->owner != dlm->node_num);
118 BUG_ON(res->owner == dlm->node_num);
120 spin_lock(&dlm->spinlock);
121 /* We want to be sure that we're not freeing a lock
122 * that still has AST's pending... */
123 in_use = !list_empty(&lock->ast_list);
124 spin_unlock(&dlm->spinlock);
126 mlog(ML_ERROR, "lockres %.*s: Someone is calling dlmunlock "
127 "while waiting for an ast!", res->lockname.len,
132 spin_lock(&res->spinlock);
133 if (res->state & DLM_LOCK_RES_IN_PROGRESS) {
135 mlog(ML_ERROR, "lockres in progress!\n");
136 spin_unlock(&res->spinlock);
139 /* ok for this to sleep if not in a network handler */
140 __dlm_wait_on_lockres(res);
141 res->state |= DLM_LOCK_RES_IN_PROGRESS;
143 spin_lock(&lock->spinlock);
145 if (res->state & DLM_LOCK_RES_RECOVERING) {
146 status = DLM_RECOVERING;
151 /* see above for what the spec says about
152 * LKM_CANCEL and the lock queue state */
153 if (flags & LKM_CANCEL)
154 status = dlm_get_cancel_actions(dlm, res, lock, lksb, &actions);
156 status = dlm_get_unlock_actions(dlm, res, lock, lksb, &actions);
158 if (status != DLM_NORMAL)
161 /* By now this has been masked out of cancel requests. */
162 if (flags & LKM_VALBLK) {
163 /* make the final update to the lvb */
165 memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
167 flags |= LKM_PUT_LVB; /* let the send function
173 /* drop locks and send message */
174 if (flags & LKM_CANCEL)
175 lock->cancel_pending = 1;
177 lock->unlock_pending = 1;
178 spin_unlock(&lock->spinlock);
179 spin_unlock(&res->spinlock);
180 status = dlm_send_remote_unlock_request(dlm, res, lock, lksb,
182 spin_lock(&res->spinlock);
183 spin_lock(&lock->spinlock);
184 /* if the master told us the lock was already granted,
185 * let the ast handle all of these actions */
186 if (status == DLM_NORMAL &&
187 lksb->status == DLM_CANCELGRANT) {
188 actions &= ~(DLM_UNLOCK_REMOVE_LOCK|
189 DLM_UNLOCK_REGRANT_LOCK|
190 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
191 } else if (status == DLM_RECOVERING ||
192 status == DLM_MIGRATING ||
193 status == DLM_FORWARD) {
194 /* must clear the actions because this unlock
195 * is about to be retried. cannot free or do
196 * any list manipulation. */
197 mlog(0, "%s:%.*s: clearing actions, %s\n",
198 dlm->name, res->lockname.len,
200 status==DLM_RECOVERING?"recovering":
201 (status==DLM_MIGRATING?"migrating":
205 if (flags & LKM_CANCEL)
206 lock->cancel_pending = 0;
208 lock->unlock_pending = 0;
212 /* get an extra ref on lock. if we are just switching
213 * lists here, we dont want the lock to go away. */
216 if (actions & DLM_UNLOCK_REMOVE_LOCK) {
217 list_del_init(&lock->list);
220 if (actions & DLM_UNLOCK_REGRANT_LOCK) {
222 list_add_tail(&lock->list, &res->granted);
224 if (actions & DLM_UNLOCK_CLEAR_CONVERT_TYPE) {
225 mlog(0, "clearing convert_type at %smaster node\n",
226 master_node ? "" : "non-");
227 lock->ml.convert_type = LKM_IVMODE;
230 /* remove the extra ref on lock */
234 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
235 if (!dlm_lock_on_list(&res->converting, lock))
236 BUG_ON(lock->ml.convert_type != LKM_IVMODE);
238 BUG_ON(lock->ml.convert_type == LKM_IVMODE);
239 spin_unlock(&lock->spinlock);
240 spin_unlock(&res->spinlock);
243 /* let the caller's final dlm_lock_put handle the actual kfree */
244 if (actions & DLM_UNLOCK_FREE_LOCK) {
245 /* this should always be coupled with list removal */
246 BUG_ON(!(actions & DLM_UNLOCK_REMOVE_LOCK));
247 mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
248 dlm_get_lock_cookie_node(lock->ml.cookie),
249 dlm_get_lock_cookie_seq(lock->ml.cookie),
250 atomic_read(&lock->lock_refs.refcount)-1);
253 if (actions & DLM_UNLOCK_CALL_AST)
256 /* if cancel or unlock succeeded, lvb work is done */
257 if (status == DLM_NORMAL)
258 lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
263 void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
264 struct dlm_lock *lock)
266 /* leave DLM_LKSB_PUT_LVB on the lksb so any final
267 * update of the lvb will be sent to the new master */
268 list_del_init(&lock->list);
271 void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
272 struct dlm_lock *lock)
274 list_del_init(&lock->list);
275 list_add_tail(&lock->list, &res->granted);
276 lock->ml.convert_type = LKM_IVMODE;
280 static inline enum dlm_status dlmunlock_master(struct dlm_ctxt *dlm,
281 struct dlm_lock_resource *res,
282 struct dlm_lock *lock,
283 struct dlm_lockstatus *lksb,
287 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 1);
290 static inline enum dlm_status dlmunlock_remote(struct dlm_ctxt *dlm,
291 struct dlm_lock_resource *res,
292 struct dlm_lock *lock,
293 struct dlm_lockstatus *lksb,
294 int flags, int *call_ast)
296 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 0);
304 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
306 static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
307 struct dlm_lock_resource *res,
308 struct dlm_lock *lock,
309 struct dlm_lockstatus *lksb,
313 struct dlm_unlock_lock unlock;
320 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
322 memset(&unlock, 0, sizeof(unlock));
323 unlock.node_idx = dlm->node_num;
324 unlock.flags = cpu_to_be32(flags);
325 unlock.cookie = lock->ml.cookie;
326 unlock.namelen = res->lockname.len;
327 memcpy(unlock.name, res->lockname.name, unlock.namelen);
329 vec[0].iov_len = sizeof(struct dlm_unlock_lock);
330 vec[0].iov_base = &unlock;
332 if (flags & LKM_PUT_LVB) {
333 /* extra data to send if we are updating lvb */
334 vec[1].iov_len = DLM_LVB_LEN;
335 vec[1].iov_base = lock->lksb->lvb;
339 tmpret = o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG, dlm->key,
340 vec, veclen, owner, &status);
342 // successfully sent and received
343 if (status == DLM_CANCELGRANT)
345 else if (status == DLM_FORWARD) {
346 mlog(0, "master was in-progress. retry\n");
350 lksb->status = status;
353 if (dlm_is_host_down(tmpret)) {
354 /* NOTE: this seems strange, but it is what we want.
355 * when the master goes down during a cancel or
356 * unlock, the recovery code completes the operation
357 * as if the master had not died, then passes the
358 * updated state to the recovery master. this thread
359 * just needs to finish out the operation and call
363 /* something bad. this will BUG in ocfs2 */
364 ret = dlm_err_to_dlm_status(tmpret);
375 * taken: takes and drops res->spinlock
377 * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
378 * return value from dlmunlock_master
380 int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data)
382 struct dlm_ctxt *dlm = data;
383 struct dlm_unlock_lock *unlock = (struct dlm_unlock_lock *)msg->buf;
384 struct dlm_lock_resource *res = NULL;
385 struct list_head *iter;
386 struct dlm_lock *lock = NULL;
387 enum dlm_status status = DLM_NORMAL;
389 struct dlm_lockstatus *lksb = NULL;
392 struct list_head *queue;
394 flags = be32_to_cpu(unlock->flags);
396 if (flags & LKM_GET_LVB) {
397 mlog(ML_ERROR, "bad args! GET_LVB specified on unlock!\n");
401 if ((flags & (LKM_PUT_LVB|LKM_CANCEL)) == (LKM_PUT_LVB|LKM_CANCEL)) {
402 mlog(ML_ERROR, "bad args! cannot modify lvb on a CANCEL "
407 if (unlock->namelen > DLM_LOCKID_NAME_MAX) {
408 mlog(ML_ERROR, "Invalid name length in unlock handler!\n");
415 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
416 "Domain %s not fully joined!\n", dlm->name);
418 mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" : "none");
420 res = dlm_lookup_lockres(dlm, unlock->name, unlock->namelen);
422 /* We assume here that a no lock resource simply means
423 * it was migrated away and destroyed before the other
424 * node could detect it. */
425 mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
426 status = DLM_FORWARD;
432 spin_lock(&res->spinlock);
433 if (res->state & DLM_LOCK_RES_RECOVERING) {
434 spin_unlock(&res->spinlock);
435 mlog(0, "returning DLM_RECOVERING\n");
436 status = DLM_RECOVERING;
440 if (res->state & DLM_LOCK_RES_MIGRATING) {
441 spin_unlock(&res->spinlock);
442 mlog(0, "returning DLM_MIGRATING\n");
443 status = DLM_MIGRATING;
447 if (res->owner != dlm->node_num) {
448 spin_unlock(&res->spinlock);
449 mlog(0, "returning DLM_FORWARD -- not master\n");
450 status = DLM_FORWARD;
454 for (i=0; i<3; i++) {
455 list_for_each(iter, queue) {
456 lock = list_entry(iter, struct dlm_lock, list);
457 if (lock->ml.cookie == unlock->cookie &&
458 lock->ml.node == unlock->node_idx) {
466 /* scan granted -> converting -> blocked queues */
469 spin_unlock(&res->spinlock);
471 status = DLM_IVLOCKID;
475 /* lock was found on queue */
477 /* unlockast only called on originating node */
478 if (flags & LKM_PUT_LVB) {
479 lksb->flags |= DLM_LKSB_PUT_LVB;
480 memcpy(&lksb->lvb[0], &unlock->lvb[0], DLM_LVB_LEN);
483 /* if this is in-progress, propagate the DLM_FORWARD
484 * all the way back out */
485 status = dlmunlock_master(dlm, res, lock, lksb, flags, &ignore);
486 if (status == DLM_FORWARD)
487 mlog(0, "lockres is in progress\n");
489 if (flags & LKM_PUT_LVB)
490 lksb->flags &= ~DLM_LKSB_PUT_LVB;
492 dlm_lockres_calc_usage(dlm, res);
493 dlm_kick_thread(dlm, res);
497 mlog(ML_ERROR, "failed to find lock to unlock! "
499 dlm_get_lock_cookie_node(unlock->cookie),
500 dlm_get_lock_cookie_seq(unlock->cookie));
502 /* send the lksb->status back to the other node */
503 status = lksb->status;
509 dlm_lockres_put(res);
517 static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
518 struct dlm_lock_resource *res,
519 struct dlm_lock *lock,
520 struct dlm_lockstatus *lksb,
523 enum dlm_status status;
525 if (dlm_lock_on_list(&res->blocked, lock)) {
526 /* cancel this outright */
527 lksb->status = DLM_NORMAL;
529 *actions = (DLM_UNLOCK_CALL_AST |
530 DLM_UNLOCK_REMOVE_LOCK);
531 } else if (dlm_lock_on_list(&res->converting, lock)) {
532 /* cancel the request, put back on granted */
533 lksb->status = DLM_NORMAL;
535 *actions = (DLM_UNLOCK_CALL_AST |
536 DLM_UNLOCK_REMOVE_LOCK |
537 DLM_UNLOCK_REGRANT_LOCK |
538 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
539 } else if (dlm_lock_on_list(&res->granted, lock)) {
540 /* too late, already granted. DLM_CANCELGRANT */
541 lksb->status = DLM_CANCELGRANT;
543 *actions = DLM_UNLOCK_CALL_AST;
545 mlog(ML_ERROR, "lock to cancel is not on any list!\n");
546 lksb->status = DLM_IVLOCKID;
547 status = DLM_IVLOCKID;
553 static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
554 struct dlm_lock_resource *res,
555 struct dlm_lock *lock,
556 struct dlm_lockstatus *lksb,
559 enum dlm_status status;
562 if (!dlm_lock_on_list(&res->granted, lock)) {
563 lksb->status = DLM_DENIED;
568 /* unlock granted lock */
569 lksb->status = DLM_NORMAL;
571 *actions = (DLM_UNLOCK_FREE_LOCK |
572 DLM_UNLOCK_CALL_AST |
573 DLM_UNLOCK_REMOVE_LOCK);
578 /* there seems to be no point in doing this async
579 * since (even for the remote case) there is really
580 * no work to queue up... so just do it and fire the
581 * unlockast by hand when done... */
582 enum dlm_status dlmunlock(struct dlm_ctxt *dlm, struct dlm_lockstatus *lksb,
583 int flags, dlm_astunlockfunc_t *unlockast, void *data)
585 enum dlm_status status;
586 struct dlm_lock_resource *res;
587 struct dlm_lock *lock = NULL;
588 int call_ast, is_master;
593 dlm_error(DLM_BADARGS);
597 if (flags & ~(LKM_CANCEL | LKM_VALBLK | LKM_INVVALBLK)) {
598 dlm_error(DLM_BADPARAM);
602 if ((flags & (LKM_VALBLK | LKM_CANCEL)) == (LKM_VALBLK | LKM_CANCEL)) {
603 mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
604 flags &= ~LKM_VALBLK;
607 if (!lksb->lockid || !lksb->lockid->lockres) {
608 dlm_error(DLM_BADPARAM);
618 dlm_lockres_get(res);
621 /* need to retry up here because owner may have changed */
622 mlog(0, "lock=%p res=%p\n", lock, res);
624 spin_lock(&res->spinlock);
625 is_master = (res->owner == dlm->node_num);
626 spin_unlock(&res->spinlock);
629 status = dlmunlock_master(dlm, res, lock, lksb, flags,
631 mlog(0, "done calling dlmunlock_master: returned %d, "
632 "call_ast is %d\n", status, call_ast);
634 status = dlmunlock_remote(dlm, res, lock, lksb, flags,
636 mlog(0, "done calling dlmunlock_remote: returned %d, "
637 "call_ast is %d\n", status, call_ast);
640 if (status == DLM_RECOVERING ||
641 status == DLM_MIGRATING ||
642 status == DLM_FORWARD) {
643 /* We want to go away for a tiny bit to allow recovery
644 * / migration to complete on this resource. I don't
645 * know of any wait queue we could sleep on as this
646 * may be happening on another node. Perhaps the
647 * proper solution is to queue up requests on the
650 /* do we want to yield(); ?? */
653 mlog(0, "retrying unlock due to pending recovery/"
654 "migration/in-progress\n");
659 mlog(0, "calling unlockast(%p, %d)\n", data, lksb->status);
661 /* it is possible that there is one last bast
662 * pending. make sure it is flushed, then
663 * call the unlockast.
664 * not an issue if this is a mastered remotely,
665 * since this lock has been removed from the
666 * lockres queues and cannot be found. */
667 dlm_kick_thread(dlm, NULL);
668 wait_event(dlm->ast_wq,
669 dlm_lock_basts_flushed(dlm, lock));
671 (*unlockast)(data, lksb->status);
674 if (status == DLM_NORMAL) {
675 mlog(0, "kicking the thread\n");
676 dlm_kick_thread(dlm, res);
680 dlm_lockres_calc_usage(dlm, res);
681 dlm_lockres_put(res);
684 mlog(0, "returning status=%d!\n", status);
687 EXPORT_SYMBOL_GPL(dlmunlock);