blob: c7f3e22bda1ea35c75ac780e725c35aef9015fe4 [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 * dlmlock.c
5 *
6 * underlying calls for lock creation
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
43#include "cluster/heartbeat.h"
44#include "cluster/nodemanager.h"
45#include "cluster/tcp.h"
46
47#include "dlmapi.h"
48#include "dlmcommon.h"
49
50#include "dlmconvert.h"
51
52#define MLOG_MASK_PREFIX ML_DLM
53#include "cluster/masklog.h"
54
Sunil Mushran724bdca2008-03-10 15:16:20 -070055static struct kmem_cache *dlm_lock_cache = NULL;
56
Ingo Molnar34af9462006-06-27 02:53:55 -070057static DEFINE_SPINLOCK(dlm_cookie_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080058static u64 dlm_next_cookie = 1;
59
60static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
61 struct dlm_lock_resource *res,
62 struct dlm_lock *lock, int flags);
63static void dlm_init_lock(struct dlm_lock *newlock, int type,
64 u8 node, u64 cookie);
65static void dlm_lock_release(struct kref *kref);
66static void dlm_lock_detach_lockres(struct dlm_lock *lock);
67
Sunil Mushran724bdca2008-03-10 15:16:20 -070068int dlm_init_lock_cache(void)
69{
70 dlm_lock_cache = kmem_cache_create("o2dlm_lock",
71 sizeof(struct dlm_lock),
72 0, SLAB_HWCACHE_ALIGN, NULL);
73 if (dlm_lock_cache == NULL)
74 return -ENOMEM;
75 return 0;
76}
77
78void dlm_destroy_lock_cache(void)
79{
80 if (dlm_lock_cache)
81 kmem_cache_destroy(dlm_lock_cache);
82}
83
Kurt Hackel6714d8e2005-12-15 14:31:23 -080084/* Tell us whether we can grant a new lock request.
85 * locking:
86 * caller needs: res->spinlock
87 * taken: none
88 * held on exit: none
89 * returns: 1 if the lock can be granted, 0 otherwise.
90 */
91static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
92 struct dlm_lock *lock)
93{
94 struct list_head *iter;
95 struct dlm_lock *tmplock;
96
97 list_for_each(iter, &res->granted) {
98 tmplock = list_entry(iter, struct dlm_lock, list);
99
100 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
101 return 0;
102 }
103
104 list_for_each(iter, &res->converting) {
105 tmplock = list_entry(iter, struct dlm_lock, list);
106
107 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
108 return 0;
Wengang Wang66f45002010-12-08 20:34:39 +0800109 if (!dlm_lock_compatible(tmplock->ml.convert_type,
110 lock->ml.type))
111 return 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800112 }
113
114 return 1;
115}
116
117/* performs lock creation at the lockres master site
118 * locking:
119 * caller needs: none
120 * taken: takes and drops res->spinlock
121 * held on exit: none
122 * returns: DLM_NORMAL, DLM_NOTQUEUED
123 */
124static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
125 struct dlm_lock_resource *res,
126 struct dlm_lock *lock, int flags)
127{
128 int call_ast = 0, kick_thread = 0;
129 enum dlm_status status = DLM_NORMAL;
130
Tao Maef6b6892011-02-21 11:10:44 +0800131 mlog(0, "type=%d\n", lock->ml.type);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800132
133 spin_lock(&res->spinlock);
134 /* if called from dlm_create_lock_handler, need to
135 * ensure it will not sleep in dlm_wait_on_lockres */
136 status = __dlm_lockres_state_to_status(res);
137 if (status != DLM_NORMAL &&
138 lock->ml.node != dlm->node_num) {
139 /* erf. state changed after lock was dropped. */
140 spin_unlock(&res->spinlock);
141 dlm_error(status);
142 return status;
143 }
144 __dlm_wait_on_lockres(res);
145 __dlm_lockres_reserve_ast(res);
146
147 if (dlm_can_grant_new_lock(res, lock)) {
148 mlog(0, "I can grant this lock right away\n");
149 /* got it right away */
150 lock->lksb->status = DLM_NORMAL;
151 status = DLM_NORMAL;
152 dlm_lock_get(lock);
153 list_add_tail(&lock->list, &res->granted);
154
155 /* for the recovery lock, we can't allow the ast
156 * to be queued since the dlmthread is already
157 * frozen. but the recovery lock is always locked
158 * with LKM_NOQUEUE so we do not need the ast in
159 * this special case */
160 if (!dlm_is_recovery_lock(res->lockname.name,
161 res->lockname.len)) {
162 kick_thread = 1;
163 call_ast = 1;
Kurt Hackelc03872f2006-03-06 14:08:49 -0800164 } else {
165 mlog(0, "%s: returning DLM_NORMAL to "
166 "node %u for reco lock\n", dlm->name,
167 lock->ml.node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800168 }
169 } else {
170 /* for NOQUEUE request, unless we get the
171 * lock right away, return DLM_NOTQUEUED */
Kurt Hackelc03872f2006-03-06 14:08:49 -0800172 if (flags & LKM_NOQUEUE) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800173 status = DLM_NOTQUEUED;
Kurt Hackelc03872f2006-03-06 14:08:49 -0800174 if (dlm_is_recovery_lock(res->lockname.name,
175 res->lockname.len)) {
176 mlog(0, "%s: returning NOTQUEUED to "
177 "node %u for reco lock\n", dlm->name,
178 lock->ml.node);
179 }
180 } else {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800181 dlm_lock_get(lock);
182 list_add_tail(&lock->list, &res->blocked);
183 kick_thread = 1;
184 }
185 }
Kurt Hackelba2bf212006-12-01 14:47:20 -0800186 /* reduce the inflight count, this may result in the lockres
187 * being purged below during calc_usage */
188 if (lock->ml.node == dlm->node_num)
189 dlm_lockres_drop_inflight_ref(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800190
191 spin_unlock(&res->spinlock);
192 wake_up(&res->wq);
193
194 /* either queue the ast or release it */
195 if (call_ast)
196 dlm_queue_ast(dlm, lock);
197 else
198 dlm_lockres_release_ast(dlm, res);
199
200 dlm_lockres_calc_usage(dlm, res);
201 if (kick_thread)
202 dlm_kick_thread(dlm, res);
203
204 return status;
205}
206
207void dlm_revert_pending_lock(struct dlm_lock_resource *res,
208 struct dlm_lock *lock)
209{
210 /* remove from local queue if it failed */
211 list_del_init(&lock->list);
212 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
213}
214
215
216/*
217 * locking:
218 * caller needs: none
219 * taken: takes and drops res->spinlock
220 * held on exit: none
221 * returns: DLM_DENIED, DLM_RECOVERING, or net status
222 */
223static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
224 struct dlm_lock_resource *res,
225 struct dlm_lock *lock, int flags)
226{
227 enum dlm_status status = DLM_DENIED;
Kurt Hackel2abaf972006-05-01 13:27:10 -0700228 int lockres_changed = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800229
Tao Maef6b6892011-02-21 11:10:44 +0800230 mlog(0, "type=%d, lockres %.*s, flags = 0x%x\n",
231 lock->ml.type, res->lockname.len,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800232 res->lockname.name, flags);
233
234 spin_lock(&res->spinlock);
235
236 /* will exit this call with spinlock held */
237 __dlm_wait_on_lockres(res);
238 res->state |= DLM_LOCK_RES_IN_PROGRESS;
239
240 /* add lock to local (secondary) queue */
241 dlm_lock_get(lock);
242 list_add_tail(&lock->list, &res->blocked);
243 lock->lock_pending = 1;
244 spin_unlock(&res->spinlock);
245
246 /* spec seems to say that you will get DLM_NORMAL when the lock
247 * has been queued, meaning we need to wait for a reply here. */
248 status = dlm_send_remote_lock_request(dlm, res, lock, flags);
249
250 spin_lock(&res->spinlock);
251 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
252 lock->lock_pending = 0;
253 if (status != DLM_NORMAL) {
Kurt Hackelc8df4122006-05-01 13:47:50 -0700254 if (status == DLM_RECOVERING &&
255 dlm_is_recovery_lock(res->lockname.name,
256 res->lockname.len)) {
257 /* recovery lock was mastered by dead node.
258 * we need to have calc_usage shoot down this
259 * lockres and completely remaster it. */
260 mlog(0, "%s: recovery lock was owned by "
261 "dead node %u, remaster it now.\n",
262 dlm->name, res->owner);
263 } else if (status != DLM_NOTQUEUED) {
Kurt Hackelc87a9ae2006-05-01 13:30:49 -0700264 /*
265 * DO NOT call calc_usage, as this would unhash
266 * the remote lockres before we ever get to use
267 * it. treat as if we never made any change to
268 * the lockres.
269 */
270 lockres_changed = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800271 dlm_error(status);
Kurt Hackelc87a9ae2006-05-01 13:30:49 -0700272 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800273 dlm_revert_pending_lock(res, lock);
274 dlm_lock_put(lock);
Sunil Mushran2bd63212010-01-25 16:57:38 -0800275 } else if (dlm_is_recovery_lock(res->lockname.name,
Kurt Hackel558c70c2006-01-18 17:07:47 -0800276 res->lockname.len)) {
277 /* special case for the $RECOVERY lock.
278 * there will never be an AST delivered to put
279 * this lock on the proper secondary queue
280 * (granted), so do it manually. */
281 mlog(0, "%s: $RECOVERY lock for this node (%u) is "
282 "mastered by %u; got lock, manually granting (no ast)\n",
283 dlm->name, dlm->node_num, res->owner);
Akinobu Mitaf1166292006-06-26 00:24:46 -0700284 list_move_tail(&lock->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800285 }
286 spin_unlock(&res->spinlock);
287
Kurt Hackel2abaf972006-05-01 13:27:10 -0700288 if (lockres_changed)
289 dlm_lockres_calc_usage(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800290
291 wake_up(&res->wq);
292 return status;
293}
294
295
296/* for remote lock creation.
297 * locking:
298 * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
299 * taken: none
300 * held on exit: none
301 * returns: DLM_NOLOCKMGR, or net status
302 */
303static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
304 struct dlm_lock_resource *res,
305 struct dlm_lock *lock, int flags)
306{
307 struct dlm_create_lock create;
308 int tmpret, status = 0;
309 enum dlm_status ret;
310
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800311 memset(&create, 0, sizeof(create));
312 create.node_idx = dlm->node_num;
313 create.requested_type = lock->ml.type;
314 create.cookie = lock->ml.cookie;
315 create.namelen = res->lockname.len;
316 create.flags = cpu_to_be32(flags);
317 memcpy(create.name, res->lockname.name, create.namelen);
318
319 tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
320 sizeof(create), res->owner, &status);
321 if (tmpret >= 0) {
Sunil Mushran8decab32011-07-24 10:23:54 -0700322 ret = status;
Kurt Hackel495ac962006-05-01 14:34:08 -0700323 if (ret == DLM_REJECTED) {
Sunil Mushran8decab32011-07-24 10:23:54 -0700324 mlog(ML_ERROR, "%s: res %.*s, Stale lockres no longer "
325 "owned by node %u. That node is coming back up "
326 "currently.\n", dlm->name, create.namelen,
Kurt Hackele4eb0362006-05-01 11:46:59 -0700327 create.name, res->owner);
328 dlm_print_one_lock_resource(res);
329 BUG();
330 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800331 } else {
Sunil Mushran8decab32011-07-24 10:23:54 -0700332 mlog(ML_ERROR, "%s: res %.*s, Error %d send CREATE LOCK to "
333 "node %u\n", dlm->name, create.namelen, create.name,
334 tmpret, res->owner);
335 if (dlm_is_host_down(tmpret))
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800336 ret = DLM_RECOVERING;
Sunil Mushran8decab32011-07-24 10:23:54 -0700337 else
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800338 ret = dlm_err_to_dlm_status(tmpret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800339 }
340
341 return ret;
342}
343
344void dlm_lock_get(struct dlm_lock *lock)
345{
346 kref_get(&lock->lock_refs);
347}
348
349void dlm_lock_put(struct dlm_lock *lock)
350{
351 kref_put(&lock->lock_refs, dlm_lock_release);
352}
353
354static void dlm_lock_release(struct kref *kref)
355{
356 struct dlm_lock *lock;
357
358 lock = container_of(kref, struct dlm_lock, lock_refs);
359
360 BUG_ON(!list_empty(&lock->list));
361 BUG_ON(!list_empty(&lock->ast_list));
362 BUG_ON(!list_empty(&lock->bast_list));
363 BUG_ON(lock->ast_pending);
364 BUG_ON(lock->bast_pending);
365
366 dlm_lock_detach_lockres(lock);
367
368 if (lock->lksb_kernel_allocated) {
369 mlog(0, "freeing kernel-allocated lksb\n");
370 kfree(lock->lksb);
371 }
Sunil Mushran724bdca2008-03-10 15:16:20 -0700372 kmem_cache_free(dlm_lock_cache, lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800373}
374
375/* associate a lock with it's lockres, getting a ref on the lockres */
376void dlm_lock_attach_lockres(struct dlm_lock *lock,
377 struct dlm_lock_resource *res)
378{
379 dlm_lockres_get(res);
380 lock->lockres = res;
381}
382
383/* drop ref on lockres, if there is still one associated with lock */
384static void dlm_lock_detach_lockres(struct dlm_lock *lock)
385{
386 struct dlm_lock_resource *res;
387
388 res = lock->lockres;
389 if (res) {
390 lock->lockres = NULL;
391 mlog(0, "removing lock's lockres reference\n");
392 dlm_lockres_put(res);
393 }
394}
395
396static void dlm_init_lock(struct dlm_lock *newlock, int type,
397 u8 node, u64 cookie)
398{
399 INIT_LIST_HEAD(&newlock->list);
400 INIT_LIST_HEAD(&newlock->ast_list);
401 INIT_LIST_HEAD(&newlock->bast_list);
402 spin_lock_init(&newlock->spinlock);
403 newlock->ml.type = type;
404 newlock->ml.convert_type = LKM_IVMODE;
405 newlock->ml.highest_blocked = LKM_IVMODE;
406 newlock->ml.node = node;
407 newlock->ml.pad1 = 0;
408 newlock->ml.list = 0;
409 newlock->ml.flags = 0;
410 newlock->ast = NULL;
411 newlock->bast = NULL;
412 newlock->astdata = NULL;
413 newlock->ml.cookie = cpu_to_be64(cookie);
414 newlock->ast_pending = 0;
415 newlock->bast_pending = 0;
416 newlock->convert_pending = 0;
417 newlock->lock_pending = 0;
418 newlock->unlock_pending = 0;
419 newlock->cancel_pending = 0;
420 newlock->lksb_kernel_allocated = 0;
421
422 kref_init(&newlock->lock_refs);
423}
424
425struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
426 struct dlm_lockstatus *lksb)
427{
428 struct dlm_lock *lock;
429 int kernel_allocated = 0;
430
Julia Lawall3914ed02010-05-11 20:28:14 +0200431 lock = kmem_cache_zalloc(dlm_lock_cache, GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800432 if (!lock)
433 return NULL;
434
435 if (!lksb) {
436 /* zero memory only if kernel-allocated */
Robert P. J. Daycd861282006-12-13 00:34:52 -0800437 lksb = kzalloc(sizeof(*lksb), GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800438 if (!lksb) {
439 kfree(lock);
440 return NULL;
441 }
442 kernel_allocated = 1;
443 }
444
445 dlm_init_lock(lock, type, node, cookie);
446 if (kernel_allocated)
447 lock->lksb_kernel_allocated = 1;
448 lock->lksb = lksb;
449 lksb->lockid = lock;
450 return lock;
451}
452
453/* handler for lock creation net message
454 * locking:
455 * caller needs: none
456 * taken: takes and drops res->spinlock
457 * held on exit: none
458 * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
459 */
Kurt Hackeld74c9802007-01-17 17:04:25 -0800460int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
461 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800462{
463 struct dlm_ctxt *dlm = data;
464 struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
465 struct dlm_lock_resource *res = NULL;
466 struct dlm_lock *newlock = NULL;
467 struct dlm_lockstatus *lksb = NULL;
468 enum dlm_status status = DLM_NORMAL;
469 char *name;
470 unsigned int namelen;
471
472 BUG_ON(!dlm);
473
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800474 if (!dlm_grab(dlm))
475 return DLM_REJECTED;
476
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800477 name = create->name;
478 namelen = create->namelen;
Kurt Hackel495ac962006-05-01 14:34:08 -0700479 status = DLM_REJECTED;
Kurt Hackele4eb0362006-05-01 11:46:59 -0700480 if (!dlm_domain_fully_joined(dlm)) {
481 mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
482 "sending a create_lock message for lock %.*s!\n",
483 dlm->name, create->node_idx, namelen, name);
484 dlm_error(status);
485 goto leave;
486 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800487
488 status = DLM_IVBUFLEN;
489 if (namelen > DLM_LOCKID_NAME_MAX) {
490 dlm_error(status);
491 goto leave;
492 }
493
494 status = DLM_SYSERR;
495 newlock = dlm_new_lock(create->requested_type,
496 create->node_idx,
497 be64_to_cpu(create->cookie), NULL);
498 if (!newlock) {
499 dlm_error(status);
500 goto leave;
501 }
502
503 lksb = newlock->lksb;
504
505 if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
506 lksb->flags |= DLM_LKSB_GET_LVB;
507 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
508 }
509
510 status = DLM_IVLOCKID;
511 res = dlm_lookup_lockres(dlm, name, namelen);
512 if (!res) {
513 dlm_error(status);
514 goto leave;
515 }
516
517 spin_lock(&res->spinlock);
518 status = __dlm_lockres_state_to_status(res);
519 spin_unlock(&res->spinlock);
520
521 if (status != DLM_NORMAL) {
522 mlog(0, "lockres recovering/migrating/in-progress\n");
523 goto leave;
524 }
525
526 dlm_lock_attach_lockres(newlock, res);
527
528 status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
529leave:
530 if (status != DLM_NORMAL)
531 if (newlock)
532 dlm_lock_put(newlock);
533
534 if (res)
535 dlm_lockres_put(res);
536
537 dlm_put(dlm);
538
539 return status;
540}
541
542
543/* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
544static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
545{
546 u64 tmpnode = node_num;
547
548 /* shift single byte of node num into top 8 bits */
549 tmpnode <<= 56;
550
551 spin_lock(&dlm_cookie_lock);
552 *cookie = (dlm_next_cookie | tmpnode);
553 if (++dlm_next_cookie & 0xff00000000000000ull) {
554 mlog(0, "This node's cookie will now wrap!\n");
555 dlm_next_cookie = 1;
556 }
557 spin_unlock(&dlm_cookie_lock);
558}
559
560enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
561 struct dlm_lockstatus *lksb, int flags,
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700562 const char *name, int namelen, dlm_astlockfunc_t *ast,
563 void *data, dlm_bastlockfunc_t *bast)
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800564{
565 enum dlm_status status;
566 struct dlm_lock_resource *res = NULL;
567 struct dlm_lock *lock = NULL;
568 int convert = 0, recovery = 0;
569
570 /* yes this function is a mess.
571 * TODO: clean this up. lots of common code in the
572 * lock and convert paths, especially in the retry blocks */
573 if (!lksb) {
574 dlm_error(DLM_BADARGS);
575 return DLM_BADARGS;
576 }
577
578 status = DLM_BADPARAM;
579 if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
580 dlm_error(status);
581 goto error;
582 }
583
584 if (flags & ~LKM_VALID_FLAGS) {
585 dlm_error(status);
586 goto error;
587 }
588
589 convert = (flags & LKM_CONVERT);
590 recovery = (flags & LKM_RECOVERY);
591
592 if (recovery &&
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700593 (!dlm_is_recovery_lock(name, namelen) || convert) ) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800594 dlm_error(status);
595 goto error;
596 }
597 if (convert && (flags & LKM_LOCAL)) {
598 mlog(ML_ERROR, "strange LOCAL convert request!\n");
599 goto error;
600 }
601
602 if (convert) {
603 /* CONVERT request */
604
605 /* if converting, must pass in a valid dlm_lock */
606 lock = lksb->lockid;
607 if (!lock) {
608 mlog(ML_ERROR, "NULL lock pointer in convert "
609 "request\n");
610 goto error;
611 }
612
613 res = lock->lockres;
614 if (!res) {
615 mlog(ML_ERROR, "NULL lockres pointer in convert "
616 "request\n");
617 goto error;
618 }
619 dlm_lockres_get(res);
620
621 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
622 * static after the original lock call. convert requests will
623 * ensure that everything is the same, or return DLM_BADARGS.
624 * this means that DLM_DENIED_NOASTS will never be returned.
625 */
626 if (lock->lksb != lksb || lock->ast != ast ||
627 lock->bast != bast || lock->astdata != data) {
628 status = DLM_BADARGS;
629 mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
630 "astdata=%p\n", lksb, ast, bast, data);
631 mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
632 "astdata=%p\n", lock->lksb, lock->ast,
633 lock->bast, lock->astdata);
634 goto error;
635 }
636retry_convert:
637 dlm_wait_for_recovery(dlm);
638
639 if (res->owner == dlm->node_num)
640 status = dlmconvert_master(dlm, res, lock, flags, mode);
641 else
642 status = dlmconvert_remote(dlm, res, lock, flags, mode);
643 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
644 status == DLM_FORWARD) {
645 /* for now, see how this works without sleeping
646 * and just retry right away. I suspect the reco
647 * or migration will complete fast enough that
648 * no waiting will be necessary */
649 mlog(0, "retrying convert with migration/recovery/"
650 "in-progress\n");
651 msleep(100);
652 goto retry_convert;
653 }
654 } else {
655 u64 tmpcookie;
656
657 /* LOCK request */
658 status = DLM_BADARGS;
659 if (!name) {
660 dlm_error(status);
661 goto error;
662 }
663
664 status = DLM_IVBUFLEN;
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700665 if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800666 dlm_error(status);
667 goto error;
668 }
669
670 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
671 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
672 if (!lock) {
673 dlm_error(status);
674 goto error;
675 }
676
677 if (!recovery)
678 dlm_wait_for_recovery(dlm);
679
680 /* find or create the lock resource */
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700681 res = dlm_get_lock_resource(dlm, name, namelen, flags);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800682 if (!res) {
683 status = DLM_IVLOCKID;
684 dlm_error(status);
685 goto error;
686 }
687
688 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
689 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
690
691 dlm_lock_attach_lockres(lock, res);
692 lock->ast = ast;
693 lock->bast = bast;
694 lock->astdata = data;
695
696retry_lock:
697 if (flags & LKM_VALBLK) {
698 mlog(0, "LKM_VALBLK passed by caller\n");
699
700 /* LVB requests for non PR, PW or EX locks are
701 * ignored. */
702 if (mode < LKM_PRMODE)
703 flags &= ~LKM_VALBLK;
704 else {
705 flags |= LKM_GET_LVB;
706 lock->lksb->flags |= DLM_LKSB_GET_LVB;
707 }
708 }
709
710 if (res->owner == dlm->node_num)
711 status = dlmlock_master(dlm, res, lock, flags);
712 else
713 status = dlmlock_remote(dlm, res, lock, flags);
714
715 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
716 status == DLM_FORWARD) {
717 mlog(0, "retrying lock with migration/"
718 "recovery/in progress\n");
719 msleep(100);
Kurt Hackel44465a72006-01-18 17:05:38 -0800720 /* no waiting for dlm_reco_thread */
721 if (recovery) {
Kurt Hackelc8df4122006-05-01 13:47:50 -0700722 if (status != DLM_RECOVERING)
723 goto retry_lock;
724
725 mlog(0, "%s: got RECOVERING "
726 "for $RECOVERY lock, master "
727 "was %u\n", dlm->name,
728 res->owner);
729 /* wait to see the node go down, then
730 * drop down and allow the lockres to
731 * get cleaned up. need to remaster. */
732 dlm_wait_for_node_death(dlm, res->owner,
733 DLM_NODE_DEATH_WAIT_MAX);
Kurt Hackel44465a72006-01-18 17:05:38 -0800734 } else {
735 dlm_wait_for_recovery(dlm);
Kurt Hackelc8df4122006-05-01 13:47:50 -0700736 goto retry_lock;
Kurt Hackel44465a72006-01-18 17:05:38 -0800737 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800738 }
739
740 if (status != DLM_NORMAL) {
741 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
742 if (status != DLM_NOTQUEUED)
743 dlm_error(status);
744 goto error;
745 }
746 }
747
748error:
749 if (status != DLM_NORMAL) {
750 if (lock && !convert)
751 dlm_lock_put(lock);
752 // this is kind of unnecessary
753 lksb->status = status;
754 }
755
756 /* put lockres ref from the convert path
757 * or from dlm_get_lock_resource */
758 if (res)
759 dlm_lockres_put(res);
760
761 return status;
762}
763EXPORT_SYMBOL_GPL(dlmlock);