1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which implements the kernel side of a minimal userspace
7 * interface to our DLM.
9 * Many of the functions here are pared down versions of dlmglue.c
12 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public
16 * License as published by the Free Software Foundation; either
17 * version 2 of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public
25 * License along with this program; if not, write to the
26 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 * Boston, MA 021110-1307, USA.
30 #include <linux/signal.h>
32 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/crc32.h>
38 #include "cluster/nodemanager.h"
39 #include "cluster/heartbeat.h"
40 #include "cluster/tcp.h"
46 #define MLOG_MASK_PREFIX ML_DLMFS
47 #include "cluster/masklog.h"
49 static inline int user_check_wait_flag(struct user_lock_res
*lockres
,
54 spin_lock(&lockres
->l_lock
);
55 ret
= lockres
->l_flags
& flag
;
56 spin_unlock(&lockres
->l_lock
);
61 static inline void user_wait_on_busy_lock(struct user_lock_res
*lockres
)
64 wait_event(lockres
->l_event
,
65 !user_check_wait_flag(lockres
, USER_LOCK_BUSY
));
68 static inline void user_wait_on_blocked_lock(struct user_lock_res
*lockres
)
71 wait_event(lockres
->l_event
,
72 !user_check_wait_flag(lockres
, USER_LOCK_BLOCKED
));
75 /* I heart container_of... */
76 static inline struct dlm_ctxt
*
77 dlm_ctxt_from_user_lockres(struct user_lock_res
*lockres
)
79 struct dlmfs_inode_private
*ip
;
81 ip
= container_of(lockres
,
82 struct dlmfs_inode_private
,
88 user_dlm_inode_from_user_lockres(struct user_lock_res
*lockres
)
90 struct dlmfs_inode_private
*ip
;
92 ip
= container_of(lockres
,
93 struct dlmfs_inode_private
,
95 return &ip
->ip_vfs_inode
;
98 static inline void user_recover_from_dlm_error(struct user_lock_res
*lockres
)
100 spin_lock(&lockres
->l_lock
);
101 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
102 spin_unlock(&lockres
->l_lock
);
105 #define user_log_dlm_error(_func, _stat, _lockres) do { \
106 mlog(ML_ERROR, "Dlm error \"%s\" while calling %s on " \
107 "resource %s: %s\n", dlm_errname(_stat), _func, \
108 _lockres->l_name, dlm_errmsg(_stat)); \
111 /* WARNING: This function lives in a world where the only three lock
112 * levels are EX, PR, and NL. It *will* have to be adjusted when more
113 * lock types are added. */
114 static inline int user_highest_compat_lock_level(int level
)
116 int new_level
= LKM_EXMODE
;
118 if (level
== LKM_EXMODE
)
119 new_level
= LKM_NLMODE
;
120 else if (level
== LKM_PRMODE
)
121 new_level
= LKM_PRMODE
;
125 static void user_ast(void *opaque
)
127 struct user_lock_res
*lockres
= opaque
;
128 struct dlm_lockstatus
*lksb
;
130 mlog(0, "AST fired for lockres %s\n", lockres
->l_name
);
132 spin_lock(&lockres
->l_lock
);
134 lksb
= &(lockres
->l_lksb
);
135 if (lksb
->status
!= DLM_NORMAL
) {
136 mlog(ML_ERROR
, "lksb status value of %u on lockres %s\n",
137 lksb
->status
, lockres
->l_name
);
138 spin_unlock(&lockres
->l_lock
);
142 mlog_bug_on_msg(lockres
->l_requested
== LKM_IVMODE
,
143 "Lockres %s, requested ivmode. flags 0x%x\n",
144 lockres
->l_name
, lockres
->l_flags
);
146 /* we're downconverting. */
147 if (lockres
->l_requested
< lockres
->l_level
) {
148 if (lockres
->l_requested
<=
149 user_highest_compat_lock_level(lockres
->l_blocking
)) {
150 lockres
->l_blocking
= LKM_NLMODE
;
151 lockres
->l_flags
&= ~USER_LOCK_BLOCKED
;
155 lockres
->l_level
= lockres
->l_requested
;
156 lockres
->l_requested
= LKM_IVMODE
;
157 lockres
->l_flags
|= USER_LOCK_ATTACHED
;
158 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
160 spin_unlock(&lockres
->l_lock
);
162 wake_up(&lockres
->l_event
);
165 static inline void user_dlm_grab_inode_ref(struct user_lock_res
*lockres
)
168 inode
= user_dlm_inode_from_user_lockres(lockres
);
173 static void user_dlm_unblock_lock(void *opaque
);
175 static void __user_dlm_queue_lockres(struct user_lock_res
*lockres
)
177 if (!(lockres
->l_flags
& USER_LOCK_QUEUED
)) {
178 user_dlm_grab_inode_ref(lockres
);
180 INIT_WORK(&lockres
->l_work
, user_dlm_unblock_lock
,
183 queue_work(user_dlm_worker
, &lockres
->l_work
);
184 lockres
->l_flags
|= USER_LOCK_QUEUED
;
188 static void __user_dlm_cond_queue_lockres(struct user_lock_res
*lockres
)
192 if (!(lockres
->l_flags
& USER_LOCK_BLOCKED
))
195 switch (lockres
->l_blocking
) {
197 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
201 if (!lockres
->l_ex_holders
)
209 __user_dlm_queue_lockres(lockres
);
212 static void user_bast(void *opaque
, int level
)
214 struct user_lock_res
*lockres
= opaque
;
216 mlog(0, "Blocking AST fired for lockres %s. Blocking level %d\n",
217 lockres
->l_name
, level
);
219 spin_lock(&lockres
->l_lock
);
220 lockres
->l_flags
|= USER_LOCK_BLOCKED
;
221 if (level
> lockres
->l_blocking
)
222 lockres
->l_blocking
= level
;
224 __user_dlm_queue_lockres(lockres
);
225 spin_unlock(&lockres
->l_lock
);
227 wake_up(&lockres
->l_event
);
230 static void user_unlock_ast(void *opaque
, enum dlm_status status
)
232 struct user_lock_res
*lockres
= opaque
;
234 mlog(0, "UNLOCK AST called on lock %s\n", lockres
->l_name
);
236 if (status
!= DLM_NORMAL
&& status
!= DLM_CANCELGRANT
)
237 mlog(ML_ERROR
, "Dlm returns status %d\n", status
);
239 spin_lock(&lockres
->l_lock
);
240 /* The teardown flag gets set early during the unlock process,
241 * so test the cancel flag to make sure that this ast isn't
242 * for a concurrent cancel. */
243 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
244 && !(lockres
->l_flags
& USER_LOCK_IN_CANCEL
)) {
245 lockres
->l_level
= LKM_IVMODE
;
246 } else if (status
== DLM_CANCELGRANT
) {
247 mlog(0, "Lock %s, cancel fails, flags 0x%x\n",
248 lockres
->l_name
, lockres
->l_flags
);
249 /* We tried to cancel a convert request, but it was
250 * already granted. Don't clear the busy flag - the
251 * ast should've done this already. */
252 BUG_ON(!(lockres
->l_flags
& USER_LOCK_IN_CANCEL
));
253 lockres
->l_flags
&= ~USER_LOCK_IN_CANCEL
;
256 BUG_ON(!(lockres
->l_flags
& USER_LOCK_IN_CANCEL
));
257 /* Cancel succeeded, we want to re-queue */
258 mlog(0, "Lock %s, cancel succeeds, flags 0x%x\n",
259 lockres
->l_name
, lockres
->l_flags
);
260 lockres
->l_requested
= LKM_IVMODE
; /* cancel an
263 lockres
->l_flags
&= ~USER_LOCK_IN_CANCEL
;
264 /* we want the unblock thread to look at it again
266 if (lockres
->l_flags
& USER_LOCK_BLOCKED
)
267 __user_dlm_queue_lockres(lockres
);
270 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
272 spin_unlock(&lockres
->l_lock
);
274 wake_up(&lockres
->l_event
);
277 static inline void user_dlm_drop_inode_ref(struct user_lock_res
*lockres
)
280 inode
= user_dlm_inode_from_user_lockres(lockres
);
284 static void user_dlm_unblock_lock(void *opaque
)
286 int new_level
, status
;
287 struct user_lock_res
*lockres
= (struct user_lock_res
*) opaque
;
288 struct dlm_ctxt
*dlm
= dlm_ctxt_from_user_lockres(lockres
);
290 mlog(0, "processing lockres %s\n", lockres
->l_name
);
292 spin_lock(&lockres
->l_lock
);
294 mlog_bug_on_msg(!(lockres
->l_flags
& USER_LOCK_QUEUED
),
295 "Lockres %s, flags 0x%x\n",
296 lockres
->l_name
, lockres
->l_flags
);
298 /* notice that we don't clear USER_LOCK_BLOCKED here. If it's
299 * set, we want user_ast clear it. */
300 lockres
->l_flags
&= ~USER_LOCK_QUEUED
;
302 /* It's valid to get here and no longer be blocked - if we get
303 * several basts in a row, we might be queued by the first
304 * one, the unblock thread might run and clear the queued
305 * flag, and finally we might get another bast which re-queues
306 * us before our ast for the downconvert is called. */
307 if (!(lockres
->l_flags
& USER_LOCK_BLOCKED
)) {
308 mlog(0, "Lockres %s, flags 0x%x: queued but not blocking\n",
309 lockres
->l_name
, lockres
->l_flags
);
310 spin_unlock(&lockres
->l_lock
);
314 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
) {
315 mlog(0, "lock is in teardown so we do nothing\n");
316 spin_unlock(&lockres
->l_lock
);
320 if (lockres
->l_flags
& USER_LOCK_BUSY
) {
321 mlog(0, "Cancel lock %s, flags 0x%x\n",
322 lockres
->l_name
, lockres
->l_flags
);
324 if (lockres
->l_flags
& USER_LOCK_IN_CANCEL
) {
325 spin_unlock(&lockres
->l_lock
);
329 lockres
->l_flags
|= USER_LOCK_IN_CANCEL
;
330 spin_unlock(&lockres
->l_lock
);
332 status
= dlmunlock(dlm
,
337 if (status
!= DLM_NORMAL
)
338 user_log_dlm_error("dlmunlock", status
, lockres
);
342 /* If there are still incompat holders, we can exit safely
343 * without worrying about re-queueing this lock as that will
344 * happen on the last call to user_cluster_unlock. */
345 if ((lockres
->l_blocking
== LKM_EXMODE
)
346 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
)) {
347 spin_unlock(&lockres
->l_lock
);
348 mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n",
349 lockres
->l_ro_holders
, lockres
->l_ex_holders
);
353 if ((lockres
->l_blocking
== LKM_PRMODE
)
354 && lockres
->l_ex_holders
) {
355 spin_unlock(&lockres
->l_lock
);
356 mlog(0, "can't downconvert for pr: ex = %u\n",
357 lockres
->l_ex_holders
);
361 /* yay, we can downconvert now. */
362 new_level
= user_highest_compat_lock_level(lockres
->l_blocking
);
363 lockres
->l_requested
= new_level
;
364 lockres
->l_flags
|= USER_LOCK_BUSY
;
365 mlog(0, "Downconvert lock from %d to %d\n",
366 lockres
->l_level
, new_level
);
367 spin_unlock(&lockres
->l_lock
);
369 /* need lock downconvert request now... */
370 status
= dlmlock(dlm
,
373 LKM_CONVERT
|LKM_VALBLK
,
378 if (status
!= DLM_NORMAL
) {
379 user_log_dlm_error("dlmlock", status
, lockres
);
380 user_recover_from_dlm_error(lockres
);
384 user_dlm_drop_inode_ref(lockres
);
387 static inline void user_dlm_inc_holders(struct user_lock_res
*lockres
,
392 lockres
->l_ex_holders
++;
395 lockres
->l_ro_holders
++;
402 /* predict what lock level we'll be dropping down to on behalf
403 * of another node, and return true if the currently wanted
404 * level will be compatible with it. */
406 user_may_continue_on_blocked_lock(struct user_lock_res
*lockres
,
409 BUG_ON(!(lockres
->l_flags
& USER_LOCK_BLOCKED
));
411 return wanted
<= user_highest_compat_lock_level(lockres
->l_blocking
);
414 int user_dlm_cluster_lock(struct user_lock_res
*lockres
,
418 int status
, local_flags
;
419 struct dlm_ctxt
*dlm
= dlm_ctxt_from_user_lockres(lockres
);
421 if (level
!= LKM_EXMODE
&&
422 level
!= LKM_PRMODE
) {
423 mlog(ML_ERROR
, "lockres %s: invalid request!\n",
429 mlog(0, "lockres %s: asking for %s lock, passed flags = 0x%x\n",
431 (level
== LKM_EXMODE
) ? "LKM_EXMODE" : "LKM_PRMODE",
435 if (signal_pending(current
)) {
436 status
= -ERESTARTSYS
;
440 spin_lock(&lockres
->l_lock
);
442 /* We only compare against the currently granted level
443 * here. If the lock is blocked waiting on a downconvert,
444 * we'll get caught below. */
445 if ((lockres
->l_flags
& USER_LOCK_BUSY
) &&
446 (level
> lockres
->l_level
)) {
447 /* is someone sitting in dlm_lock? If so, wait on
449 spin_unlock(&lockres
->l_lock
);
451 user_wait_on_busy_lock(lockres
);
455 if ((lockres
->l_flags
& USER_LOCK_BLOCKED
) &&
456 (!user_may_continue_on_blocked_lock(lockres
, level
))) {
457 /* is the lock is currently blocked on behalf of
459 spin_unlock(&lockres
->l_lock
);
461 user_wait_on_blocked_lock(lockres
);
465 if (level
> lockres
->l_level
) {
466 local_flags
= lkm_flags
| LKM_VALBLK
;
467 if (lockres
->l_level
!= LKM_IVMODE
)
468 local_flags
|= LKM_CONVERT
;
470 lockres
->l_requested
= level
;
471 lockres
->l_flags
|= USER_LOCK_BUSY
;
472 spin_unlock(&lockres
->l_lock
);
474 BUG_ON(level
== LKM_IVMODE
);
475 BUG_ON(level
== LKM_NLMODE
);
477 mlog(0, "lock %s, get lock from %d to level = %d\n",
478 lockres
->l_name
, lockres
->l_level
, level
);
480 /* call dlm_lock to upgrade lock now */
481 status
= dlmlock(dlm
,
489 if (status
!= DLM_NORMAL
) {
490 if ((lkm_flags
& LKM_NOQUEUE
) &&
491 (status
== DLM_NOTQUEUED
))
494 user_log_dlm_error("dlmlock", status
, lockres
);
497 user_recover_from_dlm_error(lockres
);
501 mlog(0, "lock %s, successfull return from dlmlock\n",
504 user_wait_on_busy_lock(lockres
);
508 user_dlm_inc_holders(lockres
, level
);
509 spin_unlock(&lockres
->l_lock
);
511 mlog(0, "lockres %s: Got %s lock!\n", lockres
->l_name
,
512 (level
== LKM_EXMODE
) ? "LKM_EXMODE" : "LKM_PRMODE");
519 static inline void user_dlm_dec_holders(struct user_lock_res
*lockres
,
524 BUG_ON(!lockres
->l_ex_holders
);
525 lockres
->l_ex_holders
--;
528 BUG_ON(!lockres
->l_ro_holders
);
529 lockres
->l_ro_holders
--;
536 void user_dlm_cluster_unlock(struct user_lock_res
*lockres
,
539 if (level
!= LKM_EXMODE
&&
540 level
!= LKM_PRMODE
) {
541 mlog(ML_ERROR
, "lockres %s: invalid request!\n", lockres
->l_name
);
545 mlog(0, "lockres %s: dropping %s lock\n", lockres
->l_name
,
546 (level
== LKM_EXMODE
) ? "LKM_EXMODE" : "LKM_PRMODE");
548 spin_lock(&lockres
->l_lock
);
549 user_dlm_dec_holders(lockres
, level
);
550 __user_dlm_cond_queue_lockres(lockres
);
551 spin_unlock(&lockres
->l_lock
);
554 void user_dlm_write_lvb(struct inode
*inode
,
558 struct user_lock_res
*lockres
= &DLMFS_I(inode
)->ip_lockres
;
559 char *lvb
= lockres
->l_lksb
.lvb
;
561 BUG_ON(len
> DLM_LVB_LEN
);
563 spin_lock(&lockres
->l_lock
);
565 BUG_ON(lockres
->l_level
< LKM_EXMODE
);
566 memcpy(lvb
, val
, len
);
568 spin_unlock(&lockres
->l_lock
);
571 void user_dlm_read_lvb(struct inode
*inode
,
575 struct user_lock_res
*lockres
= &DLMFS_I(inode
)->ip_lockres
;
576 char *lvb
= lockres
->l_lksb
.lvb
;
578 BUG_ON(len
> DLM_LVB_LEN
);
580 spin_lock(&lockres
->l_lock
);
582 BUG_ON(lockres
->l_level
< LKM_PRMODE
);
583 memcpy(val
, lvb
, len
);
585 spin_unlock(&lockres
->l_lock
);
588 void user_dlm_lock_res_init(struct user_lock_res
*lockres
,
589 struct dentry
*dentry
)
591 memset(lockres
, 0, sizeof(*lockres
));
593 spin_lock_init(&lockres
->l_lock
);
594 init_waitqueue_head(&lockres
->l_event
);
595 lockres
->l_level
= LKM_IVMODE
;
596 lockres
->l_requested
= LKM_IVMODE
;
597 lockres
->l_blocking
= LKM_IVMODE
;
599 /* should have been checked before getting here. */
600 BUG_ON(dentry
->d_name
.len
>= USER_DLM_LOCK_ID_MAX_LEN
);
602 memcpy(lockres
->l_name
,
607 int user_dlm_destroy_lock(struct user_lock_res
*lockres
)
610 struct dlm_ctxt
*dlm
= dlm_ctxt_from_user_lockres(lockres
);
612 mlog(0, "asked to destroy %s\n", lockres
->l_name
);
614 spin_lock(&lockres
->l_lock
);
615 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
) {
616 mlog(0, "Lock is already torn down\n");
617 spin_unlock(&lockres
->l_lock
);
621 lockres
->l_flags
|= USER_LOCK_IN_TEARDOWN
;
623 while (lockres
->l_flags
& USER_LOCK_BUSY
) {
624 spin_unlock(&lockres
->l_lock
);
626 mlog(0, "lock %s is busy\n", lockres
->l_name
);
628 user_wait_on_busy_lock(lockres
);
630 spin_lock(&lockres
->l_lock
);
633 if (lockres
->l_ro_holders
|| lockres
->l_ex_holders
) {
634 spin_unlock(&lockres
->l_lock
);
635 mlog(0, "lock %s has holders\n", lockres
->l_name
);
640 if (!(lockres
->l_flags
& USER_LOCK_ATTACHED
)) {
641 spin_unlock(&lockres
->l_lock
);
642 mlog(0, "lock %s is not attached\n", lockres
->l_name
);
646 lockres
->l_flags
&= ~USER_LOCK_ATTACHED
;
647 lockres
->l_flags
|= USER_LOCK_BUSY
;
648 spin_unlock(&lockres
->l_lock
);
650 mlog(0, "unlocking lockres %s\n", lockres
->l_name
);
651 status
= dlmunlock(dlm
,
656 if (status
!= DLM_NORMAL
) {
657 user_log_dlm_error("dlmunlock", status
, lockres
);
662 user_wait_on_busy_lock(lockres
);
669 struct dlm_ctxt
*user_dlm_register_context(struct qstr
*name
)
671 struct dlm_ctxt
*dlm
;
675 domain
= kmalloc(name
->len
+ 1, GFP_NOFS
);
678 return ERR_PTR(-ENOMEM
);
681 dlm_key
= crc32_le(0, name
->name
, name
->len
);
683 snprintf(domain
, name
->len
+ 1, "%.*s", name
->len
, name
->name
);
685 dlm
= dlm_register_domain(domain
, dlm_key
);
687 mlog_errno(PTR_ERR(dlm
));
693 void user_dlm_unregister_context(struct dlm_ctxt
*dlm
)
695 dlm_unregister_domain(dlm
);