blob: 2c1f306f8fa5ca8fcaed9de63a1da532a3d4aee2 [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmunlock.c
5 *
6 * underlying calls for unlocking locks
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>
31#include <linux/slab.h>
32#include <linux/highmem.h>
Kurt Hackel6714d8e2005-12-15 14:31:23 -080033#include <linux/init.h>
34#include <linux/sysctl.h>
35#include <linux/random.h>
36#include <linux/blkdev.h>
37#include <linux/socket.h>
38#include <linux/inet.h>
39#include <linux/spinlock.h>
40#include <linux/delay.h>
41
42#include "cluster/heartbeat.h"
43#include "cluster/nodemanager.h"
44#include "cluster/tcp.h"
45
46#include "dlmapi.h"
47#include "dlmcommon.h"
48
49#define MLOG_MASK_PREFIX ML_DLM
50#include "cluster/masklog.h"
51
52#define DLM_UNLOCK_FREE_LOCK 0x00000001
53#define DLM_UNLOCK_CALL_AST 0x00000002
54#define DLM_UNLOCK_REMOVE_LOCK 0x00000004
55#define DLM_UNLOCK_REGRANT_LOCK 0x00000008
56#define DLM_UNLOCK_CLEAR_CONVERT_TYPE 0x00000010
57
58
59static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
60 struct dlm_lock_resource *res,
61 struct dlm_lock *lock,
62 struct dlm_lockstatus *lksb,
63 int *actions);
64static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
65 struct dlm_lock_resource *res,
66 struct dlm_lock *lock,
67 struct dlm_lockstatus *lksb,
68 int *actions);
69
70static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
71 struct dlm_lock_resource *res,
72 struct dlm_lock *lock,
73 struct dlm_lockstatus *lksb,
74 int flags,
75 u8 owner);
76
77
78/*
79 * according to the spec:
80 * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
81 *
82 * flags & LKM_CANCEL != 0: must be converting or blocked
83 * flags & LKM_CANCEL == 0: must be granted
84 *
85 * So to unlock a converting lock, you must first cancel the
86 * convert (passing LKM_CANCEL in flags), then call the unlock
87 * again (with no LKM_CANCEL in flags).
88 */
89
90
91/*
92 * locking:
93 * caller needs: none
94 * taken: res->spinlock and lock->spinlock taken and dropped
95 * held on exit: none
96 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
97 * all callers should have taken an extra ref on lock coming in
98 */
99static enum dlm_status dlmunlock_common(struct dlm_ctxt *dlm,
100 struct dlm_lock_resource *res,
101 struct dlm_lock *lock,
102 struct dlm_lockstatus *lksb,
103 int flags, int *call_ast,
104 int master_node)
105{
106 enum dlm_status status;
107 int actions = 0;
108 int in_use;
109 u8 owner;
110
111 mlog(0, "master_node = %d, valblk = %d\n", master_node,
112 flags & LKM_VALBLK);
113
114 if (master_node)
115 BUG_ON(res->owner != dlm->node_num);
116 else
117 BUG_ON(res->owner == dlm->node_num);
118
Sunil Mushrandabc47d2009-02-03 12:37:15 -0800119 spin_lock(&dlm->ast_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800120 /* We want to be sure that we're not freeing a lock
121 * that still has AST's pending... */
122 in_use = !list_empty(&lock->ast_list);
Sunil Mushrandabc47d2009-02-03 12:37:15 -0800123 spin_unlock(&dlm->ast_lock);
Goldwyn Rodriguesc795b332009-08-20 13:43:19 -0500124 if (in_use && !(flags & LKM_CANCEL)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800125 mlog(ML_ERROR, "lockres %.*s: Someone is calling dlmunlock "
126 "while waiting for an ast!", res->lockname.len,
127 res->lockname.name);
128 return DLM_BADPARAM;
129 }
130
131 spin_lock(&res->spinlock);
132 if (res->state & DLM_LOCK_RES_IN_PROGRESS) {
Goldwyn Rodriguesc795b332009-08-20 13:43:19 -0500133 if (master_node && !(flags & LKM_CANCEL)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800134 mlog(ML_ERROR, "lockres in progress!\n");
135 spin_unlock(&res->spinlock);
136 return DLM_FORWARD;
137 }
138 /* ok for this to sleep if not in a network handler */
139 __dlm_wait_on_lockres(res);
140 res->state |= DLM_LOCK_RES_IN_PROGRESS;
141 }
142 spin_lock(&lock->spinlock);
143
144 if (res->state & DLM_LOCK_RES_RECOVERING) {
145 status = DLM_RECOVERING;
146 goto leave;
147 }
148
Kurt Hackel71ac1062007-01-05 15:02:30 -0800149 if (res->state & DLM_LOCK_RES_MIGRATING) {
150 status = DLM_MIGRATING;
151 goto leave;
152 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800153
154 /* see above for what the spec says about
155 * LKM_CANCEL and the lock queue state */
156 if (flags & LKM_CANCEL)
157 status = dlm_get_cancel_actions(dlm, res, lock, lksb, &actions);
158 else
159 status = dlm_get_unlock_actions(dlm, res, lock, lksb, &actions);
160
Kurt Hackel34e3d182006-07-15 10:22:39 -0700161 if (status != DLM_NORMAL && (status != DLM_CANCELGRANT || !master_node))
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800162 goto leave;
163
164 /* By now this has been masked out of cancel requests. */
165 if (flags & LKM_VALBLK) {
166 /* make the final update to the lvb */
167 if (master_node)
168 memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
169 else
170 flags |= LKM_PUT_LVB; /* let the send function
171 * handle it. */
172 }
173
174 if (!master_node) {
175 owner = res->owner;
176 /* drop locks and send message */
177 if (flags & LKM_CANCEL)
178 lock->cancel_pending = 1;
179 else
180 lock->unlock_pending = 1;
181 spin_unlock(&lock->spinlock);
182 spin_unlock(&res->spinlock);
183 status = dlm_send_remote_unlock_request(dlm, res, lock, lksb,
184 flags, owner);
185 spin_lock(&res->spinlock);
186 spin_lock(&lock->spinlock);
187 /* if the master told us the lock was already granted,
188 * let the ast handle all of these actions */
Kurt Hackela23eac92006-06-18 21:28:01 -0700189 if (status == DLM_CANCELGRANT) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800190 actions &= ~(DLM_UNLOCK_REMOVE_LOCK|
191 DLM_UNLOCK_REGRANT_LOCK|
192 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
Sunil Mushran2bd63212010-01-25 16:57:38 -0800193 } else if (status == DLM_RECOVERING ||
194 status == DLM_MIGRATING ||
Kurt Hackele2faea42006-01-12 14:24:55 -0800195 status == DLM_FORWARD) {
196 /* must clear the actions because this unlock
197 * is about to be retried. cannot free or do
198 * any list manipulation. */
199 mlog(0, "%s:%.*s: clearing actions, %s\n",
200 dlm->name, res->lockname.len,
201 res->lockname.name,
202 status==DLM_RECOVERING?"recovering":
203 (status==DLM_MIGRATING?"migrating":
204 "forward"));
205 actions = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800206 }
207 if (flags & LKM_CANCEL)
208 lock->cancel_pending = 0;
209 else
210 lock->unlock_pending = 0;
211
212 }
213
214 /* get an extra ref on lock. if we are just switching
215 * lists here, we dont want the lock to go away. */
216 dlm_lock_get(lock);
217
218 if (actions & DLM_UNLOCK_REMOVE_LOCK) {
219 list_del_init(&lock->list);
220 dlm_lock_put(lock);
221 }
222 if (actions & DLM_UNLOCK_REGRANT_LOCK) {
223 dlm_lock_get(lock);
224 list_add_tail(&lock->list, &res->granted);
225 }
226 if (actions & DLM_UNLOCK_CLEAR_CONVERT_TYPE) {
227 mlog(0, "clearing convert_type at %smaster node\n",
228 master_node ? "" : "non-");
229 lock->ml.convert_type = LKM_IVMODE;
230 }
231
232 /* remove the extra ref on lock */
233 dlm_lock_put(lock);
234
235leave:
236 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
237 if (!dlm_lock_on_list(&res->converting, lock))
238 BUG_ON(lock->ml.convert_type != LKM_IVMODE);
239 else
240 BUG_ON(lock->ml.convert_type == LKM_IVMODE);
241 spin_unlock(&lock->spinlock);
242 spin_unlock(&res->spinlock);
243 wake_up(&res->wq);
244
245 /* let the caller's final dlm_lock_put handle the actual kfree */
246 if (actions & DLM_UNLOCK_FREE_LOCK) {
247 /* this should always be coupled with list removal */
248 BUG_ON(!(actions & DLM_UNLOCK_REMOVE_LOCK));
Kurt Hackel29004852006-03-02 16:43:36 -0800249 mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
Kurt Hackel74aa2582007-01-17 15:11:36 -0800250 dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
251 dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
Kurt Hackel29004852006-03-02 16:43:36 -0800252 atomic_read(&lock->lock_refs.refcount)-1);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800253 dlm_lock_put(lock);
254 }
255 if (actions & DLM_UNLOCK_CALL_AST)
256 *call_ast = 1;
257
258 /* if cancel or unlock succeeded, lvb work is done */
259 if (status == DLM_NORMAL)
260 lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
261
262 return status;
263}
264
265void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
266 struct dlm_lock *lock)
267{
268 /* leave DLM_LKSB_PUT_LVB on the lksb so any final
269 * update of the lvb will be sent to the new master */
270 list_del_init(&lock->list);
271}
272
273void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
274 struct dlm_lock *lock)
275{
Akinobu Mitaf1166292006-06-26 00:24:46 -0700276 list_move_tail(&lock->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800277 lock->ml.convert_type = LKM_IVMODE;
278}
279
280
281static inline enum dlm_status dlmunlock_master(struct dlm_ctxt *dlm,
282 struct dlm_lock_resource *res,
283 struct dlm_lock *lock,
284 struct dlm_lockstatus *lksb,
285 int flags,
286 int *call_ast)
287{
288 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 1);
289}
290
291static inline enum dlm_status dlmunlock_remote(struct dlm_ctxt *dlm,
292 struct dlm_lock_resource *res,
293 struct dlm_lock *lock,
294 struct dlm_lockstatus *lksb,
295 int flags, int *call_ast)
296{
297 return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 0);
298}
299
300/*
301 * locking:
302 * caller needs: none
303 * taken: none
304 * held on exit: none
305 * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
306 */
307static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
308 struct dlm_lock_resource *res,
309 struct dlm_lock *lock,
310 struct dlm_lockstatus *lksb,
311 int flags,
312 u8 owner)
313{
314 struct dlm_unlock_lock unlock;
315 int tmpret;
316 enum dlm_status ret;
317 int status = 0;
318 struct kvec vec[2];
319 size_t veclen = 1;
320
321 mlog_entry("%.*s\n", res->lockname.len, res->lockname.name);
Kurt Hackel0032abd2006-05-01 14:39:57 -0700322
Kurt Hackel2580a582006-04-27 17:59:46 -0700323 if (owner == dlm->node_num) {
324 /* ended up trying to contact ourself. this means
325 * that the lockres had been remote but became local
326 * via a migration. just retry it, now as local */
327 mlog(0, "%s:%.*s: this node became the master due to a "
328 "migration, re-evaluate now\n", dlm->name,
329 res->lockname.len, res->lockname.name);
330 return DLM_FORWARD;
331 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800332
333 memset(&unlock, 0, sizeof(unlock));
334 unlock.node_idx = dlm->node_num;
335 unlock.flags = cpu_to_be32(flags);
336 unlock.cookie = lock->ml.cookie;
337 unlock.namelen = res->lockname.len;
338 memcpy(unlock.name, res->lockname.name, unlock.namelen);
339
340 vec[0].iov_len = sizeof(struct dlm_unlock_lock);
341 vec[0].iov_base = &unlock;
342
343 if (flags & LKM_PUT_LVB) {
344 /* extra data to send if we are updating lvb */
345 vec[1].iov_len = DLM_LVB_LEN;
346 vec[1].iov_base = lock->lksb->lvb;
347 veclen++;
348 }
349
350 tmpret = o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG, dlm->key,
351 vec, veclen, owner, &status);
352 if (tmpret >= 0) {
353 // successfully sent and received
Kurt Hackela23eac92006-06-18 21:28:01 -0700354 if (status == DLM_FORWARD)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800355 mlog(0, "master was in-progress. retry\n");
Kurt Hackela23eac92006-06-18 21:28:01 -0700356 ret = status;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800357 } else {
Wengang Wanga5196ec2010-03-30 12:09:22 +0800358 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
359 "node %u\n", tmpret, DLM_UNLOCK_LOCK_MSG, dlm->key, owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800360 if (dlm_is_host_down(tmpret)) {
361 /* NOTE: this seems strange, but it is what we want.
362 * when the master goes down during a cancel or
363 * unlock, the recovery code completes the operation
364 * as if the master had not died, then passes the
365 * updated state to the recovery master. this thread
366 * just needs to finish out the operation and call
367 * the unlockast. */
368 ret = DLM_NORMAL;
369 } else {
370 /* something bad. this will BUG in ocfs2 */
371 ret = dlm_err_to_dlm_status(tmpret);
372 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800373 }
374
375 return ret;
376}
377
378/*
379 * locking:
380 * caller needs: none
381 * taken: takes and drops res->spinlock
382 * held on exit: none
383 * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
384 * return value from dlmunlock_master
385 */
Kurt Hackeld74c9802007-01-17 17:04:25 -0800386int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
387 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800388{
389 struct dlm_ctxt *dlm = data;
390 struct dlm_unlock_lock *unlock = (struct dlm_unlock_lock *)msg->buf;
391 struct dlm_lock_resource *res = NULL;
392 struct list_head *iter;
393 struct dlm_lock *lock = NULL;
394 enum dlm_status status = DLM_NORMAL;
395 int found = 0, i;
396 struct dlm_lockstatus *lksb = NULL;
397 int ignore;
398 u32 flags;
399 struct list_head *queue;
400
401 flags = be32_to_cpu(unlock->flags);
402
403 if (flags & LKM_GET_LVB) {
404 mlog(ML_ERROR, "bad args! GET_LVB specified on unlock!\n");
405 return DLM_BADARGS;
406 }
407
408 if ((flags & (LKM_PUT_LVB|LKM_CANCEL)) == (LKM_PUT_LVB|LKM_CANCEL)) {
409 mlog(ML_ERROR, "bad args! cannot modify lvb on a CANCEL "
410 "request!\n");
411 return DLM_BADARGS;
412 }
413
414 if (unlock->namelen > DLM_LOCKID_NAME_MAX) {
415 mlog(ML_ERROR, "Invalid name length in unlock handler!\n");
416 return DLM_IVBUFLEN;
417 }
418
419 if (!dlm_grab(dlm))
420 return DLM_REJECTED;
421
422 mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
423 "Domain %s not fully joined!\n", dlm->name);
424
425 mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" : "none");
426
427 res = dlm_lookup_lockres(dlm, unlock->name, unlock->namelen);
428 if (!res) {
429 /* We assume here that a no lock resource simply means
430 * it was migrated away and destroyed before the other
431 * node could detect it. */
432 mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
433 status = DLM_FORWARD;
434 goto not_found;
435 }
436
437 queue=&res->granted;
438 found = 0;
439 spin_lock(&res->spinlock);
440 if (res->state & DLM_LOCK_RES_RECOVERING) {
441 spin_unlock(&res->spinlock);
442 mlog(0, "returning DLM_RECOVERING\n");
443 status = DLM_RECOVERING;
444 goto leave;
445 }
446
447 if (res->state & DLM_LOCK_RES_MIGRATING) {
448 spin_unlock(&res->spinlock);
449 mlog(0, "returning DLM_MIGRATING\n");
450 status = DLM_MIGRATING;
451 goto leave;
452 }
453
454 if (res->owner != dlm->node_num) {
455 spin_unlock(&res->spinlock);
456 mlog(0, "returning DLM_FORWARD -- not master\n");
457 status = DLM_FORWARD;
458 goto leave;
459 }
460
461 for (i=0; i<3; i++) {
462 list_for_each(iter, queue) {
463 lock = list_entry(iter, struct dlm_lock, list);
464 if (lock->ml.cookie == unlock->cookie &&
465 lock->ml.node == unlock->node_idx) {
466 dlm_lock_get(lock);
467 found = 1;
468 break;
469 }
470 }
471 if (found)
472 break;
473 /* scan granted -> converting -> blocked queues */
474 queue++;
475 }
476 spin_unlock(&res->spinlock);
477 if (!found) {
478 status = DLM_IVLOCKID;
479 goto not_found;
480 }
481
482 /* lock was found on queue */
483 lksb = lock->lksb;
Kurt Hackel4b1af772006-06-26 15:17:47 -0700484 if (flags & (LKM_VALBLK|LKM_PUT_LVB) &&
485 lock->ml.type != LKM_EXMODE)
486 flags &= ~(LKM_VALBLK|LKM_PUT_LVB);
487
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800488 /* unlockast only called on originating node */
489 if (flags & LKM_PUT_LVB) {
490 lksb->flags |= DLM_LKSB_PUT_LVB;
491 memcpy(&lksb->lvb[0], &unlock->lvb[0], DLM_LVB_LEN);
492 }
493
494 /* if this is in-progress, propagate the DLM_FORWARD
495 * all the way back out */
496 status = dlmunlock_master(dlm, res, lock, lksb, flags, &ignore);
497 if (status == DLM_FORWARD)
498 mlog(0, "lockres is in progress\n");
499
500 if (flags & LKM_PUT_LVB)
501 lksb->flags &= ~DLM_LKSB_PUT_LVB;
502
503 dlm_lockres_calc_usage(dlm, res);
504 dlm_kick_thread(dlm, res);
505
506not_found:
507 if (!found)
508 mlog(ML_ERROR, "failed to find lock to unlock! "
Kurt Hackel29004852006-03-02 16:43:36 -0800509 "cookie=%u:%llu\n",
Kurt Hackel74aa2582007-01-17 15:11:36 -0800510 dlm_get_lock_cookie_node(be64_to_cpu(unlock->cookie)),
511 dlm_get_lock_cookie_seq(be64_to_cpu(unlock->cookie)));
Kurt Hackela23eac92006-06-18 21:28:01 -0700512 else
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800513 dlm_lock_put(lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800514
515leave:
516 if (res)
517 dlm_lockres_put(res);
518
519 dlm_put(dlm);
520
521 return status;
522}
523
524
525static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
526 struct dlm_lock_resource *res,
527 struct dlm_lock *lock,
528 struct dlm_lockstatus *lksb,
529 int *actions)
530{
531 enum dlm_status status;
532
533 if (dlm_lock_on_list(&res->blocked, lock)) {
534 /* cancel this outright */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800535 status = DLM_NORMAL;
536 *actions = (DLM_UNLOCK_CALL_AST |
537 DLM_UNLOCK_REMOVE_LOCK);
538 } else if (dlm_lock_on_list(&res->converting, lock)) {
539 /* cancel the request, put back on granted */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800540 status = DLM_NORMAL;
541 *actions = (DLM_UNLOCK_CALL_AST |
542 DLM_UNLOCK_REMOVE_LOCK |
543 DLM_UNLOCK_REGRANT_LOCK |
544 DLM_UNLOCK_CLEAR_CONVERT_TYPE);
545 } else if (dlm_lock_on_list(&res->granted, lock)) {
Kurt Hackela23eac92006-06-18 21:28:01 -0700546 /* too late, already granted. */
547 status = DLM_CANCELGRANT;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800548 *actions = DLM_UNLOCK_CALL_AST;
549 } else {
550 mlog(ML_ERROR, "lock to cancel is not on any list!\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800551 status = DLM_IVLOCKID;
552 *actions = 0;
553 }
554 return status;
555}
556
557static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
558 struct dlm_lock_resource *res,
559 struct dlm_lock *lock,
560 struct dlm_lockstatus *lksb,
561 int *actions)
562{
563 enum dlm_status status;
564
565 /* unlock request */
566 if (!dlm_lock_on_list(&res->granted, lock)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800567 status = DLM_DENIED;
568 dlm_error(status);
569 *actions = 0;
570 } else {
571 /* unlock granted lock */
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800572 status = DLM_NORMAL;
573 *actions = (DLM_UNLOCK_FREE_LOCK |
574 DLM_UNLOCK_CALL_AST |
575 DLM_UNLOCK_REMOVE_LOCK);
576 }
577 return status;
578}
579
580/* there seems to be no point in doing this async
581 * since (even for the remote case) there is really
582 * no work to queue up... so just do it and fire the
583 * unlockast by hand when done... */
584enum dlm_status dlmunlock(struct dlm_ctxt *dlm, struct dlm_lockstatus *lksb,
585 int flags, dlm_astunlockfunc_t *unlockast, void *data)
586{
587 enum dlm_status status;
588 struct dlm_lock_resource *res;
589 struct dlm_lock *lock = NULL;
590 int call_ast, is_master;
591
592 mlog_entry_void();
593
594 if (!lksb) {
595 dlm_error(DLM_BADARGS);
596 return DLM_BADARGS;
597 }
598
599 if (flags & ~(LKM_CANCEL | LKM_VALBLK | LKM_INVVALBLK)) {
600 dlm_error(DLM_BADPARAM);
601 return DLM_BADPARAM;
602 }
603
604 if ((flags & (LKM_VALBLK | LKM_CANCEL)) == (LKM_VALBLK | LKM_CANCEL)) {
605 mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
606 flags &= ~LKM_VALBLK;
607 }
608
609 if (!lksb->lockid || !lksb->lockid->lockres) {
610 dlm_error(DLM_BADPARAM);
611 return DLM_BADPARAM;
612 }
613
614 lock = lksb->lockid;
615 BUG_ON(!lock);
616 dlm_lock_get(lock);
617
618 res = lock->lockres;
619 BUG_ON(!res);
620 dlm_lockres_get(res);
621retry:
622 call_ast = 0;
623 /* need to retry up here because owner may have changed */
624 mlog(0, "lock=%p res=%p\n", lock, res);
625
626 spin_lock(&res->spinlock);
627 is_master = (res->owner == dlm->node_num);
Kurt Hackel4b1af772006-06-26 15:17:47 -0700628 if (flags & LKM_VALBLK && lock->ml.type != LKM_EXMODE)
629 flags &= ~LKM_VALBLK;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800630 spin_unlock(&res->spinlock);
631
632 if (is_master) {
633 status = dlmunlock_master(dlm, res, lock, lksb, flags,
634 &call_ast);
635 mlog(0, "done calling dlmunlock_master: returned %d, "
636 "call_ast is %d\n", status, call_ast);
637 } else {
638 status = dlmunlock_remote(dlm, res, lock, lksb, flags,
639 &call_ast);
640 mlog(0, "done calling dlmunlock_remote: returned %d, "
641 "call_ast is %d\n", status, call_ast);
642 }
643
644 if (status == DLM_RECOVERING ||
645 status == DLM_MIGRATING ||
646 status == DLM_FORWARD) {
647 /* We want to go away for a tiny bit to allow recovery
648 * / migration to complete on this resource. I don't
649 * know of any wait queue we could sleep on as this
650 * may be happening on another node. Perhaps the
651 * proper solution is to queue up requests on the
652 * other end? */
653
654 /* do we want to yield(); ?? */
655 msleep(50);
656
657 mlog(0, "retrying unlock due to pending recovery/"
658 "migration/in-progress\n");
659 goto retry;
660 }
661
662 if (call_ast) {
Kurt Hackela23eac92006-06-18 21:28:01 -0700663 mlog(0, "calling unlockast(%p, %d)\n", data, status);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800664 if (is_master) {
Sunil Mushran2bd63212010-01-25 16:57:38 -0800665 /* it is possible that there is one last bast
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800666 * pending. make sure it is flushed, then
667 * call the unlockast.
668 * not an issue if this is a mastered remotely,
669 * since this lock has been removed from the
670 * lockres queues and cannot be found. */
671 dlm_kick_thread(dlm, NULL);
Sunil Mushran2bd63212010-01-25 16:57:38 -0800672 wait_event(dlm->ast_wq,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800673 dlm_lock_basts_flushed(dlm, lock));
674 }
Kurt Hackela23eac92006-06-18 21:28:01 -0700675 (*unlockast)(data, status);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800676 }
677
Kurt Hackela23eac92006-06-18 21:28:01 -0700678 if (status == DLM_CANCELGRANT)
679 status = DLM_NORMAL;
680
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800681 if (status == DLM_NORMAL) {
682 mlog(0, "kicking the thread\n");
683 dlm_kick_thread(dlm, res);
684 } else
685 dlm_error(status);
686
687 dlm_lockres_calc_usage(dlm, res);
688 dlm_lockres_put(res);
689 dlm_lock_put(lock);
690
691 mlog(0, "returning status=%d!\n", status);
692 return status;
693}
694EXPORT_SYMBOL_GPL(dlmunlock);
695