1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Code which implements the kernel side of a minimal userspace
6 * interface to our DLM.
8 * Many of the functions here are pared down versions of dlmglue.c
11 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
14 #include <linux/signal.h>
15 #include <linux/sched/signal.h>
17 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/crc32.h>
22 #include "../ocfs2_lockingver.h"
23 #include "../stackglue.h"
26 #define MLOG_MASK_PREFIX ML_DLMFS
27 #include "../cluster/masklog.h"
30 static inline struct user_lock_res
*user_lksb_to_lock_res(struct ocfs2_dlm_lksb
*lksb
)
32 return container_of(lksb
, struct user_lock_res
, l_lksb
);
35 static inline int user_check_wait_flag(struct user_lock_res
*lockres
,
40 spin_lock(&lockres
->l_lock
);
41 ret
= lockres
->l_flags
& flag
;
42 spin_unlock(&lockres
->l_lock
);
47 static inline void user_wait_on_busy_lock(struct user_lock_res
*lockres
)
50 wait_event(lockres
->l_event
,
51 !user_check_wait_flag(lockres
, USER_LOCK_BUSY
));
54 static inline void user_wait_on_blocked_lock(struct user_lock_res
*lockres
)
57 wait_event(lockres
->l_event
,
58 !user_check_wait_flag(lockres
, USER_LOCK_BLOCKED
));
61 /* I heart container_of... */
62 static inline struct ocfs2_cluster_connection
*
63 cluster_connection_from_user_lockres(struct user_lock_res
*lockres
)
65 struct dlmfs_inode_private
*ip
;
67 ip
= container_of(lockres
,
68 struct dlmfs_inode_private
,
74 user_dlm_inode_from_user_lockres(struct user_lock_res
*lockres
)
76 struct dlmfs_inode_private
*ip
;
78 ip
= container_of(lockres
,
79 struct dlmfs_inode_private
,
81 return &ip
->ip_vfs_inode
;
84 static inline void user_recover_from_dlm_error(struct user_lock_res
*lockres
)
86 spin_lock(&lockres
->l_lock
);
87 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
88 spin_unlock(&lockres
->l_lock
);
91 #define user_log_dlm_error(_func, _stat, _lockres) do { \
92 mlog(ML_ERROR, "Dlm error %d while calling %s on " \
93 "resource %.*s\n", _stat, _func, \
94 _lockres->l_namelen, _lockres->l_name); \
97 /* WARNING: This function lives in a world where the only three lock
98 * levels are EX, PR, and NL. It *will* have to be adjusted when more
99 * lock types are added. */
100 static inline int user_highest_compat_lock_level(int level
)
102 int new_level
= DLM_LOCK_EX
;
104 if (level
== DLM_LOCK_EX
)
105 new_level
= DLM_LOCK_NL
;
106 else if (level
== DLM_LOCK_PR
)
107 new_level
= DLM_LOCK_PR
;
111 static void user_ast(struct ocfs2_dlm_lksb
*lksb
)
113 struct user_lock_res
*lockres
= user_lksb_to_lock_res(lksb
);
116 mlog(ML_BASTS
, "AST fired for lockres %.*s, level %d => %d\n",
117 lockres
->l_namelen
, lockres
->l_name
, lockres
->l_level
,
118 lockres
->l_requested
);
120 spin_lock(&lockres
->l_lock
);
122 status
= ocfs2_dlm_lock_status(&lockres
->l_lksb
);
124 mlog(ML_ERROR
, "lksb status value of %u on lockres %.*s\n",
125 status
, lockres
->l_namelen
, lockres
->l_name
);
126 spin_unlock(&lockres
->l_lock
);
130 mlog_bug_on_msg(lockres
->l_requested
== DLM_LOCK_IV
,
131 "Lockres %.*s, requested ivmode. flags 0x%x\n",
132 lockres
->l_namelen
, lockres
->l_name
, lockres
->l_flags
);
134 /* we're downconverting. */
135 if (lockres
->l_requested
< lockres
->l_level
) {
136 if (lockres
->l_requested
<=
137 user_highest_compat_lock_level(lockres
->l_blocking
)) {
138 lockres
->l_blocking
= DLM_LOCK_NL
;
139 lockres
->l_flags
&= ~USER_LOCK_BLOCKED
;
143 lockres
->l_level
= lockres
->l_requested
;
144 lockres
->l_requested
= DLM_LOCK_IV
;
145 lockres
->l_flags
|= USER_LOCK_ATTACHED
;
146 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
148 spin_unlock(&lockres
->l_lock
);
150 wake_up(&lockres
->l_event
);
153 static inline void user_dlm_grab_inode_ref(struct user_lock_res
*lockres
)
156 inode
= user_dlm_inode_from_user_lockres(lockres
);
161 static void user_dlm_unblock_lock(struct work_struct
*work
);
163 static void __user_dlm_queue_lockres(struct user_lock_res
*lockres
)
165 if (!(lockres
->l_flags
& USER_LOCK_QUEUED
)) {
166 user_dlm_grab_inode_ref(lockres
);
168 INIT_WORK(&lockres
->l_work
, user_dlm_unblock_lock
);
170 queue_work(user_dlm_worker
, &lockres
->l_work
);
171 lockres
->l_flags
|= USER_LOCK_QUEUED
;
175 static void __user_dlm_cond_queue_lockres(struct user_lock_res
*lockres
)
179 if (!(lockres
->l_flags
& USER_LOCK_BLOCKED
))
182 switch (lockres
->l_blocking
) {
184 if (!lockres
->l_ex_holders
&& !lockres
->l_ro_holders
)
188 if (!lockres
->l_ex_holders
)
196 __user_dlm_queue_lockres(lockres
);
199 static void user_bast(struct ocfs2_dlm_lksb
*lksb
, int level
)
201 struct user_lock_res
*lockres
= user_lksb_to_lock_res(lksb
);
203 mlog(ML_BASTS
, "BAST fired for lockres %.*s, blocking %d, level %d\n",
204 lockres
->l_namelen
, lockres
->l_name
, level
, lockres
->l_level
);
206 spin_lock(&lockres
->l_lock
);
207 lockres
->l_flags
|= USER_LOCK_BLOCKED
;
208 if (level
> lockres
->l_blocking
)
209 lockres
->l_blocking
= level
;
211 __user_dlm_queue_lockres(lockres
);
212 spin_unlock(&lockres
->l_lock
);
214 wake_up(&lockres
->l_event
);
217 static void user_unlock_ast(struct ocfs2_dlm_lksb
*lksb
, int status
)
219 struct user_lock_res
*lockres
= user_lksb_to_lock_res(lksb
);
221 mlog(ML_BASTS
, "UNLOCK AST fired for lockres %.*s, flags 0x%x\n",
222 lockres
->l_namelen
, lockres
->l_name
, lockres
->l_flags
);
225 mlog(ML_ERROR
, "dlm returns status %d\n", status
);
227 spin_lock(&lockres
->l_lock
);
228 /* The teardown flag gets set early during the unlock process,
229 * so test the cancel flag to make sure that this ast isn't
230 * for a concurrent cancel. */
231 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
232 && !(lockres
->l_flags
& USER_LOCK_IN_CANCEL
)) {
233 lockres
->l_level
= DLM_LOCK_IV
;
234 } else if (status
== DLM_CANCELGRANT
) {
235 /* We tried to cancel a convert request, but it was
236 * already granted. Don't clear the busy flag - the
237 * ast should've done this already. */
238 BUG_ON(!(lockres
->l_flags
& USER_LOCK_IN_CANCEL
));
239 lockres
->l_flags
&= ~USER_LOCK_IN_CANCEL
;
242 BUG_ON(!(lockres
->l_flags
& USER_LOCK_IN_CANCEL
));
243 /* Cancel succeeded, we want to re-queue */
244 lockres
->l_requested
= DLM_LOCK_IV
; /* cancel an
247 lockres
->l_flags
&= ~USER_LOCK_IN_CANCEL
;
248 /* we want the unblock thread to look at it again
250 if (lockres
->l_flags
& USER_LOCK_BLOCKED
)
251 __user_dlm_queue_lockres(lockres
);
254 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
256 spin_unlock(&lockres
->l_lock
);
258 wake_up(&lockres
->l_event
);
262 * This is the userdlmfs locking protocol version.
264 * See fs/ocfs2/dlmglue.c for more details on locking versions.
266 static struct ocfs2_locking_protocol user_dlm_lproto
= {
268 .pv_major
= OCFS2_LOCKING_PROTOCOL_MAJOR
,
269 .pv_minor
= OCFS2_LOCKING_PROTOCOL_MINOR
,
271 .lp_lock_ast
= user_ast
,
272 .lp_blocking_ast
= user_bast
,
273 .lp_unlock_ast
= user_unlock_ast
,
276 static inline void user_dlm_drop_inode_ref(struct user_lock_res
*lockres
)
279 inode
= user_dlm_inode_from_user_lockres(lockres
);
283 static void user_dlm_unblock_lock(struct work_struct
*work
)
285 int new_level
, status
;
286 struct user_lock_res
*lockres
=
287 container_of(work
, struct user_lock_res
, l_work
);
288 struct ocfs2_cluster_connection
*conn
=
289 cluster_connection_from_user_lockres(lockres
);
291 mlog(0, "lockres %.*s\n", lockres
->l_namelen
, lockres
->l_name
);
293 spin_lock(&lockres
->l_lock
);
295 mlog_bug_on_msg(!(lockres
->l_flags
& USER_LOCK_QUEUED
),
296 "Lockres %.*s, flags 0x%x\n",
297 lockres
->l_namelen
, lockres
->l_name
, lockres
->l_flags
);
299 /* notice that we don't clear USER_LOCK_BLOCKED here. If it's
300 * set, we want user_ast clear it. */
301 lockres
->l_flags
&= ~USER_LOCK_QUEUED
;
303 /* It's valid to get here and no longer be blocked - if we get
304 * several basts in a row, we might be queued by the first
305 * one, the unblock thread might run and clear the queued
306 * flag, and finally we might get another bast which re-queues
307 * us before our ast for the downconvert is called. */
308 if (!(lockres
->l_flags
& USER_LOCK_BLOCKED
)) {
309 mlog(ML_BASTS
, "lockres %.*s USER_LOCK_BLOCKED\n",
310 lockres
->l_namelen
, lockres
->l_name
);
311 spin_unlock(&lockres
->l_lock
);
315 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
) {
316 mlog(ML_BASTS
, "lockres %.*s USER_LOCK_IN_TEARDOWN\n",
317 lockres
->l_namelen
, lockres
->l_name
);
318 spin_unlock(&lockres
->l_lock
);
322 if (lockres
->l_flags
& USER_LOCK_BUSY
) {
323 if (lockres
->l_flags
& USER_LOCK_IN_CANCEL
) {
324 mlog(ML_BASTS
, "lockres %.*s USER_LOCK_IN_CANCEL\n",
325 lockres
->l_namelen
, lockres
->l_name
);
326 spin_unlock(&lockres
->l_lock
);
330 lockres
->l_flags
|= USER_LOCK_IN_CANCEL
;
331 spin_unlock(&lockres
->l_lock
);
333 status
= ocfs2_dlm_unlock(conn
, &lockres
->l_lksb
,
336 user_log_dlm_error("ocfs2_dlm_unlock", status
, lockres
);
340 /* If there are still incompat holders, we can exit safely
341 * without worrying about re-queueing this lock as that will
342 * happen on the last call to user_cluster_unlock. */
343 if ((lockres
->l_blocking
== DLM_LOCK_EX
)
344 && (lockres
->l_ex_holders
|| lockres
->l_ro_holders
)) {
345 spin_unlock(&lockres
->l_lock
);
346 mlog(ML_BASTS
, "lockres %.*s, EX/PR Holders %u,%u\n",
347 lockres
->l_namelen
, lockres
->l_name
,
348 lockres
->l_ex_holders
, lockres
->l_ro_holders
);
352 if ((lockres
->l_blocking
== DLM_LOCK_PR
)
353 && lockres
->l_ex_holders
) {
354 spin_unlock(&lockres
->l_lock
);
355 mlog(ML_BASTS
, "lockres %.*s, EX Holders %u\n",
356 lockres
->l_namelen
, lockres
->l_name
,
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(ML_BASTS
, "lockres %.*s, downconvert %d => %d\n",
366 lockres
->l_namelen
, lockres
->l_name
, lockres
->l_level
, new_level
);
367 spin_unlock(&lockres
->l_lock
);
369 /* need lock downconvert request now... */
370 status
= ocfs2_dlm_lock(conn
, new_level
, &lockres
->l_lksb
,
371 DLM_LKF_CONVERT
|DLM_LKF_VALBLK
,
375 user_log_dlm_error("ocfs2_dlm_lock", status
, lockres
);
376 user_recover_from_dlm_error(lockres
);
380 user_dlm_drop_inode_ref(lockres
);
383 static inline void user_dlm_inc_holders(struct user_lock_res
*lockres
,
388 lockres
->l_ex_holders
++;
391 lockres
->l_ro_holders
++;
398 /* predict what lock level we'll be dropping down to on behalf
399 * of another node, and return true if the currently wanted
400 * level will be compatible with it. */
402 user_may_continue_on_blocked_lock(struct user_lock_res
*lockres
,
405 BUG_ON(!(lockres
->l_flags
& USER_LOCK_BLOCKED
));
407 return wanted
<= user_highest_compat_lock_level(lockres
->l_blocking
);
410 int user_dlm_cluster_lock(struct user_lock_res
*lockres
,
414 int status
, local_flags
;
415 struct ocfs2_cluster_connection
*conn
=
416 cluster_connection_from_user_lockres(lockres
);
418 if (level
!= DLM_LOCK_EX
&&
419 level
!= DLM_LOCK_PR
) {
420 mlog(ML_ERROR
, "lockres %.*s: invalid request!\n",
421 lockres
->l_namelen
, lockres
->l_name
);
426 mlog(ML_BASTS
, "lockres %.*s, level %d, flags = 0x%x\n",
427 lockres
->l_namelen
, lockres
->l_name
, level
, lkm_flags
);
430 if (signal_pending(current
)) {
431 status
= -ERESTARTSYS
;
435 spin_lock(&lockres
->l_lock
);
436 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
) {
437 spin_unlock(&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
| DLM_LKF_VALBLK
;
467 if (lockres
->l_level
!= DLM_LOCK_IV
)
468 local_flags
|= DLM_LKF_CONVERT
;
470 lockres
->l_requested
= level
;
471 lockres
->l_flags
|= USER_LOCK_BUSY
;
472 spin_unlock(&lockres
->l_lock
);
474 BUG_ON(level
== DLM_LOCK_IV
);
475 BUG_ON(level
== DLM_LOCK_NL
);
477 /* call dlm_lock to upgrade lock now */
478 status
= ocfs2_dlm_lock(conn
, level
, &lockres
->l_lksb
,
479 local_flags
, lockres
->l_name
,
482 if ((lkm_flags
& DLM_LKF_NOQUEUE
) &&
484 user_log_dlm_error("ocfs2_dlm_lock",
486 user_recover_from_dlm_error(lockres
);
490 user_wait_on_busy_lock(lockres
);
494 user_dlm_inc_holders(lockres
, level
);
495 spin_unlock(&lockres
->l_lock
);
502 static inline void user_dlm_dec_holders(struct user_lock_res
*lockres
,
507 BUG_ON(!lockres
->l_ex_holders
);
508 lockres
->l_ex_holders
--;
511 BUG_ON(!lockres
->l_ro_holders
);
512 lockres
->l_ro_holders
--;
519 void user_dlm_cluster_unlock(struct user_lock_res
*lockres
,
522 if (level
!= DLM_LOCK_EX
&&
523 level
!= DLM_LOCK_PR
) {
524 mlog(ML_ERROR
, "lockres %.*s: invalid request!\n",
525 lockres
->l_namelen
, lockres
->l_name
);
529 spin_lock(&lockres
->l_lock
);
530 user_dlm_dec_holders(lockres
, level
);
531 __user_dlm_cond_queue_lockres(lockres
);
532 spin_unlock(&lockres
->l_lock
);
535 void user_dlm_write_lvb(struct inode
*inode
,
539 struct user_lock_res
*lockres
= &DLMFS_I(inode
)->ip_lockres
;
542 BUG_ON(len
> DLM_LVB_LEN
);
544 spin_lock(&lockres
->l_lock
);
546 BUG_ON(lockres
->l_level
< DLM_LOCK_EX
);
547 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
548 memcpy(lvb
, val
, len
);
550 spin_unlock(&lockres
->l_lock
);
553 bool user_dlm_read_lvb(struct inode
*inode
, char *val
)
555 struct user_lock_res
*lockres
= &DLMFS_I(inode
)->ip_lockres
;
559 spin_lock(&lockres
->l_lock
);
561 BUG_ON(lockres
->l_level
< DLM_LOCK_PR
);
562 if (ocfs2_dlm_lvb_valid(&lockres
->l_lksb
)) {
563 lvb
= ocfs2_dlm_lvb(&lockres
->l_lksb
);
564 memcpy(val
, lvb
, DLM_LVB_LEN
);
568 spin_unlock(&lockres
->l_lock
);
572 void user_dlm_lock_res_init(struct user_lock_res
*lockres
,
573 struct dentry
*dentry
)
575 memset(lockres
, 0, sizeof(*lockres
));
577 spin_lock_init(&lockres
->l_lock
);
578 init_waitqueue_head(&lockres
->l_event
);
579 lockres
->l_level
= DLM_LOCK_IV
;
580 lockres
->l_requested
= DLM_LOCK_IV
;
581 lockres
->l_blocking
= DLM_LOCK_IV
;
583 /* should have been checked before getting here. */
584 BUG_ON(dentry
->d_name
.len
>= USER_DLM_LOCK_ID_MAX_LEN
);
586 memcpy(lockres
->l_name
,
589 lockres
->l_namelen
= dentry
->d_name
.len
;
592 int user_dlm_destroy_lock(struct user_lock_res
*lockres
)
595 struct ocfs2_cluster_connection
*conn
=
596 cluster_connection_from_user_lockres(lockres
);
598 mlog(ML_BASTS
, "lockres %.*s\n", lockres
->l_namelen
, lockres
->l_name
);
600 spin_lock(&lockres
->l_lock
);
601 if (lockres
->l_flags
& USER_LOCK_IN_TEARDOWN
) {
602 spin_unlock(&lockres
->l_lock
);
606 lockres
->l_flags
|= USER_LOCK_IN_TEARDOWN
;
608 while (lockres
->l_flags
& USER_LOCK_BUSY
) {
609 spin_unlock(&lockres
->l_lock
);
611 user_wait_on_busy_lock(lockres
);
613 spin_lock(&lockres
->l_lock
);
616 if (lockres
->l_ro_holders
|| lockres
->l_ex_holders
) {
617 lockres
->l_flags
&= ~USER_LOCK_IN_TEARDOWN
;
618 spin_unlock(&lockres
->l_lock
);
623 if (!(lockres
->l_flags
& USER_LOCK_ATTACHED
)) {
625 * lock is never requested, leave USER_LOCK_IN_TEARDOWN set
626 * to avoid new lock request coming in.
628 spin_unlock(&lockres
->l_lock
);
632 lockres
->l_flags
|= USER_LOCK_BUSY
;
633 spin_unlock(&lockres
->l_lock
);
635 status
= ocfs2_dlm_unlock(conn
, &lockres
->l_lksb
, DLM_LKF_VALBLK
);
637 spin_lock(&lockres
->l_lock
);
638 lockres
->l_flags
&= ~USER_LOCK_IN_TEARDOWN
;
639 lockres
->l_flags
&= ~USER_LOCK_BUSY
;
640 spin_unlock(&lockres
->l_lock
);
641 user_log_dlm_error("ocfs2_dlm_unlock", status
, lockres
);
645 user_wait_on_busy_lock(lockres
);
652 static void user_dlm_recovery_handler_noop(int node_num
,
655 /* We ignore recovery events */
659 void user_dlm_set_locking_protocol(void)
661 ocfs2_stack_glue_set_max_proto_version(&user_dlm_lproto
.lp_max_version
);
664 struct ocfs2_cluster_connection
*user_dlm_register(const struct qstr
*name
)
667 struct ocfs2_cluster_connection
*conn
;
669 rc
= ocfs2_cluster_connect_agnostic(name
->name
, name
->len
,
671 user_dlm_recovery_handler_noop
,
676 return rc
? ERR_PTR(rc
) : conn
;
679 void user_dlm_unregister(struct ocfs2_cluster_connection
*conn
)
681 ocfs2_cluster_disconnect(conn
, 0);