1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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/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
55static struct kmem_cache *dlm_lock_cache = NULL;
56
57static DEFINE_SPINLOCK(dlm_cookie_lock);
58static 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
68int 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
84
85
86
87
88
89
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;
109 if (!dlm_lock_compatible(tmplock->ml.convert_type,
110 lock->ml.type))
111 return 0;
112 }
113
114 return 1;
115}
116
117
118
119
120
121
122
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
131 mlog_entry("type=%d\n", lock->ml.type);
132
133 spin_lock(&res->spinlock);
134
135
136 status = __dlm_lockres_state_to_status(res);
137 if (status != DLM_NORMAL &&
138 lock->ml.node != dlm->node_num) {
139
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
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
156
157
158
159
160 if (!dlm_is_recovery_lock(res->lockname.name,
161 res->lockname.len)) {
162 kick_thread = 1;
163 call_ast = 1;
164 } else {
165 mlog(0, "%s: returning DLM_NORMAL to "
166 "node %u for reco lock\n", dlm->name,
167 lock->ml.node);
168 }
169 } else {
170
171
172 if (flags & LKM_NOQUEUE) {
173 status = DLM_NOTQUEUED;
174 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 {
181 dlm_lock_get(lock);
182 list_add_tail(&lock->list, &res->blocked);
183 kick_thread = 1;
184 }
185 }
186
187
188 if (lock->ml.node == dlm->node_num)
189 dlm_lockres_drop_inflight_ref(dlm, res);
190
191 spin_unlock(&res->spinlock);
192 wake_up(&res->wq);
193
194
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
211 list_del_init(&lock->list);
212 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
213}
214
215
216
217
218
219
220
221
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;
228 int lockres_changed = 1;
229
230 mlog_entry("type=%d\n", lock->ml.type);
231 mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
232 res->lockname.name, flags);
233
234 spin_lock(&res->spinlock);
235
236
237 __dlm_wait_on_lockres(res);
238 res->state |= DLM_LOCK_RES_IN_PROGRESS;
239
240
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
247
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) {
254 if (status == DLM_RECOVERING &&
255 dlm_is_recovery_lock(res->lockname.name,
256 res->lockname.len)) {
257
258
259
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) {
264
265
266
267
268
269
270 lockres_changed = 0;
271 dlm_error(status);
272 }
273 dlm_revert_pending_lock(res, lock);
274 dlm_lock_put(lock);
275 } else if (dlm_is_recovery_lock(res->lockname.name,
276 res->lockname.len)) {
277
278
279
280
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);
284 list_move_tail(&lock->list, &res->granted);
285 }
286 spin_unlock(&res->spinlock);
287
288 if (lockres_changed)
289 dlm_lockres_calc_usage(dlm, res);
290
291 wake_up(&res->wq);
292 return status;
293}
294
295
296
297
298
299
300
301
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
311 mlog_entry_void();
312
313 memset(&create, 0, sizeof(create));
314 create.node_idx = dlm->node_num;
315 create.requested_type = lock->ml.type;
316 create.cookie = lock->ml.cookie;
317 create.namelen = res->lockname.len;
318 create.flags = cpu_to_be32(flags);
319 memcpy(create.name, res->lockname.name, create.namelen);
320
321 tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
322 sizeof(create), res->owner, &status);
323 if (tmpret >= 0) {
324
325 ret = status;
326 if (ret == DLM_REJECTED) {
327 mlog(ML_ERROR, "%s:%.*s: BUG. this is a stale lockres "
328 "no longer owned by %u. that node is coming back "
329 "up currently.\n", dlm->name, create.namelen,
330 create.name, res->owner);
331 dlm_print_one_lock_resource(res);
332 BUG();
333 }
334 } else {
335 mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
336 "node %u\n", tmpret, DLM_CREATE_LOCK_MSG, dlm->key,
337 res->owner);
338 if (dlm_is_host_down(tmpret)) {
339 ret = DLM_RECOVERING;
340 mlog(0, "node %u died so returning DLM_RECOVERING "
341 "from lock message!\n", res->owner);
342 } else {
343 ret = dlm_err_to_dlm_status(tmpret);
344 }
345 }
346
347 return ret;
348}
349
350void dlm_lock_get(struct dlm_lock *lock)
351{
352 kref_get(&lock->lock_refs);
353}
354
355void dlm_lock_put(struct dlm_lock *lock)
356{
357 kref_put(&lock->lock_refs, dlm_lock_release);
358}
359
360static void dlm_lock_release(struct kref *kref)
361{
362 struct dlm_lock *lock;
363
364 lock = container_of(kref, struct dlm_lock, lock_refs);
365
366 BUG_ON(!list_empty(&lock->list));
367 BUG_ON(!list_empty(&lock->ast_list));
368 BUG_ON(!list_empty(&lock->bast_list));
369 BUG_ON(lock->ast_pending);
370 BUG_ON(lock->bast_pending);
371
372 dlm_lock_detach_lockres(lock);
373
374 if (lock->lksb_kernel_allocated) {
375 mlog(0, "freeing kernel-allocated lksb\n");
376 kfree(lock->lksb);
377 }
378 kmem_cache_free(dlm_lock_cache, lock);
379}
380
381
382void dlm_lock_attach_lockres(struct dlm_lock *lock,
383 struct dlm_lock_resource *res)
384{
385 dlm_lockres_get(res);
386 lock->lockres = res;
387}
388
389
390static void dlm_lock_detach_lockres(struct dlm_lock *lock)
391{
392 struct dlm_lock_resource *res;
393
394 res = lock->lockres;
395 if (res) {
396 lock->lockres = NULL;
397 mlog(0, "removing lock's lockres reference\n");
398 dlm_lockres_put(res);
399 }
400}
401
402static void dlm_init_lock(struct dlm_lock *newlock, int type,
403 u8 node, u64 cookie)
404{
405 INIT_LIST_HEAD(&newlock->list);
406 INIT_LIST_HEAD(&newlock->ast_list);
407 INIT_LIST_HEAD(&newlock->bast_list);
408 spin_lock_init(&newlock->spinlock);
409 newlock->ml.type = type;
410 newlock->ml.convert_type = LKM_IVMODE;
411 newlock->ml.highest_blocked = LKM_IVMODE;
412 newlock->ml.node = node;
413 newlock->ml.pad1 = 0;
414 newlock->ml.list = 0;
415 newlock->ml.flags = 0;
416 newlock->ast = NULL;
417 newlock->bast = NULL;
418 newlock->astdata = NULL;
419 newlock->ml.cookie = cpu_to_be64(cookie);
420 newlock->ast_pending = 0;
421 newlock->bast_pending = 0;
422 newlock->convert_pending = 0;
423 newlock->lock_pending = 0;
424 newlock->unlock_pending = 0;
425 newlock->cancel_pending = 0;
426 newlock->lksb_kernel_allocated = 0;
427
428 kref_init(&newlock->lock_refs);
429}
430
431struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
432 struct dlm_lockstatus *lksb)
433{
434 struct dlm_lock *lock;
435 int kernel_allocated = 0;
436
437 lock = kmem_cache_zalloc(dlm_lock_cache, GFP_NOFS);
438 if (!lock)
439 return NULL;
440
441 if (!lksb) {
442
443 lksb = kzalloc(sizeof(*lksb), GFP_NOFS);
444 if (!lksb) {
445 kfree(lock);
446 return NULL;
447 }
448 kernel_allocated = 1;
449 }
450
451 dlm_init_lock(lock, type, node, cookie);
452 if (kernel_allocated)
453 lock->lksb_kernel_allocated = 1;
454 lock->lksb = lksb;
455 lksb->lockid = lock;
456 return lock;
457}
458
459
460
461
462
463
464
465
466int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
467 void **ret_data)
468{
469 struct dlm_ctxt *dlm = data;
470 struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
471 struct dlm_lock_resource *res = NULL;
472 struct dlm_lock *newlock = NULL;
473 struct dlm_lockstatus *lksb = NULL;
474 enum dlm_status status = DLM_NORMAL;
475 char *name;
476 unsigned int namelen;
477
478 BUG_ON(!dlm);
479
480 mlog_entry_void();
481
482 if (!dlm_grab(dlm))
483 return DLM_REJECTED;
484
485 name = create->name;
486 namelen = create->namelen;
487 status = DLM_REJECTED;
488 if (!dlm_domain_fully_joined(dlm)) {
489 mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
490 "sending a create_lock message for lock %.*s!\n",
491 dlm->name, create->node_idx, namelen, name);
492 dlm_error(status);
493 goto leave;
494 }
495
496 status = DLM_IVBUFLEN;
497 if (namelen > DLM_LOCKID_NAME_MAX) {
498 dlm_error(status);
499 goto leave;
500 }
501
502 status = DLM_SYSERR;
503 newlock = dlm_new_lock(create->requested_type,
504 create->node_idx,
505 be64_to_cpu(create->cookie), NULL);
506 if (!newlock) {
507 dlm_error(status);
508 goto leave;
509 }
510
511 lksb = newlock->lksb;
512
513 if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
514 lksb->flags |= DLM_LKSB_GET_LVB;
515 mlog(0, "set DLM_LKSB_GET_LVB flag\n");
516 }
517
518 status = DLM_IVLOCKID;
519 res = dlm_lookup_lockres(dlm, name, namelen);
520 if (!res) {
521 dlm_error(status);
522 goto leave;
523 }
524
525 spin_lock(&res->spinlock);
526 status = __dlm_lockres_state_to_status(res);
527 spin_unlock(&res->spinlock);
528
529 if (status != DLM_NORMAL) {
530 mlog(0, "lockres recovering/migrating/in-progress\n");
531 goto leave;
532 }
533
534 dlm_lock_attach_lockres(newlock, res);
535
536 status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
537leave:
538 if (status != DLM_NORMAL)
539 if (newlock)
540 dlm_lock_put(newlock);
541
542 if (res)
543 dlm_lockres_put(res);
544
545 dlm_put(dlm);
546
547 return status;
548}
549
550
551
552static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
553{
554 u64 tmpnode = node_num;
555
556
557 tmpnode <<= 56;
558
559 spin_lock(&dlm_cookie_lock);
560 *cookie = (dlm_next_cookie | tmpnode);
561 if (++dlm_next_cookie & 0xff00000000000000ull) {
562 mlog(0, "This node's cookie will now wrap!\n");
563 dlm_next_cookie = 1;
564 }
565 spin_unlock(&dlm_cookie_lock);
566}
567
568enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
569 struct dlm_lockstatus *lksb, int flags,
570 const char *name, int namelen, dlm_astlockfunc_t *ast,
571 void *data, dlm_bastlockfunc_t *bast)
572{
573 enum dlm_status status;
574 struct dlm_lock_resource *res = NULL;
575 struct dlm_lock *lock = NULL;
576 int convert = 0, recovery = 0;
577
578
579
580
581 if (!lksb) {
582 dlm_error(DLM_BADARGS);
583 return DLM_BADARGS;
584 }
585
586 status = DLM_BADPARAM;
587 if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
588 dlm_error(status);
589 goto error;
590 }
591
592 if (flags & ~LKM_VALID_FLAGS) {
593 dlm_error(status);
594 goto error;
595 }
596
597 convert = (flags & LKM_CONVERT);
598 recovery = (flags & LKM_RECOVERY);
599
600 if (recovery &&
601 (!dlm_is_recovery_lock(name, namelen) || convert) ) {
602 dlm_error(status);
603 goto error;
604 }
605 if (convert && (flags & LKM_LOCAL)) {
606 mlog(ML_ERROR, "strange LOCAL convert request!\n");
607 goto error;
608 }
609
610 if (convert) {
611
612
613
614 lock = lksb->lockid;
615 if (!lock) {
616 mlog(ML_ERROR, "NULL lock pointer in convert "
617 "request\n");
618 goto error;
619 }
620
621 res = lock->lockres;
622 if (!res) {
623 mlog(ML_ERROR, "NULL lockres pointer in convert "
624 "request\n");
625 goto error;
626 }
627 dlm_lockres_get(res);
628
629
630
631
632
633
634 if (lock->lksb != lksb || lock->ast != ast ||
635 lock->bast != bast || lock->astdata != data) {
636 status = DLM_BADARGS;
637 mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
638 "astdata=%p\n", lksb, ast, bast, data);
639 mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
640 "astdata=%p\n", lock->lksb, lock->ast,
641 lock->bast, lock->astdata);
642 goto error;
643 }
644retry_convert:
645 dlm_wait_for_recovery(dlm);
646
647 if (res->owner == dlm->node_num)
648 status = dlmconvert_master(dlm, res, lock, flags, mode);
649 else
650 status = dlmconvert_remote(dlm, res, lock, flags, mode);
651 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
652 status == DLM_FORWARD) {
653
654
655
656
657 mlog(0, "retrying convert with migration/recovery/"
658 "in-progress\n");
659 msleep(100);
660 goto retry_convert;
661 }
662 } else {
663 u64 tmpcookie;
664
665
666 status = DLM_BADARGS;
667 if (!name) {
668 dlm_error(status);
669 goto error;
670 }
671
672 status = DLM_IVBUFLEN;
673 if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) {
674 dlm_error(status);
675 goto error;
676 }
677
678 dlm_get_next_cookie(dlm->node_num, &tmpcookie);
679 lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
680 if (!lock) {
681 dlm_error(status);
682 goto error;
683 }
684
685 if (!recovery)
686 dlm_wait_for_recovery(dlm);
687
688
689 res = dlm_get_lock_resource(dlm, name, namelen, flags);
690 if (!res) {
691 status = DLM_IVLOCKID;
692 dlm_error(status);
693 goto error;
694 }
695
696 mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
697 mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
698
699 dlm_lock_attach_lockres(lock, res);
700 lock->ast = ast;
701 lock->bast = bast;
702 lock->astdata = data;
703
704retry_lock:
705 if (flags & LKM_VALBLK) {
706 mlog(0, "LKM_VALBLK passed by caller\n");
707
708
709
710 if (mode < LKM_PRMODE)
711 flags &= ~LKM_VALBLK;
712 else {
713 flags |= LKM_GET_LVB;
714 lock->lksb->flags |= DLM_LKSB_GET_LVB;
715 }
716 }
717
718 if (res->owner == dlm->node_num)
719 status = dlmlock_master(dlm, res, lock, flags);
720 else
721 status = dlmlock_remote(dlm, res, lock, flags);
722
723 if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
724 status == DLM_FORWARD) {
725 mlog(0, "retrying lock with migration/"
726 "recovery/in progress\n");
727 msleep(100);
728
729 if (recovery) {
730 if (status != DLM_RECOVERING)
731 goto retry_lock;
732
733 mlog(0, "%s: got RECOVERING "
734 "for $RECOVERY lock, master "
735 "was %u\n", dlm->name,
736 res->owner);
737
738
739
740 dlm_wait_for_node_death(dlm, res->owner,
741 DLM_NODE_DEATH_WAIT_MAX);
742 } else {
743 dlm_wait_for_recovery(dlm);
744 goto retry_lock;
745 }
746 }
747
748 if (status != DLM_NORMAL) {
749 lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
750 if (status != DLM_NOTQUEUED)
751 dlm_error(status);
752 goto error;
753 }
754 }
755
756error:
757 if (status != DLM_NORMAL) {
758 if (lock && !convert)
759 dlm_lock_put(lock);
760
761 lksb->status = status;
762 }
763
764
765
766 if (res)
767 dlm_lockres_put(res);
768
769 return status;
770}
771EXPORT_SYMBOL_GPL(dlmlock);
772