blob: 671d4ff222cc083c15aa63ed33048c899da2d26b [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>
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>
42
43
44#include "cluster/heartbeat.h"
45#include "cluster/nodemanager.h"
46#include "cluster/tcp.h"
47
48#include "dlmapi.h"
49#include "dlmcommon.h"
50
51#include "dlmconvert.h"
52
53#define MLOG_MASK_PREFIX ML_DLM
54#include "cluster/masklog.h"
55
56static spinlock_t dlm_cookie_lock = SPIN_LOCK_UNLOCKED;
57static u64 dlm_next_cookie = 1;
58
59static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
60 struct dlm_lock_resource *res,
61 struct dlm_lock *lock, int flags);
62static void dlm_init_lock(struct dlm_lock *newlock, int type,
63 u8 node, u64 cookie);
64static void dlm_lock_release(struct kref *kref);
65static void dlm_lock_detach_lockres(struct dlm_lock *lock);
66
67/* Tell us whether we can grant a new lock request.
68 * locking:
69 * caller needs: res->spinlock
70 * taken: none
71 * held on exit: none
72 * returns: 1 if the lock can be granted, 0 otherwise.
73 */
74static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
75 struct dlm_lock *lock)
76{
77 struct list_head *iter;
78 struct dlm_lock *tmplock;
79
80 list_for_each(iter, &res->granted) {
81 tmplock = list_entry(iter, struct dlm_lock, list);
82
83 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
84 return 0;
85 }
86
87 list_for_each(iter, &res->converting) {
88 tmplock = list_entry(iter, struct dlm_lock, list);
89
90 if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
91 return 0;
92 }
93
94 return 1;
95}
96
97/* performs lock creation at the lockres master site
98 * locking:
99 * caller needs: none
100 * taken: takes and drops res->spinlock
101 * held on exit: none
102 * returns: DLM_NORMAL, DLM_NOTQUEUED
103 */
104static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
105 struct dlm_lock_resource *res,
106 struct dlm_lock *lock, int flags)
107{
108 int call_ast = 0, kick_thread = 0;
109 enum dlm_status status = DLM_NORMAL;
110
111 mlog_entry("type=%d\n", lock->ml.type);
112
113 spin_lock(&res->spinlock);
114 /* if called from dlm_create_lock_handler, need to
115 * ensure it will not sleep in dlm_wait_on_lockres */
116 status = __dlm_lockres_state_to_status(res);
117 if (status != DLM_NORMAL &&
118 lock->ml.node != dlm->node_num) {
119 /* erf. state changed after lock was dropped. */
120 spin_unlock(&res->spinlock);
121 dlm_error(status);
122 return status;
123 }
124 __dlm_wait_on_lockres(res);
125 __dlm_lockres_reserve_ast(res);
126
127 if (dlm_can_grant_new_lock(res, lock)) {
128 mlog(0, "I can grant this lock right away\n");
129 /* got it right away */
130 lock->lksb->status = DLM_NORMAL;
131 status = DLM_NORMAL;
132 dlm_lock_get(lock);
133 list_add_tail(&lock->list, &res->granted);
134
135 /* for the recovery lock, we can't allow the ast
136 * to be queued since the dlmthread is already
137 * frozen. but the recovery lock is always locked
138 * with LKM_NOQUEUE so we do not need the ast in
139 * this special case */
140 if (!dlm_is_recovery_lock(res->lockname.name,
141 res->lockname.len)) {
142 kick_thread = 1;
143 call_ast = 1;
144 }
145 } else {
146 /* for NOQUEUE request, unless we get the
147 * lock right away, return DLM_NOTQUEUED */
148 if (flags & LKM_NOQUEUE)
149 status = DLM_NOTQUEUED;
150 else {
151 dlm_lock_get(lock);
152 list_add_tail(&lock->list, &res->blocked);
153 kick_thread = 1;
154 }
155 }
156
157 spin_unlock(&res->spinlock);
158 wake_up(&res->wq);
159
160 /* either queue the ast or release it */
161 if (call_ast)
162 dlm_queue_ast(dlm, lock);
163 else
164 dlm_lockres_release_ast(dlm, res);
165
166 dlm_lockres_calc_usage(dlm, res);
167 if (kick_thread)
168 dlm_kick_thread(dlm, res);
169
170 return status;
171}
172
173void dlm_revert_pending_lock(struct dlm_lock_resource *res,
174 struct dlm_lock *lock)
175{
176 /* remove from local queue if it failed */
177 list_del_init(&lock->list);
178 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
179}
180
181
182/*
183 * locking:
184 * caller needs: none
185 * taken: takes and drops res->spinlock
186 * held on exit: none
187 * returns: DLM_DENIED, DLM_RECOVERING, or net status
188 */
189static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
190 struct dlm_lock_resource *res,
191 struct dlm_lock *lock, int flags)
192{
193 enum dlm_status status = DLM_DENIED;
194
195 mlog_entry("type=%d\n", lock->ml.type);
196 mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
197 res->lockname.name, flags);
198
199 spin_lock(&res->spinlock);
200
201 /* will exit this call with spinlock held */
202 __dlm_wait_on_lockres(res);
203 res->state |= DLM_LOCK_RES_IN_PROGRESS;
204
205 /* add lock to local (secondary) queue */
206 dlm_lock_get(lock);
207 list_add_tail(&lock->list, &res->blocked);
208 lock->lock_pending = 1;
209 spin_unlock(&res->spinlock);
210
211 /* spec seems to say that you will get DLM_NORMAL when the lock
212 * has been queued, meaning we need to wait for a reply here. */
213 status = dlm_send_remote_lock_request(dlm, res, lock, flags);
214
215 spin_lock(&res->spinlock);
216 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
217 lock->lock_pending = 0;
218 if (status != DLM_NORMAL) {
219 if (status != DLM_NOTQUEUED)
220 dlm_error(status);
221 dlm_revert_pending_lock(res, lock);
222 dlm_lock_put(lock);
Kurt Hackel558c70c2006-01-18 17:07:47 -0800223 } else if (dlm_is_recovery_lock(res->lockname.name,
224 res->lockname.len)) {
225 /* special case for the $RECOVERY lock.
226 * there will never be an AST delivered to put
227 * this lock on the proper secondary queue
228 * (granted), so do it manually. */
229 mlog(0, "%s: $RECOVERY lock for this node (%u) is "
230 "mastered by %u; got lock, manually granting (no ast)\n",
231 dlm->name, dlm->node_num, res->owner);
232 list_del_init(&lock->list);
233 list_add_tail(&lock->list, &res->granted);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800234 }
235 spin_unlock(&res->spinlock);
236
237 dlm_lockres_calc_usage(dlm, res);
238
239 wake_up(&res->wq);
240 return status;
241}
242
243
244/* for remote lock creation.
245 * locking:
246 * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
247 * taken: none
248 * held on exit: none
249 * returns: DLM_NOLOCKMGR, or net status
250 */
251static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
252 struct dlm_lock_resource *res,
253 struct dlm_lock *lock, int flags)
254{
255 struct dlm_create_lock create;
256 int tmpret, status = 0;
257 enum dlm_status ret;
258
259 mlog_entry_void();
260
261 memset(&create, 0, sizeof(create));
262 create.node_idx = dlm->node_num;
263 create.requested_type = lock->ml.type;
264 create.cookie = lock->ml.cookie;
265 create.namelen = res->lockname.len;
266 create.flags = cpu_to_be32(flags);
267 memcpy(create.name, res->lockname.name, create.namelen);
268
269 tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
270 sizeof(create), res->owner, &status);
271 if (tmpret >= 0) {
272 // successfully sent and received
273 ret = status; // this is already a dlm_status
274 } else {
275 mlog_errno(tmpret);
276 if (dlm_is_host_down(tmpret)) {
277 ret = DLM_RECOVERING;
278 mlog(0, "node %u died so returning DLM_RECOVERING "
279 "from lock message!\n", res->owner);
280 } else {
281 ret = dlm_err_to_dlm_status(tmpret);
282 }
283 }
284
285 return ret;
286}
287
288void dlm_lock_get(struct dlm_lock *lock)
289{
290 kref_get(&lock->lock_refs);
291}
292
293void dlm_lock_put(struct dlm_lock *lock)
294{
295 kref_put(&lock->lock_refs, dlm_lock_release);
296}
297
298static void dlm_lock_release(struct kref *kref)
299{
300 struct dlm_lock *lock;
301
302 lock = container_of(kref, struct dlm_lock, lock_refs);
303
304 BUG_ON(!list_empty(&lock->list));
305 BUG_ON(!list_empty(&lock->ast_list));
306 BUG_ON(!list_empty(&lock->bast_list));
307 BUG_ON(lock->ast_pending);
308 BUG_ON(lock->bast_pending);
309
310 dlm_lock_detach_lockres(lock);
311
312 if (lock->lksb_kernel_allocated) {
313 mlog(0, "freeing kernel-allocated lksb\n");
314 kfree(lock->lksb);
315 }
316 kfree(lock);
317}
318
319/* associate a lock with it's lockres, getting a ref on the lockres */
320void dlm_lock_attach_lockres(struct dlm_lock *lock,
321 struct dlm_lock_resource *res)
322{
323 dlm_lockres_get(res);
324 lock->lockres = res;
325}
326
327/* drop ref on lockres, if there is still one associated with lock */
328static void dlm_lock_detach_lockres(struct dlm_lock *lock)
329{
330 struct dlm_lock_resource *res;
331
332 res = lock->lockres;
333 if (res) {
334 lock->lockres = NULL;
335 mlog(0, "removing lock's lockres reference\n");
336 dlm_lockres_put(res);
337 }
338}
339
340static void dlm_init_lock(struct dlm_lock *newlock, int type,
341 u8 node, u64 cookie)
342{
343 INIT_LIST_HEAD(&newlock->list);
344 INIT_LIST_HEAD(&newlock->ast_list);
345 INIT_LIST_HEAD(&newlock->bast_list);
346 spin_lock_init(&newlock->spinlock);
347 newlock->ml.type = type;
348 newlock->ml.convert_type = LKM_IVMODE;
349 newlock->ml.highest_blocked = LKM_IVMODE;
350 newlock->ml.node = node;
351 newlock->ml.pad1 = 0;
352 newlock->ml.list = 0;
353 newlock->ml.flags = 0;
354 newlock->ast = NULL;
355 newlock->bast = NULL;
356 newlock->astdata = NULL;
357 newlock->ml.cookie = cpu_to_be64(cookie);
358 newlock->ast_pending = 0;
359 newlock->bast_pending = 0;
360 newlock->convert_pending = 0;
361 newlock->lock_pending = 0;
362 newlock->unlock_pending = 0;
363 newlock->cancel_pending = 0;
364 newlock->lksb_kernel_allocated = 0;
365
366 kref_init(&newlock->lock_refs);
367}
368
369struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
370 struct dlm_lockstatus *lksb)
371{
372 struct dlm_lock *lock;
373 int kernel_allocated = 0;
374
375 lock = kcalloc(1, sizeof(*lock), GFP_KERNEL);
376 if (!lock)
377 return NULL;
378
379 if (!lksb) {
380 /* zero memory only if kernel-allocated */
381 lksb = kcalloc(1, sizeof(*lksb), GFP_KERNEL);
382 if (!lksb) {
383 kfree(lock);
384 return NULL;
385 }
386 kernel_allocated = 1;
387 }
388
389 dlm_init_lock(lock, type, node, cookie);
390 if (kernel_allocated)
391 lock->lksb_kernel_allocated = 1;
392 lock->lksb = lksb;
393 lksb->lockid = lock;
394 return lock;
395}
396
397/* handler for lock creation net message
398 * locking:
399 * caller needs: none
400 * taken: takes and drops res->spinlock
401 * held on exit: none
402 * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
403 */
404int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data)
405{
406 struct dlm_ctxt *dlm = data;
407 struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
408 struct dlm_lock_resource *res = NULL;
409 struct dlm_lock *newlock = NULL;
410 struct dlm_lockstatus *lksb = NULL;
411 enum dlm_status status = DLM_NORMAL;
412 char *name;
413 unsigned int namelen;
414
415 BUG_ON(!dlm);
416
417 mlog_entry_void();
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 name = create->name;
426 namelen = create->namelen;
427
428 status = DLM_IVBUFLEN;
429 if (namelen > DLM_LOCKID_NAME_MAX) {
430 dlm_error(status);
431 goto leave;
432 }
433
434 status = DLM_SYSERR;
435 newlock = dlm_new_lock(create->requested_type,
436 create->node_idx,
437 be64_to_cpu(create->cookie), NULL);
438 if (!newlock) {
439 dlm_error(status);
440 goto leave;
441 }
442
443 lksb = newlock->lksb;
444
445 if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
446 lksb->flags |= DLM_LKSB_GET_LVB;
447 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
448 }
449
450 status = DLM_IVLOCKID;
451 res = dlm_lookup_lockres(dlm, name, namelen);
452 if (!res) {
453 dlm_error(status);
454 goto leave;
455 }
456
457 spin_lock(&res->spinlock);
458 status = __dlm_lockres_state_to_status(res);
459 spin_unlock(&res->spinlock);
460
461 if (status != DLM_NORMAL) {
462 mlog(0, "lockres recovering/migrating/in-progress\n");
463 goto leave;
464 }
465
466 dlm_lock_attach_lockres(newlock, res);
467
468 status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
469leave:
470 if (status != DLM_NORMAL)
471 if (newlock)
472 dlm_lock_put(newlock);
473
474 if (res)
475 dlm_lockres_put(res);
476
477 dlm_put(dlm);
478
479 return status;
480}
481
482
483/* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
484static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
485{
486 u64 tmpnode = node_num;
487
488 /* shift single byte of node num into top 8 bits */
489 tmpnode <<= 56;
490
491 spin_lock(&dlm_cookie_lock);
492 *cookie = (dlm_next_cookie | tmpnode);
493 if (++dlm_next_cookie & 0xff00000000000000ull) {
494 mlog(0, "This node's cookie will now wrap!\n");
495 dlm_next_cookie = 1;
496 }
497 spin_unlock(&dlm_cookie_lock);
498}
499
500enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
501 struct dlm_lockstatus *lksb, int flags,
502 const char *name, dlm_astlockfunc_t *ast, void *data,
503 dlm_bastlockfunc_t *bast)
504{
505 enum dlm_status status;
506 struct dlm_lock_resource *res = NULL;
507 struct dlm_lock *lock = NULL;
508 int convert = 0, recovery = 0;
509
510 /* yes this function is a mess.
511 * TODO: clean this up. lots of common code in the
512 * lock and convert paths, especially in the retry blocks */
513 if (!lksb) {
514 dlm_error(DLM_BADARGS);
515 return DLM_BADARGS;
516 }
517
518 status = DLM_BADPARAM;
519 if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
520 dlm_error(status);
521 goto error;
522 }
523
524 if (flags & ~LKM_VALID_FLAGS) {
525 dlm_error(status);
526 goto error;
527 }
528
529 convert = (flags & LKM_CONVERT);
530 recovery = (flags & LKM_RECOVERY);
531
532 if (recovery &&
533 (!dlm_is_recovery_lock(name, strlen(name)) || convert) ) {
534 dlm_error(status);
535 goto error;
536 }
537 if (convert && (flags & LKM_LOCAL)) {
538 mlog(ML_ERROR, "strange LOCAL convert request!\n");
539 goto error;
540 }
541
542 if (convert) {
543 /* CONVERT request */
544
545 /* if converting, must pass in a valid dlm_lock */
546 lock = lksb->lockid;
547 if (!lock) {
548 mlog(ML_ERROR, "NULL lock pointer in convert "
549 "request\n");
550 goto error;
551 }
552
553 res = lock->lockres;
554 if (!res) {
555 mlog(ML_ERROR, "NULL lockres pointer in convert "
556 "request\n");
557 goto error;
558 }
559 dlm_lockres_get(res);
560
561 /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
562 * static after the original lock call. convert requests will
563 * ensure that everything is the same, or return DLM_BADARGS.
564 * this means that DLM_DENIED_NOASTS will never be returned.
565 */
566 if (lock->lksb != lksb || lock->ast != ast ||
567 lock->bast != bast || lock->astdata != data) {
568 status = DLM_BADARGS;
569 mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
570 "astdata=%p\n", lksb, ast, bast, data);
571 mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
572 "astdata=%p\n", lock->lksb, lock->ast,
573 lock->bast, lock->astdata);
574 goto error;
575 }
576retry_convert:
577 dlm_wait_for_recovery(dlm);
578
579 if (res->owner == dlm->node_num)
580 status = dlmconvert_master(dlm, res, lock, flags, mode);
581 else
582 status = dlmconvert_remote(dlm, res, lock, flags, mode);
583 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
584 status == DLM_FORWARD) {
585 /* for now, see how this works without sleeping
586 * and just retry right away. I suspect the reco
587 * or migration will complete fast enough that
588 * no waiting will be necessary */
589 mlog(0, "retrying convert with migration/recovery/"
590 "in-progress\n");
591 msleep(100);
592 goto retry_convert;
593 }
594 } else {
595 u64 tmpcookie;
596
597 /* LOCK request */
598 status = DLM_BADARGS;
599 if (!name) {
600 dlm_error(status);
601 goto error;
602 }
603
604 status = DLM_IVBUFLEN;
605 if (strlen(name) > DLM_LOCKID_NAME_MAX || strlen(name) < 1) {
606 dlm_error(status);
607 goto error;
608 }
609
610 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
611 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
612 if (!lock) {
613 dlm_error(status);
614 goto error;
615 }
616
617 if (!recovery)
618 dlm_wait_for_recovery(dlm);
619
620 /* find or create the lock resource */
621 res = dlm_get_lock_resource(dlm, name, flags);
622 if (!res) {
623 status = DLM_IVLOCKID;
624 dlm_error(status);
625 goto error;
626 }
627
628 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
629 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
630
631 dlm_lock_attach_lockres(lock, res);
632 lock->ast = ast;
633 lock->bast = bast;
634 lock->astdata = data;
635
636retry_lock:
637 if (flags & LKM_VALBLK) {
638 mlog(0, "LKM_VALBLK passed by caller\n");
639
640 /* LVB requests for non PR, PW or EX locks are
641 * ignored. */
642 if (mode < LKM_PRMODE)
643 flags &= ~LKM_VALBLK;
644 else {
645 flags |= LKM_GET_LVB;
646 lock->lksb->flags |= DLM_LKSB_GET_LVB;
647 }
648 }
649
650 if (res->owner == dlm->node_num)
651 status = dlmlock_master(dlm, res, lock, flags);
652 else
653 status = dlmlock_remote(dlm, res, lock, flags);
654
655 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
656 status == DLM_FORWARD) {
657 mlog(0, "retrying lock with migration/"
658 "recovery/in progress\n");
659 msleep(100);
Kurt Hackel44465a72006-01-18 17:05:38 -0800660 /* no waiting for dlm_reco_thread */
661 if (recovery) {
662 if (status == DLM_RECOVERING) {
663 mlog(0, "%s: got RECOVERING "
664 "for $REOCVERY lock, master "
665 "was %u\n", dlm->name,
666 res->owner);
667 dlm_wait_for_node_death(dlm, res->owner,
668 DLM_NODE_DEATH_WAIT_MAX);
669 }
670 } else {
671 dlm_wait_for_recovery(dlm);
672 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800673 goto retry_lock;
674 }
675
676 if (status != DLM_NORMAL) {
677 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
678 if (status != DLM_NOTQUEUED)
679 dlm_error(status);
680 goto error;
681 }
682 }
683
684error:
685 if (status != DLM_NORMAL) {
686 if (lock && !convert)
687 dlm_lock_put(lock);
688 // this is kind of unnecessary
689 lksb->status = status;
690 }
691
692 /* put lockres ref from the convert path
693 * or from dlm_get_lock_resource */
694 if (res)
695 dlm_lockres_put(res);
696
697 return status;
698}
699EXPORT_SYMBOL_GPL(dlmlock);